diff --git a/src/backend/d3d12/BindGroupD3D12.cpp b/src/backend/d3d12/BindGroupD3D12.cpp index f572aff9a8..e5b4967328 100644 --- a/src/backend/d3d12/BindGroupD3D12.cpp +++ b/src/backend/d3d12/BindGroupD3D12.cpp @@ -12,23 +12,26 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "common/BitSetIterator.h" #include "backend/d3d12/BindGroupD3D12.h" #include "backend/d3d12/BindGroupLayoutD3D12.h" #include "backend/d3d12/BufferD3D12.h" #include "backend/d3d12/SamplerD3D12.h" #include "backend/d3d12/TextureD3D12.h" +#include "common/BitSetIterator.h" #include "backend/d3d12/D3D12Backend.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { BindGroup::BindGroup(Device* device, BindGroupBuilder* builder) : BindGroupBase(builder), mDevice(device) { } - void BindGroup::RecordDescriptors(const DescriptorHeapHandle &cbvUavSrvHeapStart, uint32_t* cbvUavSrvHeapOffset, const DescriptorHeapHandle &samplerHeapStart, uint32_t* samplerHeapOffset, uint64_t serial) { + void BindGroup::RecordDescriptors(const DescriptorHeapHandle& cbvUavSrvHeapStart, + uint32_t* cbvUavSrvHeapOffset, + const DescriptorHeapHandle& samplerHeapStart, + uint32_t* samplerHeapOffset, + uint64_t serial) { mHeapSerial = serial; const auto* bgl = ToBackend(GetLayout()); @@ -43,34 +46,36 @@ namespace d3d12 { auto d3d12Device = mDevice->GetD3D12Device(); for (uint32_t binding : IterateBitSet(layout.mask)) { switch (layout.types[binding]) { - case nxt::BindingType::UniformBuffer: - { - auto* view = ToBackend(GetBindingAsBufferView(binding)); - auto& cbv = view->GetCBVDescriptor(); - d3d12Device->CreateConstantBufferView(&cbv, cbvUavSrvHeapStart.GetCPUHandle(*cbvUavSrvHeapOffset + bindingOffsets[binding])); - } - break; - case nxt::BindingType::StorageBuffer: - { - auto* view = ToBackend(GetBindingAsBufferView(binding)); - auto& uav = view->GetUAVDescriptor(); - d3d12Device->CreateUnorderedAccessView(ToBackend(view->GetBuffer())->GetD3D12Resource().Get(), nullptr, &uav, cbvUavSrvHeapStart.GetCPUHandle(*cbvUavSrvHeapOffset + bindingOffsets[binding])); - } - break; - case nxt::BindingType::SampledTexture: - { - auto* view = ToBackend(GetBindingAsTextureView(binding)); - auto& srv = view->GetSRVDescriptor(); - d3d12Device->CreateShaderResourceView(ToBackend(view->GetTexture())->GetD3D12Resource(), &srv, cbvUavSrvHeapStart.GetCPUHandle(*cbvUavSrvHeapOffset + bindingOffsets[binding])); - } - break; - case nxt::BindingType::Sampler: - { - auto* sampler = ToBackend(GetBindingAsSampler(binding)); - auto& samplerDesc = sampler->GetSamplerDescriptor(); - d3d12Device->CreateSampler(&samplerDesc, samplerHeapStart.GetCPUHandle(*samplerHeapOffset + bindingOffsets[binding])); - } - break; + case nxt::BindingType::UniformBuffer: { + auto* view = ToBackend(GetBindingAsBufferView(binding)); + auto& cbv = view->GetCBVDescriptor(); + d3d12Device->CreateConstantBufferView( + &cbv, cbvUavSrvHeapStart.GetCPUHandle(*cbvUavSrvHeapOffset + + bindingOffsets[binding])); + } break; + case nxt::BindingType::StorageBuffer: { + auto* view = ToBackend(GetBindingAsBufferView(binding)); + auto& uav = view->GetUAVDescriptor(); + d3d12Device->CreateUnorderedAccessView( + ToBackend(view->GetBuffer())->GetD3D12Resource().Get(), nullptr, &uav, + cbvUavSrvHeapStart.GetCPUHandle(*cbvUavSrvHeapOffset + + bindingOffsets[binding])); + } break; + case nxt::BindingType::SampledTexture: { + auto* view = ToBackend(GetBindingAsTextureView(binding)); + auto& srv = view->GetSRVDescriptor(); + d3d12Device->CreateShaderResourceView( + ToBackend(view->GetTexture())->GetD3D12Resource(), &srv, + cbvUavSrvHeapStart.GetCPUHandle(*cbvUavSrvHeapOffset + + bindingOffsets[binding])); + } break; + case nxt::BindingType::Sampler: { + auto* sampler = ToBackend(GetBindingAsSampler(binding)); + auto& samplerDesc = sampler->GetSamplerDescriptor(); + d3d12Device->CreateSampler( + &samplerDesc, samplerHeapStart.GetCPUHandle(*samplerHeapOffset + + bindingOffsets[binding])); + } break; } } @@ -91,5 +96,4 @@ namespace d3d12 { return mHeapSerial; } -} -} +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/BindGroupD3D12.h b/src/backend/d3d12/BindGroupD3D12.h index 5f366621d0..30d50bd9b3 100644 --- a/src/backend/d3d12/BindGroupD3D12.h +++ b/src/backend/d3d12/BindGroupD3D12.h @@ -21,30 +21,32 @@ #include "backend/d3d12/DescriptorHeapAllocator.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { class Device; class BindGroup : public BindGroupBase { - public: - BindGroup(Device* device, BindGroupBuilder* builder); + public: + BindGroup(Device* device, BindGroupBuilder* builder); - void RecordDescriptors(const DescriptorHeapHandle &cbvSrvUavHeapStart, uint32_t* cbvUavSrvHeapOffset, const DescriptorHeapHandle &samplerHeapStart, uint32_t* samplerHeapOffset, uint64_t serial); - uint32_t GetCbvUavSrvHeapOffset() const; - uint32_t GetSamplerHeapOffset() const; - uint64_t GetHeapSerial() const; + void RecordDescriptors(const DescriptorHeapHandle& cbvSrvUavHeapStart, + uint32_t* cbvUavSrvHeapOffset, + const DescriptorHeapHandle& samplerHeapStart, + uint32_t* samplerHeapOffset, + uint64_t serial); + uint32_t GetCbvUavSrvHeapOffset() const; + uint32_t GetSamplerHeapOffset() const; + uint64_t GetHeapSerial() const; - private: - Device* mDevice; - uint32_t mCbvUavSrvHeapOffset; - uint32_t mSamplerHeapOffset; - uint32_t mCbvUavSrvCount = 0; - uint32_t mSamplerCount = 0; - uint64_t mHeapSerial = 0; + private: + Device* mDevice; + uint32_t mCbvUavSrvHeapOffset; + uint32_t mSamplerHeapOffset; + uint32_t mCbvUavSrvCount = 0; + uint32_t mSamplerCount = 0; + uint64_t mHeapSerial = 0; }; -} -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_BINDGROUPD3D12_H_ +#endif // BACKEND_D3D12_BINDGROUPD3D12_H_ diff --git a/src/backend/d3d12/BindGroupLayoutD3D12.cpp b/src/backend/d3d12/BindGroupLayoutD3D12.cpp index 38b68c077f..ba29fcef6a 100644 --- a/src/backend/d3d12/BindGroupLayoutD3D12.cpp +++ b/src/backend/d3d12/BindGroupLayoutD3D12.cpp @@ -14,15 +14,13 @@ #include "backend/d3d12/BindGroupLayoutD3D12.h" -#include "common/BitSetIterator.h" #include "backend/d3d12/D3D12Backend.h" +#include "common/BitSetIterator.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { BindGroupLayout::BindGroupLayout(Device* device, BindGroupLayoutBuilder* builder) - : BindGroupLayoutBase(builder), mDevice(device), mDescriptorCounts {} { - + : BindGroupLayoutBase(builder), mDevice(device), mDescriptorCounts{} { const auto& groupInfo = GetBindingInfo(); for (uint32_t binding : IterateBitSet(groupInfo.mask)) { @@ -42,7 +40,8 @@ namespace d3d12 { } } - auto SetDescriptorRange = [&](uint32_t index, uint32_t count, D3D12_DESCRIPTOR_RANGE_TYPE type) -> bool { + auto SetDescriptorRange = [&](uint32_t index, uint32_t count, + D3D12_DESCRIPTOR_RANGE_TYPE type) -> bool { if (count == 0) { return false; } @@ -52,7 +51,8 @@ namespace d3d12 { range.NumDescriptors = count; range.RegisterSpace = 0; range.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND; - // These ranges will be copied and range.BaseShaderRegister will be set in d3d12::PipelineLayout to account for bind group register offsets + // These ranges will be copied and range.BaseShaderRegister will be set in + // d3d12::PipelineLayout to account for bind group register offsets return true; }; @@ -60,23 +60,27 @@ namespace d3d12 { // Ranges 0-2 contain the CBV, UAV, and SRV ranges, if they exist, tightly packed // Range 3 contains the Sampler range, if there is one - if (SetDescriptorRange(rangeIndex, mDescriptorCounts[CBV], D3D12_DESCRIPTOR_RANGE_TYPE_CBV)) { + if (SetDescriptorRange(rangeIndex, mDescriptorCounts[CBV], + D3D12_DESCRIPTOR_RANGE_TYPE_CBV)) { rangeIndex++; } - if (SetDescriptorRange(rangeIndex, mDescriptorCounts[UAV], D3D12_DESCRIPTOR_RANGE_TYPE_UAV)) { + if (SetDescriptorRange(rangeIndex, mDescriptorCounts[UAV], + D3D12_DESCRIPTOR_RANGE_TYPE_UAV)) { rangeIndex++; } - if (SetDescriptorRange(rangeIndex, mDescriptorCounts[SRV], D3D12_DESCRIPTOR_RANGE_TYPE_SRV)) { + if (SetDescriptorRange(rangeIndex, mDescriptorCounts[SRV], + D3D12_DESCRIPTOR_RANGE_TYPE_SRV)) { rangeIndex++; } - SetDescriptorRange(Sampler, mDescriptorCounts[Sampler], D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER); + SetDescriptorRange(Sampler, mDescriptorCounts[Sampler], + D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER); // descriptors ranges are offset by the offset + size of the previous range std::array descriptorOffsets; descriptorOffsets[CBV] = 0; descriptorOffsets[UAV] = descriptorOffsets[CBV] + mDescriptorCounts[CBV]; descriptorOffsets[SRV] = descriptorOffsets[UAV] + mDescriptorCounts[UAV]; - descriptorOffsets[Sampler] = 0; // samplers are in a different heap + descriptorOffsets[Sampler] = 0; // samplers are in a different heap for (uint32_t binding : IterateBitSet(groupInfo.mask)) { switch (groupInfo.types[binding]) { @@ -101,11 +105,9 @@ namespace d3d12 { } uint32_t BindGroupLayout::GetCbvUavSrvDescriptorTableSize() const { - return ( - static_cast(mDescriptorCounts[CBV] > 0) + - static_cast(mDescriptorCounts[UAV] > 0) + - static_cast(mDescriptorCounts[SRV] > 0) - ); + return (static_cast(mDescriptorCounts[CBV] > 0) + + static_cast(mDescriptorCounts[UAV] > 0) + + static_cast(mDescriptorCounts[SRV] > 0)); } uint32_t BindGroupLayout::GetSamplerDescriptorTableSize() const { @@ -128,5 +130,4 @@ namespace d3d12 { return &mRanges[Sampler]; } -} -} +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/BindGroupLayoutD3D12.h b/src/backend/d3d12/BindGroupLayoutD3D12.h index c4a57b6388..5ce2aab65b 100644 --- a/src/backend/d3d12/BindGroupLayoutD3D12.h +++ b/src/backend/d3d12/BindGroupLayoutD3D12.h @@ -19,39 +19,37 @@ #include "backend/d3d12/d3d12_platform.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { class Device; class BindGroupLayout : public BindGroupLayoutBase { - public: - BindGroupLayout(Device* device, BindGroupLayoutBuilder* builder); + public: + BindGroupLayout(Device* device, BindGroupLayoutBuilder* builder); - enum DescriptorType { - CBV, - UAV, - SRV, - Sampler, - Count, - }; + enum DescriptorType { + CBV, + UAV, + SRV, + Sampler, + Count, + }; - const std::array& GetBindingOffsets() const; - uint32_t GetCbvUavSrvDescriptorTableSize() const; - uint32_t GetSamplerDescriptorTableSize() const; - uint32_t GetCbvUavSrvDescriptorCount() const; - uint32_t GetSamplerDescriptorCount() const; - const D3D12_DESCRIPTOR_RANGE* GetCbvUavSrvDescriptorRanges() const; - const D3D12_DESCRIPTOR_RANGE* GetSamplerDescriptorRanges() const; + const std::array& GetBindingOffsets() const; + uint32_t GetCbvUavSrvDescriptorTableSize() const; + uint32_t GetSamplerDescriptorTableSize() const; + uint32_t GetCbvUavSrvDescriptorCount() const; + uint32_t GetSamplerDescriptorCount() const; + const D3D12_DESCRIPTOR_RANGE* GetCbvUavSrvDescriptorRanges() const; + const D3D12_DESCRIPTOR_RANGE* GetSamplerDescriptorRanges() const; - private: - Device* mDevice; - std::array mBindingOffsets; - std::array mDescriptorCounts; - D3D12_DESCRIPTOR_RANGE mRanges[DescriptorType::Count]; + private: + Device* mDevice; + std::array mBindingOffsets; + std::array mDescriptorCounts; + D3D12_DESCRIPTOR_RANGE mRanges[DescriptorType::Count]; }; -} -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_BINDGROUPLAYOUTD3D12_H_ +#endif // BACKEND_D3D12_BINDGROUPLAYOUTD3D12_H_ diff --git a/src/backend/d3d12/BlendStateD3D12.cpp b/src/backend/d3d12/BlendStateD3D12.cpp index c0ac790361..d959507f1f 100644 --- a/src/backend/d3d12/BlendStateD3D12.cpp +++ b/src/backend/d3d12/BlendStateD3D12.cpp @@ -17,12 +17,11 @@ #include "backend/d3d12/D3D12Backend.h" #include "common/Assert.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { namespace { D3D12_BLEND D3D12Blend(nxt::BlendFactor factor) { - switch(factor) { + switch (factor) { case nxt::BlendFactor::Zero: return D3D12_BLEND_ZERO; case nxt::BlendFactor::One: @@ -55,7 +54,7 @@ namespace d3d12 { } D3D12_BLEND_OP D3D12BlendOperation(nxt::BlendOperation operation) { - switch(operation) { + switch (operation) { case nxt::BlendOperation::Add: return D3D12_BLEND_OP_ADD; case nxt::BlendOperation::Subtract: @@ -72,13 +71,21 @@ namespace d3d12 { } uint8_t D3D12RenderTargetWriteMask(nxt::ColorWriteMask colorWriteMask) { - static_assert(static_cast(nxt::ColorWriteMask::Red) == D3D12_COLOR_WRITE_ENABLE_RED, "ColorWriteMask values must match"); - static_assert(static_cast(nxt::ColorWriteMask::Green) == D3D12_COLOR_WRITE_ENABLE_GREEN, "ColorWriteMask values must match"); - static_assert(static_cast(nxt::ColorWriteMask::Blue) == D3D12_COLOR_WRITE_ENABLE_BLUE, "ColorWriteMask values must match"); - static_assert(static_cast(nxt::ColorWriteMask::Alpha) == D3D12_COLOR_WRITE_ENABLE_ALPHA, "ColorWriteMask values must match"); + static_assert(static_cast(nxt::ColorWriteMask::Red) == + D3D12_COLOR_WRITE_ENABLE_RED, + "ColorWriteMask values must match"); + static_assert(static_cast(nxt::ColorWriteMask::Green) == + D3D12_COLOR_WRITE_ENABLE_GREEN, + "ColorWriteMask values must match"); + static_assert(static_cast(nxt::ColorWriteMask::Blue) == + D3D12_COLOR_WRITE_ENABLE_BLUE, + "ColorWriteMask values must match"); + static_assert(static_cast(nxt::ColorWriteMask::Alpha) == + D3D12_COLOR_WRITE_ENABLE_ALPHA, + "ColorWriteMask values must match"); return static_cast(colorWriteMask); } - } + } // namespace BlendState::BlendState(BlendStateBuilder* builder) : BlendStateBase(builder) { auto& info = GetBlendInfo(); @@ -98,5 +105,4 @@ namespace d3d12 { return mBlendDesc; } -} -} +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/BlendStateD3D12.h b/src/backend/d3d12/BlendStateD3D12.h index 4569ffbc41..a42db7408a 100644 --- a/src/backend/d3d12/BlendStateD3D12.h +++ b/src/backend/d3d12/BlendStateD3D12.h @@ -19,20 +19,18 @@ #include "backend/d3d12/d3d12_platform.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { class BlendState : public BlendStateBase { - public: - BlendState(BlendStateBuilder* builder); + public: + BlendState(BlendStateBuilder* builder); - const D3D12_RENDER_TARGET_BLEND_DESC& GetD3D12BlendDesc() const; + const D3D12_RENDER_TARGET_BLEND_DESC& GetD3D12BlendDesc() const; - private: - D3D12_RENDER_TARGET_BLEND_DESC mBlendDesc; + private: + D3D12_RENDER_TARGET_BLEND_DESC mBlendDesc; }; -} -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_BLENDSTATED3D12_H_ +#endif // BACKEND_D3D12_BLENDSTATED3D12_H_ diff --git a/src/backend/d3d12/BufferD3D12.cpp b/src/backend/d3d12/BufferD3D12.cpp index a088b4ac66..dbbe402302 100644 --- a/src/backend/d3d12/BufferD3D12.cpp +++ b/src/backend/d3d12/BufferD3D12.cpp @@ -21,8 +21,7 @@ #include "common/Constants.h" #include "common/Math.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { namespace { D3D12_RESOURCE_FLAGS D3D12ResourceFlags(nxt::BufferUsageBit usage) { @@ -66,11 +65,9 @@ namespace d3d12 { return D3D12_HEAP_TYPE_DEFAULT; } } - } - - Buffer::Buffer(Device* device, BufferBuilder* builder) - : BufferBase(builder), mDevice(device) { + } // namespace + Buffer::Buffer(Device* device, BufferBuilder* builder) : BufferBase(builder), mDevice(device) { D3D12_RESOURCE_DESC resourceDescriptor; resourceDescriptor.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; resourceDescriptor.Alignment = 0; @@ -87,17 +84,20 @@ namespace d3d12 { auto heapType = D3D12HeapType(GetAllowedUsage()); auto bufferUsage = D3D12BufferUsage(GetUsage()); - // D3D12 requires buffers on the READBACK heap to have the D3D12_RESOURCE_STATE_COPY_DEST state + // D3D12 requires buffers on the READBACK heap to have the D3D12_RESOURCE_STATE_COPY_DEST + // state if (heapType == D3D12_HEAP_TYPE_READBACK) { bufferUsage |= D3D12_RESOURCE_STATE_COPY_DEST; } - // D3D12 requires buffers on the UPLOAD heap to have the D3D12_RESOURCE_STATE_GENERIC_READ state + // D3D12 requires buffers on the UPLOAD heap to have the D3D12_RESOURCE_STATE_GENERIC_READ + // state if (heapType == D3D12_HEAP_TYPE_UPLOAD) { bufferUsage |= D3D12_RESOURCE_STATE_GENERIC_READ; } - mResource = device->GetResourceAllocator()->Allocate(heapType, resourceDescriptor, bufferUsage); + mResource = + device->GetResourceAllocator()->Allocate(heapType, resourceDescriptor, bufferUsage); } Buffer::~Buffer() { @@ -113,10 +113,13 @@ namespace d3d12 { return mResource; } - bool Buffer::GetResourceTransitionBarrier(nxt::BufferUsageBit currentUsage, nxt::BufferUsageBit targetUsage, D3D12_RESOURCE_BARRIER* barrier) { + bool Buffer::GetResourceTransitionBarrier(nxt::BufferUsageBit currentUsage, + nxt::BufferUsageBit targetUsage, + D3D12_RESOURCE_BARRIER* barrier) { if (GetAllowedUsage() & (nxt::BufferUsageBit::MapRead | nxt::BufferUsageBit::MapWrite)) { - // Transitions are never needed for mapped buffers because they are created with and always need the Transfer(Dst|Src) state. - // Mapped buffers cannot have states outside of (MapRead|TransferDst) and (MapWrite|TransferSrc) + // Transitions are never needed for mapped buffers because they are created with and + // always need the Transfer(Dst|Src) state. Mapped buffers cannot have states outside of + // (MapRead|TransferDst) and (MapWrite|TransferSrc) return false; } @@ -146,11 +149,12 @@ namespace d3d12 { } void Buffer::SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) { - mDevice->GetResourceUploader()->BufferSubData(mResource, start * sizeof(uint32_t), count * sizeof(uint32_t), data); + mDevice->GetResourceUploader()->BufferSubData(mResource, start * sizeof(uint32_t), + count * sizeof(uint32_t), data); } void Buffer::MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t count) { - D3D12_RANGE readRange = { start, start + count }; + D3D12_RANGE readRange = {start, start + count}; char* data = nullptr; ASSERT_SUCCESS(mResource->Map(0, &readRange, reinterpret_cast(&data))); @@ -159,22 +163,22 @@ namespace d3d12 { } void Buffer::UnmapImpl() { - // TODO(enga@google.com): When MapWrite is implemented, this should state the range that was modified + // TODO(enga@google.com): When MapWrite is implemented, this should state the range that was + // modified D3D12_RANGE writeRange = {}; mResource->Unmap(0, &writeRange); mDevice->GetResourceAllocator()->Release(mResource); } - void Buffer::TransitionUsageImpl(nxt::BufferUsageBit currentUsage, nxt::BufferUsageBit targetUsage) { + void Buffer::TransitionUsageImpl(nxt::BufferUsageBit currentUsage, + nxt::BufferUsageBit targetUsage) { D3D12_RESOURCE_BARRIER barrier; if (GetResourceTransitionBarrier(currentUsage, targetUsage, &barrier)) { mDevice->GetPendingCommandList()->ResourceBarrier(1, &barrier); } } - BufferView::BufferView(BufferViewBuilder* builder) - : BufferViewBase(builder) { - + BufferView::BufferView(BufferViewBuilder* builder) : BufferViewBase(builder) { mCbvDesc.BufferLocation = ToBackend(GetBuffer())->GetVA() + GetOffset(); mCbvDesc.SizeInBytes = GetD3D12Size(); @@ -200,8 +204,7 @@ namespace d3d12 { return mUavDesc; } - MapReadRequestTracker::MapReadRequestTracker(Device* device) - : mDevice(device) { + MapReadRequestTracker::MapReadRequestTracker(Device* device) : mDevice(device) { } MapReadRequestTracker::~MapReadRequestTracker() { @@ -224,5 +227,4 @@ namespace d3d12 { mInflightRequests.ClearUpTo(finishedSerial); } -} -} +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/BufferD3D12.h b/src/backend/d3d12/BufferD3D12.h index df9eacd914..fd18a87e1e 100644 --- a/src/backend/d3d12/BufferD3D12.h +++ b/src/backend/d3d12/BufferD3D12.h @@ -20,67 +20,67 @@ #include "backend/d3d12/d3d12_platform.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { class Device; class Buffer : public BufferBase { - public: - Buffer(Device* device, BufferBuilder* builder); - ~Buffer(); + public: + Buffer(Device* device, BufferBuilder* builder); + ~Buffer(); - uint32_t GetD3D12Size() const; - ComPtr GetD3D12Resource(); - D3D12_GPU_VIRTUAL_ADDRESS GetVA() const; - bool GetResourceTransitionBarrier(nxt::BufferUsageBit currentUsage, nxt::BufferUsageBit targetUsage, D3D12_RESOURCE_BARRIER* barrier); - void OnMapReadCommandSerialFinished(uint32_t mapSerial, const void* data); + uint32_t GetD3D12Size() const; + ComPtr GetD3D12Resource(); + D3D12_GPU_VIRTUAL_ADDRESS GetVA() const; + bool GetResourceTransitionBarrier(nxt::BufferUsageBit currentUsage, + nxt::BufferUsageBit targetUsage, + D3D12_RESOURCE_BARRIER* barrier); + void OnMapReadCommandSerialFinished(uint32_t mapSerial, const void* data); - private: - Device* mDevice; - ComPtr mResource; - - // NXT API - void SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) override; - void MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t count) override; - void UnmapImpl() override; - void TransitionUsageImpl(nxt::BufferUsageBit currentUsage, nxt::BufferUsageBit targetUsage) override; + private: + Device* mDevice; + ComPtr mResource; + // NXT API + void SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) override; + void MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t count) override; + void UnmapImpl() override; + void TransitionUsageImpl(nxt::BufferUsageBit currentUsage, + nxt::BufferUsageBit targetUsage) override; }; class BufferView : public BufferViewBase { - public: - BufferView(BufferViewBuilder* builder); + public: + BufferView(BufferViewBuilder* builder); - uint32_t GetD3D12Size() const; - const D3D12_CONSTANT_BUFFER_VIEW_DESC& GetCBVDescriptor() const; - const D3D12_UNORDERED_ACCESS_VIEW_DESC& GetUAVDescriptor() const; + uint32_t GetD3D12Size() const; + const D3D12_CONSTANT_BUFFER_VIEW_DESC& GetCBVDescriptor() const; + const D3D12_UNORDERED_ACCESS_VIEW_DESC& GetUAVDescriptor() const; - private: - D3D12_CONSTANT_BUFFER_VIEW_DESC mCbvDesc; - D3D12_UNORDERED_ACCESS_VIEW_DESC mUavDesc; + private: + D3D12_CONSTANT_BUFFER_VIEW_DESC mCbvDesc; + D3D12_UNORDERED_ACCESS_VIEW_DESC mUavDesc; }; class MapReadRequestTracker { - public: - MapReadRequestTracker(Device* device); - ~MapReadRequestTracker(); + public: + MapReadRequestTracker(Device* device); + ~MapReadRequestTracker(); - void Track(Buffer* buffer, uint32_t mapSerial, const void* data); - void Tick(Serial finishedSerial); + void Track(Buffer* buffer, uint32_t mapSerial, const void* data); + void Tick(Serial finishedSerial); - private: - Device* mDevice; + private: + Device* mDevice; - struct Request { - Ref buffer; - uint32_t mapSerial; - const void* data; - }; - SerialQueue mInflightRequests; + struct Request { + Ref buffer; + uint32_t mapSerial; + const void* data; + }; + SerialQueue mInflightRequests; }; -} -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_BUFFERD3D12_H_ +#endif // BACKEND_D3D12_BUFFERD3D12_H_ diff --git a/src/backend/d3d12/CommandAllocatorManager.cpp b/src/backend/d3d12/CommandAllocatorManager.cpp index 89cf1df3bd..15343bb030 100644 --- a/src/backend/d3d12/CommandAllocatorManager.cpp +++ b/src/backend/d3d12/CommandAllocatorManager.cpp @@ -19,10 +19,10 @@ #include "common/Assert.h" #include "common/BitSetIterator.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { - CommandAllocatorManager::CommandAllocatorManager(Device* device) : device(device), mAllocatorCount(0) { + CommandAllocatorManager::CommandAllocatorManager(Device* device) + : device(device), mAllocatorCount(0) { mFreeAllocators.set(); } @@ -42,14 +42,17 @@ namespace d3d12 { if (firstFreeIndex >= mAllocatorCount) { ASSERT(firstFreeIndex == mAllocatorCount); mAllocatorCount++; - ASSERT_SUCCESS(device->GetD3D12Device()->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&mCommandAllocators[firstFreeIndex]))); + ASSERT_SUCCESS(device->GetD3D12Device()->CreateCommandAllocator( + D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&mCommandAllocators[firstFreeIndex]))); } // Mark the command allocator as used mFreeAllocators.reset(firstFreeIndex); - // Enqueue the command allocator. It will be scheduled for reset after the next ExecuteCommandLists - mInFlightCommandAllocators.Enqueue({mCommandAllocators[firstFreeIndex], firstFreeIndex}, device->GetSerial()); + // Enqueue the command allocator. It will be scheduled for reset after the next + // ExecuteCommandLists + mInFlightCommandAllocators.Enqueue({mCommandAllocators[firstFreeIndex], firstFreeIndex}, + device->GetSerial()); return mCommandAllocators[firstFreeIndex]; } @@ -63,5 +66,4 @@ namespace d3d12 { mInFlightCommandAllocators.ClearUpTo(lastCompletedSerial); } -} -} +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/CommandAllocatorManager.h b/src/backend/d3d12/CommandAllocatorManager.h index 957c435880..17c1df9a84 100644 --- a/src/backend/d3d12/CommandAllocatorManager.h +++ b/src/backend/d3d12/CommandAllocatorManager.h @@ -21,38 +21,36 @@ #include -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { class Device; class CommandAllocatorManager { - public: - CommandAllocatorManager(Device* device); + public: + CommandAllocatorManager(Device* device); - // A CommandAllocator that is reserved must be used on the next ExecuteCommandLists - // otherwise its commands may be reset before execution has completed on the GPU - ComPtr ReserveCommandAllocator(); - void Tick(uint64_t lastCompletedSerial); + // A CommandAllocator that is reserved must be used on the next ExecuteCommandLists + // otherwise its commands may be reset before execution has completed on the GPU + ComPtr ReserveCommandAllocator(); + void Tick(uint64_t lastCompletedSerial); - private: - Device* device; + private: + Device* device; - // This must be at least 2 because the Device and Queue use separate command allocators - static constexpr unsigned int kMaxCommandAllocators = 32; - unsigned int mAllocatorCount; + // This must be at least 2 because the Device and Queue use separate command allocators + static constexpr unsigned int kMaxCommandAllocators = 32; + unsigned int mAllocatorCount; - struct IndexedCommandAllocator { - ComPtr commandAllocator; - unsigned int index; - }; + struct IndexedCommandAllocator { + ComPtr commandAllocator; + unsigned int index; + }; - ComPtr mCommandAllocators[kMaxCommandAllocators]; - std::bitset mFreeAllocators; - SerialQueue mInFlightCommandAllocators; + ComPtr mCommandAllocators[kMaxCommandAllocators]; + std::bitset mFreeAllocators; + SerialQueue mInFlightCommandAllocators; }; -} -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_COMMANDALLOCATORMANAGER_H_ +#endif // BACKEND_D3D12_COMMANDALLOCATORMANAGER_H_ diff --git a/src/backend/d3d12/CommandBufferD3D12.cpp b/src/backend/d3d12/CommandBufferD3D12.cpp index 94420d08e8..7d373f90c3 100644 --- a/src/backend/d3d12/CommandBufferD3D12.cpp +++ b/src/backend/d3d12/CommandBufferD3D12.cpp @@ -15,11 +15,11 @@ #include "backend/d3d12/CommandBufferD3D12.h" #include "backend/Commands.h" -#include "backend/d3d12/D3D12Backend.h" #include "backend/d3d12/BindGroupD3D12.h" #include "backend/d3d12/BindGroupLayoutD3D12.h" #include "backend/d3d12/BufferD3D12.h" #include "backend/d3d12/ComputePipelineD3D12.h" +#include "backend/d3d12/D3D12Backend.h" #include "backend/d3d12/DescriptorHeapAllocator.h" #include "backend/d3d12/FramebufferD3D12.h" #include "backend/d3d12/InputStateD3D12.h" @@ -31,8 +31,7 @@ #include "backend/d3d12/TextureD3D12.h" #include "common/Assert.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { namespace { DXGI_FORMAT DXGIIndexFormat(nxt::IndexFormat format) { @@ -69,10 +68,13 @@ namespace d3d12 { if (bindGroups[index] != group) { bindGroups[index] = group; - // Descriptors don't need to be recorded if they have already been recorded in the heap. Indices are only updated when descriptors are recorded + // Descriptors don't need to be recorded if they have already been recorded in + // the heap. Indices are only updated when descriptors are recorded const uint64_t serial = device->GetSerial(); if (group->GetHeapSerial() != serial) { - group->RecordDescriptors(cbvSrvUavCPUDescriptorHeap, &cbvSrvUavDescriptorIndex, samplerCPUDescriptorHeap, &samplerDescriptorIndex, serial); + group->RecordDescriptors( + cbvSrvUavCPUDescriptorHeap, &cbvSrvUavDescriptorIndex, + samplerCPUDescriptorHeap, &samplerDescriptorIndex, serial); } } } @@ -88,37 +90,54 @@ namespace d3d12 { } } - void SetBindGroup(ComPtr commandList, PipelineLayout* pipelineLayout, BindGroup* group, - uint32_t index, bool force = false) { + void SetBindGroup(ComPtr commandList, + PipelineLayout* pipelineLayout, + BindGroup* group, + uint32_t index, + bool force = false) { if (bindGroups[index] != group || force) { bindGroups[index] = group; - uint32_t cbvUavSrvCount = ToBackend(group->GetLayout())->GetCbvUavSrvDescriptorCount(); - uint32_t samplerCount = ToBackend(group->GetLayout())->GetSamplerDescriptorCount(); + uint32_t cbvUavSrvCount = + ToBackend(group->GetLayout())->GetCbvUavSrvDescriptorCount(); + uint32_t samplerCount = + ToBackend(group->GetLayout())->GetSamplerDescriptorCount(); if (cbvUavSrvCount > 0) { - uint32_t parameterIndex = pipelineLayout->GetCbvUavSrvRootParameterIndex(index); + uint32_t parameterIndex = + pipelineLayout->GetCbvUavSrvRootParameterIndex(index); if (inCompute) { - commandList->SetComputeRootDescriptorTable(parameterIndex, cbvSrvUavGPUDescriptorHeap.GetGPUHandle(group->GetCbvUavSrvHeapOffset())); + commandList->SetComputeRootDescriptorTable( + parameterIndex, cbvSrvUavGPUDescriptorHeap.GetGPUHandle( + group->GetCbvUavSrvHeapOffset())); } else { - commandList->SetGraphicsRootDescriptorTable(parameterIndex, cbvSrvUavGPUDescriptorHeap.GetGPUHandle(group->GetCbvUavSrvHeapOffset())); + commandList->SetGraphicsRootDescriptorTable( + parameterIndex, cbvSrvUavGPUDescriptorHeap.GetGPUHandle( + group->GetCbvUavSrvHeapOffset())); } } if (samplerCount > 0) { - uint32_t parameterIndex = pipelineLayout->GetSamplerRootParameterIndex(index); + uint32_t parameterIndex = + pipelineLayout->GetSamplerRootParameterIndex(index); if (inCompute) { - commandList->SetComputeRootDescriptorTable(parameterIndex, samplerGPUDescriptorHeap.GetGPUHandle(group->GetSamplerHeapOffset())); + commandList->SetComputeRootDescriptorTable( + parameterIndex, samplerGPUDescriptorHeap.GetGPUHandle( + group->GetSamplerHeapOffset())); } else { - commandList->SetGraphicsRootDescriptorTable(parameterIndex, samplerGPUDescriptorHeap.GetGPUHandle(group->GetSamplerHeapOffset())); + commandList->SetGraphicsRootDescriptorTable( + parameterIndex, samplerGPUDescriptorHeap.GetGPUHandle( + group->GetSamplerHeapOffset())); } } } } - void SetInheritedBindGroups(ComPtr commandList, PipelineLayout* oldLayout, PipelineLayout* newLayout) { + void SetInheritedBindGroups(ComPtr commandList, + PipelineLayout* oldLayout, + PipelineLayout* newLayout) { if (oldLayout == nullptr) { return; } @@ -136,13 +155,17 @@ namespace d3d12 { } }; - void AllocateAndSetDescriptorHeaps(Device* device, BindGroupStateTracker* bindingTracker, CommandIterator* commands) { + void AllocateAndSetDescriptorHeaps(Device* device, + BindGroupStateTracker* bindingTracker, + CommandIterator* commands) { auto* descriptorHeapAllocator = device->GetDescriptorHeapAllocator(); // TODO(enga@google.com): This currently allocates CPU heaps of arbitrarily chosen sizes // This will not work if there are too many descriptors - bindingTracker->cbvSrvUavCPUDescriptorHeap = descriptorHeapAllocator->AllocateCPUHeap(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 8192); - bindingTracker->samplerCPUDescriptorHeap = descriptorHeapAllocator->AllocateCPUHeap(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, 2048); + bindingTracker->cbvSrvUavCPUDescriptorHeap = descriptorHeapAllocator->AllocateCPUHeap( + D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 8192); + bindingTracker->samplerCPUDescriptorHeap = + descriptorHeapAllocator->AllocateCPUHeap(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, 2048); { Command type; @@ -150,31 +173,27 @@ namespace d3d12 { while (commands->NextCommandId(&type)) { switch (type) { - case Command::SetComputePipeline: - { - SetComputePipelineCmd* cmd = commands->NextCommand(); + case Command::SetComputePipeline: { + SetComputePipelineCmd* cmd = + commands->NextCommand(); PipelineLayout* layout = ToBackend(cmd->pipeline->GetLayout()); bindingTracker->TrackInheritedGroups(lastLayout, layout); lastLayout = layout; - } - break; + } break; - case Command::SetRenderPipeline: - { - SetRenderPipelineCmd* cmd = commands->NextCommand(); + case Command::SetRenderPipeline: { + SetRenderPipelineCmd* cmd = + commands->NextCommand(); PipelineLayout* layout = ToBackend(cmd->pipeline->GetLayout()); bindingTracker->TrackInheritedGroups(lastLayout, layout); lastLayout = layout; - } - break; + } break; - case Command::SetBindGroup: - { + case Command::SetBindGroup: { SetBindGroupCmd* cmd = commands->NextCommand(); BindGroup* group = ToBackend(cmd->group.Get()); bindingTracker->TrackSetBindGroup(group, cmd->index); - } - break; + } break; default: SkipCommand(commands, type); } @@ -184,8 +203,12 @@ namespace d3d12 { } if (bindingTracker->cbvSrvUavDescriptorIndex > 0) { - // Allocate a GPU-visible heap and copy from the CPU-only heap to the GPU-visible heap - bindingTracker->cbvSrvUavGPUDescriptorHeap = descriptorHeapAllocator->AllocateGPUHeap(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, bindingTracker->cbvSrvUavDescriptorIndex); + // Allocate a GPU-visible heap and copy from the CPU-only heap to the GPU-visible + // heap + bindingTracker->cbvSrvUavGPUDescriptorHeap = + descriptorHeapAllocator->AllocateGPUHeap( + D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, + bindingTracker->cbvSrvUavDescriptorIndex); device->GetD3D12Device()->CopyDescriptorsSimple( bindingTracker->cbvSrvUavDescriptorIndex, bindingTracker->cbvSrvUavGPUDescriptorHeap.GetCPUHandle(0), @@ -194,7 +217,8 @@ namespace d3d12 { } if (bindingTracker->samplerDescriptorIndex > 0) { - bindingTracker->samplerGPUDescriptorHeap = descriptorHeapAllocator->AllocateGPUHeap(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, bindingTracker->samplerDescriptorIndex); + bindingTracker->samplerGPUDescriptorHeap = descriptorHeapAllocator->AllocateGPUHeap( + D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, bindingTracker->samplerDescriptorIndex); device->GetD3D12Device()->CopyDescriptorsSimple( bindingTracker->samplerDescriptorIndex, bindingTracker->samplerGPUDescriptorHeap.GetCPUHandle(0), @@ -202,7 +226,7 @@ namespace d3d12 { D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER); } } - } + } // namespace CommandBuffer::CommandBuffer(Device* device, CommandBufferBuilder* builder) : CommandBufferBase(builder), mDevice(device), mCommands(builder->AcquireCommands()) { @@ -217,7 +241,8 @@ namespace d3d12 { AllocateAndSetDescriptorHeaps(mDevice, &bindingTracker, &mCommands); bindingTracker.Reset(); - ID3D12DescriptorHeap* descriptorHeaps[2] = { bindingTracker.cbvSrvUavGPUDescriptorHeap.Get(), bindingTracker.samplerGPUDescriptorHeap.Get() }; + ID3D12DescriptorHeap* descriptorHeaps[2] = {bindingTracker.cbvSrvUavGPUDescriptorHeap.Get(), + bindingTracker.samplerGPUDescriptorHeap.Get()}; if (descriptorHeaps[0] && descriptorHeaps[1]) { commandList->SetDescriptorHeaps(2, descriptorHeaps); } else if (descriptorHeaps[0]) { @@ -234,393 +259,352 @@ namespace d3d12 { Framebuffer* currentFramebuffer = nullptr; uint32_t currentSubpass = 0; - while(mCommands.NextCommandId(&type)) { + while (mCommands.NextCommandId(&type)) { switch (type) { - case Command::BeginComputePass: - { - mCommands.NextCommand(); - bindingTracker.SetInComputePass(true); + case Command::BeginComputePass: { + mCommands.NextCommand(); + bindingTracker.SetInComputePass(true); + } break; + + case Command::BeginRenderPass: { + BeginRenderPassCmd* beginRenderPassCmd = + mCommands.NextCommand(); + currentRenderPass = ToBackend(beginRenderPassCmd->renderPass.Get()); + currentFramebuffer = ToBackend(beginRenderPassCmd->framebuffer.Get()); + currentSubpass = 0; + + uint32_t width = currentFramebuffer->GetWidth(); + uint32_t height = currentFramebuffer->GetHeight(); + D3D12_VIEWPORT viewport = { + 0.f, 0.f, static_cast(width), static_cast(height), 0.f, 1.f}; + D3D12_RECT scissorRect = {0, 0, static_cast(width), + static_cast(height)}; + commandList->RSSetViewports(1, &viewport); + commandList->RSSetScissorRects(1, &scissorRect); + } break; + + case Command::BeginRenderSubpass: { + mCommands.NextCommand(); + const auto& subpass = currentRenderPass->GetSubpassInfo(currentSubpass); + + Framebuffer::OMSetRenderTargetArgs args = + currentFramebuffer->GetSubpassOMSetRenderTargetArgs(currentSubpass); + if (args.dsv.ptr) { + commandList->OMSetRenderTargets(args.numRTVs, args.RTVs.data(), FALSE, + &args.dsv); + } else { + commandList->OMSetRenderTargets(args.numRTVs, args.RTVs.data(), FALSE, + nullptr); } - break; - case Command::BeginRenderPass: - { - BeginRenderPassCmd* beginRenderPassCmd = mCommands.NextCommand(); - currentRenderPass = ToBackend(beginRenderPassCmd->renderPass.Get()); - currentFramebuffer = ToBackend(beginRenderPassCmd->framebuffer.Get()); - currentSubpass = 0; + // Clear framebuffer attachments as needed - uint32_t width = currentFramebuffer->GetWidth(); - uint32_t height = currentFramebuffer->GetHeight(); - D3D12_VIEWPORT viewport = { 0.f, 0.f, static_cast(width), static_cast(height), 0.f, 1.f }; - D3D12_RECT scissorRect = { 0, 0, static_cast(width), static_cast(height) }; - commandList->RSSetViewports(1, &viewport); - commandList->RSSetScissorRects(1, &scissorRect); - } - break; + for (unsigned int location : IterateBitSet(subpass.colorAttachmentsSet)) { + uint32_t attachmentSlot = subpass.colorAttachments[location]; + const auto& attachmentInfo = + currentRenderPass->GetAttachmentInfo(attachmentSlot); - case Command::BeginRenderSubpass: - { - mCommands.NextCommand(); - const auto& subpass = currentRenderPass->GetSubpassInfo(currentSubpass); - - Framebuffer::OMSetRenderTargetArgs args = currentFramebuffer->GetSubpassOMSetRenderTargetArgs(currentSubpass); - if (args.dsv.ptr) { - commandList->OMSetRenderTargets(args.numRTVs, args.RTVs.data(), FALSE, &args.dsv); - } else { - commandList->OMSetRenderTargets(args.numRTVs, args.RTVs.data(), FALSE, nullptr); + Texture* texture = ToBackend( + currentFramebuffer->GetTextureView(attachmentSlot)->GetTexture()); + constexpr auto usage = nxt::TextureUsageBit::OutputAttachment; + // It's already validated that this texture is either frozen to the correct + // usage, or not frozen. + if (!texture->IsFrozen()) { + texture->TransitionUsageImpl(texture->GetUsage(), usage); + texture->UpdateUsageInternal(usage); } - // Clear framebuffer attachments as needed - - for (unsigned int location : IterateBitSet(subpass.colorAttachmentsSet)) { - uint32_t attachmentSlot = subpass.colorAttachments[location]; - const auto& attachmentInfo = currentRenderPass->GetAttachmentInfo(attachmentSlot); - - Texture* texture = ToBackend(currentFramebuffer->GetTextureView(attachmentSlot)->GetTexture()); - constexpr auto usage = nxt::TextureUsageBit::OutputAttachment; - // It's already validated that this texture is either frozen to the correct usage, or not frozen. - if (!texture->IsFrozen()) { - texture->TransitionUsageImpl(texture->GetUsage(), usage); - texture->UpdateUsageInternal(usage); - } - - // Only perform load op on first use - if (attachmentInfo.firstSubpass == currentSubpass) { - // Load op - color - if (attachmentInfo.colorLoadOp == nxt::LoadOp::Clear) { - auto handle = currentFramebuffer->GetRTVDescriptor(attachmentSlot); - const auto& clear = currentFramebuffer->GetClearColor(attachmentSlot); - commandList->ClearRenderTargetView(handle, clear.color, 0, nullptr); - } + // Only perform load op on first use + if (attachmentInfo.firstSubpass == currentSubpass) { + // Load op - color + if (attachmentInfo.colorLoadOp == nxt::LoadOp::Clear) { + auto handle = currentFramebuffer->GetRTVDescriptor(attachmentSlot); + const auto& clear = + currentFramebuffer->GetClearColor(attachmentSlot); + commandList->ClearRenderTargetView(handle, clear.color, 0, nullptr); } } + } - if (subpass.depthStencilAttachmentSet) { - uint32_t attachmentSlot = subpass.depthStencilAttachment; - const auto& attachmentInfo = currentRenderPass->GetAttachmentInfo(attachmentSlot); + if (subpass.depthStencilAttachmentSet) { + uint32_t attachmentSlot = subpass.depthStencilAttachment; + const auto& attachmentInfo = + currentRenderPass->GetAttachmentInfo(attachmentSlot); - // Only perform load op on first use - if (attachmentInfo.firstSubpass == currentSubpass) { - // Load op - depth/stencil - bool doDepthClear = TextureFormatHasDepth(attachmentInfo.format) && - (attachmentInfo.depthLoadOp == nxt::LoadOp::Clear); - bool doStencilClear = TextureFormatHasStencil(attachmentInfo.format) && - (attachmentInfo.stencilLoadOp == nxt::LoadOp::Clear); + // Only perform load op on first use + if (attachmentInfo.firstSubpass == currentSubpass) { + // Load op - depth/stencil + bool doDepthClear = TextureFormatHasDepth(attachmentInfo.format) && + (attachmentInfo.depthLoadOp == nxt::LoadOp::Clear); + bool doStencilClear = + TextureFormatHasStencil(attachmentInfo.format) && + (attachmentInfo.stencilLoadOp == nxt::LoadOp::Clear); - D3D12_CLEAR_FLAGS clearFlags = {}; - if (doDepthClear) { - clearFlags |= D3D12_CLEAR_FLAG_DEPTH; - } - if (doStencilClear) { - clearFlags |= D3D12_CLEAR_FLAG_STENCIL; - } - if (clearFlags) { - auto handle = currentFramebuffer->GetDSVDescriptor(attachmentSlot); - const auto& clear = currentFramebuffer->GetClearDepthStencil(attachmentSlot); - // TODO(kainino@chromium.org): investigate: should the NXT clear stencil type be uint8_t? - uint8_t clearStencil = static_cast(clear.stencil); - commandList->ClearDepthStencilView(handle, clearFlags, clear.depth, clearStencil, 0, nullptr); - } + D3D12_CLEAR_FLAGS clearFlags = {}; + if (doDepthClear) { + clearFlags |= D3D12_CLEAR_FLAG_DEPTH; + } + if (doStencilClear) { + clearFlags |= D3D12_CLEAR_FLAG_STENCIL; + } + if (clearFlags) { + auto handle = currentFramebuffer->GetDSVDescriptor(attachmentSlot); + const auto& clear = + currentFramebuffer->GetClearDepthStencil(attachmentSlot); + // TODO(kainino@chromium.org): investigate: should the NXT clear + // stencil type be uint8_t? + uint8_t clearStencil = static_cast(clear.stencil); + commandList->ClearDepthStencilView(handle, clearFlags, clear.depth, + clearStencil, 0, nullptr); } } - - static constexpr std::array defaultBlendFactor = { 0, 0, 0, 0 }; - commandList->OMSetBlendFactor(&defaultBlendFactor[0]); } - break; - case Command::CopyBufferToBuffer: - { - CopyBufferToBufferCmd* copy = mCommands.NextCommand(); - auto src = ToBackend(copy->source.buffer.Get())->GetD3D12Resource(); - auto dst = ToBackend(copy->destination.buffer.Get())->GetD3D12Resource(); - commandList->CopyBufferRegion(dst.Get(), copy->destination.offset, src.Get(), copy->source.offset, copy->size); + static constexpr std::array defaultBlendFactor = {0, 0, 0, 0}; + commandList->OMSetBlendFactor(&defaultBlendFactor[0]); + } break; + + case Command::CopyBufferToBuffer: { + CopyBufferToBufferCmd* copy = mCommands.NextCommand(); + auto src = ToBackend(copy->source.buffer.Get())->GetD3D12Resource(); + auto dst = ToBackend(copy->destination.buffer.Get())->GetD3D12Resource(); + commandList->CopyBufferRegion(dst.Get(), copy->destination.offset, src.Get(), + copy->source.offset, copy->size); + } break; + + case Command::CopyBufferToTexture: { + CopyBufferToTextureCmd* copy = mCommands.NextCommand(); + Buffer* buffer = ToBackend(copy->source.buffer.Get()); + Texture* texture = ToBackend(copy->destination.texture.Get()); + + auto copySplit = ComputeTextureCopySplit( + copy->destination.x, copy->destination.y, copy->destination.z, + copy->destination.width, copy->destination.height, copy->destination.depth, + static_cast(TextureFormatPixelSize(texture->GetFormat())), + copy->source.offset, copy->rowPitch); + + D3D12_TEXTURE_COPY_LOCATION textureLocation; + textureLocation.pResource = texture->GetD3D12Resource(); + textureLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; + textureLocation.SubresourceIndex = copy->destination.level; + + for (uint32_t i = 0; i < copySplit.count; ++i) { + auto& info = copySplit.copies[i]; + + D3D12_TEXTURE_COPY_LOCATION bufferLocation; + bufferLocation.pResource = buffer->GetD3D12Resource().Get(); + bufferLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; + bufferLocation.PlacedFootprint.Offset = copySplit.offset; + bufferLocation.PlacedFootprint.Footprint.Format = texture->GetD3D12Format(); + bufferLocation.PlacedFootprint.Footprint.Width = info.bufferSize.width; + bufferLocation.PlacedFootprint.Footprint.Height = info.bufferSize.height; + bufferLocation.PlacedFootprint.Footprint.Depth = info.bufferSize.depth; + bufferLocation.PlacedFootprint.Footprint.RowPitch = copy->rowPitch; + + D3D12_BOX sourceRegion; + sourceRegion.left = info.bufferOffset.x; + sourceRegion.top = info.bufferOffset.y; + sourceRegion.front = info.bufferOffset.z; + sourceRegion.right = info.bufferOffset.x + info.copySize.width; + sourceRegion.bottom = info.bufferOffset.y + info.copySize.height; + sourceRegion.back = info.bufferOffset.z + info.copySize.depth; + + commandList->CopyTextureRegion(&textureLocation, info.textureOffset.x, + info.textureOffset.y, info.textureOffset.z, + &bufferLocation, &sourceRegion); } - break; + } break; - case Command::CopyBufferToTexture: - { - CopyBufferToTextureCmd* copy = mCommands.NextCommand(); - Buffer* buffer = ToBackend(copy->source.buffer.Get()); - Texture* texture = ToBackend(copy->destination.texture.Get()); + case Command::CopyTextureToBuffer: { + CopyTextureToBufferCmd* copy = mCommands.NextCommand(); + Texture* texture = ToBackend(copy->source.texture.Get()); + Buffer* buffer = ToBackend(copy->destination.buffer.Get()); - auto copySplit = ComputeTextureCopySplit( - copy->destination.x, - copy->destination.y, - copy->destination.z, - copy->destination.width, - copy->destination.height, - copy->destination.depth, - static_cast(TextureFormatPixelSize(texture->GetFormat())), - copy->source.offset, - copy->rowPitch - ); + auto copySplit = ComputeTextureCopySplit( + copy->source.x, copy->source.y, copy->source.z, copy->source.width, + copy->source.height, copy->source.depth, + static_cast(TextureFormatPixelSize(texture->GetFormat())), + copy->destination.offset, copy->rowPitch); - D3D12_TEXTURE_COPY_LOCATION textureLocation; - textureLocation.pResource = texture->GetD3D12Resource(); - textureLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; - textureLocation.SubresourceIndex = copy->destination.level; + D3D12_TEXTURE_COPY_LOCATION textureLocation; + textureLocation.pResource = texture->GetD3D12Resource(); + textureLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; + textureLocation.SubresourceIndex = copy->source.level; - for (uint32_t i = 0; i < copySplit.count; ++i) { - auto& info = copySplit.copies[i]; + for (uint32_t i = 0; i < copySplit.count; ++i) { + auto& info = copySplit.copies[i]; - D3D12_TEXTURE_COPY_LOCATION bufferLocation; - bufferLocation.pResource = buffer->GetD3D12Resource().Get(); - bufferLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; - bufferLocation.PlacedFootprint.Offset = copySplit.offset; - bufferLocation.PlacedFootprint.Footprint.Format = texture->GetD3D12Format(); - bufferLocation.PlacedFootprint.Footprint.Width = info.bufferSize.width; - bufferLocation.PlacedFootprint.Footprint.Height = info.bufferSize.height; - bufferLocation.PlacedFootprint.Footprint.Depth = info.bufferSize.depth; - bufferLocation.PlacedFootprint.Footprint.RowPitch = copy->rowPitch; + D3D12_TEXTURE_COPY_LOCATION bufferLocation; + bufferLocation.pResource = buffer->GetD3D12Resource().Get(); + bufferLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; + bufferLocation.PlacedFootprint.Offset = copySplit.offset; + bufferLocation.PlacedFootprint.Footprint.Format = texture->GetD3D12Format(); + bufferLocation.PlacedFootprint.Footprint.Width = info.bufferSize.width; + bufferLocation.PlacedFootprint.Footprint.Height = info.bufferSize.height; + bufferLocation.PlacedFootprint.Footprint.Depth = info.bufferSize.depth; + bufferLocation.PlacedFootprint.Footprint.RowPitch = copy->rowPitch; - D3D12_BOX sourceRegion; - sourceRegion.left = info.bufferOffset.x; - sourceRegion.top = info.bufferOffset.y; - sourceRegion.front = info.bufferOffset.z; - sourceRegion.right = info.bufferOffset.x + info.copySize.width; - sourceRegion.bottom = info.bufferOffset.y + info.copySize.height; - sourceRegion.back = info.bufferOffset.z + info.copySize.depth; + D3D12_BOX sourceRegion; + sourceRegion.left = info.textureOffset.x; + sourceRegion.top = info.textureOffset.y; + sourceRegion.front = info.textureOffset.z; + sourceRegion.right = info.textureOffset.x + info.copySize.width; + sourceRegion.bottom = info.textureOffset.y + info.copySize.height; + sourceRegion.back = info.textureOffset.z + info.copySize.depth; - commandList->CopyTextureRegion(&textureLocation, info.textureOffset.x, info.textureOffset.y, info.textureOffset.z, &bufferLocation, &sourceRegion); - } + commandList->CopyTextureRegion(&bufferLocation, info.bufferOffset.x, + info.bufferOffset.y, info.bufferOffset.z, + &textureLocation, &sourceRegion); } - break; + } break; - case Command::CopyTextureToBuffer: - { - CopyTextureToBufferCmd* copy = mCommands.NextCommand(); - Texture* texture = ToBackend(copy->source.texture.Get()); - Buffer* buffer = ToBackend(copy->destination.buffer.Get()); + case Command::Dispatch: { + DispatchCmd* dispatch = mCommands.NextCommand(); + commandList->Dispatch(dispatch->x, dispatch->y, dispatch->z); + } break; - auto copySplit = ComputeTextureCopySplit( - copy->source.x, - copy->source.y, - copy->source.z, - copy->source.width, - copy->source.height, - copy->source.depth, - static_cast(TextureFormatPixelSize(texture->GetFormat())), - copy->destination.offset, - copy->rowPitch - ); + case Command::DrawArrays: { + DrawArraysCmd* draw = mCommands.NextCommand(); + commandList->DrawInstanced(draw->vertexCount, draw->instanceCount, + draw->firstVertex, draw->firstInstance); + } break; - D3D12_TEXTURE_COPY_LOCATION textureLocation; - textureLocation.pResource = texture->GetD3D12Resource(); - textureLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; - textureLocation.SubresourceIndex = copy->source.level; + case Command::DrawElements: { + DrawElementsCmd* draw = mCommands.NextCommand(); - for (uint32_t i = 0; i < copySplit.count; ++i) { - auto& info = copySplit.copies[i]; + commandList->DrawIndexedInstanced(draw->indexCount, draw->instanceCount, + draw->firstIndex, 0, draw->firstInstance); + } break; - D3D12_TEXTURE_COPY_LOCATION bufferLocation; - bufferLocation.pResource = buffer->GetD3D12Resource().Get(); - bufferLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; - bufferLocation.PlacedFootprint.Offset = copySplit.offset; - bufferLocation.PlacedFootprint.Footprint.Format = texture->GetD3D12Format(); - bufferLocation.PlacedFootprint.Footprint.Width = info.bufferSize.width; - bufferLocation.PlacedFootprint.Footprint.Height = info.bufferSize.height; - bufferLocation.PlacedFootprint.Footprint.Depth = info.bufferSize.depth; - bufferLocation.PlacedFootprint.Footprint.RowPitch = copy->rowPitch; + case Command::EndComputePass: { + mCommands.NextCommand(); + bindingTracker.SetInComputePass(false); + } break; - D3D12_BOX sourceRegion; - sourceRegion.left = info.textureOffset.x; - sourceRegion.top = info.textureOffset.y; - sourceRegion.front = info.textureOffset.z; - sourceRegion.right = info.textureOffset.x + info.copySize.width; - sourceRegion.bottom = info.textureOffset.y + info.copySize.height; - sourceRegion.back = info.textureOffset.z + info.copySize.depth; + case Command::EndRenderPass: { + mCommands.NextCommand(); + } break; - commandList->CopyTextureRegion(&bufferLocation, info.bufferOffset.x, info.bufferOffset.y, info.bufferOffset.z, &textureLocation, &sourceRegion); - } + case Command::EndRenderSubpass: { + mCommands.NextCommand(); + currentSubpass += 1; + } break; + + case Command::SetComputePipeline: { + SetComputePipelineCmd* cmd = mCommands.NextCommand(); + ComputePipeline* pipeline = ToBackend(cmd->pipeline).Get(); + PipelineLayout* layout = ToBackend(pipeline->GetLayout()); + + commandList->SetComputeRootSignature(layout->GetRootSignature().Get()); + commandList->SetPipelineState(pipeline->GetPipelineState().Get()); + + // TODO(enga@google.com): Implement compute pipelines + bindingTracker.SetInheritedBindGroups(commandList, lastLayout, layout); + lastLayout = layout; + } break; + + case Command::SetRenderPipeline: { + SetRenderPipelineCmd* cmd = mCommands.NextCommand(); + RenderPipeline* pipeline = ToBackend(cmd->pipeline).Get(); + PipelineLayout* layout = ToBackend(pipeline->GetLayout()); + + commandList->SetGraphicsRootSignature(layout->GetRootSignature().Get()); + commandList->SetPipelineState(pipeline->GetPipelineState().Get()); + commandList->IASetPrimitiveTopology(pipeline->GetD3D12PrimitiveTopology()); + + bindingTracker.SetInheritedBindGroups(commandList, lastLayout, layout); + + lastRenderPipeline = pipeline; + lastLayout = layout; + } break; + + case Command::SetPushConstants: { + mCommands.NextCommand(); + } break; + + case Command::SetStencilReference: { + SetStencilReferenceCmd* cmd = mCommands.NextCommand(); + + commandList->OMSetStencilRef(cmd->reference); + } break; + + case Command::SetBlendColor: { + SetBlendColorCmd* cmd = mCommands.NextCommand(); + ASSERT(lastRenderPipeline); + commandList->OMSetBlendFactor(static_cast(&cmd->r)); + } break; + + case Command::SetBindGroup: { + SetBindGroupCmd* cmd = mCommands.NextCommand(); + BindGroup* group = ToBackend(cmd->group.Get()); + bindingTracker.SetBindGroup(commandList, lastLayout, group, cmd->index); + } break; + + case Command::SetIndexBuffer: { + SetIndexBufferCmd* cmd = mCommands.NextCommand(); + + Buffer* buffer = ToBackend(cmd->buffer.Get()); + D3D12_INDEX_BUFFER_VIEW bufferView; + bufferView.BufferLocation = buffer->GetVA() + cmd->offset; + bufferView.SizeInBytes = buffer->GetSize() - cmd->offset; + // TODO(cwallez@chromium.org): Make index buffers lazily applied, right now + // this will break if the pipeline is changed for one with a different index + // format after SetIndexBuffer + bufferView.Format = DXGIIndexFormat(lastRenderPipeline->GetIndexFormat()); + + commandList->IASetIndexBuffer(&bufferView); + } break; + + case Command::SetVertexBuffers: { + SetVertexBuffersCmd* cmd = mCommands.NextCommand(); + auto buffers = mCommands.NextData>(cmd->count); + auto offsets = mCommands.NextData(cmd->count); + + auto inputState = ToBackend(lastRenderPipeline->GetInputState()); + + std::array d3d12BufferViews; + for (uint32_t i = 0; i < cmd->count; ++i) { + auto input = inputState->GetInput(cmd->startSlot + i); + Buffer* buffer = ToBackend(buffers[i].Get()); + d3d12BufferViews[i].BufferLocation = buffer->GetVA() + offsets[i]; + d3d12BufferViews[i].StrideInBytes = input.stride; + d3d12BufferViews[i].SizeInBytes = buffer->GetSize() - offsets[i]; } - break; - case Command::Dispatch: - { - DispatchCmd* dispatch = mCommands.NextCommand(); - commandList->Dispatch(dispatch->x, dispatch->y, dispatch->z); + commandList->IASetVertexBuffers(cmd->startSlot, cmd->count, + d3d12BufferViews.data()); + } break; + + case Command::TransitionBufferUsage: { + TransitionBufferUsageCmd* cmd = + mCommands.NextCommand(); + + Buffer* buffer = ToBackend(cmd->buffer.Get()); + + D3D12_RESOURCE_BARRIER barrier; + if (buffer->GetResourceTransitionBarrier(buffer->GetUsage(), cmd->usage, + &barrier)) { + commandList->ResourceBarrier(1, &barrier); } - break; - case Command::DrawArrays: - { - DrawArraysCmd* draw = mCommands.NextCommand(); - commandList->DrawInstanced( - draw->vertexCount, - draw->instanceCount, - draw->firstVertex, - draw->firstInstance - ); + buffer->UpdateUsageInternal(cmd->usage); + } break; + + case Command::TransitionTextureUsage: { + TransitionTextureUsageCmd* cmd = + mCommands.NextCommand(); + + Texture* texture = ToBackend(cmd->texture.Get()); + + D3D12_RESOURCE_BARRIER barrier; + if (texture->GetResourceTransitionBarrier(texture->GetUsage(), cmd->usage, + &barrier)) { + commandList->ResourceBarrier(1, &barrier); } - break; - case Command::DrawElements: - { - DrawElementsCmd* draw = mCommands.NextCommand(); - - commandList->DrawIndexedInstanced( - draw->indexCount, - draw->instanceCount, - draw->firstIndex, - 0, - draw->firstInstance - ); - } - break; - - case Command::EndComputePass: - { - mCommands.NextCommand(); - bindingTracker.SetInComputePass(false); - } - break; - - case Command::EndRenderPass: - { - mCommands.NextCommand(); - } - break; - - case Command::EndRenderSubpass: - { - mCommands.NextCommand(); - currentSubpass += 1; - } - break; - - case Command::SetComputePipeline: - { - SetComputePipelineCmd* cmd = mCommands.NextCommand(); - ComputePipeline* pipeline = ToBackend(cmd->pipeline).Get(); - PipelineLayout* layout = ToBackend(pipeline->GetLayout()); - - commandList->SetComputeRootSignature(layout->GetRootSignature().Get()); - commandList->SetPipelineState(pipeline->GetPipelineState().Get()); - - // TODO(enga@google.com): Implement compute pipelines - bindingTracker.SetInheritedBindGroups(commandList, lastLayout, layout); - lastLayout = layout; - } - break; - - case Command::SetRenderPipeline: - { - SetRenderPipelineCmd* cmd = mCommands.NextCommand(); - RenderPipeline* pipeline = ToBackend(cmd->pipeline).Get(); - PipelineLayout* layout = ToBackend(pipeline->GetLayout()); - - commandList->SetGraphicsRootSignature(layout->GetRootSignature().Get()); - commandList->SetPipelineState(pipeline->GetPipelineState().Get()); - commandList->IASetPrimitiveTopology(pipeline->GetD3D12PrimitiveTopology()); - - bindingTracker.SetInheritedBindGroups(commandList, lastLayout, layout); - - lastRenderPipeline = pipeline; - lastLayout = layout; - } - break; - - case Command::SetPushConstants: - { - mCommands.NextCommand(); - } - break; - - case Command::SetStencilReference: - { - SetStencilReferenceCmd *cmd = mCommands.NextCommand(); - - commandList->OMSetStencilRef(cmd->reference); - } - break; - - case Command::SetBlendColor: - { - SetBlendColorCmd* cmd = mCommands.NextCommand(); - ASSERT(lastRenderPipeline); - commandList->OMSetBlendFactor(static_cast(&cmd->r)); - } - break; - - case Command::SetBindGroup: - { - SetBindGroupCmd* cmd = mCommands.NextCommand(); - BindGroup* group = ToBackend(cmd->group.Get()); - bindingTracker.SetBindGroup(commandList, lastLayout, group, cmd->index); - } - break; - - case Command::SetIndexBuffer: - { - SetIndexBufferCmd* cmd = mCommands.NextCommand(); - - Buffer* buffer = ToBackend(cmd->buffer.Get()); - D3D12_INDEX_BUFFER_VIEW bufferView; - bufferView.BufferLocation = buffer->GetVA() + cmd->offset; - bufferView.SizeInBytes = buffer->GetSize() - cmd->offset; - //TODO(cwallez@chromium.org): Make index buffers lazily applied, right now - //this will break if the pipeline is changed for one with a different index - //format after SetIndexBuffer - bufferView.Format = DXGIIndexFormat(lastRenderPipeline->GetIndexFormat()); - - commandList->IASetIndexBuffer(&bufferView); - } - break; - - case Command::SetVertexBuffers: - { - SetVertexBuffersCmd* cmd = mCommands.NextCommand(); - auto buffers = mCommands.NextData>(cmd->count); - auto offsets = mCommands.NextData(cmd->count); - - auto inputState = ToBackend(lastRenderPipeline->GetInputState()); - - std::array d3d12BufferViews; - for (uint32_t i = 0; i < cmd->count; ++i) { - auto input = inputState->GetInput(cmd->startSlot + i); - Buffer* buffer = ToBackend(buffers[i].Get()); - d3d12BufferViews[i].BufferLocation = buffer->GetVA() + offsets[i]; - d3d12BufferViews[i].StrideInBytes = input.stride; - d3d12BufferViews[i].SizeInBytes = buffer->GetSize() - offsets[i]; - } - - commandList->IASetVertexBuffers(cmd->startSlot, cmd->count, d3d12BufferViews.data()); - } - break; - - case Command::TransitionBufferUsage: - { - TransitionBufferUsageCmd* cmd = mCommands.NextCommand(); - - Buffer* buffer = ToBackend(cmd->buffer.Get()); - - D3D12_RESOURCE_BARRIER barrier; - if (buffer->GetResourceTransitionBarrier(buffer->GetUsage(), cmd->usage, &barrier)) { - commandList->ResourceBarrier(1, &barrier); - } - - buffer->UpdateUsageInternal(cmd->usage); - } - break; - - case Command::TransitionTextureUsage: - { - TransitionTextureUsageCmd* cmd = mCommands.NextCommand(); - - Texture* texture = ToBackend(cmd->texture.Get()); - - D3D12_RESOURCE_BARRIER barrier; - if (texture->GetResourceTransitionBarrier(texture->GetUsage(), cmd->usage, &barrier)) { - commandList->ResourceBarrier(1, &barrier); - } - - texture->UpdateUsageInternal(cmd->usage); - } - break; + texture->UpdateUsageInternal(cmd->usage); + } break; } } } -} -} +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/CommandBufferD3D12.h b/src/backend/d3d12/CommandBufferD3D12.h index fb4f329293..224b3bbeec 100644 --- a/src/backend/d3d12/CommandBufferD3D12.h +++ b/src/backend/d3d12/CommandBufferD3D12.h @@ -20,23 +20,21 @@ #include "backend/d3d12/d3d12_platform.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { class Device; class CommandBuffer : public CommandBufferBase { - public: - CommandBuffer(Device* device, CommandBufferBuilder* builder); - ~CommandBuffer(); + public: + CommandBuffer(Device* device, CommandBufferBuilder* builder); + ~CommandBuffer(); - void FillCommands(ComPtr commandList); + void FillCommands(ComPtr commandList); - private: - Device* mDevice; - CommandIterator mCommands; + private: + Device* mDevice; + CommandIterator mCommands; }; -} -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_COMMANDBUFFERD3D12_H_ +#endif // BACKEND_D3D12_COMMANDBUFFERD3D12_H_ diff --git a/src/backend/d3d12/ComputePipelineD3D12.cpp b/src/backend/d3d12/ComputePipelineD3D12.cpp index 3e1dccd24e..f8822d674b 100644 --- a/src/backend/d3d12/ComputePipelineD3D12.cpp +++ b/src/backend/d3d12/ComputePipelineD3D12.cpp @@ -15,14 +15,13 @@ #include "backend/d3d12/ComputePipelineD3D12.h" #include "backend/d3d12/D3D12Backend.h" -#include "backend/d3d12/ShaderModuleD3D12.h" #include "backend/d3d12/PipelineLayoutD3D12.h" +#include "backend/d3d12/ShaderModuleD3D12.h" #include "common/Assert.h" #include -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { ComputePipeline::ComputePipeline(ComputePipelineBuilder* builder) : ComputePipelineBase(builder) { @@ -41,19 +40,9 @@ namespace d3d12 { ComPtr compiledShader; ComPtr errors; - if (FAILED(D3DCompile( - hlslSource.c_str(), - hlslSource.length(), - nullptr, - { nullptr }, - nullptr, - entryPoint.c_str(), - "cs_5_1", - compileFlags, - 0, - &compiledShader, - &errors - ))) { + if (FAILED(D3DCompile(hlslSource.c_str(), hlslSource.length(), nullptr, {nullptr}, nullptr, + entryPoint.c_str(), "cs_5_1", compileFlags, 0, &compiledShader, + &errors))) { printf("%s\n", reinterpret_cast(errors->GetBufferPointer())); ASSERT(false); } @@ -64,12 +53,12 @@ namespace d3d12 { descriptor.CS.BytecodeLength = compiledShader->GetBufferSize(); Device* device = ToBackend(builder->GetDevice()); - device->GetD3D12Device()->CreateComputePipelineState(&descriptor, IID_PPV_ARGS(&mPipelineState)); + device->GetD3D12Device()->CreateComputePipelineState(&descriptor, + IID_PPV_ARGS(&mPipelineState)); } ComPtr ComputePipeline::GetPipelineState() { return mPipelineState; } -} -} +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/ComputePipelineD3D12.h b/src/backend/d3d12/ComputePipelineD3D12.h index 0fc470709b..fd9f2a67eb 100644 --- a/src/backend/d3d12/ComputePipelineD3D12.h +++ b/src/backend/d3d12/ComputePipelineD3D12.h @@ -19,20 +19,18 @@ #include "backend/d3d12/d3d12_platform.h" -namespace backend { - namespace d3d12 { +namespace backend { namespace d3d12 { - class ComputePipeline : public ComputePipelineBase { - public: - ComputePipeline(ComputePipelineBuilder* builder); + class ComputePipeline : public ComputePipelineBase { + public: + ComputePipeline(ComputePipelineBuilder* builder); - ComPtr GetPipelineState(); + ComPtr GetPipelineState(); - private: - ComPtr mPipelineState; - }; + private: + ComPtr mPipelineState; + }; - } -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_COMPUTEPIPELINED3D12_H_ +#endif // BACKEND_D3D12_COMPUTEPIPELINED3D12_H_ diff --git a/src/backend/d3d12/D3D12Backend.cpp b/src/backend/d3d12/D3D12Backend.cpp index 16ee44d5bd..bc7fca2363 100644 --- a/src/backend/d3d12/D3D12Backend.cpp +++ b/src/backend/d3d12/D3D12Backend.cpp @@ -36,8 +36,7 @@ #include "backend/d3d12/TextureD3D12.h" #include "common/Assert.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { nxtProcTable GetNonValidatingProcs(); nxtProcTable GetValidatingProcs(); @@ -63,7 +62,8 @@ namespace d3d12 { backendDevice->NextSerial(); } - void ExecuteCommandLists(nxtDevice device, std::initializer_list commandLists) { + void ExecuteCommandLists(nxtDevice device, + std::initializer_list commandLists) { Device* backendDevice = reinterpret_cast(device); backendDevice->ExecuteCommandLists(commandLists); } @@ -89,13 +89,13 @@ namespace d3d12 { mMapReadRequestTracker(new MapReadRequestTracker(this)), mResourceAllocator(new ResourceAllocator(this)), mResourceUploader(new ResourceUploader(this)) { - D3D12_COMMAND_QUEUE_DESC queueDesc = {}; queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE; queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT; ASSERT_SUCCESS(d3d12Device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&mCommandQueue))); - ASSERT_SUCCESS(d3d12Device->CreateFence(mSerial, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&mFence))); + ASSERT_SUCCESS( + d3d12Device->CreateFence(mSerial, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&mFence))); mFenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr); ASSERT(mFenceEvent != nullptr); @@ -105,8 +105,8 @@ namespace d3d12 { Device::~Device() { const uint64_t currentSerial = GetSerial(); NextSerial(); - WaitForSerial(currentSerial); // Wait for all in-flight commands to finish executing - TickImpl(); // Call tick one last time so resources are cleaned up + WaitForSerial(currentSerial); // Wait for all in-flight commands to finish executing + TickImpl(); // Call tick one last time so resources are cleaned up delete mCommandAllocatorManager; delete mDescriptorHeapAllocator; delete mMapReadRequestTracker; @@ -139,22 +139,21 @@ namespace d3d12 { } void Device::OpenCommandList(ComPtr* commandList) { - ComPtr &cmdList = *commandList; + ComPtr& cmdList = *commandList; if (!cmdList) { ASSERT_SUCCESS(mD3d12Device->CreateCommandList( - 0, - D3D12_COMMAND_LIST_TYPE_DIRECT, - mCommandAllocatorManager->ReserveCommandAllocator().Get(), - nullptr, - IID_PPV_ARGS(&cmdList) - )); + 0, D3D12_COMMAND_LIST_TYPE_DIRECT, + mCommandAllocatorManager->ReserveCommandAllocator().Get(), nullptr, + IID_PPV_ARGS(&cmdList))); } else { - ASSERT_SUCCESS(cmdList->Reset(mCommandAllocatorManager->ReserveCommandAllocator().Get(), nullptr)); + ASSERT_SUCCESS( + cmdList->Reset(mCommandAllocatorManager->ReserveCommandAllocator().Get(), nullptr)); } } ComPtr Device::GetPendingCommandList() { - // Callers of GetPendingCommandList do so to record commands. Only reserve a command allocator when it is needed so we don't submit empty command lists + // Callers of GetPendingCommandList do so to record commands. Only reserve a command + // allocator when it is needed so we don't submit empty command lists if (!mPendingCommands.open) { OpenCommandList(&mPendingCommands.commandList); mPendingCommands.open = true; @@ -197,10 +196,12 @@ namespace d3d12 { mPendingCommands.open = false; lists[0] = mPendingCommands.commandList.Get(); std::copy(commandLists.begin(), commandLists.end(), lists.begin() + 1); - mCommandQueue->ExecuteCommandLists(static_cast(commandLists.size() + 1), lists.data()); + mCommandQueue->ExecuteCommandLists(static_cast(commandLists.size() + 1), + lists.data()); } else { std::vector lists(commandLists); - mCommandQueue->ExecuteCommandLists(static_cast(commandLists.size()), lists.data()); + mCommandQueue->ExecuteCommandLists(static_cast(commandLists.size()), + lists.data()); } } @@ -268,5 +269,4 @@ namespace d3d12 { : RenderPassBase(builder), mDevice(device) { } -} -} +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/D3D12Backend.h b/src/backend/d3d12/D3D12Backend.h index 591b5091a1..7125c7901b 100644 --- a/src/backend/d3d12/D3D12Backend.h +++ b/src/backend/d3d12/D3D12Backend.h @@ -24,8 +24,7 @@ #include "backend/d3d12/d3d12_platform.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { class BindGroup; class BindGroupLayout; @@ -77,7 +76,7 @@ namespace d3d12 { using TextureViewType = TextureView; }; - template + template auto ToBackend(T&& common) -> decltype(ToBackendBase(common)) { return ToBackendBase(common); } @@ -86,78 +85,77 @@ namespace d3d12 { // Definition of backend types class Device : public DeviceBase { - public: - Device(ComPtr d3d12Device); - ~Device(); + public: + Device(ComPtr d3d12Device); + ~Device(); - BindGroupBase* CreateBindGroup(BindGroupBuilder* builder) override; - BindGroupLayoutBase* CreateBindGroupLayout(BindGroupLayoutBuilder* builder) override; - BlendStateBase* CreateBlendState(BlendStateBuilder* builder) override; - BufferBase* CreateBuffer(BufferBuilder* builder) override; - BufferViewBase* CreateBufferView(BufferViewBuilder* builder) override; - CommandBufferBase* CreateCommandBuffer(CommandBufferBuilder* builder) override; - ComputePipelineBase* CreateComputePipeline(ComputePipelineBuilder* builder) override; - DepthStencilStateBase* CreateDepthStencilState(DepthStencilStateBuilder* builder) override; - FramebufferBase* CreateFramebuffer(FramebufferBuilder* builder) override; - InputStateBase* CreateInputState(InputStateBuilder* builder) override; - PipelineLayoutBase* CreatePipelineLayout(PipelineLayoutBuilder* builder) override; - QueueBase* CreateQueue(QueueBuilder* builder) override; - RenderPassBase* CreateRenderPass(RenderPassBuilder* builder) override; - RenderPipelineBase* CreateRenderPipeline(RenderPipelineBuilder* builder) override; - SamplerBase* CreateSampler(SamplerBuilder* builder) override; - ShaderModuleBase* CreateShaderModule(ShaderModuleBuilder* builder) override; - SwapChainBase* CreateSwapChain(SwapChainBuilder* builder) override; - TextureBase* CreateTexture(TextureBuilder* builder) override; - TextureViewBase* CreateTextureView(TextureViewBuilder* builder) override; + BindGroupBase* CreateBindGroup(BindGroupBuilder* builder) override; + BindGroupLayoutBase* CreateBindGroupLayout(BindGroupLayoutBuilder* builder) override; + BlendStateBase* CreateBlendState(BlendStateBuilder* builder) override; + BufferBase* CreateBuffer(BufferBuilder* builder) override; + BufferViewBase* CreateBufferView(BufferViewBuilder* builder) override; + CommandBufferBase* CreateCommandBuffer(CommandBufferBuilder* builder) override; + ComputePipelineBase* CreateComputePipeline(ComputePipelineBuilder* builder) override; + DepthStencilStateBase* CreateDepthStencilState(DepthStencilStateBuilder* builder) override; + FramebufferBase* CreateFramebuffer(FramebufferBuilder* builder) override; + InputStateBase* CreateInputState(InputStateBuilder* builder) override; + PipelineLayoutBase* CreatePipelineLayout(PipelineLayoutBuilder* builder) override; + QueueBase* CreateQueue(QueueBuilder* builder) override; + RenderPassBase* CreateRenderPass(RenderPassBuilder* builder) override; + RenderPipelineBase* CreateRenderPipeline(RenderPipelineBuilder* builder) override; + SamplerBase* CreateSampler(SamplerBuilder* builder) override; + ShaderModuleBase* CreateShaderModule(ShaderModuleBuilder* builder) override; + SwapChainBase* CreateSwapChain(SwapChainBuilder* builder) override; + TextureBase* CreateTexture(TextureBuilder* builder) override; + TextureViewBase* CreateTextureView(TextureViewBuilder* builder) override; - void TickImpl() override; + void TickImpl() override; - ComPtr GetD3D12Device(); - ComPtr GetCommandQueue(); + ComPtr GetD3D12Device(); + ComPtr GetCommandQueue(); - DescriptorHeapAllocator* GetDescriptorHeapAllocator(); - MapReadRequestTracker* GetMapReadRequestTracker() const; - ResourceAllocator* GetResourceAllocator(); - ResourceUploader* GetResourceUploader(); + DescriptorHeapAllocator* GetDescriptorHeapAllocator(); + MapReadRequestTracker* GetMapReadRequestTracker() const; + ResourceAllocator* GetResourceAllocator(); + ResourceUploader* GetResourceUploader(); - void OpenCommandList(ComPtr* commandList); - ComPtr GetPendingCommandList(); + void OpenCommandList(ComPtr* commandList); + ComPtr GetPendingCommandList(); - uint64_t GetSerial() const; - void NextSerial(); - void WaitForSerial(uint64_t serial); + uint64_t GetSerial() const; + void NextSerial(); + void WaitForSerial(uint64_t serial); - void ExecuteCommandLists(std::initializer_list commandLists); + void ExecuteCommandLists(std::initializer_list commandLists); - private: - uint64_t mSerial = 0; - ComPtr mFence; - HANDLE mFenceEvent; + private: + uint64_t mSerial = 0; + ComPtr mFence; + HANDLE mFenceEvent; - ComPtr mD3d12Device; - ComPtr mCommandQueue; + ComPtr mD3d12Device; + ComPtr mCommandQueue; - CommandAllocatorManager* mCommandAllocatorManager; - DescriptorHeapAllocator* mDescriptorHeapAllocator; - MapReadRequestTracker* mMapReadRequestTracker; - ResourceAllocator* mResourceAllocator; - ResourceUploader* mResourceUploader; + CommandAllocatorManager* mCommandAllocatorManager; + DescriptorHeapAllocator* mDescriptorHeapAllocator; + MapReadRequestTracker* mMapReadRequestTracker; + ResourceAllocator* mResourceAllocator; + ResourceUploader* mResourceUploader; - struct PendingCommandList { - ComPtr commandList; - bool open = false; - } mPendingCommands; + struct PendingCommandList { + ComPtr commandList; + bool open = false; + } mPendingCommands; }; class RenderPass : public RenderPassBase { - public: - RenderPass(Device* device, RenderPassBuilder* builder); + public: + RenderPass(Device* device, RenderPassBuilder* builder); - private: - Device* mDevice; + private: + Device* mDevice; }; -} -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_D3D12BACKEND_H_ +#endif // BACKEND_D3D12_D3D12BACKEND_H_ diff --git a/src/backend/d3d12/DepthStencilStateD3D12.cpp b/src/backend/d3d12/DepthStencilStateD3D12.cpp index ea5e39fc5b..72b72ee3fc 100644 --- a/src/backend/d3d12/DepthStencilStateD3D12.cpp +++ b/src/backend/d3d12/DepthStencilStateD3D12.cpp @@ -16,8 +16,7 @@ #include "common/BitSetIterator.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { static D3D12_STENCIL_OP StencilOp(nxt::StencilOperation op) { switch (op) { @@ -43,8 +42,7 @@ namespace d3d12 { } static D3D12_COMPARISON_FUNC ComparisonFunc(nxt::CompareFunction func) { - switch (func) - { + switch (func) { case nxt::CompareFunction::Always: return D3D12_COMPARISON_FUNC_ALWAYS; case nxt::CompareFunction::Equal: @@ -66,7 +64,8 @@ namespace d3d12 { } } - static D3D12_DEPTH_STENCILOP_DESC StencilOpDesc(backend::DepthStencilStateBase::StencilFaceInfo faceInfo) { + static D3D12_DEPTH_STENCILOP_DESC StencilOpDesc( + backend::DepthStencilStateBase::StencilFaceInfo faceInfo) { D3D12_DEPTH_STENCILOP_DESC desc; desc.StencilFailOp = StencilOp(faceInfo.stencilFail); @@ -79,9 +78,9 @@ namespace d3d12 { DepthStencilState::DepthStencilState(Device* device, DepthStencilStateBuilder* builder) : DepthStencilStateBase(builder), mDevice(device) { - mDepthStencilDescriptor.DepthEnable = TRUE; - mDepthStencilDescriptor.DepthWriteMask = GetDepth().depthWriteEnabled ? D3D12_DEPTH_WRITE_MASK_ALL : D3D12_DEPTH_WRITE_MASK_ZERO; + mDepthStencilDescriptor.DepthWriteMask = + GetDepth().depthWriteEnabled ? D3D12_DEPTH_WRITE_MASK_ALL : D3D12_DEPTH_WRITE_MASK_ZERO; mDepthStencilDescriptor.DepthFunc = ComparisonFunc(GetDepth().compareFunction); mDepthStencilDescriptor.StencilEnable = StencilTestEnabled() ? TRUE : FALSE; @@ -90,11 +89,10 @@ namespace d3d12 { mDepthStencilDescriptor.FrontFace = StencilOpDesc(GetStencil().front); mDepthStencilDescriptor.BackFace = StencilOpDesc(GetStencil().back); - } + } const D3D12_DEPTH_STENCIL_DESC& DepthStencilState::GetD3D12DepthStencilDescriptor() const { return mDepthStencilDescriptor; } -} -} +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/DepthStencilStateD3D12.h b/src/backend/d3d12/DepthStencilStateD3D12.h index 4020c568b1..e67944d858 100644 --- a/src/backend/d3d12/DepthStencilStateD3D12.h +++ b/src/backend/d3d12/DepthStencilStateD3D12.h @@ -19,23 +19,21 @@ #include "backend/d3d12/d3d12_platform.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { class Device; class DepthStencilState : public DepthStencilStateBase { - public: - DepthStencilState(Device* device, DepthStencilStateBuilder* builder); + public: + DepthStencilState(Device* device, DepthStencilStateBuilder* builder); - const D3D12_DEPTH_STENCIL_DESC& GetD3D12DepthStencilDescriptor() const; + const D3D12_DEPTH_STENCIL_DESC& GetD3D12DepthStencilDescriptor() const; - private: - Device* mDevice; - D3D12_DEPTH_STENCIL_DESC mDepthStencilDescriptor; + private: + Device* mDevice; + D3D12_DEPTH_STENCIL_DESC mDepthStencilDescriptor; }; -} -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_DEPTHSTENCILSTATED3D12_H_ +#endif // BACKEND_D3D12_DEPTHSTENCILSTATED3D12_H_ diff --git a/src/backend/d3d12/DescriptorHeapAllocator.cpp b/src/backend/d3d12/DescriptorHeapAllocator.cpp index 0cd97cfc6d..320a50887c 100644 --- a/src/backend/d3d12/DescriptorHeapAllocator.cpp +++ b/src/backend/d3d12/DescriptorHeapAllocator.cpp @@ -17,14 +17,15 @@ #include "backend/d3d12/D3D12Backend.h" #include "common/Assert.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { DescriptorHeapHandle::DescriptorHeapHandle() : mDescriptorHeap(nullptr), mSizeIncrement(0), mOffset(0) { } - DescriptorHeapHandle::DescriptorHeapHandle(ComPtr descriptorHeap, uint32_t sizeIncrement, uint32_t offset) + DescriptorHeapHandle::DescriptorHeapHandle(ComPtr descriptorHeap, + uint32_t sizeIncrement, + uint32_t offset) : mDescriptorHeap(descriptorHeap), mSizeIncrement(sizeIncrement), mOffset(offset) { } @@ -46,29 +47,39 @@ namespace d3d12 { return handle; } - DescriptorHeapAllocator::DescriptorHeapAllocator(Device* device) : mDevice(device), - mSizeIncrements { - device->GetD3D12Device()->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV), - device->GetD3D12Device()->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER), - device->GetD3D12Device()->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV), - device->GetD3D12Device()->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_DSV), + mSizeIncrements{ + device->GetD3D12Device()->GetDescriptorHandleIncrementSize( + D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV), + device->GetD3D12Device()->GetDescriptorHandleIncrementSize( + D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER), + device->GetD3D12Device()->GetDescriptorHandleIncrementSize( + D3D12_DESCRIPTOR_HEAP_TYPE_RTV), + device->GetD3D12Device()->GetDescriptorHandleIncrementSize( + D3D12_DESCRIPTOR_HEAP_TYPE_DSV), } { } - DescriptorHeapHandle DescriptorHeapAllocator::Allocate(D3D12_DESCRIPTOR_HEAP_TYPE type, uint32_t count, uint32_t allocationSize, DescriptorHeapInfo* heapInfo, D3D12_DESCRIPTOR_HEAP_FLAGS flags) { - // TODO(enga@google.com): This is just a linear allocator so the heap will quickly run out of space causing a new one to be allocated - // We should reuse heap subranges that have been released + DescriptorHeapHandle DescriptorHeapAllocator::Allocate(D3D12_DESCRIPTOR_HEAP_TYPE type, + uint32_t count, + uint32_t allocationSize, + DescriptorHeapInfo* heapInfo, + D3D12_DESCRIPTOR_HEAP_FLAGS flags) { + // TODO(enga@google.com): This is just a linear allocator so the heap will quickly run out + // of space causing a new one to be allocated We should reuse heap subranges that have been + // released if (count == 0) { return DescriptorHeapHandle(); } { - // If the current pool for this type has space, linearly allocate count bytes in the pool + // If the current pool for this type has space, linearly allocate count bytes in the + // pool auto& allocationInfo = heapInfo->second; if (allocationInfo.remaining >= count) { - DescriptorHeapHandle handle(heapInfo->first, mSizeIncrements[type], allocationInfo.size - allocationInfo.remaining); + DescriptorHeapHandle handle(heapInfo->first, mSizeIncrements[type], + allocationInfo.size - allocationInfo.remaining); allocationInfo.remaining -= count; Release(handle); return handle; @@ -83,9 +94,10 @@ namespace d3d12 { heapDescriptor.Flags = flags; heapDescriptor.NodeMask = 0; ComPtr heap; - ASSERT_SUCCESS(mDevice->GetD3D12Device()->CreateDescriptorHeap(&heapDescriptor, IID_PPV_ARGS(&heap))); + ASSERT_SUCCESS( + mDevice->GetD3D12Device()->CreateDescriptorHeap(&heapDescriptor, IID_PPV_ARGS(&heap))); - AllocationInfo allocationInfo = { allocationSize, allocationSize - count }; + AllocationInfo allocationInfo = {allocationSize, allocationSize - count}; *heapInfo = std::make_pair(heap, allocationInfo); DescriptorHeapHandle handle(heap, mSizeIncrements[type], 0); @@ -93,14 +105,21 @@ namespace d3d12 { return handle; } - DescriptorHeapHandle DescriptorHeapAllocator::AllocateCPUHeap(D3D12_DESCRIPTOR_HEAP_TYPE type, uint32_t count) { - return Allocate(type, count, count, &mCpuDescriptorHeapInfos[type], D3D12_DESCRIPTOR_HEAP_FLAG_NONE); + DescriptorHeapHandle DescriptorHeapAllocator::AllocateCPUHeap(D3D12_DESCRIPTOR_HEAP_TYPE type, + uint32_t count) { + return Allocate(type, count, count, &mCpuDescriptorHeapInfos[type], + D3D12_DESCRIPTOR_HEAP_FLAG_NONE); } - DescriptorHeapHandle DescriptorHeapAllocator::AllocateGPUHeap(D3D12_DESCRIPTOR_HEAP_TYPE type, uint32_t count) { - ASSERT(type == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV || type == D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER); - unsigned int heapSize = (type == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV ? kMaxCbvUavSrvHeapSize : kMaxSamplerHeapSize); - return Allocate(type, count, heapSize, &mGpuDescriptorHeapInfos[type], D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE); + DescriptorHeapHandle DescriptorHeapAllocator::AllocateGPUHeap(D3D12_DESCRIPTOR_HEAP_TYPE type, + uint32_t count) { + ASSERT(type == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV || + type == D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER); + unsigned int heapSize = + (type == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV ? kMaxCbvUavSrvHeapSize + : kMaxSamplerHeapSize); + return Allocate(type, count, heapSize, &mGpuDescriptorHeapInfos[type], + D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE); } void DescriptorHeapAllocator::Tick(uint64_t lastCompletedSerial) { @@ -110,5 +129,4 @@ namespace d3d12 { void DescriptorHeapAllocator::Release(DescriptorHeapHandle handle) { mReleasedHandles.Enqueue(handle, mDevice->GetSerial()); } -} -} +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/DescriptorHeapAllocator.h b/src/backend/d3d12/DescriptorHeapAllocator.h index 41cd46baf1..9c6bf0cbff 100644 --- a/src/backend/d3d12/DescriptorHeapAllocator.h +++ b/src/backend/d3d12/DescriptorHeapAllocator.h @@ -17,63 +17,67 @@ #include "backend/d3d12/d3d12_platform.h" -#include "common/SerialQueue.h" #include #include +#include "common/SerialQueue.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { class Device; class DescriptorHeapHandle { + public: + DescriptorHeapHandle(); + DescriptorHeapHandle(ComPtr descriptorHeap, + uint32_t sizeIncrement, + uint32_t offset); - public: - DescriptorHeapHandle(); - DescriptorHeapHandle(ComPtr descriptorHeap, uint32_t sizeIncrement, uint32_t offset); + ID3D12DescriptorHeap* Get() const; + D3D12_CPU_DESCRIPTOR_HANDLE GetCPUHandle(uint32_t index) const; + D3D12_GPU_DESCRIPTOR_HANDLE GetGPUHandle(uint32_t index) const; - ID3D12DescriptorHeap* Get() const; - D3D12_CPU_DESCRIPTOR_HANDLE GetCPUHandle(uint32_t index) const; - D3D12_GPU_DESCRIPTOR_HANDLE GetGPUHandle(uint32_t index) const; - - private: - ComPtr mDescriptorHeap; - uint32_t mSizeIncrement; - uint32_t mOffset; + private: + ComPtr mDescriptorHeap; + uint32_t mSizeIncrement; + uint32_t mOffset; }; class DescriptorHeapAllocator { - public: - DescriptorHeapAllocator(Device* device); + public: + DescriptorHeapAllocator(Device* device); - DescriptorHeapHandle AllocateGPUHeap(D3D12_DESCRIPTOR_HEAP_TYPE type, uint32_t count); - DescriptorHeapHandle AllocateCPUHeap(D3D12_DESCRIPTOR_HEAP_TYPE type, uint32_t count); - void Tick(uint64_t lastCompletedSerial); + DescriptorHeapHandle AllocateGPUHeap(D3D12_DESCRIPTOR_HEAP_TYPE type, uint32_t count); + DescriptorHeapHandle AllocateCPUHeap(D3D12_DESCRIPTOR_HEAP_TYPE type, uint32_t count); + void Tick(uint64_t lastCompletedSerial); - private: - static constexpr unsigned int kMaxCbvUavSrvHeapSize = 1000000; - static constexpr unsigned int kMaxSamplerHeapSize = 2048; - static constexpr unsigned int kDescriptorHeapTypes = D3D12_DESCRIPTOR_HEAP_TYPE::D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; + private: + static constexpr unsigned int kMaxCbvUavSrvHeapSize = 1000000; + static constexpr unsigned int kMaxSamplerHeapSize = 2048; + static constexpr unsigned int kDescriptorHeapTypes = + D3D12_DESCRIPTOR_HEAP_TYPE::D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; - struct AllocationInfo { - uint32_t size = 0; - uint32_t remaining = 0; - }; + struct AllocationInfo { + uint32_t size = 0; + uint32_t remaining = 0; + }; - using DescriptorHeapInfo = std::pair, AllocationInfo>; + using DescriptorHeapInfo = std::pair, AllocationInfo>; - DescriptorHeapHandle Allocate(D3D12_DESCRIPTOR_HEAP_TYPE type, uint32_t count, uint32_t allocationSize, DescriptorHeapInfo* heapInfo, D3D12_DESCRIPTOR_HEAP_FLAGS flags); - void Release(DescriptorHeapHandle handle); + DescriptorHeapHandle Allocate(D3D12_DESCRIPTOR_HEAP_TYPE type, + uint32_t count, + uint32_t allocationSize, + DescriptorHeapInfo* heapInfo, + D3D12_DESCRIPTOR_HEAP_FLAGS flags); + void Release(DescriptorHeapHandle handle); - Device* mDevice; + Device* mDevice; - std::array mSizeIncrements; - std::array mCpuDescriptorHeapInfos; - std::array mGpuDescriptorHeapInfos; - SerialQueue mReleasedHandles; + std::array mSizeIncrements; + std::array mCpuDescriptorHeapInfos; + std::array mGpuDescriptorHeapInfos; + SerialQueue mReleasedHandles; }; -} -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_DESCRIPTORHEAPALLOCATOR_H_ +#endif // BACKEND_D3D12_DESCRIPTORHEAPALLOCATOR_H_ diff --git a/src/backend/d3d12/FramebufferD3D12.cpp b/src/backend/d3d12/FramebufferD3D12.cpp index e45d6e577c..9b15d0d294 100644 --- a/src/backend/d3d12/FramebufferD3D12.cpp +++ b/src/backend/d3d12/FramebufferD3D12.cpp @@ -14,12 +14,11 @@ #include "backend/d3d12/FramebufferD3D12.h" -#include "common/BitSetIterator.h" #include "backend/d3d12/D3D12Backend.h" #include "backend/d3d12/TextureD3D12.h" +#include "common/BitSetIterator.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { Framebuffer::Framebuffer(Device* device, FramebufferBuilder* builder) : FramebufferBase(builder), mDevice(device) { @@ -39,32 +38,36 @@ namespace d3d12 { if (rtvCount) { mRtvHeap = device->GetDescriptorHeapAllocator()->AllocateCPUHeap( - D3D12_DESCRIPTOR_HEAP_TYPE_RTV, rtvCount); + D3D12_DESCRIPTOR_HEAP_TYPE_RTV, rtvCount); } if (dsvCount) { mDsvHeap = device->GetDescriptorHeapAllocator()->AllocateCPUHeap( - D3D12_DESCRIPTOR_HEAP_TYPE_DSV, dsvCount); + D3D12_DESCRIPTOR_HEAP_TYPE_DSV, dsvCount); } for (uint32_t attachment = 0; attachment < renderPass->GetAttachmentCount(); ++attachment) { uint32_t heapIndex = mAttachmentHeapIndices[attachment]; auto* textureView = GetTextureView(attachment); - ComPtr texture = ToBackend(textureView->GetTexture())->GetD3D12Resource(); + ComPtr texture = + ToBackend(textureView->GetTexture())->GetD3D12Resource(); auto format = textureView->GetTexture()->GetFormat(); if (TextureFormatHasDepth(format) || TextureFormatHasStencil(format)) { D3D12_CPU_DESCRIPTOR_HANDLE dsvHandle = mDsvHeap.GetCPUHandle(heapIndex); D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc = ToBackend(textureView)->GetDSVDescriptor(); - device->GetD3D12Device()->CreateDepthStencilView(texture.Get(), &dsvDesc, dsvHandle); + device->GetD3D12Device()->CreateDepthStencilView(texture.Get(), &dsvDesc, + dsvHandle); } else { D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = mRtvHeap.GetCPUHandle(heapIndex); D3D12_RENDER_TARGET_VIEW_DESC rtvDesc = ToBackend(textureView)->GetRTVDescriptor(); - device->GetD3D12Device()->CreateRenderTargetView(texture.Get(), &rtvDesc, rtvHandle); + device->GetD3D12Device()->CreateRenderTargetView(texture.Get(), &rtvDesc, + rtvHandle); } } } - Framebuffer::OMSetRenderTargetArgs Framebuffer::GetSubpassOMSetRenderTargetArgs(uint32_t subpassIndex) { + Framebuffer::OMSetRenderTargetArgs Framebuffer::GetSubpassOMSetRenderTargetArgs( + uint32_t subpassIndex) { const auto& subpassInfo = GetRenderPass()->GetSubpassInfo(subpassIndex); OMSetRenderTargetArgs args = {}; @@ -89,5 +92,4 @@ namespace d3d12 { return mDsvHeap.GetCPUHandle(mAttachmentHeapIndices[attachmentSlot]); } -} -} +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/FramebufferD3D12.h b/src/backend/d3d12/FramebufferD3D12.h index 5855654287..4393b4cc8c 100644 --- a/src/backend/d3d12/FramebufferD3D12.h +++ b/src/backend/d3d12/FramebufferD3D12.h @@ -17,41 +17,39 @@ #include "backend/Framebuffer.h" -#include "common/Constants.h" -#include "backend/d3d12/d3d12_platform.h" #include "backend/d3d12/DescriptorHeapAllocator.h" +#include "backend/d3d12/d3d12_platform.h" +#include "common/Constants.h" #include #include -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { class Device; class Framebuffer : public FramebufferBase { - public: - struct OMSetRenderTargetArgs { - unsigned int numRTVs = 0; - std::array RTVs = {}; - D3D12_CPU_DESCRIPTOR_HANDLE dsv = {}; - }; + public: + struct OMSetRenderTargetArgs { + unsigned int numRTVs = 0; + std::array RTVs = {}; + D3D12_CPU_DESCRIPTOR_HANDLE dsv = {}; + }; - Framebuffer(Device* device, FramebufferBuilder* builder); - OMSetRenderTargetArgs GetSubpassOMSetRenderTargetArgs(uint32_t subpassIndex); - D3D12_CPU_DESCRIPTOR_HANDLE GetRTVDescriptor(uint32_t attachmentSlot); - D3D12_CPU_DESCRIPTOR_HANDLE GetDSVDescriptor(uint32_t attachmentSlot); + Framebuffer(Device* device, FramebufferBuilder* builder); + OMSetRenderTargetArgs GetSubpassOMSetRenderTargetArgs(uint32_t subpassIndex); + D3D12_CPU_DESCRIPTOR_HANDLE GetRTVDescriptor(uint32_t attachmentSlot); + D3D12_CPU_DESCRIPTOR_HANDLE GetDSVDescriptor(uint32_t attachmentSlot); - private: - Device* mDevice = nullptr; - DescriptorHeapHandle mRtvHeap = {}; - DescriptorHeapHandle mDsvHeap = {}; + private: + Device* mDevice = nullptr; + DescriptorHeapHandle mRtvHeap = {}; + DescriptorHeapHandle mDsvHeap = {}; - // Indices into either the RTV or DSV heap, depending on texture format. - std::vector mAttachmentHeapIndices; + // Indices into either the RTV or DSV heap, depending on texture format. + std::vector mAttachmentHeapIndices; }; -} -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_FRAMEBUFFERD3D12_H_ +#endif // BACKEND_D3D12_FRAMEBUFFERD3D12_H_ diff --git a/src/backend/d3d12/GeneratedCodeIncludes.h b/src/backend/d3d12/GeneratedCodeIncludes.h index f037f992c6..dbd0559eef 100644 --- a/src/backend/d3d12/GeneratedCodeIncludes.h +++ b/src/backend/d3d12/GeneratedCodeIncludes.h @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "backend/d3d12/D3D12Backend.h" #include "backend/d3d12/BindGroupD3D12.h" #include "backend/d3d12/BindGroupLayoutD3D12.h" #include "backend/d3d12/BlendStateD3D12.h" #include "backend/d3d12/BufferD3D12.h" #include "backend/d3d12/CommandBufferD3D12.h" #include "backend/d3d12/ComputePipelineD3D12.h" +#include "backend/d3d12/D3D12Backend.h" #include "backend/d3d12/DepthStencilStateD3D12.h" #include "backend/d3d12/FramebufferD3D12.h" #include "backend/d3d12/InputStateD3D12.h" diff --git a/src/backend/d3d12/InputStateD3D12.cpp b/src/backend/d3d12/InputStateD3D12.cpp index 592f64c10c..4b005fd0af 100644 --- a/src/backend/d3d12/InputStateD3D12.cpp +++ b/src/backend/d3d12/InputStateD3D12.cpp @@ -16,8 +16,7 @@ #include "common/BitSetIterator.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { static DXGI_FORMAT VertexFormatType(nxt::VertexFormat format) { switch (format) { @@ -47,7 +46,6 @@ namespace d3d12 { InputState::InputState(Device* device, InputStateBuilder* builder) : InputStateBase(builder), mDevice(device) { - const auto& attributesSetMask = GetAttributesSetMask(); unsigned int count = 0; @@ -60,7 +58,8 @@ namespace d3d12 { const AttributeInfo& attribute = GetAttribute(i); - // If the HLSL semantic is TEXCOORDN the SemanticName should be "TEXCOORD" and the SemanticIndex N + // If the HLSL semantic is TEXCOORDN the SemanticName should be "TEXCOORD" and the + // SemanticIndex N inputElementDescriptor.SemanticName = "TEXCOORD"; inputElementDescriptor.SemanticIndex = static_cast(i); inputElementDescriptor.Format = VertexFormatType(attribute.format); @@ -70,7 +69,8 @@ namespace d3d12 { inputElementDescriptor.AlignedByteOffset = attribute.offset; inputElementDescriptor.InputSlotClass = InputStepModeFunction(input.stepMode); - if (inputElementDescriptor.InputSlotClass == D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA) { + if (inputElementDescriptor.InputSlotClass == + D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA) { inputElementDescriptor.InstanceDataStepRate = 0; } else { inputElementDescriptor.InstanceDataStepRate = 1; @@ -79,12 +79,10 @@ namespace d3d12 { mInputLayoutDescriptor.pInputElementDescs = mInputElementDescriptors; mInputLayoutDescriptor.NumElements = count; - } const D3D12_INPUT_LAYOUT_DESC& InputState::GetD3D12InputLayoutDescriptor() const { return mInputLayoutDescriptor; } -} -} +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/InputStateD3D12.h b/src/backend/d3d12/InputStateD3D12.h index 9f1a1d3685..fcd86c8994 100644 --- a/src/backend/d3d12/InputStateD3D12.h +++ b/src/backend/d3d12/InputStateD3D12.h @@ -19,24 +19,22 @@ #include "backend/d3d12/d3d12_platform.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { class Device; class InputState : public InputStateBase { - public: - InputState(Device* device, InputStateBuilder* builder); + public: + InputState(Device* device, InputStateBuilder* builder); - const D3D12_INPUT_LAYOUT_DESC& GetD3D12InputLayoutDescriptor() const; + const D3D12_INPUT_LAYOUT_DESC& GetD3D12InputLayoutDescriptor() const; - private: - Device* mDevice; - D3D12_INPUT_LAYOUT_DESC mInputLayoutDescriptor; - D3D12_INPUT_ELEMENT_DESC mInputElementDescriptors[kMaxVertexAttributes]; + private: + Device* mDevice; + D3D12_INPUT_LAYOUT_DESC mInputLayoutDescriptor; + D3D12_INPUT_ELEMENT_DESC mInputElementDescriptors[kMaxVertexAttributes]; }; -} -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_INPUTSTATED3D12_H_ +#endif // BACKEND_D3D12_INPUTSTATED3D12_H_ diff --git a/src/backend/d3d12/PipelineLayoutD3D12.cpp b/src/backend/d3d12/PipelineLayoutD3D12.cpp index 090f46242b..6262eadaa3 100644 --- a/src/backend/d3d12/PipelineLayoutD3D12.cpp +++ b/src/backend/d3d12/PipelineLayoutD3D12.cpp @@ -14,18 +14,16 @@ #include "backend/d3d12/PipelineLayoutD3D12.h" -#include "backend/d3d12/D3D12Backend.h" #include "backend/d3d12/BindGroupLayoutD3D12.h" +#include "backend/d3d12/D3D12Backend.h" #include "common/Assert.h" using Microsoft::WRL::ComPtr; -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { PipelineLayout::PipelineLayout(Device* device, PipelineLayoutBuilder* builder) : PipelineLayoutBase(builder), mDevice(device) { - D3D12_ROOT_PARAMETER rootParameters[kMaxBindGroups * 2]; // A root parameter is one of these types @@ -34,7 +32,8 @@ namespace d3d12 { D3D12_ROOT_CONSTANTS Constants; D3D12_ROOT_DESCRIPTOR Descriptor; } rootParameterValues[kMaxBindGroups * 2]; - // samplers must be in a separate descriptor table so we need at most twice as many tables as bind groups + // samplers must be in a separate descriptor table so we need at most twice as many tables + // as bind groups // Ranges are D3D12_DESCRIPTOR_RANGE_TYPE_(SRV|UAV|CBV|SAMPLER) // They are grouped together so each bind group has at most 4 ranges @@ -46,9 +45,11 @@ namespace d3d12 { for (uint32_t group = 0; group < kMaxBindGroups; ++group) { const BindGroupLayout* bindGroupLayout = ToBackend(GetBindGroupLayout(group)); - // Set the root descriptor table parameter and copy ranges. Ranges are offset by the bind group index - // Returns whether or not the parameter was set. A root parameter is not set if the number of ranges is 0 - auto SetRootDescriptorTable = [&](uint32_t rangeCount, const D3D12_DESCRIPTOR_RANGE* descriptorRanges) -> bool { + // Set the root descriptor table parameter and copy ranges. Ranges are offset by the + // bind group index Returns whether or not the parameter was set. A root parameter is + // not set if the number of ranges is 0 + auto SetRootDescriptorTable = + [&](uint32_t rangeCount, const D3D12_DESCRIPTOR_RANGE* descriptorRanges) -> bool { if (rangeCount == 0) { return false; } @@ -69,11 +70,13 @@ namespace d3d12 { return true; }; - if (SetRootDescriptorTable(bindGroupLayout->GetCbvUavSrvDescriptorTableSize(), bindGroupLayout->GetCbvUavSrvDescriptorRanges())) { + if (SetRootDescriptorTable(bindGroupLayout->GetCbvUavSrvDescriptorTableSize(), + bindGroupLayout->GetCbvUavSrvDescriptorRanges())) { mCbvUavSrvRootParameterInfo[group] = parameterIndex++; } - if (SetRootDescriptorTable(bindGroupLayout->GetSamplerDescriptorTableSize(), bindGroupLayout->GetSamplerDescriptorRanges())) { + if (SetRootDescriptorTable(bindGroupLayout->GetSamplerDescriptorTableSize(), + bindGroupLayout->GetSamplerDescriptorRanges())) { mSamplerRootParameterInfo[group] = parameterIndex++; } } @@ -83,15 +86,18 @@ namespace d3d12 { rootSignatureDescriptor.pParameters = rootParameters; rootSignatureDescriptor.NumStaticSamplers = 0; rootSignatureDescriptor.pStaticSamplers = nullptr; - rootSignatureDescriptor.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT; + rootSignatureDescriptor.Flags = + D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT; ComPtr signature; ComPtr error; - ASSERT_SUCCESS(D3D12SerializeRootSignature(&rootSignatureDescriptor, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error)); - ASSERT_SUCCESS(device->GetD3D12Device()->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&mRootSignature))); + ASSERT_SUCCESS(D3D12SerializeRootSignature( + &rootSignatureDescriptor, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error)); + ASSERT_SUCCESS(device->GetD3D12Device()->CreateRootSignature( + 0, signature->GetBufferPointer(), signature->GetBufferSize(), + IID_PPV_ARGS(&mRootSignature))); } - uint32_t PipelineLayout::GetCbvUavSrvRootParameterIndex(uint32_t group) const { ASSERT(group < kMaxBindGroups); return mCbvUavSrvRootParameterInfo[group]; @@ -105,5 +111,4 @@ namespace d3d12 { ComPtr PipelineLayout::GetRootSignature() { return mRootSignature; } -} -} +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/PipelineLayoutD3D12.h b/src/backend/d3d12/PipelineLayoutD3D12.h index ff80944971..a7af1f7301 100644 --- a/src/backend/d3d12/PipelineLayoutD3D12.h +++ b/src/backend/d3d12/PipelineLayoutD3D12.h @@ -19,30 +19,28 @@ #include "backend/d3d12/d3d12_platform.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { class Device; class PipelineLayout : public PipelineLayoutBase { - public: - PipelineLayout(Device* device, PipelineLayoutBuilder* builder); + public: + PipelineLayout(Device* device, PipelineLayoutBuilder* builder); - uint32_t GetCbvUavSrvRootParameterIndex(uint32_t group) const; - uint32_t GetSamplerRootParameterIndex(uint32_t group) const; + uint32_t GetCbvUavSrvRootParameterIndex(uint32_t group) const; + uint32_t GetSamplerRootParameterIndex(uint32_t group) const; - ComPtr GetRootSignature(); + ComPtr GetRootSignature(); - private: - Device* mDevice; + private: + Device* mDevice; - std::array mCbvUavSrvRootParameterInfo; - std::array mSamplerRootParameterInfo; + std::array mCbvUavSrvRootParameterInfo; + std::array mSamplerRootParameterInfo; - ComPtr mRootSignature; + ComPtr mRootSignature; }; -} -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_PIPELINELAYOUTD3D12_H_ +#endif // BACKEND_D3D12_PIPELINELAYOUTD3D12_H_ diff --git a/src/backend/d3d12/QueueD3D12.cpp b/src/backend/d3d12/QueueD3D12.cpp index ba3f860d2e..861ba18f82 100644 --- a/src/backend/d3d12/QueueD3D12.cpp +++ b/src/backend/d3d12/QueueD3D12.cpp @@ -14,17 +14,15 @@ #include "backend/d3d12/QueueD3D12.h" -#include "backend/d3d12/D3D12Backend.h" #include "backend/d3d12/CommandBufferD3D12.h" +#include "backend/d3d12/D3D12Backend.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { - Queue::Queue(Device* device, QueueBuilder* builder) - : QueueBase(builder), mDevice(device) { + Queue::Queue(Device* device, QueueBuilder* builder) : QueueBase(builder), mDevice(device) { } - void Queue::Submit(uint32_t numCommands, CommandBuffer* const * commands) { + void Queue::Submit(uint32_t numCommands, CommandBuffer* const* commands) { mDevice->Tick(); mDevice->OpenCommandList(&mCommandList); @@ -33,10 +31,9 @@ namespace d3d12 { } ASSERT_SUCCESS(mCommandList->Close()); - mDevice->ExecuteCommandLists({ mCommandList.Get() }); + mDevice->ExecuteCommandLists({mCommandList.Get()}); mDevice->NextSerial(); } -} -} +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/QueueD3D12.h b/src/backend/d3d12/QueueD3D12.h index 668b3b74e4..c0adb2a7b6 100644 --- a/src/backend/d3d12/QueueD3D12.h +++ b/src/backend/d3d12/QueueD3D12.h @@ -19,26 +19,24 @@ #include "backend/d3d12/d3d12_platform.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { class Device; class CommandBuffer; class Queue : public QueueBase { - public: - Queue(Device* device, QueueBuilder* builder); + public: + Queue(Device* device, QueueBuilder* builder); - // NXT API - void Submit(uint32_t numCommands, CommandBuffer* const * commands); + // NXT API + void Submit(uint32_t numCommands, CommandBuffer* const* commands); - private: - Device* mDevice; + private: + Device* mDevice; - ComPtr mCommandList; + ComPtr mCommandList; }; -} -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_QUEUED3D12_H_ +#endif // BACKEND_D3D12_QUEUED3D12_H_ diff --git a/src/backend/d3d12/RenderPipelineD3D12.cpp b/src/backend/d3d12/RenderPipelineD3D12.cpp index d400eb5d8a..3e23dfd28a 100644 --- a/src/backend/d3d12/RenderPipelineD3D12.cpp +++ b/src/backend/d3d12/RenderPipelineD3D12.cpp @@ -18,15 +18,14 @@ #include "backend/d3d12/D3D12Backend.h" #include "backend/d3d12/DepthStencilStateD3D12.h" #include "backend/d3d12/InputStateD3D12.h" +#include "backend/d3d12/PipelineLayoutD3D12.h" #include "backend/d3d12/ShaderModuleD3D12.h" #include "backend/d3d12/TextureD3D12.h" -#include "backend/d3d12/PipelineLayoutD3D12.h" #include "common/Assert.h" #include -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { namespace { D3D12_PRIMITIVE_TOPOLOGY D3D12PrimitiveTopology(nxt::PrimitiveTopology primitiveTopology) { @@ -46,7 +45,8 @@ namespace d3d12 { } } - D3D12_PRIMITIVE_TOPOLOGY_TYPE D3D12PrimitiveTopologyType(nxt::PrimitiveTopology primitiveTopology) { + D3D12_PRIMITIVE_TOPOLOGY_TYPE D3D12PrimitiveTopologyType( + nxt::PrimitiveTopology primitiveTopology) { switch (primitiveTopology) { case nxt::PrimitiveTopology::PointList: return D3D12_PRIMITIVE_TOPOLOGY_TYPE_POINT; @@ -60,10 +60,11 @@ namespace d3d12 { UNREACHABLE(); } } - } + } // namespace RenderPipeline::RenderPipeline(RenderPipelineBuilder* builder) - : RenderPipelineBase(builder), mD3d12PrimitiveTopology(D3D12PrimitiveTopology(GetPrimitiveTopology())) { + : RenderPipelineBase(builder), + mD3d12PrimitiveTopology(D3D12PrimitiveTopology(GetPrimitiveTopology())) { uint32_t compileFlags = 0; #if defined(_DEBUG) // Enable better shader debugging with the graphics debugging tools. @@ -86,32 +87,22 @@ namespace d3d12 { D3D12_SHADER_BYTECODE* shader = nullptr; switch (stage) { - case nxt::ShaderStage::Vertex: - shader = &descriptor.VS; - compileTarget = "vs_5_1"; - break; - case nxt::ShaderStage::Fragment: - shader = &descriptor.PS; - compileTarget = "ps_5_1"; - break; - case nxt::ShaderStage::Compute: - UNREACHABLE(); - break; + case nxt::ShaderStage::Vertex: + shader = &descriptor.VS; + compileTarget = "vs_5_1"; + break; + case nxt::ShaderStage::Fragment: + shader = &descriptor.PS; + compileTarget = "ps_5_1"; + break; + case nxt::ShaderStage::Compute: + UNREACHABLE(); + break; } - if(FAILED(D3DCompile( - hlslSource.c_str(), - hlslSource.length(), - nullptr, - nullptr, - nullptr, - entryPoint.c_str(), - compileTarget, - compileFlags, - 0, - &compiledShader[stage], - &errors - ))) { + if (FAILED(D3DCompile(hlslSource.c_str(), hlslSource.length(), nullptr, nullptr, + nullptr, entryPoint.c_str(), compileTarget, compileFlags, 0, + &compiledShader[stage], &errors))) { printf("%s\n", reinterpret_cast(errors->GetBufferPointer())); ASSERT(false); } @@ -148,7 +139,8 @@ namespace d3d12 { auto& subpassInfo = renderPass->GetSubpassInfo(GetSubPass()); if (subpassInfo.depthStencilAttachmentSet) { - const auto& attachmentInfo = renderPass->GetAttachmentInfo(subpassInfo.depthStencilAttachment); + const auto& attachmentInfo = + renderPass->GetAttachmentInfo(subpassInfo.depthStencilAttachment); descriptor.DSVFormat = D3D12TextureFormat(attachmentInfo.format); } @@ -158,7 +150,8 @@ namespace d3d12 { const auto& attachmentInfo = renderPass->GetAttachmentInfo(attachment); descriptor.RTVFormats[attachmentSlot] = D3D12TextureFormat(attachmentInfo.format); - descriptor.BlendState.RenderTarget[attachmentSlot] = ToBackend(GetBlendState(attachmentSlot))->GetD3D12BlendDesc(); + descriptor.BlendState.RenderTarget[attachmentSlot] = + ToBackend(GetBlendState(attachmentSlot))->GetD3D12BlendDesc(); attachmentCount = attachmentSlot + 1; } descriptor.NumRenderTargets = attachmentCount; @@ -174,7 +167,8 @@ namespace d3d12 { descriptor.SampleDesc.Count = 1; Device* device = ToBackend(builder->GetDevice()); - ASSERT_SUCCESS(device->GetD3D12Device()->CreateGraphicsPipelineState(&descriptor, IID_PPV_ARGS(&mPipelineState))); + ASSERT_SUCCESS(device->GetD3D12Device()->CreateGraphicsPipelineState( + &descriptor, IID_PPV_ARGS(&mPipelineState))); } D3D12_PRIMITIVE_TOPOLOGY RenderPipeline::GetD3D12PrimitiveTopology() const { @@ -185,5 +179,4 @@ namespace d3d12 { return mPipelineState; } -} -} +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/RenderPipelineD3D12.h b/src/backend/d3d12/RenderPipelineD3D12.h index e27ec37952..ad29870993 100644 --- a/src/backend/d3d12/RenderPipelineD3D12.h +++ b/src/backend/d3d12/RenderPipelineD3D12.h @@ -19,22 +19,20 @@ #include "backend/d3d12/d3d12_platform.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { class RenderPipeline : public RenderPipelineBase { - public: - RenderPipeline(RenderPipelineBuilder* builder); + public: + RenderPipeline(RenderPipelineBuilder* builder); - D3D12_PRIMITIVE_TOPOLOGY GetD3D12PrimitiveTopology() const; - ComPtr GetPipelineState(); + D3D12_PRIMITIVE_TOPOLOGY GetD3D12PrimitiveTopology() const; + ComPtr GetPipelineState(); - private: - D3D12_PRIMITIVE_TOPOLOGY mD3d12PrimitiveTopology; - ComPtr mPipelineState; + private: + D3D12_PRIMITIVE_TOPOLOGY mD3d12PrimitiveTopology; + ComPtr mPipelineState; }; -} -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_RENDERPIPELINED3D12_H_ +#endif // BACKEND_D3D12_RENDERPIPELINED3D12_H_ diff --git a/src/backend/d3d12/ResourceAllocator.cpp b/src/backend/d3d12/ResourceAllocator.cpp index edfde70e1b..251e604e5a 100644 --- a/src/backend/d3d12/ResourceAllocator.cpp +++ b/src/backend/d3d12/ResourceAllocator.cpp @@ -16,41 +16,31 @@ #include "backend/d3d12/D3D12Backend.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { namespace { static constexpr D3D12_HEAP_PROPERTIES kDefaultHeapProperties = { - D3D12_HEAP_TYPE_DEFAULT, - D3D12_CPU_PAGE_PROPERTY_UNKNOWN, - D3D12_MEMORY_POOL_UNKNOWN, - 0, - 0 - }; + D3D12_HEAP_TYPE_DEFAULT, D3D12_CPU_PAGE_PROPERTY_UNKNOWN, D3D12_MEMORY_POOL_UNKNOWN, 0, + 0}; static constexpr D3D12_HEAP_PROPERTIES kUploadHeapProperties = { - D3D12_HEAP_TYPE_UPLOAD, - D3D12_CPU_PAGE_PROPERTY_UNKNOWN, - D3D12_MEMORY_POOL_UNKNOWN, - 0, - 0 - }; + D3D12_HEAP_TYPE_UPLOAD, D3D12_CPU_PAGE_PROPERTY_UNKNOWN, D3D12_MEMORY_POOL_UNKNOWN, 0, + 0}; static constexpr D3D12_HEAP_PROPERTIES kReadbackHeapProperties = { - D3D12_HEAP_TYPE_READBACK, - D3D12_CPU_PAGE_PROPERTY_UNKNOWN, - D3D12_MEMORY_POOL_UNKNOWN, - 0, - 0 - }; - } + D3D12_HEAP_TYPE_READBACK, D3D12_CPU_PAGE_PROPERTY_UNKNOWN, D3D12_MEMORY_POOL_UNKNOWN, 0, + 0}; + } // namespace ResourceAllocator::ResourceAllocator(Device* device) : mDevice(device) { } - ComPtr ResourceAllocator::Allocate(D3D12_HEAP_TYPE heapType, const D3D12_RESOURCE_DESC &resourceDescriptor, D3D12_RESOURCE_STATES initialUsage) { + ComPtr ResourceAllocator::Allocate( + D3D12_HEAP_TYPE heapType, + const D3D12_RESOURCE_DESC& resourceDescriptor, + D3D12_RESOURCE_STATES initialUsage) { const D3D12_HEAP_PROPERTIES* heapProperties = nullptr; - switch(heapType) { + switch (heapType) { case D3D12_HEAP_TYPE_DEFAULT: heapProperties = &kDefaultHeapProperties; break; @@ -68,19 +58,15 @@ namespace d3d12 { // TODO(enga@google.com): Use CreatePlacedResource ASSERT_SUCCESS(mDevice->GetD3D12Device()->CreateCommittedResource( - heapProperties, - D3D12_HEAP_FLAG_NONE, - &resourceDescriptor, - initialUsage, - nullptr, - IID_PPV_ARGS(&resource) - )); + heapProperties, D3D12_HEAP_FLAG_NONE, &resourceDescriptor, initialUsage, nullptr, + IID_PPV_ARGS(&resource))); return resource; } void ResourceAllocator::Release(ComPtr resource) { - // Resources may still be in use on the GPU. Enqueue them so that we hold onto them until GPU execution has completed + // Resources may still be in use on the GPU. Enqueue them so that we hold onto them until + // GPU execution has completed mReleasedResources.Enqueue(resource, mDevice->GetSerial()); } @@ -88,5 +74,4 @@ namespace d3d12 { mReleasedResources.ClearUpTo(lastCompletedSerial); } -} -} +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/ResourceAllocator.h b/src/backend/d3d12/ResourceAllocator.h index 287619fb9f..df56119d5f 100644 --- a/src/backend/d3d12/ResourceAllocator.h +++ b/src/backend/d3d12/ResourceAllocator.h @@ -19,27 +19,26 @@ #include "common/SerialQueue.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { class Device; class ResourceAllocator { + public: + ResourceAllocator(Device* device); - public: - ResourceAllocator(Device* device); + ComPtr Allocate(D3D12_HEAP_TYPE heapType, + const D3D12_RESOURCE_DESC& resourceDescriptor, + D3D12_RESOURCE_STATES initialUsage); + void Release(ComPtr resource); + void Tick(uint64_t lastCompletedSerial); - ComPtr Allocate(D3D12_HEAP_TYPE heapType, const D3D12_RESOURCE_DESC &resourceDescriptor, D3D12_RESOURCE_STATES initialUsage); - void Release(ComPtr resource); - void Tick(uint64_t lastCompletedSerial); + private: + Device* mDevice; - private: - Device* mDevice; - - SerialQueue> mReleasedResources; + SerialQueue> mReleasedResources; }; -} -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_RESOURCEALLOCATIONMANAGER_H_ +#endif // BACKEND_D3D12_RESOURCEALLOCATIONMANAGER_H_ diff --git a/src/backend/d3d12/ResourceUploader.cpp b/src/backend/d3d12/ResourceUploader.cpp index 19be7c266b..bd1ac3f107 100644 --- a/src/backend/d3d12/ResourceUploader.cpp +++ b/src/backend/d3d12/ResourceUploader.cpp @@ -17,23 +17,29 @@ #include "backend/d3d12/D3D12Backend.h" #include "backend/d3d12/ResourceAllocator.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { ResourceUploader::ResourceUploader(Device* device) : mDevice(device) { } - void ResourceUploader::BufferSubData(ComPtr resource, uint32_t start, uint32_t count, const void* data) { - // TODO(enga@google.com): Use a handle to a subset of a large ring buffer. On Release, decrease reference count on the ring buffer and free when 0. - // Alternatively, the SerialQueue could be used to track which last point of the ringbuffer is in use, and start reusing chunks of it that aren't in flight. + void ResourceUploader::BufferSubData(ComPtr resource, + uint32_t start, + uint32_t count, + const void* data) { + // TODO(enga@google.com): Use a handle to a subset of a large ring buffer. On Release, + // decrease reference count on the ring buffer and free when 0. Alternatively, the + // SerialQueue could be used to track which last point of the ringbuffer is in use, and + // start reusing chunks of it that aren't in flight. UploadHandle uploadHandle = GetUploadBuffer(count); memcpy(uploadHandle.mappedBuffer, data, count); - mDevice->GetPendingCommandList()->CopyBufferRegion(resource.Get(), start, uploadHandle.resource.Get(), 0, count); + mDevice->GetPendingCommandList()->CopyBufferRegion(resource.Get(), start, + uploadHandle.resource.Get(), 0, count); Release(uploadHandle); } ResourceUploader::UploadHandle ResourceUploader::GetUploadBuffer(uint32_t requiredSize) { - // TODO(enga@google.com): This will find or create a mapped buffer of sufficient size and return a handle to a mapped range + // TODO(enga@google.com): This will find or create a mapped buffer of sufficient size and + // return a handle to a mapped range D3D12_RESOURCE_DESC resourceDescriptor; resourceDescriptor.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; resourceDescriptor.Alignment = 0; @@ -48,12 +54,14 @@ namespace d3d12 { resourceDescriptor.Flags = D3D12_RESOURCE_FLAG_NONE; UploadHandle uploadHandle; - uploadHandle.resource = mDevice->GetResourceAllocator()->Allocate(D3D12_HEAP_TYPE_UPLOAD, resourceDescriptor, D3D12_RESOURCE_STATE_GENERIC_READ); + uploadHandle.resource = mDevice->GetResourceAllocator()->Allocate( + D3D12_HEAP_TYPE_UPLOAD, resourceDescriptor, D3D12_RESOURCE_STATE_GENERIC_READ); D3D12_RANGE readRange; readRange.Begin = 0; readRange.End = 0; - uploadHandle.resource->Map(0, &readRange, reinterpret_cast(&uploadHandle.mappedBuffer)); + uploadHandle.resource->Map(0, &readRange, + reinterpret_cast(&uploadHandle.mappedBuffer)); return uploadHandle; } @@ -62,5 +70,4 @@ namespace d3d12 { mDevice->GetResourceAllocator()->Release(uploadHandle.resource); } -} -} +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/ResourceUploader.h b/src/backend/d3d12/ResourceUploader.h index cdcb372d49..e861df69dd 100644 --- a/src/backend/d3d12/ResourceUploader.h +++ b/src/backend/d3d12/ResourceUploader.h @@ -19,29 +19,30 @@ #include "backend/Forward.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { class Device; class ResourceUploader { - public: - ResourceUploader(Device* device); + public: + ResourceUploader(Device* device); - void BufferSubData(ComPtr resource, uint32_t start, uint32_t count, const void* data); + void BufferSubData(ComPtr resource, + uint32_t start, + uint32_t count, + const void* data); - private: - struct UploadHandle { - ComPtr resource; - uint8_t* mappedBuffer; - }; + private: + struct UploadHandle { + ComPtr resource; + uint8_t* mappedBuffer; + }; - UploadHandle GetUploadBuffer(uint32_t requiredSize); - void Release(UploadHandle uploadHandle); + UploadHandle GetUploadBuffer(uint32_t requiredSize); + void Release(UploadHandle uploadHandle); - Device* mDevice; + Device* mDevice; }; -} -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_RESOURCEUPLOADER_H_ +#endif // BACKEND_D3D12_RESOURCEUPLOADER_H_ diff --git a/src/backend/d3d12/SamplerD3D12.cpp b/src/backend/d3d12/SamplerD3D12.cpp index 18f8e854c7..5ba83b5415 100644 --- a/src/backend/d3d12/SamplerD3D12.cpp +++ b/src/backend/d3d12/SamplerD3D12.cpp @@ -16,14 +16,12 @@ #include "backend/d3d12/D3D12Backend.h" -namespace backend { -namespace d3d12 { - - Sampler::Sampler(SamplerBuilder* builder) - : SamplerBase(builder) { +namespace backend { namespace d3d12 { + Sampler::Sampler(SamplerBuilder* builder) : SamplerBase(builder) { // https://msdn.microsoft.com/en-us/library/windows/desktop/dn770367(v=vs.85).aspx - // D3D12_FILTER_MIN_MAG_MIP_POINT = 0 0 0 0 0 // hex value, decimal value, min linear, mag linear, mip linear + // hex value, decimal value, min linear, mag linear, mip linear + // D3D12_FILTER_MIN_MAG_MIP_POINT = 0 0 0 0 0 // D3D12_FILTER_MIN_MAG_POINT_MIP_LINEAR = 0x1 1 0 0 1 // D3D12_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT = 0x4 4 0 1 0 // D3D12_FILTER_MIN_POINT_MAG_MIP_LINEAR = 0x5 5 0 1 1 @@ -69,15 +67,14 @@ namespace d3d12 { mSamplerDesc.MipLODBias = 0.f; mSamplerDesc.MaxAnisotropy = 1; mSamplerDesc.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS; - mSamplerDesc.BorderColor[0] = mSamplerDesc.BorderColor[1] = mSamplerDesc.BorderColor[2] = mSamplerDesc.BorderColor[3] = 0; + mSamplerDesc.BorderColor[0] = mSamplerDesc.BorderColor[1] = mSamplerDesc.BorderColor[2] = + mSamplerDesc.BorderColor[3] = 0; mSamplerDesc.MinLOD = 0; mSamplerDesc.MaxLOD = D3D12_FLOAT32_MAX; - } const D3D12_SAMPLER_DESC& Sampler::GetSamplerDescriptor() const { return mSamplerDesc; } -} -} +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/SamplerD3D12.h b/src/backend/d3d12/SamplerD3D12.h index 153a531d24..9d3600dfc0 100644 --- a/src/backend/d3d12/SamplerD3D12.h +++ b/src/backend/d3d12/SamplerD3D12.h @@ -19,20 +19,18 @@ #include "backend/d3d12/d3d12_platform.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { class Sampler : public SamplerBase { - public: - Sampler(SamplerBuilder* builder); + public: + Sampler(SamplerBuilder* builder); - const D3D12_SAMPLER_DESC& GetSamplerDescriptor() const; + const D3D12_SAMPLER_DESC& GetSamplerDescriptor() const; - private: - D3D12_SAMPLER_DESC mSamplerDesc; + private: + D3D12_SAMPLER_DESC mSamplerDesc; }; -} -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_SAMPLERD3D12_H_ +#endif // BACKEND_D3D12_SAMPLERD3D12_H_ diff --git a/src/backend/d3d12/ShaderModuleD3D12.cpp b/src/backend/d3d12/ShaderModuleD3D12.cpp index 3056875149..ea53b12b55 100644 --- a/src/backend/d3d12/ShaderModuleD3D12.cpp +++ b/src/backend/d3d12/ShaderModuleD3D12.cpp @@ -16,8 +16,7 @@ #include -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { ShaderModule::ShaderModule(Device* device, ShaderModuleBuilder* builder) : ShaderModuleBase(builder), mDevice(device) { @@ -34,15 +33,18 @@ namespace d3d12 { ExtractSpirvInfo(compiler); - // rename bindings so that each register type b/u/t/s starts at 0 and then offset by kMaxBindingsPerGroup * bindGroupIndex + // rename bindings so that each register type b/u/t/s starts at 0 and then offset by + // kMaxBindingsPerGroup * bindGroupIndex auto RenumberBindings = [&](std::vector resources) { std::array baseRegisters = {}; for (const auto& resource : resources) { - auto bindGroupIndex = compiler.get_decoration(resource.id, spv::DecorationDescriptorSet); + auto bindGroupIndex = + compiler.get_decoration(resource.id, spv::DecorationDescriptorSet); auto& baseRegister = baseRegisters[bindGroupIndex]; auto bindGroupOffset = bindGroupIndex * kMaxBindingsPerGroup; - compiler.set_decoration(resource.id, spv::DecorationBinding, bindGroupOffset + baseRegister++); + compiler.set_decoration(resource.id, spv::DecorationBinding, + bindGroupOffset + baseRegister++); } }; @@ -59,5 +61,4 @@ namespace d3d12 { return mHlslSource; } -} -} +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/ShaderModuleD3D12.h b/src/backend/d3d12/ShaderModuleD3D12.h index cba2fc4210..0cedd6df7e 100644 --- a/src/backend/d3d12/ShaderModuleD3D12.h +++ b/src/backend/d3d12/ShaderModuleD3D12.h @@ -17,24 +17,22 @@ #include "backend/ShaderModule.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { class Device; class ShaderModule : public ShaderModuleBase { - public: - ShaderModule(Device* device, ShaderModuleBuilder* builder); + public: + ShaderModule(Device* device, ShaderModuleBuilder* builder); - const std::string& GetHLSLSource() const; + const std::string& GetHLSLSource() const; - private: - Device* mDevice; + private: + Device* mDevice; - std::string mHlslSource; + std::string mHlslSource; }; -} -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_SHADERMODULED3D12_H_ +#endif // BACKEND_D3D12_SHADERMODULED3D12_H_ diff --git a/src/backend/d3d12/SwapChainD3D12.cpp b/src/backend/d3d12/SwapChainD3D12.cpp index 4d3d87425d..70e718774c 100644 --- a/src/backend/d3d12/SwapChainD3D12.cpp +++ b/src/backend/d3d12/SwapChainD3D12.cpp @@ -19,11 +19,9 @@ #include -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { - SwapChain::SwapChain(SwapChainBuilder* builder) - : SwapChainBase(builder) { + SwapChain::SwapChain(SwapChainBuilder* builder) : SwapChainBase(builder) { const auto& im = GetImplementation(); nxtWSIContextD3D12 wsiContext = {}; wsiContext.device = reinterpret_cast(GetDevice()); @@ -46,6 +44,4 @@ namespace d3d12 { return new Texture(builder, nativeTexture); } -} -} - +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/SwapChainD3D12.h b/src/backend/d3d12/SwapChainD3D12.h index 139f9e820d..fa39bf85c7 100644 --- a/src/backend/d3d12/SwapChainD3D12.h +++ b/src/backend/d3d12/SwapChainD3D12.h @@ -17,19 +17,17 @@ #include "backend/SwapChain.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { class SwapChain : public SwapChainBase { - public: - SwapChain(SwapChainBuilder* builder); - ~SwapChain(); + public: + SwapChain(SwapChainBuilder* builder); + ~SwapChain(); - protected: - TextureBase* GetNextTextureImpl(TextureBuilder* builder) override; + protected: + TextureBase* GetNextTextureImpl(TextureBuilder* builder) override; }; -} -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_SWAPCHAIN_D3D12_H_ +#endif // BACKEND_D3D12_SWAPCHAIN_D3D12_H_ diff --git a/src/backend/d3d12/TextureCopySplitter.cpp b/src/backend/d3d12/TextureCopySplitter.cpp index 5bc34db3b6..040f44d026 100644 --- a/src/backend/d3d12/TextureCopySplitter.cpp +++ b/src/backend/d3d12/TextureCopySplitter.cpp @@ -17,11 +17,16 @@ #include "backend/d3d12/d3d12_platform.h" #include "common/Assert.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { namespace { - void ComputeTexelOffsets(uint32_t offset, uint32_t rowPitch, uint32_t slicePitch, uint32_t texelSize, uint32_t* texelOffsetX, uint32_t* texelOffsetY, uint32_t* texelOffsetZ) { + void ComputeTexelOffsets(uint32_t offset, + uint32_t rowPitch, + uint32_t slicePitch, + uint32_t texelSize, + uint32_t* texelOffsetX, + uint32_t* texelOffsetY, + uint32_t* texelOffsetZ) { uint32_t byteOffsetX = offset % rowPitch; offset -= byteOffsetX; uint32_t byteOffsetY = offset % slicePitch; @@ -31,10 +36,17 @@ namespace d3d12 { *texelOffsetY = byteOffsetY / rowPitch; *texelOffsetZ = byteOffsetZ / slicePitch; } - } - - TextureCopySplit ComputeTextureCopySplit(uint32_t x, uint32_t y, uint32_t z, uint32_t width, uint32_t height, uint32_t depth, uint32_t texelSize, uint32_t offset, uint32_t rowPitch) { + } // namespace + TextureCopySplit ComputeTextureCopySplit(uint32_t x, + uint32_t y, + uint32_t z, + uint32_t width, + uint32_t height, + uint32_t depth, + uint32_t texelSize, + uint32_t offset, + uint32_t rowPitch) { TextureCopySplit copy; if (z != 0 || depth > 1) { @@ -58,27 +70,30 @@ namespace d3d12 { copy.copies[0].copySize.width = width; copy.copies[0].copySize.height = height; copy.copies[0].copySize.depth = depth; - + copy.copies[0].bufferOffset.x = 0; copy.copies[0].bufferOffset.y = 0; copy.copies[0].bufferOffset.z = 0; copy.copies[0].bufferSize.width = width; copy.copies[0].bufferSize.height = height; copy.copies[0].bufferSize.depth = depth; - - // Return early. There is only one copy needed because the offset is already 512-byte aligned + + // Return early. There is only one copy needed because the offset is already 512-byte + // aligned return copy; } ASSERT(alignedOffset < offset); uint32_t texelOffsetX, texelOffsetY, texelOffsetZ; - ComputeTexelOffsets(offset - alignedOffset, rowPitch, rowPitch * height, texelSize, &texelOffsetX, &texelOffsetY, &texelOffsetZ); + ComputeTexelOffsets(offset - alignedOffset, rowPitch, rowPitch * height, texelSize, + &texelOffsetX, &texelOffsetY, &texelOffsetZ); uint32_t rowPitchInTexels = rowPitch / texelSize; if (width + texelOffsetX <= rowPitchInTexels) { - // The region's rows fit inside the row pitch. In this case, extend the width of the PlacedFootprint and copy the buffer with an offset location + // The region's rows fit inside the row pitch. In this case, extend the width of the + // PlacedFootprint and copy the buffer with an offset location // |<--------------- row pitch --------------->| // // |-------------------------------------------| @@ -133,7 +148,6 @@ namespace d3d12 { // |+++++++++ | // |-------------------------------------------| - // Copy 0: // |-------------------------------------------| // | | @@ -173,7 +187,6 @@ namespace d3d12 { copy.copies[0].bufferSize.height = height + texelOffsetY; copy.copies[0].bufferSize.depth = depth + texelOffsetZ; - copy.copies[1].textureOffset.x = x + copy.copies[0].copySize.width; copy.copies[1].textureOffset.y = y; copy.copies[1].textureOffset.z = z; @@ -190,10 +203,7 @@ namespace d3d12 { copy.copies[1].bufferSize.height = height + texelOffsetY + 1; copy.copies[1].bufferSize.depth = depth + texelOffsetZ; - return copy; - } -} -} +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/TextureCopySplitter.h b/src/backend/d3d12/TextureCopySplitter.h index 75c20c250c..66a3fe19cc 100644 --- a/src/backend/d3d12/TextureCopySplitter.h +++ b/src/backend/d3d12/TextureCopySplitter.h @@ -19,14 +19,11 @@ #include -namespace backend { -namespace d3d12 { - +namespace backend { namespace d3d12 { struct TextureCopySplit { - static constexpr unsigned int kMaxTextureCopyRegions = 2; - + struct Extent { uint32_t width = 0; uint32_t height = 0; @@ -52,9 +49,16 @@ namespace d3d12 { std::array copies; }; - TextureCopySplit ComputeTextureCopySplit(uint32_t x, uint32_t y, uint32_t z, uint32_t width, uint32_t height, uint32_t depth, uint32_t texelSize, uint32_t offset, uint32_t rowPitch); + TextureCopySplit ComputeTextureCopySplit(uint32_t x, + uint32_t y, + uint32_t z, + uint32_t width, + uint32_t height, + uint32_t depth, + uint32_t texelSize, + uint32_t offset, + uint32_t rowPitch); -} -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_TEXTURECOPYSPLITTER_H_ +#endif // BACKEND_D3D12_TEXTURECOPYSPLITTER_H_ diff --git a/src/backend/d3d12/TextureD3D12.cpp b/src/backend/d3d12/TextureD3D12.cpp index ad701de662..1bcfd7b17b 100644 --- a/src/backend/d3d12/TextureD3D12.cpp +++ b/src/backend/d3d12/TextureD3D12.cpp @@ -17,11 +17,11 @@ #include "backend/d3d12/D3D12Backend.h" #include "backend/d3d12/ResourceAllocator.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { namespace { - D3D12_RESOURCE_STATES D3D12TextureUsage(nxt::TextureUsageBit usage, nxt::TextureFormat format) { + D3D12_RESOURCE_STATES D3D12TextureUsage(nxt::TextureUsageBit usage, + nxt::TextureFormat format) { D3D12_RESOURCE_STATES resourceState = D3D12_RESOURCE_STATE_COMMON; if (usage & nxt::TextureUsageBit::TransferSrc) { @@ -31,7 +31,8 @@ namespace d3d12 { resourceState |= D3D12_RESOURCE_STATE_COPY_DEST; } if (usage & nxt::TextureUsageBit::Sampled) { - resourceState |= (D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE); + resourceState |= (D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | + D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE); } if (usage & nxt::TextureUsageBit::Storage) { resourceState |= D3D12_RESOURCE_STATE_UNORDERED_ACCESS; @@ -47,7 +48,8 @@ namespace d3d12 { return resourceState; } - D3D12_RESOURCE_FLAGS D3D12ResourceFlags(nxt::TextureUsageBit usage, nxt::TextureFormat format) { + D3D12_RESOURCE_FLAGS D3D12ResourceFlags(nxt::TextureUsageBit usage, + nxt::TextureFormat format) { D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE; if (usage & nxt::TextureUsageBit::Storage) { @@ -62,7 +64,7 @@ namespace d3d12 { } ASSERT(!(flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL) || - flags == D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL); + flags == D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL); return flags; } @@ -75,7 +77,7 @@ namespace d3d12 { } } - } + } // namespace DXGI_FORMAT D3D12TextureFormat(nxt::TextureFormat format) { switch (format) { @@ -94,7 +96,6 @@ namespace d3d12 { Texture::Texture(TextureBuilder* builder) : TextureBase(builder), mDevice(ToBackend(builder->GetDevice())) { - D3D12_RESOURCE_DESC resourceDescriptor; resourceDescriptor.Dimension = D3D12TextureDimension(GetDimension()); resourceDescriptor.Alignment = 0; @@ -108,14 +109,17 @@ namespace d3d12 { resourceDescriptor.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; resourceDescriptor.Flags = D3D12ResourceFlags(GetAllowedUsage(), GetFormat()); - mResource = mDevice->GetResourceAllocator()->Allocate(D3D12_HEAP_TYPE_DEFAULT, resourceDescriptor, D3D12TextureUsage(GetUsage(), GetFormat())); + mResource = + mDevice->GetResourceAllocator()->Allocate(D3D12_HEAP_TYPE_DEFAULT, resourceDescriptor, + D3D12TextureUsage(GetUsage(), GetFormat())); mResourcePtr = mResource.Get(); } // With this constructor, the lifetime of the ID3D12Resource is externally managed. Texture::Texture(TextureBuilder* builder, ID3D12Resource* nativeTexture) - : TextureBase(builder), mDevice(ToBackend(builder->GetDevice())), - mResourcePtr(nativeTexture) { + : TextureBase(builder), + mDevice(ToBackend(builder->GetDevice())), + mResourcePtr(nativeTexture) { } Texture::~Texture() { @@ -133,7 +137,9 @@ namespace d3d12 { return mResourcePtr; } - bool Texture::GetResourceTransitionBarrier(nxt::TextureUsageBit currentUsage, nxt::TextureUsageBit targetUsage, D3D12_RESOURCE_BARRIER* barrier) { + bool Texture::GetResourceTransitionBarrier(nxt::TextureUsageBit currentUsage, + nxt::TextureUsageBit targetUsage, + D3D12_RESOURCE_BARRIER* barrier) { D3D12_RESOURCE_STATES stateBefore = D3D12TextureUsage(currentUsage, GetFormat()); D3D12_RESOURCE_STATES stateAfter = D3D12TextureUsage(targetUsage, GetFormat()); @@ -151,16 +157,15 @@ namespace d3d12 { return true; } - void Texture::TransitionUsageImpl(nxt::TextureUsageBit currentUsage, nxt::TextureUsageBit targetUsage) { + void Texture::TransitionUsageImpl(nxt::TextureUsageBit currentUsage, + nxt::TextureUsageBit targetUsage) { D3D12_RESOURCE_BARRIER barrier; if (GetResourceTransitionBarrier(currentUsage, targetUsage, &barrier)) { mDevice->GetPendingCommandList()->ResourceBarrier(1, &barrier); } } - TextureView::TextureView(TextureViewBuilder* builder) - : TextureViewBase(builder) { - + TextureView::TextureView(TextureViewBuilder* builder) : TextureViewBase(builder) { mSrvDesc.Format = D3D12TextureFormat(GetTexture()->GetFormat()); mSrvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; switch (GetTexture()->GetDimension()) { @@ -196,5 +201,4 @@ namespace d3d12 { return dsvDesc; } -} -} +}} // namespace backend::d3d12 diff --git a/src/backend/d3d12/TextureD3D12.h b/src/backend/d3d12/TextureD3D12.h index d9fcfd48be..af36c1ad8f 100644 --- a/src/backend/d3d12/TextureD3D12.h +++ b/src/backend/d3d12/TextureD3D12.h @@ -19,43 +19,44 @@ #include "backend/d3d12/d3d12_platform.h" -namespace backend { -namespace d3d12 { +namespace backend { namespace d3d12 { class Device; DXGI_FORMAT D3D12TextureFormat(nxt::TextureFormat format); class Texture : public TextureBase { - public: - Texture(TextureBuilder* builder); - Texture(TextureBuilder* builder, ID3D12Resource* nativeTexture); - ~Texture(); + public: + Texture(TextureBuilder* builder); + Texture(TextureBuilder* builder, ID3D12Resource* nativeTexture); + ~Texture(); - DXGI_FORMAT GetD3D12Format() const; - ID3D12Resource* GetD3D12Resource(); - bool GetResourceTransitionBarrier(nxt::TextureUsageBit currentUsage, nxt::TextureUsageBit targetUsage, D3D12_RESOURCE_BARRIER* barrier); + DXGI_FORMAT GetD3D12Format() const; + ID3D12Resource* GetD3D12Resource(); + bool GetResourceTransitionBarrier(nxt::TextureUsageBit currentUsage, + nxt::TextureUsageBit targetUsage, + D3D12_RESOURCE_BARRIER* barrier); - void TransitionUsageImpl(nxt::TextureUsageBit currentUsage, nxt::TextureUsageBit targetUsage) override; + void TransitionUsageImpl(nxt::TextureUsageBit currentUsage, + nxt::TextureUsageBit targetUsage) override; - private: - Device* mDevice; - ComPtr mResource = {}; - ID3D12Resource* mResourcePtr = nullptr; + private: + Device* mDevice; + ComPtr mResource = {}; + ID3D12Resource* mResourcePtr = nullptr; }; class TextureView : public TextureViewBase { - public: - TextureView(TextureViewBuilder* builder); + public: + TextureView(TextureViewBuilder* builder); - const D3D12_SHADER_RESOURCE_VIEW_DESC& GetSRVDescriptor() const; - D3D12_RENDER_TARGET_VIEW_DESC GetRTVDescriptor(); - D3D12_DEPTH_STENCIL_VIEW_DESC GetDSVDescriptor(); + const D3D12_SHADER_RESOURCE_VIEW_DESC& GetSRVDescriptor() const; + D3D12_RENDER_TARGET_VIEW_DESC GetRTVDescriptor(); + D3D12_DEPTH_STENCIL_VIEW_DESC GetDSVDescriptor(); - private: - D3D12_SHADER_RESOURCE_VIEW_DESC mSrvDesc; + private: + D3D12_SHADER_RESOURCE_VIEW_DESC mSrvDesc; }; -} -} +}} // namespace backend::d3d12 -#endif // BACKEND_D3D12_TEXTURED3D12_H_ +#endif // BACKEND_D3D12_TEXTURED3D12_H_ diff --git a/src/backend/d3d12/d3d12_platform.h b/src/backend/d3d12/d3d12_platform.h index c086373500..b37270d61d 100644 --- a/src/backend/d3d12/d3d12_platform.h +++ b/src/backend/d3d12/d3d12_platform.h @@ -15,10 +15,10 @@ #ifndef BACKEND_D3D12_D3D12PLATFORM_H_ #define BACKEND_D3D12_D3D12PLATFORM_H_ -#include #include #include +#include using Microsoft::WRL::ComPtr; -#endif // BACKEND_D3D12_D3D12PLATFORM_H_ +#endif // BACKEND_D3D12_D3D12PLATFORM_H_