diff --git a/CMakeLists.txt b/CMakeLists.txt index 1ecc739..efed6c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,8 +92,6 @@ source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/include" PREFIX "Include" FILES $ target_compile_definitions(${PROJECT_NAME} PRIVATE DEFINITIONS "ENGINE_EXPORTS") -target_compile_definitions(${PROJECT_NAME} PUBLIC DEFINITIONS "VMA_DEBUG_LOG=printf") - if (WIN32) target_compile_definitions(${PROJECT_NAME} PRIVATE DEFINITIONS "NOMINMAX") # stop windows.h conflicting with 'std::max' endif() diff --git a/include/application.h b/include/application.h index 09e20a8..8bb6657 100644 --- a/include/application.h +++ b/include/application.h @@ -68,12 +68,14 @@ class Application { private: std::unique_ptr window_; std::unique_ptr input_manager_; - std::unique_ptr scene_manager_; std::unique_ptr renderer_; std::unordered_map> resource_managers_{}; std::filesystem::path resources_path_; + // Most resources and class instances in the game exist in this object + std::unique_ptr scene_manager_; + bool enable_frame_limiter_ = true; template diff --git a/include/renderer.h b/include/renderer.h index f0c0627..69fec30 100644 --- a/include/renderer.h +++ b/include/renderer.h @@ -5,6 +5,7 @@ #include #include +#include #include "gfx_device.h" @@ -28,6 +29,12 @@ class Renderer { ~Renderer(); + void PreRender(bool window_is_resized, glm::mat4 camera_transform); + + void Render(); + + // getters + GFXDevice* GetDevice() { return device_.get(); } const gfx::DescriptorSetLayout* GetGlobalSetLayout() { @@ -47,6 +54,12 @@ class Renderer { private: std::unique_ptr device_; + struct CameraSettings { + float vertical_fov_radians = glm::radians(70.0f); + float clip_near = 0.5f; + float clip_far = 1000.0f; + } camera_settings_; + // ALL vertex shaders must begin with: /* layout(set = 0, binding = 0) uniform GlobalSetUniformBuffer { @@ -71,6 +84,8 @@ class Renderer { UniformDescriptor frame_uniform; // updates once per frame; set 1 // in fragment shader const gfx::DescriptorSetLayout* material_set_layout; // set 2 + + float viewport_aspect_ratio_ = 1.0f; }; } // namespace engine diff --git a/include/resource_manager.h b/include/resource_manager.h index 8ff9971..2b43d25 100644 --- a/include/resource_manager.h +++ b/include/resource_manager.h @@ -6,17 +6,26 @@ #include #include #include +#include + +#include "log.h" + +#include "resources/mesh.h" namespace engine { class IResourceManager { public: - virtual ~IResourceManager() = default; + virtual ~IResourceManager(){}; }; template class ResourceManager : public IResourceManager { public: + ~ResourceManager() override { + LOG_DEBUG("Destroying resource manager... '{}'", typeid(T).name()); + } + std::shared_ptr Add(const std::string& name, std::unique_ptr&& resource) { if (resources_.contains(name) == false) { diff --git a/src/application.cpp b/src/application.cpp index 85dfb99..9e6abab 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -70,11 +70,11 @@ Application::Application(const char* appName, const char* appVersion, resources_path_ = getResourcesPath(); // register resource managers - RegisterResourceManager(); + RegisterResourceManager(); + RegisterResourceManager(); RegisterResourceManager(); RegisterResourceManager(); - RegisterResourceManager(); - RegisterResourceManager(); + RegisterResourceManager(); renderer_ = std::make_unique( appName, appVersion, window_->GetHandle(), graphicsSettings); @@ -148,10 +148,11 @@ Application::Application(const char* appName, const char* appVersion, } } -Application::~Application() {} +Application::~Application() { +} void Application::GameLoop() { - LOG_TRACE("Begin game loop..."); + LOG_DEBUG("Begin game loop..."); constexpr int FPS_LIMIT = 240; constexpr auto FRAMETIME_LIMIT = @@ -170,11 +171,13 @@ void Application::GameLoop() { if (now - lastTick >= 1000000000LL * 5LL) [[unlikely]] { lastTick = now; LOG_INFO("fps: {}", window_->GetAvgFPS()); - renderer()->GetDevice()->LogPerformanceInfo(); + //renderer()->GetDevice()->LogPerformanceInfo(); window_->ResetAvgFPS(); } /* render */ + renderer_->PreRender(window()->GetWindowResized(), glm::mat4{1.0f}); + renderer_->Render(); /* poll events */ window_->GetInputAndEvents(); diff --git a/src/renderer.cpp b/src/renderer.cpp index 7c71811..e5c89c2 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -1,5 +1,9 @@ #include "renderer.h" +#include +#include +#include + namespace engine { Renderer::Renderer(const char* app_name, const char* app_version, @@ -62,4 +66,35 @@ Renderer::~Renderer() { device_->DestroyDescriptorSetLayout(global_uniform.layout); } +void Renderer::PreRender(bool window_is_resized, glm::mat4 camera_transform) { + if (window_is_resized) { + uint32_t w, h; + device_->GetViewportSize(&w, &h); + viewport_aspect_ratio_ = (float)w / (float)h; + const glm::mat4 proj_matrix = glm::perspectiveZO( + camera_settings_.vertical_fov_radians, viewport_aspect_ratio_, + camera_settings_.clip_near, camera_settings_.clip_far); + /* update SET 0 (rarely changing uniforms)*/ + global_uniform.uniform_buffer_data.data = proj_matrix; + device_->WriteUniformBuffer(global_uniform.uniform_buffer, 0, + sizeof(global_uniform.uniform_buffer_data), + &global_uniform.uniform_buffer_data); + } + + // set camera view matrix uniform + /* update SET 1 (per frame uniforms) */ + const glm::mat4 view_matrix = glm::inverse(camera_transform); + frame_uniform.uniform_buffer_data.data = view_matrix; + device_->WriteUniformBuffer(frame_uniform.uniform_buffer, 0, + sizeof(frame_uniform.uniform_buffer_data), + &frame_uniform.uniform_buffer_data); +} + +void Renderer::Render() +{ + gfx::DrawBuffer* draw_buffer = device_->BeginRender(); + + device_->FinishRender(draw_buffer); +} + } // namespace engine \ No newline at end of file diff --git a/src/resources/font.cpp b/src/resources/font.cpp index 3c4742c..2a5a11c 100644 --- a/src/resources/font.cpp +++ b/src/resources/font.cpp @@ -18,10 +18,10 @@ Font::Font(const std::string& path) { throw std::runtime_error("Failed to initialise font!"); } - LOG_INFO("Loaded font: {}", path); + LOG_DEBUG("Created font: {}", path); } -Font::~Font() {} +Font::~Font() { LOG_DEBUG("Destroying font..."); } std::unique_ptr> Font::GetTextBitmap( const std::string& text, float height_px, int& width_out, int& height_out) { diff --git a/src/resources/mesh.cpp b/src/resources/mesh.cpp index 1beda72..741b9fb 100644 --- a/src/resources/mesh.cpp +++ b/src/resources/mesh.cpp @@ -5,43 +5,35 @@ namespace engine::resources { - Mesh::Mesh(GFXDevice* gfx, const std::vector& vertices) - : gfx_(gfx) - { - std::vector indices(vertices.size()); - for (uint32_t i = 0; i < indices.size(); i++) { - indices[i] = i; - } - InitMesh(vertices, indices); - } - - Mesh::~Mesh() - { - gfx_->DestroyBuffer(ib_); - gfx_->DestroyBuffer(vb_); - } - - const gfx::Buffer* Mesh::GetVB() - { - return vb_; - } - - const gfx::Buffer* Mesh::GetIB() - { - return ib_; - } - - uint32_t Mesh::GetCount() - { - return count_; - } - - void Mesh::InitMesh(const std::vector& vertices, const std::vector& indices) - { - vb_ = gfx_->CreateBuffer(gfx::BufferType::kVertex, vertices.size() * sizeof(Vertex), vertices.data()); - ib_ = gfx_->CreateBuffer(gfx::BufferType::kIndex, indices.size() * sizeof(uint32_t), indices.data()); - count_ = (uint32_t)indices.size(); - LOG_INFO("Loaded mesh, vertices: {}, indices: {}", vertices.size(), indices.size()); - } - +Mesh::Mesh(GFXDevice* gfx, const std::vector& vertices) : gfx_(gfx) { + std::vector indices(vertices.size()); + for (uint32_t i = 0; i < indices.size(); i++) { + indices[i] = i; + } + InitMesh(vertices, indices); } + +Mesh::~Mesh() { + LOG_DEBUG("Destroying mesh..."); + gfx_->DestroyBuffer(ib_); + gfx_->DestroyBuffer(vb_); +} + +const gfx::Buffer* Mesh::GetVB() { return vb_; } + +const gfx::Buffer* Mesh::GetIB() { return ib_; } + +uint32_t Mesh::GetCount() { return count_; } + +void Mesh::InitMesh(const std::vector& vertices, + const std::vector& indices) { + vb_ = gfx_->CreateBuffer(gfx::BufferType::kVertex, + vertices.size() * sizeof(Vertex), vertices.data()); + ib_ = gfx_->CreateBuffer(gfx::BufferType::kIndex, + indices.size() * sizeof(uint32_t), indices.data()); + count_ = (uint32_t)indices.size(); + LOG_DEBUG("Created mesh, vertices: {}, indices: {}", vertices.size(), + indices.size()); +} + +} // namespace engine::resources diff --git a/src/resources/shader.cpp b/src/resources/shader.cpp index 217ba30..63ae8b9 100644 --- a/src/resources/shader.cpp +++ b/src/resources/shader.cpp @@ -56,11 +56,14 @@ Shader::Shader(Renderer* renderer, const char* vertPath, const char* fragPath, pipeline_ = gfx_->CreatePipeline(info); - LOG_INFO("Loaded shader: {}, vertex attribs: {}", vertPath, - vertFormat.attribute_descriptions.size()); + LOG_DEBUG("Created shader: {}, pipeline: {}", vertPath, + static_cast(pipeline_)); } -Shader::~Shader() { gfx_->DestroyPipeline(pipeline_); } +Shader::~Shader() { + LOG_DEBUG("Destroying shader... pipeline: {}", static_cast(pipeline_)); + gfx_->DestroyPipeline(pipeline_); +} const gfx::Pipeline* Shader::GetPipeline() { return pipeline_; } diff --git a/src/resources/texture.cpp b/src/resources/texture.cpp index f63db00..c394adf 100644 --- a/src/resources/texture.cpp +++ b/src/resources/texture.cpp @@ -52,7 +52,7 @@ Texture::Texture(Renderer* renderer, const std::string& path, gfx_->UpdateDescriptorCombinedImageSampler( descriptor_set_, 0, image_, renderer->samplers.at(samplerInfo)); - LOG_INFO("Loaded texture: {}, width: {} height: {}", path, width, height); + LOG_DEBUG("Created texture: {}, width: {} height: {}", path, width, height); } Texture::Texture(Renderer* renderer, const uint8_t* bitmap, int width, @@ -95,11 +95,11 @@ Texture::Texture(Renderer* renderer, const uint8_t* bitmap, int width, gfx_->UpdateDescriptorCombinedImageSampler( descriptor_set_, 0, image_, renderer->samplers.at(samplerInfo)); - LOG_INFO("Loaded texture: BITMAP, width: {} height: {}", width, height); + LOG_DEBUG("Created texture: BITMAP, width: {} height: {}", width, height); } Texture::~Texture() { - LOG_INFO("Destroying texture..."); + LOG_DEBUG("Destroying texture..."); gfx_->FreeDescriptorSet(descriptor_set_); gfx_->DestroyImage(image_); } diff --git a/test/src/game.cpp b/test/src/game.cpp index 979d13a..bf0d2c1 100644 --- a/test/src/game.cpp +++ b/test/src/game.cpp @@ -79,14 +79,14 @@ void PlayGame(GameSettings settings) { } /* shared resources */ - auto grass_texture = std::make_shared( + /* auto grass_texture = std::make_shared( app.renderer(), app.GetResourcePath("textures/grass.jpg"), engine::resources::Texture::Filtering::kAnisotropic); auto space_texture = std::make_shared( app.renderer(), app.GetResourcePath("textures/space2.png"), engine::resources::Texture::Filtering::kAnisotropic); - + */ /* skybox */ { uint32_t skybox = my_scene->CreateEntity("skybox"); @@ -96,7 +96,8 @@ void PlayGame(GameSettings settings) { skybox_renderable->material = std::make_unique( app.GetResource("builtin.skybox")); - skybox_renderable->material->texture_ = space_texture; + skybox_renderable->material->texture_ = + app.GetResource("builtin.white"); skybox_renderable->mesh = GenCuboidMesh(app.renderer()->GetDevice(), 10.0f, 10.0f, 10.0f, 1.0f, true); @@ -120,7 +121,8 @@ void PlayGame(GameSettings settings) { floor_renderable->material = std::make_shared( app.GetResource("builtin.standard")); - floor_renderable->material->texture_ = grass_texture; + floor_renderable->material->texture_ = + app.GetResource("builtin.white"); floor_renderable->mesh = GenCuboidMesh(app.renderer()->GetDevice(), 100.0f, 0.1f, 100.0f, 100.0f); @@ -155,7 +157,7 @@ void PlayGame(GameSettings settings) { auto textboxComponent = my_scene->AddComponent(textbox); textboxComponent->onInit = [](void) { - LOG_INFO("Textbox custom component initialised!"); + LOG_DEBUG("Textbox custom component initialised!"); }; textboxComponent->onUpdate = [](float ts) { @@ -163,7 +165,7 @@ void PlayGame(GameSettings settings) { time_elapsed += ts; if (time_elapsed >= 1.0f) { time_elapsed = 0.0f; - LOG_INFO("COMPONENT UPDATE"); + //LOG_INFO("COMPONENT UPDATE"); } }; }