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