Skip to content

Commit 3c5495e

Browse files
SuperResolution: fix DLSS resource transitions
1 parent 84820b4 commit 3c5495e

File tree

4 files changed

+38
-21
lines changed

4 files changed

+38
-21
lines changed

Graphics/SuperResolution/include/SuperResolutionDLSS.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
/// Shared DLSS utilities used by per-API DLSS backend implementations.
3131

3232
#include <vector>
33+
#include <array>
3334

3435
#include <nvsdk_ngx_defs.h>
3536
#include <nvsdk_ngx_params.h>
@@ -117,6 +118,29 @@ class SuperResolutionDLSS : public SuperResolutionBase
117118
return m_pDLSSFeature;
118119
}
119120

121+
void TransitionResourceStates(const ExecuteSuperResolutionAttribs& Attribs)
122+
{
123+
std::array<StateTransitionDesc, 7> Barriers;
124+
Uint32 BarrierCount = 0;
125+
126+
auto AddBarrier = [&](ITextureView* pView, RESOURCE_STATE NewState) {
127+
if (pView == nullptr)
128+
return;
129+
VERIFY_EXPR(BarrierCount < Barriers.size());
130+
Barriers[BarrierCount++] = StateTransitionDesc{pView->GetTexture(), RESOURCE_STATE_UNKNOWN, NewState, STATE_TRANSITION_FLAG_UPDATE_STATE};
131+
};
132+
133+
AddBarrier(Attribs.pColorTextureSRV, RESOURCE_STATE_SHADER_RESOURCE);
134+
AddBarrier(Attribs.pDepthTextureSRV, RESOURCE_STATE_SHADER_RESOURCE);
135+
AddBarrier(Attribs.pMotionVectorsSRV, RESOURCE_STATE_SHADER_RESOURCE);
136+
AddBarrier(Attribs.pOutputTextureView, RESOURCE_STATE_UNORDERED_ACCESS);
137+
AddBarrier(Attribs.pExposureTextureSRV, RESOURCE_STATE_SHADER_RESOURCE);
138+
AddBarrier(Attribs.pReactiveMaskTextureSRV, RESOURCE_STATE_SHADER_RESOURCE);
139+
AddBarrier(Attribs.pIgnoreHistoryMaskTextureSRV, RESOURCE_STATE_SHADER_RESOURCE);
140+
141+
Attribs.pContext->TransitionResourceStates(BarrierCount, Barriers.data());
142+
}
143+
120144
protected:
121145
NVSDK_NGX_Parameter* const m_pNGXParams;
122146

Graphics/SuperResolution/src/DLSSProviderD3D11.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ class SuperResolutionD3D11_DLSS final : public SuperResolutionDLSS<CreateDLSSFea
7373
if (pDLSSFeature == nullptr)
7474
return;
7575

76+
TransitionResourceStates(Attribs);
77+
7678
auto GetD3D11Resource = [](ITextureView* pView) -> ID3D11Resource* {
7779
return pView != nullptr ?
7880
ClassPtrCast<ITextureD3D11>(pView->GetTexture())->GetD3D11Texture() :
7981
nullptr;
8082
};
8183

82-
ID3D11DeviceContext* pd3d11Ctx = ClassPtrCast<IDeviceContextD3D11>(Attribs.pContext)->GetD3D11DeviceContext();
83-
8484
NVSDK_NGX_D3D11_DLSS_Eval_Params EvalParams{};
8585
EvalParams.Feature.pInColor = GetD3D11Resource(Attribs.pColorTextureSRV);
8686
EvalParams.Feature.pInOutput = GetD3D11Resource(Attribs.pOutputTextureView);
@@ -100,6 +100,8 @@ class SuperResolutionD3D11_DLSS final : public SuperResolutionDLSS<CreateDLSSFea
100100
EvalParams.InPreExposure = Attribs.PreExposure;
101101
EvalParams.InExposureScale = Attribs.ExposureScale;
102102

103+
ID3D11DeviceContext* pd3d11Ctx = ClassPtrCast<IDeviceContextD3D11>(Attribs.pContext)->GetD3D11DeviceContext();
104+
103105
NVSDK_NGX_Result Result = NGX_D3D11_EVALUATE_DLSS_EXT(pd3d11Ctx, pDLSSFeature, m_pNGXParams, &EvalParams);
104106
if (NVSDK_NGX_FAILED(Result))
105107
LOG_ERROR_MESSAGE("DLSS D3D11 evaluation failed. NGX Result: ", static_cast<Uint32>(Result));

Graphics/SuperResolution/src/DLSSProviderD3D12.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,27 +73,14 @@ class SuperResolutionD3D12_DLSS final : public SuperResolutionDLSS<CreateDLSSFea
7373
if (pDLSSFeature == nullptr)
7474
return;
7575

76-
IDeviceContextD3D12* pCtxImpl = ClassPtrCast<IDeviceContextD3D12>(Attribs.pContext);
76+
TransitionResourceStates(Attribs);
7777

7878
auto GetD3D12Resource = [](ITextureView* pView) -> ID3D12Resource* {
7979
return pView != nullptr ?
8080
ClassPtrCast<ITextureD3D12>(pView->GetTexture())->GetD3D12Texture() :
8181
nullptr;
8282
};
8383

