vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
texture_format.h
1#pragma once
2
3#include <algorithm>
4#include <cstdint>
5
6#include "core/reflect.h"
7
8namespace Vkm::Engine {
9
10// Every enum TextureParams holds is persisted by value in cooked textures: a reorder or an addition
11// bumps COOKER_VERSION and updates the pins in writeTexture (io/asset/asset_cook.cpp).
12
18enum class TextureInternalFormat : uint8_t {
19 R8,
20 RG8,
21 RGB8,
22 RGBA8,
23 SRGB8,
24 SRGBA8,
25 RGBA16F,
26 RGBA32F,
27 BC4R,
28 BC5RG,
29 BC7RGBA,
30 BC7SRGBA
31};
32
36enum class TexturePixelFormat : uint8_t {
37 R,
38 RG,
39 RGB,
40 RGBA
41};
42
46enum class TexturePixelType : uint8_t {
47 UnsignedByte,
48 Float,
49 HalfFloat
50};
51
55enum class TextureWrapMode : uint8_t {
56 Repeat,
57 MirroredRepeat,
58 ClampToEdge,
59 ClampToBorder
60};
61
68enum class TextureFilterOverride : uint8_t {
69 None,
70 Nearest
71};
72
77 uint32_t width = 0;
78 uint32_t height = 0;
79
80 TextureInternalFormat internalFormat = TextureInternalFormat::RGBA8;
81 TexturePixelFormat format = TexturePixelFormat::RGBA;
82 TexturePixelType type = TexturePixelType::UnsignedByte;
83
84 TextureWrapMode wrapS = TextureWrapMode::ClampToEdge;
85 TextureWrapMode wrapT = TextureWrapMode::ClampToEdge;
86
87 TextureFilterOverride filterOverride = TextureFilterOverride::None;
88
89 bool generateMipmaps = true;
90
92 uint32_t mipLevels = 1;
93};
94
102enum class TextureUsage : uint8_t {
103 Color,
104 Data,
105 Normal,
106 Count
107};
108
120inline int decodeChannels(int fileChannels, TextureUsage usage) {
121 if (usage == TextureUsage::Normal) return 2;
122 if (fileChannels == 2 || (fileChannels == 1 && usage == TextureUsage::Color)) return 4;
123 return fileChannels;
124}
125
136inline TextureInternalFormat inferInternalFormat(int channels, TextureUsage usage) {
137 if (usage == TextureUsage::Color) {
138 return (channels == 3) ? TextureInternalFormat::SRGB8 : TextureInternalFormat::SRGBA8;
139 }
140 if (usage == TextureUsage::Normal) return TextureInternalFormat::RG8;
141 switch (channels) {
142 case 1: return TextureInternalFormat::R8;
143 case 3: return TextureInternalFormat::RGB8;
144 default: return TextureInternalFormat::RGBA8;
145 }
146}
147
154inline bool isSrgbFormat(TextureInternalFormat format) {
155 return format == TextureInternalFormat::SRGB8 || format == TextureInternalFormat::SRGBA8
156 || format == TextureInternalFormat::BC7SRGBA;
157}
158
165inline TextureUsage textureUsageOf(TextureInternalFormat format) {
166 if (isSrgbFormat(format)) return TextureUsage::Color;
167 if (format == TextureInternalFormat::RG8 || format == TextureInternalFormat::BC5RG) {
168 return TextureUsage::Normal;
169 }
170 return TextureUsage::Data;
171}
172
179inline uint64_t blockBytes(TextureInternalFormat format) {
180 switch (format) {
181 case TextureInternalFormat::BC4R: return 8;
182 case TextureInternalFormat::BC5RG:
183 case TextureInternalFormat::BC7RGBA:
184 case TextureInternalFormat::BC7SRGBA: return 16;
185 case TextureInternalFormat::R8:
186 case TextureInternalFormat::RG8:
187 case TextureInternalFormat::RGB8:
188 case TextureInternalFormat::RGBA8:
189 case TextureInternalFormat::SRGB8:
190 case TextureInternalFormat::SRGBA8:
191 case TextureInternalFormat::RGBA16F:
192 case TextureInternalFormat::RGBA32F: return 0;
193 }
194 return 0;
195}
196
203inline bool isCompressedFormat(TextureInternalFormat format) {
204 return blockBytes(format) != 0;
205}
206
213inline uint32_t channelCount(TexturePixelFormat format) {
214 switch (format) {
215 case TexturePixelFormat::R: return 1;
216 case TexturePixelFormat::RG: return 2;
217 case TexturePixelFormat::RGB: return 3;
218 case TexturePixelFormat::RGBA: break;
219 }
220 return 4;
221}
222
230inline uint64_t bytesPerTexel(TexturePixelFormat format, TexturePixelType type) {
231 const uint64_t channels = channelCount(format);
232 uint64_t componentBytes = 1;
233 switch (type) {
234 case TexturePixelType::UnsignedByte: componentBytes = 1; break;
235 case TexturePixelType::HalfFloat: componentBytes = 2; break;
236 case TexturePixelType::Float: componentBytes = 4; break;
237 }
238 return channels * componentBytes;
239}
240
248inline uint32_t mipChainLength(uint32_t width, uint32_t height) {
249 if (width == 0 || height == 0) return 0;
250 uint32_t extent = std::max(width, height);
251 uint32_t levels = 1;
252 while (extent > 1) {
253 extent >>= 1;
254 ++levels;
255 }
256 return levels;
257}
258
266inline uint32_t mipExtent(uint32_t extent, uint32_t level) {
267 return (level >= 32) ? 1u : std::max(extent >> level, 1u);
268}
269
276inline uint64_t textureUnitBytes(const TextureParams& params) {
277 return isCompressedFormat(params.internalFormat)
278 ? blockBytes(params.internalFormat)
279 : bytesPerTexel(params.format, params.type);
280}
281
291inline uint64_t textureLevelUnits(const TextureParams& params, uint32_t level) {
292 const uint64_t width = mipExtent(params.width, level);
293 const uint64_t height = mipExtent(params.height, level);
294 if (!isCompressedFormat(params.internalFormat)) return width * height;
295 return ((width + 3) / 4) * ((height + 3) / 4);
296}
297
308inline uint64_t textureLevelBytes(const TextureParams& params, uint32_t level) {
309 return textureLevelUnits(params, level) * textureUnitBytes(params);
310}
311
320inline bool buildsMipsAtUpload(const TextureParams& params) {
321 return params.generateMipmaps && params.mipLevels == 1 && !isCompressedFormat(params.internalFormat);
322}
323
333inline bool isMipmapped(const TextureParams& params) {
334 return params.mipLevels > 1 || buildsMipsAtUpload(params);
335}
336
343inline TexturePixelFormat inferFormat(int channels) {
344 switch (channels) {
345 case 1: return TexturePixelFormat::R;
346 case 2: return TexturePixelFormat::RG;
347 case 3: return TexturePixelFormat::RGB;
348 case 4:
349 default: return TexturePixelFormat::RGBA;
350 }
351}
352
353} // namespace Vkm::Engine
354
355VKM_ENUM_NAMES(::Vkm::Engine::TextureUsage, "Color", "Data", "Normal")
Full sampling + storage description for a texture, consumed by the backend on upload.
Definition texture_format.h:76
uint32_t mipLevels
Levels the pixel data carries, level 0 first: one, or the whole chain down to 1x1.
Definition texture_format.h:92