jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 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 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 8 | #include "GrVkPipelineState.h" |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 9 | |
| 10 | #include "GrPipeline.h" |
| 11 | #include "GrVkCommandBuffer.h" |
| 12 | #include "GrVkDescriptorPool.h" |
| 13 | #include "GrVkGpu.h" |
| 14 | #include "GrVkImageView.h" |
| 15 | #include "GrVkMemory.h" |
| 16 | #include "GrVkPipeline.h" |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 17 | #include "GrVkRenderTarget.h" |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 18 | #include "GrVkSampler.h" |
| 19 | #include "GrVkTexture.h" |
| 20 | #include "GrVkUniformBuffer.h" |
| 21 | #include "glsl/GrGLSLFragmentProcessor.h" |
| 22 | #include "glsl/GrGLSLGeometryProcessor.h" |
| 23 | #include "glsl/GrGLSLXferProcessor.h" |
| 24 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 25 | GrVkPipelineState::GrVkPipelineState(GrVkGpu* gpu, |
| 26 | const GrVkPipelineState::Desc& desc, |
| 27 | GrVkPipeline* pipeline, |
| 28 | VkPipelineLayout layout, |
| 29 | VkDescriptorSetLayout dsLayout[2], |
| 30 | const BuiltinUniformHandles& builtinUniformHandles, |
| 31 | const UniformInfoArray& uniforms, |
| 32 | uint32_t vertexUniformSize, |
| 33 | uint32_t fragmentUniformSize, |
| 34 | uint32_t numSamplers, |
| 35 | GrGLSLPrimitiveProcessor* geometryProcessor, |
| 36 | GrGLSLXferProcessor* xferProcessor, |
| 37 | const GrGLSLFragProcs& fragmentProcessors) |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 38 | : fPipeline(pipeline) |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 39 | , fPipelineLayout(layout) |
egdaniel | b4aa362 | 2016-04-06 13:47:08 -0700 | [diff] [blame] | 40 | , fStartDS(SK_MaxS32) |
| 41 | , fDSCount(0) |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 42 | , fBuiltinUniformHandles(builtinUniformHandles) |
| 43 | , fGeometryProcessor(geometryProcessor) |
| 44 | , fXferProcessor(xferProcessor) |
| 45 | , fFragmentProcessors(fragmentProcessors) |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 46 | , fDesc(desc) |
| 47 | , fDataManager(uniforms, vertexUniformSize, fragmentUniformSize) |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 48 | , fSamplerPoolManager(dsLayout[GrVkUniformHandler::kSamplerDescSet], |
| 49 | VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, numSamplers, gpu) |
| 50 | , fUniformPoolManager(dsLayout[GrVkUniformHandler::kUniformBufferDescSet], |
egdaniel | b4aa362 | 2016-04-06 13:47:08 -0700 | [diff] [blame] | 51 | VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
| 52 | (vertexUniformSize || fragmentUniformSize) ? 2 : 0, gpu) { |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 53 | fSamplers.setReserve(numSamplers); |
| 54 | fTextureViews.setReserve(numSamplers); |
| 55 | fTextures.setReserve(numSamplers); |
| 56 | |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 57 | fDescriptorSets[0] = VK_NULL_HANDLE; |
| 58 | fDescriptorSets[1] = VK_NULL_HANDLE; |
| 59 | |
| 60 | // Currently we are always binding a descriptor set for uniform buffers. |
egdaniel | b4aa362 | 2016-04-06 13:47:08 -0700 | [diff] [blame] | 61 | if (vertexUniformSize || fragmentUniformSize) { |
| 62 | fDSCount++; |
| 63 | fStartDS = GrVkUniformHandler::kUniformBufferDescSet; |
| 64 | } |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 65 | if (numSamplers) { |
| 66 | fDSCount++; |
| 67 | fStartDS = SkTMin(fStartDS, (int)GrVkUniformHandler::kSamplerDescSet); |
| 68 | } |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 69 | |
| 70 | fVertexUniformBuffer.reset(GrVkUniformBuffer::Create(gpu, vertexUniformSize, true)); |
| 71 | fFragmentUniformBuffer.reset(GrVkUniformBuffer::Create(gpu, fragmentUniformSize, true)); |
| 72 | |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 73 | fNumSamplers = numSamplers; |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 74 | } |
| 75 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 76 | GrVkPipelineState::~GrVkPipelineState() { |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 77 | // Must of freed all GPU resources before this is destroyed |
| 78 | SkASSERT(!fPipeline); |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 79 | SkASSERT(!fPipelineLayout); |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 80 | SkASSERT(!fSamplers.count()); |
| 81 | SkASSERT(!fTextureViews.count()); |
| 82 | SkASSERT(!fTextures.count()); |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame^] | 83 | for (int i = 0; i < fFragmentProcessors.count(); ++i) { |
| 84 | delete fFragmentProcessors[i]; |
| 85 | } |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 86 | } |
| 87 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 88 | void GrVkPipelineState::freeTempResources(const GrVkGpu* gpu) { |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 89 | for (int i = 0; i < fSamplers.count(); ++i) { |
| 90 | fSamplers[i]->unref(gpu); |
| 91 | } |
| 92 | fSamplers.rewind(); |
| 93 | |
| 94 | for (int i = 0; i < fTextureViews.count(); ++i) { |
| 95 | fTextureViews[i]->unref(gpu); |
| 96 | } |
| 97 | fTextureViews.rewind(); |
| 98 | |
| 99 | for (int i = 0; i < fTextures.count(); ++i) { |
| 100 | fTextures[i]->unref(gpu); |
| 101 | } |
| 102 | fTextures.rewind(); |
| 103 | } |
| 104 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 105 | void GrVkPipelineState::freeGPUResources(const GrVkGpu* gpu) { |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 106 | if (fPipeline) { |
| 107 | fPipeline->unref(gpu); |
| 108 | fPipeline = nullptr; |
| 109 | } |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 110 | |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 111 | if (fPipelineLayout) { |
| 112 | GR_VK_CALL(gpu->vkInterface(), DestroyPipelineLayout(gpu->device(), |
| 113 | fPipelineLayout, |
| 114 | nullptr)); |
jvanverth | 9846ef2 | 2016-03-02 12:08:22 -0800 | [diff] [blame] | 115 | fPipelineLayout = VK_NULL_HANDLE; |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 116 | } |
| 117 | |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 118 | if (fVertexUniformBuffer) { |
| 119 | fVertexUniformBuffer->release(gpu); |
| 120 | } |
| 121 | |
| 122 | if (fFragmentUniformBuffer) { |
| 123 | fFragmentUniformBuffer->release(gpu); |
| 124 | } |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 125 | |
| 126 | fSamplerPoolManager.freeGPUResources(gpu); |
| 127 | fUniformPoolManager.freeGPUResources(gpu); |
| 128 | |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 129 | this->freeTempResources(gpu); |
| 130 | } |
| 131 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 132 | void GrVkPipelineState::abandonGPUResources() { |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 133 | fPipeline->unrefAndAbandon(); |
| 134 | fPipeline = nullptr; |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 135 | |
jvanverth | 9846ef2 | 2016-03-02 12:08:22 -0800 | [diff] [blame] | 136 | fPipelineLayout = VK_NULL_HANDLE; |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 137 | |
| 138 | fVertexUniformBuffer->abandon(); |
| 139 | fFragmentUniformBuffer->abandon(); |
| 140 | |
| 141 | for (int i = 0; i < fSamplers.count(); ++i) { |
| 142 | fSamplers[i]->unrefAndAbandon(); |
| 143 | } |
| 144 | fSamplers.rewind(); |
| 145 | |
| 146 | for (int i = 0; i < fTextureViews.count(); ++i) { |
| 147 | fTextureViews[i]->unrefAndAbandon(); |
| 148 | } |
| 149 | fTextureViews.rewind(); |
| 150 | |
| 151 | for (int i = 0; i < fTextures.count(); ++i) { |
| 152 | fTextures[i]->unrefAndAbandon(); |
| 153 | } |
| 154 | fTextures.rewind(); |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 155 | |
| 156 | fSamplerPoolManager.abandonGPUResources(); |
| 157 | fUniformPoolManager.abandonGPUResources(); |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static void append_texture_bindings(const GrProcessor& processor, |
| 161 | SkTArray<const GrTextureAccess*>* textureBindings) { |
| 162 | if (int numTextures = processor.numTextures()) { |
| 163 | const GrTextureAccess** bindings = textureBindings->push_back_n(numTextures); |
| 164 | int i = 0; |
| 165 | do { |
| 166 | bindings[i] = &processor.textureAccess(i); |
| 167 | } while (++i < numTextures); |
| 168 | } |
| 169 | } |
| 170 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 171 | void GrVkPipelineState::setData(GrVkGpu* gpu, |
| 172 | const GrPrimitiveProcessor& primProc, |
| 173 | const GrPipeline& pipeline) { |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 174 | // This is here to protect against someone calling setData multiple times in a row without |
| 175 | // freeing the tempData between calls. |
| 176 | this->freeTempResources(gpu); |
| 177 | |
| 178 | this->setRenderTargetState(pipeline); |
| 179 | |
| 180 | SkSTArray<8, const GrTextureAccess*> textureBindings; |
| 181 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 182 | fGeometryProcessor->setData(fDataManager, primProc); |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 183 | append_texture_bindings(primProc, &textureBindings); |
| 184 | |
| 185 | for (int i = 0; i < fFragmentProcessors.count(); ++i) { |
| 186 | const GrFragmentProcessor& processor = pipeline.getFragmentProcessor(i); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 187 | fFragmentProcessors[i]->setData(fDataManager, processor); |
| 188 | fGeometryProcessor->setTransformData(primProc, fDataManager, i, |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 189 | processor.coordTransforms()); |
| 190 | append_texture_bindings(processor, &textureBindings); |
| 191 | } |
| 192 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 193 | fXferProcessor->setData(fDataManager, pipeline.getXferProcessor()); |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 194 | append_texture_bindings(pipeline.getXferProcessor(), &textureBindings); |
| 195 | |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 196 | // Get new descriptor sets |
| 197 | if (fNumSamplers) { |
| 198 | fSamplerPoolManager.getNewDescriptorSet(gpu, |
| 199 | &fDescriptorSets[GrVkUniformHandler::kSamplerDescSet]); |
egdaniel | b4aa362 | 2016-04-06 13:47:08 -0700 | [diff] [blame] | 200 | this->writeSamplers(gpu, textureBindings); |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 201 | } |
egdaniel | b4aa362 | 2016-04-06 13:47:08 -0700 | [diff] [blame] | 202 | |
| 203 | |
| 204 | if (fVertexUniformBuffer.get() || fFragmentUniformBuffer.get()) { |
| 205 | fUniformPoolManager.getNewDescriptorSet(gpu, |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 206 | &fDescriptorSets[GrVkUniformHandler::kUniformBufferDescSet]); |
egdaniel | b4aa362 | 2016-04-06 13:47:08 -0700 | [diff] [blame] | 207 | this->writeUniformBuffers(gpu); |
| 208 | } |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 209 | } |
| 210 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 211 | void GrVkPipelineState::writeUniformBuffers(const GrVkGpu* gpu) { |
| 212 | fDataManager.uploadUniformBuffers(gpu, fVertexUniformBuffer, fFragmentUniformBuffer); |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 213 | |
| 214 | VkWriteDescriptorSet descriptorWrites[2]; |
| 215 | memset(descriptorWrites, 0, 2 * sizeof(VkWriteDescriptorSet)); |
| 216 | |
| 217 | uint32_t firstUniformWrite = 0; |
| 218 | uint32_t uniformBindingUpdateCount = 0; |
| 219 | |
egdaniel | d524f16 | 2016-02-26 13:06:55 -0800 | [diff] [blame] | 220 | VkDescriptorBufferInfo vertBufferInfo; |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 221 | // Vertex Uniform Buffer |
| 222 | if (fVertexUniformBuffer.get()) { |
| 223 | ++uniformBindingUpdateCount; |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 224 | memset(&vertBufferInfo, 0, sizeof(VkDescriptorBufferInfo)); |
| 225 | vertBufferInfo.buffer = fVertexUniformBuffer->buffer(); |
| 226 | vertBufferInfo.offset = 0; |
| 227 | vertBufferInfo.range = fVertexUniformBuffer->size(); |
| 228 | |
| 229 | descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 230 | descriptorWrites[0].pNext = nullptr; |
egdaniel | b4aa362 | 2016-04-06 13:47:08 -0700 | [diff] [blame] | 231 | descriptorWrites[0].dstSet = fDescriptorSets[GrVkUniformHandler::kUniformBufferDescSet]; |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 232 | descriptorWrites[0].dstBinding = GrVkUniformHandler::kVertexBinding; |
| 233 | descriptorWrites[0].dstArrayElement = 0; |
| 234 | descriptorWrites[0].descriptorCount = 1; |
| 235 | descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
| 236 | descriptorWrites[0].pImageInfo = nullptr; |
| 237 | descriptorWrites[0].pBufferInfo = &vertBufferInfo; |
| 238 | descriptorWrites[0].pTexelBufferView = nullptr; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 239 | |
| 240 | fVertexUniformBuffer->addMemoryBarrier(gpu, |
| 241 | VK_ACCESS_HOST_WRITE_BIT, |
| 242 | VK_ACCESS_UNIFORM_READ_BIT, |
| 243 | VK_PIPELINE_STAGE_HOST_BIT, |
| 244 | VK_PIPELINE_STAGE_VERTEX_SHADER_BIT, |
| 245 | false); |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 246 | } |
| 247 | |
egdaniel | d524f16 | 2016-02-26 13:06:55 -0800 | [diff] [blame] | 248 | VkDescriptorBufferInfo fragBufferInfo; |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 249 | // Fragment Uniform Buffer |
| 250 | if (fFragmentUniformBuffer.get()) { |
| 251 | if (0 == uniformBindingUpdateCount) { |
| 252 | firstUniformWrite = 1; |
| 253 | } |
| 254 | ++uniformBindingUpdateCount; |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 255 | memset(&fragBufferInfo, 0, sizeof(VkDescriptorBufferInfo)); |
| 256 | fragBufferInfo.buffer = fFragmentUniformBuffer->buffer(); |
| 257 | fragBufferInfo.offset = 0; |
| 258 | fragBufferInfo.range = fFragmentUniformBuffer->size(); |
| 259 | |
| 260 | descriptorWrites[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 261 | descriptorWrites[1].pNext = nullptr; |
egdaniel | b4aa362 | 2016-04-06 13:47:08 -0700 | [diff] [blame] | 262 | descriptorWrites[1].dstSet = fDescriptorSets[GrVkUniformHandler::kUniformBufferDescSet]; |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 263 | descriptorWrites[1].dstBinding = GrVkUniformHandler::kFragBinding;; |
| 264 | descriptorWrites[1].dstArrayElement = 0; |
| 265 | descriptorWrites[1].descriptorCount = 1; |
| 266 | descriptorWrites[1].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
| 267 | descriptorWrites[1].pImageInfo = nullptr; |
| 268 | descriptorWrites[1].pBufferInfo = &fragBufferInfo; |
| 269 | descriptorWrites[1].pTexelBufferView = nullptr; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 270 | |
| 271 | fFragmentUniformBuffer->addMemoryBarrier(gpu, |
| 272 | VK_ACCESS_HOST_WRITE_BIT, |
| 273 | VK_ACCESS_UNIFORM_READ_BIT, |
| 274 | VK_PIPELINE_STAGE_HOST_BIT, |
| 275 | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, |
| 276 | false); |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | if (uniformBindingUpdateCount) { |
| 280 | GR_VK_CALL(gpu->vkInterface(), UpdateDescriptorSets(gpu->device(), |
| 281 | uniformBindingUpdateCount, |
| 282 | &descriptorWrites[firstUniformWrite], |
| 283 | 0, nullptr)); |
| 284 | } |
| 285 | } |
| 286 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 287 | void GrVkPipelineState::writeSamplers(GrVkGpu* gpu, |
| 288 | const SkTArray<const GrTextureAccess*>& textureBindings) { |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 289 | SkASSERT(fNumSamplers == textureBindings.count()); |
| 290 | |
| 291 | for (int i = 0; i < textureBindings.count(); ++i) { |
egdaniel | 8b6394c | 2016-03-04 07:35:10 -0800 | [diff] [blame] | 292 | const GrTextureParams& params = textureBindings[i]->getParams(); |
| 293 | fSamplers.push(gpu->resourceProvider().findOrCreateCompatibleSampler(params)); |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 294 | |
| 295 | GrVkTexture* texture = static_cast<GrVkTexture*>(textureBindings[i]->getTexture()); |
| 296 | |
| 297 | const GrVkImage::Resource* textureResource = texture->resource(); |
| 298 | textureResource->ref(); |
| 299 | fTextures.push(textureResource); |
| 300 | |
| 301 | const GrVkImageView* textureView = texture->textureView(); |
| 302 | textureView->ref(); |
| 303 | fTextureViews.push(textureView); |
| 304 | |
| 305 | // Change texture layout so it can be read in shader |
| 306 | VkImageLayout layout = texture->currentLayout(); |
| 307 | VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(layout); |
| 308 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT; |
| 309 | VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(layout); |
| 310 | VkAccessFlags dstAccessMask = VK_ACCESS_SHADER_READ_BIT; |
| 311 | texture->setImageLayout(gpu, |
| 312 | VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, |
| 313 | srcAccessMask, |
| 314 | dstAccessMask, |
| 315 | srcStageMask, |
| 316 | dstStageMask, |
| 317 | false); |
| 318 | |
| 319 | VkDescriptorImageInfo imageInfo; |
| 320 | memset(&imageInfo, 0, sizeof(VkDescriptorImageInfo)); |
| 321 | imageInfo.sampler = fSamplers[i]->sampler(); |
| 322 | imageInfo.imageView = texture->textureView()->imageView(); |
| 323 | imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 324 | |
| 325 | VkWriteDescriptorSet writeInfo; |
| 326 | memset(&writeInfo, 0, sizeof(VkWriteDescriptorSet)); |
| 327 | writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 328 | writeInfo.pNext = nullptr; |
| 329 | writeInfo.dstSet = fDescriptorSets[GrVkUniformHandler::kSamplerDescSet]; |
| 330 | writeInfo.dstBinding = i; |
| 331 | writeInfo.dstArrayElement = 0; |
| 332 | writeInfo.descriptorCount = 1; |
| 333 | writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
| 334 | writeInfo.pImageInfo = &imageInfo; |
| 335 | writeInfo.pBufferInfo = nullptr; |
| 336 | writeInfo.pTexelBufferView = nullptr; |
| 337 | |
| 338 | GR_VK_CALL(gpu->vkInterface(), UpdateDescriptorSets(gpu->device(), |
| 339 | 1, |
| 340 | &writeInfo, |
| 341 | 0, |
| 342 | nullptr)); |
| 343 | } |
| 344 | } |
| 345 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 346 | void GrVkPipelineState::setRenderTargetState(const GrPipeline& pipeline) { |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 347 | // Load the RT height uniform if it is needed to y-flip gl_FragCoord. |
| 348 | if (fBuiltinUniformHandles.fRTHeightUni.isValid() && |
| 349 | fRenderTargetState.fRenderTargetSize.fHeight != pipeline.getRenderTarget()->height()) { |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 350 | fDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 351 | SkIntToScalar(pipeline.getRenderTarget()->height())); |
| 352 | } |
| 353 | |
| 354 | // set RT adjustment |
| 355 | const GrRenderTarget* rt = pipeline.getRenderTarget(); |
| 356 | SkISize size; |
| 357 | size.set(rt->width(), rt->height()); |
| 358 | SkASSERT(fBuiltinUniformHandles.fRTAdjustmentUni.isValid()); |
| 359 | if (fRenderTargetState.fRenderTargetOrigin != rt->origin() || |
| 360 | fRenderTargetState.fRenderTargetSize != size) { |
| 361 | fRenderTargetState.fRenderTargetSize = size; |
| 362 | fRenderTargetState.fRenderTargetOrigin = rt->origin(); |
| 363 | |
| 364 | float rtAdjustmentVec[4]; |
| 365 | fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 366 | fDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec); |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 370 | void GrVkPipelineState::bind(const GrVkGpu* gpu, GrVkCommandBuffer* commandBuffer) { |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 371 | commandBuffer->bindPipeline(gpu, fPipeline); |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 372 | |
| 373 | if (fDSCount) { |
| 374 | commandBuffer->bindDescriptorSets(gpu, this, fPipelineLayout, fStartDS, fDSCount, |
| 375 | &fDescriptorSets[fStartDS], 0, nullptr); |
| 376 | } |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 377 | } |
| 378 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 379 | void GrVkPipelineState::addUniformResources(GrVkCommandBuffer& commandBuffer) { |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 380 | if (fSamplerPoolManager.fPool) { |
| 381 | commandBuffer.addResource(fSamplerPoolManager.fPool); |
| 382 | } |
| 383 | if (fUniformPoolManager.fPool) { |
| 384 | commandBuffer.addResource(fUniformPoolManager.fPool); |
| 385 | } |
| 386 | |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 387 | if (fVertexUniformBuffer.get()) { |
| 388 | commandBuffer.addResource(fVertexUniformBuffer->resource()); |
| 389 | } |
| 390 | if (fFragmentUniformBuffer.get()) { |
| 391 | commandBuffer.addResource(fFragmentUniformBuffer->resource()); |
| 392 | } |
| 393 | for (int i = 0; i < fSamplers.count(); ++i) { |
| 394 | commandBuffer.addResource(fSamplers[i]); |
| 395 | } |
| 396 | |
| 397 | for (int i = 0; i < fTextureViews.count(); ++i) { |
| 398 | commandBuffer.addResource(fTextureViews[i]); |
| 399 | } |
| 400 | |
| 401 | for (int i = 0; i < fTextures.count(); ++i) { |
| 402 | commandBuffer.addResource(fTextures[i]); |
| 403 | } |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | //////////////////////////////////////////////////////////////////////////////// |
| 407 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 408 | void GrVkPipelineState::DescriptorPoolManager::getNewPool(GrVkGpu* gpu) { |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 409 | if (fPool) { |
| 410 | fPool->unref(gpu); |
| 411 | SkASSERT(fMaxDescriptorSets < (SK_MaxU32 >> 1)); |
| 412 | fMaxDescriptorSets = fMaxDescriptorSets << 1; |
| 413 | |
| 414 | } |
| 415 | if (fMaxDescriptorSets) { |
| 416 | fPool = gpu->resourceProvider().findOrCreateCompatibleDescriptorPool(fDescType, |
| 417 | fMaxDescriptorSets); |
| 418 | } |
| 419 | SkASSERT(fPool || !fMaxDescriptorSets); |
| 420 | } |
| 421 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 422 | void GrVkPipelineState::DescriptorPoolManager::getNewDescriptorSet(GrVkGpu* gpu, VkDescriptorSet* ds) { |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 423 | if (!fMaxDescriptorSets) { |
| 424 | return; |
| 425 | } |
| 426 | if (fCurrentDescriptorSet == fMaxDescriptorSets) { |
| 427 | this->getNewPool(gpu); |
| 428 | fCurrentDescriptorSet = 0; |
| 429 | } |
| 430 | fCurrentDescriptorSet++; |
| 431 | |
| 432 | VkDescriptorSetAllocateInfo dsAllocateInfo; |
| 433 | memset(&dsAllocateInfo, 0, sizeof(VkDescriptorSetAllocateInfo)); |
| 434 | dsAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; |
| 435 | dsAllocateInfo.pNext = nullptr; |
| 436 | dsAllocateInfo.descriptorPool = fPool->descPool(); |
| 437 | dsAllocateInfo.descriptorSetCount = 1; |
| 438 | dsAllocateInfo.pSetLayouts = &fDescLayout; |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 439 | GR_VK_CALL_ERRCHECK(gpu->vkInterface(), AllocateDescriptorSets(gpu->device(), |
| 440 | &dsAllocateInfo, |
| 441 | ds)); |
| 442 | } |
| 443 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 444 | void GrVkPipelineState::DescriptorPoolManager::freeGPUResources(const GrVkGpu* gpu) { |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 445 | if (fDescLayout) { |
| 446 | GR_VK_CALL(gpu->vkInterface(), DestroyDescriptorSetLayout(gpu->device(), fDescLayout, |
| 447 | nullptr)); |
| 448 | fDescLayout = VK_NULL_HANDLE; |
| 449 | } |
| 450 | |
| 451 | if (fPool) { |
| 452 | fPool->unref(gpu); |
| 453 | fPool = nullptr; |
| 454 | } |
| 455 | } |
| 456 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 457 | void GrVkPipelineState::DescriptorPoolManager::abandonGPUResources() { |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 458 | fDescLayout = VK_NULL_HANDLE; |
| 459 | if (fPool) { |
| 460 | fPool->unrefAndAbandon(); |
| 461 | fPool = nullptr; |
| 462 | } |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 463 | } |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 464 | |
| 465 | uint32_t get_blend_info_key(const GrPipeline& pipeline) { |
| 466 | GrXferProcessor::BlendInfo blendInfo; |
| 467 | pipeline.getXferProcessor().getBlendInfo(&blendInfo); |
| 468 | |
| 469 | static const uint32_t kBlendWriteShift = 1; |
| 470 | static const uint32_t kBlendCoeffShift = 5; |
| 471 | GR_STATIC_ASSERT(kLast_GrBlendCoeff < (1 << kBlendCoeffShift)); |
| 472 | GR_STATIC_ASSERT(kFirstAdvancedGrBlendEquation - 1 < 4); |
| 473 | |
| 474 | uint32_t key = blendInfo.fWriteColor; |
| 475 | key |= (blendInfo.fSrcBlend << kBlendWriteShift); |
| 476 | key |= (blendInfo.fDstBlend << (kBlendWriteShift + kBlendCoeffShift)); |
| 477 | key |= (blendInfo.fEquation << (kBlendWriteShift + 2 * kBlendCoeffShift)); |
| 478 | |
| 479 | return key; |
| 480 | } |
| 481 | |
| 482 | void GrVkPipelineState::BuildStateKey(const GrPipeline& pipeline, GrPrimitiveType primitiveType, |
| 483 | SkTArray<uint8_t, true>* key) { |
| 484 | // Save room for the key length and key header |
| 485 | key->reset(); |
| 486 | key->push_back_n(kData_StateKeyOffset); |
| 487 | |
| 488 | GrProcessorKeyBuilder b(key); |
| 489 | |
| 490 | GrVkRenderTarget* vkRT = (GrVkRenderTarget*)pipeline.getRenderTarget(); |
| 491 | vkRT->simpleRenderPass()->genKey(&b); |
| 492 | |
| 493 | pipeline.getStencil().genKey(&b); |
| 494 | |
| 495 | SkASSERT(sizeof(GrPipelineBuilder::DrawFace) <= sizeof(uint32_t)); |
| 496 | b.add32(pipeline.getDrawFace()); |
| 497 | |
| 498 | b.add32(get_blend_info_key(pipeline)); |
| 499 | |
| 500 | b.add32(primitiveType); |
| 501 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 502 | // Set key length |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 503 | int keyLength = key->count(); |
| 504 | SkASSERT(0 == (keyLength % 4)); |
| 505 | *reinterpret_cast<uint32_t*>(key->begin()) = SkToU32(keyLength); |
| 506 | } |