diff --git a/src/engine/assets/model/Primitive.cpp b/src/engine/assets/model/Primitive.cpp index fcdc2c7..d63ac52 100644 --- a/src/engine/assets/model/Primitive.cpp +++ b/src/engine/assets/model/Primitive.cpp @@ -26,7 +26,7 @@ namespace fgl::engine if ( m_textures.albedo ) return m_textures.albedo->getID(); else - return INVALID_TEXTURE_ID; + return constants::INVALID_TEXTURE_ID; } TextureID Primitive::getNormalTextureID() const @@ -34,7 +34,7 @@ namespace fgl::engine if ( m_textures.normal ) return m_textures.normal->getID(); else - return INVALID_TEXTURE_ID; + return constants::INVALID_TEXTURE_ID; } OrientedBoundingBox< CoordinateSpace::Model > Primitive::getBoundingBox() const diff --git a/src/engine/assets/model/prebuilt/terrainModel.cpp b/src/engine/assets/model/prebuilt/terrainModel.cpp deleted file mode 100644 index 7bf8f36..0000000 --- a/src/engine/assets/model/prebuilt/terrainModel.cpp +++ /dev/null @@ -1,37 +0,0 @@ -// -// Created by kj16609 on 3/16/24. -// - -#include "terrainModel.hpp" - -#include "engine/assets/model/Model.hpp" - -namespace fgl::engine -{ - - std::shared_ptr< Model > generateTerrainModel( memory::Buffer& vertex_buffer, memory::Buffer& index_buffer ) - { - std::vector< ModelVertex > verts {}; - - constexpr glm::vec3 TOP_LEFT { -0.5f, 0.5f, 0.0f }; - constexpr glm::vec3 TOP_RIGHT { 0.5f, 0.5f, 0.0f }; - constexpr glm::vec3 BOTTOM_RIGHT { 0.5f, -0.5f, 0.0f }; - constexpr glm::vec3 BOTTOM_LEFT { -0.5f, -0.5f, 0.0f }; - constexpr float dist_mulpt { 2.0f }; - - verts.emplace_back( TOP_LEFT * dist_mulpt, glm::vec3( 1.0f ), constants::WORLD_Z, glm::vec2( 0.0f, 0.0f ) ); - verts.emplace_back( TOP_RIGHT * dist_mulpt, glm::vec3( 1.0f ), constants::WORLD_Z, glm::vec2( 1.0f, 0.0f ) ); - verts - .emplace_back( BOTTOM_RIGHT * dist_mulpt, glm::vec3( 1.0f ), constants::WORLD_Z, glm::vec2( 1.0f, 1.0f ) ); - verts.emplace_back( BOTTOM_LEFT * dist_mulpt, glm::vec3( 1.0f ), constants::WORLD_Z, glm::vec2( 0.0f, 1.0f ) ); - - std::vector< std::uint32_t > indicies { 0, 1, 2, 3 }; - - std::shared_ptr< Model > model { - Model::createModelFromVerts( std::move( verts ), std::move( indicies ), vertex_buffer, index_buffer ) - }; - - return model; - } - -} // namespace fgl::engine diff --git a/src/engine/assets/model/prebuilt/terrainModel.hpp b/src/engine/assets/model/prebuilt/terrainModel.hpp deleted file mode 100644 index f694635..0000000 --- a/src/engine/assets/model/prebuilt/terrainModel.hpp +++ /dev/null @@ -1,20 +0,0 @@ -// -// Created by kj16609 on 3/16/24. -// - -#pragma once - -#include - -namespace fgl::engine -{ - class Model; - - namespace memory - { - class Buffer; - } - - std::shared_ptr< Model > generateTerrainModel( memory::Buffer& vertex_buffer, memory::Buffer& index_buffer ); - -} // namespace fgl::engine diff --git a/src/engine/constants.hpp b/src/engine/constants.hpp index b2904c2..76110cb 100644 --- a/src/engine/constants.hpp +++ b/src/engine/constants.hpp @@ -10,6 +10,8 @@ #include #pragma GCC diagnostic pop +#include "types.hpp" + namespace fgl::engine::constants { @@ -35,10 +37,6 @@ namespace fgl::engine::constants constexpr float FAR_PLANE { 1000.0f }; constexpr glm::vec3 CENTER { 0.0f, 0.0f, 0.0f }; - constexpr auto EPSILON { std::numeric_limits< float >::epsilon() * 2 }; - - constexpr auto FRUSTUM_ORIGIN { constants::WORLD_CENTER }; - constexpr glm::mat4 MAT4_IDENTITY { 1.0f }; constexpr glm::mat3 MAT3_IDENTITY { 1.0f }; @@ -49,4 +47,8 @@ namespace fgl::engine::constants constexpr glm::vec3 WORLD_LEFT { -WORLD_RIGHT }; constexpr glm::vec3 WORLD_DOWN { -WORLD_UP }; + constexpr std::uint32_t INVALID_TEX_ID { std::numeric_limits< std::uint32_t >::max() }; + + constexpr TextureID INVALID_TEXTURE_ID { 0 }; + } // namespace fgl::engine::constants diff --git a/src/engine/gameobjects/GameObject.hpp b/src/engine/gameobjects/GameObject.hpp index 9bcab56..73ba91a 100644 --- a/src/engine/gameobjects/GameObject.hpp +++ b/src/engine/gameobjects/GameObject.hpp @@ -4,6 +4,8 @@ #pragma once +#include + #include #include @@ -73,6 +75,7 @@ namespace fgl::engine requires is_component< T > bool hasComponent() const { + ZoneScoped; for ( const GameObjectComponentPtr comp : components ) { if ( comp->id() == T::ID ) return true; @@ -85,6 +88,7 @@ namespace fgl::engine requires is_component< T > std::vector< const T* > getComponents() const { + ZoneScopedN( "Get components" ); std::vector< const T* > temp {}; for ( const ComponentEngineInterface* comp : components ) @@ -99,6 +103,7 @@ namespace fgl::engine requires is_component< T > std::vector< T* > getComponents() { + ZoneScopedN( "Get components" ); std::vector< T* > temp {}; for ( ComponentEngineInterface* comp : components ) diff --git a/src/engine/primitives/Frustum.cpp b/src/engine/primitives/Frustum.cpp index acd6de5..2049cfb 100644 --- a/src/engine/primitives/Frustum.cpp +++ b/src/engine/primitives/Frustum.cpp @@ -4,6 +4,8 @@ #include "Frustum.hpp" +#include + #include "engine/debug/drawers.hpp" #include "engine/primitives/boxes/AxisAlignedBoundingBox.hpp" #include "engine/primitives/boxes/AxisAlignedBoundingCube.hpp" @@ -16,9 +18,9 @@ namespace fgl::engine Frustum operator*( const Matrix< MatrixType::ModelToWorld >& matrix, const FrustumBase& frustum ) { - Frustum result { matrix * frustum.near, matrix * frustum.far, matrix * frustum.top, - matrix * frustum.bottom, matrix * frustum.right, matrix * frustum.left, - matrix * frustum.m_position }; + const Frustum result { matrix * frustum.near, matrix * frustum.far, matrix * frustum.top, + matrix * frustum.bottom, matrix * frustum.right, matrix * frustum.left, + matrix * frustum.m_position }; return result; } @@ -27,7 +29,7 @@ namespace fgl::engine { const glm::vec3 vector_between { point.vec() - origin.vec() }; - float dot { glm::dot( vector_between, direction.vec() ) }; + const float dot { glm::dot( vector_between, direction.vec() ) }; if ( std::isnan( dot ) ) return 0.0f; @@ -189,7 +191,7 @@ namespace fgl::engine { const auto points { this->points() }; - std::array< LineSegment< CoordinateSpace::World >, ( ( 4 * 2 ) / 2 ) * 3 > lines; + std::array< LineSegment< CoordinateSpace::World >, ( ( 4 * 2 ) / 2 ) * 3 > lines {}; //Top lines[ 0 ] = LineSegment< CoordinateSpace::World >( points[ 0 ], points[ 1 ] ); @@ -261,16 +263,15 @@ namespace fgl::engine std::pair< float, float > minMaxDot( const NormalVector axis, const auto points ) { assert( points.size() > 2 ); - float min { glm::dot( points[ 0 ].vec(), axis.vec() ) }; - float max { glm::dot( points[ 0 ].vec(), axis.vec() ) }; + float min { std::numeric_limits< float >::infinity() }; + float max { -std::numeric_limits< float >::infinity() }; for ( std::size_t i = 0; i < points.size(); ++i ) { const auto value { glm::dot( points[ i ].vec(), axis.vec() ) }; - if ( value < min ) - min = value; - else if ( value > max ) - max = value; + + min = std::min( min, value ); + max = std::max( max, value ); } return std::make_pair( min, max ); diff --git a/src/engine/rendering/pipelines/shaders/Compiler.cpp b/src/engine/rendering/pipelines/shaders/Compiler.cpp index aeee8c3..9060f36 100644 --- a/src/engine/rendering/pipelines/shaders/Compiler.cpp +++ b/src/engine/rendering/pipelines/shaders/Compiler.cpp @@ -8,6 +8,7 @@ #include #include "engine/FGL_DEFINES.hpp" +#include "engine/constants.hpp" #include "engine/debug/logging/logging.hpp" namespace fgl::engine @@ -118,6 +119,11 @@ namespace fgl::engine options.SetVulkanRulesRelaxed( false ); + // Add macro defs to the shader + options.AddMacroDefinition( "INVALID_TEXTURE_ID", std::to_string( constants::INVALID_TEXTURE_ID ) ); + options.AddMacroDefinition( "NEAR_PLANE", std::to_string( constants::NEAR_PLANE ) ); + options.AddMacroDefinition( "FAR_PLANE", std::to_string( constants::FAR_PLANE ) ); + const shaderc_shader_kind kind { getShaderKindFromName( input_name ) }; const auto preprocessed_source { getInstance().PreprocessGlsl( diff --git a/src/engine/systems/DrawPair.cpp b/src/engine/systems/DrawPair.cpp index d7e342e..12e1024 100644 --- a/src/engine/systems/DrawPair.cpp +++ b/src/engine/systems/DrawPair.cpp @@ -25,6 +25,7 @@ namespace fgl::engine { ZoneScoped; std::unordered_map< DrawKey, DrawPair > draw_pairs {}; + draw_pairs.reserve( 512 ); const auto nodes { root.getAllLeafsInFrustum( frustum ) }; @@ -89,6 +90,7 @@ namespace fgl::engine if ( auto itter = draw_pairs.find( key ); itter != draw_pairs.end() ) { + ZoneScopedN( "Accumulate for draw pair" ); //Draw command for this mesh already exists. Simply add a count to it auto& [ itter_key, pair ] = *itter; auto& [ existing_cmd, model_matrix ] = pair; @@ -99,6 +101,7 @@ namespace fgl::engine } else { + ZoneScopedN( "Create new draw pair" ); vk::DrawIndexedIndirectCommand cmd {}; cmd.firstIndex = primitive.m_index_buffer.getOffsetCount(); diff --git a/src/engine/systems/EntityRendererSystem.cpp b/src/engine/systems/EntityRendererSystem.cpp index 3c71a1f..6c6af78 100644 --- a/src/engine/systems/EntityRendererSystem.cpp +++ b/src/engine/systems/EntityRendererSystem.cpp @@ -36,7 +36,7 @@ namespace fgl::engine } } - vk::raii::CommandBuffer& EntityRendererSystem::setupSystem( FrameInfo& info ) + vk::raii::CommandBuffer& EntityRendererSystem::setupSystem( const FrameInfo& info ) { auto& command_buffer { info.command_buffer }; @@ -61,7 +61,7 @@ namespace fgl::engine texturedPass( info ); } - void EntityRendererSystem::texturelessPass( FrameInfo& info ) + void EntityRendererSystem::texturelessPass( const FrameInfo& info ) { ZoneScopedN( "Textureless pass" ); auto& command_buffer { info.command_buffer }; @@ -104,7 +104,7 @@ namespace fgl::engine draw_parameter_buffer->stride() ); } - void EntityRendererSystem::texturedPass( FrameInfo& info ) + void EntityRendererSystem::texturedPass( const FrameInfo& info ) { ZoneScopedN( "Textured pass" ); auto& command_buffer { info.command_buffer }; diff --git a/src/engine/systems/EntityRendererSystem.hpp b/src/engine/systems/EntityRendererSystem.hpp index 69ab3c7..bd1bd88 100644 --- a/src/engine/systems/EntityRendererSystem.hpp +++ b/src/engine/systems/EntityRendererSystem.hpp @@ -46,13 +46,13 @@ namespace fgl::engine PerFrameArray< std::unique_ptr< DrawParameterBufferSuballocation > > m_draw_textured_parameter_buffers {}; PerFrameArray< std::unique_ptr< ModelMatrixInfoBufferSuballocation > > m_textured_model_matrix_info_buffers {}; - vk::raii::CommandBuffer& setupSystem( FrameInfo& ); + vk::raii::CommandBuffer& setupSystem( const FrameInfo& ); public: void pass( FrameInfo& info ); - void texturelessPass( FrameInfo& info ); - void texturedPass( FrameInfo& info ); + void texturelessPass( const FrameInfo& info ); + void texturedPass( const FrameInfo& info ); EntityRendererSystem( Device& device, vk::raii::RenderPass& render_pass ); ~EntityRendererSystem() = default; diff --git a/src/engine/texture/Texture.hpp b/src/engine/texture/Texture.hpp index dbc1d7f..e0223d2 100644 --- a/src/engine/texture/Texture.hpp +++ b/src/engine/texture/Texture.hpp @@ -11,6 +11,8 @@ #include "engine/assets/AssetManager.hpp" #include "engine/assets/image/ImageView.hpp" #include "engine/assets/image/Sampler.hpp" +#include "engine/types.hpp" +#include "engine/constants.hpp" namespace fgl::engine { @@ -35,8 +37,7 @@ namespace fgl::engine using TextureStore = AssetStore< Texture >; - using TextureID = std::uint32_t; - constexpr TextureID INVALID_TEXTURE_ID { 0 }; + //TODO: Implement texture handle map to avoid loading the same texture multiple times class Texture final : public AssetInterface< Texture > diff --git a/src/engine/tree/octtree/OctTreeNode.cpp b/src/engine/tree/octtree/OctTreeNode.cpp index 96c3290..6560533 100644 --- a/src/engine/tree/octtree/OctTreeNode.cpp +++ b/src/engine/tree/octtree/OctTreeNode.cpp @@ -51,10 +51,12 @@ namespace fgl::engine void OctTreeNode::getAllLeafsInFrustum( const Frustum& frustum, std::vector< OctTreeNodeLeaf* >& out_leafs ) { + ZoneScoped; //Check if we are inside of the frustum. if ( !isInFrustum( frustum ) ) return; auto& leafs { out_leafs }; + leafs.reserve( 256 ); switch ( m_node_data.index() ) { diff --git a/src/engine/types.hpp b/src/engine/types.hpp new file mode 100644 index 0000000..fc39fcc --- /dev/null +++ b/src/engine/types.hpp @@ -0,0 +1,10 @@ +// +// Created by kj16609 on 10/4/24. +// + +#pragma once + +namespace fgl::engine +{ + using TextureID = std::uint32_t; +} diff --git a/src/shaders/textured-gbuffer.frag b/src/shaders/textured-gbuffer.frag index d89a0ce..2c0758a 100644 --- a/src/shaders/textured-gbuffer.frag +++ b/src/shaders/textured-gbuffer.frag @@ -19,25 +19,19 @@ layout (set = 1, binding = 0) uniform CameraInfo { layout (set = 2, binding = 0) uniform sampler2D tex[]; -#define NEAR_PLANE 0.01f -#define FAR_PLANE 1000.0f - float linearDepth(float depth) { float z = depth * 2.0f - 1.0f; return (2.0f * NEAR_PLANE * FAR_PLANE) / (FAR_PLANE + NEAR_PLANE - z * (FAR_PLANE - NEAR_PLANE)); } -uint INVALID_TEX_ID = 0; - void main() { out_position = vec4(in_world_pos, 1.0f); - vec3 N = vec3(0.0f); - if (in_normal_idx == INVALID_TEX_ID) + if (in_normal_idx == INVALID_TEXTURE_ID) { N = normalize(in_normal); } diff --git a/src/shaders/textureless-gbuffer.frag b/src/shaders/textureless-gbuffer.frag index db34e43..1431324 100644 --- a/src/shaders/textureless-gbuffer.frag +++ b/src/shaders/textureless-gbuffer.frag @@ -17,9 +17,6 @@ layout (set = 1, binding = 0) uniform CameraInfo { mat4 inverse_view; } ubo; -#define NEAR_PLANE 0.01f -#define FAR_PLANE 1000.0f - float linearDepth(float depth) { float z = depth * 2.0f - 1.0f;