Skip to content

Commit

Permalink
Use linear viewZ for accurate and controllable weighted filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralhalo committed Jan 16, 2022
1 parent 1d9b63b commit 099c4b4
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 12 deletions.
43 changes: 38 additions & 5 deletions assets/lumi/pipeline/color.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,19 @@
{name: "TEXTURE_MIN_FILTER", val: "NEAREST"}, {name: "TEXTURE_MAG_FILTER", val: "NEAREST"},
{name: "TEXTURE_WRAP_S", val: "CLAMP_TO_EDGE"}, {name: "TEXTURE_WRAP_T", val: "CLAMP_TO_EDGE"}
]
},
},
{
name: "view_z",
lod: 0,
internalFormat: "R32F",
pixelFormat: "RED",
pixelDataType: "FLOAT",
target: "TEXTURE_2D",
texParams: [
{name: "TEXTURE_MIN_FILTER", val: "NEAREST"}, {name: "TEXTURE_MAG_FILTER", val: "NEAREST"},
{name: "TEXTURE_WRAP_S", val: "CLAMP_TO_EDGE"}, {name: "TEXTURE_WRAP_T", val: "CLAMP_TO_EDGE"},
]
},
],

programs: [
Expand Down Expand Up @@ -137,16 +149,24 @@
]
},
{
name: "ao2",
name: "ao_final",
vertexSource: "lumi:shaders/pass/basic.vert",
fragmentSource: "lumi:shaders/pass/ao2.frag",
samplers: [
"u_vanilla_depth",
"u_view_z",
"u_gbuffer_main_etc_copy",
"u_gbuffer_lightnormal",
"u_ao",
]
},
{
name: "depth_process",
vertexSource: "lumi:shaders/pass/basic.vert",
fragmentSource: "lumi:shaders/pass/depth.frag",
samplers: [
"u_vanilla_depth",
]
},
],

framebuffers: [
Expand Down Expand Up @@ -174,10 +194,23 @@
name: "ao_final",
colorAttachments: [{image: "gbuffer_main_etc", layer: 0}]
},
{
name: "depth_process",
colorAttachments: [{image: "view_z"}]
},
],

fabulous: {
passes: [
{
name: "depth_process",
program: "depth_process",
framebuffer: "depth_process",
samplerImages: [
"vanilla_depth",
],
toggleConfig: "ss_ao",
},
{
name: "ao",
program: "ao",
Expand All @@ -200,10 +233,10 @@
},
{
name: "ao_final",
program: "ao2",
program: "ao_final",
framebuffer: "ao_final",
samplerImages: [
"vanilla_depth",
"view_z",
"gbuffer_main_etc_copy",
"gbuffer_lightnormal",
"ao_result",
Expand Down
2 changes: 1 addition & 1 deletion assets/lumi/pipeline/layers.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
{name: "TEXTURE_MIN_FILTER", val: "NEAREST"}, {name: "TEXTURE_MAG_FILTER", val: "NEAREST"},
{name: "TEXTURE_WRAP_S", val: "CLAMP_TO_EDGE"}, {name: "TEXTURE_WRAP_T", val: "CLAMP_TO_EDGE"}
]
},
},
{
name: "gbuffer_main_etc",
lod: 0,
Expand Down
2 changes: 2 additions & 0 deletions assets/lumi/shaders/pass/ao.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ vec3 getViewPos(vec2 texcoord, in sampler2D target)

void main()
{
/* NOTE: using reconstructed normals doesn't really help reduce artifacts.
and as minecraft is blocky the interpolated normals should be accurate anyway. */
vec3 viewPos = getViewPos(v_texcoord, u_vanilla_depth);
vec3 viewNormal = frx_normalModelMatrix * normalize(texture(u_gbuffer_lightnormal, vec3(v_texcoord, ID_SOLID_NORM)).xyz);

Expand Down
14 changes: 8 additions & 6 deletions assets/lumi/shaders/pass/ao2.frag
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
lumi:shaders/pass/ao2.frag
******************************************************/

uniform sampler2D u_vanilla_depth;
uniform sampler2D u_view_z;
uniform sampler2D u_gbuffer_main_etc_copy;
uniform sampler2DArray u_gbuffer_lightnormal;
uniform sampler2D u_ao;
Expand All @@ -20,19 +20,21 @@ void main()
float totalAo = 0.0;
float total = 0.0;
vec3 normal = texture(u_gbuffer_lightnormal, vec3(v_texcoord, ID_SOLID_NORM)).xyz;
float depth = texture(u_vanilla_depth, v_texcoord).r;
float fragZ = texture(u_view_z, v_texcoord).r;

const float maxdZ = 1. / 8.; // height of snow and button to represent smallest possible separation of surfaces

// this blurring costs about 0.1 ms in my hardware, helps unreliable TAA
for (float x = -2.0; x < 2.2; x += 2.0) {
for (float y = -2.0; y < 2.2; y += 2.0) {
vec2 sampleUV = v_texcoord + vec2(x, y) * v_invSize;
vec3 sampleNormal = texture(u_gbuffer_lightnormal, vec3(sampleUV, ID_SOLID_NORM)).xyz;
float sampleDepth = texture(u_vanilla_depth, sampleUV).r;
float sampleZ = texture(u_view_z, sampleUV).r;

float NdN = max(0.0, dot(sampleNormal, normal));
float dW = l2_clampScale(0.005, 0.0, abs(sampleDepth - depth));
totalAo += texture(u_ao, sampleUV).r * NdN * dW;
total += NdN * dW;
float dZ = l2_clampScale(maxdZ, 0.0, abs(sampleZ - fragZ));
totalAo += texture(u_ao, sampleUV).r * NdN * dZ;
total += NdN * dZ;
}
}

Expand Down
16 changes: 16 additions & 0 deletions assets/lumi/shaders/pass/depth.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include lumi:shaders/pass/header.glsl

/******************************************************
lumi:shaders/pass/depth.frag
******************************************************/

uniform sampler2D u_vanilla_depth;

out float viewZ;

void main()
{
float depth = texture(u_vanilla_depth, v_texcoord).r;
vec4 temp = frx_inverseProjectionMatrix * vec4(v_texcoord * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
viewZ = -temp.z / temp.w;
}

0 comments on commit 099c4b4

Please sign in to comment.