Skip to content

Commit

Permalink
tes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBarii committed Aug 5, 2024
1 parent 4c907c3 commit c7183dc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 39 deletions.
72 changes: 36 additions & 36 deletions src/Client/GUI/Engine/Effects/Blur/blur.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;\
Expand All @@ -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<char*>(errorBlob->GetBufferPointer())));
errorBlob->Release();
}
throw std::logic_error("Failed to compile shader!");
}

return shaderBlob;
}


ID3D11PixelShader *dbgShader;

void Blur::InitializePipeline()
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
5 changes: 2 additions & 3 deletions src/Client/GUI/Engine/Engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ using namespace DirectX;

struct BlurInputBuffer
{
FLOAT intensity;
XMFLOAT2 resolution;
XMFLOAT2 offset;
XMFLOAT2 halfpixel;
XMFLOAT2 _dummy;
FLOAT padding[1];
};


Expand Down

0 comments on commit c7183dc

Please sign in to comment.