diff --git a/cpp/examples/Shakti/Vulkan/hello_vulkan_image/main.cpp b/cpp/examples/Shakti/Vulkan/hello_vulkan_image/main.cpp index 8557c136e..6c156e191 100644 --- a/cpp/examples/Shakti/Vulkan/hello_vulkan_image/main.cpp +++ b/cpp/examples/Shakti/Vulkan/hello_vulkan_image/main.cpp @@ -210,6 +210,45 @@ class VulkanImageRenderer : public kvk::GraphicsBackend private: /* Methods to initialize objects for the graphics pipeline. */ + auto init_device_and_queues() -> void override + { + // According to: + // https://stackoverflow.com/questions/61434615/in-vulkan-is-it-beneficial-for-the-graphics-queue-family-to-be-separate-from-th + // + // Using distinct queue families, namely one for the graphics operations + // and another for the present operations, does not result in better + // performance. + // + // This is because the hardware does not expose present-only queue + // families... + const auto graphics_queue_family_index = + kvk::find_graphics_queue_family_indices(_physical_device).front(); + const auto present_queue_family_index = + kvk::find_present_queue_family_indices(_physical_device, _surface) + .front(); + + // Create a logical device. + auto device_extensions = std::vector{VK_KHR_SWAPCHAIN_EXTENSION_NAME}; + if constexpr (compile_for_apple) + device_extensions.emplace_back("VK_KHR_portability_subset"); + auto physical_device_features = VkPhysicalDeviceFeatures{}; + physical_device_features.samplerAnisotropy = VK_TRUE; + _device = svk::Device::Builder{_physical_device} + .enable_device_extensions(device_extensions) + .enable_queue_families( + {graphics_queue_family_index, present_queue_family_index}) + .enable_physical_device_features(physical_device_features) + .enable_validation_layers(_validation_layers) + .create(); + + SARA_DEBUG + << "[VK] - Fetching the graphics queue from the logical device...\n"; + _graphics_queue = svk::Queue{_device, graphics_queue_family_index}; + SARA_DEBUG + << "[VK] - Fetching the present queue from the logical device...\n"; + _present_queue = svk::Queue{_device, present_queue_family_index}; + } + auto init_graphics_pipeline(GLFWwindow* window, // const std::filesystem::path& vertex_shader_path, const std::filesystem::path& fragment_shader_path) @@ -442,26 +481,22 @@ class VulkanImageRenderer : public kvk::GraphicsBackend svk::DeviceMemoryFactory{_physical_device, _device} // .allocate_for_staging_buffer(_image_staging_buffer); _image_staging_buffer.bind(_image_staging_dmem, 0); - fmt::print("Initializing staging buffer OK\n"); // Copy the image data from the host buffer to the staging device buffer. _image_staging_dmem.copy_from(_image_host.data(), _image_host.size()); - fmt::print("Copy from host to staging buffer OK\n"); // Image data as device image associated with a device memory. _image = svk::Image::Builder{_device} .sizes(VkExtent2D{w, h}) - .format(VK_FORMAT_B8G8R8A8_SRGB) + .format(VK_FORMAT_R8G8B8A8_SRGB) .tiling(VK_IMAGE_TILING_OPTIMAL) .usage(VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT) .create(); - fmt::print("Initializing device image OK\n"); _image_dmem = svk::DeviceMemoryFactory{_physical_device, _device} // .allocate_for_device_image(_image); fmt::print("Initializing device image mem OK\n"); _image.bind(_image_dmem, 0); - fmt::print("Binding device image to device mem OK\n"); } auto init_image_sampler() -> void diff --git a/cpp/src/DO/Shakti/Vulkan/Device.hpp b/cpp/src/DO/Shakti/Vulkan/Device.hpp index 55f7907dd..47a1036e4 100644 --- a/cpp/src/DO/Shakti/Vulkan/Device.hpp +++ b/cpp/src/DO/Shakti/Vulkan/Device.hpp @@ -148,8 +148,8 @@ namespace DO::Shakti::Vulkan { } //! Bind the Vulkan device features we require. - auto enable_device_features(const VkPhysicalDeviceFeatures& features = {}) - -> Builder& + auto enable_physical_device_features( + const VkPhysicalDeviceFeatures& features = {}) -> Builder& { _physical_device_features = features; _create_info.pEnabledFeatures = &_physical_device_features; diff --git a/cpp/src/DO/Shakti/Vulkan/GraphicsBackend.cpp b/cpp/src/DO/Shakti/Vulkan/GraphicsBackend.cpp index e834e55bf..fb39805d9 100644 --- a/cpp/src/DO/Shakti/Vulkan/GraphicsBackend.cpp +++ b/cpp/src/DO/Shakti/Vulkan/GraphicsBackend.cpp @@ -104,7 +104,7 @@ auto GraphicsBackend::init_device_and_queues() -> void .enable_device_extensions(device_extensions) .enable_queue_families( {graphics_queue_family_index, present_queue_family_index}) - .enable_device_features({}) + .enable_physical_device_features({}) .enable_validation_layers(_validation_layers) .create(); diff --git a/cpp/src/DO/Shakti/Vulkan/GraphicsBackend.hpp b/cpp/src/DO/Shakti/Vulkan/GraphicsBackend.hpp index c5b373513..04b60ca7c 100644 --- a/cpp/src/DO/Shakti/Vulkan/GraphicsBackend.hpp +++ b/cpp/src/DO/Shakti/Vulkan/GraphicsBackend.hpp @@ -53,7 +53,7 @@ namespace DO::Kalpana::Vulkan { auto init_physical_device() -> void; - auto init_device_and_queues() -> void; + virtual auto init_device_and_queues() -> void; auto init_swapchain(GLFWwindow* window) -> void; diff --git a/cpp/src/DO/Shakti/Vulkan/Sampler.hpp b/cpp/src/DO/Shakti/Vulkan/Sampler.hpp index 32644a242..b6abf5e82 100644 --- a/cpp/src/DO/Shakti/Vulkan/Sampler.hpp +++ b/cpp/src/DO/Shakti/Vulkan/Sampler.hpp @@ -30,7 +30,10 @@ namespace DO::Shakti::Vulkan { Sampler(const Sampler&) = delete; - Sampler(Sampler&& other) = default; + Sampler(Sampler&& other) + { + swap(other); + } ~Sampler() { @@ -43,8 +46,7 @@ namespace DO::Shakti::Vulkan { auto operator=(Sampler&& other) -> Sampler& { - _device = std::move(other._device); - _handle = std::move(other._handle); + swap(other); return *this; } @@ -58,6 +60,12 @@ namespace DO::Shakti::Vulkan { return _handle; } + auto swap(Sampler& other) -> void + { + std::swap(_device, other._device); + std::swap(_handle, other._handle); + } + private: VkDevice _device = VK_NULL_HANDLE; VkSampler _handle = VK_NULL_HANDLE; diff --git a/cpp/test/Shakti/Vulkan/test_vulkan_buffer.cpp b/cpp/test/Shakti/Vulkan/test_vulkan_buffer.cpp index 2323a5223..a51c450fc 100644 --- a/cpp/test/Shakti/Vulkan/test_vulkan_buffer.cpp +++ b/cpp/test/Shakti/Vulkan/test_vulkan_buffer.cpp @@ -88,7 +88,7 @@ BOOST_AUTO_TEST_CASE(test_staging_buffer) device_extensions.emplace_back("VK_KHR_portability_subset"); const auto device = svk::Device::Builder{physical_device} .enable_device_extensions(device_extensions) - .enable_device_features({}) + .enable_physical_device_features({}) .enable_queue_families({compute_queue_family_index}) .enable_validation_layers(validation_layers_required) .create(); @@ -207,7 +207,7 @@ BOOST_AUTO_TEST_CASE(test_device_buffer) device_extensions.emplace_back("VK_KHR_portability_subset"); const auto device = svk::Device::Builder{physical_device} .enable_device_extensions(device_extensions) - .enable_device_features({}) + .enable_physical_device_features({}) .enable_queue_families({compute_queue_family_index}) .enable_validation_layers(validation_layers_required) .create(); @@ -313,7 +313,7 @@ BOOST_AUTO_TEST_CASE(test_staging_buffer_with_factory) device_extensions.emplace_back("VK_KHR_portability_subset"); const auto device = svk::Device::Builder{physical_device} .enable_device_extensions(device_extensions) - .enable_device_features({}) + .enable_physical_device_features({}) .enable_queue_families({compute_queue_family_index}) .enable_validation_layers(validation_layers_required) .create(); @@ -405,7 +405,7 @@ BOOST_AUTO_TEST_CASE(test_device_buffer_with_factory) device_extensions.emplace_back("VK_KHR_portability_subset"); const auto device = svk::Device::Builder{physical_device} .enable_device_extensions(device_extensions) - .enable_device_features({}) + .enable_physical_device_features({}) .enable_queue_families({compute_queue_family_index}) .enable_validation_layers(validation_layers_required) .create(); @@ -495,7 +495,7 @@ BOOST_AUTO_TEST_CASE(test_uniform_buffer_with_factory) device_extensions.emplace_back("VK_KHR_portability_subset"); const auto device = svk::Device::Builder{physical_device} .enable_device_extensions(device_extensions) - .enable_device_features({}) + .enable_physical_device_features({}) .enable_queue_families({compute_queue_family_index}) .enable_validation_layers(validation_layers_required) .create(); diff --git a/cpp/test/Shakti/Vulkan/test_vulkan_command_pool.cpp b/cpp/test/Shakti/Vulkan/test_vulkan_command_pool.cpp index 5c9c209b8..ac67bc22d 100644 --- a/cpp/test/Shakti/Vulkan/test_vulkan_command_pool.cpp +++ b/cpp/test/Shakti/Vulkan/test_vulkan_command_pool.cpp @@ -115,7 +115,7 @@ BOOST_AUTO_TEST_CASE(initialize_graphics_command_pool) .enable_device_extensions(device_extensions) .enable_queue_families({graphics_queue_family_index, present_queue_family_index}) - .enable_device_features({}) + .enable_physical_device_features({}) .enable_validation_layers(validation_layers_required) .create(); BOOST_CHECK(static_cast(device) != nullptr); diff --git a/cpp/test/Shakti/Vulkan/test_vulkan_descriptor_set.cpp b/cpp/test/Shakti/Vulkan/test_vulkan_descriptor_set.cpp index 04ef2369b..7e43f0d3c 100644 --- a/cpp/test/Shakti/Vulkan/test_vulkan_descriptor_set.cpp +++ b/cpp/test/Shakti/Vulkan/test_vulkan_descriptor_set.cpp @@ -86,7 +86,7 @@ BOOST_AUTO_TEST_CASE(test_device) const auto device = svk::Device::Builder{physical_device} .enable_device_extensions(device_extensions) .enable_queue_families({compute_queue_family_index}) - .enable_device_features({}) + .enable_physical_device_features({}) .enable_validation_layers(validation_layers_required) .create(); BOOST_CHECK(static_cast(device) != nullptr); diff --git a/cpp/test/Shakti/Vulkan/test_vulkan_device.cpp b/cpp/test/Shakti/Vulkan/test_vulkan_device.cpp index 46ff4fbe3..e2364b8de 100644 --- a/cpp/test/Shakti/Vulkan/test_vulkan_device.cpp +++ b/cpp/test/Shakti/Vulkan/test_vulkan_device.cpp @@ -109,7 +109,7 @@ BOOST_AUTO_TEST_CASE(test_device) .enable_device_extensions(device_extensions) .enable_queue_families({graphics_queue_family_index, present_queue_family_index}) - .enable_device_features({}) + .enable_physical_device_features({}) .enable_validation_layers(validation_layers_required) .create(); BOOST_CHECK(static_cast(device) != nullptr); diff --git a/cpp/test/Shakti/Vulkan/test_vulkan_fence.cpp b/cpp/test/Shakti/Vulkan/test_vulkan_fence.cpp index 922f79b63..b7c6f8f86 100644 --- a/cpp/test/Shakti/Vulkan/test_vulkan_fence.cpp +++ b/cpp/test/Shakti/Vulkan/test_vulkan_fence.cpp @@ -86,7 +86,7 @@ BOOST_AUTO_TEST_CASE(test_fence) const auto device = svk::Device::Builder{physical_device} .enable_device_extensions(device_extensions) .enable_queue_families({compute_queue_family_index}) - .enable_device_features({}) + .enable_physical_device_features({}) .enable_validation_layers(validation_layers_required) .create(); BOOST_CHECK(static_cast(device) != nullptr); diff --git a/cpp/test/Shakti/Vulkan/test_vulkan_graphics_pipeline.cpp b/cpp/test/Shakti/Vulkan/test_vulkan_graphics_pipeline.cpp index 58327ad9b..10aa66d66 100644 --- a/cpp/test/Shakti/Vulkan/test_vulkan_graphics_pipeline.cpp +++ b/cpp/test/Shakti/Vulkan/test_vulkan_graphics_pipeline.cpp @@ -151,7 +151,7 @@ BOOST_AUTO_TEST_CASE(test_graphics_pipeline_build) .enable_device_extensions(device_extensions) .enable_queue_families({graphics_queue_family_index, present_queue_family_index}) - .enable_device_features({}) + .enable_physical_device_features({}) .enable_validation_layers(validation_layers_required) .create(); BOOST_CHECK(static_cast(device) != nullptr); diff --git a/cpp/test/Shakti/Vulkan/test_vulkan_image.cpp b/cpp/test/Shakti/Vulkan/test_vulkan_image.cpp index 2729ea487..0f5715a23 100644 --- a/cpp/test/Shakti/Vulkan/test_vulkan_image.cpp +++ b/cpp/test/Shakti/Vulkan/test_vulkan_image.cpp @@ -93,12 +93,13 @@ BOOST_AUTO_TEST_CASE(test_image) // Allow anisotropic sampling. auto physical_device_features = VkPhysicalDeviceFeatures{}; physical_device_features.samplerAnisotropy = VK_TRUE; - const auto device = svk::Device::Builder{physical_device} - .enable_device_extensions(device_extensions) - .enable_queue_families({compute_queue_family_index}) - .enable_device_features(physical_device_features) - .enable_validation_layers(validation_layers_required) - .create(); + const auto device = + svk::Device::Builder{physical_device} + .enable_device_extensions(device_extensions) + .enable_queue_families({compute_queue_family_index}) + .enable_physical_device_features(physical_device_features) + .enable_validation_layers(validation_layers_required) + .create(); BOOST_CHECK(static_cast(device) != VK_NULL_HANDLE); diff --git a/cpp/test/Shakti/Vulkan/test_vulkan_render_pass.cpp b/cpp/test/Shakti/Vulkan/test_vulkan_render_pass.cpp index 686a41a6a..c0f7a233f 100644 --- a/cpp/test/Shakti/Vulkan/test_vulkan_render_pass.cpp +++ b/cpp/test/Shakti/Vulkan/test_vulkan_render_pass.cpp @@ -111,7 +111,7 @@ BOOST_AUTO_TEST_CASE(test_render_pass_build) .enable_device_extensions(device_extensions) .enable_queue_families({graphics_queue_family_index, present_queue_family_index}) - .enable_device_features({}) + .enable_physical_device_features({}) .enable_validation_layers(validation_layers_required) .create(); BOOST_CHECK(static_cast(device) != nullptr); diff --git a/cpp/test/Shakti/Vulkan/test_vulkan_shader.cpp b/cpp/test/Shakti/Vulkan/test_vulkan_shader.cpp index 4fe9a5e11..dc3ef512d 100644 --- a/cpp/test/Shakti/Vulkan/test_vulkan_shader.cpp +++ b/cpp/test/Shakti/Vulkan/test_vulkan_shader.cpp @@ -96,13 +96,13 @@ BOOST_AUTO_TEST_CASE(test_vulkan_shader_module) // Create a logical device. - auto device_extensions = std::vector{}; + auto device_extensions = std::vector{}; if constexpr (compile_for_apple) device_extensions.emplace_back("VK_KHR_portability_subset"); const auto device = svk::Device::Builder{physical_device} .enable_device_extensions(device_extensions) .enable_queue_families({compute_queue_family_index}) - .enable_device_features({}) + .enable_physical_device_features({}) .enable_validation_layers(validation_layers_required) .create(); BOOST_CHECK(static_cast(device) != nullptr); diff --git a/cpp/test/Shakti/Vulkan/test_vulkan_swapchain.cpp b/cpp/test/Shakti/Vulkan/test_vulkan_swapchain.cpp index 7564c38ff..9a731d7a3 100644 --- a/cpp/test/Shakti/Vulkan/test_vulkan_swapchain.cpp +++ b/cpp/test/Shakti/Vulkan/test_vulkan_swapchain.cpp @@ -110,7 +110,7 @@ BOOST_AUTO_TEST_CASE(test_swapchain_build) .enable_device_extensions(device_extensions) .enable_queue_families({graphics_queue_family_index, present_queue_family_index}) - .enable_device_features({}) + .enable_physical_device_features({}) .enable_validation_layers(validation_layers_required) .create(); BOOST_CHECK(static_cast(device) != nullptr);