Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
Texture.h
Go to the documentation of this file.
1#pragma once
2#include <cstring>
6#include <filesystem>
7#include <optional>
8#include <tiny_gltf.h>
9#include <vk_mem_alloc.h>
10#include <vulkan/vulkan_core.h>
11
12namespace Engine::Rendering
13{
14 class Renderer;
15} // namespace Engine::Rendering
16
17namespace Engine::Core
18{
20 class AssetManager;
21} // namespace Engine::Core
22
23namespace Engine::Assets
24{
26 {
27 public:
28 friend class Core::AssetManager;
29 // load image from path
30 RuntimeTexture() = default;
31 void setupImageFormat( const tinygltf::Image & imageData );
32
46 const tinygltf::Image & imageData,
47 TextureType type,
49 );
50
62
71 ~RuntimeTexture() = default;
72
73 // Delete copy constructor and copy assignment operator
74 RuntimeTexture( const RuntimeTexture & ) = delete;
76
77 // Move constructor
78 RuntimeTexture( RuntimeTexture && other ) noexcept;
79 RuntimeTexture & operator=( RuntimeTexture && other ) noexcept;
80
81 [[nodiscard]] VkImage getVkImage() const;
82 [[nodiscard]] VkDeviceMemory getVkImageMemory() const;
83 [[nodiscard]] VkImageView getVkImageView() const;
84 [[nodiscard]] VkSampler getVkImageSampler() const;
85
86 [[nodiscard]] bool isDescriptorIndexInitialized() const;
87 [[nodiscard]] uint32_t getDescriptorIndex() const;
88
89 void createDataUploadCommand( VkCommandBuffer commandBuffer, Rendering::Renderer * renderer ) const;
90
91 [[nodiscard]] VkFormat getTextureFormat() const;
92
97 [[nodiscard]] TextureType getTextureType() const
98 {
99 return textureType_;
100 }
101
102 [[nodiscard]] uint32_t getMipLevels() const
103 {
104 return mipLevels_;
105 }
106
107 [[nodiscard]] uint32_t getSourceMipLevels() const
108 {
109 return sourceMipLevels_;
110 }
111
112 [[nodiscard]] uint32_t getSourceBaseMip() const
113 {
114 return sourceBaseMip_;
115 }
116
117 [[nodiscard]] uint32_t getSourceWidth() const
118 {
119 return sourceWidth_;
120 }
121
122 [[nodiscard]] uint32_t getSourceHeight() const
123 {
124 return sourceHeight_;
125 }
126
127 [[nodiscard]] bool hasPrecomputedMipmaps() const
128 {
130 }
131
132 static uint32_t calculateMipLevels( uint32_t width, uint32_t height );
133
147 void generateMipmaps( VkCommandBuffer commandBuffer ) const;
148
154
161
162 private:
165
166 template <typename T>
167 void addPaddingHelper();
168
169 void setDebugName( const std::string & debugName ) const;
170 void cleanup() const;
171 void reset();
172
173 VkImage vkTexture{};
174 VmaAllocation textureAllocation_ = VK_NULL_HANDLE;
175 VkImageView vkTextureView{};
176 VkSampler vkTextureSampler{};
177
178 uint32_t descriptorIndex = std::numeric_limits<uint32_t>::max();
179
180 VkFormat format = VK_FORMAT_R8G8B8A8_SRGB;
182 uint32_t mipLevels_ = 1; // how many mips does the VkImage of this texture have
183 uint32_t sourceMipLevels_ = 1; // how many mips did the original ktx texture have <= mipLevels_
184 uint32_t sourceBaseMip_ = 0; // what the base mip of the original image was
185 uint32_t sourceWidth_ = 1;
186 uint32_t sourceHeight_ = 1;
188
189 tinygltf::Image imageData;
190 std::optional<Textures::Ktx2TextureData> ktx2Data_;
192 };
193
194 template <typename T>
196 {
197 constexpr size_t oldComponents = 3;
198 constexpr size_t newComponents = 4;
199 constexpr size_t oldPixelBytes = oldComponents * sizeof( T );
200 constexpr size_t newPixelBytes = newComponents * sizeof( T );
201
202 const size_t pixelCount = static_cast<size_t>( imageData.width ) * imageData.height;
203
204 std::vector<unsigned char> paddedImage( pixelCount * newPixelBytes );
205
206 T alphaVal;
207 if constexpr ( std::is_floating_point_v<T> )
208 {
209 alphaVal = static_cast<T>( 1.0 );
210 }
211 else
212 {
213 alphaVal = std::numeric_limits<T>::max();
214 }
215
216 const unsigned char * src = imageData.image.data();
217 unsigned char * dst = paddedImage.data();
218
219 for ( size_t i = 0; i < pixelCount; ++i )
220 {
221 memcpy( dst, src, oldPixelBytes );
222 memcpy( dst + oldPixelBytes, &alphaVal, sizeof( T ) );
223 src += oldPixelBytes;
224 dst += newPixelBytes;
225 }
226
227 imageData.image = std::move( paddedImage );
228 imageData.component = 4;
229 }
230
231} // namespace Engine::Assets
RuntimeTexture(const tinygltf::Image &imageData, TextureType type, Core::ApplicationContext *context)
Construct a texture with explicit type for correct format selection.
RuntimeTexture & operator=(const RuntimeTexture &)=delete
VkImageView getVkImageView() const
RuntimeTexture(RuntimeTexture &&other) noexcept
static uint32_t calculateMipLevels(uint32_t width, uint32_t height)
void setDebugName(const std::string &debugName) const
TextureType getTextureType() const
Get the semantic texture type.
Definition Texture.h:97
uint32_t getSourceWidth() const
Definition Texture.h:117
bool hasPrecomputedMipmaps() const
Definition Texture.h:127
void createResources()
Create Vulkan resources (VkImage, VkImageView, VkSampler) for this texture. Safe to call from worker ...
uint32_t getDescriptorIndex() const
uint32_t getSourceBaseMip() const
Definition Texture.h:112
uint32_t getMipLevels() const
Definition Texture.h:102
RuntimeTexture(const tinygltf::Image &imageData, Core::ApplicationContext *context)
Construct a texture with default type (BaseColor/SRGB)
RuntimeTexture & operator=(RuntimeTexture &&other) noexcept
VkSampler getVkImageSampler() const
Core::ApplicationContext * context
Definition Texture.h:191
RuntimeTexture(Textures::Ktx2TextureData ktx2Data, TextureType type, Core::ApplicationContext *context)
Construct a texture from KTX2 data with pre-computed mipmaps.
RuntimeTexture(const RuntimeTexture &)=delete
VkDeviceMemory getVkImageMemory() const
uint32_t getSourceMipLevels() const
Definition Texture.h:107
VkFormat getTextureFormat() const
uint32_t getSourceHeight() const
Definition Texture.h:122
VmaAllocation textureAllocation_
VMA allocation for texture memory.
Definition Texture.h:174
void generateMipmaps(VkCommandBuffer commandBuffer) const
Generate mipmaps for this texture using vkCmdBlitImage.
std::optional< Textures::Ktx2TextureData > ktx2Data_
Definition Texture.h:190
void setupImageFormat(const tinygltf::Image &imageData)
tinygltf::Image imageData
Definition Texture.h:189
void createDataUploadCommand(VkCommandBuffer commandBuffer, Rendering::Renderer *renderer) const
bool isDescriptorIndexInitialized() const
void discardPreparedResources()
Destroy Vulkan resources without registering the texture.
The application context is the core class which stores the basic openxr and vulkan objects.
The renderer is the main class for rendering. It owns all data which is used any time in any frame....
Definition Renderer.h:71
TextureType
Represents the semantic type of a texture, determining its Vulkan format.
Definition TextureType.h:15
@ BaseColor
Color data with gamma (SRGB format for 8-bit)
Definition TextureType.h:16
Core audio subsystem owning the miniaudio engine and managing playback.
Definition AudioConfig.h:9
Stores all data loaded from a KTX2 file including mip chain.
Definition Ktx2Loader.h:15