From d78200b71b259b87f401b24906385af4e2553fbe Mon Sep 17 00:00:00 2001 From: ladevieq Date: Mon, 8 Aug 2022 23:38:19 +0200 Subject: Refactored gltf imported and mesh management --- include/gltf.hpp | 48 +++++++++------------ include/mesh.hpp | 93 ++++++++++++++++++++++++++++++++++++++++ include/primitive-renderpass.hpp | 4 +- include/scene.hpp | 3 +- include/vk-renderer.hpp | 58 +++++++++++++++++++------ 5 files changed, 159 insertions(+), 47 deletions(-) create mode 100644 include/mesh.hpp (limited to 'include') diff --git a/include/gltf.hpp b/include/gltf.hpp index 66fb6ec..2886dea 100644 --- a/include/gltf.hpp +++ b/include/gltf.hpp @@ -2,56 +2,48 @@ #define __GLTF_HPP_ #include -#include #include using json = nlohmann::json; #include "material.hpp" - -struct mesh_part { - std::vector indices; - std::vector positions; - std::vector normals; - std::vector uvs; - material mat; - uint32_t triangles_count = 0; - uint32_t vertices_count = 0; -}; - -struct mesh { - std::vector parts; - uint32_t triangles_count = 0; - uint32_t vertices_count = 0; -}; +#include "mesh.hpp" struct node { std::vector children; - std::optional mesh; + Mesh* mesh = nullptr; }; namespace std::filesystem { class path; } -class vkrenderer; class gltf { public: - static node load(std::filesystem::path &path, std::string &filename, vkrenderer &renderer); + gltf(const std::filesystem::path &filepath); + + node root_node; + +private: + + void load_node(uint32_t index, node &parent); + + void load_meshes(); - private: - static void load_node(json &gltf_json, uint32_t index, node &parent, std::vector &meshes); + Mesh::submesh load_primitive(const json &primitive); - static std::vector load_meshes( - json &gltf_json, std::vector> &buffers_content, std::vector &materials); + Mesh::attribute load_attribute(uint32_t accessor_index); - static std::vector load_textures(json &gltf_json, std::filesystem::path &path, vkrenderer &renderer); + void load_textures(const std::filesystem::path &path); - static std::vector load_materials(json &gltf_json, std::vector &textures); + void load_materials(); - static mesh_part load_primitive(json &gltf_json, json &primitive, - std::vector> &buffers_content, std::vector &materials); + json gltf_json; + std::vector meshes; + std::vector materials; + std::vector textures; + std::vector> buffers; }; #endif diff --git a/include/mesh.hpp b/include/mesh.hpp new file mode 100644 index 0000000..34f2df8 --- /dev/null +++ b/include/mesh.hpp @@ -0,0 +1,93 @@ +#pragma once + +#include +#include +#include + +#include "vk-renderer.hpp" + +enum ATTRIBUTE_TYPE { + POSITION, + NORMAL, + TANGENT, + UV_0, + MAX_ATTRIBUTE, +}; + +struct material; + +class Mesh { + struct submesh_info { + uint32_t vertex_offset; + uint32_t index_offset; + size_t element_count; + uint32_t mat_id; + }; + +public: + struct mesh_instance { + const Mesh& mesh; + // transform a transform matrice for the instance + }; + + struct attribute { + uint8_t* data = nullptr; + size_t length = 0; + }; + + struct submesh { + attribute indices; + std::array attributes; + uint32_t vertex_count = 0; + uint32_t index_count = 0; + // material& mat; + }; + + Mesh() = default; + + [[nodiscard]] mesh_instance instantiate () const; + + void add_submesh(const submesh& submesh); + + static inline uint32_t attribute_components(enum ATTRIBUTE_TYPE attribute_type) { + switch(attribute_type) { + case ATTRIBUTE_TYPE::POSITION: + case ATTRIBUTE_TYPE::NORMAL: + return 3; + case ATTRIBUTE_TYPE::UV_0: + return 2; + default: + return 0; + } + } + + static inline size_t attribute_size(enum ATTRIBUTE_TYPE attribute_type) { + return sizeof(float) * attribute_components(attribute_type); + } + + [[nodiscard]] inline uint32_t triangle_count() const { return index_count / 3; }; + [[nodiscard]] inline uint32_t vertices_count() const { return vertex_count; }; + + + static void register_mesh(const Mesh& mesh); + +private: + + uint32_t vertex_count; + uint32_t index_count; + + std::array, ATTRIBUTE_TYPE::MAX_ATTRIBUTE> attributes; + std::vector indices; + + std::vector submeshes; + + struct gpu_mesh { + std::array attributes; + Buffer* indices; + Buffer* submeshes; + }; + + // allocate gpu mesh upon instanciation + // static std::vector gpu_meshes; + // static std::vector instances; +}; diff --git a/include/primitive-renderpass.hpp b/include/primitive-renderpass.hpp index 4d90d3c..95562fe 100644 --- a/include/primitive-renderpass.hpp +++ b/include/primitive-renderpass.hpp @@ -52,9 +52,7 @@ class PrimitiveRenderpass : public Renderpass { class Primitive { public: - Primitive(vkapi& api, PrimitiveRenderpass& render_pass) - : api(api), primitive_render_pass(render_pass) - {}; + Primitive(PrimitiveRenderpass& render_pass); void set_pipeline(const char* shader_name); diff --git a/include/scene.hpp b/include/scene.hpp index a2db2e8..78b90b7 100644 --- a/include/scene.hpp +++ b/include/scene.hpp @@ -6,7 +6,6 @@ #include "camera.hpp" class Buffer; -class vkrenderer; class scene { struct metadata { @@ -30,7 +29,7 @@ class scene { // void random_scene(); public: - scene(const camera& cam, uint32_t width, uint32_t height, vkrenderer& renderer); + scene(const camera& cam, uint32_t width, uint32_t height); metadata meta; diff --git a/include/vk-renderer.hpp b/include/vk-renderer.hpp index deee33b..827445f 100644 --- a/include/vk-renderer.hpp +++ b/include/vk-renderer.hpp @@ -1,6 +1,7 @@ #ifndef __VK_RENDERER_HPP_ #define __VK_RENDERER_HPP_ +#include #include #include "vk-context.hpp" @@ -31,17 +32,15 @@ class vkrenderer { void update_image(Texture* texture, void* data); - void update_buffer(Buffer* buffer, void* data, off_t offset, size_t size); + static Buffer* create_buffer(size_t size); - Buffer* create_buffer(size_t size, bool isStatic); + static Buffer* create_index_buffer(size_t size); - Buffer* create_index_buffer(size_t size, bool isStatic); + static Texture* create_2d_texture(size_t width, size_t height, VkFormat format); - Texture* create_2d_texture(size_t width, size_t height, VkFormat format); + static void destroy_2d_texture(Texture* texture); - void destroy_2d_texture(Texture* texture); - - Sampler* create_sampler(VkFilter filter, VkSamplerAddressMode address_mode); + static Sampler* create_sampler(VkFilter filter, VkSamplerAddressMode address_mode); ComputeRenderpass* create_compute_renderpass(); @@ -59,9 +58,9 @@ class vkrenderer { static constexpr uint32_t virtual_frames_count = 2; - vkcontext context; + static vkcontext context; - vkapi api; + static vkapi api; private: @@ -110,15 +109,46 @@ class Sampler { class Buffer { public: - Buffer(size_t size, bool isStatic) - : size(size), isStatic(isStatic) {} + Buffer(size_t size); + + ~Buffer() { + vkrenderer::api.destroy_buffer(device_buffer); + } + + void write(void* data, off_t alloc_offset, size_t data_size) const { + auto api_buffer = vkrenderer::api.get_buffer(device_buffer); + + std::memcpy((uint8_t*)api_buffer.device_ptr + alloc_offset, data, data_size); + } handle device_buffer; - size_t size; - size_t element_size; + size_t buffer_size; +}; + +class RingBuffer { + public: + RingBuffer(size_t size) + : buffer_size(size) {} + + [[nodiscard]]bool alloc(size_t alloc_size) const { + return ptr + alloc_size > buffer_size; + } + + void write(void* data, off_t alloc_offset, size_t data_size) const { + auto api_buffer = vkrenderer::api.get_buffer(device_buffer); + + std::memcpy((uint8_t*)api_buffer.device_ptr + alloc_offset, data, data_size); + } + + void reset() { + ptr = 0; + } + + handle device_buffer; - bool isStatic; + size_t buffer_size; + off_t ptr; }; #endif // !__VK_RENDERER_HPP_ -- cgit v1.3