all repos — mgba @ b325376f05945b2ca3d00b30e57de9187c5b0ef4

mGBA Game Boy Advance Emulator

Video: Remove assumption about video backend dimensions
Jeffrey Pfau jeffrey@endrift.com
Thu, 04 Feb 2016 23:16:50 -0800
commit

b325376f05945b2ca3d00b30e57de9187c5b0ef4

parent

1768721e7e2669373f5472ab56690cc9b0904097

M src/platform/opengl/gl.csrc/platform/opengl/gl.c

@@ -5,7 +5,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this

* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "gl.h" -#include "gba/video.h" +#include "util/math.h" static const GLint _glVertices[] = { 0, 0,

@@ -21,7 +21,7 @@ 1, 1,

0, 1 }; -static void GBAGLContextInit(struct VideoBackend* v, WHandle handle) { +static void GBAGLContextInit(struct VideoBackend* v, unsigned width, unsigned height, WHandle handle) { UNUSED(handle); struct GBAGLContext* context = (struct GBAGLContext*) v; glGenTextures(1, &context->tex);

@@ -31,15 +31,17 @@ #ifndef _WIN32

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); #endif + v->width = width; + v->height = height; #ifdef COLOR_16_BIT #ifdef COLOR_5_6_5 - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, toPow2(width), toPow2(height), 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0); #else - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 0); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, toPow2(width), toPow2(height), 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 0); #endif #else - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, toPow2(width), toPow2(height), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); #endif }

@@ -48,9 +50,9 @@ struct GBAGLContext* context = (struct GBAGLContext*) v;

glDeleteTextures(1, &context->tex); } -static void GBAGLContextResized(struct VideoBackend* v, int w, int h) { - int drawW = w; - int drawH = h; +static void GBAGLContextResized(struct VideoBackend* v, unsigned w, unsigned h) { + unsigned drawW = w; + unsigned drawH = h; if (v->lockAspectRatio) { if (w * 2 > h * 3) { drawW = h * 3 / 2;

@@ -80,7 +82,7 @@ glVertexPointer(2, GL_INT, 0, _glVertices);

glTexCoordPointer(2, GL_INT, 0, _glTexCoords); glMatrixMode(GL_PROJECTION); glLoadIdentity(); - glOrtho(0, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, 0, 1); + glOrtho(0, v->width, v->height, 0, 0, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glBindTexture(GL_TEXTURE_2D, context->tex);

@@ -99,12 +101,12 @@ struct GBAGLContext* context = (struct GBAGLContext*) v;

glBindTexture(GL_TEXTURE_2D, context->tex); #ifdef COLOR_16_BIT #ifdef COLOR_5_6_5 - glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, frame); + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, v->width, v->height, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, frame); #else - glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, frame); + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, v->width, v->height, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, frame); #endif #else - glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, GL_RGBA, GL_UNSIGNED_BYTE, frame); + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, v->width, v->height, GL_RGBA, GL_UNSIGNED_BYTE, frame); #endif }
M src/platform/opengl/gles2.csrc/platform/opengl/gles2.c

@@ -5,7 +5,6 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this

* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "gles2.h" -#include "gba/video.h" #include "util/configuration.h" #include "util/formatting.h" #include "util/vector.h"

@@ -72,22 +71,24 @@ 1.f, 1.f,

1.f, -1.f, }; -static void GBAGLES2ContextInit(struct VideoBackend* v, WHandle handle) { +static void GBAGLES2ContextInit(struct VideoBackend* v, unsigned width, unsigned height, WHandle handle) { UNUSED(handle); struct GBAGLES2Context* context = (struct GBAGLES2Context*) v; glGenTextures(1, &context->tex); glBindTexture(GL_TEXTURE_2D, context->tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + v->width = width; + v->height = height; #ifdef COLOR_16_BIT #ifdef COLOR_5_6_5 - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0); #else - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 0); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 0); #endif #else - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); #endif glClearColor(0.f, 0.f, 0.f, 1.f);

@@ -138,7 +139,9 @@ uniforms[3].max.fvec3[2] = 1.0f;

GBAGLES2ShaderInit(&context->initialShader, _vertexShader, _fragmentShader, -1, -1, false, uniforms, 4); GBAGLES2ShaderInit(&context->finalShader, 0, 0, 0, 0, false, 0, 0); glDeleteFramebuffers(1, &context->finalShader.fbo); + glDeleteTextures(1, &context->finalShader.tex); context->finalShader.fbo = 0; + context->finalShader.tex = 0; } static void GBAGLES2ContextDeinit(struct VideoBackend* v) {

@@ -149,9 +152,9 @@ GBAGLES2ShaderDeinit(&context->finalShader);

free(context->initialShader.uniforms); } -static void GBAGLES2ContextResized(struct VideoBackend* v, int w, int h) { - int drawW = w; - int drawH = h; +static void GBAGLES2ContextResized(struct VideoBackend* v, unsigned w, unsigned h) { + unsigned drawW = w; + unsigned drawH = h; if (v->lockAspectRatio) { if (w * 2 > h * 3) { drawW = h * 3 / 2;

@@ -159,7 +162,7 @@ } else if (w * 2 < h * 3) {

drawH = w * 2 / 3; } } - glViewport(0, 0, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS); + glViewport(0, 0, v->width, v->height); glClearColor(0.f, 0.f, 0.f, 1.f); glClear(GL_COLOR_BUFFER_BIT); glViewport((w - drawW) / 2, (h - drawH) / 2, drawW, drawH);

@@ -171,7 +174,7 @@ glClearColor(0.f, 0.f, 0.f, 1.f);

glClear(GL_COLOR_BUFFER_BIT); } -void _drawShader(struct GBAGLES2Shader* shader) { +void _drawShader(struct GBAGLES2Context* context, struct GBAGLES2Shader* shader) { GLint viewport[4]; glBindFramebuffer(GL_FRAMEBUFFER, shader->fbo); if (shader->blend) {

@@ -190,19 +193,23 @@ int padH = 0;

if (!shader->width) { drawW = viewport[2]; padW = viewport[0]; + } else if (shader->width < 0) { + drawW = context->d.width * -shader->width; } if (!shader->height) { drawH = viewport[3]; padH = viewport[1]; + } else if (shader->height < 0) { + drawH = context->d.height * -shader->height; } if (shader->integerScaling) { padW = 0; padH = 0; - drawW -= drawW % VIDEO_HORIZONTAL_PIXELS; - drawH -= drawH % VIDEO_VERTICAL_PIXELS; + drawW -= drawW % context->d.width; + drawH -= drawH % context->d.height; } glViewport(padW, padH, drawW, drawH); - if (!shader->width || !shader->height) { + if (shader->tex && (shader->width <= 0 || shader->height <= 0)) { GLint oldTex; glGetIntegerv(GL_TEXTURE_BINDING_2D, &oldTex); glBindTexture(GL_TEXTURE_2D, shader->tex);

@@ -278,12 +285,12 @@ glActiveTexture(GL_TEXTURE0);

glBindTexture(GL_TEXTURE_2D, context->tex); context->finalShader.filter = v->filter; - _drawShader(&context->initialShader); + _drawShader(context, &context->initialShader); size_t n; for (n = 0; n < context->nShaders; ++n) { - _drawShader(&context->shaders[n]); + _drawShader(context, &context->shaders[n]); } - _drawShader(&context->finalShader); + _drawShader(context, &context->finalShader); glBindFramebuffer(GL_FRAMEBUFFER, 0); glUseProgram(0); }

@@ -293,12 +300,12 @@ struct GBAGLES2Context* context = (struct GBAGLES2Context*) v;

glBindTexture(GL_TEXTURE_2D, context->tex); #ifdef COLOR_16_BIT #ifdef COLOR_5_6_5 - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, frame); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, v->width, v->height, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, frame); #else - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, frame); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, v->width, v->height, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, frame); #endif #else - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, GL_RGBA, GL_UNSIGNED_BYTE, frame); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, v->width, v->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, frame); #endif }

@@ -317,8 +324,8 @@ context->nShaders = 0;

} void GBAGLES2ShaderInit(struct GBAGLES2Shader* shader, const char* vs, const char* fs, int width, int height, bool integerScaling, struct GBAGLES2Uniform* uniforms, size_t nUniforms) { - shader->width = width >= 0 ? width : VIDEO_HORIZONTAL_PIXELS; - shader->height = height >= 0 ? height : VIDEO_VERTICAL_PIXELS; + shader->width = width; + shader->height = height; shader->integerScaling = integerScaling; shader->filter = false; shader->blend = false;

@@ -333,7 +340,7 @@ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - if (shader->width && shader->height) { + if (shader->width > 0 && shader->height > 0) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, shader->width, shader->height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); }
M src/platform/opengl/gles2.hsrc/platform/opengl/gles2.h

@@ -51,8 +51,8 @@ const char* readableName;

}; struct GBAGLES2Shader { - unsigned width; - unsigned height; + int width; + int height; bool integerScaling; bool filter; bool blend;
M src/platform/qt/DisplayGL.cppsrc/platform/qt/DisplayGL.cpp

@@ -221,7 +221,7 @@ m_gl->makeCurrent();

#if defined(_WIN32) && defined(USE_EPOXY) epoxy_handle_external_wglMakeCurrent(); #endif - m_backend->init(m_backend, reinterpret_cast<WHandle>(m_gl->winId())); + m_backend->init(m_backend, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, reinterpret_cast<WHandle>(m_gl->winId())); #if !defined(_WIN32) || defined(USE_EPOXY) if (m_supportsShaders) { m_shader.preprocessShader = static_cast<void*>(&reinterpret_cast<GBAGLES2Context*>(m_backend)->initialShader);
M src/platform/sdl/gl-sdl.csrc/platform/sdl/gl-sdl.c

@@ -53,7 +53,7 @@ renderer->gl.d.user = renderer;

renderer->gl.d.lockAspectRatio = renderer->lockAspectRatio; renderer->gl.d.filter = renderer->filter; renderer->gl.d.swap = mSDLGLCommonSwap; - renderer->gl.d.init(&renderer->gl.d, 0); + renderer->gl.d.init(&renderer->gl.d, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0); _doViewport(renderer->viewportWidth, renderer->viewportHeight, &renderer->gl.d); return true;

@@ -71,16 +71,16 @@ bool mSDLGLInitGB(struct mSDLRenderer* renderer) {

mSDLGLCommonInit(renderer); // TODO: Pass texture size along - renderer->outputBuffer = malloc(VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL); - memset(renderer->outputBuffer, 0, VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL); - renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer + GB_GBA_CENTER, VIDEO_HORIZONTAL_PIXELS); + renderer->outputBuffer = malloc(GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL); + memset(renderer->outputBuffer, 0, GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL); + renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, GB_VIDEO_HORIZONTAL_PIXELS); GBAGLContextCreate(&renderer->gl); renderer->gl.d.user = renderer; renderer->gl.d.lockAspectRatio = renderer->lockAspectRatio; renderer->gl.d.filter = renderer->filter; renderer->gl.d.swap = mSDLGLCommonSwap; - renderer->gl.d.init(&renderer->gl.d, 0); + renderer->gl.d.init(&renderer->gl.d, GB_VIDEO_HORIZONTAL_PIXELS, GB_VIDEO_VERTICAL_PIXELS, 0); _doViewport(renderer->viewportWidth, renderer->viewportHeight, &renderer->gl.d); return true;
M src/platform/sdl/main.csrc/platform/sdl/main.c

@@ -117,10 +117,10 @@ #ifdef M_CORE_GB

else if (GBIsROM(vf)) { platform = PLATFORM_GB; if (!opts.width) { - opts.width = /*GB_*/VIDEO_HORIZONTAL_PIXELS; + opts.width = GB_VIDEO_HORIZONTAL_PIXELS; } if (!opts.height) { - opts.height = /*GB_*/VIDEO_VERTICAL_PIXELS; + opts.height = GB_VIDEO_VERTICAL_PIXELS; } renderer.core = GBCoreCreate(); #ifdef BUILD_GL
M src/platform/video-backend.hsrc/platform/video-backend.h

@@ -16,17 +16,19 @@ typedef void* WHandle;

#endif struct VideoBackend { - void (*init)(struct VideoBackend*, WHandle handle); + void (*init)(struct VideoBackend*, unsigned width, unsigned height, WHandle handle); void (*deinit)(struct VideoBackend*); void (*swap)(struct VideoBackend*); void (*clear)(struct VideoBackend*); - void (*resized)(struct VideoBackend*, int w, int h); + void (*resized)(struct VideoBackend*, unsigned w, unsigned h); void (*postFrame)(struct VideoBackend*, const void* frame); void (*drawFrame)(struct VideoBackend*); void (*setMessage)(struct VideoBackend*, const char* message); void (*clearMessage)(struct VideoBackend*); void* user; + unsigned width; + unsigned height; bool filter; bool lockAspectRatio;