Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Source/Shared/UpscalerInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
59 changes: 59 additions & 0 deletions Source/Shared/UpscalerInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions Source/VK/CommandBufferVK.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions Source/VK/ConversionVK.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<VkDescriptorType, (size_t)DescriptorType::MAX_NUM> g_DescriptorTypes = {
VK_DESCRIPTOR_TYPE_SAMPLER, // SAMPLER
VK_DESCRIPTOR_TYPE_MUTABLE_EXT, // MUTABLE
Expand Down
4 changes: 3 additions & 1 deletion Source/VK/DescriptorVK.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down