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