Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix voxel gi #3024

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 16 additions & 22 deletions Shaders/deferred_light/deferred_light.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
#ifdef _Irr
#include "std/shirr.glsl"
#endif
#ifdef _VoxelRefract
#include "std/conetrace.glsl"
#endif
#ifdef _VoxelShadow
#ifndef _VoxelRefract
#include "std/conetrace.glsl"
#endif
#endif
#ifdef _SSS
#include "std/sss.glsl"
#endif
Expand All @@ -32,14 +37,16 @@ uniform sampler2D gbuffer1;
#ifdef _VoxelGI
uniform sampler2D voxels_diffuse;
uniform sampler2D voxels_specular;
#ifdef _VoxelRefract
uniform sampler2D voxels_refraction;
uniform sampler2D gbuffer_refraction;
#endif
#endif
#ifdef _VoxelAOvar
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 @@ -277,13 +284,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 @@ -369,7 +376,7 @@ void main() {
#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 Down Expand Up @@ -434,9 +441,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 Down Expand Up @@ -494,9 +499,7 @@ void main() {
, lightsArraySpot[li * 2 + 1].xyz // right
#endif
#ifdef _VoxelShadow
, voxels
, voxelsSDF
, clipmaps
, texCoord
#endif
#ifdef _MicroShadowing
, occspec.x
Expand All @@ -507,14 +510,5 @@ void main() {
);
}
#endif // _Clusters

/*
#ifdef _VoxelRefract
if(opac < 1.0) {
vec3 refraction = traceRefraction(p, n, voxels, v, ior, roughness, eye) * voxelgiRefr;
fragColor.rgb = mix(refraction, fragColor.rgb, opac);
}
#endif
*/
fragColor.a = 1.0; // Mark as opaque
}
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
35 changes: 20 additions & 15 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 All @@ -10,6 +13,7 @@ uniform sampler2D gbufferD;
uniform sampler2D gbuffer0;
uniform sampler2D gbufferD1;
uniform sampler2D gbuffer_refraction; // ior\opacity
uniform sampler2D voxels_refraction;
uniform mat4 P;
uniform mat3 V3;
uniform vec2 cameraProj;
Expand All @@ -35,7 +39,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 +52,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 Down Expand Up @@ -85,22 +89,23 @@ 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);
vec3 refractionCol;
if (all(equal(coords, vec4(0.0)))) // out of range
refractionCol = textureLod(tex1, texCoord, 0.0).rgb;
else
refractionCol = textureLod(tex1, coords.xy, 0.0).rgb;

refractionCol = clamp(refractionCol, 0.0, 1.0);
vec3 color = textureLod(tex, texCoord.xy, 0.0).rgb;
#ifdef _VoxelRefract
vec3 voxelsRefr = textureLod(voxels_refraction, texCoord, 0.0).rgb * voxelgiRefr;
refractionCol = (refractionCol + voxelsRefr) / 2;
#endif
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