Debian Codegraph @

Source/Core/VideoBackends/Vulkan/VKPipeline.cpp

raw symbols SCIP
1 // Copyright 2017 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoBackends/Vulkan/VKPipeline.h"
5
6 #include <array>
7
8 #include "Common/Assert.h"
9 #include "Common/EnumMap.h"
10 #include "Common/MsgHandler.h"
11
12 #include "VideoBackends/Vulkan/ObjectCache.h"
13 #include "VideoBackends/Vulkan/VKShader.h"
14 #include "VideoBackends/Vulkan/VKTexture.h"
15 #include "VideoBackends/Vulkan/VKVertexFormat.h"
16 #include "VideoBackends/Vulkan/VulkanContext.h"
17
18 namespace Vulkan
19 {
20 VKPipeline::VKPipeline(const AbstractPipelineConfig& config, VkPipeline pipeline,
21 VkPipelineLayout pipeline_layout, AbstractPipelineUsage usage)
22 : AbstractPipeline(config), m_pipeline(pipeline), m_pipeline_layout(pipeline_layout),
23 m_usage(usage)
24 {
25 }
26
27 VKPipeline::~VKPipeline()
28 {
29 vkDestroyPipeline(g_vulkan_context->GetDevice(), m_pipeline, nullptr);
30 }
31
32 static bool IsStripPrimitiveTopology(VkPrimitiveTopology topology)
33 {
34 return topology == VK_PRIMITIVE_TOPOLOGY_LINE_STRIP ||
35 topology == VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP ||
36 topology == VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY ||
37 topology == VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY;
38 }
39
40 static VkPipelineRasterizationStateCreateInfo
41 GetVulkanRasterizationState(const RasterizationState& state)
42 {
43 static constexpr std::array<VkCullModeFlags, 4> cull_modes = {
44 {VK_CULL_MODE_NONE, VK_CULL_MODE_BACK_BIT, VK_CULL_MODE_FRONT_BIT,
45 VK_CULL_MODE_FRONT_AND_BACK}};
46
47 bool depth_clamp = g_backend_info.bSupportsDepthClamp;
48
49 return {
50 VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, // VkStructureType sType
51 nullptr, // const void* pNext
52 0, // VkPipelineRasterizationStateCreateFlags flags
53 depth_clamp, // VkBool32 depthClampEnable
54 VK_FALSE, // VkBool32 rasterizerDiscardEnable
55 VK_POLYGON_MODE_FILL, // VkPolygonMode polygonMode
56 cull_modes[u32(state.cull_mode.Value())], // VkCullModeFlags cullMode
57 VK_FRONT_FACE_CLOCKWISE, // VkFrontFace frontFace
58 VK_FALSE, // VkBool32 depthBiasEnable
59 0.0f, // float depthBiasConstantFactor
60 0.0f, // float depthBiasClamp
61 0.0f, // float depthBiasSlopeFactor
62 1.0f // float lineWidth
63 };
64 }
65
66 static VkPipelineMultisampleStateCreateInfo GetVulkanMultisampleState(const FramebufferState& state)
67 {
68 return {
69 VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, // VkStructureType sType
70 nullptr, // const void* pNext
71 0, // VkPipelineMultisampleStateCreateFlags flags
72 static_cast<VkSampleCountFlagBits>(
73 state.samples.Value()), // VkSampleCountFlagBits rasterizationSamples
74 static_cast<bool>(state.per_sample_shading), // VkBool32 sampleShadingEnable
75 1.0f, // float minSampleShading
76 nullptr, // const VkSampleMask* pSampleMask;
77 VK_FALSE, // VkBool32 alphaToCoverageEnable
78 VK_FALSE // VkBool32 alphaToOneEnable
79 };
80 }
81
82 static VkPipelineDepthStencilStateCreateInfo GetVulkanDepthStencilState(const DepthState& state)
83 {
84 // Less/greater are swapped due to inverted depth.
85 VkCompareOp compare_op;
86 bool inverted_depth = !g_backend_info.bSupportsReversedDepthRange;
87 switch (state.func)
88 {
89 case CompareMode::Never:
90 compare_op = VK_COMPARE_OP_NEVER;
91 break;
92 case CompareMode::Less:
93 compare_op = inverted_depth ? VK_COMPARE_OP_GREATER : VK_COMPARE_OP_LESS;
94 break;
95 case CompareMode::Equal:
96 compare_op = VK_COMPARE_OP_EQUAL;
97 break;
98 case CompareMode::LEqual:
99 compare_op = inverted_depth ? VK_COMPARE_OP_GREATER_OR_EQUAL : VK_COMPARE_OP_LESS_OR_EQUAL;
100 break;
101 case CompareMode::Greater:
102 compare_op = inverted_depth ? VK_COMPARE_OP_LESS : VK_COMPARE_OP_GREATER;
103 break;
104 case CompareMode::NEqual:
105 compare_op = VK_COMPARE_OP_NOT_EQUAL;
106 break;
107 case CompareMode::GEqual:
108 compare_op = inverted_depth ? VK_COMPARE_OP_LESS_OR_EQUAL : VK_COMPARE_OP_GREATER_OR_EQUAL;
109 break;
110 case CompareMode::Always:
111 compare_op = VK_COMPARE_OP_ALWAYS;
112 break;
113 default:
114 PanicAlertFmt("Invalid compare mode {}", state.func);
115 compare_op = VK_COMPARE_OP_ALWAYS;
116 break;
117 }
118
119 return {
120 VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO, // VkStructureType sType
121 nullptr, // const void* pNext
122 0, // VkPipelineDepthStencilStateCreateFlags flags
123 state.test_enable, // VkBool32 depthTestEnable
124 state.update_enable, // VkBool32 depthWriteEnable
125 compare_op, // VkCompareOp depthCompareOp
126 VK_FALSE, // VkBool32 depthBoundsTestEnable
127 VK_FALSE, // VkBool32 stencilTestEnable
128 {}, // VkStencilOpState front
129 {}, // VkStencilOpState back
130 0.0f, // float minDepthBounds
131 1.0f // float maxDepthBounds
132 };
133 }
134
135 static VkPipelineColorBlendAttachmentState
136 GetVulkanAttachmentBlendState(const BlendingState& state, AbstractPipelineUsage usage)
137 {
138 VkPipelineColorBlendAttachmentState vk_state = {};
139
140 bool use_dual_source = state.use_dual_src;
141
142 vk_state.blendEnable = static_cast<VkBool32>(state.blend_enable);
143 vk_state.colorBlendOp = state.subtract ? VK_BLEND_OP_REVERSE_SUBTRACT : VK_BLEND_OP_ADD;
144 vk_state.alphaBlendOp = state.subtract_alpha ? VK_BLEND_OP_REVERSE_SUBTRACT : VK_BLEND_OP_ADD;
145
146 if (use_dual_source)
147 {
148 static constexpr Common::EnumMap<VkBlendFactor, SrcBlendFactor::InvDstAlpha> src_factors{
149 VK_BLEND_FACTOR_ZERO, VK_BLEND_FACTOR_ONE,
150 VK_BLEND_FACTOR_DST_COLOR, VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR,
151 VK_BLEND_FACTOR_SRC1_ALPHA, VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA,
152 VK_BLEND_FACTOR_DST_ALPHA, VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA,
153 };
154 static constexpr Common::EnumMap<VkBlendFactor, DstBlendFactor::InvDstAlpha> dst_factors{
155 VK_BLEND_FACTOR_ZERO, VK_BLEND_FACTOR_ONE,
156 VK_BLEND_FACTOR_SRC_COLOR, VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR,
157 VK_BLEND_FACTOR_SRC1_ALPHA, VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA,
158 VK_BLEND_FACTOR_DST_ALPHA, VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA,
159 };
160
161 vk_state.srcColorBlendFactor = src_factors[state.src_factor];
162 vk_state.srcAlphaBlendFactor = src_factors[state.src_factor_alpha];
163 vk_state.dstColorBlendFactor = dst_factors[state.dst_factor];
164 vk_state.dstAlphaBlendFactor = dst_factors[state.dst_factor_alpha];
165 }
166 else
167 {
168 static constexpr Common::EnumMap<VkBlendFactor, SrcBlendFactor::InvDstAlpha> src_factors{
169 VK_BLEND_FACTOR_ZERO, VK_BLEND_FACTOR_ONE,
170 VK_BLEND_FACTOR_DST_COLOR, VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR,
171 VK_BLEND_FACTOR_SRC_ALPHA, VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
172 VK_BLEND_FACTOR_DST_ALPHA, VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA,
173 };
174
175 static constexpr Common::EnumMap<VkBlendFactor, DstBlendFactor::InvDstAlpha> dst_factors{
176 VK_BLEND_FACTOR_ZERO, VK_BLEND_FACTOR_ONE,
177 VK_BLEND_FACTOR_SRC_COLOR, VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR,
178 VK_BLEND_FACTOR_SRC_ALPHA, VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
179 VK_BLEND_FACTOR_DST_ALPHA, VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA,
180 };
181
182 vk_state.srcColorBlendFactor = src_factors[state.src_factor];
183 vk_state.srcAlphaBlendFactor = src_factors[state.src_factor_alpha];
184 vk_state.dstColorBlendFactor = dst_factors[state.dst_factor];
185 vk_state.dstAlphaBlendFactor = dst_factors[state.dst_factor_alpha];
186 }
187
188 if (state.color_update)
189 {
190 vk_state.colorWriteMask =
191 VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT;
192 }
193 else
194 {
195 vk_state.colorWriteMask = 0;
196 }
197
198 if (state.alpha_update)
199 vk_state.colorWriteMask |= VK_COLOR_COMPONENT_A_BIT;
200
201 return vk_state;
202 }
203
204 static VkPipelineColorBlendStateCreateInfo
205 GetVulkanColorBlendState(const BlendingState& state,
206 const VkPipelineColorBlendAttachmentState* attachments,
207 uint32_t num_attachments)
208 {
209 static constexpr std::array<VkLogicOp, 16> vk_logic_ops = {
210 {VK_LOGIC_OP_CLEAR, VK_LOGIC_OP_AND, VK_LOGIC_OP_AND_REVERSE, VK_LOGIC_OP_COPY,
211 VK_LOGIC_OP_AND_INVERTED, VK_LOGIC_OP_NO_OP, VK_LOGIC_OP_XOR, VK_LOGIC_OP_OR,
212 VK_LOGIC_OP_NOR, VK_LOGIC_OP_EQUIVALENT, VK_LOGIC_OP_INVERT, VK_LOGIC_OP_OR_REVERSE,
213 VK_LOGIC_OP_COPY_INVERTED, VK_LOGIC_OP_OR_INVERTED, VK_LOGIC_OP_NAND, VK_LOGIC_OP_SET}};
214
215 VkBool32 vk_logic_op_enable = static_cast<VkBool32>(state.logic_op_enable);
216 if (vk_logic_op_enable && !g_backend_info.bSupportsLogicOp)
217 {
218 // At the time of writing, Adreno and Mali drivers didn't support logic ops.
219 // The "emulation" through blending path has been removed, so just disable it completely.
220 // These drivers don't support dual-source blend either, so issues are to be expected.
221 vk_logic_op_enable = VK_FALSE;
222 }
223
224 VkLogicOp vk_logic_op =
225 vk_logic_op_enable ? vk_logic_ops[u32(state.logic_mode.Value())] : VK_LOGIC_OP_CLEAR;
226
227 VkPipelineColorBlendStateCreateInfo vk_state = {
228 VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, // VkStructureType sType
229 nullptr, // const void* pNext
230 0, // VkPipelineColorBlendStateCreateFlags flags
231 vk_logic_op_enable, // VkBool32 logicOpEnable
232 vk_logic_op, // VkLogicOp logicOp
233 num_attachments, // uint32_t attachmentCount
234 attachments, // const VkPipelineColorBlendAttachmentState* pAttachments
235 {1.0f, 1.0f, 1.0f, 1.0f} // float blendConstants[4]
236 };
237
238 return vk_state;
239 }
240
241 std::unique_ptr<VKPipeline> VKPipeline::Create(const AbstractPipelineConfig& config)
242 {
243 DEBUG_ASSERT(config.vertex_shader && config.pixel_shader);
244
245 // Get render pass for config.
246 VkRenderPass render_pass = g_object_cache->GetRenderPass(
247 VKTexture::GetVkFormatForHostTextureFormat(config.framebuffer_state.color_texture_format),
248 VKTexture::GetVkFormatForHostTextureFormat(config.framebuffer_state.depth_texture_format),
249 config.framebuffer_state.samples, VK_ATTACHMENT_LOAD_OP_LOAD,
250 config.framebuffer_state.additional_color_attachment_count);
251
252 if (render_pass == VK_NULL_HANDLE)
253 {
254 PanicAlertFmt("Failed to get render pass");
255 return nullptr;
256 }
257
258 // Get pipeline layout.
259 VkPipelineLayout pipeline_layout;
260 switch (config.usage)
261 {
262 case AbstractPipelineUsage::GX:
263 pipeline_layout = g_object_cache->GetPipelineLayout(PIPELINE_LAYOUT_STANDARD);
264 break;
265 case AbstractPipelineUsage::GXUber:
266 pipeline_layout = g_object_cache->GetPipelineLayout(PIPELINE_LAYOUT_UBER);
267 break;
268 case AbstractPipelineUsage::Utility:
269 pipeline_layout = g_object_cache->GetPipelineLayout(PIPELINE_LAYOUT_UTILITY);
270 break;
271 default:
272 PanicAlertFmt("Unknown pipeline layout.");
273 return nullptr;
274 }
275
276 // Declare descriptors for empty vertex buffers/attributes
277 static const VkPipelineVertexInputStateCreateInfo empty_vertex_input_state = {
278 VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, // VkStructureType sType
279 nullptr, // const void* pNext
280 0, // VkPipelineVertexInputStateCreateFlags flags
281 0, // uint32_t vertexBindingDescriptionCount
282 nullptr, // const VkVertexInputBindingDescription* pVertexBindingDescriptions
283 0, // uint32_t vertexAttributeDescriptionCount
284 nullptr // const VkVertexInputAttributeDescription* pVertexAttributeDescriptions
285 };
286
287 // Vertex inputs
288 const VkPipelineVertexInputStateCreateInfo& vertex_input_state =
289 config.vertex_format ?
290 static_cast<const VertexFormat*>(config.vertex_format)->GetVertexInputStateInfo() :
291 empty_vertex_input_state;
292
293 // Input assembly
294 static constexpr std::array<VkPrimitiveTopology, 4> vk_primitive_topologies = {
295 {VK_PRIMITIVE_TOPOLOGY_POINT_LIST, VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
296 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP}};
297 VkPipelineInputAssemblyStateCreateInfo input_assembly_state = {
298 VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, nullptr, 0,
299 vk_primitive_topologies[static_cast<u32>(config.rasterization_state.primitive.Value())],
300 VK_FALSE};
301
302 // See Vulkan spec, section 19:
303 // If topology is VK_PRIMITIVE_TOPOLOGY_POINT_LIST, VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
304 // VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY,
305 // VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY or VK_PRIMITIVE_TOPOLOGY_PATCH_LIST,
306 // primitiveRestartEnable must be VK_FALSE
307 if (g_backend_info.bSupportsPrimitiveRestart &&
308 IsStripPrimitiveTopology(input_assembly_state.topology))
309 {
310 input_assembly_state.primitiveRestartEnable = VK_TRUE;
311 }
312
313 // Shaders to stages
314 VkPipelineShaderStageCreateInfo shader_stages[3];
315 uint32_t num_shader_stages = 0;
316 if (config.vertex_shader)
317 {
318 shader_stages[num_shader_stages++] = {
319 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
320 nullptr,
321 0,
322 VK_SHADER_STAGE_VERTEX_BIT,
323 static_cast<const VKShader*>(config.vertex_shader)->GetShaderModule(),
324 "main"};
325 }
326 if (config.geometry_shader)
327 {
328 shader_stages[num_shader_stages++] = {
329 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
330 nullptr,
331 0,
332 VK_SHADER_STAGE_GEOMETRY_BIT,
333 static_cast<const VKShader*>(config.geometry_shader)->GetShaderModule(),
334 "main"};
335 }
336 if (config.pixel_shader)
337 {
338 shader_stages[num_shader_stages++] = {
339 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
340 nullptr,
341 0,
342 VK_SHADER_STAGE_FRAGMENT_BIT,
343 static_cast<const VKShader*>(config.pixel_shader)->GetShaderModule(),
344 "main"};
345 }
346
347 // Fill in Vulkan descriptor structs from our state structures.
348 VkPipelineRasterizationStateCreateInfo rasterization_state =
349 GetVulkanRasterizationState(config.rasterization_state);
350 VkPipelineMultisampleStateCreateInfo multisample_state =
351 GetVulkanMultisampleState(config.framebuffer_state);
352 VkPipelineDepthStencilStateCreateInfo depth_stencil_state =
353 GetVulkanDepthStencilState(config.depth_state);
354 VkPipelineColorBlendAttachmentState blend_attachment_state =
355 GetVulkanAttachmentBlendState(config.blending_state, config.usage);
356
357 std::vector<VkPipelineColorBlendAttachmentState> blend_attachment_states;
358 blend_attachment_states.push_back(blend_attachment_state);
359 // Right now all our attachments have the same state
360 for (u8 i = 0; i < static_cast<u8>(config.framebuffer_state.additional_color_attachment_count);
361 i++)
362 {
363 blend_attachment_states.push_back(blend_attachment_state);
364 }
365 VkPipelineColorBlendStateCreateInfo blend_state =
366 GetVulkanColorBlendState(config.blending_state, blend_attachment_states.data(),
367 static_cast<uint32_t>(blend_attachment_states.size()));
368
369 static const VkDepthClampRangeEXT clamp_range = {0.0f, MAX_EFB_DEPTH};
370 static const VkPipelineViewportDepthClampControlCreateInfoEXT depth_clamp_state = {
371 VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_DEPTH_CLAMP_CONTROL_CREATE_INFO_EXT, nullptr,
372 VK_DEPTH_CLAMP_MODE_USER_DEFINED_RANGE_EXT, // VkDepthClampModeEXT depthClampMode
373 &clamp_range // const VkDepthClampRangeEXT* pDepthClampRange
374 };
375
376 // This viewport isn't used, but needs to be specified anyway.
377 static const VkViewport viewport = {0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f};
378 static const VkRect2D scissor = {{0, 0}, {1, 1}};
379 static const VkPipelineViewportStateCreateInfo viewport_state = {
380 VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
381 g_backend_info.bSupportsUnrestrictedDepthRange ? &depth_clamp_state : nullptr,
382 0, // VkPipelineViewportStateCreateFlags flags;
383 1, // uint32_t viewportCount
384 &viewport, // const VkViewport* pViewports
385 1, // uint32_t scissorCount
386 &scissor // const VkRect2D* pScissors
387 };
388
389 // Set viewport and scissor dynamic state so we can change it elsewhere.
390 static const std::array<VkDynamicState, 2> dynamic_states{
391 VK_DYNAMIC_STATE_VIEWPORT,
392 VK_DYNAMIC_STATE_SCISSOR,
393 };
394 static const VkPipelineDynamicStateCreateInfo dynamic_state = {
395 VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, nullptr,
396 0, // VkPipelineDynamicStateCreateFlags flags
397 static_cast<u32>(dynamic_states.size()), // uint32_t dynamicStateCount
398 dynamic_states.data() // const VkDynamicState* pDynamicStates
399 };
400
401 // Combine to full pipeline info structure.
402 VkGraphicsPipelineCreateInfo pipeline_info = {
403 VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
404 nullptr, // VkStructureType sType
405 0, // VkPipelineCreateFlags flags
406 num_shader_stages, // uint32_t stageCount
407 shader_stages, // const VkPipelineShaderStageCreateInfo* pStages
408 &vertex_input_state, // const VkPipelineVertexInputStateCreateInfo* pVertexInputState
409 &input_assembly_state, // const VkPipelineInputAssemblyStateCreateInfo* pInputAssemblyState
410 nullptr, // const VkPipelineTessellationStateCreateInfo* pTessellationState
411 &viewport_state, // const VkPipelineViewportStateCreateInfo* pViewportState
412 &rasterization_state, // const VkPipelineRasterizationStateCreateInfo* pRasterizationState
413 &multisample_state, // const VkPipelineMultisampleStateCreateInfo* pMultisampleState
414 &depth_stencil_state, // const VkPipelineDepthStencilStateCreateInfo* pDepthStencilState
415 &blend_state, // const VkPipelineColorBlendStateCreateInfo* pColorBlendState
416 &dynamic_state, // const VkPipelineDynamicStateCreateInfo* pDynamicState
417 pipeline_layout, // VkPipelineLayout layout
418 render_pass, // VkRenderPass renderPass
419 0, // uint32_t subpass
420 VK_NULL_HANDLE, // VkPipeline basePipelineHandle
421 -1 // int32_t basePipelineIndex
422 };
423
424 VkPipeline pipeline;
425 VkResult res =
426 vkCreateGraphicsPipelines(g_vulkan_context->GetDevice(), g_object_cache->GetPipelineCache(),
427 1, &pipeline_info, nullptr, &pipeline);
428 if (res != VK_SUCCESS)
429 {
430 LOG_VULKAN_ERROR(res, "vkCreateGraphicsPipelines failed: ");
431 return VK_NULL_HANDLE;
432 }
433
434 return std::make_unique<VKPipeline>(config, pipeline, pipeline_layout, config.usage);
435 }
436 } // namespace Vulkan