From 49fdefc5c9d25aa6d650338fbb7ff4a2683c4401 Mon Sep 17 00:00:00 2001 From: schwieni Date: Sat, 23 Jul 2022 17:06:31 +0200 Subject: [PATCH] Bugfixes and make improvements Double delete of shaders fixed (valgrind was used). Makefile improvements: use of implicit rules and update of Data directory instead of copy if directory is present. The implicit rules enable multiprocessor makes $ make -j $(nproc) Consecutive builds should be much faster. --- MP-APS/Core/RenderSystem.cpp | 6 +-- MP-APS/Core/RenderSystem.h | 7 ++-- MP-APS/Engine.cpp | 2 +- MP-APS/Engine.h | 2 +- MP-APS/Graphics/GLShaderProgram.cpp | 7 ++-- MP-APS/Graphics/GLShaderProgram.h | 15 +++---- MP-APS/Model.cpp | 2 +- Makefile | 61 +++++++++++++++++++++-------- install-deps-ubuntu.sh | 0 9 files changed, 63 insertions(+), 39 deletions(-) mode change 100644 => 100755 install-deps-ubuntu.sh diff --git a/MP-APS/Core/RenderSystem.cpp b/MP-APS/Core/RenderSystem.cpp index d67c27f0..bb688d82 100644 --- a/MP-APS/Core/RenderSystem.cpp +++ b/MP-APS/Core/RenderSystem.cpp @@ -118,10 +118,8 @@ void RenderSystem::Update(const Camera& camera) { } /***********************************************************************************/ -void RenderSystem::Shutdown() const { - for (const auto& shader : m_shaderCache) { - shader.second.DeleteProgram(); - } +void RenderSystem::Shutdown() { + m_shaderCache.clear(); } /***********************************************************************************/ diff --git a/MP-APS/Core/RenderSystem.h b/MP-APS/Core/RenderSystem.h index 3250e245..fd0d625a 100644 --- a/MP-APS/Core/RenderSystem.h +++ b/MP-APS/Core/RenderSystem.h @@ -25,16 +25,17 @@ class GLShaderProgram; class RenderSystem { using RenderListIterator = std::vector::const_iterator; public: + ~RenderSystem() = default; void Init(const pugi::xml_node& rendererNode); void Update(const Camera& camera); - // Release OpenGL resources - void Shutdown() const; + //! Release OpenGL resources + void Shutdown(); // void UpdateView(const Camera& camera); - // Where the magic happens + //! Where the magic happens void Render(const Camera& camera, RenderListIterator renderListBegin, RenderListIterator renderListEnd, diff --git a/MP-APS/Engine.cpp b/MP-APS/Engine.cpp index e90d9f94..bac47fa0 100644 --- a/MP-APS/Engine.cpp +++ b/MP-APS/Engine.cpp @@ -155,7 +155,7 @@ void Engine::Execute() { } /***********************************************************************************/ -void Engine::shutdown() const { +void Engine::shutdown() { m_guiSystem.Shutdown(); m_renderer.Shutdown(); ResourceManager::GetInstance().ReleaseAllResources(); diff --git a/MP-APS/Engine.h b/MP-APS/Engine.h index 5d817c41..04b3ffb9 100644 --- a/MP-APS/Engine.h +++ b/MP-APS/Engine.h @@ -20,7 +20,7 @@ class Engine { void Execute(); private: - void shutdown() const; + void shutdown(); // Performs view-frustum culling. // Returns meshes visible by the camera. diff --git a/MP-APS/Graphics/GLShaderProgram.cpp b/MP-APS/Graphics/GLShaderProgram.cpp index 79113397..0dd6277f 100644 --- a/MP-APS/Graphics/GLShaderProgram.cpp +++ b/MP-APS/Graphics/GLShaderProgram.cpp @@ -17,16 +17,17 @@ GLShaderProgram::~GLShaderProgram() { /***********************************************************************************/ void GLShaderProgram::Bind() const { - assert(m_programID != 0); + assert(m_programID != GLShaderProgram::noId); glUseProgram(m_programID); } /***********************************************************************************/ -void GLShaderProgram::DeleteProgram() const { - if (m_programID != 0) { +void GLShaderProgram::DeleteProgram() { + if (m_programID != GLShaderProgram::noId) { std::cout << "Deleting program: " << m_programName << '\n'; glDeleteProgram(m_programID); + m_programID = GLShaderProgram::noId; } } diff --git a/MP-APS/Graphics/GLShaderProgram.h b/MP-APS/Graphics/GLShaderProgram.h index cb313576..0b2af0db 100644 --- a/MP-APS/Graphics/GLShaderProgram.h +++ b/MP-APS/Graphics/GLShaderProgram.h @@ -15,13 +15,9 @@ class GLShaderProgram { ~GLShaderProgram(); GLShaderProgram(GLShaderProgram&& other) { - m_uniforms = other.m_uniforms; - m_programID = other.m_programID; - m_programName = other.m_programName; - - other.m_uniforms.clear(); - other.m_programID = 0; - other.m_programName.clear(); + std::swap(m_uniforms, other.m_uniforms); + std::swap(m_programID, other.m_programID); + std::swap(m_programName, other.m_programName); } GLShaderProgram& operator=(GLShaderProgram other) noexcept(true) { @@ -40,7 +36,7 @@ class GLShaderProgram { GLShaderProgram& operator=(const GLShaderProgram&) = delete; void Bind() const; - void DeleteProgram() const; + void DeleteProgram(); GLShaderProgram& SetUniformi(const std::string& uniformName, const int value); GLShaderProgram& SetUniformf(const std::string& uniformName, const float value); @@ -58,6 +54,7 @@ class GLShaderProgram { std::unordered_map m_uniforms; - GLuint m_programID{ 0 }; + static const GLuint noId = 0; + GLuint m_programID{ noId }; std::string m_programName; }; diff --git a/MP-APS/Model.cpp b/MP-APS/Model.cpp index fbf84635..732c1a84 100644 --- a/MP-APS/Model.cpp +++ b/MP-APS/Model.cpp @@ -141,7 +141,7 @@ void Model::processNode(aiNode* node, const aiScene* scene, const bool loadMater /***********************************************************************************/ Mesh Model::processMesh(aiMesh* mesh, const aiScene* scene, const bool loadMaterial) { std::vector vertices; - glm::vec3 min, max; + glm::vec3 min(std::numeric_limits::max()), max(std::numeric_limits::lowest()); for (auto i = 0; i < mesh->mNumVertices; ++i) { Vertex vertex; diff --git a/Makefile b/Makefile index 08422a16..20df1c79 100644 --- a/Makefile +++ b/Makefile @@ -1,23 +1,50 @@ - +# directory definitions output_dir := ./build -create_output_dir := mkdir -p $(output_dir) -glm_dir := ./MP-APS/3rdParty/glm/ -glad_dir := ./MP-APS/3rdParty/glad/include/ -glad_src := ./MP-APS/3rdParty/glad/src/glad.c -stb_dir := ./MP-APS/3rdParty/stb/ -pugixml_dir := ./MP-APS/3rdParty/pugixml/include/ -nuklear_dir := ./MP-APS/3rdParty/nuklear/include/ -fmt_dir := ./MP-APS/3rdParty/fmt/include/ -fmt_src := ./MP-APS/3rdParty/fmt/src/ +APPNAME := $(output_dir)/MP-APS + +# collect all source files +SRC += $(wildcard ./MP-APS/*.cpp) +SRC += $(wildcard ./MP-APS/Core/*.cpp) +SRC += $(wildcard ./MP-APS/Demos/*.cpp) +SRC += $(wildcard ./MP-APS/Graphics/*.cpp) +SRC += ./MP-APS/3rdParty/fmt/src/format.cc +SRC += ./MP-APS/3rdParty/glad/src/glad.c + +# Use implicit rules to build object files from source files. +# We have *.cpp *.cc *.c source files. +OBJ := $(SRC:.cpp=.o) +OBJ := $(OBJ:.cc=.o) +OBJ := $(OBJ:.c=.o) + +CPPFLAGS += -I./MP-APS/3rdParty/glm +CPPFLAGS += -I./MP-APS/3rdParty/glad/include +CPPFLAGS += -I./MP-APS/3rdParty/stb +CPPFLAGS += -I./MP-APS/3rdParty/fmt/include +CPPFLAGS += -I./MP-APS/3rdParty/pugixml/include +CPPFLAGS += -I./MP-APS/3rdParty/nuklear/include + +CXXFLAGS += -std=c++17 -Wall -Wextra -Wno-unused-variable -march=native -isystem -O3 + +LDFLAGS += + +LDLIBS += -lstdc++fs -lGL -lglfw -lpthread -lassimp -ltbb + +# virtual targets +.PHONY : all clean + +all: $(APPNAME) copydata + +$(output_dir): + mkdir -p $(output_dir) -libs := -lstdc++fs -lGL -lglfw -lpthread -lassimp -ltbb +copydata: $(output_dir) +# only update (this is faster, if already copied once) + cp -r -u ./MP-APS/Data $(output_dir) -all: - make clean - $(create_output_dir) - g++ -std=c++17 -Wall -Wextra -Wno-unused-variable -march=native -isystem -O3 -o $(output_dir)/MP-APS -I$(glm_dir) -I$(glad_dir) -I$(stb_dir) -I$(fmt_dir) -I$(pugixml_dir) -I$(nuklear_dir) ./MP-APS/*.cpp ./MP-APS/Core/*.cpp ./MP-APS/Demos/*.cpp ./MP-APS/Graphics/*.cpp $(glad_src) $(fmt_src)/format.cc $(libs) - cp -r ./MP-APS/Data/ $(output_dir) +$(APPNAME): $(OBJ) $(output_dir) + $(CXX) $(OBJ) -o $(APPNAME) $(LDFLAGS) $(LDLIBS) clean: - rm -rf $(output_dir) + $(RM) $(OBJ) + $(RM) -r $(output_dir) diff --git a/install-deps-ubuntu.sh b/install-deps-ubuntu.sh old mode 100644 new mode 100755