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>
11 #include <GLES2/gl2extchromium.h>
12 #include <GLES3/gl3.h>
14 #include "base/numerics/safe_math.h"
15 #include "gpu/command_buffer/common/gles2_cmd_format.h"
16 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
21 namespace gl_error_bit
{
24 kInvalidEnum
= (1 << 0),
25 kInvalidValue
= (1 << 1),
26 kInvalidOperation
= (1 << 2),
27 kOutOfMemory
= (1 << 3),
28 kInvalidFrameBufferOperation
= (1 << 4),
29 kContextLost
= (1 << 5)
33 int GLES2Util::GLGetNumValuesReturned(int id
) const {
35 // -- glGetBooleanv, glGetFloatv, glGetIntergerv
36 case GL_ACTIVE_TEXTURE
:
38 case GL_ALIASED_LINE_WIDTH_RANGE
:
40 case GL_ALIASED_POINT_SIZE_RANGE
:
44 case GL_ARRAY_BUFFER_BINDING
:
50 case GL_BLEND_DST_ALPHA
:
52 case GL_BLEND_DST_RGB
:
54 case GL_BLEND_EQUATION_ALPHA
:
56 case GL_BLEND_EQUATION_RGB
:
58 case GL_BLEND_SRC_ALPHA
:
60 case GL_BLEND_SRC_RGB
:
64 case GL_COLOR_CLEAR_VALUE
:
66 case GL_COLOR_WRITEMASK
:
68 case GL_COMPRESSED_TEXTURE_FORMATS
:
69 return num_compressed_texture_formats_
;
72 case GL_CULL_FACE_MODE
:
74 case GL_CURRENT_PROGRAM
:
78 case GL_DEPTH_CLEAR_VALUE
:
86 case GL_DEPTH_WRITEMASK
:
90 case GL_ELEMENT_ARRAY_BUFFER_BINDING
:
92 case GL_FRAMEBUFFER_BINDING
:
96 case GL_GENERATE_MIPMAP_HINT
:
100 case GL_IMPLEMENTATION_COLOR_READ_FORMAT
:
102 case GL_IMPLEMENTATION_COLOR_READ_TYPE
:
106 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS
:
108 case GL_MAX_CUBE_MAP_TEXTURE_SIZE
:
110 case GL_MAX_FRAGMENT_UNIFORM_VECTORS
:
112 case GL_MAX_RENDERBUFFER_SIZE
:
114 case GL_MAX_TEXTURE_IMAGE_UNITS
:
116 case GL_MAX_TEXTURE_SIZE
:
118 case GL_MAX_VARYING_VECTORS
:
120 case GL_MAX_VERTEX_ATTRIBS
:
122 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS
:
124 case GL_MAX_VERTEX_UNIFORM_VECTORS
:
126 case GL_MAX_VIEWPORT_DIMS
:
128 case GL_NUM_COMPRESSED_TEXTURE_FORMATS
:
130 case GL_NUM_SHADER_BINARY_FORMATS
:
132 case GL_PACK_ALIGNMENT
:
134 case GL_PACK_REVERSE_ROW_ORDER_ANGLE
:
136 case GL_POLYGON_OFFSET_FACTOR
:
138 case GL_POLYGON_OFFSET_FILL
:
140 case GL_POLYGON_OFFSET_UNITS
:
144 case GL_RENDERBUFFER_BINDING
:
146 case GL_SAMPLE_BUFFERS
:
148 case GL_SAMPLE_COVERAGE_INVERT
:
150 case GL_SAMPLE_COVERAGE_VALUE
:
156 case GL_SCISSOR_TEST
:
158 case GL_SHADER_BINARY_FORMATS
:
159 return num_shader_binary_formats_
;
160 case GL_SHADER_COMPILER
:
162 case GL_STENCIL_BACK_FAIL
:
164 case GL_STENCIL_BACK_FUNC
:
166 case GL_STENCIL_BACK_PASS_DEPTH_FAIL
:
168 case GL_STENCIL_BACK_PASS_DEPTH_PASS
:
170 case GL_STENCIL_BACK_REF
:
172 case GL_STENCIL_BACK_VALUE_MASK
:
174 case GL_STENCIL_BACK_WRITEMASK
:
176 case GL_STENCIL_BITS
:
178 case GL_STENCIL_CLEAR_VALUE
:
180 case GL_STENCIL_FAIL
:
182 case GL_STENCIL_FUNC
:
184 case GL_STENCIL_PASS_DEPTH_FAIL
:
186 case GL_STENCIL_PASS_DEPTH_PASS
:
190 case GL_STENCIL_TEST
:
192 case GL_STENCIL_VALUE_MASK
:
194 case GL_STENCIL_WRITEMASK
:
196 case GL_SUBPIXEL_BITS
:
198 case GL_TEXTURE_BINDING_2D
:
200 case GL_TEXTURE_BINDING_CUBE_MAP
:
202 case GL_TEXTURE_BINDING_EXTERNAL_OES
:
204 case GL_TEXTURE_BINDING_RECTANGLE_ARB
:
206 case GL_UNPACK_ALIGNMENT
:
212 case GL_COPY_READ_BUFFER_BINDING
:
214 case GL_COPY_WRITE_BUFFER_BINDING
:
216 case GL_PIXEL_PACK_BUFFER_BINDING
:
218 case GL_PIXEL_UNPACK_BUFFER_BINDING
:
220 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING
:
222 case GL_UNIFORM_BUFFER_BINDING
:
225 // -- glGetBooleanv, glGetFloatv, glGetIntergerv with
226 // GL_CHROMIUM_framebuffer_multisample
227 case GL_MAX_SAMPLES_EXT
:
229 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT
:
232 // -- glGetBufferParameteriv
235 case GL_BUFFER_USAGE
:
238 // -- glGetFramebufferAttachmentParameteriv
239 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE
:
241 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME
:
243 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL
:
245 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE
:
247 // -- glGetFramebufferAttachmentParameteriv with
248 // GL_EXT_multisampled_render_to_texture
249 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_SAMPLES_EXT
:
251 // -- glGetFramebufferAttachmentParameteriv with
253 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING_EXT
:
257 case GL_DELETE_STATUS
:
261 case GL_VALIDATE_STATUS
:
263 case GL_INFO_LOG_LENGTH
:
265 case GL_ATTACHED_SHADERS
:
267 case GL_ACTIVE_ATTRIBUTES
:
269 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
271 case GL_ACTIVE_UNIFORMS
:
273 case GL_ACTIVE_UNIFORM_MAX_LENGTH
:
276 // -- glGetRenderbufferAttachmentParameteriv
277 case GL_RENDERBUFFER_WIDTH
:
279 case GL_RENDERBUFFER_HEIGHT
:
281 case GL_RENDERBUFFER_INTERNAL_FORMAT
:
283 case GL_RENDERBUFFER_RED_SIZE
:
285 case GL_RENDERBUFFER_GREEN_SIZE
:
287 case GL_RENDERBUFFER_BLUE_SIZE
:
289 case GL_RENDERBUFFER_ALPHA_SIZE
:
291 case GL_RENDERBUFFER_DEPTH_SIZE
:
293 case GL_RENDERBUFFER_STENCIL_SIZE
:
295 // -- glGetRenderbufferAttachmentParameteriv with
296 // GL_EXT_multisampled_render_to_texture
297 case GL_RENDERBUFFER_SAMPLES_EXT
:
303 // Already defined under glGetFramebufferAttachemntParameteriv.
304 // case GL_DELETE_STATUS:
306 case GL_COMPILE_STATUS
:
308 // Already defined under glGetFramebufferAttachemntParameteriv.
309 // case GL_INFO_LOG_LENGTH:
311 case GL_SHADER_SOURCE_LENGTH
:
313 case GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE
:
316 // -- glGetTexParameterfv, glGetTexParameteriv
317 case GL_TEXTURE_MAG_FILTER
:
319 case GL_TEXTURE_MIN_FILTER
:
321 case GL_TEXTURE_WRAP_R
:
323 case GL_TEXTURE_WRAP_S
:
325 case GL_TEXTURE_WRAP_T
:
327 case GL_TEXTURE_COMPARE_FUNC
:
329 case GL_TEXTURE_COMPARE_MODE
:
331 case GL_TEXTURE_MAX_LOD
:
333 case GL_TEXTURE_MIN_LOD
:
335 case GL_TEXTURE_BASE_LEVEL
:
337 case GL_TEXTURE_MAX_LEVEL
:
339 case GL_TEXTURE_IMMUTABLE_FORMAT
:
341 case GL_TEXTURE_IMMUTABLE_LEVELS
:
343 case GL_TEXTURE_MAX_ANISOTROPY_EXT
:
346 // -- glGetVertexAttrib
347 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING
:
349 case GL_VERTEX_ATTRIB_ARRAY_ENABLED
:
351 case GL_VERTEX_ATTRIB_ARRAY_SIZE
:
353 case GL_VERTEX_ATTRIB_ARRAY_STRIDE
:
355 case GL_VERTEX_ATTRIB_ARRAY_TYPE
:
357 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED
:
359 case GL_CURRENT_VERTEX_ATTRIB
:
361 case GL_VERTEX_ATTRIB_ARRAY_INTEGER
:
363 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR
:
371 case GL_SYNC_CONDITION
:
376 // -- glHint with GL_OES_standard_derivatives
377 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES
:
380 // Chromium internal bind_generates_resource query
381 case GL_BIND_GENERATES_RESOURCE_CHROMIUM
:
392 // Return the number of elements per group of a specified format.
393 int ElementsPerGroup(int format
, int type
) {
395 case GL_UNSIGNED_SHORT_5_6_5
:
396 case GL_UNSIGNED_SHORT_4_4_4_4
:
397 case GL_UNSIGNED_SHORT_5_5_5_1
:
398 case GL_UNSIGNED_INT_24_8_OES
:
399 case GL_UNSIGNED_INT_2_10_10_10_REV
:
400 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
401 case GL_UNSIGNED_INT_5_9_9_9_REV
:
402 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
413 case GL_LUMINANCE_ALPHA
:
418 case GL_RGBA_INTEGER
:
420 case GL_SRGB_ALPHA_EXT
:
424 case GL_DEPTH_COMPONENT
:
425 case GL_DEPTH_COMPONENT24_OES
:
426 case GL_DEPTH_COMPONENT32_OES
:
427 case GL_DEPTH_COMPONENT16
:
428 case GL_DEPTH24_STENCIL8_OES
:
429 case GL_DEPTH_STENCIL_OES
:
438 // Return the number of bytes per element, based on the element type.
439 int BytesPerElement(int type
) {
441 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
444 case GL_UNSIGNED_INT_24_8_OES
:
445 case GL_UNSIGNED_INT
:
446 case GL_UNSIGNED_INT_2_10_10_10_REV
:
447 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
448 case GL_UNSIGNED_INT_5_9_9_9_REV
:
450 case GL_HALF_FLOAT_OES
:
451 case GL_UNSIGNED_SHORT
:
453 case GL_UNSIGNED_SHORT_5_6_5
:
454 case GL_UNSIGNED_SHORT_4_4_4_4
:
455 case GL_UNSIGNED_SHORT_5_5_5_1
:
457 case GL_UNSIGNED_BYTE
:
465 } // anonymous namespace
467 uint32
GLES2Util::ComputeImageGroupSize(int format
, int type
) {
468 int bytes_per_element
= BytesPerElement(type
);
469 DCHECK_GE(8, bytes_per_element
);
470 int elements_per_group
= ElementsPerGroup(format
, type
);
471 DCHECK_GE(4, elements_per_group
);
472 return bytes_per_element
* elements_per_group
;
475 bool GLES2Util::ComputeImagePaddedRowSize(
476 int width
, int format
, int type
, int unpack_alignment
,
477 uint32
* padded_row_size
) {
478 DCHECK(unpack_alignment
== 1 || unpack_alignment
== 2 ||
479 unpack_alignment
== 4 || unpack_alignment
== 8);
480 uint32 bytes_per_group
= ComputeImageGroupSize(format
, type
);
481 uint32 unpadded_row_size
;
482 if (!SafeMultiplyUint32(width
, bytes_per_group
, &unpadded_row_size
)) {
486 if (!SafeAddUint32(unpadded_row_size
, unpack_alignment
- 1, &temp
)) {
489 *padded_row_size
= (temp
/ unpack_alignment
) * unpack_alignment
;
493 // Returns the amount of data glTexImage*D or glTexSubImage*D will access.
494 bool GLES2Util::ComputeImageDataSizes(
495 int width
, int height
, int depth
, int format
, int type
,
496 int unpack_alignment
, uint32
* size
, uint32
* ret_unpadded_row_size
,
497 uint32
* ret_padded_row_size
) {
498 DCHECK(unpack_alignment
== 1 || unpack_alignment
== 2 ||
499 unpack_alignment
== 4 || unpack_alignment
== 8);
500 uint32 bytes_per_group
= ComputeImageGroupSize(format
, type
);
502 if (!SafeMultiplyUint32(width
, bytes_per_group
, &row_size
)) {
506 if (!SafeMultiplyUint32(height
, depth
, &num_of_rows
)) {
509 if (num_of_rows
> 1) {
511 if (!SafeAddUint32(row_size
, unpack_alignment
- 1, &temp
)) {
514 uint32 padded_row_size
= (temp
/ unpack_alignment
) * unpack_alignment
;
515 uint32 size_of_all_but_last_row
;
516 if (!SafeMultiplyUint32((num_of_rows
- 1), padded_row_size
,
517 &size_of_all_but_last_row
)) {
520 if (!SafeAddUint32(size_of_all_but_last_row
, row_size
, size
)) {
523 if (ret_padded_row_size
) {
524 *ret_padded_row_size
= padded_row_size
;
528 if (ret_padded_row_size
) {
529 *ret_padded_row_size
= row_size
;
532 if (ret_unpadded_row_size
) {
533 *ret_unpadded_row_size
= row_size
;
539 size_t GLES2Util::RenderbufferBytesPerPixel(int format
) {
541 case GL_STENCIL_INDEX8
:
546 case GL_DEPTH_COMPONENT16
:
550 case GL_DEPTH24_STENCIL8_OES
:
553 case GL_DEPTH_COMPONENT24_OES
:
560 uint32
GLES2Util::GetElementSizeForUniformType(int type
) {
569 return sizeof(GLfloat
);
579 case GL_SAMPLER_CUBE
:
580 case GL_SAMPLER_2D_RECT_ARB
: // extension.
581 case GL_SAMPLER_EXTERNAL_OES
: // extension.
582 return sizeof(GLint
);
585 case GL_UNSIGNED_INT
:
586 case GL_UNSIGNED_INT_VEC2
:
587 case GL_UNSIGNED_INT_VEC3
:
588 case GL_UNSIGNED_INT_VEC4
:
589 return sizeof(GLuint
);
591 case GL_SAMPLER_2D_SHADOW
:
592 case GL_SAMPLER_2D_ARRAY
:
593 case GL_SAMPLER_2D_ARRAY_SHADOW
:
594 case GL_SAMPLER_CUBE_SHADOW
:
595 case GL_INT_SAMPLER_2D
:
596 case GL_INT_SAMPLER_3D
:
597 case GL_INT_SAMPLER_CUBE
:
598 case GL_INT_SAMPLER_2D_ARRAY
:
599 case GL_UNSIGNED_INT_SAMPLER_2D
:
600 case GL_UNSIGNED_INT_SAMPLER_3D
:
601 case GL_UNSIGNED_INT_SAMPLER_CUBE
:
602 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY
:
603 return sizeof(GLint
);
604 case GL_FLOAT_MAT2x3
:
605 case GL_FLOAT_MAT3x2
:
606 case GL_FLOAT_MAT2x4
:
607 case GL_FLOAT_MAT4x2
:
608 case GL_FLOAT_MAT3x4
:
609 case GL_FLOAT_MAT4x3
:
610 return sizeof(GLfloat
);
617 uint32
GLES2Util::GetElementCountForUniformType(int type
) {
623 case GL_SAMPLER_CUBE
:
624 case GL_SAMPLER_2D_RECT_ARB
: // extension.
625 case GL_SAMPLER_EXTERNAL_OES
: // extension.
646 case GL_UNSIGNED_INT
:
648 case GL_SAMPLER_2D_SHADOW
:
649 case GL_SAMPLER_2D_ARRAY
:
650 case GL_SAMPLER_2D_ARRAY_SHADOW
:
651 case GL_SAMPLER_CUBE_SHADOW
:
652 case GL_INT_SAMPLER_2D
:
653 case GL_INT_SAMPLER_3D
:
654 case GL_INT_SAMPLER_CUBE
:
655 case GL_INT_SAMPLER_2D_ARRAY
:
656 case GL_UNSIGNED_INT_SAMPLER_2D
:
657 case GL_UNSIGNED_INT_SAMPLER_3D
:
658 case GL_UNSIGNED_INT_SAMPLER_CUBE
:
659 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY
:
661 case GL_UNSIGNED_INT_VEC2
:
663 case GL_UNSIGNED_INT_VEC3
:
665 case GL_UNSIGNED_INT_VEC4
:
667 case GL_FLOAT_MAT2x3
:
668 case GL_FLOAT_MAT3x2
:
670 case GL_FLOAT_MAT2x4
:
671 case GL_FLOAT_MAT4x2
:
673 case GL_FLOAT_MAT3x4
:
674 case GL_FLOAT_MAT4x3
:
682 size_t GLES2Util::GetGLTypeSizeForTexturesAndBuffers(uint32 type
) {
685 return sizeof(GLbyte
); // NOLINT
686 case GL_UNSIGNED_BYTE
:
687 return sizeof(GLubyte
); // NOLINT
689 return sizeof(GLshort
); // NOLINT
690 case GL_UNSIGNED_SHORT
:
691 return sizeof(GLushort
); // NOLINT
693 return sizeof(GLint
); // NOLINT
694 case GL_UNSIGNED_INT
:
695 return sizeof(GLuint
); // NOLINT
697 return sizeof(GLfloat
); // NOLINT
699 return sizeof(GLfixed
); // NOLINT
705 size_t GLES2Util::GetGLTypeSizeForPathCoordType(uint32 type
) {
708 return sizeof(GLbyte
); // NOLINT
709 case GL_UNSIGNED_BYTE
:
710 return sizeof(GLubyte
); // NOLINT
712 return sizeof(GLshort
); // NOLINT
713 case GL_UNSIGNED_SHORT
:
714 return sizeof(GLushort
); // NOLINT
716 return sizeof(GLfloat
); // NOLINT
722 uint32
GLES2Util::GLErrorToErrorBit(uint32 error
) {
724 case GL_INVALID_ENUM
:
725 return gl_error_bit::kInvalidEnum
;
726 case GL_INVALID_VALUE
:
727 return gl_error_bit::kInvalidValue
;
728 case GL_INVALID_OPERATION
:
729 return gl_error_bit::kInvalidOperation
;
730 case GL_OUT_OF_MEMORY
:
731 return gl_error_bit::kOutOfMemory
;
732 case GL_INVALID_FRAMEBUFFER_OPERATION
:
733 return gl_error_bit::kInvalidFrameBufferOperation
;
734 case GL_CONTEXT_LOST_KHR
:
735 return gl_error_bit::kContextLost
;
738 return gl_error_bit::kNoError
;
742 uint32
GLES2Util::GLErrorBitToGLError(uint32 error_bit
) {
744 case gl_error_bit::kInvalidEnum
:
745 return GL_INVALID_ENUM
;
746 case gl_error_bit::kInvalidValue
:
747 return GL_INVALID_VALUE
;
748 case gl_error_bit::kInvalidOperation
:
749 return GL_INVALID_OPERATION
;
750 case gl_error_bit::kOutOfMemory
:
751 return GL_OUT_OF_MEMORY
;
752 case gl_error_bit::kInvalidFrameBufferOperation
:
753 return GL_INVALID_FRAMEBUFFER_OPERATION
;
754 case gl_error_bit::kContextLost
:
755 return GL_CONTEXT_LOST_KHR
;
762 uint32
GLES2Util::IndexToGLFaceTarget(int index
) {
763 static uint32 faces
[] = {
764 GL_TEXTURE_CUBE_MAP_POSITIVE_X
,
765 GL_TEXTURE_CUBE_MAP_NEGATIVE_X
,
766 GL_TEXTURE_CUBE_MAP_POSITIVE_Y
,
767 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
,
768 GL_TEXTURE_CUBE_MAP_POSITIVE_Z
,
769 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
,
774 size_t GLES2Util::GLTargetToFaceIndex(uint32 target
) {
777 case GL_TEXTURE_EXTERNAL_OES
:
778 case GL_TEXTURE_RECTANGLE_ARB
:
780 case GL_TEXTURE_2D_ARRAY
:
782 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
784 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
786 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
788 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
790 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
792 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
800 uint32
GLES2Util::GetPreferredGLReadPixelsFormat(uint32 internal_format
) {
801 switch (internal_format
) {
813 uint32
GLES2Util::GetPreferredGLReadPixelsType(
814 uint32 internal_format
, uint32 texture_type
) {
815 switch (internal_format
) {
821 return GL_HALF_FLOAT_OES
;
824 // Unsized internal format, check the type
825 switch (texture_type
) {
827 case GL_HALF_FLOAT_OES
:
830 return GL_UNSIGNED_BYTE
;
833 return GL_UNSIGNED_BYTE
;
837 uint32
GLES2Util::GetChannelsForFormat(int format
) {
840 case GL_ALPHA16F_EXT
:
841 case GL_ALPHA32F_EXT
:
845 case GL_LUMINANCE_ALPHA
:
855 case GL_R11F_G11F_B10F
:
872 case GL_SRGB_ALPHA_EXT
:
873 case GL_SRGB8_ALPHA8_EXT
:
884 case GL_DEPTH_COMPONENT32_OES
:
885 case GL_DEPTH_COMPONENT24_OES
:
886 case GL_DEPTH_COMPONENT16
:
887 case GL_DEPTH_COMPONENT
:
888 case GL_DEPTH_COMPONENT32F
:
890 case GL_STENCIL_INDEX8
:
892 case GL_DEPTH_STENCIL_OES
:
893 case GL_DEPTH24_STENCIL8_OES
:
894 case GL_DEPTH32F_STENCIL8
:
895 return kDepth
| kStencil
;
919 return kRed
| kGreen
;
925 uint32
GLES2Util::GetChannelsNeededForAttachmentType(
926 int type
, uint32 max_color_attachments
) {
928 case GL_DEPTH_ATTACHMENT
:
930 case GL_STENCIL_ATTACHMENT
:
933 if (type
>= GL_COLOR_ATTACHMENT0
&&
934 type
< static_cast<int>(
935 GL_COLOR_ATTACHMENT0
+ max_color_attachments
)) {
942 std::string
GLES2Util::GetStringEnum(uint32 value
) {
943 const EnumToString
* entry
= enum_to_string_table_
;
944 const EnumToString
* end
= entry
+ enum_to_string_table_len_
;
945 for (;entry
< end
; ++entry
) {
946 if (value
== entry
->value
) {
950 std::stringstream ss
;
952 ss
.width(value
< 0x10000 ? 4 : 8);
953 ss
<< std::hex
<< value
;
954 return "0x" + ss
.str();
957 std::string
GLES2Util::GetStringError(uint32 value
) {
958 static EnumToString string_table
[] = {
959 { GL_NONE
, "GL_NONE" },
961 return GLES2Util::GetQualifiedEnumString(
962 string_table
, arraysize(string_table
), value
);
965 std::string
GLES2Util::GetStringBool(uint32 value
) {
966 return value
? "GL_TRUE" : "GL_FALSE";
969 std::string
GLES2Util::GetQualifiedEnumString(
970 const EnumToString
* table
, size_t count
, uint32 value
) {
971 for (const EnumToString
* end
= table
+ count
; table
< end
; ++table
) {
972 if (table
->value
== value
) {
976 return GetStringEnum(value
);
979 bool GLES2Util::ParseUniformName(
980 const std::string
& name
,
983 bool* getting_array
) {
986 bool getting_array_location
= false;
987 size_t open_pos
= std::string::npos
;
988 base::CheckedNumeric
<int> index
= 0;
989 if (name
[name
.size() - 1] == ']') {
990 if (name
.size() < 3) {
993 open_pos
= name
.find_last_of('[');
994 if (open_pos
== std::string::npos
||
995 open_pos
>= name
.size() - 2) {
998 size_t last
= name
.size() - 1;
999 for (size_t pos
= open_pos
+ 1; pos
< last
; ++pos
) {
1000 int8 digit
= name
[pos
] - '0';
1001 if (digit
< 0 || digit
> 9) {
1004 index
= index
* 10 + digit
;
1006 if (!index
.IsValid()) {
1009 getting_array_location
= true;
1011 *getting_array
= getting_array_location
;
1012 *element_index
= index
.ValueOrDie();
1013 *array_pos
= open_pos
;
1017 size_t GLES2Util::CalcClearBufferivDataCount(int buffer
) {
1028 size_t GLES2Util::CalcClearBufferfvDataCount(int buffer
) {
1040 void GLES2Util::MapUint64ToTwoUint32(
1041 uint64_t v64
, uint32_t* v32_0
, uint32_t* v32_1
) {
1042 DCHECK(v32_0
&& v32_1
);
1043 *v32_0
= static_cast<uint32_t>(v64
& 0xFFFFFFFF);
1044 *v32_1
= static_cast<uint32_t>((v64
& 0xFFFFFFFF00000000) >> 32);
1048 uint64_t GLES2Util::MapTwoUint32ToUint64(uint32_t v32_0
, uint32_t v32_1
) {
1049 uint64_t v64
= v32_1
;
1050 return (v64
<< 32) | v32_0
;
1054 uint32_t GLES2Util::MapBufferTargetToBindingEnum(uint32_t target
) {
1056 case GL_ARRAY_BUFFER
:
1057 return GL_ARRAY_BUFFER_BINDING
;
1058 case GL_COPY_READ_BUFFER
:
1059 return GL_COPY_READ_BUFFER_BINDING
;
1060 case GL_COPY_WRITE_BUFFER
:
1061 return GL_COPY_WRITE_BUFFER_BINDING
;
1062 case GL_ELEMENT_ARRAY_BUFFER
:
1063 return GL_ELEMENT_ARRAY_BUFFER_BINDING
;
1064 case GL_PIXEL_PACK_BUFFER
:
1065 return GL_PIXEL_PACK_BUFFER_BINDING
;
1066 case GL_PIXEL_UNPACK_BUFFER
:
1067 return GL_PIXEL_UNPACK_BUFFER_BINDING
;
1068 case GL_TRANSFORM_FEEDBACK_BUFFER
:
1069 return GL_TRANSFORM_FEEDBACK_BUFFER_BINDING
;
1070 case GL_UNIFORM_BUFFER
:
1071 return GL_UNIFORM_BUFFER_BINDING
;
1080 // WebGraphicsContext3DCommandBufferImpl configuration attributes. Those in
1081 // the 16-bit range are the same as used by EGL. Those outside the 16-bit range
1082 // are unique to Chromium. Attributes are matched using a closest fit algorithm.
1084 // From <EGL/egl.h>.
1085 const int32 kAlphaSize
= 0x3021; // EGL_ALPHA_SIZE
1086 const int32 kBlueSize
= 0x3022; // EGL_BLUE_SIZE
1087 const int32 kGreenSize
= 0x3023; // EGL_GREEN_SIZE
1088 const int32 kRedSize
= 0x3024; // EGL_RED_SIZE
1089 const int32 kDepthSize
= 0x3025; // EGL_DEPTH_SIZE
1090 const int32 kStencilSize
= 0x3026; // EGL_STENCIL_SIZE
1091 const int32 kSamples
= 0x3031; // EGL_SAMPLES
1092 const int32 kSampleBuffers
= 0x3032; // EGL_SAMPLE_BUFFERS
1093 const int32 kNone
= 0x3038; // EGL_NONE
1094 const int32 kSwapBehavior
= 0x3093; // EGL_SWAP_BEHAVIOR
1095 const int32 kBufferPreserved
= 0x3094; // EGL_BUFFER_PRESERVED
1096 const int32 kBufferDestroyed
= 0x3095; // EGL_BUFFER_DESTROYED
1099 const int32 kBindGeneratesResource
= 0x10000;
1100 const int32 kFailIfMajorPerfCaveat
= 0x10001;
1101 const int32 kLoseContextWhenOutOfMemory
= 0x10002;
1102 const int32 kWebGLVersion
= 0x10003;
1106 ContextCreationAttribHelper::ContextCreationAttribHelper()
1115 buffer_preserved(true),
1116 bind_generates_resource(true),
1117 fail_if_major_perf_caveat(false),
1118 lose_context_when_out_of_memory(false),
1122 void ContextCreationAttribHelper::Serialize(std::vector
<int32
>* attribs
) const {
1123 if (alpha_size
!= -1) {
1124 attribs
->push_back(kAlphaSize
);
1125 attribs
->push_back(alpha_size
);
1127 if (blue_size
!= -1) {
1128 attribs
->push_back(kBlueSize
);
1129 attribs
->push_back(blue_size
);
1131 if (green_size
!= -1) {
1132 attribs
->push_back(kGreenSize
);
1133 attribs
->push_back(green_size
);
1135 if (red_size
!= -1) {
1136 attribs
->push_back(kRedSize
);
1137 attribs
->push_back(red_size
);
1139 if (depth_size
!= -1) {
1140 attribs
->push_back(kDepthSize
);
1141 attribs
->push_back(depth_size
);
1143 if (stencil_size
!= -1) {
1144 attribs
->push_back(kStencilSize
);
1145 attribs
->push_back(stencil_size
);
1147 if (samples
!= -1) {
1148 attribs
->push_back(kSamples
);
1149 attribs
->push_back(samples
);
1151 if (sample_buffers
!= -1) {
1152 attribs
->push_back(kSampleBuffers
);
1153 attribs
->push_back(sample_buffers
);
1155 attribs
->push_back(kSwapBehavior
);
1156 attribs
->push_back(buffer_preserved
? kBufferPreserved
: kBufferDestroyed
);
1157 attribs
->push_back(kBindGeneratesResource
);
1158 attribs
->push_back(bind_generates_resource
? 1 : 0);
1159 attribs
->push_back(kFailIfMajorPerfCaveat
);
1160 attribs
->push_back(fail_if_major_perf_caveat
? 1 : 0);
1161 attribs
->push_back(kLoseContextWhenOutOfMemory
);
1162 attribs
->push_back(lose_context_when_out_of_memory
? 1 : 0);
1163 attribs
->push_back(kWebGLVersion
);
1164 attribs
->push_back(webgl_version
);
1165 attribs
->push_back(kNone
);
1168 bool ContextCreationAttribHelper::Parse(const std::vector
<int32
>& attribs
) {
1169 for (size_t i
= 0; i
< attribs
.size(); i
+= 2) {
1170 const int32 attrib
= attribs
[i
];
1171 if (i
+ 1 >= attribs
.size()) {
1172 if (attrib
== kNone
) {
1176 DLOG(ERROR
) << "Missing value after context creation attribute: "
1181 const int32 value
= attribs
[i
+1];
1199 stencil_size
= value
;
1204 case kSampleBuffers
:
1205 sample_buffers
= value
;
1208 buffer_preserved
= value
== kBufferPreserved
;
1210 case kBindGeneratesResource
:
1211 bind_generates_resource
= value
!= 0;
1213 case kFailIfMajorPerfCaveat
:
1214 fail_if_major_perf_caveat
= value
!= 0;
1216 case kLoseContextWhenOutOfMemory
:
1217 lose_context_when_out_of_memory
= value
!= 0;
1220 webgl_version
= value
;
1223 // Terminate list, even if more attributes.
1226 DLOG(ERROR
) << "Invalid context creation attribute: " << attrib
;
1234 #include "gpu/command_buffer/common/gles2_cmd_utils_implementation_autogen.h"
1236 } // namespace gles2