Archived
1
Fork 0

Try reformatting asset code with clang-format

This commit is contained in:
Joshua Goins 2022-08-15 10:54:34 -04:00
parent e2d29f34f2
commit 069318dc0b
5 changed files with 173 additions and 192 deletions

View file

@ -1,28 +1,23 @@
#pragma once #pragma once
#include <array>
#include <memory> #include <memory>
#include <unordered_map> #include <unordered_map>
#include <array>
#include "file.hpp"
#include "assetptr.hpp"
#include "asset_types.hpp" #include "asset_types.hpp"
#include "assetptr.hpp"
#include "file.hpp"
#include "string_utils.hpp" #include "string_utils.hpp"
template<typename T> template<typename T> std::unique_ptr<T> load_asset(const prism::path& p);
std::unique_ptr<T> load_asset(const prism::path& p);
template<typename T> template<typename T> bool can_load_asset(const prism::path& p);
bool can_load_asset(const prism::path& p);
template<class AssetType> template<class AssetType> using AssetStore = std::unordered_map<prism::path, std::unique_ptr<AssetType>>;
using AssetStore = std::unordered_map<prism::path, std::unique_ptr<AssetType>>;
template<class... Assets> template<class... Assets> class AssetPool : public AssetStore<Assets>... {
class AssetPool : public AssetStore<Assets>... {
public: public:
template<typename T> template<typename T> AssetPtr<T> add() {
AssetPtr<T> add() {
const auto p = prism::path(); const auto p = prism::path();
auto reference_block = get_reference_block(p); auto reference_block = get_reference_block(p);
@ -31,13 +26,9 @@ public:
return AssetPtr<T>(AssetStore<T>::at(p).get(), reference_block); return AssetPtr<T>(AssetStore<T>::at(p).get(), reference_block);
} }
template<typename T> template<typename T> AssetPtr<T> get(const prism::path path) { return fetch<T>(path, get_reference_block(path)); }
AssetPtr<T> get(const prism::path path) {
return fetch<T>(path, get_reference_block(path));
}
template<typename T> template<typename T> std::vector<T*> get_all() {
std::vector<T*> get_all() {
std::vector<T*> assets; 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; auto& [p, asset] = *iter;
@ -48,8 +39,7 @@ public:
return assets; return assets;
} }
template<typename T> template<typename T> AssetPtr<T> fetch(const prism::path path, ReferenceBlock* reference_block) {
AssetPtr<T> fetch(const prism::path path, ReferenceBlock* reference_block) {
if (!AssetStore<T>::count(path)) if (!AssetStore<T>::count(path))
AssetStore<T>::try_emplace(path, load_asset<T>(path)); AssetStore<T>::try_emplace(path, load_asset<T>(path));
@ -89,8 +79,7 @@ private:
return reference_blocks[path].get(); return reference_blocks[path].get();
} }
template<typename T> template<typename T> void load_asset_generic(const prism::path& path, Asset*& at, ReferenceBlock*& block) {
void load_asset_generic(const prism::path& path, Asset*& at, ReferenceBlock*& block) {
if (can_load_asset<T>(path)) { if (can_load_asset<T>(path)) {
if (!AssetStore<T>::count(path)) if (!AssetStore<T>::count(path))
AssetStore<T>::try_emplace(path, load_asset<T>(path)); AssetStore<T>::try_emplace(path, load_asset<T>(path));
@ -100,8 +89,7 @@ private:
} }
} }
template<typename T> template<typename T> void delete_asset(const prism::path path) {
void delete_asset(const prism::path path) {
auto iter = AssetStore<T>::find(path); auto iter = AssetStore<T>::find(path);
if (iter != AssetStore<T>::end()) { if (iter != AssetStore<T>::end()) {
auto& [_, asset] = *iter; auto& [_, asset] = *iter;
@ -123,8 +111,7 @@ std::unique_ptr<Texture> load_texture(const prism::path& path);
void save_material(Material* material, const prism::path& path); void save_material(Material* material, const prism::path& path);
template<typename T> template<typename T> std::unique_ptr<T> load_asset(const prism::path& path) {
std::unique_ptr<T> load_asset(const prism::path& path) {
if constexpr (std::is_same_v<T, Mesh>) { if constexpr (std::is_same_v<T, Mesh>) {
return load_mesh(path); return load_mesh(path);
} else if constexpr (std::is_same_v<T, Material>) { } else if constexpr (std::is_same_v<T, Material>) {
@ -134,8 +121,7 @@ std::unique_ptr<T> load_asset(const prism::path& path) {
} }
} }
template<typename T> template<typename T> bool can_load_asset(const prism::path& path) {
bool can_load_asset(const prism::path& path) {
if constexpr (std::is_same_v<T, Mesh>) { if constexpr (std::is_same_v<T, Mesh>) {
return path.extension() == ".model"; return path.extension() == ".model";
} else if constexpr (std::is_same_v<T, Material>) { } else if constexpr (std::is_same_v<T, Material>) {

View file

@ -2,9 +2,9 @@
#include <map> #include <map>
#include "aabb.hpp"
#include "assetptr.hpp" #include "assetptr.hpp"
#include "math.hpp" #include "math.hpp"
#include "aabb.hpp"
#include "utility.hpp" #include "utility.hpp"
class GFXBuffer; class GFXBuffer;
@ -75,8 +75,8 @@ struct Bone {
class Mesh : public Asset { class Mesh : public Asset {
public: public:
// meshes are rendered in parts if we cannot batch it in one call, i.e. a mesh // meshes are rendered in parts if we cannot batch it in one call, i.e. a
// with multiple materials with different textures, etc // mesh with multiple materials with different textures, etc
struct Part { struct Part {
std::string name; std::string name;
prism::aabb bounding_box; prism::aabb bounding_box;
@ -84,7 +84,8 @@ public:
GFXBuffer* bone_batrix_buffer = nullptr; GFXBuffer* bone_batrix_buffer = nullptr;
std::vector<Matrix4x4> offset_matrices; 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 vertex_offset = 0;
int32_t material_override = -1; int32_t material_override = -1;
std::string material_hint; std::string material_hint;

View file

@ -12,13 +12,10 @@ public:
std::string path; std::string path;
}; };
template<class T> template<class T> struct AssetPtr {
struct AssetPtr {
AssetPtr() = default; AssetPtr() = default;
AssetPtr(T* ptr, ReferenceBlock* block) : handle(ptr), block(block) { AssetPtr(T* ptr, ReferenceBlock* block) : handle(ptr), block(block) { block->references++; }
block->references++;
}
AssetPtr(const AssetPtr& rhs) { AssetPtr(const AssetPtr& rhs) {
handle = rhs.handle; handle = rhs.handle;
@ -56,23 +53,13 @@ struct AssetPtr {
T* handle = nullptr; T* handle = nullptr;
ReferenceBlock* block = nullptr; ReferenceBlock* block = nullptr;
explicit operator bool() const{ explicit operator bool() const { return handle != nullptr; }
return handle != nullptr;
}
T* operator->() { T* operator->() { return handle; }
return handle;
}
T* operator->() const { T* operator->() const { return handle; }
return handle;
}
T* operator*() { T* operator*() { return handle; }
return handle;
}
T* operator*() const { T* operator*() const { return handle; }
return handle;
}
}; };

View file

@ -1,19 +1,19 @@
#include "asset.hpp" #include "asset.hpp"
#include <map>
#include <array> #include <array>
#include <map>
#include <stb_image.h> #include <stb_image.h>
#include "log.hpp" #include "assertions.hpp"
#include "engine.hpp" #include "engine.hpp"
#include "gfx.hpp" #include "gfx.hpp"
#include "json_conversions.hpp"
#include "gfx_commandbuffer.hpp" #include "gfx_commandbuffer.hpp"
#include "assertions.hpp"
#include "renderer.hpp"
#include "input.hpp"
#include "physics.hpp"
#include "imgui_backend.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) { std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
Expects(!path.empty()); Expects(!path.empty());
@ -50,7 +50,8 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
Expects(numVertices > 0); Expects(numVertices > 0);
const auto read_buffer = [&f = file.value(), numVertices](unsigned int size) -> GFXBuffer* { 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)); 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)); f.read(buffer_ptr, size * static_cast<unsigned int>(numVertices));
@ -78,7 +79,8 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
Expects(numIndices > 0); 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)); 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); file->read(index_ptr, sizeof(uint32_t) * numIndices);
@ -172,7 +174,8 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
int numBones = 0; int numBones = 0;
file->read(&numBones); 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); p.offset_matrices.resize(numBones);
@ -210,7 +213,8 @@ std::unique_ptr<Texture> load_texture(const prism::path& path) {
file->read_all(); file->read_all();
int width, height, channels; int width, height, channels;
unsigned char* data = stbi_load_from_memory(file->cast_data<unsigned char>(), file->size(), &width, &height, &channels, 4); unsigned char* data =
stbi_load_from_memory(file->cast_data<unsigned char>(), file->size(), &width, &height, &channels, 4);
if (!data) { if (!data) {
prism::log("Failed to load texture from {}!", path.string()); prism::log("Failed to load texture from {}!", path.string());
return nullptr; return nullptr;
@ -229,7 +233,10 @@ std::unique_ptr<Texture> load_texture(const prism::path& path) {
createInfo.width = width; createInfo.width = width;
createInfo.height = height; createInfo.height = height;
createInfo.format = GFXPixelFormat::R8G8B8A8_UNORM; 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; createInfo.mip_count = std::floor(std::log2(std::max(width, height))) + 1;