bloom: improve blur/exposure constants
This commit is contained in:
parent
0c27a5aeac
commit
7939bb8b97
51
bloom.fx
51
bloom.fx
@ -1,5 +1,6 @@
|
|||||||
float2 vInvScreenSize;
|
float2 vInvScreenSize;
|
||||||
float2 vDir;
|
float2 vDir;
|
||||||
|
float fExposure;
|
||||||
|
|
||||||
Texture2D txDiffuseA;
|
Texture2D txDiffuseA;
|
||||||
SamplerState samPoint {
|
SamplerState samPoint {
|
||||||
@ -29,38 +30,28 @@ PS_INPUT VS(VS_INPUT input)
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const float offset[11] = {
|
static const float offset[6] = {
|
||||||
-9.406430666971303,
|
-4.1802892785260735,
|
||||||
-7.425801606895373,
|
-2.3013875682370335,
|
||||||
-5.445401742210555,
|
-0.45807799821605544,
|
||||||
-3.465172537482815,
|
1.3765284379445557,
|
||||||
-1.485055021558738,
|
3.2355245111649937,
|
||||||
0.4950160492928826,
|
5
|
||||||
2.4751038298192056,
|
|
||||||
4.455269417428358,
|
|
||||||
6.435576703455285,
|
|
||||||
8.41608382089975,
|
|
||||||
10
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static const float weight[11] = {
|
static const float weight[6] = {
|
||||||
0.0276904183309881,
|
0.019171156084708173,
|
||||||
0.05417056378718292,
|
0.16924312602932176,
|
||||||
0.09049273288108622,
|
0.4273923867820408,
|
||||||
0.12908964856395883,
|
0.31401440082441,
|
||||||
0.15725301673321052,
|
0.06672257638049649,
|
||||||
0.16358389071865348,
|
0.003456353899022774
|
||||||
0.14531705460040129,
|
|
||||||
0.11023607138371759,
|
|
||||||
0.0714102715628023,
|
|
||||||
0.03950209624702099,
|
|
||||||
0.011254235190977919
|
|
||||||
};
|
};
|
||||||
|
|
||||||
float4 PS(PS_INPUT input) : SV_Target
|
float4 PS(PS_INPUT input) : SV_Target
|
||||||
{
|
{
|
||||||
float4 texColor = float4(0, 0, 0, 0);
|
float4 texColor = float4(0, 0, 0, 0);
|
||||||
for (int i = 0; i < 11; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
float2 texOffset = vDir * offset[i] * vInvScreenSize;
|
float2 texOffset = vDir * offset[i] * vInvScreenSize;
|
||||||
texColor += txDiffuseA.Sample(samPoint, input.Tex + texOffset) * weight[i];
|
texColor += txDiffuseA.Sample(samPoint, input.Tex + texOffset) * weight[i];
|
||||||
}
|
}
|
||||||
@ -91,14 +82,8 @@ DepthStencilState DisableDepth
|
|||||||
|
|
||||||
float4 PSBlend(PS_INPUT input) : SV_Target
|
float4 PSBlend(PS_INPUT input) : SV_Target
|
||||||
{
|
{
|
||||||
float4 texColor = float4(0, 0, 0, 0);
|
float4 texColor = PS(input);
|
||||||
for (int i = 0; i < 11; i++) {
|
texColor = float4(1, 1, 1, 1) - exp2(-texColor * fExposure);
|
||||||
float2 texOffset = vDir * offset[i] * vInvScreenSize;
|
|
||||||
texColor += txDiffuseA.Sample(samPoint, input.Tex + texOffset) * weight[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
texColor = float4(1, 1, 1, 1) - exp2(-texColor * 3.0);
|
|
||||||
|
|
||||||
return float4(texColor.xyz, 1);
|
return float4(texColor.xyz, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
76
src/main.cpp
76
src/main.cpp
@ -2,6 +2,7 @@
|
|||||||
#include <d3d10.h>
|
#include <d3d10.h>
|
||||||
#include <d3dx10.h>
|
#include <d3dx10.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <strsafe.h>
|
||||||
|
|
||||||
#include "globals.hpp"
|
#include "globals.hpp"
|
||||||
#include "print.hpp"
|
#include "print.hpp"
|
||||||
@ -57,6 +58,14 @@ ID3D10Buffer * g_pVertexBuffersBloom[g_dwVertexBufferCountBloom];
|
|||||||
ID3D10EffectShaderResourceVariable * g_pDiffuseAVariableBloom = NULL;
|
ID3D10EffectShaderResourceVariable * g_pDiffuseAVariableBloom = NULL;
|
||||||
ID3D10EffectVectorVariable * g_pInvScreenSizeVariableBloom = NULL;
|
ID3D10EffectVectorVariable * g_pInvScreenSizeVariableBloom = NULL;
|
||||||
ID3D10EffectVectorVariable * g_pDirVariableBloom = NULL;
|
ID3D10EffectVectorVariable * g_pDirVariableBloom = NULL;
|
||||||
|
ID3D10EffectScalarVariable * g_pExposureVariableBloom = NULL;
|
||||||
|
#ifdef _DEBUG
|
||||||
|
int g_bloomPasses = 0;
|
||||||
|
float g_exposure = 3.7f;
|
||||||
|
#else
|
||||||
|
int g_bloomPasses = 4;
|
||||||
|
float g_exposure = 3.4f;
|
||||||
|
#endif
|
||||||
|
|
||||||
typedef D3DXVECTOR2 BloomVertex;
|
typedef D3DXVECTOR2 BloomVertex;
|
||||||
|
|
||||||
@ -150,6 +159,21 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi
|
|||||||
MSG msg = {};
|
MSG msg = {};
|
||||||
while (msg.message != WM_QUIT) {
|
while (msg.message != WM_QUIT) {
|
||||||
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
|
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
|
||||||
|
if (msg.message == WM_KEYDOWN) {
|
||||||
|
} else if (msg.message == WM_CHAR) {
|
||||||
|
switch (msg.wParam) {
|
||||||
|
case 'q': g_bloomPasses -= 1; break;
|
||||||
|
case 'w': g_bloomPasses += 1; break;
|
||||||
|
|
||||||
|
case 'a': g_exposure -= 0.1f; break;
|
||||||
|
case 's': g_exposure += 0.1f; break;
|
||||||
|
|
||||||
|
case 'z': g_exposure -= 0.5f; break;
|
||||||
|
case 'x': g_exposure += 0.5f; break;
|
||||||
|
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
}
|
||||||
TranslateMessage(&msg);
|
TranslateMessage(&msg);
|
||||||
DispatchMessage(&msg);
|
DispatchMessage(&msg);
|
||||||
} else {
|
} else {
|
||||||
@ -538,6 +562,7 @@ HRESULT InitBloomBuffers()
|
|||||||
g_pTechniqueBloomBlend = g_pEffectBloom->GetTechniqueByName("BloomBlend");
|
g_pTechniqueBloomBlend = g_pEffectBloom->GetTechniqueByName("BloomBlend");
|
||||||
g_pInvScreenSizeVariableBloom = g_pEffectBloom->GetVariableByName("vInvScreenSize")->AsVector();
|
g_pInvScreenSizeVariableBloom = g_pEffectBloom->GetVariableByName("vInvScreenSize")->AsVector();
|
||||||
g_pDirVariableBloom = g_pEffectBloom->GetVariableByName("vDir")->AsVector();
|
g_pDirVariableBloom = g_pEffectBloom->GetVariableByName("vDir")->AsVector();
|
||||||
|
g_pExposureVariableBloom = g_pEffectBloom->GetVariableByName("fExposure")->AsScalar();
|
||||||
g_pDiffuseAVariableBloom = g_pEffectBloom->GetVariableByName("txDiffuseA")->AsShaderResource();
|
g_pDiffuseAVariableBloom = g_pEffectBloom->GetVariableByName("txDiffuseA")->AsShaderResource();
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
@ -1196,6 +1221,24 @@ void RenderMeshStatic(const Mesh * mesh, float t)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char sprintbuf[512];
|
||||||
|
static inline int sprint(LPCSTR fmt, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
va_start(args, fmt);
|
||||||
|
char * end;
|
||||||
|
StringCbVPrintfExA(sprintbuf,
|
||||||
|
(sizeof (sprintbuf)),
|
||||||
|
&end,
|
||||||
|
NULL,
|
||||||
|
0,
|
||||||
|
fmt,
|
||||||
|
args);
|
||||||
|
va_end(args);
|
||||||
|
int length = (int)(end - sprintbuf);
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
void RenderFont()
|
void RenderFont()
|
||||||
{
|
{
|
||||||
//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
@ -1211,27 +1254,40 @@ void RenderFont()
|
|||||||
print("g_pVertexBuffersFont->Map");
|
print("g_pVertexBuffersFont->Map");
|
||||||
}
|
}
|
||||||
|
|
||||||
int advance = 10;
|
int length = sprint("bloomPasses: %d\n"
|
||||||
const char * s = "asdf";
|
" exposure: %f",
|
||||||
|
g_bloomPasses,
|
||||||
|
g_exposure);
|
||||||
|
|
||||||
|
const char start_advance = 10;
|
||||||
|
int hadvance = start_advance;
|
||||||
|
int vadvance = -10;
|
||||||
|
const char * s = sprintbuf;
|
||||||
int ix = 0;
|
int ix = 0;
|
||||||
const int charStride = (int)(g_FontSize.Texture.Width / g_FontSize.Glyph.Width);
|
const int charStride = (int)(g_FontSize.Texture.Width / g_FontSize.Glyph.Width);
|
||||||
while (ix < g_iFontBufferLength && *s) {
|
while (ix < g_iFontBufferLength && *s) {
|
||||||
char c = *s++;
|
char c = *s++;
|
||||||
|
|
||||||
float px = (float)advance;
|
float px = (float)hadvance;
|
||||||
float py = -10.0;
|
float py = (float)vadvance;
|
||||||
|
|
||||||
float cx = 0;
|
float cx = 0;
|
||||||
float cy = 0;
|
float cy = 0;
|
||||||
if (c >= 0x20 && c <= 0x7f) {
|
if (c == '\n') {
|
||||||
|
hadvance = start_advance;
|
||||||
|
vadvance -= g_FontSize.Glyph.Height;
|
||||||
|
continue;
|
||||||
|
} else if (c == ' ') {
|
||||||
|
hadvance += g_FontSize.Glyph.Width;
|
||||||
|
continue;
|
||||||
|
} else if (c >= 0x20 && c <= 0x7f) {
|
||||||
c -= 0x20;
|
c -= 0x20;
|
||||||
cx = (float)(c % charStride);
|
cx = (float)(c % charStride);
|
||||||
cy = (float)(c / charStride);
|
cy = (float)(c / charStride);
|
||||||
}
|
}
|
||||||
|
|
||||||
pData[ix++] = FontVertex(px, py, cx, cy);
|
pData[ix++] = FontVertex(px, py, cx, cy);
|
||||||
|
hadvance += g_FontSize.Glyph.Width;
|
||||||
advance += g_FontSize.Glyph.Width;
|
|
||||||
}
|
}
|
||||||
g_pVertexBuffersFont[0]->Unmap();
|
g_pVertexBuffersFont[0]->Unmap();
|
||||||
|
|
||||||
@ -1270,7 +1326,7 @@ void RenderFont()
|
|||||||
|
|
||||||
for (UINT p = 0; p < techDesc.Passes; p++) {
|
for (UINT p = 0; p < techDesc.Passes; p++) {
|
||||||
g_pTechniqueFont->GetPassByIndex(p)->Apply(0);
|
g_pTechniqueFont->GetPassByIndex(p)->Apply(0);
|
||||||
g_pd3dDevice->Draw(4, 0);
|
g_pd3dDevice->Draw(length, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1298,6 +1354,8 @@ void RenderBloom()
|
|||||||
D3D10_TECHNIQUE_DESC techDesc;
|
D3D10_TECHNIQUE_DESC techDesc;
|
||||||
g_pTechniqueBloom->GetDesc(&techDesc);
|
g_pTechniqueBloom->GetDesc(&techDesc);
|
||||||
|
|
||||||
|
g_pExposureVariableBloom->SetFloat(g_exposure);
|
||||||
|
|
||||||
D3DXVECTOR2 dirHorizontal = D3DXVECTOR2(1.0, 0.0);
|
D3DXVECTOR2 dirHorizontal = D3DXVECTOR2(1.0, 0.0);
|
||||||
D3DXVECTOR2 dirVertical = D3DXVECTOR2(0.0, 1.0);
|
D3DXVECTOR2 dirVertical = D3DXVECTOR2(0.0, 1.0);
|
||||||
|
|
||||||
@ -1313,7 +1371,7 @@ void RenderBloom()
|
|||||||
|
|
||||||
ID3D10ShaderResourceView * srv[] = { NULL };
|
ID3D10ShaderResourceView * srv[] = { NULL };
|
||||||
|
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < g_bloomPasses; i++) {
|
||||||
g_pd3dDevice->PSSetShaderResources(0, 1, srv);
|
g_pd3dDevice->PSSetShaderResources(0, 1, srv);
|
||||||
|
|
||||||
// vertical
|
// vertical
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user