mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-06 11:31:59 +08:00
Move logic of flipY and premultiplyAlpha from jsb_opengl_manual.cpp to CCGLUtils.cpp. (#1249)
* Native Renderer supports GL_UNPACK_FLIP_Y_WEBGL & GL_UNPACK_PREMULTIPLY_ALPHA_WEBGL. And adds ccFlipYOrPremultiptyAlphaIfNeeded, ccIsUnpackFlipY, ccIsPremultiplyAlpha, ccPixelStorei GL utils. * seval_to_TextureSubImageOption: uint16_t* -> uint32_t* since width * height * 4 may bigger than 65536 bytes. * Move logic of flipY and premultiplyAlpha from jsb_opengl_manual.cpp to CCGLUtils.cpp. * Reverts.
This commit is contained in:
@@ -34,8 +34,9 @@ NS_CC_BEGIN
|
||||
#define MAX_ATTRIBUTE_UNIT 16
|
||||
|
||||
//FIXME: Consider to use variable to enable/disable cache state since using macro will not be able to close it if there're serious bugs.
|
||||
// #undef CC_ENABLE_GL_STATE_CACHE
|
||||
// #define CC_ENABLE_GL_STATE_CACHE 0
|
||||
//#undef CC_ENABLE_GL_STATE_CACHE
|
||||
//#define CC_ENABLE_GL_STATE_CACHE 0
|
||||
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -45,8 +46,14 @@ namespace
|
||||
|
||||
uint32_t __enabledVertexAttribArrayFlag = 0;
|
||||
VertexAttributePointerInfo __enabledVertexAttribArrayInfo[MAX_ATTRIBUTE_UNIT];
|
||||
|
||||
GLint _currentUnpackAlignment = -1;
|
||||
|
||||
bool __unpackFlipY = false;
|
||||
bool __premultiplyAlpha = false;
|
||||
}
|
||||
|
||||
|
||||
//FIXME: need to consider invoking this after restarting game.
|
||||
void ccInvalidateStateCache()
|
||||
{
|
||||
@@ -57,6 +64,10 @@ void ccInvalidateStateCache()
|
||||
__enabledVertexAttribArrayFlag = 0;
|
||||
for (int i = 0; i < MAX_ATTRIBUTE_UNIT; ++i)
|
||||
__enabledVertexAttribArrayInfo[i] = VertexAttributePointerInfo();
|
||||
|
||||
_currentUnpackAlignment = -1;
|
||||
__unpackFlipY = false;
|
||||
__premultiplyAlpha = false;
|
||||
}
|
||||
|
||||
/****************************************************************************************
|
||||
@@ -246,4 +257,187 @@ void ccScissor(GLint x, GLint y, GLsizei width, GLsizei height)
|
||||
glScissor(x, y, width, height);
|
||||
}
|
||||
|
||||
//FIXME:cjh: ONLY SUPPORT RGBA format now.
|
||||
static void flipPixelsY(GLubyte *pixels, int bytesPerRow, int rows)
|
||||
{
|
||||
if( !pixels ) { return; }
|
||||
|
||||
GLuint middle = rows/2;
|
||||
GLuint intsPerRow = bytesPerRow / sizeof(GLuint);
|
||||
GLuint remainingBytes = bytesPerRow - intsPerRow * sizeof(GLuint);
|
||||
|
||||
for( GLuint rowTop = 0, rowBottom = rows-1; rowTop < middle; rowTop++, rowBottom-- ) {
|
||||
|
||||
// Swap bytes in packs of sizeof(GLuint) bytes
|
||||
GLuint *iTop = (GLuint *)(pixels + rowTop * bytesPerRow);
|
||||
GLuint *iBottom = (GLuint *)(pixels + rowBottom * bytesPerRow);
|
||||
|
||||
GLuint itmp;
|
||||
GLint n = intsPerRow;
|
||||
do {
|
||||
itmp = *iTop;
|
||||
*iTop++ = *iBottom;
|
||||
*iBottom++ = itmp;
|
||||
} while(--n > 0);
|
||||
|
||||
// Swap the remaining bytes
|
||||
GLubyte *bTop = (GLubyte *)iTop;
|
||||
GLubyte *bBottom = (GLubyte *)iBottom;
|
||||
|
||||
GLubyte btmp;
|
||||
switch( remainingBytes ) {
|
||||
case 3: btmp = *bTop; *bTop++ = *bBottom; *bBottom++ = btmp;
|
||||
case 2: btmp = *bTop; *bTop++ = *bBottom; *bBottom++ = btmp;
|
||||
case 1: btmp = *bTop; *bTop = *bBottom; *bBottom = btmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void flipPixelsYByFormat(GLubyte *pixels, GLenum format, uint32_t width, uint32_t height, uint32_t expectedTotalBytes)
|
||||
{
|
||||
bool isSupportFlipY = true;
|
||||
GLsizei bytesPerRow = 0;
|
||||
switch (format) {
|
||||
case GL_RGBA:
|
||||
bytesPerRow = width * 4;
|
||||
break;
|
||||
case GL_RGB:
|
||||
bytesPerRow = width * 3;
|
||||
break;
|
||||
case GL_LUMINANCE_ALPHA:
|
||||
bytesPerRow = width * 2;
|
||||
break;
|
||||
case GL_LUMINANCE:
|
||||
bytesPerRow = width;
|
||||
break;
|
||||
default:
|
||||
isSupportFlipY = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (isSupportFlipY)
|
||||
{
|
||||
assert(expectedTotalBytes == bytesPerRow * height);
|
||||
flipPixelsY((GLubyte*)pixels, bytesPerRow, height);
|
||||
}
|
||||
else
|
||||
{
|
||||
CCLOGERROR("flipPixelsYByFormat: format: 0x%X doesn't support upackFlipY!\n", format);
|
||||
}
|
||||
}
|
||||
|
||||
// Lookup tables for fast [un]premultiplied alpha color values
|
||||
// From https://bugzilla.mozilla.org/show_bug.cgi?id=662130
|
||||
static GLubyte* __premultiplyTable = nullptr;
|
||||
|
||||
static const GLubyte* premultiplyTable()
|
||||
{
|
||||
if( !__premultiplyTable ) {
|
||||
__premultiplyTable = (GLubyte*)malloc(256*256);
|
||||
|
||||
unsigned char *data = __premultiplyTable;
|
||||
for( int a = 0; a <= 255; a++ ) {
|
||||
for( int c = 0; c <= 255; c++ ) {
|
||||
data[a*256+c] = (a * c + 254) / 255;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return __premultiplyTable;
|
||||
}
|
||||
|
||||
void premultiplyPixels(const GLubyte *inPixels, GLubyte *outPixels, GLenum format, uint32_t width, uint32_t height, uint32_t expectedTotalBytes)
|
||||
{
|
||||
const GLubyte *table = premultiplyTable();
|
||||
int byteLength = 0;
|
||||
if( format == GL_RGBA )
|
||||
{
|
||||
byteLength = width * height * 4;
|
||||
assert(byteLength == expectedTotalBytes);
|
||||
for( int i = 0; i < byteLength; i += 4 ) {
|
||||
unsigned short a = inPixels[i+3] * 256;
|
||||
outPixels[i+0] = table[ a + inPixels[i+0] ];
|
||||
outPixels[i+1] = table[ a + inPixels[i+1] ];
|
||||
outPixels[i+2] = table[ a + inPixels[i+2] ];
|
||||
outPixels[i+3] = inPixels[i+3];
|
||||
}
|
||||
}
|
||||
else if ( format == GL_LUMINANCE_ALPHA )
|
||||
{
|
||||
byteLength = width * height * 2;
|
||||
assert(byteLength == expectedTotalBytes);
|
||||
for( int i = 0; i < byteLength; i += 2 ) {
|
||||
unsigned short a = inPixels[i+1] * 256;
|
||||
outPixels[i+0] = table[ a + inPixels[i+0] ];
|
||||
outPixels[i+1] = inPixels[i+1];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
CCLOGERROR("premultiplyPixels: format: 0x%X doesn't support upackFlipY!\n", format);
|
||||
}
|
||||
}
|
||||
|
||||
bool ccIsUnpackFlipY()
|
||||
{
|
||||
return __unpackFlipY;
|
||||
}
|
||||
|
||||
bool ccIsPremultiplyAlpha()
|
||||
{
|
||||
return __premultiplyAlpha;
|
||||
}
|
||||
|
||||
void ccPixelStorei(GLenum pname, GLint param)
|
||||
{
|
||||
if (pname == GL_UNPACK_FLIP_Y_WEBGL)
|
||||
{
|
||||
__unpackFlipY = param == 0 ? false : true;
|
||||
return;
|
||||
}
|
||||
else if (pname == GL_UNPACK_PREMULTIPLY_ALPHA_WEBGL)
|
||||
{
|
||||
__premultiplyAlpha = param == 0 ? false : true;
|
||||
return;
|
||||
}
|
||||
else if (pname == GL_UNPACK_COLORSPACE_CONVERSION_WEBGL)
|
||||
{
|
||||
CCLOGERROR("Warning: UNPACK_COLORSPACE_CONVERSION_WEBGL is unsupported\n");
|
||||
return;
|
||||
}
|
||||
else if (pname == GL_UNPACK_ALIGNMENT)
|
||||
{
|
||||
#if CC_ENABLE_GL_STATE_CACHE
|
||||
if (_currentUnpackAlignment != param)
|
||||
{
|
||||
glPixelStorei(pname, param);
|
||||
_currentUnpackAlignment = param;
|
||||
CCLOG("pixel store changed: %d", param);
|
||||
}
|
||||
#else
|
||||
glPixelStorei(pname, param);
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
glPixelStorei(pname, param);
|
||||
}
|
||||
}
|
||||
|
||||
void ccFlipYOrPremultiptyAlphaIfNeeded(GLenum format, GLsizei width, GLsizei height, uint32_t pixelBytes, GLvoid* pixels)
|
||||
{
|
||||
if (pixels != nullptr)
|
||||
{
|
||||
if (__unpackFlipY)
|
||||
{
|
||||
flipPixelsYByFormat((GLubyte*)pixels, format, width, height, pixelBytes);
|
||||
}
|
||||
|
||||
if (__premultiplyAlpha)
|
||||
{
|
||||
premultiplyPixels((GLubyte*)pixels, (GLubyte*)pixels, format, width, height, pixelBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
NS_CC_END
|
||||
|
||||
@@ -69,4 +69,11 @@ void ccDisableVertexAttribArray(GLuint index);
|
||||
void ccVertexAttribPointer(GLuint, GLint, GLenum, GLboolean, GLsizei, const GLvoid*);
|
||||
const VertexAttributePointerInfo* getVertexAttribPointerInfo(GLuint index);
|
||||
|
||||
// Converts pixel if unpackFlipY or premultiplyAlpha is true.
|
||||
void ccFlipYOrPremultiptyAlphaIfNeeded(GLenum format, GLsizei width, GLsizei height, uint32_t pixelBytes, GLvoid* pixels);
|
||||
|
||||
bool ccIsUnpackFlipY();
|
||||
bool ccIsPremultiplyAlpha();
|
||||
void ccPixelStorei(GLenum pname, GLint param);
|
||||
|
||||
NS_CC_END
|
||||
|
||||
@@ -340,6 +340,17 @@ void RenderTexture::initTexture()
|
||||
glBindTexture(GL_TEXTURE_2D, _texture);
|
||||
unsigned char* texData = (unsigned char*)malloc(_width * _height * 3);
|
||||
memset(texData, 0, _width * _height * 3);
|
||||
|
||||
GLint alignment = 1;
|
||||
int bytesPerRow = _width * 3;
|
||||
if (bytesPerRow % 8 == 0)
|
||||
alignment = 8;
|
||||
else if (bytesPerRow % 4 == 0)
|
||||
alignment = 4;
|
||||
else if (bytesPerRow % 2 == 0)
|
||||
alignment = 2;
|
||||
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
|
||||
glTexImage2D(GL_TEXTURE_2D,
|
||||
0,
|
||||
GL_RGB,
|
||||
|
||||
Reference in New Issue
Block a user