ANDROID: Switch renderer to GL ES2

This commit is contained in:
Dries Harnie 2013-02-07 20:58:22 +01:00
parent 15644d32fc
commit 1272c4030c
5 changed files with 88 additions and 117 deletions

View file

@ -310,7 +310,10 @@ public:
// ResidualVM specific method
virtual void launcherInitSize(uint w, uint h);
bool lockMouse(bool lock);
Graphics::PixelBuffer setupScreen(int screenW, int screenH, bool fullscreen, bool accel3d);
Graphics::PixelBuffer setupScreen(int screenW, int screenH, bool fullscreen, bool accel3d) {
return setupScreen(screenW, screenH, fullscreen, accel3d, true);
}
Graphics::PixelBuffer setupScreen(int screenW, int screenH, bool fullscreen, bool accel3d, bool isGame);
};
#endif

View file

@ -52,7 +52,7 @@ static inline GLfixed xdiv(int numerator, int denominator) {
// ResidualVM specific method
void OSystem_Android::launcherInitSize(uint w, uint h) {
//setupScreen(w, h, true, false);
setupScreen(w, h, true, true, false);
}
// ResidualVM specific method
@ -191,7 +191,7 @@ void OSystem_Android::initSurface() {
JNI::initSurface();
// Initialize OpenGLES context.
GLESTexture::initGLExtensions();
GLESTexture::initGL();
if (_game_texture)
_game_texture->reinit();
@ -233,44 +233,14 @@ void OSystem_Android::initViewport() {
assert(JNI::haveSurface());
if (_opengl) {
GLCALL(glEnable(GL_DITHER));
GLCALL(glShadeModel(GL_SMOOTH));
GLCALL(glDisableClientState(GL_VERTEX_ARRAY));
GLCALL(glDisableClientState(GL_TEXTURE_COORD_ARRAY));
GLCALL(glDisable(GL_TEXTURE_2D));
GLCALL(glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST));
} else {
// Turn off anything that looks like 3D ;)
GLCALL(glDisable(GL_DITHER));
GLCALL(glShadeModel(GL_FLAT));
GLCALL(glEnableClientState(GL_VERTEX_ARRAY));
GLCALL(glEnableClientState(GL_TEXTURE_COORD_ARRAY));
GLCALL(glEnable(GL_TEXTURE_2D));
GLCALL(glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST));
}
GLCALL(glDisable(GL_CULL_FACE));
GLCALL(glDisable(GL_DEPTH_TEST));
GLCALL(glDisable(GL_LIGHTING));
GLCALL(glDisable(GL_FOG));
GLCALL(glEnable(GL_BLEND));
GLCALL(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
GLCALL(glViewport(0, 0, _egl_surface_width, _egl_surface_height));
GLCALL(glMatrixMode(GL_PROJECTION));
GLCALL(glLoadIdentity());
GLCALL(glOrthof(0, _egl_surface_width, _egl_surface_height, 0, -1, 1));
GLCALL(glMatrixMode(GL_MODELVIEW));
GLCALL(glLoadIdentity());
LOGD("viewport size: %dx%d", _egl_surface_width, _egl_surface_height);
clearFocusRectangle();
}
@ -287,10 +257,10 @@ void OSystem_Android::initOverlay() {
// enforces the 'lowres' layout, which will be scaled back up by factor 2x,
// but this looks way better than the 'normal' layout scaled by some
// calculated factors
while (overlay_height > 480) {
overlay_width /= 2;
overlay_height /= 2;
}
// while (overlay_height > 480) {
// overlay_width /= 2;
// overlay_height /= 2;
// }
LOGI("overlay size is %ux%u", overlay_width, overlay_height);
@ -333,7 +303,7 @@ void OSystem_Android::clearScreen(FixupType type, byte count) {
for (byte i = 0; i < count; ++i) {
// clear screen
GLCALL(glClearColorx(0, 0, 0, 1 << 16));
GLCALL(glClearColor(0, 0, 0, 1 << 16));
if (_opengl) {
GLCALL(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT));
} else {
@ -460,15 +430,18 @@ void OSystem_Android::copyRectToScreen(const void *buf, int pitch,
// ResidualVM specific method
Graphics::PixelBuffer OSystem_Android::setupScreen(int screenW, int screenH, bool fullscreen, bool accel3d) {
Graphics::PixelBuffer OSystem_Android::setupScreen(int screenW, int screenH, bool fullscreen, bool accel3d, bool isGame) {
_opengl = accel3d;
initViewport();
if (_opengl) {
// resize game texture
initSize(screenW, screenH, 0);
if (isGame)
_game_texture->setGameTexture();
// format is not used by the gfx_opengl driver, use fake format
_game_pbuf.set(Graphics::PixelFormat(), 0);
} else {
Graphics::PixelFormat format = GLES565Texture::pixelFormat();
initSize(screenW, screenH, &format);
@ -488,7 +461,6 @@ void OSystem_Android::updateScreen() {
if (!JNI::haveSurface())
return;
if (!_opengl) {
if (_game_pbuf) {
int pitch = _game_texture->width() * _game_texture->getPixelFormat().bytesPerPixel;
_game_texture->updateBuffer(0, 0, _game_texture->width(), _game_texture->height(),
@ -509,8 +481,8 @@ void OSystem_Android::updateScreen() {
if ((_show_overlay || _htc_fail) && !_fullscreen)
clearScreen(kClear);
GLCALL(glPushMatrix());
// TODO: Do we have engines that use this?
#if 0
if (_shake_offset != 0 ||
(!_focus_rect.isEmpty() &&
!Common::Rect(_game_texture->width(),
@ -522,14 +494,17 @@ void OSystem_Android::updateScreen() {
// Move everything up by _shake_offset (game) pixels
GLCALL(glTranslatex(0, -_shake_offset << 16, 0));
}
#endif
// TODO this doesnt work on those sucky drivers, do it differently
// if (_show_overlay)
// GLCALL(glColor4ub(0x9f, 0x9f, 0x9f, 0x9f));
if (_focus_rect.isEmpty()) {
if (true || _focus_rect.isEmpty()) {
_game_texture->drawTextureRect();
} else {
// TODO what is this and do we have engines using it?
#if 0
GLCALL(glPushMatrix());
GLCALL(glScalex(xdiv(_egl_surface_width, _focus_rect.width()),
@ -544,6 +519,7 @@ void OSystem_Android::updateScreen() {
_game_texture->drawTextureRect();
GLCALL(glPopMatrix());
#endif
}
int cs = _mouse_targetscale;
@ -559,17 +535,13 @@ void OSystem_Android::updateScreen() {
}
if (_show_mouse && !_mouse_texture->isEmpty()) {
GLCALL(glPushMatrix());
const Common::Point &mouse = getEventManager()->getMousePos();
// Scale up ResidualVM -> OpenGL (pixel) coordinates
if (_show_overlay) {
GLCALL(glScalex(xdiv(_egl_surface_width,
_overlay_texture->width()),
xdiv(_egl_surface_height,
_overlay_texture->height()),
1 << 16));
_mouse_texture->drawTexture(mouse.x * cs, mouse.y * cs, _mouse_texture->width(), _mouse_texture->height());
}
// TODO: Port the non-overlay code as well?
#if 0
if (_show_overlay) {
} else {
const Common::Rect &r = _game_texture->getDrawRect();
@ -585,23 +557,12 @@ void OSystem_Android::updateScreen() {
(-_mouse_hotspot.y * cs) << 16,
0));
// Note the extra half texel to position the mouse in
// the middle of the x,y square:
GLCALL(glTranslatex((mouse.x << 16) | 1 << 15,
(mouse.y << 16) | 1 << 15, 0));
GLCALL(glScalex(cs << 16, cs << 16, 1 << 16));
_mouse_texture->drawTextureOrigin();
GLCALL(glPopMatrix());
}
GLCALL(glPopMatrix());
#endif
}
if (!JNI::swapBuffers())
LOGW("swapBuffers failed: 0x%x", glGetError());
}
Graphics::Surface *OSystem_Android::lockScreen() {

View file

@ -41,6 +41,7 @@
#include "base/main.h"
#include "graphics/surface.h"
#include "graphics/opengles2/shader.h"
#include "common/rect.h"
#include "common/array.h"
@ -49,12 +50,13 @@
#include "backends/platform/android/texture.h"
#include "backends/platform/android/android.h"
#include "backends/platform/android/jni.h"
// Supported GL extensions
static bool npot_supported = false;
#ifdef GL_OES_draw_texture
static bool draw_tex_supported = false;
#endif
Graphics::Shader * g_box_shader;
GLuint g_verticesVBO;
static inline GLfixed xdiv(int numerator, int denominator) {
assert(numerator < (1 << 16));
@ -73,7 +75,14 @@ static T nextHigher2(T k) {
return k + 1;
}
void GLESBaseTexture::initGLExtensions() {
const GLfloat vertices[] = {
0.0, 0.0,
1.0, 0.0,
0.0, 1.0,
1.0, 1.0,
};
void GLESBaseTexture::initGL() {
const char *ext_string =
reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS));
@ -85,12 +94,13 @@ void GLESBaseTexture::initGLExtensions() {
if (token == "GL_ARB_texture_non_power_of_two")
npot_supported = true;
#ifdef GL_OES_draw_texture
if (token == "GL_OES_draw_texture")
draw_tex_supported = true;
#endif
}
const char* attributes[] = { "position", "texcoord", NULL };
g_box_shader = Graphics::Shader::fromStrings("box", Graphics::BuiltinShaders::boxVertex, Graphics::BuiltinShaders::boxFragment, attributes);
g_verticesVBO = Graphics::Shader::createBuffer(GL_ARRAY_BUFFER, sizeof(vertices), vertices);
g_box_shader->enableVertexAttribute("position", g_verticesVBO, 2, GL_FLOAT, GL_TRUE, 2 * sizeof(float), 0);
g_box_shader->enableVertexAttribute("texcoord", g_verticesVBO, 2, GL_FLOAT, GL_TRUE, 2 * sizeof(float), 0);
}
GLESBaseTexture::GLESBaseTexture(GLenum glFormat, GLenum glType,
@ -106,7 +116,8 @@ GLESBaseTexture::GLESBaseTexture(GLenum glFormat, GLenum glType,
_all_dirty(false),
_dirty_rect(),
_pixelFormat(pixelFormat),
_palettePixelFormat()
_palettePixelFormat(),
_is_game_texture(false)
{
GLCALL(glGenTextures(1, &_texture_name));
}
@ -117,8 +128,6 @@ GLESBaseTexture::~GLESBaseTexture() {
void GLESBaseTexture::release() {
if (_texture_name) {
LOGD("Destroying texture %u", _texture_name);
GLCALL(glDeleteTextures(1, &_texture_name));
_texture_name = 0;
}
@ -178,47 +187,26 @@ void GLESBaseTexture::allocBuffer(GLuint w, GLuint h) {
}
void GLESBaseTexture::drawTexture(GLshort x, GLshort y, GLshort w, GLshort h) {
// LOGD("*** Texture %p: Drawing %dx%d rect to (%d,%d)", this, w, h, x, y);
assert(g_box_shader);
g_box_shader->use();
GLCALL(glBindTexture(GL_TEXTURE_2D, _texture_name));
const GLfloat offsetX = float(x) / float(JNI::egl_surface_width);
const GLfloat offsetY = float(y) / float(JNI::egl_surface_height);
const GLfloat sizeW = float(w) / float(JNI::egl_surface_width);
const GLfloat sizeH = float(h) / float(JNI::egl_surface_height);
const GLfloat tex_width = float(_surface.w) / float(_texture_width);
const GLfloat tex_height = float(_surface.h) / float(_texture_height);
// LOGD("*** Drawing at (%f,%f) , size %f x %f", float(x) / float(_surface.w), float(y) / float(_surface.h), tex_width, tex_height);
#ifdef GL_OES_draw_texture
// Great extension, but only works under specific conditions.
// Still a work-in-progress - disabled for now.
if (false && draw_tex_supported && !hasPalette()) {
//GLCALL(glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE));
const GLint crop[4] = { 0, _surface.h, _surface.w, -_surface.h };
g_box_shader->setUniform("offsetXY", Math::Vector2d(offsetX, offsetY));
g_box_shader->setUniform("sizeWH", Math::Vector2d(sizeW, sizeH));
g_box_shader->setUniform("texcrop", Math::Vector2d(tex_width, tex_height));
g_box_shader->setUniform("flipY", !_is_game_texture);
GLCALL(glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop));
// Android GLES bug?
GLCALL(glColor4ub(0xff, 0xff, 0xff, 0xff));
GLCALL(glDrawTexiOES(x, y, 0, w, h));
} else
#endif
{
const GLfixed tex_width = xdiv(_surface.w, _texture_width);
const GLfixed tex_height = xdiv(_surface.h, _texture_height);
const GLfixed texcoords[] = {
0, 0,
tex_width, 0,
0, tex_height,
tex_width, tex_height,
};
GLCALL(glTexCoordPointer(2, GL_FIXED, 0, texcoords));
const GLshort vertices[] = {
x, y,
x + w, y,
x, y + h,
x + w, y + h,
};
GLCALL(glVertexPointer(2, GL_SHORT, 0, vertices));
assert(ARRAYSIZE(vertices) == ARRAYSIZE(texcoords));
GLCALL(glDrawArrays(GL_TRIANGLE_STRIP, 0, ARRAYSIZE(vertices) / 2));
}
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
clearDirty();
}

View file

@ -36,7 +36,7 @@
class GLESBaseTexture {
public:
static void initGLExtensions();
static void initGL();
protected:
GLESBaseTexture(GLenum glFormat, GLenum glType,
@ -92,6 +92,14 @@ public:
return _surface.h;
}
inline GLuint texWidth() const {
return _texture_width;
}
inline GLuint texHeight() const {
return _texture_height;
}
inline uint16 pitch() const {
return _surface.pitch;
}
@ -131,6 +139,14 @@ public:
return _palettePixelFormat;
}
GLuint getTextureName() const {
return _texture_name;
}
void setGameTexture() {
_is_game_texture = true;
}
protected:
inline void setDirty() {
_all_dirty = true;
@ -169,6 +185,8 @@ protected:
Graphics::PixelFormat _pixelFormat;
Graphics::PixelFormat _palettePixelFormat;
bool _is_game_texture;
};
class GLESTexture : public GLESBaseTexture {