blob: 40a6cf07a9faa5650d306c6d726abb49b4d08842 [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
17static inline const VkFormat& attrib_type_to_vkformat(GrVertexAttribType type) {
18 SkASSERT(type >= 0 && type < kGrVertexAttribTypeCount);
19 static const VkFormat kFormats[kGrVertexAttribTypeCount] = {
20 VK_FORMAT_R32_SFLOAT, // kFloat_GrVertexAttribType
21 VK_FORMAT_R32G32_SFLOAT, // kVec2f_GrVertexAttribType
22 VK_FORMAT_R32G32B32_SFLOAT, // kVec3f_GrVertexAttribType
23 VK_FORMAT_R32G32B32A32_SFLOAT, // kVec4f_GrVertexAttribType
24 VK_FORMAT_R8_UNORM, // kUByte_GrVertexAttribType
25 VK_FORMAT_R8G8B8A8_UNORM, // kVec4ub_GrVertexAttribType
jvanverthf8535942016-02-22 13:05:51 -080026 VK_FORMAT_R16G16_UNORM, // kVec2us_GrVertexAttribType
Greg Daniel164a9f02016-02-22 09:56:40 -050027 };
28 GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType);
29 GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType);
30 GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType);
31 GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType);
32 GR_STATIC_ASSERT(4 == kUByte_GrVertexAttribType);
33 GR_STATIC_ASSERT(5 == kVec4ub_GrVertexAttribType);
jvanverthf8535942016-02-22 13:05:51 -080034 GR_STATIC_ASSERT(6 == kVec2us_GrVertexAttribType);
Greg Daniel164a9f02016-02-22 09:56:40 -050035 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kFormats) == kGrVertexAttribTypeCount);
36 return kFormats[type];
37}
38
39static void setup_vertex_input_state(const GrPrimitiveProcessor& primProc,
40 VkPipelineVertexInputStateCreateInfo* vertexInputInfo,
41 VkVertexInputBindingDescription* bindingDesc,
42 int maxBindingDescCount,
egdanielb05df0f2016-06-27 07:15:20 -070043 VkVertexInputAttributeDescription* attributeDesc) {
Greg Daniel164a9f02016-02-22 09:56:40 -050044 // for now we have only one vertex buffer and one binding
45 memset(bindingDesc, 0, sizeof(VkVertexInputBindingDescription));
46 bindingDesc->binding = 0;
47 bindingDesc->stride = (uint32_t)primProc.getVertexStride();
48 bindingDesc->inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
49
50 // setup attribute descriptions
51 int vaCount = primProc.numAttribs();
Greg Daniel164a9f02016-02-22 09:56:40 -050052 if (vaCount > 0) {
53 size_t offset = 0;
54 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
55 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
56 GrVertexAttribType attribType = attrib.fType;
57
58 VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex];
59 vkAttrib.location = attribIndex; // for now assume location = attribIndex
60 vkAttrib.binding = 0; // for now only one vertex buffer & binding
61 vkAttrib.format = attrib_type_to_vkformat(attribType);
62 vkAttrib.offset = static_cast<uint32_t>(offset);
63 offset += attrib.fOffset;
64 }
65 }
66
67 memset(vertexInputInfo, 0, sizeof(VkPipelineVertexInputStateCreateInfo));
68 vertexInputInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
69 vertexInputInfo->pNext = nullptr;
70 vertexInputInfo->flags = 0;
71 vertexInputInfo->vertexBindingDescriptionCount = 1;
72 vertexInputInfo->pVertexBindingDescriptions = bindingDesc;
73 vertexInputInfo->vertexAttributeDescriptionCount = vaCount;
74 vertexInputInfo->pVertexAttributeDescriptions = attributeDesc;
75}
76
77
78static void setup_input_assembly_state(GrPrimitiveType primitiveType,
79 VkPipelineInputAssemblyStateCreateInfo* inputAssemblyInfo) {
80 static const VkPrimitiveTopology gPrimitiveType2VkTopology[] = {
81 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
82 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
83 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN,
84 VK_PRIMITIVE_TOPOLOGY_POINT_LIST,
85 VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
86 VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
87 };
88
89 memset(inputAssemblyInfo, 0, sizeof(VkPipelineInputAssemblyStateCreateInfo));
90 inputAssemblyInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
91 inputAssemblyInfo->pNext = nullptr;
92 inputAssemblyInfo->flags = 0;
93 inputAssemblyInfo->primitiveRestartEnable = false;
94 inputAssemblyInfo->topology = gPrimitiveType2VkTopology[primitiveType];
95}
96
97
egdanielec440992016-09-13 09:54:11 -070098static VkStencilOp stencil_op_to_vk_stencil_op(GrStencilOp op) {
Greg Daniel164a9f02016-02-22 09:56:40 -050099 static const VkStencilOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700100 VK_STENCIL_OP_KEEP, // kKeep
101 VK_STENCIL_OP_ZERO, // kZero
102 VK_STENCIL_OP_REPLACE, // kReplace
103 VK_STENCIL_OP_INVERT, // kInvert
104 VK_STENCIL_OP_INCREMENT_AND_WRAP, // kIncWrap
105 VK_STENCIL_OP_DECREMENT_AND_WRAP, // kDecWrap
106 VK_STENCIL_OP_INCREMENT_AND_CLAMP, // kIncClamp
107 VK_STENCIL_OP_DECREMENT_AND_CLAMP, // kDecClamp
Greg Daniel164a9f02016-02-22 09:56:40 -0500108 };
cdalton93a379b2016-05-11 13:58:08 -0700109 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilOpCount);
110 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
111 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
112 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
113 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
114 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
115 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
116 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
117 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
118 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
119 return gTable[(int)op];
Greg Daniel164a9f02016-02-22 09:56:40 -0500120}
121
egdanielec440992016-09-13 09:54:11 -0700122static VkCompareOp stencil_func_to_vk_compare_op(GrStencilTest test) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500123 static const VkCompareOp gTable[] = {
cdalton93a379b2016-05-11 13:58:08 -0700124 VK_COMPARE_OP_ALWAYS, // kAlways
125 VK_COMPARE_OP_NEVER, // kNever
126 VK_COMPARE_OP_GREATER, // kGreater
127 VK_COMPARE_OP_GREATER_OR_EQUAL, // kGEqual
128 VK_COMPARE_OP_LESS, // kLess
129 VK_COMPARE_OP_LESS_OR_EQUAL, // kLEqual
130 VK_COMPARE_OP_EQUAL, // kEqual
131 VK_COMPARE_OP_NOT_EQUAL, // kNotEqual
Greg Daniel164a9f02016-02-22 09:56:40 -0500132 };
cdalton93a379b2016-05-11 13:58:08 -0700133 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilTestCount);
134 GR_STATIC_ASSERT(0 == (int)GrStencilTest::kAlways);
135 GR_STATIC_ASSERT(1 == (int)GrStencilTest::kNever);
136 GR_STATIC_ASSERT(2 == (int)GrStencilTest::kGreater);
137 GR_STATIC_ASSERT(3 == (int)GrStencilTest::kGEqual);
138 GR_STATIC_ASSERT(4 == (int)GrStencilTest::kLess);
139 GR_STATIC_ASSERT(5 == (int)GrStencilTest::kLEqual);
140 GR_STATIC_ASSERT(6 == (int)GrStencilTest::kEqual);
141 GR_STATIC_ASSERT(7 == (int)GrStencilTest::kNotEqual);
142 SkASSERT(test < (GrStencilTest)kGrStencilTestCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500143
cdalton93a379b2016-05-11 13:58:08 -0700144 return gTable[(int)test];
Greg Daniel164a9f02016-02-22 09:56:40 -0500145}
146
egdanielec440992016-09-13 09:54:11 -0700147static void setup_depth_stencil_state(const GrStencilSettings& stencilSettings,
148 VkPipelineDepthStencilStateCreateInfo* stencilInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500149 memset(stencilInfo, 0, sizeof(VkPipelineDepthStencilStateCreateInfo));
150 stencilInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
151 stencilInfo->pNext = nullptr;
152 stencilInfo->flags = 0;
153 // set depth testing defaults
154 stencilInfo->depthTestEnable = VK_FALSE;
155 stencilInfo->depthWriteEnable = VK_FALSE;
156 stencilInfo->depthCompareOp = VK_COMPARE_OP_ALWAYS;
157 stencilInfo->depthBoundsTestEnable = VK_FALSE;
158 stencilInfo->stencilTestEnable = !stencilSettings.isDisabled();
159 if (!stencilSettings.isDisabled()) {
160 // Set front face
cdalton93a379b2016-05-11 13:58:08 -0700161 const GrStencilSettings::Face& front = stencilSettings.front();
162 stencilInfo->front.failOp = stencil_op_to_vk_stencil_op(front.fFailOp);
163 stencilInfo->front.passOp = stencil_op_to_vk_stencil_op(front.fPassOp);
Greg Daniel164a9f02016-02-22 09:56:40 -0500164 stencilInfo->front.depthFailOp = stencilInfo->front.failOp;
cdalton93a379b2016-05-11 13:58:08 -0700165 stencilInfo->front.compareOp = stencil_func_to_vk_compare_op(front.fTest);
166 stencilInfo->front.compareMask = front.fTestMask;
167 stencilInfo->front.writeMask = front.fWriteMask;
168 stencilInfo->front.reference = front.fRef;
Greg Daniel164a9f02016-02-22 09:56:40 -0500169
170 // Set back face
cdalton93a379b2016-05-11 13:58:08 -0700171 if (!stencilSettings.isTwoSided()) {
172 stencilInfo->back = stencilInfo->front;
173 } else {
174 const GrStencilSettings::Face& back = stencilSettings.back();
175 stencilInfo->back.failOp = stencil_op_to_vk_stencil_op(back.fFailOp);
176 stencilInfo->back.passOp = stencil_op_to_vk_stencil_op(back.fPassOp);
177 stencilInfo->back.depthFailOp = stencilInfo->front.failOp;
178 stencilInfo->back.compareOp = stencil_func_to_vk_compare_op(back.fTest);
179 stencilInfo->back.compareMask = back.fTestMask;
180 stencilInfo->back.writeMask = back.fWriteMask;
181 stencilInfo->back.reference = back.fRef;
182 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500183 }
184 stencilInfo->minDepthBounds = 0.0f;
185 stencilInfo->maxDepthBounds = 1.0f;
186}
187
egdanielec440992016-09-13 09:54:11 -0700188static void setup_viewport_scissor_state(VkPipelineViewportStateCreateInfo* viewportInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500189 memset(viewportInfo, 0, sizeof(VkPipelineViewportStateCreateInfo));
190 viewportInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
191 viewportInfo->pNext = nullptr;
192 viewportInfo->flags = 0;
193
Greg Daniel164a9f02016-02-22 09:56:40 -0500194 viewportInfo->viewportCount = 1;
egdaniel470d77a2016-03-18 12:50:27 -0700195 viewportInfo->pViewports = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500196
egdaniel470d77a2016-03-18 12:50:27 -0700197 viewportInfo->scissorCount = 1;
198 viewportInfo->pScissors = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500199
Greg Daniel164a9f02016-02-22 09:56:40 -0500200 SkASSERT(viewportInfo->viewportCount == viewportInfo->scissorCount);
201}
202
egdanielec440992016-09-13 09:54:11 -0700203static void setup_multisample_state(const GrPipeline& pipeline,
204 const GrPrimitiveProcessor& primProc,
205 const GrCaps* caps,
206 VkPipelineMultisampleStateCreateInfo* multisampleInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500207 memset(multisampleInfo, 0, sizeof(VkPipelineMultisampleStateCreateInfo));
208 multisampleInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
209 multisampleInfo->pNext = nullptr;
210 multisampleInfo->flags = 0;
211 int numSamples = pipeline.getRenderTarget()->numColorSamples();
212 SkAssertResult(GrSampleCountToVkSampleCount(numSamples,
213 &multisampleInfo->rasterizationSamples));
ethannicholas28ef4452016-03-25 09:26:03 -0700214 float sampleShading = primProc.getSampleShading();
215 SkASSERT(sampleShading == 0.0f || caps->sampleShadingSupport());
216 multisampleInfo->sampleShadingEnable = sampleShading > 0.0f;
217 multisampleInfo->minSampleShading = sampleShading;
Greg Daniel164a9f02016-02-22 09:56:40 -0500218 multisampleInfo->pSampleMask = nullptr;
219 multisampleInfo->alphaToCoverageEnable = VK_FALSE;
220 multisampleInfo->alphaToOneEnable = VK_FALSE;
221}
222
223static VkBlendFactor blend_coeff_to_vk_blend(GrBlendCoeff coeff) {
224 static const VkBlendFactor gTable[] = {
225 VK_BLEND_FACTOR_ZERO, // kZero_GrBlendCoeff
226 VK_BLEND_FACTOR_ONE, // kOne_GrBlendCoeff
227 VK_BLEND_FACTOR_SRC_COLOR, // kSC_GrBlendCoeff
228 VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR, // kISC_GrBlendCoeff
229 VK_BLEND_FACTOR_DST_COLOR, // kDC_GrBlendCoeff
230 VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR, // kIDC_GrBlendCoeff
231 VK_BLEND_FACTOR_SRC_ALPHA, // kSA_GrBlendCoeff
232 VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, // kISA_GrBlendCoeff
233 VK_BLEND_FACTOR_DST_ALPHA, // kDA_GrBlendCoeff
234 VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA, // kIDA_GrBlendCoeff
235 VK_BLEND_FACTOR_CONSTANT_COLOR, // kConstC_GrBlendCoeff
236 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR, // kIConstC_GrBlendCoeff
237 VK_BLEND_FACTOR_CONSTANT_ALPHA, // kConstA_GrBlendCoeff
238 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA, // kIConstA_GrBlendCoeff
239 VK_BLEND_FACTOR_SRC1_COLOR, // kS2C_GrBlendCoeff
240 VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR, // kIS2C_GrBlendCoeff
241 VK_BLEND_FACTOR_SRC1_ALPHA, // kS2A_GrBlendCoeff
242 VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA, // kIS2A_GrBlendCoeff
243
244 };
245 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrBlendCoeffCnt);
246 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
247 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
248 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
249 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
250 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
251 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
252 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
253 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
254 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
255 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
256 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
257 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
258 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
259 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
260 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
261 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
262 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
263 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
264
265 SkASSERT((unsigned)coeff < kGrBlendCoeffCnt);
266 return gTable[coeff];
267}
268
269
270static VkBlendOp blend_equation_to_vk_blend_op(GrBlendEquation equation) {
271 static const VkBlendOp gTable[] = {
272 VK_BLEND_OP_ADD, // kAdd_GrBlendEquation
273 VK_BLEND_OP_SUBTRACT, // kSubtract_GrBlendEquation
274 VK_BLEND_OP_REVERSE_SUBTRACT, // kReverseSubtract_GrBlendEquation
275 };
276 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kFirstAdvancedGrBlendEquation);
277 GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
278 GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
279 GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
280
281 SkASSERT((unsigned)equation < kGrBlendCoeffCnt);
282 return gTable[equation];
283}
284
egdanielec440992016-09-13 09:54:11 -0700285static bool blend_coeff_refs_constant(GrBlendCoeff coeff) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500286 static const bool gCoeffReferencesBlendConst[] = {
287 false,
288 false,
289 false,
290 false,
291 false,
292 false,
293 false,
294 false,
295 false,
296 false,
297 true,
298 true,
299 true,
300 true,
301
302 // extended blend coeffs
303 false,
304 false,
305 false,
306 false,
307 };
308 return gCoeffReferencesBlendConst[coeff];
309 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
310 // Individual enum asserts already made in blend_coeff_to_vk_blend
311}
312
egdanielec440992016-09-13 09:54:11 -0700313static void setup_color_blend_state(const GrPipeline& pipeline,
314 VkPipelineColorBlendStateCreateInfo* colorBlendInfo,
315 VkPipelineColorBlendAttachmentState* attachmentState) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500316 GrXferProcessor::BlendInfo blendInfo;
317 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
318
319 GrBlendEquation equation = blendInfo.fEquation;
320 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
321 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
322 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
323 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
324
325 memset(attachmentState, 0, sizeof(VkPipelineColorBlendAttachmentState));
326 attachmentState->blendEnable = !blendOff;
327 if (!blendOff) {
328 attachmentState->srcColorBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
329 attachmentState->dstColorBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
330 attachmentState->colorBlendOp = blend_equation_to_vk_blend_op(equation);
331 attachmentState->srcAlphaBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
332 attachmentState->dstAlphaBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
333 attachmentState->alphaBlendOp = blend_equation_to_vk_blend_op(equation);
334 }
egdaniel3d5d9ac2016-03-01 12:56:15 -0800335
336 if (!blendInfo.fWriteColor) {
337 attachmentState->colorWriteMask = 0;
338 } else {
339 attachmentState->colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
340 VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
341 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500342
343 memset(colorBlendInfo, 0, sizeof(VkPipelineColorBlendStateCreateInfo));
344 colorBlendInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
345 colorBlendInfo->pNext = nullptr;
346 colorBlendInfo->flags = 0;
347 colorBlendInfo->logicOpEnable = VK_FALSE;
348 colorBlendInfo->attachmentCount = 1;
349 colorBlendInfo->pAttachments = attachmentState;
egdaniel470d77a2016-03-18 12:50:27 -0700350 // colorBlendInfo->blendConstants is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500351}
352
egdanielec440992016-09-13 09:54:11 -0700353static VkCullModeFlags draw_face_to_vk_cull_mode(GrDrawFace drawFace) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500354 // Assumes that we've set the front face to be ccw
355 static const VkCullModeFlags gTable[] = {
356 VK_CULL_MODE_NONE, // kBoth_DrawFace
357 VK_CULL_MODE_BACK_BIT, // kCCW_DrawFace, cull back face
358 VK_CULL_MODE_FRONT_BIT, // kCW_DrawFace, cull front face
359 };
robertphillips5fa7f302016-07-21 09:21:04 -0700360 GR_STATIC_ASSERT(0 == (int)GrDrawFace::kBoth);
361 GR_STATIC_ASSERT(1 == (int)GrDrawFace::kCCW);
362 GR_STATIC_ASSERT(2 == (int)GrDrawFace::kCW);
363 SkASSERT(-1 < (int)drawFace && (int)drawFace <= 2);
Greg Daniel164a9f02016-02-22 09:56:40 -0500364
robertphillips5fa7f302016-07-21 09:21:04 -0700365 return gTable[(int)drawFace];
Greg Daniel164a9f02016-02-22 09:56:40 -0500366}
367
egdanielec440992016-09-13 09:54:11 -0700368static void setup_raster_state(const GrPipeline& pipeline,
369 VkPipelineRasterizationStateCreateInfo* rasterInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500370 memset(rasterInfo, 0, sizeof(VkPipelineRasterizationStateCreateInfo));
371 rasterInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
372 rasterInfo->pNext = nullptr;
373 rasterInfo->flags = 0;
374 rasterInfo->depthClampEnable = VK_FALSE;
375 rasterInfo->rasterizerDiscardEnable = VK_FALSE;
376 rasterInfo->polygonMode = VK_POLYGON_MODE_FILL;
377 rasterInfo->cullMode = draw_face_to_vk_cull_mode(pipeline.getDrawFace());
378 rasterInfo->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
379 rasterInfo->depthBiasEnable = VK_FALSE;
380 rasterInfo->depthBiasConstantFactor = 0.0f;
381 rasterInfo->depthBiasClamp = 0.0f;
382 rasterInfo->depthBiasSlopeFactor = 0.0f;
383 rasterInfo->lineWidth = 1.0f;
384}
385
egdanielec440992016-09-13 09:54:11 -0700386static void setup_dynamic_state(VkPipelineDynamicStateCreateInfo* dynamicInfo,
387 VkDynamicState* dynamicStates) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500388 memset(dynamicInfo, 0, sizeof(VkPipelineDynamicStateCreateInfo));
389 dynamicInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
egdaniel470d77a2016-03-18 12:50:27 -0700390 dynamicInfo->pNext = VK_NULL_HANDLE;
391 dynamicInfo->flags = 0;
392 dynamicStates[0] = VK_DYNAMIC_STATE_VIEWPORT;
393 dynamicStates[1] = VK_DYNAMIC_STATE_SCISSOR;
394 dynamicStates[2] = VK_DYNAMIC_STATE_BLEND_CONSTANTS;
395 dynamicInfo->dynamicStateCount = 3;
396 dynamicInfo->pDynamicStates = dynamicStates;
Greg Daniel164a9f02016-02-22 09:56:40 -0500397}
398
399GrVkPipeline* GrVkPipeline::Create(GrVkGpu* gpu, const GrPipeline& pipeline,
csmartdaltonc633abb2016-11-01 08:55:55 -0700400 const GrStencilSettings& stencil,
Greg Daniel164a9f02016-02-22 09:56:40 -0500401 const GrPrimitiveProcessor& primProc,
402 VkPipelineShaderStageCreateInfo* shaderStageInfo,
403 int shaderStageCount,
404 GrPrimitiveType primitiveType,
405 const GrVkRenderPass& renderPass,
jvanverth03509ea2016-03-02 13:19:47 -0800406 VkPipelineLayout layout,
407 VkPipelineCache cache) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500408 VkPipelineVertexInputStateCreateInfo vertexInputInfo;
409 VkVertexInputBindingDescription bindingDesc;
egdanielb05df0f2016-06-27 07:15:20 -0700410 SkSTArray<16, VkVertexInputAttributeDescription> attributeDesc;
411 SkASSERT(primProc.numAttribs() <= gpu->vkCaps().maxVertexAttributes());
412 VkVertexInputAttributeDescription* pAttribs = attributeDesc.push_back_n(primProc.numAttribs());
413 setup_vertex_input_state(primProc, &vertexInputInfo, &bindingDesc, 1, pAttribs);
Greg Daniel164a9f02016-02-22 09:56:40 -0500414
415 VkPipelineInputAssemblyStateCreateInfo inputAssemblyInfo;
416 setup_input_assembly_state(primitiveType, &inputAssemblyInfo);
417
418 VkPipelineDepthStencilStateCreateInfo depthStencilInfo;
csmartdaltonc633abb2016-11-01 08:55:55 -0700419 setup_depth_stencil_state(stencil, &depthStencilInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500420
Greg Daniel164a9f02016-02-22 09:56:40 -0500421 VkPipelineViewportStateCreateInfo viewportInfo;
egdanielec440992016-09-13 09:54:11 -0700422 setup_viewport_scissor_state(&viewportInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500423
424 VkPipelineMultisampleStateCreateInfo multisampleInfo;
ethannicholas28ef4452016-03-25 09:26:03 -0700425 setup_multisample_state(pipeline, primProc, gpu->caps(), &multisampleInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500426
427 // We will only have one color attachment per pipeline.
428 VkPipelineColorBlendAttachmentState attachmentStates[1];
429 VkPipelineColorBlendStateCreateInfo colorBlendInfo;
egdanielec440992016-09-13 09:54:11 -0700430 setup_color_blend_state(pipeline, &colorBlendInfo, attachmentStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500431
432 VkPipelineRasterizationStateCreateInfo rasterInfo;
egdanielec440992016-09-13 09:54:11 -0700433 setup_raster_state(pipeline, &rasterInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500434
egdaniel470d77a2016-03-18 12:50:27 -0700435 VkDynamicState dynamicStates[3];
Greg Daniel164a9f02016-02-22 09:56:40 -0500436 VkPipelineDynamicStateCreateInfo dynamicInfo;
egdanielec440992016-09-13 09:54:11 -0700437 setup_dynamic_state(&dynamicInfo, dynamicStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500438
439 VkGraphicsPipelineCreateInfo pipelineCreateInfo;
440 memset(&pipelineCreateInfo, 0, sizeof(VkGraphicsPipelineCreateInfo));
441 pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
442 pipelineCreateInfo.pNext = nullptr;
443 pipelineCreateInfo.flags = 0;
444 pipelineCreateInfo.stageCount = shaderStageCount;
445 pipelineCreateInfo.pStages = shaderStageInfo;
446 pipelineCreateInfo.pVertexInputState = &vertexInputInfo;
447 pipelineCreateInfo.pInputAssemblyState = &inputAssemblyInfo;
448 pipelineCreateInfo.pTessellationState = nullptr;
449 pipelineCreateInfo.pViewportState = &viewportInfo;
450 pipelineCreateInfo.pRasterizationState = &rasterInfo;
451 pipelineCreateInfo.pMultisampleState = &multisampleInfo;
452 pipelineCreateInfo.pDepthStencilState = &depthStencilInfo;
453 pipelineCreateInfo.pColorBlendState = &colorBlendInfo;
454 pipelineCreateInfo.pDynamicState = &dynamicInfo;
455 pipelineCreateInfo.layout = layout;
456 pipelineCreateInfo.renderPass = renderPass.vkRenderPass();
457 pipelineCreateInfo.subpass = 0;
458 pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
459 pipelineCreateInfo.basePipelineIndex = -1;
460
461 VkPipeline vkPipeline;
462 VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateGraphicsPipelines(gpu->device(),
halcanary9d524f22016-03-29 09:03:52 -0700463 cache, 1,
464 &pipelineCreateInfo,
Greg Daniel164a9f02016-02-22 09:56:40 -0500465 nullptr, &vkPipeline));
466 if (err) {
467 return nullptr;
468 }
469
470 return new GrVkPipeline(vkPipeline);
471}
472
473void GrVkPipeline::freeGPUData(const GrVkGpu* gpu) const {
474 GR_VK_CALL(gpu->vkInterface(), DestroyPipeline(gpu->device(), fPipeline, nullptr));
475}
476
egdanielec440992016-09-13 09:54:11 -0700477static void set_dynamic_scissor_state(GrVkGpu* gpu,
478 GrVkCommandBuffer* cmdBuffer,
479 const GrPipeline& pipeline,
480 const GrRenderTarget& target) {
egdaniel470d77a2016-03-18 12:50:27 -0700481 // We always use one scissor and if it is disabled we just make it the size of the RT
482 const GrScissorState& scissorState = pipeline.getScissorState();
483 VkRect2D scissor;
484 if (scissorState.enabled() &&
485 !scissorState.rect().contains(0, 0, target.width(), target.height())) {
486 // This all assumes the scissorState has previously been clipped to the device space render
halcanary9d524f22016-03-29 09:03:52 -0700487 // target.
egdaniel0e72e9e2016-07-22 14:41:30 -0700488 scissor.offset.x = SkTMax(scissorState.rect().fLeft, 0);
egdaniel470d77a2016-03-18 12:50:27 -0700489 scissor.extent.width = scissorState.rect().width();
490 if (kTopLeft_GrSurfaceOrigin == target.origin()) {
491 scissor.offset.y = scissorState.rect().fTop;
492 } else {
493 SkASSERT(kBottomLeft_GrSurfaceOrigin == target.origin());
494 scissor.offset.y = target.height() - scissorState.rect().fBottom;
495 }
egdaniel0e72e9e2016-07-22 14:41:30 -0700496 scissor.offset.y = SkTMax(scissor.offset.y, 0);
egdaniel470d77a2016-03-18 12:50:27 -0700497 scissor.extent.height = scissorState.rect().height();
498
499 SkASSERT(scissor.offset.x >= 0);
egdaniel470d77a2016-03-18 12:50:27 -0700500 SkASSERT(scissor.offset.y >= 0);
egdaniel470d77a2016-03-18 12:50:27 -0700501 } else {
502 scissor.extent.width = target.width();
503 scissor.extent.height = target.height();
504 scissor.offset.x = 0;
505 scissor.offset.y = 0;
506 }
507 cmdBuffer->setScissor(gpu, 0, 1, &scissor);
508}
509
egdanielec440992016-09-13 09:54:11 -0700510static void set_dynamic_viewport_state(GrVkGpu* gpu,
511 GrVkCommandBuffer* cmdBuffer,
512 const GrRenderTarget& target) {
egdaniel470d77a2016-03-18 12:50:27 -0700513 // We always use one viewport the size of the RT
514 VkViewport viewport;
515 viewport.x = 0.0f;
516 viewport.y = 0.0f;
517 viewport.width = SkIntToScalar(target.width());
518 viewport.height = SkIntToScalar(target.height());
519 viewport.minDepth = 0.0f;
520 viewport.maxDepth = 1.0f;
521 cmdBuffer->setViewport(gpu, 0, 1, &viewport);
522}
523
egdanielec440992016-09-13 09:54:11 -0700524static void set_dynamic_blend_constant_state(GrVkGpu* gpu,
525 GrVkCommandBuffer* cmdBuffer,
526 const GrPipeline& pipeline) {
egdaniel470d77a2016-03-18 12:50:27 -0700527 GrXferProcessor::BlendInfo blendInfo;
528 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
529 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
530 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
531 float floatColors[4];
532 if (blend_coeff_refs_constant(srcCoeff) || blend_coeff_refs_constant(dstCoeff)) {
533 GrColorToRGBAFloat(blendInfo.fBlendConstant, floatColors);
534 } else {
535 memset(floatColors, 0, 4 * sizeof(float));
536 }
537 cmdBuffer->setBlendConstants(gpu, floatColors);
538}
539
egdaniel470d77a2016-03-18 12:50:27 -0700540void GrVkPipeline::SetDynamicState(GrVkGpu* gpu,
541 GrVkCommandBuffer* cmdBuffer,
542 const GrPipeline& pipeline) {
543 const GrRenderTarget& target = *pipeline.getRenderTarget();
544 set_dynamic_scissor_state(gpu, cmdBuffer, pipeline, target);
545 set_dynamic_viewport_state(gpu, cmdBuffer, target);
546 set_dynamic_blend_constant_state(gpu, cmdBuffer, pipeline);
547}