SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
VulkanTexture.hpp
Go to the documentation of this file.
1#ifndef VULKANTEXTURE_HPP_
2#define VULKANTEXTURE_HPP_
3
4#include <Core/OSDef.hpp>
5#include <Runtime/Texture.hpp>
6#include <vulkan/vulkan.h>
7
8namespace Sleak {
9namespace RenderEngine {
10
11/// Vulkan 2D texture: image + view + sampler, with per-swapchain-image descriptor sets.
12class ENGINE_API VulkanTexture : public Texture {
13public:
14 VulkanTexture(VkDevice device, VkPhysicalDevice physicalDevice,
15 VkCommandPool commandPool, VkQueue graphicsQueue);
16 ~VulkanTexture() override;
17
18 /// Uploads raw pixel data through a staging buffer and generates mips.
19 bool LoadFromMemory(const void* data, uint32_t width, uint32_t height,
20 TextureFormat format) override;
21 /// Decodes an image file and uploads it as a new Vulkan image.
22 bool LoadFromFile(const std::string& filePath) override;
23
24 void Bind(uint32_t slot = 0) const override;
25 void Unbind() const override;
26
27 void SetFilter(TextureFilter filter) override;
28 void SetWrapMode(TextureWrapMode wrapMode) override;
29 void SetLodBias(float bias) override;
30 float GetLodBias() const override { return m_lodBias; }
31
32 // Cap generated mip levels (0 = full chain). Set before Load*; used to
33 // stop atlas tiles bleeding into each other at distant mips.
34 void SetMaxMipLevels(uint32_t levels) { m_maxMipLevels = levels; }
35
36 uint32_t GetWidth() const override { return m_width; }
37 uint32_t GetHeight() const override { return m_height; }
38 TextureFormat GetFormat() const override { return m_format; }
39 TextureType GetType() const override { return TextureType::Texture2D; }
40
41 uint64_t GetImGuiTextureID() const override;
42
43 VkImageView GetImageView() const { return m_imageView; }
44 VkSampler GetSampler() const { return m_sampler; }
45
46 // Per-texture descriptor sets (one per swapchain image)
47 void SetDescriptorSets(std::vector<VkDescriptorSet> sets) { m_descriptorSets = std::move(sets); }
48 const std::vector<VkDescriptorSet>& GetDescriptorSets() const { return m_descriptorSets; }
49 bool HasDescriptorSets() const { return !m_descriptorSets.empty(); }
50
51private:
52 void Cleanup();
53 /// Writes this texture's image view/sampler into each of its per-frame descriptor sets.
54 void UpdateDescriptorSets();
55
56 /// Finds a physical device memory type matching the filter and required properties.
57 uint32_t FindMemoryType(uint32_t typeFilter,
58 VkMemoryPropertyFlags properties);
59 /// Allocates the VkImage and its backing device memory.
60 bool CreateImage(uint32_t width, uint32_t height, VkFormat format,
61 VkImageUsageFlags usage, uint32_t mipLevels);
62 bool CreateImageView(VkFormat format);
63 bool CreateSampler();
64 /// Records a pipeline barrier transitioning the image between layouts.
65 void TransitionImageLayout(VkImage image, VkImageLayout oldLayout,
66 VkImageLayout newLayout);
67 /// Blits progressively smaller mip levels from the base image.
68 void GenerateMipmaps(VkCommandBuffer cmd, int32_t width, int32_t height);
69
70 VkDevice m_device = VK_NULL_HANDLE;
71 VkPhysicalDevice m_physicalDevice = VK_NULL_HANDLE;
72 VkCommandPool m_commandPool = VK_NULL_HANDLE;
73 VkQueue m_graphicsQueue = VK_NULL_HANDLE;
74
75 VkImage m_image = VK_NULL_HANDLE;
76 VkDeviceMemory m_imageMemory = VK_NULL_HANDLE;
77 VkImageView m_imageView = VK_NULL_HANDLE;
78 VkSampler m_sampler = VK_NULL_HANDLE;
79
80 std::vector<VkDescriptorSet> m_descriptorSets;
81 mutable VkDescriptorSet m_imguiDescriptorSet = VK_NULL_HANDLE;
82
83 uint32_t m_width = 0;
84 uint32_t m_height = 0;
88 float m_lodBias = 0.0f;
89 uint32_t m_mipLevels = 1; // actual generated levels
90 uint32_t m_maxMipLevels = 0; // 0 = full chain; else clamp
91};
92
93} // namespace RenderEngine
94} // namespace Sleak
95
96#endif // VULKANTEXTURE_HPP_
int width
int height
void SetDescriptorSets(std::vector< VkDescriptorSet > sets)
const std::vector< VkDescriptorSet > & GetDescriptorSets() const
void Bind(uint32_t slot=0) const override
TextureType GetType() const override
VulkanTexture(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, VkQueue graphicsQueue)
uint32_t GetWidth() const override
TextureFormat GetFormat() const override
void SetWrapMode(TextureWrapMode wrapMode) override
void SetLodBias(float bias) override
uint32_t GetHeight() const override
void SetFilter(TextureFilter filter) override
float GetLodBias() const override
bool LoadFromFile(const std::string &filePath) override
Decodes an image file and uploads it as a new Vulkan image.
bool LoadFromMemory(const void *data, uint32_t width, uint32_t height, TextureFormat format) override
Uploads raw pixel data through a staging buffer and generates mips.
void SetMaxMipLevels(uint32_t levels)
TextureFormat
Definition Texture.hpp:10
TextureFilter
Definition Texture.hpp:28
TextureType
Definition Texture.hpp:20
TextureWrapMode
Definition Texture.hpp:43
Backend-facing rendering layer shared by the four graphics backends.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10