]> git.cworth.org Git - apitrace/blob - helpers/glsize.hpp
Merge branch 'egl-image'
[apitrace] / helpers / glsize.hpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * Copyright 2010 VMware, Inc.
5  * Copyright 2004 IBM Corporation
6  * All Rights Reserved.
7  * 
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sub license,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  * 
15  * The above copyright notice and this permission notice (including the next
16  * paragraph) shall be included in all copies or substantial portions of the
17  * Software.
18  * 
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
22  * AUTHORS,
23  * AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
25  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26  * SOFTWARE.
27  *
28  **************************************************************************/
29
30 /*
31  * Auxiliary functions to compute the size of array/blob arguments, depending.
32  */
33
34 #ifndef _GL_SIZE_HPP_
35 #define _GL_SIZE_HPP_
36
37
38 #include <string.h>
39
40 #include <algorithm>
41
42 #include "os.hpp"
43 #include "glimports.hpp"
44
45
46 static inline size_t
47 _gl_type_size(GLenum type)
48 {
49     switch (type) {
50     case GL_BOOL:
51     case GL_BYTE:
52     case GL_UNSIGNED_BYTE:
53         return 1;
54     case GL_SHORT:
55     case GL_UNSIGNED_SHORT:
56     case GL_2_BYTES:
57     case GL_HALF_FLOAT:
58         return 2;
59     case GL_3_BYTES:
60         return 3;
61     case GL_INT:
62     case GL_UNSIGNED_INT:
63     case GL_FLOAT:
64     case GL_4_BYTES:
65     case GL_FIXED:
66         return 4;
67     case GL_DOUBLE:
68         return 8;
69     default:
70         os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, type);
71         return 0;
72     }
73 }
74
75 static inline void
76 _gl_uniform_size(GLenum type, GLenum &elemType, GLint &numCols, GLint &numRows) {
77     numCols = 1;
78     numRows = 1;
79
80     switch (type) {
81     case GL_FLOAT:
82         elemType = GL_FLOAT;
83         break;
84     case GL_FLOAT_VEC2:
85         elemType = GL_FLOAT;
86         numCols = 2;
87         break;
88     case GL_FLOAT_VEC3:
89         elemType = GL_FLOAT;
90         numCols = 3;
91         break;
92     case GL_FLOAT_VEC4:
93         elemType = GL_FLOAT;
94         numCols = 4;
95         break;
96     case GL_DOUBLE:
97         elemType = GL_DOUBLE;
98         break;
99     case GL_DOUBLE_VEC2:
100         elemType = GL_DOUBLE;
101         numCols = 2;
102         break;
103     case GL_DOUBLE_VEC3:
104         elemType = GL_DOUBLE;
105         numCols = 3;
106         break;
107     case GL_DOUBLE_VEC4:
108         elemType = GL_DOUBLE;
109         numCols = 4;
110         break;
111     case GL_INT:
112         elemType = GL_INT;
113         break;
114     case GL_INT_VEC2:
115         elemType = GL_INT;
116         numCols = 2;
117         break;
118     case GL_INT_VEC3:
119         elemType = GL_INT;
120         numCols = 3;
121         break;
122     case GL_INT_VEC4:
123         elemType = GL_INT;
124         numCols = 4;
125         break;
126     case GL_UNSIGNED_INT:
127         elemType = GL_UNSIGNED_INT;
128         break;
129     case GL_UNSIGNED_INT_VEC2:
130         elemType = GL_UNSIGNED_INT;
131         numCols = 2;
132         break;
133     case GL_UNSIGNED_INT_VEC3:
134         elemType = GL_UNSIGNED_INT;
135         numCols = 3;
136         break;
137     case GL_UNSIGNED_INT_VEC4:
138         elemType = GL_UNSIGNED_INT;
139         numCols = 4;
140         break;
141     case GL_BOOL:
142         elemType = GL_BOOL;
143         break;
144     case GL_BOOL_VEC2:
145         elemType = GL_BOOL;
146         numCols = 2;
147         break;
148     case GL_BOOL_VEC3:
149         elemType = GL_BOOL;
150         numCols = 3;
151         break;
152     case GL_BOOL_VEC4:
153         elemType = GL_BOOL;
154         numCols = 4;
155         break;
156     case GL_FLOAT_MAT2:
157         elemType = GL_FLOAT;
158         numCols = 2;
159         numRows = 2;
160         break;
161     case GL_FLOAT_MAT3:
162         elemType = GL_FLOAT;
163         numCols = 3;
164         numRows = 3;
165         break;
166     case GL_FLOAT_MAT4:
167         elemType = GL_FLOAT;
168         numCols = 4;
169         numRows = 4;
170         break;
171     case GL_FLOAT_MAT2x3:
172         elemType = GL_FLOAT;
173         numCols = 2;
174         numRows = 3;
175         break;
176     case GL_FLOAT_MAT2x4:
177         elemType = GL_FLOAT;
178         numCols = 2;
179         numRows = 4;
180         break;
181     case GL_FLOAT_MAT3x2:
182         elemType = GL_FLOAT;
183         numCols = 3;
184         numRows = 2;
185         break;
186     case GL_FLOAT_MAT3x4:
187         elemType = GL_FLOAT;
188         numCols = 3;
189         numRows = 4;
190         break;
191     case GL_FLOAT_MAT4x2:
192         elemType = GL_FLOAT;
193         numCols = 4;
194         numRows = 2;
195         break;
196     case GL_FLOAT_MAT4x3:
197         elemType = GL_FLOAT;
198         numCols = 4;
199         numRows = 3;
200         break;
201     case GL_DOUBLE_MAT2:
202         elemType = GL_DOUBLE;
203         numCols = 2;
204         numRows = 2;
205         break;
206     case GL_DOUBLE_MAT3:
207         elemType = GL_DOUBLE;
208         numCols = 3;
209         numRows = 3;
210         break;
211     case GL_DOUBLE_MAT4:
212         elemType = GL_DOUBLE;
213         numCols = 4;
214         numRows = 4;
215         break;
216     case GL_DOUBLE_MAT2x3:
217         elemType = GL_DOUBLE;
218         numCols = 2;
219         numRows = 3;
220         break;
221     case GL_DOUBLE_MAT2x4:
222         elemType = GL_DOUBLE;
223         numCols = 2;
224         numRows = 4;
225         break;
226     case GL_DOUBLE_MAT3x2:
227         elemType = GL_DOUBLE;
228         numCols = 3;
229         numRows = 2;
230         break;
231     case GL_DOUBLE_MAT3x4:
232         elemType = GL_DOUBLE;
233         numCols = 3;
234         numRows = 4;
235         break;
236     case GL_DOUBLE_MAT4x2:
237         elemType = GL_DOUBLE;
238         numCols = 4;
239         numRows = 2;
240         break;
241     case GL_DOUBLE_MAT4x3:
242         elemType = GL_DOUBLE;
243         numCols = 4;
244         numRows = 3;
245         break;
246     case GL_SAMPLER_1D:
247     case GL_SAMPLER_2D:
248     case GL_SAMPLER_3D:
249     case GL_SAMPLER_CUBE:
250     case GL_SAMPLER_1D_SHADOW:
251     case GL_SAMPLER_2D_SHADOW:
252     case GL_SAMPLER_1D_ARRAY:
253     case GL_SAMPLER_2D_ARRAY:
254     case GL_SAMPLER_CUBE_MAP_ARRAY:
255     case GL_SAMPLER_1D_ARRAY_SHADOW:
256     case GL_SAMPLER_2D_ARRAY_SHADOW:
257     case GL_SAMPLER_2D_MULTISAMPLE:
258     case GL_SAMPLER_2D_MULTISAMPLE_ARRAY:
259     case GL_SAMPLER_CUBE_SHADOW:
260     case GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW:
261     case GL_SAMPLER_BUFFER:
262     case GL_SAMPLER_2D_RECT:
263     case GL_SAMPLER_2D_RECT_SHADOW:
264     case GL_INT_SAMPLER_1D:
265     case GL_INT_SAMPLER_2D:
266     case GL_INT_SAMPLER_3D:
267     case GL_INT_SAMPLER_CUBE:
268     case GL_INT_SAMPLER_1D_ARRAY:
269     case GL_INT_SAMPLER_2D_ARRAY:
270     case GL_INT_SAMPLER_CUBE_MAP_ARRAY:
271     case GL_INT_SAMPLER_2D_MULTISAMPLE:
272     case GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
273     case GL_INT_SAMPLER_BUFFER:
274     case GL_INT_SAMPLER_2D_RECT:
275     case GL_UNSIGNED_INT_SAMPLER_1D:
276     case GL_UNSIGNED_INT_SAMPLER_2D:
277     case GL_UNSIGNED_INT_SAMPLER_3D:
278     case GL_UNSIGNED_INT_SAMPLER_CUBE:
279     case GL_UNSIGNED_INT_SAMPLER_1D_ARRAY:
280     case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
281     case GL_UNSIGNED_INT_SAMPLER_CUBE_MAP_ARRAY:
282     case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE:
283     case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
284     case GL_UNSIGNED_INT_SAMPLER_BUFFER:
285     case GL_UNSIGNED_INT_SAMPLER_2D_RECT:
286         elemType = GL_INT;
287         break;
288     default:
289         os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, type);
290         elemType = GL_NONE;
291         numCols = 0;
292         numRows = 0;
293         return;
294     }
295 }
296     
297 static inline size_t
298 _glArrayPointer_size(GLint size, GLenum type, GLsizei stride, GLsizei count)
299 {
300     if (!count) {
301         return 0;
302     }
303
304     size_t elementSize = size*_gl_type_size(type);
305     if (!stride) {
306         stride = (GLsizei)elementSize;
307     }
308
309     return stride*(count - 1) + elementSize;
310 }
311
312 #define _glVertexPointer_size(size, type, stride, count) _glArrayPointer_size(size, type, stride, count)
313 #define _glNormalPointer_size(type, stride, count) _glArrayPointer_size(3, type, stride, count)
314 #define _glColorPointer_size(size, type, stride, count) _glArrayPointer_size(size, type, stride, count)
315 #define _glIndexPointer_size(type, stride, count) _glArrayPointer_size(1, type, stride, count)
316 #define _glTexCoordPointer_size(size, type, stride, count) _glArrayPointer_size(size, type, stride, count)
317 #define _glEdgeFlagPointer_size(stride, count) _glArrayPointer_size(1, GL_BOOL, stride, count)
318 #define _glFogCoordPointer_size(type, stride, count) _glArrayPointer_size(1, type, stride, count)
319 #define _glSecondaryColorPointer_size(size, type, stride, count) _glArrayPointer_size(size, type, stride, count)
320 #define _glVertexAttribPointer_size(size, type, normalized, stride, count) _glArrayPointer_size(size, type, stride, count)
321 #define _glVertexAttribPointerARB_size(size, type, normalized, stride, count) _glArrayPointer_size(size, type, stride, count)
322 #define _glVertexAttribPointerNV_size(size, type, stride, count) _glArrayPointer_size(size, type, stride, count)
323
324 static inline GLuint
325 _glDrawArrays_count(GLint first, GLsizei count)
326 {
327     if (!count) {
328         return 0;
329     }
330     return first + count;
331 }
332
333 #define _glDrawArraysEXT_count _glDrawArrays_count
334
335 static inline GLuint
336 _glDrawElementsBaseVertex_count(GLsizei count, GLenum type, const GLvoid *indices, GLint basevertex)
337 {
338     GLvoid *temp = 0;
339     GLint element_array_buffer = 0;
340
341     if (!count) {
342         return 0;
343     }
344
345     _glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &element_array_buffer);
346     if (element_array_buffer) {
347         // Read indices from index buffer object
348         GLintptr offset = (GLintptr)indices;
349         GLsizeiptr size = count*_gl_type_size(type);
350         GLvoid *temp = malloc(size);
351         if (!temp) {
352             return 0;
353         }
354         memset(temp, 0, size);
355         _glGetBufferSubData(GL_ELEMENT_ARRAY_BUFFER, offset, size, temp);
356         indices = temp;
357     } else {
358         if (!indices) {
359             return 0;
360         }
361     }
362
363     GLuint maxindex = 0;
364     GLsizei i;
365     if (type == GL_UNSIGNED_BYTE) {
366         const GLubyte *p = (const GLubyte *)indices;
367         for (i = 0; i < count; ++i) {
368             if (p[i] > maxindex) {
369                 maxindex = p[i];
370             }
371         }
372     } else if (type == GL_UNSIGNED_SHORT) {
373         const GLushort *p = (const GLushort *)indices;
374         for (i = 0; i < count; ++i) {
375             if (p[i] > maxindex) {
376                 maxindex = p[i];
377             }
378         }
379     } else if (type == GL_UNSIGNED_INT) {
380         const GLuint *p = (const GLuint *)indices;
381         for (i = 0; i < count; ++i) {
382             if (p[i] > maxindex) {
383                 maxindex = p[i];
384             }
385         }
386     } else {
387         os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, type);
388     }
389
390     if (element_array_buffer) {
391         free(temp);
392     }
393
394     maxindex += basevertex;
395
396     return maxindex + 1;
397 }
398
399 #define _glDrawRangeElementsBaseVertex_count(start, end, count, type, indices, basevertex) _glDrawElementsBaseVertex_count(count, type, indices, basevertex)
400
401 #define _glDrawElements_count(count, type, indices) _glDrawElementsBaseVertex_count(count, type, indices, 0);
402 #define _glDrawRangeElements_count(start, end, count, type, indices) _glDrawElements_count(count, type, indices)
403 #define _glDrawRangeElementsEXT_count _glDrawRangeElements_count
404
405 /* FIXME take in consideration instancing */
406 #define _glDrawArraysInstanced_count(first, count, primcount) _glDrawArrays_count(first, count)
407 #define _glDrawElementsInstanced_count(count, type, indices, primcount) _glDrawElements_count(count, type, indices)
408 #define _glDrawElementsInstancedBaseVertex_count(count, type, indices, primcount, basevertex) _glDrawElementsBaseVertex_count(count, type, indices, basevertex)
409 #define _glDrawRangeElementsInstanced_count(start, end, count, type, indices, primcount) _glDrawRangeElements_count(start, end, count, type, indices)
410 #define _glDrawRangeElementsInstancedBaseVertex_count(start, end, count, type, indices, primcount, basevertex) _glDrawRangeElementsBaseVertex_count(start, end, count, type, indices, basevertex)
411
412 #define _glDrawArraysInstancedBaseInstance_count(first, count, primcount, baseinstance) _glDrawArrays_count(first, count)
413 #define _glDrawElementsInstancedBaseInstance_count(count, type, indices, primcount, baseinstance) _glDrawElements_count(count, type, indices)
414 #define _glDrawElementsInstancedBaseVertexBaseInstance_count(count, type, indices, primcount, basevertex, baseinstance) _glDrawElementsBaseVertex_count(count, type, indices, basevertex)
415
416 #define _glDrawArraysInstancedARB_count _glDrawArraysInstanced_count
417 #define _glDrawElementsInstancedARB_count _glDrawElementsInstanced_count
418 #define _glDrawArraysInstancedEXT_count _glDrawArraysInstanced_count
419 #define _glDrawElementsInstancedEXT_count _glDrawElementsInstanced_count
420
421 static inline GLuint
422 _glDrawArraysIndirect_count(const GLvoid *indirect) {
423     os::log("apitrace: warning: %s: unsupported\n", __FUNCTION__);
424     return 0;
425 }
426
427 static inline GLuint
428 _glDrawElementsIndirect_count(GLenum type, const GLvoid *indirect) {
429     os::log("apitrace: warning: %s: unsupported\n", __FUNCTION__);
430     return 0;
431 }
432
433 static inline GLuint
434 _glMultiDrawArrays_count(const GLint *first, const GLsizei *count, GLsizei primcount) {
435     GLuint _count = 0;
436     for (GLsizei prim = 0; prim < primcount; ++prim) {
437         GLuint _count_prim = _glDrawArrays_count(first[prim], count[prim]);
438         _count = std::max(_count, _count_prim);
439     }
440     return _count;
441 }
442
443 static inline GLuint
444 _glMultiDrawElements_count(const GLsizei *count, GLenum type, const GLvoid* *indices, GLsizei primcount) {
445     GLuint _count = 0;
446     for (GLsizei prim = 0; prim < primcount; ++prim) {
447         GLuint _count_prim = _glDrawElements_count(count[prim], type, indices[prim]);
448         _count = std::max(_count, _count_prim);
449     }
450     return _count;
451 }
452
453 static inline GLuint
454 _glMultiDrawElementsBaseVertex_count(const GLsizei *count, GLenum type, const GLvoid* *indices, GLsizei primcount, const GLint * basevertex) {
455     GLuint _count = 0;
456     for (GLsizei prim = 0; prim < primcount; ++prim) {
457         GLuint _count_prim = _glDrawElementsBaseVertex_count(count[prim], type, indices[prim], basevertex[prim]);
458         _count = std::max(_count, _count_prim);
459     }
460     return _count;
461 }
462
463 #define _glMultiDrawArraysEXT_count _glMultiDrawArrays_count
464 #define _glMultiDrawElementsEXT_count _glMultiDrawElements_count
465
466 #define _glMultiModeDrawArraysIBM_count(first, count, primcount, modestride) _glMultiDrawArrays_count(first, count, primcount)
467 #define _glMultiModeDrawElementsIBM_count(count, type, indices, primcount, modestride) _glMultiDrawElements_count(count, type, (const GLvoid **)indices, primcount)
468
469
470 static inline size_t
471 _glCallLists_size(GLsizei n, GLenum type)
472 {
473     return n*_gl_type_size(type);
474 }
475
476 static inline size_t
477 _glMap1d_size(GLenum target, GLint stride, GLint order)
478 {
479     if (order < 1) {
480         return 0;
481     }
482
483     GLint channels;
484     switch (target) {
485     case GL_MAP1_INDEX:
486     case GL_MAP1_TEXTURE_COORD_1:
487         channels = 1;
488         break;
489     case GL_MAP1_TEXTURE_COORD_2:
490         channels = 2;
491         break;
492     case GL_MAP1_NORMAL:
493     case GL_MAP1_TEXTURE_COORD_3:
494     case GL_MAP1_VERTEX_3:
495         channels = 3;
496         break;
497     case GL_MAP1_COLOR_4:
498     case GL_MAP1_TEXTURE_COORD_4:
499     case GL_MAP1_VERTEX_4:
500         channels = 4;
501         break;
502     default:
503         os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, target);
504         return 0;
505     }
506
507     if (stride < channels) {
508         return 0;
509     }
510
511     return channels + stride * (order - 1);
512 }
513
514 #define _glMap1f_size _glMap1d_size
515
516 static inline size_t
517 _glMap2d_size(GLenum target, GLint ustride, GLint uorder, GLint vstride, GLint vorder)
518 {
519     if (uorder < 1 || vorder < 1) {
520         return 0;
521     }
522
523     GLint channels;
524     switch (target) {
525     case GL_MAP2_INDEX:
526     case GL_MAP2_TEXTURE_COORD_1:
527         channels = 1;
528         break;
529     case GL_MAP2_TEXTURE_COORD_2:
530         channels = 2;
531         break;
532     case GL_MAP2_NORMAL:
533     case GL_MAP2_TEXTURE_COORD_3:
534     case GL_MAP2_VERTEX_3:
535         channels = 3;
536         break;
537     case GL_MAP2_COLOR_4:
538     case GL_MAP2_TEXTURE_COORD_4:
539     case GL_MAP2_VERTEX_4:
540         channels = 4;
541         break;
542     default:
543         os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, target);
544         return 0;
545     }
546
547     if (ustride < channels || vstride < channels) {
548         return 0;
549     }
550
551     return channels + 
552            ustride * (uorder - 1) +
553            vstride * (vorder - 1);
554 }
555
556 #define _glMap2f_size _glMap2d_size
557
558 static inline unsigned
559 _gl_format_channels(GLenum format) {
560     switch (format) {
561     case GL_COLOR_INDEX:
562     case GL_RED:
563     case GL_GREEN:
564     case GL_BLUE:
565     case GL_ALPHA:
566     case GL_INTENSITY:
567     case GL_LUMINANCE:
568     case GL_DEPTH_COMPONENT:
569     case GL_STENCIL_INDEX:
570         return 1;
571     case GL_DEPTH_STENCIL:
572     case GL_LUMINANCE_ALPHA:
573     case GL_RG:
574     case GL_HILO_NV:
575     case GL_DSDT_NV:
576         return 2;
577     case GL_RGB:
578     case GL_BGR:
579     case GL_DSDT_MAG_NV:
580         return 3;
581     case GL_RGBA:
582     case GL_BGRA:
583     case GL_ABGR_EXT:
584     case GL_CMYK_EXT:
585     case GL_DSDT_MAG_VIB_NV:
586         return 4;
587     case GL_CMYKA_EXT:
588         return 5;
589     default:
590         os::log("apitrace: warning: %s: unexpected format GLenum 0x%04X\n", __FUNCTION__, format);
591         return 0;
592     }
593 }
594
595 template<class X>
596 static inline bool
597 _is_pot(X x) {
598     return (x & (x - 1)) == 0;
599 }
600
601 template<class X, class Y>
602 static inline X
603 _align(X x, Y y) {
604     return (x + (y - 1)) & ~(y - 1);
605 }
606
607 static inline size_t
608 _gl_image_size(GLenum format, GLenum type, GLsizei width, GLsizei height, GLsizei depth, GLboolean has_unpack_subimage) {
609     unsigned num_channels = _gl_format_channels(format);
610
611     unsigned bits_per_pixel;
612     switch (type) {
613     case GL_BITMAP:
614         bits_per_pixel = 1;
615         break;
616     case GL_BYTE:
617     case GL_UNSIGNED_BYTE:
618         bits_per_pixel = 8 * num_channels;
619         break;
620     case GL_SHORT:
621     case GL_UNSIGNED_SHORT:
622     case GL_HALF_FLOAT:
623         bits_per_pixel = 16 * num_channels;
624         break;
625     case GL_INT:
626     case GL_UNSIGNED_INT:
627     case GL_FLOAT:
628         bits_per_pixel = 32 * num_channels;
629         break;
630     case GL_UNSIGNED_BYTE_3_3_2:
631     case GL_UNSIGNED_BYTE_2_3_3_REV:
632         bits_per_pixel = 8;
633         break;
634     case GL_UNSIGNED_SHORT_4_4_4_4:
635     case GL_UNSIGNED_SHORT_4_4_4_4_REV:
636     case GL_UNSIGNED_SHORT_5_5_5_1:
637     case GL_UNSIGNED_SHORT_1_5_5_5_REV:
638     case GL_UNSIGNED_SHORT_5_6_5:
639     case GL_UNSIGNED_SHORT_5_6_5_REV:
640     case GL_UNSIGNED_SHORT_8_8_MESA:
641     case GL_UNSIGNED_SHORT_8_8_REV_MESA:
642         bits_per_pixel = 16;
643         break;
644     case GL_UNSIGNED_INT_8_8_8_8:
645     case GL_UNSIGNED_INT_8_8_8_8_REV:
646     case GL_UNSIGNED_INT_10_10_10_2:
647     case GL_UNSIGNED_INT_2_10_10_10_REV:
648     case GL_UNSIGNED_INT_24_8:
649     case GL_UNSIGNED_INT_10F_11F_11F_REV:
650     case GL_UNSIGNED_INT_5_9_9_9_REV:
651     case GL_UNSIGNED_INT_S8_S8_8_8_NV:
652     case GL_UNSIGNED_INT_8_8_S8_S8_REV_NV:
653         bits_per_pixel = 32;
654         break;
655     case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
656         bits_per_pixel = 64;
657         break;
658     default:
659         os::log("apitrace: warning: %s: unexpected type GLenum 0x%04X\n", __FUNCTION__, type);
660         bits_per_pixel = 0;
661         break;
662     }
663
664     GLint alignment = 4;
665     GLint row_length = 0;
666     GLint image_height = 0;
667     GLint skip_rows = 0;
668     GLint skip_pixels = 0;
669     GLint skip_images = 0;
670
671     _glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
672     if (has_unpack_subimage) {
673         _glGetIntegerv(GL_UNPACK_ROW_LENGTH,   &row_length);
674         _glGetIntegerv(GL_UNPACK_IMAGE_HEIGHT, &image_height);
675         _glGetIntegerv(GL_UNPACK_SKIP_ROWS,    &skip_rows);
676         _glGetIntegerv(GL_UNPACK_SKIP_PIXELS,  &skip_pixels);
677         _glGetIntegerv(GL_UNPACK_SKIP_IMAGES,  &skip_images);
678     }
679
680     if (row_length <= 0) {
681         row_length = width;
682     }
683
684     size_t row_stride = (row_length*bits_per_pixel + 7)/8;
685
686     if ((GLint)bits_per_pixel < alignment*8 &&
687         (bits_per_pixel & 7) == 0 &&
688         _is_pot(bits_per_pixel)) {
689         row_stride = _align(row_stride, alignment);
690     }
691
692     if (image_height <= 0) {
693         image_height = height;
694     }
695
696     /* XXX: GL_UNPACK_IMAGE_HEIGHT and GL_UNPACK_SKIP_IMAGES should probably
697      * not be considered for pixel rectangles. */
698
699     size_t image_stride = image_height*row_stride;
700
701     size_t size = depth*image_stride;
702
703     size += (skip_pixels*bits_per_pixel + 7)/8;
704     size += skip_rows*row_stride;
705     size += skip_images*image_stride;
706
707     return size;
708 }
709
710 // note that can_unpack_subimage() is generated by gltrace.py
711 #define _glTexImage3D_size(format, type, width, height, depth) _gl_image_size(format, type, width, height, depth, can_unpack_subimage())
712 #define _glTexImage2D_size(format, type, width, height)        _gl_image_size(format, type, width, height, 1, can_unpack_subimage())
713 #define _glTexImage1D_size(format, type, width)                _gl_image_size(format, type, width, 1, 1, can_unpack_subimage())
714
715 #define _glTexSubImage3D_size(format, type, width, height, depth) _glTexImage3D_size(format, type, width, height, depth)
716 #define _glTexSubImage2D_size(format, type, width, height)        _glTexImage2D_size(format, type, width, height)
717 #define _glTexSubImage1D_size(format, type, width)                _glTexImage1D_size(format, type, width)
718
719 #define _glTexImage3DEXT_size _glTexImage3D_size
720 #define _glTexImage2DEXT_size _glTexImage2D_size
721 #define _glTexImage1DEXT_size _glTexImage1D_size
722 #define _glTexSubImage3DEXT_size _glTexSubImage3D_size
723 #define _glTexSubImage2DEXT_size _glTexSubImage2D_size
724 #define _glTexSubImage1DEXT_size _glTexSubImage1D_size
725
726 #define _glTextureImage3DEXT_size _glTexImage3D_size
727 #define _glTextureImage2DEXT_size _glTexImage2D_size
728 #define _glTextureImage1DEXT_size _glTexImage1D_size
729 #define _glTextureSubImage3DEXT_size _glTexSubImage3D_size
730 #define _glTextureSubImage2DEXT_size _glTexSubImage2D_size
731 #define _glTextureSubImage1DEXT_size _glTexSubImage1D_size
732
733 #define _glMultiTexImage3DEXT_size _glTexImage3D_size
734 #define _glMultiTexImage2DEXT_size _glTexImage2D_size
735 #define _glMultiTexImage1DEXT_size _glTexImage1D_size
736 #define _glMultiTexSubImage3DEXT_size _glTexSubImage3D_size
737 #define _glMultiTexSubImage2DEXT_size _glTexSubImage2D_size
738 #define _glMultiTexSubImage1DEXT_size _glTexSubImage1D_size
739
740 #define _glDrawPixels_size(format, type, width, height) _glTexImage2D_size(format, type, width, height)
741 #define _glConvolutionFilter1D_size(format, type, width) _glTexImage1D_size(format, type, width)
742 #define _glConvolutionFilter2D_size(format, type, width, height) _glTexImage2D_size(format, type, width, height)
743 #define _glColorTable_size(format, type, width) _glTexImage1D_size(format, type, width)
744 #define _glColorSubTable_size(format, type, count) _glColorTable_size(format, type, count)
745
746 #define _glBitmap_size(width, height) _glTexImage2D_size(GL_COLOR_INDEX, GL_BITMAP, width, height)
747 #define _glPolygonStipple_size() _glBitmap_size(32, 32)
748
749 static inline size_t
750 _glClearBuffer_size(GLenum buffer)
751 {
752     switch (buffer) {
753     case GL_COLOR:
754     case GL_FRONT:
755     case GL_BACK:
756     case GL_LEFT:
757     case GL_RIGHT:
758     case GL_FRONT_AND_BACK:
759         return 4;
760     case GL_DEPTH:
761     case GL_STENCIL:
762         return 1;
763     default:
764         os::log("apitrace: warning: %s: unexpected buffer GLenum 0x%04X\n", __FUNCTION__, buffer);
765         return 0;
766     }
767 }
768
769 /* 
770  * attribute list, terminated by the given terminator.
771  */
772 template<class T>
773 static inline size_t
774 _AttribList_size(const T *pAttribList, const T terminator = static_cast<T>(0))
775 {
776     size_t size = 0;
777
778     if (pAttribList) {
779         do {
780             ++size;
781         } while (*pAttribList++ != terminator);
782     }
783
784     return size;
785 }
786
787
788 /*
789  * (key, value) attribute list, terminated by the given terminator.
790  */
791 template<class T>
792 static inline size_t
793 _AttribPairList_size(const T *pAttribList, const T terminator = static_cast<T>(0))
794 {
795     size_t size = 0;
796
797     if (pAttribList) {
798         while (pAttribList[size] != terminator) {
799             size += 2;
800         }
801         // terminator also counts
802         ++size;
803     }
804
805     return size;
806 }
807
808
809 #endif /* _GL_SIZE_HPP_ */