OSDN Git Service

Clean up GL header includes.
[android-x86/external-swiftshader.git] / src / OpenGL / common / Image.cpp
1 // SwiftShader Software Renderer
2 //
3 // Copyright(c) 2005-2013 TransGaming Inc.
4 //
5 // All rights reserved. No part of this software may be copied, distributed, transmitted,
6 // transcribed, stored in a retrieval system, translated into any human or computer
7 // language by any means, or disclosed to third parties without the explicit written
8 // agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9 // or implied, including but not limited to any patent rights, are granted to you.
10 //
11
12 #include "Image.hpp"
13
14 #include "Renderer/Blitter.hpp"
15 #include "../libEGL/Texture.hpp"
16 #include "../common/debug.h"
17 #include "Common/Math.hpp"
18 #include "Common/Thread.hpp"
19
20 #include <GLES2/gl2ext.h>
21 #include <GLES3/gl3.h>
22
23 #include <string.h>
24
25 namespace
26 {
27         int getNumBlocks(int w, int h, int blockSizeX, int blockSizeY)
28         {
29                 return ((w + blockSizeX - 1) / blockSizeX) * ((h + blockSizeY - 1) / blockSizeY);
30         }
31
32         enum DataType
33         {
34                 Bytes_1,
35                 Bytes_2,
36                 Bytes_4,
37                 Bytes_8,
38                 Bytes_16,
39                 ByteRGB,
40                 UByteRGB,
41                 ShortRGB,
42                 UShortRGB,
43                 IntRGB,
44                 UIntRGB,
45                 RGB565,
46                 FloatRGB,
47                 HalfFloatRGB,
48                 RGBA4444,
49                 RGBA5551,
50                 RGB10A2UI,
51                 R11G11B10F,
52                 RGB9E5,
53                 SRGB,
54                 SRGBA,
55                 D16,
56                 D24,
57                 D32,
58                 D32F,
59                 S8,
60                 S24_8,
61         };
62
63         template<DataType dataType>
64         void LoadImageRow(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
65         {
66                 UNIMPLEMENTED();
67         }
68
69         template<>
70         void LoadImageRow<Bytes_1>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
71         {
72                 memcpy(dest + xoffset, source, width);
73         }
74
75         template<>
76         void LoadImageRow<Bytes_2>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
77         {
78                 memcpy(dest + xoffset * 2, source, width * 2);
79         }
80
81         template<>
82         void LoadImageRow<Bytes_4>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
83         {
84                 memcpy(dest + xoffset * 4, source, width * 4);
85         }
86
87         template<>
88         void LoadImageRow<Bytes_8>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
89         {
90                 memcpy(dest + xoffset * 8, source, width * 8);
91         }
92
93         template<>
94         void LoadImageRow<Bytes_16>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
95         {
96                 memcpy(dest + xoffset * 16, source, width * 16);
97         }
98
99         template<>
100         void LoadImageRow<ByteRGB>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
101         {
102                 unsigned char *destB = dest + xoffset * 4;
103
104                 for(int x = 0; x < width; x++)
105                 {
106                         destB[4 * x + 0] = source[x * 3 + 0];
107                         destB[4 * x + 1] = source[x * 3 + 1];
108                         destB[4 * x + 2] = source[x * 3 + 2];
109                         destB[4 * x + 3] = 0x7F;
110                 }
111         }
112
113         template<>
114         void LoadImageRow<UByteRGB>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
115         {
116                 unsigned char *destB = dest + xoffset * 4;
117
118                 for(int x = 0; x < width; x++)
119                 {
120                         destB[4 * x + 0] = source[x * 3 + 0];
121                         destB[4 * x + 1] = source[x * 3 + 1];
122                         destB[4 * x + 2] = source[x * 3 + 2];
123                         destB[4 * x + 3] = 0xFF;
124                 }
125         }
126
127         template<>
128         void LoadImageRow<ShortRGB>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
129         {
130                 const unsigned short *sourceS = reinterpret_cast<const unsigned short*>(source);
131                 unsigned short *destS = reinterpret_cast<unsigned short*>(dest + xoffset * 8);
132
133                 for(int x = 0; x < width; x++)
134                 {
135                         destS[4 * x + 0] = sourceS[x * 3 + 0];
136                         destS[4 * x + 1] = sourceS[x * 3 + 1];
137                         destS[4 * x + 2] = sourceS[x * 3 + 2];
138                         destS[4 * x + 3] = 0x7FFF;
139                 }
140         }
141
142         template<>
143         void LoadImageRow<UShortRGB>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
144         {
145                 const unsigned short *sourceS = reinterpret_cast<const unsigned short*>(source);
146                 unsigned short *destS = reinterpret_cast<unsigned short*>(dest + xoffset * 8);
147
148                 for(int x = 0; x < width; x++)
149                 {
150                         destS[4 * x + 0] = sourceS[x * 3 + 0];
151                         destS[4 * x + 1] = sourceS[x * 3 + 1];
152                         destS[4 * x + 2] = sourceS[x * 3 + 2];
153                         destS[4 * x + 3] = 0xFFFF;
154                 }
155         }
156
157         template<>
158         void LoadImageRow<IntRGB>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
159         {
160                 const unsigned int *sourceI = reinterpret_cast<const unsigned int*>(source);
161                 unsigned int *destI = reinterpret_cast<unsigned int*>(dest + xoffset * 16);
162
163                 for(int x = 0; x < width; x++)
164                 {
165                         destI[4 * x + 0] = sourceI[x * 3 + 0];
166                         destI[4 * x + 1] = sourceI[x * 3 + 1];
167                         destI[4 * x + 2] = sourceI[x * 3 + 2];
168                         destI[4 * x + 3] = 0x7FFFFFFF;
169                 }
170         }
171
172         template<>
173         void LoadImageRow<UIntRGB>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
174         {
175                 const unsigned int *sourceI = reinterpret_cast<const unsigned int*>(source);
176                 unsigned int *destI = reinterpret_cast<unsigned int*>(dest + xoffset * 16);
177
178                 for(int x = 0; x < width; x++)
179                 {
180                         destI[4 * x + 0] = sourceI[x * 3 + 0];
181                         destI[4 * x + 1] = sourceI[x * 3 + 1];
182                         destI[4 * x + 2] = sourceI[x * 3 + 2];
183                         destI[4 * x + 3] = 0xFFFFFFFF;
184                 }
185         }
186
187         template<>
188         void LoadImageRow<RGB565>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
189         {
190                 memcpy(dest + xoffset * 2, source, width * 2);
191         }
192
193         template<>
194         void LoadImageRow<FloatRGB>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
195         {
196                 const float *sourceF = reinterpret_cast<const float*>(source);
197                 float *destF = reinterpret_cast<float*>(dest + xoffset * 16);
198
199                 for(int x = 0; x < width; x++)
200                 {
201                         destF[4 * x + 0] = sourceF[x * 3 + 0];
202                         destF[4 * x + 1] = sourceF[x * 3 + 1];
203                         destF[4 * x + 2] = sourceF[x * 3 + 2];
204                         destF[4 * x + 3] = 1.0f;
205                 }
206         }
207
208         template<>
209         void LoadImageRow<HalfFloatRGB>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
210         {
211                 const unsigned short *sourceH = reinterpret_cast<const unsigned short*>(source);
212                 unsigned short *destH = reinterpret_cast<unsigned short*>(dest + xoffset * 8);
213
214                 for(int x = 0; x < width; x++)
215                 {
216                         destH[4 * x + 0] = sourceH[x * 3 + 0];
217                         destH[4 * x + 1] = sourceH[x * 3 + 1];
218                         destH[4 * x + 2] = sourceH[x * 3 + 2];
219                         destH[4 * x + 3] = 0x3C00; // SEEEEEMMMMMMMMMM, S = 0, E = 15, M = 0: 16bit flpt representation of 1
220                 }
221         }
222
223         template<>
224         void LoadImageRow<RGBA4444>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
225         {
226                 const unsigned short *source4444 = reinterpret_cast<const unsigned short*>(source);
227                 unsigned char *dest4444 = dest + xoffset * 4;
228
229                 for(int x = 0; x < width; x++)
230                 {
231                         unsigned short rgba = source4444[x];
232                         dest4444[4 * x + 0] = ((rgba & 0x00F0) << 0) | ((rgba & 0x00F0) >> 4);
233                         dest4444[4 * x + 1] = ((rgba & 0x0F00) >> 4) | ((rgba & 0x0F00) >> 8);
234                         dest4444[4 * x + 2] = ((rgba & 0xF000) >> 8) | ((rgba & 0xF000) >> 12);
235                         dest4444[4 * x + 3] = ((rgba & 0x000F) << 4) | ((rgba & 0x000F) >> 0);
236                 }
237         }
238
239         template<>
240         void LoadImageRow<RGBA5551>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
241         {
242                 const unsigned short *source5551 = reinterpret_cast<const unsigned short*>(source);
243                 unsigned char *dest5551 = dest + xoffset * 4;
244
245                 for(int x = 0; x < width; x++)
246                 {
247                         unsigned short rgba = source5551[x];
248                         dest5551[4 * x + 0] = ((rgba & 0x003E) << 2) | ((rgba & 0x003E) >> 3);
249                         dest5551[4 * x + 1] = ((rgba & 0x07C0) >> 3) | ((rgba & 0x07C0) >> 8);
250                         dest5551[4 * x + 2] = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13);
251                         dest5551[4 * x + 3] = (rgba & 0x0001) ? 0xFF : 0;
252                 }
253         }
254
255         template<>
256         void LoadImageRow<RGB10A2UI>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
257         {
258                 const unsigned int *source1010102U = reinterpret_cast<const unsigned int*>(source);
259                 unsigned short *dest16U = reinterpret_cast<unsigned short*>(dest + xoffset * 8);
260
261                 for(int x = 0; x < width; x++)
262                 {
263                         unsigned int rgba = source1010102U[x];
264                         dest16U[4 * x + 0] = (rgba & 0x00000FFC) >> 2;
265                         dest16U[4 * x + 1] = (rgba & 0x003FF000) >> 12;
266                         dest16U[4 * x + 2] = (rgba & 0xFFC00000) >> 22;
267                         dest16U[4 * x + 3] = (rgba & 0x00000003);
268                 }
269         }
270
271         template<>
272         void LoadImageRow<R11G11B10F>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
273         {
274                 const sw::R11G11B10FData *sourceRGB = reinterpret_cast<const sw::R11G11B10FData*>(source);
275                 float *destF = reinterpret_cast<float*>(dest + xoffset * 16);
276
277                 for(int x = 0; x < width; x++, sourceRGB++, destF+=4)
278                 {
279                         sourceRGB->toRGBFloats(destF);
280                         destF[3] = 1.0f;
281                 }
282         }
283
284         template<>
285         void LoadImageRow<RGB9E5>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
286         {
287                 const sw::RGB9E5Data *sourceRGB = reinterpret_cast<const sw::RGB9E5Data*>(source);
288                 float *destF = reinterpret_cast<float*>(dest + xoffset * 16);
289
290                 for(int x = 0; x < width; x++, sourceRGB++, destF += 4)
291                 {
292                         sourceRGB->toRGBFloats(destF);
293                         destF[3] = 1.0f;
294                 }
295         }
296
297         template<>
298         void LoadImageRow<SRGB>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
299         {
300                 dest += xoffset * 4;
301
302                 for(int x = 0; x < width; x++)
303                 {
304                         for(int rgb = 0; rgb < 3; ++rgb)
305                         {
306                                 *dest++ = sw::sRGB8toLinear8(*source++);
307                         }
308                         *dest++ = 255;
309                 }
310         }
311
312         template<>
313         void LoadImageRow<SRGBA>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
314         {
315                 dest += xoffset * 4;
316
317                 for(int x = 0; x < width; x++)
318                 {
319                         for(int rgb = 0; rgb < 3; ++rgb)
320                         {
321                                 *dest++ = sw::sRGB8toLinear8(*source++);
322                         }
323                         *dest++ = *source++;
324                 }
325         }
326
327         template<>
328         void LoadImageRow<D16>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
329         {
330                 const unsigned short *sourceD16 = reinterpret_cast<const unsigned short*>(source);
331                 float *destF = reinterpret_cast<float*>(dest + xoffset * 4);
332
333                 for(int x = 0; x < width; x++)
334                 {
335                         destF[x] = (float)sourceD16[x] / 0xFFFF;
336                 }
337         }
338
339         template<>
340         void LoadImageRow<D24>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
341         {
342                 const unsigned int *sourceD24 = reinterpret_cast<const unsigned int*>(source);
343                 float *destF = reinterpret_cast<float*>(dest + xoffset * 4);
344
345                 for(int x = 0; x < width; x++)
346                 {
347                         destF[x] = (float)(sourceD24[x] & 0xFFFFFF00) / 0xFFFFFF00;
348                 }
349         }
350
351         template<>
352         void LoadImageRow<D32>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
353         {
354                 const unsigned int *sourceD32 = reinterpret_cast<const unsigned int*>(source);
355                 float *destF = reinterpret_cast<float*>(dest + xoffset * 4);
356
357                 for(int x = 0; x < width; x++)
358                 {
359                         destF[x] = (float)sourceD32[x] / 0xFFFFFFFF;
360                 }
361         }
362
363         template<>
364         void LoadImageRow<S8>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
365         {
366                 const unsigned int *sourceI = reinterpret_cast<const unsigned int*>(source);
367                 unsigned char *destI = dest + xoffset;
368
369                 for(int x = 0; x < width; x++)
370                 {
371                         destI[x] = static_cast<unsigned char>(sourceI[x] & 0x000000FF);   // FIXME: Quad layout
372                 }
373         }
374
375         template<>
376         void LoadImageRow<D32F>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
377         {
378                 struct D32FS8 { float depth32f; unsigned int stencil24_8; };
379                 const D32FS8 *sourceD32FS8 = reinterpret_cast<const D32FS8*>(source);
380                 float *destF = reinterpret_cast<float*>(dest + xoffset * 4);
381
382                 for(int x = 0; x < width; x++)
383                 {
384                         destF[x] = sourceD32FS8[x].depth32f;
385                 }
386         }
387
388         template<>
389         void LoadImageRow<S24_8>(const unsigned char *source, unsigned char *dest, GLint xoffset, GLsizei width)
390         {
391                 struct D32FS8 { float depth32f; unsigned int stencil24_8; };
392                 const D32FS8 *sourceD32FS8 = reinterpret_cast<const D32FS8*>(source);
393                 unsigned char *destI = dest + xoffset;
394
395                 for(int x = 0; x < width; x++)
396                 {
397                         destI[x] = static_cast<unsigned char>(sourceD32FS8[x].stencil24_8 & 0x000000FF);   // FIXME: Quad layout
398                 }
399         }
400
401         template<DataType dataType>
402         void LoadImageData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, int inputPitch, int inputHeight, int destPitch, GLsizei destHeight, const void *input, void *buffer)
403         {
404                 for(int z = 0; z < depth; ++z)
405                 {
406                         const unsigned char *inputStart = static_cast<const unsigned char*>(input) + (z * inputPitch * inputHeight);
407                         unsigned char *destStart = static_cast<unsigned char*>(buffer) + ((zoffset + z) * destPitch * destHeight);
408                         for(int y = 0; y < height; ++y)
409                         {
410                                 const unsigned char *source = inputStart + y * inputPitch;
411                                 unsigned char *dest = destStart + (y + yoffset) * destPitch;
412
413                                 LoadImageRow<dataType>(source, dest, xoffset, width);
414                         }
415                 }
416         }
417 }
418
419 namespace egl
420 {
421         sw::Format ConvertFormatType(GLenum format, GLenum type)
422         {
423                 switch(format)
424                 {
425                 case GL_LUMINANCE:
426                         switch(type)
427                         {
428                         case GL_UNSIGNED_BYTE:  return sw::FORMAT_L8;
429                         case GL_HALF_FLOAT:     return sw::FORMAT_L16F;
430                         case GL_HALF_FLOAT_OES: return sw::FORMAT_L16F;
431                         case GL_FLOAT:          return sw::FORMAT_L32F;
432                         default: UNREACHABLE(type);
433                         }
434                         break;
435                 case GL_LUMINANCE8_EXT:
436                         return sw::FORMAT_L8;
437                 case GL_LUMINANCE16F_EXT:
438                         return sw::FORMAT_L16F;
439                 case GL_LUMINANCE32F_EXT:
440                         return sw::FORMAT_L32F;
441                 case GL_LUMINANCE_ALPHA:
442                         switch(type)
443                         {
444                         case GL_UNSIGNED_BYTE:  return sw::FORMAT_A8L8;
445                         case GL_HALF_FLOAT:     return sw::FORMAT_A16L16F;
446                         case GL_HALF_FLOAT_OES: return sw::FORMAT_A16L16F;
447                         case GL_FLOAT:          return sw::FORMAT_A32L32F;
448                         default: UNREACHABLE(type);
449                         }
450                         break;
451                 case GL_LUMINANCE8_ALPHA8_EXT:
452                         return sw::FORMAT_A8L8;
453                 case GL_LUMINANCE_ALPHA16F_EXT:
454                         return sw::FORMAT_A16L16F;
455                 case GL_LUMINANCE_ALPHA32F_EXT:
456                         return sw::FORMAT_A32L32F;
457                 case GL_RGBA:
458                         switch(type)
459                         {
460                         case GL_UNSIGNED_BYTE:          return sw::FORMAT_A8B8G8R8;
461                         case GL_UNSIGNED_SHORT_4_4_4_4: return sw::FORMAT_R4G4B4A4;
462                         case GL_UNSIGNED_SHORT_5_5_5_1: return sw::FORMAT_R5G5B5A1;
463                         case GL_HALF_FLOAT:             return sw::FORMAT_A16B16G16R16F;
464                         case GL_HALF_FLOAT_OES:         return sw::FORMAT_A16B16G16R16F;
465                         case GL_FLOAT:                  return sw::FORMAT_A32B32G32R32F;
466                         default: UNREACHABLE(type);
467                         }
468                         break;
469                 case GL_BGRA_EXT:
470                 case GL_BGRA8_EXT:
471                         switch(type)
472                         {
473                         case GL_UNSIGNED_BYTE:          return sw::FORMAT_A8R8G8B8;
474                         default: UNREACHABLE(type);
475                         }
476                         break;
477                 case GL_RGB:
478                         switch(type)
479                         {
480                         case GL_UNSIGNED_BYTE:          return sw::FORMAT_B8G8R8;
481                         case GL_UNSIGNED_SHORT_5_6_5:   return sw::FORMAT_R5G6B5;
482                         case GL_HALF_FLOAT:             return sw::FORMAT_B16G16R16F;
483                         case GL_HALF_FLOAT_OES:         return sw::FORMAT_B16G16R16F;
484                         case GL_FLOAT:                  return sw::FORMAT_B32G32R32F;
485                         default: UNREACHABLE(type);
486                         }
487                         break;
488                 case GL_ALPHA:
489                         switch(type)
490                         {
491                         case GL_UNSIGNED_BYTE:          return sw::FORMAT_A8;
492                         case GL_HALF_FLOAT:             return sw::FORMAT_A16F;
493                         case GL_HALF_FLOAT_OES:         return sw::FORMAT_A16F;
494                         case GL_FLOAT:                  return sw::FORMAT_A32F;
495                         default: UNREACHABLE(type);
496                         }
497                         break;
498                 case GL_ALPHA8_EXT:
499                         return sw::FORMAT_A8;
500                 case GL_ALPHA16F_EXT:
501                         return sw::FORMAT_A16F;
502                 case GL_ALPHA32F_EXT:
503                         return sw::FORMAT_A32F;
504                 default:
505                         UNREACHABLE(format);
506                 }
507
508                 return sw::FORMAT_NULL;
509         }
510
511         sw::Format SelectInternalFormat(GLenum format, GLenum type)
512         {
513                 switch(format)
514                 {
515                 case GL_ETC1_RGB8_OES:
516                         return sw::FORMAT_ETC1;
517                 case GL_COMPRESSED_R11_EAC:
518                         return sw::FORMAT_R11_EAC;
519                 case GL_COMPRESSED_SIGNED_R11_EAC:
520                         return sw::FORMAT_SIGNED_R11_EAC;
521                 case GL_COMPRESSED_RG11_EAC:
522                         return sw::FORMAT_RG11_EAC;
523                 case GL_COMPRESSED_SIGNED_RG11_EAC:
524                         return sw::FORMAT_SIGNED_RG11_EAC;
525                 case GL_COMPRESSED_RGB8_ETC2:
526                         return sw::FORMAT_RGB8_ETC2;
527                 case GL_COMPRESSED_SRGB8_ETC2:
528                         return sw::FORMAT_SRGB8_ETC2;
529                 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
530                         return sw::FORMAT_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
531                 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
532                         return sw::FORMAT_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
533                 case GL_COMPRESSED_RGBA8_ETC2_EAC:
534                         return sw::FORMAT_RGBA8_ETC2_EAC;
535                 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
536                         return sw::FORMAT_SRGB8_ALPHA8_ETC2_EAC;
537                 case GL_COMPRESSED_RGBA_ASTC_4x4_KHR:
538                         return sw::FORMAT_RGBA_ASTC_4x4_KHR;
539                 case GL_COMPRESSED_RGBA_ASTC_5x4_KHR:
540                         return sw::FORMAT_RGBA_ASTC_5x4_KHR;
541                 case GL_COMPRESSED_RGBA_ASTC_5x5_KHR:
542                         return sw::FORMAT_RGBA_ASTC_5x5_KHR;
543                 case GL_COMPRESSED_RGBA_ASTC_6x5_KHR:
544                         return sw::FORMAT_RGBA_ASTC_6x5_KHR;
545                 case GL_COMPRESSED_RGBA_ASTC_6x6_KHR:
546                         return sw::FORMAT_RGBA_ASTC_6x6_KHR;
547                 case GL_COMPRESSED_RGBA_ASTC_8x5_KHR:
548                         return sw::FORMAT_RGBA_ASTC_8x5_KHR;
549                 case GL_COMPRESSED_RGBA_ASTC_8x6_KHR:
550                         return sw::FORMAT_RGBA_ASTC_8x6_KHR;
551                 case GL_COMPRESSED_RGBA_ASTC_8x8_KHR:
552                         return sw::FORMAT_RGBA_ASTC_8x8_KHR;
553                 case GL_COMPRESSED_RGBA_ASTC_10x5_KHR:
554                         return sw::FORMAT_RGBA_ASTC_10x5_KHR;
555                 case GL_COMPRESSED_RGBA_ASTC_10x6_KHR:
556                         return sw::FORMAT_RGBA_ASTC_10x6_KHR;
557                 case GL_COMPRESSED_RGBA_ASTC_10x8_KHR:
558                         return sw::FORMAT_RGBA_ASTC_10x8_KHR;
559                 case GL_COMPRESSED_RGBA_ASTC_10x10_KHR:
560                         return sw::FORMAT_RGBA_ASTC_10x10_KHR;
561                 case GL_COMPRESSED_RGBA_ASTC_12x10_KHR:
562                         return sw::FORMAT_RGBA_ASTC_12x10_KHR;
563                 case GL_COMPRESSED_RGBA_ASTC_12x12_KHR:
564                         return sw::FORMAT_RGBA_ASTC_12x12_KHR;
565                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
566                         return sw::FORMAT_SRGB8_ALPHA8_ASTC_4x4_KHR;
567                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
568                         return sw::FORMAT_SRGB8_ALPHA8_ASTC_5x4_KHR;
569                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
570                         return sw::FORMAT_SRGB8_ALPHA8_ASTC_5x5_KHR;
571                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
572                         return sw::FORMAT_SRGB8_ALPHA8_ASTC_6x5_KHR;
573                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
574                         return sw::FORMAT_SRGB8_ALPHA8_ASTC_6x6_KHR;
575                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
576                         return sw::FORMAT_SRGB8_ALPHA8_ASTC_8x5_KHR;
577                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
578                         return sw::FORMAT_SRGB8_ALPHA8_ASTC_8x6_KHR;
579                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
580                         return sw::FORMAT_SRGB8_ALPHA8_ASTC_8x8_KHR;
581                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
582                         return sw::FORMAT_SRGB8_ALPHA8_ASTC_10x5_KHR;
583                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
584                         return sw::FORMAT_SRGB8_ALPHA8_ASTC_10x6_KHR;
585                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
586                         return sw::FORMAT_SRGB8_ALPHA8_ASTC_10x8_KHR;
587                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
588                         return sw::FORMAT_SRGB8_ALPHA8_ASTC_10x10_KHR;
589                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
590                         return sw::FORMAT_SRGB8_ALPHA8_ASTC_12x10_KHR;
591                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
592                         return sw::FORMAT_SRGB8_ALPHA8_ASTC_12x12_KHR;
593                 #if S3TC_SUPPORT
594                 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
595                 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
596                         return sw::FORMAT_DXT1;
597                 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
598                         return sw::FORMAT_DXT3;
599                 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
600                         return sw::FORMAT_DXT5;
601                 #endif
602                 default:
603                         break;
604                 }
605
606                 switch(type)
607                 {
608                 case GL_FLOAT:
609                         switch(format)
610                         {
611                         case GL_ALPHA:
612                         case GL_ALPHA32F_EXT:
613                                 return sw::FORMAT_A32F;
614                         case GL_LUMINANCE:
615                         case GL_LUMINANCE32F_EXT:
616                                 return sw::FORMAT_L32F;
617                         case GL_LUMINANCE_ALPHA:
618                         case GL_LUMINANCE_ALPHA32F_EXT:
619                                 return sw::FORMAT_A32L32F;
620                         case GL_RED:
621                         case GL_R32F:
622                                 return sw::FORMAT_R32F;
623                         case GL_RG:
624                         case GL_RG32F:
625                                 return sw::FORMAT_G32R32F;
626                         case GL_RGB:
627                         case GL_RGB32F:
628                         case GL_RGBA:
629                         case GL_RGBA32F:
630                                 return sw::FORMAT_A32B32G32R32F;
631                         case GL_DEPTH_COMPONENT:
632                         case GL_DEPTH_COMPONENT32F:
633                                 return sw::FORMAT_D32F;
634                         default:
635                                 UNREACHABLE(format);
636                         }
637                 case GL_HALF_FLOAT:
638                 case GL_HALF_FLOAT_OES:
639                         switch(format)
640                         {
641                         case GL_ALPHA:
642                         case GL_ALPHA16F_EXT:
643                                 return sw::FORMAT_A16F;
644                         case GL_LUMINANCE:
645                         case GL_LUMINANCE16F_EXT:
646                                 return sw::FORMAT_L16F;
647                         case GL_LUMINANCE_ALPHA:
648                         case GL_LUMINANCE_ALPHA16F_EXT:
649                                 return sw::FORMAT_A16L16F;
650                         case GL_RED:
651                         case GL_R16F:
652                                 return sw::FORMAT_R16F;
653                         case GL_RG:
654                         case GL_RG16F:
655                                 return sw::FORMAT_G16R16F;
656                         case GL_RGB:
657                         case GL_RGB16F:
658                         case GL_RGBA:
659                         case GL_RGBA16F:
660                                 return sw::FORMAT_A16B16G16R16F;
661                         default:
662                                 UNREACHABLE(format);
663                         }
664                 case GL_BYTE:
665                         switch(format)
666                         {
667                         case GL_R8_SNORM:
668                         case GL_R8:
669                         case GL_RED:
670                                 return sw::FORMAT_R8I_SNORM;
671                         case GL_R8I:
672                         case GL_RED_INTEGER:
673                                 return sw::FORMAT_R8I;
674                         case GL_RG8_SNORM:
675                         case GL_RG8:
676                         case GL_RG:
677                                 return sw::FORMAT_G8R8I_SNORM;
678                         case GL_RG8I:
679                         case GL_RG_INTEGER:
680                                 return sw::FORMAT_G8R8I;
681                         case GL_RGB8_SNORM:
682                         case GL_RGB8:
683                         case GL_RGB:
684                                 return sw::FORMAT_X8B8G8R8I_SNORM;
685                         case GL_RGB8I:
686                         case GL_RGB_INTEGER:
687                                 return sw::FORMAT_X8B8G8R8I;
688                         case GL_RGBA8_SNORM:
689                         case GL_RGBA8:
690                         case GL_RGBA:
691                                 return sw::FORMAT_A8B8G8R8I_SNORM;
692                         case GL_RGBA8I:
693                         case GL_RGBA_INTEGER:
694                                 return sw::FORMAT_A8B8G8R8I;
695                         default:
696                                 UNREACHABLE(format);
697                         }
698                 case GL_UNSIGNED_BYTE:
699                         switch(format)
700                         {
701                         case GL_LUMINANCE:
702                         case GL_LUMINANCE8_EXT:
703                                 return sw::FORMAT_L8;
704                         case GL_LUMINANCE_ALPHA:
705                         case GL_LUMINANCE8_ALPHA8_EXT:
706                                 return sw::FORMAT_A8L8;
707                         case GL_R8_SNORM:
708                         case GL_R8:
709                         case GL_RED:
710                                 return sw::FORMAT_R8;
711                         case GL_R8UI:
712                         case GL_RED_INTEGER:
713                                 return sw::FORMAT_R8UI;
714                         case GL_RG8_SNORM:
715                         case GL_RG8:
716                         case GL_RG:
717                                 return sw::FORMAT_G8R8;
718                         case GL_RG8UI:
719                         case GL_RG_INTEGER:
720                                 return sw::FORMAT_G8R8UI;
721                         case GL_RGB8_SNORM:
722                         case GL_RGB8:
723                         case GL_RGB:
724                         case GL_SRGB8:
725                                 return sw::FORMAT_X8B8G8R8;
726                         case GL_RGB8UI:
727                         case GL_RGB_INTEGER:
728                                 return sw::FORMAT_X8B8G8R8UI;
729                         case GL_RGBA8_SNORM:
730                         case GL_RGBA8:
731                         case GL_RGBA:
732                         case GL_SRGB8_ALPHA8:
733                                 return sw::FORMAT_A8B8G8R8;
734                         case GL_RGBA8UI:
735                         case GL_RGBA_INTEGER:
736                                 return sw::FORMAT_A8B8G8R8UI;
737                         case GL_BGRA_EXT:
738                         case GL_BGRA8_EXT:
739                                 return sw::FORMAT_A8R8G8B8;
740                         case GL_ALPHA:
741                         case GL_ALPHA8_EXT:
742                                 return sw::FORMAT_A8;
743                         case SW_YV12_BT601:
744                                 return sw::FORMAT_YV12_BT601;
745                         case SW_YV12_BT709:
746                                 return sw::FORMAT_YV12_BT709;
747                         case SW_YV12_JFIF:
748                                 return sw::FORMAT_YV12_JFIF;
749                         default:
750                                 UNREACHABLE(format);
751                         }
752                 case GL_SHORT:
753                         switch(format)
754                         {
755                         case GL_R16I:
756                         case GL_RED_INTEGER:
757                                 return sw::FORMAT_R16I;
758                         case GL_RG16I:
759                         case GL_RG_INTEGER:
760                                 return sw::FORMAT_G16R16I;
761                         case GL_RGB16I:
762                         case GL_RGB_INTEGER:
763                                 return sw::FORMAT_X16B16G16R16I;
764                         case GL_RGBA16I:
765                         case GL_RGBA_INTEGER:
766                                 return sw::FORMAT_A16B16G16R16I;
767                         default:
768                                 UNREACHABLE(format);
769                         }
770                 case GL_UNSIGNED_SHORT:
771                         switch(format)
772                         {
773                         case GL_R16UI:
774                         case GL_RED_INTEGER:
775                                 return sw::FORMAT_R16UI;
776                         case GL_RG16UI:
777                         case GL_RG_INTEGER:
778                                 return sw::FORMAT_G16R16UI;
779                         case GL_RGB16UI:
780                         case GL_RGB_INTEGER:
781                                 return sw::FORMAT_X16B16G16R16UI;
782                         case GL_RGBA16UI:
783                         case GL_RGBA_INTEGER:
784                                 return sw::FORMAT_A16B16G16R16UI;
785                         case GL_DEPTH_COMPONENT:
786                         case GL_DEPTH_COMPONENT16:
787                                 return sw::FORMAT_D32FS8_TEXTURE;
788                         default:
789                                 UNREACHABLE(format);
790                         }
791                 case GL_INT:
792                         switch(format)
793                         {
794                         case GL_RED_INTEGER:
795                         case GL_R32I:
796                                 return sw::FORMAT_R32I;
797                         case GL_RG_INTEGER:
798                         case GL_RG32I:
799                                 return sw::FORMAT_G32R32I;
800                         case GL_RGB_INTEGER:
801                         case GL_RGB32I:
802                                 return sw::FORMAT_X32B32G32R32I;
803                         case GL_RGBA_INTEGER:
804                         case GL_RGBA32I:
805                                 return sw::FORMAT_A32B32G32R32I;
806                         default:
807                                 UNREACHABLE(format);
808                         }
809                 case GL_UNSIGNED_INT:
810                         switch(format)
811                         {
812                         case GL_RED_INTEGER:
813                         case GL_R32UI:
814                                 return sw::FORMAT_R32UI;
815                         case GL_RG_INTEGER:
816                         case GL_RG32UI:
817                                 return sw::FORMAT_G32R32UI;
818                         case GL_RGB_INTEGER:
819                         case GL_RGB32UI:
820                                 return sw::FORMAT_X32B32G32R32UI;
821                         case GL_RGBA_INTEGER:
822                         case GL_RGBA32UI:
823                                 return sw::FORMAT_A32B32G32R32UI;
824                         case GL_DEPTH_COMPONENT:
825                         case GL_DEPTH_COMPONENT16:
826                         case GL_DEPTH_COMPONENT24:
827                         case GL_DEPTH_COMPONENT32_OES:
828                                 return sw::FORMAT_D32FS8_TEXTURE;
829                         default:
830                                 UNREACHABLE(format);
831                         }
832                 case GL_UNSIGNED_INT_24_8_OES:
833                         if(format == GL_DEPTH_STENCIL || format == GL_DEPTH24_STENCIL8)
834                         {
835                                 return sw::FORMAT_D32FS8_TEXTURE;
836                         }
837                         else UNREACHABLE(format);
838                 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
839                         if(format == GL_DEPTH_STENCIL || format == GL_DEPTH32F_STENCIL8)
840                         {
841                                 return sw::FORMAT_D32FS8_TEXTURE;
842                         }
843                         else UNREACHABLE(format);
844                 case GL_UNSIGNED_SHORT_4_4_4_4:
845                         return sw::FORMAT_A8R8G8B8;
846                 case GL_UNSIGNED_SHORT_5_5_5_1:
847                         return sw::FORMAT_A8R8G8B8;
848                 case GL_UNSIGNED_SHORT_5_6_5:
849                         return sw::FORMAT_R5G6B5;
850                 case GL_UNSIGNED_INT_2_10_10_10_REV:
851                         if(format == GL_RGB10_A2UI)
852                         {
853                                 return sw::FORMAT_A16B16G16R16UI;
854                         }
855                         else
856                         {
857                                 return sw::FORMAT_A2B10G10R10;
858                         }
859                 case GL_UNSIGNED_INT_10F_11F_11F_REV:
860                 case GL_UNSIGNED_INT_5_9_9_9_REV:
861                         return sw::FORMAT_A32B32G32R32F;
862                 default:
863                         UNREACHABLE(type);
864                 }
865
866                 return sw::FORMAT_NULL;
867         }
868
869         // Returns the size, in bytes, of a single texel in an Image
870         static int ComputePixelSize(GLenum format, GLenum type)
871         {
872                 switch(type)
873                 {
874                 case GL_BYTE:
875                         switch(format)
876                         {
877                         case GL_R8:
878                         case GL_R8I:
879                         case GL_R8_SNORM:
880                         case GL_RED:             return sizeof(char);
881                         case GL_RED_INTEGER:     return sizeof(char);
882                         case GL_RG8:
883                         case GL_RG8I:
884                         case GL_RG8_SNORM:
885                         case GL_RG:              return sizeof(char) * 2;
886                         case GL_RG_INTEGER:      return sizeof(char) * 2;
887                         case GL_RGB8:
888                         case GL_RGB8I:
889                         case GL_RGB8_SNORM:
890                         case GL_RGB:             return sizeof(char) * 3;
891                         case GL_RGB_INTEGER:     return sizeof(char) * 3;
892                         case GL_RGBA8:
893                         case GL_RGBA8I:
894                         case GL_RGBA8_SNORM:
895                         case GL_RGBA:            return sizeof(char) * 4;
896                         case GL_RGBA_INTEGER:    return sizeof(char) * 4;
897                         default: UNREACHABLE(format);
898                         }
899                         break;
900                 case GL_UNSIGNED_BYTE:
901                         switch(format)
902                         {
903                         case GL_R8:
904                         case GL_R8UI:
905                         case GL_RED:             return sizeof(unsigned char);
906                         case GL_RED_INTEGER:     return sizeof(unsigned char);
907                         case GL_ALPHA8_EXT:
908                         case GL_ALPHA:           return sizeof(unsigned char);
909                         case GL_LUMINANCE8_EXT:
910                         case GL_LUMINANCE:       return sizeof(unsigned char);
911                         case GL_LUMINANCE8_ALPHA8_EXT:
912                         case GL_LUMINANCE_ALPHA: return sizeof(unsigned char) * 2;
913                         case GL_RG8:
914                         case GL_RG8UI:
915                         case GL_RG:              return sizeof(unsigned char) * 2;
916                         case GL_RG_INTEGER:      return sizeof(unsigned char) * 2;
917                         case GL_RGB8:
918                         case GL_RGB8UI:
919                         case GL_SRGB8:
920                         case GL_RGB:             return sizeof(unsigned char) * 3;
921                         case GL_RGB_INTEGER:     return sizeof(unsigned char) * 3;
922                         case GL_RGBA8:
923                         case GL_RGBA8UI:
924                         case GL_SRGB8_ALPHA8:
925                         case GL_RGBA:            return sizeof(unsigned char) * 4;
926                         case GL_RGBA_INTEGER:    return sizeof(unsigned char) * 4;
927                         case GL_BGRA_EXT:
928                         case GL_BGRA8_EXT:       return sizeof(unsigned char)* 4;
929                         default: UNREACHABLE(format);
930                         }
931                         break;
932                 case GL_SHORT:
933                         switch(format)
934                         {
935                         case GL_R16I:
936                         case GL_RED_INTEGER:     return sizeof(short);
937                         case GL_RG16I:
938                         case GL_RG_INTEGER:      return sizeof(short) * 2;
939                         case GL_RGB16I:
940                         case GL_RGB_INTEGER:     return sizeof(short) * 3;
941                         case GL_RGBA16I:
942                         case GL_RGBA_INTEGER:    return sizeof(short) * 4;
943                         default: UNREACHABLE(format);
944                         }
945                         break;
946                 case GL_UNSIGNED_SHORT:
947                         switch(format)
948                         {
949                         case GL_DEPTH_COMPONENT16:
950                         case GL_DEPTH_COMPONENT: return sizeof(unsigned short);
951                         case GL_R16UI:
952                         case GL_RED_INTEGER:     return sizeof(unsigned short);
953                         case GL_RG16UI:
954                         case GL_RG_INTEGER:      return sizeof(unsigned short) * 2;
955                         case GL_RGB16UI:
956                         case GL_RGB_INTEGER:     return sizeof(unsigned short) * 3;
957                         case GL_RGBA16UI:
958                         case GL_RGBA_INTEGER:    return sizeof(unsigned short) * 4;
959                         default: UNREACHABLE(format);
960                         }
961                         break;
962                 case GL_INT:
963                         switch(format)
964                         {
965                         case GL_R32I:
966                         case GL_RED_INTEGER:     return sizeof(int);
967                         case GL_RG32I:
968                         case GL_RG_INTEGER:      return sizeof(int) * 2;
969                         case GL_RGB32I:
970                         case GL_RGB_INTEGER:     return sizeof(int) * 3;
971                         case GL_RGBA32I:
972                         case GL_RGBA_INTEGER:    return sizeof(int) * 4;
973                         default: UNREACHABLE(format);
974                         }
975                         break;
976                 case GL_UNSIGNED_INT:
977                         switch(format)
978                         {
979                         case GL_DEPTH_COMPONENT16:
980                         case GL_DEPTH_COMPONENT24:
981                         case GL_DEPTH_COMPONENT32_OES:
982                         case GL_DEPTH_COMPONENT: return sizeof(unsigned int);
983                         case GL_R32UI:
984                         case GL_RED_INTEGER:     return sizeof(unsigned int);
985                         case GL_RG32UI:
986                         case GL_RG_INTEGER:      return sizeof(unsigned int) * 2;
987                         case GL_RGB32UI:
988                         case GL_RGB_INTEGER:     return sizeof(unsigned int) * 3;
989                         case GL_RGBA32UI:
990                         case GL_RGBA_INTEGER:    return sizeof(unsigned int) * 4;
991                         default: UNREACHABLE(format);
992                         }
993                         break;
994                 case GL_UNSIGNED_SHORT_4_4_4_4:
995                 case GL_UNSIGNED_SHORT_5_5_5_1:
996                 case GL_UNSIGNED_SHORT_5_6_5:
997                         return sizeof(unsigned short);
998                 case GL_UNSIGNED_INT_10F_11F_11F_REV:
999                 case GL_UNSIGNED_INT_5_9_9_9_REV:
1000                 case GL_UNSIGNED_INT_2_10_10_10_REV:
1001                 case GL_UNSIGNED_INT_24_8_OES:
1002                         return sizeof(unsigned int);
1003                 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
1004                         return sizeof(float) + sizeof(unsigned int);
1005                 case GL_FLOAT:
1006                         switch(format)
1007                         {
1008                         case GL_DEPTH_COMPONENT32F:
1009                         case GL_DEPTH_COMPONENT: return sizeof(float);
1010                         case GL_ALPHA32F_EXT:
1011                         case GL_ALPHA:           return sizeof(float);
1012                         case GL_LUMINANCE32F_EXT:
1013                         case GL_LUMINANCE:       return sizeof(float);
1014                         case GL_LUMINANCE_ALPHA32F_EXT:
1015                         case GL_LUMINANCE_ALPHA: return sizeof(float) * 2;
1016                         case GL_RED:             return sizeof(float);
1017                         case GL_R32F:            return sizeof(float);
1018                         case GL_RG:              return sizeof(float) * 2;
1019                         case GL_RG32F:           return sizeof(float) * 2;
1020                         case GL_RGB:             return sizeof(float) * 3;
1021                         case GL_RGB32F:          return sizeof(float) * 3;
1022                         case GL_RGBA:            return sizeof(float) * 4;
1023                         case GL_RGBA32F:         return sizeof(float) * 4;
1024                         default: UNREACHABLE(format);
1025                         }
1026                         break;
1027                 case GL_HALF_FLOAT:
1028                 case GL_HALF_FLOAT_OES:
1029                         switch(format)
1030                         {
1031                         case GL_ALPHA16F_EXT:
1032                         case GL_ALPHA:           return sizeof(unsigned short);
1033                         case GL_LUMINANCE16F_EXT:
1034                         case GL_LUMINANCE:       return sizeof(unsigned short);
1035                         case GL_LUMINANCE_ALPHA16F_EXT:
1036                         case GL_LUMINANCE_ALPHA: return sizeof(unsigned short) * 2;
1037                         case GL_RED:             return sizeof(unsigned short);
1038                         case GL_R16F:            return sizeof(unsigned short);
1039                         case GL_RG:              return sizeof(unsigned short) * 2;
1040                         case GL_RG16F:           return sizeof(unsigned short) * 2;
1041                         case GL_RGB:             return sizeof(unsigned short) * 3;
1042                         case GL_RGB16F:          return sizeof(unsigned short) * 3;
1043                         case GL_RGBA:            return sizeof(unsigned short) * 4;
1044                         case GL_RGBA16F:         return sizeof(unsigned short) * 4;
1045                         default: UNREACHABLE(format);
1046                         }
1047                         break;
1048                 default: UNREACHABLE(type);
1049                 }
1050
1051                 return 0;
1052         }
1053
1054         GLsizei ComputePitch(GLsizei width, GLenum format, GLenum type, GLint alignment)
1055         {
1056                 ASSERT(alignment > 0 && sw::isPow2(alignment));
1057
1058                 GLsizei rawPitch = ComputePixelSize(format, type) * width;
1059                 return (rawPitch + alignment - 1) & ~(alignment - 1);
1060         }
1061
1062
1063         GLsizei ComputeCompressedPitch(GLsizei width, GLenum format)
1064         {
1065                 return ComputeCompressedSize(width, 1, format);
1066         }
1067
1068         GLsizei ComputeCompressedSize(GLsizei width, GLsizei height, GLenum format)
1069         {
1070                 switch(format)
1071                 {
1072                 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
1073                 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
1074         case GL_ETC1_RGB8_OES:
1075                 case GL_COMPRESSED_R11_EAC:
1076                 case GL_COMPRESSED_SIGNED_R11_EAC:
1077                 case GL_COMPRESSED_RGB8_ETC2:
1078                 case GL_COMPRESSED_SRGB8_ETC2:
1079                 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
1080                 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
1081                         return 8 * getNumBlocks(width, height, 4, 4);
1082                 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
1083                 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
1084                 case GL_COMPRESSED_RG11_EAC:
1085                 case GL_COMPRESSED_SIGNED_RG11_EAC:
1086                 case GL_COMPRESSED_RGBA8_ETC2_EAC:
1087                 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
1088                 case GL_COMPRESSED_RGBA_ASTC_4x4_KHR:
1089                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
1090                         return 16 * getNumBlocks(width, height, 4, 4);
1091                 case GL_COMPRESSED_RGBA_ASTC_5x4_KHR:
1092                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
1093                         return 16 * getNumBlocks(width, height, 5, 4);
1094                 case GL_COMPRESSED_RGBA_ASTC_5x5_KHR:
1095                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
1096                         return 16 * getNumBlocks(width, height, 5, 5);
1097                 case GL_COMPRESSED_RGBA_ASTC_6x5_KHR:
1098                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
1099                         return 16 * getNumBlocks(width, height, 6, 5);
1100                 case GL_COMPRESSED_RGBA_ASTC_6x6_KHR:
1101                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
1102                         return 16 * getNumBlocks(width, height, 6, 6);
1103                 case GL_COMPRESSED_RGBA_ASTC_8x5_KHR:
1104                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
1105                         return 16 * getNumBlocks(width, height, 8, 5);
1106                 case GL_COMPRESSED_RGBA_ASTC_8x6_KHR:
1107                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
1108                         return 16 * getNumBlocks(width, height, 8, 6);
1109                 case GL_COMPRESSED_RGBA_ASTC_8x8_KHR:
1110                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
1111                         return 16 * getNumBlocks(width, height, 8, 8);
1112                 case GL_COMPRESSED_RGBA_ASTC_10x5_KHR:
1113                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
1114                         return 16 * getNumBlocks(width, height, 10, 5);
1115                 case GL_COMPRESSED_RGBA_ASTC_10x6_KHR:
1116                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
1117                         return 16 * getNumBlocks(width, height, 10, 6);
1118                 case GL_COMPRESSED_RGBA_ASTC_10x8_KHR:
1119                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
1120                         return 16 * getNumBlocks(width, height, 10, 8);
1121                 case GL_COMPRESSED_RGBA_ASTC_10x10_KHR:
1122                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
1123                         return 16 * getNumBlocks(width, height, 10, 10);
1124                 case GL_COMPRESSED_RGBA_ASTC_12x10_KHR:
1125                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
1126                         return 16 * getNumBlocks(width, height, 12, 10);
1127                 case GL_COMPRESSED_RGBA_ASTC_12x12_KHR:
1128                 case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
1129                         return 16 * getNumBlocks(width, height, 12, 12);
1130                 default:
1131                         return 0;
1132                 }
1133         }
1134
1135         Image::~Image()
1136         {
1137                 ASSERT(referenceCount == 0);
1138         }
1139
1140         void Image::addRef()
1141         {
1142                 if(parentTexture)
1143                 {
1144                         return parentTexture->addRef();
1145                 }
1146                 int newCount = sw::atomicIncrement(&referenceCount);
1147                 LOGLOCK("%s image=%p referenceCount=%d", __FUNCTION__, this, newCount);
1148         }
1149
1150         void Image::release()
1151         {
1152                 if(parentTexture)
1153                 {
1154                         return parentTexture->release();
1155                 }
1156
1157                 int newCount = sw::atomicDecrement(&referenceCount);
1158                 LOGLOCK("%s image=%p referenceCount=%d", __FUNCTION__, this, newCount);
1159                 if (newCount == 0)
1160                 {
1161                         ASSERT(!shared);   // Should still hold a reference if eglDestroyImage hasn't been called
1162                         delete this;
1163                 }
1164         }
1165
1166         void Image::unbind(const egl::Texture *parent)
1167         {
1168                 if(parentTexture == parent)
1169                 {
1170                         parentTexture = 0;
1171                 }
1172
1173                 release();
1174         }
1175
1176         void Image::loadImageData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const UnpackInfo& unpackInfo, const void *input)
1177         {
1178                 GLsizei inputPitch = ComputePitch((unpackInfo.rowLength == 0) ? width : unpackInfo.rowLength, format, type, unpackInfo.alignment);
1179                 GLsizei inputHeight = (unpackInfo.imageHeight == 0) ? height : unpackInfo.imageHeight;
1180                 input = ((char*)input) + (unpackInfo.skipImages * inputHeight + unpackInfo.skipRows) * inputPitch + unpackInfo.skipPixels;
1181                 sw::Format selectedInternalFormat = SelectInternalFormat(format, type);
1182                 if(selectedInternalFormat == sw::FORMAT_NULL)
1183                 {
1184                         return;
1185                 }
1186
1187                 if(selectedInternalFormat == internalFormat)
1188                 {
1189                         void *buffer = lock(0, 0, sw::LOCK_WRITEONLY);
1190
1191                         if(buffer)
1192                         {
1193                                 switch(type)
1194                                 {
1195                                 case GL_BYTE:
1196                                         switch(format)
1197                                         {
1198                                         case GL_R8:
1199                                         case GL_R8I:
1200                                         case GL_R8_SNORM:
1201                                         case GL_RED:
1202                                         case GL_RED_INTEGER:
1203                                         case GL_ALPHA:
1204                                         case GL_ALPHA8_EXT:
1205                                         case GL_LUMINANCE:
1206                                         case GL_LUMINANCE8_EXT:
1207                                                 LoadImageData<Bytes_1>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1208                                                 break;
1209                                         case GL_RG8:
1210                                         case GL_RG8I:
1211                                         case GL_RG8_SNORM:
1212                                         case GL_RG:
1213                                         case GL_RG_INTEGER:
1214                                         case GL_LUMINANCE_ALPHA:
1215                                         case GL_LUMINANCE8_ALPHA8_EXT:
1216                                                 LoadImageData<Bytes_2>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1217                                                 break;
1218                                         case GL_RGB8:
1219                                         case GL_RGB8I:
1220                                         case GL_RGB8_SNORM:
1221                                         case GL_RGB:
1222                                         case GL_RGB_INTEGER:
1223                                                 LoadImageData<ByteRGB>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1224                                                 break;
1225                                         case GL_RGBA8:
1226                                         case GL_RGBA8I:
1227                                         case GL_RGBA8_SNORM:
1228                                         case GL_RGBA:
1229                                         case GL_RGBA_INTEGER:
1230                                         case GL_BGRA_EXT:
1231                                         case GL_BGRA8_EXT:
1232                                                 LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1233                                                 break;
1234                                         default: UNREACHABLE(format);
1235                                         }
1236                                         break;
1237                                 case GL_UNSIGNED_BYTE:
1238                                         switch(format)
1239                                         {
1240                                         case GL_R8:
1241                                         case GL_R8UI:
1242                                         case GL_R8_SNORM:
1243                                         case GL_RED:
1244                                         case GL_RED_INTEGER:
1245                                         case GL_ALPHA:
1246                                         case GL_ALPHA8_EXT:
1247                                         case GL_LUMINANCE:
1248                                         case GL_LUMINANCE8_EXT:
1249                                                 LoadImageData<Bytes_1>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1250                                                 break;
1251                                         case GL_RG8:
1252                                         case GL_RG8UI:
1253                                         case GL_RG8_SNORM:
1254                                         case GL_RG:
1255                                         case GL_RG_INTEGER:
1256                                         case GL_LUMINANCE_ALPHA:
1257                                         case GL_LUMINANCE8_ALPHA8_EXT:
1258                                                 LoadImageData<Bytes_2>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1259                                                 break;
1260                                         case GL_RGB8:
1261                                         case GL_RGB8UI:
1262                                         case GL_RGB8_SNORM:
1263                                         case GL_RGB:
1264                                         case GL_RGB_INTEGER:
1265                                                 LoadImageData<UByteRGB>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1266                                                 break;
1267                                         case GL_RGBA8:
1268                                         case GL_RGBA8UI:
1269                                         case GL_RGBA8_SNORM:
1270                                         case GL_RGBA:
1271                                         case GL_RGBA_INTEGER:
1272                                         case GL_BGRA_EXT:
1273                                         case GL_BGRA8_EXT:
1274                                                 LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1275                                                 break;
1276                                         case GL_SRGB8:
1277                                                 LoadImageData<SRGB>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1278                                                 break;
1279                                         case GL_SRGB8_ALPHA8:
1280                                                 LoadImageData<SRGBA>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1281                                                 break;
1282                                         default: UNREACHABLE(format);
1283                                         }
1284                                         break;
1285                                 case GL_UNSIGNED_SHORT_5_6_5:
1286                                         switch(format)
1287                                         {
1288                                         case GL_RGB565:
1289                                         case GL_RGB:
1290                                                 LoadImageData<RGB565>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1291                                                 break;
1292                                         default: UNREACHABLE(format);
1293                                         }
1294                                         break;
1295                                 case GL_UNSIGNED_SHORT_4_4_4_4:
1296                                         switch(format)
1297                                         {
1298                                         case GL_RGBA4:
1299                                         case GL_RGBA:
1300                                                 LoadImageData<RGBA4444>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1301                                                 break;
1302                                         default: UNREACHABLE(format);
1303                                         }
1304                                         break;
1305                                 case GL_UNSIGNED_SHORT_5_5_5_1:
1306                                         switch(format)
1307                                         {
1308                                         case GL_RGB5_A1:
1309                                         case GL_RGBA:
1310                                                 LoadImageData<RGBA5551>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1311                                                 break;
1312                                         default: UNREACHABLE(format);
1313                                         }
1314                                         break;
1315                                 case GL_UNSIGNED_INT_10F_11F_11F_REV:
1316                                         switch(format)
1317                                         {
1318                                         case GL_R11F_G11F_B10F:
1319                                         case GL_RGB:
1320                                                 LoadImageData<R11G11B10F>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1321                                                 break;
1322                                         default: UNREACHABLE(format);
1323                                         }
1324                                         break;
1325                                 case GL_UNSIGNED_INT_5_9_9_9_REV:
1326                                         switch(format)
1327                                         {
1328                                         case GL_RGB9_E5:
1329                                         case GL_RGB:
1330                                                 LoadImageData<RGB9E5>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1331                                                 break;
1332                                         default: UNREACHABLE(format);
1333                                         }
1334                                         break;
1335                                 case GL_UNSIGNED_INT_2_10_10_10_REV:
1336                                         switch(format)
1337                                         {
1338                                         case GL_RGB10_A2UI:
1339                                                 LoadImageData<RGB10A2UI>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1340                                                 break;
1341                                         case GL_RGB10_A2:
1342                                         case GL_RGBA:
1343                                         case GL_RGBA_INTEGER:
1344                                                 LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1345                                                 break;
1346                                         default: UNREACHABLE(format);
1347                                         }
1348                                         break;
1349                                 case GL_FLOAT:
1350                                         switch(format)
1351                                         {
1352                                         // float textures are converted to RGBA, not BGRA
1353                                         case GL_ALPHA:
1354                                         case GL_ALPHA32F_EXT:
1355                                                 LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1356                                                 break;
1357                                         case GL_LUMINANCE:
1358                                         case GL_LUMINANCE32F_EXT:
1359                                                 LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1360                                                 break;
1361                                         case GL_LUMINANCE_ALPHA:
1362                                         case GL_LUMINANCE_ALPHA32F_EXT:
1363                                                 LoadImageData<Bytes_8>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1364                                                 break;
1365                                         case GL_RED:
1366                                         case GL_R32F:
1367                                                 LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1368                                                 break;
1369                                         case GL_RG:
1370                                         case GL_RG32F:
1371                                                 LoadImageData<Bytes_8>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1372                                                 break;
1373                                         case GL_RGB:
1374                                         case GL_RGB32F:
1375                                                 LoadImageData<FloatRGB>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1376                                                 break;
1377                                         case GL_RGBA:
1378                                         case GL_RGBA32F:
1379                                                 LoadImageData<Bytes_16>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1380                                                 break;
1381                                         case GL_DEPTH_COMPONENT:
1382                                         case GL_DEPTH_COMPONENT32F:
1383                                                 LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1384                                                 break;
1385                                         default: UNREACHABLE(format);
1386                                         }
1387                                         break;
1388                                 case GL_HALF_FLOAT:
1389                                 case GL_HALF_FLOAT_OES:
1390                                         switch(format)
1391                                         {
1392                                         case GL_ALPHA:
1393                                         case GL_ALPHA16F_EXT:
1394                                                 LoadImageData<Bytes_2>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1395                                                 break;
1396                                         case GL_LUMINANCE:
1397                                         case GL_LUMINANCE16F_EXT:
1398                                                 LoadImageData<Bytes_2>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1399                                                 break;
1400                                         case GL_LUMINANCE_ALPHA:
1401                                         case GL_LUMINANCE_ALPHA16F_EXT:
1402                                                 LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1403                                                 break;
1404                                         case GL_RED:
1405                                         case GL_R16F:
1406                                                 LoadImageData<Bytes_2>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1407                                                 break;
1408                                         case GL_RG:
1409                                         case GL_RG16F:
1410                                                 LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1411                                                 break;
1412                                         case GL_RGB:
1413                                         case GL_RGB16F:
1414                                                 LoadImageData<HalfFloatRGB>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1415                                                 break;
1416                                         case GL_RGBA:
1417                                         case GL_RGBA16F:
1418                                                 LoadImageData<Bytes_8>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1419                                                 break;
1420                                         default: UNREACHABLE(format);
1421                                         }
1422                                         break;
1423                                 case GL_SHORT:
1424                                         switch(format)
1425                                         {
1426                                         case GL_R16I:
1427                                         case GL_RED:
1428                                         case GL_RED_INTEGER:
1429                                         case GL_ALPHA:
1430                                         case GL_LUMINANCE:
1431                                                 LoadImageData<Bytes_2>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1432                                                 break;
1433                                         case GL_RG16I:
1434                                         case GL_RG:
1435                                         case GL_RG_INTEGER:
1436                                         case GL_LUMINANCE_ALPHA:
1437                                                 LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1438                                                 break;
1439                                         case GL_RGB16I:
1440                                         case GL_RGB:
1441                                         case GL_RGB_INTEGER:
1442                                                 LoadImageData<ShortRGB>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1443                                                 break;
1444                                         case GL_RGBA16I:
1445                                         case GL_RGBA:
1446                                         case GL_RGBA_INTEGER:
1447                                         case GL_BGRA_EXT:
1448                                         case GL_BGRA8_EXT:
1449                                                 LoadImageData<Bytes_8>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1450                                                 break;
1451                                         default: UNREACHABLE(format);
1452                                         }
1453                                         break;
1454                                 case GL_UNSIGNED_SHORT:
1455                                         switch(format)
1456                                         {
1457                                         case GL_R16UI:
1458                                         case GL_RED:
1459                                         case GL_RED_INTEGER:
1460                                         case GL_ALPHA:
1461                                         case GL_LUMINANCE:
1462                                                 LoadImageData<Bytes_2>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1463                                                 break;
1464                                         case GL_RG16UI:
1465                                         case GL_RG:
1466                                         case GL_RG_INTEGER:
1467                                         case GL_LUMINANCE_ALPHA:
1468                                                 LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1469                                                 break;
1470                                         case GL_RGB16UI:
1471                                         case GL_RGB:
1472                                         case GL_RGB_INTEGER:
1473                                                 LoadImageData<UShortRGB>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1474                                                 break;
1475                                         case GL_RGBA16UI:
1476                                         case GL_RGBA:
1477                                         case GL_RGBA_INTEGER:
1478                                         case GL_BGRA_EXT:
1479                                         case GL_BGRA8_EXT:
1480                                                 LoadImageData<Bytes_8>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1481                                                 break;
1482                                         case GL_DEPTH_COMPONENT:
1483                                         case GL_DEPTH_COMPONENT16:
1484                                                 LoadImageData<D16>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1485                                                 break;
1486                                         default: UNREACHABLE(format);
1487                                         }
1488                                         break;
1489                                 case GL_INT:
1490                                         switch(format)
1491                                         {
1492                                         case GL_R32I:
1493                                         case GL_RED:
1494                                         case GL_RED_INTEGER:
1495                                         case GL_ALPHA:
1496                                         case GL_LUMINANCE:
1497                                                 LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1498                                                 break;
1499                                         case GL_RG32I:
1500                                         case GL_RG:
1501                                         case GL_RG_INTEGER:
1502                                         case GL_LUMINANCE_ALPHA:
1503                                                 LoadImageData<Bytes_8>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1504                                                 break;
1505                                         case GL_RGB32I:
1506                                         case GL_RGB:
1507                                         case GL_RGB_INTEGER:
1508                                                 LoadImageData<IntRGB>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1509                                                 break;
1510                                         case GL_RGBA32I:
1511                                         case GL_RGBA:
1512                                         case GL_RGBA_INTEGER:
1513                                         case GL_BGRA_EXT:
1514                                         case GL_BGRA8_EXT:
1515                                                 LoadImageData<Bytes_16>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1516                                                 break;
1517                                         default: UNREACHABLE(format);
1518                                         }
1519                                         break;
1520                                 case GL_UNSIGNED_INT:
1521                                         switch(format)
1522                                         {
1523                                         case GL_R32UI:
1524                                         case GL_RED:
1525                                         case GL_RED_INTEGER:
1526                                         case GL_ALPHA:
1527                                         case GL_LUMINANCE:
1528                                                 LoadImageData<Bytes_2>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1529                                                 break;
1530                                         case GL_RG32UI:
1531                                         case GL_RG:
1532                                         case GL_RG_INTEGER:
1533                                         case GL_LUMINANCE_ALPHA:
1534                                                 LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1535                                                 break;
1536                                         case GL_RGB32UI:
1537                                         case GL_RGB:
1538                                         case GL_RGB_INTEGER:
1539                                                 LoadImageData<UIntRGB>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1540                                                 break;
1541                                         case GL_RGBA32UI:
1542                                         case GL_RGBA:
1543                                         case GL_RGBA_INTEGER:
1544                                         case GL_BGRA_EXT:
1545                                         case GL_BGRA8_EXT:
1546                                                 LoadImageData<Bytes_8>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1547                                                 break;
1548                                         case GL_DEPTH_COMPONENT16:
1549                                         case GL_DEPTH_COMPONENT24:
1550                                         case GL_DEPTH_COMPONENT32_OES:
1551                                         case GL_DEPTH_COMPONENT:
1552                                                 LoadImageData<D32>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1553                                                 break;
1554                                         default: UNREACHABLE(format);
1555                                         }
1556                                         break;
1557                                 case GL_UNSIGNED_INT_24_8_OES:
1558                                         loadD24S8ImageData(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, input, buffer);
1559                                         break;
1560                                 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
1561                                         loadD32FS8ImageData(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, input, buffer);
1562                                         break;
1563                                 default: UNREACHABLE(type);
1564                                 }
1565                         }
1566
1567                         unlock();
1568                 }
1569                 else
1570                 {
1571                         sw::Surface source(width, height, depth, ConvertFormatType(format, type), const_cast<void*>(input), inputPitch, inputPitch * inputHeight);
1572                         sw::Rect sourceRect(0, 0, width, height);
1573                         sw::Rect destRect(xoffset, yoffset, xoffset + width, yoffset + height);
1574                         sw::blitter.blit(&source, sourceRect, this, destRect, false);
1575                 }
1576         }
1577
1578         void Image::loadD24S8ImageData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, int inputPitch, int inputHeight, const void *input, void *buffer)
1579         {
1580                 LoadImageData<D24>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1581
1582                 unsigned char *stencil = reinterpret_cast<unsigned char*>(lockStencil(0, sw::PUBLIC));
1583
1584                 if(stencil)
1585                 {
1586                         LoadImageData<S8>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getStencilPitchB(), getHeight(), input, stencil);
1587
1588                         unlockStencil();
1589                 }
1590         }
1591
1592         void Image::loadD32FS8ImageData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, int inputPitch, int inputHeight, const void *input, void *buffer)
1593         {
1594                 LoadImageData<D32F>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
1595
1596                 unsigned char *stencil = reinterpret_cast<unsigned char*>(lockStencil(0, sw::PUBLIC));
1597
1598                 if(stencil)
1599                 {
1600                         LoadImageData<S24_8>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getStencilPitchB(), getHeight(), input, stencil);
1601
1602                         unlockStencil();
1603                 }
1604         }
1605
1606         void Image::loadCompressedData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLsizei imageSize, const void *pixels)
1607         {
1608                 if(zoffset != 0 || depth != 1)
1609                 {
1610                         UNIMPLEMENTED();   // FIXME
1611                 }
1612
1613                 int inputPitch = ComputeCompressedPitch(width, format);
1614                 int rows = imageSize / inputPitch;
1615                 void *buffer = lock(xoffset, yoffset, sw::LOCK_WRITEONLY);
1616
1617                 if(buffer)
1618                 {
1619                         for(int i = 0; i < rows; i++)
1620                         {
1621                                 memcpy((void*)((GLbyte*)buffer + i * getPitch()), (void*)((GLbyte*)pixels + i * inputPitch), inputPitch);
1622                         }
1623                 }
1624
1625                 unlock();
1626         }
1627 }