Debian Codegraph @

src/gpu/ganesh/vk/GrVkPipelineStateBuilder.cpp

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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
8 #include "src/gpu/ganesh/vk/GrVkPipelineStateBuilder.h"
9
10 #include "include/core/SkData.h"
11 #include "include/core/SkFourByteTag.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkTypes.h"
14 #include "include/gpu/ganesh/GrContextOptions.h"
15 #include "include/gpu/ganesh/GrDirectContext.h"
16 #include "include/private/base/SkTo.h"
17 #include "include/private/gpu/ganesh/GrTypesPriv.h"
18 #include "src/core/SkReadBuffer.h"
19 #include "src/core/SkTraceEvent.h"
20 #include "src/gpu/ganesh/GrAutoLocaleSetter.h"
21 #include "src/gpu/ganesh/GrDirectContextPriv.h"
22 #include "src/gpu/ganesh/GrPersistentCacheUtils.h"
23 #include "src/gpu/ganesh/GrProgramDesc.h"
24 #include "src/gpu/ganesh/GrProgramInfo.h"
25 #include "src/gpu/ganesh/GrShaderVar.h"
26 #include "src/gpu/ganesh/GrThreadSafePipelineBuilder.h"
27 #include "src/gpu/ganesh/GrXferProcessor.h"
28 #include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h"
29 #include "src/gpu/ganesh/glsl/GrGLSLVertexGeoBuilder.h"
30 #include "src/gpu/ganesh/vk/GrVkCaps.h"
31 #include "src/gpu/ganesh/vk/GrVkDescriptorSetManager.h"
32 #include "src/gpu/ganesh/vk/GrVkGpu.h"
33 #include "src/gpu/ganesh/vk/GrVkPipeline.h"
34 #include "src/gpu/ganesh/vk/GrVkPipelineState.h"
35 #include "src/gpu/ganesh/vk/GrVkResourceProvider.h"
36 #include "src/gpu/ganesh/vk/GrVkUtil.h"
37 #include "src/sksl/SkSLProgramSettings.h"
38 #include "src/sksl/codegen/SkSLNativeShader.h"
39 #include "src/utils/SkShaderUtils.h"
40
41 #include <string.h>
42 #include <cstdint>
43 #include <memory>
44 #include <utility>
45
46 class GrCaps;
47
48 GrVkPipelineState* GrVkPipelineStateBuilder::CreatePipelineState(
49 GrVkGpu* gpu,
50 const GrProgramDesc& desc,
51 const GrProgramInfo& programInfo,
52 VkRenderPass compatibleRenderPass,
53 bool overrideSubpassForResolveLoad) {
54
55 GrVkResourceProvider& resourceProvider = gpu->resourceProvider();
56
57 resourceProvider.pipelineStateCache()->stats()->incShaderCompilations();
58
59 // ensure that we use "." as a decimal separator when creating SkSL code
60 GrAutoLocaleSetter als("C");
61
62 // create a builder. This will be handed off to effects so they can use it to add
63 // uniforms, varyings, textures, etc
64 GrVkPipelineStateBuilder builder(gpu, desc, programInfo);
65
66 if (!builder.emitAndInstallProcs()) {
67 return nullptr;
68 }
69
70 return builder.finalize(desc, compatibleRenderPass, overrideSubpassForResolveLoad);
71 }
72
73 GrVkPipelineStateBuilder::GrVkPipelineStateBuilder(GrVkGpu* gpu,
74 const GrProgramDesc& desc,
75 const GrProgramInfo& programInfo)
76 : INHERITED(desc, programInfo)
77 , fGpu(gpu)
78 , fVaryingHandler(this)
79 , fUniformHandler(this) {}
80
81 const GrCaps* GrVkPipelineStateBuilder::caps() const {
82 return fGpu->caps();
83 }
84
85 void GrVkPipelineStateBuilder::finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {
86 outputColor.addLayoutQualifier("location = 0, index = 1");
87 }
88
89 bool GrVkPipelineStateBuilder::createVkShaderModule(VkShaderStageFlagBits stage,
90 const std::string& sksl,
91 VkShaderModule* shaderModule,
92 VkPipelineShaderStageCreateInfo* stageInfo,
93 const SkSL::ProgramSettings& settings,
94 SkSL::NativeShader* outSPIRV,
95 SkSL::Program::Interface* outInterface) {
96 if (!GrCompileVkShaderModule(fGpu, sksl, stage, shaderModule,
97 stageInfo, settings, outSPIRV, outInterface)) {
98 return false;
99 }
100 if (outInterface->fRTFlipUniform != SkSL::Program::Interface::kRTFlip_None) {
101 this->addRTFlipUniform(SKSL_RTFLIP_NAME);
102 }
103 return true;
104 }
105
106 bool GrVkPipelineStateBuilder::installVkShaderModule(VkShaderStageFlagBits stage,
107 const GrGLSLShaderBuilder& builder,
108 VkShaderModule* shaderModule,
109 VkPipelineShaderStageCreateInfo* stageInfo,
110 const SkSL::NativeShader& spirv,
111 SkSL::Program::Interface interface) {
112 if (!GrInstallVkShaderModule(fGpu, spirv, stage, shaderModule, stageInfo)) {
113 return false;
114 }
115 if (interface.fRTFlipUniform != SkSL::Program::Interface::kRTFlip_None) {
116 this->addRTFlipUniform(SKSL_RTFLIP_NAME);
117 }
118 return true;
119 }
120
121 static constexpr SkFourByteTag kSPIRV_Tag = SkSetFourByteTag('S', 'P', 'R', 'V');
122 static constexpr SkFourByteTag kSKSL_Tag = SkSetFourByteTag('S', 'K', 'S', 'L');
123
124 int GrVkPipelineStateBuilder::loadShadersFromCache(SkReadBuffer* cached,
125 VkShaderModule outShaderModules[],
126 VkPipelineShaderStageCreateInfo* outStageInfo) {
127 SkSL::NativeShader shaders[kGrShaderTypeCount];
128 SkSL::Program::Interface interfaces[kGrShaderTypeCount];
129
130 if (!GrPersistentCacheUtils::UnpackCachedShaders(
131 cached, shaders, /*areShadersBinary=*/true, interfaces, kGrShaderTypeCount)) {
132 return 0;
133 }
134
135 bool success = this->installVkShaderModule(VK_SHADER_STAGE_VERTEX_BIT,
136 fVS,
137 &outShaderModules[kVertex_GrShaderType],
138 &outStageInfo[0],
139 shaders[kVertex_GrShaderType],
140 interfaces[kVertex_GrShaderType]);
141
142 success = success && this->installVkShaderModule(VK_SHADER_STAGE_FRAGMENT_BIT,
143 fFS,
144 &outShaderModules[kFragment_GrShaderType],
145 &outStageInfo[1],
146 shaders[kFragment_GrShaderType],
147 interfaces[kFragment_GrShaderType]);
148
149 if (!success) {
150 for (int i = 0; i < kGrShaderTypeCount; ++i) {
151 if (outShaderModules[i]) {
152 GR_VK_CALL(fGpu->vkInterface(),
153 DestroyShaderModule(fGpu->device(), outShaderModules[i], nullptr));
154 }
155 }
156 return 0;
157 }
158 return 2;
159 }
160
161 void GrVkPipelineStateBuilder::storeShadersInCache(const SkSL::NativeShader shaders[],
162 const SkSL::Program::Interface interfaces[]) {
163 // Here we shear off the Vk-specific portion of the Desc in order to create the
164 // persistent key. This is bc Vk only caches the SPIRV code, not the fully compiled
165 // program, and that only depends on the base GrProgramDesc data.
166 // The +4 is to include the kShader_PersistentCacheKeyType code the Vulkan backend adds
167 // to the key right after the base key.
168 sk_sp<SkData> key = SkData::MakeWithoutCopy(this->desc().asKey(),
169 this->desc().initialKeyLength()+4);
170 SkString description = GrProgramDesc::Describe(fProgramInfo, *this->caps());
171
172 sk_sp<SkData> data = GrPersistentCacheUtils::PackCachedShaders(
173 shaders[0].isBinary() ? kSPIRV_Tag : kSKSL_Tag,
174 shaders,
175 interfaces,
176 kGrShaderTypeCount);
177
178 this->gpu()->getContext()->priv().getPersistentCache()->store(*key, *data, description);
179 }
180
181 GrVkPipelineState* GrVkPipelineStateBuilder::finalize(const GrProgramDesc& desc,
182 VkRenderPass compatibleRenderPass,
183 bool overrideSubpassForResolveLoad) {
184 TRACE_EVENT0("skia.shaders", TRACE_FUNC);
185
186 VkDescriptorSetLayout dsLayout[GrVkUniformHandler::kDescSetCount];
187 VkShaderModule shaderModules[kGrShaderTypeCount] = { VK_NULL_HANDLE,
188 VK_NULL_HANDLE };
189
190 GrVkResourceProvider& resourceProvider = fGpu->resourceProvider();
191 // These layouts are not owned by the PipelineStateBuilder and thus should not be destroyed
192 dsLayout[GrVkUniformHandler::kUniformBufferDescSet] = resourceProvider.getUniformDSLayout();
193
194 GrVkDescriptorSetManager::Handle samplerDSHandle;
195 resourceProvider.getSamplerDescriptorSetHandle(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
196 fUniformHandler, &samplerDSHandle);
197 dsLayout[GrVkUniformHandler::kSamplerDescSet] =
198 resourceProvider.getSamplerDSLayout(samplerDSHandle);
199
200 dsLayout[GrVkUniformHandler::kInputDescSet] = resourceProvider.getInputDSLayout();
201
202 this->finalizeShaders();
203
204 bool usePushConstants = fUniformHandler.usePushConstants();
205 VkPipelineShaderStageCreateInfo shaderStageInfo[3];
206 SkSL::ProgramSettings settings;
207 settings.fRTFlipBinding = this->gpu()->vkCaps().getFragmentUniformBinding();
208 settings.fRTFlipSet = this->gpu()->vkCaps().getFragmentUniformSet();
209 settings.fSharpenTextures =
210 this->gpu()->getContext()->priv().options().fSharpenMipmappedTextures;
211 settings.fRTFlipOffset = fUniformHandler.getRTFlipOffset();
212 settings.fUseVulkanPushConstantsForGaneshRTAdjust = usePushConstants;
213 if (fFS.fForceHighPrecision) {
214 settings.fForceHighPrecision = true;
215 }
216 SkASSERT(!this->fragColorIsInOut());
217
218 sk_sp<SkData> cached;
219 SkReadBuffer reader;
220 SkFourByteTag shaderType = 0;
221 auto persistentCache = fGpu->getContext()->priv().getPersistentCache();
222 if (persistentCache) {
223 // Here we shear off the Vk-specific portion of the Desc in order to create the
224 // persistent key. This is bc Vk only caches the SPIRV code, not the fully compiled
225 // program, and that only depends on the base GrProgramDesc data.
226 // The +4 is to include the kShader_PersistentCacheKeyType code the Vulkan backend adds
227 // to the key right after the base key.
228 sk_sp<SkData> key = SkData::MakeWithoutCopy(desc.asKey(), desc.initialKeyLength()+4);
229 cached = persistentCache->load(*key);
230 if (cached) {
231 reader.setMemory(cached->data(), cached->size());
232 shaderType = GrPersistentCacheUtils::GetType(&reader);
233 }
234 }
235
236 int numShaderStages = 0;
237 if (kSPIRV_Tag == shaderType) {
238 numShaderStages = this->loadShadersFromCache(&reader, shaderModules, shaderStageInfo);
239 }
240
241 // Proceed from sources if we didn't get a SPIRV cache (or the cache was invalid)
242 if (!numShaderStages) {
243 numShaderStages = 2; // We always have at least vertex and fragment stages.
244 SkSL::NativeShader shaders[kGrShaderTypeCount];
245 SkSL::Program::Interface interfaces[kGrShaderTypeCount];
246
247 std::string* sksl[kGrShaderTypeCount] = {
248 &fVS.fCompilerString,
249 &fFS.fCompilerString,
250 };
251 SkSL::NativeShader cached_sksl[kGrShaderTypeCount];
252 if (kSKSL_Tag == shaderType) {
253 if (GrPersistentCacheUtils::UnpackCachedShaders(&reader,
254 cached_sksl,
255 /*areShadersBinary=*/false,
256 interfaces,
257 kGrShaderTypeCount)) {
258 for (int i = 0; i < kGrShaderTypeCount; ++i) {
259 sksl[i] = &cached_sksl[i].fText;
260 }
261 }
262 }
263
264 bool success = this->createVkShaderModule(VK_SHADER_STAGE_VERTEX_BIT,
265 *sksl[kVertex_GrShaderType],
266 &shaderModules[kVertex_GrShaderType],
267 &shaderStageInfo[0],
268 settings,
269 &shaders[kVertex_GrShaderType],
270 &interfaces[kVertex_GrShaderType]);
271
272 success = success && this->createVkShaderModule(VK_SHADER_STAGE_FRAGMENT_BIT,
273 *sksl[kFragment_GrShaderType],
274 &shaderModules[kFragment_GrShaderType],
275 &shaderStageInfo[1],
276 settings,
277 &shaders[kFragment_GrShaderType],
278 &interfaces[kFragment_GrShaderType]);
279
280 if (!success) {
281 for (int i = 0; i < kGrShaderTypeCount; ++i) {
282 if (shaderModules[i]) {
283 GR_VK_CALL(fGpu->vkInterface(), DestroyShaderModule(fGpu->device(),
284 shaderModules[i], nullptr));
285 }
286 }
287 return nullptr;
288 }
289
290 if (persistentCache && !cached) {
291 if (fGpu->getContext()->priv().options().fShaderCacheStrategy ==
292 GrContextOptions::ShaderCacheStrategy::kSkSL) {
293 SkSL::NativeShader prettySkSL[kGrShaderTypeCount];
294 for (int i = 0; i < kGrShaderTypeCount; ++i) {
295 prettySkSL[i].fText = SkShaderUtils::PrettyPrint(*sksl[i]);
296 }
297 this->storeShadersInCache(prettySkSL, interfaces);
298 } else {
299 this->storeShadersInCache(shaders, interfaces);
300 }
301 }
302 }
303
304 // The vulkan spec says that if a subpass has an input attachment, then the input attachment
305 // descriptor set must be bound to all pipelines in that subpass. This includes pipelines that
306 // don't actually use the input attachment. Thus we look at the renderPassBarriers and not just
307 // the DstProxyView barrier flags to determine if we use the input attachment.
308 bool usesInput = SkToBool(fProgramInfo.renderPassBarriers() & GrXferBarrierFlags::kTexture);
309 uint32_t layoutCount =
310 usesInput ? GrVkUniformHandler::kDescSetCount : (GrVkUniformHandler::kDescSetCount - 1);
311 // Create the VkPipelineLayout
312 VkPipelineLayoutCreateInfo layoutCreateInfo;
313 memset(&layoutCreateInfo, 0, sizeof(VkPipelineLayoutCreateFlags));
314 layoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
315 layoutCreateInfo.pNext = nullptr;
316 layoutCreateInfo.flags = 0;
317 layoutCreateInfo.setLayoutCount = layoutCount;
318 layoutCreateInfo.pSetLayouts = dsLayout;
319 VkPushConstantRange pushConstantRange = {};
320 if (usePushConstants) {
321 pushConstantRange.stageFlags = fGpu->vkCaps().getPushConstantStageFlags();
322 pushConstantRange.offset = 0;
323 // size must be a multiple of 4
324 SkASSERT(!SkToBool(fUniformHandler.currentOffset() & 0x3));
325 pushConstantRange.size = fUniformHandler.currentOffset();
326 layoutCreateInfo.pushConstantRangeCount = 1;
327 layoutCreateInfo.pPushConstantRanges = &pushConstantRange;
328 } else {
329 layoutCreateInfo.pushConstantRangeCount = 0;
330 layoutCreateInfo.pPushConstantRanges = nullptr;
331 }
332
333 VkPipelineLayout pipelineLayout;
334 VkResult result;
335 GR_VK_CALL_RESULT(fGpu, result, CreatePipelineLayout(fGpu->device(), &layoutCreateInfo, nullptr,
336 &pipelineLayout));
337 if (result != VK_SUCCESS) {
338 return nullptr;
339 }
340
341 // For the vast majority of cases we only have one subpass so we default piplines to subpass 0.
342 // However, if we need to load a resolve into msaa attachment for discardable msaa then the
343 // main subpass will be 1.
344 uint32_t subpass = 0;
345 if (overrideSubpassForResolveLoad ||
346 (fProgramInfo.colorLoadOp() == GrLoadOp::kLoad &&
347 fGpu->vkCaps().programInfoWillUseDiscardableMSAA(fProgramInfo))) {
348 subpass = 1;
349 }
350 sk_sp<const GrVkPipeline> pipeline = resourceProvider.makePipeline(
351 fProgramInfo, shaderStageInfo, numShaderStages, compatibleRenderPass, pipelineLayout,
352 subpass);
353
354 for (int i = 0; i < kGrShaderTypeCount; ++i) {
355 // This if check should not be needed since calling destroy on a VK_NULL_HANDLE is allowed.
356 // However this is causing a crash in certain drivers (e.g. NVidia).
357 if (shaderModules[i]) {
358 GR_VK_CALL(fGpu->vkInterface(), DestroyShaderModule(fGpu->device(), shaderModules[i],
359 nullptr));
360 }
361 }
362
363 if (!pipeline) {
364 GR_VK_CALL(fGpu->vkInterface(), DestroyPipelineLayout(fGpu->device(), pipelineLayout,
365 nullptr));
366 return nullptr;
367 }
368
369 return new GrVkPipelineState(fGpu,
370 std::move(pipeline),
371 samplerDSHandle,
372 fUniformHandles,
373 fUniformHandler.fUniforms,
374 fUniformHandler.currentOffset(),
375 fUniformHandler.usePushConstants(),
376 fUniformHandler.fSamplers,
377 std::move(fGPImpl),
378 std::move(fXPImpl),
379 std::move(fFPImpls));
380 }