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