PBR Progress

This commit is contained in:
2024-10-17 17:47:49 -04:00
parent 0c3d82334b
commit 3009bdbe88
49 changed files with 160 additions and 160 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -57,21 +57,46 @@ namespace fgl::engine
initMaterialDataVec( m_material_data_pool );
constexpr auto offset { 2.0f };
constexpr std::size_t grid_size { 16 };
constexpr float factor_offset { 1.0f / static_cast< float >( grid_size ) };
/*
const std::filesystem::path path {
"/home/kj16609/Desktop/Projects/cxx/Mecha/src/assets/khronos-sponza/Sponza.gltf"
};
for ( std::size_t x = 0; x < grid_size; ++x )
for ( std::size_t y = 0; y < grid_size; ++y )
{
const std::filesystem::path path {
"/home/kj16609/Desktop/Projects/cxx/Mecha/src/assets/PBRSphere.gltf"
};
SceneBuilder builder { *m_vertex_buffer, *m_index_buffer };
SceneBuilder builder { *m_vertex_buffer, *m_index_buffer };
builder.loadScene( path );
builder.loadScene( path );
std::vector< GameObject > objs { builder.getGameObjects() };
std::vector< GameObject > objs { builder.getGameObjects() };
for ( auto& obj : objs )
{
m_game_objects_root.addGameObject( std::move( obj ) );
}*/
for ( auto& obj : objs )
{
auto model_components { obj.getComponents< ModelComponent >() };
for ( const auto& model_component : model_components )
{
auto& prims = ( *model_component )->m_primitives;
for ( auto& prim : prims )
{
auto& pbr { prim.m_material->properties.pbr };
pbr.roughness_factor = x * factor_offset;
pbr.metallic_factor = y * factor_offset;
prim.m_material->update();
}
}
obj.getTransform().translation = WorldCoordinate( x * offset, y * offset, 0.0f );
m_game_objects_root.addGameObject( std::move( obj ) );
}
}*/
}
static Average< float, 60 * 15 > rolling_ms_average;
@@ -242,21 +267,6 @@ namespace fgl::engine
std::cout << "Loading game objects" << std::endl;
auto command_buffer { Device::getInstance().beginSingleTimeCommands() };
{
ZoneScopedN( "Load phyiscs test" );
SceneBuilder builder { *m_vertex_buffer, *m_index_buffer };
builder.loadScene( "assets/PhysicsTest.glb" );
auto objects { builder.getGameObjects() };
for ( auto& object : objects )
{
object.addFlag( IS_VISIBLE | IS_ENTITY );
m_game_objects_root.addGameObject( std::move( object ) );
}
}
Device::getInstance().endSingleTimeCommands( command_buffer );
log::info( "Finished loading game object" );
}

View File

@@ -29,7 +29,7 @@ namespace fgl::engine
data.metallic_texture_id = getTexID( pbr.metallic_roughness_tex );
data.roughness_factor = pbr.roughness_factor;
data.roughness_factor = pbr.roughness_factor;
data.metallic_factor = pbr.metallic_factor;
// Normal
data.normal_texture_id = getTexID( normal.texture );

View File

@@ -108,7 +108,7 @@ namespace fgl::engine
for ( ComponentEngineInterface* comp : components )
{
if ( comp->id() == T::ID ) temp.emplace_back( comp );
if ( comp->id() == T::ID ) temp.emplace_back( dynamic_cast< T* >( comp ) );
}
return temp;

View File

@@ -3,7 +3,6 @@
//
#pragma once
#include <filesystem>
#include <memory>
#include "ComponentIDS.hpp"

View File

