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
This commit is contained in:
Bastien Bouclet 2015-05-25 14:29:37 +02:00
parent 317cdc55a7
commit 8e54a010b4
3 changed files with 86 additions and 8 deletions

View file

@ -57,6 +57,7 @@
#include "math/rect2d.h" #include "math/rect2d.h"
#include "math/quat.h" #include "math/quat.h"
#include "graphics/opengles2/extensions.h"
#include "graphics/opengles2/shader.h" #include "graphics/opengles2/shader.h"
#include "engines/myst3/gfx.h" #include "engines/myst3/gfx.h"
@ -125,7 +126,13 @@ ShaderRenderer::~ShaderRenderer() {
} }
Texture *ShaderRenderer::createTexture(const Graphics::Surface *surface) { 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) { void ShaderRenderer::freeTexture(Texture *texture) {
@ -391,7 +398,18 @@ Graphics::Surface *ShaderRenderer::getScreenshot() {
Graphics::Surface *s = new Graphics::Surface(); Graphics::Surface *s = new Graphics::Surface();
s->create(screen.width(), screen.height(), Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24)); 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); flipVertical(s);

View file

@ -26,6 +26,11 @@
#include "engines/myst3/gfx_opengl_texture.h" #include "engines/myst3/gfx_opengl_texture.h"
#if !defined(GL_UNPACK_ROW_LENGTH)
// The Android SDK does not declare GL_UNPACK_ROW_LENGTH_EXT
#define GL_UNPACK_ROW_LENGTH 0x0CF2
#endif
namespace Myst3 { namespace Myst3 {
// From Bit Twiddling Hacks // From Bit Twiddling Hacks
@ -40,7 +45,9 @@ static uint32 upperPowerOfTwo(uint32 v) {
return v; return v;
} }
OpenGLTexture::OpenGLTexture(const Graphics::Surface *surface, bool nonPoTSupport) { OpenGLTexture::OpenGLTexture(const Graphics::Surface *surface, bool nonPoTSupport) :
_unpackSubImageSupport(true) {
width = surface->w; width = surface->w;
height = surface->h; height = surface->h;
format = surface->format; format = surface->format;
@ -56,7 +63,12 @@ OpenGLTexture::OpenGLTexture(const Graphics::Surface *surface, bool nonPoTSuppor
if (format.bytesPerPixel == 4) { if (format.bytesPerPixel == 4) {
internalFormat = GL_RGBA; internalFormat = GL_RGBA;
#if defined(USE_GLES2)
sourceFormat = GL_UNSIGNED_BYTE;
#else
sourceFormat = GL_UNSIGNED_INT_8_8_8_8_REV; sourceFormat = GL_UNSIGNED_INT_8_8_8_8_REV;
#endif
} else if (format.bytesPerPixel == 2) { } else if (format.bytesPerPixel == 2) {
internalFormat = GL_RGB; internalFormat = GL_RGB;
sourceFormat = GL_UNSIGNED_SHORT_5_6_5; 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); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
update(surface); update(surface);
} }
OpenGLTexture::~OpenGLTexture() { OpenGLTexture::~OpenGLTexture() {
@ -85,12 +98,50 @@ void OpenGLTexture::update(const Graphics::Surface *surface) {
updatePartial(surface, Common::Rect(surface->w, surface->h)); updatePartial(surface, Common::Rect(surface->w, surface->h));
} }
void OpenGLTexture::updatePartial(const Graphics::Surface *surface, const Common::Rect &rect) { void OpenGLTexture::updateTexture(const Graphics::Surface* surface, const Common::Rect& rect) {
const Graphics::Surface subArea = surface->getSubArea(rect);
glBindTexture(GL_TEXTURE_2D, id); 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()); 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) {
#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
}
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 } // End of namespace Myst3

View file

@ -39,11 +39,20 @@ public:
void update(const Graphics::Surface *surface) override; void update(const Graphics::Surface *surface) override;
void updatePartial(const Graphics::Surface *surface, const Common::Rect &rect) 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 id;
GLuint internalFormat; GLuint internalFormat;
GLuint sourceFormat; GLuint sourceFormat;
uint32 internalWidth; uint32 internalWidth;
uint32 internalHeight; uint32 internalHeight;
private:
void updateTexture(const Graphics::Surface *surface, const Common::Rect &rect);
bool _unpackSubImageSupport;
}; };
} // End of namespace Myst3 } // End of namespace Myst3