src/render/opengl/SDL_shaders_gl.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 19 Feb 2011 21:51:21 -0800
changeset 5355 cc2215cda170
parent 5262 b530ef003506
child 5535 96594ac5fd1a
permissions -rwxr-xr-x
Added OpenGL state caching for decent speed improvement.
slouken@5228
     1
/*
slouken@5228
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 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_ShaderData shaders[NUM_SHADERS];
slouken@5228
    64
};
slouken@5228
    65
slouken@5230
    66
/*
slouken@5230
    67
 * NOTE: Always use sampler2D, etc here. We'll #define them to the
slouken@5230
    68
 *  texture_rectangle versions if we choose to use that extension.
slouken@5230
    69
 */
slouken@5228
    70
static const char *shader_source[NUM_SHADERS][2] =
slouken@5228
    71
{
slouken@5228
    72
    /* SHADER_NONE */
slouken@5228
    73
    { NULL, NULL },
slouken@5228
    74
slouken@5228
    75
    /* SHADER_SOLID */
slouken@5228
    76
    {
slouken@5228
    77
        /* vertex shader */
slouken@5231
    78
"varying vec4 v_color;\n"
slouken@5231
    79
"\n"
slouken@5231
    80
"void main()\n"
slouken@5231
    81
"{\n"
slouken@5231
    82
"    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
slouken@5231
    83
"    v_color = gl_Color;\n"
slouken@5231
    84
"}",
slouken@5228
    85
        /* fragment shader */
slouken@5231
    86
"varying vec4 v_color;\n"
slouken@5231
    87
"\n"
slouken@5231
    88
"void main()\n"
slouken@5231
    89
"{\n"
slouken@5231
    90
"    gl_FragColor = v_color;\n"
slouken@5231
    91
"}"
slouken@5228
    92
    },
slouken@5228
    93
slouken@5228
    94
    /* SHADER_RGB */
slouken@5228
    95
    {
slouken@5228
    96
        /* vertex shader */
slouken@5231
    97
"varying vec4 v_color;\n"
slouken@5231
    98
"varying vec2 v_texCoord;\n"
slouken@5231
    99
"\n"
slouken@5231
   100
"void main()\n"
slouken@5231
   101
"{\n"
slouken@5231
   102
"    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
slouken@5231
   103
"    v_color = gl_Color;\n"
slouken@5231
   104
"    v_texCoord = vec2(gl_MultiTexCoord0);\n"
slouken@5231
   105
"}",
slouken@5228
   106
        /* fragment shader */
slouken@5231
   107
"varying vec4 v_color;\n"
slouken@5231
   108
"varying vec2 v_texCoord;\n"
slouken@5231
   109
"uniform sampler2D tex0;\n"
slouken@5231
   110
"\n"
slouken@5231
   111
"void main()\n"
slouken@5231
   112
"{\n"
slouken@5231
   113
"    gl_FragColor = texture2D(tex0, v_texCoord) * v_color;\n"
slouken@5231
   114
"}"
slouken@5228
   115
    },
slouken@5238
   116
slouken@5238
   117
    /* SHADER_YV12 */
slouken@5238
   118
    {
slouken@5238
   119
        /* vertex shader */
slouken@5238
   120
"varying vec4 v_color;\n"
slouken@5238
   121
"varying vec2 v_texCoord;\n"
slouken@5238
   122
"\n"
slouken@5238
   123
"void main()\n"
slouken@5238
   124
"{\n"
slouken@5238
   125
"    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
slouken@5238
   126
"    v_color = gl_Color;\n"
slouken@5238
   127
"    v_texCoord = vec2(gl_MultiTexCoord0);\n"
slouken@5238
   128
"}",
slouken@5238
   129
        /* fragment shader */
slouken@5238
   130
"varying vec4 v_color;\n"
slouken@5238
   131
"varying vec2 v_texCoord;\n"
slouken@5238
   132
"uniform sampler2D tex0; // Y \n"
slouken@5238
   133
"uniform sampler2D tex1; // U \n"
slouken@5238
   134
"uniform sampler2D tex2; // V \n"
slouken@5238
   135
"\n"
slouken@5238
   136
"// YUV offset \n"
slouken@5238
   137
"const vec3 offset = vec3(-0.0625, -0.5, -0.5);\n"
slouken@5238
   138
"\n"
slouken@5238
   139
"// RGB coefficients \n"
slouken@5238
   140
"const vec3 Rcoeff = vec3(1.164,  0.000,  1.596);\n"
slouken@5238
   141
"const vec3 Gcoeff = vec3(1.164, -0.391, -0.813);\n"
slouken@5238
   142
"const vec3 Bcoeff = vec3(1.164,  2.018,  0.000);\n"
slouken@5238
   143
"\n"
slouken@5238
   144
"void main()\n"
slouken@5238
   145
"{\n"
slouken@5238
   146
"    vec2 tcoord;\n"
slouken@5238
   147
"    vec3 yuv, rgb;\n"
slouken@5238
   148
"\n"
slouken@5238
   149
"    // Get the Y value \n"
slouken@5238
   150
"    tcoord = v_texCoord;\n"
slouken@5238
   151
"    yuv.x = texture2D(tex0, tcoord).r;\n"
slouken@5238
   152
"\n"
slouken@5238
   153
"    // Get the U and V values \n"
slouken@5238
   154
"    tcoord *= 0.5;\n"
slouken@5238
   155
"    yuv.y = texture2D(tex1, tcoord).r;\n"
slouken@5238
   156
"    yuv.z = texture2D(tex2, tcoord).r;\n"
slouken@5238
   157
"\n"
slouken@5238
   158
"    // Do the color transform \n"
slouken@5238
   159
"    yuv += offset;\n"
slouken@5238
   160
"    rgb.r = dot(yuv, Rcoeff);\n"
slouken@5238
   161
"    rgb.g = dot(yuv, Gcoeff);\n"
slouken@5238
   162
"    rgb.b = dot(yuv, Bcoeff);\n"
slouken@5238
   163
"\n"
slouken@5238
   164
"    // That was easy. :) \n"
slouken@5238
   165
"    gl_FragColor = vec4(rgb, 1.0) * v_color;\n"
slouken@5238
   166
"}"
slouken@5238
   167
    },
slouken@5228
   168
};
slouken@5228
   169
