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