1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file is here so other GLES2 related files can have a common set of
6 // includes where appropriate.
10 #include <GLES2/gl2ext.h>
12 #include "../common/gles2_cmd_utils.h"
13 #include "../common/gles2_cmd_format.h"
18 namespace gl_error_bit
{
21 kInvalidEnum
= (1 << 0),
22 kInvalidValue
= (1 << 1),
23 kInvalidOperation
= (1 << 2),
24 kOutOfMemory
= (1 << 3),
25 kInvalidFrameBufferOperation
= (1 << 4)
29 int GLES2Util::GLGetNumValuesReturned(int id
) const {
31 // -- glGetBooleanv, glGetFloatv, glGetIntergerv
32 case GL_ACTIVE_TEXTURE
:
34 case GL_ALIASED_LINE_WIDTH_RANGE
:
36 case GL_ALIASED_POINT_SIZE_RANGE
:
40 case GL_ARRAY_BUFFER_BINDING
:
46 case GL_BLEND_DST_ALPHA
:
48 case GL_BLEND_DST_RGB
:
50 case GL_BLEND_EQUATION_ALPHA
:
52 case GL_BLEND_EQUATION_RGB
:
54 case GL_BLEND_SRC_ALPHA
:
56 case GL_BLEND_SRC_RGB
:
60 case GL_COLOR_CLEAR_VALUE
:
62 case GL_COLOR_WRITEMASK
:
64 case GL_COMPRESSED_TEXTURE_FORMATS
:
65 return num_compressed_texture_formats_
;
68 case GL_CULL_FACE_MODE
:
70 case GL_CURRENT_PROGRAM
:
74 case GL_DEPTH_CLEAR_VALUE
:
82 case GL_DEPTH_WRITEMASK
:
86 case GL_ELEMENT_ARRAY_BUFFER_BINDING
:
88 case GL_FRAMEBUFFER_BINDING
:
92 case GL_GENERATE_MIPMAP_HINT
:
96 case GL_IMPLEMENTATION_COLOR_READ_FORMAT
:
98 case GL_IMPLEMENTATION_COLOR_READ_TYPE
:
102 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS
:
104 case GL_MAX_CUBE_MAP_TEXTURE_SIZE
:
106 case GL_MAX_FRAGMENT_UNIFORM_VECTORS
:
108 case GL_MAX_RENDERBUFFER_SIZE
:
110 case GL_MAX_TEXTURE_IMAGE_UNITS
:
112 case GL_MAX_TEXTURE_SIZE
:
114 case GL_MAX_VARYING_VECTORS
:
116 case GL_MAX_VERTEX_ATTRIBS
:
118 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS
:
120 case GL_MAX_VERTEX_UNIFORM_VECTORS
:
122 case GL_MAX_VIEWPORT_DIMS
:
124 case GL_NUM_COMPRESSED_TEXTURE_FORMATS
:
126 case GL_NUM_SHADER_BINARY_FORMATS
:
128 case GL_PACK_ALIGNMENT
:
130 case GL_PACK_REVERSE_ROW_ORDER_ANGLE
:
132 case GL_POLYGON_OFFSET_FACTOR
:
134 case GL_POLYGON_OFFSET_FILL
:
136 case GL_POLYGON_OFFSET_UNITS
:
140 case GL_RENDERBUFFER_BINDING
:
142 case GL_SAMPLE_BUFFERS
:
144 case GL_SAMPLE_COVERAGE_INVERT
:
146 case GL_SAMPLE_COVERAGE_VALUE
:
152 case GL_SCISSOR_TEST
:
154 case GL_SHADER_BINARY_FORMATS
:
155 return num_shader_binary_formats_
;
156 case GL_SHADER_COMPILER
:
158 case GL_STENCIL_BACK_FAIL
:
160 case GL_STENCIL_BACK_FUNC
:
162 case GL_STENCIL_BACK_PASS_DEPTH_FAIL
:
164 case GL_STENCIL_BACK_PASS_DEPTH_PASS
:
166 case GL_STENCIL_BACK_REF
:
168 case GL_STENCIL_BACK_VALUE_MASK
:
170 case GL_STENCIL_BACK_WRITEMASK
:
172 case GL_STENCIL_BITS
:
174 case GL_STENCIL_CLEAR_VALUE
:
176 case GL_STENCIL_FAIL
:
178 case GL_STENCIL_FUNC
:
180 case GL_STENCIL_PASS_DEPTH_FAIL
:
182 case GL_STENCIL_PASS_DEPTH_PASS
:
186 case GL_STENCIL_TEST
:
188 case GL_STENCIL_VALUE_MASK
:
190 case GL_STENCIL_WRITEMASK
:
192 case GL_SUBPIXEL_BITS
:
194 case GL_TEXTURE_BINDING_2D
:
196 case GL_TEXTURE_BINDING_CUBE_MAP
:
198 case GL_TEXTURE_BINDING_EXTERNAL_OES
:
200 case GL_TEXTURE_BINDING_RECTANGLE_ARB
:
202 case GL_TEXTURE_IMMUTABLE_FORMAT_EXT
:
204 case GL_UNPACK_ALIGNMENT
:
208 // -- glGetBooleanv, glGetFloatv, glGetIntergerv with
209 // GL_CHROMIUM_framebuffer_multisample
210 case GL_MAX_SAMPLES_EXT
:
212 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT
:
215 // -- glGetBufferParameteriv
218 case GL_BUFFER_USAGE
:
221 // -- glGetFramebufferAttachmentParameteriv
222 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE
:
224 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME
:
226 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL
:
228 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE
:
231 // -- glGetFramebufferAttachmentParameteriv
232 case GL_DELETE_STATUS
:
236 case GL_VALIDATE_STATUS
:
238 case GL_INFO_LOG_LENGTH
:
240 case GL_ATTACHED_SHADERS
:
242 case GL_ACTIVE_ATTRIBUTES
:
244 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
246 case GL_ACTIVE_UNIFORMS
:
248 case GL_ACTIVE_UNIFORM_MAX_LENGTH
:
252 // -- glGetRenderbufferAttachmentParameteriv
253 case GL_RENDERBUFFER_WIDTH
:
255 case GL_RENDERBUFFER_HEIGHT
:
257 case GL_RENDERBUFFER_INTERNAL_FORMAT
:
259 case GL_RENDERBUFFER_RED_SIZE
:
261 case GL_RENDERBUFFER_GREEN_SIZE
:
263 case GL_RENDERBUFFER_BLUE_SIZE
:
265 case GL_RENDERBUFFER_ALPHA_SIZE
:
267 case GL_RENDERBUFFER_DEPTH_SIZE
:
269 case GL_RENDERBUFFER_STENCIL_SIZE
:
275 // Already defined under glGetFramebufferAttachemntParameteriv.
276 // case GL_DELETE_STATUS:
278 case GL_COMPILE_STATUS
:
280 // Already defined under glGetFramebufferAttachemntParameteriv.
281 // case GL_INFO_LOG_LENGTH:
283 case GL_SHADER_SOURCE_LENGTH
:
285 case GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE
:
288 // -- glGetTexParameterfv, glGetTexParameteriv
289 case GL_TEXTURE_MAG_FILTER
:
291 case GL_TEXTURE_MIN_FILTER
:
293 case GL_TEXTURE_WRAP_S
:
295 case GL_TEXTURE_WRAP_T
:
297 case GL_TEXTURE_MAX_ANISOTROPY_EXT
:
300 // -- glGetVertexAttribfv, glGetVertexAttribiv
301 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING
:
303 case GL_VERTEX_ATTRIB_ARRAY_ENABLED
:
305 case GL_VERTEX_ATTRIB_ARRAY_SIZE
:
307 case GL_VERTEX_ATTRIB_ARRAY_STRIDE
:
309 case GL_VERTEX_ATTRIB_ARRAY_TYPE
:
311 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED
:
313 case GL_CURRENT_VERTEX_ATTRIB
:
316 // -- glHint with GL_OES_standard_derivatives
317 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES
:
328 // Return the number of elements per group of a specified format.
329 int ElementsPerGroup(int format
, int type
) {
331 case GL_UNSIGNED_SHORT_5_6_5
:
332 case GL_UNSIGNED_SHORT_4_4_4_4
:
333 case GL_UNSIGNED_SHORT_5_5_5_1
:
334 case GL_UNSIGNED_INT_24_8_OES
:
343 case GL_LUMINANCE_ALPHA
:
350 case GL_DEPTH_COMPONENT
:
351 case GL_DEPTH_COMPONENT24_OES
:
352 case GL_DEPTH_COMPONENT32_OES
:
353 case GL_DEPTH_COMPONENT16
:
354 case GL_DEPTH24_STENCIL8_OES
:
355 case GL_DEPTH_STENCIL_OES
:
362 // Return the number of bytes per element, based on the element type.
363 int BytesPerElement(int type
) {
366 case GL_UNSIGNED_INT_24_8_OES
:
367 case GL_UNSIGNED_INT
:
369 case GL_HALF_FLOAT_OES
:
370 case GL_UNSIGNED_SHORT
:
372 case GL_UNSIGNED_SHORT_5_6_5
:
373 case GL_UNSIGNED_SHORT_4_4_4_4
:
374 case GL_UNSIGNED_SHORT_5_5_5_1
:
376 case GL_UNSIGNED_BYTE
:
384 } // anonymous namespace
386 uint32
GLES2Util::ComputeImageGroupSize(int format
, int type
) {
387 return BytesPerElement(type
) * ElementsPerGroup(format
, type
);
390 bool GLES2Util::ComputeImagePaddedRowSize(
391 int width
, int format
, int type
, int unpack_alignment
,
392 uint32
* padded_row_size
) {
393 uint32 bytes_per_group
= ComputeImageGroupSize(format
, type
);
394 uint32 unpadded_row_size
;
395 if (!SafeMultiplyUint32(width
, bytes_per_group
, &unpadded_row_size
)) {
399 if (!SafeAddUint32(unpadded_row_size
, unpack_alignment
- 1, &temp
)) {
402 *padded_row_size
= (temp
/ unpack_alignment
) * unpack_alignment
;
406 // Returns the amount of data glTexImage2D or glTexSubImage2D will access.
407 bool GLES2Util::ComputeImageDataSizes(
408 int width
, int height
, int format
, int type
, int unpack_alignment
,
409 uint32
* size
, uint32
* ret_unpadded_row_size
, uint32
* ret_padded_row_size
) {
410 uint32 bytes_per_group
= ComputeImageGroupSize(format
, type
);
412 if (!SafeMultiplyUint32(width
, bytes_per_group
, &row_size
)) {
417 if (!SafeAddUint32(row_size
, unpack_alignment
- 1, &temp
)) {
420 uint32 padded_row_size
= (temp
/ unpack_alignment
) * unpack_alignment
;
421 uint32 size_of_all_but_last_row
;
422 if (!SafeMultiplyUint32((height
- 1), padded_row_size
,
423 &size_of_all_but_last_row
)) {
426 if (!SafeAddUint32(size_of_all_but_last_row
, row_size
, size
)) {
429 if (ret_padded_row_size
) {
430 *ret_padded_row_size
= padded_row_size
;
433 if (!SafeMultiplyUint32(height
, row_size
, size
)) {
436 if (ret_padded_row_size
) {
437 *ret_padded_row_size
= row_size
;
440 if (ret_unpadded_row_size
) {
441 *ret_unpadded_row_size
= row_size
;
447 size_t GLES2Util::RenderbufferBytesPerPixel(int format
) {
449 case GL_STENCIL_INDEX8
:
454 case GL_DEPTH_COMPONENT16
:
458 case GL_DEPTH24_STENCIL8_OES
:
461 case GL_DEPTH_COMPONENT24_OES
:
468 uint32
GLES2Util::GetGLDataTypeSizeForUniforms(int type
) {
471 return sizeof(GLfloat
); // NOLINT
473 return sizeof(GLfloat
) * 2; // NOLINT
475 return sizeof(GLfloat
) * 3; // NOLINT
477 return sizeof(GLfloat
) * 4; // NOLINT
479 return sizeof(GLint
); // NOLINT
481 return sizeof(GLint
) * 2; // NOLINT
483 return sizeof(GLint
) * 3; // NOLINT
485 return sizeof(GLint
) * 4; // NOLINT
487 return sizeof(GLint
); // NOLINT
489 return sizeof(GLint
) * 2; // NOLINT
491 return sizeof(GLint
) * 3; // NOLINT
493 return sizeof(GLint
) * 4; // NOLINT
495 return sizeof(GLfloat
) * 2 * 2; // NOLINT
497 return sizeof(GLfloat
) * 3 * 3; // NOLINT
499 return sizeof(GLfloat
) * 4 * 4; // NOLINT
501 return sizeof(GLint
); // NOLINT
502 case GL_SAMPLER_2D_RECT_ARB
:
503 return sizeof(GLint
); // NOLINT
504 case GL_SAMPLER_CUBE
:
505 return sizeof(GLint
); // NOLINT
506 case GL_SAMPLER_EXTERNAL_OES
:
507 return sizeof(GLint
); // NOLINT
513 size_t GLES2Util::GetGLTypeSizeForTexturesAndBuffers(uint32 type
) {
516 return sizeof(GLbyte
); // NOLINT
517 case GL_UNSIGNED_BYTE
:
518 return sizeof(GLubyte
); // NOLINT
520 return sizeof(GLshort
); // NOLINT
521 case GL_UNSIGNED_SHORT
:
522 return sizeof(GLushort
); // NOLINT
524 return sizeof(GLint
); // NOLINT
525 case GL_UNSIGNED_INT
:
526 return sizeof(GLuint
); // NOLINT
528 return sizeof(GLfloat
); // NOLINT
530 return sizeof(GLfixed
); // NOLINT
536 uint32
GLES2Util::GLErrorToErrorBit(uint32 error
) {
538 case GL_INVALID_ENUM
:
539 return gl_error_bit::kInvalidEnum
;
540 case GL_INVALID_VALUE
:
541 return gl_error_bit::kInvalidValue
;
542 case GL_INVALID_OPERATION
:
543 return gl_error_bit::kInvalidOperation
;
544 case GL_OUT_OF_MEMORY
:
545 return gl_error_bit::kOutOfMemory
;
546 case GL_INVALID_FRAMEBUFFER_OPERATION
:
547 return gl_error_bit::kInvalidFrameBufferOperation
;
550 return gl_error_bit::kNoError
;
554 uint32
GLES2Util::GLErrorBitToGLError(uint32 error_bit
) {
556 case gl_error_bit::kInvalidEnum
:
557 return GL_INVALID_ENUM
;
558 case gl_error_bit::kInvalidValue
:
559 return GL_INVALID_VALUE
;
560 case gl_error_bit::kInvalidOperation
:
561 return GL_INVALID_OPERATION
;
562 case gl_error_bit::kOutOfMemory
:
563 return GL_OUT_OF_MEMORY
;
564 case gl_error_bit::kInvalidFrameBufferOperation
:
565 return GL_INVALID_FRAMEBUFFER_OPERATION
;
572 uint32
GLES2Util::IndexToGLFaceTarget(int index
) {
573 static uint32 faces
[] = {
574 GL_TEXTURE_CUBE_MAP_POSITIVE_X
,
575 GL_TEXTURE_CUBE_MAP_NEGATIVE_X
,
576 GL_TEXTURE_CUBE_MAP_POSITIVE_Y
,
577 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
,
578 GL_TEXTURE_CUBE_MAP_POSITIVE_Z
,
579 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
,
584 uint32
GLES2Util::GetChannelsForFormat(int format
) {
587 case GL_ALPHA16F_EXT
:
588 case GL_ALPHA32F_EXT
:
592 case GL_LUMINANCE_ALPHA
:
609 case GL_DEPTH_COMPONENT32_OES
:
610 case GL_DEPTH_COMPONENT24_OES
:
611 case GL_DEPTH_COMPONENT16
:
612 case GL_DEPTH_COMPONENT
:
614 case GL_STENCIL_INDEX8
:
616 case GL_DEPTH_STENCIL_OES
:
617 case GL_DEPTH24_STENCIL8_OES
:
618 return kDepth
| kStencil
;
624 uint32
GLES2Util::GetChannelsNeededForAttachmentType(int type
) {
626 case GL_COLOR_ATTACHMENT0
:
628 case GL_DEPTH_ATTACHMENT
:
630 case GL_STENCIL_ATTACHMENT
:
637 std::string
GLES2Util::GetStringEnum(uint32 value
) {
638 const EnumToString
* entry
= enum_to_string_table_
;
639 const EnumToString
* end
= entry
+ enum_to_string_table_len_
;
640 for (;entry
< end
; ++entry
) {
641 if (value
== entry
->value
) {
646 sprintf(buffer
, (value
< 0x10000) ? "0x%04x" : "0x%08x", value
);
650 std::string
GLES2Util::GetStringError(uint32 value
) {
651 static EnumToString string_table
[] = {
652 { GL_NONE
, "GL_NONE" },
654 return GLES2Util::GetQualifiedEnumString(
655 string_table
, arraysize(string_table
), value
);
658 std::string
GLES2Util::GetStringBool(uint32 value
) {
659 return value
? "GL_TRUE" : "GL_FALSE";
662 std::string
GLES2Util::GetQualifiedEnumString(
663 const EnumToString
* table
, size_t count
, uint32 value
) {
664 for (const EnumToString
* end
= table
+ count
; table
< end
; ++table
) {
665 if (table
->value
== value
) {
669 return GetStringEnum(value
);
672 bool GLES2Util::ParseUniformName(
673 const std::string
& name
,
676 bool* getting_array
) {
677 bool getting_array_location
= false;
678 size_t open_pos
= std::string::npos
;
680 if (name
[name
.size() - 1] == ']') {
681 if (name
.size() < 3) {
684 open_pos
= name
.find_last_of('[');
685 if (open_pos
== std::string::npos
||
686 open_pos
>= name
.size() - 2) {
689 size_t last
= name
.size() - 1;
690 for (size_t pos
= open_pos
+ 1; pos
< last
; ++pos
) {
691 int8 digit
= name
[pos
] - '0';
692 if (digit
< 0 || digit
> 9) {
695 index
= index
* 10 + digit
;
697 getting_array_location
= true;
699 *getting_array
= getting_array_location
;
700 *element_index
= index
;
701 *array_pos
= open_pos
;
705 ContextCreationAttribParser::ContextCreationAttribParser()
714 buffer_preserved_(true),
715 share_resources_(false),
716 bind_generates_resource_(true) {
719 bool ContextCreationAttribParser::Parse(const std::vector
<int32
>& attribs
) {
721 const int32 EGL_ALPHA_SIZE
= 0x3021;
722 const int32 EGL_BLUE_SIZE
= 0x3022;
723 const int32 EGL_GREEN_SIZE
= 0x3023;
724 const int32 EGL_RED_SIZE
= 0x3024;
725 const int32 EGL_DEPTH_SIZE
= 0x3025;
726 const int32 EGL_STENCIL_SIZE
= 0x3026;
727 const int32 EGL_SAMPLES
= 0x3031;
728 const int32 EGL_SAMPLE_BUFFERS
= 0x3032;
729 const int32 EGL_NONE
= 0x3038;
730 const int32 EGL_SWAP_BEHAVIOR
= 0x3093;
731 const int32 EGL_BUFFER_PRESERVED
= 0x3094;
734 const int32 SHARE_RESOURCES
= 0x10000;
735 const int32 BIND_GENERATES_RESOURCES
= 0x10001;
737 for (size_t i
= 0; i
< attribs
.size(); i
+= 2) {
738 const int32 attrib
= attribs
[i
];
739 if (i
+ 1 >= attribs
.size()) {
740 if (attrib
== EGL_NONE
) {
744 GPU_DLOG(ERROR
) << "Missing value after context creation attribute: "
749 const int32 value
= attribs
[i
+1];
766 case EGL_STENCIL_SIZE
:
767 stencil_size_
= value
;
772 case EGL_SAMPLE_BUFFERS
:
773 sample_buffers_
= value
;
775 case EGL_SWAP_BEHAVIOR
:
776 buffer_preserved_
= value
== EGL_BUFFER_PRESERVED
;
778 case SHARE_RESOURCES
:
779 share_resources_
= value
!= 0;
781 case BIND_GENERATES_RESOURCES
:
782 bind_generates_resource_
= value
!= 0;
785 // Terminate list, even if more attributes.
788 GPU_DLOG(ERROR
) << "Invalid context creation attribute: " << attrib
;
796 #include "../common/gles2_cmd_utils_implementation_autogen.h"