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