3#include <Engine/Mesh/GltfLoader.h>
4#include <Engine/Texture/Ktx2Loader.h>
5#include <Engine/Texture/TextureType.h>
9#include <vk_mem_alloc.h>
10#include <vulkan/vulkan_core.h>
270 template <
typename T>
284 VkFormat
format = VK_FORMAT_R8G8B8A8_SRGB;
298 template <
typename T>
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 );
308 std::vector<unsigned char> paddedImage( pixelCount * newPixelBytes );
311 if constexpr ( std::is_floating_point_v<T> )
313 alphaVal =
static_cast<T
>( 1.0 );
317 alphaVal = std::numeric_limits<T>::max();
320 const unsigned char * src =
imageData.image.data();
321 unsigned char * dst = paddedImage.data();
323 for (
size_t i = 0; i < pixelCount; ++i )
325 memcpy( dst, src, oldPixelBytes );
326 memcpy( dst + oldPixelBytes, &alphaVal,
sizeof( T ) );
327 src += oldPixelBytes;
328 dst += newPixelBytes;
331 imageData.image = std::move( paddedImage );
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.
bool hasPrecomputedMipmaps_
VkImageView getVkImageView() const
Gets the Vulkan image view used for sampling.
VkSampler vkTextureSampler
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.
uint32_t getSourceWidth() const
Gets the original source texture width.
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.
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.
uint32_t getMipLevels() const
Gets the number of mip levels in the Vulkan image.
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
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.
void dilateLightmapImage()
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.
VkFormat getTextureFormat() const
Gets the Vulkan image format selected for this texture.
uint32_t getSourceHeight() const
Gets the original source texture height.
VmaAllocation textureAllocation_
VMA allocation for texture memory.
void generateMipmaps(VkCommandBuffer commandBuffer) const
Generate mipmaps for this texture using vkCmdBlitImage.
VkImageView vkTextureView
std::optional< Textures::Ktx2TextureData > ktx2Data_
void setupImageFormat(const tinygltf::Image &imageData)
Selects a Vulkan format from tinygltf image bit depth and component count.
uint32_t sourceMipLevels_
tinygltf::Image imageData
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.
@ BaseColor
Color data with gamma (SRGB format for 8-bit)
Stores all data loaded from a KTX2 file including mip chain.