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