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
:
239 case GL_BUFFER_MAPPED
:
241 case GL_BUFFER_ACCESS_FLAGS
:
243 case GL_BUFFER_MAP_LENGTH
:
245 case GL_BUFFER_MAP_OFFSET
:
248 // -- glGetFramebufferAttachmentParameteriv
249 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE
:
251 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME
:
253 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL
:
255 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE
:
257 // -- glGetFramebufferAttachmentParameteriv with
258 // GL_EXT_multisampled_render_to_texture
259 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_SAMPLES_EXT
:
261 // -- glGetFramebufferAttachmentParameteriv with
263 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING_EXT
:
266 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
268 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
270 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
272 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
274 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
276 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
278 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
280 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER
:
284 case GL_DELETE_STATUS
:
288 case GL_VALIDATE_STATUS
:
290 case GL_INFO_LOG_LENGTH
:
292 case GL_ATTACHED_SHADERS
:
294 case GL_ACTIVE_ATTRIBUTES
:
296 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
:
298 case GL_ACTIVE_UNIFORMS
:
300 case GL_ACTIVE_UNIFORM_MAX_LENGTH
:
303 // -- glGetRenderbufferAttachmentParameteriv
304 case GL_RENDERBUFFER_WIDTH
:
306 case GL_RENDERBUFFER_HEIGHT
:
308 case GL_RENDERBUFFER_INTERNAL_FORMAT
:
310 case GL_RENDERBUFFER_RED_SIZE
:
312 case GL_RENDERBUFFER_GREEN_SIZE
:
314 case GL_RENDERBUFFER_BLUE_SIZE
:
316 case GL_RENDERBUFFER_ALPHA_SIZE
:
318 case GL_RENDERBUFFER_DEPTH_SIZE
:
320 case GL_RENDERBUFFER_STENCIL_SIZE
:
322 // -- glGetRenderbufferAttachmentParameteriv with
323 // GL_EXT_multisampled_render_to_texture
324 case GL_RENDERBUFFER_SAMPLES_EXT
:
330 // Already defined under glGetFramebufferAttachemntParameteriv.
331 // case GL_DELETE_STATUS:
333 case GL_COMPILE_STATUS
:
335 // Already defined under glGetFramebufferAttachemntParameteriv.
336 // case GL_INFO_LOG_LENGTH:
338 case GL_SHADER_SOURCE_LENGTH
:
340 case GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE
:
343 // -- glGetTexParameterfv, glGetTexParameteriv
344 case GL_TEXTURE_MAG_FILTER
:
346 case GL_TEXTURE_MIN_FILTER
:
348 case GL_TEXTURE_WRAP_R
:
350 case GL_TEXTURE_WRAP_S
:
352 case GL_TEXTURE_WRAP_T
:
354 case GL_TEXTURE_COMPARE_FUNC
:
356 case GL_TEXTURE_COMPARE_MODE
:
358 case GL_TEXTURE_MAX_LOD
:
360 case GL_TEXTURE_MIN_LOD
:
362 case GL_TEXTURE_BASE_LEVEL
:
364 case GL_TEXTURE_MAX_LEVEL
:
366 case GL_TEXTURE_IMMUTABLE_FORMAT
:
368 case GL_TEXTURE_IMMUTABLE_LEVELS
:
370 case GL_TEXTURE_MAX_ANISOTROPY_EXT
:
373 // -- glGetVertexAttrib
374 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING
:
376 case GL_VERTEX_ATTRIB_ARRAY_ENABLED
:
378 case GL_VERTEX_ATTRIB_ARRAY_SIZE
:
380 case GL_VERTEX_ATTRIB_ARRAY_STRIDE
:
382 case GL_VERTEX_ATTRIB_ARRAY_TYPE
:
384 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED
:
386 case GL_CURRENT_VERTEX_ATTRIB
:
388 case GL_VERTEX_ATTRIB_ARRAY_INTEGER
:
390 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR
:
398 case GL_SYNC_CONDITION
:
403 // -- glHint with GL_OES_standard_derivatives
404 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES
:
407 // Chromium internal bind_generates_resource query
408 case GL_BIND_GENERATES_RESOURCE_CHROMIUM
:
419 // Return the number of elements per group of a specified format.
420 int ElementsPerGroup(int format
, int type
) {
422 case GL_UNSIGNED_SHORT_5_6_5
:
423 case GL_UNSIGNED_SHORT_4_4_4_4
:
424 case GL_UNSIGNED_SHORT_5_5_5_1
:
425 case GL_UNSIGNED_INT_24_8_OES
:
426 case GL_UNSIGNED_INT_2_10_10_10_REV
:
427 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
428 case GL_UNSIGNED_INT_5_9_9_9_REV
:
429 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
440 case GL_LUMINANCE_ALPHA
:
445 case GL_RGBA_INTEGER
:
447 case GL_SRGB_ALPHA_EXT
:
451 case GL_DEPTH_COMPONENT
:
452 case GL_DEPTH_COMPONENT24_OES
:
453 case GL_DEPTH_COMPONENT32_OES
:
454 case GL_DEPTH_COMPONENT16
:
455 case GL_DEPTH24_STENCIL8_OES
:
456 case GL_DEPTH_STENCIL_OES
:
465 // Return the number of bytes per element, based on the element type.
466 int BytesPerElement(int type
) {
468 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
471 case GL_UNSIGNED_INT_24_8_OES
:
472 case GL_UNSIGNED_INT
:
474 case GL_UNSIGNED_INT_2_10_10_10_REV
:
475 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
476 case GL_UNSIGNED_INT_5_9_9_9_REV
:
479 case GL_HALF_FLOAT_OES
:
480 case GL_UNSIGNED_SHORT
:
482 case GL_UNSIGNED_SHORT_5_6_5
:
483 case GL_UNSIGNED_SHORT_4_4_4_4
:
484 case GL_UNSIGNED_SHORT_5_5_5_1
:
486 case GL_UNSIGNED_BYTE
:
494 } // anonymous namespace
496 uint32
GLES2Util::ComputeImageGroupSize(int format
, int type
) {
497 int bytes_per_element
= BytesPerElement(type
);
498 DCHECK_GE(8, bytes_per_element
);
499 int elements_per_group
= ElementsPerGroup(format
, type
);
500 DCHECK_GE(4, elements_per_group
);
501 return bytes_per_element
* elements_per_group
;
504 bool GLES2Util::ComputeImagePaddedRowSize(
505 int width
, int format
, int type
, int unpack_alignment
,
506 uint32
* padded_row_size
) {
507 DCHECK(unpack_alignment
== 1 || unpack_alignment
== 2 ||
508 unpack_alignment
== 4 || unpack_alignment
== 8);
509 uint32 bytes_per_group
= ComputeImageGroupSize(format
, type
);
510 uint32 unpadded_row_size
;
511 if (!SafeMultiplyUint32(width
, bytes_per_group
, &unpadded_row_size
)) {
515 if (!SafeAddUint32(unpadded_row_size
, unpack_alignment
- 1, &temp
)) {
518 *padded_row_size
= (temp
/ unpack_alignment
) * unpack_alignment
;
522 // Returns the amount of data glTexImage*D or glTexSubImage*D will access.
523 bool GLES2Util::ComputeImageDataSizes(
524 int width
, int height
, int depth
, int format
, int type
,
525 int unpack_alignment
, uint32
* size
, uint32
* ret_unpadded_row_size
,
526 uint32
* ret_padded_row_size
) {
527 DCHECK(unpack_alignment
== 1 || unpack_alignment
== 2 ||
528 unpack_alignment
== 4 || unpack_alignment
== 8);
529 uint32 bytes_per_group
= ComputeImageGroupSize(format
, type
);
531 if (!SafeMultiplyUint32(width
, bytes_per_group
, &row_size
)) {
535 if (!SafeMultiplyUint32(height
, depth
, &num_of_rows
)) {
538 if (num_of_rows
> 1) {
540 if (!SafeAddUint32(row_size
, unpack_alignment
- 1, &temp
)) {
543 uint32 padded_row_size
= (temp
/ unpack_alignment
) * unpack_alignment
;
544 uint32 size_of_all_but_last_row
;
545 if (!SafeMultiplyUint32((num_of_rows
- 1), padded_row_size
,
546 &size_of_all_but_last_row
)) {
549 if (!SafeAddUint32(size_of_all_but_last_row
, row_size
, size
)) {
552 if (ret_padded_row_size
) {
553 *ret_padded_row_size
= padded_row_size
;
557 if (ret_padded_row_size
) {
558 *ret_padded_row_size
= row_size
;
561 if (ret_unpadded_row_size
) {
562 *ret_unpadded_row_size
= row_size
;
568 size_t GLES2Util::RenderbufferBytesPerPixel(int format
) {
570 case GL_STENCIL_INDEX8
:
575 case GL_DEPTH_COMPONENT16
:
579 case GL_DEPTH24_STENCIL8_OES
:
582 case GL_DEPTH_COMPONENT24_OES
:
589 uint32
GLES2Util::GetElementSizeForUniformType(int type
) {
598 return sizeof(GLfloat
);
608 case GL_SAMPLER_CUBE
:
609 case GL_SAMPLER_2D_RECT_ARB
: // extension.
610 case GL_SAMPLER_EXTERNAL_OES
: // extension.
611 return sizeof(GLint
);
614 case GL_UNSIGNED_INT
:
615 case GL_UNSIGNED_INT_VEC2
:
616 case GL_UNSIGNED_INT_VEC3
:
617 case GL_UNSIGNED_INT_VEC4
:
618 return sizeof(GLuint
);
620 case GL_SAMPLER_2D_SHADOW
:
621 case GL_SAMPLER_2D_ARRAY
:
622 case GL_SAMPLER_2D_ARRAY_SHADOW
:
623 case GL_SAMPLER_CUBE_SHADOW
:
624 case GL_INT_SAMPLER_2D
:
625 case GL_INT_SAMPLER_3D
:
626 case GL_INT_SAMPLER_CUBE
:
627 case GL_INT_SAMPLER_2D_ARRAY
:
628 case GL_UNSIGNED_INT_SAMPLER_2D
:
629 case GL_UNSIGNED_INT_SAMPLER_3D
:
630 case GL_UNSIGNED_INT_SAMPLER_CUBE
:
631 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY
:
632 return sizeof(GLint
);
633 case GL_FLOAT_MAT2x3
:
634 case GL_FLOAT_MAT3x2
:
635 case GL_FLOAT_MAT2x4
:
636 case GL_FLOAT_MAT4x2
:
637 case GL_FLOAT_MAT3x4
:
638 case GL_FLOAT_MAT4x3
:
639 return sizeof(GLfloat
);
646 uint32
GLES2Util::GetElementCountForUniformType(int type
) {
652 case GL_SAMPLER_CUBE
:
653 case GL_SAMPLER_2D_RECT_ARB
: // extension.
654 case GL_SAMPLER_EXTERNAL_OES
: // extension.
675 case GL_UNSIGNED_INT
:
677 case GL_SAMPLER_2D_SHADOW
:
678 case GL_SAMPLER_2D_ARRAY
:
679 case GL_SAMPLER_2D_ARRAY_SHADOW
:
680 case GL_SAMPLER_CUBE_SHADOW
:
681 case GL_INT_SAMPLER_2D
:
682 case GL_INT_SAMPLER_3D
:
683 case GL_INT_SAMPLER_CUBE
:
684 case GL_INT_SAMPLER_2D_ARRAY
:
685 case GL_UNSIGNED_INT_SAMPLER_2D
:
686 case GL_UNSIGNED_INT_SAMPLER_3D
:
687 case GL_UNSIGNED_INT_SAMPLER_CUBE
:
688 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY
:
690 case GL_UNSIGNED_INT_VEC2
:
692 case GL_UNSIGNED_INT_VEC3
:
694 case GL_UNSIGNED_INT_VEC4
:
696 case GL_FLOAT_MAT2x3
:
697 case GL_FLOAT_MAT3x2
:
699 case GL_FLOAT_MAT2x4
:
700 case GL_FLOAT_MAT4x2
:
702 case GL_FLOAT_MAT3x4
:
703 case GL_FLOAT_MAT4x3
:
711 size_t GLES2Util::GetGLTypeSizeForTexturesAndBuffers(uint32 type
) {
714 return sizeof(GLbyte
); // NOLINT
715 case GL_UNSIGNED_BYTE
:
716 return sizeof(GLubyte
); // NOLINT
718 return sizeof(GLshort
); // NOLINT
719 case GL_UNSIGNED_SHORT
:
720 return sizeof(GLushort
); // NOLINT
722 return sizeof(GLint
); // NOLINT
723 case GL_UNSIGNED_INT
:
724 return sizeof(GLuint
); // NOLINT
726 return sizeof(GLfloat
); // NOLINT
728 return sizeof(GLfixed
); // NOLINT
734 size_t GLES2Util::GetGLTypeSizeForPathCoordType(uint32 type
) {
737 return sizeof(GLbyte
); // NOLINT
738 case GL_UNSIGNED_BYTE
:
739 return sizeof(GLubyte
); // NOLINT
741 return sizeof(GLshort
); // NOLINT
742 case GL_UNSIGNED_SHORT
:
743 return sizeof(GLushort
); // NOLINT
745 return sizeof(GLfloat
); // NOLINT
751 uint32
GLES2Util::GLErrorToErrorBit(uint32 error
) {
753 case GL_INVALID_ENUM
:
754 return gl_error_bit::kInvalidEnum
;
755 case GL_INVALID_VALUE
:
756 return gl_error_bit::kInvalidValue
;
757 case GL_INVALID_OPERATION
:
758 return gl_error_bit::kInvalidOperation
;
759 case GL_OUT_OF_MEMORY
:
760 return gl_error_bit::kOutOfMemory
;
761 case GL_INVALID_FRAMEBUFFER_OPERATION
:
762 return gl_error_bit::kInvalidFrameBufferOperation
;
763 case GL_CONTEXT_LOST_KHR
:
764 return gl_error_bit::kContextLost
;
767 return gl_error_bit::kNoError
;
771 uint32
GLES2Util::GLErrorBitToGLError(uint32 error_bit
) {
773 case gl_error_bit::kInvalidEnum
:
774 return GL_INVALID_ENUM
;
775 case gl_error_bit::kInvalidValue
:
776 return GL_INVALID_VALUE
;
777 case gl_error_bit::kInvalidOperation
:
778 return GL_INVALID_OPERATION
;
779 case gl_error_bit::kOutOfMemory
:
780 return GL_OUT_OF_MEMORY
;
781 case gl_error_bit::kInvalidFrameBufferOperation
:
782 return GL_INVALID_FRAMEBUFFER_OPERATION
;
783 case gl_error_bit::kContextLost
:
784 return GL_CONTEXT_LOST_KHR
;
791 uint32
GLES2Util::IndexToGLFaceTarget(int index
) {
792 static uint32 faces
[] = {
793 GL_TEXTURE_CUBE_MAP_POSITIVE_X
,
794 GL_TEXTURE_CUBE_MAP_NEGATIVE_X
,
795 GL_TEXTURE_CUBE_MAP_POSITIVE_Y
,
796 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
,
797 GL_TEXTURE_CUBE_MAP_POSITIVE_Z
,
798 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
,
803 size_t GLES2Util::GLTargetToFaceIndex(uint32 target
) {
806 case GL_TEXTURE_EXTERNAL_OES
:
807 case GL_TEXTURE_RECTANGLE_ARB
:
809 case GL_TEXTURE_2D_ARRAY
:
811 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
813 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
815 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
817 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
819 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
821 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
829 uint32
GLES2Util::GetGLReadPixelsImplementationFormat(
830 uint32 internal_format
) {
831 switch (internal_format
) {
842 return GL_RED_INTEGER
;
853 return GL_RG_INTEGER
;
857 case GL_R11F_G11F_B10F
:
868 return GL_RGBA_INTEGER
;
874 uint32
GLES2Util::GetGLReadPixelsImplementationType(
875 uint32 internal_format
, uint32 texture_type
) {
876 switch (internal_format
) {
882 return GL_UNSIGNED_SHORT
;
886 return GL_UNSIGNED_INT
;
906 case GL_R11F_G11F_B10F
:
909 // TODO(zmo): Consider return GL_UNSIGNED_INT_10F_11F_11F_REV and
914 // Unsized internal format, check the type
915 switch (texture_type
) {
917 case GL_HALF_FLOAT_OES
:
919 // TODO(zmo): Consider return GL_UNSIGNED_SHORT_5_6_5,
920 // GL_UNSIGNED_SHORT_4_4_4_4, GL_UNSIGNED_SHORT_5_5_5_1, and
921 // GL_UNSIGNED_INT_2_10_10_10_REV.
923 return GL_UNSIGNED_BYTE
;
926 return GL_UNSIGNED_BYTE
;
930 uint32
GLES2Util::GetChannelsForFormat(int format
) {
933 case GL_ALPHA16F_EXT
:
934 case GL_ALPHA32F_EXT
:
938 case GL_LUMINANCE_ALPHA
:
948 case GL_R11F_G11F_B10F
:
965 case GL_SRGB_ALPHA_EXT
:
966 case GL_SRGB8_ALPHA8_EXT
:
977 case GL_DEPTH_COMPONENT32_OES
:
978 case GL_DEPTH_COMPONENT24_OES
:
979 case GL_DEPTH_COMPONENT16
:
980 case GL_DEPTH_COMPONENT
:
981 case GL_DEPTH_COMPONENT32F
:
983 case GL_STENCIL_INDEX8
:
985 case GL_DEPTH_STENCIL_OES
:
986 case GL_DEPTH24_STENCIL8_OES
:
987 case GL_DEPTH32F_STENCIL8
:
988 return kDepth
| kStencil
;
1012 return kRed
| kGreen
;
1018 uint32
GLES2Util::GetChannelsNeededForAttachmentType(
1019 int type
, uint32 max_color_attachments
) {
1021 case GL_DEPTH_ATTACHMENT
:
1023 case GL_STENCIL_ATTACHMENT
:
1026 if (type
>= GL_COLOR_ATTACHMENT0
&&
1027 type
< static_cast<int>(
1028 GL_COLOR_ATTACHMENT0
+ max_color_attachments
)) {
1035 std::string
GLES2Util::GetStringEnum(uint32 value
) {
1036 const EnumToString
* entry
= enum_to_string_table_
;
1037 const EnumToString
* end
= entry
+ enum_to_string_table_len_
;
1038 for (;entry
< end
; ++entry
) {
1039 if (value
== entry
->value
) {
1043 std::stringstream ss
;
1045 ss
.width(value
< 0x10000 ? 4 : 8);
1046 ss
<< std::hex
<< value
;
1047 return "0x" + ss
.str();
1050 std::string
GLES2Util::GetStringError(uint32 value
) {
1051 static EnumToString string_table
[] = {
1052 { GL_NONE
, "GL_NONE" },
1054 return GLES2Util::GetQualifiedEnumString(
1055 string_table
, arraysize(string_table
), value
);
1058 std::string
GLES2Util::GetStringBool(uint32 value
) {
1059 return value
? "GL_TRUE" : "GL_FALSE";
1062 std::string
GLES2Util::GetQualifiedEnumString(
1063 const EnumToString
* table
, size_t count
, uint32 value
) {
1064 for (const EnumToString
* end
= table
+ count
; table
< end
; ++table
) {
1065 if (table
->value
== value
) {
1069 return GetStringEnum(value
);
1072 bool GLES2Util::ParseUniformName(
1073 const std::string
& name
,
1076 bool* getting_array
) {
1079 bool getting_array_location
= false;
1080 size_t open_pos
= std::string::npos
;
1081 base::CheckedNumeric
<int> index
= 0;
1082 if (name
[name
.size() - 1] == ']') {
1083 if (name
.size() < 3) {
1086 open_pos
= name
.find_last_of('[');
1087 if (open_pos
== std::string::npos
||
1088 open_pos
>= name
.size() - 2) {
1091 size_t last
= name
.size() - 1;
1092 for (size_t pos
= open_pos
+ 1; pos
< last
; ++pos
) {
1093 int8 digit
= name
[pos
] - '0';
1094 if (digit
< 0 || digit
> 9) {
1097 index
= index
* 10 + digit
;
1099 if (!index
.IsValid()) {
1102 getting_array_location
= true;
1104 *getting_array
= getting_array_location
;
1105 *element_index
= index
.ValueOrDie();
1106 *array_pos
= open_pos
;
1110 size_t GLES2Util::CalcClearBufferivDataCount(int buffer
) {
1121 size_t GLES2Util::CalcClearBufferfvDataCount(int buffer
) {
1133 void GLES2Util::MapUint64ToTwoUint32(
1134 uint64_t v64
, uint32_t* v32_0
, uint32_t* v32_1
) {
1135 DCHECK(v32_0
&& v32_1
);
1136 *v32_0
= static_cast<uint32_t>(v64
& 0xFFFFFFFF);
1137 *v32_1
= static_cast<uint32_t>((v64
& 0xFFFFFFFF00000000) >> 32);
1141 uint64_t GLES2Util::MapTwoUint32ToUint64(uint32_t v32_0
, uint32_t v32_1
) {
1142 uint64_t v64
= v32_1
;
1143 return (v64
<< 32) | v32_0
;
1147 uint32_t GLES2Util::MapBufferTargetToBindingEnum(uint32_t target
) {
1149 case GL_ARRAY_BUFFER
:
1150 return GL_ARRAY_BUFFER_BINDING
;
1151 case GL_COPY_READ_BUFFER
:
1152 return GL_COPY_READ_BUFFER_BINDING
;
1153 case GL_COPY_WRITE_BUFFER
:
1154 return GL_COPY_WRITE_BUFFER_BINDING
;
1155 case GL_ELEMENT_ARRAY_BUFFER
:
1156 return GL_ELEMENT_ARRAY_BUFFER_BINDING
;
1157 case GL_PIXEL_PACK_BUFFER
:
1158 return GL_PIXEL_PACK_BUFFER_BINDING
;
1159 case GL_PIXEL_UNPACK_BUFFER
:
1160 return GL_PIXEL_UNPACK_BUFFER_BINDING
;
1161 case GL_TRANSFORM_FEEDBACK_BUFFER
:
1162 return GL_TRANSFORM_FEEDBACK_BUFFER_BINDING
;
1163 case GL_UNIFORM_BUFFER
:
1164 return GL_UNIFORM_BUFFER_BINDING
;
1171 bool GLES2Util::IsUnsignedIntegerFormat(uint32_t internal_format
) {
1172 switch (internal_format
) {
1190 bool GLES2Util::IsSignedIntegerFormat(uint32_t internal_format
) {
1191 switch (internal_format
) {
1208 bool GLES2Util::IsIntegerFormat(uint32_t internal_format
) {
1209 return (IsUnsignedIntegerFormat(internal_format
) ||
1210 IsSignedIntegerFormat(internal_format
));
1216 // WebGraphicsContext3DCommandBufferImpl configuration attributes. Those in
1217 // the 16-bit range are the same as used by EGL. Those outside the 16-bit range
1218 // are unique to Chromium. Attributes are matched using a closest fit algorithm.
1220 // From <EGL/egl.h>.
1221 const int32 kAlphaSize
= 0x3021; // EGL_ALPHA_SIZE
1222 const int32 kBlueSize
= 0x3022; // EGL_BLUE_SIZE
1223 const int32 kGreenSize
= 0x3023; // EGL_GREEN_SIZE
1224 const int32 kRedSize
= 0x3024; // EGL_RED_SIZE
1225 const int32 kDepthSize
= 0x3025; // EGL_DEPTH_SIZE
1226 const int32 kStencilSize
= 0x3026; // EGL_STENCIL_SIZE
1227 const int32 kSamples
= 0x3031; // EGL_SAMPLES
1228 const int32 kSampleBuffers
= 0x3032; // EGL_SAMPLE_BUFFERS
1229 const int32 kNone
= 0x3038; // EGL_NONE
1230 const int32 kSwapBehavior
= 0x3093; // EGL_SWAP_BEHAVIOR
1231 const int32 kBufferPreserved
= 0x3094; // EGL_BUFFER_PRESERVED
1232 const int32 kBufferDestroyed
= 0x3095; // EGL_BUFFER_DESTROYED
1235 const int32 kBindGeneratesResource
= 0x10000;
1236 const int32 kFailIfMajorPerfCaveat
= 0x10001;
1237 const int32 kLoseContextWhenOutOfMemory
= 0x10002;
1238 const int32 kWebGLVersion
= 0x10003;
1242 ContextCreationAttribHelper::ContextCreationAttribHelper()
1251 buffer_preserved(true),
1252 bind_generates_resource(true),
1253 fail_if_major_perf_caveat(false),
1254 lose_context_when_out_of_memory(false),
1258 void ContextCreationAttribHelper::Serialize(std::vector
<int32
>* attribs
) const {
1259 if (alpha_size
!= -1) {
1260 attribs
->push_back(kAlphaSize
);
1261 attribs
->push_back(alpha_size
);
1263 if (blue_size
!= -1) {
1264 attribs
->push_back(kBlueSize
);
1265 attribs
->push_back(blue_size
);
1267 if (green_size
!= -1) {
1268 attribs
->push_back(kGreenSize
);
1269 attribs
->push_back(green_size
);
1271 if (red_size
!= -1) {
1272 attribs
->push_back(kRedSize
);
1273 attribs
->push_back(red_size
);
1275 if (depth_size
!= -1) {
1276 attribs
->push_back(kDepthSize
);
1277 attribs
->push_back(depth_size
);
1279 if (stencil_size
!= -1) {
1280 attribs
->push_back(kStencilSize
);
1281 attribs
->push_back(stencil_size
);
1283 if (samples
!= -1) {
1284 attribs
->push_back(kSamples
);
1285 attribs
->push_back(samples
);
1287 if (sample_buffers
!= -1) {
1288 attribs
->push_back(kSampleBuffers
);
1289 attribs
->push_back(sample_buffers
);
1291 attribs
->push_back(kSwapBehavior
);
1292 attribs
->push_back(buffer_preserved
? kBufferPreserved
: kBufferDestroyed
);
1293 attribs
->push_back(kBindGeneratesResource
);
1294 attribs
->push_back(bind_generates_resource
? 1 : 0);
1295 attribs
->push_back(kFailIfMajorPerfCaveat
);
1296 attribs
->push_back(fail_if_major_perf_caveat
? 1 : 0);
1297 attribs
->push_back(kLoseContextWhenOutOfMemory
);
1298 attribs
->push_back(lose_context_when_out_of_memory
? 1 : 0);
1299 attribs
->push_back(kWebGLVersion
);
1300 attribs
->push_back(webgl_version
);
1301 attribs
->push_back(kNone
);
1304 bool ContextCreationAttribHelper::Parse(const std::vector
<int32
>& attribs
) {
1305 for (size_t i
= 0; i
< attribs
.size(); i
+= 2) {
1306 const int32 attrib
= attribs
[i
];
1307 if (i
+ 1 >= attribs
.size()) {
1308 if (attrib
== kNone
) {
1312 DLOG(ERROR
) << "Missing value after context creation attribute: "
1317 const int32 value
= attribs
[i
+1];
1335 stencil_size
= value
;
1340 case kSampleBuffers
:
1341 sample_buffers
= value
;
1344 buffer_preserved
= value
== kBufferPreserved
;
1346 case kBindGeneratesResource
:
1347 bind_generates_resource
= value
!= 0;
1349 case kFailIfMajorPerfCaveat
:
1350 fail_if_major_perf_caveat
= value
!= 0;
1352 case kLoseContextWhenOutOfMemory
:
1353 lose_context_when_out_of_memory
= value
!= 0;
1356 webgl_version
= value
;
1359 // Terminate list, even if more attributes.
1362 DLOG(ERROR
) << "Invalid context creation attribute: " << attrib
;
1370 #include "gpu/command_buffer/common/gles2_cmd_utils_implementation_autogen.h"
1372 } // namespace gles2