src/render/opengl/SDL_shaders_gl.c
author Gabriel Jacobo <gabomdq@gmail.com>
Wed, 21 Aug 2013 09:47:10 -0300
changeset 7678 286c42d7c5ed
parent 7677 871d43c6968a
child 8093 b43765095a6f
permissions -rw-r--r--
OCD fixes: Adds a space after /* (glory to regular expressions!)
slouken@5228
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@5228
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@5228
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@5228
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@5228
    20
*/
slouken@5228
    21
#include "SDL_config.h"
slouken@5228
    22
slouken@5228
    23
#if SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED
slouken@5228
    24
slouken@5228
    25
#include "SDL_stdinc.h"
slouken@5228
    26
#include "SDL_log.h"
slouken@5228
    27
#include "SDL_opengl.h"
slouken@5228
    28
#include "SDL_video.h"
slouken@5228
    29
#include "SDL_shaders_gl.h"
slouken@5228
    30
slouken@5228
    31
/* OpenGL shader implementation */
slouken@5228
    32
gabomdq@7678
    33
/* #define DEBUG_SHADERS */
slouken@5231
    34
slouken@5228
    35
typedef struct
slouken@5228
    36
{
icculus@5641
    37
    GLhandleARB program;
icculus@5641
    38
    GLhandleARB vert_shader;
icculus@5641
    39
    GLhandleARB frag_shader;
slouken@5228
    40
} GL_ShaderData;
slouken@5228
    41
slouken@5228
    42
struct GL_ShaderContext
slouken@5228
    43
{
slouken@5228
    44
    GLenum (*glGetError)(void);
slouken@5228
    45
slouken@5228
    46
    PFNGLATTACHOBJECTARBPROC glAttachObjectARB;
slouken@5228
    47
    PFNGLCOMPILESHADERARBPROC glCompileShaderARB;
slouken@5228
    48
    PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObjectARB;
slouken@5228
    49
    PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObjectARB;
slouken@5228
    50
    PFNGLDELETEOBJECTARBPROC glDeleteObjectARB;
slouken@5228
    51
    PFNGLGETINFOLOGARBPROC glGetInfoLogARB;
slouken@5228
    52
    PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB;
slouken@5228
    53
    PFNGLGETUNIFORMLOCATIONARBPROC glGetUniformLocationARB;
slouken@5228
    54
    PFNGLLINKPROGRAMARBPROC glLinkProgramARB;
slouken@5228
    55
    PFNGLSHADERSOURCEARBPROC glShaderSourceARB;
slouken@5228
    56
    PFNGLUNIFORM1IARBPROC glUniform1iARB;
slouken@5228
    57
    PFNGLUNIFORM1FARBPROC glUniform1fARB;
slouken@5228
    58
    PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObjectARB;
slouken@5228
    59
slouken@5230
    60
    SDL_bool GL_ARB_texture_rectangle_supported;
slouken@5230
    61
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@5231
    77
"varying vec4 v_color;\n"
slouken@5231
    78
"\n"
slouken@5231
    79
"void main()\n"
slouken@5231
    80
"{\n"
slouken@5231
    81
"    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
slouken@5231
    82
"    v_color = gl_Color;\n"
slouken@5231
    83
"}",
slouken@5228
    84
        /* fragment shader */
slouken@5231
    85
"varying vec4 v_color;\n"
slouken@5231
    86
"\n"
slouken@5231
    87
"void main()\n"
slouken@5231
    88
"{\n"
slouken@5231
    89
"    gl_FragColor = v_color;\n"
slouken@5231
    90
"}"
slouken@5228
    91
    },
slouken@5228
    92
slouken@5228
    93
    /* SHADER_RGB */
