Skip to content

Commit 856751d

Browse files
committed
-Dev: working on VXGI (Voxelization of scene)
1 parent 960592c commit 856751d

33 files changed

+201
-83
lines changed

examples/resources/scenes/sponza.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@
291291
<translate x="0.0" y="50.0" z="0.0" />
292292
</Transform>
293293

294-
<intensity value="0.45" />
294+
<intensity value="2" />
295295
<color r="0.14" g="0.37" b="0.58" />
296296
<direction x="2.0" y="20.0" z="5.0" />
297297

@@ -358,7 +358,7 @@
358358
</Light>
359359
<Enviroment type="skybox">
360360
<Filename value="textures/moon.hdr" />
361-
<intensity value="0.3" />
361+
<intensity value="1.0" />
362362
<color r="0.14" g="0.37" b="0.58" />
363363
</Enviroment>
364364

include/engine/core/scene/scene.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,15 @@ class Scene : public Object3D
177177
inline void update_AS(bool op) {
178178
m_updateAccel = op;
179179
}
180-
void compute_limits();
180+
181+
/*
182+
Setup axis-aligned bounding volume for the entire scene. This object might be necessary for some functionalities,
183+
sush as voxelization of the scene.
184+
*/
185+
void setup_AABB();
186+
inline AABB get_AABB() const {
187+
return m_volume;
188+
}
181189
};
182190

183191
void set_meshes(Scene* const scene, std::vector<Mesh*> meshes);

