1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
4 * Copyright 2010 VMware, Inc.
5 * Copyright 2004 IBM Corporation
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:
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
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
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
28 **************************************************************************/
31 * Auxiliary functions to compute the size of array/blob arguments, depending.
41 #include "glimports.hpp"
45 __gl_type_size(GLenum type)
50 case GL_UNSIGNED_BYTE:
53 case GL_UNSIGNED_SHORT:
68 os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, type);
74 __gl_uniform_size(GLenum type, GLenum &elemType, GLint &numElems) {
101 elemType = GL_DOUBLE;
105 elemType = GL_DOUBLE;
124 case GL_UNSIGNED_INT:
125 elemType = GL_UNSIGNED_INT;
128 case GL_UNSIGNED_INT_VEC2:
129 elemType = GL_UNSIGNED_INT;
132 case GL_UNSIGNED_INT_VEC3:
133 elemType = GL_UNSIGNED_INT;
136 case GL_UNSIGNED_INT_VEC4:
137 elemType = GL_UNSIGNED_INT;
168 case GL_FLOAT_MAT2x3:
172 case GL_FLOAT_MAT2x4:
176 case GL_FLOAT_MAT3x2:
180 case GL_FLOAT_MAT3x4:
184 case GL_FLOAT_MAT4x2:
188 case GL_FLOAT_MAT4x3:
193 elemType = GL_DOUBLE;
197 elemType = GL_DOUBLE;
201 elemType = GL_DOUBLE;
204 case GL_DOUBLE_MAT2x3:
205 elemType = GL_DOUBLE;
208 case GL_DOUBLE_MAT2x4:
209 elemType = GL_DOUBLE;
212 case GL_DOUBLE_MAT3x2:
213 elemType = GL_DOUBLE;
216 case GL_DOUBLE_MAT3x4:
217 elemType = GL_DOUBLE;
220 case GL_DOUBLE_MAT4x2:
221 elemType = GL_DOUBLE;
224 case GL_DOUBLE_MAT4x3:
225 elemType = GL_DOUBLE;
231 case GL_SAMPLER_CUBE:
232 case GL_SAMPLER_1D_SHADOW:
233 case GL_SAMPLER_2D_SHADOW:
234 case GL_SAMPLER_1D_ARRAY:
235 case GL_SAMPLER_2D_ARRAY:
236 case GL_SAMPLER_CUBE_MAP_ARRAY:
237 case GL_SAMPLER_1D_ARRAY_SHADOW:
238 case GL_SAMPLER_2D_ARRAY_SHADOW:
239 case GL_SAMPLER_2D_MULTISAMPLE:
240 case GL_SAMPLER_2D_MULTISAMPLE_ARRAY:
241 case GL_SAMPLER_CUBE_SHADOW:
242 case GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW:
243 case GL_SAMPLER_BUFFER:
244 case GL_SAMPLER_2D_RECT:
245 case GL_SAMPLER_2D_RECT_SHADOW:
246 case GL_INT_SAMPLER_1D:
247 case GL_INT_SAMPLER_2D:
248 case GL_INT_SAMPLER_3D:
249 case GL_INT_SAMPLER_CUBE:
250 case GL_INT_SAMPLER_1D_ARRAY:
251 case GL_INT_SAMPLER_2D_ARRAY:
252 case GL_INT_SAMPLER_CUBE_MAP_ARRAY:
253 case GL_INT_SAMPLER_2D_MULTISAMPLE:
254 case GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
255 case GL_INT_SAMPLER_BUFFER:
256 case GL_INT_SAMPLER_2D_RECT:
257 case GL_UNSIGNED_INT_SAMPLER_1D:
258 case GL_UNSIGNED_INT_SAMPLER_2D:
259 case GL_UNSIGNED_INT_SAMPLER_3D:
260 case GL_UNSIGNED_INT_SAMPLER_CUBE:
261 case GL_UNSIGNED_INT_SAMPLER_1D_ARRAY:
262 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
263 case GL_UNSIGNED_INT_SAMPLER_CUBE_MAP_ARRAY:
264 case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE:
265 case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
266 case GL_UNSIGNED_INT_SAMPLER_BUFFER:
267 case GL_UNSIGNED_INT_SAMPLER_2D_RECT:
272 os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, type);
280 __glArrayPointer_size(GLint size, GLenum type, GLsizei stride, GLsizei maxIndex)
282 size_t elementSize = size*__gl_type_size(type);
284 stride = (GLsizei)elementSize;
286 return stride*maxIndex + elementSize;
289 #define __glVertexPointer_size(size, type, stride, maxIndex) __glArrayPointer_size(size, type, stride, maxIndex)
290 #define __glNormalPointer_size(type, stride, maxIndex) __glArrayPointer_size(3, type, stride, maxIndex)
291 #define __glColorPointer_size(size, type, stride, maxIndex) __glArrayPointer_size(size, type, stride, maxIndex)
292 #define __glIndexPointer_size(type, stride, maxIndex) __glArrayPointer_size(1, type, stride, maxIndex)
293 #define __glTexCoordPointer_size(size, type, stride, maxIndex) __glArrayPointer_size(size, type, stride, maxIndex)
294 #define __glEdgeFlagPointer_size(stride, maxIndex) __glArrayPointer_size(1, GL_BOOL, stride, maxIndex)
295 #define __glFogCoordPointer_size(type, stride, maxIndex) __glArrayPointer_size(1, type, stride, maxIndex)
296 #define __glSecondaryColorPointer_size(size, type, stride, maxIndex) __glArrayPointer_size(size, type, stride, maxIndex)
297 #define __glVertexAttribPointer_size(size, type, normalized, stride, maxIndex) __glArrayPointer_size(size, type, stride, maxIndex)
298 #define __glVertexAttribPointerARB_size(size, type, normalized, stride, maxIndex) __glArrayPointer_size(size, type, stride, maxIndex)
299 #define __glVertexAttribPointerNV_size(size, type, stride, maxIndex) __glArrayPointer_size(size, type, stride, maxIndex)
302 __glDrawArrays_maxindex(GLint first, GLsizei count)
307 return first + count - 1;
310 #define __glDrawArraysEXT_maxindex __glDrawArrays_maxindex
313 __glDrawElementsBaseVertex_maxindex(GLsizei count, GLenum type, const GLvoid *indices, GLint basevertex)
316 GLint __element_array_buffer = 0;
322 __glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &__element_array_buffer);
323 if (__element_array_buffer) {
324 // Read indices from index buffer object
325 GLintptr offset = (GLintptr)indices;
326 GLsizeiptr size = count*__gl_type_size(type);
327 GLvoid *temp = malloc(size);
331 memset(temp, 0, size);
332 __glGetBufferSubData(GL_ELEMENT_ARRAY_BUFFER, offset, size, temp);
342 if (type == GL_UNSIGNED_BYTE) {
343 const GLubyte *p = (const GLubyte *)indices;
344 for (i = 0; i < count; ++i) {
345 if (p[i] > maxindex) {
349 } else if (type == GL_UNSIGNED_SHORT) {
350 const GLushort *p = (const GLushort *)indices;
351 for (i = 0; i < count; ++i) {
352 if (p[i] > maxindex) {
356 } else if (type == GL_UNSIGNED_INT) {
357 const GLuint *p = (const GLuint *)indices;
358 for (i = 0; i < count; ++i) {
359 if (p[i] > maxindex) {
364 os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, type);
367 if (__element_array_buffer) {
371 maxindex += basevertex;
376 #define __glDrawRangeElementsBaseVertex_maxindex(start, end, count, type, indices, basevertex) __glDrawElementsBaseVertex_maxindex(count, type, indices, basevertex)
378 #define __glDrawElements_maxindex(count, type, indices) __glDrawElementsBaseVertex_maxindex(count, type, indices, 0);
379 #define __glDrawRangeElements_maxindex(start, end, count, type, indices) __glDrawElements_maxindex(count, type, indices)
380 #define __glDrawRangeElementsEXT_maxindex __glDrawRangeElements_maxindex
382 /* FIXME take in consideration instancing */
383 #define __glDrawArraysInstanced_maxindex(first, count, primcount) __glDrawArrays_maxindex(first, count)
384 #define __glDrawElementsInstanced_maxindex(count, type, indices, primcount) __glDrawElements_maxindex(count, type, indices)
385 #define __glDrawElementsInstancedBaseVertex_maxindex(count, type, indices, primcount, basevertex) __glDrawElementsBaseVertex_maxindex(count, type, indices, basevertex)
386 #define __glDrawRangeElementsInstanced_maxindex(start, end, count, type, indices, primcount) __glDrawRangeElements_maxindex(start, end, count, type, indices)
387 #define __glDrawRangeElementsInstancedBaseVertex_maxindex(start, end, count, type, indices, primcount, basevertex) __glDrawRangeElementsBaseVertex_maxindex(start, end, count, type, indices, basevertex)
389 #define __glDrawArraysInstancedBaseInstance_maxindex(first, count, primcount, baseinstance) __glDrawArrays_maxindex(first, count)
390 #define __glDrawElementsInstancedBaseInstance_maxindex(count, type, indices, primcount, baseinstance) __glDrawElements_maxindex(count, type, indices)
391 #define __glDrawElementsInstancedBaseVertexBaseInstance_maxindex(count, type, indices, primcount, basevertex, baseinstance) __glDrawElementsBaseVertex_maxindex(count, type, indices, basevertex)
393 #define __glDrawArraysInstancedARB_maxindex __glDrawArraysInstanced_maxindex
394 #define __glDrawElementsInstancedARB_maxindex __glDrawElementsInstanced_maxindex
395 #define __glDrawArraysInstancedEXT_maxindex __glDrawArraysInstanced_maxindex
396 #define __glDrawElementsInstancedEXT_maxindex __glDrawElementsInstanced_maxindex
399 __glDrawArraysIndirect_maxindex(const GLvoid *indirect) {
400 os::log("apitrace: warning: %s: unsupported\n", __FUNCTION__);
405 __glDrawElementsIndirect_maxindex(GLenum type, const GLvoid *indirect) {
406 os::log("apitrace: warning: %s: unsupported\n", __FUNCTION__);
411 __glMultiDrawArrays_maxindex(const GLint *first, const GLsizei *count, GLsizei primcount) {
413 for (GLsizei prim = 0; prim < primcount; ++prim) {
414 GLuint maxindex_prim = __glDrawArrays_maxindex(first[prim], count[prim]);
415 maxindex = std::max(maxindex, maxindex_prim);
421 __glMultiDrawElements_maxindex(const GLsizei *count, GLenum type, const GLvoid* *indices, GLsizei primcount) {
423 for (GLsizei prim = 0; prim < primcount; ++prim) {
424 GLuint maxindex_prim = __glDrawElements_maxindex(count[prim], type, indices[prim]);
425 maxindex = std::max(maxindex, maxindex_prim);
431 __glMultiDrawElementsBaseVertex_maxindex(const GLsizei *count, GLenum type, const GLvoid* *indices, GLsizei primcount, const GLint * basevertex) {
433 for (GLsizei prim = 0; prim < primcount; ++prim) {
434 GLuint maxindex_prim = __glDrawElementsBaseVertex_maxindex(count[prim], type, indices[prim], basevertex[prim]);
435 maxindex = std::max(maxindex, maxindex_prim);
440 #define __glMultiDrawArraysEXT_maxindex __glMultiDrawArrays_maxindex
441 #define __glMultiDrawElementsEXT_maxindex __glMultiDrawElements_maxindex
443 #define __glMultiModeDrawArraysIBM_maxindex(first, count, primcount, modestride) __glMultiDrawArrays_maxindex(first, count, primcount)
444 #define __glMultiModeDrawElementsIBM_maxindex(count, type, indices, primcount, modestride) __glMultiDrawElements_maxindex(count, type, (const GLvoid **)indices, primcount)
448 __glCallLists_size(GLsizei n, GLenum type)
450 return n*__gl_type_size(type);
454 __glMap1d_size(GLenum target, GLint stride, GLint order)
463 case GL_MAP1_TEXTURE_COORD_1:
466 case GL_MAP1_TEXTURE_COORD_2:
470 case GL_MAP1_TEXTURE_COORD_3:
471 case GL_MAP1_VERTEX_3:
474 case GL_MAP1_COLOR_4:
475 case GL_MAP1_TEXTURE_COORD_4:
476 case GL_MAP1_VERTEX_4:
480 os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, target);
484 if (stride < channels) {
488 return channels + stride * (order - 1);
491 #define __glMap1f_size __glMap1d_size
494 __glMap2d_size(GLenum target, GLint ustride, GLint uorder, GLint vstride, GLint vorder)
496 if (uorder < 1 || vorder < 1) {
503 case GL_MAP2_TEXTURE_COORD_1:
506 case GL_MAP2_TEXTURE_COORD_2:
510 case GL_MAP2_TEXTURE_COORD_3:
511 case GL_MAP2_VERTEX_3:
514 case GL_MAP2_COLOR_4:
515 case GL_MAP2_TEXTURE_COORD_4:
516 case GL_MAP2_VERTEX_4:
520 os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, target);
524 if (ustride < channels || vstride < channels) {
529 ustride * (uorder - 1) +
530 vstride * (vorder - 1);
533 #define __glMap2f_size __glMap2d_size
535 static inline unsigned
536 __gl_format_channels(GLenum format) {
545 case GL_DEPTH_COMPONENT:
546 case GL_STENCIL_INDEX:
548 case GL_DEPTH_STENCIL:
549 case GL_LUMINANCE_ALPHA:
562 case GL_DSDT_MAG_VIB_NV:
567 os::log("apitrace: warning: %s: unexpected format GLenum 0x%04X\n", __FUNCTION__, format);
575 return (x & (x - 1)) == 0;
578 template<class X, class Y>
581 return (x + (y - 1)) & ~(y - 1);
585 __gl_image_size(GLenum format, GLenum type, GLsizei width, GLsizei height, GLsizei depth) {
586 unsigned num_channels = __gl_format_channels(format);
588 unsigned bits_per_pixel;
594 case GL_UNSIGNED_BYTE:
595 bits_per_pixel = 8 * num_channels;
598 case GL_UNSIGNED_SHORT:
600 bits_per_pixel = 16 * num_channels;
603 case GL_UNSIGNED_INT:
605 bits_per_pixel = 32 * num_channels;
607 case GL_UNSIGNED_BYTE_3_3_2:
608 case GL_UNSIGNED_BYTE_2_3_3_REV:
611 case GL_UNSIGNED_SHORT_4_4_4_4:
612 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
613 case GL_UNSIGNED_SHORT_5_5_5_1:
614 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
615 case GL_UNSIGNED_SHORT_5_6_5:
616 case GL_UNSIGNED_SHORT_5_6_5_REV:
617 case GL_UNSIGNED_SHORT_8_8_MESA:
618 case GL_UNSIGNED_SHORT_8_8_REV_MESA:
621 case GL_UNSIGNED_INT_8_8_8_8:
622 case GL_UNSIGNED_INT_8_8_8_8_REV:
623 case GL_UNSIGNED_INT_10_10_10_2:
624 case GL_UNSIGNED_INT_2_10_10_10_REV:
625 case GL_UNSIGNED_INT_24_8:
626 case GL_UNSIGNED_INT_10F_11F_11F_REV:
627 case GL_UNSIGNED_INT_5_9_9_9_REV:
628 case GL_UNSIGNED_INT_S8_S8_8_8_NV:
629 case GL_UNSIGNED_INT_8_8_S8_S8_REV_NV:
632 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
636 os::log("apitrace: warning: %s: unexpected type GLenum 0x%04X\n", __FUNCTION__, type);
642 GLint row_length = 0;
643 GLint image_height = 0;
645 GLint skip_pixels = 0;
646 GLint skip_images = 0;
648 __glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
649 __glGetIntegerv(GL_UNPACK_ROW_LENGTH, &row_length);
650 __glGetIntegerv(GL_UNPACK_IMAGE_HEIGHT, &image_height);
651 __glGetIntegerv(GL_UNPACK_SKIP_ROWS, &skip_rows);
652 __glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &skip_pixels);
653 __glGetIntegerv(GL_UNPACK_SKIP_IMAGES, &skip_images);
655 if (row_length <= 0) {
659 size_t row_stride = (row_length*bits_per_pixel + 7)/8;
661 if ((GLint)bits_per_pixel < alignment*8 &&
662 (bits_per_pixel & 7) == 0 &&
663 _is_pot(bits_per_pixel)) {
664 row_stride = _align(row_stride, alignment);
667 if (image_height <= 0) {
668 image_height = height;
671 /* XXX: GL_UNPACK_IMAGE_HEIGHT and GL_UNPACK_SKIP_IMAGES should probably
672 * not be considered for pixel rectangles. */
674 size_t image_stride = image_height*row_stride;
676 size_t size = depth*image_stride;
678 size += (skip_pixels*bits_per_pixel + 7)/8;
679 size += skip_rows*row_stride;
680 size += skip_images*image_stride;
685 #define __glTexImage3D_size(format, type, width, height, depth) __gl_image_size(format, type, width, height, depth)
686 #define __glTexImage2D_size(format, type, width, height) __gl_image_size(format, type, width, height, 1)
687 #define __glTexImage1D_size(format, type, width) __gl_image_size(format, type, width, 1, 1)
689 #define __glTexSubImage3D_size(format, type, width, height, depth) __glTexImage3D_size(format, type, width, height, depth)
690 #define __glTexSubImage2D_size(format, type, width, height) __glTexImage2D_size(format, type, width, height)
691 #define __glTexSubImage1D_size(format, type, width) __glTexImage1D_size(format, type, width)
693 #define __glTexImage3DEXT_size __glTexImage3D_size
694 #define __glTexImage2DEXT_size __glTexImage2D_size
695 #define __glTexImage1DEXT_size __glTexImage1D_size
696 #define __glTexSubImage3DEXT_size __glTexSubImage3D_size
697 #define __glTexSubImage2DEXT_size __glTexSubImage2D_size
698 #define __glTexSubImage1DEXT_size __glTexSubImage1D_size
700 #define __glTextureImage3DEXT_size __glTexImage3D_size
701 #define __glTextureImage2DEXT_size __glTexImage2D_size
702 #define __glTextureImage1DEXT_size __glTexImage1D_size
703 #define __glTextureSubImage3DEXT_size __glTexSubImage3D_size
704 #define __glTextureSubImage2DEXT_size __glTexSubImage2D_size
705 #define __glTextureSubImage1DEXT_size __glTexSubImage1D_size
707 #define __glMultiTexImage3DEXT_size __glTexImage3D_size
708 #define __glMultiTexImage2DEXT_size __glTexImage2D_size
709 #define __glMultiTexImage1DEXT_size __glTexImage1D_size
710 #define __glMultiTexSubImage3DEXT_size __glTexSubImage3D_size
711 #define __glMultiTexSubImage2DEXT_size __glTexSubImage2D_size
712 #define __glMultiTexSubImage1DEXT_size __glTexSubImage1D_size
714 #define __glDrawPixels_size(format, type, width, height) __glTexImage2D_size(format, type, width, height)
715 #define __glConvolutionFilter1D_size(format, type, width) __glTexImage1D_size(format, type, width)
716 #define __glConvolutionFilter2D_size(format, type, width, height) __glTexImage2D_size(format, type, width, height)
717 #define __glColorTable_size(format, type, width) __glTexImage1D_size(format, type, width)
718 #define __glColorSubTable_size(format, type, count) __glColorTable_size(format, type, count)
720 #define __glBitmap_size(width, height) __glTexImage2D_size(GL_COLOR_INDEX, GL_BITMAP, width, height)
721 #define __glPolygonStipple_size() __glBitmap_size(32, 32)
724 __glClearBuffer_size(GLenum buffer)
732 case GL_FRONT_AND_BACK:
738 os::log("apitrace: warning: %s: unexpected buffer GLenum 0x%04X\n", __FUNCTION__, buffer);
744 * 0 terminated integer/float attribute list.
748 __AttribList_size(const T *pAttribList)
755 } while (*pAttribList++);
763 * (key, value) attribute list, terminated by the given terminator.
767 __AttribList_size(const T *pAttribList, T terminator)
772 while (pAttribList[size] != terminator)
774 // terminator also counts
782 #endif /* _GL_SIZE_HPP_ */