| 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 "vk/GrVkPipelineStateBuilder.h" | 8 #include "vk/GrVkPipelineStateBuilder.h" |
| 9 | 9 |
| 10 #include "vk/GrVkDescriptorSetManager.h" |
| 10 #include "vk/GrVkGpu.h" | 11 #include "vk/GrVkGpu.h" |
| 11 #include "vk/GrVkRenderPass.h" | 12 #include "vk/GrVkRenderPass.h" |
| 12 #if USE_SKSL | 13 #if USE_SKSL |
| 13 #include "SkSLCompiler.h" | 14 #include "SkSLCompiler.h" |
| 14 #endif | 15 #endif |
| 15 | 16 |
| 16 GrVkPipelineState* GrVkPipelineStateBuilder::CreatePipelineState( | 17 GrVkPipelineState* GrVkPipelineStateBuilder::CreatePipelineState( |
| 17 GrVkGpu* gpu, | 18 GrVkGpu* gpu, |
| 18 const GrPipeline&
pipeline, | 19 const GrPipeline&
pipeline, |
| 19 const GrPrimitive
Processor& primProc, | 20 const GrPrimitive
Processor& primProc, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 } | 54 } |
| 54 | 55 |
| 55 void GrVkPipelineStateBuilder::finalizeFragmentOutputColor(GrGLSLShaderVar& outp
utColor) { | 56 void GrVkPipelineStateBuilder::finalizeFragmentOutputColor(GrGLSLShaderVar& outp
utColor) { |
| 56 outputColor.setLayoutQualifier("location = 0, index = 0"); | 57 outputColor.setLayoutQualifier("location = 0, index = 0"); |
| 57 } | 58 } |
| 58 | 59 |
| 59 void GrVkPipelineStateBuilder::finalizeFragmentSecondaryColor(GrGLSLShaderVar& o
utputColor) { | 60 void GrVkPipelineStateBuilder::finalizeFragmentSecondaryColor(GrGLSLShaderVar& o
utputColor) { |
| 60 outputColor.setLayoutQualifier("location = 0, index = 1"); | 61 outputColor.setLayoutQualifier("location = 0, index = 1"); |
| 61 } | 62 } |
| 62 | 63 |
| 63 VkShaderStageFlags visibility_to_vk_stage_flags(uint32_t visibility) { | |
| 64 VkShaderStageFlags flags = 0; | |
| 65 | |
| 66 if (visibility & kVertex_GrShaderFlag) { | |
| 67 flags |= VK_SHADER_STAGE_VERTEX_BIT; | |
| 68 } | |
| 69 if (visibility & kGeometry_GrShaderFlag) { | |
| 70 flags |= VK_SHADER_STAGE_GEOMETRY_BIT; | |
| 71 } | |
| 72 if (visibility & kFragment_GrShaderFlag) { | |
| 73 flags |= VK_SHADER_STAGE_FRAGMENT_BIT; | |
| 74 } | |
| 75 return flags; | |
| 76 } | |
| 77 | |
| 78 #if USE_SKSL | 64 #if USE_SKSL |
| 79 SkSL::Program::Kind vk_shader_stage_to_skiasl_kind(VkShaderStageFlagBits stage)
{ | 65 SkSL::Program::Kind vk_shader_stage_to_skiasl_kind(VkShaderStageFlagBits stage)
{ |
| 80 if (VK_SHADER_STAGE_VERTEX_BIT == stage) { | 66 if (VK_SHADER_STAGE_VERTEX_BIT == stage) { |
| 81 return SkSL::Program::kVertex_Kind; | 67 return SkSL::Program::kVertex_Kind; |
| 82 } | 68 } |
| 83 SkASSERT(VK_SHADER_STAGE_FRAGMENT_BIT == stage); | 69 SkASSERT(VK_SHADER_STAGE_FRAGMENT_BIT == stage); |
| 84 return SkSL::Program::kFragment_Kind; | 70 return SkSL::Program::kFragment_Kind; |
| 85 } | 71 } |
| 86 #else | 72 #else |
| 87 shaderc_shader_kind vk_shader_stage_to_shaderc_kind(VkShaderStageFlagBits stage)
{ | 73 shaderc_shader_kind vk_shader_stage_to_shaderc_kind(VkShaderStageFlagBits stage)
{ |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 #else | 105 #else |
| 120 shaderc_compilation_result_t result = nullptr; | 106 shaderc_compilation_result_t result = nullptr; |
| 121 #endif | 107 #endif |
| 122 | 108 |
| 123 if (gpu->vkCaps().canUseGLSLForShaderModule()) { | 109 if (gpu->vkCaps().canUseGLSLForShaderModule()) { |
| 124 moduleCreateInfo.codeSize = strlen(shaderString.c_str()); | 110 moduleCreateInfo.codeSize = strlen(shaderString.c_str()); |
| 125 moduleCreateInfo.pCode = (const uint32_t*)shaderString.c_str(); | 111 moduleCreateInfo.pCode = (const uint32_t*)shaderString.c_str(); |
| 126 } else { | 112 } else { |
| 127 | 113 |
| 128 #if USE_SKSL | 114 #if USE_SKSL |
| 129 bool result = gpu->shaderCompiler()->toSPIRV(vk_shader_stage_to_skiasl_k
ind(stage), | 115 bool result = gpu->shaderCompiler()->toSPIRV(vk_shader_stage_to_skiasl_k
ind(stage), |
| 130 std::string(shaderString.c_
str()), | 116 std::string(shaderString.c_
str()), |
| 131 &code); | 117 &code); |
| 132 if (!result) { | 118 if (!result) { |
| 133 SkDebugf("%s\n", gpu->shaderCompiler()->errorText().c_str()); | 119 SkDebugf("%s\n", gpu->shaderCompiler()->errorText().c_str()); |
| 134 return false; | 120 return false; |
| 135 } | 121 } |
| 136 moduleCreateInfo.codeSize = code.size(); | 122 moduleCreateInfo.codeSize = code.size(); |
| 137 moduleCreateInfo.pCode = (const uint32_t*) code.c_str(); | 123 moduleCreateInfo.pCode = (const uint32_t*) code.c_str(); |
| 138 #else | 124 #else |
| 139 shaderc_compiler_t compiler = gpu->shadercCompiler(); | 125 shaderc_compiler_t compiler = gpu->shadercCompiler(); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 } | 175 } |
| 190 | 176 |
| 191 GrVkPipelineState* GrVkPipelineStateBuilder::finalize(GrPrimitiveType primitiveT
ype, | 177 GrVkPipelineState* GrVkPipelineStateBuilder::finalize(GrPrimitiveType primitiveT
ype, |
| 192 const GrVkRenderPass& rend
erPass, | 178 const GrVkRenderPass& rend
erPass, |
| 193 const GrVkPipelineState::D
esc& desc) { | 179 const GrVkPipelineState::D
esc& desc) { |
| 194 VkDescriptorSetLayout dsLayout[2]; | 180 VkDescriptorSetLayout dsLayout[2]; |
| 195 VkPipelineLayout pipelineLayout; | 181 VkPipelineLayout pipelineLayout; |
| 196 VkShaderModule vertShaderModule; | 182 VkShaderModule vertShaderModule; |
| 197 VkShaderModule fragShaderModule; | 183 VkShaderModule fragShaderModule; |
| 198 | 184 |
| 199 uint32_t numSamplers = (uint32_t)fUniformHandler.numSamplers(); | 185 GrVkResourceProvider& resourceProvider = fGpu->resourceProvider(); |
| 186 // This layout is not owned by the PipelineStateBuilder and thus should no b
e destroyed |
| 187 dsLayout[GrVkUniformHandler::kUniformBufferDescSet] = resourceProvider.getUn
iformDSLayout(); |
| 200 | 188 |
| 201 SkAutoTDeleteArray<VkDescriptorSetLayoutBinding> dsSamplerBindings( | 189 GrVkDescriptorSetManager::Handle samplerDSHandle; |
| 202 new VkDescriptorSetLayoutBi
nding[numSamplers]); | 190 resourceProvider.getSamplerDescriptorSetHandle(fUniformHandler, &samplerDSHa
ndle); |
| 203 for (uint32_t i = 0; i < numSamplers; ++i) { | 191 dsLayout[GrVkUniformHandler::kSamplerDescSet] = |
| 204 const GrVkGLSLSampler& sampler = | 192 resourceProvider.getSamplerDSLayout(samplerDSHandle); |
| 205 static_cast<const GrVkGLSLSampler&>(fUniformHandler.getSampler(i)); | |
| 206 SkASSERT(sampler.binding() == i); | |
| 207 dsSamplerBindings[i].binding = sampler.binding(); | |
| 208 dsSamplerBindings[i].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_
SAMPLER; | |
| 209 dsSamplerBindings[i].descriptorCount = 1; | |
| 210 dsSamplerBindings[i].stageFlags = visibility_to_vk_stage_flags(sampler.v
isibility()); | |
| 211 dsSamplerBindings[i].pImmutableSamplers = nullptr; | |
| 212 } | |
| 213 | |
| 214 VkDescriptorSetLayoutCreateInfo dsSamplerLayoutCreateInfo; | |
| 215 memset(&dsSamplerLayoutCreateInfo, 0, sizeof(VkDescriptorSetLayoutCreateInfo
)); | |
| 216 dsSamplerLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CR
EATE_INFO; | |
| 217 dsSamplerLayoutCreateInfo.pNext = nullptr; | |
| 218 dsSamplerLayoutCreateInfo.flags = 0; | |
| 219 dsSamplerLayoutCreateInfo.bindingCount = numSamplers; | |
| 220 // Setting to nullptr fixes an error in the param checker validation layer.
Even though | |
| 221 // bindingCount is 0 (which is valid), it still tries to validate pBindings
unless it is null. | |
| 222 dsSamplerLayoutCreateInfo.pBindings = numSamplers ? dsSamplerBindings.get()
: nullptr; | |
| 223 | |
| 224 GR_VK_CALL_ERRCHECK(fGpu->vkInterface(), | |
| 225 CreateDescriptorSetLayout(fGpu->device(), | |
| 226 &dsSamplerLayoutCreateInfo, | |
| 227 nullptr, | |
| 228 &dsLayout[GrVkUniformHandler::
kSamplerDescSet])); | |
| 229 | |
| 230 // This layout is not owned by the PipelineStateBuilder and thus should no b
e destroyed | |
| 231 dsLayout[GrVkUniformHandler::kUniformBufferDescSet] = fGpu->resourceProvider
().getUniDSLayout(); | |
| 232 | 193 |
| 233 // Create the VkPipelineLayout | 194 // Create the VkPipelineLayout |
| 234 VkPipelineLayoutCreateInfo layoutCreateInfo; | 195 VkPipelineLayoutCreateInfo layoutCreateInfo; |
| 235 memset(&layoutCreateInfo, 0, sizeof(VkPipelineLayoutCreateFlags)); | 196 memset(&layoutCreateInfo, 0, sizeof(VkPipelineLayoutCreateFlags)); |
| 236 layoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; | 197 layoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; |
| 237 layoutCreateInfo.pNext = 0; | 198 layoutCreateInfo.pNext = 0; |
| 238 layoutCreateInfo.flags = 0; | 199 layoutCreateInfo.flags = 0; |
| 239 layoutCreateInfo.setLayoutCount = 2; | 200 layoutCreateInfo.setLayoutCount = 2; |
| 240 layoutCreateInfo.pSetLayouts = dsLayout; | 201 layoutCreateInfo.pSetLayouts = dsLayout; |
| 241 layoutCreateInfo.pushConstantRangeCount = 0; | 202 layoutCreateInfo.pushConstantRangeCount = 0; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 261 fVS, | 222 fVS, |
| 262 &vertShaderModule, | 223 &vertShaderModule, |
| 263 &shaderStageInfo[0])); | 224 &shaderStageInfo[0])); |
| 264 | 225 |
| 265 SkAssertResult(CreateVkShaderModule(fGpu, | 226 SkAssertResult(CreateVkShaderModule(fGpu, |
| 266 VK_SHADER_STAGE_FRAGMENT_BIT, | 227 VK_SHADER_STAGE_FRAGMENT_BIT, |
| 267 fFS, | 228 fFS, |
| 268 &fragShaderModule, | 229 &fragShaderModule, |
| 269 &shaderStageInfo[1])); | 230 &shaderStageInfo[1])); |
| 270 | 231 |
| 271 GrVkResourceProvider& resourceProvider = fGpu->resourceProvider(); | |
| 272 GrVkPipeline* pipeline = resourceProvider.createPipeline(fPipeline, | 232 GrVkPipeline* pipeline = resourceProvider.createPipeline(fPipeline, |
| 273 fPrimProc, | 233 fPrimProc, |
| 274 shaderStageInfo, | 234 shaderStageInfo, |
| 275 2, | 235 2, |
| 276 primitiveType, | 236 primitiveType, |
| 277 renderPass, | 237 renderPass, |
| 278 pipelineLayout); | 238 pipelineLayout); |
| 279 GR_VK_CALL(fGpu->vkInterface(), DestroyShaderModule(fGpu->device(), vertShad
erModule, | 239 GR_VK_CALL(fGpu->vkInterface(), DestroyShaderModule(fGpu->device(), vertShad
erModule, |
| 280 nullptr)); | 240 nullptr)); |
| 281 GR_VK_CALL(fGpu->vkInterface(), DestroyShaderModule(fGpu->device(), fragShad
erModule, | 241 GR_VK_CALL(fGpu->vkInterface(), DestroyShaderModule(fGpu->device(), fragShad
erModule, |
| 282 nullptr)); | 242 nullptr)); |
| 283 | 243 |
| 284 if (!pipeline) { | 244 if (!pipeline) { |
| 285 GR_VK_CALL(fGpu->vkInterface(), DestroyPipelineLayout(fGpu->device(), pi
pelineLayout, | 245 GR_VK_CALL(fGpu->vkInterface(), DestroyPipelineLayout(fGpu->device(), pi
pelineLayout, |
| 286 nullptr)); | 246 nullptr)); |
| 287 GR_VK_CALL(fGpu->vkInterface(), | 247 GR_VK_CALL(fGpu->vkInterface(), |
| 288 DestroyDescriptorSetLayout(fGpu->device(), | 248 DestroyDescriptorSetLayout(fGpu->device(), |
| 289 dsLayout[GrVkUniformHandler::kSamp
lerDescSet], | 249 dsLayout[GrVkUniformHandler::kSamp
lerDescSet], |
| 290 nullptr)); | 250 nullptr)); |
| 291 | 251 |
| 292 this->cleanupFragmentProcessors(); | 252 this->cleanupFragmentProcessors(); |
| 293 return nullptr; | 253 return nullptr; |
| 294 } | 254 } |
| 295 | 255 |
| 296 return new GrVkPipelineState(fGpu, | 256 return new GrVkPipelineState(fGpu, |
| 297 desc, | 257 desc, |
| 298 pipeline, | 258 pipeline, |
| 299 pipelineLayout, | 259 pipelineLayout, |
| 300 dsLayout[GrVkUniformHandler::kSamplerDescSet], | 260 samplerDSHandle, |
| 301 fUniformHandles, | 261 fUniformHandles, |
| 302 fUniformHandler.fUniforms, | 262 fUniformHandler.fUniforms, |
| 303 fUniformHandler.fCurrentVertexUBOOffset, | 263 fUniformHandler.fCurrentVertexUBOOffset, |
| 304 fUniformHandler.fCurrentFragmentUBOOffset, | 264 fUniformHandler.fCurrentFragmentUBOOffset, |
| 305 numSamplers, | 265 (uint32_t)fUniformHandler.numSamplers(), |
| 306 fGeometryProcessor, | 266 fGeometryProcessor, |
| 307 fXferProcessor, | 267 fXferProcessor, |
| 308 fFragmentProcessors); | 268 fFragmentProcessors); |
| 309 } | 269 } |
| 270 |
| OLD | NEW |