From 0ccb10708a572a28ccb2d9505fd2ced4b39a42c6 Mon Sep 17 00:00:00 2001 From: Jelle de Haan Date: Thu, 15 Oct 2026 11:46:49 +0200 Subject: [PATCH] VK: sample depth-stencil textures in the read-only attachment layout A depth-stencil texture in "SHADER_RESOURCE" was in "VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL" and its "TEXTURE" views expected that layout, so it could not be sampled while bound as a read-only depth-stencil attachment ("DEPTH_STENCIL_READONLY", e.g. soft particles): the descriptor layout never matched (VUID-vkCmdDraw-imageLayout-00344). Shader reads of depth-stencil textures now use "VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL" for both the barrier layout and the descriptor, the Vulkan counterpart of #266 ("DIRECT_QUEUE_GENERIC_READ" on D3D12). SDK-backed upscalers (FSR, DLSR, DLRR) sample their inputs in "VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL", so CmdDispatchUpscale moves a depth-stencil depth guide into that layout around the SDK call and back. --- Source/Shared/UpscalerInterface.h | 6 +++ Source/Shared/UpscalerInterface.hpp | 59 +++++++++++++++++++++++++++++ Source/VK/CommandBufferVK.hpp | 4 +- Source/VK/ConversionVK.h | 10 +++++ Source/VK/DescriptorVK.hpp | 4 +- 5 files changed, 80 insertions(+), 3 deletions(-) diff --git a/Source/Shared/UpscalerInterface.h b/Source/Shared/UpscalerInterface.h index 55e2a661..273801e1 100644 --- a/Source/Shared/UpscalerInterface.h +++ b/Source/Shared/UpscalerInterface.h @@ -42,10 +42,16 @@ struct UpscalerImpl final : public DebugNameBase { void CmdDispatchUpscale(CommandBuffer& commandBuffer, const DispatchUpscaleDesc& dispatchUpscaleDesc); private: + void DispatchUpscaleSdk(CommandBuffer& commandBuffer, const DispatchUpscaleDesc& dispatchUpscaleDesc); + Device& m_Device; const CoreInterface& m_iCore; UpscalerDesc m_Desc = {}; +#if NRI_ENABLE_VK_SUPPORT + void* m_VkCmdPipelineBarrier2 = nullptr; // "PFN_vkCmdPipelineBarrier2" for SDK-backed upscalers +#endif + #if (NRI_ENABLE_NIS_SDK || NRI_ENABLE_FFX_SDK || NRI_ENABLE_XESS_SDK || NRI_ENABLE_NGX_SDK || NRI_ENABLE_METAL_SUPPORT) union { Nis* nis; diff --git a/Source/Shared/UpscalerInterface.hpp b/Source/Shared/UpscalerInterface.hpp index d06944d6..0613b9b5 100644 --- a/Source/Shared/UpscalerInterface.hpp +++ b/Source/Shared/UpscalerInterface.hpp @@ -838,6 +838,18 @@ Result UpscalerImpl::Create(const UpscalerDesc& upscalerDesc) { if (!IsUpscalerSupported(deviceDesc, upscalerDesc.type)) return Result::UNSUPPORTED; +#if NRI_ENABLE_VK_SUPPORT + if (deviceDesc.graphicsAPI == GraphicsAPI::VK && upscalerDesc.type != UpscalerType::NIS) { + WrapperVKInterface iWrapperVK = {}; + if (nriGetInterface(m_Device, NRI_INTERFACE(WrapperVKInterface), &iWrapperVK) != Result::SUCCESS) + return Result::UNSUPPORTED; + + VkDevice vkDevice = (VkDevice)m_iCore.GetDeviceNativeObject(&m_Device); + PFN_vkGetDeviceProcAddr vkGetDeviceProcAddr = (PFN_vkGetDeviceProcAddr)iWrapperVK.GetDeviceProcAddrVK(m_Device); + m_VkCmdPipelineBarrier2 = (void*)vkGetDeviceProcAddr(vkDevice, "vkCmdPipelineBarrier2"); + } +#endif + UpscalerProps upscalerProps = {}; GetUpscalerProps(upscalerProps); @@ -1520,7 +1532,54 @@ void UpscalerImpl::GetUpscalerProps(UpscalerProps& upscalerProps) const { upscalerProps.renderResolutionMin = upscalerProps.renderResolution; // no DRS support because of DLSS } +#if NRI_ENABLE_VK_SUPPORT +// SDK-backed upscalers sample their inputs in "VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL", but a depth-stencil texture in +// "SHADER_RESOURCE" is in "VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL" (see "GetImageLayout"), so move it around the SDK call +static void TransitionDepthForUpscalerSdk(void* cmdPipelineBarrier2, const CoreInterface& NRI, CommandBuffer& commandBuffer, const UpscalerResource& depth, bool toSdk) { + if (!cmdPipelineBarrier2 || !depth.texture) + return; + + const TextureDesc& textureDesc = NRI.GetTextureDesc(*depth.texture); + const FormatProps& formatProps = GetFormatProps(textureDesc.format); + if (!formatProps.isDepth && !formatProps.isStencil) + return; + + VkImageMemoryBarrier2 barrier = {VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2}; + barrier.srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT; + barrier.dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT; + barrier.dstAccessMask = VK_ACCESS_2_MEMORY_READ_BIT; + barrier.oldLayout = toSdk ? VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL : VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; + barrier.newLayout = toSdk ? VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL : VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL; + barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; + barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; + barrier.image = (VkImage)NRI.GetTextureNativeObject(depth.texture); + barrier.subresourceRange.aspectMask = (formatProps.isDepth ? VK_IMAGE_ASPECT_DEPTH_BIT : 0) | (formatProps.isStencil ? VK_IMAGE_ASPECT_STENCIL_BIT : 0); + barrier.subresourceRange.levelCount = VK_REMAINING_MIP_LEVELS; + barrier.subresourceRange.layerCount = VK_REMAINING_ARRAY_LAYERS; + + VkDependencyInfo dependencyInfo = {VK_STRUCTURE_TYPE_DEPENDENCY_INFO}; + dependencyInfo.imageMemoryBarrierCount = 1; + dependencyInfo.pImageMemoryBarriers = &barrier; + + VkCommandBuffer vkCommandBuffer = (VkCommandBuffer)NRI.GetCommandBufferNativeObject(&commandBuffer); + ((PFN_vkCmdPipelineBarrier2)cmdPipelineBarrier2)(vkCommandBuffer, &dependencyInfo); +} +#endif + void UpscalerImpl::CmdDispatchUpscale(CommandBuffer& commandBuffer, const DispatchUpscaleDesc& dispatchUpscaleDesc) { +#if NRI_ENABLE_VK_SUPPORT + const bool usesDenoiserGuides = m_Desc.type == UpscalerType::DLRR || m_Desc.type == UpscalerType::METALFX_DENOISED; + const UpscalerResource& depth = usesDenoiserGuides ? dispatchUpscaleDesc.guides.denoiser.depth : dispatchUpscaleDesc.guides.upscaler.depth; + + TransitionDepthForUpscalerSdk(m_VkCmdPipelineBarrier2, m_iCore, commandBuffer, depth, true); + DispatchUpscaleSdk(commandBuffer, dispatchUpscaleDesc); + TransitionDepthForUpscalerSdk(m_VkCmdPipelineBarrier2, m_iCore, commandBuffer, depth, false); +#else + DispatchUpscaleSdk(commandBuffer, dispatchUpscaleDesc); +#endif +} + +void UpscalerImpl::DispatchUpscaleSdk(CommandBuffer& commandBuffer, const DispatchUpscaleDesc& dispatchUpscaleDesc) { const UpscalerResource& output = dispatchUpscaleDesc.output; const UpscalerResource& input = dispatchUpscaleDesc.input; diff --git a/Source/VK/CommandBufferVK.hpp b/Source/VK/CommandBufferVK.hpp index f59794d1..fbcad8ea 100644 --- a/Source/VK/CommandBufferVK.hpp +++ b/Source/VK/CommandBufferVK.hpp @@ -2566,8 +2566,8 @@ NRI_INLINE void CommandBufferVK::Barrier(const BarrierDesc& barrierDesc) { out.srcAccessMask = GetAccessFlags(in.before.access); out.dstStageMask = GetPipelineStageFlags(in.after.stages); out.dstAccessMask = GetAccessFlags(in.after.access); - out.oldLayout = GetImageLayout(in.before.layout); - out.newLayout = GetImageLayout(in.after.layout); + out.oldLayout = GetImageLayout(in.before.layout, textureVK.GetDesc().format); + out.newLayout = GetImageLayout(in.after.layout, textureVK.GetDesc().format); out.srcQueueFamilyIndex = in.srcQueue ? srcQueue->GetFamilyIndex() : VK_QUEUE_FAMILY_IGNORED; out.dstQueueFamilyIndex = in.dstQueue ? dstQueue->GetFamilyIndex() : VK_QUEUE_FAMILY_IGNORED; out.image = textureVK.GetHandle(); diff --git a/Source/VK/ConversionVK.h b/Source/VK/ConversionVK.h index a443b3ab..13734e43 100644 --- a/Source/VK/ConversionVK.h +++ b/Source/VK/ConversionVK.h @@ -89,6 +89,16 @@ constexpr VkImageLayout GetImageLayout(Layout layout) { return g_ImageLayouts[(size_t)layout]; } +// Shader reads of depth-stencil textures use the read-only attachment layout, so a texture in "SHADER_RESOURCE" and one in +// "DEPTH_STENCIL_READONLY" (D3D12: "DIRECT_QUEUE_GENERIC_READ" if accessed as "SHADER_RESOURCE") share one descriptor layout +inline VkImageLayout GetImageLayout(Layout layout, Format format) { + const FormatProps& formatProps = GetFormatProps(format); + if (layout == Layout::SHADER_RESOURCE && (formatProps.isDepth || formatProps.isStencil)) + return VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL; + + return GetImageLayout(layout); +} + constexpr std::array g_DescriptorTypes = { VK_DESCRIPTOR_TYPE_SAMPLER, // SAMPLER VK_DESCRIPTOR_TYPE_MUTABLE_EXT, // MUTABLE diff --git a/Source/VK/DescriptorVK.hpp b/Source/VK/DescriptorVK.hpp index 0d8234bd..42f5ffa1 100644 --- a/Source/VK/DescriptorVK.hpp +++ b/Source/VK/DescriptorVK.hpp @@ -157,7 +157,9 @@ Result DescriptorVK::Create(const TextureViewDesc& textureViewDesc) { // Handle mixed R/RW depth-stencil specific layouts VkImageLayout expectedLayout = GetImageViewLayout(textureViewDesc.type); - if (isAspectSensitiveAttachment && (formatProps.isDepth || formatProps.isStencil)) { + if (GetImageViewDescriptorType(textureViewDesc.type) == DescriptorType::TEXTURE) + expectedLayout = GetImageLayout(Layout::SHADER_RESOURCE, textureDesc.format); + else if (isAspectSensitiveAttachment && (formatProps.isDepth || formatProps.isStencil)) { bool isDepthReadonly = textureViewDesc.planes != PlaneBits::ALL && (textureViewDesc.planes & PlaneBits::DEPTH) == 0; bool isStencilReadonly = textureViewDesc.planes != PlaneBits::ALL && (textureViewDesc.planes & PlaneBits::STENCIL) == 0;