blob: b1c8dbeeb402de32c93c55bc4ba21a983813150e [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"
9
10#include "GrGeometryProcessor.h"
11#include "GrPipeline.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
62 if (primProc.hasVertexAttribs()) {
63 vertexBinding = bindingDescs->count();
64 bindingDescs->push_back() = {
65 vertexBinding,
66 (uint32_t) primProc.getVertexStride(),
67 VK_VERTEX_INPUT_RATE_VERTEX
68 };
69 }
70
71 if (primProc.hasInstanceAttribs()) {
72 instanceBinding = bindingDescs->count();
73 bindingDescs->push_back() = {
74 instanceBinding,
75 (uint32_t) primProc.getInstanceStride(),
76 VK_VERTEX_INPUT_RATE_INSTANCE
77 };
78 }
Greg Daniel164a9f02016-02-22 09:56:40 -050079
80 // setup attribute descriptions
81 int vaCount = primProc.numAttribs();
Greg Daniel164a9f02016-02-22 09:56:40 -050082 if (vaCount > 0) {
Greg Daniel164a9f02016-02-22 09:56:40 -050083 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
Chris Dalton1d616352017-05-31 12:51:23 -060084 using InputRate = GrPrimitiveProcessor::Attribute::InputRate;
Greg Daniel164a9f02016-02-22 09:56:40 -050085 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
Greg Daniel164a9f02016-02-22 09:56:40 -050086 VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex];
87 vkAttrib.location = attribIndex; // for now assume location = attribIndex
Chris Dalton1d616352017-05-31 12:51:23 -060088 vkAttrib.binding = InputRate::kPerInstance == attrib.fInputRate ? instanceBinding
89 : vertexBinding;
90 vkAttrib.format = attrib_type_to_vkformat(attrib.fType);
91 vkAttrib.offset = attrib.fOffsetInRecord;
Greg Daniel164a9f02016-02-22 09:56:40 -050092 }
93 }
94
95 memset(vertexInputInfo, 0, sizeof(VkPipelineVertexInputStateCreateInfo));
96 vertexInputInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
97 vertexInputInfo->pNext = nullptr;
98 vertexInputInfo->flags = 0;
Chris Dalton1d616352017-05-31 12:51:23 -060099 vertexInputInfo->vertexBindingDescriptionCount = bindingDescs->count();
100 vertexInputInfo->pVertexBindingDescriptions = bindingDescs->begin();
Greg Daniel164a9f02016-02-22 09:56:40 -0500101 vertexInputInfo->vertexAttributeDescriptionCount = vaCount;
102 vertexInputInfo->pVertexAttributeDescriptions = attributeDesc;
103}
104
Chris Dalton3809bab2017-06-13 10:55:06 -0600105static VkPrimitiveTopology gr_primitive_type_to_vk_topology(GrPrimitiveType primitiveType) {
106 switch (primitiveType) {
107 case GrPrimitiveType::kTriangles:
108 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
109 case GrPrimitiveType::kTriangleStrip:
110 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
111 case GrPrimitiveType::kTriangleFan:
112 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN;
113 case GrPrimitiveType::kPoints:
114 return VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
115 case GrPrimitiveType::kLines:
116 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
117 case GrPrimitiveType::kLineStrip:
118 return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
119 case GrPrimitiveType::kLinesAdjacency:
120 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY;
121 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400122 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -0600123 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
124}
Greg Daniel164a9f02016-02-22 09:56:40 -0500125
126static void setup_input_assembly_state(GrPrimitiveType primitiveType,
127 VkPipelineInputAssemblyStateCreateInfo* inputAssemblyInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500128 memset(inputAssemblyInfo, 0, sizeof(VkPipelineInputAssemblyStateCreateInfo));
129 inputAssemblyInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
130 inputAssemblyInfo->pNext = nullptr;
131 inputAssemblyInfo->flags = 0;
132 inputAssemblyInfo->primitiveRestartEnable = false;
Chris Dalton3809bab2017-06-13 10:55:06 -0600133 inputAssemblyInfo->topology = gr_primitive_type_to_vk_topology(primitiveType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500134}
135
136
egdanielec440992016-09-13 09:54:11 -0700137static VkStencilOp stencil_op_to_vk_stencil_op(GrStencilOp op) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500138 static const VkStencilOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700139 VK_STENCIL_OP_KEEP, // kKeep
140 VK_STENCIL_OP_ZERO, // kZero
141 VK_STENCIL_OP_REPLACE, // kReplace
142 VK_STENCIL_OP_INVERT, // kInvert
143 VK_STENCIL_OP_INCREMENT_AND_WRAP, // kIncWrap
144 VK_STENCIL_OP_DECREMENT_AND_WRAP, // kDecWrap
145 VK_STENCIL_OP_INCREMENT_AND_CLAMP, // kIncClamp
146 VK_STENCIL_OP_DECREMENT_AND_CLAMP, // kDecClamp
Greg Daniel164a9f02016-02-22 09:56:40 -0500147 };
cdalton93a379b2016-05-11 13:58:08 -0700148 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilOpCount);
149 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
150 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
151 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
152 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
153 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
154 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
155 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
156 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
157 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
158 return gTable[(int)op];
Greg Daniel164a9f02016-02-22 09:56:40 -0500159}
160
egdanielec440992016-09-13 09:54:11 -0700161static VkCompareOp stencil_func_to_vk_compare_op(GrStencilTest test) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500162 static const VkCompareOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700163 VK_COMPARE_OP_ALWAYS, // kAlways
164 VK_COMPARE_OP_NEVER, // kNever
165 VK_COMPARE_OP_GREATER, // kGreater
166 VK_COMPARE_OP_GREATER_OR_EQUAL, // kGEqual
167 VK_COMPARE_OP_LESS, // kLess
168 VK_COMPARE_OP_LESS_OR_EQUAL, // kLEqual
169 VK_COMPARE_OP_EQUAL, // kEqual
170 VK_COMPARE_OP_NOT_EQUAL, // kNotEqual
Greg Daniel164a9f02016-02-22 09:56:40 -0500171 };
cdalton93a379b2016-05-11 13:58:08 -0700172 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilTestCount);
173 GR_STATIC_ASSERT(0 == (int)GrStencilTest::kAlways);
174 GR_STATIC_ASSERT(1 == (int)GrStencilTest::kNever);
175 GR_STATIC_ASSERT(2 == (int)GrStencilTest::kGreater);
176 GR_STATIC_ASSERT(3 == (int)GrStencilTest::kGEqual);
177 GR_STATIC_ASSERT(4 == (int)GrStencilTest::kLess);
178 GR_STATIC_ASSERT(5 == (int)GrStencilTest::kLEqual);
179 GR_STATIC_ASSERT(6 == (int)GrStencilTest::kEqual);
180 GR_STATIC_ASSERT(7 == (int)GrStencilTest::kNotEqual);
181 SkASSERT(test < (GrStencilTest)kGrStencilTestCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500182
cdalton93a379b2016-05-11 13:58:08 -0700183 return gTable[(int)test];
Greg Daniel164a9f02016-02-22 09:56:40 -0500184}
185
egdanielec440992016-09-13 09:54:11 -0700186static void setup_depth_stencil_state(const GrStencilSettings& stencilSettings,
187 VkPipelineDepthStencilStateCreateInfo* stencilInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500188 memset(stencilInfo, 0, sizeof(VkPipelineDepthStencilStateCreateInfo));
189 stencilInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
190 stencilInfo->pNext = nullptr;
191 stencilInfo->flags = 0;
192 // set depth testing defaults
193 stencilInfo->depthTestEnable = VK_FALSE;
194 stencilInfo->depthWriteEnable = VK_FALSE;
195 stencilInfo->depthCompareOp = VK_COMPARE_OP_ALWAYS;
196 stencilInfo->depthBoundsTestEnable = VK_FALSE;
197 stencilInfo->stencilTestEnable = !stencilSettings.isDisabled();
198 if (!stencilSettings.isDisabled()) {
199 // Set front face
cdalton93a379b2016-05-11 13:58:08 -0700200 const GrStencilSettings::Face& front = stencilSettings.front();
201 stencilInfo->front.failOp = stencil_op_to_vk_stencil_op(front.fFailOp);
202 stencilInfo->front.passOp = stencil_op_to_vk_stencil_op(front.fPassOp);
Greg Daniel164a9f02016-02-22 09:56:40 -0500203 stencilInfo->front.depthFailOp = stencilInfo->front.failOp;
cdalton93a379b2016-05-11 13:58:08 -0700204 stencilInfo->front.compareOp = stencil_func_to_vk_compare_op(front.fTest);
205 stencilInfo->front.compareMask = front.fTestMask;
206 stencilInfo->front.writeMask = front.fWriteMask;
207 stencilInfo->front.reference = front.fRef;
Greg Daniel164a9f02016-02-22 09:56:40 -0500208
209 // Set back face
cdalton93a379b2016-05-11 13:58:08 -0700210 if (!stencilSettings.isTwoSided()) {
211 stencilInfo->back = stencilInfo->front;
212 } else {
213 const GrStencilSettings::Face& back = stencilSettings.back();
214 stencilInfo->back.failOp = stencil_op_to_vk_stencil_op(back.fFailOp);
215 stencilInfo->back.passOp = stencil_op_to_vk_stencil_op(back.fPassOp);
216 stencilInfo->back.depthFailOp = stencilInfo->front.failOp;
217 stencilInfo->back.compareOp = stencil_func_to_vk_compare_op(back.fTest);
218 stencilInfo->back.compareMask = back.fTestMask;
219 stencilInfo->back.writeMask = back.fWriteMask;
220 stencilInfo->back.reference = back.fRef;
221 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500222 }
223 stencilInfo->minDepthBounds = 0.0f;
224 stencilInfo->maxDepthBounds = 1.0f;
225}
226
egdanielec440992016-09-13 09:54:11 -0700227static void setup_viewport_scissor_state(VkPipelineViewportStateCreateInfo* viewportInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500228 memset(viewportInfo, 0, sizeof(VkPipelineViewportStateCreateInfo));
229 viewportInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
230 viewportInfo->pNext = nullptr;
231 viewportInfo->flags = 0;
232
Greg Daniel164a9f02016-02-22 09:56:40 -0500233 viewportInfo->viewportCount = 1;
egdaniel470d77a2016-03-18 12:50:27 -0700234 viewportInfo->pViewports = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500235
egdaniel470d77a2016-03-18 12:50:27 -0700236 viewportInfo->scissorCount = 1;
237 viewportInfo->pScissors = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500238
Greg Daniel164a9f02016-02-22 09:56:40 -0500239 SkASSERT(viewportInfo->viewportCount == viewportInfo->scissorCount);
240}
241
egdanielec440992016-09-13 09:54:11 -0700242static void setup_multisample_state(const GrPipeline& pipeline,
243 const GrPrimitiveProcessor& primProc,
244 const GrCaps* caps,
245 VkPipelineMultisampleStateCreateInfo* multisampleInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500246 memset(multisampleInfo, 0, sizeof(VkPipelineMultisampleStateCreateInfo));
247 multisampleInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
248 multisampleInfo->pNext = nullptr;
249 multisampleInfo->flags = 0;
Robert Phillips2890fbf2017-07-26 15:48:41 -0400250 int numSamples = pipeline.proxy()->numColorSamples();
Greg Daniel164a9f02016-02-22 09:56:40 -0500251 SkAssertResult(GrSampleCountToVkSampleCount(numSamples,
252 &multisampleInfo->rasterizationSamples));
ethannicholas28ef4452016-03-25 09:26:03 -0700253 float sampleShading = primProc.getSampleShading();
254 SkASSERT(sampleShading == 0.0f || caps->sampleShadingSupport());
255 multisampleInfo->sampleShadingEnable = sampleShading > 0.0f;
256 multisampleInfo->minSampleShading = sampleShading;
Greg Daniel164a9f02016-02-22 09:56:40 -0500257 multisampleInfo->pSampleMask = nullptr;
258 multisampleInfo->alphaToCoverageEnable = VK_FALSE;
259 multisampleInfo->alphaToOneEnable = VK_FALSE;
260}
261
262static VkBlendFactor blend_coeff_to_vk_blend(GrBlendCoeff coeff) {
263 static const VkBlendFactor gTable[] = {
264 VK_BLEND_FACTOR_ZERO, // kZero_GrBlendCoeff
265 VK_BLEND_FACTOR_ONE, // kOne_GrBlendCoeff
266 VK_BLEND_FACTOR_SRC_COLOR, // kSC_GrBlendCoeff
267 VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR, // kISC_GrBlendCoeff
268 VK_BLEND_FACTOR_DST_COLOR, // kDC_GrBlendCoeff
269 VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR, // kIDC_GrBlendCoeff
270 VK_BLEND_FACTOR_SRC_ALPHA, // kSA_GrBlendCoeff
271 VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, // kISA_GrBlendCoeff
272 VK_BLEND_FACTOR_DST_ALPHA, // kDA_GrBlendCoeff
273 VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA, // kIDA_GrBlendCoeff
274 VK_BLEND_FACTOR_CONSTANT_COLOR, // kConstC_GrBlendCoeff
275 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR, // kIConstC_GrBlendCoeff
276 VK_BLEND_FACTOR_CONSTANT_ALPHA, // kConstA_GrBlendCoeff
277 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA, // kIConstA_GrBlendCoeff
278 VK_BLEND_FACTOR_SRC1_COLOR, // kS2C_GrBlendCoeff
279 VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR, // kIS2C_GrBlendCoeff
280 VK_BLEND_FACTOR_SRC1_ALPHA, // kS2A_GrBlendCoeff
281 VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA, // kIS2A_GrBlendCoeff
282
283 };
284 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrBlendCoeffCnt);
285 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
286 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
287 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
288 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
289 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
290 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
291 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
292 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
293 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
294 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
295 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
296 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
297 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
298 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
299 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
300 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
301 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
302 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
303
304 SkASSERT((unsigned)coeff < kGrBlendCoeffCnt);
305 return gTable[coeff];
306}
307
308
309static VkBlendOp blend_equation_to_vk_blend_op(GrBlendEquation equation) {
310 static const VkBlendOp gTable[] = {
311 VK_BLEND_OP_ADD, // kAdd_GrBlendEquation
312 VK_BLEND_OP_SUBTRACT, // kSubtract_GrBlendEquation
313 VK_BLEND_OP_REVERSE_SUBTRACT, // kReverseSubtract_GrBlendEquation
314 };
315 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kFirstAdvancedGrBlendEquation);
316 GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
317 GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
318 GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
319
320 SkASSERT((unsigned)equation < kGrBlendCoeffCnt);
321 return gTable[equation];
322}
323
egdanielec440992016-09-13 09:54:11 -0700324static bool blend_coeff_refs_constant(GrBlendCoeff coeff) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500325 static const bool gCoeffReferencesBlendConst[] = {
326 false,
327 false,
328 false,
329 false,
330 false,
331 false,
332 false,
333 false,
334 false,
335 false,
336 true,
337 true,
338 true,
339 true,
340
341 // extended blend coeffs
342 false,
343 false,
344 false,
345 false,
346 };
347 return gCoeffReferencesBlendConst[coeff];
348 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
349 // Individual enum asserts already made in blend_coeff_to_vk_blend
350}
351
egdanielec440992016-09-13 09:54:11 -0700352static void setup_color_blend_state(const GrPipeline& pipeline,
353 VkPipelineColorBlendStateCreateInfo* colorBlendInfo,
354 VkPipelineColorBlendAttachmentState* attachmentState) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500355 GrXferProcessor::BlendInfo blendInfo;
356 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
357
358 GrBlendEquation equation = blendInfo.fEquation;
359 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
360 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
361 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
362 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
363
364 memset(attachmentState, 0, sizeof(VkPipelineColorBlendAttachmentState));
365 attachmentState->blendEnable = !blendOff;
366 if (!blendOff) {
367 attachmentState->srcColorBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
368 attachmentState->dstColorBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
369 attachmentState->colorBlendOp = blend_equation_to_vk_blend_op(equation);
370 attachmentState->srcAlphaBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
371 attachmentState->dstAlphaBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
372 attachmentState->alphaBlendOp = blend_equation_to_vk_blend_op(equation);
373 }
egdaniel3d5d9ac2016-03-01 12:56:15 -0800374
375 if (!blendInfo.fWriteColor) {
376 attachmentState->colorWriteMask = 0;
377 } else {
378 attachmentState->colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
379 VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
380 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500381
382 memset(colorBlendInfo, 0, sizeof(VkPipelineColorBlendStateCreateInfo));
383 colorBlendInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
384 colorBlendInfo->pNext = nullptr;
385 colorBlendInfo->flags = 0;
386 colorBlendInfo->logicOpEnable = VK_FALSE;
387 colorBlendInfo->attachmentCount = 1;
388 colorBlendInfo->pAttachments = attachmentState;
egdaniel470d77a2016-03-18 12:50:27 -0700389 // colorBlendInfo->blendConstants is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500390}
391
egdanielec440992016-09-13 09:54:11 -0700392static void setup_raster_state(const GrPipeline& pipeline,
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400393 const GrCaps* caps,
egdanielec440992016-09-13 09:54:11 -0700394 VkPipelineRasterizationStateCreateInfo* rasterInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500395 memset(rasterInfo, 0, sizeof(VkPipelineRasterizationStateCreateInfo));
396 rasterInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
397 rasterInfo->pNext = nullptr;
398 rasterInfo->flags = 0;
399 rasterInfo->depthClampEnable = VK_FALSE;
400 rasterInfo->rasterizerDiscardEnable = VK_FALSE;
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400401 rasterInfo->polygonMode = caps->wireframeMode() ? VK_POLYGON_MODE_LINE
402 : VK_POLYGON_MODE_FILL;
Brian Salomonf0861672017-05-08 11:10:10 -0400403 rasterInfo->cullMode = VK_CULL_MODE_NONE;
Greg Daniel164a9f02016-02-22 09:56:40 -0500404 rasterInfo->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
405 rasterInfo->depthBiasEnable = VK_FALSE;
406 rasterInfo->depthBiasConstantFactor = 0.0f;
407 rasterInfo->depthBiasClamp = 0.0f;
408 rasterInfo->depthBiasSlopeFactor = 0.0f;
409 rasterInfo->lineWidth = 1.0f;
410}
411
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000412static void setup_dynamic_state(VkPipelineDynamicStateCreateInfo* dynamicInfo,
413 VkDynamicState* dynamicStates) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500414 memset(dynamicInfo, 0, sizeof(VkPipelineDynamicStateCreateInfo));
415 dynamicInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
egdaniel470d77a2016-03-18 12:50:27 -0700416 dynamicInfo->pNext = VK_NULL_HANDLE;
417 dynamicInfo->flags = 0;
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000418 dynamicStates[0] = VK_DYNAMIC_STATE_VIEWPORT;
419 dynamicStates[1] = VK_DYNAMIC_STATE_SCISSOR;
420 dynamicStates[2] = VK_DYNAMIC_STATE_BLEND_CONSTANTS;
421 dynamicInfo->dynamicStateCount = 3;
422 dynamicInfo->pDynamicStates = dynamicStates;
Greg Daniel164a9f02016-02-22 09:56:40 -0500423}
424
425GrVkPipeline* GrVkPipeline::Create(GrVkGpu* gpu, const GrPipeline& pipeline,
csmartdaltonc633abb2016-11-01 08:55:55 -0700426 const GrStencilSettings& stencil,
Greg Daniel164a9f02016-02-22 09:56:40 -0500427 const GrPrimitiveProcessor& primProc,
428 VkPipelineShaderStageCreateInfo* shaderStageInfo,
429 int shaderStageCount,
430 GrPrimitiveType primitiveType,
431 const GrVkRenderPass& renderPass,
jvanverth03509ea2016-03-02 13:19:47 -0800432 VkPipelineLayout layout,
433 VkPipelineCache cache) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500434 VkPipelineVertexInputStateCreateInfo vertexInputInfo;
Chris Dalton1d616352017-05-31 12:51:23 -0600435 SkSTArray<2, VkVertexInputBindingDescription, true> bindingDescs;
egdanielb05df0f2016-06-27 07:15:20 -0700436 SkSTArray<16, VkVertexInputAttributeDescription> attributeDesc;
437 SkASSERT(primProc.numAttribs() <= gpu->vkCaps().maxVertexAttributes());
438 VkVertexInputAttributeDescription* pAttribs = attributeDesc.push_back_n(primProc.numAttribs());
Chris Dalton1d616352017-05-31 12:51:23 -0600439 setup_vertex_input_state(primProc, &vertexInputInfo, &bindingDescs, pAttribs);
Greg Daniel164a9f02016-02-22 09:56:40 -0500440
441 VkPipelineInputAssemblyStateCreateInfo inputAssemblyInfo;
442 setup_input_assembly_state(primitiveType, &inputAssemblyInfo);
443
444 VkPipelineDepthStencilStateCreateInfo depthStencilInfo;
csmartdaltonc633abb2016-11-01 08:55:55 -0700445 setup_depth_stencil_state(stencil, &depthStencilInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500446
Greg Daniel164a9f02016-02-22 09:56:40 -0500447 VkPipelineViewportStateCreateInfo viewportInfo;
egdanielec440992016-09-13 09:54:11 -0700448 setup_viewport_scissor_state(&viewportInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500449
450 VkPipelineMultisampleStateCreateInfo multisampleInfo;
ethannicholas28ef4452016-03-25 09:26:03 -0700451 setup_multisample_state(pipeline, primProc, gpu->caps(), &multisampleInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500452
453 // We will only have one color attachment per pipeline.
454 VkPipelineColorBlendAttachmentState attachmentStates[1];
455 VkPipelineColorBlendStateCreateInfo colorBlendInfo;
egdanielec440992016-09-13 09:54:11 -0700456 setup_color_blend_state(pipeline, &colorBlendInfo, attachmentStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500457
458 VkPipelineRasterizationStateCreateInfo rasterInfo;
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400459 setup_raster_state(pipeline, gpu->caps(), &rasterInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500460
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000461 VkDynamicState dynamicStates[3];
Greg Daniel164a9f02016-02-22 09:56:40 -0500462 VkPipelineDynamicStateCreateInfo dynamicInfo;
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000463 setup_dynamic_state(&dynamicInfo, dynamicStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500464
465 VkGraphicsPipelineCreateInfo pipelineCreateInfo;
466 memset(&pipelineCreateInfo, 0, sizeof(VkGraphicsPipelineCreateInfo));
467 pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
468 pipelineCreateInfo.pNext = nullptr;
469 pipelineCreateInfo.flags = 0;
470 pipelineCreateInfo.stageCount = shaderStageCount;
471 pipelineCreateInfo.pStages = shaderStageInfo;
472 pipelineCreateInfo.pVertexInputState = &vertexInputInfo;
473 pipelineCreateInfo.pInputAssemblyState = &inputAssemblyInfo;
474 pipelineCreateInfo.pTessellationState = nullptr;
475 pipelineCreateInfo.pViewportState = &viewportInfo;
476 pipelineCreateInfo.pRasterizationState = &rasterInfo;
477 pipelineCreateInfo.pMultisampleState = &multisampleInfo;
478 pipelineCreateInfo.pDepthStencilState = &depthStencilInfo;
479 pipelineCreateInfo.pColorBlendState = &colorBlendInfo;
480 pipelineCreateInfo.pDynamicState = &dynamicInfo;
481 pipelineCreateInfo.layout = layout;
482 pipelineCreateInfo.renderPass = renderPass.vkRenderPass();
483 pipelineCreateInfo.subpass = 0;
484 pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
485 pipelineCreateInfo.basePipelineIndex = -1;
486
487 VkPipeline vkPipeline;
488 VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateGraphicsPipelines(gpu->device(),
halcanary9d524f22016-03-29 09:03:52 -0700489 cache, 1,
490 &pipelineCreateInfo,
Greg Daniel164a9f02016-02-22 09:56:40 -0500491 nullptr, &vkPipeline));
492 if (err) {
493 return nullptr;
494 }
495
496 return new GrVkPipeline(vkPipeline);
497}
498
499void GrVkPipeline::freeGPUData(const GrVkGpu* gpu) const {
500 GR_VK_CALL(gpu->vkInterface(), DestroyPipeline(gpu->device(), fPipeline, nullptr));
501}
502
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000503void GrVkPipeline::SetDynamicScissorRectState(GrVkGpu* gpu,
504 GrVkCommandBuffer* cmdBuffer,
505 const GrRenderTarget* renderTarget,
506 GrSurfaceOrigin rtOrigin,
507 SkIRect scissorRect) {
508 if (!scissorRect.intersect(SkIRect::MakeWH(renderTarget->width(), renderTarget->height()))) {
509 scissorRect.setEmpty();
510 }
511
512 VkRect2D scissor;
513 scissor.offset.x = scissorRect.fLeft;
514 scissor.extent.width = scissorRect.width();
515 if (kTopLeft_GrSurfaceOrigin == rtOrigin) {
516 scissor.offset.y = scissorRect.fTop;
517 } else {
518 SkASSERT(kBottomLeft_GrSurfaceOrigin == rtOrigin);
519 scissor.offset.y = renderTarget->height() - scissorRect.fBottom;
520 }
521 scissor.extent.height = scissorRect.height();
522
523 SkASSERT(scissor.offset.x >= 0);
524 SkASSERT(scissor.offset.y >= 0);
525 cmdBuffer->setScissor(gpu, 0, 1, &scissor);
526}
527
Chris Dalton46983b72017-06-06 12:27:16 -0600528void GrVkPipeline::SetDynamicViewportState(GrVkGpu* gpu,
529 GrVkCommandBuffer* cmdBuffer,
530 const GrRenderTarget* renderTarget) {
egdaniel470d77a2016-03-18 12:50:27 -0700531 // We always use one viewport the size of the RT
532 VkViewport viewport;
533 viewport.x = 0.0f;
534 viewport.y = 0.0f;
Chris Dalton46983b72017-06-06 12:27:16 -0600535 viewport.width = SkIntToScalar(renderTarget->width());
536 viewport.height = SkIntToScalar(renderTarget->height());
egdaniel470d77a2016-03-18 12:50:27 -0700537 viewport.minDepth = 0.0f;
538 viewport.maxDepth = 1.0f;
539 cmdBuffer->setViewport(gpu, 0, 1, &viewport);
540}
541
Chris Dalton46983b72017-06-06 12:27:16 -0600542void GrVkPipeline::SetDynamicBlendConstantState(GrVkGpu* gpu,
543 GrVkCommandBuffer* cmdBuffer,
544 GrPixelConfig pixelConfig,
545 const GrXferProcessor& xferProcessor) {
egdaniel470d77a2016-03-18 12:50:27 -0700546 GrXferProcessor::BlendInfo blendInfo;
Chris Dalton46983b72017-06-06 12:27:16 -0600547 xferProcessor.getBlendInfo(&blendInfo);
egdaniel470d77a2016-03-18 12:50:27 -0700548 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
549 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
550 float floatColors[4];
551 if (blend_coeff_refs_constant(srcCoeff) || blend_coeff_refs_constant(dstCoeff)) {
Greg Daniel6c9f1012017-05-11 09:20:59 -0400552 // Swizzle the blend to match what the shader will output.
Chris Dalton46983b72017-06-06 12:27:16 -0600553 const GrSwizzle& swizzle = gpu->caps()->shaderCaps()->configOutputSwizzle(pixelConfig);
Greg Daniel6c9f1012017-05-11 09:20:59 -0400554 GrColor blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
555 GrColorToRGBAFloat(blendConst, floatColors);
egdaniel470d77a2016-03-18 12:50:27 -0700556 } else {
557 memset(floatColors, 0, 4 * sizeof(float));
558 }
559 cmdBuffer->setBlendConstants(gpu, floatColors);
560}