blob: 53e9dba08f7e63faaeccfc05dd020632c97a408b [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"
14#include "GrVkProgramDesc.h"
15#include "GrVkRenderTarget.h"
16#include "GrVkUtil.h"
17
18static inline const VkFormat& attrib_type_to_vkformat(GrVertexAttribType type) {
19 SkASSERT(type >= 0 && type < kGrVertexAttribTypeCount);
20 static const VkFormat kFormats[kGrVertexAttribTypeCount] = {
21 VK_FORMAT_R32_SFLOAT, // kFloat_GrVertexAttribType
22 VK_FORMAT_R32G32_SFLOAT, // kVec2f_GrVertexAttribType
23 VK_FORMAT_R32G32B32_SFLOAT, // kVec3f_GrVertexAttribType
24 VK_FORMAT_R32G32B32A32_SFLOAT, // kVec4f_GrVertexAttribType
25 VK_FORMAT_R8_UNORM, // kUByte_GrVertexAttribType
26 VK_FORMAT_R8G8B8A8_UNORM, // kVec4ub_GrVertexAttribType
jvanverthf8535942016-02-22 13:05:51 -080027 VK_FORMAT_R16G16_UNORM, // kVec2us_GrVertexAttribType
Greg Daniel164a9f02016-02-22 09:56:40 -050028 };
29 GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType);
30 GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType);
31 GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType);
32 GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType);
33 GR_STATIC_ASSERT(4 == kUByte_GrVertexAttribType);
34 GR_STATIC_ASSERT(5 == kVec4ub_GrVertexAttribType);
jvanverthf8535942016-02-22 13:05:51 -080035 GR_STATIC_ASSERT(6 == kVec2us_GrVertexAttribType);
Greg Daniel164a9f02016-02-22 09:56:40 -050036 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kFormats) == kGrVertexAttribTypeCount);
37 return kFormats[type];
38}
39
40static void setup_vertex_input_state(const GrPrimitiveProcessor& primProc,
41 VkPipelineVertexInputStateCreateInfo* vertexInputInfo,
42 VkVertexInputBindingDescription* bindingDesc,
43 int maxBindingDescCount,
44 VkVertexInputAttributeDescription* attributeDesc,
45 int maxAttributeDescCount) {
46 // for now we have only one vertex buffer and one binding
47 memset(bindingDesc, 0, sizeof(VkVertexInputBindingDescription));
48 bindingDesc->binding = 0;
49 bindingDesc->stride = (uint32_t)primProc.getVertexStride();
50 bindingDesc->inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
51
52 // setup attribute descriptions
53 int vaCount = primProc.numAttribs();
54 SkASSERT(vaCount < maxAttributeDescCount);
55 if (vaCount > 0) {
56 size_t offset = 0;
57 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
58 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
59 GrVertexAttribType attribType = attrib.fType;
60
61 VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex];
62 vkAttrib.location = attribIndex; // for now assume location = attribIndex
63 vkAttrib.binding = 0; // for now only one vertex buffer & binding
64 vkAttrib.format = attrib_type_to_vkformat(attribType);
65 vkAttrib.offset = static_cast<uint32_t>(offset);
66 offset += attrib.fOffset;
67 }
68 }
69
70 memset(vertexInputInfo, 0, sizeof(VkPipelineVertexInputStateCreateInfo));
71 vertexInputInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
72 vertexInputInfo->pNext = nullptr;
73 vertexInputInfo->flags = 0;
74 vertexInputInfo->vertexBindingDescriptionCount = 1;
75 vertexInputInfo->pVertexBindingDescriptions = bindingDesc;
76 vertexInputInfo->vertexAttributeDescriptionCount = vaCount;
77 vertexInputInfo->pVertexAttributeDescriptions = attributeDesc;
78}
79
80
81static void setup_input_assembly_state(GrPrimitiveType primitiveType,
82 VkPipelineInputAssemblyStateCreateInfo* inputAssemblyInfo) {
83 static const VkPrimitiveTopology gPrimitiveType2VkTopology[] = {
84 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
85 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
86 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN,
87 VK_PRIMITIVE_TOPOLOGY_POINT_LIST,
88 VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
89 VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
90 };
91
92 memset(inputAssemblyInfo, 0, sizeof(VkPipelineInputAssemblyStateCreateInfo));
93 inputAssemblyInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
94 inputAssemblyInfo->pNext = nullptr;
95 inputAssemblyInfo->flags = 0;
96 inputAssemblyInfo->primitiveRestartEnable = false;
97 inputAssemblyInfo->topology = gPrimitiveType2VkTopology[primitiveType];
98}
99
100
101VkStencilOp stencil_op_to_vk_stencil_op(GrStencilOp op) {
102 static const VkStencilOp gTable[] = {
103 VK_STENCIL_OP_KEEP, // kKeep_StencilOp
104 VK_STENCIL_OP_REPLACE, // kReplace_StencilOp
105 VK_STENCIL_OP_INCREMENT_AND_WRAP, // kIncWrap_StencilOp
106 VK_STENCIL_OP_INCREMENT_AND_CLAMP, // kIncClamp_StencilOp
107 VK_STENCIL_OP_DECREMENT_AND_WRAP, // kDecWrap_StencilOp
108 VK_STENCIL_OP_DECREMENT_AND_CLAMP, // kDecClamp_StencilOp
109 VK_STENCIL_OP_ZERO, // kZero_StencilOp
110 VK_STENCIL_OP_INVERT, // kInvert_StencilOp
111 };
112 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kStencilOpCount);
113 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
114 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
115 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
116 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
117 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
118 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
119 GR_STATIC_ASSERT(6 == kZero_StencilOp);
120 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
121 SkASSERT((unsigned)op < kStencilOpCount);
122 return gTable[op];
123}
124
125VkCompareOp stencil_func_to_vk_compare_op(GrStencilFunc basicFunc) {
126 static const VkCompareOp gTable[] = {
127 VK_COMPARE_OP_ALWAYS, // kAlways_StencilFunc
128 VK_COMPARE_OP_NEVER, // kNever_StencilFunc
129 VK_COMPARE_OP_GREATER, // kGreater_StencilFunc
130 VK_COMPARE_OP_GREATER_OR_EQUAL, // kGEqual_StencilFunc
131 VK_COMPARE_OP_LESS, // kLess_StencilFunc
132 VK_COMPARE_OP_LESS_OR_EQUAL, // kLEqual_StencilFunc,
133 VK_COMPARE_OP_EQUAL, // kEqual_StencilFunc,
134 VK_COMPARE_OP_NOT_EQUAL, // kNotEqual_StencilFunc,
135 };
136 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
137 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
138 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
139 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
140 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
141 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
142 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
143 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
144 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
145 SkASSERT((unsigned)basicFunc < kBasicStencilFuncCount);
146
147 return gTable[basicFunc];
148}
149
150void setup_depth_stencil_state(const GrVkGpu* gpu,
151 const GrStencilSettings& stencilSettings,
152 VkPipelineDepthStencilStateCreateInfo* stencilInfo) {
153 memset(stencilInfo, 0, sizeof(VkPipelineDepthStencilStateCreateInfo));
154 stencilInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
155 stencilInfo->pNext = nullptr;
156 stencilInfo->flags = 0;
157 // set depth testing defaults
158 stencilInfo->depthTestEnable = VK_FALSE;
159 stencilInfo->depthWriteEnable = VK_FALSE;
160 stencilInfo->depthCompareOp = VK_COMPARE_OP_ALWAYS;
161 stencilInfo->depthBoundsTestEnable = VK_FALSE;
162 stencilInfo->stencilTestEnable = !stencilSettings.isDisabled();
163 if (!stencilSettings.isDisabled()) {
164 // Set front face
165 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
166 stencilInfo->front.failOp = stencil_op_to_vk_stencil_op(stencilSettings.failOp(face));
167 stencilInfo->front.passOp = stencil_op_to_vk_stencil_op(stencilSettings.passOp(face));
168 stencilInfo->front.depthFailOp = stencilInfo->front.failOp;
169 stencilInfo->front.compareOp = stencil_func_to_vk_compare_op(stencilSettings.func(face));
170 stencilInfo->front.compareMask = stencilSettings.funcMask(face);
egdaniel3d5d9ac2016-03-01 12:56:15 -0800171 stencilInfo->front.writeMask = stencilSettings.writeMask(face);
172 stencilInfo->front.reference = stencilSettings.funcRef(face);
Greg Daniel164a9f02016-02-22 09:56:40 -0500173
174 // Set back face
175 face = GrStencilSettings::kBack_Face;
176 stencilInfo->back.failOp = stencil_op_to_vk_stencil_op(stencilSettings.failOp(face));
177 stencilInfo->back.passOp = stencil_op_to_vk_stencil_op(stencilSettings.passOp(face));
178 stencilInfo->back.depthFailOp = stencilInfo->front.failOp;
179 stencilInfo->back.compareOp = stencil_func_to_vk_compare_op(stencilSettings.func(face));
180 stencilInfo->back.compareMask = stencilSettings.funcMask(face);
egdaniel3d5d9ac2016-03-01 12:56:15 -0800181 stencilInfo->back.writeMask = stencilSettings.writeMask(face);
182 stencilInfo->back.reference = stencilSettings.funcRef(face);
Greg Daniel164a9f02016-02-22 09:56:40 -0500183 }
184 stencilInfo->minDepthBounds = 0.0f;
185 stencilInfo->maxDepthBounds = 1.0f;
186}
187
188void setup_viewport_scissor_state(const GrVkGpu* gpu,
189 const GrPipeline& pipeline,
190 const GrVkRenderTarget* vkRT,
egdaniel470d77a2016-03-18 12:50:27 -0700191 VkPipelineViewportStateCreateInfo* viewportInfo) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500192 memset(viewportInfo, 0, sizeof(VkPipelineViewportStateCreateInfo));
193 viewportInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
194 viewportInfo->pNext = nullptr;
195 viewportInfo->flags = 0;
196
Greg Daniel164a9f02016-02-22 09:56:40 -0500197 viewportInfo->viewportCount = 1;
egdaniel470d77a2016-03-18 12:50:27 -0700198 viewportInfo->pViewports = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500199
egdaniel470d77a2016-03-18 12:50:27 -0700200 viewportInfo->scissorCount = 1;
201 viewportInfo->pScissors = nullptr; // This is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500202
Greg Daniel164a9f02016-02-22 09:56:40 -0500203 SkASSERT(viewportInfo->viewportCount == viewportInfo->scissorCount);
204}
205
halcanary9d524f22016-03-29 09:03:52 -0700206void setup_multisample_state(const GrPipeline& pipeline,
ethannicholas28ef4452016-03-25 09:26:03 -0700207 const GrPrimitiveProcessor& primProc,
208 const GrCaps* caps,
Greg Daniel164a9f02016-02-22 09:56:40 -0500209 VkPipelineMultisampleStateCreateInfo* multisampleInfo) {
210 memset(multisampleInfo, 0, sizeof(VkPipelineMultisampleStateCreateInfo));
211 multisampleInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
212 multisampleInfo->pNext = nullptr;
213 multisampleInfo->flags = 0;
214 int numSamples = pipeline.getRenderTarget()->numColorSamples();
215 SkAssertResult(GrSampleCountToVkSampleCount(numSamples,
216 &multisampleInfo->rasterizationSamples));
ethannicholas28ef4452016-03-25 09:26:03 -0700217 float sampleShading = primProc.getSampleShading();
218 SkASSERT(sampleShading == 0.0f || caps->sampleShadingSupport());
219 multisampleInfo->sampleShadingEnable = sampleShading > 0.0f;
220 multisampleInfo->minSampleShading = sampleShading;
Greg Daniel164a9f02016-02-22 09:56:40 -0500221 multisampleInfo->pSampleMask = nullptr;
222 multisampleInfo->alphaToCoverageEnable = VK_FALSE;
223 multisampleInfo->alphaToOneEnable = VK_FALSE;
224}
225
226static VkBlendFactor blend_coeff_to_vk_blend(GrBlendCoeff coeff) {
227 static const VkBlendFactor gTable[] = {
228 VK_BLEND_FACTOR_ZERO, // kZero_GrBlendCoeff
229 VK_BLEND_FACTOR_ONE, // kOne_GrBlendCoeff
230 VK_BLEND_FACTOR_SRC_COLOR, // kSC_GrBlendCoeff
231 VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR, // kISC_GrBlendCoeff
232 VK_BLEND_FACTOR_DST_COLOR, // kDC_GrBlendCoeff
233 VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR, // kIDC_GrBlendCoeff
234 VK_BLEND_FACTOR_SRC_ALPHA, // kSA_GrBlendCoeff
235 VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, // kISA_GrBlendCoeff
236 VK_BLEND_FACTOR_DST_ALPHA, // kDA_GrBlendCoeff
237 VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA, // kIDA_GrBlendCoeff
238 VK_BLEND_FACTOR_CONSTANT_COLOR, // kConstC_GrBlendCoeff
239 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR, // kIConstC_GrBlendCoeff
240 VK_BLEND_FACTOR_CONSTANT_ALPHA, // kConstA_GrBlendCoeff
241 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA, // kIConstA_GrBlendCoeff
242 VK_BLEND_FACTOR_SRC1_COLOR, // kS2C_GrBlendCoeff
243 VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR, // kIS2C_GrBlendCoeff
244 VK_BLEND_FACTOR_SRC1_ALPHA, // kS2A_GrBlendCoeff
245 VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA, // kIS2A_GrBlendCoeff
246
247 };
248 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrBlendCoeffCnt);
249 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
250 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
251 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
252 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
253 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
254 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
255 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
256 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
257 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
258 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
259 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
260 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
261 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
262 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
263 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
264 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
265 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
266 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
267
268 SkASSERT((unsigned)coeff < kGrBlendCoeffCnt);
269 return gTable[coeff];
270}
271
272
273static VkBlendOp blend_equation_to_vk_blend_op(GrBlendEquation equation) {
274 static const VkBlendOp gTable[] = {
275 VK_BLEND_OP_ADD, // kAdd_GrBlendEquation
276 VK_BLEND_OP_SUBTRACT, // kSubtract_GrBlendEquation
277 VK_BLEND_OP_REVERSE_SUBTRACT, // kReverseSubtract_GrBlendEquation
278 };
279 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kFirstAdvancedGrBlendEquation);
280 GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
281 GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
282 GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
283
284 SkASSERT((unsigned)equation < kGrBlendCoeffCnt);
285 return gTable[equation];
286}
287
288bool blend_coeff_refs_constant(GrBlendCoeff coeff) {
289 static const bool gCoeffReferencesBlendConst[] = {
290 false,
291 false,
292 false,
293 false,
294 false,
295 false,
296 false,
297 false,
298 false,
299 false,
300 true,
301 true,
302 true,
303 true,
304
305 // extended blend coeffs
306 false,
307 false,
308 false,
309 false,
310 };
311 return gCoeffReferencesBlendConst[coeff];
312 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
313 // Individual enum asserts already made in blend_coeff_to_vk_blend
314}
315
316void setup_color_blend_state(const GrVkGpu* gpu,
317 const GrPipeline& pipeline,
318 VkPipelineColorBlendStateCreateInfo* colorBlendInfo,
319 VkPipelineColorBlendAttachmentState* attachmentState) {
320 GrXferProcessor::BlendInfo blendInfo;
321 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
322
323 GrBlendEquation equation = blendInfo.fEquation;
324 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
325 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
326 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
327 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
328
329 memset(attachmentState, 0, sizeof(VkPipelineColorBlendAttachmentState));
330 attachmentState->blendEnable = !blendOff;
331 if (!blendOff) {
332 attachmentState->srcColorBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
333 attachmentState->dstColorBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
334 attachmentState->colorBlendOp = blend_equation_to_vk_blend_op(equation);
335 attachmentState->srcAlphaBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
336 attachmentState->dstAlphaBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
337 attachmentState->alphaBlendOp = blend_equation_to_vk_blend_op(equation);
338 }
egdaniel3d5d9ac2016-03-01 12:56:15 -0800339
340 if (!blendInfo.fWriteColor) {
341 attachmentState->colorWriteMask = 0;
342 } else {
343 attachmentState->colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
344 VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
345 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500346
347 memset(colorBlendInfo, 0, sizeof(VkPipelineColorBlendStateCreateInfo));
348 colorBlendInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
349 colorBlendInfo->pNext = nullptr;
350 colorBlendInfo->flags = 0;
351 colorBlendInfo->logicOpEnable = VK_FALSE;
352 colorBlendInfo->attachmentCount = 1;
353 colorBlendInfo->pAttachments = attachmentState;
egdaniel470d77a2016-03-18 12:50:27 -0700354 // colorBlendInfo->blendConstants is set dynamically
Greg Daniel164a9f02016-02-22 09:56:40 -0500355}
356
357VkCullModeFlags draw_face_to_vk_cull_mode(GrPipelineBuilder::DrawFace drawFace) {
358 // Assumes that we've set the front face to be ccw
359 static const VkCullModeFlags gTable[] = {
360 VK_CULL_MODE_NONE, // kBoth_DrawFace
361 VK_CULL_MODE_BACK_BIT, // kCCW_DrawFace, cull back face
362 VK_CULL_MODE_FRONT_BIT, // kCW_DrawFace, cull front face
363 };
364 GR_STATIC_ASSERT(0 == GrPipelineBuilder::kBoth_DrawFace);
365 GR_STATIC_ASSERT(1 == GrPipelineBuilder::kCCW_DrawFace);
366 GR_STATIC_ASSERT(2 == GrPipelineBuilder::kCW_DrawFace);
367 SkASSERT((unsigned)drawFace <= 2);
368
369 return gTable[drawFace];
370}
371
372void setup_raster_state(const GrVkGpu* gpu,
373 const GrPipeline& pipeline,
374 VkPipelineRasterizationStateCreateInfo* rasterInfo) {
375 memset(rasterInfo, 0, sizeof(VkPipelineRasterizationStateCreateInfo));
376 rasterInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
377 rasterInfo->pNext = nullptr;
378 rasterInfo->flags = 0;
379 rasterInfo->depthClampEnable = VK_FALSE;
380 rasterInfo->rasterizerDiscardEnable = VK_FALSE;
381 rasterInfo->polygonMode = VK_POLYGON_MODE_FILL;
382 rasterInfo->cullMode = draw_face_to_vk_cull_mode(pipeline.getDrawFace());
383 rasterInfo->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
384 rasterInfo->depthBiasEnable = VK_FALSE;
385 rasterInfo->depthBiasConstantFactor = 0.0f;
386 rasterInfo->depthBiasClamp = 0.0f;
387 rasterInfo->depthBiasSlopeFactor = 0.0f;
388 rasterInfo->lineWidth = 1.0f;
389}
390
391void setup_dynamic_state(const GrVkGpu* gpu,
392 const GrPipeline& pipeline,
egdaniel470d77a2016-03-18 12:50:27 -0700393 VkPipelineDynamicStateCreateInfo* dynamicInfo,
394 VkDynamicState* dynamicStates) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500395 memset(dynamicInfo, 0, sizeof(VkPipelineDynamicStateCreateInfo));
396 dynamicInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
egdaniel470d77a2016-03-18 12:50:27 -0700397 dynamicInfo->pNext = VK_NULL_HANDLE;
398 dynamicInfo->flags = 0;
399 dynamicStates[0] = VK_DYNAMIC_STATE_VIEWPORT;
400 dynamicStates[1] = VK_DYNAMIC_STATE_SCISSOR;
401 dynamicStates[2] = VK_DYNAMIC_STATE_BLEND_CONSTANTS;
402 dynamicInfo->dynamicStateCount = 3;
403 dynamicInfo->pDynamicStates = dynamicStates;
Greg Daniel164a9f02016-02-22 09:56:40 -0500404}
405
406GrVkPipeline* GrVkPipeline::Create(GrVkGpu* gpu, const GrPipeline& pipeline,
407 const GrPrimitiveProcessor& primProc,
408 VkPipelineShaderStageCreateInfo* shaderStageInfo,
409 int shaderStageCount,
410 GrPrimitiveType primitiveType,
411 const GrVkRenderPass& renderPass,
jvanverth03509ea2016-03-02 13:19:47 -0800412 VkPipelineLayout layout,
413 VkPipelineCache cache) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500414 VkPipelineVertexInputStateCreateInfo vertexInputInfo;
415 VkVertexInputBindingDescription bindingDesc;
416 // TODO: allocate this based on VkPhysicalDeviceLimits::maxVertexInputAttributes
417 static const int kMaxVertexAttributes = 16;
418 static VkVertexInputAttributeDescription attributeDesc[kMaxVertexAttributes];
419 setup_vertex_input_state(primProc, &vertexInputInfo, &bindingDesc, 1,
420 attributeDesc, kMaxVertexAttributes);
421
422 VkPipelineInputAssemblyStateCreateInfo inputAssemblyInfo;
423 setup_input_assembly_state(primitiveType, &inputAssemblyInfo);
424
425 VkPipelineDepthStencilStateCreateInfo depthStencilInfo;
426 setup_depth_stencil_state(gpu, pipeline.getStencil(), &depthStencilInfo);
427
428 GrRenderTarget* rt = pipeline.getRenderTarget();
429 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(rt);
430 VkPipelineViewportStateCreateInfo viewportInfo;
egdaniel470d77a2016-03-18 12:50:27 -0700431 setup_viewport_scissor_state(gpu, pipeline, vkRT, &viewportInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500432
433 VkPipelineMultisampleStateCreateInfo multisampleInfo;
ethannicholas28ef4452016-03-25 09:26:03 -0700434 setup_multisample_state(pipeline, primProc, gpu->caps(), &multisampleInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500435
436 // We will only have one color attachment per pipeline.
437 VkPipelineColorBlendAttachmentState attachmentStates[1];
438 VkPipelineColorBlendStateCreateInfo colorBlendInfo;
439 setup_color_blend_state(gpu, pipeline, &colorBlendInfo, attachmentStates);
440
441 VkPipelineRasterizationStateCreateInfo rasterInfo;
442 setup_raster_state(gpu, pipeline, &rasterInfo);
443
egdaniel470d77a2016-03-18 12:50:27 -0700444 VkDynamicState dynamicStates[3];
Greg Daniel164a9f02016-02-22 09:56:40 -0500445 VkPipelineDynamicStateCreateInfo dynamicInfo;
egdaniel470d77a2016-03-18 12:50:27 -0700446 setup_dynamic_state(gpu, pipeline, &dynamicInfo, dynamicStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500447
448 VkGraphicsPipelineCreateInfo pipelineCreateInfo;
449 memset(&pipelineCreateInfo, 0, sizeof(VkGraphicsPipelineCreateInfo));
450 pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
451 pipelineCreateInfo.pNext = nullptr;
452 pipelineCreateInfo.flags = 0;
453 pipelineCreateInfo.stageCount = shaderStageCount;
454 pipelineCreateInfo.pStages = shaderStageInfo;
455 pipelineCreateInfo.pVertexInputState = &vertexInputInfo;
456 pipelineCreateInfo.pInputAssemblyState = &inputAssemblyInfo;
457 pipelineCreateInfo.pTessellationState = nullptr;
458 pipelineCreateInfo.pViewportState = &viewportInfo;
459 pipelineCreateInfo.pRasterizationState = &rasterInfo;
460 pipelineCreateInfo.pMultisampleState = &multisampleInfo;
461 pipelineCreateInfo.pDepthStencilState = &depthStencilInfo;
462 pipelineCreateInfo.pColorBlendState = &colorBlendInfo;
463 pipelineCreateInfo.pDynamicState = &dynamicInfo;
464 pipelineCreateInfo.layout = layout;
465 pipelineCreateInfo.renderPass = renderPass.vkRenderPass();
466 pipelineCreateInfo.subpass = 0;
467 pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
468 pipelineCreateInfo.basePipelineIndex = -1;
469
470 VkPipeline vkPipeline;
471 VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateGraphicsPipelines(gpu->device(),
halcanary9d524f22016-03-29 09:03:52 -0700472 cache, 1,
473 &pipelineCreateInfo,
Greg Daniel164a9f02016-02-22 09:56:40 -0500474 nullptr, &vkPipeline));
475 if (err) {
476 return nullptr;
477 }
478
479 return new GrVkPipeline(vkPipeline);
480}
481
482void GrVkPipeline::freeGPUData(const GrVkGpu* gpu) const {
483 GR_VK_CALL(gpu->vkInterface(), DestroyPipeline(gpu->device(), fPipeline, nullptr));
484}
485
egdaniel470d77a2016-03-18 12:50:27 -0700486void set_dynamic_scissor_state(GrVkGpu* gpu,
487 GrVkCommandBuffer* cmdBuffer,
488 const GrPipeline& pipeline,
489 const GrRenderTarget& target) {
490 // We always use one scissor and if it is disabled we just make it the size of the RT
491 const GrScissorState& scissorState = pipeline.getScissorState();
492 VkRect2D scissor;
493 if (scissorState.enabled() &&
494 !scissorState.rect().contains(0, 0, target.width(), target.height())) {
495 // This all assumes the scissorState has previously been clipped to the device space render
halcanary9d524f22016-03-29 09:03:52 -0700496 // target.
egdaniel470d77a2016-03-18 12:50:27 -0700497 scissor.offset.x = scissorState.rect().fLeft;
498 scissor.extent.width = scissorState.rect().width();
499 if (kTopLeft_GrSurfaceOrigin == target.origin()) {
500 scissor.offset.y = scissorState.rect().fTop;
501 } else {
502 SkASSERT(kBottomLeft_GrSurfaceOrigin == target.origin());
503 scissor.offset.y = target.height() - scissorState.rect().fBottom;
504 }
505 scissor.extent.height = scissorState.rect().height();
506
507 SkASSERT(scissor.offset.x >= 0);
508 SkASSERT(scissor.offset.x + scissor.extent.width <= (uint32_t)target.width());
509 SkASSERT(scissor.offset.y >= 0);
510 SkASSERT(scissor.offset.y + scissor.extent.height <= (uint32_t)target.height());
511 } else {
512 scissor.extent.width = target.width();
513 scissor.extent.height = target.height();
514 scissor.offset.x = 0;
515 scissor.offset.y = 0;
516 }
517 cmdBuffer->setScissor(gpu, 0, 1, &scissor);
518}
519
520void set_dynamic_viewport_state(GrVkGpu* gpu,
521 GrVkCommandBuffer* cmdBuffer,
522 const GrRenderTarget& target) {
523 // We always use one viewport the size of the RT
524 VkViewport viewport;
525 viewport.x = 0.0f;
526 viewport.y = 0.0f;
527 viewport.width = SkIntToScalar(target.width());
528 viewport.height = SkIntToScalar(target.height());
529 viewport.minDepth = 0.0f;
530 viewport.maxDepth = 1.0f;
531 cmdBuffer->setViewport(gpu, 0, 1, &viewport);
532}
533
534void set_dynamic_blend_constant_state(GrVkGpu* gpu,
535 GrVkCommandBuffer* cmdBuffer,
536 const GrPipeline& pipeline) {
537 GrXferProcessor::BlendInfo blendInfo;
538 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
539 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
540 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
541 float floatColors[4];
542 if (blend_coeff_refs_constant(srcCoeff) || blend_coeff_refs_constant(dstCoeff)) {
543 GrColorToRGBAFloat(blendInfo.fBlendConstant, floatColors);
544 } else {
545 memset(floatColors, 0, 4 * sizeof(float));
546 }
547 cmdBuffer->setBlendConstants(gpu, floatColors);
548}
549
egdaniel470d77a2016-03-18 12:50:27 -0700550void GrVkPipeline::SetDynamicState(GrVkGpu* gpu,
551 GrVkCommandBuffer* cmdBuffer,
552 const GrPipeline& pipeline) {
553 const GrRenderTarget& target = *pipeline.getRenderTarget();
554 set_dynamic_scissor_state(gpu, cmdBuffer, pipeline, target);
555 set_dynamic_viewport_state(gpu, cmdBuffer, target);
556 set_dynamic_blend_constant_state(gpu, cmdBuffer, pipeline);
557}