Blender V5.3
vk_graphics_pipeline.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
13
14/* TODO: separate in the different configuration and add a main configuration that includes all.
15 * Unsure yet if how to organize this in structs to keep stack allocation small. */
16
17#pragma once
18
19#include "vk_common.hh"
20#include "vk_device.hh"
21#include "vk_pipeline_pool.hh"
23
24namespace blender::gpu {
25
27 VkPipelineRenderingCreateInfo vk_pipeline_rendering_create_info;
28 std::array<VkPipelineShaderStageCreateInfo, 3> vk_pipeline_shader_stage_create_info;
29 VkSpecializationInfo vk_specialization_info;
31 VkPipelineInputAssemblyStateCreateInfo vk_pipeline_input_assembly_state_create_info;
32 VkPipelineVertexInputStateCreateInfo vk_pipeline_vertex_input_state_create_info;
33 VkPipelineRasterizationStateCreateInfo vk_pipeline_rasterization_state_create_info;
34 VkPipelineRasterizationProvokingVertexStateCreateInfoEXT
36 VkPipelineRasterizationLineStateCreateInfoEXT vk_pipeline_rasterization_line_state_info;
38 VkPipelineDynamicStateCreateInfo vk_pipeline_dynamic_state_create_info;
39 VkPipelineViewportStateCreateInfo vk_pipeline_viewport_state_create_info;
40 VkPipelineDepthStencilStateCreateInfo vk_pipeline_depth_stencil_state_create_info;
41 VkPipelineMultisampleStateCreateInfo vk_pipeline_multisample_state_create_info;
43 VkPipelineColorBlendStateCreateInfo vk_pipeline_color_blend_state_create_info;
44 VkGraphicsPipelineCreateInfo vk_graphics_pipeline_create_info;
45 VkGraphicsPipelineLibraryCreateInfoEXT vk_graphics_pipeline_library_create_info;
46
47 /* Only used when VK_EXT_vertex_input_dynamic_state isn't supported. */
50
56 VkRenderingInputAttachmentIndexInfo vk_rendering_input_attachment_index_info_;
58
62 void build_full(VKDevice &device,
63 const VKGraphicsInfo &graphics_info,
64 VkPipeline vk_pipeline_base)
65 {
66 const VKExtensions &extensions = device.extensions_get();
67 const VKWorkarounds &workarounds = device.workarounds_get();
68 build_graphics_pipeline(extensions, graphics_info, vk_pipeline_base);
69
70 build_input_assembly_state(graphics_info.vertex_in);
71 if (!extensions.vertex_input_dynamic_state) {
72 build_vertex_input_state(device, graphics_info.vertex_in);
73 }
74
75 build_shader_stages(graphics_info.shaders);
76 const bool do_specialization_constants =
77 !graphics_info.shaders.specialization_constants.is_empty();
78 if (do_specialization_constants) {
79 build_specialization_constants(graphics_info.shaders);
80 }
81 build_dynamic_state(graphics_info.shaders, extensions, workarounds);
82 build_multisample_state();
83 build_viewport_state(graphics_info.shaders, workarounds);
84 build_rasterization_state(graphics_info.shaders, extensions);
85 build_depth_stencil_state(graphics_info.shaders);
86
87 build_color_blend_attachment_states(graphics_info.fragment_out);
88 build_color_blend_state();
89 build_dynamic_rendering(graphics_info.fragment_out);
90 }
91
96 const VKGraphicsInfo::VertexIn &vertex_input_info,
97 VkPipeline vk_pipeline_base)
98 {
99 const VKExtensions &extensions = device.extensions_get();
100 build_graphics_pipeline_library(VK_GRAPHICS_PIPELINE_LIBRARY_VERTEX_INPUT_INTERFACE_BIT_EXT);
101 build_graphics_pipeline_vertex_input_lib(vk_pipeline_base, extensions);
102 build_input_assembly_state(vertex_input_info);
103 build_dynamic_state_for_vertex_input(extensions);
104
105 if (!extensions.vertex_input_dynamic_state) {
106 build_vertex_input_state(device, vertex_input_info);
107 }
108 }
109
114 const VKExtensions &extensions,
115 const VKWorkarounds &workarounds,
116 VkPipeline vk_pipeline_base)
117 {
118 build_graphics_pipeline_library(
119 VK_GRAPHICS_PIPELINE_LIBRARY_PRE_RASTERIZATION_SHADERS_BIT_EXT |
120 VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_SHADER_BIT_EXT);
121 build_graphics_pipeline_shaders_lib(shaders_info, vk_pipeline_base);
122
123 build_shader_stages(shaders_info);
124 const bool do_specialization_constants = !shaders_info.specialization_constants.is_empty();
125 if (do_specialization_constants) {
126 build_specialization_constants(shaders_info);
127 }
128 build_dynamic_state(shaders_info, extensions, workarounds);
129 build_multisample_state();
130 build_viewport_state(shaders_info, workarounds);
131 build_rasterization_state(shaders_info, extensions);
132 build_depth_stencil_state(shaders_info);
133 build_dynamic_rendering_shaders_lib(extensions, shaders_info);
134 }
135
141 VkPipeline vk_pipeline_base)
142 {
143 build_graphics_pipeline_library(
144 VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_OUTPUT_INTERFACE_BIT_EXT);
145 build_graphics_pipeline_fragment_output_lib(vk_pipeline_base);
146 build_multisample_state();
147 build_color_blend_attachment_states(fragment_output_info);
148 build_color_blend_state();
149 build_dynamic_rendering(fragment_output_info);
150 }
151
152 private:
153 void build_graphics_pipeline(const VKExtensions &extensions,
154 const VKGraphicsInfo &graphics_info,
155 VkPipeline vk_pipeline_base)
156 {
158 vk_graphics_pipeline_create_info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
161 VK_NULL_HANDLE ?
162 2 :
163 3;
165 vk_graphics_pipeline_create_info.pInputAssemblyState =
167 vk_graphics_pipeline_create_info.pVertexInputState =
170 vk_graphics_pipeline_create_info.pRasterizationState =
172 vk_graphics_pipeline_create_info.pDepthStencilState =
176 vk_graphics_pipeline_create_info.pMultisampleState =
180 vk_graphics_pipeline_create_info.basePipelineHandle = vk_pipeline_base;
181 }
182
183 void build_graphics_pipeline_vertex_input_lib(VkPipeline vk_pipeline_base,
184 const VKExtensions &extensions)
185 {
187 VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
189 VK_PIPELINE_CREATE_LIBRARY_BIT_KHR |
190 VK_PIPELINE_CREATE_RETAIN_LINK_TIME_OPTIMIZATION_INFO_BIT_EXT,
191 0,
192 nullptr,
193 extensions.vertex_input_dynamic_state ? nullptr :
196 nullptr,
197 nullptr,
198 nullptr,
199 nullptr,
200 nullptr,
201 nullptr,
203 VK_NULL_HANDLE,
204 VK_NULL_HANDLE,
205 0,
206 vk_pipeline_base,
207 0};
208 }
209
210 void build_graphics_pipeline_shaders_lib(const VKGraphicsInfo::Shaders &shaders_info,
211 VkPipeline vk_pipeline_base)
212 {
214 VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
216 VK_PIPELINE_CREATE_LIBRARY_BIT_KHR |
217 VK_PIPELINE_CREATE_RETAIN_LINK_TIME_OPTIMIZATION_INFO_BIT_EXT,
218 shaders_info.vk_geometry_module == VK_NULL_HANDLE ? 2u : 3u,
220 nullptr,
221 nullptr,
222 nullptr,
227 nullptr,
229 shaders_info.vk_pipeline_layout,
230 VK_NULL_HANDLE,
231 0,
232 vk_pipeline_base,
233 0};
235 }
236
237 void build_graphics_pipeline_fragment_output_lib(VkPipeline vk_pipeline_base)
238 {
240 VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
242 VK_PIPELINE_CREATE_LIBRARY_BIT_KHR |
243 VK_PIPELINE_CREATE_RETAIN_LINK_TIME_OPTIMIZATION_INFO_BIT_EXT,
244 0,
245 nullptr,
246 nullptr,
247 nullptr,
248 nullptr,
249 nullptr,
250 nullptr,
252 nullptr,
254 nullptr,
255 VK_NULL_HANDLE,
256 VK_NULL_HANDLE,
257 0,
258 vk_pipeline_base,
259 0};
261 }
262
263 void build_graphics_pipeline_library(VkGraphicsPipelineLibraryFlagsEXT flags)
264 {
266 VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_LIBRARY_CREATE_INFO_EXT, nullptr, flags};
267 }
268
269 void build_shader_stages(const VKGraphicsInfo::Shaders &shaders_info)
270 {
271 vk_pipeline_shader_stage_create_info[0] = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
272 nullptr,
273 0,
274 VK_SHADER_STAGE_VERTEX_BIT,
275 shaders_info.vk_vertex_module,
276 "main",
277 nullptr};
278 vk_pipeline_shader_stage_create_info[1] = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
279 nullptr,
280 0,
281 VK_SHADER_STAGE_FRAGMENT_BIT,
282 shaders_info.vk_fragment_module,
283 "main",
284 nullptr};
285 vk_pipeline_shader_stage_create_info[2] = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
286 nullptr,
287 0,
288 VK_SHADER_STAGE_GEOMETRY_BIT,
289 shaders_info.vk_geometry_module,
290 "main",
291 nullptr};
292 }
293
294 void build_specialization_constants(const VKGraphicsInfo::Shaders &shaders_info)
295 {
296 vk_specialization_map_entries.reinitialize(shaders_info.specialization_constants.size());
297 for (int index : IndexRange(3)) {
299 }
300 for (uint32_t index : IndexRange(shaders_info.specialization_constants.size())) {
302 index, uint32_t(index * sizeof(uint32_t)), sizeof(uint32_t)};
303 }
306 shaders_info.specialization_constants.size() * sizeof(uint32_t),
307 shaders_info.specialization_constants.data()};
308 }
309
310 void build_multisample_state()
311 {
313 VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
314 nullptr,
315 0,
316 VK_SAMPLE_COUNT_1_BIT,
317 VK_FALSE,
318 1.0f,
319 nullptr,
320 VK_FALSE,
321 VK_FALSE};
322 }
323
324 void build_viewport_state(const VKGraphicsInfo::Shaders &shaders_info,
325 const VKWorkarounds &workarounds)
326 {
328 VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
329 nullptr,
330 0,
331 shaders_info.viewport_count,
332 nullptr,
333 shaders_info.viewport_count,
334 nullptr};
335 if (workarounds.static_viewport_scissor) {
336 BLI_assert(shaders_info.viewports.size() == shaders_info.viewport_count);
337 BLI_assert(shaders_info.scissors.size() == shaders_info.viewport_count);
338 vk_pipeline_viewport_state_create_info.pViewports = shaders_info.viewports.data();
339 vk_pipeline_viewport_state_create_info.pScissors = shaders_info.scissors.data();
340 }
341 }
342
343 void build_input_assembly_state(const VKGraphicsInfo::VertexIn &vertex_input_info)
344 {
346 VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO};
347 vk_pipeline_input_assembly_state_create_info.topology = vertex_input_info.vk_topology;
348 vk_pipeline_input_assembly_state_create_info.primitiveRestartEnable =
349 ELEM(vertex_input_info.vk_topology,
350 VK_PRIMITIVE_TOPOLOGY_POINT_LIST,
351 VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
352 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
353 VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY) ?
354 VK_FALSE :
355 VK_TRUE;
356 }
357
358 void build_vertex_input_state(VKDevice &device,
359 const VKGraphicsInfo::VertexIn &vertex_input_info)
360 {
361 BLI_assert_msg(!device.extensions_get().vertex_input_dynamic_state,
362 "No need to set vertex input state as dynamic state is supported.");
363 const VKVertexInputDescription &description = device.vertex_input_descriptions.get(
364 vertex_input_info.vertex_input_key);
366 VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO};
367 vk_vertex_input_attribute_descriptions.reserve(description.attributes.size());
368 vk_vertex_input_binding_descriptions.reserve(description.bindings.size());
369 for (const VkVertexInputAttributeDescription2EXT &attr : description.attributes) {
371 {attr.location, attr.binding, attr.format, attr.offset});
372 }
373 for (const VkVertexInputBindingDescription2EXT &bind : description.bindings) {
374 vk_vertex_input_binding_descriptions.append({bind.binding, bind.stride, bind.inputRate});
375 }
376
377 vk_pipeline_vertex_input_state_create_info.pVertexAttributeDescriptions =
379 vk_pipeline_vertex_input_state_create_info.vertexAttributeDescriptionCount =
381 vk_pipeline_vertex_input_state_create_info.pVertexBindingDescriptions =
383 vk_pipeline_vertex_input_state_create_info.vertexBindingDescriptionCount =
385 }
386
387 void build_rasterization_state(const VKGraphicsInfo::Shaders &shaders_info,
388 const VKExtensions &extensions)
389 {
392 VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
394 vk_pipeline_rasterization_state_create_info.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
395
396 if (extensions.provoking_vertex) {
399 VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_PROVOKING_VERTEX_STATE_CREATE_INFO_EXT;
401 shaders_info.state.provoking_vert == GPU_VERTEX_LAST ?
402 VK_PROVOKING_VERTEX_MODE_LAST_VERTEX_EXT :
403 VK_PROVOKING_VERTEX_MODE_FIRST_VERTEX_EXT;
404
407 }
408
410 static_cast<GPUFaceCullTest>(shaders_info.state.culling_test));
411 if (!extensions.extended_dynamic_state && !shaders_info.state.invert_facing) {
412 vk_pipeline_rasterization_state_create_info.frontFace = VK_FRONT_FACE_CLOCKWISE;
413 }
414
415 if (extensions.line_rasterization) {
416 /* Request use of Bresenham algorithm if supported. */
419 VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_LINE_STATE_CREATE_INFO_EXT;
420 vk_pipeline_rasterization_line_state_info.lineRasterizationMode =
421 VK_LINE_RASTERIZATION_MODE_BRESENHAM_EXT;
422 vk_pipeline_rasterization_line_state_info.stippledLineEnable = VK_FALSE;
423 vk_pipeline_rasterization_line_state_info.lineStippleFactor = 0u;
424 vk_pipeline_rasterization_line_state_info.lineStipplePattern = 0u;
429 }
430 }
431
432 void build_dynamic_state(const VKGraphicsInfo::Shaders &shaders_info,
433 const VKExtensions &extensions,
434 const VKWorkarounds &workarounds)
435 {
436 /* Viewports & scissors are dynamic unless baked into the pipeline
437 * (see `VKWorkarounds::static_viewport_scissor`). */
438 if (!workarounds.static_viewport_scissor) {
439 vk_dynamic_states.append(VK_DYNAMIC_STATE_VIEWPORT);
440 vk_dynamic_states.append(VK_DYNAMIC_STATE_SCISSOR);
441 }
442 const bool is_line_topology = ELEM(shaders_info.vk_topology,
443 VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
444 VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY,
445 VK_PRIMITIVE_TOPOLOGY_LINE_STRIP);
446 if (is_line_topology) {
447 vk_dynamic_states.append(VK_DYNAMIC_STATE_LINE_WIDTH);
448 }
449 if (shaders_info.has_stencil && shaders_info.state.stencil_test != GPU_STENCIL_NONE) {
450 vk_dynamic_states.append(VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK);
451 vk_dynamic_states.append(VK_DYNAMIC_STATE_STENCIL_REFERENCE);
452 vk_dynamic_states.append(VK_DYNAMIC_STATE_STENCIL_WRITE_MASK);
453 }
454 if (extensions.extended_dynamic_state) {
455 vk_dynamic_states.append(VK_DYNAMIC_STATE_FRONT_FACE);
456 }
457 if (extensions.vertex_input_dynamic_state) {
458 vk_dynamic_states.append(VK_DYNAMIC_STATE_VERTEX_INPUT_EXT);
459 }
460 vk_pipeline_dynamic_state_create_info = {VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
461 nullptr,
462 0,
465 }
466
467 void build_dynamic_state_for_vertex_input(const VKExtensions &extensions)
468 {
469 if (extensions.vertex_input_dynamic_state) {
470 vk_dynamic_states = {VK_DYNAMIC_STATE_VERTEX_INPUT_EXT};
471 }
472 vk_pipeline_dynamic_state_create_info = {VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
473 nullptr,
474 0,
477 }
478
479 void build_depth_stencil_state(const VKGraphicsInfo::Shaders &shaders_info)
480 {
482 VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO};
483 if (shaders_info.has_depth) {
484 vk_graphics_pipeline_create_info.pDepthStencilState =
487 (shaders_info.state.write_mask & GPU_WRITE_DEPTH) ? VK_TRUE : VK_FALSE;
488
489 vk_pipeline_depth_stencil_state_create_info.depthTestEnable = VK_TRUE;
490 switch (shaders_info.state.depth_test) {
491 case GPU_DEPTH_LESS:
492 vk_pipeline_depth_stencil_state_create_info.depthCompareOp = VK_COMPARE_OP_LESS;
493 break;
495 vk_pipeline_depth_stencil_state_create_info.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
496 break;
497 case GPU_DEPTH_EQUAL:
498 vk_pipeline_depth_stencil_state_create_info.depthCompareOp = VK_COMPARE_OP_EQUAL;
499 break;
501 vk_pipeline_depth_stencil_state_create_info.depthCompareOp = VK_COMPARE_OP_GREATER;
502 break;
505 VK_COMPARE_OP_GREATER_OR_EQUAL;
506 break;
507 case GPU_DEPTH_ALWAYS:
508 vk_pipeline_depth_stencil_state_create_info.depthCompareOp = VK_COMPARE_OP_ALWAYS;
509 break;
510 case GPU_DEPTH_NONE:
511 vk_pipeline_depth_stencil_state_create_info.depthTestEnable = VK_FALSE;
512 vk_pipeline_depth_stencil_state_create_info.depthCompareOp = VK_COMPARE_OP_NEVER;
513 break;
514 }
515 }
516
517 if (shaders_info.has_stencil) {
518 vk_graphics_pipeline_create_info.pDepthStencilState =
520
521 switch (shaders_info.state.stencil_test) {
523 vk_pipeline_depth_stencil_state_create_info.stencilTestEnable = VK_TRUE;
524 vk_pipeline_depth_stencil_state_create_info.front.compareOp = VK_COMPARE_OP_NOT_EQUAL;
525 break;
527 vk_pipeline_depth_stencil_state_create_info.stencilTestEnable = VK_TRUE;
528 vk_pipeline_depth_stencil_state_create_info.front.compareOp = VK_COMPARE_OP_EQUAL;
529 break;
531 vk_pipeline_depth_stencil_state_create_info.stencilTestEnable = VK_TRUE;
532 vk_pipeline_depth_stencil_state_create_info.front.compareOp = VK_COMPARE_OP_ALWAYS;
533 break;
534 case GPU_STENCIL_NONE:
535 vk_pipeline_depth_stencil_state_create_info.stencilTestEnable = VK_FALSE;
536 vk_pipeline_depth_stencil_state_create_info.front.compareOp = VK_COMPARE_OP_ALWAYS;
537 break;
538 }
539
540 switch (shaders_info.state.stencil_op) {
542 vk_pipeline_depth_stencil_state_create_info.front.failOp = VK_STENCIL_OP_KEEP;
543 vk_pipeline_depth_stencil_state_create_info.front.passOp = VK_STENCIL_OP_REPLACE;
544 vk_pipeline_depth_stencil_state_create_info.front.depthFailOp = VK_STENCIL_OP_KEEP;
547 break;
548
550 vk_pipeline_depth_stencil_state_create_info.front.failOp = VK_STENCIL_OP_KEEP;
552 VK_STENCIL_OP_DECREMENT_AND_WRAP;
553 vk_pipeline_depth_stencil_state_create_info.front.depthFailOp = VK_STENCIL_OP_KEEP;
557 VK_STENCIL_OP_INCREMENT_AND_WRAP;
558 break;
559
561 vk_pipeline_depth_stencil_state_create_info.front.failOp = VK_STENCIL_OP_KEEP;
562 vk_pipeline_depth_stencil_state_create_info.front.passOp = VK_STENCIL_OP_KEEP;
564 VK_STENCIL_OP_INCREMENT_AND_WRAP;
568 VK_STENCIL_OP_DECREMENT_AND_WRAP;
569 break;
570
572 default:
573 vk_pipeline_depth_stencil_state_create_info.front.failOp = VK_STENCIL_OP_KEEP;
574 vk_pipeline_depth_stencil_state_create_info.front.passOp = VK_STENCIL_OP_KEEP;
575 vk_pipeline_depth_stencil_state_create_info.front.depthFailOp = VK_STENCIL_OP_KEEP;
578 break;
579 }
580 }
581 }
582
583 void build_dynamic_rendering(const VKGraphicsInfo::FragmentOut &fragment_output_info)
584 {
586 VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO,
587 nullptr,
588 0,
589 uint32_t(fragment_output_info.color_attachment_formats.size()),
590 fragment_output_info.color_attachment_formats.data(),
591 fragment_output_info.depth_attachment_format,
592 fragment_output_info.stencil_attachment_format};
593 }
594
595 /* Shaders lib only requires the view-mask to be set. When dynamic rendering local read is
596 * used and the shader declares input attachments, the render pass color attachment count
597 * must be declared and VkRenderingInputAttachmentIndexInfo provided. The count must equal
598 * the fragment output library's VkPipelineRenderingCreateInfo::colorAttachmentCount
599 * (VUID-VkGraphicsPipelineCreateInfo-renderPass-09531). */
600 void build_dynamic_rendering_shaders_lib(const VKExtensions &extensions,
601 const VKGraphicsInfo::Shaders &shaders_info)
602 {
603 vk_pipeline_rendering_create_info = {VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO};
604 if (extensions.dynamic_rendering_local_read && shaders_info.max_input_attachment_index > 0) {
605 const uint32_t color_attachment_count = shaders_info.color_attachment_count;
606 vk_pipeline_rendering_create_info.colorAttachmentCount = color_attachment_count;
607 dummy_color_attachment_formats_.resize(color_attachment_count, VK_FORMAT_UNDEFINED);
608 vk_pipeline_rendering_create_info.pColorAttachmentFormats =
610
613 VK_STRUCTURE_TYPE_RENDERING_INPUT_ATTACHMENT_INDEX_INFO;
614 vk_rendering_input_attachment_index_info_.colorAttachmentCount = color_attachment_count;
616 }
617 }
618
619 void build_color_blend_attachment_states(const VKGraphicsInfo::FragmentOut &fragment_output_info)
620 {
621 VkPipelineColorBlendAttachmentState attachment_state = {VK_TRUE,
622 VK_BLEND_FACTOR_DST_ALPHA,
623 VK_BLEND_FACTOR_ONE,
624 VK_BLEND_OP_ADD,
625 VK_BLEND_FACTOR_ZERO,
626 VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
627 VK_BLEND_OP_ADD,
628 0};
629
630 switch (fragment_output_info.state.blend) {
631 default:
632 case GPU_BLEND_ALPHA:
633 attachment_state.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
634 attachment_state.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
635 attachment_state.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
636 attachment_state.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
637 break;
638
640 attachment_state.srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
641 attachment_state.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
642 attachment_state.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
643 attachment_state.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
644 break;
645
647 attachment_state.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
648 attachment_state.dstColorBlendFactor = VK_BLEND_FACTOR_ONE;
649 attachment_state.srcAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
650 attachment_state.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
651 break;
652
653 /* Factors are not use in min or max mode, but avoid uninitialized values. */;
654 case GPU_BLEND_MIN:
655 case GPU_BLEND_MAX:
658 attachment_state.srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
659 attachment_state.dstColorBlendFactor = VK_BLEND_FACTOR_ONE;
660 attachment_state.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
661 attachment_state.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
662 break;
663
665 attachment_state.srcColorBlendFactor = VK_BLEND_FACTOR_DST_COLOR;
666 attachment_state.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO;
667 attachment_state.srcAlphaBlendFactor = VK_BLEND_FACTOR_DST_ALPHA;
668 attachment_state.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
669 break;
670
671 case GPU_BLEND_INVERT:
672 attachment_state.srcColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR;
673 attachment_state.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO;
674 attachment_state.srcAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
675 attachment_state.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
676 break;
677
678 case GPU_BLEND_OIT:
679 attachment_state.srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
680 attachment_state.dstColorBlendFactor = VK_BLEND_FACTOR_ONE;
681 attachment_state.srcAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
682 attachment_state.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
683 break;
684
686 attachment_state.srcColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA;
687 attachment_state.dstColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
688 attachment_state.srcAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
689 attachment_state.dstAlphaBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
690 break;
691
693 attachment_state.srcColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA;
694 attachment_state.dstColorBlendFactor = VK_BLEND_FACTOR_ONE;
695 attachment_state.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA;
696 attachment_state.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
697 break;
698
699 case GPU_BLEND_CUSTOM:
700 attachment_state.srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
701 attachment_state.dstColorBlendFactor = VK_BLEND_FACTOR_SRC1_COLOR;
702 attachment_state.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
703 attachment_state.dstAlphaBlendFactor = VK_BLEND_FACTOR_SRC1_ALPHA;
704 break;
705
707 attachment_state.srcColorBlendFactor = VK_BLEND_FACTOR_ZERO;
708 attachment_state.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
709 attachment_state.srcAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
710 attachment_state.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
711 break;
712
714 attachment_state.srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
715 attachment_state.dstColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
716 attachment_state.srcAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
717 attachment_state.dstAlphaBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
718 break;
719 }
720
721 if (fragment_output_info.state.blend == GPU_BLEND_MIN) {
722 attachment_state.alphaBlendOp = VK_BLEND_OP_MIN;
723 attachment_state.colorBlendOp = VK_BLEND_OP_MIN;
724 }
725 else if (fragment_output_info.state.blend == GPU_BLEND_MAX) {
726 attachment_state.alphaBlendOp = VK_BLEND_OP_MAX;
727 attachment_state.colorBlendOp = VK_BLEND_OP_MAX;
728 }
729 else if (fragment_output_info.state.blend == GPU_BLEND_SUBTRACT) {
730 attachment_state.alphaBlendOp = VK_BLEND_OP_REVERSE_SUBTRACT;
731 attachment_state.colorBlendOp = VK_BLEND_OP_REVERSE_SUBTRACT;
732 }
733 else {
734 attachment_state.alphaBlendOp = VK_BLEND_OP_ADD;
735 attachment_state.colorBlendOp = VK_BLEND_OP_ADD;
736 }
737
738 if (fragment_output_info.state.blend != GPU_BLEND_NONE) {
739 attachment_state.blendEnable = VK_TRUE;
740 }
741 else {
742 attachment_state.blendEnable = VK_FALSE;
743 }
744
745 /* Adjust the template with the color components in the write mask. */
746 if ((fragment_output_info.state.write_mask & GPU_WRITE_RED) != 0) {
747 attachment_state.colorWriteMask |= VK_COLOR_COMPONENT_R_BIT;
748 }
749 if ((fragment_output_info.state.write_mask & GPU_WRITE_GREEN) != 0) {
750 attachment_state.colorWriteMask |= VK_COLOR_COMPONENT_G_BIT;
751 }
752 if ((fragment_output_info.state.write_mask & GPU_WRITE_BLUE) != 0) {
753 attachment_state.colorWriteMask |= VK_COLOR_COMPONENT_B_BIT;
754 }
755 if ((fragment_output_info.state.write_mask & GPU_WRITE_ALPHA) != 0) {
756 attachment_state.colorWriteMask |= VK_COLOR_COMPONENT_A_BIT;
757 }
758
760 attachment_state, fragment_output_info.color_attachment_formats.size());
761 }
762
763 void build_color_blend_state()
764 {
766 VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
767 nullptr,
768 0,
769 VK_FALSE,
770 VK_LOGIC_OP_CLEAR,
773 {1.0f, 1.0f, 1.0f, 1.0f}};
774 }
775};
776} // namespace blender::gpu
#define BLI_assert(a)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
int64_t size() const
Definition BLI_array.hh:267
const T * data() const
Definition BLI_array.hh:323
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
constexpr IndexRange()=default
const VKExtensions & extensions_get() const
Definition vk_device.hh:398
const VKWorkarounds & workarounds_get() const
Definition vk_device.hh:394
#define index(i)
VkCullModeFlags to_vk_cull_mode_flags(const GPUFaceCullTest cull_test)
Definition vk_common.cc:698
@ GPU_STENCIL_OP_COUNT_DEPTH_FAIL
Definition GPU_state.hh:153
@ GPU_STENCIL_OP_COUNT_DEPTH_PASS
Definition GPU_state.hh:152
@ GPU_STENCIL_OP_NONE
Definition GPU_state.hh:149
@ GPU_STENCIL_OP_REPLACE
Definition GPU_state.hh:150
@ GPU_BLEND_MIN
Definition GPU_state.hh:102
@ GPU_BLEND_OVERLAY_MASK_FROM_ALPHA
Definition GPU_state.hh:122
@ GPU_BLEND_TRANSPARENCY
Definition GPU_state.hh:128
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_OIT
Definition GPU_state.hh:109
@ GPU_BLEND_MAX
Definition GPU_state.hh:104
@ GPU_BLEND_ALPHA_UNDER_PREMUL
Definition GPU_state.hh:117
@ GPU_BLEND_ADDITIVE_PREMULT
Definition GPU_state.hh:93
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
@ GPU_BLEND_ADDITIVE
Definition GPU_state.hh:92
@ GPU_BLEND_BACKGROUND
Definition GPU_state.hh:111
@ GPU_BLEND_SUBTRACT
Definition GPU_state.hh:95
@ GPU_BLEND_ALPHA_PREMULT
Definition GPU_state.hh:91
@ GPU_BLEND_INVERT
Definition GPU_state.hh:100
@ GPU_BLEND_MULTIPLY
Definition GPU_state.hh:94
@ GPU_BLEND_CUSTOM
Definition GPU_state.hh:116
unsigned int uint32_t
Definition BKE_text.h:81
@ GPU_DEPTH_NONE
Definition GPU_state.hh:132
@ GPU_DEPTH_LESS_EQUAL
Definition GPU_state.hh:135
@ GPU_DEPTH_ALWAYS
Definition GPU_state.hh:133
@ GPU_DEPTH_GREATER
Definition GPU_state.hh:137
@ GPU_DEPTH_LESS
Definition GPU_state.hh:134
@ GPU_DEPTH_GREATER_EQUAL
Definition GPU_state.hh:138
@ GPU_DEPTH_EQUAL
Definition GPU_state.hh:136
@ GPU_STENCIL_ALWAYS
Definition GPU_state.hh:143
@ GPU_STENCIL_EQUAL
Definition GPU_state.hh:144
@ GPU_STENCIL_NONE
Definition GPU_state.hh:142
@ GPU_STENCIL_NEQUAL
Definition GPU_state.hh:145
@ GPU_WRITE_ALPHA
Definition GPU_state.hh:24
@ GPU_WRITE_RED
Definition GPU_state.hh:21
@ GPU_WRITE_BLUE
Definition GPU_state.hh:23
@ GPU_WRITE_GREEN
Definition GPU_state.hh:22
@ GPU_WRITE_DEPTH
Definition GPU_state.hh:25
@ GPU_VERTEX_LAST
Definition GPU_state.hh:163
Vector< shader::SpecializationConstant::Value > specialization_constants
void build_vertex_input_lib(VKDevice &device, const VKGraphicsInfo::VertexIn &vertex_input_info, VkPipeline vk_pipeline_base)
VkPipelineRasterizationStateCreateInfo vk_pipeline_rasterization_state_create_info
void build_full(VKDevice &device, const VKGraphicsInfo &graphics_info, VkPipeline vk_pipeline_base)
VkPipelineMultisampleStateCreateInfo vk_pipeline_multisample_state_create_info
void build_fragment_output_lib(const VKGraphicsInfo::FragmentOut &fragment_output_info, VkPipeline vk_pipeline_base)
VkPipelineDynamicStateCreateInfo vk_pipeline_dynamic_state_create_info
std::array< VkPipelineShaderStageCreateInfo, 3 > vk_pipeline_shader_stage_create_info
VkPipelineRasterizationProvokingVertexStateCreateInfoEXT vk_pipeline_rasterization_provoking_vertex_state_info
Vector< VkPipelineColorBlendAttachmentState > vk_pipeline_color_blend_attachment_states
Vector< VkFormat, GPU_FB_MAX_COLOR_ATTACHMENT > dummy_color_attachment_formats_
VkPipelineDepthStencilStateCreateInfo vk_pipeline_depth_stencil_state_create_info
VkPipelineRasterizationLineStateCreateInfoEXT vk_pipeline_rasterization_line_state_info
VkRenderingInputAttachmentIndexInfo vk_rendering_input_attachment_index_info_
Array< VkSpecializationMapEntry > vk_specialization_map_entries
Vector< VkVertexInputAttributeDescription > vk_vertex_input_attribute_descriptions
VkPipelineColorBlendStateCreateInfo vk_pipeline_color_blend_state_create_info
void build_shaders_lib(const VKGraphicsInfo::Shaders &shaders_info, const VKExtensions &extensions, const VKWorkarounds &workarounds, VkPipeline vk_pipeline_base)
Vector< VkVertexInputBindingDescription > vk_vertex_input_binding_descriptions
VkGraphicsPipelineLibraryCreateInfoEXT vk_graphics_pipeline_library_create_info
VkPipelineInputAssemblyStateCreateInfo vk_pipeline_input_assembly_state_create_info
VkPipelineVertexInputStateCreateInfo vk_pipeline_vertex_input_state_create_info
VkPipelineViewportStateCreateInfo vk_pipeline_viewport_state_create_info
VkPipelineRenderingCreateInfo vk_pipeline_rendering_create_info
int size() const
Definition vector.hh:130
void reserve(int new_size)
Definition vector.hh:78
void resize(int new_size)
Definition vector.hh:94