slouken@5228
   170
static SDL_bool
slouken@5230
   171
CompileShader(GL_ShaderContext *ctx, GLenum shader, const char *defines, const char *source)
slouken@5228
   172
{
slouken@5228
   173
    GLint status;
slouken@5230
   174
    const char *sources[2];
slouken@5228
   175
slouken@5230
   176
    sources[0] = defines;
slouken@5230
   177
    sources[1] = source;
slouken@5230
   178
slouken@5230
   179
    ctx->glShaderSourceARB(shader, SDL_arraysize(sources), sources, NULL);
slouken@5228
   180
    ctx->glCompileShaderARB(shader);
slouken@5228
   181
    ctx->glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
slouken@5228
   182
    if (status == 0) {
slouken@5228
   183
        GLint length;
slouken@5228
   184
        char *info;
slouken@5228
   185
slouken@5228
   186
        ctx->glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
slouken@5228
   187
        info = SDL_stack_alloc(char, length+1);
slouken@5228
   188
        ctx->glGetInfoLogARB(shader, length, NULL, info);
slouken@5228
   189
        SDL_LogError(SDL_LOG_CATEGORY_RENDER,
slouken@5231
   190
            "Failed to compile shader:\n%s%s\n%s", defines, source, info);
slouken@5231
   191
#ifdef DEBUG_SHADERS
slouken@5231
   192
        fprintf(stderr,
slouken@5231
   193
            "Failed to compile shader:\n%s%s\n%s", defines, source, info);
slouken@5231
   194
#endif
slouken@5228
   195
        SDL_stack_free(info);
slouken@5228
   196
slouken@5228
   197
        return SDL_FALSE;
slouken@5228
   198
    } else {
slouken@5228
   199
        return SDL_TRUE;
slouken@5228
   200
    }
slouken@5228
   201
}
slouken@5228
   202
slouken@5228
   203
static SDL_bool
slouken@5228
   204
