src/render/opengl/SDL_shaders_gl.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 09 Feb 2011 10:13:26 -0800
changeset 5237 55b31686f82b
parent 5231 e82908c86c9c
child 5238 c0e9d3897a85
permissions -rw-r--r--
Fixed crash when shaders aren't available.
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@5231
    34
/*#define DEBUG_SHADERS*/
slouken@5231
    35
slouken@5228
    36
typedef struct
slouken@5228
    37
{
slouken@5228
    38
    GLenum program;
slouken@5228
    39
    GLenum vert_shader;
slouken@5228
    40
    GLenum frag_shader;
slouken@5228
    41
} GL_ShaderData;
slouken@5228
    42
slouken@5228
    43
struct GL_ShaderContext
slouken@5228
    44
{
slouken@5228
    45
    GLenum (*glGetError)(void);
slouken@5228
    46
slouken@5228
    47
    PFNGLATTACHOBJECTARBPROC glAttachObjectARB;
slouken@5228
    48
    PFNGLCOMPILESHADERARBPROC glCompileShaderARB;
slouken@5228
    49
    PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObjectARB;
slouken@5228
    50
    PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObjectARB;
slouken@5228
    51
    PFNGLDELETEOBJECTARBPROC glDeleteObjectARB;
slouken@5228
    52
    PFNGLGETINFOLOGARBPROC glGetInfoLogARB;
slouken@5228
    53
    PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB;
slouken@5228
    54
    PFNGLGETUNIFORMLOCATIONARBPROC glGetUniformLocationARB;
slouken@5228
    55
    PFNGLLINKPROGRAMARBPROC glLinkProgramARB;
slouken@5228
    56
    PFNGLSHADERSOURCEARBPROC glShaderSourceARB;
slouken@5228
    57
    PFNGLUNIFORM1IARBPROC glUniform1iARB;
slouken@5228
    58
    PFNGLUNIFORM1FARBPROC glUniform1fARB;
slouken@5228
    59
    PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObjectARB;
slouken@5228
    60
slouken@5230
    61
    SDL_bool GL_ARB_texture_rectangle_supported;
slouken@5230
    62
slouken@5228
    63
    GL_Shader current_shader;
slouken@5228
    64
    GL_ShaderData shaders[NUM_SHADERS];
slouken@5228
    65
};
slouken@5228
    66
slouken@5230
    67
/*
slouken@5230
    68
 * NOTE: Always use sampler2D, etc here. We'll #define them to the
slouken@5230
    69
 *  texture_rectangle versions if we choose to use that extension.
slouken@5230
    70
 */
slouken@5228
    71
static const char *shader_source[NUM_SHADERS][2] =
slouken@5228
    72
{
slouken@5228
    73
    /* SHADER_NONE */
slouken@5228
    74
    { NULL, NULL },
slouken@5228
    75
slouken@5228
    76
    /* SHADER_SOLID */
slouken@5228
    77
    {
slouken@5228
    78
        /* vertex shader */
slouken@5231
    79
"varying vec4 v_color;\n"
slouken@5231
    80
"\n"
slouken@5231
    81
"void main()\n"
slouken@5231
    82
"{\n"
slouken@5231
    83
"    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
slouken@5231
    84
"    v_color = gl_Color;\n"
slouken@5231
    85
"}",
slouken@5228
    86
        /* fragment shader */
slouken@5231
    87
"varying vec4 v_color;\n"
slouken@5231
    88
"\n"
slouken@5231
    89
"void main()\n"
slouken@5231
    90
"{\n"
slouken@5231
    91
"    gl_FragColor = v_color;\n"
slouken@5231
    92
"}"
slouken@5228
    93
    },
slouken@5228
    94
slouken@5228
    95
    /* SHADER_RGB */
slouken@5228
    96
    {
slouken@5228
    97
        /* vertex shader */
slouken@5231
    98
"varying vec4 v_color;\n"
slouken@5231
    99
"varying vec2 v_texCoord;\n"
slouken@5231
   100
"\n"
slouken@5231
   101
"void main()\n"
slouken@5231
   102
"{\n"
slouken@5231
   103
"    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
slouken@5231
   104
"    v_color = gl_Color;\n"
slouken@5231
   105
"    v_texCoord = vec2(gl_MultiTexCoord0);\n"
slouken@5231
   106
"}",
slouken@5228
   107
        /* fragment shader */
slouken@5231
   108
"varying vec4 v_color;\n"
slouken@5231
   109
"varying vec2 v_texCoord;\n"
slouken@5231
   110
"uniform sampler2D tex0;\n"
slouken@5231
   111
"\n"
slouken@5231
   112
"void main()\n"
slouken@5231
   113
"{\n"
slouken@5231
   114
"    gl_FragColor = texture2D(tex0, v_texCoord) * v_color;\n"
slouken@5231
   115
"}"
slouken@5228
   116
    },
slouken@5228
   117
};
slouken@5228
   118
