Overte C++ Documentation
KHR.h
1 //
2 // Created by Bradley Austin Davis on 2017/05/13
3 // Copyright 2017 High Fidelity, Inc.
4 //
5 // Distributed under the Apache License, Version 2.0.
6 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
7 //
8 
9 #pragma once
10 #ifndef khronos_khr_hpp
11 #define khronos_khr_hpp
12 
13 #include <unordered_map>
14 #include <stdexcept>
15 
16 namespace khronos {
17 
18  namespace gl {
19 
20  enum class Type : uint32_t {
21  // GL 4.4 Table 8.2
22  UNSIGNED_BYTE = 0x1401,
23  BYTE = 0x1400,
24  UNSIGNED_SHORT = 0x1403,
25  SHORT = 0x1402,
26  UNSIGNED_INT = 0x1405,
27  INT = 0x1404,
28  HALF_FLOAT = 0x140B,
29  FLOAT = 0x1406,
30  UNSIGNED_BYTE_3_3_2 = 0x8032,
31  UNSIGNED_BYTE_2_3_3_REV = 0x8362,
32  UNSIGNED_SHORT_5_6_5 = 0x8363,
33  UNSIGNED_SHORT_5_6_5_REV = 0x8364,
34  UNSIGNED_SHORT_4_4_4_4 = 0x8033,
35  UNSIGNED_SHORT_4_4_4_4_REV = 0x8365,
36  UNSIGNED_SHORT_5_5_5_1 = 0x8034,
37  UNSIGNED_SHORT_1_5_5_5_REV = 0x8366,
38  UNSIGNED_INT_8_8_8_8 = 0x8035,
39  UNSIGNED_INT_8_8_8_8_REV = 0x8367,
40  UNSIGNED_INT_10_10_10_2 = 0x8036,
41  UNSIGNED_INT_2_10_10_10_REV = 0x8368,
42  UNSIGNED_INT_24_8 = 0x84FA,
43  UNSIGNED_INT_10F_11F_11F_REV = 0x8C3B,
44  UNSIGNED_INT_5_9_9_9_REV = 0x8C3E,
45  FLOAT_32_UNSIGNED_INT_24_8_REV = 0x8DAD,
46  };
47 
48  namespace texture {
49 
50  enum class Format : uint32_t {
51  COMPRESSED_FORMAT = 0,
52 
53  // GL 4.4 Table 8.3
54  STENCIL_INDEX = 0x1901,
55  DEPTH_COMPONENT = 0x1902,
56  DEPTH_STENCIL = 0x84F9,
57 
58  RED = 0x1903,
59  GREEN = 0x1904,
60  BLUE = 0x1905,
61  RG = 0x8227,
62  RGB = 0x1907,
63  RGBA = 0x1908,
64  BGR = 0x80E0,
65  BGRA = 0x80E1,
66 
67  RG_INTEGER = 0x8228,
68  RED_INTEGER = 0x8D94,
69  GREEN_INTEGER = 0x8D95,
70  BLUE_INTEGER = 0x8D96,
71  RGB_INTEGER = 0x8D98,
72  RGBA_INTEGER = 0x8D99,
73  BGR_INTEGER = 0x8D9A,
74  BGRA_INTEGER = 0x8D9B,
75  };
76 
77  enum class InternalFormat : uint32_t {
78  // GL 4.4 Table 8.12
79  R8 = 0x8229,
80  R8_SNORM = 0x8F94,
81 
82  R16 = 0x822A,
83  R16_SNORM = 0x8F98,
84 
85  RG8 = 0x822B,
86  RG8_SNORM = 0x8F95,
87 
88  RG16 = 0x822C,
89  RG16_SNORM = 0x8F99,
90 
91  R3_G3_B2 = 0x2A10,
92  RGB4 = 0x804F,
93  RGB5 = 0x8050,
94  RGB565 = 0x8D62,
95 
96  RGB8 = 0x8051,
97  RGB8_SNORM = 0x8F96,
98  RGB10 = 0x8052,
99  RGB12 = 0x8053,
100 
101  RGB16 = 0x8054,
102  RGB16_SNORM = 0x8F9A,
103 
104  RGBA2 = 0x8055,
105  RGBA4 = 0x8056,
106  RGB5_A1 = 0x8057,
107  RGBA8 = 0x8058,
108  RGBA8_SNORM = 0x8F97,
109 
110  RGB10_A2 = 0x8059,
111  RGB10_A2UI = 0x906F,
112 
113  RGBA12 = 0x805A,
114  RGBA16 = 0x805B,
115  RGBA16_SNORM = 0x8F9B,
116 
117  SRGB8 = 0x8C41,
118  SRGB8_ALPHA8 = 0x8C43,
119 
120  R16F = 0x822D,
121  RG16F = 0x822F,
122  RGB16F = 0x881B,
123  RGBA16F = 0x881A,
124 
125  R32F = 0x822E,
126  RG32F = 0x8230,
127  RGB32F = 0x8815,
128  RGBA32F = 0x8814,
129 
130  R11F_G11F_B10F = 0x8C3A,
131  RGB9_E5 = 0x8C3D,
132 
133 
134  R8I = 0x8231,
135  R8UI = 0x8232,
136  R16I = 0x8233,
137  R16UI = 0x8234,
138  R32I = 0x8235,
139  R32UI = 0x8236,
140  RG8I = 0x8237,
141  RG8UI = 0x8238,
142  RG16I = 0x8239,
143  RG16UI = 0x823A,
144  RG32I = 0x823B,
145  RG32UI = 0x823C,
146 
147  RGB8I = 0x8D8F,
148  RGB8UI = 0x8D7D,
149  RGB16I = 0x8D89,
150  RGB16UI = 0x8D77,
151 
152  RGB32I = 0x8D83,
153  RGB32UI = 0x8D71,
154  RGBA8I = 0x8D8E,
155  RGBA8UI = 0x8D7C,
156  RGBA16I = 0x8D88,
157  RGBA16UI = 0x8D76,
158  RGBA32I = 0x8D82,
159 
160  RGBA32UI = 0x8D70,
161 
162  // GL 4.4 Table 8.13
163  DEPTH_COMPONENT16 = 0x81A5,
164  DEPTH_COMPONENT24 = 0x81A6,
165  DEPTH_COMPONENT32 = 0x81A7,
166 
167  DEPTH_COMPONENT32F = 0x8CAC,
168  DEPTH24_STENCIL8 = 0x88F0,
169  DEPTH32F_STENCIL8 = 0x8CAD,
170 
171  STENCIL_INDEX1 = 0x8D46,
172  STENCIL_INDEX4 = 0x8D47,
173  STENCIL_INDEX8 = 0x8D48,
174  STENCIL_INDEX16 = 0x8D49,
175 
176  // GL 4.4 Table 8.14
177  COMPRESSED_RED = 0x8225,
178  COMPRESSED_RG = 0x8226,
179  COMPRESSED_RGB = 0x84ED,
180  COMPRESSED_RGBA = 0x84EE,
181 
182  COMPRESSED_SRGB = 0x8C48,
183  COMPRESSED_SRGB_ALPHA = 0x8C49,
184 
185  COMPRESSED_ETC1_RGB8_OES = 0x8D64,
186 
187  COMPRESSED_SRGB_S3TC_DXT1_EXT = 0x8C4C,
188  COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT = 0x8C4D,
189  COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT = 0x8C4E,
190  COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT = 0x8C4F,
191 
192  COMPRESSED_RED_RGTC1 = 0x8DBB,
193  COMPRESSED_SIGNED_RED_RGTC1 = 0x8DBC,
194  COMPRESSED_RG_RGTC2 = 0x8DBD,
195  COMPRESSED_SIGNED_RG_RGTC2 = 0x8DBE,
196 
197  COMPRESSED_RGBA_BPTC_UNORM = 0x8E8C,
198  COMPRESSED_SRGB_ALPHA_BPTC_UNORM = 0x8E8D,
199  COMPRESSED_RGB_BPTC_SIGNED_FLOAT = 0x8E8E,
200  COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT = 0x8E8F,
201 
202  COMPRESSED_RGB8_ETC2 = 0x9274,
203  COMPRESSED_SRGB8_ETC2 = 0x9275,
204  COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 = 0x9276,
205  COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 = 0x9277,
206  COMPRESSED_RGBA8_ETC2_EAC = 0x9278,
207  COMPRESSED_SRGB8_ALPHA8_ETC2_EAC = 0x9279,
208 
209  COMPRESSED_R11_EAC = 0x9270,
210  COMPRESSED_SIGNED_R11_EAC = 0x9271,
211  COMPRESSED_RG11_EAC = 0x9272,
212  COMPRESSED_SIGNED_RG11_EAC = 0x9273,
213  };
214 
215  static std::unordered_map<std::string, InternalFormat> nameToFormat {
216  { "COMPRESSED_RED", InternalFormat::COMPRESSED_RED },
217  { "COMPRESSED_RG", InternalFormat::COMPRESSED_RG },
218  { "COMPRESSED_RGB", InternalFormat::COMPRESSED_RGB },
219  { "COMPRESSED_RGBA", InternalFormat::COMPRESSED_RGBA },
220 
221  { "COMPRESSED_SRGB", InternalFormat::COMPRESSED_SRGB },
222  { "COMPRESSED_SRGB_ALPHA", InternalFormat::COMPRESSED_SRGB_ALPHA },
223 
224  { "COMPRESSED_ETC1_RGB8_OES", InternalFormat::COMPRESSED_ETC1_RGB8_OES },
225 
226  { "COMPRESSED_SRGB_S3TC_DXT1_EXT", InternalFormat::COMPRESSED_SRGB_S3TC_DXT1_EXT },
227  { "COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT", InternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT },
228  { "COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT", InternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT },
229  { "COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT", InternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT },
230 
231  { "COMPRESSED_RED_RGTC1", InternalFormat::COMPRESSED_RED_RGTC1 },
232  { "COMPRESSED_SIGNED_RED_RGTC1", InternalFormat::COMPRESSED_SIGNED_RED_RGTC1 },
233  { "COMPRESSED_RG_RGTC2", InternalFormat::COMPRESSED_RG_RGTC2 },
234  { "COMPRESSED_SIGNED_RG_RGTC2", InternalFormat::COMPRESSED_SIGNED_RG_RGTC2 },
235 
236  { "COMPRESSED_RGBA_BPTC_UNORM", InternalFormat::COMPRESSED_RGBA_BPTC_UNORM },
237  { "COMPRESSED_SRGB_ALPHA_BPTC_UNORM", InternalFormat::COMPRESSED_SRGB_ALPHA_BPTC_UNORM },
238  { "COMPRESSED_RGB_BPTC_SIGNED_FLOAT", InternalFormat::COMPRESSED_RGB_BPTC_SIGNED_FLOAT },
239  { "COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT", InternalFormat::COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT },
240 
241  { "COMPRESSED_RGB8_ETC2", InternalFormat::COMPRESSED_RGB8_ETC2 },
242  { "COMPRESSED_SRGB8_ETC2", InternalFormat::COMPRESSED_SRGB8_ETC2 },
243  { "COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2", InternalFormat::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 },
244  { "COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2", InternalFormat::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 },
245  { "COMPRESSED_RGBA8_ETC2_EAC", InternalFormat::COMPRESSED_RGBA8_ETC2_EAC },
246  { "COMPRESSED_SRGB8_ALPHA8_ETC2_EAC", InternalFormat::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC },
247 
248  { "COMPRESSED_R11_EAC", InternalFormat::COMPRESSED_R11_EAC },
249  { "COMPRESSED_SIGNED_R11_EAC", InternalFormat::COMPRESSED_SIGNED_R11_EAC },
250  { "COMPRESSED_RG11_EAC", InternalFormat::COMPRESSED_RG11_EAC },
251  { "COMPRESSED_SIGNED_RG11_EAC", InternalFormat::COMPRESSED_SIGNED_RG11_EAC }
252  };
253 
254  inline const char* toString(InternalFormat format) {
255  for (auto& pair : nameToFormat) {
256  if (pair.second == format) {
257  return pair.first.data();
258  }
259  }
260  return nullptr;
261  }
262 
263  inline bool fromString(const char* name, InternalFormat* format) {
264  auto it = nameToFormat.find(name);
265  if (it == nameToFormat.end()) {
266  return false;
267  }
268  *format = it->second;
269  return true;
270  }
271 
272  inline uint8_t evalUncompressedBlockBitSize(InternalFormat format) {
273  switch (format) {
274  case InternalFormat::R8:
275  case InternalFormat::R8_SNORM:
276  return 8;
277  case InternalFormat::R16:
278  case InternalFormat::R16_SNORM:
279  case InternalFormat::RG8:
280  case InternalFormat::RG8_SNORM:
281  return 16;
282  case InternalFormat::RG16:
283  case InternalFormat::RG16_SNORM:
284  return 16;
285  case InternalFormat::R3_G3_B2:
286  return 8;
287  case InternalFormat::RGB4:
288  return 12;
289  case InternalFormat::RGB5:
290  case InternalFormat::RGB565:
291  return 16;
292  case InternalFormat::RGB8:
293  case InternalFormat::RGB8_SNORM:
294  return 24;
295  case InternalFormat::RGB10:
296  // TODO: check if this is really the case
297  return 32;
298  case InternalFormat::RGB12:
299  // TODO: check if this is really the case
300  return 48;
301  case InternalFormat::RGB16:
302  case InternalFormat::RGB16_SNORM:
303  return 48;
304  case InternalFormat::RGBA2:
305  return 8;
306  case InternalFormat::RGBA4:
307  case InternalFormat::RGB5_A1:
308  return 16;
309  case InternalFormat::RGBA8:
310  case InternalFormat::RGBA8_SNORM:
311  case InternalFormat::RGB10_A2:
312  case InternalFormat::RGB10_A2UI:
313  return 32;
314  case InternalFormat::RGBA12:
315  return 48;
316  case InternalFormat::RGBA16:
317  case InternalFormat::RGBA16_SNORM:
318  return 64;
319  case InternalFormat::SRGB8:
320  return 24;
321  case InternalFormat::SRGB8_ALPHA8:
322  return 32;
323  case InternalFormat::R16F:
324  return 16;
325  case InternalFormat::RG16F:
326  return 32;
327  case InternalFormat::RGB16F:
328  return 48;
329  case InternalFormat::RGBA16F:
330  return 64;
331  case InternalFormat::R32F:
332  return 32;
333  case InternalFormat::RG32F:
334  return 64;
335  case InternalFormat::RGB32F:
336  return 96;
337  case InternalFormat::RGBA32F:
338  return 128;
339  case InternalFormat::R11F_G11F_B10F:
340  case InternalFormat::RGB9_E5:
341  return 32;
342  case InternalFormat::R8I:
343  case InternalFormat::R8UI:
344  return 8;
345  case InternalFormat::R16I:
346  case InternalFormat::R16UI:
347  return 16;
348  case InternalFormat::R32I:
349  case InternalFormat::R32UI:
350  return 32;
351  case InternalFormat::RG8I:
352  case InternalFormat::RG8UI:
353  return 16;
354  case InternalFormat::RG16I:
355  case InternalFormat::RG16UI:
356  return 32;
357  case InternalFormat::RG32I:
358  case InternalFormat::RG32UI:
359  return 64;
360  case InternalFormat::RGB8I:
361  case InternalFormat::RGB8UI:
362  return 24;
363  case InternalFormat::RGB16I:
364  case InternalFormat::RGB16UI:
365  return 48;
366  case InternalFormat::RGB32I:
367  case InternalFormat::RGB32UI:
368  return 96;
369  case InternalFormat::RGBA8I:
370  case InternalFormat::RGBA8UI:
371  return 32;
372  case InternalFormat::RGBA16I:
373  case InternalFormat::RGBA16UI:
374  return 64;
375  case InternalFormat::RGBA32I:
376  case InternalFormat::RGBA32UI:
377  return 128;
378  case InternalFormat::DEPTH_COMPONENT16:
379  return 16;
380  case InternalFormat::DEPTH_COMPONENT24:
381  return 24;
382  case InternalFormat::DEPTH_COMPONENT32:
383  case InternalFormat::DEPTH_COMPONENT32F:
384  case InternalFormat::DEPTH24_STENCIL8:
385  return 32;
386  case InternalFormat::DEPTH32F_STENCIL8:
387  // TODO : check if this true
388  return 40;
389  case InternalFormat::STENCIL_INDEX1:
390  return 1;
391  case InternalFormat::STENCIL_INDEX4:
392  return 4;
393  case InternalFormat::STENCIL_INDEX8:
394  return 8;
395  case InternalFormat::STENCIL_INDEX16:
396  return 16;
397 
398  default:
399  return 0;
400  }
401  }
402 
403  template <uint32_t ALIGNMENT>
404  inline uint32_t evalAlignedCompressedBlockCount(uint32_t value) {
405  enum { val = ALIGNMENT && !(ALIGNMENT & (ALIGNMENT - 1)) };
406  static_assert(val, "template parameter ALIGNMENT must be a power of 2");
407  // FIXME add static assert that ALIGNMENT is a power of 2
408  static uint32_t ALIGNMENT_REMAINDER = ALIGNMENT - 1;
409  return (value + ALIGNMENT_REMAINDER) / ALIGNMENT;
410  }
411 
412  inline uint32_t evalCompressedBlockCount(InternalFormat format, uint32_t value) {
413  switch (format) {
414  case InternalFormat::COMPRESSED_SRGB_S3TC_DXT1_EXT: // BC1
415  case InternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT: // BC1A
416  case InternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT: // BC3
417  case InternalFormat::COMPRESSED_RED_RGTC1: // BC4
418  case InternalFormat::COMPRESSED_RG_RGTC2: // BC5
419  case InternalFormat::COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT: // BC6
420  case InternalFormat::COMPRESSED_SRGB_ALPHA_BPTC_UNORM: // BC7
421  // ETC2 / EAC
422  case InternalFormat::COMPRESSED_RGB8_ETC2:
423  case InternalFormat::COMPRESSED_SRGB8_ETC2:
424  case InternalFormat::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
425  case InternalFormat::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
426  case InternalFormat::COMPRESSED_RGBA8_ETC2_EAC:
427  case InternalFormat::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
428  case InternalFormat::COMPRESSED_R11_EAC:
429  case InternalFormat::COMPRESSED_SIGNED_R11_EAC:
430  case InternalFormat::COMPRESSED_RG11_EAC:
431  case InternalFormat::COMPRESSED_SIGNED_RG11_EAC:
432  return evalAlignedCompressedBlockCount<4>(value);
433 
434  default:
435  throw std::runtime_error("Unknown format");
436  }
437  }
438 
439  inline uint8_t evalCompressedBlockSize(InternalFormat format) {
440  switch (format) {
441  case InternalFormat::COMPRESSED_SRGB_S3TC_DXT1_EXT:
442  case InternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
443  case InternalFormat::COMPRESSED_RED_RGTC1:
444  case InternalFormat::COMPRESSED_RGB8_ETC2:
445  case InternalFormat::COMPRESSED_SRGB8_ETC2:
446  case InternalFormat::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
447  case InternalFormat::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
448  case InternalFormat::COMPRESSED_R11_EAC:
449  case InternalFormat::COMPRESSED_SIGNED_R11_EAC:
450  return 8;
451 
452  case InternalFormat::COMPRESSED_SRGB_ALPHA_BPTC_UNORM:
453  case InternalFormat::COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT:
454  case InternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
455  case InternalFormat::COMPRESSED_RG_RGTC2:
456  case InternalFormat::COMPRESSED_RGBA8_ETC2_EAC:
457  case InternalFormat::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
458  case InternalFormat::COMPRESSED_RG11_EAC:
459  case InternalFormat::COMPRESSED_SIGNED_RG11_EAC:
460  return 16;
461 
462  default:
463  return 0;
464  }
465  }
466 
467  inline uint8_t evalCompressedBlockBitSize(InternalFormat format) {
468  return evalCompressedBlockSize(format) * 8;
469  }
470 
471  enum class BaseInternalFormat : uint32_t {
472  // GL 4.4 Table 8.11
473  DEPTH_COMPONENT = 0x1902,
474  DEPTH_STENCIL = 0x84F9,
475  RED = 0x1903,
476  RG = 0x8227,
477  RGB = 0x1907,
478  RGBA = 0x1908,
479  STENCIL_INDEX = 0x1901,
480  };
481 
482  inline uint8_t evalComponentCount(BaseInternalFormat format) {
483  switch (format) {
484  case BaseInternalFormat::DEPTH_COMPONENT:
485  case BaseInternalFormat::STENCIL_INDEX:
486  case BaseInternalFormat::RED:
487  return 1;
488 
489  case BaseInternalFormat::DEPTH_STENCIL:
490  case BaseInternalFormat::RG:
491  return 2;
492 
493  case BaseInternalFormat::RGB:
494  return 3;
495 
496  case BaseInternalFormat::RGBA:
497  return 4;
498 
499  default:
500  break;
501  }
502 
503  return 0;
504  }
505 
506  namespace cubemap {
507  enum Constants {
508  NUM_CUBEMAPFACES = 6,
509  };
510 
511  enum class Face {
512  POSITIVE_X = 0x8515,
513  NEGATIVE_X = 0x8516,
514  POSITIVE_Y = 0x8517,
515  NEGATIVE_Y = 0x8518,
516  POSITIVE_Z = 0x8519,
517  NEGATIVE_Z = 0x851A,
518  };
519  }
520  }
521  }
522 }
523 
524 #endif // khronos_khr_hpp