Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / gpu / command_buffer / common / gles2_cmd_utils.cc
blobe0ca1ecc650ab81fa3746dde630b9dbb7297c856
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_UNPACK_ALIGNMENT:
207 return 1;
208 case GL_VIEWPORT:
209 return 4;
211 // ES3
212 case GL_COPY_READ_BUFFER_BINDING:
213 return 1;
214 case GL_COPY_WRITE_BUFFER_BINDING:
215 return 1;
216 case GL_PIXEL_PACK_BUFFER_BINDING:
217 return 1;
218 case GL_PIXEL_UNPACK_BUFFER_BINDING:
219 return 1;
220 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
221 return 1;
222 case GL_UNIFORM_BUFFER_BINDING:
223 return 1;
225 // -- glGetBooleanv, glGetFloatv, glGetIntergerv with
226 // GL_CHROMIUM_framebuffer_multisample
227 case GL_MAX_SAMPLES_EXT:
228 return 1;
229 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
230 return 1;
232 // -- glGetBufferParameteriv
233 case GL_BUFFER_SIZE:
234 return 1;
235 case GL_BUFFER_USAGE:
236 return 1;
238 // ES3
239 case GL_BUFFER_MAPPED:
240 return 1;
241 case GL_BUFFER_ACCESS_FLAGS:
242 return 1;
243 case GL_BUFFER_MAP_LENGTH:
244 return 1;
245 case GL_BUFFER_MAP_OFFSET:
246 return 1;
248 // -- glGetFramebufferAttachmentParameteriv
249 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE:
250 return 1;
251 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME:
252 return 1;
253 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL:
254 return 1;
255 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE:
256 return 1;
257 // -- glGetFramebufferAttachmentParameteriv with
258 // GL_EXT_multisampled_render_to_texture
259 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_SAMPLES_EXT:
260 return 1;
261 // -- glGetFramebufferAttachmentParameteriv with
262 // GL_EXT_sRGB
263 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING_EXT:
264 return 1;
266 // -- glGetProgramiv
267 case GL_DELETE_STATUS:
268 return 1;
269 case GL_LINK_STATUS:
270 return 1;
271 case GL_VALIDATE_STATUS:
272 return 1;
273 case GL_INFO_LOG_LENGTH:
274 return 1;
275 case GL_ATTACHED_SHADERS:
276 return 1;
277 case GL_ACTIVE_ATTRIBUTES:
278 return 1;
279 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
280 return 1;
281 case GL_ACTIVE_UNIFORMS:
282 return 1;
283 case GL_ACTIVE_UNIFORM_MAX_LENGTH:
284 return 1;
286 // -- glGetRenderbufferAttachmentParameteriv
287 case GL_RENDERBUFFER_WIDTH:
288 return 1;
289 case GL_RENDERBUFFER_HEIGHT:
290 return 1;
291 case GL_RENDERBUFFER_INTERNAL_FORMAT:
292 return 1;
293 case GL_RENDERBUFFER_RED_SIZE:
294 return 1;
295 case GL_RENDERBUFFER_GREEN_SIZE:
296 return 1;
297 case GL_RENDERBUFFER_BLUE_SIZE:
298 return 1;
299 case GL_RENDERBUFFER_ALPHA_SIZE:
300 return 1;
301 case GL_RENDERBUFFER_DEPTH_SIZE:
302 return 1;
303 case GL_RENDERBUFFER_STENCIL_SIZE:
304 return 1;
305 // -- glGetRenderbufferAttachmentParameteriv with
306 // GL_EXT_multisampled_render_to_texture
307 case GL_RENDERBUFFER_SAMPLES_EXT:
308 return 1;
310 // -- glGetShaderiv
311 case GL_SHADER_TYPE:
312 return 1;
313 // Already defined under glGetFramebufferAttachemntParameteriv.
314 // case GL_DELETE_STATUS:
315 // return 1;
316 case GL_COMPILE_STATUS:
317 return 1;
318 // Already defined under glGetFramebufferAttachemntParameteriv.
319 // case GL_INFO_LOG_LENGTH:
320 // return 1;
321 case GL_SHADER_SOURCE_LENGTH:
322 return 1;
323 case GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE:
324 return 1;
326 // -- glGetTexParameterfv, glGetTexParameteriv
327 case GL_TEXTURE_MAG_FILTER:
328 return 1;
329 case GL_TEXTURE_MIN_FILTER:
330 return 1;
331 case GL_TEXTURE_WRAP_R:
332 return 1;
333 case GL_TEXTURE_WRAP_S:
334 return 1;
335 case GL_TEXTURE_WRAP_T:
336 return 1;
337 case GL_TEXTURE_COMPARE_FUNC:
338 return 1;
339 case GL_TEXTURE_COMPARE_MODE:
340 return 1;
341 case GL_TEXTURE_MAX_LOD:
342 return 1;
343 case GL_TEXTURE_MIN_LOD:
344 return 1;
345 case GL_TEXTURE_BASE_LEVEL:
346 return 1;
347 case GL_TEXTURE_MAX_LEVEL:
348 return 1;
349 case GL_TEXTURE_IMMUTABLE_FORMAT:
350 return 1;
351 case GL_TEXTURE_IMMUTABLE_LEVELS:
352 return 1;
353 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
354 return 1;
356 // -- glGetVertexAttrib
357 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
358 return 1;
359 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
360 return 1;
361 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
362 return 1;
363 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
364 return 1;
365 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
366 return 1;
367 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
368 return 1;
369 case GL_CURRENT_VERTEX_ATTRIB:
370 return 4;
371 case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
372 return 1;
373 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR:
374 return 1;
376 // -- glGetSynciv
377 case GL_OBJECT_TYPE:
378 return 1;
379 case GL_SYNC_STATUS:
380 return 1;
381 case GL_SYNC_CONDITION:
382 return 1;
383 case GL_SYNC_FLAGS:
384 return 1;
386 // -- glHint with GL_OES_standard_derivatives
387 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
388 return 1;
390 // Chromium internal bind_generates_resource query
391 case GL_BIND_GENERATES_RESOURCE_CHROMIUM:
392 return 1;
394 // bad enum
395 default:
396 return 0;
400 namespace {
402 // Return the number of elements per group of a specified format.
403 int ElementsPerGroup(int format, int type) {
404 switch (type) {
405 case GL_UNSIGNED_SHORT_5_6_5:
406 case GL_UNSIGNED_SHORT_4_4_4_4:
407 case GL_UNSIGNED_SHORT_5_5_5_1:
408 case GL_UNSIGNED_INT_24_8_OES:
409 case GL_UNSIGNED_INT_2_10_10_10_REV:
410 case GL_UNSIGNED_INT_10F_11F_11F_REV:
411 case GL_UNSIGNED_INT_5_9_9_9_REV:
412 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
413 return 1;
414 default:
415 break;
418 switch (format) {
419 case GL_RGB:
420 case GL_RGB_INTEGER:
421 case GL_SRGB_EXT:
422 return 3;
423 case GL_LUMINANCE_ALPHA:
424 case GL_RG_EXT:
425 case GL_RG_INTEGER:
426 return 2;
427 case GL_RGBA:
428 case GL_RGBA_INTEGER:
429 case GL_BGRA_EXT:
430 case GL_SRGB_ALPHA_EXT:
431 return 4;
432 case GL_ALPHA:
433 case GL_LUMINANCE:
434 case GL_DEPTH_COMPONENT:
435 case GL_DEPTH_COMPONENT24_OES:
436 case GL_DEPTH_COMPONENT32_OES:
437 case GL_DEPTH_COMPONENT16:
438 case GL_DEPTH24_STENCIL8_OES:
439 case GL_DEPTH_STENCIL_OES:
440 case GL_RED_EXT:
441 case GL_RED_INTEGER:
442 return 1;
443 default:
444 return 0;
448 // Return the number of bytes per element, based on the element type.
449 int BytesPerElement(int type) {
450 switch (type) {
451 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
452 return 8;
453 case GL_FLOAT:
454 case GL_UNSIGNED_INT_24_8_OES:
455 case GL_UNSIGNED_INT:
456 case GL_UNSIGNED_INT_2_10_10_10_REV:
457 case GL_UNSIGNED_INT_10F_11F_11F_REV:
458 case GL_UNSIGNED_INT_5_9_9_9_REV:
459 return 4;
460 case GL_HALF_FLOAT:
461 case GL_HALF_FLOAT_OES:
462 case GL_UNSIGNED_SHORT:
463 case GL_SHORT:
464 case GL_UNSIGNED_SHORT_5_6_5:
465 case GL_UNSIGNED_SHORT_4_4_4_4:
466 case GL_UNSIGNED_SHORT_5_5_5_1:
467 return 2;
468 case GL_UNSIGNED_BYTE:
469 case GL_BYTE:
470 return 1;
471 default:
472 return 0;
476 } // anonymous namespace
478 uint32 GLES2Util::ComputeImageGroupSize(int format, int type) {
479 int bytes_per_element = BytesPerElement(type);
480 DCHECK_GE(8, bytes_per_element);
481 int elements_per_group = ElementsPerGroup(format, type);
482 DCHECK_GE(4, elements_per_group);
483 return bytes_per_element * elements_per_group;
486 bool GLES2Util::ComputeImagePaddedRowSize(
487 int width, int format, int type, int unpack_alignment,
488 uint32* padded_row_size) {
489 DCHECK(unpack_alignment == 1 || unpack_alignment == 2 ||
490 unpack_alignment == 4 || unpack_alignment == 8);
491 uint32 bytes_per_group = ComputeImageGroupSize(format, type);
492 uint32 unpadded_row_size;
493 if (!SafeMultiplyUint32(width, bytes_per_group, &unpadded_row_size)) {
494 return false;
496 uint32 temp;
497 if (!SafeAddUint32(unpadded_row_size, unpack_alignment - 1, &temp)) {
498 return false;
500 *padded_row_size = (temp / unpack_alignment) * unpack_alignment;
501 return true;
504 // Returns the amount of data glTexImage*D or glTexSubImage*D will access.
505 bool GLES2Util::ComputeImageDataSizes(
506 int width, int height, int depth, int format, int type,
507 int unpack_alignment, uint32* size, uint32* ret_unpadded_row_size,
508 uint32* ret_padded_row_size) {
509 DCHECK(unpack_alignment == 1 || unpack_alignment == 2 ||
510 unpack_alignment == 4 || unpack_alignment == 8);
511 uint32 bytes_per_group = ComputeImageGroupSize(format, type);
512 uint32 row_size;
513 if (!SafeMultiplyUint32(width, bytes_per_group, &row_size)) {
514 return false;
516 uint32 num_of_rows;
517 if (!SafeMultiplyUint32(height, depth, &num_of_rows)) {
518 return false;
520 if (num_of_rows > 1) {
521 uint32 temp;
522 if (!SafeAddUint32(row_size, unpack_alignment - 1, &temp)) {
523 return false;
525 uint32 padded_row_size = (temp / unpack_alignment) * unpack_alignment;
526 uint32 size_of_all_but_last_row;
527 if (!SafeMultiplyUint32((num_of_rows - 1), padded_row_size,
528 &size_of_all_but_last_row)) {
529 return false;
531 if (!SafeAddUint32(size_of_all_but_last_row, row_size, size)) {
532 return false;
534 if (ret_padded_row_size) {
535 *ret_padded_row_size = padded_row_size;
537 } else {
538 *size = row_size;
539 if (ret_padded_row_size) {
540 *ret_padded_row_size = row_size;
543 if (ret_unpadded_row_size) {
544 *ret_unpadded_row_size = row_size;
547 return true;
550 size_t GLES2Util::RenderbufferBytesPerPixel(int format) {
551 switch (format) {
552 case GL_STENCIL_INDEX8:
553 return 1;
554 case GL_RGBA4:
555 case GL_RGB565:
556 case GL_RGB5_A1:
557 case GL_DEPTH_COMPONENT16:
558 return 2;
559 case GL_RGB:
560 case GL_RGBA:
561 case GL_DEPTH24_STENCIL8_OES:
562 case GL_RGB8_OES:
563 case GL_RGBA8_OES:
564 case GL_DEPTH_COMPONENT24_OES:
565 return 4;
566 default:
567 return 0;
571 uint32 GLES2Util::GetElementSizeForUniformType(int type) {
572 switch (type) {
573 case GL_FLOAT:
574 case GL_FLOAT_VEC2:
575 case GL_FLOAT_VEC3:
576 case GL_FLOAT_VEC4:
577 case GL_FLOAT_MAT2:
578 case GL_FLOAT_MAT3:
579 case GL_FLOAT_MAT4:
580 return sizeof(GLfloat);
581 case GL_INT:
582 case GL_INT_VEC2:
583 case GL_INT_VEC3:
584 case GL_INT_VEC4:
585 case GL_BOOL:
586 case GL_BOOL_VEC2:
587 case GL_BOOL_VEC3:
588 case GL_BOOL_VEC4:
589 case GL_SAMPLER_2D:
590 case GL_SAMPLER_CUBE:
591 case GL_SAMPLER_2D_RECT_ARB: // extension.
592 case GL_SAMPLER_EXTERNAL_OES: // extension.
593 return sizeof(GLint);
595 // ES3 types.
596 case GL_UNSIGNED_INT:
597 case GL_UNSIGNED_INT_VEC2:
598 case GL_UNSIGNED_INT_VEC3:
599 case GL_UNSIGNED_INT_VEC4:
600 return sizeof(GLuint);
601 case GL_SAMPLER_3D:
602 case GL_SAMPLER_2D_SHADOW:
603 case GL_SAMPLER_2D_ARRAY:
604 case GL_SAMPLER_2D_ARRAY_SHADOW:
605 case GL_SAMPLER_CUBE_SHADOW:
606 case GL_INT_SAMPLER_2D:
607 case GL_INT_SAMPLER_3D:
608 case GL_INT_SAMPLER_CUBE:
609 case GL_INT_SAMPLER_2D_ARRAY:
610 case GL_UNSIGNED_INT_SAMPLER_2D:
611 case GL_UNSIGNED_INT_SAMPLER_3D:
612 case GL_UNSIGNED_INT_SAMPLER_CUBE:
613 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
614 return sizeof(GLint);
615 case GL_FLOAT_MAT2x3:
616 case GL_FLOAT_MAT3x2:
617 case GL_FLOAT_MAT2x4:
618 case GL_FLOAT_MAT4x2:
619 case GL_FLOAT_MAT3x4:
620 case GL_FLOAT_MAT4x3:
621 return sizeof(GLfloat);
623 default:
624 return 0;
628 uint32 GLES2Util::GetElementCountForUniformType(int type) {
629 switch (type) {
630 case GL_FLOAT:
631 case GL_INT:
632 case GL_BOOL:
633 case GL_SAMPLER_2D:
634 case GL_SAMPLER_CUBE:
635 case GL_SAMPLER_2D_RECT_ARB: // extension.
636 case GL_SAMPLER_EXTERNAL_OES: // extension.
637 return 1;
638 case GL_FLOAT_VEC2:
639 case GL_INT_VEC2:
640 case GL_BOOL_VEC2:
641 return 2;
642 case GL_FLOAT_VEC3:
643 case GL_INT_VEC3:
644 case GL_BOOL_VEC3:
645 return 3;
646 case GL_FLOAT_VEC4:
647 case GL_INT_VEC4:
648 case GL_BOOL_VEC4:
649 case GL_FLOAT_MAT2:
650 return 4;
651 case GL_FLOAT_MAT3:
652 return 9;
653 case GL_FLOAT_MAT4:
654 return 16;
656 // ES3 types.
657 case GL_UNSIGNED_INT:
658 case GL_SAMPLER_3D:
659 case GL_SAMPLER_2D_SHADOW:
660 case GL_SAMPLER_2D_ARRAY:
661 case GL_SAMPLER_2D_ARRAY_SHADOW:
662 case GL_SAMPLER_CUBE_SHADOW:
663 case GL_INT_SAMPLER_2D:
664 case GL_INT_SAMPLER_3D:
665 case GL_INT_SAMPLER_CUBE:
666 case GL_INT_SAMPLER_2D_ARRAY:
667 case GL_UNSIGNED_INT_SAMPLER_2D:
668 case GL_UNSIGNED_INT_SAMPLER_3D:
669 case GL_UNSIGNED_INT_SAMPLER_CUBE:
670 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
671 return 1;
672 case GL_UNSIGNED_INT_VEC2:
673 return 2;
674 case GL_UNSIGNED_INT_VEC3:
675 return 3;
676 case GL_UNSIGNED_INT_VEC4:
677 return 4;
678 case GL_FLOAT_MAT2x3:
679 case GL_FLOAT_MAT3x2:
680 return 6;
681 case GL_FLOAT_MAT2x4:
682 case GL_FLOAT_MAT4x2:
683 return 8;
684 case GL_FLOAT_MAT3x4:
685 case GL_FLOAT_MAT4x3:
686 return 12;
688 default:
689 return 0;
693 size_t GLES2Util::GetGLTypeSizeForTexturesAndBuffers(uint32 type) {
694 switch (type) {
695 case GL_BYTE:
696 return sizeof(GLbyte); // NOLINT
697 case GL_UNSIGNED_BYTE:
698 return sizeof(GLubyte); // NOLINT
699 case GL_SHORT:
700 return sizeof(GLshort); // NOLINT
701 case GL_UNSIGNED_SHORT:
702 return sizeof(GLushort); // NOLINT
703 case GL_INT:
704 return sizeof(GLint); // NOLINT
705 case GL_UNSIGNED_INT:
706 return sizeof(GLuint); // NOLINT
707 case GL_FLOAT:
708 return sizeof(GLfloat); // NOLINT
709 case GL_FIXED:
710 return sizeof(GLfixed); // NOLINT
711 default:
712 return 0;
716 size_t GLES2Util::GetGLTypeSizeForPathCoordType(uint32 type) {
717 switch (type) {
718 case GL_BYTE:
719 return sizeof(GLbyte); // NOLINT
720 case GL_UNSIGNED_BYTE:
721 return sizeof(GLubyte); // NOLINT
722 case GL_SHORT:
723 return sizeof(GLshort); // NOLINT
724 case GL_UNSIGNED_SHORT:
725 return sizeof(GLushort); // NOLINT
726 case GL_FLOAT:
727 return sizeof(GLfloat); // NOLINT
728 default:
729 return 0;
733 uint32 GLES2Util::GLErrorToErrorBit(uint32 error) {
734 switch (error) {
735 case GL_INVALID_ENUM:
736 return gl_error_bit::kInvalidEnum;
737 case GL_INVALID_VALUE:
738 return gl_error_bit::kInvalidValue;
739 case GL_INVALID_OPERATION:
740 return gl_error_bit::kInvalidOperation;
741 case GL_OUT_OF_MEMORY:
742 return gl_error_bit::kOutOfMemory;
743 case GL_INVALID_FRAMEBUFFER_OPERATION:
744 return gl_error_bit::kInvalidFrameBufferOperation;
745 case GL_CONTEXT_LOST_KHR:
746 return gl_error_bit::kContextLost;
747 default:
748 NOTREACHED();
749 return gl_error_bit::kNoError;
753 uint32 GLES2Util::GLErrorBitToGLError(uint32 error_bit) {
754 switch (error_bit) {
755 case gl_error_bit::kInvalidEnum:
756 return GL_INVALID_ENUM;
757 case gl_error_bit::kInvalidValue:
758 return GL_INVALID_VALUE;
759 case gl_error_bit::kInvalidOperation:
760 return GL_INVALID_OPERATION;
761 case gl_error_bit::kOutOfMemory:
762 return GL_OUT_OF_MEMORY;
763 case gl_error_bit::kInvalidFrameBufferOperation:
764 return GL_INVALID_FRAMEBUFFER_OPERATION;
765 case gl_error_bit::kContextLost:
766 return GL_CONTEXT_LOST_KHR;
767 default:
768 NOTREACHED();
769 return GL_NO_ERROR;
773 uint32 GLES2Util::IndexToGLFaceTarget(int index) {
774 static uint32 faces[] = {
775 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
776 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
777 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
778 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
779 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
780 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
782 return faces[index];
785 size_t GLES2Util::GLTargetToFaceIndex(uint32 target) {
786 switch (target) {
787 case GL_TEXTURE_2D:
788 case GL_TEXTURE_EXTERNAL_OES:
789 case GL_TEXTURE_RECTANGLE_ARB:
790 case GL_TEXTURE_3D:
791 case GL_TEXTURE_2D_ARRAY:
792 return 0;
793 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
794 return 0;
795 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
796 return 1;
797 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
798 return 2;
799 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
800 return 3;
801 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
802 return 4;
803 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
804 return 5;
805 default:
806 NOTREACHED();
807 return 0;
811 uint32 GLES2Util::GetPreferredGLReadPixelsFormat(uint32 internal_format) {
812 switch (internal_format) {
813 case GL_RGB16F_EXT:
814 case GL_RGB32F_EXT:
815 return GL_RGB;
816 case GL_RGBA16F_EXT:
817 case GL_RGBA32F_EXT:
818 return GL_RGBA;
819 default:
820 return GL_RGBA;
824 uint32 GLES2Util::GetPreferredGLReadPixelsType(
825 uint32 internal_format, uint32 texture_type) {
826 switch (internal_format) {
827 case GL_RGBA32F_EXT:
828 case GL_RGB32F_EXT:
829 return GL_FLOAT;
830 case GL_RGBA16F_EXT:
831 case GL_RGB16F_EXT:
832 return GL_HALF_FLOAT_OES;
833 case GL_RGBA:
834 case GL_RGB:
835 // Unsized internal format, check the type
836 switch (texture_type) {
837 case GL_FLOAT:
838 case GL_HALF_FLOAT_OES:
839 return GL_FLOAT;
840 default:
841 return GL_UNSIGNED_BYTE;
843 default:
844 return GL_UNSIGNED_BYTE;
848 uint32 GLES2Util::GetChannelsForFormat(int format) {
849 switch (format) {
850 case GL_ALPHA:
851 case GL_ALPHA16F_EXT:
852 case GL_ALPHA32F_EXT:
853 return kAlpha;
854 case GL_LUMINANCE:
855 return kRGB;
856 case GL_LUMINANCE_ALPHA:
857 return kRGBA;
858 case GL_RGB:
859 case GL_RGB8_OES:
860 case GL_RGB565:
861 case GL_RGB16F_EXT:
862 case GL_RGB32F_EXT:
863 case GL_SRGB_EXT:
864 case GL_SRGB8:
865 case GL_RGB8_SNORM:
866 case GL_R11F_G11F_B10F:
867 case GL_RGB9_E5:
868 case GL_RGB8UI:
869 case GL_RGB8I:
870 case GL_RGB16UI:
871 case GL_RGB16I:
872 case GL_RGB32UI:
873 case GL_RGB32I:
874 return kRGB;
875 case GL_BGRA_EXT:
876 case GL_BGRA8_EXT:
877 case GL_RGBA16F_EXT:
878 case GL_RGBA32F_EXT:
879 case GL_RGBA:
880 case GL_RGBA8_OES:
881 case GL_RGBA4:
882 case GL_RGB5_A1:
883 case GL_SRGB_ALPHA_EXT:
884 case GL_SRGB8_ALPHA8_EXT:
885 case GL_RGBA8_SNORM:
886 case GL_RGB10_A2:
887 case GL_RGBA8UI:
888 case GL_RGBA8I:
889 case GL_RGB10_A2UI:
890 case GL_RGBA16UI:
891 case GL_RGBA16I:
892 case GL_RGBA32UI:
893 case GL_RGBA32I:
894 return kRGBA;
895 case GL_DEPTH_COMPONENT32_OES:
896 case GL_DEPTH_COMPONENT24_OES:
897 case GL_DEPTH_COMPONENT16:
898 case GL_DEPTH_COMPONENT:
899 case GL_DEPTH_COMPONENT32F:
900 return kDepth;
901 case GL_STENCIL_INDEX8:
902 return kStencil;
903 case GL_DEPTH_STENCIL_OES:
904 case GL_DEPTH24_STENCIL8_OES:
905 case GL_DEPTH32F_STENCIL8:
906 return kDepth | kStencil;
907 case GL_RED_EXT:
908 case GL_R8:
909 case GL_R8_SNORM:
910 case GL_R16F:
911 case GL_R32F:
912 case GL_R8UI:
913 case GL_R8I:
914 case GL_R16UI:
915 case GL_R16I:
916 case GL_R32UI:
917 case GL_R32I:
918 return kRed;
919 case GL_RG_EXT:
920 case GL_RG8:
921 case GL_RG8_SNORM:
922 case GL_RG16F:
923 case GL_RG32F:
924 case GL_RG8UI:
925 case GL_RG8I:
926 case GL_RG16UI:
927 case GL_RG16I:
928 case GL_RG32UI:
929 case GL_RG32I:
930 return kRed | kGreen;
931 default:
932 return 0x0000;
936 uint32 GLES2Util::GetChannelsNeededForAttachmentType(
937 int type, uint32 max_color_attachments) {
938 switch (type) {
939 case GL_DEPTH_ATTACHMENT:
940 return kDepth;
941 case GL_STENCIL_ATTACHMENT:
942 return kStencil;
943 default:
944 if (type >= GL_COLOR_ATTACHMENT0 &&
945 type < static_cast<int>(
946 GL_COLOR_ATTACHMENT0 + max_color_attachments)) {
947 return kRGBA;
949 return 0x0000;
953 std::string GLES2Util::GetStringEnum(uint32 value) {
954 const EnumToString* entry = enum_to_string_table_;
955 const EnumToString* end = entry + enum_to_string_table_len_;
956 for (;entry < end; ++entry) {
957 if (value == entry->value) {
958 return entry->name;
961 std::stringstream ss;
962 ss.fill('0');
963 ss.width(value < 0x10000 ? 4 : 8);
964 ss << std::hex << value;
965 return "0x" + ss.str();
968 std::string GLES2Util::GetStringError(uint32 value) {
969 static EnumToString string_table[] = {
970 { GL_NONE, "GL_NONE" },
972 return GLES2Util::GetQualifiedEnumString(
973 string_table, arraysize(string_table), value);
976 std::string GLES2Util::GetStringBool(uint32 value) {
977 return value ? "GL_TRUE" : "GL_FALSE";
980 std::string GLES2Util::GetQualifiedEnumString(
981 const EnumToString* table, size_t count, uint32 value) {
982 for (const EnumToString* end = table + count; table < end; ++table) {
983 if (table->value == value) {
984 return table->name;
987 return GetStringEnum(value);
990 bool GLES2Util::ParseUniformName(
991 const std::string& name,
992 size_t* array_pos,
993 int* element_index,
994 bool* getting_array) {
995 if (name.empty())
996 return false;
997 bool getting_array_location = false;
998 size_t open_pos = std::string::npos;
999 base::CheckedNumeric<int> index = 0;
1000 if (name[name.size() - 1] == ']') {
1001 if (name.size() < 3) {
1002 return false;
1004 open_pos = name.find_last_of('[');
1005 if (open_pos == std::string::npos ||
1006 open_pos >= name.size() - 2) {
1007 return false;
1009 size_t last = name.size() - 1;
1010 for (size_t pos = open_pos + 1; pos < last; ++pos) {
1011 int8 digit = name[pos] - '0';
1012 if (digit < 0 || digit > 9) {
1013 return false;
1015 index = index * 10 + digit;
1017 if (!index.IsValid()) {
1018 return false;
1020 getting_array_location = true;
1022 *getting_array = getting_array_location;
1023 *element_index = index.ValueOrDie();
1024 *array_pos = open_pos;
1025 return true;
1028 size_t GLES2Util::CalcClearBufferivDataCount(int buffer) {
1029 switch (buffer) {
1030 case GL_COLOR:
1031 return 4;
1032 case GL_STENCIL:
1033 return 1;
1034 default:
1035 return 0;
1039 size_t GLES2Util::CalcClearBufferfvDataCount(int buffer) {
1040 switch (buffer) {
1041 case GL_COLOR:
1042 return 4;
1043 case GL_DEPTH:
1044 return 1;
1045 default:
1046 return 0;
1050 // static
1051 void GLES2Util::MapUint64ToTwoUint32(
1052 uint64_t v64, uint32_t* v32_0, uint32_t* v32_1) {
1053 DCHECK(v32_0 && v32_1);
1054 *v32_0 = static_cast<uint32_t>(v64 & 0xFFFFFFFF);
1055 *v32_1 = static_cast<uint32_t>((v64 & 0xFFFFFFFF00000000) >> 32);
1058 // static
1059 uint64_t GLES2Util::MapTwoUint32ToUint64(uint32_t v32_0, uint32_t v32_1) {
1060 uint64_t v64 = v32_1;
1061 return (v64 << 32) | v32_0;
1064 // static
1065 uint32_t GLES2Util::MapBufferTargetToBindingEnum(uint32_t target) {
1066 switch (target) {
1067 case GL_ARRAY_BUFFER:
1068 return GL_ARRAY_BUFFER_BINDING;
1069 case GL_COPY_READ_BUFFER:
1070 return GL_COPY_READ_BUFFER_BINDING;
1071 case GL_COPY_WRITE_BUFFER:
1072 return GL_COPY_WRITE_BUFFER_BINDING;
1073 case GL_ELEMENT_ARRAY_BUFFER:
1074 return GL_ELEMENT_ARRAY_BUFFER_BINDING;
1075 case GL_PIXEL_PACK_BUFFER:
1076 return GL_PIXEL_PACK_BUFFER_BINDING;
1077 case GL_PIXEL_UNPACK_BUFFER:
1078 return GL_PIXEL_UNPACK_BUFFER_BINDING;
1079 case GL_TRANSFORM_FEEDBACK_BUFFER:
1080 return GL_TRANSFORM_FEEDBACK_BUFFER_BINDING;
1081 case GL_UNIFORM_BUFFER:
1082 return GL_UNIFORM_BUFFER_BINDING;
1083 default:
1084 return 0;
1089 namespace {
1091 // WebGraphicsContext3DCommandBufferImpl configuration attributes. Those in
1092 // the 16-bit range are the same as used by EGL. Those outside the 16-bit range
1093 // are unique to Chromium. Attributes are matched using a closest fit algorithm.
1095 // From <EGL/egl.h>.
1096 const int32 kAlphaSize = 0x3021; // EGL_ALPHA_SIZE
1097 const int32 kBlueSize = 0x3022; // EGL_BLUE_SIZE
1098 const int32 kGreenSize = 0x3023; // EGL_GREEN_SIZE
1099 const int32 kRedSize = 0x3024; // EGL_RED_SIZE
1100 const int32 kDepthSize = 0x3025; // EGL_DEPTH_SIZE
1101 const int32 kStencilSize = 0x3026; // EGL_STENCIL_SIZE
1102 const int32 kSamples = 0x3031; // EGL_SAMPLES
1103 const int32 kSampleBuffers = 0x3032; // EGL_SAMPLE_BUFFERS
1104 const int32 kNone = 0x3038; // EGL_NONE
1105 const int32 kSwapBehavior = 0x3093; // EGL_SWAP_BEHAVIOR
1106 const int32 kBufferPreserved = 0x3094; // EGL_BUFFER_PRESERVED
1107 const int32 kBufferDestroyed = 0x3095; // EGL_BUFFER_DESTROYED
1109 // Chromium only.
1110 const int32 kBindGeneratesResource = 0x10000;
1111 const int32 kFailIfMajorPerfCaveat = 0x10001;
1112 const int32 kLoseContextWhenOutOfMemory = 0x10002;
1113 const int32 kWebGLVersion = 0x10003;
1115 } // namespace
1117 ContextCreationAttribHelper::ContextCreationAttribHelper()
1118 : alpha_size(-1),
1119 blue_size(-1),
1120 green_size(-1),
1121 red_size(-1),
1122 depth_size(-1),
1123 stencil_size(-1),
1124 samples(-1),
1125 sample_buffers(-1),
1126 buffer_preserved(true),
1127 bind_generates_resource(true),
1128 fail_if_major_perf_caveat(false),
1129 lose_context_when_out_of_memory(false),
1130 webgl_version(0) {
1133 void ContextCreationAttribHelper::Serialize(std::vector<int32>* attribs) const {
1134 if (alpha_size != -1) {
1135 attribs->push_back(kAlphaSize);
1136 attribs->push_back(alpha_size);
1138 if (blue_size != -1) {
1139 attribs->push_back(kBlueSize);
1140 attribs->push_back(blue_size);
1142 if (green_size != -1) {
1143 attribs->push_back(kGreenSize);
1144 attribs->push_back(green_size);
1146 if (red_size != -1) {
1147 attribs->push_back(kRedSize);
1148 attribs->push_back(red_size);
1150 if (depth_size != -1) {
1151 attribs->push_back(kDepthSize);
1152 attribs->push_back(depth_size);
1154 if (stencil_size != -1) {
1155 attribs->push_back(kStencilSize);
1156 attribs->push_back(stencil_size);
1158 if (samples != -1) {
1159 attribs->push_back(kSamples);
1160 attribs->push_back(samples);
1162 if (sample_buffers != -1) {
1163 attribs->push_back(kSampleBuffers);
1164 attribs->push_back(sample_buffers);
1166 attribs->push_back(kSwapBehavior);
1167 attribs->push_back(buffer_preserved ? kBufferPreserved : kBufferDestroyed);
1168 attribs->push_back(kBindGeneratesResource);
1169 attribs->push_back(bind_generates_resource ? 1 : 0);
1170 attribs->push_back(kFailIfMajorPerfCaveat);
1171 attribs->push_back(fail_if_major_perf_caveat ? 1 : 0);
1172 attribs->push_back(kLoseContextWhenOutOfMemory);
1173 attribs->push_back(lose_context_when_out_of_memory ? 1 : 0);
1174 attribs->push_back(kWebGLVersion);
1175 attribs->push_back(webgl_version);
1176 attribs->push_back(kNone);
1179 bool ContextCreationAttribHelper::Parse(const std::vector<int32>& attribs) {
1180 for (size_t i = 0; i < attribs.size(); i += 2) {
1181 const int32 attrib = attribs[i];
1182 if (i + 1 >= attribs.size()) {
1183 if (attrib == kNone) {
1184 return true;
1187 DLOG(ERROR) << "Missing value after context creation attribute: "
1188 << attrib;
1189 return false;
1192 const int32 value = attribs[i+1];
1193 switch (attrib) {
1194 case kAlphaSize:
1195 alpha_size = value;
1196 break;
1197 case kBlueSize:
1198 blue_size = value;
1199 break;
1200 case kGreenSize:
1201 green_size = value;
1202 break;
1203 case kRedSize:
1204 red_size = value;
1205 break;
1206 case kDepthSize:
1207 depth_size = value;
1208 break;
1209 case kStencilSize:
1210 stencil_size = value;
1211 break;
1212 case kSamples:
1213 samples = value;
1214 break;
1215 case kSampleBuffers:
1216 sample_buffers = value;
1217 break;
1218 case kSwapBehavior:
1219 buffer_preserved = value == kBufferPreserved;
1220 break;
1221 case kBindGeneratesResource:
1222 bind_generates_resource = value != 0;
1223 break;
1224 case kFailIfMajorPerfCaveat:
1225 fail_if_major_perf_caveat = value != 0;
1226 break;
1227 case kLoseContextWhenOutOfMemory:
1228 lose_context_when_out_of_memory = value != 0;
1229 break;
1230 case kWebGLVersion:
1231 webgl_version = value;
1232 break;
1233 case kNone:
1234 // Terminate list, even if more attributes.
1235 return true;
1236 default:
1237 DLOG(ERROR) << "Invalid context creation attribute: " << attrib;
1238 return false;
1242 return true;
1245 #include "gpu/command_buffer/common/gles2_cmd_utils_implementation_autogen.h"
1247 } // namespace gles2
1248 } // namespace gpu