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