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