blob: 7571a40d5825d58af32efd4b8c747aea3696f788 [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
206void setup_multisample_state(const GrPipeline& pipeline,
207 VkPipelineMultisampleStateCreateInfo* multisampleInfo) {
208 memset(multisampleInfo, 0, sizeof(VkPipelineMultisampleStateCreateInfo));
209 multisampleInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
210 multisampleInfo->pNext = nullptr;
211 multisampleInfo->flags = 0;
212 int numSamples = pipeline.getRenderTarget()->numColorSamples();
213 SkAssertResult(GrSampleCountToVkSampleCount(numSamples,
214 &multisampleInfo->rasterizationSamples));
215 multisampleInfo->sampleShadingEnable = VK_FALSE;
216 multisampleInfo->minSampleShading = 0;
217 multisampleInfo->pSampleMask = nullptr;
218 multisampleInfo->alphaToCoverageEnable = VK_FALSE;
219 multisampleInfo->alphaToOneEnable = VK_FALSE;
220}
221
222static VkBlendFactor blend_coeff_to_vk_blend(GrBlendCoeff coeff) {
223 static const VkBlendFactor gTable[] = {
224 VK_BLEND_FACTOR_ZERO, // kZero_GrBlendCoeff
225 VK_BLEND_FACTOR_ONE, // kOne_GrBlendCoeff
226 VK_BLEND_FACTOR_SRC_COLOR, // kSC_GrBlendCoeff
227 VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR, // kISC_GrBlendCoeff
228 VK_BLEND_FACTOR_DST_COLOR, // kDC_GrBlendCoeff
229 VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR, // kIDC_GrBlendCoeff
230 VK_BLEND_FACTOR_SRC_ALPHA, // kSA_GrBlendCoeff
231 VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, // kISA_GrBlendCoeff
232 VK_BLEND_FACTOR_DST_ALPHA, // kDA_GrBlendCoeff
233 VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA, // kIDA_GrBlendCoeff
234 VK_BLEND_FACTOR_CONSTANT_COLOR, // kConstC_GrBlendCoeff
235 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR, // kIConstC_GrBlendCoeff
236 VK_BLEND_FACTOR_CONSTANT_ALPHA, // kConstA_GrBlendCoeff
237 VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA, // kIConstA_GrBlendCoeff
238 VK_BLEND_FACTOR_SRC1_COLOR, // kS2C_GrBlendCoeff
239 VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR, // kIS2C_GrBlendCoeff
240 VK_BLEND_FACTOR_SRC1_ALPHA, // kS2A_GrBlendCoeff
241 VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA, // kIS2A_GrBlendCoeff
242
243 };
244 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrBlendCoeffCnt);
245 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
246 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
247 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
248 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
249 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
250 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
251 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
252 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
253 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
254 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
255 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
256 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
257 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
258 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
259 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
260 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
261 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
262 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
263
264 SkASSERT((unsigned)coeff < kGrBlendCoeffCnt);
265 return gTable[coeff];
266}
267
268
269static VkBlendOp blend_equation_to_vk_blend_op(GrBlendEquation equation) {
270 static const VkBlendOp gTable[] = {
271 VK_BLEND_OP_ADD, // kAdd_GrBlendEquation
272 VK_BLEND_OP_SUBTRACT, // kSubtract_GrBlendEquation
273 VK_BLEND_OP_REVERSE_SUBTRACT, // kReverseSubtract_GrBlendEquation
274 };
275 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kFirstAdvancedGrBlendEquation);
276 GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
277 GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
278 GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
279
280 SkASSERT((unsigned)equation < kGrBlendCoeffCnt);
281 return gTable[equation];
282}
283
284bool blend_coeff_refs_constant(GrBlendCoeff coeff) {
285 static const bool gCoeffReferencesBlendConst[] = {
286 false,
287 false,
288 false,
289 false,
290 false,
291 false,
292 false,
293 false,
294 false,
295 false,
296 true,
297 true,
298 true,
299 true,
300
301 // extended blend coeffs
302 false,
303 false,
304 false,
305 false,
306 };
307 return gCoeffReferencesBlendConst[coeff];
308 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
309 // Individual enum asserts already made in blend_coeff_to_vk_blend
310}
311
312void setup_color_blend_state(const GrVkGpu* gpu,
313 const GrPipeline& pipeline,
314 VkPipelineColorBlendStateCreateInfo* colorBlendInfo,
315 VkPipelineColorBlendAttachmentState* attachmentState) {
316 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
353VkCullModeFlags draw_face_to_vk_cull_mode(GrPipelineBuilder::DrawFace drawFace) {
354 // 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 };
360 GR_STATIC_ASSERT(0 == GrPipelineBuilder::kBoth_DrawFace);
361 GR_STATIC_ASSERT(1 == GrPipelineBuilder::kCCW_DrawFace);
362 GR_STATIC_ASSERT(2 == GrPipelineBuilder::kCW_DrawFace);
363 SkASSERT((unsigned)drawFace <= 2);
364
365 return gTable[drawFace];
366}
367
368void setup_raster_state(const GrVkGpu* gpu,
369 const GrPipeline& pipeline,
370 VkPipelineRasterizationStateCreateInfo* rasterInfo) {
371 memset(rasterInfo, 0, sizeof(VkPipelineRasterizationStateCreateInfo));
372 rasterInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
373 rasterInfo->pNext = nullptr;
374 rasterInfo->flags = 0;
375 rasterInfo->depthClampEnable = VK_FALSE;
376 rasterInfo->rasterizerDiscardEnable = VK_FALSE;
377 rasterInfo->polygonMode = VK_POLYGON_MODE_FILL;
378 rasterInfo->cullMode = draw_face_to_vk_cull_mode(pipeline.getDrawFace());
379 rasterInfo->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
380 rasterInfo->depthBiasEnable = VK_FALSE;
381 rasterInfo->depthBiasConstantFactor = 0.0f;
382 rasterInfo->depthBiasClamp = 0.0f;
383 rasterInfo->depthBiasSlopeFactor = 0.0f;
384 rasterInfo->lineWidth = 1.0f;
385}
386
387void setup_dynamic_state(const GrVkGpu* gpu,
388 const GrPipeline& pipeline,
egdaniel470d77a2016-03-18 12:50:27 -0700389 VkPipelineDynamicStateCreateInfo* dynamicInfo,
390 VkDynamicState* dynamicStates) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500391 memset(dynamicInfo, 0, sizeof(VkPipelineDynamicStateCreateInfo));
392 dynamicInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
egdaniel470d77a2016-03-18 12:50:27 -0700393 dynamicInfo->pNext = VK_NULL_HANDLE;
394 dynamicInfo->flags = 0;
395 dynamicStates[0] = VK_DYNAMIC_STATE_VIEWPORT;
396 dynamicStates[1] = VK_DYNAMIC_STATE_SCISSOR;
397 dynamicStates[2] = VK_DYNAMIC_STATE_BLEND_CONSTANTS;
398 dynamicInfo->dynamicStateCount = 3;
399 dynamicInfo->pDynamicStates = dynamicStates;
Greg Daniel164a9f02016-02-22 09:56:40 -0500400}
401
402GrVkPipeline* GrVkPipeline::Create(GrVkGpu* gpu, const GrPipeline& pipeline,
403 const GrPrimitiveProcessor& primProc,
404 VkPipelineShaderStageCreateInfo* shaderStageInfo,
405 int shaderStageCount,
406 GrPrimitiveType primitiveType,
407 const GrVkRenderPass& renderPass,
jvanverth03509ea2016-03-02 13:19:47 -0800408 VkPipelineLayout layout,
409 VkPipelineCache cache) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500410 VkPipelineVertexInputStateCreateInfo vertexInputInfo;
411 VkVertexInputBindingDescription bindingDesc;
412 // TODO: allocate this based on VkPhysicalDeviceLimits::maxVertexInputAttributes
413 static const int kMaxVertexAttributes = 16;
414 static VkVertexInputAttributeDescription attributeDesc[kMaxVertexAttributes];
415 setup_vertex_input_state(primProc, &vertexInputInfo, &bindingDesc, 1,
416 attributeDesc, kMaxVertexAttributes);
417
418 VkPipelineInputAssemblyStateCreateInfo inputAssemblyInfo;
419 setup_input_assembly_state(primitiveType, &inputAssemblyInfo);
420
421 VkPipelineDepthStencilStateCreateInfo depthStencilInfo;
422 setup_depth_stencil_state(gpu, pipeline.getStencil(), &depthStencilInfo);
423
424 GrRenderTarget* rt = pipeline.getRenderTarget();
425 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(rt);
426 VkPipelineViewportStateCreateInfo viewportInfo;
egdaniel470d77a2016-03-18 12:50:27 -0700427 setup_viewport_scissor_state(gpu, pipeline, vkRT, &viewportInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500428
429 VkPipelineMultisampleStateCreateInfo multisampleInfo;
430 setup_multisample_state(pipeline, &multisampleInfo);
431
432 // We will only have one color attachment per pipeline.
433 VkPipelineColorBlendAttachmentState attachmentStates[1];
434 VkPipelineColorBlendStateCreateInfo colorBlendInfo;
435 setup_color_blend_state(gpu, pipeline, &colorBlendInfo, attachmentStates);
436
437 VkPipelineRasterizationStateCreateInfo rasterInfo;
438 setup_raster_state(gpu, pipeline, &rasterInfo);
439
egdaniel470d77a2016-03-18 12:50:27 -0700440 VkDynamicState dynamicStates[3];
Greg Daniel164a9f02016-02-22 09:56:40 -0500441 VkPipelineDynamicStateCreateInfo dynamicInfo;
egdaniel470d77a2016-03-18 12:50:27 -0700442 setup_dynamic_state(gpu, pipeline, &dynamicInfo, dynamicStates);
Greg Daniel164a9f02016-02-22 09:56:40 -0500443
444 VkGraphicsPipelineCreateInfo pipelineCreateInfo;
445 memset(&pipelineCreateInfo, 0, sizeof(VkGraphicsPipelineCreateInfo));
446 pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
447 pipelineCreateInfo.pNext = nullptr;
448 pipelineCreateInfo.flags = 0;
449 pipelineCreateInfo.stageCount = shaderStageCount;
450 pipelineCreateInfo.pStages = shaderStageInfo;
451 pipelineCreateInfo.pVertexInputState = &vertexInputInfo;
452 pipelineCreateInfo.pInputAssemblyState = &inputAssemblyInfo;
453 pipelineCreateInfo.pTessellationState = nullptr;
454 pipelineCreateInfo.pViewportState = &viewportInfo;
455 pipelineCreateInfo.pRasterizationState = &rasterInfo;
456 pipelineCreateInfo.pMultisampleState = &multisampleInfo;
457 pipelineCreateInfo.pDepthStencilState = &depthStencilInfo;
458 pipelineCreateInfo.pColorBlendState = &colorBlendInfo;
459 pipelineCreateInfo.pDynamicState = &dynamicInfo;
460 pipelineCreateInfo.layout = layout;
461 pipelineCreateInfo.renderPass = renderPass.vkRenderPass();
462 pipelineCreateInfo.subpass = 0;
463 pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
464 pipelineCreateInfo.basePipelineIndex = -1;
465
466 VkPipeline vkPipeline;
467 VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateGraphicsPipelines(gpu->device(),
jvanverth03509ea2016-03-02 13:19:47 -0800468 cache, 1,
Greg Daniel164a9f02016-02-22 09:56:40 -0500469 &pipelineCreateInfo,
470 nullptr, &vkPipeline));
471 if (err) {
472 return nullptr;
473 }
474
475 return new GrVkPipeline(vkPipeline);
476}
477
478void GrVkPipeline::freeGPUData(const GrVkGpu* gpu) const {
479 GR_VK_CALL(gpu->vkInterface(), DestroyPipeline(gpu->device(), fPipeline, nullptr));
480}
481
482
egdaniel470d77a2016-03-18 12:50:27 -0700483void set_dynamic_scissor_state(GrVkGpu* gpu,
484 GrVkCommandBuffer* cmdBuffer,
485 const GrPipeline& pipeline,
486 const GrRenderTarget& target) {
487 // We always use one scissor and if it is disabled we just make it the size of the RT
488 const GrScissorState& scissorState = pipeline.getScissorState();
489 VkRect2D scissor;
490 if (scissorState.enabled() &&
491 !scissorState.rect().contains(0, 0, target.width(), target.height())) {
492 // This all assumes the scissorState has previously been clipped to the device space render
493 // target.
494 scissor.offset.x = scissorState.rect().fLeft;
495 scissor.extent.width = scissorState.rect().width();
496 if (kTopLeft_GrSurfaceOrigin == target.origin()) {
497 scissor.offset.y = scissorState.rect().fTop;
498 } else {
499 SkASSERT(kBottomLeft_GrSurfaceOrigin == target.origin());
500 scissor.offset.y = target.height() - scissorState.rect().fBottom;
501 }
502 scissor.extent.height = scissorState.rect().height();
503
504 SkASSERT(scissor.offset.x >= 0);
505 SkASSERT(scissor.offset.x + scissor.extent.width <= (uint32_t)target.width());
506 SkASSERT(scissor.offset.y >= 0);
507 SkASSERT(scissor.offset.y + scissor.extent.height <= (uint32_t)target.height());
508 } else {
509 scissor.extent.width = target.width();
510 scissor.extent.height = target.height();
511 scissor.offset.x = 0;
512 scissor.offset.y = 0;
513 }
514 cmdBuffer->setScissor(gpu, 0, 1, &scissor);
515}
516
517void set_dynamic_viewport_state(GrVkGpu* gpu,
518 GrVkCommandBuffer* cmdBuffer,
519 const GrRenderTarget& target) {
520 // We always use one viewport the size of the RT
521 VkViewport viewport;
522 viewport.x = 0.0f;
523 viewport.y = 0.0f;
524 viewport.width = SkIntToScalar(target.width());
525 viewport.height = SkIntToScalar(target.height());
526 viewport.minDepth = 0.0f;
527 viewport.maxDepth = 1.0f;
528 cmdBuffer->setViewport(gpu, 0, 1, &viewport);
529}
530
531void set_dynamic_blend_constant_state(GrVkGpu* gpu,
532 GrVkCommandBuffer* cmdBuffer,
533 const GrPipeline& pipeline) {
534 GrXferProcessor::BlendInfo blendInfo;
535 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
536 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
537 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
538 float floatColors[4];
539 if (blend_coeff_refs_constant(srcCoeff) || blend_coeff_refs_constant(dstCoeff)) {
540 GrColorToRGBAFloat(blendInfo.fBlendConstant, floatColors);
541 } else {
542 memset(floatColors, 0, 4 * sizeof(float));
543 }
544 cmdBuffer->setBlendConstants(gpu, floatColors);
545}
546
547
548
549void GrVkPipeline::SetDynamicState(GrVkGpu* gpu,
550 GrVkCommandBuffer* cmdBuffer,
551 const GrPipeline& pipeline) {
552 const GrRenderTarget& target = *pipeline.getRenderTarget();
553 set_dynamic_scissor_state(gpu, cmdBuffer, pipeline, target);
554 set_dynamic_viewport_state(gpu, cmdBuffer, target);
555 set_dynamic_blend_constant_state(gpu, cmdBuffer, pipeline);
556}