blob: 04fefffceead5b160b802c25f7fefe4ca3bd4aaa [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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"
Greg Daniel164a9f02016-02-22 09:56:40 -050016
Ben Wagner6c30e742019-02-06 10:46:14 -050017#if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS)
18#include <sanitizer/lsan_interface.h>
19#endif
20
csmartdaltonb37cb232017-02-08 14:56:27 -050021static inline VkFormat attrib_type_to_vkformat(GrVertexAttribType type) {
22 switch (type) {
23 case kFloat_GrVertexAttribType:
24 return VK_FORMAT_R32_SFLOAT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040025 case kFloat2_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050026 return VK_FORMAT_R32G32_SFLOAT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040027 case kFloat3_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050028 return VK_FORMAT_R32G32B32_SFLOAT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040029 case kFloat4_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050030 return VK_FORMAT_R32G32B32A32_SFLOAT;
Brian Osmand4c29702018-09-14 16:16:55 -040031 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;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040039 case kInt2_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050040 return VK_FORMAT_R32G32_SINT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040041 case kInt3_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050042 return VK_FORMAT_R32G32B32_SINT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040043 case kInt4_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050044 return VK_FORMAT_R32G32B32A32_SINT;
Ruiqi Maob609e6d2018-07-17 10:19:38 -040045 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;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040061 case kUByte_norm_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050062 return VK_FORMAT_R8_UNORM;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040063 case kUByte4_norm_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050064 return VK_FORMAT_R8G8B8A8_UNORM;
Chris Daltona045eea2017-10-24 13:22:10 -060065 case kShort2_GrVertexAttribType:
66 return VK_FORMAT_R16G16_SINT;
Brian Osmana5c578f2018-09-19 14:19:02 -040067 case kShort4_GrVertexAttribType:
68 return VK_FORMAT_R16G16B16A16_SINT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040069 case kUShort2_GrVertexAttribType:
Robert Phillips8296e752017-08-25 08:45:21 -040070 return VK_FORMAT_R16G16_UINT;
Chris Daltona045eea2017-10-24 13:22:10 -060071 case kUShort2_norm_GrVertexAttribType:
72 return VK_FORMAT_R16G16_UNORM;
csmartdaltonb37cb232017-02-08 14:56:27 -050073 case kInt_GrVertexAttribType:
74 return VK_FORMAT_R32_SINT;
75 case kUint_GrVertexAttribType:
76 return VK_FORMAT_R32_UINT;
Robert Phillipsfe18de52019-06-06 17:21:50 -040077 case kUShort_norm_GrVertexAttribType:
78 return VK_FORMAT_R16_UNORM;
Robert Phillips66a46032019-06-18 08:00:42 -040079 // Experimental (for Y416)
80 case kUShort4_norm_GrVertexAttribType:
81 return VK_FORMAT_R16G16B16A16_UNORM;
csmartdaltonb37cb232017-02-08 14:56:27 -050082 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040083 SK_ABORT("Unknown vertex attrib type");
csmartdaltonb37cb232017-02-08 14:56:27 -050084 return VK_FORMAT_UNDEFINED;
Greg Daniel164a9f02016-02-22 09:56:40 -050085}
86
87static void setup_vertex_input_state(const GrPrimitiveProcessor& primProc,
Chris Dalton1d616352017-05-31 12:51:23 -060088 VkPipelineVertexInputStateCreateInfo* vertexInputInfo,
89 SkSTArray<2, VkVertexInputBindingDescription, true>* bindingDescs,
90 VkVertexInputAttributeDescription* attributeDesc) {
Kevin Lubick42846132018-01-05 10:11:11 -050091 uint32_t vertexBinding = 0, instanceBinding = 0;
Chris Dalton1d616352017-05-31 12:51:23 -060092
Brian Salomon92be2f72018-06-19 14:33:47 -040093 int nextBinding = bindingDescs->count();
94 if (primProc.hasVertexAttributes()) {
95 vertexBinding = nextBinding++;
Chris Dalton1d616352017-05-31 12:51:23 -060096 }
97
Brian Salomon92be2f72018-06-19 14:33:47 -040098 if (primProc.hasInstanceAttributes()) {
99 instanceBinding = nextBinding;
Chris Dalton1d616352017-05-31 12:51:23 -0600100 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500101
102 // setup attribute descriptions
Brian Salomon92be2f72018-06-19 14:33:47 -0400103 int vaCount = primProc.numVertexAttributes();
104 int attribIndex = 0;
105 size_t vertexAttributeOffset = 0;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500106 for (const auto& attrib : primProc.vertexAttributes()) {
Brian Salomon92be2f72018-06-19 14:33:47 -0400107 VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex];
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500108 vkAttrib.location = attribIndex++; // for now assume location = attribIndex
Brian Salomon92be2f72018-06-19 14:33:47 -0400109 vkAttrib.binding = vertexBinding;
Brian Osmand4c29702018-09-14 16:16:55 -0400110 vkAttrib.format = attrib_type_to_vkformat(attrib.cpuType());
Brian Salomon92be2f72018-06-19 14:33:47 -0400111 vkAttrib.offset = vertexAttributeOffset;
Brian Salomon92be2f72018-06-19 14:33:47 -0400112 vertexAttributeOffset += attrib.sizeAlign4();
113 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500114 SkASSERT(vertexAttributeOffset == primProc.vertexStride());
Brian Salomon92be2f72018-06-19 14:33:47 -0400115
116 int iaCount = primProc.numInstanceAttributes();
117 size_t instanceAttributeOffset = 0;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500118 for (const auto& attrib : primProc.instanceAttributes()) {
Brian Salomon92be2f72018-06-19 14:33:47 -0400119 VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex];
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500120 vkAttrib.location = attribIndex++; // for now assume location = attribIndex
Brian Salomon92be2f72018-06-19 14:33:47 -0400121 vkAttrib.binding = instanceBinding;
Brian Osmand4c29702018-09-14 16:16:55 -0400122 vkAttrib.format = attrib_type_to_vkformat(attrib.cpuType());
Brian Salomon92be2f72018-06-19 14:33:47 -0400123 vkAttrib.offset = instanceAttributeOffset;
Brian Salomon92be2f72018-06-19 14:33:47 -0400124 instanceAttributeOffset += attrib.sizeAlign4();
125 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500126 SkASSERT(instanceAttributeOffset == primProc.instanceStride());
Brian Salomon92be2f72018-06-19 14:33:47 -0400127
128 if (primProc.hasVertexAttributes()) {
129 bindingDescs->push_back() = {
130 vertexBinding,
131 (uint32_t) vertexAttributeOffset,
132 VK_VERTEX_INPUT_RATE_VERTEX
133 };
134 }
135 if (primProc.hasInstanceAttributes()) {
136 bindingDescs->push_back() = {
137 instanceBinding,
138 (uint32_t) instanceAttributeOffset,
139 VK_VERTEX_INPUT_RATE_INSTANCE
140 };
Greg Daniel164a9f02016-02-22 09:56:40 -0500141 }
142
143 memset(vertexInputInfo, 0, sizeof(VkPipelineVertexInputStateCreateInfo));
144 vertexInputInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
145 vertexInputInfo->pNext = nullptr;
146 vertexInputInfo->flags = 0;
Chris Dalton1d616352017-05-31 12:51:23 -0600147 vertexInputInfo->vertexBindingDescriptionCount = bindingDescs->count();
148 vertexInputInfo->pVertexBindingDescriptions = bindingDescs->begin();
Brian Salomon92be2f72018-06-19 14:33:47 -0400149 vertexInputInfo->vertexAttributeDescriptionCount = vaCount + iaCount;
Greg Daniel164a9f02016-02-22 09:56:40 -0500150 vertexInputInfo->pVertexAttributeDescriptions = attributeDesc;
151}
152
Chris Dalton3809bab2017-06-13 10:55:06 -0600153static VkPrimitiveTopology gr_primitive_type_to_vk_topology(GrPrimitiveType primitiveType) {
154 switch (primitiveType) {
155 case GrPrimitiveType::kTriangles:
156 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
157 case GrPrimitiveType::kTriangleStrip:
158 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -0600159 case GrPrimitiveType::kPoints:
160 return VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
161 case GrPrimitiveType::kLines:
162 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
163 case GrPrimitiveType::kLineStrip:
164 return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
165 case GrPrimitiveType::kLinesAdjacency:
166 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY;
167 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400168 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -0600169 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
170}
Greg Daniel164a9f02016-02-22 09:56:40 -0500171
172static void setup_input_assembly_state(GrPrimitiveType primitiveType,
173 VkPipelineInputAssemblyStateCreateInfo* inputAssemblyInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500174 memset(inputAssemblyInfo, 0, sizeof(VkPipelineInputAssemblyStateCreateInfo));
175 inputAssemblyInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
176 inputAssemblyInfo->pNext = nullptr;
177 inputAssemblyInfo->flags = 0;
178 inputAssemblyInfo->primitiveRestartEnable = false;
Chris Dalton3809bab2017-06-13 10:55:06 -0600179 inputAssemblyInfo->topology = gr_primitive_type_to_vk_topology(primitiveType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500180}
181
182
egdanielec440992016-09-13 09:54:11 -0700183static VkStencilOp stencil_op_to_vk_stencil_op(GrStencilOp op) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500184 static const VkStencilOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700185 VK_STENCIL_OP_KEEP, // kKeep
186 VK_STENCIL_OP_ZERO, // kZero
187 VK_STENCIL_OP_REPLACE, // kReplace
188 VK_STENCIL_OP_INVERT, // kInvert
189 VK_STENCIL_OP_INCREMENT_AND_WRAP, // kIncWrap
190 VK_STENCIL_OP_DECREMENT_AND_WRAP, // kDecWrap
191 VK_STENCIL_OP_INCREMENT_AND_CLAMP, // kIncClamp
192 VK_STENCIL_OP_DECREMENT_AND_CLAMP, // kDecClamp
Greg Daniel164a9f02016-02-22 09:56:40 -0500193 };
cdalton93a379b2016-05-11 13:58:08 -0700194 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilOpCount);
195 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
196 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
197 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
198 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
199 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
200 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
201 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
202 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
203 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
204 return gTable[(int)op];
Greg Daniel164a9f02016-02-22 09:56:40 -0500205}
206
egdanielec440992016-09-13 09:54:11 -0700207static VkCompareOp stencil_func_to_vk_compare_op(GrStencilTest test) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500208 static const VkCompareOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700209 VK_COMPARE_OP_ALWAYS, // kAlways
210 VK_COMPARE_OP_NEVER, // kNever
211 VK_COMPARE_OP_GREATER, // kGreater
212 VK_COMPARE_OP_GREATER_OR_EQUAL, // kGEqual
213 VK_COMPARE_OP_LESS, // kLess
214 VK_COMPARE_OP_LESS_OR_EQUAL, // kLEqual
215 VK_COMPARE_OP_EQUAL, // kEqual
216 VK_COMPARE_OP_NOT_EQUAL, // kNotEqual
Greg Daniel164a9f02016-02-22 09:56:40 -0500217 };
cdalton93a379b2016-05-11 13:58:08 -0700218 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilTestCount);
219 GR_STATIC_ASSERT(0 == (int)GrStencilTest::kAlways);
220 GR_STATIC_ASSERT(1 == (int)GrStencilTest::kNever);
221 GR_STATIC_ASSERT(2 == (int)GrStencilTest::kGreater);
222 GR_STATIC_ASSERT(3 == (int)GrStencilTest::kGEqual);
223 GR_STATIC_ASSERT(4 == (int)GrStencilTest::kLess);
224 GR_STATIC_ASSERT(5 == (int)GrStencilTest::kLEqual);
225 GR_STATIC_ASSERT(6 == (int)GrStencilTest::kEqual);
226 GR_STATIC_ASSERT(7 == (int)GrStencilTest::kNotEqual);
227 SkASSERT(test < (GrStencilTest)kGrStencilTestCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500228
cdalton93a379b2016-05-11 13:58:08 -0700229 return gTable[(int)test];
Greg Daniel164a9f02016-02-22 09:56:40 -0500230}
231
Chris Dalton71713f62019-04-18 12:41:03 -0600232static void setup_stencil_op_state(
233 VkStencilOpState* opState, const GrStencilSettings::Face& stencilFace) {
234 opState->failOp = stencil_op_to_vk_stencil_op(stencilFace.fFailOp);
235 opState->passOp = stencil_op_to_vk_stencil_op(stencilFace.fPassOp);
236 opState->depthFailOp = opState->failOp;
237 opState->compareOp = stencil_func_to_vk_compare_op(stencilFace.fTest);
238 opState->compareMask = stencilFace.fTestMask;
239 opState->writeMask = stencilFace.fWriteMask;
240 opState->reference = stencilFace.fRef;
241}
242
243static void setup_depth_stencil_state(
244 const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin,
245 VkPipelineDepthStencilStateCreateInfo* stencilInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500246 memset(stencilInfo, 0, sizeof(VkPipelineDepthStencilStateCreateInfo));
247 stencilInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
248 stencilInfo->pNext = nullptr;
249 stencilInfo->flags = 0;
250 // set depth testing defaults
251 stencilInfo->depthTestEnable = VK_FALSE;
252 stencilInfo->depthWriteEnable = VK_FALSE;
253 stencilInfo->depthCompareOp = VK_COMPARE_OP_ALWAYS;
254 stencilInfo->depthBoundsTestEnable = VK_FALSE;
255 stencilInfo->stencilTestEnable = !stencilSettings.isDisabled();
256 if (!stencilSettings.isDisabled()) {
cdalton93a379b2016-05-11 13:58:08 -0700257 if (!stencilSettings.isTwoSided()) {
Chris Dalton71713f62019-04-18 12:41:03 -0600258 setup_stencil_op_state(&stencilInfo->front, stencilSettings.frontAndBack());
cdalton93a379b2016-05-11 13:58:08 -0700259 stencilInfo->back = stencilInfo->front;
260 } else {
Chris Dalton71713f62019-04-18 12:41:03 -0600261 setup_stencil_op_state(&stencilInfo->front, stencilSettings.front(origin));
262 setup_stencil_op_state(&stencilInfo->back, stencilSettings.back(origin));
cdalton93a379b2016-05-11 13:58:08 -0700263 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500264 }
265 stencilInfo->minDepthBounds = 0.0f;
266 stencilInfo->maxDepthBounds = 1.0f;
267}
268
egdanielec440992016-09-13 09:54:11 -0700269static void setup_viewport_scissor_state(VkPipelineViewportStateCreateInfo* viewportInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500270 memset(viewportInfo, 0, sizeof(VkPipelineViewportStateCreateInfo));
271 viewportInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
272 viewportInfo->pNext = nullptr;
273 viewportInfo->flags = 0;
274
Greg Daniel164a9f02016-02-22 09:56:40 -0500275 viewportInfo->viewportCount = 1;
egdaniel470d77a2016-03-18 12:50:27 -0700276 viewportInfo->pViewports = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500277
egdaniel470d77a2016-03-18 12:50:27 -0700278 viewportInfo->scissorCount = 1;
279 viewportInfo->pScissors = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500280
Greg Daniel164a9f02016-02-22 09:56:40 -0500281 SkASSERT(viewportInfo->viewportCount == viewportInfo->scissorCount);
282}
283
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500284static void setup_multisample_state(int numColorSamples,
285 const GrPrimitiveProcessor& primProc,
Brian Salomonff168d92018-06-23 15:17:27 -0400286 const GrPipeline& pipeline,
egdanielec440992016-09-13 09:54:11 -0700287 const GrCaps* caps,
288 VkPipelineMultisampleStateCreateInfo* multisampleInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500289 memset(multisampleInfo, 0, sizeof(VkPipelineMultisampleStateCreateInfo));
290 multisampleInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
291 multisampleInfo->pNext = nullptr;
292 multisampleInfo->flags = 0;
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500293 SkAssertResult(GrSampleCountToVkSampleCount(numColorSamples,
Greg Daniel164a9f02016-02-22 09:56:40 -0500294 &multisampleInfo->rasterizationSamples));
Brian Osmana63593a2018-12-06 15:54:53 -0500295 multisampleInfo->sampleShadingEnable = VK_FALSE;
296 multisampleInfo->minSampleShading = 0.0f;
Greg Daniel164a9f02016-02-22 09:56:40 -0500297 multisampleInfo->pSampleMask = nullptr;
298 multisampleInfo->alphaToCoverageEnable = VK_FALSE;
299 multisampleInfo->alphaToOneEnable = VK_FALSE;
300}
301
302static VkBlendFactor blend_coeff_to_vk_blend(GrBlendCoeff coeff) {
303 static const VkBlendFactor gTable[] = {
304 VK_BLEND_FACTOR_ZERO, // kZero_GrBlendCoeff
305 VK_BLEND_FACTOR_ONE, // kOne_GrBlendCoeff
306 VK_BLEND_FACTOR_SRC_COLOR, // kSC_GrBlendCoeff
307 VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR, // kISC_GrBlendCoeff
308 VK_BLEND_FACTOR_DST_COLOR, // kDC_GrBlendCoeff
309 VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR, // kIDC_GrBlendCoeff
310 VK_BLEND_FACTOR_SRC_ALPHA, // kSA_GrBlendCoeff
311 VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, // kISA_GrBlendCoeff
312 VK_BLEND_FACTOR_DST_ALPHA, // kDA_GrBlendCoeff
313 VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA, // kIDA_GrBlendCoeff
314 VK_BLEND_FACTOR_CONSTANT_COLOR, // kConstC_GrBlendCoeff
315 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR, // kIConstC_GrBlendCoeff
316 VK_BLEND_FACTOR_CONSTANT_ALPHA, // kConstA_GrBlendCoeff
317 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA, // kIConstA_GrBlendCoeff
318 VK_BLEND_FACTOR_SRC1_COLOR, // kS2C_GrBlendCoeff
319 VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR, // kIS2C_GrBlendCoeff
320 VK_BLEND_FACTOR_SRC1_ALPHA, // kS2A_GrBlendCoeff
321 VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA, // kIS2A_GrBlendCoeff
Mike Klein36743362018-11-06 08:23:30 -0500322 VK_BLEND_FACTOR_ZERO, // kIllegal_GrBlendCoeff
Greg Daniel164a9f02016-02-22 09:56:40 -0500323 };
324 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrBlendCoeffCnt);
325 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
326 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
327 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
328 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
329 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
330 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
331 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
332 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
333 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
334 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
335 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
336 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
337 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
338 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
339 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
340 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
341 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
342 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
343
344 SkASSERT((unsigned)coeff < kGrBlendCoeffCnt);
345 return gTable[coeff];
346}
347
348
349static VkBlendOp blend_equation_to_vk_blend_op(GrBlendEquation equation) {
350 static const VkBlendOp gTable[] = {
Greg Daniel313c6952018-08-08 09:24:08 -0400351 // Basic blend ops
352 VK_BLEND_OP_ADD,
353 VK_BLEND_OP_SUBTRACT,
354 VK_BLEND_OP_REVERSE_SUBTRACT,
355
356 // Advanced blend ops
357 VK_BLEND_OP_SCREEN_EXT,
358 VK_BLEND_OP_OVERLAY_EXT,
359 VK_BLEND_OP_DARKEN_EXT,
360 VK_BLEND_OP_LIGHTEN_EXT,
361 VK_BLEND_OP_COLORDODGE_EXT,
362 VK_BLEND_OP_COLORBURN_EXT,
363 VK_BLEND_OP_HARDLIGHT_EXT,
364 VK_BLEND_OP_SOFTLIGHT_EXT,
365 VK_BLEND_OP_DIFFERENCE_EXT,
366 VK_BLEND_OP_EXCLUSION_EXT,
367 VK_BLEND_OP_MULTIPLY_EXT,
368 VK_BLEND_OP_HSL_HUE_EXT,
369 VK_BLEND_OP_HSL_SATURATION_EXT,
370 VK_BLEND_OP_HSL_COLOR_EXT,
Mike Klein36743362018-11-06 08:23:30 -0500371 VK_BLEND_OP_HSL_LUMINOSITY_EXT,
372
373 // Illegal.
374 VK_BLEND_OP_ADD,
Greg Daniel164a9f02016-02-22 09:56:40 -0500375 };
Greg Daniel164a9f02016-02-22 09:56:40 -0500376 GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
377 GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
378 GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
Greg Daniel313c6952018-08-08 09:24:08 -0400379 GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
380 GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
381 GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
382 GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
383 GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
384 GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
385 GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
386 GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
387 GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
388 GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
389 GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
390 GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
391 GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
392 GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
393 GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
394 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrBlendEquationCnt);
Greg Daniel164a9f02016-02-22 09:56:40 -0500395
396 SkASSERT((unsigned)equation < kGrBlendCoeffCnt);
397 return gTable[equation];
398}
399
egdanielec440992016-09-13 09:54:11 -0700400static bool blend_coeff_refs_constant(GrBlendCoeff coeff) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500401 static const bool gCoeffReferencesBlendConst[] = {
402 false,
403 false,
404 false,
405 false,
406 false,
407 false,
408 false,
409 false,
410 false,
411 false,
412 true,
413 true,
414 true,
415 true,
416
417 // extended blend coeffs
418 false,
419 false,
420 false,
421 false,
Mike Klein36743362018-11-06 08:23:30 -0500422
423 // Illegal
424 false,
Greg Daniel164a9f02016-02-22 09:56:40 -0500425 };
426 return gCoeffReferencesBlendConst[coeff];
427 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
428 // Individual enum asserts already made in blend_coeff_to_vk_blend
429}
430
egdanielec440992016-09-13 09:54:11 -0700431static void setup_color_blend_state(const GrPipeline& pipeline,
432 VkPipelineColorBlendStateCreateInfo* colorBlendInfo,
433 VkPipelineColorBlendAttachmentState* attachmentState) {
Chris Daltonbbb3f642019-07-24 12:25:08 -0400434 const GrXferProcessor::BlendInfo& blendInfo = pipeline.getXferProcessor().getBlendInfo();
Greg Daniel164a9f02016-02-22 09:56:40 -0500435
436 GrBlendEquation equation = blendInfo.fEquation;
437 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
438 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
439 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
440 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
441
442 memset(attachmentState, 0, sizeof(VkPipelineColorBlendAttachmentState));
443 attachmentState->blendEnable = !blendOff;
444 if (!blendOff) {
445 attachmentState->srcColorBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
446 attachmentState->dstColorBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
447 attachmentState->colorBlendOp = blend_equation_to_vk_blend_op(equation);
448 attachmentState->srcAlphaBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
449 attachmentState->dstAlphaBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
450 attachmentState->alphaBlendOp = blend_equation_to_vk_blend_op(equation);
451 }
egdaniel3d5d9ac2016-03-01 12:56:15 -0800452
453 if (!blendInfo.fWriteColor) {
454 attachmentState->colorWriteMask = 0;
455 } else {
456 attachmentState->colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
457 VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
458 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500459
460 memset(colorBlendInfo, 0, sizeof(VkPipelineColorBlendStateCreateInfo));
461 colorBlendInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
462 colorBlendInfo->pNext = nullptr;
463 colorBlendInfo->flags = 0;
464 colorBlendInfo->logicOpEnable = VK_FALSE;
465 colorBlendInfo->attachmentCount = 1;
466 colorBlendInfo->pAttachments = attachmentState;
egdaniel470d77a2016-03-18 12:50:27 -0700467 // colorBlendInfo->blendConstants is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500468}
469
egdanielec440992016-09-13 09:54:11 -0700470static void setup_raster_state(const GrPipeline& pipeline,
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400471 const GrCaps* caps,
egdanielec440992016-09-13 09:54:11 -0700472 VkPipelineRasterizationStateCreateInfo* rasterInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500473 memset(rasterInfo, 0, sizeof(VkPipelineRasterizationStateCreateInfo));
474 rasterInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
475 rasterInfo->pNext = nullptr;
476 rasterInfo->flags = 0;
477 rasterInfo->depthClampEnable = VK_FALSE;
478 rasterInfo->rasterizerDiscardEnable = VK_FALSE;
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400479 rasterInfo->polygonMode = caps->wireframeMode() ? VK_POLYGON_MODE_LINE
480 : VK_POLYGON_MODE_FILL;
Brian Salomonf0861672017-05-08 11:10:10 -0400481 rasterInfo->cullMode = VK_CULL_MODE_NONE;
Chris Daltonb91c4662018-08-01 10:46:22 -0600482 rasterInfo->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Greg Daniel164a9f02016-02-22 09:56:40 -0500483 rasterInfo->depthBiasEnable = VK_FALSE;
484 rasterInfo->depthBiasConstantFactor = 0.0f;
485 rasterInfo->depthBiasClamp = 0.0f;
486 rasterInfo->depthBiasSlopeFactor = 0.0f;
487 rasterInfo->lineWidth = 1.0f;
488}
489
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000490static void setup_dynamic_state(VkPipelineDynamicStateCreateInfo* dynamicInfo,
491 VkDynamicState* dynamicStates) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500492 memset(dynamicInfo, 0, sizeof(VkPipelineDynamicStateCreateInfo));
493 dynamicInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
egdaniel470d77a2016-03-18 12:50:27 -0700494 dynamicInfo->pNext = VK_NULL_HANDLE;
495 dynamicInfo->flags = 0;
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000496 dynamicStates[0] = VK_DYNAMIC_STATE_VIEWPORT;
497 dynamicStates[1] = VK_DYNAMIC_STATE_SCISSOR;
498 dynamicStates[2] = VK_DYNAMIC_STATE_BLEND_CONSTANTS;
499 dynamicInfo->dynamicStateCount = 3;
500 dynamicInfo->pDynamicStates = dynamicStates;
Greg Daniel164a9f02016-02-22 09:56:40 -0500501}
502
Chris Dalton71713f62019-04-18 12:41:03 -0600503GrVkPipeline* GrVkPipeline::Create(
504 GrVkGpu* gpu, int numColorSamples, const GrPrimitiveProcessor& primProc,
505 const GrPipeline& pipeline, const GrStencilSettings& stencil, GrSurfaceOrigin origin,
506 VkPipelineShaderStageCreateInfo* shaderStageInfo, int shaderStageCount,
507 GrPrimitiveType primitiveType, VkRenderPass compatibleRenderPass, VkPipelineLayout layout,
508 VkPipelineCache cache) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500509 VkPipelineVertexInputStateCreateInfo vertexInputInfo;
Chris Dalton1d616352017-05-31 12:51:23 -0600510 SkSTArray<2, VkVertexInputBindingDescription, true> bindingDescs;
egdanielb05df0f2016-06-27 07:15:20 -0700511 SkSTArray<16, VkVertexInputAttributeDescription> attributeDesc;
Brian Salomon92be2f72018-06-19 14:33:47 -0400512 int totalAttributeCnt = primProc.numVertexAttributes() + primProc.numInstanceAttributes();
513 SkASSERT(totalAttributeCnt <= gpu->vkCaps().maxVertexAttributes());
514 VkVertexInputAttributeDescription* pAttribs = attributeDesc.push_back_n(totalAttributeCnt);
Chris Dalton1d616352017-05-31 12:51:23 -0600515 setup_vertex_input_state(primProc, &vertexInputInfo, &bindingDescs, pAttribs);
Greg Daniel164a9f02016-02-22 09:56:40 -0500516
517 VkPipelineInputAssemblyStateCreateInfo inputAssemblyInfo;
518 setup_input_assembly_state(primitiveType, &inputAssemblyInfo);
519
520 VkPipelineDepthStencilStateCreateInfo depthStencilInfo;
Chris Dalton71713f62019-04-18 12:41:03 -0600521 setup_depth_stencil_state(stencil, origin, &depthStencilInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500522
Greg Daniel164a9f02016-02-22 09:56:40 -0500523 VkPipelineViewportStateCreateInfo viewportInfo;
egdanielec440992016-09-13 09:54:11 -0700524 setup_viewport_scissor_state(&viewportInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500525
526 VkPipelineMultisampleStateCreateInfo multisampleInfo;
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500527 setup_multisample_state(numColorSamples, primProc, pipeline, gpu->caps(), &multisampleInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500528
529 // We will only have one color attachment per pipeline.
530 VkPipelineColorBlendAttachmentState attachmentStates[1];
531 VkPipelineColorBlendStateCreateInfo colorBlendInfo;
egdanielec440992016-09-13 09:54:11 -0700532 setup_color_blend_state(pipeline, &colorBlendInfo, attachmentStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500533
534 VkPipelineRasterizationStateCreateInfo rasterInfo;
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400535 setup_raster_state(pipeline, gpu->caps(), &rasterInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500536
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000537 VkDynamicState dynamicStates[3];
Greg Daniel164a9f02016-02-22 09:56:40 -0500538 VkPipelineDynamicStateCreateInfo dynamicInfo;
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000539 setup_dynamic_state(&dynamicInfo, dynamicStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500540
541 VkGraphicsPipelineCreateInfo pipelineCreateInfo;
542 memset(&pipelineCreateInfo, 0, sizeof(VkGraphicsPipelineCreateInfo));
543 pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
544 pipelineCreateInfo.pNext = nullptr;
545 pipelineCreateInfo.flags = 0;
546 pipelineCreateInfo.stageCount = shaderStageCount;
547 pipelineCreateInfo.pStages = shaderStageInfo;
548 pipelineCreateInfo.pVertexInputState = &vertexInputInfo;
549 pipelineCreateInfo.pInputAssemblyState = &inputAssemblyInfo;
550 pipelineCreateInfo.pTessellationState = nullptr;
551 pipelineCreateInfo.pViewportState = &viewportInfo;
552 pipelineCreateInfo.pRasterizationState = &rasterInfo;
553 pipelineCreateInfo.pMultisampleState = &multisampleInfo;
554 pipelineCreateInfo.pDepthStencilState = &depthStencilInfo;
555 pipelineCreateInfo.pColorBlendState = &colorBlendInfo;
556 pipelineCreateInfo.pDynamicState = &dynamicInfo;
557 pipelineCreateInfo.layout = layout;
Greg Daniel99b88e02018-10-03 15:31:20 -0400558 pipelineCreateInfo.renderPass = compatibleRenderPass;
Greg Daniel164a9f02016-02-22 09:56:40 -0500559 pipelineCreateInfo.subpass = 0;
560 pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
561 pipelineCreateInfo.basePipelineIndex = -1;
562
563 VkPipeline vkPipeline;
Ben Wagner6c30e742019-02-06 10:46:14 -0500564 VkResult err;
565 {
566#if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS)
567 // skia:8712
568 __lsan::ScopedDisabler lsanDisabler;
569#endif
570 err = GR_VK_CALL(gpu->vkInterface(), CreateGraphicsPipelines(gpu->device(),
571 cache, 1,
572 &pipelineCreateInfo,
573 nullptr, &vkPipeline));
574 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500575 if (err) {
Greg Daniel5ba448c2018-02-26 13:30:47 -0500576 SkDebugf("Failed to create pipeline. Error: %d\n", err);
Greg Daniel164a9f02016-02-22 09:56:40 -0500577 return nullptr;
578 }
579
580 return new GrVkPipeline(vkPipeline);
581}
582
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500583void GrVkPipeline::freeGPUData(GrVkGpu* gpu) const {
Greg Daniel164a9f02016-02-22 09:56:40 -0500584 GR_VK_CALL(gpu->vkInterface(), DestroyPipeline(gpu->device(), fPipeline, nullptr));
585}
586
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000587void GrVkPipeline::SetDynamicScissorRectState(GrVkGpu* gpu,
588 GrVkCommandBuffer* cmdBuffer,
589 const GrRenderTarget* renderTarget,
590 GrSurfaceOrigin rtOrigin,
591 SkIRect scissorRect) {
592 if (!scissorRect.intersect(SkIRect::MakeWH(renderTarget->width(), renderTarget->height()))) {
593 scissorRect.setEmpty();
594 }
595
596 VkRect2D scissor;
597 scissor.offset.x = scissorRect.fLeft;
598 scissor.extent.width = scissorRect.width();
599 if (kTopLeft_GrSurfaceOrigin == rtOrigin) {
600 scissor.offset.y = scissorRect.fTop;
601 } else {
602 SkASSERT(kBottomLeft_GrSurfaceOrigin == rtOrigin);
603 scissor.offset.y = renderTarget->height() - scissorRect.fBottom;
604 }
605 scissor.extent.height = scissorRect.height();
606
607 SkASSERT(scissor.offset.x >= 0);
608 SkASSERT(scissor.offset.y >= 0);
609 cmdBuffer->setScissor(gpu, 0, 1, &scissor);
610}
611
Chris Dalton46983b72017-06-06 12:27:16 -0600612void GrVkPipeline::SetDynamicViewportState(GrVkGpu* gpu,
613 GrVkCommandBuffer* cmdBuffer,
614 const GrRenderTarget* renderTarget) {
egdaniel470d77a2016-03-18 12:50:27 -0700615 // We always use one viewport the size of the RT
616 VkViewport viewport;
617 viewport.x = 0.0f;
618 viewport.y = 0.0f;
Chris Dalton46983b72017-06-06 12:27:16 -0600619 viewport.width = SkIntToScalar(renderTarget->width());
620 viewport.height = SkIntToScalar(renderTarget->height());
egdaniel470d77a2016-03-18 12:50:27 -0700621 viewport.minDepth = 0.0f;
622 viewport.maxDepth = 1.0f;
623 cmdBuffer->setViewport(gpu, 0, 1, &viewport);
624}
625
Chris Dalton46983b72017-06-06 12:27:16 -0600626void GrVkPipeline::SetDynamicBlendConstantState(GrVkGpu* gpu,
627 GrVkCommandBuffer* cmdBuffer,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400628 const GrSwizzle& swizzle,
Chris Dalton46983b72017-06-06 12:27:16 -0600629 const GrXferProcessor& xferProcessor) {
Chris Daltonbbb3f642019-07-24 12:25:08 -0400630 const GrXferProcessor::BlendInfo& blendInfo = xferProcessor.getBlendInfo();
egdaniel470d77a2016-03-18 12:50:27 -0700631 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
632 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
633 float floatColors[4];
634 if (blend_coeff_refs_constant(srcCoeff) || blend_coeff_refs_constant(dstCoeff)) {
Greg Daniel6c9f1012017-05-11 09:20:59 -0400635 // Swizzle the blend to match what the shader will output.
Brian Osman422f95b2018-11-05 16:49:04 -0500636 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
637 floatColors[0] = blendConst.fR;
638 floatColors[1] = blendConst.fG;
639 floatColors[2] = blendConst.fB;
640 floatColors[3] = blendConst.fA;
egdaniel470d77a2016-03-18 12:50:27 -0700641 } else {
642 memset(floatColors, 0, 4 * sizeof(float));
643 }
644 cmdBuffer->setBlendConstants(gpu, floatColors);
645}