diff --git a/code/CMakeLists.txt b/code/CMakeLists.txt index 7a1d473..942a02f 100644 --- a/code/CMakeLists.txt +++ b/code/CMakeLists.txt @@ -1,10 +1,23 @@ -cmake_minimum_required(VERSION 3.10) +cmake_minimum_required(VERSION 3.16) set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED True) +# Enable unity builds +set(CMAKE_UNITY_BUILD true) +# Enabling LTO +include(CheckIPOSupported) +check_ipo_supported(RESULT has_ipo_support) +if(has_ipo_support) + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) +endif() + +set(CMAKE_CXX_FLAGS_DEBUG -ftime-trace) + project(Graphics) add_subdirectory(Engine) add_subdirectory(Demos) + add_subdirectory(Test) + diff --git a/code/Demos/CMakeLists.txt b/code/Demos/CMakeLists.txt index b1e9b87..324c2ae 100644 --- a/code/Demos/CMakeLists.txt +++ b/code/Demos/CMakeLists.txt @@ -8,6 +8,8 @@ file(COPY ${CMAKE_SOURCE_DIR}/.clang-tidy DESTINATION ${CMAKE_CURRENT_BINARY_DIR add_executable(Demos ${DEMOS_SOURCES} ${DEMOS_HEADERS}) +target_precompile_headers(Demos PUBLIC ${CMAKE_CURRENT_INCLUDE_DIR}/Engine.hpp) + if(DEFINED EMSCRIPTEN) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s EXPORTED_FUNCTIONS='_main' -s EXPORT_NAME=\"'WasmModuleDemos'\"") # Move index.html. diff --git a/code/Demos/src/Main.cpp b/code/Demos/src/Main.cpp index 274c218..55a28c7 100644 --- a/code/Demos/src/Main.cpp +++ b/code/Demos/src/Main.cpp @@ -122,6 +122,7 @@ struct SpinningTeapotLogic { ib.load_indices(model.indices); vb.load_vertices(model.vertices); glDrawElements(GL_TRIANGLES, ib.get_count(), GL_UNSIGNED_INT, (void*)0); + vb.bind(); } void stop() { diff --git a/code/Engine/CMakeLists.txt b/code/Engine/CMakeLists.txt index 659ed25..58a6493 100644 --- a/code/Engine/CMakeLists.txt +++ b/code/Engine/CMakeLists.txt @@ -31,6 +31,8 @@ if(NOT MSVC) target_compile_options(Engine PRIVATE -fexperimental-library -Wall -Wextra -Wpedantic) endif() +target_precompile_headers(Engine PRIVATE include/EnginePch.hpp) + if(DEFINED EMSCRIPTEN) target_link_libraries(Engine PUBLIC glm::glm assimp::assimp EnTT::EnTT Utily::Utily lodepng ${BULLET_LIBRARIES} diff --git a/code/Engine/include/Engine.hpp b/code/Engine/include/Engine.hpp new file mode 100644 index 0000000..ec4b789 --- /dev/null +++ b/code/Engine/include/Engine.hpp @@ -0,0 +1,6 @@ +#pragma once + +#include "EnginePch.hpp" +#include "App.hpp" +#include "Components/Components.hpp" +#include "Model/Model.hpp" \ No newline at end of file diff --git a/code/Engine/include/EnginePch.hpp b/code/Engine/include/EnginePch.hpp new file mode 100644 index 0000000..c62d17a --- /dev/null +++ b/code/Engine/include/EnginePch.hpp @@ -0,0 +1,6 @@ +#pragma once + +#include "Utily/Utily.hpp" +#include "glm/glm.hpp" +#include "glm/mat4x4.hpp" +#include "Config.hpp" \ No newline at end of file diff --git a/code/Engine/include/Renderer/BatchDrawer.hpp b/code/Engine/include/Renderer/BatchDrawer.hpp index 2787d1c..6df277d 100644 --- a/code/Engine/include/Renderer/BatchDrawer.hpp +++ b/code/Engine/include/Renderer/BatchDrawer.hpp @@ -61,12 +61,12 @@ namespace Renderer { constexpr static std::ptrdiff_t mm_uniform_replacement_offset = std::distance(mm_uniform_string.begin(), std::ranges::find(mm_uniform_string, '$')); // Copy and ensure null ended string. - std::array mm_uniform = { mm_uniform_string.begin(), mm_uniform_string.end() }; - mm_uniform.back() = '\0'; + std::array mm_uniform { '\0' }; + std::ranges::copy(mm_uniform_string, mm_uniform.begin()); uint32_t i = 0; auto vert_iter = vertices_buffer.begin(); - auto indi_iter = vertices_buffer.begin(); + auto indi_iter = indices_buffer.begin(); for (auto& [model, transform, texture] : textured_models) { const auto tex_unit = texture.bind(true).on_error(Utily::ErrorHandler::print_then_quit).value(); const auto index_offset = static_cast(std::distance(vertices_buffer.begin(), vert_iter)); @@ -78,11 +78,11 @@ namespace Renderer { // Account for index offset auto add_index_offset = [&](Model::Index index) { return index + index_offset; }; - auto indi_iter = std::ranges::copy(model.indices | add_index_offset, indi_iter); + indi_iter = std::ranges::copy(model.indices | add_index_offset, indi_iter); // Add texture index and model transform index auto add_tex_unit = [&](const Model::Vertex& v) { return Model::BatchingVertex { v.position, v.normal, v.uv_coord, tex_unit, i }; }; - auto vert_iter = std::ranges::copy(model.vertices | add_tex_unit, vert_iter); + vert_iter = std::ranges::copy(model.vertices | add_tex_unit, vert_iter); ++i; } diff --git a/code/Engine/src/Renderer/IndexBuffer.cpp b/code/Engine/src/Renderer/IndexBuffer.cpp index 803e08d..2f3341b 100644 --- a/code/Engine/src/Renderer/IndexBuffer.cpp +++ b/code/Engine/src/Renderer/IndexBuffer.cpp @@ -3,23 +3,23 @@ #include namespace Renderer { - constexpr static uint32_t INVALID_BUFFER_ID = 0; - static IndexBuffer* last_bound = nullptr; + constexpr static uint32_t INVALID_INDEX_BUFFER_ID = 0; + static IndexBuffer* last_bound_ib = nullptr; IndexBuffer::IndexBuffer(IndexBuffer&& other) noexcept : _id(std::exchange(other._id, std::nullopt)) , _count(std::exchange(other._count, 0)) { - last_bound = nullptr; + last_bound_ib = nullptr; } auto IndexBuffer::init() noexcept -> Utily::Result { if (_id.has_value()) { return Utily::Error { "Trying to override in-use index buffer" }; } - _id = INVALID_BUFFER_ID; + _id = INVALID_INDEX_BUFFER_ID; _count = 0; glGenBuffers(1, &_id.value()); - if (_id.value() == INVALID_BUFFER_ID) { + if (_id.value() == INVALID_INDEX_BUFFER_ID) { _id = std::nullopt; return Utily::Error { "Failed to create Index Buffer. glGenBuffers failed." }; } @@ -27,26 +27,26 @@ namespace Renderer { } void IndexBuffer::stop() noexcept { - if (_id.value_or(INVALID_BUFFER_ID) != INVALID_BUFFER_ID) { + if (_id.value_or(INVALID_INDEX_BUFFER_ID) != INVALID_INDEX_BUFFER_ID) { glDeleteBuffers(1, &_id.value()); } _id = std::nullopt; _count = 0; - if (last_bound == this) { - last_bound = nullptr; + if (last_bound_ib == this) { + last_bound_ib = nullptr; } } void IndexBuffer::bind() noexcept { if constexpr (Config::DEBUG_LEVEL != Config::DebugInfo::none) { - if (_id.value_or(INVALID_BUFFER_ID) == INVALID_BUFFER_ID) { + if (_id.value_or(INVALID_INDEX_BUFFER_ID) == INVALID_INDEX_BUFFER_ID) { std::cerr << "Trying to unbind invalid vertex buffer."; assert(false); } } - if (last_bound != this) { - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _id.value_or(INVALID_BUFFER_ID)); - last_bound = this; + if (last_bound_ib != this) { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _id.value_or(INVALID_INDEX_BUFFER_ID)); + last_bound_ib = this; } } @@ -54,15 +54,15 @@ namespace Renderer { if constexpr (Config::SKIP_UNBINDING) { return; } else if constexpr (Config::DEBUG_LEVEL != Config::DebugInfo::none) { - if (_id.value_or(INVALID_BUFFER_ID) == INVALID_BUFFER_ID) { + if (_id.value_or(INVALID_INDEX_BUFFER_ID) == INVALID_INDEX_BUFFER_ID) { std::cerr << "Trying to unbind invalid vertex buffer."; assert(false); } } - if (last_bound != nullptr) { + if (last_bound_ib != nullptr) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - last_bound = nullptr; + last_bound_ib = nullptr; } } diff --git a/code/Engine/src/Renderer/Shader.cpp b/code/Engine/src/Renderer/Shader.cpp index c37e0da..a9084de 100644 --- a/code/Engine/src/Renderer/Shader.cpp +++ b/code/Engine/src/Renderer/Shader.cpp @@ -8,12 +8,12 @@ using namespace std::literals; namespace Renderer { - static Shader* last_bound = nullptr; + static Shader* last_bound_s = nullptr; Shader::Shader(Shader&& other) : _program_id(std::exchange(other._program_id, std::nullopt)) , _cached_uniforms(std::move(other._cached_uniforms)) { - last_bound = nullptr; + last_bound_s = nullptr; } auto Shader::compile_shader(Type type, const std::string_view& source) -> Utily::Result { @@ -116,8 +116,8 @@ namespace Renderer { assert(_program_id.has_value()); } } - if (last_bound != this) { - last_bound = this; + if (last_bound_s != this) { + last_bound_s = this; } glUseProgram(_program_id.value()); } @@ -131,9 +131,9 @@ namespace Renderer { } } - if (last_bound != this && last_bound != nullptr) { + if (last_bound_s != this && last_bound_s != nullptr) { glUseProgram(0); - last_bound = nullptr; + last_bound_s = nullptr; } } void Shader::stop() { @@ -142,8 +142,8 @@ namespace Renderer { glDeleteProgram(*_program_id); _program_id = std::nullopt; } - if (last_bound == this) { - last_bound = nullptr; + if (last_bound_s == this) { + last_bound_s = nullptr; } } diff --git a/code/Engine/src/Renderer/VertexBuffer.cpp b/code/Engine/src/Renderer/VertexBuffer.cpp index 549bb6d..5a74d0b 100644 --- a/code/Engine/src/Renderer/VertexBuffer.cpp +++ b/code/Engine/src/Renderer/VertexBuffer.cpp @@ -3,21 +3,21 @@ #include namespace Renderer { - constexpr static uint32_t INVALID_BUFFER_ID = 0; - static VertexBuffer* last_bound = nullptr; + constexpr static uint32_t INVALID_VERTEX_BUFFER_ID = 0; + static VertexBuffer* last_bound_vb = nullptr; VertexBuffer::VertexBuffer(VertexBuffer&& other) noexcept : _id(std::exchange(other._id, std::nullopt)) { - last_bound = nullptr; + last_bound_vb = nullptr; } auto VertexBuffer::init() noexcept -> Utily::Result { if (_id) { return Utily::Error { "Trying to override in-use vertex buffer" }; } - _id = INVALID_BUFFER_ID; + _id = INVALID_VERTEX_BUFFER_ID; glGenBuffers(1, &_id.value()); - if (_id.value() == INVALID_BUFFER_ID) { + if (_id.value() == INVALID_VERTEX_BUFFER_ID) { _id = std::nullopt; return Utily::Error { "Failed to create Vertex buffer. glGenBuffers failed." }; } @@ -25,41 +25,41 @@ namespace Renderer { } void VertexBuffer::stop() noexcept { - if (_id.value_or(INVALID_BUFFER_ID) != INVALID_BUFFER_ID) { + if (_id.value_or(INVALID_VERTEX_BUFFER_ID) != INVALID_VERTEX_BUFFER_ID) { glDeleteBuffers(1, &_id.value()); } _id = std::nullopt; - if (last_bound == this) { - last_bound = this; + if (last_bound_vb == this) { + last_bound_vb = this; } } void VertexBuffer::bind() noexcept { if constexpr (Config::DEBUG_LEVEL != Config::DebugInfo::none) { - if (_id.value_or(INVALID_BUFFER_ID) == INVALID_BUFFER_ID) { + if (_id.value_or(INVALID_VERTEX_BUFFER_ID) == INVALID_VERTEX_BUFFER_ID) { std::cerr << "Trying to bind invalid vertex buffer."; assert(false); } } - if (last_bound != this) { - glBindBuffer(GL_ARRAY_BUFFER, _id.value_or(INVALID_BUFFER_ID)); - last_bound = this; + if (last_bound_vb != this) { + glBindBuffer(GL_ARRAY_BUFFER, _id.value_or(INVALID_VERTEX_BUFFER_ID)); + last_bound_vb = this; } } void VertexBuffer::unbind() noexcept { if constexpr (Config::SKIP_UNBINDING) { return; } else if constexpr (Config::DEBUG_LEVEL != Config::DebugInfo::none) { - if (_id.value_or(INVALID_BUFFER_ID) == INVALID_BUFFER_ID) { + if (_id.value_or(INVALID_VERTEX_BUFFER_ID) == INVALID_VERTEX_BUFFER_ID) { std::cerr << "Trying to unbind invalid vertex buffer."; assert(false); } } - if (last_bound != nullptr) { + if (last_bound_vb != nullptr) { glBindBuffer(GL_ARRAY_BUFFER, 0); - last_bound = nullptr; + last_bound_vb = nullptr; } } diff --git a/code/Test/CMakeLists.txt b/code/Test/CMakeLists.txt index 9cfa9f1..4572fa3 100644 --- a/code/Test/CMakeLists.txt +++ b/code/Test/CMakeLists.txt @@ -8,6 +8,8 @@ file(COPY ${CMAKE_SOURCE_DIR}/.clang-tidy DESTINATION ${CMAKE_CURRENT_BINARY_DIR add_executable(Test ${TEST_SOURCES} ${TEST_HEADERS}) +target_precompile_headers(Test PRIVATE include/TestPch.hpp ${CMAKE_CURRENT_INCLUDE_DIR}/Engine.hpp) + if(DEFINED EMSCRIPTEN) set(CMAKE_EXECUTABLE_SUFFIX ".html") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s EXPORTED_FUNCTIONS='_main' --closure 1") diff --git a/code/Test/include/Integration/BasicApps.hpp b/code/Test/include/Integration/BasicApps.hpp index 38b88a0..40877fb 100644 --- a/code/Test/include/Integration/BasicApps.hpp +++ b/code/Test/include/Integration/BasicApps.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include "TestPch.hpp" #include #include diff --git a/code/Test/include/TestPch.hpp b/code/Test/include/TestPch.hpp new file mode 100644 index 0000000..1eb159c --- /dev/null +++ b/code/Test/include/TestPch.hpp @@ -0,0 +1,4 @@ +#pragma once + +#include "Utily/Utily.hpp" +#include \ No newline at end of file diff --git a/code/Test/include/Unit/UnitBenchDrawer.hpp b/code/Test/include/Unit/UnitBenchDrawer.hpp index 6dcc6ee..c6550ed 100644 --- a/code/Test/include/Unit/UnitBenchDrawer.hpp +++ b/code/Test/include/Unit/UnitBenchDrawer.hpp @@ -1,8 +1,7 @@ #pragma once #include "Renderer/BatchDrawer.hpp" -#include "Utily/Utily.hpp" -#include +#include "TestPch.hpp" #include #include diff --git a/code/Test/include/Unit/UnitModelStatic.hpp b/code/Test/include/Unit/UnitModelStatic.hpp index 4122b60..a5aac57 100644 --- a/code/Test/include/Unit/UnitModelStatic.hpp +++ b/code/Test/include/Unit/UnitModelStatic.hpp @@ -1,8 +1,7 @@ #pragma once #include "Model/Model.hpp" -#include "Utily/Utily.hpp" -#include +#include "TestPch.hpp" #include #include diff --git a/code/Test/src/Main.cpp b/code/Test/src/Main.cpp index c01acb6..63bcda2 100644 --- a/code/Test/src/Main.cpp +++ b/code/Test/src/Main.cpp @@ -1,4 +1,4 @@ -#include +#include "TestPch.hpp" #include "Unit/UnitModelStatic.hpp" #include "Unit/UnitBenchDrawer.hpp" diff --git a/code/build-native.bat b/code/build-native.bat index 228598f..a44d2d5 100644 --- a/code/build-native.bat +++ b/code/build-native.bat @@ -1,16 +1,18 @@ @echo off set VCPKG_PATH=C:/apps/vcpkg/vcpkg/ +set BUILD_TYPE=Debug if not exist "build-native\" ( mkdir build-native ) + cd build-native -call cmake .. -G "Ninja" -DCMAKE_TOOLCHAIN_FILE=%VCPKG_PATH%/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows -call cmake --build . --config Release +call cmake .. -G "Ninja" -DCMAKE_TOOLCHAIN_FILE=%VCPKG_PATH%/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows -DCMAKE_BUILD_TYPE=%BUILD_TYPE% +call cmake --build . --config %BUILD_TYPE% -cd Test -call Test -cd ../Demos -call Demos \ No newline at end of file +@REM cd Test +@REM call Test +@REM cd ../Demos +@REM call Demos \ No newline at end of file diff --git a/code/build-web.bat b/code/build-web.bat index 63d8ed9..d0c1ed9 100644 --- a/code/build-web.bat +++ b/code/build-web.bat @@ -1,7 +1,7 @@ set VCPKG_PATH=C:/apps/vcpkg/vcpkg/ set EMSDK=C:/apps/emscripten/emsdk/ set EMSCRIPTEN=C:/apps/emscripten/emsdk/upstream/emscripten/ -set BUILD_TYPE=Release +set BUILD_TYPE=Debug if not exist "build-web\" ( mkdir build-web