1 /*
2 vid_common_glsl.c
3
4 Common OpenGLSL video driver functions
5
6 Copyright (C) 1996-1997 Id Software, Inc.
7 Copyright (C) 2011 Bill Currie <bill@taniwha.org>
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
18 See the GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to:
22
23 Free Software Foundation, Inc.
24 59 Temple Place - Suite 330
25 Boston, MA 02111-1307, USA
26
27 */
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #ifdef HAVE_MATH_H
33 # include <math.h>
34 #endif
35 #ifdef HAVE_STRING_H
36 # include <string.h>
37 #endif
38 #ifdef HAVE_STRINGS_H
39 # include <strings.h>
40 #endif
41
42 #include "QF/cvar.h"
43 #include "QF/dstring.h"
44 #include "QF/input.h"
45 #include "QF/qargs.h"
46 #include "QF/quakefs.h"
47 #include "QF/sys.h"
48 #include "QF/va.h"
49 #include "QF/vid.h"
50 #include "QF/GLSL/defines.h"
51 #include "QF/GLSL/funcs.h"
52 #include "QF/GLSL/qf_vid.h"
53 #include "QF/GLSL/qf_textures.h"
54
55 #include "compat.h"
56 #include "d_iface.h"
57 #include "r_internal.h"
58
59 int glsl_palette;
60 int glsl_colormap;
61
62 static void
GLSL_Common_Init_Cvars(void)63 GLSL_Common_Init_Cvars (void)
64 {
65 }
66
67 void
GLSL_SetPalette(const byte * palette)68 GLSL_SetPalette (const byte *palette)
69 {
70 const byte *col, *ip;
71 byte *pal, *op;
72 unsigned r, g, b, v;
73 unsigned short i;
74 unsigned *table;
75
76 // 8 8 8 encoding
77 Sys_MaskPrintf (SYS_VID, "Converting 8to24\n");
78
79 table = d_8to24table;
80 for (i = 0; i < 255; i++) { // used to be i<256, see d_8to24table below
81 r = palette[i * 3 + 0];
82 g = palette[i * 3 + 1];
83 b = palette[i * 3 + 2];
84 #ifdef WORDS_BIGENDIAN
85 v = (255 << 0) + (r << 24) + (g << 16) + (b << 8);
86 #else
87 v = (255 << 24) + (r << 0) + (g << 8) + (b << 16);
88 #endif
89 *table++ = v;
90 }
91 d_8to24table[255] = 0; // 255 is transparent
92
93 Sys_MaskPrintf (SYS_VID, "Converting palette/colormap to RGBA textures\n");
94 pal = malloc (256 * VID_GRADES * 4);
95 for (i = 0, col = vr_data.vid->colormap8, op = pal; i < 256 * VID_GRADES;
96 i++) {
97 ip = palette + *col++ * 3;
98 *op++ = *ip++;
99 *op++ = *ip++;
100 *op++ = *ip++;
101 *op++ = 255; // alpha = 1
102 }
103 for (i = 0; i < VID_GRADES; i++)
104 pal[i * 256 * 4 + 255 + 3] = 0;
105
106 if (!glsl_colormap) {
107 GLuint tex;
108 qfeglGenTextures (1, &tex);
109 glsl_colormap = tex;
110 }
111 qfeglBindTexture (GL_TEXTURE_2D, glsl_colormap);
112 qfeglTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 256, VID_GRADES, 0,
113 GL_RGBA, GL_UNSIGNED_BYTE, pal);
114 qfeglTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
115 qfeglTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
116 qfeglTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
117 qfeglTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
118
119 for (i = 0, ip = palette, op = pal; i < 255; i++) {
120 *op++ = *ip++;
121 *op++ = *ip++;
122 *op++ = *ip++;
123 *op++ = 255; // alpha = 1
124 }
125 QuatZero (op);
126
127 if (!glsl_palette) {
128 GLuint tex;
129 qfeglGenTextures (1, &tex);
130 glsl_palette = tex;
131 }
132 qfeglBindTexture (GL_TEXTURE_2D, glsl_palette);
133 qfeglTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 256, 1, 0,
134 GL_RGBA, GL_UNSIGNED_BYTE, pal);
135 qfeglTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
136 qfeglTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
137 qfeglTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
138 qfeglTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
139 free (pal);
140 }
141
142 void
GLSL_Init_Common(void)143 GLSL_Init_Common (void)
144 {
145 EGLF_FindFunctions ();
146
147 GLSL_Common_Init_Cvars ();
148
149 GLSL_TextureInit ();
150
151 if (developer->int_val & SYS_GLSL) {
152 GLint max;
153
154 qfeglGetIntegerv (GL_MAX_VERTEX_UNIFORM_VECTORS, &max);
155 Sys_Printf ("max vertex uniform vectors: %d\n", max);
156 qfeglGetIntegerv (GL_MAX_FRAGMENT_UNIFORM_VECTORS, &max);
157 Sys_Printf ("max fragment uniforms: %d\n", max);
158 qfeglGetIntegerv (GL_MAX_TEXTURE_IMAGE_UNITS, &max);
159 Sys_Printf ("max texture image units: %d\n", max);
160 qfeglGetIntegerv (GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &max);
161 Sys_Printf ("max vertex texture image units: %d\n", max);
162 qfeglGetIntegerv (GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max);
163 Sys_Printf ("max combined texture image units: %d\n", max);
164 qfeglGetIntegerv (GL_MAX_CUBE_MAP_TEXTURE_SIZE, &max);
165 Sys_Printf ("max cube map texture size: %d\n", max);
166 qfeglGetIntegerv (GL_MAX_RENDERBUFFER_SIZE, &max);
167 Sys_Printf ("max renderbuffer size: %d\n", max);
168 qfeglGetIntegerv (GL_MAX_VARYING_VECTORS, &max);
169 Sys_Printf ("max varying vectors: %d\n", max);
170 qfeglGetIntegerv (GL_MAX_VERTEX_ATTRIBS, &max);
171 Sys_Printf ("max vertex attribs: %d\n", max);
172 }
173
174 qfeglClearColor (0, 0, 0, 0);
175
176 qfeglPixelStorei (GL_UNPACK_ALIGNMENT, 1);
177
178 qfeglEnable (GL_TEXTURE_2D);
179 qfeglFrontFace (GL_CW);
180 qfeglCullFace (GL_BACK);
181
182 qfeglEnable (GL_BLEND);
183 qfeglBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
184 }
185
186 int
GLSL_CompileShader(const char * name,const char * shader_src,int type)187 GLSL_CompileShader (const char *name, const char *shader_src, int type)
188 {
189 const char *src[1];
190 int shader;
191 int compiled;
192
193 src[0] = shader_src;
194 shader = qfeglCreateShader (type);
195 qfeglShaderSource (shader, 1, src, 0);
196 qfeglCompileShader (shader);
197 qfeglGetShaderiv (shader, GL_COMPILE_STATUS, &compiled);
198 if (!compiled || (developer->int_val & SYS_GLSL)) {
199 dstring_t *log = dstring_new ();
200 int size;
201 qfeglGetShaderiv (shader, GL_INFO_LOG_LENGTH, &size);
202 log->size = size + 1; // for terminating null
203 dstring_adjust (log);
204 qfeglGetShaderInfoLog (shader, log->size, 0, log->str);
205 if (!compiled)
206 qfeglDeleteShader (shader);
207 Sys_Printf ("Shader (%s) compile log:\n----8<----\n%s----8<----\n",
208 name, log->str);
209 dstring_delete (log);
210 if (!compiled)
211 return 0;
212 }
213 return shader;
214 }
215
216 static const char *
type_name(GLenum type)217 type_name (GLenum type)
218 {
219 switch (type) {
220 case GL_FLOAT_VEC2:
221 return "vec2";
222 case GL_FLOAT_VEC3:
223 return "vec3";
224 case GL_FLOAT_VEC4:
225 return "vec4";
226 case GL_INT_VEC2:
227 return "ivec2";
228 case GL_INT_VEC3:
229 return "ivec3";
230 case GL_INT_VEC4:
231 return "ivec4";
232 case GL_BOOL:
233 return "bool";
234 case GL_BOOL_VEC2:
235 return "bvec2";
236 case GL_BOOL_VEC3:
237 return "bvec3";
238 case GL_BOOL_VEC4:
239 return "bvec4";
240 case GL_FLOAT_MAT2:
241 return "mat2";
242 case GL_FLOAT_MAT3:
243 return "mat3";
244 case GL_FLOAT_MAT4:
245 return "mat4";
246 case GL_SAMPLER_2D:
247 return "sampler_2d";
248 case GL_SAMPLER_CUBE:
249 return "sampler_cube";
250 case GL_BYTE:
251 return "byte";
252 case GL_UNSIGNED_BYTE:
253 return "unsigned byte";
254 case GL_SHORT:
255 return "short";
256 case GL_UNSIGNED_SHORT:
257 return "unsigned short";
258 case GL_INT:
259 return "int";
260 case GL_UNSIGNED_INT:
261 return "unsigned int";
262 case GL_FLOAT:
263 return "float";
264 case GL_FIXED:
265 return "fixed";
266 }
267 return va("%x", type);
268 }
269
270 static void
dump_program(const char * name,int program)271 dump_program (const char *name, int program)
272 {
273 GLint ind = 0;
274 GLint count = 0;
275 GLint size;
276 dstring_t *pname;
277 GLint psize = 0;
278 GLenum ptype = 0;
279
280 pname = dstring_new ();
281 qfeglGetProgramiv (program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &size);
282 pname->size = size;
283 dstring_adjust (pname);
284
285 qfeglGetProgramiv(program, GL_ACTIVE_UNIFORMS, &count);
286 Sys_Printf("Program %s (%d) has %i uniforms\n", name, program, count);
287 for (ind = 0; ind < count; ind++) {
288 qfeglGetActiveUniform(program, ind, pname->size, 0, &psize, &ptype,
289 pname->str);
290 Sys_Printf ("Uniform %i name \"%s\" size %i type %s\n", (int)ind,
291 pname->str, (int)psize, type_name (ptype));
292 }
293
294 qfeglGetProgramiv (program, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &size);
295 pname->size = size;
296 dstring_adjust (pname);
297
298 qfeglGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &count);
299 Sys_Printf("Program %s (%d) has %i attributes\n", name, program, count);
300 for (ind = 0; ind < count; ind++) {
301 qfeglGetActiveAttrib(program, ind, pname->size, 0, &psize, &ptype,
302 pname->str);
303 Sys_Printf ("Attribute %i name \"%s\" size %i type %s\n", (int)ind,
304 pname->str, (int)psize, type_name (ptype));
305 }
306 dstring_delete (pname);
307 }
308
309 int
GLSL_LinkProgram(const char * name,int vert,int frag)310 GLSL_LinkProgram (const char *name, int vert, int frag)
311 {
312 int program;
313 int linked;
314
315 program = qfeglCreateProgram ();
316 qfeglAttachShader (program, vert);
317 qfeglAttachShader (program, frag);
318 qfeglLinkProgram (program);
319
320 qfeglGetProgramiv (program, GL_LINK_STATUS, &linked);
321 if (!linked || (developer->int_val & SYS_GLSL)) {
322 dstring_t *log = dstring_new ();
323 int size;
324 qfeglGetProgramiv (program, GL_INFO_LOG_LENGTH, &size);
325 log->size = size + 1; // for terminating null
326 dstring_adjust (log);
327 qfeglGetProgramInfoLog (program, log->size, 0, log->str);
328 if (!linked)
329 qfeglDeleteProgram (program);
330 Sys_Printf ("Program (%s) link log:\n----8<----\n%s----8<----\n",
331 name, log->str);
332 dstring_delete (log);
333 if (!linked)
334 return 0;
335 }
336 if (developer->int_val & SYS_GLSL)
337 dump_program (name, program);
338 return program;
339 }
340
341 int
GLSL_ResolveShaderParam(int program,shaderparam_t * param)342 GLSL_ResolveShaderParam (int program, shaderparam_t *param)
343 {
344 if (param->uniform) {
345 param->location = qfeglGetUniformLocation (program, param->name);
346 } else {
347 param->location = qfeglGetAttribLocation (program, param->name);
348 }
349 if (param->location < 0) {
350 Sys_Printf ("could not resolve %s %s\n",
351 param->uniform ? "uniform" : "attribute", param->name);
352 } else {
353 Sys_MaskPrintf (SYS_GLSL, "Resolved %s %s @ %d\n",
354 param->uniform ? "uniform" : "attribute",
355 param->name, param->location);
356 }
357 return param->location;
358 }
359
360 void
GLSL_DumpAttribArrays(void)361 GLSL_DumpAttribArrays (void)
362 {
363 GLint max = 0;
364 GLint ind;
365 GLint enabled;
366 GLint size = -1;
367 GLint stride = -1;
368 GLint type = -1;
369 GLint norm = -1;
370 GLint binding = -1;
371 GLint current[4] = {-1, -1, -1, -1};
372 void *pointer = 0;
373
374 qfeglGetIntegerv (GL_MAX_VERTEX_ATTRIBS, &max);
375
376 for (ind = 0; ind < max; ind++) {
377 qfeglGetVertexAttribiv (ind, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &enabled);
378 Sys_Printf ("attrib %d: %sabled\n", ind, enabled ? "en" : "dis");
379 qfeglGetVertexAttribiv (ind, GL_VERTEX_ATTRIB_ARRAY_SIZE, &size);
380 qfeglGetVertexAttribiv (ind, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &stride);
381 qfeglGetVertexAttribiv (ind, GL_VERTEX_ATTRIB_ARRAY_TYPE, &type);
382 qfeglGetVertexAttribiv (ind, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &norm);
383 qfeglGetVertexAttribiv (ind, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING,
384 &binding);
385 qfeglGetVertexAttribiv (ind, GL_CURRENT_VERTEX_ATTRIB, current);
386 qfeglGetVertexAttribPointerv (ind, GL_VERTEX_ATTRIB_ARRAY_POINTER,
387 &pointer);
388 Sys_Printf (" %d %d '%s' %d %d (%d %d %d %d) %p\n", size, stride,
389 type_name (type), norm, binding, QuatExpand (current),
390 pointer);
391 }
392 }
393