slouken@5228
    94
    {
slouken@5228
    95
        /* vertex shader */
slouken@5231
    96
"varying vec4 v_color;\n"
slouken@5231
    97
"varying vec2 v_texCoord;\n"
slouken@5231
    98
"\n"
slouken@5231
    99
"void main()\n"
slouken@5231
   100
"{\n"
slouken@5231
   101
"    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
slouken@5231
   102
"    v_color = gl_Color;\n"
slouken@5231
   103
"    v_texCoord = vec2(gl_MultiTexCoord0);\n"
slouken@5231
   104
"}",
slouken@5228
   105
        /* fragment shader */
slouken@5231
   106
"varying vec4 v_color;\n"
slouken@5231
   107
"varying vec2 v_texCoord;\n"
slouken@5231
   108
"uniform sampler2D tex0;\n"
slouken@5231
   109
"\n"
slouken@5231
   110
"void main()\n"
slouken@5231
   111
"{\n"
slouken@5231
   112
"    gl_FragColor = texture2D(tex0, v_texCoord) * v_color;\n"
slouken@5231
   113
"}"
slouken@5228
   114
    },
slouken@5238
   115
slouken@5238
   116
    /* SHADER_YV12 */
slouken@5238
   117
    {
slouken@5238
   118
        /* vertex shader */
slouken@5238
   119
"varying vec4 v_color;\n"
slouken@5238
   120
"varying vec2 v_texCoord;\n"
slouken@5238
   121
"\n"
slouken@5238
   122
"void main()\n"
slouken@5238
   123
"{\n"
slouken@5238
   124
"    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
slouken@5238
   125
"    v_color = gl_Color;\n"
slouken@5238
   126
"    v_texCoord = vec2(gl_MultiTexCoord0);\n"
slouken@5238
   127
"}",
slouken@5238
   128
        /* fragment shader */
slouken@5238
   129
"varying vec4 v_color;\n"
slouken@5238
   130
"varying vec2 v_texCoord;\n"
slouken@5238
   131
"uniform sampler2D tex0; // Y \n"
slouken@5238
   132
"uniform sampler2D tex1; // U \n"
slouken@5238
   133
"uniform sampler2D tex2; // V \n"
slouken@5238
   134
"\n"
slouken@5238
   135
"// YUV offset \n"
slouken@5238
   136
"const vec3 offset = vec3(-0.0625, -0.5, -0.5);\n"
slouken@5238
   137
"\n"
slouken@5238
   138
"// RGB coefficients \n"
slouken@5238
   139
"const vec3 Rcoeff = vec3(1.164,  0.000,  1.596);\n"
slouken@5238
   140
"const vec3 Gcoeff = vec3(1.164, -0.391, -0.813);\n"
slouken@5238
   141
"const vec3 Bcoeff = vec3(1.164,  2.018,  0.000);\n"
slouken@5238
   142
"\n"
slouken@5238
   143
"void main()\n"
slouken@5238
   144
"{\n"
slouken@5238
   145
"    vec2 tcoord;\n"
slouken@5238
   146
"    vec3 yuv, rgb;\n"
slouken@5238
   147
"\n"
slouken@5238
   148
"    // Get the Y value \n"
slouken@5238
   149
"    tcoord = v_texCoord;\n"
slouken@5238
   150
"    yuv.x = texture2D(tex0, tcoord).r;\n"
slouken@5238
   151
"\n"
slouken@5238
   152
"    // Get the U and V values \n"
slouken@5238
   153
"    tcoord *= 0.5;\n"
slouken@5238
   154
"    yuv.y = texture2D(tex1, tcoord).r;\n"
slouken@5238
   155
"    yuv.z = texture2D(tex2, tcoord).r;\n"
slouken@5238
   156
"\n"
slouken@5238
   157
"    // Do the color transform \n"
slouken@5238
   158
"    yuv += offset;\n"
slouken@5238
   159
"    rgb.r = dot(yuv, Rcoeff);\n"
slouken@5238
   160
"    rgb.g = dot(yuv, Gcoeff);\n"
slouken@5238
   161
"    rgb.b = dot(yuv, Bcoeff);\n"
slouken@5238
   162
"\n"
slouken@5238
   163
"    // That was easy. :) \n"
slouken@5238
   164
"    gl_FragColor = vec4(rgb, 1.0) * v_color;\n"
slouken@5238
   165
"}"
slouken@5238
   166
    },
