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:
67 OS::DebugMessage("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, type);
73 __gl_uniform_size(GLenum type, GLenum &elemType, GLint &numElems) {
100 elemType = GL_DOUBLE;
104 elemType = GL_DOUBLE;
123 case GL_UNSIGNED_INT:
124 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;
167 case GL_FLOAT_MAT2x3:
171 case GL_FLOAT_MAT2x4:
175 case GL_FLOAT_MAT3x2:
179 case GL_FLOAT_MAT3x4:
183 case GL_FLOAT_MAT4x2:
187 case GL_FLOAT_MAT4x3:
192 elemType = GL_DOUBLE;
196 elemType = GL_DOUBLE;
200 elemType = GL_DOUBLE;
203 case GL_DOUBLE_MAT2x3:
204 elemType = GL_DOUBLE;
207 case GL_DOUBLE_MAT2x4:
208 elemType = GL_DOUBLE;
211 case GL_DOUBLE_MAT3x2:
212 elemType = GL_DOUBLE;
215 case GL_DOUBLE_MAT3x4:
216 elemType = GL_DOUBLE;
219 case GL_DOUBLE_MAT4x2:
220 elemType = GL_DOUBLE;
223 case GL_DOUBLE_MAT4x3:
224 elemType = GL_DOUBLE;
230 case GL_SAMPLER_CUBE:
231 case GL_SAMPLER_1D_SHADOW:
232 case GL_SAMPLER_2D_SHADOW:
233 case GL_SAMPLER_1D_ARRAY:
234 case GL_SAMPLER_2D_ARRAY:
235 case GL_SAMPLER_CUBE_MAP_ARRAY:
236 case GL_SAMPLER_1D_ARRAY_SHADOW:
237 case GL_SAMPLER_2D_ARRAY_SHADOW:
238 case GL_SAMPLER_2D_MULTISAMPLE:
239 case GL_SAMPLER_2D_MULTISAMPLE_ARRAY:
240 case GL_SAMPLER_CUBE_SHADOW:
241 case GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW:
242 case GL_SAMPLER_BUFFER:
243 case GL_SAMPLER_2D_RECT:
244 case GL_SAMPLER_2D_RECT_SHADOW:
245 case GL_INT_SAMPLER_1D:
246 case GL_INT_SAMPLER_2D:
247 case GL_INT_SAMPLER_3D:
248 case GL_INT_SAMPLER_CUBE:
249 case GL_INT_SAMPLER_1D_ARRAY:
250 case GL_INT_SAMPLER_2D_ARRAY:
251 case GL_INT_SAMPLER_CUBE_MAP_ARRAY:
252 case GL_INT_SAMPLER_2D_MULTISAMPLE:
253 case GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
254 case GL_INT_SAMPLER_BUFFER:
255 case GL_INT_SAMPLER_2D_RECT:
256 case GL_UNSIGNED_INT_SAMPLER_1D:
257 case GL_UNSIGNED_INT_SAMPLER_2D:
258 case GL_UNSIGNED_INT_SAMPLER_3D:
259 case GL_UNSIGNED_INT_SAMPLER_CUBE:
260 case GL_UNSIGNED_INT_SAMPLER_1D_ARRAY:
261 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
262 case GL_UNSIGNED_INT_SAMPLER_CUBE_MAP_ARRAY:
263 case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE:
264 case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
265 case GL_UNSIGNED_INT_SAMPLER_BUFFER:
266 case GL_UNSIGNED_INT_SAMPLER_2D_RECT:
271 OS::DebugMessage("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, type);
279 __glArrayPointer_size(GLint size, GLenum type, GLsizei stride, GLsizei maxIndex)
281 size_t elementSize = size*__gl_type_size(type);
283 stride = (GLsizei)elementSize;
285 return stride*maxIndex + elementSize;
288 #define __glVertexPointer_size(size, type, stride, maxIndex) __glArrayPointer_size(size, type, stride, maxIndex)
289 #define __glNormalPointer_size(type, stride, maxIndex) __glArrayPointer_size(3, type, stride, maxIndex)
290 #define __glColorPointer_size(size, type, stride, maxIndex) __glArrayPointer_size(size, type, stride, maxIndex)
291 #define __glIndexPointer_size(type, stride, maxIndex) __glArrayPointer_size(1, type, stride, maxIndex)
292 #define __glTexCoordPointer_size(size, type, stride, maxIndex) __glArrayPointer_size(size, type, stride, maxIndex)
293 #define __glEdgeFlagPointer_size(stride, maxIndex) __glArrayPointer_size(1, GL_BOOL, stride, maxIndex)
294 #define __glFogCoordPointer_size(type, stride, maxIndex) __glArrayPointer_size(1, type, stride, maxIndex)
295 #define __glSecondaryColorPointer_size(size, type, stride, maxIndex) __glArrayPointer_size(size, type, stride, maxIndex)
296 #define __glVertexAttribPointer_size(size, type, normalized, stride, maxIndex) __glArrayPointer_size(size, type, stride, maxIndex)
297 #define __glVertexAttribPointerARB_size(size, type, normalized, stride, maxIndex) __glArrayPointer_size(size, type, stride, maxIndex)
298 #define __glVertexAttribPointerNV_size(size, type, stride, maxIndex) __glArrayPointer_size(size, type, stride, maxIndex)
301 __glDrawArrays_maxindex(GLint first, GLsizei count)
306 return first + count - 1;
309 #define __glDrawArraysEXT_maxindex __glDrawArrays_maxindex
312 __glDrawElementsBaseVertex_maxindex(GLsizei count, GLenum type, const GLvoid *indices, GLint basevertex)
315 GLint __element_array_buffer = 0;
321 __glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &__element_array_buffer);
322 if (__element_array_buffer) {
323 // Read indices from index buffer object
324 GLintptr offset = (GLintptr)indices;
325 GLsizeiptr size = count*__gl_type_size(type);
326 GLvoid *temp = malloc(size);
330 memset(temp, 0, size);
331 __glGetBufferSubData(GL_ELEMENT_ARRAY_BUFFER, offset, size, temp);
341 if (type == GL_UNSIGNED_BYTE) {
342 const GLubyte *p = (const GLubyte *)indices;
343 for (i = 0; i < count; ++i) {
344 if (p[i] > maxindex) {
348 } else if (type == GL_UNSIGNED_SHORT) {
349 const GLushort *p = (const GLushort *)indices;
350 for (i = 0; i < count; ++i) {
351 if (p[i] > maxindex) {
355 } else if (type == GL_UNSIGNED_INT) {
356 const GLuint *p = (const GLuint *)indices;
357 for (i = 0; i < count; ++i) {
358 if (p[i] > maxindex) {
363 OS::DebugMessage("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, type);
366 if (__element_array_buffer) {
370 maxindex += basevertex;
375 #define __glDrawRangeElementsBaseVertex_maxindex(start, end, count, type, indices, basevertex) __glDrawElementsBaseVertex_maxindex(count, type, indices, basevertex)
377 #define __glDrawElements_maxindex(count, type, indices) __glDrawElementsBaseVertex_maxindex(count, type, indices, 0);
378 #define __glDrawRangeElements_maxindex(start, end, count, type, indices) __glDrawElements_maxindex(count, type, indices)
379 #define __glDrawRangeElementsEXT_maxindex __glDrawRangeElements_maxindex
381 /* FIXME take in consideration instancing */
382 #define __glDrawArraysInstanced_maxindex(first, count, primcount) __glDrawArrays_maxindex(first, count)
383 #define __glDrawElementsInstanced_maxindex(count, type, indices, primcount) __glDrawElements_maxindex(count, type, indices)
384 #define __glDrawElementsInstancedBaseVertex_maxindex(count, type, indices, primcount, basevertex) __glDrawElementsBaseVertex_maxindex(count, type, indices, basevertex)
385 #define __glDrawRangeElementsInstanced_maxindex(start, end, count, type, indices, primcount) __glDrawRangeElements_maxindex(start, end, count, type, indices)
386 #define __glDrawRangeElementsInstancedBaseVertex_maxindex(start, end, count, type, indices, primcount, basevertex) __glDrawRangeElementsBaseVertex_maxindex(start, end, count, type, indices, basevertex)
388 #define __glDrawArraysInstancedBaseInstance_maxindex(first, count, primcount, baseinstance) __glDrawArrays_maxindex(first, count)
389 #define __glDrawElementsInstancedBaseInstance_maxindex(count, type, indices, primcount, baseinstance) __glDrawElements_maxindex(count, type, indices)
390 #define __glDrawElementsInstancedBaseVertexBaseInstance_maxindex(count, type, indices, primcount, basevertex, baseinstance) __glDrawElementsBaseVertex_maxindex(count, type, indices, basevertex)
392 #define __glDrawArraysInstancedARB_maxindex __glDrawArraysInstanced_maxindex
393 #define __glDrawElementsInstancedARB_maxindex __glDrawElementsInstanced_maxindex
394 #define __glDrawArraysInstancedEXT_maxindex __glDrawArraysInstanced_maxindex
395 #define __glDrawElementsInstancedEXT_maxindex __glDrawElementsInstanced_maxindex
398 __glDrawArraysIndirect_maxindex(const GLvoid *indirect) {
399 OS::DebugMessage("apitrace: warning: %s: unsupported\n", __FUNCTION__);
404 __glDrawElementsIndirect_maxindex(GLenum type, const GLvoid *indirect) {
405 OS::DebugMessage("apitrace: warning: %s: unsupported\n", __FUNCTION__);
410 __glMultiDrawArrays_maxindex(const GLint *first, const GLsizei *count, GLsizei primcount) {
412 for (GLsizei prim = 0; prim < primcount; ++prim) {
413 GLuint maxindex_prim = __glDrawArrays_maxindex(first[prim], count[prim]);
414 maxindex = std::max(maxindex, maxindex_prim);
420 __glMultiDrawElements_maxindex(const GLsizei *count, GLenum type, const GLvoid* *indices, GLsizei primcount) {
422 for (GLsizei prim = 0; prim < primcount; ++prim) {
423 GLuint maxindex_prim = __glDrawElements_maxindex(count[prim], type, indices[prim]);
424 maxindex = std::max(maxindex, maxindex_prim);
430 __glMultiDrawElementsBaseVertex_maxindex(const GLsizei *count, GLenum type, const GLvoid* *indices, GLsizei primcount, const GLint * basevertex) {
432 for (GLsizei prim = 0; prim < primcount; ++prim) {
433 GLuint maxindex_prim = __glDrawElementsBaseVertex_maxindex(count[prim], type, indices[prim], basevertex[prim]);
434 maxindex = std::max(maxindex, maxindex_prim);
439 #define __glMultiDrawArraysEXT_maxindex __glMultiDrawArrays_maxindex
440 #define __glMultiDrawElementsEXT_maxindex __glMultiDrawElements_maxindex
442 #define __glMultiModeDrawArraysIBM_maxindex(first, count, primcount, modestride) __glMultiDrawArrays_maxindex(first, count, primcount)
443 #define __glMultiModeDrawElementsIBM_maxindex(count, type, indices, primcount, modestride) __glMultiDrawElements_maxindex(count, type, (const GLvoid **)indices, primcount)
447 __glCallLists_size(GLsizei n, GLenum type)
449 return n*__gl_type_size(type);
453 __glMap1d_size(GLenum target, GLint stride, GLint order)
462 case GL_MAP1_TEXTURE_COORD_1:
465 case GL_MAP1_TEXTURE_COORD_2:
469 case GL_MAP1_TEXTURE_COORD_3:
470 case GL_MAP1_VERTEX_3:
473 case GL_MAP1_COLOR_4:
474 case GL_MAP1_TEXTURE_COORD_4:
475 case GL_MAP1_VERTEX_4:
479 OS::DebugMessage("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, target);
483 if (stride < channels) {
487 return channels + stride * (order - 1);
490 #define __glMap1f_size __glMap1d_size
493 __glMap2d_size(GLenum target, GLint ustride, GLint uorder, GLint vstride, GLint vorder)
495 if (uorder < 1 || vorder < 1) {
502 case GL_MAP2_TEXTURE_COORD_1:
505 case GL_MAP2_TEXTURE_COORD_2:
509 case GL_MAP2_TEXTURE_COORD_3:
510 case GL_MAP2_VERTEX_3:
513 case GL_MAP2_COLOR_4:
514 case GL_MAP2_TEXTURE_COORD_4:
515 case GL_MAP2_VERTEX_4:
519 OS::DebugMessage("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, target);
523 if (ustride < channels || vstride < channels) {
528 ustride * (uorder - 1) +
529 vstride * (vorder - 1);
532 #define __glMap2f_size __glMap2d_size
534 static inline unsigned
535 __gl_format_channels(GLenum format) {
544 case GL_DEPTH_COMPONENT:
545 case GL_STENCIL_INDEX:
547 case GL_DEPTH_STENCIL:
548 case GL_LUMINANCE_ALPHA:
561 case GL_DSDT_MAG_VIB_NV:
566 OS::DebugMessage("apitrace: warning: %s: unexpected format GLenum 0x%04X\n", __FUNCTION__, format);
574 return (x & (x - 1)) == 0;
577 template<class X, class Y>
580 return (x + (y - 1)) & ~(y - 1);
584 __gl_image_size(GLenum format, GLenum type, GLsizei width, GLsizei height, GLsizei depth) {
585 unsigned num_channels = __gl_format_channels(format);
587 unsigned bits_per_pixel;
593 case GL_UNSIGNED_BYTE:
594 bits_per_pixel = 8 * num_channels;
597 case GL_UNSIGNED_SHORT:
599 bits_per_pixel = 16 * num_channels;
602 case GL_UNSIGNED_INT:
604 bits_per_pixel = 32 * num_channels;
606 case GL_UNSIGNED_BYTE_3_3_2:
607 case GL_UNSIGNED_BYTE_2_3_3_REV:
610 case GL_UNSIGNED_SHORT_4_4_4_4:
611 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
612 case GL_UNSIGNED_SHORT_5_5_5_1:
613 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
614 case GL_UNSIGNED_SHORT_5_6_5:
615 case GL_UNSIGNED_SHORT_5_6_5_REV:
616 case GL_UNSIGNED_SHORT_8_8_MESA:
617 case GL_UNSIGNED_SHORT_8_8_REV_MESA:
620 case GL_UNSIGNED_INT_8_8_8_8:
621 case GL_UNSIGNED_INT_8_8_8_8_REV:
622 case GL_UNSIGNED_INT_10_10_10_2:
623 case GL_UNSIGNED_INT_2_10_10_10_REV:
624 case GL_UNSIGNED_INT_24_8:
625 case GL_UNSIGNED_INT_10F_11F_11F_REV:
626 case GL_UNSIGNED_INT_5_9_9_9_REV:
627 case GL_UNSIGNED_INT_S8_S8_8_8_NV:
628 case GL_UNSIGNED_INT_8_8_S8_S8_REV_NV:
631 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
635 OS::DebugMessage("apitrace: warning: %s: unexpected type GLenum 0x%04X\n", __FUNCTION__, type);
641 GLint row_length = 0;
642 GLint image_height = 0;
644 GLint skip_pixels = 0;
645 GLint skip_images = 0;
647 __glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
648 __glGetIntegerv(GL_UNPACK_ROW_LENGTH, &row_length);
649 __glGetIntegerv(GL_UNPACK_IMAGE_HEIGHT, &image_height);
650 __glGetIntegerv(GL_UNPACK_SKIP_ROWS, &skip_rows);
651 __glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &skip_pixels);
652 __glGetIntegerv(GL_UNPACK_SKIP_IMAGES, &skip_images);
654 if (row_length <= 0) {
658 size_t row_stride = (row_length*bits_per_pixel + 7)/8;
660 if ((GLint)bits_per_pixel < alignment*8 &&
661 (bits_per_pixel & 7) == 0 &&
662 _is_pot(bits_per_pixel)) {
663 row_stride = _align(row_stride, alignment);
666 if (image_height <= 0) {
667 image_height = height;
670 /* XXX: GL_UNPACK_IMAGE_HEIGHT and GL_UNPACK_SKIP_IMAGES should probably
671 * not be considered for pixel rectangles. */
673 size_t image_stride = image_height*row_stride;
675 size_t size = depth*image_stride;
677 size += (skip_pixels*bits_per_pixel + 7)/8;
678 size += skip_rows*row_stride;
679 size += skip_images*image_stride;
684 #define __glTexImage3D_size(format, type, width, height, depth) __gl_image_size(format, type, width, height, depth)
685 #define __glTexImage2D_size(format, type, width, height) __gl_image_size(format, type, width, height, 1)
686 #define __glTexImage1D_size(format, type, width) __gl_image_size(format, type, width, 1, 1)
688 #define __glTexSubImage3D_size(format, type, width, height, depth) __glTexImage3D_size(format, type, width, height, depth)
689 #define __glTexSubImage2D_size(format, type, width, height) __glTexImage2D_size(format, type, width, height)
690 #define __glTexSubImage1D_size(format, type, width) __glTexImage1D_size(format, type, width)
692 #define __glTexImage3DEXT_size __glTexImage3D_size
693 #define __glTexImage2DEXT_size __glTexImage2D_size
694 #define __glTexImage1DEXT_size __glTexImage1D_size
695 #define __glTexSubImage3DEXT_size __glTexSubImage3D_size
696 #define __glTexSubImage2DEXT_size __glTexSubImage2D_size
697 #define __glTexSubImage1DEXT_size __glTexSubImage1D_size
699 #define __glTextureImage3DEXT_size __glTexImage3D_size
700 #define __glTextureImage2DEXT_size __glTexImage2D_size
701 #define __glTextureImage1DEXT_size __glTexImage1D_size
702 #define __glTextureSubImage3DEXT_size __glTexSubImage3D_size
703 #define __glTextureSubImage2DEXT_size __glTexSubImage2D_size
704 #define __glTextureSubImage1DEXT_size __glTexSubImage1D_size
706 #define __glMultiTexImage3DEXT_size __glTexImage3D_size
707 #define __glMultiTexImage2DEXT_size __glTexImage2D_size
708 #define __glMultiTexImage1DEXT_size __glTexImage1D_size
709 #define __glMultiTexSubImage3DEXT_size __glTexSubImage3D_size
710 #define __glMultiTexSubImage2DEXT_size __glTexSubImage2D_size
711 #define __glMultiTexSubImage1DEXT_size __glTexSubImage1D_size
713 #define __glDrawPixels_size(format, type, width, height) __glTexImage2D_size(format, type, width, height)
714 #define __glConvolutionFilter1D_size(format, type, width) __glTexImage1D_size(format, type, width)
715 #define __glConvolutionFilter2D_size(format, type, width, height) __glTexImage2D_size(format, type, width, height)
716 #define __glColorTable_size(format, type, width) __glTexImage1D_size(format, type, width)
717 #define __glColorSubTable_size(format, type, count) __glColorTable_size(format, type, count)
719 #define __glBitmap_size(width, height) __glTexImage2D_size(GL_COLOR_INDEX, GL_BITMAP, width, height)
720 #define __glPolygonStipple_size() __glBitmap_size(32, 32)
723 __glClearBuffer_size(GLenum buffer)
731 case GL_FRONT_AND_BACK:
737 OS::DebugMessage("apitrace: warning: %s: unexpected buffer GLenum 0x%04X\n", __FUNCTION__, buffer);
743 * 0 terminated integer/float attribute list.
747 __AttribList_size(const T *pAttribList)
754 } while (*pAttribList++);
761 #endif /* _GL_SIZE_HPP_ */