Infobar material design refresh: bg color
[chromium-blink-merge.git] / gpu / command_buffer / common / gles2_cmd_utils.cc
blobe30e2e38c74705a99d7b14f1f8acabaa8a0aac26
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_INT:
457 case GL_UNSIGNED_INT_2_10_10_10_REV:
458 case GL_UNSIGNED_INT_10F_11F_11F_REV:
459 case GL_UNSIGNED_INT_5_9_9_9_REV:
460 return 4;
461 case GL_HALF_FLOAT:
462 case GL_HALF_FLOAT_OES:
463 case GL_UNSIGNED_SHORT:
464 case GL_SHORT:
465 case GL_UNSIGNED_SHORT_5_6_5:
466 case GL_UNSIGNED_SHORT_4_4_4_4:
467 case GL_UNSIGNED_SHORT_5_5_5_1:
468 return 2;
469 case GL_UNSIGNED_BYTE:
470 case GL_BYTE:
471 return 1;
472 default:
473 return 0;
477 } // anonymous namespace
479 uint32 GLES2Util::ComputeImageGroupSize(int format, int type) {
480 int bytes_per_element = BytesPerElement(type);
481 DCHECK_GE(8, bytes_per_element);
482 int elements_per_group = ElementsPerGroup(format, type);
483 DCHECK_GE(4, elements_per_group);
484 return bytes_per_element * elements_per_group;
487 bool GLES2Util::ComputeImagePaddedRowSize(
488 int width, int format, int type, int unpack_alignment,
489 uint32* padded_row_size) {
490 DCHECK(unpack_alignment == 1 || unpack_alignment == 2 ||
491 unpack_alignment == 4 || unpack_alignment == 8);
492 uint32 bytes_per_group = ComputeImageGroupSize(format, type);
493 uint32 unpadded_row_size;
494 if (!SafeMultiplyUint32(width, bytes_per_group, &unpadded_row_size)) {
495 return false;
497 uint32 temp;
498 if (!SafeAddUint32(unpadded_row_size, unpack_alignment - 1, &temp)) {
499 return false;
501 *padded_row_size = (temp / unpack_alignment) * unpack_alignment;
502 return true;
505 // Returns the amount of data glTexImage*D or glTexSubImage*D will access.
506 bool GLES2Util::ComputeImageDataSizes(
507 int width, int height, int depth, int format, int type,
508 int unpack_alignment, uint32* size, uint32* ret_unpadded_row_size,
509 uint32* ret_padded_row_size) {
510 DCHECK(unpack_alignment == 1 || unpack_alignment == 2 ||
511 unpack_alignment == 4 || unpack_alignment == 8);
512 uint32 bytes_per_group = ComputeImageGroupSize(format, type);
513 uint32 row_size;
514 if (!SafeMultiplyUint32(width, bytes_per_group, &row_size)) {
515 return false;
517 uint32 num_of_rows;
518 if (!SafeMultiplyUint32(height, depth, &num_of_rows)) {
519 return false;
521 if (num_of_rows > 1) {
522 uint32 temp;
523 if (!SafeAddUint32(row_size, unpack_alignment - 1, &temp)) {
524 return false;
526 uint32 padded_row_size = (temp / unpack_alignment) * unpack_alignment;
527 uint32 size_of_all_but_last_row;
528 if (!SafeMultiplyUint32((num_of_rows - 1), padded_row_size,
529 &size_of_all_but_last_row)) {
530 return false;
532 if (!SafeAddUint32(size_of_all_but_last_row, row_size, size)) {
533 return false;
535 if (ret_padded_row_size) {
536 *ret_padded_row_size = padded_row_size;
538 } else {
539 *size = row_size;
540 if (ret_padded_row_size) {
541 *ret_padded_row_size = row_size;
544 if (ret_unpadded_row_size) {
545 *ret_unpadded_row_size = row_size;
548 return true;
551 size_t GLES2Util::RenderbufferBytesPerPixel(int format) {
552 switch (format) {
553 case GL_STENCIL_INDEX8:
554 return 1;
555 case GL_RGBA4:
556 case GL_RGB565:
557 case GL_RGB5_A1:
558 case GL_DEPTH_COMPONENT16:
559 return 2;
560 case GL_RGB:
561 case GL_RGBA:
562 case GL_DEPTH24_STENCIL8_OES:
563 case GL_RGB8_OES:
564 case GL_RGBA8_OES:
565 case GL_DEPTH_COMPONENT24_OES:
566 return 4;
567 default:
568 return 0;
572 uint32 GLES2Util::GetElementSizeForUniformType(int type) {
573 switch (type) {
574 case GL_FLOAT:
575 case GL_FLOAT_VEC2:
576 case GL_FLOAT_VEC3:
577 case GL_FLOAT_VEC4:
578 case GL_FLOAT_MAT2:
579 case GL_FLOAT_MAT3:
580 case GL_FLOAT_MAT4:
581 return sizeof(GLfloat);
582 case GL_INT:
583 case GL_INT_VEC2:
584 case GL_INT_VEC3:
585 case GL_INT_VEC4:
586 case GL_BOOL:
587 case GL_BOOL_VEC2:
588 case GL_BOOL_VEC3:
589 case GL_BOOL_VEC4:
590 case GL_SAMPLER_2D:
591 case GL_SAMPLER_CUBE:
592 case GL_SAMPLER_2D_RECT_ARB: // extension.
593 case GL_SAMPLER_EXTERNAL_OES: // extension.
594 return sizeof(GLint);
596 // ES3 types.
597 case GL_UNSIGNED_INT:
598 case GL_UNSIGNED_INT_VEC2:
599 case GL_UNSIGNED_INT_VEC3:
600 case GL_UNSIGNED_INT_VEC4:
601 return sizeof(GLuint);
602 case GL_SAMPLER_3D:
603 case GL_SAMPLER_2D_SHADOW:
604 case GL_SAMPLER_2D_ARRAY:
605 case GL_SAMPLER_2D_ARRAY_SHADOW:
606 case GL_SAMPLER_CUBE_SHADOW:
607 case GL_INT_SAMPLER_2D:
608 case GL_INT_SAMPLER_3D:
609 case GL_INT_SAMPLER_CUBE:
610 case GL_INT_SAMPLER_2D_ARRAY:
611 case GL_UNSIGNED_INT_SAMPLER_2D:
612 case GL_UNSIGNED_INT_SAMPLER_3D:
613 case GL_UNSIGNED_INT_SAMPLER_CUBE:
614 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
615 return sizeof(GLint);
616 case GL_FLOAT_MAT2x3:
617 case GL_FLOAT_MAT3x2:
618 case GL_FLOAT_MAT2x4:
619 case GL_FLOAT_MAT4x2:
620 case GL_FLOAT_MAT3x4:
621 case GL_FLOAT_MAT4x3:
622 return sizeof(GLfloat);
624 default:
625 return 0;
629 uint32 GLES2Util::GetElementCountForUniformType(int type) {
630 switch (type) {
631 case GL_FLOAT:
632 case GL_INT:
633 case GL_BOOL:
634 case GL_SAMPLER_2D:
635 case GL_SAMPLER_CUBE:
636 case GL_SAMPLER_2D_RECT_ARB: // extension.
637 case GL_SAMPLER_EXTERNAL_OES: // extension.
638 return 1;
639 case GL_FLOAT_VEC2:
640 case GL_INT_VEC2:
641 case GL_BOOL_VEC2:
642 return 2;
643 case GL_FLOAT_VEC3:
644 case GL_INT_VEC3:
645 case GL_BOOL_VEC3:
646 return 3;
647 case GL_FLOAT_VEC4:
648 case GL_INT_VEC4:
649 case GL_BOOL_VEC4:
650 case GL_FLOAT_MAT2:
651 return 4;
652 case GL_FLOAT_MAT3:
653 return 9;
654 case GL_FLOAT_MAT4:
655 return 16;
657 // ES3 types.
658 case GL_UNSIGNED_INT:
659 case GL_SAMPLER_3D:
660 case GL_SAMPLER_2D_SHADOW:
661 case GL_SAMPLER_2D_ARRAY:
662 case GL_SAMPLER_2D_ARRAY_SHADOW:
663 case GL_SAMPLER_CUBE_SHADOW:
664 case GL_INT_SAMPLER_2D:
665 case GL_INT_SAMPLER_3D:
666 case GL_INT_SAMPLER_CUBE:
667 case GL_INT_SAMPLER_2D_ARRAY:
668 case GL_UNSIGNED_INT_SAMPLER_2D:
669 case GL_UNSIGNED_INT_SAMPLER_3D:
670 case GL_UNSIGNED_INT_SAMPLER_CUBE:
671 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
672 return 1;
673 case GL_UNSIGNED_INT_VEC2:
674 return 2;
675 case GL_UNSIGNED_INT_VEC3:
676 return 3;
677 case GL_UNSIGNED_INT_VEC4:
678 return 4;
679 case GL_FLOAT_MAT2x3:
680 case GL_FLOAT_MAT3x2:
681 return 6;
682 case GL_FLOAT_MAT2x4:
683 case GL_FLOAT_MAT4x2:
684 return 8;
685 case GL_FLOAT_MAT3x4:
686 case GL_FLOAT_MAT4x3:
687 return 12;
689 default:
690 return 0;
694 size_t GLES2Util::GetGLTypeSizeForTexturesAndBuffers(uint32 type) {
695 switch (type) {
696 case GL_BYTE:
697 return sizeof(GLbyte); // NOLINT
698 case GL_UNSIGNED_BYTE:
699 return sizeof(GLubyte); // NOLINT
700 case GL_SHORT:
701 return sizeof(GLshort); // NOLINT
702 case GL_UNSIGNED_SHORT:
703 return sizeof(GLushort); // NOLINT
704 case GL_INT:
705 return sizeof(GLint); // NOLINT
706 case GL_UNSIGNED_INT:
707 return sizeof(GLuint); // NOLINT
708 case GL_FLOAT:
709 return sizeof(GLfloat); // NOLINT
710 case GL_FIXED:
711 return sizeof(GLfixed); // NOLINT
712 default:
713 return 0;
717 size_t GLES2Util::GetGLTypeSizeForPathCoordType(uint32 type) {
718 switch (type) {
719 case GL_BYTE:
720 return sizeof(GLbyte); // NOLINT
721 case GL_UNSIGNED_BYTE:
722 return sizeof(GLubyte); // NOLINT
723 case GL_SHORT:
724 return sizeof(GLshort); // NOLINT
725 case GL_UNSIGNED_SHORT:
726 return sizeof(GLushort); // NOLINT
727 case GL_FLOAT:
728 return sizeof(GLfloat); // NOLINT
729 default:
730 return 0;
734 uint32 GLES2Util::GLErrorToErrorBit(uint32 error) {
735 switch (error) {
736 case GL_INVALID_ENUM:
737 return gl_error_bit::kInvalidEnum;
738 case GL_INVALID_VALUE:
739 return gl_error_bit::kInvalidValue;
740 case GL_INVALID_OPERATION:
741 return gl_error_bit::kInvalidOperation;
742 case GL_OUT_OF_MEMORY:
743 return gl_error_bit::kOutOfMemory;
744 case GL_INVALID_FRAMEBUFFER_OPERATION:
745 return gl_error_bit::kInvalidFrameBufferOperation;
746 case GL_CONTEXT_LOST_KHR:
747 return gl_error_bit::kContextLost;
748 default:
749 NOTREACHED();
750 return gl_error_bit::kNoError;
754 uint32 GLES2Util::GLErrorBitToGLError(uint32 error_bit) {
755 switch (error_bit) {
756 case gl_error_bit::kInvalidEnum:
757 return GL_INVALID_ENUM;
758 case gl_error_bit::kInvalidValue:
759 return GL_INVALID_VALUE;
760 case gl_error_bit::kInvalidOperation:
761 return GL_INVALID_OPERATION;
762 case gl_error_bit::kOutOfMemory:
763 return GL_OUT_OF_MEMORY;
764 case gl_error_bit::kInvalidFrameBufferOperation:
765 return GL_INVALID_FRAMEBUFFER_OPERATION;
766 case gl_error_bit::kContextLost:
767 return GL_CONTEXT_LOST_KHR;
768 default:
769 NOTREACHED();
770 return GL_NO_ERROR;
774 uint32 GLES2Util::IndexToGLFaceTarget(int index) {
775 static uint32 faces[] = {
776 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
777 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
778 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
779 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
780 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
781 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
783 return faces[index];
786 size_t GLES2Util::GLTargetToFaceIndex(uint32 target) {
787 switch (target) {
788 case GL_TEXTURE_2D:
789 case GL_TEXTURE_EXTERNAL_OES:
790 case GL_TEXTURE_RECTANGLE_ARB:
791 case GL_TEXTURE_3D:
792 case GL_TEXTURE_2D_ARRAY:
793 return 0;
794 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
795 return 0;
796 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
797 return 1;
798 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
799 return 2;
800 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
801 return 3;
802 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
803 return 4;
804 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
805 return 5;
806 default:
807 NOTREACHED();
808 return 0;
812 uint32 GLES2Util::GetGLReadPixelsImplementationFormat(
813 uint32 internal_format) {
814 switch (internal_format) {
815 case GL_R8:
816 case GL_R16F:
817 case GL_R32F:
818 return GL_RED;
819 case GL_R8UI:
820 case GL_R8I:
821 case GL_R16UI:
822 case GL_R16I:
823 case GL_R32UI:
824 case GL_R32I:
825 return GL_RED_INTEGER;
826 case GL_RG8:
827 case GL_RG16F:
828 case GL_RG32F:
829 return GL_RG;
830 case GL_RG8UI:
831 case GL_RG8I:
832 case GL_RG16UI:
833 case GL_RG16I:
834 case GL_RG32UI:
835 case GL_RG32I:
836 return GL_RG_INTEGER;
837 case GL_RGB:
838 case GL_RGB8:
839 case GL_RGB565:
840 case GL_R11F_G11F_B10F:
841 case GL_RGB16F:
842 case GL_RGB32F:
843 return GL_RGB;
844 case GL_RGBA8UI:
845 case GL_RGBA8I:
846 case GL_RGB10_A2UI:
847 case GL_RGBA16UI:
848 case GL_RGBA16I:
849 case GL_RGBA32UI:
850 case GL_RGBA32I:
851 return GL_RGBA_INTEGER;
852 default:
853 return GL_RGBA;
857 uint32 GLES2Util::GetGLReadPixelsImplementationType(
858 uint32 internal_format, uint32 texture_type) {
859 switch (internal_format) {
860 case GL_R16UI:
861 case GL_RG16UI:
862 case GL_RGBA16UI:
863 case GL_RGB10_A2:
864 case GL_RGB10_A2UI:
865 return GL_UNSIGNED_SHORT;
866 case GL_R32UI:
867 case GL_RG32UI:
868 case GL_RGBA32UI:
869 return GL_UNSIGNED_INT;
870 case GL_R8I:
871 case GL_RG8I:
872 case GL_RGBA8I:
873 return GL_BYTE;
874 case GL_R16I:
875 case GL_RG16I:
876 case GL_RGBA16I:
877 return GL_SHORT;
878 case GL_R32I:
879 case GL_RG32I:
880 case GL_RGBA32I:
881 return GL_INT;
882 case GL_R32F:
883 case GL_RG32F:
884 case GL_RGB32F:
885 case GL_RGBA32F:
886 return GL_FLOAT;
887 case GL_R16F:
888 case GL_RG16F:
889 case GL_R11F_G11F_B10F:
890 case GL_RGB16F:
891 case GL_RGBA16F:
892 // TODO(zmo): Consider return GL_UNSIGNED_INT_10F_11F_11F_REV and
893 // GL_HALF_FLOAT.
894 return GL_FLOAT;
895 case GL_RGBA:
896 case GL_RGB:
897 // Unsized internal format, check the type
898 switch (texture_type) {
899 case GL_FLOAT:
900 case GL_HALF_FLOAT_OES:
901 return GL_FLOAT;
902 // TODO(zmo): Consider return GL_UNSIGNED_SHORT_5_6_5,
903 // GL_UNSIGNED_SHORT_4_4_4_4, GL_UNSIGNED_SHORT_5_5_5_1, and
904 // GL_UNSIGNED_INT_2_10_10_10_REV.
905 default:
906 return GL_UNSIGNED_BYTE;
908 default:
909 return GL_UNSIGNED_BYTE;
913 uint32 GLES2Util::GetChannelsForFormat(int format) {
914 switch (format) {
915 case GL_ALPHA:
916 case GL_ALPHA16F_EXT:
917 case GL_ALPHA32F_EXT:
918 return kAlpha;
919 case GL_LUMINANCE:
920 return kRGB;
921 case GL_LUMINANCE_ALPHA:
922 return kRGBA;
923 case GL_RGB:
924 case GL_RGB8_OES:
925 case GL_RGB565:
926 case GL_RGB16F_EXT:
927 case GL_RGB32F_EXT:
928 case GL_SRGB_EXT:
929 case GL_SRGB8:
930 case GL_RGB8_SNORM:
931 case GL_R11F_G11F_B10F:
932 case GL_RGB9_E5:
933 case GL_RGB8UI:
934 case GL_RGB8I:
935 case GL_RGB16UI:
936 case GL_RGB16I:
937 case GL_RGB32UI:
938 case GL_RGB32I:
939 return kRGB;
940 case GL_BGRA_EXT:
941 case GL_BGRA8_EXT:
942 case GL_RGBA16F_EXT:
943 case GL_RGBA32F_EXT:
944 case GL_RGBA:
945 case GL_RGBA8_OES:
946 case GL_RGBA4:
947 case GL_RGB5_A1:
948 case GL_SRGB_ALPHA_EXT:
949 case GL_SRGB8_ALPHA8_EXT:
950 case GL_RGBA8_SNORM:
951 case GL_RGB10_A2:
952 case GL_RGBA8UI:
953 case GL_RGBA8I:
954 case GL_RGB10_A2UI:
955 case GL_RGBA16UI:
956 case GL_RGBA16I:
957 case GL_RGBA32UI:
958 case GL_RGBA32I:
959 return kRGBA;
960 case GL_DEPTH_COMPONENT32_OES:
961 case GL_DEPTH_COMPONENT24_OES:
962 case GL_DEPTH_COMPONENT16:
963 case GL_DEPTH_COMPONENT:
964 case GL_DEPTH_COMPONENT32F:
965 return kDepth;
966 case GL_STENCIL_INDEX8:
967 return kStencil;
968 case GL_DEPTH_STENCIL_OES:
969 case GL_DEPTH24_STENCIL8_OES:
970 case GL_DEPTH32F_STENCIL8:
971 return kDepth | kStencil;
972 case GL_RED_EXT:
973 case GL_R8:
974 case GL_R8_SNORM:
975 case GL_R16F:
976 case GL_R32F:
977 case GL_R8UI:
978 case GL_R8I:
979 case GL_R16UI:
980 case GL_R16I:
981 case GL_R32UI:
982 case GL_R32I:
983 return kRed;
984 case GL_RG_EXT:
985 case GL_RG8:
986 case GL_RG8_SNORM:
987 case GL_RG16F:
988 case GL_RG32F:
989 case GL_RG8UI:
990 case GL_RG8I:
991 case GL_RG16UI:
992 case GL_RG16I:
993 case GL_RG32UI:
994 case GL_RG32I:
995 return kRed | kGreen;
996 default:
997 return 0x0000;
1001 uint32 GLES2Util::GetChannelsNeededForAttachmentType(
1002 int type, uint32 max_color_attachments) {
1003 switch (type) {
1004 case GL_DEPTH_ATTACHMENT:
1005 return kDepth;
1006 case GL_STENCIL_ATTACHMENT:
1007 return kStencil;
1008 default:
1009 if (type >= GL_COLOR_ATTACHMENT0 &&
1010 type < static_cast<int>(
1011 GL_COLOR_ATTACHMENT0 + max_color_attachments)) {
1012 return kRGBA;
1014 return 0x0000;
1018 std::string GLES2Util::GetStringEnum(uint32 value) {
1019 const EnumToString* entry = enum_to_string_table_;
1020 const EnumToString* end = entry + enum_to_string_table_len_;
1021 for (;entry < end; ++entry) {
1022 if (value == entry->value) {
1023 return entry->name;
1026 std::stringstream ss;
1027 ss.fill('0');
1028 ss.width(value < 0x10000 ? 4 : 8);
1029 ss << std::hex << value;
1030 return "0x" + ss.str();
1033 std::string GLES2Util::GetStringError(uint32 value) {
1034 static EnumToString string_table[] = {
1035 { GL_NONE, "GL_NONE" },
1037 return GLES2Util::GetQualifiedEnumString(
1038 string_table, arraysize(string_table), value);
1041 std::string GLES2Util::GetStringBool(uint32 value) {
1042 return value ? "GL_TRUE" : "GL_FALSE";
1045 std::string GLES2Util::GetQualifiedEnumString(
1046 const EnumToString* table, size_t count, uint32 value) {
1047 for (const EnumToString* end = table + count; table < end; ++table) {
1048 if (table->value == value) {
1049 return table->name;
1052 return GetStringEnum(value);
1055 bool GLES2Util::ParseUniformName(
1056 const std::string& name,
1057 size_t* array_pos,
1058 int* element_index,
1059 bool* getting_array) {
1060 if (name.empty())
1061 return false;
1062 bool getting_array_location = false;
1063 size_t open_pos = std::string::npos;
1064 base::CheckedNumeric<int> index = 0;
1065 if (name[name.size() - 1] == ']') {
1066 if (name.size() < 3) {
1067 return false;
1069 open_pos = name.find_last_of('[');
1070 if (open_pos == std::string::npos ||
1071 open_pos >= name.size() - 2) {
1072 return false;
1074 size_t last = name.size() - 1;
1075 for (size_t pos = open_pos + 1; pos < last; ++pos) {
1076 int8 digit = name[pos] - '0';
1077 if (digit < 0 || digit > 9) {
1078 return false;
1080 index = index * 10 + digit;
1082 if (!index.IsValid()) {
1083 return false;
1085 getting_array_location = true;
1087 *getting_array = getting_array_location;
1088 *element_index = index.ValueOrDie();
1089 *array_pos = open_pos;
1090 return true;
1093 size_t GLES2Util::CalcClearBufferivDataCount(int buffer) {
1094 switch (buffer) {
1095 case GL_COLOR:
1096 return 4;
1097 case GL_STENCIL:
1098 return 1;
1099 default:
1100 return 0;
1104 size_t GLES2Util::CalcClearBufferfvDataCount(int buffer) {
1105 switch (buffer) {
1106 case GL_COLOR:
1107 return 4;
1108 case GL_DEPTH:
1109 return 1;
1110 default:
1111 return 0;
1115 // static
1116 void GLES2Util::MapUint64ToTwoUint32(
1117 uint64_t v64, uint32_t* v32_0, uint32_t* v32_1) {
1118 DCHECK(v32_0 && v32_1);
1119 *v32_0 = static_cast<uint32_t>(v64 & 0xFFFFFFFF);
1120 *v32_1 = static_cast<uint32_t>((v64 & 0xFFFFFFFF00000000) >> 32);
1123 // static
1124 uint64_t GLES2Util::MapTwoUint32ToUint64(uint32_t v32_0, uint32_t v32_1) {
1125 uint64_t v64 = v32_1;
1126 return (v64 << 32) | v32_0;
1129 // static
1130 uint32_t GLES2Util::MapBufferTargetToBindingEnum(uint32_t target) {
1131 switch (target) {
1132 case GL_ARRAY_BUFFER:
1133 return GL_ARRAY_BUFFER_BINDING;
1134 case GL_COPY_READ_BUFFER:
1135 return GL_COPY_READ_BUFFER_BINDING;
1136 case GL_COPY_WRITE_BUFFER:
1137 return GL_COPY_WRITE_BUFFER_BINDING;
1138 case GL_ELEMENT_ARRAY_BUFFER:
1139 return GL_ELEMENT_ARRAY_BUFFER_BINDING;
1140 case GL_PIXEL_PACK_BUFFER:
1141 return GL_PIXEL_PACK_BUFFER_BINDING;
1142 case GL_PIXEL_UNPACK_BUFFER:
1143 return GL_PIXEL_UNPACK_BUFFER_BINDING;
1144 case GL_TRANSFORM_FEEDBACK_BUFFER:
1145 return GL_TRANSFORM_FEEDBACK_BUFFER_BINDING;
1146 case GL_UNIFORM_BUFFER:
1147 return GL_UNIFORM_BUFFER_BINDING;
1148 default:
1149 return 0;
1153 // static
1154 bool GLES2Util::IsUnsignedIntegerFormat(uint32_t internal_format) {
1155 switch (internal_format) {
1156 case GL_R8UI:
1157 case GL_R16UI:
1158 case GL_R32UI:
1159 case GL_RG8UI:
1160 case GL_RG16UI:
1161 case GL_RG32UI:
1162 case GL_RGBA8UI:
1163 case GL_RGB10_A2UI:
1164 case GL_RGBA16UI:
1165 case GL_RGBA32UI:
1166 return true;
1167 default:
1168 return false;
1172 // static
1173 bool GLES2Util::IsSignedIntegerFormat(uint32_t internal_format) {
1174 switch (internal_format) {
1175 case GL_R8I:
1176 case GL_R16I:
1177 case GL_R32I:
1178 case GL_RG8I:
1179 case GL_RG16I:
1180 case GL_RG32I:
1181 case GL_RGBA8I:
1182 case GL_RGBA16I:
1183 case GL_RGBA32I:
1184 return true;
1185 default:
1186 return false;
1190 // static
1191 bool GLES2Util::IsIntegerFormat(uint32_t internal_format) {
1192 return (IsUnsignedIntegerFormat(internal_format) ||
1193 IsSignedIntegerFormat(internal_format));
1197 namespace {
1199 // WebGraphicsContext3DCommandBufferImpl configuration attributes. Those in
1200 // the 16-bit range are the same as used by EGL. Those outside the 16-bit range
1201 // are unique to Chromium. Attributes are matched using a closest fit algorithm.
1203 // From <EGL/egl.h>.
1204 const int32 kAlphaSize = 0x3021; // EGL_ALPHA_SIZE
1205 const int32 kBlueSize = 0x3022; // EGL_BLUE_SIZE
1206 const int32 kGreenSize = 0x3023; // EGL_GREEN_SIZE
1207 const int32 kRedSize = 0x3024; // EGL_RED_SIZE
1208 const int32 kDepthSize = 0x3025; // EGL_DEPTH_SIZE
1209 const int32 kStencilSize = 0x3026; // EGL_STENCIL_SIZE
1210 const int32 kSamples = 0x3031; // EGL_SAMPLES
1211 const int32 kSampleBuffers = 0x3032; // EGL_SAMPLE_BUFFERS
1212 const int32 kNone = 0x3038; // EGL_NONE
1213 const int32 kSwapBehavior = 0x3093; // EGL_SWAP_BEHAVIOR
1214 const int32 kBufferPreserved = 0x3094; // EGL_BUFFER_PRESERVED
1215 const int32 kBufferDestroyed = 0x3095; // EGL_BUFFER_DESTROYED
1217 // Chromium only.
1218 const int32 kBindGeneratesResource = 0x10000;
1219 const int32 kFailIfMajorPerfCaveat = 0x10001;
1220 const int32 kLoseContextWhenOutOfMemory = 0x10002;
1221 const int32 kWebGLVersion = 0x10003;
1223 } // namespace
1225 ContextCreationAttribHelper::ContextCreationAttribHelper()
1226 : alpha_size(-1),
1227 blue_size(-1),
1228 green_size(-1),
1229 red_size(-1),
1230 depth_size(-1),
1231 stencil_size(-1),
1232 samples(-1),
1233 sample_buffers(-1),
1234 buffer_preserved(true),
1235 bind_generates_resource(true),
1236 fail_if_major_perf_caveat(false),
1237 lose_context_when_out_of_memory(false),
1238 webgl_version(0) {
1241 void ContextCreationAttribHelper::Serialize(std::vector<int32>* attribs) const {
1242 if (alpha_size != -1) {
1243 attribs->push_back(kAlphaSize);
1244 attribs->push_back(alpha_size);
1246 if (blue_size != -1) {
1247 attribs->push_back(kBlueSize);
1248 attribs->push_back(blue_size);
1250 if (green_size != -1) {
1251 attribs->push_back(kGreenSize);
1252 attribs->push_back(green_size);
1254 if (red_size != -1) {
1255 attribs->push_back(kRedSize);
1256 attribs->push_back(red_size);
1258 if (depth_size != -1) {
1259 attribs->push_back(kDepthSize);
1260 attribs->push_back(depth_size);
1262 if (stencil_size != -1) {
1263 attribs->push_back(kStencilSize);
1264 attribs->push_back(stencil_size);
1266 if (samples != -1) {
1267 attribs->push_back(kSamples);
1268 attribs->push_back(samples);
1270 if (sample_buffers != -1) {
1271 attribs->push_back(kSampleBuffers);
1272 attribs->push_back(sample_buffers);
1274 attribs->push_back(kSwapBehavior);
1275 attribs->push_back(buffer_preserved ? kBufferPreserved : kBufferDestroyed);
1276 attribs->push_back(kBindGeneratesResource);
1277 attribs->push_back(bind_generates_resource ? 1 : 0);
1278 attribs->push_back(kFailIfMajorPerfCaveat);
1279 attribs->push_back(fail_if_major_perf_caveat ? 1 : 0);
1280 attribs->push_back(kLoseContextWhenOutOfMemory);
1281 attribs->push_back(lose_context_when_out_of_memory ? 1 : 0);
1282 attribs->push_back(kWebGLVersion);
1283 attribs->push_back(webgl_version);
1284 attribs->push_back(kNone);
1287 bool ContextCreationAttribHelper::Parse(const std::vector<int32>& attribs) {
1288 for (size_t i = 0; i < attribs.size(); i += 2) {
1289 const int32 attrib = attribs[i];
1290 if (i + 1 >= attribs.size()) {
1291 if (attrib == kNone) {
1292 return true;
1295 DLOG(ERROR) << "Missing value after context creation attribute: "
1296 << attrib;
1297 return false;
1300 const int32 value = attribs[i+1];
1301 switch (attrib) {
1302 case kAlphaSize:
1303 alpha_size = value;
1304 break;
1305 case kBlueSize:
1306 blue_size = value;
1307 break;
1308 case kGreenSize:
1309 green_size = value;
1310 break;
1311 case kRedSize:
1312 red_size = value;
1313 break;
1314 case kDepthSize:
1315 depth_size = value;
1316 break;
1317 case kStencilSize:
1318 stencil_size = value;
1319 break;
1320 case kSamples:
1321 samples = value;
1322 break;
1323 case kSampleBuffers:
1324 sample_buffers = value;
1325 break;
1326 case kSwapBehavior:
1327 buffer_preserved = value == kBufferPreserved;
1328 break;
1329 case kBindGeneratesResource:
1330 bind_generates_resource = value != 0;
1331 break;
1332 case kFailIfMajorPerfCaveat:
1333 fail_if_major_perf_caveat = value != 0;
1334 break;
1335 case kLoseContextWhenOutOfMemory:
1336 lose_context_when_out_of_memory = value != 0;
1337 break;
1338 case kWebGLVersion:
1339 webgl_version = value;
1340 break;
1341 case kNone:
1342 // Terminate list, even if more attributes.
1343 return true;
1344 default:
1345 DLOG(ERROR) << "Invalid context creation attribute: " << attrib;
1346 return false;
1350 return true;
1353 #include "gpu/command_buffer/common/gles2_cmd_utils_implementation_autogen.h"
1355 } // namespace gles2
1356 } // namespace gpu