slouken@5228
   119
static SDL_bool
slouken@5230
   120
CompileShader(GL_ShaderContext *ctx, GLenum shader, const char *defines, const char *source)
slouken@5228
   121
{
slouken@5228
   122
    GLint status;
slouken@5230
   123
    const char *sources[2];
slouken@5228
   124
slouken@5230
   125
    sources[0] = defines;
slouken@5230
   126
    sources[1] = source;
slouken@5230
   127
slouken@5230
   128
    ctx->glShaderSourceARB(shader, SDL_arraysize(sources), sources, NULL);
slouken@5228
   129
    ctx->glCompileShaderARB(shader);
slouken@5228
   130
    ctx->glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
slouken@5228
   131
    if (status == 0) {
slouken@5228
   132
        GLint length;
slouken@5228
   133
        char *info;
slouken@5228
   134
slouken@5228
   135
        ctx->glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
slouken@5228
   136
        info = SDL_stack_alloc(char, length+1);
slouken@5228
   137
        ctx->glGetInfoLogARB(shader, length, NULL, info);
slouken@5228
   138
        SDL_LogError(SDL_LOG_CATEGORY_RENDER,
slouken@5231
   139
            "Failed to compile shader:\n%s%s\n%s", defines, source, info);
slouken@5231
   140
#ifdef DEBUG_SHADERS
slouken@5231
   141
        fprintf(stderr,
slouken@5231
   142
            "Failed to compile shader:\n%s%s\n%s", defines, source, info);
slouken@5231
   143
#endif
slouken@5228
   144
        SDL_stack_free(info);
slouken@5228
   145
slouken@5228
   146
        return SDL_FALSE;
slouken@5228
   147
    } else {
slouken@5228
   148
        return SDL_TRUE;
slouken@5228
   149
    }
slouken@5228
   150
}
slouken@5228
   151
slouken@5228
   152
static SDL_bool
slouken@5228
   153
CompileShaderProgram(GL_ShaderContext *ctx, int index, GL_ShaderData *data)
slouken@5228
   154
{
slouken@5228
   155
    const int num_tmus_bound = 4;
slouken@5230
   156
    const char *vert_defines = "";
slouken@5230
   157
    const char *frag_defines = "";
slouken@5228
   158
    int i;
slouken@5228
   159
    GLint location;
slouken@5228
   160
slouken@5228
   161
    if (index == SHADER_NONE) {
slouken@5228
   162
        return SDL_TRUE;
slouken@5228
   163
    }
slouken@5228
   164
slouken@5228
   165
    ctx->glGetError();
slouken@5228
   166
slouken@5230
   167
    /* Make sure we use the correct sampler type for our texture type */
slouken@5230
   168
    if (ctx->GL_ARB_texture_rectangle_supported) {
slouken@5230
   169
        frag_defines = 
slouken@5230
   170
"#define sampler2D sampler2DRect\n"
slouken@5230
   171
"#define texture2D texture2DRect\n";
slouken@5230
   172
    }
slouken@5230
   173
slouken@5228
   174
    /* Create one program object to rule them all */
slouken@5228
   175
    data->program = ctx->glCreateProgramObjectARB();
slouken@5228
   176
slouken@5228
   177
    /* Create the vertex shader */
slouken@5228
   178
    data->vert_shader = ctx->glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
slouken@5230
   179
    if (!CompileShader(ctx, data->vert_shader, vert_defines, shader_source[index][0])) {
slouken@5228
   180
        return SDL_FALSE;
slouken@5228
   181
    }
slouken@5228
   182
slouken@5228
   183
    /* Create the fragment shader */
slouken@5228
   184
    data->frag_shader = ctx->glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
slouken@5230
   185
    if (!CompileShader(ctx, data->frag_shader, frag_defines, shader_source[index][1])) {
slouken@5228
   186
        return SDL_FALSE;
slouken@5228
   187
    }
slouken@5228
   188
slouken@5228
   189
    /* ... and in the darkness bind them */
slouken@5228
   190
    ctx->glAttachObjectARB(data->program, data->vert_shader);
slouken@5228
   191
    ctx->glAttachObjectARB(data->program, data->frag_shader);
slouken@5228
   192
    ctx->glLinkProgramARB(data->program);
slouken@5228
   193
slouken@5228
   194
    /* Set up some uniform variables */
slouken@5228
   195
    ctx->glUseProgramObjectARB(data->program);
slouken@5228
   196
    for (i = 0; i < num_tmus_bound; ++i) {
slouken@5228
   197
        char tex_name[5];
slouken@5228
   198
        SDL_snprintf(tex_name, SDL_arraysize(tex_name), "tex%d", i);
slouken@5228
   199
        location = ctx->glGetUniformLocationARB(data->program, tex_name);
slouken@5228
   200
        if (location >= 0) {
slouken@5229
   201
            ctx->glUniform1iARB(location, i);
slouken@5228
   202
        }
slouken@5228
   203
    }
slouken@5228
   204
    ctx->glUseProgramObjectARB(0);
slouken@5228
   205
    
slouken@5228
   206
    return (ctx->glGetError() == GL_NO_ERROR);
slouken@5228
   207
}
slouken@5228
   208
