From a87ad2eac867b77a75e05ad48bb3ee622e5735d3 Mon Sep 17 00:00:00 2001 From: ladevieq Date: Sun, 5 Mar 2023 21:00:21 +0100 Subject: Change freelist implementation --- api-test/main.cpp | 63 +++++++++++++++++++------ api-test/vk-bindless.cpp | 4 ++ api-test/vk-device.cpp | 118 ++++++++++++++++++++--------------------------- 3 files changed, 101 insertions(+), 84 deletions(-) (limited to 'api-test') diff --git a/api-test/main.cpp b/api-test/main.cpp index 30668e2..0a2ab71 100644 --- a/api-test/main.cpp +++ b/api-test/main.cpp @@ -27,6 +27,9 @@ struct device_texture; RENDERDOC_API_1_4_1 *rdoc_api = nullptr; #endif +static constexpr size_t Kb = 1024U; +static constexpr size_t Mb = 1024U * Kb; + void ui() { ImGui::NewFrame(); bool open = true; @@ -77,8 +80,8 @@ int main() { } #endif - constexpr size_t window_width = 640U; - constexpr size_t window_height = 360U; + constexpr size_t window_width = 1280U; + constexpr size_t window_height = 720U; window wnd { window_width, window_height }; ImGui::CreateContext(); @@ -166,15 +169,16 @@ int main() { addr = static_cast(static_cast(staging_buffer.mapped_ptr) + ui_offset); memcpy(addr, pixels, size); - auto vertex_buffer = device.get_buffer(device.create_buffer({ - .size = 1024U * 1024U * sizeof(uint32_t), + auto vertex_buffer_handle = device.create_buffer({ + .size = Mb * sizeof(uint32_t), .usages = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, .memory_properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, .memory_usage = VMA_MEMORY_USAGE_CPU_TO_GPU, - })); + }); + auto vertex_buffer = device.get_buffer(vertex_buffer_handle); auto index_buffer_handle = device.create_buffer({ - .size = 1024U * 1024U * sizeof(uint32_t), + .size = Mb * sizeof(uint32_t), .usages = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, .memory_properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, .memory_usage = VMA_MEMORY_USAGE_CPU_TO_GPU, @@ -226,9 +230,9 @@ int main() { }; const auto& uniform_buffer = device.get_bindingmodel().get_uniform_buffer(); compute_params params { - device.get_texture(second_texture).storage_index, - device.get_texture(gpu_texture_handle).storage_index, - device.get_texture(output_texture_handle).storage_index, + device.get_texture(second_texture).get_storage_index(), + device.get_texture(gpu_texture_handle).get_storage_index(), + device.get_texture(output_texture_handle).get_storage_index(), }; memcpy(uniform_buffer.mapped_ptr, ¶ms, sizeof(params)); @@ -249,12 +253,19 @@ int main() { RD_END_CAPTURE; std::array ui_command_buffers = {command_buffers[1]}; - // std::array, 1U> color_attachment {gpu_texture_handle}; - surface.acquire_image_index(acquire_semaphore); - std::array, 1U> color_attachment {surface.get_backbuffer_image()}; + std::array, 1U> color_attachment {}; while(wnd.isOpen) { wnd.poll_events(); + for (auto& event : wnd.events) { + if (event.type == EVENT_TYPES::MOUSE_MOVE) { + io.MousePos.x = static_cast(event.x); + io.MousePos.y = static_cast(event.y); + } + + io.MouseDown[0] = event.type == EVENT_TYPES::BUTTON_PRESS; + } + ui(); auto* draw_data = ImGui::GetDrawData(); @@ -262,6 +273,9 @@ int main() { device.wait(semaphore); + surface.acquire_image_index(acquire_semaphore); + color_attachment[0] = surface.get_backbuffer_image(); + RD_START_CAPTURE; command_buffers[1].start(), @@ -299,7 +313,7 @@ int main() { -1.f - draw_data->DisplayPos.x * (2.f / draw_data->DisplaySize.x), -1.f - draw_data->DisplayPos.y * (2.f / draw_data->DisplaySize.y), }, - .texture_index = ui_texture.sampled_index, + .texture_index = ui_texture.get_sampled_index(), }; command_buffers[1].render({ @@ -327,9 +341,28 @@ int main() { device.wait(semaphore); device.present(surface_handle, {}); RD_END_CAPTURE; - - std::system("PAUSE"); } + device.wait(semaphore); + + device.destroy_surface(surface_handle); + + device.destroy_pipeline(compute); + device.destroy_pipeline(graphics); + + device.destroy_semaphore(semaphore); + device.destroy_semaphore(acquire_semaphore); + + device.destroy_texture(gpu_texture_handle); + device.destroy_texture(second_texture); + device.destroy_texture(output_texture_handle); + device.destroy_texture(ui_texture_handle); + + device.destroy_buffer(staging_buffer_handle); + device.destroy_buffer(vertex_buffer_handle); + device.destroy_buffer(index_buffer_handle); + + ImGui::DestroyContext(); + return 0; } diff --git a/api-test/vk-bindless.cpp b/api-test/vk-bindless.cpp index c48aea1..c27211f 100644 --- a/api-test/vk-bindless.cpp +++ b/api-test/vk-bindless.cpp @@ -118,6 +118,7 @@ bindless_model bindless_model::create_bindless_model() { } void bindless_model::destroy_bindless_model(bindless_model& bindless) { + auto& render_device = vkdevice::get_render_device(); auto* device = vkdevice::get_render_device().get_device(); vkDestroyDescriptorPool(device, bindless.descriptor_pool, nullptr); @@ -128,6 +129,9 @@ void bindless_model::destroy_bindless_model(bindless_model& bindless) { vkDestroyPipelineLayout(device, bindless.layout, nullptr); bindless.destroy_immutable_samplers(device); + + render_device.destroy_buffer(bindless.instances_uniform_buffer_handle); + render_device.destroy_buffer(bindless.draws_uniform_buffer_handle); } void bindless_model::create_layout(VkDevice device) { diff --git a/api-test/vk-device.cpp b/api-test/vk-device.cpp index 2742220..42f6269 100644 --- a/api-test/vk-device.cpp +++ b/api-test/vk-device.cpp @@ -44,26 +44,6 @@ vkdevice::~vkdevice() { bindless_model::destroy_bindless_model(bindless); - for (const auto& surface : surfaces) { - destroy_surface(surface); - } - - for (const auto& texture : textures) { - destroy_texture(texture); - } - - for (const auto& buffer : buffers) { - destroy_buffer(buffer); - } - - for (const auto& pipeline : pipelines) { - destroy_pipeline(pipeline); - } - - for (const auto& semaphore : semaphores) { - destroy_semaphore(semaphore); - } - vmaDestroyAllocator(gpu_allocator); for (auto& queue : queues) { @@ -80,13 +60,15 @@ vkdevice::~vkdevice() { } handle vkdevice::create_texture(const texture_desc& desc) { - device_texture device_texture { desc }; + device_texture device_texture; + device_texture.init(desc); - return { textures.add(device_texture) }; + return textures.add(device_texture); } handle vkdevice::create_buffer(const buffer_desc& desc) { device_buffer device_buffer{ + .size = desc.size, }; VkBufferCreateInfo create_info{ @@ -128,7 +110,7 @@ handle vkdevice::create_buffer(const buffer_desc& desc) { device_buffer.mapped_ptr = alloc_info.pMappedData; - return { buffers.add(device_buffer) }; + return buffers.add(device_buffer); } handle vkdevice::create_pipeline(const pipeline_desc& desc) { @@ -298,7 +280,7 @@ handle vkdevice::create_pipeline(const pipeline_desc& desc) { } } - return { pipelines.add(device_pipeline) }; + return pipelines.add(device_pipeline); } handle vkdevice::create_surface(const surface_desc& desc) { @@ -319,7 +301,7 @@ handle vkdevice::create_surface(const surface_desc& desc) { create_swapchain(desc, device_surface); - return { surfaces.add(device_surface) }; + return surfaces.add(device_surface); } handle vkdevice::create_semaphore(const semaphore_desc& desc) { @@ -341,58 +323,35 @@ handle vkdevice::create_semaphore(const semaphore_desc& desc) VKCHECK(vkCreateSemaphore(device, &create_info, nullptr, &semaphore.vk_semaphore)); - return { semaphores.add(semaphore) }; -} - -void vkdevice::destroy_texture(const device_texture& texture) { - destroy_texture({ .id = textures.find(texture) }); + return semaphores.add(semaphore); } void vkdevice::destroy_texture(handle handle) { - const auto& texture = get_texture(handle); - textures.remove(handle.id); - - vmaDestroyImage(gpu_allocator, texture.vk_image, texture.alloc); - - vkDestroyImageView(device, texture.whole_view, nullptr); - - for (const auto& mip_view : texture.mips_views) { - vkDestroyImageView(device, mip_view, nullptr); - } -} + auto& texture = get_texture(handle); -void vkdevice::destroy_buffer(const device_buffer& buffer) { - destroy_buffer({ .id = buffers.find(buffer) }); + texture.release(); + textures.remove(handle); } void vkdevice::destroy_buffer(handle handle) { const auto& buffer = get_buffer(handle); vmaDestroyBuffer(gpu_allocator, buffer.vk_buffer, buffer.alloc); -} - -void vkdevice::destroy_pipeline(const device_pipeline& pipeline) { - destroy_pipeline({ .id = pipelines.find(pipeline) }); + buffers.remove(handle); } void vkdevice::destroy_pipeline(handle handle) { const auto& pipeline = vkdevice::get_pipeline(handle); vkDestroyPipeline(device, pipeline.vk_pipeline, nullptr); -} - -void vkdevice::destroy_semaphore(const device_semaphore& semaphore) { - destroy_semaphore({ .id = semaphores.find(semaphore)}); + pipelines.remove(handle); } void vkdevice::destroy_semaphore(handle handle) { auto& semaphore = vkdevice::get_semaphore(handle); vkDestroySemaphore(device, semaphore.vk_semaphore, nullptr); -} - -void vkdevice::destroy_surface(const device_surface& surface) { - destroy_surface({ .id = surfaces.find(surface)}); + semaphores.remove(handle); } void vkdevice::destroy_surface(handle handle) { @@ -400,13 +359,7 @@ void vkdevice::destroy_surface(handle handle) { for (auto texture_index{0U}; texture_index < surface.image_count; texture_index++) { auto texture_handle = surface.swapchain_images[texture_index]; - const auto& texture = get_texture(texture_handle); - - vkDestroyImageView(device, texture.whole_view, nullptr); - - for (auto mip_index{0U}; mip_index < texture.mips; mip_index++) { - vkDestroyImageView(device, texture.mips_views[mip_index], nullptr); - } + destroy_texture(texture_handle); } vkDestroySwapchainKHR(device, surface.vk_swapchain, nullptr); @@ -425,6 +378,7 @@ void vkdevice::wait(handle semaphore_handle) { .pValues = &semaphore.value, }; VKCHECK(vkWaitSemaphores(device, &wait_info, -1)); + vkDeviceWaitIdle(device); } void vkdevice::submit(std::span buffers, handle wait_handle, handle signal_handle) { @@ -649,6 +603,7 @@ void vkdevice::create_swapchain(const surface_desc& desc, device_surface& surfac .width = extent.width, .height = extent.height, .depth = 1U, + .mips = 1U, .usages = desc.usages, .format = desc.surface_format.format, .type = VK_IMAGE_TYPE_2D, @@ -888,10 +843,10 @@ void vkdevice::create_debug_layer_callback() { } -freelist device_texture::sampled_indices = {}; -freelist device_texture::storage_indices = {}; +idlist<> device_texture::sampled_indices = {}; +idlist<> device_texture::storage_indices = {}; -device_texture::device_texture(const texture_desc& desc) { +void device_texture::init(const texture_desc& desc) { auto& render_device = vkdevice::get_render_device(); const auto& bindless = render_device.get_bindingmodel(); auto* device = render_device.get_device(); @@ -956,14 +911,14 @@ device_texture::device_texture(const texture_desc& desc) { std::vector write_descriptor_sets; if ((desc.usages & VK_IMAGE_USAGE_STORAGE_BIT) != 0) { - storage_index = storage_indices.add(this); + storage_index = storage_indices.add(); write_descriptor_sets.push_back({ .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .pNext = nullptr, .dstSet = bindless.sets[BindlessSetType::GLOBAL], .dstBinding = 2U, - .dstArrayElement = storage_index, + .dstArrayElement = storage_index.id, .descriptorCount = sizeof(images_info) / sizeof(VkDescriptorImageInfo), .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, .pImageInfo = images_info, @@ -972,14 +927,14 @@ device_texture::device_texture(const texture_desc& desc) { }); } if ((desc.usages & VK_IMAGE_USAGE_SAMPLED_BIT) != 0) { - sampled_index = sampled_indices.add(this); + sampled_index = sampled_indices.add(); write_descriptor_sets.push_back({ .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .pNext = nullptr, .dstSet = bindless.sets[BindlessSetType::GLOBAL], .dstBinding = 1U, - .dstArrayElement = sampled_index, + .dstArrayElement = sampled_index.id, .descriptorCount = sizeof(images_info) / sizeof(VkDescriptorImageInfo), .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, .pImageInfo = images_info, @@ -991,6 +946,31 @@ device_texture::device_texture(const texture_desc& desc) { vkUpdateDescriptorSets(device, write_descriptor_sets.size(), write_descriptor_sets.data(), 0U, nullptr); } +void device_texture::release() { + auto& render_device = vkdevice::get_render_device(); + auto* device = render_device.get_device(); + auto* gpu_allocator = render_device.get_allocator(); + + if (alloc != nullptr) { + vmaDestroyImage(gpu_allocator, vk_image, alloc); + } + + vkDestroyImageView(device, whole_view, nullptr); + + for (auto mip_index{ 0U }; mip_index < mips; mip_index++) { + const auto& mip_view = mips_views[mip_index]; + vkDestroyImageView(device, mip_view, nullptr); + } + + if (storage_index.is_valid()) { + storage_indices.remove(storage_index); + } + + if (sampled_index.is_valid()) { + sampled_indices.remove(sampled_index); + } +} + void device_texture::create_views() { auto* device = vkdevice::get_render_device().get_device(); -- cgit v1.3