]> git.cworth.org Git - apitrace/blob - glsize.hpp
99df9dd048ffd4801d9f7255673bf5b132f670eb
[apitrace] / glsize.hpp
1 /**************************************************************************
2  *
3  * Copyright 2010 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 #ifndef _GL_HELPERS_HPP_
27 #define _GL_HELPERS_HPP_
28
29
30 #include <cassert>
31
32 static inline size_t
33 __gl_calllists_size(GLsizei n, GLenum type)
34 {
35    size_t bytes;
36    switch(type) {
37    case GL_BYTE:
38    case GL_UNSIGNED_BYTE:
39       bytes = 1;
40       break;
41    case GL_2_BYTES:
42    case GL_SHORT:
43    case GL_UNSIGNED_SHORT:
44       bytes = 2;
45       break;
46    case GL_3_BYTES:
47       bytes = 3;
48       break;
49    case GL_4_BYTES:
50    case GL_INT:
51    case GL_UNSIGNED_INT:
52    case GL_FLOAT:
53       bytes = 4;
54       break;
55    default:
56       assert(0);
57       bytes = 1;
58    }
59
60    return n*bytes;
61 }
62
63 static inline size_t
64 __gl_image_size(GLenum format, GLenum type, GLsizei width, GLsizei height, GLsizei depth, GLint border) {
65    size_t num_channels;
66    switch (format) {
67    case GL_COLOR_INDEX:
68    case GL_RED:
69    case GL_GREEN:
70    case GL_BLUE:
71    case GL_ALPHA:
72    case GL_INTENSITY:
73    case GL_DEPTH_COMPONENT:
74    case GL_STENCIL_INDEX:
75       num_channels = 1;
76       break;
77    case GL_LUMINANCE_ALPHA:
78       num_channels = 2;
79       break;
80    case GL_RGB:
81    case GL_BGR:
82       num_channels = 3;
83       break;
84    case GL_RGBA:
85    case GL_BGRA:
86       num_channels = 4;
87       break;
88    default:
89       assert(0);
90       num_channels = 0;
91       break;
92    }
93
94    size_t bits_per_pixel;
95    switch (type) {
96    case GL_BITMAP:
97       bits_per_pixel = 1;
98       break;
99    case GL_BYTE:
100    case GL_UNSIGNED_BYTE:
101       bits_per_pixel = 8 * num_channels;
102       break;
103    case GL_SHORT:
104    case GL_UNSIGNED_SHORT:
105       bits_per_pixel = 16 * num_channels;
106       break;
107    case GL_INT:
108    case GL_UNSIGNED_INT:
109    case GL_FLOAT:
110       bits_per_pixel = 32 * num_channels;
111       break;
112    case GL_UNSIGNED_BYTE_3_3_2:
113    case GL_UNSIGNED_BYTE_2_3_3_REV:
114    case GL_UNSIGNED_SHORT_5_6_5:
115    case GL_UNSIGNED_SHORT_5_6_5_REV:
116       bits_per_pixel = 8;
117       break;
118    case GL_UNSIGNED_SHORT_4_4_4_4:
119    case GL_UNSIGNED_SHORT_4_4_4_4_REV:
120    case GL_UNSIGNED_SHORT_5_5_5_1:
121    case GL_UNSIGNED_SHORT_1_5_5_5_REV:
122       bits_per_pixel = 16;
123       break;
124    case GL_UNSIGNED_INT_8_8_8_8:
125    case GL_UNSIGNED_INT_8_8_8_8_REV:
126    case GL_UNSIGNED_INT_10_10_10_2:
127    case GL_UNSIGNED_INT_2_10_10_10_REV:
128       bits_per_pixel = 32;
129       break;
130    default:
131       assert(0);
132       bits_per_pixel = 0;
133       break;
134    }
135
136    /* FIXME: consider glPixelStore settings */
137
138    size_t row_stride = (width*bits_per_pixel + 7)/8;
139
140    size_t slice_stride = height*row_stride;
141
142    return depth*slice_stride;
143 }
144
145 static inline size_t
146 __gl_bitmap_size(GLsizei width, GLsizei height) {
147    return __gl_image_size(GL_COLOR_INDEX, GL_BITMAP, width, height, 1, 0);
148 }
149
150 #endif /* _GL_HELPERS_HPP_ */