From fcb8b390fb07b24912b0db92f1ddadb87edd14e6 Mon Sep 17 00:00:00 2001 From: luboslenco Date: Tue, 25 Mar 2025 16:12:52 +0100 Subject: [PATCH] kong test --- base/shaders/smaa_blend_weight.frag.glsl | 308 ------------------- base/shaders/smaa_blend_weight.kong | 375 +++++++++++++++++++++++ base/shaders/smaa_blend_weight.vert.glsl | 29 -- base/shaders/taa_pass.kong | 2 +- 4 files changed, 376 insertions(+), 338 deletions(-) delete mode 100644 base/shaders/smaa_blend_weight.frag.glsl create mode 100644 base/shaders/smaa_blend_weight.kong delete mode 100644 base/shaders/smaa_blend_weight.vert.glsl diff --git a/base/shaders/smaa_blend_weight.frag.glsl b/base/shaders/smaa_blend_weight.frag.glsl deleted file mode 100644 index 8aa3ef42..00000000 --- a/base/shaders/smaa_blend_weight.frag.glsl +++ /dev/null @@ -1,308 +0,0 @@ -#version 450 - -#define SMAA_MAX_SEARCH_STEPS_DIAG 8 -#define SMAA_AREATEX_MAX_DISTANCE 16 -#define SMAA_AREATEX_MAX_DISTANCE_DIAG 20 -#define SMAA_AREATEX_PIXEL_SIZE (1.0 / vec2(160.0, 560.0)) -#define SMAA_AREATEX_SUBTEX_SIZE (1.0 / 7.0) -#define SMAA_SEARCHTEX_SIZE vec2(66.0, 33.0) -#define SMAA_SEARCHTEX_PACKED_SIZE vec2(64.0, 16.0) -#define SMAA_CORNER_ROUNDING 25 -#define SMAA_CORNER_ROUNDING_NORM (float(SMAA_CORNER_ROUNDING) / 100.0) -#define SMAA_AREATEX_SELECT(sample) sample.rg -#define SMAA_SEARCHTEX_SELECT(sample) sample.r -#define mad(a, b, c) (a * b + c) -#define saturate(a) clamp(a, 0.0, 1.0) -#define round(a) floor(a + 0.5) - -uniform sampler2D edges_tex; -uniform sampler2D area_tex; -uniform sampler2D search_tex; -uniform vec2 screen_size; -uniform vec2 screen_size_inv; - -in vec2 tex_coord; -in vec2 pixcoord; -in vec4 offset0; -in vec4 offset1; -in vec4 offset2; -out vec4 frag_color; - -vec2 cdw_end; - -vec4 textureLod_a(sampler2D edges_tex, vec2 coord, float lod) { - coord.y = 1.0 - coord.y; - return textureLod(edges_tex, coord, lod); -} - -#define smaa_sample_level_zero_offset(edges_tex, coord, offset) textureLod_a(edges_tex, coord + offset * screen_size_inv.xy, 0.0) - -vec2 smaa_decode_diag_bilinear_access(vec2 e) { - e.r = e.r * abs(5.0 * e.r - 5.0 * 0.75); - return round(e); -} - -vec4 smaa_decode_diag_bilinear_access(vec4 e) { - e.rb = e.rb * abs(5.0 * e.rb - 5.0 * 0.75); - return round(e); -} - -vec2 smaa_search_diag1(vec2 texcoord, vec2 dir) { - vec4 coord = vec4(texcoord, -1.0, 1.0); - vec3 t = vec3(screen_size_inv.xy, 1.0); - while (coord.z < float(SMAA_MAX_SEARCH_STEPS_DIAG - 1) && coord.w > 0.9) { - coord.xyz = mad(t, vec3(dir, 1.0), coord.xyz); - cdw_end = textureLod_a(edges_tex, coord.xy, 0.0).rg; - coord.w = dot(cdw_end /*e*/, vec2(0.5, 0.5)); - } - return coord.zw; -} - -vec2 smaa_search_diag2(vec2 texcoord, vec2 dir) { - vec4 coord = vec4(texcoord, -1.0, 1.0); - coord.x += 0.25 * screen_size_inv.x; - vec3 t = vec3(screen_size_inv.xy, 1.0); - float cw = coord.w; // TODO: krafix hlsl bug - while (coord.z < float(SMAA_MAX_SEARCH_STEPS_DIAG - 1) && cw > 0.9) { - coord.xyz = mad(t, vec3(dir, 1.0), coord.xyz); - cdw_end = textureLod_a(edges_tex, coord.xy, 0.0).rg; - cdw_end = smaa_decode_diag_bilinear_access(cdw_end); - cw = dot(cdw_end, vec2(0.5, 0.5)); - } - coord.w = cw; - return coord.zw; -} - -vec2 smaa_area_diag(vec2 dist, vec2 e, float offset) { - vec2 texcoord = mad(vec2(SMAA_AREATEX_MAX_DISTANCE_DIAG, SMAA_AREATEX_MAX_DISTANCE_DIAG), e, dist); - texcoord = mad(SMAA_AREATEX_PIXEL_SIZE, texcoord, 0.5 * SMAA_AREATEX_PIXEL_SIZE); - texcoord.x += 0.5; - texcoord.y += SMAA_AREATEX_SUBTEX_SIZE * offset; - return SMAA_AREATEX_SELECT(textureLod(area_tex, texcoord, 0.0)); -} - -vec2 smaa_calculate_diag_weights(vec2 texcoord, vec2 e, vec4 subsample_indices) { - vec2 weights = vec2(0.0, 0.0); - - vec4 d; - if (e.r > 0.0) { - d.xz = smaa_search_diag1(texcoord, vec2(-1.0, 1.0)); - float dadd = cdw_end.y > 0.9 ? 1.0 : 0.0; - d.x += dadd; - } - else { - d.xz = vec2(0.0, 0.0); - } - d.yw = smaa_search_diag1(texcoord, vec2(1.0, -1.0)); - - if (d.x + d.y > 2.0) { - vec4 coords = mad(vec4(-d.x + 0.25, d.x, d.y, -d.y - 0.25), screen_size_inv.xyxy, texcoord.xyxy); - vec4 c; - - c.xy = smaa_sample_level_zero_offset(edges_tex, coords.xy, vec2(-1, 0)).rg; - c.zw = smaa_sample_level_zero_offset(edges_tex, coords.zw, vec2( 1, 0)).rg; - c.yxwz = smaa_decode_diag_bilinear_access(c.xyzw); - - vec2 cc = mad(vec2(2.0, 2.0), c.xz, c.yw); - - float a1condx = step(0.9, d.z); - float a1condy = step(0.9, d.w); - if (a1condx == 1.0) cc.x = 0.0; - if (a1condy == 1.0) cc.y = 0.0; - - weights += smaa_area_diag(d.xy, cc, subsample_indices.z); - } - - d.xz = smaa_search_diag2(texcoord, vec2(-1.0, -1.0)); - if (smaa_sample_level_zero_offset(edges_tex, texcoord, vec2(1, 0)).r > 0.0) { - d.yw = smaa_search_diag2(texcoord, vec2(1.0, 1.0)); - float dadd = cdw_end.y > 0.9 ? 1.0 : 0.0; - d.y += dadd; - } - else { - d.yw = vec2(0.0, 0.0); - } - - if (d.x + d.y > 2.0) { - vec4 coords = mad(vec4(-d.x, -d.x, d.y, d.y), screen_size_inv.xyxy, texcoord.xyxy); - vec4 c; - c.x = smaa_sample_level_zero_offset(edges_tex, coords.xy, vec2(-1, 0)).g; - c.y = smaa_sample_level_zero_offset(edges_tex, coords.xy, vec2( 0, -1)).r; - c.zw = smaa_sample_level_zero_offset(edges_tex, coords.zw, vec2( 1, 0)).gr; - vec2 cc = mad(vec2(2.0, 2.0), c.xz, c.yw); - - float a1condx = step(0.9, d.z); - float a1condy = step(0.9, d.w); - if (a1condx == 1.0) cc.x = 0.0; - if (a1condy == 1.0) cc.y = 0.0; - - weights += smaa_area_diag(d.xy, cc, subsample_indices.w).gr; - } - - return weights; -} - -float smaa_search_length(vec2 e, float offset) { - vec2 scale = SMAA_SEARCHTEX_SIZE * vec2(0.5, -1.0); - vec2 bias = SMAA_SEARCHTEX_SIZE * vec2(offset, 1.0); - - scale += vec2(-1.0, 1.0); - bias += vec2( 0.5, -0.5); - - scale *= 1.0 / SMAA_SEARCHTEX_PACKED_SIZE; - bias *= 1.0 / SMAA_SEARCHTEX_PACKED_SIZE; - - vec2 coord = mad(scale, e, bias); - - return SMAA_SEARCHTEX_SELECT(textureLod(search_tex, coord, 0.0)); -} - -float smaa_search_x_left(vec2 texcoord, float end) { - vec2 e = vec2(0.0, 1.0); - while (texcoord.x > end && e.g > 0.8281 && e.r == 0.0) { - e = textureLod_a(edges_tex, texcoord, 0.0).rg; - texcoord = mad(-vec2(2.0, 0.0), screen_size_inv.xy, texcoord); - } - - float offset = mad(-(255.0 / 127.0), smaa_search_length(e, 0.0), 3.25); - return mad(screen_size_inv.x, offset, texcoord.x); -} - -float smaa_search_x_right(vec2 texcoord, float end) { - vec2 e = vec2(0.0, 1.0); - while (texcoord.x < end && e.g > 0.8281 && e.r == 0.0) { - e = textureLod_a(edges_tex, texcoord, 0.0).rg; - texcoord = mad(vec2(2.0, 0.0), screen_size_inv.xy, texcoord); - } - - float offset = mad(-(255.0 / 127.0), smaa_search_length(e, 0.5), 3.25); - return mad(-screen_size_inv.x, offset, texcoord.x); -} - -float smaa_search_y_up(vec2 texcoord, float end) { - vec2 e = vec2(1.0, 0.0); - while (texcoord.y > end && e.r > 0.8281 && e.g == 0.0) { - e = textureLod_a(edges_tex, texcoord, 0.0).rg; - texcoord = mad(-vec2(0.0, 2.0), screen_size_inv.xy, texcoord); - } - float offset = mad(-(255.0 / 127.0), smaa_search_length(e.gr, 0.0), 3.25); - return mad(screen_size_inv.y, offset, texcoord.y); -} - -float smaa_search_y_down(vec2 texcoord, float end) { - vec2 e = vec2(1.0, 0.0); - while (texcoord.y < end && e.r > 0.8281 && e.g == 0.0) { - e = textureLod_a(edges_tex, texcoord, 0.0).rg; - texcoord = mad(vec2(0.0, 2.0), screen_size_inv.xy, texcoord); - } - float offset = mad(-(255.0 / 127.0), smaa_search_length(e.gr, 0.5), 3.25); - return mad(-screen_size_inv.y, offset, texcoord.y); -} - -vec2 smaa_area(vec2 dist, float e1, float e2, float offset) { - vec2 texcoord = mad(vec2(SMAA_AREATEX_MAX_DISTANCE, SMAA_AREATEX_MAX_DISTANCE), round(4.0 * vec2(e1, e2)), dist); - texcoord = mad(SMAA_AREATEX_PIXEL_SIZE, texcoord, 0.5 * SMAA_AREATEX_PIXEL_SIZE); - texcoord.y = mad(SMAA_AREATEX_SUBTEX_SIZE, offset, texcoord.y); - return SMAA_AREATEX_SELECT(textureLod(area_tex, texcoord, 0.0)); -} - -vec2 smaa_detect_horizontal_corner_pattern(vec2 weights, vec4 texcoord, vec2 d) { - vec2 left_right = step(d.xy, d.yx); - vec2 rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * left_right; - - rounding /= left_right.x + left_right.y; - - vec2 factor = vec2(1.0, 1.0); - factor.x -= rounding.x * smaa_sample_level_zero_offset(edges_tex, texcoord.xy, vec2(0, 1)).r; - factor.x -= rounding.y * smaa_sample_level_zero_offset(edges_tex, texcoord.zw, vec2(1, 1)).r; - factor.y -= rounding.x * smaa_sample_level_zero_offset(edges_tex, texcoord.xy, vec2(0, -2)).r; - factor.y -= rounding.y * smaa_sample_level_zero_offset(edges_tex, texcoord.zw, vec2(1, -2)).r; - - weights *= saturate(factor); - return weights; -} - -vec2 smaa_detect_vertical_corner_pattern(vec2 weights, vec4 texcoord, vec2 d) { - vec2 left_right = step(d.xy, d.yx); - vec2 rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * left_right; - - rounding /= left_right.x + left_right.y; - - vec2 factor = vec2(1.0, 1.0); - factor.x -= rounding.x * smaa_sample_level_zero_offset(edges_tex, texcoord.xy, vec2( 1, 0)).g; - factor.x -= rounding.y * smaa_sample_level_zero_offset(edges_tex, texcoord.zw, vec2( 1, 1)).g; - factor.y -= rounding.x * smaa_sample_level_zero_offset(edges_tex, texcoord.xy, vec2(-2, 0)).g; - factor.y -= rounding.y * smaa_sample_level_zero_offset(edges_tex, texcoord.zw, vec2(-2, 1)).g; - - weights *= saturate(factor); - return weights; -} - - -vec4 smaa_blending_weight_calculation_ps(vec2 texcoord, vec2 pixcoord, vec4 subsample_indices) { - vec4 weights = vec4(0.0, 0.0, 0.0, 0.0); - - vec2 e = textureLod_a(edges_tex, texcoord, 0.0).rg; - - if (e.g > 0.0) { - weights.rg = smaa_calculate_diag_weights(texcoord, e, subsample_indices); - if (weights.r == -weights.g) { - vec2 d; - - vec3 coords; - coords.x = smaa_search_x_left(offset0.xy, offset2.x); - coords.y = offset1.y; - d.x = coords.x; - - float e1 = textureLod_a(edges_tex, coords.xy, 0.0).r; - - coords.z = smaa_search_x_right(offset0.zw, offset2.y); - d.y = coords.z; - - d = abs(round(mad(screen_size.xx, d, -pixcoord.xx))); - - vec2 sqrt_d = sqrt(d); - - float e2 = smaa_sample_level_zero_offset(edges_tex, coords.zy, vec2(1, 0)).r; - - weights.rg = smaa_area(sqrt_d, e1, e2, subsample_indices.y); - - coords.y = texcoord.y; - weights.rg = smaa_detect_horizontal_corner_pattern(weights.rg, coords.xyzy, d); - } - else { - e.r = 0.0; - } - } - - if (e.r > 0.0) { - vec2 d; - - vec3 coords; - coords.y = smaa_search_y_up(offset1.xy, offset2.z); - coords.x = offset0.x; - d.x = coords.y; - - float e1 = textureLod_a(edges_tex, coords.xy, 0.0).g; - - coords.z = smaa_search_y_down(offset1.zw, offset2.w); - d.y = coords.z; - - d = abs(round(mad(screen_size.yy, d, -pixcoord.yy))); - - vec2 sqrt_d = sqrt(d); - - float e2 = smaa_sample_level_zero_offset(edges_tex, coords.xz, vec2(0, 1)).g; - - weights.ba = smaa_area(sqrt_d, e1, e2, subsample_indices.x); - - coords.x = texcoord.x; - weights.ba = smaa_detect_vertical_corner_pattern(weights.ba, coords.xyxz, d); - } - - return weights; -} - -void main() { - frag_color = smaa_blending_weight_calculation_ps(tex_coord, pixcoord, vec4(0.0, 0.0, 0.0, 0.0)); -} diff --git a/base/shaders/smaa_blend_weight.kong b/base/shaders/smaa_blend_weight.kong new file mode 100644 index 00000000..0b13716c --- /dev/null +++ b/base/shaders/smaa_blend_weight.kong @@ -0,0 +1,375 @@ + +#[set(everything)] +const edges_tex: tex2d; + +#[set(everything)] +const edges_tex_sampler: sampler; + +#[set(everything)] +const area_tex: tex2d; + +#[set(everything)] +const area_tex_sampler: sampler; + +#[set(everything)] +const search_tex: tex2d; + +#[set(everything)] +const search_tex_sampler: sampler; + +#[set(everything)] +const constants: { + screen_size: float2; + screen_size_inv: float2; +}; + +struct vert_in { + pos: float2; +} + +struct vert_out { + pos: float4; + tex: float2; + pixcoord: float2; + offset0: float4; + offset1: float4; + offset2: float4; +} + +fun smaa_blend_weight_vert(input: vert_in): vert_out { + var output: vert_out; + output.tex = input.pos.xy * 0.5 + 0.5; + output.pixcoord = output.tex * constants.screen_size; + output.pos = float4(input.pos.xy, 0.0, 1.0); + + var xyxy: float4 = float4(constants.screen_size_inv.xy, constants.screen_size_inv.xy); + output.offset0 = xyxy * float4(-0.25, -0.125, 1.25, -0.125) + float4(output.tex.xy, output.tex.xy); + output.offset1 = xyxy * float4(-0.125, -0.25, -0.125, 1.25) + float4(output.tex.xy, output.tex.xy); + var xxyy: float4 = float4(constants.screen_size_inv.xx, constants.screen_size_inv.yy); + output.offset2 = xxyy * float4(-32.0, 32.0, -32.0, 32.0) + float4(output.offset0.xz, output.offset1.yw); + return output; +} + +fun abs1(a: float): float { + if (a < 0) { + return -a; + } + return a; +} + +fun sample_edges_tex(coord: float2): float4 { + coord.y = 1.0 - coord.y; + return sample_lod(edges_tex, edges_tex_sampler, coord, 0.0); +} + +fun smaa_sample_level_zero_offset(coord: float2, offset: float2): float4 { + return sample_edges_tex(coord + offset * constants.screen_size_inv.xy); +} + +fun smaa_decode_diag_bilinear_access2(e: float2): float2 { + e.r = e.r * abs1(5.0 * e.r - 5.0 * 0.75); + // return floor(e + 0.5); + e.x = floor(e.x + 0.5); + e.y = floor(e.y + 0.5); + return e; +} + +fun smaa_decode_diag_bilinear_access4(e: float4): float4 { + // e.rb = e.rb * abs(5.0 * e.rb - 5.0 * 0.75); + e.r = e.r * abs1(5.0 * e.r - 5.0 * 0.75); + e.b = e.b * abs1(5.0 * e.b - 5.0 * 0.75); + // return floor(e + 0.5); + e.x = floor(e.x + 0.5); + e.y = floor(e.y + 0.5); + e.z = floor(e.z + 0.5); + e.w = floor(e.w + 0.5); + return e; +} + +fun smaa_search_diag1(texcoord: float2, dir: float2): float3 { + var coord: float4 = float4(texcoord, -1.0, 1.0); + var t: float3 = float3(constants.screen_size_inv.xy, 1.0); + var cdw_end: float2; + while (coord.z < 7.0 && coord.w > 0.9) { + coord.xyz = t * float3(dir, 1.0) + coord.xyz; + cdw_end = sample_edges_tex(coord.xy).rg; + coord.w = dot(cdw_end /*e*/, float2(0.5, 0.5)); + } + return float3(coord.zw, cdw_end.y); +} + +fun smaa_search_diag2(texcoord: float2, dir: float2): float3 { + var coord: float4 = float4(texcoord, -1.0, 1.0); + coord.x += 0.25 * constants.screen_size_inv.x; + var t: float3 = float3(constants.screen_size_inv.xy, 1.0); + var cw: float = coord.w; + var cdw_end: float2; + while (coord.z < 7.0 && cw > 0.9) { + coord.xyz = t * float3(dir, 1.0) + coord.xyz; + cdw_end = sample_edges_tex(coord.xy).rg; + cdw_end = smaa_decode_diag_bilinear_access2(cdw_end); + cw = dot(cdw_end, float2(0.5, 0.5)); + } + coord.w = cw; + return float3(coord.zw, cdw_end.y); +} + +fun smaa_area_diag(dist: float2, e: float2, offset: float): float2 { + var texcoord: float2 = float2(20, 20) * e + dist; + texcoord = float2(1.0 / 160.0, 1.0 / 560.0) * texcoord + 0.5 * float2(1.0 / 160.0, 1.0 / 560.0); + texcoord.x += 0.5; + texcoord.y += (1.0 / 7.0) * offset; + return sample_lod(area_tex, area_tex_sampler, texcoord, 0.0).rg; +} + +fun smaa_calculate_diag_weights(texcoord: float2, e: float2): float2 { + var weights: float2 = float2(0.0, 0.0); + var d: float4; + if (e.r > 0.0) { + var r: float3 = smaa_search_diag1(texcoord, float2(-1.0, 1.0)); + d.xz = r.xy; + var dadd: float; + if (r.z > 0.9) { + dadd = 1.0; + } + else { + dadd = 0.0; + } + d.x += dadd; + } + else { + d.xz = float2(0.0, 0.0); + } + d.yw = smaa_search_diag1(texcoord, float2(1.0, -1.0)).xy; + + if (d.x + d.y > 2.0) { + var xyxy: float4 = float4(constants.screen_size_inv.xy, constants.screen_size_inv.xy); + var coords: float4 = float4(-d.x + 0.25, d.x, d.y, -d.y - 0.25) * xyxy + float4(texcoord.xy, texcoord.xy); + var c: float4; + + c.xy = smaa_sample_level_zero_offset(coords.xy, float2(-1, 0)).rg; + c.zw = smaa_sample_level_zero_offset(coords.zw, float2( 1, 0)).rg; + c.yxwz = smaa_decode_diag_bilinear_access4(c.xyzw); + + var cc: float2 = float2(2.0, 2.0) * c.xz + c.yw; + var a1condx: float = step(0.9, d.z); + var a1condy: float = step(0.9, d.w); + if (a1condx == 1.0) { + cc.x = 0.0; + } + if (a1condy == 1.0) { + cc.y = 0.0; + } + weights += smaa_area_diag(d.xy, cc, 0.0); + } + + d.xz = smaa_search_diag2(texcoord, float2(-1.0, -1.0)).xy; + if (smaa_sample_level_zero_offset(texcoord, float2(1, 0)).r > 0.0) { + var r: float3 = smaa_search_diag2(texcoord, float2(1.0, 1.0)); + d.yw = r.xy; + var dadd: float; + if (r.z > 0.9) { + dadd = 1.0; + } + else { + dadd = 0.0; + } + d.y += dadd; + } + else { + d.yw = float2(0.0, 0.0); + } + + if (d.x + d.y > 2.0) { + var xyxy: float4 = float4(constants.screen_size_inv.xy, constants.screen_size_inv.xy); + var coords: float4 = float4(-d.x, -d.x, d.y, d.y) * xyxy + float4(texcoord.xy, texcoord.xy); + var c: float4; + c.x = smaa_sample_level_zero_offset(coords.xy, float2(-1, 0)).g; + c.y = smaa_sample_level_zero_offset(coords.xy, float2( 0, -1)).r; + c.zw = smaa_sample_level_zero_offset(coords.zw, float2( 1, 0)).gr; + var cc: float2 = float2(2.0, 2.0) * c.xz + c.yw; + + var a1condx: float = step(0.9, d.z); + var a1condy: float = step(0.9, d.w); + if (a1condx == 1.0) { + cc.x = 0.0; + } + if (a1condy == 1.0) { + cc.y = 0.0; + } + weights += smaa_area_diag(d.xy, cc, 0.0).gr; + } + return weights; +} + +fun smaa_search_length(e: float2, offset: float): float { + var scale: float2 = float2(66.0, 33.0) * float2(0.5, -1.0); + var bias: float2 = float2(66.0, 33.0) * float2(offset, 1.0); + scale += float2(-1.0, 1.0); + bias += float2( 0.5, -0.5); + scale *= 1.0 / float2(64.0, 16.0); + bias *= 1.0 / float2(64.0, 16.0); + var coord: float2 = scale * e + bias; + return sample_lod(search_tex, search_tex_sampler, coord, 0.0).r; +} + +fun smaa_search_x_left(texcoord: float2, end: float): float { + var e: float2 = float2(0.0, 1.0); + while (texcoord.x > end && e.g > 0.8281 && e.r == 0.0) { + e = sample_edges_tex(texcoord).rg; + texcoord = -float2(2.0, 0.0) * constants.screen_size_inv.xy + texcoord; + } + var offset: float = -(255.0 / 127.0) * smaa_search_length(e, 0.0) + 3.25; + return constants.screen_size_inv.x * offset + texcoord.x; +} + +fun smaa_search_x_right(texcoord: float2, end: float): float { + var e: float2 = float2(0.0, 1.0); + while (texcoord.x < end && e.g > 0.8281 && e.r == 0.0) { + e = sample_edges_tex(texcoord).rg; + texcoord = float2(2.0, 0.0) * constants.screen_size_inv.xy + texcoord; + } + var offset: float = -(255.0 / 127.0) * smaa_search_length(e, 0.5) + 3.25; + return -constants.screen_size_inv.x * offset + texcoord.x; +} + +fun smaa_search_y_up(texcoord: float2, end: float): float { + var e: float2 = float2(1.0, 0.0); + while (texcoord.y > end && e.r > 0.8281 && e.g == 0.0) { + e = sample_edges_tex(texcoord).rg; + texcoord = -float2(0.0, 2.0) * constants.screen_size_inv.xy + texcoord; + } + var offset: float = -(255.0 / 127.0) * smaa_search_length(e.gr, 0.0) + 3.25; + return constants.screen_size_inv.y * offset + texcoord.y; +} + +fun smaa_search_y_down(texcoord: float2, end: float): float { + var e: float2 = float2(1.0, 0.0); + while (texcoord.y < end && e.r > 0.8281 && e.g == 0.0) { + e = sample_edges_tex(texcoord).rg; + texcoord = float2(0.0, 2.0) * constants.screen_size_inv.xy + texcoord; + } + var offset: float = -(255.0 / 127.0) * smaa_search_length(e.gr, 0.5) + 3.25; + return -constants.screen_size_inv.y * offset + texcoord.y; +} + +fun smaa_area(dist: float2, e1: float, e2: float): float2 { + // var f2: float2 = floor(4.0 * float2(e1, e2)); + var f2: float2 = float2(floor(4.0 * e1), floor(4.0 * e2)); + var texcoord: float2 = float2(16, 16) * (f2 + 0.5) + dist; + texcoord = float2(1.0 / 160.0, 1.0 / 560.0) * texcoord + 0.5 * float2(1.0 / 160.0, 1.0 / 560.0); + texcoord.y = (1.0 / 7.0) + texcoord.y; + return sample_lod(area_tex, area_tex_sampler, texcoord, 0.0).rg; +} + +fun smaa_detect_horizontal_corner_pattern(weights: float2, texcoord: float4, d: float2): float2 { + // var left_right: float2 = step(d.xy, d.yx); + var left_right: float2 = float2(step(d.x, d.y), step(d.y, d.x)); + + var rounding: float2 = (1.0 - 0.25) * left_right; + rounding /= left_right.x + left_right.y; + var factor: float2 = float2(1.0, 1.0); + factor.x -= rounding.x * smaa_sample_level_zero_offset(texcoord.xy, float2(0, 1)).r; + factor.x -= rounding.y * smaa_sample_level_zero_offset(texcoord.zw, float2(1, 1)).r; + factor.y -= rounding.x * smaa_sample_level_zero_offset(texcoord.xy, float2(0, -2)).r; + factor.y -= rounding.y * smaa_sample_level_zero_offset(texcoord.zw, float2(1, -2)).r; + // weights *= saturate(factor); + weights.x *= saturate(factor.x); + weights.y *= saturate(factor.y); + return weights; +} + +fun smaa_detect_vertical_corner_pattern(weights: float2, texcoord: float4, d: float2): float2 { + // var left_right: float2 = step(d.xy, d.yx); + var left_right: float2 = float2(step(d.x, d.y), step(d.y, d.x)); + + var rounding: float2 = (1.0 - 0.25) * left_right; + rounding /= left_right.x + left_right.y; + var factor: float2 = float2(1.0, 1.0); + factor.x -= rounding.x * smaa_sample_level_zero_offset(texcoord.xy, float2( 1, 0)).g; + factor.x -= rounding.y * smaa_sample_level_zero_offset(texcoord.zw, float2( 1, 1)).g; + factor.y -= rounding.x * smaa_sample_level_zero_offset(texcoord.xy, float2(-2, 0)).g; + factor.y -= rounding.y * smaa_sample_level_zero_offset(texcoord.zw, float2(-2, 1)).g; + weights.x *= saturate(factor.x); + weights.y *= saturate(factor.y); + return weights; +} + +fun smaa_blend_weight_frag(input: vert_out): float4 { + var weights: float4 = float4(0.0, 0.0, 0.0, 0.0); + var e: float2 = sample_edges_tex(input.tex).rg; + + if (e.g > 0.0) { + weights.rg = smaa_calculate_diag_weights(input.tex, e); + if (weights.r == -weights.g) { + var d: float2; + + var coords: float3; + coords.x = smaa_search_x_left(input.offset0.xy, input.offset2.x); + coords.y = input.offset1.y; + d.x = coords.x; + + var e1: float = sample_edges_tex(coords.xy).r; + + coords.z = smaa_search_x_right(input.offset0.zw, input.offset2.y); + d.y = coords.z; + + var f2: float2 = constants.screen_size.xx * d - input.pixcoord.xx; + // d = abs(floor(f2) + 0.5); + d.x = abs1(floor(f2.x) + 0.5); + d.y = abs1(floor(f2.y) + 0.5); + + // var sqrt_d: float2 = sqrt(d); + var sqrt_d: float2 = float2(sqrt(d.x), sqrt(d.y)); + + var e2: float = smaa_sample_level_zero_offset(coords.zy, float2(1, 0)).r; + weights.rg = smaa_area(sqrt_d, e1, e2); + coords.y = input.tex.y; + + var xyzy: float4 = float4(coords.xy, coords.zy); + weights.rg = smaa_detect_horizontal_corner_pattern(weights.rg, xyzy, d); + } + else { + e.r = 0.0; + } + } + + if (e.r > 0.0) { + var d: float2; + + var coords: float3; + coords.y = smaa_search_y_up(input.offset1.xy, input.offset2.z); + coords.x = input.offset0.x; + d.x = coords.y; + + var e1: float = sample_edges_tex(coords.xy).g; + + coords.z = smaa_search_y_down(input.offset1.zw, input.offset2.w); + d.y = coords.z; + + var f2: float2 = constants.screen_size.yy * d - input.pixcoord.yy; + // d = abs(floor(f2) + 0.5); + d.x = abs1(floor(f2.x) + 0.5); + d.y = abs1(floor(f2.y) + 0.5); + + // var sqrt_d: float2 = sqrt(d); + var sqrt_d: float2 = float2(sqrt(d.x), sqrt(d.y)); + + var e2: float = smaa_sample_level_zero_offset(coords.xz, float2(0, 1)).g; + // weights.ba = smaa_area(sqrt_d, e1, e2); + weights.ba = smaa_area(float2(0,0), 0, 0); + coords.x = input.tex.x; + + var xyxz: float4 = float4(coords.xy, coords.xz); + // weights.ba = smaa_detect_vertical_corner_pattern(weights.ba, xyxz, d); + weights.ba = smaa_detect_vertical_corner_pattern(float2(1,1), float4(1,1,1,1), float2(1,1)); + } + + return weights; +} + +#[pipe] +struct pipe { + vertex = smaa_blend_weight_vert; + fragment = smaa_blend_weight_frag; +} diff --git a/base/shaders/smaa_blend_weight.vert.glsl b/base/shaders/smaa_blend_weight.vert.glsl deleted file mode 100644 index a666570e..00000000 --- a/base/shaders/smaa_blend_weight.vert.glsl +++ /dev/null @@ -1,29 +0,0 @@ -#version 450 - -uniform vec2 screen_size; -uniform vec2 screen_size_inv; - -in vec2 pos; -out vec2 tex_coord; -out vec2 pixcoord; -out vec4 offset0; -out vec4 offset1; -out vec4 offset2; - -const int SMAA_MAX_SEARCH_STEPS = 16; - -void main() { - const vec2 madd = vec2(0.5, 0.5); - tex_coord = pos.xy * madd + madd; - - pixcoord = tex_coord * screen_size; - - offset0 = screen_size_inv.xyxy * vec4(-0.25, -0.125, 1.25, -0.125) + tex_coord.xyxy; - offset1 = screen_size_inv.xyxy * vec4(-0.125, -0.25, -0.125, 1.25) + tex_coord.xyxy; - - offset2 = screen_size_inv.xxyy * - (vec4(-2.0, 2.0, -2.0, 2.0) * float(SMAA_MAX_SEARCH_STEPS)) + - vec4(offset0.xz, offset1.yw); - - gl_Position = vec4(pos.xy, 0.0, 1.0); -} diff --git a/base/shaders/taa_pass.kong b/base/shaders/taa_pass.kong index d796d92e..988382b7 100644 --- a/base/shaders/taa_pass.kong +++ b/base/shaders/taa_pass.kong @@ -31,7 +31,7 @@ fun taa_pass_vert(input: vert_in): vert_out { fun taa_pass_frag(input: vert_out): float4 { var current: float4 = sample_lod(tex, tex_sampler, input.tex, 0.0); var previous: float4 = sample_lod(tex2, tex2_sampler, input.tex, 0.0); - return float4(lerp(current.rgb, previous.rgb, 0.5), 1.0); + return float4(lerp(current.rgb, previous.rgb, 0.833), 1.0); } #[pipe]