Skip to content

Commit

Permalink
aw2 & ffo - sop fix now stable
Browse files Browse the repository at this point in the history
  • Loading branch information
cdozdil committed Mar 26, 2024
1 parent b44b45f commit f10859a
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 97 deletions.
2 changes: 1 addition & 1 deletion CyberXeSS/CyberXeSS.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<IncludePath>$(SolutionDir)external\vulkan\include;$(SolutionDir)external\nvngx_dlss_sdk;$(SolutionDir)external\xess\inc\xess;$(SolutionDir)external\simpleini;$(SolutionDir)external\unordered_dense\include;$(SolutionDir)external\spdlog\include;$(IncludePath)</IncludePath>
<LibraryPath>$(SolutionDir)CyberXess\cas\lib;$(SolutionDir)CyberXess\fsr2\lib;$(SolutionDir)CyberXess\fsr2_212\lib;$(SolutionDir)CyberXess\vulkan;$(SolutionDir)external\xess\lib;$(SolutionDir)CyberXess\d3dx;$(LibraryPath)</LibraryPath>
<TargetName>nvngx</TargetName>
<OutDir>D:\Folders\Games\Deep Rock Galactic\FSD\Binaries\Win64\</OutDir>
<OutDir>F:\Games\Alan Wake 2\</OutDir>
<IntDir>.\x64\Debug</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand Down
7 changes: 2 additions & 5 deletions CyberXeSS/NVNGX_DLSS_Dx11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,9 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D11_EvaluateFeature(ID3D11DeviceConte
Dx11Contexts[handleId].reset();
auto it = std::find_if(Dx11Contexts.begin(), Dx11Contexts.end(), [&handleId](const auto& p) { return p.first == handleId; });
Dx11Contexts.erase(it);

return NVSDK_NGX_Result_Success;
}

// next frame prepare stuff
// next prepare stuff
if (Config::Instance()->newBackend == "xess")
{
Config::Instance()->Dx11Upscaler = "xess";
Expand All @@ -317,9 +315,8 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D11_EvaluateFeature(ID3D11DeviceConte

if (Dx11Contexts[InFeatureHandle->Id].get()->Init(D3D11Device, InDevCtx, InParameters))
{
// next frame we will start rendering again
Config::Instance()->changeBackend = false;
return NVSDK_NGX_Result_Success;
//return NVSDK_NGX_Result_Success;
}
else
return NVSDK_NGX_Result_Fail;
Expand Down
4 changes: 2 additions & 2 deletions CyberXeSS/NVNGX_DLSS_Dx12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom
auto it = std::find_if(Dx12Contexts.begin(), Dx12Contexts.end(), [&handleId](const auto& p) { return p.first == handleId; });
Dx12Contexts.erase(it);

return NVSDK_NGX_Result_Success;
//return NVSDK_NGX_Result_Success;
}

// next frame prepare stuff
Expand All @@ -353,7 +353,7 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom
{
// next frame we will start rendering again
Config::Instance()->changeBackend = false;
return NVSDK_NGX_Result_Success;
//return NVSDK_NGX_Result_Success;
}
else
return NVSDK_NGX_Result_Fail;
Expand Down
49 changes: 47 additions & 2 deletions CyberXeSS/backends/xess/XeSSFeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ RWTexture2D<float4> OutputTexture : register(u0);
void main(uint3 DTid : SV_DispatchThreadID)
{
float4 pixel = InputTexture[DTid.xy];
pixel.rgb *= 10.0;
pixel.rgb *= 125.0;
OutputTexture[DTid.xy] = pixel;
})";

Expand All @@ -26,7 +26,7 @@ RWTexture2D<float4> OutputTexture : register(u0);
void main(uint3 DTid : SV_DispatchThreadID)
{
float4 pixel = InputTexture[DTid.xy];
pixel.rgb *= 0.1;
pixel.rgb *= 0.008;
OutputTexture[DTid.xy] = pixel;
})";

Expand Down Expand Up @@ -247,3 +247,48 @@ float XeSSFeature::GetSharpness(const NVSDK_NGX_Parameter* InParameters)

return sharpness;
}

