Skip to content

Commit

Permalink
fix voxel gi
Browse files Browse the repository at this point in the history
  • Loading branch information
e2002e committed Jul 22, 2024
1 parent cbd03a4 commit 6ddd2e4
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 141 deletions.
2 changes: 1 addition & 1 deletion Shaders/deferred_light/deferred_light.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ void main() {
#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
38 changes: 18 additions & 20 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 @@ -157,7 +157,7 @@ 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;
Expand All @@ -167,7 +167,7 @@ vec4 traceDiffuse(const vec3 origin, const vec3 normal, const sampler3D voxels,
}

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 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);

Expand All @@ -186,8 +186,8 @@ vec3 traceRefraction(const vec3 origin, const vec3 normal, sampler3D voxels, sam
#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;
Expand All @@ -205,7 +205,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 +228,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,19 +246,19 @@ 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;
Expand All @@ -275,7 +274,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,12 +305,11 @@ 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) {
if (false) {
// 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);
Expand All @@ -322,14 +320,14 @@ float traceConeShadow(const sampler3D voxels, const sampler3D voxelsSDF, const v
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);
return amount * voxelgiOcc;
amount = clamp(amount, 0.0, 1.0);
return amount;
}
#endif
#endif // _CONETRACE_GLSL_
2 changes: 1 addition & 1 deletion Shaders/std/light.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ uniform sampler2D sltcMag;
#endif
#ifdef _Clusters
uniform sampler2DShadow shadowMapSpot[maxLightsCluster];
uniform mat4 LWVPSpotArray[maxLightsCluster];
uniform mat4 LWVPSpot[maxLightsCluster];
#endif
#endif
#endif
Expand Down
59 changes: 49 additions & 10 deletions Shaders/voxel_light/voxel_light.comp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@ uniform float clipmaps[voxelgiClipmapCount * 10];
uniform int clipmapLevel;

uniform layout(r32ui) uimage3D voxelsLight;
uniform layout(r32ui) uimage3D voxels;

#ifdef _ShadowMap
uniform sampler2DShadow shadowMap;
uniform sampler2D shadowMapTransparent;
uniform sampler2DShadow shadowMapSpot;
uniform sampler2D shadowMapSpotTransparent;
#ifdef _ShadowMapAtlas
uniform sampler2DShadow shadowMapPoint;
uniform sampler2D shadowMapPointTransparent;
#else
uniform samplerCubeShadow shadowMapPoint;
uniform samplerCube shadowMapPointTransparent;
#endif
#endif

Expand Down Expand Up @@ -83,7 +88,6 @@ float lpToDepth(vec3 lp, const vec2 lightProj) {

void main() {
int res = voxelgiResolution.x;

ivec3 dst = ivec3(gl_GlobalInvocationID.xyz);

vec3 P = (gl_GlobalInvocationID.xyz + 0.5) / voxelgiResolution;
Expand All @@ -92,13 +96,11 @@ void main() {
P *= voxelgiResolution;
P += vec3(clipmaps[int(clipmapLevel * 10 + 4)], clipmaps[int(clipmapLevel * 10 + 5)], clipmaps[int(clipmapLevel * 10 + 6)]);

vec4 light = vec4(0.0);

float visibility;
vec3 visibility;
vec3 lp = lightPos - P;
vec3 l;
if (lightType == 0) { l = lightDir; visibility = 1.0; }
else { l = normalize(lp); visibility = attenuate(distance(P, lightPos)); }
if (lightType == 0) { l = lightDir; visibility = vec3(1.0); }
else { l = normalize(lp); visibility = vec3(attenuate(distance(P, lightPos))); }

// float dotNL = max(dot(wnormal, l), 0.0);
// if (dotNL == 0.0) return;
Expand All @@ -107,7 +109,7 @@ void main() {
if (lightShadow == 1) {
vec4 lightPosition = LVP * vec4(P, 1.0);
vec3 lPos = lightPosition.xyz / lightPosition.w;
visibility = texture(shadowMap, vec3(lPos.xy, lPos.z - shadowsBias)).r;
visibility = texture(shadowMap, vec3(lPos.xy, lPos.z - shadowsBias)).rrr;
}
else if (lightShadow == 2) {
vec4 lightPosition = LVP * vec4(P, 1.0);
Expand Down Expand Up @@ -138,8 +140,45 @@ void main() {
}
}

light.rgb += visibility * lightColor;
light = clamp(light, vec4(0.0), vec4(1.0));
vec3 light = visibility * lightColor;

for (int i = 0; i < 6; i++) {
ivec3 src = ivec3(gl_GlobalInvocationID.xyz);
src.x += i * res;
vec3 N = vec3(0.0);
N.r = float(imageLoad(voxels, src + ivec3(0, 0, voxelgiResolution.x * 7))) / 255;
N.g = float(imageLoad(voxels, src + ivec3(0, 0, voxelgiResolution.x * 8))) / 255;
N /= 2;
vec3 wnormal = decode_oct(N.rg * 2 - 1);

vec3 coneDirection = wnormal;
vec3 aniso_direction = coneDirection;
uvec3 face_offsets = uvec3(
aniso_direction.x > 0 ? 0 : 1,
aniso_direction.y > 0 ? 2 : 3,
aniso_direction.z > 0 ? 4 : 5
);
vec3 direction_weights = abs(coneDirection);

if (direction_weights.x > 0) {
vec3 light_weight = light * direction_weights.x;
imageAtomicAdd(voxelsLight, src + ivec3(face_offsets.x, 0, 0), uint(light_weight.r * 255));
imageAtomicAdd(voxelsLight, src + ivec3(face_offsets.x, 0, voxelgiResolution.x), uint(light_weight.g * 255));
imageAtomicAdd(voxelsLight, src + ivec3(face_offsets.x, 0, voxelgiResolution.x * 2), uint(light_weight.b * 255));
}

imageAtomicMax(voxelsLight, dst, convVec4ToRGBA8(light));
if (direction_weights.y > 0) {
vec3 light_weight = light * direction_weights.y;
imageAtomicAdd(voxelsLight, src + ivec3(face_offsets.y, 0, 0), uint(light_weight.r * 255));
imageAtomicAdd(voxelsLight, src + ivec3(face_offsets.y, 0, voxelgiResolution.x), uint(light_weight.g * 255));
imageAtomicAdd(voxelsLight, src + ivec3(face_offsets.y, 0, voxelgiResolution.x * 2), uint(light_weight.b * 255));
}

if (direction_weights.z > 0) {
vec3 light_weight = light * direction_weights.z;
imageAtomicAdd(voxelsLight, src + ivec3(face_offsets.z, 0, 0), uint(light_weight.r * 255));
imageAtomicAdd(voxelsLight, src + ivec3(face_offsets.z, 0, voxelgiResolution.x), uint(light_weight.g * 255));
imageAtomicAdd(voxelsLight, src + ivec3(face_offsets.z, 0, voxelgiResolution.x * 2), uint(light_weight.b * 255));
}
}
}
8 changes: 4 additions & 4 deletions Shaders/voxel_offsetprev/voxel_offsetprev.comp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ layout (local_size_x = 8, local_size_y = 8, local_size_z = 8) in;
#include "std/voxels_constants.glsl"

#ifdef _VoxelGI
uniform layout(rgba8) image3D voxelsB;
uniform layout(rgba8) image3D voxelsOut;
uniform layout(rgba16) image3D voxelsB;
uniform layout(rgba16) image3D voxelsOut;
#else
uniform layout(r8) image3D voxelsB;
uniform layout(r8) image3D voxelsOut;
uniform layout(r16) image3D voxelsB;
uniform layout(r16) image3D voxelsOut;
#endif

uniform int clipmapLevel;
Expand Down
4 changes: 2 additions & 2 deletions Shaders/voxel_sdf_jumpflood/voxel_sdf_jumpflood.comp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ THE SOFTWARE.

#include "compiled.inc"

uniform layout(r8) image3D input_sdf;
uniform layout(r8) image3D output_sdf;
uniform layout(r16) image3D input_sdf;
uniform layout(r16) image3D output_sdf;

uniform float jump_size;
uniform int clipmapLevel;
Expand Down
Loading

0 comments on commit 6ddd2e4

Please sign in to comment.