blob: 0ba111af0990c522ddafc20e10a3c326125f7cea [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,
egdanielb05df0f2016-06-27 07:15:20 -070044 VkVertexInputAttributeDescription* attributeDesc) {
Greg Daniel164a9f02016-02-22 09:56:40 -050045 // for now we have only one vertex buffer and one binding
46 memset(bindingDesc, 0, sizeof(VkVertexInputBindingDescription));
47 bindingDesc->binding = 0;
48 bindingDesc->stride = (uint32_t)primProc.getVertexStride();
49 bindingDesc->inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
50
51 // setup attribute descriptions
52 int vaCount = primProc.numAttribs();
Greg Daniel164a9f02016-02-22 09:56:40 -050053 if (vaCount > 0) {
54 size_t offset = 0;
55 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
56 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
57 GrVertexAttribType attribType = attrib.fType;
58
59 VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex];
60 vkAttrib.location = attribIndex; // for now assume location = attribIndex
61 vkAttrib.binding = 0; // for now only one vertex buffer & binding
62 vkAttrib.format = attrib_type_to_vkformat(attribType);
63 vkAttrib.offset = static_cast<uint32_t>(offset);
64 offset += attrib.fOffset;
65 }
66 }
67
68 memset(vertexInputInfo, 0, sizeof(VkPipelineVertexInputStateCreateInfo));
69 vertexInputInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
70 vertexInputInfo->pNext = nullptr;
71 vertexInputInfo->flags = 0;
72 vertexInputInfo->vertexBindingDescriptionCount = 1;
73 vertexInputInfo->pVertexBindingDescriptions = bindingDesc;
74 vertexInputInfo->vertexAttributeDescriptionCount = vaCount;
75 vertexInputInfo->pVertexAttributeDescriptions = attributeDesc;
76}
77
78
79static void setup_input_assembly_state(GrPrimitiveType primitiveType,
80 VkPipelineInputAssemblyStateCreateInfo* inputAssemblyInfo) {
81 static const VkPrimitiveTopology gPrimitiveType2VkTopology[] = {
82 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
83 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
84 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN,
85 VK_PRIMITIVE_TOPOLOGY_POINT_LIST,
86 VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
87 VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
88 };
89
90 memset(inputAssemblyInfo, 0, sizeof(VkPipelineInputAssemblyStateCreateInfo));
91 inputAssemblyInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
92 inputAssemblyInfo->pNext = nullptr;
93 inputAssemblyInfo->flags = 0;
94 inputAssemblyInfo->primitiveRestartEnable = false;
95 inputAssemblyInfo->topology = gPrimitiveType2VkTopology[primitiveType];
96}
97
98
99VkStencilOp stencil_op_to_vk_stencil_op(GrStencilOp op) {
100 static const VkStencilOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700101 VK_STENCIL_OP_KEEP, // kKeep
102 VK_STENCIL_OP_ZERO, // kZero
103 VK_STENCIL_OP_REPLACE, // kReplace
104 VK_STENCIL_OP_INVERT, // kInvert
105 VK_STENCIL_OP_INCREMENT_AND_WRAP, // kIncWrap
106 VK_STENCIL_OP_DECREMENT_AND_WRAP, // kDecWrap
107 VK_STENCIL_OP_INCREMENT_AND_CLAMP, // kIncClamp
108 VK_STENCIL_OP_DECREMENT_AND_CLAMP, // kDecClamp
Greg Daniel164a9f02016-02-22 09:56:40 -0500109 };
cdalton93a379b2016-05-11 13:58:08 -0700110 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilOpCount);
111 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
112 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
113 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
114 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
115 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
116 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
117 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
118 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
119 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
120 return gTable[(int)op];
Greg Daniel164a9f02016-02-22 09:56:40 -0500121}
122
cdalton93a379b2016-05-11 13:58:08 -0700123VkCompareOp stencil_func_to_vk_compare_op(GrStencilTest test) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500124 static const VkCompareOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700125 VK_COMPARE_OP_ALWAYS, // kAlways
126 VK_COMPARE_OP_NEVER, // kNever
127 VK_COMPARE_OP_GREATER, // kGreater
128 VK_COMPARE_OP_GREATER_OR_EQUAL, // kGEqual
129 VK_COMPARE_OP_LESS, // kLess
130 VK_COMPARE_OP_LESS_OR_EQUAL, // kLEqual
131 VK_COMPARE_OP_EQUAL, // kEqual
132 VK_COMPARE_OP_NOT_EQUAL, // kNotEqual
Greg Daniel164a9f02016-02-22 09:56:40 -0500133 };
cdalton93a379b2016-05-11 13:58:08 -0700134 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilTestCount);
135 GR_STATIC_ASSERT(0 == (int)GrStencilTest::kAlways);
136 GR_STATIC_ASSERT(1 == (int)GrStencilTest::kNever);
137 GR_STATIC_ASSERT(2 == (int)GrStencilTest::kGreater);
138 GR_STATIC_ASSERT(3 == (int)GrStencilTest::kGEqual);
139 GR_STATIC_ASSERT(4 == (int)GrStencilTest::kLess);
140 GR_STATIC_ASSERT(5 == (int)GrStencilTest::kLEqual);
141 GR_STATIC_ASSERT(6 == (int)GrStencilTest::kEqual);
142 GR_STATIC_ASSERT(7 == (int)GrStencilTest::kNotEqual);
143 SkASSERT(test < (GrStencilTest)kGrStencilTestCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500144
cdalton93a379b2016-05-11 13:58:08 -0700145 return gTable[(int)test];
Greg Daniel164a9f02016-02-22 09:56:40 -0500146}
147
148void setup_depth_stencil_state(const GrVkGpu* gpu,
149 const GrStencilSettings& stencilSettings,
150 VkPipelineDepthStencilStateCreateInfo* stencilInfo) {
151 memset(stencilInfo, 0, sizeof(VkPipelineDepthStencilStateCreateInfo));
152 stencilInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
153 stencilInfo->pNext = nullptr;
154 stencilInfo->flags = 0;
155 // set depth testing defaults
156 stencilInfo->depthTestEnable = VK_FALSE;
157 stencilInfo->depthWriteEnable = VK_FALSE;
158 stencilInfo->depthCompareOp = VK_COMPARE_OP_ALWAYS;
159 stencilInfo->depthBoundsTestEnable = VK_FALSE;
160 stencilInfo->stencilTestEnable = !stencilSettings.isDisabled();
161 if (!stencilSettings.isDisabled()) {
162 // Set front face
cdalton93a379b2016-05-11 13:58:08 -0700163 const GrStencilSettings::Face& front = stencilSettings.front();
164 stencilInfo->front.failOp = stencil_op_to_vk_stencil_op(front.fFailOp);
165 stencilInfo->front.passOp = stencil_op_to_vk_stencil_op(front.fPassOp);
Greg Daniel164a9f02016-02-22 09:56:40 -0500166 stencilInfo->front.depthFailOp = stencilInfo->front.failOp;
cdalton93a379b2016-05-11 13:58:08 -0700167 stencilInfo->front.compareOp = stencil_func_to_vk_compare_op(front.fTest);
168 stencilInfo->front.compareMask = front.fTestMask;
169 stencilInfo->front.writeMask = front.fWriteMask;
170 stencilInfo->front.reference = front.fRef;
Greg Daniel164a9f02016-02-22 09:56:40 -0500171
172 // Set back face
cdalton93a379b2016-05-11 13:58:08 -0700173 if (!stencilSettings.isTwoSided()) {
174 stencilInfo->back = stencilInfo->front;
175 } else {
176 const GrStencilSettings::Face& back = stencilSettings.back();
177 stencilInfo->back.failOp = stencil_op_to_vk_stencil_op(back.fFailOp);
178 stencilInfo->back.passOp = stencil_op_to_vk_stencil_op(back.fPassOp);
179 stencilInfo->back.depthFailOp = stencilInfo->front.failOp;
180 stencilInfo->back.compareOp = stencil_func_to_vk_compare_op(back.fTest);
181 stencilInfo->back.compareMask = back.fTestMask;
182 stencilInfo->back.writeMask = back.fWriteMask;
183 stencilInfo->back.reference = back.fRef;
184 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500185 }
186 stencilInfo->minDepthBounds = 0.0f;
187 stencilInfo->maxDepthBounds = 1.0f;
188}
189
190void setup_viewport_scissor_state(const GrVkGpu* gpu,
191 const GrPipeline& pipeline,
192 const GrVkRenderTarget* vkRT,
egdaniel470d77a2016-03-18 12:50:27 -0700193 VkPipelineViewportStateCreateInfo* viewportInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500194 memset(viewportInfo, 0, sizeof(VkPipelineViewportStateCreateInfo));
195 viewportInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
196 viewportInfo->pNext = nullptr;
197 viewportInfo->flags = 0;
198
Greg Daniel164a9f02016-02-22 09:56:40 -0500199 viewportInfo->viewportCount = 1;
egdaniel470d77a2016-03-18 12:50:27 -0700200 viewportInfo->pViewports = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500201
egdaniel470d77a2016-03-18 12:50:27 -0700202 viewportInfo->scissorCount = 1;
203 viewportInfo->pScissors = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500204
Greg Daniel164a9f02016-02-22 09:56:40 -0500205 SkASSERT(viewportInfo->viewportCount == viewportInfo->scissorCount);
206}
207
halcanary9d524f22016-03-29 09:03:52 -0700208void setup_multisample_state(const GrPipeline& pipeline,
ethannicholas28ef4452016-03-25 09:26:03 -0700209 const GrPrimitiveProcessor& primProc,
210 const GrCaps* caps,
Greg Daniel164a9f02016-02-22 09:56:40 -0500211 VkPipelineMultisampleStateCreateInfo* multisampleInfo) {
212 memset(multisampleInfo, 0, sizeof(VkPipelineMultisampleStateCreateInfo));
213 multisampleInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
214 multisampleInfo->pNext = nullptr;
215 multisampleInfo->flags = 0;
216 int numSamples = pipeline.getRenderTarget()->numColorSamples();
217 SkAssertResult(GrSampleCountToVkSampleCount(numSamples,
218 &multisampleInfo->rasterizationSamples));
ethannicholas28ef4452016-03-25 09:26:03 -0700219 float sampleShading = primProc.getSampleShading();
220 SkASSERT(sampleShading == 0.0f || caps->sampleShadingSupport());
221 multisampleInfo->sampleShadingEnable = sampleShading > 0.0f;
222 multisampleInfo->minSampleShading = sampleShading;
Greg Daniel164a9f02016-02-22 09:56:40 -0500223 multisampleInfo->pSampleMask = nullptr;
224 multisampleInfo->alphaToCoverageEnable = VK_FALSE;
225 multisampleInfo->alphaToOneEnable = VK_FALSE;
226}
227
228static VkBlendFactor blend_coeff_to_vk_blend(GrBlendCoeff coeff) {
229 static const VkBlendFactor gTable[] = {
230 VK_BLEND_FACTOR_ZERO, // kZero_GrBlendCoeff
231 VK_BLEND_FACTOR_ONE, // kOne_GrBlendCoeff
232 VK_BLEND_FACTOR_SRC_COLOR, // kSC_GrBlendCoeff
233 VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR, // kISC_GrBlendCoeff
234 VK_BLEND_FACTOR_DST_COLOR, // kDC_GrBlendCoeff
235 VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR, // kIDC_GrBlendCoeff
236 VK_BLEND_FACTOR_SRC_ALPHA, // kSA_GrBlendCoeff
237 VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, // kISA_GrBlendCoeff
238 VK_BLEND_FACTOR_DST_ALPHA, // kDA_GrBlendCoeff
239 VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA, // kIDA_GrBlendCoeff
240 VK_BLEND_FACTOR_CONSTANT_COLOR, // kConstC_GrBlendCoeff
241 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR, // kIConstC_GrBlendCoeff
242 VK_BLEND_FACTOR_CONSTANT_ALPHA, // kConstA_GrBlendCoeff
243 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA, // kIConstA_GrBlendCoeff
244 VK_BLEND_FACTOR_SRC1_COLOR, // kS2C_GrBlendCoeff
245 VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR, // kIS2C_GrBlendCoeff
246 VK_BLEND_FACTOR_SRC1_ALPHA, // kS2A_GrBlendCoeff
247 VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA, // kIS2A_GrBlendCoeff
248
249 };
250 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrBlendCoeffCnt);
251 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
252 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
253 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
254 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
255 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
256 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
257 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
258 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
259 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
260 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
261 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
262 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
263 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
264 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
265 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
266 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
267 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
268 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
269
270 SkASSERT((unsigned)coeff < kGrBlendCoeffCnt);
271 return gTable[coeff];
272}
273
274
275static VkBlendOp blend_equation_to_vk_blend_op(GrBlendEquation equation) {
276 static const VkBlendOp gTable[] = {
277 VK_BLEND_OP_ADD, // kAdd_GrBlendEquation
278 VK_BLEND_OP_SUBTRACT, // kSubtract_GrBlendEquation
279 VK_BLEND_OP_REVERSE_SUBTRACT, // kReverseSubtract_GrBlendEquation
280 };
281 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kFirstAdvancedGrBlendEquation);
282 GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
283 GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
284 GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
285
286 SkASSERT((unsigned)equation < kGrBlendCoeffCnt);
287 return gTable[equation];
288}
289
290bool blend_coeff_refs_constant(GrBlendCoeff coeff) {
291 static const bool gCoeffReferencesBlendConst[] = {
292 false,
293 false,
294 false,
295 false,
296 false,
297 false,
298 false,
299 false,
300 false,
301 false,
302 true,
303 true,
304 true,
305 true,
306
307 // extended blend coeffs
308 false,
309 false,
310 false,
311 false,
312 };
313 return gCoeffReferencesBlendConst[coeff];
314 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
315 // Individual enum asserts already made in blend_coeff_to_vk_blend
316}
317
318void setup_color_blend_state(const GrVkGpu* gpu,
319 const GrPipeline& pipeline,
320 VkPipelineColorBlendStateCreateInfo* colorBlendInfo,
321 VkPipelineColorBlendAttachmentState* attachmentState) {
322 GrXferProcessor::BlendInfo blendInfo;
323 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
324
325 GrBlendEquation equation = blendInfo.fEquation;
326 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
327 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
328 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
329 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
330
331 memset(attachmentState, 0, sizeof(VkPipelineColorBlendAttachmentState));
332 attachmentState->blendEnable = !blendOff;
333 if (!blendOff) {
334 attachmentState->srcColorBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
335 attachmentState->dstColorBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
336 attachmentState->colorBlendOp = blend_equation_to_vk_blend_op(equation);
337 attachmentState->srcAlphaBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
338 attachmentState->dstAlphaBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
339 attachmentState->alphaBlendOp = blend_equation_to_vk_blend_op(equation);
340 }
egdaniel3d5d9ac2016-03-01 12:56:15 -0800341
342 if (!blendInfo.fWriteColor) {
343 attachmentState->colorWriteMask = 0;
344 } else {
345 attachmentState->colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
346 VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
347 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500348
349 memset(colorBlendInfo, 0, sizeof(VkPipelineColorBlendStateCreateInfo));
350 colorBlendInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
351 colorBlendInfo->pNext = nullptr;
352 colorBlendInfo->flags = 0;
353 colorBlendInfo->logicOpEnable = VK_FALSE;
354 colorBlendInfo->attachmentCount = 1;
355 colorBlendInfo->pAttachments = attachmentState;
egdaniel470d77a2016-03-18 12:50:27 -0700356 // colorBlendInfo->blendConstants is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500357}
358
359VkCullModeFlags draw_face_to_vk_cull_mode(GrPipelineBuilder::DrawFace drawFace) {
360 // Assumes that we've set the front face to be ccw
361 static const VkCullModeFlags gTable[] = {
362 VK_CULL_MODE_NONE, // kBoth_DrawFace
363 VK_CULL_MODE_BACK_BIT, // kCCW_DrawFace, cull back face
364 VK_CULL_MODE_FRONT_BIT, // kCW_DrawFace, cull front face
365 };
366 GR_STATIC_ASSERT(0 == GrPipelineBuilder::kBoth_DrawFace);
367 GR_STATIC_ASSERT(1 == GrPipelineBuilder::kCCW_DrawFace);
368 GR_STATIC_ASSERT(2 == GrPipelineBuilder::kCW_DrawFace);
369 SkASSERT((unsigned)drawFace <= 2);
370
371 return gTable[drawFace];
372}
373
374void setup_raster_state(const GrVkGpu* gpu,
375 const GrPipeline& pipeline,
376 VkPipelineRasterizationStateCreateInfo* rasterInfo) {
377 memset(rasterInfo, 0, sizeof(VkPipelineRasterizationStateCreateInfo));
378 rasterInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
379 rasterInfo->pNext = nullptr;
380 rasterInfo->flags = 0;
381 rasterInfo->depthClampEnable = VK_FALSE;
382 rasterInfo->rasterizerDiscardEnable = VK_FALSE;
383 rasterInfo->polygonMode = VK_POLYGON_MODE_FILL;
384 rasterInfo->cullMode = draw_face_to_vk_cull_mode(pipeline.getDrawFace());
385 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
393void setup_dynamic_state(const GrVkGpu* gpu,
394 const GrPipeline& pipeline,
egdaniel470d77a2016-03-18 12:50:27 -0700395 VkPipelineDynamicStateCreateInfo* dynamicInfo,
396 VkDynamicState* dynamicStates) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500397 memset(dynamicInfo, 0, sizeof(VkPipelineDynamicStateCreateInfo));
398 dynamicInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
egdaniel470d77a2016-03-18 12:50:27 -0700399 dynamicInfo->pNext = VK_NULL_HANDLE;
400 dynamicInfo->flags = 0;
401 dynamicStates[0] = VK_DYNAMIC_STATE_VIEWPORT;
402 dynamicStates[1] = VK_DYNAMIC_STATE_SCISSOR;
403 dynamicStates[2] = VK_DYNAMIC_STATE_BLEND_CONSTANTS;
404 dynamicInfo->dynamicStateCount = 3;
405 dynamicInfo->pDynamicStates = dynamicStates;
Greg Daniel164a9f02016-02-22 09:56:40 -0500406}
407
408GrVkPipeline* GrVkPipeline::Create(GrVkGpu* gpu, const GrPipeline& pipeline,
409 const GrPrimitiveProcessor& primProc,
410 VkPipelineShaderStageCreateInfo* shaderStageInfo,
411 int shaderStageCount,
412 GrPrimitiveType primitiveType,
413 const GrVkRenderPass& renderPass,
jvanverth03509ea2016-03-02 13:19:47 -0800414 VkPipelineLayout layout,
415 VkPipelineCache cache) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500416 VkPipelineVertexInputStateCreateInfo vertexInputInfo;
417 VkVertexInputBindingDescription bindingDesc;
egdanielb05df0f2016-06-27 07:15:20 -0700418 SkSTArray<16, VkVertexInputAttributeDescription> attributeDesc;
419 SkASSERT(primProc.numAttribs() <= gpu->vkCaps().maxVertexAttributes());
420 VkVertexInputAttributeDescription* pAttribs = attributeDesc.push_back_n(primProc.numAttribs());
421 setup_vertex_input_state(primProc, &vertexInputInfo, &bindingDesc, 1, pAttribs);
Greg Daniel164a9f02016-02-22 09:56:40 -0500422
423 VkPipelineInputAssemblyStateCreateInfo inputAssemblyInfo;
424 setup_input_assembly_state(primitiveType, &inputAssemblyInfo);
425
426 VkPipelineDepthStencilStateCreateInfo depthStencilInfo;
427 setup_depth_stencil_state(gpu, pipeline.getStencil(), &depthStencilInfo);
428
429 GrRenderTarget* rt = pipeline.getRenderTarget();
430 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(rt);
431 VkPipelineViewportStateCreateInfo viewportInfo;
egdaniel470d77a2016-03-18 12:50:27 -0700432 setup_viewport_scissor_state(gpu, pipeline, vkRT, &viewportInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500433
434 VkPipelineMultisampleStateCreateInfo multisampleInfo;
ethannicholas28ef4452016-03-25 09:26:03 -0700435 setup_multisample_state(pipeline, primProc, gpu->caps(), &multisampleInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500436
437 // We will only have one color attachment per pipeline.
438 VkPipelineColorBlendAttachmentState attachmentStates[1];
439 VkPipelineColorBlendStateCreateInfo colorBlendInfo;
440 setup_color_blend_state(gpu, pipeline, &colorBlendInfo, attachmentStates);
441
442 VkPipelineRasterizationStateCreateInfo rasterInfo;
443 setup_raster_state(gpu, pipeline, &rasterInfo);
444
egdaniel470d77a2016-03-18 12:50:27 -0700445 VkDynamicState dynamicStates[3];
Greg Daniel164a9f02016-02-22 09:56:40 -0500446 VkPipelineDynamicStateCreateInfo dynamicInfo;
egdaniel470d77a2016-03-18 12:50:27 -0700447 setup_dynamic_state(gpu, pipeline, &dynamicInfo, dynamicStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500448
449 VkGraphicsPipelineCreateInfo pipelineCreateInfo;
450 memset(&pipelineCreateInfo, 0, sizeof(VkGraphicsPipelineCreateInfo));
451 pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
452 pipelineCreateInfo.pNext = nullptr;
453 pipelineCreateInfo.flags = 0;
454 pipelineCreateInfo.stageCount = shaderStageCount;
455 pipelineCreateInfo.pStages = shaderStageInfo;
456 pipelineCreateInfo.pVertexInputState = &vertexInputInfo;
457 pipelineCreateInfo.pInputAssemblyState = &inputAssemblyInfo;
458 pipelineCreateInfo.pTessellationState = nullptr;
459 pipelineCreateInfo.pViewportState = &viewportInfo;
460 pipelineCreateInfo.pRasterizationState = &rasterInfo;
461 pipelineCreateInfo.pMultisampleState = &multisampleInfo;
462 pipelineCreateInfo.pDepthStencilState = &depthStencilInfo;
463 pipelineCreateInfo.pColorBlendState = &colorBlendInfo;
464 pipelineCreateInfo.pDynamicState = &dynamicInfo;
465 pipelineCreateInfo.layout = layout;
466 pipelineCreateInfo.renderPass = renderPass.vkRenderPass();
467 pipelineCreateInfo.subpass = 0;
468 pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
469 pipelineCreateInfo.basePipelineIndex = -1;
470
471 VkPipeline vkPipeline;
472 VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateGraphicsPipelines(gpu->device(),
halcanary9d524f22016-03-29 09:03:52 -0700473 cache, 1,
474 &pipelineCreateInfo,
Greg Daniel164a9f02016-02-22 09:56:40 -0500475 nullptr, &vkPipeline));
476 if (err) {
477 return nullptr;
478 }
479
480 return new GrVkPipeline(vkPipeline);
481}
482
483void GrVkPipeline::freeGPUData(const GrVkGpu* gpu) const {
484 GR_VK_CALL(gpu->vkInterface(), DestroyPipeline(gpu->device(), fPipeline, nullptr));
485}
486
egdaniel470d77a2016-03-18 12:50:27 -0700487void set_dynamic_scissor_state(GrVkGpu* gpu,
488 GrVkCommandBuffer* cmdBuffer,
489 const GrPipeline& pipeline,
490 const GrRenderTarget& target) {
491 // We always use one scissor and if it is disabled we just make it the size of the RT
492 const GrScissorState& scissorState = pipeline.getScissorState();
493 VkRect2D scissor;
494 if (scissorState.enabled() &&
495 !scissorState.rect().contains(0, 0, target.width(), target.height())) {
496 // This all assumes the scissorState has previously been clipped to the device space render
halcanary9d524f22016-03-29 09:03:52 -0700497 // target.
egdaniel470d77a2016-03-18 12:50:27 -0700498 scissor.offset.x = scissorState.rect().fLeft;
499 scissor.extent.width = scissorState.rect().width();
500 if (kTopLeft_GrSurfaceOrigin == target.origin()) {
501 scissor.offset.y = scissorState.rect().fTop;
502 } else {
503 SkASSERT(kBottomLeft_GrSurfaceOrigin == target.origin());
504 scissor.offset.y = target.height() - scissorState.rect().fBottom;
505 }
506 scissor.extent.height = scissorState.rect().height();
507
508 SkASSERT(scissor.offset.x >= 0);
509 SkASSERT(scissor.offset.x + scissor.extent.width <= (uint32_t)target.width());
510 SkASSERT(scissor.offset.y >= 0);
511 SkASSERT(scissor.offset.y + scissor.extent.height <= (uint32_t)target.height());
512 } else {
513 scissor.extent.width = target.width();
514 scissor.extent.height = target.height();
515 scissor.offset.x = 0;
516 scissor.offset.y = 0;
517 }
518 cmdBuffer->setScissor(gpu, 0, 1, &scissor);
519}
520
521void set_dynamic_viewport_state(GrVkGpu* gpu,
522 GrVkCommandBuffer* cmdBuffer,
523 const GrRenderTarget& target) {
524 // We always use one viewport the size of the RT
525 VkViewport viewport;
526 viewport.x = 0.0f;
527 viewport.y = 0.0f;
528 viewport.width = SkIntToScalar(target.width());
529 viewport.height = SkIntToScalar(target.height());
530 viewport.minDepth = 0.0f;
531 viewport.maxDepth = 1.0f;
532 cmdBuffer->setViewport(gpu, 0, 1, &viewport);
533}
534
535void set_dynamic_blend_constant_state(GrVkGpu* gpu,
536 GrVkCommandBuffer* cmdBuffer,
537 const GrPipeline& pipeline) {
538 GrXferProcessor::BlendInfo blendInfo;
539 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
540 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)) {
544 GrColorToRGBAFloat(blendInfo.fBlendConstant, floatColors);
545 } else {
546 memset(floatColors, 0, 4 * sizeof(float));
547 }
548 cmdBuffer->setBlendConstants(gpu, floatColors);
549}
550
egdaniel470d77a2016-03-18 12:50:27 -0700551void GrVkPipeline::SetDynamicState(GrVkGpu* gpu,
552 GrVkCommandBuffer* cmdBuffer,
553 const GrPipeline& pipeline) {
554 const GrRenderTarget& target = *pipeline.getRenderTarget();
555 set_dynamic_scissor_state(gpu, cmdBuffer, pipeline, target);
556 set_dynamic_viewport_state(gpu, cmdBuffer, target);
557 set_dynamic_blend_constant_state(gpu, cmdBuffer, pipeline);
558}