Home | History | Annotate | Download | only in common
      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 "Renderer/Blitter.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 	Image::~Image()
   1182 	{
   1183 		if(parentTexture)
   1184 		{
   1185 			parentTexture->release();
   1186 		}
   1187 
   1188 		ASSERT(!shared);
   1189 	}
   1190 
   1191 	void Image::release()
   1192 	{
   1193 		int refs = dereference();
   1194 
   1195 		if(refs > 0)
   1196 		{
   1197 			if(parentTexture)
   1198 			{
   1199 				parentTexture->sweep();
   1200 			}
   1201 		}
   1202 		else
   1203 		{
   1204 			delete this;
   1205 		}
   1206 	}
   1207 
   1208 	void Image::unbind(const egl::Texture *parent)
   1209 	{
   1210 		if(parentTexture == parent)
   1211 		{
   1212 			parentTexture = nullptr;
   1213 		}
   1214 
   1215 		release();
   1216 	}
   1217 
   1218 	bool Image::isChildOf(const egl::Texture *parent) const
   1219 	{
   1220 		return parentTexture == parent;
   1221 	}
   1222 
   1223 	void Image::loadImageData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const UnpackInfo& unpackInfo, const void *input)
   1224 	{
   1225 		GLsizei inputWidth = (unpackInfo.rowLength == 0) ? width : unpackInfo.rowLength;
   1226 		GLsizei inputPitch = ComputePitch(inputWidth, format, type, unpackInfo.alignment);
   1227 		GLsizei inputHeight = (unpackInfo.imageHeight == 0) ? height : unpackInfo.imageHeight;
   1228 		input = ((char*)input) + ComputePackingOffset(format, type, inputWidth, inputHeight, unpackInfo.alignment, unpackInfo.skipImages, unpackInfo.skipRows, unpackInfo.skipPixels);
   1229 		sw::Format selectedInternalFormat = SelectInternalFormat(format, type);
   1230 		if(selectedInternalFormat == sw::FORMAT_NULL)
   1231 		{
   1232 			return;
   1233 		}
   1234 
   1235 		if(selectedInternalFormat == internalFormat)
   1236 		{
   1237 			void *buffer = lock(0, 0, sw::LOCK_WRITEONLY);
   1238 
   1239 			if(buffer)
   1240 			{
   1241 				switch(type)
   1242 				{
   1243 				case GL_BYTE:
   1244 					switch(format)
   1245 					{
   1246 					case GL_R8:
   1247 					case GL_R8I:
   1248 					case GL_R8_SNORM:
   1249 					case GL_RED:
   1250 					case GL_RED_INTEGER:
   1251 					case GL_ALPHA:
   1252 					case GL_ALPHA8_EXT:
   1253 					case GL_LUMINANCE:
   1254 					case GL_LUMINANCE8_EXT:
   1255 						LoadImageData<Bytes_1>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1256 						break;
   1257 					case GL_RG8:
   1258 					case GL_RG8I:
   1259 					case GL_RG8_SNORM:
   1260 					case GL_RG:
   1261 					case GL_RG_INTEGER:
   1262 					case GL_LUMINANCE_ALPHA:
   1263 					case GL_LUMINANCE8_ALPHA8_EXT:
   1264 						LoadImageData<Bytes_2>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1265 						break;
   1266 					case GL_RGB8:
   1267 					case GL_RGB8I:
   1268 					case GL_RGB8_SNORM:
   1269 					case GL_RGB:
   1270 					case GL_RGB_INTEGER:
   1271 						LoadImageData<ByteRGB>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1272 						break;
   1273 					case GL_RGBA8:
   1274 					case GL_RGBA8I:
   1275 					case GL_RGBA8_SNORM:
   1276 					case GL_RGBA:
   1277 					case GL_RGBA_INTEGER:
   1278 					case GL_BGRA_EXT:
   1279 					case GL_BGRA8_EXT:
   1280 						LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1281 						break;
   1282 					default: UNREACHABLE(format);
   1283 					}
   1284 					break;
   1285 				case GL_UNSIGNED_BYTE:
   1286 					switch(format)
   1287 					{
   1288 					case GL_R8:
   1289 					case GL_R8UI:
   1290 					case GL_R8_SNORM:
   1291 					case GL_RED:
   1292 					case GL_RED_INTEGER:
   1293 					case GL_ALPHA:
   1294 					case GL_ALPHA8_EXT:
   1295 					case GL_LUMINANCE:
   1296 					case GL_LUMINANCE8_EXT:
   1297 						LoadImageData<Bytes_1>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1298 						break;
   1299 					case GL_RG8:
   1300 					case GL_RG8UI:
   1301 					case GL_RG8_SNORM:
   1302 					case GL_RG:
   1303 					case GL_RG_INTEGER:
   1304 					case GL_LUMINANCE_ALPHA:
   1305 					case GL_LUMINANCE8_ALPHA8_EXT:
   1306 						LoadImageData<Bytes_2>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1307 						break;
   1308 					case GL_RGB8:
   1309 					case GL_RGB8UI:
   1310 					case GL_RGB8_SNORM:
   1311 					case GL_RGB:
   1312 					case GL_RGB_INTEGER:
   1313 						LoadImageData<UByteRGB>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1314 						break;
   1315 					case GL_RGBA8:
   1316 					case GL_RGBA8UI:
   1317 					case GL_RGBA8_SNORM:
   1318 					case GL_RGBA:
   1319 					case GL_RGBA_INTEGER:
   1320 					case GL_BGRA_EXT:
   1321 					case GL_BGRA8_EXT:
   1322 						LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1323 						break;
   1324 					case GL_SRGB8:
   1325 						LoadImageData<SRGB>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1326 						break;
   1327 					case GL_SRGB8_ALPHA8:
   1328 						LoadImageData<SRGBA>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1329 						break;
   1330 					default: UNREACHABLE(format);
   1331 					}
   1332 					break;
   1333 				case GL_UNSIGNED_SHORT_5_6_5:
   1334 					switch(format)
   1335 					{
   1336 					case GL_RGB565:
   1337 					case GL_RGB:
   1338 						LoadImageData<RGB565>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1339 						break;
   1340 					default: UNREACHABLE(format);
   1341 					}
   1342 					break;
   1343 				case GL_UNSIGNED_SHORT_4_4_4_4:
   1344 					switch(format)
   1345 					{
   1346 					case GL_RGBA4:
   1347 					case GL_RGBA:
   1348 						LoadImageData<RGBA4444>(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_SHORT_5_5_5_1:
   1354 					switch(format)
   1355 					{
   1356 					case GL_RGB5_A1:
   1357 					case GL_RGBA:
   1358 						LoadImageData<RGBA5551>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1359 						break;
   1360 					default: UNREACHABLE(format);
   1361 					}
   1362 					break;
   1363 				case GL_UNSIGNED_INT_10F_11F_11F_REV:
   1364 					switch(format)
   1365 					{
   1366 					case GL_R11F_G11F_B10F:
   1367 					case GL_RGB:
   1368 						LoadImageData<R11G11B10F>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1369 						break;
   1370 					default: UNREACHABLE(format);
   1371 					}
   1372 					break;
   1373 				case GL_UNSIGNED_INT_5_9_9_9_REV:
   1374 					switch(format)
   1375 					{
   1376 					case GL_RGB9_E5:
   1377 					case GL_RGB:
   1378 						LoadImageData<RGB9E5>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1379 						break;
   1380 					default: UNREACHABLE(format);
   1381 					}
   1382 					break;
   1383 				case GL_UNSIGNED_INT_2_10_10_10_REV:
   1384 					switch(format)
   1385 					{
   1386 					case GL_RGB10_A2UI:
   1387 						LoadImageData<RGB10A2UI>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1388 						break;
   1389 					case GL_RGB10_A2:
   1390 					case GL_RGBA:
   1391 					case GL_RGBA_INTEGER:
   1392 						LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1393 						break;
   1394 					default: UNREACHABLE(format);
   1395 					}
   1396 					break;
   1397 				case GL_FLOAT:
   1398 					switch(format)
   1399 					{
   1400 					// float textures are converted to RGBA, not BGRA
   1401 					case GL_ALPHA:
   1402 					case GL_ALPHA32F_EXT:
   1403 						LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1404 						break;
   1405 					case GL_LUMINANCE:
   1406 					case GL_LUMINANCE32F_EXT:
   1407 						LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1408 						break;
   1409 					case GL_LUMINANCE_ALPHA:
   1410 					case GL_LUMINANCE_ALPHA32F_EXT:
   1411 						LoadImageData<Bytes_8>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1412 						break;
   1413 					case GL_RED:
   1414 					case GL_R32F:
   1415 						LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1416 						break;
   1417 					case GL_RG:
   1418 					case GL_RG32F:
   1419 						LoadImageData<Bytes_8>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1420 						break;
   1421 					case GL_RGB:
   1422 					case GL_RGB32F:
   1423 						LoadImageData<FloatRGB>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1424 						break;
   1425 					case GL_RGBA:
   1426 					case GL_RGBA32F:
   1427 						LoadImageData<Bytes_16>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1428 						break;
   1429 					case GL_DEPTH_COMPONENT:
   1430 					case GL_DEPTH_COMPONENT32F:
   1431 						LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1432 						break;
   1433 					default: UNREACHABLE(format);
   1434 					}
   1435 					break;
   1436 				case GL_HALF_FLOAT:
   1437 				case GL_HALF_FLOAT_OES:
   1438 					switch(format)
   1439 					{
   1440 					case GL_ALPHA:
   1441 					case GL_ALPHA16F_EXT:
   1442 						LoadImageData<Bytes_2>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1443 						break;
   1444 					case GL_LUMINANCE:
   1445 					case GL_LUMINANCE16F_EXT:
   1446 						LoadImageData<Bytes_2>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1447 						break;
   1448 					case GL_LUMINANCE_ALPHA:
   1449 					case GL_LUMINANCE_ALPHA16F_EXT:
   1450 						LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1451 						break;
   1452 					case GL_RED:
   1453 					case GL_R16F:
   1454 						LoadImageData<Bytes_2>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1455 						break;
   1456 					case GL_RG:
   1457 					case GL_RG16F:
   1458 						LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1459 						break;
   1460 					case GL_RGB:
   1461 					case GL_RGB16F:
   1462 						LoadImageData<HalfFloatRGB>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1463 						break;
   1464 					case GL_RGBA:
   1465 					case GL_RGBA16F:
   1466 						LoadImageData<Bytes_8>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1467 						break;
   1468 					default: UNREACHABLE(format);
   1469 					}
   1470 					break;
   1471 				case GL_SHORT:
   1472 					switch(format)
   1473 					{
   1474 					case GL_R16I:
   1475 					case GL_RED:
   1476 					case GL_RED_INTEGER:
   1477 					case GL_ALPHA:
   1478 					case GL_LUMINANCE:
   1479 						LoadImageData<Bytes_2>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1480 						break;
   1481 					case GL_RG16I:
   1482 					case GL_RG:
   1483 					case GL_RG_INTEGER:
   1484 					case GL_LUMINANCE_ALPHA:
   1485 						LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1486 						break;
   1487 					case GL_RGB16I:
   1488 					case GL_RGB:
   1489 					case GL_RGB_INTEGER:
   1490 						LoadImageData<ShortRGB>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1491 						break;
   1492 					case GL_RGBA16I:
   1493 					case GL_RGBA:
   1494 					case GL_RGBA_INTEGER:
   1495 					case GL_BGRA_EXT:
   1496 					case GL_BGRA8_EXT:
   1497 						LoadImageData<Bytes_8>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1498 						break;
   1499 					default: UNREACHABLE(format);
   1500 					}
   1501 					break;
   1502 				case GL_UNSIGNED_SHORT:
   1503 					switch(format)
   1504 					{
   1505 					case GL_R16UI:
   1506 					case GL_RED:
   1507 					case GL_RED_INTEGER:
   1508 					case GL_ALPHA:
   1509 					case GL_LUMINANCE:
   1510 						LoadImageData<Bytes_2>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1511 						break;
   1512 					case GL_RG16UI:
   1513 					case GL_RG:
   1514 					case GL_RG_INTEGER:
   1515 					case GL_LUMINANCE_ALPHA:
   1516 						LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1517 						break;
   1518 					case GL_RGB16UI:
   1519 					case GL_RGB:
   1520 					case GL_RGB_INTEGER:
   1521 						LoadImageData<UShortRGB>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1522 						break;
   1523 					case GL_RGBA16UI:
   1524 					case GL_RGBA:
   1525 					case GL_RGBA_INTEGER:
   1526 					case GL_BGRA_EXT:
   1527 					case GL_BGRA8_EXT:
   1528 						LoadImageData<Bytes_8>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1529 						break;
   1530 					case GL_DEPTH_COMPONENT:
   1531 					case GL_DEPTH_COMPONENT16:
   1532 						LoadImageData<D16>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1533 						break;
   1534 					default: UNREACHABLE(format);
   1535 					}
   1536 					break;
   1537 				case GL_INT:
   1538 					switch(format)
   1539 					{
   1540 					case GL_R32I:
   1541 					case GL_RED:
   1542 					case GL_RED_INTEGER:
   1543 					case GL_ALPHA:
   1544 					case GL_LUMINANCE:
   1545 						LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1546 						break;
   1547 					case GL_RG32I:
   1548 					case GL_RG:
   1549 					case GL_RG_INTEGER:
   1550 					case GL_LUMINANCE_ALPHA:
   1551 						LoadImageData<Bytes_8>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1552 						break;
   1553 					case GL_RGB32I:
   1554 					case GL_RGB:
   1555 					case GL_RGB_INTEGER:
   1556 						LoadImageData<IntRGB>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1557 						break;
   1558 					case GL_RGBA32I:
   1559 					case GL_RGBA:
   1560 					case GL_RGBA_INTEGER:
   1561 					case GL_BGRA_EXT:
   1562 					case GL_BGRA8_EXT:
   1563 						LoadImageData<Bytes_16>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1564 						break;
   1565 					default: UNREACHABLE(format);
   1566 					}
   1567 					break;
   1568 				case GL_UNSIGNED_INT:
   1569 					switch(format)
   1570 					{
   1571 					case GL_R32UI:
   1572 					case GL_RED:
   1573 					case GL_RED_INTEGER:
   1574 					case GL_ALPHA:
   1575 					case GL_LUMINANCE:
   1576 						LoadImageData<Bytes_4>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1577 						break;
   1578 					case GL_RG32UI:
   1579 					case GL_RG:
   1580 					case GL_RG_INTEGER:
   1581 					case GL_LUMINANCE_ALPHA:
   1582 						LoadImageData<Bytes_8>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1583 						break;
   1584 					case GL_RGB32UI:
   1585 					case GL_RGB:
   1586 					case GL_RGB_INTEGER:
   1587 						LoadImageData<UIntRGB>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1588 						break;
   1589 					case GL_RGBA32UI:
   1590 					case GL_RGBA:
   1591 					case GL_RGBA_INTEGER:
   1592 					case GL_BGRA_EXT:
   1593 					case GL_BGRA8_EXT:
   1594 						LoadImageData<Bytes_16>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1595 						break;
   1596 					case GL_DEPTH_COMPONENT16:
   1597 					case GL_DEPTH_COMPONENT24:
   1598 					case GL_DEPTH_COMPONENT32_OES:
   1599 					case GL_DEPTH_COMPONENT:
   1600 						LoadImageData<D32>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1601 						break;
   1602 					default: UNREACHABLE(format);
   1603 					}
   1604 					break;
   1605 				case GL_UNSIGNED_INT_24_8_OES:
   1606 					loadD24S8ImageData(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, input, buffer);
   1607 					break;
   1608 				case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
   1609 					loadD32FS8ImageData(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, input, buffer);
   1610 					break;
   1611 				default: UNREACHABLE(type);
   1612 				}
   1613 			}
   1614 
   1615 			unlock();
   1616 		}
   1617 		else
   1618 		{
   1619 			sw::Surface source(width, height, depth, ConvertFormatType(format, type), const_cast<void*>(input), inputPitch, inputPitch * inputHeight);
   1620 			sw::Rect sourceRect(0, 0, width, height);
   1621 			sw::Rect destRect(xoffset, yoffset, xoffset + width, yoffset + height);
   1622 			sw::blitter.blit(&source, sourceRect, this, destRect, false);
   1623 		}
   1624 	}
   1625 
   1626 	void Image::loadD24S8ImageData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, int inputPitch, int inputHeight, const void *input, void *buffer)
   1627 	{
   1628 		LoadImageData<D24>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1629 
   1630 		unsigned char *stencil = reinterpret_cast<unsigned char*>(lockStencil(0, sw::PUBLIC));
   1631 
   1632 		if(stencil)
   1633 		{
   1634 			LoadImageData<S8>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getStencilPitchB(), getHeight(), input, stencil);
   1635 
   1636 			unlockStencil();
   1637 		}
   1638 	}
   1639 
   1640 	void Image::loadD32FS8ImageData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, int inputPitch, int inputHeight, const void *input, void *buffer)
   1641 	{
   1642 		LoadImageData<D32F>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
   1643 
   1644 		unsigned char *stencil = reinterpret_cast<unsigned char*>(lockStencil(0, sw::PUBLIC));
   1645 
   1646 		if(stencil)
   1647 		{
   1648 			LoadImageData<S24_8>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getStencilPitchB(), getHeight(), input, stencil);
   1649 
   1650 			unlockStencil();
   1651 		}
   1652 	}
   1653 
   1654 	void Image::loadCompressedData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLsizei imageSize, const void *pixels)
   1655 	{
   1656 		if(zoffset != 0 || depth != 1)
   1657 		{
   1658 			UNIMPLEMENTED();   // FIXME
   1659 		}
   1660 
   1661 		int inputPitch = ComputeCompressedPitch(width, format);
   1662 		int rows = imageSize / inputPitch;
   1663 		void *buffer = lock(xoffset, yoffset, sw::LOCK_WRITEONLY);
   1664 
   1665 		if(buffer)
   1666 		{
   1667 			for(int i = 0; i < rows; i++)
   1668 			{
   1669 				memcpy((void*)((GLbyte*)buffer + i * getPitch()), (void*)((GLbyte*)pixels + i * inputPitch), inputPitch);
   1670 			}
   1671 		}
   1672 
   1673 		unlock();
   1674 	}
   1675 }
   1676