8000 Fixing unpack with alignment by kan6868 · Pull Request #803 · coronalabs/corona · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fixing unpack with alignment #803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 52 additions & 3 deletions librtt/Renderer/Rtt_GLTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,49 @@ namespace Rtt

// ----------------------------------------------------------------------------

GLint CalculateOptimalAlignment(U32 width, GLenum format)
{
int bytesPerPixel = 4;

switch(format) {
case GL_ALPHA:
#if defined( Rtt_MetalANGLE)
case GL_RED_EXT:
#else
case GL_LUMINANCE:
#endif
bytesPerPixel = 1;
break;

case GL_RGB:
bytesPerPixel = 3;
break;

case GL_RGBA:
case GL_BGRA_EXT:
case GL_ABGR_EXT:
bytesPerPixel = 4;
break;

#ifdef Rtt_NXS_ENV
case GL_LUMINANCE_ALPHA:
bytesPerPixel = 2;
break;
#endif
default: // Default
bytesPerPixel = 4;
break;
}

const U32 rowBytes = width * bytesPerPixel;

if(rowBytes % 8 == 0) return 8;
if(rowBytes % 4 == 0) return 4;
if(rowBytes % 2 == 0) return 2;

return 1; // No alignment
}

void
GLTexture::Create( CPUResource* resource )
{
Expand Down Expand Up @@ -135,10 +178,12 @@ GLTexture::Create( CPUResource* resource )
const U32 h = texture->GetHeight();
const U8* data = texture->GetData();
{
#if defined( Rtt_EMSCRIPTEN_ENV )
glPixelStorei( GL_UNPACK_ALIGNMENT, texture->GetByteAlignment() );
//#if defined( Rtt_EMSCRIPTEN_ENV )
// glPixelStorei( GL_UNPACK_ALIGNMENT, texture->GetByteAlignment() );
// GL_CHECK_ERROR();
//#endif
glPixelStorei(GL_UNPACK_ALIGNMENT, CalculateOptimalAlignment(w, internalFormat));
GL_CHECK_ERROR();
#endif

// It is valid to pass a NULL pointer, so allocation is done either way
glTexImage2D( GL_TEXTURE_2D, 0, internalFormat, w, h, 0, format, type, data );
Expand Down Expand Up @@ -174,6 +219,10 @@ GLTexture::Update( CPUResource* resource )
getFormatTokens( texture->GetFormat(), internalFormat, format, type );

glBindTexture( GL_TEXTURE_2D, GetName() );

glPixelStorei(GL_UNPACK_ALIGNMENT, CalculateOptimalAlignment(w, internalFormat));
GL_CHECK_ERROR();

if (internalFormat == fCachedFormat && w == fCachedWidth && h == fCachedHeight )
{
glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, w, h, format, type, data );
Expand Down
0