Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 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 | |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 8 | #include "src/gpu/vk/GrVkPipeline.h" |
| 9 | |
Brian Osman | 7a20b5c | 2021-03-15 16:23:33 -0400 | [diff] [blame] | 10 | #include "src/core/SkTraceEvent.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrGeometryProcessor.h" |
| 12 | #include "src/gpu/GrPipeline.h" |
| 13 | #include "src/gpu/GrStencilSettings.h" |
| 14 | #include "src/gpu/vk/GrVkCommandBuffer.h" |
| 15 | #include "src/gpu/vk/GrVkGpu.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/vk/GrVkRenderTarget.h" |
| 17 | #include "src/gpu/vk/GrVkUtil.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 18 | |
Ben Wagner | 6c30e74 | 2019-02-06 10:46:14 -0500 | [diff] [blame] | 19 | #if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS) |
| 20 | #include <sanitizer/lsan_interface.h> |
| 21 | #endif |
| 22 | |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 23 | static inline VkFormat attrib_type_to_vkformat(GrVertexAttribType type) { |
| 24 | switch (type) { |
| 25 | case kFloat_GrVertexAttribType: |
| 26 | return VK_FORMAT_R32_SFLOAT; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 27 | case kFloat2_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 28 | return VK_FORMAT_R32G32_SFLOAT; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 29 | case kFloat3_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 30 | return VK_FORMAT_R32G32B32_SFLOAT; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 31 | case kFloat4_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 32 | return VK_FORMAT_R32G32B32A32_SFLOAT; |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 33 | case kHalf_GrVertexAttribType: |
| 34 | return VK_FORMAT_R16_SFLOAT; |
| 35 | case kHalf2_GrVertexAttribType: |
| 36 | return VK_FORMAT_R16G16_SFLOAT; |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 37 | case kHalf4_GrVertexAttribType: |
| 38 | return VK_FORMAT_R16G16B16A16_SFLOAT; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 39 | case kInt2_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 40 | return VK_FORMAT_R32G32_SINT; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 41 | case kInt3_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 42 | return VK_FORMAT_R32G32B32_SINT; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 43 | case kInt4_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 44 | return VK_FORMAT_R32G32B32A32_SINT; |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 45 | case kByte_GrVertexAttribType: |
| 46 | return VK_FORMAT_R8_SINT; |
| 47 | case kByte2_GrVertexAttribType: |
| 48 | return VK_FORMAT_R8G8_SINT; |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 49 | case kByte4_GrVertexAttribType: |
| 50 | return VK_FORMAT_R8G8B8A8_SINT; |
| 51 | case kUByte_GrVertexAttribType: |
| 52 | return VK_FORMAT_R8_UINT; |
| 53 | case kUByte2_GrVertexAttribType: |
| 54 | return VK_FORMAT_R8G8_UINT; |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 55 | case kUByte4_GrVertexAttribType: |
| 56 | return VK_FORMAT_R8G8B8A8_UINT; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 57 | case kUByte_norm_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 58 | return VK_FORMAT_R8_UNORM; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 59 | case kUByte4_norm_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 60 | return VK_FORMAT_R8G8B8A8_UNORM; |
Chris Dalton | a045eea | 2017-10-24 13:22:10 -0600 | [diff] [blame] | 61 | case kShort2_GrVertexAttribType: |
| 62 | return VK_FORMAT_R16G16_SINT; |
Brian Osman | a5c578f | 2018-09-19 14:19:02 -0400 | [diff] [blame] | 63 | case kShort4_GrVertexAttribType: |
| 64 | return VK_FORMAT_R16G16B16A16_SINT; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 65 | case kUShort2_GrVertexAttribType: |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 66 | return VK_FORMAT_R16G16_UINT; |
Chris Dalton | a045eea | 2017-10-24 13:22:10 -0600 | [diff] [blame] | 67 | case kUShort2_norm_GrVertexAttribType: |
| 68 | return VK_FORMAT_R16G16_UNORM; |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 69 | case kInt_GrVertexAttribType: |
| 70 | return VK_FORMAT_R32_SINT; |
| 71 | case kUint_GrVertexAttribType: |
| 72 | return VK_FORMAT_R32_UINT; |
Robert Phillips | fe18de5 | 2019-06-06 17:21:50 -0400 | [diff] [blame] | 73 | case kUShort_norm_GrVertexAttribType: |
| 74 | return VK_FORMAT_R16_UNORM; |
Robert Phillips | 66a4603 | 2019-06-18 08:00:42 -0400 | [diff] [blame] | 75 | case kUShort4_norm_GrVertexAttribType: |
| 76 | return VK_FORMAT_R16G16B16A16_UNORM; |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 77 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 78 | SK_ABORT("Unknown vertex attrib type"); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 79 | } |
| 80 | |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 81 | static void setup_vertex_input_state( |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 82 | const GrGeometryProcessor::AttributeSet& vertexAttribs, |
| 83 | const GrGeometryProcessor::AttributeSet& instanceAttribs, |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 84 | VkPipelineVertexInputStateCreateInfo* vertexInputInfo, |
| 85 | SkSTArray<2, VkVertexInputBindingDescription, true>* bindingDescs, |
| 86 | VkVertexInputAttributeDescription* attributeDesc) { |
| 87 | int vaCount = vertexAttribs.count(); |
| 88 | int iaCount = instanceAttribs.count(); |
| 89 | |
Kevin Lubick | 4284613 | 2018-01-05 10:11:11 -0500 | [diff] [blame] | 90 | uint32_t vertexBinding = 0, instanceBinding = 0; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 91 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 92 | int nextBinding = bindingDescs->count(); |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 93 | if (vaCount) { |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 94 | vertexBinding = nextBinding++; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 95 | } |
| 96 | |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 97 | if (iaCount) { |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 98 | instanceBinding = nextBinding; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 99 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 100 | |
| 101 | // setup attribute descriptions |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 102 | int attribIndex = 0; |
| 103 | size_t vertexAttributeOffset = 0; |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 104 | for (const auto& attrib : vertexAttribs) { |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 105 | VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex]; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 106 | vkAttrib.location = attribIndex++; // for now assume location = attribIndex |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 107 | vkAttrib.binding = vertexBinding; |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 108 | vkAttrib.format = attrib_type_to_vkformat(attrib.cpuType()); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 109 | vkAttrib.offset = vertexAttributeOffset; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 110 | vertexAttributeOffset += attrib.sizeAlign4(); |
| 111 | } |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 112 | SkASSERT(vertexAttributeOffset == vertexAttribs.stride()); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 113 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 114 | size_t instanceAttributeOffset = 0; |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 115 | for (const auto& attrib : instanceAttribs) { |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 116 | VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex]; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 117 | vkAttrib.location = attribIndex++; // for now assume location = attribIndex |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 118 | vkAttrib.binding = instanceBinding; |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 119 | vkAttrib.format = attrib_type_to_vkformat(attrib.cpuType()); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 120 | vkAttrib.offset = instanceAttributeOffset; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 121 | instanceAttributeOffset += attrib.sizeAlign4(); |
| 122 | } |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 123 | SkASSERT(instanceAttributeOffset == instanceAttribs.stride()); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 124 | |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 125 | if (vaCount) { |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 126 | bindingDescs->push_back() = { |
| 127 | vertexBinding, |
| 128 | (uint32_t) vertexAttributeOffset, |
| 129 | VK_VERTEX_INPUT_RATE_VERTEX |
| 130 | }; |
| 131 | } |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 132 | if (iaCount) { |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 133 | bindingDescs->push_back() = { |
| 134 | instanceBinding, |
| 135 | (uint32_t) instanceAttributeOffset, |
| 136 | VK_VERTEX_INPUT_RATE_INSTANCE |
| 137 | }; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | memset(vertexInputInfo, 0, sizeof(VkPipelineVertexInputStateCreateInfo)); |
| 141 | vertexInputInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; |
| 142 | vertexInputInfo->pNext = nullptr; |
| 143 | vertexInputInfo->flags = 0; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 144 | vertexInputInfo->vertexBindingDescriptionCount = bindingDescs->count(); |
| 145 | vertexInputInfo->pVertexBindingDescriptions = bindingDescs->begin(); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 146 | vertexInputInfo->vertexAttributeDescriptionCount = vaCount + iaCount; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 147 | vertexInputInfo->pVertexAttributeDescriptions = attributeDesc; |
| 148 | } |
| 149 | |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 150 | static VkPrimitiveTopology gr_primitive_type_to_vk_topology(GrPrimitiveType primitiveType) { |
| 151 | switch (primitiveType) { |
| 152 | case GrPrimitiveType::kTriangles: |
| 153 | return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; |
| 154 | case GrPrimitiveType::kTriangleStrip: |
| 155 | return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 156 | case GrPrimitiveType::kPoints: |
| 157 | return VK_PRIMITIVE_TOPOLOGY_POINT_LIST; |
| 158 | case GrPrimitiveType::kLines: |
| 159 | return VK_PRIMITIVE_TOPOLOGY_LINE_LIST; |
| 160 | case GrPrimitiveType::kLineStrip: |
| 161 | return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP; |
Chris Dalton | 5a2f962 | 2019-12-27 14:56:38 -0700 | [diff] [blame] | 162 | case GrPrimitiveType::kPatches: |
Robert Phillips | 571177f | 2019-10-04 14:41:49 -0400 | [diff] [blame] | 163 | case GrPrimitiveType::kPath: |
| 164 | SK_ABORT("Unsupported primitive type"); |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 165 | } |
Greg Daniel | ed4e52c | 2020-04-13 14:35:19 -0400 | [diff] [blame] | 166 | SkUNREACHABLE; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 167 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 168 | |
| 169 | static void setup_input_assembly_state(GrPrimitiveType primitiveType, |
| 170 | VkPipelineInputAssemblyStateCreateInfo* inputAssemblyInfo) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 171 | memset(inputAssemblyInfo, 0, sizeof(VkPipelineInputAssemblyStateCreateInfo)); |
| 172 | inputAssemblyInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; |
| 173 | inputAssemblyInfo->pNext = nullptr; |
| 174 | inputAssemblyInfo->flags = 0; |
| 175 | inputAssemblyInfo->primitiveRestartEnable = false; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 176 | inputAssemblyInfo->topology = gr_primitive_type_to_vk_topology(primitiveType); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 177 | } |
| 178 | |
egdaniel | ec44099 | 2016-09-13 09:54:11 -0700 | [diff] [blame] | 179 | static VkStencilOp stencil_op_to_vk_stencil_op(GrStencilOp op) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 180 | static const VkStencilOp gTable[] = { |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 181 | VK_STENCIL_OP_KEEP, // kKeep |
| 182 | VK_STENCIL_OP_ZERO, // kZero |
| 183 | VK_STENCIL_OP_REPLACE, // kReplace |
| 184 | VK_STENCIL_OP_INVERT, // kInvert |
| 185 | VK_STENCIL_OP_INCREMENT_AND_WRAP, // kIncWrap |
| 186 | VK_STENCIL_OP_DECREMENT_AND_WRAP, // kDecWrap |
| 187 | VK_STENCIL_OP_INCREMENT_AND_CLAMP, // kIncClamp |
| 188 | VK_STENCIL_OP_DECREMENT_AND_CLAMP, // kDecClamp |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 189 | }; |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 190 | static_assert(SK_ARRAY_COUNT(gTable) == kGrStencilOpCount); |
| 191 | static_assert(0 == (int)GrStencilOp::kKeep); |
| 192 | static_assert(1 == (int)GrStencilOp::kZero); |
| 193 | static_assert(2 == (int)GrStencilOp::kReplace); |
| 194 | static_assert(3 == (int)GrStencilOp::kInvert); |
| 195 | static_assert(4 == (int)GrStencilOp::kIncWrap); |
| 196 | static_assert(5 == (int)GrStencilOp::kDecWrap); |
| 197 | static_assert(6 == (int)GrStencilOp::kIncClamp); |
| 198 | static_assert(7 == (int)GrStencilOp::kDecClamp); |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 199 | SkASSERT(op < (GrStencilOp)kGrStencilOpCount); |
| 200 | return gTable[(int)op]; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 201 | } |
| 202 | |
egdaniel | ec44099 | 2016-09-13 09:54:11 -0700 | [diff] [blame] | 203 | static VkCompareOp stencil_func_to_vk_compare_op(GrStencilTest test) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 204 | static const VkCompareOp gTable[] = { |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 205 | VK_COMPARE_OP_ALWAYS, // kAlways |
| 206 | VK_COMPARE_OP_NEVER, // kNever |
| 207 | VK_COMPARE_OP_GREATER, // kGreater |
| 208 | VK_COMPARE_OP_GREATER_OR_EQUAL, // kGEqual |
| 209 | VK_COMPARE_OP_LESS, // kLess |
| 210 | VK_COMPARE_OP_LESS_OR_EQUAL, // kLEqual |
| 211 | VK_COMPARE_OP_EQUAL, // kEqual |
| 212 | VK_COMPARE_OP_NOT_EQUAL, // kNotEqual |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 213 | }; |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 214 | static_assert(SK_ARRAY_COUNT(gTable) == kGrStencilTestCount); |
| 215 | static_assert(0 == (int)GrStencilTest::kAlways); |
| 216 | static_assert(1 == (int)GrStencilTest::kNever); |
| 217 | static_assert(2 == (int)GrStencilTest::kGreater); |
| 218 | static_assert(3 == (int)GrStencilTest::kGEqual); |
| 219 | static_assert(4 == (int)GrStencilTest::kLess); |
| 220 | static_assert(5 == (int)GrStencilTest::kLEqual); |
| 221 | static_assert(6 == (int)GrStencilTest::kEqual); |
| 222 | static_assert(7 == (int)GrStencilTest::kNotEqual); |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 223 | SkASSERT(test < (GrStencilTest)kGrStencilTestCount); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 224 | |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 225 | return gTable[(int)test]; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 226 | } |
| 227 | |
Chris Dalton | 71713f6 | 2019-04-18 12:41:03 -0600 | [diff] [blame] | 228 | static void setup_stencil_op_state( |
| 229 | VkStencilOpState* opState, const GrStencilSettings::Face& stencilFace) { |
| 230 | opState->failOp = stencil_op_to_vk_stencil_op(stencilFace.fFailOp); |
| 231 | opState->passOp = stencil_op_to_vk_stencil_op(stencilFace.fPassOp); |
| 232 | opState->depthFailOp = opState->failOp; |
| 233 | opState->compareOp = stencil_func_to_vk_compare_op(stencilFace.fTest); |
| 234 | opState->compareMask = stencilFace.fTestMask; |
| 235 | opState->writeMask = stencilFace.fWriteMask; |
| 236 | opState->reference = stencilFace.fRef; |
| 237 | } |
| 238 | |
Robert Phillips | 6c2aa7a | 2019-10-17 19:06:39 +0000 | [diff] [blame] | 239 | static void setup_depth_stencil_state( |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 240 | const GrStencilSettings& stencilSettings, |
| 241 | GrSurfaceOrigin origin, |
Robert Phillips | 6c2aa7a | 2019-10-17 19:06:39 +0000 | [diff] [blame] | 242 | VkPipelineDepthStencilStateCreateInfo* stencilInfo) { |
Robert Phillips | a87c529 | 2019-11-12 10:12:42 -0500 | [diff] [blame] | 243 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 244 | memset(stencilInfo, 0, sizeof(VkPipelineDepthStencilStateCreateInfo)); |
| 245 | stencilInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; |
| 246 | stencilInfo->pNext = nullptr; |
| 247 | stencilInfo->flags = 0; |
| 248 | // set depth testing defaults |
| 249 | stencilInfo->depthTestEnable = VK_FALSE; |
| 250 | stencilInfo->depthWriteEnable = VK_FALSE; |
| 251 | stencilInfo->depthCompareOp = VK_COMPARE_OP_ALWAYS; |
| 252 | stencilInfo->depthBoundsTestEnable = VK_FALSE; |
| 253 | stencilInfo->stencilTestEnable = !stencilSettings.isDisabled(); |
| 254 | if (!stencilSettings.isDisabled()) { |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 255 | if (!stencilSettings.isTwoSided()) { |
Chris Dalton | 67d43fe | 2019-11-05 23:01:21 -0700 | [diff] [blame] | 256 | setup_stencil_op_state(&stencilInfo->front, stencilSettings.singleSidedFace()); |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 257 | stencilInfo->back = stencilInfo->front; |
| 258 | } else { |
Chris Dalton | 67d43fe | 2019-11-05 23:01:21 -0700 | [diff] [blame] | 259 | setup_stencil_op_state(&stencilInfo->front, stencilSettings.postOriginCCWFace(origin)); |
| 260 | setup_stencil_op_state(&stencilInfo->back, stencilSettings.postOriginCWFace(origin)); |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 261 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 262 | } |
| 263 | stencilInfo->minDepthBounds = 0.0f; |
| 264 | stencilInfo->maxDepthBounds = 1.0f; |
| 265 | } |
| 266 | |
egdaniel | ec44099 | 2016-09-13 09:54:11 -0700 | [diff] [blame] | 267 | static void setup_viewport_scissor_state(VkPipelineViewportStateCreateInfo* viewportInfo) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 268 | memset(viewportInfo, 0, sizeof(VkPipelineViewportStateCreateInfo)); |
| 269 | viewportInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; |
| 270 | viewportInfo->pNext = nullptr; |
| 271 | viewportInfo->flags = 0; |
| 272 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 273 | viewportInfo->viewportCount = 1; |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 274 | viewportInfo->pViewports = nullptr; // This is set dynamically |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 275 | |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 276 | viewportInfo->scissorCount = 1; |
| 277 | viewportInfo->pScissors = nullptr; // This is set dynamically |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 278 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 279 | SkASSERT(viewportInfo->viewportCount == viewportInfo->scissorCount); |
| 280 | } |
| 281 | |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 282 | static void setup_multisample_state(int numSamples, |
egdaniel | ec44099 | 2016-09-13 09:54:11 -0700 | [diff] [blame] | 283 | const GrCaps* caps, |
| 284 | VkPipelineMultisampleStateCreateInfo* multisampleInfo) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 285 | memset(multisampleInfo, 0, sizeof(VkPipelineMultisampleStateCreateInfo)); |
| 286 | multisampleInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; |
| 287 | multisampleInfo->pNext = nullptr; |
| 288 | multisampleInfo->flags = 0; |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 289 | SkAssertResult(GrSampleCountToVkSampleCount(numSamples, |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 290 | &multisampleInfo->rasterizationSamples)); |
Brian Osman | a63593a | 2018-12-06 15:54:53 -0500 | [diff] [blame] | 291 | multisampleInfo->sampleShadingEnable = VK_FALSE; |
| 292 | multisampleInfo->minSampleShading = 0.0f; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 293 | multisampleInfo->pSampleMask = nullptr; |
| 294 | multisampleInfo->alphaToCoverageEnable = VK_FALSE; |
| 295 | multisampleInfo->alphaToOneEnable = VK_FALSE; |
| 296 | } |
| 297 | |
| 298 | static VkBlendFactor blend_coeff_to_vk_blend(GrBlendCoeff coeff) { |
Greg Daniel | 6e2af5c | 2020-03-30 15:07:05 -0400 | [diff] [blame] | 299 | switch (coeff) { |
| 300 | case kZero_GrBlendCoeff: |
| 301 | return VK_BLEND_FACTOR_ZERO; |
| 302 | case kOne_GrBlendCoeff: |
| 303 | return VK_BLEND_FACTOR_ONE; |
| 304 | case kSC_GrBlendCoeff: |
| 305 | return VK_BLEND_FACTOR_SRC_COLOR; |
| 306 | case kISC_GrBlendCoeff: |
| 307 | return VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR; |
| 308 | case kDC_GrBlendCoeff: |
| 309 | return VK_BLEND_FACTOR_DST_COLOR; |
| 310 | case kIDC_GrBlendCoeff: |
| 311 | return VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR; |
| 312 | case kSA_GrBlendCoeff: |
| 313 | return VK_BLEND_FACTOR_SRC_ALPHA; |
| 314 | case kISA_GrBlendCoeff: |
| 315 | return VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; |
| 316 | case kDA_GrBlendCoeff: |
| 317 | return VK_BLEND_FACTOR_DST_ALPHA; |
| 318 | case kIDA_GrBlendCoeff: |
| 319 | return VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA; |
| 320 | case kConstC_GrBlendCoeff: |
| 321 | return VK_BLEND_FACTOR_CONSTANT_COLOR; |
| 322 | case kIConstC_GrBlendCoeff: |
| 323 | return VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR; |
| 324 | case kS2C_GrBlendCoeff: |
| 325 | return VK_BLEND_FACTOR_SRC1_COLOR; |
| 326 | case kIS2C_GrBlendCoeff: |
| 327 | return VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR; |
| 328 | case kS2A_GrBlendCoeff: |
| 329 | return VK_BLEND_FACTOR_SRC1_ALPHA; |
| 330 | case kIS2A_GrBlendCoeff: |
| 331 | return VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA; |
| 332 | case kIllegal_GrBlendCoeff: |
| 333 | return VK_BLEND_FACTOR_ZERO; |
| 334 | } |
| 335 | SkUNREACHABLE; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 336 | } |
| 337 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 338 | static VkBlendOp blend_equation_to_vk_blend_op(GrBlendEquation equation) { |
| 339 | static const VkBlendOp gTable[] = { |
Greg Daniel | 313c695 | 2018-08-08 09:24:08 -0400 | [diff] [blame] | 340 | // Basic blend ops |
| 341 | VK_BLEND_OP_ADD, |
| 342 | VK_BLEND_OP_SUBTRACT, |
| 343 | VK_BLEND_OP_REVERSE_SUBTRACT, |
| 344 | |
| 345 | // Advanced blend ops |
| 346 | VK_BLEND_OP_SCREEN_EXT, |
| 347 | VK_BLEND_OP_OVERLAY_EXT, |
| 348 | VK_BLEND_OP_DARKEN_EXT, |
| 349 | VK_BLEND_OP_LIGHTEN_EXT, |
| 350 | VK_BLEND_OP_COLORDODGE_EXT, |
| 351 | VK_BLEND_OP_COLORBURN_EXT, |
| 352 | VK_BLEND_OP_HARDLIGHT_EXT, |
| 353 | VK_BLEND_OP_SOFTLIGHT_EXT, |
| 354 | VK_BLEND_OP_DIFFERENCE_EXT, |
| 355 | VK_BLEND_OP_EXCLUSION_EXT, |
| 356 | VK_BLEND_OP_MULTIPLY_EXT, |
| 357 | VK_BLEND_OP_HSL_HUE_EXT, |
| 358 | VK_BLEND_OP_HSL_SATURATION_EXT, |
| 359 | VK_BLEND_OP_HSL_COLOR_EXT, |
Mike Klein | 3674336 | 2018-11-06 08:23:30 -0500 | [diff] [blame] | 360 | VK_BLEND_OP_HSL_LUMINOSITY_EXT, |
| 361 | |
| 362 | // Illegal. |
| 363 | VK_BLEND_OP_ADD, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 364 | }; |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 365 | static_assert(0 == kAdd_GrBlendEquation); |
| 366 | static_assert(1 == kSubtract_GrBlendEquation); |
| 367 | static_assert(2 == kReverseSubtract_GrBlendEquation); |
| 368 | static_assert(3 == kScreen_GrBlendEquation); |
| 369 | static_assert(4 == kOverlay_GrBlendEquation); |
| 370 | static_assert(5 == kDarken_GrBlendEquation); |
| 371 | static_assert(6 == kLighten_GrBlendEquation); |
| 372 | static_assert(7 == kColorDodge_GrBlendEquation); |
| 373 | static_assert(8 == kColorBurn_GrBlendEquation); |
| 374 | static_assert(9 == kHardLight_GrBlendEquation); |
| 375 | static_assert(10 == kSoftLight_GrBlendEquation); |
| 376 | static_assert(11 == kDifference_GrBlendEquation); |
| 377 | static_assert(12 == kExclusion_GrBlendEquation); |
| 378 | static_assert(13 == kMultiply_GrBlendEquation); |
| 379 | static_assert(14 == kHSLHue_GrBlendEquation); |
| 380 | static_assert(15 == kHSLSaturation_GrBlendEquation); |
| 381 | static_assert(16 == kHSLColor_GrBlendEquation); |
| 382 | static_assert(17 == kHSLLuminosity_GrBlendEquation); |
| 383 | static_assert(SK_ARRAY_COUNT(gTable) == kGrBlendEquationCnt); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 384 | |
Greg Daniel | 6e2af5c | 2020-03-30 15:07:05 -0400 | [diff] [blame] | 385 | SkASSERT((unsigned)equation < kGrBlendEquationCnt); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 386 | return gTable[equation]; |
| 387 | } |
| 388 | |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 389 | static void setup_color_blend_state(const GrXferProcessor::BlendInfo& blendInfo, |
egdaniel | ec44099 | 2016-09-13 09:54:11 -0700 | [diff] [blame] | 390 | VkPipelineColorBlendStateCreateInfo* colorBlendInfo, |
| 391 | VkPipelineColorBlendAttachmentState* attachmentState) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 392 | GrBlendEquation equation = blendInfo.fEquation; |
| 393 | GrBlendCoeff srcCoeff = blendInfo.fSrcBlend; |
| 394 | GrBlendCoeff dstCoeff = blendInfo.fDstBlend; |
Greg Daniel | 0a33551 | 2020-03-31 09:14:57 -0400 | [diff] [blame] | 395 | bool blendOff = GrBlendShouldDisable(equation, srcCoeff, dstCoeff); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 396 | |
| 397 | memset(attachmentState, 0, sizeof(VkPipelineColorBlendAttachmentState)); |
| 398 | attachmentState->blendEnable = !blendOff; |
| 399 | if (!blendOff) { |
| 400 | attachmentState->srcColorBlendFactor = blend_coeff_to_vk_blend(srcCoeff); |
| 401 | attachmentState->dstColorBlendFactor = blend_coeff_to_vk_blend(dstCoeff); |
| 402 | attachmentState->colorBlendOp = blend_equation_to_vk_blend_op(equation); |
| 403 | attachmentState->srcAlphaBlendFactor = blend_coeff_to_vk_blend(srcCoeff); |
| 404 | attachmentState->dstAlphaBlendFactor = blend_coeff_to_vk_blend(dstCoeff); |
| 405 | attachmentState->alphaBlendOp = blend_equation_to_vk_blend_op(equation); |
| 406 | } |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 407 | |
| 408 | if (!blendInfo.fWriteColor) { |
| 409 | attachmentState->colorWriteMask = 0; |
| 410 | } else { |
| 411 | attachmentState->colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | |
| 412 | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; |
| 413 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 414 | |
| 415 | memset(colorBlendInfo, 0, sizeof(VkPipelineColorBlendStateCreateInfo)); |
| 416 | colorBlendInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; |
| 417 | colorBlendInfo->pNext = nullptr; |
| 418 | colorBlendInfo->flags = 0; |
| 419 | colorBlendInfo->logicOpEnable = VK_FALSE; |
| 420 | colorBlendInfo->attachmentCount = 1; |
| 421 | colorBlendInfo->pAttachments = attachmentState; |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 422 | // colorBlendInfo->blendConstants is set dynamically |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 423 | } |
| 424 | |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 425 | static void setup_raster_state(bool isWireframe, |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame] | 426 | const GrCaps* caps, |
egdaniel | ec44099 | 2016-09-13 09:54:11 -0700 | [diff] [blame] | 427 | VkPipelineRasterizationStateCreateInfo* rasterInfo) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 428 | memset(rasterInfo, 0, sizeof(VkPipelineRasterizationStateCreateInfo)); |
| 429 | rasterInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; |
| 430 | rasterInfo->pNext = nullptr; |
| 431 | rasterInfo->flags = 0; |
| 432 | rasterInfo->depthClampEnable = VK_FALSE; |
| 433 | rasterInfo->rasterizerDiscardEnable = VK_FALSE; |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 434 | rasterInfo->polygonMode = (caps->wireframeMode() || isWireframe) ? |
Chris Dalton | 1215cda | 2019-12-17 21:44:04 -0700 | [diff] [blame] | 435 | VK_POLYGON_MODE_LINE : VK_POLYGON_MODE_FILL; |
Brian Salomon | f086167 | 2017-05-08 11:10:10 -0400 | [diff] [blame] | 436 | rasterInfo->cullMode = VK_CULL_MODE_NONE; |
Chris Dalton | b91c466 | 2018-08-01 10:46:22 -0600 | [diff] [blame] | 437 | rasterInfo->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 438 | rasterInfo->depthBiasEnable = VK_FALSE; |
| 439 | rasterInfo->depthBiasConstantFactor = 0.0f; |
| 440 | rasterInfo->depthBiasClamp = 0.0f; |
| 441 | rasterInfo->depthBiasSlopeFactor = 0.0f; |
| 442 | rasterInfo->lineWidth = 1.0f; |
| 443 | } |
| 444 | |
Chris Dalton | ce425af | 2019-12-16 10:39:03 -0700 | [diff] [blame] | 445 | static void setup_conservative_raster_info( |
| 446 | VkPipelineRasterizationConservativeStateCreateInfoEXT* conservativeRasterInfo) { |
| 447 | memset(conservativeRasterInfo, 0, |
| 448 | sizeof(VkPipelineRasterizationConservativeStateCreateInfoEXT)); |
| 449 | conservativeRasterInfo->sType = |
| 450 | VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_CONSERVATIVE_STATE_CREATE_INFO_EXT; |
| 451 | conservativeRasterInfo->pNext = nullptr; |
| 452 | conservativeRasterInfo->flags = 0; |
| 453 | conservativeRasterInfo->conservativeRasterizationMode = |
| 454 | VK_CONSERVATIVE_RASTERIZATION_MODE_OVERESTIMATE_EXT; |
| 455 | conservativeRasterInfo->extraPrimitiveOverestimationSize = 0; |
| 456 | } |
| 457 | |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 458 | static void setup_dynamic_state(VkPipelineDynamicStateCreateInfo* dynamicInfo, |
Greg Daniel | d178606 | 2020-10-28 15:38:07 +0000 | [diff] [blame] | 459 | VkDynamicState* dynamicStates) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 460 | memset(dynamicInfo, 0, sizeof(VkPipelineDynamicStateCreateInfo)); |
| 461 | dynamicInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 462 | dynamicInfo->pNext = VK_NULL_HANDLE; |
| 463 | dynamicInfo->flags = 0; |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 464 | dynamicStates[0] = VK_DYNAMIC_STATE_VIEWPORT; |
| 465 | dynamicStates[1] = VK_DYNAMIC_STATE_SCISSOR; |
| 466 | dynamicStates[2] = VK_DYNAMIC_STATE_BLEND_CONSTANTS; |
Greg Daniel | d178606 | 2020-10-28 15:38:07 +0000 | [diff] [blame] | 467 | dynamicInfo->dynamicStateCount = 3; |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 468 | dynamicInfo->pDynamicStates = dynamicStates; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 469 | } |
| 470 | |
Greg Daniel | 3ef052c | 2021-01-05 12:20:27 -0500 | [diff] [blame] | 471 | sk_sp<GrVkPipeline> GrVkPipeline::Make(GrVkGpu* gpu, |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 472 | const GrGeometryProcessor::AttributeSet& vertexAttribs, |
| 473 | const GrGeometryProcessor::AttributeSet& instanceAttribs, |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 474 | GrPrimitiveType primitiveType, |
| 475 | GrSurfaceOrigin origin, |
| 476 | const GrStencilSettings& stencilSettings, |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 477 | int numSamples, |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 478 | bool isHWAntialiasState, |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 479 | const GrXferProcessor::BlendInfo& blendInfo, |
| 480 | bool isWireframe, |
| 481 | bool useConservativeRaster, |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 482 | uint32_t subpass, |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 483 | VkPipelineShaderStageCreateInfo* shaderStageInfo, |
| 484 | int shaderStageCount, |
| 485 | VkRenderPass compatibleRenderPass, |
| 486 | VkPipelineLayout layout, |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 487 | bool ownsLayout, |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 488 | VkPipelineCache cache) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 489 | VkPipelineVertexInputStateCreateInfo vertexInputInfo; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 490 | SkSTArray<2, VkVertexInputBindingDescription, true> bindingDescs; |
egdaniel | b05df0f | 2016-06-27 07:15:20 -0700 | [diff] [blame] | 491 | SkSTArray<16, VkVertexInputAttributeDescription> attributeDesc; |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 492 | int totalAttributeCnt = vertexAttribs.count() + instanceAttribs.count(); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 493 | SkASSERT(totalAttributeCnt <= gpu->vkCaps().maxVertexAttributes()); |
| 494 | VkVertexInputAttributeDescription* pAttribs = attributeDesc.push_back_n(totalAttributeCnt); |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 495 | setup_vertex_input_state(vertexAttribs, instanceAttribs, &vertexInputInfo, &bindingDescs, |
| 496 | pAttribs); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 497 | |
| 498 | VkPipelineInputAssemblyStateCreateInfo inputAssemblyInfo; |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 499 | setup_input_assembly_state(primitiveType, &inputAssemblyInfo); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 500 | |
| 501 | VkPipelineDepthStencilStateCreateInfo depthStencilInfo; |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 502 | setup_depth_stencil_state(stencilSettings, origin, &depthStencilInfo); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 503 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 504 | VkPipelineViewportStateCreateInfo viewportInfo; |
egdaniel | ec44099 | 2016-09-13 09:54:11 -0700 | [diff] [blame] | 505 | setup_viewport_scissor_state(&viewportInfo); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 506 | |
| 507 | VkPipelineMultisampleStateCreateInfo multisampleInfo; |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 508 | setup_multisample_state(numSamples, gpu->caps(), &multisampleInfo); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 509 | |
| 510 | // We will only have one color attachment per pipeline. |
| 511 | VkPipelineColorBlendAttachmentState attachmentStates[1]; |
| 512 | VkPipelineColorBlendStateCreateInfo colorBlendInfo; |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 513 | setup_color_blend_state(blendInfo, &colorBlendInfo, attachmentStates); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 514 | |
| 515 | VkPipelineRasterizationStateCreateInfo rasterInfo; |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 516 | setup_raster_state(isWireframe, gpu->caps(), &rasterInfo); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 517 | |
Chris Dalton | ce425af | 2019-12-16 10:39:03 -0700 | [diff] [blame] | 518 | VkPipelineRasterizationConservativeStateCreateInfoEXT conservativeRasterInfo; |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 519 | if (useConservativeRaster) { |
Chris Dalton | ce425af | 2019-12-16 10:39:03 -0700 | [diff] [blame] | 520 | SkASSERT(gpu->caps()->conservativeRasterSupport()); |
| 521 | setup_conservative_raster_info(&conservativeRasterInfo); |
| 522 | conservativeRasterInfo.pNext = rasterInfo.pNext; |
| 523 | rasterInfo.pNext = &conservativeRasterInfo; |
| 524 | } |
| 525 | |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 526 | VkDynamicState dynamicStates[3]; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 527 | VkPipelineDynamicStateCreateInfo dynamicInfo; |
Greg Daniel | d178606 | 2020-10-28 15:38:07 +0000 | [diff] [blame] | 528 | setup_dynamic_state(&dynamicInfo, dynamicStates); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 529 | |
| 530 | VkGraphicsPipelineCreateInfo pipelineCreateInfo; |
| 531 | memset(&pipelineCreateInfo, 0, sizeof(VkGraphicsPipelineCreateInfo)); |
| 532 | pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
| 533 | pipelineCreateInfo.pNext = nullptr; |
| 534 | pipelineCreateInfo.flags = 0; |
| 535 | pipelineCreateInfo.stageCount = shaderStageCount; |
| 536 | pipelineCreateInfo.pStages = shaderStageInfo; |
| 537 | pipelineCreateInfo.pVertexInputState = &vertexInputInfo; |
| 538 | pipelineCreateInfo.pInputAssemblyState = &inputAssemblyInfo; |
| 539 | pipelineCreateInfo.pTessellationState = nullptr; |
| 540 | pipelineCreateInfo.pViewportState = &viewportInfo; |
| 541 | pipelineCreateInfo.pRasterizationState = &rasterInfo; |
| 542 | pipelineCreateInfo.pMultisampleState = &multisampleInfo; |
| 543 | pipelineCreateInfo.pDepthStencilState = &depthStencilInfo; |
| 544 | pipelineCreateInfo.pColorBlendState = &colorBlendInfo; |
| 545 | pipelineCreateInfo.pDynamicState = &dynamicInfo; |
| 546 | pipelineCreateInfo.layout = layout; |
Greg Daniel | 99b88e0 | 2018-10-03 15:31:20 -0400 | [diff] [blame] | 547 | pipelineCreateInfo.renderPass = compatibleRenderPass; |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 548 | pipelineCreateInfo.subpass = subpass; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 549 | pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE; |
| 550 | pipelineCreateInfo.basePipelineIndex = -1; |
| 551 | |
| 552 | VkPipeline vkPipeline; |
Ben Wagner | 6c30e74 | 2019-02-06 10:46:14 -0500 | [diff] [blame] | 553 | VkResult err; |
| 554 | { |
Brian Osman | 7a20b5c | 2021-03-15 16:23:33 -0400 | [diff] [blame] | 555 | TRACE_EVENT0("skia.shaders", "CreateGraphicsPipeline"); |
Ben Wagner | 6c30e74 | 2019-02-06 10:46:14 -0500 | [diff] [blame] | 556 | #if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS) |
| 557 | // skia:8712 |
| 558 | __lsan::ScopedDisabler lsanDisabler; |
| 559 | #endif |
Greg Daniel | d034c62 | 2019-11-20 09:33:29 -0500 | [diff] [blame] | 560 | GR_VK_CALL_RESULT(gpu, err, CreateGraphicsPipelines(gpu->device(), cache, 1, |
| 561 | &pipelineCreateInfo, nullptr, |
| 562 | &vkPipeline)); |
Ben Wagner | 6c30e74 | 2019-02-06 10:46:14 -0500 | [diff] [blame] | 563 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 564 | if (err) { |
Greg Daniel | 5ba448c | 2018-02-26 13:30:47 -0500 | [diff] [blame] | 565 | SkDebugf("Failed to create pipeline. Error: %d\n", err); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 566 | return nullptr; |
| 567 | } |
| 568 | |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 569 | if (!ownsLayout) { |
| 570 | layout = VK_NULL_HANDLE; |
| 571 | } |
Greg Daniel | 3ef052c | 2021-01-05 12:20:27 -0500 | [diff] [blame] | 572 | return sk_sp<GrVkPipeline>(new GrVkPipeline(gpu, vkPipeline, layout)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 573 | } |
| 574 | |
Greg Daniel | 3ef052c | 2021-01-05 12:20:27 -0500 | [diff] [blame] | 575 | sk_sp<GrVkPipeline> GrVkPipeline::Make(GrVkGpu* gpu, |
Greg Daniel | 91b37b1 | 2021-01-05 15:40:54 -0500 | [diff] [blame] | 576 | const GrProgramInfo& programInfo, |
| 577 | VkPipelineShaderStageCreateInfo* shaderStageInfo, |
| 578 | int shaderStageCount, |
| 579 | VkRenderPass compatibleRenderPass, |
| 580 | VkPipelineLayout layout, |
| 581 | VkPipelineCache cache, |
| 582 | uint32_t subpass) { |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 583 | const GrGeometryProcessor& geomProc = programInfo.geomProc(); |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 584 | const GrPipeline& pipeline = programInfo.pipeline(); |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 585 | |
Greg Daniel | 3ef052c | 2021-01-05 12:20:27 -0500 | [diff] [blame] | 586 | return Make(gpu, |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 587 | geomProc.vertexAttributes(), |
| 588 | geomProc.instanceAttributes(), |
Greg Daniel | 3ef052c | 2021-01-05 12:20:27 -0500 | [diff] [blame] | 589 | programInfo.primitiveType(), |
| 590 | programInfo.origin(), |
| 591 | programInfo.nonGLStencilSettings(), |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 592 | programInfo.numSamples(), |
Chris Dalton | eb0195e | 2021-08-18 21:39:02 -0600 | [diff] [blame] | 593 | programInfo.numSamples() > 1, |
Greg Daniel | 3ef052c | 2021-01-05 12:20:27 -0500 | [diff] [blame] | 594 | pipeline.getXferProcessor().getBlendInfo(), |
| 595 | pipeline.isWireframe(), |
| 596 | pipeline.usesConservativeRaster(), |
| 597 | subpass, |
| 598 | shaderStageInfo, |
| 599 | shaderStageCount, |
| 600 | compatibleRenderPass, |
| 601 | layout, |
| 602 | /*ownsLayout=*/true, |
| 603 | cache); |
Greg Daniel | b39d076 | 2020-12-30 15:02:34 -0500 | [diff] [blame] | 604 | } |
| 605 | |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 606 | void GrVkPipeline::freeGPUData() const { |
| 607 | GR_VK_CALL(fGpu->vkInterface(), DestroyPipeline(fGpu->device(), fPipeline, nullptr)); |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 608 | if (fPipelineLayout != VK_NULL_HANDLE) { |
| 609 | GR_VK_CALL(fGpu->vkInterface(), |
| 610 | DestroyPipelineLayout(fGpu->device(), fPipelineLayout, nullptr)); |
| 611 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 612 | } |
| 613 | |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 614 | void GrVkPipeline::SetDynamicScissorRectState(GrVkGpu* gpu, |
| 615 | GrVkCommandBuffer* cmdBuffer, |
Greg Daniel | a36cb40 | 2021-04-20 12:42:11 -0400 | [diff] [blame] | 616 | SkISize colorAttachmentDimensions, |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 617 | GrSurfaceOrigin rtOrigin, |
Greg Daniel | 4a0d36d | 2019-09-30 12:24:36 -0400 | [diff] [blame] | 618 | const SkIRect& scissorRect) { |
| 619 | SkASSERT(scissorRect.isEmpty() || |
Greg Daniel | a36cb40 | 2021-04-20 12:42:11 -0400 | [diff] [blame] | 620 | SkIRect::MakeSize(colorAttachmentDimensions).contains(scissorRect)); |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 621 | |
| 622 | VkRect2D scissor; |
| 623 | scissor.offset.x = scissorRect.fLeft; |
| 624 | scissor.extent.width = scissorRect.width(); |
| 625 | if (kTopLeft_GrSurfaceOrigin == rtOrigin) { |
| 626 | scissor.offset.y = scissorRect.fTop; |
| 627 | } else { |
| 628 | SkASSERT(kBottomLeft_GrSurfaceOrigin == rtOrigin); |
Greg Daniel | a36cb40 | 2021-04-20 12:42:11 -0400 | [diff] [blame] | 629 | scissor.offset.y = colorAttachmentDimensions.height() - scissorRect.fBottom; |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 630 | } |
| 631 | scissor.extent.height = scissorRect.height(); |
| 632 | |
| 633 | SkASSERT(scissor.offset.x >= 0); |
| 634 | SkASSERT(scissor.offset.y >= 0); |
| 635 | cmdBuffer->setScissor(gpu, 0, 1, &scissor); |
| 636 | } |
| 637 | |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 638 | void GrVkPipeline::SetDynamicViewportState(GrVkGpu* gpu, |
| 639 | GrVkCommandBuffer* cmdBuffer, |
Greg Daniel | a36cb40 | 2021-04-20 12:42:11 -0400 | [diff] [blame] | 640 | SkISize colorAttachmentDimensions) { |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 641 | // We always use one viewport the size of the RT |
| 642 | VkViewport viewport; |
| 643 | viewport.x = 0.0f; |
| 644 | viewport.y = 0.0f; |
Greg Daniel | a36cb40 | 2021-04-20 12:42:11 -0400 | [diff] [blame] | 645 | viewport.width = SkIntToScalar(colorAttachmentDimensions.width()); |
| 646 | viewport.height = SkIntToScalar(colorAttachmentDimensions.height()); |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 647 | viewport.minDepth = 0.0f; |
| 648 | viewport.maxDepth = 1.0f; |
| 649 | cmdBuffer->setViewport(gpu, 0, 1, &viewport); |
| 650 | } |
| 651 | |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 652 | void GrVkPipeline::SetDynamicBlendConstantState(GrVkGpu* gpu, |
| 653 | GrVkCommandBuffer* cmdBuffer, |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 654 | const GrSwizzle& swizzle, |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 655 | const GrXferProcessor& xferProcessor) { |
Chris Dalton | bbb3f64 | 2019-07-24 12:25:08 -0400 | [diff] [blame] | 656 | const GrXferProcessor::BlendInfo& blendInfo = xferProcessor.getBlendInfo(); |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 657 | GrBlendCoeff srcCoeff = blendInfo.fSrcBlend; |
| 658 | GrBlendCoeff dstCoeff = blendInfo.fDstBlend; |
| 659 | float floatColors[4]; |
Greg Daniel | 6e2af5c | 2020-03-30 15:07:05 -0400 | [diff] [blame] | 660 | if (GrBlendCoeffRefsConstant(srcCoeff) || GrBlendCoeffRefsConstant(dstCoeff)) { |
Greg Daniel | 6c9f101 | 2017-05-11 09:20:59 -0400 | [diff] [blame] | 661 | // Swizzle the blend to match what the shader will output. |
Brian Osman | 422f95b | 2018-11-05 16:49:04 -0500 | [diff] [blame] | 662 | SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant); |
| 663 | floatColors[0] = blendConst.fR; |
| 664 | floatColors[1] = blendConst.fG; |
| 665 | floatColors[2] = blendConst.fB; |
| 666 | floatColors[3] = blendConst.fA; |
Greg Daniel | 95cf99b | 2020-09-17 11:56:24 -0400 | [diff] [blame] | 667 | cmdBuffer->setBlendConstants(gpu, floatColors); |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 668 | } |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 669 | } |