• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // utilities.cpp: Conversion functions and other utility routines.
16 
17 #include "utilities.h"
18 
19 #include "Framebuffer.h"
20 #include "main.h"
21 #include "mathutil.h"
22 #include "Context.h"
23 #include "Shader.h"
24 #include "common/debug.h"
25 
26 #include <limits>
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 namespace es2
31 {
32 
UniformComponentCount(GLenum type)33 	unsigned int UniformComponentCount(GLenum type)
34 	{
35 		switch(type)
36 		{
37 		case GL_BOOL:
38 		case GL_FLOAT:
39 		case GL_INT:
40 		case GL_UNSIGNED_INT:
41 		case GL_SAMPLER_2D:
42 		case GL_SAMPLER_CUBE:
43 		case GL_SAMPLER_2D_RECT_ARB:
44 		case GL_SAMPLER_EXTERNAL_OES:
45 		case GL_SAMPLER_3D_OES:
46 		case GL_SAMPLER_2D_ARRAY:
47 		case GL_SAMPLER_2D_SHADOW:
48 		case GL_SAMPLER_CUBE_SHADOW:
49 		case GL_SAMPLER_2D_ARRAY_SHADOW:
50 		case GL_INT_SAMPLER_2D:
51 		case GL_UNSIGNED_INT_SAMPLER_2D:
52 		case GL_INT_SAMPLER_CUBE:
53 		case GL_UNSIGNED_INT_SAMPLER_CUBE:
54 		case GL_INT_SAMPLER_3D:
55 		case GL_UNSIGNED_INT_SAMPLER_3D:
56 		case GL_INT_SAMPLER_2D_ARRAY:
57 		case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
58 			return 1;
59 		case GL_BOOL_VEC2:
60 		case GL_FLOAT_VEC2:
61 		case GL_INT_VEC2:
62 		case GL_UNSIGNED_INT_VEC2:
63 			return 2;
64 		case GL_INT_VEC3:
65 		case GL_UNSIGNED_INT_VEC3:
66 		case GL_FLOAT_VEC3:
67 		case GL_BOOL_VEC3:
68 			return 3;
69 		case GL_BOOL_VEC4:
70 		case GL_FLOAT_VEC4:
71 		case GL_INT_VEC4:
72 		case GL_UNSIGNED_INT_VEC4:
73 		case GL_FLOAT_MAT2:
74 			return 4;
75 		case GL_FLOAT_MAT2x3:
76 		case GL_FLOAT_MAT3x2:
77 			return 6;
78 		case GL_FLOAT_MAT2x4:
79 		case GL_FLOAT_MAT4x2:
80 			return 8;
81 		case GL_FLOAT_MAT3:
82 			return 9;
83 		case GL_FLOAT_MAT3x4:
84 		case GL_FLOAT_MAT4x3:
85 			return 12;
86 		case GL_FLOAT_MAT4:
87 			return 16;
88 		default:
89 			UNREACHABLE(type);
90 		}
91 
92 		return 0;
93 	}
94 
UniformComponentType(GLenum type)95 	GLenum UniformComponentType(GLenum type)
96 	{
97 		switch(type)
98 		{
99 		case GL_BOOL:
100 		case GL_BOOL_VEC2:
101 		case GL_BOOL_VEC3:
102 		case GL_BOOL_VEC4:
103 			return GL_BOOL;
104 		case GL_FLOAT:
105 		case GL_FLOAT_VEC2:
106 		case GL_FLOAT_VEC3:
107 		case GL_FLOAT_VEC4:
108 		case GL_FLOAT_MAT2:
109 		case GL_FLOAT_MAT2x3:
110 		case GL_FLOAT_MAT2x4:
111 		case GL_FLOAT_MAT3:
112 		case GL_FLOAT_MAT3x2:
113 		case GL_FLOAT_MAT3x4:
114 		case GL_FLOAT_MAT4:
115 		case GL_FLOAT_MAT4x2:
116 		case GL_FLOAT_MAT4x3:
117 			return GL_FLOAT;
118 		case GL_INT:
119 		case GL_SAMPLER_2D:
120 		case GL_SAMPLER_CUBE:
121 		case GL_SAMPLER_2D_RECT_ARB:
122 		case GL_SAMPLER_EXTERNAL_OES:
123 		case GL_SAMPLER_3D_OES:
124 		case GL_SAMPLER_2D_ARRAY:
125 		case GL_SAMPLER_2D_SHADOW:
126 		case GL_SAMPLER_CUBE_SHADOW:
127 		case GL_SAMPLER_2D_ARRAY_SHADOW:
128 		case GL_INT_SAMPLER_2D:
129 		case GL_UNSIGNED_INT_SAMPLER_2D:
130 		case GL_INT_SAMPLER_CUBE:
131 		case GL_UNSIGNED_INT_SAMPLER_CUBE:
132 		case GL_INT_SAMPLER_3D:
133 		case GL_UNSIGNED_INT_SAMPLER_3D:
134 		case GL_INT_SAMPLER_2D_ARRAY:
135 		case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
136 		case GL_INT_VEC2:
137 		case GL_INT_VEC3:
138 		case GL_INT_VEC4:
139 			return GL_INT;
140 		case GL_UNSIGNED_INT:
141 		case GL_UNSIGNED_INT_VEC2:
142 		case GL_UNSIGNED_INT_VEC3:
143 		case GL_UNSIGNED_INT_VEC4:
144 			return GL_UNSIGNED_INT;
145 		default:
146 			UNREACHABLE(type);
147 		}
148 
149 		return GL_NONE;
150 	}
151 
UniformTypeSize(GLenum type)152 	size_t UniformTypeSize(GLenum type)
153 	{
154 		switch(type)
155 		{
156 		case GL_BOOL:  return sizeof(GLboolean);
157 		case GL_FLOAT: return sizeof(GLfloat);
158 		case GL_INT:   return sizeof(GLint);
159 		case GL_UNSIGNED_INT: return sizeof(GLuint);
160 		}
161 
162 		return UniformTypeSize(UniformComponentType(type)) * UniformComponentCount(type);
163 	}
164 
IsSamplerUniform(GLenum type)165 	bool IsSamplerUniform(GLenum type)
166 	{
167 		switch(type)
168 		{
169 		case GL_SAMPLER_2D:
170 		case GL_SAMPLER_CUBE:
171 		case GL_SAMPLER_2D_RECT_ARB:
172 		case GL_SAMPLER_EXTERNAL_OES:
173 		case GL_SAMPLER_3D_OES:
174 		case GL_SAMPLER_2D_ARRAY:
175 		case GL_SAMPLER_2D_SHADOW:
176 		case GL_SAMPLER_CUBE_SHADOW:
177 		case GL_SAMPLER_2D_ARRAY_SHADOW:
178 		case GL_INT_SAMPLER_2D:
179 		case GL_UNSIGNED_INT_SAMPLER_2D:
180 		case GL_INT_SAMPLER_CUBE:
181 		case GL_UNSIGNED_INT_SAMPLER_CUBE:
182 		case GL_INT_SAMPLER_3D:
183 		case GL_UNSIGNED_INT_SAMPLER_3D:
184 		case GL_INT_SAMPLER_2D_ARRAY:
185 		case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
186 			return true;
187 		default:
188 			return false;
189 		}
190 	}
191 
VariableRowCount(GLenum type)192 	int VariableRowCount(GLenum type)
193 	{
194 		switch(type)
195 		{
196 		case GL_NONE:
197 			return 0;
198 		case GL_BOOL:
199 		case GL_FLOAT:
200 		case GL_INT:
201 		case GL_UNSIGNED_INT:
202 		case GL_BOOL_VEC2:
203 		case GL_FLOAT_VEC2:
204 		case GL_INT_VEC2:
205 		case GL_UNSIGNED_INT_VEC2:
206 		case GL_INT_VEC3:
207 		case GL_UNSIGNED_INT_VEC3:
208 		case GL_FLOAT_VEC3:
209 		case GL_BOOL_VEC3:
210 		case GL_BOOL_VEC4:
211 		case GL_FLOAT_VEC4:
212 		case GL_INT_VEC4:
213 		case GL_UNSIGNED_INT_VEC4:
214 		case GL_SAMPLER_2D:
215 		case GL_SAMPLER_CUBE:
216 		case GL_SAMPLER_2D_RECT_ARB:
217 		case GL_SAMPLER_EXTERNAL_OES:
218 		case GL_SAMPLER_3D_OES:
219 		case GL_SAMPLER_2D_ARRAY:
220 		case GL_SAMPLER_2D_SHADOW:
221 		case GL_SAMPLER_CUBE_SHADOW:
222 		case GL_SAMPLER_2D_ARRAY_SHADOW:
223 		case GL_INT_SAMPLER_2D:
224 		case GL_UNSIGNED_INT_SAMPLER_2D:
225 		case GL_INT_SAMPLER_CUBE:
226 		case GL_UNSIGNED_INT_SAMPLER_CUBE:
227 		case GL_INT_SAMPLER_3D:
228 		case GL_UNSIGNED_INT_SAMPLER_3D:
229 		case GL_INT_SAMPLER_2D_ARRAY:
230 		case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
231 			return 1;
232 		case GL_FLOAT_MAT2:
233 		case GL_FLOAT_MAT3x2:
234 		case GL_FLOAT_MAT4x2:
235 			return 2;
236 		case GL_FLOAT_MAT3:
237 		case GL_FLOAT_MAT2x3:
238 		case GL_FLOAT_MAT4x3:
239 			return 3;
240 		case GL_FLOAT_MAT4:
241 		case GL_FLOAT_MAT2x4:
242 		case GL_FLOAT_MAT3x4:
243 			return 4;
244 		default:
245 			UNREACHABLE(type);
246 		}
247 
248 		return 0;
249 	}
250 
VariableColumnCount(GLenum type)251 	int VariableColumnCount(GLenum type)
252 	{
253 		switch(type)
254 		{
255 		case GL_NONE:
256 			return 0;
257 		case GL_BOOL:
258 		case GL_FLOAT:
259 		case GL_INT:
260 		case GL_UNSIGNED_INT:
261 			return 1;
262 		case GL_BOOL_VEC2:
263 		case GL_FLOAT_VEC2:
264 		case GL_INT_VEC2:
265 		case GL_UNSIGNED_INT_VEC2:
266 		case GL_FLOAT_MAT2:
267 		case GL_FLOAT_MAT2x3:
268 		case GL_FLOAT_MAT2x4:
269 			return 2;
270 		case GL_INT_VEC3:
271 		case GL_UNSIGNED_INT_VEC3:
272 		case GL_FLOAT_VEC3:
273 		case GL_BOOL_VEC3:
274 		case GL_FLOAT_MAT3:
275 		case GL_FLOAT_MAT3x2:
276 		case GL_FLOAT_MAT3x4:
277 			return 3;
278 		case GL_BOOL_VEC4:
279 		case GL_FLOAT_VEC4:
280 		case GL_INT_VEC4:
281 		case GL_UNSIGNED_INT_VEC4:
282 		case GL_FLOAT_MAT4:
283 		case GL_FLOAT_MAT4x2:
284 		case GL_FLOAT_MAT4x3:
285 			return 4;
286 		default:
287 			UNREACHABLE(type);
288 		}
289 
290 		return 0;
291 	}
292 
VariableRegisterCount(GLenum type)293 	int VariableRegisterCount(GLenum type)
294 	{
295 		// Number of registers used is the number of columns for matrices or 1 for scalars and vectors
296 		return (VariableRowCount(type) > 1) ? VariableColumnCount(type) : 1;
297 	}
298 
VariableRegisterSize(GLenum type)299 	int VariableRegisterSize(GLenum type)
300 	{
301 		// Number of components per register is the number of rows for matrices or columns for scalars and vectors
302 		int nbRows = VariableRowCount(type);
303 		return (nbRows > 1) ? nbRows : VariableColumnCount(type);
304 	}
305 
AllocateFirstFreeBits(unsigned int * bits,unsigned int allocationSize,unsigned int bitsSize)306 	int AllocateFirstFreeBits(unsigned int *bits, unsigned int allocationSize, unsigned int bitsSize)
307 	{
308 		ASSERT(allocationSize <= bitsSize);
309 
310 		unsigned int mask = std::numeric_limits<unsigned int>::max() >> (std::numeric_limits<unsigned int>::digits - allocationSize);
311 
312 		for(unsigned int i = 0; i < bitsSize - allocationSize + 1; i++)
313 		{
314 			if((*bits & mask) == 0)
315 			{
316 				*bits |= mask;
317 				return i;
318 			}
319 
320 			mask <<= 1;
321 		}
322 
323 		return -1;
324 	}
325 
IsCompressed(GLint internalformat)326 	bool IsCompressed(GLint internalformat)
327 	{
328 		switch(internalformat)
329 		{
330 		case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
331 		case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
332 		case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
333 		case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
334 		case GL_ETC1_RGB8_OES:
335 		case GL_COMPRESSED_R11_EAC:
336 		case GL_COMPRESSED_SIGNED_R11_EAC:
337 		case GL_COMPRESSED_RG11_EAC:
338 		case GL_COMPRESSED_SIGNED_RG11_EAC:
339 		case GL_COMPRESSED_RGB8_ETC2:
340 		case GL_COMPRESSED_SRGB8_ETC2:
341 		case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
342 		case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
343 		case GL_COMPRESSED_RGBA8_ETC2_EAC:
344 		case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
345 			return true;
346 		case GL_COMPRESSED_RGBA_ASTC_4x4_KHR:
347 		case GL_COMPRESSED_RGBA_ASTC_5x4_KHR:
348 		case GL_COMPRESSED_RGBA_ASTC_5x5_KHR:
349 		case GL_COMPRESSED_RGBA_ASTC_6x5_KHR:
350 		case GL_COMPRESSED_RGBA_ASTC_6x6_KHR:
351 		case GL_COMPRESSED_RGBA_ASTC_8x5_KHR:
352 		case GL_COMPRESSED_RGBA_ASTC_8x6_KHR:
353 		case GL_COMPRESSED_RGBA_ASTC_8x8_KHR:
354 		case GL_COMPRESSED_RGBA_ASTC_10x5_KHR:
355 		case GL_COMPRESSED_RGBA_ASTC_10x6_KHR:
356 		case GL_COMPRESSED_RGBA_ASTC_10x8_KHR:
357 		case GL_COMPRESSED_RGBA_ASTC_10x10_KHR:
358 		case GL_COMPRESSED_RGBA_ASTC_12x10_KHR:
359 		case GL_COMPRESSED_RGBA_ASTC_12x12_KHR:
360 		case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
361 		case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
362 		case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
363 		case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
364 		case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
365 		case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
366 		case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
367 		case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
368 		case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
369 		case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
370 		case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
371 		case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
372 		case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
373 		case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
374 			return ASTC_SUPPORT;
375 		default:
376 			return false;
377 		}
378 	}
379 
IsSizedInternalFormat(GLint internalformat)380 	bool IsSizedInternalFormat(GLint internalformat)
381 	{
382 		switch(internalformat)
383 		{
384 		case GL_ALPHA8_EXT:
385 		case GL_LUMINANCE8_EXT:
386 		case GL_LUMINANCE8_ALPHA8_EXT:
387 		case GL_ALPHA32F_EXT:
388 		case GL_LUMINANCE32F_EXT:
389 		case GL_LUMINANCE_ALPHA32F_EXT:
390 		case GL_ALPHA16F_EXT:
391 		case GL_LUMINANCE16F_EXT:
392 		case GL_LUMINANCE_ALPHA16F_EXT:
393 		case GL_R8:
394 		case GL_R8UI:
395 		case GL_R8I:
396 		case GL_R16UI:
397 		case GL_R16I:
398 		case GL_R32UI:
399 		case GL_R32I:
400 		case GL_RG8:
401 		case GL_RG8UI:
402 		case GL_RG8I:
403 		case GL_RG16UI:
404 		case GL_RG16I:
405 		case GL_RG32UI:
406 		case GL_RG32I:
407 		case GL_SRGB8_ALPHA8:
408 		case GL_RGB8UI:
409 		case GL_RGB8I:
410 		case GL_RGB16UI:
411 		case GL_RGB16I:
412 		case GL_RGB32UI:
413 		case GL_RGB32I:
414 		case GL_RG8_SNORM:
415 		case GL_R8_SNORM:
416 		case GL_RGB10_A2:
417 		case GL_RGBA8UI:
418 		case GL_RGBA8I:
419 		case GL_RGB10_A2UI:
420 		case GL_RGBA16UI:
421 		case GL_RGBA16I:
422 		case GL_RGBA32I:
423 		case GL_RGBA32UI:
424 		case GL_RGBA4:
425 		case GL_RGB5_A1:
426 		case GL_RGB565:
427 		case GL_RGB8:
428 		case GL_RGBA8:
429 		case GL_BGRA8_EXT:   // GL_APPLE_texture_format_BGRA8888
430 		case GL_R16F:
431 		case GL_RG16F:
432 		case GL_R11F_G11F_B10F:
433 		case GL_RGB16F:
434 		case GL_RGBA16F:
435 		case GL_R32F:
436 		case GL_RG32F:
437 		case GL_RGB32F:
438 		case GL_RGBA32F:
439 		case GL_DEPTH_COMPONENT24:
440 		case GL_DEPTH_COMPONENT32_OES:
441 		case GL_DEPTH_COMPONENT32F:
442 		case GL_DEPTH32F_STENCIL8:
443 		case GL_DEPTH_COMPONENT16:
444 		case GL_STENCIL_INDEX8:
445 		case GL_DEPTH24_STENCIL8_OES:
446 		case GL_RGBA8_SNORM:
447 		case GL_SRGB8:
448 		case GL_RGB8_SNORM:
449 		case GL_RGB9_E5:
450 			return true;
451 		default:
452 			return false;
453 		}
454 	}
455 
ValidateSubImageParams(bool compressed,bool copy,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLsizei width,GLsizei height,GLenum format,GLenum type,Texture * texture)456 	GLenum ValidateSubImageParams(bool compressed, bool copy, GLenum target, GLint level, GLint xoffset, GLint yoffset,
457 	                              GLsizei width, GLsizei height, GLenum format, GLenum type, Texture *texture)
458 	{
459 		if(!texture)
460 		{
461 			return GL_INVALID_OPERATION;
462 		}
463 
464 		GLenum sizedInternalFormat = texture->getFormat(target, level);
465 
466 		if(compressed)
467 		{
468 			if(format != sizedInternalFormat)
469 			{
470 				return GL_INVALID_OPERATION;
471 			}
472 		}
473 		else if(!copy)   // CopyTexSubImage doesn't have format/type parameters.
474 		{
475 			GLenum validationError = ValidateTextureFormatType(format, type, sizedInternalFormat, target);
476 			if(validationError != GL_NO_ERROR)
477 			{
478 				return validationError;
479 			}
480 		}
481 
482 		if(compressed)
483 		{
484 			if((width % 4 != 0 && width != texture->getWidth(target, 0)) ||
485 			   (height % 4 != 0 && height != texture->getHeight(target, 0)))
486 			{
487 				return GL_INVALID_OPERATION;
488 			}
489 		}
490 
491 		if(xoffset + width > texture->getWidth(target, level) ||
492 		   yoffset + height > texture->getHeight(target, level))
493 		{
494 			return GL_INVALID_VALUE;
495 		}
496 
497 		return GL_NO_ERROR;
498 	}
499 
ValidateSubImageParams(bool compressed,bool copy,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLenum type,Texture * texture)500 	GLenum ValidateSubImageParams(bool compressed, bool copy, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset,
501 	                              GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, Texture *texture)
502 	{
503 		if(!texture)
504 		{
505 			return GL_INVALID_OPERATION;
506 		}
507 
508 		if(compressed != texture->isCompressed(target, level))
509 		{
510 			return GL_INVALID_OPERATION;
511 		}
512 
513 		if(!copy)
514 		{
515 			GLenum sizedInternalFormat = texture->getFormat(target, level);
516 
517 			GLenum validationError = ValidateTextureFormatType(format, type, sizedInternalFormat, target);
518 			if(validationError != GL_NO_ERROR)
519 			{
520 				return validationError;
521 			}
522 		}
523 
524 		if(compressed)
525 		{
526 			if((width % 4 != 0 && width != texture->getWidth(target, 0)) ||
527 			   (height % 4 != 0 && height != texture->getHeight(target, 0)) ||
528 			   (depth % 4 != 0 && depth != texture->getDepth(target, 0)))
529 			{
530 				return GL_INVALID_OPERATION;
531 			}
532 		}
533 
534 		if(xoffset + width > texture->getWidth(target, level) ||
535 		   yoffset + height > texture->getHeight(target, level) ||
536 		   zoffset + depth > texture->getDepth(target, level))
537 		{
538 			return GL_INVALID_VALUE;
539 		}
540 
541 		return GL_NO_ERROR;
542 	}
543 
ValidateCopyFormats(GLenum textureFormat,GLenum colorbufferFormat)544 	bool ValidateCopyFormats(GLenum textureFormat, GLenum colorbufferFormat)
545 	{
546 		ASSERT(!gl::IsUnsizedInternalFormat(textureFormat));
547 		ASSERT(!gl::IsUnsizedInternalFormat(colorbufferFormat));
548 
549 		if(GetColorComponentType(textureFormat) == GL_NONE)
550 		{
551 			return error(GL_INVALID_ENUM, false);
552 		}
553 
554 		if(GetColorComponentType(colorbufferFormat) != GetColorComponentType(textureFormat))
555 		{
556 			return error(GL_INVALID_OPERATION, false);
557 		}
558 
559 		if(GetColorEncoding(colorbufferFormat) != GetColorEncoding(textureFormat))
560 		{
561 			return error(GL_INVALID_OPERATION, false);
562 		}
563 
564 		GLenum baseTexureFormat = gl::GetBaseInternalFormat(textureFormat);
565 		GLenum baseColorbufferFormat = gl::GetBaseInternalFormat(colorbufferFormat);
566 
567 		// [OpenGL ES 2.0.24] table 3.9
568 		// [OpenGL ES 3.0.5] table 3.16
569 		switch(baseTexureFormat)
570 		{
571 		case GL_ALPHA:
572 			if(baseColorbufferFormat != GL_ALPHA &&
573 			   baseColorbufferFormat != GL_RGBA &&
574 			   baseColorbufferFormat != GL_BGRA_EXT)   // GL_EXT_texture_format_BGRA8888 / GL_APPLE_texture_format_BGRA8888
575 			{
576 				return error(GL_INVALID_OPERATION, false);
577 			}
578 			break;
579 		case GL_LUMINANCE_ALPHA:
580 		case GL_RGBA:
581 			if(baseColorbufferFormat != GL_RGBA &&
582 			   baseColorbufferFormat != GL_BGRA_EXT)   // GL_EXT_texture_format_BGRA8888 / GL_APPLE_texture_format_BGRA8888
583 			{
584 				return error(GL_INVALID_OPERATION, false);
585 			}
586 			break;
587 		case GL_LUMINANCE:
588 		case GL_RED:
589 			if(baseColorbufferFormat != GL_RED &&
590 			   baseColorbufferFormat != GL_RG &&
591 			   baseColorbufferFormat != GL_RGB &&
592 			   baseColorbufferFormat != GL_RGBA &&
593 			   baseColorbufferFormat != GL_BGRA_EXT)   // GL_EXT_texture_format_BGRA8888 / GL_APPLE_texture_format_BGRA8888
594 			{
595 				return error(GL_INVALID_OPERATION, false);
596 			}
597 			break;
598 		case GL_RG:
599 			if(baseColorbufferFormat != GL_RG &&
600 			   baseColorbufferFormat != GL_RGB &&
601 			   baseColorbufferFormat != GL_RGBA &&
602 			   baseColorbufferFormat != GL_BGRA_EXT)   // GL_EXT_texture_format_BGRA8888 / GL_APPLE_texture_format_BGRA8888
603 			{
604 				return error(GL_INVALID_OPERATION, false);
605 			}
606 			break;
607 		case GL_RGB:
608 			if(baseColorbufferFormat != GL_RGB &&
609 			   baseColorbufferFormat != GL_RGBA &&
610 			   baseColorbufferFormat != GL_BGRA_EXT)   // GL_EXT_texture_format_BGRA8888 / GL_APPLE_texture_format_BGRA8888
611 			{
612 				return error(GL_INVALID_OPERATION, false);
613 			}
614 			break;
615 		case GL_DEPTH_COMPONENT:
616 		case GL_DEPTH_STENCIL_OES:
617 			return error(GL_INVALID_OPERATION, false);
618 		case GL_BGRA_EXT:   // GL_EXT_texture_format_BGRA8888 nor GL_APPLE_texture_format_BGRA8888 mention the format to be accepted by glCopyTexImage2D.
619 		default:
620 			return error(GL_INVALID_ENUM, false);
621 		}
622 
623 		return true;
624 	}
625 
ValidateReadPixelsFormatType(const Framebuffer * framebuffer,GLenum format,GLenum type)626 	bool ValidateReadPixelsFormatType(const Framebuffer *framebuffer, GLenum format, GLenum type)
627 	{
628 		// GL_NV_read_depth
629 		if(format == GL_DEPTH_COMPONENT)
630 		{
631 			Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
632 
633 			if(!depthbuffer)
634 			{
635 				return error(GL_INVALID_OPERATION, false);
636 			}
637 
638 			GLint internalformat = depthbuffer->getFormat();
639 
640 			switch(type)
641 			{
642 			case GL_UNSIGNED_SHORT:
643 			case GL_UNSIGNED_INT_24_8_OES:
644 				switch(internalformat)
645 				{
646 				case GL_DEPTH_COMPONENT16:
647 				case GL_DEPTH_COMPONENT24:
648 				case GL_DEPTH_COMPONENT32_OES:
649 				case GL_DEPTH24_STENCIL8:
650 					break;
651 				case GL_DEPTH_COMPONENT32F:
652 				case GL_DEPTH32F_STENCIL8:
653 					return error(GL_INVALID_OPERATION, false);
654 				default:
655 					UNREACHABLE(internalformat);
656 					return error(GL_INVALID_OPERATION, false);
657 				}
658 				break;
659 			case GL_FLOAT:
660 				switch(internalformat)
661 				{
662 				case GL_DEPTH_COMPONENT32F:
663 				case GL_DEPTH32F_STENCIL8:
664 					break;
665 				case GL_DEPTH_COMPONENT16:
666 				case GL_DEPTH_COMPONENT24:
667 				case GL_DEPTH_COMPONENT32_OES:
668 				case GL_DEPTH24_STENCIL8:
669 					return error(GL_INVALID_OPERATION, false);
670 				default:
671 					UNREACHABLE(internalformat);
672 					return error(GL_INVALID_OPERATION, false);
673 				}
674 				break;
675 			default:
676 				return error(GL_INVALID_ENUM, false);
677 			}
678 
679 			return true;
680 		}
681 
682 		// GL_NV_read_depth_stencil
683 		if(format == GL_DEPTH_STENCIL_OES)
684 		{
685 			Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
686 
687 			if(!depthbuffer)
688 			{
689 				return error(GL_INVALID_OPERATION, false);
690 			}
691 
692 			GLint internalformat = depthbuffer->getFormat();
693 
694 			switch(type)
695 			{
696 			case GL_UNSIGNED_INT_24_8_OES:
697 				switch(internalformat)
698 				{
699 				case GL_DEPTH24_STENCIL8:
700 					break;
701 				case GL_DEPTH32F_STENCIL8:
702 					return error(GL_INVALID_OPERATION, false);
703 				default:
704 					UNREACHABLE(internalformat);
705 					return error(GL_INVALID_OPERATION, false);
706 				}
707 				break;
708 			case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
709 				switch(internalformat)
710 				{
711 				case GL_DEPTH32F_STENCIL8:
712 					break;
713 				case GL_DEPTH24_STENCIL8:
714 					return error(GL_INVALID_OPERATION, false);
715 				default:
716 					UNREACHABLE(internalformat);
717 					return error(GL_INVALID_OPERATION, false);
718 				}
719 				break;
720 			default:
721 				return error(GL_INVALID_ENUM, false);
722 			}
723 
724 			return true;
725 		}
726 
727 		// GL_NV_read_stencil
728 		if(format == GL_STENCIL_INDEX_OES)
729 		{
730 			Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
731 
732 			if(!stencilbuffer)
733 			{
734 				return error(GL_INVALID_OPERATION, false);
735 			}
736 
737 			switch(type)
738 			{
739 			case GL_UNSIGNED_BYTE:
740 				break;
741 			default:
742 				return error(GL_INVALID_ENUM, false);
743 			}
744 
745 			return true;
746 		}
747 
748 		Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
749 
750 		if(!colorbuffer)
751 		{
752 			return error(GL_INVALID_OPERATION, false);
753 		}
754 
755 		GLint internalformat = colorbuffer->getFormat();
756 
757 		if(IsNormalizedInteger(internalformat))
758 		{
759 			// Combination always supported by normalized fixed-point rendering surfaces.
760 			if(format == GL_RGBA && type == GL_UNSIGNED_BYTE)
761 			{
762 				return true;
763 			}
764 
765 			// GL_EXT_read_format_bgra combinations.
766 			if(format == GL_BGRA_EXT)
767 			{
768 				if(type == GL_UNSIGNED_BYTE ||
769 				   type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT ||
770 				   type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT)
771 				{
772 					return true;
773 				}
774 			}
775 		}
776 		else if(IsFloatFormat(internalformat))
777 		{
778 			// Combination always supported by floating-point rendering surfaces.
779 			// Supported in OpenGL ES 2.0 due to GL_EXT_color_buffer_half_float.
780 			if(format == GL_RGBA && type == GL_FLOAT)
781 			{
782 				return true;
783 			}
784 		}
785 		else if(IsSignedNonNormalizedInteger(internalformat))
786 		{
787 			if(format == GL_RGBA_INTEGER && type == GL_INT)
788 			{
789 				return true;
790 			}
791 		}
792 		else if(IsUnsignedNonNormalizedInteger(internalformat))
793 		{
794 			if(format == GL_RGBA_INTEGER && type == GL_UNSIGNED_INT)
795 			{
796 				return true;
797 			}
798 		}
799 		else UNREACHABLE(internalformat);
800 
801 		// GL_IMPLEMENTATION_COLOR_READ_FORMAT / GL_IMPLEMENTATION_COLOR_READ_TYPE
802 		GLenum implementationReadFormat = GL_NONE;
803 		GLenum implementationReadType = GL_NONE;
804 		switch(format)
805 		{
806 		default:
807 			implementationReadFormat = framebuffer->getImplementationColorReadFormat();
808 			implementationReadType = framebuffer->getImplementationColorReadType();
809 			break;
810 		case GL_DEPTH_COMPONENT:
811 			implementationReadFormat = framebuffer->getDepthReadFormat();
812 			implementationReadType = framebuffer->getDepthReadType();
813 			break;
814 		}
815 
816 		GLenum coreType = (type == GL_HALF_FLOAT_OES) ? GL_HALF_FLOAT : type;
817 
818 		if(format == implementationReadFormat && coreType == implementationReadType)
819 		{
820 			return true;
821 		}
822 
823 		// Additional third combination accepted by OpenGL ES 3.0.
824 		if(internalformat == GL_RGB10_A2)
825 		{
826 			if(format == GL_RGBA && type == GL_UNSIGNED_INT_2_10_10_10_REV)
827 			{
828 				return true;
829 			}
830 		}
831 
832 		return error(GL_INVALID_OPERATION, false);
833 	}
834 
IsDepthTexture(GLint format)835 	bool IsDepthTexture(GLint format)
836 	{
837 		return format == GL_DEPTH_COMPONENT16 ||
838 		       format == GL_DEPTH_COMPONENT24 ||
839 		       format == GL_DEPTH_COMPONENT32_OES ||
840 		       format == GL_DEPTH_COMPONENT32F ||
841 		       format == GL_DEPTH24_STENCIL8 ||
842 		       format == GL_DEPTH32F_STENCIL8;
843 	}
844 
IsStencilTexture(GLint format)845 	bool IsStencilTexture(GLint format)
846 	{
847 		return format == GL_DEPTH24_STENCIL8 ||
848 		       format == GL_DEPTH32F_STENCIL8 ||
849 		       format == GL_STENCIL_INDEX8;
850 	}
851 
IsCubemapTextureTarget(GLenum target)852 	bool IsCubemapTextureTarget(GLenum target)
853 	{
854 		return (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z);
855 	}
856 
CubeFaceIndex(GLenum cubeFace)857 	int CubeFaceIndex(GLenum cubeFace)
858 	{
859 		switch(cubeFace)
860 		{
861 		case GL_TEXTURE_CUBE_MAP:
862 		case GL_TEXTURE_CUBE_MAP_POSITIVE_X: return 0;
863 		case GL_TEXTURE_CUBE_MAP_NEGATIVE_X: return 1;
864 		case GL_TEXTURE_CUBE_MAP_POSITIVE_Y: return 2;
865 		case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y: return 3;
866 		case GL_TEXTURE_CUBE_MAP_POSITIVE_Z: return 4;
867 		case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: return 5;
868 		default: UNREACHABLE(cubeFace); return 0;
869 		}
870 	}
871 
IsTextureTarget(GLenum target)872 	bool IsTextureTarget(GLenum target)
873 	{
874 		return target == GL_TEXTURE_2D || IsCubemapTextureTarget(target) || target == GL_TEXTURE_3D || target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_RECTANGLE_ARB;
875 	}
876 
ValidateTextureFormatType(GLenum format,GLenum type,GLint internalformat,GLenum target)877 	GLenum ValidateTextureFormatType(GLenum format, GLenum type, GLint internalformat, GLenum target)
878 	{
879 		switch(type)
880 		{
881 		case GL_UNSIGNED_BYTE:
882 		case GL_UNSIGNED_SHORT_4_4_4_4:
883 		case GL_UNSIGNED_SHORT_5_5_5_1:
884 		case GL_UNSIGNED_SHORT_5_6_5:
885 		case GL_FLOAT:               // GL_OES_texture_float
886 		case GL_HALF_FLOAT_OES:      // GL_OES_texture_half_float
887 		case GL_HALF_FLOAT:
888 		case GL_UNSIGNED_INT_24_8:   // GL_OES_packed_depth_stencil (GL_UNSIGNED_INT_24_8_EXT)
889 		case GL_UNSIGNED_SHORT:      // GL_OES_depth_texture
890 		case GL_UNSIGNED_INT:        // GL_OES_depth_texture
891 			break;
892 		case GL_BYTE:
893 		case GL_SHORT:
894 		case GL_INT:
895 		case GL_UNSIGNED_INT_2_10_10_10_REV:
896 		case GL_UNSIGNED_INT_10F_11F_11F_REV:
897 		case GL_UNSIGNED_INT_5_9_9_9_REV:
898 		case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
899 			break;
900 		default:
901 			return GL_INVALID_ENUM;
902 		}
903 
904 		switch(format)
905 		{
906 		case GL_ALPHA:
907 		case GL_RGB:
908 		case GL_RGBA:
909 		case GL_LUMINANCE:
910 		case GL_LUMINANCE_ALPHA:
911 		case GL_BGRA_EXT:          // GL_EXT_texture_format_BGRA8888
912 		case GL_RED_EXT:           // GL_EXT_texture_rg
913 		case GL_RG_EXT:            // GL_EXT_texture_rg
914 			break;
915 		case GL_DEPTH_STENCIL:     // GL_OES_packed_depth_stencil (GL_DEPTH_STENCIL_OES)
916 		case GL_DEPTH_COMPONENT:   // GL_OES_depth_texture
917 			switch(target)
918 			{
919 			case GL_TEXTURE_2D:
920 			case GL_TEXTURE_2D_ARRAY:
921 			case GL_TEXTURE_CUBE_MAP_POSITIVE_X:   // GL_OES_depth_texture_cube_map
922 			case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
923 			case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
924 			case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
925 			case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
926 			case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
927 				break;
928 			default:
929 				return GL_INVALID_OPERATION;
930 			}
931 			break;
932 		case GL_RED_INTEGER:
933 		case GL_RG_INTEGER:
934 		case GL_RGB_INTEGER:
935 		case GL_RGBA_INTEGER:
936 			break;
937 		default:
938 			return GL_INVALID_ENUM;
939 		}
940 
941 		if((GLenum)internalformat != format)
942 		{
943 			if(gl::IsUnsizedInternalFormat(internalformat))
944 			{
945 				return GL_INVALID_OPERATION;
946 			}
947 
948 			if(!IsSizedInternalFormat(internalformat))
949 			{
950 				return GL_INVALID_VALUE;
951 			}
952 		}
953 
954 		if((GLenum)internalformat == format)
955 		{
956 			// Validate format, type, and unsized internalformat combinations [OpenGL ES 3.0 Table 3.3]
957 			switch(format)
958 			{
959 			case GL_RGBA:
960 				switch(type)
961 				{
962 				case GL_UNSIGNED_BYTE:
963 				case GL_UNSIGNED_SHORT_4_4_4_4:
964 				case GL_UNSIGNED_SHORT_5_5_5_1:
965 				case GL_FLOAT:            // GL_OES_texture_float
966 				case GL_HALF_FLOAT_OES:   // GL_OES_texture_half_float
967 					break;
968 				default:
969 					return GL_INVALID_OPERATION;
970 				}
971 				break;
972 			case GL_RGB:
973 				switch(type)
974 				{
975 				case GL_UNSIGNED_BYTE:
976 				case GL_UNSIGNED_SHORT_5_6_5:
977 				case GL_FLOAT:            // GL_OES_texture_float
978 				case GL_HALF_FLOAT_OES:   // GL_OES_texture_half_float
979 					break;
980 				default:
981 					return GL_INVALID_OPERATION;
982 				}
983 				break;
984 			case GL_LUMINANCE_ALPHA:
985 			case GL_LUMINANCE:
986 			case GL_ALPHA:
987 				switch(type)
988 				{
989 				case GL_UNSIGNED_BYTE:
990 				case GL_FLOAT:            // GL_OES_texture_float
991 				case GL_HALF_FLOAT_OES:   // GL_OES_texture_half_float
992 					break;
993 				default:
994 					return GL_INVALID_OPERATION;
995 				}
996 				break;
997 			case GL_DEPTH_COMPONENT:
998 				switch(type)
999 				{
1000 				case GL_UNSIGNED_SHORT:   // GL_OES_depth_texture
1001 				case GL_UNSIGNED_INT:     // GL_OES_depth_texture
1002 					break;
1003 				default:
1004 					return GL_INVALID_OPERATION;
1005 				}
1006 				break;
1007 			case GL_DEPTH_STENCIL_OES:
1008 				switch(type)
1009 				{
1010 				case GL_UNSIGNED_INT_24_8_OES:   // GL_OES_packed_depth_stencil
1011 					break;
1012 				default:
1013 					return GL_INVALID_OPERATION;
1014 				}
1015 				break;
1016 			case GL_RED_EXT:
1017 			case GL_RG_EXT:
1018 				switch(type)
1019 				{
1020 				case GL_UNSIGNED_BYTE:    // GL_EXT_texture_rg
1021 				case GL_FLOAT:            // GL_EXT_texture_rg + GL_OES_texture_float
1022 				case GL_HALF_FLOAT_OES:   // GL_EXT_texture_rg + GL_OES_texture_half_float
1023 					break;
1024 				default:
1025 					return GL_INVALID_OPERATION;
1026 				}
1027 				break;
1028 			case GL_BGRA_EXT:
1029 				if(type != GL_UNSIGNED_BYTE)   // GL_APPLE_texture_format_BGRA8888 / GL_EXT_texture_format_BGRA8888
1030 				{
1031 					return GL_INVALID_OPERATION;
1032 				}
1033 				break;
1034 			default:
1035 				UNREACHABLE(format);
1036 				return GL_INVALID_ENUM;
1037 			}
1038 
1039 			return GL_NO_ERROR;
1040 		}
1041 
1042 		// Validate format, type, and sized internalformat combinations [OpenGL ES 3.0 Table 3.2]
1043 		bool validSizedInternalformat = false;
1044 		#define VALIDATE_INTERNALFORMAT(...) { GLint validInternalformats[] = {__VA_ARGS__}; for(GLint v : validInternalformats) {if(internalformat == v) validSizedInternalformat = true;} } break;
1045 
1046 		switch(format)
1047 		{
1048 		case GL_RGBA:
1049 			switch(type)
1050 			{
1051 			case GL_UNSIGNED_BYTE:               VALIDATE_INTERNALFORMAT(GL_RGBA8, GL_RGB5_A1, GL_RGBA4, GL_SRGB8_ALPHA8)
1052 			case GL_BYTE:                        VALIDATE_INTERNALFORMAT(GL_RGBA8_SNORM)
1053 			case GL_UNSIGNED_SHORT_4_4_4_4:      VALIDATE_INTERNALFORMAT(GL_RGBA4)
1054 			case GL_UNSIGNED_SHORT_5_5_5_1:      VALIDATE_INTERNALFORMAT(GL_RGB5_A1)
1055 			case GL_UNSIGNED_INT_2_10_10_10_REV: VALIDATE_INTERNALFORMAT(GL_RGB10_A2, GL_RGB5_A1)
1056 			case GL_HALF_FLOAT_OES:
1057 			case GL_HALF_FLOAT:                  VALIDATE_INTERNALFORMAT(GL_RGBA16F)
1058 			case GL_FLOAT:                       VALIDATE_INTERNALFORMAT(GL_RGBA32F, GL_RGBA16F)
1059 			default:                             return GL_INVALID_OPERATION;
1060 			}
1061 			break;
1062 		case GL_RGBA_INTEGER:
1063 			switch(type)
1064 			{
1065 			case GL_UNSIGNED_BYTE:               VALIDATE_INTERNALFORMAT(GL_RGBA8UI)
1066 			case GL_BYTE:                        VALIDATE_INTERNALFORMAT(GL_RGBA8I)
1067 			case GL_UNSIGNED_SHORT:              VALIDATE_INTERNALFORMAT(GL_RGBA16UI)
1068 			case GL_SHORT:                       VALIDATE_INTERNALFORMAT(GL_RGBA16I)
1069 			case GL_UNSIGNED_INT:                VALIDATE_INTERNALFORMAT(GL_RGBA32UI)
1070 			case GL_INT:                         VALIDATE_INTERNALFORMAT(GL_RGBA32I)
1071 			case GL_UNSIGNED_INT_2_10_10_10_REV: VALIDATE_INTERNALFORMAT(GL_RGB10_A2UI)
1072 			default:                             return GL_INVALID_OPERATION;
1073 			}
1074 			break;
1075 		case GL_RGB:
1076 			switch(type)
1077 			{
1078 			case GL_UNSIGNED_BYTE:                VALIDATE_INTERNALFORMAT(GL_RGB8, GL_RGB565, GL_SRGB8)
1079 			case GL_BYTE:                         VALIDATE_INTERNALFORMAT(GL_RGB8_SNORM)
1080 			case GL_UNSIGNED_SHORT_5_6_5:         VALIDATE_INTERNALFORMAT(GL_RGB565)
1081 			case GL_UNSIGNED_INT_10F_11F_11F_REV: VALIDATE_INTERNALFORMAT(GL_R11F_G11F_B10F)
1082 			case GL_UNSIGNED_INT_5_9_9_9_REV:     VALIDATE_INTERNALFORMAT(GL_RGB9_E5)
1083 			case GL_HALF_FLOAT_OES:
1084 			case GL_HALF_FLOAT:                   VALIDATE_INTERNALFORMAT(GL_RGB16F, GL_R11F_G11F_B10F, GL_RGB9_E5)
1085 			case GL_FLOAT:                        VALIDATE_INTERNALFORMAT(GL_RGB32F, GL_RGB16F, GL_R11F_G11F_B10F, GL_RGB9_E5)
1086 			default:                              return GL_INVALID_OPERATION;
1087 			}
1088 			break;
1089 		case GL_RGB_INTEGER:
1090 			switch(type)
1091 			{
1092 			case GL_UNSIGNED_BYTE:  VALIDATE_INTERNALFORMAT(GL_RGB8UI)
1093 			case GL_BYTE:           VALIDATE_INTERNALFORMAT(GL_RGB8I)
1094 			case GL_UNSIGNED_SHORT: VALIDATE_INTERNALFORMAT(GL_RGB16UI)
1095 			case GL_SHORT:          VALIDATE_INTERNALFORMAT(GL_RGB16I)
1096 			case GL_UNSIGNED_INT:   VALIDATE_INTERNALFORMAT(GL_RGB32UI)
1097 			case GL_INT:            VALIDATE_INTERNALFORMAT(GL_RGB32I)
1098 			default:                return GL_INVALID_OPERATION;
1099 			}
1100 			break;
1101 		case GL_RG:
1102 			switch(type)
1103 			{
1104 			case GL_UNSIGNED_BYTE:  VALIDATE_INTERNALFORMAT(GL_RG8)
1105 			case GL_BYTE:           VALIDATE_INTERNALFORMAT(GL_RG8_SNORM)
1106 			case GL_HALF_FLOAT_OES:
1107 			case GL_HALF_FLOAT:     VALIDATE_INTERNALFORMAT(GL_RG16F)
1108 			case GL_FLOAT:          VALIDATE_INTERNALFORMAT(GL_RG32F, GL_RG16F)
1109 			default:                return GL_INVALID_OPERATION;
1110 			}
1111 			break;
1112 		case GL_RG_INTEGER:
1113 			switch(type)
1114 			{
1115 			case GL_UNSIGNED_BYTE:  VALIDATE_INTERNALFORMAT(GL_RG8UI)
1116 			case GL_BYTE:           VALIDATE_INTERNALFORMAT(GL_RG8I)
1117 			case GL_UNSIGNED_SHORT: VALIDATE_INTERNALFORMAT(GL_RG16UI)
1118 			case GL_SHORT:          VALIDATE_INTERNALFORMAT(GL_RG16I)
1119 			case GL_UNSIGNED_INT:   VALIDATE_INTERNALFORMAT(GL_RG32UI)
1120 			case GL_INT:            VALIDATE_INTERNALFORMAT(GL_RG32I)
1121 			default:                return GL_INVALID_OPERATION;
1122 			}
1123 			break;
1124 		case GL_RED:
1125 			switch(type)
1126 			{
1127 			case GL_UNSIGNED_BYTE:  VALIDATE_INTERNALFORMAT(GL_R8)
1128 			case GL_BYTE:           VALIDATE_INTERNALFORMAT(GL_R8_SNORM)
1129 			case GL_HALF_FLOAT_OES:
1130 			case GL_HALF_FLOAT:     VALIDATE_INTERNALFORMAT(GL_R16F)
1131 			case GL_FLOAT:          VALIDATE_INTERNALFORMAT(GL_R32F, GL_R16F)
1132 			default:                return GL_INVALID_OPERATION;
1133 			}
1134 			break;
1135 		case GL_RED_INTEGER:
1136 			switch(type)
1137 			{
1138 			case GL_UNSIGNED_BYTE:  VALIDATE_INTERNALFORMAT(GL_R8UI)
1139 			case GL_BYTE:           VALIDATE_INTERNALFORMAT(GL_R8I)
1140 			case GL_UNSIGNED_SHORT: VALIDATE_INTERNALFORMAT(GL_R16UI)
1141 			case GL_SHORT:          VALIDATE_INTERNALFORMAT(GL_R16I)
1142 			case GL_UNSIGNED_INT:   VALIDATE_INTERNALFORMAT(GL_R32UI)
1143 			case GL_INT:            VALIDATE_INTERNALFORMAT(GL_R32I)
1144 			default:                return GL_INVALID_OPERATION;
1145 			}
1146 			break;
1147 		case GL_DEPTH_COMPONENT:
1148 			switch(type)
1149 			{
1150 			case GL_UNSIGNED_SHORT: VALIDATE_INTERNALFORMAT(GL_DEPTH_COMPONENT16)
1151 			case GL_UNSIGNED_INT:   VALIDATE_INTERNALFORMAT(GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT16)
1152 			case GL_FLOAT:          VALIDATE_INTERNALFORMAT(GL_DEPTH_COMPONENT32F)
1153 			default:                return GL_INVALID_OPERATION;
1154 			}
1155 			break;
1156 		case GL_DEPTH_STENCIL:
1157 			switch(type)
1158 			{
1159 			case GL_UNSIGNED_INT_24_8:              VALIDATE_INTERNALFORMAT(GL_DEPTH24_STENCIL8)
1160 			case GL_FLOAT_32_UNSIGNED_INT_24_8_REV: VALIDATE_INTERNALFORMAT(GL_DEPTH32F_STENCIL8)
1161 			default:                                return GL_INVALID_OPERATION;
1162 			}
1163 			break;
1164 		case GL_LUMINANCE_ALPHA:
1165 			switch(type)
1166 			{
1167 			case GL_UNSIGNED_BYTE:  VALIDATE_INTERNALFORMAT(GL_LUMINANCE8_ALPHA8_EXT)
1168 			case GL_HALF_FLOAT_OES:
1169 			case GL_HALF_FLOAT:     VALIDATE_INTERNALFORMAT(GL_LUMINANCE_ALPHA16F_EXT)
1170 			case GL_FLOAT:          VALIDATE_INTERNALFORMAT(GL_LUMINANCE_ALPHA32F_EXT, GL_LUMINANCE_ALPHA16F_EXT)
1171 			default:
1172 				return GL_INVALID_OPERATION;
1173 			}
1174 			break;
1175 		case GL_LUMINANCE:
1176 			switch(type)
1177 			{
1178 			case GL_UNSIGNED_BYTE:  VALIDATE_INTERNALFORMAT(GL_LUMINANCE8_EXT)
1179 			case GL_HALF_FLOAT_OES:
1180 			case GL_HALF_FLOAT:     VALIDATE_INTERNALFORMAT(GL_LUMINANCE16F_EXT)
1181 			case GL_FLOAT:          VALIDATE_INTERNALFORMAT(GL_LUMINANCE32F_EXT, GL_LUMINANCE16F_EXT)
1182 			default:
1183 				return GL_INVALID_OPERATION;
1184 			}
1185 			break;
1186 		case GL_ALPHA:
1187 			switch(type)
1188 			{
1189 			case GL_UNSIGNED_BYTE:  VALIDATE_INTERNALFORMAT(GL_ALPHA8_EXT)
1190 			case GL_HALF_FLOAT_OES:
1191 			case GL_HALF_FLOAT:     VALIDATE_INTERNALFORMAT(GL_ALPHA16F_EXT)
1192 			case GL_FLOAT:          VALIDATE_INTERNALFORMAT(GL_ALPHA32F_EXT, GL_ALPHA16F_EXT)
1193 			default:
1194 				return GL_INVALID_OPERATION;
1195 			}
1196 			break;
1197 		case GL_BGRA_EXT:   // GL_APPLE_texture_format_BGRA8888
1198 			switch(type)
1199 			{
1200 			case GL_UNSIGNED_BYTE: VALIDATE_INTERNALFORMAT(GL_BGRA8_EXT)
1201 			default:               return GL_INVALID_OPERATION;
1202 			}
1203 			break;
1204 		default:
1205 			UNREACHABLE(format);
1206 			return GL_INVALID_ENUM;
1207 		}
1208 
1209 		#undef VALIDATE_INTERNALFORMAT
1210 
1211 		if(!validSizedInternalformat)
1212 		{
1213 			return GL_INVALID_OPERATION;
1214 		}
1215 
1216 		return GL_NO_ERROR;
1217 	}
1218 
GetTypeSize(GLenum type)1219 	size_t GetTypeSize(GLenum type)
1220 	{
1221 		switch(type)
1222 		{
1223 		case GL_BYTE:
1224 		case GL_UNSIGNED_BYTE:
1225 			return 1;
1226 		case GL_UNSIGNED_SHORT_4_4_4_4:
1227 		case GL_UNSIGNED_SHORT_5_5_5_1:
1228 		case GL_UNSIGNED_SHORT_5_6_5:
1229 		case GL_UNSIGNED_SHORT:
1230 		case GL_SHORT:
1231 		case GL_HALF_FLOAT:
1232 		case GL_HALF_FLOAT_OES:
1233 			return 2;
1234 		case GL_FLOAT:
1235 		case GL_UNSIGNED_INT_24_8:
1236 		case GL_UNSIGNED_INT:
1237 		case GL_INT:
1238 		case GL_UNSIGNED_INT_2_10_10_10_REV:
1239 		case GL_UNSIGNED_INT_10F_11F_11F_REV:
1240 		case GL_UNSIGNED_INT_5_9_9_9_REV:
1241 			return 4;
1242 		case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
1243 			return 8;
1244 		default:
1245 			UNREACHABLE(type);
1246 			break;
1247 		}
1248 
1249 		return 1;
1250 	}
1251 
ConvertReadFormatType(GLenum format,GLenum type)1252 	sw::Format ConvertReadFormatType(GLenum format, GLenum type)
1253 	{
1254 		switch(format)
1255 		{
1256 		case GL_LUMINANCE:
1257 			switch(type)
1258 			{
1259 			case GL_UNSIGNED_BYTE:  return sw::FORMAT_L8;
1260 			case GL_HALF_FLOAT:     return sw::FORMAT_L16F;
1261 			case GL_HALF_FLOAT_OES: return sw::FORMAT_L16F;
1262 			case GL_FLOAT:          return sw::FORMAT_L32F;
1263 			default: UNREACHABLE(type);
1264 			}
1265 			break;
1266 		case GL_LUMINANCE_ALPHA:
1267 			switch(type)
1268 			{
1269 			case GL_UNSIGNED_BYTE:  return sw::FORMAT_A8L8;
1270 			case GL_HALF_FLOAT:     return sw::FORMAT_A16L16F;
1271 			case GL_HALF_FLOAT_OES: return sw::FORMAT_A16L16F;
1272 			case GL_FLOAT:          return sw::FORMAT_A32L32F;
1273 			default: UNREACHABLE(type);
1274 			}
1275 			break;
1276 		case GL_RGBA:
1277 			switch(type)
1278 			{
1279 			case GL_UNSIGNED_BYTE:          return sw::FORMAT_A8B8G8R8;
1280 			case GL_UNSIGNED_SHORT_4_4_4_4: return sw::FORMAT_R4G4B4A4;
1281 			case GL_UNSIGNED_SHORT_5_5_5_1: return sw::FORMAT_R5G5B5A1;
1282 			case GL_HALF_FLOAT:             return sw::FORMAT_A16B16G16R16F;
1283 			case GL_HALF_FLOAT_OES:         return sw::FORMAT_A16B16G16R16F;
1284 			case GL_FLOAT:                  return sw::FORMAT_A32B32G32R32F;
1285 			case GL_UNSIGNED_INT_2_10_10_10_REV_EXT: return sw::FORMAT_A2B10G10R10;
1286 			default: UNREACHABLE(type);
1287 			}
1288 			break;
1289 		case GL_BGRA_EXT:
1290 			switch(type)
1291 			{
1292 			case GL_UNSIGNED_BYTE:                  return sw::FORMAT_A8R8G8B8;
1293 			case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT: return sw::FORMAT_A4R4G4B4;
1294 			case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT: return sw::FORMAT_A1R5G5B5;
1295 			default: UNREACHABLE(type);
1296 			}
1297 			break;
1298 		case GL_RGB:
1299 			switch(type)
1300 			{
1301 			case GL_UNSIGNED_BYTE:          return sw::FORMAT_B8G8R8;
1302 			case GL_UNSIGNED_SHORT_5_6_5:   return sw::FORMAT_R5G6B5;
1303 			case GL_HALF_FLOAT:             return sw::FORMAT_B16G16R16F;
1304 			case GL_HALF_FLOAT_OES:         return sw::FORMAT_B16G16R16F;
1305 			case GL_FLOAT:                  return sw::FORMAT_B32G32R32F;
1306 			default: UNREACHABLE(type);
1307 			}
1308 			break;
1309 		case GL_RG:
1310 			switch(type)
1311 			{
1312 			case GL_UNSIGNED_BYTE:          return sw::FORMAT_G8R8;
1313 			case GL_HALF_FLOAT:             return sw::FORMAT_G16R16F;
1314 			case GL_HALF_FLOAT_OES:         return sw::FORMAT_G16R16F;
1315 			case GL_FLOAT:                  return sw::FORMAT_G32R32F;
1316 			default: UNREACHABLE(type);
1317 			}
1318 			break;
1319 		case GL_RED:
1320 			switch(type)
1321 			{
1322 			case GL_UNSIGNED_BYTE:          return sw::FORMAT_R8;
1323 			case GL_HALF_FLOAT:             return sw::FORMAT_R16F;
1324 			case GL_HALF_FLOAT_OES:         return sw::FORMAT_R16F;
1325 			case GL_FLOAT:                  return sw::FORMAT_R32F;
1326 			default: UNREACHABLE(type);
1327 			}
1328 			break;
1329 		case GL_ALPHA:
1330 			switch(type)
1331 			{
1332 			case GL_UNSIGNED_BYTE:          return sw::FORMAT_A8;
1333 			case GL_HALF_FLOAT:             return sw::FORMAT_A16F;
1334 			case GL_HALF_FLOAT_OES:         return sw::FORMAT_A16F;
1335 			case GL_FLOAT:                  return sw::FORMAT_A32F;
1336 			default: UNREACHABLE(type);
1337 			}
1338 			break;
1339 		case GL_RED_INTEGER:
1340 			switch(type)
1341 			{
1342 			case GL_INT:          return sw::FORMAT_R32I;
1343 			case GL_UNSIGNED_INT: return sw::FORMAT_R32UI;
1344 			default: UNREACHABLE(type);
1345 			}
1346 			break;
1347 		case GL_RG_INTEGER:
1348 			switch(type)
1349 			{
1350 			case GL_INT:          return sw::FORMAT_G32R32I;
1351 			case GL_UNSIGNED_INT: return sw::FORMAT_G32R32UI;
1352 			default: UNREACHABLE(type);
1353 			}
1354 			break;
1355 		case GL_RGB_INTEGER:
1356 			switch(type)
1357 			{
1358 			case GL_INT:          return sw::FORMAT_X32B32G32R32I;
1359 			case GL_UNSIGNED_INT: return sw::FORMAT_X32B32G32R32UI;
1360 			default: UNREACHABLE(type);
1361 			}
1362 			break;
1363 		case GL_RGBA_INTEGER:
1364 			switch(type)
1365 			{
1366 			case GL_INT:          return sw::FORMAT_A32B32G32R32I;
1367 			case GL_UNSIGNED_INT: return sw::FORMAT_A32B32G32R32UI;
1368 			case GL_UNSIGNED_INT_2_10_10_10_REV: return sw::FORMAT_A2B10G10R10UI;
1369 			default: UNREACHABLE(type);
1370 			}
1371 			break;
1372 		case GL_DEPTH_COMPONENT:
1373 			switch(type)
1374 			{
1375 			case GL_UNSIGNED_SHORT:        return sw::FORMAT_D16;
1376 			case GL_UNSIGNED_INT_24_8_OES: return sw::FORMAT_D24X8;
1377 			case GL_FLOAT:                 return sw::FORMAT_D32F_LOCKABLE;
1378 			default: UNREACHABLE(type);
1379 			}
1380 			break;
1381 		case GL_STENCIL_INDEX_OES:
1382 			switch(type)
1383 			{
1384 			case GL_UNSIGNED_BYTE: return sw::FORMAT_S8;
1385 			default: UNREACHABLE(type);
1386 			}
1387 			break;
1388 		case GL_DEPTH_STENCIL_OES:   // Cannot be read as one format. Handled separately.
1389 		default:
1390 			UNREACHABLE(format);
1391 			break;
1392 		}
1393 
1394 		return sw::FORMAT_NULL;
1395 	}
1396 
IsColorRenderable(GLint internalformat)1397 	bool IsColorRenderable(GLint internalformat)
1398 	{
1399 		if(IsCompressed(internalformat))
1400 		{
1401 			return false;
1402 		}
1403 
1404 		switch(internalformat)
1405 		{
1406 		case GL_RGBA4:
1407 		case GL_RGB5_A1:
1408 		case GL_RGB565:
1409 		case GL_R8:
1410 		case GL_RG8:
1411 		case GL_RGB8:
1412 		case GL_RGBA8:
1413 		case GL_R16F:
1414 		case GL_RG16F:
1415 		case GL_RGB16F:
1416 		case GL_RGBA16F:
1417 		case GL_R32F:
1418 		case GL_RG32F:
1419 		case GL_RGB32F:
1420 		case GL_RGBA32F:     // GL_EXT_color_buffer_float, OpenGL ES 3.0+ only.
1421 		case GL_BGRA8_EXT:   // GL_EXT_texture_format_BGRA8888
1422 		case GL_R8UI:
1423 		case GL_R8I:
1424 		case GL_R16UI:
1425 		case GL_R16I:
1426 		case GL_R32UI:
1427 		case GL_R32I:
1428 		case GL_RG8UI:
1429 		case GL_RG8I:
1430 		case GL_RG16UI:
1431 		case GL_RG16I:
1432 		case GL_RG32UI:
1433 		case GL_RG32I:
1434 		case GL_SRGB8_ALPHA8:
1435 		case GL_RGB10_A2:
1436 		case GL_RGBA8UI:
1437 		case GL_RGBA8I:
1438 		case GL_RGB10_A2UI:
1439 		case GL_RGBA16UI:
1440 		case GL_RGBA16I:
1441 		case GL_RGBA32I:
1442 		case GL_RGBA32UI:
1443 		case GL_R11F_G11F_B10F:
1444 			return true;
1445 		case GL_R8_SNORM:
1446 		case GL_RG8_SNORM:
1447 		case GL_RGB8_SNORM:
1448 		case GL_RGBA8_SNORM:
1449 		case GL_ALPHA8_EXT:
1450 		case GL_LUMINANCE8_EXT:
1451 		case GL_LUMINANCE8_ALPHA8_EXT:
1452 		case GL_ALPHA32F_EXT:
1453 		case GL_LUMINANCE32F_EXT:
1454 		case GL_LUMINANCE_ALPHA32F_EXT:
1455 		case GL_ALPHA16F_EXT:
1456 		case GL_LUMINANCE16F_EXT:
1457 		case GL_LUMINANCE_ALPHA16F_EXT:
1458 		case GL_DEPTH_COMPONENT24:
1459 		case GL_DEPTH_COMPONENT32_OES:
1460 		case GL_DEPTH_COMPONENT32F:
1461 		case GL_DEPTH32F_STENCIL8:
1462 		case GL_DEPTH_COMPONENT16:
1463 		case GL_STENCIL_INDEX8:
1464 		case GL_DEPTH24_STENCIL8_OES:
1465 			return false;
1466 		default:
1467 			UNIMPLEMENTED();
1468 		}
1469 
1470 		return false;
1471 	}
1472 
IsDepthRenderable(GLint internalformat)1473 	bool IsDepthRenderable(GLint internalformat)
1474 	{
1475 		if(IsCompressed(internalformat))
1476 		{
1477 			return false;
1478 		}
1479 
1480 		switch(internalformat)
1481 		{
1482 		case GL_DEPTH_COMPONENT24:
1483 		case GL_DEPTH_COMPONENT16:
1484 		case GL_DEPTH24_STENCIL8_OES:    // GL_OES_packed_depth_stencil
1485 		case GL_DEPTH_COMPONENT32_OES:   // GL_OES_depth32
1486 		case GL_DEPTH32F_STENCIL8:
1487 		case GL_DEPTH_COMPONENT32F:
1488 			return true;
1489 		case GL_STENCIL_INDEX8:
1490 		case GL_R8:
1491 		case GL_R8UI:
1492 		case GL_R8I:
1493 		case GL_R16UI:
1494 		case GL_R16I:
1495 		case GL_R32UI:
1496 		case GL_R32I:
1497 		case GL_RG8:
1498 		case GL_RG8UI:
1499 		case GL_RG8I:
1500 		case GL_RG16UI:
1501 		case GL_RG16I:
1502 		case GL_RG32UI:
1503 		case GL_RG32I:
1504 		case GL_SRGB8_ALPHA8:
1505 		case GL_RGB10_A2:
1506 		case GL_RGBA8UI:
1507 		case GL_RGBA8I:
1508 		case GL_RGB10_A2UI:
1509 		case GL_RGBA16UI:
1510 		case GL_RGBA16I:
1511 		case GL_RGBA32I:
1512 		case GL_RGBA32UI:
1513 		case GL_RGBA4:
1514 		case GL_RGB5_A1:
1515 		case GL_RGB565:
1516 		case GL_RGB8:
1517 		case GL_RGBA8:
1518 		case GL_RED:
1519 		case GL_RG:
1520 		case GL_RGB:
1521 		case GL_RGBA:
1522 		case GL_R16F:
1523 		case GL_RG16F:
1524 		case GL_R11F_G11F_B10F:
1525 		case GL_RGB16F:
1526 		case GL_RGBA16F:
1527 		case GL_R32F:
1528 		case GL_RG32F:
1529 		case GL_RGB32F:
1530 		case GL_RGBA32F:
1531 		case GL_R8_SNORM:
1532 		case GL_RG8_SNORM:
1533 		case GL_RGB8_SNORM:
1534 		case GL_RGBA8_SNORM:
1535 			return false;
1536 		default:
1537 			UNIMPLEMENTED();
1538 		}
1539 
1540 		return false;
1541 	}
1542 
IsStencilRenderable(GLint internalformat)1543 	bool IsStencilRenderable(GLint internalformat)
1544 	{
1545 		if(IsCompressed(internalformat))
1546 		{
1547 			return false;
1548 		}
1549 
1550 		switch(internalformat)
1551 		{
1552 		case GL_STENCIL_INDEX8:
1553 		case GL_DEPTH24_STENCIL8_OES:
1554 		case GL_DEPTH32F_STENCIL8:
1555 			return true;
1556 		case GL_R8:
1557 		case GL_R8UI:
1558 		case GL_R8I:
1559 		case GL_R16UI:
1560 		case GL_R16I:
1561 		case GL_R32UI:
1562 		case GL_R32I:
1563 		case GL_RG8:
1564 		case GL_RG8UI:
1565 		case GL_RG8I:
1566 		case GL_RG16UI:
1567 		case GL_RG16I:
1568 		case GL_RG32UI:
1569 		case GL_RG32I:
1570 		case GL_SRGB8_ALPHA8:
1571 		case GL_RGB10_A2:
1572 		case GL_RGBA8UI:
1573 		case GL_RGBA8I:
1574 		case GL_RGB10_A2UI:
1575 		case GL_RGBA16UI:
1576 		case GL_RGBA16I:
1577 		case GL_RGBA32I:
1578 		case GL_RGBA32UI:
1579 		case GL_RGBA4:
1580 		case GL_RGB5_A1:
1581 		case GL_RGB565:
1582 		case GL_RGB8:
1583 		case GL_RGBA8:
1584 		case GL_RED:
1585 		case GL_RG:
1586 		case GL_RGB:
1587 		case GL_RGBA:
1588 		case GL_R16F:
1589 		case GL_RG16F:
1590 		case GL_R11F_G11F_B10F:
1591 		case GL_RGB16F:
1592 		case GL_RGBA16F:
1593 		case GL_R32F:
1594 		case GL_RG32F:
1595 		case GL_RGB32F:
1596 		case GL_RGBA32F:
1597 		case GL_DEPTH_COMPONENT16:
1598 		case GL_DEPTH_COMPONENT24:
1599 		case GL_DEPTH_COMPONENT32_OES:
1600 		case GL_DEPTH_COMPONENT32F:
1601 		case GL_R8_SNORM:
1602 		case GL_RG8_SNORM:
1603 		case GL_RGB8_SNORM:
1604 		case GL_RGBA8_SNORM:
1605 			return false;
1606 		default:
1607 			UNIMPLEMENTED();
1608 		}
1609 
1610 		return false;
1611 	}
1612 
IsMipmappable(GLint internalformat)1613 	bool IsMipmappable(GLint internalformat)
1614 	{
1615 		if(internalformat == GL_NONE)
1616 		{
1617 			return true;   // Image unspecified. Not an error.
1618 		}
1619 
1620 		if(IsNonNormalizedInteger(internalformat))
1621 		{
1622 			return false;
1623 		}
1624 
1625 		switch(internalformat)
1626 		{
1627 		case GL_ALPHA8_EXT:
1628 		case GL_LUMINANCE8_EXT:
1629 		case GL_LUMINANCE8_ALPHA8_EXT:
1630 		case GL_ALPHA32F_EXT:
1631 		case GL_LUMINANCE32F_EXT:
1632 		case GL_LUMINANCE_ALPHA32F_EXT:
1633 		case GL_ALPHA16F_EXT:
1634 		case GL_LUMINANCE16F_EXT:
1635 		case GL_LUMINANCE_ALPHA16F_EXT:
1636 			return true;
1637 		default:
1638 			return IsColorRenderable(internalformat);
1639 		}
1640 	}
1641 
GetAlphaSize(GLint internalformat)1642 	GLuint GetAlphaSize(GLint internalformat)
1643 	{
1644 		switch(internalformat)
1645 		{
1646 		case GL_NONE:           return 0;
1647 		case GL_RGBA4:          return 4;
1648 		case GL_RGB5_A1:        return 1;
1649 		case GL_RGB565:         return 0;
1650 		case GL_R8:             return 0;
1651 		case GL_RG8:            return 0;
1652 		case GL_RGB8:           return 0;
1653 		case GL_RGBA8:          return 8;
1654 		case GL_R16F:           return 0;
1655 		case GL_RG16F:          return 0;
1656 		case GL_RGB16F:         return 0;
1657 		case GL_RGBA16F:        return 16;
1658 		case GL_R32F:           return 0;
1659 		case GL_RG32F:          return 0;
1660 		case GL_RGB32F:         return 0;
1661 		case GL_RGBA32F:        return 32;
1662 		case GL_BGRA8_EXT:      return 8;
1663 		case GL_R8UI:           return 0;
1664 		case GL_R8I:            return 0;
1665 		case GL_R16UI:          return 0;
1666 		case GL_R16I:           return 0;
1667 		case GL_R32UI:          return 0;
1668 		case GL_R32I:           return 0;
1669 		case GL_RG8UI:          return 0;
1670 		case GL_RG8I:           return 0;
1671 		case GL_RG16UI:         return 0;
1672 		case GL_RG16I:          return 0;
1673 		case GL_RG32UI:         return 0;
1674 		case GL_RG32I:          return 0;
1675 		case GL_SRGB8_ALPHA8:   return 8;
1676 		case GL_RGB10_A2:       return 2;
1677 		case GL_RGBA8UI:        return 8;
1678 		case GL_RGBA8I:         return 8;
1679 		case GL_RGB10_A2UI:     return 2;
1680 		case GL_RGBA16UI:       return 16;
1681 		case GL_RGBA16I:        return 16;
1682 		case GL_RGBA32I:        return 32;
1683 		case GL_RGBA32UI:       return 32;
1684 		case GL_R11F_G11F_B10F: return 0;
1685 		default:
1686 		//	UNREACHABLE(internalformat);
1687 			return 0;
1688 		}
1689 	}
1690 
GetRedSize(GLint internalformat)1691 	GLuint GetRedSize(GLint internalformat)
1692 	{
1693 		switch(internalformat)
1694 		{
1695 		case GL_NONE:           return 0;
1696 		case GL_RGBA4:          return 4;
1697 		case GL_RGB5_A1:        return 5;
1698 		case GL_RGB565:         return 5;
1699 		case GL_R8:             return 8;
1700 		case GL_RG8:            return 8;
1701 		case GL_RGB8:           return 8;
1702 		case GL_RGBA8:          return 8;
1703 		case GL_R16F:           return 16;
1704 		case GL_RG16F:          return 16;
1705 		case GL_RGB16F:         return 16;
1706 		case GL_RGBA16F:        return 16;
1707 		case GL_R32F:           return 32;
1708 		case GL_RG32F:          return 32;
1709 		case GL_RGB32F:         return 32;
1710 		case GL_RGBA32F:        return 32;
1711 		case GL_BGRA8_EXT:      return 8;
1712 		case GL_R8UI:           return 8;
1713 		case GL_R8I:            return 8;
1714 		case GL_R16UI:          return 16;
1715 		case GL_R16I:           return 16;
1716 		case GL_R32UI:          return 32;
1717 		case GL_R32I:           return 32;
1718 		case GL_RG8UI:          return 8;
1719 		case GL_RG8I:           return 8;
1720 		case GL_RG16UI:         return 16;
1721 		case GL_RG16I:          return 16;
1722 		case GL_RG32UI:         return 32;
1723 		case GL_RG32I:          return 32;
1724 		case GL_SRGB8_ALPHA8:   return 8;
1725 		case GL_RGB10_A2:       return 10;
1726 		case GL_RGBA8UI:        return 8;
1727 		case GL_RGBA8I:         return 8;
1728 		case GL_RGB10_A2UI:     return 10;
1729 		case GL_RGBA16UI:       return 16;
1730 		case GL_RGBA16I:        return 16;
1731 		case GL_RGBA32I:        return 32;
1732 		case GL_RGBA32UI:       return 32;
1733 		case GL_R11F_G11F_B10F: return 11;
1734 		default:
1735 		//	UNREACHABLE(internalformat);
1736 			return 0;
1737 		}
1738 	}
1739 
GetGreenSize(GLint internalformat)1740 	GLuint GetGreenSize(GLint internalformat)
1741 	{
1742 		switch(internalformat)
1743 		{
1744 		case GL_NONE:           return 0;
1745 		case GL_RGBA4:          return 4;
1746 		case GL_RGB5_A1:        return 5;
1747 		case GL_RGB565:         return 6;
1748 		case GL_R8:             return 0;
1749 		case GL_RG8:            return 8;
1750 		case GL_RGB8:           return 8;
1751 		case GL_RGBA8:          return 8;
1752 		case GL_R16F:           return 0;
1753 		case GL_RG16F:          return 16;
1754 		case GL_RGB16F:         return 16;
1755 		case GL_RGBA16F:        return 16;
1756 		case GL_R32F:           return 0;
1757 		case GL_RG32F:          return 32;
1758 		case GL_RGB32F:         return 32;
1759 		case GL_RGBA32F:        return 32;
1760 		case GL_BGRA8_EXT:      return 8;
1761 		case GL_R8UI:           return 0;
1762 		case GL_R8I:            return 0;
1763 		case GL_R16UI:          return 0;
1764 		case GL_R16I:           return 0;
1765 		case GL_R32UI:          return 0;
1766 		case GL_R32I:           return 0;
1767 		case GL_RG8UI:          return 8;
1768 		case GL_RG8I:           return 8;
1769 		case GL_RG16UI:         return 16;
1770 		case GL_RG16I:          return 16;
1771 		case GL_RG32UI:         return 32;
1772 		case GL_RG32I:          return 32;
1773 		case GL_SRGB8_ALPHA8:   return 8;
1774 		case GL_RGB10_A2:       return 10;
1775 		case GL_RGBA8UI:        return 8;
1776 		case GL_RGBA8I:         return 8;
1777 		case GL_RGB10_A2UI:     return 10;
1778 		case GL_RGBA16UI:       return 16;
1779 		case GL_RGBA16I:        return 16;
1780 		case GL_RGBA32I:        return 32;
1781 		case GL_RGBA32UI:       return 32;
1782 		case GL_R11F_G11F_B10F: return 11;
1783 		default:
1784 		//	UNREACHABLE(internalformat);
1785 			return 0;
1786 		}
1787 	}
1788 
GetBlueSize(GLint internalformat)1789 	GLuint GetBlueSize(GLint internalformat)
1790 	{
1791 		switch(internalformat)
1792 		{
1793 		case GL_NONE:           return 0;
1794 		case GL_RGBA4:          return 4;
1795 		case GL_RGB5_A1:        return 5;
1796 		case GL_RGB565:         return 5;
1797 		case GL_R8:             return 0;
1798 		case GL_RG8:            return 0;
1799 		case GL_RGB8:           return 8;
1800 		case GL_RGBA8:          return 8;
1801 		case GL_R16F:           return 0;
1802 		case GL_RG16F:          return 0;
1803 		case GL_RGB16F:         return 16;
1804 		case GL_RGBA16F:        return 16;
1805 		case GL_R32F:           return 0;
1806 		case GL_RG32F:          return 0;
1807 		case GL_RGB32F:         return 32;
1808 		case GL_RGBA32F:        return 32;
1809 		case GL_BGRA8_EXT:      return 8;
1810 		case GL_R8UI:           return 0;
1811 		case GL_R8I:            return 0;
1812 		case GL_R16UI:          return 0;
1813 		case GL_R16I:           return 0;
1814 		case GL_R32UI:          return 0;
1815 		case GL_R32I:           return 0;
1816 		case GL_RG8UI:          return 0;
1817 		case GL_RG8I:           return 0;
1818 		case GL_RG16UI:         return 0;
1819 		case GL_RG16I:          return 0;
1820 		case GL_RG32UI:         return 0;
1821 		case GL_RG32I:          return 0;
1822 		case GL_SRGB8_ALPHA8:   return 8;
1823 		case GL_RGB10_A2:       return 10;
1824 		case GL_RGBA8UI:        return 8;
1825 		case GL_RGBA8I:         return 8;
1826 		case GL_RGB10_A2UI:     return 10;
1827 		case GL_RGBA16UI:       return 16;
1828 		case GL_RGBA16I:        return 16;
1829 		case GL_RGBA32I:        return 32;
1830 		case GL_RGBA32UI:       return 32;
1831 		case GL_R11F_G11F_B10F: return 10;
1832 		default:
1833 		//	UNREACHABLE(internalformat);
1834 			return 0;
1835 		}
1836 	}
1837 
GetDepthSize(GLint internalformat)1838 	GLuint GetDepthSize(GLint internalformat)
1839 	{
1840 		switch(internalformat)
1841 		{
1842 		case GL_STENCIL_INDEX8:        return 0;
1843 		case GL_DEPTH_COMPONENT16:     return 16;
1844 		case GL_DEPTH_COMPONENT24:     return 24;
1845 		case GL_DEPTH_COMPONENT32_OES: return 32;
1846 		case GL_DEPTH_COMPONENT32F:    return 32;
1847 		case GL_DEPTH24_STENCIL8:      return 24;
1848 		case GL_DEPTH32F_STENCIL8:     return 32;
1849 		default:
1850 		//	UNREACHABLE(internalformat);
1851 			return 0;
1852 		}
1853 	}
1854 
GetStencilSize(GLint internalformat)1855 	GLuint GetStencilSize(GLint internalformat)
1856 	{
1857 		switch(internalformat)
1858 		{
1859 		case GL_STENCIL_INDEX8:        return 8;
1860 		case GL_DEPTH_COMPONENT16:     return 0;
1861 		case GL_DEPTH_COMPONENT24:     return 0;
1862 		case GL_DEPTH_COMPONENT32_OES: return 0;
1863 		case GL_DEPTH_COMPONENT32F:    return 0;
1864 		case GL_DEPTH24_STENCIL8:      return 8;
1865 		case GL_DEPTH32F_STENCIL8:     return 8;
1866 		default:
1867 		//	UNREACHABLE(internalformat);
1868 			return 0;
1869 		}
1870 	}
1871 
GetColorComponentType(GLint internalformat)1872 	GLenum GetColorComponentType(GLint internalformat)
1873 	{
1874 		switch(internalformat)
1875 		{
1876 		case GL_ALPHA8_EXT:
1877 		case GL_LUMINANCE8_ALPHA8_EXT:
1878 		case GL_LUMINANCE8_EXT:
1879 		case GL_R8:
1880 		case GL_RG8:
1881 		case GL_SRGB8_ALPHA8:
1882 		case GL_RGB10_A2:
1883 		case GL_RGBA4:
1884 		case GL_RGB5_A1:
1885 		case GL_RGB565:
1886 		case GL_RGB8:
1887 		case GL_RGBA8:
1888 		case GL_SRGB8:
1889 		case GL_BGRA8_EXT:
1890 			return GL_UNSIGNED_NORMALIZED;
1891 		case GL_R8_SNORM:
1892 		case GL_RG8_SNORM:
1893 		case GL_RGB8_SNORM:
1894 		case GL_RGBA8_SNORM:
1895 			return GL_SIGNED_NORMALIZED;
1896 		case GL_R8UI:
1897 		case GL_R16UI:
1898 		case GL_R32UI:
1899 		case GL_RG8UI:
1900 		case GL_RG16UI:
1901 		case GL_RG32UI:
1902 		case GL_RGB8UI:
1903 		case GL_RGB16UI:
1904 		case GL_RGB32UI:
1905 		case GL_RGB10_A2UI:
1906 		case GL_RGBA16UI:
1907 		case GL_RGBA32UI:
1908 		case GL_RGBA8UI:
1909 			return GL_UNSIGNED_INT;
1910 		case GL_R8I:
1911 		case GL_R16I:
1912 		case GL_R32I:
1913 		case GL_RG8I:
1914 		case GL_RG16I:
1915 		case GL_RG32I:
1916 		case GL_RGB8I:
1917 		case GL_RGB16I:
1918 		case GL_RGB32I:
1919 		case GL_RGBA8I:
1920 		case GL_RGBA16I:
1921 		case GL_RGBA32I:
1922 			return GL_INT;
1923 		case GL_ALPHA32F_EXT:
1924 		case GL_LUMINANCE32F_EXT:
1925 		case GL_LUMINANCE_ALPHA32F_EXT:
1926 		case GL_ALPHA16F_EXT:
1927 		case GL_LUMINANCE16F_EXT:
1928 		case GL_LUMINANCE_ALPHA16F_EXT:
1929 		case GL_R16F:
1930 		case GL_RG16F:
1931 		case GL_R11F_G11F_B10F:
1932 		case GL_RGB16F:
1933 		case GL_RGBA16F:
1934 		case GL_R32F:
1935 		case GL_RG32F:
1936 		case GL_RGB32F:
1937 		case GL_RGBA32F:
1938 		case GL_RGB9_E5:
1939 			return GL_FLOAT;
1940 		default:
1941 		//	UNREACHABLE(internalformat);
1942 			return GL_NONE;
1943 		}
1944 	}
1945 
GetComponentType(GLint internalformat,GLenum attachment)1946 	GLenum GetComponentType(GLint internalformat, GLenum attachment)
1947 	{
1948 		// Can be one of GL_FLOAT, GL_INT, GL_UNSIGNED_INT, GL_SIGNED_NORMALIZED, or GL_UNSIGNED_NORMALIZED
1949 		switch(attachment)
1950 		{
1951 		case GL_COLOR_ATTACHMENT0:
1952 		case GL_COLOR_ATTACHMENT1:
1953 		case GL_COLOR_ATTACHMENT2:
1954 		case GL_COLOR_ATTACHMENT3:
1955 		case GL_COLOR_ATTACHMENT4:
1956 		case GL_COLOR_ATTACHMENT5:
1957 		case GL_COLOR_ATTACHMENT6:
1958 		case GL_COLOR_ATTACHMENT7:
1959 		case GL_COLOR_ATTACHMENT8:
1960 		case GL_COLOR_ATTACHMENT9:
1961 		case GL_COLOR_ATTACHMENT10:
1962 		case GL_COLOR_ATTACHMENT11:
1963 		case GL_COLOR_ATTACHMENT12:
1964 		case GL_COLOR_ATTACHMENT13:
1965 		case GL_COLOR_ATTACHMENT14:
1966 		case GL_COLOR_ATTACHMENT15:
1967 		case GL_COLOR_ATTACHMENT16:
1968 		case GL_COLOR_ATTACHMENT17:
1969 		case GL_COLOR_ATTACHMENT18:
1970 		case GL_COLOR_ATTACHMENT19:
1971 		case GL_COLOR_ATTACHMENT20:
1972 		case GL_COLOR_ATTACHMENT21:
1973 		case GL_COLOR_ATTACHMENT22:
1974 		case GL_COLOR_ATTACHMENT23:
1975 		case GL_COLOR_ATTACHMENT24:
1976 		case GL_COLOR_ATTACHMENT25:
1977 		case GL_COLOR_ATTACHMENT26:
1978 		case GL_COLOR_ATTACHMENT27:
1979 		case GL_COLOR_ATTACHMENT28:
1980 		case GL_COLOR_ATTACHMENT29:
1981 		case GL_COLOR_ATTACHMENT30:
1982 		case GL_COLOR_ATTACHMENT31:
1983 			return GetColorComponentType(internalformat);
1984 		case GL_DEPTH_ATTACHMENT:
1985 		case GL_STENCIL_ATTACHMENT:
1986 			// Only color buffers may have integer components.
1987 			return GL_FLOAT;
1988 		default:
1989 			UNREACHABLE(attachment);
1990 			return GL_NONE;
1991 		}
1992 	}
1993 
IsNormalizedInteger(GLint internalformat)1994 	bool IsNormalizedInteger(GLint internalformat)
1995 	{
1996 		GLenum type = GetColorComponentType(internalformat);
1997 
1998 		return type == GL_UNSIGNED_NORMALIZED || type == GL_SIGNED_NORMALIZED;
1999 	}
2000 
IsNonNormalizedInteger(GLint internalformat)2001 	bool IsNonNormalizedInteger(GLint internalformat)
2002 	{
2003 		GLenum type = GetColorComponentType(internalformat);
2004 
2005 		return type == GL_UNSIGNED_INT || type == GL_INT;
2006 	}
2007 
IsFloatFormat(GLint internalformat)2008 	bool IsFloatFormat(GLint internalformat)
2009 	{
2010 		return GetColorComponentType(internalformat) == GL_FLOAT;
2011 	}
2012 
IsSignedNonNormalizedInteger(GLint internalformat)2013 	bool IsSignedNonNormalizedInteger(GLint internalformat)
2014 	{
2015 		return GetColorComponentType(internalformat) == GL_INT;
2016 	}
2017 
IsUnsignedNonNormalizedInteger(GLint internalformat)2018 	bool IsUnsignedNonNormalizedInteger(GLint internalformat)
2019 	{
2020 		return GetColorComponentType(internalformat) == GL_UNSIGNED_INT;
2021 	}
2022 
GetColorEncoding(GLint internalformat)2023 	GLenum GetColorEncoding(GLint internalformat)
2024 	{
2025 		switch(internalformat)
2026 		{
2027 		case GL_SRGB8:
2028 		case GL_SRGB8_ALPHA8:
2029 			return GL_SRGB;
2030 		default:
2031 			// [OpenGL ES 3.0.5] section 6.1.13 page 242:
2032 			// If attachment is not a color attachment, or no data storage or texture image
2033 			// has been specified for the attachment, params will contain the value LINEAR.
2034 			return GL_LINEAR;
2035 		}
2036 	}
2037 
ParseUniformName(const std::string & name,unsigned int * outSubscript)2038 	std::string ParseUniformName(const std::string &name, unsigned int *outSubscript)
2039 	{
2040 		// Strip any trailing array operator and retrieve the subscript
2041 		size_t open = name.find_last_of('[');
2042 		size_t close = name.find_last_of(']');
2043 		bool hasIndex = (open != std::string::npos) && (close == name.length() - 1);
2044 		if(!hasIndex)
2045 		{
2046 			if(outSubscript)
2047 			{
2048 				*outSubscript = GL_INVALID_INDEX;
2049 			}
2050 			return name;
2051 		}
2052 
2053 		if(outSubscript)
2054 		{
2055 			int index = atoi(name.substr(open + 1).c_str());
2056 			if(index >= 0)
2057 			{
2058 				*outSubscript = index;
2059 			}
2060 			else
2061 			{
2062 				*outSubscript = GL_INVALID_INDEX;
2063 			}
2064 		}
2065 
2066 		return name.substr(0, open);
2067 	}
2068 }
2069 
2070 namespace es2sw
2071 {
ConvertDepthComparison(GLenum comparison)2072 	sw::DepthCompareMode ConvertDepthComparison(GLenum comparison)
2073 	{
2074 		switch(comparison)
2075 		{
2076 		case GL_NEVER:    return sw::DEPTH_NEVER;
2077 		case GL_ALWAYS:   return sw::DEPTH_ALWAYS;
2078 		case GL_LESS:     return sw::DEPTH_LESS;
2079 		case GL_LEQUAL:   return sw::DEPTH_LESSEQUAL;
2080 		case GL_EQUAL:    return sw::DEPTH_EQUAL;
2081 		case GL_GREATER:  return sw::DEPTH_GREATER;
2082 		case GL_GEQUAL:   return sw::DEPTH_GREATEREQUAL;
2083 		case GL_NOTEQUAL: return sw::DEPTH_NOTEQUAL;
2084 		default: UNREACHABLE(comparison);
2085 		}
2086 
2087 		return sw::DEPTH_ALWAYS;
2088 	}
2089 
ConvertStencilComparison(GLenum comparison)2090 	sw::StencilCompareMode ConvertStencilComparison(GLenum comparison)
2091 	{
2092 		switch(comparison)
2093 		{
2094 		case GL_NEVER:    return sw::STENCIL_NEVER;
2095 		case GL_ALWAYS:   return sw::STENCIL_ALWAYS;
2096 		case GL_LESS:     return sw::STENCIL_LESS;
2097 		case GL_LEQUAL:   return sw::STENCIL_LESSEQUAL;
2098 		case GL_EQUAL:    return sw::STENCIL_EQUAL;
2099 		case GL_GREATER:  return sw::STENCIL_GREATER;
2100 		case GL_GEQUAL:   return sw::STENCIL_GREATEREQUAL;
2101 		case GL_NOTEQUAL: return sw::STENCIL_NOTEQUAL;
2102 		default: UNREACHABLE(comparison);
2103 		}
2104 
2105 		return sw::STENCIL_ALWAYS;
2106 	}
2107 
ConvertColor(es2::Color color)2108 	sw::Color<float> ConvertColor(es2::Color color)
2109 	{
2110 		return sw::Color<float>(color.red, color.green, color.blue, color.alpha);
2111 	}
2112 
ConvertBlendFunc(GLenum blend)2113 	sw::BlendFactor ConvertBlendFunc(GLenum blend)
2114 	{
2115 		switch(blend)
2116 		{
2117 		case GL_ZERO:                     return sw::BLEND_ZERO;
2118 		case GL_ONE:                      return sw::BLEND_ONE;
2119 		case GL_SRC_COLOR:                return sw::BLEND_SOURCE;
2120 		case GL_ONE_MINUS_SRC_COLOR:      return sw::BLEND_INVSOURCE;
2121 		case GL_DST_COLOR:                return sw::BLEND_DEST;
2122 		case GL_ONE_MINUS_DST_COLOR:      return sw::BLEND_INVDEST;
2123 		case GL_SRC_ALPHA:                return sw::BLEND_SOURCEALPHA;
2124 		case GL_ONE_MINUS_SRC_ALPHA:      return sw::BLEND_INVSOURCEALPHA;
2125 		case GL_DST_ALPHA:                return sw::BLEND_DESTALPHA;
2126 		case GL_ONE_MINUS_DST_ALPHA:      return sw::BLEND_INVDESTALPHA;
2127 		case GL_CONSTANT_COLOR:           return sw::BLEND_CONSTANT;
2128 		case GL_ONE_MINUS_CONSTANT_COLOR: return sw::BLEND_INVCONSTANT;
2129 		case GL_CONSTANT_ALPHA:           return sw::BLEND_CONSTANTALPHA;
2130 		case GL_ONE_MINUS_CONSTANT_ALPHA: return sw::BLEND_INVCONSTANTALPHA;
2131 		case GL_SRC_ALPHA_SATURATE:       return sw::BLEND_SRCALPHASAT;
2132 		default: UNREACHABLE(blend);
2133 		}
2134 
2135 		return sw::BLEND_ZERO;
2136 	}
2137 
ConvertBlendOp(GLenum blendOp)2138 	sw::BlendOperation ConvertBlendOp(GLenum blendOp)
2139 	{
2140 		switch(blendOp)
2141 		{
2142 		case GL_FUNC_ADD:              return sw::BLENDOP_ADD;
2143 		case GL_FUNC_SUBTRACT:         return sw::BLENDOP_SUB;
2144 		case GL_FUNC_REVERSE_SUBTRACT: return sw::BLENDOP_INVSUB;
2145 		case GL_MIN_EXT:               return sw::BLENDOP_MIN;
2146 		case GL_MAX_EXT:               return sw::BLENDOP_MAX;
2147 		default: UNREACHABLE(blendOp);
2148 		}
2149 
2150 		return sw::BLENDOP_ADD;
2151 	}
2152 
ConvertStencilOp(GLenum stencilOp)2153 	sw::StencilOperation ConvertStencilOp(GLenum stencilOp)
2154 	{
2155 		switch(stencilOp)
2156 		{
2157 		case GL_ZERO:      return sw::OPERATION_ZERO;
2158 		case GL_KEEP:      return sw::OPERATION_KEEP;
2159 		case GL_REPLACE:   return sw::OPERATION_REPLACE;
2160 		case GL_INCR:      return sw::OPERATION_INCRSAT;
2161 		case GL_DECR:      return sw::OPERATION_DECRSAT;
2162 		case GL_INVERT:    return sw::OPERATION_INVERT;
2163 		case GL_INCR_WRAP: return sw::OPERATION_INCR;
2164 		case GL_DECR_WRAP: return sw::OPERATION_DECR;
2165 		default: UNREACHABLE(stencilOp);
2166 		}
2167 
2168 		return sw::OPERATION_KEEP;
2169 	}
2170 
ConvertTextureWrap(GLenum wrap)2171 	sw::AddressingMode ConvertTextureWrap(GLenum wrap)
2172 	{
2173 		switch(wrap)
2174 		{
2175 		case GL_REPEAT:            return sw::ADDRESSING_WRAP;
2176 		case GL_CLAMP_TO_EDGE:     return sw::ADDRESSING_CLAMP;
2177 		case GL_MIRRORED_REPEAT:   return sw::ADDRESSING_MIRROR;
2178 		default: UNREACHABLE(wrap);
2179 		}
2180 
2181 		return sw::ADDRESSING_WRAP;
2182 	}
2183 
ConvertCompareFunc(GLenum compareFunc,GLenum compareMode)2184 	sw::CompareFunc ConvertCompareFunc(GLenum compareFunc, GLenum compareMode)
2185 	{
2186 		if(compareMode == GL_COMPARE_REF_TO_TEXTURE)
2187 		{
2188 			switch(compareFunc)
2189 			{
2190 			case GL_LEQUAL:   return sw::COMPARE_LESSEQUAL;
2191 			case GL_GEQUAL:   return sw::COMPARE_GREATEREQUAL;
2192 			case GL_LESS:     return sw::COMPARE_LESS;
2193 			case GL_GREATER:  return sw::COMPARE_GREATER;
2194 			case GL_EQUAL:    return sw::COMPARE_EQUAL;
2195 			case GL_NOTEQUAL: return sw::COMPARE_NOTEQUAL;
2196 			case GL_ALWAYS:   return sw::COMPARE_ALWAYS;
2197 			case GL_NEVER:    return sw::COMPARE_NEVER;
2198 			default: UNREACHABLE(compareFunc);
2199 			}
2200 		}
2201 		else if(compareMode == GL_NONE)
2202 		{
2203 			return sw::COMPARE_BYPASS;
2204 		}
2205 		else UNREACHABLE(compareMode);
2206 
2207 		return sw::COMPARE_BYPASS;
2208 	};
2209 
ConvertSwizzleType(GLenum swizzleType)2210 	sw::SwizzleType ConvertSwizzleType(GLenum swizzleType)
2211 	{
2212 		switch(swizzleType)
2213 		{
2214 		case GL_RED:   return sw::SWIZZLE_RED;
2215 		case GL_GREEN: return sw::SWIZZLE_GREEN;
2216 		case GL_BLUE:  return sw::SWIZZLE_BLUE;
2217 		case GL_ALPHA: return sw::SWIZZLE_ALPHA;
2218 		case GL_ZERO:  return sw::SWIZZLE_ZERO;
2219 		case GL_ONE:   return sw::SWIZZLE_ONE;
2220 		default: UNREACHABLE(swizzleType);
2221 		}
2222 
2223 		return sw::SWIZZLE_RED;
2224 	};
2225 
ConvertCullMode(GLenum cullFace,GLenum frontFace)2226 	sw::CullMode ConvertCullMode(GLenum cullFace, GLenum frontFace)
2227 	{
2228 		switch(cullFace)
2229 		{
2230 		case GL_FRONT:
2231 			return (frontFace == GL_CCW ? sw::CULL_CLOCKWISE : sw::CULL_COUNTERCLOCKWISE);
2232 		case GL_BACK:
2233 			return (frontFace == GL_CCW ? sw::CULL_COUNTERCLOCKWISE : sw::CULL_CLOCKWISE);
2234 		case GL_FRONT_AND_BACK:
2235 			return sw::CULL_NONE;   // culling will be handled during draw
2236 		default: UNREACHABLE(cullFace);
2237 		}
2238 
2239 		return sw::CULL_COUNTERCLOCKWISE;
2240 	}
2241 
ConvertColorMask(bool red,bool green,bool blue,bool alpha)2242 	unsigned int ConvertColorMask(bool red, bool green, bool blue, bool alpha)
2243 	{
2244 		return (red   ? 0x00000001 : 0) |
2245 			   (green ? 0x00000002 : 0) |
2246 			   (blue  ? 0x00000004 : 0) |
2247 			   (alpha ? 0x00000008 : 0);
2248 	}
2249 
ConvertMipMapFilter(GLenum minFilter)2250 	sw::MipmapType ConvertMipMapFilter(GLenum minFilter)
2251 	{
2252 		switch(minFilter)
2253 		{
2254 		case GL_NEAREST:
2255 		case GL_LINEAR:
2256 			return sw::MIPMAP_NONE;
2257 		case GL_NEAREST_MIPMAP_NEAREST:
2258 		case GL_LINEAR_MIPMAP_NEAREST:
2259 			return sw::MIPMAP_POINT;
2260 		case GL_NEAREST_MIPMAP_LINEAR:
2261 		case GL_LINEAR_MIPMAP_LINEAR:
2262 			return sw::MIPMAP_LINEAR;
2263 		default:
2264 			UNREACHABLE(minFilter);
2265 			return sw::MIPMAP_NONE;
2266 		}
2267 	}
2268 
ConvertTextureFilter(GLenum minFilter,GLenum magFilter,float maxAnisotropy)2269 	sw::FilterType ConvertTextureFilter(GLenum minFilter, GLenum magFilter, float maxAnisotropy)
2270 	{
2271 		if(maxAnisotropy > 1.0f)
2272 		{
2273 			return sw::FILTER_ANISOTROPIC;
2274 		}
2275 
2276 		switch(magFilter)
2277 		{
2278 		case GL_NEAREST:
2279 		case GL_LINEAR:
2280 			break;
2281 		default:
2282 			UNREACHABLE(magFilter);
2283 		}
2284 
2285 		switch(minFilter)
2286 		{
2287 		case GL_NEAREST:
2288 		case GL_NEAREST_MIPMAP_NEAREST:
2289 		case GL_NEAREST_MIPMAP_LINEAR:
2290 			return (magFilter == GL_NEAREST) ? sw::FILTER_POINT : sw::FILTER_MIN_POINT_MAG_LINEAR;
2291 		case GL_LINEAR:
2292 		case GL_LINEAR_MIPMAP_NEAREST:
2293 		case GL_LINEAR_MIPMAP_LINEAR:
2294 			return (magFilter == GL_NEAREST) ? sw::FILTER_MIN_LINEAR_MAG_POINT : sw::FILTER_LINEAR;
2295 		default:
2296 			UNREACHABLE(minFilter);
2297 			return sw::FILTER_POINT;
2298 		}
2299 	}
2300 
ConvertPrimitiveType(GLenum primitiveType,GLsizei elementCount,GLenum elementType,sw::DrawType & drawType,int & primitiveCount,int & verticesPerPrimitive)2301 	bool ConvertPrimitiveType(GLenum primitiveType, GLsizei elementCount, GLenum elementType, sw::DrawType &drawType, int &primitiveCount, int &verticesPerPrimitive)
2302 	{
2303 		switch(primitiveType)
2304 		{
2305 		case GL_POINTS:
2306 			drawType = sw::DRAW_POINTLIST;
2307 			primitiveCount = elementCount;
2308 			verticesPerPrimitive = 1;
2309 			break;
2310 		case GL_LINES:
2311 			drawType = sw::DRAW_LINELIST;
2312 			primitiveCount = elementCount / 2;
2313 			verticesPerPrimitive = 2;
2314 			break;
2315 		case GL_LINE_LOOP:
2316 			drawType = sw::DRAW_LINELOOP;
2317 			primitiveCount = elementCount;
2318 			verticesPerPrimitive = 2;
2319 			break;
2320 		case GL_LINE_STRIP:
2321 			drawType = sw::DRAW_LINESTRIP;
2322 			primitiveCount = elementCount - 1;
2323 			verticesPerPrimitive = 2;
2324 			break;
2325 		case GL_TRIANGLES:
2326 			drawType = sw::DRAW_TRIANGLELIST;
2327 			primitiveCount = elementCount / 3;
2328 			verticesPerPrimitive = 3;
2329 			break;
2330 		case GL_TRIANGLE_STRIP:
2331 			drawType = sw::DRAW_TRIANGLESTRIP;
2332 			primitiveCount = elementCount - 2;
2333 			verticesPerPrimitive = 3;
2334 			break;
2335 		case GL_TRIANGLE_FAN:
2336 			drawType = sw::DRAW_TRIANGLEFAN;
2337 			primitiveCount = elementCount - 2;
2338 			verticesPerPrimitive = 3;
2339 			break;
2340 		default:
2341 			return false;
2342 		}
2343 
2344 		sw::DrawType elementSize;
2345 		switch(elementType)
2346 		{
2347 		case GL_NONE:           elementSize = sw::DRAW_NONINDEXED; break;
2348 		case GL_UNSIGNED_BYTE:  elementSize = sw::DRAW_INDEXED8;   break;
2349 		case GL_UNSIGNED_SHORT: elementSize = sw::DRAW_INDEXED16;  break;
2350 		case GL_UNSIGNED_INT:   elementSize = sw::DRAW_INDEXED32;  break;
2351 		default: return false;
2352 		}
2353 
2354 		drawType = sw::DrawType(drawType | elementSize);
2355 
2356 		return true;
2357 	}
2358 }
2359 
2360 namespace sw2es
2361 {
ConvertBackBufferFormat(sw::Format format)2362 	GLenum ConvertBackBufferFormat(sw::Format format)
2363 	{
2364 		switch(format)
2365 		{
2366 		case sw::FORMAT_A4R4G4B4: return GL_RGBA4;
2367 		case sw::FORMAT_A8R8G8B8: return GL_RGBA8;
2368 		case sw::FORMAT_A8B8G8R8: return GL_RGBA8;
2369 		case sw::FORMAT_A1R5G5B5: return GL_RGB5_A1;
2370 		case sw::FORMAT_R5G6B5:   return GL_RGB565;
2371 		case sw::FORMAT_X8R8G8B8: return GL_RGB8;
2372 		case sw::FORMAT_X8B8G8R8: return GL_RGB8;
2373 		case sw::FORMAT_SRGB8_A8: return GL_RGBA8;
2374 		case sw::FORMAT_SRGB8_X8: return GL_RGB8;
2375 		default:
2376 			UNREACHABLE(format);
2377 		}
2378 
2379 		return GL_RGBA4;
2380 	}
2381 
ConvertDepthStencilFormat(sw::Format format)2382 	GLenum ConvertDepthStencilFormat(sw::Format format)
2383 	{
2384 		switch(format)
2385 		{
2386 		case sw::FORMAT_D16:    return GL_DEPTH_COMPONENT16;
2387 		case sw::FORMAT_D24X8:  return GL_DEPTH_COMPONENT24;
2388 		case sw::FORMAT_D32:    return GL_DEPTH_COMPONENT32_OES;
2389 		case sw::FORMAT_D24S8:  return GL_DEPTH24_STENCIL8_OES;
2390 		case sw::FORMAT_D32F:   return GL_DEPTH_COMPONENT32F;
2391 		case sw::FORMAT_D32FS8: return GL_DEPTH32F_STENCIL8;
2392 		case sw::FORMAT_S8:     return GL_STENCIL_INDEX8;
2393 		default:
2394 			UNREACHABLE(format);
2395 		}
2396 
2397 		return GL_DEPTH24_STENCIL8_OES;
2398 	}
2399 }