Hubris Engine Dev
A Project to learn and get into Game Engine developement.
 
Loading...
Searching...
No Matches
Format.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <cstddef>
5
6#define DEFINE_FORMAT(format, name, componentType, size, channels, isdepth, isSRGB) \
7{format, name, componentType, size, channels, isdepth, isSRGB }
8
9namespace Hubris::Graphics {
10
11 // Enum representing component data types.
12 enum class ComponentType {
16 UInt = 1 << 2,
17 SInt = 1 << 3,
18 Float = 1 << 4,
19 Depth = 1 << 5,
20 Stencil = 1 << 6
21 };
22
23 // Enum representing pixel/texture formats.
79
80 // Struct holding all traits of a format.
81 struct FormatInfo {
83 const char* name;
85 uint32_t size;
86 uint32_t channels;
87 bool isDepth;
88 bool isSrgb;
89 };
90
91 // Central data table. TODO: Simplify declarations using the DEFINE_FORMAT macro.
92 constexpr FormatInfo formatTable[] = {
93 { Format::Undefined, "Undefined", ComponentType::Unknown, 0, 0, false, false },
94
95 { Format::R8Unorm, "R8Unorm", ComponentType::UNorm, 1, 1, false, false },
96 { Format::R8Snorm, "R8Snorm", ComponentType::SNorm, 1, 1, false, false },
97 { Format::R8Uint, "R8Uint", ComponentType::UInt, 1, 1, false, false },
98 { Format::R8Sint, "R8Sint", ComponentType::SInt, 1, 1, false, false },
99
100 { Format::R8G8Unorm, "R8G8Unorm", ComponentType::UNorm, 2, 2, false, false },
101 { Format::R8G8Snorm, "R8G8Snorm", ComponentType::SNorm, 2, 2, false, false },
102 { Format::R8G8Uint, "R8G8Uint", ComponentType::UInt, 2, 2, false, false },
103 { Format::R8G8Sint, "R8G8Sint", ComponentType::SInt, 2, 2, false, false },
104
105 { Format::R8G8B8A8Unorm, "R8G8B8A8Unorm", ComponentType::UNorm, 4, 4, false, false },
106 { Format::R8G8B8A8Snorm, "R8G8B8A8Snorm", ComponentType::SNorm, 4, 4, false, false },
107 { Format::R8G8B8A8Uint, "R8G8B8A8Uint", ComponentType::UInt, 4, 4, false, false },
108 { Format::R8G8B8A8Sint, "R8G8B8A8Sint", ComponentType::SInt, 4, 4, false, false },
109 { Format::R8G8B8A8Srgb, "R8G8B8A8Srgb", ComponentType::UNorm, 4, 4, false, true },
110
111 { Format::B8G8R8A8Unorm, "B8G8R8A8Unorm", ComponentType::UNorm, 4, 4, false, false },
112 { Format::B8G8R8A8Srgb, "B8G8R8A8Srgb", ComponentType::UNorm, 4, 4, false, true },
113
114 { Format::A2R10G10B10Unorm, "A2R10G10B10Unorm", ComponentType::UNorm, 4, 4, false, false },
115
116 { Format::R16Float, "R16Float", ComponentType::Float, 2, 1, false, false },
117 { Format::R16Uint, "R16Uint", ComponentType::UInt, 2, 1, false, false },
118 { Format::R16Sint, "R16Sint", ComponentType::SInt, 2, 1, false, false },
119
120 { Format::R16G16Float, "R16G16Float", ComponentType::Float, 4, 2, false, false },
121 { Format::R16G16Uint, "R16G16Uint", ComponentType::UInt, 4, 2, false, false },
122 { Format::R16G16Sint, "R16G16Sint", ComponentType::SInt, 4, 2, false, false },
123
124 { Format::R16G16B16A16Float, "R16G16B16A16Float", ComponentType::Float, 8, 4, false, false },
125 { Format::R16G16B16A16Uint, "R16G16B16A16Uint", ComponentType::UInt, 8, 4, false, false },
126 { Format::R16G16B16A16Sint, "R16G16B16A16Sint", ComponentType::SInt, 8, 4, false, false },
127
128 { Format::R32Uint, "R32Uint", ComponentType::UInt, 4, 1, false, false },
129 { Format::R32Sint, "R32Sint", ComponentType::SInt, 4, 1, false, false },
130 { Format::R32Float, "R32Float", ComponentType::Float, 4, 1, false, false },
131
132 { Format::R32G32Uint, "R32G32Uint", ComponentType::UInt, 8, 2, false, false },
133 { Format::R32G32Sint, "R32G32Sint", ComponentType::SInt, 8, 2, false, false },
134 { Format::R32G32Float, "R32G32Float", ComponentType::Float, 8, 2, false, false },
135
136 { Format::R32G32B32A32Uint, "R32G32B32A32Uint", ComponentType::UInt, 16, 4, false, false },
137 { Format::R32G32B32A32Sint, "R32G32B32A32Sint", ComponentType::SInt, 16, 4, false, false },
138 { Format::R32G32B32A32Float, "R32G32B32A32Float", ComponentType::Float, 16, 4, false, false },
139
140 { Format::D16Unorm, "D16Unorm", ComponentType::Depth,2, 1, true, false },
141 { Format::D24UnormS8Uint, "D24UnormS8Uint", ComponentType::Depth,4, 2, true, false },
142 { Format::D32Sfloat, "D32Sfloat", ComponentType::Depth,4, 1, true, false },
143 { Format::D32SfloatS8Uint, "D32SfloatS8Uint", ComponentType::Depth,5, 2, true, false }
144 };
145
146 constexpr const FormatInfo* getFormatInfo(Format fmt) {
147 for (const auto& f : formatTable)
148 if (f.format == fmt)
149 return &f;
150 return nullptr;
151 }
152
153 constexpr const char* formatName(Format fmt) {
154 auto* info = getFormatInfo(fmt);
155 return info ? info->name : "Unknown";
156 }
157
159 auto* info = getFormatInfo(fmt);
160 return info ? info->type : ComponentType::Unknown;
161 }
162
163 constexpr size_t formatSize(Format fmt) {
164 auto* info = getFormatInfo(fmt);
165 return info ? info->size : 0;
166 }
167
168 constexpr uint32_t formatChannels(Format fmt) {
169 auto* info = getFormatInfo(fmt);
170 return info ? info->channels : 0;
171 }
172
173 constexpr bool isDepthFormat(Format fmt) {
174 auto* info = getFormatInfo(fmt);
175 return info ? info->isDepth : false;
176 }
177
178 constexpr bool isSrgbFormat(Format fmt) {
179 auto* info = getFormatInfo(fmt);
180 return info ? info->isSrgb : false;
181 }
182
183 // Traits wrapper.
184 template<Format Fmt>
186 static constexpr bool IsColor = !isDepthFormat(Fmt);
187 static constexpr size_t Size = formatSize(Fmt);
188 static constexpr ComponentType Type = componentType(Fmt);
189 static constexpr uint32_t Channels = formatChannels(Fmt);
190 static constexpr bool IsSrgb = isSrgbFormat(Fmt);
191 };
192
193} // namespace Hubris
Contains all graphics related classes and structs.
Definition Format.h:9
constexpr bool isDepthFormat(Format fmt)
Definition Format.h:173
constexpr bool isSrgbFormat(Format fmt)
Definition Format.h:178
constexpr FormatInfo formatTable[]
Definition Format.h:92
constexpr uint32_t formatChannels(Format fmt)
Definition Format.h:168
constexpr const char * formatName(Format fmt)
Definition Format.h:153
constexpr ComponentType componentType(Format fmt)
Definition Format.h:158
constexpr size_t formatSize(Format fmt)
Definition Format.h:163
constexpr const FormatInfo * getFormatInfo(Format fmt)
Definition Format.h:146
static constexpr ComponentType Type
Definition Format.h:188
static constexpr uint32_t Channels
Definition Format.h:189
static constexpr bool IsSrgb
Definition Format.h:190
static constexpr bool IsColor
Definition Format.h:186
static constexpr size_t Size
Definition Format.h:187