]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_pixel_format.h
Initial vogl checkin
[vogl] / src / voglcore / vogl_pixel_format.h
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 // File: vogl_pixel_format.h
28 #pragma once
29
30 #include "vogl_core.h"
31 #include "vogl_dxt.h"
32 #include "vogl.h"
33 #include "dds_defs.h"
34
35 namespace vogl
36 {
37     namespace pixel_format_helpers
38     {
39         uint get_num_formats();
40         pixel_format get_pixel_format_by_index(uint index);
41
42         const char *get_pixel_format_string(pixel_format fmt);
43
44         const char *get_vogl_format_string(vogl_format fmt);
45
46         inline bool is_grayscale(pixel_format fmt)
47         {
48             switch (fmt)
49             {
50                 case PIXEL_FMT_L8:
51                 case PIXEL_FMT_A8L8:
52                     return true;
53                 default:
54                     break;
55             }
56             return false;
57         }
58
59         inline bool is_dxt1(pixel_format fmt)
60         {
61             return (fmt == PIXEL_FMT_DXT1) || (fmt == PIXEL_FMT_DXT1A);
62         }
63
64         // has_alpha() should probably be called "has_opacity()" - it indicates if the format encodes opacity
65         // because some swizzled DXT5 formats do not encode opacity.
66         inline bool has_alpha(pixel_format fmt)
67         {
68             switch (fmt)
69             {
70                 case PIXEL_FMT_DXT1A:
71                 case PIXEL_FMT_DXT2:
72                 case PIXEL_FMT_DXT3:
73                 case PIXEL_FMT_DXT4:
74                 case PIXEL_FMT_DXT5:
75                 case PIXEL_FMT_DXT5A:
76                 case PIXEL_FMT_A8R8G8B8:
77                 case PIXEL_FMT_A8:
78                 case PIXEL_FMT_A8L8:
79                 case PIXEL_FMT_DXT5_AGBR:
80                     return true;
81                 default:
82                     break;
83             }
84             return false;
85         }
86
87         inline bool is_alpha_only(pixel_format fmt)
88         {
89             switch (fmt)
90             {
91                 case PIXEL_FMT_A8:
92                 case PIXEL_FMT_DXT5A:
93                     return true;
94                 default:
95                     break;
96             }
97             return false;
98         }
99
100         inline bool is_normal_map(pixel_format fmt)
101         {
102             switch (fmt)
103             {
104                 case PIXEL_FMT_3DC:
105                 case PIXEL_FMT_DXN:
106                 case PIXEL_FMT_DXT5_xGBR:
107                 case PIXEL_FMT_DXT5_xGxR:
108                 case PIXEL_FMT_DXT5_AGBR:
109                     return true;
110                 default:
111                     break;
112             }
113             return false;
114         }
115
116         inline int is_dxt(pixel_format fmt)
117         {
118             switch (fmt)
119             {
120                 case PIXEL_FMT_DXT1:
121                 case PIXEL_FMT_DXT1A:
122                 case PIXEL_FMT_DXT2:
123                 case PIXEL_FMT_DXT3:
124                 case PIXEL_FMT_DXT4:
125                 case PIXEL_FMT_DXT5:
126                 case PIXEL_FMT_3DC:
127                 case PIXEL_FMT_DXT5A:
128                 case PIXEL_FMT_DXN:
129                 case PIXEL_FMT_DXT5_CCxY:
130                 case PIXEL_FMT_DXT5_xGxR:
131                 case PIXEL_FMT_DXT5_xGBR:
132                 case PIXEL_FMT_DXT5_AGBR:
133                 case PIXEL_FMT_ETC1:
134                     return true;
135                 default:
136                     break;
137             }
138             return false;
139         }
140
141         inline int get_fundamental_format(pixel_format fmt)
142         {
143             switch (fmt)
144             {
145                 case PIXEL_FMT_DXT1A:
146                     return PIXEL_FMT_DXT1;
147                 case PIXEL_FMT_DXT5_CCxY:
148                 case PIXEL_FMT_DXT5_xGxR:
149                 case PIXEL_FMT_DXT5_xGBR:
150                 case PIXEL_FMT_DXT5_AGBR:
151                     return PIXEL_FMT_DXT5;
152                 default:
153                     break;
154             }
155             return fmt;
156         }
157
158         inline dxt_format get_dxt_format(pixel_format fmt)
159         {
160             switch (fmt)
161             {
162                 case PIXEL_FMT_DXT1:
163                     return cDXT1;
164                 case PIXEL_FMT_DXT1A:
165                     return cDXT1A;
166                 case PIXEL_FMT_DXT2:
167                     return cDXT3;
168                 case PIXEL_FMT_DXT3:
169                     return cDXT3;
170                 case PIXEL_FMT_DXT4:
171                     return cDXT5;
172                 case PIXEL_FMT_DXT5:
173                     return cDXT5;
174                 case PIXEL_FMT_3DC:
175                     return cDXN_YX;
176                 case PIXEL_FMT_DXT5A:
177                     return cDXT5A;
178                 case PIXEL_FMT_DXN:
179                     return cDXN_XY;
180                 case PIXEL_FMT_DXT5_CCxY:
181                     return cDXT5;
182                 case PIXEL_FMT_DXT5_xGxR:
183                     return cDXT5;
184                 case PIXEL_FMT_DXT5_xGBR:
185                     return cDXT5;
186                 case PIXEL_FMT_DXT5_AGBR:
187                     return cDXT5;
188                 case PIXEL_FMT_ETC1:
189                     return cETC1;
190                 default:
191                     break;
192             }
193             return cDXTInvalid;
194         }
195
196         inline pixel_format from_dxt_format(dxt_format dxt_fmt)
197         {
198             switch (dxt_fmt)
199             {
200                 case cDXT1:
201                     return PIXEL_FMT_DXT1;
202                 case cDXT1A:
203                     return PIXEL_FMT_DXT1A;
204                 case cDXT3:
205                     return PIXEL_FMT_DXT3;
206                 case cDXT5:
207                     return PIXEL_FMT_DXT5;
208                 case cDXN_XY:
209                     return PIXEL_FMT_DXN;
210                 case cDXN_YX:
211                     return PIXEL_FMT_3DC;
212                 case cDXT5A:
213                     return PIXEL_FMT_DXT5A;
214                 case cETC1:
215                     return PIXEL_FMT_ETC1;
216                 default:
217                     break;
218             }
219             VOGL_ASSERT(false);
220             return PIXEL_FMT_INVALID;
221         }
222
223         inline bool is_pixel_format_non_srgb(pixel_format fmt)
224         {
225             switch (fmt)
226             {
227                 case PIXEL_FMT_3DC:
228                 case PIXEL_FMT_DXN:
229                 case PIXEL_FMT_DXT5A:
230                 case PIXEL_FMT_DXT5_CCxY:
231                 case PIXEL_FMT_DXT5_xGxR:
232                 case PIXEL_FMT_DXT5_xGBR:
233                 case PIXEL_FMT_DXT5_AGBR:
234                     return true;
235                 default:
236                     break;
237             }
238             return false;
239         }
240
241         inline bool is_vogl_format_non_srgb(vogl_format fmt)
242         {
243             switch (fmt)
244             {
245                 case cCRNFmtDXN_XY:
246                 case cCRNFmtDXN_YX:
247                 case cCRNFmtDXT5A:
248                 case cCRNFmtDXT5_CCxY:
249                 case cCRNFmtDXT5_xGxR:
250                 case cCRNFmtDXT5_xGBR:
251                 case cCRNFmtDXT5_AGBR:
252                     return true;
253                 default:
254                     break;
255             }
256             return false;
257         }
258
259         inline uint get_bpp(pixel_format fmt)
260         {
261             switch (fmt)
262             {
263                 case PIXEL_FMT_DXT1:
264                     return 4;
265                 case PIXEL_FMT_DXT1A:
266                     return 4;
267                 case PIXEL_FMT_ETC1:
268                     return 4;
269                 case PIXEL_FMT_DXT2:
270                     return 8;
271                 case PIXEL_FMT_DXT3:
272                     return 8;
273                 case PIXEL_FMT_DXT4:
274                     return 8;
275                 case PIXEL_FMT_DXT5:
276                     return 8;
277                 case PIXEL_FMT_3DC:
278                     return 8;
279                 case PIXEL_FMT_DXT5A:
280                     return 4;
281                 case PIXEL_FMT_R8G8B8:
282                     return 24;
283                 case PIXEL_FMT_A8R8G8B8:
284                     return 32;
285                 case PIXEL_FMT_A8:
286                     return 8;
287                 case PIXEL_FMT_L8:
288                     return 8;
289                 case PIXEL_FMT_A8L8:
290                     return 16;
291                 case PIXEL_FMT_DXN:
292                     return 8;
293                 case PIXEL_FMT_DXT5_CCxY:
294                     return 8;
295                 case PIXEL_FMT_DXT5_xGxR:
296                     return 8;
297                 case PIXEL_FMT_DXT5_xGBR:
298                     return 8;
299                 case PIXEL_FMT_DXT5_AGBR:
300                     return 8;
301                 default:
302                     break;
303             }
304             VOGL_ASSERT(false);
305             return 0;
306         };
307
308         inline uint get_dxt_bytes_per_block(pixel_format fmt)
309         {
310             switch (fmt)
311             {
312                 case PIXEL_FMT_DXT1:
313                     return 8;
314                 case PIXEL_FMT_DXT1A:
315                     return 8;
316                 case PIXEL_FMT_DXT5A:
317                     return 8;
318                 case PIXEL_FMT_ETC1:
319                     return 8;
320                 case PIXEL_FMT_DXT2:
321                     return 16;
322                 case PIXEL_FMT_DXT3:
323                     return 16;
324                 case PIXEL_FMT_DXT4:
325                     return 16;
326                 case PIXEL_FMT_DXT5:
327                     return 16;
328                 case PIXEL_FMT_3DC:
329                     return 16;
330                 case PIXEL_FMT_DXN:
331                     return 16;
332                 case PIXEL_FMT_DXT5_CCxY:
333                     return 16;
334                 case PIXEL_FMT_DXT5_xGxR:
335                     return 16;
336                 case PIXEL_FMT_DXT5_xGBR:
337                     return 16;
338                 case PIXEL_FMT_DXT5_AGBR:
339                     return 16;
340                 default:
341                     break;
342             }
343             VOGL_ASSERT(false);
344             return 0;
345         }
346
347         enum component_flags
348         {
349             cCompFlagRValid = 1,
350             cCompFlagGValid = 2,
351             cCompFlagBValid = 4,
352             cCompFlagAValid = 8,
353             cCompFlagGrayscale = 16,
354             cCompFlagNormalMap = 32,
355             cCompFlagLumaChroma = 64,
356             cCompFlagsRGBValid = cCompFlagRValid | cCompFlagGValid | cCompFlagBValid,
357             cCompFlagsRGBAValid = cCompFlagRValid | cCompFlagGValid | cCompFlagBValid | cCompFlagAValid,
358             cDefaultCompFlags = cCompFlagsRGBAValid
359         };
360
361         component_flags get_component_flags(pixel_format fmt);
362
363         vogl_format convert_pixel_format_to_best_vogl_format(pixel_format vogl_fmt);
364
365         pixel_format convert_vogl_format_to_pixel_format(vogl_format fmt);
366
367     } // namespace pixel_format_helpers
368
369 } // namespace vogl