Skip to content

Commit 7159a3e

Browse files
committed
Better method for cone tracing
1 parent 419c762 commit 7159a3e

File tree

8 files changed

+193
-239
lines changed

8 files changed

+193
-239
lines changed

examples/resources/scenes/sponza.xml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<!-- MESHES -->
1515
<Mesh type="file" name="Sponza">
1616

17-
<Filename value="meshes/walls.obj" />
17+
<Filename value="meshes/testWall.obj" />
1818

1919
<Transform>
2020
<translate x="0.0" y="-2.0" z="0.0" />
@@ -36,7 +36,7 @@
3636
<albedo r="0.5" g="0.5" b="0.5" />
3737
<Textures>
3838
<albedo path="textures/Arches/Arches_Diffuse.png" />
39-
<normals path="textures/Arches/Arches_Normal.png" />
39+
<!-- <normals path="textures/Arches/Arches_Normal.png" /> -->
4040
<!-- <mask path="textures/Arches/Arches_MaskMap.png" type="UnityHDRP" /> -->
4141
</Textures>
4242
</Material>
@@ -264,14 +264,14 @@
264264

265265

266266
<!-- IF DAY -->
267-
<!-- <Light type="directional" name="Sun">
267+
<Light type="directional" name="Sun">
268268
<Transform>
269269
<translate x="0.0" y="50.0" z="0.0" />
270270
</Transform>
271271

272-
<intensity value="15.0" />
272+
<intensity value="10.0" />
273273
<color r="1.0" g="0.9" b="0.8" />
274-
<direction x="2.0" y="20.0" z="5.0" />
274+
<direction x="2.0" y="30.0" z="5.0" />
275275

276276
<Shadow type="rt">
277277
<samples value="2" />
@@ -281,12 +281,12 @@
281281

282282
<Enviroment type="skybox">
283283
<Filename value="textures/cloudy.hdr" />
284-
<intensity value="0.5" />
285-
</Enviroment> -->
284+
<intensity value="0.01" />
285+
</Enviroment>
286286

287287

288288
<!-- IF NIGHT -->
289-
<Light type="directional" name="Moon">
289+
<!-- <Light type="directional" name="Moon">
290290
<Transform>
291291
<translate x="0.0" y="50.0" z="0.0" />
292292
</Transform>
@@ -360,7 +360,7 @@
360360
<Filename value="textures/moon.hdr" />
361361
<intensity value="1.0" />
362362
<color r="0.14" g="0.37" b="0.58" />
363-
</Enviroment>
363+
</Enviroment> -->
364364

365365

366366
<!-- FOG -->

include/engine/core/passes/composition_pass.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ struct SSR { // Settings for Screen Space Reflections
3737
uint32_t enabled = 1;
3838
};
3939
struct VXGI { // Settings for Voxel Based GI
40-
float strength = 0.2f;
41-
uint32_t resolution = 256;
42-
uint32_t samples = 8;
43-
uint32_t enabled = 1;
40+
float strength = 1.0f;
41+
float diffuseConeSpread = 0.577f;
42+
float offset = 1.0f;
43+
float maxDistance = 75.0f;
44+
uint32_t resolution = 256;
45+
uint32_t samples = 8;
46+
uint32_t enabled = 1;
4447
};
4548

4649
class CompositionPass : public GraphicPass

