diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/vk-api.hpp | 2 | ||||
| -rw-r--r-- | include/vk-renderer.hpp | 131 |
2 files changed, 76 insertions, 57 deletions
diff --git a/include/vk-api.hpp b/include/vk-api.hpp index 6baaa73..40ccb50 100644 --- a/include/vk-api.hpp +++ b/include/vk-api.hpp @@ -115,7 +115,7 @@ class vkapi { handle create_buffer(size_t data_size, VkBufferUsageFlags buffer_usage, uint32_t mem_usage); void copy_buffer(VkCommandBuffer cmd_buf, handle src, handle dst, size_t size); - void copy_buffer(VkCommandBuffer cmd_buf, handle src, handle dst); + void copy_buffer(VkCommandBuffer cmd_buf, handle src, handle dst, off_t buffer_offset = 0); void destroy_buffer(handle buffer); diff --git a/include/vk-renderer.hpp b/include/vk-renderer.hpp index ca4c261..63dcbf7 100644 --- a/include/vk-renderer.hpp +++ b/include/vk-renderer.hpp @@ -2,6 +2,7 @@ #define __VK_RENDERER_HPP_ #include <cstdint> +#include <format> #include <vector> #include "vk-context.hpp" @@ -15,10 +16,28 @@ class Primitive; class window; class Texture; -class Buffer; class Sampler; class RingBuffer; + +class Buffer { + public: + + Buffer(size_t size, VkBufferUsageFlagBits usage); + ~Buffer(); + + void write(void* data, off_t alloc_offset, size_t data_size) const; + + void resize(size_t new_size); + + handle device_buffer; + + size_t buffer_size; + + VkBufferUsageFlagBits usage; +}; + + class vkrenderer { public: vkrenderer(window& wnd); @@ -28,10 +47,16 @@ class vkrenderer { void recreate_swapchain(); + void update_images(); + + static void queue_image_update(Texture* texture); + static Buffer* create_buffer(size_t size); static Buffer* create_index_buffer(size_t size); + static Buffer* create_staging_buffer(size_t size); + static Texture* create_2d_texture(size_t width, size_t height, VkFormat format, Sampler *sampler = nullptr); static void destroy_2d_texture(Texture* texture); @@ -47,8 +72,6 @@ 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]; } @@ -72,14 +95,13 @@ class vkrenderer { std::vector<Renderpass*> renderpasses; - static constexpr uint32_t initial_copy_command_buffers = 10U; uint32_t virtual_frame_index = 0; uint32_t swapchain_image_index = 0; VkCommandBuffer graphics_command_buffers[virtual_frames_count]; - std::vector<VkCommandBuffer> copy_command_buffers[virtual_frames_count]; + VkCommandBuffer copy_command_buffers[virtual_frames_count]; - RingBuffer* staging_buffer; + RingBuffer* staging_buffers[virtual_frames_count]; pipeline tonemapping_pipeline; @@ -96,20 +118,24 @@ class vkrenderer { std::vector<Texture*> swapchain_textures; const uint32_t min_swapchain_image_count = 3; + + static std::vector<Texture*> upload_queue; }; class RingBuffer { public: - RingBuffer(size_t size) - : buffer_size(size) {} - [[nodiscard]]size_t alloc(size_t alloc_size) const { + static const off_t invalid_alloc = -1; + + RingBuffer(size_t size); + + [[nodiscard]]off_t alloc(size_t alloc_size) const { assert(alloc_size > 0); if (ptr + alloc_size < buffer_size) { return ptr; } - return 0; + return invalid_alloc; } void write(void* data, off_t alloc_offset, size_t data_size) const { @@ -134,66 +160,59 @@ class Sampler { handle device_sampler; }; -// TODO: Move those to there own files class Texture { public: - void write(void* data) const { - auto image = vkrenderer::api.get_image(device_image); - auto texture_size = image.size.width * image.size.height * 4; - - // TODO: Do all this inside vkrenderer - // auto* buffer = vkrenderer::get_staging_buffer(); - // auto alloc_offset = buffer->alloc(texture_size); - // assert(alloc_offset > 0); - - // // std::memcpy(buffer.device_ptr, data, texture_size); - // buffer->write(data, alloc_offset, texture_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])) + Texture(handle image_handle) { + auto image = vkrenderer::api.get_image(image_handle); + width = image.size.width; + height = image.size.height; + depth = image.size.depth; - // 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)) + device_image = image_handle; } - Sampler* sampler = nullptr; + Texture(size_t width, size_t height, size_t depth, VkFormat format = VK_FORMAT_R8G8B8A8_UNORM, Sampler* texture_sampler = nullptr) + : width(width), height(height), depth(depth) { + sampler = texture_sampler; + device_image = vkrenderer::api.create_image( + { .width = (uint32_t)width, .height = (uint32_t)height, .depth = 1 }, + format, + VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT + ); + } - handle device_image; -}; + void update(void* new_data) { + if (data == nullptr) { + free(data); + } + data = new_data; -class Buffer { - public: + // vkrenderer::update_image(this); + } - Buffer(size_t size); + size_t size() const { + auto image = vkrenderer::api.get_image(device_image); + return width * height * depth * pixel_size(image.format); + } - ~Buffer() { - vkrenderer::api.destroy_buffer(device_buffer); + static size_t pixel_size(VkFormat format) { + switch (format) { + case VK_FORMAT_R8G8B8A8_UNORM: + return 4; + default: + return 0; + } } - void write(void* data, off_t alloc_offset, size_t data_size) const { - auto api_buffer = vkrenderer::api.get_buffer(device_buffer); + Sampler* sampler = nullptr; - std::memcpy((uint8_t*)api_buffer.device_ptr + alloc_offset, data, data_size); - } + void* data = nullptr; - handle device_buffer; + size_t width = 0; + size_t height = 0; + size_t depth = 0; - size_t buffer_size; + handle device_image; }; - #endif // !__VK_RENDERER_HPP_ |
