Beginning of a framework for OpenGL shaders
authorSam Lantinga <slouken@libsdl.org>
Tue, 08 Feb 2011 16:27:52 -0800
changeset 5228811beeb698f9
parent 5227 9c0c4d767ef6
child 5229 c015d3e63631
Beginning of a framework for OpenGL shaders
src/render/opengl/SDL_render_gl.c
src/render/opengl/SDL_shaders_gl.c
src/render/opengl/SDL_shaders_gl.h
     1.1 --- a/src/render/opengl/SDL_render_gl.c	Tue Feb 08 10:38:12 2011 -0800
     1.2 +++ b/src/render/opengl/SDL_render_gl.c	Tue Feb 08 16:27:52 2011 -0800
     1.3 @@ -25,6 +25,7 @@
     1.4  
     1.5  #include "SDL_opengl.h"
     1.6  #include "../SDL_sysrender.h"
     1.7 +#include "SDL_shaders_gl.h"
     1.8  
     1.9  #ifdef __MACOSX__
    1.10  #include <OpenGL/OpenGL.h>
    1.11 @@ -94,6 +95,15 @@
    1.12  
    1.13      void (*glTextureRangeAPPLE) (GLenum target, GLsizei length,
    1.14                                   const GLvoid * pointer);
    1.15 +
    1.16 +    /* Multitexture support */
    1.17 +    SDL_bool GL_ARB_multitexture_supported;
    1.18 +    PFNGLACTIVETEXTUREARBPROC glActiveTextureARB;
    1.19 +    int num_texture_units;
    1.20 +
    1.21 +    /* Shader support */
    1.22 +    GL_ShaderContext *shaders;
    1.23 +
    1.24  } GL_RenderData;
    1.25  
    1.26  typedef struct
    1.27 @@ -262,6 +272,25 @@
    1.28              SDL_GL_GetProcAddress("glTextureRangeAPPLE");
    1.29      }
    1.30  
    1.31 +    /* Check for multitexture support */
    1.32 +    if (SDL_GL_ExtensionSupported("GL_ARB_multitexture")) {
    1.33 +        data->glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC) SDL_GL_GetProcAddress("glActiveTextureARB");
    1.34 +        if (data->glActiveTextureARB) {
    1.35 +            data->GL_ARB_multitexture_supported = SDL_TRUE;
    1.36 +            data->glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &data->num_texture_units);
    1.37 +        }
    1.38 +    }
    1.39 +
    1.40 +    /* Check for shader support */
    1.41 +    //data->shaders = GL_CreateShaderContext();
    1.42 +
    1.43 +#if 0
    1.44 +    /* We support YV12 textures using 3 textures and a shader */
    1.45 +    if (data->shaders && data->num_texture_units >= 3) {
    1.46 +        renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_YV12;
    1.47 +    }
    1.48 +#endif
    1.49 +
    1.50      /* Set up parameters for rendering */
    1.51      data->blendMode = -1;
    1.52      data->glDisable(GL_DEPTH_TEST);
    1.53 @@ -579,6 +608,7 @@
    1.54      GL_ActivateRenderer(renderer);
    1.55  
    1.56      GL_SetBlendMode(data, renderer->blendMode);
    1.57 +    GL_SelectShader(data->shaders, SHADER_SOLID);
    1.58  
    1.59      data->glColor4f((GLfloat) renderer->r * inv255f,
    1.60                      (GLfloat) renderer->g * inv255f,
    1.61 @@ -604,6 +634,7 @@
    1.62      GL_ActivateRenderer(renderer);
    1.63  
    1.64      GL_SetBlendMode(data, renderer->blendMode);
    1.65 +    GL_SelectShader(data->shaders, SHADER_SOLID);
    1.66  
    1.67      data->glColor4f((GLfloat) renderer->r * inv255f,
    1.68                      (GLfloat) renderer->g * inv255f,
    1.69 @@ -674,6 +705,7 @@
    1.70      GL_ActivateRenderer(renderer);
    1.71  
    1.72      GL_SetBlendMode(data, renderer->blendMode);
    1.73 +    GL_SelectShader(data->shaders, SHADER_SOLID);
    1.74  
    1.75      data->glColor4f((GLfloat) renderer->r * inv255f,
    1.76                      (GLfloat) renderer->g * inv255f,
    1.77 @@ -727,6 +759,7 @@
    1.78      }
    1.79  
    1.80      GL_SetBlendMode(data, texture->blendMode);
    1.81 +    GL_SelectShader(data->shaders, SHADER_RGB);
    1.82  
    1.83      data->glBegin(GL_TRIANGLE_STRIP);
    1.84      data->glTexCoord2f(minu, minv);
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/render/opengl/SDL_shaders_gl.c	Tue Feb 08 16:27:52 2011 -0800
     2.3 @@ -0,0 +1,304 @@
     2.4 +/*
     2.5 +    SDL - Simple DirectMedia Layer
     2.6 +    Copyright (C) 1997-2010 Sam Lantinga
     2.7 +
     2.8 +    This library is free software; you can redistribute it and/or
     2.9 +    modify it under the terms of the GNU Lesser General Public
    2.10 +    License as published by the Free Software Foundation; either
    2.11 +    version 2.1 of the License, or (at your option) any later version.
    2.12 +
    2.13 +    This library is distributed in the hope that it will be useful,
    2.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    2.16 +    Lesser General Public License for more details.
    2.17 +
    2.18 +    You should have received a copy of the GNU Lesser General Public
    2.19 +    License along with this library; if not, write to the Free Software
    2.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    2.21 +
    2.22 +    Sam Lantinga
    2.23 +    slouken@libsdl.org
    2.24 +*/
    2.25 +#include "SDL_config.h"
    2.26 +
    2.27 +#if SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED
    2.28 +
    2.29 +#include "SDL_stdinc.h"
    2.30 +#include "SDL_log.h"
    2.31 +#include "SDL_opengl.h"
    2.32 +#include "SDL_video.h"
    2.33 +#include "SDL_shaders_gl.h"
    2.34 +
    2.35 +/* OpenGL shader implementation */
    2.36 +
    2.37 +typedef struct
    2.38 +{
    2.39 +    GLenum program;
    2.40 +    GLenum vert_shader;
    2.41 +    GLenum frag_shader;
    2.42 +} GL_ShaderData;
    2.43 +
    2.44 +struct GL_ShaderContext
    2.45 +{
    2.46 +    GLenum (*glGetError)(void);
    2.47 +
    2.48 +    PFNGLATTACHOBJECTARBPROC glAttachObjectARB;
    2.49 +    PFNGLCOMPILESHADERARBPROC glCompileShaderARB;
    2.50 +    PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObjectARB;
    2.51 +    PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObjectARB;
    2.52 +    PFNGLDELETEOBJECTARBPROC glDeleteObjectARB;
    2.53 +    PFNGLGETINFOLOGARBPROC glGetInfoLogARB;
    2.54 +    PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB;
    2.55 +    PFNGLGETUNIFORMLOCATIONARBPROC glGetUniformLocationARB;
    2.56 +    PFNGLLINKPROGRAMARBPROC glLinkProgramARB;
    2.57 +    PFNGLSHADERSOURCEARBPROC glShaderSourceARB;
    2.58 +    PFNGLUNIFORM1IARBPROC glUniform1iARB;
    2.59 +    PFNGLUNIFORM1FARBPROC glUniform1fARB;
    2.60 +    PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObjectARB;
    2.61 +
    2.62 +    GL_Shader current_shader;
    2.63 +    GL_ShaderData shaders[NUM_SHADERS];
    2.64 +};
    2.65 +
    2.66 +static const char *shader_source[NUM_SHADERS][2] =
    2.67 +{
    2.68 +    /* SHADER_NONE */
    2.69 +    { NULL, NULL },
    2.70 +
    2.71 +    /* SHADER_SOLID */
    2.72 +    {
    2.73 +        /* vertex shader */
    2.74 +        " \
    2.75 +varying vec4 v_color; \
    2.76 + \
    2.77 +void main() \
    2.78 +{ \
    2.79 +    gl_Position = ftransform(); \
    2.80 +    v_color = gl_Color; \
    2.81 +} \
    2.82 +",
    2.83 +        /* fragment shader */
    2.84 +        " \
    2.85 +varying vec4 v_color; \
    2.86 + \
    2.87 +void main() \
    2.88 +{ \
    2.89 +    gl_FragColor = v_color; \
    2.90 +} \
    2.91 +"
    2.92 +    },
    2.93 +
    2.94 +    /* SHADER_RGB */
    2.95 +    {
    2.96 +        /* vertex shader */
    2.97 +        " \
    2.98 +varying vec4 v_color; \
    2.99 +varying vec2 v_texCoord; \
   2.100 + \
   2.101 +void main() \
   2.102 +{ \
   2.103 +    gl_Position = ftransform(); \
   2.104 +    v_color = gl_Color; \
   2.105 +    v_texCoord = vec2(gl_MultiTexCoord0); \
   2.106 +} \
   2.107 +",
   2.108 +        /* fragment shader */
   2.109 +        " \
   2.110 +varying vec4 v_color; \
   2.111 +varying vec2 v_texCoord; \
   2.112 +uniform sampler2D tex0; \
   2.113 + \
   2.114 +void main() \
   2.115 +{ \
   2.116 +    gl_FragColor = texture2D(tex0, v_texCoord) * v_color; \
   2.117 +} \
   2.118 +"
   2.119 +    },
   2.120 +};
   2.121 +
   2.122 +static SDL_bool
   2.123 +CompileShader(GL_ShaderContext *ctx, GLenum shader, const char *source)
   2.124 +{
   2.125 +    GLint status;
   2.126 +
   2.127 +    ctx->glShaderSourceARB(shader, 1, &source, NULL);
   2.128 +    ctx->glCompileShaderARB(shader);
   2.129 +    ctx->glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
   2.130 +    if (status == 0) {
   2.131 +        GLint length;
   2.132 +        char *info;
   2.133 +
   2.134 +        ctx->glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
   2.135 +        info = SDL_stack_alloc(char, length+1);
   2.136 +        ctx->glGetInfoLogARB(shader, length, NULL, info);
   2.137 +        SDL_LogError(SDL_LOG_CATEGORY_RENDER,
   2.138 +            "Failed to compile shader:\n%s\n%s", source, info);
   2.139 +fprintf(stderr, "Failed to compile shader:\n%s\n%s", source, info);
   2.140 +        SDL_stack_free(info);
   2.141 +
   2.142 +        return SDL_FALSE;
   2.143 +    } else {
   2.144 +        return SDL_TRUE;
   2.145 +    }
   2.146 +}
   2.147 +
   2.148 +static SDL_bool
   2.149 +CompileShaderProgram(GL_ShaderContext *ctx, int index, GL_ShaderData *data)
   2.150 +{
   2.151 +    const int num_tmus_bound = 4;
   2.152 +    GLint status;
   2.153 +    int i;
   2.154 +    GLint location;
   2.155 +
   2.156 +    if (index == SHADER_NONE) {
   2.157 +        return SDL_TRUE;
   2.158 +    }
   2.159 +
   2.160 +    ctx->glGetError();
   2.161 +
   2.162 +    /* Create one program object to rule them all */
   2.163 +    data->program = ctx->glCreateProgramObjectARB();
   2.164 +
   2.165 +    /* Create the vertex shader */
   2.166 +    data->vert_shader = ctx->glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
   2.167 +    if (!CompileShader(ctx, data->vert_shader, shader_source[index][0])) {
   2.168 +        return SDL_FALSE;
   2.169 +    }
   2.170 +
   2.171 +    /* Create the fragment shader */
   2.172 +    data->frag_shader = ctx->glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
   2.173 +    if (!CompileShader(ctx, data->frag_shader, shader_source[index][1])) {
   2.174 +        return SDL_FALSE;
   2.175 +    }
   2.176 +
   2.177 +    /* ... and in the darkness bind them */
   2.178 +    ctx->glAttachObjectARB(data->program, data->vert_shader);
   2.179 +    ctx->glAttachObjectARB(data->program, data->frag_shader);
   2.180 +    ctx->glLinkProgramARB(data->program);
   2.181 +
   2.182 +    /* Set up some uniform variables */
   2.183 +    ctx->glUseProgramObjectARB(data->program);
   2.184 +    for (i = 0; i < num_tmus_bound; ++i) {
   2.185 +        char tex_name[5];
   2.186 +        SDL_snprintf(tex_name, SDL_arraysize(tex_name), "tex%d", i);
   2.187 +        location = ctx->glGetUniformLocationARB(data->program, tex_name);
   2.188 +        if (location >= 0) {
   2.189 +            ctx->glUniform1iARB(location, 1);
   2.190 +        }
   2.191 +    }
   2.192 +    ctx->glUseProgramObjectARB(0);
   2.193 +    
   2.194 +    return (ctx->glGetError() == GL_NO_ERROR);
   2.195 +}
   2.196 +
   2.197 +static void
   2.198 +DestroyShaderProgram(GL_ShaderContext *ctx, GL_ShaderData *data)
   2.199 +{
   2.200 +    if (index == SHADER_NONE) {
   2.201 +        return;
   2.202 +    }
   2.203 +
   2.204 +    ctx->glDeleteObjectARB(data->vert_shader);
   2.205 +    ctx->glDeleteObjectARB(data->frag_shader);
   2.206 +    ctx->glDeleteObjectARB(data->program);
   2.207 +}
   2.208 +
   2.209 +GL_ShaderContext *
   2.210 +GL_CreateShaderContext()
   2.211 +{
   2.212 +    GL_ShaderContext *ctx;
   2.213 +    SDL_bool shaders_supported;
   2.214 +    int i;
   2.215 +
   2.216 +    ctx = (GL_ShaderContext *)SDL_calloc(1, sizeof(*ctx));
   2.217 +    if (!ctx) {
   2.218 +        return NULL;
   2.219 +    }
   2.220 +
   2.221 +    /* Check for shader support */
   2.222 +    shaders_supported = SDL_FALSE;
   2.223 +    if (SDL_GL_ExtensionSupported("GL_ARB_shader_objects") &&
   2.224 +        SDL_GL_ExtensionSupported("GL_ARB_shading_language_100") &&
   2.225 +        SDL_GL_ExtensionSupported("GL_ARB_vertex_shader") &&
   2.226 +        SDL_GL_ExtensionSupported("GL_ARB_fragment_shader")) {
   2.227 +        ctx->glGetError = (GLenum (*)(void)) SDL_GL_GetProcAddress("glGetError");
   2.228 +        ctx->glAttachObjectARB = (PFNGLATTACHOBJECTARBPROC) SDL_GL_GetProcAddress("glAttachObjectARB");
   2.229 +        ctx->glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC) SDL_GL_GetProcAddress("glCompileShaderARB");
   2.230 +        ctx->glCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glCreateProgramObjectARB");
   2.231 +        ctx->glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC) SDL_GL_GetProcAddress("glCreateShaderObjectARB");
   2.232 +        ctx->glDeleteObjectARB = (PFNGLDELETEOBJECTARBPROC) SDL_GL_GetProcAddress("glDeleteObjectARB");
   2.233 +        ctx->glGetInfoLogARB = (PFNGLGETINFOLOGARBPROC) SDL_GL_GetProcAddress("glGetInfoLogARB");
   2.234 +        ctx->glGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC) SDL_GL_GetProcAddress("glGetObjectParameterivARB");
   2.235 +        ctx->glGetUniformLocationARB = (PFNGLGETUNIFORMLOCATIONARBPROC) SDL_GL_GetProcAddress("glGetUniformLocationARB");
   2.236 +        ctx->glLinkProgramARB = (PFNGLLINKPROGRAMARBPROC) SDL_GL_GetProcAddress("glLinkProgramARB");
   2.237 +        ctx->glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC) SDL_GL_GetProcAddress("glShaderSourceARB");
   2.238 +        ctx->glUniform1iARB = (PFNGLUNIFORM1IARBPROC) SDL_GL_GetProcAddress("glUniform1iARB");
   2.239 +        ctx->glUniform1fARB = (PFNGLUNIFORM1FARBPROC) SDL_GL_GetProcAddress("glUniform1fARB");
   2.240 +        ctx->glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glUseProgramObjectARB");
   2.241 +        if (ctx->glGetError &&
   2.242 +            ctx->glAttachObjectARB &&
   2.243 +            ctx->glCompileShaderARB &&
   2.244 +            ctx->glCreateProgramObjectARB &&
   2.245 +            ctx->glCreateShaderObjectARB &&
   2.246 +            ctx->glDeleteObjectARB &&
   2.247 +            ctx->glGetInfoLogARB &&
   2.248 +            ctx->glGetObjectParameterivARB &&
   2.249 +            ctx->glGetUniformLocationARB &&
   2.250 +            ctx->glLinkProgramARB &&
   2.251 +            ctx->glShaderSourceARB &&
   2.252 +            ctx->glUniform1iARB &&
   2.253 +            ctx->glUniform1fARB &&
   2.254 +            ctx->glUseProgramObjectARB) {
   2.255 +            shaders_supported = SDL_TRUE;
   2.256 +        }
   2.257 +    }
   2.258 +
   2.259 +    if (!shaders_supported) {
   2.260 +        GL_DestroyShaderContext(ctx);
   2.261 +        return NULL;
   2.262 +    }
   2.263 +
   2.264 +    /* Compile all the shaders */
   2.265 +    for (i = 0; i < NUM_SHADERS; ++i) {
   2.266 +        if (!CompileShaderProgram(ctx, i, &ctx->shaders[i])) {
   2.267 +fprintf(stderr, "Unable to compile shader!\n");
   2.268 +            GL_DestroyShaderContext(ctx);
   2.269 +            return NULL;
   2.270 +        }
   2.271 +    }
   2.272 +
   2.273 +    /* We're done! */
   2.274 +    return ctx;
   2.275 +}
   2.276 +
   2.277 +void
   2.278 +GL_SelectShader(GL_ShaderContext *ctx, GL_Shader shader)
   2.279 +{
   2.280 +    /* Nothing to do if there's no shader support */
   2.281 +    if (!ctx) {
   2.282 +        return;
   2.283 +    }
   2.284 +
   2.285 +    /* Nothing to do if there's no shader change */
   2.286 +    if (shader == ctx->current_shader) {
   2.287 +        return;
   2.288 +    }
   2.289 +
   2.290 +    ctx->glUseProgramObjectARB(ctx->shaders[shader].program);
   2.291 +    ctx->current_shader = shader;
   2.292 +}
   2.293 +
   2.294 +void
   2.295 +GL_DestroyShaderContext(GL_ShaderContext *ctx)
   2.296 +{
   2.297 +    int i;
   2.298 +
   2.299 +    for (i = 0; i < NUM_SHADERS; ++i) {
   2.300 +        DestroyShaderProgram(ctx, &ctx->shaders[i]);
   2.301 +    }
   2.302 +    SDL_free(ctx);
   2.303 +}
   2.304 +
   2.305 +#endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
   2.306 +
   2.307 +/* vi: set ts=4 sw=4 expandtab: */
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/render/opengl/SDL_shaders_gl.h	Tue Feb 08 16:27:52 2011 -0800
     3.3 @@ -0,0 +1,40 @@
     3.4 +/*
     3.5 +    SDL - Simple DirectMedia Layer
     3.6 +    Copyright (C) 1997-2010 Sam Lantinga
     3.7 +
     3.8 +    This library is free software; you can redistribute it and/or
     3.9 +    modify it under the terms of the GNU Lesser General Public
    3.10 +    License as published by the Free Software Foundation; either
    3.11 +    version 2.1 of the License, or (at your option) any later version.
    3.12 +
    3.13 +    This library is distributed in the hope that it will be useful,
    3.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    3.16 +    Lesser General Public License for more details.
    3.17 +
    3.18 +    You should have received a copy of the GNU Lesser General Public
    3.19 +    License along with this library; if not, write to the Free Software
    3.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    3.21 +
    3.22 +    Sam Lantinga
    3.23 +    slouken@libsdl.org
    3.24 +*/
    3.25 +#include "SDL_config.h"
    3.26 +
    3.27 +/* OpenGL shader implementation */
    3.28 +
    3.29 +typedef enum {
    3.30 +    SHADER_NONE,
    3.31 +    SHADER_SOLID,
    3.32 +    SHADER_RGB,
    3.33 +    //SHADER_YV12,
    3.34 +    NUM_SHADERS
    3.35 +} GL_Shader;
    3.36 +
    3.37 +typedef struct GL_ShaderContext GL_ShaderContext;
    3.38 +
    3.39 +extern GL_ShaderContext * GL_CreateShaderContext();
    3.40 +extern void GL_SelectShader(GL_ShaderContext *ctx, GL_Shader shader);
    3.41 +extern void GL_DestroyShaderContext(GL_ShaderContext *ctx);
    3.42 +
    3.43 +/* vi: set ts=4 sw=4 expandtab: */