blob: 67005bec2021525233b41d76a0a1608ebf716a75 [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"
9
10#include "GrGeometryProcessor.h"
11#include "GrPipeline.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;
21 case kVec2f_GrVertexAttribType:
22 return VK_FORMAT_R32G32_SFLOAT;
23 case kVec3f_GrVertexAttribType:
24 return VK_FORMAT_R32G32B32_SFLOAT;
25 case kVec4f_GrVertexAttribType:
26 return VK_FORMAT_R32G32B32A32_SFLOAT;
27 case kVec2i_GrVertexAttribType:
28 return VK_FORMAT_R32G32_SINT;
29 case kVec3i_GrVertexAttribType:
30 return VK_FORMAT_R32G32B32_SINT;
31 case kVec4i_GrVertexAttribType:
32 return VK_FORMAT_R32G32B32A32_SINT;
33 case kUByte_GrVertexAttribType:
34 return VK_FORMAT_R8_UNORM;
35 case kVec4ub_GrVertexAttribType:
36 return VK_FORMAT_R8G8B8A8_UNORM;
37 case kVec2us_GrVertexAttribType:
38 return VK_FORMAT_R16G16_UNORM;
39 case kInt_GrVertexAttribType:
40 return VK_FORMAT_R32_SINT;
41 case kUint_GrVertexAttribType:
42 return VK_FORMAT_R32_UINT;
43 }
44 SkFAIL("Unknown vertex attrib type");
45 return VK_FORMAT_UNDEFINED;
Greg Daniel164a9f02016-02-22 09:56:40 -050046}
47
48static void setup_vertex_input_state(const GrPrimitiveProcessor& primProc,
Chris Dalton1d616352017-05-31 12:51:23 -060049 VkPipelineVertexInputStateCreateInfo* vertexInputInfo,
50 SkSTArray<2, VkVertexInputBindingDescription, true>* bindingDescs,
51 VkVertexInputAttributeDescription* attributeDesc) {
52 uint32_t vertexBinding, instanceBinding;
53
54 if (primProc.hasVertexAttribs()) {
55 vertexBinding = bindingDescs->count();
56 bindingDescs->push_back() = {
57 vertexBinding,
58 (uint32_t) primProc.getVertexStride(),
59 VK_VERTEX_INPUT_RATE_VERTEX
60 };
61 }
62
63 if (primProc.hasInstanceAttribs()) {
64 instanceBinding = bindingDescs->count();
65 bindingDescs->push_back() = {
66 instanceBinding,
67 (uint32_t) primProc.getInstanceStride(),
68 VK_VERTEX_INPUT_RATE_INSTANCE
69 };
70 }
Greg Daniel164a9f02016-02-22 09:56:40 -050071
72 // setup attribute descriptions
73 int vaCount = primProc.numAttribs();
Greg Daniel164a9f02016-02-22 09:56:40 -050074 if (vaCount > 0) {
Greg Daniel164a9f02016-02-22 09:56:40 -050075 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
Chris Dalton1d616352017-05-31 12:51:23 -060076 using InputRate = GrPrimitiveProcessor::Attribute::InputRate;
Greg Daniel164a9f02016-02-22 09:56:40 -050077 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
Greg Daniel164a9f02016-02-22 09:56:40 -050078 VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex];
79 vkAttrib.location = attribIndex; // for now assume location = attribIndex
Chris Dalton1d616352017-05-31 12:51:23 -060080 vkAttrib.binding = InputRate::kPerInstance == attrib.fInputRate ? instanceBinding
81 : vertexBinding;
82 vkAttrib.format = attrib_type_to_vkformat(attrib.fType);
83 vkAttrib.offset = attrib.fOffsetInRecord;
Greg Daniel164a9f02016-02-22 09:56:40 -050084 }
85 }
86
87 memset(vertexInputInfo, 0, sizeof(VkPipelineVertexInputStateCreateInfo));
88 vertexInputInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
89 vertexInputInfo->pNext = nullptr;
90 vertexInputInfo->flags = 0;
Chris Dalton1d616352017-05-31 12:51:23 -060091 vertexInputInfo->vertexBindingDescriptionCount = bindingDescs->count();
92 vertexInputInfo->pVertexBindingDescriptions = bindingDescs->begin();
Greg Daniel164a9f02016-02-22 09:56:40 -050093 vertexInputInfo->vertexAttributeDescriptionCount = vaCount;
94 vertexInputInfo->pVertexAttributeDescriptions = attributeDesc;
95}
96
Chris Dalton3809bab2017-06-13 10:55:06 -060097static VkPrimitiveTopology gr_primitive_type_to_vk_topology(GrPrimitiveType primitiveType) {
98 switch (primitiveType) {
99 case GrPrimitiveType::kTriangles:
100 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
101 case GrPrimitiveType::kTriangleStrip:
102 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
103 case GrPrimitiveType::kTriangleFan:
104 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN;
105 case GrPrimitiveType::kPoints:
106 return VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
107 case GrPrimitiveType::kLines:
108 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
109 case GrPrimitiveType::kLineStrip:
110 return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
111 case GrPrimitiveType::kLinesAdjacency:
112 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY;
113 }
114 SkFAIL("invalid GrPrimitiveType");
115 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
116}
Greg Daniel164a9f02016-02-22 09:56:40 -0500117
118static void setup_input_assembly_state(GrPrimitiveType primitiveType,
119 VkPipelineInputAssemblyStateCreateInfo* inputAssemblyInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500120 memset(inputAssemblyInfo, 0, sizeof(VkPipelineInputAssemblyStateCreateInfo));
121 inputAssemblyInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
122 inputAssemblyInfo->pNext = nullptr;
123 inputAssemblyInfo->flags = 0;
124 inputAssemblyInfo->primitiveRestartEnable = false;
Chris Dalton3809bab2017-06-13 10:55:06 -0600125 inputAssemblyInfo->topology = gr_primitive_type_to_vk_topology(primitiveType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500126}
127
128
egdanielec440992016-09-13 09:54:11 -0700129static VkStencilOp stencil_op_to_vk_stencil_op(GrStencilOp op) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500130 static const VkStencilOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700131 VK_STENCIL_OP_KEEP, // kKeep
132 VK_STENCIL_OP_ZERO, // kZero
133 VK_STENCIL_OP_REPLACE, // kReplace
134 VK_STENCIL_OP_INVERT, // kInvert
135 VK_STENCIL_OP_INCREMENT_AND_WRAP, // kIncWrap
136 VK_STENCIL_OP_DECREMENT_AND_WRAP, // kDecWrap
137 VK_STENCIL_OP_INCREMENT_AND_CLAMP, // kIncClamp
138 VK_STENCIL_OP_DECREMENT_AND_CLAMP, // kDecClamp
Greg Daniel164a9f02016-02-22 09:56:40 -0500139 };
cdalton93a379b2016-05-11 13:58:08 -0700140 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilOpCount);
141 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
142 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
143 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
144 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
145 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
146 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
147 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
148 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
149 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
150 return gTable[(int)op];
Greg Daniel164a9f02016-02-22 09:56:40 -0500151}
152
egdanielec440992016-09-13 09:54:11 -0700153static VkCompareOp stencil_func_to_vk_compare_op(GrStencilTest test) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500154 static const VkCompareOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700155 VK_COMPARE_OP_ALWAYS, // kAlways
156 VK_COMPARE_OP_NEVER, // kNever
157 VK_COMPARE_OP_GREATER, // kGreater
158 VK_COMPARE_OP_GREATER_OR_EQUAL, // kGEqual
159 VK_COMPARE_OP_LESS, // kLess
160 VK_COMPARE_OP_LESS_OR_EQUAL, // kLEqual
161 VK_COMPARE_OP_EQUAL, // kEqual
162 VK_COMPARE_OP_NOT_EQUAL, // kNotEqual
Greg Daniel164a9f02016-02-22 09:56:40 -0500163 };
cdalton93a379b2016-05-11 13:58:08 -0700164 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilTestCount);
165 GR_STATIC_ASSERT(0 == (int)GrStencilTest::kAlways);
166 GR_STATIC_ASSERT(1 == (int)GrStencilTest::kNever);
167 GR_STATIC_ASSERT(2 == (int)GrStencilTest::kGreater);
168 GR_STATIC_ASSERT(3 == (int)GrStencilTest::kGEqual);
169 GR_STATIC_ASSERT(4 == (int)GrStencilTest::kLess);
170 GR_STATIC_ASSERT(5 == (int)GrStencilTest::kLEqual);
171 GR_STATIC_ASSERT(6 == (int)GrStencilTest::kEqual);
172 GR_STATIC_ASSERT(7 == (int)GrStencilTest::kNotEqual);
173 SkASSERT(test < (GrStencilTest)kGrStencilTestCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500174
cdalton93a379b2016-05-11 13:58:08 -0700175 return gTable[(int)test];
Greg Daniel164a9f02016-02-22 09:56:40 -0500176}
177
egdanielec440992016-09-13 09:54:11 -0700178static void setup_depth_stencil_state(const GrStencilSettings& stencilSettings,
179 VkPipelineDepthStencilStateCreateInfo* stencilInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500180 memset(stencilInfo, 0, sizeof(VkPipelineDepthStencilStateCreateInfo));
181 stencilInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
182 stencilInfo->pNext = nullptr;
183 stencilInfo->flags = 0;
184 // set depth testing defaults
185 stencilInfo->depthTestEnable = VK_FALSE;
186 stencilInfo->depthWriteEnable = VK_FALSE;
187 stencilInfo->depthCompareOp = VK_COMPARE_OP_ALWAYS;
188 stencilInfo->depthBoundsTestEnable = VK_FALSE;
189 stencilInfo->stencilTestEnable = !stencilSettings.isDisabled();
190 if (!stencilSettings.isDisabled()) {
191 // Set front face
cdalton93a379b2016-05-11 13:58:08 -0700192 const GrStencilSettings::Face& front = stencilSettings.front();
193 stencilInfo->front.failOp = stencil_op_to_vk_stencil_op(front.fFailOp);
194 stencilInfo->front.passOp = stencil_op_to_vk_stencil_op(front.fPassOp);
Greg Daniel164a9f02016-02-22 09:56:40 -0500195 stencilInfo->front.depthFailOp = stencilInfo->front.failOp;
cdalton93a379b2016-05-11 13:58:08 -0700196 stencilInfo->front.compareOp = stencil_func_to_vk_compare_op(front.fTest);
197 stencilInfo->front.compareMask = front.fTestMask;
198 stencilInfo->front.writeMask = front.fWriteMask;
199 stencilInfo->front.reference = front.fRef;
Greg Daniel164a9f02016-02-22 09:56:40 -0500200
201 // Set back face
cdalton93a379b2016-05-11 13:58:08 -0700202 if (!stencilSettings.isTwoSided()) {
203 stencilInfo->back = stencilInfo->front;
204 } else {
205 const GrStencilSettings::Face& back = stencilSettings.back();
206 stencilInfo->back.failOp = stencil_op_to_vk_stencil_op(back.fFailOp);
207 stencilInfo->back.passOp = stencil_op_to_vk_stencil_op(back.fPassOp);
208 stencilInfo->back.depthFailOp = stencilInfo->front.failOp;
209 stencilInfo->back.compareOp = stencil_func_to_vk_compare_op(back.fTest);
210 stencilInfo->back.compareMask = back.fTestMask;
211 stencilInfo->back.writeMask = back.fWriteMask;
212 stencilInfo->back.reference = back.fRef;
213 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500214 }
215 stencilInfo->minDepthBounds = 0.0f;
216 stencilInfo->maxDepthBounds = 1.0f;
217}
218
egdanielec440992016-09-13 09:54:11 -0700219static void setup_viewport_scissor_state(VkPipelineViewportStateCreateInfo* viewportInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500220 memset(viewportInfo, 0, sizeof(VkPipelineViewportStateCreateInfo));
221 viewportInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
222 viewportInfo->pNext = nullptr;
223 viewportInfo->flags = 0;
224
Greg Daniel164a9f02016-02-22 09:56:40 -0500225 viewportInfo->viewportCount = 1;
egdaniel470d77a2016-03-18 12:50:27 -0700226 viewportInfo->pViewports = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500227
egdaniel470d77a2016-03-18 12:50:27 -0700228 viewportInfo->scissorCount = 1;
229 viewportInfo->pScissors = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500230
Greg Daniel164a9f02016-02-22 09:56:40 -0500231 SkASSERT(viewportInfo->viewportCount == viewportInfo->scissorCount);
232}
233
egdanielec440992016-09-13 09:54:11 -0700234static void setup_multisample_state(const GrPipeline& pipeline,
235 const GrPrimitiveProcessor& primProc,
236 const GrCaps* caps,
237 VkPipelineMultisampleStateCreateInfo* multisampleInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500238 memset(multisampleInfo, 0, sizeof(VkPipelineMultisampleStateCreateInfo));
239 multisampleInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
240 multisampleInfo->pNext = nullptr;
241 multisampleInfo->flags = 0;
Robert Phillips2890fbf2017-07-26 15:48:41 -0400242 int numSamples = pipeline.proxy()->numColorSamples();
Greg Daniel164a9f02016-02-22 09:56:40 -0500243 SkAssertResult(GrSampleCountToVkSampleCount(numSamples,
244 &multisampleInfo->rasterizationSamples));
ethannicholas28ef4452016-03-25 09:26:03 -0700245 float sampleShading = primProc.getSampleShading();
246 SkASSERT(sampleShading == 0.0f || caps->sampleShadingSupport());
247 multisampleInfo->sampleShadingEnable = sampleShading > 0.0f;
248 multisampleInfo->minSampleShading = sampleShading;
Greg Daniel164a9f02016-02-22 09:56:40 -0500249 multisampleInfo->pSampleMask = nullptr;
250 multisampleInfo->alphaToCoverageEnable = VK_FALSE;
251 multisampleInfo->alphaToOneEnable = VK_FALSE;
252}
253
254static VkBlendFactor blend_coeff_to_vk_blend(GrBlendCoeff coeff) {
255 static const VkBlendFactor gTable[] = {
256 VK_BLEND_FACTOR_ZERO, // kZero_GrBlendCoeff
257 VK_BLEND_FACTOR_ONE, // kOne_GrBlendCoeff
258 VK_BLEND_FACTOR_SRC_COLOR, // kSC_GrBlendCoeff
259 VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR, // kISC_GrBlendCoeff
260 VK_BLEND_FACTOR_DST_COLOR, // kDC_GrBlendCoeff
261 VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR, // kIDC_GrBlendCoeff
262 VK_BLEND_FACTOR_SRC_ALPHA, // kSA_GrBlendCoeff
263 VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, // kISA_GrBlendCoeff
264 VK_BLEND_FACTOR_DST_ALPHA, // kDA_GrBlendCoeff
265 VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA, // kIDA_GrBlendCoeff
266 VK_BLEND_FACTOR_CONSTANT_COLOR, // kConstC_GrBlendCoeff
267 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR, // kIConstC_GrBlendCoeff
268 VK_BLEND_FACTOR_CONSTANT_ALPHA, // kConstA_GrBlendCoeff
269 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA, // kIConstA_GrBlendCoeff
270 VK_BLEND_FACTOR_SRC1_COLOR, // kS2C_GrBlendCoeff
271 VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR, // kIS2C_GrBlendCoeff
272 VK_BLEND_FACTOR_SRC1_ALPHA, // kS2A_GrBlendCoeff
273 VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA, // kIS2A_GrBlendCoeff
274
275 };
276 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrBlendCoeffCnt);
277 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
278 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
279 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
280 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
281 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
282 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
283 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
284 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
285 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
286 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
287 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
288 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
289 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
290 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
291 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
292 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
293 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
294 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
295
296 SkASSERT((unsigned)coeff < kGrBlendCoeffCnt);
297 return gTable[coeff];
298}
299
300
301static VkBlendOp blend_equation_to_vk_blend_op(GrBlendEquation equation) {
302 static const VkBlendOp gTable[] = {
303 VK_BLEND_OP_ADD, // kAdd_GrBlendEquation
304 VK_BLEND_OP_SUBTRACT, // kSubtract_GrBlendEquation
305 VK_BLEND_OP_REVERSE_SUBTRACT, // kReverseSubtract_GrBlendEquation
306 };
307 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kFirstAdvancedGrBlendEquation);
308 GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
309 GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
310 GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
311
312 SkASSERT((unsigned)equation < kGrBlendCoeffCnt);
313 return gTable[equation];
314}
315
egdanielec440992016-09-13 09:54:11 -0700316static bool blend_coeff_refs_constant(GrBlendCoeff coeff) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500317 static const bool gCoeffReferencesBlendConst[] = {
318 false,
319 false,
320 false,
321 false,
322 false,
323 false,
324 false,
325 false,
326 false,
327 false,
328 true,
329 true,
330 true,
331 true,
332
333 // extended blend coeffs
334 false,
335 false,
336 false,
337 false,
338 };
339 return gCoeffReferencesBlendConst[coeff];
340 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
341 // Individual enum asserts already made in blend_coeff_to_vk_blend
342}
343
egdanielec440992016-09-13 09:54:11 -0700344static void setup_color_blend_state(const GrPipeline& pipeline,
345 VkPipelineColorBlendStateCreateInfo* colorBlendInfo,
346 VkPipelineColorBlendAttachmentState* attachmentState) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500347 GrXferProcessor::BlendInfo blendInfo;
348 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
349
350 GrBlendEquation equation = blendInfo.fEquation;
351 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
352 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
353 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
354 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
355
356 memset(attachmentState, 0, sizeof(VkPipelineColorBlendAttachmentState));
357 attachmentState->blendEnable = !blendOff;
358 if (!blendOff) {
359 attachmentState->srcColorBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
360 attachmentState->dstColorBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
361 attachmentState->colorBlendOp = blend_equation_to_vk_blend_op(equation);
362 attachmentState->srcAlphaBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
363 attachmentState->dstAlphaBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
364 attachmentState->alphaBlendOp = blend_equation_to_vk_blend_op(equation);
365 }
egdaniel3d5d9ac2016-03-01 12:56:15 -0800366
367 if (!blendInfo.fWriteColor) {
368 attachmentState->colorWriteMask = 0;
369 } else {
370 attachmentState->colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
371 VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
372 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500373
374 memset(colorBlendInfo, 0, sizeof(VkPipelineColorBlendStateCreateInfo));
375 colorBlendInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
376 colorBlendInfo->pNext = nullptr;
377 colorBlendInfo->flags = 0;
378 colorBlendInfo->logicOpEnable = VK_FALSE;
379 colorBlendInfo->attachmentCount = 1;
380 colorBlendInfo->pAttachments = attachmentState;
egdaniel470d77a2016-03-18 12:50:27 -0700381 // colorBlendInfo->blendConstants is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500382}
383
egdanielec440992016-09-13 09:54:11 -0700384static void setup_raster_state(const GrPipeline& pipeline,
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400385 const GrCaps* caps,
egdanielec440992016-09-13 09:54:11 -0700386 VkPipelineRasterizationStateCreateInfo* rasterInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500387 memset(rasterInfo, 0, sizeof(VkPipelineRasterizationStateCreateInfo));
388 rasterInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
389 rasterInfo->pNext = nullptr;
390 rasterInfo->flags = 0;
391 rasterInfo->depthClampEnable = VK_FALSE;
392 rasterInfo->rasterizerDiscardEnable = VK_FALSE;
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400393 rasterInfo->polygonMode = caps->wireframeMode() ? VK_POLYGON_MODE_LINE
394 : VK_POLYGON_MODE_FILL;
Brian Salomonf0861672017-05-08 11:10:10 -0400395 rasterInfo->cullMode = VK_CULL_MODE_NONE;
Greg Daniel164a9f02016-02-22 09:56:40 -0500396 rasterInfo->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
397 rasterInfo->depthBiasEnable = VK_FALSE;
398 rasterInfo->depthBiasConstantFactor = 0.0f;
399 rasterInfo->depthBiasClamp = 0.0f;
400 rasterInfo->depthBiasSlopeFactor = 0.0f;
401 rasterInfo->lineWidth = 1.0f;
402}
403
egdanielec440992016-09-13 09:54:11 -0700404static void setup_dynamic_state(VkPipelineDynamicStateCreateInfo* dynamicInfo,
405 VkDynamicState* dynamicStates) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500406 memset(dynamicInfo, 0, sizeof(VkPipelineDynamicStateCreateInfo));
407 dynamicInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
egdaniel470d77a2016-03-18 12:50:27 -0700408 dynamicInfo->pNext = VK_NULL_HANDLE;
409 dynamicInfo->flags = 0;
410 dynamicStates[0] = VK_DYNAMIC_STATE_VIEWPORT;
411 dynamicStates[1] = VK_DYNAMIC_STATE_SCISSOR;
412 dynamicStates[2] = VK_DYNAMIC_STATE_BLEND_CONSTANTS;
413 dynamicInfo->dynamicStateCount = 3;
414 dynamicInfo->pDynamicStates = dynamicStates;
Greg Daniel164a9f02016-02-22 09:56:40 -0500415}
416
417GrVkPipeline* GrVkPipeline::Create(GrVkGpu* gpu, const GrPipeline& pipeline,
csmartdaltonc633abb2016-11-01 08:55:55 -0700418 const GrStencilSettings& stencil,
Greg Daniel164a9f02016-02-22 09:56:40 -0500419 const GrPrimitiveProcessor& primProc,
420 VkPipelineShaderStageCreateInfo* shaderStageInfo,
421 int shaderStageCount,
422 GrPrimitiveType primitiveType,
423 const GrVkRenderPass& renderPass,
jvanverth03509ea2016-03-02 13:19:47 -0800424 VkPipelineLayout layout,
425 VkPipelineCache cache) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500426 VkPipelineVertexInputStateCreateInfo vertexInputInfo;
Chris Dalton1d616352017-05-31 12:51:23 -0600427 SkSTArray<2, VkVertexInputBindingDescription, true> bindingDescs;
egdanielb05df0f2016-06-27 07:15:20 -0700428 SkSTArray<16, VkVertexInputAttributeDescription> attributeDesc;
429 SkASSERT(primProc.numAttribs() <= gpu->vkCaps().maxVertexAttributes());
430 VkVertexInputAttributeDescription* pAttribs = attributeDesc.push_back_n(primProc.numAttribs());
Chris Dalton1d616352017-05-31 12:51:23 -0600431 setup_vertex_input_state(primProc, &vertexInputInfo, &bindingDescs, pAttribs);
Greg Daniel164a9f02016-02-22 09:56:40 -0500432
433 VkPipelineInputAssemblyStateCreateInfo inputAssemblyInfo;
434 setup_input_assembly_state(primitiveType, &inputAssemblyInfo);
435
436 VkPipelineDepthStencilStateCreateInfo depthStencilInfo;
csmartdaltonc633abb2016-11-01 08:55:55 -0700437 setup_depth_stencil_state(stencil, &depthStencilInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500438
Greg Daniel164a9f02016-02-22 09:56:40 -0500439 VkPipelineViewportStateCreateInfo viewportInfo;
egdanielec440992016-09-13 09:54:11 -0700440 setup_viewport_scissor_state(&viewportInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500441
442 VkPipelineMultisampleStateCreateInfo multisampleInfo;
ethannicholas28ef4452016-03-25 09:26:03 -0700443 setup_multisample_state(pipeline, primProc, gpu->caps(), &multisampleInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500444
445 // We will only have one color attachment per pipeline.
446 VkPipelineColorBlendAttachmentState attachmentStates[1];
447 VkPipelineColorBlendStateCreateInfo colorBlendInfo;
egdanielec440992016-09-13 09:54:11 -0700448 setup_color_blend_state(pipeline, &colorBlendInfo, attachmentStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500449
450 VkPipelineRasterizationStateCreateInfo rasterInfo;
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400451 setup_raster_state(pipeline, gpu->caps(), &rasterInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500452
egdaniel470d77a2016-03-18 12:50:27 -0700453 VkDynamicState dynamicStates[3];
Greg Daniel164a9f02016-02-22 09:56:40 -0500454 VkPipelineDynamicStateCreateInfo dynamicInfo;
egdanielec440992016-09-13 09:54:11 -0700455 setup_dynamic_state(&dynamicInfo, dynamicStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500456
457 VkGraphicsPipelineCreateInfo pipelineCreateInfo;
458 memset(&pipelineCreateInfo, 0, sizeof(VkGraphicsPipelineCreateInfo));
459 pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
460 pipelineCreateInfo.pNext = nullptr;
461 pipelineCreateInfo.flags = 0;
462 pipelineCreateInfo.stageCount = shaderStageCount;
463 pipelineCreateInfo.pStages = shaderStageInfo;
464 pipelineCreateInfo.pVertexInputState = &vertexInputInfo;
465 pipelineCreateInfo.pInputAssemblyState = &inputAssemblyInfo;
466 pipelineCreateInfo.pTessellationState = nullptr;
467 pipelineCreateInfo.pViewportState = &viewportInfo;
468 pipelineCreateInfo.pRasterizationState = &rasterInfo;
469 pipelineCreateInfo.pMultisampleState = &multisampleInfo;
470 pipelineCreateInfo.pDepthStencilState = &depthStencilInfo;
471 pipelineCreateInfo.pColorBlendState = &colorBlendInfo;
472 pipelineCreateInfo.pDynamicState = &dynamicInfo;
473 pipelineCreateInfo.layout = layout;
474 pipelineCreateInfo.renderPass = renderPass.vkRenderPass();
475 pipelineCreateInfo.subpass = 0;
476 pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
477 pipelineCreateInfo.basePipelineIndex = -1;
478
479 VkPipeline vkPipeline;
480 VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateGraphicsPipelines(gpu->device(),
halcanary9d524f22016-03-29 09:03:52 -0700481 cache, 1,
482 &pipelineCreateInfo,
Greg Daniel164a9f02016-02-22 09:56:40 -0500483 nullptr, &vkPipeline));
484 if (err) {
485 return nullptr;
486 }
487
488 return new GrVkPipeline(vkPipeline);
489}
490
491void GrVkPipeline::freeGPUData(const GrVkGpu* gpu) const {
492 GR_VK_CALL(gpu->vkInterface(), DestroyPipeline(gpu->device(), fPipeline, nullptr));
493}
494
Chris Dalton46983b72017-06-06 12:27:16 -0600495void GrVkPipeline::SetDynamicScissorRectState(GrVkGpu* gpu,
496 GrVkCommandBuffer* cmdBuffer,
497 const GrRenderTarget* renderTarget,
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400498 GrSurfaceOrigin rtOrigin,
Chris Dalton46983b72017-06-06 12:27:16 -0600499 SkIRect scissorRect) {
500 if (!scissorRect.intersect(SkIRect::MakeWH(renderTarget->width(), renderTarget->height()))) {
501 scissorRect.setEmpty();
egdaniel470d77a2016-03-18 12:50:27 -0700502 }
Chris Dalton46983b72017-06-06 12:27:16 -0600503
504 VkRect2D scissor;
505 scissor.offset.x = scissorRect.fLeft;
506 scissor.extent.width = scissorRect.width();
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400507 if (kTopLeft_GrSurfaceOrigin == rtOrigin) {
Chris Dalton46983b72017-06-06 12:27:16 -0600508 scissor.offset.y = scissorRect.fTop;
509 } else {
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400510 SkASSERT(kBottomLeft_GrSurfaceOrigin == rtOrigin);
Chris Dalton46983b72017-06-06 12:27:16 -0600511 scissor.offset.y = renderTarget->height() - scissorRect.fBottom;
512 }
513 scissor.extent.height = scissorRect.height();
514
515 SkASSERT(scissor.offset.x >= 0);
516 SkASSERT(scissor.offset.y >= 0);
egdaniel470d77a2016-03-18 12:50:27 -0700517 cmdBuffer->setScissor(gpu, 0, 1, &scissor);
518}
519
Chris Dalton46983b72017-06-06 12:27:16 -0600520void GrVkPipeline::SetDynamicViewportState(GrVkGpu* gpu,
521 GrVkCommandBuffer* cmdBuffer,
522 const GrRenderTarget* renderTarget) {
egdaniel470d77a2016-03-18 12:50:27 -0700523 // We always use one viewport the size of the RT
524 VkViewport viewport;
525 viewport.x = 0.0f;
526 viewport.y = 0.0f;
Chris Dalton46983b72017-06-06 12:27:16 -0600527 viewport.width = SkIntToScalar(renderTarget->width());
528 viewport.height = SkIntToScalar(renderTarget->height());
egdaniel470d77a2016-03-18 12:50:27 -0700529 viewport.minDepth = 0.0f;
530 viewport.maxDepth = 1.0f;
531 cmdBuffer->setViewport(gpu, 0, 1, &viewport);
532}
533
Chris Dalton46983b72017-06-06 12:27:16 -0600534void GrVkPipeline::SetDynamicBlendConstantState(GrVkGpu* gpu,
535 GrVkCommandBuffer* cmdBuffer,
536 GrPixelConfig pixelConfig,
537 const GrXferProcessor& xferProcessor) {
egdaniel470d77a2016-03-18 12:50:27 -0700538 GrXferProcessor::BlendInfo blendInfo;
Chris Dalton46983b72017-06-06 12:27:16 -0600539 xferProcessor.getBlendInfo(&blendInfo);
egdaniel470d77a2016-03-18 12:50:27 -0700540 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)) {
Greg Daniel6c9f1012017-05-11 09:20:59 -0400544 // Swizzle the blend to match what the shader will output.
Chris Dalton46983b72017-06-06 12:27:16 -0600545 const GrSwizzle& swizzle = gpu->caps()->shaderCaps()->configOutputSwizzle(pixelConfig);
Greg Daniel6c9f1012017-05-11 09:20:59 -0400546 GrColor blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
547 GrColorToRGBAFloat(blendConst, floatColors);
egdaniel470d77a2016-03-18 12:50:27 -0700548 } else {
549 memset(floatColors, 0, 4 * sizeof(float));
550 }
551 cmdBuffer->setBlendConstants(gpu, floatColors);
552}