blob: 385cc8b48bd634275cbe84c8a30d57d9230231ea [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
8#include "GrVkPipeline.h"
Greg Daniel164a9f02016-02-22 09:56:40 -05009#include "GrGeometryProcessor.h"
10#include "GrPipeline.h"
Brian Salomon1471df92018-06-08 10:49:00 -040011#include "GrStencilSettings.h"
egdaniel470d77a2016-03-18 12:50:27 -070012#include "GrVkCommandBuffer.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050013#include "GrVkGpu.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050014#include "GrVkRenderTarget.h"
15#include "GrVkUtil.h"
16
csmartdaltonb37cb232017-02-08 14:56:27 -050017static inline VkFormat attrib_type_to_vkformat(GrVertexAttribType type) {
18 switch (type) {
19 case kFloat_GrVertexAttribType:
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040020 case kHalf_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050021 return VK_FORMAT_R32_SFLOAT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040022 case kFloat2_GrVertexAttribType:
23 case kHalf2_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050024 return VK_FORMAT_R32G32_SFLOAT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040025 case kFloat3_GrVertexAttribType:
26 case kHalf3_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050027 return VK_FORMAT_R32G32B32_SFLOAT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040028 case kFloat4_GrVertexAttribType:
29 case kHalf4_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050030 return VK_FORMAT_R32G32B32A32_SFLOAT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040031 case kInt2_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050032 return VK_FORMAT_R32G32_SINT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040033 case kInt3_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050034 return VK_FORMAT_R32G32B32_SINT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040035 case kInt4_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050036 return VK_FORMAT_R32G32B32A32_SINT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040037 case kUByte_norm_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050038 return VK_FORMAT_R8_UNORM;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040039 case kUByte4_norm_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050040 return VK_FORMAT_R8G8B8A8_UNORM;
Chris Daltona045eea2017-10-24 13:22:10 -060041 case kShort2_GrVertexAttribType:
42 return VK_FORMAT_R16G16_SINT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040043 case kUShort2_GrVertexAttribType:
Robert Phillips8296e752017-08-25 08:45:21 -040044 return VK_FORMAT_R16G16_UINT;
Chris Daltona045eea2017-10-24 13:22:10 -060045 case kUShort2_norm_GrVertexAttribType:
46 return VK_FORMAT_R16G16_UNORM;
csmartdaltonb37cb232017-02-08 14:56:27 -050047 case kInt_GrVertexAttribType:
48 return VK_FORMAT_R32_SINT;
49 case kUint_GrVertexAttribType:
50 return VK_FORMAT_R32_UINT;
51 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040052 SK_ABORT("Unknown vertex attrib type");
csmartdaltonb37cb232017-02-08 14:56:27 -050053 return VK_FORMAT_UNDEFINED;
Greg Daniel164a9f02016-02-22 09:56:40 -050054}
55
56static void setup_vertex_input_state(const GrPrimitiveProcessor& primProc,
Chris Dalton1d616352017-05-31 12:51:23 -060057 VkPipelineVertexInputStateCreateInfo* vertexInputInfo,
58 SkSTArray<2, VkVertexInputBindingDescription, true>* bindingDescs,
59 VkVertexInputAttributeDescription* attributeDesc) {
Kevin Lubick42846132018-01-05 10:11:11 -050060 uint32_t vertexBinding = 0, instanceBinding = 0;
Chris Dalton1d616352017-05-31 12:51:23 -060061
Brian Salomon92be2f72018-06-19 14:33:47 -040062 int nextBinding = bindingDescs->count();
63 if (primProc.hasVertexAttributes()) {
64 vertexBinding = nextBinding++;
Chris Dalton1d616352017-05-31 12:51:23 -060065 }
66
Brian Salomon92be2f72018-06-19 14:33:47 -040067 if (primProc.hasInstanceAttributes()) {
68 instanceBinding = nextBinding;
Chris Dalton1d616352017-05-31 12:51:23 -060069 }
Greg Daniel164a9f02016-02-22 09:56:40 -050070
71 // setup attribute descriptions
Brian Salomon92be2f72018-06-19 14:33:47 -040072 int vaCount = primProc.numVertexAttributes();
73 int attribIndex = 0;
74 size_t vertexAttributeOffset = 0;
75 for (; attribIndex < vaCount; attribIndex++) {
76 const GrGeometryProcessor::Attribute& attrib = primProc.vertexAttribute(attribIndex);
77 VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex];
78 vkAttrib.location = attribIndex; // for now assume location = attribIndex
79 vkAttrib.binding = vertexBinding;
80 vkAttrib.format = attrib_type_to_vkformat(attrib.type());
81 vkAttrib.offset = vertexAttributeOffset;
82 SkASSERT(vkAttrib.offset == primProc.debugOnly_vertexAttributeOffset(attribIndex));
83 vertexAttributeOffset += attrib.sizeAlign4();
84 }
85 SkASSERT(vertexAttributeOffset == primProc.debugOnly_vertexStride());
86
87 int iaCount = primProc.numInstanceAttributes();
88 size_t instanceAttributeOffset = 0;
89 for (int iaIndex = 0; iaIndex < iaCount; ++iaIndex, ++attribIndex) {
90 const GrGeometryProcessor::Attribute& attrib = primProc.instanceAttribute(iaIndex);
91 VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex];
92 vkAttrib.location = attribIndex; // for now assume location = attribIndex
93 vkAttrib.binding = instanceBinding;
94 vkAttrib.format = attrib_type_to_vkformat(attrib.type());
95 vkAttrib.offset = instanceAttributeOffset;
96 SkASSERT(vkAttrib.offset == primProc.debugOnly_instanceAttributeOffset(iaIndex));
97 instanceAttributeOffset += attrib.sizeAlign4();
98 }
99 SkASSERT(instanceAttributeOffset == primProc.debugOnly_instanceStride());
100
101 if (primProc.hasVertexAttributes()) {
102 bindingDescs->push_back() = {
103 vertexBinding,
104 (uint32_t) vertexAttributeOffset,
105 VK_VERTEX_INPUT_RATE_VERTEX
106 };
107 }
108 if (primProc.hasInstanceAttributes()) {
109 bindingDescs->push_back() = {
110 instanceBinding,
111 (uint32_t) instanceAttributeOffset,
112 VK_VERTEX_INPUT_RATE_INSTANCE
113 };
Greg Daniel164a9f02016-02-22 09:56:40 -0500114 }
115
116 memset(vertexInputInfo, 0, sizeof(VkPipelineVertexInputStateCreateInfo));
117 vertexInputInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
118 vertexInputInfo->pNext = nullptr;
119 vertexInputInfo->flags = 0;
Chris Dalton1d616352017-05-31 12:51:23 -0600120 vertexInputInfo->vertexBindingDescriptionCount = bindingDescs->count();
121 vertexInputInfo->pVertexBindingDescriptions = bindingDescs->begin();
Brian Salomon92be2f72018-06-19 14:33:47 -0400122 vertexInputInfo->vertexAttributeDescriptionCount = vaCount + iaCount;
Greg Daniel164a9f02016-02-22 09:56:40 -0500123 vertexInputInfo->pVertexAttributeDescriptions = attributeDesc;
124}
125
Chris Dalton3809bab2017-06-13 10:55:06 -0600126static VkPrimitiveTopology gr_primitive_type_to_vk_topology(GrPrimitiveType primitiveType) {
127 switch (primitiveType) {
128 case GrPrimitiveType::kTriangles:
129 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
130 case GrPrimitiveType::kTriangleStrip:
131 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -0600132 case GrPrimitiveType::kPoints:
133 return VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
134 case GrPrimitiveType::kLines:
135 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
136 case GrPrimitiveType::kLineStrip:
137 return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
138 case GrPrimitiveType::kLinesAdjacency:
139 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY;
140 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400141 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -0600142 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
143}
Greg Daniel164a9f02016-02-22 09:56:40 -0500144
145static void setup_input_assembly_state(GrPrimitiveType primitiveType,
146 VkPipelineInputAssemblyStateCreateInfo* inputAssemblyInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500147 memset(inputAssemblyInfo, 0, sizeof(VkPipelineInputAssemblyStateCreateInfo));
148 inputAssemblyInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
149 inputAssemblyInfo->pNext = nullptr;
150 inputAssemblyInfo->flags = 0;
151 inputAssemblyInfo->primitiveRestartEnable = false;
Chris Dalton3809bab2017-06-13 10:55:06 -0600152 inputAssemblyInfo->topology = gr_primitive_type_to_vk_topology(primitiveType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500153}
154
155
egdanielec440992016-09-13 09:54:11 -0700156static VkStencilOp stencil_op_to_vk_stencil_op(GrStencilOp op) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500157 static const VkStencilOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700158 VK_STENCIL_OP_KEEP, // kKeep
159 VK_STENCIL_OP_ZERO, // kZero
160 VK_STENCIL_OP_REPLACE, // kReplace
161 VK_STENCIL_OP_INVERT, // kInvert
162 VK_STENCIL_OP_INCREMENT_AND_WRAP, // kIncWrap
163 VK_STENCIL_OP_DECREMENT_AND_WRAP, // kDecWrap
164 VK_STENCIL_OP_INCREMENT_AND_CLAMP, // kIncClamp
165 VK_STENCIL_OP_DECREMENT_AND_CLAMP, // kDecClamp
Greg Daniel164a9f02016-02-22 09:56:40 -0500166 };
cdalton93a379b2016-05-11 13:58:08 -0700167 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilOpCount);
168 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
169 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
170 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
171 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
172 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
173 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
174 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
175 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
176 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
177 return gTable[(int)op];
Greg Daniel164a9f02016-02-22 09:56:40 -0500178}
179
egdanielec440992016-09-13 09:54:11 -0700180static VkCompareOp stencil_func_to_vk_compare_op(GrStencilTest test) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500181 static const VkCompareOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700182 VK_COMPARE_OP_ALWAYS, // kAlways
183 VK_COMPARE_OP_NEVER, // kNever
184 VK_COMPARE_OP_GREATER, // kGreater
185 VK_COMPARE_OP_GREATER_OR_EQUAL, // kGEqual
186 VK_COMPARE_OP_LESS, // kLess
187 VK_COMPARE_OP_LESS_OR_EQUAL, // kLEqual
188 VK_COMPARE_OP_EQUAL, // kEqual
189 VK_COMPARE_OP_NOT_EQUAL, // kNotEqual
Greg Daniel164a9f02016-02-22 09:56:40 -0500190 };
cdalton93a379b2016-05-11 13:58:08 -0700191 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilTestCount);
192 GR_STATIC_ASSERT(0 == (int)GrStencilTest::kAlways);
193 GR_STATIC_ASSERT(1 == (int)GrStencilTest::kNever);
194 GR_STATIC_ASSERT(2 == (int)GrStencilTest::kGreater);
195 GR_STATIC_ASSERT(3 == (int)GrStencilTest::kGEqual);
196 GR_STATIC_ASSERT(4 == (int)GrStencilTest::kLess);
197 GR_STATIC_ASSERT(5 == (int)GrStencilTest::kLEqual);
198 GR_STATIC_ASSERT(6 == (int)GrStencilTest::kEqual);
199 GR_STATIC_ASSERT(7 == (int)GrStencilTest::kNotEqual);
200 SkASSERT(test < (GrStencilTest)kGrStencilTestCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500201
cdalton93a379b2016-05-11 13:58:08 -0700202 return gTable[(int)test];
Greg Daniel164a9f02016-02-22 09:56:40 -0500203}
204
egdanielec440992016-09-13 09:54:11 -0700205static void setup_depth_stencil_state(const GrStencilSettings& stencilSettings,
206 VkPipelineDepthStencilStateCreateInfo* stencilInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500207 memset(stencilInfo, 0, sizeof(VkPipelineDepthStencilStateCreateInfo));
208 stencilInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
209 stencilInfo->pNext = nullptr;
210 stencilInfo->flags = 0;
211 // set depth testing defaults
212 stencilInfo->depthTestEnable = VK_FALSE;
213 stencilInfo->depthWriteEnable = VK_FALSE;
214 stencilInfo->depthCompareOp = VK_COMPARE_OP_ALWAYS;
215 stencilInfo->depthBoundsTestEnable = VK_FALSE;
216 stencilInfo->stencilTestEnable = !stencilSettings.isDisabled();
217 if (!stencilSettings.isDisabled()) {
218 // Set front face
cdalton93a379b2016-05-11 13:58:08 -0700219 const GrStencilSettings::Face& front = stencilSettings.front();
220 stencilInfo->front.failOp = stencil_op_to_vk_stencil_op(front.fFailOp);
221 stencilInfo->front.passOp = stencil_op_to_vk_stencil_op(front.fPassOp);
Greg Daniel164a9f02016-02-22 09:56:40 -0500222 stencilInfo->front.depthFailOp = stencilInfo->front.failOp;
cdalton93a379b2016-05-11 13:58:08 -0700223 stencilInfo->front.compareOp = stencil_func_to_vk_compare_op(front.fTest);
224 stencilInfo->front.compareMask = front.fTestMask;
225 stencilInfo->front.writeMask = front.fWriteMask;
226 stencilInfo->front.reference = front.fRef;
Greg Daniel164a9f02016-02-22 09:56:40 -0500227
228 // Set back face
cdalton93a379b2016-05-11 13:58:08 -0700229 if (!stencilSettings.isTwoSided()) {
230 stencilInfo->back = stencilInfo->front;
231 } else {
232 const GrStencilSettings::Face& back = stencilSettings.back();
233 stencilInfo->back.failOp = stencil_op_to_vk_stencil_op(back.fFailOp);
234 stencilInfo->back.passOp = stencil_op_to_vk_stencil_op(back.fPassOp);
235 stencilInfo->back.depthFailOp = stencilInfo->front.failOp;
236 stencilInfo->back.compareOp = stencil_func_to_vk_compare_op(back.fTest);
237 stencilInfo->back.compareMask = back.fTestMask;
238 stencilInfo->back.writeMask = back.fWriteMask;
239 stencilInfo->back.reference = back.fRef;
240 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500241 }
242 stencilInfo->minDepthBounds = 0.0f;
243 stencilInfo->maxDepthBounds = 1.0f;
244}
245
egdanielec440992016-09-13 09:54:11 -0700246static void setup_viewport_scissor_state(VkPipelineViewportStateCreateInfo* viewportInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500247 memset(viewportInfo, 0, sizeof(VkPipelineViewportStateCreateInfo));
248 viewportInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
249 viewportInfo->pNext = nullptr;
250 viewportInfo->flags = 0;
251
Greg Daniel164a9f02016-02-22 09:56:40 -0500252 viewportInfo->viewportCount = 1;
egdaniel470d77a2016-03-18 12:50:27 -0700253 viewportInfo->pViewports = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500254
egdaniel470d77a2016-03-18 12:50:27 -0700255 viewportInfo->scissorCount = 1;
256 viewportInfo->pScissors = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500257
Greg Daniel164a9f02016-02-22 09:56:40 -0500258 SkASSERT(viewportInfo->viewportCount == viewportInfo->scissorCount);
259}
260
Brian Salomonff168d92018-06-23 15:17:27 -0400261static void setup_multisample_state(const GrPrimitiveProcessor& primProc,
262 const GrPipeline& pipeline,
egdanielec440992016-09-13 09:54:11 -0700263 const GrCaps* caps,
264 VkPipelineMultisampleStateCreateInfo* multisampleInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500265 memset(multisampleInfo, 0, sizeof(VkPipelineMultisampleStateCreateInfo));
266 multisampleInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
267 multisampleInfo->pNext = nullptr;
268 multisampleInfo->flags = 0;
Robert Phillips2890fbf2017-07-26 15:48:41 -0400269 int numSamples = pipeline.proxy()->numColorSamples();
Greg Daniel164a9f02016-02-22 09:56:40 -0500270 SkAssertResult(GrSampleCountToVkSampleCount(numSamples,
271 &multisampleInfo->rasterizationSamples));
ethannicholas28ef4452016-03-25 09:26:03 -0700272 float sampleShading = primProc.getSampleShading();
273 SkASSERT(sampleShading == 0.0f || caps->sampleShadingSupport());
274 multisampleInfo->sampleShadingEnable = sampleShading > 0.0f;
275 multisampleInfo->minSampleShading = sampleShading;
Greg Daniel164a9f02016-02-22 09:56:40 -0500276 multisampleInfo->pSampleMask = nullptr;
277 multisampleInfo->alphaToCoverageEnable = VK_FALSE;
278 multisampleInfo->alphaToOneEnable = VK_FALSE;
279}
280
281static VkBlendFactor blend_coeff_to_vk_blend(GrBlendCoeff coeff) {
282 static const VkBlendFactor gTable[] = {
283 VK_BLEND_FACTOR_ZERO, // kZero_GrBlendCoeff
284 VK_BLEND_FACTOR_ONE, // kOne_GrBlendCoeff
285 VK_BLEND_FACTOR_SRC_COLOR, // kSC_GrBlendCoeff
286 VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR, // kISC_GrBlendCoeff
287 VK_BLEND_FACTOR_DST_COLOR, // kDC_GrBlendCoeff
288 VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR, // kIDC_GrBlendCoeff
289 VK_BLEND_FACTOR_SRC_ALPHA, // kSA_GrBlendCoeff
290 VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, // kISA_GrBlendCoeff
291 VK_BLEND_FACTOR_DST_ALPHA, // kDA_GrBlendCoeff
292 VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA, // kIDA_GrBlendCoeff
293 VK_BLEND_FACTOR_CONSTANT_COLOR, // kConstC_GrBlendCoeff
294 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR, // kIConstC_GrBlendCoeff
295 VK_BLEND_FACTOR_CONSTANT_ALPHA, // kConstA_GrBlendCoeff
296 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA, // kIConstA_GrBlendCoeff
297 VK_BLEND_FACTOR_SRC1_COLOR, // kS2C_GrBlendCoeff
298 VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR, // kIS2C_GrBlendCoeff
299 VK_BLEND_FACTOR_SRC1_ALPHA, // kS2A_GrBlendCoeff
300 VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA, // kIS2A_GrBlendCoeff
301
302 };
303 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrBlendCoeffCnt);
304 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
305 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
306 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
307 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
308 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
309 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
310 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
311 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
312 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
313 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
314 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
315 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
316 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
317 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
318 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
319 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
320 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
321 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
322
323 SkASSERT((unsigned)coeff < kGrBlendCoeffCnt);
324 return gTable[coeff];
325}
326
327
328static VkBlendOp blend_equation_to_vk_blend_op(GrBlendEquation equation) {
329 static const VkBlendOp gTable[] = {
330 VK_BLEND_OP_ADD, // kAdd_GrBlendEquation
331 VK_BLEND_OP_SUBTRACT, // kSubtract_GrBlendEquation
332 VK_BLEND_OP_REVERSE_SUBTRACT, // kReverseSubtract_GrBlendEquation
333 };
334 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kFirstAdvancedGrBlendEquation);
335 GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
336 GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
337 GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
338
339 SkASSERT((unsigned)equation < kGrBlendCoeffCnt);
340 return gTable[equation];
341}
342
egdanielec440992016-09-13 09:54:11 -0700343static bool blend_coeff_refs_constant(GrBlendCoeff coeff) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500344 static const bool gCoeffReferencesBlendConst[] = {
345 false,
346 false,
347 false,
348 false,
349 false,
350 false,
351 false,
352 false,
353 false,
354 false,
355 true,
356 true,
357 true,
358 true,
359
360 // extended blend coeffs
361 false,
362 false,
363 false,
364 false,
365 };
366 return gCoeffReferencesBlendConst[coeff];
367 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
368 // Individual enum asserts already made in blend_coeff_to_vk_blend
369}
370
egdanielec440992016-09-13 09:54:11 -0700371static void setup_color_blend_state(const GrPipeline& pipeline,
372 VkPipelineColorBlendStateCreateInfo* colorBlendInfo,
373 VkPipelineColorBlendAttachmentState* attachmentState) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500374 GrXferProcessor::BlendInfo blendInfo;
375 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
376
377 GrBlendEquation equation = blendInfo.fEquation;
378 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
379 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
380 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
381 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
382
383 memset(attachmentState, 0, sizeof(VkPipelineColorBlendAttachmentState));
384 attachmentState->blendEnable = !blendOff;
385 if (!blendOff) {
386 attachmentState->srcColorBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
387 attachmentState->dstColorBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
388 attachmentState->colorBlendOp = blend_equation_to_vk_blend_op(equation);
389 attachmentState->srcAlphaBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
390 attachmentState->dstAlphaBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
391 attachmentState->alphaBlendOp = blend_equation_to_vk_blend_op(equation);
392 }
egdaniel3d5d9ac2016-03-01 12:56:15 -0800393
394 if (!blendInfo.fWriteColor) {
395 attachmentState->colorWriteMask = 0;
396 } else {
397 attachmentState->colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
398 VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
399 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500400
401 memset(colorBlendInfo, 0, sizeof(VkPipelineColorBlendStateCreateInfo));
402 colorBlendInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
403 colorBlendInfo->pNext = nullptr;
404 colorBlendInfo->flags = 0;
405 colorBlendInfo->logicOpEnable = VK_FALSE;
406 colorBlendInfo->attachmentCount = 1;
407 colorBlendInfo->pAttachments = attachmentState;
egdaniel470d77a2016-03-18 12:50:27 -0700408 // colorBlendInfo->blendConstants is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500409}
410
egdanielec440992016-09-13 09:54:11 -0700411static void setup_raster_state(const GrPipeline& pipeline,
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400412 const GrCaps* caps,
egdanielec440992016-09-13 09:54:11 -0700413 VkPipelineRasterizationStateCreateInfo* rasterInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500414 memset(rasterInfo, 0, sizeof(VkPipelineRasterizationStateCreateInfo));
415 rasterInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
416 rasterInfo->pNext = nullptr;
417 rasterInfo->flags = 0;
418 rasterInfo->depthClampEnable = VK_FALSE;
419 rasterInfo->rasterizerDiscardEnable = VK_FALSE;
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400420 rasterInfo->polygonMode = caps->wireframeMode() ? VK_POLYGON_MODE_LINE
421 : VK_POLYGON_MODE_FILL;
Brian Salomonf0861672017-05-08 11:10:10 -0400422 rasterInfo->cullMode = VK_CULL_MODE_NONE;
Greg Daniel164a9f02016-02-22 09:56:40 -0500423 rasterInfo->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
424 rasterInfo->depthBiasEnable = VK_FALSE;
425 rasterInfo->depthBiasConstantFactor = 0.0f;
426 rasterInfo->depthBiasClamp = 0.0f;
427 rasterInfo->depthBiasSlopeFactor = 0.0f;
428 rasterInfo->lineWidth = 1.0f;
429}
430
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000431static void setup_dynamic_state(VkPipelineDynamicStateCreateInfo* dynamicInfo,
432 VkDynamicState* dynamicStates) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500433 memset(dynamicInfo, 0, sizeof(VkPipelineDynamicStateCreateInfo));
434 dynamicInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
egdaniel470d77a2016-03-18 12:50:27 -0700435 dynamicInfo->pNext = VK_NULL_HANDLE;
436 dynamicInfo->flags = 0;
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000437 dynamicStates[0] = VK_DYNAMIC_STATE_VIEWPORT;
438 dynamicStates[1] = VK_DYNAMIC_STATE_SCISSOR;
439 dynamicStates[2] = VK_DYNAMIC_STATE_BLEND_CONSTANTS;
440 dynamicInfo->dynamicStateCount = 3;
441 dynamicInfo->pDynamicStates = dynamicStates;
Greg Daniel164a9f02016-02-22 09:56:40 -0500442}
443
Brian Salomonff168d92018-06-23 15:17:27 -0400444GrVkPipeline* GrVkPipeline::Create(GrVkGpu* gpu,
Greg Daniel164a9f02016-02-22 09:56:40 -0500445 const GrPrimitiveProcessor& primProc,
Brian Salomonff168d92018-06-23 15:17:27 -0400446 const GrPipeline& pipeline,
447 const GrStencilSettings& stencil,
Greg Daniel164a9f02016-02-22 09:56:40 -0500448 VkPipelineShaderStageCreateInfo* shaderStageInfo,
449 int shaderStageCount,
450 GrPrimitiveType primitiveType,
451 const GrVkRenderPass& renderPass,
jvanverth03509ea2016-03-02 13:19:47 -0800452 VkPipelineLayout layout,
453 VkPipelineCache cache) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500454 VkPipelineVertexInputStateCreateInfo vertexInputInfo;
Chris Dalton1d616352017-05-31 12:51:23 -0600455 SkSTArray<2, VkVertexInputBindingDescription, true> bindingDescs;
egdanielb05df0f2016-06-27 07:15:20 -0700456 SkSTArray<16, VkVertexInputAttributeDescription> attributeDesc;
Brian Salomon92be2f72018-06-19 14:33:47 -0400457 int totalAttributeCnt = primProc.numVertexAttributes() + primProc.numInstanceAttributes();
458 SkASSERT(totalAttributeCnt <= gpu->vkCaps().maxVertexAttributes());
459 VkVertexInputAttributeDescription* pAttribs = attributeDesc.push_back_n(totalAttributeCnt);
Chris Dalton1d616352017-05-31 12:51:23 -0600460 setup_vertex_input_state(primProc, &vertexInputInfo, &bindingDescs, pAttribs);
Greg Daniel164a9f02016-02-22 09:56:40 -0500461
462 VkPipelineInputAssemblyStateCreateInfo inputAssemblyInfo;
463 setup_input_assembly_state(primitiveType, &inputAssemblyInfo);
464
465 VkPipelineDepthStencilStateCreateInfo depthStencilInfo;
csmartdaltonc633abb2016-11-01 08:55:55 -0700466 setup_depth_stencil_state(stencil, &depthStencilInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500467
Greg Daniel164a9f02016-02-22 09:56:40 -0500468 VkPipelineViewportStateCreateInfo viewportInfo;
egdanielec440992016-09-13 09:54:11 -0700469 setup_viewport_scissor_state(&viewportInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500470
471 VkPipelineMultisampleStateCreateInfo multisampleInfo;
Brian Salomonff168d92018-06-23 15:17:27 -0400472 setup_multisample_state(primProc, pipeline, gpu->caps(), &multisampleInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500473
474 // We will only have one color attachment per pipeline.
475 VkPipelineColorBlendAttachmentState attachmentStates[1];
476 VkPipelineColorBlendStateCreateInfo colorBlendInfo;
egdanielec440992016-09-13 09:54:11 -0700477 setup_color_blend_state(pipeline, &colorBlendInfo, attachmentStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500478
479 VkPipelineRasterizationStateCreateInfo rasterInfo;
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400480 setup_raster_state(pipeline, gpu->caps(), &rasterInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500481
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000482 VkDynamicState dynamicStates[3];
Greg Daniel164a9f02016-02-22 09:56:40 -0500483 VkPipelineDynamicStateCreateInfo dynamicInfo;
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000484 setup_dynamic_state(&dynamicInfo, dynamicStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500485
486 VkGraphicsPipelineCreateInfo pipelineCreateInfo;
487 memset(&pipelineCreateInfo, 0, sizeof(VkGraphicsPipelineCreateInfo));
488 pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
489 pipelineCreateInfo.pNext = nullptr;
490 pipelineCreateInfo.flags = 0;
491 pipelineCreateInfo.stageCount = shaderStageCount;
492 pipelineCreateInfo.pStages = shaderStageInfo;
493 pipelineCreateInfo.pVertexInputState = &vertexInputInfo;
494 pipelineCreateInfo.pInputAssemblyState = &inputAssemblyInfo;
495 pipelineCreateInfo.pTessellationState = nullptr;
496 pipelineCreateInfo.pViewportState = &viewportInfo;
497 pipelineCreateInfo.pRasterizationState = &rasterInfo;
498 pipelineCreateInfo.pMultisampleState = &multisampleInfo;
499 pipelineCreateInfo.pDepthStencilState = &depthStencilInfo;
500 pipelineCreateInfo.pColorBlendState = &colorBlendInfo;
501 pipelineCreateInfo.pDynamicState = &dynamicInfo;
502 pipelineCreateInfo.layout = layout;
503 pipelineCreateInfo.renderPass = renderPass.vkRenderPass();
504 pipelineCreateInfo.subpass = 0;
505 pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
506 pipelineCreateInfo.basePipelineIndex = -1;
507
508 VkPipeline vkPipeline;
509 VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateGraphicsPipelines(gpu->device(),
halcanary9d524f22016-03-29 09:03:52 -0700510 cache, 1,
511 &pipelineCreateInfo,
Greg Daniel164a9f02016-02-22 09:56:40 -0500512 nullptr, &vkPipeline));
513 if (err) {
Greg Daniel5ba448c2018-02-26 13:30:47 -0500514 SkDebugf("Failed to create pipeline. Error: %d\n", err);
Greg Daniel164a9f02016-02-22 09:56:40 -0500515 return nullptr;
516 }
517
518 return new GrVkPipeline(vkPipeline);
519}
520
521void GrVkPipeline::freeGPUData(const GrVkGpu* gpu) const {
522 GR_VK_CALL(gpu->vkInterface(), DestroyPipeline(gpu->device(), fPipeline, nullptr));
523}
524
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000525void GrVkPipeline::SetDynamicScissorRectState(GrVkGpu* gpu,
526 GrVkCommandBuffer* cmdBuffer,
527 const GrRenderTarget* renderTarget,
528 GrSurfaceOrigin rtOrigin,
529 SkIRect scissorRect) {
530 if (!scissorRect.intersect(SkIRect::MakeWH(renderTarget->width(), renderTarget->height()))) {
531 scissorRect.setEmpty();
532 }
533
534 VkRect2D scissor;
535 scissor.offset.x = scissorRect.fLeft;
536 scissor.extent.width = scissorRect.width();
537 if (kTopLeft_GrSurfaceOrigin == rtOrigin) {
538 scissor.offset.y = scissorRect.fTop;
539 } else {
540 SkASSERT(kBottomLeft_GrSurfaceOrigin == rtOrigin);
541 scissor.offset.y = renderTarget->height() - scissorRect.fBottom;
542 }
543 scissor.extent.height = scissorRect.height();
544
545 SkASSERT(scissor.offset.x >= 0);
546 SkASSERT(scissor.offset.y >= 0);
547 cmdBuffer->setScissor(gpu, 0, 1, &scissor);
548}
549
Chris Dalton46983b72017-06-06 12:27:16 -0600550void GrVkPipeline::SetDynamicViewportState(GrVkGpu* gpu,
551 GrVkCommandBuffer* cmdBuffer,
552 const GrRenderTarget* renderTarget) {
egdaniel470d77a2016-03-18 12:50:27 -0700553 // We always use one viewport the size of the RT
554 VkViewport viewport;
555 viewport.x = 0.0f;
556 viewport.y = 0.0f;
Chris Dalton46983b72017-06-06 12:27:16 -0600557 viewport.width = SkIntToScalar(renderTarget->width());
558 viewport.height = SkIntToScalar(renderTarget->height());
egdaniel470d77a2016-03-18 12:50:27 -0700559 viewport.minDepth = 0.0f;
560 viewport.maxDepth = 1.0f;
561 cmdBuffer->setViewport(gpu, 0, 1, &viewport);
562}
563
Chris Dalton46983b72017-06-06 12:27:16 -0600564void GrVkPipeline::SetDynamicBlendConstantState(GrVkGpu* gpu,
565 GrVkCommandBuffer* cmdBuffer,
566 GrPixelConfig pixelConfig,
567 const GrXferProcessor& xferProcessor) {
egdaniel470d77a2016-03-18 12:50:27 -0700568 GrXferProcessor::BlendInfo blendInfo;
Chris Dalton46983b72017-06-06 12:27:16 -0600569 xferProcessor.getBlendInfo(&blendInfo);
egdaniel470d77a2016-03-18 12:50:27 -0700570 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
571 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
572 float floatColors[4];
573 if (blend_coeff_refs_constant(srcCoeff) || blend_coeff_refs_constant(dstCoeff)) {
Greg Daniel6c9f1012017-05-11 09:20:59 -0400574 // Swizzle the blend to match what the shader will output.
Chris Dalton46983b72017-06-06 12:27:16 -0600575 const GrSwizzle& swizzle = gpu->caps()->shaderCaps()->configOutputSwizzle(pixelConfig);
Greg Daniel6c9f1012017-05-11 09:20:59 -0400576 GrColor blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
577 GrColorToRGBAFloat(blendConst, floatColors);
egdaniel470d77a2016-03-18 12:50:27 -0700578 } else {
579 memset(floatColors, 0, 4 * sizeof(float));
580 }
581 cmdBuffer->setBlendConstants(gpu, floatColors);
582}