blob: 809333f7ef24bb4c2f1a8ef2ca48a35ea896e9b5 [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:
20 return VK_FORMAT_R32_SFLOAT;
21 case kVec2f_GrVertexAttribType:
22 return VK_FORMAT_R32G32_SFLOAT;
23 case kVec3f_GrVertexAttribType:
24 return VK_FORMAT_R32G32B32_SFLOAT;
25 case kVec4f_GrVertexAttribType:
26 return VK_FORMAT_R32G32B32A32_SFLOAT;
27 case kVec2i_GrVertexAttribType:
28 return VK_FORMAT_R32G32_SINT;
29 case kVec3i_GrVertexAttribType:
30 return VK_FORMAT_R32G32B32_SINT;
31 case kVec4i_GrVertexAttribType:
32 return VK_FORMAT_R32G32B32A32_SINT;
33 case kUByte_GrVertexAttribType:
34 return VK_FORMAT_R8_UNORM;
35 case kVec4ub_GrVertexAttribType:
36 return VK_FORMAT_R8G8B8A8_UNORM;
37 case kVec2us_GrVertexAttribType:
38 return VK_FORMAT_R16G16_UNORM;
39 case kInt_GrVertexAttribType:
40 return VK_FORMAT_R32_SINT;
41 case kUint_GrVertexAttribType:
42 return VK_FORMAT_R32_UINT;
43 }
44 SkFAIL("Unknown vertex attrib type");
45 return VK_FORMAT_UNDEFINED;
Greg Daniel164a9f02016-02-22 09:56:40 -050046}
47
48static void setup_vertex_input_state(const GrPrimitiveProcessor& primProc,
49 VkPipelineVertexInputStateCreateInfo* vertexInputInfo,
50 VkVertexInputBindingDescription* bindingDesc,
51 int maxBindingDescCount,
egdanielb05df0f2016-06-27 07:15:20 -070052 VkVertexInputAttributeDescription* attributeDesc) {
Greg Daniel164a9f02016-02-22 09:56:40 -050053 // for now we have only one vertex buffer and one binding
54 memset(bindingDesc, 0, sizeof(VkVertexInputBindingDescription));
55 bindingDesc->binding = 0;
56 bindingDesc->stride = (uint32_t)primProc.getVertexStride();
57 bindingDesc->inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
58
59 // setup attribute descriptions
60 int vaCount = primProc.numAttribs();
Greg Daniel164a9f02016-02-22 09:56:40 -050061 if (vaCount > 0) {
62 size_t offset = 0;
63 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
64 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
65 GrVertexAttribType attribType = attrib.fType;
66
67 VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex];
68 vkAttrib.location = attribIndex; // for now assume location = attribIndex
69 vkAttrib.binding = 0; // for now only one vertex buffer & binding
70 vkAttrib.format = attrib_type_to_vkformat(attribType);
71 vkAttrib.offset = static_cast<uint32_t>(offset);
72 offset += attrib.fOffset;
73 }
74 }
75
76 memset(vertexInputInfo, 0, sizeof(VkPipelineVertexInputStateCreateInfo));
77 vertexInputInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
78 vertexInputInfo->pNext = nullptr;
79 vertexInputInfo->flags = 0;
80 vertexInputInfo->vertexBindingDescriptionCount = 1;
81 vertexInputInfo->pVertexBindingDescriptions = bindingDesc;
82 vertexInputInfo->vertexAttributeDescriptionCount = vaCount;
83 vertexInputInfo->pVertexAttributeDescriptions = attributeDesc;
84}
85
86
87static void setup_input_assembly_state(GrPrimitiveType primitiveType,
88 VkPipelineInputAssemblyStateCreateInfo* inputAssemblyInfo) {
89 static const VkPrimitiveTopology gPrimitiveType2VkTopology[] = {
90 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
91 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
92 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN,
93 VK_PRIMITIVE_TOPOLOGY_POINT_LIST,
94 VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
95 VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
96 };
97
98 memset(inputAssemblyInfo, 0, sizeof(VkPipelineInputAssemblyStateCreateInfo));
99 inputAssemblyInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
100 inputAssemblyInfo->pNext = nullptr;
101 inputAssemblyInfo->flags = 0;
102 inputAssemblyInfo->primitiveRestartEnable = false;
103 inputAssemblyInfo->topology = gPrimitiveType2VkTopology[primitiveType];
104}
105
106
egdanielec440992016-09-13 09:54:11 -0700107static VkStencilOp stencil_op_to_vk_stencil_op(GrStencilOp op) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500108 static const VkStencilOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700109 VK_STENCIL_OP_KEEP, // kKeep
110 VK_STENCIL_OP_ZERO, // kZero
111 VK_STENCIL_OP_REPLACE, // kReplace
112 VK_STENCIL_OP_INVERT, // kInvert
113 VK_STENCIL_OP_INCREMENT_AND_WRAP, // kIncWrap
114 VK_STENCIL_OP_DECREMENT_AND_WRAP, // kDecWrap
115 VK_STENCIL_OP_INCREMENT_AND_CLAMP, // kIncClamp
116 VK_STENCIL_OP_DECREMENT_AND_CLAMP, // kDecClamp
Greg Daniel164a9f02016-02-22 09:56:40 -0500117 };
cdalton93a379b2016-05-11 13:58:08 -0700118 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilOpCount);
119 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
120 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
121 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
122 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
123 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
124 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
125 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
126 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
127 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
128 return gTable[(int)op];
Greg Daniel164a9f02016-02-22 09:56:40 -0500129}
130
egdanielec440992016-09-13 09:54:11 -0700131static VkCompareOp stencil_func_to_vk_compare_op(GrStencilTest test) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500132 static const VkCompareOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700133 VK_COMPARE_OP_ALWAYS, // kAlways
134 VK_COMPARE_OP_NEVER, // kNever
135 VK_COMPARE_OP_GREATER, // kGreater
136 VK_COMPARE_OP_GREATER_OR_EQUAL, // kGEqual
137 VK_COMPARE_OP_LESS, // kLess
138 VK_COMPARE_OP_LESS_OR_EQUAL, // kLEqual
139 VK_COMPARE_OP_EQUAL, // kEqual
140 VK_COMPARE_OP_NOT_EQUAL, // kNotEqual
Greg Daniel164a9f02016-02-22 09:56:40 -0500141 };
cdalton93a379b2016-05-11 13:58:08 -0700142 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilTestCount);
143 GR_STATIC_ASSERT(0 == (int)GrStencilTest::kAlways);
144 GR_STATIC_ASSERT(1 == (int)GrStencilTest::kNever);
145 GR_STATIC_ASSERT(2 == (int)GrStencilTest::kGreater);
146 GR_STATIC_ASSERT(3 == (int)GrStencilTest::kGEqual);
147 GR_STATIC_ASSERT(4 == (int)GrStencilTest::kLess);
148 GR_STATIC_ASSERT(5 == (int)GrStencilTest::kLEqual);
149 GR_STATIC_ASSERT(6 == (int)GrStencilTest::kEqual);
150 GR_STATIC_ASSERT(7 == (int)GrStencilTest::kNotEqual);
151 SkASSERT(test < (GrStencilTest)kGrStencilTestCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500152
cdalton93a379b2016-05-11 13:58:08 -0700153 return gTable[(int)test];
Greg Daniel164a9f02016-02-22 09:56:40 -0500154}
155
egdanielec440992016-09-13 09:54:11 -0700156static void setup_depth_stencil_state(const GrStencilSettings& stencilSettings,
157 VkPipelineDepthStencilStateCreateInfo* stencilInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500158 memset(stencilInfo, 0, sizeof(VkPipelineDepthStencilStateCreateInfo));
159 stencilInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
160 stencilInfo->pNext = nullptr;
161 stencilInfo->flags = 0;
162 // set depth testing defaults
163 stencilInfo->depthTestEnable = VK_FALSE;
164 stencilInfo->depthWriteEnable = VK_FALSE;
165 stencilInfo->depthCompareOp = VK_COMPARE_OP_ALWAYS;
166 stencilInfo->depthBoundsTestEnable = VK_FALSE;
167 stencilInfo->stencilTestEnable = !stencilSettings.isDisabled();
168 if (!stencilSettings.isDisabled()) {
169 // Set front face
cdalton93a379b2016-05-11 13:58:08 -0700170 const GrStencilSettings::Face& front = stencilSettings.front();
171 stencilInfo->front.failOp = stencil_op_to_vk_stencil_op(front.fFailOp);
172 stencilInfo->front.passOp = stencil_op_to_vk_stencil_op(front.fPassOp);
Greg Daniel164a9f02016-02-22 09:56:40 -0500173 stencilInfo->front.depthFailOp = stencilInfo->front.failOp;
cdalton93a379b2016-05-11 13:58:08 -0700174 stencilInfo->front.compareOp = stencil_func_to_vk_compare_op(front.fTest);
175 stencilInfo->front.compareMask = front.fTestMask;
176 stencilInfo->front.writeMask = front.fWriteMask;
177 stencilInfo->front.reference = front.fRef;
Greg Daniel164a9f02016-02-22 09:56:40 -0500178
179 // Set back face
cdalton93a379b2016-05-11 13:58:08 -0700180 if (!stencilSettings.isTwoSided()) {
181 stencilInfo->back = stencilInfo->front;
182 } else {
183 const GrStencilSettings::Face& back = stencilSettings.back();
184 stencilInfo->back.failOp = stencil_op_to_vk_stencil_op(back.fFailOp);
185 stencilInfo->back.passOp = stencil_op_to_vk_stencil_op(back.fPassOp);
186 stencilInfo->back.depthFailOp = stencilInfo->front.failOp;
187 stencilInfo->back.compareOp = stencil_func_to_vk_compare_op(back.fTest);
188 stencilInfo->back.compareMask = back.fTestMask;
189 stencilInfo->back.writeMask = back.fWriteMask;
190 stencilInfo->back.reference = back.fRef;
191 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500192 }
193 stencilInfo->minDepthBounds = 0.0f;
194 stencilInfo->maxDepthBounds = 1.0f;
195}
196
egdanielec440992016-09-13 09:54:11 -0700197static void setup_viewport_scissor_state(VkPipelineViewportStateCreateInfo* viewportInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500198 memset(viewportInfo, 0, sizeof(VkPipelineViewportStateCreateInfo));
199 viewportInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
200 viewportInfo->pNext = nullptr;
201 viewportInfo->flags = 0;
202
Greg Daniel164a9f02016-02-22 09:56:40 -0500203 viewportInfo->viewportCount = 1;
egdaniel470d77a2016-03-18 12:50:27 -0700204 viewportInfo->pViewports = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500205
egdaniel470d77a2016-03-18 12:50:27 -0700206 viewportInfo->scissorCount = 1;
207 viewportInfo->pScissors = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500208
Greg Daniel164a9f02016-02-22 09:56:40 -0500209 SkASSERT(viewportInfo->viewportCount == viewportInfo->scissorCount);
210}
211
egdanielec440992016-09-13 09:54:11 -0700212static void setup_multisample_state(const GrPipeline& pipeline,
213 const GrPrimitiveProcessor& primProc,
214 const GrCaps* caps,
215 VkPipelineMultisampleStateCreateInfo* multisampleInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500216 memset(multisampleInfo, 0, sizeof(VkPipelineMultisampleStateCreateInfo));
217 multisampleInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
218 multisampleInfo->pNext = nullptr;
219 multisampleInfo->flags = 0;
220 int numSamples = pipeline.getRenderTarget()->numColorSamples();
221 SkAssertResult(GrSampleCountToVkSampleCount(numSamples,
222 &multisampleInfo->rasterizationSamples));
ethannicholas28ef4452016-03-25 09:26:03 -0700223 float sampleShading = primProc.getSampleShading();
224 SkASSERT(sampleShading == 0.0f || caps->sampleShadingSupport());
225 multisampleInfo->sampleShadingEnable = sampleShading > 0.0f;
226 multisampleInfo->minSampleShading = sampleShading;
Greg Daniel164a9f02016-02-22 09:56:40 -0500227 multisampleInfo->pSampleMask = nullptr;
228 multisampleInfo->alphaToCoverageEnable = VK_FALSE;
229 multisampleInfo->alphaToOneEnable = VK_FALSE;
230}
231
232static VkBlendFactor blend_coeff_to_vk_blend(GrBlendCoeff coeff) {
233 static const VkBlendFactor gTable[] = {
234 VK_BLEND_FACTOR_ZERO, // kZero_GrBlendCoeff
235 VK_BLEND_FACTOR_ONE, // kOne_GrBlendCoeff
236 VK_BLEND_FACTOR_SRC_COLOR, // kSC_GrBlendCoeff
237 VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR, // kISC_GrBlendCoeff
238 VK_BLEND_FACTOR_DST_COLOR, // kDC_GrBlendCoeff
239 VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR, // kIDC_GrBlendCoeff
240 VK_BLEND_FACTOR_SRC_ALPHA, // kSA_GrBlendCoeff
241 VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, // kISA_GrBlendCoeff
242 VK_BLEND_FACTOR_DST_ALPHA, // kDA_GrBlendCoeff
243 VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA, // kIDA_GrBlendCoeff
244 VK_BLEND_FACTOR_CONSTANT_COLOR, // kConstC_GrBlendCoeff
245 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR, // kIConstC_GrBlendCoeff
246 VK_BLEND_FACTOR_CONSTANT_ALPHA, // kConstA_GrBlendCoeff
247 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA, // kIConstA_GrBlendCoeff
248 VK_BLEND_FACTOR_SRC1_COLOR, // kS2C_GrBlendCoeff
249 VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR, // kIS2C_GrBlendCoeff
250 VK_BLEND_FACTOR_SRC1_ALPHA, // kS2A_GrBlendCoeff
251 VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA, // kIS2A_GrBlendCoeff
252
253 };
254 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrBlendCoeffCnt);
255 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
256 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
257 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
258 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
259 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
260 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
261 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
262 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
263 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
264 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
265 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
266 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
267 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
268 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
269 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
270 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
271 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
272 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
273
274 SkASSERT((unsigned)coeff < kGrBlendCoeffCnt);
275 return gTable[coeff];
276}
277
278
279static VkBlendOp blend_equation_to_vk_blend_op(GrBlendEquation equation) {
280 static const VkBlendOp gTable[] = {
281 VK_BLEND_OP_ADD, // kAdd_GrBlendEquation
282 VK_BLEND_OP_SUBTRACT, // kSubtract_GrBlendEquation
283 VK_BLEND_OP_REVERSE_SUBTRACT, // kReverseSubtract_GrBlendEquation
284 };
285 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kFirstAdvancedGrBlendEquation);
286 GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
287 GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
288 GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
289
290 SkASSERT((unsigned)equation < kGrBlendCoeffCnt);
291 return gTable[equation];
292}
293
egdanielec440992016-09-13 09:54:11 -0700294static bool blend_coeff_refs_constant(GrBlendCoeff coeff) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500295 static const bool gCoeffReferencesBlendConst[] = {
296 false,
297 false,
298 false,
299 false,
300 false,
301 false,
302 false,
303 false,
304 false,
305 false,
306 true,
307 true,
308 true,
309 true,
310
311 // extended blend coeffs
312 false,
313 false,
314 false,
315 false,
316 };
317 return gCoeffReferencesBlendConst[coeff];
318 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
319 // Individual enum asserts already made in blend_coeff_to_vk_blend
320}
321
egdanielec440992016-09-13 09:54:11 -0700322static void setup_color_blend_state(const GrPipeline& pipeline,
323 VkPipelineColorBlendStateCreateInfo* colorBlendInfo,
324 VkPipelineColorBlendAttachmentState* attachmentState) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500325 GrXferProcessor::BlendInfo blendInfo;
326 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
327
328 GrBlendEquation equation = blendInfo.fEquation;
329 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
330 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
331 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
332 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
333
334 memset(attachmentState, 0, sizeof(VkPipelineColorBlendAttachmentState));
335 attachmentState->blendEnable = !blendOff;
336 if (!blendOff) {
337 attachmentState->srcColorBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
338 attachmentState->dstColorBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
339 attachmentState->colorBlendOp = blend_equation_to_vk_blend_op(equation);
340 attachmentState->srcAlphaBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
341 attachmentState->dstAlphaBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
342 attachmentState->alphaBlendOp = blend_equation_to_vk_blend_op(equation);
343 }
egdaniel3d5d9ac2016-03-01 12:56:15 -0800344
345 if (!blendInfo.fWriteColor) {
346 attachmentState->colorWriteMask = 0;
347 } else {
348 attachmentState->colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
349 VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
350 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500351
352 memset(colorBlendInfo, 0, sizeof(VkPipelineColorBlendStateCreateInfo));
353 colorBlendInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
354 colorBlendInfo->pNext = nullptr;
355 colorBlendInfo->flags = 0;
356 colorBlendInfo->logicOpEnable = VK_FALSE;
357 colorBlendInfo->attachmentCount = 1;
358 colorBlendInfo->pAttachments = attachmentState;
egdaniel470d77a2016-03-18 12:50:27 -0700359 // colorBlendInfo->blendConstants is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500360}
361
egdanielec440992016-09-13 09:54:11 -0700362static void setup_raster_state(const GrPipeline& pipeline,
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400363 const GrCaps* caps,
egdanielec440992016-09-13 09:54:11 -0700364 VkPipelineRasterizationStateCreateInfo* rasterInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500365 memset(rasterInfo, 0, sizeof(VkPipelineRasterizationStateCreateInfo));
366 rasterInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
367 rasterInfo->pNext = nullptr;
368 rasterInfo->flags = 0;
369 rasterInfo->depthClampEnable = VK_FALSE;
370 rasterInfo->rasterizerDiscardEnable = VK_FALSE;
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400371 rasterInfo->polygonMode = caps->wireframeMode() ? VK_POLYGON_MODE_LINE
372 : VK_POLYGON_MODE_FILL;
Brian Salomonf0861672017-05-08 11:10:10 -0400373 rasterInfo->cullMode = VK_CULL_MODE_NONE;
Greg Daniel164a9f02016-02-22 09:56:40 -0500374 rasterInfo->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
375 rasterInfo->depthBiasEnable = VK_FALSE;
376 rasterInfo->depthBiasConstantFactor = 0.0f;
377 rasterInfo->depthBiasClamp = 0.0f;
378 rasterInfo->depthBiasSlopeFactor = 0.0f;
379 rasterInfo->lineWidth = 1.0f;
380}
381
egdanielec440992016-09-13 09:54:11 -0700382static void setup_dynamic_state(VkPipelineDynamicStateCreateInfo* dynamicInfo,
383 VkDynamicState* dynamicStates) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500384 memset(dynamicInfo, 0, sizeof(VkPipelineDynamicStateCreateInfo));
385 dynamicInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
egdaniel470d77a2016-03-18 12:50:27 -0700386 dynamicInfo->pNext = VK_NULL_HANDLE;
387 dynamicInfo->flags = 0;
388 dynamicStates[0] = VK_DYNAMIC_STATE_VIEWPORT;
389 dynamicStates[1] = VK_DYNAMIC_STATE_SCISSOR;
390 dynamicStates[2] = VK_DYNAMIC_STATE_BLEND_CONSTANTS;
391 dynamicInfo->dynamicStateCount = 3;
392 dynamicInfo->pDynamicStates = dynamicStates;
Greg Daniel164a9f02016-02-22 09:56:40 -0500393}
394
395GrVkPipeline* GrVkPipeline::Create(GrVkGpu* gpu, const GrPipeline& pipeline,
csmartdaltonc633abb2016-11-01 08:55:55 -0700396 const GrStencilSettings& stencil,
Greg Daniel164a9f02016-02-22 09:56:40 -0500397 const GrPrimitiveProcessor& primProc,
398 VkPipelineShaderStageCreateInfo* shaderStageInfo,
399 int shaderStageCount,
400 GrPrimitiveType primitiveType,
401 const GrVkRenderPass& renderPass,
jvanverth03509ea2016-03-02 13:19:47 -0800402 VkPipelineLayout layout,
403 VkPipelineCache cache) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500404 VkPipelineVertexInputStateCreateInfo vertexInputInfo;
405 VkVertexInputBindingDescription bindingDesc;
egdanielb05df0f2016-06-27 07:15:20 -0700406 SkSTArray<16, VkVertexInputAttributeDescription> attributeDesc;
407 SkASSERT(primProc.numAttribs() <= gpu->vkCaps().maxVertexAttributes());
408 VkVertexInputAttributeDescription* pAttribs = attributeDesc.push_back_n(primProc.numAttribs());
409 setup_vertex_input_state(primProc, &vertexInputInfo, &bindingDesc, 1, pAttribs);
Greg Daniel164a9f02016-02-22 09:56:40 -0500410
411 VkPipelineInputAssemblyStateCreateInfo inputAssemblyInfo;
412 setup_input_assembly_state(primitiveType, &inputAssemblyInfo);
413
414 VkPipelineDepthStencilStateCreateInfo depthStencilInfo;
csmartdaltonc633abb2016-11-01 08:55:55 -0700415 setup_depth_stencil_state(stencil, &depthStencilInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500416
Greg Daniel164a9f02016-02-22 09:56:40 -0500417 VkPipelineViewportStateCreateInfo viewportInfo;
egdanielec440992016-09-13 09:54:11 -0700418 setup_viewport_scissor_state(&viewportInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500419
420 VkPipelineMultisampleStateCreateInfo multisampleInfo;
ethannicholas28ef4452016-03-25 09:26:03 -0700421 setup_multisample_state(pipeline, primProc, gpu->caps(), &multisampleInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500422
423 // We will only have one color attachment per pipeline.
424 VkPipelineColorBlendAttachmentState attachmentStates[1];
425 VkPipelineColorBlendStateCreateInfo colorBlendInfo;
egdanielec440992016-09-13 09:54:11 -0700426 setup_color_blend_state(pipeline, &colorBlendInfo, attachmentStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500427
428 VkPipelineRasterizationStateCreateInfo rasterInfo;
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400429 setup_raster_state(pipeline, gpu->caps(), &rasterInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500430
egdaniel470d77a2016-03-18 12:50:27 -0700431 VkDynamicState dynamicStates[3];
Greg Daniel164a9f02016-02-22 09:56:40 -0500432 VkPipelineDynamicStateCreateInfo dynamicInfo;
egdanielec440992016-09-13 09:54:11 -0700433 setup_dynamic_state(&dynamicInfo, dynamicStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500434
435 VkGraphicsPipelineCreateInfo pipelineCreateInfo;
436 memset(&pipelineCreateInfo, 0, sizeof(VkGraphicsPipelineCreateInfo));
437 pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
438 pipelineCreateInfo.pNext = nullptr;
439 pipelineCreateInfo.flags = 0;
440 pipelineCreateInfo.stageCount = shaderStageCount;
441 pipelineCreateInfo.pStages = shaderStageInfo;
442 pipelineCreateInfo.pVertexInputState = &vertexInputInfo;
443 pipelineCreateInfo.pInputAssemblyState = &inputAssemblyInfo;
444 pipelineCreateInfo.pTessellationState = nullptr;
445 pipelineCreateInfo.pViewportState = &viewportInfo;
446 pipelineCreateInfo.pRasterizationState = &rasterInfo;
447 pipelineCreateInfo.pMultisampleState = &multisampleInfo;
448 pipelineCreateInfo.pDepthStencilState = &depthStencilInfo;
449 pipelineCreateInfo.pColorBlendState = &colorBlendInfo;
450 pipelineCreateInfo.pDynamicState = &dynamicInfo;
451 pipelineCreateInfo.layout = layout;
452 pipelineCreateInfo.renderPass = renderPass.vkRenderPass();
453 pipelineCreateInfo.subpass = 0;
454 pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
455 pipelineCreateInfo.basePipelineIndex = -1;
456
457 VkPipeline vkPipeline;
458 VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateGraphicsPipelines(gpu->device(),
halcanary9d524f22016-03-29 09:03:52 -0700459 cache, 1,
460 &pipelineCreateInfo,
Greg Daniel164a9f02016-02-22 09:56:40 -0500461 nullptr, &vkPipeline));
462 if (err) {
463 return nullptr;
464 }
465
466 return new GrVkPipeline(vkPipeline);
467}
468
469void GrVkPipeline::freeGPUData(const GrVkGpu* gpu) const {
470 GR_VK_CALL(gpu->vkInterface(), DestroyPipeline(gpu->device(), fPipeline, nullptr));
471}
472
egdanielec440992016-09-13 09:54:11 -0700473static void set_dynamic_scissor_state(GrVkGpu* gpu,
474 GrVkCommandBuffer* cmdBuffer,
475 const GrPipeline& pipeline,
476 const GrRenderTarget& target) {
egdaniel470d77a2016-03-18 12:50:27 -0700477 // We always use one scissor and if it is disabled we just make it the size of the RT
478 const GrScissorState& scissorState = pipeline.getScissorState();
479 VkRect2D scissor;
480 if (scissorState.enabled() &&
481 !scissorState.rect().contains(0, 0, target.width(), target.height())) {
482 // This all assumes the scissorState has previously been clipped to the device space render
halcanary9d524f22016-03-29 09:03:52 -0700483 // target.
egdaniel0e72e9e2016-07-22 14:41:30 -0700484 scissor.offset.x = SkTMax(scissorState.rect().fLeft, 0);
egdaniel470d77a2016-03-18 12:50:27 -0700485 scissor.extent.width = scissorState.rect().width();
486 if (kTopLeft_GrSurfaceOrigin == target.origin()) {
487 scissor.offset.y = scissorState.rect().fTop;
488 } else {
489 SkASSERT(kBottomLeft_GrSurfaceOrigin == target.origin());
490 scissor.offset.y = target.height() - scissorState.rect().fBottom;
491 }
egdaniel0e72e9e2016-07-22 14:41:30 -0700492 scissor.offset.y = SkTMax(scissor.offset.y, 0);
egdaniel470d77a2016-03-18 12:50:27 -0700493 scissor.extent.height = scissorState.rect().height();
494
495 SkASSERT(scissor.offset.x >= 0);
egdaniel470d77a2016-03-18 12:50:27 -0700496 SkASSERT(scissor.offset.y >= 0);
egdaniel470d77a2016-03-18 12:50:27 -0700497 } else {
498 scissor.extent.width = target.width();
499 scissor.extent.height = target.height();
500 scissor.offset.x = 0;
501 scissor.offset.y = 0;
502 }
503 cmdBuffer->setScissor(gpu, 0, 1, &scissor);
504}
505
egdanielec440992016-09-13 09:54:11 -0700506static void set_dynamic_viewport_state(GrVkGpu* gpu,
507 GrVkCommandBuffer* cmdBuffer,
508 const GrRenderTarget& target) {
egdaniel470d77a2016-03-18 12:50:27 -0700509 // We always use one viewport the size of the RT
510 VkViewport viewport;
511 viewport.x = 0.0f;
512 viewport.y = 0.0f;
513 viewport.width = SkIntToScalar(target.width());
514 viewport.height = SkIntToScalar(target.height());
515 viewport.minDepth = 0.0f;
516 viewport.maxDepth = 1.0f;
517 cmdBuffer->setViewport(gpu, 0, 1, &viewport);
518}
519
egdanielec440992016-09-13 09:54:11 -0700520static void set_dynamic_blend_constant_state(GrVkGpu* gpu,
521 GrVkCommandBuffer* cmdBuffer,
522 const GrPipeline& pipeline) {
egdaniel470d77a2016-03-18 12:50:27 -0700523 GrXferProcessor::BlendInfo blendInfo;
524 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
525 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
526 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
527 float floatColors[4];
528 if (blend_coeff_refs_constant(srcCoeff) || blend_coeff_refs_constant(dstCoeff)) {
529 GrColorToRGBAFloat(blendInfo.fBlendConstant, floatColors);
530 } else {
531 memset(floatColors, 0, 4 * sizeof(float));
532 }
533 cmdBuffer->setBlendConstants(gpu, floatColors);
534}
535
egdaniel470d77a2016-03-18 12:50:27 -0700536void GrVkPipeline::SetDynamicState(GrVkGpu* gpu,
537 GrVkCommandBuffer* cmdBuffer,
538 const GrPipeline& pipeline) {
539 const GrRenderTarget& target = *pipeline.getRenderTarget();
540 set_dynamic_scissor_state(gpu, cmdBuffer, pipeline, target);
541 set_dynamic_viewport_state(gpu, cmdBuffer, target);
542 set_dynamic_blend_constant_state(gpu, cmdBuffer, pipeline);
543}