Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
569 changes: 101 additions & 468 deletions src/ruis/render/vulkan/context.cpp

Large diffs are not rendered by default.

98 changes: 18 additions & 80 deletions src/ruis/render/vulkan/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "shaders/shader_common.hpp"

#include "surface_provider.hpp"
#include "factory.hpp"
#include "surface.hpp"
#include "vulkan.hpp"

namespace ruis::render::vulkan {
Expand Down Expand Up @@ -87,8 +88,9 @@ class context : public ruis::render::context
{
public:
context(
const utki::shared_ref<ruis::render::native_window>& native_window, //
const utki::shared_ref<const surface_provider>& surface_provider
utki::shared_ref<ruis::render::native_window> native_window, //
utki::shared_ref<const surface> surface, //
const utki::shared_ref<const ruis::render::vulkan::factory>& factory
);
~context() override;

Expand All @@ -100,54 +102,9 @@ class context : public ruis::render::context

// ===============================
// ====== factory functions ======

utki::shared_ref<shaders> make_shaders() const override;

utki::shared_ref<ruis::render::texture_2d> make_texture_2d(
rasterimage::format format,
rasterimage::dimensioned::dimensions_type dims,
texture_2d_parameters params
) const override;

utki::shared_ref<ruis::render::texture_2d> make_texture_2d(
const rasterimage::image_variant& imvar,
texture_2d_parameters params
) const override;

utki::shared_ref<ruis::render::texture_2d> make_texture_2d(
rasterimage::image_variant&& imvar,
texture_2d_parameters params
) const override;

utki::shared_ref<ruis::render::texture_depth> make_texture_depth( //
rasterimage::dimensioned::dimensions_type dims
) const override;

utki::shared_ref<ruis::render::texture_cube> 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<ruis::render::vertex_buffer> make_vertex_buffer(utki::span<const r4::vector4<float>> vertices
) const override;
utki::shared_ref<ruis::render::vertex_buffer> make_vertex_buffer(utki::span<const r4::vector3<float>> vertices
) const override;
utki::shared_ref<ruis::render::vertex_buffer> make_vertex_buffer(utki::span<const r4::vector2<float>> vertices
) const override;
utki::shared_ref<ruis::render::vertex_buffer> make_vertex_buffer(utki::span<const float> vertices) const override;

utki::shared_ref<ruis::render::index_buffer> make_index_buffer(utki::span<const uint16_t> indices) const override;
utki::shared_ref<ruis::render::index_buffer> make_index_buffer(utki::span<const uint32_t> indices) const override;

utki::shared_ref<ruis::render::vertex_array> make_vertex_array(
std::vector<utki::shared_ref<const ruis::render::vertex_buffer>> buffers, //
utki::shared_ref<const ruis::render::index_buffer> 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<ruis::render::frame_buffer> make_framebuffer( //
std::shared_ptr<ruis::render::texture_2d> color,
Expand Down Expand Up @@ -181,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)
Expand Down Expand Up @@ -227,33 +186,29 @@ 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<void(VkCommandBuffer)> 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<const ruis::render::vulkan::factory> 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
// (the surface must outlive the swapchain)
utki::shared_ref<const surface_provider> surface_provider_ref;
utki::shared_ref<const surface> 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;

Expand Down Expand Up @@ -288,17 +243,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
Expand All @@ -308,12 +252,6 @@ class context : public ruis::render::context
std::vector<void*> 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<texture_2d*> all_textures;
bool textures_uploaded = false;
void ensure_textures_uploaded();

// DEBUG: 0 = screen (swapchain), 1 = offscreen framebuffer

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

/* ================ LICENSE END ================ */

#include "vulkan_device.hpp"
#include "device.hpp"

namespace ruis::render::vulkan {

Expand Down Expand Up @@ -52,56 +52,62 @@ 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<VkPhysicalDevice> 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");
}
physical_device = devices[0];
}
} // 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);
this->instance = VK_NULL_HANDLE;
}
}

std::unique_ptr<vulkan_device> vulkan_device::make(
std::vector<std::string> instance_extensions,
std::vector<std::string> device_extensions
)
device::device(std::vector<std::string> instance_extensions, std::vector<std::string> device_extensions) :
instance(VK_NULL_HANDLE),
physical_device(VK_NULL_HANDLE),
vk_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<vulkan_device>(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<VkQueueFamilyProperties> 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;
}
}
Expand Down Expand Up @@ -129,12 +135,10 @@ std::unique_ptr<vulkan_device> 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;
check_vk_result(vkCreateDevice(this->physical_device, &device_info, nullptr, &this->vk_device), "vkCreateDevice");

return ret;
vkGetDeviceQueue(this->vk_device, graphics_family, 0, &this->queue);
this->graphics_queue_family = graphics_family;
}

} // namespace ruis::render::vulkan
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +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 surface_providers 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
static std::unique_ptr<vulkan_device> make(
std::vector<std::string> instance_extensions,
std::vector<std::string> device_extensions
);
device(std::vector<std::string> instance_extensions, std::vector<std::string> device_extensions);

VkInstance get_instance() const
{
Expand All @@ -72,7 +69,7 @@ class vulkan_device

VkDevice get_device() const
{
return this->device;
return this->vk_device;
}

VkQueue get_queue() const
Expand All @@ -86,11 +83,9 @@ class vulkan_device
}

private:
vulkan_device() = default;

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;
Expand Down
Loading
Loading