slouken@5228
   209
static void
slouken@5228
   210
DestroyShaderProgram(GL_ShaderContext *ctx, GL_ShaderData *data)
slouken@5228
   211
{
slouken@5228
   212
    if (index == SHADER_NONE) {
slouken@5228
   213
        return;
slouken@5228
   214
    }
slouken@5228
   215
slouken@5228
   216
    ctx->glDeleteObjectARB(data->vert_shader);
slouken@5228
   217
    ctx->glDeleteObjectARB(data->frag_shader);
slouken@5228
   218
    ctx->glDeleteObjectARB(data->program);
slouken@5228
   219
}
slouken@5228
   220
slouken@5228
   221
GL_ShaderContext *
slouken@5228
   222
GL_CreateShaderContext()
slouken@5228
   223
{
slouken@5228
   224
    GL_ShaderContext *ctx;
slouken@5228
   225
    SDL_bool shaders_supported;
slouken@5228
   226
    int i;
slouken@5228
   227
slouken@5228
   228
    ctx = (GL_ShaderContext *)SDL_calloc(1, sizeof(*ctx));
slouken@5228
   229
    if (!ctx) {
slouken@5228
   230
        return NULL;
slouken@5228
   231
    }
slouken@5228
   232
slouken@5230
   233
    if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
slouken@5230
   234
        || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
slouken@5230
   235
        ctx->GL_ARB_texture_rectangle_supported = SDL_TRUE;
slouken@5230
   236
    }
slouken@5230
   237
slouken@5228
   238
    /* Check for shader support */
slouken@5228
   239
    shaders_supported = SDL_FALSE;
slouken@5228
   240
    if (SDL_GL_ExtensionSupported("GL_ARB_shader_objects") &&
slouken@5228
   241
        SDL_GL_ExtensionSupported("GL_ARB_shading_language_100") &&
slouken@5228
   242
        SDL_GL_ExtensionSupported("GL_ARB_vertex_shader") &&
slouken@5228
   243
        SDL_GL_ExtensionSupported("GL_ARB_fragment_shader")) {
slouken@5228
   244
        ctx->glGetError = (GLenum (*)(void)) SDL_GL_GetProcAddress("glGetError");
slouken@5228
   245
        ctx->glAttachObjectARB = (PFNGLATTACHOBJECTARBPROC) SDL_GL_GetProcAddress("glAttachObjectARB");
slouken@5228
   246
        ctx->glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC) SDL_GL_GetProcAddress("glCompileShaderARB");
slouken@5228
   247
        ctx->glCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glCreateProgramObjectARB");
slouken@5228
   248
        ctx->glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC) SDL_GL_GetProcAddress("glCreateShaderObjectARB");
slouken@5228
   249
        ctx->glDeleteObjectARB = (PFNGLDELETEOBJECTARBPROC) SDL_GL_GetProcAddress("glDeleteObjectARB");
slouken@5228
   250
        ctx->glGetInfoLogARB = (PFNGLGETINFOLOGARBPROC) SDL_GL_GetProcAddress("glGetInfoLogARB");
slouken@5228
   251
        ctx->glGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC) SDL_GL_GetProcAddress("glGetObjectParameterivARB");
