From 8ac6acaa244097b37fc49ad1be23f92bdd1bdeb0 Mon Sep 17 00:00:00 2001 From: Johannes Schneider Date: Sat, 13 May 2023 01:34:59 +0200 Subject: [PATCH] [gpu-texture] Test VkSampler wrapper (squash me) --- .../vulkan-renderer/wrapper/gpu_texture.hpp | 10 ++-- src/vulkan-renderer/wrapper/gpu_texture.cpp | 52 +++++++------------ 2 files changed, 23 insertions(+), 39 deletions(-) diff --git a/include/inexor/vulkan-renderer/wrapper/gpu_texture.hpp b/include/inexor/vulkan-renderer/wrapper/gpu_texture.hpp index f6ac32191..31b8158c2 100644 --- a/include/inexor/vulkan-renderer/wrapper/gpu_texture.hpp +++ b/include/inexor/vulkan-renderer/wrapper/gpu_texture.hpp @@ -4,6 +4,7 @@ #include "inexor/vulkan-renderer/wrapper/device.hpp" #include "inexor/vulkan-renderer/wrapper/gpu_memory_buffer.hpp" #include "inexor/vulkan-renderer/wrapper/image.hpp" +#include "inexor/vulkan-renderer/wrapper/sampler.hpp" #include @@ -20,7 +21,7 @@ class GPUMemoryBuffer; /// @todo Support 3D textures and cube maps (implement new and separate wrappers though). class GpuTexture { std::unique_ptr m_texture_image; - VkSampler m_sampler{VK_NULL_HANDLE}; + std::unique_ptr m_sampler; int m_texture_width{0}; int m_texture_height{0}; @@ -42,9 +43,6 @@ class GpuTexture { /// @param new_layout The new image layout. void transition_image_layout(VkImage image, VkImageLayout old_layout, VkImageLayout new_layout); - /// @brief Create the texture sampler. - void create_texture_sampler(); - public: /// @brief Construct a texture from a file. /// @param device The const reference to a device RAII wrapper instance. @@ -66,7 +64,7 @@ class GpuTexture { GpuTexture(const GpuTexture &) = delete; GpuTexture(GpuTexture &&) noexcept; - ~GpuTexture(); + ~GpuTexture() = default; GpuTexture &operator=(const GpuTexture &) = delete; GpuTexture &operator=(GpuTexture &&) = delete; @@ -84,7 +82,7 @@ class GpuTexture { } [[nodiscard]] VkSampler sampler() const { - return m_sampler; + return m_sampler->sampler(); } }; diff --git a/src/vulkan-renderer/wrapper/gpu_texture.cpp b/src/vulkan-renderer/wrapper/gpu_texture.cpp index 85b9e31bb..c4086b712 100644 --- a/src/vulkan-renderer/wrapper/gpu_texture.cpp +++ b/src/vulkan-renderer/wrapper/gpu_texture.cpp @@ -35,10 +35,6 @@ GpuTexture::GpuTexture(GpuTexture &&other) noexcept m_sampler = std::exchange(other.m_sampler, nullptr); } -GpuTexture::~GpuTexture() { - vkDestroySampler(m_device.device(), m_sampler, nullptr); -} - void GpuTexture::create_texture(void *texture_data, const std::size_t texture_size) { const VkExtent2D extent{ // Because stb_image stored the texture's width and height as a normal int, we need a cast here @@ -70,35 +66,25 @@ void GpuTexture::create_texture(void *texture_data, const std::size_t texture_si VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); }); - create_texture_sampler(); -} - -void GpuTexture::create_texture_sampler() { - VkPhysicalDeviceFeatures device_features; - vkGetPhysicalDeviceFeatures(m_device.physical_device(), &device_features); - - VkPhysicalDeviceProperties graphics_card_properties; - vkGetPhysicalDeviceProperties(m_device.physical_device(), &graphics_card_properties); - - const auto sampler_ci = make_info({ - .magFilter = VK_FILTER_LINEAR, - .minFilter = VK_FILTER_LINEAR, - .mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR, - .addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT, - .addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT, - .addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT, - .mipLodBias = 0.0f, - .anisotropyEnable = VK_FALSE, - .maxAnisotropy = 1.0f, - .compareEnable = VK_FALSE, - .compareOp = VK_COMPARE_OP_ALWAYS, - .minLod = 0.0f, - .maxLod = 0.0f, - .borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK, - .unnormalizedCoordinates = VK_FALSE, - }); - - m_device.create_sampler(sampler_ci, &m_sampler, m_name); + m_sampler = std::make_unique(m_device, + make_info({ + .magFilter = VK_FILTER_LINEAR, + .minFilter = VK_FILTER_LINEAR, + .mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR, + .addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT, + .addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT, + .addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT, + .mipLodBias = 0.0f, + .anisotropyEnable = VK_FALSE, + .maxAnisotropy = 1.0f, + .compareEnable = VK_FALSE, + .compareOp = VK_COMPARE_OP_ALWAYS, + .minLod = 0.0f, + .maxLod = 0.0f, + .borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK, + .unnormalizedCoordinates = VK_FALSE, + }), + "default sampler"); } } // namespace inexor::vulkan_renderer::wrapper