9#include <vk_mem_alloc.h>
10#include <vulkan/vulkan_core.h>
274 template <
typename T>
288 VkFormat
format = VK_FORMAT_R8G8B8A8_SRGB;
302 template <
typename T>
305 constexpr size_t oldComponents = 3;
306 constexpr size_t newComponents = 4;
307 constexpr size_t oldPixelBytes = oldComponents *
sizeof( T );
308 constexpr size_t newPixelBytes = newComponents *
sizeof( T );
312 std::vector<unsigned char> paddedImage( pixelCount * newPixelBytes );
315 if constexpr ( std::is_floating_point_v<T> )
317 alphaVal =
static_cast<T
>( 1.0 );
321 alphaVal = std::numeric_limits<T>::max();
324 const unsigned char * src =
imageData.image.data();
325 unsigned char * dst = paddedImage.data();
327 for (
size_t i = 0; i < pixelCount; ++i )
329 memcpy( dst, src, oldPixelBytes );
330 memcpy( dst + oldPixelBytes, &alphaVal,
sizeof( T ) );
331 src += oldPixelBytes;
332 dst += newPixelBytes;
335 imageData.image = std::move( paddedImage );
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
Runtime textures are move-only because they own Vulkan handles.
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.
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.
RuntimeTexture(const tinygltf::Image &imageData, Core::ApplicationContext *context)
Construct a texture with default type (BaseColor/SRGB)
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
void createDataUploadCommand(VkCommandBuffer commandBuffer, Rendering::Renderer *renderer) const
Records commands that upload prepared image data into the Vulkan texture.
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.
The renderer is the main class for rendering. It owns all data which is used any time in any frame....
TextureType
Represents the semantic type of a texture, determining its Vulkan format.
@ BaseColor
Color data with gamma (SRGB format for 8-bit)
Manages IBL (Image-Based Lighting) resources for specular reflections.
Stores all data loaded from a KTX2 file including mip chain.