blob: 0b4289edec8b2653dbe500433c22161bacd39a1f [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
97
98static void setup_input_assembly_state(GrPrimitiveType primitiveType,
99 VkPipelineInputAssemblyStateCreateInfo* inputAssemblyInfo) {
Ravi Mistry7cd974f2017-06-13 14:45:23 +0000100 static const VkPrimitiveTopology gPrimitiveType2VkTopology[] = {
101 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
102 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
103 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN,
104 VK_PRIMITIVE_TOPOLOGY_POINT_LIST,
105 VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
106 VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
107 };
108
Greg Daniel164a9f02016-02-22 09:56:40 -0500109 memset(inputAssemblyInfo, 0, sizeof(VkPipelineInputAssemblyStateCreateInfo));
110 inputAssemblyInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
111 inputAssemblyInfo->pNext = nullptr;
112 inputAssemblyInfo->flags = 0;
113 inputAssemblyInfo->primitiveRestartEnable = false;
Ravi Mistry7cd974f2017-06-13 14:45:23 +0000114 inputAssemblyInfo->topology = gPrimitiveType2VkTopology[primitiveType];
Greg Daniel164a9f02016-02-22 09:56:40 -0500115}
116
117
egdanielec440992016-09-13 09:54:11 -0700118static VkStencilOp stencil_op_to_vk_stencil_op(GrStencilOp op) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500119 static const VkStencilOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700120 VK_STENCIL_OP_KEEP, // kKeep
121 VK_STENCIL_OP_ZERO, // kZero
122 VK_STENCIL_OP_REPLACE, // kReplace
123 VK_STENCIL_OP_INVERT, // kInvert
124 VK_STENCIL_OP_INCREMENT_AND_WRAP, // kIncWrap
125 VK_STENCIL_OP_DECREMENT_AND_WRAP, // kDecWrap
126 VK_STENCIL_OP_INCREMENT_AND_CLAMP, // kIncClamp
127 VK_STENCIL_OP_DECREMENT_AND_CLAMP, // kDecClamp
Greg Daniel164a9f02016-02-22 09:56:40 -0500128 };
cdalton93a379b2016-05-11 13:58:08 -0700129 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilOpCount);
130 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
131 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
132 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
133 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
134 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
135 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
136 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
137 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
138 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
139 return gTable[(int)op];
Greg Daniel164a9f02016-02-22 09:56:40 -0500140}
141
egdanielec440992016-09-13 09:54:11 -0700142static VkCompareOp stencil_func_to_vk_compare_op(GrStencilTest test) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500143 static const VkCompareOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700144 VK_COMPARE_OP_ALWAYS, // kAlways
145 VK_COMPARE_OP_NEVER, // kNever
146 VK_COMPARE_OP_GREATER, // kGreater
147 VK_COMPARE_OP_GREATER_OR_EQUAL, // kGEqual
148 VK_COMPARE_OP_LESS, // kLess
149 VK_COMPARE_OP_LESS_OR_EQUAL, // kLEqual
150 VK_COMPARE_OP_EQUAL, // kEqual
151 VK_COMPARE_OP_NOT_EQUAL, // kNotEqual
Greg Daniel164a9f02016-02-22 09:56:40 -0500152 };
cdalton93a379b2016-05-11 13:58:08 -0700153 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilTestCount);
154 GR_STATIC_ASSERT(0 == (int)GrStencilTest::kAlways);
155 GR_STATIC_ASSERT(1 == (int)GrStencilTest::kNever);
156 GR_STATIC_ASSERT(2 == (int)GrStencilTest::kGreater);
157 GR_STATIC_ASSERT(3 == (int)GrStencilTest::kGEqual);
158 GR_STATIC_ASSERT(4 == (int)GrStencilTest::kLess);
159 GR_STATIC_ASSERT(5 == (int)GrStencilTest::kLEqual);
160 GR_STATIC_ASSERT(6 == (int)GrStencilTest::kEqual);
161 GR_STATIC_ASSERT(7 == (int)GrStencilTest::kNotEqual);
162 SkASSERT(test < (GrStencilTest)kGrStencilTestCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500163
cdalton93a379b2016-05-11 13:58:08 -0700164 return gTable[(int)test];
Greg Daniel164a9f02016-02-22 09:56:40 -0500165}
166
egdanielec440992016-09-13 09:54:11 -0700167static void setup_depth_stencil_state(const GrStencilSettings& stencilSettings,
168 VkPipelineDepthStencilStateCreateInfo* stencilInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500169 memset(stencilInfo, 0, sizeof(VkPipelineDepthStencilStateCreateInfo));
170 stencilInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
171 stencilInfo->pNext = nullptr;
172 stencilInfo->flags = 0;
173 // set depth testing defaults
174 stencilInfo->depthTestEnable = VK_FALSE;
175 stencilInfo->depthWriteEnable = VK_FALSE;
176 stencilInfo->depthCompareOp = VK_COMPARE_OP_ALWAYS;
177 stencilInfo->depthBoundsTestEnable = VK_FALSE;
178 stencilInfo->stencilTestEnable = !stencilSettings.isDisabled();
179 if (!stencilSettings.isDisabled()) {
180 // Set front face
cdalton93a379b2016-05-11 13:58:08 -0700181 const GrStencilSettings::Face& front = stencilSettings.front();
182 stencilInfo->front.failOp = stencil_op_to_vk_stencil_op(front.fFailOp);
183 stencilInfo->front.passOp = stencil_op_to_vk_stencil_op(front.fPassOp);
Greg Daniel164a9f02016-02-22 09:56:40 -0500184 stencilInfo->front.depthFailOp = stencilInfo->front.failOp;
cdalton93a379b2016-05-11 13:58:08 -0700185 stencilInfo->front.compareOp = stencil_func_to_vk_compare_op(front.fTest);
186 stencilInfo->front.compareMask = front.fTestMask;
187 stencilInfo->front.writeMask = front.fWriteMask;
188 stencilInfo->front.reference = front.fRef;
Greg Daniel164a9f02016-02-22 09:56:40 -0500189
190 // Set back face
cdalton93a379b2016-05-11 13:58:08 -0700191 if (!stencilSettings.isTwoSided()) {
192 stencilInfo->back = stencilInfo->front;
193 } else {
194 const GrStencilSettings::Face& back = stencilSettings.back();
195 stencilInfo->back.failOp = stencil_op_to_vk_stencil_op(back.fFailOp);
196 stencilInfo->back.passOp = stencil_op_to_vk_stencil_op(back.fPassOp);
197 stencilInfo->back.depthFailOp = stencilInfo->front.failOp;
198 stencilInfo->back.compareOp = stencil_func_to_vk_compare_op(back.fTest);
199 stencilInfo->back.compareMask = back.fTestMask;
200 stencilInfo->back.writeMask = back.fWriteMask;
201 stencilInfo->back.reference = back.fRef;
202 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500203 }
204 stencilInfo->minDepthBounds = 0.0f;
205 stencilInfo->maxDepthBounds = 1.0f;
206}
207
egdanielec440992016-09-13 09:54:11 -0700208static void setup_viewport_scissor_state(VkPipelineViewportStateCreateInfo* viewportInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500209 memset(viewportInfo, 0, sizeof(VkPipelineViewportStateCreateInfo));
210 viewportInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
211 viewportInfo->pNext = nullptr;
212 viewportInfo->flags = 0;
213
Greg Daniel164a9f02016-02-22 09:56:40 -0500214 viewportInfo->viewportCount = 1;
egdaniel470d77a2016-03-18 12:50:27 -0700215 viewportInfo->pViewports = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500216
egdaniel470d77a2016-03-18 12:50:27 -0700217 viewportInfo->scissorCount = 1;
218 viewportInfo->pScissors = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500219
Greg Daniel164a9f02016-02-22 09:56:40 -0500220 SkASSERT(viewportInfo->viewportCount == viewportInfo->scissorCount);
221}
222
egdanielec440992016-09-13 09:54:11 -0700223static void setup_multisample_state(const GrPipeline& pipeline,
224 const GrPrimitiveProcessor& primProc,
225 const GrCaps* caps,
226 VkPipelineMultisampleStateCreateInfo* multisampleInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500227 memset(multisampleInfo, 0, sizeof(VkPipelineMultisampleStateCreateInfo));
228 multisampleInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
229 multisampleInfo->pNext = nullptr;
230 multisampleInfo->flags = 0;
231 int numSamples = pipeline.getRenderTarget()->numColorSamples();
232 SkAssertResult(GrSampleCountToVkSampleCount(numSamples,
233 &multisampleInfo->rasterizationSamples));
ethannicholas28ef4452016-03-25 09:26:03 -0700234 float sampleShading = primProc.getSampleShading();
235 SkASSERT(sampleShading == 0.0f || caps->sampleShadingSupport());
236 multisampleInfo->sampleShadingEnable = sampleShading > 0.0f;
237 multisampleInfo->minSampleShading = sampleShading;
Greg Daniel164a9f02016-02-22 09:56:40 -0500238 multisampleInfo->pSampleMask = nullptr;
239 multisampleInfo->alphaToCoverageEnable = VK_FALSE;
240 multisampleInfo->alphaToOneEnable = VK_FALSE;
241}
242
243static VkBlendFactor blend_coeff_to_vk_blend(GrBlendCoeff coeff) {
244 static const VkBlendFactor gTable[] = {
245 VK_BLEND_FACTOR_ZERO, // kZero_GrBlendCoeff
246 VK_BLEND_FACTOR_ONE, // kOne_GrBlendCoeff
247 VK_BLEND_FACTOR_SRC_COLOR, // kSC_GrBlendCoeff
248 VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR, // kISC_GrBlendCoeff
249 VK_BLEND_FACTOR_DST_COLOR, // kDC_GrBlendCoeff
250 VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR, // kIDC_GrBlendCoeff
251 VK_BLEND_FACTOR_SRC_ALPHA, // kSA_GrBlendCoeff
252 VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, // kISA_GrBlendCoeff
253 VK_BLEND_FACTOR_DST_ALPHA, // kDA_GrBlendCoeff
254 VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA, // kIDA_GrBlendCoeff
255 VK_BLEND_FACTOR_CONSTANT_COLOR, // kConstC_GrBlendCoeff
256 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR, // kIConstC_GrBlendCoeff
257 VK_BLEND_FACTOR_CONSTANT_ALPHA, // kConstA_GrBlendCoeff
258 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA, // kIConstA_GrBlendCoeff
259 VK_BLEND_FACTOR_SRC1_COLOR, // kS2C_GrBlendCoeff
260 VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR, // kIS2C_GrBlendCoeff
261 VK_BLEND_FACTOR_SRC1_ALPHA, // kS2A_GrBlendCoeff
262 VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA, // kIS2A_GrBlendCoeff
263
264 };
265 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrBlendCoeffCnt);
266 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
267 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
268 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
269 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
270 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
271 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
272 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
273 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
274 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
275 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
276 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
277 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
278 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
279 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
280 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
281 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
282 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
283 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
284
285 SkASSERT((unsigned)coeff < kGrBlendCoeffCnt);
286 return gTable[coeff];
287}
288
289
290static VkBlendOp blend_equation_to_vk_blend_op(GrBlendEquation equation) {
291 static const VkBlendOp gTable[] = {
292 VK_BLEND_OP_ADD, // kAdd_GrBlendEquation
293 VK_BLEND_OP_SUBTRACT, // kSubtract_GrBlendEquation
294 VK_BLEND_OP_REVERSE_SUBTRACT, // kReverseSubtract_GrBlendEquation
295 };
296 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kFirstAdvancedGrBlendEquation);
297 GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
298 GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
299 GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
300
301 SkASSERT((unsigned)equation < kGrBlendCoeffCnt);
302 return gTable[equation];
303}
304
egdanielec440992016-09-13 09:54:11 -0700305static bool blend_coeff_refs_constant(GrBlendCoeff coeff) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500306 static const bool gCoeffReferencesBlendConst[] = {
307 false,
308 false,
309 false,
310 false,
311 false,
312 false,
313 false,
314 false,
315 false,
316 false,
317 true,
318 true,
319 true,
320 true,
321
322 // extended blend coeffs
323 false,
324 false,
325 false,
326 false,
327 };
328 return gCoeffReferencesBlendConst[coeff];
329 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
330 // Individual enum asserts already made in blend_coeff_to_vk_blend
331}
332
egdanielec440992016-09-13 09:54:11 -0700333static void setup_color_blend_state(const GrPipeline& pipeline,
334 VkPipelineColorBlendStateCreateInfo* colorBlendInfo,
335 VkPipelineColorBlendAttachmentState* attachmentState) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500336 GrXferProcessor::BlendInfo blendInfo;
337 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
338
339 GrBlendEquation equation = blendInfo.fEquation;
340 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
341 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
342 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
343 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
344
345 memset(attachmentState, 0, sizeof(VkPipelineColorBlendAttachmentState));
346 attachmentState->blendEnable = !blendOff;
347 if (!blendOff) {
348 attachmentState->srcColorBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
349 attachmentState->dstColorBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
350 attachmentState->colorBlendOp = blend_equation_to_vk_blend_op(equation);
351 attachmentState->srcAlphaBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
352 attachmentState->dstAlphaBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
353 attachmentState->alphaBlendOp = blend_equation_to_vk_blend_op(equation);
354 }
egdaniel3d5d9ac2016-03-01 12:56:15 -0800355
356 if (!blendInfo.fWriteColor) {
357 attachmentState->colorWriteMask = 0;
358 } else {
359 attachmentState->colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
360 VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
361 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500362
363 memset(colorBlendInfo, 0, sizeof(VkPipelineColorBlendStateCreateInfo));
364 colorBlendInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
365 colorBlendInfo->pNext = nullptr;
366 colorBlendInfo->flags = 0;
367 colorBlendInfo->logicOpEnable = VK_FALSE;
368 colorBlendInfo->attachmentCount = 1;
369 colorBlendInfo->pAttachments = attachmentState;
egdaniel470d77a2016-03-18 12:50:27 -0700370 // colorBlendInfo->blendConstants is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500371}
372
egdanielec440992016-09-13 09:54:11 -0700373static void setup_raster_state(const GrPipeline& pipeline,
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400374 const GrCaps* caps,
egdanielec440992016-09-13 09:54:11 -0700375 VkPipelineRasterizationStateCreateInfo* rasterInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500376 memset(rasterInfo, 0, sizeof(VkPipelineRasterizationStateCreateInfo));
377 rasterInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
378 rasterInfo->pNext = nullptr;
379 rasterInfo->flags = 0;
380 rasterInfo->depthClampEnable = VK_FALSE;
381 rasterInfo->rasterizerDiscardEnable = VK_FALSE;
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400382 rasterInfo->polygonMode = caps->wireframeMode() ? VK_POLYGON_MODE_LINE
383 : VK_POLYGON_MODE_FILL;
Brian Salomonf0861672017-05-08 11:10:10 -0400384 rasterInfo->cullMode = VK_CULL_MODE_NONE;
Greg Daniel164a9f02016-02-22 09:56:40 -0500385 rasterInfo->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
386 rasterInfo->depthBiasEnable = VK_FALSE;
387 rasterInfo->depthBiasConstantFactor = 0.0f;
388 rasterInfo->depthBiasClamp = 0.0f;
389 rasterInfo->depthBiasSlopeFactor = 0.0f;
390 rasterInfo->lineWidth = 1.0f;
391}
392
egdanielec440992016-09-13 09:54:11 -0700393static void setup_dynamic_state(VkPipelineDynamicStateCreateInfo* dynamicInfo,
394 VkDynamicState* dynamicStates) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500395 memset(dynamicInfo, 0, sizeof(VkPipelineDynamicStateCreateInfo));
396 dynamicInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
egdaniel470d77a2016-03-18 12:50:27 -0700397 dynamicInfo->pNext = VK_NULL_HANDLE;
398 dynamicInfo->flags = 0;
399 dynamicStates[0] = VK_DYNAMIC_STATE_VIEWPORT;
400 dynamicStates[1] = VK_DYNAMIC_STATE_SCISSOR;
401 dynamicStates[2] = VK_DYNAMIC_STATE_BLEND_CONSTANTS;
402 dynamicInfo->dynamicStateCount = 3;
403 dynamicInfo->pDynamicStates = dynamicStates;
Greg Daniel164a9f02016-02-22 09:56:40 -0500404}
405
406GrVkPipeline* GrVkPipeline::Create(GrVkGpu* gpu, const GrPipeline& pipeline,
csmartdaltonc633abb2016-11-01 08:55:55 -0700407 const GrStencilSettings& stencil,
Greg Daniel164a9f02016-02-22 09:56:40 -0500408 const GrPrimitiveProcessor& primProc,
409 VkPipelineShaderStageCreateInfo* shaderStageInfo,
410 int shaderStageCount,
411 GrPrimitiveType primitiveType,
412 const GrVkRenderPass& renderPass,
jvanverth03509ea2016-03-02 13:19:47 -0800413 VkPipelineLayout layout,
414 VkPipelineCache cache) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500415 VkPipelineVertexInputStateCreateInfo vertexInputInfo;
Chris Dalton1d616352017-05-31 12:51:23 -0600416 SkSTArray<2, VkVertexInputBindingDescription, true> bindingDescs;
egdanielb05df0f2016-06-27 07:15:20 -0700417 SkSTArray<16, VkVertexInputAttributeDescription> attributeDesc;
418 SkASSERT(primProc.numAttribs() <= gpu->vkCaps().maxVertexAttributes());
419 VkVertexInputAttributeDescription* pAttribs = attributeDesc.push_back_n(primProc.numAttribs());
Chris Dalton1d616352017-05-31 12:51:23 -0600420 setup_vertex_input_state(primProc, &vertexInputInfo, &bindingDescs, pAttribs);
Greg Daniel164a9f02016-02-22 09:56:40 -0500421
422 VkPipelineInputAssemblyStateCreateInfo inputAssemblyInfo;
423 setup_input_assembly_state(primitiveType, &inputAssemblyInfo);
424
425 VkPipelineDepthStencilStateCreateInfo depthStencilInfo;
csmartdaltonc633abb2016-11-01 08:55:55 -0700426 setup_depth_stencil_state(stencil, &depthStencilInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500427
Greg Daniel164a9f02016-02-22 09:56:40 -0500428 VkPipelineViewportStateCreateInfo viewportInfo;
egdanielec440992016-09-13 09:54:11 -0700429 setup_viewport_scissor_state(&viewportInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500430
431 VkPipelineMultisampleStateCreateInfo multisampleInfo;
ethannicholas28ef4452016-03-25 09:26:03 -0700432 setup_multisample_state(pipeline, primProc, gpu->caps(), &multisampleInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500433
434 // We will only have one color attachment per pipeline.
435 VkPipelineColorBlendAttachmentState attachmentStates[1];
436 VkPipelineColorBlendStateCreateInfo colorBlendInfo;
egdanielec440992016-09-13 09:54:11 -0700437 setup_color_blend_state(pipeline, &colorBlendInfo, attachmentStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500438
439 VkPipelineRasterizationStateCreateInfo rasterInfo;
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400440 setup_raster_state(pipeline, gpu->caps(), &rasterInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500441
egdaniel470d77a2016-03-18 12:50:27 -0700442 VkDynamicState dynamicStates[3];
Greg Daniel164a9f02016-02-22 09:56:40 -0500443 VkPipelineDynamicStateCreateInfo dynamicInfo;
egdanielec440992016-09-13 09:54:11 -0700444 setup_dynamic_state(&dynamicInfo, dynamicStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500445
446 VkGraphicsPipelineCreateInfo pipelineCreateInfo;
447 memset(&pipelineCreateInfo, 0, sizeof(VkGraphicsPipelineCreateInfo));
448 pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
449 pipelineCreateInfo.pNext = nullptr;
450 pipelineCreateInfo.flags = 0;
451 pipelineCreateInfo.stageCount = shaderStageCount;
452 pipelineCreateInfo.pStages = shaderStageInfo;
453 pipelineCreateInfo.pVertexInputState = &vertexInputInfo;
454 pipelineCreateInfo.pInputAssemblyState = &inputAssemblyInfo;
455 pipelineCreateInfo.pTessellationState = nullptr;
456 pipelineCreateInfo.pViewportState = &viewportInfo;
457 pipelineCreateInfo.pRasterizationState = &rasterInfo;
458 pipelineCreateInfo.pMultisampleState = &multisampleInfo;
459 pipelineCreateInfo.pDepthStencilState = &depthStencilInfo;
460 pipelineCreateInfo.pColorBlendState = &colorBlendInfo;
461 pipelineCreateInfo.pDynamicState = &dynamicInfo;
462 pipelineCreateInfo.layout = layout;
463 pipelineCreateInfo.renderPass = renderPass.vkRenderPass();
464 pipelineCreateInfo.subpass = 0;
465 pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
466 pipelineCreateInfo.basePipelineIndex = -1;
467
468 VkPipeline vkPipeline;
469 VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateGraphicsPipelines(gpu->device(),
halcanary9d524f22016-03-29 09:03:52 -0700470 cache, 1,
471 &pipelineCreateInfo,
Greg Daniel164a9f02016-02-22 09:56:40 -0500472 nullptr, &vkPipeline));
473 if (err) {
474 return nullptr;
475 }
476
477 return new GrVkPipeline(vkPipeline);
478}
479
480void GrVkPipeline::freeGPUData(const GrVkGpu* gpu) const {
481 GR_VK_CALL(gpu->vkInterface(), DestroyPipeline(gpu->device(), fPipeline, nullptr));
482}
483
Chris Dalton46983b72017-06-06 12:27:16 -0600484void GrVkPipeline::SetDynamicScissorRectState(GrVkGpu* gpu,
485 GrVkCommandBuffer* cmdBuffer,
486 const GrRenderTarget* renderTarget,
487 SkIRect scissorRect) {
488 if (!scissorRect.intersect(SkIRect::MakeWH(renderTarget->width(), renderTarget->height()))) {
489 scissorRect.setEmpty();
egdaniel470d77a2016-03-18 12:50:27 -0700490 }
Chris Dalton46983b72017-06-06 12:27:16 -0600491
492 VkRect2D scissor;
493 scissor.offset.x = scissorRect.fLeft;
494 scissor.extent.width = scissorRect.width();
495 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
496 scissor.offset.y = scissorRect.fTop;
497 } else {
498 SkASSERT(kBottomLeft_GrSurfaceOrigin == renderTarget->origin());
499 scissor.offset.y = renderTarget->height() - scissorRect.fBottom;
500 }
501 scissor.extent.height = scissorRect.height();
502
503 SkASSERT(scissor.offset.x >= 0);
504 SkASSERT(scissor.offset.y >= 0);
egdaniel470d77a2016-03-18 12:50:27 -0700505 cmdBuffer->setScissor(gpu, 0, 1, &scissor);
506}
507
Chris Dalton46983b72017-06-06 12:27:16 -0600508void GrVkPipeline::SetDynamicViewportState(GrVkGpu* gpu,
509 GrVkCommandBuffer* cmdBuffer,
510 const GrRenderTarget* renderTarget) {
egdaniel470d77a2016-03-18 12:50:27 -0700511 // We always use one viewport the size of the RT
512 VkViewport viewport;
513 viewport.x = 0.0f;
514 viewport.y = 0.0f;
Chris Dalton46983b72017-06-06 12:27:16 -0600515 viewport.width = SkIntToScalar(renderTarget->width());
516 viewport.height = SkIntToScalar(renderTarget->height());
egdaniel470d77a2016-03-18 12:50:27 -0700517 viewport.minDepth = 0.0f;
518 viewport.maxDepth = 1.0f;
519 cmdBuffer->setViewport(gpu, 0, 1, &viewport);
520}
521
Chris Dalton46983b72017-06-06 12:27:16 -0600522void GrVkPipeline::SetDynamicBlendConstantState(GrVkGpu* gpu,
523 GrVkCommandBuffer* cmdBuffer,
524 GrPixelConfig pixelConfig,
525 const GrXferProcessor& xferProcessor) {
egdaniel470d77a2016-03-18 12:50:27 -0700526 GrXferProcessor::BlendInfo blendInfo;
Chris Dalton46983b72017-06-06 12:27:16 -0600527 xferProcessor.getBlendInfo(&blendInfo);
egdaniel470d77a2016-03-18 12:50:27 -0700528 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
529 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
530 float floatColors[4];
531 if (blend_coeff_refs_constant(srcCoeff) || blend_coeff_refs_constant(dstCoeff)) {
Greg Daniel6c9f1012017-05-11 09:20:59 -0400532 // Swizzle the blend to match what the shader will output.
Chris Dalton46983b72017-06-06 12:27:16 -0600533 const GrSwizzle& swizzle = gpu->caps()->shaderCaps()->configOutputSwizzle(pixelConfig);
Greg Daniel6c9f1012017-05-11 09:20:59 -0400534 GrColor blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
535 GrColorToRGBAFloat(blendConst, floatColors);
egdaniel470d77a2016-03-18 12:50:27 -0700536 } else {
537 memset(floatColors, 0, 4 * sizeof(float));
538 }
539 cmdBuffer->setBlendConstants(gpu, floatColors);
540}