2006-07-19 07:18:45 +00:00
|
|
|
/*
|
2011-04-08 13:03:26 -07:00
|
|
|
Simple DirectMedia Layer
|
2011-12-31 09:28:07 -05:00
|
|
|
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
|
2011-04-08 13:03:26 -07:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
2006-07-19 07:18:45 +00:00
|
|
|
*/
|
|
|
|
#include "SDL_config.h"
|
|
|
|
|
2011-02-08 10:04:09 -08:00
|
|
|
#if SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED
|
2006-07-22 08:33:18 +00:00
|
|
|
|
2011-02-08 22:40:19 -08:00
|
|
|
#include "SDL_hints.h"
|
|
|
|
#include "SDL_log.h"
|
2006-07-22 08:33:18 +00:00
|
|
|
#include "SDL_opengl.h"
|
2011-02-02 14:34:54 -08:00
|
|
|
#include "../SDL_sysrender.h"
|
2011-02-08 16:27:52 -08:00
|
|
|
#include "SDL_shaders_gl.h"
|
2006-07-19 07:18:45 +00:00
|
|
|
|
2007-08-15 04:04:17 +00:00
|
|
|
#ifdef __MACOSX__
|
|
|
|
#include <OpenGL/OpenGL.h>
|
|
|
|
#endif
|
|
|
|
|
2008-11-22 06:59:22 +00:00
|
|
|
|
2006-07-19 07:18:45 +00:00
|
|
|
/* OpenGL renderer implementation */
|
|
|
|
|
2007-08-12 07:02:28 +00:00
|
|
|
/* Details on optimizing the texture path on Mac OS X:
|
2011-02-06 00:48:41 -08:00
|
|
|
http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_texturedata/opengl_texturedata.html
|
2007-08-12 07:02:28 +00:00
|
|
|
*/
|
|
|
|
|
2011-02-02 14:34:54 -08:00
|
|
|
/* Used to re-create the window with OpenGL capability */
|
|
|
|
extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
|
2008-12-06 00:56:47 +00:00
|
|
|
|
2006-08-28 03:17:39 +00:00
|
|
|
static const float inv255f = 1.0f / 255.0f;
|
|
|
|
|
2006-07-19 07:18:45 +00:00
|
|
|
static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
|
2011-02-01 19:19:43 -08:00
|
|
|
static void GL_WindowEvent(SDL_Renderer * renderer,
|
|
|
|
const SDL_WindowEvent *event);
|
2006-07-19 07:18:45 +00:00
|
|
|
static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
|
|
|
|
static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
|
|
|
|
const SDL_Rect * rect, const void *pixels,
|
|
|
|
int pitch);
|
|
|
|
static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
|
2011-02-03 00:19:40 -08:00
|
|
|
const SDL_Rect * rect, void **pixels, int *pitch);
|
2006-07-19 07:18:45 +00:00
|
|
|
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
|
2011-02-15 13:59:59 -08:00
|
|
|
static int GL_UpdateViewport(SDL_Renderer * renderer);
|
2009-12-23 01:55:00 +00:00
|
|
|
static int GL_RenderClear(SDL_Renderer * renderer);
|
|
|
|
static int GL_RenderDrawPoints(SDL_Renderer * renderer,
|
|
|
|
const SDL_Point * points, int count);
|
|
|
|
static int GL_RenderDrawLines(SDL_Renderer * renderer,
|
|
|
|
const SDL_Point * points, int count);
|
|
|
|
static int GL_RenderFillRects(SDL_Renderer * renderer,
|
2011-02-15 13:59:59 -08:00
|
|
|
const SDL_Rect * rects, int count);
|
2006-07-19 07:18:45 +00:00
|
|
|
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
|
2006-08-28 03:17:39 +00:00
|
|
|
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
|
2009-11-15 04:58:03 +00:00
|
|
|
static int GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
|
2009-11-16 07:13:07 +00:00
|
|
|
Uint32 pixel_format, void * pixels, int pitch);
|
2012-01-18 22:45:49 -05:00
|
|
|
static int GL_SetTargetTexture(SDL_Renderer * renderer, SDL_Texture * texture);
|
2006-07-19 07:18:45 +00:00
|
|
|
static void GL_RenderPresent(SDL_Renderer * renderer);
|
|
|
|
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
|
|
|
|
static void GL_DestroyRenderer(SDL_Renderer * renderer);
|
|
|
|
|
|
|
|
|
|
|
|
SDL_RenderDriver GL_RenderDriver = {
|
|
|
|
GL_CreateRenderer,
|
|
|
|
{
|
|
|
|
"opengl",
|
2011-02-06 00:48:41 -08:00
|
|
|
(SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
|
2011-02-03 00:19:40 -08:00
|
|
|
1,
|
|
|
|
{SDL_PIXELFORMAT_ARGB8888},
|
2006-07-19 07:18:45 +00:00
|
|
|
0,
|
|
|
|
0}
|
|
|
|
};
|
|
|
|
|
2012-01-18 22:45:49 -05:00
|
|
|
typedef struct GL_FBOList GL_FBOList;
|
|
|
|
|
|
|
|
struct GL_FBOList
|
|
|
|
{
|
|
|
|
Uint32 w, h;
|
|
|
|
GLuint FBO;
|
|
|
|
GL_FBOList *next;
|
|
|
|
};
|
|
|
|
|
2006-07-19 07:18:45 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
SDL_GLContext context;
|
2006-07-22 21:58:17 +00:00
|
|
|
SDL_bool GL_ARB_texture_rectangle_supported;
|
2011-02-19 21:51:21 -08:00
|
|
|
struct {
|
|
|
|
GL_Shader shader;
|
|
|
|
Uint32 color;
|
|
|
|
int blendMode;
|
|
|
|
} current;
|
2012-01-18 22:45:49 -05:00
|
|
|
|
|
|
|
SDL_bool GL_EXT_framebuffer_object_supported;
|
|
|
|
GL_FBOList *framebuffers;
|
|
|
|
SDL_Texture *renderTarget;
|
|
|
|
SDL_Rect viewport_copy;
|
2006-07-22 23:04:41 +00:00
|
|
|
|
|
|
|
/* OpenGL functions */
|
|
|
|
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
|
2011-02-06 02:35:14 -08:00
|
|
|
#include "SDL_glfuncs.h"
|
2006-07-22 23:04:41 +00:00
|
|
|
#undef SDL_PROC
|
2006-08-06 23:34:59 +00:00
|
|
|
|
2011-02-08 16:27:52 -08:00
|
|
|
/* Multitexture support */
|
|
|
|
SDL_bool GL_ARB_multitexture_supported;
|
|
|
|
PFNGLACTIVETEXTUREARBPROC glActiveTextureARB;
|
2011-02-19 14:27:16 -08:00
|
|
|
GLint num_texture_units;
|
2012-01-18 22:45:49 -05:00
|
|
|
|
|
|
|
PFNGLGENFRAMEBUFFERSEXTPROC glGenFramebuffersEXT;
|
|
|
|
PFNGLDELETEFRAMEBUFFERSEXTPROC glDeleteFramebuffersEXT;
|
|
|
|
PFNGLFRAMEBUFFERTEXTURE2DEXTPROC glFramebufferTexture2DEXT;
|
|
|
|
PFNGLBINDFRAMEBUFFEREXTPROC glBindFramebufferEXT;
|
|
|
|
PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC glCheckFramebufferStatusEXT;
|
2011-02-08 16:27:52 -08:00
|
|
|
|
|
|
|
/* Shader support */
|
|
|
|
GL_ShaderContext *shaders;
|
|
|
|
|
2006-07-19 07:18:45 +00:00
|
|
|
} GL_RenderData;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GLuint texture;
|
2006-07-22 08:33:18 +00:00
|
|
|
GLenum type;
|
2006-07-19 07:18:45 +00:00
|
|
|
GLfloat texw;
|
|
|
|
GLfloat texh;
|
2006-07-22 08:33:18 +00:00
|
|
|
GLenum format;
|
|
|
|
GLenum formattype;
|
2006-07-19 07:18:45 +00:00
|
|
|
void *pixels;
|
|
|
|
int pitch;
|
2011-02-08 10:38:12 -08:00
|
|
|
SDL_Rect locked_rect;
|
2011-02-12 00:25:02 -08:00
|
|
|
|
|
|
|
/* YV12 texture support */
|
|
|
|
SDL_bool yuv;
|
|
|
|
GLuint utexture;
|
|
|
|
GLuint vtexture;
|
2012-01-18 22:45:49 -05:00
|
|
|
|
|
|
|
GL_FBOList *fbo;
|
2006-07-19 07:18:45 +00:00
|
|
|
} GL_TextureData;
|
|
|
|
|
|
|
|
|
2006-07-22 21:02:57 +00:00
|
|
|
static void
|
|
|
|
GL_SetError(const char *prefix, GLenum result)
|
|
|
|
{
|
|
|
|
const char *error;
|
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
case GL_NO_ERROR:
|
|
|
|
error = "GL_NO_ERROR";
|
|
|
|
break;
|
|
|
|
case GL_INVALID_ENUM:
|
|
|
|
error = "GL_INVALID_ENUM";
|
|
|
|
break;
|
|
|
|
case GL_INVALID_VALUE:
|
|
|
|
error = "GL_INVALID_VALUE";
|
|
|
|
break;
|
|
|
|
case GL_INVALID_OPERATION:
|
|
|
|
error = "GL_INVALID_OPERATION";
|
|
|
|
break;
|
|
|
|
case GL_STACK_OVERFLOW:
|
|
|
|
error = "GL_STACK_OVERFLOW";
|
|
|
|
break;
|
|
|
|
case GL_STACK_UNDERFLOW:
|
|
|
|
error = "GL_STACK_UNDERFLOW";
|
|
|
|
break;
|
|
|
|
case GL_OUT_OF_MEMORY:
|
|
|
|
error = "GL_OUT_OF_MEMORY";
|
|
|
|
break;
|
|
|
|
case GL_TABLE_TOO_LARGE:
|
|
|
|
error = "GL_TABLE_TOO_LARGE";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = "UNKNOWN";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
SDL_SetError("%s: %s", prefix, error);
|
|
|
|
}
|
|
|
|
|
2006-07-22 23:04:41 +00:00
|
|
|
static int
|
|
|
|
GL_LoadFunctions(GL_RenderData * data)
|
|
|
|
{
|
|
|
|
#ifdef __SDL_NOGETPROCADDR__
|
|
|
|
#define SDL_PROC(ret,func,params) data->func=func;
|
|
|
|
#else
|
|
|
|
#define SDL_PROC(ret,func,params) \
|
|
|
|
do { \
|
|
|
|
data->func = SDL_GL_GetProcAddress(#func); \
|
|
|
|
if ( ! data->func ) { \
|
|
|
|
SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
|
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
} while ( 0 );
|
|
|
|
#endif /* __SDL_NOGETPROCADDR__ */
|
|
|
|
|
2011-02-06 02:35:14 -08:00
|
|
|
#include "SDL_glfuncs.h"
|
2006-07-22 23:04:41 +00:00
|
|
|
#undef SDL_PROC
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-15 13:59:59 -08:00
|
|
|
static SDL_GLContext SDL_CurrentContext = NULL;
|
|
|
|
|
|
|
|
static int
|
|
|
|
GL_ActivateRenderer(SDL_Renderer * renderer)
|
|
|
|
{
|
|
|
|
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
|
|
|
|
|
|
|
|
if (SDL_CurrentContext != data->context) {
|
|
|
|
if (SDL_GL_MakeCurrent(renderer->window, data->context) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
SDL_CurrentContext = data->context;
|
|
|
|
|
|
|
|
GL_UpdateViewport(renderer);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-19 21:51:21 -08:00
|
|
|
/* This is called if we need to invalidate all of the SDL OpenGL state */
|
|
|
|
static void
|
|
|
|
GL_ResetState(SDL_Renderer *renderer)
|
|
|
|
{
|
|
|
|
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
|
|
|
|
|
|
|
|
if (SDL_CurrentContext == data->context) {
|
|
|
|
GL_UpdateViewport(renderer);
|
|
|
|
} else {
|
|
|
|
GL_ActivateRenderer(renderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
data->current.shader = SHADER_NONE;
|
|
|
|
data->current.color = 0;
|
|
|
|
data->current.blendMode = -1;
|
|
|
|
|
|
|
|
data->glDisable(GL_DEPTH_TEST);
|
|
|
|
data->glDisable(GL_CULL_FACE);
|
|
|
|
/* This ended up causing video discrepancies between OpenGL and Direct3D */
|
|
|
|
/*data->glEnable(GL_LINE_SMOOTH);*/
|
|
|
|
|
|
|
|
data->glMatrixMode(GL_MODELVIEW);
|
|
|
|
data->glLoadIdentity();
|
|
|
|
}
|
|
|
|
|
2012-01-18 22:45:49 -05:00
|
|
|
|
|
|
|
GL_FBOList *
|
|
|
|
GL_GetFBO(GL_RenderData *data, Uint32 w, Uint32 h)
|
|
|
|
{
|
|
|
|
GL_FBOList *result = data->framebuffers;
|
|
|
|
|
|
|
|
while (result && ((result->w != w) || (result->h != h))) {
|
|
|
|
result = result->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
result = SDL_malloc(sizeof(GL_FBOList));
|
|
|
|
if (result) {
|
|
|
|
result->w = w;
|
|
|
|
result->h = h;
|
|
|
|
data->glGenFramebuffersEXT(1, &result->FBO);
|
|
|
|
result->next = data->framebuffers;
|
|
|
|
data->framebuffers = result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-07-19 07:18:45 +00:00
|
|
|
SDL_Renderer *
|
|
|
|
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
|
|
|
|
{
|
|
|
|
SDL_Renderer *renderer;
|
|
|
|
GL_RenderData *data;
|
2011-02-08 22:40:19 -08:00
|
|
|
const char *hint;
|
2006-07-28 08:43:17 +00:00
|
|
|
GLint value;
|
2011-02-02 14:34:54 -08:00
|
|
|
Uint32 window_flags;
|
2006-07-19 07:18:45 +00:00
|
|
|
|
2011-02-02 14:34:54 -08:00
|
|
|
window_flags = SDL_GetWindowFlags(window);
|
|
|
|
if (!(window_flags & SDL_WINDOW_OPENGL)) {
|
|
|
|
if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
|
2012-01-07 21:01:33 -08:00
|
|
|
/* Uh oh, better try to put it back... */
|
|
|
|
SDL_RecreateWindow(window, window_flags);
|
2006-07-22 21:02:57 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2006-07-19 07:18:45 +00:00
|
|
|
}
|
|
|
|
|
2006-07-22 08:33:18 +00:00
|
|
|
renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
|
2006-07-19 07:18:45 +00:00
|
|
|
if (!renderer) {
|
|
|
|
SDL_OutOfMemory();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-07-22 08:33:18 +00:00
|
|
|
data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
|
2006-07-19 07:18:45 +00:00
|
|
|
if (!data) {
|
|
|
|
GL_DestroyRenderer(renderer);
|
|
|
|
SDL_OutOfMemory();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-02-01 19:19:43 -08:00
|
|
|
renderer->WindowEvent = GL_WindowEvent;
|
2006-07-19 07:18:45 +00:00
|
|
|
renderer->CreateTexture = GL_CreateTexture;
|
|
|
|
renderer->UpdateTexture = GL_UpdateTexture;
|
|
|
|
renderer->LockTexture = GL_LockTexture;
|
|
|
|
renderer->UnlockTexture = GL_UnlockTexture;
|
2011-02-15 13:59:59 -08:00
|
|
|
renderer->UpdateViewport = GL_UpdateViewport;
|
2009-12-23 01:55:00 +00:00
|
|
|
renderer->RenderClear = GL_RenderClear;
|
|
|
|
renderer->RenderDrawPoints = GL_RenderDrawPoints;
|
|
|
|
renderer->RenderDrawLines = GL_RenderDrawLines;
|
|
|
|
renderer->RenderFillRects = GL_RenderFillRects;
|
2006-07-19 07:18:45 +00:00
|
|
|
renderer->RenderCopy = GL_RenderCopy;
|
2012-01-18 22:45:49 -05:00
|
|
|
renderer->SetTargetTexture = GL_SetTargetTexture;
|
2009-11-15 04:58:03 +00:00
|
|
|
renderer->RenderReadPixels = GL_RenderReadPixels;
|
2006-07-19 07:18:45 +00:00
|
|
|
renderer->RenderPresent = GL_RenderPresent;
|
|
|
|
renderer->DestroyTexture = GL_DestroyTexture;
|
|
|
|
renderer->DestroyRenderer = GL_DestroyRenderer;
|
|
|
|
renderer->info = GL_RenderDriver.info;
|
2011-02-01 12:19:46 -08:00
|
|
|
renderer->info.flags = SDL_RENDERER_ACCELERATED;
|
2011-02-15 13:59:59 -08:00
|
|
|
renderer->driverdata = data;
|
Fixed bug 1256 - Invalid window warning in GL_CreateRenderer
Martin Gerhardy 2011-07-27 02:26:06 PDT
the window reference is lost in the GL_CreateRenderer function. The attached
patch should fix this error.
#0 SDLSystem_LogOutputFunction (userdata=0x63b010, category=1,
priority=SDL_LOG_PRIORITY_ERROR, message=0x7fffffffcd00 "Invalid window") at
src/system/sdl/SDLSystem.cpp:8
#1 0x00007ffff7b1ddb3 in SDL_LogMessageV (category=1,
priority=SDL_LOG_PRIORITY_ERROR, fmt=<value optimized out>, ap=<value optimized
out>) at src/SDL_log.c:275
#2 0x00007ffff7b1df7c in SDL_LogError (category=<value optimized out>,
fmt=<value optimized out>) at src/SDL_log.c:212
#3 0x00007ffff7b1d582 in SDL_SetError (fmt=0x7ffff7baaff0 "") at
src/SDL_error.c:111
#4 0x00007ffff7b96f9e in SDL_GL_MakeCurrent (window=0x0, ctx=0xa62ce0) at
src/video/SDL_video.c:2484
#5 0x00007ffff7b4ba0c in GL_ActivateRenderer (renderer=0xa8f680) at
src/render/opengl/SDL_render_gl.c:195
#6 0x00007ffff7b4c59a in GL_ResetState (window=0x918010, flags=<value
optimized out>) at src/render/opengl/SDL_render_gl.c:214
#7 GL_CreateRenderer (window=0x918010, flags=<value optimized out>) at
src/render/opengl/SDL_render_gl.c:343
#8 0x00007ffff7b48053 in SDL_CreateRenderer (window=0x918010, index=<value
optimized out>, flags=2) at src/render/SDL_render.c:166
2012-01-07 02:32:08 -05:00
|
|
|
renderer->window = window;
|
2006-07-19 07:18:45 +00:00
|
|
|
|
2010-01-21 06:21:52 +00:00
|
|
|
data->context = SDL_GL_CreateContext(window);
|
2006-07-19 07:18:45 +00:00
|
|
|
if (!data->context) {
|
|
|
|
GL_DestroyRenderer(renderer);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-01-21 06:21:52 +00:00
|
|
|
if (SDL_GL_MakeCurrent(window, data->context) < 0) {
|
2006-07-19 07:18:45 +00:00
|
|
|
GL_DestroyRenderer(renderer);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-02-06 02:35:14 -08:00
|
|
|
|
|
|
|
if (GL_LoadFunctions(data) < 0) {
|
|
|
|
GL_DestroyRenderer(renderer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-08-15 04:04:17 +00:00
|
|
|
#ifdef __MACOSX__
|
|
|
|
/* Enable multi-threaded rendering */
|
|
|
|
/* Disabled until Ryan finishes his VBO/PBO code...
|
2008-01-08 00:10:46 +00:00
|
|
|
CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
|
|
|
|
*/
|
2007-08-15 04:04:17 +00:00
|
|
|
#endif
|
|
|
|
|
2006-08-05 17:09:42 +00:00
|
|
|
if (flags & SDL_RENDERER_PRESENTVSYNC) {
|
2006-07-19 07:18:45 +00:00
|
|
|
SDL_GL_SetSwapInterval(1);
|
|
|
|
} else {
|
|
|
|
SDL_GL_SetSwapInterval(0);
|
|
|
|
}
|
|
|
|
if (SDL_GL_GetSwapInterval() > 0) {
|
2006-08-05 17:09:42 +00:00
|
|
|
renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
|
2006-07-19 07:18:45 +00:00
|
|
|
}
|
|
|
|
|
2006-07-28 08:43:17 +00:00
|
|
|
data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
|
|
|
|
renderer->info.max_texture_width = value;
|
|
|
|
data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
|
|
|
|
renderer->info.max_texture_height = value;
|
2006-07-22 08:33:18 +00:00
|
|
|
|
2006-07-22 21:58:17 +00:00
|
|
|
if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
|
|
|
|
|| SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
|
|
|
|
data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
|
|
|
|
}
|
2006-07-22 08:33:18 +00:00
|
|
|
|
2011-02-08 16:27:52 -08:00
|
|
|
/* Check for multitexture support */
|
|
|
|
if (SDL_GL_ExtensionSupported("GL_ARB_multitexture")) {
|
|
|
|
data->glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC) SDL_GL_GetProcAddress("glActiveTextureARB");
|
|
|
|
if (data->glActiveTextureARB) {
|
|
|
|
data->GL_ARB_multitexture_supported = SDL_TRUE;
|
|
|
|
data->glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &data->num_texture_units);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for shader support */
|
2011-02-08 22:40:19 -08:00
|
|
|
hint = SDL_GetHint(SDL_HINT_RENDER_OPENGL_SHADERS);
|
|
|
|
if (!hint || *hint != '0') {
|
|
|
|
data->shaders = GL_CreateShaderContext();
|
|
|
|
}
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_RENDER, "OpenGL shaders: %s",
|
|
|
|
data->shaders ? "ENABLED" : "DISABLED");
|
2011-02-08 16:27:52 -08:00
|
|
|
|
|
|
|
/* We support YV12 textures using 3 textures and a shader */
|
|
|
|
if (data->shaders && data->num_texture_units >= 3) {
|
|
|
|
renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_YV12;
|
2011-02-12 00:25:02 -08:00
|
|
|
renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_IYUV;
|
2011-02-08 16:27:52 -08:00
|
|
|
}
|
2012-01-18 22:45:49 -05:00
|
|
|
|
|
|
|
if (SDL_GL_ExtensionSupported("GL_EXT_framebuffer_object")) {
|
|
|
|
data->GL_EXT_framebuffer_object_supported = SDL_TRUE;
|
|
|
|
data->glGenFramebuffersEXT = (PFNGLGENFRAMEBUFFERSEXTPROC)
|
|
|
|
SDL_GL_GetProcAddress("glGenFramebuffersEXT");
|
|
|
|
data->glDeleteFramebuffersEXT = (PFNGLDELETEFRAMEBUFFERSEXTPROC)
|
|
|
|
SDL_GL_GetProcAddress("glDeleteFramebuffersEXT");
|
|
|
|
data->glFramebufferTexture2DEXT = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC)
|
|
|
|
SDL_GL_GetProcAddress("glFramebufferTexture2DEXT");
|
|
|
|
data->glBindFramebufferEXT = (PFNGLBINDFRAMEBUFFEREXTPROC)
|
|
|
|
SDL_GL_GetProcAddress("glBindFramebufferEXT");
|
|
|
|
data->glCheckFramebufferStatusEXT = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC)
|
|
|
|
SDL_GL_GetProcAddress("glCheckFramebufferStatusEXT");
|
|
|
|
}
|
|
|
|
data->framebuffers = NULL;
|
|
|
|
data->renderTarget = NULL;
|
2011-02-08 16:27:52 -08:00
|
|
|
|
2006-07-19 07:18:45 +00:00
|
|
|
/* Set up parameters for rendering */
|
2011-02-19 21:51:21 -08:00
|
|
|
GL_ResetState(renderer);
|
2006-07-19 07:18:45 +00:00
|
|
|
|
|
|
|
return renderer;
|
|
|
|
}
|
|
|
|
|
2011-02-01 19:19:43 -08:00
|
|
|
static void
|
|
|
|
GL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
|
2006-08-06 04:39:13 +00:00
|
|
|
{
|
2011-02-12 19:02:14 -08:00
|
|
|
if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
|
2011-02-01 19:19:43 -08:00
|
|
|
/* Rebind the context to the window area and update matrices */
|
|
|
|
SDL_CurrentContext = NULL;
|
|
|
|
}
|
2006-07-22 19:51:48 +00:00
|
|
|
}
|
|
|
|
|
2006-07-22 19:03:31 +00:00
|
|
|
static __inline__ int
|
|
|
|
power_of_2(int input)
|
|
|
|
{
|
|
|
|
int value = 1;
|
|
|
|
|
|
|
|
while (value < input) {
|
|
|
|
value <<= 1;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2009-11-15 08:01:34 +00:00
|
|
|
static __inline__ SDL_bool
|
|
|
|
convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
|
|
|
|
GLint* internalFormat, GLenum* format, GLenum* type)
|
2006-07-19 07:18:45 +00:00
|
|
|
{
|
2009-11-15 08:01:34 +00:00
|
|
|
switch (pixel_format) {
|
2006-08-05 17:09:42 +00:00
|
|
|
case SDL_PIXELFORMAT_ARGB8888:
|
2009-11-15 08:01:34 +00:00
|
|
|
*internalFormat = GL_RGBA8;
|
|
|
|
*format = GL_BGRA;
|
2011-02-03 00:19:40 -08:00
|
|
|
*type = GL_UNSIGNED_INT_8_8_8_8_REV;
|
2008-12-07 04:02:23 +00:00
|
|
|
break;
|
2011-02-12 00:25:02 -08:00
|
|
|
case SDL_PIXELFORMAT_YV12:
|
|
|
|
case SDL_PIXELFORMAT_IYUV:
|
|
|
|
*internalFormat = GL_LUMINANCE;
|
|
|
|
*format = GL_LUMINANCE;
|
|
|
|
*type = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
2006-07-22 08:33:18 +00:00
|
|
|
default:
|
2009-11-15 08:01:34 +00:00
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
|
2011-03-13 11:18:35 -07:00
|
|
|
static GLenum
|
|
|
|
GetScaleQuality(void)
|
|
|
|
{
|
|
|
|
const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
|
|
|
|
|
|
|
|
if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
|
|
|
|
return GL_NEAREST;
|
|
|
|
} else {
|
|
|
|
return GL_LINEAR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-18 22:45:49 -05:00
|
|
|
static int
|
|
|
|
GL_SetTargetTexture(SDL_Renderer * renderer, SDL_Texture * texture)
|
|
|
|
{
|
|
|
|
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
|
|
|
|
|
|
|
|
GL_TextureData *texturedata;
|
|
|
|
GLenum status;
|
|
|
|
|
|
|
|
if (!renderer) return -1;
|
|
|
|
GL_ActivateRenderer(renderer);
|
|
|
|
|
|
|
|
if (! data->GL_EXT_framebuffer_object_supported) {
|
|
|
|
SDL_Unsupported();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (texture == NULL) {
|
|
|
|
if (data->renderTarget != NULL) {
|
|
|
|
data->renderTarget = NULL;
|
|
|
|
renderer->viewport = data->viewport_copy;
|
|
|
|
data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
|
|
|
data->glMatrixMode(GL_PROJECTION);
|
|
|
|
data->glLoadIdentity();
|
|
|
|
data->glMatrixMode(GL_MODELVIEW);
|
|
|
|
data->glLoadIdentity();
|
|
|
|
data->glViewport(renderer->viewport.x, renderer->viewport.y, renderer->viewport.w, renderer->viewport.h);
|
|
|
|
data->glOrtho(0.0, (GLdouble) renderer->viewport.w, (GLdouble) renderer->viewport.h, 0.0, 0.0, 1.0);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (renderer != texture->renderer) return -1;
|
|
|
|
if (data->renderTarget==NULL) {
|
|
|
|
// Keep a copy of the default viewport to restore when texture==NULL
|
|
|
|
data->viewport_copy = renderer->viewport;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
texturedata = (GL_TextureData *) texture->driverdata;
|
|
|
|
if (!texturedata) {
|
|
|
|
if (texture->native && texture->native->driverdata) {
|
|
|
|
texture = texture->native;
|
|
|
|
texturedata = texture->driverdata;
|
|
|
|
}
|
|
|
|
else return -1;
|
|
|
|
}
|
|
|
|
data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, texturedata->fbo->FBO);
|
|
|
|
/* TODO: check if texture pixel format allows this operation */
|
|
|
|
data->glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, texturedata->type, texturedata->texture, 0);
|
|
|
|
/* Check FBO status */
|
|
|
|
status = data->glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
|
|
|
|
if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->renderTarget = texture;
|
|
|
|
renderer->viewport.x = 0;
|
|
|
|
renderer->viewport.y = 0;
|
|
|
|
renderer->viewport.w = texture->w;
|
|
|
|
renderer->viewport.h = texture->h;
|
|
|
|
data->glMatrixMode(GL_PROJECTION);
|
|
|
|
data->glLoadIdentity();
|
|
|
|
data->glOrtho(0.0, (GLdouble) texture->w, 0.0, (GLdouble) texture->h, 0.0, 1.0);
|
|
|
|
data->glMatrixMode(GL_MODELVIEW);
|
|
|
|
data->glLoadIdentity();
|
|
|
|
data->glViewport(0, 0, texture->w, texture->h);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-15 08:01:34 +00:00
|
|
|
static int
|
|
|
|
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
|
|
|
|
{
|
|
|
|
GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
|
|
|
|
GL_TextureData *data;
|
|
|
|
GLint internalFormat;
|
|
|
|
GLenum format, type;
|
|
|
|
int texture_w, texture_h;
|
2011-03-21 17:15:49 -07:00
|
|
|
GLenum scaleMode;
|
2009-11-15 08:01:34 +00:00
|
|
|
GLenum result;
|
|
|
|
|
2011-02-01 19:19:43 -08:00
|
|
|
GL_ActivateRenderer(renderer);
|
|
|
|
|
2009-11-15 08:01:34 +00:00
|
|
|
if (!convert_format(renderdata, texture->format, &internalFormat,
|
|
|
|
&format, &type)) {
|
2011-01-12 19:33:04 -08:00
|
|
|
SDL_SetError("Texture format %s not supported by OpenGL",
|
|
|
|
SDL_GetPixelFormatName(texture->format));
|
2006-07-22 08:33:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-07-19 07:18:45 +00:00
|
|
|
|
2006-07-22 08:33:18 +00:00
|
|
|
data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
|
2006-07-19 07:18:45 +00:00
|
|
|
if (!data) {
|
|
|
|
SDL_OutOfMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-08-11 20:54:31 +00:00
|
|
|
if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
|
2011-02-12 00:25:02 -08:00
|
|
|
size_t size;
|
2011-02-03 00:19:40 -08:00
|
|
|
data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
|
2011-02-12 00:25:02 -08:00
|
|
|
size = texture->h * data->pitch;
|
|
|
|
if (texture->format == SDL_PIXELFORMAT_YV12 ||
|
|
|
|
texture->format == SDL_PIXELFORMAT_IYUV) {
|
|
|
|
/* Need to add size for the U and V planes */
|
|
|
|
size += (2 * (texture->h * data->pitch) / 4);
|
|
|
|
}
|
2011-02-26 21:39:34 -08:00
|
|
|
data->pixels = SDL_calloc(1, size);
|
2007-08-11 20:54:31 +00:00
|
|
|
if (!data->pixels) {
|
|
|
|
SDL_OutOfMemory();
|
|
|
|
SDL_free(data);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-19 07:18:45 +00:00
|
|
|
texture->driverdata = data;
|
2012-01-18 22:45:49 -05:00
|
|
|
|
|
|
|
if (texture->access == SDL_TEXTUREACCESS_TARGET) {
|
|
|
|
data->fbo = GL_GetFBO(renderdata, texture->w, texture->h);
|
|
|
|
} else {
|
|
|
|
data->fbo = NULL;
|
|
|
|
}
|
2006-07-19 07:18:45 +00:00
|
|
|
|
2006-07-22 23:04:41 +00:00
|
|
|
renderdata->glGetError();
|
|
|
|
renderdata->glGenTextures(1, &data->texture);
|
2012-01-18 22:45:49 -05:00
|
|
|
if ((renderdata->GL_ARB_texture_rectangle_supported)
|
|
|
|
/*&& texture->access != SDL_TEXTUREACCESS_TARGET*/){
|
2006-07-22 21:58:17 +00:00
|
|
|
data->type = GL_TEXTURE_RECTANGLE_ARB;
|
|
|
|
texture_w = texture->w;
|
|
|
|
texture_h = texture->h;
|
2008-12-06 00:56:47 +00:00
|
|
|
data->texw = (GLfloat) texture_w;
|
|
|
|
data->texh = (GLfloat) texture_h;
|
2006-07-22 21:58:17 +00:00
|
|
|
} else {
|
|
|
|
data->type = GL_TEXTURE_2D;
|
|
|
|
texture_w = power_of_2(texture->w);
|
|
|
|
texture_h = power_of_2(texture->h);
|
2008-12-06 00:56:47 +00:00
|
|
|
data->texw = (GLfloat) (texture->w) / texture_w;
|
2006-07-22 21:58:17 +00:00
|
|
|
data->texh = (GLfloat) texture->h / texture_h;
|
|
|
|
}
|
2008-12-06 00:56:47 +00:00
|
|
|
|
2006-07-22 08:33:18 +00:00
|
|
|
data->format = format;
|
|
|
|
data->formattype = type;
|
2011-03-21 17:15:49 -07:00
|
|
|
scaleMode = GetScaleQuality();
|
Date: Fri, 19 Dec 2008 20:17:35 +0100
From: Couriersud
Subject: Re: Aw: Experience using SDL1.3 in sdlmame/Proposal for api additions
> For consistency you'd probably want:
> SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a);
> SDL_SetRenderDrawBlendMode(SDL_BlendMode blendMode);
> SDL_RenderLine(int x1, int y1, int x2, int y2);
> SDL_RenderFill(SDL_Rect *rect);
>
> You probably also want to add API functions query the current state.
>
I have implemented the above api for the opengl, x11, directfb and
software renderers. I have also renamed *TEXTUREBLENDMODE* constants to
BLENDMODE*. The unix build compiles. The windows renderer still needs to
be updated, but I have no windows development machine at hand. Have a
look at the x11 renderer for a sample.
Vector games now run at 90% both on opengl and directfb in comparison to
sdlmame's own opengl renderer. The same applies to raster games.
The diff also includes
a) Changed XDrawRect to XFillRect in x11 renderer
b) A number of changes to fix blending and modulation issues in the
directfb renderer.
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%403346
2008-12-20 12:00:00 +00:00
|
|
|
renderdata->glEnable(data->type);
|
2006-07-22 23:04:41 +00:00
|
|
|
renderdata->glBindTexture(data->type, data->texture);
|
2011-03-21 17:15:49 -07:00
|
|
|
renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER, scaleMode);
|
|
|
|
renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER, scaleMode);
|
2012-01-10 21:00:47 -05:00
|
|
|
/* According to the spec, CLAMP_TO_EDGE is the default for TEXTURE_RECTANGLE
|
|
|
|
and setting it causes an INVALID_ENUM error in the latest NVidia drivers.
|
|
|
|
*/
|
|
|
|
if (data->type != GL_TEXTURE_RECTANGLE_ARB) {
|
|
|
|
renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
|
|
|
|
GL_CLAMP_TO_EDGE);
|
|
|
|
renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
|
|
|
|
GL_CLAMP_TO_EDGE);
|
|
|
|
}
|
2008-12-06 17:43:10 +00:00
|
|
|
#ifdef __MACOSX__
|
2007-08-12 07:02:28 +00:00
|
|
|
#ifndef GL_TEXTURE_STORAGE_HINT_APPLE
|
|
|
|
#define GL_TEXTURE_STORAGE_HINT_APPLE 0x85BC
|
|
|
|
#endif
|
|
|
|
#ifndef STORAGE_CACHED_APPLE
|
|
|
|
#define STORAGE_CACHED_APPLE 0x85BE
|
|
|
|
#endif
|
|
|
|
#ifndef STORAGE_SHARED_APPLE
|
|
|
|
#define STORAGE_SHARED_APPLE 0x85BF
|
|
|
|
#endif
|
|
|
|
if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
|
|
|
|
renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
|
|
|
|
GL_STORAGE_SHARED_APPLE);
|
|
|
|
} else {
|
|
|
|
renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
|
|
|
|
GL_STORAGE_CACHED_APPLE);
|
|
|
|
}
|
2008-01-08 00:10:46 +00:00
|
|
|
if (texture->access == SDL_TEXTUREACCESS_STREAMING
|
2011-02-24 23:42:58 -08:00
|
|
|
&& texture->format == SDL_PIXELFORMAT_ARGB8888
|
|
|
|
&& (texture->w % 8) == 0) {
|
2007-08-12 07:02:28 +00:00
|
|
|
renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
|
2011-02-24 23:42:58 -08:00
|
|
|
renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
|
|
|
renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
|
|
|
|
(data->pitch / SDL_BYTESPERPIXEL(texture->format)));
|
2007-08-12 07:02:28 +00:00
|
|
|
renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
|
|
|
|
texture_h, 0, format, type, data->pixels);
|
2011-02-24 23:42:58 -08:00
|
|
|
renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
|
2011-02-03 00:19:40 -08:00
|
|
|
}
|
|
|
|
else
|
2007-08-12 07:02:28 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
|
|
|
|
texture_h, 0, format, type, NULL);
|
|
|
|
}
|
2009-01-14 06:53:03 +00:00
|
|
|
renderdata->glDisable(data->type);
|
2006-07-22 23:04:41 +00:00
|
|
|
result = renderdata->glGetError();
|
2006-07-22 21:02:57 +00:00
|
|
|
if (result != GL_NO_ERROR) {
|
|
|
|
GL_SetError("glTexImage2D()", result);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-02-12 00:25:02 -08:00
|
|
|
|
|
|
|
if (texture->format == SDL_PIXELFORMAT_YV12 ||
|
|
|
|
texture->format == SDL_PIXELFORMAT_IYUV) {
|
|
|
|
data->yuv = SDL_TRUE;
|
|
|
|
|
|
|
|
renderdata->glGenTextures(1, &data->utexture);
|
|
|
|
renderdata->glGenTextures(1, &data->vtexture);
|
|
|
|
renderdata->glEnable(data->type);
|
|
|
|
|
|
|
|
renderdata->glBindTexture(data->type, data->utexture);
|
2011-03-21 17:15:49 -07:00
|
|
|
renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
|
|
|
|
scaleMode);
|
|
|
|
renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
|
|
|
|
scaleMode);
|
2011-02-12 00:25:02 -08:00
|
|
|
renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
|
|
|
|
GL_CLAMP_TO_EDGE);
|
|
|
|
renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
|
|
|
|
GL_CLAMP_TO_EDGE);
|
|
|
|
renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w/2,
|
|
|
|
texture_h/2, 0, format, type, NULL);
|
|
|
|
|
|
|
|
renderdata->glBindTexture(data->type, data->vtexture);
|
2011-03-21 17:15:49 -07:00
|
|
|
renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
|
|
|
|
scaleMode);
|
|
|
|
renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
|
|
|
|
scaleMode);
|
2011-02-12 00:25:02 -08:00
|
|
|
renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
|
|
|
|
GL_CLAMP_TO_EDGE);
|
|
|
|
renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
|
|
|
|
GL_CLAMP_TO_EDGE);
|
|
|
|
renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w/2,
|
|
|
|
texture_h/2, 0, format, type, NULL);
|
|
|
|
|
|
|
|
renderdata->glDisable(data->type);
|
|
|
|
}
|
2006-07-19 07:18:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
|
|
|
|
const SDL_Rect * rect, const void *pixels, int pitch)
|
|
|
|
{
|
2006-07-22 23:04:41 +00:00
|
|
|
GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
|
2006-07-19 07:18:45 +00:00
|
|
|
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
|
2006-07-22 21:02:57 +00:00
|
|
|
GLenum result;
|
2006-07-19 07:18:45 +00:00
|
|
|
|
2011-02-01 19:19:43 -08:00
|
|
|
GL_ActivateRenderer(renderer);
|
|
|
|
|
2006-07-22 23:04:41 +00:00
|
|
|
renderdata->glGetError();
|
2011-02-24 23:42:58 -08:00
|
|
|
renderdata->glEnable(data->type);
|
|
|
|
renderdata->glBindTexture(data->type, data->texture);
|
2011-02-08 10:38:12 -08:00
|
|
|
renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
|
|
|
renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
|
|
|
|
(pitch / SDL_BYTESPERPIXEL(texture->format)));
|
2006-07-22 23:04:41 +00:00
|
|
|
renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
|
|
|
|
rect->h, data->format, data->formattype,
|
|
|
|
pixels);
|
2011-02-12 00:25:02 -08:00
|
|
|
if (data->yuv) {
|
2011-02-12 00:42:39 -08:00
|
|
|
renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, (pitch / 2));
|
|
|
|
|
2011-02-12 00:25:02 -08:00
|
|
|
/* Skip to the correct offset into the next texture */
|
2011-12-30 18:18:42 -05:00
|
|
|
pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
|
2011-02-12 00:25:02 -08:00
|
|
|
if (texture->format == SDL_PIXELFORMAT_YV12) {
|
|
|
|
renderdata->glBindTexture(data->type, data->vtexture);
|
|
|
|
} else {
|
|
|
|
renderdata->glBindTexture(data->type, data->utexture);
|
|
|
|
}
|
|
|
|
renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
|
|
|
|
rect->w/2, rect->h/2,
|
|
|
|
data->format, data->formattype, pixels);
|
|
|
|
|
|
|
|
/* Skip to the correct offset into the next texture */
|
2011-12-30 18:18:42 -05:00
|
|
|
pixels = (const void*)((const Uint8*)pixels + (rect->h * pitch)/4);
|
2011-02-12 00:25:02 -08:00
|
|
|
if (texture->format == SDL_PIXELFORMAT_YV12) {
|
|
|
|
renderdata->glBindTexture(data->type, data->utexture);
|
|
|
|
} else {
|
|
|
|
renderdata->glBindTexture(data->type, data->vtexture);
|
|
|
|
}
|
|
|
|
renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
|
|
|
|
rect->w/2, rect->h/2,
|
|
|
|
data->format, data->formattype, pixels);
|
|
|
|
}
|
2009-01-14 06:53:03 +00:00
|
|
|
renderdata->glDisable(data->type);
|
2006-07-22 23:04:41 +00:00
|
|
|
result = renderdata->glGetError();
|
2006-07-22 21:02:57 +00:00
|
|
|
if (result != GL_NO_ERROR) {
|
|
|
|
GL_SetError("glTexSubImage2D()", result);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-07-19 07:18:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
|
2011-02-03 00:19:40 -08:00
|
|
|
const SDL_Rect * rect, void **pixels, int *pitch)
|
2006-07-19 07:18:45 +00:00
|
|
|
{
|
|
|
|
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
|
|
|
|
|
2011-02-08 10:38:12 -08:00
|
|
|
data->locked_rect = *rect;
|
|
|
|
*pixels =
|
2006-07-22 08:33:18 +00:00
|
|
|
(void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
|
2011-02-03 00:19:40 -08:00
|
|
|
rect->x * SDL_BYTESPERPIXEL(texture->format));
|
2006-07-22 08:33:18 +00:00
|
|
|
*pitch = data->pitch;
|
2006-07-19 07:18:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
|
|
|
|
{
|
|
|
|
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
|
2011-02-08 10:38:12 -08:00
|
|
|
const SDL_Rect *rect;
|
|
|
|
void *pixels;
|
2006-07-19 07:18:45 +00:00
|
|
|
|
2011-02-08 10:38:12 -08:00
|
|
|
rect = &data->locked_rect;
|
|
|
|
pixels =
|
|
|
|
(void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
|
|
|
|
rect->x * SDL_BYTESPERPIXEL(texture->format));
|
|
|
|
GL_UpdateTexture(renderer, texture, rect, pixels, data->pitch);
|
2006-07-19 07:18:45 +00:00
|
|
|
}
|
|
|
|
|
2011-02-15 13:59:59 -08:00
|
|
|
static int
|
|
|
|
GL_UpdateViewport(SDL_Renderer * renderer)
|
2011-02-07 20:06:26 -08:00
|
|
|
{
|
|
|
|
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
|
|
|
|
|
2011-02-15 13:59:59 -08:00
|
|
|
if (SDL_CurrentContext != data->context) {
|
|
|
|
/* We'll update the viewport after we rebind the context */
|
|
|
|
return 0;
|
|
|
|
}
|
2011-02-07 20:06:26 -08:00
|
|
|
|
2011-02-15 13:59:59 -08:00
|
|
|
data->glViewport(renderer->viewport.x, renderer->viewport.y,
|
|
|
|
renderer->viewport.w, renderer->viewport.h);
|
2011-02-07 20:06:26 -08:00
|
|
|
|
2011-02-15 13:59:59 -08:00
|
|
|
data->glMatrixMode(GL_PROJECTION);
|
|
|
|
data->glLoadIdentity();
|
|
|
|
data->glOrtho((GLdouble) 0,
|
|
|
|
(GLdouble) renderer->viewport.w,
|
|
|
|
(GLdouble) renderer->viewport.h,
|
|
|
|
(GLdouble) 0, 0.0, 1.0);
|
|
|
|
return 0;
|
2011-02-07 20:06:26 -08:00
|
|
|
}
|
|
|
|
|
2011-02-19 21:51:21 -08:00
|
|
|
static void
|
|
|
|
GL_SetShader(GL_RenderData * data, GL_Shader shader)
|
|
|
|
{
|
|
|
|
if (data->shaders && shader != data->current.shader) {
|
|
|
|
GL_SelectShader(data->shaders, shader);
|
|
|
|
data->current.shader = shader;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
GL_SetColor(GL_RenderData * data, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
|
|
|
|
{
|
|
|
|
Uint32 color = ((a << 24) | (r << 16) | (g << 8) | b);
|
|
|
|
|
|
|
|
if (color != data->current.color) {
|
|
|
|
data->glColor4f((GLfloat) r * inv255f,
|
|
|
|
(GLfloat) g * inv255f,
|
|
|
|
(GLfloat) b * inv255f,
|
|
|
|
(GLfloat) a * inv255f);
|
|
|
|
data->current.color = color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-31 07:56:56 +00:00
|
|
|
static void
|
2011-01-31 23:23:57 -08:00
|
|
|
GL_SetBlendMode(GL_RenderData * data, int blendMode)
|
2008-12-31 07:56:56 +00:00
|
|
|
{
|
2011-02-19 21:51:21 -08:00
|
|
|
if (blendMode != data->current.blendMode) {
|
2008-12-31 07:56:56 +00:00
|
|
|
switch (blendMode) {
|
|
|
|
case SDL_BLENDMODE_NONE:
|
|
|
|
data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
|
|
|
|
data->glDisable(GL_BLEND);
|
|
|
|
break;
|
|
|
|
case SDL_BLENDMODE_BLEND:
|
|
|
|
data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
|
|
|
data->glEnable(GL_BLEND);
|
|
|
|
data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
break;
|
|
|
|
case SDL_BLENDMODE_ADD:
|
|
|
|
data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
|
|
|
data->glEnable(GL_BLEND);
|
|
|
|
data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
|
|
|
|
break;
|
2011-02-04 19:50:56 -08:00
|
|
|
case SDL_BLENDMODE_MOD:
|
|
|
|
data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
|
|
|
data->glEnable(GL_BLEND);
|
|
|
|
data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
|
|
|
|
break;
|
2008-12-31 07:56:56 +00:00
|
|
|
}
|
2011-02-19 21:51:21 -08:00
|
|
|
data->current.blendMode = blendMode;
|
2008-12-31 07:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-19 21:51:21 -08:00
|
|
|
static void
|
|
|
|
GL_SetDrawingState(SDL_Renderer * renderer)
|
|
|
|
{
|
|
|
|
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
|
|
|
|
|
|
|
|
GL_ActivateRenderer(renderer);
|
|
|
|
|
2011-02-21 22:26:59 -08:00
|
|
|
GL_SetColor(data, renderer->r,
|
|
|
|
renderer->g,
|
|
|
|
renderer->b,
|
|
|
|
renderer->a);
|
2011-02-19 21:51:21 -08:00
|
|
|
|
|
|
|
GL_SetBlendMode(data, renderer->blendMode);
|
|
|
|
|
|
|
|
GL_SetShader(data, SHADER_SOLID);
|
|
|
|
}
|
|
|
|
|
Date: Fri, 19 Dec 2008 20:17:35 +0100
From: Couriersud
Subject: Re: Aw: Experience using SDL1.3 in sdlmame/Proposal for api additions
> For consistency you'd probably want:
> SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a);
> SDL_SetRenderDrawBlendMode(SDL_BlendMode blendMode);
> SDL_RenderLine(int x1, int y1, int x2, int y2);
> SDL_RenderFill(SDL_Rect *rect);
>
> You probably also want to add API functions query the current state.
>
I have implemented the above api for the opengl, x11, directfb and
software renderers. I have also renamed *TEXTUREBLENDMODE* constants to
BLENDMODE*. The unix build compiles. The windows renderer still needs to
be updated, but I have no windows development machine at hand. Have a
look at the x11 renderer for a sample.
Vector games now run at 90% both on opengl and directfb in comparison to
sdlmame's own opengl renderer. The same applies to raster games.
The diff also includes
a) Changed XDrawRect to XFillRect in x11 renderer
b) A number of changes to fix blending and modulation issues in the
directfb renderer.
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%403346
2008-12-20 12:00:00 +00:00
|
|
|
static int
|
2009-12-23 01:55:00 +00:00
|
|
|
GL_RenderClear(SDL_Renderer * renderer)
|
|
|
|
{
|
|
|
|
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
|
|
|
|
|
2011-02-01 19:19:43 -08:00
|
|
|
GL_ActivateRenderer(renderer);
|
|
|
|
|
2009-12-23 01:55:00 +00:00
|
|
|
data->glClearColor((GLfloat) renderer->r * inv255f,
|
|
|
|
(GLfloat) renderer->g * inv255f,
|
|
|
|
(GLfloat) renderer->b * inv255f,
|
|
|
|
(GLfloat) renderer->a * inv255f);
|
|
|
|
|
|
|
|
data->glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
GL_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
|
|
|
|
int count)
|
2006-07-19 07:18:45 +00:00
|
|
|
{
|
|
|
|
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
|
2009-12-09 15:56:56 +00:00
|
|
|
int i;
|
2008-12-30 04:22:24 +00:00
|
|
|
|
2011-02-19 21:51:21 -08:00
|
|
|
GL_SetDrawingState(renderer);
|
2008-12-30 04:22:24 +00:00
|
|
|
|
|
|
|
data->glBegin(GL_POINTS);
|
2009-12-09 15:56:56 +00:00
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
|
|
|
|
}
|
2008-12-30 04:22:24 +00:00
|
|
|
data->glEnd();
|
Date: Fri, 19 Dec 2008 20:17:35 +0100
From: Couriersud
Subject: Re: Aw: Experience using SDL1.3 in sdlmame/Proposal for api additions
> For consistency you'd probably want:
> SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a);
> SDL_SetRenderDrawBlendMode(SDL_BlendMode blendMode);
> SDL_RenderLine(int x1, int y1, int x2, int y2);
> SDL_RenderFill(SDL_Rect *rect);
>
> You probably also want to add API functions query the current state.
>
I have implemented the above api for the opengl, x11, directfb and
software renderers. I have also renamed *TEXTUREBLENDMODE* constants to
BLENDMODE*. The unix build compiles. The windows renderer still needs to
be updated, but I have no windows development machine at hand. Have a
look at the x11 renderer for a sample.
Vector games now run at 90% both on opengl and directfb in comparison to
sdlmame's own opengl renderer. The same applies to raster games.
The diff also includes
a) Changed XDrawRect to XFillRect in x11 renderer
b) A number of changes to fix blending and modulation issues in the
directfb renderer.
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%403346
2008-12-20 12:00:00 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-21 17:39:41 +00:00
|
|
|
static int
|
2009-12-23 01:55:00 +00:00
|
|
|
GL_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
|
|
|
|
int count)
|
2008-12-21 17:39:41 +00:00
|
|
|
{
|
|
|
|
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
|
2009-12-09 15:56:56 +00:00
|
|
|
int i;
|
2008-12-21 17:39:41 +00:00
|
|
|
|
2011-02-19 21:51:21 -08:00
|
|
|
GL_SetDrawingState(renderer);
|
2008-12-21 17:39:41 +00:00
|
|
|
|
2009-12-09 15:56:56 +00:00
|
|
|
if (count > 2 &&
|
|
|
|
points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
|
|
|
|
data->glBegin(GL_LINE_LOOP);
|
|
|
|
/* GL_LINE_LOOP takes care of the final segment */
|
|
|
|
--count;
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
|
|
|
|
}
|
|
|
|
data->glEnd();
|
|
|
|
} else {
|
2011-01-24 15:46:11 -08:00
|
|
|
#if defined(__APPLE__) || defined(__WIN32__)
|
2010-11-19 21:51:33 -08:00
|
|
|
#else
|
2010-10-16 17:14:04 -07:00
|
|
|
int x1, y1, x2, y2;
|
2010-11-19 21:51:33 -08:00
|
|
|
#endif
|
2010-10-16 17:14:04 -07:00
|
|
|
|
2009-12-09 15:56:56 +00:00
|
|
|
data->glBegin(GL_LINE_STRIP);
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
data->glVertex2f(0.5f + points[i].x, 0.5f + points[i].y);
|
|
|
|
}
|
|
|
|
data->glEnd();
|
|
|
|
|
|
|
|
/* The line is half open, so we need one more point to complete it.
|
|
|
|
* http://www.opengl.org/documentation/specs/version1.1/glspec1.1/node47.html
|
|
|
|
* If we have to, we can use vertical line and horizontal line textures
|
|
|
|
* for vertical and horizontal lines, and then create custom textures
|
|
|
|
* for diagonal lines and software render those. It's terrible, but at
|
|
|
|
* least it would be pixel perfect.
|
|
|
|
*/
|
|
|
|
data->glBegin(GL_POINTS);
|
2011-01-24 15:46:11 -08:00
|
|
|
#if defined(__APPLE__) || defined(__WIN32__)
|
2009-12-09 15:56:56 +00:00
|
|
|
/* Mac OS X and Windows seem to always leave the second point open */
|
|
|
|
data->glVertex2f(0.5f + points[count-1].x, 0.5f + points[count-1].y);
|
2009-11-21 07:22:59 +00:00
|
|
|
#else
|
2009-12-09 15:56:56 +00:00
|
|
|
/* Linux seems to leave the right-most or bottom-most point open */
|
2010-10-16 17:14:04 -07:00
|
|
|
x1 = points[0].x;
|
|
|
|
y1 = points[0].y;
|
|
|
|
x2 = points[count-1].x;
|
|
|
|
y2 = points[count-1].y;
|
2009-12-09 15:56:56 +00:00
|
|
|
|
|
|
|
if (x1 > x2) {
|
|
|
|
data->glVertex2f(0.5f + x1, 0.5f + y1);
|
|
|
|
} else if (x2 > x1) {
|
|
|
|
data->glVertex2f(0.5f + x2, 0.5f + y2);
|
|
|
|
} else if (y1 > y2) {
|
|
|
|
data->glVertex2f(0.5f + x1, 0.5f + y1);
|
|
|
|
} else if (y2 > y1) {
|
|
|
|
data->glVertex2f(0.5f + x2, 0.5f + y2);
|
|
|
|
}
|
2009-11-21 07:22:59 +00:00
|
|
|
#endif
|
2009-12-09 15:56:56 +00:00
|
|
|
data->glEnd();
|
|
|
|
}
|
2009-11-19 04:59:19 +00:00
|
|
|
|
2008-12-21 17:39:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-23 01:55:00 +00:00
|
|
|
static int
|
2011-02-15 13:59:59 -08:00
|
|
|
GL_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect * rects, int count)
|
Date: Fri, 19 Dec 2008 20:17:35 +0100
From: Couriersud
Subject: Re: Aw: Experience using SDL1.3 in sdlmame/Proposal for api additions
> For consistency you'd probably want:
> SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a);
> SDL_SetRenderDrawBlendMode(SDL_BlendMode blendMode);
> SDL_RenderLine(int x1, int y1, int x2, int y2);
> SDL_RenderFill(SDL_Rect *rect);
>
> You probably also want to add API functions query the current state.
>
I have implemented the above api for the opengl, x11, directfb and
software renderers. I have also renamed *TEXTUREBLENDMODE* constants to
BLENDMODE*. The unix build compiles. The windows renderer still needs to
be updated, but I have no windows development machine at hand. Have a
look at the x11 renderer for a sample.
Vector games now run at 90% both on opengl and directfb in comparison to
sdlmame's own opengl renderer. The same applies to raster games.
The diff also includes
a) Changed XDrawRect to XFillRect in x11 renderer
b) A number of changes to fix blending and modulation issues in the
directfb renderer.
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%403346
2008-12-20 12:00:00 +00:00
|
|
|
{
|
|
|
|
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
|
2009-12-09 15:56:56 +00:00
|
|
|
int i;
|
Date: Fri, 19 Dec 2008 20:17:35 +0100
From: Couriersud
Subject: Re: Aw: Experience using SDL1.3 in sdlmame/Proposal for api additions
> For consistency you'd probably want:
> SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a);
> SDL_SetRenderDrawBlendMode(SDL_BlendMode blendMode);
> SDL_RenderLine(int x1, int y1, int x2, int y2);
> SDL_RenderFill(SDL_Rect *rect);
>
> You probably also want to add API functions query the current state.
>
I have implemented the above api for the opengl, x11, directfb and
software renderers. I have also renamed *TEXTUREBLENDMODE* constants to
BLENDMODE*. The unix build compiles. The windows renderer still needs to
be updated, but I have no windows development machine at hand. Have a
look at the x11 renderer for a sample.
Vector games now run at 90% both on opengl and directfb in comparison to
sdlmame's own opengl renderer. The same applies to raster games.
The diff also includes
a) Changed XDrawRect to XFillRect in x11 renderer
b) A number of changes to fix blending and modulation issues in the
directfb renderer.
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%403346
2008-12-20 12:00:00 +00:00
|
|
|
|
2011-02-19 21:51:21 -08:00
|
|
|
GL_SetDrawingState(renderer);
|
2008-12-31 07:56:56 +00:00
|
|
|
|
2009-12-09 15:56:56 +00:00
|
|
|
for (i = 0; i < count; ++i) {
|
2011-02-15 13:59:59 -08:00
|
|
|
const SDL_Rect *rect = &rects[i];
|
2009-12-09 15:56:56 +00:00
|
|
|
|
|
|
|
data->glRecti(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
|
|
|
|
}
|
2008-12-21 17:39:41 +00:00
|
|
|
|
2006-07-19 07:18:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
|
2006-08-28 03:17:39 +00:00
|
|
|
const SDL_Rect * srcrect, const SDL_Rect * dstrect)
|
2006-07-19 07:18:45 +00:00
|
|
|
{
|
|
|
|
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
|
|
|
|
GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
|
|
|
|
int minx, miny, maxx, maxy;
|
|
|
|
GLfloat minu, maxu, minv, maxv;
|
|
|
|
|
2011-02-01 19:19:43 -08:00
|
|
|
GL_ActivateRenderer(renderer);
|
|
|
|
|
Date: Fri, 19 Dec 2008 20:17:35 +0100
From: Couriersud
Subject: Re: Aw: Experience using SDL1.3 in sdlmame/Proposal for api additions
> For consistency you'd probably want:
> SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a);
> SDL_SetRenderDrawBlendMode(SDL_BlendMode blendMode);
> SDL_RenderLine(int x1, int y1, int x2, int y2);
> SDL_RenderFill(SDL_Rect *rect);
>
> You probably also want to add API functions query the current state.
>
I have implemented the above api for the opengl, x11, directfb and
software renderers. I have also renamed *TEXTUREBLENDMODE* constants to
BLENDMODE*. The unix build compiles. The windows renderer still needs to
be updated, but I have no windows development machine at hand. Have a
look at the x11 renderer for a sample.
Vector games now run at 90% both on opengl and directfb in comparison to
sdlmame's own opengl renderer. The same applies to raster games.
The diff also includes
a) Changed XDrawRect to XFillRect in x11 renderer
b) A number of changes to fix blending and modulation issues in the
directfb renderer.
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%403346
2008-12-20 12:00:00 +00:00
|
|
|
data->glEnable(texturedata->type);
|
2011-02-12 00:25:02 -08:00
|
|
|
if (texturedata->yuv) {
|
|
|
|
data->glActiveTextureARB(GL_TEXTURE2_ARB);
|
|
|
|
data->glBindTexture(texturedata->type, texturedata->vtexture);
|
2011-02-19 21:51:21 -08:00
|
|
|
|
2011-02-12 00:25:02 -08:00
|
|
|
data->glActiveTextureARB(GL_TEXTURE1_ARB);
|
|
|
|
data->glBindTexture(texturedata->type, texturedata->utexture);
|
2011-02-19 21:51:21 -08:00
|
|
|
|
2011-02-12 00:25:02 -08:00
|
|
|
data->glActiveTextureARB(GL_TEXTURE0_ARB);
|
|
|
|
}
|
2006-07-22 23:04:41 +00:00
|
|
|
data->glBindTexture(texturedata->type, texturedata->texture);
|
|
|
|
|
2006-08-28 03:17:39 +00:00
|
|
|
if (texture->modMode) {
|
2011-02-19 21:51:21 -08:00
|
|
|
GL_SetColor(data, texture->r, texture->g, texture->b, texture->a);
|
2006-08-28 03:17:39 +00:00
|
|
|
} else {
|
2011-02-19 21:51:21 -08:00
|
|
|
GL_SetColor(data, 255, 255, 255, 255);
|
2006-08-28 03:17:39 +00:00
|
|
|
}
|
|
|
|
|
2011-01-31 23:23:57 -08:00
|
|
|
GL_SetBlendMode(data, texture->blendMode);
|
2011-02-19 21:51:21 -08:00
|
|
|
|
2011-02-12 00:25:02 -08:00
|
|
|
if (texturedata->yuv) {
|
2011-02-19 21:51:21 -08:00
|
|
|
GL_SetShader(data, SHADER_YV12);
|
2011-02-12 00:25:02 -08:00
|
|
|
} else {
|
2011-02-19 21:51:21 -08:00
|
|
|
GL_SetShader(data, SHADER_RGB);
|
2011-02-12 00:25:02 -08:00
|
|
|
}
|
2009-11-21 05:29:31 +00:00
|
|
|
|
2011-02-19 21:51:21 -08:00
|
|
|
minx = dstrect->x;
|
|
|
|
miny = dstrect->y;
|
|
|
|
maxx = dstrect->x + dstrect->w;
|
|
|
|
maxy = dstrect->y + dstrect->h;
|
|
|
|
|
|
|
|
minu = (GLfloat) srcrect->x / texture->w;
|
|
|
|
minu *= texturedata->texw;
|
|
|
|
maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
|
|
|
|
maxu *= texturedata->texw;
|
|
|
|
minv = (GLfloat) srcrect->y / texture->h;
|
|
|
|
minv *= texturedata->texh;
|
|
|
|
maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
|
|
|
|
maxv *= texturedata->texh;
|
|
|
|
|
2006-07-22 23:04:41 +00:00
|
|
|
data->glBegin(GL_TRIANGLE_STRIP);
|
|
|
|
data->glTexCoord2f(minu, minv);
|
2009-11-21 06:34:43 +00:00
|
|
|
data->glVertex2f((GLfloat) minx, (GLfloat) miny);
|
2006-07-22 23:04:41 +00:00
|
|
|
data->glTexCoord2f(maxu, minv);
|
2009-11-21 06:34:43 +00:00
|
|
|
data->glVertex2f((GLfloat) maxx, (GLfloat) miny);
|
2006-07-22 23:04:41 +00:00
|
|
|
data->glTexCoord2f(minu, maxv);
|
2009-11-21 06:34:43 +00:00
|
|
|
data->glVertex2f((GLfloat) minx, (GLfloat) maxy);
|
2006-07-22 23:04:41 +00:00
|
|
|
data->glTexCoord2f(maxu, maxv);
|
2009-11-21 06:34:43 +00:00
|
|
|
data->glVertex2f((GLfloat) maxx, (GLfloat) maxy);
|
2006-07-22 23:04:41 +00:00
|
|
|
data->glEnd();
|
2006-07-19 07:18:45 +00:00
|
|
|
|
Date: Fri, 19 Dec 2008 20:17:35 +0100
From: Couriersud
Subject: Re: Aw: Experience using SDL1.3 in sdlmame/Proposal for api additions
> For consistency you'd probably want:
> SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a);
> SDL_SetRenderDrawBlendMode(SDL_BlendMode blendMode);
> SDL_RenderLine(int x1, int y1, int x2, int y2);
> SDL_RenderFill(SDL_Rect *rect);
>
> You probably also want to add API functions query the current state.
>
I have implemented the above api for the opengl, x11, directfb and
software renderers. I have also renamed *TEXTUREBLENDMODE* constants to
BLENDMODE*. The unix build compiles. The windows renderer still needs to
be updated, but I have no windows development machine at hand. Have a
look at the x11 renderer for a sample.
Vector games now run at 90% both on opengl and directfb in comparison to
sdlmame's own opengl renderer. The same applies to raster games.
The diff also includes
a) Changed XDrawRect to XFillRect in x11 renderer
b) A number of changes to fix blending and modulation issues in the
directfb renderer.
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%403346
2008-12-20 12:00:00 +00:00
|
|
|
data->glDisable(texturedata->type);
|
|
|
|
|
2006-07-19 07:18:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-15 04:58:03 +00:00
|
|
|
static int
|
|
|
|
GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
|
2009-11-16 07:13:07 +00:00
|
|
|
Uint32 pixel_format, void * pixels, int pitch)
|
2009-11-15 04:58:03 +00:00
|
|
|
{
|
2009-11-15 08:01:34 +00:00
|
|
|
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
|
2010-01-21 06:21:52 +00:00
|
|
|
SDL_Window *window = renderer->window;
|
2011-03-10 01:03:43 -08:00
|
|
|
Uint32 temp_format = SDL_PIXELFORMAT_ARGB8888;
|
|
|
|
void *temp_pixels;
|
|
|
|
int temp_pitch;
|
2009-11-15 08:01:34 +00:00
|
|
|
GLint internalFormat;
|
|
|
|
GLenum format, type;
|
2009-11-16 07:13:07 +00:00
|
|
|
Uint8 *src, *dst, *tmp;
|
2011-02-02 14:34:54 -08:00
|
|
|
int w, h, length, rows;
|
2011-03-10 01:03:43 -08:00
|
|
|
int status;
|
2009-11-15 08:01:34 +00:00
|
|
|
|
2011-02-01 19:19:43 -08:00
|
|
|
GL_ActivateRenderer(renderer);
|
|
|
|
|
2011-03-10 01:03:43 -08:00
|
|
|
temp_pitch = rect->w * SDL_BYTESPERPIXEL(temp_format);
|
|
|
|
temp_pixels = SDL_malloc(rect->h * temp_pitch);
|
|
|
|
if (!temp_pixels) {
|
|
|
|
SDL_OutOfMemory();
|
2009-11-15 08:01:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-03-10 01:03:43 -08:00
|
|
|
convert_format(data, temp_format, &internalFormat, &format, &type);
|
|
|
|
|
2011-02-02 14:34:54 -08:00
|
|
|
SDL_GetWindowSize(window, &w, &h);
|
|
|
|
|
2009-11-18 07:35:00 +00:00
|
|
|
data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
|
|
|
data->glPixelStorei(GL_PACK_ROW_LENGTH,
|
2011-03-10 01:03:43 -08:00
|
|
|
(temp_pitch / SDL_BYTESPERPIXEL(temp_format)));
|
2009-11-15 08:01:34 +00:00
|
|
|
|
2011-02-02 14:34:54 -08:00
|
|
|
data->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
|
2011-03-10 01:03:43 -08:00
|
|
|
format, type, temp_pixels);
|
2009-11-16 07:13:07 +00:00
|
|
|
|
|
|
|
/* Flip the rows to be top-down */
|
2011-03-10 01:03:43 -08:00
|
|
|
length = rect->w * SDL_BYTESPERPIXEL(temp_format);
|
|
|
|
src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
|
|
|
|
dst = (Uint8*)temp_pixels;
|
2009-11-16 07:13:07 +00:00
|
|
|
tmp = SDL_stack_alloc(Uint8, length);
|
|
|
|
rows = rect->h / 2;
|
|
|
|
while (rows--) {
|
|
|
|
SDL_memcpy(tmp, dst, length);
|
|
|
|
SDL_memcpy(dst, src, length);
|
|
|
|
SDL_memcpy(src, tmp, length);
|
2011-03-10 01:03:43 -08:00
|
|
|
dst += temp_pitch;
|
|
|
|
src -= temp_pitch;
|
2009-11-16 07:13:07 +00:00
|
|
|
}
|
|
|
|
SDL_stack_free(tmp);
|
2009-11-17 06:50:29 +00:00
|
|
|
|
2011-03-10 01:03:43 -08:00
|
|
|
status = SDL_ConvertPixels(rect->w, rect->h,
|
|
|
|
temp_format, temp_pixels, temp_pitch,
|
|
|
|
pixel_format, pixels, pitch);
|
|
|
|
SDL_free(temp_pixels);
|
|
|
|
|
|
|
|
return status;
|
2009-11-15 04:58:03 +00:00
|
|
|
}
|
|
|
|
|
2006-07-19 07:18:45 +00:00
|
|
|
static void
|
|
|
|
GL_RenderPresent(SDL_Renderer * renderer)
|
|
|
|
{
|
2011-02-01 19:19:43 -08:00
|
|
|
GL_ActivateRenderer(renderer);
|
|
|
|
|
2006-07-19 07:18:45 +00:00
|
|
|
SDL_GL_SwapWindow(renderer->window);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
|
|
|
|
{
|
2006-07-22 23:04:41 +00:00
|
|
|
GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
|
2006-07-19 07:18:45 +00:00
|
|
|
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
|
|
|
|
|
2011-02-01 19:19:43 -08:00
|
|
|
GL_ActivateRenderer(renderer);
|
|
|
|
|
2006-07-19 07:18:45 +00:00
|
|
|
if (!data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (data->texture) {
|
2006-07-22 23:04:41 +00:00
|
|
|
renderdata->glDeleteTextures(1, &data->texture);
|
2006-07-22 08:33:18 +00:00
|
|
|
}
|
2011-02-12 00:25:02 -08:00
|
|
|
if (data->yuv) {
|
|
|
|
renderdata->glDeleteTextures(1, &data->utexture);
|
|
|
|
renderdata->glDeleteTextures(1, &data->vtexture);
|
|
|
|
}
|
2006-07-22 08:33:18 +00:00
|
|
|
if (data->pixels) {
|
|
|
|
SDL_free(data->pixels);
|
2006-07-19 07:18:45 +00:00
|
|
|
}
|
|
|
|
SDL_free(data);
|
|
|
|
texture->driverdata = NULL;
|
|
|
|
}
|
|
|
|
|
2006-08-07 05:24:13 +00:00
|
|
|
static void
|
2006-07-19 07:18:45 +00:00
|
|
|
GL_DestroyRenderer(SDL_Renderer * renderer)
|
|
|
|
{
|
|
|
|
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
|
|
|
|
|
|
|
|
if (data) {
|
2011-07-16 14:59:12 -07:00
|
|
|
if (data->shaders) {
|
|
|
|
GL_DestroyShaderContext(data->shaders);
|
|
|
|
}
|
2006-07-22 08:33:18 +00:00
|
|
|
if (data->context) {
|
2012-01-18 22:45:49 -05:00
|
|
|
while (data->framebuffers) {
|
|
|
|
GL_FBOList *nextnode = data->framebuffers->next;
|
|
|
|
/* delete the framebuffer object */
|
|
|
|
data->glDeleteFramebuffersEXT(1, &data->framebuffers->FBO);
|
|
|
|
SDL_free(data->framebuffers);
|
|
|
|
data->framebuffers = nextnode;
|
|
|
|
}
|
2008-03-14 18:17:49 +00:00
|
|
|
/* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
|
2006-07-22 08:33:18 +00:00
|
|
|
SDL_GL_DeleteContext(data->context);
|
2006-07-19 07:18:45 +00:00
|
|
|
}
|
|
|
|
SDL_free(data);
|
|
|
|
}
|
|
|
|
SDL_free(renderer);
|
|
|
|
}
|
|
|
|
|
2011-02-08 10:04:09 -08:00
|
|
|
#endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
|
2006-07-19 07:18:45 +00:00
|
|
|
|
|
|
|
/* vi: set ts=4 sw=4 expandtab: */
|