Skip to content

Commit

Permalink
Sync D3D12 changes from RavEngine
Browse files Browse the repository at this point in the history
~ fix some issues with resource barriers
~ disable reflection due to dependency on dxcompiler.dll
  • Loading branch information
Ravbug committed Oct 15, 2023
1 parent a6d6aeb commit 2f65e87
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
39 changes: 20 additions & 19 deletions src/D3D12CommandBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,40 +140,34 @@ namespace RGL {
const auto layout = currentRenderPipeline->pipelineLayout;
const auto bindPoint = layout->slotForBufferIdx(bindingOffset);

D3D12_RESOURCE_STATES newState;
bool wasWritten = false;
if (layout->bufferIdxIsUAV(bindingOffset)) {
bool isUAV = layout->bufferIdxIsUAV(bindingOffset);

SyncIfNeeded(static_cast<const BufferD3D12*>(buffer.get()), isUAV ? D3D12_RESOURCE_STATE_UNORDERED_ACCESS : D3D12_RESOURCE_STATE_GENERIC_READ, isUAV);

if (isUAV) {
commandList->SetGraphicsRootUnorderedAccessView(bindPoint, casted->vertexBufferView.BufferLocation + offsetIntoBuffer);
newState = D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
wasWritten = true;
}
else {
commandList->SetGraphicsRootShaderResourceView(bindPoint, casted->vertexBufferView.BufferLocation + offsetIntoBuffer);
newState = D3D12_RESOURCE_STATE_GENERIC_READ;
wasWritten = false;
}
SyncIfNeeded(static_cast<const BufferD3D12*>(buffer.get()), newState, wasWritten);
}
void CommandBufferD3D12::BindComputeBuffer(RGLBufferPtr buffer, uint32_t bindingOffset, uint32_t offsetIntoBuffer)
{

auto casted = std::static_pointer_cast<BufferD3D12>(buffer);
const auto currentLayout = currentComputePipeline->pipelineLayout;
const auto slotidx = currentLayout->slotForBufferIdx(bindingOffset);

bool isUAV = currentLayout->bufferIdxIsUAV(bindingOffset);

SyncIfNeeded(static_cast<const BufferD3D12*>(buffer.get()), isUAV ? D3D12_RESOURCE_STATE_UNORDERED_ACCESS : D3D12_RESOURCE_STATE_GENERIC_READ, isUAV);

D3D12_RESOURCE_STATES newState;
bool wasWritten = false;
if (currentLayout->bufferIdxIsUAV(bindingOffset)) {
if (isUAV) {
commandList->SetComputeRootUnorderedAccessView(slotidx, casted->vertexBufferView.BufferLocation + offsetIntoBuffer);
newState = D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
wasWritten = true;
}
else {
commandList->SetComputeRootShaderResourceView(slotidx, casted->vertexBufferView.BufferLocation + offsetIntoBuffer);
newState = D3D12_RESOURCE_STATE_GENERIC_READ;
wasWritten = false;
}
SyncIfNeeded(static_cast<const BufferD3D12*>(buffer.get()), newState, wasWritten);

}
void CommandBufferD3D12::SetVertexBuffer(RGLBufferPtr buffer, const VertexBufferBinding& bindingInfo)
Expand Down Expand Up @@ -422,6 +416,12 @@ namespace RGL {

// a resource transition is in order
if (buffer->canBeTransitioned && current != needed) {

// is the transition possible?
if (needed == D3D12_RESOURCE_STATE_UNORDERED_ACCESS && !buffer->isWritable) {
goto barrierBail;
}

barriers[nBarriers] = CD3DX12_RESOURCE_BARRIER::Transition(
buffer->buffer.Get(),
current,
Expand All @@ -435,20 +435,21 @@ namespace RGL {

nBarriers++;
}
barrierBail:

if ((*it).second.written && buffer->isWritable) {
// do a simple UAV barrier because access needs to be synchronized here
barriers[nBarriers] = CD3DX12_RESOURCE_BARRIER::UAV(buffer->buffer.Get());
nBarriers++;
}

// update written state
it->second.written = written;

//TODO: barriers of size 1 are inefficient. We should batch these somehow.
if (nBarriers > 0) {
commandList->ResourceBarrier(nBarriers, barriers);
}
// update written state
it->second.written = written;

}
void CommandBufferD3D12::SyncIfNeeded(const TextureD3D12* texture, D3D12_RESOURCE_STATES needed, bool written)
{
Expand Down
2 changes: 2 additions & 0 deletions src/D3D12ShaderLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace RGL {
}
void ShaderLibraryD3D12::ShaderReflect()
{
#ifdef REFL_ENABLED
DxcBuffer reflectionData{
.Ptr = shaderBytecode.pShaderBytecode,
.Size = shaderBytecode.BytecodeLength,
Expand Down Expand Up @@ -91,6 +92,7 @@ namespace RGL {
bufferBindingStore->insert(std::make_pair(i, info));
}
}
#endif
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/RGLD3D12.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <format>
#include <d3d12.h>

//#define REFL_ENABLED

inline void DX_CHECK(HRESULT dx_check_hr) {
RGL::Assert(!FAILED(dx_check_hr), _com_error(dx_check_hr,nullptr).ErrorMessage());
}
Expand Down
5 changes: 5 additions & 0 deletions src/RGLD3D12Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
#include <iostream>
#include <atlbase.h>
#include <dxcapi.h>

#ifdef REFL_ENABLED
#pragma comment(lib, "dxcompiler.lib") // to get access to new compiler api
#endif

using namespace RGL;
using namespace Microsoft::WRL;
Expand Down Expand Up @@ -282,7 +285,9 @@ namespace RGL {
RGL::currentAPI = API::Direct3D12;
EnableDebugLayer();
InitializeAftermath();
#ifdef REFL_ENABLED
DxcCreateInstance(CLSID_DxcUtils, IID_PPV_ARGS(&dxcUtilsPtr));
#endif
}

void RGL::DeintD3D12()
Expand Down

0 comments on commit 2f65e87

Please sign in to comment.