Skip to content
Permalink
Browse files

MYST3: Remove usage of Desktop specific GL features in GLES2 mode

- GL_UNPACK_ROW_LENGTH can only be used when extension GL_EXT_unpack_subimage is available
- GL_UNSIGNED_INT_8_8_8_8_REV does not exist in GLES2
  • Loading branch information...
bgK committed May 25, 2015
1 parent e0716bb commit 92d174caefbb235db491591e65f9463507a0cd8a
Showing with 85 additions and 7 deletions.
  1. +20 −2 engines/myst3/gfx_opengl_shaders.cpp
  2. +56 −5 engines/myst3/gfx_opengl_texture.cpp
  3. +9 −0 engines/myst3/gfx_opengl_texture.h
@@ -57,6 +57,7 @@
#include "math/rect2d.h"
#include "math/quat.h"

#include "graphics/opengles2/extensions.h"
#include "graphics/opengles2/shader.h"

#include "engines/myst3/gfx.h"
@@ -125,7 +126,13 @@ ShaderRenderer::~ShaderRenderer() {
}

Texture *ShaderRenderer::createTexture(const Graphics::Surface *surface) {
return new OpenGLTexture(surface, true);
OpenGLTexture *texture = new OpenGLTexture(surface, true);

#if defined(USE_GLES2)
texture->setUnpackSubImageSupport(Graphics::isExtensionSupported("GL_EXT_unpack_subimage"));
#endif

return texture;
}

void ShaderRenderer::freeTexture(Texture *texture) {
@@ -391,7 +398,18 @@ Graphics::Surface *ShaderRenderer::getScreenshot() {
Graphics::Surface *s = new Graphics::Surface();
s->create(screen.width(), screen.height(), Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24));

glReadPixels(screen.left, screen.top, screen.width(), screen.height(), GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, s->getPixels());
#if defined(USE_GLES2)
GLenum format = GL_UNSIGNED_BYTE;
#else
GLenum format = GL_UNSIGNED_INT_8_8_8_8_REV;
#endif

glReadPixels(screen.left, screen.top, screen.width(), screen.height(), GL_RGBA, format, s->getPixels());

#if defined(USE_GLES2) && defined(SCUMM_BIG_ENDIAN)
// OpenGL ES does not support the GL_UNSIGNED_INT_8_8_8_8_REV texture format, we need to byteswap the surface
OpenGLTexture::byteswapSurface(s);
#endif

flipVertical(s);

@@ -26,6 +26,11 @@

#include "engines/myst3/gfx_opengl_texture.h"

#if defined(USE_GLES2)
// Alias the ES extension constant to the desktop GL one
#define GL_UNPACK_ROW_LENGTH GL_UNPACK_ROW_LENGTH_EXT
#endif

namespace Myst3 {

// From Bit Twiddling Hacks
@@ -40,7 +45,9 @@ static uint32 upperPowerOfTwo(uint32 v) {
return v;
}

OpenGLTexture::OpenGLTexture(const Graphics::Surface *surface, bool nonPoTSupport) {
OpenGLTexture::OpenGLTexture(const Graphics::Surface *surface, bool nonPoTSupport) :
_unpackSubImageSupport(true) {

width = surface->w;
height = surface->h;
format = surface->format;
@@ -56,7 +63,12 @@ OpenGLTexture::OpenGLTexture(const Graphics::Surface *surface, bool nonPoTSuppor

if (format.bytesPerPixel == 4) {
internalFormat = GL_RGBA;

#if defined(USE_GLES2)
sourceFormat = GL_UNSIGNED_BYTE;
#else
sourceFormat = GL_UNSIGNED_INT_8_8_8_8_REV;
#endif
} else if (format.bytesPerPixel == 2) {
internalFormat = GL_RGB;
sourceFormat = GL_UNSIGNED_SHORT_5_6_5;
@@ -75,6 +87,7 @@ OpenGLTexture::OpenGLTexture(const Graphics::Surface *surface, bool nonPoTSuppor
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

update(surface);

}

OpenGLTexture::~OpenGLTexture() {
@@ -85,12 +98,50 @@ void OpenGLTexture::update(const Graphics::Surface *surface) {
updatePartial(surface, Common::Rect(surface->w, surface->h));
}

void OpenGLTexture::updateTexture(const Graphics::Surface* surface, const Common::Rect& rect) {
glBindTexture(GL_TEXTURE_2D, id);

if (_unpackSubImageSupport) {
const Graphics::Surface subArea = surface->getSubArea(rect);

glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch / surface->format.bytesPerPixel);
glTexSubImage2D(GL_TEXTURE_2D, 0, rect.left, rect.top, subArea.w, subArea.h, internalFormat, sourceFormat, subArea.getPixels());
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
} else {
// GL_UNPACK_ROW_LENGTH is not supported, don't bother and do a full texture update
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, surface->w, surface->h, internalFormat, sourceFormat, surface->getPixels());
}
}

void OpenGLTexture::updatePartial(const Graphics::Surface *surface, const Common::Rect &rect) {
const Graphics::Surface subArea = surface->getSubArea(rect);
#if defined(USE_GLES2) && defined(SCUMM_BIG_ENDIAN)
// OpenGL ES does not support the GL_UNSIGNED_INT_8_8_8_8_REV texture format
// we need to byteswap before hand
Graphics::Surface swappedSurface;
swappedSurface.copyFrom(*surface);
byteswapSurface(&swappedSurface);
updateTexture(&swappedSurface, rect);
swappedSurface.free();
#else
updateTexture(surface, rect);
#endif

glBindTexture(GL_TEXTURE_2D, id);
glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch / surface->format.bytesPerPixel);
glTexSubImage2D(GL_TEXTURE_2D, 0, rect.left, rect.top, subArea.w, subArea.h, internalFormat, sourceFormat, subArea.getPixels());
}

void OpenGLTexture::byteswapSurface(Graphics::Surface *surface) {
for (int y = 0; y < surface->h; y++) {
for (int x = 0; x < surface->w; x++) {
if (surface->format.bytesPerPixel == 4) {
uint32 *pixel = (uint32 *) (surface->getBasePtr(x, y));
*pixel = SWAP_BYTES_32(*pixel);
} else if (surface->format.bytesPerPixel == 2) {
uint16 *pixel = (uint16 *) (surface->getBasePtr(x, y));
*pixel = SWAP_BYTES_16(*pixel);
} else {
error("Unexpected bytes per pixedl %d", surface->format.bytesPerPixel);
}
}
}
}

} // End of namespace Myst3
@@ -39,11 +39,20 @@ class OpenGLTexture : public Texture {
void update(const Graphics::Surface *surface) override;
void updatePartial(const Graphics::Surface *surface, const Common::Rect &rect) override;

void setUnpackSubImageSupport(bool unpackSubImageSupport) { _unpackSubImageSupport = unpackSubImageSupport; }

static void byteswapSurface(Graphics::Surface *surface);

GLuint id;
GLuint internalFormat;
GLuint sourceFormat;
uint32 internalWidth;
uint32 internalHeight;

private:
void updateTexture(const Graphics::Surface *surface, const Common::Rect &rect);

bool _unpackSubImageSupport;
};

} // End of namespace Myst3

0 comments on commit 92d174c

Please sign in to comment.
You can’t perform that action at this time.