]> git.cworth.org Git - apitrace/blob - glsize.hpp
3eb78fcb59baabb39f4b17c6ad891f8833e498dc
[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 #include "glimports.hpp"
33
34
35 static inline size_t
36 __gl_calllists_size(GLsizei n, GLenum type)
37 {
38    size_t bytes;
39    switch(type) {
40    case GL_BYTE:
41    case GL_UNSIGNED_BYTE:
42       bytes = 1;
43       break;
44    case GL_2_BYTES:
45    case GL_SHORT:
46    case GL_UNSIGNED_SHORT:
47       bytes = 2;
48       break;
49    case GL_3_BYTES:
50       bytes = 3;
51       break;
52    case GL_4_BYTES:
53    case GL_INT:
54    case GL_UNSIGNED_INT:
55    case GL_FLOAT:
56       bytes = 4;
57       break;
58    default:
59       assert(0);
60       bytes = 1;
61    }
62
63    return n*bytes;
64 }
65
66 static inline size_t
67 __gl_image_size(GLenum format, GLenum type, GLsizei width, GLsizei height, GLsizei depth, GLint border) {
68    size_t num_channels;
69    switch (format) {
70    case GL_COLOR_INDEX:
71    case GL_RED:
72    case GL_GREEN:
73    case GL_BLUE:
74    case GL_ALPHA:
75    case GL_INTENSITY:
76    case GL_DEPTH_COMPONENT:
77    case GL_STENCIL_INDEX:
78       num_channels = 1;
79       break;
80    case GL_LUMINANCE_ALPHA:
81       num_channels = 2;
82       break;
83    case GL_RGB:
84    case GL_BGR:
85       num_channels = 3;
86       break;
87    case GL_RGBA:
88    case GL_BGRA:
89       num_channels = 4;
90       break;
91    default:
92       assert(0);
93       num_channels = 0;
94       break;
95    }
96
97    size_t bits_per_pixel;
98    switch (type) {
99    case GL_BITMAP:
100       bits_per_pixel = 1;
101       break;
102    case GL_BYTE:
103    case GL_UNSIGNED_BYTE:
104       bits_per_pixel = 8 * num_channels;
105       break;
106    case GL_SHORT:
107    case GL_UNSIGNED_SHORT:
108       bits_per_pixel = 16 * num_channels;
109       break;
110    case GL_INT:
111    case GL_UNSIGNED_INT:
112    case GL_FLOAT:
113       bits_per_pixel = 32 * num_channels;
114       break;
115    case GL_UNSIGNED_BYTE_3_3_2:
116    case GL_UNSIGNED_BYTE_2_3_3_REV:
117    case GL_UNSIGNED_SHORT_5_6_5:
118    case GL_UNSIGNED_SHORT_5_6_5_REV:
119       bits_per_pixel = 8;
120       break;
121    case GL_UNSIGNED_SHORT_4_4_4_4:
122    case GL_UNSIGNED_SHORT_4_4_4_4_REV:
123    case GL_UNSIGNED_SHORT_5_5_5_1:
124    case GL_UNSIGNED_SHORT_1_5_5_5_REV:
125       bits_per_pixel = 16;
126       break;
127    case GL_UNSIGNED_INT_8_8_8_8:
128    case GL_UNSIGNED_INT_8_8_8_8_REV:
129    case GL_UNSIGNED_INT_10_10_10_2:
130    case GL_UNSIGNED_INT_2_10_10_10_REV:
131       bits_per_pixel = 32;
132       break;
133    default:
134       assert(0);
135       bits_per_pixel = 0;
136       break;
137    }
138
139    /* FIXME: consider glPixelStore settings */
140
141    size_t row_stride = (width*bits_per_pixel + 7)/8;
142
143    size_t slice_stride = height*row_stride;
144
145    return depth*slice_stride;
146 }
147
148 static inline size_t
149 __gl_bitmap_size(GLsizei width, GLsizei height) {
150    return __gl_image_size(GL_COLOR_INDEX, GL_BITMAP, width, height, 1, 0);
151 }
152
153 #endif /* _GL_HELPERS_HPP_ */