Reduced defocus effect to one pass (HLSL/BGFX)

- removed second defocus pass
- limited defocus stength to a maximum of 2.0
This commit is contained in:
Jezze 2016-10-22 22:55:32 +02:00
parent ef16086fea
commit d9ae40b9fe
9 changed files with 47 additions and 55 deletions

View file

@ -84,7 +84,7 @@
{ "type": "vec2", "name": "radial_converge_green", "text": "Green Radial Convergence, ", "default": [ 0.0, 0.0 ], "max": [ 10.0, 10.0 ], "min": [ -10.0, -10.0 ], "step": 0.1, "format": "%2.1f", "screen": "raster" }, { "type": "vec2", "name": "radial_converge_green", "text": "Green Radial Convergence, ", "default": [ 0.0, 0.0 ], "max": [ 10.0, 10.0 ], "min": [ -10.0, -10.0 ], "step": 0.1, "format": "%2.1f", "screen": "raster" },
{ "type": "vec2", "name": "radial_converge_blue", "text": "Blue Radial Convergence, ", "default": [ 0.0, 0.0 ], "max": [ 10.0, 10.0 ], "min": [ -10.0, -10.0 ], "step": 0.1, "format": "%2.1f", "screen": "raster" }, { "type": "vec2", "name": "radial_converge_blue", "text": "Blue Radial Convergence, ", "default": [ 0.0, 0.0 ], "max": [ 10.0, 10.0 ], "min": [ -10.0, -10.0 ], "step": 0.1, "format": "%2.1f", "screen": "raster" },
{ "type": "vec2", "name": "defocus", "text": "Defocus, ", "default": [ 0.5, 0.5 ], "max": [ 10.0, 10.0 ], "min": [ 0.0, 0.0 ], "step": 0.1, "format": "%2.1f", "screen": "crt" }, { "type": "vec2", "name": "defocus", "text": "Defocus, ", "default": [ 0.5, 0.5 ], "max": [ 2.0, 2.0 ], "min": [ 0.0, 0.0 ], "step": 0.1, "format": "%1.1f", "screen": "crt" },
{ "type": "color", "name": "phosphor", "text": "Phosphor Persistence, ", "default": [ 0.45, 0.45, 0.45 ], "max": [ 1.00, 1.00, 1.00 ], "min": [ 0.00, 0.00, 0.00 ], "step": 0.01, "format": "%1.2f", "screen": "crt" }, { "type": "color", "name": "phosphor", "text": "Phosphor Persistence, ", "default": [ 0.45, 0.45, 0.45 ], "max": [ 1.00, 1.00, 1.00 ], "min": [ 0.00, 0.00, 0.00 ], "step": 0.01, "format": "%1.2f", "screen": "crt" },
@ -391,21 +391,7 @@
"output": "internal" "output": "internal"
}, },
{ "effect": "hlsl/defocus", { "effect": "hlsl/defocus",
"name": "Defocus Pass 1", "name": "Defocus Pass",
"disablewhen": [
{ "type": "slider", "condition": "equal", "combine": "or", "name": "adjustments", "value": 0 },
{ "type": "slider", "condition": "equal", "combine": "or", "name": "defocus", "value": [ 0, 0 ] }
],
"uniforms": [
{ "uniform": "u_defocus", "slider": "defocus" }
],
"input": [
{ "sampler": "s_tex", "target": "internal" }
],
"output": "internal"
},
{ "effect": "hlsl/defocus",
"name": "Defocus Pass 2",
"disablewhen": [ "disablewhen": [
{ "type": "slider", "condition": "equal", "combine": "or", "name": "adjustments", "value": 0 }, { "type": "slider", "condition": "equal", "combine": "or", "name": "adjustments", "value": 0 },
{ "type": "slider", "condition": "equal", "combine": "or", "name": "defocus", "value": [ 0, 0 ] } { "type": "slider", "condition": "equal", "combine": "or", "name": "defocus", "value": [ 0, 0 ] }

View file

@ -78,36 +78,38 @@ VS_OUTPUT vs_main(VS_INPUT Input)
uniform float2 Defocus = float2(0.0f, 0.0f); uniform float2 Defocus = float2(0.0f, 0.0f);
static const float2 Coord1Offset = float2( 0.75f, 0.50f); // previously this pass was applied two times with offsets of 0.25, 0.5, 0.75, 1.0
static const float2 Coord2Offset = float2( 0.25f, 1.00f); // now this pass is applied only once with offsets of 0.25, 0.55, 1.0, 1.6 to achieve the same appearance as before till a maximum defocus of 2.0
static const float2 Coord3Offset = float2(-0.50f, 0.75f); static const float2 CoordOffset8[8] =
static const float2 Coord4Offset = float2(-1.00f, 0.25f); {
static const float2 Coord5Offset = float2(-0.75f, -0.50f); // 0.075x² + 0.225x + 0.25
static const float2 Coord6Offset = float2(-0.25f, -1.00f); float2(-1.60f, 0.25f),
static const float2 Coord7Offset = float2( 0.50f, -0.75f); float2(-1.00f, -0.55f),
static const float2 Coord8Offset = float2( 1.00f, -0.25f); float2(-0.55f, 1.00f),
float2(-0.25f, -1.60f),
float2( 0.25f, 1.60f),
float2( 0.55f, -1.00f),
float2( 1.00f, 0.55f),
float2( 1.60f, -0.25f),
};
float4 ps_main(PS_INPUT Input) : COLOR float4 ps_main(PS_INPUT Input) : COLOR
{ {
// imaginary texel dimensions independed from screen dimension, but ratio // imaginary texel dimensions independed from source and target dimension
float2 TexelDims = (1.0f / 1024); float2 TexelDims = (1.0f / 1024.0f);
float2 DefocusTexelDims = Defocus * TexelDims; float2 DefocusTexelDims = Defocus * TexelDims;
float4 d = tex2D(DiffuseSampler, Input.TexCoord); float3 texel = tex2D(DiffuseSampler, Input.TexCoord).rgb;
float3 d1 = tex2D(DiffuseSampler, Input.TexCoord + Coord1Offset * DefocusTexelDims).rgb; float samples = 1.0f;
float3 d2 = tex2D(DiffuseSampler, Input.TexCoord + Coord2Offset * DefocusTexelDims).rgb;
float3 d3 = tex2D(DiffuseSampler, Input.TexCoord + Coord3Offset * DefocusTexelDims).rgb;
float3 d4 = tex2D(DiffuseSampler, Input.TexCoord + Coord4Offset * DefocusTexelDims).rgb;
float3 d5 = tex2D(DiffuseSampler, Input.TexCoord + Coord5Offset * DefocusTexelDims).rgb;
float3 d6 = tex2D(DiffuseSampler, Input.TexCoord + Coord6Offset * DefocusTexelDims).rgb;
float3 d7 = tex2D(DiffuseSampler, Input.TexCoord + Coord7Offset * DefocusTexelDims).rgb;
float3 d8 = tex2D(DiffuseSampler, Input.TexCoord + Coord8Offset * DefocusTexelDims).rgb;
float3 blurred = (d.rgb + d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8) / 9.0f; for (int i = 0; i < 8; i++)
blurred = lerp(d.rgb, blurred, 1.0f); {
texel += tex2D(DiffuseSampler, Input.TexCoord + CoordOffset8[i] * DefocusTexelDims).rgb;
samples += 1.0f;
}
return float4(blurred, d.a); return float4(texel / samples, 1.0f);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -120,7 +122,7 @@ technique DefaultTechnique
{ {
Lighting = FALSE; Lighting = FALSE;
VertexShader = compile vs_2_0 vs_main(); VertexShader = compile vs_3_0 vs_main();
PixelShader = compile ps_2_0 ps_main(); PixelShader = compile ps_3_0 ps_main();
} }
} }

View file

@ -23,17 +23,23 @@ SAMPLER2D(s_tex, 0);
void main() void main()
{ {
const vec2 Coord1Offset = vec2( 0.75, 0.50); // previously this pass was applied two times with offsets of 0.25, 0.5, 0.75, 1.0
const vec2 Coord2Offset = vec2( 0.25, 1.00); // now this pass is applied only once with offsets of 0.25, 0.55, 1.0, 1.6 to achieve the same appearance as before till a maximum defocus of 2.0
const vec2 Coord3Offset = vec2(-0.50, 0.75); // 0.075 + 0.225x + 0.25
const vec2 Coord4Offset = vec2(-1.00, 0.25); const vec2 Coord1Offset = vec2(-1.60, 0.25);
const vec2 Coord5Offset = vec2(-0.75, -0.50); const vec2 Coord2Offset = vec2(-1.00, -0.55);
const vec2 Coord6Offset = vec2(-0.25, -1.00); const vec2 Coord3Offset = vec2(-0.55, 1.00);
const vec2 Coord7Offset = vec2( 0.50, -0.75); const vec2 Coord4Offset = vec2(-0.25, -1.60);
const vec2 Coord8Offset = vec2( 1.00, -0.25); const vec2 Coord5Offset = vec2( 0.25, 1.60);
const vec2 Coord6Offset = vec2( 0.55, -1.00);
const vec2 Coord7Offset = vec2( 1.00, 0.55);
const vec2 Coord8Offset = vec2( 1.60, -0.25);
// imaginary texel dimensions independed from source and target dimension
vec2 TexelDims = vec2_splat(1.0 / 1024.0);
vec2 DefocusTexelDims = u_defocus.xy * TexelDims.xy;
vec2 DefocusTexelDims = u_defocus.xy / u_tex_size0.xy;
vec4 d0 = texture2D(s_tex, v_texcoord0); vec4 d0 = texture2D(s_tex, v_texcoord0);
vec4 d1 = texture2D(s_tex, v_texcoord0 + Coord1Offset * DefocusTexelDims); vec4 d1 = texture2D(s_tex, v_texcoord0 + Coord1Offset * DefocusTexelDims);
vec4 d2 = texture2D(s_tex, v_texcoord0 + Coord2Offset * DefocusTexelDims); vec4 d2 = texture2D(s_tex, v_texcoord0 + Coord2Offset * DefocusTexelDims);

View file

@ -1350,8 +1350,7 @@ void shaders::render_quad(poly_info *poly, int vertnum)
next_index = color_convolution_pass(rt, next_index, poly, vertnum); // handled in bgfx next_index = color_convolution_pass(rt, next_index, poly, vertnum); // handled in bgfx
next_index = prescale_pass(rt, next_index, poly, vertnum); // handled in bgfx next_index = prescale_pass(rt, next_index, poly, vertnum); // handled in bgfx
next_index = deconverge_pass(rt, next_index, poly, vertnum); // handled in bgfx next_index = deconverge_pass(rt, next_index, poly, vertnum); // handled in bgfx
next_index = defocus_pass(rt, next_index, poly, vertnum); // 1st pass next_index = defocus_pass(rt, next_index, poly, vertnum);
next_index = defocus_pass(rt, next_index, poly, vertnum); // 2nd pass
next_index = phosphor_pass(rt, next_index, poly, vertnum); next_index = phosphor_pass(rt, next_index, poly, vertnum);
// create bloom textures // create bloom textures
@ -1430,8 +1429,7 @@ void shaders::render_quad(poly_info *poly, int vertnum)
next_index = vector_buffer_pass(rt, next_index, poly, vertnum); next_index = vector_buffer_pass(rt, next_index, poly, vertnum);
next_index = deconverge_pass(rt, next_index, poly, vertnum); next_index = deconverge_pass(rt, next_index, poly, vertnum);
next_index = defocus_pass(rt, next_index, poly, vertnum); // 1st pass next_index = defocus_pass(rt, next_index, poly, vertnum);
next_index = defocus_pass(rt, next_index, poly, vertnum); // 2nd pass
next_index = phosphor_pass(rt, next_index, poly, vertnum); next_index = phosphor_pass(rt, next_index, poly, vertnum);
// create bloom textures // create bloom textures
@ -2004,7 +2002,7 @@ slider_desc shaders::s_sliders[] =
{ "Scanline Brightness Offset", 0, 0, 100, 1, SLIDER_FLOAT, SLIDER_SCREEN_TYPE_LCD_OR_RASTER, SLIDER_SCANLINE_BRIGHT_OFFSET, 0.01f, "%1.2f", {} }, { "Scanline Brightness Offset", 0, 0, 100, 1, SLIDER_FLOAT, SLIDER_SCREEN_TYPE_LCD_OR_RASTER, SLIDER_SCANLINE_BRIGHT_OFFSET, 0.01f, "%1.2f", {} },
{ "Scanline Jitter Amount", 0, 0, 100, 1, SLIDER_FLOAT, SLIDER_SCREEN_TYPE_LCD_OR_RASTER, SLIDER_SCANLINE_JITTER, 0.01f, "%1.2f", {} }, { "Scanline Jitter Amount", 0, 0, 100, 1, SLIDER_FLOAT, SLIDER_SCREEN_TYPE_LCD_OR_RASTER, SLIDER_SCANLINE_JITTER, 0.01f, "%1.2f", {} },
{ "Hum Bar Amount", 0, 0, 100, 1, SLIDER_FLOAT, SLIDER_SCREEN_TYPE_LCD_OR_RASTER, SLIDER_HUM_BAR_ALPHA, 0.01f, "%2.2f", {} }, { "Hum Bar Amount", 0, 0, 100, 1, SLIDER_FLOAT, SLIDER_SCREEN_TYPE_LCD_OR_RASTER, SLIDER_HUM_BAR_ALPHA, 0.01f, "%2.2f", {} },
{ "Defocus", 0, 0, 100, 1, SLIDER_VEC2, SLIDER_SCREEN_TYPE_ANY, SLIDER_DEFOCUS, 0.1f, "%2.1f", {} }, { "Defocus", 0, 0, 20, 1, SLIDER_VEC2, SLIDER_SCREEN_TYPE_ANY, SLIDER_DEFOCUS, 0.1f, "%1.1f", {} },
{ "Linear Convergence X,", -100, 0, 100, 1, SLIDER_COLOR, SLIDER_SCREEN_TYPE_ANY, SLIDER_CONVERGE_X, 0.1f, "%3.1f",{} }, { "Linear Convergence X,", -100, 0, 100, 1, SLIDER_COLOR, SLIDER_SCREEN_TYPE_ANY, SLIDER_CONVERGE_X, 0.1f, "%3.1f",{} },
{ "Linear Convergence Y,", -100, 0, 100, 1, SLIDER_COLOR, SLIDER_SCREEN_TYPE_ANY, SLIDER_CONVERGE_Y, 0.1f, "%3.1f", {} }, { "Linear Convergence Y,", -100, 0, 100, 1, SLIDER_COLOR, SLIDER_SCREEN_TYPE_ANY, SLIDER_CONVERGE_Y, 0.1f, "%3.1f", {} },
{ "Radial Convergence X,", -100, 0, 100, 1, SLIDER_COLOR, SLIDER_SCREEN_TYPE_ANY, SLIDER_RADIAL_CONVERGE_X, 0.1f, "%3.1f", {} }, { "Radial Convergence X,", -100, 0, 100, 1, SLIDER_COLOR, SLIDER_SCREEN_TYPE_ANY, SLIDER_RADIAL_CONVERGE_X, 0.1f, "%3.1f", {} },