Skip to content

Commit 43fed25

Browse files
D3D12: Implement ISuperResolution using DirectSR
1 parent 6559b6c commit 43fed25

15 files changed

Lines changed: 1238 additions & 40 deletions

Graphics/GraphicsEngine/interface/GraphicsTypes.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3335,10 +3335,28 @@ DILIGENT_TYPED_ENUM(SUPER_RESOLUTION_TEMPORAL_CAP_FLAGS, Uint32)
33353335
DEFINE_FLAG_ENUM_OPERATORS(SUPER_RESOLUTION_TEMPORAL_CAP_FLAGS)
33363336

33373337

3338+
/// Super resolution creation flags.
3339+
DILIGENT_TYPED_ENUM(SUPER_RESOLUTION_CREATE_FLAGS, Uint32)
3340+
{
3341+
SUPER_RESOLUTION_CREATE_FLAG_NONE = 0u,
3342+
3343+
/// When set, the upscaler automatically calculates exposure for each frame.
3344+
/// The exposure texture in ExecuteSuperResolutionAttribs is ignored.
3345+
SUPER_RESOLUTION_CREATE_FLAG_AUTO_EXPOSURE = 1u << 0,
3346+
3347+
/// When set, enables the sharpening pass in the upscaler.
3348+
/// The Sharpness field in ExecuteSuperResolutionAttribs controls the amount.
3349+
SUPER_RESOLUTION_CREATE_FLAG_ENABLE_SHARPENING = 1u << 1,
3350+
3351+
SUPER_RESOLUTION_CREATE_FLAG_LAST = SUPER_RESOLUTION_CREATE_FLAG_ENABLE_SHARPENING
3352+
};
3353+
DEFINE_FLAG_ENUM_OPERATORS(SUPER_RESOLUTION_CREATE_FLAGS)
3354+
3355+
33383356
/// Information about a supported super resolution upscaler variant
33393357
struct SuperResolutionInfo
33403358
{
3341-
/// Human-readable name of the upscaler variant (e.g. "MetalFX Spatial", "MetalFX Temporal").
3359+
/// Human-readable name of the upscaler variant (e.g. "DLSS", "FSR", "MetalFX Spatial", "MetalFX Temporal").
33423360
Char Name[128] DEFAULT_INITIALIZER({});
33433361

33443362
/// Unique identifier for this upscaler variant.

Graphics/GraphicsEngine/interface/SuperResolution.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,14 @@ struct SuperResolutionDesc DILIGENT_DERIVE(DeviceObjectAttribs)
8989
/// Unlike the reactive mask which provides proportional control, this is a binary decision.
9090
TEXTURE_FORMAT IgnoreHistoryMaskFormat DEFAULT_INITIALIZER(TEX_FORMAT_UNKNOWN);
9191

92-
/// When True, the upscaler automatically calculates exposure for each frame.
93-
/// When auto exposure is enabled, the exposure texture in
94-
/// ExecuteSuperResolutionAttribs is ignored.
95-
Bool AutoExposureEnabled DEFAULT_INITIALIZER(True);
92+
/// Exposure scale texture format.
93+
/// Optional. When auto-exposure is disabled, specifies the format of the 1x1 exposure
94+
/// texture provided in ExecuteSuperResolutionAttribs::pExposureTextureSRV.
95+
TEXTURE_FORMAT ExposureFormat DEFAULT_INITIALIZER(TEX_FORMAT_UNKNOWN);
96+
97+
/// Engine creation flags controlling the super resolution upscaler behavior.
98+
/// See SUPER_RESOLUTION_CREATE_FLAGS.
99+
SUPER_RESOLUTION_CREATE_FLAGS Flags DEFAULT_INITIALIZER(SUPER_RESOLUTION_CREATE_FLAG_NONE);
96100
};
97101
typedef struct SuperResolutionDesc SuperResolutionDesc;
98102

@@ -112,6 +116,16 @@ struct SuperResolutionSourceSettingsAttribs
112116
/// Target (output) texture height. Must be greater than zero.
113117
Uint32 OutputHeight DEFAULT_INITIALIZER(0);
114118