CompileShaderProgram(GL_ShaderContext *ctx, int index, GL_ShaderData *data)
slouken@5228
   205
{
slouken@5228
   206
    const int num_tmus_bound = 4;
slouken@5230
   207
    const char *vert_defines = "";
slouken@5230
   208
    const char *frag_defines = "";
slouken@5228
   209
    int i;
slouken@5228
   210
    GLint location;
slouken@5228
   211
slouken@5228
   212
    if (index == SHADER_NONE) {
slouken@5228
   213
        return SDL_TRUE;
slouken@5228
   214
    }
slouken@5228
   215
slouken@5228
   216
    ctx->glGetError();
slouken@5228
   217
slouken@5230
   218
    /* Make sure we use the correct sampler type for our texture type */
slouken@5230
   219
    if (ctx->GL_ARB_texture_rectangle_supported) {
slouken@5230
   220
        frag_defines = 
slouken@5230
   221
"#define sampler2D sampler2DRect\n"
slouken@5230
   222
"#define texture2D texture2DRect\n";
slouken@5230
   223
    }
slouken@5230
   224
slouken@5228
   225
    /* Create one program object to rule them all */
slouken@5228
   226
    data->program = ctx->glCreateProgramObjectARB();
slouken@5228
   227
slouken@5228
   228
    /* Create the vertex shader */
slouken@5228
   229
    data->vert_shader = ctx->glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
slouken@5230
   230
    if (!CompileShader(ctx, data->vert_shader, vert_defines, shader_source[index][0])) {
slouken@5228
   231
        return SDL_FALSE;
slouken@5228
   232
    }
slouken@5228
   233
slouken@5228
   234
    /* Create the fragment shader */
slouken@5228
   235
    data->frag_shader = ctx->glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
slouken@5230
   236
    if (!CompileShader(ctx, data->frag_shader, frag_defines, shader_source[index][1])) {
slouken@5228
   237
        return SDL_FALSE;
slouken@5228
   238
    }
slouken@5228
   239
slouken@5228
   240
    /* ... and in the darkness bind them */
slouken@5228
   241
    ctx->glAttachObjectARB(data->program, data->vert_shader);
slouken@5228
   242
    ctx->glAttachObjectARB(data->program, data->frag_shader);
slouken@5228
   243
    ctx->glLinkProgramARB(data->program);
slouken@5228
   244
slouken@5228
   245
    /* Set up some uniform variables */
slouken@5228
   246
    ctx->glUseProgramObjectARB(data->program);
slouken@5228
   247
    for (i = 0; i < num_tmus_bound; ++i) {
slouken@5228
   248
        char tex_name[5];
slouken@5228
   249
        SDL_snprintf(tex_name, SDL_arraysize(tex_name), "tex%d", i);
slouken@5228
   250
        location = ctx->glGetUniformLocationARB(data->program, tex_name);
slouken@5228
   251
        if (location >= 0) {
slouken@5229
   252
            ctx->glUniform1iARB(location, i);
slouken@5228
   253
        }
slouken@5228
   254
    }
slouken@5228
   255
    ctx->glUseProgramObjectARB(0);
slouken@5228
   256
    
slouken@5228
   257
    return (ctx->glGetError() == GL_NO_ERROR);
slouken@5228
   258
}
slouken@5228
   259
slouken@5228
   260
static void
slouken@5228
   261
DestroyShaderProgram(GL_ShaderContext *ctx, GL_ShaderData *data)
slouken@5228
   262
{
slouken@5228
   263
    ctx->glDeleteObjectARB(data->vert_shader);
slouken@5228
   264
    ctx->glDeleteObjectARB(data->frag_shader);
slouken@5228
   265
    ctx->glDeleteObjectARB(data->program);
slouken@5228
   266
}
slouken@5228
   267
slouken@5228
   268
GL_ShaderContext *
slouken@5228
   269
GL_CreateShaderContext()
slouken@5228
   270
{
slouken@5228
   271
    GL_ShaderContext *ctx;
slouken@5228
   272
    SDL_bool shaders_supported;
slouken@5228
   273
    int i;
slouken@5228
   274
slouken@5228
   275
    ctx = (GL_ShaderContext *)SDL_calloc(1, sizeof(*ctx));
slouken@5228
   276
    if (!ctx) {
slouken@5228
   277
        return NULL;
slouken@5228
   278
    }
slouken@5228
   279
slouken@5230
   280
    if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
slouken@5230
   281
        || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
slouken@5230
   282
        ctx->GL_ARB_texture_rectangle_supported = SDL_TRUE;
slouken@5230
   283
    }
slouken@5230
   284
slouken@5228
   285
    /* Check for shader support */
slouken@5228
   286
    shaders_supported = SDL_FALSE;
slouken@5228
   287
    if (SDL_GL_ExtensionSupported("GL_ARB_shader_objects") &&
slouken@5228
   288
        SDL_GL_ExtensionSupported("GL_ARB_shading_language_100") &&
slouken@5228
   289
        SDL_GL_ExtensionSupported("GL_ARB_vertex_shader") &&
slouken@5228
   290
        SDL_GL_ExtensionSupported("GL_ARB_fragment_shader")) {
slouken@5228
   291
        ctx->glGetError = (GLenum (*)(void)) SDL_GL_GetProcAddress("glGetError");
slouken@5228
   292
        ctx->glAttachObjectARB = (PFNGLATTACHOBJECTARBPROC) SDL_GL_GetProcAddress("glAttachObjectARB");
slouken@5228
   293
        ctx->glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC) SDL_GL_GetProcAddress("glCompileShaderARB");
