Skip to content

Commit

Permalink
refactorings etc
Browse files Browse the repository at this point in the history
  • Loading branch information
cdozdil committed Dec 13, 2023
1 parent 880df1b commit 3fe8880
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 57 deletions.
34 changes: 18 additions & 16 deletions CyberXeSS/CyberXessDx11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,59 +159,61 @@ NVSDK_NGX_Result NVSDK_NGX_D3D11_GetFeatureRequirements(IDXGIAdapter* Adapter, c

NVSDK_NGX_Result NVSDK_NGX_D3D11_EvaluateFeature(ID3D11DeviceContext* InDevCtx, const NVSDK_NGX_Handle* InFeatureHandle, const NVSDK_NGX_Parameter* InParameters, PFN_NVSDK_NGX_ProgressCallback InCallback)
{
if (CyberXessContext::instance()->Dx12Device == nullptr)
const auto instance = CyberXessContext::instance();

if (instance->Dx12Device == nullptr)
{
LOG("NVSDK_NGX_D3D11_EvaluateFeature no Dx12Device device!", LEVEL_DEBUG);
return NVSDK_NGX_Result_FAIL_PlatformError;
}

CyberXessContext::instance()->Dx11DeviceContext = InDevCtx;
instance->Dx11DeviceContext = InDevCtx;

HRESULT result;

if (CyberXessContext::instance()->Dx12CommandQueue == nullptr)
if (instance->Dx12CommandQueue == nullptr)
{
D3D12_COMMAND_QUEUE_DESC queueDesc = {};
queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_DISABLE_GPU_TIMEOUT;
queueDesc.Priority = D3D12_COMMAND_QUEUE_PRIORITY_HIGH;
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_COMPUTE;
//queueDesc.Priority = D3D12_COMMAND_QUEUE_PRIORITY_HIGH;

// CreateCommandQueue
result = CyberXessContext::instance()->Dx12Device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&CyberXessContext::instance()->Dx12CommandQueue));
result = instance->Dx12Device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&instance->Dx12CommandQueue));
LOG("NVSDK_NGX_D3D11_EvaluateFeature CreateCommandQueue result: " + int_to_hex(result));

if (result != S_OK || CyberXessContext::instance()->Dx12CommandQueue == nullptr)
if (result != S_OK || instance->Dx12CommandQueue == nullptr)
return NVSDK_NGX_Result_FAIL_PlatformError;

// CreateCommandAllocator
result = CyberXessContext::instance()->Dx12Device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_COMPUTE, IID_PPV_ARGS(&CyberXessContext::instance()->Dx12CommandAllocator));
result = instance->Dx12Device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_COMPUTE, IID_PPV_ARGS(&instance->Dx12CommandAllocator));
LOG("NVSDK_NGX_D3D11_EvaluateFeature CreateCommandAllocator result: " + int_to_hex(result));

if (FAILED(result))
return NVSDK_NGX_Result_FAIL_PlatformError;

// CreateCommandList
result = CyberXessContext::instance()->Dx12Device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_COMPUTE, CyberXessContext::instance()->Dx12CommandAllocator, nullptr, IID_PPV_ARGS(&CyberXessContext::instance()->Dx12CommandList));
result = instance->Dx12Device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_COMPUTE, instance->Dx12CommandAllocator, nullptr, IID_PPV_ARGS(&instance->Dx12CommandList));
LOG("NVSDK_NGX_D3D11_EvaluateFeature CreateCommandList result: " + int_to_hex(result));

if (FAILED(result))
return NVSDK_NGX_Result_FAIL_PlatformError;
}
else
{
CyberXessContext::instance()->Dx12CommandAllocator->Reset();
CyberXessContext::instance()->Dx12CommandList->Reset(CyberXessContext::instance()->Dx12CommandAllocator, nullptr);
instance->Dx12CommandAllocator->Reset();
instance->Dx12CommandList->Reset(instance->Dx12CommandAllocator, nullptr);
}

auto eResult = NVSDK_NGX_D3D12_EvaluateFeature(CyberXessContext::instance()->Dx12CommandList, InFeatureHandle, InParameters, InCallback);
auto eResult = NVSDK_NGX_D3D12_EvaluateFeature(instance->Dx12CommandList, InFeatureHandle, InParameters, InCallback);
LOG("NVSDK_NGX_D3D11_EvaluateFeature result: " + int_to_hex(eResult));

