mirror of
https://github.com/optiscaler/OptiScaler.git
synced 2026-11-22 13:47:11 +00:00
Crop DLSS subrects on the Dx11 side of the Dx12 bridge and copy back only the view
This commit is contained in:
1 parent
8055f35a6e
commit
07b21c75de
15 files changed
+444
-95
No files matched your search
@@ -374,6 +374,8 @@ copy NUL "$(SolutionDir)x64\Release\a\!! EXTRACT ALL FILES TO GAME FOLDER !!" /Y
|
||||
<ClInclude Include="shaders\resource_copy\precompile\rc_Shader_Vk.h" />
|
||||
<ClInclude Include="shaders\resource_copy\precompile\rc_offset_Shader.h" />
|
||||
<ClInclude Include="shaders\resource_copy\precompile\rc_offset_Shader_Dx11.h" />
|
||||
<ClInclude Include="shaders\resource_copy\RC_Common.h" />
|
||||
<ClInclude Include="shaders\resource_copy\RC_Dx11.h" />
|
||||
<ClInclude Include="shaders\resource_copy\RC_Dx12.h" />
|
||||
<ClInclude Include="shaders\resource_copy\RC_Vk.h" />
|
||||
<ClInclude Include="shaders\velocity_convert\precompile\vc_Shader_Vk.h" />
|
||||
@@ -678,6 +680,7 @@ copy NUL "$(SolutionDir)x64\Release\a\!! EXTRACT ALL FILES TO GAME FOLDER !!" /Y
|
||||
<ClCompile Include="shaders\reproject\Reproject_Dx12.cpp" />
|
||||
<ClCompile Include="shaders\Shader_Common.cpp" />
|
||||
<ClCompile Include="shaders\render_ui\RUI_Dx12.cpp" />
|
||||
<ClCompile Include="shaders\resource_copy\RC_Dx11.cpp" />
|
||||
<ClCompile Include="shaders\resource_copy\RC_Dx12.cpp" />
|
||||
<ClCompile Include="shaders\resource_copy\RC_Vk.cpp" />
|
||||
<ClCompile Include="shaders\velocity_convert\VC_Vk.cpp" />
|
||||
|
||||
@@ -725,6 +725,12 @@
|
||||
<ClInclude Include="shaders\depth_transfer\DT_Vk.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="shaders\resource_copy\RC_Common.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="shaders\resource_copy\RC_Dx11.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="shaders\resource_copy\RC_Dx12.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
@@ -1643,6 +1649,9 @@
|
||||
<ClCompile Include="shaders\depth_transfer\DT_Vk.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="shaders\resource_copy\RC_Dx11.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="shaders\resource_copy\RC_Dx12.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
|
||||
#include <dxgiformat.h>
|
||||
|
||||
// DXGI formats for copying textures with the resource copy shaders
|
||||
class RC_Formats
|
||||
{
|
||||
public:
|
||||
// Typeless formats TranslateTypelessFormats doesn't cover, R16 is D16 depth or the half float FFX assumes
|
||||
static DXGI_FORMAT SrvFormat(DXGI_FORMAT format, bool depth)
|
||||
{
|
||||
switch (format)
|
||||
{
|
||||
case DXGI_FORMAT_R16_TYPELESS:
|
||||
return depth ? DXGI_FORMAT_R16_UNORM : DXGI_FORMAT_R16_FLOAT;
|
||||
|
||||
case DXGI_FORMAT_R8_TYPELESS:
|
||||
return DXGI_FORMAT_R8_UNORM;
|
||||
|
||||
case DXGI_FORMAT_B8G8R8X8_TYPELESS:
|
||||
return DXGI_FORMAT_B8G8R8X8_UNORM;
|
||||
|
||||
default:
|
||||
return DXGI_FORMAT_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
// The copy must be writable through a UAV and read back the same values
|
||||
static DXGI_FORMAT BufferFormat(DXGI_FORMAT format, bool depth)
|
||||
{
|
||||
// Depth formats can't be UAVs, the shader reads the depth plane as float
|
||||
if (depth)
|
||||
return DXGI_FORMAT_R32_FLOAT;
|
||||
|
||||
switch (format)
|
||||
{
|
||||
// sRGB can't be a UAV, the shader reads them decoded so keep the precision
|
||||
case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
|
||||
case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
|
||||
case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
|
||||
return DXGI_FORMAT_R16G16B16A16_FLOAT;
|
||||
|
||||
// BGRA UAV stores are optional
|
||||
case DXGI_FORMAT_B8G8R8A8_TYPELESS:
|
||||
case DXGI_FORMAT_B8G8R8A8_UNORM:
|
||||
case DXGI_FORMAT_B8G8R8X8_TYPELESS:
|
||||
case DXGI_FORMAT_B8G8R8X8_UNORM:
|
||||
return DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
|
||||
default:
|
||||
if (auto srvFormat = SrvFormat(format, depth); srvFormat != DXGI_FORMAT_UNKNOWN)
|
||||
return srvFormat;
|
||||
|
||||
return format;
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,154 @@
|
||||
#include "pch.h"
|
||||
#include "RC_Dx11.h"
|
||||
|
||||
#include "RC_Common.h"
|
||||
#include "precompile/rc_offset_Shader_Dx11.h"
|
||||
|
||||
bool RC_Dx11::CreateBufferResource(ID3D11Device* InDevice, ID3D11Resource* InSource, uint32_t InWidth,
|
||||
uint32_t InHeight)
|
||||
{
|
||||
D3D11_TEXTURE2D_DESC current {};
|
||||
|
||||
if (_buffer != nullptr)
|
||||
_buffer->GetDesc(¤t);
|
||||
|
||||
return CreateBufferResourceCommon(InDevice, InSource, _buffer,
|
||||
[&](D3D11_TEXTURE2D_DESC& desc)
|
||||
{
|
||||
desc.Format = RC_Formats::BufferFormat(desc.Format, _depth);
|
||||
desc.Width = InWidth;
|
||||
desc.Height = InHeight;
|
||||
|
||||
if (_buffer != nullptr && current.Format == desc.Format)
|
||||
{
|
||||
desc.Width = std::max(desc.Width, current.Width);
|
||||
desc.Height = std::max(desc.Height, current.Height);
|
||||
}
|
||||
|
||||
desc.MipLevels = 1;
|
||||
desc.ArraySize = 1;
|
||||
desc.SampleDesc = { 1, 0 };
|
||||
desc.Usage = D3D11_USAGE_DEFAULT;
|
||||
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS;
|
||||
desc.CPUAccessFlags = 0;
|
||||
desc.MiscFlags = 0;
|
||||
});
|
||||
}
|
||||
|
||||
bool RC_Dx11::InitializeViews(ID3D11Texture2D* InResource, ID3D11Texture2D* OutResource)
|
||||
{
|
||||
if (InResource != _currentInResource || _srvInput == nullptr)
|
||||
{
|
||||
SAFE_RELEASE(_srvInput);
|
||||
|
||||
D3D11_TEXTURE2D_DESC desc;
|
||||
InResource->GetDesc(&desc);
|
||||
|
||||
auto format = RC_Formats::SrvFormat(desc.Format, _depth);
|
||||
|
||||
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
|
||||
srvDesc.Format = format != DXGI_FORMAT_UNKNOWN ? format : TranslateTypelessFormats(desc.Format);
|
||||
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
|
||||
srvDesc.Texture2D.MipLevels = 1;
|
||||
|
||||
auto hr = _device->CreateShaderResourceView(InResource, &srvDesc, &_srvInput);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
LOG_ERROR("[{0}] CreateShaderResourceView error {1:x}", _name, hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
_currentInResource = InResource;
|
||||
}
|
||||
|
||||
return InitializeUAV(OutResource, _currentOutResource, _uavOutput);
|
||||
}
|
||||
|
||||
bool RC_Dx11::Dispatch(ID3D11DeviceContext* InContext, ID3D11Texture2D* InResource, ID3D11Texture2D* OutResource,
|
||||
uint32_t InSrcX, uint32_t InSrcY, uint32_t InDstX, uint32_t InDstY, uint32_t InWidth,
|
||||
uint32_t InHeight)
|
||||
{
|
||||
if (!_init || InContext == nullptr || InResource == nullptr || OutResource == nullptr)
|
||||
return false;
|
||||
|
||||
LOG_DEBUG("[{0}] Start!", _name);
|
||||
|
||||
if (!InitializeViews(InResource, OutResource))
|
||||
return false;
|
||||
|
||||
InternalConstants constants {};
|
||||
constants.SrcOffset[0] = InSrcX;
|
||||
constants.SrcOffset[1] = InSrcY;
|
||||
constants.DstOffset[0] = InDstX;
|
||||
constants.DstOffset[1] = InDstY;
|
||||
constants.Size[0] = InWidth;
|
||||
constants.Size[1] = InHeight;
|
||||
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
auto hr = InContext->Map(_constantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
if (hr == DXGI_ERROR_DEVICE_REMOVED && _device != nullptr)
|
||||
Util::GetDeviceRemovedReason(_device);
|
||||
|
||||
LOG_ERROR("[{0}] Map error {1:x}", _name, hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(mappedResource.pData, &constants, sizeof(constants));
|
||||
InContext->Unmap(_constantBuffer, 0);
|
||||
|
||||
InContext->CSSetShader(_computeShader, nullptr, 0);
|
||||
InContext->CSSetConstantBuffers(0, 1, &_constantBuffer);
|
||||
InContext->CSSetShaderResources(0, 1, &_srvInput);
|
||||
InContext->CSSetUnorderedAccessViews(0, 1, &_uavOutput, nullptr);
|
||||
|
||||
InContext->Dispatch((InWidth + InNumThreadsX - 1) / InNumThreadsX, (InHeight + InNumThreadsY - 1) / InNumThreadsY,
|
||||
1);
|
||||
|
||||
// Unbind resources
|
||||
ID3D11UnorderedAccessView* nullUAV = nullptr;
|
||||
InContext->CSSetUnorderedAccessViews(0, 1, &nullUAV, nullptr);
|
||||
ID3D11ShaderResourceView* nullSRV = nullptr;
|
||||
InContext->CSSetShaderResources(0, 1, &nullSRV);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
RC_Dx11::RC_Dx11(std::string InName, ID3D11Device* InDevice, bool InDepth)
|
||||
: Shader_Dx11(InName, InDevice), _depth(InDepth)
|
||||
{
|
||||
if (InDevice == nullptr)
|
||||
{
|
||||
LOG_ERROR("InDevice is nullptr!");
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_DEBUG("{0} start!", _name);
|
||||
|
||||
_device = InDevice;
|
||||
|
||||
auto result = CreateComputeShader(InDevice, _computeShader, reinterpret_cast<const void*>(rc_offset_cso),
|
||||
sizeof(rc_offset_cso), nullptr);
|
||||
|
||||
if (FAILED(result))
|
||||
{
|
||||
LOG_ERROR("[{0}] CreateComputeShader error: {1:X}", _name, result);
|
||||
return;
|
||||
}
|
||||
|
||||
D3D11_BUFFER_DESC cbDesc = {};
|
||||
cbDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
cbDesc.ByteWidth = sizeof(InternalConstants);
|
||||
cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
|
||||
result = InDevice->CreateBuffer(&cbDesc, nullptr, &_constantBuffer);
|
||||
if (result != S_OK)
|
||||
{
|
||||
LOG_ERROR("CreateBuffer error: {0:X}", (UINT) result);
|
||||
return;
|
||||
}
|
||||
|
||||
_init = true;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
#include "SysUtils.h"
|
||||
|
||||
#include <shaders/Shader_Dx11.h>
|
||||
#include <d3d11.h>
|
||||
|
||||
// Copies a rectangle between textures through an SRV and a UAV, used to crop and place DLSS subrects
|
||||
class RC_Dx11 : public Shader_Dx11
|
||||
{
|
||||
private:
|
||||
struct alignas(256) InternalConstants
|
||||
{
|
||||
uint32_t SrcOffset[2];
|
||||
uint32_t DstOffset[2];
|
||||
uint32_t Size[2];
|
||||
};
|
||||
|
||||
uint32_t InNumThreadsX = 16;
|
||||
uint32_t InNumThreadsY = 16;
|
||||
bool _depth = false;
|
||||
|
||||
bool InitializeViews(ID3D11Texture2D* InResource, ID3D11Texture2D* OutResource);
|
||||
|
||||
public:
|
||||
// Only grows, so dynamic resolution doesn't recreate it every frame
|
||||
bool CreateBufferResource(ID3D11Device* InDevice, ID3D11Resource* InSource, uint32_t InWidth, uint32_t InHeight);
|
||||
bool Dispatch(ID3D11DeviceContext* InContext, ID3D11Texture2D* InResource, ID3D11Texture2D* OutResource,
|
||||
uint32_t InSrcX, uint32_t InSrcY, uint32_t InDstX, uint32_t InDstY, uint32_t InWidth,
|
||||
uint32_t InHeight);
|
||||
|
||||
// InDepth: the source is a depth buffer, read as depth and copied to R32_FLOAT
|
||||
RC_Dx11(std::string InName, ID3D11Device* InDevice, bool InDepth = false);
|
||||
|
||||
// Shader_Dx11 releases it but InitializeUAV never took a reference
|
||||
~RC_Dx11() { _currentOutResource = nullptr; }
|
||||
};
|
||||
@@ -1,64 +1,16 @@
|
||||
#include "pch.h"
|
||||
#include "RC_Dx12.h"
|
||||
|
||||
#include "RC_Common.h"
|
||||
#include "precompile/rc_offset_Shader.h"
|
||||
|
||||
// Typeless formats TranslateTypelessFormats doesn't cover, R16 is D16 depth or the half float FFX assumes
|
||||
static DXGI_FORMAT SrvFormat(DXGI_FORMAT format, bool depth)
|
||||
{
|
||||
switch (format)
|
||||
{
|
||||
case DXGI_FORMAT_R16_TYPELESS:
|
||||
return depth ? DXGI_FORMAT_R16_UNORM : DXGI_FORMAT_R16_FLOAT;
|
||||
|
||||
case DXGI_FORMAT_R8_TYPELESS:
|
||||
return DXGI_FORMAT_R8_UNORM;
|
||||
|
||||
case DXGI_FORMAT_B8G8R8X8_TYPELESS:
|
||||
return DXGI_FORMAT_B8G8R8X8_UNORM;
|
||||
|
||||
default:
|
||||
return DXGI_FORMAT_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
// The copy must be writable through a UAV and read back the same values
|
||||
static DXGI_FORMAT BufferFormat(DXGI_FORMAT format, bool depth)
|
||||
{
|
||||
// Depth formats can't be UAVs, the shader reads the depth plane as float
|
||||
if (depth)
|
||||
return DXGI_FORMAT_R32_FLOAT;
|
||||
|
||||
switch (format)
|
||||
{
|
||||
// sRGB can't be a UAV, the shader reads them decoded so keep the precision
|
||||
case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
|
||||
case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
|
||||
case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
|
||||
return DXGI_FORMAT_R16G16B16A16_FLOAT;
|
||||
|
||||
// BGRA UAV stores are optional
|
||||
case DXGI_FORMAT_B8G8R8A8_TYPELESS:
|
||||
case DXGI_FORMAT_B8G8R8A8_UNORM:
|
||||
case DXGI_FORMAT_B8G8R8X8_TYPELESS:
|
||||
case DXGI_FORMAT_B8G8R8X8_UNORM:
|
||||
return DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
|
||||
default:
|
||||
if (auto srvFormat = SrvFormat(format, depth); srvFormat != DXGI_FORMAT_UNKNOWN)
|
||||
return srvFormat;
|
||||
|
||||
return format;
|
||||
}
|
||||
}
|
||||
|
||||
bool RC_Dx12::CreateBufferResource(ID3D12Device* InDevice, ID3D12Resource* InSource, uint32_t InWidth,
|
||||
uint32_t InHeight, D3D12_RESOURCE_STATES InState)
|
||||
{
|
||||
if (InSource == nullptr)
|
||||
return false;
|
||||
|
||||
auto format = BufferFormat(InSource->GetDesc().Format, _depth);
|
||||
auto format = RC_Formats::BufferFormat(InSource->GetDesc().Format, _depth);
|
||||
uint64_t width = InWidth;
|
||||
uint32_t height = InHeight;
|
||||
|
||||
@@ -105,7 +57,7 @@ bool RC_Dx12::Dispatch(ID3D12GraphicsCommandList* InCmdList, ID3D12Resource* InR
|
||||
FrameDescriptorHeap& currentHeap = _frameHeaps[_counter];
|
||||
|
||||
CreateShaderResourceView(_device, InResource, currentHeap.GetSrvCPU(0),
|
||||
SrvFormat(InResource->GetDesc().Format, _depth));
|
||||
RC_Formats::SrvFormat(InResource->GetDesc().Format, _depth));
|
||||
CreateUnorderedAccessView(_device, OutResource, currentHeap.GetUavCPU(0), 0);
|
||||
|
||||
InternalConstants constants {};
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
#include <Config.h>
|
||||
#include "IFeature.h"
|
||||
|
||||
#include <magic_enum.hpp>
|
||||
|
||||
void IFeature::SetHandle(unsigned int InHandleId)
|
||||
{
|
||||
_handle = new NVSDK_NGX_Handle { InHandleId };
|
||||
@@ -269,21 +271,12 @@ void IFeature::GetRenderResolution(const NVSDK_NGX_Parameter* InParameters, unsi
|
||||
|
||||
void IFeature::ReadSubrects(const NVSDK_NGX_Parameter* InParameters)
|
||||
{
|
||||
static constexpr std::array<std::pair<const char*, const char*>, (size_t) SubrectSlot::Count> baseKeys = { {
|
||||
{ NVSDK_NGX_Parameter_DLSS_Input_Color_Subrect_Base_X, NVSDK_NGX_Parameter_DLSS_Input_Color_Subrect_Base_Y },
|
||||
{ NVSDK_NGX_Parameter_DLSS_Input_Depth_Subrect_Base_X, NVSDK_NGX_Parameter_DLSS_Input_Depth_Subrect_Base_Y },
|
||||
{ NVSDK_NGX_Parameter_DLSS_Input_MV_SubrectBase_X, NVSDK_NGX_Parameter_DLSS_Input_MV_SubrectBase_Y },
|
||||
{ NVSDK_NGX_Parameter_DLSS_Input_Bias_Current_Color_SubrectBase_X,
|
||||
NVSDK_NGX_Parameter_DLSS_Input_Bias_Current_Color_SubrectBase_Y },
|
||||
{ NVSDK_NGX_Parameter_DLSS_Output_Subrect_Base_X, NVSDK_NGX_Parameter_DLSS_Output_Subrect_Base_Y },
|
||||
} };
|
||||
|
||||
for (size_t i = 0; i < baseKeys.size(); i++)
|
||||
for (size_t i = 0; i < SubrectBaseKeys.size(); i++)
|
||||
{
|
||||
auto& rect = _subrects[i];
|
||||
rect = { 0, 0, _renderWidth, _renderHeight };
|
||||
InParameters->Get(baseKeys[i].first, &rect.X);
|
||||
InParameters->Get(baseKeys[i].second, &rect.Y);
|
||||
rect.Given = InParameters->Get(SubrectBaseKeys[i].first, &rect.X) == NVSDK_NGX_Result_Success;
|
||||
rect.Given |= InParameters->Get(SubrectBaseKeys[i].second, &rect.Y) == NVSDK_NGX_Result_Success;
|
||||
}
|
||||
|
||||
// Display resolution motion vectors and the output keep the creation size, even with dynamic resolution
|
||||
@@ -298,7 +291,33 @@ void IFeature::ReadSubrects(const NVSDK_NGX_Parameter* InParameters)
|
||||
output.Height = DisplayHeight();
|
||||
|
||||
if (!_outputSubrects)
|
||||
output.X = output.Y = 0;
|
||||
output = { 0, 0, output.Width, output.Height };
|
||||
}
|
||||
|
||||
bool IFeature::SubrectFits(SubrectSlot InSlot, const Subrect& InRect, uint64_t InWidth, uint32_t InHeight)
|
||||
{
|
||||
const uint32_t slotBit = 1u << (uint32_t) InSlot;
|
||||
|
||||
if ((uint64_t) InRect.X + InRect.Width > InWidth || (uint64_t) InRect.Y + InRect.Height > InHeight)
|
||||
{
|
||||
if (!(_subrectWarned & slotBit))
|
||||
{
|
||||
LOG_WARN("{} subrect {}x{} at ({}, {}) is outside the {}x{} texture, using it as is",
|
||||
magic_enum::enum_name(InSlot), InRect.Width, InRect.Height, InRect.X, InRect.Y, InWidth, InHeight);
|
||||
_subrectWarned |= slotBit;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!(_subrectLogged & slotBit))
|
||||
{
|
||||
LOG_INFO("{} subrect {}x{} at ({}, {}) of the {}x{} texture, copying it for {}", magic_enum::enum_name(InSlot),
|
||||
InRect.Width, InRect.Height, InRect.X, InRect.Y, InWidth, InHeight, Name());
|
||||
_subrectLogged |= slotBit;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
float IFeature::GetSharpness(const NVSDK_NGX_Parameter* InParameters)
|
||||
|
||||
@@ -50,10 +50,27 @@ struct Subrect
|
||||
uint32_t Y = 0;
|
||||
uint32_t Width = 0;
|
||||
uint32_t Height = 0;
|
||||
bool Given = false; // The game passed the base, even (0, 0)
|
||||
|
||||
bool HasOffset() const { return X != 0 || Y != 0; }
|
||||
|
||||
// Only part of the texture belongs to this view, the rest can be other views
|
||||
bool IsPartOf(uint64_t InWidth, uint32_t InHeight) const
|
||||
{
|
||||
return HasOffset() || (Given && (Width < InWidth || Height < InHeight));
|
||||
}
|
||||
};
|
||||
|
||||
// Base X/Y parameter names, in SubrectSlot order
|
||||
inline constexpr std::array<std::pair<const char*, const char*>, (size_t) SubrectSlot::Count> SubrectBaseKeys = { {
|
||||
{ NVSDK_NGX_Parameter_DLSS_Input_Color_Subrect_Base_X, NVSDK_NGX_Parameter_DLSS_Input_Color_Subrect_Base_Y },
|
||||
{ NVSDK_NGX_Parameter_DLSS_Input_Depth_Subrect_Base_X, NVSDK_NGX_Parameter_DLSS_Input_Depth_Subrect_Base_Y },
|
||||
{ NVSDK_NGX_Parameter_DLSS_Input_MV_SubrectBase_X, NVSDK_NGX_Parameter_DLSS_Input_MV_SubrectBase_Y },
|
||||
{ NVSDK_NGX_Parameter_DLSS_Input_Bias_Current_Color_SubrectBase_X,
|
||||
NVSDK_NGX_Parameter_DLSS_Input_Bias_Current_Color_SubrectBase_Y },
|
||||
{ NVSDK_NGX_Parameter_DLSS_Output_Subrect_Base_X, NVSDK_NGX_Parameter_DLSS_Output_Subrect_Base_Y },
|
||||
} };
|
||||
|
||||
struct DetailedGpuTime
|
||||
{
|
||||
std::string name;
|
||||
@@ -132,6 +149,8 @@ class IFeature
|
||||
void GetRenderResolution(const NVSDK_NGX_Parameter* InParameters, unsigned int* OutWidth, unsigned int* OutHeight);
|
||||
void ReadSubrects(const NVSDK_NGX_Parameter* InParameters);
|
||||
const Subrect& GetSubrect(SubrectSlot InSlot) const { return _subrects[(size_t) InSlot]; }
|
||||
// Warns once when the subrect is outside the texture, otherwise logs it once
|
||||
bool SubrectFits(SubrectSlot InSlot, const Subrect& InRect, uint64_t InWidth, uint32_t InHeight);
|
||||
void GetDynamicOutputResolution(NVSDK_NGX_Parameter* InParameters, unsigned int* width, unsigned int* height);
|
||||
float GetSharpness(const NVSDK_NGX_Parameter* InParameters);
|
||||
|
||||
|
||||
@@ -5,6 +5,39 @@
|
||||
|
||||
using Microsoft::WRL::ComPtr;
|
||||
|
||||
ID3D11Resource* IFeature_Dx11::SubrectInput(ID3D11DeviceContext* InContext, ID3D11Resource* InResource,
|
||||
SubrectSlot InSlot)
|
||||
{
|
||||
const auto& rect = GetSubrect(InSlot);
|
||||
|
||||
if (InResource == nullptr || !rect.HasOffset())
|
||||
return InResource;
|
||||
|
||||
ComPtr<ID3D11Texture2D> texture;
|
||||
|
||||
if (InResource->QueryInterface(IID_PPV_ARGS(&texture)) != S_OK)
|
||||
return InResource;
|
||||
|
||||
D3D11_TEXTURE2D_DESC desc {};
|
||||
texture->GetDesc(&desc);
|
||||
|
||||
if (!SubrectFits(InSlot, rect, desc.Width, desc.Height))
|
||||
return InResource;
|
||||
|
||||
auto& copy = SubrectCopy[(size_t) InSlot];
|
||||
|
||||
if (copy == nullptr)
|
||||
copy = std::make_unique<RC_Dx11>("Subrect Copy", Device, InSlot == SubrectSlot::Depth);
|
||||
|
||||
if (!copy->IsInit() || !copy->CreateBufferResource(Device, InResource, rect.Width, rect.Height) ||
|
||||
!copy->Dispatch(InContext, texture.Get(), copy->Buffer(), rect.X, rect.Y, 0, 0, rect.Width, rect.Height))
|
||||
{
|
||||
return InResource;
|
||||
}
|
||||
|
||||
return copy->Buffer();
|
||||
}
|
||||
|
||||
bool IFeature_Dx11::Init(ID3D11Device* InDevice, ID3D11DeviceContext* InContext, NVSDK_NGX_Parameter* InParameters)
|
||||
{
|
||||
if (InDevice == nullptr)
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <shaders/output_scaling/OS_Dx11.h>
|
||||
#include <shaders/bias/Bias_Dx11.h>
|
||||
#include <shaders/magnifier/Magnifier_Dx11.h>
|
||||
#include <shaders/resource_copy/RC_Dx11.h>
|
||||
#include <gpu_time/GpuTime_Dx11.h>
|
||||
|
||||
class IFeature_Dx11 : public virtual IFeature
|
||||
@@ -35,6 +36,12 @@ class IFeature_Dx11 : public virtual IFeature
|
||||
|
||||
std::unique_ptr<GpuTime_Dx11> UpscalerTime = nullptr;
|
||||
|
||||
// Created on the first view that doesn't start at (0, 0)
|
||||
std::array<std::unique_ptr<RC_Dx11>, (size_t) SubrectSlot::Count> SubrectCopy;
|
||||
|
||||
// Resource for backends without subrect support, a copy of the view when it doesn't start at (0, 0)
|
||||
ID3D11Resource* SubrectInput(ID3D11DeviceContext* InContext, ID3D11Resource* InResource, SubrectSlot InSlot);
|
||||
|
||||
virtual bool InitInternal(ID3D11DeviceContext* InContext, NVSDK_NGX_Parameter* InParameters) = 0;
|
||||
virtual bool EvaluateInternal(ID3D11DeviceContext* DeviceContext, NVSDK_NGX_Parameter* InParameters) = 0;
|
||||
|
||||
|
||||
@@ -227,10 +227,10 @@ bool IFeature_Dx11wDx12::ProcessDx11Textures(const NVSDK_NGX_Parameter* InParame
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IFeature_Dx11wDx12::CopyBackOutput()
|
||||
bool IFeature_Dx11wDx12::CopyBackOutput(const D3D11_BOX* InView, UINT InDstX, UINT InDstY)
|
||||
{
|
||||
const auto resourceFrame = (UINT) (_frameCount % DX11_WITH_DX12_CACHED_FRAMES);
|
||||
return Dx11WithDx12::CopyUpscalerOutputToDx11(ResourceCache, resourceFrame);
|
||||
return Dx11WithDx12::CopyUpscalerOutputToDx11(ResourceCache, resourceFrame, InView, InDstX, InDstY);
|
||||
}
|
||||
|
||||
bool IFeature_Dx11wDx12::Init(ID3D11Device* InDevice, ID3D11DeviceContext* InContext, NVSDK_NGX_Parameter* InParameters)
|
||||
@@ -364,8 +364,11 @@ bool IFeature_Dx11wDx12::Evaluate(ID3D11DeviceContext* InDeviceContext, NVSDK_NG
|
||||
ComPtr<ID3D11RenderTargetView> restoreRTVs[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT] = {};
|
||||
ID3D11RenderTargetView* rawRTVs[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT] = {};
|
||||
ComPtr<ID3D11DepthStencilView> restoreDSV = nullptr;
|
||||
ComPtr<ID3D11ComputeShader> restoreCS = nullptr;
|
||||
|
||||
// backup compute shader resources
|
||||
InDeviceContext->CSGetShader(restoreCS.GetAddressOf(), nullptr, nullptr);
|
||||
|
||||
for (UINT i = 0; i < D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT; i++)
|
||||
{
|
||||
InDeviceContext->CSGetShaderResources(i, 1, restoreSRVs[i].GetAddressOf());
|
||||
@@ -400,8 +403,48 @@ bool IFeature_Dx11wDx12::Evaluate(ID3D11DeviceContext* InDeviceContext, NVSDK_NG
|
||||
bool dx12EvalResult = false;
|
||||
bool commandListRecording = false;
|
||||
bool commandListExecuted = false;
|
||||
|
||||
// The D3D12 feature gets the views at (0, 0), so only they are shared and copied back
|
||||
uint32_t zeroedSubrects = 0;
|
||||
std::optional<D3D11_BOX> outputView;
|
||||
|
||||
auto zeroSubrect = [&](SubrectSlot slot)
|
||||
{
|
||||
InParameters->Set(SubrectBaseKeys[(size_t) slot].first, 0u);
|
||||
InParameters->Set(SubrectBaseKeys[(size_t) slot].second, 0u);
|
||||
zeroedSubrects |= 1u << (uint32_t) slot;
|
||||
};
|
||||
|
||||
do
|
||||
{
|
||||
// The D3D12 feature calls GetRenderResolution, without dimensions it's the creation size like DLSS
|
||||
InParameters->Get(NVSDK_NGX_Parameter_DLSS_Render_Subrect_Dimensions_Width, &_renderWidth);
|
||||
InParameters->Get(NVSDK_NGX_Parameter_DLSS_Render_Subrect_Dimensions_Height, &_renderHeight);
|
||||
ReadSubrects(InParameters);
|
||||
|
||||
static constexpr std::pair<SubrectSlot, const char*> subrectInputs[] = {
|
||||
{ SubrectSlot::Color, NVSDK_NGX_Parameter_Color },
|
||||
{ SubrectSlot::Depth, NVSDK_NGX_Parameter_Depth },
|
||||
{ SubrectSlot::MV, NVSDK_NGX_Parameter_MotionVectors },
|
||||
{ SubrectSlot::Mask, NVSDK_NGX_Parameter_DLSS_Input_Bias_Current_Color_Mask },
|
||||
};
|
||||
|
||||
for (const auto& [slot, name] : subrectInputs)
|
||||
{
|
||||
ID3D11Resource* resource = nullptr;
|
||||
|
||||
if (!getOriginalNgxResource(InParameters, name, &resource))
|
||||
continue;
|
||||
|
||||
auto view = SubrectInput(InDeviceContext, resource, slot);
|
||||
|
||||
if (view != resource)
|
||||
{
|
||||
InParameters->Set(name, (void*) view);
|
||||
zeroSubrect(slot);
|
||||
}
|
||||
}
|
||||
|
||||
if (!ProcessDx11Textures(InParameters))
|
||||
{
|
||||
LOG_ERROR("Can't process Dx11 textures!");
|
||||
@@ -415,6 +458,18 @@ bool IFeature_Dx11wDx12::Evaluate(ID3D11DeviceContext* InDeviceContext, NVSDK_NG
|
||||
|
||||
commandListRecording = true;
|
||||
|
||||
// An already shared output is written in place, the D3D12 feature places the view there
|
||||
const auto& outputRect = GetSubrect(SubrectSlot::Output);
|
||||
const auto& outputDesc = dx11Out.SourceDesc;
|
||||
|
||||
if (cache.ParamOutput[resourceFrame] != static_cast<ID3D11Resource*>(dx11Out.SharedTexture) &&
|
||||
outputRect.IsPartOf(outputDesc.Width, outputDesc.Height) &&
|
||||
SubrectFits(SubrectSlot::Output, outputRect, outputDesc.Width, outputDesc.Height))
|
||||
{
|
||||
outputView = D3D11_BOX { 0, 0, 0, outputRect.Width, outputRect.Height, 1 };
|
||||
zeroSubrect(SubrectSlot::Output);
|
||||
}
|
||||
|
||||
InParameters->Set(NVSDK_NGX_Parameter_Color, (void*) dx11Color.Dx12Resource);
|
||||
InParameters->Set(NVSDK_NGX_Parameter_MotionVectors, (void*) dx11Mv.Dx12Resource);
|
||||
InParameters->Set(NVSDK_NGX_Parameter_Output, (void*) dx11Out.Dx12Resource);
|
||||
@@ -458,6 +513,15 @@ bool IFeature_Dx11wDx12::Evaluate(ID3D11DeviceContext* InDeviceContext, NVSDK_NG
|
||||
InParameters->Set("FSR.reactive", restoreFsrReactive);
|
||||
InParameters->Set("FSR.transparencyAndComposition", restoreFsrTransparency);
|
||||
|
||||
for (uint32_t slot = 0; slot < (uint32_t) SubrectSlot::Count; slot++)
|
||||
{
|
||||
if (zeroedSubrects & (1u << slot))
|
||||
{
|
||||
InParameters->Set(SubrectBaseKeys[slot].first, _subrects[slot].X);
|
||||
InParameters->Set(SubrectBaseKeys[slot].second, _subrects[slot].Y);
|
||||
}
|
||||
}
|
||||
|
||||
if (commandListRecording)
|
||||
{
|
||||
const auto closeResult = cmdList->Close();
|
||||
@@ -494,7 +558,9 @@ bool IFeature_Dx11wDx12::Evaluate(ID3D11DeviceContext* InDeviceContext, NVSDK_NG
|
||||
if (!dx12EvalResult || !commandListExecuted)
|
||||
break;
|
||||
|
||||
if (!CopyBackOutput())
|
||||
const auto& outputRect = GetSubrect(SubrectSlot::Output);
|
||||
|
||||
if (!CopyBackOutput(outputView ? &outputView.value() : nullptr, outputRect.X, outputRect.Y))
|
||||
{
|
||||
LOG_ERROR("Can't copy output texture back!");
|
||||
break;
|
||||
@@ -509,7 +575,10 @@ bool IFeature_Dx11wDx12::Evaluate(ID3D11DeviceContext* InDeviceContext, NVSDK_NG
|
||||
else
|
||||
Dx11WithDx12::ClearLastPreparedUpscalerFrameState(ResourceCache);
|
||||
|
||||
// restore compute shader resources
|
||||
// restore compute shader resources, the shader only changes when we cropped the inputs
|
||||
if (zeroedSubrects & ~(1u << (uint32_t) SubrectSlot::Output))
|
||||
InDeviceContext->CSSetShader(restoreCS.Get(), nullptr, 0);
|
||||
|
||||
for (UINT i = 0; i < D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT; i++)
|
||||
{
|
||||
auto raw = restoreSRVs[i].Get();
|
||||
|
||||
@@ -45,7 +45,7 @@ class IFeature_Dx11wDx12 : public virtual IFeature_Dx11
|
||||
|
||||
bool CreateD3D12Objects();
|
||||
bool ProcessDx11Textures(const NVSDK_NGX_Parameter* InParameters);
|
||||
bool CopyBackOutput();
|
||||
bool CopyBackOutput(const D3D11_BOX* InView, UINT InDstX, UINT InDstY);
|
||||
|
||||
void ResourceBarrier(ID3D12GraphicsCommandList* InCommandList, ID3D12Resource* InResource,
|
||||
D3D12_RESOURCE_STATES InBeforeState, D3D12_RESOURCE_STATES InAfterState);
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
#include "IFeature_Dx12.h"
|
||||
#include "State.h"
|
||||
#include <misc/DeferredRelease.h>
|
||||
#include <magic_enum.hpp>
|
||||
|
||||
void IFeature_Dx12::ResourceBarrier(ID3D12GraphicsCommandList* InCommandList, ID3D12Resource* InResource,
|
||||
D3D12_RESOURCE_STATES InBeforeState, D3D12_RESOURCE_STATES InAfterState) const
|
||||
@@ -40,28 +39,10 @@ void IFeature_Dx12::RestoreBarriers(ID3D12GraphicsCommandList* InCommandList)
|
||||
|
||||
bool IFeature_Dx12::CheckSubrect(SubrectSlot InSlot, const Subrect& InRect, ID3D12Resource* InResource)
|
||||
{
|
||||
const uint32_t slotBit = 1u << (uint32_t) InSlot;
|
||||
const auto desc = InResource->GetDesc();
|
||||
|
||||
if ((uint64_t) InRect.X + InRect.Width > desc.Width || (uint64_t) InRect.Y + InRect.Height > desc.Height)
|
||||
{
|
||||
if (!(_subrectWarned & slotBit))
|
||||
{
|
||||
LOG_WARN("{} subrect {}x{} at ({}, {}) is outside the {}x{} texture, using it as is",
|
||||
magic_enum::enum_name(InSlot), InRect.Width, InRect.Height, InRect.X, InRect.Y, desc.Width,
|
||||
desc.Height);
|
||||
_subrectWarned |= slotBit;
|
||||
}
|
||||
|
||||
if (!SubrectFits(InSlot, InRect, desc.Width, desc.Height))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!(_subrectLogged & slotBit))
|
||||
{
|
||||
LOG_INFO("{} subrect {}x{} at ({}, {}) of the {}x{} texture, copying it for {}", magic_enum::enum_name(InSlot),
|
||||
InRect.Width, InRect.Height, InRect.X, InRect.Y, desc.Width, desc.Height, Name());
|
||||
_subrectLogged |= slotBit;
|
||||
}
|
||||
|
||||
auto& copy = SubrectCopy[(size_t) InSlot];
|
||||
|
||||
@@ -326,11 +307,13 @@ bool IFeature_Dx12::Evaluate(ID3D12GraphicsCommandList* InCommandList, NVSDK_NGX
|
||||
}
|
||||
|
||||
// The upscaler and the passes work on the view at (0, 0) of our buffers, this last pass writes it to the output
|
||||
// base. Native subrect upscalers without passes write there by themselves.
|
||||
// base. Upscalers write only their view by themselves, at (0, 0) or at the base for native subrect ones, but the
|
||||
// passes write the whole texture over the other views.
|
||||
const bool nativeSubrects = upscaler == Upscaler::XeSS || upscaler == Upscaler::DLSS || upscaler == Upscaler::DLSSD;
|
||||
bool placeOutput = false;
|
||||
|
||||
if (paramOutput && outputRect.HasOffset() && (!nativeSubrects || !pipeline.empty()) &&
|
||||
if (paramOutput && outputRect.IsPartOf(paramOutput->GetDesc().Width, paramOutput->GetDesc().Height) &&
|
||||
(!pipeline.empty() || (outputRect.HasOffset() && !nativeSubrects)) &&
|
||||
CheckSubrect(SubrectSlot::Output, outputRect, paramOutput))
|
||||
{
|
||||
// By value, the lambdas run after this scope
|
||||
|
||||
@@ -993,7 +993,8 @@ Dx11WithDx12::PrepareUpscalerResources(D3D11_UPSCALER_RESOURCE_CACHE_C& cache, c
|
||||
return result;
|
||||
}
|
||||
|
||||
bool Dx11WithDx12::CopyUpscalerOutputToDx11(D3D11_UPSCALER_RESOURCE_CACHE_C& cache, UINT frameIndex)
|
||||
bool Dx11WithDx12::CopyUpscalerOutputToDx11(D3D11_UPSCALER_RESOURCE_CACHE_C& cache, UINT frameIndex,
|
||||
const D3D11_BOX* InView, UINT InDstX, UINT InDstY)
|
||||
{
|
||||
if (Dx11DeviceContext == nullptr)
|
||||
{
|
||||
@@ -1016,8 +1017,13 @@ bool Dx11WithDx12::CopyUpscalerOutputToDx11(D3D11_UPSCALER_RESOURCE_CACHE_C& cac
|
||||
}
|
||||
|
||||
// An already shared output is written in place, copying a resource onto itself is invalid
|
||||
if (cache.ParamOutput[outputIndex] != static_cast<ID3D11Resource*>(cache.Output[outputIndex].SharedTexture))
|
||||
Dx11DeviceContext->CopyResource(cache.ParamOutput[outputIndex], cache.Output[outputIndex].SharedTexture);
|
||||
auto output = cache.ParamOutput[outputIndex];
|
||||
auto shared = cache.Output[outputIndex].SharedTexture;
|
||||
|
||||
if (output != static_cast<ID3D11Resource*>(shared) && InView != nullptr)
|
||||
Dx11DeviceContext->CopySubresourceRegion(output, 0, InDstX, InDstY, 0, shared, 0, InView);
|
||||
else if (output != static_cast<ID3D11Resource*>(shared))
|
||||
Dx11DeviceContext->CopyResource(output, shared);
|
||||
|
||||
// After a CPU wait the D3D11 queue is idle, submit right away instead of waiting for the next flush
|
||||
if (UseCpuWaitForDx12())
|
||||
|
||||
@@ -159,7 +159,9 @@ class Dx11WithDx12
|
||||
|
||||
static bool SyncDx11ToDx12();
|
||||
static bool SyncDx12ToDx11();
|
||||
static bool CopyUpscalerOutputToDx11(D3D11_UPSCALER_RESOURCE_CACHE_C& cache, UINT frameIndex);
|
||||
// InView: rectangle of the shared output copied to InDstX/InDstY, nullptr copies the whole texture
|
||||
static bool CopyUpscalerOutputToDx11(D3D11_UPSCALER_RESOURCE_CACHE_C& cache, UINT frameIndex,
|
||||
const D3D11_BOX* InView = nullptr, UINT InDstX = 0, UINT InDstY = 0);
|
||||
|
||||
static bool CheckMask(ResourceMask mask, ResourceMask resource);
|
||||
|
||||
|
||||
Reference in new issue
Block a user