bool XeSSFeature::CreateBufferResource(ID3D12Device* InDevice, ID3D12Resource* InSource, ID3D12Resource** OutDest, D3D12_RESOURCE_STATES InDestState)
{
if (InDevice == nullptr || InSource == nullptr)
return false;

D3D12_RESOURCE_DESC texDesc = InSource->GetDesc();

if (*OutDest != nullptr)
{
auto bufDesc = (*OutDest)->GetDesc();

if (bufDesc.Width != texDesc.Width || bufDesc.Height != texDesc.Height || bufDesc.Format != texDesc.Format)
{
(*OutDest)->Release();
*OutDest = nullptr;
}
else
return true;
}

spdlog::debug("XeSSFeature::CreateCasBufferResource Start!");

D3D12_HEAP_PROPERTIES heapProperties;
D3D12_HEAP_FLAGS heapFlags;
HRESULT hr = InSource->GetHeapProperties(&heapProperties, &heapFlags);

if (hr != S_OK)
{
spdlog::error("XeSSFeature::CreateBufferResource GetHeapProperties result: {0:x}", hr);
return false;
}

texDesc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS | D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS;

hr = InDevice->CreateCommittedResource(&heapProperties, D3D12_HEAP_FLAG_NONE, &texDesc, InDestState, nullptr, IID_PPV_ARGS(OutDest));

if (hr != S_OK)
{
spdlog::error("XeSSFeature::CreateBufferResource CreateCommittedResource result: {0:x}", hr);
return false;
}

return true;
}
1 change: 1 addition & 0 deletions CyberXeSS/backends/xess/XeSSFeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class XeSSFeature : public virtual IFeature

bool InitXeSS(ID3D12Device* device, const NVSDK_NGX_Parameter* InParameters);
float GetSharpness(const NVSDK_NGX_Parameter* InParameters);
bool CreateBufferResource(ID3D12Device* InDevice, ID3D12Resource* InSource, ID3D12Resource** OutDest, D3D12_RESOURCE_STATES InDestState);

public:

