src/render/opengl/SDL_shaders_gl.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 08 Feb 2011 16:27:52 -0800
changeset 5228 811beeb698f9
child 5229 c015d3e63631
permissions -rw-r--r--
Beginning of a framework for OpenGL shaders
slouken@5228
     1
/*
slouken@5228
     2
    SDL - Simple DirectMedia Layer
slouken@5228
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@5228
     4
slouken@5228
     5
    This library is free software; you can redistribute it and/or
slouken@5228
     6
    modify it under the terms of the GNU Lesser General Public
slouken@5228
     7
    License as published by the Free Software Foundation; either
slouken@5228
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@5228
     9
slouken@5228
    10
    This library is distributed in the hope that it will be useful,
slouken@5228
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@5228
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@5228
    13
    Lesser General Public License for more details.
slouken@5228
    14
slouken@5228
    15
    You should have received a copy of the GNU Lesser General Public
slouken@5228
    16
    License along with this library; if not, write to the Free Software
slouken@5228
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@5228
    18
slouken@5228
    19
    Sam Lantinga
slouken@5228
    20
    slouken@libsdl.org
slouken@5228
    21
*/
slouken@5228
    22
#include "SDL_config.h"
slouken@5228
    23
slouken@5228
    24
#if SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED
slouken@5228
    25
slouken@5228
    26
#include "SDL_stdinc.h"
slouken@5228
    27
#include "SDL_log.h"
slouken@5228
    28
#include "SDL_opengl.h"
slouken@5228
    29
#include "SDL_video.h"
slouken@5228
    30
#include "SDL_shaders_gl.h"
slouken@5228
    31
slouken@5228
    32
/* OpenGL shader implementation */
slouken@5228
    33
slouken@5228
    34
typedef struct
slouken@5228
    35
{
slouken@5228
    36
    GLenum program;
slouken@5228
    37
    GLenum vert_shader;
slouken@5228
    38
    GLenum frag_shader;
slouken@5228
    39
} GL_ShaderData;
slouken@5228
    40
slouken@5228
    41
struct GL_ShaderContext
slouken@5228
    42
{
slouken@5228
    43
    GLenum (*glGetError)(void);
slouken@5228
    44
slouken@5228
    45
    PFNGLATTACHOBJECTARBPROC glAttachObjectARB;
slouken@5228
    46
    PFNGLCOMPILESHADERARBPROC glCompileShaderARB;
slouken@5228
    47
    PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObjectARB;
slouken@5228
    48
    PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObjectARB;
slouken@5228
    49
    PFNGLDELETEOBJECTARBPROC glDeleteObjectARB;
slouken@5228
    50
    PFNGLGETINFOLOGARBPROC glGetInfoLogARB;
slouken@5228
    51
    PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB;
slouken@5228
    52
    PFNGLGETUNIFORMLOCATIONARBPROC glGetUniformLocationARB;
slouken@5228
    53
    PFNGLLINKPROGRAMARBPROC glLinkProgramARB;
slouken@5228
    54
    PFNGLSHADERSOURCEARBPROC glShaderSourceARB;
slouken@5228
    55
    PFNGLUNIFORM1IARBPROC glUniform1iARB;
slouken@5228
    56
    PFNGLUNIFORM1FARBPROC glUniform1fARB;
slouken@5228
    57
    PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObjectARB;
slouken@5228
    58
slouken@5228
    59
    GL_Shader current_shader;
slouken@5228
    60
    GL_ShaderData shaders[NUM_SHADERS];
slouken@5228
    61
};
slouken@5228
    62
slouken@5228
    63
