Converted rendering engine to reverse-z

This commit is contained in:
TriVoxel
2025-04-29 14:33:13 -06:00
parent 0cdb90bdb8
commit 5b5de92890
16 changed files with 151 additions and 124 deletions

View File

@ -17,7 +17,8 @@ uniform float envmapStrength;
#include "std/environment_sample.glsl"
uniform sampler2D tex; // Environment map
uniform sampler2D gbufferD; // Depth buffer
//uniform sampler2D depthtex; // Full Depth buffer
uniform sampler2D gbufferD; // Cheap Depth buffer
uniform sampler2D gbuffer0; // Normal, roughness
uniform sampler2D gbuffer1; // Base color, spec
uniform mat4 P; // Projection matrix
@ -25,15 +26,10 @@ uniform mat3 V3; // View matrix
uniform vec2 cameraProj; // Camera projection params
uniform vec2 invScreenSize; // (1.0/width, 1.0/height)
const float ssrPrecision = 100.0; // 0.0 - 100.0 (user slider control)
const float ssrPrecision = 0.0; // 0.0 - 100.0 (user slider control)
//const float rayThickness = 0.1; // TODO: Adds some thickness to prevent gaps
uniform int coneTraceMode = 2; // 0 = no weighting, 1 = light, 2 = strong
const int coneTraceTapCount = 18; // Number of taps (higher = more precision)
#ifdef _CPostprocess
uniform vec3 PPComp9;
uniform vec3 PPComp10;
#endif
uniform int ssrConetraceMode = 2; // 0 = no weighting, 1 = light, 2 = strong
const int ssrConetraceTaps = 18; // Number of taps (higher = more precision)
in vec3 viewRay;
in vec2 texCoord;
@ -54,17 +50,21 @@ int dynamicMaxSteps() {
}
vec2 getProjectedCoord(const vec3 hit) {
vec4 projectedCoord = P * vec4(hit, 1.0);
projectedCoord.xy /= projectedCoord.w;
projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
projectedCoord.xy = clamp(projectedCoord.xy, 0.0, 1.0);
projectedCoord.xy += invScreenSize * 0.5; // half-pixel offset
return projectedCoord.xy;
vec4 clip = P * vec4(hit, 1.0);
vec2 uv = clip.xy / clip.w;
uv = uv * 0.5 + 0.5;
#ifdef _InvY
uv.y = 1.0 - uv.y;
#endif
uv = clamp(uv, 0.0, 1.0);
uv += invScreenSize * 0.5; // half-pixel offset
return uv;
}
float getDeltaDepth(const vec3 hit) {
vec2 screenUV = getProjectedCoord(hit);
float sampledDepth = textureLod(gbufferD, screenUV, 0.0).r;
float raw = textureLod(gbufferD, screenUV, 0.0).r;
float sampledDepth = raw * 2.0 - 1.0;
float linearSampledDepth = linearize(sampledDepth, cameraProj);
return linearSampledDepth - hit.z;
@ -129,16 +129,16 @@ vec3 coneTraceApprox(vec3 reflDir, float roughness) {
float coneSpread = roughness * 0.5; // widen based on roughness
for (int i = 0; i < coneTraceTapCount; ++i) {
float angle = float(i) / float(coneTraceTapCount) * 6.2831853;
for (int i = 0; i < ssrConetraceTaps; ++i) {
float angle = float(i) / float(ssrConetraceTaps) * 6.2831853;
vec2 offset = rot * vec2(cos(angle), sin(angle)) * coneSpread;
vec3 sampleDir = normalize(reflDir + vec3(offset, 0.0));
float weight = 1.0;
if (coneTraceMode == 1) {
if (ssrConetraceMode == 1) {
weight = pow(max(dot(reflDir, sampleDir), 0.0), 2.0); // Light cosine lobe
}
else if (coneTraceMode == 2) {
else if (ssrConetraceMode == 2) {
weight = pow(max(dot(reflDir, sampleDir), 0.0), 8.0); // Strong GGX lobe
}
@ -175,8 +175,11 @@ void main() {
float spec = fract(textureLod(gbuffer1, texCoord, 0.0).a);
if (spec == 0.0) { fragColor.rgb = vec3(0.0); return; }
float d = textureLod(gbufferD, texCoord, 0.0).r * 2.0 - 1.0;
if (d == 1.0) { fragColor.rgb = vec3(0.0); return; }
// sample raw depth, bail if empty
float dRaw = textureLod(gbufferD, texCoord, 0.0).r;
if (dRaw == 0.0) { fragColor.rgb = vec3(0.0); return; }
// convert to NDC z before reconstructing
float d = dRaw * 2.0 - 1.0;
vec3 n = decode_oct(g0.rg);
vec3 viewNormal = V3 * n;

View File

@ -4,6 +4,7 @@
"name": "ssr_pass",
"depth_write": false,
"compare_mode": "always",
"blend_mode": "replace",
"cull_mode": "none",
"links": [
{
@ -23,14 +24,12 @@
"link": "_cameraPlaneProj"
},
{
"name": "PPComp9",
"link": "_PPComp9",
"ifdef": ["_CPostprocess"]
"name": "coneTraceMode",
"link": "_coneTraceMode"
},
{
"name": "PPComp10",
"link": "_PPComp10",
"ifdef": ["_CPostprocess"]
"name": "coneTraceTapCount",
"link": "_coneTraceTapCount"
}
],
"texture_params": [],