Skip to content

Commit

Permalink
IBL first step
Browse files Browse the repository at this point in the history
  • Loading branch information
AEspinosaDev committed Oct 28, 2024
1 parent ad9cb04 commit dfdcc65
Show file tree
Hide file tree
Showing 23 changed files with 1,143 additions and 731 deletions.
5 changes: 4 additions & 1 deletion examples/renderer-app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,10 @@ void VulkanRenderer::setup()

TextureHDR* envMap = new TextureHDR();
Tools::Loaders::load_HDRi(envMap, TEXTURE_PATH + "night.hdr");
m_scene->set_skybox(new Skybox(envMap));
Skybox* sky = new Skybox(envMap);
sky->set_rotation(22.0f);
sky->set_intensity(0.25f);
m_scene->set_skybox(sky);

m_controller = new Tools::Controller(camera, m_window);
}
Expand Down
2 changes: 2 additions & 0 deletions include/engine/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
#define JPG "jpg"
#define HAIR "hair"

#define CUBEMAP_FACES 6

/// Simple exception class, which stores a human-readable error description
class VKException : public std::runtime_error
{
Expand Down
2 changes: 1 addition & 1 deletion include/engine/core/renderpasses/forward_pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ForwardPass : public RenderPass

void connect_to_previous_images(std::vector<Graphics::Image> images);

void set_envmap_descriptor(Graphics::Image env);
void set_envmap_descriptor(Graphics::Image env, Graphics::Image irr);
};

} // namespace Core
Expand Down
47 changes: 47 additions & 0 deletions include/engine/core/renderpasses/irradiance_compute_pass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
This file is part of Vulkan-Engine, a simple to use Vulkan based 3D library
MIT License
Copyright (c) 2023 Antonio Espinosa Garcia
*/
#ifndef IRR_COMP_PASS_H
#define IRR_COMP_PASS_H
#include <engine/core/renderpasses/renderpass.h>

VULKAN_ENGINE_NAMESPACE_BEGIN

namespace Core
{

class IrrandianceComputePass : public RenderPass
{
Graphics::DescriptorSet m_captureDescriptorSet;
Graphics::Buffer m_captureBuffer;

public:
IrrandianceComputePass(Graphics::Context *ctx, Extent2D extent)
: RenderPass(ctx, extent, 1, CUBEMAP_FACES, false)
{
}

void init();

void create_descriptors();

void create_graphic_pipelines();

void render(uint32_t frameIndex, Scene *const scene, uint32_t presentImageIndex = 0);

void upload_data(uint32_t frameIndex, Scene *const scene);

void connect_env_cubemap(Graphics::Image env);

void cleanup();
};

} // namespace Core
VULKAN_ENGINE_NAMESPACE_END

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PanoramaConverterPass : public RenderPass

public:
PanoramaConverterPass(Graphics::Context *ctx, Extent2D extent, Mesh *vignette)
: RenderPass(ctx, extent, 1, 6, false), m_vignette(vignette)
: RenderPass(ctx, extent, 1, CUBEMAP_FACES, false), m_vignette(vignette)
{
}

Expand Down
16 changes: 16 additions & 0 deletions include/engine/core/scene/skybox.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Skybox
// Settings
float m_blurriness{0.0f};
float m_intensity{1.0f};
float m_rotation{0.0f};

bool m_updateEnviroment{true};

