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 | |
| 8 | #include "GrVkPipeline.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 9 | #include "GrGeometryProcessor.h" |
| 10 | #include "GrPipeline.h" |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 11 | #include "GrStencilSettings.h" |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 12 | #include "GrVkCommandBuffer.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 13 | #include "GrVkGpu.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 14 | #include "GrVkRenderTarget.h" |
| 15 | #include "GrVkUtil.h" |
| 16 | |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 17 | static inline VkFormat attrib_type_to_vkformat(GrVertexAttribType type) { |
| 18 | switch (type) { |
| 19 | case kFloat_GrVertexAttribType: |
| 20 | return VK_FORMAT_R32_SFLOAT; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 21 | case kFloat2_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 22 | return VK_FORMAT_R32G32_SFLOAT; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 23 | case kFloat3_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 24 | return VK_FORMAT_R32G32B32_SFLOAT; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 25 | case kFloat4_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 26 | return VK_FORMAT_R32G32B32A32_SFLOAT; |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 27 | case kHalf_GrVertexAttribType: |
| 28 | return VK_FORMAT_R16_SFLOAT; |
| 29 | case kHalf2_GrVertexAttribType: |
| 30 | return VK_FORMAT_R16G16_SFLOAT; |
| 31 | case kHalf3_GrVertexAttribType: |
| 32 | return VK_FORMAT_R16G16B16_SFLOAT; |
| 33 | case kHalf4_GrVertexAttribType: |
| 34 | return VK_FORMAT_R16G16B16A16_SFLOAT; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 35 | case kInt2_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 36 | return VK_FORMAT_R32G32_SINT; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 37 | case kInt3_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 38 | return VK_FORMAT_R32G32B32_SINT; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 39 | case kInt4_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 40 | return VK_FORMAT_R32G32B32A32_SINT; |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 41 | case kByte_GrVertexAttribType: |
| 42 | return VK_FORMAT_R8_SINT; |
| 43 | case kByte2_GrVertexAttribType: |
| 44 | return VK_FORMAT_R8G8_SINT; |
| 45 | case kByte3_GrVertexAttribType: |
| 46 | return VK_FORMAT_R8G8B8_SINT; |
| 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; |
| 53 | case kUByte3_GrVertexAttribType: |
| 54 | return VK_FORMAT_R8G8B8_UINT; |
| 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; |
| 73 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 74 | SK_ABORT("Unknown vertex attrib type"); |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 75 | return VK_FORMAT_UNDEFINED; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static void setup_vertex_input_state(const GrPrimitiveProcessor& primProc, |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 79 | VkPipelineVertexInputStateCreateInfo* vertexInputInfo, |
| 80 | SkSTArray<2, VkVertexInputBindingDescription, true>* bindingDescs, |
| 81 | VkVertexInputAttributeDescription* attributeDesc) { |
Kevin Lubick | 4284613 | 2018-01-05 10:11:11 -0500 | [diff] [blame] | 82 | uint32_t vertexBinding = 0, instanceBinding = 0; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 83 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 84 | int nextBinding = bindingDescs->count(); |
| 85 | if (primProc.hasVertexAttributes()) { |
| 86 | vertexBinding = nextBinding++; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 87 | } |
| 88 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 89 | if (primProc.hasInstanceAttributes()) { |
| 90 | instanceBinding = nextBinding; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 91 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 92 | |
| 93 | // setup attribute descriptions |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 94 | int vaCount = primProc.numVertexAttributes(); |
| 95 | int attribIndex = 0; |
| 96 | size_t vertexAttributeOffset = 0; |
| 97 | for (; attribIndex < vaCount; attribIndex++) { |
| 98 | const GrGeometryProcessor::Attribute& attrib = primProc.vertexAttribute(attribIndex); |
| 99 | VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex]; |
| 100 | vkAttrib.location = attribIndex; // for now assume location = attribIndex |
| 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; |
| 104 | SkASSERT(vkAttrib.offset == primProc.debugOnly_vertexAttributeOffset(attribIndex)); |
| 105 | vertexAttributeOffset += attrib.sizeAlign4(); |
| 106 | } |
| 107 | SkASSERT(vertexAttributeOffset == primProc.debugOnly_vertexStride()); |
| 108 | |
| 109 | int iaCount = primProc.numInstanceAttributes(); |
| 110 | size_t instanceAttributeOffset = 0; |
| 111 | for (int iaIndex = 0; iaIndex < iaCount; ++iaIndex, ++attribIndex) { |
| 112 | const GrGeometryProcessor::Attribute& attrib = primProc.instanceAttribute(iaIndex); |
| 113 | VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex]; |
| 114 | vkAttrib.location = attribIndex; // for now assume location = attribIndex |
| 115 | vkAttrib.binding = instanceBinding; |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 116 | vkAttrib.format = attrib_type_to_vkformat(attrib.cpuType()); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 117 | vkAttrib.offset = instanceAttributeOffset; |
| 118 | SkASSERT(vkAttrib.offset == primProc.debugOnly_instanceAttributeOffset(iaIndex)); |
| 119 | instanceAttributeOffset += attrib.sizeAlign4(); |
| 120 | } |
| 121 | SkASSERT(instanceAttributeOffset == primProc.debugOnly_instanceStride()); |
| 122 | |
| 123 | if (primProc.hasVertexAttributes()) { |
| 124 | bindingDescs->push_back() = { |
| 125 | vertexBinding, |
| 126 | (uint32_t) vertexAttributeOffset, |
| 127 | VK_VERTEX_INPUT_RATE_VERTEX |
| 128 | }; |
| 129 | } |
| 130 | if (primProc.hasInstanceAttributes()) { |
| 131 | bindingDescs->push_back() = { |
| 132 | instanceBinding, |
| 133 | (uint32_t) instanceAttributeOffset, |
| 134 | VK_VERTEX_INPUT_RATE_INSTANCE |
| 135 | }; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | memset(vertexInputInfo, 0, sizeof(VkPipelineVertexInputStateCreateInfo)); |
| 139 | vertexInputInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; |
| 140 | vertexInputInfo->pNext = nullptr; |
| 141 | vertexInputInfo->flags = 0; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 142 | vertexInputInfo->vertexBindingDescriptionCount = bindingDescs->count(); |
| 143 | vertexInputInfo->pVertexBindingDescriptions = bindingDescs->begin(); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 144 | vertexInputInfo->vertexAttributeDescriptionCount = vaCount + iaCount; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 145 | vertexInputInfo->pVertexAttributeDescriptions = attributeDesc; |
| 146 | } |
| 147 | |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 148 | static VkPrimitiveTopology gr_primitive_type_to_vk_topology(GrPrimitiveType primitiveType) { |
| 149 | switch (primitiveType) { |
| 150 | case GrPrimitiveType::kTriangles: |
| 151 | return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; |
| 152 | case GrPrimitiveType::kTriangleStrip: |
| 153 | return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 154 | case GrPrimitiveType::kPoints: |
| 155 | return VK_PRIMITIVE_TOPOLOGY_POINT_LIST; |
| 156 | case GrPrimitiveType::kLines: |
| 157 | return VK_PRIMITIVE_TOPOLOGY_LINE_LIST; |
| 158 | case GrPrimitiveType::kLineStrip: |
| 159 | return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP; |
| 160 | case GrPrimitiveType::kLinesAdjacency: |
| 161 | return VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY; |
| 162 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 163 | SK_ABORT("invalid GrPrimitiveType"); |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 164 | return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; |
| 165 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 166 | |
| 167 | static void setup_input_assembly_state(GrPrimitiveType primitiveType, |
| 168 | VkPipelineInputAssemblyStateCreateInfo* inputAssemblyInfo) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 169 | memset(inputAssemblyInfo, 0, sizeof(VkPipelineInputAssemblyStateCreateInfo)); |
| 170 | inputAssemblyInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; |
| 171 | inputAssemblyInfo->pNext = nullptr; |
| 172 | inputAssemblyInfo->flags = 0; |
| 173 | inputAssemblyInfo->primitiveRestartEnable = false; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 174 | inputAssemblyInfo->topology = gr_primitive_type_to_vk_topology(primitiveType); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | |
egdaniel | ec44099 | 2016-09-13 09:54:11 -0700 | [diff] [blame] | 178 | static VkStencilOp stencil_op_to_vk_stencil_op(GrStencilOp op) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 179 | static const VkStencilOp gTable[] = { |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 180 | VK_STENCIL_OP_KEEP, // kKeep |
| 181 | VK_STENCIL_OP_ZERO, // kZero |
| 182 | VK_STENCIL_OP_REPLACE, // kReplace |
| 183 | VK_STENCIL_OP_INVERT, // kInvert |
| 184 | VK_STENCIL_OP_INCREMENT_AND_WRAP, // kIncWrap |
| 185 | VK_STENCIL_OP_DECREMENT_AND_WRAP, // kDecWrap |
| 186 | VK_STENCIL_OP_INCREMENT_AND_CLAMP, // kIncClamp |
| 187 | VK_STENCIL_OP_DECREMENT_AND_CLAMP, // kDecClamp |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 188 | }; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 189 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilOpCount); |
| 190 | GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep); |
| 191 | GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero); |
| 192 | GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace); |
| 193 | GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert); |
| 194 | GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap); |
| 195 | GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap); |
| 196 | GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp); |
| 197 | GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp); |
| 198 | SkASSERT(op < (GrStencilOp)kGrStencilOpCount); |
| 199 | return gTable[(int)op]; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 200 | } |
| 201 | |
egdaniel | ec44099 | 2016-09-13 09:54:11 -0700 | [diff] [blame] | 202 | static VkCompareOp stencil_func_to_vk_compare_op(GrStencilTest test) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 203 | static const VkCompareOp gTable[] = { |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 204 | VK_COMPARE_OP_ALWAYS, // kAlways |
| 205 | VK_COMPARE_OP_NEVER, // kNever |
| 206 | VK_COMPARE_OP_GREATER, // kGreater |
| 207 | VK_COMPARE_OP_GREATER_OR_EQUAL, // kGEqual |
| 208 | VK_COMPARE_OP_LESS, // kLess |
| 209 | VK_COMPARE_OP_LESS_OR_EQUAL, // kLEqual |
| 210 | VK_COMPARE_OP_EQUAL, // kEqual |
| 211 | VK_COMPARE_OP_NOT_EQUAL, // kNotEqual |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 212 | }; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 213 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilTestCount); |
| 214 | GR_STATIC_ASSERT(0 == (int)GrStencilTest::kAlways); |
| 215 | GR_STATIC_ASSERT(1 == (int)GrStencilTest::kNever); |
| 216 | GR_STATIC_ASSERT(2 == (int)GrStencilTest::kGreater); |
| 217 | GR_STATIC_ASSERT(3 == (int)GrStencilTest::kGEqual); |
| 218 | GR_STATIC_ASSERT(4 == (int)GrStencilTest::kLess); |
| 219 | GR_STATIC_ASSERT(5 == (int)GrStencilTest::kLEqual); |
| 220 | GR_STATIC_ASSERT(6 == (int)GrStencilTest::kEqual); |
| 221 | GR_STATIC_ASSERT(7 == (int)GrStencilTest::kNotEqual); |
| 222 | SkASSERT(test < (GrStencilTest)kGrStencilTestCount); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 223 | |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 224 | return gTable[(int)test]; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 225 | } |
| 226 | |
egdaniel | ec44099 | 2016-09-13 09:54:11 -0700 | [diff] [blame] | 227 | static void setup_depth_stencil_state(const GrStencilSettings& stencilSettings, |
| 228 | VkPipelineDepthStencilStateCreateInfo* stencilInfo) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 229 | memset(stencilInfo, 0, sizeof(VkPipelineDepthStencilStateCreateInfo)); |
| 230 | stencilInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; |
| 231 | stencilInfo->pNext = nullptr; |
| 232 | stencilInfo->flags = 0; |
| 233 | // set depth testing defaults |
| 234 | stencilInfo->depthTestEnable = VK_FALSE; |
| 235 | stencilInfo->depthWriteEnable = VK_FALSE; |
| 236 | stencilInfo->depthCompareOp = VK_COMPARE_OP_ALWAYS; |
| 237 | stencilInfo->depthBoundsTestEnable = VK_FALSE; |
| 238 | stencilInfo->stencilTestEnable = !stencilSettings.isDisabled(); |
| 239 | if (!stencilSettings.isDisabled()) { |
| 240 | // Set front face |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 241 | const GrStencilSettings::Face& front = stencilSettings.front(); |
| 242 | stencilInfo->front.failOp = stencil_op_to_vk_stencil_op(front.fFailOp); |
| 243 | stencilInfo->front.passOp = stencil_op_to_vk_stencil_op(front.fPassOp); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 244 | stencilInfo->front.depthFailOp = stencilInfo->front.failOp; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 245 | stencilInfo->front.compareOp = stencil_func_to_vk_compare_op(front.fTest); |
| 246 | stencilInfo->front.compareMask = front.fTestMask; |
| 247 | stencilInfo->front.writeMask = front.fWriteMask; |
| 248 | stencilInfo->front.reference = front.fRef; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 249 | |
| 250 | // Set back face |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 251 | if (!stencilSettings.isTwoSided()) { |
| 252 | stencilInfo->back = stencilInfo->front; |
| 253 | } else { |
| 254 | const GrStencilSettings::Face& back = stencilSettings.back(); |
| 255 | stencilInfo->back.failOp = stencil_op_to_vk_stencil_op(back.fFailOp); |
| 256 | stencilInfo->back.passOp = stencil_op_to_vk_stencil_op(back.fPassOp); |
| 257 | stencilInfo->back.depthFailOp = stencilInfo->front.failOp; |
| 258 | stencilInfo->back.compareOp = stencil_func_to_vk_compare_op(back.fTest); |
| 259 | stencilInfo->back.compareMask = back.fTestMask; |
| 260 | stencilInfo->back.writeMask = back.fWriteMask; |
| 261 | stencilInfo->back.reference = back.fRef; |
| 262 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 263 | } |
| 264 | stencilInfo->minDepthBounds = 0.0f; |
| 265 | stencilInfo->maxDepthBounds = 1.0f; |
| 266 | } |
| 267 | |
egdaniel | ec44099 | 2016-09-13 09:54:11 -0700 | [diff] [blame] | 268 | static void setup_viewport_scissor_state(VkPipelineViewportStateCreateInfo* viewportInfo) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 269 | memset(viewportInfo, 0, sizeof(VkPipelineViewportStateCreateInfo)); |
| 270 | viewportInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; |
| 271 | viewportInfo->pNext = nullptr; |
| 272 | viewportInfo->flags = 0; |
| 273 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 274 | viewportInfo->viewportCount = 1; |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 275 | viewportInfo->pViewports = nullptr; // This is set dynamically |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 276 | |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 277 | viewportInfo->scissorCount = 1; |
| 278 | viewportInfo->pScissors = nullptr; // This is set dynamically |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 279 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 280 | SkASSERT(viewportInfo->viewportCount == viewportInfo->scissorCount); |
| 281 | } |
| 282 | |
Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 283 | static void setup_multisample_state(const GrPrimitiveProcessor& primProc, |
| 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 | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 291 | int numSamples = pipeline.proxy()->numColorSamples(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 292 | SkAssertResult(GrSampleCountToVkSampleCount(numSamples, |
| 293 | &multisampleInfo->rasterizationSamples)); |
ethannicholas | 28ef445 | 2016-03-25 09:26:03 -0700 | [diff] [blame] | 294 | float sampleShading = primProc.getSampleShading(); |
| 295 | SkASSERT(sampleShading == 0.0f || caps->sampleShadingSupport()); |
| 296 | multisampleInfo->sampleShadingEnable = sampleShading > 0.0f; |
| 297 | multisampleInfo->minSampleShading = sampleShading; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 298 | multisampleInfo->pSampleMask = nullptr; |
| 299 | multisampleInfo->alphaToCoverageEnable = VK_FALSE; |
| 300 | multisampleInfo->alphaToOneEnable = VK_FALSE; |
| 301 | } |
| 302 | |
| 303 | static VkBlendFactor blend_coeff_to_vk_blend(GrBlendCoeff coeff) { |
| 304 | static const VkBlendFactor gTable[] = { |
| 305 | VK_BLEND_FACTOR_ZERO, // kZero_GrBlendCoeff |
| 306 | VK_BLEND_FACTOR_ONE, // kOne_GrBlendCoeff |
| 307 | VK_BLEND_FACTOR_SRC_COLOR, // kSC_GrBlendCoeff |
| 308 | VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR, // kISC_GrBlendCoeff |
| 309 | VK_BLEND_FACTOR_DST_COLOR, // kDC_GrBlendCoeff |
| 310 | VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR, // kIDC_GrBlendCoeff |
| 311 | VK_BLEND_FACTOR_SRC_ALPHA, // kSA_GrBlendCoeff |
| 312 | VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, // kISA_GrBlendCoeff |
| 313 | VK_BLEND_FACTOR_DST_ALPHA, // kDA_GrBlendCoeff |
| 314 | VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA, // kIDA_GrBlendCoeff |
| 315 | VK_BLEND_FACTOR_CONSTANT_COLOR, // kConstC_GrBlendCoeff |
| 316 | VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR, // kIConstC_GrBlendCoeff |
| 317 | VK_BLEND_FACTOR_CONSTANT_ALPHA, // kConstA_GrBlendCoeff |
| 318 | VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA, // kIConstA_GrBlendCoeff |
| 319 | VK_BLEND_FACTOR_SRC1_COLOR, // kS2C_GrBlendCoeff |
| 320 | VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR, // kIS2C_GrBlendCoeff |
| 321 | VK_BLEND_FACTOR_SRC1_ALPHA, // kS2A_GrBlendCoeff |
| 322 | VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA, // kIS2A_GrBlendCoeff |
| 323 | |
| 324 | }; |
| 325 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrBlendCoeffCnt); |
| 326 | GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff); |
| 327 | GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff); |
| 328 | GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff); |
| 329 | GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff); |
| 330 | GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff); |
| 331 | GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff); |
| 332 | GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff); |
| 333 | GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff); |
| 334 | GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff); |
| 335 | GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff); |
| 336 | GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff); |
| 337 | GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff); |
| 338 | GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff); |
| 339 | GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff); |
| 340 | GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff); |
| 341 | GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff); |
| 342 | GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff); |
| 343 | GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff); |
| 344 | |
| 345 | SkASSERT((unsigned)coeff < kGrBlendCoeffCnt); |
| 346 | return gTable[coeff]; |
| 347 | } |
| 348 | |
| 349 | |
| 350 | static VkBlendOp blend_equation_to_vk_blend_op(GrBlendEquation equation) { |
| 351 | static const VkBlendOp gTable[] = { |
Greg Daniel | 313c695 | 2018-08-08 09:24:08 -0400 | [diff] [blame] | 352 | // Basic blend ops |
| 353 | VK_BLEND_OP_ADD, |
| 354 | VK_BLEND_OP_SUBTRACT, |
| 355 | VK_BLEND_OP_REVERSE_SUBTRACT, |
| 356 | |
| 357 | // Advanced blend ops |
| 358 | VK_BLEND_OP_SCREEN_EXT, |
| 359 | VK_BLEND_OP_OVERLAY_EXT, |
| 360 | VK_BLEND_OP_DARKEN_EXT, |
| 361 | VK_BLEND_OP_LIGHTEN_EXT, |
| 362 | VK_BLEND_OP_COLORDODGE_EXT, |
| 363 | VK_BLEND_OP_COLORBURN_EXT, |
| 364 | VK_BLEND_OP_HARDLIGHT_EXT, |
| 365 | VK_BLEND_OP_SOFTLIGHT_EXT, |
| 366 | VK_BLEND_OP_DIFFERENCE_EXT, |
| 367 | VK_BLEND_OP_EXCLUSION_EXT, |
| 368 | VK_BLEND_OP_MULTIPLY_EXT, |
| 369 | VK_BLEND_OP_HSL_HUE_EXT, |
| 370 | VK_BLEND_OP_HSL_SATURATION_EXT, |
| 371 | VK_BLEND_OP_HSL_COLOR_EXT, |
| 372 | VK_BLEND_OP_HSL_LUMINOSITY_EXT |
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, |
| 420 | }; |
| 421 | return gCoeffReferencesBlendConst[coeff]; |
| 422 | GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst)); |
| 423 | // Individual enum asserts already made in blend_coeff_to_vk_blend |
| 424 | } |
| 425 | |
egdaniel | ec44099 | 2016-09-13 09:54:11 -0700 | [diff] [blame] | 426 | static void setup_color_blend_state(const GrPipeline& pipeline, |
| 427 | VkPipelineColorBlendStateCreateInfo* colorBlendInfo, |
| 428 | VkPipelineColorBlendAttachmentState* attachmentState) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 429 | GrXferProcessor::BlendInfo blendInfo; |
| 430 | pipeline.getXferProcessor().getBlendInfo(&blendInfo); |
| 431 | |
| 432 | GrBlendEquation equation = blendInfo.fEquation; |
| 433 | GrBlendCoeff srcCoeff = blendInfo.fSrcBlend; |
| 434 | GrBlendCoeff dstCoeff = blendInfo.fDstBlend; |
| 435 | bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) && |
| 436 | kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff; |
| 437 | |
| 438 | memset(attachmentState, 0, sizeof(VkPipelineColorBlendAttachmentState)); |
| 439 | attachmentState->blendEnable = !blendOff; |
| 440 | if (!blendOff) { |
| 441 | attachmentState->srcColorBlendFactor = blend_coeff_to_vk_blend(srcCoeff); |
| 442 | attachmentState->dstColorBlendFactor = blend_coeff_to_vk_blend(dstCoeff); |
| 443 | attachmentState->colorBlendOp = blend_equation_to_vk_blend_op(equation); |
| 444 | attachmentState->srcAlphaBlendFactor = blend_coeff_to_vk_blend(srcCoeff); |
| 445 | attachmentState->dstAlphaBlendFactor = blend_coeff_to_vk_blend(dstCoeff); |
| 446 | attachmentState->alphaBlendOp = blend_equation_to_vk_blend_op(equation); |
| 447 | } |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 448 | |
| 449 | if (!blendInfo.fWriteColor) { |
| 450 | attachmentState->colorWriteMask = 0; |
| 451 | } else { |
| 452 | attachmentState->colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | |
| 453 | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; |
| 454 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 455 | |
| 456 | memset(colorBlendInfo, 0, sizeof(VkPipelineColorBlendStateCreateInfo)); |
| 457 | colorBlendInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; |
| 458 | colorBlendInfo->pNext = nullptr; |
| 459 | colorBlendInfo->flags = 0; |
| 460 | colorBlendInfo->logicOpEnable = VK_FALSE; |
| 461 | colorBlendInfo->attachmentCount = 1; |
| 462 | colorBlendInfo->pAttachments = attachmentState; |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 463 | // colorBlendInfo->blendConstants is set dynamically |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 464 | } |
| 465 | |
egdaniel | ec44099 | 2016-09-13 09:54:11 -0700 | [diff] [blame] | 466 | static void setup_raster_state(const GrPipeline& pipeline, |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame] | 467 | const GrCaps* caps, |
egdaniel | ec44099 | 2016-09-13 09:54:11 -0700 | [diff] [blame] | 468 | VkPipelineRasterizationStateCreateInfo* rasterInfo) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 469 | memset(rasterInfo, 0, sizeof(VkPipelineRasterizationStateCreateInfo)); |
| 470 | rasterInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; |
| 471 | rasterInfo->pNext = nullptr; |
| 472 | rasterInfo->flags = 0; |
| 473 | rasterInfo->depthClampEnable = VK_FALSE; |
| 474 | rasterInfo->rasterizerDiscardEnable = VK_FALSE; |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame] | 475 | rasterInfo->polygonMode = caps->wireframeMode() ? VK_POLYGON_MODE_LINE |
| 476 | : VK_POLYGON_MODE_FILL; |
Brian Salomon | f086167 | 2017-05-08 11:10:10 -0400 | [diff] [blame] | 477 | rasterInfo->cullMode = VK_CULL_MODE_NONE; |
Chris Dalton | b91c466 | 2018-08-01 10:46:22 -0600 | [diff] [blame] | 478 | rasterInfo->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 479 | rasterInfo->depthBiasEnable = VK_FALSE; |
| 480 | rasterInfo->depthBiasConstantFactor = 0.0f; |
| 481 | rasterInfo->depthBiasClamp = 0.0f; |
| 482 | rasterInfo->depthBiasSlopeFactor = 0.0f; |
| 483 | rasterInfo->lineWidth = 1.0f; |
| 484 | } |
| 485 | |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 486 | static void setup_dynamic_state(VkPipelineDynamicStateCreateInfo* dynamicInfo, |
| 487 | VkDynamicState* dynamicStates) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 488 | memset(dynamicInfo, 0, sizeof(VkPipelineDynamicStateCreateInfo)); |
| 489 | dynamicInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 490 | dynamicInfo->pNext = VK_NULL_HANDLE; |
| 491 | dynamicInfo->flags = 0; |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 492 | dynamicStates[0] = VK_DYNAMIC_STATE_VIEWPORT; |
| 493 | dynamicStates[1] = VK_DYNAMIC_STATE_SCISSOR; |
| 494 | dynamicStates[2] = VK_DYNAMIC_STATE_BLEND_CONSTANTS; |
| 495 | dynamicInfo->dynamicStateCount = 3; |
| 496 | dynamicInfo->pDynamicStates = dynamicStates; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 497 | } |
| 498 | |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 499 | GrVkPipeline* GrVkPipeline::Create(GrVkGpu* gpu, const GrPrimitiveProcessor& primProc, |
| 500 | const GrPipeline& pipeline, const GrStencilSettings& stencil, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 501 | VkPipelineShaderStageCreateInfo* shaderStageInfo, |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 502 | int shaderStageCount, GrPrimitiveType primitiveType, |
| 503 | const GrVkRenderPass& renderPass, VkPipelineLayout layout, |
jvanverth | 03509ea | 2016-03-02 13:19:47 -0800 | [diff] [blame] | 504 | VkPipelineCache cache) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 505 | VkPipelineVertexInputStateCreateInfo vertexInputInfo; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 506 | SkSTArray<2, VkVertexInputBindingDescription, true> bindingDescs; |
egdaniel | b05df0f | 2016-06-27 07:15:20 -0700 | [diff] [blame] | 507 | SkSTArray<16, VkVertexInputAttributeDescription> attributeDesc; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 508 | int totalAttributeCnt = primProc.numVertexAttributes() + primProc.numInstanceAttributes(); |
| 509 | SkASSERT(totalAttributeCnt <= gpu->vkCaps().maxVertexAttributes()); |
| 510 | VkVertexInputAttributeDescription* pAttribs = attributeDesc.push_back_n(totalAttributeCnt); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 511 | setup_vertex_input_state(primProc, &vertexInputInfo, &bindingDescs, pAttribs); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 512 | |
| 513 | VkPipelineInputAssemblyStateCreateInfo inputAssemblyInfo; |
| 514 | setup_input_assembly_state(primitiveType, &inputAssemblyInfo); |
| 515 | |
| 516 | VkPipelineDepthStencilStateCreateInfo depthStencilInfo; |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 517 | setup_depth_stencil_state(stencil, &depthStencilInfo); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 518 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 519 | VkPipelineViewportStateCreateInfo viewportInfo; |
egdaniel | ec44099 | 2016-09-13 09:54:11 -0700 | [diff] [blame] | 520 | setup_viewport_scissor_state(&viewportInfo); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 521 | |
| 522 | VkPipelineMultisampleStateCreateInfo multisampleInfo; |
Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 523 | setup_multisample_state(primProc, pipeline, gpu->caps(), &multisampleInfo); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 524 | |
| 525 | // We will only have one color attachment per pipeline. |
| 526 | VkPipelineColorBlendAttachmentState attachmentStates[1]; |
| 527 | VkPipelineColorBlendStateCreateInfo colorBlendInfo; |
egdaniel | ec44099 | 2016-09-13 09:54:11 -0700 | [diff] [blame] | 528 | setup_color_blend_state(pipeline, &colorBlendInfo, attachmentStates); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 529 | |
| 530 | VkPipelineRasterizationStateCreateInfo rasterInfo; |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame] | 531 | setup_raster_state(pipeline, gpu->caps(), &rasterInfo); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 532 | |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 533 | VkDynamicState dynamicStates[3]; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 534 | VkPipelineDynamicStateCreateInfo dynamicInfo; |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 535 | setup_dynamic_state(&dynamicInfo, dynamicStates); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 536 | |
| 537 | VkGraphicsPipelineCreateInfo pipelineCreateInfo; |
| 538 | memset(&pipelineCreateInfo, 0, sizeof(VkGraphicsPipelineCreateInfo)); |
| 539 | pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
| 540 | pipelineCreateInfo.pNext = nullptr; |
| 541 | pipelineCreateInfo.flags = 0; |
| 542 | pipelineCreateInfo.stageCount = shaderStageCount; |
| 543 | pipelineCreateInfo.pStages = shaderStageInfo; |
| 544 | pipelineCreateInfo.pVertexInputState = &vertexInputInfo; |
| 545 | pipelineCreateInfo.pInputAssemblyState = &inputAssemblyInfo; |
| 546 | pipelineCreateInfo.pTessellationState = nullptr; |
| 547 | pipelineCreateInfo.pViewportState = &viewportInfo; |
| 548 | pipelineCreateInfo.pRasterizationState = &rasterInfo; |
| 549 | pipelineCreateInfo.pMultisampleState = &multisampleInfo; |
| 550 | pipelineCreateInfo.pDepthStencilState = &depthStencilInfo; |
| 551 | pipelineCreateInfo.pColorBlendState = &colorBlendInfo; |
| 552 | pipelineCreateInfo.pDynamicState = &dynamicInfo; |
| 553 | pipelineCreateInfo.layout = layout; |
| 554 | pipelineCreateInfo.renderPass = renderPass.vkRenderPass(); |
| 555 | pipelineCreateInfo.subpass = 0; |
| 556 | pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE; |
| 557 | pipelineCreateInfo.basePipelineIndex = -1; |
| 558 | |
| 559 | VkPipeline vkPipeline; |
| 560 | VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateGraphicsPipelines(gpu->device(), |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 561 | cache, 1, |
| 562 | &pipelineCreateInfo, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 563 | nullptr, &vkPipeline)); |
| 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 | |
| 569 | return new GrVkPipeline(vkPipeline); |
| 570 | } |
| 571 | |
| 572 | void GrVkPipeline::freeGPUData(const GrVkGpu* gpu) const { |
| 573 | GR_VK_CALL(gpu->vkInterface(), DestroyPipeline(gpu->device(), fPipeline, nullptr)); |
| 574 | } |
| 575 | |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 576 | void GrVkPipeline::SetDynamicScissorRectState(GrVkGpu* gpu, |
| 577 | GrVkCommandBuffer* cmdBuffer, |
| 578 | const GrRenderTarget* renderTarget, |
| 579 | GrSurfaceOrigin rtOrigin, |
| 580 | SkIRect scissorRect) { |
| 581 | if (!scissorRect.intersect(SkIRect::MakeWH(renderTarget->width(), renderTarget->height()))) { |
| 582 | scissorRect.setEmpty(); |
| 583 | } |
| 584 | |
| 585 | VkRect2D scissor; |
| 586 | scissor.offset.x = scissorRect.fLeft; |
| 587 | scissor.extent.width = scissorRect.width(); |
| 588 | if (kTopLeft_GrSurfaceOrigin == rtOrigin) { |
| 589 | scissor.offset.y = scissorRect.fTop; |
| 590 | } else { |
| 591 | SkASSERT(kBottomLeft_GrSurfaceOrigin == rtOrigin); |
| 592 | scissor.offset.y = renderTarget->height() - scissorRect.fBottom; |
| 593 | } |
| 594 | scissor.extent.height = scissorRect.height(); |
| 595 | |
| 596 | SkASSERT(scissor.offset.x >= 0); |
| 597 | SkASSERT(scissor.offset.y >= 0); |
| 598 | cmdBuffer->setScissor(gpu, 0, 1, &scissor); |
| 599 | } |
| 600 | |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 601 | void GrVkPipeline::SetDynamicViewportState(GrVkGpu* gpu, |
| 602 | GrVkCommandBuffer* cmdBuffer, |
| 603 | const GrRenderTarget* renderTarget) { |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 604 | // We always use one viewport the size of the RT |
| 605 | VkViewport viewport; |
| 606 | viewport.x = 0.0f; |
| 607 | viewport.y = 0.0f; |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 608 | viewport.width = SkIntToScalar(renderTarget->width()); |
| 609 | viewport.height = SkIntToScalar(renderTarget->height()); |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 610 | viewport.minDepth = 0.0f; |
| 611 | viewport.maxDepth = 1.0f; |
| 612 | cmdBuffer->setViewport(gpu, 0, 1, &viewport); |
| 613 | } |
| 614 | |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 615 | void GrVkPipeline::SetDynamicBlendConstantState(GrVkGpu* gpu, |
| 616 | GrVkCommandBuffer* cmdBuffer, |
| 617 | GrPixelConfig pixelConfig, |
| 618 | const GrXferProcessor& xferProcessor) { |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 619 | GrXferProcessor::BlendInfo blendInfo; |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 620 | xferProcessor.getBlendInfo(&blendInfo); |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 621 | GrBlendCoeff srcCoeff = blendInfo.fSrcBlend; |
| 622 | GrBlendCoeff dstCoeff = blendInfo.fDstBlend; |
| 623 | float floatColors[4]; |
| 624 | if (blend_coeff_refs_constant(srcCoeff) || blend_coeff_refs_constant(dstCoeff)) { |
Greg Daniel | 6c9f101 | 2017-05-11 09:20:59 -0400 | [diff] [blame] | 625 | // Swizzle the blend to match what the shader will output. |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 626 | const GrSwizzle& swizzle = gpu->caps()->shaderCaps()->configOutputSwizzle(pixelConfig); |
Greg Daniel | 6c9f101 | 2017-05-11 09:20:59 -0400 | [diff] [blame] | 627 | GrColor blendConst = swizzle.applyTo(blendInfo.fBlendConstant); |
| 628 | GrColorToRGBAFloat(blendConst, floatColors); |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 629 | } else { |
| 630 | memset(floatColors, 0, 4 * sizeof(float)); |
| 631 | } |
| 632 | cmdBuffer->setBlendConstants(gpu, floatColors); |
| 633 | } |