84-
pCtxImpl->TransitionTextureState(Attribs.pColorTextureSRV->GetTexture(), D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
85-
pCtxImpl->TransitionTextureState(Attribs.pDepthTextureSRV->GetTexture(), D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
86-
pCtxImpl->TransitionTextureState(Attribs.pMotionVectorsSRV->GetTexture(), D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
87-
pCtxImpl->TransitionTextureState(Attribs.pOutputTextureView->GetTexture(), D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
88-
if (Attribs.pExposureTextureSRV)
89-
pCtxImpl->TransitionTextureState(Attribs.pExposureTextureSRV->GetTexture(), D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
90-
if (Attribs.pReactiveMaskTextureSRV)
91-
pCtxImpl->TransitionTextureState(Attribs.pReactiveMaskTextureSRV->GetTexture(), D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
92-
if (Attribs.pIgnoreHistoryMaskTextureSRV)
93-
pCtxImpl->TransitionTextureState(Attribs.pIgnoreHistoryMaskTextureSRV->GetTexture(), D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
94-
95-
ID3D12GraphicsCommandList* pCmdList = pCtxImpl->GetD3D12CommandList();
96-
9784
NVSDK_NGX_D3D12_DLSS_Eval_Params EvalParams{};
9885
EvalParams.Feature.pInColor = GetD3D12Resource(Attribs.pColorTextureSRV);
9986
EvalParams.Feature.pInOutput = GetD3D12Resource(Attribs.pOutputTextureView);
@@ -113,12 +100,15 @@ class SuperResolutionD3D12_DLSS final : public SuperResolutionDLSS<CreateDLSSFea
113100
EvalParams.InPreExposure = Attribs.PreExposure;
114101
EvalParams.InExposureScale = Attribs.ExposureScale;
115102

103+
IDeviceContextD3D12* pCtx = ClassPtrCast<IDeviceContextD3D12>(Attribs.pContext);
104+
ID3D12GraphicsCommandList* pCmdList = pCtx->GetD3D12CommandList();
105+
116106
NVSDK_NGX_Result Result = NGX_D3D12_EVALUATE_DLSS_EXT(pCmdList, pDLSSFeature, m_pNGXParams, &EvalParams);
117107
if (NVSDK_NGX_FAILED(Result))
118108
LOG_ERROR_MESSAGE("DLSS D3D12 evaluation failed. NGX Result: ", static_cast<Uint32>(Result));
119109

120-
pCtxImpl->TransitionTextureState(Attribs.pOutputTextureView->GetTexture(), D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE);
121-
pCtxImpl->Flush();
110+
pCtx->TransitionTextureState(Attribs.pOutputTextureView->GetTexture(), D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE);
111+
pCtx->Flush();
122112
}
123113
};
124114

Graphics/SuperResolution/src/DLSSProviderVk.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class SuperResolutionVk_DLSS final : public SuperResolutionDLSS<CreateDLSSFeatur
7575
if (pDLSSFeature == nullptr)
7676
return;
7777

78-
IDeviceContextVk* pCtxImpl = ClassPtrCast<IDeviceContextVk>(Attribs.pContext);
78+
TransitionResourceStates(Attribs);
7979

8080
auto CreateNGXResourceVK = [](ITextureView* pView, VkImageAspectFlags AspectMask, bool bReadWrite) -> NVSDK_NGX_Resource_VK {
8181
ITextureVk* pTexVk = ClassPtrCast<ITextureVk>(pView->GetTexture());
@@ -92,8 +92,6 @@ class SuperResolutionVk_DLSS final : public SuperResolutionDLSS<CreateDLSSFeatur
9292
return NVSDK_NGX_Create_ImageView_Resource_VK(pViewVk->GetVulkanImageView(), pTexVk->GetVkImage(), SubresourceRange, TexFormatToVkFormat(TexDesc.Format), TexDesc.Width, TexDesc.Height, bReadWrite);
9393
};
9494

95-
VkCommandBuffer vkCmdBuffer = pCtxImpl->GetVkCommandBuffer();
96-
9795
NVSDK_NGX_Resource_VK ColorResource = CreateNGXResourceVK(Attribs.pColorTextureSRV, VK_IMAGE_ASPECT_COLOR_BIT, false);
9896
NVSDK_NGX_Resource_VK OutputResource = CreateNGXResourceVK(Attribs.pOutputTextureView, VK_IMAGE_ASPECT_COLOR_BIT, true);
9997
NVSDK_NGX_Resource_VK DepthResource = CreateNGXResourceVK(Attribs.pDepthTextureSRV, VK_IMAGE_ASPECT_DEPTH_BIT, false);
@@ -130,6 +128,9 @@ class SuperResolutionVk_DLSS final : public SuperResolutionDLSS<CreateDLSSFeatur
130128
EvalParams.InPreExposure = Attribs.PreExposure;
131129
EvalParams.InExposureScale = Attribs.ExposureScale;
132130

131+
IDeviceContextVk* pCtxImpl = ClassPtrCast<IDeviceContextVk>(Attribs.pContext);
132+
VkCommandBuffer vkCmdBuffer = pCtxImpl->GetVkCommandBuffer();
133+
133134
NVSDK_NGX_Result Result = NGX_VULKAN_EVALUATE_DLSS_EXT(vkCmdBuffer, pDLSSFeature, m_pNGXParams, &EvalParams);
134135
if (NVSDK_NGX_FAILED(Result))
135136
LOG_ERROR_MESSAGE("DLSS Vulkan evaluation failed. NGX Result: ", static_cast<Uint32>(Result));

0 commit comments

Comments
 (0)