public:
Expand Down Expand Up @@ -71,6 +73,20 @@ class Skybox
{
m_intensity = i;
}
/*
In degrees
*/
float get_rotation() const
{
return m_rotation;
}
/*
In degrees
*/
void set_rotation(float r)
{
m_rotation = r;
}
bool update_enviroment() const
{
return m_updateEnviroment;
Expand Down
4 changes: 4 additions & 0 deletions include/engine/graphics/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ struct Context
void upload_vertex_arrays(Buffer &vbo, size_t vboSize, const void *vboData, Buffer &ibo, size_t iboSize,
const void *iboData, bool indexed);

void destroy_vertex_arrays(Buffer &vbo, Buffer &ibo, bool indexed);

void upload_texture_image(const void *imgCache, size_t bytesPerPixel, Image *const img, bool mipmapping);

void destroy_texture_image(Image *const img);

/*
MISC
-----------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion include/engine/graphics/shaderpass.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ struct ShaderPassSettings
VK_DYNAMIC_STATE_VIEWPORT,
VK_DYNAMIC_STATE_SCISSOR,
};

// Push Constants
std::vector<VkPushConstantRange> pushConstants = {};
};

struct ShaderPass
Expand Down Expand Up @@ -120,7 +123,7 @@ void build_pipeline_layout(VkDevice &device, DescriptorManager &descriptorManage
void build_pipeline(VkDevice &device, VkRenderPass renderPass, VkExtent2D &extent, ShaderPass &shaderPass);
}; // namespace PipelineBuilder

} // namespace render
} // namespace Graphics

VULKAN_ENGINE_NAMESPACE_END
#endif
9 changes: 8 additions & 1 deletion include/engine/graphics/uniforms.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,14 @@ struct MaterialUniforms
Vec4 dataSlot6;
};

} // namespace render
struct SkyboxUniforms
{
float blurriness;
float intensity;
float rotation;
};

} // namespace Graphics

VULKAN_ENGINE_NAMESPACE_END

Expand Down
35 changes: 32 additions & 3 deletions include/engine/systems/renderers/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#include <engine/common.h>

#include <engine/core/materials/material.h>
#include <engine/core/renderpasses/irradiance_compute_pass.h>
#include <engine/core/renderpasses/panorama_conversion_pass.h>
#include <engine/core/renderpasses/renderpass.h>
#include <engine/core/textures/texture.h>
#include <engine/core/textures/textureLDR.h>
#include <engine/core/windows/window.h>
Expand Down Expand Up @@ -69,6 +69,7 @@ struct RenderPipeline

// Auxiliar passes
Core::PanoramaConverterPass *panoramaConverterPass{nullptr};
Core::IrrandianceComputePass *irradianceComputePass{nullptr};

void push_renderpass(Core::RenderPass *pass)
{
Expand All @@ -82,6 +83,28 @@ struct RenderPipeline
pass->render(frameIndex, scene, presentImageIndex);
}
}
void flush()
{
for (Core::RenderPass *pass : renderpasses)
{
pass->cleanup();
}
if (panoramaConverterPass)
panoramaConverterPass->cleanup();
if (irradianceComputePass)
irradianceComputePass->cleanup();
}
void flush_framebuffers()
{
for (Core::RenderPass *pass : renderpasses)
{
pass->clean_framebuffer();
}
if (panoramaConverterPass)
panoramaConverterPass->clean_framebuffer();
if (irradianceComputePass)
irradianceComputePass->clean_framebuffer();
}
};

/**
Expand Down Expand Up @@ -264,13 +287,19 @@ class BaseRenderer
*/
virtual void update_object_data(Core::Scene *const scene);
/*
Initialize and setup textures and uniforms in given material
Initialize and setup texture IMAGE
*/
virtual void upload_material_textures(Core::IMaterial *const mat);
void upload_texture_image(Core::ITexture *const t);
void destroy_texture_image(Core::ITexture *const t);
/*
Upload geometry vertex buffers to the GPU
*/
void upload_geometry_data(Core::Geometry *const g);
void destroy_geometry_data(Core::Geometry *const g);
/*
Setup skybox
*/
void setup_skybox(Core::Scene *const scene);

#pragma region GUI
/*
Expand Down
2 changes: 1 addition & 1 deletion resources/shaders/forward/physically_based.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ float g_ao;
vec3 computeLighting(LightUniform light) {

//Vector setup
vec3 lightDir = light.type == 0 ? normalize(light.position - v_pos) : normalize(light.position);
vec3 lightDir = light.type != 1 ? normalize(light.position - v_pos) : normalize(light.data.xyz); //Direction in case of directionlal light
vec3 viewDir = normalize(-v_pos);
vec3 halfVector = normalize(lightDir + viewDir); //normalize(viewDir + lightDir);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,55 @@
#shader vertex
#version 460 core

layout (location = 0) in vec3 pos;

out vec3 _pos;

uniform mat4 u_proj;
uniform mat4 u_view;
layout(location = 0) in vec3 pos;

void main()
{
_pos = pos;
gl_Position = u_proj * u_view * vec4(_pos, 1.0);
gl_Position = vec4(pos, 1.0);
}

#shader geometry
#version 460

layout(triangles) in;
layout(triangle_strip, max_vertices = 18) out;

layout(set = 0, binding = 1) uniform CaptureData{
mat4 proj;
mat4 views[6];
} capture;

layout(location = 0) out vec3 _pos;


void main() {

//Generate view array, and choose one on the array

for(int i = 0; i < 6; i++) {

gl_Layer = i;

for (int j = 0; j < 3; j++) {
_pos = (capture.proj * capture.views[i] * gl_in[j].gl_Position).xyz;
gl_Position = vec4(_pos,1.0);
EmitVertex();
}
EndPrimitive();

}
}

#shader fragment
#version 460 core
#version 460 core

#define PI 3.1415926535897932384626433832795

#define PI 3.1415926535897932384626433832795
layout(location = 0) in vec3 _pos;

in vec3 _pos;
out vec4 li;
layout(location = 0) out vec4 li;

uniform samplerCube u_envMap;
layout(set = 0, binding = 0) uniform samplerCube u_envMap;


void main()
Expand Down
10 changes: 4 additions & 6 deletions resources/shaders/panorama_converter.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ layout(triangle_strip, max_vertices = 18) out; //6*3

layout(location = 0) in vec2 texCoord[];


layout(location = 0) out vec2 otexCoord;

void main() {
Expand All @@ -29,16 +30,13 @@ void main() {
gl_Layer = i;


for (int i = 0; i < 3; i++) {
gl_Position = gl_in[i].gl_Position;
otexCoord = texCoord[i];
for (int j = 0; j < 3; j++) {
gl_Position = gl_in[j].gl_Position;
otexCoord = texCoord[j];
EmitVertex();
}
EndPrimitive();


EndPrimitive();

}
}

Expand Down
28 changes: 26 additions & 2 deletions resources/shaders/skybox.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,29 @@ layout(location = 0) in vec3 position;

layout(location = 0) out vec3 _uv;

layout(push_constant) uniform SkyboxConstants {
float blurriness;
float intensity;
float rotation;
} skybox;

mat4 rotationY(float angle) {
float radAngle = radians(angle);
float c = cos(angle);
float s = sin(angle);

return mat4(
c, 0.0, s, 0.0,
0.0, 1.0, 0.0, 0.0,
-s, 0.0, c, 0.0,
0.0, 0.0, 0.0, 1.0
);
}

void main()
{
_uv = position;
vec4 outPos = camera.proj * mat4(mat3(camera.view)) * vec4(position, 1.0); //Take out translation of the equation
vec4 outPos = camera.proj * mat4(mat3(camera.view)) * rotationY(skybox.rotation) * vec4(position, 1.0); //Take out translation of the equation
gl_Position = outPos.xyww;
}

Expand All @@ -22,10 +40,16 @@ layout(location = 0) in vec3 _uv;

layout(location = 0) out vec4 fragColor;

layout(push_constant) uniform SkyboxConstants {
float blurriness;
float intensity;
float rotation;
} skybox;

layout(set = 0, binding = 3) uniform samplerCube u_skymap;

void main()
{
vec3 color =texture(u_skymap, _uv).rgb;
vec3 color =texture(u_skymap, _uv).rgb * skybox.intensity;
fragColor = vec4(color,1.0);
}
Loading

0 comments on commit dfdcc65

Please sign in to comment.