slouken@5228
   167
};
slouken@5228
   168
slouken@5228
   169
static SDL_bool
icculus@5641
   170
CompileShader(GL_ShaderContext *ctx, GLhandleARB shader, const char *defines, const char *source)
slouken@5228
   171
{
slouken@5228
   172
    GLint status;
slouken@5230
   173
    const char *sources[2];
slouken@5228
   174
slouken@5230
   175
    sources[0] = defines;
slouken@5230
   176
    sources[1] = source;
slouken@5230
   177
slouken@5230
   178
    ctx->glShaderSourceARB(shader, SDL_arraysize(sources), sources, NULL);
slouken@5228
   179
    ctx->glCompileShaderARB(shader);
slouken@5228
   180
    ctx->glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
slouken@5228
   181
    if (status == 0) {
slouken@5228
   182
        GLint length;
slouken@5228
   183
        char *info;
slouken@5228
   184
slouken@5228
   185
        ctx->glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
slouken@5228
   186
        info = SDL_stack_alloc(char, length+1);
slouken@5228
   187
        ctx->glGetInfoLogARB(shader, length, NULL, info);
slouken@5228
   188
        SDL_LogError(SDL_LOG_CATEGORY_RENDER,
slouken@5231
   189
            "Failed to compile shader:\n%s%s\n%s", defines, source, info);
slouken@5231
   190
#ifdef DEBUG_SHADERS
slouken@5231
   191
        fprintf(stderr,
slouken@5231
   192
            "Failed to compile shader:\n%s%s\n%s", defines, source, info);
slouken@5231
   193
#endif
slouken@5228
   194
        SDL_stack_free(info);
slouken@5228
   195
slouken@5228
   196
        return SDL_FALSE;
slouken@5228
   197
    } else {
slouken@5228
   198
        return SDL_TRUE;
slouken@5228
   199
    }
slouken@5228
   200
}
slouken@5228
   201
slouken@5228
   202
static SDL_bool
slouken@5228
   203
CompileShaderProgram(GL_ShaderContext *ctx, int index, GL_ShaderData *data)
slouken@5228
   204
{
slouken@5228
   205
    const int num_tmus_bound = 4;
slouken@5230
   206
    const char *vert_defines = "";
slouken@5230
   207
    const char *frag_defines = "";
slouken@5228
   208
    int i;
slouken@5228
   209
    GLint location;
slouken@5228
   210
slouken@5228
   211
    if (index == SHADER_NONE) {
slouken@5228
   212
        return SDL_TRUE;
slouken@5228
   213
    }
slouken@5228
   214
slouken@5228
   215
    ctx->glGetError();
slouken@5228
   216
slouken@5230
   217
    /* Make sure we use the correct sampler type for our texture type */
slouken@5230
   218
    if (ctx->GL_ARB_texture_rectangle_supported) {
slouken@7191
   219
        frag_defines =
slouken@5230
   220
"#define sampler2D sampler2DRect\n"
slouken@5230
   221
"#define texture2D texture2DRect\n";
slouken@5230
   222
    }
slouken@5230
   223
slouken@5228
   224
    /* Create one program object to rule them all */
slouken@5228
   225
    data->program = ctx->glCreateProgramObjectARB();
slouken@5228
   226
slouken@5228
   227
    /* Create the vertex shader */
slouken@5228
   228
    data->vert_shader = ctx->glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
slouken@5230
   229
    if (!CompileShader(ctx, data->vert_shader, vert_defines, shader_source[index][0])) {
slouken@5228
   230
        return SDL_FALSE;
slouken@5228
   231
    }
slouken@5228
   232
slouken@5228
   233
    /* Create the fragment shader */
slouken@5228
   234
    data->frag_shader = ctx->glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
slouken@5230
   235
    if (!CompileShader(ctx, data->frag_shader, frag_defines, shader_source[index][1])) {
slouken@5228
   236
        return SDL_FALSE;
slouken@5228
   237
    }
slouken@5228
   238
slouken@5228
   239
    /* ... and in the darkness bind them */
slouken@5228
   240
    ctx->glAttachObjectARB(data->program, data->vert_shader);
slouken@5228
   241
    ctx->glAttachObjectARB(data->program, data->frag_shader);
slouken@5228
   242
    ctx->glLinkProgramARB(data->program);
slouken@5228
   243
slouken@5228
   244
    /* Set up some uniform variables */
slouken@5228
   245
    ctx->glUseProgramObjectARB(data->program);
slouken@5228
   246
    for (i = 0; i < num_tmus_bound; ++i) {
slouken@6903
   247
        char tex_name[10];
slouken@5228
   248
        SDL_snprintf(tex_name, SDL_arraysize(tex_name), "tex%d", i);
slouken@5228
   249
        location = ctx->glGetUniformLocationARB(data->program, tex_name);
slouken@5228
   250
        if (location >= 0) {
slouken@5229
   251
            ctx->glUniform1iARB(location, i);
slouken@5228
   252
        }
slouken@5228
   253
    }
slouken@5228
   254
    ctx->glUseProgramObjectARB(0);
slouken@7191
   255
slouken@5228
   256
    return (ctx->glGetError() == GL_NO_ERROR);
slouken@5228
   257
}
slouken@5228
   258