static const char *shader_source[NUM_SHADERS][2] =
slouken@5228
    64
{
slouken@5228
    65
    /* SHADER_NONE */
slouken@5228
    66
    { NULL, NULL },
slouken@5228
    67
slouken@5228
    68
    /* SHADER_SOLID */
slouken@5228
    69
    {
slouken@5228
    70
        /* vertex shader */
slouken@5228
    71
        " \
slouken@5228
    72
varying vec4 v_color; \
slouken@5228
    73
 \
slouken@5228
    74
void main() \
slouken@5228
    75
{ \
slouken@5228
    76
    gl_Position = ftransform(); \
slouken@5228
    77
    v_color = gl_Color; \
slouken@5228
    78
} \
slouken@5228
    79
",
slouken@5228
    80
        /* fragment shader */
slouken@5228
    81
        " \
slouken@5228
    82
varying vec4 v_color; \
slouken@5228
    83
 \
slouken@5228
    84
void main() \
slouken@5228
    85
{ \
slouken@5228
    86
    gl_FragColor = v_color; \
slouken@5228
    87
} \
slouken@5228
    88
"
slouken@5228
    89
    },
slouken@5228
    90
slouken@5228
    91
    /* SHADER_RGB */
slouken@5228
    92
    {
slouken@5228
    93
        /* vertex shader */
slouken@5228
    94
        " \
slouken@5228
    95
varying vec4 v_color; \
slouken@5228
    96
varying vec2 v_texCoord; \
slouken@5228
    97
 \
slouken@5228
    98
void main() \
slouken@5228
    99
{ \
slouken@5228
   100
    gl_Position = ftransform(); \
slouken@5228
   101
    v_color = gl_Color; \
slouken@5228
   102
    v_texCoord = vec2(gl_MultiTexCoord0); \
slouken@5228
   103
} \
slouken@5228
   104
",
slouken@5228
   105
        /* fragment shader */
slouken@5228
   106
        " \
slouken@5228
   107
varying vec4 v_color; \
slouken@5228
   108
varying vec2 v_texCoord; \
slouken@5228
   109
uniform sampler2D tex0; \
slouken@5228
   110
 \
slouken@5228
   111
void main() \
slouken@5228
   112
{ \
slouken@5228
   113
    gl_FragColor = texture2D(tex0, v_texCoord) * v_color; \
slouken@5228
   114
} \
slouken@5228
   115
"
slouken@5228
   116
    },
slouken@5228
   117
};
slouken@5228
   118
slouken@5228
   119
static SDL_bool
slouken@5228
   120
CompileShader(GL_ShaderContext *ctx, GLenum shader, const char *source)
slouken@5228
   121
{
slouken@5228
   122
    GLint status;
slouken@5228
   123
slouken@5228
   124
    ctx->glShaderSourceARB(shader, 1, &source, NULL);
slouken@5228
   125
    ctx->glCompileShaderARB(shader);
slouken@5228
   126
    ctx->glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
slouken@5228
   127
    if (status == 0) {
slouken@5228
   128
        GLint length;
slouken@5228
   129
        char *info;
slouken@5228
   130
slouken@5228
   131
        ctx->glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
slouken@5228
   132
        info = SDL_stack_alloc(char, length+1);
slouken@5228
   133
        ctx->glGetInfoLogARB(shader, length, NULL, info);
slouken@5228
   134
        SDL_LogError(SDL_LOG_CATEGORY_RENDER,
slouken@5228
   135
            "Failed to compile shader:\n%s\n%s", source, info);
slouken@5228
   136
fprintf(stderr, "Failed to compile shader:\n%s\n%s", source, info);
slouken@5228
   137
        SDL_stack_free(info);
slouken@5228
   138
slouken@5228
   139
        return SDL_FALSE;
slouken@5228
   140
    } else {
slouken@5228
   141
        return SDL_TRUE;
slouken@5228
   142
    }
slouken@5228
   143
}
slouken@5228
   144
slouken@5228
   145
static SDL_bool
slouken@5228
   146
