src/render/opengl/SDL_shaders_gl.c
changeset 5228 811beeb698f9
child 5229 c015d3e63631
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/render/opengl/SDL_shaders_gl.c	Tue Feb 08 16:27:52 2011 -0800
     1.3 @@ -0,0 +1,304 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2010 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +#if SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED
    1.28 +
    1.29 +#include "SDL_stdinc.h"
    1.30 +#include "SDL_log.h"
    1.31 +#include "SDL_opengl.h"
    1.32 +#include "SDL_video.h"
    1.33 +#include "SDL_shaders_gl.h"
    1.34 +
    1.35 +/* OpenGL shader implementation */
    1.36 +
    1.37 +typedef struct
    1.38 +{
    1.39 +    GLenum program;
    1.40 +    GLenum vert_shader;
    1.41 +    GLenum frag_shader;
    1.42 +} GL_ShaderData;
    1.43 +
    1.44 +struct GL_ShaderContext
    1.45 +{
    1.46 +    GLenum (*glGetError)(void);
    1.47 +
    1.48 +    PFNGLATTACHOBJECTARBPROC glAttachObjectARB;
    1.49 +    PFNGLCOMPILESHADERARBPROC glCompileShaderARB;
    1.50 +    PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObjectARB;
    1.51 +    PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObjectARB;
    1.52 +    PFNGLDELETEOBJECTARBPROC glDeleteObjectARB;
    1.53 +    PFNGLGETINFOLOGARBPROC glGetInfoLogARB;
    1.54 +    PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB;
    1.55 +    PFNGLGETUNIFORMLOCATIONARBPROC glGetUniformLocationARB;
    1.56 +    PFNGLLINKPROGRAMARBPROC glLinkProgramARB;
    1.57 +    PFNGLSHADERSOURCEARBPROC glShaderSourceARB;
    1.58 +    PFNGLUNIFORM1IARBPROC glUniform1iARB;
    1.59 +    PFNGLUNIFORM1FARBPROC glUniform1fARB;
    1.60 +    PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObjectARB;
    1.61 +
    1.62 +    GL_Shader current_shader;
    1.63 +    GL_ShaderData shaders[NUM_SHADERS];
    1.64 +};
    1.65 +
    1.66 +static const char *shader_source[NUM_SHADERS][2] =
    1.67 +{
    1.68 +    /* SHADER_NONE */
    1.69 +    { NULL, NULL },
    1.70 +
    1.71 +    /* SHADER_SOLID */
    1.72 +    {
    1.73 +        /* vertex shader */
    1.74 +        " \
    1.75 +varying vec4 v_color; \
    1.76 + \
    1.77 +void main() \
    1.78 +{ \
    1.79 +    gl_Position = ftransform(); \
    1.80 +    v_color = gl_Color; \
    1.81 +} \
    1.82 +",
    1.83 +        /* fragment shader */
    1.84 +        " \
    1.85 +varying vec4 v_color; \
    1.86 + \
    1.87 +void main() \
    1.88 +{ \
    1.89 +    gl_FragColor = v_color; \
    1.90 +} \
    1.91 +"
    1.92 +    },
    1.93 +
    1.94 +    /* SHADER_RGB */
    1.95 +    {
    1.96 +        /* vertex shader */
    1.97 +        " \
    1.98 +varying vec4 v_color; \
    1.99 +varying vec2 v_texCoord; \
   1.100 + \
   1.101 +void main() \
   1.102 +{ \
   1.103 +    gl_Position = ftransform(); \
   1.104 +    v_color = gl_Color; \
   1.105 +    v_texCoord = vec2(gl_MultiTexCoord0); \
   1.106 +} \
   1.107 +",
   1.108 +        /* fragment shader */
   1.109 +        " \
   1.110 +varying vec4 v_color; \
   1.111 +varying vec2 v_texCoord; \
   1.112 +uniform sampler2D tex0; \
   1.113 + \
   1.114 +void main() \
   1.115 +{ \
   1.116 +    gl_FragColor = texture2D(tex0, v_texCoord) * v_color; \
   1.117 +} \
   1.118 +"
   1.119 +    },
   1.120 +};
   1.121 +
   1.122 +static SDL_bool
   1.123 +CompileShader(GL_ShaderContext *ctx, GLenum shader, const char *source)
   1.124 +{
   1.125 +    GLint status;
   1.126 +
   1.127 +    ctx->glShaderSourceARB(shader, 1, &source, NULL);
   1.128 +    ctx->glCompileShaderARB(shader);
   1.129 +    ctx->glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
   1.130 +    if (status == 0) {
   1.131 +        GLint length;
   1.132 +        char *info;
   1.133 +
   1.134 +        ctx->glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
   1.135 +        info = SDL_stack_alloc(char, length+1);
   1.136 +        ctx->glGetInfoLogARB(shader, length, NULL, info);
   1.137 +        SDL_LogError(SDL_LOG_CATEGORY_RENDER,
   1.138 +            "Failed to compile shader:\n%s\n%s", source, info);
   1.139 +fprintf(stderr, "Failed to compile shader:\n%s\n%s", source, info);
   1.140 +        SDL_stack_free(info);
   1.141 +
   1.142 +        return SDL_FALSE;
   1.143 +    } else {
   1.144 +        return SDL_TRUE;
   1.145 +    }
   1.146 +}
   1.147 +
   1.148 +static SDL_bool
   1.149 +CompileShaderProgram(GL_ShaderContext *ctx, int index, GL_ShaderData *data)
   1.150 +{
   1.151 +    const int num_tmus_bound = 4;
   1.152 +    GLint status;
   1.153 +    int i;
   1.154 +    GLint location;
   1.155 +
   1.156 +    if (index == SHADER_NONE) {
   1.157 +        return SDL_TRUE;
   1.158 +    }
   1.159 +
   1.160 +    ctx->glGetError();
   1.161 +
   1.162 +    /* Create one program object to rule them all */
   1.163 +    data->program = ctx->glCreateProgramObjectARB();
   1.164 +
   1.165 +    /* Create the vertex shader */
   1.166 +    data->vert_shader = ctx->glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
   1.167 +    if (!CompileShader(ctx, data->vert_shader, shader_source[index][0])) {
   1.168 +        return SDL_FALSE;
   1.169 +    }
   1.170 +
   1.171 +    /* Create the fragment shader */
   1.172 +    data->frag_shader = ctx->glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
   1.173 +    if (!CompileShader(ctx, data->frag_shader, shader_source[index][1])) {
   1.174 +        return SDL_FALSE;
   1.175 +    }
   1.176 +
   1.177 +    /* ... and in the darkness bind them */
   1.178 +    ctx->glAttachObjectARB(data->program, data->vert_shader);
   1.179 +    ctx->glAttachObjectARB(data->program, data->frag_shader);
   1.180 +    ctx->glLinkProgramARB(data->program);
   1.181 +
   1.182 +    /* Set up some uniform variables */
   1.183 +    ctx->glUseProgramObjectARB(data->program);
   1.184 +    for (i = 0; i < num_tmus_bound; ++i) {
   1.185 +        char tex_name[5];
   1.186 +        SDL_snprintf(tex_name, SDL_arraysize(tex_name), "tex%d", i);
   1.187 +        location = ctx->glGetUniformLocationARB(data->program, tex_name);
   1.188 +        if (location >= 0) {
   1.189 +            ctx->glUniform1iARB(location, 1);
   1.190 +        }
   1.191 +    }
   1.192 +    ctx->glUseProgramObjectARB(0);
   1.193 +    
   1.194 +    return (ctx->glGetError() == GL_NO_ERROR);
   1.195 +}
   1.196 +
   1.197 +static void
   1.198 +DestroyShaderProgram(GL_ShaderContext *ctx, GL_ShaderData *data)
   1.199 +{
   1.200 +    if (index == SHADER_NONE) {
   1.201 +        return;
   1.202 +    }
   1.203 +
   1.204 +    ctx->glDeleteObjectARB(data->vert_shader);
   1.205 +    ctx->glDeleteObjectARB(data->frag_shader);
   1.206 +    ctx->glDeleteObjectARB(data->program);
   1.207 +}
   1.208 +
   1.209 +GL_ShaderContext *
   1.210 +GL_CreateShaderContext()
   1.211 +{
   1.212 +    GL_ShaderContext *ctx;
   1.213 +    SDL_bool shaders_supported;
   1.214 +    int i;
   1.215 +
   1.216 +    ctx = (GL_ShaderContext *)SDL_calloc(1, sizeof(*ctx));
   1.217 +    if (!ctx) {
   1.218 +        return NULL;
   1.219 +    }
   1.220 +
   1.221 +    /* Check for shader support */
   1.222 +    shaders_supported = SDL_FALSE;
   1.223 +    if (SDL_GL_ExtensionSupported("GL_ARB_shader_objects") &&
   1.224 +        SDL_GL_ExtensionSupported("GL_ARB_shading_language_100") &&
   1.225 +        SDL_GL_ExtensionSupported("GL_ARB_vertex_shader") &&
   1.226 +        SDL_GL_ExtensionSupported("GL_ARB_fragment_shader")) {
   1.227 +        ctx->glGetError = (GLenum (*)(void)) SDL_GL_GetProcAddress("glGetError");
   1.228 +        ctx->glAttachObjectARB = (PFNGLATTACHOBJECTARBPROC) SDL_GL_GetProcAddress("glAttachObjectARB");
   1.229 +        ctx->glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC) SDL_GL_GetProcAddress("glCompileShaderARB");
   1.230 +        ctx->glCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glCreateProgramObjectARB");
   1.231 +        ctx->glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC) SDL_GL_GetProcAddress("glCreateShaderObjectARB");
   1.232 +        ctx->glDeleteObjectARB = (PFNGLDELETEOBJECTARBPROC) SDL_GL_GetProcAddress("glDeleteObjectARB");
   1.233 +        ctx->glGetInfoLogARB = (PFNGLGETINFOLOGARBPROC) SDL_GL_GetProcAddress("glGetInfoLogARB");
   1.234 +        ctx->glGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC) SDL_GL_GetProcAddress("glGetObjectParameterivARB");
   1.235 +        ctx->glGetUniformLocationARB = (PFNGLGETUNIFORMLOCATIONARBPROC) SDL_GL_GetProcAddress("glGetUniformLocationARB");
   1.236 +        ctx->glLinkProgramARB = (PFNGLLINKPROGRAMARBPROC) SDL_GL_GetProcAddress("glLinkProgramARB");
   1.237 +        ctx->glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC) SDL_GL_GetProcAddress("glShaderSourceARB");
   1.238 +        ctx->glUniform1iARB = (PFNGLUNIFORM1IARBPROC) SDL_GL_GetProcAddress("glUniform1iARB");
   1.239 +        ctx->glUniform1fARB = (PFNGLUNIFORM1FARBPROC) SDL_GL_GetProcAddress("glUniform1fARB");
   1.240 +        ctx->glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glUseProgramObjectARB");
   1.241 +        if (ctx->glGetError &&
   1.242 +            ctx->glAttachObjectARB &&
   1.243 +            ctx->glCompileShaderARB &&
   1.244 +            ctx->glCreateProgramObjectARB &&
   1.245 +            ctx->glCreateShaderObjectARB &&
   1.246 +            ctx->glDeleteObjectARB &&
   1.247 +            ctx->glGetInfoLogARB &&
   1.248 +            ctx->glGetObjectParameterivARB &&
   1.249 +            ctx->glGetUniformLocationARB &&
   1.250 +            ctx->glLinkProgramARB &&
   1.251 +            ctx->glShaderSourceARB &&
   1.252 +            ctx->glUniform1iARB &&
   1.253 +            ctx->glUniform1fARB &&
   1.254 +            ctx->glUseProgramObjectARB) {
   1.255 +            shaders_supported = SDL_TRUE;
   1.256 +        }
   1.257 +    }
   1.258 +
   1.259 +    if (!shaders_supported) {
   1.260 +        GL_DestroyShaderContext(ctx);
   1.261 +        return NULL;
   1.262 +    }
   1.263 +
   1.264 +    /* Compile all the shaders */
   1.265 +    for (i = 0; i < NUM_SHADERS; ++i) {
   1.266 +        if (!CompileShaderProgram(ctx, i, &ctx->shaders[i])) {
   1.267 +fprintf(stderr, "Unable to compile shader!\n");
   1.268 +            GL_DestroyShaderContext(ctx);
   1.269 +            return NULL;
   1.270 +        }
   1.271 +    }
   1.272 +
   1.273 +    /* We're done! */
   1.274 +    return ctx;
   1.275 +}
   1.276 +
   1.277 +void
   1.278 +GL_SelectShader(GL_ShaderContext *ctx, GL_Shader shader)
   1.279 +{
   1.280 +    /* Nothing to do if there's no shader support */
   1.281 +    if (!ctx) {
   1.282 +        return;
   1.283 +    }
   1.284 +
   1.285 +    /* Nothing to do if there's no shader change */
   1.286 +    if (shader == ctx->current_shader) {
   1.287 +        return;
   1.288 +    }
   1.289 +
   1.290 +    ctx->glUseProgramObjectARB(ctx->shaders[shader].program);
   1.291 +    ctx->current_shader = shader;
   1.292 +}
   1.293 +
   1.294 +void
   1.295 +GL_DestroyShaderContext(GL_ShaderContext *ctx)
   1.296 +{
   1.297 +    int i;
   1.298 +
   1.299 +    for (i = 0; i < NUM_SHADERS; ++i) {
   1.300 +        DestroyShaderProgram(ctx, &ctx->shaders[i]);
   1.301 +    }
   1.302 +    SDL_free(ctx);
   1.303 +}
   1.304 +
   1.305 +#endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
   1.306 +
   1.307 +/* vi: set ts=4 sw=4 expandtab: */