@@ -130,6 +130,9 @@ namespace fgl::engine
options.AddMacroDefinition( "NEAR_PLANE", std::to_string( constants::NEAR_PLANE ) );
options.AddMacroDefinition( "FAR_PLANE", std::to_string( constants::FAR_PLANE ) );
// Helpful constants
options.AddMacroDefinition( "PI", std::to_string( std::numbers::pi_v< float > ) );
const shaderc_shader_kind kind { getShaderKindFromName( input_name ) };
const auto preprocessed_source { getInstance().PreprocessGlsl(

View File

@@ -4,6 +4,7 @@
#include "CompositionSystem.hpp"
#include "engine/camera/Camera.hpp"
#include "engine/rendering/pipelines/v2/AttachmentBuilder.hpp"
#include "engine/rendering/pipelines/v2/Pipeline.hpp"
#include "engine/rendering/pipelines/v2/PipelineBuilder.hpp"
@@ -18,6 +19,7 @@ namespace fgl::engine
PipelineBuilder builder { render_pass, SUBPASS };
builder.addDescriptorSet( gbuffer_set );
builder.addDescriptorSet( Camera::getDescriptorLayout() );
builder.addColorAttachment().finish();
@@ -44,6 +46,7 @@ namespace fgl::engine
m_composite_pipeline->bind( command_buffer );
m_composite_pipeline->bindDescriptor( command_buffer, info.getGBufferDescriptor() );
m_composite_pipeline->bindDescriptor( command_buffer, info.getCameraDescriptor() );
return info.command_buffer;
}

View File

@@ -4,32 +4,120 @@ layout (input_attachment_index = 0, binding = 0) uniform subpassInput i_color;
layout (input_attachment_index = 1, binding = 1) uniform subpassInput i_position;
layout (input_attachment_index = 2, binding = 2) uniform subpassInput i_normal;
layout (input_attachment_index = 3, binding = 3) uniform subpassInput i_metallic;
//layout (input_attachment_index = 4, binding = 4) uniform subpassInput i_specular;
layout (input_attachment_index = 4, binding = 4) uniform subpassInput i_emissive;
layout (location = 0) in vec2 in_uv;
layout (location = 0) out vec4 out_color;
layout (set = 1, binding = 0) uniform CameraInfo {
mat4 projection;
mat4 view;
mat4 inverse_view;
} camera_info;
//TODO: uniform binding with sun information
vec3 getCameraPosition()
{
return vec3(
camera_info.inverse_view[3][0],
camera_info.inverse_view[3][1],
camera_info.inverse_view[3][2]
);
}
vec3 sun_dir = vec3(-1.0, -1.0, -1.0);
// GGX/Towbridge-Reitz normal distribution function.
// Uses Disney's reparametrization of alpha = roughness^2.
float ndfGGX(float cosLh, float roughness)
{
float alpha = roughness * roughness;
float alphaSq = alpha * alpha;
float denom = (cosLh * cosLh) * (alphaSq - 1.0) + 1.0;
return alphaSq / (PI * denom * denom);
}
// Single term for separable Schlick-GGX below.
float gaSchlickG1(float cosTheta, float k)
{
return cosTheta / (cosTheta * (1.0 - k) + k);
}
// Schlick-GGX approximation of geometric attenuation function using Smith's method.
float gaSchlickGGX(float cosLi, float cosLo, float roughness)
{
float r = roughness + 1.0;
float k = (r * r) / 8.0;// Epic suggests using this roughness remapping for analytic lights.
return gaSchlickG1(cosLi, k) * gaSchlickG1(cosLo, k);
}
vec3 schlick(vec3 F0, float cosTheta)
{
return F0 + (vec3(1.0) - F0) * pow(1.0 - cosTheta, 5.0);
}
void main()
{
vec3 position = subpassLoad(i_position).xyz;
vec3 normal = subpassLoad(i_normal).xyz;
vec3 color = subpassLoad(i_color).xyz;
const vec3 albedo = subpassLoad(i_color).xyz;
#define ambient 0.5
const vec3 position = subpassLoad(i_position).xyz;
const vec3 normal = subpassLoad(i_normal).xyz;
const vec3 sun_dir = vec3(0.0, 0.0, -1.0);
const vec3 sun_up = -sun_dir;
// metallic, roughness, occlusion
// r, g, b
const vec3 metallic_comb = subpassLoad(i_metallic).xyz;
const float diff = dot(sun_up, normal);
// split the channels of ease of use
const float metallic_value = metallic_comb.r;
const float roughness_value = metallic_comb.g;
const float occlusion = metallic_comb.b;
// Calculate sun light
vec3 sun_color = vec3(1.0);
//vec3 sun_dir = normalize(vec3(0.0, -0.5, 0.5) - position);
//float diff = max(dot(normalize(normal), sun_dir), 0.0);
vec3 diffuse = diff * sun_color;
const vec3 camera_pos = getCameraPosition();
vec3 frag_color = (ambient + diffuse) * color;
// Calculate the vector from the world position to the camera
const vec3 Lo = normalize(camera_pos - position);
const vec3 N = normalize(normal);
out_color = vec4(frag_color, 1.0);
float cosLo = max(dot(N, Lo), 0.0);
// Specular reflection
//vec3 Lr = 2.0 * cosLo * N - Lo;
vec3 Lr = reflect(-Lo, N);
const vec3 fresnel_factor = vec3(0.04);
// Fresnel
vec3 F0 = mix(fresnel_factor, albedo, metallic_value);
vec3 direct_lighting = vec3(0.0);
// Do this for each light
{
vec3 Li = -sun_dir;
vec3 Lradiance = vec3(1.0);// color?
// half vector
vec3 Lh = normalize(Li + Lo);
float cosLi = max(dot(N, Li), 0.0);
float cosLh = max(dot(N, Lh), 0.0);
vec3 F = schlick(F0, max(dot(Lh, Lo), 0.0));
float D = ndfGGX(cosLh, roughness_value);
float G = gaSchlickGGX(cosLi, cosLo, roughness_value);
vec3 kb = mix(vec3(1.0) - F, vec3(0.0), metallic_value);
vec3 diffuse_BRDF = kb * albedo;
vec3 specular_BRDF = (F * D * G) / max(0.04, 4.0 * cosLi * cosLo);
direct_lighting = (diffuse_BRDF + specular_BRDF) * Lradiance * cosLi;
}
vec3 ambient_lighting = albedo * 0.1;
out_color = vec4(direct_lighting + ambient_lighting, 1.0);
}

View File

@@ -2,4 +2,5 @@ layout (location = 0) out vec4 out_color;
layout (location = 1) out vec3 out_position;
layout (location = 2) out vec4 out_normal;
layout (location = 3) out vec3 out_metallic;
//layout (location = 4) out vec3 out_specular;
layout (location = 4) out vec3 out_emissive;

View File

@@ -88,4 +88,17 @@ void main()
float occlusion_scalar = 0.0;
out_metallic = vec3(metallic_scalar, roughness_scalar, occlusion_scalar);
const uint normal_texture_id = materials[mat_id].normal_texture_id;
if (normal_texture_id != INVALID_TEXTURE_ID)
{
vec3 tex_sample = texture(tex[normal_texture_id], in_uv).xyz;
out_normal = vec4(tex_sample * vec3(materials[mat_id].normal_scale), 1.0);
}
else
{
out_normal = vec4(in_normal, 1.0);
}
}

View File

@@ -15,7 +15,7 @@ void main() {
vec4 position_world = instance_model_matrix * vec4(position, 1.0);
gl_Position = ubo.projection * ubo.view * position_world;
out_world_pos = vec3(gl_Position);
out_world_pos = vec3(position_world);
mat3 normal_matrix = transpose(inverse(mat3(instance_model_matrix)));