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