From de3ff648b97d417edc29b673c210418de5714a29 Mon Sep 17 00:00:00 2001 From: cdozdil Date: Thu, 25 Apr 2024 16:43:52 +0300 Subject: [PATCH] tried to improve perf a bit --- OptiScaler/ds/DS_Dx12.cpp | 6 ++-- OptiScaler/ds/DS_Dx12.h | 75 ++++++++++++++++++++------------------- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/OptiScaler/ds/DS_Dx12.cpp b/OptiScaler/ds/DS_Dx12.cpp index f06331ed..9d7f1850 100644 --- a/OptiScaler/ds/DS_Dx12.cpp +++ b/OptiScaler/ds/DS_Dx12.cpp @@ -5,7 +5,7 @@ static ID3DBlob* CompileShader(const char* shaderCode, const char* entryPoint, c ID3DBlob* shaderBlob = nullptr; ID3DBlob* errorBlob = nullptr; - HRESULT hr = D3DCompile(shaderCode, strlen(shaderCode), nullptr, nullptr, nullptr, entryPoint, target, 0, 0, &shaderBlob, &errorBlob); + HRESULT hr = D3DCompile(shaderCode, strlen(shaderCode), nullptr, nullptr, nullptr, entryPoint, target, D3DCOMPILE_OPTIMIZATION_LEVEL3, 0, &shaderBlob, &errorBlob); if (FAILED(hr)) { @@ -113,9 +113,9 @@ void DS_Dx12::SetBufferState(ID3D12GraphicsCommandList* InCommandList, D3D12_RES _bufferState = InState; } -bool DS_Dx12::Dispatch(ID3D12Device* InDevice, ID3D12GraphicsCommandList* InCmdList, ID3D12Resource* InResource, ID3D12Resource* OutResource, uint32_t InNumThreadsX, uint32_t InNumThreadsY) +bool DS_Dx12::Dispatch(ID3D12Device* InDevice, ID3D12GraphicsCommandList* InCmdList, ID3D12Resource* InResource, ID3D12Resource* OutResource) { - if (!_init || InDevice == nullptr || InCmdList == nullptr || InResource == nullptr || OutResource == nullptr || InNumThreadsX == 0 || InNumThreadsY == 0) + if (!_init || InDevice == nullptr || InCmdList == nullptr || InResource == nullptr || OutResource == nullptr) return false; spdlog::debug("CS_Dx12::Dispatch [{0}] Start!", _name); diff --git a/OptiScaler/ds/DS_Dx12.h b/OptiScaler/ds/DS_Dx12.h index e5d2329b..0d3d1625 100644 --- a/OptiScaler/ds/DS_Dx12.h +++ b/OptiScaler/ds/DS_Dx12.h @@ -8,10 +8,10 @@ struct alignas(256) Constants { - int32_t srcWidth; - int32_t srcHeight; - int32_t destWidth; - int32_t destHeight; + int32_t srcWidth; + int32_t srcHeight; + int32_t destWidth; + int32_t destHeight; }; class DS_Dx12 @@ -30,13 +30,16 @@ private: D3D12_GPU_DESCRIPTOR_HANDLE _gpuCbvHandle[2]{ { NULL }, { NULL } }; int _counter = 0; + uint32_t InNumThreadsX = 32; + uint32_t InNumThreadsY = 32; + std::string downsampleCode = R"( cbuffer Params : register(b0) { - int _SrcWidth; - int _SrcHeight; - int _DstWidth; - int _DstHeight; + int _SrcWidth; + int _SrcHeight; + int _DstWidth; + int _DstHeight; }; Texture2D InputTexture : register(t0); @@ -44,41 +47,41 @@ RWTexture2D OutputTexture : register(u0); float bicubic_weight(float x) { - float a = -0.5f; - float absX = abs(x); + float a = -0.5f; + float absX = abs(x); - if (absX <= 1.0f) - return (a + 2.0f) * absX * absX * absX - (a + 3.0f) * absX * absX + 1.0f; - else if (absX < 2.0f) - return a * absX * absX * absX - 5.0f * a * absX * absX + 8.0f * a * absX - 4.0f * a; - else - return 0.0f; + if (absX <= 1.0f) + return (a + 2.0f) * absX * absX * absX - (a + 3.0f) * absX * absX + 1.0f; + else if (absX < 2.0f) + return a * absX * absX * absX - 5.0f * a * absX * absX + 8.0f * a * absX - 4.0f * a; + else + return 0.0f; } -[numthreads(16, 16, 1)] +[numthreads(32, 32, 1)] void CSMain(uint3 DTid : SV_DispatchThreadID) { - if (DTid.x >= _DstWidth || DTid.y >= _DstHeight) - return; + if (DTid.x >= _DstWidth || DTid.y >= _DstHeight) + return; - float2 uv = float2(DTid.x / (_DstWidth - 1.0f), DTid.y / (_DstHeight - 1.0f)); - float2 pixel = uv * float2(_SrcWidth, _SrcHeight); - float2 texel = floor(pixel); - float2 t = pixel - texel; - t = t * t * (3.0f - 2.0f * t); + float2 uv = float2(DTid.x / (_DstWidth - 1.0f), DTid.y / (_DstHeight - 1.0f)); + float2 pixel = uv * float2(_SrcWidth, _SrcHeight); + float2 texel = floor(pixel); + float2 t = pixel - texel; + t = t * t * (3.0f - 2.0f * t); - float4 result = float4(0.0f, 0.0f, 0.0f, 0.0f); - for (int y = -1; y <= 2; y++) - { - for (int x = -1; x <= 2; x++) - { - float4 color = InputTexture.Load(int3(texel.x + x, texel.y + y, 0)); - float weight = bicubic_weight(x - t.x) * bicubic_weight(y - t.y); - result += color * weight; - } - } + float4 result = float4(0.0f, 0.0f, 0.0f, 0.0f); + for (int y = -1; y <= 2; y++) + { + for (int x = -1; x <= 2; x++) + { + float4 color = InputTexture.Load(int3(texel.x + x, texel.y + y, 0)); + float weight = bicubic_weight(x - t.x) * bicubic_weight(y - t.y); + result += color * weight; + } + } - OutputTexture[DTid.xy] = result; + OutputTexture[DTid.xy] = result; } )"; @@ -90,7 +93,7 @@ void CSMain(uint3 DTid : SV_DispatchThreadID) public: bool CreateBufferResource(ID3D12Device* InDevice, ID3D12Resource* InSource, D3D12_RESOURCE_STATES InState); void SetBufferState(ID3D12GraphicsCommandList* InCommandList, D3D12_RESOURCE_STATES InState); - bool Dispatch(ID3D12Device* InDevice, ID3D12GraphicsCommandList* InCmdList, ID3D12Resource* InResource, ID3D12Resource* OutResource, uint32_t InNumThreadsX, uint32_t InNumThreadsY); + bool Dispatch(ID3D12Device* InDevice, ID3D12GraphicsCommandList* InCmdList, ID3D12Resource* InResource, ID3D12Resource* OutResource); float Scale = 1.0f; ID3D12Resource* Buffer() { return _buffer; }