From b0708f165601fad16aa59715dbe32796a6e7081e Mon Sep 17 00:00:00 2001 From: ladevieq Date: Tue, 6 Sep 2022 22:41:00 +0200 Subject: Setup for texture upload system --- include/gltf.hpp | 9 +++- include/material.hpp | 13 +++--- include/mesh.hpp | 6 +-- include/vk-api.hpp | 13 +++--- include/vk-context.hpp | 21 ++++----- include/vk-renderer.hpp | 113 +++++++++++++++++++++++++++++++++--------------- 6 files changed, 112 insertions(+), 63 deletions(-) (limited to 'include') diff --git a/include/gltf.hpp b/include/gltf.hpp index 2886dea..59189d4 100644 --- a/include/gltf.hpp +++ b/include/gltf.hpp @@ -15,6 +15,13 @@ struct node { Mesh* mesh = nullptr; }; +struct raw_image { + int32_t x; + int32_t y; + int32_t n; + uint8_t *data; +}; + namespace std::filesystem { class path; } @@ -42,7 +49,7 @@ private: json gltf_json; std::vector meshes; std::vector materials; - std::vector textures; + std::vector textures; std::vector> buffers; }; diff --git a/include/material.hpp b/include/material.hpp index efa6141..5ac08f1 100644 --- a/include/material.hpp +++ b/include/material.hpp @@ -1,11 +1,12 @@ -#ifndef __MATERIAL_HPP_ -#define __MATERIAL_HPP_ +#pragma once #include #include "vec3.hpp" -struct texture { +class Texture; + +struct gpu_texture { uint32_t image_id; uint32_t sampler_id; uint32_t padding[2]; @@ -13,10 +14,8 @@ struct texture { struct material { color base_color; - texture base_color_texture; - texture metallic_roughness_texture; + Texture* base_color_texture; + Texture* metallic_roughness_texture; float metalness = 1.f; float roughness = 1.f; }; - -#endif // !__MATERIAL_HPP_ diff --git a/include/mesh.hpp b/include/mesh.hpp index b95c2a8..9a30be6 100644 --- a/include/mesh.hpp +++ b/include/mesh.hpp @@ -20,7 +20,7 @@ class Mesh { uint32_t vertex_offset; uint32_t index_offset; size_t element_count; - uint32_t mat_id; + material& material; }; public: @@ -39,14 +39,14 @@ public: std::array attributes; uint32_t vertex_count = 0; uint32_t index_count = 0; - // material& mat; + // material& material; }; Mesh() = default; [[nodiscard]] mesh_instance instantiate () const; - void add_submesh(const submesh& submesh); + void add_submesh(const submesh& submesh, material& material); static inline uint32_t attribute_components(enum ATTRIBUTE_TYPE attribute_type) { switch(attribute_type) { diff --git a/include/vk-api.hpp b/include/vk-api.hpp index 2b88e42..6baaa73 100644 --- a/include/vk-api.hpp +++ b/include/vk-api.hpp @@ -140,8 +140,9 @@ class vkapi { void destroy_sampler(handle sampler); - std::vector create_command_buffers(size_t command_buffers_count); - void destroy_command_buffers(std::vector &command_buffers); + [[nodiscard]] VkCommandPool create_command_pool() const; + + void allocate_command_buffers(VkCommandPool command_pool, VkCommandBuffer* command_buffers, size_t count) const; VkRenderPass create_render_pass(std::vector& color_attachments_format, VkImageLayout initial_layout, VkImageLayout final_layout); @@ -160,10 +161,6 @@ class vkapi { void destroy_pipeline(pipeline &pipeline) const; - std::vector create_descriptor_sets(VkDescriptorSetLayout descriptor_sets_layout, size_t descriptor_sets_count); - void destroy_descriptor_sets(std::vector &descriptor_sets); - - VkSurfaceKHR create_surface(window& wnd) const; void destroy_surface(VkSurfaceKHR surface); @@ -195,7 +192,8 @@ class vkapi { void end_record(VkCommandBuffer command_buffer); - VkResult submit(VkCommandBuffer command_buffer, VkSemaphore wait_semaphore, VkSemaphore signal_semaphore, VkFence submission_fence) const; + VkResult submit_graphics(VkCommandBuffer command_buffer, VkSemaphore wait_semaphore, VkSemaphore signal_semaphore, VkFence submission_fence) const; + VkResult submit_copy(VkCommandBuffer command_buffer, VkSemaphore wait_semaphore, VkSemaphore signal_semaphore, VkFence submission_fence) const; VkResult present(swapchain& swapchain, uint32_t image_index, VkSemaphore wait_semaphore) const; @@ -216,7 +214,6 @@ class vkapi { std::vector images; std::vector samplers; - VkCommandPool command_pool; VkDescriptorPool descriptor_pool; }; diff --git a/include/vk-context.hpp b/include/vk-context.hpp index 8d0b91f..58d7ce9 100644 --- a/include/vk-context.hpp +++ b/include/vk-context.hpp @@ -1,11 +1,15 @@ -#ifndef __VK_CONTEXT_HPP_ -#define __VK_CONTEXT_HPP_ +#pragma once -#include #include "vulkan-loader.hpp" using VmaAllocator = struct VmaAllocator_T*; +struct queue { + VkQueue handle; + uint32_t index; + VkQueueFlags usages; +}; + class vkcontext { public: vkcontext(); @@ -19,8 +23,7 @@ class vkcontext { VmaAllocator allocator; - uint32_t queue_index; - VkQueue queue; + queue graphics_queue; private: void create_instance(); @@ -33,16 +36,14 @@ class vkcontext { void select_physical_device(); - void select_queue(VkQueueFlags queueUsage); + [[nodiscard]] uint32_t select_queue(VkQueueFlags queue_usage) const; - static void check_available_instance_layers(std::vector& needed_layers); + static void check_available_instance_layers(const char* needed_layers[], size_t needed_layers_count); - static void check_available_instance_extensions(std::vector& needed_extensions); + static void check_available_instance_extensions(const char* needed_extensions[], size_t needed_extensions_count); static bool support_required_features(VkPhysicalDevice physical_device); VkDebugUtilsMessengerEXT debugMessenger; }; - -#endif // !__VK_CONTEXT_HPP_ diff --git a/include/vk-renderer.hpp b/include/vk-renderer.hpp index 827445f..ca4c261 100644 --- a/include/vk-renderer.hpp +++ b/include/vk-renderer.hpp @@ -17,6 +17,7 @@ class window; class Texture; class Buffer; class Sampler; +class RingBuffer; class vkrenderer { public: @@ -24,24 +25,20 @@ class vkrenderer { ~vkrenderer(); - void begin_frame(); - - void finish_frame(); - void recreate_swapchain(); - void update_image(Texture* texture, void* data); static Buffer* create_buffer(size_t size); static Buffer* create_index_buffer(size_t size); - static Texture* create_2d_texture(size_t width, size_t height, VkFormat format); + static Texture* create_2d_texture(size_t width, size_t height, VkFormat format, Sampler *sampler = nullptr); static void destroy_2d_texture(Texture* texture); static Sampler* create_sampler(VkFilter filter, VkSamplerAddressMode address_mode); + ComputeRenderpass* create_compute_renderpass(); PrimitiveRenderpass* create_primitive_renderpass(); @@ -50,6 +47,8 @@ class vkrenderer { void render(); + RingBuffer* get_staging_buffer() { return staging_buffer; } + [[nodiscard]]uint32_t frame_index() const { return this->virtual_frame_index; } [[nodiscard]]Texture* back_buffer() const { return swapchain_textures[swapchain_image_index]; } @@ -64,18 +63,23 @@ class vkrenderer { private: - void handle_swapchain_result(VkResult function_result); + void begin_frame(); + + void finish_frame(); + - // vkapi api; + void handle_swapchain_result(VkResult function_result); std::vector renderpasses; + static constexpr uint32_t initial_copy_command_buffers = 10U; uint32_t virtual_frame_index = 0; uint32_t swapchain_image_index = 0; - std::vector command_buffers; + VkCommandBuffer graphics_command_buffers[virtual_frames_count]; + std::vector copy_command_buffers[virtual_frames_count]; - handle staging_buffer; + RingBuffer* staging_buffer; pipeline tonemapping_pipeline; @@ -84,6 +88,9 @@ class vkrenderer { VkSemaphore execution_semaphores[virtual_frames_count]; VkSemaphore acquire_semaphores[virtual_frames_count]; + VkCommandPool graphics_command_pool; + VkCommandPool copy_command_pools[virtual_frames_count]; + VkSurfaceKHR platform_surface; swapchain swapchain; std::vector swapchain_textures; @@ -91,13 +98,34 @@ class vkrenderer { const uint32_t min_swapchain_image_count = 3; }; -// TODO: Move those to there own files -class Texture { +class RingBuffer { public: + RingBuffer(size_t size) + : buffer_size(size) {} - sampler sampler; + [[nodiscard]]size_t alloc(size_t alloc_size) const { + assert(alloc_size > 0); - handle device_image; + if (ptr + alloc_size < buffer_size) { + return ptr; + } + return 0; + } + + 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; + + size_t buffer_size; + off_t ptr; }; class Sampler { @@ -106,33 +134,55 @@ class Sampler { handle device_sampler; }; -class Buffer { +// TODO: Move those to there own files +class Texture { public: - Buffer(size_t size); + void write(void* data) const { + auto image = vkrenderer::api.get_image(device_image); + auto texture_size = image.size.width * image.size.height * 4; - ~Buffer() { - vkrenderer::api.destroy_buffer(device_buffer); - } + // TODO: Do all this inside vkrenderer + // auto* buffer = vkrenderer::get_staging_buffer(); + // auto alloc_offset = buffer->alloc(texture_size); + // assert(alloc_offset > 0); - void write(void* data, off_t alloc_offset, size_t data_size) const { - auto api_buffer = vkrenderer::api.get_buffer(device_buffer); + // // std::memcpy(buffer.device_ptr, data, texture_size); + // buffer->write(data, alloc_offset, texture_size); - std::memcpy((uint8_t*)api_buffer.device_ptr + alloc_offset, data, data_size); + // vkrenderer::api.create_command_buffers + // submit upload work + // VKRESULT(vkWaitForFences(vkrenderer::context.device, 1, &submission_fences[virtual_frame_index], VK_TRUE, UINT64_MAX)) + // VKRESULT(vkResetFences(vkrenderer::context.device, 1, &submission_fences[virtual_frame_index])) + + // auto* cmd_buf = command_buffers[virtual_frame_index]; + // api.start_record(cmd_buf); + + // api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, device_image); + + // api.copy_buffer(cmd_buf, buffer, device_image); + + // api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, device_image); + + // api.end_record(cmd_buf); + + + + // VKRESULT(vkWaitForFences(vkrenderer::context.device, 1, &submission_fences[virtual_frame_index], VK_TRUE, UINT64_MAX)) } - handle device_buffer; + Sampler* sampler = nullptr; - size_t buffer_size; + handle device_image; }; -class RingBuffer { +class Buffer { public: - RingBuffer(size_t size) - : buffer_size(size) {} - [[nodiscard]]bool alloc(size_t alloc_size) const { - return ptr + alloc_size > buffer_size; + Buffer(size_t size); + + ~Buffer() { + vkrenderer::api.destroy_buffer(device_buffer); } void write(void* data, off_t alloc_offset, size_t data_size) const { @@ -141,14 +191,9 @@ class RingBuffer { std::memcpy((uint8_t*)api_buffer.device_ptr + alloc_offset, data, data_size); } - void reset() { - ptr = 0; - } - handle device_buffer; size_t buffer_size; - off_t ptr; }; #endif // !__VK_RENDERER_HPP_ -- cgit v1.3