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/GrVkGpu.h" | 10 #include "vk/GrVkGpu.h" |
11 #include "vk/GrVkRenderPass.h" | 11 #include "vk/GrVkRenderPass.h" |
| 12 #if USE_SKSL |
| 13 #include "SkSLCompiler.h" |
| 14 #endif |
12 | 15 |
13 GrVkPipelineState* GrVkPipelineStateBuilder::CreatePipelineState( | 16 GrVkPipelineState* GrVkPipelineStateBuilder::CreatePipelineState( |
14 GrVkGpu* gpu, | 17 GrVkGpu* gpu, |
15 const GrPipeline&
pipeline, | 18 const GrPipeline&
pipeline, |
16 const GrPrimitive
Processor& primProc, | 19 const GrPrimitive
Processor& primProc, |
17 GrPrimitiveType p
rimitiveType, | 20 GrPrimitiveType p
rimitiveType, |
18 const GrVkPipelin
eState::Desc& desc, | 21 const GrVkPipelin
eState::Desc& desc, |
19 const GrVkRenderP
ass& renderPass) { | 22 const GrVkRenderP
ass& renderPass) { |
20 // create a builder. This will be handed off to effects so they can use it
to add | 23 // create a builder. This will be handed off to effects so they can use it
to add |
21 // uniforms, varyings, textures, etc | 24 // uniforms, varyings, textures, etc |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 } | 68 } |
66 if (visibility & kGeometry_GrShaderFlag) { | 69 if (visibility & kGeometry_GrShaderFlag) { |
67 flags |= VK_SHADER_STAGE_GEOMETRY_BIT; | 70 flags |= VK_SHADER_STAGE_GEOMETRY_BIT; |
68 } | 71 } |
69 if (visibility & kFragment_GrShaderFlag) { | 72 if (visibility & kFragment_GrShaderFlag) { |
70 flags |= VK_SHADER_STAGE_FRAGMENT_BIT; | 73 flags |= VK_SHADER_STAGE_FRAGMENT_BIT; |
71 } | 74 } |
72 return flags; | 75 return flags; |
73 } | 76 } |
74 | 77 |
| 78 #if USE_SKSL |
| 79 SkSL::Program::Kind vk_shader_stage_to_skiasl_kind(VkShaderStageFlagBits stage)
{ |
| 80 if (VK_SHADER_STAGE_VERTEX_BIT == stage) { |
| 81 return SkSL::Program::kVertex_Kind; |
| 82 } |
| 83 SkASSERT(VK_SHADER_STAGE_FRAGMENT_BIT == stage); |
| 84 return SkSL::Program::kFragment_Kind; |
| 85 } |
| 86 #else |
75 shaderc_shader_kind vk_shader_stage_to_shaderc_kind(VkShaderStageFlagBits stage)
{ | 87 shaderc_shader_kind vk_shader_stage_to_shaderc_kind(VkShaderStageFlagBits stage)
{ |
76 if (VK_SHADER_STAGE_VERTEX_BIT == stage) { | 88 if (VK_SHADER_STAGE_VERTEX_BIT == stage) { |
77 return shaderc_glsl_vertex_shader; | 89 return shaderc_glsl_vertex_shader; |
78 } | 90 } |
79 SkASSERT(VK_SHADER_STAGE_FRAGMENT_BIT == stage); | 91 SkASSERT(VK_SHADER_STAGE_FRAGMENT_BIT == stage); |
80 return shaderc_glsl_fragment_shader; | 92 return shaderc_glsl_fragment_shader; |
81 } | 93 } |
| 94 #endif |
82 | 95 |
| 96 #include <fstream> |
| 97 #include <sstream> |
83 bool GrVkPipelineStateBuilder::CreateVkShaderModule(const GrVkGpu* gpu, | 98 bool GrVkPipelineStateBuilder::CreateVkShaderModule(const GrVkGpu* gpu, |
84 VkShaderStageFlagBits stage, | 99 VkShaderStageFlagBits stage, |
85 const GrGLSLShaderBuilder& b
uilder, | 100 const GrGLSLShaderBuilder& b
uilder, |
86 VkShaderModule* shaderModule
, | 101 VkShaderModule* shaderModule
, |
87 VkPipelineShaderStageCreateI
nfo* stageInfo) { | 102 VkPipelineShaderStageCreateI
nfo* stageInfo) { |
88 SkString shaderString; | 103 SkString shaderString; |
89 for (int i = 0; i < builder.fCompilerStrings.count(); ++i) { | 104 for (int i = 0; i < builder.fCompilerStrings.count(); ++i) { |
90 if (builder.fCompilerStrings[i]) { | 105 if (builder.fCompilerStrings[i]) { |
91 shaderString.append(builder.fCompilerStrings[i]); | 106 shaderString.append(builder.fCompilerStrings[i]); |
92 shaderString.append("\n"); | 107 shaderString.append("\n"); |
93 } | 108 } |
94 } | 109 } |
95 | 110 |
96 VkShaderModuleCreateInfo moduleCreateInfo; | 111 VkShaderModuleCreateInfo moduleCreateInfo; |
97 memset(&moduleCreateInfo, 0, sizeof(VkShaderModuleCreateInfo)); | 112 memset(&moduleCreateInfo, 0, sizeof(VkShaderModuleCreateInfo)); |
98 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; | 113 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; |
99 moduleCreateInfo.pNext = nullptr; | 114 moduleCreateInfo.pNext = nullptr; |
100 moduleCreateInfo.flags = 0; | 115 moduleCreateInfo.flags = 0; |
101 | 116 |
| 117 #if USE_SKSL |
| 118 std::string code; |
| 119 #else |
102 shaderc_compilation_result_t result = nullptr; | 120 shaderc_compilation_result_t result = nullptr; |
| 121 #endif |
103 | 122 |
104 if (gpu->vkCaps().canUseGLSLForShaderModule()) { | 123 if (gpu->vkCaps().canUseGLSLForShaderModule()) { |
105 moduleCreateInfo.codeSize = strlen(shaderString.c_str()); | 124 moduleCreateInfo.codeSize = strlen(shaderString.c_str()); |
106 moduleCreateInfo.pCode = (const uint32_t*)shaderString.c_str(); | 125 moduleCreateInfo.pCode = (const uint32_t*)shaderString.c_str(); |
107 } else { | 126 } else { |
108 | 127 |
| 128 #if USE_SKSL |
| 129 bool result = gpu->shaderCompiler()->toSPIRV(vk_shader_stage_to_skiasl_k
ind(stage), |
| 130 std::string(shaderString.c_
str()), |
| 131 &code); |
| 132 if (!result) { |
| 133 SkDebugf("%s\n", gpu->shaderCompiler()->errorText().c_str()); |
| 134 return false; |
| 135 } |
| 136 moduleCreateInfo.codeSize = code.size(); |
| 137 moduleCreateInfo.pCode = (const uint32_t*) code.c_str(); |
| 138 #else |
109 shaderc_compiler_t compiler = gpu->shadercCompiler(); | 139 shaderc_compiler_t compiler = gpu->shadercCompiler(); |
110 | 140 |
111 shaderc_compile_options_t options = shaderc_compile_options_initialize()
; | 141 shaderc_compile_options_t options = shaderc_compile_options_initialize()
; |
112 | 142 |
113 shaderc_shader_kind shadercStage = vk_shader_stage_to_shaderc_kind(stage
); | 143 shaderc_shader_kind shadercStage = vk_shader_stage_to_shaderc_kind(stage
); |
114 result = shaderc_compile_into_spv(compiler, | 144 result = shaderc_compile_into_spv(compiler, |
115 shaderString.c_str(), | 145 shaderString.c_str(), |
116 strlen(shaderString.c_str()), | 146 strlen(shaderString.c_str()), |
117 shadercStage, | 147 shadercStage, |
118 "shader", | 148 "shader", |
119 "main", | 149 "main", |
120 options); | 150 options); |
121 shaderc_compile_options_release(options); | 151 shaderc_compile_options_release(options); |
122 #ifdef SK_DEBUG | 152 #ifdef SK_DEBUG |
123 if (shaderc_result_get_num_errors(result)) { | 153 if (shaderc_result_get_num_errors(result)) { |
124 SkDebugf("%s\n", shaderString.c_str()); | 154 SkDebugf("%s\n", shaderString.c_str()); |
125 SkDebugf("%s\n", shaderc_result_get_error_message(result)); | 155 SkDebugf("%s\n", shaderc_result_get_error_message(result)); |
126 return false; | 156 return false; |
127 } | 157 } |
128 #endif | 158 #endif // SK_DEBUG |
129 | 159 |
130 moduleCreateInfo.codeSize = shaderc_result_get_length(result); | 160 moduleCreateInfo.codeSize = shaderc_result_get_length(result); |
131 moduleCreateInfo.pCode = (const uint32_t*)shaderc_result_get_bytes(resul
t); | 161 moduleCreateInfo.pCode = (const uint32_t*)shaderc_result_get_bytes(resul
t); |
| 162 #endif // USE_SKSL |
132 } | 163 } |
133 | 164 |
134 VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateShaderModule(gpu->device
(), | 165 VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateShaderModule(gpu->device
(), |
135 &moduleCrea
teInfo, | 166 &moduleCrea
teInfo, |
136 nullptr, | 167 nullptr, |
137 shaderModul
e)); | 168 shaderModul
e)); |
| 169 |
138 if (!gpu->vkCaps().canUseGLSLForShaderModule()) { | 170 if (!gpu->vkCaps().canUseGLSLForShaderModule()) { |
| 171 #if !USE_SKSL |
139 shaderc_result_release(result); | 172 shaderc_result_release(result); |
| 173 #endif |
140 } | 174 } |
141 if (err) { | 175 if (err) { |
142 return false; | 176 return false; |
143 } | 177 } |
144 | 178 |
145 memset(stageInfo, 0, sizeof(VkPipelineShaderStageCreateInfo)); | 179 memset(stageInfo, 0, sizeof(VkPipelineShaderStageCreateInfo)); |
146 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; | 180 stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
147 stageInfo->pNext = nullptr; | 181 stageInfo->pNext = nullptr; |
148 stageInfo->flags = 0; | 182 stageInfo->flags = 0; |
149 stageInfo->stage = stage; | 183 stageInfo->stage = stage; |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 dsLayout[GrVkUniformHandler::kSamplerDescSet], | 300 dsLayout[GrVkUniformHandler::kSamplerDescSet], |
267 fUniformHandles, | 301 fUniformHandles, |
268 fUniformHandler.fUniforms, | 302 fUniformHandler.fUniforms, |
269 fUniformHandler.fCurrentVertexUBOOffset, | 303 fUniformHandler.fCurrentVertexUBOOffset, |
270 fUniformHandler.fCurrentFragmentUBOOffset, | 304 fUniformHandler.fCurrentFragmentUBOOffset, |
271 numSamplers, | 305 numSamplers, |
272 fGeometryProcessor, | 306 fGeometryProcessor, |
273 fXferProcessor, | 307 fXferProcessor, |
274 fFragmentProcessors); | 308 fFragmentProcessors); |
275 } | 309 } |
OLD | NEW |