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