OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrVkResourceProvider.h" | 8 #include "GrVkResourceProvider.h" |
9 | 9 |
10 #include "GrTextureParams.h" | 10 #include "GrTextureParams.h" |
11 #include "GrVkCommandBuffer.h" | 11 #include "GrVkCommandBuffer.h" |
12 #include "GrVkPipeline.h" | 12 #include "GrVkPipeline.h" |
13 #include "GrVkRenderTarget.h" | 13 #include "GrVkRenderTarget.h" |
14 #include "GrVkSampler.h" | 14 #include "GrVkSampler.h" |
15 #include "GrVkUtil.h" | 15 #include "GrVkUtil.h" |
16 | 16 |
17 #ifdef SK_TRACE_VK_RESOURCES | 17 #ifdef SK_TRACE_VK_RESOURCES |
18 GrVkResource::Trace GrVkResource::fTrace; | 18 GrVkResource::Trace GrVkResource::fTrace; |
19 uint32_t GrVkResource::fKeyCounter = 0; | 19 uint32_t GrVkResource::fKeyCounter = 0; |
20 #endif | 20 #endif |
21 | 21 |
22 GrVkResourceProvider::GrVkResourceProvider(GrVkGpu* gpu) | 22 GrVkResourceProvider::GrVkResourceProvider(GrVkGpu* gpu) |
23 : fGpu(gpu) | 23 : fGpu(gpu) |
24 , fPipelineCache(VK_NULL_HANDLE) | 24 , fPipelineCache(VK_NULL_HANDLE) |
25 , fUniformDescPool(nullptr) | |
26 , fCurrentUniformDescCount(0) { | 25 , fCurrentUniformDescCount(0) { |
27 fPipelineStateCache = new PipelineStateCache(gpu); | 26 fPipelineStateCache = new PipelineStateCache(gpu); |
28 } | 27 } |
29 | 28 |
30 GrVkResourceProvider::~GrVkResourceProvider() { | 29 GrVkResourceProvider::~GrVkResourceProvider() { |
31 SkASSERT(0 == fRenderPassArray.count()); | 30 SkASSERT(0 == fRenderPassArray.count()); |
32 SkASSERT(VK_NULL_HANDLE == fPipelineCache); | 31 SkASSERT(VK_NULL_HANDLE == fPipelineCache); |
33 delete fPipelineStateCache; | 32 delete fPipelineStateCache; |
34 } | 33 } |
35 | 34 |
(...skipping 18 matching lines...) Expand all Loading... |
54 dsUniformLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CR
EATE_INFO; | 53 dsUniformLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CR
EATE_INFO; |
55 dsUniformLayoutCreateInfo.pNext = nullptr; | 54 dsUniformLayoutCreateInfo.pNext = nullptr; |
56 dsUniformLayoutCreateInfo.flags = 0; | 55 dsUniformLayoutCreateInfo.flags = 0; |
57 dsUniformLayoutCreateInfo.bindingCount = 2; | 56 dsUniformLayoutCreateInfo.bindingCount = 2; |
58 dsUniformLayoutCreateInfo.pBindings = dsUniBindings; | 57 dsUniformLayoutCreateInfo.pBindings = dsUniBindings; |
59 | 58 |
60 GR_VK_CALL_ERRCHECK(fGpu->vkInterface(), CreateDescriptorSetLayout(fGpu->dev
ice(), | 59 GR_VK_CALL_ERRCHECK(fGpu->vkInterface(), CreateDescriptorSetLayout(fGpu->dev
ice(), |
61 &dsUnifor
mLayoutCreateInfo, | 60 &dsUnifor
mLayoutCreateInfo, |
62 nullptr, | 61 nullptr, |
63 &fUniform
DescLayout)); | 62 &fUniform
DescLayout)); |
64 fCurrMaxUniDescriptors = kStartNumUniformDescriptors; | 63 |
65 fUniformDescPool = this->findOrCreateCompatibleDescriptorPool(VK_DESCRIPTOR_
TYPE_UNIFORM_BUFFER, | 64 this->getDescSetHandle(0, fUniformDescLayout, &fUniformDSHandle); |
66 fCurrMaxUniDes
criptors); | |
67 } | 65 } |
68 | 66 |
69 void GrVkResourceProvider::init() { | 67 void GrVkResourceProvider::init() { |
70 VkPipelineCacheCreateInfo createInfo; | 68 VkPipelineCacheCreateInfo createInfo; |
71 memset(&createInfo, 0, sizeof(VkPipelineCacheCreateInfo)); | 69 memset(&createInfo, 0, sizeof(VkPipelineCacheCreateInfo)); |
72 createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; | 70 createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; |
73 createInfo.pNext = nullptr; | 71 createInfo.pNext = nullptr; |
74 createInfo.flags = 0; | 72 createInfo.flags = 0; |
75 createInfo.initialDataSize = 0; | 73 createInfo.initialDataSize = 0; |
76 createInfo.pInitialData = nullptr; | 74 createInfo.pInitialData = nullptr; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 } | 182 } |
185 | 183 |
186 sk_sp<GrVkPipelineState> GrVkResourceProvider::findOrCreateCompatiblePipelineSta
te( | 184 sk_sp<GrVkPipelineState> GrVkResourceProvider::findOrCreateCompatiblePipelineSta
te( |
187 const GrPipelin
e& pipeline, | 185 const GrPipelin
e& pipeline, |
188 const GrPrimiti
veProcessor& proc, | 186 const GrPrimiti
veProcessor& proc, |
189 GrPrimitiveType
primitiveType, | 187 GrPrimitiveType
primitiveType, |
190 const GrVkRende
rPass& renderPass) { | 188 const GrVkRende
rPass& renderPass) { |
191 return fPipelineStateCache->refPipelineState(pipeline, proc, primitiveType,
renderPass); | 189 return fPipelineStateCache->refPipelineState(pipeline, proc, primitiveType,
renderPass); |
192 } | 190 } |
193 | 191 |
194 void GrVkResourceProvider::getUniformDescriptorSet(VkDescriptorSet* ds, | 192 |
195 const GrVkDescriptorPool** ou
tPool) { | 193 void GrVkResourceProvider::getDescSetHandle(uint32_t numSamplers, VkDescriptorSe
tLayout layout, |
196 fCurrentUniformDescCount += kNumUniformDescPerSet; | 194 GrVkDescriptorSetManager::Handle* ha
ndle) { |
197 if (fCurrentUniformDescCount > fCurrMaxUniDescriptors) { | 195 SkASSERT(handle); |
198 fUniformDescPool->unref(fGpu); | 196 for (int i = 0; i < fDescriptorSetManagers.count(); ++i) { |
199 uint32_t newPoolSize = fCurrMaxUniDescriptors + ((fCurrMaxUniDescriptors
+ 1) >> 1); | 197 if (fDescriptorSetManagers[i].isCompatible(numSamplers)) { |
200 if (newPoolSize < kMaxUniformDescriptors) { | 198 *handle = GrVkDescriptorSetManager::Handle(i); |
201 fCurrMaxUniDescriptors = newPoolSize; | 199 return; |
202 } else { | |
203 fCurrMaxUniDescriptors = kMaxUniformDescriptors; | |
204 } | 200 } |
205 fUniformDescPool = | |
206 this->findOrCreateCompatibleDescriptorPool(VK_DESCRIPTOR_TYPE_UNIFOR
M_BUFFER, | |
207 fCurrMaxUniDescriptors); | |
208 fCurrentUniformDescCount = kNumUniformDescPerSet; | |
209 } | 201 } |
210 SkASSERT(fUniformDescPool); | |
211 | 202 |
212 VkDescriptorSetAllocateInfo dsAllocateInfo; | 203 // Failed to find a DescSetManager, we must create a new one; |
213 memset(&dsAllocateInfo, 0, sizeof(VkDescriptorSetAllocateInfo)); | 204 VkDescriptorType type = numSamplers ? VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMP
LER |
214 dsAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; | 205 : VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
215 dsAllocateInfo.pNext = nullptr; | 206 |
216 dsAllocateInfo.descriptorPool = fUniformDescPool->descPool(); | 207 fDescriptorSetManagers.emplace_back(fGpu, layout, type, numSamplers); |
217 dsAllocateInfo.descriptorSetCount = 1; | 208 *handle = GrVkDescriptorSetManager::Handle(fDescriptorSetManagers.count() -
1); |
218 dsAllocateInfo.pSetLayouts = &fUniformDescLayout; | 209 } |
219 GR_VK_CALL_ERRCHECK(fGpu->vkInterface(), AllocateDescriptorSets(fGpu->device
(), | 210 |
220 &dsAllocateI
nfo, | 211 const GrVkDescriptorSet* GrVkResourceProvider::getUniformDescriptorSet() { |
221 ds)); | 212 SkASSERT(fUniformDSHandle.isValid()); |
222 *outPool = fUniformDescPool; | 213 return fDescriptorSetManagers[fUniformDSHandle.toIndex()].getDescriptorSet(f
Gpu, |
| 214 f
UniformDSHandle); |
| 215 } |
| 216 |
| 217 |
| 218 void GrVkResourceProvider::recycleDescriptorSet(const GrVkDescriptorSet* descSet
, |
| 219 const GrVkDescriptorSetManager::
Handle& handle) { |
| 220 SkASSERT(descSet); |
| 221 SkASSERT(handle.isValid()); |
| 222 int managerIdx = handle.toIndex(); |
| 223 SkASSERT(managerIdx < fDescriptorSetManagers.count()); |
| 224 fDescriptorSetManagers[managerIdx].recycleDescriptorSet(descSet); |
223 } | 225 } |
224 | 226 |
225 GrVkPrimaryCommandBuffer* GrVkResourceProvider::findOrCreatePrimaryCommandBuffer
() { | 227 GrVkPrimaryCommandBuffer* GrVkResourceProvider::findOrCreatePrimaryCommandBuffer
() { |
226 GrVkPrimaryCommandBuffer* cmdBuffer = nullptr; | 228 GrVkPrimaryCommandBuffer* cmdBuffer = nullptr; |
227 int count = fAvailableCommandBuffers.count(); | 229 int count = fAvailableCommandBuffers.count(); |
228 if (count > 0) { | 230 if (count > 0) { |
229 cmdBuffer = fAvailableCommandBuffers[count -1]; | 231 cmdBuffer = fAvailableCommandBuffers[count - 1]; |
230 SkASSERT(cmdBuffer->finished(fGpu)); | 232 SkASSERT(cmdBuffer->finished(fGpu)); |
231 fAvailableCommandBuffers.removeShuffle(count - 1); | 233 fAvailableCommandBuffers.removeShuffle(count - 1); |
232 } else { | 234 } else { |
233 cmdBuffer = GrVkPrimaryCommandBuffer::Create(fGpu, fGpu->cmdPool()); | 235 cmdBuffer = GrVkPrimaryCommandBuffer::Create(fGpu, fGpu->cmdPool()); |
234 } | 236 } |
235 fActiveCommandBuffers.push_back(cmdBuffer); | 237 fActiveCommandBuffers.push_back(cmdBuffer); |
236 cmdBuffer->ref(); | 238 cmdBuffer->ref(); |
237 return cmdBuffer; | 239 return cmdBuffer; |
238 } | 240 } |
239 | 241 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 | 308 |
307 GR_VK_CALL(fGpu->vkInterface(), DestroyPipelineCache(fGpu->device(), fPipeli
neCache, nullptr)); | 309 GR_VK_CALL(fGpu->vkInterface(), DestroyPipelineCache(fGpu->device(), fPipeli
neCache, nullptr)); |
308 fPipelineCache = VK_NULL_HANDLE; | 310 fPipelineCache = VK_NULL_HANDLE; |
309 | 311 |
310 if (fUniformDescLayout) { | 312 if (fUniformDescLayout) { |
311 GR_VK_CALL(fGpu->vkInterface(), DestroyDescriptorSetLayout(fGpu->device(
), | 313 GR_VK_CALL(fGpu->vkInterface(), DestroyDescriptorSetLayout(fGpu->device(
), |
312 fUniformDescL
ayout, | 314 fUniformDescL
ayout, |
313 nullptr)); | 315 nullptr)); |
314 fUniformDescLayout = VK_NULL_HANDLE; | 316 fUniformDescLayout = VK_NULL_HANDLE; |
315 } | 317 } |
316 fUniformDescPool->unref(fGpu); | 318 |
| 319 // We must release/destroy all command buffers and pipeline states before re
leasing the |
| 320 // GrVkDescriptorSetManagers |
| 321 for (int i = 0; i < fDescriptorSetManagers.count(); ++i) { |
| 322 fDescriptorSetManagers[i].release(fGpu); |
| 323 } |
| 324 fDescriptorSetManagers.reset(); |
317 } | 325 } |
318 | 326 |
319 void GrVkResourceProvider::abandonResources() { | 327 void GrVkResourceProvider::abandonResources() { |
320 // release our active command buffers | 328 // release our active command buffers |
321 for (int i = 0; i < fActiveCommandBuffers.count(); ++i) { | 329 for (int i = 0; i < fActiveCommandBuffers.count(); ++i) { |
322 SkASSERT(fActiveCommandBuffers[i]->finished(fGpu)); | 330 SkASSERT(fActiveCommandBuffers[i]->finished(fGpu)); |
323 SkASSERT(fActiveCommandBuffers[i]->unique()); | 331 SkASSERT(fActiveCommandBuffers[i]->unique()); |
324 fActiveCommandBuffers[i]->unrefAndAbandon(); | 332 fActiveCommandBuffers[i]->unrefAndAbandon(); |
325 } | 333 } |
326 fActiveCommandBuffers.reset(); | 334 fActiveCommandBuffers.reset(); |
(...skipping 22 matching lines...) Expand all Loading... |
349 SkTDynamicHash<GrVkSampler, uint16_t>::Iter iter(&fSamplers); | 357 SkTDynamicHash<GrVkSampler, uint16_t>::Iter iter(&fSamplers); |
350 for (; !iter.done(); ++iter) { | 358 for (; !iter.done(); ++iter) { |
351 (*iter).unrefAndAbandon(); | 359 (*iter).unrefAndAbandon(); |
352 } | 360 } |
353 fSamplers.reset(); | 361 fSamplers.reset(); |
354 | 362 |
355 fPipelineStateCache->abandon(); | 363 fPipelineStateCache->abandon(); |
356 | 364 |
357 fPipelineCache = VK_NULL_HANDLE; | 365 fPipelineCache = VK_NULL_HANDLE; |
358 | 366 |
359 fUniformDescLayout = VK_NULL_HANDLE; | 367 // We must abandon all command buffers and pipeline states before abandoning
the |
360 fUniformDescPool->unrefAndAbandon(); | 368 // GrVkDescriptorSetManagers |
| 369 for (int i = 0; i < fDescriptorSetManagers.count(); ++i) { |
| 370 fDescriptorSetManagers[i].abandon(); |
| 371 } |
| 372 fDescriptorSetManagers.reset(); |
| 373 |
361 } | 374 } |
362 | 375 |
363 //////////////////////////////////////////////////////////////////////////////// | 376 //////////////////////////////////////////////////////////////////////////////// |
364 | 377 |
365 GrVkResourceProvider::CompatibleRenderPassSet::CompatibleRenderPassSet( | 378 GrVkResourceProvider::CompatibleRenderPassSet::CompatibleRenderPassSet( |
366 const GrVkG
pu* gpu, | 379 const GrVkG
pu* gpu, |
367 const GrVkR
enderTarget& target) | 380 const GrVkR
enderTarget& target) |
368 : fLastReturnedIndex(0) { | 381 : fLastReturnedIndex(0) { |
369 fRenderPasses.emplace_back(new GrVkRenderPass()); | 382 fRenderPasses.emplace_back(new GrVkRenderPass()); |
370 fRenderPasses[0]->initSimple(gpu, target); | 383 fRenderPasses[0]->initSimple(gpu, target); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 } | 419 } |
407 | 420 |
408 void GrVkResourceProvider::CompatibleRenderPassSet::abandonResources() { | 421 void GrVkResourceProvider::CompatibleRenderPassSet::abandonResources() { |
409 for (int i = 0; i < fRenderPasses.count(); ++i) { | 422 for (int i = 0; i < fRenderPasses.count(); ++i) { |
410 if (fRenderPasses[i]) { | 423 if (fRenderPasses[i]) { |
411 fRenderPasses[i]->unrefAndAbandon(); | 424 fRenderPasses[i]->unrefAndAbandon(); |
412 fRenderPasses[i] = nullptr; | 425 fRenderPasses[i] = nullptr; |
413 } | 426 } |
414 } | 427 } |
415 } | 428 } |
OLD | NEW |