| 1 |
/* |
| 2 |
* Copyright 2023 Google LLC |
| 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/graphite/vk/VulkanGraphicsPipeline.h" |
| 9 |
|
| 10 |
#include "include/gpu/ShaderErrorHandler.h" |
| 11 |
#include "include/gpu/graphite/TextureInfo.h" |
| 12 |
#include "src/core/SkSLTypeShared.h" |
| 13 |
#include "src/core/SkTraceEvent.h" |
| 14 |
#include "src/gpu/SkSLToBackend.h" |
| 15 |
#include "src/gpu/graphite/Attribute.h" |
| 16 |
#include "src/gpu/graphite/ContextUtils.h" |
| 17 |
#include "src/gpu/graphite/GraphicsPipelineDesc.h" |
| 18 |
#include "src/gpu/graphite/Log.h" |
| 19 |
#include "src/gpu/graphite/RenderPassDesc.h" |
| 20 |
#include "src/gpu/graphite/RendererProvider.h" |
| 21 |
#include "src/gpu/graphite/ResourceTypes.h" |
| 22 |
#include "src/gpu/graphite/RuntimeEffectDictionary.h" |
| 23 |
#include "src/gpu/graphite/ShaderInfo.h" |
| 24 |
#include "src/gpu/graphite/vk/VulkanCaps.h" |
| 25 |
#include "src/gpu/graphite/vk/VulkanGraphicsPipeline.h" |
| 26 |
#include "src/gpu/graphite/vk/VulkanRenderPass.h" |
| 27 |
#include "src/gpu/graphite/vk/VulkanResourceProvider.h" |
| 28 |
#include "src/gpu/graphite/vk/VulkanSharedContext.h" |
| 29 |
#include "src/gpu/graphite/vk/VulkanSpirvTransforms.h" |
| 30 |
#include "src/gpu/vk/VulkanUtilsPriv.h" |
| 31 |
#include "src/sksl/SkSLProgramKind.h" |
| 32 |
#include "src/sksl/SkSLProgramSettings.h" |
| 33 |
#include "src/sksl/ir/SkSLProgram.h" |
| 34 |
#include "src/utils/SkShaderUtils.h" |
| 35 |
|
| 36 |
namespace skgpu::graphite { |
| 37 |
|
| 38 |
static inline VkFormat attrib_type_to_vkformat(VertexAttribType type) { |
| 39 |
switch (type) { |
| 40 |
case VertexAttribType::kFloat: |
| 41 |
return VK_FORMAT_R32_SFLOAT; |
| 42 |
case VertexAttribType::kFloat2: |
| 43 |
return VK_FORMAT_R32G32_SFLOAT; |
| 44 |
case VertexAttribType::kFloat3: |
| 45 |
return VK_FORMAT_R32G32B32_SFLOAT; |
| 46 |
case VertexAttribType::kFloat4: |
| 47 |
return VK_FORMAT_R32G32B32A32_SFLOAT; |
| 48 |
case VertexAttribType::kHalf: |
| 49 |
return VK_FORMAT_R16_SFLOAT; |
| 50 |
case VertexAttribType::kHalf2: |
| 51 |
return VK_FORMAT_R16G16_SFLOAT; |
| 52 |
case VertexAttribType::kHalf4: |
| 53 |
return VK_FORMAT_R16G16B16A16_SFLOAT; |
| 54 |
case VertexAttribType::kInt2: |
| 55 |
return VK_FORMAT_R32G32_SINT; |
| 56 |
case VertexAttribType::kInt3: |
| 57 |
return VK_FORMAT_R32G32B32_SINT; |
| 58 |
case VertexAttribType::kInt4: |
| 59 |
return VK_FORMAT_R32G32B32A32_SINT; |
| 60 |
case VertexAttribType::kUInt2: |
| 61 |
return VK_FORMAT_R32G32_UINT; |
| 62 |
case VertexAttribType::kByte: |
| 63 |
return VK_FORMAT_R8_SINT; |
| 64 |
case VertexAttribType::kByte2: |
| 65 |
return VK_FORMAT_R8G8_SINT; |
| 66 |
case VertexAttribType::kByte4: |
| 67 |
return VK_FORMAT_R8G8B8A8_SINT; |
| 68 |
case VertexAttribType::kUByte: |
| 69 |
return VK_FORMAT_R8_UINT; |
| 70 |
case VertexAttribType::kUByte2: |
| 71 |
return VK_FORMAT_R8G8_UINT; |
| 72 |
case VertexAttribType::kUByte4: |
| 73 |
return VK_FORMAT_R8G8B8A8_UINT; |
| 74 |
case VertexAttribType::kUByte_norm: |
| 75 |
return VK_FORMAT_R8_UNORM; |
| 76 |
case VertexAttribType::kUByte4_norm: |
| 77 |
return VK_FORMAT_R8G8B8A8_UNORM; |
| 78 |
case VertexAttribType::kShort2: |
| 79 |
return VK_FORMAT_R16G16_SINT; |
| 80 |
case VertexAttribType::kShort4: |
| 81 |
return VK_FORMAT_R16G16B16A16_SINT; |
| 82 |
case VertexAttribType::kUShort2: |
| 83 |
return VK_FORMAT_R16G16_UINT; |
| 84 |
case VertexAttribType::kUShort2_norm: |
| 85 |
return VK_FORMAT_R16G16_UNORM; |
| 86 |
case VertexAttribType::kInt: |
| 87 |
return VK_FORMAT_R32_SINT; |
| 88 |
case VertexAttribType::kUInt: |
| 89 |
return VK_FORMAT_R32_UINT; |
| 90 |
case VertexAttribType::kUShort_norm: |
| 91 |
return VK_FORMAT_R16_UNORM; |
| 92 |
case VertexAttribType::kUShort4_norm: |
| 93 |
return VK_FORMAT_R16G16B16A16_UNORM; |
| 94 |
} |
| 95 |
SK_ABORT("Unknown vertex attrib type"); |
| 96 |
} |
| 97 |
|
| 98 |
// A helper to derive the vertex input state either for version 1 or 2 of the Vulkan structs. |
| 99 |
template <typename VertexInputBindingDescription, typename VertexInputAttributeDescription> |
| 100 |
static void get_vertex_input_state( |
| 101 |
VkVertexInputRate appendVertexRate, |
| 102 |
const SkSpan<const Attribute>& staticAttrs, |
| 103 |
const SkSpan<const Attribute>& appendAttrs, |
| 104 |
skia_private::STArray<2, VertexInputBindingDescription>& bindingDescs, |
| 105 |
skia_private::STArray<16, VertexInputAttributeDescription>& attributeDescs, |
| 106 |
const VertexInputBindingDescription& defaultBindingDesc, |
| 107 |
const VertexInputAttributeDescription& defaultAttributeDesc) { |
| 108 |
int attribIndex = 0; |
| 109 |
size_t staticAttributeOffset = 0; |
| 110 |
for (auto attrib : staticAttrs) { |
| 111 |
VertexInputAttributeDescription vkAttrib = defaultAttributeDesc; |
| 112 |
vkAttrib.location = attribIndex++; |
| 113 |
vkAttrib.binding = VulkanGraphicsPipeline::kStaticDataBufferIndex; |
| 114 |
vkAttrib.format = attrib_type_to_vkformat(attrib.cpuType()); |
| 115 |
vkAttrib.offset = staticAttributeOffset; |
| 116 |
staticAttributeOffset += attrib.sizeAlign4(); |
| 117 |
attributeDescs.push_back(vkAttrib); |
| 118 |
} |
| 119 |
|
| 120 |
size_t appendAttributeOffset = 0; |
| 121 |
for (auto attrib : appendAttrs) { |
| 122 |
VertexInputAttributeDescription vkAttrib = defaultAttributeDesc; |
| 123 |
vkAttrib.location = attribIndex++; |
| 124 |
vkAttrib.binding = VulkanGraphicsPipeline::kAppendDataBufferIndex; |
| 125 |
vkAttrib.format = attrib_type_to_vkformat(attrib.cpuType()); |
| 126 |
vkAttrib.offset = appendAttributeOffset; |
| 127 |
appendAttributeOffset += attrib.sizeAlign4(); |
| 128 |
attributeDescs.push_back(vkAttrib); |
| 129 |
} |
| 130 |
|
| 131 |
if (!staticAttrs.empty()) { |
| 132 |
VertexInputBindingDescription vkBinding = defaultBindingDesc; |
| 133 |
vkBinding.binding = VulkanGraphicsPipeline::kStaticDataBufferIndex; |
| 134 |
vkBinding.stride = (uint32_t)staticAttributeOffset; |
| 135 |
vkBinding.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; |
| 136 |
bindingDescs.push_back(vkBinding); |
| 137 |
} |
| 138 |
if (!appendAttrs.empty()) { |
| 139 |
VertexInputBindingDescription vkBinding = defaultBindingDesc; |
| 140 |
vkBinding.binding = VulkanGraphicsPipeline::kAppendDataBufferIndex; |
| 141 |
vkBinding.stride = (uint32_t)appendAttributeOffset; |
| 142 |
vkBinding.inputRate = appendVertexRate; |
| 143 |
bindingDescs.push_back(vkBinding); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
static void setup_vertex_input_state( |
| 148 |
VkVertexInputRate appendVertexRate, |
| 149 |
const SkSpan<const Attribute>& staticAttrs, |
| 150 |
const SkSpan<const Attribute>& appendAttrs, |
| 151 |
VkPipelineVertexInputStateCreateInfo* vertexInputInfo, |
| 152 |
skia_private::STArray<2, VkVertexInputBindingDescription>& bindingDescs, |
| 153 |
skia_private::STArray<16, VkVertexInputAttributeDescription>& attributeDescs) { |
| 154 |
// Setup attribute & binding descriptions |
| 155 |
get_vertex_input_state(appendVertexRate, |
| 156 |
staticAttrs, |
| 157 |
appendAttrs, |
| 158 |
bindingDescs, |
| 159 |
attributeDescs, |
| 160 |
/*defaultBindingDesc=*/{}, |
| 161 |
/*defaultAttributeDesc=*/{}); |
| 162 |
|
| 163 |
*vertexInputInfo = {}; |
| 164 |
vertexInputInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; |
| 165 |
vertexInputInfo->vertexBindingDescriptionCount = bindingDescs.size(); |
| 166 |
vertexInputInfo->pVertexBindingDescriptions = bindingDescs.begin(); |
| 167 |
vertexInputInfo->vertexAttributeDescriptionCount = attributeDescs.size(); |
| 168 |
vertexInputInfo->pVertexAttributeDescriptions = attributeDescs.begin(); |
| 169 |
} |
| 170 |
|
| 171 |
static VkPrimitiveTopology primitive_type_to_vk_topology(PrimitiveType primitiveType) { |
| 172 |
switch (primitiveType) { |
| 173 |
case PrimitiveType::kTriangles: |
| 174 |
return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; |
| 175 |
case PrimitiveType::kTriangleStrip: |
| 176 |
return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP; |
| 177 |
case PrimitiveType::kPoints: |
| 178 |
return VK_PRIMITIVE_TOPOLOGY_POINT_LIST; |
| 179 |
} |
| 180 |
SkUNREACHABLE; |
| 181 |
} |
| 182 |
|
| 183 |
static void setup_input_assembly_state(const Caps& caps, |
| 184 |
PrimitiveType primitiveType, |
| 185 |
VkPipelineInputAssemblyStateCreateInfo* inputAssemblyInfo) { |
| 186 |
*inputAssemblyInfo = {}; |
| 187 |
inputAssemblyInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; |
| 188 |
inputAssemblyInfo->primitiveRestartEnable = VK_FALSE; |
| 189 |
if (caps.useBasicDynamicState()) { |
| 190 |
// Note: when topology is dynamic state, the topology _class_ still needs to be specified. |
| 191 |
inputAssemblyInfo->topology = primitiveType == PrimitiveType::kPoints |
| 192 |
? VK_PRIMITIVE_TOPOLOGY_POINT_LIST |
| 193 |
: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; |
| 194 |
} else { |
| 195 |
inputAssemblyInfo->topology = primitive_type_to_vk_topology(primitiveType); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
static VkStencilOp stencil_op_to_vk_stencil_op(StencilOp op) { |
| 200 |
static const VkStencilOp gTable[] = { |
| 201 |
VK_STENCIL_OP_KEEP, // kKeep |
| 202 |
VK_STENCIL_OP_ZERO, // kZero |
| 203 |
VK_STENCIL_OP_REPLACE, // kReplace |
| 204 |
VK_STENCIL_OP_INVERT, // kInvert |
| 205 |
VK_STENCIL_OP_INCREMENT_AND_WRAP, // kIncWrap |
| 206 |
VK_STENCIL_OP_DECREMENT_AND_WRAP, // kDecWrap |
| 207 |
VK_STENCIL_OP_INCREMENT_AND_CLAMP, // kIncClamp |
| 208 |
VK_STENCIL_OP_DECREMENT_AND_CLAMP, // kDecClamp |
| 209 |
}; |
| 210 |
static_assert(std::size(gTable) == kStencilOpCount); |
| 211 |
static_assert(0 == (int)StencilOp::kKeep); |
| 212 |
static_assert(1 == (int)StencilOp::kZero); |
| 213 |
static_assert(2 == (int)StencilOp::kReplace); |
| 214 |
static_assert(3 == (int)StencilOp::kInvert); |
| 215 |
static_assert(4 == (int)StencilOp::kIncWrap); |
| 216 |
static_assert(5 == (int)StencilOp::kDecWrap); |
| 217 |
static_assert(6 == (int)StencilOp::kIncClamp); |
| 218 |
static_assert(7 == (int)StencilOp::kDecClamp); |
| 219 |
SkASSERT(op < (StencilOp)kStencilOpCount); |
| 220 |
return gTable[(int)op]; |
| 221 |
} |
| 222 |
|
| 223 |
static VkCompareOp compare_op_to_vk_compare_op(CompareOp op) { |
| 224 |
static const VkCompareOp gTable[] = { |
| 225 |
VK_COMPARE_OP_ALWAYS, // kAlways |
| 226 |
VK_COMPARE_OP_NEVER, // kNever |
| 227 |
VK_COMPARE_OP_GREATER, // kGreater |
| 228 |
VK_COMPARE_OP_GREATER_OR_EQUAL, // kGEqual |
| 229 |
VK_COMPARE_OP_LESS, // kLess |
| 230 |
VK_COMPARE_OP_LESS_OR_EQUAL, // kLEqual |
| 231 |
VK_COMPARE_OP_EQUAL, // kEqual |
| 232 |
VK_COMPARE_OP_NOT_EQUAL, // kNotEqual |
| 233 |
}; |
| 234 |
static_assert(std::size(gTable) == kCompareOpCount); |
| 235 |
static_assert(0 == (int)CompareOp::kAlways); |
| 236 |
static_assert(1 == (int)CompareOp::kNever); |
| 237 |
static_assert(2 == (int)CompareOp::kGreater); |
| 238 |
static_assert(3 == (int)CompareOp::kGEqual); |
| 239 |
static_assert(4 == (int)CompareOp::kLess); |
| 240 |
static_assert(5 == (int)CompareOp::kLEqual); |
| 241 |
static_assert(6 == (int)CompareOp::kEqual); |
| 242 |
static_assert(7 == (int)CompareOp::kNotEqual); |
| 243 |
SkASSERT(op < (CompareOp)kCompareOpCount); |
| 244 |
|
| 245 |
return gTable[(int)op]; |
| 246 |
} |
| 247 |
|
| 248 |
static void setup_stencil_op_state(VkStencilOpState* opState, |
| 249 |
const DepthStencilSettings::Face& face, |
| 250 |
uint32_t referenceValue) { |
| 251 |
opState->failOp = stencil_op_to_vk_stencil_op(face.fStencilFailOp); |
| 252 |
opState->passOp = stencil_op_to_vk_stencil_op(face.fDepthStencilPassOp); |
| 253 |
opState->depthFailOp = stencil_op_to_vk_stencil_op(face.fDepthFailOp); |
| 254 |
opState->compareOp = compare_op_to_vk_compare_op(face.fCompareOp); |
| 255 |
opState->compareMask = face.fReadMask; |
| 256 |
// Note: On some old ARM driver versions, dynamic state for stencil write mask doesn't work |
| 257 |
// correctly in the presence of discard or alpha to coverage, if the static state provided |
| 258 |
// when creating the pipeline has a value of 0. However, both alphaToCoverageEnable and |
| 259 |
// rasterizerDiscardEnable are always false in Graphite, so no driver bug workaround is |
| 260 |
// necessary at the moment. |
| 261 |
opState->writeMask = face.fWriteMask; |
| 262 |
opState->reference = referenceValue; |
| 263 |
} |
| 264 |
|
| 265 |
static void setup_depth_stencil_state(const Caps& caps, |
| 266 |
const DepthStencilSettings& stencilSettings, |
| 267 |
VkPipelineDepthStencilStateCreateInfo* stencilInfo) { |
| 268 |
SkASSERT(stencilSettings.fDepthTestEnabled || |
| 269 |
stencilSettings.fDepthCompareOp == CompareOp::kAlways); |
| 270 |
|
| 271 |
*stencilInfo = {}; |
| 272 |
stencilInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; |
| 273 |
if (!caps.useBasicDynamicState()) { |
| 274 |
stencilInfo->depthTestEnable = stencilSettings.fDepthTestEnabled; |
| 275 |
stencilInfo->depthWriteEnable = stencilSettings.fDepthWriteEnabled; |
| 276 |
stencilInfo->depthCompareOp = compare_op_to_vk_compare_op(stencilSettings.fDepthCompareOp); |
| 277 |
stencilInfo->depthBoundsTestEnable = VK_FALSE; |
| 278 |
stencilInfo->stencilTestEnable = stencilSettings.fStencilTestEnabled; |
| 279 |
if (stencilSettings.fStencilTestEnabled) { |
| 280 |
setup_stencil_op_state(&stencilInfo->front, |
| 281 |
stencilSettings.fFrontStencil, |
| 282 |
stencilSettings.fStencilReferenceValue); |
| 283 |
setup_stencil_op_state(&stencilInfo->back, |
| 284 |
stencilSettings.fBackStencil, |
| 285 |
stencilSettings.fStencilReferenceValue); |
| 286 |
} |
| 287 |
stencilInfo->minDepthBounds = 0.0f; |
| 288 |
stencilInfo->maxDepthBounds = 1.0f; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
static void setup_viewport_scissor_state(VkPipelineViewportStateCreateInfo* viewportInfo) { |
| 293 |
*viewportInfo = {}; |
| 294 |
viewportInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; |
| 295 |
|
| 296 |
// The viewport and scissor are set dyanmically with draw pass commands |
| 297 |
viewportInfo->viewportCount = 1; |
| 298 |
viewportInfo->scissorCount = 1; |
| 299 |
|
| 300 |
SkASSERT(viewportInfo->viewportCount == viewportInfo->scissorCount); |
| 301 |
} |
| 302 |
|
| 303 |
static void setup_multisample_state(SampleCount sampleCount, |
| 304 |
VkPipelineMultisampleStateCreateInfo* multisampleInfo) { |
| 305 |
*multisampleInfo = {}; |
| 306 |
multisampleInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; |
| 307 |
multisampleInfo->rasterizationSamples = SampleCountToVkSampleCount(sampleCount); |
| 308 |
multisampleInfo->sampleShadingEnable = VK_FALSE; |
| 309 |
multisampleInfo->minSampleShading = 0.0f; |
| 310 |
multisampleInfo->pSampleMask = nullptr; |
| 311 |
multisampleInfo->alphaToCoverageEnable = VK_FALSE; |
| 312 |
multisampleInfo->alphaToOneEnable = VK_FALSE; |
| 313 |
} |
| 314 |
|
| 315 |
static VkBlendFactor blend_coeff_to_vk_blend(skgpu::BlendCoeff coeff) { |
| 316 |
switch (coeff) { |
| 317 |
case skgpu::BlendCoeff::kZero: |
| 318 |
return VK_BLEND_FACTOR_ZERO; |
| 319 |
case skgpu::BlendCoeff::kOne: |
| 320 |
return VK_BLEND_FACTOR_ONE; |
| 321 |
case skgpu::BlendCoeff::kSC: |
| 322 |
return VK_BLEND_FACTOR_SRC_COLOR; |
| 323 |
case skgpu::BlendCoeff::kISC: |
| 324 |
return VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR; |
| 325 |
case skgpu::BlendCoeff::kDC: |
| 326 |
return VK_BLEND_FACTOR_DST_COLOR; |
| 327 |
case skgpu::BlendCoeff::kIDC: |
| 328 |
return VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR; |
| 329 |
case skgpu::BlendCoeff::kSA: |
| 330 |
return VK_BLEND_FACTOR_SRC_ALPHA; |
| 331 |
case skgpu::BlendCoeff::kISA: |
| 332 |
return VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; |
| 333 |
case skgpu::BlendCoeff::kDA: |
| 334 |
return VK_BLEND_FACTOR_DST_ALPHA; |
| 335 |
case skgpu::BlendCoeff::kIDA: |
| 336 |
return VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA; |
| 337 |
case skgpu::BlendCoeff::kConstC: |
| 338 |
return VK_BLEND_FACTOR_CONSTANT_COLOR; |
| 339 |
case skgpu::BlendCoeff::kIConstC: |
| 340 |
return VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR; |
| 341 |
case skgpu::BlendCoeff::kS2C: |
| 342 |
return VK_BLEND_FACTOR_SRC1_COLOR; |
| 343 |
case skgpu::BlendCoeff::kIS2C: |
| 344 |
return VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR; |
| 345 |
case skgpu::BlendCoeff::kS2A: |
| 346 |
return VK_BLEND_FACTOR_SRC1_ALPHA; |
| 347 |
case skgpu::BlendCoeff::kIS2A: |
| 348 |
return VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA; |
| 349 |
case skgpu::BlendCoeff::kIllegal: |
| 350 |
return VK_BLEND_FACTOR_ZERO; |
| 351 |
} |
| 352 |
SkUNREACHABLE; |
| 353 |
} |
| 354 |
|
| 355 |
static VkBlendOp blend_equation_to_vk_blend_op(skgpu::BlendEquation equation) { |
| 356 |
static const VkBlendOp gTable[] = { |
| 357 |
// Basic blend ops |
| 358 |
VK_BLEND_OP_ADD, |
| 359 |
VK_BLEND_OP_SUBTRACT, |
| 360 |
VK_BLEND_OP_REVERSE_SUBTRACT, |
| 361 |
|
| 362 |
// Advanced blend ops |
| 363 |
VK_BLEND_OP_SCREEN_EXT, |
| 364 |
VK_BLEND_OP_OVERLAY_EXT, |
| 365 |
VK_BLEND_OP_DARKEN_EXT, |
| 366 |
VK_BLEND_OP_LIGHTEN_EXT, |
| 367 |
VK_BLEND_OP_COLORDODGE_EXT, |
| 368 |
VK_BLEND_OP_COLORBURN_EXT, |
| 369 |
VK_BLEND_OP_HARDLIGHT_EXT, |
| 370 |
VK_BLEND_OP_SOFTLIGHT_EXT, |
| 371 |
VK_BLEND_OP_DIFFERENCE_EXT, |
| 372 |
VK_BLEND_OP_EXCLUSION_EXT, |
| 373 |
VK_BLEND_OP_MULTIPLY_EXT, |
| 374 |
VK_BLEND_OP_HSL_HUE_EXT, |
| 375 |
VK_BLEND_OP_HSL_SATURATION_EXT, |
| 376 |
VK_BLEND_OP_HSL_COLOR_EXT, |
| 377 |
VK_BLEND_OP_HSL_LUMINOSITY_EXT, |
| 378 |
|
| 379 |
// Illegal. |
| 380 |
VK_BLEND_OP_ADD, |
| 381 |
}; |
| 382 |
static_assert(0 == (int)skgpu::BlendEquation::kAdd); |
| 383 |
static_assert(1 == (int)skgpu::BlendEquation::kSubtract); |
| 384 |
static_assert(2 == (int)skgpu::BlendEquation::kReverseSubtract); |
| 385 |
static_assert(3 == (int)skgpu::BlendEquation::kScreen); |
| 386 |
static_assert(4 == (int)skgpu::BlendEquation::kOverlay); |
| 387 |
static_assert(5 == (int)skgpu::BlendEquation::kDarken); |
| 388 |
static_assert(6 == (int)skgpu::BlendEquation::kLighten); |
| 389 |
static_assert(7 == (int)skgpu::BlendEquation::kColorDodge); |
| 390 |
static_assert(8 == (int)skgpu::BlendEquation::kColorBurn); |
| 391 |
static_assert(9 == (int)skgpu::BlendEquation::kHardLight); |
| 392 |
static_assert(10 == (int)skgpu::BlendEquation::kSoftLight); |
| 393 |
static_assert(11 == (int)skgpu::BlendEquation::kDifference); |
| 394 |
static_assert(12 == (int)skgpu::BlendEquation::kExclusion); |
| 395 |
static_assert(13 == (int)skgpu::BlendEquation::kMultiply); |
| 396 |
static_assert(14 == (int)skgpu::BlendEquation::kHSLHue); |
| 397 |
static_assert(15 == (int)skgpu::BlendEquation::kHSLSaturation); |
| 398 |
static_assert(16 == (int)skgpu::BlendEquation::kHSLColor); |
| 399 |
static_assert(17 == (int)skgpu::BlendEquation::kHSLLuminosity); |
| 400 |
static_assert(std::size(gTable) == skgpu::kBlendEquationCnt); |
| 401 |
|
| 402 |
SkASSERT((unsigned)equation < skgpu::kBlendEquationCnt); |
| 403 |
return gTable[(int)equation]; |
| 404 |
} |
| 405 |
|
| 406 |
static void setup_color_blend_state(const VulkanCaps& caps, |
| 407 |
const skgpu::BlendInfo& blendInfo, |
| 408 |
VkPipelineColorBlendStateCreateInfo* colorBlendInfo, |
| 409 |
VkPipelineColorBlendAttachmentState* attachmentState) { |
| 410 |
skgpu::BlendEquation equation = blendInfo.fEquation; |
| 411 |
skgpu::BlendCoeff srcCoeff = blendInfo.fSrcBlend; |
| 412 |
skgpu::BlendCoeff dstCoeff = blendInfo.fDstBlend; |
| 413 |
bool blendOff = skgpu::BlendShouldDisable(equation, srcCoeff, dstCoeff); |
| 414 |
|
| 415 |
*attachmentState = {}; |
| 416 |
attachmentState->blendEnable = !blendOff; |
| 417 |
if (!blendOff) { |
| 418 |
attachmentState->srcColorBlendFactor = blend_coeff_to_vk_blend(srcCoeff); |
| 419 |
attachmentState->dstColorBlendFactor = blend_coeff_to_vk_blend(dstCoeff); |
| 420 |
attachmentState->colorBlendOp = blend_equation_to_vk_blend_op(equation); |
| 421 |
attachmentState->srcAlphaBlendFactor = blend_coeff_to_vk_blend(srcCoeff); |
| 422 |
attachmentState->dstAlphaBlendFactor = blend_coeff_to_vk_blend(dstCoeff); |
| 423 |
attachmentState->alphaBlendOp = blend_equation_to_vk_blend_op(equation); |
| 424 |
} |
| 425 |
|
| 426 |
if (!blendInfo.fWritesColor) { |
| 427 |
attachmentState->colorWriteMask = 0; |
| 428 |
} else { |
| 429 |
attachmentState->colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | |
| 430 |
VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; |
| 431 |
} |
| 432 |
|
| 433 |
*colorBlendInfo = {}; |
| 434 |
colorBlendInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; |
| 435 |
colorBlendInfo->logicOpEnable = VK_FALSE; |
| 436 |
colorBlendInfo->attachmentCount = 1; |
| 437 |
colorBlendInfo->pAttachments = attachmentState; |
| 438 |
// colorBlendInfo->blendConstants is set dynamically |
| 439 |
|
| 440 |
if (caps.supportsRasterizationOrderColorAttachmentAccess()) { |
| 441 |
colorBlendInfo->flags = |
| 442 |
VK_PIPELINE_COLOR_BLEND_STATE_CREATE_RASTERIZATION_ORDER_ATTACHMENT_ACCESS_BIT_EXT; |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
static void setup_raster_state(const Caps& caps, |
| 447 |
bool isWireframe, |
| 448 |
VkPipelineRasterizationStateCreateInfo* rasterInfo) { |
| 449 |
*rasterInfo = {}; |
| 450 |
rasterInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; |
| 451 |
rasterInfo->depthClampEnable = VK_FALSE; |
| 452 |
rasterInfo->rasterizerDiscardEnable = VK_FALSE; |
| 453 |
rasterInfo->polygonMode = isWireframe ? VK_POLYGON_MODE_LINE : VK_POLYGON_MODE_FILL; |
| 454 |
rasterInfo->cullMode = VK_CULL_MODE_NONE; |
| 455 |
rasterInfo->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; |
| 456 |
rasterInfo->depthBiasEnable = VK_FALSE; |
| 457 |
rasterInfo->depthBiasConstantFactor = 0.0f; |
| 458 |
rasterInfo->depthBiasClamp = 0.0f; |
| 459 |
rasterInfo->depthBiasSlopeFactor = 0.0f; |
| 460 |
rasterInfo->lineWidth = 1.0f; |
| 461 |
} |
| 462 |
|
| 463 |
static void setup_shader_stage_info(VkShaderStageFlagBits stage, |
| 464 |
VkShaderModule shaderModule, |
| 465 |
VkPipelineShaderStageCreateInfo* shaderStageInfo) { |
| 466 |
*shaderStageInfo = {}; |
| 467 |
shaderStageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 468 |
shaderStageInfo->stage = stage; |
| 469 |
shaderStageInfo->module = shaderModule; |
| 470 |
shaderStageInfo->pName = "main"; |
| 471 |
} |
| 472 |
|
| 473 |
static VkDescriptorSetLayout descriptor_data_to_layout( |
| 474 |
const VulkanSharedContext* sharedContext, const SkSpan<DescriptorData>& descriptorData) { |
| 475 |
// descriptorData can be empty to indicate that we should create a mock placeholder layout |
| 476 |
// with no descriptors. |
| 477 |
VkDescriptorSetLayout setLayout; |
| 478 |
DescriptorDataToVkDescSetLayout(sharedContext, descriptorData, &setLayout); |
| 479 |
if (setLayout == VK_NULL_HANDLE) { |
| 480 |
SKGPU_LOG_E("Failed to create descriptor set layout; pipeline creation will fail.\n"); |
| 481 |
return VK_NULL_HANDLE; |
| 482 |
} |
| 483 |
return setLayout; |
| 484 |
} |
| 485 |
|
| 486 |
static void destroy_desc_set_layouts(const VulkanSharedContext* sharedContext, |
| 487 |
skia_private::TArray<VkDescriptorSetLayout>& setLayouts) { |
| 488 |
for (int i = 0; i < setLayouts.size(); i++) { |
| 489 |
if (setLayouts[i] != VK_NULL_HANDLE) { |
| 490 |
VULKAN_CALL(sharedContext->interface(), |
| 491 |
DestroyDescriptorSetLayout(sharedContext->device(), |
| 492 |
setLayouts[i], |
| 493 |
nullptr)); |
| 494 |
} |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
static bool input_attachment_desc_set_layout(VkDescriptorSetLayout& outLayout, |
| 499 |
const VulkanSharedContext* sharedContext, |
| 500 |
bool mockOnly) { |
| 501 |
skia_private::STArray<1, DescriptorData> inputAttachmentDesc; |
| 502 |
|
| 503 |
if (!mockOnly) { |
| 504 |
inputAttachmentDesc.push_back(VulkanGraphicsPipeline::kInputAttachmentDescriptor); |
| 505 |
} |
| 506 |
|
| 507 |
// If mockOnly is true (meaning no input attachment descriptor is actually needed), then still |
| 508 |
// request a mock VkDescriptorSetLayout handle by passing in the unpopulated span. |
| 509 |
outLayout = descriptor_data_to_layout(sharedContext, {inputAttachmentDesc}); |
| 510 |
return outLayout != VK_NULL_HANDLE; |
| 511 |
} |
| 512 |
|
| 513 |
static bool uniform_desc_set_layout(VkDescriptorSetLayout& outLayout, |
| 514 |
const VulkanSharedContext* sharedContext, |
| 515 |
bool hasCombinedUniforms, |
| 516 |
bool hasGradientBuffer) { |
| 517 |
// Define a container with size reserved for up to kNumUniformBuffers descriptors. Only add |
| 518 |
// DescriptorData for uniforms that actually are used and need to be included in the layout. |
| 519 |
skia_private::STArray< |
| 520 |
VulkanGraphicsPipeline::kNumUniformBuffers, DescriptorData> uniformDescriptors; |
| 521 |
|
| 522 |
DescriptorType uniformBufferType = |
| 523 |
sharedContext->caps()->storageBufferSupport() ? DescriptorType::kStorageBuffer |
| 524 |
: DescriptorType::kUniformBuffer; |
| 525 |
if (hasCombinedUniforms) { |
| 526 |
uniformDescriptors.push_back({ |
| 527 |
uniformBufferType, /*count=*/1, |
| 528 |
VulkanGraphicsPipeline::kCombinedUniformIndex, |
| 529 |
PipelineStageFlags::kVertexShader | PipelineStageFlags::kFragmentShader}); |
| 530 |
} |
| 531 |
if (hasGradientBuffer) { |
| 532 |
uniformDescriptors.push_back({ |
| 533 |
DescriptorType::kStorageBuffer, |
| 534 |
/*count=*/1, |
| 535 |
VulkanGraphicsPipeline::kGradientBufferIndex, |
| 536 |
PipelineStageFlags::kFragmentShader}); |
| 537 |
} |
| 538 |
|
| 539 |
// If no uniforms are used, still request a mock VkDescriptorSetLayout handle by passing in the |
| 540 |
// unpopulated span of uniformDescriptors to descriptor set layout creation. |
| 541 |
outLayout = descriptor_data_to_layout(sharedContext, {uniformDescriptors}); |
| 542 |
return true; |
| 543 |
} |
| 544 |
|
| 545 |
static bool texture_sampler_desc_set_layout(VkDescriptorSetLayout& outLayout, |
| 546 |
const VulkanSharedContext* sharedContext, |
| 547 |
const int numTextureSamplers, |
| 548 |
SkSpan<sk_sp<VulkanSampler>> immutableSamplers) { |
| 549 |
SkASSERT(numTextureSamplers >= 0); |
| 550 |
// The immutable sampler span size must be = the total number of texture/samplers such that |
| 551 |
// we can use the index of a sampler as its binding index (or we just have none, which |
| 552 |
// enables us to skip some of this logic entirely). |
| 553 |
SkASSERT(immutableSamplers.empty() || |
| 554 |
SkTo<int>(immutableSamplers.size()) == numTextureSamplers); |
| 555 |
|
| 556 |
skia_private::TArray<DescriptorData> textureSamplerDescs(numTextureSamplers); |
| 557 |
for (int i = 0; i < numTextureSamplers; i++) { |
| 558 |
Sampler* immutableSampler = nullptr; |
| 559 |
if (!immutableSamplers.empty() && immutableSamplers[i]) { |
| 560 |
immutableSampler = immutableSamplers[i].get(); |
| 561 |
} |
| 562 |
textureSamplerDescs.push_back({DescriptorType::kCombinedTextureSampler, |
| 563 |
/*count=*/1, |
| 564 |
/*bindingIdx=*/i, |
| 565 |
PipelineStageFlags::kFragmentShader, |
| 566 |
immutableSampler}); |
| 567 |
} |
| 568 |
|
| 569 |
// If no texture/samplers are used, a mock VkDescriptorSetLayout handle by passing in the |
| 570 |
// unpopulated span of textureSamplerDescs to descriptor set layout creation. |
| 571 |
outLayout = descriptor_data_to_layout(sharedContext, {textureSamplerDescs}); |
| 572 |
return outLayout != VK_NULL_HANDLE; |
| 573 |
} |
| 574 |
|
| 575 |
static VkPipelineLayout setup_pipeline_layout(const VulkanSharedContext* sharedContext, |
| 576 |
uint32_t pushConstantSize, |
| 577 |
VkShaderStageFlagBits pushConstantPipelineStageFlags, |
| 578 |
bool hasCombinedUniforms, |
| 579 |
bool hasGradientBuffer, |
| 580 |
int numTextureSamplers, |
| 581 |
bool loadMsaaFromResolve, |
| 582 |
SkSpan<sk_sp<VulkanSampler>> immutableSamplers) { |
| 583 |
// Create a container with the max anticipated amount (kMaxNumDescSets) of VkDescriptorSetLayout |
| 584 |
// handles which will be used to create the pipeline layout. |
| 585 |
skia_private::STArray< |
| 586 |
VulkanGraphicsPipeline::kMaxNumDescSets, VkDescriptorSetLayout> setLayouts; |
| 587 |
setLayouts.push_back_n(VulkanGraphicsPipeline::kMaxNumDescSets, VkDescriptorSetLayout()); |
| 588 |
|
| 589 |
// Populate the container with actual descriptor set layout handles. Each index should contain |
| 590 |
// either a valid/real or a mock/placehodler layout handle. Mock VkDescriptorSetLayouts do not |
| 591 |
// actually contain any descriptors, but are needed as placeholders to maintain expected |
| 592 |
// descriptor set binding indices. This is because VK_NULL_HANDLE is a valid |
| 593 |
// VkDescriptorSetLayout value iff the graphicsPipelineLibrary feature is enabled, which is not |
| 594 |
// the case for all targeted devices (see |
| 595 |
// VUID-VkPipelineLayoutCreateInfo-graphicsPipelineLibrary-06753). If any of the helpers |
| 596 |
// encounter an error (i.e., return false), return a null VkPipelineLayout. |
| 597 |
if (!input_attachment_desc_set_layout( |
| 598 |
setLayouts[VulkanGraphicsPipeline::kDstAsInputDescSetIndex], |
| 599 |
sharedContext, |
| 600 |
/*mockOnly=*/false) || // We always add an input attachment descriptor |
| 601 |
!uniform_desc_set_layout( |
| 602 |
setLayouts[VulkanGraphicsPipeline::kUniformBufferDescSetIndex], |
| 603 |
sharedContext, |
| 604 |
hasCombinedUniforms, |
| 605 |
hasGradientBuffer) || |
| 606 |
!texture_sampler_desc_set_layout( |
| 607 |
setLayouts[VulkanGraphicsPipeline::kTextureBindDescSetIndex], |
| 608 |
sharedContext, |
| 609 |
numTextureSamplers, |
| 610 |
immutableSamplers) || |
| 611 |
!input_attachment_desc_set_layout( |
| 612 |
setLayouts[VulkanGraphicsPipeline::kLoadMsaaFromResolveInputDescSetIndex], |
| 613 |
sharedContext, |
| 614 |
/*mockOnly=*/!loadMsaaFromResolve)) { // Actual descriptor needed iff loading MSAA |
| 615 |
destroy_desc_set_layouts(sharedContext, setLayouts); |
| 616 |
return VK_NULL_HANDLE; |
| 617 |
} |
| 618 |
|
| 619 |
// Generate a pipeline layout using the now-populated descriptor set layout array |
| 620 |
VkPushConstantRange pushConstantRange; |
| 621 |
if (pushConstantSize) { |
| 622 |
pushConstantRange.offset = 0; |
| 623 |
pushConstantRange.size = pushConstantSize; |
| 624 |
pushConstantRange.stageFlags = pushConstantPipelineStageFlags; |
| 625 |
} |
| 626 |
VkPipelineLayoutCreateInfo layoutCreateInfo = {}; |
| 627 |
layoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; |
| 628 |
layoutCreateInfo.setLayoutCount = setLayouts.size(); |
| 629 |
layoutCreateInfo.pSetLayouts = setLayouts.begin(); |
| 630 |
layoutCreateInfo.pushConstantRangeCount = pushConstantSize ? 1 : 0; |
| 631 |
layoutCreateInfo.pPushConstantRanges = pushConstantSize ? &pushConstantRange : nullptr; |
| 632 |
|
| 633 |
VkResult result; |
| 634 |
VkPipelineLayout layout; |
| 635 |
VULKAN_CALL_RESULT(sharedContext, |
| 636 |
result, |
| 637 |
CreatePipelineLayout(sharedContext->device(), |
| 638 |
&layoutCreateInfo, |
| 639 |
/*const VkAllocationCallbacks*=*/nullptr, |
| 640 |
&layout)); |
| 641 |
|
| 642 |
// DescriptorSetLayouts can be deleted after the pipeline layout is created. |
| 643 |
destroy_desc_set_layouts(sharedContext, setLayouts); |
| 644 |
|
| 645 |
return result == VK_SUCCESS ? layout : VK_NULL_HANDLE; |
| 646 |
} |
| 647 |
|
| 648 |
static VkPipeline create_graphics_pipeline(VulkanSharedContext* sharedContext, |
| 649 |
VkGraphicsPipelineCreateInfo* pipelineCreateInfo) { |
| 650 |
VkPipeline vkPipeline = VK_NULL_HANDLE; |
| 651 |
VkResult result = VK_PIPELINE_COMPILE_REQUIRED; |
| 652 |
{ |
| 653 |
TRACE_EVENT0_ALWAYS("skia.shaders", "CreateGraphicsPipeline"); |
| 654 |
|
| 655 |
if (sharedContext->vulkanCaps().supportsPipelineCreationCacheControl()) { |
| 656 |
TRACE_EVENT0_ALWAYS("skia.shaders", "CreateGraphicsPipeline-CacheLookup"); |
| 657 |
|
| 658 |
pipelineCreateInfo->flags |= |
| 659 |
VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT_EXT; |
| 660 |
VULKAN_CALL_RESULT_NOCHECK( |
| 661 |
sharedContext->interface(), |
| 662 |
result, |
| 663 |
CreateGraphicsPipelines(sharedContext->device(), |
| 664 |
sharedContext->getPipelineCache(), |
| 665 |
/*createInfoCount=*/ 1, |
| 666 |
pipelineCreateInfo, |
| 667 |
/*pAllocator=*/nullptr, |
| 668 |
&vkPipeline)); |
| 669 |
sharedContext->checkVkResult(result); |
| 670 |
} |
| 671 |
|
| 672 |
if (result == VK_PIPELINE_COMPILE_REQUIRED) { |
| 673 |
TRACE_EVENT0_ALWAYS( |
| 674 |
"skia.shaders", |
| 675 |
TRACE_STR_STATIC(sharedContext->vulkanCaps().supportsPipelineCreationCacheControl() |
| 676 |
? "CreateGraphicsPipeline-CompileAfterCacheMiss" |
| 677 |
: "CreateGraphicsPipeline-CacheLookupOrCompile")); |
| 678 |
|
| 679 |
pipelineCreateInfo->flags &= |
| 680 |
~VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT_EXT; |
| 681 |
VULKAN_CALL_RESULT(sharedContext, |
| 682 |
result, |
| 683 |
CreateGraphicsPipelines(sharedContext->device(), |
| 684 |
sharedContext->getPipelineCache(), |
| 685 |
/*createInfoCount=*/1, |
| 686 |
pipelineCreateInfo, |
| 687 |
/*pAllocator=*/nullptr, |
| 688 |
&vkPipeline)); |
| 689 |
if (result == VK_SUCCESS) { |
| 690 |
sharedContext->pipelineCompileWasRequired(); |
| 691 |
} |
| 692 |
} |
| 693 |
} |
| 694 |
if (result != VK_SUCCESS) { |
| 695 |
SKGPU_LOG_E("Failed to create pipeline. Error: %d\n", result); |
| 696 |
return VK_NULL_HANDLE; |
| 697 |
} |
| 698 |
|
| 699 |
return vkPipeline; |
| 700 |
} |
| 701 |
|
| 702 |
static bool create_shaders_pipeline(VulkanSharedContext* sharedContext, |
| 703 |
VkGraphicsPipelineCreateInfo& completePipelineInfo, |
| 704 |
VkPipelineLibraryCreateInfoKHR& shadersLibraryInfo, |
| 705 |
VkGraphicsPipelineLibraryCreateInfoEXT& libraryInfo, |
| 706 |
VkPipeline* shadersPipeline) { |
| 707 |
// Provide state that only pertains to the shaders subset to create the library. |
| 708 |
VkGraphicsPipelineCreateInfo shadersPipelineCreateInfo = {}; |
| 709 |
shadersPipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
| 710 |
shadersPipelineCreateInfo.flags = 0; |
| 711 |
shadersPipelineCreateInfo.stageCount = completePipelineInfo.stageCount; |
| 712 |
shadersPipelineCreateInfo.pStages = completePipelineInfo.pStages; |
| 713 |
shadersPipelineCreateInfo.pViewportState = completePipelineInfo.pViewportState; |
| 714 |
shadersPipelineCreateInfo.pRasterizationState = completePipelineInfo.pRasterizationState; |
| 715 |
shadersPipelineCreateInfo.pMultisampleState = completePipelineInfo.pMultisampleState; |
| 716 |
shadersPipelineCreateInfo.pDepthStencilState = completePipelineInfo.pDepthStencilState; |
| 717 |
shadersPipelineCreateInfo.pDynamicState = completePipelineInfo.pDynamicState; |
| 718 |
shadersPipelineCreateInfo.layout = completePipelineInfo.layout; |
| 719 |
shadersPipelineCreateInfo.renderPass = completePipelineInfo.renderPass; |
| 720 |
shadersPipelineCreateInfo.subpass = completePipelineInfo.subpass; |
| 721 |
|
| 722 |
// Specify that this is the shaders subset of the pipeline. |
| 723 |
libraryInfo.flags = VK_GRAPHICS_PIPELINE_LIBRARY_PRE_RASTERIZATION_SHADERS_BIT_EXT | |
| 724 |
VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_SHADER_BIT_EXT; |
| 725 |
shadersPipelineCreateInfo.flags |= VK_PIPELINE_CREATE_LIBRARY_BIT_KHR; |
| 726 |
skgpu::AddToPNextChain(&shadersPipelineCreateInfo, &libraryInfo); |
| 727 |
|
| 728 |
*shadersPipeline = create_graphics_pipeline(sharedContext, &shadersPipelineCreateInfo); |
| 729 |
if (*shadersPipeline == VK_NULL_HANDLE) { |
| 730 |
return false; |
| 731 |
} |
| 732 |
|
| 733 |
// The complete pipeline doesn't need all the state anymore, it inherits them from the |
| 734 |
// shaders library. |
| 735 |
completePipelineInfo.stageCount = 0; |
| 736 |
completePipelineInfo.pStages = nullptr; |
| 737 |
completePipelineInfo.pViewportState = nullptr; |
| 738 |
completePipelineInfo.pRasterizationState = nullptr; |
| 739 |
completePipelineInfo.pDepthStencilState = nullptr; |
| 740 |
|
| 741 |
// The complete pipeline provides the vertex input and fragment output interface state. |
| 742 |
// It's important that these states are not built into a separate library, because some |
| 743 |
// drivers can generate more efficient blend code if they have information about the |
| 744 |
// fragment shader. |
| 745 |
libraryInfo.flags = VK_GRAPHICS_PIPELINE_LIBRARY_VERTEX_INPUT_INTERFACE_BIT_EXT | |
| 746 |
VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_OUTPUT_INTERFACE_BIT_EXT; |
| 747 |
skgpu::AddToPNextChain(&completePipelineInfo, &libraryInfo); |
| 748 |
|
| 749 |
// Tell the complete pipeline to link with the shaders library: |
| 750 |
shadersLibraryInfo.libraryCount = 1; |
| 751 |
shadersLibraryInfo.pLibraries = shadersPipeline; |
| 752 |
AddToPNextChain(&completePipelineInfo, &shadersLibraryInfo); |
| 753 |
|
| 754 |
return true; |
| 755 |
} |
| 756 |
|
| 757 |
using VkDynamicStateList = std::array<VkDynamicState, 22>; |
| 758 |
static void setup_dynamic_state(const Caps& caps, |
| 759 |
VkPipelineDynamicStateCreateInfo* dynamicInfo, |
| 760 |
VkDynamicStateList* dynamicStates) { |
| 761 |
uint32_t count = 0; |
| 762 |
|
| 763 |
// The following state is always dynamic in Graphite |
| 764 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_VIEWPORT; |
| 765 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_SCISSOR; |
| 766 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_BLEND_CONSTANTS; |
| 767 |
|
| 768 |
if (caps.useBasicDynamicState()) { |
| 769 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_LINE_WIDTH; |
| 770 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_DEPTH_BIAS; |
| 771 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_DEPTH_BOUNDS; |
| 772 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK; |
| 773 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_STENCIL_WRITE_MASK; |
| 774 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_STENCIL_REFERENCE; |
| 775 |
|
| 776 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_DEPTH_BOUNDS_TEST_ENABLE; |
| 777 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_DEPTH_BIAS_ENABLE; |
| 778 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_DEPTH_COMPARE_OP; |
| 779 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_DEPTH_TEST_ENABLE; |
| 780 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_DEPTH_WRITE_ENABLE; |
| 781 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_STENCIL_OP; |
| 782 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_STENCIL_TEST_ENABLE; |
| 783 |
|
| 784 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_PRIMITIVE_RESTART_ENABLE; |
| 785 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY; |
| 786 |
|
| 787 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_CULL_MODE; |
| 788 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_FRONT_FACE; |
| 789 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_RASTERIZER_DISCARD_ENABLE; |
| 790 |
} |
| 791 |
if (caps.useVertexInputDynamicState()) { |
| 792 |
(*dynamicStates)[count++] = VK_DYNAMIC_STATE_VERTEX_INPUT_EXT; |
| 793 |
} |
| 794 |
|
| 795 |
// VK_DYNAMIC_STATE_SCISSOR_WITH_COUNT_EXT and VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT are not |
| 796 |
// used because Graphite only sets one scissor and viewport. |
| 797 |
// |
| 798 |
// VK_DYNAMIC_STATE_LOGIC_OP_EXT and VK_DYNAMIC_STATE_PATCH_CONTROL_POINTS_EXT are irrelevant to |
| 799 |
// Graphite. |
| 800 |
// |
| 801 |
// VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE is not used because it will be eventually made |
| 802 |
// obsolete with VK_EXT_vertex_input_dynamic_state and VK_EXT_graphics_pipeline_library. |
| 803 |
// |
| 804 |
// Dynamic state from VK_EXT_extended_dynamic_state3 is not yet used. They _could_ be useful, |
| 805 |
// but very few bits are universal and lots of the interesting bits are covered by |
| 806 |
// VK_EXT_graphics_pipeline_library already. Still, state like |
| 807 |
// VK_DYNAMIC_STATE_DEPTH_CLAMP_ENABLE_EXT or VK_DYNAMIC_STATE_POLYGON_MODE_EXT could be used in |
| 808 |
// the future. |
| 809 |
|
| 810 |
memset(dynamicInfo, 0, sizeof(VkPipelineDynamicStateCreateInfo)); |
| 811 |
dynamicInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; |
| 812 |
dynamicInfo->dynamicStateCount = count; |
| 813 |
dynamicInfo->pDynamicStates = dynamicStates->data(); |
| 814 |
} |
| 815 |
|
| 816 |
VulkanProgramInfo::~VulkanProgramInfo() { |
| 817 |
if (fVS != VK_NULL_HANDLE) { |
| 818 |
VULKAN_CALL(fSharedContext->interface(), |
| 819 |
DestroyShaderModule(fSharedContext->device(), fVS, nullptr)); |
| 820 |
fVS = VK_NULL_HANDLE; |
| 821 |
} |
| 822 |
if (fFS != VK_NULL_HANDLE) { |
| 823 |
VULKAN_CALL(fSharedContext->interface(), |
| 824 |
DestroyShaderModule(fSharedContext->device(), fFS, nullptr)); |
| 825 |
fFS = VK_NULL_HANDLE; |
| 826 |
} |
| 827 |
if (fLayout != VK_NULL_HANDLE) { |
| 828 |
VULKAN_CALL(fSharedContext->interface(), |
| 829 |
DestroyPipelineLayout(fSharedContext->device(), |
| 830 |
fLayout, |
| 831 |
nullptr)); |
| 832 |
fLayout = VK_NULL_HANDLE; |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
sk_sp<VulkanGraphicsPipeline> VulkanGraphicsPipeline::Make( |
| 837 |
VulkanSharedContext* sharedContext, |
| 838 |
const RuntimeEffectDictionary* runtimeDict, |
| 839 |
const UniqueKey& pipelineKey, |
| 840 |
const GraphicsPipelineDesc& pipelineDesc, |
| 841 |
const RenderPassDesc& renderPassDesc, |
| 842 |
SkEnumBitMask<PipelineCreationFlags> pipelineCreationFlags, |
| 843 |
uint32_t compilationID) { |
| 844 |
VulkanThreadSafeResourceProvider* threadSafeResourceProvider = |
| 845 |
sharedContext->threadSafeResourceProvider(); |
| 846 |
|
| 847 |
SkSL::ProgramSettings settings; |
| 848 |
settings.fSharpenTextures = true; |
| 849 |
settings.fForceNoRTFlip = true; |
| 850 |
|
| 851 |
ShaderErrorHandler* errorHandler = sharedContext->caps()->shaderErrorHandler(); |
| 852 |
|
| 853 |
const RenderStep* step = sharedContext->rendererProvider()->lookup(pipelineDesc.renderStepID()); |
| 854 |
|
| 855 |
if (step->staticAttributes().size() + step->appendAttributes().size() > |
| 856 |
sharedContext->vulkanCaps().maxVertexAttributes()) { |
| 857 |
SKGPU_LOG_W("Requested more than the supported number of vertex attributes"); |
| 858 |
return nullptr; |
| 859 |
} |
| 860 |
|
| 861 |
skia_private::TArray<SamplerDesc> descContainer {}; |
| 862 |
std::unique_ptr<ShaderInfo> shaderInfo = |
| 863 |
ShaderInfo::Make(sharedContext->caps(), |
| 864 |
sharedContext->shaderCodeDictionary(), |
| 865 |
runtimeDict, |
| 866 |
renderPassDesc, |
| 867 |
step, |
| 868 |
pipelineDesc.paintParamsID(), |
| 869 |
&descContainer); |
| 870 |
|
| 871 |
// Populate an array of sampler ptrs where a sampler's index within the array indicates their |
| 872 |
// binding index within the descriptor set. Initialize all values to nullptr, which represents a |
| 873 |
// "regular", dynamic sampler at that index. |
| 874 |
skia_private::TArray<sk_sp<VulkanSampler>> immutableSamplers; |
| 875 |
immutableSamplers.push_back_n(shaderInfo->numFragmentTexturesAndSamplers()); |
| 876 |
|
| 877 |
// This logic relies upon Vulkan using combined texture/sampler bindings, which is necessary for |
| 878 |
// ycbcr samplers per the Vulkan spec. |
| 879 |
SkASSERT(!sharedContext->caps()->resourceBindingRequirements().fSeparateTextureAndSamplerBinding |
| 880 |
&& shaderInfo->numFragmentTexturesAndSamplers() == descContainer.size()); |
| 881 |
for (int i = 0; i < descContainer.size(); i++) { |
| 882 |
// If a SamplerDesc is not equivalent to the default-initialized SamplerDesc, that indicates |
| 883 |
// the usage of an immutable sampler. That sampler desc should then be used to obtain an |
| 884 |
// actual immutable sampler from the resource provider and added at the proper index within |
| 885 |
// immutableSamplers for inclusion in the pipeline layout. |
| 886 |
if (descContainer.at(i) != SamplerDesc()) { |
| 887 |
sk_sp<Sampler> immutableSampler = |
| 888 |
threadSafeResourceProvider->findOrCreateCompatibleSampler(descContainer.at(i)); |
| 889 |
sk_sp<VulkanSampler> vulkanSampler = |
| 890 |
sk_ref_sp<VulkanSampler>(static_cast<VulkanSampler*>(immutableSampler.get())); |
| 891 |
SkASSERT(vulkanSampler); |
| 892 |
immutableSamplers[i] = std::move(vulkanSampler); |
| 893 |
} |
| 894 |
} |
| 895 |
|
| 896 |
auto program = VulkanProgramInfo::Make(sharedContext); |
| 897 |
|
| 898 |
const std::string& fsSkSL = shaderInfo->fragmentSkSL(); |
| 899 |
const bool hasFragmentSkSL = !fsSkSL.empty(); |
| 900 |
SkSL::NativeShader vsSPIRV, fsSPIRV; |
| 901 |
SkSL::Program::Interface vsInterface, fsInterface; |
| 902 |
if (hasFragmentSkSL) { |
| 903 |
if (!skgpu::SkSLToSPIRV(sharedContext->caps()->shaderCaps(), |
| 904 |
fsSkSL, |
| 905 |
SkSL::ProgramKind::kGraphiteFragment, |
| 906 |
settings, |
| 907 |
&fsSPIRV, |
| 908 |
&fsInterface, |
| 909 |
errorHandler)) { |
| 910 |
return nullptr; |
| 911 |
} |
| 912 |
|
| 913 |
// Apply transformations to the fragment shader SPIR-V if needed. |
| 914 |
// |
| 915 |
// SkSL->SPIR-V compilations are relatively costly. In anticipation of caching those |
| 916 |
// operations, we allow certain transformations to be applied directly on to compiled |
| 917 |
// SPIR-V. This allows the SkSL to be compiled only once, and the SPIR-V from the cache can |
| 918 |
// simply be directly modified afterwards as needed. This is why transform options exist |
| 919 |
// independently from flags used in SkSL->SPIRV compilation. |
| 920 |
SPIRVTransformOptions options; |
| 921 |
options.fMultisampleInputLoad = |
| 922 |
renderPassDesc.fSampleCount > SampleCount::k1 && |
| 923 |
shaderInfo->dstReadStrategy() == DstReadStrategy::kReadFromInput; |
| 924 |
if (options.fMultisampleInputLoad) { |
| 925 |
fsSPIRV = TransformSPIRV(fsSPIRV, options); |
| 926 |
} |
| 927 |
|
| 928 |
if(!program->setFragmentShader(CreateVulkanShaderModule( |
| 929 |
sharedContext, fsSPIRV, VK_SHADER_STAGE_FRAGMENT_BIT))) { |
| 930 |
return nullptr; |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
const std::string& vsSkSL = shaderInfo->vertexSkSL(); |
| 935 |
if (!skgpu::SkSLToSPIRV(sharedContext->caps()->shaderCaps(), |
| 936 |
vsSkSL, |
| 937 |
SkSL::ProgramKind::kGraphiteVertex, |
| 938 |
settings, |
| 939 |
&vsSPIRV, |
| 940 |
&vsInterface, |
| 941 |
errorHandler)) { |
| 942 |
return nullptr; |
| 943 |
} |
| 944 |
if (!program->setVertexShader(CreateVulkanShaderModule( |
| 945 |
sharedContext, vsSPIRV, VK_SHADER_STAGE_VERTEX_BIT))) { |
| 946 |
return nullptr; |
| 947 |
} |
| 948 |
|
| 949 |
// TODO: Query RenderPassDesc for input attachment information. For now, we only use one for |
| 950 |
// loading MSAA from resolve so we can simply pass in 0 when not doing that. |
| 951 |
if (!program->setLayout(setup_pipeline_layout( |
| 952 |
sharedContext, |
| 953 |
VulkanResourceProvider::kIntrinsicConstantSize, |
| 954 |
VulkanResourceProvider::kIntrinsicConstantStageFlags, |
| 955 |
shaderInfo->hasCombinedUniforms(), |
| 956 |
shaderInfo->hasGradientBuffer(), |
| 957 |
shaderInfo->numFragmentTexturesAndSamplers(), |
| 958 |
/*loadMsaaFromResolve=*/false, |
| 959 |
SkSpan<sk_sp<VulkanSampler>>(immutableSamplers)))) { |
| 960 |
return nullptr; |
| 961 |
} |
| 962 |
|
| 963 |
// This pipeline factory is for regular pipelines that run in the main subpass. Depending on |
| 964 |
// if there will be a load-MSAA subpass, the index changes. Ideally with |
| 965 |
// VK_dynamic_rendering_local_read, we can reuse pipelines across subpasses for layer elision. |
| 966 |
int subpassIndex = RenderPassDescWillLoadMSAAFromResolve(renderPassDesc) ? 1 : 0; |
| 967 |
VertexInputBindingDescriptions vertexBindingDescriptions; |
| 968 |
VertexInputAttributeDescriptions vertexAttributeDescriptions; |
| 969 |
VkPipeline shadersPipeline = VK_NULL_HANDLE; |
| 970 |
VkPipeline vkPipeline = MakePipeline( |
| 971 |
sharedContext, |
| 972 |
*program, |
| 973 |
subpassIndex, |
| 974 |
step->primitiveType(), |
| 975 |
step->appendsVertices() ? VK_VERTEX_INPUT_RATE_VERTEX : VK_VERTEX_INPUT_RATE_INSTANCE, |
| 976 |
step->staticAttributes(), |
| 977 |
step->appendAttributes(), |
| 978 |
vertexBindingDescriptions, |
| 979 |
vertexAttributeDescriptions, |
| 980 |
step->depthStencilSettings(), |
| 981 |
shaderInfo->blendInfo(), |
| 982 |
renderPassDesc, |
| 983 |
&shadersPipeline); |
| 984 |
|
| 985 |
sk_sp<VulkanGraphicsPipeline> pipeline; |
| 986 |
if (vkPipeline) { |
| 987 |
PipelineInfo pipelineInfo{ *shaderInfo, pipelineCreationFlags, |
| 988 |
pipelineKey.hash(), compilationID }; |
| 989 |
#if defined(GPU_TEST_UTILS) |
| 990 |
pipelineInfo.fNativeVertexShader = SkShaderUtils::SpirvAsHexStream(vsSPIRV.fBinary); |
| 991 |
pipelineInfo.fNativeFragmentShader = SkShaderUtils::SpirvAsHexStream(fsSPIRV.fBinary); |
| 992 |
#endif |
| 993 |
|
| 994 |
pipeline = sk_sp<VulkanGraphicsPipeline>( |
| 995 |
new VulkanGraphicsPipeline(sharedContext, |
| 996 |
pipelineInfo, |
| 997 |
shaderInfo->pipelineLabel(), |
| 998 |
program->releaseLayout(), |
| 999 |
vkPipeline, |
| 1000 |
shadersPipeline, |
| 1001 |
/*ownsPipelineLayout=*/true, |
| 1002 |
std::move(immutableSamplers), |
| 1003 |
step->renderStepID(), |
| 1004 |
step->primitiveType(), |
| 1005 |
step->depthStencilSettings(), |
| 1006 |
std::move(vertexBindingDescriptions), |
| 1007 |
std::move(vertexAttributeDescriptions))); |
| 1008 |
} |
| 1009 |
|
| 1010 |
return pipeline; |
| 1011 |
} |
| 1012 |
|
| 1013 |
VkPipeline VulkanGraphicsPipeline::MakePipeline( |
| 1014 |
VulkanSharedContext* sharedContext, |
| 1015 |
const VulkanProgramInfo& program, |
| 1016 |
int subpassIndex, |
| 1017 |
PrimitiveType primitiveType, |
| 1018 |
VkVertexInputRate appendInputRate, |
| 1019 |
SkSpan<const Attribute> staticAttrs, |
| 1020 |
SkSpan<const Attribute> appendAttrs, |
| 1021 |
VertexInputBindingDescriptions& vertexBindingDescriptions, |
| 1022 |
VertexInputAttributeDescriptions& vertexAttributeDescriptions, |
| 1023 |
const DepthStencilSettings& depthStencilSettings, |
| 1024 |
const BlendInfo& blendInfo, |
| 1025 |
const RenderPassDesc& renderPassDesc, |
| 1026 |
VkPipeline* shadersPipeline) { |
| 1027 |
SkASSERT(program.layout() && program.vs()); // but a fragment shader isn't required |
| 1028 |
|
| 1029 |
VulkanThreadSafeResourceProvider* threadSafeResourceProvider = |
| 1030 |
sharedContext->threadSafeResourceProvider(); |
| 1031 |
|
| 1032 |
VkPipelineVertexInputStateCreateInfo vertexInputInfo; |
| 1033 |
skia_private::STArray<2, VkVertexInputBindingDescription> bindingDescs; |
| 1034 |
skia_private::STArray<16, VkVertexInputAttributeDescription> attributeDescs; |
| 1035 |
if (sharedContext->caps()->useVertexInputDynamicState()) { |
| 1036 |
VkVertexInputBindingDescription2EXT defaultBindingDesc = {}; |
| 1037 |
defaultBindingDesc.sType = VK_STRUCTURE_TYPE_VERTEX_INPUT_BINDING_DESCRIPTION_2_EXT; |
| 1038 |
defaultBindingDesc.divisor = 1; |
| 1039 |
|
| 1040 |
VkVertexInputAttributeDescription2EXT defaultAttributeDesc = {}; |
| 1041 |
defaultAttributeDesc.sType = VK_STRUCTURE_TYPE_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION_2_EXT; |
| 1042 |
|
| 1043 |
get_vertex_input_state(appendInputRate, |
| 1044 |
staticAttrs, |
| 1045 |
appendAttrs, |
| 1046 |
vertexBindingDescriptions, |
| 1047 |
vertexAttributeDescriptions, |
| 1048 |
defaultBindingDesc, |
| 1049 |
defaultAttributeDesc); |
| 1050 |
} else { |
| 1051 |
setup_vertex_input_state(appendInputRate, |
| 1052 |
staticAttrs, |
| 1053 |
appendAttrs, |
| 1054 |
&vertexInputInfo, |
| 1055 |
bindingDescs, |
| 1056 |
attributeDescs); |
| 1057 |
} |
| 1058 |
|
| 1059 |
VkPipelineInputAssemblyStateCreateInfo inputAssemblyInfo; |
| 1060 |
setup_input_assembly_state(*sharedContext->caps(), primitiveType, &inputAssemblyInfo); |
| 1061 |
|
| 1062 |
VkPipelineDepthStencilStateCreateInfo depthStencilInfo; |
| 1063 |
setup_depth_stencil_state(*sharedContext->caps(), depthStencilSettings, &depthStencilInfo); |
| 1064 |
|
| 1065 |
VkPipelineViewportStateCreateInfo viewportInfo; |
| 1066 |
setup_viewport_scissor_state(&viewportInfo); |
| 1067 |
|
| 1068 |
VkPipelineMultisampleStateCreateInfo multisampleInfo; |
| 1069 |
setup_multisample_state(renderPassDesc.fSampleCount, &multisampleInfo); |
| 1070 |
|
| 1071 |
// We will only have one color blend attachment per pipeline. |
| 1072 |
VkPipelineColorBlendAttachmentState attachmentStates[1]; |
| 1073 |
VkPipelineColorBlendStateCreateInfo colorBlendInfo; |
| 1074 |
setup_color_blend_state( |
| 1075 |
sharedContext->vulkanCaps(), blendInfo, &colorBlendInfo, attachmentStates); |
| 1076 |
|
| 1077 |
VkPipelineRasterizationStateCreateInfo rasterInfo; |
| 1078 |
// TODO: Check for wire frame mode once that is an available context option within graphite. |
| 1079 |
setup_raster_state(*sharedContext->caps(), /*isWireframe=*/false, &rasterInfo); |
| 1080 |
|
| 1081 |
VkPipelineShaderStageCreateInfo pipelineShaderStages[2]; |
| 1082 |
setup_shader_stage_info(VK_SHADER_STAGE_VERTEX_BIT, |
| 1083 |
program.vs(), |
| 1084 |
&pipelineShaderStages[0]); |
| 1085 |
if (program.fs()) { |
| 1086 |
setup_shader_stage_info(VK_SHADER_STAGE_FRAGMENT_BIT, |
| 1087 |
program.fs(), |
| 1088 |
&pipelineShaderStages[1]); |
| 1089 |
} |
| 1090 |
|
| 1091 |
VkDynamicStateList dynamicStates; |
| 1092 |
VkPipelineDynamicStateCreateInfo dynamicInfo; |
| 1093 |
setup_dynamic_state(*sharedContext->caps(), &dynamicInfo, &dynamicStates); |
| 1094 |
|
| 1095 |
sk_sp<VulkanRenderPass> compatibleRenderPass = |
| 1096 |
threadSafeResourceProvider->findOrCreateRenderPass(renderPassDesc, |
| 1097 |
/*compatibleOnly=*/true); |
| 1098 |
if (!compatibleRenderPass) { |
| 1099 |
SKGPU_LOG_E("Failed to create compatible renderpass for pipeline"); |
| 1100 |
return VK_NULL_HANDLE; |
| 1101 |
} |
| 1102 |
SkDEBUGCODE(int subpassCount = RenderPassDescWillLoadMSAAFromResolve(renderPassDesc) ? 2 : 1;) |
| 1103 |
SkASSERT(subpassIndex >= 0 && subpassIndex < subpassCount); |
| 1104 |
|
| 1105 |
VkGraphicsPipelineCreateInfo pipelineCreateInfo = {}; |
| 1106 |
pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
| 1107 |
pipelineCreateInfo.stageCount = program.fs() ? 2 : 1; |
| 1108 |
pipelineCreateInfo.pStages = &pipelineShaderStages[0]; |
| 1109 |
pipelineCreateInfo.pVertexInputState = |
| 1110 |
sharedContext->caps()->useVertexInputDynamicState() ? nullptr : &vertexInputInfo; |
| 1111 |
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyInfo; |
| 1112 |
pipelineCreateInfo.pTessellationState = nullptr; |
| 1113 |
pipelineCreateInfo.pViewportState = &viewportInfo; |
| 1114 |
pipelineCreateInfo.pRasterizationState = &rasterInfo; |
| 1115 |
pipelineCreateInfo.pMultisampleState = &multisampleInfo; |
| 1116 |
pipelineCreateInfo.pDepthStencilState = &depthStencilInfo; |
| 1117 |
pipelineCreateInfo.pColorBlendState = &colorBlendInfo; |
| 1118 |
pipelineCreateInfo.pDynamicState = &dynamicInfo; |
| 1119 |
pipelineCreateInfo.layout = program.layout(); |
| 1120 |
pipelineCreateInfo.renderPass = compatibleRenderPass->renderPass(); |
| 1121 |
pipelineCreateInfo.subpass = subpassIndex; |
| 1122 |
pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE; |
| 1123 |
pipelineCreateInfo.basePipelineIndex = -1; |
| 1124 |
|
| 1125 |
VkPipelineLibraryCreateInfoKHR shadersLibraryInfo = {}; |
| 1126 |
shadersLibraryInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LIBRARY_CREATE_INFO_KHR; |
| 1127 |
|
| 1128 |
VkGraphicsPipelineLibraryCreateInfoEXT libraryInfo = {}; |
| 1129 |
libraryInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_LIBRARY_CREATE_INFO_EXT; |
| 1130 |
|
| 1131 |
if (shadersPipeline && sharedContext->caps()->usePipelineLibraries()) { |
| 1132 |
// When using VK_EXT_graphics_pipeline_library, create the "shaders" subset of the pipeline, |
| 1133 |
// and then create the full pipeline (with the vertex input and fragment output state) |
| 1134 |
// immediately afterwards using the shaders library, which is a cheap operation. |
| 1135 |
// This is typically what vendors without dynamic blend state do in their GL drivers. |
| 1136 |
// |
| 1137 |
// This can be further optimized by the front-end creating fewer shaders pipelines, and only |
| 1138 |
// create multiple full pipelines out of them as needed. |
| 1139 |
if (!create_shaders_pipeline(sharedContext, |
| 1140 |
pipelineCreateInfo, |
| 1141 |
shadersLibraryInfo, |
| 1142 |
libraryInfo, |
| 1143 |
shadersPipeline)) { |
| 1144 |
return VK_NULL_HANDLE; |
| 1145 |
} |
| 1146 |
} |
| 1147 |
|
| 1148 |
return create_graphics_pipeline(sharedContext, &pipelineCreateInfo); |
| 1149 |
} |
| 1150 |
|
| 1151 |
std::unique_ptr<VulkanProgramInfo> VulkanGraphicsPipeline::CreateLoadMSAAProgram( |
| 1152 |
const VulkanSharedContext* sharedContext) { |
| 1153 |
SkSL::ProgramSettings settings; |
| 1154 |
settings.fForceNoRTFlip = true; |
| 1155 |
SkSL::NativeShader vsSPIRV, fsSPIRV; |
| 1156 |
ShaderErrorHandler* errorHandler = sharedContext->caps()->shaderErrorHandler(); |
| 1157 |
|
| 1158 |
std::string vertShaderText; |
| 1159 |
vertShaderText.append( |
| 1160 |
"layout(vulkan, push_constant) uniform vertexUniformBuffer {" |
| 1161 |
"half4 uPosXform;" |
| 1162 |
"};" |
| 1163 |
|
| 1164 |
// MSAA Load Program VS |
| 1165 |
"void main() {" |
| 1166 |
"float2 position = float2(sk_VertexID >> 1, sk_VertexID & 1);" |
| 1167 |
"sk_Position.xy = position * uPosXform.xy + uPosXform.zw;" |
| 1168 |
"sk_Position.zw = half2(0, 1);" |
| 1169 |
"}"); |
| 1170 |
|
| 1171 |
std::string fragShaderText; |
| 1172 |
fragShaderText.append( |
| 1173 |
"layout(vulkan, input_attachment_index=0, set=" + |
| 1174 |
std::to_string(VulkanGraphicsPipeline::kLoadMsaaFromResolveInputDescSetIndex) + |
| 1175 |
", binding=0) subpassInput uInput;" |
| 1176 |
|
| 1177 |
// MSAA Load Program FS |
| 1178 |
"void main() {" |
| 1179 |
"sk_FragColor = subpassLoad(uInput);" |
| 1180 |
"}"); |
| 1181 |
|
| 1182 |
auto program = VulkanProgramInfo::Make(sharedContext); |
| 1183 |
|
| 1184 |
SkSL::Program::Interface vsInterface, fsInterface; |
| 1185 |
if (!skgpu::SkSLToSPIRV(sharedContext->caps()->shaderCaps(), |
| 1186 |
vertShaderText, |
| 1187 |
SkSL::ProgramKind::kGraphiteVertex, |
| 1188 |
settings, |
| 1189 |
&vsSPIRV, |
| 1190 |
&vsInterface, |
| 1191 |
errorHandler)) { |
| 1192 |
return nullptr; |
| 1193 |
} |
| 1194 |
if (!program->setVertexShader(CreateVulkanShaderModule( |
| 1195 |
sharedContext, vsSPIRV, VK_SHADER_STAGE_VERTEX_BIT))) { |
| 1196 |
return nullptr; |
| 1197 |
} |
| 1198 |
|
| 1199 |
if (!skgpu::SkSLToSPIRV(sharedContext->caps()->shaderCaps(), |
| 1200 |
fragShaderText, |
| 1201 |
SkSL::ProgramKind::kGraphiteFragment, |
| 1202 |
settings, |
| 1203 |
&fsSPIRV, |
| 1204 |
&fsInterface, |
| 1205 |
errorHandler)) { |
| 1206 |
return nullptr; |
| 1207 |
} |
| 1208 |
if (!program->setFragmentShader(CreateVulkanShaderModule( |
| 1209 |
sharedContext, fsSPIRV, VK_SHADER_STAGE_FRAGMENT_BIT))) { |
| 1210 |
return nullptr; |
| 1211 |
} |
| 1212 |
|
| 1213 |
// The load msaa pipeline takes no step or paint uniforms and no instance attributes. It only |
| 1214 |
// references one input attachment texture (which does not require a sampler) and one vertex |
| 1215 |
// attribute (NDC position) |
| 1216 |
skia_private::TArray<DescriptorData> inputAttachmentDescriptors(1); |
| 1217 |
inputAttachmentDescriptors.push_back(VulkanGraphicsPipeline::kInputAttachmentDescriptor); |
| 1218 |
// This pipeline is used to read from the resolve attachment to a color attachment. We should |
| 1219 |
// never require an immutable sampler for this, since that would imply that we are rendering to |
| 1220 |
// a surface with an external format. |
| 1221 |
if (!program->setLayout(setup_pipeline_layout( |
| 1222 |
sharedContext, |
| 1223 |
/*pushConstantSize=*/32, |
| 1224 |
(VkShaderStageFlagBits)VK_SHADER_STAGE_VERTEX_BIT, |
| 1225 |
/*hasCombinedUniforms=*/false, |
| 1226 |
/*hasGradientBuffer=*/false, |
| 1227 |
/*numTextureSamplers=*/0, |
| 1228 |
/*loadMsaaFromResolve=*/true, |
| 1229 |
/*immutableSamplers=*/{}))) { |
| 1230 |
return nullptr; |
| 1231 |
} |
| 1232 |
|
| 1233 |
return program; |
| 1234 |
} |
| 1235 |
|
| 1236 |
sk_sp<VulkanGraphicsPipeline> VulkanGraphicsPipeline::MakeLoadMSAAPipeline( |
| 1237 |
VulkanSharedContext* sharedContext, |
| 1238 |
const VulkanProgramInfo& loadMSAAProgram, |
| 1239 |
const RenderPassDesc& renderPassDesc) { |
| 1240 |
// The load MSAA pipeline does not have any vertex or instance attributes, does not use the |
| 1241 |
// depth or stencil attachments. Unlike the general Make factory, we do not destroy any of the |
| 1242 |
// shader modules or layout. The pipeline layout will not be owned by the created |
| 1243 |
// VulkanGraphicsPipeline either, as it's owned by the resource provider. |
| 1244 |
SkASSERT(RenderPassDescWillLoadMSAAFromResolve(renderPassDesc)); |
| 1245 |
VertexInputBindingDescriptions vertexBindingDescriptions; |
| 1246 |
VertexInputAttributeDescriptions vertexAttributeDescriptions; |
| 1247 |
VkPipeline vkPipeline = MakePipeline(sharedContext, |
| 1248 |
loadMSAAProgram, |
| 1249 |
/*subpassIndex=*/0, // loading to MSAA is always first |
| 1250 |
PrimitiveType::kTriangleStrip, |
| 1251 |
/*appendInputRate=*/{}, |
| 1252 |
/*staticAttrs=*/{}, |
| 1253 |
/*appendAttrs=*/{}, |
| 1254 |
vertexBindingDescriptions, |
| 1255 |
vertexAttributeDescriptions, |
| 1256 |
/*depthStencilSettings=*/{}, |
| 1257 |
/*blendInfo=*/{}, |
| 1258 |
renderPassDesc, |
| 1259 |
/*shadersPipeline=*/nullptr); |
| 1260 |
if (!vkPipeline) { |
| 1261 |
return nullptr; |
| 1262 |
} |
| 1263 |
|
| 1264 |
SkASSERT(vertexBindingDescriptions.empty()); |
| 1265 |
SkASSERT(vertexAttributeDescriptions.empty()); |
| 1266 |
|
| 1267 |
std::string pipelineLabel = "LoadMSAAFromResolve + "; |
| 1268 |
pipelineLabel += renderPassDesc.toString().c_str(); |
| 1269 |
return sk_sp<VulkanGraphicsPipeline>( |
| 1270 |
new VulkanGraphicsPipeline(sharedContext, |
| 1271 |
/*pipelineInfo=*/{}, // leave empty for an internal pipeline |
| 1272 |
pipelineLabel, |
| 1273 |
loadMSAAProgram.layout(), |
| 1274 |
vkPipeline, |
| 1275 |
/*shadersPipeline=*/VK_NULL_HANDLE, |
| 1276 |
/*ownsPipelineLayout=*/false, |
| 1277 |
/*immutableSamplers=*/{}, |
| 1278 |
RenderStep::RenderStepID::kInvalid, |
| 1279 |
PrimitiveType::kTriangleStrip, |
| 1280 |
/*depthStencilSettings=*/{}, |
| 1281 |
/*vertexBindingDescriptions=*/{}, |
| 1282 |
/*vertexAttributeDescriptions=*/{})); |
| 1283 |
} |
| 1284 |
|
| 1285 |
VulkanGraphicsPipeline::VulkanGraphicsPipeline( |
| 1286 |
const VulkanSharedContext* sharedContext, |
| 1287 |
const PipelineInfo& pipelineInfo, |
| 1288 |
std::string_view pipelineLabel, |
| 1289 |
VkPipelineLayout pipelineLayout, |
| 1290 |
VkPipeline pipeline, |
| 1291 |
VkPipeline shadersPipeline, |
| 1292 |
bool ownsPipelineLayout, |
| 1293 |
skia_private::TArray<sk_sp<VulkanSampler>>&& immutableSamplers, |
| 1294 |
RenderStep::RenderStepID renderStepID, |
| 1295 |
PrimitiveType primitiveType, |
| 1296 |
const DepthStencilSettings& depthStencilSettings, |
| 1297 |
VertexInputBindingDescriptions&& vertexBindingDescriptions, |
| 1298 |
VertexInputAttributeDescriptions&& vertexAttributeDescriptions) |
| 1299 |
: GraphicsPipeline(sharedContext, pipelineInfo, pipelineLabel) |
| 1300 |
, fPipelineLayout(pipelineLayout) |
| 1301 |
, fPipeline(pipeline) |
| 1302 |
, fShadersPipeline(shadersPipeline) |
| 1303 |
, fOwnsPipelineLayout(ownsPipelineLayout) |
| 1304 |
, fImmutableSamplers(std::move(immutableSamplers)) |
| 1305 |
, fPrimitiveType(primitiveType) |
| 1306 |
, fDepthStencilSettings(depthStencilSettings) |
| 1307 |
, fRenderStepID(renderStepID) |
| 1308 |
, fVertexBindingDescriptions(std::move(vertexBindingDescriptions)) |
| 1309 |
, fVertexAttributeDescriptions(std::move(vertexAttributeDescriptions)) {} |
| 1310 |
|
| 1311 |
void VulkanGraphicsPipeline::freeGpuData() { |
| 1312 |
auto sharedCtxt = static_cast<const VulkanSharedContext*>(this->sharedContext()); |
| 1313 |
if (fShadersPipeline != VK_NULL_HANDLE) { |
| 1314 |
VULKAN_CALL(sharedCtxt->interface(), |
| 1315 |
DestroyPipeline(sharedCtxt->device(), fShadersPipeline, nullptr)); |
| 1316 |
} |
| 1317 |
if (fPipeline != VK_NULL_HANDLE) { |
| 1318 |
VULKAN_CALL(sharedCtxt->interface(), |
| 1319 |
DestroyPipeline(sharedCtxt->device(), fPipeline, nullptr)); |
| 1320 |
} |
| 1321 |
if (fOwnsPipelineLayout && fPipelineLayout != VK_NULL_HANDLE) { |
| 1322 |
VULKAN_CALL(sharedCtxt->interface(), |
| 1323 |
DestroyPipelineLayout(sharedCtxt->device(), fPipelineLayout, nullptr)); |
| 1324 |
} |
| 1325 |
} |
| 1326 |
|
| 1327 |
void VulkanGraphicsPipeline::updateDynamicState(const VulkanSharedContext* sharedContext, |
| 1328 |
VkCommandBuffer commandBuffer, |
| 1329 |
const VulkanGraphicsPipeline* previous) const { |
| 1330 |
// The front-end currently is unaware of dynamic state, so we have no choice but to calculate |
| 1331 |
// the diff between pipelines and update the state as needed. This is not quite so efficient, |
| 1332 |
// but will need awareness from the front-end to optimize. |
| 1333 |
if (sharedContext->caps()->useBasicDynamicState()) { |
| 1334 |
const DepthStencilSettings& depthStencil = fDepthStencilSettings; |
| 1335 |
const DepthStencilSettings::Face& frontStencil = depthStencil.fFrontStencil; |
| 1336 |
const DepthStencilSettings::Face& backStencil = depthStencil.fBackStencil; |
| 1337 |
|
| 1338 |
const DepthStencilSettings* pastDepthStencil = |
| 1339 |
previous ? &previous->fDepthStencilSettings : nullptr; |
| 1340 |
const DepthStencilSettings::Face* pastFrontStencil = |
| 1341 |
previous ? &pastDepthStencil->fFrontStencil : nullptr; |
| 1342 |
const DepthStencilSettings::Face* pastBackStencil = |
| 1343 |
previous ? &pastDepthStencil->fBackStencil : nullptr; |
| 1344 |
|
| 1345 |
const bool frontStencilReadMaskDirty = |
| 1346 |
previous == nullptr || pastFrontStencil->fReadMask != frontStencil.fReadMask; |
| 1347 |
if (frontStencilReadMaskDirty) { |
| 1348 |
VULKAN_CALL(sharedContext->interface(), |
| 1349 |
CmdSetStencilCompareMask( |
| 1350 |
commandBuffer, VK_STENCIL_FACE_FRONT_BIT, frontStencil.fReadMask)); |
| 1351 |
} |
| 1352 |
const bool backStencilReadMaskDirty = |
| 1353 |
previous == nullptr || pastBackStencil->fReadMask != backStencil.fReadMask; |
| 1354 |
if (backStencilReadMaskDirty) { |
| 1355 |
VULKAN_CALL(sharedContext->interface(), |
| 1356 |
CmdSetStencilCompareMask( |
| 1357 |
commandBuffer, VK_STENCIL_FACE_BACK_BIT, backStencil.fReadMask)); |
| 1358 |
} |
| 1359 |
const bool frontStencilWriteMaskDirty = |
| 1360 |
previous == nullptr || pastFrontStencil->fWriteMask != frontStencil.fWriteMask; |
| 1361 |
if (frontStencilWriteMaskDirty) { |
| 1362 |
VULKAN_CALL(sharedContext->interface(), |
| 1363 |
CmdSetStencilWriteMask( |
| 1364 |
commandBuffer, VK_STENCIL_FACE_FRONT_BIT, frontStencil.fWriteMask)); |
| 1365 |
} |
| 1366 |
const bool backStencilWriteMaskDirty = |
| 1367 |
previous == nullptr || pastBackStencil->fWriteMask != backStencil.fWriteMask; |
| 1368 |
if (backStencilWriteMaskDirty) { |
| 1369 |
VULKAN_CALL(sharedContext->interface(), |
| 1370 |
CmdSetStencilWriteMask( |
| 1371 |
commandBuffer, VK_STENCIL_FACE_BACK_BIT, backStencil.fWriteMask)); |
| 1372 |
} |
| 1373 |
const bool stencilReferenceDirty = |
| 1374 |
previous == nullptr || |
| 1375 |
pastDepthStencil->fStencilReferenceValue != depthStencil.fStencilReferenceValue; |
| 1376 |
if (stencilReferenceDirty) { |
| 1377 |
VULKAN_CALL(sharedContext->interface(), |
| 1378 |
CmdSetStencilReference(commandBuffer, |
| 1379 |
VK_STENCIL_FACE_FRONT_AND_BACK, |
| 1380 |
depthStencil.fStencilReferenceValue)); |
| 1381 |
} |
| 1382 |
const bool depthCompareOpDirty = |
| 1383 |
previous == nullptr || |
| 1384 |
pastDepthStencil->fDepthCompareOp != depthStencil.fDepthCompareOp; |
| 1385 |
if (depthCompareOpDirty) { |
| 1386 |
VULKAN_CALL(sharedContext->interface(), |
| 1387 |
CmdSetDepthCompareOp( |
| 1388 |
commandBuffer, |
| 1389 |
compare_op_to_vk_compare_op(depthStencil.fDepthCompareOp))); |
| 1390 |
} |
| 1391 |
const bool depthTestEnabledDirty = |
| 1392 |
previous == nullptr || |
| 1393 |
pastDepthStencil->fDepthTestEnabled != depthStencil.fDepthTestEnabled; |
| 1394 |
if (depthTestEnabledDirty) { |
| 1395 |
VULKAN_CALL(sharedContext->interface(), |
| 1396 |
CmdSetDepthTestEnable(commandBuffer, depthStencil.fDepthTestEnabled)); |
| 1397 |
} |
| 1398 |
const bool depthWriteEnabledDirty = |
| 1399 |
previous == nullptr || |
| 1400 |
pastDepthStencil->fDepthWriteEnabled != depthStencil.fDepthWriteEnabled; |
| 1401 |
if (depthWriteEnabledDirty) { |
| 1402 |
VULKAN_CALL(sharedContext->interface(), |
| 1403 |
CmdSetDepthWriteEnable(commandBuffer, depthStencil.fDepthWriteEnabled)); |
| 1404 |
} |
| 1405 |
const bool stencilTestEnabledDirty = |
| 1406 |
previous == nullptr || |
| 1407 |
pastDepthStencil->fStencilTestEnabled != depthStencil.fStencilTestEnabled; |
| 1408 |
if (stencilTestEnabledDirty) { |
| 1409 |
VULKAN_CALL(sharedContext->interface(), |
| 1410 |
CmdSetStencilTestEnable(commandBuffer, depthStencil.fStencilTestEnabled)); |
| 1411 |
} |
| 1412 |
const bool frontStencilOpsDirty = |
| 1413 |
previous == nullptr || |
| 1414 |
pastFrontStencil->fDepthStencilPassOp != frontStencil.fDepthStencilPassOp || |
| 1415 |
pastFrontStencil->fStencilFailOp != frontStencil.fStencilFailOp || |
| 1416 |
pastFrontStencil->fDepthFailOp != frontStencil.fDepthFailOp || |
| 1417 |
pastFrontStencil->fCompareOp != frontStencil.fCompareOp; |
| 1418 |
if (frontStencilOpsDirty) { |
| 1419 |
VULKAN_CALL( |
| 1420 |
sharedContext->interface(), |
| 1421 |
CmdSetStencilOp(commandBuffer, |
| 1422 |
VK_STENCIL_FACE_FRONT_BIT, |
| 1423 |
/*failOp=*/ |
| 1424 |
stencil_op_to_vk_stencil_op(frontStencil.fStencilFailOp), |
| 1425 |
/*passOp=*/ |
| 1426 |
stencil_op_to_vk_stencil_op(frontStencil.fDepthStencilPassOp), |
| 1427 |
/*depthFailOp=*/ |
| 1428 |
stencil_op_to_vk_stencil_op(frontStencil.fDepthFailOp), |
| 1429 |
/*compareOp=*/ |
| 1430 |
compare_op_to_vk_compare_op(frontStencil.fCompareOp))); |
| 1431 |
} |
| 1432 |
const bool backStencilOpsDirty = |
| 1433 |
previous == nullptr || |
| 1434 |
pastBackStencil->fDepthStencilPassOp != backStencil.fDepthStencilPassOp || |
| 1435 |
pastBackStencil->fStencilFailOp != backStencil.fStencilFailOp || |
| 1436 |
pastBackStencil->fDepthFailOp != backStencil.fDepthFailOp || |
| 1437 |
pastBackStencil->fCompareOp != backStencil.fCompareOp; |
| 1438 |
if (backStencilOpsDirty) { |
| 1439 |
VULKAN_CALL( |
| 1440 |
sharedContext->interface(), |
| 1441 |
CmdSetStencilOp(commandBuffer, |
| 1442 |
VK_STENCIL_FACE_BACK_BIT, |
| 1443 |
/*failOp=*/ |
| 1444 |
stencil_op_to_vk_stencil_op(backStencil.fStencilFailOp), |
| 1445 |
/*passOp=*/ |
| 1446 |
stencil_op_to_vk_stencil_op(backStencil.fDepthStencilPassOp), |
| 1447 |
/*depthFailOp=*/ |
| 1448 |
stencil_op_to_vk_stencil_op(backStencil.fDepthFailOp), |
| 1449 |
/*compareOp=*/ |
| 1450 |
compare_op_to_vk_compare_op(backStencil.fCompareOp))); |
| 1451 |
} |
| 1452 |
const bool primitiveTypeDirty = |
| 1453 |
previous == nullptr || previous->fPrimitiveType != fPrimitiveType; |
| 1454 |
if (primitiveTypeDirty) { |
| 1455 |
VULKAN_CALL(sharedContext->interface(), |
| 1456 |
CmdSetPrimitiveTopology(commandBuffer, |
| 1457 |
primitive_type_to_vk_topology(fPrimitiveType))); |
| 1458 |
} |
| 1459 |
} |
| 1460 |
if (sharedContext->caps()->useVertexInputDynamicState()) { |
| 1461 |
const bool vertexInputDirty = |
| 1462 |
previous == nullptr || previous->fRenderStepID != fRenderStepID; |
| 1463 |
|
| 1464 |
if (vertexInputDirty) { |
| 1465 |
VULKAN_CALL(sharedContext->interface(), |
| 1466 |
CmdSetVertexInput(commandBuffer, |
| 1467 |
fVertexBindingDescriptions.size(), |
| 1468 |
fVertexBindingDescriptions.begin(), |
| 1469 |
fVertexAttributeDescriptions.size(), |
| 1470 |
fVertexAttributeDescriptions.begin())); |
| 1471 |
} |
| 1472 |
} |
| 1473 |
} |
| 1474 |
|
| 1475 |
} // namespace skgpu::graphite |