Vulkan Schnee 0.0.1 (22840dc8)
High-performance rendering engine
Loading...
Searching...
No Matches
Texture.h
1#pragma once
2#include <cstring>
3#include <Engine/Mesh/GltfLoader.h>
4#include <Engine/Texture/Ktx2Loader.h>
5#include <Engine/Texture/TextureType.h>
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{
33 {
34 public:
35 friend class Core::AssetManager;
36
40 RuntimeTexture() = default;
41
46 void setupImageFormat( const tinygltf::Image & imageData );
47
61
73
82
89 ~RuntimeTexture() = default;
90
94 RuntimeTexture( const RuntimeTexture & ) = delete;
95
100
105 RuntimeTexture( RuntimeTexture && other ) noexcept;
106
113
118 [[nodiscard]] VkImage getVkImage() const;
119
124 [[nodiscard]] VkDeviceMemory getVkImageMemory() const;
125
130 [[nodiscard]] VkImageView getVkImageView() const;
131
136 [[nodiscard]] VkSampler getVkImageSampler() const;
137
142 [[nodiscard]] bool isDescriptorIndexInitialized() const;
143
148 [[nodiscard]] uint32_t getDescriptorIndex() const;
149
159 void createDataUploadCommand( VkCommandBuffer commandBuffer, Rendering::Renderer * renderer );
160
165 [[nodiscard]] VkFormat getTextureFormat() const;
166
171 [[nodiscard]] TextureType getTextureType() const
172 {
173 return textureType_;
174 }
175
180 [[nodiscard]] uint32_t getMipLevels() const
181 {
182 return mipLevels_;
183 }
184
189 [[nodiscard]] uint32_t getSourceMipLevels() const
190 {
191 return sourceMipLevels_;
192 }
193
198 [[nodiscard]] uint32_t getSourceBaseMip() const
199 {
200 return sourceBaseMip_;
201 }
202
207 [[nodiscard]] uint32_t getSourceWidth() const
208 {
209 return sourceWidth_;
210 }
211
216 [[nodiscard]] uint32_t getSourceHeight() const
217 {
218 return sourceHeight_;
219 }
220
225 [[nodiscard]] bool hasPrecomputedMipmaps() const
226 {
228 }
229
236 static uint32_t calculateMipLevels( uint32_t width, uint32_t height );
237
251 void generateMipmaps( VkCommandBuffer commandBuffer ) const;
252
258
265
266 private:
269
270 template <typename T>
271 void addPaddingHelper();
272
273 void setDebugName( const std::string & debugName ) const;
274 void cleanup() const;
275 void reset();
276
277 VkImage vkTexture{};
278 VmaAllocation textureAllocation_ = VK_NULL_HANDLE;
279 VkImageView vkTextureView{};
280 VkSampler vkTextureSampler{};
281
282 uint32_t descriptorIndex = std::numeric_limits<uint32_t>::max();
283
284 VkFormat format = VK_FORMAT_R8G8B8A8_SRGB;
286 uint32_t mipLevels_ = 1; // how many mips does the VkImage of this texture have
287 uint32_t sourceMipLevels_ = 1; // how many mips did the original ktx texture have <= mipLevels_
288 uint32_t sourceBaseMip_ = 0; // what the base mip of the original image was
289 uint32_t sourceWidth_ = 1;
290 uint32_t sourceHeight_ = 1;
292
293 tinygltf::Image imageData;
294 std::optional<Textures::Ktx2TextureData> ktx2Data_;
296 };
297
298 template <typename T>
300 {
301 constexpr size_t oldComponents = 3;
302 constexpr size_t newComponents = 4;
303 constexpr size_t oldPixelBytes = oldComponents * sizeof( T );
304 constexpr size_t newPixelBytes = newComponents * sizeof( T );
305
306 const size_t pixelCount = static_cast<size_t>( imageData.width ) * imageData.height;
307
308 std::vector<unsigned char> paddedImage( pixelCount * newPixelBytes );
309
310 T alphaVal;
311 if constexpr ( std::is_floating_point_v<T> )
312 {
313 alphaVal = static_cast<T>( 1.0 );
314 }
315 else
316 {
317 alphaVal = std::numeric_limits<T>::max();
318 }
319
320 const unsigned char * src = imageData.image.data();
321 unsigned char * dst = paddedImage.data();
322
323 for ( size_t i = 0; i < pixelCount; ++i )
324 {
325 memcpy( dst, src, oldPixelBytes );
326 memcpy( dst + oldPixelBytes, &alphaVal, sizeof( T ) );
327 src += oldPixelBytes;
328 dst += newPixelBytes;
329 }
330
331 imageData.image = std::move( paddedImage );
332 imageData.component = 4;
333 }
334
335} // namespace Engine::Assets
RuntimeTexture & operator=(const RuntimeTexture &)=delete
Runtime textures are move-only because they own Vulkan handles.
RuntimeTexture(tinygltf::Image imageData, TextureType type, Core::ApplicationContext *context)
Construct a texture with explicit type for correct format selection.
VkImageView getVkImageView() const
Gets the Vulkan image view used for sampling.
RuntimeTexture(RuntimeTexture &&other) noexcept
Move-constructs a texture and transfers Vulkan handles and prepared upload data.
static uint32_t calculateMipLevels(uint32_t width, uint32_t height)
Calculates the full mip chain length for a texture size.
~RuntimeTexture()=default
Destroys the C++ object without automatically releasing Vulkan resources.
void setDebugName(const std::string &debugName) const
TextureType getTextureType() const
Get the semantic texture type.
Definition Texture.h:171
uint32_t getSourceWidth() const
Gets the original source texture width.
Definition Texture.h:207
RuntimeTexture(tinygltf::Image imageData, Core::ApplicationContext *context)
Construct a texture with default type (BaseColor/SRGB)
bool hasPrecomputedMipmaps() const
Checks whether source data already contains mipmaps.
Definition Texture.h:225
void createResources()
Create Vulkan resources (VkImage, VkImageView, VkSampler) for this texture. Safe to call from worker ...
uint32_t getDescriptorIndex() const
Gets the descriptor array slot assigned by the asset manager.
VkImage getVkImage() const
Gets the Vulkan image handle.
uint32_t getSourceBaseMip() const
Gets the base mip represented by the source texture data.
Definition Texture.h:198
uint32_t getMipLevels() const
Gets the number of mip levels in the Vulkan image.
Definition Texture.h:180
void createDataUploadCommand(VkCommandBuffer commandBuffer, Rendering::Renderer *renderer)
Records commands that upload prepared image data into the Vulkan texture.
RuntimeTexture & operator=(RuntimeTexture &&other) noexcept
Move-assigns a texture and transfers Vulkan handles and prepared upload data.
VkSampler getVkImageSampler() const
Gets the sampler used for shader reads.
Core::ApplicationContext * context
Definition Texture.h:295
RuntimeTexture()=default
Creates an empty texture with no image data or Vulkan resources.
RuntimeTexture(Textures::Ktx2TextureData ktx2Data, TextureType type, Core::ApplicationContext *context)
Construct a texture from KTX2 data with pre-computed mipmaps.
RuntimeTexture(const RuntimeTexture &)=delete
Runtime textures are move-only because they own Vulkan handles.
VkDeviceMemory getVkImageMemory() const
Gets the device memory backing the VMA image allocation.
uint32_t getSourceMipLevels() const
Gets the number of mip levels present in the source texture data.
Definition Texture.h:189
VkFormat getTextureFormat() const
Gets the Vulkan image format selected for this texture.
uint32_t getSourceHeight() const
Gets the original source texture height.
Definition Texture.h:216
VmaAllocation textureAllocation_
VMA allocation for texture memory.
Definition Texture.h:278
void generateMipmaps(VkCommandBuffer commandBuffer) const
Generate mipmaps for this texture using vkCmdBlitImage.
std::optional< Textures::Ktx2TextureData > ktx2Data_
Definition Texture.h:294
void setupImageFormat(const tinygltf::Image &imageData)
Selects a Vulkan format from tinygltf image bit depth and component count.
tinygltf::Image imageData
Definition Texture.h:293
bool isDescriptorIndexInitialized() const
Checks whether this texture has a descriptor array slot.
void discardPreparedResources()
Destroy Vulkan resources without registering the texture.
The application context is the core class which stores the basic openxr and vulkan objects.
Coordinates engine asset loading pipelines and runtime texture descriptor ownership.
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
Stores all data loaded from a KTX2 file including mip chain.
Definition Ktx2Loader.h:16