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 <filesystem>
3#include <tiny_gltf.h>
4#include <vulkan/vulkan_core.h>
5#include <vk_mem_alloc.h>
8
9namespace EngineCore
10{
11class AssetManager;
12class Engine;
13struct TextureLoadData;
15class Renderer;
16} // namespace EngineCore
17
18namespace EngineCore
19{
21{
22 public:
24 // load image from path
25 Texture() = default;
26 void setupImageFormat(const tinygltf::Image& imageData);
27
43 Texture(const tinygltf::Image& imageData, TextureType type, ApplicationContext* context);
44
52 Texture(const tinygltf::Image& imageData, ApplicationContext* context);
53 ~Texture();
54
55 static std::string createTexturePath(const std::filesystem::path& assetPath,
56 const GltfLoader::GltfMeshPrimitiveData& primitiveData);
57
58 // Delete copy constructor and copy assignment operator
59 Texture(const Texture&) = delete;
60 Texture& operator=(const Texture&) = delete;
61
62 // Move constructor
63 Texture(Texture&& other) noexcept;
64 Texture& operator=(Texture&& other) noexcept;
65
66 [[nodiscard]] VkImage getVkImage() const;
67 [[nodiscard]] VkDeviceMemory getVkImageMemory() const;
68 [[nodiscard]] VkImageView getVkImageView() const;
69 [[nodiscard]] VkSampler getVkImageSampler() const;
70
71 [[nodiscard]] bool isDescriptorIndexInitialized() const;
72 [[nodiscard]] uint32_t getDescriptorIndex() const;
73
74 void createDataUploadCommand(VkCommandBuffer commandBuffer, Renderer* renderer) const;
75
76 [[nodiscard]] VkFormat getTextureFormat() const;
77
82 [[nodiscard]] TextureType getTextureType() const { return textureType_; }
83
84 private:
85 void addPaddingToImage();
86
87 template <typename T> void addPaddingHelper();
88
89 void setDebugName(const std::string& debugName);
90 void createResources();
91 void cleanup() const;
92 void reset();
93
94 VkImage vkTexture{};
95 VmaAllocation textureAllocation_ = VK_NULL_HANDLE;
96 VkImageView vkTextureView{};
97 VkSampler vkTextureSampler{};
98
99 uint32_t descriptorIndex = std::numeric_limits<uint32_t>::max();
100
101 VkFormat format = VK_FORMAT_R8G8B8A8_SRGB;
103
104 tinygltf::Image imageData;
106};
107
108template<typename T>
110{
111 size_t bytesPerComponent = sizeof(T);
112 size_t oldPixelSizeBytes = 3 * bytesPerComponent; // 3 components * T_size bytes/component
113 size_t newPixelSizeBytes = 4 * bytesPerComponent; // 4 components * T_size bytes/component
114
115 std::vector<unsigned char> paddedImage;
116 paddedImage.reserve(imageData.width * imageData.height * newPixelSizeBytes);
117
118 // Determine opaque alpha value based on type
119 T alphaVal;
120 if constexpr (std::is_floating_point_v<T>) {
121 alphaVal = 1.0f; // Standard opaque for float textures
122 } else {
123 alphaVal = std::numeric_limits<T>::max(); // Max value for integer types
124 }
125
126 // Get byte representation of alpha value
127 unsigned char alphaBytes[sizeof(T)];
128 memcpy(alphaBytes, &alphaVal, sizeof(T));
129
130 // Iterate through original image data and create padded version
131 // Assume tinygltf::Image::image.size() is correct for original 3-component data
132 for (size_t i = 0; i < imageData.image.size(); i += oldPixelSizeBytes)
133 {
134 // Copy R, G, B bytes
135 for (size_t j = 0; j < oldPixelSizeBytes; ++j)
136 {
137 paddedImage.push_back(imageData.image[i + j]);
138 }
139 // Append A bytes
140 for (size_t j = 0; j < sizeof(T); ++j)
141 {
142 paddedImage.push_back(alphaBytes[j]);
143 }
144 }
145
146 // Replace original image data with padded data
147 imageData.image = std::move(paddedImage); // Use move for efficiency
148 imageData.component = 4; // Update component count
149 // imageData.bits remains the same (e.g., 8, 16, 32)
150}
151
152} // namespace EngineCore
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:72
TextureType textureType_
Definition Texture.h:102
VmaAllocation textureAllocation_
VMA allocation for texture memory.
Definition Texture.h:95
VkDeviceMemory getVkImageMemory() const
Definition Texture.cpp:254
VkImage vkTexture
Definition Texture.h:94
uint32_t descriptorIndex
Definition Texture.h:99
void setupImageFormat(const tinygltf::Image &imageData)
Definition Texture.cpp:13
TextureType getTextureType() const
Get the semantic texture type.
Definition Texture.h:82
tinygltf::Image imageData
Definition Texture.h:104
void cleanup() const
Definition Texture.cpp:227
Texture(const Texture &)=delete
Texture & operator=(const Texture &)=delete
void setDebugName(const std::string &debugName)
Definition Texture.cpp:207
VkImageView vkTextureView
Definition Texture.h:96
VkSampler getVkImageSampler() const
Definition Texture.cpp:270
static std::string createTexturePath(const std::filesystem::path &assetPath, const GltfLoader::GltfMeshPrimitiveData &primitiveData)
Definition Texture.cpp:219
VkSampler vkTextureSampler
Definition Texture.h:97
bool isDescriptorIndexInitialized() const
Definition Texture.cpp:274
VkImage getVkImage() const
Definition Texture.cpp:249
void addPaddingHelper()
Definition Texture.h:109
friend AssetManager
Definition Texture.h:23
VkImageView getVkImageView() const
Definition Texture.cpp:265
void createDataUploadCommand(VkCommandBuffer commandBuffer, Renderer *renderer) const
Definition Texture.cpp:282
uint32_t getDescriptorIndex() const
Definition Texture.cpp:278
VkFormat getTextureFormat() const
Definition Texture.cpp:383
ApplicationContext * context
Definition Texture.h:105
Log category system implementation.
TextureType
Represents the semantic type of a texture, determining its Vulkan format.
Definition TextureType.h:18
@ BaseColor
Color data with gamma (SRGB format for 8-bit)
Definition TextureType.h:19
Stores intermediate data from loading the asset from disk. These are the vertices,...
Definition GltfLoader.h:350