diff options
| author | ladevieq <ladevie.quentin@gmail.com> | 2022-08-08 23:38:19 +0200 |
|---|---|---|
| committer | ladevieq <ladevie.quentin@gmail.com> | 2022-08-08 23:38:19 +0200 |
| commit | d78200b71b259b87f401b24906385af4e2553fbe (patch) | |
| tree | a3f50e975303896063f49c069752bd1be774e766 /include | |
| parent | 45247b737b1527fbeffac2c68a41e71c565d86a8 (diff) | |
Refactored gltf imported and mesh management
Diffstat (limited to 'include')
| -rw-r--r-- | include/gltf.hpp | 48 | ||||
| -rw-r--r-- | include/mesh.hpp | 93 | ||||
| -rw-r--r-- | include/primitive-renderpass.hpp | 4 | ||||
| -rw-r--r-- | include/scene.hpp | 3 | ||||
| -rw-r--r-- | include/vk-renderer.hpp | 58 |
5 files changed, 159 insertions, 47 deletions
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 <vector>
-#include <optional>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
#include "material.hpp"
-
-struct mesh_part {
- std::vector<uint16_t> indices;
- std::vector<float> positions;
- std::vector<float> normals;
- std::vector<float> uvs;
- material mat;
- uint32_t triangles_count = 0;
- uint32_t vertices_count = 0;
-};
-
-struct mesh {
- std::vector<mesh_part> parts;
- uint32_t triangles_count = 0;
- uint32_t vertices_count = 0;
-};
+#include "mesh.hpp"
struct node {
std::vector<node> children;
- std::optional<mesh> 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<mesh> &meshes);
+ Mesh::submesh load_primitive(const json &primitive);
- static std::vector<mesh> load_meshes(
- json &gltf_json, std::vector<std::vector<uint8_t>> &buffers_content, std::vector<material> &materials);
+ Mesh::attribute load_attribute(uint32_t accessor_index);
- static std::vector<texture> load_textures(json &gltf_json, std::filesystem::path &path, vkrenderer &renderer);
+ void load_textures(const std::filesystem::path &path);
- static std::vector<material> load_materials(json &gltf_json, std::vector<texture> &textures);
+ void load_materials();
- static mesh_part load_primitive(json &gltf_json, json &primitive,
- std::vector<std::vector<uint8_t>> &buffers_content, std::vector<material> &materials);
+ json gltf_json;
+ std::vector<Mesh> meshes;
+ std::vector<material> materials;
+ std::vector<texture> textures;
+ std::vector<std::vector<uint8_t>> 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 <cstdint> +#include <vector> +#include <array> + +#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<attribute, ATTRIBUTE_TYPE::MAX_ATTRIBUTE> 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<std::vector<float>, ATTRIBUTE_TYPE::MAX_ATTRIBUTE> attributes; + std::vector<uint16_t> indices; + + std::vector<submesh_info> submeshes; + + struct gpu_mesh { + std::array<Buffer*, ATTRIBUTE_TYPE::MAX_ATTRIBUTE> attributes; + Buffer* indices; + Buffer* submeshes; + }; + + // allocate gpu mesh upon instanciation + // static std::vector<gpu_mesh> gpu_meshes; + // static std::vector<transform> 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 <cstdint> #include <vector> #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_ |