slouken@5228
   259
static void
slouken@5228
   260
DestroyShaderProgram(GL_ShaderContext *ctx, GL_ShaderData *data)
slouken@5228
   261
{
slouken@5228
   262
    ctx->glDeleteObjectARB(data->vert_shader);
slouken@5228
   263
    ctx->glDeleteObjectARB(data->frag_shader);
slouken@5228
   264
    ctx->glDeleteObjectARB(data->program);
slouken@5228
   265
}
slouken@5228
   266
slouken@5228
   267
GL_ShaderContext *
slouken@5228
   268
GL_CreateShaderContext()
slouken@5228
   269
{
slouken@5228
   270
    GL_ShaderContext *ctx;
slouken@5228
   271
    SDL_bool shaders_supported;
slouken@5228
   272
    int i;
slouken@5228
   273
slouken@5228
   274
    ctx = (GL_ShaderContext *)SDL_calloc(1, sizeof(*ctx));
slouken@5228
   275
    if (!ctx) {
slouken@5228
   276
        return NULL;
slouken@5228
   277
    }
slouken@5228
   278
slouken@5230
   279
    if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
slouken@5230
   280
        || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
slouken@5230
   281
        ctx->GL_ARB_texture_rectangle_supported = SDL_TRUE;
slouken@5230
   282
    }
slouken@5230
   283
slouken@5228
   284
    /* Check for shader support */
slouken@5228
   285
    shaders_supported = SDL_FALSE;
slouken@5228
   286
    if (SDL_GL_ExtensionSupported("GL_ARB_shader_objects") &&
slouken@5228
   287
        SDL_GL_ExtensionSupported("GL_ARB_shading_language_100") &&
slouken@5228
   288
        SDL_GL_ExtensionSupported("GL_ARB_vertex_shader") &&
slouken@5228
   289
        SDL_GL_ExtensionSupported("GL_ARB_fragment_shader")) {
slouken@5228
   290
        ctx->glGetError = (GLenum (*)(void)) SDL_GL_GetProcAddress("glGetError");
slouken@5228
   291
        ctx->glAttachObjectARB = (PFNGLATTACHOBJECTARBPROC) SDL_GL_GetProcAddress("glAttachObjectARB");
slouken@5228
   292
        ctx->glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC) SDL_GL_GetProcAddress("glCompileShaderARB");
slouken@5228
   293
        ctx->glCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glCreateProgramObjectARB");
slouken@5228
   294
        ctx->glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC) SDL_GL_GetProcAddress("glCreateShaderObjectARB");
slouken@5228
   295
        ctx->glDeleteObjectARB = (PFNGLDELETEOBJECTARBPROC) SDL_GL_GetProcAddress("glDeleteObjectARB");
slouken@5228
   296
        ctx->glGetInfoLogARB = (PFNGLGETINFOLOGARBPROC) SDL_GL_GetProcAddress("glGetInfoLogARB");