include/engine/graphics/uniforms.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ struct CameraUniforms {
3131

3232
struct LightUniforms {
3333

34-
Vec4 position = {0.0f, 0.0f, 0.0f, 0.0f}; // w for type
35-
Vec4 color = {0.0f, 0.0f, 0.0f, 0.0f};
36-
Vec4 dataSlot1 = {0.0f, 0.0f, 0.0f, 0.0f};
34+
Vec4 position = {0.0f, 0.0f, 0.0f, 0.0f}; // w for type
35+
Vec4 worldPosition = {0.0f, 0.0f, 0.0f, 0.0f}; // w for type
36+
Vec4 color = {0.0f, 0.0f, 0.0f, 0.0f};
37+
Vec4 dataSlot1 = {0.0f, 0.0f, 0.0f, 0.0f};
3738
Mat4 viewProj;
3839
Vec4 dataSlot2 = {0.0f, 0.0f, 0.0f, 0.0f};
3940
Vec4 dataSlot3 = {0.0f, 0.0f, 0.0f, 0.0f};
@@ -43,6 +44,8 @@ struct SceneUniforms {
4344
Vec4 fogColorAndSSAO; // w is for enabling SSAO
4445
Vec4 fogParams; // x for near, y for far, z for intensity, w enable.
4546
Vec4 ambientColor; // w intensity
47+
Vec4 maxCoord;
48+
Vec4 minCoord;
4649
LightUniforms lightUniforms[ENGINE_MAX_LIGHTS];
4750
int numLights;
4851
int SSAOtype;
Lines changed: 110 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33

44
#include camera.glsl
55
#include object.glsl
6+
#include light.glsl
7+
#include scene.glsl
8+
#include utils.glsl
69

710
//Input
811
layout(location = 0) in vec3 pos;
912
layout(location = 1) in vec3 normal;
1013
layout(location = 2) in vec2 uv;
11-
layout(location = 3) in vec3 tangent;
1214

1315
//Output
1416
layout(location = 0) out vec3 v_pos;
1517
layout(location = 1) out vec3 v_normal;
1618
layout(location = 2) out vec2 v_uv;
17-
layout(location = 3) out mat3 v_TBN;
1819

1920
layout(set = 1, binding = 1) uniform MaterialUniforms {
2021
vec4 slot1;
@@ -29,39 +30,71 @@ layout(set = 1, binding = 1) uniform MaterialUniforms {
2930

3031

3132

32-
void main() {
33-
34-
gl_Position = camera.viewProj * object.model * vec4(pos, 1.0);
3533

36-
v_uv = vec2(uv.x * material.slot2.x, (1-uv.y) * material.slot2.y); //Tiling
34+
void main() {
3735

38-
mat4 mv = camera.view * object.model;
39-
v_pos = (mv * vec4(pos, 1.0)).xyz;
36+
v_pos = (object.model * vec4(pos, 1.0)).xyz;
37+
v_uv = vec2(uv.x * material.slot2.x, (1-uv.y) * material.slot2.y);
38+
v_normal = normalize(mat3(transpose(inverse(object.model))) * normal);
4039

41-
v_normal = normalize(mat3(transpose(inverse(mv))) * normal);
40+
vec3 ndc = mapToZeroOne(v_pos, scene.minCoord.xyz, scene.maxCoord.xyz) * 2.0 -1.0;
41+
gl_Position = vec4(ndc, 1.0);
42+
// gl_Position = camera.viewProj * vec4(v_pos, 1.0);
43+
44+
}
45+
#shader geometry
46+
#version 460
4247

43-
if(int(material.slot5.x) == 1) { //If has normal texture
44-
vec3 T = normalize(vec3(mv * vec4(tangent, 0.0)));
45-
vec3 N = normalize(vec3(mv * vec4(normal, 0.0)));
46-
vec3 B = cross(N, T);
47-
v_TBN = mat3(T, B, N);
48-
}
48+
layout(triangles) in;
49+
layout(triangle_strip, max_vertices = 3) out;
50+
51+
layout(location = 0) in vec3 v_pos[];
52+
layout(location = 1) in vec3 v_normal[];
53+
layout(location = 2) in vec2 v_uv[];
54+
55+
layout(location = 0) out vec3 _pos;
56+
layout(location = 1) out vec3 _normal;
57+
layout(location = 2) out vec2 _uv;
58+
59+
void main(){
60+
61+
const vec3 p1 = gl_in[1].gl_Position.xyz - gl_in[0].gl_Position.xyz;
62+
const vec3 p2 = gl_in[2].gl_Position.xyz - gl_in[0].gl_Position.xyz;
63+
const vec3 p = abs(cross(p1, p2));
64+
65+
for(uint i = 0; i < 3; ++i){
66+
_pos = v_pos[i];
67+
_normal = v_normal[i];
68+
_uv = v_uv[i];
69+
if(p.z > p.x && p.z > p.y){
70+
gl_Position = vec4(gl_in[i].gl_Position.x, gl_in[i].gl_Position.y, 0, 1);
71+
} else if (p.x > p.y && p.x > p.z){
72+
gl_Position = vec4(gl_in[i].gl_Position.y, gl_in[i].gl_Position.z, 0, 1);
73+
} else {
74+
gl_Position = vec4(gl_in[i].gl_Position.x, gl_in[i].gl_Position.z, 0, 1);
75+
}
76+
EmitVertex();
77+
}
78+
EndPrimitive();
4979
}
5080

81+
5182
#shader fragment
5283
#version 460
84+
#include light.glsl
85+
#include scene.glsl
86+
#include utils.glsl
5387
#include material_defines.glsl
5488

55-
layout(location = 0) in vec3 v_pos;
56-
layout(location = 1) in vec3 v_normal;
57-
layout(location = 2) in vec2 v_uv;
58-
layout(location = 3) in mat3 v_TBN;
89+
layout(location = 0) in vec3 _pos;
90+
layout(location = 1) in vec3 _normal;
91+
layout(location = 2) in vec2 _uv;
5992

6093
layout(set = 0, binding = 2) uniform sampler2DArray shadowMap;
6194
layout(set = 0, binding = 3) uniform samplerCube irradianceMap;
6295
// layout(set = 0, binding = 4) uniform accelerationStructureEXT TLAS;
6396
layout(set = 0, binding = 5) uniform sampler2D blueNoiseMap;
64-
layout(set = 0, binding = 6, r32f) uniform image3D voxelImage;
97+
layout(set = 0, binding = 6, r32f) uniform image3D voxelImage;
6598

6699
layout(set = 1, binding = 1) uniform MaterialUniforms {
67100
vec4 slot1;
@@ -82,13 +115,6 @@ layout(set = 2, binding = 3) uniform sampler2D materialText2;
82115
layout(set = 2, binding = 4) uniform sampler2D materialText3;
83116
layout(set = 2, binding = 5) uniform sampler2D materialText4;
84117

85-
//Output
86-
// layout(location = 0) out vec4 outPos;
87-
// layout(location = 1) out vec4 outNormal;
88-
// layout(location = 2) out vec4 outAlbedo;
89-
// layout(location = 3) out vec4 outMaterial; //U8
90-
// layout(location = 4) out vec4 outEmissionF; //F32
91-
// layout(location = 5) out vec4 outTemporal;
92118

93119
#define EPSILON 0.1
94120

@@ -108,12 +134,11 @@ void setupSurfaceProperties(){
108134
if(material.slot8.w == PHYSICAL_MATERIAL){
109135

110136
//Setting input surface properties
111-
g_albedo = int(material.slot4.w)== 1 ? mix(material.slot1.rgb, texture(albedoTex, v_uv).rgb, material.slot3.x) : material.slot1.rgb;
112-
g_opacity = int(material.slot4.w)== 1 ? mix(material.slot1.w, texture(albedoTex, v_uv).a, material.slot6.z) : material.slot1.w;
113-
g_normal = int(material.slot5.x)== 1 ? normalize((v_TBN * (texture(normalTex, v_uv).rgb * 2.0 - 1.0))) : normalize( v_normal );
137+
g_albedo = int(material.slot4.w)== 1 ? mix(material.slot1.rgb, texture(albedoTex, _uv).rgb, material.slot3.x) : material.slot1.rgb;
138+
g_opacity = int(material.slot4.w)== 1 ? mix(material.slot1.w, texture(albedoTex, _uv).a, material.slot6.z) : material.slot1.w;
114139

115140
if(int(material.slot6.x)== 1) {
116-
vec4 mask = texture(materialText1, v_uv).rgba; //Correction linearize color
141+
vec4 mask = texture(materialText1, _uv).rgba; //Correction linearize color
117142
if(int(material.slot6.y) == 0) { //HDRP UNITY
118143
//Unity HDRP uses glossiness not roughness pipeline, so it has to be inversed
119144
g_material.r = 1.0 - mix(material.slot3.w,mask.a, material.slot4.x);
@@ -127,12 +152,12 @@ void setupSurfaceProperties(){
127152
// TO DO ...
128153
}
129154
} else {
130-
g_material.r = material.slot5.y== 1 ? mix(material.slot3.w, texture(materialText1, v_uv).r, material.slot4.x) : material.slot3.w; //Roughness
131-
g_material.g = material.slot5.z== 1 ? mix(material.slot3.y, texture(materialText2, v_uv).r, material.slot3.z) : material.slot3.y; //Metalness
132-
g_material.b = material.slot5.w== 1 ? mix(material.slot4.y, texture(materialText3, v_uv).r, material.slot4.z) : material.slot4.y; //AO
155+
g_material.r = material.slot5.y== 1 ? mix(material.slot3.w, texture(materialText1, _uv).r, material.slot4.x) : material.slot3.w; //Roughness
156+
g_material.g = material.slot5.z== 1 ? mix(material.slot3.y, texture(materialText2, _uv).r, material.slot3.z) : material.slot3.y; //Metalness
157+
g_material.b = material.slot5.w== 1 ? mix(material.slot4.y, texture(materialText3, _uv).r, material.slot4.z) : material.slot4.y; //AO
133158
}
134159

135-
g_emisison = material.slot6.w == 1 ? mix(material.slot7.rgb, texture(materialText4, v_uv).rgb, material.slot7.w) : material.slot7.rgb;
160+
g_emisison = material.slot6.w == 1 ? mix(material.slot7.rgb, texture(materialText4, _uv).rgb, material.slot7.w) : material.slot7.rgb;
136161
g_emisison *= material.slot8.x;
137162

138163
g_fresnelThreshold = material.slot8.y;
@@ -141,7 +166,7 @@ void setupSurfaceProperties(){
141166

142167
}
143168
if(material.slot8.w == UNLIT_MATERIAL){
144-
g_albedo = int(material.slot2.w) == 1 ? texture(albedoTex, v_uv).rgb : material.slot1.rgb;
169+
g_albedo = int(material.slot2.w) == 1 ? texture(albedoTex, _uv).rgb : material.slot1.rgb;
145170
g_material.w = UNLIT_MATERIAL;
146171
}
147172
if(material.slot8.w == HAIR_STRAND_MATERIAL){
@@ -155,12 +180,59 @@ void setupSurfaceProperties(){
155180

156181

157182
}
183+
ivec3 worldSpaceToVoxelSpace(vec3 worldPos)
184+
{
185+
vec3 uvw = mapToZeroOne(worldPos, scene.minCoord.xyz, scene.maxCoord.xyz);
186+
ivec3 voxelPos = ivec3(uvw * imageSize(voxelImage));
187+
return voxelPos;
188+
}
189+
// vec3 evaluateDiffuseLighting(Light light)
190+
// {
191+
192+
// float cosTheta = dot(normalize(inData.Normal), lightDir);
193+
// if (cosTheta > 0.0)
194+
// {
195+
// vec3 diffuse = light.Color * cosTheta * albedo;
196+
// float attenuation = GetAttenuationFactor(dist * dist, light.Radius);
197+
198+
// return diffuse * attenuation;
199+
// }
200+
201+
// return vec3(0.0);
202+
// }
203+
158204

159205

160206
void main() {
161207

162-
// setupSurfaceProperties();
208+
209+
setupSurfaceProperties();
210+
211+
vec3 color = vec3(0.0);
212+
213+
for(int i = 0; i < scene.numLights; i++) {
214+
if(isInAreaOfInfluence(scene.lights[i], _pos)){
215+
216+
//Diffuse Component ________________________
217+
218+
// vec3 lighting = vec3(0.0);
219+
// lighting = evalSchlickSmithBRDF(
220+
// scene.lights[i].type != DIRECTIONAL_LIGHT ? normalize(scene.lights[i].position - g_pos) : normalize(scene.lights[i].position.xyz), //wi
221+
// normalize(-g_pos), //wo
222+
// scene.lights[i].color * computeAttenuation( scene.lights[i], g_pos) * scene.lights[i].intensity, //radiance
223+
// brdf
224+
// );
225+
226+
// color += lighting;
227+
}
228+
}
229+
230+
color = g_albedo;
231+
color += g_emisison;
232+
163233

164-
imageStore(voxelImage, ivec3(gl_FragCoord.x,gl_FragCoord.y ,0), vec4(1.0));
234+
vec4 result = g_opacity * vec4(vec3(color), 1);
235+
ivec3 voxelPos = worldSpaceToVoxelSpace(_pos);
236+
imageStore(voxelImage, voxelPos, result);
165237

166238
}

resources/shaders/deferred/composition.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ void main()
166166
TLAS,
167167
blueNoiseMap,
168168
modelPos,
169-
scene.lights[i].type != DIRECTIONAL_LIGHT ? scene.lights[i].shadowData.xyz - modelPos : scene.lights[i].shadowData.xyz,
169+
scene.lights[i].type != DIRECTIONAL_LIGHT ? scene.lights[i].worldPosition.xyz - modelPos : scene.lights[i].shadowData.xyz,
170170
int(scene.lights[i].shadowData.w),
171171
scene.lights[i].area,
172172
0);
File renamed without changes.

resources/shaders/scripts/BRDFs/marschner_LUT_BSDF.glsl renamed to resources/shaders/include/BRDFs/marschner_LUT_BSDF.glsl

File renamed without changes.

0 commit comments

Comments
 (0)