From c7183dca16757c868594fce89baa5bfd9fed180b Mon Sep 17 00:00:00 2001 From: Bari <58800830+TheBarii@users.noreply.github.com> Date: Mon, 5 Aug 2024 14:27:25 +0600 Subject: [PATCH] tes --- src/Client/GUI/Engine/Effects/Blur/blur.cpp | 72 ++++++++++----------- src/Client/GUI/Engine/Engine.hpp | 5 +- 2 files changed, 38 insertions(+), 39 deletions(-) diff --git a/src/Client/GUI/Engine/Effects/Blur/blur.cpp b/src/Client/GUI/Engine/Effects/Blur/blur.cpp index d2979ea5..c8feec01 100644 --- a/src/Client/GUI/Engine/Effects/Blur/blur.cpp +++ b/src/Client/GUI/Engine/Effects/Blur/blur.cpp @@ -78,54 +78,53 @@ float4 main(float4 screenSpace : SV_Position) : SV_TARGET const char *upsampleShaderSrc = R"( cbuffer BlurInputBuffer : register(b0) { + float intensity; float2 resolution; - float2 offset; - float2 halfPixel; }; sampler sampler0 : register(s0); -Texture2D texture0 : register(t0); +Texture2D inputTexture : register(t0); float4 main(float4 screenSpace : SV_Position) : SV_TARGET { - float2 uv = screenSpace.xy / resolution; - float4 colorSum = float4(0.0, 0.0, 0.0, 0.0); - - static const float2 offsets[9] = { - float2(-1.0, -1.0) * halfPixel * offset, - float2(0.0, -1.0) * halfPixel * offset, - float2(1.0, -1.0) * halfPixel * offset, - float2(-1.0, 0.0) * halfPixel * offset, - float2(0.0, 0.0) * halfPixel * offset, - float2(1.0, 0.0) * halfPixel * offset, - float2(-1.0, 1.0) * halfPixel * offset, - float2(0.0, 1.0) * halfPixel * offset, - float2(1.0, 1.0) * halfPixel * offset - }; - - static const float weights[9] = { - 0.06136, 0.12245, 0.06136, - 0.12245, 0.24477, 0.12245, - 0.06136, 0.12245, 0.06136 + float2 texSize = resolution; + float2 texelSize = 1.0f / texSize; + float2 texCoord = screenSpace.xy / texSize; + + // Scale offsets with a larger factor for more noticeable blur + float2 offsets[9] = { + float2(-1.0f, -1.0f), float2(0.0f, -1.0f), float2(1.0f, -1.0f), + float2(-1.0f, 0.0f), float2(0.0f, 0.0f), float2(1.0f, 0.0f), + float2(-1.0f, 1.0f), float2(0.0f, 1.0f), float2(1.0f, 1.0f) }; - float weightSum = 0.0; + float weights[9]; + float sum = 0.0f; + float sigma = intensity * 2.0f; // Scale intensity for more noticeable blur for (int i = 0; i < 9; i++) { - weightSum += weights[i]; + float2 offset = offsets[i] * texelSize * sigma; + weights[i] = exp(-dot(offset, offset) / (2.0f * sigma * sigma)); + sum += weights[i]; } + float4 color = float4(0.0f, 0.0f, 0.0f, 0.0f); for (int i = 0; i < 9; i++) { - colorSum += texture0.Sample(sampler0, uv + offsets[i]) * (weights[i] / weightSum); + float2 offset = offsets[i] * texelSize * sigma; + color += inputTexture.Sample(sampler0, texCoord + offset) * weights[i]; } - return colorSum; + color /= sum; + + return color; } + )"; + const char *dbgDrawTextureShaderSrc = "cbuffer BlurInputBuffer : register(b0)\ {\ float2 resolution;\ @@ -146,20 +145,25 @@ float4 main(PS_INPUT input, float4 screenSpace : SV_Position) : SV_TARGET {\ ID3DBlob *TryCompileShader(const char *pSrcData, const char *pTarget) { HRESULT hr; + ID3DBlob *shaderBlob = nullptr; + ID3DBlob *errorBlob = nullptr; - ID3DBlob *shaderBlob; - ID3DBlob *errorBlob; hr = D3DCompile(pSrcData, strlen(pSrcData), nullptr, nullptr, nullptr, "main", pTarget, 0, 0, &shaderBlob, &errorBlob); if (FAILED(hr)) { - Logger::error("[Blur] Failed to compile shader"); - errorBlob->Release(); + if (errorBlob) + { + Logger::error(std::format("[Blur] Failed to compile shader: {}", static_cast(errorBlob->GetBufferPointer()))); + errorBlob->Release(); + } throw std::logic_error("Failed to compile shader!"); } + return shaderBlob; } + ID3D11PixelShader *dbgShader; void Blur::InitializePipeline() @@ -232,8 +236,6 @@ void Blur::RenderToRTV(ID3D11RenderTargetView *pRenderTargetView, ID3D11ShaderRe pContext->PSSetShaderResources(0, 1, (ID3D11ShaderResourceView **)&null); pContext->OMSetRenderTargets(1, &pRenderTargetView, nullptr); - constantBuffer.resolution = rtvSize; - constantBuffer.halfpixel = XMFLOAT2(0.5 / rtvSize.x, 0.5 / rtvSize.y); pContext->UpdateSubresource(pConstantBuffer, 0, nullptr, &constantBuffer, 0, 0); pContext->IASetInputLayout(pInputLayout); @@ -323,14 +325,12 @@ void Blur::RenderBlur(ID3D11RenderTargetView *pDstRenderTargetView, int iteratio XMFLOAT2 fbSize = XMFLOAT2(desc.Width, desc.Height); - constantBuffer.offset = XMFLOAT2(intensity * 4, intensity * 4); + constantBuffer.intensity = intensity; + constantBuffer.resolution = fbSize; pContext->PSSetShader(pUpsampleShader, nullptr, 0); RenderToRTV(pDstRenderTargetView, pOrigShaderResourceView, fbSize); - - - pContext->Release(); pOrigShaderResourceView->Release(); } diff --git a/src/Client/GUI/Engine/Engine.hpp b/src/Client/GUI/Engine/Engine.hpp index 6ee3216a..9cb33a3c 100644 --- a/src/Client/GUI/Engine/Engine.hpp +++ b/src/Client/GUI/Engine/Engine.hpp @@ -27,10 +27,9 @@ using namespace DirectX; struct BlurInputBuffer { + FLOAT intensity; XMFLOAT2 resolution; - XMFLOAT2 offset; - XMFLOAT2 halfpixel; - XMFLOAT2 _dummy; + FLOAT padding[1]; };