]> git.cworth.org Git - apitrace/blob - helpers/glsize.hpp
gltrace,glretrace: Better support for indirect draws from user memory.
[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 /**
333  * Same as glGetIntegerv, but passing the result in the return value.
334  */
335 static inline GLint
336 _glGetInteger(GLenum pname) {
337     GLint param = 0;
338     _glGetIntegerv(pname, &param);
339     return param;
340 }
341
342 static inline GLint
343 _element_array_buffer_binding(void) {
344     return _glGetInteger(GL_ELEMENT_ARRAY_BUFFER_BINDING);
345 }
346
347 static inline GLuint
348 _glDrawArrays_count(GLint first, GLsizei count)
349 {
350     if (!count) {
351         return 0;
352     }
353     return first + count;
354 }
355
356 #define _glDrawArraysEXT_count _glDrawArrays_count
357
358 /* Forward declaration for definition in gltrace.py */
359 void
360 _shadow_glGetBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size,
361                               GLvoid *data);
362
363 static inline GLuint
364 _glDrawElementsBaseVertex_count(GLsizei count, GLenum type, const GLvoid *indices, GLint basevertex)
365 {
366     GLvoid *temp = 0;
367
368     if (!count) {
369         return 0;
370     }
371
372     GLint element_array_buffer = _element_array_buffer_binding();
373     if (element_array_buffer) {
374         // Read indices from index buffer object
375         GLintptr offset = (GLintptr)indices;
376         GLsizeiptr size = count*_gl_type_size(type);
377         GLvoid *temp = malloc(size);
378         if (!temp) {
379             return 0;
380         }
381         memset(temp, 0, size);
382         _shadow_glGetBufferSubData(GL_ELEMENT_ARRAY_BUFFER, offset, size, temp);
383         indices = temp;
384     } else {
385         if (!indices) {
386             return 0;
387         }
388     }
389
390     GLuint maxindex = 0;
391     GLsizei i;
392     if (type == GL_UNSIGNED_BYTE) {
393         const GLubyte *p = (const GLubyte *)indices;
394         for (i = 0; i < count; ++i) {
395             if (p[i] > maxindex) {
396                 maxindex = p[i];
397             }
398         }
399     } else if (type == GL_UNSIGNED_SHORT) {
400         const GLushort *p = (const GLushort *)indices;
401         for (i = 0; i < count; ++i) {
402             if (p[i] > maxindex) {
403                 maxindex = p[i];
404             }
405         }
406     } else if (type == GL_UNSIGNED_INT) {
407         const GLuint *p = (const GLuint *)indices;
408         for (i = 0; i < count; ++i) {
409             if (p[i] > maxindex) {
410                 maxindex = p[i];
411             }
412         }
413     } else {
414         os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, type);
415     }
416
417     if (element_array_buffer) {
418         free(temp);
419     }
420
421     maxindex += basevertex;
422
423     return maxindex + 1;
424 }
425
426 #define _glDrawRangeElementsBaseVertex_count(start, end, count, type, indices, basevertex) _glDrawElementsBaseVertex_count(count, type, indices, basevertex)
427
428 #define _glDrawElements_count(count, type, indices) _glDrawElementsBaseVertex_count(count, type, indices, 0);
429 #define _glDrawRangeElements_count(start, end, count, type, indices) _glDrawElements_count(count, type, indices)
430 #define _glDrawRangeElementsEXT_count _glDrawRangeElements_count
431
432 /* FIXME take in consideration instancing */
433 #define _glDrawArraysInstanced_count(first, count, primcount) _glDrawArrays_count(first, count)
434 #define _glDrawElementsInstanced_count(count, type, indices, primcount) _glDrawElements_count(count, type, indices)
435 #define _glDrawElementsInstancedBaseVertex_count(count, type, indices, primcount, basevertex) _glDrawElementsBaseVertex_count(count, type, indices, basevertex)
436 #define _glDrawRangeElementsInstanced_count(start, end, count, type, indices, primcount) _glDrawRangeElements_count(start, end, count, type, indices)
437 #define _glDrawRangeElementsInstancedBaseVertex_count(start, end, count, type, indices, primcount, basevertex) _glDrawRangeElementsBaseVertex_count(start, end, count, type, indices, basevertex)
438
439 #define _glDrawArraysInstancedBaseInstance_count(first, count, primcount, baseinstance) _glDrawArrays_count(first, count)
440 #define _glDrawElementsInstancedBaseInstance_count(count, type, indices, primcount, baseinstance) _glDrawElements_count(count, type, indices)
441 #define _glDrawElementsInstancedBaseVertexBaseInstance_count(count, type, indices, primcount, basevertex, baseinstance) _glDrawElementsBaseVertex_count(count, type, indices, basevertex)
442
443 #define _glDrawArraysInstancedARB_count _glDrawArraysInstanced_count
444 #define _glDrawElementsInstancedARB_count _glDrawElementsInstanced_count
445 #define _glDrawArraysInstancedEXT_count _glDrawArraysInstanced_count
446 #define _glDrawElementsInstancedEXT_count _glDrawElementsInstanced_count
447
448 typedef struct {
449     GLuint count;
450     GLuint primCount;
451     GLuint first;
452     GLuint baseInstance;
453 } DrawArraysIndirectCommand;
454
455 static inline GLuint
456 _glMultiDrawArraysIndirect_count(const GLvoid *indirect, GLsizei drawcount, GLsizei stride) {
457     const DrawArraysIndirectCommand *cmd;
458     GLvoid *temp = 0;
459
460     if (drawcount <= 0) {
461         return 0;
462     }
463
464     if (stride == 0) {
465         stride = sizeof *cmd;
466     }
467
468     GLint draw_indirect_buffer = _glGetInteger(GL_DRAW_INDIRECT_BUFFER_BINDING);
469     if (draw_indirect_buffer) {
470         // Read commands from indirect buffer object
471         GLintptr offset = (GLintptr)indirect;
472         GLsizeiptr size = sizeof *cmd + (drawcount - 1) * stride;
473         GLvoid *temp = malloc(size);
474         if (!temp) {
475             return 0;
476         }
477         memset(temp, 0, size);
478         _glGetBufferSubData(GL_DRAW_INDIRECT_BUFFER, offset, size, temp);
479         indirect = temp;
480     } else {
481         if (!indirect) {
482             return 0;
483         }
484     }
485
486     GLuint count = 0;
487     for (GLsizei i = 0; i < drawcount; ++i) {
488         cmd = (const DrawArraysIndirectCommand *)((const uint8_t *)indirect + i * stride);
489
490         GLuint count_i = _glDrawArraysInstancedBaseInstance_count(
491             cmd->first,
492             cmd->count,
493             cmd->primCount,
494             cmd->baseInstance
495         );
496
497         count = std::max(count, count_i);
498     }
499
500     if (draw_indirect_buffer) {
501         free(temp);
502     }
503
504     return count;
505 }
506
507 static inline GLuint
508 _glDrawArraysIndirect_count(const GLvoid *indirect) {
509     return _glMultiDrawArraysIndirect_count(indirect, 1, 0);
510 }
511
512 typedef struct {
513     GLuint count;
514     GLuint primCount;
515     GLuint firstIndex;
516     GLuint baseVertex;
517     GLuint baseInstance;
518 } DrawElementsIndirectCommand;
519
520 static inline GLuint
521 _glMultiDrawElementsIndirect_count(GLenum type, const GLvoid *indirect, GLsizei drawcount, GLsizei stride) {
522     const DrawElementsIndirectCommand *cmd;
523     GLvoid *temp = 0;
524
525     if (drawcount <= 0) {
526         return 0;
527     }
528
529     if (stride == 0) {
530         stride = sizeof *cmd;
531     }
532
533     GLint draw_indirect_buffer = _glGetInteger(GL_DRAW_INDIRECT_BUFFER_BINDING);
534     if (draw_indirect_buffer) {
535         // Read commands from indirect buffer object
536         GLintptr offset = (GLintptr)indirect;
537         GLsizeiptr size = sizeof *cmd + (drawcount - 1) * stride;
538         GLvoid *temp = malloc(size);
539         if (!temp) {
540             return 0;
541         }
542         memset(temp, 0, size);
543         _glGetBufferSubData(GL_DRAW_INDIRECT_BUFFER, offset, size, temp);
544         indirect = temp;
545     } else {
546         if (!indirect) {
547             return 0;
548         }
549     }
550
551     cmd = (const DrawElementsIndirectCommand *)indirect;
552
553     GLuint count = 0;
554     for (GLsizei i = 0; i < drawcount; ++i) {
555         cmd = (const DrawElementsIndirectCommand *)((const uint8_t *)indirect + i * stride);
556
557         GLuint count_i = _glDrawElementsInstancedBaseVertexBaseInstance_count(
558             cmd->count,
559             type,
560             (GLvoid *)(uintptr_t)(cmd->firstIndex * _gl_type_size(type)),
561             cmd->primCount,
562             cmd->baseVertex,
563             cmd->baseInstance
564         );
565
566         count = std::max(count, count_i);
567     }
568
569     if (draw_indirect_buffer) {
570         free(temp);
571     }
572
573     return count;
574 }
575
576 static inline GLuint
577 _glDrawElementsIndirect_count(GLenum type, const GLvoid *indirect) {
578     return _glMultiDrawElementsIndirect_count(type, indirect, 1, 0);
579 }
580
581 #define _glMultiDrawArraysIndirectAMD_count _glMultiDrawArraysIndirect_count
582 #define _glMultiDrawElementsIndirectAMD_count _glMultiDrawElementsIndirect_count
583
584 static inline GLuint
585 _glMultiDrawArrays_count(const GLint *first, const GLsizei *count, GLsizei drawcount) {
586     GLuint _count = 0;
587     for (GLsizei draw = 0; draw < drawcount; ++draw) {
588         GLuint _count_draw = _glDrawArrays_count(first[draw], count[draw]);
589         _count = std::max(_count, _count_draw);
590     }
591     return _count;
592 }
593
594 static inline GLuint
595 _glMultiDrawElements_count(const GLsizei *count, GLenum type, const GLvoid* const *indices, GLsizei drawcount) {
596     GLuint _count = 0;
597     for (GLsizei draw = 0; draw < drawcount; ++draw) {
598         GLuint _count_draw = _glDrawElements_count(count[draw], type, indices[draw]);
599         _count = std::max(_count, _count_draw);
600     }
601     return _count;
602 }
603
604 static inline GLuint
605 _glMultiDrawElementsBaseVertex_count(const GLsizei *count, GLenum type, const GLvoid* const *indices, GLsizei drawcount, const GLint * basevertex) {
606     GLuint _count = 0;
607     for (GLsizei draw = 0; draw < drawcount; ++draw) {
608         GLuint _count_draw = _glDrawElementsBaseVertex_count(count[draw], type, indices[draw], basevertex[draw]);
609         _count = std::max(_count, _count_draw);
610     }
611     return _count;
612 }
613
614 #define _glMultiDrawArraysEXT_count _glMultiDrawArrays_count
615 #define _glMultiDrawElementsEXT_count _glMultiDrawElements_count
616
617 #define _glMultiModeDrawArraysIBM_count(first, count, drawcount, modestride) _glMultiDrawArrays_count(first, count, drawcount)
618 #define _glMultiModeDrawElementsIBM_count(count, type, indices, drawcount, modestride) _glMultiDrawElements_count(count, type, (const GLvoid **)indices, drawcount)
619
620
621 static inline size_t
622 _glCallLists_size(GLsizei n, GLenum type)
623 {
624     return n*_gl_type_size(type);
625 }
626
627 static inline size_t
628 _glMap1d_size(GLenum target, GLint stride, GLint order)
629 {
630     if (order < 1) {
631         return 0;
632     }
633
634     GLint channels;
635     switch (target) {
636     case GL_MAP1_INDEX:
637     case GL_MAP1_TEXTURE_COORD_1:
638         channels = 1;
639         break;
640     case GL_MAP1_TEXTURE_COORD_2:
641         channels = 2;
642         break;
643     case GL_MAP1_NORMAL:
644     case GL_MAP1_TEXTURE_COORD_3:
645     case GL_MAP1_VERTEX_3:
646         channels = 3;
647         break;
648     case GL_MAP1_COLOR_4:
649     case GL_MAP1_TEXTURE_COORD_4:
650     case GL_MAP1_VERTEX_4:
651         channels = 4;
652         break;
653     default:
654         os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, target);
655         return 0;
656     }
657
658     if (stride < channels) {
659         return 0;
660     }
661
662     return channels + stride * (order - 1);
663 }
664
665 #define _glMap1f_size _glMap1d_size
666
667 static inline size_t
668 _glMap2d_size(GLenum target, GLint ustride, GLint uorder, GLint vstride, GLint vorder)
669 {
670     if (uorder < 1 || vorder < 1) {
671         return 0;
672     }
673
674     GLint channels;
675     switch (target) {
676     case GL_MAP2_INDEX:
677     case GL_MAP2_TEXTURE_COORD_1:
678         channels = 1;
679         break;
680     case GL_MAP2_TEXTURE_COORD_2:
681         channels = 2;
682         break;
683     case GL_MAP2_NORMAL:
684     case GL_MAP2_TEXTURE_COORD_3:
685     case GL_MAP2_VERTEX_3:
686         channels = 3;
687         break;
688     case GL_MAP2_COLOR_4:
689     case GL_MAP2_TEXTURE_COORD_4:
690     case GL_MAP2_VERTEX_4:
691         channels = 4;
692         break;
693     default:
694         os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, target);
695         return 0;
696     }
697
698     if (ustride < channels || vstride < channels) {
699         return 0;
700     }
701
702     return channels + 
703            ustride * (uorder - 1) +
704            vstride * (vorder - 1);
705 }
706
707 #define _glMap2f_size _glMap2d_size
708
709 static inline unsigned
710 _gl_format_channels(GLenum format) {
711     switch (format) {
712     case GL_COLOR_INDEX:
713     case GL_RED:
714     case GL_GREEN:
715     case GL_BLUE:
716     case GL_ALPHA:
717     case GL_INTENSITY:
718     case GL_LUMINANCE:
719     case GL_DEPTH_COMPONENT:
720     case GL_STENCIL_INDEX:
721         return 1;
722     case GL_DEPTH_STENCIL:
723     case GL_LUMINANCE_ALPHA:
724     case GL_RG:
725     case GL_HILO_NV:
726     case GL_DSDT_NV:
727         return 2;
728     case GL_RGB:
729     case GL_BGR:
730     case GL_DSDT_MAG_NV:
731         return 3;
732     case GL_RGBA:
733     case GL_BGRA:
734     case GL_ABGR_EXT:
735     case GL_CMYK_EXT:
736     case GL_DSDT_MAG_VIB_NV:
737         return 4;
738     case GL_CMYKA_EXT:
739         return 5;
740     default:
741         os::log("apitrace: warning: %s: unexpected format GLenum 0x%04X\n", __FUNCTION__, format);
742         return 0;
743     }
744 }
745
746 template<class X>
747 static inline bool
748 _is_pot(X x) {
749     return (x & (x - 1)) == 0;
750 }
751
752 template<class X, class Y>
753 static inline X
754 _align(X x, Y y) {
755     return (x + (y - 1)) & ~(y - 1);
756 }
757
758 static inline void
759 _gl_format_size(GLenum format, GLenum type,
760                 unsigned & bits_per_element, unsigned & bits_per_pixel)
761 {
762     unsigned num_channels = _gl_format_channels(format);
763
764     switch (type) {
765     case GL_BITMAP:
766         bits_per_pixel = bits_per_element = 1;
767         break;
768     case GL_BYTE:
769     case GL_UNSIGNED_BYTE:
770         bits_per_element = 8;
771         bits_per_pixel = bits_per_element * num_channels;
772         break;
773     case GL_SHORT:
774     case GL_UNSIGNED_SHORT:
775     case GL_HALF_FLOAT:
776         bits_per_element = 16;
777         bits_per_pixel = bits_per_element * num_channels;
778         break;
779     case GL_INT:
780     case GL_UNSIGNED_INT:
781     case GL_FLOAT:
782         bits_per_element = 32;
783         bits_per_pixel = bits_per_element * num_channels;
784         break;
785     case GL_UNSIGNED_BYTE_3_3_2:
786     case GL_UNSIGNED_BYTE_2_3_3_REV:
787         bits_per_pixel = bits_per_element = 8;
788         break;
789     case GL_UNSIGNED_SHORT_4_4_4_4:
790     case GL_UNSIGNED_SHORT_4_4_4_4_REV:
791     case GL_UNSIGNED_SHORT_5_5_5_1:
792     case GL_UNSIGNED_SHORT_1_5_5_5_REV:
793     case GL_UNSIGNED_SHORT_5_6_5:
794     case GL_UNSIGNED_SHORT_5_6_5_REV:
795     case GL_UNSIGNED_SHORT_8_8_MESA:
796     case GL_UNSIGNED_SHORT_8_8_REV_MESA:
797         bits_per_pixel = bits_per_element = 16;
798         break;
799     case GL_UNSIGNED_INT_8_8_8_8:
800     case GL_UNSIGNED_INT_8_8_8_8_REV:
801     case GL_UNSIGNED_INT_10_10_10_2:
802     case GL_UNSIGNED_INT_2_10_10_10_REV:
803     case GL_UNSIGNED_INT_24_8:
804     case GL_UNSIGNED_INT_10F_11F_11F_REV:
805     case GL_UNSIGNED_INT_5_9_9_9_REV:
806     case GL_UNSIGNED_INT_S8_S8_8_8_NV:
807     case GL_UNSIGNED_INT_8_8_S8_S8_REV_NV:
808         bits_per_pixel = bits_per_element = 32;
809         break;
810     case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
811         bits_per_pixel = bits_per_element = 64;
812         break;
813     default:
814         os::log("apitrace: warning: %s: unexpected type GLenum 0x%04X\n", __FUNCTION__, type);
815         bits_per_pixel = bits_per_element = 0;
816         break;
817     }
818 }
819
820 static inline size_t
821 _glClearBufferData_size(GLenum format, GLenum type) {
822     unsigned bits_per_element;
823     unsigned bits_per_pixel;
824     _gl_format_size(format, type, bits_per_element, bits_per_pixel);
825     return (bits_per_pixel + 7)/8;
826 }
827
828 static inline size_t
829 _gl_image_size(GLenum format, GLenum type, GLsizei width, GLsizei height, GLsizei depth, GLboolean has_unpack_subimage) {
830
831     unsigned bits_per_element;
832     unsigned bits_per_pixel;
833     _gl_format_size(format, type, bits_per_element, bits_per_pixel);
834
835     GLint alignment = 4;
836     GLint row_length = 0;
837     GLint image_height = 0;
838     GLint skip_rows = 0;
839     GLint skip_pixels = 0;
840     GLint skip_images = 0;
841
842     _glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
843     if (has_unpack_subimage) {
844         _glGetIntegerv(GL_UNPACK_ROW_LENGTH,   &row_length);
845         _glGetIntegerv(GL_UNPACK_IMAGE_HEIGHT, &image_height);
846         _glGetIntegerv(GL_UNPACK_SKIP_ROWS,    &skip_rows);
847         _glGetIntegerv(GL_UNPACK_SKIP_PIXELS,  &skip_pixels);
848         _glGetIntegerv(GL_UNPACK_SKIP_IMAGES,  &skip_images);
849     }
850
851     if (row_length <= 0) {
852         row_length = width;
853     }
854
855     size_t row_stride = (row_length*bits_per_pixel + 7)/8;
856
857     if ((bits_per_element == 1*8 ||
858          bits_per_element == 2*8 ||
859          bits_per_element == 4*8 ||
860          bits_per_element == 8*8) &&
861         (GLint)bits_per_element < alignment*8) {
862         row_stride = _align(row_stride, alignment);
863     }
864
865     if (image_height <= 0) {
866         image_height = height;
867     }
868
869     /* XXX: GL_UNPACK_IMAGE_HEIGHT and GL_UNPACK_SKIP_IMAGES should probably
870      * not be considered for pixel rectangles. */
871
872     size_t image_stride = image_height*row_stride;
873
874     size_t size = depth*image_stride;
875
876     size += (skip_pixels*bits_per_pixel + 7)/8;
877     size += skip_rows*row_stride;
878     size += skip_images*image_stride;
879
880     return size;
881 }
882
883 // note that can_unpack_subimage() is generated by gltrace.py
884 #define _glTexImage3D_size(format, type, width, height, depth) _gl_image_size(format, type, width, height, depth, can_unpack_subimage())
885 #define _glTexImage2D_size(format, type, width, height)        _gl_image_size(format, type, width, height, 1, can_unpack_subimage())
886 #define _glTexImage1D_size(format, type, width)                _gl_image_size(format, type, width, 1, 1, can_unpack_subimage())
887
888 #define _glTexSubImage3D_size(format, type, width, height, depth) _glTexImage3D_size(format, type, width, height, depth)
889 #define _glTexSubImage2D_size(format, type, width, height)        _glTexImage2D_size(format, type, width, height)
890 #define _glTexSubImage1D_size(format, type, width)                _glTexImage1D_size(format, type, width)
891
892 #define _glTexImage3DEXT_size _glTexImage3D_size
893 #define _glTexImage2DEXT_size _glTexImage2D_size
894 #define _glTexImage1DEXT_size _glTexImage1D_size
895 #define _glTexSubImage3DEXT_size _glTexSubImage3D_size
896 #define _glTexSubImage2DEXT_size _glTexSubImage2D_size
897 #define _glTexSubImage1DEXT_size _glTexSubImage1D_size
898
899 #define _glTextureImage3DEXT_size _glTexImage3D_size
900 #define _glTextureImage2DEXT_size _glTexImage2D_size
901 #define _glTextureImage1DEXT_size _glTexImage1D_size
902 #define _glTextureSubImage3DEXT_size _glTexSubImage3D_size
903 #define _glTextureSubImage2DEXT_size _glTexSubImage2D_size
904 #define _glTextureSubImage1DEXT_size _glTexSubImage1D_size
905
906 #define _glMultiTexImage3DEXT_size _glTexImage3D_size
907 #define _glMultiTexImage2DEXT_size _glTexImage2D_size
908 #define _glMultiTexImage1DEXT_size _glTexImage1D_size
909 #define _glMultiTexSubImage3DEXT_size _glTexSubImage3D_size
910 #define _glMultiTexSubImage2DEXT_size _glTexSubImage2D_size
911 #define _glMultiTexSubImage1DEXT_size _glTexSubImage1D_size
912
913 #define _glDrawPixels_size(format, type, width, height) _glTexImage2D_size(format, type, width, height)
914 #define _glConvolutionFilter1D_size(format, type, width) _glTexImage1D_size(format, type, width)
915 #define _glConvolutionFilter2D_size(format, type, width, height) _glTexImage2D_size(format, type, width, height)
916 #define _glColorTable_size(format, type, width) _glTexImage1D_size(format, type, width)
917 #define _glColorSubTable_size(format, type, count) _glColorTable_size(format, type, count)
918
919 #define _glBitmap_size(width, height) _glTexImage2D_size(GL_COLOR_INDEX, GL_BITMAP, width, height)
920 #define _glPolygonStipple_size() _glBitmap_size(32, 32)
921
922 static inline size_t
923 _glClearBuffer_size(GLenum buffer)
924 {
925     switch (buffer) {
926     case GL_COLOR:
927     case GL_FRONT:
928     case GL_BACK:
929     case GL_LEFT:
930     case GL_RIGHT:
931     case GL_FRONT_AND_BACK:
932         return 4;
933     case GL_DEPTH:
934     case GL_STENCIL:
935         return 1;
936     default:
937         os::log("apitrace: warning: %s: unexpected buffer GLenum 0x%04X\n", __FUNCTION__, buffer);
938         return 0;
939     }
940 }
941
942
943 /**
944  * Helper function for determining the string lengths for glShaderSource and
945  * glShaderSourceARB, which is a tad too complex to inline in the specs.
946  */
947 template<class Char>
948 static inline size_t
949 _glShaderSource_length(const Char * const * string, const GLint *length, GLsizei index)
950 {
951     if (length != NULL && length[index] >= 0) {
952         return (size_t)length[index];
953     } else {
954         return strlen(string[index]);
955     }
956 }
957
958 /**
959  * Helper function for determining the string lengths for glGetDebugMessageLog*.
960  */
961 template<class Char>
962 static inline size_t
963 _glGetDebugMessageLog_length(const Char * string, const GLsizei *lengths, GLuint count)
964 {
965     size_t size = 0;
966     GLuint index;
967     if (lengths) {
968         for (index = 0; index < count; ++index) {
969             size += lengths[index];
970         }
971     } else {
972         for (index = 0; index < count; ++index) {
973             size += strlen(&string[size]) + 1;
974         }
975     }
976     if (size) {
977         // Remove the last null terminator
978         --size;
979     }
980     return size;
981 }
982
983 /* 
984  * attribute list, terminated by the given terminator.
985  */
986 template<class T>
987 static inline size_t
988 _AttribList_size(const T *pAttribList, const T terminator = static_cast<T>(0))
989 {
990     size_t size = 0;
991
992     if (pAttribList) {
993         do {
994             ++size;
995         } while (*pAttribList++ != terminator);
996     }
997
998     return size;
999 }
1000
1001
1002 /*
1003  * (key, value) attribute list, terminated by the given terminator.
1004  */
1005 template<class T>
1006 static inline size_t
1007 _AttribPairList_size(const T *pAttribList, const T terminator = static_cast<T>(0))
1008 {
1009     size_t size = 0;
1010
1011     if (pAttribList) {
1012         while (pAttribList[size] != terminator) {
1013             size += 2;
1014         }
1015         // terminator also counts
1016         ++size;
1017     }
1018
1019     return size;
1020 }
1021
1022
1023 #endif /* _GL_SIZE_HPP_ */