Skip to content

Commit

Permalink
Merge pull request #3039 from e2002e/gi_clipmaps
Browse files Browse the repository at this point in the history
Gi clipmaps
  • Loading branch information
luboslenco authored Sep 3, 2024
2 parents c61cc91 + 8a00e1c commit 61c4c86
Show file tree
Hide file tree
Showing 29 changed files with 934 additions and 538 deletions.
55 changes: 24 additions & 31 deletions Shaders/deferred_light/deferred_light.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
#ifdef _Irr
#include "std/shirr.glsl"
#endif
#ifdef _VoxelShadow
#include "std/conetrace.glsl"
#endif
#ifdef _SSS
#include "std/sss.glsl"
#endif
Expand All @@ -37,9 +34,7 @@ uniform sampler2D voxels_specular;
uniform sampler2D voxels_ao;
#endif
#ifdef _VoxelShadow
uniform float clipmaps[voxelgiClipmapCount * 10];
uniform sampler3D voxels;
uniform sampler3D voxelsSDF;
uniform sampler2D voxels_shadows;
#endif

uniform float envmapStrength;
Expand Down Expand Up @@ -130,7 +125,7 @@ uniform vec2 cameraPlane;
#ifndef _SingleAtlas
//!uniform sampler2DShadow shadowMapAtlasPoint;
#endif
//!uniform vec4 pointLightDataArray[4];
//!uniform vec4 pointLightDataArray[maxLightsCluster * 6];
#else
//!uniform samplerCubeShadow shadowMapPoint[4];
#endif
Expand All @@ -143,7 +138,7 @@ uniform vec2 cameraPlane;
#else
//!uniform sampler2DShadow shadowMapSpot[4];
#endif
//!uniform mat4 LWVPSpotArray[4];
//!uniform mat4 LWVPSpotArray[maxLightsCluster];
#endif
#endif
#endif
Expand Down Expand Up @@ -277,13 +272,13 @@ void main() {
envl.rgb *= envmapStrength * occspec.x;

#ifdef _VoxelGI
fragColor.rgb = textureLod(voxels_diffuse, texCoord, 0.0).rgb * voxelgiDiff * albedo;
fragColor.rgb = textureLod(voxels_diffuse, texCoord, 0.0).rgb * albedo * voxelgiDiff;
if(roughness < 1.0 && occspec.y > 0.0)
fragColor.rgb += textureLod(voxels_specular, texCoord, 0.0).rgb * voxelgiRefl * occspec.y;
fragColor.rgb += textureLod(voxels_specular, texCoord, 0.0).rgb * occspec.y * voxelgiRefl;
#endif

#ifdef _VoxelAOvar
envl.rgb *= 1.0 - textureLod(voxels_ao, texCoord, 0.0).r;
envl.rgb *= textureLod(voxels_ao, texCoord, 0.0).r;
#endif

#ifndef _VoxelGI
Expand Down Expand Up @@ -353,23 +348,25 @@ void main() {
);
#else
vec4 lPos = LWVP * vec4(p + n * shadowsBias * 100, 1.0);
if (lPos.w > 0.0) svisibility = shadowTest(
#ifdef _ShadowMapAtlas
#ifndef _SingleAtlas
shadowMapAtlasSun
if (lPos.w > 0.0) {
svisibility = shadowTest(
#ifdef _ShadowMapAtlas
#ifndef _SingleAtlas
shadowMapAtlasSun
#else
shadowMapAtlas
#endif
#else
shadowMapAtlas
shadowMap
#endif
#else
shadowMap
#endif
, lPos.xyz / lPos.w, shadowsBias
);
, lPos.xyz / lPos.w, shadowsBias
);
}
#endif
#endif

#ifdef _VoxelShadow
svisibility *= 1.0 - traceShadow(p, n, voxels, voxelsSDF, sunDir, clipmaps);
svisibility *= textureLod(voxels_shadows, texCoord, 0.0).r * voxelgiShad;
#endif

#ifdef _SSRS
Expand All @@ -388,7 +385,7 @@ void main() {
svisibility *= clamp(sdotNL + 2.0 * occspec.x * occspec.x - 1.0, 0.0, 1.0);
#endif

fragColor.rgb += sdirect * svisibility * sunCol;
fragColor.rgb += sdirect * sunCol * svisibility;

// #ifdef _Hair // Aniso
// if (matid == 2) {
Expand Down Expand Up @@ -417,7 +414,7 @@ void main() {
#else
shadowMap
#endif
);
);//TODO implement transparent shadowmaps into the SSSSTransmittance()
}
#endif

