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{
33 {
34 public:
35 friend class Core::AssetManager;
36
40 RuntimeTexture() = default;
41
46 void setupImageFormat( const tinygltf::Image & imageData );
47
61 const tinygltf::Image & imageData,
62 TextureType type,
64 );
65
77
86
93 ~RuntimeTexture() = default;
94
98 RuntimeTexture( const RuntimeTexture & ) = delete;
99
104
109 RuntimeTexture( RuntimeTexture && other ) noexcept;
110
117
122 [[nodiscard]] VkImage getVkImage() const;
123
128 [[nodiscard]] VkDeviceMemory getVkImageMemory() const;
129
134 [[nodiscard]] VkImageView getVkImageView() const;
135
140 [[nodiscard]] VkSampler getVkImageSampler() const;
141
146 [[nodiscard]] bool isDescriptorIndexInitialized() const;
147
152 [[nodiscard]] uint32_t getDescriptorIndex() const;
153
163 void createDataUploadCommand( VkCommandBuffer commandBuffer, Rendering::Renderer * renderer ) const;
164
169 [[nodiscard]] VkFormat getTextureFormat() const;
170
175 [[nodiscard]] TextureType getTextureType() const
176 {
177 return textureType_;
178 }
179
184 [[nodiscard]] uint32_t getMipLevels() const
185 {
186 return mipLevels_;
187 }
188
193 [[nodiscard]] uint32_t getSourceMipLevels() const
194 {
195 return sourceMipLevels_;
196 }
197
202 [[nodiscard]] uint32_t getSourceBaseMip() const
203 {
204 return sourceBaseMip_;
205 }
206
211 [[nodiscard]] uint32_t getSourceWidth() const
212 {
213 return sourceWidth_;
214 }
215
220 [[nodiscard]] uint32_t getSourceHeight() const
221 {
222 return sourceHeight_;
223 }
224
229 [[nodiscard]] bool hasPrecomputedMipmaps() const
230 {
232 }
233
240 static uint32_t calculateMipLevels( uint32_t width, uint32_t height );
241
255 void generateMipmaps( VkCommandBuffer commandBuffer ) const;
256
262
269
270 private:
273
274 template <typename T>
275 void addPaddingHelper();
276
277 void setDebugName( const std::string & debugName ) const;
278 void cleanup() const;
279 void reset();
280
281 VkImage vkTexture{};
282 VmaAllocation textureAllocation_ = VK_NULL_HANDLE;
283 VkImageView vkTextureView{};
284 VkSampler vkTextureSampler{};
285
286 uint32_t descriptorIndex = std::numeric_limits<uint32_t>::max();
287
288 VkFormat format = VK_FORMAT_R8G8B8A8_SRGB;
290 uint32_t mipLevels_ = 1; // how many mips does the VkImage of this texture have
291 uint32_t sourceMipLevels_ = 1; // how many mips did the original ktx texture have <= mipLevels_
292 uint32_t sourceBaseMip_ = 0; // what the base mip of the original image was
293 uint32_t sourceWidth_ = 1;
294 uint32_t sourceHeight_ = 1;
296
297 tinygltf::Image imageData;
298 std::optional<Textures::Ktx2TextureData> ktx2Data_;
300 };
301
302 template <typename T>
304 {
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 );
309
310 const size_t pixelCount = static_cast<size_t>( imageData.width ) * imageData.height;
311
312 std::vector<unsigned char> paddedImage( pixelCount * newPixelBytes );
313
314 T alphaVal;
315 if constexpr ( std::is_floating_point_v<T> )
316 {
317 alphaVal = static_cast<T>( 1.0 );
318 }
319 else
320 {
321 alphaVal = std::numeric_limits<T>::max();
322 }
323
324 const unsigned char * src = imageData.image.data();
325 unsigned char * dst = paddedImage.data();
326
327 for ( size_t i = 0; i < pixelCount; ++i )
328 {
329 memcpy( dst, src, oldPixelBytes );
330 memcpy( dst + oldPixelBytes, &alphaVal, sizeof( T ) );
331 src += oldPixelBytes;
332 dst += newPixelBytes;
333 }
334
335 imageData.image = std::move( paddedImage );
336 imageData.component = 4;
337 }
338
339} // 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
Runtime textures are move-only because they own Vulkan handles.
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:175
uint32_t getSourceWidth() const
Gets the original source texture width.
Definition Texture.h:211
bool hasPrecomputedMipmaps() const
Checks whether source data already contains mipmaps.
Definition Texture.h:229
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:202
uint32_t getMipLevels() const
Gets the number of mip levels in the Vulkan image.
Definition Texture.h:184
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
Definition Texture.h:299
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:193
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:220
VmaAllocation textureAllocation_
VMA allocation for texture memory.
Definition Texture.h:282
void generateMipmaps(VkCommandBuffer commandBuffer) const
Generate mipmaps for this texture using vkCmdBlitImage.
std::optional< Textures::Ktx2TextureData > ktx2Data_
Definition Texture.h:298
void setupImageFormat(const tinygltf::Image &imageData)
Selects a Vulkan format from tinygltf image bit depth and component count.
tinygltf::Image imageData
Definition Texture.h:297
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....
Definition Renderer.h:77
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
Manages IBL (Image-Based Lighting) resources for specular reflections.
Stores all data loaded from a KTX2 file including mip chain.
Definition Ktx2Loader.h:15