119+
/// Output texture format.
120+
/// Some backends (e.g. DirectSR) may return different optimal input resolutions
121+
/// depending on the output format. When set to TEX_FORMAT_UNKNOWN, the backend will use a reasonable default.
122+
TEXTURE_FORMAT OutputFormat DEFAULT_INITIALIZER(TEX_FORMAT_UNKNOWN);
123+
124+
/// Engine creation flags controlling the super resolution upscaler behavior.
125+
/// These flags affect the optimal source resolution returned by the backend.
126+
/// Must match the flags that will be used when creating the upscaler.
127+
SUPER_RESOLUTION_CREATE_FLAGS Flags DEFAULT_INITIALIZER(SUPER_RESOLUTION_CREATE_FLAG_NONE);
128+
115129
/// Optimization type controlling the quality/performance trade-off.
116130
SUPER_RESOLUTION_OPTIMIZATION_TYPE OptimizationType DEFAULT_INITIALIZER(SUPER_RESOLUTION_OPTIMIZATION_TYPE_BALANCED);
117131
};
@@ -155,7 +169,7 @@ struct ExecuteSuperResolutionAttribs
155169
/// Exposure texture (shader resource view).
156170
/// Optional. A 1x1 R16_FLOAT texture containing the exposure value.
157171
/// The upscaler reads the R channel and uses it to multiply the input color.
158-
/// Ignored when SuperResolutionDesc::AutoExposureEnabled is True.
172+
/// Ignored when SuperResolutionDesc::Flags includes SUPER_RESOLUTION_CREATE_FLAG_AUTO_EXPOSURE.
159173
ITextureView* pExposureTextureSRV DEFAULT_INITIALIZER(nullptr);
160174

161175
/// Reactive mask texture (shader resource view).

