src/render/opengl/SDL_shaders_gl.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 08 Feb 2011 16:27:52 -0800
changeset 5228 811beeb698f9
child 5229 c015d3e63631
permissions -rw-r--r--
Beginning of a framework for OpenGL shaders
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5228
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     2
    SDL - Simple DirectMedia Layer
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     3
    Copyright (C) 1997-2010 Sam Lantinga
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     5
    This library is free software; you can redistribute it and/or
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     6
    modify it under the terms of the GNU Lesser General Public
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     7
    License as published by the Free Software Foundation; either
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
    version 2.1 of the License, or (at your option) any later version.
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     9
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    10
    This library is distributed in the hope that it will be useful,
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
    Lesser General Public License for more details.
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    14
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
    You should have received a copy of the GNU Lesser General Public
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    16
    License along with this library; if not, write to the Free Software
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    18
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    19
    Sam Lantinga
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
    slouken@libsdl.org
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
*/
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
#include "SDL_config.h"
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
#if SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
#include "SDL_stdinc.h"
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
#include "SDL_log.h"
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
#include "SDL_opengl.h"
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
#include "SDL_video.h"
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
#include "SDL_shaders_gl.h"
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
/* OpenGL shader implementation */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
typedef struct
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
{
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
    GLenum program;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
    GLenum vert_shader;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
    GLenum frag_shader;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
} GL_ShaderData;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
struct GL_ShaderContext
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
{
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
    GLenum (*glGetError)(void);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    45
    PFNGLATTACHOBJECTARBPROC glAttachObjectARB;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
    PFNGLCOMPILESHADERARBPROC glCompileShaderARB;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
    PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObjectARB;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
    PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObjectARB;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
    PFNGLDELETEOBJECTARBPROC glDeleteObjectARB;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
    PFNGLGETINFOLOGARBPROC glGetInfoLogARB;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
    PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
    PFNGLGETUNIFORMLOCATIONARBPROC glGetUniformLocationARB;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
    PFNGLLINKPROGRAMARBPROC glLinkProgramARB;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
    PFNGLSHADERSOURCEARBPROC glShaderSourceARB;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
    PFNGLUNIFORM1IARBPROC glUniform1iARB;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
    PFNGLUNIFORM1FARBPROC glUniform1fARB;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
    PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObjectARB;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    58
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
    GL_Shader current_shader;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
    GL_ShaderData shaders[NUM_SHADERS];
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
};
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
static const char *shader_source[NUM_SHADERS][2] =
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
{
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
    /* SHADER_NONE */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
    { NULL, NULL },
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
    /* SHADER_SOLID */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
    {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
        /* vertex shader */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
        " \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
varying vec4 v_color; \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
 \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
void main() \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
{ \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
    gl_Position = ftransform(); \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
    v_color = gl_Color; \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
} \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
",
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
        /* fragment shader */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    81
        " \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
varying vec4 v_color; \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
 \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
void main() \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
{ \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
    gl_FragColor = v_color; \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    87
} \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
"
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    89
    },
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    90
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    91
    /* SHADER_RGB */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    92
    {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    93
        /* vertex shader */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    94
        " \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    95
varying vec4 v_color; \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    96
varying vec2 v_texCoord; \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    97
 \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    98
void main() \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    99
{ \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   100
    gl_Position = ftransform(); \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   101
    v_color = gl_Color; \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   102
    v_texCoord = vec2(gl_MultiTexCoord0); \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   103
} \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   104
",
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   105
        /* fragment shader */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   106
        " \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   107
varying vec4 v_color; \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   108
varying vec2 v_texCoord; \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   109
uniform sampler2D tex0; \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
 \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   111
void main() \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
{ \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
    gl_FragColor = texture2D(tex0, v_texCoord) * v_color; \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
} \
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
"
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   116
    },
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   117
};
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   118
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   119
static SDL_bool
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   120
CompileShader(GL_ShaderContext *ctx, GLenum shader, const char *source)
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   121
{
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   122
    GLint status;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   123
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   124
    ctx->glShaderSourceARB(shader, 1, &source, NULL);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   125
    ctx->glCompileShaderARB(shader);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   126
    ctx->glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   127
    if (status == 0) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   128
        GLint length;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   129
        char *info;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   130
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   131
        ctx->glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   132
        info = SDL_stack_alloc(char, length+1);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   133
        ctx->glGetInfoLogARB(shader, length, NULL, info);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   134
        SDL_LogError(SDL_LOG_CATEGORY_RENDER,
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   135
            "Failed to compile shader:\n%s\n%s", source, info);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   136
fprintf(stderr, "Failed to compile shader:\n%s\n%s", source, info);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   137
        SDL_stack_free(info);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   138
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   139
        return SDL_FALSE;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   140
    } else {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   141
        return SDL_TRUE;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   142
    }
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   143
}
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   144
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   145
static SDL_bool
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   146
CompileShaderProgram(GL_ShaderContext *ctx, int index, GL_ShaderData *data)
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   147
{
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   148
    const int num_tmus_bound = 4;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   149
    GLint status;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   150
    int i;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   151
    GLint location;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   152
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   153
    if (index == SHADER_NONE) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   154
        return SDL_TRUE;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   155
    }
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   156
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   157
    ctx->glGetError();
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   158
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   159
    /* Create one program object to rule them all */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   160
    data->program = ctx->glCreateProgramObjectARB();
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   161
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   162
    /* Create the vertex shader */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   163
    data->vert_shader = ctx->glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   164
    if (!CompileShader(ctx, data->vert_shader, shader_source[index][0])) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   165
        return SDL_FALSE;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   166
    }
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   167
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   168
    /* Create the fragment shader */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   169
    data->frag_shader = ctx->glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   170
    if (!CompileShader(ctx, data->frag_shader, shader_source[index][1])) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   171
        return SDL_FALSE;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   172
    }
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   173
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   174
    /* ... and in the darkness bind them */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   175
    ctx->glAttachObjectARB(data->program, data->vert_shader);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   176
    ctx->glAttachObjectARB(data->program, data->frag_shader);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   177
    ctx->glLinkProgramARB(data->program);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   178
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   179
    /* Set up some uniform variables */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   180
    ctx->glUseProgramObjectARB(data->program);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   181
    for (i = 0; i < num_tmus_bound; ++i) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   182
        char tex_name[5];
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   183
        SDL_snprintf(tex_name, SDL_arraysize(tex_name), "tex%d", i);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
        location = ctx->glGetUniformLocationARB(data->program, tex_name);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   185
        if (location >= 0) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   186
            ctx->glUniform1iARB(location, 1);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   187
        }
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   188
    }
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   189
    ctx->glUseProgramObjectARB(0);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   190
    
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   191
    return (ctx->glGetError() == GL_NO_ERROR);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   192
}
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   193
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   194
static void
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   195
DestroyShaderProgram(GL_ShaderContext *ctx, GL_ShaderData *data)
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   196
{
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   197
    if (index == SHADER_NONE) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   198
        return;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   199
    }
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   200
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   201
    ctx->glDeleteObjectARB(data->vert_shader);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   202
    ctx->glDeleteObjectARB(data->frag_shader);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   203
    ctx->glDeleteObjectARB(data->program);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   204
}
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   205
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   206
GL_ShaderContext *
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   207
GL_CreateShaderContext()
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   208
{
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   209
    GL_ShaderContext *ctx;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   210
    SDL_bool shaders_supported;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   211
    int i;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   212
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   213
    ctx = (GL_ShaderContext *)SDL_calloc(1, sizeof(*ctx));
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   214
    if (!ctx) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   215
        return NULL;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   216
    }
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   217
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   218
    /* Check for shader support */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   219
    shaders_supported = SDL_FALSE;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   220
    if (SDL_GL_ExtensionSupported("GL_ARB_shader_objects") &&
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   221
        SDL_GL_ExtensionSupported("GL_ARB_shading_language_100") &&
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   222
        SDL_GL_ExtensionSupported("GL_ARB_vertex_shader") &&
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   223
        SDL_GL_ExtensionSupported("GL_ARB_fragment_shader")) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   224
        ctx->glGetError = (GLenum (*)(void)) SDL_GL_GetProcAddress("glGetError");
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   225
        ctx->glAttachObjectARB = (PFNGLATTACHOBJECTARBPROC) SDL_GL_GetProcAddress("glAttachObjectARB");
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   226
        ctx->glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC) SDL_GL_GetProcAddress("glCompileShaderARB");
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   227
        ctx->glCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glCreateProgramObjectARB");
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   228
        ctx->glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC) SDL_GL_GetProcAddress("glCreateShaderObjectARB");
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   229
        ctx->glDeleteObjectARB = (PFNGLDELETEOBJECTARBPROC) SDL_GL_GetProcAddress("glDeleteObjectARB");
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   230
        ctx->glGetInfoLogARB = (PFNGLGETINFOLOGARBPROC) SDL_GL_GetProcAddress("glGetInfoLogARB");
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   231
        ctx->glGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC) SDL_GL_GetProcAddress("glGetObjectParameterivARB");
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   232
        ctx->glGetUniformLocationARB = (PFNGLGETUNIFORMLOCATIONARBPROC) SDL_GL_GetProcAddress("glGetUniformLocationARB");
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   233
        ctx->glLinkProgramARB = (PFNGLLINKPROGRAMARBPROC) SDL_GL_GetProcAddress("glLinkProgramARB");
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   234
        ctx->glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC) SDL_GL_GetProcAddress("glShaderSourceARB");
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   235
        ctx->glUniform1iARB = (PFNGLUNIFORM1IARBPROC) SDL_GL_GetProcAddress("glUniform1iARB");
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   236
        ctx->glUniform1fARB = (PFNGLUNIFORM1FARBPROC) SDL_GL_GetProcAddress("glUniform1fARB");
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   237
        ctx->glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glUseProgramObjectARB");
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   238
        if (ctx->glGetError &&
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   239
            ctx->glAttachObjectARB &&
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   240
            ctx->glCompileShaderARB &&
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   241
            ctx->glCreateProgramObjectARB &&
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   242
            ctx->glCreateShaderObjectARB &&
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   243
            ctx->glDeleteObjectARB &&
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   244
            ctx->glGetInfoLogARB &&
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   245
            ctx->glGetObjectParameterivARB &&
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   246
            ctx->glGetUniformLocationARB &&
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   247
            ctx->glLinkProgramARB &&
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   248
            ctx->glShaderSourceARB &&
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   249
            ctx->glUniform1iARB &&
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   250
            ctx->glUniform1fARB &&
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   251
            ctx->glUseProgramObjectARB) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   252
            shaders_supported = SDL_TRUE;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   253
        }
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   254
    }
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   255
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   256
    if (!shaders_supported) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   257
        GL_DestroyShaderContext(ctx);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   258
        return NULL;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   259
    }
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   260
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   261
    /* Compile all the shaders */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   262
    for (i = 0; i < NUM_SHADERS; ++i) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   263
        if (!CompileShaderProgram(ctx, i, &ctx->shaders[i])) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   264