slouken@5228
   252
        ctx->glGetUniformLocationARB = (PFNGLGETUNIFORMLOCATIONARBPROC) SDL_GL_GetProcAddress("glGetUniformLocationARB");
slouken@5228
   253
        ctx->glLinkProgramARB = (PFNGLLINKPROGRAMARBPROC) SDL_GL_GetProcAddress("glLinkProgramARB");
slouken@5228
   254
        ctx->glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC) SDL_GL_GetProcAddress("glShaderSourceARB");
slouken@5228
   255
        ctx->glUniform1iARB = (PFNGLUNIFORM1IARBPROC) SDL_GL_GetProcAddress("glUniform1iARB");
slouken@5228
   256
        ctx->glUniform1fARB = (PFNGLUNIFORM1FARBPROC) SDL_GL_GetProcAddress("glUniform1fARB");
slouken@5228
   257
        ctx->glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glUseProgramObjectARB");
slouken@5228
   258
        if (ctx->glGetError &&
slouken@5228
   259
            ctx->glAttachObjectARB &&
slouken@5228
   260
            ctx->glCompileShaderARB &&
slouken@5228
   261
            ctx->glCreateProgramObjectARB &&
slouken@5228
   262
            ctx->glCreateShaderObjectARB &&
slouken@5228
   263
            ctx->glDeleteObjectARB &&
slouken@5228
   264
            ctx->glGetInfoLogARB &&
slouken@5228
   265
            ctx->glGetObjectParameterivARB &&
slouken@5228
   266
            ctx->glGetUniformLocationARB &&
slouken@5228
   267
            ctx->glLinkProgramARB &&
slouken@5228
   268
            ctx->glShaderSourceARB &&
slouken@5228
   269
            ctx->glUniform1iARB &&
slouken@5228
   270
            ctx->glUniform1fARB &&
slouken@5228
   271
            ctx->glUseProgramObjectARB) {
slouken@5228
   272
            shaders_supported = SDL_TRUE;
slouken@5228
   273
        }
slouken@5228
   274
    }
slouken@5228
   275
slouken@5228
   276
    if (!shaders_supported) {
slouken@5237
   277
        SDL_free(ctx);
slouken@5228
   278
        return NULL;
slouken@5228
   279
    }
slouken@5228
   280
slouken@5228
   281
    /* Compile all the shaders */
slouken@5228
   282
    for (i = 0; i < NUM_SHADERS; ++i) {
slouken@5228
   283
        if (!CompileShaderProgram(ctx, i, &ctx->shaders[i])) {
slouken@5228
   284
fprintf(stderr, "Unable to compile shader!\n");
slouken@5228
   285
            GL_DestroyShaderContext(ctx);
slouken@5228
   286
            return NULL;
slouken@5228
   287
        }
slouken@5228
   288
    }
slouken@5228
   289
slouken@5228
   290
    /* We're done! */
slouken@5228
   291
    return ctx;
slouken@5228
   292
}
slouken@5228
   293
slouken@5228
   294
void
slouken@5228
   295
GL_SelectShader(GL_ShaderContext *ctx, GL_Shader shader)
slouken@5228
   296
{
slouken@5228
   297
    /* Nothing to do if there's no shader support */
slouken@5228
   298
    if (!ctx) {
slouken@5228
   299
        return;
slouken@5228
   300
    }
slouken@5228
   301
slouken@5228
   302
    /* Nothing to do if there's no shader change */
slouken@5228
   303
    if (shader == ctx->current_shader) {
slouken@5228
   304
        return;
slouken@5228
   305
    }
slouken@5228
   306
slouken@5228
   307
    ctx->glUseProgramObjectARB(ctx->shaders[shader].program);
slouken@5228
   308
    ctx->current_shader = shader;
slouken@5228
   309
}
slouken@5228
   310
slouken@5228
   311
void
slouken@5228
   312
GL_DestroyShaderContext(GL_ShaderContext *ctx)
slouken@5228
   313
{
slouken@5228
   314
    int i;
slouken@5228
   315
slouken@5228
   316
    for (i = 0; i < NUM_SHADERS; ++i) {
slouken@5228
   317
        DestroyShaderProgram(ctx, &ctx->shaders[i]);
slouken@5228
   318
    }
slouken@5228
   319
    SDL_free(ctx);
slouken@5228
   320
}
slouken@5228
   321
slouken@5228
   322
#endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
slouken@5228
   323
slouken@5228
   324
/* vi: set ts=4 sw=4 expandtab: */