Skip to content

Commit

Permalink
-Dev: working on VXGI (Voxelization of scene)
Browse files Browse the repository at this point in the history
  • Loading branch information
AEspinosaDev committed Jan 9, 2025
1 parent 960592c commit 856751d
Show file tree
Hide file tree
Showing 33 changed files with 201 additions and 83 deletions.
4 changes: 2 additions & 2 deletions examples/resources/scenes/sponza.xml
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@
<translate x="0.0" y="50.0" z="0.0" />
</Transform>

<intensity value="0.45" />
<intensity value="2" />
<color r="0.14" g="0.37" b="0.58" />
<direction x="2.0" y="20.0" z="5.0" />

Expand Down Expand Up @@ -358,7 +358,7 @@
</Light>
<Enviroment type="skybox">
<Filename value="textures/moon.hdr" />
<intensity value="0.3" />
<intensity value="1.0" />
<color r="0.14" g="0.37" b="0.58" />
</Enviroment>

Expand Down
10 changes: 9 additions & 1 deletion include/engine/core/scene/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,15 @@ class Scene : public Object3D
inline void update_AS(bool op) {
m_updateAccel = op;
}
void compute_limits();

/*
Setup axis-aligned bounding volume for the entire scene. This object might be necessary for some functionalities,
sush as voxelization of the scene.
*/
void setup_AABB();
inline AABB get_AABB() const {
return m_volume;
}
};

