Flutter Engine
The Flutter Engine
GrD3DCpuDescriptorManager.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2020 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
9
11
13 : fRTVDescriptorPool(gpu, D3D12_DESCRIPTOR_HEAP_TYPE_RTV)
14 , fDSVDescriptorPool(gpu, D3D12_DESCRIPTOR_HEAP_TYPE_DSV)
15 , fShaderViewDescriptorPool(gpu, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV)
16 , fSamplerDescriptorPool(gpu, D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER) {}
17
19 GrD3DGpu* gpu, ID3D12Resource* textureResource) {
20 const GrD3DDescriptorHeap::CPUHandle& descriptor = fRTVDescriptorPool.allocateHandle(gpu);
21 gpu->device()->CreateRenderTargetView(textureResource, nullptr, descriptor.fHandle);
22 return descriptor;
23}
24
26 const GrD3DDescriptorHeap::CPUHandle& rtvDescriptor) {
27 fRTVDescriptorPool.releaseHandle(rtvDescriptor);
28}
29
31 GrD3DGpu* gpu, ID3D12Resource* textureResource) {
32 const GrD3DDescriptorHeap::CPUHandle& descriptor = fDSVDescriptorPool.allocateHandle(gpu);
33 gpu->device()->CreateDepthStencilView(textureResource, nullptr, descriptor.fHandle);
34 return descriptor;
35}
36
38 const GrD3DDescriptorHeap::CPUHandle& dsvDescriptor) {
39 fDSVDescriptorPool.releaseHandle(dsvDescriptor);
40}
41
43 GrD3DGpu* gpu, ID3D12Resource* bufferResource, size_t offset, size_t size) {
44 const GrD3DDescriptorHeap::CPUHandle& descriptor =
45 fShaderViewDescriptorPool.allocateHandle(gpu);
46 D3D12_CONSTANT_BUFFER_VIEW_DESC desc = {};
47 desc.BufferLocation = bufferResource->GetGPUVirtualAddress() + offset;
48 desc.SizeInBytes = size;
49 gpu->device()->CreateConstantBufferView(&desc, descriptor.fHandle);
50 return descriptor;
51}
52
54 GrD3DGpu* gpu, ID3D12Resource* resource,
55 unsigned int mostDetailedMip, unsigned int mipLevels) {
56 const GrD3DDescriptorHeap::CPUHandle& descriptor =
57 fShaderViewDescriptorPool.allocateHandle(gpu);
58 // TODO: for 4:2:0 YUV formats we'll need to map two different views, one for Y and one for UV.
59 // For now map the entire resource.
60 D3D12_SHADER_RESOURCE_VIEW_DESC desc = {};
61 desc.Format = resource->GetDesc().Format;
62 desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
63 desc.Texture2D.MostDetailedMip = mostDetailedMip;
64 desc.Texture2D.MipLevels = mipLevels;
65 desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
66 gpu->device()->CreateShaderResourceView(resource, &desc, descriptor.fHandle);
67 return descriptor;
68}
69
71 GrD3DGpu* gpu, ID3D12Resource* resource, unsigned int mipSlice) {
72 const GrD3DDescriptorHeap::CPUHandle& descriptor =
73 fShaderViewDescriptorPool.allocateHandle(gpu);
74 if (resource->GetDesc().Dimension == D3D12_RESOURCE_DIMENSION_BUFFER) {
75 // TODO: figure out buffer setup
76 gpu->device()->CreateUnorderedAccessView(resource, nullptr, nullptr, descriptor.fHandle);
77 } else {
78 D3D12_UNORDERED_ACCESS_VIEW_DESC desc = {};
79 desc.Format = resource->GetDesc().Format;
80 desc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;
81 desc.Texture2D.MipSlice = mipSlice;
82 gpu->device()->CreateUnorderedAccessView(resource, nullptr, &desc, descriptor.fHandle);
83 }
84 return descriptor;
85}
86
89 fShaderViewDescriptorPool.releaseHandle(view);
90}
91
93 GrD3DGpu* gpu,
94 D3D12_FILTER filter,
95 float maxLOD,
96 unsigned int maxAnisotropy,
97 D3D12_TEXTURE_ADDRESS_MODE addressModeU,
98 D3D12_TEXTURE_ADDRESS_MODE addressModeV) {
99 const GrD3DDescriptorHeap::CPUHandle& descriptor = fSamplerDescriptorPool.allocateHandle(gpu);
100 D3D12_SAMPLER_DESC desc = {};
101 desc.Filter = filter;
102 desc.AddressU = addressModeU;
103 desc.AddressV = addressModeV;
104 desc.AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
105 desc.MipLODBias = 0;
106 desc.MaxAnisotropy = maxAnisotropy;
107 desc.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS;
108 // desc.BorderColor initialized to { 0, 0, 0, 0 } by default initializer, above.
109 desc.MinLOD = 0;
110 desc.MaxLOD = maxLOD;
111
112 gpu->device()->CreateSampler(&desc, descriptor.fHandle);
113 return descriptor;
114}
115
117 const GrD3DDescriptorHeap::CPUHandle& samplerDescriptor) {
118 fSamplerDescriptorPool.releaseHandle(samplerDescriptor);
119}
120
121////////////////////////////////////////////////////////////////////////////////////////////////
122
123std::unique_ptr<GrD3DCpuDescriptorManager::Heap> GrD3DCpuDescriptorManager::Heap::Make(
124 GrD3DGpu* gpu, D3D12_DESCRIPTOR_HEAP_TYPE type, unsigned int numDescriptors) {
125 std::unique_ptr<GrD3DDescriptorHeap> heap =
126 GrD3DDescriptorHeap::Make(gpu, type, numDescriptors, D3D12_DESCRIPTOR_HEAP_FLAG_NONE);
127 if (!heap) {
128 return nullptr;
129 }
130
131 return std::unique_ptr<Heap>(new Heap(heap, numDescriptors));
132}
133
134GrD3DDescriptorHeap::CPUHandle GrD3DCpuDescriptorManager::Heap::allocateCPUHandle() {
135 SkBitSet::OptionalIndex freeBlock = fFreeBlocks.findFirst();
136 SkASSERT(freeBlock.has_value());
137 fFreeBlocks.reset(*freeBlock);
138 --fFreeCount;
139 return fHeap->getCPUHandle(*freeBlock);
140}
141
142void GrD3DCpuDescriptorManager::Heap::freeCPUHandle(const GrD3DDescriptorHeap::CPUHandle& handle) {
143 SkASSERT(this->ownsHandle(handle));
144 size_t index = fHeap->getIndex(handle);
145 fFreeBlocks.set(index);
146 ++fFreeCount;
147}
148
149////////////////////////////////////////////////////////////////////////////////////////////////
150
151GrD3DCpuDescriptorManager::HeapPool::HeapPool(GrD3DGpu* gpu, D3D12_DESCRIPTOR_HEAP_TYPE heapType)
152 : fMaxAvailableDescriptors(32)
153 , fHeapType(heapType) {
154 std::unique_ptr<GrD3DCpuDescriptorManager::Heap> heap =
155 GrD3DCpuDescriptorManager::Heap::Make(gpu, fHeapType, fMaxAvailableDescriptors);
156 fDescriptorHeaps.push_back(std::move(heap));
157}
158
159GrD3DDescriptorHeap::CPUHandle GrD3DCpuDescriptorManager::HeapPool::allocateHandle(
160 GrD3DGpu* gpu) {
161 for (unsigned int i = 0; i < fDescriptorHeaps.size(); ++i) {
162 if (fDescriptorHeaps[i]->canAllocate()) {
163 GrD3DDescriptorHeap::CPUHandle handle = fDescriptorHeaps[i]->allocateCPUHandle();
164 return handle;
165 }
166 }
167
168 // need to allocate more space
169 std::unique_ptr<GrD3DCpuDescriptorManager::Heap> heap =
170 GrD3DCpuDescriptorManager::Heap::Make(gpu, fHeapType, fMaxAvailableDescriptors);
171 // TODO: handle failed heap creation and/or memory restrictions better
172 // skbug.com/11959
173 SkASSERT(heap);
174
175 fDescriptorHeaps.push_back(std::move(heap));
176 fMaxAvailableDescriptors *= 2;
178 fDescriptorHeaps[fDescriptorHeaps.size() - 1]->allocateCPUHandle();
179 return handle;
180}
181
182void GrD3DCpuDescriptorManager::HeapPool::releaseHandle(
183 const GrD3DDescriptorHeap::CPUHandle& dsvDescriptor) {
184 for (unsigned int i = 0; i < fDescriptorHeaps.size(); ++i) {
185 if (fDescriptorHeaps[i]->ownsHandle(dsvDescriptor)) {
186 fDescriptorHeaps[i]->freeCPUHandle(dsvDescriptor);
187 return;
188 }
189 }
190 SkASSERT(false);
191}
#define SkASSERT(cond)
Definition: SkAssert.h:116
static SkString resource(SkPDFResourceType type, int index)
GLenum type
GrD3DDescriptorHeap::CPUHandle createUnorderedAccessView(GrD3DGpu *, ID3D12Resource *resource, unsigned int mipSlice)
void recycleSampler(const GrD3DDescriptorHeap::CPUHandle &)
void recycleRenderTargetView(const GrD3DDescriptorHeap::CPUHandle &)
GrD3DDescriptorHeap::CPUHandle createShaderResourceView(GrD3DGpu *, ID3D12Resource *resource, unsigned int mostDetailedMip, unsigned int mipLevels)
GrD3DDescriptorHeap::CPUHandle createDepthStencilView(GrD3DGpu *, ID3D12Resource *textureResource)
GrD3DDescriptorHeap::CPUHandle createConstantBufferView(GrD3DGpu *, ID3D12Resource *bufferResource, size_t offset, size_t size)
GrD3DDescriptorHeap::CPUHandle createSampler(GrD3DGpu *, D3D12_FILTER filter, float maxLOD, unsigned int maxAnisotropy, D3D12_TEXTURE_ADDRESS_MODE addressModeU, D3D12_TEXTURE_ADDRESS_MODE addressModeV)
void recycleDepthStencilView(const GrD3DDescriptorHeap::CPUHandle &)
void recycleShaderView(const GrD3DDescriptorHeap::CPUHandle &)
GrD3DDescriptorHeap::CPUHandle createRenderTargetView(GrD3DGpu *, ID3D12Resource *textureResource)
static std::unique_ptr< GrD3DDescriptorHeap > Make(GrD3DGpu *gpu, D3D12_DESCRIPTOR_HEAP_TYPE, unsigned int numDescriptors, D3D12_DESCRIPTOR_HEAP_FLAGS)
ID3D12Device * device() const
Definition: GrD3DGpu.h:43
std::optional< size_t > OptionalIndex
Definition: SkBitSet.h:92
SK_API sk_sp< SkDocument > Make(SkWStream *dst, const SkSerialProcs *=nullptr, std::function< void(const SkPicture *)> onEndPage=nullptr)
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
SeparatedVector2 offset
D3D12_CPU_DESCRIPTOR_HANDLE fHandle