From 54278c111c338b7c618c8ab06ed7ffb0f6e8adea Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Thu, 19 Nov 2026 04:45:58 +0200 Subject: [PATCH 01/12] render: move make_* into vulkan::factory Follow-up to ruis issue #190: the shareable-object make_* functions now live on a new vulkan::factory that wraps the shared vulkan_device (VkInstance + VkDevice + VkQueue). vulkan::context takes an injected factory and no longer creates GPU objects; it uses the factory's device for swapchain/framebuffer/draw work. The one-shot command buffer, descriptor pool, and pipeline cache/layout/dsl resources move to the factory (shared across all windows). all_textures (deferred upload tracking) moves to the factory. --- src/ruis/render/vulkan/context.cpp | 430 +--------------- src/ruis/render/vulkan/context.hpp | 86 +--- src/ruis/render/vulkan/factory.cpp | 459 ++++++++++++++++++ src/ruis/render/vulkan/factory.hpp | 193 ++++++++ src/ruis/render/vulkan/index_buffer.cpp | 10 +- src/ruis/render/vulkan/index_buffer.hpp | 8 +- .../render/vulkan/shaders/shader_color.cpp | 2 +- .../render/vulkan/shaders/shader_color.hpp | 4 +- .../vulkan/shaders/shader_color_pos_lum.cpp | 2 +- .../vulkan/shaders/shader_color_pos_lum.hpp | 4 +- .../vulkan/shaders/shader_color_pos_tex.cpp | 2 +- .../vulkan/shaders/shader_color_pos_tex.hpp | 4 +- .../shaders/shader_color_pos_tex_alpha.cpp | 2 +- .../shaders/shader_color_pos_tex_alpha.hpp | 4 +- .../render/vulkan/shaders/shader_common.cpp | 5 +- .../render/vulkan/shaders/shader_common.hpp | 2 +- .../render/vulkan/shaders/shader_pos_clr.cpp | 2 +- .../render/vulkan/shaders/shader_pos_clr.hpp | 4 +- .../render/vulkan/shaders/shader_pos_tex.cpp | 2 +- .../render/vulkan/shaders/shader_pos_tex.hpp | 4 +- src/ruis/render/vulkan/texture_2d.cpp | 18 +- src/ruis/render/vulkan/texture_2d.hpp | 8 +- src/ruis/render/vulkan/texture_depth.cpp | 6 +- src/ruis/render/vulkan/texture_depth.hpp | 4 +- src/ruis/render/vulkan/vertex_array.cpp | 2 +- src/ruis/render/vulkan/vertex_array.hpp | 2 +- src/ruis/render/vulkan/vertex_buffer.cpp | 8 +- src/ruis/render/vulkan/vertex_buffer.hpp | 4 +- 28 files changed, 737 insertions(+), 544 deletions(-) create mode 100644 src/ruis/render/vulkan/factory.cpp create mode 100644 src/ruis/render/vulkan/factory.hpp diff --git a/src/ruis/render/vulkan/context.cpp b/src/ruis/render/vulkan/context.cpp index 333896b..ca5d3eb 100644 --- a/src/ruis/render/vulkan/context.cpp +++ b/src/ruis/render/vulkan/context.cpp @@ -105,10 +105,12 @@ VkPresentModeKHR pick_present_mode(VkPhysicalDevice phys, VkSurfaceKHR surface, context::context( const utki::shared_ref& native_window, // - const utki::shared_ref& surface_provider + const utki::shared_ref& surface_provider, // + const utki::shared_ref& factory ) : ruis::render::context( native_window, + factory, [] { r4::matrix4 m{}; // maps the framework's normalized viewport coordinates (x right, y down, @@ -125,6 +127,7 @@ context::context( return parameters{m}; }() ), + vu_factory(factory), // physical_device(surface_provider.get().get_physical_device()), // device(surface_provider.get().get_device()), // surface_provider_ref(surface_provider), // @@ -155,32 +158,9 @@ context::context( }; VU_CHECK(vkAllocateCommandBuffers(this->device, &alloc_info, &this->command_buffer)); } - { - const auto pool_info = VkCommandPoolCreateInfo{ - .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, - .flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, - .queueFamilyIndex = 0 - }; - { - VU_CHECK(vkCreateCommandPool(this->device, &pool_info, nullptr, &this->one_shot_pool)); - } - - const auto alloc_info = VkCommandBufferAllocateInfo{ - .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, - .commandPool = this->one_shot_pool, - .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, - .commandBufferCount = 1 - }; - VU_CHECK(vkAllocateCommandBuffers(this->device, &alloc_info, &this->one_shot_command_buffer)); - } { const auto fence_info = VkFenceCreateInfo{.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO}; - { - VU_CHECK(vkCreateFence(this->device, &fence_info, nullptr, &this->frame_fence)); - } - { - VU_CHECK(vkCreateFence(this->device, &fence_info, nullptr, &this->one_shot_fence)); - } + VU_CHECK(vkCreateFence(this->device, &fence_info, nullptr, &this->frame_fence)); } { const auto sem_info = VkSemaphoreCreateInfo{.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO}; @@ -208,64 +188,6 @@ context::context( } } - // ---- descriptor set layouts + pipeline layouts ---- - { - // the UBO is read from both the vertex shader (matrix) and the fragment - // shader (color), so both stages must be in the stage flags - const auto ubo_binding = VkDescriptorSetLayoutBinding{ - .binding = 0, - .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, - .descriptorCount = 1, - .stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT - }; - { - const auto dsl_info = VkDescriptorSetLayoutCreateInfo{ - .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, - .bindingCount = 1, - .pBindings = &ubo_binding - }; - VU_CHECK(vkCreateDescriptorSetLayout(this->device, &dsl_info, nullptr, &this->dsl_no_tex)); - } - - const auto with_tex_bindings = std::array{ - {ubo_binding, - {.binding = 1, - .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, - .descriptorCount = 1, - .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT}} - }; - { - const auto dsl_info = VkDescriptorSetLayoutCreateInfo{ - .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, - .bindingCount = 2, - .pBindings = with_tex_bindings.data() - }; - VU_CHECK(vkCreateDescriptorSetLayout(this->device, &dsl_info, nullptr, &this->dsl_with_tex)); - } - } - { - { - const auto pl_info = VkPipelineLayoutCreateInfo{ - .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, - .setLayoutCount = 1, - .pSetLayouts = &this->dsl_no_tex - }; - VU_CHECK(vkCreatePipelineLayout(this->device, &pl_info, nullptr, &this->pipeline_layout_no_tex)); - } - { - const auto pl_info = VkPipelineLayoutCreateInfo{ - .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, - .setLayoutCount = 1, - .pSetLayouts = &this->dsl_with_tex - }; - VU_CHECK(vkCreatePipelineLayout(this->device, &pl_info, nullptr, &this->pipeline_layout_with_tex)); - } - } - { - const auto pc_info = VkPipelineCacheCreateInfo{.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO}; - VU_CHECK(vkCreatePipelineCache(this->device, &pc_info, nullptr, &this->pipeline_cache)); - } - // ---- per-draw uniform buffer ring ---- // the ring starts empty; slots are allocated on demand in allocate_ubo_slot() @@ -643,9 +565,6 @@ context::~context() if (this->frame_fence != VK_NULL_HANDLE) { vkDestroyFence(this->device, this->frame_fence, nullptr); } - if (this->one_shot_fence != VK_NULL_HANDLE) { - vkDestroyFence(this->device, this->one_shot_fence, nullptr); - } if (this->image_available_sem != VK_NULL_HANDLE) { vkDestroySemaphore(this->device, this->image_available_sem, nullptr); } @@ -658,33 +577,11 @@ context::~context() if (this->command_pool != VK_NULL_HANDLE) { vkDestroyCommandPool(this->device, this->command_pool, nullptr); } - if (this->one_shot_command_buffer != VK_NULL_HANDLE) { - vkFreeCommandBuffers(this->device, this->one_shot_pool, 1, &this->one_shot_command_buffer); - } - if (this->one_shot_pool != VK_NULL_HANDLE) { - vkDestroyCommandPool(this->device, this->one_shot_pool, nullptr); - } - // destroy pipelines for (auto& [key, pipeline] : this->pipelines) { vkDestroyPipeline(this->device, pipeline, nullptr); } - if (this->pipeline_cache != VK_NULL_HANDLE) { - vkDestroyPipelineCache(this->device, this->pipeline_cache, nullptr); - } - if (this->pipeline_layout_no_tex != VK_NULL_HANDLE) { - vkDestroyPipelineLayout(this->device, this->pipeline_layout_no_tex, nullptr); - } - if (this->pipeline_layout_with_tex != VK_NULL_HANDLE) { - vkDestroyPipelineLayout(this->device, this->pipeline_layout_with_tex, nullptr); - } - if (this->dsl_no_tex != VK_NULL_HANDLE) { - vkDestroyDescriptorSetLayout(this->device, this->dsl_no_tex, nullptr); - } - if (this->dsl_with_tex != VK_NULL_HANDLE) { - vkDestroyDescriptorSetLayout(this->device, this->dsl_with_tex, nullptr); - } if (this->descriptor_pool != VK_NULL_HANDLE) { vkDestroyDescriptorPool(this->device, this->descriptor_pool, nullptr); } @@ -752,179 +649,6 @@ context::~context() this->swapchain_images.clear(); } -// =============================== -// = factory functions = - -utki::shared_ref context::make_shaders() const -{ - auto ret = utki::make_shared(); - // NOLINTNEXTLINE(bugprone-unused-return-value, "false positive") - ret.get().pos_tex = std::make_unique(this->get_shared_ref()); - // NOLINTNEXTLINE(bugprone-unused-return-value, "false positive") - ret.get().color_pos = std::make_unique(this->get_shared_ref()); - // NOLINTNEXTLINE(bugprone-unused-return-value, "false positive") - ret.get().pos_clr = std::make_unique(this->get_shared_ref()); - // NOLINTNEXTLINE(bugprone-unused-return-value, "false positive") - ret.get().color_pos_tex = std::make_unique(this->get_shared_ref()); - // NOLINTNEXTLINE(bugprone-unused-return-value, "false positive") - ret.get().color_pos_tex_alpha = std::make_unique(this->get_shared_ref()); - // NOLINTNEXTLINE(bugprone-unused-return-value, "false positive") - ret.get().color_pos_lum = std::make_unique(this->get_shared_ref()); - return ret; -} - -utki::shared_ref context::make_texture_2d( - rasterimage::format format, - rasterimage::dimensioned::dimensions_type dims, - texture_2d_parameters params -) const -{ - return utki::make_shared(this->get_shared_ref(), format, dims, nullptr, 0, params); -} - -utki::shared_ref context::make_texture_2d( - const rasterimage::image_variant& imvar, - texture_2d_parameters params -) const -{ - // Not supported (need rvalue reference to move the data) - throw std::logic_error("vulkan make_texture_2d: const image_variant not supported"); -} - -utki::shared_ref context::make_texture_2d( - rasterimage::image_variant&& imvar, // - texture_2d_parameters params -) const -{ - auto iv = std::move(imvar); - return std::visit( - [this, &imvar = iv, ¶ms](auto&& im) -> utki::shared_ref { - if constexpr (sizeof(im.pixels().front().front()) != 1) { - throw std::logic_error("vulkan make_texture_2d: non-8bit images are not supported"); - } else { - // NOTE: do NOT flip_vertical() here. Unlike OpenGL (texture origin at - // bottom-left), Vulkan's image row 0 is the top of the texture, so the - // top-to-bottom image data must be uploaded as-is. - auto data = im.pixels(); - return utki::make_shared( - this->get_shared_ref(), // - imvar.get_format(), - im.dims(), - data.front().data(), - data.size_bytes(), - std::move(params) - ); - } - }, - iv.variant - ); -} - -utki::shared_ref context::make_texture_depth(rasterimage::dimensioned::dimensions_type dims -) const -{ - return utki::make_shared(this->get_shared_ref(), dims); -} - -utki::shared_ref context::make_texture_cube( - // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved, "unimplemented stub: signature must match the base class") - rasterimage::image_variant&& positive_x, // - // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved, "unimplemented stub: signature must match the base class") - rasterimage::image_variant&& negative_x, // - // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved, "unimplemented stub: signature must match the base class") - rasterimage::image_variant&& positive_y, // - // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved, "unimplemented stub: signature must match the base class") - rasterimage::image_variant&& negative_y, // - // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved, "unimplemented stub: signature must match the base class") - rasterimage::image_variant&& positive_z, // - // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved, "unimplemented stub: signature must match the base class") - rasterimage::image_variant&& negative_z // -) const -{ - (void)positive_x; - (void)negative_x; - (void)positive_y; - (void)negative_y; - (void)positive_z; - (void)negative_z; - throw std::runtime_error("vulkan make_texture_cube: not implemented"); -} - -utki::shared_ref context::make_vertex_buffer(utki::span> vertices -) const -{ - std::vector padded; - padded.reserve(vertices.size() * 4); - for (const auto& v : vertices) { - padded.push_back(v.x()); - padded.push_back(v.y()); - padded.push_back(v.z()); - padded.push_back(v.w()); - } - return utki::make_shared(this->get_shared_ref(), std::move(padded)); -} - -utki::shared_ref context::make_vertex_buffer(utki::span> vertices -) const -{ - std::vector padded; - padded.reserve(vertices.size() * 4); - for (const auto& v : vertices) { - padded.push_back(v.x()); - padded.push_back(v.y()); - padded.push_back(v.z()); - padded.push_back(1.0f); - } - return utki::make_shared(this->get_shared_ref(), std::move(padded)); -} - -utki::shared_ref context::make_vertex_buffer(utki::span> vertices -) const -{ - std::vector padded; - padded.reserve(vertices.size() * 4); - for (const auto& v : vertices) { - padded.push_back(v.x()); - padded.push_back(v.y()); - padded.push_back(0.0f); - padded.push_back(1.0f); - } - return utki::make_shared(this->get_shared_ref(), std::move(padded)); -} - -utki::shared_ref context::make_vertex_buffer(utki::span vertices) const -{ - // a flat list of floats: interpreted as 4-component vertices - const auto count = vertices.size() / 4; - std::vector padded(vertices.begin(), vertices.end()); - (void)count; - return utki::make_shared(this->get_shared_ref(), std::move(padded)); -} - -utki::shared_ref context::make_index_buffer(utki::span indices) const -{ - return utki::make_shared(this->get_shared_ref(), indices); -} - -utki::shared_ref context::make_index_buffer(utki::span indices) const -{ - return utki::make_shared(this->get_shared_ref(), indices); -} - -utki::shared_ref context::make_vertex_array( - std::vector> buffers, // - utki::shared_ref indices, - ruis::render::vertex_array::mode rendering_mode -) const -{ - return utki::make_shared( - this->get_shared_ref(), - std::move(buffers), - std::move(indices), - rendering_mode - ); -} - utki::shared_ref context::make_framebuffer( // std::shared_ptr color, std::shared_ptr depth, @@ -1173,7 +897,7 @@ VkDescriptorSet context::allocate_descriptor_set( VkSampler sampler ) { - const auto dsl = textured ? this->dsl_with_tex : this->dsl_no_tex; + const auto dsl = textured ? this->vu_factory.get().get_dsl_with_tex() : this->vu_factory.get().get_dsl_no_tex(); VkDescriptorSet set = VK_NULL_HANDLE; const auto alloc_info = VkDescriptorSetAllocateInfo{ @@ -1307,134 +1031,6 @@ void context::draw_indexed( vkCmdDrawIndexed(this->command_buffer, uint32_t(vk_indices.elements_count), 1, 0, 0, 0); } -void context::one_shot(std::function cmd) -{ - // wait for all previously submitted work on the queue to complete. vkQueueWaitIdle is - // used instead of vkWaitForFences because the AMD driver does not respect the - // timeout parameter of vkWaitForFences, which deadlocks when waiting for a fence - // that was never submitted (e.g. the first call). - vkQueueWaitIdle(this->queue); - - VU_CHECK(vkResetCommandBuffer(this->one_shot_command_buffer, 0)); - const auto begin_info = VkCommandBufferBeginInfo{ - .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, - .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT - }; - VU_CHECK(vkBeginCommandBuffer(this->one_shot_command_buffer, &begin_info)); - - cmd(this->one_shot_command_buffer); - - VU_CHECK(vkEndCommandBuffer(this->one_shot_command_buffer)); - - const auto submit_info = VkSubmitInfo{ - .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, - .waitSemaphoreCount = 0, - .pWaitSemaphores = nullptr, - .commandBufferCount = 1, - .pCommandBuffers = &this->one_shot_command_buffer, - .signalSemaphoreCount = 0, - .pSignalSemaphores = nullptr - }; - // VK_NULL_HANDLE instead of nullptr: on 32-bit targets the Vulkan headers - // define non-dispatchable handles (VkFence, VkSemaphore, ...) as 64-bit - // integers, not pointers - VU_CHECK(vkQueueSubmit(this->queue, 1, &submit_info, VK_NULL_HANDLE)); - - // wait for the command buffer to complete, so the caller can safely use the - // uploaded resources right away (e.g. sample the texture in the current frame) - vkQueueWaitIdle(this->queue); -} - -void context::ensure_textures_uploaded() -{ - // find textures that need uploading (no global flag: new textures can be created at any time) - std::vector pending; - for (auto* tex : this->all_textures) { - if (!tex->uploaded && !tex->deferred_data.empty()) { - pending.push_back(tex); - } - } - - if (pending.empty()) { - // mark empty textures as uploaded - for (auto* tex : this->all_textures) { - if (!tex->uploaded && tex->deferred_data.empty()) { - tex->uploaded = true; - } - } - return; - } - - // calculate total staging size - VkDeviceSize total_size = 0; - for (auto* tex : pending) { - total_size += tex->staging_size(); - } - - if (total_size == 0) { - return; - } - - // create a single staging buffer for all textures - VkBuffer staging_buffer = VK_NULL_HANDLE; - VkDeviceMemory staging_memory = VK_NULL_HANDLE; - { - const auto buffer_info = VkBufferCreateInfo{ - .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, - .size = total_size, - .usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT, - .sharingMode = VK_SHARING_MODE_EXCLUSIVE - }; - VU_CHECK(vkCreateBuffer(this->device, &buffer_info, nullptr, &staging_buffer)); - - VkMemoryRequirements mem_reqs{}; - vkGetBufferMemoryRequirements(this->device, staging_buffer, &mem_reqs); - - VkPhysicalDeviceMemoryProperties props{}; - vkGetPhysicalDeviceMemoryProperties(this->physical_device, &props); - - const auto alloc_info = VkMemoryAllocateInfo{ - .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, - .allocationSize = mem_reqs.size, - .memoryTypeIndex = pick_host_visible_memory_type(this->physical_device, mem_reqs) - }; - VU_CHECK(vkAllocateMemory(this->device, &alloc_info, nullptr, &staging_memory)); - VU_CHECK(vkBindBufferMemory(this->device, staging_buffer, staging_memory, 0)); - } - - // copy all texture data into the staging buffer - void* mapped = nullptr; - VU_CHECK(vkMapMemory(this->device, staging_memory, 0, total_size, 0, &mapped)); - const auto mapped_span = utki::span(static_cast(mapped), total_size); - VkDeviceSize offset = 0; - for (auto* tex : pending) { - const auto sz = tex->staging_size(); - std::memcpy(mapped_span.subspan(offset, sz).data(), tex->deferred_data.data(), sz); - offset += sz; - } - vkUnmapMemory(this->device, staging_memory); - - // record all upload commands into a single one-shot command buffer - VkDeviceSize cmd_offset = 0; - this->one_shot([&](VkCommandBuffer cmd) { - for (auto* tex : pending) { - const auto sz = tex->staging_size(); - tex->record_upload_commands(cmd, staging_buffer, cmd_offset); - cmd_offset += sz; - } - }); - - // clean up - vkFreeMemory(this->device, staging_memory, nullptr); - vkDestroyBuffer(this->device, staging_buffer, nullptr); - - // mark uploaded textures - for (auto* tex : pending) { - tex->deferred_data.clear(); - tex->uploaded = true; - } -} - rasterimage::image_variant context::readback_framebuffer_color(const ruis::render::frame_buffer& fb) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "fb is always created by this renderer") @@ -1784,7 +1380,8 @@ VkPipeline context::build_pipeline( }; // ---- layout ---- - const auto layout = key.textured ? this->pipeline_layout_with_tex : this->pipeline_layout_no_tex; + const auto layout = key.textured ? this->vu_factory.get().get_pipeline_layout_with_tex() + : this->vu_factory.get().get_pipeline_layout_no_tex(); // ---- multisample state ---- const auto multisample = VkPipelineMultisampleStateCreateInfo{ @@ -1812,7 +1409,14 @@ VkPipeline context::build_pipeline( }; VkPipeline pipeline = VK_NULL_HANDLE; - VU_CHECK(vkCreateGraphicsPipelines(this->device, this->pipeline_cache, 1, &info, nullptr, &pipeline)); + VU_CHECK(vkCreateGraphicsPipelines( + this->device, + this->vu_factory.get().get_pipeline_cache(), + 1, + &info, + nullptr, + &pipeline + )); // destroy the shader modules (they are now baked into the pipeline) vkDestroyShaderModule(this->device, vertex_stage.module, nullptr); @@ -1831,7 +1435,7 @@ void context::begin_render_pass_internal(bool has_depth) has_depth = true; } // ensure all textures are uploaded before the first render pass - this->ensure_textures_uploaded(); + this->vu_factory.get().ensure_textures_uploaded(); // determine the render pass (clear or load) const bool clear = this->pending_clear; diff --git a/src/ruis/render/vulkan/context.hpp b/src/ruis/render/vulkan/context.hpp index ae8ca27..b4c552d 100644 --- a/src/ruis/render/vulkan/context.hpp +++ b/src/ruis/render/vulkan/context.hpp @@ -31,6 +31,7 @@ along with this program. If not, see . #include "shaders/shader_common.hpp" +#include "factory.hpp" #include "surface_provider.hpp" #include "vulkan.hpp" @@ -88,7 +89,8 @@ class context : public ruis::render::context public: context( const utki::shared_ref& native_window, // - const utki::shared_ref& surface_provider + const utki::shared_ref& surface_provider, // + const utki::shared_ref& factory ); ~context() override; @@ -100,54 +102,9 @@ class context : public ruis::render::context // =============================== // ====== factory functions ====== - - utki::shared_ref make_shaders() const override; - - utki::shared_ref make_texture_2d( - rasterimage::format format, - rasterimage::dimensioned::dimensions_type dims, - texture_2d_parameters params - ) const override; - - utki::shared_ref make_texture_2d( - const rasterimage::image_variant& imvar, - texture_2d_parameters params - ) const override; - - utki::shared_ref make_texture_2d( - rasterimage::image_variant&& imvar, - texture_2d_parameters params - ) const override; - - utki::shared_ref make_texture_depth( // - rasterimage::dimensioned::dimensions_type dims - ) const override; - - utki::shared_ref make_texture_cube( - rasterimage::image_variant&& positive_x, - rasterimage::image_variant&& negative_x, - rasterimage::image_variant&& positive_y, - rasterimage::image_variant&& negative_y, - rasterimage::image_variant&& positive_z, - rasterimage::image_variant&& negative_z - ) const override; - - utki::shared_ref make_vertex_buffer(utki::span> vertices - ) const override; - utki::shared_ref make_vertex_buffer(utki::span> vertices - ) const override; - utki::shared_ref make_vertex_buffer(utki::span> vertices - ) const override; - utki::shared_ref make_vertex_buffer(utki::span vertices) const override; - - utki::shared_ref make_index_buffer(utki::span indices) const override; - utki::shared_ref make_index_buffer(utki::span indices) const override; - - utki::shared_ref make_vertex_array( - std::vector> buffers, // - utki::shared_ref indices, - ruis::render::vertex_array::mode rendering_mode - ) const override; + // NOTE: the shareable render objects (textures, buffers, vertex arrays, shaders) + // are created by the injected render factory (see factory()). The only object a + // context creates is the (per-window) frame buffer. utki::shared_ref make_framebuffer( // std::shared_ptr color, @@ -227,24 +184,20 @@ class context : public ruis::render::context VkDescriptorSet descriptor_set ); - // run a one-shot command buffer (used for texture uploads); blocks until complete - void one_shot(std::function cmd); - // read back the color attachment of the given frame buffer to CPU RAM (screenshot) rasterimage::image_variant readback_framebuffer_color(const ruis::render::frame_buffer& fb); - // number of components in a vertex buffer (all vertex buffers are stored as vec4) - static constexpr uint32_t vertex_component_count = 4; - public: // used by frame_buffer to read back its color pixels friend class frame_buffer; - // ---- vulkan core (public: used by derived GPU resource classes) ---- + // the render factory (shared across all windows): provides the device, the + // one-shot upload path, and the shared pipeline / descriptor set layouts + const utki::shared_ref vu_factory; + + // ---- vulkan core (used internally by this per-window context) ---- VkPhysicalDevice physical_device = VK_NULL_HANDLE; VkDevice device = VK_NULL_HANDLE; - VkPipelineLayout pipeline_layout_no_tex = VK_NULL_HANDLE; - VkPipelineLayout pipeline_layout_with_tex = VK_NULL_HANDLE; private: // keep the surface provider alive for the lifetime of the context @@ -288,17 +241,6 @@ class context : public ruis::render::context VkSemaphore render_finished_sem = VK_NULL_HANDLE; VkDescriptorPool descriptor_pool = VK_NULL_HANDLE; - // ---- one-shot command submission (texture uploads) ---- - VkCommandPool one_shot_pool = VK_NULL_HANDLE; - VkCommandBuffer one_shot_command_buffer = VK_NULL_HANDLE; - VkFence one_shot_fence = VK_NULL_HANDLE; - - // ---- shared descriptor / pipeline layout ---- - VkDescriptorSetLayout dsl_no_tex = VK_NULL_HANDLE; // binding 0: UBO - VkDescriptorSetLayout dsl_with_tex = VK_NULL_HANDLE; // binding 0: UBO, binding 1: sampled image - - VkPipelineCache pipeline_cache = VK_NULL_HANDLE; - // ---- per-draw uniform buffer ring ---- // each draw in a frame needs its own UBO, because the GPU reads the UBO contents at // execution time (after the whole frame has been recorded), so a single shared UBO @@ -308,12 +250,6 @@ class context : public ruis::render::context std::vector ubo_ring_map; // kept mapped for fast per-draw writes size_t ubo_ring_index = 0; // reset at the start of each frame -public: - // ---- texture tracking for deferred upload (public: used by texture_2d) ---- - std::vector all_textures; - bool textures_uploaded = false; - void ensure_textures_uploaded(); - // DEBUG: 0 = screen (swapchain), 1 = offscreen framebuffer private: diff --git a/src/ruis/render/vulkan/factory.cpp b/src/ruis/render/vulkan/factory.cpp new file mode 100644 index 0000000..f84933b --- /dev/null +++ b/src/ruis/render/vulkan/factory.cpp @@ -0,0 +1,459 @@ +/* +ruis-render-vulkan - Vulkan renderer + +Copyright (C) 2012-2026 Ivan Gagis + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +/* ================ LICENSE END ================ */ + +#include "factory.hpp" + +#include + +#include "shaders/shader_color.hpp" +#include "shaders/shader_color_pos_lum.hpp" +#include "shaders/shader_color_pos_tex.hpp" +#include "shaders/shader_color_pos_tex_alpha.hpp" +#include "shaders/shader_pos_clr.hpp" +#include "shaders/shader_pos_tex.hpp" + +#include "index_buffer.hpp" +#include "texture_2d.hpp" +#include "texture_depth.hpp" +#include "vertex_array.hpp" +#include "vertex_buffer.hpp" + +using namespace ruis::render::vulkan; + +factory::factory( + const utki::shared_ref& native_window, // + const utki::shared_ref& surface_provider +) : + device(surface_provider.get().get_device()), // + physical_device(surface_provider.get().get_physical_device()), // + queue(surface_provider.get().get_queue()), // + instance(surface_provider.get().get_instance()) +{ + (void)native_window; + + // ---- one-shot command submission resources (shared: used for texture uploads) ---- + { + const auto pool_info = VkCommandPoolCreateInfo{ + .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, + .flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, + .queueFamilyIndex = 0 + }; + { + VU_CHECK(vkCreateCommandPool(this->device, &pool_info, nullptr, &this->one_shot_pool)); + } + + const auto alloc_info = VkCommandBufferAllocateInfo{ + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, + .commandPool = this->one_shot_pool, + .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, + .commandBufferCount = 1 + }; + VU_CHECK(vkAllocateCommandBuffers(this->device, &alloc_info, &this->one_shot_command_buffer)); + } + { + const auto fence_info = VkFenceCreateInfo{.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO}; + VU_CHECK(vkCreateFence(this->device, &fence_info, nullptr, &this->one_shot_fence)); + } + + // ---- descriptor set layouts + pipeline layouts (shared) ---- + { + // the UBO is read from both the vertex shader (matrix) and the fragment + // shader (color), so both stages must be in the stage flags + const auto ubo_binding = VkDescriptorSetLayoutBinding{ + .binding = 0, + .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .descriptorCount = 1, + .stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT + }; + { + const auto dsl_info = VkDescriptorSetLayoutCreateInfo{ + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, + .bindingCount = 1, + .pBindings = &ubo_binding + }; + VU_CHECK(vkCreateDescriptorSetLayout(this->device, &dsl_info, nullptr, &this->dsl_no_tex)); + } + + const auto with_tex_bindings = std::array{ + {ubo_binding, + {.binding = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, + .descriptorCount = 1, + .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT}} + }; + { + const auto dsl_info = VkDescriptorSetLayoutCreateInfo{ + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, + .bindingCount = 2, + .pBindings = with_tex_bindings.data() + }; + VU_CHECK(vkCreateDescriptorSetLayout(this->device, &dsl_info, nullptr, &this->dsl_with_tex)); + } + } + { + { + const auto pl_info = VkPipelineLayoutCreateInfo{ + .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, + .setLayoutCount = 1, + .pSetLayouts = &this->dsl_no_tex + }; + VU_CHECK(vkCreatePipelineLayout(this->device, &pl_info, nullptr, &this->pipeline_layout_no_tex)); + } + { + const auto pl_info = VkPipelineLayoutCreateInfo{ + .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, + .setLayoutCount = 1, + .pSetLayouts = &this->dsl_with_tex + }; + VU_CHECK(vkCreatePipelineLayout(this->device, &pl_info, nullptr, &this->pipeline_layout_with_tex)); + } + } + { + const auto pc_info = VkPipelineCacheCreateInfo{.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO}; + VU_CHECK(vkCreatePipelineCache(this->device, &pc_info, nullptr, &this->pipeline_cache)); + } +} + +factory::~factory() +{ + // ---- shared descriptor / pipeline resources ---- + if (this->pipeline_cache != VK_NULL_HANDLE) { + vkDestroyPipelineCache(this->device, this->pipeline_cache, nullptr); + } + if (this->pipeline_layout_no_tex != VK_NULL_HANDLE) { + vkDestroyPipelineLayout(this->device, this->pipeline_layout_no_tex, nullptr); + } + if (this->pipeline_layout_with_tex != VK_NULL_HANDLE) { + vkDestroyPipelineLayout(this->device, this->pipeline_layout_with_tex, nullptr); + } + if (this->dsl_no_tex != VK_NULL_HANDLE) { + vkDestroyDescriptorSetLayout(this->device, this->dsl_no_tex, nullptr); + } + if (this->dsl_with_tex != VK_NULL_HANDLE) { + vkDestroyDescriptorSetLayout(this->device, this->dsl_with_tex, nullptr); + } + + // ---- one-shot command submission resources ---- + if (this->one_shot_fence != VK_NULL_HANDLE) { + vkDestroyFence(this->device, this->one_shot_fence, nullptr); + } + if (this->one_shot_command_buffer != VK_NULL_HANDLE) { + vkFreeCommandBuffers(this->device, this->one_shot_pool, 1, &this->one_shot_command_buffer); + } + if (this->one_shot_pool != VK_NULL_HANDLE) { + vkDestroyCommandPool(this->device, this->one_shot_pool, nullptr); + } +} + +utki::shared_ref factory::make_shaders() const +{ + auto ret = utki::make_shared(); + // NOLINTNEXTLINE(bugprone-unused-return-value, "false positive") + ret.get().pos_tex = std::make_unique(this->get_shared_ref()); + // NOLINTNEXTLINE(bugprone-unused-return-value, "false positive") + ret.get().color_pos = std::make_unique(this->get_shared_ref()); + // NOLINTNEXTLINE(bugprone-unused-return-value, "false positive") + ret.get().pos_clr = std::make_unique(this->get_shared_ref()); + // NOLINTNEXTLINE(bugprone-unused-return-value, "false positive") + ret.get().color_pos_tex = std::make_unique(this->get_shared_ref()); + // NOLINTNEXTLINE(bugprone-unused-return-value, "false positive") + ret.get().color_pos_tex_alpha = std::make_unique(this->get_shared_ref()); + // NOLINTNEXTLINE(bugprone-unused-return-value, "false positive") + ret.get().color_pos_lum = std::make_unique(this->get_shared_ref()); + return ret; +} + +utki::shared_ref factory::make_texture_2d( + rasterimage::format format, + rasterimage::dimensioned::dimensions_type dims, + texture_2d_parameters params +) const +{ + return utki::make_shared(this->get_shared_ref(), format, dims, nullptr, 0, params); +} + +utki::shared_ref factory::make_texture_2d( + const rasterimage::image_variant& imvar, + texture_2d_parameters params +) const +{ + (void)imvar; + (void)params; + // Not supported (need rvalue reference to move the data) + throw std::logic_error("vulkan make_texture_2d: const image_variant not supported"); +} + +utki::shared_ref factory::make_texture_2d( + rasterimage::image_variant&& imvar, // + texture_2d_parameters params +) const +{ + auto iv = std::move(imvar); + return std::visit( + [this, &imvar = iv, ¶ms](auto&& im) -> utki::shared_ref { + if constexpr (sizeof(im.pixels().front().front()) != 1) { + throw std::logic_error("vulkan make_texture_2d: non-8bit images are not supported"); + } else { + // NOTE: do NOT flip_vertical() here. Unlike OpenGL (texture origin at + // bottom-left), Vulkan's image row 0 is the top of the texture, so the + // top-to-bottom image data must be uploaded as-is. + auto data = im.pixels(); + return utki::make_shared( + this->get_shared_ref(), // + imvar.get_format(), + im.dims(), + data.front().data(), + data.size_bytes(), + std::move(params) + ); + } + }, + iv.variant + ); +} + +utki::shared_ref factory::make_texture_depth(rasterimage::dimensioned::dimensions_type dims +) const +{ + return utki::make_shared(this->get_shared_ref(), dims); +} + +utki::shared_ref factory::make_texture_cube( + // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved, "unimplemented stub: signature must match the base class") + rasterimage::image_variant&& positive_x, // + // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved, "unimplemented stub: signature must match the base class") + rasterimage::image_variant&& negative_x, // + // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved, "unimplemented stub: signature must match the base class") + rasterimage::image_variant&& positive_y, // + // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved, "unimplemented stub: signature must match the base class") + rasterimage::image_variant&& negative_y, // + // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved, "unimplemented stub: signature must match the base class") + rasterimage::image_variant&& positive_z, // + // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved, "unimplemented stub: signature must match the base class") + rasterimage::image_variant&& negative_z // +) const +{ + (void)positive_x; + (void)negative_x; + (void)positive_y; + (void)negative_y; + (void)positive_z; + (void)negative_z; + throw std::runtime_error("vulkan make_texture_cube: not implemented"); +} + +utki::shared_ref factory::make_vertex_buffer(utki::span> vertices +) const +{ + std::vector padded; + padded.reserve(vertices.size() * 4); + for (const auto& v : vertices) { + padded.push_back(v.x()); + padded.push_back(v.y()); + padded.push_back(v.z()); + padded.push_back(v.w()); + } + return utki::make_shared(this->get_shared_ref(), std::move(padded)); +} + +utki::shared_ref factory::make_vertex_buffer(utki::span> vertices +) const +{ + std::vector padded; + padded.reserve(vertices.size() * 4); + for (const auto& v : vertices) { + padded.push_back(v.x()); + padded.push_back(v.y()); + padded.push_back(v.z()); + padded.push_back(1.0f); + } + return utki::make_shared(this->get_shared_ref(), std::move(padded)); +} + +utki::shared_ref factory::make_vertex_buffer(utki::span> vertices +) const +{ + std::vector padded; + padded.reserve(vertices.size() * 4); + for (const auto& v : vertices) { + padded.push_back(v.x()); + padded.push_back(v.y()); + padded.push_back(0.0f); + padded.push_back(1.0f); + } + return utki::make_shared(this->get_shared_ref(), std::move(padded)); +} + +utki::shared_ref factory::make_vertex_buffer(utki::span vertices) const +{ + // a flat list of floats: interpreted as 4-component vertices + std::vector padded(vertices.begin(), vertices.end()); + return utki::make_shared(this->get_shared_ref(), std::move(padded)); +} + +utki::shared_ref factory::make_index_buffer(utki::span indices) const +{ + return utki::make_shared(this->get_shared_ref(), indices); +} + +utki::shared_ref factory::make_index_buffer(utki::span indices) const +{ + return utki::make_shared(this->get_shared_ref(), indices); +} + +utki::shared_ref factory::make_vertex_array( + std::vector> buffers, // + utki::shared_ref indices, + ruis::render::vertex_array::mode rendering_mode +) const +{ + return utki::make_shared( + this->get_shared_ref(), + std::move(buffers), + std::move(indices), + rendering_mode + ); +} + +void factory::one_shot(std::function cmd) const +{ + // wait for all previously submitted work on the queue to complete. vkQueueWaitIdle is + // used instead of vkWaitForFences because the AMD driver does not respect the + // timeout parameter of vkWaitForFences, which deadlocks when waiting for a fence + // that was never submitted (e.g. the first call). + vkQueueWaitIdle(this->queue); + + VU_CHECK(vkResetCommandBuffer(this->one_shot_command_buffer, 0)); + const auto begin_info = VkCommandBufferBeginInfo{ + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, + .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT + }; + VU_CHECK(vkBeginCommandBuffer(this->one_shot_command_buffer, &begin_info)); + + cmd(this->one_shot_command_buffer); + + VU_CHECK(vkEndCommandBuffer(this->one_shot_command_buffer)); + + const auto submit_info = VkSubmitInfo{ + .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, + .waitSemaphoreCount = 0, + .pWaitSemaphores = nullptr, + .commandBufferCount = 1, + .pCommandBuffers = &this->one_shot_command_buffer, + .signalSemaphoreCount = 0, + .pSignalSemaphores = nullptr + }; + // VK_NULL_HANDLE instead of nullptr: on 32-bit targets the Vulkan headers + // define non-dispatchable handles (VkFence, VkSemaphore, ...) as 64-bit + // integers, not pointers + VU_CHECK(vkQueueSubmit(this->queue, 1, &submit_info, VK_NULL_HANDLE)); + + // wait for the command buffer to complete, so the caller can safely use the + // uploaded resources right away (e.g. sample the texture in the current frame) + vkQueueWaitIdle(this->queue); +} + +void factory::ensure_textures_uploaded() const +{ + // find textures that need uploading (no global flag: new textures can be created at any time) + std::vector pending; + for (auto* tex : this->all_textures) { + if (!tex->uploaded && !tex->deferred_data.empty()) { + pending.push_back(tex); + } + } + + if (pending.empty()) { + // mark empty textures as uploaded + for (auto* tex : this->all_textures) { + if (!tex->uploaded && tex->deferred_data.empty()) { + tex->uploaded = true; + } + } + return; + } + + // calculate total staging size + VkDeviceSize total_size = 0; + for (auto* tex : pending) { + total_size += tex->staging_size(); + } + + if (total_size == 0) { + return; + } + + // create a single staging buffer for all textures + VkBuffer staging_buffer = VK_NULL_HANDLE; + VkDeviceMemory staging_memory = VK_NULL_HANDLE; + { + const auto buffer_info = VkBufferCreateInfo{ + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .size = total_size, + .usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT, + .sharingMode = VK_SHARING_MODE_EXCLUSIVE + }; + VU_CHECK(vkCreateBuffer(this->device, &buffer_info, nullptr, &staging_buffer)); + + VkMemoryRequirements mem_reqs{}; + vkGetBufferMemoryRequirements(this->device, staging_buffer, &mem_reqs); + + const auto alloc_info = VkMemoryAllocateInfo{ + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, + .allocationSize = mem_reqs.size, + .memoryTypeIndex = pick_host_visible_memory_type(this->physical_device, mem_reqs) + }; + VU_CHECK(vkAllocateMemory(this->device, &alloc_info, nullptr, &staging_memory)); + VU_CHECK(vkBindBufferMemory(this->device, staging_buffer, staging_memory, 0)); + } + + // copy all texture data into the staging buffer + void* mapped = nullptr; + VU_CHECK(vkMapMemory(this->device, staging_memory, 0, total_size, 0, &mapped)); + const auto mapped_span = utki::span(static_cast(mapped), total_size); + VkDeviceSize offset = 0; + for (auto* tex : pending) { + const auto sz = tex->staging_size(); + std::memcpy(mapped_span.subspan(offset, sz).data(), tex->deferred_data.data(), sz); + offset += sz; + } + vkUnmapMemory(this->device, staging_memory); + + // record all upload commands into a single one-shot command buffer + VkDeviceSize cmd_offset = 0; + this->one_shot([&](VkCommandBuffer cmd) { + for (auto* tex : pending) { + const auto sz = tex->staging_size(); + tex->record_upload_commands(cmd, staging_buffer, cmd_offset); + cmd_offset += sz; + } + }); + + // clean up + vkFreeMemory(this->device, staging_memory, nullptr); + vkDestroyBuffer(this->device, staging_buffer, nullptr); + + // mark uploaded textures + for (auto* tex : pending) { + tex->deferred_data.clear(); + tex->uploaded = true; + } +} diff --git a/src/ruis/render/vulkan/factory.hpp b/src/ruis/render/vulkan/factory.hpp new file mode 100644 index 0000000..29f3238 --- /dev/null +++ b/src/ruis/render/vulkan/factory.hpp @@ -0,0 +1,193 @@ +/* +ruis-render-vulkan - Vulkan renderer + +Copyright (C) 2012-2026 Ivan Gagis + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +/* ================ LICENSE END ================ */ + +#pragma once + +#include +#include + +#include +#include +#include + +#include "surface_provider.hpp" +#include "vulkan.hpp" + +namespace ruis::render::vulkan { + +class texture_2d; +class texture_depth; +class texture_cube; +class vertex_array; +class vertex_buffer; +class index_buffer; + +/** + * @brief Vulkan render factory. + * + * Owns the shared Vulkan resources (the logical device, the one-shot command + * pool used for uploads, the shared pipeline/descriptor set layouts and the + * pipeline cache) and is the only place that creates the shareable rendering + * objects (textures, vertex/index buffers, vertex arrays, shaders). + * + * A single factory is shared across all windows of an application: it wraps the + * shared @ref vulkan_device (via the surface_provider) so that GPU objects created + * for one window (e.g. the common white texture and quad vertex buffers) can be + * used by any other window. + */ +class factory : public ruis::render::factory +{ +public: + static constexpr uint32_t vertex_component_count = 4; + + // ---- shared Vulkan handles (public: used by derived GPU resource classes) ---- + VkDevice device = VK_NULL_HANDLE; + VkPhysicalDevice physical_device = VK_NULL_HANDLE; + VkQueue queue = VK_NULL_HANDLE; + VkInstance instance = VK_NULL_HANDLE; + + // ---- texture tracking for deferred upload (public: used by texture_2d) ---- + std::vector all_textures; + + factory( + const utki::shared_ref& native_window, // + const utki::shared_ref& surface_provider + ); + ~factory() override; + + // noncopyable: owns the shared GPU resources + factory(const factory&) = delete; + factory& operator=(const factory&) = delete; + factory(factory&&) = delete; + factory& operator=(factory&&) = delete; + + // ---- shared pipeline / descriptor resources (used by the per-window context) ---- + VkPipelineLayout get_pipeline_layout_no_tex() const + { + return this->pipeline_layout_no_tex; + } + + VkPipelineLayout get_pipeline_layout_with_tex() const + { + return this->pipeline_layout_with_tex; + } + + VkDescriptorSetLayout get_dsl_no_tex() const + { + return this->dsl_no_tex; + } + + VkDescriptorSetLayout get_dsl_with_tex() const + { + return this->dsl_with_tex; + } + + VkPipelineCache get_pipeline_cache() const + { + return this->pipeline_cache; + } + + // =============================== + // ====== factory functions ====== + + utki::shared_ref make_shaders() const override; + + utki::shared_ref make_texture_2d( + rasterimage::format format, + rasterimage::dimensioned::dimensions_type dims, + texture_2d_parameters params + ) const override; + + utki::shared_ref make_texture_2d( + const rasterimage::image_variant& imvar, + texture_2d_parameters params + ) const override; + + utki::shared_ref make_texture_2d( + rasterimage::image_variant&& imvar, + texture_2d_parameters params + ) const override; + + utki::shared_ref make_texture_depth( // + rasterimage::dimensioned::dimensions_type dims + ) const override; + + utki::shared_ref make_texture_cube( + rasterimage::image_variant&& positive_x, + rasterimage::image_variant&& negative_x, + rasterimage::image_variant&& positive_y, + rasterimage::image_variant&& negative_y, + rasterimage::image_variant&& positive_z, + rasterimage::image_variant&& negative_z + ) const override; + + utki::shared_ref make_vertex_buffer( // + utki::span> vertices + ) const override; + utki::shared_ref make_vertex_buffer( // + utki::span> vertices + ) const override; + utki::shared_ref make_vertex_buffer( // + utki::span> vertices + ) const override; + utki::shared_ref make_vertex_buffer( // + utki::span vertices + ) const override; + + utki::shared_ref make_index_buffer( // + utki::span indices + ) const override; + utki::shared_ref make_index_buffer( // + utki::span indices + ) const override; + + utki::shared_ref make_vertex_array( + std::vector> buffers, // + utki::shared_ref indices, + ruis::render::vertex_array::mode rendering_mode + ) const override; + + // =============================== + // = vulkan-specific interface = + + // run a one-shot command buffer (used for texture uploads); blocks until complete. + // const: it only submits GPU work, it does not mutate the factory's C++ state. + void one_shot(std::function cmd) const; + + // upload all deferred textures to the GPU (idempotent). const: it only mutates + // the texture objects (their upload state), not the factory's C++ state. + void ensure_textures_uploaded() const; + +private: + // ---- one-shot command submission (texture uploads) ---- + VkCommandPool one_shot_pool = VK_NULL_HANDLE; + VkCommandBuffer one_shot_command_buffer = VK_NULL_HANDLE; + VkFence one_shot_fence = VK_NULL_HANDLE; + + // ---- shared descriptor / pipeline layout ---- + VkPipelineLayout pipeline_layout_no_tex = VK_NULL_HANDLE; + VkPipelineLayout pipeline_layout_with_tex = VK_NULL_HANDLE; + VkDescriptorSetLayout dsl_no_tex = VK_NULL_HANDLE; // binding 0: UBO + VkDescriptorSetLayout dsl_with_tex = VK_NULL_HANDLE; // binding 0: UBO, binding 1: sampled image + VkPipelineCache pipeline_cache = VK_NULL_HANDLE; +}; + +} // namespace ruis::render::vulkan diff --git a/src/ruis/render/vulkan/index_buffer.cpp b/src/ruis/render/vulkan/index_buffer.cpp index 1f92b6f..d62c158 100644 --- a/src/ruis/render/vulkan/index_buffer.cpp +++ b/src/ruis/render/vulkan/index_buffer.cpp @@ -28,7 +28,7 @@ along with this program. If not, see . using namespace ruis::render::vulkan; index_buffer::index_buffer( - const utki::shared_ref& rendering_context, // + const utki::shared_ref& rendering_context, // const void* data, size_t size_bytes, size_t size, @@ -44,7 +44,7 @@ index_buffer::index_buffer( } index_buffer::index_buffer( - const utki::shared_ref& rendering_context, // + const utki::shared_ref& rendering_context, // utki::span indices ) : index_buffer( @@ -57,7 +57,7 @@ index_buffer::index_buffer( {} index_buffer::index_buffer( - const utki::shared_ref& rendering_context, // + const utki::shared_ref& rendering_context, // utki::span indices ) : index_buffer( @@ -72,7 +72,7 @@ index_buffer::index_buffer( index_buffer::~index_buffer() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer owns the object; the base-class reference comes from the ruis API") - auto& ctx = static_cast(this->ctx_ref.get()); + auto& ctx = static_cast(this->ctx_ref.get()); if (this->buffer != VK_NULL_HANDLE) { vkDestroyBuffer(ctx.device, this->buffer, nullptr); vkFreeMemory(ctx.device, this->memory, nullptr); @@ -86,7 +86,7 @@ void index_buffer::create_vk_buffer(const void* data, size_t size_bytes) } // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer owns the object; the base-class reference comes from the ruis API") - auto& ctx = static_cast(this->ctx_ref.get()); + auto& ctx = static_cast(this->ctx_ref.get()); const auto buffer_info = VkBufferCreateInfo{ .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, diff --git a/src/ruis/render/vulkan/index_buffer.hpp b/src/ruis/render/vulkan/index_buffer.hpp index bb1b711..d1737f4 100644 --- a/src/ruis/render/vulkan/index_buffer.hpp +++ b/src/ruis/render/vulkan/index_buffer.hpp @@ -33,14 +33,14 @@ class index_buffer : public ruis::render::index_buffer { public: VkBuffer buffer = VK_NULL_HANDLE; - const utki::shared_ref ctx_ref; + const utki::shared_ref ctx_ref; VkDeviceMemory memory = VK_NULL_HANDLE; VkIndexType index_type = VK_INDEX_TYPE_UINT16; uint32_t elements_count = 0; uint32_t byte_size = 0; index_buffer( - const utki::shared_ref& rendering_context, // + const utki::shared_ref& rendering_context, // const void* data, size_t size_bytes, size_t size, @@ -48,11 +48,11 @@ class index_buffer : public ruis::render::index_buffer ); index_buffer( - const utki::shared_ref& rendering_context, // + const utki::shared_ref& rendering_context, // utki::span indices ); index_buffer( - const utki::shared_ref& rendering_context, // + const utki::shared_ref& rendering_context, // utki::span indices ); diff --git a/src/ruis/render/vulkan/shaders/shader_color.cpp b/src/ruis/render/vulkan/shaders/shader_color.cpp index 27af2ea..ae74c3a 100644 --- a/src/ruis/render/vulkan/shaders/shader_color.cpp +++ b/src/ruis/render/vulkan/shaders/shader_color.cpp @@ -27,7 +27,7 @@ along with this program. If not, see . using namespace ruis::render::vulkan; -shader_color::shader_color(const utki::shared_ref& rendering_context) : +shader_color::shader_color(const utki::shared_ref& rendering_context) : ruis::render::coloring_shader(rendering_context), ctx_ref(rendering_context) {} diff --git a/src/ruis/render/vulkan/shaders/shader_color.hpp b/src/ruis/render/vulkan/shaders/shader_color.hpp index bffa76c..900bf70 100644 --- a/src/ruis/render/vulkan/shaders/shader_color.hpp +++ b/src/ruis/render/vulkan/shaders/shader_color.hpp @@ -31,13 +31,13 @@ namespace ruis::render::vulkan { class shader_color : public ruis::render::coloring_shader { public: - shader_color(const utki::shared_ref& rendering_context); + shader_color(const utki::shared_ref& rendering_context); void render(const r4::matrix4& m, const ruis::render::vertex_array& va, const r4::vector4& color) const override; private: - const utki::shared_ref ctx_ref; + const utki::shared_ref ctx_ref; }; } // namespace ruis::render::vulkan diff --git a/src/ruis/render/vulkan/shaders/shader_color_pos_lum.cpp b/src/ruis/render/vulkan/shaders/shader_color_pos_lum.cpp index 7748e48..f12b9fb 100644 --- a/src/ruis/render/vulkan/shaders/shader_color_pos_lum.cpp +++ b/src/ruis/render/vulkan/shaders/shader_color_pos_lum.cpp @@ -27,7 +27,7 @@ along with this program. If not, see . using namespace ruis::render::vulkan; -shader_color_pos_lum::shader_color_pos_lum(const utki::shared_ref& rendering_context) : +shader_color_pos_lum::shader_color_pos_lum(const utki::shared_ref& rendering_context) : ruis::render::coloring_shader(rendering_context), ctx_ref(rendering_context) {} diff --git a/src/ruis/render/vulkan/shaders/shader_color_pos_lum.hpp b/src/ruis/render/vulkan/shaders/shader_color_pos_lum.hpp index 0a741c8..b203513 100644 --- a/src/ruis/render/vulkan/shaders/shader_color_pos_lum.hpp +++ b/src/ruis/render/vulkan/shaders/shader_color_pos_lum.hpp @@ -31,13 +31,13 @@ namespace ruis::render::vulkan { class shader_color_pos_lum : public ruis::render::coloring_shader { public: - shader_color_pos_lum(const utki::shared_ref& rendering_context); + shader_color_pos_lum(const utki::shared_ref& rendering_context); void render(const r4::matrix4& m, const ruis::render::vertex_array& va, const r4::vector4& color) const override; private: - const utki::shared_ref ctx_ref; + const utki::shared_ref ctx_ref; }; } // namespace ruis::render::vulkan diff --git a/src/ruis/render/vulkan/shaders/shader_color_pos_tex.cpp b/src/ruis/render/vulkan/shaders/shader_color_pos_tex.cpp index 1e75088..8db8e62 100644 --- a/src/ruis/render/vulkan/shaders/shader_color_pos_tex.cpp +++ b/src/ruis/render/vulkan/shaders/shader_color_pos_tex.cpp @@ -29,7 +29,7 @@ along with this program. If not, see . using namespace ruis::render::vulkan; -shader_color_pos_tex::shader_color_pos_tex(const utki::shared_ref& rendering_context) : +shader_color_pos_tex::shader_color_pos_tex(const utki::shared_ref& rendering_context) : ruis::render::coloring_texturing_shader(rendering_context), ctx_ref(rendering_context) {} diff --git a/src/ruis/render/vulkan/shaders/shader_color_pos_tex.hpp b/src/ruis/render/vulkan/shaders/shader_color_pos_tex.hpp index 00bd526..cf6976d 100644 --- a/src/ruis/render/vulkan/shaders/shader_color_pos_tex.hpp +++ b/src/ruis/render/vulkan/shaders/shader_color_pos_tex.hpp @@ -31,7 +31,7 @@ namespace ruis::render::vulkan { class shader_color_pos_tex : public ruis::render::coloring_texturing_shader { public: - shader_color_pos_tex(const utki::shared_ref& rendering_context); + shader_color_pos_tex(const utki::shared_ref& rendering_context); void render( const r4::matrix4& m, @@ -41,7 +41,7 @@ class shader_color_pos_tex : public ruis::render::coloring_texturing_shader ) const override; private: - const utki::shared_ref ctx_ref; + const utki::shared_ref ctx_ref; }; } // namespace ruis::render::vulkan diff --git a/src/ruis/render/vulkan/shaders/shader_color_pos_tex_alpha.cpp b/src/ruis/render/vulkan/shaders/shader_color_pos_tex_alpha.cpp index cb6418b..b76da23 100644 --- a/src/ruis/render/vulkan/shaders/shader_color_pos_tex_alpha.cpp +++ b/src/ruis/render/vulkan/shaders/shader_color_pos_tex_alpha.cpp @@ -30,7 +30,7 @@ along with this program. If not, see . using namespace ruis::render::vulkan; shader_color_pos_tex_alpha::shader_color_pos_tex_alpha( - const utki::shared_ref& rendering_context + const utki::shared_ref& rendering_context ) : ruis::render::coloring_texturing_shader(rendering_context), ctx_ref(rendering_context) diff --git a/src/ruis/render/vulkan/shaders/shader_color_pos_tex_alpha.hpp b/src/ruis/render/vulkan/shaders/shader_color_pos_tex_alpha.hpp index e2a9f03..f12960e 100644 --- a/src/ruis/render/vulkan/shaders/shader_color_pos_tex_alpha.hpp +++ b/src/ruis/render/vulkan/shaders/shader_color_pos_tex_alpha.hpp @@ -31,7 +31,7 @@ namespace ruis::render::vulkan { class shader_color_pos_tex_alpha : public ruis::render::coloring_texturing_shader { public: - shader_color_pos_tex_alpha(const utki::shared_ref& rendering_context); + shader_color_pos_tex_alpha(const utki::shared_ref& rendering_context); void render( const r4::matrix4& m, @@ -41,7 +41,7 @@ class shader_color_pos_tex_alpha : public ruis::render::coloring_texturing_shade ) const override; private: - const utki::shared_ref ctx_ref; + const utki::shared_ref ctx_ref; }; } // namespace ruis::render::vulkan diff --git a/src/ruis/render/vulkan/shaders/shader_common.cpp b/src/ruis/render/vulkan/shaders/shader_common.cpp index 6194838..78a1fcb 100644 --- a/src/ruis/render/vulkan/shaders/shader_common.cpp +++ b/src/ruis/render/vulkan/shaders/shader_common.cpp @@ -31,7 +31,7 @@ along with this program. If not, see . namespace ruis::render::vulkan { void vulkan_shader_render( - const utki::shared_ref& rendering_context, // + const utki::shared_ref& rendering_context, // shader_kind kind, const void* vertex_spirv, size_t vertex_spirv_len, @@ -108,7 +108,8 @@ void vulkan_shader_render( ctx.draw_indexed( pipeline, - textured ? ctx.pipeline_layout_with_tex : ctx.pipeline_layout_no_tex, + textured ? ctx.vu_factory.get().get_pipeline_layout_with_tex() + : ctx.vu_factory.get().get_pipeline_layout_no_tex(), va, descriptor_set ); diff --git a/src/ruis/render/vulkan/shaders/shader_common.hpp b/src/ruis/render/vulkan/shaders/shader_common.hpp index 5f2fac8..26a375e 100644 --- a/src/ruis/render/vulkan/shaders/shader_common.hpp +++ b/src/ruis/render/vulkan/shaders/shader_common.hpp @@ -69,7 +69,7 @@ class texture_2d; * may be different from the context currently rendering) before the draw. */ void vulkan_shader_render( - const utki::shared_ref& rendering_context, // + const utki::shared_ref& rendering_context, // shader_kind kind, const void* vertex_spirv, size_t vertex_spirv_len, diff --git a/src/ruis/render/vulkan/shaders/shader_pos_clr.cpp b/src/ruis/render/vulkan/shaders/shader_pos_clr.cpp index 1681bfe..9c47bbd 100644 --- a/src/ruis/render/vulkan/shaders/shader_pos_clr.cpp +++ b/src/ruis/render/vulkan/shaders/shader_pos_clr.cpp @@ -27,7 +27,7 @@ along with this program. If not, see . using namespace ruis::render::vulkan; -shader_pos_clr::shader_pos_clr(const utki::shared_ref& rendering_context) : +shader_pos_clr::shader_pos_clr(const utki::shared_ref& rendering_context) : ruis::render::shader(rendering_context), ctx_ref(rendering_context) {} diff --git a/src/ruis/render/vulkan/shaders/shader_pos_clr.hpp b/src/ruis/render/vulkan/shaders/shader_pos_clr.hpp index 9f5d5c4..ced342f 100644 --- a/src/ruis/render/vulkan/shaders/shader_pos_clr.hpp +++ b/src/ruis/render/vulkan/shaders/shader_pos_clr.hpp @@ -31,12 +31,12 @@ namespace ruis::render::vulkan { class shader_pos_clr : public ruis::render::shader { public: - shader_pos_clr(const utki::shared_ref& rendering_context); + shader_pos_clr(const utki::shared_ref& rendering_context); void render(const r4::matrix4& m, const ruis::render::vertex_array& va) const override; private: - const utki::shared_ref ctx_ref; + const utki::shared_ref ctx_ref; }; } // namespace ruis::render::vulkan diff --git a/src/ruis/render/vulkan/shaders/shader_pos_tex.cpp b/src/ruis/render/vulkan/shaders/shader_pos_tex.cpp index 0a30cbb..a790812 100644 --- a/src/ruis/render/vulkan/shaders/shader_pos_tex.cpp +++ b/src/ruis/render/vulkan/shaders/shader_pos_tex.cpp @@ -29,7 +29,7 @@ along with this program. If not, see . using namespace ruis::render::vulkan; -shader_pos_tex::shader_pos_tex(const utki::shared_ref& rendering_context) : +shader_pos_tex::shader_pos_tex(const utki::shared_ref& rendering_context) : ruis::render::texturing_shader(rendering_context), ctx_ref(rendering_context) {} diff --git a/src/ruis/render/vulkan/shaders/shader_pos_tex.hpp b/src/ruis/render/vulkan/shaders/shader_pos_tex.hpp index a32f188..627d193 100644 --- a/src/ruis/render/vulkan/shaders/shader_pos_tex.hpp +++ b/src/ruis/render/vulkan/shaders/shader_pos_tex.hpp @@ -31,13 +31,13 @@ namespace ruis::render::vulkan { class shader_pos_tex : public ruis::render::texturing_shader { public: - shader_pos_tex(const utki::shared_ref& rendering_context); + shader_pos_tex(const utki::shared_ref& rendering_context); void render(const r4::matrix4& m, const ruis::render::vertex_array& va, const ruis::render::texture_2d& tex) const override; private: - const utki::shared_ref ctx_ref; + const utki::shared_ref ctx_ref; }; } // namespace ruis::render::vulkan diff --git a/src/ruis/render/vulkan/texture_2d.cpp b/src/ruis/render/vulkan/texture_2d.cpp index 265d6ff..f57248e 100644 --- a/src/ruis/render/vulkan/texture_2d.cpp +++ b/src/ruis/render/vulkan/texture_2d.cpp @@ -92,12 +92,12 @@ void destroy_staging_buffer(VkDevice device, VkBuffer buffer, VkDeviceMemory mem } // namespace texture_2d::texture_2d( - const utki::shared_ref& rendering_context, // + const utki::shared_ref& rendering_context, // rasterimage::format type, rasterimage::dimensioned::dimensions_type dims, const uint8_t* data, size_t data_size, - ruis::render::context::texture_2d_parameters params + ruis::render::factory::texture_2d_parameters params ) : ruis::render::texture_2d( rendering_context, // @@ -112,7 +112,7 @@ texture_2d::texture_2d( // register with the context for deferred upload tracking // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& vk_ctx = const_cast(static_cast(rendering_context.get())); + auto& vk_ctx = const_cast(static_cast(rendering_context.get())); vk_ctx.all_textures.push_back(this); if (data != nullptr && data_size != 0) { @@ -246,7 +246,7 @@ void texture_2d::ensure_uploaded() // no data to upload: clear the image to transparent so that // undefined GPU memory does not produce garbage pixels // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& ctx = const_cast(static_cast(this->ctx_ref.get())); + auto& ctx = const_cast(static_cast(this->ctx_ref.get())); ctx.one_shot([&](VkCommandBuffer cmd) { const auto range = VkImageSubresourceRange{ .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, @@ -317,7 +317,7 @@ void texture_2d::ensure_uploaded() texture_2d::~texture_2d() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& ctx = const_cast(static_cast(this->ctx_ref.get())); + auto& ctx = const_cast(static_cast(this->ctx_ref.get())); // remove from the context's texture list auto& tex_list = ctx.all_textures; for (auto it = tex_list.begin(); it != tex_list.end(); ++it) { @@ -337,7 +337,7 @@ texture_2d::~texture_2d() void texture_2d::create_image(rasterimage::dimensioned::dimensions_type dims) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& ctx = const_cast(static_cast(this->ctx_ref.get())); + auto& ctx = const_cast(static_cast(this->ctx_ref.get())); VkImageCreateInfo image_info{}; image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; @@ -384,10 +384,10 @@ void texture_2d::create_image(rasterimage::dimensioned::dimensions_type dims) VU_CHECK(vkCreateImageView(ctx.device, &view_info, nullptr, &this->image_view)); } -void texture_2d::create_sampler(ruis::render::context::texture_2d_parameters params) +void texture_2d::create_sampler(ruis::render::factory::texture_2d_parameters params) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& ctx = const_cast(static_cast(this->ctx_ref.get())); + auto& ctx = const_cast(static_cast(this->ctx_ref.get())); (void)ctx; const auto sampler_info = VkSamplerCreateInfo{ @@ -416,7 +416,7 @@ void texture_2d::create_sampler(ruis::render::context::texture_2d_parameters par void texture_2d::upload_data(const uint8_t* data, size_t data_size) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& ctx = const_cast(static_cast(this->ctx_ref.get())); + auto& ctx = const_cast(static_cast(this->ctx_ref.get())); VkBuffer staging_buffer = VK_NULL_HANDLE; VkDeviceMemory staging_memory = VK_NULL_HANDLE; diff --git a/src/ruis/render/vulkan/texture_2d.hpp b/src/ruis/render/vulkan/texture_2d.hpp index aa27f4a..9207dc3 100644 --- a/src/ruis/render/vulkan/texture_2d.hpp +++ b/src/ruis/render/vulkan/texture_2d.hpp @@ -41,12 +41,12 @@ class texture_2d : public ruis::render::texture_2d rasterimage::format ri_format = rasterimage::format::rgba; texture_2d( - const utki::shared_ref& rendering_context, // + const utki::shared_ref& rendering_context, // rasterimage::format type, rasterimage::dimensioned::dimensions_type dims, const uint8_t* data, size_t data_size, - ruis::render::context::texture_2d_parameters params + ruis::render::factory::texture_2d_parameters params ); // upload the data after the swapchain is ready @@ -60,10 +60,10 @@ class texture_2d : public ruis::render::texture_2d ~texture_2d() override; private: - const utki::shared_ref ctx_ref; + const utki::shared_ref ctx_ref; void create_image(rasterimage::dimensioned::dimensions_type dims); void upload_data(const uint8_t* data, size_t data_size); - void create_sampler(ruis::render::context::texture_2d_parameters params); + void create_sampler(ruis::render::factory::texture_2d_parameters params); // deferred upload (public for batched upload in context) diff --git a/src/ruis/render/vulkan/texture_depth.cpp b/src/ruis/render/vulkan/texture_depth.cpp index 89c9330..900c8c6 100644 --- a/src/ruis/render/vulkan/texture_depth.cpp +++ b/src/ruis/render/vulkan/texture_depth.cpp @@ -26,7 +26,7 @@ along with this program. If not, see . using namespace ruis::render::vulkan; texture_depth::texture_depth( - const utki::shared_ref& rendering_context, // + const utki::shared_ref& rendering_context, // r4::vector2 dims ) : ruis::render::texture_depth(rendering_context, dims), @@ -38,7 +38,7 @@ texture_depth::texture_depth( texture_depth::~texture_depth() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer owns the object; the base-class reference comes from the ruis API") - auto& ctx = static_cast(this->ctx_ref.get()); + auto& ctx = static_cast(this->ctx_ref.get()); vkDestroyImageView(ctx.device, this->image_view, nullptr); vkDestroyImage(ctx.device, this->image, nullptr); if (this->memory != VK_NULL_HANDLE) { @@ -49,7 +49,7 @@ texture_depth::~texture_depth() void texture_depth::create_image() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer owns the object; the base-class reference comes from the ruis API") - auto& ctx = static_cast(this->ctx_ref.get()); + auto& ctx = static_cast(this->ctx_ref.get()); const auto image_info = VkImageCreateInfo{ .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, diff --git a/src/ruis/render/vulkan/texture_depth.hpp b/src/ruis/render/vulkan/texture_depth.hpp index cb186d1..caa2ac5 100644 --- a/src/ruis/render/vulkan/texture_depth.hpp +++ b/src/ruis/render/vulkan/texture_depth.hpp @@ -36,7 +36,7 @@ class texture_depth : public ruis::render::texture_depth VkImageView image_view = VK_NULL_HANDLE; texture_depth( - const utki::shared_ref& rendering_context, // + const utki::shared_ref& rendering_context, // r4::vector2 dims ); texture_depth(const texture_depth&) = delete; @@ -46,7 +46,7 @@ class texture_depth : public ruis::render::texture_depth ~texture_depth() override; private: - const utki::shared_ref ctx_ref; + const utki::shared_ref ctx_ref; void create_image(); }; diff --git a/src/ruis/render/vulkan/vertex_array.cpp b/src/ruis/render/vulkan/vertex_array.cpp index 094da43..28bd0d4 100644 --- a/src/ruis/render/vulkan/vertex_array.cpp +++ b/src/ruis/render/vulkan/vertex_array.cpp @@ -28,7 +28,7 @@ along with this program. If not, see . using namespace ruis::render::vulkan; vertex_array::vertex_array( - const utki::shared_ref& rendering_context, // + const utki::shared_ref& rendering_context, // buffers_type buffers, utki::shared_ref indices, mode rendering_mode diff --git a/src/ruis/render/vulkan/vertex_array.hpp b/src/ruis/render/vulkan/vertex_array.hpp index 801df8c..19ee55d 100644 --- a/src/ruis/render/vulkan/vertex_array.hpp +++ b/src/ruis/render/vulkan/vertex_array.hpp @@ -36,7 +36,7 @@ class vertex_array : public ruis::render::vertex_array { public: vertex_array( - const utki::shared_ref& rendering_context, // + const utki::shared_ref& rendering_context, // buffers_type buffers, utki::shared_ref indices, mode rendering_mode diff --git a/src/ruis/render/vulkan/vertex_buffer.cpp b/src/ruis/render/vulkan/vertex_buffer.cpp index bb78b41..cb1947f 100644 --- a/src/ruis/render/vulkan/vertex_buffer.cpp +++ b/src/ruis/render/vulkan/vertex_buffer.cpp @@ -28,10 +28,10 @@ along with this program. If not, see . using namespace ruis::render::vulkan; vertex_buffer::vertex_buffer( - const utki::shared_ref& rendering_context, // + const utki::shared_ref& rendering_context, // std::vector&& padded_data ) : - ruis::render::vertex_buffer(rendering_context, padded_data.size() / context::vertex_component_count), + ruis::render::vertex_buffer(rendering_context, padded_data.size() / factory::vertex_component_count), ctx_ref(rendering_context) { this->create_vk_buffer(std::move(padded_data)); @@ -40,7 +40,7 @@ vertex_buffer::vertex_buffer( vertex_buffer::~vertex_buffer() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer owns the object; the base-class reference comes from the ruis API") - auto& ctx = static_cast(this->ctx_ref.get()); + auto& ctx = static_cast(this->ctx_ref.get()); if (this->buffer != VK_NULL_HANDLE) { vkDestroyBuffer(ctx.device, this->buffer, nullptr); vkFreeMemory(ctx.device, this->memory, nullptr); @@ -57,7 +57,7 @@ void vertex_buffer::create_vk_buffer( } // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer owns the object; the base-class reference comes from the ruis API") - auto& ctx = static_cast(this->ctx_ref.get()); + auto& ctx = static_cast(this->ctx_ref.get()); this->byte_size = uint32_t(data.size() * sizeof(float)); diff --git a/src/ruis/render/vulkan/vertex_buffer.hpp b/src/ruis/render/vulkan/vertex_buffer.hpp index bf0a930..8b4e1ec 100644 --- a/src/ruis/render/vulkan/vertex_buffer.hpp +++ b/src/ruis/render/vulkan/vertex_buffer.hpp @@ -53,7 +53,7 @@ class vertex_buffer : public ruis::render::vertex_buffer uint32_t byte_size = 0; vertex_buffer( - const utki::shared_ref& rendering_context, // + const utki::shared_ref& rendering_context, // std::vector&& padded_data ); @@ -65,7 +65,7 @@ class vertex_buffer : public ruis::render::vertex_buffer ~vertex_buffer() override; private: - const utki::shared_ref ctx_ref; + const utki::shared_ref ctx_ref; void create_vk_buffer(std::vector&& data); }; From a58786ab560c08de69c6d714ce41a22c4a93cc2b Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Thu, 19 Nov 2026 15:06:10 +0200 Subject: [PATCH 02/12] qualify factory type in downcasts to avoid name hiding The base-class 'factory' data member hides the type in derived scope, so the downcasts in the concrete buffer/texture classes now use the fully-qualified ruis::render::vulkan::factory type. --- src/ruis/render/vulkan/index_buffer.cpp | 4 ++-- src/ruis/render/vulkan/texture_2d.cpp | 12 ++++++------ src/ruis/render/vulkan/texture_depth.cpp | 4 ++-- src/ruis/render/vulkan/vertex_buffer.cpp | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/ruis/render/vulkan/index_buffer.cpp b/src/ruis/render/vulkan/index_buffer.cpp index d62c158..9c593d8 100644 --- a/src/ruis/render/vulkan/index_buffer.cpp +++ b/src/ruis/render/vulkan/index_buffer.cpp @@ -72,7 +72,7 @@ index_buffer::index_buffer( index_buffer::~index_buffer() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer owns the object; the base-class reference comes from the ruis API") - auto& ctx = static_cast(this->ctx_ref.get()); + auto& ctx = static_cast(this->ctx_ref.get()); if (this->buffer != VK_NULL_HANDLE) { vkDestroyBuffer(ctx.device, this->buffer, nullptr); vkFreeMemory(ctx.device, this->memory, nullptr); @@ -86,7 +86,7 @@ void index_buffer::create_vk_buffer(const void* data, size_t size_bytes) } // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer owns the object; the base-class reference comes from the ruis API") - auto& ctx = static_cast(this->ctx_ref.get()); + auto& ctx = static_cast(this->ctx_ref.get()); const auto buffer_info = VkBufferCreateInfo{ .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, diff --git a/src/ruis/render/vulkan/texture_2d.cpp b/src/ruis/render/vulkan/texture_2d.cpp index f57248e..52d0239 100644 --- a/src/ruis/render/vulkan/texture_2d.cpp +++ b/src/ruis/render/vulkan/texture_2d.cpp @@ -112,7 +112,7 @@ texture_2d::texture_2d( // register with the context for deferred upload tracking // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& vk_ctx = const_cast(static_cast(rendering_context.get())); + auto& vk_ctx = const_cast(static_cast(rendering_context.get())); vk_ctx.all_textures.push_back(this); if (data != nullptr && data_size != 0) { @@ -246,7 +246,7 @@ void texture_2d::ensure_uploaded() // no data to upload: clear the image to transparent so that // undefined GPU memory does not produce garbage pixels // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& ctx = const_cast(static_cast(this->ctx_ref.get())); + auto& ctx = const_cast(static_cast(this->ctx_ref.get())); ctx.one_shot([&](VkCommandBuffer cmd) { const auto range = VkImageSubresourceRange{ .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, @@ -317,7 +317,7 @@ void texture_2d::ensure_uploaded() texture_2d::~texture_2d() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& ctx = const_cast(static_cast(this->ctx_ref.get())); + auto& ctx = const_cast(static_cast(this->ctx_ref.get())); // remove from the context's texture list auto& tex_list = ctx.all_textures; for (auto it = tex_list.begin(); it != tex_list.end(); ++it) { @@ -337,7 +337,7 @@ texture_2d::~texture_2d() void texture_2d::create_image(rasterimage::dimensioned::dimensions_type dims) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& ctx = const_cast(static_cast(this->ctx_ref.get())); + auto& ctx = const_cast(static_cast(this->ctx_ref.get())); VkImageCreateInfo image_info{}; image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; @@ -387,7 +387,7 @@ void texture_2d::create_image(rasterimage::dimensioned::dimensions_type dims) void texture_2d::create_sampler(ruis::render::factory::texture_2d_parameters params) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& ctx = const_cast(static_cast(this->ctx_ref.get())); + auto& ctx = const_cast(static_cast(this->ctx_ref.get())); (void)ctx; const auto sampler_info = VkSamplerCreateInfo{ @@ -416,7 +416,7 @@ void texture_2d::create_sampler(ruis::render::factory::texture_2d_parameters par void texture_2d::upload_data(const uint8_t* data, size_t data_size) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& ctx = const_cast(static_cast(this->ctx_ref.get())); + auto& ctx = const_cast(static_cast(this->ctx_ref.get())); VkBuffer staging_buffer = VK_NULL_HANDLE; VkDeviceMemory staging_memory = VK_NULL_HANDLE; diff --git a/src/ruis/render/vulkan/texture_depth.cpp b/src/ruis/render/vulkan/texture_depth.cpp index 900c8c6..ad2d288 100644 --- a/src/ruis/render/vulkan/texture_depth.cpp +++ b/src/ruis/render/vulkan/texture_depth.cpp @@ -38,7 +38,7 @@ texture_depth::texture_depth( texture_depth::~texture_depth() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer owns the object; the base-class reference comes from the ruis API") - auto& ctx = static_cast(this->ctx_ref.get()); + auto& ctx = static_cast(this->ctx_ref.get()); vkDestroyImageView(ctx.device, this->image_view, nullptr); vkDestroyImage(ctx.device, this->image, nullptr); if (this->memory != VK_NULL_HANDLE) { @@ -49,7 +49,7 @@ texture_depth::~texture_depth() void texture_depth::create_image() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer owns the object; the base-class reference comes from the ruis API") - auto& ctx = static_cast(this->ctx_ref.get()); + auto& ctx = static_cast(this->ctx_ref.get()); const auto image_info = VkImageCreateInfo{ .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, diff --git a/src/ruis/render/vulkan/vertex_buffer.cpp b/src/ruis/render/vulkan/vertex_buffer.cpp index cb1947f..de079dc 100644 --- a/src/ruis/render/vulkan/vertex_buffer.cpp +++ b/src/ruis/render/vulkan/vertex_buffer.cpp @@ -40,7 +40,7 @@ vertex_buffer::vertex_buffer( vertex_buffer::~vertex_buffer() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer owns the object; the base-class reference comes from the ruis API") - auto& ctx = static_cast(this->ctx_ref.get()); + auto& ctx = static_cast(this->ctx_ref.get()); if (this->buffer != VK_NULL_HANDLE) { vkDestroyBuffer(ctx.device, this->buffer, nullptr); vkFreeMemory(ctx.device, this->memory, nullptr); @@ -57,7 +57,7 @@ void vertex_buffer::create_vk_buffer( } // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer owns the object; the base-class reference comes from the ruis API") - auto& ctx = static_cast(this->ctx_ref.get()); + auto& ctx = static_cast(this->ctx_ref.get()); this->byte_size = uint32_t(data.size() * sizeof(float)); From 58c9ef4e0d820bee2817dd512b8e49443a09c99e Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Thu, 19 Nov 2026 15:07:54 +0200 Subject: [PATCH 03/12] format --- src/ruis/render/vulkan/texture_2d.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/ruis/render/vulkan/texture_2d.cpp b/src/ruis/render/vulkan/texture_2d.cpp index 52d0239..63204a9 100644 --- a/src/ruis/render/vulkan/texture_2d.cpp +++ b/src/ruis/render/vulkan/texture_2d.cpp @@ -112,7 +112,9 @@ texture_2d::texture_2d( // register with the context for deferred upload tracking // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& vk_ctx = const_cast(static_cast(rendering_context.get())); + auto& vk_ctx = const_cast( + static_cast(rendering_context.get()) + ); vk_ctx.all_textures.push_back(this); if (data != nullptr && data_size != 0) { @@ -246,7 +248,9 @@ void texture_2d::ensure_uploaded() // no data to upload: clear the image to transparent so that // undefined GPU memory does not produce garbage pixels // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& ctx = const_cast(static_cast(this->ctx_ref.get())); + auto& ctx = const_cast( + static_cast(this->ctx_ref.get()) + ); ctx.one_shot([&](VkCommandBuffer cmd) { const auto range = VkImageSubresourceRange{ .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, @@ -317,7 +321,9 @@ void texture_2d::ensure_uploaded() texture_2d::~texture_2d() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& ctx = const_cast(static_cast(this->ctx_ref.get())); + auto& ctx = + const_cast(static_cast(this->ctx_ref.get() + )); // remove from the context's texture list auto& tex_list = ctx.all_textures; for (auto it = tex_list.begin(); it != tex_list.end(); ++it) { @@ -337,7 +343,9 @@ texture_2d::~texture_2d() void texture_2d::create_image(rasterimage::dimensioned::dimensions_type dims) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& ctx = const_cast(static_cast(this->ctx_ref.get())); + auto& ctx = + const_cast(static_cast(this->ctx_ref.get() + )); VkImageCreateInfo image_info{}; image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; @@ -387,7 +395,9 @@ void texture_2d::create_image(rasterimage::dimensioned::dimensions_type dims) void texture_2d::create_sampler(ruis::render::factory::texture_2d_parameters params) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& ctx = const_cast(static_cast(this->ctx_ref.get())); + auto& ctx = + const_cast(static_cast(this->ctx_ref.get() + )); (void)ctx; const auto sampler_info = VkSamplerCreateInfo{ @@ -416,7 +426,9 @@ void texture_2d::create_sampler(ruis::render::factory::texture_2d_parameters par void texture_2d::upload_data(const uint8_t* data, size_t data_size) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& ctx = const_cast(static_cast(this->ctx_ref.get())); + auto& ctx = + const_cast(static_cast(this->ctx_ref.get() + )); VkBuffer staging_buffer = VK_NULL_HANDLE; VkDeviceMemory staging_memory = VK_NULL_HANDLE; From 62c9467facec5319d8bc0baf523b10e5638381bd Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Thu, 19 Nov 2026 18:40:04 +0200 Subject: [PATCH 04/12] render: address review comments on the factory refactor - Rename the factory constructor parameter from rendering_context to factory and pass it by value with std::move (a shared_ref should be passed by value, not by const reference). - Drop the redundant ctx_ref members from the Vulkan shaders, GPU objects (texture_2d, vertex_buffer, texture_depth, index_buffer) and the frame_buffer: the base classes already retain the factory (now protected) or the context (frame_buffer), so the derived classes access them directly. - Hide the factory's all_textures list behind private mutable state with const register_texture()/unregister_texture() accessors, so the texture ctor/dtor no longer need a const_cast on the const factory reference. - Rename the context's retained factory reference from vu_factory to factory and remove the now-unnecessary const_casts on the factory (all of its methods are const). --- src/ruis/render/vulkan/context.cpp | 21 +++---- src/ruis/render/vulkan/context.hpp | 2 +- src/ruis/render/vulkan/factory.cpp | 11 ++++ src/ruis/render/vulkan/factory.hpp | 22 ++++++- src/ruis/render/vulkan/frame_buffer.cpp | 11 ++-- src/ruis/render/vulkan/frame_buffer.hpp | 1 - src/ruis/render/vulkan/index_buffer.cpp | 17 +++--- src/ruis/render/vulkan/index_buffer.hpp | 7 +-- .../render/vulkan/shaders/shader_color.cpp | 7 +-- .../render/vulkan/shaders/shader_color.hpp | 5 +- .../vulkan/shaders/shader_color_pos_lum.cpp | 7 +-- .../vulkan/shaders/shader_color_pos_lum.hpp | 5 +- .../vulkan/shaders/shader_color_pos_tex.cpp | 7 +-- .../vulkan/shaders/shader_color_pos_tex.hpp | 5 +- .../shaders/shader_color_pos_tex_alpha.cpp | 9 +-- .../shaders/shader_color_pos_tex_alpha.hpp | 5 +- .../render/vulkan/shaders/shader_common.cpp | 7 ++- .../render/vulkan/shaders/shader_common.hpp | 2 +- .../render/vulkan/shaders/shader_pos_clr.cpp | 7 +-- .../render/vulkan/shaders/shader_pos_clr.hpp | 5 +- .../render/vulkan/shaders/shader_pos_tex.cpp | 7 +-- .../render/vulkan/shaders/shader_pos_tex.hpp | 5 +- src/ruis/render/vulkan/texture_2d.cpp | 58 ++++++------------- src/ruis/render/vulkan/texture_2d.hpp | 3 +- src/ruis/render/vulkan/texture_depth.cpp | 9 ++- src/ruis/render/vulkan/texture_depth.hpp | 3 +- src/ruis/render/vulkan/vertex_array.cpp | 4 +- src/ruis/render/vulkan/vertex_array.hpp | 2 +- src/ruis/render/vulkan/vertex_buffer.cpp | 13 +++-- src/ruis/render/vulkan/vertex_buffer.hpp | 3 +- 30 files changed, 121 insertions(+), 149 deletions(-) diff --git a/src/ruis/render/vulkan/context.cpp b/src/ruis/render/vulkan/context.cpp index ca5d3eb..459a380 100644 --- a/src/ruis/render/vulkan/context.cpp +++ b/src/ruis/render/vulkan/context.cpp @@ -127,7 +127,7 @@ context::context( return parameters{m}; }() ), - vu_factory(factory), // + factory(factory), // physical_device(surface_provider.get().get_physical_device()), // device(surface_provider.get().get_device()), // surface_provider_ref(surface_provider), // @@ -897,7 +897,7 @@ VkDescriptorSet context::allocate_descriptor_set( VkSampler sampler ) { - const auto dsl = textured ? this->vu_factory.get().get_dsl_with_tex() : this->vu_factory.get().get_dsl_no_tex(); + const auto dsl = textured ? this->factory.get().get_dsl_with_tex() : this->factory.get().get_dsl_no_tex(); VkDescriptorSet set = VK_NULL_HANDLE; const auto alloc_info = VkDescriptorSetAllocateInfo{ @@ -1380,8 +1380,8 @@ VkPipeline context::build_pipeline( }; // ---- layout ---- - const auto layout = key.textured ? this->vu_factory.get().get_pipeline_layout_with_tex() - : this->vu_factory.get().get_pipeline_layout_no_tex(); + const auto layout = key.textured ? this->factory.get().get_pipeline_layout_with_tex() + : this->factory.get().get_pipeline_layout_no_tex(); // ---- multisample state ---- const auto multisample = VkPipelineMultisampleStateCreateInfo{ @@ -1409,14 +1409,9 @@ VkPipeline context::build_pipeline( }; VkPipeline pipeline = VK_NULL_HANDLE; - VU_CHECK(vkCreateGraphicsPipelines( - this->device, - this->vu_factory.get().get_pipeline_cache(), - 1, - &info, - nullptr, - &pipeline - )); + VU_CHECK( + vkCreateGraphicsPipelines(this->device, this->factory.get().get_pipeline_cache(), 1, &info, nullptr, &pipeline) + ); // destroy the shader modules (they are now baked into the pipeline) vkDestroyShaderModule(this->device, vertex_stage.module, nullptr); @@ -1435,7 +1430,7 @@ void context::begin_render_pass_internal(bool has_depth) has_depth = true; } // ensure all textures are uploaded before the first render pass - this->vu_factory.get().ensure_textures_uploaded(); + this->factory.get().ensure_textures_uploaded(); // determine the render pass (clear or load) const bool clear = this->pending_clear; diff --git a/src/ruis/render/vulkan/context.hpp b/src/ruis/render/vulkan/context.hpp index b4c552d..c05b698 100644 --- a/src/ruis/render/vulkan/context.hpp +++ b/src/ruis/render/vulkan/context.hpp @@ -193,7 +193,7 @@ class context : public ruis::render::context // the render factory (shared across all windows): provides the device, the // one-shot upload path, and the shared pipeline / descriptor set layouts - const utki::shared_ref vu_factory; + const utki::shared_ref factory; // ---- vulkan core (used internally by this per-window context) ---- VkPhysicalDevice physical_device = VK_NULL_HANDLE; diff --git a/src/ruis/render/vulkan/factory.cpp b/src/ruis/render/vulkan/factory.cpp index f84933b..cd465f5 100644 --- a/src/ruis/render/vulkan/factory.cpp +++ b/src/ruis/render/vulkan/factory.cpp @@ -457,3 +457,14 @@ void factory::ensure_textures_uploaded() const tex->uploaded = true; } } + +void factory::unregister_texture(texture_2d* t) const +{ + auto& tex_list = this->all_textures; + for (auto it = tex_list.begin(); it != tex_list.end(); ++it) { + if (*it == t) { + tex_list.erase(it); + break; + } + } +} diff --git a/src/ruis/render/vulkan/factory.hpp b/src/ruis/render/vulkan/factory.hpp index 29f3238..b3a49a0 100644 --- a/src/ruis/render/vulkan/factory.hpp +++ b/src/ruis/render/vulkan/factory.hpp @@ -33,11 +33,18 @@ along with this program. If not, see . namespace ruis::render::vulkan { +// the vulkan derived classes intentionally share names with the ruis::render base classes +// NOLINTNEXTLINE(bugprone-forward-declaration-namespace, "derived class shadows the ruis::render base class by design") class texture_2d; +// NOLINTNEXTLINE(bugprone-forward-declaration-namespace, "derived class shadows the ruis::render base class by design") class texture_depth; +// NOLINTNEXTLINE(bugprone-forward-declaration-namespace, "derived class shadows the ruis::render base class by design") class texture_cube; +// NOLINTNEXTLINE(bugprone-forward-declaration-namespace, "derived class shadows the ruis::render base class by design") class vertex_array; +// NOLINTNEXTLINE(bugprone-forward-declaration-namespace, "derived class shadows the ruis::render base class by design") class vertex_buffer; +// NOLINTNEXTLINE(bugprone-forward-declaration-namespace, "derived class shadows the ruis::render base class by design") class index_buffer; /** @@ -64,9 +71,6 @@ class factory : public ruis::render::factory VkQueue queue = VK_NULL_HANDLE; VkInstance instance = VK_NULL_HANDLE; - // ---- texture tracking for deferred upload (public: used by texture_2d) ---- - std::vector all_textures; - factory( const utki::shared_ref& native_window, // const utki::shared_ref& surface_provider @@ -176,7 +180,19 @@ class factory : public ruis::render::factory // the texture objects (their upload state), not the factory's C++ state. void ensure_textures_uploaded() const; + // register / unregister a texture with the factory's deferred-upload tracking. + // const: they only mutate the (mutable) tracking list, not the factory's C++ state. + void register_texture(texture_2d* t) const + { + this->all_textures.push_back(t); + } + + void unregister_texture(texture_2d* t) const; + private: + // ---- texture tracking for deferred upload (mutable: guarded by const register/unregister) ---- + mutable std::vector all_textures; + // ---- one-shot command submission (texture uploads) ---- VkCommandPool one_shot_pool = VK_NULL_HANDLE; VkCommandBuffer one_shot_command_buffer = VK_NULL_HANDLE; diff --git a/src/ruis/render/vulkan/frame_buffer.cpp b/src/ruis/render/vulkan/frame_buffer.cpp index b5e3726..e2fa07f 100644 --- a/src/ruis/render/vulkan/frame_buffer.cpp +++ b/src/ruis/render/vulkan/frame_buffer.cpp @@ -31,8 +31,7 @@ frame_buffer::frame_buffer( std::shared_ptr depth, std::shared_ptr stencil ) : - ruis::render::frame_buffer(rendering_context, std::move(color), std::move(depth), std::move(stencil)), - ctx_ref(rendering_context) + ruis::render::frame_buffer(rendering_context, std::move(color), std::move(depth), std::move(stencil)) { // validate that the color and depth attachments are Vulkan textures const auto& vcolor = this->vulkan_color(); @@ -40,7 +39,7 @@ frame_buffer::frame_buffer( // create the VkFramebuffer // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer owns the object; the base-class reference comes from the ruis API") - auto& ctx = static_cast(this->ctx_ref.get()); + auto& ctx = static_cast(this->rendering_context.get()); const auto w = uint32_t(vcolor.dims().x()); const auto h = uint32_t(vcolor.dims().y()); @@ -77,7 +76,7 @@ frame_buffer::~frame_buffer() { if (this->framebuffer != VK_NULL_HANDLE) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer owns the object; the base-class reference comes from the ruis API") - auto& ctx = static_cast(this->ctx_ref.get()); + auto& ctx = static_cast(this->rendering_context.get()); vkDestroyFramebuffer(ctx.device, this->framebuffer, nullptr); } } @@ -92,8 +91,8 @@ rasterimage::image_variant frame_buffer::get_color_pixels() // read the color attachment back to CPU RAM. the FBO render was recorded in the // frame's command buffer (deferred), so the readback is appended to it, then the // frame is submitted and waited on. - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& ctx = const_cast(static_cast(this->ctx_ref.get())); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer owns the object; the base-class reference comes from the ruis API") + auto& ctx = static_cast(this->rendering_context.get()); return ctx.readback_framebuffer_color(*this); } diff --git a/src/ruis/render/vulkan/frame_buffer.hpp b/src/ruis/render/vulkan/frame_buffer.hpp index a6d6e93..e36a6ca 100644 --- a/src/ruis/render/vulkan/frame_buffer.hpp +++ b/src/ruis/render/vulkan/frame_buffer.hpp @@ -55,7 +55,6 @@ class frame_buffer : public ruis::render::frame_buffer VkFramebuffer vulkan_framebuffer() const; private: - const utki::shared_ref ctx_ref; VkFramebuffer framebuffer = VK_NULL_HANDLE; }; diff --git a/src/ruis/render/vulkan/index_buffer.cpp b/src/ruis/render/vulkan/index_buffer.cpp index 9c593d8..121f2d9 100644 --- a/src/ruis/render/vulkan/index_buffer.cpp +++ b/src/ruis/render/vulkan/index_buffer.cpp @@ -28,14 +28,13 @@ along with this program. If not, see . using namespace ruis::render::vulkan; index_buffer::index_buffer( - const utki::shared_ref& rendering_context, // + utki::shared_ref factory, // const void* data, size_t size_bytes, size_t size, VkIndexType index_type ) : - ruis::render::index_buffer(rendering_context), - ctx_ref(rendering_context), + ruis::render::index_buffer(std::move(factory)), index_type(index_type), elements_count(uint32_t(size)), byte_size(uint32_t(size_bytes)) @@ -44,11 +43,11 @@ index_buffer::index_buffer( } index_buffer::index_buffer( - const utki::shared_ref& rendering_context, // + utki::shared_ref factory, // utki::span indices ) : index_buffer( - rendering_context, // + std::move(factory), // indices.data(), indices.size_bytes(), indices.size(), @@ -57,11 +56,11 @@ index_buffer::index_buffer( {} index_buffer::index_buffer( - const utki::shared_ref& rendering_context, // + utki::shared_ref factory, // utki::span indices ) : index_buffer( - rendering_context, // + std::move(factory), // indices.data(), indices.size_bytes(), indices.size(), @@ -72,7 +71,7 @@ index_buffer::index_buffer( index_buffer::~index_buffer() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer owns the object; the base-class reference comes from the ruis API") - auto& ctx = static_cast(this->ctx_ref.get()); + const auto& ctx = static_cast(this->factory.get()); if (this->buffer != VK_NULL_HANDLE) { vkDestroyBuffer(ctx.device, this->buffer, nullptr); vkFreeMemory(ctx.device, this->memory, nullptr); @@ -86,7 +85,7 @@ void index_buffer::create_vk_buffer(const void* data, size_t size_bytes) } // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer owns the object; the base-class reference comes from the ruis API") - auto& ctx = static_cast(this->ctx_ref.get()); + const auto& ctx = static_cast(this->factory.get()); const auto buffer_info = VkBufferCreateInfo{ .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, diff --git a/src/ruis/render/vulkan/index_buffer.hpp b/src/ruis/render/vulkan/index_buffer.hpp index d1737f4..4917a75 100644 --- a/src/ruis/render/vulkan/index_buffer.hpp +++ b/src/ruis/render/vulkan/index_buffer.hpp @@ -33,14 +33,13 @@ class index_buffer : public ruis::render::index_buffer { public: VkBuffer buffer = VK_NULL_HANDLE; - const utki::shared_ref ctx_ref; VkDeviceMemory memory = VK_NULL_HANDLE; VkIndexType index_type = VK_INDEX_TYPE_UINT16; uint32_t elements_count = 0; uint32_t byte_size = 0; index_buffer( - const utki::shared_ref& rendering_context, // + utki::shared_ref factory, // const void* data, size_t size_bytes, size_t size, @@ -48,11 +47,11 @@ class index_buffer : public ruis::render::index_buffer ); index_buffer( - const utki::shared_ref& rendering_context, // + utki::shared_ref factory, // utki::span indices ); index_buffer( - const utki::shared_ref& rendering_context, // + utki::shared_ref factory, // utki::span indices ); diff --git a/src/ruis/render/vulkan/shaders/shader_color.cpp b/src/ruis/render/vulkan/shaders/shader_color.cpp index ae74c3a..ba10295 100644 --- a/src/ruis/render/vulkan/shaders/shader_color.cpp +++ b/src/ruis/render/vulkan/shaders/shader_color.cpp @@ -27,9 +27,8 @@ along with this program. If not, see . using namespace ruis::render::vulkan; -shader_color::shader_color(const utki::shared_ref& rendering_context) : - ruis::render::coloring_shader(rendering_context), - ctx_ref(rendering_context) +shader_color::shader_color(utki::shared_ref factory) : + ruis::render::coloring_shader(std::move(factory)) {} void shader_color::render( @@ -39,7 +38,7 @@ void shader_color::render( ) const { vulkan_shader_render( - this->ctx_ref, // + this->factory, // shader_kind::color, std::data(spirv_shader_color_vertex), spirv_shader_color_vertex_size, diff --git a/src/ruis/render/vulkan/shaders/shader_color.hpp b/src/ruis/render/vulkan/shaders/shader_color.hpp index 900bf70..8a256f6 100644 --- a/src/ruis/render/vulkan/shaders/shader_color.hpp +++ b/src/ruis/render/vulkan/shaders/shader_color.hpp @@ -31,13 +31,10 @@ namespace ruis::render::vulkan { class shader_color : public ruis::render::coloring_shader { public: - shader_color(const utki::shared_ref& rendering_context); + shader_color(utki::shared_ref factory); void render(const r4::matrix4& m, const ruis::render::vertex_array& va, const r4::vector4& color) const override; - -private: - const utki::shared_ref ctx_ref; }; } // namespace ruis::render::vulkan diff --git a/src/ruis/render/vulkan/shaders/shader_color_pos_lum.cpp b/src/ruis/render/vulkan/shaders/shader_color_pos_lum.cpp index f12b9fb..7567082 100644 --- a/src/ruis/render/vulkan/shaders/shader_color_pos_lum.cpp +++ b/src/ruis/render/vulkan/shaders/shader_color_pos_lum.cpp @@ -27,9 +27,8 @@ along with this program. If not, see . using namespace ruis::render::vulkan; -shader_color_pos_lum::shader_color_pos_lum(const utki::shared_ref& rendering_context) : - ruis::render::coloring_shader(rendering_context), - ctx_ref(rendering_context) +shader_color_pos_lum::shader_color_pos_lum(utki::shared_ref factory) : + ruis::render::coloring_shader(std::move(factory)) {} void shader_color_pos_lum::render( @@ -39,7 +38,7 @@ void shader_color_pos_lum::render( ) const { vulkan_shader_render( - this->ctx_ref, // + this->factory, // shader_kind::color_pos_lum, std::data(spirv_shader_color_pos_lum_vertex), spirv_shader_color_pos_lum_vertex_size, diff --git a/src/ruis/render/vulkan/shaders/shader_color_pos_lum.hpp b/src/ruis/render/vulkan/shaders/shader_color_pos_lum.hpp index b203513..f90b951 100644 --- a/src/ruis/render/vulkan/shaders/shader_color_pos_lum.hpp +++ b/src/ruis/render/vulkan/shaders/shader_color_pos_lum.hpp @@ -31,13 +31,10 @@ namespace ruis::render::vulkan { class shader_color_pos_lum : public ruis::render::coloring_shader { public: - shader_color_pos_lum(const utki::shared_ref& rendering_context); + shader_color_pos_lum(utki::shared_ref factory); void render(const r4::matrix4& m, const ruis::render::vertex_array& va, const r4::vector4& color) const override; - -private: - const utki::shared_ref ctx_ref; }; } // namespace ruis::render::vulkan diff --git a/src/ruis/render/vulkan/shaders/shader_color_pos_tex.cpp b/src/ruis/render/vulkan/shaders/shader_color_pos_tex.cpp index 8db8e62..fca1cbf 100644 --- a/src/ruis/render/vulkan/shaders/shader_color_pos_tex.cpp +++ b/src/ruis/render/vulkan/shaders/shader_color_pos_tex.cpp @@ -29,9 +29,8 @@ along with this program. If not, see . using namespace ruis::render::vulkan; -shader_color_pos_tex::shader_color_pos_tex(const utki::shared_ref& rendering_context) : - ruis::render::coloring_texturing_shader(rendering_context), - ctx_ref(rendering_context) +shader_color_pos_tex::shader_color_pos_tex(utki::shared_ref factory) : + ruis::render::coloring_texturing_shader(std::move(factory)) {} void shader_color_pos_tex::render( @@ -46,7 +45,7 @@ void shader_color_pos_tex::render( const auto& vk_tex = static_cast(tex); vulkan_shader_render( - this->ctx_ref, // + this->factory, // shader_kind::color_pos_tex, std::data(spirv_shader_color_pos_tex_vertex), spirv_shader_color_pos_tex_vertex_size, diff --git a/src/ruis/render/vulkan/shaders/shader_color_pos_tex.hpp b/src/ruis/render/vulkan/shaders/shader_color_pos_tex.hpp index cf6976d..863014c 100644 --- a/src/ruis/render/vulkan/shaders/shader_color_pos_tex.hpp +++ b/src/ruis/render/vulkan/shaders/shader_color_pos_tex.hpp @@ -31,7 +31,7 @@ namespace ruis::render::vulkan { class shader_color_pos_tex : public ruis::render::coloring_texturing_shader { public: - shader_color_pos_tex(const utki::shared_ref& rendering_context); + shader_color_pos_tex(utki::shared_ref factory); void render( const r4::matrix4& m, @@ -39,9 +39,6 @@ class shader_color_pos_tex : public ruis::render::coloring_texturing_shader const r4::vector4& color, const ruis::render::texture_2d& tex ) const override; - -private: - const utki::shared_ref ctx_ref; }; } // namespace ruis::render::vulkan diff --git a/src/ruis/render/vulkan/shaders/shader_color_pos_tex_alpha.cpp b/src/ruis/render/vulkan/shaders/shader_color_pos_tex_alpha.cpp index b76da23..d2b74e5 100644 --- a/src/ruis/render/vulkan/shaders/shader_color_pos_tex_alpha.cpp +++ b/src/ruis/render/vulkan/shaders/shader_color_pos_tex_alpha.cpp @@ -29,11 +29,8 @@ along with this program. If not, see . using namespace ruis::render::vulkan; -shader_color_pos_tex_alpha::shader_color_pos_tex_alpha( - const utki::shared_ref& rendering_context -) : - ruis::render::coloring_texturing_shader(rendering_context), - ctx_ref(rendering_context) +shader_color_pos_tex_alpha::shader_color_pos_tex_alpha(utki::shared_ref factory) : + ruis::render::coloring_texturing_shader(std::move(factory)) {} void shader_color_pos_tex_alpha::render( @@ -48,7 +45,7 @@ void shader_color_pos_tex_alpha::render( const auto& vk_tex = static_cast(tex); vulkan_shader_render( - this->ctx_ref, // + this->factory, // shader_kind::color_pos_tex_alpha, std::data(spirv_shader_color_pos_tex_alpha_vertex), spirv_shader_color_pos_tex_alpha_vertex_size, diff --git a/src/ruis/render/vulkan/shaders/shader_color_pos_tex_alpha.hpp b/src/ruis/render/vulkan/shaders/shader_color_pos_tex_alpha.hpp index f12960e..c1fbd2c 100644 --- a/src/ruis/render/vulkan/shaders/shader_color_pos_tex_alpha.hpp +++ b/src/ruis/render/vulkan/shaders/shader_color_pos_tex_alpha.hpp @@ -31,7 +31,7 @@ namespace ruis::render::vulkan { class shader_color_pos_tex_alpha : public ruis::render::coloring_texturing_shader { public: - shader_color_pos_tex_alpha(const utki::shared_ref& rendering_context); + shader_color_pos_tex_alpha(utki::shared_ref factory); void render( const r4::matrix4& m, @@ -39,9 +39,6 @@ class shader_color_pos_tex_alpha : public ruis::render::coloring_texturing_shade const r4::vector4& color, const ruis::render::texture_2d& tex ) const override; - -private: - const utki::shared_ref ctx_ref; }; } // namespace ruis::render::vulkan diff --git a/src/ruis/render/vulkan/shaders/shader_common.cpp b/src/ruis/render/vulkan/shaders/shader_common.cpp index 78a1fcb..3d6f360 100644 --- a/src/ruis/render/vulkan/shaders/shader_common.cpp +++ b/src/ruis/render/vulkan/shaders/shader_common.cpp @@ -31,7 +31,7 @@ along with this program. If not, see . namespace ruis::render::vulkan { void vulkan_shader_render( - const utki::shared_ref& rendering_context, // + const utki::shared_ref& factory, // NOLINT (unused: render uses context::current()) shader_kind kind, const void* vertex_spirv, size_t vertex_spirv_len, @@ -43,6 +43,8 @@ void vulkan_shader_render( const ruis::render::vertex_array& va ) { + (void)factory; + // use the currently active context (set by ruis::render::context::apply()), // not the context stored in the shader (which may be the shared resource-loader context). auto& ctx = @@ -108,8 +110,7 @@ void vulkan_shader_render( ctx.draw_indexed( pipeline, - textured ? ctx.vu_factory.get().get_pipeline_layout_with_tex() - : ctx.vu_factory.get().get_pipeline_layout_no_tex(), + textured ? ctx.factory.get().get_pipeline_layout_with_tex() : ctx.factory.get().get_pipeline_layout_no_tex(), va, descriptor_set ); diff --git a/src/ruis/render/vulkan/shaders/shader_common.hpp b/src/ruis/render/vulkan/shaders/shader_common.hpp index 26a375e..e5eab8c 100644 --- a/src/ruis/render/vulkan/shaders/shader_common.hpp +++ b/src/ruis/render/vulkan/shaders/shader_common.hpp @@ -69,7 +69,7 @@ class texture_2d; * may be different from the context currently rendering) before the draw. */ void vulkan_shader_render( - const utki::shared_ref& rendering_context, // + const utki::shared_ref& factory, // NOLINT (unused: render uses context::current()) shader_kind kind, const void* vertex_spirv, size_t vertex_spirv_len, diff --git a/src/ruis/render/vulkan/shaders/shader_pos_clr.cpp b/src/ruis/render/vulkan/shaders/shader_pos_clr.cpp index 9c47bbd..1340c8b 100644 --- a/src/ruis/render/vulkan/shaders/shader_pos_clr.cpp +++ b/src/ruis/render/vulkan/shaders/shader_pos_clr.cpp @@ -27,15 +27,14 @@ along with this program. If not, see . using namespace ruis::render::vulkan; -shader_pos_clr::shader_pos_clr(const utki::shared_ref& rendering_context) : - ruis::render::shader(rendering_context), - ctx_ref(rendering_context) +shader_pos_clr::shader_pos_clr(utki::shared_ref factory) : + ruis::render::shader(std::move(factory)) {} void shader_pos_clr::render(const r4::matrix4& m, const ruis::render::vertex_array& va) const { vulkan_shader_render( - this->ctx_ref, // + this->factory, // shader_kind::pos_clr, std::data(spirv_shader_pos_clr_vertex), spirv_shader_pos_clr_vertex_size, diff --git a/src/ruis/render/vulkan/shaders/shader_pos_clr.hpp b/src/ruis/render/vulkan/shaders/shader_pos_clr.hpp index ced342f..eb9ba3e 100644 --- a/src/ruis/render/vulkan/shaders/shader_pos_clr.hpp +++ b/src/ruis/render/vulkan/shaders/shader_pos_clr.hpp @@ -31,12 +31,9 @@ namespace ruis::render::vulkan { class shader_pos_clr : public ruis::render::shader { public: - shader_pos_clr(const utki::shared_ref& rendering_context); + shader_pos_clr(utki::shared_ref factory); void render(const r4::matrix4& m, const ruis::render::vertex_array& va) const override; - -private: - const utki::shared_ref ctx_ref; }; } // namespace ruis::render::vulkan diff --git a/src/ruis/render/vulkan/shaders/shader_pos_tex.cpp b/src/ruis/render/vulkan/shaders/shader_pos_tex.cpp index a790812..2ac9d94 100644 --- a/src/ruis/render/vulkan/shaders/shader_pos_tex.cpp +++ b/src/ruis/render/vulkan/shaders/shader_pos_tex.cpp @@ -29,9 +29,8 @@ along with this program. If not, see . using namespace ruis::render::vulkan; -shader_pos_tex::shader_pos_tex(const utki::shared_ref& rendering_context) : - ruis::render::texturing_shader(rendering_context), - ctx_ref(rendering_context) +shader_pos_tex::shader_pos_tex(utki::shared_ref factory) : + ruis::render::texturing_shader(std::move(factory)) {} void shader_pos_tex::render( @@ -61,7 +60,7 @@ void shader_pos_tex::render( } vulkan_shader_render( - this->ctx_ref, // + this->factory, // shader_kind::pos_tex, std::data(spirv_shader_pos_tex_vertex), spirv_shader_pos_tex_vertex_size, diff --git a/src/ruis/render/vulkan/shaders/shader_pos_tex.hpp b/src/ruis/render/vulkan/shaders/shader_pos_tex.hpp index 627d193..039aad6 100644 --- a/src/ruis/render/vulkan/shaders/shader_pos_tex.hpp +++ b/src/ruis/render/vulkan/shaders/shader_pos_tex.hpp @@ -31,13 +31,10 @@ namespace ruis::render::vulkan { class shader_pos_tex : public ruis::render::texturing_shader { public: - shader_pos_tex(const utki::shared_ref& rendering_context); + shader_pos_tex(utki::shared_ref factory); void render(const r4::matrix4& m, const ruis::render::vertex_array& va, const ruis::render::texture_2d& tex) const override; - -private: - const utki::shared_ref ctx_ref; }; } // namespace ruis::render::vulkan diff --git a/src/ruis/render/vulkan/texture_2d.cpp b/src/ruis/render/vulkan/texture_2d.cpp index 63204a9..2b4bec6 100644 --- a/src/ruis/render/vulkan/texture_2d.cpp +++ b/src/ruis/render/vulkan/texture_2d.cpp @@ -92,7 +92,7 @@ void destroy_staging_buffer(VkDevice device, VkBuffer buffer, VkDeviceMemory mem } // namespace texture_2d::texture_2d( - const utki::shared_ref& rendering_context, // + utki::shared_ref factory, // rasterimage::format type, rasterimage::dimensioned::dimensions_type dims, const uint8_t* data, @@ -100,22 +100,19 @@ texture_2d::texture_2d( ruis::render::factory::texture_2d_parameters params ) : ruis::render::texture_2d( - rendering_context, // + std::move(factory), // dims ), format(to_vk_format_8bit(type)), // - ri_format(type), // - ctx_ref(rendering_context) + ri_format(type) // { this->create_image(dims); this->create_sampler(params); - // register with the context for deferred upload tracking - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& vk_ctx = const_cast( - static_cast(rendering_context.get()) - ); - vk_ctx.all_textures.push_back(this); + // register with the factory for deferred upload tracking + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the factory is owned by the renderer; the base-class reference comes from the ruis API") + const auto& vk_ctx = static_cast(this->factory.get()); + vk_ctx.register_texture(this); if (data != nullptr && data_size != 0) { // defer the upload until the swapchain is ready @@ -247,10 +244,8 @@ void texture_2d::ensure_uploaded() if (this->deferred_data.empty()) { // no data to upload: clear the image to transparent so that // undefined GPU memory does not produce garbage pixels - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& ctx = const_cast( - static_cast(this->ctx_ref.get()) - ); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the factory is owned by the renderer; the base-class reference comes from the ruis API") + const auto& ctx = static_cast(this->factory.get()); ctx.one_shot([&](VkCommandBuffer cmd) { const auto range = VkImageSubresourceRange{ .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, @@ -320,18 +315,10 @@ void texture_2d::ensure_uploaded() texture_2d::~texture_2d() { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& ctx = - const_cast(static_cast(this->ctx_ref.get() - )); - // remove from the context's texture list - auto& tex_list = ctx.all_textures; - for (auto it = tex_list.begin(); it != tex_list.end(); ++it) { - if (*it == this) { - tex_list.erase(it); - break; - } - } + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the factory is owned by the renderer; the base-class reference comes from the ruis API") + const auto& ctx = static_cast(this->factory.get()); + // remove from the factory's texture list + ctx.unregister_texture(this); vkDestroySampler(ctx.device, this->sampler, nullptr); vkDestroyImageView(ctx.device, this->image_view, nullptr); vkDestroyImage(ctx.device, this->image, nullptr); @@ -342,10 +329,8 @@ texture_2d::~texture_2d() void texture_2d::create_image(rasterimage::dimensioned::dimensions_type dims) { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& ctx = - const_cast(static_cast(this->ctx_ref.get() - )); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the factory is owned by the renderer; the base-class reference comes from the ruis API") + const auto& ctx = static_cast(this->factory.get()); VkImageCreateInfo image_info{}; image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; @@ -394,11 +379,8 @@ void texture_2d::create_image(rasterimage::dimensioned::dimensions_type dims) void texture_2d::create_sampler(ruis::render::factory::texture_2d_parameters params) { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& ctx = - const_cast(static_cast(this->ctx_ref.get() - )); - (void)ctx; + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the factory is owned by the renderer; the base-class reference comes from the ruis API") + const auto& ctx = static_cast(this->factory.get()); const auto sampler_info = VkSamplerCreateInfo{ .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, @@ -425,10 +407,8 @@ void texture_2d::create_sampler(ruis::render::factory::texture_2d_parameters par void texture_2d::upload_data(const uint8_t* data, size_t data_size) { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-type-static-cast-downcast, "the context is owned by the renderer; the const qualifier comes from the shared_ref API") - auto& ctx = - const_cast(static_cast(this->ctx_ref.get() - )); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the factory is owned by the renderer; the base-class reference comes from the ruis API") + const auto& ctx = static_cast(this->factory.get()); VkBuffer staging_buffer = VK_NULL_HANDLE; VkDeviceMemory staging_memory = VK_NULL_HANDLE; diff --git a/src/ruis/render/vulkan/texture_2d.hpp b/src/ruis/render/vulkan/texture_2d.hpp index 9207dc3..ed0f60a 100644 --- a/src/ruis/render/vulkan/texture_2d.hpp +++ b/src/ruis/render/vulkan/texture_2d.hpp @@ -41,7 +41,7 @@ class texture_2d : public ruis::render::texture_2d rasterimage::format ri_format = rasterimage::format::rgba; texture_2d( - const utki::shared_ref& rendering_context, // + utki::shared_ref factory, // rasterimage::format type, rasterimage::dimensioned::dimensions_type dims, const uint8_t* data, @@ -60,7 +60,6 @@ class texture_2d : public ruis::render::texture_2d ~texture_2d() override; private: - const utki::shared_ref ctx_ref; void create_image(rasterimage::dimensioned::dimensions_type dims); void upload_data(const uint8_t* data, size_t data_size); void create_sampler(ruis::render::factory::texture_2d_parameters params); diff --git a/src/ruis/render/vulkan/texture_depth.cpp b/src/ruis/render/vulkan/texture_depth.cpp index ad2d288..4b351c9 100644 --- a/src/ruis/render/vulkan/texture_depth.cpp +++ b/src/ruis/render/vulkan/texture_depth.cpp @@ -26,11 +26,10 @@ along with this program. If not, see . using namespace ruis::render::vulkan; texture_depth::texture_depth( - const utki::shared_ref& rendering_context, // + utki::shared_ref factory, // r4::vector2 dims ) : - ruis::render::texture_depth(rendering_context, dims), - ctx_ref(rendering_context) + ruis::render::texture_depth(std::move(factory), dims) { this->create_image(); } @@ -38,7 +37,7 @@ texture_depth::texture_depth( texture_depth::~texture_depth() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer owns the object; the base-class reference comes from the ruis API") - auto& ctx = static_cast(this->ctx_ref.get()); + const auto& ctx = static_cast(this->factory.get()); vkDestroyImageView(ctx.device, this->image_view, nullptr); vkDestroyImage(ctx.device, this->image, nullptr); if (this->memory != VK_NULL_HANDLE) { @@ -49,7 +48,7 @@ texture_depth::~texture_depth() void texture_depth::create_image() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer owns the object; the base-class reference comes from the ruis API") - auto& ctx = static_cast(this->ctx_ref.get()); + const auto& ctx = static_cast(this->factory.get()); const auto image_info = VkImageCreateInfo{ .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, diff --git a/src/ruis/render/vulkan/texture_depth.hpp b/src/ruis/render/vulkan/texture_depth.hpp index caa2ac5..74b4770 100644 --- a/src/ruis/render/vulkan/texture_depth.hpp +++ b/src/ruis/render/vulkan/texture_depth.hpp @@ -36,7 +36,7 @@ class texture_depth : public ruis::render::texture_depth VkImageView image_view = VK_NULL_HANDLE; texture_depth( - const utki::shared_ref& rendering_context, // + utki::shared_ref factory, // r4::vector2 dims ); texture_depth(const texture_depth&) = delete; @@ -46,7 +46,6 @@ class texture_depth : public ruis::render::texture_depth ~texture_depth() override; private: - const utki::shared_ref ctx_ref; void create_image(); }; diff --git a/src/ruis/render/vulkan/vertex_array.cpp b/src/ruis/render/vulkan/vertex_array.cpp index 28bd0d4..1197422 100644 --- a/src/ruis/render/vulkan/vertex_array.cpp +++ b/src/ruis/render/vulkan/vertex_array.cpp @@ -28,12 +28,12 @@ along with this program. If not, see . using namespace ruis::render::vulkan; vertex_array::vertex_array( - const utki::shared_ref& rendering_context, // + utki::shared_ref factory, // buffers_type buffers, utki::shared_ref indices, mode rendering_mode ) : - ruis::render::vertex_array(rendering_context, std::move(buffers), std::move(indices), rendering_mode) + ruis::render::vertex_array(std::move(factory), std::move(buffers), std::move(indices), rendering_mode) { // all buffers must be Vulkan vertex buffers and the index buffer a Vulkan index buffer for (const auto& b : this->buffers) { diff --git a/src/ruis/render/vulkan/vertex_array.hpp b/src/ruis/render/vulkan/vertex_array.hpp index 19ee55d..f4e6524 100644 --- a/src/ruis/render/vulkan/vertex_array.hpp +++ b/src/ruis/render/vulkan/vertex_array.hpp @@ -36,7 +36,7 @@ class vertex_array : public ruis::render::vertex_array { public: vertex_array( - const utki::shared_ref& rendering_context, // + utki::shared_ref factory, // buffers_type buffers, utki::shared_ref indices, mode rendering_mode diff --git a/src/ruis/render/vulkan/vertex_buffer.cpp b/src/ruis/render/vulkan/vertex_buffer.cpp index de079dc..92b311d 100644 --- a/src/ruis/render/vulkan/vertex_buffer.cpp +++ b/src/ruis/render/vulkan/vertex_buffer.cpp @@ -28,11 +28,14 @@ along with this program. If not, see . using namespace ruis::render::vulkan; vertex_buffer::vertex_buffer( - const utki::shared_ref& rendering_context, // + utki::shared_ref factory, // std::vector&& padded_data ) : - ruis::render::vertex_buffer(rendering_context, padded_data.size() / factory::vertex_component_count), - ctx_ref(rendering_context) + // the parameter 'factory' shadows the type name, so the static member is fully qualified + ruis::render::vertex_buffer( + std::move(factory), + padded_data.size() / ruis::render::vulkan::factory::vertex_component_count + ) { this->create_vk_buffer(std::move(padded_data)); } @@ -40,7 +43,7 @@ vertex_buffer::vertex_buffer( vertex_buffer::~vertex_buffer() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer owns the object; the base-class reference comes from the ruis API") - auto& ctx = static_cast(this->ctx_ref.get()); + const auto& ctx = static_cast(this->factory.get()); if (this->buffer != VK_NULL_HANDLE) { vkDestroyBuffer(ctx.device, this->buffer, nullptr); vkFreeMemory(ctx.device, this->memory, nullptr); @@ -57,7 +60,7 @@ void vertex_buffer::create_vk_buffer( } // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the renderer owns the object; the base-class reference comes from the ruis API") - auto& ctx = static_cast(this->ctx_ref.get()); + const auto& ctx = static_cast(this->factory.get()); this->byte_size = uint32_t(data.size() * sizeof(float)); diff --git a/src/ruis/render/vulkan/vertex_buffer.hpp b/src/ruis/render/vulkan/vertex_buffer.hpp index 8b4e1ec..14dd214 100644 --- a/src/ruis/render/vulkan/vertex_buffer.hpp +++ b/src/ruis/render/vulkan/vertex_buffer.hpp @@ -53,7 +53,7 @@ class vertex_buffer : public ruis::render::vertex_buffer uint32_t byte_size = 0; vertex_buffer( - const utki::shared_ref& rendering_context, // + utki::shared_ref factory, // std::vector&& padded_data ); @@ -65,7 +65,6 @@ class vertex_buffer : public ruis::render::vertex_buffer ~vertex_buffer() override; private: - const utki::shared_ref ctx_ref; void create_vk_buffer(std::vector&& data); }; From 678c8a5f8e7eb843c7600271ad414dc4bd8a022e Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Thu, 19 Nov 2026 20:33:24 +0200 Subject: [PATCH 05/12] factory: remove the unused native_window constructor parameter The factory obtains all GPU handles from the surface provider and never uses the native window, so the parameter was dead weight. Drop it. --- src/ruis/render/vulkan/factory.cpp | 7 +------ src/ruis/render/vulkan/factory.hpp | 5 +---- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/ruis/render/vulkan/factory.cpp b/src/ruis/render/vulkan/factory.cpp index cd465f5..4aed048 100644 --- a/src/ruis/render/vulkan/factory.cpp +++ b/src/ruis/render/vulkan/factory.cpp @@ -38,17 +38,12 @@ along with this program. If not, see . using namespace ruis::render::vulkan; -factory::factory( - const utki::shared_ref& native_window, // - const utki::shared_ref& surface_provider -) : +factory::factory(const utki::shared_ref& surface_provider) : // device(surface_provider.get().get_device()), // physical_device(surface_provider.get().get_physical_device()), // queue(surface_provider.get().get_queue()), // instance(surface_provider.get().get_instance()) { - (void)native_window; - // ---- one-shot command submission resources (shared: used for texture uploads) ---- { const auto pool_info = VkCommandPoolCreateInfo{ diff --git a/src/ruis/render/vulkan/factory.hpp b/src/ruis/render/vulkan/factory.hpp index b3a49a0..fa18b4b 100644 --- a/src/ruis/render/vulkan/factory.hpp +++ b/src/ruis/render/vulkan/factory.hpp @@ -71,10 +71,7 @@ class factory : public ruis::render::factory VkQueue queue = VK_NULL_HANDLE; VkInstance instance = VK_NULL_HANDLE; - factory( - const utki::shared_ref& native_window, // - const utki::shared_ref& surface_provider - ); + factory(const utki::shared_ref& surface_provider); ~factory() override; // noncopyable: owns the shared GPU resources From 7b853927fb0d1683401962db2ab9e560b0403761 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Fri, 20 Nov 2026 23:43:41 +0200 Subject: [PATCH 06/12] Rename surface_provider to surface, make vulkan_device ctor public - Rename surface_provider class to surface (file and class name) - Make vulkan_device constructor public (remove make() factory method) so callers can use make_shared directly - Rename VkSurfaceKHR member to vk_surface to avoid name clash with class --- src/ruis/render/vulkan/context.cpp | 30 ++++++++-------- src/ruis/render/vulkan/context.hpp | 8 ++--- src/ruis/render/vulkan/factory.cpp | 10 +++--- src/ruis/render/vulkan/factory.hpp | 6 ++-- .../{surface_provider.cpp => surface.cpp} | 22 ++++++------ .../{surface_provider.hpp => surface.hpp} | 20 +++++------ src/ruis/render/vulkan/vulkan_device.cpp | 34 +++++++++++-------- src/ruis/render/vulkan/vulkan_device.hpp | 9 ++--- 8 files changed, 69 insertions(+), 70 deletions(-) rename src/ruis/render/vulkan/{surface_provider.cpp => surface.cpp} (66%) rename src/ruis/render/vulkan/{surface_provider.hpp => surface.hpp} (84%) diff --git a/src/ruis/render/vulkan/context.cpp b/src/ruis/render/vulkan/context.cpp index 459a380..3a17b57 100644 --- a/src/ruis/render/vulkan/context.cpp +++ b/src/ruis/render/vulkan/context.cpp @@ -105,7 +105,7 @@ VkPresentModeKHR pick_present_mode(VkPhysicalDevice phys, VkSurfaceKHR surface, context::context( const utki::shared_ref& native_window, // - const utki::shared_ref& surface_provider, // + const utki::shared_ref& surface, // const utki::shared_ref& factory ) : ruis::render::context( @@ -128,12 +128,12 @@ context::context( }() ), factory(factory), // - physical_device(surface_provider.get().get_physical_device()), // - device(surface_provider.get().get_device()), // - surface_provider_ref(surface_provider), // - instance(surface_provider.get().get_instance()), // - queue(surface_provider.get().get_queue()), // - surface(surface_provider.get().get_surface()) + physical_device(surface.get().get_physical_device()), // + device(surface.get().get_device()), // + surface_ref(surface), // + instance(surface.get().get_instance()), // + queue(surface.get().get_queue()), // + vk_surface(surface.get().get_surface()) { (void)native_window; @@ -193,7 +193,7 @@ context::context( // ---- swapchain (only if we have a surface; headless contexts skip this) ---- // must come before create_render_passes() which uses swapchain_format - if (this->surface_provider_ref.get().has_surface()) { + if (this->surface_ref.get().has_surface()) { this->create_swapchain(); } @@ -201,22 +201,22 @@ context::context( this->create_render_passes(); // ---- swapchain framebuffers ---- - if (this->surface_provider_ref.get().has_surface()) { + if (this->surface_ref.get().has_surface()) { this->create_swapchain_framebuffers(); } } void context::create_swapchain() { - utki::assert(this->surface != VK_NULL_HANDLE, SL); + utki::assert(this->vk_surface != VK_NULL_HANDLE, SL); const auto dims = this->native_window.get().get_dims(); this->window_dims = r4::vector2(std::max(1, dims.x()), std::max(1, dims.y())); - const auto surface_format = pick_surface_format(this->physical_device, this->surface); + const auto surface_format = pick_surface_format(this->physical_device, this->vk_surface); this->swapchain_format = surface_format.format; VkSurfaceCapabilitiesKHR caps{}; - VU_CHECK(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(this->physical_device, this->surface, &caps)); + VU_CHECK(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(this->physical_device, this->vk_surface, &caps)); // clamp the dimensions to the supported range uint32_t width = this->window_dims.x(); @@ -241,13 +241,13 @@ void context::create_swapchain() const auto vsync_enabled = this->is_vsync_enabled(); this->swapchain_vsync = vsync_enabled; - const auto present_mode = pick_present_mode(this->physical_device, this->surface, vsync_enabled); + const auto present_mode = pick_present_mode(this->physical_device, this->vk_surface, vsync_enabled); const uint32_t queue_family = 0; const auto info = VkSwapchainCreateInfoKHR{ .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, - .surface = this->surface, + .surface = this->vk_surface, .minImageCount = image_count, .imageFormat = this->swapchain_format, .imageColorSpace = surface_format.colorSpace, @@ -301,7 +301,7 @@ void context::create_swapchain() void context::ensure_swapchain_valid() { // headless contexts (e.g. resource loading) have no surface; nothing to do - if (!this->surface_provider_ref.get().has_surface()) { + if (!this->surface_ref.get().has_surface()) { return; } // use get_dims() which accounts for the HiDPI scale factor diff --git a/src/ruis/render/vulkan/context.hpp b/src/ruis/render/vulkan/context.hpp index c05b698..1b581a7 100644 --- a/src/ruis/render/vulkan/context.hpp +++ b/src/ruis/render/vulkan/context.hpp @@ -32,7 +32,7 @@ along with this program. If not, see . #include "shaders/shader_common.hpp" #include "factory.hpp" -#include "surface_provider.hpp" +#include "surface.hpp" #include "vulkan.hpp" namespace ruis::render::vulkan { @@ -89,7 +89,7 @@ class context : public ruis::render::context public: context( const utki::shared_ref& native_window, // - const utki::shared_ref& surface_provider, // + const utki::shared_ref& surface, // const utki::shared_ref& factory ); ~context() override; @@ -202,11 +202,11 @@ class context : public ruis::render::context private: // keep the surface provider alive for the lifetime of the context // (the surface must outlive the swapchain) - utki::shared_ref surface_provider_ref; + utki::shared_ref surface_ref; VkInstance instance = VK_NULL_HANDLE; VkQueue queue = VK_NULL_HANDLE; - VkSurfaceKHR surface = VK_NULL_HANDLE; + VkSurfaceKHR vk_surface = VK_NULL_HANDLE; VkPhysicalDeviceProperties device_properties{}; uint32_t graphics_queue_family = 0; diff --git a/src/ruis/render/vulkan/factory.cpp b/src/ruis/render/vulkan/factory.cpp index 4aed048..c62bd99 100644 --- a/src/ruis/render/vulkan/factory.cpp +++ b/src/ruis/render/vulkan/factory.cpp @@ -38,11 +38,11 @@ along with this program. If not, see . using namespace ruis::render::vulkan; -factory::factory(const utki::shared_ref& surface_provider) : // - device(surface_provider.get().get_device()), // - physical_device(surface_provider.get().get_physical_device()), // - queue(surface_provider.get().get_queue()), // - instance(surface_provider.get().get_instance()) +factory::factory(const utki::shared_ref& surface) : // + device(surface.get().get_device()), // + physical_device(surface.get().get_physical_device()), // + queue(surface.get().get_queue()), // + instance(surface.get().get_instance()) { // ---- one-shot command submission resources (shared: used for texture uploads) ---- { diff --git a/src/ruis/render/vulkan/factory.hpp b/src/ruis/render/vulkan/factory.hpp index fa18b4b..dfd9658 100644 --- a/src/ruis/render/vulkan/factory.hpp +++ b/src/ruis/render/vulkan/factory.hpp @@ -28,7 +28,7 @@ along with this program. If not, see . #include #include -#include "surface_provider.hpp" +#include "surface.hpp" #include "vulkan.hpp" namespace ruis::render::vulkan { @@ -56,7 +56,7 @@ class index_buffer; * objects (textures, vertex/index buffers, vertex arrays, shaders). * * A single factory is shared across all windows of an application: it wraps the - * shared @ref vulkan_device (via the surface_provider) so that GPU objects created + * shared @ref vulkan_device (via the surface) so that GPU objects created * for one window (e.g. the common white texture and quad vertex buffers) can be * used by any other window. */ @@ -71,7 +71,7 @@ class factory : public ruis::render::factory VkQueue queue = VK_NULL_HANDLE; VkInstance instance = VK_NULL_HANDLE; - factory(const utki::shared_ref& surface_provider); + factory(const utki::shared_ref& surface); ~factory() override; // noncopyable: owns the shared GPU resources diff --git a/src/ruis/render/vulkan/surface_provider.cpp b/src/ruis/render/vulkan/surface.cpp similarity index 66% rename from src/ruis/render/vulkan/surface_provider.cpp rename to src/ruis/render/vulkan/surface.cpp index 4d43d9e..959938e 100644 --- a/src/ruis/render/vulkan/surface_provider.cpp +++ b/src/ruis/render/vulkan/surface.cpp @@ -19,41 +19,41 @@ along with this program. If not, see . /* ================ LICENSE END ================ */ -#include "surface_provider.hpp" +#include "surface.hpp" #include "vulkan_device.hpp" namespace ruis::render::vulkan { -surface_provider::surface_provider(const vulkan_device& device, VkSurfaceKHR surface) : +surface::surface(const vulkan_device& device, VkSurfaceKHR surface) : device(&device), - surface(surface) + vk_surface(surface) {} -surface_provider::~surface_provider() +surface::~surface() { - if (this->surface != VK_NULL_HANDLE) { - vkDestroySurfaceKHR(this->device->get_instance(), this->surface, nullptr); - this->surface = VK_NULL_HANDLE; + if (this->vk_surface != VK_NULL_HANDLE) { + vkDestroySurfaceKHR(this->device->get_instance(), this->vk_surface, nullptr); + this->vk_surface = VK_NULL_HANDLE; } } -VkInstance surface_provider::get_instance() const +VkInstance surface::get_instance() const { return this->device->get_instance(); } -VkPhysicalDevice surface_provider::get_physical_device() const +VkPhysicalDevice surface::get_physical_device() const { return this->device->get_physical_device(); } -VkDevice surface_provider::get_device() const +VkDevice surface::get_device() const { return this->device->get_device(); } -VkQueue surface_provider::get_queue() const +VkQueue surface::get_queue() const { return this->device->get_queue(); } diff --git a/src/ruis/render/vulkan/surface_provider.hpp b/src/ruis/render/vulkan/surface.hpp similarity index 84% rename from src/ruis/render/vulkan/surface_provider.hpp rename to src/ruis/render/vulkan/surface.hpp index e14090e..36a3668 100644 --- a/src/ruis/render/vulkan/surface_provider.hpp +++ b/src/ruis/render/vulkan/surface.hpp @@ -41,16 +41,16 @@ class vulkan_device; * provider holds a reference to the shared vulkan_device and the window's * surface. It owns the surface (destroying it in the destructor). */ -class surface_provider +class surface { public: - surface_provider(const vulkan_device& device, VkSurfaceKHR surface); - ~surface_provider(); + surface(const vulkan_device& device, VkSurfaceKHR surface); + ~surface(); - surface_provider(const surface_provider&) = delete; - surface_provider& operator=(const surface_provider&) = delete; - surface_provider(surface_provider&&) = delete; - surface_provider& operator=(surface_provider&&) = delete; + surface(const surface&) = delete; + surface& operator=(const surface&) = delete; + surface(surface&&) = delete; + surface& operator=(surface&&) = delete; /** * @brief The shared Vulkan instance. @@ -78,7 +78,7 @@ class surface_provider */ VkSurfaceKHR get_surface() const { - return this->surface; + return this->vk_surface; } /** @@ -86,12 +86,12 @@ class surface_provider */ bool has_surface() const { - return this->surface != VK_NULL_HANDLE; + return this->vk_surface != VK_NULL_HANDLE; } private: const vulkan_device* device; - VkSurfaceKHR surface; + VkSurfaceKHR vk_surface; }; } // namespace ruis::render::vulkan diff --git a/src/ruis/render/vulkan/vulkan_device.cpp b/src/ruis/render/vulkan/vulkan_device.cpp index 8939c6b..507803b 100644 --- a/src/ruis/render/vulkan/vulkan_device.cpp +++ b/src/ruis/render/vulkan/vulkan_device.cpp @@ -78,30 +78,36 @@ vulkan_device::~vulkan_device() } } -std::unique_ptr vulkan_device::make( - std::vector instance_extensions, - std::vector device_extensions -) +vulkan_device::vulkan_device(std::vector instance_extensions, std::vector device_extensions) : + instance(VK_NULL_HANDLE), + physical_device(VK_NULL_HANDLE), + device(VK_NULL_HANDLE), + queue(VK_NULL_HANDLE), + graphics_queue_family(0), + owns_instance(false) { if (device_extensions.empty()) { device_extensions.emplace_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); } - auto ret = std::unique_ptr(new vulkan_device()); - - create_instance_and_physical_device(instance_extensions, ret->instance, ret->physical_device, ret->owns_instance); + create_instance_and_physical_device( + instance_extensions, + this->instance, + this->physical_device, + this->owns_instance + ); // find the graphics queue family std::vector families; uint32_t family_count = 0; - vkGetPhysicalDeviceQueueFamilyProperties(ret->physical_device, &family_count, nullptr); + vkGetPhysicalDeviceQueueFamilyProperties(this->physical_device, &family_count, nullptr); families.resize(family_count); - vkGetPhysicalDeviceQueueFamilyProperties(ret->physical_device, &family_count, families.data()); + vkGetPhysicalDeviceQueueFamilyProperties(this->physical_device, &family_count, families.data()); uint32_t graphics_family = 0; for (uint32_t i = 0; i < family_count; ++i) { if (families[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) { - ret->graphics_queue_family = i; + this->graphics_queue_family = i; break; } } @@ -129,12 +135,10 @@ std::unique_ptr vulkan_device::make( device_info.ppEnabledExtensionNames = c_device_extensions.data(); } - VU_CHECK(vkCreateDevice(ret->physical_device, &device_info, nullptr, &ret->device)); - - vkGetDeviceQueue(ret->device, graphics_family, 0, &ret->queue); - ret->graphics_queue_family = graphics_family; + VU_CHECK(vkCreateDevice(this->physical_device, &device_info, nullptr, &this->device)); - return ret; + vkGetDeviceQueue(this->device, graphics_family, 0, &this->queue); + this->graphics_queue_family = graphics_family; } } // namespace ruis::render::vulkan diff --git a/src/ruis/render/vulkan/vulkan_device.hpp b/src/ruis/render/vulkan/vulkan_device.hpp index 2abdbc5..3da0bb5 100644 --- a/src/ruis/render/vulkan/vulkan_device.hpp +++ b/src/ruis/render/vulkan/vulkan_device.hpp @@ -42,7 +42,7 @@ namespace ruis::render::vulkan { * The platform layer provides the instance extensions (e.g. VK_KHR_xlib_surface * or VK_KHR_wayland_surface) and the device extensions (e.g. VK_KHR_swapchain). * - * The vulkan_device must outlive all surface_providers that reference it. + * The vulkan_device must outlive all surfaces that reference it. */ class vulkan_device { @@ -55,10 +55,7 @@ class vulkan_device vulkan_device& operator=(vulkan_device&&) = delete; // create the Vulkan device - static std::unique_ptr make( - std::vector instance_extensions, - std::vector device_extensions - ); + vulkan_device(std::vector instance_extensions, std::vector device_extensions); VkInstance get_instance() const { @@ -86,8 +83,6 @@ class vulkan_device } private: - vulkan_device() = default; - VkInstance instance = VK_NULL_HANDLE; VkPhysicalDevice physical_device = VK_NULL_HANDLE; VkDevice device = VK_NULL_HANDLE; From 9dce36046afefbe7cffa303dc764c84e2ee3b7b2 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Sat, 21 Nov 2026 00:15:08 +0200 Subject: [PATCH 07/12] Rename vulkan_device to device - Rename vulkan_device class to device (file and class name) - Rename VkDevice member to vk_device to avoid name clash with class - Rename surface member 'const device* device' to 'vu_device' --- .../vulkan/{vulkan_device.cpp => device.cpp} | 18 +++++++-------- .../vulkan/{vulkan_device.hpp => device.hpp} | 22 +++++++++---------- src/ruis/render/vulkan/factory.hpp | 2 +- src/ruis/render/vulkan/surface.cpp | 16 +++++++------- src/ruis/render/vulkan/surface.hpp | 10 ++++----- 5 files changed, 34 insertions(+), 34 deletions(-) rename src/ruis/render/vulkan/{vulkan_device.cpp => device.cpp} (90%) rename src/ruis/render/vulkan/{vulkan_device.hpp => device.hpp} (78%) diff --git a/src/ruis/render/vulkan/vulkan_device.cpp b/src/ruis/render/vulkan/device.cpp similarity index 90% rename from src/ruis/render/vulkan/vulkan_device.cpp rename to src/ruis/render/vulkan/device.cpp index 507803b..85e22c6 100644 --- a/src/ruis/render/vulkan/vulkan_device.cpp +++ b/src/ruis/render/vulkan/device.cpp @@ -19,7 +19,7 @@ along with this program. If not, see . /* ================ LICENSE END ================ */ -#include "vulkan_device.hpp" +#include "device.hpp" namespace ruis::render::vulkan { @@ -66,11 +66,11 @@ void create_instance_and_physical_device( } } // namespace -vulkan_device::~vulkan_device() +device::~device() { - if (this->device != VK_NULL_HANDLE) { - vkDestroyDevice(this->device, nullptr); - this->device = VK_NULL_HANDLE; + if (this->vk_device != VK_NULL_HANDLE) { + vkDestroyDevice(this->vk_device, nullptr); + this->vk_device = VK_NULL_HANDLE; } if (this->owns_instance && this->instance != VK_NULL_HANDLE) { vkDestroyInstance(this->instance, nullptr); @@ -78,10 +78,10 @@ vulkan_device::~vulkan_device() } } -vulkan_device::vulkan_device(std::vector instance_extensions, std::vector device_extensions) : +device::device(std::vector instance_extensions, std::vector device_extensions) : instance(VK_NULL_HANDLE), physical_device(VK_NULL_HANDLE), - device(VK_NULL_HANDLE), + vk_device(VK_NULL_HANDLE), queue(VK_NULL_HANDLE), graphics_queue_family(0), owns_instance(false) @@ -135,9 +135,9 @@ vulkan_device::vulkan_device(std::vector instance_extensions, std:: device_info.ppEnabledExtensionNames = c_device_extensions.data(); } - VU_CHECK(vkCreateDevice(this->physical_device, &device_info, nullptr, &this->device)); + VU_CHECK(vkCreateDevice(this->physical_device, &device_info, nullptr, &this->vk_device)); - vkGetDeviceQueue(this->device, graphics_family, 0, &this->queue); + vkGetDeviceQueue(this->vk_device, graphics_family, 0, &this->queue); this->graphics_queue_family = graphics_family; } diff --git a/src/ruis/render/vulkan/vulkan_device.hpp b/src/ruis/render/vulkan/device.hpp similarity index 78% rename from src/ruis/render/vulkan/vulkan_device.hpp rename to src/ruis/render/vulkan/device.hpp index 3da0bb5..0b23f65 100644 --- a/src/ruis/render/vulkan/vulkan_device.hpp +++ b/src/ruis/render/vulkan/device.hpp @@ -35,27 +35,27 @@ namespace ruis::render::vulkan { /** * @brief Owns the Vulkan instance, physical device and logical device. * - * A single vulkan_device is shared across all windows of an application. + * A single device is shared across all windows of an application. * Sharing one logical device lets GPU objects created for one window (e.g. the * common white texture and quad vertex buffers) be used by any other window. * * The platform layer provides the instance extensions (e.g. VK_KHR_xlib_surface * or VK_KHR_wayland_surface) and the device extensions (e.g. VK_KHR_swapchain). * - * The vulkan_device must outlive all surfaces that reference it. + * The device must outlive all surfaces that reference it. */ -class vulkan_device +class device { public: - ~vulkan_device(); + ~device(); - vulkan_device(const vulkan_device&) = delete; - vulkan_device& operator=(const vulkan_device&) = delete; - vulkan_device(vulkan_device&&) = delete; - vulkan_device& operator=(vulkan_device&&) = delete; + device(const device&) = delete; + device& operator=(const device&) = delete; + device(device&&) = delete; + device& operator=(device&&) = delete; // create the Vulkan device - vulkan_device(std::vector instance_extensions, std::vector device_extensions); + device(std::vector instance_extensions, std::vector device_extensions); VkInstance get_instance() const { @@ -69,7 +69,7 @@ class vulkan_device VkDevice get_device() const { - return this->device; + return this->vk_device; } VkQueue get_queue() const @@ -85,7 +85,7 @@ class vulkan_device private: VkInstance instance = VK_NULL_HANDLE; VkPhysicalDevice physical_device = VK_NULL_HANDLE; - VkDevice device = VK_NULL_HANDLE; + VkDevice vk_device = VK_NULL_HANDLE; VkQueue queue = VK_NULL_HANDLE; uint32_t graphics_queue_family = 0; bool owns_instance = false; diff --git a/src/ruis/render/vulkan/factory.hpp b/src/ruis/render/vulkan/factory.hpp index dfd9658..f36017e 100644 --- a/src/ruis/render/vulkan/factory.hpp +++ b/src/ruis/render/vulkan/factory.hpp @@ -56,7 +56,7 @@ class index_buffer; * objects (textures, vertex/index buffers, vertex arrays, shaders). * * A single factory is shared across all windows of an application: it wraps the - * shared @ref vulkan_device (via the surface) so that GPU objects created + * shared @ref device (via the surface) so that GPU objects created * for one window (e.g. the common white texture and quad vertex buffers) can be * used by any other window. */ diff --git a/src/ruis/render/vulkan/surface.cpp b/src/ruis/render/vulkan/surface.cpp index 959938e..3cb22b1 100644 --- a/src/ruis/render/vulkan/surface.cpp +++ b/src/ruis/render/vulkan/surface.cpp @@ -21,41 +21,41 @@ along with this program. If not, see . #include "surface.hpp" -#include "vulkan_device.hpp" +#include "device.hpp" namespace ruis::render::vulkan { -surface::surface(const vulkan_device& device, VkSurfaceKHR surface) : - device(&device), +surface::surface(const device& device, VkSurfaceKHR surface) : + vu_device(&device), vk_surface(surface) {} surface::~surface() { if (this->vk_surface != VK_NULL_HANDLE) { - vkDestroySurfaceKHR(this->device->get_instance(), this->vk_surface, nullptr); + vkDestroySurfaceKHR(this->vu_device->get_instance(), this->vk_surface, nullptr); this->vk_surface = VK_NULL_HANDLE; } } VkInstance surface::get_instance() const { - return this->device->get_instance(); + return this->vu_device->get_instance(); } VkPhysicalDevice surface::get_physical_device() const { - return this->device->get_physical_device(); + return this->vu_device->get_physical_device(); } VkDevice surface::get_device() const { - return this->device->get_device(); + return this->vu_device->get_device(); } VkQueue surface::get_queue() const { - return this->device->get_queue(); + return this->vu_device->get_queue(); } } // namespace ruis::render::vulkan diff --git a/src/ruis/render/vulkan/surface.hpp b/src/ruis/render/vulkan/surface.hpp index 36a3668..422e424 100644 --- a/src/ruis/render/vulkan/surface.hpp +++ b/src/ruis/render/vulkan/surface.hpp @@ -26,25 +26,25 @@ along with this program. If not, see . namespace ruis::render::vulkan { // forward declaration -class vulkan_device; +class device; /** * @brief Provides the Vulkan resources the renderer needs for a single window. * - * A single vulkan_device (instance, physical device, logical device and + * A single device (instance, physical device, logical device and * graphics queue) is shared across all windows of an application. Sharing one * logical device lets GPU objects created for one window (e.g. the common * white texture and quad vertex buffers) be used by any other window. * * The per-window VkSurfaceKHR is created by the platform layer (e.g. via the * VK_KHR_xlib_surface or VK_KHR_wayland_surface extension). This surface - * provider holds a reference to the shared vulkan_device and the window's + * provider holds a reference to the shared device and the window's * surface. It owns the surface (destroying it in the destructor). */ class surface { public: - surface(const vulkan_device& device, VkSurfaceKHR surface); + surface(const device& device, VkSurfaceKHR surface); ~surface(); surface(const surface&) = delete; @@ -90,7 +90,7 @@ class surface } private: - const vulkan_device* device; + const device* vu_device; VkSurfaceKHR vk_surface; }; From 5f5712d21dcba5440833f8f1a4261d4d3e576411 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Sat, 21 Nov 2026 00:39:27 +0200 Subject: [PATCH 08/12] Remove VU_CHECK macro, use check_vk_result directly with inline message --- src/ruis/render/vulkan/context.cpp | 78 ++++++++++++------------ src/ruis/render/vulkan/device.cpp | 8 +-- src/ruis/render/vulkan/factory.cpp | 32 +++++----- src/ruis/render/vulkan/frame_buffer.cpp | 4 +- src/ruis/render/vulkan/index_buffer.cpp | 8 +-- src/ruis/render/vulkan/texture_2d.cpp | 18 +++--- src/ruis/render/vulkan/texture_depth.cpp | 8 +-- src/ruis/render/vulkan/vertex_buffer.cpp | 8 +-- src/ruis/render/vulkan/vulkan.hpp | 3 - 9 files changed, 82 insertions(+), 85 deletions(-) diff --git a/src/ruis/render/vulkan/context.cpp b/src/ruis/render/vulkan/context.cpp index 3a17b57..c3499bf 100644 --- a/src/ruis/render/vulkan/context.cpp +++ b/src/ruis/render/vulkan/context.cpp @@ -147,7 +147,7 @@ context::context( .queueFamilyIndex = 0 }; { - VU_CHECK(vkCreateCommandPool(this->device, &pool_info, nullptr, &this->command_pool)); + check_vk_result(vkCreateCommandPool(this->device, &pool_info, nullptr, &this->command_pool), "vkCreateCommandPool"); } const auto alloc_info = VkCommandBufferAllocateInfo{ @@ -156,19 +156,19 @@ context::context( .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, .commandBufferCount = 1 }; - VU_CHECK(vkAllocateCommandBuffers(this->device, &alloc_info, &this->command_buffer)); + check_vk_result(vkAllocateCommandBuffers(this->device, &alloc_info, &this->command_buffer), "vkAllocateCommandBuffers"); } { const auto fence_info = VkFenceCreateInfo{.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO}; - VU_CHECK(vkCreateFence(this->device, &fence_info, nullptr, &this->frame_fence)); + check_vk_result(vkCreateFence(this->device, &fence_info, nullptr, &this->frame_fence), "vkCreateFence"); } { const auto sem_info = VkSemaphoreCreateInfo{.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO}; { - VU_CHECK(vkCreateSemaphore(this->device, &sem_info, nullptr, &this->image_available_sem)); + check_vk_result(vkCreateSemaphore(this->device, &sem_info, nullptr, &this->image_available_sem), "vkCreateSemaphore"); } { - VU_CHECK(vkCreateSemaphore(this->device, &sem_info, nullptr, &this->render_finished_sem)); + check_vk_result(vkCreateSemaphore(this->device, &sem_info, nullptr, &this->render_finished_sem), "vkCreateSemaphore"); } } { @@ -184,7 +184,7 @@ context::context( .pPoolSizes = pool_sizes.data() }; { - VU_CHECK(vkCreateDescriptorPool(this->device, &pool_info, nullptr, &this->descriptor_pool)); + check_vk_result(vkCreateDescriptorPool(this->device, &pool_info, nullptr, &this->descriptor_pool), "vkCreateDescriptorPool"); } } @@ -216,7 +216,7 @@ void context::create_swapchain() this->swapchain_format = surface_format.format; VkSurfaceCapabilitiesKHR caps{}; - VU_CHECK(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(this->physical_device, this->vk_surface, &caps)); + check_vk_result(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(this->physical_device, this->vk_surface, &caps), "vkGetPhysicalDeviceSurfaceCapabilitiesKHR"); // clamp the dimensions to the supported range uint32_t width = this->window_dims.x(); @@ -278,13 +278,13 @@ void context::create_swapchain() } { - VU_CHECK(vkCreateSwapchainKHR(this->device, &info, nullptr, &this->swapchain)); + check_vk_result(vkCreateSwapchainKHR(this->device, &info, nullptr, &this->swapchain), "vkCreateSwapchainKHR"); } uint32_t count = 0; - VU_CHECK(vkGetSwapchainImagesKHR(this->device, this->swapchain, &count, nullptr)); + check_vk_result(vkGetSwapchainImagesKHR(this->device, this->swapchain, &count, nullptr), "vkGetSwapchainImagesKHR"); this->swapchain_images.resize(count); - VU_CHECK(vkGetSwapchainImagesKHR(this->device, this->swapchain, &count, this->swapchain_images.data())); + check_vk_result(vkGetSwapchainImagesKHR(this->device, this->swapchain, &count, this->swapchain_images.data()), "vkGetSwapchainImagesKHR"); this->swapchain_image_count = count; this->window_dims = r4::vector2(width, height); @@ -317,7 +317,7 @@ void context::ensure_swapchain_valid() } // the window was resized (or the swapchain is suboptimal); wait for the GPU to // finish before recreating the swapchain - VU_CHECK(vkQueueWaitIdle(this->queue)); + check_vk_result(vkQueueWaitIdle(this->queue), "vkQueueWaitIdle"); // reset the frame state (the in-flight frame is discarded) this->frame_begun = false; this->render_pass_in_progress = false; @@ -357,7 +357,7 @@ void context::create_depth_image() .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED }; { - VU_CHECK(vkCreateImage(this->device, &image_info, nullptr, &this->depth_image)); + check_vk_result(vkCreateImage(this->device, &image_info, nullptr, &this->depth_image), "vkCreateImage"); } VkMemoryRequirements mem_reqs{}; @@ -367,8 +367,8 @@ void context::create_depth_image() .allocationSize = mem_reqs.size, .memoryTypeIndex = pick_device_local_memory_type(this->physical_device, mem_reqs) }; - VU_CHECK(vkAllocateMemory(this->device, &alloc_info, nullptr, &this->depth_image_memory)); - VU_CHECK(vkBindImageMemory(this->device, this->depth_image, this->depth_image_memory, 0)); + check_vk_result(vkAllocateMemory(this->device, &alloc_info, nullptr, &this->depth_image_memory), "vkAllocateMemory"); + check_vk_result(vkBindImageMemory(this->device, this->depth_image, this->depth_image_memory, 0), "vkBindImageMemory"); { const auto view_info = VkImageViewCreateInfo{ @@ -383,7 +383,7 @@ void context::create_depth_image() .baseArrayLayer = 0, .layerCount = 1} }; - VU_CHECK(vkCreateImageView(this->device, &view_info, nullptr, &this->depth_image_view)); + check_vk_result(vkCreateImageView(this->device, &view_info, nullptr, &this->depth_image_view), "vkCreateImageView"); } } @@ -458,7 +458,7 @@ void context::create_render_passes() }; { VkRenderPass render_pass{}; - VU_CHECK(vkCreateRenderPass(this->device, &info, nullptr, &render_pass)); + check_vk_result(vkCreateRenderPass(this->device, &info, nullptr, &render_pass), "vkCreateRenderPass"); return render_pass; } }; @@ -531,7 +531,7 @@ void context::create_swapchain_framebuffers() }; { VkImageView image_view{}; - VU_CHECK(vkCreateImageView(this->device, &view_info, nullptr, &image_view)); + check_vk_result(vkCreateImageView(this->device, &view_info, nullptr, &image_view), "vkCreateImageView"); this->swapchain_image_views[i] = image_view; } } @@ -551,7 +551,7 @@ void context::create_swapchain_framebuffers() }; { VkFramebuffer fb{}; - VU_CHECK(vkCreateFramebuffer(this->device, &fb_info, nullptr, &fb)); + check_vk_result(vkCreateFramebuffer(this->device, &fb_info, nullptr, &fb), "vkCreateFramebuffer"); this->swapchain_framebuffers[i] = fb; } } @@ -823,12 +823,12 @@ void context::ensure_frame_began() } // reset and begin the command buffer - VU_CHECK(vkResetCommandBuffer(this->command_buffer, 0)); + check_vk_result(vkResetCommandBuffer(this->command_buffer, 0), "vkResetCommandBuffer"); const auto begin_info = VkCommandBufferBeginInfo{ .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT }; - VU_CHECK(vkBeginCommandBuffer(this->command_buffer, &begin_info)); + check_vk_result(vkBeginCommandBuffer(this->command_buffer, &begin_info), "vkBeginCommandBuffer"); this->frame_begun = true; this->frame_has_swapchain = false; @@ -852,7 +852,7 @@ void context::allocate_ubo_slot() VkBuffer buf = VK_NULL_HANDLE; { VkBuffer buffer{}; - VU_CHECK(vkCreateBuffer(this->device, &buffer_info, nullptr, &buffer)); + check_vk_result(vkCreateBuffer(this->device, &buffer_info, nullptr, &buffer), "vkCreateBuffer"); buf = buffer; } VkMemoryRequirements mem_reqs{}; @@ -863,10 +863,10 @@ void context::allocate_ubo_slot() .memoryTypeIndex = pick_host_visible_memory_type(this->physical_device, mem_reqs) }; VkDeviceMemory mem = VK_NULL_HANDLE; - VU_CHECK(vkAllocateMemory(this->device, &alloc_info, nullptr, &mem)); - VU_CHECK(vkBindBufferMemory(this->device, buf, mem, 0)); + check_vk_result(vkAllocateMemory(this->device, &alloc_info, nullptr, &mem), "vkAllocateMemory"); + check_vk_result(vkBindBufferMemory(this->device, buf, mem, 0), "vkBindBufferMemory"); void* mapped = nullptr; - VU_CHECK(vkMapMemory(this->device, mem, 0, sizeof(draw_uniforms), 0, &mapped)); + check_vk_result(vkMapMemory(this->device, mem, 0, sizeof(draw_uniforms), 0, &mapped), "vkMapMemory"); this->ubo_ring.push_back(buf); this->ubo_ring_mem.push_back(mem); this->ubo_ring_map.push_back(mapped); @@ -906,7 +906,7 @@ VkDescriptorSet context::allocate_descriptor_set( .descriptorSetCount = 1, .pSetLayouts = &dsl }; - VU_CHECK(vkAllocateDescriptorSets(this->device, &alloc_info, &set)); + check_vk_result(vkAllocateDescriptorSets(this->device, &alloc_info, &set), "vkAllocateDescriptorSets"); this->frame_descriptor_sets.push_back(set); std::vector writes; @@ -1052,7 +1052,7 @@ rasterimage::image_variant context::readback_framebuffer_color(const ruis::rende .usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT, .sharingMode = VK_SHARING_MODE_EXCLUSIVE }; - VU_CHECK(vkCreateBuffer(this->device, &buffer_info, nullptr, &staging_buffer)); + check_vk_result(vkCreateBuffer(this->device, &buffer_info, nullptr, &staging_buffer), "vkCreateBuffer"); VkMemoryRequirements mem_reqs{}; vkGetBufferMemoryRequirements(this->device, staging_buffer, &mem_reqs); VkPhysicalDeviceMemoryProperties props{}; @@ -1068,8 +1068,8 @@ rasterimage::image_variant context::readback_framebuffer_color(const ruis::rende break; } } - VU_CHECK(vkAllocateMemory(this->device, &alloc_info, nullptr, &staging_memory)); - VU_CHECK(vkBindBufferMemory(this->device, staging_buffer, staging_memory, 0)); + check_vk_result(vkAllocateMemory(this->device, &alloc_info, nullptr, &staging_memory), "vkAllocateMemory"); + check_vk_result(vkBindBufferMemory(this->device, staging_buffer, staging_memory, 0), "vkBindBufferMemory"); } // append the readback to the frame's command buffer (the FBO render was recorded there) @@ -1140,10 +1140,10 @@ rasterimage::image_variant context::readback_framebuffer_color(const ruis::rende .signalSemaphoreCount = 0, .pSignalSemaphores = nullptr }; - VU_CHECK(vkEndCommandBuffer(this->command_buffer)); - VU_CHECK(vkQueueSubmit(this->queue, 1, &submit_info, VK_NULL_HANDLE)); + check_vk_result(vkEndCommandBuffer(this->command_buffer), "vkEndCommandBuffer"); + check_vk_result(vkQueueSubmit(this->queue, 1, &submit_info, VK_NULL_HANDLE), "vkQueueSubmit"); // wait for the GPU to finish using vkQueueWaitIdle (works on AMD driver) - VU_CHECK(vkQueueWaitIdle(this->queue)); + check_vk_result(vkQueueWaitIdle(this->queue), "vkQueueWaitIdle"); this->frame_submitted = false; this->frame_begun = false; this->frame_has_swapchain = false; @@ -1155,7 +1155,7 @@ rasterimage::image_variant context::readback_framebuffer_color(const ruis::rende // read the data back std::vector buf(total_bytes); void* mapped = nullptr; - VU_CHECK(vkMapMemory(this->device, staging_memory, 0, total_bytes, 0, &mapped)); + check_vk_result(vkMapMemory(this->device, staging_memory, 0, total_bytes, 0, &mapped), "vkMapMemory"); std::memcpy(buf.data(), mapped, total_bytes); vkUnmapMemory(this->device, staging_memory); @@ -1409,9 +1409,9 @@ VkPipeline context::build_pipeline( }; VkPipeline pipeline = VK_NULL_HANDLE; - VU_CHECK( + check_vk_result( vkCreateGraphicsPipelines(this->device, this->factory.get().get_pipeline_cache(), 1, &info, nullptr, &pipeline) - ); + , "vkCreateGraphicsPipelines"); // destroy the shader modules (they are now baked into the pipeline) vkDestroyShaderModule(this->device, vertex_stage.module, nullptr); @@ -1567,7 +1567,7 @@ void context::submit_frame(bool present) } // end the command buffer { - VU_CHECK(vkEndCommandBuffer(this->command_buffer)); + check_vk_result(vkEndCommandBuffer(this->command_buffer), "vkEndCommandBuffer"); } // build the submit info @@ -1588,7 +1588,7 @@ void context::submit_frame(bool present) .signalSemaphoreCount = present ? 1u : 0u, .pSignalSemaphores = present ? &this->render_finished_sem : nullptr }; - VU_CHECK(vkQueueSubmit(this->queue, 1, &submit_info, this->frame_fence)); + check_vk_result(vkQueueSubmit(this->queue, 1, &submit_info, this->frame_fence), "vkQueueSubmit"); this->frame_submitted = true; if (present) { @@ -1612,8 +1612,8 @@ void context::submit_frame(bool present) // wait for the frame to complete using vkQueueWaitIdle (safest for AMD driver) { - VU_CHECK(vkQueueWaitIdle(this->queue)); - VU_CHECK(vkWaitForFences(this->device, 1, &this->frame_fence, VK_TRUE, 1000000000ull)); // 1s timeout + check_vk_result(vkQueueWaitIdle(this->queue), "vkQueueWaitIdle"); + check_vk_result(vkWaitForFences(this->device, 1, &this->frame_fence, VK_TRUE, 1000000000ull), "vkWaitForFences"); // 1s timeout vkResetFences(this->device, 1, &this->frame_fence); this->frame_submitted = false; } @@ -1655,7 +1655,7 @@ VkShaderModule context::create_shader_module(const void* spirv, size_t size_word }; { VkShaderModule shader_module{}; - VU_CHECK(vkCreateShaderModule(this->device, &info, nullptr, &shader_module)); + check_vk_result(vkCreateShaderModule(this->device, &info, nullptr, &shader_module), "vkCreateShaderModule"); return shader_module; } } diff --git a/src/ruis/render/vulkan/device.cpp b/src/ruis/render/vulkan/device.cpp index 85e22c6..47a07d7 100644 --- a/src/ruis/render/vulkan/device.cpp +++ b/src/ruis/render/vulkan/device.cpp @@ -52,13 +52,13 @@ void create_instance_and_physical_device( info.ppEnabledExtensionNames = c_extensions.data(); } - VU_CHECK(vkCreateInstance(&info, nullptr, &instance)); + check_vk_result(vkCreateInstance(&info, nullptr, &instance), "vkCreateInstance"); owns_instance = true; uint32_t count = 0; - VU_CHECK(vkEnumeratePhysicalDevices(instance, &count, nullptr)); + check_vk_result(vkEnumeratePhysicalDevices(instance, &count, nullptr), "vkEnumeratePhysicalDevices"); std::vector devices(count); - VU_CHECK(vkEnumeratePhysicalDevices(instance, &count, devices.data())); + check_vk_result(vkEnumeratePhysicalDevices(instance, &count, devices.data()), "vkEnumeratePhysicalDevices"); if (count == 0) { throw std::runtime_error("no Vulkan physical devices found"); } @@ -135,7 +135,7 @@ device::device(std::vector instance_extensions, std::vectorphysical_device, &device_info, nullptr, &this->vk_device)); + check_vk_result(vkCreateDevice(this->physical_device, &device_info, nullptr, &this->vk_device), "vkCreateDevice"); vkGetDeviceQueue(this->vk_device, graphics_family, 0, &this->queue); this->graphics_queue_family = graphics_family; diff --git a/src/ruis/render/vulkan/factory.cpp b/src/ruis/render/vulkan/factory.cpp index c62bd99..8d05c1a 100644 --- a/src/ruis/render/vulkan/factory.cpp +++ b/src/ruis/render/vulkan/factory.cpp @@ -52,7 +52,7 @@ factory::factory(const utki::shared_ref& surface) : // .queueFamilyIndex = 0 }; { - VU_CHECK(vkCreateCommandPool(this->device, &pool_info, nullptr, &this->one_shot_pool)); + check_vk_result(vkCreateCommandPool(this->device, &pool_info, nullptr, &this->one_shot_pool), "vkCreateCommandPool"); } const auto alloc_info = VkCommandBufferAllocateInfo{ @@ -61,11 +61,11 @@ factory::factory(const utki::shared_ref& surface) : // .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, .commandBufferCount = 1 }; - VU_CHECK(vkAllocateCommandBuffers(this->device, &alloc_info, &this->one_shot_command_buffer)); + check_vk_result(vkAllocateCommandBuffers(this->device, &alloc_info, &this->one_shot_command_buffer), "vkAllocateCommandBuffers"); } { const auto fence_info = VkFenceCreateInfo{.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO}; - VU_CHECK(vkCreateFence(this->device, &fence_info, nullptr, &this->one_shot_fence)); + check_vk_result(vkCreateFence(this->device, &fence_info, nullptr, &this->one_shot_fence), "vkCreateFence"); } // ---- descriptor set layouts + pipeline layouts (shared) ---- @@ -84,7 +84,7 @@ factory::factory(const utki::shared_ref& surface) : // .bindingCount = 1, .pBindings = &ubo_binding }; - VU_CHECK(vkCreateDescriptorSetLayout(this->device, &dsl_info, nullptr, &this->dsl_no_tex)); + check_vk_result(vkCreateDescriptorSetLayout(this->device, &dsl_info, nullptr, &this->dsl_no_tex), "vkCreateDescriptorSetLayout"); } const auto with_tex_bindings = std::array{ @@ -100,7 +100,7 @@ factory::factory(const utki::shared_ref& surface) : // .bindingCount = 2, .pBindings = with_tex_bindings.data() }; - VU_CHECK(vkCreateDescriptorSetLayout(this->device, &dsl_info, nullptr, &this->dsl_with_tex)); + check_vk_result(vkCreateDescriptorSetLayout(this->device, &dsl_info, nullptr, &this->dsl_with_tex), "vkCreateDescriptorSetLayout"); } } { @@ -110,7 +110,7 @@ factory::factory(const utki::shared_ref& surface) : // .setLayoutCount = 1, .pSetLayouts = &this->dsl_no_tex }; - VU_CHECK(vkCreatePipelineLayout(this->device, &pl_info, nullptr, &this->pipeline_layout_no_tex)); + check_vk_result(vkCreatePipelineLayout(this->device, &pl_info, nullptr, &this->pipeline_layout_no_tex), "vkCreatePipelineLayout"); } { const auto pl_info = VkPipelineLayoutCreateInfo{ @@ -118,12 +118,12 @@ factory::factory(const utki::shared_ref& surface) : // .setLayoutCount = 1, .pSetLayouts = &this->dsl_with_tex }; - VU_CHECK(vkCreatePipelineLayout(this->device, &pl_info, nullptr, &this->pipeline_layout_with_tex)); + check_vk_result(vkCreatePipelineLayout(this->device, &pl_info, nullptr, &this->pipeline_layout_with_tex), "vkCreatePipelineLayout"); } } { const auto pc_info = VkPipelineCacheCreateInfo{.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO}; - VU_CHECK(vkCreatePipelineCache(this->device, &pc_info, nullptr, &this->pipeline_cache)); + check_vk_result(vkCreatePipelineCache(this->device, &pc_info, nullptr, &this->pipeline_cache), "vkCreatePipelineCache"); } } @@ -336,16 +336,16 @@ void factory::one_shot(std::function cmd) const // that was never submitted (e.g. the first call). vkQueueWaitIdle(this->queue); - VU_CHECK(vkResetCommandBuffer(this->one_shot_command_buffer, 0)); + check_vk_result(vkResetCommandBuffer(this->one_shot_command_buffer, 0), "vkResetCommandBuffer"); const auto begin_info = VkCommandBufferBeginInfo{ .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT }; - VU_CHECK(vkBeginCommandBuffer(this->one_shot_command_buffer, &begin_info)); + check_vk_result(vkBeginCommandBuffer(this->one_shot_command_buffer, &begin_info), "vkBeginCommandBuffer"); cmd(this->one_shot_command_buffer); - VU_CHECK(vkEndCommandBuffer(this->one_shot_command_buffer)); + check_vk_result(vkEndCommandBuffer(this->one_shot_command_buffer), "vkEndCommandBuffer"); const auto submit_info = VkSubmitInfo{ .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, @@ -359,7 +359,7 @@ void factory::one_shot(std::function cmd) const // VK_NULL_HANDLE instead of nullptr: on 32-bit targets the Vulkan headers // define non-dispatchable handles (VkFence, VkSemaphore, ...) as 64-bit // integers, not pointers - VU_CHECK(vkQueueSubmit(this->queue, 1, &submit_info, VK_NULL_HANDLE)); + check_vk_result(vkQueueSubmit(this->queue, 1, &submit_info, VK_NULL_HANDLE), "vkQueueSubmit"); // wait for the command buffer to complete, so the caller can safely use the // uploaded resources right away (e.g. sample the texture in the current frame) @@ -406,7 +406,7 @@ void factory::ensure_textures_uploaded() const .usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT, .sharingMode = VK_SHARING_MODE_EXCLUSIVE }; - VU_CHECK(vkCreateBuffer(this->device, &buffer_info, nullptr, &staging_buffer)); + check_vk_result(vkCreateBuffer(this->device, &buffer_info, nullptr, &staging_buffer), "vkCreateBuffer"); VkMemoryRequirements mem_reqs{}; vkGetBufferMemoryRequirements(this->device, staging_buffer, &mem_reqs); @@ -416,13 +416,13 @@ void factory::ensure_textures_uploaded() const .allocationSize = mem_reqs.size, .memoryTypeIndex = pick_host_visible_memory_type(this->physical_device, mem_reqs) }; - VU_CHECK(vkAllocateMemory(this->device, &alloc_info, nullptr, &staging_memory)); - VU_CHECK(vkBindBufferMemory(this->device, staging_buffer, staging_memory, 0)); + check_vk_result(vkAllocateMemory(this->device, &alloc_info, nullptr, &staging_memory), "vkAllocateMemory"); + check_vk_result(vkBindBufferMemory(this->device, staging_buffer, staging_memory, 0), "vkBindBufferMemory"); } // copy all texture data into the staging buffer void* mapped = nullptr; - VU_CHECK(vkMapMemory(this->device, staging_memory, 0, total_size, 0, &mapped)); + check_vk_result(vkMapMemory(this->device, staging_memory, 0, total_size, 0, &mapped), "vkMapMemory"); const auto mapped_span = utki::span(static_cast(mapped), total_size); VkDeviceSize offset = 0; for (auto* tex : pending) { diff --git a/src/ruis/render/vulkan/frame_buffer.cpp b/src/ruis/render/vulkan/frame_buffer.cpp index e2fa07f..3c2807a 100644 --- a/src/ruis/render/vulkan/frame_buffer.cpp +++ b/src/ruis/render/vulkan/frame_buffer.cpp @@ -56,7 +56,7 @@ frame_buffer::frame_buffer( .height = h, .layers = 1 }; - VU_CHECK(vkCreateFramebuffer(ctx.device, &fb_info, nullptr, &this->framebuffer)); + check_vk_result(vkCreateFramebuffer(ctx.device, &fb_info, nullptr, &this->framebuffer), "vkCreateFramebuffer"); } else { const auto attachments = std::array{{vcolor.image_view}}; const auto fb_info = VkFramebufferCreateInfo{ @@ -68,7 +68,7 @@ frame_buffer::frame_buffer( .height = h, .layers = 1 }; - VU_CHECK(vkCreateFramebuffer(ctx.device, &fb_info, nullptr, &this->framebuffer)); + check_vk_result(vkCreateFramebuffer(ctx.device, &fb_info, nullptr, &this->framebuffer), "vkCreateFramebuffer"); } } diff --git a/src/ruis/render/vulkan/index_buffer.cpp b/src/ruis/render/vulkan/index_buffer.cpp index 121f2d9..a9c7d3e 100644 --- a/src/ruis/render/vulkan/index_buffer.cpp +++ b/src/ruis/render/vulkan/index_buffer.cpp @@ -93,7 +93,7 @@ void index_buffer::create_vk_buffer(const void* data, size_t size_bytes) .usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT, .sharingMode = VK_SHARING_MODE_EXCLUSIVE }; - VU_CHECK(vkCreateBuffer(ctx.device, &buffer_info, nullptr, &this->buffer)); + check_vk_result(vkCreateBuffer(ctx.device, &buffer_info, nullptr, &this->buffer), "vkCreateBuffer"); VkMemoryRequirements mem_reqs{}; vkGetBufferMemoryRequirements(ctx.device, this->buffer, &mem_reqs); @@ -103,11 +103,11 @@ void index_buffer::create_vk_buffer(const void* data, size_t size_bytes) .allocationSize = mem_reqs.size, .memoryTypeIndex = pick_host_visible_memory_type(ctx.physical_device, mem_reqs) }; - VU_CHECK(vkAllocateMemory(ctx.device, &alloc_info, nullptr, &this->memory)); - VU_CHECK(vkBindBufferMemory(ctx.device, this->buffer, this->memory, 0)); + check_vk_result(vkAllocateMemory(ctx.device, &alloc_info, nullptr, &this->memory), "vkAllocateMemory"); + check_vk_result(vkBindBufferMemory(ctx.device, this->buffer, this->memory, 0), "vkBindBufferMemory"); void* mapped = nullptr; - VU_CHECK(vkMapMemory(ctx.device, this->memory, 0, this->byte_size, 0, &mapped)); + check_vk_result(vkMapMemory(ctx.device, this->memory, 0, this->byte_size, 0, &mapped), "vkMapMemory"); std::memcpy(mapped, data, size_bytes); vkUnmapMemory(ctx.device, this->memory); } diff --git a/src/ruis/render/vulkan/texture_2d.cpp b/src/ruis/render/vulkan/texture_2d.cpp index 2b4bec6..0c978b7 100644 --- a/src/ruis/render/vulkan/texture_2d.cpp +++ b/src/ruis/render/vulkan/texture_2d.cpp @@ -49,7 +49,7 @@ void create_staging_buffer( buffer_info.size = size; buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; - VU_CHECK(vkCreateBuffer(device, &buffer_info, nullptr, &buffer)); + check_vk_result(vkCreateBuffer(device, &buffer_info, nullptr, &buffer), "vkCreateBuffer"); VkMemoryRequirements mem_reqs{}; vkGetBufferMemoryRequirements(device, buffer, &mem_reqs); @@ -73,11 +73,11 @@ void create_staging_buffer( if (!found) { throw std::runtime_error("create_staging_buffer: no host-visible memory type found"); } - VU_CHECK(vkAllocateMemory(device, &alloc_info, nullptr, &memory)); - VU_CHECK(vkBindBufferMemory(device, buffer, memory, 0)); + check_vk_result(vkAllocateMemory(device, &alloc_info, nullptr, &memory), "vkAllocateMemory"); + check_vk_result(vkBindBufferMemory(device, buffer, memory, 0), "vkBindBufferMemory"); void* mapped = nullptr; - VU_CHECK(vkMapMemory(device, memory, 0, size, 0, &mapped)); + check_vk_result(vkMapMemory(device, memory, 0, size, 0, &mapped), "vkMapMemory"); std::memcpy(mapped, data, size); vkUnmapMemory(device, memory); } @@ -345,7 +345,7 @@ void texture_2d::create_image(rasterimage::dimensioned::dimensions_type dims) image_info.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; image_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; - VU_CHECK(vkCreateImage(ctx.device, &image_info, nullptr, &this->image)); + check_vk_result(vkCreateImage(ctx.device, &image_info, nullptr, &this->image), "vkCreateImage"); VkMemoryRequirements mem_reqs{}; vkGetImageMemoryRequirements(ctx.device, this->image, &mem_reqs); @@ -354,8 +354,8 @@ void texture_2d::create_image(rasterimage::dimensioned::dimensions_type dims) .allocationSize = mem_reqs.size, .memoryTypeIndex = pick_host_visible_memory_type(ctx.physical_device, mem_reqs) }; - VU_CHECK(vkAllocateMemory(ctx.device, &alloc_info, nullptr, &this->memory)); - VU_CHECK(vkBindImageMemory(ctx.device, this->image, this->memory, 0)); + check_vk_result(vkAllocateMemory(ctx.device, &alloc_info, nullptr, &this->memory), "vkAllocateMemory"); + check_vk_result(vkBindImageMemory(ctx.device, this->image, this->memory, 0), "vkBindImageMemory"); const auto view_info = VkImageViewCreateInfo{ .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, @@ -374,7 +374,7 @@ void texture_2d::create_image(rasterimage::dimensioned::dimensions_type dims) .baseArrayLayer = 0, .layerCount = 1} }; - VU_CHECK(vkCreateImageView(ctx.device, &view_info, nullptr, &this->image_view)); + check_vk_result(vkCreateImageView(ctx.device, &view_info, nullptr, &this->image_view), "vkCreateImageView"); } void texture_2d::create_sampler(ruis::render::factory::texture_2d_parameters params) @@ -402,7 +402,7 @@ void texture_2d::create_sampler(ruis::render::factory::texture_2d_parameters par .borderColor = VK_BORDER_COLOR_INT_TRANSPARENT_BLACK, .unnormalizedCoordinates = VK_FALSE }; - VU_CHECK(vkCreateSampler(ctx.device, &sampler_info, nullptr, &this->sampler)); + check_vk_result(vkCreateSampler(ctx.device, &sampler_info, nullptr, &this->sampler), "vkCreateSampler"); } void texture_2d::upload_data(const uint8_t* data, size_t data_size) diff --git a/src/ruis/render/vulkan/texture_depth.cpp b/src/ruis/render/vulkan/texture_depth.cpp index 4b351c9..c5c2a80 100644 --- a/src/ruis/render/vulkan/texture_depth.cpp +++ b/src/ruis/render/vulkan/texture_depth.cpp @@ -62,7 +62,7 @@ void texture_depth::create_image() .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED }; - VU_CHECK(vkCreateImage(ctx.device, &image_info, nullptr, &this->image)); + check_vk_result(vkCreateImage(ctx.device, &image_info, nullptr, &this->image), "vkCreateImage"); VkMemoryRequirements mem_reqs{}; vkGetImageMemoryRequirements(ctx.device, this->image, &mem_reqs); @@ -71,8 +71,8 @@ void texture_depth::create_image() .allocationSize = mem_reqs.size, .memoryTypeIndex = pick_device_local_memory_type(ctx.physical_device, mem_reqs) }; - VU_CHECK(vkAllocateMemory(ctx.device, &alloc_info, nullptr, &this->memory)); - VU_CHECK(vkBindImageMemory(ctx.device, this->image, this->memory, 0)); + check_vk_result(vkAllocateMemory(ctx.device, &alloc_info, nullptr, &this->memory), "vkAllocateMemory"); + check_vk_result(vkBindImageMemory(ctx.device, this->image, this->memory, 0), "vkBindImageMemory"); const auto view_info = VkImageViewCreateInfo{ .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, @@ -86,5 +86,5 @@ void texture_depth::create_image() .baseArrayLayer = 0, .layerCount = 1} }; - VU_CHECK(vkCreateImageView(ctx.device, &view_info, nullptr, &this->image_view)); + check_vk_result(vkCreateImageView(ctx.device, &view_info, nullptr, &this->image_view), "vkCreateImageView"); } diff --git a/src/ruis/render/vulkan/vertex_buffer.cpp b/src/ruis/render/vulkan/vertex_buffer.cpp index 92b311d..961acd4 100644 --- a/src/ruis/render/vulkan/vertex_buffer.cpp +++ b/src/ruis/render/vulkan/vertex_buffer.cpp @@ -70,7 +70,7 @@ void vertex_buffer::create_vk_buffer( .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, .sharingMode = VK_SHARING_MODE_EXCLUSIVE }; - VU_CHECK(vkCreateBuffer(ctx.device, &buffer_info, nullptr, &this->buffer)); + check_vk_result(vkCreateBuffer(ctx.device, &buffer_info, nullptr, &this->buffer), "vkCreateBuffer"); VkMemoryRequirements mem_reqs{}; vkGetBufferMemoryRequirements(ctx.device, this->buffer, &mem_reqs); @@ -80,11 +80,11 @@ void vertex_buffer::create_vk_buffer( .allocationSize = mem_reqs.size, .memoryTypeIndex = pick_host_visible_memory_type(ctx.physical_device, mem_reqs) }; - VU_CHECK(vkAllocateMemory(ctx.device, &alloc_info, nullptr, &this->memory)); - VU_CHECK(vkBindBufferMemory(ctx.device, this->buffer, this->memory, 0)); + check_vk_result(vkAllocateMemory(ctx.device, &alloc_info, nullptr, &this->memory), "vkAllocateMemory"); + check_vk_result(vkBindBufferMemory(ctx.device, this->buffer, this->memory, 0), "vkBindBufferMemory"); void* mapped = nullptr; - VU_CHECK(vkMapMemory(ctx.device, this->memory, 0, this->byte_size, 0, &mapped)); + check_vk_result(vkMapMemory(ctx.device, this->memory, 0, this->byte_size, 0, &mapped), "vkMapMemory"); std::memcpy(mapped, data.data(), data.size() * sizeof(float)); vkUnmapMemory(ctx.device, this->memory); } diff --git a/src/ruis/render/vulkan/vulkan.hpp b/src/ruis/render/vulkan/vulkan.hpp index 80f6096..0f2e994 100644 --- a/src/ruis/render/vulkan/vulkan.hpp +++ b/src/ruis/render/vulkan/vulkan.hpp @@ -48,9 +48,6 @@ inline void check_vk_result(VkResult res, const char* where) } } -// checks the result of a Vulkan call -#define VU_CHECK(x) ::ruis::render::vulkan::check_vk_result((x), #x) - // converts a ruis render blend factor to a VkBlendFactor inline VkBlendFactor to_vk_blend_factor(ruis::render::context::blend_factor f) { From 2efd12c819e30875445dda49a337fcf68701f641 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Sat, 21 Nov 2026 01:14:13 +0200 Subject: [PATCH 09/12] Apply formatting --- src/ruis/render/vulkan/context.cpp | 60 +++++++++++++++++++++++------- src/ruis/render/vulkan/factory.cpp | 35 +++++++++++++---- 2 files changed, 75 insertions(+), 20 deletions(-) diff --git a/src/ruis/render/vulkan/context.cpp b/src/ruis/render/vulkan/context.cpp index c3499bf..02d9fd8 100644 --- a/src/ruis/render/vulkan/context.cpp +++ b/src/ruis/render/vulkan/context.cpp @@ -147,7 +147,10 @@ context::context( .queueFamilyIndex = 0 }; { - check_vk_result(vkCreateCommandPool(this->device, &pool_info, nullptr, &this->command_pool), "vkCreateCommandPool"); + check_vk_result( + vkCreateCommandPool(this->device, &pool_info, nullptr, &this->command_pool), + "vkCreateCommandPool" + ); } const auto alloc_info = VkCommandBufferAllocateInfo{ @@ -156,7 +159,10 @@ context::context( .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, .commandBufferCount = 1 }; - check_vk_result(vkAllocateCommandBuffers(this->device, &alloc_info, &this->command_buffer), "vkAllocateCommandBuffers"); + check_vk_result( + vkAllocateCommandBuffers(this->device, &alloc_info, &this->command_buffer), + "vkAllocateCommandBuffers" + ); } { const auto fence_info = VkFenceCreateInfo{.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO}; @@ -165,10 +171,16 @@ context::context( { const auto sem_info = VkSemaphoreCreateInfo{.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO}; { - check_vk_result(vkCreateSemaphore(this->device, &sem_info, nullptr, &this->image_available_sem), "vkCreateSemaphore"); + check_vk_result( + vkCreateSemaphore(this->device, &sem_info, nullptr, &this->image_available_sem), + "vkCreateSemaphore" + ); } { - check_vk_result(vkCreateSemaphore(this->device, &sem_info, nullptr, &this->render_finished_sem), "vkCreateSemaphore"); + check_vk_result( + vkCreateSemaphore(this->device, &sem_info, nullptr, &this->render_finished_sem), + "vkCreateSemaphore" + ); } } { @@ -184,7 +196,10 @@ context::context( .pPoolSizes = pool_sizes.data() }; { - check_vk_result(vkCreateDescriptorPool(this->device, &pool_info, nullptr, &this->descriptor_pool), "vkCreateDescriptorPool"); + check_vk_result( + vkCreateDescriptorPool(this->device, &pool_info, nullptr, &this->descriptor_pool), + "vkCreateDescriptorPool" + ); } } @@ -216,7 +231,10 @@ void context::create_swapchain() this->swapchain_format = surface_format.format; VkSurfaceCapabilitiesKHR caps{}; - check_vk_result(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(this->physical_device, this->vk_surface, &caps), "vkGetPhysicalDeviceSurfaceCapabilitiesKHR"); + check_vk_result( + vkGetPhysicalDeviceSurfaceCapabilitiesKHR(this->physical_device, this->vk_surface, &caps), + "vkGetPhysicalDeviceSurfaceCapabilitiesKHR" + ); // clamp the dimensions to the supported range uint32_t width = this->window_dims.x(); @@ -284,7 +302,10 @@ void context::create_swapchain() uint32_t count = 0; check_vk_result(vkGetSwapchainImagesKHR(this->device, this->swapchain, &count, nullptr), "vkGetSwapchainImagesKHR"); this->swapchain_images.resize(count); - check_vk_result(vkGetSwapchainImagesKHR(this->device, this->swapchain, &count, this->swapchain_images.data()), "vkGetSwapchainImagesKHR"); + check_vk_result( + vkGetSwapchainImagesKHR(this->device, this->swapchain, &count, this->swapchain_images.data()), + "vkGetSwapchainImagesKHR" + ); this->swapchain_image_count = count; this->window_dims = r4::vector2(width, height); @@ -367,8 +388,14 @@ void context::create_depth_image() .allocationSize = mem_reqs.size, .memoryTypeIndex = pick_device_local_memory_type(this->physical_device, mem_reqs) }; - check_vk_result(vkAllocateMemory(this->device, &alloc_info, nullptr, &this->depth_image_memory), "vkAllocateMemory"); - check_vk_result(vkBindImageMemory(this->device, this->depth_image, this->depth_image_memory, 0), "vkBindImageMemory"); + check_vk_result( + vkAllocateMemory(this->device, &alloc_info, nullptr, &this->depth_image_memory), + "vkAllocateMemory" + ); + check_vk_result( + vkBindImageMemory(this->device, this->depth_image, this->depth_image_memory, 0), + "vkBindImageMemory" + ); { const auto view_info = VkImageViewCreateInfo{ @@ -383,7 +410,10 @@ void context::create_depth_image() .baseArrayLayer = 0, .layerCount = 1} }; - check_vk_result(vkCreateImageView(this->device, &view_info, nullptr, &this->depth_image_view), "vkCreateImageView"); + check_vk_result( + vkCreateImageView(this->device, &view_info, nullptr, &this->depth_image_view), + "vkCreateImageView" + ); } } @@ -1410,8 +1440,9 @@ VkPipeline context::build_pipeline( VkPipeline pipeline = VK_NULL_HANDLE; check_vk_result( - vkCreateGraphicsPipelines(this->device, this->factory.get().get_pipeline_cache(), 1, &info, nullptr, &pipeline) - , "vkCreateGraphicsPipelines"); + vkCreateGraphicsPipelines(this->device, this->factory.get().get_pipeline_cache(), 1, &info, nullptr, &pipeline), + "vkCreateGraphicsPipelines" + ); // destroy the shader modules (they are now baked into the pipeline) vkDestroyShaderModule(this->device, vertex_stage.module, nullptr); @@ -1613,7 +1644,10 @@ void context::submit_frame(bool present) // wait for the frame to complete using vkQueueWaitIdle (safest for AMD driver) { check_vk_result(vkQueueWaitIdle(this->queue), "vkQueueWaitIdle"); - check_vk_result(vkWaitForFences(this->device, 1, &this->frame_fence, VK_TRUE, 1000000000ull), "vkWaitForFences"); // 1s timeout + check_vk_result( + vkWaitForFences(this->device, 1, &this->frame_fence, VK_TRUE, 1000000000ull), + "vkWaitForFences" + ); // 1s timeout vkResetFences(this->device, 1, &this->frame_fence); this->frame_submitted = false; } diff --git a/src/ruis/render/vulkan/factory.cpp b/src/ruis/render/vulkan/factory.cpp index 8d05c1a..c4d8fac 100644 --- a/src/ruis/render/vulkan/factory.cpp +++ b/src/ruis/render/vulkan/factory.cpp @@ -52,7 +52,10 @@ factory::factory(const utki::shared_ref& surface) : // .queueFamilyIndex = 0 }; { - check_vk_result(vkCreateCommandPool(this->device, &pool_info, nullptr, &this->one_shot_pool), "vkCreateCommandPool"); + check_vk_result( + vkCreateCommandPool(this->device, &pool_info, nullptr, &this->one_shot_pool), + "vkCreateCommandPool" + ); } const auto alloc_info = VkCommandBufferAllocateInfo{ @@ -61,7 +64,10 @@ factory::factory(const utki::shared_ref& surface) : // .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, .commandBufferCount = 1 }; - check_vk_result(vkAllocateCommandBuffers(this->device, &alloc_info, &this->one_shot_command_buffer), "vkAllocateCommandBuffers"); + check_vk_result( + vkAllocateCommandBuffers(this->device, &alloc_info, &this->one_shot_command_buffer), + "vkAllocateCommandBuffers" + ); } { const auto fence_info = VkFenceCreateInfo{.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO}; @@ -84,7 +90,10 @@ factory::factory(const utki::shared_ref& surface) : // .bindingCount = 1, .pBindings = &ubo_binding }; - check_vk_result(vkCreateDescriptorSetLayout(this->device, &dsl_info, nullptr, &this->dsl_no_tex), "vkCreateDescriptorSetLayout"); + check_vk_result( + vkCreateDescriptorSetLayout(this->device, &dsl_info, nullptr, &this->dsl_no_tex), + "vkCreateDescriptorSetLayout" + ); } const auto with_tex_bindings = std::array{ @@ -100,7 +109,10 @@ factory::factory(const utki::shared_ref& surface) : // .bindingCount = 2, .pBindings = with_tex_bindings.data() }; - check_vk_result(vkCreateDescriptorSetLayout(this->device, &dsl_info, nullptr, &this->dsl_with_tex), "vkCreateDescriptorSetLayout"); + check_vk_result( + vkCreateDescriptorSetLayout(this->device, &dsl_info, nullptr, &this->dsl_with_tex), + "vkCreateDescriptorSetLayout" + ); } } { @@ -110,7 +122,10 @@ factory::factory(const utki::shared_ref& surface) : // .setLayoutCount = 1, .pSetLayouts = &this->dsl_no_tex }; - check_vk_result(vkCreatePipelineLayout(this->device, &pl_info, nullptr, &this->pipeline_layout_no_tex), "vkCreatePipelineLayout"); + check_vk_result( + vkCreatePipelineLayout(this->device, &pl_info, nullptr, &this->pipeline_layout_no_tex), + "vkCreatePipelineLayout" + ); } { const auto pl_info = VkPipelineLayoutCreateInfo{ @@ -118,12 +133,18 @@ factory::factory(const utki::shared_ref& surface) : // .setLayoutCount = 1, .pSetLayouts = &this->dsl_with_tex }; - check_vk_result(vkCreatePipelineLayout(this->device, &pl_info, nullptr, &this->pipeline_layout_with_tex), "vkCreatePipelineLayout"); + check_vk_result( + vkCreatePipelineLayout(this->device, &pl_info, nullptr, &this->pipeline_layout_with_tex), + "vkCreatePipelineLayout" + ); } } { const auto pc_info = VkPipelineCacheCreateInfo{.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO}; - check_vk_result(vkCreatePipelineCache(this->device, &pc_info, nullptr, &this->pipeline_cache), "vkCreatePipelineCache"); + check_vk_result( + vkCreatePipelineCache(this->device, &pc_info, nullptr, &this->pipeline_cache), + "vkCreatePipelineCache" + ); } } From 222a7a0e72e4677ce611ecb1076022cda2eefd03 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Sat, 21 Nov 2026 02:19:11 +0200 Subject: [PATCH 10/12] Address review comments: pass by value+move, remove unused param, use ranges::find --- src/ruis/render/vulkan/context.cpp | 8 +++----- src/ruis/render/vulkan/context.hpp | 4 ++-- src/ruis/render/vulkan/factory.cpp | 7 +------ src/ruis/render/vulkan/factory.hpp | 1 + src/ruis/render/vulkan/shaders/shader_color.cpp | 1 - src/ruis/render/vulkan/shaders/shader_color_pos_lum.cpp | 1 - src/ruis/render/vulkan/shaders/shader_color_pos_tex.cpp | 1 - .../render/vulkan/shaders/shader_color_pos_tex_alpha.cpp | 1 - src/ruis/render/vulkan/shaders/shader_common.cpp | 3 --- src/ruis/render/vulkan/shaders/shader_common.hpp | 1 - src/ruis/render/vulkan/shaders/shader_pos_clr.cpp | 1 - src/ruis/render/vulkan/shaders/shader_pos_tex.cpp | 1 - 12 files changed, 7 insertions(+), 23 deletions(-) diff --git a/src/ruis/render/vulkan/context.cpp b/src/ruis/render/vulkan/context.cpp index 02d9fd8..3505e74 100644 --- a/src/ruis/render/vulkan/context.cpp +++ b/src/ruis/render/vulkan/context.cpp @@ -104,12 +104,12 @@ VkPresentModeKHR pick_present_mode(VkPhysicalDevice phys, VkSurfaceKHR surface, } // namespace context::context( - const utki::shared_ref& native_window, // - const utki::shared_ref& surface, // + utki::shared_ref native_window, // + utki::shared_ref surface, // const utki::shared_ref& factory ) : ruis::render::context( - native_window, + std::move(native_window), factory, [] { r4::matrix4 m{}; @@ -135,8 +135,6 @@ context::context( queue(surface.get().get_queue()), // vk_surface(surface.get().get_surface()) { - (void)native_window; - vkGetPhysicalDeviceProperties(this->physical_device, &this->device_properties); // ---- command submission resources ---- diff --git a/src/ruis/render/vulkan/context.hpp b/src/ruis/render/vulkan/context.hpp index 1b581a7..b7aaa3d 100644 --- a/src/ruis/render/vulkan/context.hpp +++ b/src/ruis/render/vulkan/context.hpp @@ -88,8 +88,8 @@ class context : public ruis::render::context { public: context( - const utki::shared_ref& native_window, // - const utki::shared_ref& surface, // + utki::shared_ref native_window, // + utki::shared_ref surface, // const utki::shared_ref& factory ); ~context() override; diff --git a/src/ruis/render/vulkan/factory.cpp b/src/ruis/render/vulkan/factory.cpp index c4d8fac..5bfca7b 100644 --- a/src/ruis/render/vulkan/factory.cpp +++ b/src/ruis/render/vulkan/factory.cpp @@ -477,10 +477,5 @@ void factory::ensure_textures_uploaded() const void factory::unregister_texture(texture_2d* t) const { auto& tex_list = this->all_textures; - for (auto it = tex_list.begin(); it != tex_list.end(); ++it) { - if (*it == t) { - tex_list.erase(it); - break; - } - } + tex_list.erase(std::ranges::find(tex_list, t)); } diff --git a/src/ruis/render/vulkan/factory.hpp b/src/ruis/render/vulkan/factory.hpp index f36017e..16933b0 100644 --- a/src/ruis/render/vulkan/factory.hpp +++ b/src/ruis/render/vulkan/factory.hpp @@ -173,6 +173,7 @@ class factory : public ruis::render::factory // const: it only submits GPU work, it does not mutate the factory's C++ state. void one_shot(std::function cmd) const; + // review: what is that deferred textures uploading? can we just wait for the texture to be fully uploaded to GPU when the texture is created inside of the make_texture_2d and only then return? // upload all deferred textures to the GPU (idempotent). const: it only mutates // the texture objects (their upload state), not the factory's C++ state. void ensure_textures_uploaded() const; diff --git a/src/ruis/render/vulkan/shaders/shader_color.cpp b/src/ruis/render/vulkan/shaders/shader_color.cpp index ba10295..c0658be 100644 --- a/src/ruis/render/vulkan/shaders/shader_color.cpp +++ b/src/ruis/render/vulkan/shaders/shader_color.cpp @@ -38,7 +38,6 @@ void shader_color::render( ) const { vulkan_shader_render( - this->factory, // shader_kind::color, std::data(spirv_shader_color_vertex), spirv_shader_color_vertex_size, diff --git a/src/ruis/render/vulkan/shaders/shader_color_pos_lum.cpp b/src/ruis/render/vulkan/shaders/shader_color_pos_lum.cpp index 7567082..7bbac25 100644 --- a/src/ruis/render/vulkan/shaders/shader_color_pos_lum.cpp +++ b/src/ruis/render/vulkan/shaders/shader_color_pos_lum.cpp @@ -38,7 +38,6 @@ void shader_color_pos_lum::render( ) const { vulkan_shader_render( - this->factory, // shader_kind::color_pos_lum, std::data(spirv_shader_color_pos_lum_vertex), spirv_shader_color_pos_lum_vertex_size, diff --git a/src/ruis/render/vulkan/shaders/shader_color_pos_tex.cpp b/src/ruis/render/vulkan/shaders/shader_color_pos_tex.cpp index fca1cbf..6d01c41 100644 --- a/src/ruis/render/vulkan/shaders/shader_color_pos_tex.cpp +++ b/src/ruis/render/vulkan/shaders/shader_color_pos_tex.cpp @@ -45,7 +45,6 @@ void shader_color_pos_tex::render( const auto& vk_tex = static_cast(tex); vulkan_shader_render( - this->factory, // shader_kind::color_pos_tex, std::data(spirv_shader_color_pos_tex_vertex), spirv_shader_color_pos_tex_vertex_size, diff --git a/src/ruis/render/vulkan/shaders/shader_color_pos_tex_alpha.cpp b/src/ruis/render/vulkan/shaders/shader_color_pos_tex_alpha.cpp index d2b74e5..afa8fec 100644 --- a/src/ruis/render/vulkan/shaders/shader_color_pos_tex_alpha.cpp +++ b/src/ruis/render/vulkan/shaders/shader_color_pos_tex_alpha.cpp @@ -45,7 +45,6 @@ void shader_color_pos_tex_alpha::render( const auto& vk_tex = static_cast(tex); vulkan_shader_render( - this->factory, // shader_kind::color_pos_tex_alpha, std::data(spirv_shader_color_pos_tex_alpha_vertex), spirv_shader_color_pos_tex_alpha_vertex_size, diff --git a/src/ruis/render/vulkan/shaders/shader_common.cpp b/src/ruis/render/vulkan/shaders/shader_common.cpp index 3d6f360..d2d0c12 100644 --- a/src/ruis/render/vulkan/shaders/shader_common.cpp +++ b/src/ruis/render/vulkan/shaders/shader_common.cpp @@ -31,7 +31,6 @@ along with this program. If not, see . namespace ruis::render::vulkan { void vulkan_shader_render( - const utki::shared_ref& factory, // NOLINT (unused: render uses context::current()) shader_kind kind, const void* vertex_spirv, size_t vertex_spirv_len, @@ -43,8 +42,6 @@ void vulkan_shader_render( const ruis::render::vertex_array& va ) { - (void)factory; - // use the currently active context (set by ruis::render::context::apply()), // not the context stored in the shader (which may be the shared resource-loader context). auto& ctx = diff --git a/src/ruis/render/vulkan/shaders/shader_common.hpp b/src/ruis/render/vulkan/shaders/shader_common.hpp index e5eab8c..2a75534 100644 --- a/src/ruis/render/vulkan/shaders/shader_common.hpp +++ b/src/ruis/render/vulkan/shaders/shader_common.hpp @@ -69,7 +69,6 @@ class texture_2d; * may be different from the context currently rendering) before the draw. */ void vulkan_shader_render( - const utki::shared_ref& factory, // NOLINT (unused: render uses context::current()) shader_kind kind, const void* vertex_spirv, size_t vertex_spirv_len, diff --git a/src/ruis/render/vulkan/shaders/shader_pos_clr.cpp b/src/ruis/render/vulkan/shaders/shader_pos_clr.cpp index 1340c8b..e49792e 100644 --- a/src/ruis/render/vulkan/shaders/shader_pos_clr.cpp +++ b/src/ruis/render/vulkan/shaders/shader_pos_clr.cpp @@ -34,7 +34,6 @@ shader_pos_clr::shader_pos_clr(utki::shared_ref fac void shader_pos_clr::render(const r4::matrix4& m, const ruis::render::vertex_array& va) const { vulkan_shader_render( - this->factory, // shader_kind::pos_clr, std::data(spirv_shader_pos_clr_vertex), spirv_shader_pos_clr_vertex_size, diff --git a/src/ruis/render/vulkan/shaders/shader_pos_tex.cpp b/src/ruis/render/vulkan/shaders/shader_pos_tex.cpp index 2ac9d94..0f5d81b 100644 --- a/src/ruis/render/vulkan/shaders/shader_pos_tex.cpp +++ b/src/ruis/render/vulkan/shaders/shader_pos_tex.cpp @@ -60,7 +60,6 @@ void shader_pos_tex::render( } vulkan_shader_render( - this->factory, // shader_kind::pos_tex, std::data(spirv_shader_pos_tex_vertex), spirv_shader_pos_tex_vertex_size, From 27d2ee71c3905326b9ff962d34bb82f9cc3efbb3 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Sat, 21 Nov 2026 02:31:45 +0200 Subject: [PATCH 11/12] Implement synchronous texture upload in make_texture_2d (review #1) Remove the deferred texture upload mechanism. Textures are now uploaded synchronously to the GPU when created, using a one-shot command buffer. This simplifies the code and eliminates the need for tracking uploaded state across frames. --- src/ruis/render/vulkan/context.cpp | 3 - src/ruis/render/vulkan/factory.cpp | 60 +--- src/ruis/render/vulkan/factory.hpp | 19 +- .../render/vulkan/shaders/shader_common.cpp | 8 - src/ruis/render/vulkan/texture_2d.cpp | 294 ++++-------------- src/ruis/render/vulkan/texture_2d.hpp | 3 +- 6 files changed, 78 insertions(+), 309 deletions(-) diff --git a/src/ruis/render/vulkan/context.cpp b/src/ruis/render/vulkan/context.cpp index 3505e74..24031c9 100644 --- a/src/ruis/render/vulkan/context.cpp +++ b/src/ruis/render/vulkan/context.cpp @@ -1458,9 +1458,6 @@ void context::begin_render_pass_internal(bool has_depth) if (this->current_fb == nullptr) { has_depth = true; } - // ensure all textures are uploaded before the first render pass - this->factory.get().ensure_textures_uploaded(); - // determine the render pass (clear or load) const bool clear = this->pending_clear; const auto render_pass = this->get_current_render_pass(has_depth, clear); diff --git a/src/ruis/render/vulkan/factory.cpp b/src/ruis/render/vulkan/factory.cpp index 5bfca7b..41dbfa2 100644 --- a/src/ruis/render/vulkan/factory.cpp +++ b/src/ruis/render/vulkan/factory.cpp @@ -387,37 +387,13 @@ void factory::one_shot(std::function cmd) const vkQueueWaitIdle(this->queue); } -void factory::ensure_textures_uploaded() const +void factory::upload_texture(texture_2d* t) const { - // find textures that need uploading (no global flag: new textures can be created at any time) - std::vector pending; - for (auto* tex : this->all_textures) { - if (!tex->uploaded && !tex->deferred_data.empty()) { - pending.push_back(tex); - } - } - - if (pending.empty()) { - // mark empty textures as uploaded - for (auto* tex : this->all_textures) { - if (!tex->uploaded && tex->deferred_data.empty()) { - tex->uploaded = true; - } - } - return; - } - - // calculate total staging size - VkDeviceSize total_size = 0; - for (auto* tex : pending) { - total_size += tex->staging_size(); - } - + const auto total_size = t->staging_size(); if (total_size == 0) { return; } - // create a single staging buffer for all textures VkBuffer staging_buffer = VK_NULL_HANDLE; VkDeviceMemory staging_memory = VK_NULL_HANDLE; { @@ -441,41 +417,19 @@ void factory::ensure_textures_uploaded() const check_vk_result(vkBindBufferMemory(this->device, staging_buffer, staging_memory, 0), "vkBindBufferMemory"); } - // copy all texture data into the staging buffer void* mapped = nullptr; check_vk_result(vkMapMemory(this->device, staging_memory, 0, total_size, 0, &mapped), "vkMapMemory"); - const auto mapped_span = utki::span(static_cast(mapped), total_size); - VkDeviceSize offset = 0; - for (auto* tex : pending) { - const auto sz = tex->staging_size(); - std::memcpy(mapped_span.subspan(offset, sz).data(), tex->deferred_data.data(), sz); - offset += sz; - } + std::memcpy(static_cast(mapped), t->deferred_data.data(), total_size); vkUnmapMemory(this->device, staging_memory); - // record all upload commands into a single one-shot command buffer - VkDeviceSize cmd_offset = 0; + // record the upload commands and submit (one_shot() resets, submits, and waits) this->one_shot([&](VkCommandBuffer cmd) { - for (auto* tex : pending) { - const auto sz = tex->staging_size(); - tex->record_upload_commands(cmd, staging_buffer, cmd_offset); - cmd_offset += sz; - } + t->record_upload_commands(cmd, staging_buffer, 0); }); - // clean up vkFreeMemory(this->device, staging_memory, nullptr); vkDestroyBuffer(this->device, staging_buffer, nullptr); - // mark uploaded textures - for (auto* tex : pending) { - tex->deferred_data.clear(); - tex->uploaded = true; - } -} - -void factory::unregister_texture(texture_2d* t) const -{ - auto& tex_list = this->all_textures; - tex_list.erase(std::ranges::find(tex_list, t)); + t->deferred_data.clear(); + t->uploaded = true; } diff --git a/src/ruis/render/vulkan/factory.hpp b/src/ruis/render/vulkan/factory.hpp index 16933b0..68a24fb 100644 --- a/src/ruis/render/vulkan/factory.hpp +++ b/src/ruis/render/vulkan/factory.hpp @@ -173,24 +173,11 @@ class factory : public ruis::render::factory // const: it only submits GPU work, it does not mutate the factory's C++ state. void one_shot(std::function cmd) const; - // review: what is that deferred textures uploading? can we just wait for the texture to be fully uploaded to GPU when the texture is created inside of the make_texture_2d and only then return? - // upload all deferred textures to the GPU (idempotent). const: it only mutates - // the texture objects (their upload state), not the factory's C++ state. - void ensure_textures_uploaded() const; - - // register / unregister a texture with the factory's deferred-upload tracking. - // const: they only mutate the (mutable) tracking list, not the factory's C++ state. - void register_texture(texture_2d* t) const - { - this->all_textures.push_back(t); - } - - void unregister_texture(texture_2d* t) const; + // upload a single texture to the GPU synchronously (blocks until complete). + // called from the texture_2d constructor after the data is converted to RGBA. + void upload_texture(texture_2d* t) const; private: - // ---- texture tracking for deferred upload (mutable: guarded by const register/unregister) ---- - mutable std::vector all_textures; - // ---- one-shot command submission (texture uploads) ---- VkCommandPool one_shot_pool = VK_NULL_HANDLE; VkCommandBuffer one_shot_command_buffer = VK_NULL_HANDLE; diff --git a/src/ruis/render/vulkan/shaders/shader_common.cpp b/src/ruis/render/vulkan/shaders/shader_common.cpp index d2d0c12..40b4213 100644 --- a/src/ruis/render/vulkan/shaders/shader_common.cpp +++ b/src/ruis/render/vulkan/shaders/shader_common.cpp @@ -60,14 +60,6 @@ void vulkan_shader_render( // make sure the texture data is uploaded to the GPU. The texture is owned by // the context that created it (e.g. the shared resource-loader context), - // which may be different from the context that is currently rendering, so - // the upload must be driven from the texture itself (it uses its own - // context's one-shot command path). - if (texture != nullptr) { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast, "ensure_uploaded mutates the upload state") - const_cast(texture)->ensure_uploaded(); - } - ctx.ensure_frame_began(); // map the VAO's rendering mode to a Vulkan primitive topology diff --git a/src/ruis/render/vulkan/texture_2d.cpp b/src/ruis/render/vulkan/texture_2d.cpp index 0c978b7..01de7b4 100644 --- a/src/ruis/render/vulkan/texture_2d.cpp +++ b/src/ruis/render/vulkan/texture_2d.cpp @@ -33,64 +33,6 @@ namespace { constexpr uint8_t opaque_alpha = 255; } // namespace -namespace { -// create a host-visible staging buffer with the given contents -void create_staging_buffer( - VkDevice device, // - VkPhysicalDevice physical_device, - const void* data, - VkDeviceSize size, - VkBuffer& buffer, - VkDeviceMemory& memory -) -{ - VkBufferCreateInfo buffer_info{}; - buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; - buffer_info.size = size; - buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; - buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; - check_vk_result(vkCreateBuffer(device, &buffer_info, nullptr, &buffer), "vkCreateBuffer"); - - VkMemoryRequirements mem_reqs{}; - vkGetBufferMemoryRequirements(device, buffer, &mem_reqs); - - VkPhysicalDeviceMemoryProperties props{}; - vkGetPhysicalDeviceMemoryProperties(physical_device, &props); - VkMemoryAllocateInfo alloc_info{}; - alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; - alloc_info.allocationSize = mem_reqs.size; - alloc_info.memoryTypeIndex = 0; - bool found = false; - for (uint32_t i = 0; i < props.memoryTypeCount; ++i) { - if ((mem_reqs.memoryTypeBits & (1 << i)) && - (props.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)) - { - alloc_info.memoryTypeIndex = i; - found = true; - break; - } - } - if (!found) { - throw std::runtime_error("create_staging_buffer: no host-visible memory type found"); - } - check_vk_result(vkAllocateMemory(device, &alloc_info, nullptr, &memory), "vkAllocateMemory"); - check_vk_result(vkBindBufferMemory(device, buffer, memory, 0), "vkBindBufferMemory"); - - void* mapped = nullptr; - check_vk_result(vkMapMemory(device, memory, 0, size, 0, &mapped), "vkMapMemory"); - std::memcpy(mapped, data, size); - vkUnmapMemory(device, memory); -} - -void destroy_staging_buffer(VkDevice device, VkBuffer buffer, VkDeviceMemory memory) -{ - if (buffer != VK_NULL_HANDLE) { - vkDestroyBuffer(device, buffer, nullptr); - vkFreeMemory(device, memory, nullptr); - } -} -} // namespace - texture_2d::texture_2d( utki::shared_ref factory, // rasterimage::format type, @@ -109,11 +51,6 @@ texture_2d::texture_2d( this->create_image(dims); this->create_sampler(params); - // register with the factory for deferred upload tracking - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the factory is owned by the renderer; the base-class reference comes from the ruis API") - const auto& vk_ctx = static_cast(this->factory.get()); - vk_ctx.register_texture(this); - if (data != nullptr && data_size != 0) { // defer the upload until the swapchain is ready // convert the data to RGBA (4 bytes per pixel) for Vulkan @@ -157,6 +94,73 @@ texture_2d::texture_2d( this->deferred_data.assign(data_span.begin(), data_span.end()); break; } + // upload the texture to the GPU synchronously + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the factory is owned by the renderer; the base-class reference comes from the ruis API") + static_cast(this->factory.get()).upload_texture(this); + } else { + // no data: clear the image to transparent so that undefined GPU memory + // does not produce garbage pixels + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the factory is owned by the renderer; the base-class reference comes from the ruis API") + auto& vk_f = static_cast(this->factory.get()); + vk_f.one_shot([&](VkCommandBuffer cmd) { + const auto range = VkImageSubresourceRange{ + .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, + .baseMipLevel = 0, + .levelCount = 1, + .baseArrayLayer = 0, + .layerCount = 1 + }; + const auto barrier = VkImageMemoryBarrier{ + .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, + .srcAccessMask = 0, + .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT, + .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED, + .newLayout = VK_IMAGE_LAYOUT_GENERAL, + .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .image = this->image, + .subresourceRange = range + }; + vkCmdPipelineBarrier( + cmd, + VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, + VK_PIPELINE_STAGE_TRANSFER_BIT, + 0, + 0, + nullptr, + 0, + nullptr, + 1, + &barrier + ); + const auto clear_color = VkClearColorValue{ + .float32 = {0.0f, 0.0f, 0.0f, 0.0f} + }; + vkCmdClearColorImage(cmd, this->image, VK_IMAGE_LAYOUT_GENERAL, &clear_color, 1, &range); + const auto barrier2 = VkImageMemoryBarrier{ + .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, + .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT, + .dstAccessMask = VK_ACCESS_SHADER_READ_BIT, + .oldLayout = VK_IMAGE_LAYOUT_GENERAL, + .newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, + .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .image = this->image, + .subresourceRange = range + }; + vkCmdPipelineBarrier( + cmd, + VK_PIPELINE_STAGE_TRANSFER_BIT, + VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, + 0, + 0, + nullptr, + 0, + nullptr, + 1, + &barrier2 + ); + }); } } @@ -236,89 +240,10 @@ void texture_2d::record_upload_commands(VkCommandBuffer cmd, VkBuffer staging_bu ); } -void texture_2d::ensure_uploaded() -{ - if (this->uploaded) { - return; - } - if (this->deferred_data.empty()) { - // no data to upload: clear the image to transparent so that - // undefined GPU memory does not produce garbage pixels - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the factory is owned by the renderer; the base-class reference comes from the ruis API") - const auto& ctx = static_cast(this->factory.get()); - ctx.one_shot([&](VkCommandBuffer cmd) { - const auto range = VkImageSubresourceRange{ - .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, - .baseMipLevel = 0, - .levelCount = 1, - .baseArrayLayer = 0, - .layerCount = 1 - }; - const auto barrier = VkImageMemoryBarrier{ - .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, - .srcAccessMask = 0, - .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT, - .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED, - .newLayout = VK_IMAGE_LAYOUT_GENERAL, - .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .image = this->image, - .subresourceRange = range - }; - vkCmdPipelineBarrier( - cmd, - VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, - VK_PIPELINE_STAGE_TRANSFER_BIT, - 0, - 0, - nullptr, - 0, - nullptr, - 1, - &barrier - ); - const auto clear_color = VkClearColorValue{ - .float32 = {0.0f, 0.0f, 0.0f, 0.0f} - }; - vkCmdClearColorImage(cmd, this->image, VK_IMAGE_LAYOUT_GENERAL, &clear_color, 1, &range); - const auto barrier2 = VkImageMemoryBarrier{ - .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, - .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT, - .dstAccessMask = VK_ACCESS_SHADER_READ_BIT, - .oldLayout = VK_IMAGE_LAYOUT_GENERAL, - .newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, - .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .image = this->image, - .subresourceRange = range - }; - vkCmdPipelineBarrier( - cmd, - VK_PIPELINE_STAGE_TRANSFER_BIT, - VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, - 0, - 0, - nullptr, - 0, - nullptr, - 1, - &barrier2 - ); - }); - this->uploaded = true; - return; - } - this->upload_data(this->deferred_data.data(), this->deferred_data.size()); - this->deferred_data.clear(); - this->uploaded = true; -} - texture_2d::~texture_2d() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the factory is owned by the renderer; the base-class reference comes from the ruis API") const auto& ctx = static_cast(this->factory.get()); - // remove from the factory's texture list - ctx.unregister_texture(this); vkDestroySampler(ctx.device, this->sampler, nullptr); vkDestroyImageView(ctx.device, this->image_view, nullptr); vkDestroyImage(ctx.device, this->image, nullptr); @@ -404,88 +329,3 @@ void texture_2d::create_sampler(ruis::render::factory::texture_2d_parameters par }; check_vk_result(vkCreateSampler(ctx.device, &sampler_info, nullptr, &this->sampler), "vkCreateSampler"); } - -void texture_2d::upload_data(const uint8_t* data, size_t data_size) -{ - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast, "the factory is owned by the renderer; the base-class reference comes from the ruis API") - const auto& ctx = static_cast(this->factory.get()); - - VkBuffer staging_buffer = VK_NULL_HANDLE; - VkDeviceMemory staging_memory = VK_NULL_HANDLE; - create_staging_buffer(ctx.device, ctx.physical_device, data, data_size, staging_buffer, staging_memory); - - ctx.one_shot([&](VkCommandBuffer cmd) { - const auto w = static_cast(this->dims().x()); - const auto h = static_cast(this->dims().y()); - - // transition to transfer dst - const auto dst_barrier = VkImageMemoryBarrier{ - .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, - .srcAccessMask = 0, - .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT, - .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED, - .newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, - .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .image = this->image, - .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1} - }; - vkCmdPipelineBarrier( - cmd, - VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, - VK_PIPELINE_STAGE_TRANSFER_BIT, - 0, - 0, - nullptr, - 0, - nullptr, - 1, - &dst_barrier - ); - - const auto region = VkBufferImageCopy{ - .bufferOffset = 0, - .bufferRowLength = 0, // 0 means tightly packed (use image width); data is always RGBA - .bufferImageHeight = h, - .imageSubresource = - {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .mipLevel = 0, .baseArrayLayer = 0, .layerCount = 1}, - .imageOffset = {0, 0, 0}, - .imageExtent = {w, h, 1} - }; - vkCmdCopyBufferToImage( - cmd, // - staging_buffer, - this->image, - VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, - 1, - ®ion - ); - - // transition to shader read-only - const auto img_barrier = VkImageMemoryBarrier{ - .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, - .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT, - .dstAccessMask = VK_ACCESS_SHADER_READ_BIT, - .oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, - .newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, - .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .image = this->image, - .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1} - }; - vkCmdPipelineBarrier( - cmd, - VK_PIPELINE_STAGE_TRANSFER_BIT, - VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, - 0, - 0, - nullptr, - 0, - nullptr, - 1, - &img_barrier - ); - }); - - destroy_staging_buffer(ctx.device, staging_buffer, staging_memory); -} diff --git a/src/ruis/render/vulkan/texture_2d.hpp b/src/ruis/render/vulkan/texture_2d.hpp index ed0f60a..ef6eb8e 100644 --- a/src/ruis/render/vulkan/texture_2d.hpp +++ b/src/ruis/render/vulkan/texture_2d.hpp @@ -50,7 +50,6 @@ class texture_2d : public ruis::render::texture_2d ); // upload the data after the swapchain is ready - void ensure_uploaded(); texture_2d(const texture_2d&) = delete; texture_2d& operator=(const texture_2d&) = delete; @@ -61,7 +60,7 @@ class texture_2d : public ruis::render::texture_2d private: void create_image(rasterimage::dimensioned::dimensions_type dims); - void upload_data(const uint8_t* data, size_t data_size); + void create_sampler(ruis::render::factory::texture_2d_parameters params); // deferred upload (public for batched upload in context) From 1cae4ae5a812d6a0e7dce5156834e1ac2bbde8ad Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Sat, 21 Nov 2026 03:28:33 +0200 Subject: [PATCH 12/12] Override swap_frame_buffers() to call present() directly The Vulkan context now handles presentation through the render context's swap_frame_buffers() virtual method, eliminating the need for the native window to hold a pointer back to the context. --- src/ruis/render/vulkan/context.cpp | 5 +++++ src/ruis/render/vulkan/context.hpp | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/ruis/render/vulkan/context.cpp b/src/ruis/render/vulkan/context.cpp index 24031c9..7177422 100644 --- a/src/ruis/render/vulkan/context.cpp +++ b/src/ruis/render/vulkan/context.cpp @@ -1196,6 +1196,11 @@ rasterimage::image_variant context::readback_framebuffer_color(const ruis::rende return {std::move(im)}; } +void context::swap_frame_buffers() +{ + this->present(); +} + void context::present() { // end the render pass if in progress diff --git a/src/ruis/render/vulkan/context.hpp b/src/ruis/render/vulkan/context.hpp index b7aaa3d..9f4ef16 100644 --- a/src/ruis/render/vulkan/context.hpp +++ b/src/ruis/render/vulkan/context.hpp @@ -138,10 +138,12 @@ class context : public ruis::render::context bool is_depth_enabled() const noexcept override; void enable_depth(bool enable) override; + void swap_frame_buffers() override; + // =============================== // = vulkan-specific interface = - // present the current frame (called by the native window at the end of a frame) + // present the current frame void present(); // the packed key of the current blend state (for pipeline selection)