Overte C++ Documentation
Format.h
1 //
2 // Format.h
3 // interface/src/gpu
4 //
5 // Created by Sam Gateau on 10/29/2014.
6 // Copyright 2014 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 #ifndef hifi_gpu_Format_h
12 #define hifi_gpu_Format_h
13 
14 #include <assert.h>
15 #include <memory>
16 
17 #include "Forward.h"
18 
19 namespace gpu {
20 
21 class Backend;
22 
23 // Description of a scalar type
24 enum Type : uint8_t {
25 
26  FLOAT = 0,
27  INT32,
28  UINT32,
29  HALF,
30  INT16,
31  UINT16,
32  INT8,
33  UINT8,
34 
35  NINT32,
36  NUINT32,
37  NINT16,
38  NUINT16,
39  NINT8,
40  NUINT8,
41  NUINT2,
42  NINT2_10_10_10,
43 
44  COMPRESSED,
45 
46  NUM_TYPES,
47 
48  BOOL = UINT8,
49  NORMALIZED_START = NINT32,
50 };
51 // Array providing the size in bytes for a given scalar type
52 static const int TYPE_SIZE[NUM_TYPES] = {
53  4, // FLOAT
54  4, // INT32
55  4, // UINT32
56  2, // HALF
57  2, // INT16
58  2, // UINT16
59  1, // INT8
60  1, // UINT8
61 
62  // normalized values
63  4, // NINT32
64  4, // NUINT32
65  2, // NINT16
66  2, // NUINT16
67  1, // NINT8
68  1, // NUINT8
69  1, // NUINT2
70  1, // NINT2_10_10_10
71 
72  1, // COMPRESSED
73 };
74 
75 // Array answering the question Does this type is integer or not
76 static const bool TYPE_IS_INTEGER[NUM_TYPES] = {
77  false, // FLOAT
78  true, // INT32
79  true, // UINT32
80  false, // HALF
81  true, // INT16
82  true, // UINT16
83  true, // INT8
84  true, // UINT8
85 
86  // Normalized values
87  false, // NINT32
88  false, // NUINT32
89  false, // NINT16
90  false, // NUINT16
91  false, // NINT8
92  false, // NUINT8
93  false, // NUINT2
94  false, // NINT2_10_10_10
95 
96  false, // COMPRESSED
97 };
98 
99 // Dimension of an Element
100 enum Dimension : uint8_t {
101  SCALAR = 0,
102  VEC2,
103  VEC3,
104  VEC4,
105  MAT2,
106  MAT3,
107  MAT4,
108  TILE4x4, // Blob element's size is defined from the type and semantic, it s counted as 1 component
109  NUM_DIMENSIONS,
110 };
111 
112 // Count (of scalars) in an Element for a given Dimension
113 static const int DIMENSION_LOCATION_COUNT[NUM_DIMENSIONS] = {
114  1,
115  1,
116  1,
117  1,
118  1,
119  3,
120  4,
121  1,
122 };
123 
124 // Count (of scalars) in an Element for a given Dimension's location
125 static const int DIMENSION_SCALAR_COUNT_PER_LOCATION[NUM_DIMENSIONS] = {
126  1,
127  2,
128  3,
129  4,
130  4,
131  3,
132  4,
133  1,
134 };
135 
136 // Count (of scalars) in an Element for a given Dimension
137 static const int DIMENSION_SCALAR_COUNT[NUM_DIMENSIONS] = {
138  1,
139  2,
140  3,
141  4,
142  4,
143  9,
144  16,
145  1,
146 };
147 
148 // Tile dimension described by the ELement for "Tilexxx" DIMENSIONs
149 static const glm::ivec2 DIMENSION_TILE_DIM[NUM_DIMENSIONS] = {
150  { 1, 1 },
151  { 1, 1 },
152  { 1, 1 },
153  { 1, 1 },
154  { 1, 1 },
155  { 1, 1 },
156  { 1, 1 },
157  { 4, 4 },
158 };
159 
160 // Semantic of an Element
161 // Provide information on how to use the element
162 enum Semantic : uint8_t {
163  RAW = 0, // used as RAW memory
164 
165  RED,
166  RGB,
167  RGBA,
168  BGRA,
169 
170  XY,
171  XYZ,
172  XYZW,
173  QUAT,
174  UV,
175  INDEX, //used by index buffer of a mesh
176  PART, // used by part buffer of a mesh
177 
178  DEPTH, // Depth only buffer
179  STENCIL, // Stencil only buffer
180  DEPTH_STENCIL, // Depth Stencil buffer
181 
182  SRED,
183  SRGB,
184  SRGBA,
185  SBGRA,
186 
187  // These are generic compression format smeantic for images
188  // THey must be used with Dim = BLOB and Type = Compressed
189  // THe size of a compressed element is defined from the semantic
190  _FIRST_COMPRESSED,
191 
192  COMPRESSED_BC1_SRGB,
193  COMPRESSED_BC1_SRGBA,
194  COMPRESSED_BC3_SRGBA,
195  COMPRESSED_BC4_RED,
196  COMPRESSED_BC5_XY,
197  COMPRESSED_BC6_RGB,
198  COMPRESSED_BC7_SRGBA,
199 
200  COMPRESSED_ETC2_RGB,
201  COMPRESSED_ETC2_SRGB,
202  COMPRESSED_ETC2_RGB_PUNCHTHROUGH_ALPHA,
203  COMPRESSED_ETC2_SRGB_PUNCHTHROUGH_ALPHA,
204  COMPRESSED_ETC2_RGBA,
205  COMPRESSED_ETC2_SRGBA,
206  COMPRESSED_EAC_RED,
207  COMPRESSED_EAC_RED_SIGNED,
208  COMPRESSED_EAC_XY,
209  COMPRESSED_EAC_XY_SIGNED,
210 
211  _LAST_COMPRESSED,
212 
213  R11G11B10,
214  RGB9E5,
215 
216  UNIFORM,
217  UNIFORM_BUFFER,
218  RESOURCE_BUFFER,
219  SAMPLER,
220  SAMPLER_MULTISAMPLE,
221  SAMPLER_SHADOW,
222 
223 
224  NUM_SEMANTICS, // total Number of semantics (not a valid Semantic)!
225 };
226 
227 // Array providing the scaling factor to size in bytes depending on a given semantic
228 static const int SEMANTIC_SIZE_FACTOR[NUM_SEMANTICS] = {
229  1, //RAW = 0, // used as RAW memory
230 
231  1, //RED,
232  1, //RGB,
233  1, //RGBA,
234  1, //BGRA,
235 
236  1, //XY,
237  1, //XYZ,
238  1, //XYZW,
239  1, //QUAT,
240  1, //UV,
241  1, //INDEX, //used by index buffer of a mesh
242  1, //PART, // used by part buffer of a mesh
243 
244  1, //DEPTH, // Depth only buffer
245  1, //STENCIL, // Stencil only buffer
246  1, //DEPTH_STENCIL, // Depth Stencil buffer
247 
248  1, //SRED,
249  1, //SRGB,
250  1, //SRGBA,
251  1, //SBGRA,
252 
253  // These are generic compression format smeantic for images
254  // THey must be used with Dim = BLOB and Type = Compressed
255  // THe size of a compressed element is defined from the semantic
256  1, //_FIRST_COMPRESSED,
257 
258  8, //COMPRESSED_BC1_SRGB, 1/2 byte/pixel * 4x4 pixels = 8 bytes
259  8, //COMPRESSED_BC1_SRGBA, 1/2 byte/pixel * 4x4 pixels = 8 bytes
260  16, //COMPRESSED_BC3_SRGBA, 1 byte/pixel * 4x4 pixels = 16 bytes
261  8, //COMPRESSED_BC4_RED, 1/2 byte/pixel * 4x4 pixels = 8 bytes
262  16, //COMPRESSED_BC5_XY, 1 byte/pixel * 4x4 pixels = 16 bytes
263  16, //COMPRESSED_BC6_RGB, 1 byte/pixel * 4x4 pixels = 16 bytes
264  16, //COMPRESSED_BC7_SRGBA, 1 byte/pixel * 4x4 pixels = 16 bytes
265 
266  8, //COMPRESSED_ETC2_RGB,
267  8, //COMPRESSED_ETC2_SRGB,
268  8, //COMPRESSED_ETC2_RGB_PUNCHTHROUGH_ALPHA,
269  8, //COMPRESSED_ETC2_SRGB_PUNCHTHROUGH_ALPHA,
270  16, //COMPRESSED_ETC2_RGBA,
271  16, //COMPRESSED_ETC2_SRGBA,
272  8, //COMPRESSED_EAC_RED,
273  8, //COMPRESSED_EAC_RED_SIGNED,
274  16, //COMPRESSED_EAC_XY,
275  16, //COMPRESSED_EAC_XY_SIGNED,
276 
277  1, //_LAST_COMPRESSED,
278 
279  1, //R11G11B10,
280  1, //RGB9E5
281 
282  1, //UNIFORM,
283  1, //UNIFORM_BUFFER,
284  1, //RESOURCE_BUFFER,
285  1, //SAMPLER,
286  1, //SAMPLER_MULTISAMPLE,
287  1, //SAMPLER_SHADOW,
288 };
289 
290 
291 // Element is a simple 16bit value that contains everything we need to know about an element
292 // of a buffer, a pixel of a texture, a varying input/output or uniform from a shader pipeline.
293 // Type and dimension of the element, and semantic
294 class Element {
295 public:
296  Element(Dimension dim, Type type, Semantic sem) :
297  _semantic(sem),
298  _dimension(dim),
299  _type(type)
300  {}
301  Element() :
302  _semantic(RAW),
303  _dimension(SCALAR),
304  _type(INT8)
305  {}
306 
307  Semantic getSemantic() const { return (Semantic)_semantic; }
308 
309  Dimension getDimension() const { return (Dimension)_dimension; }
310 
311  bool isCompressed() const { return uint8(getSemantic() - _FIRST_COMPRESSED) <= uint8(_LAST_COMPRESSED - _FIRST_COMPRESSED); }
312 
313  Type getType() const { return (Type)_type; }
314  bool isNormalized() const { return (getType() >= NORMALIZED_START); }
315  bool isInteger() const { return TYPE_IS_INTEGER[getType()]; }
316 
317  uint8 getScalarCount() const { return DIMENSION_SCALAR_COUNT[(Dimension)_dimension]; }
318  uint32 getSize() const { return (DIMENSION_SCALAR_COUNT[_dimension] * TYPE_SIZE[_type] * SEMANTIC_SIZE_FACTOR[_semantic]); }
319  const glm::ivec2& getTile() const { return (DIMENSION_TILE_DIM[_dimension]); }
320 
321  uint8 getLocationCount() const { return DIMENSION_LOCATION_COUNT[(Dimension)_dimension]; }
322  uint8 getLocationScalarCount() const { return DIMENSION_SCALAR_COUNT_PER_LOCATION[(Dimension)_dimension]; }
323  uint32 getLocationSize() const { return DIMENSION_SCALAR_COUNT_PER_LOCATION[_dimension] * TYPE_SIZE[_type]; }
324 
325  uint16 getRaw() const { return *((const uint16*) (this)); }
326 
327 
328  bool operator ==(const Element& right) const {
329  return getRaw() == right.getRaw();
330  }
331  bool operator !=(const Element& right) const {
332  return getRaw() != right.getRaw();
333  }
334 
335  static const Element COLOR_R_8;
336  static const Element COLOR_SR_8;
337  static const Element COLOR_RGBA_32;
338  static const Element COLOR_SRGBA_32;
339  static const Element COLOR_BGRA_32;
340  static const Element COLOR_SBGRA_32;
341  static const Element COLOR_RGBA_2;
342  static const Element COLOR_R11G11B10;
343  static const Element COLOR_RGB9E5;
344  static const Element COLOR_COMPRESSED_BCX_RED;
345  static const Element COLOR_COMPRESSED_BCX_SRGB;
346  static const Element COLOR_COMPRESSED_BCX_SRGBA_MASK;
347  static const Element COLOR_COMPRESSED_BCX_SRGBA;
348  static const Element COLOR_COMPRESSED_BCX_XY;
349  static const Element COLOR_COMPRESSED_BCX_SRGBA_HIGH;
350  static const Element COLOR_COMPRESSED_BCX_HDR_RGB;
351  static const Element COLOR_COMPRESSED_ETC2_RGB;
352  static const Element COLOR_COMPRESSED_ETC2_SRGB;
353  static const Element COLOR_COMPRESSED_ETC2_RGB_PUNCHTHROUGH_ALPHA;
354  static const Element COLOR_COMPRESSED_ETC2_SRGB_PUNCHTHROUGH_ALPHA;
355  static const Element COLOR_COMPRESSED_ETC2_RGBA;
356  static const Element COLOR_COMPRESSED_ETC2_SRGBA;
357  static const Element COLOR_COMPRESSED_EAC_RED;
358  static const Element COLOR_COMPRESSED_EAC_RED_SIGNED;
359  static const Element COLOR_COMPRESSED_EAC_XY;
360  static const Element COLOR_COMPRESSED_EAC_XY_SIGNED;
361  static const Element DEPTH24_STENCIL8;
362  static const Element VEC2NU8_XY;
363  static const Element VEC4F_COLOR_RGBA;
364  static const Element VEC2F_UV;
365  static const Element VEC2F_XY;
366  static const Element VEC3F_XYZ;
367  static const Element VEC4F_XYZW;
368  static const Element VEC4F_NORMALIZED_XYZ10W2;
369  static const Element INDEX_UINT16;
370  static const Element INDEX_INT32;
371  static const Element PART_DRAWCALL;
372 
373  protected:
374  uint16 _semantic : 7;
375  uint16 _dimension : 4;
376  uint16 _type : 5;
377 };
378 
379 
380 enum ComparisonFunction {
381  NEVER = 0,
382  LESS,
383  EQUAL,
384  LESS_EQUAL,
385  GREATER,
386  NOT_EQUAL,
387  GREATER_EQUAL,
388  ALWAYS,
389 
390  NUM_COMPARISON_FUNCS,
391 };
392 
393 enum Primitive {
394  POINTS = 0,
395  LINES,
396  LINE_STRIP,
397  TRIANGLES,
398  TRIANGLE_STRIP,
399  TRIANGLE_FAN,
400  NUM_PRIMITIVES,
401 };
402 
403 };
404 
405 
406 #endif