Skip to content

Commit

Permalink
[gpu-texture] Test VkSampler wrapper (squash me)
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmNotHanni committed May 12, 2023
1 parent e0f5438 commit 8ac6aca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 39 deletions.
10 changes: 4 additions & 6 deletions include/inexor/vulkan-renderer/wrapper/gpu_texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <volk.h>

Expand All @@ -20,7 +21,7 @@ class GPUMemoryBuffer;
/// @todo Support 3D textures and cube maps (implement new and separate wrappers though).
class GpuTexture {
std::unique_ptr<wrapper::Image> m_texture_image;
VkSampler m_sampler{VK_NULL_HANDLE};
std::unique_ptr<Sampler> m_sampler;

int m_texture_width{0};
int m_texture_height{0};
Expand All @@ -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.
Expand All @@ -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;
Expand All @@ -84,7 +82,7 @@ class GpuTexture {
}

[[nodiscard]] VkSampler sampler() const {
return m_sampler;
return m_sampler->sampler();
}
};

Expand Down
52 changes: 19 additions & 33 deletions src/vulkan-renderer/wrapper/gpu_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<VkSamplerCreateInfo>({
.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<Sampler>(m_device,
make_info<VkSamplerCreateInfo>({
.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

0 comments on commit 8ac6aca

Please sign in to comment.