include/engine/core/passes/voxelization_pass.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ namespace Core {
1717

1818
class VoxelizationPass : public GraphicPass
1919
{
20+
const uint16_t RESOURCE_IMAGES = 4;
21+
2022
/*Descriptors*/
2123
struct FrameDescriptors {
2224
Graphics::DescriptorSet globalDescritor;
@@ -30,7 +32,7 @@ class VoxelizationPass : public GraphicPass
3032
public:
3133
VoxelizationPass(Graphics::Device* ctx, uint32_t resolution)
3234
: BasePass(ctx, {resolution, resolution}, 1, 1, false, "VOXELIZATION") {
33-
m_resourceImages.resize(1);
35+
m_resourceImages.resize(RESOURCE_IMAGES);
3436
}
3537

3638
void setup_attachments(std::vector<Graphics::AttachmentInfo>& attachments,

resources/shaders/VXGI/voxelization.glsl

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ void main() {
3939

4040
vec3 ndc = mapToZeroOne(v_pos, scene.minCoord.xyz, scene.maxCoord.xyz) * 2.0 -1.0;
4141
gl_Position = vec4(ndc, 1.0);
42-
// gl_Position = camera.viewProj * vec4(v_pos, 1.0);
4342

4443
}
4544
#shader geometry
@@ -83,6 +82,11 @@ void main(){
8382
#version 460
8483
#extension GL_EXT_ray_tracing : enable
8584
#extension GL_EXT_ray_query : enable
85+
#extension GL_EXT_shader_atomic_float2 : enable
86+
// #extension GL_EXT_shader_16bit_storage : require
87+
// #extension GL_EXT_gpu_shader5 : require
88+
// #extension GL_EXT_shader_explicit_arithmetic_types_float16 : require
89+
// #extension GL_EXT_shader_atomic_fp16_vector : require
8690
#include light.glsl
8791
#include scene.glsl
8892
#include utils.glsl
@@ -98,7 +102,7 @@ layout(location = 2) in vec2 _uv;
98102
layout(set = 0, binding = 2) uniform sampler2DArray shadowMap;
99103
layout(set = 0, binding = 3) uniform samplerCube irradianceMap;
100104
layout(set = 0, binding = 4) uniform accelerationStructureEXT TLAS;
101-
layout(set = 0, binding = 5) uniform sampler2D blueNoiseMap;
105+
layout(set = 0, binding = 5) uniform sampler2D samplerMap;
102106
layout(set = 0, binding = 6, r32f) uniform image3D voxelImage;
103107

104108
layout(set = 1, binding = 1) uniform MaterialUniforms {
@@ -179,42 +183,43 @@ void main() {
179183
if(isInAreaOfInfluence(scene.lights[i].worldPosition.xyz, _pos,scene.lights[i].areaEffect,int(scene.lights[i].type))){
180184

181185
//Diffuse Component ________________________
182-
183186
vec3 lighting = vec3(0.0);
184187
lighting = evalDiffuseLighting(
185188
scene.lights[i].type != DIRECTIONAL_LIGHT ? normalize(scene.lights[i].worldPosition.xyz - _pos) : normalize(scene.lights[i].worldPosition.xyz), //wi //wo
186189
scene.lights[i].color * computeAttenuation(scene.lights[i].worldPosition.xyz, _pos,scene.lights[i].areaEffect,int(scene.lights[i].type)) * scene.lights[i].intensity
187190
);
188191

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-
// }
192+
//Visibility Component__________________________
193+
if(scene.lights[i].shadowCast == 1) {
194+
// if(scene.lights[i].shadowType == 0) //Classic
195+
// lighting *= computeShadow(shadowMap, scene.lights[i], i, _pos);
196+
// if(scene.lights[i].shadowType == 1) //VSM
197+
// lighting *= computeVarianceShadow(shadowMap, scene.lights[i], i, _pos);
198+
if(scene.lights[i].shadowType == 2) //Raytraced
199+
lighting *= computeRaytracedShadow(
200+
TLAS,
201+
samplerMap,
202+
_pos,
203+
scene.lights[i].type != DIRECTIONAL_LIGHT ? scene.lights[i].worldPosition.xyz - _pos : scene.lights[i].shadowData.xyz,
204+
1,
205+
0.0,
206+
0);
207+
}
206208

207209
color += lighting;
208210
}
209211
}
210212

211213
color += g_emisison;
212-
vec3 ambient = (scene.ambientIntensity * scene.ambientColor) * g_albedo;
214+
215+
vec3 ambient = scene.ambientIntensity * g_albedo;
213216
color +=ambient;
214-
215217

216218
vec4 result = g_opacity * vec4(vec3(color), 1);
217219
ivec3 voxelPos = worldSpaceToVoxelSpace(_pos);
220+
221+
// imageAtomicMax(voxelImage, voxelPos, 1);
222+
// imageAtomicMax(voxelImage, voxelPos, f16vec4(color, 1.0));
218223
imageStore(voxelImage, voxelPos, result);
219224

220225
}

resources/shaders/deferred/composition.glsl

Lines changed: 11 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ layout(location = 1) out vec4 outBrightColor;
4747
layout(set = 0, binding = 2) uniform sampler2DArray shadowMap;
4848
layout(set = 0, binding = 4) uniform samplerCube irradianceMap;
4949
layout(set = 0, binding = 5) uniform accelerationStructureEXT TLAS;
50-
layout(set = 0, binding = 6) uniform sampler2D blueNoiseMap;
50+
layout(set = 0, binding = 6) uniform sampler2D samplerMap;
5151
layout(set = 0, binding = 7) uniform sampler3D voxelMap;
5252
//G-BUFFER
5353
layout(set = 1, binding = 0) uniform sampler2D positionBuffer;
@@ -56,7 +56,7 @@ layout(set = 1, binding = 2) uniform sampler2D colorBuffer;
5656
layout(set = 1, binding = 3) uniform sampler2D materialBuffer;
5757
layout(set = 1, binding = 4) uniform sampler2D emissionBuffer;
5858
layout(set = 1, binding = 5) uniform sampler2D preCompositionBuffer;
59-
59+
//TEMPORAL
6060
layout(set = 1, binding = 6) uniform sampler2D prevBuffer;
6161

6262
//SETTINGS
@@ -87,70 +87,6 @@ int g_isReflective;
8787
vec4 g_temp;
8888

8989

90-
91-
vec3 GI(vec3 worldPos, vec3 worldNormal){
92-
const float VOXEL_SIZE = 1.0/float(settings.vxgi.resolution);
93-
const float ISQRT2 = 0.707106;
94-
const float ANGLE_MIX = 0.5; // Angle mix (1.0f => orthogonal direction, 0.0f => direction of normal).
95-
const float w[3] = {1.0, 1.0, 1.0}; // Cone weights.
96-
97-
// Find a base for the side cones with the normal as one of its base vectors.
98-
const vec3 ortho = normalize(orthogonal(worldNormal));
99-
const vec3 ortho2 = normalize(cross(ortho, worldNormal));
100-
101-
// Find base vectors for the corner cones too.
102-
const vec3 corner = 0.5 * (ortho + ortho2);
103-
const vec3 corner2 = 0.5 * (ortho - ortho2);
104-
105-
// Find start position of trace (start with a bit of offset).
106-
const vec3 N_OFFSET = worldNormal * (1 + 4 * ISQRT2) * VOXEL_SIZE;
107-
const vec3 C_ORIGIN = worldPos + N_OFFSET;
108-
109-
// Accumulate indirect diffuse light.
110-
vec3 indirect = vec3(0);
111-
112-
// We offset forward in normal direction, and backward in cone direction.
113-
// Backward in cone direction improves GI, and forward direction removes
114-
// artifacts.
115-
const float CONE_OFFSET = -0.01;
116-
const float CONE_SPREAD = 0.325;
117-
118-
// Trace front cone
119-
indirect += w[0] * traceVoxelCone(voxelMap, C_ORIGIN + CONE_OFFSET * worldNormal, worldNormal,CONE_SPREAD, VOXEL_SIZE );
120-
121-
// Trace 4 side cones.
122-
const vec3 s1 = mix(worldNormal, ortho, ANGLE_MIX);
123-
indirect += w[1] * traceVoxelCone(voxelMap, C_ORIGIN + CONE_OFFSET * ortho, s1,CONE_SPREAD, VOXEL_SIZE );
124-
125-
const vec3 s2 = mix(worldNormal, -ortho, ANGLE_MIX);
126-
indirect += w[1] * traceVoxelCone(voxelMap, C_ORIGIN - CONE_OFFSET * ortho, s2,CONE_SPREAD, VOXEL_SIZE );
127-
128-
const vec3 s3 = mix(worldNormal, ortho2, ANGLE_MIX);
129-
indirect += w[1] * traceVoxelCone(voxelMap, C_ORIGIN + CONE_OFFSET * ortho2, s3,CONE_SPREAD, VOXEL_SIZE );
130-
131-
const vec3 s4 = mix(worldNormal, -ortho2, ANGLE_MIX);
132-
indirect += w[1] * traceVoxelCone(voxelMap, C_ORIGIN - CONE_OFFSET * ortho2, s4,CONE_SPREAD, VOXEL_SIZE );
133-
134-
// Trace 4 corner cones.
135-
const vec3 c1 = mix(worldNormal, corner, ANGLE_MIX);
136-
indirect += w[2] * traceVoxelCone(voxelMap, C_ORIGIN + CONE_OFFSET * corner, c1,CONE_SPREAD, VOXEL_SIZE );
137-
138-
const vec3 c2 = mix(worldNormal, -corner, ANGLE_MIX);
139-
indirect += w[2] * traceVoxelCone(voxelMap, C_ORIGIN - CONE_OFFSET * corner, c2,CONE_SPREAD, VOXEL_SIZE );
140-
141-
const vec3 c3 = mix(worldNormal, corner2, ANGLE_MIX);
142-
indirect += w[2] * traceVoxelCone(voxelMap, C_ORIGIN + CONE_OFFSET * corner2, c3,CONE_SPREAD, VOXEL_SIZE );
143-
144-
const vec3 c4 = mix(worldNormal, -corner2, ANGLE_MIX);
145-
indirect += w[2] * traceVoxelCone(voxelMap, C_ORIGIN - CONE_OFFSET * corner2, c4,CONE_SPREAD, VOXEL_SIZE );
146-
147-
// Return result.
148-
// return DIFFUSE_INDIRECT_FACTOR * material.diffuseReflectivity * acc * (material.diffuseColor + vec3(0.001f));
149-
return indirect;
150-
151-
152-
}
153-
15490
void main()
15591
{
15692

@@ -209,55 +145,27 @@ void main()
209145
brdf.F0 = vec3(0.04);
210146
brdf.F0 = mix(brdf.F0, brdf.albedo, brdf.metalness);
211147
brdf.emission = g_emission;
212-
// Indirect Component ____________________________
213-
if(settings.vxgi.enabled == 1){
214-
vec3 diffuseIndirect = vec3(0.0);
215-
216-
const float VOXEL_SIZE = 1.0/float(settings.vxgi.resolution);
217148

218149

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;
150+
// Indirect Component ____________________________
151+
if(settings.vxgi.enabled == 1){
242152

153+
vec4 diffuseIndirect = diffuseVoxelGI(voxelMap, modelPos, modelNormal, settings.vxgi, scene.maxCoord.x-scene.minCoord.x);
243154

244-
// indirect = GI(modelPos, modelNormal)*settings.vxgi.strength*g_albedo;
245-
indirect*= settings.enableAO == 1 ? (brdf.ao * SSAO) : brdf.ao;
155+
indirect = diffuseIndirect.rgb * g_albedo;
156+
indirect*= settings.enableAO == 1 ? (brdf.ao * SSAO) : brdf.ao; //Add ambient occlusion
246157

247158
// vec3 specularConeDirection = reflect(normalize(camera.position.xyz-modelPos), modelNormal);
248159
// vec3 specularIndirect = vec3(0.0);
249-
250-
251160
// specularIndirect = traceVoxelCone(voxelMap, startPos, specularConeDirection, max(brdf.roughness, 0.05) , VOXEL_SIZE );
252161
// // specularIndirect = castCone(startPos, specularConeDirection, max(roughness, MIN_SPECULAR_APERTURE), MAX_TRACE_DISTANCE, minLevel).rgb * specColor.rgb * u_indirectSpecularIntensity;
253162
// indirect2+=specularIndirect;
254163
}
255-
256-
257164
//Direct Component ________________________
258165
for(int i = 0; i < scene.numLights; i++) {
259166
//If inside liught area influence
260167
if(isInAreaOfInfluence(scene.lights[i].position, g_pos,scene.lights[i].areaEffect,int(scene.lights[i].type))){
168+
261169
//Direct Component ________________________
262170
vec3 lighting = vec3(0.0);
263171
lighting = evalSchlickSmithBRDF(
@@ -266,7 +174,8 @@ void main()
266174
scene.lights[i].color * computeAttenuation(scene.lights[i].position, g_pos,scene.lights[i].areaEffect,int(scene.lights[i].type)) * scene.lights[i].intensity, //radiance
267175
brdf
268176
);
269-
//Shadow Component ________________________
177+
178+
//Visibility Component ________________________
270179
if(scene.lights[i].shadowCast == 1) {
271180
if(scene.lights[i].shadowType == 0) //Classic
272181
lighting *= computeShadow(shadowMap, scene.lights[i], i, modelPos);
@@ -275,7 +184,7 @@ void main()
275184
if(scene.lights[i].shadowType == 2) //Raytraced
276185
lighting *= computeRaytracedShadow(
277186
TLAS,
278-
blueNoiseMap,
187+
samplerMap,
279188
modelPos,
280189
scene.lights[i].type != DIRECTIONAL_LIGHT ? scene.lights[i].worldPosition.xyz - modelPos : scene.lights[i].shadowData.xyz,
281190
int(scene.lights[i].shadowData.w),

0 commit comments

Comments
 (0)