• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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_SHADERVISIBLEDESCRIPTORALLOCATOR_H_
16 #define DAWNNATIVE_D3D12_SHADERVISIBLEDESCRIPTORALLOCATOR_H_
17 
18 #include "dawn_native/Error.h"
19 #include "dawn_native/RingBufferAllocator.h"
20 #include "dawn_native/d3d12/IntegerTypes.h"
21 #include "dawn_native/d3d12/PageableD3D12.h"
22 #include "dawn_native/d3d12/d3d12_platform.h"
23 
24 #include <list>
25 
26 // |ShaderVisibleDescriptorAllocator| allocates a variable-sized block of descriptors from a GPU
27 // descriptor heap pool.
28 // Internally, it manages a list of heaps using a ringbuffer block allocator. The heap is in one
29 // of two states: switched in or out. Only a switched in heap can be bound to the pipeline. If
30 // the heap is full, the caller must switch-in a new heap before re-allocating and the old one
31 // is returned to the pool.
32 namespace dawn_native { namespace d3d12 {
33 
34     class Device;
35     class GPUDescriptorHeapAllocation;
36 
37     class ShaderVisibleDescriptorHeap : public Pageable {
38       public:
39         ShaderVisibleDescriptorHeap(ComPtr<ID3D12DescriptorHeap> d3d12DescriptorHeap,
40                                     uint64_t size);
41         ID3D12DescriptorHeap* GetD3D12DescriptorHeap() const;
42 
43       private:
44         ComPtr<ID3D12DescriptorHeap> mD3d12DescriptorHeap;
45     };
46 
47     class ShaderVisibleDescriptorAllocator {
48       public:
49         static ResultOrError<std::unique_ptr<ShaderVisibleDescriptorAllocator>> Create(
50             Device* device,
51             D3D12_DESCRIPTOR_HEAP_TYPE heapType);
52 
53         ShaderVisibleDescriptorAllocator(Device* device, D3D12_DESCRIPTOR_HEAP_TYPE heapType);
54 
55         // Returns true if the allocation was successful, when false is returned the current heap is
56         // full and AllocateAndSwitchShaderVisibleHeap() must be called.
57         bool AllocateGPUDescriptors(uint32_t descriptorCount,
58                                     ExecutionSerial pendingSerial,
59                                     D3D12_CPU_DESCRIPTOR_HANDLE* baseCPUDescriptor,
60                                     GPUDescriptorHeapAllocation* allocation);
61 
62         void Tick(ExecutionSerial completedSerial);
63 
64         ID3D12DescriptorHeap* GetShaderVisibleHeap() const;
65         MaybeError AllocateAndSwitchShaderVisibleHeap();
66 
67         // For testing purposes only.
68         HeapVersionID GetShaderVisibleHeapSerialForTesting() const;
69         uint64_t GetShaderVisibleHeapSizeForTesting() const;
70         uint64_t GetShaderVisiblePoolSizeForTesting() const;
71         bool IsShaderVisibleHeapLockedResidentForTesting() const;
72         bool IsLastShaderVisibleHeapInLRUForTesting() const;
73 
74         bool IsAllocationStillValid(const GPUDescriptorHeapAllocation& allocation) const;
75 
76       private:
77         struct SerialDescriptorHeap {
78             ExecutionSerial heapSerial;
79             std::unique_ptr<ShaderVisibleDescriptorHeap> heap;
80         };
81 
82         ResultOrError<std::unique_ptr<ShaderVisibleDescriptorHeap>> AllocateHeap(
83             uint32_t descriptorCount) const;
84 
85         std::unique_ptr<ShaderVisibleDescriptorHeap> mHeap;
86         RingBufferAllocator mAllocator;
87         std::list<SerialDescriptorHeap> mPool;
88         D3D12_DESCRIPTOR_HEAP_TYPE mHeapType;
89 
90         Device* mDevice;
91 
92         // The serial value of 0 means the shader-visible heaps have not been allocated.
93         // This value is never returned in the GPUDescriptorHeapAllocation after
94         // AllocateGPUDescriptors() is called.
95         HeapVersionID mHeapSerial = HeapVersionID(0);
96 
97         uint32_t mSizeIncrement;
98 
99         // The descriptor count is the current size of the heap in number of descriptors.
100         // This is stored on the allocator to avoid extra conversions.
101         uint32_t mDescriptorCount = 0;
102     };
103 }}  // namespace dawn_native::d3d12
104 
105 #endif  // DAWNNATIVE_D3D12_SHADERVISIBLEDESCRIPTORALLOCATOR_H_
106