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