Skip to content

Commit

Permalink
dx12: cleanup draw-indirect PR
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Jul 7, 2024
1 parent 35ebfc4 commit 23e870d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 46 deletions.
9 changes: 9 additions & 0 deletions Engine/gapi/abstractgraphicsapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ void AbstractGraphicsApi::CommandBuffer::barrier(Texture& tex, ResourceAccess pr
barrier(&b,1);
}

void AbstractGraphicsApi::CommandBuffer::barrier(const Buffer& buf, ResourceAccess prev, ResourceAccess next) {
AbstractGraphicsApi::BarrierDesc b;
b.buffer = &buf;
b.prev = prev;
b.next = next;
b.discard = (prev==ResourceAccess::None);
barrier(&b,1);
}

void AbstractGraphicsApi::CommandBuffer::begin(bool tranfer) {
begin();
}
Expand Down
3 changes: 2 additions & 1 deletion Engine/gapi/abstractgraphicsapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ namespace Tempest {
void ssboBarriers(Detail::ResourceState&,PipelineStage){}
};
struct BarrierDesc {
Buffer* buffer = nullptr;
const Buffer* buffer = nullptr;
Texture* texture = nullptr;
Swapchain* swapchain = nullptr;
uint32_t swId = 0;
Expand All @@ -550,6 +550,7 @@ namespace Tempest {

virtual void barrier(const BarrierDesc* desc, size_t cnt) = 0;
virtual void barrier(Texture& tex, ResourceAccess prev, ResourceAccess next, uint32_t mipId);
virtual void barrier(const Buffer& buf, ResourceAccess prev, ResourceAccess next);

virtual void generateMipmap(Texture& image, uint32_t texWidth, uint32_t texHeight, uint32_t mipLevels) = 0;
virtual void copy(Buffer& dest, size_t offset, Texture& src, uint32_t width, uint32_t height, uint32_t mip) = 0;
Expand Down
57 changes: 23 additions & 34 deletions Engine/gapi/directx12/dxcommandbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ static ID3D12Resource* toDxResource(const AbstractGraphicsApi::BarrierDesc& b) {
}

if(b.buffer!=nullptr) {
DxBuffer& buf = *reinterpret_cast<DxBuffer*>(b.buffer);
auto& buf = *reinterpret_cast<const DxBuffer*>(b.buffer);
return buf.impl.get();
}

Expand Down Expand Up @@ -620,8 +620,8 @@ void DxCommandBuffer::implSetUniforms(AbstractGraphicsApi::Desc& u, bool isCompu
}

void DxCommandBuffer::restoreIndirect() {
for(auto& i:indirectCmd) {
issueExplicitIndirectToCommonStateTransition(i);
for(auto i:indirectCmd) {
barrier(*i, ResourceAccess::Indirect, ResourceAccess::UavReadWriteAll);
}
indirectCmd.clear();
}
Expand All @@ -642,9 +642,17 @@ void DxCommandBuffer::barrier(const AbstractGraphicsApi::BarrierDesc* desc, size
barrier.UAV.pResource = nullptr;
}
else if(b.buffer!=nullptr) {
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV;
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
barrier.UAV.pResource = toDxResource(b);
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
barrier.Transition.pResource = toDxResource(b);
barrier.Transition.StateBefore = ::nativeFormat(b.prev);
barrier.Transition.StateAfter = ::nativeFormat(b.next);
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
if(barrier.Transition.StateBefore==barrier.Transition.StateAfter) {
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV;
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
barrier.UAV.pResource = toDxResource(b);
}
}
else {
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
Expand Down Expand Up @@ -743,9 +751,10 @@ void DxCommandBuffer::drawIndirect(const AbstractGraphicsApi::Buffer& indirect,
resState.onUavUsage(ind.nonUniqId, NonUniqResId::I_None, PipelineStage::S_Indirect);
//resState.flush(*this);

if(true && indirectCmd.find(ind.impl.get())==indirectCmd.end()) {
indirectCmd.insert(ind.impl.get());
issueExplicitCommonToIndirectStateTransition(ind.impl.get());
if(indirectCmd.find(&ind)==indirectCmd.end()) {
indirectCmd.insert(&ind);
// UNORDERED_ACCESS to INDIRECT_ARGUMENT (the UMD can assume the UAV writes occurred before the Render Pass)
barrier(indirect, ResourceAccess::UavReadWriteAll, ResourceAccess::Indirect);
}

impl->ExecuteIndirect(sign, 1, ind.impl.get(), UINT64(offset), nullptr, 0);
Expand All @@ -761,10 +770,12 @@ void DxCommandBuffer::dispatchMeshIndirect(const AbstractGraphicsApi::Buffer& in

// block future writers
resState.onUavUsage(ind.nonUniqId, NonUniqResId::I_None, PipelineStage::S_Indirect);
//resState.setLayout(indirect, ResourceAccess::Indirect);

if(true && indirectCmd.find(ind.impl.get())==indirectCmd.end()) {
indirectCmd.insert(ind.impl.get());
issueExplicitCommonToIndirectStateTransition(ind.impl.get());
if(indirectCmd.find(&ind)==indirectCmd.end()) {
indirectCmd.insert(&ind);
// UNORDERED_ACCESS to INDIRECT_ARGUMENT (the UMD can assume the UAV writes occurred before the Render Pass)
barrier(indirect, ResourceAccess::UavReadWriteAll, ResourceAccess::Indirect);
}
impl->ExecuteIndirect(sign, 1, ind.impl.get(), UINT64(offset), nullptr, 0);
}
Expand Down Expand Up @@ -969,28 +980,6 @@ void DxCommandBuffer::copyNative(AbstractGraphicsApi::Buffer& dstBuf, size_t off
impl->CopyTextureRegion(&dstLoc, 0, 0, 0, &srcLoc, nullptr);
}

void DxCommandBuffer::issueExplicitResourceStateTransition(ID3D12Resource* buf, D3D12_RESOURCE_STATES stateBefore, D3D12_RESOURCE_STATES stateAfter)
{
D3D12_RESOURCE_BARRIER barrier;
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
barrier.Transition.pResource = buf;
barrier.Transition.StateAfter = stateAfter;
barrier.Transition.StateBefore = stateBefore;
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
impl->ResourceBarrier(1, &barrier);
}

void DxCommandBuffer::issueExplicitCommonToIndirectStateTransition(ID3D12Resource* buf)
{
issueExplicitResourceStateTransition(buf, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT);
}

void DxCommandBuffer::issueExplicitIndirectToCommonStateTransition(ID3D12Resource* buf)
{
issueExplicitResourceStateTransition(buf, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT, D3D12_RESOURCE_STATE_COMMON);
}

#endif


6 changes: 1 addition & 5 deletions Engine/gapi/directx12/dxcommandbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class DxCommandBuffer:public AbstractGraphicsApi::CommandBuffer {
struct FillUAV;
Stage* stageResources = nullptr;

std::unordered_set<ID3D12Resource*> indirectCmd;
std::unordered_set<const AbstractGraphicsApi::Buffer*> indirectCmd;

void pushChunk();
void newChunk();
Expand All @@ -134,10 +134,6 @@ class DxCommandBuffer:public AbstractGraphicsApi::CommandBuffer {
void pushStage(Stage* cmd);
void implSetUniforms(AbstractGraphicsApi::Desc& u, bool isCompute);
void restoreIndirect();

void issueExplicitResourceStateTransition(ID3D12Resource* buf, D3D12_RESOURCE_STATES stateBefore, D3D12_RESOURCE_STATES stateAfter);
void issueExplicitCommonToIndirectStateTransition(ID3D12Resource* buf);
void issueExplicitIndirectToCommonStateTransition(ID3D12Resource* buf);
};

}
Expand Down
10 changes: 6 additions & 4 deletions Engine/gapi/directx12/dxdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,17 @@ DxDevice::DxDevice(IDXGIAdapter1& adapter, const ApiEntry& dllApi)
{
D3D12_INDIRECT_ARGUMENT_DESC arg = {};
D3D12_COMMAND_SIGNATURE_DESC desc = {};
desc.ByteStride = sizeof(D3D12_DRAW_ARGUMENTS);
desc.NumArgumentDescs = 1;
desc.pArgumentDescs = &arg;

arg.Type = D3D12_INDIRECT_ARGUMENT_TYPE_DRAW;
desc.ByteStride = sizeof(D3D12_DRAW_ARGUMENTS);
desc.NumArgumentDescs = 1;
arg.Type = D3D12_INDIRECT_ARGUMENT_TYPE_DRAW;
dxAssert(device->CreateCommandSignature(&desc, nullptr, uuid<ID3D12CommandSignature>(), reinterpret_cast<void**>(&drawIndirectSgn)));

if(props.meshlets.meshShader) {
arg.Type = D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH_MESH;
desc.ByteStride = sizeof(D3D12_DISPATCH_MESH_ARGUMENTS);
desc.NumArgumentDescs = 1;
arg.Type = D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH_MESH;
dxAssert(device->CreateCommandSignature(&desc, nullptr, uuid<ID3D12CommandSignature>(), reinterpret_cast<void**>(&drawMeshIndirectSgn)));
}

Expand Down
4 changes: 2 additions & 2 deletions Engine/gapi/vulkan/vcommandbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ static VkImage toVkResource(const AbstractGraphicsApi::BarrierDesc& b) {
}

if(b.buffer!=nullptr) {
VBuffer& buf = *reinterpret_cast<VBuffer*>(b.buffer);
auto& buf = *reinterpret_cast<const VBuffer*>(b.buffer);
return buf.impl;
}

Expand Down Expand Up @@ -880,7 +880,7 @@ void VCommandBuffer::barrier(const AbstractGraphicsApi::BarrierDesc* desc, size_
bx.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER_2_KHR;
bx.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
bx.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
bx.buffer = reinterpret_cast<VBuffer&>(*b.buffer).impl;
bx.buffer = reinterpret_cast<const VBuffer&>(*b.buffer).impl;
bx.offset = 0;
bx.size = VK_WHOLE_SIZE;

Expand Down

0 comments on commit 23e870d

Please sign in to comment.