blob: 90d5e4429dd1e68c4cf14b2e35c63cbdabcf5afc [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
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 Daniel164a9f02016-02-22 09:56:40 -05009#include "GrGeometryProcessor.h"
10#include "GrPipeline.h"
Brian Salomon1471df92018-06-08 10:49:00 -040011#include "GrStencilSettings.h"
egdaniel470d77a2016-03-18 12:50:27 -070012#include "GrVkCommandBuffer.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050013#include "GrVkGpu.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050014#include "GrVkRenderTarget.h"
15#include "GrVkUtil.h"
16
csmartdaltonb37cb232017-02-08 14:56:27 -050017static inline VkFormat attrib_type_to_vkformat(GrVertexAttribType type) {
18 switch (type) {
19 case kFloat_GrVertexAttribType:
20 return VK_FORMAT_R32_SFLOAT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040021 case kFloat2_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050022 return VK_FORMAT_R32G32_SFLOAT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040023 case kFloat3_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050024 return VK_FORMAT_R32G32B32_SFLOAT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040025 case kFloat4_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050026 return VK_FORMAT_R32G32B32A32_SFLOAT;
Brian Osmand4c29702018-09-14 16:16:55 -040027 case kHalf_GrVertexAttribType:
28 return VK_FORMAT_R16_SFLOAT;
29 case kHalf2_GrVertexAttribType:
30 return VK_FORMAT_R16G16_SFLOAT;
31 case kHalf3_GrVertexAttribType:
32 return VK_FORMAT_R16G16B16_SFLOAT;
33 case kHalf4_GrVertexAttribType:
34 return VK_FORMAT_R16G16B16A16_SFLOAT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040035 case kInt2_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050036 return VK_FORMAT_R32G32_SINT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040037 case kInt3_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050038 return VK_FORMAT_R32G32B32_SINT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040039 case kInt4_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050040 return VK_FORMAT_R32G32B32A32_SINT;
Ruiqi Maob609e6d2018-07-17 10:19:38 -040041 case kByte_GrVertexAttribType:
42 return VK_FORMAT_R8_SINT;
43 case kByte2_GrVertexAttribType:
44 return VK_FORMAT_R8G8_SINT;
45 case kByte3_GrVertexAttribType:
46 return VK_FORMAT_R8G8B8_SINT;
47 case kByte4_GrVertexAttribType:
48 return VK_FORMAT_R8G8B8A8_SINT;
49 case kUByte_GrVertexAttribType:
50 return VK_FORMAT_R8_UINT;
51 case kUByte2_GrVertexAttribType:
52 return VK_FORMAT_R8G8_UINT;
53 case kUByte3_GrVertexAttribType:
54 return VK_FORMAT_R8G8B8_UINT;
55 case kUByte4_GrVertexAttribType:
56 return VK_FORMAT_R8G8B8A8_UINT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040057 case kUByte_norm_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050058 return VK_FORMAT_R8_UNORM;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040059 case kUByte4_norm_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050060 return VK_FORMAT_R8G8B8A8_UNORM;
Chris Daltona045eea2017-10-24 13:22:10 -060061 case kShort2_GrVertexAttribType:
62 return VK_FORMAT_R16G16_SINT;
Brian Osmana5c578f2018-09-19 14:19:02 -040063 case kShort4_GrVertexAttribType:
64 return VK_FORMAT_R16G16B16A16_SINT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040065 case kUShort2_GrVertexAttribType:
Robert Phillips8296e752017-08-25 08:45:21 -040066 return VK_FORMAT_R16G16_UINT;
Chris Daltona045eea2017-10-24 13:22:10 -060067 case kUShort2_norm_GrVertexAttribType:
68 return VK_FORMAT_R16G16_UNORM;
csmartdaltonb37cb232017-02-08 14:56:27 -050069 case kInt_GrVertexAttribType:
70 return VK_FORMAT_R32_SINT;
71 case kUint_GrVertexAttribType:
72 return VK_FORMAT_R32_UINT;
73 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040074 SK_ABORT("Unknown vertex attrib type");
csmartdaltonb37cb232017-02-08 14:56:27 -050075 return VK_FORMAT_UNDEFINED;
Greg Daniel164a9f02016-02-22 09:56:40 -050076}
77
78static void setup_vertex_input_state(const GrPrimitiveProcessor& primProc,
Chris Dalton1d616352017-05-31 12:51:23 -060079 VkPipelineVertexInputStateCreateInfo* vertexInputInfo,
80 SkSTArray<2, VkVertexInputBindingDescription, true>* bindingDescs,
81 VkVertexInputAttributeDescription* attributeDesc) {
Kevin Lubick42846132018-01-05 10:11:11 -050082 uint32_t vertexBinding = 0, instanceBinding = 0;
Chris Dalton1d616352017-05-31 12:51:23 -060083
Brian Salomon92be2f72018-06-19 14:33:47 -040084 int nextBinding = bindingDescs->count();
85 if (primProc.hasVertexAttributes()) {
86 vertexBinding = nextBinding++;
Chris Dalton1d616352017-05-31 12:51:23 -060087 }
88
Brian Salomon92be2f72018-06-19 14:33:47 -040089 if (primProc.hasInstanceAttributes()) {
90 instanceBinding = nextBinding;
Chris Dalton1d616352017-05-31 12:51:23 -060091 }
Greg Daniel164a9f02016-02-22 09:56:40 -050092
93 // setup attribute descriptions
Brian Salomon92be2f72018-06-19 14:33:47 -040094 int vaCount = primProc.numVertexAttributes();
95 int attribIndex = 0;
96 size_t vertexAttributeOffset = 0;
Brian Osmanf04fb3c2018-11-12 15:34:00 -050097 for (const auto& attrib : primProc.vertexAttributes()) {
Brian Salomon92be2f72018-06-19 14:33:47 -040098 VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex];
Brian Osmanf04fb3c2018-11-12 15:34:00 -050099 vkAttrib.location = attribIndex++; // for now assume location = attribIndex
Brian Salomon92be2f72018-06-19 14:33:47 -0400100 vkAttrib.binding = vertexBinding;
Brian Osmand4c29702018-09-14 16:16:55 -0400101 vkAttrib.format = attrib_type_to_vkformat(attrib.cpuType());
Brian Salomon92be2f72018-06-19 14:33:47 -0400102 vkAttrib.offset = vertexAttributeOffset;
Brian Salomon92be2f72018-06-19 14:33:47 -0400103 vertexAttributeOffset += attrib.sizeAlign4();
104 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500105 SkASSERT(vertexAttributeOffset == primProc.vertexStride());
Brian Salomon92be2f72018-06-19 14:33:47 -0400106
107 int iaCount = primProc.numInstanceAttributes();
108 size_t instanceAttributeOffset = 0;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500109 for (const auto& attrib : primProc.instanceAttributes()) {
Brian Salomon92be2f72018-06-19 14:33:47 -0400110 VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex];
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500111 vkAttrib.location = attribIndex++; // for now assume location = attribIndex
Brian Salomon92be2f72018-06-19 14:33:47 -0400112 vkAttrib.binding = instanceBinding;
Brian Osmand4c29702018-09-14 16:16:55 -0400113 vkAttrib.format = attrib_type_to_vkformat(attrib.cpuType());
Brian Salomon92be2f72018-06-19 14:33:47 -0400114 vkAttrib.offset = instanceAttributeOffset;
Brian Salomon92be2f72018-06-19 14:33:47 -0400115 instanceAttributeOffset += attrib.sizeAlign4();
116 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500117 SkASSERT(instanceAttributeOffset == primProc.instanceStride());
Brian Salomon92be2f72018-06-19 14:33:47 -0400118
119 if (primProc.hasVertexAttributes()) {
120 bindingDescs->push_back() = {
121 vertexBinding,
122 (uint32_t) vertexAttributeOffset,
123 VK_VERTEX_INPUT_RATE_VERTEX
124 };
125 }
126 if (primProc.hasInstanceAttributes()) {
127 bindingDescs->push_back() = {
128 instanceBinding,
129 (uint32_t) instanceAttributeOffset,
130 VK_VERTEX_INPUT_RATE_INSTANCE
131 };
Greg Daniel164a9f02016-02-22 09:56:40 -0500132 }
133
134 memset(vertexInputInfo, 0, sizeof(VkPipelineVertexInputStateCreateInfo));
135 vertexInputInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
136 vertexInputInfo->pNext = nullptr;
137 vertexInputInfo->flags = 0;
Chris Dalton1d616352017-05-31 12:51:23 -0600138 vertexInputInfo->vertexBindingDescriptionCount = bindingDescs->count();
139 vertexInputInfo->pVertexBindingDescriptions = bindingDescs->begin();
Brian Salomon92be2f72018-06-19 14:33:47 -0400140 vertexInputInfo->vertexAttributeDescriptionCount = vaCount + iaCount;
Greg Daniel164a9f02016-02-22 09:56:40 -0500141 vertexInputInfo->pVertexAttributeDescriptions = attributeDesc;
142}
143
Chris Dalton3809bab2017-06-13 10:55:06 -0600144static VkPrimitiveTopology gr_primitive_type_to_vk_topology(GrPrimitiveType primitiveType) {
145 switch (primitiveType) {
146 case GrPrimitiveType::kTriangles:
147 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
148 case GrPrimitiveType::kTriangleStrip:
149 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -0600150 case GrPrimitiveType::kPoints:
151 return VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
152 case GrPrimitiveType::kLines:
153 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
154 case GrPrimitiveType::kLineStrip:
155 return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
156 case GrPrimitiveType::kLinesAdjacency:
157 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY;
158 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400159 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -0600160 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
161}
Greg Daniel164a9f02016-02-22 09:56:40 -0500162
163static void setup_input_assembly_state(GrPrimitiveType primitiveType,
164 VkPipelineInputAssemblyStateCreateInfo* inputAssemblyInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500165 memset(inputAssemblyInfo, 0, sizeof(VkPipelineInputAssemblyStateCreateInfo));
166 inputAssemblyInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
167 inputAssemblyInfo->pNext = nullptr;
168 inputAssemblyInfo->flags = 0;
169 inputAssemblyInfo->primitiveRestartEnable = false;
Chris Dalton3809bab2017-06-13 10:55:06 -0600170 inputAssemblyInfo->topology = gr_primitive_type_to_vk_topology(primitiveType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500171}
172
173
egdanielec440992016-09-13 09:54:11 -0700174static VkStencilOp stencil_op_to_vk_stencil_op(GrStencilOp op) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500175 static const VkStencilOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700176 VK_STENCIL_OP_KEEP, // kKeep
177 VK_STENCIL_OP_ZERO, // kZero
178 VK_STENCIL_OP_REPLACE, // kReplace
179 VK_STENCIL_OP_INVERT, // kInvert
180 VK_STENCIL_OP_INCREMENT_AND_WRAP, // kIncWrap
181 VK_STENCIL_OP_DECREMENT_AND_WRAP, // kDecWrap
182 VK_STENCIL_OP_INCREMENT_AND_CLAMP, // kIncClamp
183 VK_STENCIL_OP_DECREMENT_AND_CLAMP, // kDecClamp
Greg Daniel164a9f02016-02-22 09:56:40 -0500184 };
cdalton93a379b2016-05-11 13:58:08 -0700185 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilOpCount);
186 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
187 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
188 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
189 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
190 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
191 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
192 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
193 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
194 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
195 return gTable[(int)op];
Greg Daniel164a9f02016-02-22 09:56:40 -0500196}
197
egdanielec440992016-09-13 09:54:11 -0700198static VkCompareOp stencil_func_to_vk_compare_op(GrStencilTest test) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500199 static const VkCompareOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700200 VK_COMPARE_OP_ALWAYS, // kAlways
201 VK_COMPARE_OP_NEVER, // kNever
202 VK_COMPARE_OP_GREATER, // kGreater
203 VK_COMPARE_OP_GREATER_OR_EQUAL, // kGEqual
204 VK_COMPARE_OP_LESS, // kLess
205 VK_COMPARE_OP_LESS_OR_EQUAL, // kLEqual
206 VK_COMPARE_OP_EQUAL, // kEqual
207 VK_COMPARE_OP_NOT_EQUAL, // kNotEqual
Greg Daniel164a9f02016-02-22 09:56:40 -0500208 };
cdalton93a379b2016-05-11 13:58:08 -0700209 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilTestCount);
210 GR_STATIC_ASSERT(0 == (int)GrStencilTest::kAlways);
211 GR_STATIC_ASSERT(1 == (int)GrStencilTest::kNever);
212 GR_STATIC_ASSERT(2 == (int)GrStencilTest::kGreater);
213 GR_STATIC_ASSERT(3 == (int)GrStencilTest::kGEqual);
214 GR_STATIC_ASSERT(4 == (int)GrStencilTest::kLess);
215 GR_STATIC_ASSERT(5 == (int)GrStencilTest::kLEqual);
216 GR_STATIC_ASSERT(6 == (int)GrStencilTest::kEqual);
217 GR_STATIC_ASSERT(7 == (int)GrStencilTest::kNotEqual);
218 SkASSERT(test < (GrStencilTest)kGrStencilTestCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500219
cdalton93a379b2016-05-11 13:58:08 -0700220 return gTable[(int)test];
Greg Daniel164a9f02016-02-22 09:56:40 -0500221}
222
egdanielec440992016-09-13 09:54:11 -0700223static void setup_depth_stencil_state(const GrStencilSettings& stencilSettings,
224 VkPipelineDepthStencilStateCreateInfo* stencilInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500225 memset(stencilInfo, 0, sizeof(VkPipelineDepthStencilStateCreateInfo));
226 stencilInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
227 stencilInfo->pNext = nullptr;
228 stencilInfo->flags = 0;
229 // set depth testing defaults
230 stencilInfo->depthTestEnable = VK_FALSE;
231 stencilInfo->depthWriteEnable = VK_FALSE;
232 stencilInfo->depthCompareOp = VK_COMPARE_OP_ALWAYS;
233 stencilInfo->depthBoundsTestEnable = VK_FALSE;
234 stencilInfo->stencilTestEnable = !stencilSettings.isDisabled();
235 if (!stencilSettings.isDisabled()) {
236 // Set front face
cdalton93a379b2016-05-11 13:58:08 -0700237 const GrStencilSettings::Face& front = stencilSettings.front();
238 stencilInfo->front.failOp = stencil_op_to_vk_stencil_op(front.fFailOp);
239 stencilInfo->front.passOp = stencil_op_to_vk_stencil_op(front.fPassOp);
Greg Daniel164a9f02016-02-22 09:56:40 -0500240 stencilInfo->front.depthFailOp = stencilInfo->front.failOp;
cdalton93a379b2016-05-11 13:58:08 -0700241 stencilInfo->front.compareOp = stencil_func_to_vk_compare_op(front.fTest);
242 stencilInfo->front.compareMask = front.fTestMask;
243 stencilInfo->front.writeMask = front.fWriteMask;
244 stencilInfo->front.reference = front.fRef;
Greg Daniel164a9f02016-02-22 09:56:40 -0500245
246 // Set back face
cdalton93a379b2016-05-11 13:58:08 -0700247 if (!stencilSettings.isTwoSided()) {
248 stencilInfo->back = stencilInfo->front;
249 } else {
250 const GrStencilSettings::Face& back = stencilSettings.back();
251 stencilInfo->back.failOp = stencil_op_to_vk_stencil_op(back.fFailOp);
252 stencilInfo->back.passOp = stencil_op_to_vk_stencil_op(back.fPassOp);
253 stencilInfo->back.depthFailOp = stencilInfo->front.failOp;
254 stencilInfo->back.compareOp = stencil_func_to_vk_compare_op(back.fTest);
255 stencilInfo->back.compareMask = back.fTestMask;
256 stencilInfo->back.writeMask = back.fWriteMask;
257 stencilInfo->back.reference = back.fRef;
258 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500259 }
260 stencilInfo->minDepthBounds = 0.0f;
261 stencilInfo->maxDepthBounds = 1.0f;
262}
263
egdanielec440992016-09-13 09:54:11 -0700264static void setup_viewport_scissor_state(VkPipelineViewportStateCreateInfo* viewportInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500265 memset(viewportInfo, 0, sizeof(VkPipelineViewportStateCreateInfo));
266 viewportInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
267 viewportInfo->pNext = nullptr;
268 viewportInfo->flags = 0;
269
Greg Daniel164a9f02016-02-22 09:56:40 -0500270 viewportInfo->viewportCount = 1;
egdaniel470d77a2016-03-18 12:50:27 -0700271 viewportInfo->pViewports = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500272
egdaniel470d77a2016-03-18 12:50:27 -0700273 viewportInfo->scissorCount = 1;
274 viewportInfo->pScissors = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500275
Greg Daniel164a9f02016-02-22 09:56:40 -0500276 SkASSERT(viewportInfo->viewportCount == viewportInfo->scissorCount);
277}
278
Brian Salomonff168d92018-06-23 15:17:27 -0400279static void setup_multisample_state(const GrPrimitiveProcessor& primProc,
280 const GrPipeline& pipeline,
egdanielec440992016-09-13 09:54:11 -0700281 const GrCaps* caps,
282 VkPipelineMultisampleStateCreateInfo* multisampleInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500283 memset(multisampleInfo, 0, sizeof(VkPipelineMultisampleStateCreateInfo));
284 multisampleInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
285 multisampleInfo->pNext = nullptr;
286 multisampleInfo->flags = 0;
Robert Phillips2890fbf2017-07-26 15:48:41 -0400287 int numSamples = pipeline.proxy()->numColorSamples();
Greg Daniel164a9f02016-02-22 09:56:40 -0500288 SkAssertResult(GrSampleCountToVkSampleCount(numSamples,
289 &multisampleInfo->rasterizationSamples));
Brian Osmana63593a2018-12-06 15:54:53 -0500290 multisampleInfo->sampleShadingEnable = VK_FALSE;
291 multisampleInfo->minSampleShading = 0.0f;
Greg Daniel164a9f02016-02-22 09:56:40 -0500292 multisampleInfo->pSampleMask = nullptr;
293 multisampleInfo->alphaToCoverageEnable = VK_FALSE;
294 multisampleInfo->alphaToOneEnable = VK_FALSE;
295}
296
297static VkBlendFactor blend_coeff_to_vk_blend(GrBlendCoeff coeff) {
298 static const VkBlendFactor gTable[] = {
299 VK_BLEND_FACTOR_ZERO, // kZero_GrBlendCoeff
300 VK_BLEND_FACTOR_ONE, // kOne_GrBlendCoeff
301 VK_BLEND_FACTOR_SRC_COLOR, // kSC_GrBlendCoeff
302 VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR, // kISC_GrBlendCoeff
303 VK_BLEND_FACTOR_DST_COLOR, // kDC_GrBlendCoeff
304 VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR, // kIDC_GrBlendCoeff
305 VK_BLEND_FACTOR_SRC_ALPHA, // kSA_GrBlendCoeff
306 VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, // kISA_GrBlendCoeff
307 VK_BLEND_FACTOR_DST_ALPHA, // kDA_GrBlendCoeff
308 VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA, // kIDA_GrBlendCoeff
309 VK_BLEND_FACTOR_CONSTANT_COLOR, // kConstC_GrBlendCoeff
310 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR, // kIConstC_GrBlendCoeff
311 VK_BLEND_FACTOR_CONSTANT_ALPHA, // kConstA_GrBlendCoeff
312 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA, // kIConstA_GrBlendCoeff
313 VK_BLEND_FACTOR_SRC1_COLOR, // kS2C_GrBlendCoeff
314 VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR, // kIS2C_GrBlendCoeff
315 VK_BLEND_FACTOR_SRC1_ALPHA, // kS2A_GrBlendCoeff
316 VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA, // kIS2A_GrBlendCoeff
Mike Klein36743362018-11-06 08:23:30 -0500317 VK_BLEND_FACTOR_ZERO, // kIllegal_GrBlendCoeff
Greg Daniel164a9f02016-02-22 09:56:40 -0500318 };
319 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrBlendCoeffCnt);
320 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
321 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
322 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
323 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
324 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
325 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
326 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
327 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
328 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
329 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
330 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
331 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
332 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
333 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
334 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
335 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
336 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
337 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
338
339 SkASSERT((unsigned)coeff < kGrBlendCoeffCnt);
340 return gTable[coeff];
341}
342
343
344static VkBlendOp blend_equation_to_vk_blend_op(GrBlendEquation equation) {
345 static const VkBlendOp gTable[] = {
Greg Daniel313c6952018-08-08 09:24:08 -0400346 // Basic blend ops
347 VK_BLEND_OP_ADD,
348 VK_BLEND_OP_SUBTRACT,
349 VK_BLEND_OP_REVERSE_SUBTRACT,
350
351 // Advanced blend ops
352 VK_BLEND_OP_SCREEN_EXT,
353 VK_BLEND_OP_OVERLAY_EXT,
354 VK_BLEND_OP_DARKEN_EXT,
355 VK_BLEND_OP_LIGHTEN_EXT,
356 VK_BLEND_OP_COLORDODGE_EXT,
357 VK_BLEND_OP_COLORBURN_EXT,
358 VK_BLEND_OP_HARDLIGHT_EXT,
359 VK_BLEND_OP_SOFTLIGHT_EXT,
360 VK_BLEND_OP_DIFFERENCE_EXT,
361 VK_BLEND_OP_EXCLUSION_EXT,
362 VK_BLEND_OP_MULTIPLY_EXT,
363 VK_BLEND_OP_HSL_HUE_EXT,
364 VK_BLEND_OP_HSL_SATURATION_EXT,
365 VK_BLEND_OP_HSL_COLOR_EXT,
Mike Klein36743362018-11-06 08:23:30 -0500366 VK_BLEND_OP_HSL_LUMINOSITY_EXT,
367
368 // Illegal.
369 VK_BLEND_OP_ADD,
Greg Daniel164a9f02016-02-22 09:56:40 -0500370 };
Greg Daniel164a9f02016-02-22 09:56:40 -0500371 GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
372 GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
373 GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
Greg Daniel313c6952018-08-08 09:24:08 -0400374 GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
375 GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
376 GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
377 GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
378 GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
379 GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
380 GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
381 GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
382 GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
383 GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
384 GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
385 GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
386 GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
387 GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
388 GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
389 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrBlendEquationCnt);
Greg Daniel164a9f02016-02-22 09:56:40 -0500390
391 SkASSERT((unsigned)equation < kGrBlendCoeffCnt);
392 return gTable[equation];
393}
394
egdanielec440992016-09-13 09:54:11 -0700395static bool blend_coeff_refs_constant(GrBlendCoeff coeff) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500396 static const bool gCoeffReferencesBlendConst[] = {
397 false,
398 false,
399 false,
400 false,
401 false,
402 false,
403 false,
404 false,
405 false,
406 false,
407 true,
408 true,
409 true,
410 true,
411
412 // extended blend coeffs
413 false,
414 false,
415 false,
416 false,
Mike Klein36743362018-11-06 08:23:30 -0500417
418 // Illegal
419 false,
Greg Daniel164a9f02016-02-22 09:56:40 -0500420 };
421 return gCoeffReferencesBlendConst[coeff];
422 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
423 // Individual enum asserts already made in blend_coeff_to_vk_blend
424}
425
egdanielec440992016-09-13 09:54:11 -0700426static void setup_color_blend_state(const GrPipeline& pipeline,
427 VkPipelineColorBlendStateCreateInfo* colorBlendInfo,
428 VkPipelineColorBlendAttachmentState* attachmentState) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500429 GrXferProcessor::BlendInfo blendInfo;
430 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
431
432 GrBlendEquation equation = blendInfo.fEquation;
433 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
434 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
435 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
436 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
437
438 memset(attachmentState, 0, sizeof(VkPipelineColorBlendAttachmentState));
439 attachmentState->blendEnable = !blendOff;
440 if (!blendOff) {
441 attachmentState->srcColorBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
442 attachmentState->dstColorBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
443 attachmentState->colorBlendOp = blend_equation_to_vk_blend_op(equation);
444 attachmentState->srcAlphaBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
445 attachmentState->dstAlphaBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
446 attachmentState->alphaBlendOp = blend_equation_to_vk_blend_op(equation);
447 }
egdaniel3d5d9ac2016-03-01 12:56:15 -0800448
449 if (!blendInfo.fWriteColor) {
450 attachmentState->colorWriteMask = 0;
451 } else {
452 attachmentState->colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
453 VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
454 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500455
456 memset(colorBlendInfo, 0, sizeof(VkPipelineColorBlendStateCreateInfo));
457 colorBlendInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
458 colorBlendInfo->pNext = nullptr;
459 colorBlendInfo->flags = 0;
460 colorBlendInfo->logicOpEnable = VK_FALSE;
461 colorBlendInfo->attachmentCount = 1;
462 colorBlendInfo->pAttachments = attachmentState;
egdaniel470d77a2016-03-18 12:50:27 -0700463 // colorBlendInfo->blendConstants is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500464}
465
egdanielec440992016-09-13 09:54:11 -0700466static void setup_raster_state(const GrPipeline& pipeline,
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400467 const GrCaps* caps,
egdanielec440992016-09-13 09:54:11 -0700468 VkPipelineRasterizationStateCreateInfo* rasterInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500469 memset(rasterInfo, 0, sizeof(VkPipelineRasterizationStateCreateInfo));
470 rasterInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
471 rasterInfo->pNext = nullptr;
472 rasterInfo->flags = 0;
473 rasterInfo->depthClampEnable = VK_FALSE;
474 rasterInfo->rasterizerDiscardEnable = VK_FALSE;
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400475 rasterInfo->polygonMode = caps->wireframeMode() ? VK_POLYGON_MODE_LINE
476 : VK_POLYGON_MODE_FILL;
Brian Salomonf0861672017-05-08 11:10:10 -0400477 rasterInfo->cullMode = VK_CULL_MODE_NONE;
Chris Daltonb91c4662018-08-01 10:46:22 -0600478 rasterInfo->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Greg Daniel164a9f02016-02-22 09:56:40 -0500479 rasterInfo->depthBiasEnable = VK_FALSE;
480 rasterInfo->depthBiasConstantFactor = 0.0f;
481 rasterInfo->depthBiasClamp = 0.0f;
482 rasterInfo->depthBiasSlopeFactor = 0.0f;
483 rasterInfo->lineWidth = 1.0f;
484}
485
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000486static void setup_dynamic_state(VkPipelineDynamicStateCreateInfo* dynamicInfo,
487 VkDynamicState* dynamicStates) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500488 memset(dynamicInfo, 0, sizeof(VkPipelineDynamicStateCreateInfo));
489 dynamicInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
egdaniel470d77a2016-03-18 12:50:27 -0700490 dynamicInfo->pNext = VK_NULL_HANDLE;
491 dynamicInfo->flags = 0;
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000492 dynamicStates[0] = VK_DYNAMIC_STATE_VIEWPORT;
493 dynamicStates[1] = VK_DYNAMIC_STATE_SCISSOR;
494 dynamicStates[2] = VK_DYNAMIC_STATE_BLEND_CONSTANTS;
495 dynamicInfo->dynamicStateCount = 3;
496 dynamicInfo->pDynamicStates = dynamicStates;
Greg Daniel164a9f02016-02-22 09:56:40 -0500497}
498
Brian Salomon49348902018-06-26 09:12:38 -0400499GrVkPipeline* GrVkPipeline::Create(GrVkGpu* gpu, const GrPrimitiveProcessor& primProc,
500 const GrPipeline& pipeline, const GrStencilSettings& stencil,
Greg Daniel164a9f02016-02-22 09:56:40 -0500501 VkPipelineShaderStageCreateInfo* shaderStageInfo,
Brian Salomon49348902018-06-26 09:12:38 -0400502 int shaderStageCount, GrPrimitiveType primitiveType,
Greg Daniel99b88e02018-10-03 15:31:20 -0400503 VkRenderPass compatibleRenderPass, VkPipelineLayout layout,
jvanverth03509ea2016-03-02 13:19:47 -0800504 VkPipelineCache cache) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500505 VkPipelineVertexInputStateCreateInfo vertexInputInfo;
Chris Dalton1d616352017-05-31 12:51:23 -0600506 SkSTArray<2, VkVertexInputBindingDescription, true> bindingDescs;
egdanielb05df0f2016-06-27 07:15:20 -0700507 SkSTArray<16, VkVertexInputAttributeDescription> attributeDesc;
Brian Salomon92be2f72018-06-19 14:33:47 -0400508 int totalAttributeCnt = primProc.numVertexAttributes() + primProc.numInstanceAttributes();
509 SkASSERT(totalAttributeCnt <= gpu->vkCaps().maxVertexAttributes());
510 VkVertexInputAttributeDescription* pAttribs = attributeDesc.push_back_n(totalAttributeCnt);
Chris Dalton1d616352017-05-31 12:51:23 -0600511 setup_vertex_input_state(primProc, &vertexInputInfo, &bindingDescs, pAttribs);
Greg Daniel164a9f02016-02-22 09:56:40 -0500512
513 VkPipelineInputAssemblyStateCreateInfo inputAssemblyInfo;
514 setup_input_assembly_state(primitiveType, &inputAssemblyInfo);
515
516 VkPipelineDepthStencilStateCreateInfo depthStencilInfo;
csmartdaltonc633abb2016-11-01 08:55:55 -0700517 setup_depth_stencil_state(stencil, &depthStencilInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500518
Greg Daniel164a9f02016-02-22 09:56:40 -0500519 VkPipelineViewportStateCreateInfo viewportInfo;
egdanielec440992016-09-13 09:54:11 -0700520 setup_viewport_scissor_state(&viewportInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500521
522 VkPipelineMultisampleStateCreateInfo multisampleInfo;
Brian Salomonff168d92018-06-23 15:17:27 -0400523 setup_multisample_state(primProc, pipeline, gpu->caps(), &multisampleInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500524
525 // We will only have one color attachment per pipeline.
526 VkPipelineColorBlendAttachmentState attachmentStates[1];
527 VkPipelineColorBlendStateCreateInfo colorBlendInfo;
egdanielec440992016-09-13 09:54:11 -0700528 setup_color_blend_state(pipeline, &colorBlendInfo, attachmentStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500529
530 VkPipelineRasterizationStateCreateInfo rasterInfo;
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400531 setup_raster_state(pipeline, gpu->caps(), &rasterInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500532
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000533 VkDynamicState dynamicStates[3];
Greg Daniel164a9f02016-02-22 09:56:40 -0500534 VkPipelineDynamicStateCreateInfo dynamicInfo;
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000535 setup_dynamic_state(&dynamicInfo, dynamicStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500536
537 VkGraphicsPipelineCreateInfo pipelineCreateInfo;
538 memset(&pipelineCreateInfo, 0, sizeof(VkGraphicsPipelineCreateInfo));
539 pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
540 pipelineCreateInfo.pNext = nullptr;
541 pipelineCreateInfo.flags = 0;
542 pipelineCreateInfo.stageCount = shaderStageCount;
543 pipelineCreateInfo.pStages = shaderStageInfo;
544 pipelineCreateInfo.pVertexInputState = &vertexInputInfo;
545 pipelineCreateInfo.pInputAssemblyState = &inputAssemblyInfo;
546 pipelineCreateInfo.pTessellationState = nullptr;
547 pipelineCreateInfo.pViewportState = &viewportInfo;
548 pipelineCreateInfo.pRasterizationState = &rasterInfo;
549 pipelineCreateInfo.pMultisampleState = &multisampleInfo;
550 pipelineCreateInfo.pDepthStencilState = &depthStencilInfo;
551 pipelineCreateInfo.pColorBlendState = &colorBlendInfo;
552 pipelineCreateInfo.pDynamicState = &dynamicInfo;
553 pipelineCreateInfo.layout = layout;
Greg Daniel99b88e02018-10-03 15:31:20 -0400554 pipelineCreateInfo.renderPass = compatibleRenderPass;
Greg Daniel164a9f02016-02-22 09:56:40 -0500555 pipelineCreateInfo.subpass = 0;
556 pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
557 pipelineCreateInfo.basePipelineIndex = -1;
558
559 VkPipeline vkPipeline;
560 VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateGraphicsPipelines(gpu->device(),
halcanary9d524f22016-03-29 09:03:52 -0700561 cache, 1,
562 &pipelineCreateInfo,
Greg Daniel164a9f02016-02-22 09:56:40 -0500563 nullptr, &vkPipeline));
564 if (err) {
Greg Daniel5ba448c2018-02-26 13:30:47 -0500565 SkDebugf("Failed to create pipeline. Error: %d\n", err);
Greg Daniel164a9f02016-02-22 09:56:40 -0500566 return nullptr;
567 }
568
569 return new GrVkPipeline(vkPipeline);
570}
571
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500572void GrVkPipeline::freeGPUData(GrVkGpu* gpu) const {
Greg Daniel164a9f02016-02-22 09:56:40 -0500573 GR_VK_CALL(gpu->vkInterface(), DestroyPipeline(gpu->device(), fPipeline, nullptr));
574}
575
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000576void GrVkPipeline::SetDynamicScissorRectState(GrVkGpu* gpu,
577 GrVkCommandBuffer* cmdBuffer,
578 const GrRenderTarget* renderTarget,
579 GrSurfaceOrigin rtOrigin,
580 SkIRect scissorRect) {
581 if (!scissorRect.intersect(SkIRect::MakeWH(renderTarget->width(), renderTarget->height()))) {
582 scissorRect.setEmpty();
583 }
584
585 VkRect2D scissor;
586 scissor.offset.x = scissorRect.fLeft;
587 scissor.extent.width = scissorRect.width();
588 if (kTopLeft_GrSurfaceOrigin == rtOrigin) {
589 scissor.offset.y = scissorRect.fTop;
590 } else {
591 SkASSERT(kBottomLeft_GrSurfaceOrigin == rtOrigin);
592 scissor.offset.y = renderTarget->height() - scissorRect.fBottom;
593 }
594 scissor.extent.height = scissorRect.height();
595
596 SkASSERT(scissor.offset.x >= 0);
597 SkASSERT(scissor.offset.y >= 0);
598 cmdBuffer->setScissor(gpu, 0, 1, &scissor);
599}
600
Chris Dalton46983b72017-06-06 12:27:16 -0600601void GrVkPipeline::SetDynamicViewportState(GrVkGpu* gpu,
602 GrVkCommandBuffer* cmdBuffer,
603 const GrRenderTarget* renderTarget) {
egdaniel470d77a2016-03-18 12:50:27 -0700604 // We always use one viewport the size of the RT
605 VkViewport viewport;
606 viewport.x = 0.0f;
607 viewport.y = 0.0f;
Chris Dalton46983b72017-06-06 12:27:16 -0600608 viewport.width = SkIntToScalar(renderTarget->width());
609 viewport.height = SkIntToScalar(renderTarget->height());
egdaniel470d77a2016-03-18 12:50:27 -0700610 viewport.minDepth = 0.0f;
611 viewport.maxDepth = 1.0f;
612 cmdBuffer->setViewport(gpu, 0, 1, &viewport);
613}
614
Chris Dalton46983b72017-06-06 12:27:16 -0600615void GrVkPipeline::SetDynamicBlendConstantState(GrVkGpu* gpu,
616 GrVkCommandBuffer* cmdBuffer,
617 GrPixelConfig pixelConfig,
618 const GrXferProcessor& xferProcessor) {
egdaniel470d77a2016-03-18 12:50:27 -0700619 GrXferProcessor::BlendInfo blendInfo;
Chris Dalton46983b72017-06-06 12:27:16 -0600620 xferProcessor.getBlendInfo(&blendInfo);
egdaniel470d77a2016-03-18 12:50:27 -0700621 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
622 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
623 float floatColors[4];
624 if (blend_coeff_refs_constant(srcCoeff) || blend_coeff_refs_constant(dstCoeff)) {
Greg Daniel6c9f1012017-05-11 09:20:59 -0400625 // Swizzle the blend to match what the shader will output.
Chris Dalton46983b72017-06-06 12:27:16 -0600626 const GrSwizzle& swizzle = gpu->caps()->shaderCaps()->configOutputSwizzle(pixelConfig);
Brian Osman422f95b2018-11-05 16:49:04 -0500627 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
628 floatColors[0] = blendConst.fR;
629 floatColors[1] = blendConst.fG;
630 floatColors[2] = blendConst.fB;
631 floatColors[3] = blendConst.fA;
egdaniel470d77a2016-03-18 12:50:27 -0700632 } else {
633 memset(floatColors, 0, 4 * sizeof(float));
634 }
635 cmdBuffer->setBlendConstants(gpu, floatColors);
636}