Skip to content

Commit 419c762

Browse files
committed
Fixed voxelization volume
1 parent 4a056a1 commit 419c762

File tree

6 files changed

+166
-54
lines changed

6 files changed

+166
-54
lines changed

resources/shaders/VXGI/voxelization.glsl

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -185,21 +185,24 @@ void main() {
185185
scene.lights[i].type != DIRECTIONAL_LIGHT ? normalize(scene.lights[i].worldPosition.xyz - _pos) : normalize(scene.lights[i].worldPosition.xyz), //wi //wo
186186
scene.lights[i].color * computeAttenuation(scene.lights[i].worldPosition.xyz, _pos,scene.lights[i].areaEffect,int(scene.lights[i].type)) * scene.lights[i].intensity
187187
);
188-
if(scene.lights[i].shadowCast == 1) {
189-
if(scene.lights[i].shadowType == 0) //Classic
190-
lighting *= computeShadow(shadowMap, scene.lights[i], i, _pos);
191-
if(scene.lights[i].shadowType == 1) //VSM
192-
lighting *= computeVarianceShadow(shadowMap, scene.lights[i], i, _pos);
193-
if(scene.lights[i].shadowType == 2) //Raytraced
194-
lighting *= computeRaytracedShadow(
195-
TLAS,
196-
blueNoiseMap,
197-
_pos,
198-
scene.lights[i].type != DIRECTIONAL_LIGHT ? scene.lights[i].worldPosition.xyz - _pos : scene.lights[i].shadowData.xyz,
199-
1,
200-
0.0,
201-
0);
202-
}
188+
189+
190+
//Visibility__________________________
191+
// if(scene.lights[i].shadowCast == 1) {
192+
// if(scene.lights[i].shadowType == 0) //Classic
193+
// lighting *= computeShadow(shadowMap, scene.lights[i], i, _pos);
194+
// if(scene.lights[i].shadowType == 1) //VSM
195+
// lighting *= computeVarianceShadow(shadowMap, scene.lights[i], i, _pos);
196+
// if(scene.lights[i].shadowType == 2) //Raytraced
197+
// lighting *= computeRaytracedShadow(
198+
// TLAS,
199+
// blueNoiseMap,
200+
// _pos,
201+
// scene.lights[i].type != DIRECTIONAL_LIGHT ? scene.lights[i].worldPosition.xyz - _pos : scene.lights[i].shadowData.xyz,
202+
// 1,
203+
// 0.0,
204+
// 0);
205+
// }
203206

204207
color += lighting;
205208
}

resources/shaders/deferred/composition.glsl

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ int g_isReflective;
8787
vec4 g_temp;
8888

8989

