diff --git a/.gitmodules b/.gitmodules index 9a2a4d7..346484a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -16,6 +16,3 @@ [submodule "dependencies/VulkanMemoryAllocator"] path = dependencies/VulkanMemoryAllocator url = https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator -[submodule "dependencies/assimp"] - path = dependencies/assimp - url = https://github.com/assimp/assimp \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b6c720..0d6ddcc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,7 +54,6 @@ set(SRC_FILES "src/systems/transform.cpp" "src/util/files.cpp" "src/util/gltf_loader.cpp" - "src/util/model_loader.cpp" "src/vulkan/device.cpp" "src/vulkan/device.h" "src/vulkan/gpu_allocator.cpp" @@ -101,7 +100,6 @@ set(INCLUDE_FILES "include/util.h" "include/util/files.h" "include/util/gltf_loader.h" - "include/util/model_loader.h" "include/window.h" ) @@ -182,18 +180,6 @@ endif() # stb target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC dependencies/stb) -# assimp -set(BUILD_SHARED_LIBS OFF CACHE INTERNAL "" FORCE) -set(ASSIMP_BUILD_TESTS OFF CACHE INTERNAL "" FORCE) -set(ASSIMP_BUILD_ZLIB ON CACHE INTERNAL "" FORCE) -set(ASSIMP_NO_EXPORT ON CACHE INTERNAL "" FORCE) -set(ASSIMP_INSTALL OFF CACHE INTERNAL "" FORCE) -set(ASSIMP_BUILD_COLLADA_IMPORTER ON CACHE INTERNAL "" FORCE) -set(ASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT OFF CACHE INTERNAL "" FORCE) -add_subdirectory(dependencies/assimp) -target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE dependencies/assimp/include) -target_link_libraries(${PROJECT_NAME} PRIVATE assimp) - # public libraries: # SDL2: diff --git a/dependencies/assimp b/dependencies/assimp deleted file mode 160000 index 6392dbf..0000000 --- a/dependencies/assimp +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 6392dbfe4d9b0e23d4e573d0f3e45a198412ece1 diff --git a/include/resources/mesh.h b/include/resources/mesh.h index 7b9a8d3..57183b8 100644 --- a/include/resources/mesh.h +++ b/include/resources/mesh.h @@ -1,4 +1,5 @@ #ifndef ENGINE_INCLUDE_RESOURCES_MESH_H_ +#ifndef ENGINE_INCLUDE_RESOURCES_MESH_H_ #define ENGINE_INCLUDE_RESOURCES_MESH_H_ #include @@ -12,44 +13,39 @@ namespace engine { struct Vertex { - glm::vec3 pos; - glm::vec3 norm; - glm::vec4 tangent; // w component flips binormal if -1. w should be 1 or -1 - glm::vec2 uv; - static constexpr int FloatsPerVertex() { return static_cast(sizeof(Vertex) / sizeof(float)); } + glm::vec3 pos; + glm::vec3 norm; + glm::vec4 tangent; // w component flips binormal if -1. w should be 1 or -1 + glm::vec2 uv; + static constexpr int FloatsPerVertex() { return static_cast(sizeof(Vertex) / sizeof(float)); } }; -} // namespace engine +} // namespace engine namespace engine { class Mesh { - public: - Mesh(GFXDevice* gfx, const std::vector& vertices); - Mesh(GFXDevice* gfx, const std::vector& vertices, - const std::vector& indices) - : gfx_(gfx) { - InitMesh(vertices, indices); - } - ~Mesh(); - Mesh(const Mesh&) = delete; - Mesh& operator=(const Mesh&) = delete; + public: + Mesh(GFXDevice* gfx, const std::vector& vertices); + Mesh(GFXDevice* gfx, const std::vector& vertices, const std::vector& indices); + ~Mesh(); + Mesh(const Mesh&) = delete; + Mesh& operator=(const Mesh&) = delete; - const gfx::Buffer* GetVB(); - const gfx::Buffer* GetIB(); - uint32_t GetCount(); + const gfx::Buffer* GetVB(); + const gfx::Buffer* GetIB(); + uint32_t GetCount(); - private: - GFXDevice* const gfx_; + private: + GFXDevice* const gfx_; - const gfx::Buffer* vb_; - const gfx::Buffer* ib_; - uint32_t count_; + const gfx::Buffer* vb_; + const gfx::Buffer* ib_; + uint32_t count_; - void InitMesh(const std::vector& vertices, - const std::vector& indices); + void InitMesh(const std::vector& vertices, const std::vector& indices); }; -} // namespace engine +} // namespace engine #endif \ No newline at end of file diff --git a/include/util/model_loader.h b/include/util/model_loader.h deleted file mode 100644 index 8acb905..0000000 --- a/include/util/model_loader.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef ENGINE_INCLUDE_UTIL_MODEL_LOADER_H_ -#define ENGINE_INCLUDE_UTIL_MODEL_LOADER_H_ - -#include - -#include "scene.h" - -namespace engine { -namespace util { - -Entity LoadMeshFromFile(Scene* parent, const std::string& path, bool is_static = false); - -} // namespace util -} // namespace engine - -#endif \ No newline at end of file diff --git a/src/application.cpp b/src/application.cpp index f313b8b..8d86148 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -210,8 +210,8 @@ void Application::GameLoop() uint64_t now = window_->GetNanos(); if (now - lastTick >= 1000000000LL * 5LL) [[unlikely]] { lastTick = now; - LOG_INFO("fps: {}", std::lroundf(avg_fps)); - // renderer()->GetDevice()->LogPerformanceInfo(); + LOG_DEBUG("fps: {}", std::lroundf(avg_fps)); + renderer()->GetDevice()->LogPerformanceInfo(); window_->ResetAvgFPS(); } diff --git a/src/gfx_device_vulkan.cpp b/src/gfx_device_vulkan.cpp index 93f5361..125af50 100644 --- a/src/gfx_device_vulkan.cpp +++ b/src/gfx_device_vulkan.cpp @@ -2310,18 +2310,18 @@ void GFXDevice::LogPerformanceInfo() memProps.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2; vkGetPhysicalDeviceMemoryProperties2(pimpl->device.physicalDevice, &memProps); - LOG_INFO("GPU Memory Statistics:"); + LOG_DEBUG("GPU Memory Statistics:"); for (uint32_t i = 0; i < memProps.memoryProperties.memoryHeapCount; i++) { const VmaStatistics& statistics = pStats.memoryHeap[i].statistics; VkMemoryHeap heap = memProps.memoryProperties.memoryHeaps[i]; - LOG_INFO("Memory heap {}", i); + LOG_DEBUG("Memory heap {}", i); if (heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) { - LOG_INFO(" DEVICE_LOCAL"); + LOG_DEBUG(" DEVICE_LOCAL"); } - LOG_INFO(" Memory blocks allocated: {} ({} MiB)", statistics.blockCount, statistics.allocationBytes / (1024 * 1024)); - LOG_INFO(" Number of allocations: {} ({} MiB)", statistics.allocationCount, statistics.allocationBytes / (1024 * 1024)); - LOG_INFO(" Max size: {} MiB", heap.size / (1024 * 1024)); + LOG_DEBUG(" Memory blocks allocated: {} ({} MiB)", statistics.blockCount, statistics.allocationBytes / (1024 * 1024)); + LOG_DEBUG(" Number of allocations: {} ({} MiB)", statistics.allocationCount, statistics.allocationBytes / (1024 * 1024)); + LOG_DEBUG(" Max size: {} MiB", heap.size / (1024 * 1024)); } } diff --git a/src/resources/font.cpp b/src/resources/font.cpp index 6d55f00..fcc2657 100644 --- a/src/resources/font.cpp +++ b/src/resources/font.cpp @@ -21,7 +21,7 @@ Font::Font(const std::string& path) { LOG_DEBUG("Created font: {}", path); } -Font::~Font() { LOG_DEBUG("Destroying font..."); } +Font::~Font() { LOG_DEBUG("Destroyed font"); } std::unique_ptr> Font::GetTextBitmap( const std::string& text, float height_px, int& width_out, int& height_out) { diff --git a/src/resources/material.cpp b/src/resources/material.cpp index decced2..6a6a5da 100644 --- a/src/resources/material.cpp +++ b/src/resources/material.cpp @@ -7,9 +7,10 @@ namespace engine { Material::Material(Renderer* renderer, std::shared_ptr shader) : shader_(shader), renderer_(renderer) { material_set_ = renderer->GetDevice()->AllocateDescriptorSet(renderer->GetMaterialSetLayout()); + LOG_DEBUG("Created material"); } -Material::~Material() { renderer_->GetDevice()->FreeDescriptorSet(material_set_); } +Material::~Material() { renderer_->GetDevice()->FreeDescriptorSet(material_set_); LOG_DEBUG("Destroyed material"); } void Material::SetAlbedoTexture(std::shared_ptr texture) { diff --git a/src/resources/mesh.cpp b/src/resources/mesh.cpp index fc31573..1059cc7 100644 --- a/src/resources/mesh.cpp +++ b/src/resources/mesh.cpp @@ -5,18 +5,22 @@ namespace engine { -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(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_); +Mesh::Mesh(GFXDevice* gfx, const std::vector& vertices, const std::vector& indices) : gfx_(gfx) { InitMesh(vertices, indices); } + +Mesh::~Mesh() +{ + gfx_->DestroyBuffer(ib_); + gfx_->DestroyBuffer(vb_); + LOG_DEBUG("Destroyed mesh"); } const gfx::Buffer* Mesh::GetVB() { return vb_; } @@ -25,15 +29,12 @@ 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()); +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 +} // namespace engine diff --git a/src/resources/shader.cpp b/src/resources/shader.cpp index e643505..f1fa1dd 100644 --- a/src/resources/shader.cpp +++ b/src/resources/shader.cpp @@ -62,8 +62,8 @@ Shader::Shader(Renderer* renderer, const std::string& vertPath, const std::strin } Shader::~Shader() { - LOG_DEBUG("Destroying shader... pipeline: {}", static_cast(pipeline_)); gfx_->DestroyPipeline(pipeline_); + LOG_DEBUG("Destroyed shader, pipeline: {}", static_cast(pipeline_)); } const gfx::Pipeline* Shader::GetPipeline() { return pipeline_; } diff --git a/src/resources/texture.cpp b/src/resources/texture.cpp index 2fdffbc..15825a9 100644 --- a/src/resources/texture.cpp +++ b/src/resources/texture.cpp @@ -24,8 +24,8 @@ Texture::Texture(Renderer* renderer, const uint8_t* bitmap, int width, int heigh Texture::~Texture() { - LOG_DEBUG("Destroying texture..."); gfx_->DestroyImage(image_); + LOG_DEBUG("Destroyed texture"); } std::unique_ptr LoadTextureFromFile(const std::string& path, gfx::SamplerInfo samplerInfo, Renderer* renderer, bool srgb) diff --git a/src/systems/collisions.cpp b/src/systems/collisions.cpp index 0d8eea2..4cb1529 100644 --- a/src/systems/collisions.cpp +++ b/src/systems/collisions.cpp @@ -124,7 +124,7 @@ void CollisionSystem::OnUpdate(float ts) } } - LOG_INFO("BUILT BVH!"); + LOG_DEBUG("BUILT BVH!"); colliders_size_last_update_ = colliders_size_now_; } diff --git a/src/util/model_loader.cpp b/src/util/model_loader.cpp deleted file mode 100644 index c4b6f97..0000000 --- a/src/util/model_loader.cpp +++ /dev/null @@ -1,225 +0,0 @@ -#include "util/model_loader.h" - -#include "log.h" - -#include "application.h" - -#include "components/transform.h" -#include "components/mesh_renderable.h" - -#include "resources/texture.h" -#include "resources/material.h" -#include "resources/shader.h" -#include "resources/mesh.h" - -#include -#include -#include -#include -#include -#include -#include - -#include - -#include -#include - -namespace engine::util { - -static void buildGraph(const std::map>& textures, const std::vector>& meshes, - const std::vector& meshTextureIndices, aiNode* parentNode, Scene* scene, Entity parentObj, bool is_static) -{ - - // convert to glm column major - glm::mat4 transform{}; - for (int i = 0; i < 4; i++) { - for (int j = 0; j < 4; j++) { - transform[i][j] = parentNode->mTransformation[j][i]; - } - } - - // get position - glm::vec3 position{transform[3][0], transform[3][1], transform[3][2]}; - - // remove position from matrix - transform[3][0] = 0.0f; - transform[3][1] = 0.0f; - transform[3][2] = 0.0f; - - // get scale - glm::vec3 scale{}; - scale.x = sqrt(transform[0][0] * transform[0][0] + transform[0][1] * transform[0][1] + transform[0][2] * transform[0][2]); - scale.y = sqrt(transform[1][0] * transform[1][0] + transform[1][1] * transform[1][1] + transform[1][2] * transform[1][2]); - scale.z = sqrt(transform[2][0] * transform[2][0] + transform[2][1] * transform[2][1] + transform[2][2] * transform[2][2]); - - // remove scaling from matrix - for (int row = 0; row < 3; row++) { - transform[0][row] /= scale.x; - transform[1][row] /= scale.y; - transform[2][row] /= scale.z; - } - - // get rotation - glm::quat rotation = glm::quat_cast(transform); - - // ASSIMP always makes the root node Y-up - // We want Z-up - if (parentNode->mParent == nullptr) { - // if this is the root node - rotation *= glm::angleAxis(glm::half_pi(), glm::vec3{1.0f, 0.0f, 0.0f}); - } - - // update position, scale, rotation - auto parentTransform = scene->GetComponent(parentObj); - parentTransform->position = position; - parentTransform->scale = scale; - parentTransform->rotation = rotation; - parentTransform->is_static = is_static; - - for (uint32_t i = 0; i < parentNode->mNumMeshes; i++) { - // create child node for each mesh - auto child = scene->CreateEntity("_mesh" + std::to_string(i), parentObj); - scene->GetComponent(child)->is_static = is_static; - auto childRenderer = scene->AddComponent(child); - childRenderer->mesh = meshes[parentNode->mMeshes[i]]; - childRenderer->material = std::make_shared(scene->app()->renderer(), scene->app()->GetResource("builtin.fancy")); - if (textures.contains(meshTextureIndices[parentNode->mMeshes[i]])) { - childRenderer->material->SetAlbedoTexture(textures.at(meshTextureIndices[parentNode->mMeshes[i]])); - } - else { - childRenderer->material->SetAlbedoTexture(scene->app()->GetResource("builtin.white")); - } - childRenderer->material->SetNormalTexture(scene->app()->GetResource("builtin.normal")); - } - - for (uint32_t i = 0; i < parentNode->mNumChildren; i++) { - buildGraph(textures, meshes, meshTextureIndices, parentNode->mChildren[i], scene, scene->CreateEntity("child" + std::to_string(i), parentObj), - is_static); - } -} - -Entity LoadMeshFromFile(Scene* parent, const std::string& path, bool is_static) -{ - - Assimp::Importer importer; - - class myStream : public Assimp::LogStream { - public: - void write(const char* message) override - { - (void)message; - LOG_TRACE("ASSIMP: {}", message); - } - }; - - const unsigned int severity = Assimp::Logger::Debugging | Assimp::Logger::Info | Assimp::Logger::Err | Assimp::Logger::Warn; - Assimp::DefaultLogger::get()->attachStream(new myStream, severity); - - // remove everything but texcoords, normals, meshes, materials - importer.SetPropertyInteger(AI_CONFIG_PP_RVC_FLAGS, aiComponent_ANIMATIONS | aiComponent_BONEWEIGHTS | aiComponent_CAMERAS | aiComponent_COLORS | - aiComponent_LIGHTS | aiComponent_TANGENTS_AND_BITANGENTS | aiComponent_TEXTURES | 0); - importer.SetPropertyInteger(AI_CONFIG_PP_SBP_REMOVE, aiPrimitiveType_POINT | aiPrimitiveType_LINE | aiPrimitiveType_POLYGON); - - const aiScene* scene = - importer.ReadFile(path, - aiProcess_JoinIdenticalVertices | aiProcess_Triangulate | aiProcess_SortByPType | aiProcess_RemoveComponent | - aiProcess_SplitLargeMeshes | // leave at default maximum - aiProcess_ValidateDataStructure | // make sure to log the output - aiProcess_ImproveCacheLocality | aiProcess_RemoveRedundantMaterials | aiProcess_FindInvalidData | aiProcess_GenSmoothNormals | - aiProcess_GenUVCoords | aiProcess_TransformUVCoords | aiProcess_FlipUVs | // Collada uses bottom-left origin - aiProcess_CalcTangentSpace | 0); - - const char* errString = importer.GetErrorString(); - if (errString[0] != '\0' || scene == nullptr) { - throw std::runtime_error("assimp error: " + std::string(errString)); - } - - if (scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE) { - throw std::runtime_error("assimp error (incomplete): " + std::string(errString)); - } - - assert(scene->HasAnimations() == false); - assert(scene->HasCameras() == false); - assert(scene->HasLights() == false); - assert(scene->hasSkeletons() == false); - - LOG_TRACE("material count: {}, mesh count: {}", scene->mNumMaterials, scene->mNumMeshes); - - std::map> textures{}; - - for (uint32_t i = 0; i < scene->mNumMaterials; i++) { - const aiMaterial* m = scene->mMaterials[i]; - LOG_TRACE("Material {}:", i); - LOG_TRACE(" Name: {}", m->GetName().C_Str()); - for (uint32_t j = 0; j < m->mNumProperties; j++) { - [[maybe_unused]] const aiMaterialProperty* p = m->mProperties[j]; - LOG_TRACE(" prop {}, key: {}", j, p->mKey.C_Str()); - } - - if (aiGetMaterialTextureCount(m, aiTextureType_DIFFUSE) >= 1) { - aiString texPath{}; - aiGetMaterialTexture(m, aiTextureType_DIFFUSE, 0, &texPath); - LOG_TRACE(" Diffuse tex: {}", texPath.C_Str()); - std::filesystem::path absPath = path; - absPath = absPath.parent_path(); - absPath /= texPath.C_Str(); - try { - textures[i] = LoadTextureFromFile(absPath.string(), gfx::SamplerInfo{}, parent->app()->renderer()); - } - catch (const std::runtime_error&) { - textures[i] = parent->app()->GetResource("builtin.white"); - } - } - } - - std::vector> meshes{}; - std::vector meshMaterialIndices{}; - for (uint32_t i = 0; i < scene->mNumMeshes; i++) { - const aiMesh* m = scene->mMeshes[i]; - meshMaterialIndices.push_back(m->mMaterialIndex); - std::vector vertices(m->mNumVertices); - std::vector indices((size_t)m->mNumFaces * 3); - LOG_TRACE("Mesh {}: vertex count {}", i, vertices.size()); - LOG_TRACE("Mesh {}: index count {}", i, indices.size()); - - if (m->mTangents == nullptr) { - throw std::runtime_error("No tangents array found!"); - } - - for (uint32_t j = 0; j < vertices.size(); j++) { - Vertex v{}; - v.pos.x = m->mVertices[j].x; - v.pos.y = m->mVertices[j].y; - v.pos.z = m->mVertices[j].z; - v.norm.x = m->mNormals[j].x; - v.norm.y = m->mNormals[j].y; - v.norm.z = m->mNormals[j].z; - v.tangent.x = m->mTangents[j].x; - v.tangent.y = m->mTangents[j].y; - v.tangent.z = m->mTangents[j].z; - v.tangent.w = 1.0f; - v.uv.x = m->mTextureCoords[0][j].x; - v.uv.y = m->mTextureCoords[0][j].y; - vertices[j] = v; - } - - for (uint32_t j = 0; j < indices.size() / 3; j++) { - indices[(size_t)j * 3 + 0] = m->mFaces[j].mIndices[0]; - indices[(size_t)j * 3 + 1] = m->mFaces[j].mIndices[1]; - indices[(size_t)j * 3 + 2] = m->mFaces[j].mIndices[2]; - } - meshes.push_back(std::make_shared(parent->app()->renderer()->GetDevice(), vertices, indices)); - } - - Entity obj = parent->CreateEntity(scene->GetShortFilename(path.c_str())); - - buildGraph(textures, meshes, meshMaterialIndices, scene->mRootNode, parent, obj, is_static); - - LOG_INFO("Loaded model: {}, meshes: {}, textures: {}", scene->GetShortFilename(path.c_str()), meshes.size(), textures.size()); - - Assimp::DefaultLogger::kill(); - return obj; -} - -} // namespace engine::util diff --git a/test/src/camera_controller.cpp b/test/src/camera_controller.cpp index 29a993a..e4b98ff 100644 --- a/test/src/camera_controller.cpp +++ b/test/src/camera_controller.cpp @@ -209,11 +209,11 @@ void CameraControllerSystem::OnUpdate(float ts) engine::Raycast cast = scene_->GetSystem()->GetRaycast(ray); if (cast.hit) { - LOG_INFO("Distance: {} m", cast.distance); - LOG_INFO("Location: {} {} {}", cast.location.x, cast.location.y, cast.location.z); - LOG_INFO("Normal: {} {} {}", cast.normal.x, cast.normal.y, cast.normal.z); - LOG_INFO("Ray direction: {} {} {}", ray.direction.x, ray.direction.y, ray.direction.z); - LOG_INFO("Hit Entity: {}", scene_->GetComponent(cast.hit_entity)->tag); + LOG_TRACE("Distance: {} m", cast.distance); + LOG_TRACE("Location: {} {} {}", cast.location.x, cast.location.y, cast.location.z); + LOG_TRACE("Normal: {} {} {}", cast.normal.x, cast.normal.y, cast.normal.z); + LOG_TRACE("Ray direction: {} {} {}", ray.direction.x, ray.direction.y, ray.direction.z); + LOG_TRACE("Hit Entity: {}", scene_->GetComponent(cast.hit_entity)->tag); c->perm_lines.clear(); c->perm_lines.emplace_back(ray.origin, cast.location, glm::vec3{0.0f, 0.0f, 1.0f}); scene_->GetComponent(cast.hit_entity)->visible ^= true;