CyberXessContext::instance()->Dx12CommandList->Close();
ID3D12CommandList* ppCommandLists[] = { CyberXessContext::instance()->Dx12CommandList };
CyberXessContext::instance()->Dx12CommandQueue->ExecuteCommandLists(_countof(ppCommandLists), ppCommandLists);
instance->Dx12CommandList->Close();
ID3D12CommandList* ppCommandLists[] = { instance->Dx12CommandList };
instance->Dx12CommandQueue->ExecuteCommandLists(1, ppCommandLists);

CyberXessContext::instance()->Dx12FenceValueCounter++;
CyberXessContext::instance()->Dx12Fence->Release();
instance->Dx12FenceValueCounter++;
instance->Dx12Fence->Release();

return eResult;
}
81 changes: 40 additions & 41 deletions CyberXeSS/CyberXessDx12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "CyberXess.h"
#include "Util.h"

static int cnt = 0;
//static int cnt = 0;

inline void LogCallback(const char* Message, xess_logging_level_t Level)
{
Expand Down Expand Up @@ -36,10 +36,8 @@ static std::string ResultToString(xess_result_t result)

FeatureContext* CreateContext(NVSDK_NGX_Handle** OutHandle)
{
auto instance = CyberXessContext::instance();
auto deviceContext = instance->CreateContext();
auto deviceContext = CyberXessContext::instance()->CreateContext();
*OutHandle = &deviceContext->Handle;

return deviceContext;
}

Expand Down Expand Up @@ -267,7 +265,8 @@ static bool CreateFeature(ID3D12GraphicsCommandList* InCmdList, const NVSDK_NGX_

if (ret != XESS_RESULT_SUCCESS)
{
LOG("NVSDK_NGX_D3D12_CreateFeature xessD3D12Init error : -> " + ResultToString(ret), LEVEL_ERROR);
LOG("NVSDK_NGX_D3D12_CreateFeature xessD3D12Init error: " + ResultToString(ret), LEVEL_ERROR);
CyberXessContext::instance()->init = false;
return false;
}

Expand Down Expand Up @@ -470,9 +469,7 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_CreateFeature(ID3D12GraphicsComma
if (CyberXessContext::instance()->MyConfig->DelayedInit.value_or(false))
return NVSDK_NGX_Result_Success;

auto result = CreateFeature(InCmdList, &context->Handle);

if (result)
if (CreateFeature(InCmdList, &context->Handle))
return NVSDK_NGX_Result_Success;

LOG("NVSDK_NGX_D3D12_CreateFeature: CreateFeature failed", LEVEL_ERROR);
Expand All @@ -486,6 +483,7 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_ReleaseFeature(NVSDK_NGX_Handle*

auto deviceContext = CyberXessContext::instance()->Contexts[InHandle->Id].get();
auto result = xessDestroyContext(deviceContext->XessContext);
LOG("NVSDK_NGX_D3D12_ReleaseFeature: xessDestroyContext result: " + ResultToString(result), LEVEL_DEBUG);
CyberXessContext::instance()->DeleteContext(InHandle);
return NVSDK_NGX_Result_Success;
}
Expand Down Expand Up @@ -517,16 +515,15 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom
LOG("NVSDK_NGX_D3D12_EvaluateFeature callback exist", LEVEL_WARNING);

const auto inParams = static_cast<const NvParameter*>(InParameters);
const auto instance = CyberXessContext::instance();

auto instance = CyberXessContext::instance();

if (!CyberXessContext::instance()->init)
if (!instance->init)
{
LOG("NVSDK_NGX_D3D12_EvaluateFeature init is false, calling CreateFeature!", LEVEL_WARNING);
CyberXessContext::instance()->init = CreateFeature(InCmdList, InFeatureHandle);
instance->init = CreateFeature(InCmdList, InFeatureHandle);
}

if (!CyberXessContext::instance()->init)
if (!instance->init)
{
LOG("NVSDK_NGX_D3D12_EvaluateFeature init still is null CreateFeature failed!", LEVEL_ERROR);
return NVSDK_NGX_Result_Fail;
Expand Down Expand Up @@ -560,13 +557,13 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom
{
LOG("NVSDK_NGX_D3D12_EvaluateFeature Color exist..", LEVEL_DEBUG);

if (CyberXessContext::instance()->Dx11on12Device != nullptr)
if (instance->Dx11on12Device != nullptr)
{
params.pColorTexture = nullptr;

d3d11on11Result = CyberXessContext::instance()->Dx11on12Device->UnwrapUnderlyingResource(
d3d11on11Result = instance->Dx11on12Device->UnwrapUnderlyingResource(
(ID3D11Resource*)inParams->Color,
CyberXessContext::instance()->Dx12CommandQueue,
instance->Dx12CommandQueue,
IID_PPV_ARGS(&params.pColorTexture)
);

Expand All @@ -585,13 +582,13 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom
{
LOG("NVSDK_NGX_D3D12_EvaluateFeature MotionVectors exist..", LEVEL_DEBUG);

if (CyberXessContext::instance()->Dx11on12Device != nullptr)
if (instance->Dx11on12Device != nullptr)
{
params.pVelocityTexture = nullptr;

d3d11on11Result = CyberXessContext::instance()->Dx11on12Device->UnwrapUnderlyingResource(
d3d11on11Result = instance->Dx11on12Device->UnwrapUnderlyingResource(
(ID3D11Resource*)inParams->MotionVectors,
CyberXessContext::instance()->Dx12CommandQueue,
instance->Dx12CommandQueue,
IID_PPV_ARGS(&params.pVelocityTexture)
);

Expand All @@ -610,13 +607,13 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom
{
LOG("NVSDK_NGX_D3D12_EvaluateFeature Output exist..", LEVEL_DEBUG);

if (CyberXessContext::instance()->Dx11on12Device != nullptr)
if (instance->Dx11on12Device != nullptr)
{
params.pOutputTexture = nullptr;

d3d11on11Result = CyberXessContext::instance()->Dx11on12Device->UnwrapUnderlyingResource(
d3d11on11Result = instance->Dx11on12Device->UnwrapUnderlyingResource(
(ID3D11Resource*)inParams->Output,
CyberXessContext::instance()->Dx12CommandQueue,
instance->Dx12CommandQueue,
IID_PPV_ARGS(&params.pOutputTexture)
);

Expand All @@ -631,17 +628,17 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom
return NVSDK_NGX_Result_FAIL_InvalidParameter;
}

if (inParams->Depth && !CyberXessContext::instance()->MyConfig->DisplayResolution.value_or(false))
if (inParams->Depth && !instance->MyConfig->DisplayResolution.value_or(false))
{
LOG("NVSDK_NGX_D3D12_EvaluateFeature Depth exist..", LEVEL_INFO);

if (CyberXessContext::instance()->Dx11on12Device != nullptr)
if (instance->Dx11on12Device != nullptr)
{
params.pDepthTexture = nullptr;

d3d11on11Result = CyberXessContext::instance()->Dx11on12Device->UnwrapUnderlyingResource(
d3d11on11Result = instance->Dx11on12Device->UnwrapUnderlyingResource(
(ID3D11Resource*)inParams->Depth,
CyberXessContext::instance()->Dx12CommandQueue,
instance->Dx12CommandQueue,
IID_PPV_ARGS(&params.pDepthTexture)
);

Expand All @@ -652,15 +649,15 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom
}
else
{
if (!CyberXessContext::instance()->MyConfig->DisplayResolution.value_or(false))
if (!instance->MyConfig->DisplayResolution.value_or(false))
LOG("NVSDK_NGX_D3D12_EvaluateFeature Depth not exist!!", LEVEL_ERROR);
else
LOG("NVSDK_NGX_D3D12_EvaluateFeature Using high res motion vectors, depth is not needed!!", LEVEL_INFO);

params.pDepthTexture = nullptr;
}

if (!CyberXessContext::instance()->MyConfig->AutoExposure.value_or(false))
if (!instance->MyConfig->AutoExposure.value_or(false))
{
if (inParams->ExposureTexture == nullptr)
{
Expand All @@ -671,13 +668,13 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom
{
LOG("NVSDK_NGX_D3D12_EvaluateFeature ExposureTexture exist..", LEVEL_INFO);

if (CyberXessContext::instance()->Dx11on12Device != nullptr)
if (instance->Dx11on12Device != nullptr)
{
params.pExposureScaleTexture = nullptr;

d3d11on11Result = CyberXessContext::instance()->Dx11on12Device->UnwrapUnderlyingResource(
d3d11on11Result = instance->Dx11on12Device->UnwrapUnderlyingResource(
(ID3D11Resource*)inParams->ExposureTexture,
CyberXessContext::instance()->Dx12CommandQueue,
instance->Dx12CommandQueue,
IID_PPV_ARGS(&params.pExposureScaleTexture)
);

Expand All @@ -693,19 +690,19 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom
params.pExposureScaleTexture = nullptr;
}

if (!CyberXessContext::instance()->MyConfig->DisableReactiveMask.value_or(true))
if (!instance->MyConfig->DisableReactiveMask.value_or(true))
{
if (inParams->TransparencyMask != nullptr)
{
LOG("NVSDK_NGX_D3D12_EvaluateFeature TransparencyMask exist..", LEVEL_INFO);

if (CyberXessContext::instance()->Dx11on12Device != nullptr)
if (instance->Dx11on12Device != nullptr)
{
params.pResponsivePixelMaskTexture = nullptr;

d3d11on11Result = CyberXessContext::instance()->Dx11on12Device->UnwrapUnderlyingResource(
d3d11on11Result = instance->Dx11on12Device->UnwrapUnderlyingResource(
(ID3D11Resource*)inParams->TransparencyMask,
CyberXessContext::instance()->Dx12CommandQueue,
instance->Dx12CommandQueue,
IID_PPV_ARGS(&params.pResponsivePixelMaskTexture)
);

Expand Down Expand Up @@ -734,9 +731,9 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom
return NVSDK_NGX_Result_Fail;
}

const UINT64 fence = CyberXessContext::instance()->Dx12FenceValueCounter;
const UINT64 fence = instance->Dx12FenceValueCounter;

if (CyberXessContext::instance()->Dx11on12Device != nullptr)
if (instance->Dx11on12Device != nullptr)
{
// Transition render targets D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE for XeSS
std::vector<CD3DX12_RESOURCE_BARRIER> transitions = {};
Expand All @@ -756,6 +753,8 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom
if (params.pResponsivePixelMaskTexture != nullptr)
CD3DX12_RESOURCE_BARRIER::Transition(params.pResponsivePixelMaskTexture,
D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);

// Transition output D3D12_RESOURCE_STATE_UNORDERED_ACCESS for XeSS
if (params.pOutputTexture != nullptr)
CD3DX12_RESOURCE_BARRIER::Transition(params.pResponsivePixelMaskTexture,
D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
Expand All @@ -766,13 +765,13 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom
LOG("NVSDK_NGX_D3D12_EvaluateFeature Executing!!", LEVEL_INFO);
xessResult = xessD3D12Execute(deviceContext->XessContext, InCmdList, &params);

if (CyberXessContext::instance()->Dx11on12Device != nullptr)
if (instance->Dx11on12Device != nullptr)
{
auto bResult = CyberXessContext::instance()->Dx12Device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&CyberXessContext::instance()->Dx12Fence));
auto bResult = instance->Dx12Device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&instance->Dx12Fence));
LOG("NVSDK_NGX_D3D12_EvaluateFeature CreateFence result: " + int_to_hex(bResult), LEVEL_DEBUG);

// Signal the command queue and wait for the fence
bResult = CyberXessContext::instance()->Dx12CommandQueue->Signal(CyberXessContext::instance()->Dx12Fence, fence);
bResult = instance->Dx12CommandQueue->Signal(instance->Dx12Fence, fence);
LOG("NVSDK_NGX_D3D12_EvaluateFeature Signal result: " + int_to_hex(bResult), LEVEL_DEBUG);
}

Expand All @@ -782,7 +781,7 @@ NVSDK_NGX_API NVSDK_NGX_Result NVSDK_NGX_D3D12_EvaluateFeature(ID3D12GraphicsCom
return NVSDK_NGX_Result_Fail;
}

if (CyberXessContext::instance()->Dx11DeviceContext != nullptr && params.pOutputTexture)
if (instance->Dx11DeviceContext != nullptr && params.pOutputTexture)
{
UINT64 signals[1] = { fence };
ID3D12Fence* fences[1] = { CyberXessContext::instance()->Dx12Fence };
Expand Down

0 comments on commit 3fe8880

Please sign in to comment.