blob: c07f08299972b6a8178b1c33b2cea51e7432ca9c [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;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040063 case kUShort2_GrVertexAttribType:
Robert Phillips8296e752017-08-25 08:45:21 -040064 return VK_FORMAT_R16G16_UINT;
Chris Daltona045eea2017-10-24 13:22:10 -060065 case kUShort2_norm_GrVertexAttribType:
66 return VK_FORMAT_R16G16_UNORM;
csmartdaltonb37cb232017-02-08 14:56:27 -050067 case kInt_GrVertexAttribType:
68 return VK_FORMAT_R32_SINT;
69 case kUint_GrVertexAttribType:
70 return VK_FORMAT_R32_UINT;
71 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040072 SK_ABORT("Unknown vertex attrib type");
csmartdaltonb37cb232017-02-08 14:56:27 -050073 return VK_FORMAT_UNDEFINED;
Greg Daniel164a9f02016-02-22 09:56:40 -050074}
75
76static void setup_vertex_input_state(const GrPrimitiveProcessor& primProc,
Chris Dalton1d616352017-05-31 12:51:23 -060077 VkPipelineVertexInputStateCreateInfo* vertexInputInfo,
78 SkSTArray<2, VkVertexInputBindingDescription, true>* bindingDescs,
79 VkVertexInputAttributeDescription* attributeDesc) {
Kevin Lubick42846132018-01-05 10:11:11 -050080 uint32_t vertexBinding = 0, instanceBinding = 0;
Chris Dalton1d616352017-05-31 12:51:23 -060081
Brian Salomon92be2f72018-06-19 14:33:47 -040082 int nextBinding = bindingDescs->count();
83 if (primProc.hasVertexAttributes()) {
84 vertexBinding = nextBinding++;
Chris Dalton1d616352017-05-31 12:51:23 -060085 }
86
Brian Salomon92be2f72018-06-19 14:33:47 -040087 if (primProc.hasInstanceAttributes()) {
88 instanceBinding = nextBinding;
Chris Dalton1d616352017-05-31 12:51:23 -060089 }
Greg Daniel164a9f02016-02-22 09:56:40 -050090
91 // setup attribute descriptions
Brian Salomon92be2f72018-06-19 14:33:47 -040092 int vaCount = primProc.numVertexAttributes();
93 int attribIndex = 0;
94 size_t vertexAttributeOffset = 0;
95 for (; attribIndex < vaCount; attribIndex++) {
96 const GrGeometryProcessor::Attribute& attrib = primProc.vertexAttribute(attribIndex);
97 VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex];
98 vkAttrib.location = attribIndex; // for now assume location = attribIndex
99 vkAttrib.binding = vertexBinding;
Brian Osmand4c29702018-09-14 16:16:55 -0400100 vkAttrib.format = attrib_type_to_vkformat(attrib.cpuType());
Brian Salomon92be2f72018-06-19 14:33:47 -0400101 vkAttrib.offset = vertexAttributeOffset;
102 SkASSERT(vkAttrib.offset == primProc.debugOnly_vertexAttributeOffset(attribIndex));
103 vertexAttributeOffset += attrib.sizeAlign4();
104 }
105 SkASSERT(vertexAttributeOffset == primProc.debugOnly_vertexStride());
106
107 int iaCount = primProc.numInstanceAttributes();
108 size_t instanceAttributeOffset = 0;
109 for (int iaIndex = 0; iaIndex < iaCount; ++iaIndex, ++attribIndex) {
110 const GrGeometryProcessor::Attribute& attrib = primProc.instanceAttribute(iaIndex);
111 VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex];
112 vkAttrib.location = attribIndex; // for now assume location = attribIndex
113 vkAttrib.binding = instanceBinding;
Brian Osmand4c29702018-09-14 16:16:55 -0400114 vkAttrib.format = attrib_type_to_vkformat(attrib.cpuType());
Brian Salomon92be2f72018-06-19 14:33:47 -0400115 vkAttrib.offset = instanceAttributeOffset;
116 SkASSERT(vkAttrib.offset == primProc.debugOnly_instanceAttributeOffset(iaIndex));
117 instanceAttributeOffset += attrib.sizeAlign4();
118 }
119 SkASSERT(instanceAttributeOffset == primProc.debugOnly_instanceStride());
120
121 if (primProc.hasVertexAttributes()) {
122 bindingDescs->push_back() = {
123 vertexBinding,
124 (uint32_t) vertexAttributeOffset,
125 VK_VERTEX_INPUT_RATE_VERTEX
126 };
127 }
128 if (primProc.hasInstanceAttributes()) {
129 bindingDescs->push_back() = {
130 instanceBinding,
131 (uint32_t) instanceAttributeOffset,
132 VK_VERTEX_INPUT_RATE_INSTANCE
133 };
Greg Daniel164a9f02016-02-22 09:56:40 -0500134 }
135
136 memset(vertexInputInfo, 0, sizeof(VkPipelineVertexInputStateCreateInfo));
137 vertexInputInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
138 vertexInputInfo->pNext = nullptr;
139 vertexInputInfo->flags = 0;
Chris Dalton1d616352017-05-31 12:51:23 -0600140 vertexInputInfo->vertexBindingDescriptionCount = bindingDescs->count();
141 vertexInputInfo->pVertexBindingDescriptions = bindingDescs->begin();
Brian Salomon92be2f72018-06-19 14:33:47 -0400142 vertexInputInfo->vertexAttributeDescriptionCount = vaCount + iaCount;
Greg Daniel164a9f02016-02-22 09:56:40 -0500143 vertexInputInfo->pVertexAttributeDescriptions = attributeDesc;
144}
145
Chris Dalton3809bab2017-06-13 10:55:06 -0600146static VkPrimitiveTopology gr_primitive_type_to_vk_topology(GrPrimitiveType primitiveType) {
147 switch (primitiveType) {
148 case GrPrimitiveType::kTriangles:
149 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
150 case GrPrimitiveType::kTriangleStrip:
151 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -0600152 case GrPrimitiveType::kPoints:
153 return VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
154 case GrPrimitiveType::kLines:
155 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
156 case GrPrimitiveType::kLineStrip:
157 return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
158 case GrPrimitiveType::kLinesAdjacency:
159 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY;
160 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400161 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -0600162 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
163}
Greg Daniel164a9f02016-02-22 09:56:40 -0500164
165static void setup_input_assembly_state(GrPrimitiveType primitiveType,
166 VkPipelineInputAssemblyStateCreateInfo* inputAssemblyInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500167 memset(inputAssemblyInfo, 0, sizeof(VkPipelineInputAssemblyStateCreateInfo));
168 inputAssemblyInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
169 inputAssemblyInfo->pNext = nullptr;
170 inputAssemblyInfo->flags = 0;
171 inputAssemblyInfo->primitiveRestartEnable = false;
Chris Dalton3809bab2017-06-13 10:55:06 -0600172 inputAssemblyInfo->topology = gr_primitive_type_to_vk_topology(primitiveType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500173}
174
175
egdanielec440992016-09-13 09:54:11 -0700176static VkStencilOp stencil_op_to_vk_stencil_op(GrStencilOp op) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500177 static const VkStencilOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700178 VK_STENCIL_OP_KEEP, // kKeep
179 VK_STENCIL_OP_ZERO, // kZero
180 VK_STENCIL_OP_REPLACE, // kReplace
181 VK_STENCIL_OP_INVERT, // kInvert
182 VK_STENCIL_OP_INCREMENT_AND_WRAP, // kIncWrap
183 VK_STENCIL_OP_DECREMENT_AND_WRAP, // kDecWrap
184 VK_STENCIL_OP_INCREMENT_AND_CLAMP, // kIncClamp
185 VK_STENCIL_OP_DECREMENT_AND_CLAMP, // kDecClamp
Greg Daniel164a9f02016-02-22 09:56:40 -0500186 };
cdalton93a379b2016-05-11 13:58:08 -0700187 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilOpCount);
188 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
189 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
190 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
191 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
192 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
193 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
194 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
195 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
196 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
197 return gTable[(int)op];
Greg Daniel164a9f02016-02-22 09:56:40 -0500198}
199
egdanielec440992016-09-13 09:54:11 -0700200static VkCompareOp stencil_func_to_vk_compare_op(GrStencilTest test) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500201 static const VkCompareOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700202 VK_COMPARE_OP_ALWAYS, // kAlways
203 VK_COMPARE_OP_NEVER, // kNever
204 VK_COMPARE_OP_GREATER, // kGreater
205 VK_COMPARE_OP_GREATER_OR_EQUAL, // kGEqual
206 VK_COMPARE_OP_LESS, // kLess
207 VK_COMPARE_OP_LESS_OR_EQUAL, // kLEqual
208 VK_COMPARE_OP_EQUAL, // kEqual
209 VK_COMPARE_OP_NOT_EQUAL, // kNotEqual
Greg Daniel164a9f02016-02-22 09:56:40 -0500210 };
cdalton93a379b2016-05-11 13:58:08 -0700211 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilTestCount);
212 GR_STATIC_ASSERT(0 == (int)GrStencilTest::kAlways);
213 GR_STATIC_ASSERT(1 == (int)GrStencilTest::kNever);
214 GR_STATIC_ASSERT(2 == (int)GrStencilTest::kGreater);
215 GR_STATIC_ASSERT(3 == (int)GrStencilTest::kGEqual);
216 GR_STATIC_ASSERT(4 == (int)GrStencilTest::kLess);
217 GR_STATIC_ASSERT(5 == (int)GrStencilTest::kLEqual);
218 GR_STATIC_ASSERT(6 == (int)GrStencilTest::kEqual);
219 GR_STATIC_ASSERT(7 == (int)GrStencilTest::kNotEqual);
220 SkASSERT(test < (GrStencilTest)kGrStencilTestCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500221
cdalton93a379b2016-05-11 13:58:08 -0700222 return gTable[(int)test];
Greg Daniel164a9f02016-02-22 09:56:40 -0500223}
224
egdanielec440992016-09-13 09:54:11 -0700225static void setup_depth_stencil_state(const GrStencilSettings& stencilSettings,
226 VkPipelineDepthStencilStateCreateInfo* stencilInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500227 memset(stencilInfo, 0, sizeof(VkPipelineDepthStencilStateCreateInfo));
228 stencilInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
229 stencilInfo->pNext = nullptr;
230 stencilInfo->flags = 0;
231 // set depth testing defaults
232 stencilInfo->depthTestEnable = VK_FALSE;
233 stencilInfo->depthWriteEnable = VK_FALSE;
234 stencilInfo->depthCompareOp = VK_COMPARE_OP_ALWAYS;
235 stencilInfo->depthBoundsTestEnable = VK_FALSE;
236 stencilInfo->stencilTestEnable = !stencilSettings.isDisabled();
237 if (!stencilSettings.isDisabled()) {
238 // Set front face
cdalton93a379b2016-05-11 13:58:08 -0700239 const GrStencilSettings::Face& front = stencilSettings.front();
240 stencilInfo->front.failOp = stencil_op_to_vk_stencil_op(front.fFailOp);
241 stencilInfo->front.passOp = stencil_op_to_vk_stencil_op(front.fPassOp);
Greg Daniel164a9f02016-02-22 09:56:40 -0500242 stencilInfo->front.depthFailOp = stencilInfo->front.failOp;
cdalton93a379b2016-05-11 13:58:08 -0700243 stencilInfo->front.compareOp = stencil_func_to_vk_compare_op(front.fTest);
244 stencilInfo->front.compareMask = front.fTestMask;
245 stencilInfo->front.writeMask = front.fWriteMask;
246 stencilInfo->front.reference = front.fRef;
Greg Daniel164a9f02016-02-22 09:56:40 -0500247
248 // Set back face
cdalton93a379b2016-05-11 13:58:08 -0700249 if (!stencilSettings.isTwoSided()) {
250 stencilInfo->back = stencilInfo->front;
251 } else {
252 const GrStencilSettings::Face& back = stencilSettings.back();
253 stencilInfo->back.failOp = stencil_op_to_vk_stencil_op(back.fFailOp);
254 stencilInfo->back.passOp = stencil_op_to_vk_stencil_op(back.fPassOp);
255 stencilInfo->back.depthFailOp = stencilInfo->front.failOp;
256 stencilInfo->back.compareOp = stencil_func_to_vk_compare_op(back.fTest);
257 stencilInfo->back.compareMask = back.fTestMask;
258 stencilInfo->back.writeMask = back.fWriteMask;
259 stencilInfo->back.reference = back.fRef;
260 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500261 }
262 stencilInfo->minDepthBounds = 0.0f;
263 stencilInfo->maxDepthBounds = 1.0f;
264}
265
egdanielec440992016-09-13 09:54:11 -0700266static void setup_viewport_scissor_state(VkPipelineViewportStateCreateInfo* viewportInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500267 memset(viewportInfo, 0, sizeof(VkPipelineViewportStateCreateInfo));
268 viewportInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
269 viewportInfo->pNext = nullptr;
270 viewportInfo->flags = 0;
271
Greg Daniel164a9f02016-02-22 09:56:40 -0500272 viewportInfo->viewportCount = 1;
egdaniel470d77a2016-03-18 12:50:27 -0700273 viewportInfo->pViewports = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500274
egdaniel470d77a2016-03-18 12:50:27 -0700275 viewportInfo->scissorCount = 1;
276 viewportInfo->pScissors = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500277
Greg Daniel164a9f02016-02-22 09:56:40 -0500278 SkASSERT(viewportInfo->viewportCount == viewportInfo->scissorCount);
279}
280
Brian Salomonff168d92018-06-23 15:17:27 -0400281static void setup_multisample_state(const GrPrimitiveProcessor& primProc,
282 const GrPipeline& pipeline,
egdanielec440992016-09-13 09:54:11 -0700283 const GrCaps* caps,
284 VkPipelineMultisampleStateCreateInfo* multisampleInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500285 memset(multisampleInfo, 0, sizeof(VkPipelineMultisampleStateCreateInfo));
286 multisampleInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
287 multisampleInfo->pNext = nullptr;
288 multisampleInfo->flags = 0;
Robert Phillips2890fbf2017-07-26 15:48:41 -0400289 int numSamples = pipeline.proxy()->numColorSamples();
Greg Daniel164a9f02016-02-22 09:56:40 -0500290 SkAssertResult(GrSampleCountToVkSampleCount(numSamples,
291 &multisampleInfo->rasterizationSamples));
ethannicholas28ef4452016-03-25 09:26:03 -0700292 float sampleShading = primProc.getSampleShading();
293 SkASSERT(sampleShading == 0.0f || caps->sampleShadingSupport());
294 multisampleInfo->sampleShadingEnable = sampleShading > 0.0f;
295 multisampleInfo->minSampleShading = sampleShading;
Greg Daniel164a9f02016-02-22 09:56:40 -0500296 multisampleInfo->pSampleMask = nullptr;
297 multisampleInfo->alphaToCoverageEnable = VK_FALSE;
298 multisampleInfo->alphaToOneEnable = VK_FALSE;
299}
300
301static VkBlendFactor blend_coeff_to_vk_blend(GrBlendCoeff coeff) {
302 static const VkBlendFactor gTable[] = {
303 VK_BLEND_FACTOR_ZERO, // kZero_GrBlendCoeff
304 VK_BLEND_FACTOR_ONE, // kOne_GrBlendCoeff
305 VK_BLEND_FACTOR_SRC_COLOR, // kSC_GrBlendCoeff
306 VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR, // kISC_GrBlendCoeff
307 VK_BLEND_FACTOR_DST_COLOR, // kDC_GrBlendCoeff
308 VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR, // kIDC_GrBlendCoeff
309 VK_BLEND_FACTOR_SRC_ALPHA, // kSA_GrBlendCoeff
310 VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, // kISA_GrBlendCoeff
311 VK_BLEND_FACTOR_DST_ALPHA, // kDA_GrBlendCoeff
312 VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA, // kIDA_GrBlendCoeff
313 VK_BLEND_FACTOR_CONSTANT_COLOR, // kConstC_GrBlendCoeff
314 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR, // kIConstC_GrBlendCoeff
315 VK_BLEND_FACTOR_CONSTANT_ALPHA, // kConstA_GrBlendCoeff
316 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA, // kIConstA_GrBlendCoeff
317 VK_BLEND_FACTOR_SRC1_COLOR, // kS2C_GrBlendCoeff
318 VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR, // kIS2C_GrBlendCoeff
319 VK_BLEND_FACTOR_SRC1_ALPHA, // kS2A_GrBlendCoeff
320 VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA, // kIS2A_GrBlendCoeff
321
322 };
323 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrBlendCoeffCnt);
324 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
325 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
326 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
327 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
328 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
329 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
330 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
331 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
332 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
333 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
334 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
335 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
336 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
337 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
338 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
339 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
340 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
341 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
342
343 SkASSERT((unsigned)coeff < kGrBlendCoeffCnt);
344 return gTable[coeff];
345}
346
347
348static VkBlendOp blend_equation_to_vk_blend_op(GrBlendEquation equation) {
349 static const VkBlendOp gTable[] = {
Greg Daniel313c6952018-08-08 09:24:08 -0400350 // Basic blend ops
351 VK_BLEND_OP_ADD,
352 VK_BLEND_OP_SUBTRACT,
353 VK_BLEND_OP_REVERSE_SUBTRACT,
354
355 // Advanced blend ops
356 VK_BLEND_OP_SCREEN_EXT,
357 VK_BLEND_OP_OVERLAY_EXT,
358 VK_BLEND_OP_DARKEN_EXT,
359 VK_BLEND_OP_LIGHTEN_EXT,
360 VK_BLEND_OP_COLORDODGE_EXT,
361 VK_BLEND_OP_COLORBURN_EXT,
362 VK_BLEND_OP_HARDLIGHT_EXT,
363 VK_BLEND_OP_SOFTLIGHT_EXT,
364 VK_BLEND_OP_DIFFERENCE_EXT,
365 VK_BLEND_OP_EXCLUSION_EXT,
366 VK_BLEND_OP_MULTIPLY_EXT,
367 VK_BLEND_OP_HSL_HUE_EXT,
368 VK_BLEND_OP_HSL_SATURATION_EXT,
369 VK_BLEND_OP_HSL_COLOR_EXT,
370 VK_BLEND_OP_HSL_LUMINOSITY_EXT
Greg Daniel164a9f02016-02-22 09:56:40 -0500371 };
Greg Daniel164a9f02016-02-22 09:56:40 -0500372 GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
373 GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
374 GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
Greg Daniel313c6952018-08-08 09:24:08 -0400375 GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
376 GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
377 GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
378 GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
379 GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
380 GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
381 GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
382 GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
383 GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
384 GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
385 GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
386 GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
387 GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
388 GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
389 GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
390 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrBlendEquationCnt);
Greg Daniel164a9f02016-02-22 09:56:40 -0500391
392 SkASSERT((unsigned)equation < kGrBlendCoeffCnt);
393 return gTable[equation];
394}
395
egdanielec440992016-09-13 09:54:11 -0700396static bool blend_coeff_refs_constant(GrBlendCoeff coeff) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500397 static const bool gCoeffReferencesBlendConst[] = {
398 false,
399 false,
400 false,
401 false,
402 false,
403 false,
404 false,
405 false,
406 false,
407 false,
408 true,
409 true,
410 true,
411 true,
412
413 // extended blend coeffs
414 false,
415 false,
416 false,
417 false,
418 };
419 return gCoeffReferencesBlendConst[coeff];
420 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
421 // Individual enum asserts already made in blend_coeff_to_vk_blend
422}
423
egdanielec440992016-09-13 09:54:11 -0700424static void setup_color_blend_state(const GrPipeline& pipeline,
425 VkPipelineColorBlendStateCreateInfo* colorBlendInfo,
426 VkPipelineColorBlendAttachmentState* attachmentState) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500427 GrXferProcessor::BlendInfo blendInfo;
428 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
429
430 GrBlendEquation equation = blendInfo.fEquation;
431 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
432 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
433 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
434 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
435
436 memset(attachmentState, 0, sizeof(VkPipelineColorBlendAttachmentState));
437 attachmentState->blendEnable = !blendOff;
438 if (!blendOff) {
439 attachmentState->srcColorBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
440 attachmentState->dstColorBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
441 attachmentState->colorBlendOp = blend_equation_to_vk_blend_op(equation);
442 attachmentState->srcAlphaBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
443 attachmentState->dstAlphaBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
444 attachmentState->alphaBlendOp = blend_equation_to_vk_blend_op(equation);
445 }
egdaniel3d5d9ac2016-03-01 12:56:15 -0800446
447 if (!blendInfo.fWriteColor) {
448 attachmentState->colorWriteMask = 0;
449 } else {
450 attachmentState->colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
451 VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
452 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500453
454 memset(colorBlendInfo, 0, sizeof(VkPipelineColorBlendStateCreateInfo));
455 colorBlendInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
456 colorBlendInfo->pNext = nullptr;
457 colorBlendInfo->flags = 0;
458 colorBlendInfo->logicOpEnable = VK_FALSE;
459 colorBlendInfo->attachmentCount = 1;
460 colorBlendInfo->pAttachments = attachmentState;
egdaniel470d77a2016-03-18 12:50:27 -0700461 // colorBlendInfo->blendConstants is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500462}
463
egdanielec440992016-09-13 09:54:11 -0700464static void setup_raster_state(const GrPipeline& pipeline,
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400465 const GrCaps* caps,
egdanielec440992016-09-13 09:54:11 -0700466 VkPipelineRasterizationStateCreateInfo* rasterInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500467 memset(rasterInfo, 0, sizeof(VkPipelineRasterizationStateCreateInfo));
468 rasterInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
469 rasterInfo->pNext = nullptr;
470 rasterInfo->flags = 0;
471 rasterInfo->depthClampEnable = VK_FALSE;
472 rasterInfo->rasterizerDiscardEnable = VK_FALSE;
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400473 rasterInfo->polygonMode = caps->wireframeMode() ? VK_POLYGON_MODE_LINE
474 : VK_POLYGON_MODE_FILL;
Brian Salomonf0861672017-05-08 11:10:10 -0400475 rasterInfo->cullMode = VK_CULL_MODE_NONE;
Chris Daltonb91c4662018-08-01 10:46:22 -0600476 rasterInfo->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Greg Daniel164a9f02016-02-22 09:56:40 -0500477 rasterInfo->depthBiasEnable = VK_FALSE;
478 rasterInfo->depthBiasConstantFactor = 0.0f;
479 rasterInfo->depthBiasClamp = 0.0f;
480 rasterInfo->depthBiasSlopeFactor = 0.0f;
481 rasterInfo->lineWidth = 1.0f;
482}
483
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000484static void setup_dynamic_state(VkPipelineDynamicStateCreateInfo* dynamicInfo,
485 VkDynamicState* dynamicStates) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500486 memset(dynamicInfo, 0, sizeof(VkPipelineDynamicStateCreateInfo));
487 dynamicInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
egdaniel470d77a2016-03-18 12:50:27 -0700488 dynamicInfo->pNext = VK_NULL_HANDLE;
489 dynamicInfo->flags = 0;
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000490 dynamicStates[0] = VK_DYNAMIC_STATE_VIEWPORT;
491 dynamicStates[1] = VK_DYNAMIC_STATE_SCISSOR;
492 dynamicStates[2] = VK_DYNAMIC_STATE_BLEND_CONSTANTS;
493 dynamicInfo->dynamicStateCount = 3;
494 dynamicInfo->pDynamicStates = dynamicStates;
Greg Daniel164a9f02016-02-22 09:56:40 -0500495}
496
Brian Salomon49348902018-06-26 09:12:38 -0400497GrVkPipeline* GrVkPipeline::Create(GrVkGpu* gpu, const GrPrimitiveProcessor& primProc,
498 const GrPipeline& pipeline, const GrStencilSettings& stencil,
Greg Daniel164a9f02016-02-22 09:56:40 -0500499 VkPipelineShaderStageCreateInfo* shaderStageInfo,
Brian Salomon49348902018-06-26 09:12:38 -0400500 int shaderStageCount, GrPrimitiveType primitiveType,
501 const GrVkRenderPass& renderPass, VkPipelineLayout layout,
jvanverth03509ea2016-03-02 13:19:47 -0800502 VkPipelineCache cache) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500503 VkPipelineVertexInputStateCreateInfo vertexInputInfo;
Chris Dalton1d616352017-05-31 12:51:23 -0600504 SkSTArray<2, VkVertexInputBindingDescription, true> bindingDescs;
egdanielb05df0f2016-06-27 07:15:20 -0700505 SkSTArray<16, VkVertexInputAttributeDescription> attributeDesc;
Brian Salomon92be2f72018-06-19 14:33:47 -0400506 int totalAttributeCnt = primProc.numVertexAttributes() + primProc.numInstanceAttributes();
507 SkASSERT(totalAttributeCnt <= gpu->vkCaps().maxVertexAttributes());
508 VkVertexInputAttributeDescription* pAttribs = attributeDesc.push_back_n(totalAttributeCnt);
Chris Dalton1d616352017-05-31 12:51:23 -0600509 setup_vertex_input_state(primProc, &vertexInputInfo, &bindingDescs, pAttribs);
Greg Daniel164a9f02016-02-22 09:56:40 -0500510
511 VkPipelineInputAssemblyStateCreateInfo inputAssemblyInfo;
512 setup_input_assembly_state(primitiveType, &inputAssemblyInfo);
513
514 VkPipelineDepthStencilStateCreateInfo depthStencilInfo;
csmartdaltonc633abb2016-11-01 08:55:55 -0700515 setup_depth_stencil_state(stencil, &depthStencilInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500516
Greg Daniel164a9f02016-02-22 09:56:40 -0500517 VkPipelineViewportStateCreateInfo viewportInfo;
egdanielec440992016-09-13 09:54:11 -0700518 setup_viewport_scissor_state(&viewportInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500519
520 VkPipelineMultisampleStateCreateInfo multisampleInfo;
Brian Salomonff168d92018-06-23 15:17:27 -0400521 setup_multisample_state(primProc, pipeline, gpu->caps(), &multisampleInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500522
523 // We will only have one color attachment per pipeline.
524 VkPipelineColorBlendAttachmentState attachmentStates[1];
525 VkPipelineColorBlendStateCreateInfo colorBlendInfo;
egdanielec440992016-09-13 09:54:11 -0700526 setup_color_blend_state(pipeline, &colorBlendInfo, attachmentStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500527
528 VkPipelineRasterizationStateCreateInfo rasterInfo;
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400529 setup_raster_state(pipeline, gpu->caps(), &rasterInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500530
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000531 VkDynamicState dynamicStates[3];
Greg Daniel164a9f02016-02-22 09:56:40 -0500532 VkPipelineDynamicStateCreateInfo dynamicInfo;
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000533 setup_dynamic_state(&dynamicInfo, dynamicStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500534
535 VkGraphicsPipelineCreateInfo pipelineCreateInfo;
536 memset(&pipelineCreateInfo, 0, sizeof(VkGraphicsPipelineCreateInfo));
537 pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
538 pipelineCreateInfo.pNext = nullptr;
539 pipelineCreateInfo.flags = 0;
540 pipelineCreateInfo.stageCount = shaderStageCount;
541 pipelineCreateInfo.pStages = shaderStageInfo;
542 pipelineCreateInfo.pVertexInputState = &vertexInputInfo;
543 pipelineCreateInfo.pInputAssemblyState = &inputAssemblyInfo;
544 pipelineCreateInfo.pTessellationState = nullptr;
545 pipelineCreateInfo.pViewportState = &viewportInfo;
546 pipelineCreateInfo.pRasterizationState = &rasterInfo;
547 pipelineCreateInfo.pMultisampleState = &multisampleInfo;
548 pipelineCreateInfo.pDepthStencilState = &depthStencilInfo;
549 pipelineCreateInfo.pColorBlendState = &colorBlendInfo;
550 pipelineCreateInfo.pDynamicState = &dynamicInfo;
551 pipelineCreateInfo.layout = layout;
552 pipelineCreateInfo.renderPass = renderPass.vkRenderPass();
553 pipelineCreateInfo.subpass = 0;
554 pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
555 pipelineCreateInfo.basePipelineIndex = -1;
556
557 VkPipeline vkPipeline;
558 VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateGraphicsPipelines(gpu->device(),
halcanary9d524f22016-03-29 09:03:52 -0700559 cache, 1,
560 &pipelineCreateInfo,
Greg Daniel164a9f02016-02-22 09:56:40 -0500561 nullptr, &vkPipeline));
562 if (err) {
Greg Daniel5ba448c2018-02-26 13:30:47 -0500563 SkDebugf("Failed to create pipeline. Error: %d\n", err);
Greg Daniel164a9f02016-02-22 09:56:40 -0500564 return nullptr;
565 }
566
567 return new GrVkPipeline(vkPipeline);
568}
569
570void GrVkPipeline::freeGPUData(const GrVkGpu* gpu) const {
571 GR_VK_CALL(gpu->vkInterface(), DestroyPipeline(gpu->device(), fPipeline, nullptr));
572}
573
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000574void GrVkPipeline::SetDynamicScissorRectState(GrVkGpu* gpu,
575 GrVkCommandBuffer* cmdBuffer,
576 const GrRenderTarget* renderTarget,
577 GrSurfaceOrigin rtOrigin,
578 SkIRect scissorRect) {
579 if (!scissorRect.intersect(SkIRect::MakeWH(renderTarget->width(), renderTarget->height()))) {
580 scissorRect.setEmpty();
581 }
582
583 VkRect2D scissor;
584 scissor.offset.x = scissorRect.fLeft;
585 scissor.extent.width = scissorRect.width();
586 if (kTopLeft_GrSurfaceOrigin == rtOrigin) {
587 scissor.offset.y = scissorRect.fTop;
588 } else {
589 SkASSERT(kBottomLeft_GrSurfaceOrigin == rtOrigin);
590 scissor.offset.y = renderTarget->height() - scissorRect.fBottom;
591 }
592 scissor.extent.height = scissorRect.height();
593
594 SkASSERT(scissor.offset.x >= 0);
595 SkASSERT(scissor.offset.y >= 0);
596 cmdBuffer->setScissor(gpu, 0, 1, &scissor);
597}
598
Chris Dalton46983b72017-06-06 12:27:16 -0600599void GrVkPipeline::SetDynamicViewportState(GrVkGpu* gpu,
600 GrVkCommandBuffer* cmdBuffer,
601 const GrRenderTarget* renderTarget) {
egdaniel470d77a2016-03-18 12:50:27 -0700602 // We always use one viewport the size of the RT
603 VkViewport viewport;
604 viewport.x = 0.0f;
605 viewport.y = 0.0f;
Chris Dalton46983b72017-06-06 12:27:16 -0600606 viewport.width = SkIntToScalar(renderTarget->width());
607 viewport.height = SkIntToScalar(renderTarget->height());
egdaniel470d77a2016-03-18 12:50:27 -0700608 viewport.minDepth = 0.0f;
609 viewport.maxDepth = 1.0f;
610 cmdBuffer->setViewport(gpu, 0, 1, &viewport);
611}
612
Chris Dalton46983b72017-06-06 12:27:16 -0600613void GrVkPipeline::SetDynamicBlendConstantState(GrVkGpu* gpu,
614 GrVkCommandBuffer* cmdBuffer,
615 GrPixelConfig pixelConfig,
616 const GrXferProcessor& xferProcessor) {
egdaniel470d77a2016-03-18 12:50:27 -0700617 GrXferProcessor::BlendInfo blendInfo;
Chris Dalton46983b72017-06-06 12:27:16 -0600618 xferProcessor.getBlendInfo(&blendInfo);
egdaniel470d77a2016-03-18 12:50:27 -0700619 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
620 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
621 float floatColors[4];
622 if (blend_coeff_refs_constant(srcCoeff) || blend_coeff_refs_constant(dstCoeff)) {
Greg Daniel6c9f1012017-05-11 09:20:59 -0400623 // Swizzle the blend to match what the shader will output.
Chris Dalton46983b72017-06-06 12:27:16 -0600624 const GrSwizzle& swizzle = gpu->caps()->shaderCaps()->configOutputSwizzle(pixelConfig);
Greg Daniel6c9f1012017-05-11 09:20:59 -0400625 GrColor blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
626 GrColorToRGBAFloat(blendConst, floatColors);
egdaniel470d77a2016-03-18 12:50:27 -0700627 } else {
628 memset(floatColors, 0, 4 * sizeof(float));
629 }
630 cmdBuffer->setBlendConstants(gpu, floatColors);
631}