fprintf(stderr, "Unable to compile shader!\n");
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   265
            GL_DestroyShaderContext(ctx);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   266
            return NULL;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   267
        }
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   268
    }
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   269
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   270
    /* We're done! */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   271
    return ctx;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   272
}
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   273
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   274
void
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   275
GL_SelectShader(GL_ShaderContext *ctx, GL_Shader shader)
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   276
{
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   277
    /* Nothing to do if there's no shader support */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   278
    if (!ctx) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   279
        return;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   280
    }
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   281
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   282
    /* Nothing to do if there's no shader change */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   283
    if (shader == ctx->current_shader) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   284
        return;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   285
    }
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   286
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   287
    ctx->glUseProgramObjectARB(ctx->shaders[shader].program);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   288
    ctx->current_shader = shader;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   289
}
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   290
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   291
void
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   292
GL_DestroyShaderContext(GL_ShaderContext *ctx)
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   293
{
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   294
    int i;
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   295
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   296
    for (i = 0; i < NUM_SHADERS; ++i) {
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   297
        DestroyShaderProgram(ctx, &ctx->shaders[i]);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   298
    }
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   299
    SDL_free(ctx);
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   300
}
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   301
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   302
#endif /* SDL_VIDEO_RENDER_OGL && !SDL_RENDER_DISABLED */
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   303
811beeb698f9 Beginning of a framework for OpenGL shaders
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   304
/* vi: set ts=4 sw=4 expandtab: */