Skip to content

Commit 37f8c26

Browse files
committed
-Hotfix: Fix in voxelization of the scene
1 parent ee7adf3 commit 37f8c26

File tree

4 files changed

+91
-94
lines changed

4 files changed

+91
-94
lines changed

examples/resources/scenes/sponza.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@
272272
<albedo r="0.5" g="0.5" b="0.5" />
273273
</Material>
274274
</Mesh>
275-
<!-- <Mesh type="file" name="Curtain 0">
275+
<Mesh type="file" name="Curtain 0">
276276
<Filename value="meshes/curtain1.obj" />
277277

278278
<Material type="physical">
@@ -355,8 +355,8 @@
355355
</Textures>
356356
<albedo r="0.5" g="0.5" b="0.5" />
357357
</Material>
358-
</Mesh> -->
359-
<!-- <Mesh type="file" name="Carpet">
358+
</Mesh>
359+
<Mesh type="file" name="Carpet">
360360
<Filename value="meshes/carpet.obj" />
361361

362362
<Material type="physical">
@@ -369,7 +369,7 @@
369369
</Textures>
370370
<albedo r="0.5" g="0.5" b="0.5" />
371371
</Material>
372-
</Mesh> -->
372+
</Mesh>
373373

374374
</Children>
375375
</Mesh>

resources/shaders/VXGI/voxelization.glsl

Lines changed: 70 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,31 @@ layout(location = 1) out vec3 v_normal;
1818
layout(location = 2) out vec2 v_uv;
1919

2020
layout(set = 1, binding = 1) uniform MaterialUniforms {
21-
vec4 slot1;
22-
vec4 slot2;
23-
vec4 slot3;
24-
vec4 slot4;
25-
vec4 slot5;
26-
vec4 slot6;
27-
vec4 slot7;
28-
vec4 slot8;
21+
vec4 slot1;
22+
vec4 slot2;
23+
vec4 slot3;
24+
vec4 slot4;
25+
vec4 slot5;
26+
vec4 slot6;
27+
vec4 slot7;
28+
vec4 slot8;
2929
} material;
3030

31-
3231
void main() {
3332

3433
v_pos = (object.model * vec4(pos, 1.0)).xyz;
35-
v_uv = vec2(uv.x * material.slot2.x, (1-uv.y) * material.slot2.y);
34+
v_uv = vec2(uv.x * material.slot2.x, (1 - uv.y) * material.slot2.y);
3635
v_normal = normalize(mat3(transpose(inverse(object.model))) * normal);
3736

38-
vec3 ndc = mapToZeroOne(v_pos, scene.minCoord.xyz, scene.maxCoord.xyz) * 2.0 -1.0;
37+
vec3 ndc = mapToZeroOne(v_pos, scene.minCoord.xyz, scene.maxCoord.xyz);
38+
ndc.xy = ndc.xy * 2.0 - 1.0;
3939
gl_Position = vec4(ndc, 1.0);
40-
40+
4141
}
4242
#shader geometry
4343
#version 460
4444
#extension GL_NV_geometry_shader_passthrough : enable
4545

46-
4746
layout(triangles) in;
4847
layout(triangle_strip, max_vertices = 3) out;
4948

@@ -55,29 +54,28 @@ layout(location = 0) out vec3 _pos;
5554
layout(location = 1) out vec3 _normal;
5655
layout(location = 2) out vec2 _uv;
5756