Expand All @@ -434,9 +431,7 @@ void main() {
, true, spotData.x, spotData.y, spotDir, spotData.zw, spotRight
#endif
#ifdef _VoxelShadow
, voxels
, voxelsSDF
, clipmaps
, texCoord
#endif
#ifdef _MicroShadowing
, occspec.x
Expand All @@ -448,7 +443,7 @@ void main() {

#ifdef _Spot
#ifdef _SSS
if (matid == 2) fragColor.rgb += fragColor.rgb * SSSSTransmittance(LWVPSpot0, p, n, normalize(pointPos - p), lightPlane.y, shadowMapSpot[0]);
if (matid == 2) fragColor.rgb += fragColor.rgb * SSSSTransmittance(LWVPSpot0, p, n, normalize(pointPos - p), lightPlane.y, shadowMapSpot[0]);//TODO implement transparent shadowmaps into the SSSSTransmittance()
#endif
#endif

Expand Down Expand Up @@ -494,9 +489,7 @@ void main() {
, lightsArraySpot[li * 2 + 1].xyz // right
#endif
#ifdef _VoxelShadow
, voxels
, voxelsSDF
, clipmaps
, texCoord
#endif
#ifdef _MicroShadowing
, occspec.x
Expand Down
2 changes: 1 addition & 1 deletion Shaders/ssr_pass/ssr_pass.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void main() {

vec3 viewNormal = V3 * n;
vec3 viewPos = getPosView(viewRay, d, cameraProj);
vec3 reflected = reflect(normalize(viewPos), viewNormal);
vec3 reflected = reflect(viewPos, viewNormal);
hitCoord = viewPos;

#ifdef _CPostprocess
Expand Down
26 changes: 12 additions & 14 deletions Shaders/ssrefr_pass/ssrefr_pass.frag.glsl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//https://lettier.github.io/3d-game-shaders-for-beginners/screen-space-refraction.html
//Implemented by Yvain Douard.

#version 450

#include "compiled.inc"
Expand Down Expand Up @@ -35,7 +38,7 @@ vec2 getProjectedCoord(const vec3 hit) {
}

float getDeltaDepth(const vec3 hit) {
depth = textureLod(gbufferD1, getProjectedCoord(hit), 0.0).r * 2.0 - 1.0;
float depth = textureLod(gbufferD1, getProjectedCoord(hit), 0.0).r * 2.0 - 1.0;
vec3 viewPos = getPosView(viewRay, depth, cameraProj);
return viewPos.z - hit.z;
}
Expand All @@ -48,7 +51,7 @@ vec4 binarySearch(vec3 dir) {
ddepth = getDeltaDepth(hitCoord);
if (ddepth < 0.0) hitCoord += dir;
}
if (abs(ddepth) > ss_refractionSearchDist / 500) return vec4(0.0);
if (abs(ddepth) > ss_refractionSearchDist) return vec4(0.0);
return vec4(getProjectedCoord(hitCoord), 0.0, 1.0);
}

Expand All @@ -72,7 +75,11 @@ void main() {

float d = textureLod(gbufferD, texCoord, 0.0).r * 2.0 - 1.0;

if (d == 1.0 || ior == 1.0 || opac == 1.0) {
if (opac == 1.0) {
fragColor.rgb = textureLod(tex, texCoord, 0.0).rgb;
return;
}
if (d == 1.0 || d == 0.0 || ior == 1.0) {
fragColor.rgb = textureLod(tex1, texCoord, 0.0).rgb;
return;
}
Expand All @@ -85,22 +92,13 @@ void main() {

vec3 viewNormal = V3 * n;
vec3 viewPos = getPosView(viewRay, d, cameraProj);
vec3 refracted = refract(normalize(viewPos), viewNormal, 1.0 / ior);
vec3 refracted = refract(viewPos, viewNormal, 1.0 / ior);
hitCoord = viewPos;

vec3 dir = refracted * (1.0 - rand(texCoord) * ss_refractionJitter * roughness) * 2.0;

vec4 coords = rayCast(dir);
vec2 deltaCoords = abs(vec2(0.5, 0.5) - coords.xy);
float screenEdgeFactor = clamp(1.0 - (deltaCoords.x + deltaCoords.y), 0.0, 1.0);

float refractivity = 1.0;

float intensity = pow(refractivity, ss_refractionFalloffExp) * screenEdgeFactor * clamp((ss_refractionSearchDist - length(viewPos - hitCoord)) * (1.0 / ss_refractionSearchDist), 0.0, 1.0) * coords.w;

intensity = clamp(intensity, 0.0, 1.0);
vec3 refractionCol = textureLod(tex1, coords.xy, 0.0).rgb;
refractionCol = clamp(refractionCol, 0.0, 1.0);
vec3 color = textureLod(tex, texCoord.xy, 0.0).rgb;
fragColor.rgb = mix(refractionCol * intensity, color, opac);
fragColor.rgb = mix(refractionCol, color, opac);
}
77 changes: 41 additions & 36 deletions Shaders/std/conetrace.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ THE SOFTWARE.
// http://www.seas.upenn.edu/%7Epcozzi/OpenGLInsights/OpenGLInsights-SparseVoxelization.pdf
// https://research.nvidia.com/sites/default/files/publications/GIVoxels-pg2011-authors.pdf

const float MAX_DISTANCE = voxelgiRange * 100.0;
const float MAX_DISTANCE = voxelgiRange;

#ifdef _VoxelGI
uniform sampler3D dummy;
Expand Down Expand Up @@ -93,7 +93,7 @@ vec4 traceCone(const sampler3D voxels, const sampler3D voxelsSDF, const vec3 ori
float dist = voxelSize0;
float step_dist = dist;
vec3 samplePos;
vec3 start_pos = origin + n * voxelSize0;
vec3 start_pos = origin + n * voxelSize0 * voxelgiOffset;
int clipmap_index0 = 0;

vec3 aniso_direction = -dir;
Expand Down Expand Up @@ -157,42 +157,49 @@ vec4 traceDiffuse(const vec3 origin, const vec3 normal, const sampler3D voxels,
if (cosTheta <= 0)
continue;
int precomputed_direction = 6 + i;
amount += traceCone(voxels, dummy, origin, normal, coneDir, precomputed_direction, false, DIFFUSE_CONE_APERTURE, 1, clipmaps) * cosTheta;
amount += traceCone(voxels, dummy, origin, normal, coneDir, precomputed_direction, false, DIFFUSE_CONE_APERTURE, 1.0, clipmaps) * cosTheta;
sum += cosTheta;
}

amount /= sum;
amount.rgb = max(vec3(0.0), amount.rgb);
amount.a = clamp(amount.a, 0.0, 1.0);

return amount * voxelgiOcc;
}

vec4 traceSpecular(const vec3 origin, const vec3 normal, const sampler3D voxels, const sampler3D voxelsSDF, const vec3 viewDir, const float roughness, const float clipmaps[voxelgiClipmapCount * 10], const vec2 pixel) {
vec3 specularDir = reflect(-viewDir, normal);
vec3 P = origin;// + specularDir * (BayerMatrix8[int(pixel.x) % 8][int(pixel.y) % 8] - 0.5) * voxelgiStep;
vec3 specularDir = normalize(reflect(-viewDir, normal));
vec3 P = origin + specularDir * (BayerMatrix8[int(pixel.x) % 8][int(pixel.y) % 8] - 0.5) * voxelgiStep;
vec4 amount = traceCone(voxels, voxelsSDF, P, normal, specularDir, 0, true, roughness, voxelgiStep, clipmaps);

amount.rgb = max(vec3(0.0), amount.rgb);
amount.a = clamp(amount.a, 0.0, 1.0);

return amount * voxelgiOcc;
}

/*
vec3 traceRefraction(const vec3 origin, const vec3 normal, sampler3D voxels, sampler3D voxelsSDF, const vec3 viewDir, const float ior, const float roughness, const float clipmaps[voxelgiClipmapCount * 10]) {
vec4 traceRefraction(const vec3 origin, const vec3 normal, sampler3D voxels, sampler3D voxelsSDF, const vec3 viewDir, const float ior, const float roughness, const float clipmaps[voxelgiClipmapCount * 10], const vec2 pixel) {
const float transmittance = 1.0;
vec3 refractionDir = refract(viewDir, normal, 1.0 / ior);
return transmittance * traceCone(voxels, origin, normal, refractionDir, 0, roughness, voxelgiStep, clipmaps).xyz * voxelgiOcc;
vec3 P = origin + refractionDir * (BayerMatrix8[int(pixel.x) % 8][int(pixel.y) % 8] - 0.5) * voxelgiStep;
vec4 amount = transmittance * traceCone(voxels, voxelsSDF, P, normal, refractionDir, 0, true, roughness, voxelgiStep, clipmaps);

amount.rgb = max(vec3(0.0), amount.rgb);
amount.a = clamp(amount.a, 0.0, 1.0);

return amount * voxelgiOcc;
}
*/
#endif

#ifdef _VoxelAOvar
float traceConeAO(const sampler3D voxels, const vec3 origin, const vec3 n, const vec3 dir, const int precomputed_direction, const float aperture, const float clipmaps[voxelgiClipmapCount * 10]) {
float opacity = 0.0;
float traceConeAO(const sampler3D voxels, const vec3 origin, const vec3 n, const vec3 dir, const int precomputed_direction, const float aperture, const float step_size, const float clipmaps[voxelgiClipmapCount * 10]) {
float sampleCol = 0.0;
float voxelSize0 = float(clipmaps[0]) * 2.0;
float dist = voxelSize0;
float step_dist = dist;
vec3 samplePos;
vec3 start_pos = origin + n * voxelSize0;
vec3 start_pos = origin + n * voxelSize0 * voxelgiOffset;
int clipmap_index0 = 0;

vec3 aniso_direction = -dir;
Expand All @@ -205,7 +212,7 @@ float traceConeAO(const sampler3D voxels, const vec3 origin, const vec3 n, const

float coneCoefficient = 2.0 * tan(aperture * 0.5);

while (opacity < 1.0 && dist < MAX_DISTANCE && clipmap_index0 < voxelgiClipmapCount) {
while (sampleCol < 1.0 && dist < MAX_DISTANCE && clipmap_index0 < voxelgiClipmapCount) {
float mipSample = 0.0;
float diam = max(voxelSize0, dist * coneCoefficient);
float lod = clamp(log2(diam / voxelSize0), clipmap_index0, voxelgiClipmapCount - 1);
Expand All @@ -228,13 +235,12 @@ float traceConeAO(const sampler3D voxels, const vec3 origin, const vec3 n, const
mipSample = mix(mipSample, mipSampleNext, clipmap_blend);
}

float a = 1.0 - opacity;
opacity += a * mipSample;
sampleCol += (1.0 - sampleCol) * mipSample;

step_dist = diam;
step_dist = diam * step_size;
dist += step_dist;
}
return opacity;
return sampleCol;
}


Expand All @@ -247,24 +253,24 @@ float traceAO(const vec3 origin, const vec3 normal, const sampler3D voxels, cons
const float cosTheta = dot(normal, coneDir);
if (cosTheta <= 0)
continue;
amount += traceConeAO(voxels, origin, normal, coneDir, precomputed_direction, DIFFUSE_CONE_APERTURE, clipmaps) * cosTheta;
amount += traceConeAO(voxels, origin, normal, coneDir, precomputed_direction, DIFFUSE_CONE_APERTURE, voxelgiStep, clipmaps) * cosTheta;
sum += cosTheta;
}
amount /= sum;
amount = max(0.0, amount);
amount = clamp(amount, 0.0, 1.0);
return amount * voxelgiOcc;
}
#endif


#ifdef _VoxelShadow
float traceConeShadow(const sampler3D voxels, const sampler3D voxelsSDF, const vec3 origin, const vec3 n, const vec3 dir, const float aperture, const float step_size, const float clipmaps[voxelgiClipmapCount * 10]) {
float opacity = 0.0;
float sampleCol = 0.0;
float voxelSize0 = float(clipmaps[0]) * 2.0;
float dist = voxelSize0;
float step_dist = dist;
vec3 samplePos;
vec3 start_pos = origin + n * voxelSize0;
vec3 start_pos = origin + n * voxelSize0 * voxelgiOffset;
int clipmap_index0 = 0;

vec3 aniso_direction = -dir;
Expand All @@ -275,7 +281,7 @@ float traceConeShadow(const sampler3D voxels, const sampler3D voxelsSDF, const v
) / (6 + DIFFUSE_CONE_COUNT);
vec3 direction_weight = abs(dir);

while (opacity < 1.0 && dist < MAX_DISTANCE && clipmap_index0 < voxelgiClipmapCount) {
while (sampleCol < 1.0 && dist < MAX_DISTANCE * 100 && clipmap_index0 < voxelgiClipmapCount) {
float mipSample = 0.0;
float diam = max(voxelSize0, dist * 2.0 * tan(aperture * 0.5));
float lod = clamp(log2(diam / voxelSize0), clipmap_index0, voxelgiClipmapCount - 1);
Expand Down Expand Up @@ -306,29 +312,28 @@ float traceConeShadow(const sampler3D voxels, const sampler3D voxelsSDF, const v
mipSample = mix(mipSample, mipSampleNext, clipmap_blend);
}

float a = 1.0 - opacity;
opacity += a * mipSample;
sampleCol += (1.0 - sampleCol) * mipSample;

float stepSizeCurrent = step_size;

if (true) {
// half texel correction is applied to avoid sampling over current clipmap:
const vec3 half_texel = vec3(0.5) / voxelgiResolution;
vec3 tc0 = clamp(samplePos, half_texel, 1 - half_texel);
tc0.y = (tc0.y + clipmap_index) / voxelgiClipmapCount; // remap into clipmap
float sdf = textureLod(voxelsSDF, tc0, 0.0).r;
stepSizeCurrent = max(step_size, sdf - diam);
}
// half texel correction is applied to avoid sampling over current clipmap:
const vec3 half_texel = vec3(0.5) / voxelgiResolution;
vec3 tc0 = clamp(samplePos, half_texel, 1 - half_texel);
tc0.y = (tc0.y + clipmap_index) / voxelgiClipmapCount; // remap into clipmap
float sdf = textureLod(voxelsSDF, tc0, 0.0).r;
stepSizeCurrent = max(step_size, sdf - diam);

step_dist = diam * stepSizeCurrent;
dist += step_dist;
}
return opacity;
return sampleCol;
}


float traceShadow(const vec3 origin, const vec3 normal, const sampler3D voxels, const sampler3D voxelsSDF, const vec3 dir, const float clipmaps[voxelgiClipmapCount * 10]) {
float amount = traceConeShadow(voxels, voxelsSDF, origin, normal, dir, DIFFUSE_CONE_APERTURE, voxelgiStep, clipmaps);
amount = max(0.0, amount);
float traceShadow(const vec3 origin, const vec3 normal, const sampler3D voxels, const sampler3D voxelsSDF, const vec3 dir, const float clipmaps[voxelgiClipmapCount * 10], const vec2 pixel) {
vec3 P = origin + dir * (BayerMatrix8[int(pixel.x) % 8][int(pixel.y) % 8] - 0.5) * voxelgiStep;
float amount = traceConeShadow(voxels, voxelsSDF, P, normal, dir, DIFFUSE_CONE_APERTURE, voxelgiStep, clipmaps);
amount = clamp(amount, 0.0, 1.0);
return amount * voxelgiOcc;
}
#endif
Expand Down
Loading

0 comments on commit 61c4c86

Please sign in to comment.