Graphics/GraphicsEngineD3D12/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ set(INCLUDE
4444
include/ShaderResourceCacheD3D12.hpp
4545
include/ShaderResourcesD3D12.hpp
4646
include/ShaderVariableManagerD3D12.hpp
47+
include/SuperResolutionD3D12Impl.hpp
4748
include/SwapChainD3D12Impl.hpp
4849
include/TextureD3D12Impl.hpp
4950
include/TextureViewD3D12Impl.hpp
@@ -108,6 +109,7 @@ set(SRC
108109
src/ShaderResourceCacheD3D12.cpp
109110
src/ShaderResourcesD3D12.cpp
110111
src/ShaderVariableManagerD3D12.cpp
112+
src/SuperResolutionD3D12Impl.cpp
111113
src/SwapChainD3D12Impl.cpp
112114
src/TextureD3D12Impl.cpp
113115
src/TextureViewD3D12Impl.cpp

Graphics/GraphicsEngineD3D12/include/D3D12Loader.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2026 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -29,6 +29,7 @@
2929

3030
#include <d3d12.h>
3131

32+
3233
namespace Diligent
3334
{
3435

@@ -48,9 +49,15 @@ using D3D12SerializeRootSignatureProcType = HRESULT(WINAPI*)(
4849
_Out_ ID3DBlob** ppBlob,
4950
_Always_(_Outptr_opt_result_maybenull_) ID3DBlob** ppErrorBlob);
5051

52+
using D3D12GetInterfaceProcType = HRESULT(WINAPI*)(
53+
_In_ REFCLSID rclsid,
54+
_In_ REFIID riid,
55+
_COM_Outptr_opt_ void** ppvDebug);
56+
5157
extern D3D12CreateDeviceProcType D3D12CreateDevice;
5258
extern D3D12GetDebugInterfaceProcType D3D12GetDebugInterface;
5359
extern D3D12SerializeRootSignatureProcType D3D12SerializeRootSignature;
60+
extern D3D12GetInterfaceProcType D3D12GetInterface;
5461

5562
HMODULE LoadD3D12Dll(const char* DLLPath = "d3d12.dll");
5663

Graphics/GraphicsEngineD3D12/include/D3D12TypeConversions.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ D3D12_COMMAND_LIST_TYPE QueueIdToD3D12CommandListType(HardwareQueueIndex Qu
114114
COMMAND_QUEUE_TYPE D3D12CommandListTypeToCmdQueueType(D3D12_COMMAND_LIST_TYPE ListType);
115115
D3D12_COMMAND_QUEUE_PRIORITY QueuePriorityToD3D12QueuePriority(QUEUE_PRIORITY Priority);
116116

117+
DSR_SUPERRES_CREATE_ENGINE_FLAGS SuperResolutionCreateFlagsToDSRFlags(SUPER_RESOLUTION_CREATE_FLAGS Flags);
118+
117119
static constexpr HardwareQueueIndex D3D12HWQueueIndex_Graphics{Uint8{0}};
118120
static constexpr HardwareQueueIndex D3D12HWQueueIndex_Compute{Uint8{1}};
119121
static constexpr HardwareQueueIndex D3D12HWQueueIndex_Copy{Uint8{2}};

Graphics/GraphicsEngineD3D12/include/RenderDeviceD3D12Impl.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
#include "DXCompiler.hpp"
4848
#include "RootSignature.hpp"
4949

50+
#include <directsr.h>
51+
5052

5153
// The macros below are only defined in Win SDK 19041+ and are missing in 17763
5254
#ifndef D3D12_RAYTRACING_MAX_RAY_GENERATION_SHADER_THREADS
@@ -223,6 +225,8 @@ class RenderDeviceD3D12Impl final : public RenderDeviceNextGenBase<RenderDeviceD
223225

224226
RootSignatureCacheD3D12& GetRootSignatureCache() { return m_RootSignatureCache; }
225227

228+
IDSRDevice* GetDSRDevice() const { return m_pDSRDevice; }
229+
226230
DescriptorHeapAllocation AllocateDescriptors(D3D12_DESCRIPTOR_HEAP_TYPE Type, UINT Count = 1);
227231
DescriptorHeapAllocation AllocateGPUDescriptors(D3D12_DESCRIPTOR_HEAP_TYPE Type, UINT Count = 1);
228232

@@ -345,6 +349,8 @@ class RenderDeviceD3D12Impl final : public RenderDeviceNextGenBase<RenderDeviceD
345349

346350
bool m_IsPSOCacheSupported = false;
347351

352+
CComPtr<IDSRDevice> m_pDSRDevice;
353+
348354
#ifdef DILIGENT_DEVELOPMENT
349355
Uint32 m_MaxD3D12DeviceVersion = 0;
350356
#endif
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2026 Diligent Graphics LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* In no event and under no legal theory, whether in tort (including negligence),
17+
* contract, or otherwise, unless required by applicable law (such as deliberate
18+
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
19+
* liable for any damages, including any direct, indirect, special, incidental,
20+
* or consequential damages of any character arising as a result of this License or
21+
* out of the use or inability to use the software (including but not limited to damages
22+
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
23+
* all other commercial damages or losses), even if such Contributor has been advised
24+
* of the possibility of such damages.
25+
*/
26+
27+
#pragma once
28+
29+
/// \file
30+
/// Declaration of Diligent::SuperResolutionD3D12Impl class
31+
32+
#include "EngineD3D12ImplTraits.hpp"
33+
#include "DeviceObjectBase.hpp"
34+
#include "SuperResolution.h"
35+
36+
#include <vector>
37+
38+
39+
namespace Diligent
40+
{
41+
42+
class DeviceContextD3D12Impl;
43+
44+
/// Implementation of the ISuperResolution interface for Direct3D12 backend using DirectSR.
45+
class SuperResolutionD3D12Impl final : public DeviceObjectBase<ISuperResolution, RenderDeviceD3D12Impl, SuperResolutionDesc>
46+
{
47+
public:
48+
using TSRUpscalerBase = DeviceObjectBase<ISuperResolution, RenderDeviceD3D12Impl, SuperResolutionDesc>;
49+
50+
SuperResolutionD3D12Impl(IReferenceCounters* pRefCounters,
51+
RenderDeviceD3D12Impl* pDevice,
52+
const SuperResolutionDesc& Desc);
53+
54+
~SuperResolutionD3D12Impl();
55+
56+
IMPLEMENT_QUERY_INTERFACE_IN_PLACE(IID_SuperResolution, TSRUpscalerBase)
57+
58+
/// Implementation of ISuperResolution::GetOptimalJitterPattern().
59+
virtual void DILIGENT_CALL_TYPE GetOptimalJitterPattern(Uint32 Index, float* pJitterX, float* pJitterY) const override final;
60+
61+
/// Encodes the upscaling operation.
62+
void Execute(DeviceContextD3D12Impl& Ctx, const ExecuteSuperResolutionAttribs& Attribs);
63+
64+
private:
65+
CComPtr<IDSRSuperResEngine> m_pDSREngine;
66+
CComPtr<IDSRSuperResUpscaler> m_pDSRUpscaler;
67+
std::vector<DSR_FLOAT2> m_JitterPattern;
68+
};
69+
70+
} // namespace Diligent

0 commit comments

Comments
 (0)