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