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>
13 #include "gpu/command_buffer/common/gles2_cmd_format.h"
14 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
19 namespace gl_error_bit {
22 kInvalidEnum = (1 << 0),
23 kInvalidValue = (1 << 1),
24 kInvalidOperation = (1 << 2),
25 kOutOfMemory = (1 << 3),
26 kInvalidFrameBufferOperation = (1 << 4)
30 int GLES2Util::GLGetNumValuesReturned(int id) const {
32 // -- glGetBooleanv, glGetFloatv, glGetIntergerv
33 case GL_ACTIVE_TEXTURE:
35 case GL_ALIASED_LINE_WIDTH_RANGE:
37 case GL_ALIASED_POINT_SIZE_RANGE:
41 case GL_ARRAY_BUFFER_BINDING:
47 case GL_BLEND_DST_ALPHA:
49 case GL_BLEND_DST_RGB:
51 case GL_BLEND_EQUATION_ALPHA:
53 case GL_BLEND_EQUATION_RGB:
55 case GL_BLEND_SRC_ALPHA:
57 case GL_BLEND_SRC_RGB:
61 case GL_COLOR_CLEAR_VALUE:
63 case GL_COLOR_WRITEMASK:
65 case GL_COMPRESSED_TEXTURE_FORMATS:
66 return num_compressed_texture_formats_;
69 case GL_CULL_FACE_MODE:
71 case GL_CURRENT_PROGRAM:
75 case GL_DEPTH_CLEAR_VALUE:
83 case GL_DEPTH_WRITEMASK:
87 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
89 case GL_FRAMEBUFFER_BINDING:
93 case GL_GENERATE_MIPMAP_HINT:
97 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
99 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
103 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
105 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
107 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
109 case GL_MAX_RENDERBUFFER_SIZE:
111 case GL_MAX_TEXTURE_IMAGE_UNITS:
113 case GL_MAX_TEXTURE_SIZE:
115 case GL_MAX_VARYING_VECTORS:
117 case GL_MAX_VERTEX_ATTRIBS:
119 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
121 case GL_MAX_VERTEX_UNIFORM_VECTORS:
123 case GL_MAX_VIEWPORT_DIMS:
125 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
127 case GL_NUM_SHADER_BINARY_FORMATS:
129 case GL_PACK_ALIGNMENT:
131 case GL_PACK_REVERSE_ROW_ORDER_ANGLE:
133 case GL_POLYGON_OFFSET_FACTOR:
135 case GL_POLYGON_OFFSET_FILL:
137 case GL_POLYGON_OFFSET_UNITS:
141 case GL_RENDERBUFFER_BINDING:
143 case GL_SAMPLE_BUFFERS:
145 case GL_SAMPLE_COVERAGE_INVERT:
147 case GL_SAMPLE_COVERAGE_VALUE:
153 case GL_SCISSOR_TEST:
155 case GL_SHADER_BINARY_FORMATS:
156 return num_shader_binary_formats_;
157 case GL_SHADER_COMPILER:
159 case GL_STENCIL_BACK_FAIL:
161 case GL_STENCIL_BACK_FUNC:
163 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
165 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
167 case GL_STENCIL_BACK_REF:
169 case GL_STENCIL_BACK_VALUE_MASK:
171 case GL_STENCIL_BACK_WRITEMASK:
173 case GL_STENCIL_BITS:
175 case GL_STENCIL_CLEAR_VALUE:
177 case GL_STENCIL_FAIL:
179 case GL_STENCIL_FUNC:
181 case GL_STENCIL_PASS_DEPTH_FAIL:
183 case GL_STENCIL_PASS_DEPTH_PASS:
187 case GL_STENCIL_TEST:
189 case GL_STENCIL_VALUE_MASK:
191 case GL_STENCIL_WRITEMASK:
193 case GL_SUBPIXEL_BITS:
195 case GL_TEXTURE_BINDING_2D:
197 case GL_TEXTURE_BINDING_CUBE_MAP:
199 case GL_TEXTURE_BINDING_EXTERNAL_OES:
201 case GL_TEXTURE_BINDING_RECTANGLE_ARB:
203 case GL_TEXTURE_IMMUTABLE_FORMAT_EXT:
205 case GL_UNPACK_ALIGNMENT:
209 // -- glGetBooleanv, glGetFloatv, glGetIntergerv with
210 // GL_CHROMIUM_framebuffer_multisample
211 case GL_MAX_SAMPLES_EXT:
213 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
216 // -- glGetBufferParameteriv
219 case GL_BUFFER_USAGE:
222 // -- glGetFramebufferAttachmentParameteriv
223 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE:
225 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME:
227 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL:
229 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE:
231 // -- glGetFramebufferAttachmentParameteriv with
232 // GL_EXT_multisampled_render_to_texture
233 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_SAMPLES_EXT:
237 case GL_DELETE_STATUS:
241 case GL_VALIDATE_STATUS:
243 case GL_INFO_LOG_LENGTH:
245 case GL_ATTACHED_SHADERS:
247 case GL_ACTIVE_ATTRIBUTES:
249 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
251 case GL_ACTIVE_UNIFORMS:
253 case GL_ACTIVE_UNIFORM_MAX_LENGTH:
257 // -- glGetRenderbufferAttachmentParameteriv
258 case GL_RENDERBUFFER_WIDTH:
260 case GL_RENDERBUFFER_HEIGHT:
262 case GL_RENDERBUFFER_INTERNAL_FORMAT:
264 case GL_RENDERBUFFER_RED_SIZE:
266 case GL_RENDERBUFFER_GREEN_SIZE:
268 case GL_RENDERBUFFER_BLUE_SIZE:
270 case GL_RENDERBUFFER_ALPHA_SIZE:
272 case GL_RENDERBUFFER_DEPTH_SIZE:
274 case GL_RENDERBUFFER_STENCIL_SIZE:
276 // -- glGetRenderbufferAttachmentParameteriv with
277 // GL_EXT_multisampled_render_to_texture
278 case GL_RENDERBUFFER_SAMPLES_EXT:
284 // Already defined under glGetFramebufferAttachemntParameteriv.
285 // case GL_DELETE_STATUS:
287 case GL_COMPILE_STATUS:
289 // Already defined under glGetFramebufferAttachemntParameteriv.
290 // case GL_INFO_LOG_LENGTH:
292 case GL_SHADER_SOURCE_LENGTH:
294 case GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE:
297 // -- glGetTexParameterfv, glGetTexParameteriv
298 case GL_TEXTURE_MAG_FILTER:
300 case GL_TEXTURE_MIN_FILTER:
302 case GL_TEXTURE_WRAP_S:
304 case GL_TEXTURE_WRAP_T:
306 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
309 // -- glGetVertexAttribfv, glGetVertexAttribiv
310 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
312 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
314 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
316 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
318 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
320 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
322 case GL_CURRENT_VERTEX_ATTRIB:
325 // -- glHint with GL_OES_standard_derivatives
326 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
337 // Return the number of elements per group of a specified format.
338 int ElementsPerGroup(int format, int type) {
340 case GL_UNSIGNED_SHORT_5_6_5:
341 case GL_UNSIGNED_SHORT_4_4_4_4:
342 case GL_UNSIGNED_SHORT_5_5_5_1:
343 case GL_UNSIGNED_INT_24_8_OES:
352 case GL_LUMINANCE_ALPHA:
359 case GL_DEPTH_COMPONENT:
360 case GL_DEPTH_COMPONENT24_OES:
361 case GL_DEPTH_COMPONENT32_OES:
362 case GL_DEPTH_COMPONENT16:
363 case GL_DEPTH24_STENCIL8_OES:
364 case GL_DEPTH_STENCIL_OES:
371 // Return the number of bytes per element, based on the element type.
372 int BytesPerElement(int type) {
375 case GL_UNSIGNED_INT_24_8_OES:
376 case GL_UNSIGNED_INT:
378 case GL_HALF_FLOAT_OES:
379 case GL_UNSIGNED_SHORT:
381 case GL_UNSIGNED_SHORT_5_6_5:
382 case GL_UNSIGNED_SHORT_4_4_4_4:
383 case GL_UNSIGNED_SHORT_5_5_5_1:
385 case GL_UNSIGNED_BYTE:
393 } // anonymous namespace
395 uint32 GLES2Util::ComputeImageGroupSize(int format, int type) {
396 return BytesPerElement(type) * ElementsPerGroup(format, type);
399 bool GLES2Util::ComputeImagePaddedRowSize(
400 int width, int format, int type, int unpack_alignment,
401 uint32* padded_row_size) {
402 uint32 bytes_per_group = ComputeImageGroupSize(format, type);
403 uint32 unpadded_row_size;
404 if (!SafeMultiplyUint32(width, bytes_per_group, &unpadded_row_size)) {
408 if (!SafeAddUint32(unpadded_row_size, unpack_alignment - 1, &temp)) {
411 *padded_row_size = (temp / unpack_alignment) * unpack_alignment;
415 // Returns the amount of data glTexImage2D or glTexSubImage2D will access.
416 bool GLES2Util::ComputeImageDataSizes(
417 int width, int height, int format, int type, int unpack_alignment,
418 uint32* size, uint32* ret_unpadded_row_size, uint32* ret_padded_row_size) {
419 uint32 bytes_per_group = ComputeImageGroupSize(format, type);
421 if (!SafeMultiplyUint32(width, bytes_per_group, &row_size)) {
426 if (!SafeAddUint32(row_size, unpack_alignment - 1, &temp)) {
429 uint32 padded_row_size = (temp / unpack_alignment) * unpack_alignment;
430 uint32 size_of_all_but_last_row;
431 if (!SafeMultiplyUint32((height - 1), padded_row_size,
432 &size_of_all_but_last_row)) {
435 if (!SafeAddUint32(size_of_all_but_last_row, row_size, size)) {
438 if (ret_padded_row_size) {
439 *ret_padded_row_size = padded_row_size;
442 if (!SafeMultiplyUint32(height, row_size, size)) {
445 if (ret_padded_row_size) {
446 *ret_padded_row_size = row_size;
449 if (ret_unpadded_row_size) {
450 *ret_unpadded_row_size = row_size;
456 size_t GLES2Util::RenderbufferBytesPerPixel(int format) {
458 case GL_STENCIL_INDEX8:
463 case GL_DEPTH_COMPONENT16:
467 case GL_DEPTH24_STENCIL8_OES:
470 case GL_DEPTH_COMPONENT24_OES:
477 uint32 GLES2Util::GetGLDataTypeSizeForUniforms(int type) {
480 return sizeof(GLfloat); // NOLINT
482 return sizeof(GLfloat) * 2; // NOLINT
484 return sizeof(GLfloat) * 3; // NOLINT
486 return sizeof(GLfloat) * 4; // NOLINT
488 return sizeof(GLint); // NOLINT
490 return sizeof(GLint) * 2; // NOLINT
492 return sizeof(GLint) * 3; // NOLINT
494 return sizeof(GLint) * 4; // NOLINT
496 return sizeof(GLint); // NOLINT
498 return sizeof(GLint) * 2; // NOLINT
500 return sizeof(GLint) * 3; // NOLINT
502 return sizeof(GLint) * 4; // NOLINT
504 return sizeof(GLfloat) * 2 * 2; // NOLINT
506 return sizeof(GLfloat) * 3 * 3; // NOLINT
508 return sizeof(GLfloat) * 4 * 4; // NOLINT
510 return sizeof(GLint); // NOLINT
511 case GL_SAMPLER_2D_RECT_ARB:
512 return sizeof(GLint); // NOLINT
513 case GL_SAMPLER_CUBE:
514 return sizeof(GLint); // NOLINT
515 case GL_SAMPLER_EXTERNAL_OES:
516 return sizeof(GLint); // NOLINT
522 size_t GLES2Util::GetGLTypeSizeForTexturesAndBuffers(uint32 type) {
525 return sizeof(GLbyte); // NOLINT
526 case GL_UNSIGNED_BYTE:
527 return sizeof(GLubyte); // NOLINT
529 return sizeof(GLshort); // NOLINT
530 case GL_UNSIGNED_SHORT:
531 return sizeof(GLushort); // NOLINT
533 return sizeof(GLint); // NOLINT
534 case GL_UNSIGNED_INT:
535 return sizeof(GLuint); // NOLINT
537 return sizeof(GLfloat); // NOLINT
539 return sizeof(GLfixed); // NOLINT
545 uint32 GLES2Util::GLErrorToErrorBit(uint32 error) {
547 case GL_INVALID_ENUM:
548 return gl_error_bit::kInvalidEnum;
549 case GL_INVALID_VALUE:
550 return gl_error_bit::kInvalidValue;
551 case GL_INVALID_OPERATION:
552 return gl_error_bit::kInvalidOperation;
553 case GL_OUT_OF_MEMORY:
554 return gl_error_bit::kOutOfMemory;
555 case GL_INVALID_FRAMEBUFFER_OPERATION:
556 return gl_error_bit::kInvalidFrameBufferOperation;
559 return gl_error_bit::kNoError;
563 uint32 GLES2Util::GLErrorBitToGLError(uint32 error_bit) {
565 case gl_error_bit::kInvalidEnum:
566 return GL_INVALID_ENUM;
567 case gl_error_bit::kInvalidValue:
568 return GL_INVALID_VALUE;
569 case gl_error_bit::kInvalidOperation:
570 return GL_INVALID_OPERATION;
571 case gl_error_bit::kOutOfMemory:
572 return GL_OUT_OF_MEMORY;
573 case gl_error_bit::kInvalidFrameBufferOperation:
574 return GL_INVALID_FRAMEBUFFER_OPERATION;
581 uint32 GLES2Util::IndexToGLFaceTarget(int index) {
582 static uint32 faces[] = {
583 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
584 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
585 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
586 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
587 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
588 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
593 uint32 GLES2Util::GetPreferredGLReadPixelsFormat(uint32 internal_format) {
594 switch (internal_format) {
606 uint32 GLES2Util::GetPreferredGLReadPixelsType(
607 uint32 internal_format, uint32 texture_type) {
608 switch (internal_format) {
614 return GL_HALF_FLOAT_OES;
617 // Unsized internal format, check the type
618 switch (texture_type) {
620 case GL_HALF_FLOAT_OES:
623 return GL_UNSIGNED_BYTE;
626 return GL_UNSIGNED_BYTE;
630 uint32 GLES2Util::GetChannelsForFormat(int format) {
633 case GL_ALPHA16F_EXT:
634 case GL_ALPHA32F_EXT:
638 case GL_LUMINANCE_ALPHA:
655 case GL_DEPTH_COMPONENT32_OES:
656 case GL_DEPTH_COMPONENT24_OES:
657 case GL_DEPTH_COMPONENT16:
658 case GL_DEPTH_COMPONENT:
660 case GL_STENCIL_INDEX8:
662 case GL_DEPTH_STENCIL_OES:
663 case GL_DEPTH24_STENCIL8_OES:
664 return kDepth | kStencil;
670 uint32 GLES2Util::GetChannelsNeededForAttachmentType(
671 int type, uint32 max_color_attachments) {
673 case GL_DEPTH_ATTACHMENT:
675 case GL_STENCIL_ATTACHMENT:
678 if (type >= GL_COLOR_ATTACHMENT0 &&
679 type < static_cast<int>(
680 GL_COLOR_ATTACHMENT0 + max_color_attachments)) {
687 std::string GLES2Util::GetStringEnum(uint32 value) {
688 const EnumToString* entry = enum_to_string_table_;
689 const EnumToString* end = entry + enum_to_string_table_len_;
690 for (;entry < end; ++entry) {
691 if (value == entry->value) {
695 std::stringstream ss;
697 ss.width(value < 0x10000 ? 4 : 8);
698 ss << std::hex << value;
699 return "0x" + ss.str();
702 std::string GLES2Util::GetStringError(uint32 value) {
703 static EnumToString string_table[] = {
704 { GL_NONE, "GL_NONE" },
706 return GLES2Util::GetQualifiedEnumString(
707 string_table, arraysize(string_table), value);
710 std::string GLES2Util::GetStringBool(uint32 value) {
711 return value ? "GL_TRUE" : "GL_FALSE";
714 std::string GLES2Util::GetQualifiedEnumString(
715 const EnumToString* table, size_t count, uint32 value) {
716 for (const EnumToString* end = table + count; table < end; ++table) {
717 if (table->value == value) {
721 return GetStringEnum(value);
724 bool GLES2Util::ParseUniformName(
725 const std::string& name,
728 bool* getting_array) {
729 bool getting_array_location = false;
730 size_t open_pos = std::string::npos;
732 if (name[name.size() - 1] == ']') {
733 if (name.size() < 3) {
736 open_pos = name.find_last_of('[');
737 if (open_pos == std::string::npos ||
738 open_pos >= name.size() - 2) {
741 size_t last = name.size() - 1;
742 for (size_t pos = open_pos + 1; pos < last; ++pos) {
743 int8 digit = name[pos] - '0';
744 if (digit < 0 || digit > 9) {
747 index = index * 10 + digit;
749 getting_array_location = true;
751 *getting_array = getting_array_location;
752 *element_index = index;
753 *array_pos = open_pos;
760 const int32 kAlphaSize = 0x3021; // EGL_ALPHA_SIZE
761 const int32 kBlueSize = 0x3022; // EGL_BLUE_SIZE
762 const int32 kGreenSize = 0x3023; // EGL_GREEN_SIZE
763 const int32 kRedSize = 0x3024; // EGL_RED_SIZE
764 const int32 kDepthSize = 0x3025; // EGL_DEPTH_SIZE
765 const int32 kStencilSize = 0x3026; // EGL_STENCIL_SIZE
766 const int32 kSamples = 0x3031; // EGL_SAMPLES
767 const int32 kSampleBuffers = 0x3032; // EGL_SAMPLE_BUFFERS
768 const int32 kNone = 0x3038; // EGL_NONE
769 const int32 kSwapBehavior = 0x3093; // EGL_SWAP_BEHAVIOR
770 const int32 kBufferPreserved = 0x3094; // EGL_BUFFER_PRESERVED
771 const int32 kBufferDestroyed = 0x3095; // EGL_BUFFER_DESTROYED
774 const int32 kShareResources = 0x10000;
775 const int32 kBindGeneratesResource = 0x10001;
776 const int32 kFailIfMajorPerfCaveat = 0x10002;
780 ContextCreationAttribHelper::ContextCreationAttribHelper()
789 buffer_preserved_(true),
790 share_resources_(false),
791 bind_generates_resource_(true),
792 fail_if_major_perf_caveat_(false) {
795 void ContextCreationAttribHelper::Serialize(std::vector<int32>* attribs) {
796 if (alpha_size_ != -1) {
797 attribs->push_back(kAlphaSize);
798 attribs->push_back(alpha_size_);
800 if (blue_size_ != -1) {
801 attribs->push_back(kBlueSize);
802 attribs->push_back(blue_size_);
804 if (green_size_ != -1) {
805 attribs->push_back(kGreenSize);
806 attribs->push_back(green_size_);
808 if (red_size_ != -1) {
809 attribs->push_back(kRedSize);
810 attribs->push_back(red_size_);
812 if (depth_size_ != -1) {
813 attribs->push_back(kDepthSize);
814 attribs->push_back(depth_size_);
816 if (stencil_size_ != -1) {
817 attribs->push_back(kStencilSize);
818 attribs->push_back(stencil_size_);
820 if (samples_ != -1) {
821 attribs->push_back(kSamples);
822 attribs->push_back(samples_);
824 if (sample_buffers_ != -1) {
825 attribs->push_back(kSampleBuffers);
826 attribs->push_back(sample_buffers_);
828 attribs->push_back(kSwapBehavior);
829 attribs->push_back(buffer_preserved_ ? kBufferPreserved : kBufferDestroyed);
830 attribs->push_back(kShareResources);
831 attribs->push_back(share_resources_ ? 1 : 0);
832 attribs->push_back(kBindGeneratesResource);
833 attribs->push_back(bind_generates_resource_ ? 1 : 0);
834 attribs->push_back(kFailIfMajorPerfCaveat);
835 attribs->push_back(fail_if_major_perf_caveat_ ? 1 : 0);
836 attribs->push_back(kNone);
839 bool ContextCreationAttribHelper::Parse(const std::vector<int32>& attribs) {
840 for (size_t i = 0; i < attribs.size(); i += 2) {
841 const int32 attrib = attribs[i];
842 if (i + 1 >= attribs.size()) {
843 if (attrib == kNone) {
847 DLOG(ERROR) << "Missing value after context creation attribute: "
852 const int32 value = attribs[i+1];
870 stencil_size_ = value;
876 sample_buffers_ = value;
879 buffer_preserved_ = value == kBufferPreserved;
881 case kShareResources:
882 share_resources_ = value != 0;
884 case kBindGeneratesResource:
885 bind_generates_resource_ = value != 0;
887 case kFailIfMajorPerfCaveat:
888 fail_if_major_perf_caveat_ = value != 0;
891 // Terminate list, even if more attributes.
894 DLOG(ERROR) << "Invalid context creation attribute: " << attrib;
902 #include "gpu/command_buffer/common/gles2_cmd_utils_implementation_autogen.h"