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