CompileShaderProgram(GL_ShaderContext *ctx, int index, GL_ShaderData *data)
slouken@5228
   147
{
slouken@5228
   148
    const int num_tmus_bound = 4;
slouken@5228
   149
    GLint status;
slouken@5228
   150
    int i;
slouken@5228
   151
    GLint location;
slouken@5228
   152
slouken@5228
   153
    if (index == SHADER_NONE) {
slouken@5228
   154
        return SDL_TRUE;
slouken@5228
   155
    }
slouken@5228
   156
slouken@5228
   157
    ctx->glGetError();
slouken@5228
   158
slouken@5228
   159
    /* Create one program object to rule them all */
slouken@5228
   160
    data->program = ctx->glCreateProgramObjectARB();
slouken@5228
   161
slouken@5228
   162
    /* Create the vertex shader */
slouken@5228
   163
    data->vert_shader = ctx->glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
slouken@5228
   164
    if (!CompileShader(ctx, data->vert_shader, shader_source[index][0])) {
slouken@5228
   165
        return SDL_FALSE;
slouken@5228
   166
    }
slouken@5228
   167
slouken@5228
   168
    /* Create the fragment shader */
slouken@5228
   169
    data->frag_shader = ctx->glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
slouken@5228
   170
    if (!CompileShader(ctx, data->frag_shader, shader_source[index][1])) {
slouken@5228
   171
        return SDL_FALSE;
slouken@5228
   172
    }
slouken@5228
   173
slouken@5228
   174
    /* ... and in the darkness bind them */
slouken@5228
   175
    ctx->glAttachObjectARB(data->program, data->vert_shader);
slouken@5228
   176
    ctx->glAttachObjectARB(data->program, data->frag_shader);
slouken@5228
   177
    ctx->glLinkProgramARB(data->program);
slouken@5228
   178
slouken@5228
   179
    /* Set up some uniform variables */
slouken@5228
   180
    ctx->glUseProgramObjectARB(data->program);
slouken@5228
   181
    for (i = 0; i < num_tmus_bound; ++i) {
slouken@5228
   182
        char tex_name[5];
slouken@5228
   183
        SDL_snprintf(tex_name, SDL_arraysize(tex_name), "tex%d", i);
slouken@5228
   184
        location = ctx->glGetUniformLocationARB(data->program, tex_name);
slouken@5228
   185
        if (location >= 0) {
slouken@5228
   186
            ctx->glUniform1iARB(location, 1);
slouken@5228
   187
        }
slouken@5228
   188
    }
slouken@5228
   189
    ctx->glUseProgramObjectARB(0);
slouken@5228
   190
    
slouken@5228
   191
    return (ctx->glGetError() == GL_NO_ERROR);
slouken@5228
   192
}
slouken@5228
   193
slouken@5228
   194
static void
slouken@5228
   195
DestroyShaderProgram(GL_ShaderContext *ctx, GL_ShaderData *data)
slouken@5228
   196
{
slouken@5228
   197
    if (index == SHADER_NONE) {
slouken@5228
   198
        return;
slouken@5228
   199
    }
slouken@5228
   200
slouken@5228
   201
    ctx->glDeleteObjectARB(data->vert_shader);
slouken@5228
   202
    ctx->glDeleteObjectARB(data->frag_shader);
slouken@5228
   203
    ctx->glDeleteObjectARB(data->program);
slouken@5228
   204
}
slouken@5228
   205
slouken@5228
   206
GL_ShaderContext *
slouken@5228
   207
GL_CreateShaderContext()
slouken@5228
   208
{
slouken@5228
   209
    GL_ShaderContext *ctx;
slouken@5228
   210
    SDL_bool shaders_supported;
slouken@5228
   211
    int i;
slouken@5228
   212
slouken@5228
   213
    ctx = (GL_ShaderContext *)SDL_calloc(1, sizeof(*ctx));
slouken@5228
   214
    if (!ctx) {
slouken@5228
   215
        return NULL;
slouken@5228
   216
    }
slouken@5228
   217
slouken@5228
   218
    /* Check for shader support */
slouken@5228
   219
    shaders_supported = SDL_FALSE;
slouken@5228
   220
    if (SDL_GL_ExtensionSupported("GL_ARB_shader_objects") &&
slouken@5228
   221
        SDL_GL_ExtensionSupported("GL_ARB_shading_language_100") &&
slouken@5228
   222
        SDL_GL_ExtensionSupported("GL_ARB_vertex_shader") &&
slouken@5228
   223
        SDL_GL_ExtensionSupported("GL_ARB_fragment_shader")) {
slouken@5228
   224
        ctx->glGetError = (GLenum (*)(void)) SDL_GL_GetProcAddress("glGetError");
slouken@5228
   225
        ctx->glAttachObjectARB = (PFNGLATTACHOBJECTARBPROC) SDL_GL_GetProcAddress("glAttachObjectARB");
slouken@5228
   226
        ctx->glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC) SDL_GL_GetProcAddress("glCompileShaderARB");
slouken@5228
   227
        ctx->glCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glCreateProgramObjectARB");
slouken@5228
   228
        ctx->glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC) SDL_GL_GetProcAddress("glCreateShaderObjectARB");
slouken@5228
   229
        ctx->glDeleteObjectARB = (PFNGLDELETEOBJECTARBPROC) SDL_GL_GetProcAddress("glDeleteObjectARB");
slouken@5228
   230
        ctx->glGetInfoLogARB = (PFNGLGETINFOLOGARBPROC) SDL_GL_GetProcAddress("glGetInfoLogARB");
slouken@5228
   231
        ctx->glGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC) SDL_GL_GetProcAddress("glGetObjectParameterivARB");
