OSDN Git Service

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