OSDN Git Service

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