slouken@5228
   232
        ctx->glGetUniformLocationARB = (PFNGLGETUNIFORMLOCATIONARBPROC) SDL_GL_GetProcAddress("glGetUniformLocationARB");
slouken@5228
   233
        ctx->glLinkProgramARB = (PFNGLLINKPROGRAMARBPROC) SDL_GL_GetProcAddress("glLinkProgramARB");
slouken@5228
   234
        ctx->glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC) SDL_GL_GetProcAddress("glShaderSourceARB");
slouken@5228
   235
        ctx->glUniform1iARB = (PFNGLUNIFORM1IARBPROC) SDL_GL_GetProcAddress("glUniform1iARB");
slouken@5228
   236
        ctx->glUniform1fARB = (PFNGLUNIFORM1FARBPROC) SDL_GL_GetProcAddress("glUniform1fARB");
slouken@5228
   237
        ctx->glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glUseProgramObjectARB");
slouken@5228
   238
        if (ctx->glGetError &&
slouken@5228
   239
            ctx->glAttachObjectARB &&
slouken@5228
   240
            ctx->glCompileShaderARB &&
slouken@5228
   241
            ctx->glCreateProgramObjectARB &&
slouken@5228
   242
            ctx->glCreateShaderObjectARB &&
slouken@5228
   243
            ctx->glDeleteObjectARB &&
slouken@5228
   244
            ctx->glGetInfoLogARB &&
slouken@5228
   245
            ctx->glGetObjectParameterivARB &&
slouken@5228
   246
            ctx->glGetUniformLocationARB &&
slouken@5228
   247
            ctx->glLinkProgramARB &&
slouken@5228
   248
            ctx->glShaderSourceARB &&
slouken@5228
   249
            ctx->glUniform1iARB &&
slouken@5228
   250
            ctx->glUniform1fARB &&
slouken@5228
   251
            ctx->glUseProgramObjectARB) {
slouken@5228
   252
            shaders_supported = SDL_TRUE;
slouken@5228
   253
        }
slouken@5228
   254
    }
slouken@5228
   255
slouken@5228
   256
    if (!shaders_supported) {
slouken@5228
   257
        GL_DestroyShaderContext(ctx);
slouken@5228
   258
        return NULL;
slouken@5228
   259
    }
slouken@5228
   260
slouken@5228
   261
    /* Compile all the shaders */
slouken@5228
   262
    for (i = 0; i < NUM_SHADERS; ++i) {
slouken@5228
   263
        if (!CompileShaderProgram(ctx, i, &ctx->shaders[i])) {
slouken@5228
   264
fprintf(stderr, "Unable to compile shader!\n");
slouken@5228
   265
            GL_DestroyShaderContext(ctx);
slouken@5228
   266
            return NULL;
slouken@5228
   267
        }
slouken@5228
   268
    }
slouken@5228
   269
slouken@5228
   270
    /* We're done! */
slouken@5228
   271
    return ctx;
slouken@5228
   272
}
slouken@5228
   273
slouken@5228
   274
void
slouken@5228
   275
GL_SelectShader(GL_ShaderContext *ctx, GL_Shader shader)
slouken@5228
   276
{
slouken@5228
   277
    /* Nothing to do if there's no shader support */
slouken@5228
   278
    if (!ctx) {
slouken@5228
   279
        return;
slouken@5228
   280
    }
slouken@5228
   281
slouken@5228
   282
    /* Nothing to do if there's no shader change */
slouken@5228
   283
    if (shader == ctx->current_shader) {
slouken@5228
   284
        return;
slouken@5228
   285
    }
slouken@5228
   286
slouken@5228
   287
    ctx->glUseProgramObjectARB(ctx->shaders[shader].program);
slouken@5228
   288
    ctx->current_shader = shader;
slouken@5228
   289
}
slouken@5228
   290
slouken@5228
   291
void
slouken@5228
   292
GL_DestroyShaderContext(GL_ShaderContext *ctx)
slouken@5228
   293
{
slouken@5228
   294
    int i;
slouken@5228
   295
slouken@5228
   296
    for (i = 0; i < NUM_SHADERS; ++i) {
slouken@5228
   297
        DestroyShaderProgram(ctx, &ctx->shaders[i]);
slouken@5228
   298
    }
slouken@5228
   299
    SDL_free(ctx);
slouken@5228
   300
}
slouken@5228
   301
slouken@5228
   302
#endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
slouken@5228
   303
slouken@5228
   304
/* vi: set ts=4 sw=4 expandtab: */