void set_meshes(Scene* const scene, std::vector<Mesh*> meshes);
Expand Down
9 changes: 6 additions & 3 deletions include/engine/graphics/uniforms.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ struct CameraUniforms {

struct LightUniforms {

Vec4 position = {0.0f, 0.0f, 0.0f, 0.0f}; // w for type
Vec4 color = {0.0f, 0.0f, 0.0f, 0.0f};
Vec4 dataSlot1 = {0.0f, 0.0f, 0.0f, 0.0f};
Vec4 position = {0.0f, 0.0f, 0.0f, 0.0f}; // w for type
Vec4 worldPosition = {0.0f, 0.0f, 0.0f, 0.0f}; // w for type
Vec4 color = {0.0f, 0.0f, 0.0f, 0.0f};
Vec4 dataSlot1 = {0.0f, 0.0f, 0.0f, 0.0f};
Mat4 viewProj;
Vec4 dataSlot2 = {0.0f, 0.0f, 0.0f, 0.0f};
Vec4 dataSlot3 = {0.0f, 0.0f, 0.0f, 0.0f};
Expand All @@ -43,6 +44,8 @@ struct SceneUniforms {
Vec4 fogColorAndSSAO; // w is for enabling SSAO
Vec4 fogParams; // x for near, y for far, z for intensity, w enable.
Vec4 ambientColor; // w intensity
Vec4 maxCoord;
Vec4 minCoord;
LightUniforms lightUniforms[ENGINE_MAX_LIGHTS];
int numLights;
int SSAOtype;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@

#include camera.glsl
#include object.glsl
#include light.glsl
#include scene.glsl
#include utils.glsl

//Input
layout(location = 0) in vec3 pos;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 uv;
layout(location = 3) in vec3 tangent;

//Output
layout(location = 0) out vec3 v_pos;
layout(location = 1) out vec3 v_normal;
layout(location = 2) out vec2 v_uv;
layout(location = 3) out mat3 v_TBN;

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



void main() {

gl_Position = camera.viewProj * object.model * vec4(pos, 1.0);

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

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

v_normal = normalize(mat3(transpose(inverse(mv))) * normal);
vec3 ndc = mapToZeroOne(v_pos, scene.minCoord.xyz, scene.maxCoord.xyz) * 2.0 -1.0;
gl_Position = vec4(ndc, 1.0);
// gl_Position = camera.viewProj * vec4(v_pos, 1.0);

}
#shader geometry
#version 460

if(int(material.slot5.x) == 1) { //If has normal texture
vec3 T = normalize(vec3(mv * vec4(tangent, 0.0)));
vec3 N = normalize(vec3(mv * vec4(normal, 0.0)));
vec3 B = cross(N, T);
v_TBN = mat3(T, B, N);
}
layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;

layout(location = 0) in vec3 v_pos[];
layout(location = 1) in vec3 v_normal[];
layout(location = 2) in vec2 v_uv[];

layout(location = 0) out vec3 _pos;
layout(location = 1) out vec3 _normal;
layout(location = 2) out vec2 _uv;

void main(){

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

for(uint i = 0; i < 3; ++i){
_pos = v_pos[i];
_normal = v_normal[i];
_uv = v_uv[i];
if(p.z > p.x && p.z > p.y){
gl_Position = vec4(gl_in[i].gl_Position.x, gl_in[i].gl_Position.y, 0, 1);
} else if (p.x > p.y && p.x > p.z){
gl_Position = vec4(gl_in[i].gl_Position.y, gl_in[i].gl_Position.z, 0, 1);
} else {
gl_Position = vec4(gl_in[i].gl_Position.x, gl_in[i].gl_Position.z, 0, 1);
}
EmitVertex();
}
EndPrimitive();
}


#shader fragment
#version 460
#include light.glsl
#include scene.glsl
#include utils.glsl
#include material_defines.glsl

layout(location = 0) in vec3 v_pos;
layout(location = 1) in vec3 v_normal;
layout(location = 2) in vec2 v_uv;
layout(location = 3) in mat3 v_TBN;
layout(location = 0) in vec3 _pos;
layout(location = 1) in vec3 _normal;
layout(location = 2) in vec2 _uv;

layout(set = 0, binding = 2) uniform sampler2DArray shadowMap;
layout(set = 0, binding = 3) uniform samplerCube irradianceMap;
// layout(set = 0, binding = 4) uniform accelerationStructureEXT TLAS;
layout(set = 0, binding = 5) uniform sampler2D blueNoiseMap;
layout(set = 0, binding = 6, r32f) uniform image3D voxelImage;
layout(set = 0, binding = 6, r32f) uniform image3D voxelImage;

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

//Output
// layout(location = 0) out vec4 outPos;
// layout(location = 1) out vec4 outNormal;
// layout(location = 2) out vec4 outAlbedo;
// layout(location = 3) out vec4 outMaterial; //U8
// layout(location = 4) out vec4 outEmissionF; //F32
// layout(location = 5) out vec4 outTemporal;

#define EPSILON 0.1

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

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

if(int(material.slot6.x)== 1) {
vec4 mask = texture(materialText1, v_uv).rgba; //Correction linearize color
vec4 mask = texture(materialText1, _uv).rgba; //Correction linearize color
if(int(material.slot6.y) == 0) { //HDRP UNITY
//Unity HDRP uses glossiness not roughness pipeline, so it has to be inversed
g_material.r = 1.0 - mix(material.slot3.w,mask.a, material.slot4.x);
Expand All @@ -127,12 +152,12 @@ void setupSurfaceProperties(){
// TO DO ...
}
} else {
g_material.r = material.slot5.y== 1 ? mix(material.slot3.w, texture(materialText1, v_uv).r, material.slot4.x) : material.slot3.w; //Roughness
g_material.g = material.slot5.z== 1 ? mix(material.slot3.y, texture(materialText2, v_uv).r, material.slot3.z) : material.slot3.y; //Metalness
g_material.b = material.slot5.w== 1 ? mix(material.slot4.y, texture(materialText3, v_uv).r, material.slot4.z) : material.slot4.y; //AO
g_material.r = material.slot5.y== 1 ? mix(material.slot3.w, texture(materialText1, _uv).r, material.slot4.x) : material.slot3.w; //Roughness
g_material.g = material.slot5.z== 1 ? mix(material.slot3.y, texture(materialText2, _uv).r, material.slot3.z) : material.slot3.y; //Metalness
g_material.b = material.slot5.w== 1 ? mix(material.slot4.y, texture(materialText3, _uv).r, material.slot4.z) : material.slot4.y; //AO
}

g_emisison = material.slot6.w == 1 ? mix(material.slot7.rgb, texture(materialText4, v_uv).rgb, material.slot7.w) : material.slot7.rgb;
g_emisison = material.slot6.w == 1 ? mix(material.slot7.rgb, texture(materialText4, _uv).rgb, material.slot7.w) : material.slot7.rgb;
g_emisison *= material.slot8.x;

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

}
if(material.slot8.w == UNLIT_MATERIAL){
g_albedo = int(material.slot2.w) == 1 ? texture(albedoTex, v_uv).rgb : material.slot1.rgb;
g_albedo = int(material.slot2.w) == 1 ? texture(albedoTex, _uv).rgb : material.slot1.rgb;
g_material.w = UNLIT_MATERIAL;
}
if(material.slot8.w == HAIR_STRAND_MATERIAL){
Expand All @@ -155,12 +180,59 @@ void setupSurfaceProperties(){


}
ivec3 worldSpaceToVoxelSpace(vec3 worldPos)
{
vec3 uvw = mapToZeroOne(worldPos, scene.minCoord.xyz, scene.maxCoord.xyz);
ivec3 voxelPos = ivec3(uvw * imageSize(voxelImage));
return voxelPos;
}
// vec3 evaluateDiffuseLighting(Light light)
// {

// float cosTheta = dot(normalize(inData.Normal), lightDir);
// if (cosTheta > 0.0)
// {
// vec3 diffuse = light.Color * cosTheta * albedo;
// float attenuation = GetAttenuationFactor(dist * dist, light.Radius);

// return diffuse * attenuation;
// }

// return vec3(0.0);
// }



void main() {

// setupSurfaceProperties();

setupSurfaceProperties();

vec3 color = vec3(0.0);

for(int i = 0; i < scene.numLights; i++) {
if(isInAreaOfInfluence(scene.lights[i], _pos)){

//Diffuse Component ________________________

// vec3 lighting = vec3(0.0);
// lighting = evalSchlickSmithBRDF(
// scene.lights[i].type != DIRECTIONAL_LIGHT ? normalize(scene.lights[i].position - g_pos) : normalize(scene.lights[i].position.xyz), //wi
// normalize(-g_pos), //wo
// scene.lights[i].color * computeAttenuation( scene.lights[i], g_pos) * scene.lights[i].intensity, //radiance
// brdf
// );

// color += lighting;
}
}

color = g_albedo;
color += g_emisison;


imageStore(voxelImage, ivec3(gl_FragCoord.x,gl_FragCoord.y ,0), vec4(1.0));
vec4 result = g_opacity * vec4(vec3(color), 1);
ivec3 voxelPos = worldSpaceToVoxelSpace(_pos);
imageStore(voxelImage, voxelPos, result);

}
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion resources/shaders/deferred/composition.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ void main()
TLAS,
blueNoiseMap,
modelPos,
scene.lights[i].type != DIRECTIONAL_LIGHT ? scene.lights[i].shadowData.xyz - modelPos : scene.lights[i].shadowData.xyz,
scene.lights[i].type != DIRECTIONAL_LIGHT ? scene.lights[i].worldPosition.xyz - modelPos : scene.lights[i].shadowData.xyz,
int(scene.lights[i].shadowData.w),
scene.lights[i].area,
0);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ struct LightUniform{
vec3 position;
float type;

vec3 worldPosition;
float unused;

vec3 color;
float intensity;

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ layout(set = 0, binding = 1) uniform SceneUniforms {
vec3 ambientColor;
float ambientIntensity;

vec4 maxCoord;
vec4 minCoord;

LightUniform lights[MAX_LIGHTS];

int numLights;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,15 @@ vec3 shiftTangent(vec3 T, vec3 N, float shift) {

float getLuminance(vec3 li) {
return 0.2126 * li.r + 0.7152 * li.g + 0.0722 * li.b;
}


vec3 mapRangeToAnOther(vec3 value, vec3 valueMin, vec3 valueMax, vec3 mapMin, vec3 mapMax)
{
return (value - valueMin) / (valueMax - valueMin) * (mapMax - mapMin) + mapMin;
}

vec3 mapToZeroOne(vec3 value, vec3 rangeMin, vec3 rangeMax)
{
return mapRangeToAnOther(value, rangeMin, rangeMax, vec3(0.0), vec3(1.0));
}
File renamed without changes.
6 changes: 3 additions & 3 deletions src/core/passes/bloom_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void BloomPass::setup_shader_passes() {
const uint32_t SETTINGS_UNIFORM_SIZE = sizeof(float);

ComputeShaderPass* downsamplePass =
new ComputeShaderPass(m_device->get_handle(), ENGINE_RESOURCES_PATH "shaders/compute/downsample.glsl");
new ComputeShaderPass(m_device->get_handle(), ENGINE_RESOURCES_PATH "shaders/bloom/downsample.glsl");
downsamplePass->settings.descriptorSetLayoutIDs = {{GLOBAL_LAYOUT, true}};
downsamplePass->settings.pushConstants.push_back(PushConstant(SHADER_STAGE_COMPUTE, MIPMAP_UNIFORM_SIZE));

Expand All @@ -80,7 +80,7 @@ void BloomPass::setup_shader_passes() {
m_shaderPasses["downsample"] = downsamplePass;

ComputeShaderPass* upsamplePass =
new ComputeShaderPass(m_device->get_handle(), ENGINE_RESOURCES_PATH "shaders/compute/upsample.glsl");
new ComputeShaderPass(m_device->get_handle(), ENGINE_RESOURCES_PATH "shaders/bloom/upsample.glsl");
upsamplePass->settings.descriptorSetLayoutIDs = {{GLOBAL_LAYOUT, true}};
upsamplePass->settings.pushConstants.push_back(PushConstant(SHADER_STAGE_COMPUTE, MIPMAP_UNIFORM_SIZE));

Expand All @@ -90,7 +90,7 @@ void BloomPass::setup_shader_passes() {
m_shaderPasses["upsample"] = upsamplePass;

GraphicShaderPass* bloomPass = new GraphicShaderPass(
m_device->get_handle(), m_renderpass, m_imageExtent, ENGINE_RESOURCES_PATH "shaders/misc/bloom.glsl");
m_device->get_handle(), m_renderpass, m_imageExtent, ENGINE_RESOURCES_PATH "shaders/bloom/compose.glsl");
bloomPass->settings.descriptorSetLayoutIDs = {{GLOBAL_LAYOUT, true}};
bloomPass->graphicSettings.attributes = {{POSITION_ATTRIBUTE, true},
{NORMAL_ATTRIBUTE, false},
Expand Down
4 changes: 2 additions & 2 deletions src/core/passes/voxelization_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ void VoxelizationPass::setup_uniforms(std::vector<Graphics::Frame>& frames) {
void VoxelizationPass::setup_shader_passes() {

GraphicShaderPass* voxelPass = new GraphicShaderPass(
m_device->get_handle(), m_renderpass, m_imageExtent, ENGINE_RESOURCES_PATH "shaders/misc/voxelization.glsl");
m_device->get_handle(), m_renderpass, m_imageExtent, ENGINE_RESOURCES_PATH "shaders/VXGI/voxelization.glsl");
voxelPass->settings.descriptorSetLayoutIDs = {
{GLOBAL_LAYOUT, true}, {OBJECT_LAYOUT, true}, {OBJECT_TEXTURE_LAYOUT, true}};
voxelPass->graphicSettings.attributes = {{POSITION_ATTRIBUTE, true},
{NORMAL_ATTRIBUTE, true},
{UV_ATTRIBUTE, true},
{TANGENT_ATTRIBUTE, true},
{TANGENT_ATTRIBUTE, false},
{COLOR_ATTRIBUTE, false}};
voxelPass->graphicSettings.dynamicStates = {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR};
VkPipelineColorBlendAttachmentState state = Init::color_blend_attachment_state(false);
Expand Down
Loading

0 comments on commit 856751d

Please sign in to comment.