mirror of
https://git.neolegacy.dev/neoStudiosLCE/neoLegacy.git
synced 2026-11-23 20:25:51 +01:00
post processing settings
This commit is contained in:
1 parent
ff8c7fdaa2
commit
ff10600277
25 files changed
+1104
-58
No files matched your search
@@ -1,7 +1,6 @@
|
||||
#include "stdafx.h"
|
||||
#include "../Common/PostProcesser.h"
|
||||
#include <d3dcompiler.h>
|
||||
#include <vector>
|
||||
|
||||
#pragma comment(lib, "d3dcompiler.lib")
|
||||
|
||||
@@ -18,29 +17,111 @@ const char* PostProcesser::g_gammaVSCode =
|
||||
"}\n";
|
||||
|
||||
const char* PostProcesser::g_gammaPSCode =
|
||||
"cbuffer GammaCB : register(b0)\n"
|
||||
"{\n"
|
||||
" float gamma;\n"
|
||||
" float _pad;\n"
|
||||
" float2 uvOffset;\n"
|
||||
" float2 uvScale;\n"
|
||||
" float2 _pad2;\n"
|
||||
"};\n"
|
||||
"Texture2D sceneTex : register(t0);\n"
|
||||
"SamplerState sceneSampler : register(s0);\n"
|
||||
"float4 main(float4 pos : SV_Position, float2 uv : TEXCOORD0) : SV_Target\n"
|
||||
"{\n"
|
||||
" float2 texUV = uvOffset + uv * uvScale;\n"
|
||||
" float4 color = sceneTex.Sample(sceneSampler, texUV);\n"
|
||||
"\n"
|
||||
" color.rgb = max(color.rgb, 0.0);\n"
|
||||
"\n"
|
||||
" color.rgb = pow(color.rgb, 1.0 / gamma);\n"
|
||||
"\n"
|
||||
"cbuffer PostProcessCB : register(b0) { float gamma; float inverseWidth; float inverseHeight; float threshold; float2 uvOffset; float2 uvScale; float2 bloomUvOffset; float2 bloomUvScale; float2 direction; float bloomIntensity; float bloomKnee; };\n"
|
||||
"Texture2D sceneTex : register(t0); SamplerState sceneSampler : register(s0);\n"
|
||||
"float4 main(float4 pos : SV_Position, float2 uv : TEXCOORD0) : SV_Target {\n"
|
||||
" float4 color = sceneTex.Sample(sceneSampler, uvOffset + uv * uvScale);\n"
|
||||
" color.rgb = pow(max(color.rgb, 0.0), 1.0 / max(gamma, 0.001));\n"
|
||||
" return color;\n"
|
||||
"}\n";
|
||||
|
||||
PostProcesser::PostProcesser() = default;
|
||||
const char* PostProcesser::g_fxaaPSCode =
|
||||
"cbuffer PostProcessCB : register(b0) { float gamma; float inverseWidth; float inverseHeight; float threshold; float2 uvOffset; float2 uvScale; float2 bloomUvOffset; float2 bloomUvScale; float2 direction; float bloomIntensity; float bloomKnee; };\n"
|
||||
"Texture2D sceneTex : register(t0); SamplerState sceneSampler : register(s0);\n"
|
||||
"float Luma(float3 color) { return dot(color, float3(0.299, 0.587, 0.114)); }\n"
|
||||
"float2 ClampUV(float2 uv) { float2 halfPixel = float2(inverseWidth, inverseHeight) * 0.5; return clamp(uv, uvOffset + halfPixel, uvOffset + uvScale - halfPixel); }\n"
|
||||
"float4 main(float4 pos : SV_Position, float2 uv : TEXCOORD0) : SV_Target {\n"
|
||||
" float2 pixel = float2(inverseWidth, inverseHeight);\n"
|
||||
" float2 uvCenter = uvOffset + uv * uvScale;\n"
|
||||
" float lumaNW = Luma(sceneTex.Sample(sceneSampler, ClampUV(uvCenter + pixel * float2(-1.0, -1.0))).rgb);\n"
|
||||
" float lumaNE = Luma(sceneTex.Sample(sceneSampler, ClampUV(uvCenter + pixel * float2(1.0, -1.0))).rgb);\n"
|
||||
" float lumaSW = Luma(sceneTex.Sample(sceneSampler, ClampUV(uvCenter + pixel * float2(-1.0, 1.0))).rgb);\n"
|
||||
" float lumaSE = Luma(sceneTex.Sample(sceneSampler, ClampUV(uvCenter + pixel * float2(1.0, 1.0))).rgb);\n"
|
||||
" float4 center = sceneTex.Sample(sceneSampler, uvCenter);\n"
|
||||
" float lumaM = Luma(center.rgb);\n"
|
||||
" float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));\n"
|
||||
" float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));\n"
|
||||
" if (lumaMax - lumaMin < max(0.0312, lumaMax * threshold)) return center;\n"
|
||||
" float2 edge = float2(-((lumaNW + lumaNE) - (lumaSW + lumaSE)), (lumaNW + lumaSW) - (lumaNE + lumaSE));\n"
|
||||
" float reduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * 0.03125, 0.0078125);\n"
|
||||
" float reciprocal = 1.0 / (min(abs(edge.x), abs(edge.y)) + reduce);\n"
|
||||
" edge = clamp(edge * reciprocal, -8.0, 8.0) * pixel;\n"
|
||||
" float3 rgbA = 0.5 * (sceneTex.Sample(sceneSampler, ClampUV(uvCenter + edge * (1.0 / 3.0 - 0.5))).rgb + sceneTex.Sample(sceneSampler, ClampUV(uvCenter + edge * (2.0 / 3.0 - 0.5))).rgb);\n"
|
||||
" float3 rgbB = rgbA * 0.5 + 0.25 * (sceneTex.Sample(sceneSampler, ClampUV(uvCenter + edge * -0.5)).rgb + sceneTex.Sample(sceneSampler, ClampUV(uvCenter + edge * 0.5)).rgb);\n"
|
||||
" float lumaB = Luma(rgbB);\n"
|
||||
" float3 filtered = (lumaB < lumaMin || lumaB > lumaMax) ? rgbA : rgbB;\n"
|
||||
" return float4(lerp(center.rgb, filtered, saturate(bloomIntensity)), center.a);\n"
|
||||
"}\n";
|
||||
|
||||
const char* PostProcesser::g_bloomExtractPSCode =
|
||||
"cbuffer PostProcessCB : register(b0) { float gamma; float inverseWidth; float inverseHeight; float threshold; float2 uvOffset; float2 uvScale; float2 bloomUvOffset; float2 bloomUvScale; float2 direction; float bloomIntensity; float bloomKnee; };\n"
|
||||
"Texture2D sceneTex : register(t0); SamplerState sceneSampler : register(s0);\n"
|
||||
"float4 main(float4 pos : SV_Position, float2 uv : TEXCOORD0) : SV_Target {\n"
|
||||
" float4 color = sceneTex.Sample(sceneSampler, uvOffset + uv * uvScale);\n"
|
||||
" float brightness = max(color.r, max(color.g, color.b));\n"
|
||||
" float soft = saturate((brightness - threshold + bloomKnee) / max(2.0 * bloomKnee, 0.0001));\n"
|
||||
" soft = soft * soft * bloomKnee;\n"
|
||||
" float contribution = max(brightness - threshold, soft) / max(brightness, 0.0001);\n"
|
||||
" return float4(color.rgb * contribution, color.a);\n"
|
||||
"}\n";
|
||||
|
||||
const char* PostProcesser::g_bloomBlurPSCode =
|
||||
"cbuffer PostProcessCB : register(b0) { float gamma; float inverseWidth; float inverseHeight; float threshold; float2 uvOffset; float2 uvScale; float2 bloomUvOffset; float2 bloomUvScale; float2 direction; float bloomIntensity; float bloomKnee; };\n"
|
||||
"Texture2D sceneTex : register(t0); SamplerState sceneSampler : register(s0);\n"
|
||||
"float2 ClampUV(float2 uv) { float2 halfPixel = float2(inverseWidth, inverseHeight) * 0.5; return clamp(uv, uvOffset + halfPixel, uvOffset + uvScale - halfPixel); }\n"
|
||||
"float4 main(float4 pos : SV_Position, float2 uv : TEXCOORD0) : SV_Target {\n"
|
||||
" float2 center = uvOffset + uv * uvScale;\n"
|
||||
" float2 stepUV = direction * float2(inverseWidth, inverseHeight);\n"
|
||||
" float4 color = sceneTex.Sample(sceneSampler, center) * 0.227027;\n"
|
||||
" color += sceneTex.Sample(sceneSampler, ClampUV(center + stepUV * 1.0)) * 0.1945946;\n"
|
||||
" color += sceneTex.Sample(sceneSampler, ClampUV(center - stepUV * 1.0)) * 0.1945946;\n"
|
||||
" color += sceneTex.Sample(sceneSampler, ClampUV(center + stepUV * 2.0)) * 0.1216216;\n"
|
||||
" color += sceneTex.Sample(sceneSampler, ClampUV(center - stepUV * 2.0)) * 0.1216216;\n"
|
||||
" color += sceneTex.Sample(sceneSampler, ClampUV(center + stepUV * 3.0)) * 0.054054;\n"
|
||||
" color += sceneTex.Sample(sceneSampler, ClampUV(center - stepUV * 3.0)) * 0.054054;\n"
|
||||
" color += sceneTex.Sample(sceneSampler, ClampUV(center + stepUV * 4.0)) * 0.016216;\n"
|
||||
" color += sceneTex.Sample(sceneSampler, ClampUV(center - stepUV * 4.0)) * 0.016216;\n"
|
||||
" return color;\n"
|
||||
"}\n";
|
||||
|
||||
const char* PostProcesser::g_bloomCompositePSCode =
|
||||
"cbuffer PostProcessCB : register(b0) { float gamma; float inverseWidth; float inverseHeight; float threshold; float2 uvOffset; float2 uvScale; float2 bloomUvOffset; float2 bloomUvScale; float2 direction; float bloomIntensity; float bloomKnee; };\n"
|
||||
"Texture2D sceneTex : register(t0); Texture2D bloomTex : register(t1); SamplerState sceneSampler : register(s0);\n"
|
||||
"float4 main(float4 pos : SV_Position, float2 uv : TEXCOORD0) : SV_Target {\n"
|
||||
" float2 texUV = uvOffset + uv * uvScale;\n"
|
||||
" float4 color = sceneTex.Sample(sceneSampler, texUV);\n"
|
||||
" float2 bloomUV = bloomUvOffset + uv * bloomUvScale;\n"
|
||||
" color.rgb = saturate(color.rgb + bloomTex.Sample(sceneSampler, bloomUV).rgb * bloomIntensity);\n"
|
||||
" return color;\n"
|
||||
"}\n";
|
||||
|
||||
bool CompilePixelShader(ID3D11Device* device, const char* source, const char* name, UINT compileFlags, ID3D11PixelShader** shader) {
|
||||
ID3DBlob* shaderBlob = nullptr;
|
||||
ID3DBlob* errorBlob = nullptr;
|
||||
const HRESULT compileResult = D3DCompile(source, strlen(source), name, nullptr, nullptr, "main", "ps_4_0", compileFlags, 0, &shaderBlob, &errorBlob);
|
||||
if (FAILED(compileResult)) {
|
||||
if (errorBlob) {
|
||||
app.DebugPrintf(static_cast<const char*>(errorBlob->GetBufferPointer()));
|
||||
errorBlob->Release();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const HRESULT createResult = device->CreatePixelShader(shaderBlob->GetBufferPointer(), shaderBlob->GetBufferSize(), nullptr, shader);
|
||||
|
||||
shaderBlob->Release();
|
||||
if (errorBlob) errorBlob->Release();
|
||||
if (FAILED(createResult)) app.DebugPrintf("PostProcesser: failed to create a pixel shader.\n");
|
||||
|
||||
return SUCCEEDED(createResult);
|
||||
}
|
||||
|
||||
PostProcesser::PostProcesser()
|
||||
{
|
||||
RegisterPass(PostProcessType::Bloom, PostProcessStage::BeforeNativeHud);
|
||||
RegisterPass(PostProcessType::FXAA, PostProcessStage::BeforeNativeHud);
|
||||
RegisterPass(PostProcessType::Gamma, PostProcessStage::BeforeNativeHud);
|
||||
}
|
||||
|
||||
PostProcesser::~PostProcesser()
|
||||
{
|
||||
@@ -63,6 +144,11 @@ void PostProcesser::SetGamma(float gamma)
|
||||
m_gamma = gamma;
|
||||
}
|
||||
|
||||
void PostProcesser::SetFXAAStrength(float strength)
|
||||
{
|
||||
m_fxaaStrength = max(0.0f, min(strength, 1.0f));
|
||||
}
|
||||
|
||||
void PostProcesser::Init()
|
||||
{
|
||||
if (!g_pd3dDevice || !g_pSwapChain)
|
||||
@@ -155,14 +241,45 @@ void PostProcesser::Init()
|
||||
if (FAILED(hr))
|
||||
return;
|
||||
|
||||
if (!CompilePixelShader(g_pd3dDevice, g_fxaaPSCode, "FXAAPS", compileFlags, &m_pFXAAPS) ||
|
||||
!CompilePixelShader(g_pd3dDevice, g_bloomExtractPSCode, "BloomExtractPS", compileFlags, &m_pBloomExtractPS) ||
|
||||
!CompilePixelShader(g_pd3dDevice, g_bloomBlurPSCode, "BloomBlurPS", compileFlags, &m_pBloomBlurPS) ||
|
||||
!CompilePixelShader(g_pd3dDevice, g_bloomCompositePSCode, "BloomCompositePS", compileFlags, &m_pBloomCompositePS))
|
||||
{
|
||||
Cleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
D3D11_TEXTURE2D_DESC bloomDesc = texDesc;
|
||||
bloomDesc.Width = max(1U, bbDesc.Width / 2);
|
||||
bloomDesc.Height = max(1U, bbDesc.Height / 2);
|
||||
m_bloomTexWidth = bloomDesc.Width;
|
||||
m_bloomTexHeight = bloomDesc.Height;
|
||||
|
||||
if (FAILED(g_pd3dDevice->CreateTexture2D(&bloomDesc, nullptr, &m_pBloomTexA)) ||
|
||||
FAILED(g_pd3dDevice->CreateShaderResourceView(m_pBloomTexA, nullptr, &m_pBloomSRVA)) ||
|
||||
FAILED(g_pd3dDevice->CreateRenderTargetView(m_pBloomTexA, nullptr, &m_pBloomRTVA)) ||
|
||||
FAILED(g_pd3dDevice->CreateTexture2D(&bloomDesc, nullptr, &m_pBloomTexB)) ||
|
||||
FAILED(g_pd3dDevice->CreateShaderResourceView(m_pBloomTexB, nullptr, &m_pBloomSRVB)) ||
|
||||
FAILED(g_pd3dDevice->CreateRenderTargetView(m_pBloomTexB, nullptr, &m_pBloomRTVB)))
|
||||
{
|
||||
OutputDebugStringA("PostProcesser: failed to create bloom render targets.\n");
|
||||
Cleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
D3D11_BUFFER_DESC cbDesc;
|
||||
ZeroMemory(&cbDesc, sizeof(cbDesc));
|
||||
cbDesc.ByteWidth = sizeof(GammaCBData);
|
||||
cbDesc.ByteWidth = sizeof(PostProcessCBData);
|
||||
cbDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
|
||||
GammaCBData initData = {1.0f, 0, 0.0f, 0.0f, 1.0f, 1.0f, {0, 0}};
|
||||
PostProcessCBData initData = {1.0f, 1.0f / static_cast<float>(m_gammaTexWidth),
|
||||
1.0f / static_cast<float>(m_gammaTexHeight),
|
||||
0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
|
||||
0.0f, 0.0f, 1.0f, 1.0f,
|
||||
0.0f, 0.0f, 0.0f, 0.0f};
|
||||
D3D11_SUBRESOURCE_DATA srData;
|
||||
srData.pSysMem = &initData;
|
||||
srData.SysMemPitch = 0;
|
||||
@@ -170,7 +287,10 @@ void PostProcesser::Init()
|
||||
|
||||
hr = g_pd3dDevice->CreateBuffer(&cbDesc, &srData, &m_pGammaCB);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Cleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
D3D11_SAMPLER_DESC sampDesc;
|
||||
ZeroMemory(&sampDesc, sizeof(sampDesc));
|
||||
@@ -181,7 +301,18 @@ void PostProcesser::Init()
|
||||
|
||||
hr = g_pd3dDevice->CreateSamplerState(&sampDesc, &m_pGammaSampler);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Cleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
hr = g_pd3dDevice->CreateSamplerState(&sampDesc, &m_pPostProcessSampler);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Cleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
D3D11_RASTERIZER_DESC rasDesc;
|
||||
ZeroMemory(&rasDesc, sizeof(rasDesc));
|
||||
@@ -192,7 +323,10 @@ void PostProcesser::Init()
|
||||
|
||||
hr = g_pd3dDevice->CreateRasterizerState(&rasDesc, &m_pGammaRastState);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Cleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
D3D11_DEPTH_STENCIL_DESC dsDesc;
|
||||
ZeroMemory(&dsDesc, sizeof(dsDesc));
|
||||
@@ -201,7 +335,10 @@ void PostProcesser::Init()
|
||||
|
||||
hr = g_pd3dDevice->CreateDepthStencilState(&dsDesc, &m_pGammaDepthState);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Cleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
D3D11_BLEND_DESC blendDesc;
|
||||
ZeroMemory(&blendDesc, sizeof(blendDesc));
|
||||
@@ -210,7 +347,10 @@ void PostProcesser::Init()
|
||||
|
||||
hr = g_pd3dDevice->CreateBlendState(&blendDesc, &m_pGammaBlendState);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Cleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
m_initialized = true;
|
||||
}
|
||||
@@ -239,8 +379,10 @@ void PostProcesser::Apply() const
|
||||
hr = ctx->Map(m_pGammaCB, 0, mapType, 0, &mapped);
|
||||
if (SUCCEEDED(hr))
|
||||
{
|
||||
GammaCBData *cb = static_cast<GammaCBData *>(mapped.pData);
|
||||
PostProcessCBData *cb = static_cast<PostProcessCBData *>(mapped.pData);
|
||||
cb->gamma = m_gamma;
|
||||
cb->inverseWidth = 1.0f / static_cast<float>(m_gammaTexWidth);
|
||||
cb->inverseHeight = 1.0f / static_cast<float>(m_gammaTexHeight);
|
||||
cb->uvOffsetX = 0.0f;
|
||||
cb->uvOffsetY = 0.0f;
|
||||
cb->uvScaleX = 1.0f;
|
||||
@@ -324,7 +466,10 @@ void PostProcesser::CopyBackbuffer()
|
||||
ID3D11Texture2D* pBackBuffer = nullptr;
|
||||
HRESULT hr = g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<LPVOID*>(&pBackBuffer));
|
||||
if (FAILED(hr))
|
||||
{
|
||||
OutputDebugStringA("PostProcesser: failed to acquire the backbuffer for copying.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
D3D11_BOX srcBox = { 0, 0, 0, m_gammaTexWidth, m_gammaTexHeight, 1 };
|
||||
ctx->CopySubresourceRegion(m_pGammaOffscreenTex, 0, 0, 0, 0, pBackBuffer, 0, &srcBox);
|
||||
@@ -361,10 +506,12 @@ void PostProcesser::ApplyFromCopied() const
|
||||
const HRESULT hr = ctx->Map(m_pGammaCB, 0, mapType, 0, &mapped);
|
||||
if (SUCCEEDED(hr))
|
||||
{
|
||||
const auto cb = static_cast<GammaCBData*>(mapped.pData);
|
||||
const auto cb = static_cast<PostProcessCBData*>(mapped.pData);
|
||||
cb->gamma = m_gamma;
|
||||
const float texW = static_cast<float>(m_gammaTexWidth);
|
||||
const float texH = static_cast<float>(m_gammaTexHeight);
|
||||
cb->inverseWidth = 1.0f / texW;
|
||||
cb->inverseHeight = 1.0f / texH;
|
||||
cb->uvOffsetX = vp.TopLeftX / texW;
|
||||
cb->uvOffsetY = vp.TopLeftY / texH;
|
||||
cb->uvScaleX = vp.Width / texW;
|
||||
@@ -412,8 +559,337 @@ void PostProcesser::ApplyFromCopied() const
|
||||
oldDSV->Release();
|
||||
}
|
||||
|
||||
void PostProcesser::RegisterPass(PostProcessType type, PostProcessStage stage, bool enabled)
|
||||
{
|
||||
for (RegisteredPass& pass : m_registeredPasses)
|
||||
{
|
||||
if (pass.type == type)
|
||||
{
|
||||
pass.stage = stage;
|
||||
pass.enabled = enabled;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
m_registeredPasses.push_back({type, stage, enabled});
|
||||
}
|
||||
|
||||
void PostProcesser::SetPassEnabled(PostProcessType type, bool enabled)
|
||||
{
|
||||
for (RegisteredPass& pass : m_registeredPasses)
|
||||
{
|
||||
if (pass.type == type)
|
||||
{
|
||||
pass.enabled = enabled;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PostProcesser::RunStage(PostProcessStage stage)
|
||||
{
|
||||
if (!m_initialized || !g_pImmediateContext)
|
||||
return;
|
||||
|
||||
D3D11_VIEWPORT viewport = {};
|
||||
UINT viewportCount = 1;
|
||||
g_pImmediateContext->RSGetViewports(&viewportCount, &viewport);
|
||||
if (viewportCount == 0 || viewport.Width <= 0.0f || viewport.Height <= 0.0f)
|
||||
return;
|
||||
|
||||
for (const RegisteredPass& pass : m_registeredPasses)
|
||||
{
|
||||
if (!pass.enabled || pass.stage != stage)
|
||||
continue;
|
||||
|
||||
switch (pass.type)
|
||||
{
|
||||
case PostProcessType::Bloom:
|
||||
ApplyBloom(viewport);
|
||||
break;
|
||||
case PostProcessType::FXAA:
|
||||
ApplyFXAA(viewport);
|
||||
break;
|
||||
case PostProcessType::Gamma:
|
||||
ApplyGamma(viewport);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool PostProcesser::DrawFullscreen(ID3D11PixelShader* pixelShader,
|
||||
ID3D11ShaderResourceView* source,
|
||||
ID3D11ShaderResourceView* bloom,
|
||||
ID3D11RenderTargetView* target,
|
||||
const D3D11_VIEWPORT& outputViewport,
|
||||
const D3D11_VIEWPORT& sourceViewport,
|
||||
float sourceWidth,
|
||||
float sourceHeight,
|
||||
const D3D11_VIEWPORT* bloomViewport,
|
||||
float bloomWidth,
|
||||
float bloomHeight,
|
||||
float directionX,
|
||||
float directionY,
|
||||
float gamma,
|
||||
float threshold,
|
||||
float bloomIntensity,
|
||||
float bloomKnee) const
|
||||
{
|
||||
if (!pixelShader || !source || !target || !g_pImmediateContext)
|
||||
return false;
|
||||
|
||||
D3D11_MAPPED_SUBRESOURCE mapped = {};
|
||||
const D3D11_MAP mapType = m_wineMode ? D3D11_MAP_WRITE_NO_OVERWRITE : D3D11_MAP_WRITE_DISCARD;
|
||||
if (FAILED(g_pImmediateContext->Map(m_pGammaCB, 0, mapType, 0, &mapped)))
|
||||
{
|
||||
OutputDebugStringA("PostProcesser: failed to map post-process constants.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
PostProcessCBData* cb = static_cast<PostProcessCBData*>(mapped.pData);
|
||||
const float textureWidth = sourceWidth;
|
||||
const float textureHeight = sourceHeight;
|
||||
const D3D11_VIEWPORT& bloomSourceViewport = bloomViewport ? *bloomViewport : sourceViewport;
|
||||
if (!bloomViewport)
|
||||
{
|
||||
bloomWidth = sourceWidth;
|
||||
bloomHeight = sourceHeight;
|
||||
}
|
||||
cb->gamma = gamma;
|
||||
cb->inverseWidth = 1.0f / textureWidth;
|
||||
cb->inverseHeight = 1.0f / textureHeight;
|
||||
cb->threshold = threshold;
|
||||
cb->uvOffsetX = sourceViewport.TopLeftX / textureWidth;
|
||||
cb->uvOffsetY = sourceViewport.TopLeftY / textureHeight;
|
||||
cb->uvScaleX = sourceViewport.Width / textureWidth;
|
||||
cb->uvScaleY = sourceViewport.Height / textureHeight;
|
||||
cb->bloomUvOffsetX = bloomSourceViewport.TopLeftX / bloomWidth;
|
||||
cb->bloomUvOffsetY = bloomSourceViewport.TopLeftY / bloomHeight;
|
||||
cb->bloomUvScaleX = bloomSourceViewport.Width / bloomWidth;
|
||||
cb->bloomUvScaleY = bloomSourceViewport.Height / bloomHeight;
|
||||
cb->directionX = directionX;
|
||||
cb->directionY = directionY;
|
||||
cb->bloomIntensity = bloomIntensity;
|
||||
cb->bloomKnee = bloomKnee;
|
||||
g_pImmediateContext->Unmap(m_pGammaCB, 0);
|
||||
|
||||
ID3D11RenderTargetView* oldRTV = nullptr;
|
||||
ID3D11DepthStencilView* oldDSV = nullptr;
|
||||
g_pImmediateContext->OMGetRenderTargets(1, &oldRTV, &oldDSV);
|
||||
D3D11_VIEWPORT oldViewports[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE] = {};
|
||||
UINT oldViewportCount = D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE;
|
||||
g_pImmediateContext->RSGetViewports(&oldViewportCount, oldViewports);
|
||||
|
||||
ID3D11InputLayout* oldInputLayout = nullptr;
|
||||
g_pImmediateContext->IAGetInputLayout(&oldInputLayout);
|
||||
D3D11_PRIMITIVE_TOPOLOGY oldTopology;
|
||||
g_pImmediateContext->IAGetPrimitiveTopology(&oldTopology);
|
||||
|
||||
constexpr UINT maxClassInstances = 256;
|
||||
ID3D11ClassInstance* oldVSInstances[maxClassInstances] = {};
|
||||
ID3D11ClassInstance* oldPSInstances[maxClassInstances] = {};
|
||||
UINT oldVSInstanceCount = maxClassInstances;
|
||||
UINT oldPSInstanceCount = maxClassInstances;
|
||||
ID3D11VertexShader* oldVS = nullptr;
|
||||
ID3D11PixelShader* oldPS = nullptr;
|
||||
g_pImmediateContext->VSGetShader(&oldVS, oldVSInstances, &oldVSInstanceCount);
|
||||
g_pImmediateContext->PSGetShader(&oldPS, oldPSInstances, &oldPSInstanceCount);
|
||||
|
||||
ID3D11ShaderResourceView* oldResources[2] = {nullptr, nullptr};
|
||||
ID3D11SamplerState* oldSampler = nullptr;
|
||||
ID3D11Buffer* oldConstantBuffer = nullptr;
|
||||
g_pImmediateContext->PSGetShaderResources(0, 2, oldResources);
|
||||
g_pImmediateContext->PSGetSamplers(0, 1, &oldSampler);
|
||||
g_pImmediateContext->PSGetConstantBuffers(0, 1, &oldConstantBuffer);
|
||||
|
||||
ID3D11RasterizerState* oldRasterizer = nullptr;
|
||||
ID3D11DepthStencilState* oldDepthState = nullptr;
|
||||
UINT oldStencilReference = 0;
|
||||
ID3D11BlendState* oldBlendState = nullptr;
|
||||
FLOAT oldBlendFactor[4] = {};
|
||||
UINT oldSampleMask = 0;
|
||||
g_pImmediateContext->RSGetState(&oldRasterizer);
|
||||
g_pImmediateContext->OMGetDepthStencilState(&oldDepthState, &oldStencilReference);
|
||||
g_pImmediateContext->OMGetBlendState(&oldBlendState, oldBlendFactor, &oldSampleMask);
|
||||
|
||||
ID3D11ShaderResourceView* nullResources[2] = {nullptr, nullptr};
|
||||
g_pImmediateContext->PSSetShaderResources(0, 2, nullResources);
|
||||
g_pImmediateContext->OMSetRenderTargets(1, &target, nullptr);
|
||||
g_pImmediateContext->RSSetViewports(1, &outputViewport);
|
||||
g_pImmediateContext->IASetInputLayout(nullptr);
|
||||
g_pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
||||
g_pImmediateContext->VSSetShader(m_pGammaVS, nullptr, 0);
|
||||
g_pImmediateContext->PSSetShader(pixelShader, nullptr, 0);
|
||||
ID3D11ShaderResourceView* resources[2] = {source, bloom};
|
||||
g_pImmediateContext->PSSetShaderResources(0, 2, resources);
|
||||
g_pImmediateContext->PSSetSamplers(0, 1, &m_pPostProcessSampler);
|
||||
g_pImmediateContext->PSSetConstantBuffers(0, 1, &m_pGammaCB);
|
||||
g_pImmediateContext->RSSetState(m_pGammaRastState);
|
||||
g_pImmediateContext->OMSetDepthStencilState(m_pGammaDepthState, 0);
|
||||
|
||||
constexpr float blendFactor[4] = {0, 0, 0, 0};
|
||||
g_pImmediateContext->OMSetBlendState(m_pGammaBlendState, blendFactor, 0xFFFFFFFF);
|
||||
g_pImmediateContext->Draw(3, 0);
|
||||
g_pImmediateContext->PSSetShaderResources(0, 2, nullResources);
|
||||
|
||||
g_pImmediateContext->OMSetRenderTargets(1, &oldRTV, oldDSV);
|
||||
g_pImmediateContext->RSSetViewports(oldViewportCount, oldViewports);
|
||||
g_pImmediateContext->IASetInputLayout(oldInputLayout);
|
||||
g_pImmediateContext->IASetPrimitiveTopology(oldTopology);
|
||||
g_pImmediateContext->VSSetShader(oldVS, oldVSInstances, oldVSInstanceCount);
|
||||
g_pImmediateContext->PSSetShader(oldPS, oldPSInstances, oldPSInstanceCount);
|
||||
g_pImmediateContext->PSSetShaderResources(0, 2, oldResources);
|
||||
g_pImmediateContext->PSSetSamplers(0, 1, &oldSampler);
|
||||
g_pImmediateContext->PSSetConstantBuffers(0, 1, &oldConstantBuffer);
|
||||
g_pImmediateContext->RSSetState(oldRasterizer);
|
||||
g_pImmediateContext->OMSetDepthStencilState(oldDepthState, oldStencilReference);
|
||||
g_pImmediateContext->OMSetBlendState(oldBlendState, oldBlendFactor, oldSampleMask);
|
||||
|
||||
if (oldRTV)
|
||||
oldRTV->Release();
|
||||
if (oldDSV)
|
||||
oldDSV->Release();
|
||||
if (oldInputLayout)
|
||||
oldInputLayout->Release();
|
||||
if (oldVS)
|
||||
oldVS->Release();
|
||||
if (oldPS)
|
||||
oldPS->Release();
|
||||
for (UINT i = 0; i < oldVSInstanceCount; ++i)
|
||||
{
|
||||
if (oldVSInstances[i])
|
||||
oldVSInstances[i]->Release();
|
||||
}
|
||||
for (UINT i = 0; i < oldPSInstanceCount; ++i)
|
||||
{
|
||||
if (oldPSInstances[i])
|
||||
oldPSInstances[i]->Release();
|
||||
}
|
||||
for (ID3D11ShaderResourceView* resource : oldResources)
|
||||
{
|
||||
if (resource)
|
||||
resource->Release();
|
||||
}
|
||||
if (oldSampler)
|
||||
oldSampler->Release();
|
||||
if (oldConstantBuffer)
|
||||
oldConstantBuffer->Release();
|
||||
if (oldRasterizer)
|
||||
oldRasterizer->Release();
|
||||
if (oldDepthState)
|
||||
oldDepthState->Release();
|
||||
if (oldBlendState)
|
||||
oldBlendState->Release();
|
||||
return true;
|
||||
}
|
||||
|
||||
void PostProcesser::ApplyFXAA(const D3D11_VIEWPORT& viewport)
|
||||
{
|
||||
CopyBackbuffer();
|
||||
DrawFullscreen(m_pFXAAPS, m_pGammaOffscreenSRV, nullptr, g_pRenderTargetView,
|
||||
viewport, viewport, static_cast<float>(m_gammaTexWidth),
|
||||
static_cast<float>(m_gammaTexHeight), nullptr, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.125f, m_fxaaStrength, 0.0f);
|
||||
}
|
||||
|
||||
void PostProcesser::ApplyGamma(const D3D11_VIEWPORT& viewport)
|
||||
{
|
||||
if (m_gamma > 0.99f && m_gamma < 1.01f)
|
||||
return;
|
||||
|
||||
CopyBackbuffer();
|
||||
DrawFullscreen(m_pGammaPS, m_pGammaOffscreenSRV, nullptr, g_pRenderTargetView,
|
||||
viewport, viewport, static_cast<float>(m_gammaTexWidth),
|
||||
static_cast<float>(m_gammaTexHeight), nullptr, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, m_gamma, 0.0f, 0.0f, 0.0f);
|
||||
}
|
||||
|
||||
void PostProcesser::ApplyBloom(const D3D11_VIEWPORT& viewport)
|
||||
{
|
||||
const float scaleX = static_cast<float>(m_bloomTexWidth) / static_cast<float>(m_gammaTexWidth);
|
||||
const float scaleY = static_cast<float>(m_bloomTexHeight) / static_cast<float>(m_gammaTexHeight);
|
||||
D3D11_VIEWPORT bloomViewport = viewport;
|
||||
bloomViewport.TopLeftX *= scaleX;
|
||||
bloomViewport.TopLeftY *= scaleY;
|
||||
bloomViewport.Width *= scaleX;
|
||||
bloomViewport.Height *= scaleY;
|
||||
|
||||
CopyBackbuffer();
|
||||
if (!DrawFullscreen(m_pBloomExtractPS, m_pGammaOffscreenSRV, nullptr, m_pBloomRTVA,
|
||||
bloomViewport, viewport, static_cast<float>(m_gammaTexWidth),
|
||||
static_cast<float>(m_gammaTexHeight), nullptr, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.8f, 0.0f, 0.5f))
|
||||
return;
|
||||
if (!DrawFullscreen(m_pBloomBlurPS, m_pBloomSRVA, nullptr, m_pBloomRTVB,
|
||||
bloomViewport, bloomViewport, static_cast<float>(m_bloomTexWidth),
|
||||
static_cast<float>(m_bloomTexHeight), nullptr, 0.0f, 0.0f,
|
||||
1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f))
|
||||
return;
|
||||
if (!DrawFullscreen(m_pBloomBlurPS, m_pBloomSRVB, nullptr, m_pBloomRTVA,
|
||||
bloomViewport, bloomViewport, static_cast<float>(m_bloomTexWidth),
|
||||
static_cast<float>(m_bloomTexHeight), nullptr, 0.0f, 0.0f,
|
||||
0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f))
|
||||
return;
|
||||
DrawFullscreen(m_pBloomCompositePS, m_pGammaOffscreenSRV, m_pBloomSRVA,
|
||||
g_pRenderTargetView, viewport, viewport,
|
||||
static_cast<float>(m_gammaTexWidth), static_cast<float>(m_gammaTexHeight),
|
||||
&bloomViewport, static_cast<float>(m_bloomTexWidth),
|
||||
static_cast<float>(m_bloomTexHeight), 0.0f, 0.0f, 1.0f,
|
||||
0.0f, 0.65f, 0.0f);
|
||||
}
|
||||
|
||||
void PostProcesser::Cleanup()
|
||||
{
|
||||
if (m_pBloomCompositePS)
|
||||
{
|
||||
m_pBloomCompositePS->Release();
|
||||
m_pBloomCompositePS = nullptr;
|
||||
}
|
||||
if (m_pBloomBlurPS)
|
||||
{
|
||||
m_pBloomBlurPS->Release();
|
||||
m_pBloomBlurPS = nullptr;
|
||||
}
|
||||
if (m_pBloomExtractPS)
|
||||
{
|
||||
m_pBloomExtractPS->Release();
|
||||
m_pBloomExtractPS = nullptr;
|
||||
}
|
||||
if (m_pFXAAPS)
|
||||
{
|
||||
m_pFXAAPS->Release();
|
||||
m_pFXAAPS = nullptr;
|
||||
}
|
||||
if (m_pBloomRTVA)
|
||||
{
|
||||
m_pBloomRTVA->Release();
|
||||
m_pBloomRTVA = nullptr;
|
||||
}
|
||||
if (m_pBloomSRVA)
|
||||
{
|
||||
m_pBloomSRVA->Release();
|
||||
m_pBloomSRVA = nullptr;
|
||||
}
|
||||
if (m_pBloomTexA)
|
||||
{
|
||||
m_pBloomTexA->Release();
|
||||
m_pBloomTexA = nullptr;
|
||||
}
|
||||
if (m_pBloomRTVB)
|
||||
{
|
||||
m_pBloomRTVB->Release();
|
||||
m_pBloomRTVB = nullptr;
|
||||
}
|
||||
if (m_pBloomSRVB)
|
||||
{
|
||||
m_pBloomSRVB->Release();
|
||||
m_pBloomSRVB = nullptr;
|
||||
}
|
||||
if (m_pBloomTexB)
|
||||
{
|
||||
m_pBloomTexB->Release();
|
||||
m_pBloomTexB = nullptr;
|
||||
}
|
||||
if (m_pGammaBlendState)
|
||||
{
|
||||
m_pGammaBlendState->Release();
|
||||
@@ -434,6 +910,11 @@ void PostProcesser::Cleanup()
|
||||
m_pGammaSampler->Release();
|
||||
m_pGammaSampler = nullptr;
|
||||
}
|
||||
if (m_pPostProcessSampler)
|
||||
{
|
||||
m_pPostProcessSampler->Release();
|
||||
m_pPostProcessSampler = nullptr;
|
||||
}
|
||||
if (m_pGammaCB)
|
||||
{
|
||||
m_pGammaCB->Release();
|
||||
@@ -466,4 +947,9 @@ void PostProcesser::Cleanup()
|
||||
}
|
||||
|
||||
m_initialized = false;
|
||||
m_useCustomViewport = false;
|
||||
m_gammaTexWidth = 0;
|
||||
m_gammaTexHeight = 0;
|
||||
m_bloomTexWidth = 0;
|
||||
m_bloomTexHeight = 0;
|
||||
}
|
||||
Reference in new issue
Block a user