Android Chromoting: Remove exit-fullscreen button.
[chromium-blink-merge.git] / gpu / command_buffer / common / gles2_cmd_utils.cc
blobcaf30af3e99d8abad139ab462f2c966df9442ef4
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.
8 #include <sstream>
9 #include <GLES2/gl2.h>
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"
18 namespace gpu {
19 namespace gles2 {
21 namespace gl_error_bit {
22 enum GLErrorBit {
23 kNoError = 0,
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 {
34 switch (id) {
35 // -- glGetBooleanv, glGetFloatv, glGetIntergerv
36 case GL_ACTIVE_TEXTURE:
37 return 1;
38 case GL_ALIASED_LINE_WIDTH_RANGE:
39 return 2;
40 case GL_ALIASED_POINT_SIZE_RANGE:
41 return 2;
42 case GL_ALPHA_BITS:
43 return 1;
44 case GL_ARRAY_BUFFER_BINDING:
45 return 1;
46 case GL_BLEND:
47 return 1;
48 case GL_BLEND_COLOR:
49 return 4;
50 case GL_BLEND_DST_ALPHA:
51 return 1;
52 case GL_BLEND_DST_RGB:
53 return 1;
54 case GL_BLEND_EQUATION_ALPHA:
55 return 1;
56 case GL_BLEND_EQUATION_RGB:
57 return 1;
58 case GL_BLEND_SRC_ALPHA:
59 return 1;
60 case GL_BLEND_SRC_RGB:
61 return 1;
62 case GL_BLUE_BITS:
63 return 1;
64 case GL_COLOR_CLEAR_VALUE:
65 return 4;
66 case GL_COLOR_WRITEMASK:
67 return 4;
68 case GL_COMPRESSED_TEXTURE_FORMATS:
69 return num_compressed_texture_formats_;
70 case GL_CULL_FACE:
71 return 1;
72 case GL_CULL_FACE_MODE:
73 return 1;
74 case GL_CURRENT_PROGRAM:
75 return 1;
76 case GL_DEPTH_BITS:
77 return 1;
78 case GL_DEPTH_CLEAR_VALUE:
79 return 1;
80 case GL_DEPTH_FUNC:
81 return 1;
82 case GL_DEPTH_RANGE:
83 return 2;
84 case GL_DEPTH_TEST:
85 return 1;
86 case GL_DEPTH_WRITEMASK:
87 return 1;
88 case GL_DITHER:
89 return 1;
90 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
91 return 1;
92 case GL_FRAMEBUFFER_BINDING:
93 return 1;
94 case GL_FRONT_FACE:
95 return 1;
96 case GL_GENERATE_MIPMAP_HINT:
97 return 1;
98 case GL_GREEN_BITS:
99 return 1;
100 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
101 return 1;
102 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
103 return 1;
104 case GL_LINE_WIDTH:
105 return 1;
106 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
107 return 1;
108 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
109 return 1;
110 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
111 return 1;
112 case GL_MAX_RENDERBUFFER_SIZE:
113 return 1;
114 case GL_MAX_TEXTURE_IMAGE_UNITS:
115 return 1;
116 case GL_MAX_TEXTURE_SIZE:
117 return 1;
118 case GL_MAX_VARYING_VECTORS:
119 return 1;
120 case GL_MAX_VERTEX_ATTRIBS:
121 return 1;
122 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
123 return 1;
124 case GL_MAX_VERTEX_UNIFORM_VECTORS:
125 return 1;
126 case GL_MAX_VIEWPORT_DIMS:
127 return 2;
128 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
129 return 1;
130 case GL_NUM_SHADER_BINARY_FORMATS:
131 return 1;
132 case GL_PACK_ALIGNMENT:
133 return 1;
134 case GL_PACK_REVERSE_ROW_ORDER_ANGLE:
135 return 1;
136 case GL_POLYGON_OFFSET_FACTOR:
137 return 1;
138 case GL_POLYGON_OFFSET_FILL:
139 return 1;
140 case GL_POLYGON_OFFSET_UNITS:
141 return 1;
142 case GL_RED_BITS:
143 return 1;
144 case GL_RENDERBUFFER_BINDING:
145 return 1;
146 case GL_SAMPLE_BUFFERS:
147 return 1;
148 case GL_SAMPLE_COVERAGE_INVERT:
149 return 1;
150 case GL_SAMPLE_COVERAGE_VALUE:
151 return 1;
152 case GL_SAMPLES:
153 return 1;
154 case GL_SCISSOR_BOX:
155 return 4;
156 case GL_SCISSOR_TEST:
157 return 1;
158 case GL_SHADER_BINARY_FORMATS:
159 return num_shader_binary_formats_;
160 case GL_SHADER_COMPILER:
161 return 1;
162 case GL_STENCIL_BACK_FAIL:
163 return 1;
164 case GL_STENCIL_BACK_FUNC:
165 return 1;
166 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
167 return 1;
168 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
169 return 1;
170 case GL_STENCIL_BACK_REF:
171 return 1;
172 case GL_STENCIL_BACK_VALUE_MASK:
173 return 1;
174 case GL_STENCIL_BACK_WRITEMASK:
175 return 1;
176 case GL_STENCIL_BITS:
177 return 1;
178 case GL_STENCIL_CLEAR_VALUE:
179 return 1;
180 case GL_STENCIL_FAIL:
181 return 1;
182 case GL_STENCIL_FUNC:
183 return 1;
184 case GL_STENCIL_PASS_DEPTH_FAIL:
185 return 1;
186 case GL_STENCIL_PASS_DEPTH_PASS:
187 return 1;
188 case GL_STENCIL_REF:
189 return 1;
190 case GL_STENCIL_TEST:
191 return 1;
192 case GL_STENCIL_VALUE_MASK:
193 return 1;
194 case GL_STENCIL_WRITEMASK:
195 return 1;
196 case GL_SUBPIXEL_BITS:
197 return 1;
198 case GL_TEXTURE_BINDING_2D:
199 return 1;
200 case GL_TEXTURE_BINDING_CUBE_MAP:
201 return 1;
202 case GL_TEXTURE_BINDING_EXTERNAL_OES:
203 return 1;
204 case GL_TEXTURE_BINDING_RECTANGLE_ARB:
205 return 1;
206 case GL_TEXTURE_IMMUTABLE_FORMAT_EXT:
207 return 1;
208 case GL_UNPACK_ALIGNMENT:
209 return 1;
210 case GL_VIEWPORT:
211 return 4;
212 // -- glGetBooleanv, glGetFloatv, glGetIntergerv with
213 // GL_CHROMIUM_framebuffer_multisample
214 case GL_MAX_SAMPLES_EXT:
215 return 1;
216 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
217 return 1;
219 // -- glGetBufferParameteriv
220 case GL_BUFFER_SIZE:
221 return 1;
222 case GL_BUFFER_USAGE:
223 return 1;
225 // -- glGetFramebufferAttachmentParameteriv
226 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE:
227 return 1;
228 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME:
229 return 1;
230 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL:
231 return 1;
232 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE:
233 return 1;
234 // -- glGetFramebufferAttachmentParameteriv with
235 // GL_EXT_multisampled_render_to_texture
236 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_SAMPLES_EXT:
237 return 1;
238 // -- glGetFramebufferAttachmentParameteriv with
239 // GL_EXT_sRGB
240 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING_EXT:
241 return 1;
243 // -- glGetProgramiv
244 case GL_DELETE_STATUS:
245 return 1;
246 case GL_LINK_STATUS:
247 return 1;
248 case GL_VALIDATE_STATUS:
249 return 1;
250 case GL_INFO_LOG_LENGTH:
251 return 1;
252 case GL_ATTACHED_SHADERS:
253 return 1;
254 case GL_ACTIVE_ATTRIBUTES:
255 return 1;
256 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
257 return 1;
258 case GL_ACTIVE_UNIFORMS:
259 return 1;
260 case GL_ACTIVE_UNIFORM_MAX_LENGTH:
261 return 1;
264 // -- glGetRenderbufferAttachmentParameteriv
265 case GL_RENDERBUFFER_WIDTH:
266 return 1;
267 case GL_RENDERBUFFER_HEIGHT:
268 return 1;
269 case GL_RENDERBUFFER_INTERNAL_FORMAT:
270 return 1;
271 case GL_RENDERBUFFER_RED_SIZE:
272 return 1;
273 case GL_RENDERBUFFER_GREEN_SIZE:
274 return 1;
275 case GL_RENDERBUFFER_BLUE_SIZE:
276 return 1;
277 case GL_RENDERBUFFER_ALPHA_SIZE:
278 return 1;
279 case GL_RENDERBUFFER_DEPTH_SIZE:
280 return 1;
281 case GL_RENDERBUFFER_STENCIL_SIZE:
282 return 1;
283 // -- glGetRenderbufferAttachmentParameteriv with
284 // GL_EXT_multisampled_render_to_texture
285 case GL_RENDERBUFFER_SAMPLES_EXT:
286 return 1;
288 // -- glGetShaderiv
289 case GL_SHADER_TYPE:
290 return 1;
291 // Already defined under glGetFramebufferAttachemntParameteriv.
292 // case GL_DELETE_STATUS:
293 // return 1;
294 case GL_COMPILE_STATUS:
295 return 1;
296 // Already defined under glGetFramebufferAttachemntParameteriv.
297 // case GL_INFO_LOG_LENGTH:
298 // return 1;
299 case GL_SHADER_SOURCE_LENGTH:
300 return 1;
301 case GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE:
302 return 1;
304 // -- glGetTexParameterfv, glGetTexParameteriv
305 case GL_TEXTURE_MAG_FILTER:
306 return 1;
307 case GL_TEXTURE_MIN_FILTER:
308 return 1;
309 case GL_TEXTURE_WRAP_S:
310 return 1;
311 case GL_TEXTURE_WRAP_T:
312 return 1;
313 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
314 return 1;
316 // -- glGetVertexAttribfv, glGetVertexAttribiv
317 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
318 return 1;
319 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
320 return 1;
321 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
322 return 1;
323 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
324 return 1;
325 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
326 return 1;
327 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
328 return 1;
329 case GL_CURRENT_VERTEX_ATTRIB:
330 return 4;
332 // -- glGetSynciv
333 case GL_OBJECT_TYPE:
334 return 1;
335 case GL_SYNC_STATUS:
336 return 1;
337 case GL_SYNC_CONDITION:
338 return 1;
339 case GL_SYNC_FLAGS:
340 return 1;
342 // -- glHint with GL_OES_standard_derivatives
343 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
344 return 1;
346 // Chromium internal bind_generates_resource query
347 case GL_BIND_GENERATES_RESOURCE_CHROMIUM:
348 return 1;
350 // bad enum
351 default:
352 return 0;
356 namespace {
358 // Return the number of elements per group of a specified format.
359 int ElementsPerGroup(int format, int type) {
360 switch (type) {
361 case GL_UNSIGNED_SHORT_5_6_5:
362 case GL_UNSIGNED_SHORT_4_4_4_4:
363 case GL_UNSIGNED_SHORT_5_5_5_1:
364 case GL_UNSIGNED_INT_24_8_OES:
365 case GL_UNSIGNED_INT_2_10_10_10_REV:
366 case GL_UNSIGNED_INT_10F_11F_11F_REV:
367 case GL_UNSIGNED_INT_5_9_9_9_REV:
368 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
369 return 1;
370 default:
371 break;
374 switch (format) {
375 case GL_RGB:
376 case GL_RGB_INTEGER:
377 case GL_SRGB_EXT:
378 return 3;
379 case GL_LUMINANCE_ALPHA:
380 case GL_RG_EXT:
381 case GL_RG_INTEGER:
382 return 2;
383 case GL_RGBA:
384 case GL_RGBA_INTEGER:
385 case GL_BGRA_EXT:
386 case GL_SRGB_ALPHA_EXT:
387 return 4;
388 case GL_ALPHA:
389 case GL_LUMINANCE:
390 case GL_DEPTH_COMPONENT:
391 case GL_DEPTH_COMPONENT24_OES:
392 case GL_DEPTH_COMPONENT32_OES:
393 case GL_DEPTH_COMPONENT16:
394 case GL_DEPTH24_STENCIL8_OES:
395 case GL_DEPTH_STENCIL_OES:
396 case GL_RED_EXT:
397 case GL_RED_INTEGER:
398 return 1;
399 default:
400 return 0;
404 // Return the number of bytes per element, based on the element type.
405 int BytesPerElement(int type) {
406 switch (type) {
407 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
408 return 8;
409 case GL_FLOAT:
410 case GL_UNSIGNED_INT_24_8_OES:
411 case GL_UNSIGNED_INT:
412 case GL_UNSIGNED_INT_2_10_10_10_REV:
413 case GL_UNSIGNED_INT_10F_11F_11F_REV:
414 case GL_UNSIGNED_INT_5_9_9_9_REV:
415 return 4;
416 case GL_HALF_FLOAT_OES:
417 case GL_UNSIGNED_SHORT:
418 case GL_SHORT:
419 case GL_UNSIGNED_SHORT_5_6_5:
420 case GL_UNSIGNED_SHORT_4_4_4_4:
421 case GL_UNSIGNED_SHORT_5_5_5_1:
422 return 2;
423 case GL_UNSIGNED_BYTE:
424 case GL_BYTE:
425 return 1;
426 default:
427 return 0;
431 } // anonymous namespace
433 uint32 GLES2Util::ComputeImageGroupSize(int format, int type) {
434 int bytes_per_element = BytesPerElement(type);
435 DCHECK_GE(8, bytes_per_element);
436 int elements_per_group = ElementsPerGroup(format, type);
437 DCHECK_GE(4, elements_per_group);
438 return bytes_per_element * elements_per_group;
441 bool GLES2Util::ComputeImagePaddedRowSize(
442 int width, int format, int type, int unpack_alignment,
443 uint32* padded_row_size) {
444 DCHECK(unpack_alignment == 1 || unpack_alignment == 2 ||
445 unpack_alignment == 4 || unpack_alignment == 8);
446 uint32 bytes_per_group = ComputeImageGroupSize(format, type);
447 uint32 unpadded_row_size;
448 if (!SafeMultiplyUint32(width, bytes_per_group, &unpadded_row_size)) {
449 return false;
451 uint32 temp;
452 if (!SafeAddUint32(unpadded_row_size, unpack_alignment - 1, &temp)) {
453 return false;
455 *padded_row_size = (temp / unpack_alignment) * unpack_alignment;
456 return true;
459 // Returns the amount of data glTexImage*D or glTexSubImage*D will access.
460 bool GLES2Util::ComputeImageDataSizes(
461 int width, int height, int depth, int format, int type,
462 int unpack_alignment, uint32* size, uint32* ret_unpadded_row_size,
463 uint32* ret_padded_row_size) {
464 DCHECK(unpack_alignment == 1 || unpack_alignment == 2 ||
465 unpack_alignment == 4 || unpack_alignment == 8);
466 uint32 bytes_per_group = ComputeImageGroupSize(format, type);
467 uint32 row_size;
468 if (!SafeMultiplyUint32(width, bytes_per_group, &row_size)) {
469 return false;
471 uint32 num_of_rows;
472 if (!SafeMultiplyUint32(height, depth, &num_of_rows)) {
473 return false;
475 if (num_of_rows > 1) {
476 uint32 temp;
477 if (!SafeAddUint32(row_size, unpack_alignment - 1, &temp)) {
478 return false;
480 uint32 padded_row_size = (temp / unpack_alignment) * unpack_alignment;
481 uint32 size_of_all_but_last_row;
482 if (!SafeMultiplyUint32((num_of_rows - 1), padded_row_size,
483 &size_of_all_but_last_row)) {
484 return false;
486 if (!SafeAddUint32(size_of_all_but_last_row, row_size, size)) {
487 return false;
489 if (ret_padded_row_size) {
490 *ret_padded_row_size = padded_row_size;
492 } else {
493 *size = row_size;
494 if (ret_padded_row_size) {
495 *ret_padded_row_size = row_size;
498 if (ret_unpadded_row_size) {
499 *ret_unpadded_row_size = row_size;
502 return true;
505 size_t GLES2Util::RenderbufferBytesPerPixel(int format) {
506 switch (format) {
507 case GL_STENCIL_INDEX8:
508 return 1;
509 case GL_RGBA4:
510 case GL_RGB565:
511 case GL_RGB5_A1:
512 case GL_DEPTH_COMPONENT16:
513 return 2;
514 case GL_RGB:
515 case GL_RGBA:
516 case GL_DEPTH24_STENCIL8_OES:
517 case GL_RGB8_OES:
518 case GL_RGBA8_OES:
519 case GL_DEPTH_COMPONENT24_OES:
520 return 4;
521 default:
522 return 0;
526 uint32 GLES2Util::GetGLDataTypeSizeForUniforms(int type) {
527 switch (type) {
528 case GL_FLOAT:
529 return sizeof(GLfloat); // NOLINT
530 case GL_FLOAT_VEC2:
531 return sizeof(GLfloat) * 2; // NOLINT
532 case GL_FLOAT_VEC3:
533 return sizeof(GLfloat) * 3; // NOLINT
534 case GL_FLOAT_VEC4:
535 return sizeof(GLfloat) * 4; // NOLINT
536 case GL_INT:
537 return sizeof(GLint); // NOLINT
538 case GL_INT_VEC2:
539 return sizeof(GLint) * 2; // NOLINT
540 case GL_INT_VEC3:
541 return sizeof(GLint) * 3; // NOLINT
542 case GL_INT_VEC4:
543 return sizeof(GLint) * 4; // NOLINT
544 case GL_BOOL:
545 return sizeof(GLint); // NOLINT
546 case GL_BOOL_VEC2:
547 return sizeof(GLint) * 2; // NOLINT
548 case GL_BOOL_VEC3:
549 return sizeof(GLint) * 3; // NOLINT
550 case GL_BOOL_VEC4:
551 return sizeof(GLint) * 4; // NOLINT
552 case GL_FLOAT_MAT2:
553 return sizeof(GLfloat) * 2 * 2; // NOLINT
554 case GL_FLOAT_MAT3:
555 return sizeof(GLfloat) * 3 * 3; // NOLINT
556 case GL_FLOAT_MAT4:
557 return sizeof(GLfloat) * 4 * 4; // NOLINT
558 case GL_SAMPLER_2D:
559 return sizeof(GLint); // NOLINT
560 case GL_SAMPLER_2D_RECT_ARB:
561 return sizeof(GLint); // NOLINT
562 case GL_SAMPLER_CUBE:
563 return sizeof(GLint); // NOLINT
564 case GL_SAMPLER_EXTERNAL_OES:
565 return sizeof(GLint); // NOLINT
566 default:
567 return 0;
571 size_t GLES2Util::GetGLTypeSizeForTexturesAndBuffers(uint32 type) {
572 switch (type) {
573 case GL_BYTE:
574 return sizeof(GLbyte); // NOLINT
575 case GL_UNSIGNED_BYTE:
576 return sizeof(GLubyte); // NOLINT
577 case GL_SHORT:
578 return sizeof(GLshort); // NOLINT
579 case GL_UNSIGNED_SHORT:
580 return sizeof(GLushort); // NOLINT
581 case GL_INT:
582 return sizeof(GLint); // NOLINT
583 case GL_UNSIGNED_INT:
584 return sizeof(GLuint); // NOLINT
585 case GL_FLOAT:
586 return sizeof(GLfloat); // NOLINT
587 case GL_FIXED:
588 return sizeof(GLfixed); // NOLINT
589 default:
590 return 0;
594 uint32 GLES2Util::GLErrorToErrorBit(uint32 error) {
595 switch (error) {
596 case GL_INVALID_ENUM:
597 return gl_error_bit::kInvalidEnum;
598 case GL_INVALID_VALUE:
599 return gl_error_bit::kInvalidValue;
600 case GL_INVALID_OPERATION:
601 return gl_error_bit::kInvalidOperation;
602 case GL_OUT_OF_MEMORY:
603 return gl_error_bit::kOutOfMemory;
604 case GL_INVALID_FRAMEBUFFER_OPERATION:
605 return gl_error_bit::kInvalidFrameBufferOperation;
606 case GL_CONTEXT_LOST_KHR:
607 return gl_error_bit::kContextLost;
608 default:
609 NOTREACHED();
610 return gl_error_bit::kNoError;
614 uint32 GLES2Util::GLErrorBitToGLError(uint32 error_bit) {
615 switch (error_bit) {
616 case gl_error_bit::kInvalidEnum:
617 return GL_INVALID_ENUM;
618 case gl_error_bit::kInvalidValue:
619 return GL_INVALID_VALUE;
620 case gl_error_bit::kInvalidOperation:
621 return GL_INVALID_OPERATION;
622 case gl_error_bit::kOutOfMemory:
623 return GL_OUT_OF_MEMORY;
624 case gl_error_bit::kInvalidFrameBufferOperation:
625 return GL_INVALID_FRAMEBUFFER_OPERATION;
626 case gl_error_bit::kContextLost:
627 return GL_CONTEXT_LOST_KHR;
628 default:
629 NOTREACHED();
630 return GL_NO_ERROR;
634 uint32 GLES2Util::IndexToGLFaceTarget(int index) {
635 static uint32 faces[] = {
636 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
637 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
638 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
639 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
640 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
641 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
643 return faces[index];
646 size_t GLES2Util::GLTargetToFaceIndex(uint32 target) {
647 switch (target) {
648 case GL_TEXTURE_2D:
649 case GL_TEXTURE_EXTERNAL_OES:
650 case GL_TEXTURE_RECTANGLE_ARB:
651 return 0;
652 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
653 return 0;
654 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
655 return 1;
656 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
657 return 2;
658 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
659 return 3;
660 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
661 return 4;
662 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
663 return 5;
664 default:
665 NOTREACHED();
666 return 0;
670 uint32 GLES2Util::GetPreferredGLReadPixelsFormat(uint32 internal_format) {
671 switch (internal_format) {
672 case GL_RGB16F_EXT:
673 case GL_RGB32F_EXT:
674 return GL_RGB;
675 case GL_RGBA16F_EXT:
676 case GL_RGBA32F_EXT:
677 return GL_RGBA;
678 default:
679 return GL_RGBA;
683 uint32 GLES2Util::GetPreferredGLReadPixelsType(
684 uint32 internal_format, uint32 texture_type) {
685 switch (internal_format) {
686 case GL_RGBA32F_EXT:
687 case GL_RGB32F_EXT:
688 return GL_FLOAT;
689 case GL_RGBA16F_EXT:
690 case GL_RGB16F_EXT:
691 return GL_HALF_FLOAT_OES;
692 case GL_RGBA:
693 case GL_RGB:
694 // Unsized internal format, check the type
695 switch (texture_type) {
696 case GL_FLOAT:
697 case GL_HALF_FLOAT_OES:
698 return GL_FLOAT;
699 default:
700 return GL_UNSIGNED_BYTE;
702 default:
703 return GL_UNSIGNED_BYTE;
707 uint32 GLES2Util::GetChannelsForFormat(int format) {
708 switch (format) {
709 case GL_ALPHA:
710 case GL_ALPHA16F_EXT:
711 case GL_ALPHA32F_EXT:
712 return kAlpha;
713 case GL_LUMINANCE:
714 return kRGB;
715 case GL_LUMINANCE_ALPHA:
716 return kRGBA;
717 case GL_RGB:
718 case GL_RGB8_OES:
719 case GL_RGB565:
720 case GL_RGB16F_EXT:
721 case GL_RGB32F_EXT:
722 case GL_SRGB_EXT:
723 case GL_SRGB8:
724 case GL_RGB8_SNORM:
725 case GL_R11F_G11F_B10F:
726 case GL_RGB9_E5:
727 case GL_RGB8UI:
728 case GL_RGB8I:
729 case GL_RGB16UI:
730 case GL_RGB16I:
731 case GL_RGB32UI:
732 case GL_RGB32I:
733 return kRGB;
734 case GL_BGRA_EXT:
735 case GL_BGRA8_EXT:
736 case GL_RGBA16F_EXT:
737 case GL_RGBA32F_EXT:
738 case GL_RGBA:
739 case GL_RGBA8_OES:
740 case GL_RGBA4:
741 case GL_RGB5_A1:
742 case GL_SRGB_ALPHA_EXT:
743 case GL_SRGB8_ALPHA8_EXT:
744 case GL_RGBA8_SNORM:
745 case GL_RGB10_A2:
746 case GL_RGBA8UI:
747 case GL_RGBA8I:
748 case GL_RGB10_A2UI:
749 case GL_RGBA16UI:
750 case GL_RGBA16I:
751 case GL_RGBA32UI:
752 case GL_RGBA32I:
753 return kRGBA;
754 case GL_DEPTH_COMPONENT32_OES:
755 case GL_DEPTH_COMPONENT24_OES:
756 case GL_DEPTH_COMPONENT16:
757 case GL_DEPTH_COMPONENT:
758 case GL_DEPTH_COMPONENT32F:
759 return kDepth;
760 case GL_STENCIL_INDEX8:
761 return kStencil;
762 case GL_DEPTH_STENCIL_OES:
763 case GL_DEPTH24_STENCIL8_OES:
764 case GL_DEPTH32F_STENCIL8:
765 return kDepth | kStencil;
766 case GL_RED_EXT:
767 case GL_R8:
768 case GL_R8_SNORM:
769 case GL_R16F:
770 case GL_R32F:
771 case GL_R8UI:
772 case GL_R8I:
773 case GL_R16UI:
774 case GL_R16I:
775 case GL_R32UI:
776 case GL_R32I:
777 return kRed;
778 case GL_RG_EXT:
779 case GL_RG8:
780 case GL_RG8_SNORM:
781 case GL_RG16F:
782 case GL_RG32F:
783 case GL_RG8UI:
784 case GL_RG8I:
785 case GL_RG16UI:
786 case GL_RG16I:
787 case GL_RG32UI:
788 case GL_RG32I:
789 return kRed | kGreen;
790 default:
791 return 0x0000;
795 uint32 GLES2Util::GetChannelsNeededForAttachmentType(
796 int type, uint32 max_color_attachments) {
797 switch (type) {
798 case GL_DEPTH_ATTACHMENT:
799 return kDepth;
800 case GL_STENCIL_ATTACHMENT:
801 return kStencil;
802 default:
803 if (type >= GL_COLOR_ATTACHMENT0 &&
804 type < static_cast<int>(
805 GL_COLOR_ATTACHMENT0 + max_color_attachments)) {
806 return kRGBA;
808 return 0x0000;
812 std::string GLES2Util::GetStringEnum(uint32 value) {
813 const EnumToString* entry = enum_to_string_table_;
814 const EnumToString* end = entry + enum_to_string_table_len_;
815 for (;entry < end; ++entry) {
816 if (value == entry->value) {
817 return entry->name;
820 std::stringstream ss;
821 ss.fill('0');
822 ss.width(value < 0x10000 ? 4 : 8);
823 ss << std::hex << value;
824 return "0x" + ss.str();
827 std::string GLES2Util::GetStringError(uint32 value) {
828 static EnumToString string_table[] = {
829 { GL_NONE, "GL_NONE" },
831 return GLES2Util::GetQualifiedEnumString(
832 string_table, arraysize(string_table), value);
835 std::string GLES2Util::GetStringBool(uint32 value) {
836 return value ? "GL_TRUE" : "GL_FALSE";
839 std::string GLES2Util::GetQualifiedEnumString(
840 const EnumToString* table, size_t count, uint32 value) {
841 for (const EnumToString* end = table + count; table < end; ++table) {
842 if (table->value == value) {
843 return table->name;
846 return GetStringEnum(value);
849 bool GLES2Util::ParseUniformName(
850 const std::string& name,
851 size_t* array_pos,
852 int* element_index,
853 bool* getting_array) {
854 if (name.empty())
855 return false;
856 bool getting_array_location = false;
857 size_t open_pos = std::string::npos;
858 base::CheckedNumeric<int> index = 0;
859 if (name[name.size() - 1] == ']') {
860 if (name.size() < 3) {
861 return false;
863 open_pos = name.find_last_of('[');
864 if (open_pos == std::string::npos ||
865 open_pos >= name.size() - 2) {
866 return false;
868 size_t last = name.size() - 1;
869 for (size_t pos = open_pos + 1; pos < last; ++pos) {
870 int8 digit = name[pos] - '0';
871 if (digit < 0 || digit > 9) {
872 return false;
874 index = index * 10 + digit;
876 if (!index.IsValid()) {
877 return false;
879 getting_array_location = true;
881 *getting_array = getting_array_location;
882 *element_index = index.ValueOrDie();
883 *array_pos = open_pos;
884 return true;
887 size_t GLES2Util::CalcClearBufferivDataCount(int buffer) {
888 switch (buffer) {
889 case GL_COLOR:
890 return 4;
891 case GL_STENCIL:
892 return 1;
893 default:
894 return 0;
898 size_t GLES2Util::CalcClearBufferfvDataCount(int buffer) {
899 switch (buffer) {
900 case GL_COLOR:
901 return 4;
902 case GL_DEPTH:
903 return 1;
904 default:
905 return 0;
909 // static
910 void GLES2Util::MapUint64ToTwoUint32(
911 uint64_t v64, uint32_t* v32_0, uint32_t* v32_1) {
912 DCHECK(v32_0 && v32_1);
913 *v32_0 = static_cast<uint32_t>(v64 & 0xFFFFFFFF);
914 *v32_1 = static_cast<uint32_t>((v64 & 0xFFFFFFFF00000000) >> 32);
917 // static
918 uint64_t GLES2Util::MapTwoUint32ToUint64(uint32_t v32_0, uint32_t v32_1) {
919 uint64_t v64 = v32_1;
920 return (v64 << 32) | v32_0;
923 // static
924 uint32_t GLES2Util::MapBufferTargetToBindingEnum(uint32_t target) {
925 switch (target) {
926 case GL_ARRAY_BUFFER:
927 return GL_ARRAY_BUFFER_BINDING;
928 case GL_COPY_READ_BUFFER:
929 return GL_COPY_READ_BUFFER_BINDING;
930 case GL_COPY_WRITE_BUFFER:
931 return GL_COPY_WRITE_BUFFER_BINDING;
932 case GL_ELEMENT_ARRAY_BUFFER:
933 return GL_ELEMENT_ARRAY_BUFFER_BINDING;
934 case GL_PIXEL_PACK_BUFFER:
935 return GL_PIXEL_PACK_BUFFER_BINDING;
936 case GL_PIXEL_UNPACK_BUFFER:
937 return GL_PIXEL_UNPACK_BUFFER_BINDING;
938 case GL_TRANSFORM_FEEDBACK_BUFFER:
939 return GL_TRANSFORM_FEEDBACK_BUFFER_BINDING;
940 case GL_UNIFORM_BUFFER:
941 return GL_UNIFORM_BUFFER_BINDING;
942 default:
943 return 0;
948 namespace {
950 // WebGraphicsContext3DCommandBufferImpl configuration attributes. Those in
951 // the 16-bit range are the same as used by EGL. Those outside the 16-bit range
952 // are unique to Chromium. Attributes are matched using a closest fit algorithm.
954 // From <EGL/egl.h>.
955 const int32 kAlphaSize = 0x3021; // EGL_ALPHA_SIZE
956 const int32 kBlueSize = 0x3022; // EGL_BLUE_SIZE
957 const int32 kGreenSize = 0x3023; // EGL_GREEN_SIZE
958 const int32 kRedSize = 0x3024; // EGL_RED_SIZE
959 const int32 kDepthSize = 0x3025; // EGL_DEPTH_SIZE
960 const int32 kStencilSize = 0x3026; // EGL_STENCIL_SIZE
961 const int32 kSamples = 0x3031; // EGL_SAMPLES
962 const int32 kSampleBuffers = 0x3032; // EGL_SAMPLE_BUFFERS
963 const int32 kNone = 0x3038; // EGL_NONE
964 const int32 kSwapBehavior = 0x3093; // EGL_SWAP_BEHAVIOR
965 const int32 kBufferPreserved = 0x3094; // EGL_BUFFER_PRESERVED
966 const int32 kBufferDestroyed = 0x3095; // EGL_BUFFER_DESTROYED
968 // Chromium only.
969 const int32 kBindGeneratesResource = 0x10000;
970 const int32 kFailIfMajorPerfCaveat = 0x10001;
971 const int32 kLoseContextWhenOutOfMemory = 0x10002;
972 const int32 kES3ContextRequired = 0x10003;
974 } // namespace
976 ContextCreationAttribHelper::ContextCreationAttribHelper()
977 : alpha_size(-1),
978 blue_size(-1),
979 green_size(-1),
980 red_size(-1),
981 depth_size(-1),
982 stencil_size(-1),
983 samples(-1),
984 sample_buffers(-1),
985 buffer_preserved(true),
986 bind_generates_resource(true),
987 fail_if_major_perf_caveat(false),
988 lose_context_when_out_of_memory(false),
989 es3_context_required(false) {}
991 void ContextCreationAttribHelper::Serialize(std::vector<int32>* attribs) const {
992 if (alpha_size != -1) {
993 attribs->push_back(kAlphaSize);
994 attribs->push_back(alpha_size);
996 if (blue_size != -1) {
997 attribs->push_back(kBlueSize);
998 attribs->push_back(blue_size);
1000 if (green_size != -1) {
1001 attribs->push_back(kGreenSize);
1002 attribs->push_back(green_size);
1004 if (red_size != -1) {
1005 attribs->push_back(kRedSize);
1006 attribs->push_back(red_size);
1008 if (depth_size != -1) {
1009 attribs->push_back(kDepthSize);
1010 attribs->push_back(depth_size);
1012 if (stencil_size != -1) {
1013 attribs->push_back(kStencilSize);
1014 attribs->push_back(stencil_size);
1016 if (samples != -1) {
1017 attribs->push_back(kSamples);
1018 attribs->push_back(samples);
1020 if (sample_buffers != -1) {
1021 attribs->push_back(kSampleBuffers);
1022 attribs->push_back(sample_buffers);
1024 attribs->push_back(kSwapBehavior);
1025 attribs->push_back(buffer_preserved ? kBufferPreserved : kBufferDestroyed);
1026 attribs->push_back(kBindGeneratesResource);
1027 attribs->push_back(bind_generates_resource ? 1 : 0);
1028 attribs->push_back(kFailIfMajorPerfCaveat);
1029 attribs->push_back(fail_if_major_perf_caveat ? 1 : 0);
1030 attribs->push_back(kLoseContextWhenOutOfMemory);
1031 attribs->push_back(lose_context_when_out_of_memory ? 1 : 0);
1032 attribs->push_back(kES3ContextRequired);
1033 attribs->push_back(es3_context_required ? 1 : 0);
1034 attribs->push_back(kNone);
1037 bool ContextCreationAttribHelper::Parse(const std::vector<int32>& attribs) {
1038 for (size_t i = 0; i < attribs.size(); i += 2) {
1039 const int32 attrib = attribs[i];
1040 if (i + 1 >= attribs.size()) {
1041 if (attrib == kNone) {
1042 return true;
1045 DLOG(ERROR) << "Missing value after context creation attribute: "
1046 << attrib;
1047 return false;
1050 const int32 value = attribs[i+1];
1051 switch (attrib) {
1052 case kAlphaSize:
1053 alpha_size = value;
1054 break;
1055 case kBlueSize:
1056 blue_size = value;
1057 break;
1058 case kGreenSize:
1059 green_size = value;
1060 break;
1061 case kRedSize:
1062 red_size = value;
1063 break;
1064 case kDepthSize:
1065 depth_size = value;
1066 break;
1067 case kStencilSize:
1068 stencil_size = value;
1069 break;
1070 case kSamples:
1071 samples = value;
1072 break;
1073 case kSampleBuffers:
1074 sample_buffers = value;
1075 break;
1076 case kSwapBehavior:
1077 buffer_preserved = value == kBufferPreserved;
1078 break;
1079 case kBindGeneratesResource:
1080 bind_generates_resource = value != 0;
1081 break;
1082 case kFailIfMajorPerfCaveat:
1083 fail_if_major_perf_caveat = value != 0;
1084 break;
1085 case kLoseContextWhenOutOfMemory:
1086 lose_context_when_out_of_memory = value != 0;
1087 break;
1088 case kES3ContextRequired:
1089 es3_context_required = value != 0;
1090 break;
1091 case kNone:
1092 // Terminate list, even if more attributes.
1093 return true;
1094 default:
1095 DLOG(ERROR) << "Invalid context creation attribute: " << attrib;
1096 return false;
1100 return true;
1103 #include "gpu/command_buffer/common/gles2_cmd_utils_implementation_autogen.h"
1105 } // namespace gles2
1106 } // namespace gpu