58-
void main(){
57+
void main() {
5958

6059
const vec3 p1 = gl_in[1].gl_Position.xyz - gl_in[0].gl_Position.xyz;
6160
const vec3 p2 = gl_in[2].gl_Position.xyz - gl_in[0].gl_Position.xyz;
6261
const vec3 p = abs(cross(p1, p2));
6362

64-
for(uint i = 0; i < 3; ++i){
65-
_pos = v_pos[i];
66-
_normal = v_normal[i];
63+
for(uint i = 0; i < 3; ++i) {
64+
_pos = v_pos[i];
65+
_normal = v_normal[i];
6766
_uv = v_uv[i];
68-
if(p.z > p.x && p.z > p.y){
69-
gl_Position = vec4(gl_in[i].gl_Position.x, gl_in[i].gl_Position.y, 0, 1);
70-
} else if (p.x > p.y && p.x > p.z){
71-
gl_Position = vec4(gl_in[i].gl_Position.y, gl_in[i].gl_Position.z, 0, 1);
72-
} else {
73-
gl_Position = vec4(gl_in[i].gl_Position.x, gl_in[i].gl_Position.z, 0, 1);
74-
}
75-
EmitVertex();
76-
}
67+
if(p.z > p.x && p.z > p.y) {
68+
gl_Position = vec4(gl_in[i].gl_Position.x, gl_in[i].gl_Position.y, 0, 1);
69+
} else if(p.x > p.y && p.x > p.z) {
70+
gl_Position = vec4(gl_in[i].gl_Position.y, gl_in[i].gl_Position.z, 0, 1);
71+
} else {
72+
gl_Position = vec4(gl_in[i].gl_Position.x, gl_in[i].gl_Position.z, 0, 1);
73+
}
74+
EmitVertex();
75+
}
7776
EndPrimitive();
7877
}
7978

80-
8179
#shader fragment
8280
#version 460
8381

@@ -96,30 +94,29 @@ void main(){
9694
#include raytracing.glsl
9795
#include material_defines.glsl
9896

99-
10097
layout(location = 0) in vec3 _pos;
10198
layout(location = 1) in vec3 _normal;
10299
layout(location = 2) in vec2 _uv;
103100

104-
layout(set = 0, binding = 2) uniform sampler2DArray shadowMap;
105-
layout(set = 0, binding = 3) uniform samplerCube irradianceMap;
106-
layout(set = 0, binding = 4) uniform accelerationStructureEXT TLAS;
107-
layout(set = 0, binding = 5) uniform sampler2D samplerMap;
101+
layout(set = 0, binding = 2) uniform sampler2DArray shadowMap;
102+
layout(set = 0, binding = 3) uniform samplerCube irradianceMap;
103+
layout(set = 0, binding = 4) uniform accelerationStructureEXT TLAS;
104+
layout(set = 0, binding = 5) uniform sampler2D samplerMap;
108105

109-
layout(set = 0, binding = 6, r32f) uniform image3D voxelImage;
106+
layout(set = 0, binding = 6, r32f) uniform image3D voxelImage;
110107
#ifdef USE_IMG_ATOMIC_OPERATION
111-
layout(set = 0, binding = 7, r32ui) uniform uimage3D auxVoxelImages[3];
108+
layout(set = 0, binding = 7, r32ui) uniform uimage3D auxVoxelImages[3];
112109
#endif
113110

114111
layout(set = 1, binding = 1) uniform MaterialUniforms {
115-
vec4 slot1;
116-
vec4 slot2;
117-
vec4 slot3;
118-
vec4 slot4;
119-
vec4 slot5;
120-
vec4 slot6;
121-
vec4 slot7;
122-
vec4 slot8;
112+
vec4 slot1;
113+
vec4 slot2;
114+
vec4 slot3;
115+
vec4 slot4;
116+
vec4 slot5;
117+
vec4 slot6;
118+
vec4 slot7;
119+
vec4 slot8;
123120
} material;
124121

125122
layout(set = 2, binding = 0) uniform sampler2D textures[];
@@ -136,97 +133,84 @@ int objectID = int(object.materialID);
136133
///////////////////////////////////////////
137134
//Surface Global properties
138135
///////////////////////////////////////////
139-
vec3 g_albedo = vec3(0.0);
140-
float g_opacity = 1.0;
141-
vec3 g_emisison = vec3(0.0);
136+
vec3 g_albedo = vec3(0.0);
137+
float g_opacity = 1.0;
138+
vec3 g_emisison = vec3(0.0);
142139

143-
void setupSurfaceProperties(){
140+
void setupSurfaceProperties() {
144141

145142
//Depending on material
146-
if(material.slot8.w == PHYSICAL_MATERIAL){
143+
if(material.slot8.w == PHYSICAL_MATERIAL) {
147144

148145
//Setting input surface properties
149-
g_albedo = int(material.slot4.w)== 1 ? mix(material.slot1.rgb, texture(ALBEDO_TEX, _uv).rgb, material.slot3.x) : material.slot1.rgb;
150-
g_opacity = int(material.slot4.w)== 1 ? mix(material.slot1.w, texture(ALBEDO_TEX, _uv).a, material.slot6.z) : material.slot1.w;
146+
g_albedo = int(material.slot4.w) == 1 ? mix(material.slot1.rgb, texture(ALBEDO_TEX, _uv).rgb, material.slot3.x) : material.slot1.rgb;
147+
g_opacity = int(material.slot4.w) == 1 ? mix(material.slot1.w, texture(ALBEDO_TEX, _uv).a, material.slot6.z) : material.slot1.w;
151148
g_emisison = material.slot6.w == 1 ? mix(material.slot7.rgb, texture(MATERIAL_TEX4, _uv).rgb, material.slot7.w) : material.slot7.rgb;
152149
g_emisison *= material.slot8.x;
153150

154151
}
155-
if(material.slot8.w == UNLIT_MATERIAL){
152+
if(material.slot8.w == UNLIT_MATERIAL) {
156153
g_albedo = int(material.slot2.w) == 1 ? texture(ALBEDO_TEX, _uv).rgb : material.slot1.rgb;
157154
}
158-
if(material.slot8.w == HAIR_STRAND_MATERIAL){
155+
if(material.slot8.w == HAIR_STRAND_MATERIAL) {
159156
// TBD .........
160157
}
161-
if(material.slot8.w == PHONG_MATERIAL){
158+
if(material.slot8.w == PHONG_MATERIAL) {
162159
// TBD .........
163160
}
164-
165161

166162
}
167-
ivec3 worldSpaceToVoxelSpace(vec3 worldPos)
168-
{
163+
ivec3 worldSpaceToVoxelSpace(vec3 worldPos) {
169164
vec3 uvw = mapToZeroOne(worldPos, scene.minCoord.xyz, scene.maxCoord.xyz);
170165
ivec3 voxelPos = ivec3(uvw * imageSize(voxelImage));
171166
return voxelPos;
172167
}
173-
vec3 evalDiffuseLighting(vec3 wi,
174-
vec3 radiance)
175-
{
176-
float cosTheta = max(dot(_normal, wi),0.0);
177-
if (cosTheta > 0.0)
178-
{
168+
vec3 evalDiffuseLighting(
169+
vec3 wi,
170+
vec3 radiance
171+
) {
172+
float cosTheta = max(dot(_normal, wi), 0.0);
173+
if(cosTheta > 0.0) {
179174
vec3 diffuse = radiance * cosTheta * g_albedo;
180175
return diffuse;
181176
}
182177
return vec3(0.0);
183178
}
184179

185-
186180
void main() {
187181
setupSurfaceProperties();
188182

189183
vec3 color = vec3(0.0);
190184

191185
for(int i = 0; i < scene.numLights; i++) {
192-
if(isInAreaOfInfluence(scene.lights[i].worldPosition.xyz, _pos,scene.lights[i].areaEffect,int(scene.lights[i].type))){
186+
if(isInAreaOfInfluence(scene.lights[i].worldPosition.xyz, _pos, scene.lights[i].areaEffect, int(scene.lights[i].type))) {
193187

194188
//Diffuse Component ________________________
195189
vec3 lighting = vec3(0.0);
196-
lighting = evalDiffuseLighting(
197-
scene.lights[i].type != DIRECTIONAL_LIGHT ? normalize(scene.lights[i].worldPosition.xyz - _pos) : normalize(-scene.lights[i].worldPosition.xyz), //wi //wo
198-
scene.lights[i].color * computeAttenuation(scene.lights[i].worldPosition.xyz, _pos,scene.lights[i].areaEffect,int(scene.lights[i].type)) * scene.lights[i].intensity
199-
);
190+
lighting = evalDiffuseLighting(scene.lights[i].type != DIRECTIONAL_LIGHT ? normalize(scene.lights[i].worldPosition.xyz - _pos) : normalize(-scene.lights[i].worldPosition.xyz), //wi //wo
191+
scene.lights[i].color * computeAttenuation(scene.lights[i].worldPosition.xyz, _pos, scene.lights[i].areaEffect, int(scene.lights[i].type)) * scene.lights[i].intensity);
200192

201193
//Visibility Component__________________________
202194
if(scene.lights[i].shadowCast == 1) {
203-
if(scene.lights[i].shadowType == 0) //Classic
204-
lighting *= computeShadow(shadowMap, scene.lights[i], i, _pos);
205-
if(scene.lights[i].shadowType == 1) //VSM
206-
lighting *= computeVarianceShadow(shadowMap, scene.lights[i], i, _pos);
207-
if(scene.lights[i].shadowType == 2) //Raytraced
208-
lighting *= computeRaytracedShadow(
209-
TLAS,
210-
samplerMap,
211-
_pos,
212-
scene.lights[i].type != DIRECTIONAL_LIGHT ? scene.lights[i].worldPosition.xyz - _pos : -scene.lights[i].shadowData.xyz,
213-
1,
214-
0.0,
215-
scene.lights[i].type != DIRECTIONAL_LIGHT ? length(scene.lights[i].worldPosition.xyz - _pos) : 30.0,
216-
0);
217-
218-
}
219-
195+
if(scene.lights[i].shadowType == 0) //Classic
196+
lighting *= computeShadow(shadowMap, scene.lights[i], i, _pos);
197+
if(scene.lights[i].shadowType == 1) //VSM
198+
lighting *= computeVarianceShadow(shadowMap, scene.lights[i], i, _pos);
199+
if(scene.lights[i].shadowType == 2) //Raytraced
200+
lighting *= computeRaytracedShadow(TLAS, samplerMap, _pos, scene.lights[i].type != DIRECTIONAL_LIGHT ? scene.lights[i].worldPosition.xyz - _pos : -scene.lights[i].shadowData.xyz, 1, 0.0, scene.lights[i].type != DIRECTIONAL_LIGHT ? length(scene.lights[i].worldPosition.xyz - _pos) : 30.0, 0);
201+
202+
}
203+
220204
color += lighting;
221205
}
222206
}
223207

224208
color += g_emisison;
225209

226210
vec3 ambient = scene.ambientIntensity * scene.ambientColor * g_albedo;
227-
color +=ambient;
211+
color += ambient;
228212

229-
vec4 result = g_opacity * vec4(vec3(color), 1);
213+
vec4 result = g_opacity * vec4(vec3(color), 1);
230214
ivec3 voxelPos = worldSpaceToVoxelSpace(_pos);
231215

232216
#ifdef USE_IMG_ATOMIC_OPERATION

src/core/scene/scene.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,21 @@ void VKFW::Core::Scene::update_AABB() {
4040
}
4141

4242
// 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);
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+
48+
// Step 1: Compute bounding box center and extent
49+
Vec3 center = (m_volume.maxCoords + m_volume.minCoords) * 0.5f;
50+
Vec3 extent = (m_volume.maxCoords - m_volume.minCoords) * 0.5f;
51+
52+
// Step 2: Compute the largest extent (to make it a cube)
53+
float halfSize = std::max({extent.x, extent.y, extent.z});
54+
55+
// Step 3: Expand min and max symmetrically around center
56+
m_volume.minCoords = center - Vec3(halfSize);
57+
m_volume.maxCoords = center + Vec3(halfSize);
4758

4859
m_volume.center = (m_volume.maxCoords + m_volume.minCoords) * 0.5f;
4960
}

src/graphics/command_buffer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ void Graphics::CommandBuffer::copy_image_to_buffer(Image& img, Buffer& buffer) {
445445
vkCmdCopyImageToBuffer(handle, img.handle, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, buffer.handle, 1, &copyRegion);
446446
}
447447
}
448+
448449
void Graphics::CommandBuffer::generate_mipmaps(Image& img, ImageLayout initialLayout, ImageLayout finalLayout) {
449450

450451
int32_t mipWidth = img.extent.width;
@@ -525,4 +526,5 @@ void Graphics::CommandBuffer::generate_mipmaps(Image& img, ImageLayout initialLa
525526

526527
img.currentLayout = finalLayout;
527528
}
529+
528530
VULKAN_ENGINE_NAMESPACE_END

0 commit comments

Comments
 (0)