slouken@5228
   297
        ctx->glGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC) SDL_GL_GetProcAddress("glGetObjectParameterivARB");
slouken@5228
   298
        ctx->glGetUniformLocationARB = (PFNGLGETUNIFORMLOCATIONARBPROC) SDL_GL_GetProcAddress("glGetUniformLocationARB");
slouken@5228
   299
        ctx->glLinkProgramARB = (PFNGLLINKPROGRAMARBPROC) SDL_GL_GetProcAddress("glLinkProgramARB");
slouken@5228
   300
        ctx->glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC) SDL_GL_GetProcAddress("glShaderSourceARB");
slouken@5228
   301
        ctx->glUniform1iARB = (PFNGLUNIFORM1IARBPROC) SDL_GL_GetProcAddress("glUniform1iARB");
slouken@5228
   302
        ctx->glUniform1fARB = (PFNGLUNIFORM1FARBPROC) SDL_GL_GetProcAddress("glUniform1fARB");
slouken@5228
   303
        ctx->glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glUseProgramObjectARB");
slouken@5228
   304
        if (ctx->glGetError &&
slouken@5228
   305
            ctx->glAttachObjectARB &&
slouken@5228
   306
            ctx->glCompileShaderARB &&
slouken@5228
   307
            ctx->glCreateProgramObjectARB &&
slouken@5228
   308
            ctx->glCreateShaderObjectARB &&
slouken@5228
   309
            ctx->glDeleteObjectARB &&
slouken@5228
   310
            ctx->glGetInfoLogARB &&
slouken@5228
   311
            ctx->glGetObjectParameterivARB &&
slouken@5228
   312
            ctx->glGetUniformLocationARB &&
slouken@5228
   313
            ctx->glLinkProgramARB &&
slouken@5228
   314
            ctx->glShaderSourceARB &&
slouken@5228
   315
            ctx->glUniform1iARB &&
slouken@5228
   316
            ctx->glUniform1fARB &&
slouken@5228
   317
            ctx->glUseProgramObjectARB) {
slouken@5228
   318
            shaders_supported = SDL_TRUE;
slouken@5228
   319
        }
slouken@5228
   320
    }
slouken@5228
   321
slouken@5228
   322
    if (!shaders_supported) {
slouken@5237
   323
        SDL_free(ctx);
slouken@5228
   324
        return NULL;
slouken@5228
   325
    }
slouken@5228
   326
slouken@5228
   327
    /* Compile all the shaders */
slouken@5228
   328
    for (i = 0; i < NUM_SHADERS; ++i) {
slouken@5228
   329
        if (!CompileShaderProgram(ctx, i, &ctx->shaders[i])) {
slouken@5228
   330
            GL_DestroyShaderContext(ctx);
slouken@5228
   331
            return NULL;
slouken@5228
   332
        }
slouken@5228
   333
    }
slouken@5228
   334
slouken@5228
   335
    /* We're done! */
slouken@5228
   336
    return ctx;
slouken@5228
   337
}
slouken@5228
   338
slouken@5228
   339
void
slouken@5228
   340
GL_SelectShader(GL_ShaderContext *ctx, GL_Shader shader)
slouken@5228
   341
{
slouken@5228
   342
    ctx->glUseProgramObjectARB(ctx->shaders[shader].program);
slouken@5228
   343
}
slouken@5228
   344
slouken@5228
   345
void
slouken@5228
   346
GL_DestroyShaderContext(GL_ShaderContext *ctx)
slouken@5228
   347
{
slouken@5228
   348
    int i;
slouken@5228
   349
slouken@5228
   350
    for (i = 0; i < NUM_SHADERS; ++i) {
slouken@5228
   351
        DestroyShaderProgram(ctx, &ctx->shaders[i]);
slouken@5228
   352
    }
slouken@5228
   353
    SDL_free(ctx);
slouken@5228
   354
}
slouken@5228
   355
slouken@5228
   356
#endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
slouken@5228
   357
slouken@5228
   358
/* vi: set ts=4 sw=4 expandtab: */