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 "GrVkProgram.h" | 8 #include "GrVkProgram.h" |
9 | 9 |
10 #include "GrPipeline.h" | 10 #include "GrPipeline.h" |
11 #include "GrVkCommandBuffer.h" | 11 #include "GrVkCommandBuffer.h" |
12 #include "GrVkDescriptorPool.h" | 12 #include "GrVkDescriptorPool.h" |
13 #include "GrVkGpu.h" | 13 #include "GrVkGpu.h" |
14 #include "GrVkImageView.h" | 14 #include "GrVkImageView.h" |
15 #include "GrVkMemory.h" | 15 #include "GrVkMemory.h" |
16 #include "GrVkPipeline.h" | 16 #include "GrVkPipeline.h" |
17 #include "GrVkSampler.h" | 17 #include "GrVkSampler.h" |
18 #include "GrVkTexture.h" | 18 #include "GrVkTexture.h" |
19 #include "GrVkUniformBuffer.h" | 19 #include "GrVkUniformBuffer.h" |
20 #include "glsl/GrGLSLFragmentProcessor.h" | 20 #include "glsl/GrGLSLFragmentProcessor.h" |
21 #include "glsl/GrGLSLGeometryProcessor.h" | 21 #include "glsl/GrGLSLGeometryProcessor.h" |
22 #include "glsl/GrGLSLXferProcessor.h" | 22 #include "glsl/GrGLSLXferProcessor.h" |
23 | 23 |
24 GrVkProgram::GrVkProgram(GrVkGpu* gpu, | 24 GrVkProgram::GrVkProgram(GrVkGpu* gpu, |
25 GrVkPipeline* pipeline, | 25 GrVkPipeline* pipeline, |
26 VkPipelineLayout layout, | 26 VkPipelineLayout layout, |
27 VkDescriptorSetLayout dsLayout[2], | 27 VkDescriptorSetLayout dsLayout[2], |
28 GrVkDescriptorPool* descriptorPool, | |
29 VkDescriptorSet descriptorSets[2], | |
30 const BuiltinUniformHandles& builtinUniformHandles, | 28 const BuiltinUniformHandles& builtinUniformHandles, |
31 const UniformInfoArray& uniforms, | 29 const UniformInfoArray& uniforms, |
32 uint32_t vertexUniformSize, | 30 uint32_t vertexUniformSize, |
33 uint32_t fragmentUniformSize, | 31 uint32_t fragmentUniformSize, |
34 uint32_t numSamplers, | 32 uint32_t numSamplers, |
35 GrGLSLPrimitiveProcessor* geometryProcessor, | 33 GrGLSLPrimitiveProcessor* geometryProcessor, |
36 GrGLSLXferProcessor* xferProcessor, | 34 GrGLSLXferProcessor* xferProcessor, |
37 const GrGLSLFragProcs& fragmentProcessors) | 35 const GrGLSLFragProcs& fragmentProcessors) |
38 : fDescriptorPool(descriptorPool) | 36 : fPipeline(pipeline) |
39 , fPipeline(pipeline) | |
40 , fPipelineLayout(layout) | 37 , fPipelineLayout(layout) |
41 , fBuiltinUniformHandles(builtinUniformHandles) | 38 , fBuiltinUniformHandles(builtinUniformHandles) |
42 , fGeometryProcessor(geometryProcessor) | 39 , fGeometryProcessor(geometryProcessor) |
43 , fXferProcessor(xferProcessor) | 40 , fXferProcessor(xferProcessor) |
44 , fFragmentProcessors(fragmentProcessors) | 41 , fFragmentProcessors(fragmentProcessors) |
45 , fProgramDataManager(uniforms, vertexUniformSize, fragmentUniformSize) { | 42 , fProgramDataManager(uniforms, vertexUniformSize, fragmentUniformSize) |
| 43 , fSamplerPoolManager(dsLayout[GrVkUniformHandler::kSamplerDescSet], |
| 44 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, numSamplers
, gpu) |
| 45 , fUniformPoolManager(dsLayout[GrVkUniformHandler::kUniformBufferDescSet], |
| 46 VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 2, gpu) { |
46 fSamplers.setReserve(numSamplers); | 47 fSamplers.setReserve(numSamplers); |
47 fTextureViews.setReserve(numSamplers); | 48 fTextureViews.setReserve(numSamplers); |
48 fTextures.setReserve(numSamplers); | 49 fTextures.setReserve(numSamplers); |
49 | 50 |
50 memcpy(fDSLayout, dsLayout, 2 * sizeof(VkDescriptorSetLayout)); | 51 fDescriptorSets[0] = VK_NULL_HANDLE; |
51 memcpy(fDescriptorSets, descriptorSets, 2 * sizeof(VkDescriptorSetLayout)); | 52 fDescriptorSets[1] = VK_NULL_HANDLE; |
| 53 |
| 54 // Currently we are always binding a descriptor set for uniform buffers. |
| 55 fStartDS = GrVkUniformHandler::kUniformBufferDescSet; |
| 56 fDSCount = 1; |
| 57 if (numSamplers) { |
| 58 fDSCount++; |
| 59 fStartDS = SkTMin(fStartDS, (int)GrVkUniformHandler::kSamplerDescSet); |
| 60 } |
52 | 61 |
53 fVertexUniformBuffer.reset(GrVkUniformBuffer::Create(gpu, vertexUniformSize,
true)); | 62 fVertexUniformBuffer.reset(GrVkUniformBuffer::Create(gpu, vertexUniformSize,
true)); |
54 fFragmentUniformBuffer.reset(GrVkUniformBuffer::Create(gpu, fragmentUniformS
ize, true)); | 63 fFragmentUniformBuffer.reset(GrVkUniformBuffer::Create(gpu, fragmentUniformS
ize, true)); |
55 | 64 |
56 #ifdef SK_DEBUG | 65 #ifdef SK_DEBUG |
57 fNumSamplers = numSamplers; | 66 fNumSamplers = numSamplers; |
58 #endif | 67 #endif |
59 } | 68 } |
60 | 69 |
61 GrVkProgram::~GrVkProgram() { | 70 GrVkProgram::~GrVkProgram() { |
62 // Must of freed all GPU resources before this is destroyed | 71 // Must of freed all GPU resources before this is destroyed |
63 SkASSERT(!fPipeline); | 72 SkASSERT(!fPipeline); |
64 SkASSERT(!fDescriptorPool); | |
65 SkASSERT(!fPipelineLayout); | 73 SkASSERT(!fPipelineLayout); |
66 SkASSERT(!fDSLayout[0]); | |
67 SkASSERT(!fDSLayout[1]); | |
68 SkASSERT(!fSamplers.count()); | 74 SkASSERT(!fSamplers.count()); |
69 SkASSERT(!fTextureViews.count()); | 75 SkASSERT(!fTextureViews.count()); |
70 SkASSERT(!fTextures.count()); | 76 SkASSERT(!fTextures.count()); |
71 } | 77 } |
72 | 78 |
73 void GrVkProgram::freeTempResources(const GrVkGpu* gpu) { | 79 void GrVkProgram::freeTempResources(const GrVkGpu* gpu) { |
74 for (int i = 0; i < fSamplers.count(); ++i) { | 80 for (int i = 0; i < fSamplers.count(); ++i) { |
75 fSamplers[i]->unref(gpu); | 81 fSamplers[i]->unref(gpu); |
76 } | 82 } |
77 fSamplers.rewind(); | 83 fSamplers.rewind(); |
78 | 84 |
79 for (int i = 0; i < fTextureViews.count(); ++i) { | 85 for (int i = 0; i < fTextureViews.count(); ++i) { |
80 fTextureViews[i]->unref(gpu); | 86 fTextureViews[i]->unref(gpu); |
81 } | 87 } |
82 fTextureViews.rewind(); | 88 fTextureViews.rewind(); |
83 | 89 |
84 for (int i = 0; i < fTextures.count(); ++i) { | 90 for (int i = 0; i < fTextures.count(); ++i) { |
85 fTextures[i]->unref(gpu); | 91 fTextures[i]->unref(gpu); |
86 } | 92 } |
87 fTextures.rewind(); | 93 fTextures.rewind(); |
88 } | 94 } |
89 | 95 |
90 void GrVkProgram::freeGPUResources(const GrVkGpu* gpu) { | 96 void GrVkProgram::freeGPUResources(const GrVkGpu* gpu) { |
91 if (fPipeline) { | 97 if (fPipeline) { |
92 fPipeline->unref(gpu); | 98 fPipeline->unref(gpu); |
93 fPipeline = nullptr; | 99 fPipeline = nullptr; |
94 } | 100 } |
95 if (fDescriptorPool) { | 101 |
96 fDescriptorPool->unref(gpu); | |
97 fDescriptorPool = nullptr; | |
98 } | |
99 if (fPipelineLayout) { | 102 if (fPipelineLayout) { |
100 GR_VK_CALL(gpu->vkInterface(), DestroyPipelineLayout(gpu->device(), | 103 GR_VK_CALL(gpu->vkInterface(), DestroyPipelineLayout(gpu->device(), |
101 fPipelineLayout, | 104 fPipelineLayout, |
102 nullptr)); | 105 nullptr)); |
103 fPipelineLayout = VK_NULL_HANDLE; | 106 fPipelineLayout = VK_NULL_HANDLE; |
104 } | 107 } |
105 | 108 |
106 if (fDSLayout[0]) { | |
107 GR_VK_CALL(gpu->vkInterface(), DestroyDescriptorSetLayout(gpu->device(),
fDSLayout[0], | |
108 nullptr)); | |
109 fDSLayout[0] = VK_NULL_HANDLE; | |
110 } | |
111 if (fDSLayout[1]) { | |
112 GR_VK_CALL(gpu->vkInterface(), DestroyDescriptorSetLayout(gpu->device(),
fDSLayout[1], | |
113 nullptr)); | |
114 fDSLayout[1] = VK_NULL_HANDLE; | |
115 } | |
116 | |
117 if (fVertexUniformBuffer) { | 109 if (fVertexUniformBuffer) { |
118 fVertexUniformBuffer->release(gpu); | 110 fVertexUniformBuffer->release(gpu); |
119 } | 111 } |
120 | 112 |
121 if (fFragmentUniformBuffer) { | 113 if (fFragmentUniformBuffer) { |
122 fFragmentUniformBuffer->release(gpu); | 114 fFragmentUniformBuffer->release(gpu); |
123 } | 115 } |
| 116 |
| 117 fSamplerPoolManager.freeGPUResources(gpu); |
| 118 fUniformPoolManager.freeGPUResources(gpu); |
| 119 |
124 this->freeTempResources(gpu); | 120 this->freeTempResources(gpu); |
125 } | 121 } |
126 | 122 |
127 void GrVkProgram::abandonGPUResources() { | 123 void GrVkProgram::abandonGPUResources() { |
128 fPipeline->unrefAndAbandon(); | 124 fPipeline->unrefAndAbandon(); |
129 fPipeline = nullptr; | 125 fPipeline = nullptr; |
130 fDescriptorPool->unrefAndAbandon(); | 126 |
131 fDescriptorPool = nullptr; | |
132 fPipelineLayout = VK_NULL_HANDLE; | 127 fPipelineLayout = VK_NULL_HANDLE; |
133 fDSLayout[0] = VK_NULL_HANDLE; | |
134 fDSLayout[1] = VK_NULL_HANDLE; | |
135 | 128 |
136 fVertexUniformBuffer->abandon(); | 129 fVertexUniformBuffer->abandon(); |
137 fFragmentUniformBuffer->abandon(); | 130 fFragmentUniformBuffer->abandon(); |
138 | 131 |
139 for (int i = 0; i < fSamplers.count(); ++i) { | 132 for (int i = 0; i < fSamplers.count(); ++i) { |
140 fSamplers[i]->unrefAndAbandon(); | 133 fSamplers[i]->unrefAndAbandon(); |
141 } | 134 } |
142 fSamplers.rewind(); | 135 fSamplers.rewind(); |
143 | 136 |
144 for (int i = 0; i < fTextureViews.count(); ++i) { | 137 for (int i = 0; i < fTextureViews.count(); ++i) { |
145 fTextureViews[i]->unrefAndAbandon(); | 138 fTextureViews[i]->unrefAndAbandon(); |
146 } | 139 } |
147 fTextureViews.rewind(); | 140 fTextureViews.rewind(); |
148 | 141 |
149 for (int i = 0; i < fTextures.count(); ++i) { | 142 for (int i = 0; i < fTextures.count(); ++i) { |
150 fTextures[i]->unrefAndAbandon(); | 143 fTextures[i]->unrefAndAbandon(); |
151 } | 144 } |
152 fTextures.rewind(); | 145 fTextures.rewind(); |
| 146 |
| 147 fSamplerPoolManager.abandonGPUResources(); |
| 148 fUniformPoolManager.abandonGPUResources(); |
153 } | 149 } |
154 | 150 |
155 static void append_texture_bindings(const GrProcessor& processor, | 151 static void append_texture_bindings(const GrProcessor& processor, |
156 SkTArray<const GrTextureAccess*>* textureBin
dings) { | 152 SkTArray<const GrTextureAccess*>* textureBin
dings) { |
157 if (int numTextures = processor.numTextures()) { | 153 if (int numTextures = processor.numTextures()) { |
158 const GrTextureAccess** bindings = textureBindings->push_back_n(numTextu
res); | 154 const GrTextureAccess** bindings = textureBindings->push_back_n(numTextu
res); |
159 int i = 0; | 155 int i = 0; |
160 do { | 156 do { |
161 bindings[i] = &processor.textureAccess(i); | 157 bindings[i] = &processor.textureAccess(i); |
162 } while (++i < numTextures); | 158 } while (++i < numTextures); |
(...skipping 18 matching lines...) Expand all Loading... |
181 const GrFragmentProcessor& processor = pipeline.getFragmentProcessor(i); | 177 const GrFragmentProcessor& processor = pipeline.getFragmentProcessor(i); |
182 fFragmentProcessors[i]->setData(fProgramDataManager, processor); | 178 fFragmentProcessors[i]->setData(fProgramDataManager, processor); |
183 fGeometryProcessor->setTransformData(primProc, fProgramDataManager, i, | 179 fGeometryProcessor->setTransformData(primProc, fProgramDataManager, i, |
184 processor.coordTransforms()); | 180 processor.coordTransforms()); |
185 append_texture_bindings(processor, &textureBindings); | 181 append_texture_bindings(processor, &textureBindings); |
186 } | 182 } |
187 | 183 |
188 fXferProcessor->setData(fProgramDataManager, pipeline.getXferProcessor()); | 184 fXferProcessor->setData(fProgramDataManager, pipeline.getXferProcessor()); |
189 append_texture_bindings(pipeline.getXferProcessor(), &textureBindings); | 185 append_texture_bindings(pipeline.getXferProcessor(), &textureBindings); |
190 | 186 |
| 187 // Get new descriptor sets |
| 188 if (fNumSamplers) { |
| 189 fSamplerPoolManager.getNewDescriptorSet(gpu, |
| 190 &fDescriptorSets[GrVkUniformHandler
::kSamplerDescSet]); |
| 191 } |
| 192 fUniformPoolManager.getNewDescriptorSet(gpu, |
| 193 &fDescriptorSets[GrVkUniformHandler::kUni
formBufferDescSet]); |
| 194 |
191 this->writeUniformBuffers(gpu); | 195 this->writeUniformBuffers(gpu); |
192 | 196 |
193 this->writeSamplers(gpu, textureBindings); | 197 this->writeSamplers(gpu, textureBindings); |
194 } | 198 } |
195 | 199 |
196 void GrVkProgram::writeUniformBuffers(const GrVkGpu* gpu) { | 200 void GrVkProgram::writeUniformBuffers(const GrVkGpu* gpu) { |
197 fProgramDataManager.uploadUniformBuffers(gpu, fVertexUniformBuffer, fFragmen
tUniformBuffer); | 201 fProgramDataManager.uploadUniformBuffers(gpu, fVertexUniformBuffer, fFragmen
tUniformBuffer); |
198 | 202 |
199 VkWriteDescriptorSet descriptorWrites[2]; | 203 VkWriteDescriptorSet descriptorWrites[2]; |
200 memset(descriptorWrites, 0, 2 * sizeof(VkWriteDescriptorSet)); | 204 memset(descriptorWrites, 0, 2 * sizeof(VkWriteDescriptorSet)); |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 fRenderTargetState.fRenderTargetOrigin = rt->origin(); | 337 fRenderTargetState.fRenderTargetOrigin = rt->origin(); |
334 | 338 |
335 float rtAdjustmentVec[4]; | 339 float rtAdjustmentVec[4]; |
336 fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec); | 340 fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec); |
337 fProgramDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, r
tAdjustmentVec); | 341 fProgramDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, r
tAdjustmentVec); |
338 } | 342 } |
339 } | 343 } |
340 | 344 |
341 void GrVkProgram::bind(const GrVkGpu* gpu, GrVkCommandBuffer* commandBuffer) { | 345 void GrVkProgram::bind(const GrVkGpu* gpu, GrVkCommandBuffer* commandBuffer) { |
342 commandBuffer->bindPipeline(gpu, fPipeline); | 346 commandBuffer->bindPipeline(gpu, fPipeline); |
343 commandBuffer->bindDescriptorSets(gpu, this, fPipelineLayout, 0, 2, fDescrip
torSets, 0, | 347 |
344 nullptr); | 348 if (fDSCount) { |
| 349 commandBuffer->bindDescriptorSets(gpu, this, fPipelineLayout, fStartDS,
fDSCount, |
| 350 &fDescriptorSets[fStartDS], 0, nullptr
); |
| 351 } |
345 } | 352 } |
346 | 353 |
347 void GrVkProgram::addUniformResources(GrVkCommandBuffer& commandBuffer) { | 354 void GrVkProgram::addUniformResources(GrVkCommandBuffer& commandBuffer) { |
348 #if 1 | 355 if (fSamplerPoolManager.fPool) { |
349 commandBuffer.addResource(fDescriptorPool); | 356 commandBuffer.addResource(fSamplerPoolManager.fPool); |
| 357 } |
| 358 if (fUniformPoolManager.fPool) { |
| 359 commandBuffer.addResource(fUniformPoolManager.fPool); |
| 360 } |
| 361 |
350 if (fVertexUniformBuffer.get()) { | 362 if (fVertexUniformBuffer.get()) { |
351 commandBuffer.addResource(fVertexUniformBuffer->resource()); | 363 commandBuffer.addResource(fVertexUniformBuffer->resource()); |
352 } | 364 } |
353 if (fFragmentUniformBuffer.get()) { | 365 if (fFragmentUniformBuffer.get()) { |
354 commandBuffer.addResource(fFragmentUniformBuffer->resource()); | 366 commandBuffer.addResource(fFragmentUniformBuffer->resource()); |
355 } | 367 } |
356 for (int i = 0; i < fSamplers.count(); ++i) { | 368 for (int i = 0; i < fSamplers.count(); ++i) { |
357 commandBuffer.addResource(fSamplers[i]); | 369 commandBuffer.addResource(fSamplers[i]); |
358 } | 370 } |
359 | 371 |
360 for (int i = 0; i < fTextureViews.count(); ++i) { | 372 for (int i = 0; i < fTextureViews.count(); ++i) { |
361 commandBuffer.addResource(fTextureViews[i]); | 373 commandBuffer.addResource(fTextureViews[i]); |
362 } | 374 } |
363 | 375 |
364 for (int i = 0; i < fTextures.count(); ++i) { | 376 for (int i = 0; i < fTextures.count(); ++i) { |
365 commandBuffer.addResource(fTextures[i]); | 377 commandBuffer.addResource(fTextures[i]); |
366 } | 378 } |
367 #endif | |
368 } | 379 } |
| 380 |
| 381 //////////////////////////////////////////////////////////////////////////////// |
| 382 |
| 383 void GrVkProgram::DescriptorPoolManager::getNewPool(GrVkGpu* gpu) { |
| 384 if (fPool) { |
| 385 fPool->unref(gpu); |
| 386 SkASSERT(fMaxDescriptorSets < (SK_MaxU32 >> 1)); |
| 387 fMaxDescriptorSets = fMaxDescriptorSets << 1; |
| 388 |
| 389 } |
| 390 if (fMaxDescriptorSets) { |
| 391 fPool = gpu->resourceProvider().findOrCreateCompatibleDescriptorPool(fDe
scType, |
| 392 fMa
xDescriptorSets); |
| 393 } |
| 394 SkASSERT(fPool || !fMaxDescriptorSets); |
| 395 } |
| 396 |
| 397 void GrVkProgram::DescriptorPoolManager::getNewDescriptorSet(GrVkGpu* gpu, VkDes
criptorSet* ds) { |
| 398 if (!fMaxDescriptorSets) { |
| 399 return; |
| 400 } |
| 401 if (fCurrentDescriptorSet == fMaxDescriptorSets) { |
| 402 this->getNewPool(gpu); |
| 403 fCurrentDescriptorSet = 0; |
| 404 } |
| 405 fCurrentDescriptorSet++; |
| 406 |
| 407 VkDescriptorSetAllocateInfo dsAllocateInfo; |
| 408 memset(&dsAllocateInfo, 0, sizeof(VkDescriptorSetAllocateInfo)); |
| 409 dsAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; |
| 410 dsAllocateInfo.pNext = nullptr; |
| 411 dsAllocateInfo.descriptorPool = fPool->descPool(); |
| 412 dsAllocateInfo.descriptorSetCount = 1; |
| 413 dsAllocateInfo.pSetLayouts = &fDescLayout; |
| 414 |
| 415 GR_VK_CALL_ERRCHECK(gpu->vkInterface(), AllocateDescriptorSets(gpu->device()
, |
| 416 &dsAllocateIn
fo, |
| 417 ds)); |
| 418 } |
| 419 |
| 420 void GrVkProgram::DescriptorPoolManager::freeGPUResources(const GrVkGpu* gpu) { |
| 421 if (fDescLayout) { |
| 422 GR_VK_CALL(gpu->vkInterface(), DestroyDescriptorSetLayout(gpu->device(),
fDescLayout, |
| 423 nullptr)); |
| 424 fDescLayout = VK_NULL_HANDLE; |
| 425 } |
| 426 |
| 427 if (fPool) { |
| 428 fPool->unref(gpu); |
| 429 fPool = nullptr; |
| 430 } |
| 431 } |
| 432 |
| 433 void GrVkProgram::DescriptorPoolManager::abandonGPUResources() { |
| 434 fDescLayout = VK_NULL_HANDLE; |
| 435 if (fPool) { |
| 436 fPool->unrefAndAbandon(); |
| 437 fPool = nullptr; |
| 438 } |
| 439 } |
OLD | NEW |