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