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,113 +1,101 @@
|
|||
#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);
|
||||
|
||||
|
||||
AssetStore<T>::try_emplace(p, std::make_unique<T>());
|
||||
|
||||
|
||||
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>
|
||||
std::vector<T*> get_all() {
|
||||
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() {
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
std::tuple<Asset*, ReferenceBlock*> load_asset_generic(const prism::path path) {
|
||||
Asset* asset = nullptr;
|
||||
ReferenceBlock* block = nullptr;
|
||||
|
||||
|
||||
(load_asset_generic<Assets>(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<Assets>(path)), ...);
|
||||
|
||||
|
||||
iter = reference_blocks.erase(iter);
|
||||
} else {
|
||||
iter = std::next(iter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::unordered_map<prism::path, std::unique_ptr<ReferenceBlock>> 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<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();
|
||||
block = get_reference_block(path);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
|
||||
AssetStore<T>::erase(iter);
|
||||
}
|
||||
}
|
||||
|
@ -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,16 +2,16 @@
|
|||
|
||||
#include <map>
|
||||
|
||||
#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<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;
|
||||
|
@ -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;
|
||||
|
|
|
@ -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,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; }
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
|
@ -35,34 +35,35 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
|||
|
||||
auto mesh = std::make_unique<Mesh>();
|
||||
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<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);
|
||||
} else {
|
||||
f.seek(size * static_cast<unsigned int>(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<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;
|
||||
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<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);
|
||||
}
|
||||
|
||||
|
||||
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<std::string, uint32_t> boneMapping;
|
||||
std::map<size_t, std::string> 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<Mesh> 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<Mesh> load_mesh(const prism::path& path) {
|
|||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
// 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<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;
|
||||
}
|
||||
|
||||
|
||||
Expects(width > 0);
|
||||
Expects(height > 0);
|
||||
|
||||
|
||||
auto texture = std::make_unique<Texture>();
|
||||
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<Texture> load_texture(const prism::path& path) {
|
|||
|
||||
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 {};
|
||||
}
|
||||
|
||||
|
||||
nlohmann::json j;
|
||||
file->read_as_stream() >> j;
|
||||
|
||||
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;
|
||||
|
@ -295,7 +302,7 @@ std::unique_ptr<Material> 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;
|
||||
|
||||
|
|
Reference in a new issue