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