Try reformatting asset code with clang-format
This commit is contained in:
parent
e2d29f34f2
commit
069318dc0b
5 changed files with 173 additions and 192 deletions
|
@ -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)
|
||||
|
|
|
@ -1,28 +1,23 @@
|
|||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <array>
|
||||
|
||||
#include "file.hpp"
|
||||
#include "assetptr.hpp"
|
||||
#include "asset_types.hpp"
|
||||
#include "assetptr.hpp"
|
||||
#include "file.hpp"
|
||||
#include "string_utils.hpp"
|
||||
|
||||
template<typename T>
|
||||
std::unique_ptr<T> load_asset(const prism::path& p);
|
||||
template<typename T> std::unique_ptr<T> load_asset(const prism::path& p);
|
||||
|
||||
template<typename T>
|
||||
bool can_load_asset(const prism::path& p);
|
||||
template<typename T> bool can_load_asset(const prism::path& p);
|
||||
|
||||
template<class AssetType>
|
||||
using AssetStore = std::unordered_map<prism::path, std::unique_ptr<AssetType>>;
|
||||
template<class AssetType> using AssetStore = std::unordered_map<prism::path, std::unique_ptr<AssetType>>;
|
||||
|
||||
template<class... Assets>
|
||||
class AssetPool : public AssetStore<Assets>... {
|
||||
template<class... Assets> class AssetPool : public AssetStore<Assets>... {
|
||||
public:
|
||||
template<typename T>
|
||||
AssetPtr<T> add() {
|
||||
template<typename T> AssetPtr<T> add() {
|
||||
const auto p = prism::path();
|
||||
auto reference_block = get_reference_block(p);
|
||||
|
||||
|
@ -31,15 +26,11 @@ public:
|
|||
return AssetPtr<T>(AssetStore<T>::at(p).get(), reference_block);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
AssetPtr<T> get(const prism::path path) {
|
||||
return fetch<T>(path, get_reference_block(path));
|
||||
}
|
||||
template<typename T> AssetPtr<T> get(const prism::path path) { return fetch<T>(path, get_reference_block(path)); }
|
||||
|
||||
template<typename T>
|
||||
std::vector<T*> get_all() {
|
||||
template<typename T> std::vector<T*> get_all() {
|
||||
std::vector<T*> assets;
|
||||
for(auto iter = AssetStore<T>::begin(); iter != AssetStore<T>::end(); iter++) {
|
||||
for (auto iter = AssetStore<T>::begin(); iter != AssetStore<T>::end(); iter++) {
|
||||
auto& [p, asset] = *iter;
|
||||
|
||||
assets.push_back(asset.get());
|
||||
|
@ -48,9 +39,8 @@ public:
|
|||
return assets;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
AssetPtr<T> fetch(const prism::path path, ReferenceBlock* reference_block) {
|
||||
if(!AssetStore<T>::count(path))
|
||||
template<typename T> AssetPtr<T> fetch(const prism::path path, ReferenceBlock* reference_block) {
|
||||
if (!AssetStore<T>::count(path))
|
||||
AssetStore<T>::try_emplace(path, load_asset<T>(path));
|
||||
|
||||
return AssetPtr<T>(AssetStore<T>::at(path).get(), reference_block);
|
||||
|
@ -66,10 +56,10 @@ public:
|
|||
}
|
||||
|
||||
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<Assets>(path)), ...);
|
||||
|
||||
iter = reference_blocks.erase(iter);
|
||||
|
@ -83,16 +73,15 @@ public:
|
|||
|
||||
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<ReferenceBlock>());
|
||||
|
||||
return reference_blocks[path].get();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void load_asset_generic(const prism::path& path, Asset*& at, ReferenceBlock*& block) {
|
||||
if(can_load_asset<T>(path)) {
|
||||
if(!AssetStore<T>::count(path))
|
||||
template<typename T> void load_asset_generic(const prism::path& path, Asset*& at, ReferenceBlock*& block) {
|
||||
if (can_load_asset<T>(path)) {
|
||||
if (!AssetStore<T>::count(path))
|
||||
AssetStore<T>::try_emplace(path, load_asset<T>(path));
|
||||
|
||||
at = AssetStore<T>::at(path).get();
|
||||
|
@ -100,10 +89,9 @@ private:
|
|||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void delete_asset(const prism::path path) {
|
||||
template<typename T> void delete_asset(const prism::path path) {
|
||||
auto iter = AssetStore<T>::find(path);
|
||||
if(iter != AssetStore<T>::end()) {
|
||||
if (iter != AssetStore<T>::end()) {
|
||||
auto& [_, asset] = *iter;
|
||||
|
||||
asset.reset();
|
||||
|
@ -123,24 +111,22 @@ std::unique_ptr<Texture> load_texture(const prism::path& path);
|
|||
|
||||
void save_material(Material* material, const prism::path& path);
|
||||
|
||||
template<typename T>
|
||||
std::unique_ptr<T> load_asset(const prism::path& path) {
|
||||
template<typename T> std::unique_ptr<T> load_asset(const prism::path& path) {
|
||||
if constexpr (std::is_same_v<T, Mesh>) {
|
||||
return load_mesh(path);
|
||||
} else if constexpr(std::is_same_v<T, Material>) {
|
||||
} else if constexpr (std::is_same_v<T, Material>) {
|
||||
return load_material(path);
|
||||
} else if constexpr(std::is_same_v<T, Texture>) {
|
||||
} else if constexpr (std::is_same_v<T, Texture>) {
|
||||
return load_texture(path);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool can_load_asset(const prism::path& path) {
|
||||
if constexpr(std::is_same_v<T, Mesh>) {
|
||||
template<typename T> bool can_load_asset(const prism::path& path) {
|
||||
if constexpr (std::is_same_v<T, Mesh>) {
|
||||
return path.extension() == ".model";
|
||||
} else if constexpr(std::is_same_v<T, Material>) {
|
||||
} else if constexpr (std::is_same_v<T, Material>) {
|
||||
return path.extension() == ".material";
|
||||
} else if constexpr(std::is_same_v<T, Texture>) {
|
||||
} else if constexpr (std::is_same_v<T, Texture>) {
|
||||
return path.extension() == ".png";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
|
||||
#include <map>
|
||||
|
||||
#include "aabb.hpp"
|
||||
#include "assetptr.hpp"
|
||||
#include "math.hpp"
|
||||
#include "aabb.hpp"
|
||||
#include "utility.hpp"
|
||||
|
||||
class GFXBuffer;
|
||||
|
@ -75,8 +75,8 @@ 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;
|
||||
|
@ -84,7 +84,8 @@ public:
|
|||
GFXBuffer* bone_batrix_buffer = nullptr;
|
||||
std::vector<Matrix4x4> 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;
|
||||
|
|
|
@ -12,24 +12,21 @@ public:
|
|||
std::string path;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
struct AssetPtr {
|
||||
template<class T> 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,12 +38,12 @@ struct AssetPtr {
|
|||
}
|
||||
|
||||
~AssetPtr() {
|
||||
if(block != nullptr)
|
||||
if (block != nullptr)
|
||||
block->references--;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
if(block != nullptr)
|
||||
if (block != nullptr)
|
||||
block->references--;
|
||||
|
||||
block = 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->() const { return handle; }
|
||||
|
||||
T* operator*() {
|
||||
return handle;
|
||||
}
|
||||
T* operator*() { return handle; }
|
||||
|
||||
T* operator*() const {
|
||||
return handle;
|
||||
}
|
||||
T* operator*() const { return handle; }
|
||||
};
|
||||
|
|
|
@ -1,25 +1,25 @@
|
|||
#include "asset.hpp"
|
||||
|
||||
#include <map>
|
||||
#include <array>
|
||||
#include <map>
|
||||
#include <stb_image.h>
|
||||
|
||||
#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<Mesh> 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<Mesh> 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;
|
||||
|
@ -50,9 +50,10 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
|||
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<unsigned int>(numVertices), false, GFXBufferUsage::Vertex);
|
||||
auto buffer = engine->get_gfx()->create_buffer(
|
||||
nullptr, size * static_cast<unsigned int>(numVertices), false, GFXBufferUsage::Vertex);
|
||||
auto buffer_ptr = reinterpret_cast<unsigned char*>(engine->get_gfx()->get_buffer_contents(buffer));
|
||||
if(buffer_ptr != nullptr) {
|
||||
if (buffer_ptr != nullptr) {
|
||||
f.read(buffer_ptr, size * static_cast<unsigned int>(numVertices));
|
||||
|
||||
engine->get_gfx()->release_buffer_contents(buffer, buffer_ptr);
|
||||
|
@ -70,7 +71,7 @@ std::unique_ptr<Mesh> 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;
|
||||
|
@ -78,9 +79,10 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
|||
|
||||
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<uint32_t*>(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);
|
||||
|
@ -93,7 +95,7 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
|||
|
||||
mesh->bones.reserve(bone_len);
|
||||
|
||||
if(bone_len > 0) {
|
||||
if (bone_len > 0) {
|
||||
file->read(&mesh->global_inverse_transformation);
|
||||
|
||||
std::map<std::string, uint32_t> boneMapping;
|
||||
|
@ -114,7 +116,7 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
|||
prism::float3 scl;
|
||||
file->read(&scl);
|
||||
|
||||
if(!boneMapping.count(bone)) {
|
||||
if (!boneMapping.count(bone)) {
|
||||
Bone b;
|
||||
b.index = mesh->bones.size();
|
||||
b.name = bone;
|
||||
|
@ -123,7 +125,7 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
|||
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);
|
||||
|
@ -132,15 +134,15 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
|||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -153,14 +155,14 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
|||
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);
|
||||
}
|
||||
|
||||
|
@ -172,9 +174,10 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
|||
int numBones = 0;
|
||||
file->read(&numBones);
|
||||
|
||||
p.bone_batrix_buffer = engine->get_gfx()->create_buffer(nullptr, sizeof(Matrix4x4) * 128, true, GFXBufferUsage::Storage);
|
||||
p.bone_batrix_buffer =
|
||||
engine->get_gfx()->create_buffer(nullptr, sizeof(Matrix4x4) * 128, true, GFXBufferUsage::Storage);
|
||||
|
||||
if(numBones > 0) {
|
||||
if (numBones > 0) {
|
||||
p.offset_matrices.resize(numBones);
|
||||
|
||||
file->read(p.offset_matrices.data(), sizeof(Matrix4x4) * numBones);
|
||||
|
@ -182,7 +185,7 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
|||
|
||||
file->read(&p.material_override);
|
||||
|
||||
if(version == 7) {
|
||||
if (version == 7) {
|
||||
file->read_string(p.material_hint);
|
||||
}
|
||||
|
||||
|
@ -199,7 +202,7 @@ std::unique_ptr<Texture> 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;
|
||||
}
|
||||
|
@ -210,8 +213,9 @@ std::unique_ptr<Texture> load_texture(const prism::path& path) {
|
|||
file->read_all();
|
||||
|
||||
int width, height, channels;
|
||||
unsigned char* data = stbi_load_from_memory(file->cast_data<unsigned char>(), file->size(), &width, &height, &channels, 4);
|
||||
if(!data) {
|
||||
unsigned char* data =
|
||||
stbi_load_from_memory(file->cast_data<unsigned char>(), file->size(), &width, &height, &channels, 4);
|
||||
if (!data) {
|
||||
prism::log("Failed to load texture from {}!", path.string());
|
||||
return nullptr;
|
||||
}
|
||||
|
@ -229,16 +233,19 @@ std::unique_ptr<Texture> load_texture(const prism::path& path) {
|
|||
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
|
||||
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)
|
||||
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);
|
||||
|
@ -255,7 +262,7 @@ std::unique_ptr<Material> 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 {};
|
||||
}
|
||||
|
@ -266,7 +273,7 @@ std::unique_ptr<Material> load_material(const prism::path& path) {
|
|||
auto mat = std::make_unique<Material>();
|
||||
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<Material> load_material(const prism::path& path) {
|
|||
p.float_value = property["float_value"];
|
||||
p.type = property["type"];
|
||||
|
||||
if(!property["asset_value"].get<std::string>().empty()) {
|
||||
if (!property["asset_value"].get<std::string>().empty()) {
|
||||
p.value_tex = assetm->get<Texture>(prism::game_domain / property["asset_value"].get<std::string>());
|
||||
}
|
||||
|
||||
|
@ -286,7 +293,7 @@ std::unique_ptr<Material> 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;
|
||||
|
|
Reference in a new issue