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