Adding FreeType2 and UTF-8 Support.
[divverent/darkplaces.git] / gl_rmain.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // r_main.c
21
22 #include "quakedef.h"
23 #include "cl_dyntexture.h"
24 #include "r_shadow.h"
25 #include "polygon.h"
26 #include "image.h"
27 #include "ft2.h"
28
29 mempool_t *r_main_mempool;
30 rtexturepool_t *r_main_texturepool;
31
32 static int r_frame = 0; ///< used only by R_GetCurrentTexture
33
34 qboolean r_loadnormalmap;
35 qboolean r_loadgloss;
36 qboolean r_loadfog;
37
38 //
39 // screen size info
40 //
41 r_refdef_t r_refdef;
42
43 cvar_t r_motionblur = {CVAR_SAVE, "r_motionblur", "0", "motionblur value scale - 0.5 recommended"};
44 cvar_t r_damageblur = {CVAR_SAVE, "r_damageblur", "0", "motionblur based on damage"};
45 cvar_t r_motionblur_vmin = {CVAR_SAVE, "r_motionblur_vmin", "300", "minimum influence from velocity"};
46 cvar_t r_motionblur_vmax = {CVAR_SAVE, "r_motionblur_vmax", "600", "maximum influence from velocity"};
47 cvar_t r_motionblur_bmin = {CVAR_SAVE, "r_motionblur_bmin", "0.5", "velocity at which there is no blur yet (may be negative to always have some blur)"};
48 cvar_t r_motionblur_vcoeff = {CVAR_SAVE, "r_motionblur_vcoeff", "0.05", "sliding average reaction time for velocity"};
49 cvar_t r_motionblur_maxblur = {CVAR_SAVE, "r_motionblur_maxblur", "0.88", "cap for motionblur alpha value"};
50 cvar_t r_motionblur_randomize = {CVAR_SAVE, "r_motionblur_randomize", "0.1", "randomizing coefficient to workaround ghosting"};
51
52 // TODO do we want a r_equalize_entities cvar that works on all ents, or would that be a cheat?
53 cvar_t r_equalize_entities_fullbright = {CVAR_SAVE, "r_equalize_entities_fullbright", "0", "render fullbright entities by equalizing their lightness, not by not rendering light"};
54 cvar_t r_equalize_entities_minambient = {CVAR_SAVE, "r_equalize_entities_minambient", "0.5", "light equalizing: ensure at least this ambient/diffuse ratio"};
55 cvar_t r_equalize_entities_by = {CVAR_SAVE, "r_equalize_entities_by", "0.7", "light equalizing: exponent of dynamics compression (0 = no compression, 1 = full compression)"};
56 cvar_t r_equalize_entities_to = {CVAR_SAVE, "r_equalize_entities_to", "0.8", "light equalizing: target light level"};
57
58 cvar_t r_depthfirst = {CVAR_SAVE, "r_depthfirst", "0", "renders a depth-only version of the scene before normal rendering begins to eliminate overdraw, values: 0 = off, 1 = world depth, 2 = world and model depth"};
59 cvar_t r_useinfinitefarclip = {CVAR_SAVE, "r_useinfinitefarclip", "1", "enables use of a special kind of projection matrix that has an extremely large farclip"};
60 cvar_t r_farclip_base = {0, "r_farclip_base", "65536", "farclip (furthest visible distance) for rendering when r_useinfinitefarclip is 0"};
61 cvar_t r_farclip_world = {0, "r_farclip_world", "2", "adds map size to farclip multiplied by this value"};
62 cvar_t r_nearclip = {0, "r_nearclip", "1", "distance from camera of nearclip plane" };
63 cvar_t r_showbboxes = {0, "r_showbboxes", "0", "shows bounding boxes of server entities, value controls opacity scaling (1 = 10%,  10 = 100%)"};
64 cvar_t r_showsurfaces = {0, "r_showsurfaces", "0", "1 shows surfaces as different colors, or a value of 2 shows triangle draw order (for analyzing whether meshes are optimized for vertex cache)"};
65 cvar_t r_showtris = {0, "r_showtris", "0", "shows triangle outlines, value controls brightness (can be above 1)"};
66 cvar_t r_shownormals = {0, "r_shownormals", "0", "shows per-vertex surface normals and tangent vectors for bumpmapped lighting"};
67 cvar_t r_showlighting = {0, "r_showlighting", "0", "shows areas lit by lights, useful for finding out why some areas of a map render slowly (bright orange = lots of passes = slow), a value of 2 disables depth testing which can be interesting but not very useful"};
68 cvar_t r_showshadowvolumes = {0, "r_showshadowvolumes", "0", "shows areas shadowed by lights, useful for finding out why some areas of a map render slowly (bright blue = lots of passes = slow), a value of 2 disables depth testing which can be interesting but not very useful"};
69 cvar_t r_showcollisionbrushes = {0, "r_showcollisionbrushes", "0", "draws collision brushes in quake3 maps (mode 1), mode 2 disables rendering of world (trippy!)"};
70 cvar_t r_showcollisionbrushes_polygonfactor = {0, "r_showcollisionbrushes_polygonfactor", "-1", "expands outward the brush polygons a little bit, used to make collision brushes appear infront of walls"};
71 cvar_t r_showcollisionbrushes_polygonoffset = {0, "r_showcollisionbrushes_polygonoffset", "0", "nudges brush polygon depth in hardware depth units, used to make collision brushes appear infront of walls"};
72 cvar_t r_showdisabledepthtest = {0, "r_showdisabledepthtest", "0", "disables depth testing on r_show* cvars, allowing you to see what hidden geometry the graphics card is processing"};
73 cvar_t r_drawportals = {0, "r_drawportals", "0", "shows portals (separating polygons) in world interior in quake1 maps"};
74 cvar_t r_drawentities = {0, "r_drawentities","1", "draw entities (doors, players, projectiles, etc)"};
75 cvar_t r_drawviewmodel = {0, "r_drawviewmodel","1", "draw your weapon model"};
76 cvar_t r_cullentities_trace = {0, "r_cullentities_trace", "1", "probabistically cull invisible entities"};
77 cvar_t r_cullentities_trace_samples = {0, "r_cullentities_trace_samples", "2", "number of samples to test for entity culling (in addition to center sample)"};
78 cvar_t r_cullentities_trace_tempentitysamples = {0, "r_cullentities_trace_tempentitysamples", "-1", "number of samples to test for entity culling of temp entities (including all CSQC entities), -1 disables trace culling on these entities to prevent flicker (pvs still applies)"};
79 cvar_t r_cullentities_trace_enlarge = {0, "r_cullentities_trace_enlarge", "0", "box enlargement for entity culling"};
80 cvar_t r_cullentities_trace_delay = {0, "r_cullentities_trace_delay", "1", "number of seconds until the entity gets actually culled"};
81 cvar_t r_speeds = {0, "r_speeds","0", "displays rendering statistics and per-subsystem timings"};
82 cvar_t r_fullbright = {0, "r_fullbright","0", "makes map very bright and renders faster"};
83 cvar_t r_wateralpha = {CVAR_SAVE, "r_wateralpha","1", "opacity of water polygons"};
84 cvar_t r_dynamic = {CVAR_SAVE, "r_dynamic","1", "enables dynamic lights (rocket glow and such)"};
85 cvar_t r_fullbrights = {CVAR_SAVE, "r_fullbrights", "1", "enables glowing pixels in quake textures (changes need r_restart to take effect)"};
86 cvar_t r_shadows = {CVAR_SAVE, "r_shadows", "0", "casts fake stencil shadows from models onto the world (rtlights are unaffected by this); when set to 2, always cast the shadows in the direction set by r_shadows_throwdirection, otherwise use the model lighting."};
87 cvar_t r_shadows_darken = {CVAR_SAVE, "r_shadows_darken", "0.5", "how much shadowed areas will be darkened"};
88 cvar_t r_shadows_throwdistance = {CVAR_SAVE, "r_shadows_throwdistance", "500", "how far to cast shadows from models"};
89 cvar_t r_shadows_throwdirection = {CVAR_SAVE, "r_shadows_throwdirection", "0 0 -1", "override throwing direction for r_shadows 2"};
90 cvar_t r_shadows_drawafterrtlighting = {CVAR_SAVE, "r_shadows_drawafterrtlighting", "0", "draw fake shadows AFTER realtime lightning is drawn. May be useful for simulating fast sunlight on large outdoor maps with only one noshadow rtlight. The price is less realistic appearance of dynamic light shadows."};
91 cvar_t r_shadows_castfrombmodels = {CVAR_SAVE, "r_shadows_castfrombmodels", "0", "do cast shadows from bmodels"};
92 cvar_t r_q1bsp_skymasking = {0, "r_q1bsp_skymasking", "1", "allows sky polygons in quake1 maps to obscure other geometry"};
93 cvar_t r_polygonoffset_submodel_factor = {0, "r_polygonoffset_submodel_factor", "0", "biases depth values of world submodels such as doors, to prevent z-fighting artifacts in Quake maps"};
94 cvar_t r_polygonoffset_submodel_offset = {0, "r_polygonoffset_submodel_offset", "14", "biases depth values of world submodels such as doors, to prevent z-fighting artifacts in Quake maps"};
95 cvar_t r_polygonoffset_decals_factor = {0, "r_polygonoffset_decals_factor", "0", "biases depth values of decals to prevent z-fighting artifacts"};
96 cvar_t r_polygonoffset_decals_offset = {0, "r_polygonoffset_decals_offset", "-14", "biases depth values of decals to prevent z-fighting artifacts"};
97 cvar_t r_fog_exp2 = {0, "r_fog_exp2", "0", "uses GL_EXP2 fog (as in Nehahra) rather than realistic GL_EXP fog"};
98 cvar_t r_drawfog = {CVAR_SAVE, "r_drawfog", "1", "allows one to disable fog rendering"};
99 cvar_t r_transparentdepthmasking = {CVAR_SAVE, "r_transparentdepthmasking", "0", "enables depth writes on transparent meshes whose materially is normally opaque, this prevents seeing the inside of a transparent mesh"};
100
101 cvar_t gl_fogenable = {0, "gl_fogenable", "0", "nehahra fog enable (for Nehahra compatibility only)"};
102 cvar_t gl_fogdensity = {0, "gl_fogdensity", "0.25", "nehahra fog density (recommend values below 0.1) (for Nehahra compatibility only)"};
103 cvar_t gl_fogred = {0, "gl_fogred","0.3", "nehahra fog color red value (for Nehahra compatibility only)"};
104 cvar_t gl_foggreen = {0, "gl_foggreen","0.3", "nehahra fog color green value (for Nehahra compatibility only)"};
105 cvar_t gl_fogblue = {0, "gl_fogblue","0.3", "nehahra fog color blue value (for Nehahra compatibility only)"};
106 cvar_t gl_fogstart = {0, "gl_fogstart", "0", "nehahra fog start distance (for Nehahra compatibility only)"};
107 cvar_t gl_fogend = {0, "gl_fogend","0", "nehahra fog end distance (for Nehahra compatibility only)"};
108 cvar_t gl_skyclip = {0, "gl_skyclip", "4608", "nehahra farclip distance - the real fog end (for Nehahra compatibility only)"};
109
110 cvar_t r_textureunits = {0, "r_textureunits", "32", "number of texture units to use in GL 1.1 and GL 1.3 rendering paths"};
111 static cvar_t gl_combine = {CVAR_READONLY, "gl_combine", "1", "indicates whether the OpenGL 1.3 rendering path is active"};
112 static cvar_t r_glsl = {CVAR_READONLY, "r_glsl", "1", "indicates whether the OpenGL 2.0 rendering path is active"};
113
114 cvar_t r_glsl_deluxemapping = {CVAR_SAVE, "r_glsl_deluxemapping", "1", "use per pixel lighting on deluxemap-compiled q3bsp maps (or a value of 2 forces deluxemap shading even without deluxemaps)"};
115 cvar_t r_glsl_offsetmapping = {CVAR_SAVE, "r_glsl_offsetmapping", "0", "offset mapping effect (also known as parallax mapping or virtual displacement mapping)"};
116 cvar_t r_glsl_offsetmapping_reliefmapping = {CVAR_SAVE, "r_glsl_offsetmapping_reliefmapping", "0", "relief mapping effect (higher quality)"};
117 cvar_t r_glsl_offsetmapping_scale = {CVAR_SAVE, "r_glsl_offsetmapping_scale", "0.04", "how deep the offset mapping effect is"};
118 cvar_t r_glsl_postprocess = {CVAR_SAVE, "r_glsl_postprocess", "0", "use a GLSL postprocessing shader"};
119 cvar_t r_glsl_postprocess_uservec1 = {CVAR_SAVE, "r_glsl_postprocess_uservec1", "0 0 0 0", "a 4-component vector to pass as uservec1 to the postprocessing shader (only useful if default.glsl has been customized)"};
120 cvar_t r_glsl_postprocess_uservec2 = {CVAR_SAVE, "r_glsl_postprocess_uservec2", "0 0 0 0", "a 4-component vector to pass as uservec2 to the postprocessing shader (only useful if default.glsl has been customized)"};
121 cvar_t r_glsl_postprocess_uservec3 = {CVAR_SAVE, "r_glsl_postprocess_uservec3", "0 0 0 0", "a 4-component vector to pass as uservec3 to the postprocessing shader (only useful if default.glsl has been customized)"};
122 cvar_t r_glsl_postprocess_uservec4 = {CVAR_SAVE, "r_glsl_postprocess_uservec4", "0 0 0 0", "a 4-component vector to pass as uservec4 to the postprocessing shader (only useful if default.glsl has been customized)"};
123
124 cvar_t r_water = {CVAR_SAVE, "r_water", "0", "whether to use reflections and refraction on water surfaces (note: r_wateralpha must be set below 1)"};
125 cvar_t r_water_clippingplanebias = {CVAR_SAVE, "r_water_clippingplanebias", "1", "a rather technical setting which avoids black pixels around water edges"};
126 cvar_t r_water_resolutionmultiplier = {CVAR_SAVE, "r_water_resolutionmultiplier", "0.5", "multiplier for screen resolution when rendering refracted/reflected scenes, 1 is full quality, lower values are faster"};
127 cvar_t r_water_refractdistort = {CVAR_SAVE, "r_water_refractdistort", "0.01", "how much water refractions shimmer"};
128 cvar_t r_water_reflectdistort = {CVAR_SAVE, "r_water_reflectdistort", "0.01", "how much water reflections shimmer"};
129
130 cvar_t r_lerpsprites = {CVAR_SAVE, "r_lerpsprites", "1", "enables animation smoothing on sprites"};
131 cvar_t r_lerpmodels = {CVAR_SAVE, "r_lerpmodels", "1", "enables animation smoothing on models"};
132 cvar_t r_lerplightstyles = {CVAR_SAVE, "r_lerplightstyles", "0", "enable animation smoothing on flickering lights"};
133 cvar_t r_waterscroll = {CVAR_SAVE, "r_waterscroll", "1", "makes water scroll around, value controls how much"};
134
135 cvar_t r_bloom = {CVAR_SAVE, "r_bloom", "0", "enables bloom effect (makes bright pixels affect neighboring pixels)"};
136 cvar_t r_bloom_colorscale = {CVAR_SAVE, "r_bloom_colorscale", "1", "how bright the glow is"};
137 cvar_t r_bloom_brighten = {CVAR_SAVE, "r_bloom_brighten", "2", "how bright the glow is, after subtract/power"};
138 cvar_t r_bloom_blur = {CVAR_SAVE, "r_bloom_blur", "4", "how large the glow is"};
139 cvar_t r_bloom_resolution = {CVAR_SAVE, "r_bloom_resolution", "320", "what resolution to perform the bloom effect at (independent of screen resolution)"};
140 cvar_t r_bloom_colorexponent = {CVAR_SAVE, "r_bloom_colorexponent", "1", "how exagerated the glow is"};
141 cvar_t r_bloom_colorsubtract = {CVAR_SAVE, "r_bloom_colorsubtract", "0.125", "reduces bloom colors by a certain amount"};
142
143 cvar_t r_hdr = {CVAR_SAVE, "r_hdr", "0", "enables High Dynamic Range bloom effect (higher quality version of r_bloom)"};
144 cvar_t r_hdr_scenebrightness = {CVAR_SAVE, "r_hdr_scenebrightness", "1", "global rendering brightness"};
145 cvar_t r_hdr_glowintensity = {CVAR_SAVE, "r_hdr_glowintensity", "1", "how bright light emitting textures should appear"};
146 cvar_t r_hdr_range = {CVAR_SAVE, "r_hdr_range", "4", "how much dynamic range to render bloom with (equivilant to multiplying r_bloom_brighten by this value and dividing r_bloom_colorscale by this value)"};
147
148 cvar_t r_smoothnormals_areaweighting = {0, "r_smoothnormals_areaweighting", "1", "uses significantly faster (and supposedly higher quality) area-weighted vertex normals and tangent vectors rather than summing normalized triangle normals and tangents"};
149
150 cvar_t developer_texturelogging = {0, "developer_texturelogging", "0", "produces a textures.log file containing names of skins and map textures the engine tried to load"};
151
152 cvar_t gl_lightmaps = {0, "gl_lightmaps", "0", "draws only lightmaps, no texture (for level designers)"};
153
154 cvar_t r_test = {0, "r_test", "0", "internal development use only, leave it alone (usually does nothing anyway)"};
155 cvar_t r_batchmode = {0, "r_batchmode", "1", "selects method of rendering multiple surfaces with one driver call (values are 0, 1, 2, etc...)"};
156 cvar_t r_track_sprites = {CVAR_SAVE, "r_track_sprites", "1", "track SPR_LABEL* sprites by putting them as indicator at the screen border to rotate to"};
157 cvar_t r_track_sprites_flags = {CVAR_SAVE, "r_track_sprites_flags", "1", "1: Rotate sprites accodringly, 2: Make it a continuous rotation"};
158 cvar_t r_track_sprites_scalew = {CVAR_SAVE, "r_track_sprites_scalew", "1", "width scaling of tracked sprites"};
159 cvar_t r_track_sprites_scaleh = {CVAR_SAVE, "r_track_sprites_scaleh", "1", "height scaling of tracked sprites"};
160 cvar_t r_glsl_saturation = {CVAR_SAVE, "r_glsl_saturation", "1", "saturation multiplier (only working in glsl!)"};
161
162 cvar_t r_framedatasize = {CVAR_SAVE, "r_framedatasize", "1", "size of renderer data cache used during one frame (for skeletal animation caching, light processing, etc)"};
163
164 extern cvar_t v_glslgamma;
165
166 extern qboolean v_flipped_state;
167
168 static struct r_bloomstate_s
169 {
170         qboolean enabled;
171         qboolean hdr;
172
173         int bloomwidth, bloomheight;
174
175         int screentexturewidth, screentextureheight;
176         rtexture_t *texture_screen; /// \note also used for motion blur if enabled!
177
178         int bloomtexturewidth, bloomtextureheight;
179         rtexture_t *texture_bloom;
180
181         // arrays for rendering the screen passes
182         float screentexcoord2f[8];
183         float bloomtexcoord2f[8];
184         float offsettexcoord2f[8];
185
186         r_viewport_t viewport;
187 }
188 r_bloomstate;
189
190 r_waterstate_t r_waterstate;
191
192 /// shadow volume bsp struct with automatically growing nodes buffer
193 svbsp_t r_svbsp;
194
195 rtexture_t *r_texture_blanknormalmap;
196 rtexture_t *r_texture_white;
197 rtexture_t *r_texture_grey128;
198 rtexture_t *r_texture_black;
199 rtexture_t *r_texture_notexture;
200 rtexture_t *r_texture_whitecube;
201 rtexture_t *r_texture_normalizationcube;
202 rtexture_t *r_texture_fogattenuation;
203 rtexture_t *r_texture_gammaramps;
204 unsigned int r_texture_gammaramps_serial;
205 //rtexture_t *r_texture_fogintensity;
206
207 unsigned int r_queries[MAX_OCCLUSION_QUERIES];
208 unsigned int r_numqueries;
209 unsigned int r_maxqueries;
210
211 typedef struct r_qwskincache_s
212 {
213         char name[MAX_QPATH];
214         skinframe_t *skinframe;
215 }
216 r_qwskincache_t;
217
218 static r_qwskincache_t *r_qwskincache;
219 static int r_qwskincache_size;
220
221 /// vertex coordinates for a quad that covers the screen exactly
222 const float r_screenvertex3f[12] =
223 {
224         0, 0, 0,
225         1, 0, 0,
226         1, 1, 0,
227         0, 1, 0
228 };
229
230 extern void R_DrawModelShadows(void);
231
232 void R_ModulateColors(float *in, float *out, int verts, float r, float g, float b)
233 {
234         int i;
235         for (i = 0;i < verts;i++)
236         {
237                 out[0] = in[0] * r;
238                 out[1] = in[1] * g;
239                 out[2] = in[2] * b;
240                 out[3] = in[3];
241                 in += 4;
242                 out += 4;
243         }
244 }
245
246 void R_FillColors(float *out, int verts, float r, float g, float b, float a)
247 {
248         int i;
249         for (i = 0;i < verts;i++)
250         {
251                 out[0] = r;
252                 out[1] = g;
253                 out[2] = b;
254                 out[3] = a;
255                 out += 4;
256         }
257 }
258
259 // FIXME: move this to client?
260 void FOG_clear(void)
261 {
262         if (gamemode == GAME_NEHAHRA)
263         {
264                 Cvar_Set("gl_fogenable", "0");
265                 Cvar_Set("gl_fogdensity", "0.2");
266                 Cvar_Set("gl_fogred", "0.3");
267                 Cvar_Set("gl_foggreen", "0.3");
268                 Cvar_Set("gl_fogblue", "0.3");
269         }
270         r_refdef.fog_density = 0;
271         r_refdef.fog_red = 0;
272         r_refdef.fog_green = 0;
273         r_refdef.fog_blue = 0;
274         r_refdef.fog_alpha = 1;
275         r_refdef.fog_start = 0;
276         r_refdef.fog_end = 16384;
277         r_refdef.fog_height = 1<<30;
278         r_refdef.fog_fadedepth = 128;
279 }
280
281 static void R_BuildBlankTextures(void)
282 {
283         unsigned char data[4];
284         data[2] = 128; // normal X
285         data[1] = 128; // normal Y
286         data[0] = 255; // normal Z
287         data[3] = 128; // height
288         r_texture_blanknormalmap = R_LoadTexture2D(r_main_texturepool, "blankbump", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, NULL);
289         data[0] = 255;
290         data[1] = 255;
291         data[2] = 255;
292         data[3] = 255;
293         r_texture_white = R_LoadTexture2D(r_main_texturepool, "blankwhite", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, NULL);
294         data[0] = 128;
295         data[1] = 128;
296         data[2] = 128;
297         data[3] = 255;
298         r_texture_grey128 = R_LoadTexture2D(r_main_texturepool, "blankgrey128", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, NULL);
299         data[0] = 0;
300         data[1] = 0;
301         data[2] = 0;
302         data[3] = 255;
303         r_texture_black = R_LoadTexture2D(r_main_texturepool, "blankblack", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, NULL);
304 }
305
306 static void R_BuildNoTexture(void)
307 {
308         int x, y;
309         unsigned char pix[16][16][4];
310         // this makes a light grey/dark grey checkerboard texture
311         for (y = 0;y < 16;y++)
312         {
313                 for (x = 0;x < 16;x++)
314                 {
315                         if ((y < 8) ^ (x < 8))
316                         {
317                                 pix[y][x][0] = 128;
318                                 pix[y][x][1] = 128;
319                                 pix[y][x][2] = 128;
320                                 pix[y][x][3] = 255;
321                         }
322                         else
323                         {
324                                 pix[y][x][0] = 64;
325                                 pix[y][x][1] = 64;
326                                 pix[y][x][2] = 64;
327                                 pix[y][x][3] = 255;
328                         }
329                 }
330         }
331         r_texture_notexture = R_LoadTexture2D(r_main_texturepool, "notexture", 16, 16, &pix[0][0][0], TEXTYPE_BGRA, TEXF_MIPMAP | TEXF_PERSISTENT, NULL);
332 }
333
334 static void R_BuildWhiteCube(void)
335 {
336         unsigned char data[6*1*1*4];
337         memset(data, 255, sizeof(data));
338         r_texture_whitecube = R_LoadTextureCubeMap(r_main_texturepool, "whitecube", 1, data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_PERSISTENT, NULL);
339 }
340
341 static void R_BuildNormalizationCube(void)
342 {
343         int x, y, side;
344         vec3_t v;
345         vec_t s, t, intensity;
346 #define NORMSIZE 64
347         unsigned char *data;
348         data = Mem_Alloc(tempmempool, 6*NORMSIZE*NORMSIZE*4);
349         for (side = 0;side < 6;side++)
350         {
351                 for (y = 0;y < NORMSIZE;y++)
352                 {
353                         for (x = 0;x < NORMSIZE;x++)
354                         {
355                                 s = (x + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
356                                 t = (y + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
357                                 switch(side)
358                                 {
359                                 default:
360                                 case 0:
361                                         v[0] = 1;
362                                         v[1] = -t;
363                                         v[2] = -s;
364                                         break;
365                                 case 1:
366                                         v[0] = -1;
367                                         v[1] = -t;
368                                         v[2] = s;
369                                         break;
370                                 case 2:
371                                         v[0] = s;
372                                         v[1] = 1;
373                                         v[2] = t;
374                                         break;
375                                 case 3:
376                                         v[0] = s;
377                                         v[1] = -1;
378                                         v[2] = -t;
379                                         break;
380                                 case 4:
381                                         v[0] = s;
382                                         v[1] = -t;
383                                         v[2] = 1;
384                                         break;
385                                 case 5:
386                                         v[0] = -s;
387                                         v[1] = -t;
388                                         v[2] = -1;
389                                         break;
390                                 }
391                                 intensity = 127.0f / sqrt(DotProduct(v, v));
392                                 data[((side*64+y)*64+x)*4+2] = (unsigned char)(128.0f + intensity * v[0]);
393                                 data[((side*64+y)*64+x)*4+1] = (unsigned char)(128.0f + intensity * v[1]);
394                                 data[((side*64+y)*64+x)*4+0] = (unsigned char)(128.0f + intensity * v[2]);
395                                 data[((side*64+y)*64+x)*4+3] = 255;
396                         }
397                 }
398         }
399         r_texture_normalizationcube = R_LoadTextureCubeMap(r_main_texturepool, "normalcube", NORMSIZE, data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_PERSISTENT, NULL);
400         Mem_Free(data);
401 }
402
403 static void R_BuildFogTexture(void)
404 {
405         int x, b;
406 #define FOGWIDTH 256
407         unsigned char data1[FOGWIDTH][4];
408         //unsigned char data2[FOGWIDTH][4];
409         double d, r, alpha;
410
411         r_refdef.fogmasktable_start = r_refdef.fog_start;
412         r_refdef.fogmasktable_alpha = r_refdef.fog_alpha;
413         r_refdef.fogmasktable_range = r_refdef.fogrange;
414         r_refdef.fogmasktable_density = r_refdef.fog_density;
415
416         r = r_refdef.fogmasktable_range / FOGMASKTABLEWIDTH;
417         for (x = 0;x < FOGMASKTABLEWIDTH;x++)
418         {
419                 d = (x * r - r_refdef.fogmasktable_start);
420                 if(developer.integer >= 100)
421                         Con_Printf("%f ", d);
422                 d = max(0, d);
423                 if (r_fog_exp2.integer)
424                         alpha = exp(-r_refdef.fogmasktable_density * r_refdef.fogmasktable_density * 0.0001 * d * d);
425                 else
426                         alpha = exp(-r_refdef.fogmasktable_density * 0.004 * d);
427                 if(developer.integer >= 100)
428                         Con_Printf(" : %f ", alpha);
429                 alpha = 1 - (1 - alpha) * r_refdef.fogmasktable_alpha;
430                 if(developer.integer >= 100)
431                         Con_Printf(" = %f\n", alpha);
432                 r_refdef.fogmasktable[x] = bound(0, alpha, 1);
433         }
434
435         for (x = 0;x < FOGWIDTH;x++)
436         {
437                 b = (int)(r_refdef.fogmasktable[x * (FOGMASKTABLEWIDTH - 1) / (FOGWIDTH - 1)] * 255);
438                 data1[x][0] = b;
439                 data1[x][1] = b;
440                 data1[x][2] = b;
441                 data1[x][3] = 255;
442                 //data2[x][0] = 255 - b;
443                 //data2[x][1] = 255 - b;
444                 //data2[x][2] = 255 - b;
445                 //data2[x][3] = 255;
446         }
447         if (r_texture_fogattenuation)
448         {
449                 R_UpdateTexture(r_texture_fogattenuation, &data1[0][0], 0, 0, FOGWIDTH, 1);
450                 //R_UpdateTexture(r_texture_fogattenuation, &data2[0][0], 0, 0, FOGWIDTH, 1);
451         }
452         else
453         {
454                 r_texture_fogattenuation = R_LoadTexture2D(r_main_texturepool, "fogattenuation", FOGWIDTH, 1, &data1[0][0], TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_PERSISTENT | TEXF_ALLOWUPDATES, NULL);
455                 //r_texture_fogintensity = R_LoadTexture2D(r_main_texturepool, "fogintensity", FOGWIDTH, 1, &data2[0][0], TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_ALLOWUPDATES, NULL);
456         }
457 }
458
459 static const char *builtinshaderstring =
460 "// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader\n"
461 "// written by Forest 'LordHavoc' Hale\n"
462 "\n"
463 "// enable various extensions depending on permutation:\n"
464 "\n"
465 "#ifdef VERTEX_SHADER\n"
466 "uniform mat4 ModelViewProjectionMatrix;\n"
467 "#endif\n"
468 "\n"
469 "#ifdef MODE_DEPTH_OR_SHADOW\n"
470 "#ifdef VERTEX_SHADER\n"
471 "void main(void)\n"
472 "{\n"
473 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
474 "}\n"
475 "#endif\n"
476 "#else // !MODE_DEPTH_ORSHADOW\n"
477 "#ifdef MODE_SHOWDEPTH\n"
478 "#ifdef VERTEX_SHADER\n"
479 "void main(void)\n"
480 "{\n"
481 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
482 "       gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0);\n"
483 "}\n"
484 "#endif\n"
485 "\n"
486 "#ifdef FRAGMENT_SHADER\n"
487 "void main(void)\n"
488 "{\n"
489 "       gl_FragColor = gl_Color;\n"
490 "}\n"
491 "#endif\n"
492 "#else // !MODE_SHOWDEPTH\n"
493 "#ifdef MODE_POSTPROCESS\n"
494 "varying vec2 TexCoord1;\n"
495 "varying vec2 TexCoord2;\n"
496 "\n"
497 "#ifdef VERTEX_SHADER\n"
498 "void main(void)\n"
499 "{\n"
500 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
501 "       TexCoord1 = gl_MultiTexCoord0.xy;\n"
502 "#ifdef USEBLOOM\n"
503 "       TexCoord2 = gl_MultiTexCoord1.xy;\n"
504 "#endif\n"
505 "}\n"
506 "#endif\n"
507 "\n"
508 "#ifdef FRAGMENT_SHADER\n"
509 "uniform sampler2D Texture_First;\n"
510 "#ifdef USEBLOOM\n"
511 "uniform sampler2D Texture_Second;\n"
512 "#endif\n"
513 "#ifdef USEGAMMARAMPS\n"
514 "uniform sampler2D Texture_GammaRamps;\n"
515 "#endif\n"
516 "#ifdef USESATURATION\n"
517 "uniform float Saturation;\n"
518 "#endif\n"
519 "#ifdef USEVIEWTINT\n"
520 "uniform vec4 ViewTintColor;\n"
521 "#endif\n"
522 "//uncomment these if you want to use them:\n"
523 "uniform vec4 UserVec1;\n"
524 "// uniform vec4 UserVec2;\n"
525 "// uniform vec4 UserVec3;\n"
526 "// uniform vec4 UserVec4;\n"
527 "// uniform float ClientTime;\n"
528 "uniform vec2 PixelSize;\n"
529 "void main(void)\n"
530 "{\n"
531 "       gl_FragColor = texture2D(Texture_First, TexCoord1);\n"
532 "#ifdef USEBLOOM\n"
533 "       gl_FragColor += texture2D(Texture_Second, TexCoord2);\n"
534 "#endif\n"
535 "#ifdef USEVIEWTINT\n"
536 "       gl_FragColor = mix(gl_FragColor, ViewTintColor, ViewTintColor.a);\n"
537 "#endif\n"
538 "\n"
539 "#ifdef USEPOSTPROCESSING\n"
540 "// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want\n"
541 "// this code does a blur with the radius specified in the first component of r_glsl_postprocess_uservec1 and blends it using the second component\n"
542 "       gl_FragColor += texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y;\n"
543 "       gl_FragColor += texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y;\n"
544 "       gl_FragColor += texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y;\n"
545 "       gl_FragColor += texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107,  0.707107)) * UserVec1.y;\n"
546 "       gl_FragColor += texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990,  0.891007)) * UserVec1.y;\n"
547 "       gl_FragColor /= (1 + 5 * UserVec1.y);\n"
548 "#endif\n"
549 "\n"
550 "#ifdef USESATURATION\n"
551 "       //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter\n"
552 "       float y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114));\n"
553 "       //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation;\n"
554 "       gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation);\n"
555 "#endif\n"
556 "\n"
557 "#ifdef USEGAMMARAMPS\n"
558 "       gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r;\n"
559 "       gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g;\n"
560 "       gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b;\n"
561 "#endif\n"
562 "}\n"
563 "#endif\n"
564 "#else // !MODE_POSTPROCESS\n"
565 "#ifdef MODE_GENERIC\n"
566 "#ifdef USEDIFFUSE\n"
567 "varying vec2 TexCoord1;\n"
568 "#endif\n"
569 "#ifdef USESPECULAR\n"
570 "varying vec2 TexCoord2;\n"
571 "#endif\n"
572 "#ifdef VERTEX_SHADER\n"
573 "void main(void)\n"
574 "{\n"
575 "       gl_FrontColor = gl_Color;\n"
576 "#ifdef USEDIFFUSE\n"
577 "       TexCoord1 = gl_MultiTexCoord0.xy;\n"
578 "#endif\n"
579 "#ifdef USESPECULAR\n"
580 "       TexCoord2 = gl_MultiTexCoord1.xy;\n"
581 "#endif\n"
582 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
583 "}\n"
584 "#endif\n"
585 "\n"
586 "#ifdef FRAGMENT_SHADER\n"
587 "#ifdef USEDIFFUSE\n"
588 "uniform sampler2D Texture_First;\n"
589 "#endif\n"
590 "#ifdef USESPECULAR\n"
591 "uniform sampler2D Texture_Second;\n"
592 "#endif\n"
593 "\n"
594 "void main(void)\n"
595 "{\n"
596 "       gl_FragColor = gl_Color;\n"
597 "#ifdef USEDIFFUSE\n"
598 "       gl_FragColor *= texture2D(Texture_First, TexCoord1);\n"
599 "#endif\n"
600 "\n"
601 "#ifdef USESPECULAR\n"
602 "       vec4 tex2 = texture2D(Texture_Second, TexCoord2);\n"
603 "# ifdef USECOLORMAPPING\n"
604 "       gl_FragColor *= tex2;\n"
605 "# endif\n"
606 "# ifdef USEGLOW\n"
607 "       gl_FragColor += tex2;\n"
608 "# endif\n"
609 "# ifdef USEVERTEXTEXTUREBLEND\n"
610 "       gl_FragColor = mix(gl_FragColor, tex2, tex2.a);\n"
611 "# endif\n"
612 "#endif\n"
613 "}\n"
614 "#endif\n"
615 "#else // !MODE_GENERIC\n"
616 "#ifdef MODE_BLOOMBLUR\n"
617 "varying TexCoord;\n"
618 "#ifdef VERTEX_SHADER\n"
619 "void main(void)\n"
620 "{\n"
621 "       gl_FrontColor = gl_Color;\n"
622 "       TexCoord = gl_MultiTexCoord0.xy;\n"
623 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
624 "}\n"
625 "#endif\n"
626 "\n"
627 "#ifdef FRAGMENT_SHADER\n"
628 "uniform sampler2D Texture_First;\n"
629 "uniform vec4 BloomBlur_Parameters;\n"
630 "\n"
631 "void main(void)\n"
632 "{\n"
633 "       int i;\n"
634 "       vec2 tc = TexCoord;\n"
635 "       vec3 color = texture2D(Texture_First, tc).rgb;\n"
636 "       tc += BloomBlur_Parameters.xy;\n"
637 "       for (i = 1;i < SAMPLES;i++)\n"
638 "       {\n"
639 "               color += texture2D(Texture_First, tc).rgb;\n"
640 "               tc += BloomBlur_Parameters.xy;\n"
641 "       }\n"
642 "       gl_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1);\n"
643 "}\n"
644 "#endif\n"
645 "#else // !MODE_BLOOMBLUR\n"
646 "#ifdef MODE_REFRACTION\n"
647 "varying vec2 TexCoord;\n"
648 "varying vec4 ModelViewProjectionPosition;\n"
649 "uniform mat4 TexMatrix;\n"
650 "#ifdef VERTEX_SHADER\n"
651 "\n"
652 "void main(void)\n"
653 "{\n"
654 "       TexCoord = vec2(TexMatrix * gl_MultiTexCoord0);\n"
655 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
656 "       ModelViewProjectionPosition = gl_Position;\n"
657 "}\n"
658 "#endif\n"
659 "\n"
660 "#ifdef FRAGMENT_SHADER\n"
661 "uniform sampler2D Texture_Normal;\n"
662 "uniform sampler2D Texture_Refraction;\n"
663 "uniform sampler2D Texture_Reflection;\n"
664 "\n"
665 "uniform vec4 DistortScaleRefractReflect;\n"
666 "uniform vec4 ScreenScaleRefractReflect;\n"
667 "uniform vec4 ScreenCenterRefractReflect;\n"
668 "uniform vec4 RefractColor;\n"
669 "uniform vec4 ReflectColor;\n"
670 "uniform float ReflectFactor;\n"
671 "uniform float ReflectOffset;\n"
672 "\n"
673 "void main(void)\n"
674 "{\n"
675 "       vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w);\n"
676 "       //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
677 "       vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
678 "       vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xy * DistortScaleRefractReflect.xy;\n"
679 "       // FIXME temporary hack to detect the case that the reflection\n"
680 "       // gets blackened at edges due to leaving the area that contains actual\n"
681 "       // content.\n"
682 "       // Remove this 'ack once we have a better way to stop this thing from\n"
683 "       // 'appening.\n"
684 "       float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05);\n"
685 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05);\n"
686 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05);\n"
687 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05);\n"
688 "       ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);\n"
689 "       gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor;\n"
690 "}\n"
691 "#endif\n"
692 "#else // !MODE_REFRACTION\n"
693 "#ifdef MODE_WATER\n"
694 "varying vec2 TexCoord;\n"
695 "varying vec3 EyeVector;\n"
696 "varying vec4 ModelViewProjectionPosition;\n"
697 "#ifdef VERTEX_SHADER\n"
698 "uniform vec3 EyePosition;\n"
699 "uniform mat4 TexMatrix;\n"
700 "\n"
701 "void main(void)\n"
702 "{\n"
703 "       TexCoord = vec2(TexMatrix * gl_MultiTexCoord0);\n"
704 "       vec3 EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
705 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
706 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
707 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
708 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
709 "       ModelViewProjectionPosition = gl_Position;\n"
710 "}\n"
711 "#endif\n"
712 "\n"
713 "#ifdef FRAGMENT_SHADER\n"
714 "uniform sampler2D Texture_Normal;\n"
715 "uniform sampler2D Texture_Refraction;\n"
716 "uniform sampler2D Texture_Reflection;\n"
717 "\n"
718 "uniform vec4 DistortScaleRefractReflect;\n"
719 "uniform vec4 ScreenScaleRefractReflect;\n"
720 "uniform vec4 ScreenCenterRefractReflect;\n"
721 "uniform vec4 RefractColor;\n"
722 "uniform vec4 ReflectColor;\n"
723 "uniform float ReflectFactor;\n"
724 "uniform float ReflectOffset;\n"
725 "\n"
726 "void main(void)\n"
727 "{\n"
728 "       vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
729 "       //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
730 "       vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
731 "       vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * DistortScaleRefractReflect;\n"
732 "       // FIXME temporary hack to detect the case that the reflection\n"
733 "       // gets blackened at edges due to leaving the area that contains actual\n"
734 "       // content.\n"
735 "       // Remove this 'ack once we have a better way to stop this thing from\n"
736 "       // 'appening.\n"
737 "       float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05);\n"
738 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05);\n"
739 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05);\n"
740 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05);\n"
741 "       ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f);\n"
742 "       f       = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05);\n"
743 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05);\n"
744 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05);\n"
745 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05);\n"
746 "       ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f);\n"
747 "       float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset;\n"
748 "       gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel);\n"
749 "}\n"
750 "#endif\n"
751 "#else // !MODE_WATER\n"
752 "\n"
753 "#if defined(USESHADOWMAPRECT) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USEDEFERREDLIGHTMAP)\n"
754 "# extension GL_ARB_texture_rectangle : enable\n"
755 "#endif\n"
756 "\n"
757 "#ifdef USESHADOWMAP2D\n"
758 "# ifdef GL_EXT_gpu_shader4\n"
759 "#   extension GL_EXT_gpu_shader4 : enable\n"
760 "# endif\n"
761 "# ifdef GL_ARB_texture_gather\n"
762 "#   extension GL_ARB_texture_gather : enable\n"
763 "# else\n"
764 "#   ifdef GL_AMD_texture_texture4\n"
765 "#     extension GL_AMD_texture_texture4 : enable\n"
766 "#   endif\n"
767 "# endif\n"
768 "#endif\n"
769 "\n"
770 "#ifdef USESHADOWMAPCUBE\n"
771 "# extension GL_EXT_gpu_shader4 : enable\n"
772 "#endif\n"
773 "\n"
774 "#ifdef USESHADOWSAMPLER\n"
775 "# extension GL_ARB_shadow : enable\n"
776 "#endif\n"
777 "\n"
778 "// common definitions between vertex shader and fragment shader:\n"
779 "\n"
780 "//#ifdef __GLSL_CG_DATA_TYPES\n"
781 "//# define myhalf half\n"
782 "//# define myhalf2 half2\n"
783 "//# define myhalf3half3\n"
784 "//# define myhalf4 half4\n"
785 "//#else\n"
786 "# define myhalf float\n"
787 "# define myhalf2 vec2\n"
788 "# define myhalf3 vec3\n"
789 "# define myhalf4 vec4\n"
790 "//#endif\n"
791 "\n"
792 "#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE)\n"
793 "# define USEFOG\n"
794 "#endif\n"
795 "\n"
796 "varying vec2 TexCoord;\n"
797 "#ifdef USEVERTEXTEXTUREBLEND\n"
798 "varying vec2 TexCoord2;\n"
799 "#endif\n"
800 "#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)\n"
801 "#define USELIGHTMAP\n"
802 "varying vec2 TexCoordLightmap;\n"
803 "#endif\n"
804 "\n"
805 "#ifdef MODE_LIGHTSOURCE\n"
806 "varying vec3 CubeVector;\n"
807 "#endif\n"
808 "\n"
809 "#ifdef MODE_LIGHTSOURCE\n"
810 "varying vec3 LightVector;\n"
811 "#endif\n"
812 "#if defined(MODE_LIGHTDIRECTION)\n"
813 "varying vec3 LightVector;\n"
814 "#endif\n"
815 "\n"
816 "#if defined(USEOFFSETMAPPING) || defined(USESPECULAR)\n"
817 "//#if defined(USEOFFSETMAPPING) || defined(USESPECULAR) || defined(MODE_LIGHTDIRECTION) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)\n"
818 "#define USEEYEVECTOR\n"
819 "varying vec3 EyeVector;\n"
820 "#endif\n"
821 "#ifdef USEFOG\n"
822 "varying vec3 EyeVectorModelSpace;\n"
823 "varying float FogPlaneVertexDist;\n"
824 "#endif\n"
825 "\n"
826 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY)\n"
827 "varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent)\n"
828 "varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal)\n"
829 "varying vec3 VectorR; // direction of R texcoord (surface normal)\n"
830 "#endif\n"
831 "\n"
832 "#ifdef USEREFLECTION\n"
833 "varying vec4 ModelViewProjectionPosition;\n"
834 "#endif\n"
835 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
836 "uniform vec3 LightPosition;\n"
837 "varying vec4 ModelViewPosition;\n"
838 "#endif\n"
839 "\n"
840 "#ifdef MODE_LIGHTSOURCE\n"
841 "uniform vec3 LightPosition;\n"
842 "#endif\n"
843 "uniform vec3 EyePosition;\n"
844 "#ifdef MODE_LIGHTDIRECTION\n"
845 "uniform vec3 LightDir;\n"
846 "#endif\n"
847 "uniform vec4 FogPlane;\n"
848 "\n"
849 "\n"
850 "\n"
851 "\n"
852 "\n"
853 "// vertex shader specific:\n"
854 "#ifdef VERTEX_SHADER\n"
855 "\n"
856 "// TODO: get rid of tangentt (texcoord2) and use a crossproduct to regenerate it from tangents (texcoord1) and normal (texcoord3), this would require sending a 4 component texcoord1 with W as 1 or -1 according to which side the texcoord2 should be on\n"
857 "\n"
858 "#ifdef MODE_DEFERREDGEOMETRY\n"
859 "uniform mat4 TexMatrix;\n"
860 "#ifdef USEVERTEXTEXTUREBLEND\n"
861 "uniform mat4 BackgroundTexMatrix;\n"
862 "#endif\n"
863 "uniform mat4 ModelViewMatrix;\n"
864 "void main(void)\n"
865 "{\n"
866 "       TexCoord = vec2(TexMatrix * gl_MultiTexCoord0);\n"
867 "#ifdef USEVERTEXTEXTUREBLEND\n"
868 "       gl_FrontColor = gl_Color;\n"
869 "       TexCoord2 = vec2(BackgroundTexMatrix * gl_MultiTexCoord0);\n"
870 "#endif\n"
871 "\n"
872 "       // transform unnormalized eye direction into tangent space\n"
873 "#ifdef USEOFFSETMAPPING\n"
874 "       vec3 EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
875 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
876 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
877 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
878 "#endif\n"
879 "\n"
880 "       VectorS = (ModelViewMatrix * vec4(gl_MultiTexCoord1.xyz, 0)).xyz;\n"
881 "       VectorT = (ModelViewMatrix * vec4(gl_MultiTexCoord2.xyz, 0)).xyz;\n"
882 "       VectorR = (ModelViewMatrix * vec4(gl_MultiTexCoord3.xyz, 0)).xyz;\n"
883 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
884 "}\n"
885 "#else // !MODE_DEFERREDGEOMETRY\n"
886 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
887 "uniform mat4 ModelViewMatrix;\n"
888 "void main(void)\n"
889 "{\n"
890 "       ModelViewPosition = ModelViewMatrix * gl_Vertex;\n"
891 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
892 "}\n"
893 "#else // !MODE_DEFERREDLIGHTSOURCE\n"
894 "uniform mat4 TexMatrix;\n"
895 "#ifdef USEVERTEXTEXTUREBLEND\n"
896 "uniform mat4 BackgroundTexMatrix;\n"
897 "#endif\n"
898 "#ifdef MODE_LIGHTSOURCE\n"
899 "uniform mat4 ModelToLight;\n"
900 "#endif\n"
901 "void main(void)\n"
902 "{\n"
903 "#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND)\n"
904 "       gl_FrontColor = gl_Color;\n"
905 "#endif\n"
906 "       // copy the surface texcoord\n"
907 "       TexCoord = vec2(TexMatrix * gl_MultiTexCoord0);\n"
908 "#ifdef USEVERTEXTEXTUREBLEND\n"
909 "       TexCoord2 = vec2(BackgroundTexMatrix * gl_MultiTexCoord0);\n"
910 "#endif\n"
911 "#ifdef USELIGHTMAP\n"
912 "       TexCoordLightmap = vec2(gl_MultiTexCoord4);\n"
913 "#endif\n"
914 "\n"
915 "#ifdef MODE_LIGHTSOURCE\n"
916 "       // transform vertex position into light attenuation/cubemap space\n"
917 "       // (-1 to +1 across the light box)\n"
918 "       CubeVector = vec3(ModelToLight * gl_Vertex);\n"
919 "\n"
920 "# ifdef USEDIFFUSE\n"
921 "       // transform unnormalized light direction into tangent space\n"
922 "       // (we use unnormalized to ensure that it interpolates correctly and then\n"
923 "       //  normalize it per pixel)\n"
924 "       vec3 lightminusvertex = LightPosition - gl_Vertex.xyz;\n"
925 "       LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz);\n"
926 "       LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz);\n"
927 "       LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz);\n"
928 "# endif\n"
929 "#endif\n"
930 "\n"
931 "#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE)\n"
932 "       LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz);\n"
933 "       LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz);\n"
934 "       LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz);\n"
935 "#endif\n"
936 "\n"
937 "       // transform unnormalized eye direction into tangent space\n"
938 "#ifdef USEEYEVECTOR\n"
939 "#ifndef USEFOG\n"
940 "       vec3 EyeVectorModelSpace;\n"
941 "#endif\n"
942 "       EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
943 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
944 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
945 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
946 "#endif\n"
947 "\n"
948 "#ifdef USEFOG\n"
949 "#ifndef USEEYEVECTOR\n"
950 "       EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
951 "#endif\n"
952 "       FogPlaneVertexDist = dot(FogPlane, gl_Vertex);\n"
953 "#endif\n"
954 "\n"
955 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
956 "       VectorS = gl_MultiTexCoord1.xyz;\n"
957 "       VectorT = gl_MultiTexCoord2.xyz;\n"
958 "       VectorR = gl_MultiTexCoord3.xyz;\n"
959 "#endif\n"
960 "\n"
961 "       // transform vertex to camera space, using ftransform to match non-VS rendering\n"
962 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
963 "\n"
964 "#ifdef USEREFLECTION\n"
965 "       ModelViewProjectionPosition = gl_Position;\n"
966 "#endif\n"
967 "}\n"
968 "#endif // !MODE_DEFERREDLIGHTSOURCE\n"
969 "#endif // !MODE_DEFERREDGEOMETRY\n"
970 "\n"
971 "#endif // VERTEX_SHADER\n"
972 "\n"
973 "\n"
974 "\n"
975 "\n"
976 "// fragment shader specific:\n"
977 "#ifdef FRAGMENT_SHADER\n"
978 "\n"
979 "uniform sampler2D Texture_Normal;\n"
980 "uniform sampler2D Texture_Color;\n"
981 "//#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)\n"
982 "uniform sampler2D Texture_Gloss;\n"
983 "//#endif\n"
984 "#ifdef USEGLOW\n"
985 "uniform sampler2D Texture_Glow;\n"
986 "#endif\n"
987 "#ifdef USEVERTEXTEXTUREBLEND\n"
988 "uniform sampler2D Texture_SecondaryNormal;\n"
989 "uniform sampler2D Texture_SecondaryColor;\n"
990 "//#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)\n"
991 "uniform sampler2D Texture_SecondaryGloss;\n"
992 "//#endif\n"
993 "#ifdef USEGLOW\n"
994 "uniform sampler2D Texture_SecondaryGlow;\n"
995 "#endif\n"
996 "#endif\n"
997 "#ifdef USECOLORMAPPING\n"
998 "uniform sampler2D Texture_Pants;\n"
999 "uniform sampler2D Texture_Shirt;\n"
1000 "#endif\n"
1001 "#ifdef USEFOG\n"
1002 "uniform sampler2D Texture_FogMask;\n"
1003 "#endif\n"
1004 "#ifdef USELIGHTMAP\n"
1005 "uniform sampler2D Texture_Lightmap;\n"
1006 "#endif\n"
1007 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)\n"
1008 "uniform sampler2D Texture_Deluxemap;\n"
1009 "#endif\n"
1010 "#ifdef USEREFLECTION\n"
1011 "uniform sampler2D Texture_Reflection;\n"
1012 "#endif\n"
1013 "\n"
1014 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
1015 "uniform sampler2DRect Texture_ScreenDepth;\n"
1016 "uniform sampler2DRect Texture_ScreenNormalMap;\n"
1017 "#endif\n"
1018 "#ifdef USEDEFERREDLIGHTMAP\n"
1019 "uniform sampler2DRect Texture_ScreenDiffuse;\n"
1020 "uniform sampler2DRect Texture_ScreenSpecular;\n"
1021 "#endif\n"
1022 "\n"
1023 "uniform myhalf3 Color_Pants;\n"
1024 "uniform myhalf3 Color_Shirt;\n"
1025 "uniform myhalf3 FogColor;\n"
1026 "\n"
1027 "#ifdef USEFOG\n"
1028 "uniform float FogRangeRecip;\n"
1029 "uniform float FogPlaneViewDist;\n"
1030 "uniform float FogHeightFade;\n"
1031 "myhalf FogVertex(void)\n"
1032 "{\n"
1033 "       float fogfrac;\n"
1034 "#ifdef USEFOGOUTSIDE\n"
1035 "       fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade);\n"
1036 "#else\n"
1037 "       fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);\n"
1038 "#endif\n"
1039 "       return myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)));\n"
1040 "}\n"
1041 "#endif\n"
1042 "\n"
1043 "#ifdef USEOFFSETMAPPING\n"
1044 "uniform float OffsetMapping_Scale;\n"
1045 "vec2 OffsetMapping(vec2 TexCoord)\n"
1046 "{\n"
1047 "#ifdef USEOFFSETMAPPING_RELIEFMAPPING\n"
1048 "       // 14 sample relief mapping: linear search and then binary search\n"
1049 "       // this basically steps forward a small amount repeatedly until it finds\n"
1050 "       // itself inside solid, then jitters forward and back using decreasing\n"
1051 "       // amounts to find the impact\n"
1052 "       //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1);\n"
1053 "       //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
1054 "       vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
1055 "       vec3 RT = vec3(TexCoord, 1);\n"
1056 "       OffsetVector *= 0.1;\n"
1057 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1058 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1059 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1060 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1061 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1062 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1063 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1064 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1065 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1066 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z)          - 0.5);\n"
1067 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5    - 0.25);\n"
1068 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25   - 0.125);\n"
1069 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125  - 0.0625);\n"
1070 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125);\n"
1071 "       return RT.xy;\n"
1072 "#else\n"
1073 "       // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits)\n"
1074 "       // this basically moves forward the full distance, and then backs up based\n"
1075 "       // on height of samples\n"
1076 "       //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1));\n"
1077 "       //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1));\n"
1078 "       vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1));\n"
1079 "       TexCoord += OffsetVector;\n"
1080 "       OffsetVector *= 0.333;\n"
1081 "       TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
1082 "       TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
1083 "       TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
1084 "       return TexCoord;\n"
1085 "#endif\n"
1086 "}\n"
1087 "#endif // USEOFFSETMAPPING\n"
1088 "\n"
1089 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE)\n"
1090 "uniform sampler2D Texture_Attenuation;\n"
1091 "uniform samplerCube Texture_Cube;\n"
1092 "\n"
1093 "#ifdef USESHADOWMAPRECT\n"
1094 "# ifdef USESHADOWSAMPLER\n"
1095 "uniform sampler2DRectShadow Texture_ShadowMapRect;\n"
1096 "# else\n"
1097 "uniform sampler2DRect Texture_ShadowMapRect;\n"
1098 "# endif\n"
1099 "#endif\n"
1100 "\n"
1101 "#ifdef USESHADOWMAP2D\n"
1102 "# ifdef USESHADOWSAMPLER\n"
1103 "uniform sampler2DShadow Texture_ShadowMap2D;\n"
1104 "# else\n"
1105 "uniform sampler2D Texture_ShadowMap2D;\n"
1106 "# endif\n"
1107 "#endif\n"
1108 "\n"
1109 "#ifdef USESHADOWMAPVSDCT\n"
1110 "uniform samplerCube Texture_CubeProjection;\n"
1111 "#endif\n"
1112 "\n"
1113 "#ifdef USESHADOWMAPCUBE\n"
1114 "# ifdef USESHADOWSAMPLER\n"
1115 "uniform samplerCubeShadow Texture_ShadowMapCube;\n"
1116 "# else\n"
1117 "uniform samplerCube Texture_ShadowMapCube;\n"
1118 "# endif\n"
1119 "#endif\n"
1120 "\n"
1121 "#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE)\n"
1122 "uniform vec2 ShadowMap_TextureScale;\n"
1123 "uniform vec4 ShadowMap_Parameters;\n"
1124 "#endif\n"
1125 "\n"
1126 "#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D)\n"
1127 "vec3 GetShadowMapTC2D(vec3 dir)\n"
1128 "{\n"
1129 "       vec3 adir = abs(dir);\n"
1130 "# ifndef USESHADOWMAPVSDCT\n"
1131 "       vec2 tc;\n"
1132 "       vec2 offset;\n"
1133 "       float ma;\n"
1134 "       if (adir.x > adir.y)\n"
1135 "       {\n"
1136 "               if (adir.x > adir.z) // X\n"
1137 "               {\n"
1138 "                       ma = adir.x;\n"
1139 "                       tc = dir.zy;\n"
1140 "                       offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5);\n"
1141 "               }\n"
1142 "               else // Z\n"
1143 "               {\n"
1144 "                       ma = adir.z;\n"
1145 "                       tc = dir.xy;\n"
1146 "                       offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5);\n"
1147 "               }\n"
1148 "       }\n"
1149 "       else\n"
1150 "       {\n"
1151 "               if (adir.y > adir.z) // Y\n"
1152 "               {\n"
1153 "                       ma = adir.y;\n"
1154 "                       tc = dir.xz;\n"
1155 "                       offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5);\n"
1156 "               }\n"
1157 "               else // Z\n"
1158 "               {\n"
1159 "                       ma = adir.z;\n"
1160 "                       tc = dir.xy;\n"
1161 "                       offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5);\n"
1162 "               }\n"
1163 "       }\n"
1164 "\n"
1165 "       vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma;\n"
1166 "       stc.xy += offset * ShadowMap_Parameters.y;\n"
1167 "       stc.z += ShadowMap_Parameters.z;\n"
1168 "       return stc;\n"
1169 "# else\n"
1170 "       vec4 proj = textureCube(Texture_CubeProjection, dir);\n"
1171 "       float ma = max(max(adir.x, adir.y), adir.z);\n"
1172 "       vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma;\n"
1173 "       stc.xy += proj.zw * ShadowMap_Parameters.y;\n"
1174 "       stc.z += ShadowMap_Parameters.z;\n"
1175 "       return stc;\n"
1176 "# endif\n"
1177 "}\n"
1178 "#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D)\n"
1179 "\n"
1180 "#ifdef USESHADOWMAPCUBE\n"
1181 "vec4 GetShadowMapTCCube(vec3 dir)\n"
1182 "{\n"
1183 "    vec3 adir = abs(dir);\n"
1184 "    return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z));\n"
1185 "}\n"
1186 "#endif\n"
1187 "\n"
1188 "# ifdef USESHADOWMAPRECT\n"
1189 "float ShadowMapCompare(vec3 dir)\n"
1190 "{\n"
1191 "       vec3 shadowmaptc = GetShadowMapTC2D(dir);\n"
1192 "       float f;\n"
1193 "#  ifdef USESHADOWSAMPLER\n"
1194 "\n"
1195 "#    ifdef USESHADOWMAPPCF\n"
1196 "#      define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r\n"
1197 "    f = dot(vec4(0.25), vec4(texval(-0.4, 1.0), texval(-1.0, -0.4), texval(0.4, -1.0), texval(1.0, 0.4)));\n"
1198 "#    else\n"
1199 "    f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r;\n"
1200 "#    endif\n"
1201 "\n"
1202 "#  else\n"
1203 "\n"
1204 "#    ifdef USESHADOWMAPPCF\n"
1205 "#      if USESHADOWMAPPCF > 1\n"
1206 "#        define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r\n"
1207 "    vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);\n"
1208 "    vec4 row1 = step(shadowmaptc.z, vec4(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0), texval( 2.0, -1.0)));\n"
1209 "    vec4 row2 = step(shadowmaptc.z, vec4(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0), texval( 2.0,  0.0)));\n"
1210 "    vec4 row3 = step(shadowmaptc.z, vec4(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0), texval( 2.0,  1.0)));\n"
1211 "    vec4 row4 = step(shadowmaptc.z, vec4(texval(-1.0,  2.0), texval( 0.0,  2.0), texval( 1.0,  2.0), texval( 2.0,  2.0)));\n"
1212 "    vec4 cols = row2 + row3 + mix(row1, row4, offset.y);\n"
1213 "    f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));\n"
1214 "#      else\n"
1215 "#        define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r\n"
1216 "    vec2 offset = fract(shadowmaptc.xy);\n"
1217 "    vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));\n"
1218 "    vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0)));\n"
1219 "    vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0)));\n"
1220 "    vec3 cols = row2 + mix(row1, row3, offset.y);\n"
1221 "    f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25));\n"
1222 "#      endif\n"
1223 "#    else\n"
1224 "    f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r);\n"
1225 "#    endif\n"
1226 "\n"
1227 "#  endif\n"
1228 "       return f;\n"
1229 "}\n"
1230 "# endif\n"
1231 "\n"
1232 "# ifdef USESHADOWMAP2D\n"
1233 "float ShadowMapCompare(vec3 dir)\n"
1234 "{\n"
1235 "    vec3 shadowmaptc = GetShadowMapTC2D(dir);\n"
1236 "    float f;\n"
1237 "\n"
1238 "#  ifdef USESHADOWSAMPLER\n"
1239 "#    ifdef USESHADOWMAPPCF\n"
1240 "#      define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r  \n"
1241 "    vec2 center = shadowmaptc.xy*ShadowMap_TextureScale;\n"
1242 "    f = dot(vec4(0.25), vec4(texval(-0.4, 1.0), texval(-1.0, -0.4), texval(0.4, -1.0), texval(1.0, 0.4)));\n"
1243 "#    else\n"
1244 "    f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r;\n"
1245 "#    endif\n"
1246 "#  else\n"
1247 "#    ifdef USESHADOWMAPPCF\n"
1248 "#     if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4)\n"
1249 "#      ifdef GL_ARB_texture_gather\n"
1250 "#        define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y))\n"
1251 "#      else\n"
1252 "#        define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale)\n"
1253 "#      endif\n"
1254 "    vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);\n"
1255 "    center *= ShadowMap_TextureScale;\n"
1256 "    vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0));\n"
1257 "    vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0));\n"
1258 "    vec4 group3 = step(shadowmaptc.z, texval(-1.0,  1.0));\n"
1259 "    vec4 group4 = step(shadowmaptc.z, texval( 1.0,  1.0));\n"
1260 "    vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) +\n"
1261 "                mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y);\n"
1262 "    f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));\n"
1263 "#     else\n"
1264 "#      ifdef GL_EXT_gpu_shader4\n"
1265 "#        define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r\n"
1266 "#      else\n"
1267 "#        define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r  \n"
1268 "#      endif\n"
1269 "#      if USESHADOWMAPPCF > 1\n"
1270 "    vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);\n"
1271 "    center *= ShadowMap_TextureScale;\n"
1272 "    vec4 row1 = step(shadowmaptc.z, vec4(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0), texval( 2.0, -1.0)));\n"
1273 "    vec4 row2 = step(shadowmaptc.z, vec4(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0), texval( 2.0,  0.0)));\n"
1274 "    vec4 row3 = step(shadowmaptc.z, vec4(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0), texval( 2.0,  1.0)));\n"
1275 "    vec4 row4 = step(shadowmaptc.z, vec4(texval(-1.0,  2.0), texval( 0.0,  2.0), texval( 1.0,  2.0), texval( 2.0,  2.0)));\n"
1276 "    vec4 cols = row2 + row3 + mix(row1, row4, offset.y);\n"
1277 "    f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));\n"
1278 "#      else\n"
1279 "    vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy);\n"
1280 "    vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));\n"
1281 "    vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0)));\n"
1282 "    vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0)));\n"
1283 "    vec3 cols = row2 + mix(row1, row3, offset.y);\n"
1284 "    f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25));\n"
1285 "#      endif\n"
1286 "#     endif\n"
1287 "#    else\n"
1288 "    f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r);\n"
1289 "#    endif\n"
1290 "#  endif\n"
1291 "    return f;\n"
1292 "}\n"
1293 "# endif\n"
1294 "\n"
1295 "# ifdef USESHADOWMAPCUBE\n"
1296 "float ShadowMapCompare(vec3 dir)\n"
1297 "{\n"
1298 "    // apply depth texture cubemap as light filter\n"
1299 "    vec4 shadowmaptc = GetShadowMapTCCube(dir);\n"
1300 "    float f;\n"
1301 "#  ifdef USESHADOWSAMPLER\n"
1302 "    f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r;\n"
1303 "#  else\n"
1304 "    f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r);\n"
1305 "#  endif\n"
1306 "    return f;\n"
1307 "}\n"
1308 "# endif\n"
1309 "#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE)\n"
1310 "\n"
1311 "#ifdef MODE_DEFERREDGEOMETRY\n"
1312 "void main(void)\n"
1313 "{\n"
1314 "#ifdef USEOFFSETMAPPING\n"
1315 "       // apply offsetmapping\n"
1316 "       vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
1317 "#define TexCoord TexCoordOffset\n"
1318 "#endif\n"
1319 "\n"
1320 "#ifdef USEALPHAKILL\n"
1321 "       if (texture2D(Texture_Color, TexCoord).a < 0.5)\n"
1322 "               discard;\n"
1323 "#endif\n"
1324 "\n"
1325 "#ifdef USEVERTEXTEXTUREBLEND\n"
1326 "       float alpha = texture2D(Texture_Color, TexCoord).a;\n"
1327 "       float terrainblend = clamp(float(gl_Color.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0));\n"
1328 "       //float terrainblend = min(float(gl_Color.a) * alpha * 2.0, float(1.0));\n"
1329 "       //float terrainblend = float(gl_Color.a) * alpha > 0.5;\n"
1330 "#endif\n"
1331 "\n"
1332 "#ifdef USEVERTEXTEXTUREBLEND\n"
1333 "       vec3 surfacenormal = mix(vec3(texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(texture2D(Texture_Normal, TexCoord)), terrainblend) - vec3(0.5, 0.5, 0.5);\n"
1334 "#else\n"
1335 "       vec3 surfacenormal = vec3(texture2D(Texture_Normal, TexCoord)) - vec3(0.5, 0.5, 0.5);\n"
1336 "#endif\n"
1337 "\n"
1338 "       gl_FragColor = vec4(normalize(surfacenormal.x * VectorS + surfacenormal.y * VectorT + surfacenormal.z * VectorR) * 0.5 + vec3(0.5, 0.5, 0.5), 1);\n"
1339 "}\n"
1340 "#else // !MODE_DEFERREDGEOMETRY\n"
1341 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
1342 "uniform mat4 ViewToLight;\n"
1343 "// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far));\n"
1344 "uniform vec2 ScreenToDepth;\n"
1345 "uniform myhalf3 DeferredColor_Ambient;\n"
1346 "uniform myhalf3 DeferredColor_Diffuse;\n"
1347 "#ifdef USESPECULAR\n"
1348 "uniform myhalf3 DeferredColor_Specular;\n"
1349 "uniform myhalf SpecularPower;\n"
1350 "#endif\n"
1351 "void main(void)\n"
1352 "{\n"
1353 "       // calculate viewspace pixel position\n"
1354 "       vec3 position;\n"
1355 "       position.z = ScreenToDepth.y / (texture2DRect(Texture_ScreenDepth, gl_FragCoord.xy).r + ScreenToDepth.x);\n"
1356 "       position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z);\n"
1357 "       // decode viewspace pixel normal\n"
1358 "       myhalf4 normalmap = texture2DRect(Texture_ScreenNormalMap, gl_FragCoord.xy);\n"
1359 "       myhalf3 surfacenormal = normalize(normalmap.rgb - myhalf3(0.5,0.5,0.5));\n"
1360 "       // surfacenormal = pixel normal in viewspace\n"
1361 "       // LightVector = pixel to light in viewspace\n"
1362 "       // CubeVector = position in lightspace\n"
1363 "       // eyevector = pixel to view in viewspace\n"
1364 "       vec3 CubeVector = vec3(ViewToLight * vec4(position,1));\n"
1365 "       myhalf fade = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));\n"
1366 "#ifdef USEDIFFUSE\n"
1367 "       // calculate diffuse shading\n"
1368 "       myhalf3 lightnormal = myhalf3(normalize(LightPosition - position));\n"
1369 "       myhalf diffuse = myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
1370 "#endif\n"
1371 "#ifdef USESPECULAR\n"
1372 "       // calculate directional shading\n"
1373 "       vec3 eyevector = position * -1.0;\n"
1374 "#  ifdef USEEXACTSPECULARMATH\n"
1375 "       myhalf specular = pow(myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), SpecularPower);\n"
1376 "#  else\n"
1377 "       myhalf3 specularnormal = normalize(lightnormal + myhalf3(normalize(eyevector)));\n"
1378 "       myhalf specular = pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
1379 "#  endif\n"
1380 "#endif\n"
1381 "\n"
1382 "#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D)\n"
1383 "       fade *= ShadowMapCompare(CubeVector);\n"
1384 "#endif\n"
1385 "\n"
1386 "#ifdef USEDIFFUSE\n"
1387 "       gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0);\n"
1388 "#else\n"
1389 "       gl_FragData[0] = vec4(DeferredColor_Ambient * fade, 1.0);\n"
1390 "#endif\n"
1391 "#ifdef USESPECULAR\n"
1392 "       gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0);\n"
1393 "#else\n"
1394 "       gl_FragData[1] = vec4(0.0, 0.0, 0.0, 1.0);\n"
1395 "#endif\n"
1396 "\n"
1397 "# ifdef USECUBEFILTER\n"
1398 "       vec3 cubecolor = textureCube(Texture_Cube, CubeVector).rgb;\n"
1399 "       gl_FragData[0] *= cubecolor;\n"
1400 "       gl_FragData[1] *= cubecolor;\n"
1401 "# endif\n"
1402 "}\n"
1403 "#else // !MODE_DEFERREDLIGHTSOURCE\n"
1404 "#ifdef USEDEFERREDLIGHTMAP\n"
1405 "uniform myhalf3 DeferredMod_Diffuse;\n"
1406 "uniform myhalf3 DeferredMod_Specular;\n"
1407 "#endif\n"
1408 "uniform myhalf3 Color_Ambient;\n"
1409 "uniform myhalf3 Color_Diffuse;\n"
1410 "uniform myhalf3 Color_Specular;\n"
1411 "uniform myhalf SpecularPower;\n"
1412 "#ifdef USEGLOW\n"
1413 "uniform myhalf3 Color_Glow;\n"
1414 "#endif\n"
1415 "uniform myhalf Alpha;\n"
1416 "#ifdef USEREFLECTION\n"
1417 "uniform vec4 DistortScaleRefractReflect;\n"
1418 "uniform vec4 ScreenScaleRefractReflect;\n"
1419 "uniform vec4 ScreenCenterRefractReflect;\n"
1420 "uniform myhalf4 ReflectColor;\n"
1421 "#endif\n"
1422 "#ifdef MODE_LIGHTDIRECTION\n"
1423 "uniform myhalf3 LightColor;\n"
1424 "#endif\n"
1425 "#ifdef MODE_LIGHTSOURCE\n"
1426 "uniform myhalf3 LightColor;\n"
1427 "#endif\n"
1428 "void main(void)\n"
1429 "{\n"
1430 "#ifdef USEOFFSETMAPPING\n"
1431 "       // apply offsetmapping\n"
1432 "       vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
1433 "#define TexCoord TexCoordOffset\n"
1434 "#endif\n"
1435 "\n"
1436 "       // combine the diffuse textures (base, pants, shirt)\n"
1437 "       myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord));\n"
1438 "#ifdef USEALPHAKILL\n"
1439 "       if (color.a < 0.5)\n"
1440 "               discard;\n"
1441 "#endif\n"
1442 "       color.a *= Alpha;\n"
1443 "#ifdef USECOLORMAPPING\n"
1444 "       color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt;\n"
1445 "#endif\n"
1446 "#ifdef USEVERTEXTEXTUREBLEND\n"
1447 "       myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0));\n"
1448 "       //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0));\n"
1449 "       //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5;\n"
1450 "       color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend);\n"
1451 "       color.a = 1.0;\n"
1452 "       //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend);\n"
1453 "#endif\n"
1454 "\n"
1455 "       // get the surface normal\n"
1456 "#ifdef USEVERTEXTEXTUREBLEND\n"
1457 "       myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5));\n"
1458 "#else\n"
1459 "       myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5));\n"
1460 "#endif\n"
1461 "\n"
1462 "       // get the material colors\n"
1463 "       myhalf3 diffusetex = color.rgb;\n"
1464 "#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)\n"
1465 "# ifdef USEVERTEXTEXTUREBLEND\n"
1466 "       myhalf3 glosstex = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend);\n"
1467 "# else\n"
1468 "       myhalf3 glosstex = myhalf3(texture2D(Texture_Gloss, TexCoord));\n"
1469 "# endif\n"
1470 "#endif\n"
1471 "\n"
1472 "\n"
1473 "\n"
1474 "\n"
1475 "#ifdef MODE_LIGHTSOURCE\n"
1476 "       // light source\n"
1477 "       myhalf3 lightnormal = myhalf3(normalize(LightVector));\n"
1478 "       myhalf diffuse = myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
1479 "       color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse);\n"
1480 "#ifdef USESPECULAR\n"
1481 "#ifdef USEEXACTSPECULARMATH\n"
1482 "       myhalf specular = pow(myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower);\n"
1483 "#else\n"
1484 "       myhalf3 specularnormal = normalize(lightnormal + myhalf3(normalize(EyeVector)));\n"
1485 "       myhalf specular = pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
1486 "#endif\n"
1487 "       color.rgb += glosstex * (specular * Color_Specular);\n"
1488 "#endif\n"
1489 "       color.rgb *= LightColor;\n"
1490 "       color.rgb *= myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));\n"
1491 "#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D)\n"
1492 "       color.rgb *= ShadowMapCompare(CubeVector);\n"
1493 "#endif\n"
1494 "# ifdef USECUBEFILTER\n"
1495 "       color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector));\n"
1496 "# endif\n"
1497 "#endif // MODE_LIGHTSOURCE\n"
1498 "\n"
1499 "\n"
1500 "\n"
1501 "\n"
1502 "#ifdef MODE_LIGHTDIRECTION\n"
1503 "#define SHADING\n"
1504 "       myhalf3 lightnormal = myhalf3(normalize(LightVector));\n"
1505 "#define lightcolor LightColor\n"
1506 "#endif // MODE_LIGHTDIRECTION\n"
1507 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
1508 "#define SHADING\n"
1509 "       // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe)\n"
1510 "       myhalf3 lightnormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0);\n"
1511 "       myhalf3 lightcolor = myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap));\n"
1512 "       // convert modelspace light vector to tangentspace\n"
1513 "       myhalf3 lightnormal;\n"
1514 "       lightnormal.x = dot(lightnormal_modelspace, myhalf3(VectorS));\n"
1515 "       lightnormal.y = dot(lightnormal_modelspace, myhalf3(VectorT));\n"
1516 "       lightnormal.z = dot(lightnormal_modelspace, myhalf3(VectorR));\n"
1517 "       // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division)\n"
1518 "       // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar\n"
1519 "       // is used (the lightmap and deluxemap coords correspond to virtually random coordinates\n"
1520 "       // on that luxel, and NOT to its center, because recursive triangle subdivision is used\n"
1521 "       // to map the luxels to coordinates on the draw surfaces), which also causes\n"
1522 "       // deluxemaps to be wrong because light contributions from the wrong side of the surface\n"
1523 "       // are added up. To prevent divisions by zero or strong exaggerations, a max()\n"
1524 "       // nudge is done here at expense of some additional fps. This is ONLY needed for\n"
1525 "       // deluxemaps, tangentspace deluxemap avoid this problem by design.\n"
1526 "       lightcolor *= 1.0 / max(0.25, lightnormal.z);\n"
1527 "#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
1528 "#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n"
1529 "#define SHADING\n"
1530 "       // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light)\n"
1531 "       myhalf3 lightnormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0);\n"
1532 "       myhalf3 lightcolor = myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap));\n"
1533 "#endif\n"
1534 "\n"
1535 "\n"
1536 "\n"
1537 "\n"
1538 "#ifdef MODE_LIGHTMAP\n"
1539 "       color.rgb = diffusetex * (Color_Ambient + myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * Color_Diffuse);\n"
1540 "#endif // MODE_LIGHTMAP\n"
1541 "#ifdef MODE_VERTEXCOLOR\n"
1542 "       color.rgb = diffusetex * (Color_Ambient + myhalf3(gl_Color.rgb) * Color_Diffuse);\n"
1543 "#endif // MODE_VERTEXCOLOR\n"
1544 "#ifdef MODE_FLATCOLOR\n"
1545 "       color.rgb = diffusetex * Color_Ambient;\n"
1546 "#endif // MODE_FLATCOLOR\n"
1547 "\n"
1548 "\n"
1549 "\n"
1550 "\n"
1551 "#ifdef SHADING\n"
1552 "# ifdef USEDIFFUSE\n"
1553 "       myhalf diffuse = myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
1554 "#  ifdef USESPECULAR\n"
1555 "#   ifdef USEEXACTSPECULARMATH\n"
1556 "       myhalf specular = pow(myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower);\n"
1557 "#   else\n"
1558 "       myhalf3 specularnormal = normalize(lightnormal + myhalf3(normalize(EyeVector)));\n"
1559 "       myhalf specular = pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
1560 "#   endif\n"
1561 "       color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex * Color_Specular * specular) * lightcolor;\n"
1562 "#  else\n"
1563 "       color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor);\n"
1564 "#  endif\n"
1565 "# else\n"
1566 "       color.rgb = diffusetex * Color_Ambient;\n"
1567 "# endif\n"
1568 "#endif\n"
1569 "\n"
1570 "#ifdef USEDEFERREDLIGHTMAP\n"
1571 "       color.rgb += diffusetex * myhalf3(texture2DRect(Texture_ScreenDiffuse, gl_FragCoord.xy)) * DeferredMod_Diffuse;\n"
1572 "       color.rgb += glosstex * myhalf3(texture2DRect(Texture_ScreenSpecular, gl_FragCoord.xy)) * DeferredMod_Specular;\n"
1573 "#endif\n"
1574 "\n"
1575 "#ifdef USEGLOW\n"
1576 "#ifdef USEVERTEXTEXTUREBLEND\n"
1577 "       color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend) * Color_Glow;\n"
1578 "#else\n"
1579 "       color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * Color_Glow;\n"
1580 "#endif\n"
1581 "#endif\n"
1582 "\n"
1583 "#ifdef USEFOG\n"
1584 "       color.rgb = mix(FogColor, color.rgb, FogVertex());\n"
1585 "#endif\n"
1586 "\n"
1587 "       // reflection must come last because it already contains exactly the correct fog (the reflection render preserves camera distance from the plane, it only flips the side) and ContrastBoost/SceneBrightness\n"
1588 "#ifdef USEREFLECTION\n"
1589 "       vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
1590 "       //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
1591 "       vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw;\n"
1592 "       vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw;\n"
1593 "       // FIXME temporary hack to detect the case that the reflection\n"
1594 "       // gets blackened at edges due to leaving the area that contains actual\n"
1595 "       // content.\n"
1596 "       // Remove this 'ack once we have a better way to stop this thing from\n"
1597 "       // 'appening.\n"
1598 "       float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05);\n"
1599 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05);\n"
1600 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05);\n"
1601 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05);\n"
1602 "       ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);\n"
1603 "       color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a);\n"
1604 "#endif\n"
1605 "\n"
1606 "       gl_FragColor = vec4(color);\n"
1607 "}\n"
1608 "#endif // !MODE_DEFERREDLIGHTSOURCE\n"
1609 "#endif // !MODE_DEFERREDGEOMETRY\n"
1610 "\n"
1611 "#endif // FRAGMENT_SHADER\n"
1612 "\n"
1613 "#endif // !MODE_WATER\n"
1614 "#endif // !MODE_REFRACTION\n"
1615 "#endif // !MODE_BLOOMBLUR\n"
1616 "#endif // !MODE_GENERIC\n"
1617 "#endif // !MODE_POSTPROCESS\n"
1618 "#endif // !MODE_SHOWDEPTH\n"
1619 "#endif // !MODE_DEPTH_OR_SHADOW\n"
1620 ;
1621
1622 const char *builtincgshaderstring = "";
1623
1624 typedef struct shaderpermutationinfo_s
1625 {
1626         const char *pretext;
1627         const char *name;
1628 }
1629 shaderpermutationinfo_t;
1630
1631 typedef struct shadermodeinfo_s
1632 {
1633         const char *vertexfilename;
1634         const char *geometryfilename;
1635         const char *fragmentfilename;
1636         const char *pretext;
1637         const char *name;
1638 }
1639 shadermodeinfo_t;
1640
1641 typedef enum shaderpermutation_e
1642 {
1643         SHADERPERMUTATION_DIFFUSE = 1<<0, ///< (lightsource) whether to use directional shading
1644         SHADERPERMUTATION_VERTEXTEXTUREBLEND = 1<<1, ///< indicates this is a two-layer material blend based on vertex alpha (q3bsp)
1645         SHADERPERMUTATION_VIEWTINT = 1<<2, ///< view tint (postprocessing only)
1646         SHADERPERMUTATION_COLORMAPPING = 1<<3, ///< indicates this is a colormapped skin
1647         SHADERPERMUTATION_SATURATION = 1<<4, ///< saturation (postprocessing only)
1648         SHADERPERMUTATION_FOGINSIDE = 1<<5, ///< tint the color by fog color or black if using additive blend mode
1649         SHADERPERMUTATION_FOGOUTSIDE = 1<<6, ///< tint the color by fog color or black if using additive blend mode
1650         SHADERPERMUTATION_GAMMARAMPS = 1<<7, ///< gamma (postprocessing only)
1651         SHADERPERMUTATION_CUBEFILTER = 1<<8, ///< (lightsource) use cubemap light filter
1652         SHADERPERMUTATION_GLOW = 1<<9, ///< (lightmap) blend in an additive glow texture
1653         SHADERPERMUTATION_BLOOM = 1<<10, ///< bloom (postprocessing only)
1654         SHADERPERMUTATION_SPECULAR = 1<<11, ///< (lightsource or deluxemapping) render specular effects
1655         SHADERPERMUTATION_POSTPROCESSING = 1<<12, ///< user defined postprocessing (postprocessing only)
1656         SHADERPERMUTATION_EXACTSPECULARMATH = 1<<13, ///< (lightsource or deluxemapping) use exact reflection map for specular effects, as opposed to the usual OpenGL approximation
1657         SHADERPERMUTATION_REFLECTION = 1<<14, ///< normalmap-perturbed reflection of the scene infront of the surface, preformed as an overlay on the surface
1658         SHADERPERMUTATION_OFFSETMAPPING = 1<<15, ///< adjust texcoords to roughly simulate a displacement mapped surface
1659         SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING = 1<<16, ///< adjust texcoords to accurately simulate a displacement mapped surface (requires OFFSETMAPPING to also be set!)
1660         SHADERPERMUTATION_SHADOWMAPRECT = 1<<17, ///< (lightsource) use shadowmap rectangle texture as light filter
1661         SHADERPERMUTATION_SHADOWMAPCUBE = 1<<18, ///< (lightsource) use shadowmap cubemap texture as light filter
1662         SHADERPERMUTATION_SHADOWMAP2D = 1<<19, ///< (lightsource) use shadowmap rectangle texture as light filter
1663         SHADERPERMUTATION_SHADOWMAPPCF = 1<<20, ///< (lightsource) use percentage closer filtering on shadowmap test results
1664         SHADERPERMUTATION_SHADOWMAPPCF2 = 1<<21, ///< (lightsource) use higher quality percentage closer filtering on shadowmap test results
1665         SHADERPERMUTATION_SHADOWSAMPLER = 1<<22, ///< (lightsource) use hardware shadowmap test
1666         SHADERPERMUTATION_SHADOWMAPVSDCT = 1<<23, ///< (lightsource) use virtual shadow depth cube texture for shadowmap indexing
1667         SHADERPERMUTATION_DEFERREDLIGHTMAP = 1<<24, ///< (lightmap) read Texture_ScreenDiffuse/Specular textures and add them on top of lightmapping
1668         SHADERPERMUTATION_ALPHAKILL = 1<<25, ///< (deferredgeometry) discard pixel if diffuse texture alpha below 0.5
1669         SHADERPERMUTATION_LIMIT = 1<<26, ///< size of permutations array
1670         SHADERPERMUTATION_COUNT = 27 ///< size of shaderpermutationinfo array
1671 }
1672 shaderpermutation_t;
1673
1674 // NOTE: MUST MATCH ORDER OF SHADERPERMUTATION_* DEFINES!
1675 shaderpermutationinfo_t shaderpermutationinfo[SHADERPERMUTATION_COUNT] =
1676 {
1677         {"#define USEDIFFUSE\n", " diffuse"},
1678         {"#define USEVERTEXTEXTUREBLEND\n", " vertextextureblend"},
1679         {"#define USEVIEWTINT\n", " viewtint"},
1680         {"#define USECOLORMAPPING\n", " colormapping"},
1681         {"#define USESATURATION\n", " saturation"},
1682         {"#define USEFOGINSIDE\n", " foginside"},
1683         {"#define USEFOGOUTSIDE\n", " fogoutside"},
1684         {"#define USEGAMMARAMPS\n", " gammaramps"},
1685         {"#define USECUBEFILTER\n", " cubefilter"},
1686         {"#define USEGLOW\n", " glow"},
1687         {"#define USEBLOOM\n", " bloom"},
1688         {"#define USESPECULAR\n", " specular"},
1689         {"#define USEPOSTPROCESSING\n", " postprocessing"},
1690         {"#define USEEXACTSPECULARMATH\n", " exactspecularmath"},
1691         {"#define USEREFLECTION\n", " reflection"},
1692         {"#define USEOFFSETMAPPING\n", " offsetmapping"},
1693         {"#define USEOFFSETMAPPING_RELIEFMAPPING\n", " reliefmapping"},
1694         {"#define USESHADOWMAPRECT\n", " shadowmaprect"},
1695         {"#define USESHADOWMAPCUBE\n", " shadowmapcube"},
1696         {"#define USESHADOWMAP2D\n", " shadowmap2d"},
1697         {"#define USESHADOWMAPPCF 1\n", " shadowmappcf"},
1698         {"#define USESHADOWMAPPCF 2\n", " shadowmappcf2"},
1699         {"#define USESHADOWSAMPLER\n", " shadowsampler"},
1700         {"#define USESHADOWMAPVSDCT\n", " shadowmapvsdct"},
1701         {"#define USEDEFERREDLIGHTMAP\n", " deferredlightmap"},
1702         {"#define USEALPHAKILL\n", " alphakill"},
1703 };
1704
1705 /// this enum is multiplied by SHADERPERMUTATION_MODEBASE
1706 typedef enum shadermode_e
1707 {
1708         SHADERMODE_GENERIC, ///< (particles/HUD/etc) vertex color, optionally multiplied by one texture
1709         SHADERMODE_POSTPROCESS, ///< postprocessing shader (r_glsl_postprocess)
1710         SHADERMODE_DEPTH_OR_SHADOW, ///< (depthfirst/shadows) vertex shader only
1711         SHADERMODE_FLATCOLOR, ///< (lightmap) modulate texture by uniform color (q1bsp, q3bsp)
1712         SHADERMODE_VERTEXCOLOR, ///< (lightmap) modulate texture by vertex colors (q3bsp)
1713         SHADERMODE_LIGHTMAP, ///< (lightmap) modulate texture by lightmap texture (q1bsp, q3bsp)
1714         SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE, ///< (lightmap) use directional pixel shading from texture containing modelspace light directions (q3bsp deluxemap)
1715         SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE, ///< (lightmap) use directional pixel shading from texture containing tangentspace light directions (q1bsp deluxemap)
1716         SHADERMODE_LIGHTDIRECTION, ///< (lightmap) use directional pixel shading from fixed light direction (q3bsp)
1717         SHADERMODE_LIGHTSOURCE, ///< (lightsource) use directional pixel shading from light source (rtlight)
1718         SHADERMODE_REFRACTION, ///< refract background (the material is rendered normally after this pass)
1719         SHADERMODE_WATER, ///< refract background and reflection (the material is rendered normally after this pass)
1720         SHADERMODE_SHOWDEPTH, ///< (debugging) renders depth as color
1721         SHADERMODE_DEFERREDGEOMETRY, ///< (deferred) render material properties to screenspace geometry buffers
1722         SHADERMODE_DEFERREDLIGHTSOURCE, ///< (deferred) use directional pixel shading from light source (rtlight) on screenspace geometry buffers
1723         SHADERMODE_COUNT
1724 }
1725 shadermode_t;
1726
1727 // NOTE: MUST MATCH ORDER OF SHADERMODE_* ENUMS!
1728 shadermodeinfo_t glslshadermodeinfo[SHADERMODE_COUNT] =
1729 {
1730         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_GENERIC\n", " generic"},
1731         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_POSTPROCESS\n", " postprocess"},
1732         {"glsl/default.glsl", NULL, NULL               , "#define MODE_DEPTH_OR_SHADOW\n", " depth/shadow"},
1733         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_FLATCOLOR\n", " flatcolor"},
1734         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_VERTEXCOLOR\n", " vertexcolor"},
1735         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTMAP\n", " lightmap"},
1736         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
1737         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
1738         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTION\n", " lightdirection"},
1739         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTSOURCE\n", " lightsource"},
1740         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_REFRACTION\n", " refraction"},
1741         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_WATER\n", " water"},
1742         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_SHOWDEPTH\n", " showdepth"},
1743         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_DEFERREDGEOMETRY\n", " deferredgeometry"},
1744         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_DEFERREDLIGHTSOURCE\n", " deferredlightsource"},
1745 };
1746
1747 #ifdef SUPPORTCG
1748 shadermodeinfo_t cgshadermodeinfo[SHADERMODE_COUNT] =
1749 {
1750         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_GENERIC\n", " generic"},
1751         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_POSTPROCESS\n", " postprocess"},
1752         {"cg/default.cg", NULL, NULL           , "#define MODE_DEPTH_OR_SHADOW\n", " depth/shadow"},
1753         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_FLATCOLOR\n", " flatcolor"},
1754         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_VERTEXCOLOR\n", " vertexcolor"},
1755         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_LIGHTMAP\n", " lightmap"},
1756         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
1757         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
1758         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_LIGHTDIRECTION\n", " lightdirection"},
1759         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_LIGHTSOURCE\n", " lightsource"},
1760         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_REFRACTION\n", " refraction"},
1761         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_WATER\n", " water"},
1762         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_SHOWDEPTH\n", " showdepth"},
1763         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_DEFERREDGEOMETRY\n", " deferredgeometry"},
1764         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_DEFERREDLIGHTSOURCE\n", " deferredlightsource"},
1765 };
1766 #endif
1767
1768 struct r_glsl_permutation_s;
1769 typedef struct r_glsl_permutation_s
1770 {
1771         /// hash lookup data
1772         struct r_glsl_permutation_s *hashnext;
1773         unsigned int mode;
1774         unsigned int permutation;
1775
1776         /// indicates if we have tried compiling this permutation already
1777         qboolean compiled;
1778         /// 0 if compilation failed
1779         int program;
1780         /// locations of detected uniforms in program object, or -1 if not found
1781         int loc_Texture_First;
1782         int loc_Texture_Second;
1783         int loc_Texture_GammaRamps;
1784         int loc_Texture_Normal;
1785         int loc_Texture_Color;
1786         int loc_Texture_Gloss;
1787         int loc_Texture_Glow;
1788         int loc_Texture_SecondaryNormal;
1789         int loc_Texture_SecondaryColor;
1790         int loc_Texture_SecondaryGloss;
1791         int loc_Texture_SecondaryGlow;
1792         int loc_Texture_Pants;
1793         int loc_Texture_Shirt;
1794         int loc_Texture_FogMask;
1795         int loc_Texture_Lightmap;
1796         int loc_Texture_Deluxemap;
1797         int loc_Texture_Attenuation;
1798         int loc_Texture_Cube;
1799         int loc_Texture_Refraction;
1800         int loc_Texture_Reflection;
1801         int loc_Texture_ShadowMapRect;
1802         int loc_Texture_ShadowMapCube;
1803         int loc_Texture_ShadowMap2D;
1804         int loc_Texture_CubeProjection;
1805         int loc_Texture_ScreenDepth;
1806         int loc_Texture_ScreenNormalMap;
1807         int loc_Texture_ScreenDiffuse;
1808         int loc_Texture_ScreenSpecular;
1809         int loc_Alpha;
1810         int loc_BloomBlur_Parameters;
1811         int loc_ClientTime;
1812         int loc_Color_Ambient;
1813         int loc_Color_Diffuse;
1814         int loc_Color_Specular;
1815         int loc_Color_Glow;
1816         int loc_Color_Pants;
1817         int loc_Color_Shirt;
1818         int loc_DeferredColor_Ambient;
1819         int loc_DeferredColor_Diffuse;
1820         int loc_DeferredColor_Specular;
1821         int loc_DeferredMod_Diffuse;
1822         int loc_DeferredMod_Specular;
1823         int loc_DistortScaleRefractReflect;
1824         int loc_EyePosition;
1825         int loc_FogColor;
1826         int loc_FogHeightFade;
1827         int loc_FogPlane;
1828         int loc_FogPlaneViewDist;
1829         int loc_FogRangeRecip;
1830         int loc_LightColor;
1831         int loc_LightDir;
1832         int loc_LightPosition;
1833         int loc_OffsetMapping_Scale;
1834         int loc_PixelSize;
1835         int loc_ReflectColor;
1836         int loc_ReflectFactor;
1837         int loc_ReflectOffset;
1838         int loc_RefractColor;
1839         int loc_Saturation;
1840         int loc_ScreenCenterRefractReflect;
1841         int loc_ScreenScaleRefractReflect;
1842         int loc_ScreenToDepth;
1843         int loc_ShadowMap_Parameters;
1844         int loc_ShadowMap_TextureScale;
1845         int loc_SpecularPower;
1846         int loc_UserVec1;
1847         int loc_UserVec2;
1848         int loc_UserVec3;
1849         int loc_UserVec4;
1850         int loc_ViewTintColor;
1851         int loc_ViewToLight;
1852         int loc_ModelToLight;
1853         int loc_TexMatrix;
1854         int loc_BackgroundTexMatrix;
1855         int loc_ModelViewProjectionMatrix;
1856         int loc_ModelViewMatrix;
1857 }
1858 r_glsl_permutation_t;
1859
1860 #define SHADERPERMUTATION_HASHSIZE 256
1861
1862 /// information about each possible shader permutation
1863 r_glsl_permutation_t *r_glsl_permutationhash[SHADERMODE_COUNT][SHADERPERMUTATION_HASHSIZE];
1864 /// currently selected permutation
1865 r_glsl_permutation_t *r_glsl_permutation;
1866 /// storage for permutations linked in the hash table
1867 memexpandablearray_t r_glsl_permutationarray;
1868
1869 static r_glsl_permutation_t *R_GLSL_FindPermutation(unsigned int mode, unsigned int permutation)
1870 {
1871         //unsigned int hashdepth = 0;
1872         unsigned int hashindex = (permutation * 0x1021) & (SHADERPERMUTATION_HASHSIZE - 1);
1873         r_glsl_permutation_t *p;
1874         for (p = r_glsl_permutationhash[mode][hashindex];p;p = p->hashnext)
1875         {
1876                 if (p->mode == mode && p->permutation == permutation)
1877                 {
1878                         //if (hashdepth > 10)
1879                         //      Con_Printf("R_GLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
1880                         return p;
1881                 }
1882                 //hashdepth++;
1883         }
1884         p = (r_glsl_permutation_t*)Mem_ExpandableArray_AllocRecord(&r_glsl_permutationarray);
1885         p->mode = mode;
1886         p->permutation = permutation;
1887         p->hashnext = r_glsl_permutationhash[mode][hashindex];
1888         r_glsl_permutationhash[mode][hashindex] = p;
1889         //if (hashdepth > 10)
1890         //      Con_Printf("R_GLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
1891         return p;
1892 }
1893
1894 static char *R_GLSL_GetText(const char *filename, qboolean printfromdisknotice)
1895 {
1896         char *shaderstring;
1897         if (!filename || !filename[0])
1898                 return NULL;
1899         shaderstring = (char *)FS_LoadFile(filename, r_main_mempool, false, NULL);
1900         if (shaderstring)
1901         {
1902                 if (printfromdisknotice)
1903                         Con_DPrintf("from disk %s... ", filename);
1904                 return shaderstring;
1905         }
1906         else if (!strcmp(filename, "glsl/default.glsl"))
1907         {
1908                 shaderstring = (char *) Mem_Alloc(r_main_mempool, strlen(builtinshaderstring) + 1);
1909                 memcpy(shaderstring, builtinshaderstring, strlen(builtinshaderstring) + 1);
1910         }
1911         return shaderstring;
1912 }
1913
1914 static void R_GLSL_CompilePermutation(r_glsl_permutation_t *p, unsigned int mode, unsigned int permutation)
1915 {
1916         int i;
1917         shadermodeinfo_t *modeinfo = glslshadermodeinfo + mode;
1918         int vertstrings_count = 0;
1919         int geomstrings_count = 0;
1920         int fragstrings_count = 0;
1921         char *vertexstring, *geometrystring, *fragmentstring;
1922         const char *vertstrings_list[32+3];
1923         const char *geomstrings_list[32+3];
1924         const char *fragstrings_list[32+3];
1925         char permutationname[256];
1926
1927         if (p->compiled)
1928                 return;
1929         p->compiled = true;
1930         p->program = 0;
1931
1932         permutationname[0] = 0;
1933         vertexstring   = R_GLSL_GetText(modeinfo->vertexfilename, true);
1934         geometrystring = R_GLSL_GetText(modeinfo->geometryfilename, false);
1935         fragmentstring = R_GLSL_GetText(modeinfo->fragmentfilename, false);
1936
1937         strlcat(permutationname, modeinfo->vertexfilename, sizeof(permutationname));
1938
1939         // the first pretext is which type of shader to compile as
1940         // (later these will all be bound together as a program object)
1941         vertstrings_list[vertstrings_count++] = "#define VERTEX_SHADER\n";
1942         geomstrings_list[geomstrings_count++] = "#define GEOMETRY_SHADER\n";
1943         fragstrings_list[fragstrings_count++] = "#define FRAGMENT_SHADER\n";
1944
1945         // the second pretext is the mode (for example a light source)
1946         vertstrings_list[vertstrings_count++] = modeinfo->pretext;
1947         geomstrings_list[geomstrings_count++] = modeinfo->pretext;
1948         fragstrings_list[fragstrings_count++] = modeinfo->pretext;
1949         strlcat(permutationname, modeinfo->name, sizeof(permutationname));
1950
1951         // now add all the permutation pretexts
1952         for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1953         {
1954                 if (permutation & (1<<i))
1955                 {
1956                         vertstrings_list[vertstrings_count++] = shaderpermutationinfo[i].pretext;
1957                         geomstrings_list[geomstrings_count++] = shaderpermutationinfo[i].pretext;
1958                         fragstrings_list[fragstrings_count++] = shaderpermutationinfo[i].pretext;
1959                         strlcat(permutationname, shaderpermutationinfo[i].name, sizeof(permutationname));
1960                 }
1961                 else
1962                 {
1963                         // keep line numbers correct
1964                         vertstrings_list[vertstrings_count++] = "\n";
1965                         geomstrings_list[geomstrings_count++] = "\n";
1966                         fragstrings_list[fragstrings_count++] = "\n";
1967                 }
1968         }
1969
1970         // now append the shader text itself
1971         vertstrings_list[vertstrings_count++] = vertexstring;
1972         geomstrings_list[geomstrings_count++] = geometrystring;
1973         fragstrings_list[fragstrings_count++] = fragmentstring;
1974
1975         // if any sources were NULL, clear the respective list
1976         if (!vertexstring)
1977                 vertstrings_count = 0;
1978         if (!geometrystring)
1979                 geomstrings_count = 0;
1980         if (!fragmentstring)
1981                 fragstrings_count = 0;
1982
1983         // compile the shader program
1984         if (vertstrings_count + geomstrings_count + fragstrings_count)
1985                 p->program = GL_Backend_CompileProgram(vertstrings_count, vertstrings_list, geomstrings_count, geomstrings_list, fragstrings_count, fragstrings_list);
1986         if (p->program)
1987         {
1988                 CHECKGLERROR
1989                 qglUseProgramObjectARB(p->program);CHECKGLERROR
1990                 // look up all the uniform variable names we care about, so we don't
1991                 // have to look them up every time we set them
1992
1993                 p->loc_Texture_First              = qglGetUniformLocationARB(p->program, "Texture_First");
1994                 p->loc_Texture_Second             = qglGetUniformLocationARB(p->program, "Texture_Second");
1995                 p->loc_Texture_GammaRamps         = qglGetUniformLocationARB(p->program, "Texture_GammaRamps");
1996                 p->loc_Texture_Normal             = qglGetUniformLocationARB(p->program, "Texture_Normal");
1997                 p->loc_Texture_Color              = qglGetUniformLocationARB(p->program, "Texture_Color");
1998                 p->loc_Texture_Gloss              = qglGetUniformLocationARB(p->program, "Texture_Gloss");
1999                 p->loc_Texture_Glow               = qglGetUniformLocationARB(p->program, "Texture_Glow");
2000                 p->loc_Texture_SecondaryNormal    = qglGetUniformLocationARB(p->program, "Texture_SecondaryNormal");
2001                 p->loc_Texture_SecondaryColor     = qglGetUniformLocationARB(p->program, "Texture_SecondaryColor");
2002                 p->loc_Texture_SecondaryGloss     = qglGetUniformLocationARB(p->program, "Texture_SecondaryGloss");
2003                 p->loc_Texture_SecondaryGlow      = qglGetUniformLocationARB(p->program, "Texture_SecondaryGlow");
2004                 p->loc_Texture_Pants              = qglGetUniformLocationARB(p->program, "Texture_Pants");
2005                 p->loc_Texture_Shirt              = qglGetUniformLocationARB(p->program, "Texture_Shirt");
2006                 p->loc_Texture_FogMask            = qglGetUniformLocationARB(p->program, "Texture_FogMask");
2007                 p->loc_Texture_Lightmap           = qglGetUniformLocationARB(p->program, "Texture_Lightmap");
2008                 p->loc_Texture_Deluxemap          = qglGetUniformLocationARB(p->program, "Texture_Deluxemap");
2009                 p->loc_Texture_Attenuation        = qglGetUniformLocationARB(p->program, "Texture_Attenuation");
2010                 p->loc_Texture_Cube               = qglGetUniformLocationARB(p->program, "Texture_Cube");
2011                 p->loc_Texture_Refraction         = qglGetUniformLocationARB(p->program, "Texture_Refraction");
2012                 p->loc_Texture_Reflection         = qglGetUniformLocationARB(p->program, "Texture_Reflection");
2013                 p->loc_Texture_ShadowMapRect      = qglGetUniformLocationARB(p->program, "Texture_ShadowMapRect");
2014                 p->loc_Texture_ShadowMapCube      = qglGetUniformLocationARB(p->program, "Texture_ShadowMapCube");
2015                 p->loc_Texture_ShadowMap2D        = qglGetUniformLocationARB(p->program, "Texture_ShadowMap2D");
2016                 p->loc_Texture_CubeProjection     = qglGetUniformLocationARB(p->program, "Texture_CubeProjection");
2017                 p->loc_Texture_ScreenDepth        = qglGetUniformLocationARB(p->program, "Texture_ScreenDepth");
2018                 p->loc_Texture_ScreenNormalMap    = qglGetUniformLocationARB(p->program, "Texture_ScreenNormalMap");
2019                 p->loc_Texture_ScreenDiffuse      = qglGetUniformLocationARB(p->program, "Texture_ScreenDiffuse");
2020                 p->loc_Texture_ScreenSpecular     = qglGetUniformLocationARB(p->program, "Texture_ScreenSpecular");
2021                 p->loc_Alpha                      = qglGetUniformLocationARB(p->program, "Alpha");
2022                 p->loc_BloomBlur_Parameters       = qglGetUniformLocationARB(p->program, "BloomBlur_Parameters");
2023                 p->loc_ClientTime                 = qglGetUniformLocationARB(p->program, "ClientTime");
2024                 p->loc_Color_Ambient              = qglGetUniformLocationARB(p->program, "Color_Ambient");
2025                 p->loc_Color_Diffuse              = qglGetUniformLocationARB(p->program, "Color_Diffuse");
2026                 p->loc_Color_Specular             = qglGetUniformLocationARB(p->program, "Color_Specular");
2027                 p->loc_Color_Glow                 = qglGetUniformLocationARB(p->program, "Color_Glow");
2028                 p->loc_Color_Pants                = qglGetUniformLocationARB(p->program, "Color_Pants");
2029                 p->loc_Color_Shirt                = qglGetUniformLocationARB(p->program, "Color_Shirt");
2030                 p->loc_DeferredColor_Ambient      = qglGetUniformLocationARB(p->program, "DeferredColor_Ambient");
2031                 p->loc_DeferredColor_Diffuse      = qglGetUniformLocationARB(p->program, "DeferredColor_Diffuse");
2032                 p->loc_DeferredColor_Specular     = qglGetUniformLocationARB(p->program, "DeferredColor_Specular");
2033                 p->loc_DeferredMod_Diffuse        = qglGetUniformLocationARB(p->program, "DeferredMod_Diffuse");
2034                 p->loc_DeferredMod_Specular       = qglGetUniformLocationARB(p->program, "DeferredMod_Specular");
2035                 p->loc_DistortScaleRefractReflect = qglGetUniformLocationARB(p->program, "DistortScaleRefractReflect");
2036                 p->loc_EyePosition                = qglGetUniformLocationARB(p->program, "EyePosition");
2037                 p->loc_FogColor                   = qglGetUniformLocationARB(p->program, "FogColor");
2038                 p->loc_FogHeightFade              = qglGetUniformLocationARB(p->program, "FogHeightFade");
2039                 p->loc_FogPlane                   = qglGetUniformLocationARB(p->program, "FogPlane");
2040                 p->loc_FogPlaneViewDist           = qglGetUniformLocationARB(p->program, "FogPlaneViewDist");
2041                 p->loc_FogRangeRecip              = qglGetUniformLocationARB(p->program, "FogRangeRecip");
2042                 p->loc_LightColor                 = qglGetUniformLocationARB(p->program, "LightColor");
2043                 p->loc_LightDir                   = qglGetUniformLocationARB(p->program, "LightDir");
2044                 p->loc_LightPosition              = qglGetUniformLocationARB(p->program, "LightPosition");
2045                 p->loc_OffsetMapping_Scale        = qglGetUniformLocationARB(p->program, "OffsetMapping_Scale");
2046                 p->loc_PixelSize                  = qglGetUniformLocationARB(p->program, "PixelSize");
2047                 p->loc_ReflectColor               = qglGetUniformLocationARB(p->program, "ReflectColor");
2048                 p->loc_ReflectFactor              = qglGetUniformLocationARB(p->program, "ReflectFactor");
2049                 p->loc_ReflectOffset              = qglGetUniformLocationARB(p->program, "ReflectOffset");
2050                 p->loc_RefractColor               = qglGetUniformLocationARB(p->program, "RefractColor");
2051                 p->loc_Saturation                 = qglGetUniformLocationARB(p->program, "Saturation");
2052                 p->loc_ScreenCenterRefractReflect = qglGetUniformLocationARB(p->program, "ScreenCenterRefractReflect");
2053                 p->loc_ScreenScaleRefractReflect  = qglGetUniformLocationARB(p->program, "ScreenScaleRefractReflect");
2054                 p->loc_ScreenToDepth              = qglGetUniformLocationARB(p->program, "ScreenToDepth");
2055                 p->loc_ShadowMap_Parameters       = qglGetUniformLocationARB(p->program, "ShadowMap_Parameters");
2056                 p->loc_ShadowMap_TextureScale     = qglGetUniformLocationARB(p->program, "ShadowMap_TextureScale");
2057                 p->loc_SpecularPower              = qglGetUniformLocationARB(p->program, "SpecularPower");
2058                 p->loc_UserVec1                   = qglGetUniformLocationARB(p->program, "UserVec1");
2059                 p->loc_UserVec2                   = qglGetUniformLocationARB(p->program, "UserVec2");
2060                 p->loc_UserVec3                   = qglGetUniformLocationARB(p->program, "UserVec3");
2061                 p->loc_UserVec4                   = qglGetUniformLocationARB(p->program, "UserVec4");
2062                 p->loc_ViewTintColor              = qglGetUniformLocationARB(p->program, "ViewTintColor");
2063                 p->loc_ViewToLight                = qglGetUniformLocationARB(p->program, "ViewToLight");
2064                 p->loc_ModelToLight               = qglGetUniformLocationARB(p->program, "ModelToLight");
2065                 p->loc_TexMatrix                  = qglGetUniformLocationARB(p->program, "TexMatrix");
2066                 p->loc_BackgroundTexMatrix        = qglGetUniformLocationARB(p->program, "BackgroundTexMatrix");
2067                 p->loc_ModelViewMatrix            = qglGetUniformLocationARB(p->program, "ModelViewMatrix");
2068                 p->loc_ModelViewProjectionMatrix  = qglGetUniformLocationARB(p->program, "ModelViewProjectionMatrix");
2069                 // initialize the samplers to refer to the texture units we use
2070                 if (p->loc_Texture_First           >= 0) qglUniform1iARB(p->loc_Texture_First          , GL20TU_FIRST);
2071                 if (p->loc_Texture_Second          >= 0) qglUniform1iARB(p->loc_Texture_Second         , GL20TU_SECOND);
2072                 if (p->loc_Texture_GammaRamps      >= 0) qglUniform1iARB(p->loc_Texture_GammaRamps     , GL20TU_GAMMARAMPS);
2073                 if (p->loc_Texture_Normal          >= 0) qglUniform1iARB(p->loc_Texture_Normal         , GL20TU_NORMAL);
2074                 if (p->loc_Texture_Color           >= 0) qglUniform1iARB(p->loc_Texture_Color          , GL20TU_COLOR);
2075                 if (p->loc_Texture_Gloss           >= 0) qglUniform1iARB(p->loc_Texture_Gloss          , GL20TU_GLOSS);
2076                 if (p->loc_Texture_Glow            >= 0) qglUniform1iARB(p->loc_Texture_Glow           , GL20TU_GLOW);
2077                 if (p->loc_Texture_SecondaryNormal >= 0) qglUniform1iARB(p->loc_Texture_SecondaryNormal, GL20TU_SECONDARY_NORMAL);
2078                 if (p->loc_Texture_SecondaryColor  >= 0) qglUniform1iARB(p->loc_Texture_SecondaryColor , GL20TU_SECONDARY_COLOR);
2079                 if (p->loc_Texture_SecondaryGloss  >= 0) qglUniform1iARB(p->loc_Texture_SecondaryGloss , GL20TU_SECONDARY_GLOSS);
2080                 if (p->loc_Texture_SecondaryGlow   >= 0) qglUniform1iARB(p->loc_Texture_SecondaryGlow  , GL20TU_SECONDARY_GLOW);
2081                 if (p->loc_Texture_Pants           >= 0) qglUniform1iARB(p->loc_Texture_Pants          , GL20TU_PANTS);
2082                 if (p->loc_Texture_Shirt           >= 0) qglUniform1iARB(p->loc_Texture_Shirt          , GL20TU_SHIRT);
2083                 if (p->loc_Texture_FogMask         >= 0) qglUniform1iARB(p->loc_Texture_FogMask        , GL20TU_FOGMASK);
2084                 if (p->loc_Texture_Lightmap        >= 0) qglUniform1iARB(p->loc_Texture_Lightmap       , GL20TU_LIGHTMAP);
2085                 if (p->loc_Texture_Deluxemap       >= 0) qglUniform1iARB(p->loc_Texture_Deluxemap      , GL20TU_DELUXEMAP);
2086                 if (p->loc_Texture_Attenuation     >= 0) qglUniform1iARB(p->loc_Texture_Attenuation    , GL20TU_ATTENUATION);
2087                 if (p->loc_Texture_Cube            >= 0) qglUniform1iARB(p->loc_Texture_Cube           , GL20TU_CUBE);
2088                 if (p->loc_Texture_Refraction      >= 0) qglUniform1iARB(p->loc_Texture_Refraction     , GL20TU_REFRACTION);
2089                 if (p->loc_Texture_Reflection      >= 0) qglUniform1iARB(p->loc_Texture_Reflection     , GL20TU_REFLECTION);
2090                 if (p->loc_Texture_ShadowMapRect   >= 0) qglUniform1iARB(p->loc_Texture_ShadowMapRect  , GL20TU_SHADOWMAPRECT);
2091                 if (p->loc_Texture_ShadowMapCube   >= 0) qglUniform1iARB(p->loc_Texture_ShadowMapCube  , GL20TU_SHADOWMAPCUBE);
2092                 if (p->loc_Texture_ShadowMap2D     >= 0) qglUniform1iARB(p->loc_Texture_ShadowMap2D    , GL20TU_SHADOWMAP2D);
2093                 if (p->loc_Texture_CubeProjection  >= 0) qglUniform1iARB(p->loc_Texture_CubeProjection , GL20TU_CUBEPROJECTION);
2094                 if (p->loc_Texture_ScreenDepth     >= 0) qglUniform1iARB(p->loc_Texture_ScreenDepth    , GL20TU_SCREENDEPTH);
2095                 if (p->loc_Texture_ScreenNormalMap >= 0) qglUniform1iARB(p->loc_Texture_ScreenNormalMap, GL20TU_SCREENNORMALMAP);
2096                 if (p->loc_Texture_ScreenDiffuse   >= 0) qglUniform1iARB(p->loc_Texture_ScreenDiffuse  , GL20TU_SCREENDIFFUSE);
2097                 if (p->loc_Texture_ScreenSpecular  >= 0) qglUniform1iARB(p->loc_Texture_ScreenSpecular , GL20TU_SCREENSPECULAR);
2098                 CHECKGLERROR
2099                 if (developer.integer)
2100                         Con_Printf("^5GLSL shader %s compiled.\n", permutationname);
2101         }
2102         else
2103                 Con_Printf("^1GLSL shader %s failed!  some features may not work properly.\n", permutationname);
2104
2105         // free the strings
2106         if (vertexstring)
2107                 Mem_Free(vertexstring);
2108         if (geometrystring)
2109                 Mem_Free(geometrystring);
2110         if (fragmentstring)
2111                 Mem_Free(fragmentstring);
2112 }
2113
2114 void R_SetupShader_SetPermutationGLSL(unsigned int mode, unsigned int permutation)
2115 {
2116         r_glsl_permutation_t *perm = R_GLSL_FindPermutation(mode, permutation);
2117         if (r_glsl_permutation != perm)
2118         {
2119                 r_glsl_permutation = perm;
2120                 if (!r_glsl_permutation->program)
2121                 {
2122                         if (!r_glsl_permutation->compiled)
2123                                 R_GLSL_CompilePermutation(perm, mode, permutation);
2124                         if (!r_glsl_permutation->program)
2125                         {
2126                                 // remove features until we find a valid permutation
2127                                 int i;
2128                                 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
2129                                 {
2130                                         // reduce i more quickly whenever it would not remove any bits
2131                                         int j = 1<<(SHADERPERMUTATION_COUNT-1-i);
2132                                         if (!(permutation & j))
2133                                                 continue;
2134                                         permutation -= j;
2135                                         r_glsl_permutation = R_GLSL_FindPermutation(mode, permutation);
2136                                         if (!r_glsl_permutation->compiled)
2137                                                 R_GLSL_CompilePermutation(perm, mode, permutation);
2138                                         if (r_glsl_permutation->program)
2139                                                 break;
2140                                 }
2141                                 if (i >= SHADERPERMUTATION_COUNT)
2142                                 {
2143                                         //Con_Printf("Could not find a working OpenGL 2.0 shader for permutation %s %s\n", shadermodeinfo[mode].vertexfilename, shadermodeinfo[mode].pretext);
2144                                         r_glsl_permutation = R_GLSL_FindPermutation(mode, permutation);
2145                                         qglUseProgramObjectARB(0);CHECKGLERROR
2146                                         return; // no bit left to clear, entire mode is broken
2147                                 }
2148                         }
2149                 }
2150                 CHECKGLERROR
2151                 qglUseProgramObjectARB(r_glsl_permutation->program);CHECKGLERROR
2152         }
2153         if (r_glsl_permutation->loc_ModelViewProjectionMatrix >= 0) qglUniformMatrix4fvARB(r_glsl_permutation->loc_ModelViewProjectionMatrix, 1, false, gl_modelviewprojection16f);
2154 }
2155
2156 #ifdef SUPPORTCG
2157 #include <Cg/cgGL.h>
2158 struct r_cg_permutation_s;
2159 typedef struct r_cg_permutation_s
2160 {
2161         /// hash lookup data
2162         struct r_cg_permutation_s *hashnext;
2163         unsigned int mode;
2164         unsigned int permutation;
2165
2166         /// indicates if we have tried compiling this permutation already
2167         qboolean compiled;
2168         /// 0 if compilation failed
2169         CGprogram vprogram;
2170         CGprogram fprogram;
2171         /// locations of detected parameters in programs, or NULL if not found
2172         CGparameter vp_EyePosition;
2173         CGparameter vp_FogPlane;
2174         CGparameter vp_LightDir;
2175         CGparameter vp_LightPosition;
2176         CGparameter vp_ModelToLight;
2177         CGparameter vp_TexMatrix;
2178         CGparameter vp_BackgroundTexMatrix;
2179         CGparameter vp_ModelViewProjectionMatrix;
2180         CGparameter vp_ModelViewMatrix;
2181
2182         CGparameter fp_Texture_First;
2183         CGparameter fp_Texture_Second;
2184         CGparameter fp_Texture_GammaRamps;
2185         CGparameter fp_Texture_Normal;
2186         CGparameter fp_Texture_Color;
2187         CGparameter fp_Texture_Gloss;
2188         CGparameter fp_Texture_Glow;
2189         CGparameter fp_Texture_SecondaryNormal;
2190         CGparameter fp_Texture_SecondaryColor;
2191         CGparameter fp_Texture_SecondaryGloss;
2192         CGparameter fp_Texture_SecondaryGlow;
2193         CGparameter fp_Texture_Pants;
2194         CGparameter fp_Texture_Shirt;
2195         CGparameter fp_Texture_FogMask;
2196         CGparameter fp_Texture_Lightmap;
2197         CGparameter fp_Texture_Deluxemap;
2198         CGparameter fp_Texture_Attenuation;
2199         CGparameter fp_Texture_Cube;
2200         CGparameter fp_Texture_Refraction;
2201         CGparameter fp_Texture_Reflection;
2202         CGparameter fp_Texture_ShadowMapRect;
2203         CGparameter fp_Texture_ShadowMapCube;
2204         CGparameter fp_Texture_ShadowMap2D;
2205         CGparameter fp_Texture_CubeProjection;
2206         CGparameter fp_Texture_ScreenDepth;
2207         CGparameter fp_Texture_ScreenNormalMap;
2208         CGparameter fp_Texture_ScreenDiffuse;
2209         CGparameter fp_Texture_ScreenSpecular;
2210         CGparameter fp_Alpha;
2211         CGparameter fp_BloomBlur_Parameters;
2212         CGparameter fp_ClientTime;
2213         CGparameter fp_Color_Ambient;
2214         CGparameter fp_Color_Diffuse;
2215         CGparameter fp_Color_Specular;
2216         CGparameter fp_Color_Glow;
2217         CGparameter fp_Color_Pants;
2218         CGparameter fp_Color_Shirt;
2219         CGparameter fp_DeferredColor_Ambient;
2220         CGparameter fp_DeferredColor_Diffuse;
2221         CGparameter fp_DeferredColor_Specular;
2222         CGparameter fp_DeferredMod_Diffuse;
2223         CGparameter fp_DeferredMod_Specular;
2224         CGparameter fp_DistortScaleRefractReflect;
2225         CGparameter fp_EyePosition;
2226         CGparameter fp_FogColor;
2227         CGparameter fp_FogHeightFade;
2228         CGparameter fp_FogPlane;
2229         CGparameter fp_FogPlaneViewDist;
2230         CGparameter fp_FogRangeRecip;
2231         CGparameter fp_LightColor;
2232         CGparameter fp_LightDir;
2233         CGparameter fp_LightPosition;
2234         CGparameter fp_OffsetMapping_Scale;
2235         CGparameter fp_PixelSize;
2236         CGparameter fp_ReflectColor;
2237         CGparameter fp_ReflectFactor;
2238         CGparameter fp_ReflectOffset;
2239         CGparameter fp_RefractColor;
2240         CGparameter fp_Saturation;
2241         CGparameter fp_ScreenCenterRefractReflect;
2242         CGparameter fp_ScreenScaleRefractReflect;
2243         CGparameter fp_ScreenToDepth;
2244         CGparameter fp_ShadowMap_Parameters;
2245         CGparameter fp_ShadowMap_TextureScale;
2246         CGparameter fp_SpecularPower;
2247         CGparameter fp_UserVec1;
2248         CGparameter fp_UserVec2;
2249         CGparameter fp_UserVec3;
2250         CGparameter fp_UserVec4;
2251         CGparameter fp_ViewTintColor;
2252         CGparameter fp_ViewToLight;
2253 }
2254 r_cg_permutation_t;
2255
2256 /// information about each possible shader permutation
2257 r_cg_permutation_t *r_cg_permutationhash[SHADERMODE_COUNT][SHADERPERMUTATION_HASHSIZE];
2258 /// currently selected permutation
2259 r_cg_permutation_t *r_cg_permutation;
2260 /// storage for permutations linked in the hash table
2261 memexpandablearray_t r_cg_permutationarray;
2262
2263 static r_cg_permutation_t *R_CG_FindPermutation(unsigned int mode, unsigned int permutation)
2264 {
2265         //unsigned int hashdepth = 0;
2266         unsigned int hashindex = (permutation * 0x1021) & (SHADERPERMUTATION_HASHSIZE - 1);
2267         r_cg_permutation_t *p;
2268         for (p = r_cg_permutationhash[mode][hashindex];p;p = p->hashnext)
2269         {
2270                 if (p->mode == mode && p->permutation == permutation)
2271                 {
2272                         //if (hashdepth > 10)
2273                         //      Con_Printf("R_CG_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
2274                         return p;
2275                 }
2276                 //hashdepth++;
2277         }
2278         p = (r_cg_permutation_t*)Mem_ExpandableArray_AllocRecord(&r_cg_permutationarray);
2279         p->mode = mode;
2280         p->permutation = permutation;
2281         p->hashnext = r_cg_permutationhash[mode][hashindex];
2282         r_cg_permutationhash[mode][hashindex] = p;
2283         //if (hashdepth > 10)
2284         //      Con_Printf("R_CG_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
2285         return p;
2286 }
2287
2288 static char *R_CG_GetText(const char *filename, qboolean printfromdisknotice)
2289 {
2290         char *shaderstring;
2291         if (!filename || !filename[0])
2292                 return NULL;
2293         shaderstring = (char *)FS_LoadFile(filename, r_main_mempool, false, NULL);
2294         if (shaderstring)
2295         {
2296                 if (printfromdisknotice)
2297                         Con_DPrintf("from disk %s... ", filename);
2298                 return shaderstring;
2299         }
2300         else if (!strcmp(filename, "cg/default.cg"))
2301         {
2302                 shaderstring = (char *) Mem_Alloc(r_main_mempool, strlen(builtincgshaderstring) + 1);
2303                 memcpy(shaderstring, builtincgshaderstring, strlen(builtincgshaderstring) + 1);
2304         }
2305         return shaderstring;
2306 }
2307
2308 static void R_CG_CompilePermutation(r_cg_permutation_t *p, unsigned int mode, unsigned int permutation)
2309 {
2310         int i;
2311         shadermodeinfo_t *modeinfo = cgshadermodeinfo + mode;
2312         int vertstrings_count = 0, vertstring_length = 0;
2313         int geomstrings_count = 0, geomstring_length = 0;
2314         int fragstrings_count = 0, fragstring_length = 0;
2315         char *t;
2316         char *vertexstring, *geometrystring, *fragmentstring;
2317         char *vertstring, *geomstring, *fragstring;
2318         const char *vertstrings_list[32+3];
2319         const char *geomstrings_list[32+3];
2320         const char *fragstrings_list[32+3];
2321         char permutationname[256];
2322
2323         if (p->compiled)
2324                 return;
2325         p->compiled = true;
2326         p->vprogram = NULL;
2327         p->fprogram = NULL;
2328
2329         permutationname[0] = 0;
2330         vertexstring   = R_CG_GetText(modeinfo->vertexfilename, true);
2331         geometrystring = R_CG_GetText(modeinfo->geometryfilename, false);
2332         fragmentstring = R_CG_GetText(modeinfo->fragmentfilename, false);
2333
2334         strlcat(permutationname, modeinfo->vertexfilename, sizeof(permutationname));
2335
2336         // the first pretext is which type of shader to compile as
2337         // (later these will all be bound together as a program object)
2338         vertstrings_list[vertstrings_count++] = "#define VERTEX_SHADER\n";
2339         geomstrings_list[geomstrings_count++] = "#define GEOMETRY_SHADER\n";
2340         fragstrings_list[fragstrings_count++] = "#define FRAGMENT_SHADER\n";
2341
2342         // the second pretext is the mode (for example a light source)
2343         vertstrings_list[vertstrings_count++] = modeinfo->pretext;
2344         geomstrings_list[geomstrings_count++] = modeinfo->pretext;
2345         fragstrings_list[fragstrings_count++] = modeinfo->pretext;
2346         strlcat(permutationname, modeinfo->name, sizeof(permutationname));
2347
2348         // now add all the permutation pretexts
2349         for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
2350         {
2351                 if (permutation & (1<<i))
2352                 {
2353                         vertstrings_list[vertstrings_count++] = shaderpermutationinfo[i].pretext;
2354                         geomstrings_list[geomstrings_count++] = shaderpermutationinfo[i].pretext;
2355                         fragstrings_list[fragstrings_count++] = shaderpermutationinfo[i].pretext;
2356                         strlcat(permutationname, shaderpermutationinfo[i].name, sizeof(permutationname));
2357                 }
2358                 else
2359                 {
2360                         // keep line numbers correct
2361                         vertstrings_list[vertstrings_count++] = "\n";
2362                         geomstrings_list[geomstrings_count++] = "\n";
2363                         fragstrings_list[fragstrings_count++] = "\n";
2364                 }
2365         }
2366
2367         // now append the shader text itself
2368         vertstrings_list[vertstrings_count++] = vertexstring;
2369         geomstrings_list[geomstrings_count++] = geometrystring;
2370         fragstrings_list[fragstrings_count++] = fragmentstring;
2371
2372         // if any sources were NULL, clear the respective list
2373         if (!vertexstring)
2374                 vertstrings_count = 0;
2375         if (!geometrystring)
2376                 geomstrings_count = 0;
2377         if (!fragmentstring)
2378                 fragstrings_count = 0;
2379
2380         vertstring_length = 0;
2381         for (i = 0;i < vertstrings_count;i++)
2382                 vertstring_length += strlen(vertstrings_list[i]);
2383         vertstring = t = Mem_Alloc(tempmempool, vertstring_length + 1);
2384         for (i = 0;i < vertstrings_count;t += strlen(vertstrings_list[i]), i++)
2385                 memcpy(t, vertstrings_list[i], strlen(vertstrings_list[i]));
2386
2387         geomstring_length = 0;
2388         for (i = 0;i < geomstrings_count;i++)
2389                 geomstring_length += strlen(geomstrings_list[i]);
2390         geomstring = t = Mem_Alloc(tempmempool, geomstring_length + 1);
2391         for (i = 0;i < geomstrings_count;t += strlen(geomstrings_list[i]), i++)
2392                 memcpy(t, geomstrings_list[i], strlen(geomstrings_list[i]));
2393
2394         fragstring_length = 0;
2395         for (i = 0;i < fragstrings_count;i++)
2396                 fragstring_length += strlen(fragstrings_list[i]);
2397         fragstring = t = Mem_Alloc(tempmempool, fragstring_length + 1);
2398         for (i = 0;i < fragstrings_count;t += strlen(fragstrings_list[i]), i++)
2399                 memcpy(t, fragstrings_list[i], strlen(fragstrings_list[i]));
2400
2401         // compile the shader program
2402         if (vertstring[0] || geomstring[0] || fragstring[0])
2403         {
2404                 if (vertstring[0])
2405                         p->vprogram = cgCreateProgram(vid.cgcontext, CG_SOURCE, vertstring, CG_PROFILE_ARBVP1, NULL, NULL);
2406                 if (fragstring[0])
2407                         p->fprogram = cgCreateProgram(vid.cgcontext, CG_SOURCE, fragstring, CG_PROFILE_ARBFP1, NULL, NULL);
2408         }
2409
2410         if (p->vprogram)
2411         {
2412                 cgGLBindProgram(p->vprogram);
2413                 cgGLEnableProfile(cgGLGetLatestProfile(CG_GL_VERTEX));
2414         }
2415         else
2416                 cgGLDisableProfile(cgGLGetLatestProfile(CG_GL_VERTEX));
2417         if (p->fprogram)
2418         {
2419                 cgGLBindProgram(p->fprogram);
2420                 cgGLEnableProfile(cgGLGetLatestProfile(CG_GL_FRAGMENT));
2421         }
2422         else
2423                 cgGLDisableProfile(cgGLGetLatestProfile(CG_GL_FRAGMENT));
2424
2425         if (p->vprogram || p->fprogram)
2426         {
2427                 // look up all the uniform variable names we care about, so we don't
2428                 // have to look them up every time we set them
2429                 p->vp_EyePosition                = cgGetNamedParameter(p->vprogram, "EyePosition");
2430                 p->vp_FogPlane                   = cgGetNamedParameter(p->vprogram, "FogPlane");
2431                 p->vp_LightDir                   = cgGetNamedParameter(p->vprogram, "LightDir");
2432                 p->vp_LightPosition              = cgGetNamedParameter(p->vprogram, "LightPosition");
2433                 p->vp_ModelToLight               = cgGetNamedParameter(p->vprogram, "ModelToLight");
2434                 p->vp_TexMatrix                  = cgGetNamedParameter(p->vprogram, "TexMatrix");
2435                 p->vp_BackgroundTexMatrix        = cgGetNamedParameter(p->vprogram, "BackgroundTexMatrix");
2436                 p->vp_ModelViewProjectionMatrix  = cgGetNamedParameter(p->vprogram, "ModelViewProjectionMatrix");
2437                 p->vp_ModelViewMatrix            = cgGetNamedParameter(p->vprogram, "ModelViewMatrix");
2438
2439                 p->fp_Texture_First              = cgGetNamedParameter(p->fprogram, "Texture_First");
2440                 p->fp_Texture_Second             = cgGetNamedParameter(p->fprogram, "Texture_Second");
2441                 p->fp_Texture_GammaRamps         = cgGetNamedParameter(p->fprogram, "Texture_GammaRamps");
2442                 p->fp_Texture_Normal             = cgGetNamedParameter(p->fprogram, "Texture_Normal");
2443                 p->fp_Texture_Color              = cgGetNamedParameter(p->fprogram, "Texture_Color");
2444                 p->fp_Texture_Gloss              = cgGetNamedParameter(p->fprogram, "Texture_Gloss");
2445                 p->fp_Texture_Glow               = cgGetNamedParameter(p->fprogram, "Texture_Glow");
2446                 p->fp_Texture_SecondaryNormal    = cgGetNamedParameter(p->fprogram, "Texture_SecondaryNormal");
2447                 p->fp_Texture_SecondaryColor     = cgGetNamedParameter(p->fprogram, "Texture_SecondaryColor");
2448                 p->fp_Texture_SecondaryGloss     = cgGetNamedParameter(p->fprogram, "Texture_SecondaryGloss");
2449                 p->fp_Texture_SecondaryGlow      = cgGetNamedParameter(p->fprogram, "Texture_SecondaryGlow");
2450                 p->fp_Texture_Pants              = cgGetNamedParameter(p->fprogram, "Texture_Pants");
2451                 p->fp_Texture_Shirt              = cgGetNamedParameter(p->fprogram, "Texture_Shirt");
2452                 p->fp_Texture_FogMask            = cgGetNamedParameter(p->fprogram, "Texture_FogMask");
2453                 p->fp_Texture_Lightmap           = cgGetNamedParameter(p->fprogram, "Texture_Lightmap");
2454                 p->fp_Texture_Deluxemap          = cgGetNamedParameter(p->fprogram, "Texture_Deluxemap");
2455                 p->fp_Texture_Attenuation        = cgGetNamedParameter(p->fprogram, "Texture_Attenuation");
2456                 p->fp_Texture_Cube               = cgGetNamedParameter(p->fprogram, "Texture_Cube");
2457                 p->fp_Texture_Refraction         = cgGetNamedParameter(p->fprogram, "Texture_Refraction");
2458                 p->fp_Texture_Reflection         = cgGetNamedParameter(p->fprogram, "Texture_Reflection");
2459                 p->fp_Texture_ShadowMapRect      = cgGetNamedParameter(p->fprogram, "Texture_ShadowMapRect");
2460                 p->fp_Texture_ShadowMapCube      = cgGetNamedParameter(p->fprogram, "Texture_ShadowMapCube");
2461                 p->fp_Texture_ShadowMap2D        = cgGetNamedParameter(p->fprogram, "Texture_ShadowMap2D");
2462                 p->fp_Texture_CubeProjection     = cgGetNamedParameter(p->fprogram, "Texture_CubeProjection");
2463                 p->fp_Texture_ScreenDepth        = cgGetNamedParameter(p->fprogram, "Texture_ScreenDepth");
2464                 p->fp_Texture_ScreenNormalMap    = cgGetNamedParameter(p->fprogram, "Texture_ScreenNormalMap");
2465                 p->fp_Texture_ScreenDiffuse      = cgGetNamedParameter(p->fprogram, "Texture_ScreenDiffuse");
2466                 p->fp_Texture_ScreenSpecular     = cgGetNamedParameter(p->fprogram, "Texture_ScreenSpecular");
2467                 p->fp_Alpha                      = cgGetNamedParameter(p->fprogram, "Alpha");
2468                 p->fp_BloomBlur_Parameters       = cgGetNamedParameter(p->fprogram, "BloomBlur_Parameters");
2469                 p->fp_ClientTime                 = cgGetNamedParameter(p->fprogram, "ClientTime");
2470                 p->fp_Color_Ambient              = cgGetNamedParameter(p->fprogram, "Color_Ambient");
2471                 p->fp_Color_Diffuse              = cgGetNamedParameter(p->fprogram, "Color_Diffuse");
2472                 p->fp_Color_Specular             = cgGetNamedParameter(p->fprogram, "Color_Specular");
2473                 p->fp_Color_Glow                 = cgGetNamedParameter(p->fprogram, "Color_Glow");
2474                 p->fp_Color_Pants                = cgGetNamedParameter(p->fprogram, "Color_Pants");
2475                 p->fp_Color_Shirt                = cgGetNamedParameter(p->fprogram, "Color_Shirt");
2476                 p->fp_DeferredColor_Ambient      = cgGetNamedParameter(p->fprogram, "DeferredColor_Ambient");
2477                 p->fp_DeferredColor_Diffuse      = cgGetNamedParameter(p->fprogram, "DeferredColor_Diffuse");
2478                 p->fp_DeferredColor_Specular     = cgGetNamedParameter(p->fprogram, "DeferredColor_Specular");
2479                 p->fp_DeferredMod_Diffuse        = cgGetNamedParameter(p->fprogram, "DeferredMod_Diffuse");
2480                 p->fp_DeferredMod_Specular       = cgGetNamedParameter(p->fprogram, "DeferredMod_Specular");
2481                 p->fp_DistortScaleRefractReflect = cgGetNamedParameter(p->fprogram, "DistortScaleRefractReflect");
2482                 p->fp_EyePosition                = cgGetNamedParameter(p->fprogram, "EyePosition");
2483                 p->fp_FogColor                   = cgGetNamedParameter(p->fprogram, "FogColor");
2484                 p->fp_FogHeightFade              = cgGetNamedParameter(p->fprogram, "FogHeightFade");
2485                 p->fp_FogPlane                   = cgGetNamedParameter(p->fprogram, "FogPlane");
2486                 p->fp_FogPlaneViewDist           = cgGetNamedParameter(p->fprogram, "FogPlaneViewDist");
2487                 p->fp_FogRangeRecip              = cgGetNamedParameter(p->fprogram, "FogRangeRecip");
2488                 p->fp_LightColor                 = cgGetNamedParameter(p->fprogram, "LightColor");
2489                 p->fp_LightDir                   = cgGetNamedParameter(p->fprogram, "LightDir");
2490                 p->fp_LightPosition              = cgGetNamedParameter(p->fprogram, "LightPosition");
2491                 p->fp_OffsetMapping_Scale        = cgGetNamedParameter(p->fprogram, "OffsetMapping_Scale");
2492                 p->fp_PixelSize                  = cgGetNamedParameter(p->fprogram, "PixelSize");
2493                 p->fp_ReflectColor               = cgGetNamedParameter(p->fprogram, "ReflectColor");
2494                 p->fp_ReflectFactor              = cgGetNamedParameter(p->fprogram, "ReflectFactor");
2495                 p->fp_ReflectOffset              = cgGetNamedParameter(p->fprogram, "ReflectOffset");
2496                 p->fp_RefractColor               = cgGetNamedParameter(p->fprogram, "RefractColor");
2497                 p->fp_Saturation                 = cgGetNamedParameter(p->fprogram, "Saturation");
2498                 p->fp_ScreenCenterRefractReflect = cgGetNamedParameter(p->fprogram, "ScreenCenterRefractReflect");
2499                 p->fp_ScreenScaleRefractReflect  = cgGetNamedParameter(p->fprogram, "ScreenScaleRefractReflect");
2500                 p->fp_ScreenToDepth              = cgGetNamedParameter(p->fprogram, "ScreenToDepth");
2501                 p->fp_ShadowMap_Parameters       = cgGetNamedParameter(p->fprogram, "ShadowMap_Parameters");
2502                 p->fp_ShadowMap_TextureScale     = cgGetNamedParameter(p->fprogram, "ShadowMap_TextureScale");
2503                 p->fp_SpecularPower              = cgGetNamedParameter(p->fprogram, "SpecularPower");
2504                 p->fp_UserVec1                   = cgGetNamedParameter(p->fprogram, "UserVec1");
2505                 p->fp_UserVec2                   = cgGetNamedParameter(p->fprogram, "UserVec2");
2506                 p->fp_UserVec3                   = cgGetNamedParameter(p->fprogram, "UserVec3");
2507                 p->fp_UserVec4                   = cgGetNamedParameter(p->fprogram, "UserVec4");
2508                 p->fp_ViewTintColor              = cgGetNamedParameter(p->fprogram, "ViewTintColor");
2509                 p->fp_ViewToLight                = cgGetNamedParameter(p->fprogram, "ViewToLight");
2510                 CHECKGLERROR
2511                 if (developer.integer)
2512                         Con_Printf("^5CG shader %s compiled.\n", permutationname);
2513         }
2514         else
2515                 Con_Printf("^1CG shader %s failed!  some features may not work properly.\n", permutationname);
2516
2517         // free the strings
2518         if (vertstring)
2519                 Mem_Free(vertstring);
2520         if (geomstring)
2521                 Mem_Free(geomstring);
2522         if (fragstring)
2523                 Mem_Free(fragstring);
2524         if (vertexstring)
2525                 Mem_Free(vertexstring);
2526         if (geometrystring)
2527                 Mem_Free(geometrystring);
2528         if (fragmentstring)
2529                 Mem_Free(fragmentstring);
2530 }
2531
2532 void R_SetupShader_SetPermutationCG(unsigned int mode, unsigned int permutation)
2533 {
2534         r_cg_permutation_t *perm = R_CG_FindPermutation(mode, permutation);
2535         if (r_cg_permutation != perm)
2536         {
2537                 r_cg_permutation = perm;
2538                 cgGLUnbindProgram(cgGLGetLatestProfile(CG_GL_VERTEX));
2539                 cgGLUnbindProgram(cgGLGetLatestProfile(CG_GL_FRAGMENT));
2540                 if (!r_cg_permutation->vprogram && !r_cg_permutation->fprogram)
2541                 {
2542                         cgGLDisableProfile(cgGLGetLatestProfile(CG_GL_VERTEX));
2543                         cgGLDisableProfile(cgGLGetLatestProfile(CG_GL_FRAGMENT));
2544                         if (!r_cg_permutation->compiled)
2545                                 R_CG_CompilePermutation(perm, mode, permutation);
2546                         if (!r_cg_permutation->vprogram && !r_cg_permutation->fprogram)
2547                         {
2548                                 // remove features until we find a valid permutation
2549                                 int i;
2550                                 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
2551                                 {
2552                                         // reduce i more quickly whenever it would not remove any bits
2553                                         int j = 1<<(SHADERPERMUTATION_COUNT-1-i);
2554                                         if (!(permutation & j))
2555                                                 continue;
2556                                         permutation -= j;
2557                                         r_cg_permutation = R_CG_FindPermutation(mode, permutation);
2558                                         if (!r_cg_permutation->compiled)
2559                                                 R_CG_CompilePermutation(perm, mode, permutation);
2560                                         if (r_cg_permutation->vprogram || r_cg_permutation->fprogram)
2561                                                 break;
2562                                 }
2563                                 if (i >= SHADERPERMUTATION_COUNT)
2564                                 {
2565                                         //Con_Printf("Could not find a working Cg shader for permutation %s %s\n", shadermodeinfo[mode].vertexfilename, shadermodeinfo[mode].pretext);
2566                                         r_cg_permutation = R_CG_FindPermutation(mode, permutation);
2567                                         return; // no bit left to clear, entire mode is broken
2568                                 }
2569                         }
2570                 }
2571                 CHECKGLERROR
2572                 if (r_cg_permutation->vprogram)
2573                 {
2574                         cgGLBindProgram(r_cg_permutation->vprogram);
2575                         cgGLEnableProfile(cgGLGetLatestProfile(CG_GL_VERTEX));
2576                 }
2577                 else
2578                         cgGLDisableProfile(cgGLGetLatestProfile(CG_GL_VERTEX));
2579                 if (r_cg_permutation->fprogram)
2580                 {
2581                         cgGLBindProgram(r_cg_permutation->fprogram);
2582                         cgGLEnableProfile(cgGLGetLatestProfile(CG_GL_FRAGMENT));
2583                 }
2584                 else
2585                         cgGLDisableProfile(cgGLGetLatestProfile(CG_GL_FRAGMENT));
2586         }
2587         if (r_cg_permutation->vp_ModelViewProjectionMatrix) cgGLSetMatrixParameterfc(r_cg_permutation->vp_ModelViewProjectionMatrix, gl_modelviewprojection16f);
2588 }
2589 #endif
2590
2591 void R_GLSL_Restart_f(void)
2592 {
2593         unsigned int i, limit;
2594         r_glsl_permutation_t *p;
2595         limit = Mem_ExpandableArray_IndexRange(&r_glsl_permutationarray);
2596         for (i = 0;i < limit;i++)
2597         {
2598                 if ((p = (r_glsl_permutation_t*)Mem_ExpandableArray_RecordAtIndex(&r_glsl_permutationarray, i)))
2599                 {
2600                         GL_Backend_FreeProgram(p->program);
2601                         Mem_ExpandableArray_FreeRecord(&r_glsl_permutationarray, (void*)p);
2602                 }
2603         }
2604         memset(r_glsl_permutationhash, 0, sizeof(r_glsl_permutationhash));
2605 #ifdef SUPPORTCG
2606         if (vid.cgcontext)
2607         {
2608                 r_cg_permutation_t *p;
2609                 limit = Mem_ExpandableArray_IndexRange(&r_cg_permutationarray);
2610                 for (i = 0;i < limit;i++)
2611                 {
2612                         if ((p = (r_cg_permutation_t*)Mem_ExpandableArray_RecordAtIndex(&r_cg_permutationarray, i)))
2613                         {
2614                                 if (p->vprogram)
2615                                         cgDestroyProgram(p->vprogram);
2616                                 if (p->fprogram)
2617                                         cgDestroyProgram(p->fprogram);
2618                                 Mem_ExpandableArray_FreeRecord(&r_cg_permutationarray, (void*)p);
2619                         }
2620                 }
2621                 memset(r_cg_permutationhash, 0, sizeof(r_cg_permutationhash));
2622         }
2623 #endif
2624 }
2625
2626 void R_GLSL_DumpShader_f(void)
2627 {
2628         int i;
2629         qfile_t *file;
2630
2631         file = FS_OpenRealFile("glsl/default.glsl", "w", false);
2632         if (file)
2633         {
2634                 FS_Print(file, "/* The engine may define the following macros:\n");
2635                 FS_Print(file, "#define VERTEX_SHADER\n#define GEOMETRY_SHADER\n#define FRAGMENT_SHADER\n");
2636                 for (i = 0;i < SHADERMODE_COUNT;i++)
2637                         FS_Print(file, glslshadermodeinfo[i].pretext);
2638                 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
2639                         FS_Print(file, shaderpermutationinfo[i].pretext);
2640                 FS_Print(file, "*/\n");
2641                 FS_Print(file, builtinshaderstring);
2642                 FS_Close(file);
2643                 Con_Printf("glsl/default.glsl written\n");
2644         }
2645         else
2646                 Con_Printf("failed to write to glsl/default.glsl\n");
2647
2648 #ifdef SUPPORTCG
2649         file = FS_OpenRealFile("cg/default.cg", "w", false);
2650         if (file)
2651         {
2652                 FS_Print(file, "/* The engine may define the following macros:\n");
2653                 FS_Print(file, "#define VERTEX_SHADER\n#define GEOMETRY_SHADER\n#define FRAGMENT_SHADER\n");
2654                 for (i = 0;i < SHADERMODE_COUNT;i++)
2655                         FS_Print(file, cgshadermodeinfo[i].pretext);
2656                 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
2657                         FS_Print(file, shaderpermutationinfo[i].pretext);
2658                 FS_Print(file, "*/\n");
2659                 FS_Print(file, builtincgshaderstring);
2660                 FS_Close(file);
2661                 Con_Printf("cg/default.cg written\n");
2662         }
2663         else
2664                 Con_Printf("failed to write to cg/default.cg\n");
2665 #endif
2666 }
2667
2668 void R_SetupShader_Generic(rtexture_t *first, rtexture_t *second, int texturemode, int rgbscale)
2669 {
2670         if (!second)
2671                 texturemode = GL_MODULATE;
2672         switch (vid.renderpath)
2673         {
2674         case RENDERPATH_GL20:
2675                 R_SetupShader_SetPermutationGLSL(SHADERMODE_GENERIC, (first ? SHADERPERMUTATION_DIFFUSE : 0) | (second ? SHADERPERMUTATION_SPECULAR : 0) | (r_shadow_glossexact.integer ? SHADERPERMUTATION_EXACTSPECULARMATH : 0) | (texturemode == GL_MODULATE ? SHADERPERMUTATION_COLORMAPPING : (texturemode == GL_ADD ? SHADERPERMUTATION_GLOW : (texturemode == GL_DECAL ? SHADERPERMUTATION_VERTEXTEXTUREBLEND : 0))));
2676                 if (r_glsl_permutation->loc_Texture_First ) R_Mesh_TexBind(GL20TU_FIRST , R_GetTexture(first ));
2677                 if (r_glsl_permutation->loc_Texture_Second) R_Mesh_TexBind(GL20TU_SECOND, R_GetTexture(second));
2678                 break;
2679         case RENDERPATH_CGGL:
2680 #ifdef SUPPORTCG
2681                 R_SetupShader_SetPermutationCG(SHADERMODE_GENERIC, (first ? SHADERPERMUTATION_DIFFUSE : 0) | (second ? SHADERPERMUTATION_SPECULAR : 0) | (r_shadow_glossexact.integer ? SHADERPERMUTATION_EXACTSPECULARMATH : 0) | (texturemode == GL_MODULATE ? SHADERPERMUTATION_COLORMAPPING : (texturemode == GL_ADD ? SHADERPERMUTATION_GLOW : (texturemode == GL_DECAL ? SHADERPERMUTATION_VERTEXTEXTUREBLEND : 0))));
2682                 if (r_cg_permutation->fp_Texture_First ) cgGLSetTextureParameter(r_cg_permutation->fp_Texture_First , R_GetTexture(first ));
2683                 if (r_cg_permutation->fp_Texture_Second) cgGLSetTextureParameter(r_cg_permutation->fp_Texture_Second, R_GetTexture(second));
2684 #endif
2685                 break;
2686         case RENDERPATH_GL13:
2687                 R_Mesh_TexBind(0, R_GetTexture(first ));
2688                 R_Mesh_TexCombine(0, GL_MODULATE, GL_MODULATE, 1, 1);
2689                 R_Mesh_TexBind(1, R_GetTexture(second));
2690                 if (second)
2691                         R_Mesh_TexCombine(1, texturemode, texturemode, rgbscale, 1);
2692                 break;
2693         case RENDERPATH_GL11:
2694                 R_Mesh_TexBind(0, R_GetTexture(first ));
2695                 break;
2696         }
2697 }
2698
2699 void R_SetupShader_DepthOrShadow(void)
2700 {
2701         switch (vid.renderpath)
2702         {
2703         case RENDERPATH_GL20:
2704                 R_SetupShader_SetPermutationGLSL(SHADERMODE_DEPTH_OR_SHADOW, 0);
2705                 break;
2706         case RENDERPATH_CGGL:
2707 #ifdef SUPPORTCG
2708                 R_SetupShader_SetPermutationCG(SHADERMODE_DEPTH_OR_SHADOW, 0);
2709 #endif
2710                 break;
2711         case RENDERPATH_GL13:
2712                 R_Mesh_TexBind(0, 0);
2713                 R_Mesh_TexBind(1, 0);
2714                 break;
2715         case RENDERPATH_GL11:
2716                 R_Mesh_TexBind(0, 0);
2717                 break;
2718         }
2719 }
2720
2721 void R_SetupShader_ShowDepth(void)
2722 {
2723         switch (vid.renderpath)
2724         {
2725         case RENDERPATH_GL20:
2726                 R_SetupShader_SetPermutationGLSL(SHADERMODE_SHOWDEPTH, 0);
2727                 break;
2728         case RENDERPATH_CGGL:
2729 #ifdef SUPPORTCG
2730                 R_SetupShader_SetPermutationCG(SHADERMODE_SHOWDEPTH, 0);
2731 #endif
2732                 break;
2733         case RENDERPATH_GL13:
2734                 break;
2735         case RENDERPATH_GL11:
2736                 break;
2737         }
2738 }
2739
2740 extern qboolean r_shadow_usingdeferredprepass;
2741 extern cvar_t r_shadow_deferred_8bitrange;
2742 extern rtexture_t *r_shadow_attenuationgradienttexture;
2743 extern rtexture_t *r_shadow_attenuation2dtexture;
2744 extern rtexture_t *r_shadow_attenuation3dtexture;
2745 extern qboolean r_shadow_usingshadowmaprect;
2746 extern qboolean r_shadow_usingshadowmapcube;
2747 extern qboolean r_shadow_usingshadowmap2d;
2748 extern float r_shadow_shadowmap_texturescale[2];
2749 extern float r_shadow_shadowmap_parameters[4];
2750 extern qboolean r_shadow_shadowmapvsdct;
2751 extern qboolean r_shadow_shadowmapsampler;
2752 extern int r_shadow_shadowmappcf;
2753 extern rtexture_t *r_shadow_shadowmaprectangletexture;
2754 extern rtexture_t *r_shadow_shadowmap2dtexture;
2755 extern rtexture_t *r_shadow_shadowmapcubetexture[R_SHADOW_SHADOWMAP_NUMCUBEMAPS];
2756 extern rtexture_t *r_shadow_shadowmapvsdcttexture;
2757 extern int r_shadow_shadowmaplod; // changes for each light based on distance
2758 extern int r_shadow_prepass_width;
2759 extern int r_shadow_prepass_height;
2760 extern rtexture_t *r_shadow_prepassgeometrydepthtexture;
2761 extern rtexture_t *r_shadow_prepassgeometrynormalmaptexture;
2762 extern rtexture_t *r_shadow_prepasslightingdiffusetexture;
2763 extern rtexture_t *r_shadow_prepasslightingspeculartexture;
2764 void R_SetupShader_Surface(const vec3_t lightcolorbase, qboolean modellighting, float ambientscale, float diffusescale, float specularscale, rsurfacepass_t rsurfacepass)
2765 {
2766         // select a permutation of the lighting shader appropriate to this
2767         // combination of texture, entity, light source, and fogging, only use the
2768         // minimum features necessary to avoid wasting rendering time in the
2769         // fragment shader on features that are not being used
2770         unsigned int permutation = 0;
2771         unsigned int mode = 0;
2772         float m16f[16];
2773         // TODO: implement geometry-shader based shadow volumes someday
2774         if (r_glsl_offsetmapping.integer)
2775         {
2776                 permutation |= SHADERPERMUTATION_OFFSETMAPPING;
2777                 if (r_glsl_offsetmapping_reliefmapping.integer)
2778                         permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;
2779         }
2780         if (rsurfacepass == RSURFPASS_BACKGROUND)
2781         {
2782                 // distorted background
2783                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERSHADER)
2784                         mode = SHADERMODE_WATER;
2785                 else
2786                         mode = SHADERMODE_REFRACTION;
2787         }
2788         else if (rsurfacepass == RSURFPASS_DEFERREDGEOMETRY)
2789         {
2790                 // normalmap (deferred prepass), may use alpha test on diffuse
2791                 mode = SHADERMODE_DEFERREDGEOMETRY;
2792                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2793                         permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2794                 if (r_glsl_offsetmapping.integer)
2795                 {
2796                         permutation |= SHADERPERMUTATION_OFFSETMAPPING;
2797                         if (r_glsl_offsetmapping_reliefmapping.integer)
2798                                 permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;
2799                 }
2800         }
2801         else if (rsurfacepass == RSURFPASS_RTLIGHT)
2802         {
2803                 // light source
2804                 mode = SHADERMODE_LIGHTSOURCE;
2805                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2806                         permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2807                 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2808                         permutation |= SHADERPERMUTATION_CUBEFILTER;
2809                 if (diffusescale > 0)
2810                         permutation |= SHADERPERMUTATION_DIFFUSE;
2811                 if (specularscale > 0)
2812                         permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
2813                 if (r_refdef.fogenabled)
2814                         permutation |= r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE;
2815                 if (rsurface.texture->colormapping)
2816                         permutation |= SHADERPERMUTATION_COLORMAPPING;
2817                 if (r_shadow_usingshadowmaprect || r_shadow_usingshadowmap2d || r_shadow_usingshadowmapcube)
2818                 {
2819                         if (r_shadow_usingshadowmaprect)
2820                                 permutation |= SHADERPERMUTATION_SHADOWMAPRECT;
2821                         if (r_shadow_usingshadowmap2d)
2822                                 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
2823                         if (r_shadow_usingshadowmapcube)
2824                                 permutation |= SHADERPERMUTATION_SHADOWMAPCUBE;
2825                         else if(r_shadow_shadowmapvsdct)
2826                                 permutation |= SHADERPERMUTATION_SHADOWMAPVSDCT;
2827
2828                         if (r_shadow_shadowmapsampler)
2829                                 permutation |= SHADERPERMUTATION_SHADOWSAMPLER;
2830                         if (r_shadow_shadowmappcf > 1)
2831                                 permutation |= SHADERPERMUTATION_SHADOWMAPPCF2;
2832                         else if (r_shadow_shadowmappcf)
2833                                 permutation |= SHADERPERMUTATION_SHADOWMAPPCF;
2834                 }
2835         }
2836         else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
2837         {
2838                 // unshaded geometry (fullbright or ambient model lighting)
2839                 mode = SHADERMODE_FLATCOLOR;
2840                 ambientscale = diffusescale = specularscale = 0;
2841                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2842                         permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2843                 if (rsurface.texture->glowtexture && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
2844                         permutation |= SHADERPERMUTATION_GLOW;
2845                 if (r_refdef.fogenabled)
2846                         permutation |= r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE;
2847                 if (rsurface.texture->colormapping)
2848                         permutation |= SHADERPERMUTATION_COLORMAPPING;
2849                 if (r_glsl_offsetmapping.integer)
2850                 {
2851                         permutation |= SHADERPERMUTATION_OFFSETMAPPING;
2852                         if (r_glsl_offsetmapping_reliefmapping.integer)
2853                                 permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;
2854                 }
2855                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
2856                         permutation |= SHADERPERMUTATION_REFLECTION;
2857         }
2858         else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT_DIRECTIONAL)
2859         {
2860                 // directional model lighting
2861                 mode = SHADERMODE_LIGHTDIRECTION;
2862                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2863                         permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2864                 if (rsurface.texture->glowtexture && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
2865                         permutation |= SHADERPERMUTATION_GLOW;
2866                 permutation |= SHADERPERMUTATION_DIFFUSE;
2867                 if (specularscale > 0)
2868                         permutation |= SHADERPERMUTATION_SPECULAR;
2869                 if (r_refdef.fogenabled)
2870                         permutation |= r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE;
2871                 if (rsurface.texture->colormapping)
2872                         permutation |= SHADERPERMUTATION_COLORMAPPING;
2873                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
2874                         permutation |= SHADERPERMUTATION_REFLECTION;
2875                 if (r_shadow_usingdeferredprepass && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED))
2876                         permutation |= SHADERPERMUTATION_DEFERREDLIGHTMAP;
2877         }
2878         else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
2879         {
2880                 // ambient model lighting
2881                 mode = SHADERMODE_LIGHTDIRECTION;
2882                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2883                         permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2884                 if (rsurface.texture->glowtexture && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
2885                         permutation |= SHADERPERMUTATION_GLOW;
2886                 if (r_refdef.fogenabled)
2887                         permutation |= r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE;
2888                 if (rsurface.texture->colormapping)
2889                         permutation |= SHADERPERMUTATION_COLORMAPPING;
2890                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
2891                         permutation |= SHADERPERMUTATION_REFLECTION;
2892                 if (r_shadow_usingdeferredprepass && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED))
2893                         permutation |= SHADERPERMUTATION_DEFERREDLIGHTMAP;
2894         }
2895         else
2896         {
2897                 // lightmapped wall
2898                 if (r_glsl_deluxemapping.integer >= 1 && rsurface.uselightmaptexture && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brushq3.deluxemapping)
2899                 {
2900                         // deluxemapping (light direction texture)
2901                         if (rsurface.uselightmaptexture && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brushq3.deluxemapping && r_refdef.scene.worldmodel->brushq3.deluxemapping_modelspace)
2902                                 mode = SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE;
2903                         else
2904                                 mode = SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
2905                         permutation |= SHADERPERMUTATION_DIFFUSE;
2906                         if (specularscale > 0)
2907                                 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
2908                 }
2909                 else if (r_glsl_deluxemapping.integer >= 2)
2910                 {
2911                         // fake deluxemapping (uniform light direction in tangentspace)
2912                         mode = SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
2913                         permutation |= SHADERPERMUTATION_DIFFUSE;
2914                         if (specularscale > 0)
2915                                 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
2916                 }
2917                 else if (rsurface.uselightmaptexture)
2918                 {
2919                         // ordinary lightmapping (q1bsp, q3bsp)
2920                         mode = SHADERMODE_LIGHTMAP;
2921                 }
2922                 else
2923                 {
2924                         // ordinary vertex coloring (q3bsp)
2925                         mode = SHADERMODE_VERTEXCOLOR;
2926                 }
2927                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2928                         permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2929                 if (rsurface.texture->glowtexture && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
2930                         permutation |= SHADERPERMUTATION_GLOW;
2931                 if (r_refdef.fogenabled)
2932                         permutation |= r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE;
2933                 if (rsurface.texture->colormapping)
2934                         permutation |= SHADERPERMUTATION_COLORMAPPING;
2935                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
2936                         permutation |= SHADERPERMUTATION_REFLECTION;
2937                 if (r_shadow_usingdeferredprepass && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED))
2938                         permutation |= SHADERPERMUTATION_DEFERREDLIGHTMAP;
2939         }
2940         if(permutation & SHADERPERMUTATION_SPECULAR)
2941                 if(r_shadow_glossexact.integer)
2942                         permutation |= SHADERPERMUTATION_EXACTSPECULARMATH;
2943         if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST) && r_shadow_usingdeferredprepass)
2944                 permutation |= SHADERPERMUTATION_ALPHAKILL;
2945         switch(vid.renderpath)
2946         {
2947         case RENDERPATH_GL20:
2948                 R_SetupShader_SetPermutationGLSL(mode, permutation);
2949                 if (mode == SHADERMODE_LIGHTSOURCE)
2950                 {
2951                         if (r_glsl_permutation->loc_ModelToLight >= 0) {Matrix4x4_ToArrayFloatGL(&rsurface.entitytolight, m16f);qglUniformMatrix4fvARB(r_glsl_permutation->loc_ModelToLight, 1, false, m16f);}
2952                         if (r_glsl_permutation->loc_LightPosition >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightPosition, rsurface.entitylightorigin[0], rsurface.entitylightorigin[1], rsurface.entitylightorigin[2]);
2953                         if (r_glsl_permutation->loc_LightColor >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightColor, lightcolorbase[0], lightcolorbase[1], lightcolorbase[2]);
2954                         if (r_glsl_permutation->loc_Color_Ambient >= 0) qglUniform3fARB(r_glsl_permutation->loc_Color_Ambient, rsurface.colormod[0] * ambientscale, rsurface.colormod[1] * ambientscale, rsurface.colormod[2] * ambientscale);
2955                         if (r_glsl_permutation->loc_Color_Diffuse >= 0) qglUniform3fARB(r_glsl_permutation->loc_Color_Diffuse, rsurface.colormod[0] * diffusescale, rsurface.colormod[1] * diffusescale, rsurface.colormod[2] * diffusescale);
2956                         if (r_glsl_permutation->loc_Color_Specular >= 0) qglUniform3fARB(r_glsl_permutation->loc_Color_Specular, specularscale, specularscale, specularscale);
2957         
2958                         // additive passes are only darkened by fog, not tinted
2959                         if (r_glsl_permutation->loc_FogColor >= 0)
2960                                 qglUniform3fARB(r_glsl_permutation->loc_FogColor, 0, 0, 0);
2961                         if (r_glsl_permutation->loc_ShadowMap_TextureScale >= 0) qglUniform2fARB(r_glsl_permutation->loc_ShadowMap_TextureScale, r_shadow_shadowmap_texturescale[0], r_shadow_shadowmap_texturescale[1]);
2962                         if (r_glsl_permutation->loc_ShadowMap_Parameters >= 0) qglUniform4fARB(r_glsl_permutation->loc_ShadowMap_Parameters, r_shadow_shadowmap_parameters[0], r_shadow_shadowmap_parameters[1], r_shadow_shadowmap_parameters[2], r_shadow_shadowmap_parameters[3]);
2963                         if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularPower, rsurface.texture->specularpower * ((permutation & SHADERPERMUTATION_EXACTSPECULARMATH) ? 0.25f : 1.0f));
2964                 }
2965                 else
2966                 {
2967                         if (mode == SHADERMODE_FLATCOLOR)
2968                         {
2969                                 if (r_glsl_permutation->loc_Color_Ambient >= 0) qglUniform3fARB(r_glsl_permutation->loc_Color_Ambient, rsurface.colormod[0], rsurface.colormod[1], rsurface.colormod[2]);
2970                         }
2971                         else if (mode == SHADERMODE_LIGHTDIRECTION)
2972                         {
2973                                 if (r_glsl_permutation->loc_Color_Ambient >= 0) qglUniform3fARB(r_glsl_permutation->loc_Color_Ambient, (r_refdef.scene.ambient + rsurface.modellight_ambient[0] * r_refdef.lightmapintensity) * rsurface.colormod[0], (r_refdef.scene.ambient + rsurface.modellight_ambient[1] * r_refdef.lightmapintensity) * rsurface.colormod[1], (r_refdef.scene.ambient + rsurface.modellight_ambient[2] * r_refdef.lightmapintensity) * rsurface.colormod[2]);
2974                                 if (r_glsl_permutation->loc_Color_Diffuse >= 0) qglUniform3fARB(r_glsl_permutation->loc_Color_Diffuse, r_refdef.lightmapintensity, r_refdef.lightmapintensity, r_refdef.lightmapintensity);
2975                                 if (r_glsl_permutation->loc_Color_Specular >= 0) qglUniform3fARB(r_glsl_permutation->loc_Color_Specular, r_refdef.lightmapintensity * specularscale, r_refdef.lightmapintensity * specularscale, r_refdef.lightmapintensity * specularscale);
2976                                 if (r_glsl_permutation->loc_DeferredMod_Diffuse >= 0) qglUniform3fARB(r_glsl_permutation->loc_DeferredMod_Diffuse, rsurface.colormod[0] * r_shadow_deferred_8bitrange.value, rsurface.colormod[1] * r_shadow_deferred_8bitrange.value, rsurface.colormod[2] * r_shadow_deferred_8bitrange.value);
2977                                 if (r_glsl_permutation->loc_DeferredMod_Specular >= 0) qglUniform3fARB(r_glsl_permutation->loc_DeferredMod_Specular, specularscale * r_shadow_deferred_8bitrange.value, specularscale * r_shadow_deferred_8bitrange.value, specularscale * r_shadow_deferred_8bitrange.value);
2978                                 if (r_glsl_permutation->loc_LightColor >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightColor, rsurface.modellight_diffuse[0], rsurface.modellight_diffuse[1], rsurface.modellight_diffuse[2]);
2979                                 if (r_glsl_permutation->loc_LightDir >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightDir, rsurface.modellight_lightdir[0], rsurface.modellight_lightdir[1], rsurface.modellight_lightdir[2]);
2980                         }
2981                         else
2982                         {
2983                                 if (r_glsl_permutation->loc_Color_Ambient >= 0) qglUniform3fARB(r_glsl_permutation->loc_Color_Ambient, r_refdef.scene.ambient * rsurface.colormod[0], r_refdef.scene.ambient * rsurface.colormod[1], r_refdef.scene.ambient * rsurface.colormod[2]);
2984                                 if (r_glsl_permutation->loc_Color_Diffuse >= 0) qglUniform3fARB(r_glsl_permutation->loc_Color_Diffuse, rsurface.texture->lightmapcolor[0], rsurface.texture->lightmapcolor[1], rsurface.texture->lightmapcolor[2]);
2985                                 if (r_glsl_permutation->loc_Color_Specular >= 0) qglUniform3fARB(r_glsl_permutation->loc_Color_Specular, r_refdef.lightmapintensity * specularscale, r_refdef.lightmapintensity * specularscale, r_refdef.lightmapintensity * specularscale);
2986                                 if (r_glsl_permutation->loc_DeferredMod_Diffuse >= 0) qglUniform3fARB(r_glsl_permutation->loc_DeferredMod_Diffuse, rsurface.colormod[0] * diffusescale * r_shadow_deferred_8bitrange.value, rsurface.colormod[1] * diffusescale * r_shadow_deferred_8bitrange.value, rsurface.colormod[2] * diffusescale * r_shadow_deferred_8bitrange.value);
2987                                 if (r_glsl_permutation->loc_DeferredMod_Specular >= 0) qglUniform3fARB(r_glsl_permutation->loc_DeferredMod_Specular, specularscale * r_shadow_deferred_8bitrange.value, specularscale * r_shadow_deferred_8bitrange.value, specularscale * r_shadow_deferred_8bitrange.value);
2988                         }
2989                         // additive passes are only darkened by fog, not tinted
2990                         if (r_glsl_permutation->loc_FogColor >= 0)
2991                         {
2992                                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ADD)
2993                                         qglUniform3fARB(r_glsl_permutation->loc_FogColor, 0, 0, 0);
2994                                 else
2995                                         qglUniform3fARB(r_glsl_permutation->loc_FogColor, r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2]);
2996                         }
2997                         if (r_glsl_permutation->loc_DistortScaleRefractReflect >= 0) qglUniform4fARB(r_glsl_permutation->loc_DistortScaleRefractReflect, r_water_refractdistort.value * rsurface.texture->refractfactor, r_water_refractdistort.value * rsurface.texture->refractfactor, r_water_reflectdistort.value * rsurface.texture->reflectfactor, r_water_reflectdistort.value * rsurface.texture->reflectfactor);
2998                         if (r_glsl_permutation->loc_ScreenScaleRefractReflect >= 0) qglUniform4fARB(r_glsl_permutation->loc_ScreenScaleRefractReflect, r_waterstate.screenscale[0], r_waterstate.screenscale[1], r_waterstate.screenscale[0], r_waterstate.screenscale[1]);
2999                         if (r_glsl_permutation->loc_ScreenCenterRefractReflect >= 0) qglUniform4fARB(r_glsl_permutation->loc_ScreenCenterRefractReflect, r_waterstate.screencenter[0], r_waterstate.screencenter[1], r_waterstate.screencenter[0], r_waterstate.screencenter[1]);
3000                         if (r_glsl_permutation->loc_RefractColor >= 0) qglUniform4fvARB(r_glsl_permutation->loc_RefractColor, 1, rsurface.texture->refractcolor4f);
3001                         if (r_glsl_permutation->loc_ReflectColor >= 0) qglUniform4fvARB(r_glsl_permutation->loc_ReflectColor, 1, rsurface.texture->reflectcolor4f);
3002                         if (r_glsl_permutation->loc_ReflectFactor >= 0) qglUniform1fARB(r_glsl_permutation->loc_ReflectFactor, rsurface.texture->reflectmax - rsurface.texture->reflectmin);
3003                         if (r_glsl_permutation->loc_ReflectOffset >= 0) qglUniform1fARB(r_glsl_permutation->loc_ReflectOffset, rsurface.texture->reflectmin);
3004                         if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularPower, rsurface.texture->specularpower * ((permutation & SHADERPERMUTATION_EXACTSPECULARMATH) ? 0.25f : 1.0f));
3005                 }
3006                 if (r_glsl_permutation->loc_ModelViewMatrix >= 0) qglUniformMatrix4fvARB(r_glsl_permutation->loc_ModelViewMatrix, 1, false, gl_modelview16f);
3007                 if (r_glsl_permutation->loc_TexMatrix >= 0) {Matrix4x4_ToArrayFloatGL(&rsurface.texture->currenttexmatrix, m16f);qglUniformMatrix4fvARB(r_glsl_permutation->loc_TexMatrix, 1, false, m16f);}
3008                 if (r_glsl_permutation->loc_BackgroundTexMatrix >= 0) {Matrix4x4_ToArrayFloatGL(&rsurface.texture->currentbackgroundtexmatrix, m16f);qglUniformMatrix4fvARB(r_glsl_permutation->loc_BackgroundTexMatrix, 1, false, m16f);}
3009                 if (r_glsl_permutation->loc_Color_Glow >= 0) qglUniform3fARB(r_glsl_permutation->loc_Color_Glow, rsurface.glowmod[0], rsurface.glowmod[1], rsurface.glowmod[2]);
3010                 if (r_glsl_permutation->loc_Alpha >= 0) qglUniform1fARB(r_glsl_permutation->loc_Alpha, rsurface.texture->lightmapcolor[3]);
3011                 if (r_glsl_permutation->loc_EyePosition >= 0) qglUniform3fARB(r_glsl_permutation->loc_EyePosition, rsurface.localvieworigin[0], rsurface.localvieworigin[1], rsurface.localvieworigin[2]);
3012                 if (r_glsl_permutation->loc_Color_Pants >= 0)
3013                 {
3014                         if (rsurface.texture->pantstexture)
3015                                 qglUniform3fARB(r_glsl_permutation->loc_Color_Pants, rsurface.colormap_pantscolor[0], rsurface.colormap_pantscolor[1], rsurface.colormap_pantscolor[2]);
3016                         else
3017                                 qglUniform3fARB(r_glsl_permutation->loc_Color_Pants, 0, 0, 0);
3018                 }
3019                 if (r_glsl_permutation->loc_Color_Shirt >= 0)
3020                 {
3021                         if (rsurface.texture->shirttexture)
3022                                 qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, rsurface.colormap_shirtcolor[0], rsurface.colormap_shirtcolor[1], rsurface.colormap_shirtcolor[2]);
3023                         else
3024                                 qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, 0, 0, 0);
3025                 }
3026                 if (r_glsl_permutation->loc_FogPlane >= 0) qglUniform4fARB(r_glsl_permutation->loc_FogPlane, rsurface.fogplane[0], rsurface.fogplane[1], rsurface.fogplane[2], rsurface.fogplane[3]);
3027                 if (r_glsl_permutation->loc_FogPlaneViewDist >= 0) qglUniform1fARB(r_glsl_permutation->loc_FogPlaneViewDist, rsurface.fogplaneviewdist);
3028                 if (r_glsl_permutation->loc_FogRangeRecip >= 0) qglUniform1fARB(r_glsl_permutation->loc_FogRangeRecip, rsurface.fograngerecip);
3029                 if (r_glsl_permutation->loc_FogHeightFade >= 0) qglUniform1fARB(r_glsl_permutation->loc_FogHeightFade, rsurface.fogheightfade);
3030                 if (r_glsl_permutation->loc_OffsetMapping_Scale >= 0) qglUniform1fARB(r_glsl_permutation->loc_OffsetMapping_Scale, r_glsl_offsetmapping_scale.value);
3031                 if (r_glsl_permutation->loc_ScreenToDepth >= 0) qglUniform2fARB(r_glsl_permutation->loc_ScreenToDepth, r_refdef.view.viewport.screentodepth[0], r_refdef.view.viewport.screentodepth[1]);
3032
3033         //      if (r_glsl_permutation->loc_Texture_First           >= 0) R_Mesh_TexBind(GL20TU_FIRST             ,          R_GetTexture(r_texture_white                                     ));
3034         //      if (r_glsl_permutation->loc_Texture_Second          >= 0) R_Mesh_TexBind(GL20TU_SECOND            ,          R_GetTexture(r_texture_white                                     ));
3035         //      if (r_glsl_permutation->loc_Texture_GammaRamps      >= 0) R_Mesh_TexBind(GL20TU_GAMMARAMPS        ,          R_GetTexture(r_texture_gammaramps                                ));
3036                 if (r_glsl_permutation->loc_Texture_Normal          >= 0) R_Mesh_TexBind(GL20TU_NORMAL            ,          R_GetTexture(rsurface.texture->nmaptexture                       ));
3037                 if (r_glsl_permutation->loc_Texture_Color           >= 0) R_Mesh_TexBind(GL20TU_COLOR             ,          R_GetTexture(rsurface.texture->basetexture                       ));
3038                 if (r_glsl_permutation->loc_Texture_Gloss           >= 0) R_Mesh_TexBind(GL20TU_GLOSS             ,          R_GetTexture(rsurface.texture->glosstexture                      ));
3039                 if (r_glsl_permutation->loc_Texture_Glow            >= 0) R_Mesh_TexBind(GL20TU_GLOW              ,          R_GetTexture(rsurface.texture->glowtexture                       ));
3040                 if (r_glsl_permutation->loc_Texture_SecondaryNormal >= 0) R_Mesh_TexBind(GL20TU_SECONDARY_NORMAL  ,          R_GetTexture(rsurface.texture->backgroundnmaptexture             ));
3041                 if (r_glsl_permutation->loc_Texture_SecondaryColor  >= 0) R_Mesh_TexBind(GL20TU_SECONDARY_COLOR   ,          R_GetTexture(rsurface.texture->backgroundbasetexture             ));
3042                 if (r_glsl_permutation->loc_Texture_SecondaryGloss  >= 0) R_Mesh_TexBind(GL20TU_SECONDARY_GLOSS   ,          R_GetTexture(rsurface.texture->backgroundglosstexture            ));
3043                 if (r_glsl_permutation->loc_Texture_SecondaryGlow   >= 0) R_Mesh_TexBind(GL20TU_SECONDARY_GLOW    ,          R_GetTexture(rsurface.texture->backgroundglowtexture             ));
3044                 if (r_glsl_permutation->loc_Texture_Pants           >= 0) R_Mesh_TexBind(GL20TU_PANTS             ,          R_GetTexture(rsurface.texture->pantstexture                      ));
3045                 if (r_glsl_permutation->loc_Texture_Shirt           >= 0) R_Mesh_TexBind(GL20TU_SHIRT             ,          R_GetTexture(rsurface.texture->shirttexture                      ));
3046                 if (r_glsl_permutation->loc_Texture_FogMask         >= 0) R_Mesh_TexBind(GL20TU_FOGMASK           ,          R_GetTexture(r_texture_fogattenuation                            ));
3047                 if (r_glsl_permutation->loc_Texture_Lightmap        >= 0) R_Mesh_TexBind(GL20TU_LIGHTMAP          ,          R_GetTexture(r_texture_white                                     ));
3048                 if (r_glsl_permutation->loc_Texture_Deluxemap       >= 0) R_Mesh_TexBind(GL20TU_LIGHTMAP          ,          R_GetTexture(r_texture_blanknormalmap                            ));
3049                 if (r_glsl_permutation->loc_Texture_Attenuation     >= 0) R_Mesh_TexBind(GL20TU_ATTENUATION       ,          R_GetTexture(r_shadow_attenuationgradienttexture                 ));
3050                 if (r_glsl_permutation->loc_Texture_Refraction      >= 0) R_Mesh_TexBind(GL20TU_REFRACTION        ,          R_GetTexture(r_texture_white                                     ));
3051                 if (r_glsl_permutation->loc_Texture_Reflection      >= 0) R_Mesh_TexBind(GL20TU_REFLECTION        ,          R_GetTexture(r_texture_white                                     ));
3052                 if (r_glsl_permutation->loc_Texture_ScreenDepth     >= 0) R_Mesh_TexBindAll(GL20TU_SCREENDEPTH    , 0, 0, 0, R_GetTexture(r_shadow_prepassgeometrydepthtexture                ));
3053                 if (r_glsl_permutation->loc_Texture_ScreenNormalMap >= 0) R_Mesh_TexBindAll(GL20TU_SCREENNORMALMAP, 0, 0, 0, R_GetTexture(r_shadow_prepassgeometrynormalmaptexture            ));
3054                 if (r_glsl_permutation->loc_Texture_ScreenDiffuse   >= 0) R_Mesh_TexBindAll(GL20TU_SCREENDIFFUSE  , 0, 0, 0, R_GetTexture(r_shadow_prepasslightingdiffusetexture              ));
3055                 if (r_glsl_permutation->loc_Texture_ScreenSpecular  >= 0) R_Mesh_TexBindAll(GL20TU_SCREENSPECULAR , 0, 0, 0, R_GetTexture(r_shadow_prepasslightingspeculartexture             ));
3056                 if (rsurface.rtlight)
3057                 {
3058                         if (r_glsl_permutation->loc_Texture_Cube            >= 0) R_Mesh_TexBindAll(GL20TU_CUBE           , 0, 0,    R_GetTexture(rsurface.rtlight->currentcubemap                    ), 0);
3059                         if (r_glsl_permutation->loc_Texture_ShadowMapRect   >= 0) R_Mesh_TexBindAll(GL20TU_SHADOWMAPRECT  , 0, 0, 0, R_GetTexture(r_shadow_shadowmaprectangletexture                  ));
3060                         if (r_shadow_usingshadowmapcube)
3061                                 if (r_glsl_permutation->loc_Texture_ShadowMapCube   >= 0) R_Mesh_TexBindAll(GL20TU_SHADOWMAPCUBE  , 0, 0,    R_GetTexture(r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod]), 0);
3062                         if (r_glsl_permutation->loc_Texture_ShadowMap2D     >= 0) R_Mesh_TexBind(GL20TU_SHADOWMAP2D       ,          R_GetTexture(r_shadow_shadowmap2dtexture                         ));
3063                         if (r_glsl_permutation->loc_Texture_CubeProjection  >= 0) R_Mesh_TexBindAll(GL20TU_CUBEPROJECTION , 0, 0,    R_GetTexture(r_shadow_shadowmapvsdcttexture                      ), 0);
3064                 }
3065                 CHECKGLERROR
3066                 break;
3067         case RENDERPATH_CGGL:
3068 #ifdef SUPPORTCG
3069                 R_SetupShader_SetPermutationGLSL(mode, permutation);
3070                 if (mode == SHADERMODE_LIGHTSOURCE)
3071                 {
3072                         if (r_cg_permutation->vp_ModelToLight >= 0) {Matrix4x4_ToArrayFloatGL(&rsurface.entitytolight, m16f);cgGLSetMatrixParameterfc(r_cg_permutation->vp_ModelToLight, m16f);}
3073                         if (r_cg_permutation->vp_LightPosition >= 0) cgGLSetParameter3f(r_cg_permutation->vp_LightPosition, rsurface.entitylightorigin[0], rsurface.entitylightorigin[1], rsurface.entitylightorigin[2]);
3074                 }
3075                 else
3076                 {
3077                         if (mode == SHADERMODE_LIGHTDIRECTION)
3078                         {
3079                                 if (r_cg_permutation->vp_LightDir >= 0) cgGLSetParameter3f(r_cg_permutation->vp_LightDir, rsurface.modellight_lightdir[0], rsurface.modellight_lightdir[1], rsurface.modellight_lightdir[2]);
3080                         }
3081                 }
3082                 if (r_cg_permutation->vp_ModelViewMatrix) cgGLSetMatrixParameterfc(r_cg_permutation->vp_ModelViewMatrix, gl_modelviewprojection16f);
3083                 if (r_cg_permutation->vp_TexMatrix >= 0) {Matrix4x4_ToArrayFloatGL(&rsurface.texture->currenttexmatrix, m16f);cgGLSetMatrixParameterfc(r_cg_permutation->vp_TexMatrix, m16f);}
3084                 if (r_cg_permutation->vp_BackgroundTexMatrix >= 0) {Matrix4x4_ToArrayFloatGL(&rsurface.texture->currentbackgroundtexmatrix, m16f);cgGLSetMatrixParameterfc(r_cg_permutation->vp_BackgroundTexMatrix, m16f);}
3085                 if (r_cg_permutation->vp_EyePosition >= 0) cgGLSetParameter3f(r_cg_permutation->vp_EyePosition, rsurface.localvieworigin[0], rsurface.localvieworigin[1], rsurface.localvieworigin[2]);
3086                 if (r_cg_permutation->vp_FogPlane >= 0) cgGLSetParameter4f(r_cg_permutation->vp_FogPlane, rsurface.fogplane[0], rsurface.fogplane[1], rsurface.fogplane[2], rsurface.fogplane[3]);
3087                 CHECKGLERROR
3088
3089                 if (mode == SHADERMODE_LIGHTSOURCE)
3090                 {
3091                         if (r_cg_permutation->fp_LightPosition >= 0) cgGLSetParameter3f(r_cg_permutation->fp_LightPosition, rsurface.entitylightorigin[0], rsurface.entitylightorigin[1], rsurface.entitylightorigin[2]);
3092                         if (r_cg_permutation->fp_LightColor >= 0) cgGLSetParameter3f(r_cg_permutation->fp_LightColor, lightcolorbase[0], lightcolorbase[1], lightcolorbase[2]);
3093                         if (r_cg_permutation->fp_Color_Ambient >= 0) cgGLSetParameter3f(r_cg_permutation->fp_Color_Ambient, rsurface.colormod[0] * ambientscale, rsurface.colormod[1] * ambientscale, rsurface.colormod[2] * ambientscale);
3094                         if (r_cg_permutation->fp_Color_Diffuse >= 0) cgGLSetParameter3f(r_cg_permutation->fp_Color_Diffuse, rsurface.colormod[0] * diffusescale, rsurface.colormod[1] * diffusescale, rsurface.colormod[2] * diffusescale);
3095                         if (r_cg_permutation->fp_Color_Specular >= 0) cgGLSetParameter3f(r_cg_permutation->fp_Color_Specular, specularscale, specularscale, specularscale);
3096
3097                         // additive passes are only darkened by fog, not tinted
3098                         if (r_cg_permutation->fp_FogColor >= 0)
3099                                 cgGLSetParameter3f(r_cg_permutation->fp_FogColor, 0, 0, 0);
3100                         if (r_cg_permutation->fp_ShadowMap_TextureScale >= 0) cgGLSetParameter2f(r_cg_permutation->fp_ShadowMap_TextureScale, r_shadow_shadowmap_texturescale[0], r_shadow_shadowmap_texturescale[1]);
3101                         if (r_cg_permutation->fp_ShadowMap_Parameters >= 0) cgGLSetParameter4f(r_cg_permutation->fp_ShadowMap_Parameters, r_shadow_shadowmap_parameters[0], r_shadow_shadowmap_parameters[1], r_shadow_shadowmap_parameters[2], r_shadow_shadowmap_parameters[3]);
3102                         if (r_cg_permutation->fp_SpecularPower >= 0) cgGLSetParameter1f(r_cg_permutation->fp_SpecularPower, rsurface.texture->specularpower * ((permutation & SHADERPERMUTATION_EXACTSPECULARMATH) ? 0.25f : 1.0f));
3103                 }
3104                 else
3105                 {
3106                         if (mode == SHADERMODE_FLATCOLOR)
3107                         {
3108                                 if (r_cg_permutation->fp_Color_Ambient >= 0) cgGLSetParameter3f(r_cg_permutation->fp_Color_Ambient, rsurface.colormod[0], rsurface.colormod[1], rsurface.colormod[2]);
3109                         }