From 86e6a13649eb5d023d2b3c0799904fc12bfa4e64 Mon Sep 17 00:00:00 2001 From: Peng Huang Date: Thu, 6 Apr 2023 19:59:05 +0000 Subject: [PATCH] d3d11: add BindGroup, BindGroupLayout and PipelineLayout Bug: dawn:1705 Change-Id: I9bc7545328409c7ce8fd8adc26a35b872cda1420 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/126302 Reviewed-by: Austin Eng Kokoro: Kokoro Commit-Queue: Peng Huang --- src/dawn/native/BUILD.gn | 6 ++ src/dawn/native/CMakeLists.txt | 6 ++ src/dawn/native/d3d11/BindGroupD3D11.cpp | 40 +++++++++ src/dawn/native/d3d11/BindGroupD3D11.h | 40 +++++++++ .../native/d3d11/BindGroupLayoutD3D11.cpp | 43 ++++++++++ src/dawn/native/d3d11/BindGroupLayoutD3D11.h | 47 +++++++++++ src/dawn/native/d3d11/DeviceD3D11.cpp | 9 +- src/dawn/native/d3d11/PipelineLayoutD3D11.cpp | 84 +++++++++++++++++++ src/dawn/native/d3d11/PipelineLayoutD3D11.h | 61 ++++++++++++++ 9 files changed, 333 insertions(+), 3 deletions(-) create mode 100644 src/dawn/native/d3d11/BindGroupD3D11.cpp create mode 100644 src/dawn/native/d3d11/BindGroupD3D11.h create mode 100644 src/dawn/native/d3d11/BindGroupLayoutD3D11.cpp create mode 100644 src/dawn/native/d3d11/BindGroupLayoutD3D11.h create mode 100644 src/dawn/native/d3d11/PipelineLayoutD3D11.cpp create mode 100644 src/dawn/native/d3d11/PipelineLayoutD3D11.h diff --git a/src/dawn/native/BUILD.gn b/src/dawn/native/BUILD.gn index 92eaa8489e..ea5fd67067 100644 --- a/src/dawn/native/BUILD.gn +++ b/src/dawn/native/BUILD.gn @@ -427,6 +427,10 @@ source_set("sources") { "d3d11/AdapterD3D11.h", "d3d11/BackendD3D11.cpp", "d3d11/BackendD3D11.h", + "d3d11/BindGroupD3D11.cpp", + "d3d11/BindGroupD3D11.h", + "d3d11/BindGroupLayoutD3D11.cpp", + "d3d11/BindGroupLayoutD3D11.h", "d3d11/CommandRecordingContextD3D11.cpp", "d3d11/CommandRecordingContextD3D11.h", "d3d11/D3D11Backend.cpp", @@ -435,6 +439,8 @@ source_set("sources") { "d3d11/DeviceInfoD3D11.cpp", "d3d11/DeviceInfoD3D11.h", "d3d11/Forward.h", + "d3d11/PipelineLayoutD3D11.cpp", + "d3d11/PipelineLayoutD3D11.h", "d3d11/PlatformFunctionsD3D11.cpp", "d3d11/PlatformFunctionsD3D11.h", "d3d11/QueueD3D11.cpp", diff --git a/src/dawn/native/CMakeLists.txt b/src/dawn/native/CMakeLists.txt index 71c22d308e..676f07e5e8 100644 --- a/src/dawn/native/CMakeLists.txt +++ b/src/dawn/native/CMakeLists.txt @@ -284,6 +284,10 @@ if (DAWN_ENABLE_D3D11) "d3d11/AdapterD3D11.h" "d3d11/BackendD3D11.cpp" "d3d11/BackendD3D11.h" + "d3d11/BindGroupD3D11.cpp" + "d3d11/BindGroupD3D11.h" + "d3d11/BindGroupLayoutD3D11.cpp" + "d3d11/BindGroupLayoutD3D11.h" "d3d11/CommandRecordingContextD3D11.cpp" "d3d11/CommandRecordingContextD3D11.h" "d3d11/D3D11Backend.cpp" @@ -292,6 +296,8 @@ if (DAWN_ENABLE_D3D11) "d3d11/DeviceInfoD3D11.cpp" "d3d11/DeviceInfoD3D11.h" "d3d11/Forward.h" + "d3d11/PipelineLayoutD3D11.cpp" + "d3d11/PipelineLayoutD3D11.h" "d3d11/PlatformFunctionsD3D11.cpp" "d3d11/PlatformFunctionsD3D11.h" "d3d11/QueueD3D11.cpp" diff --git a/src/dawn/native/d3d11/BindGroupD3D11.cpp b/src/dawn/native/d3d11/BindGroupD3D11.cpp new file mode 100644 index 0000000000..b3b8640f50 --- /dev/null +++ b/src/dawn/native/d3d11/BindGroupD3D11.cpp @@ -0,0 +1,40 @@ +// Copyright 2023 The Dawn Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "dawn/native/d3d11/BindGroupD3D11.h" + +#include + +#include "dawn/native/Texture.h" +#include "dawn/native/d3d11/BindGroupLayoutD3D11.h" +#include "dawn/native/d3d11/DeviceD3D11.h" + +namespace dawn::native::d3d11 { + +// static +Ref BindGroup::Create(Device* device, const BindGroupDescriptor* descriptor) { + return ToBackend(descriptor->layout)->AllocateBindGroup(device, descriptor); +} + +BindGroup::BindGroup(Device* device, const BindGroupDescriptor* descriptor) + : BindGroupBase(this, device, descriptor) {} + +BindGroup::~BindGroup() = default; + +void BindGroup::DestroyImpl() { + BindGroupBase::DestroyImpl(); + ToBackend(GetLayout())->DeallocateBindGroup(this); +} + +} // namespace dawn::native::d3d11 diff --git a/src/dawn/native/d3d11/BindGroupD3D11.h b/src/dawn/native/d3d11/BindGroupD3D11.h new file mode 100644 index 0000000000..735d39fbe7 --- /dev/null +++ b/src/dawn/native/d3d11/BindGroupD3D11.h @@ -0,0 +1,40 @@ +// Copyright 2023 The Dawn Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef SRC_DAWN_NATIVE_D3D11_BINDGROUPD3D11_H_ +#define SRC_DAWN_NATIVE_D3D11_BINDGROUPD3D11_H_ + +#include "dawn/common/PlacementAllocated.h" +#include "dawn/native/BindGroup.h" + +namespace dawn::native::d3d11 { + +class Device; + +class BindGroup final : public BindGroupBase, public PlacementAllocated { + public: + static Ref Create(Device* device, const BindGroupDescriptor* descriptor); + + private: + friend SlabAllocator; + + BindGroup(Device* device, const BindGroupDescriptor* descriptor); + ~BindGroup() override; + + void DestroyImpl() override; +}; + +} // namespace dawn::native::d3d11 + +#endif // SRC_DAWN_NATIVE_D3D11_BINDGROUPD3D11_H_ diff --git a/src/dawn/native/d3d11/BindGroupLayoutD3D11.cpp b/src/dawn/native/d3d11/BindGroupLayoutD3D11.cpp new file mode 100644 index 0000000000..bd1d8fdc44 --- /dev/null +++ b/src/dawn/native/d3d11/BindGroupLayoutD3D11.cpp @@ -0,0 +1,43 @@ +// Copyright 2023 The Dawn Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "dawn/native/d3d11/BindGroupLayoutD3D11.h" + +#include "dawn/native/d3d11/DeviceD3D11.h" + +namespace dawn::native::d3d11 { + +ResultOrError> BindGroupLayout::Create( + Device* device, + const BindGroupLayoutDescriptor* descriptor, + PipelineCompatibilityToken pipelineCompatibilityToken) { + return AcquireRef(new BindGroupLayout(device, descriptor, pipelineCompatibilityToken)); +} + +BindGroupLayout::BindGroupLayout(Device* device, + const BindGroupLayoutDescriptor* descriptor, + PipelineCompatibilityToken pipelineCompatibilityToken) + : BindGroupLayoutBase(device, descriptor, pipelineCompatibilityToken), + mBindGroupAllocator(MakeFrontendBindGroupAllocator(4096)) {} + +Ref BindGroupLayout::AllocateBindGroup(Device* device, + const BindGroupDescriptor* descriptor) { + return AcquireRef(mBindGroupAllocator.Allocate(device, descriptor)); +} + +void BindGroupLayout::DeallocateBindGroup(BindGroup* bindGroup) { + mBindGroupAllocator.Deallocate(bindGroup); +} + +} // namespace dawn::native::d3d11 diff --git a/src/dawn/native/d3d11/BindGroupLayoutD3D11.h b/src/dawn/native/d3d11/BindGroupLayoutD3D11.h new file mode 100644 index 0000000000..5f61eef4d1 --- /dev/null +++ b/src/dawn/native/d3d11/BindGroupLayoutD3D11.h @@ -0,0 +1,47 @@ +// Copyright 2023 The Dawn Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef SRC_DAWN_NATIVE_D3D11_BINDGROUPLAYOUTD3D11_H_ +#define SRC_DAWN_NATIVE_D3D11_BINDGROUPLAYOUTD3D11_H_ + +#include "dawn/common/SlabAllocator.h" +#include "dawn/native/BindGroupLayout.h" +#include "dawn/native/d3d11/BindGroupD3D11.h" + +namespace dawn::native::d3d11 { + +class Device; + +class BindGroupLayout final : public BindGroupLayoutBase { + public: + static ResultOrError> Create( + Device* device, + const BindGroupLayoutDescriptor* descriptor, + PipelineCompatibilityToken pipelineCompatibilityToken); + + Ref AllocateBindGroup(Device* device, const BindGroupDescriptor* descriptor); + void DeallocateBindGroup(BindGroup* bindGroup); + + private: + BindGroupLayout(Device* device, + const BindGroupLayoutDescriptor* descriptor, + PipelineCompatibilityToken pipelineCompatibilityToken); + ~BindGroupLayout() override = default; + + SlabAllocator mBindGroupAllocator; +}; + +} // namespace dawn::native::d3d11 + +#endif // SRC_DAWN_NATIVE_D3D11_BINDGROUPLAYOUTD3D11_H_ diff --git a/src/dawn/native/d3d11/DeviceD3D11.cpp b/src/dawn/native/d3d11/DeviceD3D11.cpp index 647a9ba160..bd7c65031c 100644 --- a/src/dawn/native/d3d11/DeviceD3D11.cpp +++ b/src/dawn/native/d3d11/DeviceD3D11.cpp @@ -30,6 +30,9 @@ #include "dawn/native/d3d/D3DError.h" #include "dawn/native/d3d11/AdapterD3D11.h" #include "dawn/native/d3d11/BackendD3D11.h" +#include "dawn/native/d3d11/BindGroupD3D11.h" +#include "dawn/native/d3d11/BindGroupLayoutD3D11.h" +#include "dawn/native/d3d11/PipelineLayoutD3D11.h" #include "dawn/native/d3d11/PlatformFunctionsD3D11.h" #include "dawn/native/d3d11/QueueD3D11.h" #include "dawn/native/d3d11/SamplerD3D11.h" @@ -229,13 +232,13 @@ MaybeError Device::ExecutePendingCommandContext() { ResultOrError> Device::CreateBindGroupImpl( const BindGroupDescriptor* descriptor) { - return DAWN_UNIMPLEMENTED_ERROR("CreateBindGroupImpl"); + return BindGroup::Create(this, descriptor); } ResultOrError> Device::CreateBindGroupLayoutImpl( const BindGroupLayoutDescriptor* descriptor, PipelineCompatibilityToken pipelineCompatibilityToken) { - return DAWN_UNIMPLEMENTED_ERROR("CreateBindGroupLayoutImpl"); + return BindGroupLayout::Create(this, descriptor, pipelineCompatibilityToken); } ResultOrError> Device::CreateBufferImpl(const BufferDescriptor* descriptor) { @@ -255,7 +258,7 @@ Ref Device::CreateUninitializedComputePipelineImpl( ResultOrError> Device::CreatePipelineLayoutImpl( const PipelineLayoutDescriptor* descriptor) { - return DAWN_UNIMPLEMENTED_ERROR("CreatePipelineLayoutImpl"); + return PipelineLayout::Create(this, descriptor); } ResultOrError> Device::CreateQuerySetImpl(const QuerySetDescriptor* descriptor) { diff --git a/src/dawn/native/d3d11/PipelineLayoutD3D11.cpp b/src/dawn/native/d3d11/PipelineLayoutD3D11.cpp new file mode 100644 index 0000000000..ac39e50546 --- /dev/null +++ b/src/dawn/native/d3d11/PipelineLayoutD3D11.cpp @@ -0,0 +1,84 @@ +// Copyright 2023 The Dawn Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "dawn/native/d3d11/PipelineLayoutD3D11.h" + +#include "dawn/common/BitSetIterator.h" +#include "dawn/native/BindGroupLayout.h" +#include "dawn/native/d3d11/DeviceD3D11.h" + +namespace dawn::native::d3d11 { + +// static +ResultOrError> PipelineLayout::Create( + Device* device, + const PipelineLayoutDescriptor* descriptor) { + Ref pipelineLayout = AcquireRef(new PipelineLayout(device, descriptor)); + DAWN_TRY(pipelineLayout->Initialize()); + return pipelineLayout; +} + +MaybeError PipelineLayout::Initialize() { + unsigned int constantBufferIndex = 0; + unsigned int samplerIndex = 0; + unsigned int shaderResourceViewIndex = 0; + unsigned int unorderedAccessViewIndex = 0; + unsigned int storageTextureIndex = 0; + + for (BindGroupIndex group : IterateBitSet(GetBindGroupLayoutsMask())) { + const BindGroupLayoutBase* bgl = GetBindGroupLayout(group); + mIndexInfo[group].resize(bgl->GetBindingCount()); + + for (BindingIndex bindingIndex{0}; bindingIndex < bgl->GetBindingCount(); ++bindingIndex) { + const BindingInfo& bindingInfo = bgl->GetBindingInfo(bindingIndex); + switch (bindingInfo.bindingType) { + case BindingInfoType::Buffer: + switch (bindingInfo.buffer.type) { + case wgpu::BufferBindingType::Uniform: + mIndexInfo[group][bindingIndex] = constantBufferIndex++; + break; + case wgpu::BufferBindingType::Storage: + case kInternalStorageBufferBinding: + case wgpu::BufferBindingType::ReadOnlyStorage: + mIndexInfo[group][bindingIndex] = unorderedAccessViewIndex++; + break; + case wgpu::BufferBindingType::Undefined: + UNREACHABLE(); + } + break; + + case BindingInfoType::Sampler: + mIndexInfo[group][bindingIndex] = samplerIndex++; + break; + + case BindingInfoType::Texture: + case BindingInfoType::ExternalTexture: + mIndexInfo[group][bindingIndex] = shaderResourceViewIndex++; + break; + + case BindingInfoType::StorageTexture: + mIndexInfo[group][bindingIndex] = storageTextureIndex++; + break; + } + } + } + + return {}; +} + +const PipelineLayout::BindingIndexInfo& PipelineLayout::GetBindingIndexInfo() const { + return mIndexInfo; +} + +} // namespace dawn::native::d3d11 diff --git a/src/dawn/native/d3d11/PipelineLayoutD3D11.h b/src/dawn/native/d3d11/PipelineLayoutD3D11.h new file mode 100644 index 0000000000..46f6100ac2 --- /dev/null +++ b/src/dawn/native/d3d11/PipelineLayoutD3D11.h @@ -0,0 +1,61 @@ +// Copyright 2023 The Dawn Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef SRC_DAWN_NATIVE_D3D11_PIPELINELAYOUTD3D11_H_ +#define SRC_DAWN_NATIVE_D3D11_PIPELINELAYOUTD3D11_H_ + +#include "dawn/native/PipelineLayout.h" + +#include "dawn/common/ityp_array.h" +#include "dawn/common/ityp_vector.h" +#include "dawn/native/BindingInfo.h" +#include "dawn/native/d3d/d3d_platform.h" + +namespace dawn::native::d3d11 { + +class Device; + +// For D3D11, uniform buffers, samplers, sampled textures, and storage buffers are bind to +// different kind of slots. The number of slots for each type is limited by the D3D11 spec. +// So we need to pack the bindings by type into the slots tightly. +// And D3D11 uses SM 5.0 which doesn't support spaces(binding groups). so we need to flatten +// the binding groups into a single array. +class PipelineLayout final : public PipelineLayoutBase { + public: + // constant buffer slot reserved for index offsets and num workgroups. + static constexpr uint32_t kReservedConstantBufferSlot = + D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - 1; + static constexpr uint32_t kFirstIndexOffsetConstantBufferSlot = kReservedConstantBufferSlot; + static constexpr uint32_t kNumWorkgroupsConstantBufferSlot = kReservedConstantBufferSlot; + + static ResultOrError> Create(Device* device, + const PipelineLayoutDescriptor* descriptor); + + using BindingIndexInfo = + ityp::array, kMaxBindGroups>; + const BindingIndexInfo& GetBindingIndexInfo() const; + + private: + using PipelineLayoutBase::PipelineLayoutBase; + + ~PipelineLayout() override = default; + + MaybeError Initialize(); + + BindingIndexInfo mIndexInfo; +}; + +} // namespace dawn::native::d3d11 + +#endif // SRC_DAWN_NATIVE_D3D11_PIPELINELAYOUTD3D11_H_