1/* 2* Copyright (C) 2011 The Android Open Source Project 3* 4* Licensed under the Apache License, Version 2.0 (the "License"); 5* you may not use this file except in compliance with the License. 6* You may obtain a copy of the License at 7* 8* http://www.apache.org/licenses/LICENSE-2.0 9* 10* Unless required by applicable law or agreed to in writing, software 11* distributed under the License is distributed on an "AS IS" BASIS, 12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13* See the License for the specific language governing permissions and 14* limitations under the License. 15*/ 16#include "glUtils.h" 17#include <string.h> 18#include "ErrorLog.h" 19#include <IOStream.h> 20 21size_t glSizeof(GLenum type) 22{ 23 size_t retval = 0; 24 switch(type) { 25 case GL_BYTE: 26 case GL_UNSIGNED_BYTE: 27 retval = 1; 28 break; 29 case GL_SHORT: 30 case GL_UNSIGNED_SHORT: 31 case GL_HALF_FLOAT_OES: 32 retval = 2; 33 break; 34 case GL_INT: 35 case GL_FLOAT: 36 case GL_FIXED: 37 case GL_BOOL: 38 retval = 4; 39 break; 40#ifdef GL_DOUBLE 41 case GL_DOUBLE: 42 retval = 8; 43 break; 44#endif 45 case GL_FLOAT_VEC2: 46 case GL_INT_VEC2: 47 case GL_BOOL_VEC2: 48 retval = 8; 49 break; 50 case GL_INT_VEC3: 51 case GL_BOOL_VEC3: 52 case GL_FLOAT_VEC3: 53 retval = 12; 54 break; 55 case GL_FLOAT_VEC4: 56 case GL_BOOL_VEC4: 57 case GL_INT_VEC4: 58 case GL_FLOAT_MAT2: 59 retval = 16; 60 break; 61 case GL_FLOAT_MAT3: 62 retval = 36; 63 break; 64 case GL_FLOAT_MAT4: 65 retval = 64; 66 break; 67 case GL_SAMPLER_2D: 68 case GL_SAMPLER_CUBE: 69 retval = 4; 70 break; 71 default: 72 ERR("**** ERROR unknown type 0x%x (%s,%d)\n", type, __FUNCTION__,__LINE__); 73 } 74 return retval; 75 76} 77 78size_t glUtilsParamSize(GLenum param) 79{ 80 size_t s = 0; 81 82 switch(param) 83 { 84 case GL_DEPTH_TEST: 85 case GL_DEPTH_FUNC: 86 case GL_DEPTH_BITS: 87 case GL_MAX_CLIP_PLANES: 88 case GL_GREEN_BITS: 89 case GL_MAX_MODELVIEW_STACK_DEPTH: 90 case GL_MAX_PROJECTION_STACK_DEPTH: 91 case GL_MAX_TEXTURE_STACK_DEPTH: 92 case GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES: 93 case GL_IMPLEMENTATION_COLOR_READ_TYPE_OES: 94 case GL_NUM_COMPRESSED_TEXTURE_FORMATS: 95 case GL_MAX_TEXTURE_SIZE: 96 case GL_TEXTURE_GEN_MODE_OES: 97 case GL_TEXTURE_ENV_MODE: 98 case GL_FOG_MODE: 99 case GL_FOG_DENSITY: 100 case GL_FOG_START: 101 case GL_FOG_END: 102 case GL_SPOT_EXPONENT: 103 case GL_CONSTANT_ATTENUATION: 104 case GL_LINEAR_ATTENUATION: 105 case GL_QUADRATIC_ATTENUATION: 106 case GL_SHININESS: 107 case GL_LIGHT_MODEL_TWO_SIDE: 108 case GL_POINT_SIZE: 109 case GL_POINT_SIZE_MIN: 110 case GL_POINT_SIZE_MAX: 111 case GL_POINT_FADE_THRESHOLD_SIZE: 112 case GL_CULL_FACE_MODE: 113 case GL_FRONT_FACE: 114 case GL_SHADE_MODEL: 115 case GL_DEPTH_WRITEMASK: 116 case GL_DEPTH_CLEAR_VALUE: 117 case GL_STENCIL_FAIL: 118 case GL_STENCIL_PASS_DEPTH_FAIL: 119 case GL_STENCIL_PASS_DEPTH_PASS: 120 case GL_STENCIL_REF: 121 case GL_STENCIL_WRITEMASK: 122 case GL_MATRIX_MODE: 123 case GL_MODELVIEW_STACK_DEPTH: 124 case GL_PROJECTION_STACK_DEPTH: 125 case GL_TEXTURE_STACK_DEPTH: 126 case GL_ALPHA_TEST_FUNC: 127 case GL_ALPHA_TEST_REF: 128 case GL_ALPHA_TEST: 129 case GL_BLEND_DST: 130 case GL_BLEND_SRC: 131 case GL_BLEND: 132 case GL_LOGIC_OP_MODE: 133 case GL_SCISSOR_TEST: 134 case GL_MAX_TEXTURE_UNITS: 135 case GL_ACTIVE_TEXTURE: 136 case GL_ALPHA_BITS: 137 case GL_ARRAY_BUFFER_BINDING: 138 case GL_BLUE_BITS: 139 case GL_CLIENT_ACTIVE_TEXTURE: 140 case GL_CLIP_PLANE0: 141 case GL_CLIP_PLANE1: 142 case GL_CLIP_PLANE2: 143 case GL_CLIP_PLANE3: 144 case GL_CLIP_PLANE4: 145 case GL_CLIP_PLANE5: 146 case GL_COLOR_ARRAY: 147 case GL_COLOR_ARRAY_BUFFER_BINDING: 148 case GL_COLOR_ARRAY_SIZE: 149 case GL_COLOR_ARRAY_STRIDE: 150 case GL_COLOR_ARRAY_TYPE: 151 case GL_COLOR_LOGIC_OP: 152 case GL_COLOR_MATERIAL: 153 case GL_PACK_ALIGNMENT: 154 case GL_PERSPECTIVE_CORRECTION_HINT: 155 case GL_POINT_SIZE_ARRAY_BUFFER_BINDING_OES: 156 case GL_POINT_SIZE_ARRAY_STRIDE_OES: 157 case GL_POINT_SIZE_ARRAY_TYPE_OES: 158 case GL_POINT_SMOOTH: 159 case GL_POINT_SMOOTH_HINT: 160 case GL_POINT_SPRITE_OES: 161 case GL_COORD_REPLACE_OES: 162 case GL_COMBINE_ALPHA: 163 case GL_SRC0_RGB: 164 case GL_SRC1_RGB: 165 case GL_SRC2_RGB: 166 case GL_OPERAND0_RGB: 167 case GL_OPERAND1_RGB: 168 case GL_OPERAND2_RGB: 169 case GL_SRC0_ALPHA: 170 case GL_SRC1_ALPHA: 171 case GL_SRC2_ALPHA: 172 case GL_OPERAND0_ALPHA: 173 case GL_OPERAND1_ALPHA: 174 case GL_OPERAND2_ALPHA: 175 case GL_RGB_SCALE: 176 case GL_ALPHA_SCALE: 177 case GL_COMBINE_RGB: 178 case GL_POLYGON_OFFSET_FACTOR: 179 case GL_POLYGON_OFFSET_FILL: 180 case GL_POLYGON_OFFSET_UNITS: 181 case GL_RED_BITS: 182 case GL_RESCALE_NORMAL: 183 case GL_SAMPLE_ALPHA_TO_COVERAGE: 184 case GL_SAMPLE_ALPHA_TO_ONE: 185 case GL_SAMPLE_BUFFERS: 186 case GL_SAMPLE_COVERAGE: 187 case GL_SAMPLE_COVERAGE_INVERT: 188 case GL_SAMPLE_COVERAGE_VALUE: 189 case GL_SAMPLES: 190 case GL_STENCIL_BITS: 191 case GL_STENCIL_CLEAR_VALUE: 192 case GL_STENCIL_FUNC: 193 case GL_STENCIL_TEST: 194 case GL_STENCIL_VALUE_MASK: 195 case GL_STENCIL_BACK_FUNC: 196 case GL_STENCIL_BACK_VALUE_MASK: 197 case GL_STENCIL_BACK_REF: 198 case GL_STENCIL_BACK_FAIL: 199 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: 200 case GL_STENCIL_BACK_PASS_DEPTH_PASS: 201 case GL_STENCIL_BACK_WRITEMASK: 202 case GL_TEXTURE_2D: 203 case GL_TEXTURE_BINDING_2D: 204 case GL_TEXTURE_BINDING_CUBE_MAP: 205 case GL_TEXTURE_BINDING_EXTERNAL_OES: 206 case GL_TEXTURE_COORD_ARRAY: 207 case GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING: 208 case GL_TEXTURE_COORD_ARRAY_SIZE: 209 case GL_TEXTURE_COORD_ARRAY_STRIDE: 210 case GL_TEXTURE_COORD_ARRAY_TYPE: 211 case GL_UNPACK_ALIGNMENT: 212 case GL_VERTEX_ARRAY: 213 case GL_VERTEX_ARRAY_BUFFER_BINDING: 214 case GL_VERTEX_ARRAY_SIZE: 215 case GL_VERTEX_ARRAY_STRIDE: 216 case GL_VERTEX_ARRAY_TYPE: 217 case GL_SPOT_CUTOFF: 218 case GL_TEXTURE_MIN_FILTER: 219 case GL_TEXTURE_MAG_FILTER: 220 case GL_TEXTURE_WRAP_S: 221 case GL_TEXTURE_WRAP_T: 222 case GL_GENERATE_MIPMAP: 223 case GL_GENERATE_MIPMAP_HINT: 224 case GL_RENDERBUFFER_WIDTH_OES: 225 case GL_RENDERBUFFER_HEIGHT_OES: 226 case GL_RENDERBUFFER_INTERNAL_FORMAT_OES: 227 case GL_RENDERBUFFER_RED_SIZE_OES: 228 case GL_RENDERBUFFER_GREEN_SIZE_OES: 229 case GL_RENDERBUFFER_BLUE_SIZE_OES: 230 case GL_RENDERBUFFER_ALPHA_SIZE_OES: 231 case GL_RENDERBUFFER_DEPTH_SIZE_OES: 232 case GL_RENDERBUFFER_STENCIL_SIZE_OES: 233 case GL_RENDERBUFFER_BINDING: 234 case GL_FRAMEBUFFER_BINDING: 235 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_OES: 236 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_OES: 237 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_OES: 238 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_OES: 239 case GL_FENCE_STATUS_NV: 240 case GL_FENCE_CONDITION_NV: 241 case GL_TEXTURE_WIDTH_QCOM: 242 case GL_TEXTURE_HEIGHT_QCOM: 243 case GL_TEXTURE_DEPTH_QCOM: 244 case GL_TEXTURE_INTERNAL_FORMAT_QCOM: 245 case GL_TEXTURE_FORMAT_QCOM: 246 case GL_TEXTURE_TYPE_QCOM: 247 case GL_TEXTURE_IMAGE_VALID_QCOM: 248 case GL_TEXTURE_NUM_LEVELS_QCOM: 249 case GL_TEXTURE_TARGET_QCOM: 250 case GL_TEXTURE_OBJECT_VALID_QCOM: 251 case GL_BLEND_EQUATION_RGB_OES: 252 case GL_BLEND_EQUATION_ALPHA_OES: 253 case GL_BLEND_DST_RGB_OES: 254 case GL_BLEND_SRC_RGB_OES: 255 case GL_BLEND_DST_ALPHA_OES: 256 case GL_BLEND_SRC_ALPHA_OES: 257 case GL_MAX_LIGHTS: 258 case GL_SHADER_TYPE: 259 case GL_DELETE_STATUS: 260 case GL_COMPILE_STATUS: 261 case GL_INFO_LOG_LENGTH: 262 case GL_SHADER_SOURCE_LENGTH: 263 case GL_CURRENT_PROGRAM: 264 case GL_LINK_STATUS: 265 case GL_VALIDATE_STATUS: 266 case GL_ATTACHED_SHADERS: 267 case GL_ACTIVE_UNIFORMS: 268 case GL_ACTIVE_ATTRIBUTES: 269 case GL_SUBPIXEL_BITS: 270 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: 271 case GL_NUM_SHADER_BINARY_FORMATS: 272 case GL_SHADER_COMPILER: 273 case GL_MAX_VERTEX_ATTRIBS: 274 case GL_MAX_VERTEX_UNIFORM_VECTORS: 275 case GL_MAX_VARYING_VECTORS: 276 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: 277 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: 278 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: 279 case GL_MAX_RENDERBUFFER_SIZE: 280 case GL_MAX_TEXTURE_IMAGE_UNITS: 281 case GL_REQUIRED_TEXTURE_IMAGE_UNITS_OES: 282 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: 283 case GL_LINE_WIDTH: 284 s = 1; 285 break; 286 case GL_ALIASED_LINE_WIDTH_RANGE: 287 case GL_ALIASED_POINT_SIZE_RANGE: 288 case GL_DEPTH_RANGE: 289 case GL_MAX_VIEWPORT_DIMS: 290 case GL_SMOOTH_POINT_SIZE_RANGE: 291 case GL_SMOOTH_LINE_WIDTH_RANGE: 292 s= 2; 293 break; 294 case GL_SPOT_DIRECTION: 295 case GL_POINT_DISTANCE_ATTENUATION: 296 case GL_CURRENT_NORMAL: 297 s = 3; 298 break; 299 case GL_CURRENT_VERTEX_ATTRIB: 300 case GL_CURRENT_TEXTURE_COORDS: 301 case GL_CURRENT_COLOR: 302 case GL_FOG_COLOR: 303 case GL_AMBIENT: 304 case GL_DIFFUSE: 305 case GL_SPECULAR: 306 case GL_EMISSION: 307 case GL_POSITION: 308 case GL_LIGHT_MODEL_AMBIENT: 309 case GL_TEXTURE_ENV_COLOR: 310 case GL_SCISSOR_BOX: 311 case GL_VIEWPORT: 312 case GL_TEXTURE_CROP_RECT_OES: 313 case GL_COLOR_CLEAR_VALUE: 314 case GL_COLOR_WRITEMASK: 315 case GL_AMBIENT_AND_DIFFUSE: 316 case GL_BLEND_COLOR: 317 s = 4; 318 break; 319 case GL_MODELVIEW_MATRIX: 320 case GL_PROJECTION_MATRIX: 321 case GL_TEXTURE_MATRIX: 322 s = 16; 323 break; 324 default: 325 ERR("glUtilsParamSize: unknow param 0x%08x\n", param); 326 s = 1; // assume 1 327 } 328 return s; 329} 330 331void glUtilsPackPointerData(unsigned char *dst, unsigned char *src, 332 int size, GLenum type, unsigned int stride, 333 unsigned int datalen) 334{ 335 unsigned int vsize = size * glSizeof(type); 336 if (stride == 0) stride = vsize; 337 338 if (stride == vsize) { 339 memcpy(dst, src, datalen); 340 } else { 341 for (unsigned int i = 0; i < datalen; i += vsize) { 342 memcpy(dst, src, vsize); 343 dst += vsize; 344 src += stride; 345 } 346 } 347} 348 349void glUtilsWritePackPointerData(void* _stream, unsigned char *src, 350 int size, GLenum type, unsigned int stride, 351 unsigned int datalen) 352{ 353 IOStream* stream = reinterpret_cast<IOStream*>(_stream); 354 355 unsigned int vsize = size * glSizeof(type); 356 if (stride == 0) stride = vsize; 357 358 if (stride == vsize) { 359 stream->writeFully(src, datalen); 360 } else { 361 for (unsigned int i = 0; i < datalen; i += vsize) { 362 stream->writeFully(src, (size_t)vsize); 363 src += stride; 364 } 365 } 366} 367 368int glUtilsPixelBitSize(GLenum format, GLenum type) 369{ 370 int components = 0; 371 int componentsize = 0; 372 int pixelsize = 0; 373 switch(type) { 374 case GL_BYTE: 375 case GL_UNSIGNED_BYTE: 376 componentsize = 8; 377 break; 378 case GL_SHORT: 379 case GL_UNSIGNED_SHORT: 380 case GL_UNSIGNED_SHORT_5_6_5: 381 case GL_UNSIGNED_SHORT_4_4_4_4: 382 case GL_UNSIGNED_SHORT_5_5_5_1: 383 case GL_RGB565_OES: 384 case GL_RGB5_A1_OES: 385 case GL_RGBA4_OES: 386 pixelsize = 16; 387 break; 388 case GL_INT: 389 case GL_UNSIGNED_INT: 390 case GL_FLOAT: 391 case GL_FIXED: 392 case GL_UNSIGNED_INT_24_8_OES: 393 pixelsize = 32; 394 break; 395 default: 396 ERR("glUtilsPixelBitSize: unknown pixel type - assuming pixel data 0\n"); 397 componentsize = 0; 398 } 399 400 if (pixelsize == 0) { 401 switch(format) { 402#if 0 403 case GL_RED: 404 case GL_GREEN: 405 case GL_BLUE: 406#endif 407 case GL_ALPHA: 408 case GL_LUMINANCE: 409 case GL_DEPTH_COMPONENT: 410 case GL_DEPTH_STENCIL_OES: 411 components = 1; 412 break; 413 case GL_LUMINANCE_ALPHA: 414 components = 2; 415 break; 416 case GL_RGB: 417#if 0 418 case GL_BGR: 419#endif 420 components = 3; 421 break; 422 case GL_RGBA: 423 case GL_BGRA_EXT: 424 components = 4; 425 break; 426 default: 427 ERR("glUtilsPixelBitSize: unknown pixel format...\n"); 428 components = 0; 429 } 430 pixelsize = components * componentsize; 431 } 432 433 return pixelsize; 434} 435 436// pack a list of strings into one. 437void glUtilsPackStrings(char *ptr, char **strings, GLint *length, GLsizei count) 438{ 439 char *p = ptr; 440 *p = '\0'; 441 for (int i = 0; i < count; i++) { 442 int l=0; 443 if (strings[i]!=NULL) { 444 if (length == NULL || length[i] < 0) { 445 l = strlen(strings[i]); 446 strcat(p, strings[i]); 447 } else { 448 l = length[i]; 449 strncat(p, strings[i], l); 450 } 451 } 452 p += l; 453 } 454} 455 456// claculate the length of a list of strings 457int glUtilsCalcShaderSourceLen( char **strings, GLint *length, GLsizei count) 458{ 459 int len = 0; 460 for (int i = 0; i < count; i++) { 461 int l; 462 if (length == NULL || length[i] < 0) { 463 l = strings[i]!=NULL ? strlen(strings[i]) : 0; 464 } else { 465 l = length[i]; 466 } 467 len += l; 468 } 469 return len; 470 471} 472