src/render/opengl/SDL_shaders_gl.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 08 Feb 2011 20:23:52 -0800
changeset 5230 8efa43b915be
parent 5229 c015d3e63631
child 5231 e82908c86c9c
permissions -rw-r--r--
Fixed the shaders (needed to use texture2DRect) - thanks Ryan!
     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 typedef struct
    35 {
    36     GLenum program;
    37     GLenum vert_shader;
    38     GLenum frag_shader;
    39 } GL_ShaderData;
    40 
    41 struct GL_ShaderContext
    42 {
    43     GLenum (*glGetError)(void);
    44 
    45     PFNGLATTACHOBJECTARBPROC glAttachObjectARB;
    46     PFNGLCOMPILESHADERARBPROC glCompileShaderARB;
    47     PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObjectARB;
    48     PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObjectARB;
    49     PFNGLDELETEOBJECTARBPROC glDeleteObjectARB;
    50     PFNGLGETINFOLOGARBPROC glGetInfoLogARB;
    51     PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB;
    52     PFNGLGETUNIFORMLOCATIONARBPROC glGetUniformLocationARB;
    53     PFNGLLINKPROGRAMARBPROC glLinkProgramARB;
    54     PFNGLSHADERSOURCEARBPROC glShaderSourceARB;
    55     PFNGLUNIFORM1IARBPROC glUniform1iARB;
    56     PFNGLUNIFORM1FARBPROC glUniform1fARB;
    57     PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObjectARB;
    58 
    59     SDL_bool GL_ARB_texture_rectangle_supported;
    60 
    61     GL_Shader current_shader;
    62     GL_ShaderData shaders[NUM_SHADERS];
    63 };
    64 
    65 /*
    66  * NOTE: Always use sampler2D, etc here. We'll #define them to the
    67  *  texture_rectangle versions if we choose to use that extension.
    68  */
    69 static const char *shader_source[NUM_SHADERS][2] =
    70 {
    71     /* SHADER_NONE */
    72     { NULL, NULL },
    73 
    74     /* SHADER_SOLID */
    75     {
    76         /* vertex shader */
    77         " \
    78 varying vec4 v_color; \
    79  \
    80 void main() \
    81 { \
    82     gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; \
    83     v_color = gl_Color; \
    84 } \
    85 ",
    86         /* fragment shader */
    87         " \
    88 varying vec4 v_color; \
    89  \
    90 void main() \
    91 { \
    92     gl_FragColor = v_color; \
    93 } \
    94 "
    95     },
    96 
    97     /* SHADER_RGB */
    98     {
    99         /* vertex shader */
   100         " \
   101 varying vec4 v_color; \
   102 varying vec2 v_texCoord; \
   103  \
   104 void main() \
   105 { \
   106     gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; \
   107     v_color = gl_Color; \
   108     v_texCoord = vec2(gl_MultiTexCoord0); \
   109 } \
   110 ",
   111         /* fragment shader */
   112         " \
   113 varying vec4 v_color; \
   114 varying vec2 v_texCoord; \
   115 uniform sampler2D tex0; \
   116  \
   117 void main() \
   118 { \
   119     gl_FragColor = texture2D(tex0, v_texCoord) * v_color; \
   120 } \
   121 "
   122     },
   123 };
   124 
   125 static SDL_bool
   126 CompileShader(GL_ShaderContext *ctx, GLenum shader, const char *defines, const char *source)
   127 {
   128     GLint status;
   129     const char *sources[2];
   130 
   131     sources[0] = defines;
   132     sources[1] = source;
   133 
   134     ctx->glShaderSourceARB(shader, SDL_arraysize(sources), sources, NULL);
   135     ctx->glCompileShaderARB(shader);
   136     ctx->glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
   137     if (status == 0) {
   138         GLint length;
   139         char *info;
   140 
   141         ctx->glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
   142         info = SDL_stack_alloc(char, length+1);
   143         ctx->glGetInfoLogARB(shader, length, NULL, info);
   144         SDL_LogError(SDL_LOG_CATEGORY_RENDER,
   145             "Failed to compile shader:\n%s\n%s", source, info);
   146 fprintf(stderr, "Failed to compile shader:\n%s\n%s", source, info);
   147         SDL_stack_free(info);
   148 
   149         return SDL_FALSE;
   150     } else {
   151         return SDL_TRUE;
   152     }
   153 }
   154 
   155 static SDL_bool
   156 CompileShaderProgram(GL_ShaderContext *ctx, int index, GL_ShaderData *data)
   157 {
   158     const int num_tmus_bound = 4;
   159     const char *vert_defines = "";
   160     const char *frag_defines = "";
   161     int i;
   162     GLint location;
   163 
   164     if (index == SHADER_NONE) {
   165         return SDL_TRUE;
   166     }
   167 
   168     ctx->glGetError();
   169 
   170     /* Make sure we use the correct sampler type for our texture type */
   171     if (ctx->GL_ARB_texture_rectangle_supported) {
   172         frag_defines = 
   173 "#define sampler2D sampler2DRect\n"
   174 "#define texture2D texture2DRect\n";
   175     }
   176 
   177     /* Create one program object to rule them all */
   178     data->program = ctx->glCreateProgramObjectARB();
   179 
   180     /* Create the vertex shader */
   181     data->vert_shader = ctx->glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
   182     if (!CompileShader(ctx, data->vert_shader, vert_defines, shader_source[index][0])) {
   183         return SDL_FALSE;
   184     }
   185 
   186     /* Create the fragment shader */
   187     data->frag_shader = ctx->glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
   188     if (!CompileShader(ctx, data->frag_shader, frag_defines, shader_source[index][1])) {
   189         return SDL_FALSE;
   190     }
   191 
   192     /* ... and in the darkness bind them */
   193     ctx->glAttachObjectARB(data->program, data->vert_shader);
   194     ctx->glAttachObjectARB(data->program, data->frag_shader);
   195     ctx->glLinkProgramARB(data->program);
   196 
   197     /* Set up some uniform variables */
   198     ctx->glUseProgramObjectARB(data->program);
   199     for (i = 0; i < num_tmus_bound; ++i) {
   200         char tex_name[5];
   201         SDL_snprintf(tex_name, SDL_arraysize(tex_name), "tex%d", i);
   202         location = ctx->glGetUniformLocationARB(data->program, tex_name);
   203         if (location >= 0) {
   204             ctx->glUniform1iARB(location, i);
   205         }
   206     }
   207     ctx->glUseProgramObjectARB(0);
   208     
   209     return (ctx->glGetError() == GL_NO_ERROR);
   210 }
   211 
   212 static void
   213 DestroyShaderProgram(GL_ShaderContext *ctx, GL_ShaderData *data)
   214 {
   215     if (index == SHADER_NONE) {
   216         return;
   217     }
   218 
   219     ctx->glDeleteObjectARB(data->vert_shader);
   220     ctx->glDeleteObjectARB(data->frag_shader);
   221     ctx->glDeleteObjectARB(data->program);
   222 }
   223 
   224 GL_ShaderContext *
   225 GL_CreateShaderContext()
   226 {
   227     GL_ShaderContext *ctx;
   228     SDL_bool shaders_supported;
   229     int i;
   230 
   231     ctx = (GL_ShaderContext *)SDL_calloc(1, sizeof(*ctx));
   232     if (!ctx) {
   233         return NULL;
   234     }
   235 
   236     if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
   237         || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
   238         ctx->GL_ARB_texture_rectangle_supported = SDL_TRUE;
   239     }
   240 
   241     /* Check for shader support */
   242     shaders_supported = SDL_FALSE;
   243     if (SDL_GL_ExtensionSupported("GL_ARB_shader_objects") &&
   244         SDL_GL_ExtensionSupported("GL_ARB_shading_language_100") &&
   245         SDL_GL_ExtensionSupported("GL_ARB_vertex_shader") &&
   246         SDL_GL_ExtensionSupported("GL_ARB_fragment_shader")) {
   247         ctx->glGetError = (GLenum (*)(void)) SDL_GL_GetProcAddress("glGetError");
   248         ctx->glAttachObjectARB = (PFNGLATTACHOBJECTARBPROC) SDL_GL_GetProcAddress("glAttachObjectARB");
   249         ctx->glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC) SDL_GL_GetProcAddress("glCompileShaderARB");
   250         ctx->glCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glCreateProgramObjectARB");
   251         ctx->glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC) SDL_GL_GetProcAddress("glCreateShaderObjectARB");
   252         ctx->glDeleteObjectARB = (PFNGLDELETEOBJECTARBPROC) SDL_GL_GetProcAddress("glDeleteObjectARB");
   253         ctx->glGetInfoLogARB = (PFNGLGETINFOLOGARBPROC) SDL_GL_GetProcAddress("glGetInfoLogARB");
   254         ctx->glGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC) SDL_GL_GetProcAddress("glGetObjectParameterivARB");
   255         ctx->glGetUniformLocationARB = (PFNGLGETUNIFORMLOCATIONARBPROC) SDL_GL_GetProcAddress("glGetUniformLocationARB");
   256         ctx->glLinkProgramARB = (PFNGLLINKPROGRAMARBPROC) SDL_GL_GetProcAddress("glLinkProgramARB");
   257         ctx->glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC) SDL_GL_GetProcAddress("glShaderSourceARB");
   258         ctx->glUniform1iARB = (PFNGLUNIFORM1IARBPROC) SDL_GL_GetProcAddress("glUniform1iARB");
   259         ctx->glUniform1fARB = (PFNGLUNIFORM1FARBPROC) SDL_GL_GetProcAddress("glUniform1fARB");
   260         ctx->glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glUseProgramObjectARB");
   261         if (ctx->glGetError &&
   262             ctx->glAttachObjectARB &&
   263             ctx->glCompileShaderARB &&
   264             ctx->glCreateProgramObjectARB &&
   265             ctx->glCreateShaderObjectARB &&
   266             ctx->glDeleteObjectARB &&
   267             ctx->glGetInfoLogARB &&
   268             ctx->glGetObjectParameterivARB &&
   269             ctx->glGetUniformLocationARB &&
   270             ctx->glLinkProgramARB &&
   271             ctx->glShaderSourceARB &&
   272             ctx->glUniform1iARB &&
   273             ctx->glUniform1fARB &&
   274             ctx->glUseProgramObjectARB) {
   275             shaders_supported = SDL_TRUE;
   276         }
   277     }
   278 
   279     if (!shaders_supported) {
   280         GL_DestroyShaderContext(ctx);
   281         return NULL;
   282     }
   283 
   284     /* Compile all the shaders */
   285     for (i = 0; i < NUM_SHADERS; ++i) {
   286         if (!CompileShaderProgram(ctx, i, &ctx->shaders[i])) {
   287 fprintf(stderr, "Unable to compile shader!\n");
   288             GL_DestroyShaderContext(ctx);
   289             return NULL;
   290         }
   291     }
   292 
   293     /* We're done! */
   294     return ctx;
   295 }
   296 
   297 void
   298 GL_SelectShader(GL_ShaderContext *ctx, GL_Shader shader)
   299 {
   300     /* Nothing to do if there's no shader support */
   301     if (!ctx) {
   302         return;
   303     }
   304 
   305     /* Nothing to do if there's no shader change */
   306     if (shader == ctx->current_shader) {
   307         return;
   308     }
   309 
   310     ctx->glUseProgramObjectARB(ctx->shaders[shader].program);
   311     ctx->current_shader = shader;
   312 }
   313 
   314 void
   315 GL_DestroyShaderContext(GL_ShaderContext *ctx)
   316 {
   317     int i;
   318 
   319     for (i = 0; i < NUM_SHADERS; ++i) {
   320         DestroyShaderProgram(ctx, &ctx->shaders[i]);
   321     }
   322     SDL_free(ctx);
   323 }
   324 
   325 #endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
   326 
   327 /* vi: set ts=4 sw=4 expandtab: */