annotate src/render/opengl/SDL_shaders_gl.c @ 5269:7ace5f8f432f

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