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