aboutsummaryrefslogtreecommitdiffstats
path: root/src/vk-renderer.cpp
diff options
context:
space:
mode:
authorladevieq <ladevie.quentin@gmail.com>2022-09-06 22:41:00 +0200
committerladevieq <ladevie.quentin@gmail.com>2022-09-06 22:41:00 +0200
commitb0708f165601fad16aa59715dbe32796a6e7081e (patch)
tree7741e0fd5e180905618d27ac4003c74633b96a13 /src/vk-renderer.cpp
parent24b02adbf6189025c00d62417fb4b49346f22c98 (diff)
Setup for texture upload system
Diffstat (limited to 'src/vk-renderer.cpp')
-rw-r--r--src/vk-renderer.cpp148
1 files changed, 74 insertions, 74 deletions
diff --git a/src/vk-renderer.cpp b/src/vk-renderer.cpp
index 189ac33..91115ba 100644
--- a/src/vk-renderer.cpp
+++ b/src/vk-renderer.cpp
@@ -18,8 +18,8 @@
#define VKRESULT(result) result;
#endif
-vkcontext vkrenderer::context = vkcontext();
-vkapi vkrenderer::api = vkapi(vkrenderer::context);
+auto vkrenderer::context = vkcontext();
+auto vkrenderer::api = vkapi(vkrenderer::context);
vkrenderer::vkrenderer(window& wnd) {
platform_surface = api.create_surface(wnd);
@@ -40,15 +40,21 @@ vkrenderer::vkrenderer(window& wnd) {
tonemapping_pipeline = api.create_compute_pipeline("tonemapping");
- command_buffers = api.create_command_buffers(virtual_frames_count);
+ graphics_command_pool = api.create_command_pool();
+ api.allocate_command_buffers(graphics_command_pool, graphics_command_buffers, virtual_frames_count);
for(size_t index { 0 }; index < virtual_frames_count; index++) {
submission_fences[index] = api.create_fence();
execution_semaphores[index] = api.create_semaphore();
acquire_semaphores[index] = api.create_semaphore();
+
+ copy_command_pools[index] = api.create_command_pool();
+ copy_command_buffers[index].resize(initial_copy_command_buffers);
+ api.allocate_command_buffers(copy_command_pools[index], copy_command_buffers[index].data(), copy_command_buffers[index].size());
}
- staging_buffer = api.create_buffer(4096 * 4096 * 4 * sizeof(uint32_t), VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_ONLY);
+
+ // staging_buffer = api.create_buffer(4096 * 4096 * 4 * sizeof(uint32_t), VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_ONLY);
}
vkrenderer::~vkrenderer() {
@@ -58,44 +64,28 @@ vkrenderer::~vkrenderer() {
delete renderpass;
}
+ for(size_t index { 0 }; index < virtual_frames_count; index++) {
+ vkFreeCommandBuffers(
+ context.device,
+ copy_command_pools[index],
+ copy_command_buffers[index].size(),
+ copy_command_buffers[index].data()
+ );
+ }
+
api.destroy_fences(submission_fences, virtual_frames_count);
api.destroy_semaphores(execution_semaphores, virtual_frames_count);
api.destroy_semaphores(acquire_semaphores, virtual_frames_count);
- api.destroy_command_buffers(command_buffers);
+ vkDestroyCommandPool(context.device, graphics_command_pool, nullptr);
+
+ vkFreeCommandBuffers(context.device, graphics_command_pool, virtual_frames_count, graphics_command_buffers);
api.destroy_pipeline(tonemapping_pipeline);
api.destroy_swapchain(swapchain);
api.destroy_surface(platform_surface);
}
-
-void vkrenderer::begin_frame() {
- VKRESULT(vkWaitForFences(context.device, 1, &submission_fences[virtual_frame_index], VK_TRUE, UINT64_MAX))
- VKRESULT(vkResetFences(context.device, 1, &submission_fences[virtual_frame_index]))
-
- swapchain_image_index = 0;
- auto acquire_result = vkAcquireNextImageKHR(context.device, swapchain.handle, UINT64_MAX, acquire_semaphores[virtual_frame_index], VK_NULL_HANDLE, &swapchain_image_index);
- handle_swapchain_result(acquire_result);
-
- api.start_record(command_buffers[virtual_frame_index]);
-}
-
-void vkrenderer::finish_frame() {
- auto* cmd_buf = command_buffers[virtual_frame_index];
-
- // api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, swapchain.images[swapchain_image_index]);
-
- api.end_record(cmd_buf);
-
- api.submit(cmd_buf, acquire_semaphores[virtual_frame_index], execution_semaphores[virtual_frame_index], submission_fences[virtual_frame_index]);
-
- auto present_result = api.present(swapchain, swapchain_image_index, execution_semaphores[virtual_frame_index]);
- handle_swapchain_result(present_result);
-
- virtual_frame_index = ++virtual_frame_index % virtual_frames_count;
-}
-
void vkrenderer::recreate_swapchain() {
VKRESULT(vkWaitForFences(context.device, vkrenderer::virtual_frames_count, submission_fences, VK_TRUE, UINT64_MAX))
@@ -117,47 +107,12 @@ void vkrenderer::recreate_swapchain() {
}
}
-void vkrenderer::handle_swapchain_result(VkResult function_result) {
- switch(function_result) {
- case VK_ERROR_OUT_OF_DATE_KHR:
- return recreate_swapchain();
- case VK_ERROR_SURFACE_LOST_KHR:
- assert(true && "surface lost");
- default:
- return;
- }
-}
-
-void vkrenderer::update_image(Texture* texture, void* data) {
- auto image = api.get_image(texture->device_image);
- auto max_texture_size = image.size.width * image.size.height * 4;
- auto buffer = api.get_buffer(staging_buffer);
- if (buffer.size < max_texture_size) {
- api.destroy_buffer(staging_buffer);
-
- staging_buffer = api.create_buffer(max_texture_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_ONLY);
- }
-
- std::memcpy(buffer.device_ptr, data, max_texture_size);
-
- VKRESULT(vkWaitForFences(context.device, 1, &submission_fences[virtual_frame_index], VK_TRUE, UINT64_MAX))
- VKRESULT(vkResetFences(context.device, 1, &submission_fences[virtual_frame_index]))
-
- auto* cmd_buf = command_buffers[virtual_frame_index];
- api.start_record(cmd_buf);
+void upload_image(void* data, size_t size) {
+ // copy to staging buffer
- api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, texture->device_image);
-
- api.copy_buffer(cmd_buf, staging_buffer, texture->device_image);
-
- api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, texture->device_image);
-
- api.end_record(cmd_buf);
-
-
- api.submit(cmd_buf, VK_NULL_HANDLE, VK_NULL_HANDLE, submission_fences[virtual_frame_index]);
-
- VKRESULT(vkWaitForFences(context.device, 1, &submission_fences[virtual_frame_index], VK_TRUE, UINT64_MAX))
+ // allocate command buffers
+ // get command buffer
+ // copy to gpu resident texture
}
Buffer* vkrenderer::create_buffer(size_t size) {
@@ -176,8 +131,9 @@ Buffer* vkrenderer::create_index_buffer(size_t size) {
return buffer;
}
-Texture* vkrenderer::create_2d_texture(size_t width, size_t height, VkFormat format) {
+Texture* vkrenderer::create_2d_texture(size_t width, size_t height, VkFormat format, Sampler *sampler) {
auto* texture = new Texture();
+ texture->sampler = sampler;
texture->device_image = api.create_image(
{ .width = (uint32_t)width, .height = (uint32_t)height, .depth = 1 },
format,
@@ -220,7 +176,7 @@ Primitive* vkrenderer::create_primitive(PrimitiveRenderpass& primitive_render_pa
void vkrenderer::render() {
begin_frame();
- auto* cmd_buf = command_buffers[virtual_frame_index];
+ auto* cmd_buf = graphics_command_buffers[virtual_frame_index];
for (auto& renderpass: renderpasses) {
renderpass->execute(*this, cmd_buf);
@@ -242,7 +198,51 @@ void vkrenderer::render() {
finish_frame();
}
+// private functions
+void vkrenderer::begin_frame() {
+
+ VKRESULT(vkWaitForFences(context.device, 1, &submission_fences[virtual_frame_index], VK_TRUE, UINT64_MAX))
+ VKRESULT(vkResetFences(context.device, 1, &submission_fences[virtual_frame_index]))
+
+ swapchain_image_index = 0;
+ auto acquire_result = vkAcquireNextImageKHR(context.device, swapchain.handle, UINT64_MAX, acquire_semaphores[virtual_frame_index], VK_NULL_HANDLE, &swapchain_image_index);
+ handle_swapchain_result(acquire_result);
+
+ api.start_record(graphics_command_buffers[virtual_frame_index]);
+}
+
+void vkrenderer::finish_frame() {
+ auto* cmd_buf = graphics_command_buffers[virtual_frame_index];
+
+ // api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, swapchain.images[swapchain_image_index]);
+
+ api.end_record(cmd_buf);
+
+ // wait for acquire + copy semaphores
+
+ api.submit_graphics(cmd_buf, acquire_semaphores[virtual_frame_index], execution_semaphores[virtual_frame_index], submission_fences[virtual_frame_index]);
+
+ auto present_result = api.present(swapchain, swapchain_image_index, execution_semaphores[virtual_frame_index]);
+ handle_swapchain_result(present_result);
+
+ virtual_frame_index = ++virtual_frame_index % virtual_frames_count;
+}
+
+
+void vkrenderer::handle_swapchain_result(VkResult function_result) {
+ switch(function_result) {
+ case VK_ERROR_OUT_OF_DATE_KHR:
+ return recreate_swapchain();
+ case VK_ERROR_SURFACE_LOST_KHR:
+ assert(true && "surface lost");
+ default:
+ return;
+ }
+}
+
+
Buffer::Buffer(size_t size)
: buffer_size(size) {
device_buffer = vkrenderer::api.create_buffer(size, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU);
}
+