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