| 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 |
outShaderModules[i] = VK_NULL_HANDLE; |
| 155 |
} |
| 156 |
} |
| 157 |
return 0; |
| 158 |
} |
| 159 |
return 2; |
| 160 |
} |
| 161 |
|
| 162 |
void GrVkPipelineStateBuilder::storeShadersInCache(const SkSL::NativeShader shaders[], |
| 163 |
const SkSL::Program::Interface interfaces[]) { |
| 164 |
// Here we shear off the Vk-specific portion of the Desc in order to create the |
| 165 |
// persistent key. This is bc Vk only caches the SPIRV code, not the fully compiled |
| 166 |
// program, and that only depends on the base GrProgramDesc data. |
| 167 |
// The +4 is to include the kShader_PersistentCacheKeyType code the Vulkan backend adds |
| 168 |
// to the key right after the base key. |
| 169 |
sk_sp<SkData> key = SkData::MakeWithoutCopy(this->desc().asKey(), |
| 170 |
this->desc().initialKeyLength()+4); |
| 171 |
SkString description = GrProgramDesc::Describe(fProgramInfo, *this->caps()); |
| 172 |
|
| 173 |
sk_sp<SkData> data = GrPersistentCacheUtils::PackCachedShaders( |
| 174 |
shaders[0].isBinary() ? kSPIRV_Tag : kSKSL_Tag, |
| 175 |
shaders, |
| 176 |
interfaces, |
| 177 |
kGrShaderTypeCount); |
| 178 |
|
| 179 |
this->gpu()->getContext()->priv().getPersistentCache()->store(*key, *data, description); |
| 180 |
} |
| 181 |
|
| 182 |
GrVkPipelineState* GrVkPipelineStateBuilder::finalize(const GrProgramDesc& desc, |
| 183 |
VkRenderPass compatibleRenderPass, |
| 184 |
bool overrideSubpassForResolveLoad) { |
| 185 |
TRACE_EVENT0("skia.shaders", TRACE_FUNC); |
| 186 |
|
| 187 |
VkDescriptorSetLayout dsLayout[GrVkUniformHandler::kDescSetCount]; |
| 188 |
VkShaderModule shaderModules[kGrShaderTypeCount] = { VK_NULL_HANDLE, |
| 189 |
VK_NULL_HANDLE }; |
| 190 |
|
| 191 |
GrVkResourceProvider& resourceProvider = fGpu->resourceProvider(); |
| 192 |
// These layouts are not owned by the PipelineStateBuilder and thus should not be destroyed |
| 193 |
dsLayout[GrVkUniformHandler::kUniformBufferDescSet] = resourceProvider.getUniformDSLayout(); |
| 194 |
|
| 195 |
GrVkDescriptorSetManager::Handle samplerDSHandle; |
| 196 |
resourceProvider.getSamplerDescriptorSetHandle(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
| 197 |
fUniformHandler, &samplerDSHandle); |
| 198 |
dsLayout[GrVkUniformHandler::kSamplerDescSet] = |
| 199 |
resourceProvider.getSamplerDSLayout(samplerDSHandle); |
| 200 |
|
| 201 |
dsLayout[GrVkUniformHandler::kInputDescSet] = resourceProvider.getInputDSLayout(); |
| 202 |
|
| 203 |
this->finalizeShaders(); |
| 204 |
|
| 205 |
bool usePushConstants = fUniformHandler.usePushConstants(); |
| 206 |
VkPipelineShaderStageCreateInfo shaderStageInfo[3]; |
| 207 |
SkSL::ProgramSettings settings; |
| 208 |
settings.fRTFlipBinding = this->gpu()->vkCaps().getFragmentUniformBinding(); |
| 209 |
settings.fRTFlipSet = this->gpu()->vkCaps().getFragmentUniformSet(); |
| 210 |
settings.fSharpenTextures = |
| 211 |
this->gpu()->getContext()->priv().options().fSharpenMipmappedTextures; |
| 212 |
settings.fRTFlipOffset = fUniformHandler.getRTFlipOffset(); |
| 213 |
settings.fUseVulkanPushConstantsForGaneshRTAdjust = usePushConstants; |
| 214 |
if (fFS.fForceHighPrecision) { |
| 215 |
settings.fForceHighPrecision = true; |
| 216 |
} |
| 217 |
SkASSERT(!this->fragColorIsInOut()); |
| 218 |
|
| 219 |
sk_sp<SkData> cached; |
| 220 |
SkReadBuffer reader; |
| 221 |
SkFourByteTag shaderType = 0; |
| 222 |
auto persistentCache = fGpu->getContext()->priv().getPersistentCache(); |
| 223 |
if (persistentCache) { |
| 224 |
// Here we shear off the Vk-specific portion of the Desc in order to create the |
| 225 |
// persistent key. This is bc Vk only caches the SPIRV code, not the fully compiled |
| 226 |
// program, and that only depends on the base GrProgramDesc data. |
| 227 |
// The +4 is to include the kShader_PersistentCacheKeyType code the Vulkan backend adds |
| 228 |
// to the key right after the base key. |
| 229 |
sk_sp<SkData> key = SkData::MakeWithoutCopy(desc.asKey(), desc.initialKeyLength()+4); |
| 230 |
cached = persistentCache->load(*key); |
| 231 |
if (cached) { |
| 232 |
reader.setMemory(cached->data(), cached->size()); |
| 233 |
shaderType = GrPersistentCacheUtils::GetType(&reader); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
int numShaderStages = 0; |
| 238 |
if (kSPIRV_Tag == shaderType) { |
| 239 |
numShaderStages = this->loadShadersFromCache(&reader, shaderModules, shaderStageInfo); |
| 240 |
} |
| 241 |
|
| 242 |
// Proceed from sources if we didn't get a SPIRV cache (or the cache was invalid) |
| 243 |
if (!numShaderStages) { |
| 244 |
numShaderStages = 2; // We always have at least vertex and fragment stages. |
| 245 |
SkSL::NativeShader shaders[kGrShaderTypeCount]; |
| 246 |
SkSL::Program::Interface interfaces[kGrShaderTypeCount]; |
| 247 |
|
| 248 |
std::string* sksl[kGrShaderTypeCount] = { |
| 249 |
&fVS.fCompilerString, |
| 250 |
&fFS.fCompilerString, |
| 251 |
}; |
| 252 |
SkSL::NativeShader cached_sksl[kGrShaderTypeCount]; |
| 253 |
if (kSKSL_Tag == shaderType) { |
| 254 |
if (GrPersistentCacheUtils::UnpackCachedShaders(&reader, |
| 255 |
cached_sksl, |
| 256 |
/*areShadersBinary=*/false, |
| 257 |
interfaces, |
| 258 |
kGrShaderTypeCount)) { |
| 259 |
for (int i = 0; i < kGrShaderTypeCount; ++i) { |
| 260 |
sksl[i] = &cached_sksl[i].fText; |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
bool success = this->createVkShaderModule(VK_SHADER_STAGE_VERTEX_BIT, |
| 266 |
*sksl[kVertex_GrShaderType], |
| 267 |
&shaderModules[kVertex_GrShaderType], |
| 268 |
&shaderStageInfo[0], |
| 269 |
settings, |
| 270 |
&shaders[kVertex_GrShaderType], |
| 271 |
&interfaces[kVertex_GrShaderType]); |
| 272 |
|
| 273 |
success = success && this->createVkShaderModule(VK_SHADER_STAGE_FRAGMENT_BIT, |
| 274 |
*sksl[kFragment_GrShaderType], |
| 275 |
&shaderModules[kFragment_GrShaderType], |
| 276 |
&shaderStageInfo[1], |
| 277 |
settings, |
| 278 |
&shaders[kFragment_GrShaderType], |
| 279 |
&interfaces[kFragment_GrShaderType]); |
| 280 |
|
| 281 |
if (!success) { |
| 282 |
for (int i = 0; i < kGrShaderTypeCount; ++i) { |
| 283 |
if (shaderModules[i]) { |
| 284 |
GR_VK_CALL(fGpu->vkInterface(), DestroyShaderModule(fGpu->device(), |
| 285 |
shaderModules[i], nullptr)); |
| •286 |
shaderModules[i] = VK_NULL_HANDLE; |
| 287 |
} |
| 288 |
} |
| 289 |
return nullptr; |
| 290 |
} |
| 291 |
|
| 292 |
if (persistentCache && !cached) { |
| 293 |
if (fGpu->getContext()->priv().options().fShaderCacheStrategy == |
| 294 |
GrContextOptions::ShaderCacheStrategy::kSkSL) { |
| 295 |
SkSL::NativeShader prettySkSL[kGrShaderTypeCount]; |
| 296 |
for (int i = 0; i < kGrShaderTypeCount; ++i) { |
| 297 |
prettySkSL[i].fText = SkShaderUtils::PrettyPrint(*sksl[i]); |
| 298 |
} |
| 299 |
this->storeShadersInCache(prettySkSL, interfaces); |
| 300 |
} else { |
| 301 |
this->storeShadersInCache(shaders, interfaces); |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
// The vulkan spec says that if a subpass has an input attachment, then the input attachment |
| 307 |
// descriptor set must be bound to all pipelines in that subpass. This includes pipelines that |
| 308 |
// don't actually use the input attachment. Thus we look at the renderPassBarriers and not just |
| 309 |
// the DstProxyView barrier flags to determine if we use the input attachment. |
| 310 |
bool usesInput = SkToBool(fProgramInfo.renderPassBarriers() & GrXferBarrierFlags::kTexture); |
| 311 |
uint32_t layoutCount = |
| 312 |
usesInput ? GrVkUniformHandler::kDescSetCount : (GrVkUniformHandler::kDescSetCount - 1); |
| 313 |
// Create the VkPipelineLayout |
| 314 |
VkPipelineLayoutCreateInfo layoutCreateInfo; |
| 315 |
memset(&layoutCreateInfo, 0, sizeof(VkPipelineLayoutCreateFlags)); |
| 316 |
layoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; |
| 317 |
layoutCreateInfo.pNext = nullptr; |
| 318 |
layoutCreateInfo.flags = 0; |
| 319 |
layoutCreateInfo.setLayoutCount = layoutCount; |
| 320 |
layoutCreateInfo.pSetLayouts = dsLayout; |
| 321 |
VkPushConstantRange pushConstantRange = {}; |
| 322 |
if (usePushConstants) { |
| 323 |
pushConstantRange.stageFlags = fGpu->vkCaps().getPushConstantStageFlags(); |
| 324 |
pushConstantRange.offset = 0; |
| 325 |
// size must be a multiple of 4 |
| 326 |
SkASSERT(!SkToBool(fUniformHandler.currentOffset() & 0x3)); |
| 327 |
pushConstantRange.size = fUniformHandler.currentOffset(); |
| 328 |
layoutCreateInfo.pushConstantRangeCount = 1; |
| 329 |
layoutCreateInfo.pPushConstantRanges = &pushConstantRange; |
| 330 |
} else { |
| 331 |
layoutCreateInfo.pushConstantRangeCount = 0; |
| 332 |
layoutCreateInfo.pPushConstantRanges = nullptr; |
| 333 |
} |
| 334 |
|
| 335 |
VkPipelineLayout pipelineLayout; |
| 336 |
VkResult result; |
| 337 |
GR_VK_CALL_RESULT(fGpu, result, CreatePipelineLayout(fGpu->device(), &layoutCreateInfo, nullptr, |
| 338 |
&pipelineLayout)); |
| 339 |
if (result != VK_SUCCESS) { |
| 340 |
return nullptr; |
| 341 |
} |
| 342 |
|
| 343 |
// For the vast majority of cases we only have one subpass so we default piplines to subpass 0. |
| 344 |
// However, if we need to load a resolve into msaa attachment for discardable msaa then the |
| 345 |
// main subpass will be 1. |
| 346 |
uint32_t subpass = 0; |
| 347 |
if (overrideSubpassForResolveLoad || |
| 348 |
(fProgramInfo.colorLoadOp() == GrLoadOp::kLoad && |
| 349 |
fGpu->vkCaps().programInfoWillUseDiscardableMSAA(fProgramInfo))) { |
| 350 |
subpass = 1; |
| 351 |
} |
| 352 |
sk_sp<const GrVkPipeline> pipeline = resourceProvider.makePipeline( |
| 353 |
fProgramInfo, shaderStageInfo, numShaderStages, compatibleRenderPass, pipelineLayout, |
| 354 |
subpass); |
| 355 |
|
| 356 |
for (int i = 0; i < kGrShaderTypeCount; ++i) { |
| 357 |
// This if check should not be needed since calling destroy on a VK_NULL_HANDLE is allowed. |
| 358 |
// However this is causing a crash in certain drivers (e.g. NVidia). |
| 359 |
if (shaderModules[i]) { |
| 360 |
GR_VK_CALL(fGpu->vkInterface(), DestroyShaderModule(fGpu->device(), shaderModules[i], |
| 361 |
nullptr)); |
| •362 |
shaderModules[i] = VK_NULL_HANDLE; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
if (!pipeline) { |
| 367 |
GR_VK_CALL(fGpu->vkInterface(), DestroyPipelineLayout(fGpu->device(), pipelineLayout, |
| 368 |
nullptr)); |
| 369 |
return nullptr; |
| 370 |
} |
| 371 |
|
| 372 |
return new GrVkPipelineState(fGpu, |
| 373 |
std::move(pipeline), |
| 374 |
samplerDSHandle, |
| 375 |
fUniformHandles, |
| 376 |
fUniformHandler.fUniforms, |
| 377 |
fUniformHandler.currentOffset(), |
| 378 |
fUniformHandler.usePushConstants(), |
| 379 |
fUniformHandler.fSamplers, |
| 380 |
std::move(fGPImpl), |
| 381 |
std::move(fXPImpl), |
| 382 |
std::move(fFPImpls)); |
| 383 |
} |