]> git.cworth.org Git - apitrace/blob - helpers/glsize.hpp
Handle GL_ARB_vertex_array_bgra better.
[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     if (size == GL_BGRA) {
305         size = 4; 
306     }
307
308     if (size > 4) {
309         os::log("apitrace: warning: %s: unexpected size 0x%04X\n", __FUNCTION__, size);
310     }
311
312     size_t elementSize = size*_gl_type_size(type);
313     if (!stride) {
314         stride = (GLsizei)elementSize;
315     }
316
317     return stride*(count - 1) + elementSize;
318 }
319
320 #define _glVertexPointer_size(size, type, stride, count) _glArrayPointer_size(size, type, stride, count)
321 #define _glNormalPointer_size(type, stride, count) _glArrayPointer_size(3, type, stride, count)
322 #define _glColorPointer_size(size, type, stride, count) _glArrayPointer_size(size, type, stride, count)
323 #define _glIndexPointer_size(type, stride, count) _glArrayPointer_size(1, type, stride, count)
324 #define _glTexCoordPointer_size(size, type, stride, count) _glArrayPointer_size(size, type, stride, count)
325 #define _glEdgeFlagPointer_size(stride, count) _glArrayPointer_size(1, GL_BOOL, stride, count)
326 #define _glFogCoordPointer_size(type, stride, count) _glArrayPointer_size(1, type, stride, count)
327 #define _glSecondaryColorPointer_size(size, type, stride, count) _glArrayPointer_size(size, type, stride, count)
328 #define _glVertexAttribPointer_size(size, type, normalized, stride, count) _glArrayPointer_size(size, type, stride, count)
329 #define _glVertexAttribPointerARB_size(size, type, normalized, stride, count) _glArrayPointer_size(size, type, stride, count)
330 #define _glVertexAttribPointerNV_size(size, type, stride, count) _glArrayPointer_size(size, type, stride, count)
331
332 static inline GLuint
333 _glDrawArrays_count(GLint first, GLsizei count)
334 {
335     if (!count) {
336         return 0;
337     }
338     return first + count;
339 }
340
341 #define _glDrawArraysEXT_count _glDrawArrays_count
342
343 static inline GLuint
344 _glDrawElementsBaseVertex_count(GLsizei count, GLenum type, const GLvoid *indices, GLint basevertex)
345 {
346     GLvoid *temp = 0;
347     GLint element_array_buffer = 0;
348
349     if (!count) {
350         return 0;
351     }
352
353     _glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &element_array_buffer);
354     if (element_array_buffer) {
355         // Read indices from index buffer object
356         GLintptr offset = (GLintptr)indices;
357         GLsizeiptr size = count*_gl_type_size(type);
358         GLvoid *temp = malloc(size);
359         if (!temp) {
360             return 0;
361         }
362         memset(temp, 0, size);
363         _glGetBufferSubData(GL_ELEMENT_ARRAY_BUFFER, offset, size, temp);
364         indices = temp;
365     } else {
366         if (!indices) {
367             return 0;
368         }
369     }
370
371     GLuint maxindex = 0;
372     GLsizei i;
373     if (type == GL_UNSIGNED_BYTE) {
374         const GLubyte *p = (const GLubyte *)indices;
375         for (i = 0; i < count; ++i) {
376             if (p[i] > maxindex) {
377                 maxindex = p[i];
378             }
379         }
380     } else if (type == GL_UNSIGNED_SHORT) {
381         const GLushort *p = (const GLushort *)indices;
382         for (i = 0; i < count; ++i) {
383             if (p[i] > maxindex) {
384                 maxindex = p[i];
385             }
386         }
387     } else if (type == GL_UNSIGNED_INT) {
388         const GLuint *p = (const GLuint *)indices;
389         for (i = 0; i < count; ++i) {
390             if (p[i] > maxindex) {
391                 maxindex = p[i];
392             }
393         }
394     } else {
395         os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, type);
396     }
397
398     if (element_array_buffer) {
399         free(temp);
400     }
401
402     maxindex += basevertex;
403
404     return maxindex + 1;
405 }
406
407 #define _glDrawRangeElementsBaseVertex_count(start, end, count, type, indices, basevertex) _glDrawElementsBaseVertex_count(count, type, indices, basevertex)
408
409 #define _glDrawElements_count(count, type, indices) _glDrawElementsBaseVertex_count(count, type, indices, 0);
410 #define _glDrawRangeElements_count(start, end, count, type, indices) _glDrawElements_count(count, type, indices)
411 #define _glDrawRangeElementsEXT_count _glDrawRangeElements_count
412
413 /* FIXME take in consideration instancing */
414 #define _glDrawArraysInstanced_count(first, count, primcount) _glDrawArrays_count(first, count)
415 #define _glDrawElementsInstanced_count(count, type, indices, primcount) _glDrawElements_count(count, type, indices)
416 #define _glDrawElementsInstancedBaseVertex_count(count, type, indices, primcount, basevertex) _glDrawElementsBaseVertex_count(count, type, indices, basevertex)
417 #define _glDrawRangeElementsInstanced_count(start, end, count, type, indices, primcount) _glDrawRangeElements_count(start, end, count, type, indices)
418 #define _glDrawRangeElementsInstancedBaseVertex_count(start, end, count, type, indices, primcount, basevertex) _glDrawRangeElementsBaseVertex_count(start, end, count, type, indices, basevertex)
419
420 #define _glDrawArraysInstancedBaseInstance_count(first, count, primcount, baseinstance) _glDrawArrays_count(first, count)
421 #define _glDrawElementsInstancedBaseInstance_count(count, type, indices, primcount, baseinstance) _glDrawElements_count(count, type, indices)
422 #define _glDrawElementsInstancedBaseVertexBaseInstance_count(count, type, indices, primcount, basevertex, baseinstance) _glDrawElementsBaseVertex_count(count, type, indices, basevertex)
423
424 #define _glDrawArraysInstancedARB_count _glDrawArraysInstanced_count
425 #define _glDrawElementsInstancedARB_count _glDrawElementsInstanced_count
426 #define _glDrawArraysInstancedEXT_count _glDrawArraysInstanced_count
427 #define _glDrawElementsInstancedEXT_count _glDrawElementsInstanced_count
428
429 static inline GLuint
430 _glDrawArraysIndirect_count(const GLvoid *indirect) {
431     os::log("apitrace: warning: %s: unsupported\n", __FUNCTION__);
432     return 0;
433 }
434
435 static inline GLuint
436 _glDrawElementsIndirect_count(GLenum type, const GLvoid *indirect) {
437     os::log("apitrace: warning: %s: unsupported\n", __FUNCTION__);
438     return 0;
439 }
440
441 static inline GLuint
442 _glMultiDrawArrays_count(const GLint *first, const GLsizei *count, GLsizei drawcount) {
443     GLuint _count = 0;
444     for (GLsizei draw = 0; draw < drawcount; ++draw) {
445         GLuint _count_draw = _glDrawArrays_count(first[draw], count[draw]);
446         _count = std::max(_count, _count_draw);
447     }
448     return _count;
449 }
450
451 static inline GLuint
452 _glMultiDrawElements_count(const GLsizei *count, GLenum type, const GLvoid* const *indices, GLsizei drawcount) {
453     GLuint _count = 0;
454     for (GLsizei draw = 0; draw < drawcount; ++draw) {
455         GLuint _count_draw = _glDrawElements_count(count[draw], type, indices[draw]);
456         _count = std::max(_count, _count_draw);
457     }
458     return _count;
459 }
460
461 static inline GLuint
462 _glMultiDrawElementsBaseVertex_count(const GLsizei *count, GLenum type, const GLvoid* const *indices, GLsizei drawcount, const GLint * basevertex) {
463     GLuint _count = 0;
464     for (GLsizei draw = 0; draw < drawcount; ++draw) {
465         GLuint _count_draw = _glDrawElementsBaseVertex_count(count[draw], type, indices[draw], basevertex[draw]);
466         _count = std::max(_count, _count_draw);
467     }
468     return _count;
469 }
470
471 #define _glMultiDrawArraysEXT_count _glMultiDrawArrays_count
472 #define _glMultiDrawElementsEXT_count _glMultiDrawElements_count
473
474 #define _glMultiModeDrawArraysIBM_count(first, count, drawcount, modestride) _glMultiDrawArrays_count(first, count, drawcount)
475 #define _glMultiModeDrawElementsIBM_count(count, type, indices, drawcount, modestride) _glMultiDrawElements_count(count, type, (const GLvoid **)indices, drawcount)
476
477
478 static inline size_t
479 _glCallLists_size(GLsizei n, GLenum type)
480 {
481     return n*_gl_type_size(type);
482 }
483
484 static inline size_t
485 _glMap1d_size(GLenum target, GLint stride, GLint order)
486 {
487     if (order < 1) {
488         return 0;
489     }
490
491     GLint channels;
492     switch (target) {
493     case GL_MAP1_INDEX:
494     case GL_MAP1_TEXTURE_COORD_1:
495         channels = 1;
496         break;
497     case GL_MAP1_TEXTURE_COORD_2:
498         channels = 2;
499         break;
500     case GL_MAP1_NORMAL:
501     case GL_MAP1_TEXTURE_COORD_3:
502     case GL_MAP1_VERTEX_3:
503         channels = 3;
504         break;
505     case GL_MAP1_COLOR_4:
506     case GL_MAP1_TEXTURE_COORD_4:
507     case GL_MAP1_VERTEX_4:
508         channels = 4;
509         break;
510     default:
511         os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, target);
512         return 0;
513     }
514
515     if (stride < channels) {
516         return 0;
517     }
518
519     return channels + stride * (order - 1);
520 }
521
522 #define _glMap1f_size _glMap1d_size
523
524 static inline size_t
525 _glMap2d_size(GLenum target, GLint ustride, GLint uorder, GLint vstride, GLint vorder)
526 {
527     if (uorder < 1 || vorder < 1) {
528         return 0;
529     }
530
531     GLint channels;
532     switch (target) {
533     case GL_MAP2_INDEX:
534     case GL_MAP2_TEXTURE_COORD_1:
535         channels = 1;
536         break;
537     case GL_MAP2_TEXTURE_COORD_2:
538         channels = 2;
539         break;
540     case GL_MAP2_NORMAL:
541     case GL_MAP2_TEXTURE_COORD_3:
542     case GL_MAP2_VERTEX_3:
543         channels = 3;
544         break;
545     case GL_MAP2_COLOR_4:
546     case GL_MAP2_TEXTURE_COORD_4:
547     case GL_MAP2_VERTEX_4:
548         channels = 4;
549         break;
550     default:
551         os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, target);
552         return 0;
553     }
554
555     if (ustride < channels || vstride < channels) {
556         return 0;
557     }
558
559     return channels + 
560            ustride * (uorder - 1) +
561            vstride * (vorder - 1);
562 }
563
564 #define _glMap2f_size _glMap2d_size
565
566 static inline unsigned
567 _gl_format_channels(GLenum format) {
568     switch (format) {
569     case GL_COLOR_INDEX:
570     case GL_RED:
571     case GL_GREEN:
572     case GL_BLUE:
573     case GL_ALPHA:
574     case GL_INTENSITY:
575     case GL_LUMINANCE:
576     case GL_DEPTH_COMPONENT:
577     case GL_STENCIL_INDEX:
578         return 1;
579     case GL_DEPTH_STENCIL:
580     case GL_LUMINANCE_ALPHA:
581     case GL_RG:
582     case GL_HILO_NV:
583     case GL_DSDT_NV:
584         return 2;
585     case GL_RGB:
586     case GL_BGR:
587     case GL_DSDT_MAG_NV:
588         return 3;
589     case GL_RGBA:
590     case GL_BGRA:
591     case GL_ABGR_EXT:
592     case GL_CMYK_EXT:
593     case GL_DSDT_MAG_VIB_NV:
594         return 4;
595     case GL_CMYKA_EXT:
596         return 5;
597     default:
598         os::log("apitrace: warning: %s: unexpected format GLenum 0x%04X\n", __FUNCTION__, format);
599         return 0;
600     }
601 }
602
603 template<class X>
604 static inline bool
605 _is_pot(X x) {
606     return (x & (x - 1)) == 0;
607 }
608
609 template<class X, class Y>
610 static inline X
611 _align(X x, Y y) {
612     return (x + (y - 1)) & ~(y - 1);
613 }
614
615 static inline size_t
616 _gl_image_size(GLenum format, GLenum type, GLsizei width, GLsizei height, GLsizei depth, GLboolean has_unpack_subimage) {
617     unsigned num_channels = _gl_format_channels(format);
618
619     unsigned bits_per_element;
620     unsigned bits_per_pixel;
621     switch (type) {
622     case GL_BITMAP:
623         bits_per_pixel = bits_per_element = 1;
624         break;
625     case GL_BYTE:
626     case GL_UNSIGNED_BYTE:
627         bits_per_element = 8;
628         bits_per_pixel = bits_per_element * num_channels;
629         break;
630     case GL_SHORT:
631     case GL_UNSIGNED_SHORT:
632     case GL_HALF_FLOAT:
633         bits_per_element = 16;
634         bits_per_pixel = bits_per_element * num_channels;
635         break;
636     case GL_INT:
637     case GL_UNSIGNED_INT:
638     case GL_FLOAT:
639         bits_per_element = 32;
640         bits_per_pixel = bits_per_element * num_channels;
641         break;
642     case GL_UNSIGNED_BYTE_3_3_2:
643     case GL_UNSIGNED_BYTE_2_3_3_REV:
644         bits_per_pixel = bits_per_element = 8;
645         break;
646     case GL_UNSIGNED_SHORT_4_4_4_4:
647     case GL_UNSIGNED_SHORT_4_4_4_4_REV:
648     case GL_UNSIGNED_SHORT_5_5_5_1:
649     case GL_UNSIGNED_SHORT_1_5_5_5_REV:
650     case GL_UNSIGNED_SHORT_5_6_5:
651     case GL_UNSIGNED_SHORT_5_6_5_REV:
652     case GL_UNSIGNED_SHORT_8_8_MESA:
653     case GL_UNSIGNED_SHORT_8_8_REV_MESA:
654         bits_per_pixel = bits_per_element = 16;
655         break;
656     case GL_UNSIGNED_INT_8_8_8_8:
657     case GL_UNSIGNED_INT_8_8_8_8_REV:
658     case GL_UNSIGNED_INT_10_10_10_2:
659     case GL_UNSIGNED_INT_2_10_10_10_REV:
660     case GL_UNSIGNED_INT_24_8:
661     case GL_UNSIGNED_INT_10F_11F_11F_REV:
662     case GL_UNSIGNED_INT_5_9_9_9_REV:
663     case GL_UNSIGNED_INT_S8_S8_8_8_NV:
664     case GL_UNSIGNED_INT_8_8_S8_S8_REV_NV:
665         bits_per_pixel = bits_per_element = 32;
666         break;
667     case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
668         bits_per_pixel = bits_per_element = 64;
669         break;
670     default:
671         os::log("apitrace: warning: %s: unexpected type GLenum 0x%04X\n", __FUNCTION__, type);
672         bits_per_pixel = bits_per_element = 0;
673         break;
674     }
675
676     GLint alignment = 4;
677     GLint row_length = 0;
678     GLint image_height = 0;
679     GLint skip_rows = 0;
680     GLint skip_pixels = 0;
681     GLint skip_images = 0;
682
683     _glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
684     if (has_unpack_subimage) {
685         _glGetIntegerv(GL_UNPACK_ROW_LENGTH,   &row_length);
686         _glGetIntegerv(GL_UNPACK_IMAGE_HEIGHT, &image_height);
687         _glGetIntegerv(GL_UNPACK_SKIP_ROWS,    &skip_rows);
688         _glGetIntegerv(GL_UNPACK_SKIP_PIXELS,  &skip_pixels);
689         _glGetIntegerv(GL_UNPACK_SKIP_IMAGES,  &skip_images);
690     }
691
692     if (row_length <= 0) {
693         row_length = width;
694     }
695
696     size_t row_stride = (row_length*bits_per_pixel + 7)/8;
697
698     if ((bits_per_element == 1*8 ||
699          bits_per_element == 2*8 ||
700          bits_per_element == 4*8 ||
701          bits_per_element == 8*8) &&
702         (GLint)bits_per_element < alignment*8) {
703         row_stride = _align(row_stride, alignment);
704     }
705
706     if (image_height <= 0) {
707         image_height = height;
708     }
709
710     /* XXX: GL_UNPACK_IMAGE_HEIGHT and GL_UNPACK_SKIP_IMAGES should probably
711      * not be considered for pixel rectangles. */
712
713     size_t image_stride = image_height*row_stride;
714
715     size_t size = depth*image_stride;
716
717     size += (skip_pixels*bits_per_pixel + 7)/8;
718     size += skip_rows*row_stride;
719     size += skip_images*image_stride;
720
721     return size;
722 }
723
724 // note that can_unpack_subimage() is generated by gltrace.py
725 #define _glTexImage3D_size(format, type, width, height, depth) _gl_image_size(format, type, width, height, depth, can_unpack_subimage())
726 #define _glTexImage2D_size(format, type, width, height)        _gl_image_size(format, type, width, height, 1, can_unpack_subimage())
727 #define _glTexImage1D_size(format, type, width)                _gl_image_size(format, type, width, 1, 1, can_unpack_subimage())
728
729 #define _glTexSubImage3D_size(format, type, width, height, depth) _glTexImage3D_size(format, type, width, height, depth)
730 #define _glTexSubImage2D_size(format, type, width, height)        _glTexImage2D_size(format, type, width, height)
731 #define _glTexSubImage1D_size(format, type, width)                _glTexImage1D_size(format, type, width)
732
733 #define _glTexImage3DEXT_size _glTexImage3D_size
734 #define _glTexImage2DEXT_size _glTexImage2D_size
735 #define _glTexImage1DEXT_size _glTexImage1D_size
736 #define _glTexSubImage3DEXT_size _glTexSubImage3D_size
737 #define _glTexSubImage2DEXT_size _glTexSubImage2D_size
738 #define _glTexSubImage1DEXT_size _glTexSubImage1D_size
739
740 #define _glTextureImage3DEXT_size _glTexImage3D_size
741 #define _glTextureImage2DEXT_size _glTexImage2D_size
742 #define _glTextureImage1DEXT_size _glTexImage1D_size
743 #define _glTextureSubImage3DEXT_size _glTexSubImage3D_size
744 #define _glTextureSubImage2DEXT_size _glTexSubImage2D_size
745 #define _glTextureSubImage1DEXT_size _glTexSubImage1D_size
746
747 #define _glMultiTexImage3DEXT_size _glTexImage3D_size
748 #define _glMultiTexImage2DEXT_size _glTexImage2D_size
749 #define _glMultiTexImage1DEXT_size _glTexImage1D_size
750 #define _glMultiTexSubImage3DEXT_size _glTexSubImage3D_size
751 #define _glMultiTexSubImage2DEXT_size _glTexSubImage2D_size
752 #define _glMultiTexSubImage1DEXT_size _glTexSubImage1D_size
753
754 #define _glDrawPixels_size(format, type, width, height) _glTexImage2D_size(format, type, width, height)
755 #define _glConvolutionFilter1D_size(format, type, width) _glTexImage1D_size(format, type, width)
756 #define _glConvolutionFilter2D_size(format, type, width, height) _glTexImage2D_size(format, type, width, height)
757 #define _glColorTable_size(format, type, width) _glTexImage1D_size(format, type, width)
758 #define _glColorSubTable_size(format, type, count) _glColorTable_size(format, type, count)
759
760 #define _glBitmap_size(width, height) _glTexImage2D_size(GL_COLOR_INDEX, GL_BITMAP, width, height)
761 #define _glPolygonStipple_size() _glBitmap_size(32, 32)
762
763 static inline size_t
764 _glClearBuffer_size(GLenum buffer)
765 {
766     switch (buffer) {
767     case GL_COLOR:
768     case GL_FRONT:
769     case GL_BACK:
770     case GL_LEFT:
771     case GL_RIGHT:
772     case GL_FRONT_AND_BACK:
773         return 4;
774     case GL_DEPTH:
775     case GL_STENCIL:
776         return 1;
777     default:
778         os::log("apitrace: warning: %s: unexpected buffer GLenum 0x%04X\n", __FUNCTION__, buffer);
779         return 0;
780     }
781 }
782
783 /* 
784  * attribute list, terminated by the given terminator.
785  */
786 template<class T>
787 static inline size_t
788 _AttribList_size(const T *pAttribList, const T terminator = static_cast<T>(0))
789 {
790     size_t size = 0;
791
792     if (pAttribList) {
793         do {
794             ++size;
795         } while (*pAttribList++ != terminator);
796     }
797
798     return size;
799 }
800
801
802 /*
803  * (key, value) attribute list, terminated by the given terminator.
804  */
805 template<class T>
806 static inline size_t
807 _AttribPairList_size(const T *pAttribList, const T terminator = static_cast<T>(0))
808 {
809     size_t size = 0;
810
811     if (pAttribList) {
812         while (pAttribList[size] != terminator) {
813             size += 2;
814         }
815         // terminator also counts
816         ++size;
817     }
818
819     return size;
820 }
821
822
823 #endif /* _GL_SIZE_HPP_ */