Expand Down
46 changes: 35 additions & 11 deletions CyberXeSS/backends/xess/XeSSFeature_Dx11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ bool XeSSFeatureDx11::Evaluate(ID3D11DeviceContext* InDeviceContext, const NVSDK

if (Config::Instance()->CasEnabled.value_or(true) && sharpness > 0.0f)
{
if (CAS->Buffer() == nullptr && !CAS->CreateBufferResource(Dx12on11Device, dx11Out.Dx12Resource, D3D12_RESOURCE_STATE_UNORDERED_ACCESS))
if (!CAS->CreateBufferResource(Dx12on11Device, dx11Out.Dx12Resource, D3D12_RESOURCE_STATE_UNORDERED_ACCESS))
{
spdlog::error("XeSSFeatureDx12::Evaluate Can't create cas buffer!");

Expand All @@ -138,7 +138,10 @@ bool XeSSFeatureDx11::Evaluate(ID3D11DeviceContext* InDeviceContext, const NVSDK
else
{
if (Config::Instance()->ColorSpaceFix.value_or(false) && OutputEncode->CreateBufferResource(Dx12on11Device, dx11Out.Dx12Resource, D3D12_RESOURCE_STATE_UNORDERED_ACCESS))
{
OutputEncode->SetBufferState(Dx12CommandList, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
params.pOutputTexture = OutputEncode->Buffer();
}
else
params.pOutputTexture = dx11Out.Dx12Resource;
}
Expand Down Expand Up @@ -193,6 +196,8 @@ bool XeSSFeatureDx11::Evaluate(ID3D11DeviceContext* InDeviceContext, const NVSDK

if (Config::Instance()->ColorSpaceFix.value_or(false) && OutputEncode->CreateBufferResource(Dx12on11Device, params.pOutputTexture, D3D12_RESOURCE_STATE_UNORDERED_ACCESS))
{
OutputEncode->SetBufferState(Dx12CommandList, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);

if (!CAS->Dispatch(Dx12CommandList, sharpness, params.pOutputTexture, OutputEncode->Buffer()))
{
Config::Instance()->CasEnabled = false;
Expand All @@ -209,6 +214,8 @@ bool XeSSFeatureDx11::Evaluate(ID3D11DeviceContext* InDeviceContext, const NVSDK
}
else
{
CAS->SetBufferState(Dx12CommandList, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);

if (!CAS->Dispatch(Dx12CommandList, sharpness, CAS->Buffer(), dx11Out.Dx12Resource))
{
Config::Instance()->CasEnabled = false;
Expand All @@ -228,18 +235,26 @@ bool XeSSFeatureDx11::Evaluate(ID3D11DeviceContext* InDeviceContext, const NVSDK
if (OutputEncode->Buffer() && Config::Instance()->ColorSpaceFix.value_or(false))
{
OutputEncode->SetBufferState(Dx12CommandList, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
OutputEncode->Dispatch(Dx12on11Device, Dx12CommandList, OutputEncode->Buffer(), dx11Out.Dx12Resource, 16, 16);

#if _DEBUG
D3DX11SaveTextureToFile(InDeviceContext, dx11Out.SharedTexture, D3DX11_IFF_JPG, L"D:\\aaaa.jpg");
#endif // _DEBUG
if (CreateBufferResource(Dx12on11Device, dx11Out.Dx12Resource, &_outBuffer, D3D12_RESOURCE_STATE_UNORDERED_ACCESS))
{
OutputEncode->Dispatch(Dx12on11Device, Dx12CommandList, OutputEncode->Buffer(), _outBuffer, 16, 16);

}
ResourceBarrier(Dx12CommandList, _outBuffer, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE);
ResourceBarrier(Dx12CommandList, dx11Out.Dx12Resource, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_DEST);

// Execute dx12 commands to process xess
Dx12CommandList->Close();
ID3D12CommandList* ppCommandLists[] = { Dx12CommandList };
Dx12CommandQueue->ExecuteCommandLists(1, ppCommandLists);
Dx12CommandList->CopyResource(dx11Out.Dx12Resource, _outBuffer);
}
else
{
OutputEncode->SetBufferState(Dx12CommandList, D3D12_RESOURCE_STATE_COPY_SOURCE);
ResourceBarrier(Dx12CommandList, dx11Out.Dx12Resource, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_DEST);

Dx12CommandList->CopyResource(dx11Out.Dx12Resource, OutputEncode->Buffer());
}

ResourceBarrier(Dx12CommandList, dx11Out.Dx12Resource, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
}

if (!CopyBackOutput())
{
Expand All @@ -251,6 +266,11 @@ bool XeSSFeatureDx11::Evaluate(ID3D11DeviceContext* InDeviceContext, const NVSDK
return false;
}

// Execute dx12 commands to process xess
Dx12CommandList->Close();
ID3D12CommandList* ppCommandLists[] = { Dx12CommandList };
Dx12CommandQueue->ExecuteCommandLists(1, ppCommandLists);

// imgui
if (Imgui)
{
Expand All @@ -270,6 +290,10 @@ bool XeSSFeatureDx11::Evaluate(ID3D11DeviceContext* InDeviceContext, const NVSDK

XeSSFeatureDx11::~XeSSFeatureDx11()
{
spdlog::debug("XeSSFeatureDx11::XeSSFeatureDx11");
if (_outBuffer != nullptr)
{
_outBuffer->Release();
_outBuffer = nullptr;
}
}

3 changes: 3 additions & 0 deletions CyberXeSS/backends/xess/XeSSFeature_Dx11.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

class XeSSFeatureDx11 : public XeSSFeature, public IFeature_Dx11wDx12
{
private:
ID3D12Resource* _outBuffer;

protected:

public:
Expand Down
31 changes: 16 additions & 15 deletions CyberXeSS/backends/xess/XeSSFeature_Dx12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,26 +111,23 @@ bool XeSSFeatureDx12::Evaluate(ID3D12GraphicsCommandList* InCommandList, const N
spdlog::debug("XeSSFeatureDx12::Evaluate Output exist..");

if (Config::Instance()->OutputResourceBarrier.has_value())
ResourceBarrier(InCommandList, paramOutput,
(D3D12_RESOURCE_STATES)Config::Instance()->OutputResourceBarrier.value(),
D3D12_RESOURCE_STATE_UNORDERED_ACCESS);

if (Config::Instance()->CasEnabled.value_or(true) && sharpness > 0.0f)
{
if (CAS->Buffer() == nullptr && !CAS->CreateBufferResource(Device, paramOutput, D3D12_RESOURCE_STATE_UNORDERED_ACCESS))
{
spdlog::error("XeSSFeatureDx12::Evaluate Can't create cas buffer!");
return false;
}
ResourceBarrier(InCommandList, paramOutput, (D3D12_RESOURCE_STATES)Config::Instance()->OutputResourceBarrier.value(), D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
}

if (Config::Instance()->CasEnabled.value_or(true) && sharpness > 0.0f && CAS->CreateBufferResource(Device, paramOutput, D3D12_RESOURCE_STATE_UNORDERED_ACCESS))
{
CAS->SetBufferState(InCommandList, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
params.pOutputTexture = CAS->Buffer();
}
else if (Config::Instance()->ColorSpaceFix.value_or(false) && OutputEncode->CreateBufferResource(Device, paramOutput, D3D12_RESOURCE_STATE_UNORDERED_ACCESS))
{
OutputEncode->SetBufferState(InCommandList, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
params.pOutputTexture = OutputEncode->Buffer();
}
else
{
if (Config::Instance()->ColorSpaceFix.value_or(false) && OutputEncode->CreateBufferResource(Device, paramOutput, D3D12_RESOURCE_STATE_UNORDERED_ACCESS))
params.pOutputTexture = OutputEncode->Buffer();
else
params.pOutputTexture = paramOutput;
params.pOutputTexture = paramOutput;
}
}
else
Expand Down Expand Up @@ -229,12 +226,14 @@ bool XeSSFeatureDx12::Evaluate(ID3D12GraphicsCommandList* InCommandList, const N
}

//apply cas
if (Config::Instance()->CasEnabled.value_or(true) && sharpness > 0.0f)
if (Config::Instance()->CasEnabled.value_or(true) && sharpness > 0.0f && CAS->Buffer() != nullptr)
{
ResourceBarrier(InCommandList, params.pOutputTexture, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);

if (Config::Instance()->ColorSpaceFix.value_or(false) && OutputEncode->CreateBufferResource(Device, params.pOutputTexture, D3D12_RESOURCE_STATE_UNORDERED_ACCESS))
{
OutputEncode->SetBufferState(InCommandList, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);

if (!CAS->Dispatch(InCommandList, sharpness, params.pOutputTexture, OutputEncode->Buffer()))
{
Config::Instance()->CasEnabled = false;
Expand All @@ -243,6 +242,8 @@ bool XeSSFeatureDx12::Evaluate(ID3D12GraphicsCommandList* InCommandList, const N
}
else
{
CAS->SetBufferState(InCommandList, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);

if (!CAS->Dispatch(InCommandList, sharpness, params.pOutputTexture, paramOutput))
{
Config::Instance()->CasEnabled = false;
Expand Down
4 changes: 3 additions & 1 deletion CyberXeSS/cas/CAS_Dx12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ inline static FfxCas::FfxSurfaceFormat ffxGetSurfaceFormatDX12(DXGI_FORMAT forma
return FfxCas::FFX_SURFACE_FORMAT_R16_UNORM;
case (DXGI_FORMAT_R16_SNORM):
return FfxCas::FFX_SURFACE_FORMAT_R16_SNORM;
//case DXGI_FORMAT_R16_SINT:

case DXGI_FORMAT_R8_TYPELESS:
case DXGI_FORMAT_R8_UNORM:
Expand Down Expand Up @@ -225,6 +224,9 @@ bool CAS_Dx12::Dispatch(ID3D12CommandList* InCommandList, float InSharpness, ID3

void CAS_Dx12::SetBufferState(ID3D12GraphicsCommandList* InCommandList, D3D12_RESOURCE_STATES InState)
{
if (_bufferState == InState)
return;

D3D12_RESOURCE_BARRIER barrier = {};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
barrier.Transition.pResource = _buffer;
Expand Down
Loading

0 comments on commit f10859a

Please sign in to comment.