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.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2011 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #if SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED
    25 
    26 #include "SDL_stdinc.h"
    27 #include "SDL_log.h"
    28 #include "SDL_opengl.h"
    29 #include "SDL_video.h"
    30 #include "SDL_shaders_gl.h"
    31 
    32 /* OpenGL shader implementation */
    33 
    34 /*#define DEBUG_SHADERS*/
    35 
    36 typedef struct
    37 {
    38     GLenum program;
    39     GLenum vert_shader;
    40     GLenum frag_shader;
    41 } GL_ShaderData;
    42 
    43 struct GL_ShaderContext
    44 {
    45     GLenum (*glGetError)(void);
    46 
    47     PFNGLATTACHOBJECTARBPROC glAttachObjectARB;
    48     PFNGLCOMPILESHADERARBPROC glCompileShaderARB;
    49     PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObjectARB;
    50     PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObjectARB;
    51     PFNGLDELETEOBJECTARBPROC glDeleteObjectARB;
    52     PFNGLGETINFOLOGARBPROC glGetInfoLogARB;
    53     PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB;
    54     PFNGLGETUNIFORMLOCATIONARBPROC glGetUniformLocationARB;
    55     PFNGLLINKPROGRAMARBPROC glLinkProgramARB;
    56     PFNGLSHADERSOURCEARBPROC glShaderSourceARB;
    57     PFNGLUNIFORM1IARBPROC glUniform1iARB;
    58     PFNGLUNIFORM1FARBPROC glUniform1fARB;
    59     PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObjectARB;
    60 
    61     SDL_bool GL_ARB_texture_rectangle_supported;
    62 
    63     GL_ShaderData shaders[NUM_SHADERS];
    64 };
    65 
    66 /*
    67  * NOTE: Always use sampler2D, etc here. We'll #define them to the
    68  *  texture_rectangle versions if we choose to use that extension.
    69  */
    70 static const char *shader_source[NUM_SHADERS][2] =
    71 {
    72     /* SHADER_NONE */
    73     { NULL, NULL },
    74 
    75     /* SHADER_SOLID */
    76     {
    77         /* vertex shader */
    78 "varying vec4 v_color;\n"
    79 "\n"
    80 "void main()\n"
    81 "{\n"
    82 "    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
    83 "    v_color = gl_Color;\n"
    84 "}",
    85         /* fragment shader */
    86 "varying vec4 v_color;\n"
    87 "\n"
    88 "void main()\n"
    89 "{\n"
    90 "    gl_FragColor = v_color;\n"
    91 "}"
    92     },
    93 
    94     /* SHADER_RGB */
    95     {
    96         /* vertex shader */
    97 "varying vec4 v_color;\n"
    98 "varying vec2 v_texCoord;\n"
    99 "\n"
   100 "void main()\n"
   101 "{\n"
   102 "    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
   103 "    v_color = gl_Color;\n"
   104 "    v_texCoord = vec2(gl_MultiTexCoord0);\n"
   105 "}",
   106         /* fragment shader */
   107 "varying vec4 v_color;\n"
   108 "varying vec2 v_texCoord;\n"
   109 "uniform sampler2D tex0;\n"
   110 "\n"
   111 "void main()\n"
   112 "{\n"
   113 "    gl_FragColor = texture2D(tex0, v_texCoord) * v_color;\n"
   114 "}"
   115     },
   116 
   117     /* SHADER_YV12 */
   118     {
   119         /* vertex shader */
   120 "varying vec4 v_color;\n"
   121 "varying vec2 v_texCoord;\n"
   122 "\n"
   123 "void main()\n"
   124 "{\n"
   125 "    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
   126 "    v_color = gl_Color;\n"
   127 "    v_texCoord = vec2(gl_MultiTexCoord0);\n"
   128 "}",
   129         /* fragment shader */
   130 "varying vec4 v_color;\n"
   131 "varying vec2 v_texCoord;\n"
   132 "uniform sampler2D tex0; // Y \n"
   133 "uniform sampler2D tex1; // U \n"
   134 "uniform sampler2D tex2; // V \n"
   135 "\n"
   136 "// YUV offset \n"
   137 "const vec3 offset = vec3(-0.0625, -0.5, -0.5);\n"
   138 "\n"
   139 "// RGB coefficients \n"
   140 "const vec3 Rcoeff = vec3(1.164,  0.000,  1.596);\n"
   141 "const vec3 Gcoeff = vec3(1.164, -0.391, -0.813);\n"
   142 "const vec3 Bcoeff = vec3(1.164,  2.018,  0.000);\n"
   143 "\n"
   144 "void main()\n"
   145 "{\n"
   146 "    vec2 tcoord;\n"
   147 "    vec3 yuv, rgb;\n"
   148 "\n"
   149 "    // Get the Y value \n"
   150 "    tcoord = v_texCoord;\n"
   151 "    yuv.x = texture2D(tex0, tcoord).r;\n"
   152 "\n"
   153 "    // Get the U and V values \n"
   154 "    tcoord *= 0.5;\n"
   155 "    yuv.y = texture2D(tex1, tcoord).r;\n"
   156 "    yuv.z = texture2D(tex2, tcoord).r;\n"
   157 "\n"
   158 "    // Do the color transform \n"
   159 "    yuv += offset;\n"
   160 "    rgb.r = dot(yuv, Rcoeff);\n"
   161 "    rgb.g = dot(yuv, Gcoeff);\n"
   162 "    rgb.b = dot(yuv, Bcoeff);\n"
   163 "\n"
   164 "    // That was easy. :) \n"
   165 "    gl_FragColor = vec4(rgb, 1.0) * v_color;\n"
   166 "}"
   167     },
   168 };
   169 
   170 static SDL_bool
   171 CompileShader(GL_ShaderContext *ctx, GLenum shader, const char *defines, const char *source)
   172 {
   173     GLint status;
   174     const char *sources[2];
   175 
   176     sources[0] = defines;
   177     sources[1] = source;
   178 
   179     ctx->glShaderSourceARB(shader, SDL_arraysize(sources), sources, NULL);
   180     ctx->glCompileShaderARB(shader);
   181     ctx->glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
   182     if (status == 0) {
   183         GLint length;
   184         char *info;
   185 
   186         ctx->glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
   187         info = SDL_stack_alloc(char, length+1);
   188         ctx->glGetInfoLogARB(shader, length, NULL, info);
   189         SDL_LogError(SDL_LOG_CATEGORY_RENDER,
   190             "Failed to compile shader:\n%s%s\n%s", defines, source, info);
   191 #ifdef DEBUG_SHADERS
   192         fprintf(stderr,
   193             "Failed to compile shader:\n%s%s\n%s", defines, source, info);
   194 #endif
   195         SDL_stack_free(info);
   196 
   197         return SDL_FALSE;
   198     } else {
   199         return SDL_TRUE;
   200     }
   201 }
   202 
   203 static SDL_bool
   204 CompileShaderProgram(GL_ShaderContext *ctx, int index, GL_ShaderData *data)
   205 {
   206     const int num_tmus_bound = 4;
   207     const char *vert_defines = "";
   208     const char *frag_defines = "";
   209     int i;
   210     GLint location;
   211 
   212     if (index == SHADER_NONE) {
   213         return SDL_TRUE;
   214     }
   215 
   216     ctx->glGetError();
   217 
   218     /* Make sure we use the correct sampler type for our texture type */
   219     if (ctx->GL_ARB_texture_rectangle_supported) {
   220         frag_defines = 
   221 "#define sampler2D sampler2DRect\n"
   222 "#define texture2D texture2DRect\n";
   223     }
   224 
   225     /* Create one program object to rule them all */
   226     data->program = ctx->glCreateProgramObjectARB();
   227 
   228     /* Create the vertex shader */
   229     data->vert_shader = ctx->glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
   230     if (!CompileShader(ctx, data->vert_shader, vert_defines, shader_source[index][0])) {
   231         return SDL_FALSE;
   232     }
   233 
   234     /* Create the fragment shader */
   235     data->frag_shader = ctx->glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
   236     if (!CompileShader(ctx, data->frag_shader, frag_defines, shader_source[index][1])) {
   237         return SDL_FALSE;
   238     }
   239 
   240     /* ... and in the darkness bind them */
   241     ctx->glAttachObjectARB(data->program, data->vert_shader);
   242     ctx->glAttachObjectARB(data->program, data->frag_shader);
   243     ctx->glLinkProgramARB(data->program);
   244 
   245     /* Set up some uniform variables */
   246     ctx->glUseProgramObjectARB(data->program);
   247     for (i = 0; i < num_tmus_bound; ++i) {
   248         char tex_name[5];
   249         SDL_snprintf(tex_name, SDL_arraysize(tex_name), "tex%d", i);
   250         location = ctx->glGetUniformLocationARB(data->program, tex_name);
   251         if (location >= 0) {
   252             ctx->glUniform1iARB(location, i);
   253         }
   254     }
   255     ctx->glUseProgramObjectARB(0);
   256     
   257     return (ctx->glGetError() == GL_NO_ERROR);
   258 }
   259 
   260 static void
   261 DestroyShaderProgram(GL_ShaderContext *ctx, GL_ShaderData *data)
   262 {
   263     ctx->glDeleteObjectARB(data->vert_shader);
   264     ctx->glDeleteObjectARB(data->frag_shader);
   265     ctx->glDeleteObjectARB(data->program);
   266 }
   267 
   268 GL_ShaderContext *
   269 GL_CreateShaderContext()
   270 {
   271     GL_ShaderContext *ctx;
   272     SDL_bool shaders_supported;
   273     int i;
   274 
   275     ctx = (GL_ShaderContext *)SDL_calloc(1, sizeof(*ctx));
   276     if (!ctx) {
   277         return NULL;
   278     }
   279 
   280     if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
   281         || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
   282         ctx->GL_ARB_texture_rectangle_supported = SDL_TRUE;
   283     }
   284 
   285     /* Check for shader support */
   286     shaders_supported = SDL_FALSE;
   287     if (SDL_GL_ExtensionSupported("GL_ARB_shader_objects") &&
   288         SDL_GL_ExtensionSupported("GL_ARB_shading_language_100") &&
   289         SDL_GL_ExtensionSupported("GL_ARB_vertex_shader") &&
   290         SDL_GL_ExtensionSupported("GL_ARB_fragment_shader")) {
   291         ctx->glGetError = (GLenum (*)(void)) SDL_GL_GetProcAddress("glGetError");
   292         ctx->glAttachObjectARB = (PFNGLATTACHOBJECTARBPROC) SDL_GL_GetProcAddress("glAttachObjectARB");
   293         ctx->glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC) SDL_GL_GetProcAddress("glCompileShaderARB");
   294         ctx->glCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glCreateProgramObjectARB");
   295         ctx->glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC) SDL_GL_GetProcAddress("glCreateShaderObjectARB");
   296         ctx->glDeleteObjectARB = (PFNGLDELETEOBJECTARBPROC) SDL_GL_GetProcAddress("glDeleteObjectARB");
   297         ctx->glGetInfoLogARB = (PFNGLGETINFOLOGARBPROC) SDL_GL_GetProcAddress("glGetInfoLogARB");
   298         ctx->glGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC) SDL_GL_GetProcAddress("glGetObjectParameterivARB");
   299         ctx->glGetUniformLocationARB = (PFNGLGETUNIFORMLOCATIONARBPROC) SDL_GL_GetProcAddress("glGetUniformLocationARB");
   300         ctx->glLinkProgramARB = (PFNGLLINKPROGRAMARBPROC) SDL_GL_GetProcAddress("glLinkProgramARB");
   301         ctx->glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC) SDL_GL_GetProcAddress("glShaderSourceARB");
   302         ctx->glUniform1iARB = (PFNGLUNIFORM1IARBPROC) SDL_GL_GetProcAddress("glUniform1iARB");
   303         ctx->glUniform1fARB = (PFNGLUNIFORM1FARBPROC) SDL_GL_GetProcAddress("glUniform1fARB");
   304         ctx->glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glUseProgramObjectARB");
   305         if (ctx->glGetError &&
   306             ctx->glAttachObjectARB &&
   307             ctx->glCompileShaderARB &&
   308             ctx->glCreateProgramObjectARB &&
   309             ctx->glCreateShaderObjectARB &&
   310             ctx->glDeleteObjectARB &&
   311             ctx->glGetInfoLogARB &&
   312             ctx->glGetObjectParameterivARB &&
   313             ctx->glGetUniformLocationARB &&
   314             ctx->glLinkProgramARB &&
   315             ctx->glShaderSourceARB &&
   316             ctx->glUniform1iARB &&
   317             ctx->glUniform1fARB &&
   318             ctx->glUseProgramObjectARB) {
   319             shaders_supported = SDL_TRUE;
   320         }
   321     }
   322 
   323     if (!shaders_supported) {
   324         SDL_free(ctx);
   325         return NULL;
   326     }
   327 
   328     /* Compile all the shaders */
   329     for (i = 0; i < NUM_SHADERS; ++i) {
   330         if (!CompileShaderProgram(ctx, i, &ctx->shaders[i])) {
   331             GL_DestroyShaderContext(ctx);
   332             return NULL;
   333         }
   334     }
   335 
   336     /* We're done! */
   337     return ctx;
   338 }
   339 
   340 void
   341 GL_SelectShader(GL_ShaderContext *ctx, GL_Shader shader)
   342 {
   343     ctx->glUseProgramObjectARB(ctx->shaders[shader].program);
   344 }
   345 
   346 void
   347 GL_DestroyShaderContext(GL_ShaderContext *ctx)
   348 {
   349     int i;
   350 
   351     for (i = 0; i < NUM_SHADERS; ++i) {
   352         DestroyShaderProgram(ctx, &ctx->shaders[i]);
   353     }
   354     SDL_free(ctx);
   355 }
   356 
   357 #endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
   358 
   359 /* vi: set ts=4 sw=4 expandtab: */