UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
VulkanRHIPrivate.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved..
2
3#pragma once
4
5// Dependencies
6#include "CoreMinimal.h"
8#include "Misc/ScopeLock.h"
9#include "RHI.h"
10#include "RenderUtils.h"
11#include "RHIValidation.h"
12
13// let the platform set up the headers and some defines
14#include "VulkanPlatform.h"
15
16// the configuration will set up anything not set up by the platform
17#include "VulkanConfiguration.h"
18
20
21#include "VulkanState.h"
22#include "VulkanResources.h"
23#include "VulkanUtil.h"
24#include "VulkanViewport.h"
25#include "VulkanDynamicRHI.h"
26#include "RHI.h"
27
28#include "GPUProfiler.h"
29#include "VulkanQueue.h"
30#include "VulkanCommandBuffer.h"
31#include "Stats/Stats.h"
32
33class FVulkanQueue;
35class FVulkanShader;
37class FVulkanGfxPipeline;
40
41
42template<typename BitsType>
43constexpr bool VKHasAllFlags(VkFlags Flags, BitsType Contains)
44{
45 return (Flags & Contains) == Contains;
46}
47
48template<typename BitsType>
49constexpr bool VKHasAnyFlags(VkFlags Flags, BitsType Contains)
50{
51 return (Flags & Contains) != 0;
52}
53
55{
56 switch (InStage)
57 {
66 case SF_RayHitGroup: return VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR; // vkrt todo: How to handle VK_SHADER_STAGE_ANY_HIT_BIT_KHR?
68 default:
69 checkf(false, TEXT("Undefined shader stage %d"), (int32)InStage);
70 break;
71 }
72
74}
75
77{
78 switch (FlagBits)
79 {
86
90
91 // Hit group frequencies
95 return SF_RayHitGroup;
96
97 default:
98 checkf(false, TEXT("Undefined VkShaderStageFlagBits %d"), (int32)FlagBits);
99 break;
100 }
101
102 return SF_NumFrequencies;
103}
104
105static constexpr int32 GetNumStagesForBindPoint(VkPipelineBindPoint BindPoint)
106{
107 switch (BindPoint)
108 {
111
114
117 };
118
119 return 0;
120}
121
123{
124 struct
125 {
126#if PLATFORM_LITTLE_ENDIAN
127 uint32 Tertiary : 6;
128 uint32 Secondary : 8;
129 uint32 Minor : 8;
130 uint32 Major : 10;
131#else
136#endif
137 };
139};
140
141// Transitions an image to the specified layout. This does not update the layout cached internally by the RHI; the calling code must do that explicitly via FVulkanCommandListContext::GetLayoutManager() if necessary.
143
144
145// Regular pipeline creation stats
153
158
159
160// Graphic pipeline library stats
170
171
172// General Stats
206#if VULKAN_ENABLE_AGGRESSIVE_STATS
214#endif
216
217namespace VulkanRHI
218{
219 static uint32 GetNumBitsPerPixel(VkFormat Format)
220 {
221 switch (Format)
222 {
238 return 32;
241 return 8;
249 return 16;
260 return 64;
263 return 128;
265 return 8;
270 return 4;
279 return 8;
284 return 8;
285
286 // No pixel, only blocks!
287#if PLATFORM_DESKTOP
288 //MapFormatSupport(PF_DXT1, VK_FORMAT_BC1_RGB_UNORM_BLOCK); // Also what OpenGL expects (RGBA instead RGB, but not SRGB)
289 //MapFormatSupport(PF_DXT3, VK_FORMAT_BC2_UNORM_BLOCK);
290 //MapFormatSupport(PF_DXT5, VK_FORMAT_BC3_UNORM_BLOCK);
291 //MapFormatSupport(PF_BC4, VK_FORMAT_BC4_UNORM_BLOCK);
292 //MapFormatSupport(PF_BC5, VK_FORMAT_BC5_UNORM_BLOCK);
293 //MapFormatSupport(PF_BC6H, VK_FORMAT_BC6H_UFLOAT_BLOCK);
294 //MapFormatSupport(PF_BC7, VK_FORMAT_BC7_UNORM_BLOCK);
295#elif PLATFORM_ANDROID
296 //MapFormatSupport(PF_ASTC_4x4, VK_FORMAT_ASTC_4x4_UNORM_BLOCK);
297 //MapFormatSupport(PF_ASTC_6x6, VK_FORMAT_ASTC_6x6_UNORM_BLOCK);
298 //MapFormatSupport(PF_ASTC_8x8, VK_FORMAT_ASTC_8x8_UNORM_BLOCK);
299 //MapFormatSupport(PF_ASTC_10x10, VK_FORMAT_ASTC_10x10_UNORM_BLOCK);
300 //MapFormatSupport(PF_ASTC_12x12, VK_FORMAT_ASTC_12x12_UNORM_BLOCK);
301 //MapFormatSupport(PF_ETC1, VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK);
302 //MapFormatSupport(PF_ETC2_RGB, VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK);
303 //MapFormatSupport(PF_ETC2_RGBA, VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK);
304#endif
305 default:
306 break;
307 }
308
309 checkf(0, TEXT("Unhandled bits per pixel for VkFormat %d"), (uint32)Format);
310 return 8;
311 }
312
313 static VkImageAspectFlags GetAspectMaskFromUEFormat(EPixelFormat Format, bool bIncludeStencil, bool bIncludeDepth = true)
314 {
315 switch (Format)
316 {
317 case PF_X24_G8:
319 case PF_DepthStencil:
321 case PF_ShadowDepth:
322 case PF_D24:
324 default:
326 }
327 }
328
329 static bool VulkanFormatHasStencil(VkFormat Format)
330 {
331 switch (Format)
332 {
337 return true;
338 default:
339 return false;
340 }
341 }
342}
343
344#if VULKAN_HAS_DEBUGGING_ENABLED
346#endif
347
348static inline VkAttachmentLoadOp RenderTargetLoadActionToVulkan(ERenderTargetLoadAction InLoadAction)
349{
351
352 switch (InLoadAction)
353 {
357 default: break;
358 }
359
360 // Check for missing translation
362 return OutLoadAction;
363}
364
365static inline VkAttachmentStoreOp RenderTargetStoreActionToVulkan(ERenderTargetStoreAction InStoreAction)
366{
368
369 switch (InStoreAction)
370 {
373 break;
377 break;
378 default:
379 break;
380 }
381
382 // Check for missing translation
384 return OutStoreAction;
385}
386
388inline VkFormat UEToVkTextureFormat(EPixelFormat UEFormat, const bool bIsSRGB)
389{
390 if (bIsSRGB)
391 {
392 return GVulkanSRGBFormat[UEFormat];
393 }
394 else
395 {
396 return (VkFormat)GPixelFormats[UEFormat].PlatformFormat;
397 }
398}
399
400static inline VkFormat UEToVkBufferFormat(EVertexElementType Type)
401{
402 switch (Type)
403 {
404 case VET_Float1:
406 case VET_Float2:
408 case VET_Float3:
410 case VET_PackedNormal:
412 case VET_UByte4:
414 case VET_UByte4N:
416 case VET_Color:
418 case VET_Short2:
420 case VET_Short4:
422 case VET_Short2N:
424 case VET_Half2:
426 case VET_Half4:
428 case VET_Short4N: // 4 X 16 bit word: normalized
430 case VET_UShort2:
432 case VET_UShort4:
434 case VET_UShort2N: // 16 bit word normalized to (value/65535.0:value/65535.0:0:0:1)
436 case VET_UShort4N: // 4 X 16 bit word unsigned: normalized
438 case VET_Float4:
440 case VET_URGB10A2N:
442 case VET_UInt:
443 return VK_FORMAT_R32_UINT;
444 default:
445 break;
446 }
447
448 check(!"Undefined vertex-element format conversion");
449 return VK_FORMAT_UNDEFINED;
450}
451
452static inline bool IsAstcLdrFormat(VkFormat Format)
453{
455}
456
457static inline bool IsAstcSrgbFormat(VkFormat Format)
458{
459 switch (Format)
460 {
475 return true;
476 default:
477 return false;
478 }
479}
480
481#if UE_BUILD_DEBUG || UE_BUILD_DEVELOPMENT
483#endif
484
485namespace VulkanRHI
486{
487 static inline FString GetPipelineCacheFilename()
488 {
489 return FPaths::ProjectSavedDir() / TEXT("VulkanPSO.cache");
490 }
491
492 static inline FString GetValidationCacheFilename()
493 {
494 return FPaths::ProjectSavedDir() / TEXT("VulkanValidation.cache");
495 }
496
497#if VULKAN_ENABLE_DRAW_MARKERS
498 inline void SetDebugName(PFN_vkSetDebugUtilsObjectNameEXT SetDebugName, VkDevice Device, VkImage Image, const char* Name)
499 {
502 Info.objectType = VK_OBJECT_TYPE_IMAGE;
503 Info.objectHandle = (uint64)Image;
504 Info.pObjectName = Name;
505 SetDebugName(Device, &Info);
506}
507#endif
508
509 // Merge a depth and a stencil layout for drivers that don't support VK_KHR_separate_depth_stencil_layouts
511 {
513 {
515 TEXT("You can't merge transfer src layout without anything else than transfer src (%s != %s). ")
516 TEXT("You need either VK_KHR_separate_depth_stencil_layouts or GRHISupportsSeparateDepthStencilCopyAccess enabled."),
519 }
520
522 {
524 TEXT("You can't merge transfer dst layout without anything else than transfer dst (%s != %s). ")
525 TEXT("You need either VK_KHR_separate_depth_stencil_layouts or GRHISupportsSeparateDepthStencilCopyAccess enabled."),
528 }
529
531 {
533 }
534
535 // Depth formats used on textures that aren't targets (like GBlackTextureDepthCube)
537 {
539 }
540
542 {
547 };
548
550 {
552 TEXT("Layouts were already merged but they are mismatched (%s != %s)."),
554 return DepthLayout;
555 }
556
558 {
560 {
562 }
563 else
564 {
567 }
568 }
570 {
572 {
574 }
575 else
576 {
579 }
580 }
581 else
582 {
584 }
585 }
586
587 inline void HeavyWeightBarrier(VkCommandBuffer CmdBuffer)
588 {
590 ZeroVulkanStruct(Barrier, VK_STRUCTURE_TYPE_MEMORY_BARRIER);
629 VulkanRHI::vkCmdPipelineBarrier(CmdBuffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 1, &Barrier, 0, nullptr, 0, nullptr);
630 }
631
633 {
634#if UE_BUILD_DEBUG || UE_BUILD_DEVELOPMENT
635 if (CVarVulkanDebugBarrier.GetValueOnAnyThread() & CVarConditionMask)
636 {
637 HeavyWeightBarrier(CmdBuffer);
638 }
639#endif
640 }
641}
642
644{
645 // Descriptor cache path for WriteAccelerationStructure() is not implemented, so disable if RT is enabled
647}
648
650{
652 {
653 case VRSImage_Palette:
654 return true;
655
657 return true;
658
660 checkf(false, TEXT("A texture was marked as a shading rate source but attachment VRS is not supported on this device. Ensure GRHISupportsAttachmentVariableRateShading and GRHIAttachmentVariableRateShadingEnabled are true before specifying a shading rate attachment."));
661 break;
662
663 default:
664 checkf(false, TEXT("Unrecognized shading rate image data type. Specified type was %d"), (int)GRHIVariableRateShadingImageDataType);
665 break;
666 }
667
668 return false;
669}
670
672
673#if VULKAN_HAS_DEBUGGING_ENABLED
674extern bool GRenderDocFound;
675#endif
676
677const int GMaxCrashBufferEntries = 2048;
678
680
#define check(expr)
Definition AssertionMacros.h:314
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
#define DECLARE_CYCLE_STAT_EXTERN(CounterName, StatId, GroupId, API)
Definition Stats.h:679
#define DECLARE_DWORD_COUNTER_STAT_EXTERN(CounterName, StatId, GroupId, API)
Definition Stats.h:682
#define DECLARE_STATS_GROUP(GroupDesc, GroupId, GroupCat)
Definition Stats.h:689
#define DECLARE_DWORD_ACCUMULATOR_STAT_EXTERN(CounterName, StatId, GroupId, API)
Definition Stats.h:684
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
FPixelFormatInfo GPixelFormats[PF_MAX]
Definition PixelFormat.cpp:31
EPixelFormat
Definition PixelFormat.h:16
@ PF_DepthStencil
Definition PixelFormat.h:28
@ PF_MAX
Definition PixelFormat.h:111
@ PF_D24
Definition PixelFormat.h:37
@ PF_ShadowDepth
Definition PixelFormat.h:29
@ PF_X24_G8
Definition PixelFormat.h:61
EVertexElementType
Definition RHIDefinitions.h:496
@ VET_Short4
Definition RHIDefinitions.h:507
@ VET_Half4
Definition RHIDefinitions.h:510
@ VET_UShort2N
Definition RHIDefinitions.h:514
@ VET_Short2
Definition RHIDefinitions.h:506
@ VET_Float1
Definition RHIDefinitions.h:498
@ VET_UByte4N
Definition RHIDefinitions.h:504
@ VET_Color
Definition RHIDefinitions.h:505
@ VET_Float3
Definition RHIDefinitions.h:500
@ VET_UShort4N
Definition RHIDefinitions.h:515
@ VET_Half2
Definition RHIDefinitions.h:509
@ VET_UShort2
Definition RHIDefinitions.h:512
@ VET_Float4
Definition RHIDefinitions.h:501
@ VET_UByte4
Definition RHIDefinitions.h:503
@ VET_PackedNormal
Definition RHIDefinitions.h:502
@ VET_Short2N
Definition RHIDefinitions.h:508
@ VET_UInt
Definition RHIDefinitions.h:517
@ VET_Short4N
Definition RHIDefinitions.h:511
@ VET_UShort4
Definition RHIDefinitions.h:513
@ VET_URGB10A2N
Definition RHIDefinitions.h:516
@ VET_Float2
Definition RHIDefinitions.h:499
EShaderFrequency
Definition RHIDefinitions.h:202
@ SF_Compute
Definition RHIDefinitions.h:208
@ SF_NumFrequencies
Definition RHIDefinitions.h:216
@ SF_Amplification
Definition RHIDefinitions.h:205
@ SF_Vertex
Definition RHIDefinitions.h:203
@ SF_Mesh
Definition RHIDefinitions.h:204
@ SF_Geometry
Definition RHIDefinitions.h:207
@ SF_RayGen
Definition RHIDefinitions.h:209
@ SF_RayCallable
Definition RHIDefinitions.h:212
@ SF_RayMiss
Definition RHIDefinitions.h:210
@ SF_RayHitGroup
Definition RHIDefinitions.h:211
@ SF_Pixel
Definition RHIDefinitions.h:206
@ VRSImage_Fractional
Definition RHIDefinitions.h:885
@ VRSImage_Palette
Definition RHIDefinitions.h:884
@ VRSImage_NotSupported
Definition RHIDefinitions.h:883
ERenderTargetStoreAction
Definition RHIDefinitions.h:1272
ERenderTargetLoadAction
Definition RHIDefinitions.h:1253
#define GRHIVariableRateShadingImageDataType
Definition RHIGlobals.h:891
ERHIFeatureLevel::Type GMaxRHIFeatureLevel
Definition RHI.cpp:1338
#define VK_TYPE_TO_STRING(Type, Value)
Definition VulkanConfiguration.h:244
VULKANRHI_API class FVulkanDynamicRHI * GVulkanRHI
Definition VulkanUtil.cpp:30
TAtomic< uint64 > GVulkanBufferHandleIdCounter
Definition VulkanRHI.cpp:50
bool ValidateShadingRateDataType()
Definition VulkanRHIPrivate.h:649
constexpr bool VKHasAnyFlags(VkFlags Flags, BitsType Contains)
Definition VulkanRHIPrivate.h:49
const int GMaxCrashBufferEntries
Definition VulkanRHIPrivate.h:677
TAtomic< uint64 > GVulkanBufferViewHandleIdCounter
Definition VulkanRHI.cpp:51
TAtomic< uint64 > GVulkanSamplerHandleIdCounter
Definition VulkanRHI.cpp:53
void VulkanSetImageLayout(FVulkanCommandBuffer *CmdBuffer, VkImage Image, VkImageLayout OldLayout, VkImageLayout NewLayout, const VkImageSubresourceRange &SubresourceRange)
Definition VulkanBarriers.cpp:1817
TAtomic< uint64 > GVulkanDSetLayoutHandleIdCounter
Definition VulkanRHI.cpp:54
VkFormat GVulkanSRGBFormat[PF_MAX]
Definition VulkanDevice.cpp:91
int32 GWaitForIdleOnSubmit
Definition VulkanQueue.cpp:13
VkShaderStageFlagBits UEFrequencyToVKStageBit(EShaderFrequency InStage)
Definition VulkanRHIPrivate.h:54
constexpr bool VKHasAllFlags(VkFlags Flags, BitsType Contains)
Definition VulkanRHIPrivate.h:43
VkFormat UEToVkTextureFormat(EPixelFormat UEFormat, const bool bIsSRGB)
Definition VulkanRHIPrivate.h:388
EShaderFrequency VkStageBitToUEFrequency(VkShaderStageFlagBits FlagBits)
Definition VulkanRHIPrivate.h:76
TAtomic< uint64 > GVulkanImageViewHandleIdCounter
Definition VulkanRHI.cpp:52
bool UseVulkanDescriptorCache()
Definition VulkanRHIPrivate.h:643
uint32_t uint32
Definition binka_ue_file_header.h:6
static CORE_API const FString & ProjectSavedDir()
Definition Paths.cpp:496
Definition VulkanCommandBuffer.h:43
Definition VulkanDescriptorSets.h:245
Definition VulkanDynamicRHI.h:69
Definition VulkanQueue.h:53
Definition VulkanRenderpass.h:453
Definition VulkanResources.h:108
Definition Atomic.h:538
Definition IConsoleManager.h:1792
@ ES3_1
Definition RHIFeatureLevel.h:25
@ NumRayTracingStages
Definition VulkanCommon.h:49
@ NumGraphicsStages
Definition VulkanCommon.h:42
@ NumComputeStages
Definition VulkanCommon.h:53
Definition VulkanCommandBuffer.h:22
void HeavyWeightBarrier(VkCommandBuffer CmdBuffer)
Definition VulkanRHIPrivate.h:587
void DebugHeavyWeightBarrier(VkCommandBuffer CmdBuffer, int32 CVarConditionMask)
Definition VulkanRHIPrivate.h:632
VkImageLayout GetMergedDepthStencilLayout(VkImageLayout DepthLayout, VkImageLayout StencilLayout)
Definition VulkanRHIPrivate.h:510
uint32 PlatformFormat
Definition PixelFormat.h:477
Definition VulkanRHIPrivate.h:123
uint32 Secondary
Definition VulkanRHIPrivate.h:134
uint32 Major
Definition VulkanRHIPrivate.h:132
uint32 Packed
Definition VulkanRHIPrivate.h:138
uint32 Tertiary
Definition VulkanRHIPrivate.h:135
uint32 Minor
Definition VulkanRHIPrivate.h:133