1 // SwiftShader Software Renderer
3 // Copyright(c) 2005-2013 TransGaming Inc.
5 // All rights reserved. No part of this software may be copied, distributed, transmitted,
6 // transcribed, stored in a retrieval system, translated into any human or computer
7 // language by any means, or disclosed to third parties without the explicit written
8 // agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9 // or implied, including but not limited to any patent rights, are granted to you.
15 #include "utilities.h"
16 #include "../common/debug.h"
17 #include "Common/Thread.hpp"
19 #include <GLES2/gl2ext.h>
23 static sw::Resource *getParentResource(Texture *texture)
27 return texture->getResource();
33 Image::Image(Texture *parentTexture, GLsizei width, GLsizei height, GLenum format, GLenum type)
34 : parentTexture(parentTexture)
35 , egl::Image(getParentResource(parentTexture), width, height, format, type, selectInternalFormat(format, type))
40 Image::Image(Texture *parentTexture, GLsizei width, GLsizei height, sw::Format internalFormat, int multiSampleDepth, bool lockable, bool renderTarget)
41 : parentTexture(parentTexture)
42 , egl::Image(getParentResource(parentTexture), width, height, multiSampleDepth, internalFormat, lockable, renderTarget)
49 ASSERT(referenceCount == 0);
56 parentTexture->addRef();
59 sw::atomicIncrement(&referenceCount);
66 parentTexture->release();
69 if(referenceCount > 0)
71 sw::atomicDecrement(&referenceCount);
74 if(referenceCount == 0)
87 sw::Format Image::selectInternalFormat(GLenum format, GLenum type)
90 if(format == GL_COMPRESSED_RGB_S3TC_DXT1_EXT ||
91 format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT)
93 return sw::FORMAT_DXT1;
95 else if(format == GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE)
97 return sw::FORMAT_DXT3;
99 else if(format == GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE)
101 return sw::FORMAT_DXT5;
107 return sw::FORMAT_A32B32G32R32F;
109 else if(type == GL_HALF_FLOAT_OES)
111 return sw::FORMAT_A16B16G16R16F;
113 else if(type == GL_UNSIGNED_BYTE)
115 if(format == GL_LUMINANCE)
117 return sw::FORMAT_L8;
119 else if(format == GL_LUMINANCE_ALPHA)
121 return sw::FORMAT_A8L8;
123 else if(format == GL_RGBA || format == GL_BGRA_EXT)
125 return sw::FORMAT_A8R8G8B8;
127 else if(format == GL_RGB)
129 return sw::FORMAT_X8R8G8B8;
131 else if(format == GL_ALPHA)
133 return sw::FORMAT_A8;
137 else if(type == GL_UNSIGNED_SHORT || type == GL_UNSIGNED_INT)
139 if(format == GL_DEPTH_COMPONENT)
141 return sw::FORMAT_D32FS8_TEXTURE;
145 else if(type == GL_UNSIGNED_INT_24_8_OES)
147 if(format == GL_DEPTH_STENCIL_OES)
149 return sw::FORMAT_D32FS8_TEXTURE;
153 else if(type == GL_UNSIGNED_SHORT_4_4_4_4)
155 return sw::FORMAT_A8R8G8B8;
157 else if(type == GL_UNSIGNED_SHORT_5_5_5_1)
159 return sw::FORMAT_A8R8G8B8;
161 else if(type == GL_UNSIGNED_SHORT_5_6_5)
163 return sw::FORMAT_X8R8G8B8;
167 return sw::FORMAT_A8R8G8B8;
170 void Image::loadImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *input)
172 GLsizei inputPitch = ComputePitch(width, format, type, unpackAlignment);
173 void *buffer = lock(0, 0, sw::LOCK_WRITEONLY);
179 case GL_UNSIGNED_BYTE:
183 loadAlphaImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
186 loadLuminanceImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
188 case GL_LUMINANCE_ALPHA:
189 loadLuminanceAlphaImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
192 loadRGBUByteImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
195 loadRGBAUByteImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
198 loadBGRAImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
200 default: UNREACHABLE();
203 case GL_UNSIGNED_SHORT_5_6_5:
207 loadRGB565ImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
209 default: UNREACHABLE();
212 case GL_UNSIGNED_SHORT_4_4_4_4:
216 loadRGBA4444ImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
218 default: UNREACHABLE();
221 case GL_UNSIGNED_SHORT_5_5_5_1:
225 loadRGBA5551ImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
227 default: UNREACHABLE();
233 // float textures are converted to RGBA, not BGRA
235 loadAlphaFloatImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
238 loadLuminanceFloatImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
240 case GL_LUMINANCE_ALPHA:
241 loadLuminanceAlphaFloatImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
244 loadRGBFloatImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
247 loadRGBAFloatImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
249 default: UNREACHABLE();
252 case GL_HALF_FLOAT_OES:
255 // float textures are converted to RGBA, not BGRA
257 loadAlphaHalfFloatImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
260 loadLuminanceHalfFloatImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
262 case GL_LUMINANCE_ALPHA:
263 loadLuminanceAlphaHalfFloatImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
266 loadRGBHalfFloatImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
269 loadRGBAHalfFloatImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
271 default: UNREACHABLE();
274 case GL_UNSIGNED_SHORT:
275 loadD16ImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
277 case GL_UNSIGNED_INT:
278 loadD32ImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
280 case GL_UNSIGNED_INT_24_8_OES:
281 loadD24S8ImageData(xoffset, yoffset, width, height, inputPitch, input, buffer);
283 default: UNREACHABLE();
290 void Image::loadAlphaImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer) const
292 for(int y = 0; y < height; y++)
294 const unsigned char *source = static_cast<const unsigned char*>(input) + y * inputPitch;
295 unsigned char *dest = static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset;
297 memcpy(dest, source, width);
301 void Image::loadAlphaFloatImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer) const
303 for(int y = 0; y < height; y++)
305 const float *source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
306 float *dest = reinterpret_cast<float*>(static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset * 16);
308 for(int x = 0; x < width; x++)
313 dest[4 * x + 3] = source[x];
318 void Image::loadAlphaHalfFloatImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer) const
320 for(int y = 0; y < height; y++)
322 const unsigned short *source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
323 unsigned short *dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset * 8);
325 for(int x = 0; x < width; x++)
330 dest[4 * x + 3] = source[x];
335 void Image::loadLuminanceImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer) const
337 for(int y = 0; y < height; y++)
339 const unsigned char *source = static_cast<const unsigned char*>(input) + y * inputPitch;
340 unsigned char *dest = static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset;
342 memcpy(dest, source, width);
346 void Image::loadLuminanceFloatImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer) const
348 for(int y = 0; y < height; y++)
350 const float *source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
351 float *dest = reinterpret_cast<float*>(static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset * 16);
353 for(int x = 0; x < width; x++)
355 dest[4 * x + 0] = source[x];
356 dest[4 * x + 1] = source[x];
357 dest[4 * x + 2] = source[x];
358 dest[4 * x + 3] = 1.0f;
363 void Image::loadLuminanceHalfFloatImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer) const
365 for(int y = 0; y < height; y++)
367 const unsigned short *source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
368 unsigned short *dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset * 8);
370 for(int x = 0; x < width; x++)
372 dest[4 * x + 0] = source[x];
373 dest[4 * x + 1] = source[x];
374 dest[4 * x + 2] = source[x];
375 dest[4 * x + 3] = 0x3C00; // SEEEEEMMMMMMMMMM, S = 0, E = 15, M = 0: 16bit flpt representation of 1
380 void Image::loadLuminanceAlphaImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer) const
382 for(int y = 0; y < height; y++)
384 const unsigned char *source = static_cast<const unsigned char*>(input) + y * inputPitch;
385 unsigned char *dest = static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset * 2;
387 memcpy(dest, source, width * 2);
391 void Image::loadLuminanceAlphaFloatImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer) const
393 for(int y = 0; y < height; y++)
395 const float *source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
396 float *dest = reinterpret_cast<float*>(static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset * 16);
398 for(int x = 0; x < width; x++)
400 dest[4 * x + 0] = source[2*x+0];
401 dest[4 * x + 1] = source[2*x+0];
402 dest[4 * x + 2] = source[2*x+0];
403 dest[4 * x + 3] = source[2*x+1];
408 void Image::loadLuminanceAlphaHalfFloatImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer) const
410 for(int y = 0; y < height; y++)
412 const unsigned short *source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
413 unsigned short *dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset * 8);
415 for(int x = 0; x < width; x++)
417 dest[4 * x + 0] = source[2*x+0];
418 dest[4 * x + 1] = source[2*x+0];
419 dest[4 * x + 2] = source[2*x+0];
420 dest[4 * x + 3] = source[2*x+1];
425 void Image::loadRGBUByteImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer) const
427 for(int y = 0; y < height; y++)
429 const unsigned char *source = static_cast<const unsigned char*>(input) + y * inputPitch;
430 unsigned char *dest = static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset * 4;
432 for(int x = 0; x < width; x++)
434 dest[4 * x + 0] = source[x * 3 + 2];
435 dest[4 * x + 1] = source[x * 3 + 1];
436 dest[4 * x + 2] = source[x * 3 + 0];
437 dest[4 * x + 3] = 0xFF;
442 void Image::loadRGB565ImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer) const
444 for(int y = 0; y < height; y++)
446 const unsigned short *source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
447 unsigned char *dest = static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset * 4;
449 for(int x = 0; x < width; x++)
451 unsigned short rgba = source[x];
452 dest[4 * x + 0] = ((rgba & 0x001F) << 3) | ((rgba & 0x001F) >> 2);
453 dest[4 * x + 1] = ((rgba & 0x07E0) >> 3) | ((rgba & 0x07E0) >> 9);
454 dest[4 * x + 2] = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13);
455 dest[4 * x + 3] = 0xFF;
460 void Image::loadRGBFloatImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer) const
462 for(int y = 0; y < height; y++)
464 const float *source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
465 float *dest = reinterpret_cast<float*>(static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset * 16);
467 for(int x = 0; x < width; x++)
469 dest[4 * x + 0] = source[x * 3 + 0];
470 dest[4 * x + 1] = source[x * 3 + 1];
471 dest[4 * x + 2] = source[x * 3 + 2];
472 dest[4 * x + 3] = 1.0f;
477 void Image::loadRGBHalfFloatImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer) const
479 for(int y = 0; y < height; y++)
481 const unsigned short *source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
482 unsigned short *dest = reinterpret_cast<unsigned short*>(static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset * 8);
484 for(int x = 0; x < width; x++)
486 dest[4 * x + 0] = source[x * 3 + 0];
487 dest[4 * x + 1] = source[x * 3 + 1];
488 dest[4 * x + 2] = source[x * 3 + 2];
489 dest[4 * x + 3] = 0x3C00; // SEEEEEMMMMMMMMMM, S = 0, E = 15, M = 0: 16bit flpt representation of 1
494 void Image::loadRGBAUByteImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer) const
496 for(int y = 0; y < height; y++)
498 const unsigned int *source = reinterpret_cast<const unsigned int*>(static_cast<const unsigned char*>(input) + y * inputPitch);
499 unsigned int *dest = reinterpret_cast<unsigned int*>(static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset * 4);
501 for(int x = 0; x < width; x++)
503 unsigned int rgba = source[x];
504 dest[x] = (rgba & 0xFF00FF00) | ((rgba << 16) & 0x00FF0000) | ((rgba >> 16) & 0x000000FF);
509 void Image::loadRGBA4444ImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer) const
511 for(int y = 0; y < height; y++)
513 const unsigned short *source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
514 unsigned char *dest = static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset * 4;
516 for(int x = 0; x < width; x++)
518 unsigned short rgba = source[x];
519 dest[4 * x + 0] = ((rgba & 0x00F0) << 0) | ((rgba & 0x00F0) >> 4);
520 dest[4 * x + 1] = ((rgba & 0x0F00) >> 4) | ((rgba & 0x0F00) >> 8);
521 dest[4 * x + 2] = ((rgba & 0xF000) >> 8) | ((rgba & 0xF000) >> 12);
522 dest[4 * x + 3] = ((rgba & 0x000F) << 4) | ((rgba & 0x000F) >> 0);
527 void Image::loadRGBA5551ImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer) const
529 for(int y = 0; y < height; y++)
531 const unsigned short *source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
532 unsigned char *dest = static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset * 4;
534 for(int x = 0; x < width; x++)
536 unsigned short rgba = source[x];
537 dest[4 * x + 0] = ((rgba & 0x003E) << 2) | ((rgba & 0x003E) >> 3);
538 dest[4 * x + 1] = ((rgba & 0x07C0) >> 3) | ((rgba & 0x07C0) >> 8);
539 dest[4 * x + 2] = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13);
540 dest[4 * x + 3] = (rgba & 0x0001) ? 0xFF : 0;
545 void Image::loadRGBAFloatImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer) const
547 for(int y = 0; y < height; y++)
549 const float *source = reinterpret_cast<const float*>(static_cast<const unsigned char*>(input) + y * inputPitch);
550 float *dest = reinterpret_cast<float*>(static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset * 16);
552 memcpy(dest, source, width * 16);
556 void Image::loadRGBAHalfFloatImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer) const
558 for(int y = 0; y < height; y++)
560 const unsigned char *source = static_cast<const unsigned char*>(input) + y * inputPitch;
561 unsigned char *dest = static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset * 8;
563 memcpy(dest, source, width * 8);
567 void Image::loadBGRAImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer) const
569 for(int y = 0; y < height; y++)
571 const unsigned char *source = static_cast<const unsigned char*>(input) + y * inputPitch;
572 unsigned char *dest = static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset * 4;
574 memcpy(dest, source, width*4);
578 void Image::loadD16ImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer) const
580 for(int y = 0; y < height; y++)
582 const unsigned short *source = reinterpret_cast<const unsigned short*>(static_cast<const unsigned char*>(input) + y * inputPitch);
583 float *dest = reinterpret_cast<float*>(static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset * 4);
585 for(int x = 0; x < width; x++)
587 dest[x] = (float)source[x] / 0xFFFF;
592 void Image::loadD32ImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer) const
594 for(int y = 0; y < height; y++)
596 const unsigned int *source = reinterpret_cast<const unsigned int*>(static_cast<const unsigned char*>(input) + y * inputPitch);
597 float *dest = reinterpret_cast<float*>(static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset * 4);
599 for(int x = 0; x < width; x++)
601 dest[x] = (float)source[x] / 0xFFFFFFFF;
606 void Image::loadD24S8ImageData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, int inputPitch, const void *input, void *buffer)
608 for(int y = 0; y < height; y++)
610 const unsigned int *source = reinterpret_cast<const unsigned int*>(static_cast<const unsigned char*>(input) + y * inputPitch);
611 float *dest = reinterpret_cast<float*>(static_cast<unsigned char*>(buffer) + (y + yoffset) * getPitch() + xoffset * 4);
613 for(int x = 0; x < width; x++)
615 dest[x] = (float)(source[x] & 0xFFFFFF00) / 0xFFFFFF00;
619 unsigned char *stencil = reinterpret_cast<unsigned char*>(lockStencil(0, sw::PUBLIC));
623 for(int y = 0; y < height; y++)
625 const unsigned int *source = reinterpret_cast<const unsigned int*>(static_cast<const unsigned char*>(input) + y * inputPitch);
626 unsigned char *dest = static_cast<unsigned char*>(stencil) + (y + yoffset) * getStencilPitchB() + xoffset;
628 for(int x = 0; x < width; x++)
630 dest[x] = static_cast<unsigned char>(source[x] & 0x000000FF); // FIXME: Quad layout
638 void Image::loadCompressedData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels)
640 int inputPitch = ComputeCompressedPitch(width, format);
641 int rows = imageSize / inputPitch;
642 void *buffer = lock(xoffset, yoffset, sw::LOCK_WRITEONLY);
646 for(int i = 0; i < rows; i++)
648 memcpy((void*)((GLbyte*)buffer + i * getPitch()), (void*)((GLbyte*)pixels + i * inputPitch), inputPitch);