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