blob: 60e7f432599a97e9c276ea9e6eaacb9812306d86 [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;
Ruiqi Maob609e6d2018-07-17 10:19:38 -040037 case kByte_GrVertexAttribType:
38 return VK_FORMAT_R8_SINT;
39 case kByte2_GrVertexAttribType:
40 return VK_FORMAT_R8G8_SINT;
41 case kByte3_GrVertexAttribType:
42 return VK_FORMAT_R8G8B8_SINT;
43 case kByte4_GrVertexAttribType:
44 return VK_FORMAT_R8G8B8A8_SINT;
45 case kUByte_GrVertexAttribType:
46 return VK_FORMAT_R8_UINT;
47 case kUByte2_GrVertexAttribType:
48 return VK_FORMAT_R8G8_UINT;
49 case kUByte3_GrVertexAttribType:
50 return VK_FORMAT_R8G8B8_UINT;
51 case kUByte4_GrVertexAttribType:
52 return VK_FORMAT_R8G8B8A8_UINT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040053 case kUByte_norm_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050054 return VK_FORMAT_R8_UNORM;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040055 case kUByte4_norm_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050056 return VK_FORMAT_R8G8B8A8_UNORM;
Chris Daltona045eea2017-10-24 13:22:10 -060057 case kShort2_GrVertexAttribType:
58 return VK_FORMAT_R16G16_SINT;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040059 case kUShort2_GrVertexAttribType:
Robert Phillips8296e752017-08-25 08:45:21 -040060 return VK_FORMAT_R16G16_UINT;
Chris Daltona045eea2017-10-24 13:22:10 -060061 case kUShort2_norm_GrVertexAttribType:
62 return VK_FORMAT_R16G16_UNORM;
csmartdaltonb37cb232017-02-08 14:56:27 -050063 case kInt_GrVertexAttribType:
64 return VK_FORMAT_R32_SINT;
65 case kUint_GrVertexAttribType:
66 return VK_FORMAT_R32_UINT;
67 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040068 SK_ABORT("Unknown vertex attrib type");
csmartdaltonb37cb232017-02-08 14:56:27 -050069 return VK_FORMAT_UNDEFINED;
Greg Daniel164a9f02016-02-22 09:56:40 -050070}
71
72static void setup_vertex_input_state(const GrPrimitiveProcessor& primProc,
Chris Dalton1d616352017-05-31 12:51:23 -060073 VkPipelineVertexInputStateCreateInfo* vertexInputInfo,
74 SkSTArray<2, VkVertexInputBindingDescription, true>* bindingDescs,
75 VkVertexInputAttributeDescription* attributeDesc) {
Kevin Lubick42846132018-01-05 10:11:11 -050076 uint32_t vertexBinding = 0, instanceBinding = 0;
Chris Dalton1d616352017-05-31 12:51:23 -060077
Brian Salomon92be2f72018-06-19 14:33:47 -040078 int nextBinding = bindingDescs->count();
79 if (primProc.hasVertexAttributes()) {
80 vertexBinding = nextBinding++;
Chris Dalton1d616352017-05-31 12:51:23 -060081 }
82
Brian Salomon92be2f72018-06-19 14:33:47 -040083 if (primProc.hasInstanceAttributes()) {
84 instanceBinding = nextBinding;
Chris Dalton1d616352017-05-31 12:51:23 -060085 }
Greg Daniel164a9f02016-02-22 09:56:40 -050086
87 // setup attribute descriptions
Brian Salomon92be2f72018-06-19 14:33:47 -040088 int vaCount = primProc.numVertexAttributes();
89 int attribIndex = 0;
90 size_t vertexAttributeOffset = 0;
91 for (; attribIndex < vaCount; attribIndex++) {
92 const GrGeometryProcessor::Attribute& attrib = primProc.vertexAttribute(attribIndex);
93 VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex];
94 vkAttrib.location = attribIndex; // for now assume location = attribIndex
95 vkAttrib.binding = vertexBinding;
96 vkAttrib.format = attrib_type_to_vkformat(attrib.type());
97 vkAttrib.offset = vertexAttributeOffset;
98 SkASSERT(vkAttrib.offset == primProc.debugOnly_vertexAttributeOffset(attribIndex));
99 vertexAttributeOffset += attrib.sizeAlign4();
100 }
101 SkASSERT(vertexAttributeOffset == primProc.debugOnly_vertexStride());
102
103 int iaCount = primProc.numInstanceAttributes();
104 size_t instanceAttributeOffset = 0;
105 for (int iaIndex = 0; iaIndex < iaCount; ++iaIndex, ++attribIndex) {
106 const GrGeometryProcessor::Attribute& attrib = primProc.instanceAttribute(iaIndex);
107 VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex];
108 vkAttrib.location = attribIndex; // for now assume location = attribIndex
109 vkAttrib.binding = instanceBinding;
110 vkAttrib.format = attrib_type_to_vkformat(attrib.type());
111 vkAttrib.offset = instanceAttributeOffset;
112 SkASSERT(vkAttrib.offset == primProc.debugOnly_instanceAttributeOffset(iaIndex));
113 instanceAttributeOffset += attrib.sizeAlign4();
114 }
115 SkASSERT(instanceAttributeOffset == primProc.debugOnly_instanceStride());
116
117 if (primProc.hasVertexAttributes()) {
118 bindingDescs->push_back() = {
119 vertexBinding,
120 (uint32_t) vertexAttributeOffset,
121 VK_VERTEX_INPUT_RATE_VERTEX
122 };
123 }
124 if (primProc.hasInstanceAttributes()) {
125 bindingDescs->push_back() = {
126 instanceBinding,
127 (uint32_t) instanceAttributeOffset,
128 VK_VERTEX_INPUT_RATE_INSTANCE
129 };
Greg Daniel164a9f02016-02-22 09:56:40 -0500130 }
131
132 memset(vertexInputInfo, 0, sizeof(VkPipelineVertexInputStateCreateInfo));
133 vertexInputInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
134 vertexInputInfo->pNext = nullptr;
135 vertexInputInfo->flags = 0;
Chris Dalton1d616352017-05-31 12:51:23 -0600136 vertexInputInfo->vertexBindingDescriptionCount = bindingDescs->count();
137 vertexInputInfo->pVertexBindingDescriptions = bindingDescs->begin();
Brian Salomon92be2f72018-06-19 14:33:47 -0400138 vertexInputInfo->vertexAttributeDescriptionCount = vaCount + iaCount;
Greg Daniel164a9f02016-02-22 09:56:40 -0500139 vertexInputInfo->pVertexAttributeDescriptions = attributeDesc;
140}
141
Chris Dalton3809bab2017-06-13 10:55:06 -0600142static VkPrimitiveTopology gr_primitive_type_to_vk_topology(GrPrimitiveType primitiveType) {
143 switch (primitiveType) {
144 case GrPrimitiveType::kTriangles:
145 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
146 case GrPrimitiveType::kTriangleStrip:
147 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -0600148 case GrPrimitiveType::kPoints:
149 return VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
150 case GrPrimitiveType::kLines:
151 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
152 case GrPrimitiveType::kLineStrip:
153 return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
154 case GrPrimitiveType::kLinesAdjacency:
155 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY;
156 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400157 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -0600158 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
159}
Greg Daniel164a9f02016-02-22 09:56:40 -0500160
161static void setup_input_assembly_state(GrPrimitiveType primitiveType,
162 VkPipelineInputAssemblyStateCreateInfo* inputAssemblyInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500163 memset(inputAssemblyInfo, 0, sizeof(VkPipelineInputAssemblyStateCreateInfo));
164 inputAssemblyInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
165 inputAssemblyInfo->pNext = nullptr;
166 inputAssemblyInfo->flags = 0;
167 inputAssemblyInfo->primitiveRestartEnable = false;
Chris Dalton3809bab2017-06-13 10:55:06 -0600168 inputAssemblyInfo->topology = gr_primitive_type_to_vk_topology(primitiveType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500169}
170
171
egdanielec440992016-09-13 09:54:11 -0700172static VkStencilOp stencil_op_to_vk_stencil_op(GrStencilOp op) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500173 static const VkStencilOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700174 VK_STENCIL_OP_KEEP, // kKeep
175 VK_STENCIL_OP_ZERO, // kZero
176 VK_STENCIL_OP_REPLACE, // kReplace
177 VK_STENCIL_OP_INVERT, // kInvert
178 VK_STENCIL_OP_INCREMENT_AND_WRAP, // kIncWrap
179 VK_STENCIL_OP_DECREMENT_AND_WRAP, // kDecWrap
180 VK_STENCIL_OP_INCREMENT_AND_CLAMP, // kIncClamp
181 VK_STENCIL_OP_DECREMENT_AND_CLAMP, // kDecClamp
Greg Daniel164a9f02016-02-22 09:56:40 -0500182 };
cdalton93a379b2016-05-11 13:58:08 -0700183 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilOpCount);
184 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
185 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
186 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
187 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
188 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
189 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
190 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
191 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
192 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
193 return gTable[(int)op];
Greg Daniel164a9f02016-02-22 09:56:40 -0500194}
195
egdanielec440992016-09-13 09:54:11 -0700196static VkCompareOp stencil_func_to_vk_compare_op(GrStencilTest test) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500197 static const VkCompareOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700198 VK_COMPARE_OP_ALWAYS, // kAlways
199 VK_COMPARE_OP_NEVER, // kNever
200 VK_COMPARE_OP_GREATER, // kGreater
201 VK_COMPARE_OP_GREATER_OR_EQUAL, // kGEqual
202 VK_COMPARE_OP_LESS, // kLess
203 VK_COMPARE_OP_LESS_OR_EQUAL, // kLEqual
204 VK_COMPARE_OP_EQUAL, // kEqual
205 VK_COMPARE_OP_NOT_EQUAL, // kNotEqual
Greg Daniel164a9f02016-02-22 09:56:40 -0500206 };
cdalton93a379b2016-05-11 13:58:08 -0700207 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilTestCount);
208 GR_STATIC_ASSERT(0 == (int)GrStencilTest::kAlways);
209 GR_STATIC_ASSERT(1 == (int)GrStencilTest::kNever);
210 GR_STATIC_ASSERT(2 == (int)GrStencilTest::kGreater);
211 GR_STATIC_ASSERT(3 == (int)GrStencilTest::kGEqual);
212 GR_STATIC_ASSERT(4 == (int)GrStencilTest::kLess);
213 GR_STATIC_ASSERT(5 == (int)GrStencilTest::kLEqual);
214 GR_STATIC_ASSERT(6 == (int)GrStencilTest::kEqual);
215 GR_STATIC_ASSERT(7 == (int)GrStencilTest::kNotEqual);
216 SkASSERT(test < (GrStencilTest)kGrStencilTestCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500217
cdalton93a379b2016-05-11 13:58:08 -0700218 return gTable[(int)test];
Greg Daniel164a9f02016-02-22 09:56:40 -0500219}
220
egdanielec440992016-09-13 09:54:11 -0700221static void setup_depth_stencil_state(const GrStencilSettings& stencilSettings,
222 VkPipelineDepthStencilStateCreateInfo* stencilInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500223 memset(stencilInfo, 0, sizeof(VkPipelineDepthStencilStateCreateInfo));
224 stencilInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
225 stencilInfo->pNext = nullptr;
226 stencilInfo->flags = 0;
227 // set depth testing defaults
228 stencilInfo->depthTestEnable = VK_FALSE;
229 stencilInfo->depthWriteEnable = VK_FALSE;
230 stencilInfo->depthCompareOp = VK_COMPARE_OP_ALWAYS;
231 stencilInfo->depthBoundsTestEnable = VK_FALSE;
232 stencilInfo->stencilTestEnable = !stencilSettings.isDisabled();
233 if (!stencilSettings.isDisabled()) {
234 // Set front face
cdalton93a379b2016-05-11 13:58:08 -0700235 const GrStencilSettings::Face& front = stencilSettings.front();
236 stencilInfo->front.failOp = stencil_op_to_vk_stencil_op(front.fFailOp);
237 stencilInfo->front.passOp = stencil_op_to_vk_stencil_op(front.fPassOp);
Greg Daniel164a9f02016-02-22 09:56:40 -0500238 stencilInfo->front.depthFailOp = stencilInfo->front.failOp;
cdalton93a379b2016-05-11 13:58:08 -0700239 stencilInfo->front.compareOp = stencil_func_to_vk_compare_op(front.fTest);
240 stencilInfo->front.compareMask = front.fTestMask;
241 stencilInfo->front.writeMask = front.fWriteMask;
242 stencilInfo->front.reference = front.fRef;
Greg Daniel164a9f02016-02-22 09:56:40 -0500243
244 // Set back face
cdalton93a379b2016-05-11 13:58:08 -0700245 if (!stencilSettings.isTwoSided()) {
246 stencilInfo->back = stencilInfo->front;
247 } else {
248 const GrStencilSettings::Face& back = stencilSettings.back();
249 stencilInfo->back.failOp = stencil_op_to_vk_stencil_op(back.fFailOp);
250 stencilInfo->back.passOp = stencil_op_to_vk_stencil_op(back.fPassOp);
251 stencilInfo->back.depthFailOp = stencilInfo->front.failOp;
252 stencilInfo->back.compareOp = stencil_func_to_vk_compare_op(back.fTest);
253 stencilInfo->back.compareMask = back.fTestMask;
254 stencilInfo->back.writeMask = back.fWriteMask;
255 stencilInfo->back.reference = back.fRef;
256 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500257 }
258 stencilInfo->minDepthBounds = 0.0f;
259 stencilInfo->maxDepthBounds = 1.0f;
260}
261
egdanielec440992016-09-13 09:54:11 -0700262static void setup_viewport_scissor_state(VkPipelineViewportStateCreateInfo* viewportInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500263 memset(viewportInfo, 0, sizeof(VkPipelineViewportStateCreateInfo));
264 viewportInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
265 viewportInfo->pNext = nullptr;
266 viewportInfo->flags = 0;
267
Greg Daniel164a9f02016-02-22 09:56:40 -0500268 viewportInfo->viewportCount = 1;
egdaniel470d77a2016-03-18 12:50:27 -0700269 viewportInfo->pViewports = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500270
egdaniel470d77a2016-03-18 12:50:27 -0700271 viewportInfo->scissorCount = 1;
272 viewportInfo->pScissors = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500273
Greg Daniel164a9f02016-02-22 09:56:40 -0500274 SkASSERT(viewportInfo->viewportCount == viewportInfo->scissorCount);
275}
276
Brian Salomonff168d92018-06-23 15:17:27 -0400277static void setup_multisample_state(const GrPrimitiveProcessor& primProc,
278 const GrPipeline& pipeline,
egdanielec440992016-09-13 09:54:11 -0700279 const GrCaps* caps,
280 VkPipelineMultisampleStateCreateInfo* multisampleInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500281 memset(multisampleInfo, 0, sizeof(VkPipelineMultisampleStateCreateInfo));
282 multisampleInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
283 multisampleInfo->pNext = nullptr;
284 multisampleInfo->flags = 0;
Robert Phillips2890fbf2017-07-26 15:48:41 -0400285 int numSamples = pipeline.proxy()->numColorSamples();
Greg Daniel164a9f02016-02-22 09:56:40 -0500286 SkAssertResult(GrSampleCountToVkSampleCount(numSamples,
287 &multisampleInfo->rasterizationSamples));
ethannicholas28ef4452016-03-25 09:26:03 -0700288 float sampleShading = primProc.getSampleShading();
289 SkASSERT(sampleShading == 0.0f || caps->sampleShadingSupport());
290 multisampleInfo->sampleShadingEnable = sampleShading > 0.0f;
291 multisampleInfo->minSampleShading = sampleShading;
Greg Daniel164a9f02016-02-22 09:56:40 -0500292 multisampleInfo->pSampleMask = nullptr;
293 multisampleInfo->alphaToCoverageEnable = VK_FALSE;
294 multisampleInfo->alphaToOneEnable = VK_FALSE;
295}
296
297static VkBlendFactor blend_coeff_to_vk_blend(GrBlendCoeff coeff) {
298 static const VkBlendFactor gTable[] = {
299 VK_BLEND_FACTOR_ZERO, // kZero_GrBlendCoeff
300 VK_BLEND_FACTOR_ONE, // kOne_GrBlendCoeff
301 VK_BLEND_FACTOR_SRC_COLOR, // kSC_GrBlendCoeff
302 VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR, // kISC_GrBlendCoeff
303 VK_BLEND_FACTOR_DST_COLOR, // kDC_GrBlendCoeff
304 VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR, // kIDC_GrBlendCoeff
305 VK_BLEND_FACTOR_SRC_ALPHA, // kSA_GrBlendCoeff
306 VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, // kISA_GrBlendCoeff
307 VK_BLEND_FACTOR_DST_ALPHA, // kDA_GrBlendCoeff
308 VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA, // kIDA_GrBlendCoeff
309 VK_BLEND_FACTOR_CONSTANT_COLOR, // kConstC_GrBlendCoeff
310 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR, // kIConstC_GrBlendCoeff
311 VK_BLEND_FACTOR_CONSTANT_ALPHA, // kConstA_GrBlendCoeff
312 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA, // kIConstA_GrBlendCoeff
313 VK_BLEND_FACTOR_SRC1_COLOR, // kS2C_GrBlendCoeff
314 VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR, // kIS2C_GrBlendCoeff
315 VK_BLEND_FACTOR_SRC1_ALPHA, // kS2A_GrBlendCoeff
316 VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA, // kIS2A_GrBlendCoeff
317
318 };
319 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrBlendCoeffCnt);
320 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
321 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
322 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
323 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
324 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
325 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
326 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
327 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
328 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
329 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
330 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
331 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
332 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
333 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
334 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
335 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
336 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
337 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
338
339 SkASSERT((unsigned)coeff < kGrBlendCoeffCnt);
340 return gTable[coeff];
341}
342
343
344static VkBlendOp blend_equation_to_vk_blend_op(GrBlendEquation equation) {
345 static const VkBlendOp gTable[] = {
346 VK_BLEND_OP_ADD, // kAdd_GrBlendEquation
347 VK_BLEND_OP_SUBTRACT, // kSubtract_GrBlendEquation
348 VK_BLEND_OP_REVERSE_SUBTRACT, // kReverseSubtract_GrBlendEquation
349 };
350 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kFirstAdvancedGrBlendEquation);
351 GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
352 GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
353 GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
354
355 SkASSERT((unsigned)equation < kGrBlendCoeffCnt);
356 return gTable[equation];
357}
358
egdanielec440992016-09-13 09:54:11 -0700359static bool blend_coeff_refs_constant(GrBlendCoeff coeff) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500360 static const bool gCoeffReferencesBlendConst[] = {
361 false,
362 false,
363 false,
364 false,
365 false,
366 false,
367 false,
368 false,
369 false,
370 false,
371 true,
372 true,
373 true,
374 true,
375
376 // extended blend coeffs
377 false,
378 false,
379 false,
380 false,
381 };
382 return gCoeffReferencesBlendConst[coeff];
383 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
384 // Individual enum asserts already made in blend_coeff_to_vk_blend
385}
386
egdanielec440992016-09-13 09:54:11 -0700387static void setup_color_blend_state(const GrPipeline& pipeline,
388 VkPipelineColorBlendStateCreateInfo* colorBlendInfo,
389 VkPipelineColorBlendAttachmentState* attachmentState) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500390 GrXferProcessor::BlendInfo blendInfo;
391 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
392
393 GrBlendEquation equation = blendInfo.fEquation;
394 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
395 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
396 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
397 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
398
399 memset(attachmentState, 0, sizeof(VkPipelineColorBlendAttachmentState));
400 attachmentState->blendEnable = !blendOff;
401 if (!blendOff) {
402 attachmentState->srcColorBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
403 attachmentState->dstColorBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
404 attachmentState->colorBlendOp = blend_equation_to_vk_blend_op(equation);
405 attachmentState->srcAlphaBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
406 attachmentState->dstAlphaBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
407 attachmentState->alphaBlendOp = blend_equation_to_vk_blend_op(equation);
408 }
egdaniel3d5d9ac2016-03-01 12:56:15 -0800409
410 if (!blendInfo.fWriteColor) {
411 attachmentState->colorWriteMask = 0;
412 } else {
413 attachmentState->colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
414 VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
415 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500416
417 memset(colorBlendInfo, 0, sizeof(VkPipelineColorBlendStateCreateInfo));
418 colorBlendInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
419 colorBlendInfo->pNext = nullptr;
420 colorBlendInfo->flags = 0;
421 colorBlendInfo->logicOpEnable = VK_FALSE;
422 colorBlendInfo->attachmentCount = 1;
423 colorBlendInfo->pAttachments = attachmentState;
egdaniel470d77a2016-03-18 12:50:27 -0700424 // colorBlendInfo->blendConstants is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500425}
426
egdanielec440992016-09-13 09:54:11 -0700427static void setup_raster_state(const GrPipeline& pipeline,
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400428 const GrCaps* caps,
egdanielec440992016-09-13 09:54:11 -0700429 VkPipelineRasterizationStateCreateInfo* rasterInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500430 memset(rasterInfo, 0, sizeof(VkPipelineRasterizationStateCreateInfo));
431 rasterInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
432 rasterInfo->pNext = nullptr;
433 rasterInfo->flags = 0;
434 rasterInfo->depthClampEnable = VK_FALSE;
435 rasterInfo->rasterizerDiscardEnable = VK_FALSE;
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400436 rasterInfo->polygonMode = caps->wireframeMode() ? VK_POLYGON_MODE_LINE
437 : VK_POLYGON_MODE_FILL;
Brian Salomonf0861672017-05-08 11:10:10 -0400438 rasterInfo->cullMode = VK_CULL_MODE_NONE;
Greg Daniel164a9f02016-02-22 09:56:40 -0500439 rasterInfo->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
440 rasterInfo->depthBiasEnable = VK_FALSE;
441 rasterInfo->depthBiasConstantFactor = 0.0f;
442 rasterInfo->depthBiasClamp = 0.0f;
443 rasterInfo->depthBiasSlopeFactor = 0.0f;
444 rasterInfo->lineWidth = 1.0f;
445}
446
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000447static void setup_dynamic_state(VkPipelineDynamicStateCreateInfo* dynamicInfo,
448 VkDynamicState* dynamicStates) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500449 memset(dynamicInfo, 0, sizeof(VkPipelineDynamicStateCreateInfo));
450 dynamicInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
egdaniel470d77a2016-03-18 12:50:27 -0700451 dynamicInfo->pNext = VK_NULL_HANDLE;
452 dynamicInfo->flags = 0;
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000453 dynamicStates[0] = VK_DYNAMIC_STATE_VIEWPORT;
454 dynamicStates[1] = VK_DYNAMIC_STATE_SCISSOR;
455 dynamicStates[2] = VK_DYNAMIC_STATE_BLEND_CONSTANTS;
456 dynamicInfo->dynamicStateCount = 3;
457 dynamicInfo->pDynamicStates = dynamicStates;
Greg Daniel164a9f02016-02-22 09:56:40 -0500458}
459
Brian Salomon49348902018-06-26 09:12:38 -0400460GrVkPipeline* GrVkPipeline::Create(GrVkGpu* gpu, const GrPrimitiveProcessor& primProc,
461 const GrPipeline& pipeline, const GrStencilSettings& stencil,
Greg Daniel164a9f02016-02-22 09:56:40 -0500462 VkPipelineShaderStageCreateInfo* shaderStageInfo,
Brian Salomon49348902018-06-26 09:12:38 -0400463 int shaderStageCount, GrPrimitiveType primitiveType,
464 const GrVkRenderPass& renderPass, VkPipelineLayout layout,
jvanverth03509ea2016-03-02 13:19:47 -0800465 VkPipelineCache cache) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500466 VkPipelineVertexInputStateCreateInfo vertexInputInfo;
Chris Dalton1d616352017-05-31 12:51:23 -0600467 SkSTArray<2, VkVertexInputBindingDescription, true> bindingDescs;
egdanielb05df0f2016-06-27 07:15:20 -0700468 SkSTArray<16, VkVertexInputAttributeDescription> attributeDesc;
Brian Salomon92be2f72018-06-19 14:33:47 -0400469 int totalAttributeCnt = primProc.numVertexAttributes() + primProc.numInstanceAttributes();
470 SkASSERT(totalAttributeCnt <= gpu->vkCaps().maxVertexAttributes());
471 VkVertexInputAttributeDescription* pAttribs = attributeDesc.push_back_n(totalAttributeCnt);
Chris Dalton1d616352017-05-31 12:51:23 -0600472 setup_vertex_input_state(primProc, &vertexInputInfo, &bindingDescs, pAttribs);
Greg Daniel164a9f02016-02-22 09:56:40 -0500473
474 VkPipelineInputAssemblyStateCreateInfo inputAssemblyInfo;
475 setup_input_assembly_state(primitiveType, &inputAssemblyInfo);
476
477 VkPipelineDepthStencilStateCreateInfo depthStencilInfo;
csmartdaltonc633abb2016-11-01 08:55:55 -0700478 setup_depth_stencil_state(stencil, &depthStencilInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500479
Greg Daniel164a9f02016-02-22 09:56:40 -0500480 VkPipelineViewportStateCreateInfo viewportInfo;
egdanielec440992016-09-13 09:54:11 -0700481 setup_viewport_scissor_state(&viewportInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500482
483 VkPipelineMultisampleStateCreateInfo multisampleInfo;
Brian Salomonff168d92018-06-23 15:17:27 -0400484 setup_multisample_state(primProc, pipeline, gpu->caps(), &multisampleInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500485
486 // We will only have one color attachment per pipeline.
487 VkPipelineColorBlendAttachmentState attachmentStates[1];
488 VkPipelineColorBlendStateCreateInfo colorBlendInfo;
egdanielec440992016-09-13 09:54:11 -0700489 setup_color_blend_state(pipeline, &colorBlendInfo, attachmentStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500490
491 VkPipelineRasterizationStateCreateInfo rasterInfo;
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400492 setup_raster_state(pipeline, gpu->caps(), &rasterInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500493
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000494 VkDynamicState dynamicStates[3];
Greg Daniel164a9f02016-02-22 09:56:40 -0500495 VkPipelineDynamicStateCreateInfo dynamicInfo;
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000496 setup_dynamic_state(&dynamicInfo, dynamicStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500497
498 VkGraphicsPipelineCreateInfo pipelineCreateInfo;
499 memset(&pipelineCreateInfo, 0, sizeof(VkGraphicsPipelineCreateInfo));
500 pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
501 pipelineCreateInfo.pNext = nullptr;
502 pipelineCreateInfo.flags = 0;
503 pipelineCreateInfo.stageCount = shaderStageCount;
504 pipelineCreateInfo.pStages = shaderStageInfo;
505 pipelineCreateInfo.pVertexInputState = &vertexInputInfo;
506 pipelineCreateInfo.pInputAssemblyState = &inputAssemblyInfo;
507 pipelineCreateInfo.pTessellationState = nullptr;
508 pipelineCreateInfo.pViewportState = &viewportInfo;
509 pipelineCreateInfo.pRasterizationState = &rasterInfo;
510 pipelineCreateInfo.pMultisampleState = &multisampleInfo;
511 pipelineCreateInfo.pDepthStencilState = &depthStencilInfo;
512 pipelineCreateInfo.pColorBlendState = &colorBlendInfo;
513 pipelineCreateInfo.pDynamicState = &dynamicInfo;
514 pipelineCreateInfo.layout = layout;
515 pipelineCreateInfo.renderPass = renderPass.vkRenderPass();
516 pipelineCreateInfo.subpass = 0;
517 pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
518 pipelineCreateInfo.basePipelineIndex = -1;
519
520 VkPipeline vkPipeline;
521 VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateGraphicsPipelines(gpu->device(),
halcanary9d524f22016-03-29 09:03:52 -0700522 cache, 1,
523 &pipelineCreateInfo,
Greg Daniel164a9f02016-02-22 09:56:40 -0500524 nullptr, &vkPipeline));
525 if (err) {
Greg Daniel5ba448c2018-02-26 13:30:47 -0500526 SkDebugf("Failed to create pipeline. Error: %d\n", err);
Greg Daniel164a9f02016-02-22 09:56:40 -0500527 return nullptr;
528 }
529
530 return new GrVkPipeline(vkPipeline);
531}
532
533void GrVkPipeline::freeGPUData(const GrVkGpu* gpu) const {
534 GR_VK_CALL(gpu->vkInterface(), DestroyPipeline(gpu->device(), fPipeline, nullptr));
535}
536
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000537void GrVkPipeline::SetDynamicScissorRectState(GrVkGpu* gpu,
538 GrVkCommandBuffer* cmdBuffer,
539 const GrRenderTarget* renderTarget,
540 GrSurfaceOrigin rtOrigin,
541 SkIRect scissorRect) {
542 if (!scissorRect.intersect(SkIRect::MakeWH(renderTarget->width(), renderTarget->height()))) {
543 scissorRect.setEmpty();
544 }
545
546 VkRect2D scissor;
547 scissor.offset.x = scissorRect.fLeft;
548 scissor.extent.width = scissorRect.width();
549 if (kTopLeft_GrSurfaceOrigin == rtOrigin) {
550 scissor.offset.y = scissorRect.fTop;
551 } else {
552 SkASSERT(kBottomLeft_GrSurfaceOrigin == rtOrigin);
553 scissor.offset.y = renderTarget->height() - scissorRect.fBottom;
554 }
555 scissor.extent.height = scissorRect.height();
556
557 SkASSERT(scissor.offset.x >= 0);
558 SkASSERT(scissor.offset.y >= 0);
559 cmdBuffer->setScissor(gpu, 0, 1, &scissor);
560}
561
Chris Dalton46983b72017-06-06 12:27:16 -0600562void GrVkPipeline::SetDynamicViewportState(GrVkGpu* gpu,
563 GrVkCommandBuffer* cmdBuffer,
564 const GrRenderTarget* renderTarget) {
egdaniel470d77a2016-03-18 12:50:27 -0700565 // We always use one viewport the size of the RT
566 VkViewport viewport;
567 viewport.x = 0.0f;
568 viewport.y = 0.0f;
Chris Dalton46983b72017-06-06 12:27:16 -0600569 viewport.width = SkIntToScalar(renderTarget->width());
570 viewport.height = SkIntToScalar(renderTarget->height());
egdaniel470d77a2016-03-18 12:50:27 -0700571 viewport.minDepth = 0.0f;
572 viewport.maxDepth = 1.0f;
573 cmdBuffer->setViewport(gpu, 0, 1, &viewport);
574}
575
Chris Dalton46983b72017-06-06 12:27:16 -0600576void GrVkPipeline::SetDynamicBlendConstantState(GrVkGpu* gpu,
577 GrVkCommandBuffer* cmdBuffer,
578 GrPixelConfig pixelConfig,
579 const GrXferProcessor& xferProcessor) {
egdaniel470d77a2016-03-18 12:50:27 -0700580 GrXferProcessor::BlendInfo blendInfo;
Chris Dalton46983b72017-06-06 12:27:16 -0600581 xferProcessor.getBlendInfo(&blendInfo);
egdaniel470d77a2016-03-18 12:50:27 -0700582 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
583 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
584 float floatColors[4];
585 if (blend_coeff_refs_constant(srcCoeff) || blend_coeff_refs_constant(dstCoeff)) {
Greg Daniel6c9f1012017-05-11 09:20:59 -0400586 // Swizzle the blend to match what the shader will output.
Chris Dalton46983b72017-06-06 12:27:16 -0600587 const GrSwizzle& swizzle = gpu->caps()->shaderCaps()->configOutputSwizzle(pixelConfig);
Greg Daniel6c9f1012017-05-11 09:20:59 -0400588 GrColor blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
589 GrColorToRGBAFloat(blendConst, floatColors);
egdaniel470d77a2016-03-18 12:50:27 -0700590 } else {
591 memset(floatColors, 0, 4 * sizeof(float));
592 }
593 cmdBuffer->setBlendConstants(gpu, floatColors);
594}