From 069318dc0b9beb0ebd5930a1b60e2f2b6a8e713b Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Mon, 15 Aug 2022 10:54:34 -0400 Subject: [PATCH] Try reformatting asset code with clang-format --- engine/asset/CMakeLists.txt | 20 ++-- engine/asset/include/asset.hpp | 110 ++++++++--------- engine/asset/include/asset_types.hpp | 19 +-- engine/asset/include/assetptr.hpp | 43 +++---- engine/asset/src/asset.cpp | 173 ++++++++++++++------------- 5 files changed, 173 insertions(+), 192 deletions(-) diff --git a/engine/asset/CMakeLists.txt b/engine/asset/CMakeLists.txt index e8cf62a..d6a4aa4 100644 --- a/engine/asset/CMakeLists.txt +++ b/engine/asset/CMakeLists.txt @@ -1,18 +1,18 @@ set(SRC - include/asset_types.hpp - include/asset.hpp - include/assetptr.hpp + include/asset_types.hpp + include/asset.hpp + include/assetptr.hpp src/asset.cpp) add_library(Asset STATIC ${SRC}) target_include_directories(Asset PUBLIC include) target_link_libraries(Asset - PUBLIC - Math - Renderer - PRIVATE - stb - Log - Core) + PUBLIC + Math + Renderer + PRIVATE + stb + Log + Core) set_engine_properties(Asset) diff --git a/engine/asset/include/asset.hpp b/engine/asset/include/asset.hpp index 92277bb..94dda47 100644 --- a/engine/asset/include/asset.hpp +++ b/engine/asset/include/asset.hpp @@ -1,113 +1,101 @@ #pragma once +#include #include #include -#include -#include "file.hpp" -#include "assetptr.hpp" #include "asset_types.hpp" +#include "assetptr.hpp" +#include "file.hpp" #include "string_utils.hpp" -template -std::unique_ptr load_asset(const prism::path& p); +template std::unique_ptr load_asset(const prism::path& p); -template -bool can_load_asset(const prism::path& p); +template bool can_load_asset(const prism::path& p); -template -using AssetStore = std::unordered_map>; +template using AssetStore = std::unordered_map>; -template -class AssetPool : public AssetStore... { +template class AssetPool : public AssetStore... { public: - template - AssetPtr add() { + template AssetPtr add() { const auto p = prism::path(); auto reference_block = get_reference_block(p); - + AssetStore::try_emplace(p, std::make_unique()); - + return AssetPtr(AssetStore::at(p).get(), reference_block); } - template - AssetPtr get(const prism::path path) { - return fetch(path, get_reference_block(path)); - } - - template - std::vector get_all() { + template AssetPtr get(const prism::path path) { return fetch(path, get_reference_block(path)); } + + template std::vector get_all() { std::vector assets; - for(auto iter = AssetStore::begin(); iter != AssetStore::end(); iter++) { + for (auto iter = AssetStore::begin(); iter != AssetStore::end(); iter++) { auto& [p, asset] = *iter; - + assets.push_back(asset.get()); } - + return assets; } - - template - AssetPtr fetch(const prism::path path, ReferenceBlock* reference_block) { - if(!AssetStore::count(path)) + + template AssetPtr fetch(const prism::path path, ReferenceBlock* reference_block) { + if (!AssetStore::count(path)) AssetStore::try_emplace(path, load_asset(path)); - + return AssetPtr(AssetStore::at(path).get(), reference_block); } - + std::tuple load_asset_generic(const prism::path path) { Asset* asset = nullptr; ReferenceBlock* block = nullptr; - + (load_asset_generic(path, asset, block), ...); - + return {asset, block}; } - + void perform_cleanup() { - for(auto iter = reference_blocks.begin(); iter != reference_blocks.end();) { + for (auto iter = reference_blocks.begin(); iter != reference_blocks.end();) { auto& [path, block] = *iter; - - if(block->references == 0) { + + if (block->references == 0) { ((delete_asset(path)), ...); - + iter = reference_blocks.erase(iter); } else { iter = std::next(iter); } } } - + std::unordered_map> reference_blocks; - + private: ReferenceBlock* get_reference_block(const prism::path& path) { - if(!reference_blocks.count(path)) + if (!reference_blocks.count(path)) reference_blocks.try_emplace(path, std::make_unique()); return reference_blocks[path].get(); } - - template - void load_asset_generic(const prism::path& path, Asset*& at, ReferenceBlock*& block) { - if(can_load_asset(path)) { - if(!AssetStore::count(path)) + + template void load_asset_generic(const prism::path& path, Asset*& at, ReferenceBlock*& block) { + if (can_load_asset(path)) { + if (!AssetStore::count(path)) AssetStore::try_emplace(path, load_asset(path)); - + at = AssetStore::at(path).get(); block = get_reference_block(path); } } - - template - void delete_asset(const prism::path path) { + + template void delete_asset(const prism::path path) { auto iter = AssetStore::find(path); - if(iter != AssetStore::end()) { + if (iter != AssetStore::end()) { auto& [_, asset] = *iter; - + asset.reset(); - + AssetStore::erase(iter); } } @@ -123,24 +111,22 @@ std::unique_ptr load_texture(const prism::path& path); void save_material(Material* material, const prism::path& path); -template -std::unique_ptr load_asset(const prism::path& path) { +template std::unique_ptr load_asset(const prism::path& path) { if constexpr (std::is_same_v) { return load_mesh(path); - } else if constexpr(std::is_same_v) { + } else if constexpr (std::is_same_v) { return load_material(path); - } else if constexpr(std::is_same_v) { + } else if constexpr (std::is_same_v) { return load_texture(path); } } -template -bool can_load_asset(const prism::path& path) { - if constexpr(std::is_same_v) { +template bool can_load_asset(const prism::path& path) { + if constexpr (std::is_same_v) { return path.extension() == ".model"; - } else if constexpr(std::is_same_v) { + } else if constexpr (std::is_same_v) { return path.extension() == ".material"; - } else if constexpr(std::is_same_v) { + } else if constexpr (std::is_same_v) { return path.extension() == ".png"; } } diff --git a/engine/asset/include/asset_types.hpp b/engine/asset/include/asset_types.hpp index 7cd6811..bde27ee 100644 --- a/engine/asset/include/asset_types.hpp +++ b/engine/asset/include/asset_types.hpp @@ -2,16 +2,16 @@ #include +#include "aabb.hpp" #include "assetptr.hpp" #include "math.hpp" -#include "aabb.hpp" #include "utility.hpp" class GFXBuffer; class GFXTexture; class Texture : public Asset { -public: +public: GFXTexture* handle = nullptr; int width = 0, height = 0; }; @@ -75,16 +75,17 @@ struct Bone { class Mesh : public Asset { public: - // meshes are rendered in parts if we cannot batch it in one call, i.e. a mesh - // with multiple materials with different textures, etc + // meshes are rendered in parts if we cannot batch it in one call, i.e. a + // mesh with multiple materials with different textures, etc struct Part { std::string name; prism::aabb bounding_box; - + GFXBuffer* bone_batrix_buffer = nullptr; std::vector offset_matrices; - - uint32_t index_offset = 0, index_count = 0;; + + uint32_t index_offset = 0, index_count = 0; + ; int32_t vertex_offset = 0; int32_t material_override = -1; std::string material_hint; @@ -101,9 +102,9 @@ public: GFXBuffer* tangent_buffer = nullptr; GFXBuffer* bitangent_buffer = nullptr; GFXBuffer* bone_buffer = nullptr; - + GFXBuffer* index_buffer = nullptr; - + Matrix4x4 global_inverse_transformation; uint32_t num_indices = 0; diff --git a/engine/asset/include/assetptr.hpp b/engine/asset/include/assetptr.hpp index 4dfd3c7..1f26fe1 100644 --- a/engine/asset/include/assetptr.hpp +++ b/engine/asset/include/assetptr.hpp @@ -12,24 +12,21 @@ public: std::string path; }; -template -struct AssetPtr { +template struct AssetPtr { AssetPtr() = default; - AssetPtr(T* ptr, ReferenceBlock* block) : handle(ptr), block(block) { - block->references++; - } + AssetPtr(T* ptr, ReferenceBlock* block) : handle(ptr), block(block) { block->references++; } - AssetPtr(const AssetPtr &rhs) { + AssetPtr(const AssetPtr& rhs) { handle = rhs.handle; block = rhs.block; - if(block != nullptr) + if (block != nullptr) block->references++; } AssetPtr& operator=(const AssetPtr& rhs) { - if(&rhs != this) { + if (&rhs != this) { handle = rhs.handle; block = rhs.block; @@ -41,14 +38,14 @@ struct AssetPtr { } ~AssetPtr() { - if(block != nullptr) + if (block != nullptr) block->references--; } - + void clear() { - if(block != nullptr) + if (block != nullptr) block->references--; - + block = nullptr; handle = nullptr; } @@ -56,23 +53,13 @@ struct AssetPtr { T* handle = nullptr; ReferenceBlock* block = nullptr; - explicit operator bool() const{ - return handle != nullptr; - } + explicit operator bool() const { return handle != nullptr; } - T* operator->() { - return handle; - } + T* operator->() { return handle; } - T* operator->() const { - return handle; - } - - T* operator*() { - return handle; - } + T* operator->() const { return handle; } - T* operator*() const { - return handle; - } + T* operator*() { return handle; } + + T* operator*() const { return handle; } }; diff --git a/engine/asset/src/asset.cpp b/engine/asset/src/asset.cpp index e59531f..80b19c4 100644 --- a/engine/asset/src/asset.cpp +++ b/engine/asset/src/asset.cpp @@ -1,25 +1,25 @@ #include "asset.hpp" -#include #include +#include #include -#include "log.hpp" +#include "assertions.hpp" #include "engine.hpp" #include "gfx.hpp" -#include "json_conversions.hpp" #include "gfx_commandbuffer.hpp" -#include "assertions.hpp" -#include "renderer.hpp" -#include "input.hpp" -#include "physics.hpp" #include "imgui_backend.hpp" +#include "input.hpp" +#include "json_conversions.hpp" +#include "log.hpp" +#include "physics.hpp" +#include "renderer.hpp" std::unique_ptr load_mesh(const prism::path& path) { Expects(!path.empty()); - + auto file = prism::open_file(path, true); - if(!file.has_value()) { + if (!file.has_value()) { prism::log("Failed to load mesh from {}!", path.string()); return nullptr; } @@ -27,7 +27,7 @@ std::unique_ptr load_mesh(const prism::path& path) { int version = 0; file->read(&version); - if(version == 5 || version == 6 || version == 7) { + if (version == 5 || version == 6 || version == 7) { } else { prism::log("{} failed the mesh version check! reported version = {}", path.string(), version); return nullptr; @@ -35,34 +35,35 @@ std::unique_ptr load_mesh(const prism::path& path) { auto mesh = std::make_unique(); mesh->path = path.string(); - + enum MeshType : int { Static, Skinned } mesh_type; - + file->read(&mesh_type); // TODO: use unsigned int here int numVertices = 0; file->read(&numVertices); - + Expects(numVertices > 0); - + const auto read_buffer = [&f = file.value(), numVertices](unsigned int size) -> GFXBuffer* { - auto buffer = engine->get_gfx()->create_buffer(nullptr, size * static_cast(numVertices), false, GFXBufferUsage::Vertex); + auto buffer = engine->get_gfx()->create_buffer( + nullptr, size * static_cast(numVertices), false, GFXBufferUsage::Vertex); auto buffer_ptr = reinterpret_cast(engine->get_gfx()->get_buffer_contents(buffer)); - if(buffer_ptr != nullptr) { + if (buffer_ptr != nullptr) { f.read(buffer_ptr, size * static_cast(numVertices)); engine->get_gfx()->release_buffer_contents(buffer, buffer_ptr); } else { f.seek(size * static_cast(numVertices)); } - + return buffer; }; - + // read positions mesh->position_buffer = read_buffer(sizeof(prism::float3)); mesh->normal_buffer = read_buffer(sizeof(prism::float3)); @@ -70,51 +71,52 @@ std::unique_ptr load_mesh(const prism::path& path) { mesh->tangent_buffer = read_buffer(sizeof(prism::float3)); mesh->bitangent_buffer = read_buffer(sizeof(prism::float3)); - if(mesh_type == MeshType::Skinned) + if (mesh_type == MeshType::Skinned) mesh->bone_buffer = read_buffer(sizeof(BoneVertexData)); - + int numIndices = 0; file->read(&numIndices); - + Expects(numIndices > 0); - - mesh->index_buffer = engine->get_gfx()->create_buffer(nullptr, sizeof(uint32_t) * numIndices, false, GFXBufferUsage::Index); + + mesh->index_buffer = + engine->get_gfx()->create_buffer(nullptr, sizeof(uint32_t) * numIndices, false, GFXBufferUsage::Index); auto index_ptr = reinterpret_cast(engine->get_gfx()->get_buffer_contents(mesh->index_buffer)); - if(index_ptr != nullptr) { + if (index_ptr != nullptr) { file->read(index_ptr, sizeof(uint32_t) * numIndices); } else { file->seek(sizeof(uint32_t) * numIndices); } - + engine->get_gfx()->release_buffer_contents(mesh->index_buffer, index_ptr); int bone_len = 0; file->read(&bone_len); - + mesh->bones.reserve(bone_len); - - if(bone_len > 0) { + + if (bone_len > 0) { file->read(&mesh->global_inverse_transformation); - + std::map boneMapping; std::map parentQueue; for (int v = 0; v < bone_len; v++) { std::string bone, parent; - + file->read_string(bone); file->read_string(parent); prism::float3 pos; file->read(&pos); - + Quaternion rot; file->read(&rot); prism::float3 scl; file->read(&scl); - - if(!boneMapping.count(bone)) { + + if (!boneMapping.count(bone)) { Bone b; b.index = mesh->bones.size(); b.name = bone; @@ -122,67 +124,68 @@ std::unique_ptr load_mesh(const prism::path& path) { b.position = pos; b.rotation = rot; b.scale = scl; - - if(parent != "none" && !parent.empty()) + + if (parent != "none" && !parent.empty()) parentQueue[b.index] = parent; - + mesh->bones.push_back(b); - + boneMapping[bone] = b.index; } } - - for(auto& [index, parentName] : parentQueue) { - for(auto& bone : mesh->bones) { - if(bone.name == parentName) + + for (auto& [index, parentName] : parentQueue) { + for (auto& bone : mesh->bones) { + if (bone.name == parentName) mesh->bones[index].parent = &bone; } } - - for(auto& bone : mesh->bones) { - if(bone.parent == nullptr) + + for (auto& bone : mesh->bones) { + if (bone.parent == nullptr) mesh->root_bone = &bone; } } - + int numMeshes = 0; file->read(&numMeshes); - + Expects(numMeshes > 0); - + mesh->parts.resize(numMeshes); uint32_t vertexOffset = 0, indexOffset = 0; - for(int i = 0; i < numMeshes; i++) { + for (int i = 0; i < numMeshes; i++) { auto& p = mesh->parts[i]; p.vertex_offset = vertexOffset; p.index_offset = indexOffset; file->read_string(p.name); - - if(version >= 6) { + + if (version >= 6) { file->read(&p.bounding_box); } - + int numVerts = 0; file->read(&numVerts); - + file->read(&p.index_count); - + int numBones = 0; file->read(&numBones); - - p.bone_batrix_buffer = engine->get_gfx()->create_buffer(nullptr, sizeof(Matrix4x4) * 128, true, GFXBufferUsage::Storage); - - if(numBones > 0) { + + p.bone_batrix_buffer = + engine->get_gfx()->create_buffer(nullptr, sizeof(Matrix4x4) * 128, true, GFXBufferUsage::Storage); + + if (numBones > 0) { p.offset_matrices.resize(numBones); - + file->read(p.offset_matrices.data(), sizeof(Matrix4x4) * numBones); } file->read(&p.material_override); - if(version == 7) { + if (version == 7) { file->read_string(p.material_hint); } @@ -197,52 +200,56 @@ std::unique_ptr load_mesh(const prism::path& path) { std::unique_ptr load_texture(const prism::path& path) { Expects(!path.empty()); - + auto file = prism::open_file(path, true); - if(!file.has_value()) { + if (!file.has_value()) { prism::log("Failed to load texture from {}!", path.string()); return nullptr; } - + // TODO: expose somehow?? const bool should_generate_mipmaps = true; file->read_all(); int width, height, channels; - unsigned char* data = stbi_load_from_memory(file->cast_data(), file->size(), &width, &height, &channels, 4); - if(!data) { + unsigned char* data = + stbi_load_from_memory(file->cast_data(), file->size(), &width, &height, &channels, 4); + if (!data) { prism::log("Failed to load texture from {}!", path.string()); return nullptr; } - + Expects(width > 0); Expects(height > 0); - + auto texture = std::make_unique(); texture->path = path.string(); texture->width = width; texture->height = height; - + GFXTextureCreateInfo createInfo = {}; createInfo.label = path.string(); createInfo.width = width; createInfo.height = height; createInfo.format = GFXPixelFormat::R8G8B8A8_UNORM; - createInfo.usage = GFXTextureUsage::Sampled | GFXTextureUsage::TransferDst | GFXTextureUsage::TransferSrc; // src and dst are needed for copy tex data -> image and mipmap gen (from image data) respectively - - if(should_generate_mipmaps) + createInfo.usage = GFXTextureUsage::Sampled | GFXTextureUsage::TransferDst | + GFXTextureUsage::TransferSrc; // src and dst are needed for copy tex + // data -> image and mipmap gen (from + // image data) respectively + + if (should_generate_mipmaps) createInfo.mip_count = std::floor(std::log2(std::max(width, height))) + 1; - + texture->handle = engine->get_gfx()->create_texture(createInfo); engine->get_gfx()->copy_texture(texture->handle, data, width * height * 4); - - if(createInfo.mip_count > 1) { + + if (createInfo.mip_count > 1) { GFXCommandBuffer* cmd_buf = engine->get_gfx()->acquire_command_buffer(false); - + cmd_buf->generate_mipmaps(texture->handle, createInfo.mip_count); - + engine->get_gfx()->submit(cmd_buf, nullptr); } @@ -253,20 +260,20 @@ std::unique_ptr load_texture(const prism::path& path) { std::unique_ptr load_material(const prism::path& path) { Expects(!path.empty()); - + auto file = prism::open_file(path); - if(!file.has_value()) { + if (!file.has_value()) { prism::log("Failed to load material from {}!", path.string()); return {}; } - + nlohmann::json j; file->read_as_stream() >> j; auto mat = std::make_unique(); mat->path = path.string(); - - if(!j.count("version") || j["version"] != 3) { + + if (!j.count("version") || j["version"] != 3) { prism::log("Material {} failed the version check!", path.string()); return mat; } @@ -277,7 +284,7 @@ std::unique_ptr load_material(const prism::path& path) { p.float_value = property["float_value"]; p.type = property["type"]; - if(!property["asset_value"].get().empty()) { + if (!property["asset_value"].get().empty()) { p.value_tex = assetm->get(prism::game_domain / property["asset_value"].get()); } @@ -286,7 +293,7 @@ std::unique_ptr load_material(const prism::path& path) { mat->colorProperty = loadProperty(j["color"]); - if(j.contains("normal")) + if (j.contains("normal")) mat->normalProperty = loadProperty(j["normal"]); return mat; @@ -295,7 +302,7 @@ std::unique_ptr load_material(const prism::path& path) { void save_material(Material* material, const prism::path& path) { Expects(material != nullptr); Expects(!path.empty()); - + nlohmann::json j; j["version"] = 3;