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 &numCols, GLint &numRows) {
102 elemType = GL_DOUBLE;
106 elemType = GL_DOUBLE;
124 case GL_UNSIGNED_INT:
125 elemType = GL_UNSIGNED_INT;
127 case GL_UNSIGNED_INT_VEC2:
128 elemType = GL_UNSIGNED_INT;
131 case GL_UNSIGNED_INT_VEC3:
132 elemType = GL_UNSIGNED_INT;
135 case GL_UNSIGNED_INT_VEC4:
136 elemType = GL_UNSIGNED_INT;
169 case GL_FLOAT_MAT2x3:
174 case GL_FLOAT_MAT2x4:
179 case GL_FLOAT_MAT3x2:
184 case GL_FLOAT_MAT3x4:
189 case GL_FLOAT_MAT4x2:
194 case GL_FLOAT_MAT4x3:
200 elemType = GL_DOUBLE;
205 elemType = GL_DOUBLE;
210 elemType = GL_DOUBLE;
214 case GL_DOUBLE_MAT2x3:
215 elemType = GL_DOUBLE;
219 case GL_DOUBLE_MAT2x4:
220 elemType = GL_DOUBLE;
224 case GL_DOUBLE_MAT3x2:
225 elemType = GL_DOUBLE;
229 case GL_DOUBLE_MAT3x4:
230 elemType = GL_DOUBLE;
234 case GL_DOUBLE_MAT4x2:
235 elemType = GL_DOUBLE;
239 case GL_DOUBLE_MAT4x3:
240 elemType = GL_DOUBLE;
247 case GL_SAMPLER_CUBE:
248 case GL_SAMPLER_1D_SHADOW:
249 case GL_SAMPLER_2D_SHADOW:
250 case GL_SAMPLER_1D_ARRAY:
251 case GL_SAMPLER_2D_ARRAY:
252 case GL_SAMPLER_CUBE_MAP_ARRAY:
253 case GL_SAMPLER_1D_ARRAY_SHADOW:
254 case GL_SAMPLER_2D_ARRAY_SHADOW:
255 case GL_SAMPLER_2D_MULTISAMPLE:
256 case GL_SAMPLER_2D_MULTISAMPLE_ARRAY:
257 case GL_SAMPLER_CUBE_SHADOW:
258 case GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW:
259 case GL_SAMPLER_BUFFER:
260 case GL_SAMPLER_2D_RECT:
261 case GL_SAMPLER_2D_RECT_SHADOW:
262 case GL_INT_SAMPLER_1D:
263 case GL_INT_SAMPLER_2D:
264 case GL_INT_SAMPLER_3D:
265 case GL_INT_SAMPLER_CUBE:
266 case GL_INT_SAMPLER_1D_ARRAY:
267 case GL_INT_SAMPLER_2D_ARRAY:
268 case GL_INT_SAMPLER_CUBE_MAP_ARRAY:
269 case GL_INT_SAMPLER_2D_MULTISAMPLE:
270 case GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
271 case GL_INT_SAMPLER_BUFFER:
272 case GL_INT_SAMPLER_2D_RECT:
273 case GL_UNSIGNED_INT_SAMPLER_1D:
274 case GL_UNSIGNED_INT_SAMPLER_2D:
275 case GL_UNSIGNED_INT_SAMPLER_3D:
276 case GL_UNSIGNED_INT_SAMPLER_CUBE:
277 case GL_UNSIGNED_INT_SAMPLER_1D_ARRAY:
278 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
279 case GL_UNSIGNED_INT_SAMPLER_CUBE_MAP_ARRAY:
280 case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE:
281 case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
282 case GL_UNSIGNED_INT_SAMPLER_BUFFER:
283 case GL_UNSIGNED_INT_SAMPLER_2D_RECT:
287 os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, type);
296 _glArrayPointer_size(GLint size, GLenum type, GLsizei stride, GLsizei count)
302 size_t elementSize = size*_gl_type_size(type);
304 stride = (GLsizei)elementSize;
307 return stride*(count - 1) + elementSize;
310 #define _glVertexPointer_size(size, type, stride, count) _glArrayPointer_size(size, type, stride, count)
311 #define _glNormalPointer_size(type, stride, count) _glArrayPointer_size(3, type, stride, count)
312 #define _glColorPointer_size(size, type, stride, count) _glArrayPointer_size(size, type, stride, count)
313 #define _glIndexPointer_size(type, stride, count) _glArrayPointer_size(1, type, stride, count)
314 #define _glTexCoordPointer_size(size, type, stride, count) _glArrayPointer_size(size, type, stride, count)
315 #define _glEdgeFlagPointer_size(stride, count) _glArrayPointer_size(1, GL_BOOL, stride, count)
316 #define _glFogCoordPointer_size(type, stride, count) _glArrayPointer_size(1, type, stride, count)
317 #define _glSecondaryColorPointer_size(size, type, stride, count) _glArrayPointer_size(size, type, stride, count)
318 #define _glVertexAttribPointer_size(size, type, normalized, stride, count) _glArrayPointer_size(size, type, stride, count)
319 #define _glVertexAttribPointerARB_size(size, type, normalized, stride, count) _glArrayPointer_size(size, type, stride, count)
320 #define _glVertexAttribPointerNV_size(size, type, stride, count) _glArrayPointer_size(size, type, stride, count)
323 _glDrawArrays_count(GLint first, GLsizei count)
328 return first + count;
331 #define _glDrawArraysEXT_count _glDrawArrays_count
334 _glDrawElementsBaseVertex_count(GLsizei count, GLenum type, const GLvoid *indices, GLint basevertex)
337 GLint element_array_buffer = 0;
343 _glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &element_array_buffer);
344 if (element_array_buffer) {
345 // Read indices from index buffer object
346 GLintptr offset = (GLintptr)indices;
347 GLsizeiptr size = count*_gl_type_size(type);
348 GLvoid *temp = malloc(size);
352 memset(temp, 0, size);
353 _glGetBufferSubData(GL_ELEMENT_ARRAY_BUFFER, offset, size, temp);
363 if (type == GL_UNSIGNED_BYTE) {
364 const GLubyte *p = (const GLubyte *)indices;
365 for (i = 0; i < count; ++i) {
366 if (p[i] > maxindex) {
370 } else if (type == GL_UNSIGNED_SHORT) {
371 const GLushort *p = (const GLushort *)indices;
372 for (i = 0; i < count; ++i) {
373 if (p[i] > maxindex) {
377 } else if (type == GL_UNSIGNED_INT) {
378 const GLuint *p = (const GLuint *)indices;
379 for (i = 0; i < count; ++i) {
380 if (p[i] > maxindex) {
385 os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, type);
388 if (element_array_buffer) {
392 maxindex += basevertex;
397 #define _glDrawRangeElementsBaseVertex_count(start, end, count, type, indices, basevertex) _glDrawElementsBaseVertex_count(count, type, indices, basevertex)
399 #define _glDrawElements_count(count, type, indices) _glDrawElementsBaseVertex_count(count, type, indices, 0);
400 #define _glDrawRangeElements_count(start, end, count, type, indices) _glDrawElements_count(count, type, indices)
401 #define _glDrawRangeElementsEXT_count _glDrawRangeElements_count
403 /* FIXME take in consideration instancing */
404 #define _glDrawArraysInstanced_count(first, count, primcount) _glDrawArrays_count(first, count)
405 #define _glDrawElementsInstanced_count(count, type, indices, primcount) _glDrawElements_count(count, type, indices)
406 #define _glDrawElementsInstancedBaseVertex_count(count, type, indices, primcount, basevertex) _glDrawElementsBaseVertex_count(count, type, indices, basevertex)
407 #define _glDrawRangeElementsInstanced_count(start, end, count, type, indices, primcount) _glDrawRangeElements_count(start, end, count, type, indices)
408 #define _glDrawRangeElementsInstancedBaseVertex_count(start, end, count, type, indices, primcount, basevertex) _glDrawRangeElementsBaseVertex_count(start, end, count, type, indices, basevertex)
410 #define _glDrawArraysInstancedBaseInstance_count(first, count, primcount, baseinstance) _glDrawArrays_count(first, count)
411 #define _glDrawElementsInstancedBaseInstance_count(count, type, indices, primcount, baseinstance) _glDrawElements_count(count, type, indices)
412 #define _glDrawElementsInstancedBaseVertexBaseInstance_count(count, type, indices, primcount, basevertex, baseinstance) _glDrawElementsBaseVertex_count(count, type, indices, basevertex)
414 #define _glDrawArraysInstancedARB_count _glDrawArraysInstanced_count
415 #define _glDrawElementsInstancedARB_count _glDrawElementsInstanced_count
416 #define _glDrawArraysInstancedEXT_count _glDrawArraysInstanced_count
417 #define _glDrawElementsInstancedEXT_count _glDrawElementsInstanced_count
420 _glDrawArraysIndirect_count(const GLvoid *indirect) {
421 os::log("apitrace: warning: %s: unsupported\n", __FUNCTION__);
426 _glDrawElementsIndirect_count(GLenum type, const GLvoid *indirect) {
427 os::log("apitrace: warning: %s: unsupported\n", __FUNCTION__);
432 _glMultiDrawArrays_count(const GLint *first, const GLsizei *count, GLsizei primcount) {
434 for (GLsizei prim = 0; prim < primcount; ++prim) {
435 GLuint _count_prim = _glDrawArrays_count(first[prim], count[prim]);
436 _count = std::max(_count, _count_prim);
442 _glMultiDrawElements_count(const GLsizei *count, GLenum type, const GLvoid* *indices, GLsizei primcount) {
444 for (GLsizei prim = 0; prim < primcount; ++prim) {
445 GLuint _count_prim = _glDrawElements_count(count[prim], type, indices[prim]);
446 _count = std::max(_count, _count_prim);
452 _glMultiDrawElementsBaseVertex_count(const GLsizei *count, GLenum type, const GLvoid* *indices, GLsizei primcount, const GLint * basevertex) {
454 for (GLsizei prim = 0; prim < primcount; ++prim) {
455 GLuint _count_prim = _glDrawElementsBaseVertex_count(count[prim], type, indices[prim], basevertex[prim]);
456 _count = std::max(_count, _count_prim);
461 #define _glMultiDrawArraysEXT_count _glMultiDrawArrays_count
462 #define _glMultiDrawElementsEXT_count _glMultiDrawElements_count
464 #define _glMultiModeDrawArraysIBM_count(first, count, primcount, modestride) _glMultiDrawArrays_count(first, count, primcount)
465 #define _glMultiModeDrawElementsIBM_count(count, type, indices, primcount, modestride) _glMultiDrawElements_count(count, type, (const GLvoid **)indices, primcount)
469 _glCallLists_size(GLsizei n, GLenum type)
471 return n*_gl_type_size(type);
475 _glMap1d_size(GLenum target, GLint stride, GLint order)
484 case GL_MAP1_TEXTURE_COORD_1:
487 case GL_MAP1_TEXTURE_COORD_2:
491 case GL_MAP1_TEXTURE_COORD_3:
492 case GL_MAP1_VERTEX_3:
495 case GL_MAP1_COLOR_4:
496 case GL_MAP1_TEXTURE_COORD_4:
497 case GL_MAP1_VERTEX_4:
501 os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, target);
505 if (stride < channels) {
509 return channels + stride * (order - 1);
512 #define _glMap1f_size _glMap1d_size
515 _glMap2d_size(GLenum target, GLint ustride, GLint uorder, GLint vstride, GLint vorder)
517 if (uorder < 1 || vorder < 1) {
524 case GL_MAP2_TEXTURE_COORD_1:
527 case GL_MAP2_TEXTURE_COORD_2:
531 case GL_MAP2_TEXTURE_COORD_3:
532 case GL_MAP2_VERTEX_3:
535 case GL_MAP2_COLOR_4:
536 case GL_MAP2_TEXTURE_COORD_4:
537 case GL_MAP2_VERTEX_4:
541 os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, target);
545 if (ustride < channels || vstride < channels) {
550 ustride * (uorder - 1) +
551 vstride * (vorder - 1);
554 #define _glMap2f_size _glMap2d_size
556 static inline unsigned
557 _gl_format_channels(GLenum format) {
566 case GL_DEPTH_COMPONENT:
567 case GL_STENCIL_INDEX:
569 case GL_DEPTH_STENCIL:
570 case GL_LUMINANCE_ALPHA:
583 case GL_DSDT_MAG_VIB_NV:
588 os::log("apitrace: warning: %s: unexpected format GLenum 0x%04X\n", __FUNCTION__, format);
596 return (x & (x - 1)) == 0;
599 template<class X, class Y>
602 return (x + (y - 1)) & ~(y - 1);
606 _gl_image_size(GLenum format, GLenum type, GLsizei width, GLsizei height, GLsizei depth, GLboolean has_unpack_subimage) {
607 unsigned num_channels = _gl_format_channels(format);
609 unsigned bits_per_pixel;
615 case GL_UNSIGNED_BYTE:
616 bits_per_pixel = 8 * num_channels;
619 case GL_UNSIGNED_SHORT:
621 bits_per_pixel = 16 * num_channels;
624 case GL_UNSIGNED_INT:
626 bits_per_pixel = 32 * num_channels;
628 case GL_UNSIGNED_BYTE_3_3_2:
629 case GL_UNSIGNED_BYTE_2_3_3_REV:
632 case GL_UNSIGNED_SHORT_4_4_4_4:
633 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
634 case GL_UNSIGNED_SHORT_5_5_5_1:
635 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
636 case GL_UNSIGNED_SHORT_5_6_5:
637 case GL_UNSIGNED_SHORT_5_6_5_REV:
638 case GL_UNSIGNED_SHORT_8_8_MESA:
639 case GL_UNSIGNED_SHORT_8_8_REV_MESA:
642 case GL_UNSIGNED_INT_8_8_8_8:
643 case GL_UNSIGNED_INT_8_8_8_8_REV:
644 case GL_UNSIGNED_INT_10_10_10_2:
645 case GL_UNSIGNED_INT_2_10_10_10_REV:
646 case GL_UNSIGNED_INT_24_8:
647 case GL_UNSIGNED_INT_10F_11F_11F_REV:
648 case GL_UNSIGNED_INT_5_9_9_9_REV:
649 case GL_UNSIGNED_INT_S8_S8_8_8_NV:
650 case GL_UNSIGNED_INT_8_8_S8_S8_REV_NV:
653 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
657 os::log("apitrace: warning: %s: unexpected type GLenum 0x%04X\n", __FUNCTION__, type);
663 GLint row_length = 0;
664 GLint image_height = 0;
666 GLint skip_pixels = 0;
667 GLint skip_images = 0;
669 _glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
670 if (has_unpack_subimage) {
671 _glGetIntegerv(GL_UNPACK_ROW_LENGTH, &row_length);
672 _glGetIntegerv(GL_UNPACK_IMAGE_HEIGHT, &image_height);
673 _glGetIntegerv(GL_UNPACK_SKIP_ROWS, &skip_rows);
674 _glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &skip_pixels);
675 _glGetIntegerv(GL_UNPACK_SKIP_IMAGES, &skip_images);
678 if (row_length <= 0) {
682 size_t row_stride = (row_length*bits_per_pixel + 7)/8;
684 if ((GLint)bits_per_pixel < alignment*8 &&
685 (bits_per_pixel & 7) == 0 &&
686 _is_pot(bits_per_pixel)) {
687 row_stride = _align(row_stride, alignment);
690 if (image_height <= 0) {
691 image_height = height;
694 /* XXX: GL_UNPACK_IMAGE_HEIGHT and GL_UNPACK_SKIP_IMAGES should probably
695 * not be considered for pixel rectangles. */
697 size_t image_stride = image_height*row_stride;
699 size_t size = depth*image_stride;
701 size += (skip_pixels*bits_per_pixel + 7)/8;
702 size += skip_rows*row_stride;
703 size += skip_images*image_stride;
708 // note that can_unpack_subimage() is generated by gltrace.py
709 #define _glTexImage3D_size(format, type, width, height, depth) _gl_image_size(format, type, width, height, depth, can_unpack_subimage())
710 #define _glTexImage2D_size(format, type, width, height) _gl_image_size(format, type, width, height, 1, can_unpack_subimage())
711 #define _glTexImage1D_size(format, type, width) _gl_image_size(format, type, width, 1, 1, can_unpack_subimage())
713 #define _glTexSubImage3D_size(format, type, width, height, depth) _glTexImage3D_size(format, type, width, height, depth)
714 #define _glTexSubImage2D_size(format, type, width, height) _glTexImage2D_size(format, type, width, height)
715 #define _glTexSubImage1D_size(format, type, width) _glTexImage1D_size(format, type, width)
717 #define _glTexImage3DEXT_size _glTexImage3D_size
718 #define _glTexImage2DEXT_size _glTexImage2D_size
719 #define _glTexImage1DEXT_size _glTexImage1D_size
720 #define _glTexSubImage3DEXT_size _glTexSubImage3D_size
721 #define _glTexSubImage2DEXT_size _glTexSubImage2D_size
722 #define _glTexSubImage1DEXT_size _glTexSubImage1D_size
724 #define _glTextureImage3DEXT_size _glTexImage3D_size
725 #define _glTextureImage2DEXT_size _glTexImage2D_size
726 #define _glTextureImage1DEXT_size _glTexImage1D_size
727 #define _glTextureSubImage3DEXT_size _glTexSubImage3D_size
728 #define _glTextureSubImage2DEXT_size _glTexSubImage2D_size
729 #define _glTextureSubImage1DEXT_size _glTexSubImage1D_size
731 #define _glMultiTexImage3DEXT_size _glTexImage3D_size
732 #define _glMultiTexImage2DEXT_size _glTexImage2D_size
733 #define _glMultiTexImage1DEXT_size _glTexImage1D_size
734 #define _glMultiTexSubImage3DEXT_size _glTexSubImage3D_size
735 #define _glMultiTexSubImage2DEXT_size _glTexSubImage2D_size
736 #define _glMultiTexSubImage1DEXT_size _glTexSubImage1D_size
738 #define _glDrawPixels_size(format, type, width, height) _glTexImage2D_size(format, type, width, height)
739 #define _glConvolutionFilter1D_size(format, type, width) _glTexImage1D_size(format, type, width)
740 #define _glConvolutionFilter2D_size(format, type, width, height) _glTexImage2D_size(format, type, width, height)
741 #define _glColorTable_size(format, type, width) _glTexImage1D_size(format, type, width)
742 #define _glColorSubTable_size(format, type, count) _glColorTable_size(format, type, count)
744 #define _glBitmap_size(width, height) _glTexImage2D_size(GL_COLOR_INDEX, GL_BITMAP, width, height)
745 #define _glPolygonStipple_size() _glBitmap_size(32, 32)
748 _glClearBuffer_size(GLenum buffer)
756 case GL_FRONT_AND_BACK:
762 os::log("apitrace: warning: %s: unexpected buffer GLenum 0x%04X\n", __FUNCTION__, buffer);
768 * attribute list, terminated by the given terminator.
772 _AttribList_size(const T *pAttribList, const T terminator = static_cast<T>(0))
779 } while (*pAttribList++ != terminator);
787 * (key, value) attribute list, terminated by the given terminator.
791 _AttribPairList_size(const T *pAttribList, const T terminator = static_cast<T>(0))
796 while (pAttribList[size] != terminator) {
799 // terminator also counts
807 #endif /* _GL_SIZE_HPP_ */