• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef DAWNNATIVE_D3D12_BINDGROUPLAYOUTD3D12_H_
16 #define DAWNNATIVE_D3D12_BINDGROUPLAYOUTD3D12_H_
17 
18 #include "dawn_native/BindGroupLayout.h"
19 
20 #include "common/SlabAllocator.h"
21 #include "common/ityp_stack_vec.h"
22 #include "dawn_native/d3d12/d3d12_platform.h"
23 
24 namespace dawn_native { namespace d3d12 {
25 
26     class BindGroup;
27     class CPUDescriptorHeapAllocation;
28     class Device;
29     class StagingDescriptorAllocator;
30 
31     // A purposefully invalid register space.
32     //
33     // We use the bind group index as the register space, but don't know the bind group index until
34     // pipeline layout creation time. This value should be replaced in PipelineLayoutD3D12.
35     static constexpr uint32_t kRegisterSpacePlaceholder =
36         D3D12_DRIVER_RESERVED_REGISTER_SPACE_VALUES_START;
37 
38     class BindGroupLayout final : public BindGroupLayoutBase {
39       public:
40         static Ref<BindGroupLayout> Create(Device* device,
41                                            const BindGroupLayoutDescriptor* descriptor,
42                                            PipelineCompatibilityToken pipelineCompatibilityToken);
43 
44         ResultOrError<Ref<BindGroup>> AllocateBindGroup(Device* device,
45                                                         const BindGroupDescriptor* descriptor);
46         void DeallocateBindGroup(BindGroup* bindGroup, CPUDescriptorHeapAllocation* viewAllocation);
47 
48         // The offset (in descriptor count) into the corresponding descriptor heap. Not valid for
49         // dynamic binding indexes.
50         ityp::span<BindingIndex, const uint32_t> GetDescriptorHeapOffsets() const;
51 
52         // The D3D shader register that the Dawn binding index is mapped to by this bind group
53         // layout.
54         uint32_t GetShaderRegister(BindingIndex bindingIndex) const;
55 
56         // Counts of descriptors in the descriptor tables.
57         uint32_t GetCbvUavSrvDescriptorCount() const;
58         uint32_t GetSamplerDescriptorCount() const;
59 
60         const std::vector<D3D12_DESCRIPTOR_RANGE>& GetCbvUavSrvDescriptorRanges() const;
61         const std::vector<D3D12_DESCRIPTOR_RANGE>& GetSamplerDescriptorRanges() const;
62 
63       private:
64         BindGroupLayout(Device* device,
65                         const BindGroupLayoutDescriptor* descriptor,
66                         PipelineCompatibilityToken pipelineCompatibilityToken);
67         ~BindGroupLayout() override = default;
68 
69         // Contains the offset into the descriptor heap for the given resource view. Samplers and
70         // non-samplers are stored in separate descriptor heaps, so the offsets should be unique
71         // within each group and tightly packed.
72         //
73         // Dynamic resources are not used here since their descriptors are placed directly in root
74         // parameters.
75         ityp::stack_vec<BindingIndex, uint32_t, kMaxOptimalBindingsPerGroup> mDescriptorHeapOffsets;
76 
77         // Contains the shader register this binding is mapped to.
78         ityp::stack_vec<BindingIndex, uint32_t, kMaxOptimalBindingsPerGroup> mShaderRegisters;
79 
80         uint32_t mCbvUavSrvDescriptorCount;
81         uint32_t mSamplerDescriptorCount;
82 
83         std::vector<D3D12_DESCRIPTOR_RANGE> mCbvUavSrvDescriptorRanges;
84         std::vector<D3D12_DESCRIPTOR_RANGE> mSamplerDescriptorRanges;
85 
86         SlabAllocator<BindGroup> mBindGroupAllocator;
87 
88         StagingDescriptorAllocator* mSamplerAllocator = nullptr;
89         StagingDescriptorAllocator* mViewAllocator = nullptr;
90     };
91 
92 }}  // namespace dawn_native::d3d12
93 
94 #endif  // DAWNNATIVE_D3D12_BINDGROUPLAYOUTD3D12_H_
95