90+
9091
vec3 GI(vec3 worldPos, vec3 worldNormal){
9192
const float VOXEL_SIZE = 1.0/float(settings.vxgi.resolution);
9293
const float ISQRT2 = 0.707106;
@@ -112,35 +113,36 @@ vec3 GI(vec3 worldPos, vec3 worldNormal){
112113
// Backward in cone direction improves GI, and forward direction removes
113114
// artifacts.
114115
const float CONE_OFFSET = -0.01;
116+
const float CONE_SPREAD = 0.325;
115117

116118
// Trace front cone
117-
indirect += w[0] * traceDiffuseVoxelCone(voxelMap, C_ORIGIN + CONE_OFFSET * worldNormal, worldNormal, VOXEL_SIZE );
119+
indirect += w[0] * traceVoxelCone(voxelMap, C_ORIGIN + CONE_OFFSET * worldNormal, worldNormal,CONE_SPREAD, VOXEL_SIZE );
118120

119121
// Trace 4 side cones.
120122
const vec3 s1 = mix(worldNormal, ortho, ANGLE_MIX);
121-
indirect += w[1] * traceDiffuseVoxelCone(voxelMap, C_ORIGIN + CONE_OFFSET * ortho, s1, VOXEL_SIZE );
123+
indirect += w[1] * traceVoxelCone(voxelMap, C_ORIGIN + CONE_OFFSET * ortho, s1,CONE_SPREAD, VOXEL_SIZE );
122124

123125
const vec3 s2 = mix(worldNormal, -ortho, ANGLE_MIX);
124-
indirect += w[1] * traceDiffuseVoxelCone(voxelMap, C_ORIGIN - CONE_OFFSET * ortho, s2, VOXEL_SIZE );
126+
indirect += w[1] * traceVoxelCone(voxelMap, C_ORIGIN - CONE_OFFSET * ortho, s2,CONE_SPREAD, VOXEL_SIZE );
125127

126128
const vec3 s3 = mix(worldNormal, ortho2, ANGLE_MIX);
127-
indirect += w[1] * traceDiffuseVoxelCone(voxelMap, C_ORIGIN + CONE_OFFSET * ortho2, s3, VOXEL_SIZE );
129+
indirect += w[1] * traceVoxelCone(voxelMap, C_ORIGIN + CONE_OFFSET * ortho2, s3,CONE_SPREAD, VOXEL_SIZE );
128130

129131
const vec3 s4 = mix(worldNormal, -ortho2, ANGLE_MIX);
130-
indirect += w[1] * traceDiffuseVoxelCone(voxelMap, C_ORIGIN - CONE_OFFSET * ortho2, s4, VOXEL_SIZE );
132+
indirect += w[1] * traceVoxelCone(voxelMap, C_ORIGIN - CONE_OFFSET * ortho2, s4,CONE_SPREAD, VOXEL_SIZE );
131133

132134
// Trace 4 corner cones.
133135
const vec3 c1 = mix(worldNormal, corner, ANGLE_MIX);
134-
indirect += w[2] * traceDiffuseVoxelCone(voxelMap, C_ORIGIN + CONE_OFFSET * corner, c1, VOXEL_SIZE );
136+
indirect += w[2] * traceVoxelCone(voxelMap, C_ORIGIN + CONE_OFFSET * corner, c1,CONE_SPREAD, VOXEL_SIZE );
135137

136138
const vec3 c2 = mix(worldNormal, -corner, ANGLE_MIX);
137-
indirect += w[2] * traceDiffuseVoxelCone(voxelMap, C_ORIGIN - CONE_OFFSET * corner, c2, VOXEL_SIZE );
139+
indirect += w[2] * traceVoxelCone(voxelMap, C_ORIGIN - CONE_OFFSET * corner, c2,CONE_SPREAD, VOXEL_SIZE );
138140

139141
const vec3 c3 = mix(worldNormal, corner2, ANGLE_MIX);
140-
indirect += w[2] * traceDiffuseVoxelCone(voxelMap, C_ORIGIN + CONE_OFFSET * corner2, c3, VOXEL_SIZE );
142+
indirect += w[2] * traceVoxelCone(voxelMap, C_ORIGIN + CONE_OFFSET * corner2, c3,CONE_SPREAD, VOXEL_SIZE );
141143

142144
const vec3 c4 = mix(worldNormal, -corner2, ANGLE_MIX);
143-
indirect += w[2] * traceDiffuseVoxelCone(voxelMap, C_ORIGIN - CONE_OFFSET * corner2, c4, VOXEL_SIZE );
145+
indirect += w[2] * traceVoxelCone(voxelMap, C_ORIGIN - CONE_OFFSET * corner2, c4,CONE_SPREAD, VOXEL_SIZE );
144146

145147
// Return result.
146148
// return DIFFUSE_INDIRECT_FACTOR * material.diffuseReflectivity * acc * (material.diffuseColor + vec3(0.001f));
@@ -187,6 +189,7 @@ void main()
187189
vec3 direct = vec3(0.0);
188190
vec3 ambient = vec3(0.0);
189191
vec3 indirect = vec3(0.0);
192+
vec3 indirect2 = vec3(0.0);
190193

191194
vec3 modelPos = (camera.invView * vec4(g_pos.xyz, 1.0)).xyz;
192195
vec3 modelNormal = (camera.invView * vec4(g_normal.xyz, 0.0)).xyz;
@@ -206,12 +209,50 @@ void main()
206209
brdf.F0 = vec3(0.04);
207210
brdf.F0 = mix(brdf.F0, brdf.albedo, brdf.metalness);
208211
brdf.emission = g_emission;
209-
210-
// GI ____________________________
212+
// Indirect Component ____________________________
211213
if(settings.vxgi.enabled == 1){
212-
indirect = GI(modelPos, modelNormal)*settings.vxgi.strength*g_albedo;
214+
vec3 diffuseIndirect = vec3(0.0);
215+
216+
const float VOXEL_SIZE = 1.0/float(settings.vxgi.resolution);
217+
218+
219+
// Offset startPos to avoid self occlusion
220+
vec3 startPos = modelPos + modelNormal * VOXEL_SIZE;
221+
222+
float coneTraceCount = 0.0;
223+
float cosSum = 0.0;
224+
for (int i = 0; i < DIFFUSE_CONE_COUNT; ++i)
225+
{
226+
float cosTheta = dot(modelNormal, DIFFUSE_CONE_DIRECTIONS[i]);
227+
228+
if (cosTheta < 0.0)
229+
continue;
230+
231+
coneTraceCount += 1.0;
232+
diffuseIndirect += traceVoxelCone(voxelMap, startPos, DIFFUSE_CONE_DIRECTIONS[i],DIFFUSE_CONE_APERTURE , VOXEL_SIZE );
233+
}
234+
235+
diffuseIndirect /= coneTraceCount;
236+
// indirectContribution.a *= u_ambientOcclusionFactor;
237+
238+
diffuseIndirect.rgb *= g_albedo * settings.vxgi.strength;
239+
240+
241+
indirect = diffuseIndirect.rgb;
242+
243+
244+
// indirect = GI(modelPos, modelNormal)*settings.vxgi.strength*g_albedo;
213245
indirect*= settings.enableAO == 1 ? (brdf.ao * SSAO) : brdf.ao;
246+
247+
// vec3 specularConeDirection = reflect(normalize(camera.position.xyz-modelPos), modelNormal);
248+
// vec3 specularIndirect = vec3(0.0);
249+
250+
251+
// specularIndirect = traceVoxelCone(voxelMap, startPos, specularConeDirection, max(brdf.roughness, 0.05) , VOXEL_SIZE );
252+
// // specularIndirect = castCone(startPos, specularConeDirection, max(roughness, MIN_SPECULAR_APERTURE), MAX_TRACE_DISTANCE, minLevel).rgb * specColor.rgb * u_indirectSpecularIntensity;
253+
// indirect2+=specularIndirect;
214254
}
255+
215256

216257
//Direct Component ________________________
217258
for(int i = 0; i < scene.numLights; i++) {
@@ -232,7 +273,6 @@ void main()
232273
if(scene.lights[i].shadowType == 1) //VSM
233274
lighting *= computeVarianceShadow(shadowMap, scene.lights[i], i, modelPos);
234275
if(scene.lights[i].shadowType == 2) //Raytraced
235-
// lighting *= traceShadowCone(voxelMap, modelPos, scene.lights[i].type != DIRECTIONAL_LIGHT ? scene.lights[i].worldPosition.xyz - modelPos : scene.lights[i].shadowData.xyz, modelNormal, 1.0/float(settings.vxgi.resolution), length(scene.lights[i].worldPosition.xyz - modelPos) );
236276
lighting *= computeRaytracedShadow(
237277
TLAS,
238278
blueNoiseMap,
@@ -263,6 +303,7 @@ void main()
263303
ambient = (scene.ambientIntensity * scene.ambientColor) * brdf.albedo;
264304
}
265305
ambient *= settings.enableAO == 1 ? (brdf.ao * SSAO) : brdf.ao;
306+
266307
//SSR ________________________________
267308
vec3 fresnel = fresnelSchlick(max(dot(g_normal, normalize(g_pos)), 0.0), brdf.F0);
268309
if(settings.ssr.enabled == 1 && g_isReflective == 1){

resources/shaders/include/VXGI.glsl

Lines changed: 79 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,62 @@ struct VXGI {
1313
uint enabled;
1414
};
1515

16-
vec3 traceDiffuseVoxelCone(sampler3D voxelization ,const vec3 O, vec3 dir, const float VOXEL_SIZE){
16+
const int DIFFUSE_CONE_COUNT = 16;
17+
const float DIFFUSE_CONE_APERTURE = 0.872665;
18+
19+
const vec3 DIFFUSE_CONE_DIRECTIONS[16] = {
20+
vec3(0.57735, 0.57735, 0.57735),
21+
vec3(0.57735, -0.57735, -0.57735),
22+
vec3(-0.57735, 0.57735, -0.57735),
23+
vec3(-0.57735, -0.57735, 0.57735),
24+
vec3(-0.903007, -0.182696, -0.388844),
25+
vec3(-0.903007, 0.182696, 0.388844),
26+
vec3(0.903007, -0.182696, 0.388844),
27+
vec3(0.903007, 0.182696, -0.388844),
28+
vec3(-0.388844, -0.903007, -0.182696),
29+
vec3(0.388844, -0.903007, 0.182696),
30+
vec3(0.388844, 0.903007, -0.182696),
31+
vec3(-0.388844, 0.903007, 0.182696),
32+
vec3(-0.182696, -0.388844, -0.903007),
33+
vec3(0.182696, 0.388844, -0.903007),
34+
vec3(-0.182696, 0.388844, 0.903007),
35+
vec3(0.182696, -0.388844, 0.903007)
36+
};
37+
38+
// vec4 traceVoxelCone(sampler3D voxelization, vec3 origin, vec3 direction, float tanHalfAngle)
39+
// {
40+
// float lod = 0.0f;
41+
// vec3 color = vec3(0.0f);
42+
// float alpha = 0.0f;
43+
// float occlusion = 0.0f;
44+
45+
// //Voxel Cube Size
46+
// float voxelWorldSize = VoxelGridWorldSize / VoxelDimensions;
47+
// float dist = voxelWorldSize;
48+
// vec3 startPos = Position_world + Normal_world * voxelWorldSize;
49+
50+
// while(dist < MAX_DISTANCE && alpha < MAX_ALPHA)
51+
// {
52+
// float diameter = max(voxelWorldSize, 2.0f * tanHalfAngle * dist);
53+
// float lodLevel = log2(diameter / voxelWorldSize);
54+
// vec4 voxelColor = SampleVoxels(startPos + dist * direction, lodLevel);
55+
56+
// color += (1.0f - alpha) * voxelColor.rgb;
57+
// occlusion += ((1.0f - alpha) * voxelColor.a) / (1.0f + 0.03f * diameter);
58+
// alpha += (1.0f - alpha) * voxelColor.a;
59+
// dist += diameter;
60+
// }
61+
62+
// return vec4(color, occlusion);
63+
// }
64+
65+
/*
66+
FIRST TRY
67+
*/
68+
vec3 traceVoxelCone(sampler3D voxelization ,const vec3 O, vec3 dir, const float CONE_SPREAD, const float VOXEL_SIZE){
1769
dir = normalize(dir);
1870

19-
const float CONE_SPREAD = 0.325;
71+
// const float CONE_SPREAD = 0.325;
2072

2173
vec4 color = vec4(0.0);
2274

@@ -49,25 +101,28 @@ vec3 traceDiffuseVoxelCone(sampler3D voxelization ,const vec3 O, vec3 dir, const
49101

50102
}
51103

52-
// Returns a soft shadow blend by using shadow cone tracing.
53-
// Uses 2 samples per step, so it's pretty expensive.
54-
float traceShadowCone(sampler3D voxelization , vec3 O, vec3 dir, vec3 normal, const float VOXEL_SIZE, float targetDistance){
55-
O += normal * 0.05; // Removes artifacts but makes self shadowing for dense meshes meh.
56-
57-
float color = 0;
58-
59-
float dist = 3 * VOXEL_SIZE;
60-
// I'm using a pretty big margin here since I use an emissive light ball with a pretty big radius in my demo scenes.
61-
const float STOP = targetDistance - 16 * VOXEL_SIZE;
62-
63-
while(dist < STOP && color < 1){
64-
vec3 c = mapToZeroOne(O + dist * dir, scene.minCoord.xyz, scene.maxCoord.xyz);
65-
float l = pow(dist, 2); // Experimenting with inverse square falloff for shadows.
66-
float s1 = 0.062 * textureLod(voxelization, c, 1 + 0.75 * l).a;
67-
float s2 = 0.135 * textureLod(voxelization, c, 4.5 * l).a;
68-
float s = s1 + s2;
69-
color += (1 - color) * s;
70-
dist += 0.9 * VOXEL_SIZE * (1 + 0.05 * l);
71-
}
72-
return 1 - pow(smoothstep(0, 1, color * 1.4), 1.0 / 1.4);
73-
}
104+
vec3 diffuseVoxelGI(sampler3D voxelMap, vec3 worldPos, vec3 worldNormal, int resolution){
105+
// vec3 diffuseIndirect = vec3(0.0);
106+
107+
// const float VOXEL_SIZE = 1.0/float(resolution);
108+
109+
110+
// vec3 startPos = worldPos + worldNormal * VOXEL_SIZE;
111+
112+
// float coneTraceCount = 0.0;
113+
// float cosSum = 0.0;
114+
// for (int i = 0; i < DIFFUSE_CONE_COUNT; ++i)
115+
// {
116+
// float cosTheta = dot(worldNormal, DIFFUSE_CONE_DIRECTIONS[i]);
117+
// if (cosTheta < 0.0)
118+
// continue;
119+
// coneTraceCount += 1.0;
120+
// diffuseIndirect += traceVoxelCone(voxelMap, startPos, DIFFUSE_CONE_DIRECTIONS[i],DIFFUSE_CONE_APERTURE , VOXEL_SIZE ) * cosTheta;
121+
// }
122+
123+
// diffuseIndirect /= DIFFUSE_CONE_COUNT * 0.5;
124+
125+
// // indirectContribution.a *= u_ambientOcclusionFactor;
126+
// return diffuseIndirect;
127+
128+
}

resources/shaders/misc/tonemapping.glsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ layout(location = 0) out vec4 outputImage;
2727
void main()
2828
{
2929
vec3 result = texture(inputImage,v_uv).rgb;
30-
result = vec3(1.0) - exp(-result * 1.0);
30+
float exposure = 1.0;
31+
result = vec3(1.0) - exp(-result * exposure);
3132
// result = result / (result + vec3(1.0));
3233

3334
outputImage = vec4(result,1.0); //WIP

src/core/passes/voxelization_pass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ void VoxelizationPass::create_voxelization_image() {
1717
m_resourceImages[0].create_view(config);
1818

1919
SamplerConfig samplerConfig = {};
20-
samplerConfig.samplerAddressMode = ADDRESS_MODE_CLAMP_TO_EDGE;
20+
samplerConfig.samplerAddressMode = ADDRESS_MODE_CLAMP_TO_BORDER;
21+
samplerConfig.border = BorderColor::FLOAT_OPAQUE_BLACK;
2122
m_resourceImages[0].create_sampler(samplerConfig);
2223
}
2324

src/core/scene/scene.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ VKFW::Graphics::TLAS* VKFW::Core::get_TLAS(Scene* const scene) {
88
}
99
void VKFW::Core::Scene::update_AABB() {
1010

11+
m_volume.maxCoords = Vec3(0.0);
12+
m_volume.minCoords = Vec3(INFINITY);
13+
1114
for (Mesh* m : m_meshes)
1215
{
16+
if (!m->is_active())
17+
continue;
1318
BV const* bvolume = m->get_bounding_volume();
1419
if (!bvolume)
1520
continue;
@@ -34,5 +39,11 @@ void VKFW::Core::Scene::update_AABB() {
3439
m_volume.minCoords.z = minCoords.z;
3540
}
3641

42+
// Make a cube container for voxelization
43+
float maxTerm = std::max(std::max(m_volume.maxCoords.r, m_volume.maxCoords.g), m_volume.maxCoords.b);
44+
float minTerm = std::min(std::min(m_volume.minCoords.r, m_volume.minCoords.g), m_volume.minCoords.b);
45+
m_volume.maxCoords = Vec3(maxTerm);
46+
m_volume.minCoords = Vec3(minTerm);
47+
3748
m_volume.center = (m_volume.maxCoords + m_volume.minCoords) * 0.5f;
3849
}

0 commit comments

Comments
 (0)