slouken@5228
   294
        ctx->glCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glCreateProgramObjectARB");
slouken@5228
   295
        ctx->glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC) SDL_GL_GetProcAddress("glCreateShaderObjectARB");
slouken@5228
   296
        ctx->glDeleteObjectARB = (PFNGLDELETEOBJECTARBPROC) SDL_GL_GetProcAddress("glDeleteObjectARB");
slouken@5228
   297
        ctx->glGetInfoLogARB = (PFNGLGETINFOLOGARBPROC) SDL_GL_GetProcAddress("glGetInfoLogARB");
slouken@5228
   298
        ctx->glGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC) SDL_GL_GetProcAddress("glGetObjectParameterivARB");
slouken@5228
   299
        ctx->glGetUniformLocationARB = (PFNGLGETUNIFORMLOCATIONARBPROC) SDL_GL_GetProcAddress("glGetUniformLocationARB");
slouken@5228
   300
        ctx->glLinkProgramARB = (PFNGLLINKPROGRAMARBPROC) SDL_GL_GetProcAddress("glLinkProgramARB");
slouken@5228
   301
        ctx->glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC) SDL_GL_GetProcAddress("glShaderSourceARB");
slouken@5228
   302
        ctx->glUniform1iARB = (PFNGLUNIFORM1IARBPROC) SDL_GL_GetProcAddress("glUniform1iARB");
slouken@5228
   303
        ctx->glUniform1fARB = (PFNGLUNIFORM1FARBPROC) SDL_GL_GetProcAddress("glUniform1fARB");
slouken@5228
   304
        ctx->glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glUseProgramObjectARB");
slouken@5228
   305
        if (ctx->glGetError &&
slouken@5228
   306
            ctx->glAttachObjectARB &&
slouken@5228
   307
            ctx->glCompileShaderARB &&
slouken@5228
   308
            ctx->glCreateProgramObjectARB &&
slouken@5228
   309
            ctx->glCreateShaderObjectARB &&
slouken@5228
   310
            ctx->glDeleteObjectARB &&
slouken@5228
   311
            ctx->glGetInfoLogARB &&
slouken@5228
   312
            ctx->glGetObjectParameterivARB &&
slouken@5228
   313
            ctx->glGetUniformLocationARB &&
slouken@5228
   314
            ctx->glLinkProgramARB &&
slouken@5228
   315
            ctx->glShaderSourceARB &&
slouken@5228
   316
            ctx->glUniform1iARB &&
slouken@5228
   317
            ctx->glUniform1fARB &&
slouken@5228
   318
            ctx->glUseProgramObjectARB) {
slouken@5228
   319
            shaders_supported = SDL_TRUE;
slouken@5228
   320
        }
slouken@5228
   321
    }
slouken@5228
   322
slouken@5228
   323
    if (!shaders_supported) {
slouken@5237
   324
        SDL_free(ctx);
slouken@5228
   325
        return NULL;
slouken@5228
   326
    }
slouken@5228
   327
slouken@5228
   328
    /* Compile all the shaders */
slouken@5228
   329
    for (i = 0; i < NUM_SHADERS; ++i) {
slouken@5228
   330
        if (!CompileShaderProgram(ctx, i, &ctx->shaders[i])) {
slouken@5228
   331
            GL_DestroyShaderContext(ctx);
slouken@5228
   332
            return NULL;
slouken@5228
   333
        }
slouken@5228
   334
    }
slouken@5228
   335
slouken@5228
   336
    /* We're done! */
slouken@5228
   337
    return ctx;
slouken@5228
   338
}
slouken@5228
   339
slouken@5228
   340
void
slouken@5228
   341
GL_SelectShader(GL_ShaderContext *ctx, GL_Shader shader)
slouken@5228
   342
{
slouken@5228
   343
    ctx->glUseProgramObjectARB(ctx->shaders[shader].program);
slouken@5228
   344
}
slouken@5228
   345
slouken@5228
   346
void
slouken@5228
   347
GL_DestroyShaderContext(GL_ShaderContext *ctx)
slouken@5228
   348
{
slouken@5228
   349
    int i;
slouken@5228
   350
slouken@5228
   351
    for (i = 0; i < NUM_SHADERS; ++i) {
slouken@5228
   352
        DestroyShaderProgram(ctx, &ctx->shaders[i]);
slouken@5228
   353
    }
slouken@5228
   354
    SDL_free(ctx);
slouken@5228
   355
}
slouken@5228
   356
slouken@5228
   357
#endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
slouken@5228
   358
slouken@5228
   359
/* vi: set ts=4 sw=4 expandtab: */