SleakEngine 0.1.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 /// Takes ownership of per-swapchain-image descriptor sets allocated from
47 /// pool, releasing any sets the texture held before.
48 void SetDescriptorSets(std::vector<VkDescriptorSet> sets,
49 VkDescriptorPool pool);
50 const std::vector<VkDescriptorSet>& GetDescriptorSets() const { return m_descriptorSets; }
51 bool HasDescriptorSets() const { return !m_descriptorSets.empty(); }
52
53 /// Stamps textures released from now on with the serial of the next
54 /// frame submission, the last one that may still reference them.
55 static void SetReleaseSerial(uint64_t serial);
56 /// Destroys released textures whose stamped submission has completed.
57 static void DestroyReleased(uint64_t completedSerial);
58 /// Destroys every released texture; the caller guarantees the GPU is idle.
59 static void DestroyAllReleased();
60
61 private:
62 /// Hands the descriptor sets and GPU objects to the release queue so they
63 /// outlive any frame still in flight.
64 void Cleanup();
65 /// Writes this texture's image view/sampler into each of its per-frame descriptor sets.
66 void UpdateDescriptorSets();
67
68 /// Finds a physical device memory type matching the filter and required properties.
69 uint32_t FindMemoryType(uint32_t typeFilter,
70 VkMemoryPropertyFlags properties);
71 /// Allocates the VkImage and its backing device memory.
72 bool CreateImage(uint32_t width, uint32_t height, VkFormat format,
73 VkImageUsageFlags usage, uint32_t mipLevels);
74 bool CreateImageView(VkFormat format);
75 bool CreateSampler();
76 /// Records a pipeline barrier transitioning the image between layouts.
77 void TransitionImageLayout(VkImage image, VkImageLayout oldLayout,
78 VkImageLayout newLayout);
79 /// Blits progressively smaller mip levels from the base image.
80 void GenerateMipmaps(VkCommandBuffer cmd, int32_t width, int32_t height);
81
82 VkDevice m_device = VK_NULL_HANDLE;
83 VkPhysicalDevice m_physicalDevice = VK_NULL_HANDLE;
84 VkCommandPool m_commandPool = VK_NULL_HANDLE;
85 VkQueue m_graphicsQueue = VK_NULL_HANDLE;
86
87 VkImage m_image = VK_NULL_HANDLE;
88 VkDeviceMemory m_imageMemory = VK_NULL_HANDLE;
89 VkImageView m_imageView = VK_NULL_HANDLE;
90 VkSampler m_sampler = VK_NULL_HANDLE;
91
92 std::vector<VkDescriptorSet> m_descriptorSets;
93 VkDescriptorPool m_descriptorPool = VK_NULL_HANDLE;
94 mutable VkDescriptorSet m_imguiDescriptorSet = VK_NULL_HANDLE;
95
96 uint32_t m_width = 0;
97 uint32_t m_height = 0;
101 float m_lodBias = 0.0f;
102 uint32_t m_mipLevels = 1; // actual generated levels
103 uint32_t m_maxMipLevels = 0; // 0 = full chain; else clamp
104};
105
106} // namespace RenderEngine
107} // namespace Sleak
108
109#endif // VULKANTEXTURE_HPP_
int width
int height
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