make infobar height configurable
[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 #include "csprogs.h"
29 #include "cl_video.h"
30
31 #ifdef SUPPORTD3D
32 #include <d3d9.h>
33 extern LPDIRECT3DDEVICE9 vid_d3d9dev;
34 #endif
35
36 mempool_t *r_main_mempool;
37 rtexturepool_t *r_main_texturepool;
38
39 static int r_textureframe = 0; ///< used only by R_GetCurrentTexture
40
41 static qboolean r_loadnormalmap;
42 static qboolean r_loadgloss;
43 qboolean r_loadfog;
44 static qboolean r_loaddds;
45 static qboolean r_savedds;
46
47 //
48 // screen size info
49 //
50 r_refdef_t r_refdef;
51
52 cvar_t r_motionblur = {CVAR_SAVE, "r_motionblur", "0", "motionblur value scale - 0.5 recommended"};
53 cvar_t r_damageblur = {CVAR_SAVE, "r_damageblur", "0", "motionblur based on damage"};
54 cvar_t r_motionblur_vmin = {CVAR_SAVE, "r_motionblur_vmin", "300", "minimum influence from velocity"};
55 cvar_t r_motionblur_vmax = {CVAR_SAVE, "r_motionblur_vmax", "600", "maximum influence from velocity"};
56 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)"};
57 cvar_t r_motionblur_vcoeff = {CVAR_SAVE, "r_motionblur_vcoeff", "0.05", "sliding average reaction time for velocity"};
58 cvar_t r_motionblur_maxblur = {CVAR_SAVE, "r_motionblur_maxblur", "0.88", "cap for motionblur alpha value"};
59 cvar_t r_motionblur_randomize = {CVAR_SAVE, "r_motionblur_randomize", "0.1", "randomizing coefficient to workaround ghosting"};
60
61 // TODO do we want a r_equalize_entities cvar that works on all ents, or would that be a cheat?
62 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"};
63 cvar_t r_equalize_entities_minambient = {CVAR_SAVE, "r_equalize_entities_minambient", "0.5", "light equalizing: ensure at least this ambient/diffuse ratio"};
64 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)"};
65 cvar_t r_equalize_entities_to = {CVAR_SAVE, "r_equalize_entities_to", "0.8", "light equalizing: target light level"};
66
67 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"};
68 cvar_t r_useinfinitefarclip = {CVAR_SAVE, "r_useinfinitefarclip", "1", "enables use of a special kind of projection matrix that has an extremely large farclip"};
69 cvar_t r_farclip_base = {0, "r_farclip_base", "65536", "farclip (furthest visible distance) for rendering when r_useinfinitefarclip is 0"};
70 cvar_t r_farclip_world = {0, "r_farclip_world", "2", "adds map size to farclip multiplied by this value"};
71 cvar_t r_nearclip = {0, "r_nearclip", "1", "distance from camera of nearclip plane" };
72 cvar_t r_showbboxes = {0, "r_showbboxes", "0", "shows bounding boxes of server entities, value controls opacity scaling (1 = 10%,  10 = 100%)"};
73 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)"};
74 cvar_t r_showtris = {0, "r_showtris", "0", "shows triangle outlines, value controls brightness (can be above 1)"};
75 cvar_t r_shownormals = {0, "r_shownormals", "0", "shows per-vertex surface normals and tangent vectors for bumpmapped lighting"};
76 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"};
77 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"};
78 cvar_t r_showcollisionbrushes = {0, "r_showcollisionbrushes", "0", "draws collision brushes in quake3 maps (mode 1), mode 2 disables rendering of world (trippy!)"};
79 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"};
80 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"};
81 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"};
82 cvar_t r_drawportals = {0, "r_drawportals", "0", "shows portals (separating polygons) in world interior in quake1 maps"};
83 cvar_t r_drawentities = {0, "r_drawentities","1", "draw entities (doors, players, projectiles, etc)"};
84 cvar_t r_draw2d = {0, "r_draw2d","1", "draw 2D stuff (dangerous to turn off)"};
85 cvar_t r_drawworld = {0, "r_drawworld","1", "draw world (most static stuff)"};
86 cvar_t r_drawviewmodel = {0, "r_drawviewmodel","1", "draw your weapon model"};
87 cvar_t r_drawexteriormodel = {0, "r_drawexteriormodel","1", "draw your player model (e.g. in chase cam, reflections)"};
88 cvar_t r_cullentities_trace = {0, "r_cullentities_trace", "1", "probabistically cull invisible entities"};
89 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)"};
90 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)"};
91 cvar_t r_cullentities_trace_enlarge = {0, "r_cullentities_trace_enlarge", "0", "box enlargement for entity culling"};
92 cvar_t r_cullentities_trace_delay = {0, "r_cullentities_trace_delay", "1", "number of seconds until the entity gets actually culled"};
93 cvar_t r_speeds = {0, "r_speeds","0", "displays rendering statistics and per-subsystem timings"};
94 cvar_t r_fullbright = {0, "r_fullbright","0", "makes map very bright and renders faster"};
95
96 cvar_t r_fakelight = {0, "r_fakelight","0", "render 'fake' lighting instead of real lightmaps"};
97 cvar_t r_fakelight_intensity = {0, "r_fakelight_intensity","0.75", "fakelight intensity modifier"};
98 #define FAKELIGHT_ENABLED (r_fakelight.integer >= 2 || (r_fakelight.integer && r_refdef.scene.worldmodel && !r_refdef.scene.worldmodel->lit))
99
100 cvar_t r_wateralpha = {CVAR_SAVE, "r_wateralpha","1", "opacity of water polygons"};
101 cvar_t r_dynamic = {CVAR_SAVE, "r_dynamic","1", "enables dynamic lights (rocket glow and such)"};
102 cvar_t r_fullbrights = {CVAR_SAVE, "r_fullbrights", "1", "enables glowing pixels in quake textures (changes need r_restart to take effect)"};
103 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."};
104 cvar_t r_shadows_darken = {CVAR_SAVE, "r_shadows_darken", "0.5", "how much shadowed areas will be darkened"};
105 cvar_t r_shadows_throwdistance = {CVAR_SAVE, "r_shadows_throwdistance", "500", "how far to cast shadows from models"};
106 cvar_t r_shadows_throwdirection = {CVAR_SAVE, "r_shadows_throwdirection", "0 0 -1", "override throwing direction for r_shadows 2"};
107 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."};
108 cvar_t r_shadows_castfrombmodels = {CVAR_SAVE, "r_shadows_castfrombmodels", "0", "do cast shadows from bmodels"};
109 cvar_t r_shadows_focus = {CVAR_SAVE, "r_shadows_focus", "0 0 0", "offset the shadowed area focus"};
110 cvar_t r_shadows_shadowmapscale = {CVAR_SAVE, "r_shadows_shadowmapscale", "1", "increases shadowmap quality (multiply global shadowmap precision) for fake shadows. Needs shadowmapping ON."};
111 cvar_t r_q1bsp_skymasking = {0, "r_q1bsp_skymasking", "1", "allows sky polygons in quake1 maps to obscure other geometry"};
112 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"};
113 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"};
114 cvar_t r_polygonoffset_decals_factor = {0, "r_polygonoffset_decals_factor", "0", "biases depth values of decals to prevent z-fighting artifacts"};
115 cvar_t r_polygonoffset_decals_offset = {0, "r_polygonoffset_decals_offset", "-14", "biases depth values of decals to prevent z-fighting artifacts"};
116 cvar_t r_fog_exp2 = {0, "r_fog_exp2", "0", "uses GL_EXP2 fog (as in Nehahra) rather than realistic GL_EXP fog"};
117 cvar_t r_drawfog = {CVAR_SAVE, "r_drawfog", "1", "allows one to disable fog rendering"};
118 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"};
119
120 cvar_t gl_fogenable = {0, "gl_fogenable", "0", "nehahra fog enable (for Nehahra compatibility only)"};
121 cvar_t gl_fogdensity = {0, "gl_fogdensity", "0.25", "nehahra fog density (recommend values below 0.1) (for Nehahra compatibility only)"};
122 cvar_t gl_fogred = {0, "gl_fogred","0.3", "nehahra fog color red value (for Nehahra compatibility only)"};
123 cvar_t gl_foggreen = {0, "gl_foggreen","0.3", "nehahra fog color green value (for Nehahra compatibility only)"};
124 cvar_t gl_fogblue = {0, "gl_fogblue","0.3", "nehahra fog color blue value (for Nehahra compatibility only)"};
125 cvar_t gl_fogstart = {0, "gl_fogstart", "0", "nehahra fog start distance (for Nehahra compatibility only)"};
126 cvar_t gl_fogend = {0, "gl_fogend","0", "nehahra fog end distance (for Nehahra compatibility only)"};
127 cvar_t gl_skyclip = {0, "gl_skyclip", "4608", "nehahra farclip distance - the real fog end (for Nehahra compatibility only)"};
128
129 cvar_t r_texture_dds_load = {CVAR_SAVE, "r_texture_dds_load", "0", "load compressed dds/filename.dds texture instead of filename.tga, if the file exists (requires driver support)"};
130 cvar_t r_texture_dds_save = {CVAR_SAVE, "r_texture_dds_save", "0", "save compressed dds/filename.dds texture when filename.tga is loaded, so that it can be loaded instead next time"};
131
132 cvar_t r_texture_convertsRGB_2d = {0, "r_texture_convertsRGB_2d", "0", "load textures as sRGB and convert to linear for proper shading"};
133 cvar_t r_texture_convertsRGB_skin = {0, "r_texture_convertsRGB_skin", "0", "load textures as sRGB and convert to linear for proper shading"};
134 cvar_t r_texture_convertsRGB_cubemap = {0, "r_texture_convertsRGB_cubemap", "0", "load textures as sRGB and convert to linear for proper shading"};
135 cvar_t r_texture_convertsRGB_skybox = {0, "r_texture_convertsRGB_skybox", "0", "load textures as sRGB and convert to linear for proper shading"};
136 cvar_t r_texture_convertsRGB_particles = {0, "r_texture_convertsRGB_particles", "0", "load textures as sRGB and convert to linear for proper shading"};
137
138 cvar_t r_textureunits = {0, "r_textureunits", "32", "number of texture units to use in GL 1.1 and GL 1.3 rendering paths"};
139 static cvar_t gl_combine = {CVAR_READONLY, "gl_combine", "1", "indicates whether the OpenGL 1.3 rendering path is active"};
140 static cvar_t r_glsl = {CVAR_READONLY, "r_glsl", "1", "indicates whether the OpenGL 2.0 rendering path is active"};
141
142 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)"};
143 cvar_t r_glsl_offsetmapping = {CVAR_SAVE, "r_glsl_offsetmapping", "0", "offset mapping effect (also known as parallax mapping or virtual displacement mapping)"};
144 cvar_t r_glsl_offsetmapping_reliefmapping = {CVAR_SAVE, "r_glsl_offsetmapping_reliefmapping", "0", "relief mapping effect (higher quality)"};
145 cvar_t r_glsl_offsetmapping_scale = {CVAR_SAVE, "r_glsl_offsetmapping_scale", "0.04", "how deep the offset mapping effect is"};
146 cvar_t r_glsl_postprocess = {CVAR_SAVE, "r_glsl_postprocess", "0", "use a GLSL postprocessing shader"};
147 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)"};
148 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)"};
149 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)"};
150 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)"};
151 cvar_t r_glsl_postprocess_uservec1_enable = {CVAR_SAVE, "r_glsl_postprocess_uservec1_enable", "1", "enables postprocessing uservec1 usage, creates USERVEC1 define (only useful if default.glsl has been customized)"};
152 cvar_t r_glsl_postprocess_uservec2_enable = {CVAR_SAVE, "r_glsl_postprocess_uservec2_enable", "1", "enables postprocessing uservec2 usage, creates USERVEC1 define (only useful if default.glsl has been customized)"};
153 cvar_t r_glsl_postprocess_uservec3_enable = {CVAR_SAVE, "r_glsl_postprocess_uservec3_enable", "1", "enables postprocessing uservec3 usage, creates USERVEC1 define (only useful if default.glsl has been customized)"};
154 cvar_t r_glsl_postprocess_uservec4_enable = {CVAR_SAVE, "r_glsl_postprocess_uservec4_enable", "1", "enables postprocessing uservec4 usage, creates USERVEC1 define (only useful if default.glsl has been customized)"};
155
156 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)"};
157 cvar_t r_water_clippingplanebias = {CVAR_SAVE, "r_water_clippingplanebias", "1", "a rather technical setting which avoids black pixels around water edges"};
158 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"};
159 cvar_t r_water_refractdistort = {CVAR_SAVE, "r_water_refractdistort", "0.01", "how much water refractions shimmer"};
160 cvar_t r_water_reflectdistort = {CVAR_SAVE, "r_water_reflectdistort", "0.01", "how much water reflections shimmer"};
161 cvar_t r_water_scissormode = {0, "r_water_scissormode", "3", "scissor (1) or cull (2) or both (3) water renders"};
162
163 cvar_t r_lerpsprites = {CVAR_SAVE, "r_lerpsprites", "0", "enables animation smoothing on sprites"};
164 cvar_t r_lerpmodels = {CVAR_SAVE, "r_lerpmodels", "1", "enables animation smoothing on models"};
165 cvar_t r_lerplightstyles = {CVAR_SAVE, "r_lerplightstyles", "0", "enable animation smoothing on flickering lights"};
166 cvar_t r_waterscroll = {CVAR_SAVE, "r_waterscroll", "1", "makes water scroll around, value controls how much"};
167
168 cvar_t r_bloom = {CVAR_SAVE, "r_bloom", "0", "enables bloom effect (makes bright pixels affect neighboring pixels)"};
169 cvar_t r_bloom_colorscale = {CVAR_SAVE, "r_bloom_colorscale", "1", "how bright the glow is"};
170 cvar_t r_bloom_brighten = {CVAR_SAVE, "r_bloom_brighten", "2", "how bright the glow is, after subtract/power"};
171 cvar_t r_bloom_blur = {CVAR_SAVE, "r_bloom_blur", "4", "how large the glow is"};
172 cvar_t r_bloom_resolution = {CVAR_SAVE, "r_bloom_resolution", "320", "what resolution to perform the bloom effect at (independent of screen resolution)"};
173 cvar_t r_bloom_colorexponent = {CVAR_SAVE, "r_bloom_colorexponent", "1", "how exagerated the glow is"};
174 cvar_t r_bloom_colorsubtract = {CVAR_SAVE, "r_bloom_colorsubtract", "0.125", "reduces bloom colors by a certain amount"};
175
176 cvar_t r_hdr = {CVAR_SAVE, "r_hdr", "0", "enables High Dynamic Range bloom effect (higher quality version of r_bloom)"};
177 cvar_t r_hdr_scenebrightness = {CVAR_SAVE, "r_hdr_scenebrightness", "1", "global rendering brightness"};
178 cvar_t r_hdr_glowintensity = {CVAR_SAVE, "r_hdr_glowintensity", "1", "how bright light emitting textures should appear"};
179 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)"};
180
181 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"};
182
183 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"};
184
185 cvar_t gl_lightmaps = {0, "gl_lightmaps", "0", "draws only lightmaps, no texture (for level designers)"};
186
187 cvar_t r_test = {0, "r_test", "0", "internal development use only, leave it alone (usually does nothing anyway)"};
188 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"};
189 cvar_t r_track_sprites_flags = {CVAR_SAVE, "r_track_sprites_flags", "1", "1: Rotate sprites accodringly, 2: Make it a continuous rotation"};
190 cvar_t r_track_sprites_scalew = {CVAR_SAVE, "r_track_sprites_scalew", "1", "width scaling of tracked sprites"};
191 cvar_t r_track_sprites_scaleh = {CVAR_SAVE, "r_track_sprites_scaleh", "1", "height scaling of tracked sprites"};
192 cvar_t r_overheadsprites_perspective = {CVAR_SAVE, "r_overheadsprites_perspective", "5", "fake perspective effect for SPR_OVERHEAD sprites"};
193 cvar_t r_overheadsprites_pushback = {CVAR_SAVE, "r_overheadsprites_pushback", "15", "how far to pull the SPR_OVERHEAD sprites toward the eye (used to avoid intersections with 3D models)"};
194 cvar_t r_overheadsprites_scalex = {CVAR_SAVE, "r_overheadsprites_scalex", "1", "additional scale for overhead sprites for x axis"};
195 cvar_t r_overheadsprites_scaley = {CVAR_SAVE, "r_overheadsprites_scaley", "1", "additional scale for overhead sprites for y axis"};
196
197 cvar_t r_glsl_saturation = {CVAR_SAVE, "r_glsl_saturation", "1", "saturation multiplier (only working in glsl!)"};
198 cvar_t r_glsl_saturation_redcompensate = {CVAR_SAVE, "r_glsl_saturation_redcompensate", "0", "a 'vampire sight' addition to desaturation effect, does compensation for red color, r_glsl_restart is required"};
199
200 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)"};
201
202 extern cvar_t v_glslgamma;
203
204 extern qboolean v_flipped_state;
205
206 static struct r_bloomstate_s
207 {
208         qboolean enabled;
209         qboolean hdr;
210
211         int bloomwidth, bloomheight;
212
213         int screentexturewidth, screentextureheight;
214         rtexture_t *texture_screen; /// \note also used for motion blur if enabled!
215
216         int bloomtexturewidth, bloomtextureheight;
217         rtexture_t *texture_bloom;
218
219         // arrays for rendering the screen passes
220         float screentexcoord2f[8];
221         float bloomtexcoord2f[8];
222         float offsettexcoord2f[8];
223
224         r_viewport_t viewport;
225 }
226 r_bloomstate;
227
228 r_waterstate_t r_waterstate;
229
230 /// shadow volume bsp struct with automatically growing nodes buffer
231 svbsp_t r_svbsp;
232
233 rtexture_t *r_texture_blanknormalmap;
234 rtexture_t *r_texture_white;
235 rtexture_t *r_texture_grey128;
236 rtexture_t *r_texture_black;
237 rtexture_t *r_texture_notexture;
238 rtexture_t *r_texture_whitecube;
239 rtexture_t *r_texture_normalizationcube;
240 rtexture_t *r_texture_fogattenuation;
241 rtexture_t *r_texture_fogheighttexture;
242 rtexture_t *r_texture_gammaramps;
243 unsigned int r_texture_gammaramps_serial;
244 //rtexture_t *r_texture_fogintensity;
245 rtexture_t *r_texture_reflectcube;
246
247 // TODO: hash lookups?
248 typedef struct cubemapinfo_s
249 {
250         char basename[64];
251         rtexture_t *texture;
252 }
253 cubemapinfo_t;
254
255 int r_texture_numcubemaps;
256 cubemapinfo_t r_texture_cubemaps[MAX_CUBEMAPS];
257
258 unsigned int r_queries[MAX_OCCLUSION_QUERIES];
259 unsigned int r_numqueries;
260 unsigned int r_maxqueries;
261
262 typedef struct r_qwskincache_s
263 {
264         char name[MAX_QPATH];
265         skinframe_t *skinframe;
266 }
267 r_qwskincache_t;
268
269 static r_qwskincache_t *r_qwskincache;
270 static int r_qwskincache_size;
271
272 /// vertex coordinates for a quad that covers the screen exactly
273 extern const float r_screenvertex3f[12];
274 extern const float r_d3dscreenvertex3f[12];
275 const float r_screenvertex3f[12] =
276 {
277         0, 0, 0,
278         1, 0, 0,
279         1, 1, 0,
280         0, 1, 0
281 };
282 const float r_d3dscreenvertex3f[12] =
283 {
284         0, 1, 0,
285         1, 1, 0,
286         1, 0, 0,
287         0, 0, 0
288 };
289
290 void R_ModulateColors(float *in, float *out, int verts, float r, float g, float b)
291 {
292         int i;
293         for (i = 0;i < verts;i++)
294         {
295                 out[0] = in[0] * r;
296                 out[1] = in[1] * g;
297                 out[2] = in[2] * b;
298                 out[3] = in[3];
299                 in += 4;
300                 out += 4;
301         }
302 }
303
304 void R_FillColors(float *out, int verts, float r, float g, float b, float a)
305 {
306         int i;
307         for (i = 0;i < verts;i++)
308         {
309                 out[0] = r;
310                 out[1] = g;
311                 out[2] = b;
312                 out[3] = a;
313                 out += 4;
314         }
315 }
316
317 // FIXME: move this to client?
318 void FOG_clear(void)
319 {
320         if (gamemode == GAME_NEHAHRA)
321         {
322                 Cvar_Set("gl_fogenable", "0");
323                 Cvar_Set("gl_fogdensity", "0.2");
324                 Cvar_Set("gl_fogred", "0.3");
325                 Cvar_Set("gl_foggreen", "0.3");
326                 Cvar_Set("gl_fogblue", "0.3");
327         }
328         r_refdef.fog_density = 0;
329         r_refdef.fog_red = 0;
330         r_refdef.fog_green = 0;
331         r_refdef.fog_blue = 0;
332         r_refdef.fog_alpha = 1;
333         r_refdef.fog_start = 0;
334         r_refdef.fog_end = 16384;
335         r_refdef.fog_height = 1<<30;
336         r_refdef.fog_fadedepth = 128;
337         memset(r_refdef.fog_height_texturename, 0, sizeof(r_refdef.fog_height_texturename));
338 }
339
340 static void R_BuildBlankTextures(void)
341 {
342         unsigned char data[4];
343         data[2] = 128; // normal X
344         data[1] = 128; // normal Y
345         data[0] = 255; // normal Z
346         data[3] = 128; // height
347         r_texture_blanknormalmap = R_LoadTexture2D(r_main_texturepool, "blankbump", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
348         data[0] = 255;
349         data[1] = 255;
350         data[2] = 255;
351         data[3] = 255;
352         r_texture_white = R_LoadTexture2D(r_main_texturepool, "blankwhite", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
353         data[0] = 128;
354         data[1] = 128;
355         data[2] = 128;
356         data[3] = 255;
357         r_texture_grey128 = R_LoadTexture2D(r_main_texturepool, "blankgrey128", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
358         data[0] = 0;
359         data[1] = 0;
360         data[2] = 0;
361         data[3] = 255;
362         r_texture_black = R_LoadTexture2D(r_main_texturepool, "blankblack", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
363 }
364
365 static void R_BuildNoTexture(void)
366 {
367         int x, y;
368         unsigned char pix[16][16][4];
369         // this makes a light grey/dark grey checkerboard texture
370         for (y = 0;y < 16;y++)
371         {
372                 for (x = 0;x < 16;x++)
373                 {
374                         if ((y < 8) ^ (x < 8))
375                         {
376                                 pix[y][x][0] = 128;
377                                 pix[y][x][1] = 128;
378                                 pix[y][x][2] = 128;
379                                 pix[y][x][3] = 255;
380                         }
381                         else
382                         {
383                                 pix[y][x][0] = 64;
384                                 pix[y][x][1] = 64;
385                                 pix[y][x][2] = 64;
386                                 pix[y][x][3] = 255;
387                         }
388                 }
389         }
390         r_texture_notexture = R_LoadTexture2D(r_main_texturepool, "notexture", 16, 16, &pix[0][0][0], TEXTYPE_BGRA, TEXF_MIPMAP | TEXF_PERSISTENT, -1, NULL);
391 }
392
393 static void R_BuildWhiteCube(void)
394 {
395         unsigned char data[6*1*1*4];
396         memset(data, 255, sizeof(data));
397         r_texture_whitecube = R_LoadTextureCubeMap(r_main_texturepool, "whitecube", 1, data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_PERSISTENT, -1, NULL);
398 }
399
400 static void R_BuildNormalizationCube(void)
401 {
402         int x, y, side;
403         vec3_t v;
404         vec_t s, t, intensity;
405 #define NORMSIZE 64
406         unsigned char *data;
407         data = (unsigned char *)Mem_Alloc(tempmempool, 6*NORMSIZE*NORMSIZE*4);
408         for (side = 0;side < 6;side++)
409         {
410                 for (y = 0;y < NORMSIZE;y++)
411                 {
412                         for (x = 0;x < NORMSIZE;x++)
413                         {
414                                 s = (x + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
415                                 t = (y + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
416                                 switch(side)
417                                 {
418                                 default:
419                                 case 0:
420                                         v[0] = 1;
421                                         v[1] = -t;
422                                         v[2] = -s;
423                                         break;
424                                 case 1:
425                                         v[0] = -1;
426                                         v[1] = -t;
427                                         v[2] = s;
428                                         break;
429                                 case 2:
430                                         v[0] = s;
431                                         v[1] = 1;
432                                         v[2] = t;
433                                         break;
434                                 case 3:
435                                         v[0] = s;
436                                         v[1] = -1;
437                                         v[2] = -t;
438                                         break;
439                                 case 4:
440                                         v[0] = s;
441                                         v[1] = -t;
442                                         v[2] = 1;
443                                         break;
444                                 case 5:
445                                         v[0] = -s;
446                                         v[1] = -t;
447                                         v[2] = -1;
448                                         break;
449                                 }
450                                 intensity = 127.0f / sqrt(DotProduct(v, v));
451                                 data[((side*64+y)*64+x)*4+2] = (unsigned char)(128.0f + intensity * v[0]);
452                                 data[((side*64+y)*64+x)*4+1] = (unsigned char)(128.0f + intensity * v[1]);
453                                 data[((side*64+y)*64+x)*4+0] = (unsigned char)(128.0f + intensity * v[2]);
454                                 data[((side*64+y)*64+x)*4+3] = 255;
455                         }
456                 }
457         }
458         r_texture_normalizationcube = R_LoadTextureCubeMap(r_main_texturepool, "normalcube", NORMSIZE, data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_PERSISTENT, -1, NULL);
459         Mem_Free(data);
460 }
461
462 static void R_BuildFogTexture(void)
463 {
464         int x, b;
465 #define FOGWIDTH 256
466         unsigned char data1[FOGWIDTH][4];
467         //unsigned char data2[FOGWIDTH][4];
468         double d, r, alpha;
469
470         r_refdef.fogmasktable_start = r_refdef.fog_start;
471         r_refdef.fogmasktable_alpha = r_refdef.fog_alpha;
472         r_refdef.fogmasktable_range = r_refdef.fogrange;
473         r_refdef.fogmasktable_density = r_refdef.fog_density;
474
475         r = r_refdef.fogmasktable_range / FOGMASKTABLEWIDTH;
476         for (x = 0;x < FOGMASKTABLEWIDTH;x++)
477         {
478                 d = (x * r - r_refdef.fogmasktable_start);
479                 if(developer_extra.integer)
480                         Con_DPrintf("%f ", d);
481                 d = max(0, d);
482                 if (r_fog_exp2.integer)
483                         alpha = exp(-r_refdef.fogmasktable_density * r_refdef.fogmasktable_density * 0.0001 * d * d);
484                 else
485                         alpha = exp(-r_refdef.fogmasktable_density * 0.004 * d);
486                 if(developer_extra.integer)
487                         Con_DPrintf(" : %f ", alpha);
488                 alpha = 1 - (1 - alpha) * r_refdef.fogmasktable_alpha;
489                 if(developer_extra.integer)
490                         Con_DPrintf(" = %f\n", alpha);
491                 r_refdef.fogmasktable[x] = bound(0, alpha, 1);
492         }
493
494         for (x = 0;x < FOGWIDTH;x++)
495         {
496                 b = (int)(r_refdef.fogmasktable[x * (FOGMASKTABLEWIDTH - 1) / (FOGWIDTH - 1)] * 255);
497                 data1[x][0] = b;
498                 data1[x][1] = b;
499                 data1[x][2] = b;
500                 data1[x][3] = 255;
501                 //data2[x][0] = 255 - b;
502                 //data2[x][1] = 255 - b;
503                 //data2[x][2] = 255 - b;
504                 //data2[x][3] = 255;
505         }
506         if (r_texture_fogattenuation)
507         {
508                 R_UpdateTexture(r_texture_fogattenuation, &data1[0][0], 0, 0, FOGWIDTH, 1);
509                 //R_UpdateTexture(r_texture_fogattenuation, &data2[0][0], 0, 0, FOGWIDTH, 1);
510         }
511         else
512         {
513                 r_texture_fogattenuation = R_LoadTexture2D(r_main_texturepool, "fogattenuation", FOGWIDTH, 1, &data1[0][0], TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_PERSISTENT, -1, NULL);
514                 //r_texture_fogintensity = R_LoadTexture2D(r_main_texturepool, "fogintensity", FOGWIDTH, 1, &data2[0][0], TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP, NULL);
515         }
516 }
517
518 static void R_BuildFogHeightTexture(void)
519 {
520         unsigned char *inpixels;
521         int size;
522         int x;
523         int y;
524         int j;
525         float c[4];
526         float f;
527         inpixels = NULL;
528         strlcpy(r_refdef.fogheighttexturename, r_refdef.fog_height_texturename, sizeof(r_refdef.fogheighttexturename));
529         if (r_refdef.fogheighttexturename[0])
530                 inpixels = loadimagepixelsbgra(r_refdef.fogheighttexturename, true, false, false, NULL);
531         if (!inpixels)
532         {
533                 r_refdef.fog_height_tablesize = 0;
534                 if (r_texture_fogheighttexture)
535                         R_FreeTexture(r_texture_fogheighttexture);
536                 r_texture_fogheighttexture = NULL;
537                 if (r_refdef.fog_height_table2d)
538                         Mem_Free(r_refdef.fog_height_table2d);
539                 r_refdef.fog_height_table2d = NULL;
540                 if (r_refdef.fog_height_table1d)
541                         Mem_Free(r_refdef.fog_height_table1d);
542                 r_refdef.fog_height_table1d = NULL;
543                 return;
544         }
545         size = image_width;
546         r_refdef.fog_height_tablesize = size;
547         r_refdef.fog_height_table1d = (unsigned char *)Mem_Alloc(r_main_mempool, size * 4);
548         r_refdef.fog_height_table2d = (unsigned char *)Mem_Alloc(r_main_mempool, size * size * 4);
549         memcpy(r_refdef.fog_height_table1d, inpixels, size * 4);
550         Mem_Free(inpixels);
551         // LordHavoc: now the magic - what is that table2d for?  it is a cooked
552         // average fog color table accounting for every fog layer between a point
553         // and the camera.  (Note: attenuation is handled separately!)
554         for (y = 0;y < size;y++)
555         {
556                 for (x = 0;x < size;x++)
557                 {
558                         Vector4Clear(c);
559                         f = 0;
560                         if (x < y)
561                         {
562                                 for (j = x;j <= y;j++)
563                                 {
564                                         Vector4Add(c, r_refdef.fog_height_table1d + j*4, c);
565                                         f++;
566                                 }
567                         }
568                         else
569                         {
570                                 for (j = x;j >= y;j--)
571                                 {
572                                         Vector4Add(c, r_refdef.fog_height_table1d + j*4, c);
573                                         f++;
574                                 }
575                         }
576                         f = 1.0f / f;
577                         r_refdef.fog_height_table2d[(y*size+x)*4+0] = (unsigned char)(c[0] * f);
578                         r_refdef.fog_height_table2d[(y*size+x)*4+1] = (unsigned char)(c[1] * f);
579                         r_refdef.fog_height_table2d[(y*size+x)*4+2] = (unsigned char)(c[2] * f);
580                         r_refdef.fog_height_table2d[(y*size+x)*4+3] = (unsigned char)(c[3] * f);
581                 }
582         }
583         r_texture_fogheighttexture = R_LoadTexture2D(r_main_texturepool, "fogheighttable", size, size, r_refdef.fog_height_table2d, TEXTYPE_BGRA, TEXF_ALPHA | TEXF_CLAMP, -1, NULL);
584 }
585
586 //=======================================================================================================================================================
587
588 static const char *builtinshaderstring =
589 "// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader\n"
590 "// written by Forest 'LordHavoc' Hale\n"
591 "// shadowmapping enhancements by Lee 'eihrul' Salzman\n"
592 "\n"
593 "#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE)\n"
594 "# define USEFOG\n"
595 "#endif\n"
596 "#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)\n"
597 "#define USELIGHTMAP\n"
598 "#endif\n"
599 "#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT)\n"
600 "#define USEEYEVECTOR\n"
601 "#endif\n"
602 "\n"
603 "#ifdef USESHADOWMAP2D\n"
604 "# ifdef GL_EXT_gpu_shader4\n"
605 "#   extension GL_EXT_gpu_shader4 : enable\n"
606 "# endif\n"
607 "# ifdef GL_ARB_texture_gather\n"
608 "#   extension GL_ARB_texture_gather : enable\n"
609 "# else\n"
610 "#   ifdef GL_AMD_texture_texture4\n"
611 "#     extension GL_AMD_texture_texture4 : enable\n"
612 "#   endif\n"
613 "# endif\n"
614 "#endif\n"
615 "\n"
616 "//#ifdef USESHADOWSAMPLER\n"
617 "//# extension GL_ARB_shadow : enable\n"
618 "//#endif\n"
619 "\n"
620 "//#ifdef __GLSL_CG_DATA_TYPES\n"
621 "//# define myhalf half\n"
622 "//# define myhalf2 half2\n"
623 "//# define myhalf3 half3\n"
624 "//# define myhalf4 half4\n"
625 "//#else\n"
626 "# define myhalf float\n"
627 "# define myhalf2 vec2\n"
628 "# define myhalf3 vec3\n"
629 "# define myhalf4 vec4\n"
630 "//#endif\n"
631 "\n"
632 "#ifdef VERTEX_SHADER\n"
633 "uniform mat4 ModelViewProjectionMatrix;\n"
634 "#endif\n"
635 "\n"
636 "#ifdef MODE_DEPTH_OR_SHADOW\n"
637 "#ifdef VERTEX_SHADER\n"
638 "void main(void)\n"
639 "{\n"
640 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
641 "}\n"
642 "#endif\n"
643 "#else // !MODE_DEPTH_ORSHADOW\n"
644 "\n"
645 "\n"
646 "\n"
647 "\n"
648 "#ifdef MODE_SHOWDEPTH\n"
649 "#ifdef VERTEX_SHADER\n"
650 "void main(void)\n"
651 "{\n"
652 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
653 "       gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0);\n"
654 "}\n"
655 "#endif\n"
656 "\n"
657 "#ifdef FRAGMENT_SHADER\n"
658 "void main(void)\n"
659 "{\n"
660 "       gl_FragColor = gl_Color;\n"
661 "}\n"
662 "#endif\n"
663 "#else // !MODE_SHOWDEPTH\n"
664 "\n"
665 "\n"
666 "\n"
667 "\n"
668 "#ifdef MODE_POSTPROCESS\n"
669 "varying vec2 TexCoord1;\n"
670 "varying vec2 TexCoord2;\n"
671 "\n"
672 "#ifdef VERTEX_SHADER\n"
673 "void main(void)\n"
674 "{\n"
675 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
676 "       TexCoord1 = gl_MultiTexCoord0.xy;\n"
677 "#ifdef USEBLOOM\n"
678 "       TexCoord2 = gl_MultiTexCoord4.xy;\n"
679 "#endif\n"
680 "}\n"
681 "#endif\n"
682 "\n"
683 "#ifdef FRAGMENT_SHADER\n"
684 "uniform sampler2D Texture_First;\n"
685 "#ifdef USEBLOOM\n"
686 "uniform sampler2D Texture_Second;\n"
687 "uniform vec4 BloomColorSubtract;\n"
688 "#endif\n"
689 "#ifdef USEGAMMARAMPS\n"
690 "uniform sampler2D Texture_GammaRamps;\n"
691 "#endif\n"
692 "#ifdef USESATURATION\n"
693 "uniform float Saturation;\n"
694 "#endif\n"
695 "#ifdef USEVIEWTINT\n"
696 "uniform vec4 ViewTintColor;\n"
697 "#endif\n"
698 "//uncomment these if you want to use them:\n"
699 "uniform vec4 UserVec1;\n"
700 "uniform vec4 UserVec2;\n"
701 "// uniform vec4 UserVec3;\n"
702 "// uniform vec4 UserVec4;\n"
703 "// uniform float ClientTime;\n"
704 "uniform vec2 PixelSize;\n"
705 "void main(void)\n"
706 "{\n"
707 "       gl_FragColor = texture2D(Texture_First, TexCoord1);\n"
708 "#ifdef USEBLOOM\n"
709 "       gl_FragColor += max(vec4(0,0,0,0), texture2D(Texture_Second, TexCoord2) - BloomColorSubtract);\n"
710 "#endif\n"
711 "#ifdef USEVIEWTINT\n"
712 "       gl_FragColor = mix(gl_FragColor, ViewTintColor, ViewTintColor.a);\n"
713 "#endif\n"
714 "\n"
715 "#ifdef USEPOSTPROCESSING\n"
716 "// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want\n"
717 "// 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"
718 "       float sobel = 1.0;\n"
719 "       // vec2 ts = textureSize(Texture_First, 0);\n"
720 "       // vec2 px = vec2(1/ts.x, 1/ts.y);\n"
721 "       vec2 px = PixelSize;\n"
722 "       vec3 x1 = texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb;\n"
723 "       vec3 x2 = texture2D(Texture_First, TexCoord1 + vec2(-px.x,  0.0)).rgb;\n"
724 "       vec3 x3 = texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb;\n"
725 "       vec3 x4 = texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb;\n"
726 "       vec3 x5 = texture2D(Texture_First, TexCoord1 + vec2( px.x,  0.0)).rgb;\n"
727 "       vec3 x6 = texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb;\n"
728 "       vec3 y1 = texture2D(Texture_First, TexCoord1 + vec2( px.x,-px.y)).rgb;\n"
729 "       vec3 y2 = texture2D(Texture_First, TexCoord1 + vec2(  0.0,-px.y)).rgb;\n"
730 "       vec3 y3 = texture2D(Texture_First, TexCoord1 + vec2(-px.x,-px.y)).rgb;\n"
731 "       vec3 y4 = texture2D(Texture_First, TexCoord1 + vec2( px.x, px.y)).rgb;\n"
732 "       vec3 y5 = texture2D(Texture_First, TexCoord1 + vec2(  0.0, px.y)).rgb;\n"
733 "       vec3 y6 = texture2D(Texture_First, TexCoord1 + vec2(-px.x, px.y)).rgb;\n"
734 "       float px1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x1);\n"
735 "       float px2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), x2);\n"
736 "       float px3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), x3);\n"
737 "       float px4 =  1.0 * dot(vec3(0.3, 0.59, 0.11), x4);\n"
738 "       float px5 =  2.0 * dot(vec3(0.3, 0.59, 0.11), x5);\n"
739 "       float px6 =  1.0 * dot(vec3(0.3, 0.59, 0.11), x6);\n"
740 "       float py1 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y1);\n"
741 "       float py2 = -2.0 * dot(vec3(0.3, 0.59, 0.11), y2);\n"
742 "       float py3 = -1.0 * dot(vec3(0.3, 0.59, 0.11), y3);\n"
743 "       float py4 =  1.0 * dot(vec3(0.3, 0.59, 0.11), y4);\n"
744 "       float py5 =  2.0 * dot(vec3(0.3, 0.59, 0.11), y5);\n"
745 "       float py6 =  1.0 * dot(vec3(0.3, 0.59, 0.11), y6);\n"
746 "       sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6);\n"
747 "       gl_FragColor += texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y;\n"
748 "       gl_FragColor += texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y;\n"
749 "       gl_FragColor += texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y;\n"
750 "       gl_FragColor += texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107,  0.707107)) * UserVec1.y;\n"
751 "       gl_FragColor += texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990,  0.891007)) * UserVec1.y;\n"
752 "       gl_FragColor /= (1.0 + 5.0 * UserVec1.y);\n"
753 "       gl_FragColor.rgb = gl_FragColor.rgb * (1.0 + UserVec2.x) + vec3(max(0.0, sobel - UserVec2.z))*UserVec2.y;\n"
754 "#endif\n"
755 "\n"
756 "#ifdef USESATURATION\n"
757 "       //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter\n"
758 "       float y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114));\n"
759 "       // 'vampire sight' effect, wheres red is compensated\n"
760 "       #ifdef SATURATION_REDCOMPENSATE\n"
761 "               float rboost = max(0.0, (gl_FragColor.r - max(gl_FragColor.g, gl_FragColor.b))*(1.0 - Saturation));\n"
762 "               gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation);\n"
763 "               gl_FragColor.r += rboost;\n"
764 "       #else\n"
765 "               // normal desaturation\n"
766 "               //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation;\n"
767 "               gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation);\n"
768 "       #endif\n"
769 "#endif\n"
770 "\n"
771 "#ifdef USEGAMMARAMPS\n"
772 "       gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r;\n"
773 "       gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g;\n"
774 "       gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b;\n"
775 "#endif\n"
776 "}\n"
777 "#endif\n"
778 "#else // !MODE_POSTPROCESS\n"
779 "\n"
780 "\n"
781 "\n"
782 "\n"
783 "#ifdef MODE_GENERIC\n"
784 "#ifdef USEDIFFUSE\n"
785 "varying vec2 TexCoord1;\n"
786 "#endif\n"
787 "#ifdef USESPECULAR\n"
788 "varying vec2 TexCoord2;\n"
789 "#endif\n"
790 "#ifdef VERTEX_SHADER\n"
791 "void main(void)\n"
792 "{\n"
793 "       gl_FrontColor = gl_Color;\n"
794 "#ifdef USEDIFFUSE\n"
795 "       TexCoord1 = gl_MultiTexCoord0.xy;\n"
796 "#endif\n"
797 "#ifdef USESPECULAR\n"
798 "       TexCoord2 = gl_MultiTexCoord1.xy;\n"
799 "#endif\n"
800 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
801 "}\n"
802 "#endif\n"
803 "\n"
804 "#ifdef FRAGMENT_SHADER\n"
805 "#ifdef USEDIFFUSE\n"
806 "uniform sampler2D Texture_First;\n"
807 "#endif\n"
808 "#ifdef USESPECULAR\n"
809 "uniform sampler2D Texture_Second;\n"
810 "#endif\n"
811 "\n"
812 "void main(void)\n"
813 "{\n"
814 "#ifdef USEVIEWTINT\n"
815 "       gl_FragColor = gl_Color;\n"
816 "#else\n"
817 "       gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
818 "#endif\n"
819 "#ifdef USEDIFFUSE\n"
820 "       gl_FragColor *= texture2D(Texture_First, TexCoord1);\n"
821 "#endif\n"
822 "\n"
823 "#ifdef USESPECULAR\n"
824 "       vec4 tex2 = texture2D(Texture_Second, TexCoord2);\n"
825 "# ifdef USECOLORMAPPING\n"
826 "       gl_FragColor *= tex2;\n"
827 "# endif\n"
828 "# ifdef USEGLOW\n"
829 "       gl_FragColor += tex2;\n"
830 "# endif\n"
831 "# ifdef USEVERTEXTEXTUREBLEND\n"
832 "       gl_FragColor = mix(gl_FragColor, tex2, tex2.a);\n"
833 "# endif\n"
834 "#endif\n"
835 "}\n"
836 "#endif\n"
837 "#else // !MODE_GENERIC\n"
838 "\n"
839 "\n"
840 "\n"
841 "\n"
842 "#ifdef MODE_BLOOMBLUR\n"
843 "varying TexCoord;\n"
844 "#ifdef VERTEX_SHADER\n"
845 "void main(void)\n"
846 "{\n"
847 "       gl_FrontColor = gl_Color;\n"
848 "       TexCoord = gl_MultiTexCoord0.xy;\n"
849 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
850 "}\n"
851 "#endif\n"
852 "\n"
853 "#ifdef FRAGMENT_SHADER\n"
854 "uniform sampler2D Texture_First;\n"
855 "uniform vec4 BloomBlur_Parameters;\n"
856 "\n"
857 "void main(void)\n"
858 "{\n"
859 "       int i;\n"
860 "       vec2 tc = TexCoord;\n"
861 "       vec3 color = texture2D(Texture_First, tc).rgb;\n"
862 "       tc += BloomBlur_Parameters.xy;\n"
863 "       for (i = 1;i < SAMPLES;i++)\n"
864 "       {\n"
865 "               color += texture2D(Texture_First, tc).rgb;\n"
866 "               tc += BloomBlur_Parameters.xy;\n"
867 "       }\n"
868 "       gl_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1);\n"
869 "}\n"
870 "#endif\n"
871 "#else // !MODE_BLOOMBLUR\n"
872 "#ifdef MODE_REFRACTION\n"
873 "varying vec2 TexCoord;\n"
874 "varying vec4 ModelViewProjectionPosition;\n"
875 "uniform mat4 TexMatrix;\n"
876 "#ifdef VERTEX_SHADER\n"
877 "\n"
878 "void main(void)\n"
879 "{\n"
880 "       TexCoord = vec2(TexMatrix * gl_MultiTexCoord0);\n"
881 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
882 "       ModelViewProjectionPosition = gl_Position;\n"
883 "}\n"
884 "#endif\n"
885 "\n"
886 "#ifdef FRAGMENT_SHADER\n"
887 "uniform sampler2D Texture_Normal;\n"
888 "uniform sampler2D Texture_Refraction;\n"
889 "uniform sampler2D Texture_Reflection;\n"
890 "\n"
891 "uniform vec4 DistortScaleRefractReflect;\n"
892 "uniform vec4 ScreenScaleRefractReflect;\n"
893 "uniform vec4 ScreenCenterRefractReflect;\n"
894 "uniform vec4 RefractColor;\n"
895 "uniform vec4 ReflectColor;\n"
896 "uniform float ReflectFactor;\n"
897 "uniform float ReflectOffset;\n"
898 "\n"
899 "void main(void)\n"
900 "{\n"
901 "       vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w);\n"
902 "       //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
903 "       vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
904 "       vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xy * DistortScaleRefractReflect.xy;\n"
905 "       // FIXME temporary hack to detect the case that the reflection\n"
906 "       // gets blackened at edges due to leaving the area that contains actual\n"
907 "       // content.\n"
908 "       // Remove this 'ack once we have a better way to stop this thing from\n"
909 "       // 'appening.\n"
910 "       float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05);\n"
911 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05);\n"
912 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05);\n"
913 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05);\n"
914 "       ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);\n"
915 "       gl_FragColor = vec4(texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * RefractColor;\n"
916 "}\n"
917 "#endif\n"
918 "#else // !MODE_REFRACTION\n"
919 "\n"
920 "\n"
921 "\n"
922 "\n"
923 "#ifdef MODE_WATER\n"
924 "varying vec2 TexCoord;\n"
925 "varying vec3 EyeVector;\n"
926 "varying vec4 ModelViewProjectionPosition;\n"
927 "#ifdef VERTEX_SHADER\n"
928 "uniform vec3 EyePosition;\n"
929 "uniform mat4 TexMatrix;\n"
930 "\n"
931 "void main(void)\n"
932 "{\n"
933 "       TexCoord = vec2(TexMatrix * gl_MultiTexCoord0);\n"
934 "       vec3 EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
935 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
936 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
937 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
938 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
939 "       ModelViewProjectionPosition = gl_Position;\n"
940 "}\n"
941 "#endif\n"
942 "\n"
943 "#ifdef FRAGMENT_SHADER\n"
944 "uniform sampler2D Texture_Normal;\n"
945 "uniform sampler2D Texture_Refraction;\n"
946 "uniform sampler2D Texture_Reflection;\n"
947 "\n"
948 "uniform vec4 DistortScaleRefractReflect;\n"
949 "uniform vec4 ScreenScaleRefractReflect;\n"
950 "uniform vec4 ScreenCenterRefractReflect;\n"
951 "uniform vec4 RefractColor;\n"
952 "uniform vec4 ReflectColor;\n"
953 "uniform float ReflectFactor;\n"
954 "uniform float ReflectOffset;\n"
955 "uniform float ClientTime;\n"
956 "#ifdef USENORMALMAPSCROLLBLEND\n"
957 "uniform vec2 NormalmapScrollBlend;\n"
958 "#endif\n"
959 "\n"
960 "void main(void)\n"
961 "{\n"
962 "       vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
963 "       //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
964 "       vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
965 "       //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0);\n"
966 "       // slight water animation via 2 layer scrolling (todo: tweak)\n"
967 "       #ifdef USENORMALMAPSCROLLBLEND\n"
968 "               vec3 normal = texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0);\n"
969 "               normal += texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb;\n"
970 "               vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * DistortScaleRefractReflect;\n"
971 "       #else\n"
972 "               vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * DistortScaleRefractReflect;\n"
973 "       #endif\n"
974 "       // FIXME temporary hack to detect the case that the reflection\n"
975 "       // gets blackened at edges due to leaving the area that contains actual\n"
976 "       // content.\n"
977 "       // Remove this 'ack once we have a better way to stop this thing from\n"
978 "       // 'appening.\n"
979 "       float f  = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, 0.01)).rgb) / 0.002);\n"
980 "       f       *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.005, -0.01)).rgb) / 0.002);\n"
981 "       f       *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, 0.01)).rgb) / 0.002);\n"
982 "       f       *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.005, -0.01)).rgb) / 0.002);\n"
983 "       ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f);\n"
984 "       f  = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, 0.005)).rgb) / 0.002);\n"
985 "       f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.005, -0.005)).rgb) / 0.002);\n"
986 "       f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002);\n"
987 "       f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002);\n"
988 "       ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f);\n"
989 "       float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset;\n"
990 "       gl_FragColor = mix(vec4(texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * RefractColor, vec4(texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel);\n"
991 "}\n"
992 "#endif\n"
993 "#else // !MODE_WATER\n"
994 "\n"
995 "\n"
996 "\n"
997 "\n"
998 "// common definitions between vertex shader and fragment shader:\n"
999 "\n"
1000 "varying vec2 TexCoord;\n"
1001 "#ifdef USEVERTEXTEXTUREBLEND\n"
1002 "varying vec2 TexCoord2;\n"
1003 "#endif\n"
1004 "#ifdef USELIGHTMAP\n"
1005 "varying vec2 TexCoordLightmap;\n"
1006 "#endif\n"
1007 "\n"
1008 "#ifdef MODE_LIGHTSOURCE\n"
1009 "varying vec3 CubeVector;\n"
1010 "#endif\n"
1011 "\n"
1012 "#if (defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)) && defined(USEDIFFUSE)\n"
1013 "varying vec3 LightVector;\n"
1014 "#endif\n"
1015 "\n"
1016 "#ifdef USEEYEVECTOR\n"
1017 "varying vec3 EyeVector;\n"
1018 "#endif\n"
1019 "#ifdef USEFOG\n"
1020 "varying vec4 EyeVectorModelSpaceFogPlaneVertexDist;\n"
1021 "#endif\n"
1022 "\n"
1023 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE)\n"
1024 "varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent)\n"
1025 "varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal)\n"
1026 "varying vec3 VectorR; // direction of R texcoord (surface normal)\n"
1027 "#endif\n"
1028 "\n"
1029 "#ifdef USEREFLECTION\n"
1030 "varying vec4 ModelViewProjectionPosition;\n"
1031 "#endif\n"
1032 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
1033 "uniform vec3 LightPosition;\n"
1034 "varying vec4 ModelViewPosition;\n"
1035 "#endif\n"
1036 "\n"
1037 "#ifdef MODE_LIGHTSOURCE\n"
1038 "uniform vec3 LightPosition;\n"
1039 "#endif\n"
1040 "uniform vec3 EyePosition;\n"
1041 "#ifdef MODE_LIGHTDIRECTION\n"
1042 "uniform vec3 LightDir;\n"
1043 "#endif\n"
1044 "uniform vec4 FogPlane;\n"
1045 "\n"
1046 "#ifdef USESHADOWMAPORTHO\n"
1047 "varying vec3 ShadowMapTC;\n"
1048 "#endif\n"
1049 "\n"
1050 "\n"
1051 "\n"
1052 "\n"
1053 "\n"
1054 "// 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"
1055 "\n"
1056 "// fragment shader specific:\n"
1057 "#ifdef FRAGMENT_SHADER\n"
1058 "\n"
1059 "uniform sampler2D Texture_Normal;\n"
1060 "uniform sampler2D Texture_Color;\n"
1061 "uniform sampler2D Texture_Gloss;\n"
1062 "#ifdef USEGLOW\n"
1063 "uniform sampler2D Texture_Glow;\n"
1064 "#endif\n"
1065 "#ifdef USEVERTEXTEXTUREBLEND\n"
1066 "uniform sampler2D Texture_SecondaryNormal;\n"
1067 "uniform sampler2D Texture_SecondaryColor;\n"
1068 "uniform sampler2D Texture_SecondaryGloss;\n"
1069 "#ifdef USEGLOW\n"
1070 "uniform sampler2D Texture_SecondaryGlow;\n"
1071 "#endif\n"
1072 "#endif\n"
1073 "#ifdef USECOLORMAPPING\n"
1074 "uniform sampler2D Texture_Pants;\n"
1075 "uniform sampler2D Texture_Shirt;\n"
1076 "#endif\n"
1077 "#ifdef USEFOG\n"
1078 "#ifdef USEFOGHEIGHTTEXTURE\n"
1079 "uniform sampler2D Texture_FogHeightTexture;\n"
1080 "#endif\n"
1081 "uniform sampler2D Texture_FogMask;\n"
1082 "#endif\n"
1083 "#ifdef USELIGHTMAP\n"
1084 "uniform sampler2D Texture_Lightmap;\n"
1085 "#endif\n"
1086 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)\n"
1087 "uniform sampler2D Texture_Deluxemap;\n"
1088 "#endif\n"
1089 "#ifdef USEREFLECTION\n"
1090 "uniform sampler2D Texture_Reflection;\n"
1091 "#endif\n"
1092 "\n"
1093 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
1094 "uniform sampler2D Texture_ScreenDepth;\n"
1095 "uniform sampler2D Texture_ScreenNormalMap;\n"
1096 "#endif\n"
1097 "#ifdef USEDEFERREDLIGHTMAP\n"
1098 "uniform sampler2D Texture_ScreenDiffuse;\n"
1099 "uniform sampler2D Texture_ScreenSpecular;\n"
1100 "#endif\n"
1101 "\n"
1102 "uniform myhalf3 Color_Pants;\n"
1103 "uniform myhalf3 Color_Shirt;\n"
1104 "uniform myhalf3 FogColor;\n"
1105 "\n"
1106 "#ifdef USEFOG\n"
1107 "uniform float FogRangeRecip;\n"
1108 "uniform float FogPlaneViewDist;\n"
1109 "uniform float FogHeightFade;\n"
1110 "vec3 FogVertex(vec3 surfacecolor)\n"
1111 "{\n"
1112 "       vec3 EyeVectorModelSpace = EyeVectorModelSpaceFogPlaneVertexDist.xyz;\n"
1113 "       float FogPlaneVertexDist = EyeVectorModelSpaceFogPlaneVertexDist.w;\n"
1114 "       float fogfrac;\n"
1115 "#ifdef USEFOGHEIGHTTEXTURE\n"
1116 "       vec4 fogheightpixel = texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade));\n"
1117 "       fogfrac = fogheightpixel.a;\n"
1118 "       return mix(fogheightpixel.rgb * FogColor, surfacecolor, texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r);\n"
1119 "#else\n"
1120 "# ifdef USEFOGOUTSIDE\n"
1121 "       fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade);\n"
1122 "# else\n"
1123 "       fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);\n"
1124 "# endif\n"
1125 "       return mix(FogColor, surfacecolor, texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r);\n"
1126 "#endif\n"
1127 "}\n"
1128 "#endif\n"
1129 "\n"
1130 "#ifdef USEOFFSETMAPPING\n"
1131 "uniform float OffsetMapping_Scale;\n"
1132 "vec2 OffsetMapping(vec2 TexCoord)\n"
1133 "{\n"
1134 "#ifdef USEOFFSETMAPPING_RELIEFMAPPING\n"
1135 "       // 14 sample relief mapping: linear search and then binary search\n"
1136 "       // this basically steps forward a small amount repeatedly until it finds\n"
1137 "       // itself inside solid, then jitters forward and back using decreasing\n"
1138 "       // amounts to find the impact\n"
1139 "       //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1);\n"
1140 "       //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
1141 "       vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
1142 "       vec3 RT = vec3(TexCoord, 1);\n"
1143 "       OffsetVector *= 0.1;\n"
1144 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1145 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1146 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1147 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1148 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1149 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1150 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1151 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1152 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1153 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z)          - 0.5);\n"
1154 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5    - 0.25);\n"
1155 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25   - 0.125);\n"
1156 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125  - 0.0625);\n"
1157 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125);\n"
1158 "       return RT.xy;\n"
1159 "#else\n"
1160 "       // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits)\n"
1161 "       // this basically moves forward the full distance, and then backs up based\n"
1162 "       // on height of samples\n"
1163 "       //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1));\n"
1164 "       //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1));\n"
1165 "       vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1));\n"
1166 "       TexCoord += OffsetVector;\n"
1167 "       OffsetVector *= 0.5;\n"
1168 "       TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
1169 "       TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
1170 "       return TexCoord;\n"
1171 "#endif\n"
1172 "}\n"
1173 "#endif // USEOFFSETMAPPING\n"
1174 "\n"
1175 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE)\n"
1176 "uniform sampler2D Texture_Attenuation;\n"
1177 "uniform samplerCube Texture_Cube;\n"
1178 "#endif\n"
1179 "\n"
1180 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO)\n"
1181 "\n"
1182 "#ifdef USESHADOWMAP2D\n"
1183 "# ifdef USESHADOWSAMPLER\n"
1184 "uniform sampler2DShadow Texture_ShadowMap2D;\n"
1185 "# else\n"
1186 "uniform sampler2D Texture_ShadowMap2D;\n"
1187 "# endif\n"
1188 "#endif\n"
1189 "\n"
1190 "#ifdef USESHADOWMAPVSDCT\n"
1191 "uniform samplerCube Texture_CubeProjection;\n"
1192 "#endif\n"
1193 "\n"
1194 "#if defined(USESHADOWMAP2D)\n"
1195 "uniform vec2 ShadowMap_TextureScale;\n"
1196 "uniform vec4 ShadowMap_Parameters;\n"
1197 "#endif\n"
1198 "\n"
1199 "#if defined(USESHADOWMAP2D)\n"
1200 "# ifdef USESHADOWMAPORTHO\n"
1201 "#  define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz))\n"
1202 "# else\n"
1203 "#  ifdef USESHADOWMAPVSDCT\n"
1204 "vec3 GetShadowMapTC2D(vec3 dir)\n"
1205 "{\n"
1206 "       vec3 adir = abs(dir);\n"
1207 "       vec2 aparams = ShadowMap_Parameters.xy / max(max(adir.x, adir.y), adir.z);\n"
1208 "       vec4 proj = textureCube(Texture_CubeProjection, dir);\n"
1209 "       return vec3(mix(dir.xy, dir.zz, proj.xy) * aparams.x + proj.zw * ShadowMap_Parameters.z, aparams.y + ShadowMap_Parameters.w);\n"
1210 "}\n"
1211 "#  else\n"
1212 "vec3 GetShadowMapTC2D(vec3 dir)\n"
1213 "{\n"
1214 "       vec3 adir = abs(dir);\n"
1215 "       float ma = adir.z;\n"
1216 "       vec4 proj = vec4(dir, 2.5);\n"
1217 "       if (adir.x > ma) { ma = adir.x; proj = vec4(dir.zyx, 0.5); }\n"
1218 "       if (adir.y > ma) { ma = adir.y; proj = vec4(dir.xzy, 1.5); }\n"
1219 "       vec2 aparams = ShadowMap_Parameters.xy / ma;\n"
1220 "       return vec3(proj.xy * aparams.x + vec2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, aparams.y + ShadowMap_Parameters.w);\n"
1221 "}\n"
1222 "#  endif\n"
1223 "# endif\n"
1224 "#endif // defined(USESHADOWMAP2D)\n"
1225 "\n"
1226 "# ifdef USESHADOWMAP2D\n"
1227 "float ShadowMapCompare(vec3 dir)\n"
1228 "{\n"
1229 "       vec3 shadowmaptc = GetShadowMapTC2D(dir);\n"
1230 "       float f;\n"
1231 "\n"
1232 "#  ifdef USESHADOWSAMPLER\n"
1233 "#    ifdef USESHADOWMAPPCF\n"
1234 "#      define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r  \n"
1235 "       vec2 center = shadowmaptc.xy*ShadowMap_TextureScale;\n"
1236 "       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"
1237 "#    else\n"
1238 "       f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r;\n"
1239 "#    endif\n"
1240 "#  else\n"
1241 "#    ifdef USESHADOWMAPPCF\n"
1242 "#     if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4)\n"
1243 "#      ifdef GL_ARB_texture_gather\n"
1244 "#        define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y))\n"
1245 "#      else\n"
1246 "#        define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)\n"
1247 "#      endif\n"
1248 "       vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale;\n"
1249 "#      if USESHADOWMAPPCF > 1\n"
1250 "   vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0));\n"
1251 "   vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0));\n"
1252 "   vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0));\n"
1253 "   vec4 group4 = step(shadowmaptc.z, texval(-2.0,  0.0));\n"
1254 "   vec4 group5 = step(shadowmaptc.z, texval( 0.0,  0.0));\n"
1255 "   vec4 group6 = step(shadowmaptc.z, texval( 2.0,  0.0));\n"
1256 "   vec4 group7 = step(shadowmaptc.z, texval(-2.0,  2.0));\n"
1257 "   vec4 group8 = step(shadowmaptc.z, texval( 0.0,  2.0));\n"
1258 "   vec4 group9 = step(shadowmaptc.z, texval( 2.0,  2.0));\n"
1259 "       vec4 locols = vec4(group1.ab, group3.ab);\n"
1260 "       vec4 hicols = vec4(group7.rg, group9.rg);\n"
1261 "       locols.yz += group2.ab;\n"
1262 "       hicols.yz += group8.rg;\n"
1263 "       vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) +\n"
1264 "                               vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) +\n"
1265 "                               mix(locols, hicols, offset.y);\n"
1266 "       vec4 cols = group5 + vec4(group2.rg, group8.ab);\n"
1267 "       cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x);\n"
1268 "       f = dot(cols, vec4(1.0/25.0));\n"
1269 "#      else\n"
1270 "       vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0));\n"
1271 "       vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0));\n"
1272 "       vec4 group3 = step(shadowmaptc.z, texval(-1.0,  1.0));\n"
1273 "       vec4 group4 = step(shadowmaptc.z, texval( 1.0,  1.0));\n"
1274 "       vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) +\n"
1275 "                               mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y);\n"
1276 "       f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));\n"
1277 "#      endif\n"
1278 "#     else\n"
1279 "#      ifdef GL_EXT_gpu_shader4\n"
1280 "#        define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r\n"
1281 "#      else\n"
1282 "#        define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r  \n"
1283 "#      endif\n"
1284 "#      if USESHADOWMAPPCF > 1\n"
1285 "       vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);\n"
1286 "       center *= ShadowMap_TextureScale;\n"
1287 "       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"
1288 "       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"
1289 "       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"
1290 "       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"
1291 "       vec4 cols = row2 + row3 + mix(row1, row4, offset.y);\n"
1292 "       f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));\n"
1293 "#      else\n"
1294 "       vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy);\n"
1295 "       vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));\n"
1296 "       vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0)));\n"
1297 "       vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0)));\n"
1298 "       vec3 cols = row2 + mix(row1, row3, offset.y);\n"
1299 "       f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25));\n"
1300 "#      endif\n"
1301 "#     endif\n"
1302 "#    else\n"
1303 "       f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r);\n"
1304 "#    endif\n"
1305 "#  endif\n"
1306 "#  ifdef USESHADOWMAPORTHO\n"
1307 "       return mix(ShadowMap_Parameters.w, 1.0, f);\n"
1308 "#  else\n"
1309 "       return f;\n"
1310 "#  endif\n"
1311 "}\n"
1312 "# endif\n"
1313 "#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO)\n"
1314 "#endif // FRAGMENT_SHADER\n"
1315 "\n"
1316 "\n"
1317 "\n"
1318 "\n"
1319 "#ifdef MODE_DEFERREDGEOMETRY\n"
1320 "#ifdef VERTEX_SHADER\n"
1321 "uniform mat4 TexMatrix;\n"
1322 "#ifdef USEVERTEXTEXTUREBLEND\n"
1323 "uniform mat4 BackgroundTexMatrix;\n"
1324 "#endif\n"
1325 "uniform mat4 ModelViewMatrix;\n"
1326 "void main(void)\n"
1327 "{\n"
1328 "       TexCoord = vec2(TexMatrix * gl_MultiTexCoord0);\n"
1329 "#ifdef USEVERTEXTEXTUREBLEND\n"
1330 "       gl_FrontColor = gl_Color;\n"
1331 "       TexCoord2 = vec2(BackgroundTexMatrix * gl_MultiTexCoord0);\n"
1332 "#endif\n"
1333 "\n"
1334 "       // transform unnormalized eye direction into tangent space\n"
1335 "#ifdef USEOFFSETMAPPING\n"
1336 "       vec3 EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
1337 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
1338 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
1339 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
1340 "#endif\n"
1341 "\n"
1342 "       VectorS = (ModelViewMatrix * vec4(gl_MultiTexCoord1.xyz, 0)).xyz;\n"
1343 "       VectorT = (ModelViewMatrix * vec4(gl_MultiTexCoord2.xyz, 0)).xyz;\n"
1344 "       VectorR = (ModelViewMatrix * vec4(gl_MultiTexCoord3.xyz, 0)).xyz;\n"
1345 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
1346 "}\n"
1347 "#endif // VERTEX_SHADER\n"
1348 "\n"
1349 "#ifdef FRAGMENT_SHADER\n"
1350 "void main(void)\n"
1351 "{\n"
1352 "#ifdef USEOFFSETMAPPING\n"
1353 "       // apply offsetmapping\n"
1354 "       vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
1355 "#define TexCoord TexCoordOffset\n"
1356 "#endif\n"
1357 "\n"
1358 "#ifdef USEALPHAKILL\n"
1359 "       if (texture2D(Texture_Color, TexCoord).a < 0.5)\n"
1360 "               discard;\n"
1361 "#endif\n"
1362 "\n"
1363 "#ifdef USEVERTEXTEXTUREBLEND\n"
1364 "       float alpha = texture2D(Texture_Color, TexCoord).a;\n"
1365 "       float terrainblend = clamp(float(gl_Color.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0));\n"
1366 "       //float terrainblend = min(float(gl_Color.a) * alpha * 2.0, float(1.0));\n"
1367 "       //float terrainblend = float(gl_Color.a) * alpha > 0.5;\n"
1368 "#endif\n"
1369 "\n"
1370 "#ifdef USEVERTEXTEXTUREBLEND\n"
1371 "       vec3 surfacenormal = mix(vec3(texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(texture2D(Texture_Normal, TexCoord)), terrainblend) - vec3(0.5, 0.5, 0.5);\n"
1372 "       float a = mix(texture2D(Texture_SecondaryGloss, TexCoord2).a, texture2D(Texture_Gloss, TexCoord).a, terrainblend);\n"
1373 "#else\n"
1374 "       vec3 surfacenormal = vec3(texture2D(Texture_Normal, TexCoord)) - vec3(0.5, 0.5, 0.5);\n"
1375 "       float a = texture2D(Texture_Gloss, TexCoord).a;\n"
1376 "#endif\n"
1377 "\n"
1378 "       gl_FragColor = vec4(normalize(surfacenormal.x * VectorS + surfacenormal.y * VectorT + surfacenormal.z * VectorR) * 0.5 + vec3(0.5, 0.5, 0.5), a);\n"
1379 "}\n"
1380 "#endif // FRAGMENT_SHADER\n"
1381 "#else // !MODE_DEFERREDGEOMETRY\n"
1382 "\n"
1383 "\n"
1384 "\n"
1385 "\n"
1386 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
1387 "#ifdef VERTEX_SHADER\n"
1388 "uniform mat4 ModelViewMatrix;\n"
1389 "void main(void)\n"
1390 "{\n"
1391 "       ModelViewPosition = ModelViewMatrix * gl_Vertex;\n"
1392 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
1393 "}\n"
1394 "#endif // VERTEX_SHADER\n"
1395 "\n"
1396 "#ifdef FRAGMENT_SHADER\n"
1397 "uniform mat4 ViewToLight;\n"
1398 "// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far));\n"
1399 "uniform vec2 ScreenToDepth;\n"
1400 "uniform myhalf3 DeferredColor_Ambient;\n"
1401 "uniform myhalf3 DeferredColor_Diffuse;\n"
1402 "#ifdef USESPECULAR\n"
1403 "uniform myhalf3 DeferredColor_Specular;\n"
1404 "uniform myhalf SpecularPower;\n"
1405 "#endif\n"
1406 "uniform myhalf2 PixelToScreenTexCoord;\n"
1407 "void main(void)\n"
1408 "{\n"
1409 "       // calculate viewspace pixel position\n"
1410 "       vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord;\n"
1411 "       vec3 position;\n"
1412 "       position.z = ScreenToDepth.y / (texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x);\n"
1413 "       position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z);\n"
1414 "       // decode viewspace pixel normal\n"
1415 "       myhalf4 normalmap = texture2D(Texture_ScreenNormalMap, ScreenTexCoord);\n"
1416 "       myhalf3 surfacenormal = normalize(normalmap.rgb - myhalf3(0.5,0.5,0.5));\n"
1417 "       // surfacenormal = pixel normal in viewspace\n"
1418 "       // LightVector = pixel to light in viewspace\n"
1419 "       // CubeVector = position in lightspace\n"
1420 "       // eyevector = pixel to view in viewspace\n"
1421 "       vec3 CubeVector = vec3(ViewToLight * vec4(position,1));\n"
1422 "       myhalf fade = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));\n"
1423 "#ifdef USEDIFFUSE\n"
1424 "       // calculate diffuse shading\n"
1425 "       myhalf3 lightnormal = myhalf3(normalize(LightPosition - position));\n"
1426 "       myhalf diffuse = myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
1427 "#endif\n"
1428 "#ifdef USESPECULAR\n"
1429 "       // calculate directional shading\n"
1430 "       vec3 eyevector = position * -1.0;\n"
1431 "#  ifdef USEEXACTSPECULARMATH\n"
1432 "       myhalf specular = pow(myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), SpecularPower * normalmap.a);\n"
1433 "#  else\n"
1434 "       myhalf3 specularnormal = normalize(lightnormal + myhalf3(normalize(eyevector)));\n"
1435 "       myhalf specular = pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * normalmap.a);\n"
1436 "#  endif\n"
1437 "#endif\n"
1438 "\n"
1439 "#if defined(USESHADOWMAP2D)\n"
1440 "       fade *= ShadowMapCompare(CubeVector);\n"
1441 "#endif\n"
1442 "\n"
1443 "#ifdef USEDIFFUSE\n"
1444 "       gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0);\n"
1445 "#else\n"
1446 "       gl_FragData[0] = vec4(DeferredColor_Ambient * fade, 1.0);\n"
1447 "#endif\n"
1448 "#ifdef USESPECULAR\n"
1449 "       gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0);\n"
1450 "#else\n"
1451 "       gl_FragData[1] = vec4(0.0, 0.0, 0.0, 1.0);\n"
1452 "#endif\n"
1453 "\n"
1454 "# ifdef USECUBEFILTER\n"
1455 "       vec3 cubecolor = textureCube(Texture_Cube, CubeVector).rgb;\n"
1456 "       gl_FragData[0].rgb *= cubecolor;\n"
1457 "       gl_FragData[1].rgb *= cubecolor;\n"
1458 "# endif\n"
1459 "}\n"
1460 "#endif // FRAGMENT_SHADER\n"
1461 "#else // !MODE_DEFERREDLIGHTSOURCE\n"
1462 "\n"
1463 "\n"
1464 "\n"
1465 "\n"
1466 "#ifdef VERTEX_SHADER\n"
1467 "uniform mat4 TexMatrix;\n"
1468 "#ifdef USEVERTEXTEXTUREBLEND\n"
1469 "uniform mat4 BackgroundTexMatrix;\n"
1470 "#endif\n"
1471 "#ifdef MODE_LIGHTSOURCE\n"
1472 "uniform mat4 ModelToLight;\n"
1473 "#endif\n"
1474 "#ifdef USESHADOWMAPORTHO\n"
1475 "uniform mat4 ShadowMapMatrix;\n"
1476 "#endif\n"
1477 "void main(void)\n"
1478 "{\n"
1479 "#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND)\n"
1480 "       gl_FrontColor = gl_Color;\n"
1481 "#endif\n"
1482 "       // copy the surface texcoord\n"
1483 "       TexCoord = vec2(TexMatrix * gl_MultiTexCoord0);\n"
1484 "#ifdef USEVERTEXTEXTUREBLEND\n"
1485 "       TexCoord2 = vec2(BackgroundTexMatrix * gl_MultiTexCoord0);\n"
1486 "#endif\n"
1487 "#ifdef USELIGHTMAP\n"
1488 "       TexCoordLightmap = vec2(gl_MultiTexCoord4);\n"
1489 "#endif\n"
1490 "\n"
1491 "#ifdef MODE_LIGHTSOURCE\n"
1492 "       // transform vertex position into light attenuation/cubemap space\n"
1493 "       // (-1 to +1 across the light box)\n"
1494 "       CubeVector = vec3(ModelToLight * gl_Vertex);\n"
1495 "\n"
1496 "# ifdef USEDIFFUSE\n"
1497 "       // transform unnormalized light direction into tangent space\n"
1498 "       // (we use unnormalized to ensure that it interpolates correctly and then\n"
1499 "       //  normalize it per pixel)\n"
1500 "       vec3 lightminusvertex = LightPosition - gl_Vertex.xyz;\n"
1501 "       LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz);\n"
1502 "       LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz);\n"
1503 "       LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz);\n"
1504 "# endif\n"
1505 "#endif\n"
1506 "\n"
1507 "#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE)\n"
1508 "       LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz);\n"
1509 "       LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz);\n"
1510 "       LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz);\n"
1511 "#endif\n"
1512 "\n"
1513 "       // transform unnormalized eye direction into tangent space\n"
1514 "#ifdef USEEYEVECTOR\n"
1515 "       vec3 EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
1516 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
1517 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
1518 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
1519 "#endif\n"
1520 "\n"
1521 "#ifdef USEFOG\n"
1522 "       EyeVectorModelSpaceFogPlaneVertexDist.xyz = EyePosition - gl_Vertex.xyz;\n"
1523 "       EyeVectorModelSpaceFogPlaneVertexDist.w = dot(FogPlane, gl_Vertex);\n"
1524 "#endif\n"
1525 "\n"
1526 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE)\n"
1527 "       VectorS = gl_MultiTexCoord1.xyz;\n"
1528 "       VectorT = gl_MultiTexCoord2.xyz;\n"
1529 "       VectorR = gl_MultiTexCoord3.xyz;\n"
1530 "#endif\n"
1531 "\n"
1532 "       // transform vertex to camera space, using ftransform to match non-VS rendering\n"
1533 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
1534 "\n"
1535 "#ifdef USESHADOWMAPORTHO\n"
1536 "       ShadowMapTC = vec3(ShadowMapMatrix * gl_Position);\n"
1537 "#endif\n"
1538 "\n"
1539 "#ifdef USEREFLECTION\n"
1540 "       ModelViewProjectionPosition = gl_Position;\n"
1541 "#endif\n"
1542 "}\n"
1543 "#endif // VERTEX_SHADER\n"
1544 "\n"
1545 "\n"
1546 "\n"
1547 "\n"
1548 "#ifdef FRAGMENT_SHADER\n"
1549 "#ifdef USEDEFERREDLIGHTMAP\n"
1550 "uniform myhalf2 PixelToScreenTexCoord;\n"
1551 "uniform myhalf3 DeferredMod_Diffuse;\n"
1552 "uniform myhalf3 DeferredMod_Specular;\n"
1553 "#endif\n"
1554 "uniform myhalf3 Color_Ambient;\n"
1555 "uniform myhalf3 Color_Diffuse;\n"
1556 "uniform myhalf3 Color_Specular;\n"
1557 "uniform myhalf SpecularPower;\n"
1558 "#ifdef USEGLOW\n"
1559 "uniform myhalf3 Color_Glow;\n"
1560 "#endif\n"
1561 "uniform myhalf Alpha;\n"
1562 "#ifdef USEREFLECTION\n"
1563 "uniform vec4 DistortScaleRefractReflect;\n"
1564 "uniform vec4 ScreenScaleRefractReflect;\n"
1565 "uniform vec4 ScreenCenterRefractReflect;\n"
1566 "uniform myhalf4 ReflectColor;\n"
1567 "#endif\n"
1568 "#ifdef USEREFLECTCUBE\n"
1569 "uniform mat4 ModelToReflectCube;\n"
1570 "uniform sampler2D Texture_ReflectMask;\n"
1571 "uniform samplerCube Texture_ReflectCube;\n"
1572 "#endif\n"
1573 "#ifdef MODE_LIGHTDIRECTION\n"
1574 "uniform myhalf3 LightColor;\n"
1575 "#endif\n"
1576 "#ifdef MODE_LIGHTSOURCE\n"
1577 "uniform myhalf3 LightColor;\n"
1578 "#endif\n"
1579 "void main(void)\n"
1580 "{\n"
1581 "#ifdef USEOFFSETMAPPING\n"
1582 "       // apply offsetmapping\n"
1583 "       vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
1584 "#define TexCoord TexCoordOffset\n"
1585 "#endif\n"
1586 "\n"
1587 "       // combine the diffuse textures (base, pants, shirt)\n"
1588 "       myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord));\n"
1589 "#ifdef USEALPHAKILL\n"
1590 "       if (color.a < 0.5)\n"
1591 "               discard;\n"
1592 "#endif\n"
1593 "       color.a *= Alpha;\n"
1594 "#ifdef USECOLORMAPPING\n"
1595 "       color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt;\n"
1596 "#endif\n"
1597 "#ifdef USEVERTEXTEXTUREBLEND\n"
1598 "       myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0));\n"
1599 "       //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0));\n"
1600 "       //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5;\n"
1601 "       color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend);\n"
1602 "       color.a = 1.0;\n"
1603 "       //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend);\n"
1604 "#endif\n"
1605 "\n"
1606 "       // get the surface normal\n"
1607 "#ifdef USEVERTEXTEXTUREBLEND\n"
1608 "       myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5));\n"
1609 "#else\n"
1610 "       myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5));\n"
1611 "#endif\n"
1612 "\n"
1613 "       // get the material colors\n"
1614 "       myhalf3 diffusetex = color.rgb;\n"
1615 "#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)\n"
1616 "# ifdef USEVERTEXTEXTUREBLEND\n"
1617 "       myhalf4 glosstex = mix(myhalf4(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf4(texture2D(Texture_Gloss, TexCoord)), terrainblend);\n"
1618 "# else\n"
1619 "       myhalf4 glosstex = myhalf4(texture2D(Texture_Gloss, TexCoord));\n"
1620 "# endif\n"
1621 "#endif\n"
1622 "\n"
1623 "#ifdef USEREFLECTCUBE\n"
1624 "       vec3 TangentReflectVector = reflect(-EyeVector, surfacenormal);\n"
1625 "       vec3 ModelReflectVector = TangentReflectVector.x * VectorS + TangentReflectVector.y * VectorT + TangentReflectVector.z * VectorR;\n"
1626 "       vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0));\n"
1627 "       diffusetex += myhalf3(texture2D(Texture_ReflectMask, TexCoord)) * myhalf3(textureCube(Texture_ReflectCube, ReflectCubeTexCoord));\n"
1628 "#endif\n"
1629 "\n"
1630 "\n"
1631 "\n"
1632 "\n"
1633 "#ifdef MODE_LIGHTSOURCE\n"
1634 "       // light source\n"
1635 "#ifdef USEDIFFUSE\n"
1636 "       myhalf3 lightnormal = myhalf3(normalize(LightVector));\n"
1637 "       myhalf diffuse = myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
1638 "       color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse);\n"
1639 "#ifdef USESPECULAR\n"
1640 "#ifdef USEEXACTSPECULARMATH\n"
1641 "       myhalf specular = pow(myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower * glosstex.a);\n"
1642 "#else\n"
1643 "       myhalf3 specularnormal = normalize(lightnormal + myhalf3(normalize(EyeVector)));\n"
1644 "       myhalf specular = pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * glosstex.a);\n"
1645 "#endif\n"
1646 "       color.rgb += glosstex.rgb * (specular * Color_Specular);\n"
1647 "#endif\n"
1648 "#else\n"
1649 "       color.rgb = diffusetex * Color_Ambient;\n"
1650 "#endif\n"
1651 "       color.rgb *= LightColor;\n"
1652 "       color.rgb *= myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));\n"
1653 "#if defined(USESHADOWMAP2D)\n"
1654 "       color.rgb *= ShadowMapCompare(CubeVector);\n"
1655 "#endif\n"
1656 "# ifdef USECUBEFILTER\n"
1657 "       color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector));\n"
1658 "# endif\n"
1659 "#endif // MODE_LIGHTSOURCE\n"
1660 "\n"
1661 "\n"
1662 "\n"
1663 "\n"
1664 "#ifdef MODE_LIGHTDIRECTION\n"
1665 "#define SHADING\n"
1666 "#ifdef USEDIFFUSE\n"
1667 "       myhalf3 lightnormal = myhalf3(normalize(LightVector));\n"
1668 "#endif\n"
1669 "#define lightcolor LightColor\n"
1670 "#endif // MODE_LIGHTDIRECTION\n"
1671 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
1672 "#define SHADING\n"
1673 "       // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe)\n"
1674 "       myhalf3 lightnormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0);\n"
1675 "       myhalf3 lightcolor = myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap));\n"
1676 "       // convert modelspace light vector to tangentspace\n"
1677 "       myhalf3 lightnormal;\n"
1678 "       lightnormal.x = dot(lightnormal_modelspace, myhalf3(VectorS));\n"
1679 "       lightnormal.y = dot(lightnormal_modelspace, myhalf3(VectorT));\n"
1680 "       lightnormal.z = dot(lightnormal_modelspace, myhalf3(VectorR));\n"
1681 "       lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this\n"
1682 "       // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division)\n"
1683 "       // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar\n"
1684 "       // is used (the lightmap and deluxemap coords correspond to virtually random coordinates\n"
1685 "       // on that luxel, and NOT to its center, because recursive triangle subdivision is used\n"
1686 "       // to map the luxels to coordinates on the draw surfaces), which also causes\n"
1687 "       // deluxemaps to be wrong because light contributions from the wrong side of the surface\n"
1688 "       // are added up. To prevent divisions by zero or strong exaggerations, a max()\n"
1689 "       // nudge is done here at expense of some additional fps. This is ONLY needed for\n"
1690 "       // deluxemaps, tangentspace deluxemap avoid this problem by design.\n"
1691 "       lightcolor *= 1.0 / max(0.25, lightnormal.z);\n"
1692 "#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
1693 "#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n"
1694 "#define SHADING\n"
1695 "       // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light)\n"
1696 "       myhalf3 lightnormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0);\n"
1697 "       myhalf3 lightcolor = myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap));\n"
1698 "#endif\n"
1699 "\n"
1700 "\n"
1701 "\n"
1702 "\n"
1703 "#ifdef MODE_FAKELIGHT\n"
1704 "#define SHADING\n"
1705 "myhalf3 lightnormal = myhalf3(normalize(EyeVector));\n"
1706 "myhalf3 lightcolor = myhalf3(1.0);\n"
1707 "#endif // MODE_FAKELIGHT\n"
1708 "\n"
1709 "\n"
1710 "\n"
1711 "\n"
1712 "#ifdef MODE_LIGHTMAP\n"
1713 "       color.rgb = diffusetex * (Color_Ambient + myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * Color_Diffuse);\n"
1714 "#endif // MODE_LIGHTMAP\n"
1715 "#ifdef MODE_VERTEXCOLOR\n"
1716 "       color.rgb = diffusetex * (Color_Ambient + myhalf3(gl_Color.rgb) * Color_Diffuse);\n"
1717 "#endif // MODE_VERTEXCOLOR\n"
1718 "#ifdef MODE_FLATCOLOR\n"
1719 "       color.rgb = diffusetex * Color_Ambient;\n"
1720 "#endif // MODE_FLATCOLOR\n"
1721 "\n"
1722 "\n"
1723 "\n"
1724 "\n"
1725 "#ifdef SHADING\n"
1726 "# ifdef USEDIFFUSE\n"
1727 "       myhalf diffuse = myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
1728 "#  ifdef USESPECULAR\n"
1729 "#   ifdef USEEXACTSPECULARMATH\n"
1730 "       myhalf specular = pow(myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower * glosstex.a);\n"
1731 "#   else\n"
1732 "       myhalf3 specularnormal = normalize(lightnormal + myhalf3(normalize(EyeVector)));\n"
1733 "       myhalf specular = pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * glosstex.a);\n"
1734 "#   endif\n"
1735 "       color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor;\n"
1736 "#  else\n"
1737 "       color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor);\n"
1738 "#  endif\n"
1739 "# else\n"
1740 "       color.rgb = diffusetex * Color_Ambient;\n"
1741 "# endif\n"
1742 "#endif\n"
1743 "\n"
1744 "#ifdef USESHADOWMAPORTHO\n"
1745 "       color.rgb *= ShadowMapCompare(ShadowMapTC);\n"
1746 "#endif\n"
1747 "\n"
1748 "#ifdef USEDEFERREDLIGHTMAP\n"
1749 "       vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord;\n"
1750 "       color.rgb += diffusetex * myhalf3(texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse;\n"
1751 "       color.rgb += glosstex.rgb * myhalf3(texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular;\n"
1752 "#endif\n"
1753 "\n"
1754 "#ifdef USEGLOW\n"
1755 "#ifdef USEVERTEXTEXTUREBLEND\n"
1756 "       color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend) * Color_Glow;\n"
1757 "#else\n"
1758 "       color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * Color_Glow;\n"
1759 "#endif\n"
1760 "#endif\n"
1761 "\n"
1762 "#ifdef USEFOG\n"
1763 "       color.rgb = FogVertex(color.rgb);\n"
1764 "#endif\n"
1765 "\n"
1766 "       // 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"
1767 "#ifdef USEREFLECTION\n"
1768 "       vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
1769 "       //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
1770 "       vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw;\n"
1771 "       vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw;\n"
1772 "       // FIXME temporary hack to detect the case that the reflection\n"
1773 "       // gets blackened at edges due to leaving the area that contains actual\n"
1774 "       // content.\n"
1775 "       // Remove this 'ack once we have a better way to stop this thing from\n"
1776 "       // 'appening.\n"
1777 "       float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05);\n"
1778 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05);\n"
1779 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05);\n"
1780 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05);\n"
1781 "       ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);\n"
1782 "       color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a);\n"
1783 "#endif\n"
1784 "\n"
1785 "       gl_FragColor = vec4(color);\n"
1786 "}\n"
1787 "#endif // FRAGMENT_SHADER\n"
1788 "\n"
1789 "#endif // !MODE_DEFERREDLIGHTSOURCE\n"
1790 "#endif // !MODE_DEFERREDGEOMETRY\n"
1791 "#endif // !MODE_WATER\n"
1792 "#endif // !MODE_REFRACTION\n"
1793 "#endif // !MODE_BLOOMBLUR\n"
1794 "#endif // !MODE_GENERIC\n"
1795 "#endif // !MODE_POSTPROCESS\n"
1796 "#endif // !MODE_SHOWDEPTH\n"
1797 "#endif // !MODE_DEPTH_OR_SHADOW\n"
1798 ;
1799
1800 /*
1801 =========================================================================================================================================================
1802
1803
1804
1805 =========================================================================================================================================================
1806
1807
1808
1809 =========================================================================================================================================================
1810
1811
1812
1813 =========================================================================================================================================================
1814
1815
1816
1817 =========================================================================================================================================================
1818
1819
1820
1821 =========================================================================================================================================================
1822
1823
1824
1825 =========================================================================================================================================================
1826 */
1827
1828 const char *builtincgshaderstring =
1829 "// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader\n"
1830 "// written by Forest 'LordHavoc' Hale\n"
1831 "// shadowmapping enhancements by Lee 'eihrul' Salzman\n"
1832 "\n"
1833 "// FIXME: we need to get rid of ModelViewProjectionPosition to make room for the texcoord for this\n"
1834 "#if defined(USEREFLECTION)\n"
1835 "#undef USESHADOWMAPORTHO\n"
1836 "#endif\n"
1837 "\n"
1838 "#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE)\n"
1839 "# define USEFOG\n"
1840 "#endif\n"
1841 "#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)\n"
1842 "#define USELIGHTMAP\n"
1843 "#endif\n"
1844 "#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT)\n"
1845 "#define USEEYEVECTOR\n"
1846 "#endif\n"
1847 "\n"
1848 "#ifdef FRAGMENT_SHADER\n"
1849 "#ifdef HLSL\n"
1850 "//#undef USESHADOWMAPPCF\n"
1851 "//#define texDepth2D(tex,texcoord) tex2D(tex,texcoord).r\n"
1852 "#define texDepth2D(tex,texcoord) dot(tex2D(tex,texcoord).rgb, float3(1.0, 255.0/65536.0, 255.0/16777216.0))\n"
1853 "#else\n"
1854 "#define texDepth2D(tex,texcoord) tex2D(tex,texcoord).r\n"
1855 "#endif\n"
1856 "#endif\n"
1857 "\n"
1858 "#ifdef MODE_DEPTH_OR_SHADOW\n"
1859 "#ifdef VERTEX_SHADER\n"
1860 "void main\n"
1861 "(\n"
1862 "float4 gl_Vertex : POSITION,\n"
1863 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
1864 "out float4 gl_Position : POSITION,\n"
1865 "out float Depth : TEXCOORD0\n"
1866 ")\n"
1867 "{\n"
1868 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
1869 "       Depth = gl_Position.z;\n"
1870 "}\n"
1871 "#endif\n"
1872 "\n"
1873 "#ifdef FRAGMENT_SHADER\n"
1874 "void main\n"
1875 "(\n"
1876 "float Depth : TEXCOORD0,\n"
1877 "out float4 gl_FragColor : COLOR\n"
1878 ")\n"
1879 "{\n"
1880 "//     float4 temp = float4(Depth,Depth*(65536.0/255.0),Depth*(16777216.0/255.0),0.0);\n"
1881 "       float4 temp = float4(Depth,Depth*256.0,Depth*65536.0,0.0);\n"
1882 "       temp.yz -= floor(temp.yz);\n"
1883 "       gl_FragColor = temp;\n"
1884 "//     gl_FragColor = float4(Depth,0,0,0);\n"
1885 "}\n"
1886 "#endif\n"
1887 "#else // !MODE_DEPTH_ORSHADOW\n"
1888 "\n"
1889 "\n"
1890 "\n"
1891 "\n"
1892 "#ifdef MODE_SHOWDEPTH\n"
1893 "#ifdef VERTEX_SHADER\n"
1894 "void main\n"
1895 "(\n"
1896 "float4 gl_Vertex : POSITION,\n"
1897 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
1898 "out float4 gl_Position : POSITION,\n"
1899 "out float4 gl_FrontColor : COLOR0\n"
1900 ")\n"
1901 "{\n"
1902 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
1903 "       gl_FrontColor = float4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0);\n"
1904 "}\n"
1905 "#endif\n"
1906 "\n"
1907 "#ifdef FRAGMENT_SHADER\n"
1908 "void main\n"
1909 "(\n"
1910 "float4 gl_FrontColor : COLOR0,\n"
1911 "out float4 gl_FragColor : COLOR\n"
1912 ")\n"
1913 "{\n"
1914 "       gl_FragColor = gl_FrontColor;\n"
1915 "}\n"
1916 "#endif\n"
1917 "#else // !MODE_SHOWDEPTH\n"
1918 "\n"
1919 "\n"
1920 "\n"
1921 "\n"
1922 "#ifdef MODE_POSTPROCESS\n"
1923 "\n"
1924 "#ifdef VERTEX_SHADER\n"
1925 "void main\n"
1926 "(\n"
1927 "float4 gl_Vertex : POSITION,\n"
1928 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
1929 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
1930 "float4 gl_MultiTexCoord4 : TEXCOORD4,\n"
1931 "out float4 gl_Position : POSITION,\n"
1932 "out float2 TexCoord1 : TEXCOORD0,\n"
1933 "out float2 TexCoord2 : TEXCOORD1\n"
1934 ")\n"
1935 "{\n"
1936 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
1937 "       TexCoord1 = gl_MultiTexCoord0.xy;\n"
1938 "#ifdef USEBLOOM\n"
1939 "       TexCoord2 = gl_MultiTexCoord4.xy;\n"
1940 "#endif\n"
1941 "}\n"
1942 "#endif\n"
1943 "\n"
1944 "#ifdef FRAGMENT_SHADER\n"
1945 "void main\n"
1946 "(\n"
1947 "float2 TexCoord1 : TEXCOORD0,\n"
1948 "float2 TexCoord2 : TEXCOORD1,\n"
1949 "uniform sampler Texture_First : register(s0),\n"
1950 "#ifdef USEBLOOM\n"
1951 "uniform sampler Texture_Second : register(s1),\n"
1952 "#endif\n"
1953 "#ifdef USEGAMMARAMPS\n"
1954 "uniform sampler Texture_GammaRamps : register(s2),\n"
1955 "#endif\n"
1956 "#ifdef USESATURATION\n"
1957 "uniform float Saturation : register(c30),\n"
1958 "#endif\n"
1959 "#ifdef USEVIEWTINT\n"
1960 "uniform float4 ViewTintColor : register(c41),\n"
1961 "#endif\n"
1962 "uniform float4 UserVec1 : register(c37),\n"
1963 "uniform float4 UserVec2 : register(c38),\n"
1964 "uniform float4 UserVec3 : register(c39),\n"
1965 "uniform float4 UserVec4 : register(c40),\n"
1966 "uniform float ClientTime : register(c2),\n"
1967 "uniform float2 PixelSize : register(c25),\n"
1968 "uniform float4 BloomColorSubtract : register(c43),\n"
1969 "out float4 gl_FragColor : COLOR\n"
1970 ")\n"
1971 "{\n"
1972 "       gl_FragColor = tex2D(Texture_First, TexCoord1);\n"
1973 "#ifdef USEBLOOM\n"
1974 "       gl_FragColor += max(float4(0,0,0,0), tex2D(Texture_Second, TexCoord2) - BloomColorSubtract);\n"
1975 "#endif\n"
1976 "#ifdef USEVIEWTINT\n"
1977 "       gl_FragColor = lerp(gl_FragColor, ViewTintColor, ViewTintColor.a);\n"
1978 "#endif\n"
1979 "\n"
1980 "#ifdef USEPOSTPROCESSING\n"
1981 "// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want\n"
1982 "// 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"
1983 "       float sobel = 1.0;\n"
1984 "       // float2 ts = textureSize(Texture_First, 0);\n"
1985 "       // float2 px = float2(1/ts.x, 1/ts.y);\n"
1986 "       float2 px = PixelSize;\n"
1987 "       float3 x1 = tex2D(Texture_First, TexCoord1 + float2(-px.x, px.y)).rgb;\n"
1988 "       float3 x2 = tex2D(Texture_First, TexCoord1 + float2(-px.x,  0.0)).rgb;\n"
1989 "       float3 x3 = tex2D(Texture_First, TexCoord1 + float2(-px.x,-px.y)).rgb;\n"
1990 "       float3 x4 = tex2D(Texture_First, TexCoord1 + float2( px.x, px.y)).rgb;\n"
1991 "       float3 x5 = tex2D(Texture_First, TexCoord1 + float2( px.x,  0.0)).rgb;\n"
1992 "       float3 x6 = tex2D(Texture_First, TexCoord1 + float2( px.x,-px.y)).rgb;\n"
1993 "       float3 y1 = tex2D(Texture_First, TexCoord1 + float2( px.x,-px.y)).rgb;\n"
1994 "       float3 y2 = tex2D(Texture_First, TexCoord1 + float2(  0.0,-px.y)).rgb;\n"
1995 "       float3 y3 = tex2D(Texture_First, TexCoord1 + float2(-px.x,-px.y)).rgb;\n"
1996 "       float3 y4 = tex2D(Texture_First, TexCoord1 + float2( px.x, px.y)).rgb;\n"
1997 "       float3 y5 = tex2D(Texture_First, TexCoord1 + float2(  0.0, px.y)).rgb;\n"
1998 "       float3 y6 = tex2D(Texture_First, TexCoord1 + float2(-px.x, px.y)).rgb;\n"
1999 "       float px1 = -1.0 * dot(float3(0.3, 0.59, 0.11), x1);\n"
2000 "       float px2 = -2.0 * dot(float3(0.3, 0.59, 0.11), x2);\n"
2001 "       float px3 = -1.0 * dot(float3(0.3, 0.59, 0.11), x3);\n"
2002 "       float px4 =  1.0 * dot(float3(0.3, 0.59, 0.11), x4);\n"
2003 "       float px5 =  2.0 * dot(float3(0.3, 0.59, 0.11), x5);\n"
2004 "       float px6 =  1.0 * dot(float3(0.3, 0.59, 0.11), x6);\n"
2005 "       float py1 = -1.0 * dot(float3(0.3, 0.59, 0.11), y1);\n"
2006 "       float py2 = -2.0 * dot(float3(0.3, 0.59, 0.11), y2);\n"
2007 "       float py3 = -1.0 * dot(float3(0.3, 0.59, 0.11), y3);\n"
2008 "       float py4 =  1.0 * dot(float3(0.3, 0.59, 0.11), y4);\n"
2009 "       float py5 =  2.0 * dot(float3(0.3, 0.59, 0.11), y5);\n"
2010 "       float py6 =  1.0 * dot(float3(0.3, 0.59, 0.11), y6);\n"
2011 "       sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6);\n"
2012 "       gl_FragColor += tex2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*float2(-0.987688, -0.156434)) * UserVec1.y;\n"
2013 "       gl_FragColor += tex2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*float2(-0.156434, -0.891007)) * UserVec1.y;\n"
2014 "       gl_FragColor += tex2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*float2( 0.891007, -0.453990)) * UserVec1.y;\n"
2015 "       gl_FragColor += tex2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*float2( 0.707107,  0.707107)) * UserVec1.y;\n"
2016 "       gl_FragColor += tex2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*float2(-0.453990,  0.891007)) * UserVec1.y;\n"
2017 "       gl_FragColor /= (1.0 + 5.0 * UserVec1.y);\n"
2018 "       gl_FragColor.rgb = gl_FragColor.rgb * (1.0 + UserVec2.x) + float3(1,1,1)*max(0.0, sobel - UserVec2.z)*UserVec2.y;\n"
2019 "#endif\n"
2020 "\n"
2021 "#ifdef USESATURATION\n"
2022 "       //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter\n"
2023 "       float y = dot(gl_FragColor.rgb, float3(0.299, 0.587, 0.114));\n"
2024 "       // 'vampire sight' effect, wheres red is compensated\n"
2025 "       #ifdef SATURATION_REDCOMPENSATE\n"
2026 "               float rboost = max(0.0, (gl_FragColor.r - max(gl_FragColor.g, gl_FragColor.b))*(1.0 - Saturation));\n"
2027 "               gl_FragColor.rgb = mix(float3(y,y,y), gl_FragColor.rgb, Saturation);\n"
2028 "               gl_FragColor.r += r;\n"
2029 "       #else\n"
2030 "               // normal desaturation\n"
2031 "               //gl_FragColor = float3(y,y,y) + (gl_FragColor.rgb - float3(y)) * Saturation;\n"
2032 "               gl_FragColor.rgb = lerp(float3(y,y,y), gl_FragColor.rgb, Saturation);\n"
2033 "       #endif\n"
2034 "#endif\n"
2035 "\n"
2036 "#ifdef USEGAMMARAMPS\n"
2037 "       gl_FragColor.r = tex2D(Texture_GammaRamps, float2(gl_FragColor.r, 0)).r;\n"
2038 "       gl_FragColor.g = tex2D(Texture_GammaRamps, float2(gl_FragColor.g, 0)).g;\n"
2039 "       gl_FragColor.b = tex2D(Texture_GammaRamps, float2(gl_FragColor.b, 0)).b;\n"
2040 "#endif\n"
2041 "}\n"
2042 "#endif\n"
2043 "#else // !MODE_POSTPROCESS\n"
2044 "\n"
2045 "\n"
2046 "\n"
2047 "\n"
2048 "#ifdef MODE_GENERIC\n"
2049 "#ifdef VERTEX_SHADER\n"
2050 "void main\n"
2051 "(\n"
2052 "float4 gl_Vertex : POSITION,\n"
2053 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
2054 "float4 gl_Color : COLOR0,\n"
2055 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
2056 "float4 gl_MultiTexCoord1 : TEXCOORD1,\n"
2057 "out float4 gl_Position : POSITION,\n"
2058 "#ifdef USEDIFFUSE\n"
2059 "out float2 TexCoord1 : TEXCOORD0,\n"
2060 "#endif\n"
2061 "#ifdef USESPECULAR\n"
2062 "out float2 TexCoord2 : TEXCOORD1,\n"
2063 "#endif\n"
2064 "out float4 gl_FrontColor : COLOR\n"
2065 ")\n"
2066 "{\n"
2067 "#ifdef HLSL\n"
2068 "       gl_FrontColor = gl_Color.bgra; // NOTE: D3DCOLOR is backwards\n"
2069 "#else\n"
2070 "       gl_FrontColor = gl_Color; // Cg is forward\n"
2071 "#endif\n"
2072 "#ifdef USEDIFFUSE\n"
2073 "       TexCoord1 = gl_MultiTexCoord0.xy;\n"
2074 "#endif\n"
2075 "#ifdef USESPECULAR\n"
2076 "       TexCoord2 = gl_MultiTexCoord1.xy;\n"
2077 "#endif\n"
2078 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
2079 "}\n"
2080 "#endif\n"
2081 "\n"
2082 "#ifdef FRAGMENT_SHADER\n"
2083 "\n"
2084 "void main\n"
2085 "(\n"
2086 "float4 gl_FrontColor : COLOR0,\n"
2087 "float2 TexCoord1 : TEXCOORD0,\n"
2088 "float2 TexCoord2 : TEXCOORD1,\n"
2089 "#ifdef USEDIFFUSE\n"
2090 "uniform sampler Texture_First : register(s0),\n"
2091 "#endif\n"
2092 "#ifdef USESPECULAR\n"
2093 "uniform sampler Texture_Second : register(s1),\n"
2094 "#endif\n"
2095 "out float4 gl_FragColor : COLOR\n"
2096 ")\n"
2097 "{\n"
2098 "#ifdef USEVIEWTINT\n"
2099 "       gl_FragColor = gl_FrontColor;\n"
2100 "#else\n"
2101 "       gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
2102 "#endif\n"
2103 "#ifdef USEDIFFUSE\n"
2104 "       gl_FragColor *= tex2D(Texture_First, TexCoord1);\n"
2105 "#endif\n"
2106 "\n"
2107 "#ifdef USESPECULAR\n"
2108 "       float4 tex2 = tex2D(Texture_Second, TexCoord2);\n"
2109 "# ifdef USECOLORMAPPING\n"
2110 "       gl_FragColor *= tex2;\n"
2111 "# endif\n"
2112 "# ifdef USEGLOW\n"
2113 "       gl_FragColor += tex2;\n"
2114 "# endif\n"
2115 "# ifdef USEVERTEXTEXTUREBLEND\n"
2116 "       gl_FragColor = lerp(gl_FragColor, tex2, tex2.a);\n"
2117 "# endif\n"
2118 "#endif\n"
2119 "}\n"
2120 "#endif\n"
2121 "#else // !MODE_GENERIC\n"
2122 "\n"
2123 "\n"
2124 "\n"
2125 "\n"
2126 "#ifdef MODE_BLOOMBLUR\n"
2127 "#ifdef VERTEX_SHADER\n"
2128 "void main\n"
2129 "(\n"
2130 "float4 gl_Vertex : POSITION,\n"
2131 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
2132 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
2133 "out float4 gl_Position : POSITION,\n"
2134 "out float2 TexCoord : TEXCOORD0\n"
2135 ")\n"
2136 "{\n"
2137 "       TexCoord = gl_MultiTexCoord0.xy;\n"
2138 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
2139 "}\n"
2140 "#endif\n"
2141 "\n"
2142 "#ifdef FRAGMENT_SHADER\n"
2143 "\n"
2144 "void main\n"
2145 "(\n"
2146 "float2 TexCoord : TEXCOORD0,\n"
2147 "uniform sampler Texture_First : register(s0),\n"
2148 "uniform float4 BloomBlur_Parameters : register(c1),\n"
2149 "out float4 gl_FragColor : COLOR\n"
2150 ")\n"
2151 "{\n"
2152 "       int i;\n"
2153 "       float2 tc = TexCoord;\n"
2154 "       float3 color = tex2D(Texture_First, tc).rgb;\n"
2155 "       tc += BloomBlur_Parameters.xy;\n"
2156 "       for (i = 1;i < SAMPLES;i++)\n"
2157 "       {\n"
2158 "               color += tex2D(Texture_First, tc).rgb;\n"
2159 "               tc += BloomBlur_Parameters.xy;\n"
2160 "       }\n"
2161 "       gl_FragColor = float4(color * BloomBlur_Parameters.z + float3(BloomBlur_Parameters.w), 1);\n"
2162 "}\n"
2163 "#endif\n"
2164 "#else // !MODE_BLOOMBLUR\n"
2165 "#ifdef MODE_REFRACTION\n"
2166 "#ifdef VERTEX_SHADER\n"
2167 "void main\n"
2168 "(\n"
2169 "float4 gl_Vertex : POSITION,\n"
2170 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
2171 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
2172 "uniform float4x4 TexMatrix : register(c0),\n"
2173 "uniform float3 EyePosition : register(c24),\n"
2174 "out float4 gl_Position : POSITION,\n"
2175 "out float2 TexCoord : TEXCOORD0,\n"
2176 "out float3 EyeVector : TEXCOORD1,\n"
2177 "out float4 ModelViewProjectionPosition : TEXCOORD2\n"
2178 ")\n"
2179 "{\n"
2180 "       TexCoord = mul(TexMatrix, gl_MultiTexCoord0).xy;\n"
2181 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
2182 "       ModelViewProjectionPosition = gl_Position;\n"
2183 "}\n"
2184 "#endif\n"
2185 "\n"
2186 "#ifdef FRAGMENT_SHADER\n"
2187 "void main\n"
2188 "(\n"
2189 "float2 TexCoord : TEXCOORD0,\n"
2190 "float3 EyeVector : TEXCOORD1,\n"
2191 "float4 ModelViewProjectionPosition : TEXCOORD2,\n"
2192 "uniform sampler Texture_Normal : register(s0),\n"
2193 "uniform sampler Texture_Refraction : register(s3),\n"
2194 "uniform sampler Texture_Reflection : register(s7),\n"
2195 "uniform float4 DistortScaleRefractReflect : register(c14),\n"
2196 "uniform float4 ScreenScaleRefractReflect : register(c32),\n"
2197 "uniform float4 ScreenCenterRefractReflect : register(c31),\n"
2198 "uniform float4 RefractColor : register(c29),\n"
2199 "out float4 gl_FragColor : COLOR\n"
2200 ")\n"
2201 "{\n"
2202 "       float2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w);\n"
2203 "       //float2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(tex2D(Texture_Normal, TexCoord).rgb - float3(0.5,0.5,0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
2204 "       float2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
2205 "       float2 ScreenTexCoord = SafeScreenTexCoord + normalize(tex2D(Texture_Normal, TexCoord).rgb - float3(0.5,0.5,0.5)).xy * DistortScaleRefractReflect.xy;\n"
2206 "       // FIXME temporary hack to detect the case that the reflection\n"
2207 "       // gets blackened at edges due to leaving the area that contains actual\n"
2208 "       // content.\n"
2209 "       // Remove this 'ack once we have a better way to stop this thing from\n"
2210 "       // 'appening.\n"
2211 "       float f = min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord + float2(0.01, 0.01)).rgb) / 0.05);\n"
2212 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord + float2(0.01, -0.01)).rgb) / 0.05);\n"
2213 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord + float2(-0.01, 0.01)).rgb) / 0.05);\n"
2214 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord + float2(-0.01, -0.01)).rgb) / 0.05);\n"
2215 "       ScreenTexCoord = lerp(SafeScreenTexCoord, ScreenTexCoord, f);\n"
2216 "       gl_FragColor = float4(tex2D(Texture_Refraction, ScreenTexCoord).rgb, 1) * RefractColor;\n"
2217 "}\n"
2218 "#endif\n"
2219 "#else // !MODE_REFRACTION\n"
2220 "\n"
2221 "\n"
2222 "\n"
2223 "\n"
2224 "#ifdef MODE_WATER\n"
2225 "#ifdef VERTEX_SHADER\n"
2226 "\n"
2227 "void main\n"
2228 "(\n"
2229 "float4 gl_Vertex : POSITION,\n"
2230 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
2231 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
2232 "float4 gl_MultiTexCoord1 : TEXCOORD1,\n"
2233 "float4 gl_MultiTexCoord2 : TEXCOORD2,\n"
2234 "float4 gl_MultiTexCoord3 : TEXCOORD3,\n"
2235 "uniform float4x4 TexMatrix : register(c0),\n"
2236 "uniform float3 EyePosition : register(c24),\n"
2237 "out float4 gl_Position : POSITION,\n"
2238 "out float2 TexCoord : TEXCOORD0,\n"
2239 "out float3 EyeVector : TEXCOORD1,\n"
2240 "out float4 ModelViewProjectionPosition : TEXCOORD2\n"
2241 ")\n"
2242 "{\n"
2243 "       TexCoord = mul(TexMatrix, gl_MultiTexCoord0).xy;\n"
2244 "       float3 EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
2245 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
2246 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
2247 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
2248 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
2249 "       ModelViewProjectionPosition = gl_Position;\n"
2250 "}\n"
2251 "#endif\n"
2252 "\n"
2253 "#ifdef FRAGMENT_SHADER\n"
2254 "void main\n"
2255 "(\n"
2256 "float2 TexCoord : TEXCOORD0,\n"
2257 "float3 EyeVector : TEXCOORD1,\n"
2258 "float4 ModelViewProjectionPosition : TEXCOORD2,\n"
2259 "uniform sampler Texture_Normal : register(s0),\n"
2260 "uniform sampler Texture_Refraction : register(s3),\n"
2261 "uniform sampler Texture_Reflection : register(s7),\n"
2262 "uniform float4 DistortScaleRefractReflect : register(c14),\n"
2263 "uniform float4 ScreenScaleRefractReflect : register(c32),\n"
2264 "uniform float4 ScreenCenterRefractReflect : register(c31),\n"
2265 "uniform float4 RefractColor : register(c29),\n"
2266 "uniform float4 ReflectColor : register(c26),\n"
2267 "uniform float ReflectFactor : register(c27),\n"
2268 "uniform float ReflectOffset : register(c28),\n"
2269 "out float4 gl_FragColor : COLOR\n"
2270 ")\n"
2271 "{\n"
2272 "       float4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
2273 "       //float4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(tex2D(Texture_Normal, TexCoord).rgb - float3(0.5,0.5,0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
2274 "       float4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
2275 "       //SafeScreenTexCoord = gl_FragCoord.xyxy * float4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0);\n"
2276 "       float4 ScreenTexCoord = SafeScreenTexCoord + float2(normalize(tex2D(Texture_Normal, TexCoord).rgb - float3(0.5,0.5,0.5)).xy).xyxy * DistortScaleRefractReflect;\n"
2277 "       // FIXME temporary hack to detect the case that the reflection\n"
2278 "       // gets blackened at edges due to leaving the area that contains actual\n"
2279 "       // content.\n"
2280 "       // Remove this 'ack once we have a better way to stop this thing from\n"
2281 "       // 'appening.\n"
2282 "       float f = min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord.xy + float2(0.01, 0.01)).rgb) / 0.05);\n"
2283 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord.xy + float2(0.01, -0.01)).rgb) / 0.05);\n"
2284 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord.xy + float2(-0.01, 0.01)).rgb) / 0.05);\n"
2285 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord.xy + float2(-0.01, -0.01)).rgb) / 0.05);\n"
2286 "       ScreenTexCoord.xy = lerp(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f);\n"
2287 "       f  = min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord.zw + float2(0.01, 0.01)).rgb) / 0.05);\n"
2288 "       f *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord.zw + float2(0.01, -0.01)).rgb) / 0.05);\n"
2289 "       f *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord.zw + float2(-0.01, 0.01)).rgb) / 0.05);\n"
2290 "       f *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord.zw + float2(-0.01, -0.01)).rgb) / 0.05);\n"
2291 "       ScreenTexCoord.zw = lerp(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f);\n"
2292 "       float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset;\n"
2293 "       gl_FragColor = lerp(float4(tex2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * RefractColor, float4(tex2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel);\n"
2294 "}\n"
2295 "#endif\n"
2296 "#else // !MODE_WATER\n"
2297 "\n"
2298 "\n"
2299 "\n"
2300 "\n"
2301 "// 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"
2302 "\n"
2303 "// fragment shader specific:\n"
2304 "#ifdef FRAGMENT_SHADER\n"
2305 "\n"
2306 "#ifdef USEFOG\n"
2307 "float3 FogVertex(float3 surfacecolor, float3 FogColor, float3 EyeVectorModelSpace, float FogPlaneVertexDist, float FogRangeRecip, float FogPlaneViewDist, float FogHeightFade, sampler Texture_FogMask, sampler Texture_FogHeightTexture)\n"
2308 "{\n"
2309 "       float fogfrac;\n"
2310 "#ifdef USEFOGHEIGHTTEXTURE\n"
2311 "       float4 fogheightpixel = tex2D(Texture_FogHeightTexture, float2(1,1) + float2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade));\n"
2312 "       fogfrac = fogheightpixel.a;\n"
2313 "       return lerp(fogheightpixel.rgb * FogColor, surfacecolor, tex2D(Texture_FogMask, float2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r);\n"
2314 "#else\n"
2315 "# ifdef USEFOGOUTSIDE\n"
2316 "       fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade);\n"
2317 "# else\n"
2318 "       fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);\n"
2319 "# endif\n"
2320 "       return lerp(FogColor, surfacecolor, tex2D(Texture_FogMask, float2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r);\n"
2321 "#endif\n"
2322 "}\n"
2323 "#endif\n"
2324 "\n"
2325 "#ifdef USEOFFSETMAPPING\n"
2326 "float2 OffsetMapping(float2 TexCoord, float OffsetMapping_Scale, float3 EyeVector, sampler Texture_Normal)\n"
2327 "{\n"
2328 "#ifdef USEOFFSETMAPPING_RELIEFMAPPING\n"
2329 "       // 14 sample relief mapping: linear search and then binary search\n"
2330 "       // this basically steps forward a small amount repeatedly until it finds\n"
2331 "       // itself inside solid, then jitters forward and back using decreasing\n"
2332 "       // amounts to find the impact\n"
2333 "       //float3 OffsetVector = float3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * float2(-1, 1), -1);\n"
2334 "       //float3 OffsetVector = float3(normalize(EyeVector.xy) * OffsetMapping_Scale * float2(-1, 1), -1);\n"
2335 "       float3 OffsetVector = float3(normalize(EyeVector).xy * OffsetMapping_Scale * float2(-1, 1), -1);\n"
2336 "       float3 RT = float3(TexCoord, 1);\n"
2337 "       OffsetVector *= 0.1;\n"
2338 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2339 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2340 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2341 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2342 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2343 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2344 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2345 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2346 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2347 "       RT += OffsetVector * (step(tex2D(Texture_Normal, RT.xy).a, RT.z)          - 0.5);\n"
2348 "       RT += OffsetVector * (step(tex2D(Texture_Normal, RT.xy).a, RT.z) * 0.5    - 0.25);\n"
2349 "       RT += OffsetVector * (step(tex2D(Texture_Normal, RT.xy).a, RT.z) * 0.25   - 0.125);\n"
2350 "       RT += OffsetVector * (step(tex2D(Texture_Normal, RT.xy).a, RT.z) * 0.125  - 0.0625);\n"
2351 "       RT += OffsetVector * (step(tex2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125);\n"
2352 "       return RT.xy;\n"
2353 "#else\n"
2354 "       // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits)\n"
2355 "       // this basically moves forward the full distance, and then backs up based\n"
2356 "       // on height of samples\n"
2357 "       //float2 OffsetVector = float2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * float2(-1, 1));\n"
2358 "       //float2 OffsetVector = float2(normalize(EyeVector.xy) * OffsetMapping_Scale * float2(-1, 1));\n"
2359 "       float2 OffsetVector = float2(normalize(EyeVector).xy * OffsetMapping_Scale * float2(-1, 1));\n"
2360 "       TexCoord += OffsetVector;\n"
2361 "       OffsetVector *= 0.333;\n"
2362 "       TexCoord -= OffsetVector * tex2D(Texture_Normal, TexCoord).a;\n"
2363 "       TexCoord -= OffsetVector * tex2D(Texture_Normal, TexCoord).a;\n"
2364 "       TexCoord -= OffsetVector * tex2D(Texture_Normal, TexCoord).a;\n"
2365 "       return TexCoord;\n"
2366 "#endif\n"
2367 "}\n"
2368 "#endif // USEOFFSETMAPPING\n"
2369 "\n"
2370 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO)\n"
2371 "#if defined(USESHADOWMAP2D)\n"
2372 "# ifdef USESHADOWMAPORTHO\n"
2373 "#  define GetShadowMapTC2D(dir, ShadowMap_Parameters) (min(dir, ShadowMap_Parameters.xyz))\n"
2374 "# else\n"
2375 "#  ifdef USESHADOWMAPVSDCT\n"
2376 "float3 GetShadowMapTC2D(float3 dir, float4 ShadowMap_Parameters, samplerCUBE Texture_CubeProjection)\n"
2377 "{\n"
2378 "       float3 adir = abs(dir);\n"
2379 "       float2 aparams = ShadowMap_Parameters.xy / max(max(adir.x, adir.y), adir.z);\n"
2380 "       float4 proj = texCUBE(Texture_CubeProjection, dir);\n"
2381 "       return float3(lerp(dir.xy, dir.zz, proj.xy) * aparams.x + proj.zw * ShadowMap_Parameters.z, aparams.y + ShadowMap_Parameters.w);\n"
2382 "}\n"
2383 "#  else\n"
2384 "float3 GetShadowMapTC2D(float3 dir, float4 ShadowMap_Parameters)\n"
2385 "{\n"
2386 "       float3 adir = abs(dir);\n"
2387 "       float ma = adir.z;\n"
2388 "       float4 proj = float4(dir, 2.5);\n"
2389 "       if (adir.x > ma) { ma = adir.x; proj = float4(dir.zyx, 0.5); }\n"
2390 "       if (adir.y > ma) { ma = adir.y; proj = float4(dir.xzy, 1.5); }\n"
2391 "#ifdef HLSL\n"
2392 "       return float3(proj.xy * ShadowMap_Parameters.x / ma + float2(0.5,0.5) + float2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, ma + 64 * ShadowMap_Parameters.w);\n"
2393 "#else\n"
2394 "       float2 aparams = ShadowMap_Parameters.xy / ma;\n"
2395 "       return float3(proj.xy * aparams.x + float2(proj.z < 0.0 ? 1.5 : 0.5, proj.w) * ShadowMap_Parameters.z, aparams.y + ShadowMap_Parameters.w);\n"
2396 "#endif\n"
2397 "}\n"
2398 "#  endif\n"
2399 "# endif\n"
2400 "#endif // defined(USESHADOWMAP2D)\n"
2401 "\n"
2402 "# ifdef USESHADOWMAP2D\n"
2403 "#ifdef USESHADOWMAPVSDCT\n"
2404 "float ShadowMapCompare(float3 dir, sampler Texture_ShadowMap2D, float4 ShadowMap_Parameters, float2 ShadowMap_TextureScale, samplerCUBE Texture_CubeProjection)\n"
2405 "#else\n"
2406 "float ShadowMapCompare(float3 dir, sampler Texture_ShadowMap2D, float4 ShadowMap_Parameters, float2 ShadowMap_TextureScale)\n"
2407 "#endif\n"
2408 "{\n"
2409 "#ifdef USESHADOWMAPVSDCT\n"
2410 "       float3 shadowmaptc = GetShadowMapTC2D(dir, ShadowMap_Parameters, Texture_CubeProjection);\n"
2411 "#else\n"
2412 "       float3 shadowmaptc = GetShadowMapTC2D(dir, ShadowMap_Parameters);\n"
2413 "#endif\n"
2414 "       float f;\n"
2415 "\n"
2416 "#  ifdef USESHADOWSAMPLER\n"
2417 "#    ifdef USESHADOWMAPPCF\n"
2418 "#      define texval(x, y) tex2Dproj(Texture_ShadowMap2D, float4(center + float2(x, y)*ShadowMap_TextureScale, shadowmaptc.z, 1.0)).r  \n"
2419 "       float2 center = shadowmaptc.xy*ShadowMap_TextureScale;\n"
2420 "       f = dot(float4(0.25,0.25,0.25,0.25), float4(texval(-0.4, 1.0), texval(-1.0, -0.4), texval(0.4, -1.0), texval(1.0, 0.4)));\n"
2421 "#    else\n"
2422 "       f = tex2Dproj(Texture_ShadowMap2D, float4(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z, 1.0)).r;\n"
2423 "#    endif\n"
2424 "#  else\n"
2425 "#    ifdef USESHADOWMAPPCF\n"
2426 "#     if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4)\n"
2427 "#      ifdef GL_ARB_texture_gather\n"
2428 "#        define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, int2(x, y))\n"
2429 "#      else\n"
2430 "#        define texval(x, y) texture4(Texture_ShadowMap2D, center + float2(x, y)*ShadowMap_TextureScale)\n"
2431 "#      endif\n"
2432 "       float2 offset = frac(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale;\n"
2433 "#      if USESHADOWMAPPCF > 1\n"
2434 "       float4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0));\n"
2435 "       float4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0));\n"
2436 "       float4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0));\n"
2437 "       float4 group4 = step(shadowmaptc.z, texval(-2.0,  0.0));\n"
2438 "       float4 group5 = step(shadowmaptc.z, texval( 0.0,  0.0));\n"
2439 "       float4 group6 = step(shadowmaptc.z, texval( 2.0,  0.0));\n"
2440 "       float4 group7 = step(shadowmaptc.z, texval(-2.0,  2.0));\n"
2441 "       float4 group8 = step(shadowmaptc.z, texval( 0.0,  2.0));\n"
2442 "       float4 group9 = step(shadowmaptc.z, texval( 2.0,  2.0));\n"
2443 "       float4 locols = float4(group1.ab, group3.ab);\n"
2444 "       float4 hicols = float4(group7.rg, group9.rg);\n"
2445 "       locols.yz += group2.ab;\n"
2446 "       hicols.yz += group8.rg;\n"
2447 "       float4 midcols = float4(group1.rg, group3.rg) + float4(group7.ab, group9.ab) +\n"
2448 "                               float4(group4.rg, group6.rg) + float4(group4.ab, group6.ab) +\n"
2449 "                               lerp(locols, hicols, offset.y);\n"
2450 "       float4 cols = group5 + float4(group2.rg, group8.ab);\n"
2451 "       cols.xyz += lerp(midcols.xyz, midcols.yzw, offset.x);\n"
2452 "       f = dot(cols, float4(1.0/25.0));\n"
2453 "#      else\n"
2454 "       float4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0));\n"
2455 "       float4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0));\n"
2456 "       float4 group3 = step(shadowmaptc.z, texval(-1.0,  1.0));\n"
2457 "       float4 group4 = step(shadowmaptc.z, texval( 1.0,  1.0));\n"
2458 "       float4 cols = float4(group1.rg, group2.rg) + float4(group3.ab, group4.ab) +\n"
2459 "                               lerp(float4(group1.ab, group2.ab), float4(group3.rg, group4.rg), offset.y);\n"
2460 "       f = dot(lerp(cols.xyz, cols.yzw, offset.x), float3(1.0/9.0));\n"
2461 "#      endif\n"
2462 "#     else\n"
2463 "#      ifdef GL_EXT_gpu_shader4\n"
2464 "#        define texval(x, y) tex2DOffset(Texture_ShadowMap2D, center, int2(x, y)).r\n"
2465 "#      else\n"
2466 "#        define texval(x, y) texDepth2D(Texture_ShadowMap2D, center + float2(x, y)*ShadowMap_TextureScale).r  \n"
2467 "#      endif\n"
2468 "#      if USESHADOWMAPPCF > 1\n"
2469 "       float2 center = shadowmaptc.xy - 0.5, offset = frac(center);\n"
2470 "       center *= ShadowMap_TextureScale;\n"
2471 "       float4 row1 = step(shadowmaptc.z, float4(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0), texval( 2.0, -1.0)));\n"
2472 "       float4 row2 = step(shadowmaptc.z, float4(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0), texval( 2.0,  0.0)));\n"
2473 "       float4 row3 = step(shadowmaptc.z, float4(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0), texval( 2.0,  1.0)));\n"
2474 "       float4 row4 = step(shadowmaptc.z, float4(texval(-1.0,  2.0), texval( 0.0,  2.0), texval( 1.0,  2.0), texval( 2.0,  2.0)));\n"
2475 "       float4 cols = row2 + row3 + lerp(row1, row4, offset.y);\n"
2476 "       f = dot(lerp(cols.xyz, cols.yzw, offset.x), float3(1.0/9.0));\n"
2477 "#      else\n"
2478 "       float2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = frac(shadowmaptc.xy);\n"
2479 "       float3 row1 = step(shadowmaptc.z, float3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));\n"
2480 "       float3 row2 = step(shadowmaptc.z, float3(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0)));\n"
2481 "       float3 row3 = step(shadowmaptc.z, float3(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0)));\n"
2482 "       float3 cols = row2 + lerp(row1, row3, offset.y);\n"
2483 "       f = dot(lerp(cols.xy, cols.yz, offset.x), float2(0.25,0.25));\n"
2484 "#      endif\n"
2485 "#     endif\n"
2486 "#    else\n"
2487 "       f = step(shadowmaptc.z, tex2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r);\n"
2488 "#    endif\n"
2489 "#  endif\n"
2490 "#  ifdef USESHADOWMAPORTHO\n"
2491 "       return lerp(ShadowMap_Parameters.w, 1.0, f);\n"
2492 "#  else\n"
2493 "       return f;\n"
2494 "#  endif\n"
2495 "}\n"
2496 "# endif\n"
2497 "#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO)\n"
2498 "#endif // FRAGMENT_SHADER\n"
2499 "\n"
2500 "\n"
2501 "\n"
2502 "\n"
2503 "#ifdef MODE_DEFERREDGEOMETRY\n"
2504 "#ifdef VERTEX_SHADER\n"
2505 "void main\n"
2506 "(\n"
2507 "float4 gl_Vertex : POSITION,\n"
2508 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
2509 "#ifdef USEVERTEXTEXTUREBLEND\n"
2510 "float4 gl_Color : COLOR0,\n"
2511 "#endif\n"
2512 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
2513 "float4 gl_MultiTexCoord1 : TEXCOORD1,\n"
2514 "float4 gl_MultiTexCoord2 : TEXCOORD2,\n"
2515 "float4 gl_MultiTexCoord3 : TEXCOORD3,\n"
2516 "uniform float4x4 TexMatrix : register(c0),\n"
2517 "#ifdef USEVERTEXTEXTUREBLEND\n"
2518 "uniform float4x4 BackgroundTexMatrix : register(c4),\n"
2519 "#endif\n"
2520 "uniform float4x4 ModelViewMatrix : register(c12),\n"
2521 "#ifdef USEOFFSETMAPPING\n"
2522 "uniform float3 EyePosition : register(c24),\n"
2523 "#endif\n"
2524 "out float4 gl_Position : POSITION,\n"
2525 "#ifdef USEVERTEXTEXTUREBLEND\n"
2526 "out float4 gl_FrontColor : COLOR,\n"
2527 "#endif\n"
2528 "out float4 TexCoordBoth : TEXCOORD0,\n"
2529 "#ifdef USEOFFSETMAPPING\n"
2530 "out float3 EyeVector : TEXCOORD2,\n"
2531 "#endif\n"
2532 "out float3 VectorS : TEXCOORD5, // direction of S texcoord (sometimes crudely called tangent)\n"
2533 "out float3 VectorT : TEXCOORD6, // direction of T texcoord (sometimes crudely called binormal)\n"
2534 "out float4 VectorR : TEXCOORD7 // direction of R texcoord (surface normal), Depth value\n"
2535 ")\n"
2536 "{\n"
2537 "       TexCoordBoth = mul(TexMatrix, gl_MultiTexCoord0);\n"
2538 "#ifdef USEVERTEXTEXTUREBLEND\n"
2539 "#ifdef HLSL\n"
2540 "       gl_FrontColor = gl_Color.bgra; // NOTE: D3DCOLOR is backwards\n"
2541 "#else\n"
2542 "       gl_FrontColor = gl_Color; // Cg is forward\n"
2543 "#endif\n"
2544 "       TexCoordBoth.zw = float2(Backgroundmul(TexMatrix, gl_MultiTexCoord0));\n"
2545 "#endif\n"
2546 "\n"
2547 "       // transform unnormalized eye direction into tangent space\n"
2548 "#ifdef USEOFFSETMAPPING\n"
2549 "       float3 EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
2550 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
2551 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
2552 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
2553 "#endif\n"
2554 "\n"
2555 "       VectorS = mul(ModelViewMatrix, float4(gl_MultiTexCoord1.xyz, 0)).xyz;\n"
2556 "       VectorT = mul(ModelViewMatrix, float4(gl_MultiTexCoord2.xyz, 0)).xyz;\n"
2557 "       VectorR.xyz = mul(ModelViewMatrix, float4(gl_MultiTexCoord3.xyz, 0)).xyz;\n"
2558 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
2559 "       VectorR.w = gl_Position.z;\n"
2560 "}\n"
2561 "#endif // VERTEX_SHADER\n"
2562 "\n"
2563 "#ifdef FRAGMENT_SHADER\n"
2564 "void main\n"
2565 "(\n"
2566 "float4 TexCoordBoth : TEXCOORD0,\n"
2567 "float3 EyeVector : TEXCOORD2,\n"
2568 "float3 VectorS : TEXCOORD5, // direction of S texcoord (sometimes crudely called tangent)\n"
2569 "float3 VectorT : TEXCOORD6, // direction of T texcoord (sometimes crudely called binormal)\n"
2570 "float4 VectorR : TEXCOORD7, // direction of R texcoord (surface normal), Depth value\n"
2571 "uniform sampler Texture_Normal : register(s0),\n"
2572 "#ifdef USEALPHAKILL\n"
2573 "uniform sampler Texture_Color : register(s1),\n"
2574 "#endif\n"
2575 "uniform sampler Texture_Gloss : register(s2),\n"
2576 "#ifdef USEVERTEXTEXTUREBLEND\n"
2577 "uniform sampler Texture_SecondaryNormal : register(s4),\n"
2578 "uniform sampler Texture_SecondaryGloss : register(s6),\n"
2579 "#endif\n"
2580 "#ifdef USEOFFSETMAPPING\n"
2581 "uniform float OffsetMapping_Scale : register(c24),\n"
2582 "#endif\n"
2583 "uniform half SpecularPower : register(c36),\n"
2584 "#ifdef HLSL\n"
2585 "out float4 gl_FragData0 : COLOR0,\n"
2586 "out float4 gl_FragData1 : COLOR1\n"
2587 "#else\n"
2588 "out float4 gl_FragColor : COLOR\n"
2589 "#endif\n"
2590 ")\n"
2591 "{\n"
2592 "       float2 TexCoord = TexCoordBoth.xy;\n"
2593 "#ifdef USEOFFSETMAPPING\n"
2594 "       // apply offsetmapping\n"
2595 "       float2 TexCoordOffset = OffsetMapping(TexCoord, OffsetMapping_Scale, EyeVector, Texture_Normal);\n"
2596 "#define TexCoord TexCoordOffset\n"
2597 "#endif\n"
2598 "\n"
2599 "#ifdef USEALPHAKILL\n"
2600 "       if (tex2D(Texture_Color, TexCoord).a < 0.5)\n"
2601 "               discard;\n"
2602 "#endif\n"
2603 "\n"
2604 "#ifdef USEVERTEXTEXTUREBLEND\n"
2605 "       float alpha = tex2D(Texture_Color, TexCoord).a;\n"
2606 "       float terrainblend = clamp(float(gl_FrontColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0));\n"
2607 "       //float terrainblend = min(float(gl_FrontColor.a) * alpha * 2.0, float(1.0));\n"
2608 "       //float terrainblend = float(gl_FrontColor.a) * alpha > 0.5;\n"
2609 "#endif\n"
2610 "\n"
2611 "#ifdef USEVERTEXTEXTUREBLEND\n"
2612 "       float3 surfacenormal = lerp(tex2D(Texture_SecondaryNormal, TexCoord2).rgb, tex2D(Texture_Normal, TexCoord).rgb, terrainblend) - float3(0.5, 0.5, 0.5);\n"
2613 "       float a = lerp(tex2D(Texture_SecondaryGloss, TexCoord2).a, tex2D(Texture_Gloss, TexCoord).a, terrainblend);\n"
2614 "#else\n"
2615 "       float3 surfacenormal = tex2D(Texture_Normal, TexCoord).rgb - float3(0.5, 0.5, 0.5);\n"
2616 "       float a = tex2D(Texture_Gloss, TexCoord).a;\n"
2617 "#endif\n"
2618 "\n"
2619 "#ifdef HLSL\n"
2620 "       gl_FragData0 = float4(normalize(surfacenormal.x * VectorS + surfacenormal.y * VectorT + surfacenormal.z * VectorR.xyz) * 0.5 + float3(0.5, 0.5, 0.5), a);\n"
2621 "       float Depth = VectorR.w / 256.0;\n"
2622 "       float4 depthcolor = float4(Depth,Depth*65536.0/255.0,Depth*16777216.0/255.0,0.0);\n"
2623 "//     float4 depthcolor = float4(Depth,Depth*256.0,Depth*65536.0,0.0);\n"
2624 "       depthcolor.yz -= floor(depthcolor.yz);\n"
2625 "       gl_FragData1 = depthcolor;\n"
2626 "#else\n"
2627 "       gl_FragColor = float4(normalize(surfacenormal.x * VectorS + surfacenormal.y * VectorT + surfacenormal.z * VectorR) * 0.5 + float3(0.5, 0.5, 0.5), a);\n"
2628 "#endif\n"
2629 "}\n"
2630 "#endif // FRAGMENT_SHADER\n"
2631 "#else // !MODE_DEFERREDGEOMETRY\n"
2632 "\n"
2633 "\n"
2634 "\n"
2635 "\n"
2636 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
2637 "#ifdef VERTEX_SHADER\n"
2638 "void main\n"
2639 "(\n"
2640 "float4 gl_Vertex : POSITION,\n"
2641 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
2642 "uniform float4x4 ModelViewMatrix : register(c12),\n"
2643 "out float4 gl_Position : POSITION,\n"
2644 "out float4 ModelViewPosition : TEXCOORD0\n"
2645 ")\n"
2646 "{\n"
2647 "       ModelViewPosition = mul(ModelViewMatrix, gl_Vertex);\n"
2648 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
2649 "}\n"
2650 "#endif // VERTEX_SHADER\n"
2651 "\n"
2652 "#ifdef FRAGMENT_SHADER\n"
2653 "void main\n"
2654 "(\n"
2655 "#ifdef HLSL\n"
2656 "float2 Pixel : VPOS,\n"
2657 "#else\n"
2658 "float2 Pixel : WPOS,\n"
2659 "#endif\n"
2660 "float4 ModelViewPosition : TEXCOORD0,\n"
2661 "uniform float4x4 ViewToLight : register(c44),\n"
2662 "uniform float2 ScreenToDepth : register(c33), // ScreenToDepth = float2(Far / (Far - Near), Far * Near / (Near - Far));\n"
2663 "uniform float3 LightPosition : register(c23),\n"
2664 "uniform half2 PixelToScreenTexCoord : register(c42),\n"
2665 "uniform half3 DeferredColor_Ambient : register(c9),\n"
2666 "uniform half3 DeferredColor_Diffuse : register(c10),\n"
2667 "#ifdef USESPECULAR\n"
2668 "uniform half3 DeferredColor_Specular : register(c11),\n"
2669 "uniform half SpecularPower : register(c36),\n"
2670 "#endif\n"
2671 "uniform sampler Texture_Attenuation : register(s9),\n"
2672 "uniform sampler Texture_ScreenDepth : register(s13),\n"
2673 "uniform sampler Texture_ScreenNormalMap : register(s14),\n"
2674 "\n"
2675 "#ifdef USECUBEFILTER\n"
2676 "uniform samplerCUBE Texture_Cube : register(s10),\n"
2677 "#endif\n"
2678 "\n"
2679 "#ifdef USESHADOWMAP2D\n"
2680 "# ifdef USESHADOWSAMPLER\n"
2681 "uniform sampler Texture_ShadowMap2D : register(s15),\n"
2682 "# else\n"
2683 "uniform sampler Texture_ShadowMap2D : register(s15),\n"
2684 "# endif\n"
2685 "#endif\n"
2686 "\n"
2687 "#ifdef USESHADOWMAPVSDCT\n"
2688 "uniform samplerCUBE Texture_CubeProjection : register(s12),\n"
2689 "#endif\n"
2690 "\n"
2691 "#if defined(USESHADOWMAP2D)\n"
2692 "uniform float2 ShadowMap_TextureScale : register(c35),\n"
2693 "uniform float4 ShadowMap_Parameters : register(c34),\n"
2694 "#endif\n"
2695 "\n"
2696 "out float4 gl_FragData0 : COLOR0,\n"
2697 "out float4 gl_FragData1 : COLOR1\n"
2698 ")\n"
2699 "{\n"
2700 "       // calculate viewspace pixel position\n"
2701 "       float2 ScreenTexCoord = Pixel * PixelToScreenTexCoord;\n"
2702 "       //ScreenTexCoord.y = ScreenTexCoord.y * -1 + 1; // Cg is opposite?\n"
2703 "       float3 position;\n"
2704 "#ifdef HLSL\n"
2705 "       position.z = texDepth2D(Texture_ScreenDepth, ScreenTexCoord) * 256.0;\n"
2706 "#else\n"
2707 "       position.z = ScreenToDepth.y / (texDepth2D(Texture_ScreenDepth, ScreenTexCoord) + ScreenToDepth.x);\n"
2708 "#endif\n"
2709 "       position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z);\n"
2710 "       // decode viewspace pixel normal\n"
2711 "       half4 normalmap = half4(tex2D(Texture_ScreenNormalMap, ScreenTexCoord));\n"
2712 "       half3 surfacenormal = half3(normalize(normalmap.rgb - half3(0.5,0.5,0.5)));\n"
2713 "       // surfacenormal = pixel normal in viewspace\n"
2714 "       // LightVector = pixel to light in viewspace\n"
2715 "       // CubeVector = position in lightspace\n"
2716 "       // eyevector = pixel to view in viewspace\n"
2717 "       float3 CubeVector = mul(ViewToLight, float4(position,1)).xyz;\n"
2718 "       half fade = half(tex2D(Texture_Attenuation, float2(length(CubeVector), 0.0)).r);\n"
2719 "#ifdef USEDIFFUSE\n"
2720 "       // calculate diffuse shading\n"
2721 "       half3 lightnormal = half3(normalize(LightPosition - position));\n"
2722 "       half diffuse = half(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
2723 "#endif\n"
2724 "#ifdef USESPECULAR\n"
2725 "       // calculate directional shading\n"
2726 "       float3 eyevector = position * -1.0;\n"
2727 "#  ifdef USEEXACTSPECULARMATH\n"
2728 "       half specular = half(pow(half(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), SpecularPower * normalmap.a));\n"
2729 "#  else\n"
2730 "       half3 specularnormal = half3(normalize(lightnormal + half3(normalize(eyevector))));\n"
2731 "       half specular = half(pow(half(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * normalmap.a));\n"
2732 "#  endif\n"
2733 "#endif\n"
2734 "\n"
2735 "#if defined(USESHADOWMAP2D)\n"
2736 "       fade *= half(ShadowMapCompare(CubeVector, Texture_ShadowMap2D, ShadowMap_Parameters, ShadowMap_TextureScale\n"
2737 "#ifdef USESHADOWMAPVSDCT\n"
2738 ", Texture_CubeProjection\n"
2739 "#endif\n"
2740 "       ));\n"
2741 "#endif\n"
2742 "\n"
2743 "#ifdef USEDIFFUSE\n"
2744 "       gl_FragData0 = float4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0);\n"
2745 "#else\n"
2746 "       gl_FragData0 = float4(DeferredColor_Ambient * fade, 1.0);\n"
2747 "#endif\n"
2748 "#ifdef USESPECULAR\n"
2749 "       gl_FragData1 = float4(DeferredColor_Specular * (specular * fade), 1.0);\n"
2750 "#else\n"
2751 "       gl_FragData1 = float4(0.0, 0.0, 0.0, 1.0);\n"
2752 "#endif\n"
2753 "\n"
2754 "# ifdef USECUBEFILTER\n"
2755 "       float3 cubecolor = texCUBE(Texture_Cube, CubeVector).rgb;\n"
2756 "       gl_FragData0.rgb *= cubecolor;\n"
2757 "       gl_FragData1.rgb *= cubecolor;\n"
2758 "# endif\n"
2759 "}\n"
2760 "#endif // FRAGMENT_SHADER\n"
2761 "#else // !MODE_DEFERREDLIGHTSOURCE\n"
2762 "\n"
2763 "\n"
2764 "\n"
2765 "\n"
2766 "#ifdef VERTEX_SHADER\n"
2767 "void main\n"
2768 "(\n"
2769 "float4 gl_Vertex : POSITION,\n"
2770 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
2771 "#if defined(USEVERTEXTEXTUREBLEND) || defined(MODE_VERTEXCOLOR)\n"
2772 "float4 gl_Color : COLOR0,\n"
2773 "#endif\n"
2774 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
2775 "float4 gl_MultiTexCoord1 : TEXCOORD1,\n"
2776 "float4 gl_MultiTexCoord2 : TEXCOORD2,\n"
2777 "float4 gl_MultiTexCoord3 : TEXCOORD3,\n"
2778 "float4 gl_MultiTexCoord4 : TEXCOORD4,\n"
2779 "\n"
2780 "uniform float3 EyePosition : register(c24),\n"
2781 "uniform float4x4 TexMatrix : register(c0),\n"
2782 "#ifdef USEVERTEXTEXTUREBLEND\n"
2783 "uniform float4x4 BackgroundTexMatrix : register(c4),\n"
2784 "#endif\n"
2785 "#ifdef MODE_LIGHTSOURCE\n"
2786 "uniform float4x4 ModelToLight : register(c20),\n"
2787 "#endif\n"
2788 "#ifdef MODE_LIGHTSOURCE\n"
2789 "uniform float3 LightPosition : register(c27),\n"
2790 "#endif\n"
2791 "#ifdef MODE_LIGHTDIRECTION\n"
2792 "uniform float3 LightDir : register(c26),\n"
2793 "#endif\n"
2794 "uniform float4 FogPlane : register(c25),\n"
2795 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
2796 "uniform float3 LightPosition : register(c27),\n"
2797 "#endif\n"
2798 "#ifdef USESHADOWMAPORTHO\n"
2799 "uniform float4x4 ShadowMapMatrix : register(c16),\n"
2800 "#endif\n"
2801 "#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND)\n"
2802 "out float4 gl_FrontColor : COLOR,\n"
2803 "#endif\n"
2804 "out float4 TexCoordBoth : TEXCOORD0,\n"
2805 "#ifdef USELIGHTMAP\n"
2806 "out float2 TexCoordLightmap : TEXCOORD1,\n"
2807 "#endif\n"
2808 "#ifdef USEEYEVECTOR\n"
2809 "out float3 EyeVector : TEXCOORD2,\n"
2810 "#endif\n"
2811 "#ifdef USEREFLECTION\n"
2812 "out float4 ModelViewProjectionPosition : TEXCOORD3,\n"
2813 "#endif\n"
2814 "#ifdef USEFOG\n"
2815 "out float4 EyeVectorModelSpaceFogPlaneVertexDist : TEXCOORD4,\n"
2816 "#endif\n"
2817 "#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) || defined(USEDIFFUSE)\n"
2818 "out float3 LightVector : TEXCOORD1,\n"
2819 "#endif\n"
2820 "#ifdef MODE_LIGHTSOURCE\n"
2821 "out float3 CubeVector : TEXCOORD3,\n"
2822 "#endif\n"
2823 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE)\n"
2824 "out float3 VectorS : TEXCOORD5, // direction of S texcoord (sometimes crudely called tangent)\n"
2825 "out float3 VectorT : TEXCOORD6, // direction of T texcoord (sometimes crudely called binormal)\n"
2826 "out float3 VectorR : TEXCOORD7, // direction of R texcoord (surface normal)\n"
2827 "#endif\n"
2828 "#ifdef USESHADOWMAPORTHO\n"
2829 "out float3 ShadowMapTC : TEXCOORD3, // CONFLICTS WITH USEREFLECTION!\n"
2830 "#endif\n"
2831 "out float4 gl_Position : POSITION\n"
2832 ")\n"
2833 "{\n"
2834 "#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND)\n"
2835 "#ifdef HLSL\n"
2836 "       gl_FrontColor = gl_Color.bgra; // NOTE: D3DCOLOR is backwards\n"
2837 "#else\n"
2838 "       gl_FrontColor = gl_Color; // Cg is forward\n"
2839 "#endif\n"
2840 "#endif\n"
2841 "       // copy the surface texcoord\n"
2842 "       TexCoordBoth = mul(TexMatrix, gl_MultiTexCoord0);\n"
2843 "#ifdef USEVERTEXTEXTUREBLEND\n"
2844 "       TexCoordBoth.zw = mul(BackgroundTexMatrix, gl_MultiTexCoord0).xy;\n"
2845 "#endif\n"
2846 "#ifdef USELIGHTMAP\n"
2847 "       TexCoordLightmap = gl_MultiTexCoord4.xy;\n"
2848 "#endif\n"
2849 "\n"
2850 "#ifdef MODE_LIGHTSOURCE\n"
2851 "       // transform vertex position into light attenuation/cubemap space\n"
2852 "       // (-1 to +1 across the light box)\n"
2853 "       CubeVector = mul(ModelToLight, gl_Vertex).xyz;\n"
2854 "\n"
2855 "# ifdef USEDIFFUSE\n"
2856 "       // transform unnormalized light direction into tangent space\n"
2857 "       // (we use unnormalized to ensure that it interpolates correctly and then\n"
2858 "       //  normalize it per pixel)\n"
2859 "       float3 lightminusvertex = LightPosition - gl_Vertex.xyz;\n"
2860 "       LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz);\n"
2861 "       LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz);\n"
2862 "       LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz);\n"
2863 "# endif\n"
2864 "#endif\n"
2865 "\n"
2866 "#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE)\n"
2867 "       LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz);\n"
2868 "       LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz);\n"
2869 "       LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz);\n"
2870 "#endif\n"
2871 "\n"
2872 "       // transform unnormalized eye direction into tangent space\n"
2873 "#ifdef USEEYEVECTOR\n"
2874 "       float3 EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
2875 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
2876 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
2877 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
2878 "#endif\n"
2879 "\n"
2880 "#ifdef USEFOG\n"
2881 "       EyeVectorModelSpaceFogPlaneVertexDist.xyz = EyePosition - gl_Vertex.xyz;\n"
2882 "       EyeVectorModelSpaceFogPlaneVertexDist.w = dot(FogPlane, gl_Vertex);\n"
2883 "#endif\n"
2884 "\n"
2885 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
2886 "       VectorS = gl_MultiTexCoord1.xyz;\n"
2887 "       VectorT = gl_MultiTexCoord2.xyz;\n"
2888 "       VectorR = gl_MultiTexCoord3.xyz;\n"
2889 "#endif\n"
2890 "\n"
2891 "       // transform vertex to camera space, using ftransform to match non-VS rendering\n"
2892 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
2893 "\n"
2894 "#ifdef USESHADOWMAPORTHO\n"
2895 "       ShadowMapTC = mul(ShadowMapMatrix, gl_Position).xyz;\n"
2896 "#endif\n"
2897 "\n"
2898 "#ifdef USEREFLECTION\n"
2899 "       ModelViewProjectionPosition = gl_Position;\n"
2900 "#endif\n"
2901 "}\n"
2902 "#endif // VERTEX_SHADER\n"
2903 "\n"
2904 "\n"
2905 "\n"
2906 "\n"
2907 "#ifdef FRAGMENT_SHADER\n"
2908 "void main\n"
2909 "(\n"
2910 "#ifdef USEDEFERREDLIGHTMAP\n"
2911 "#ifdef HLSL\n"
2912 "float2 Pixel : VPOS,\n"
2913 "#else\n"
2914 "float2 Pixel : WPOS,\n"
2915 "#endif\n"
2916 "#endif\n"
2917 "float4 gl_FrontColor : COLOR,\n"
2918 "float4 TexCoordBoth : TEXCOORD0,\n"
2919 "#ifdef USELIGHTMAP\n"
2920 "float2 TexCoordLightmap : TEXCOORD1,\n"
2921 "#endif\n"
2922 "#ifdef USEEYEVECTOR\n"
2923 "float3 EyeVector : TEXCOORD2,\n"
2924 "#endif\n"
2925 "#ifdef USEREFLECTION\n"
2926 "float4 ModelViewProjectionPosition : TEXCOORD3,\n"
2927 "#endif\n"
2928 "#ifdef USEFOG\n"
2929 "float4 EyeVectorModelSpaceFogPlaneVertexDist : TEXCOORD4,\n"
2930 "#endif\n"
2931 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)\n"
2932 "float3 LightVector : TEXCOORD1,\n"
2933 "#endif\n"
2934 "#ifdef MODE_LIGHTSOURCE\n"
2935 "float3 CubeVector : TEXCOORD3,\n"
2936 "#endif\n"
2937 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
2938 "float4 ModelViewPosition : TEXCOORD0,\n"
2939 "#endif\n"
2940 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE)\n"
2941 "float3 VectorS : TEXCOORD5, // direction of S texcoord (sometimes crudely called tangent)\n"
2942 "float3 VectorT : TEXCOORD6, // direction of T texcoord (sometimes crudely called binormal)\n"
2943 "float3 VectorR : TEXCOORD7, // direction of R texcoord (surface normal)\n"
2944 "#endif\n"
2945 "#ifdef USESHADOWMAPORTHO\n"
2946 "float3 ShadowMapTC : TEXCOORD3, // CONFLICTS WITH USEREFLECTION!\n"
2947 "#endif\n"
2948 "\n"
2949 "uniform sampler Texture_Normal : register(s0),\n"
2950 "uniform sampler Texture_Color : register(s1),\n"
2951 "#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)\n"
2952 "uniform sampler Texture_Gloss : register(s2),\n"
2953 "#endif\n"
2954 "#ifdef USEGLOW\n"
2955 "uniform sampler Texture_Glow : register(s3),\n"
2956 "#endif\n"
2957 "#ifdef USEVERTEXTEXTUREBLEND\n"
2958 "uniform sampler Texture_SecondaryNormal : register(s4),\n"
2959 "uniform sampler Texture_SecondaryColor : register(s5),\n"
2960 "#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)\n"
2961 "uniform sampler Texture_SecondaryGloss : register(s6),\n"
2962 "#endif\n"
2963 "#ifdef USEGLOW\n"
2964 "uniform sampler Texture_SecondaryGlow : register(s7),\n"
2965 "#endif\n"
2966 "#endif\n"
2967 "#ifdef USECOLORMAPPING\n"
2968 "uniform sampler Texture_Pants : register(s4),\n"
2969 "uniform sampler Texture_Shirt : register(s7),\n"
2970 "#endif\n"
2971 "#ifdef USEFOG\n"
2972 "uniform sampler Texture_FogHeightTexture : register(s14),\n"
2973 "uniform sampler Texture_FogMask : register(s8),\n"
2974 "#endif\n"
2975 "#ifdef USELIGHTMAP\n"
2976 "uniform sampler Texture_Lightmap : register(s9),\n"
2977 "#endif\n"
2978 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)\n"
2979 "uniform sampler Texture_Deluxemap : register(s10),\n"
2980 "#endif\n"
2981 "#ifdef USEREFLECTION\n"
2982 "uniform sampler Texture_Reflection : register(s7),\n"
2983 "#endif\n"
2984 "\n"
2985 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
2986 "uniform sampler Texture_ScreenDepth : register(s13),\n"
2987 "uniform sampler Texture_ScreenNormalMap : register(s14),\n"
2988 "#endif\n"
2989 "#ifdef USEDEFERREDLIGHTMAP\n"
2990 "uniform sampler Texture_ScreenDepth : register(s13),\n"
2991 "uniform sampler Texture_ScreenNormalMap : register(s14),\n"
2992 "uniform sampler Texture_ScreenDiffuse : register(s11),\n"
2993 "uniform sampler Texture_ScreenSpecular : register(s12),\n"
2994 "#endif\n"
2995 "\n"
2996 "#ifdef USECOLORMAPPING\n"
2997 "uniform half3 Color_Pants : register(c7),\n"
2998 "uniform half3 Color_Shirt : register(c8),\n"
2999 "#endif\n"
3000 "#ifdef USEFOG\n"
3001 "uniform float3 FogColor : register(c16),\n"
3002 "uniform float FogRangeRecip : register(c20),\n"
3003 "uniform float FogPlaneViewDist : register(c19),\n"
3004 "uniform float FogHeightFade : register(c17),\n"
3005 "#endif\n"
3006 "\n"
3007 "#ifdef USEOFFSETMAPPING\n"
3008 "uniform float OffsetMapping_Scale : register(c24),\n"
3009 "#endif\n"
3010 "\n"
3011 "#ifdef USEDEFERREDLIGHTMAP\n"
3012 "uniform half2 PixelToScreenTexCoord : register(c42),\n"
3013 "uniform half3 DeferredMod_Diffuse : register(c12),\n"
3014 "uniform half3 DeferredMod_Specular : register(c13),\n"
3015 "#endif\n"
3016 "uniform half3 Color_Ambient : register(c3),\n"
3017 "uniform half3 Color_Diffuse : register(c4),\n"
3018 "uniform half3 Color_Specular : register(c5),\n"
3019 "uniform half SpecularPower : register(c36),\n"
3020 "#ifdef USEGLOW\n"
3021 "uniform half3 Color_Glow : register(c6),\n"
3022 "#endif\n"
3023 "uniform half Alpha : register(c0),\n"
3024 "#ifdef USEREFLECTION\n"
3025 "uniform float4 DistortScaleRefractReflect : register(c14),\n"
3026 "uniform float4 ScreenScaleRefractReflect : register(c32),\n"
3027 "uniform float4 ScreenCenterRefractReflect : register(c31),\n"
3028 "uniform half4 ReflectColor : register(c26),\n"
3029 "#endif\n"
3030 "#ifdef USEREFLECTCUBE\n"
3031 "uniform float4x4 ModelToReflectCube : register(c48),\n"
3032 "uniform sampler Texture_ReflectMask : register(s5),\n"
3033 "uniform samplerCUBE Texture_ReflectCube : register(s6),\n"
3034 "#endif\n"
3035 "#ifdef MODE_LIGHTDIRECTION\n"
3036 "uniform half3 LightColor : register(c21),\n"
3037 "#endif\n"
3038 "#ifdef MODE_LIGHTSOURCE\n"
3039 "uniform half3 LightColor : register(c21),\n"
3040 "#endif\n"
3041 "\n"
3042 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE)\n"
3043 "uniform sampler Texture_Attenuation : register(s9),\n"
3044 "uniform samplerCUBE Texture_Cube : register(s10),\n"
3045 "#endif\n"
3046 "\n"
3047 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO)\n"
3048 "\n"
3049 "#ifdef USESHADOWMAP2D\n"
3050 "# ifdef USESHADOWSAMPLER\n"
3051 "uniform sampler Texture_ShadowMap2D : register(s15),\n"
3052 "# else\n"
3053 "uniform sampler Texture_ShadowMap2D : register(s15),\n"
3054 "# endif\n"
3055 "#endif\n"
3056 "\n"
3057 "#ifdef USESHADOWMAPVSDCT\n"
3058 "uniform samplerCUBE Texture_CubeProjection : register(s12),\n"
3059 "#endif\n"
3060 "\n"
3061 "#if defined(USESHADOWMAP2D)\n"
3062 "uniform float2 ShadowMap_TextureScale : register(c35),\n"
3063 "uniform float4 ShadowMap_Parameters : register(c34),\n"
3064 "#endif\n"
3065 "#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO)\n"
3066 "\n"
3067 "out float4 gl_FragColor : COLOR\n"
3068 ")\n"
3069 "{\n"
3070 "       float2 TexCoord = TexCoordBoth.xy;\n"
3071 "#ifdef USEVERTEXTEXTUREBLEND\n"
3072 "       float2 TexCoord2 = TexCoordBoth.zw;\n"
3073 "#endif\n"
3074 "#ifdef USEOFFSETMAPPING\n"
3075 "       // apply offsetmapping\n"
3076 "       float2 TexCoordOffset = OffsetMapping(TexCoord, OffsetMapping_Scale, EyeVector, Texture_Normal);\n"
3077 "#define TexCoord TexCoordOffset\n"
3078 "#endif\n"
3079 "\n"
3080 "       // combine the diffuse textures (base, pants, shirt)\n"
3081 "       half4 color = half4(tex2D(Texture_Color, TexCoord));\n"
3082 "#ifdef USEALPHAKILL\n"
3083 "       if (color.a < 0.5)\n"
3084 "               discard;\n"
3085 "#endif\n"
3086 "       color.a *= Alpha;\n"
3087 "#ifdef USECOLORMAPPING\n"
3088 "       color.rgb += half3(tex2D(Texture_Pants, TexCoord).rgb) * Color_Pants + half3(tex2D(Texture_Shirt, TexCoord).rgb) * Color_Shirt;\n"
3089 "#endif\n"
3090 "#ifdef USEVERTEXTEXTUREBLEND\n"
3091 "       half terrainblend = clamp(half(gl_FrontColor.a) * color.a * 2.0 - 0.5, half(0.0), half(1.0));\n"
3092 "       //half terrainblend = min(half(gl_FrontColor.a) * color.a * 2.0, half(1.0));\n"
3093 "       //half terrainblend = half(gl_FrontColor.a) * color.a > 0.5;\n"
3094 "       color.rgb = half3(lerp(tex2D(Texture_SecondaryColor, TexCoord2).rgb, float3(color.rgb), terrainblend));\n"
3095 "       color.a = 1.0;\n"
3096 "       //color = half4(lerp(float4(1, 0, 0, 1), color, terrainblend));\n"
3097 "#endif\n"
3098 "\n"
3099 "       // get the surface normal\n"
3100 "#ifdef USEVERTEXTEXTUREBLEND\n"
3101 "       half3 surfacenormal = normalize(half3(lerp(tex2D(Texture_SecondaryNormal, TexCoord2).rgb, tex2D(Texture_Normal, TexCoord).rgb, terrainblend)) - half3(0.5, 0.5, 0.5));\n"
3102 "#else\n"
3103 "       half3 surfacenormal = half3(normalize(half3(tex2D(Texture_Normal, TexCoord).rgb) - half3(0.5, 0.5, 0.5)));\n"
3104 "#endif\n"
3105 "\n"
3106 "       // get the material colors\n"
3107 "       half3 diffusetex = color.rgb;\n"
3108 "#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)\n"
3109 "# ifdef USEVERTEXTEXTUREBLEND\n"
3110 "       half4 glosstex = half4(lerp(tex2D(Texture_SecondaryGloss, TexCoord2), tex2D(Texture_Gloss, TexCoord), terrainblend));\n"
3111 "# else\n"
3112 "       half4 glosstex = half4(tex2D(Texture_Gloss, TexCoord));\n"
3113 "# endif\n"
3114 "#endif\n"
3115 "\n"
3116 "#ifdef USEREFLECTCUBE\n"
3117 "       float3 TangentReflectVector = reflect(-EyeVector, surfacenormal);\n"
3118 "       float3 ModelReflectVector = TangentReflectVector.x * VectorS + TangentReflectVector.y * VectorT + TangentReflectVector.z * VectorR;\n"
3119 "       float3 ReflectCubeTexCoord = mul(ModelToReflectCube, float4(ModelReflectVector, 0)).xyz;\n"
3120 "       diffusetex += half3(tex2D(Texture_ReflectMask, TexCoord).rgb) * half3(texCUBE(Texture_ReflectCube, ReflectCubeTexCoord).rgb);\n"
3121 "#endif\n"
3122 "\n"
3123 "\n"
3124 "\n"
3125 "\n"
3126 "#ifdef MODE_LIGHTSOURCE\n"
3127 "       // light source\n"
3128 "#ifdef USEDIFFUSE\n"
3129 "       half3 lightnormal = half3(normalize(LightVector));\n"
3130 "       half diffuse = half(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
3131 "       color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse);\n"
3132 "#ifdef USESPECULAR\n"
3133 "#ifdef USEEXACTSPECULARMATH\n"
3134 "       half specular = half(pow(half(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower * glosstex.a));\n"
3135 "#else\n"
3136 "       half3 specularnormal = half3(normalize(lightnormal + half3(normalize(EyeVector))));\n"
3137 "       half specular = half(pow(half(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * glosstex.a));\n"
3138 "#endif\n"
3139 "       color.rgb += glosstex.rgb * (specular * Color_Specular);\n"
3140 "#endif\n"
3141 "#else\n"
3142 "       color.rgb = diffusetex * Color_Ambient;\n"
3143 "#endif\n"
3144 "       color.rgb *= LightColor;\n"
3145 "       color.rgb *= half(tex2D(Texture_Attenuation, float2(length(CubeVector), 0.0)).r);\n"
3146 "#if defined(USESHADOWMAP2D)\n"
3147 "       color.rgb *= half(ShadowMapCompare(CubeVector, Texture_ShadowMap2D, ShadowMap_Parameters, ShadowMap_TextureScale\n"
3148 "#ifdef USESHADOWMAPVSDCT\n"
3149 ", Texture_CubeProjection\n"
3150 "#endif\n"
3151 "       ));\n"
3152 "\n"
3153 "#endif\n"
3154 "# ifdef USECUBEFILTER\n"
3155 "       color.rgb *= half3(texCUBE(Texture_Cube, CubeVector).rgb);\n"
3156 "# endif\n"
3157 "\n"
3158 "#ifdef USESHADOWMAP2D\n"
3159 "#ifdef USESHADOWMAPVSDCT\n"
3160 "//     float3 shadowmaptc = GetShadowMapTC2D(CubeVector, ShadowMap_Parameters, Texture_CubeProjection);\n"
3161 "#else\n"
3162 "//     float3 shadowmaptc = GetShadowMapTC2D(CubeVector, ShadowMap_Parameters);\n"
3163 "#endif\n"
3164 "//     color.rgb = half3(tex2D(Texture_ShadowMap2D, float2(0.1,0.1)).rgb);\n"
3165 "//     color.rgb = half3(tex2D(Texture_ShadowMap2D, shadowmaptc.xy * ShadowMap_TextureScale).rgb);\n"
3166 "//     color.rgb = half3(shadowmaptc.xyz * float3(ShadowMap_TextureScale,1.0));\n"
3167 "//     color.r = half(texDepth2D(Texture_ShadowMap2D, shadowmaptc.xy * ShadowMap_TextureScale));\n"
3168 "//     color.rgb = half3(tex2D(Texture_ShadowMap2D, float2(0.1,0.1)).rgb);\n"
3169 "//     color.rgb = half3(tex2D(Texture_ShadowMap2D, shadowmaptc.xy * ShadowMap_TextureScale).rgb);\n"
3170 "//     color.rgb = half3(shadowmaptc.xyz * float3(ShadowMap_TextureScale,1.0));\n"
3171 "//     color.r = half(texDepth2D(Texture_ShadowMap2D, shadowmaptc.xy * ShadowMap_TextureScale));\n"
3172 "//     color.r = half(shadowmaptc.z - texDepth2D(Texture_ShadowMap2D, shadowmaptc.xy * ShadowMap_TextureScale));\n"
3173 "//     color.r = half(shadowmaptc.z);\n"
3174 "//     color.r = half(texDepth2D(Texture_ShadowMap2D, shadowmaptc.xy * ShadowMap_TextureScale));\n"
3175 "//     color.r = half(shadowmaptc.z);\n"
3176 "//     color.r = 1;\n"
3177 "//     color.rgb = abs(CubeVector);\n"
3178 "#endif\n"
3179 "//     color.rgb = half3(1,1,1);\n"
3180 "#endif // MODE_LIGHTSOURCE\n"
3181 "\n"
3182 "\n"
3183 "\n"
3184 "\n"
3185 "#ifdef MODE_LIGHTDIRECTION\n"
3186 "#define SHADING\n"
3187 "#ifdef USEDIFFUSE\n"
3188 "       half3 lightnormal = half3(normalize(LightVector));\n"
3189 "#endif\n"
3190 "#define lightcolor LightColor\n"
3191 "#endif // MODE_LIGHTDIRECTION\n"
3192 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
3193 "#define SHADING\n"
3194 "       // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe)\n"
3195 "       half3 lightnormal_modelspace = half3(tex2D(Texture_Deluxemap, TexCoordLightmap).rgb) * 2.0 + half3(-1.0, -1.0, -1.0);\n"
3196 "       half3 lightcolor = half3(tex2D(Texture_Lightmap, TexCoordLightmap).rgb);\n"
3197 "       // convert modelspace light vector to tangentspace\n"
3198 "       half3 lightnormal;\n"
3199 "       lightnormal.x = dot(lightnormal_modelspace, half3(VectorS));\n"
3200 "       lightnormal.y = dot(lightnormal_modelspace, half3(VectorT));\n"
3201 "       lightnormal.z = dot(lightnormal_modelspace, half3(VectorR));\n"
3202 "       // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division)\n"
3203 "       // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar\n"
3204 "       // is used (the lightmap and deluxemap coords correspond to virtually random coordinates\n"
3205 "       // on that luxel, and NOT to its center, because recursive triangle subdivision is used\n"
3206 "       // to map the luxels to coordinates on the draw surfaces), which also causes\n"
3207 "       // deluxemaps to be wrong because light contributions from the wrong side of the surface\n"
3208 "       // are added up. To prevent divisions by zero or strong exaggerations, a max()\n"
3209 "       // nudge is done here at expense of some additional fps. This is ONLY needed for\n"
3210 "       // deluxemaps, tangentspace deluxemap avoid this problem by design.\n"
3211 "       lightcolor *= 1.0 / max(0.25, lightnormal.z);\n"
3212 "#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
3213 "#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n"
3214 "#define SHADING\n"
3215 "       // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light)\n"
3216 "       half3 lightnormal = half3(tex2D(Texture_Deluxemap, TexCoordLightmap).rgb) * 2.0 + half3(-1.0, -1.0, -1.0);\n"
3217 "       half3 lightcolor = half3(tex2D(Texture_Lightmap, TexCoordLightmap).rgb);\n"
3218 "#endif\n"
3219 "\n"
3220 "\n"
3221 "\n"
3222 "\n"
3223 "#ifdef MODE_FAKELIGHT\n"
3224 "#define SHADING\n"
3225 "half3 lightnormal = half3(normalize(EyeVector));\n"
3226 "half3 lightcolor = half3(1.0,1.0,1.0);\n"
3227 "#endif // MODE_FAKELIGHT\n"
3228 "\n"
3229 "\n"
3230 "\n"
3231 "\n"
3232 "#ifdef MODE_LIGHTMAP\n"
3233 "       color.rgb = diffusetex * (Color_Ambient + half3(tex2D(Texture_Lightmap, TexCoordLightmap).rgb) * Color_Diffuse);\n"
3234 "#endif // MODE_LIGHTMAP\n"
3235 "#ifdef MODE_VERTEXCOLOR\n"
3236 "       color.rgb = diffusetex * (Color_Ambient + half3(gl_FrontColor.rgb) * Color_Diffuse);\n"
3237 "#endif // MODE_VERTEXCOLOR\n"
3238 "#ifdef MODE_FLATCOLOR\n"
3239 "       color.rgb = diffusetex * Color_Ambient;\n"
3240 "#endif // MODE_FLATCOLOR\n"
3241 "\n"
3242 "\n"
3243 "\n"
3244 "\n"
3245 "#ifdef SHADING\n"
3246 "# ifdef USEDIFFUSE\n"
3247 "       half diffuse = half(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
3248 "#  ifdef USESPECULAR\n"
3249 "#   ifdef USEEXACTSPECULARMATH\n"
3250 "       half specular = half(pow(half(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower * glosstex.a));\n"
3251 "#   else\n"
3252 "       half3 specularnormal = half3(normalize(lightnormal + half3(normalize(EyeVector))));\n"
3253 "       half specular = half(pow(half(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * glosstex.a));\n"
3254 "#   endif\n"
3255 "       color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor;\n"
3256 "#  else\n"
3257 "       color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor);\n"
3258 "#  endif\n"
3259 "# else\n"
3260 "       color.rgb = diffusetex * Color_Ambient;\n"
3261 "# endif\n"
3262 "#endif\n"
3263 "\n"
3264 "#ifdef USESHADOWMAPORTHO\n"
3265 "       color.rgb *= half(ShadowMapCompare(ShadowMapTC, Texture_ShadowMap2D, ShadowMap_Parameters, ShadowMap_TextureScale));\n"
3266 "#endif\n"
3267 "\n"
3268 "#ifdef USEDEFERREDLIGHTMAP\n"
3269 "       float2 ScreenTexCoord = Pixel * PixelToScreenTexCoord;\n"
3270 "       color.rgb += diffusetex * half3(tex2D(Texture_ScreenDiffuse, ScreenTexCoord).rgb) * DeferredMod_Diffuse;\n"
3271 "       color.rgb += glosstex.rgb * half3(tex2D(Texture_ScreenSpecular, ScreenTexCoord).rgb) * DeferredMod_Specular;\n"
3272 "//     color.rgb = half3(tex2D(Texture_ScreenDepth, ScreenTexCoord).rgb);\n"
3273 "//     color.r = half(texDepth2D(Texture_ScreenDepth, ScreenTexCoord)) * 1.0;\n"
3274 "#endif\n"
3275 "\n"
3276 "#ifdef USEGLOW\n"
3277 "#ifdef USEVERTEXTEXTUREBLEND\n"
3278 "       color.rgb += half3(lerp(tex2D(Texture_SecondaryGlow, TexCoord2).rgb, tex2D(Texture_Glow, TexCoord).rgb, terrainblend)) * Color_Glow;\n"
3279 "#else\n"
3280 "       color.rgb += half3(tex2D(Texture_Glow, TexCoord).rgb) * Color_Glow;\n"
3281 "#endif\n"
3282 "#endif\n"
3283 "\n"
3284 "#ifdef USEFOG\n"
3285 "       color.rgb = FogVertex(color.rgb, FogColor, EyeVectorModelSpaceFogPlaneVertexDist.xyz, EyeVectorModelSpaceFogPlaneVertexDist.w, FogRangeRecip, FogPlaneViewDist, FogHeightFade, Texture_FogMask, Texture_FogHeightTexture);\n"
3286 "#endif\n"
3287 "\n"
3288 "       // 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"
3289 "#ifdef USEREFLECTION\n"
3290 "       float4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
3291 "       //float4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(half3(tex2D(Texture_Normal, TexCoord).rgb) - half3(0.5,0.5,0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
3292 "       float2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw;\n"
3293 "       float2 ScreenTexCoord = SafeScreenTexCoord + float3(normalize(half3(tex2D(Texture_Normal, TexCoord).rgb) - half3(0.5,0.5,0.5))).xy * DistortScaleRefractReflect.zw;\n"
3294 "       // FIXME temporary hack to detect the case that the reflection\n"
3295 "       // gets blackened at edges due to leaving the area that contains actual\n"
3296 "       // content.\n"
3297 "       // Remove this 'ack once we have a better way to stop this thing from\n"
3298 "       // 'appening.\n"
3299 "       float f = min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord + float2(0.01, 0.01)).rgb) / 0.05);\n"
3300 "       f      *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord + float2(0.01, -0.01)).rgb) / 0.05);\n"
3301 "       f      *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord + float2(-0.01, 0.01)).rgb) / 0.05);\n"
3302 "       f      *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord + float2(-0.01, -0.01)).rgb) / 0.05);\n"
3303 "       ScreenTexCoord = lerp(SafeScreenTexCoord, ScreenTexCoord, f);\n"
3304 "       color.rgb = lerp(color.rgb, half3(tex2D(Texture_Reflection, ScreenTexCoord).rgb) * ReflectColor.rgb, ReflectColor.a);\n"
3305 "#endif\n"
3306 "\n"
3307 "       gl_FragColor = float4(color);\n"
3308 "}\n"
3309 "#endif // FRAGMENT_SHADER\n"
3310 "\n"
3311 "#endif // !MODE_DEFERREDLIGHTSOURCE\n"
3312 "#endif // !MODE_DEFERREDGEOMETRY\n"
3313 "#endif // !MODE_WATER\n"
3314 "#endif // !MODE_REFRACTION\n"
3315 "#endif // !MODE_BLOOMBLUR\n"
3316 "#endif // !MODE_GENERIC\n"
3317 "#endif // !MODE_POSTPROCESS\n"
3318 "#endif // !MODE_SHOWDEPTH\n"
3319 "#endif // !MODE_DEPTH_OR_SHADOW\n"
3320 ;
3321
3322 char *glslshaderstring = NULL;
3323 char *cgshaderstring = NULL;
3324 char *hlslshaderstring = NULL;
3325
3326 //=======================================================================================================================================================
3327
3328 typedef struct shaderpermutationinfo_s
3329 {
3330         const char *pretext;
3331         const char *name;
3332 }
3333 shaderpermutationinfo_t;
3334
3335 typedef struct shadermodeinfo_s
3336 {
3337         const char *vertexfilename;
3338         const char *geometryfilename;
3339         const char *fragmentfilename;
3340         const char *pretext;
3341         const char *name;
3342 }
3343 shadermodeinfo_t;
3344
3345 typedef enum shaderpermutation_e
3346 {
3347         SHADERPERMUTATION_DIFFUSE = 1<<0, ///< (lightsource) whether to use directional shading
3348         SHADERPERMUTATION_VERTEXTEXTUREBLEND = 1<<1, ///< indicates this is a two-layer material blend based on vertex alpha (q3bsp)
3349         SHADERPERMUTATION_VIEWTINT = 1<<2, ///< view tint (postprocessing only), use vertex colors (generic only)
3350         SHADERPERMUTATION_COLORMAPPING = 1<<3, ///< indicates this is a colormapped skin
3351         SHADERPERMUTATION_SATURATION = 1<<4, ///< saturation (postprocessing only)
3352         SHADERPERMUTATION_FOGINSIDE = 1<<5, ///< tint the color by fog color or black if using additive blend mode
3353         SHADERPERMUTATION_FOGOUTSIDE = 1<<6, ///< tint the color by fog color or black if using additive blend mode
3354         SHADERPERMUTATION_FOGHEIGHTTEXTURE = 1<<7, ///< fog color and density determined by texture mapped on vertical axis
3355         SHADERPERMUTATION_GAMMARAMPS = 1<<8, ///< gamma (postprocessing only)
3356         SHADERPERMUTATION_CUBEFILTER = 1<<9, ///< (lightsource) use cubemap light filter
3357         SHADERPERMUTATION_GLOW = 1<<10, ///< (lightmap) blend in an additive glow texture
3358         SHADERPERMUTATION_BLOOM = 1<<11, ///< bloom (postprocessing only)
3359         SHADERPERMUTATION_SPECULAR = 1<<12, ///< (lightsource or deluxemapping) render specular effects
3360         SHADERPERMUTATION_POSTPROCESSING = 1<<13, ///< user defined postprocessing (postprocessing only)
3361         SHADERPERMUTATION_REFLECTION = 1<<14, ///< normalmap-perturbed reflection of the scene infront of the surface, preformed as an overlay on the surface
3362         SHADERPERMUTATION_OFFSETMAPPING = 1<<15, ///< adjust texcoords to roughly simulate a displacement mapped surface
3363         SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING = 1<<16, ///< adjust texcoords to accurately simulate a displacement mapped surface (requires OFFSETMAPPING to also be set!)
3364         SHADERPERMUTATION_SHADOWMAP2D = 1<<17, ///< (lightsource) use shadowmap texture as light filter
3365         SHADERPERMUTATION_SHADOWMAPPCF = 1<<18, ///< (lightsource) use percentage closer filtering on shadowmap test results
3366         SHADERPERMUTATION_SHADOWMAPPCF2 = 1<<19, ///< (lightsource) use higher quality percentage closer filtering on shadowmap test results
3367         SHADERPERMUTATION_SHADOWSAMPLER = 1<<20, ///< (lightsource) use hardware shadowmap test
3368         SHADERPERMUTATION_SHADOWMAPVSDCT = 1<<21, ///< (lightsource) use virtual shadow depth cube texture for shadowmap indexing
3369         SHADERPERMUTATION_SHADOWMAPORTHO = 1<<22, //< (lightsource) use orthographic shadowmap projection
3370         SHADERPERMUTATION_DEFERREDLIGHTMAP = 1<<23, ///< (lightmap) read Texture_ScreenDiffuse/Specular textures and add them on top of lightmapping
3371         SHADERPERMUTATION_ALPHAKILL = 1<<24, ///< (deferredgeometry) discard pixel if diffuse texture alpha below 0.5
3372         SHADERPERMUTATION_REFLECTCUBE = 1<<25, ///< fake reflections using global cubemap (not HDRI light probe)
3373         SHADERPERMUTATION_NORMALMAPSCROLLBLEND = 1<<26, // (water) counter-direction normalmaps scrolling
3374         SHADERPERMUTATION_LIMIT = 1<<27, ///< size of permutations array
3375         SHADERPERMUTATION_COUNT = 27 ///< size of shaderpermutationinfo array
3376 }
3377 shaderpermutation_t;
3378
3379 // NOTE: MUST MATCH ORDER OF SHADERPERMUTATION_* DEFINES!
3380 shaderpermutationinfo_t shaderpermutationinfo[SHADERPERMUTATION_COUNT] =
3381 {
3382         {"#define USEDIFFUSE\n", " diffuse"},
3383         {"#define USEVERTEXTEXTUREBLEND\n", " vertextextureblend"},
3384         {"#define USEVIEWTINT\n", " viewtint"},
3385         {"#define USECOLORMAPPING\n", " colormapping"},
3386         {"#define USESATURATION\n", " saturation"},
3387         {"#define USEFOGINSIDE\n", " foginside"},
3388         {"#define USEFOGOUTSIDE\n", " fogoutside"},
3389         {"#define USEFOGHEIGHTTEXTURE\n", " fogheighttexture"},
3390         {"#define USEGAMMARAMPS\n", " gammaramps"},
3391         {"#define USECUBEFILTER\n", " cubefilter"},
3392         {"#define USEGLOW\n", " glow"},
3393         {"#define USEBLOOM\n", " bloom"},
3394         {"#define USESPECULAR\n", " specular"},
3395         {"#define USEPOSTPROCESSING\n", " postprocessing"},
3396         {"#define USEREFLECTION\n", " reflection"},
3397         {"#define USEOFFSETMAPPING\n", " offsetmapping"},
3398         {"#define USEOFFSETMAPPING_RELIEFMAPPING\n", " reliefmapping"},
3399         {"#define USESHADOWMAP2D\n", " shadowmap2d"},
3400         {"#define USESHADOWMAPPCF 1\n", " shadowmappcf"},
3401         {"#define USESHADOWMAPPCF 2\n", " shadowmappcf2"},
3402         {"#define USESHADOWSAMPLER\n", " shadowsampler"},
3403         {"#define USESHADOWMAPVSDCT\n", " shadowmapvsdct"},
3404         {"#define USESHADOWMAPORTHO\n", " shadowmaportho"},
3405         {"#define USEDEFERREDLIGHTMAP\n", " deferredlightmap"},
3406         {"#define USEALPHAKILL\n", " alphakill"},
3407         {"#define USEREFLECTCUBE\n", " reflectcube"},
3408         {"#define USENORMALMAPSCROLLBLEND\n", " normalmapscrollblend"},
3409 };
3410
3411 // this enum selects which of the glslshadermodeinfo entries should be used
3412 typedef enum shadermode_e
3413 {
3414         SHADERMODE_GENERIC, ///< (particles/HUD/etc) vertex color, optionally multiplied by one texture
3415         SHADERMODE_POSTPROCESS, ///< postprocessing shader (r_glsl_postprocess)
3416         SHADERMODE_DEPTH_OR_SHADOW, ///< (depthfirst/shadows) vertex shader only
3417         SHADERMODE_FLATCOLOR, ///< (lightmap) modulate texture by uniform color (q1bsp, q3bsp)
3418         SHADERMODE_VERTEXCOLOR, ///< (lightmap) modulate texture by vertex colors (q3bsp)
3419         SHADERMODE_LIGHTMAP, ///< (lightmap) modulate texture by lightmap texture (q1bsp, q3bsp)
3420         SHADERMODE_FAKELIGHT, ///< (fakelight) modulate texture by "fake" lighting (no lightmaps, no nothing)
3421         SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE, ///< (lightmap) use directional pixel shading from texture containing modelspace light directions (q3bsp deluxemap)
3422         SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE, ///< (lightmap) use directional pixel shading from texture containing tangentspace light directions (q1bsp deluxemap)
3423         SHADERMODE_LIGHTDIRECTION, ///< (lightmap) use directional pixel shading from fixed light direction (q3bsp)
3424         SHADERMODE_LIGHTSOURCE, ///< (lightsource) use directional pixel shading from light source (rtlight)
3425         SHADERMODE_REFRACTION, ///< refract background (the material is rendered normally after this pass)
3426         SHADERMODE_WATER, ///< refract background and reflection (the material is rendered normally after this pass)
3427         SHADERMODE_SHOWDEPTH, ///< (debugging) renders depth as color
3428         SHADERMODE_DEFERREDGEOMETRY, ///< (deferred) render material properties to screenspace geometry buffers
3429         SHADERMODE_DEFERREDLIGHTSOURCE, ///< (deferred) use directional pixel shading from light source (rtlight) on screenspace geometry buffers
3430         SHADERMODE_COUNT
3431 }
3432 shadermode_t;
3433
3434 // NOTE: MUST MATCH ORDER OF SHADERMODE_* ENUMS!
3435 shadermodeinfo_t glslshadermodeinfo[SHADERMODE_COUNT] =
3436 {
3437         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_GENERIC\n", " generic"},
3438         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_POSTPROCESS\n", " postprocess"},
3439         {"glsl/default.glsl", NULL, NULL               , "#define MODE_DEPTH_OR_SHADOW\n", " depth/shadow"},
3440         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_FLATCOLOR\n", " flatcolor"},
3441         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_VERTEXCOLOR\n", " vertexcolor"},
3442         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTMAP\n", " lightmap"},
3443         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_FAKELIGHT\n", " fakelight"},
3444         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
3445         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
3446         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTION\n", " lightdirection"},
3447         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTSOURCE\n", " lightsource"},
3448         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_REFRACTION\n", " refraction"},
3449         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_WATER\n", " water"},
3450         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_SHOWDEPTH\n", " showdepth"},
3451         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_DEFERREDGEOMETRY\n", " deferredgeometry"},
3452         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_DEFERREDLIGHTSOURCE\n", " deferredlightsource"},
3453 };
3454
3455 #ifdef SUPPORTCG
3456 shadermodeinfo_t cgshadermodeinfo[SHADERMODE_COUNT] =
3457 {
3458         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_GENERIC\n", " generic"},
3459         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_POSTPROCESS\n", " postprocess"},
3460         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_DEPTH_OR_SHADOW\n", " depth/shadow"},
3461         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_FLATCOLOR\n", " flatcolor"},
3462         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_VERTEXCOLOR\n", " vertexcolor"},
3463         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_LIGHTMAP\n", " lightmap"},
3464         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_FAKELIGHT\n", " fakelight"},
3465         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
3466         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
3467         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_LIGHTDIRECTION\n", " lightdirection"},
3468         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_LIGHTSOURCE\n", " lightsource"},
3469         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_REFRACTION\n", " refraction"},
3470         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_WATER\n", " water"},
3471         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_SHOWDEPTH\n", " showdepth"},
3472         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_DEFERREDGEOMETRY\n", " deferredgeometry"},
3473         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_DEFERREDLIGHTSOURCE\n", " deferredlightsource"},
3474 };
3475 #endif
3476
3477 #ifdef SUPPORTD3D
3478 shadermodeinfo_t hlslshadermodeinfo[SHADERMODE_COUNT] =
3479 {
3480         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_GENERIC\n", " generic"},
3481         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_POSTPROCESS\n", " postprocess"},
3482         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_DEPTH_OR_SHADOW\n", " depth/shadow"},
3483         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_FLATCOLOR\n", " flatcolor"},
3484         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_VERTEXCOLOR\n", " vertexcolor"},
3485         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_LIGHTMAP\n", " lightmap"},
3486         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_FAKELIGHT\n", " fakelight"},
3487         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
3488         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
3489         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_LIGHTDIRECTION\n", " lightdirection"},
3490         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_LIGHTSOURCE\n", " lightsource"},
3491         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_REFRACTION\n", " refraction"},
3492         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_WATER\n", " water"},
3493         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_SHOWDEPTH\n", " showdepth"},
3494         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_DEFERREDGEOMETRY\n", " deferredgeometry"},
3495         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_DEFERREDLIGHTSOURCE\n", " deferredlightsource"},
3496 };
3497 #endif
3498
3499 struct r_glsl_permutation_s;
3500 typedef struct r_glsl_permutation_s
3501 {
3502         /// hash lookup data
3503         struct r_glsl_permutation_s *hashnext;
3504         unsigned int mode;
3505         unsigned int permutation;
3506
3507         /// indicates if we have tried compiling this permutation already
3508         qboolean compiled;
3509         /// 0 if compilation failed
3510         int program;
3511         /// locations of detected uniforms in program object, or -1 if not found
3512         int loc_Texture_First;
3513         int loc_Texture_Second;
3514         int loc_Texture_GammaRamps;
3515         int loc_Texture_Normal;
3516         int loc_Texture_Color;
3517         int loc_Texture_Gloss;
3518         int loc_Texture_Glow;
3519         int loc_Texture_SecondaryNormal;
3520         int loc_Texture_SecondaryColor;
3521         int loc_Texture_SecondaryGloss;
3522         int loc_Texture_SecondaryGlow;
3523         int loc_Texture_Pants;
3524         int loc_Texture_Shirt;
3525         int loc_Texture_FogHeightTexture;
3526         int loc_Texture_FogMask;
3527         int loc_Texture_Lightmap;
3528         int loc_Texture_Deluxemap;
3529         int loc_Texture_Attenuation;
3530         int loc_Texture_Cube;
3531         int loc_Texture_Refraction;
3532         int loc_Texture_Reflection;
3533         int loc_Texture_ShadowMap2D;
3534         int loc_Texture_CubeProjection;
3535         int loc_Texture_ScreenDepth;
3536         int loc_Texture_ScreenNormalMap;
3537         int loc_Texture_ScreenDiffuse;
3538         int loc_Texture_ScreenSpecular;
3539         int loc_Texture_ReflectMask;
3540         int loc_Texture_ReflectCube;
3541         int loc_Alpha;
3542         int loc_BloomBlur_Parameters;
3543         int loc_ClientTime;
3544         int loc_Color_Ambient;
3545         int loc_Color_Diffuse;
3546         int loc_Color_Specular;
3547         int loc_Color_Glow;
3548         int loc_Color_Pants;
3549         int loc_Color_Shirt;
3550         int loc_DeferredColor_Ambient;
3551         int loc_DeferredColor_Diffuse;
3552         int loc_DeferredColor_Specular;
3553         int loc_DeferredMod_Diffuse;
3554         int loc_DeferredMod_Specular;
3555         int loc_DistortScaleRefractReflect;
3556         int loc_EyePosition;
3557         int loc_FogColor;
3558         int loc_FogHeightFade;
3559         int loc_FogPlane;
3560         int loc_FogPlaneViewDist;
3561         int loc_FogRangeRecip;
3562         int loc_LightColor;
3563         int loc_LightDir;
3564         int loc_LightPosition;
3565         int loc_OffsetMapping_Scale;
3566         int loc_PixelSize;
3567         int loc_ReflectColor;
3568         int loc_ReflectFactor;
3569         int loc_ReflectOffset;
3570         int loc_RefractColor;
3571         int loc_Saturation;
3572         int loc_ScreenCenterRefractReflect;
3573         int loc_ScreenScaleRefractReflect;
3574         int loc_ScreenToDepth;
3575         int loc_ShadowMap_Parameters;
3576         int loc_ShadowMap_TextureScale;
3577         int loc_SpecularPower;
3578         int loc_UserVec1;
3579         int loc_UserVec2;
3580         int loc_UserVec3;
3581         int loc_UserVec4;
3582         int loc_ViewTintColor;
3583         int loc_ViewToLight;
3584         int loc_ModelToLight;
3585         int loc_TexMatrix;
3586         int loc_BackgroundTexMatrix;
3587         int loc_ModelViewProjectionMatrix;
3588         int loc_ModelViewMatrix;
3589         int loc_PixelToScreenTexCoord;
3590         int loc_ModelToReflectCube;
3591         int loc_ShadowMapMatrix;
3592         int loc_BloomColorSubtract;
3593         int loc_NormalmapScrollBlend;
3594 }
3595 r_glsl_permutation_t;
3596
3597 #define SHADERPERMUTATION_HASHSIZE 256
3598
3599
3600 // non-degradable "lightweight" shader parameters to keep the permutations simpler
3601 // these can NOT degrade! only use for simple stuff
3602 enum
3603 {
3604         SHADERSTATICPARM_SATURATION_REDCOMPENSATE = 0, ///< red compensation filter for saturation
3605         SHADERSTATICPARM_EXACTSPECULARMATH = 1, ///< (lightsource or deluxemapping) use exact reflection map for specular effects, as opposed to the usual OpenGL approximation
3606         SHADERSTATICPARM_POSTPROCESS_USERVEC1 = 2, ///< postprocess uservec1 is enabled
3607         SHADERSTATICPARM_POSTPROCESS_USERVEC2 = 3, ///< postprocess uservec2 is enabled
3608         SHADERSTATICPARM_POSTPROCESS_USERVEC3 = 4, ///< postprocess uservec3 is enabled
3609         SHADERSTATICPARM_POSTPROCESS_USERVEC4 = 5  ///< postprocess uservec4 is enabled
3610 };
3611 #define SHADERSTATICPARMS_COUNT 6
3612
3613 static const char *shaderstaticparmstrings_list[SHADERSTATICPARMS_COUNT];
3614 static int shaderstaticparms_count = 0;
3615
3616 static unsigned int r_compileshader_staticparms[(SHADERSTATICPARMS_COUNT + 0x1F) >> 5] = {0};
3617 #define R_COMPILESHADER_STATICPARM_ENABLE(p) r_compileshader_staticparms[(p) >> 5] |= (1 << ((p) & 0x1F))
3618 qboolean R_CompileShader_CheckStaticParms(void)
3619 {
3620         static int r_compileshader_staticparms_save[1];
3621         memcpy(r_compileshader_staticparms_save, r_compileshader_staticparms, sizeof(r_compileshader_staticparms));
3622         memset(r_compileshader_staticparms, 0, sizeof(r_compileshader_staticparms));
3623
3624         // detect all
3625         if (r_glsl_saturation_redcompensate.integer)
3626                 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_SATURATION_REDCOMPENSATE);
3627         if (r_shadow_glossexact.integer)
3628                 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_EXACTSPECULARMATH);
3629         if (r_glsl_postprocess.integer)
3630         {
3631                 if (r_glsl_postprocess_uservec1_enable.integer)
3632                         R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC1);
3633                 if (r_glsl_postprocess_uservec2_enable.integer)
3634                         R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC2);
3635                 if (r_glsl_postprocess_uservec3_enable.integer)
3636                         R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC3);
3637                 if (r_glsl_postprocess_uservec4_enable.integer)
3638                         R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC4);
3639         }
3640         return memcmp(r_compileshader_staticparms, r_compileshader_staticparms_save, sizeof(r_compileshader_staticparms)) != 0;
3641 }
3642
3643 #define R_COMPILESHADER_STATICPARM_EMIT(p, n) \
3644         if(r_compileshader_staticparms[(p) >> 5] & (1 << ((p) & 0x1F))) \
3645                 shaderstaticparmstrings_list[shaderstaticparms_count++] = "#define " n "\n"; \
3646         else \
3647                 shaderstaticparmstrings_list[shaderstaticparms_count++] = "\n"
3648 void R_CompileShader_AddStaticParms(unsigned int mode, unsigned int permutation)
3649 {
3650         shaderstaticparms_count = 0;
3651
3652         // emit all
3653         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_SATURATION_REDCOMPENSATE, "SATURATION_REDCOMPENSATE");
3654         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_EXACTSPECULARMATH, "USEEXACTSPECULARMATH");
3655         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC1, "USERVEC1");
3656         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC2, "USERVEC2");
3657         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC3, "USERVEC3");
3658         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC4, "USERVEC4");
3659 }
3660
3661 /// information about each possible shader permutation
3662 r_glsl_permutation_t *r_glsl_permutationhash[SHADERMODE_COUNT][SHADERPERMUTATION_HASHSIZE];
3663 /// currently selected permutation
3664 r_glsl_permutation_t *r_glsl_permutation;
3665 /// storage for permutations linked in the hash table
3666 memexpandablearray_t r_glsl_permutationarray;
3667
3668 static r_glsl_permutation_t *R_GLSL_FindPermutation(unsigned int mode, unsigned int permutation)
3669 {
3670         //unsigned int hashdepth = 0;
3671         unsigned int hashindex = (permutation * 0x1021) & (SHADERPERMUTATION_HASHSIZE - 1);
3672         r_glsl_permutation_t *p;
3673         for (p = r_glsl_permutationhash[mode][hashindex];p;p = p->hashnext)
3674         {
3675                 if (p->mode == mode && p->permutation == permutation)
3676                 {
3677                         //if (hashdepth > 10)
3678                         //      Con_Printf("R_GLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
3679                         return p;
3680                 }
3681                 //hashdepth++;
3682         }
3683         p = (r_glsl_permutation_t*)Mem_ExpandableArray_AllocRecord(&r_glsl_permutationarray);
3684         p->mode = mode;
3685         p->permutation = permutation;
3686         p->hashnext = r_glsl_permutationhash[mode][hashindex];
3687         r_glsl_permutationhash[mode][hashindex] = p;
3688         //if (hashdepth > 10)
3689         //      Con_Printf("R_GLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
3690         return p;
3691 }
3692
3693 static char *R_GLSL_GetText(const char *filename, qboolean printfromdisknotice)
3694 {
3695         char *shaderstring;
3696         if (!filename || !filename[0])
3697                 return NULL;
3698         if (!strcmp(filename, "glsl/default.glsl"))
3699         {
3700                 if (!glslshaderstring)
3701                 {
3702                         glslshaderstring = (char *)FS_LoadFile(filename, r_main_mempool, false, NULL);
3703                         if (glslshaderstring)
3704                                 Con_DPrintf("Loading shaders from file %s...\n", filename);
3705                         else
3706                                 glslshaderstring = (char *)builtinshaderstring;
3707                 }
3708                 shaderstring = (char *) Mem_Alloc(r_main_mempool, strlen(glslshaderstring) + 1);
3709                 memcpy(shaderstring, glslshaderstring, strlen(glslshaderstring) + 1);
3710                 return shaderstring;
3711         }
3712         shaderstring = (char *)FS_LoadFile(filename, r_main_mempool, false, NULL);
3713         if (shaderstring)
3714         {
3715                 if (printfromdisknotice)
3716                         Con_DPrintf("from disk %s... ", filename);
3717                 return shaderstring;
3718         }
3719         return shaderstring;
3720 }
3721
3722 static void R_GLSL_CompilePermutation(r_glsl_permutation_t *p, unsigned int mode, unsigned int permutation)
3723 {
3724         int i;
3725         shadermodeinfo_t *modeinfo = glslshadermodeinfo + mode;
3726         char *vertexstring, *geometrystring, *fragmentstring;
3727         char permutationname[256];
3728         int vertstrings_count = 0;
3729         int geomstrings_count = 0;
3730         int fragstrings_count = 0;
3731         const char *vertstrings_list[32+3+SHADERSTATICPARMS_COUNT+1];
3732         const char *geomstrings_list[32+3+SHADERSTATICPARMS_COUNT+1];
3733         const char *fragstrings_list[32+3+SHADERSTATICPARMS_COUNT+1];
3734
3735         if (p->compiled)
3736                 return;
3737         p->compiled = true;
3738         p->program = 0;
3739
3740         permutationname[0] = 0;
3741         vertexstring   = R_GLSL_GetText(modeinfo->vertexfilename, true);
3742         geometrystring = R_GLSL_GetText(modeinfo->geometryfilename, false);
3743         fragmentstring = R_GLSL_GetText(modeinfo->fragmentfilename, false);
3744
3745         strlcat(permutationname, modeinfo->vertexfilename, sizeof(permutationname));
3746
3747         // the first pretext is which type of shader to compile as
3748         // (later these will all be bound together as a program object)
3749         vertstrings_list[vertstrings_count++] = "#define VERTEX_SHADER\n";
3750         geomstrings_list[geomstrings_count++] = "#define GEOMETRY_SHADER\n";
3751         fragstrings_list[fragstrings_count++] = "#define FRAGMENT_SHADER\n";
3752
3753         // the second pretext is the mode (for example a light source)
3754         vertstrings_list[vertstrings_count++] = modeinfo->pretext;
3755         geomstrings_list[geomstrings_count++] = modeinfo->pretext;
3756         fragstrings_list[fragstrings_count++] = modeinfo->pretext;
3757         strlcat(permutationname, modeinfo->name, sizeof(permutationname));
3758
3759         // now add all the permutation pretexts
3760         for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
3761         {
3762                 if (permutation & (1<<i))
3763                 {
3764                         vertstrings_list[vertstrings_count++] = shaderpermutationinfo[i].pretext;
3765                         geomstrings_list[geomstrings_count++] = shaderpermutationinfo[i].pretext;
3766                         fragstrings_list[fragstrings_count++] = shaderpermutationinfo[i].pretext;
3767                         strlcat(permutationname, shaderpermutationinfo[i].name, sizeof(permutationname));
3768                 }
3769                 else
3770                 {
3771                         // keep line numbers correct
3772                         vertstrings_list[vertstrings_count++] = "\n";
3773                         geomstrings_list[geomstrings_count++] = "\n";
3774                         fragstrings_list[fragstrings_count++] = "\n";
3775                 }
3776         }
3777
3778         // add static parms
3779         R_CompileShader_AddStaticParms(mode, permutation);
3780         memcpy((char *)(vertstrings_list + vertstrings_count), shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
3781         vertstrings_count += shaderstaticparms_count;
3782         memcpy((char *)(geomstrings_list + geomstrings_count), shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
3783         geomstrings_count += shaderstaticparms_count;
3784         memcpy((char *)(fragstrings_list + fragstrings_count), shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
3785         fragstrings_count += shaderstaticparms_count;
3786
3787         // now append the shader text itself
3788         vertstrings_list[vertstrings_count++] = vertexstring;
3789         geomstrings_list[geomstrings_count++] = geometrystring;
3790         fragstrings_list[fragstrings_count++] = fragmentstring;
3791
3792         // if any sources were NULL, clear the respective list
3793         if (!vertexstring)
3794                 vertstrings_count = 0;
3795         if (!geometrystring)
3796                 geomstrings_count = 0;
3797         if (!fragmentstring)
3798                 fragstrings_count = 0;
3799
3800         // compile the shader program
3801         if (vertstrings_count + geomstrings_count + fragstrings_count)
3802                 p->program = GL_Backend_CompileProgram(vertstrings_count, vertstrings_list, geomstrings_count, geomstrings_list, fragstrings_count, fragstrings_list);
3803         if (p->program)
3804         {
3805                 CHECKGLERROR
3806                 qglUseProgramObjectARB(p->program);CHECKGLERROR
3807                 // look up all the uniform variable names we care about, so we don't
3808                 // have to look them up every time we set them
3809
3810                 p->loc_Texture_First              = qglGetUniformLocationARB(p->program, "Texture_First");
3811                 p->loc_Texture_Second             = qglGetUniformLocationARB(p->program, "Texture_Second");
3812                 p->loc_Texture_GammaRamps         = qglGetUniformLocationARB(p->program, "Texture_GammaRamps");
3813                 p->loc_Texture_Normal             = qglGetUniformLocationARB(p->program, "Texture_Normal");
3814                 p->loc_Texture_Color              = qglGetUniformLocationARB(p->program, "Texture_Color");
3815                 p->loc_Texture_Gloss              = qglGetUniformLocationARB(p->program, "Texture_Gloss");
3816                 p->loc_Texture_Glow               = qglGetUniformLocationARB(p->program, "Texture_Glow");
3817                 p->loc_Texture_SecondaryNormal    = qglGetUniformLocationARB(p->program, "Texture_SecondaryNormal");
3818                 p->loc_Texture_SecondaryColor     = qglGetUniformLocationARB(p->program, "Texture_SecondaryColor");
3819                 p->loc_Texture_SecondaryGloss     = qglGetUniformLocationARB(p->program, "Texture_SecondaryGloss");
3820                 p->loc_Texture_SecondaryGlow      = qglGetUniformLocationARB(p->program, "Texture_SecondaryGlow");
3821                 p->loc_Texture_Pants              = qglGetUniformLocationARB(p->program, "Texture_Pants");
3822                 p->loc_Texture_Shirt              = qglGetUniformLocationARB(p->program, "Texture_Shirt");
3823                 p->loc_Texture_FogHeightTexture   = qglGetUniformLocationARB(p->program, "Texture_FogHeightTexture");
3824                 p->loc_Texture_FogMask            = qglGetUniformLocationARB(p->program, "Texture_FogMask");
3825                 p->loc_Texture_Lightmap           = qglGetUniformLocationARB(p->program, "Texture_Lightmap");
3826                 p->loc_Texture_Deluxemap          = qglGetUniformLocationARB(p->program, "Texture_Deluxemap");
3827                 p->loc_Texture_Attenuation        = qglGetUniformLocationARB(p->program, "Texture_Attenuation");
3828                 p->loc_Texture_Cube               = qglGetUniformLocationARB(p->program, "Texture_Cube");
3829                 p->loc_Texture_Refraction         = qglGetUniformLocationARB(p->program, "Texture_Refraction");
3830                 p->loc_Texture_Reflection         = qglGetUniformLocationARB(p->program, "Texture_Reflection");
3831                 p->loc_Texture_ShadowMap2D        = qglGetUniformLocationARB(p->program, "Texture_ShadowMap2D");
3832                 p->loc_Texture_CubeProjection     = qglGetUniformLocationARB(p->program, "Texture_CubeProjection");
3833                 p->loc_Texture_ScreenDepth        = qglGetUniformLocationARB(p->program, "Texture_ScreenDepth");
3834                 p->loc_Texture_ScreenNormalMap    = qglGetUniformLocationARB(p->program, "Texture_ScreenNormalMap");
3835                 p->loc_Texture_ScreenDiffuse      = qglGetUniformLocationARB(p->program, "Texture_ScreenDiffuse");
3836                 p->loc_Texture_ScreenSpecular     = qglGetUniformLocationARB(p->program, "Texture_ScreenSpecular");
3837                 p->loc_Texture_ReflectMask        = qglGetUniformLocationARB(p->program, "Texture_ReflectMask");
3838                 p->loc_Texture_ReflectCube        = qglGetUniformLocationARB(p->program, "Texture_ReflectCube");
3839                 p->loc_Alpha                      = qglGetUniformLocationARB(p->program, "Alpha");
3840                 p->loc_BloomBlur_Parameters       = qglGetUniformLocationARB(p->program, "BloomBlur_Parameters");
3841                 p->loc_ClientTime                 = qglGetUniformLocationARB(p->program, "ClientTime");
3842                 p->loc_Color_Ambient              = qglGetUniformLocationARB(p->program, "Color_Ambient");
3843                 p->loc_Color_Diffuse              = qglGetUniformLocationARB(p->program, "Color_Diffuse");
3844                 p->loc_Color_Specular             = qglGetUniformLocationARB(p->program, "Color_Specular");
3845                 p->loc_Color_Glow                 = qglGetUniformLocationARB(p->program, "Color_Glow");
3846                 p->loc_Color_Pants                = qglGetUniformLocationARB(p->program, "Color_Pants");
3847                 p->loc_Color_Shirt                = qglGetUniformLocationARB(p->program, "Color_Shirt");
3848                 p->loc_DeferredColor_Ambient      = qglGetUniformLocationARB(p->program, "DeferredColor_Ambient");
3849                 p->loc_DeferredColor_Diffuse      = qglGetUniformLocationARB(p->program, "DeferredColor_Diffuse");
3850                 p->loc_DeferredColor_Specular     = qglGetUniformLocationARB(p->program, "DeferredColor_Specular");
3851                 p->loc_DeferredMod_Diffuse        = qglGetUniformLocationARB(p->program, "DeferredMod_Diffuse");
3852                 p->loc_DeferredMod_Specular       = qglGetUniformLocationARB(p->program, "DeferredMod_Specular");
3853                 p->loc_DistortScaleRefractReflect = qglGetUniformLocationARB(p->program, "DistortScaleRefractReflect");
3854                 p->loc_EyePosition                = qglGetUniformLocationARB(p->program, "EyePosition");
3855                 p->loc_FogColor                   = qglGetUniformLocationARB(p->program, "FogColor");
3856                 p->loc_FogHeightFade              = qglGetUniformLocationARB(p->program, "FogHeightFade");
3857                 p->loc_FogPlane                   = qglGetUniformLocationARB(p->program, "FogPlane");
3858                 p->loc_FogPlaneViewDist           = qglGetUniformLocationARB(p->program, "FogPlaneViewDist");
3859                 p->loc_FogRangeRecip              = qglGetUniformLocationARB(p->program, "FogRangeRecip");
3860                 p->loc_LightColor                 = qglGetUniformLocationARB(p->program, "LightColor");
3861                 p->loc_LightDir                   = qglGetUniformLocationARB(p->program, "LightDir");
3862                 p->loc_LightPosition              = qglGetUniformLocationARB(p->program, "LightPosition");
3863                 p->loc_OffsetMapping_Scale        = qglGetUniformLocationARB(p->program, "OffsetMapping_Scale");
3864                 p->loc_PixelSize                  = qglGetUniformLocationARB(p->program, "PixelSize");
3865                 p->loc_ReflectColor               = qglGetUniformLocationARB(p->program, "ReflectColor");
3866                 p->loc_ReflectFactor              = qglGetUniformLocationARB(p->program, "ReflectFactor");
3867                 p->loc_ReflectOffset              = qglGetUniformLocationARB(p->program, "ReflectOffset");
3868                 p->loc_RefractColor               = qglGetUniformLocationARB(p->program, "RefractColor");
3869                 p->loc_Saturation                 = qglGetUniformLocationARB(p->program, "Saturation");
3870                 p->loc_ScreenCenterRefractReflect = qglGetUniformLocationARB(p->program, "ScreenCenterRefractReflect");
3871                 p->loc_ScreenScaleRefractReflect  = qglGetUniformLocationARB(p->program, "ScreenScaleRefractReflect");
3872                 p->loc_ScreenToDepth              = qglGetUniformLocationARB(p->program, "ScreenToDepth");
3873                 p->loc_ShadowMap_Parameters       = qglGetUniformLocationARB(p->program, "ShadowMap_Parameters");
3874                 p->loc_ShadowMap_TextureScale     = qglGetUniformLocationARB(p->program, "ShadowMap_TextureScale");
3875                 p->loc_SpecularPower              = qglGetUniformLocationARB(p->program, "SpecularPower");
3876                 p->loc_UserVec1                   = qglGetUniformLocationARB(p->program, "UserVec1");
3877                 p->loc_UserVec2                   = qglGetUniformLocationARB(p->program, "UserVec2");
3878                 p->loc_UserVec3                   = qglGetUniformLocationARB(p->program, "UserVec3");
3879                 p->loc_UserVec4                   = qglGetUniformLocationARB(p->program, "UserVec4");
3880                 p->loc_ViewTintColor              = qglGetUniformLocationARB(p->program, "ViewTintColor");
3881                 p->loc_ViewToLight                = qglGetUniformLocationARB(p->program, "ViewToLight");
3882                 p->loc_ModelToLight               = qglGetUniformLocationARB(p->program, "ModelToLight");
3883                 p->loc_TexMatrix                  = qglGetUniformLocationARB(p->program, "TexMatrix");
3884                 p->loc_BackgroundTexMatrix        = qglGetUniformLocationARB(p->program, "BackgroundTexMatrix");
3885                 p->loc_ModelViewMatrix            = qglGetUniformLocationARB(p->program, "ModelViewMatrix");
3886                 p->loc_ModelViewProjectionMatrix  = qglGetUniformLocationARB(p->program, "ModelViewProjectionMatrix");
3887                 p->loc_PixelToScreenTexCoord      = qglGetUniformLocationARB(p->program, "PixelToScreenTexCoord");
3888                 p->loc_ModelToReflectCube         = qglGetUniformLocationARB(p->program, "ModelToReflectCube");
3889                 p->loc_ShadowMapMatrix            = qglGetUniformLocationARB(p->program, "ShadowMapMatrix");
3890                 p->loc_BloomColorSubtract         = qglGetUniformLocationARB(p->program, "BloomColorSubtract");
3891                 p->loc_NormalmapScrollBlend       = qglGetUniformLocationARB(p->program, "NormalmapScrollBlend");
3892                 // initialize the samplers to refer to the texture units we use
3893                 if (p->loc_Texture_First           >= 0) qglUniform1iARB(p->loc_Texture_First          , GL20TU_FIRST);
3894                 if (p->loc_Texture_Second          >= 0) qglUniform1iARB(p->loc_Texture_Second         , GL20TU_SECOND);
3895                 if (p->loc_Texture_GammaRamps      >= 0) qglUniform1iARB(p->loc_Texture_GammaRamps     , GL20TU_GAMMARAMPS);
3896                 if (p->loc_Texture_Normal          >= 0) qglUniform1iARB(p->loc_Texture_Normal         , GL20TU_NORMAL);
3897                 if (p->loc_Texture_Color           >= 0) qglUniform1iARB(p->loc_Texture_Color          , GL20TU_COLOR);
3898                 if (p->loc_Texture_Gloss           >= 0) qglUniform1iARB(p->loc_Texture_Gloss          , GL20TU_GLOSS);
3899                 if (p->loc_Texture_Glow            >= 0) qglUniform1iARB(p->loc_Texture_Glow           , GL20TU_GLOW);
3900                 if (p->loc_Texture_SecondaryNormal >= 0) qglUniform1iARB(p->loc_Texture_SecondaryNormal, GL20TU_SECONDARY_NORMAL);
3901                 if (p->loc_Texture_SecondaryColor  >= 0) qglUniform1iARB(p->loc_Texture_SecondaryColor , GL20TU_SECONDARY_COLOR);
3902                 if (p->loc_Texture_SecondaryGloss  >= 0) qglUniform1iARB(p->loc_Texture_SecondaryGloss , GL20TU_SECONDARY_GLOSS);
3903                 if (p->loc_Texture_SecondaryGlow   >= 0) qglUniform1iARB(p->loc_Texture_SecondaryGlow  , GL20TU_SECONDARY_GLOW);
3904                 if (p->loc_Texture_Pants           >= 0) qglUniform1iARB(p->loc_Texture_Pants          , GL20TU_PANTS);
3905                 if (p->loc_Texture_Shirt           >= 0) qglUniform1iARB(p->loc_Texture_Shirt          , GL20TU_SHIRT);
3906                 if (p->loc_Texture_FogHeightTexture>= 0) qglUniform1iARB(p->loc_Texture_FogHeightTexture, GL20TU_FOGHEIGHTTEXTURE);
3907                 if (p->loc_Texture_FogMask         >= 0) qglUniform1iARB(p->loc_Texture_FogMask        , GL20TU_FOGMASK);
3908                 if (p->loc_Texture_Lightmap        >= 0) qglUniform1iARB(p->loc_Texture_Lightmap       , GL20TU_LIGHTMAP);
3909                 if (p->loc_Texture_Deluxemap       >= 0) qglUniform1iARB(p->loc_Texture_Deluxemap      , GL20TU_DELUXEMAP);
3910                 if (p->loc_Texture_Attenuation     >= 0) qglUniform1iARB(p->loc_Texture_Attenuation    , GL20TU_ATTENUATION);
3911                 if (p->loc_Texture_Cube            >= 0) qglUniform1iARB(p->loc_Texture_Cube           , GL20TU_CUBE);
3912                 if (p->loc_Texture_Refraction      >= 0) qglUniform1iARB(p->loc_Texture_Refraction     , GL20TU_REFRACTION);
3913                 if (p->loc_Texture_Reflection      >= 0) qglUniform1iARB(p->loc_Texture_Reflection     , GL20TU_REFLECTION);
3914                 if (p->loc_Texture_ShadowMap2D     >= 0) qglUniform1iARB(p->loc_Texture_ShadowMap2D    , GL20TU_SHADOWMAP2D);
3915                 if (p->loc_Texture_CubeProjection  >= 0) qglUniform1iARB(p->loc_Texture_CubeProjection , GL20TU_CUBEPROJECTION);
3916                 if (p->loc_Texture_ScreenDepth     >= 0) qglUniform1iARB(p->loc_Texture_ScreenDepth    , GL20TU_SCREENDEPTH);
3917                 if (p->loc_Texture_ScreenNormalMap >= 0) qglUniform1iARB(p->loc_Texture_ScreenNormalMap, GL20TU_SCREENNORMALMAP);
3918                 if (p->loc_Texture_ScreenDiffuse   >= 0) qglUniform1iARB(p->loc_Texture_ScreenDiffuse  , GL20TU_SCREENDIFFUSE);
3919                 if (p->loc_Texture_ScreenSpecular  >= 0) qglUniform1iARB(p->loc_Texture_ScreenSpecular , GL20TU_SCREENSPECULAR);
3920                 if (p->loc_Texture_ReflectMask     >= 0) qglUniform1iARB(p->loc_Texture_ReflectMask    , GL20TU_REFLECTMASK);
3921                 if (p->loc_Texture_ReflectCube     >= 0) qglUniform1iARB(p->loc_Texture_ReflectCube    , GL20TU_REFLECTCUBE);
3922                 CHECKGLERROR
3923                 Con_DPrintf("^5GLSL shader %s compiled.\n", permutationname);
3924         }
3925         else
3926                 Con_Printf("^1GLSL shader %s failed!  some features may not work properly.\n", permutationname);
3927
3928         // free the strings
3929         if (vertexstring)
3930                 Mem_Free(vertexstring);
3931         if (geometrystring)
3932                 Mem_Free(geometrystring);
3933         if (fragmentstring)
3934                 Mem_Free(fragmentstring);
3935 }
3936
3937 void R_SetupShader_SetPermutationGLSL(unsigned int mode, unsigned int permutation)
3938 {
3939         r_glsl_permutation_t *perm = R_GLSL_FindPermutation(mode, permutation);
3940         if (r_glsl_permutation != perm)
3941         {
3942                 r_glsl_permutation = perm;
3943                 if (!r_glsl_permutation->program)
3944                 {
3945                         if (!r_glsl_permutation->compiled)
3946                                 R_GLSL_CompilePermutation(perm, mode, permutation);
3947                         if (!r_glsl_permutation->program)
3948                         {
3949                                 // remove features until we find a valid permutation
3950                                 int i;
3951                                 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
3952                                 {
3953                                         // reduce i more quickly whenever it would not remove any bits
3954                                         int j = 1<<(SHADERPERMUTATION_COUNT-1-i);
3955                                         if (!(permutation & j))
3956                                                 continue;
3957                                         permutation -= j;
3958                                         r_glsl_permutation = R_GLSL_FindPermutation(mode, permutation);
3959                                         if (!r_glsl_permutation->compiled)
3960                                                 R_GLSL_CompilePermutation(perm, mode, permutation);
3961                                         if (r_glsl_permutation->program)
3962                                                 break;
3963                                 }
3964                                 if (i >= SHADERPERMUTATION_COUNT)
3965                                 {
3966                                         //Con_Printf("Could not find a working OpenGL 2.0 shader for permutation %s %s\n", shadermodeinfo[mode].vertexfilename, shadermodeinfo[mode].pretext);
3967                                         r_glsl_permutation = R_GLSL_FindPermutation(mode, permutation);
3968                                         qglUseProgramObjectARB(0);CHECKGLERROR
3969                                         return; // no bit left to clear, entire mode is broken
3970                                 }
3971                         }
3972                 }
3973                 CHECKGLERROR
3974                 qglUseProgramObjectARB(r_glsl_permutation->program);CHECKGLERROR
3975         }
3976         if (r_glsl_permutation->loc_ModelViewProjectionMatrix >= 0) qglUniformMatrix4fvARB(r_glsl_permutation->loc_ModelViewProjectionMatrix, 1, false, gl_modelviewprojection16f);
3977         if (r_glsl_permutation->loc_ModelViewMatrix >= 0) qglUniformMatrix4fvARB(r_glsl_permutation->loc_ModelViewMatrix, 1, false, gl_modelview16f);
3978         if (r_glsl_permutation->loc_ClientTime >= 0) qglUniform1fARB(r_glsl_permutation->loc_ClientTime, cl.time);
3979 }
3980
3981 #ifdef SUPPORTCG
3982 #include <Cg/cgGL.h>
3983 struct r_cg_permutation_s;
3984 typedef struct r_cg_permutation_s
3985 {
3986         /// hash lookup data
3987         struct r_cg_permutation_s *hashnext;
3988         unsigned int mode;
3989         unsigned int permutation;
3990
3991         /// indicates if we have tried compiling this permutation already
3992         qboolean compiled;
3993         /// 0 if compilation failed
3994         CGprogram vprogram;
3995         CGprogram fprogram;
3996         /// locations of detected parameters in programs, or NULL if not found
3997         CGparameter vp_EyePosition;
3998         CGparameter vp_FogPlane;
3999         CGparameter vp_LightDir;
4000         CGparameter vp_LightPosition;
4001         CGparameter vp_ModelToLight;
4002         CGparameter vp_TexMatrix;
4003         CGparameter vp_BackgroundTexMatrix;
4004         CGparameter vp_ModelViewProjectionMatrix;
4005         CGparameter vp_ModelViewMatrix;
4006         CGparameter vp_ShadowMapMatrix;
4007
4008         CGparameter fp_Texture_First;
4009         CGparameter fp_Texture_Second;
4010         CGparameter fp_Texture_GammaRamps;
4011         CGparameter fp_Texture_Normal;
4012         CGparameter fp_Texture_Color;
4013         CGparameter fp_Texture_Gloss;
4014         CGparameter fp_Texture_Glow;
4015         CGparameter fp_Texture_SecondaryNormal;
4016         CGparameter fp_Texture_SecondaryColor;
4017         CGparameter fp_Texture_SecondaryGloss;
4018         CGparameter fp_Texture_SecondaryGlow;
4019         CGparameter fp_Texture_Pants;
4020         CGparameter fp_Texture_Shirt;
4021         CGparameter fp_Texture_FogHeightTexture;
4022         CGparameter fp_Texture_FogMask;
4023         CGparameter fp_Texture_Lightmap;
4024         CGparameter fp_Texture_Deluxemap;
4025         CGparameter fp_Texture_Attenuation;
4026         CGparameter fp_Texture_Cube;
4027         CGparameter fp_Texture_Refraction;
4028         CGparameter fp_Texture_Reflection;
4029         CGparameter fp_Texture_ShadowMap2D;
4030         CGparameter fp_Texture_CubeProjection;
4031         CGparameter fp_Texture_ScreenDepth;
4032         CGparameter fp_Texture_ScreenNormalMap;
4033         CGparameter fp_Texture_ScreenDiffuse;
4034         CGparameter fp_Texture_ScreenSpecular;
4035         CGparameter fp_Texture_ReflectMask;
4036         CGparameter fp_Texture_ReflectCube;
4037         CGparameter fp_Alpha;
4038         CGparameter fp_BloomBlur_Parameters;
4039         CGparameter fp_ClientTime;
4040         CGparameter fp_Color_Ambient;
4041         CGparameter fp_Color_Diffuse;
4042         CGparameter fp_Color_Specular;
4043         CGparameter fp_Color_Glow;
4044         CGparameter fp_Color_Pants;
4045         CGparameter fp_Color_Shirt;
4046         CGparameter fp_DeferredColor_Ambient;
4047         CGparameter fp_DeferredColor_Diffuse;
4048         CGparameter fp_DeferredColor_Specular;
4049         CGparameter fp_DeferredMod_Diffuse;
4050         CGparameter fp_DeferredMod_Specular;
4051         CGparameter fp_DistortScaleRefractReflect;
4052         CGparameter fp_EyePosition;
4053         CGparameter fp_FogColor;
4054         CGparameter fp_FogHeightFade;
4055         CGparameter fp_FogPlane;
4056         CGparameter fp_FogPlaneViewDist;
4057         CGparameter fp_FogRangeRecip;
4058         CGparameter fp_LightColor;
4059         CGparameter fp_LightDir;
4060         CGparameter fp_LightPosition;
4061         CGparameter fp_OffsetMapping_Scale;
4062         CGparameter fp_PixelSize;
4063         CGparameter fp_ReflectColor;
4064         CGparameter fp_ReflectFactor;
4065         CGparameter fp_ReflectOffset;
4066         CGparameter fp_RefractColor;
4067         CGparameter fp_Saturation;
4068         CGparameter fp_ScreenCenterRefractReflect;
4069         CGparameter fp_ScreenScaleRefractReflect;
4070         CGparameter fp_ScreenToDepth;
4071         CGparameter fp_ShadowMap_Parameters;
4072         CGparameter fp_ShadowMap_TextureScale;
4073         CGparameter fp_SpecularPower;
4074         CGparameter fp_UserVec1;
4075         CGparameter fp_UserVec2;
4076         CGparameter fp_UserVec3;
4077         CGparameter fp_UserVec4;
4078         CGparameter fp_ViewTintColor;
4079         CGparameter fp_ViewToLight;
4080         CGparameter fp_PixelToScreenTexCoord;
4081         CGparameter fp_ModelToReflectCube;
4082         CGparameter fp_BloomColorSubtract;
4083         CGparameter fp_NormalmapScrollBlend;
4084 }
4085 r_cg_permutation_t;
4086
4087 /// information about each possible shader permutation
4088 r_cg_permutation_t *r_cg_permutationhash[SHADERMODE_COUNT][SHADERPERMUTATION_HASHSIZE];
4089 /// currently selected permutation
4090 r_cg_permutation_t *r_cg_permutation;
4091 /// storage for permutations linked in the hash table
4092 memexpandablearray_t r_cg_permutationarray;
4093
4094 #define CHECKCGERROR {CGerror err = cgGetError(), err2 = err;if (err){Con_Printf("%s:%i CG error %i: %s : %s\n", __FILE__, __LINE__, err, cgGetErrorString(err), cgGetLastErrorString(&err2));if (err == 1) Con_Printf("last listing:\n%s\n", cgGetLastListing(vid.cgcontext));}}
4095
4096 static r_cg_permutation_t *R_CG_FindPermutation(unsigned int mode, unsigned int permutation)
4097 {
4098         //unsigned int hashdepth = 0;
4099         unsigned int hashindex = (permutation * 0x1021) & (SHADERPERMUTATION_HASHSIZE - 1);
4100         r_cg_permutation_t *p;
4101         for (p = r_cg_permutationhash[mode][hashindex];p;p = p->hashnext)
4102         {
4103                 if (p->mode == mode && p->permutation == permutation)
4104                 {
4105                         //if (hashdepth > 10)
4106                         //      Con_Printf("R_CG_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
4107                         return p;
4108                 }
4109                 //hashdepth++;
4110         }
4111         p = (r_cg_permutation_t*)Mem_ExpandableArray_AllocRecord(&r_cg_permutationarray);
4112         p->mode = mode;
4113         p->permutation = permutation;
4114         p->hashnext = r_cg_permutationhash[mode][hashindex];
4115         r_cg_permutationhash[mode][hashindex] = p;
4116         //if (hashdepth > 10)
4117         //      Con_Printf("R_CG_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
4118         return p;
4119 }
4120
4121 static char *R_CG_GetText(const char *filename, qboolean printfromdisknotice)
4122 {
4123         char *shaderstring;
4124         if (!filename || !filename[0])
4125                 return NULL;
4126         if (!strcmp(filename, "cg/default.cg"))
4127         {
4128                 if (!cgshaderstring)
4129                 {
4130                         cgshaderstring = (char *)FS_LoadFile(filename, r_main_mempool, false, NULL);
4131                         if (cgshaderstring)
4132                                 Con_DPrintf("Loading shaders from file %s...\n", filename);
4133                         else
4134                                 cgshaderstring = (char *)builtincgshaderstring;
4135                 }
4136                 shaderstring = (char *) Mem_Alloc(r_main_mempool, strlen(cgshaderstring) + 1);
4137                 memcpy(shaderstring, cgshaderstring, strlen(cgshaderstring) + 1);
4138                 return shaderstring;
4139         }
4140         shaderstring = (char *)FS_LoadFile(filename, r_main_mempool, false, NULL);
4141         if (shaderstring)
4142         {
4143                 if (printfromdisknotice)
4144                         Con_DPrintf("from disk %s... ", filename);
4145                 return shaderstring;
4146         }
4147         return shaderstring;
4148 }
4149
4150 static void R_CG_CacheShader(r_cg_permutation_t *p, const char *cachename, const char *vertstring, const char *fragstring)
4151 {
4152         // TODO: load or create .fp and .vp shader files
4153 }
4154
4155 static void R_CG_CompilePermutation(r_cg_permutation_t *p, unsigned int mode, unsigned int permutation)
4156 {
4157         int i;
4158         shadermodeinfo_t *modeinfo = cgshadermodeinfo + mode;
4159         int vertstring_length = 0;
4160         int geomstring_length = 0;
4161         int fragstring_length = 0;
4162         char *t;
4163         char *vertexstring, *geometrystring, *fragmentstring;
4164         char *vertstring, *geomstring, *fragstring;
4165         char permutationname[256];
4166         char cachename[256];
4167         CGprofile vertexProfile;
4168         CGprofile fragmentProfile;
4169         int vertstrings_count = 0;
4170         int geomstrings_count = 0;
4171         int fragstrings_count = 0;
4172         const char *vertstrings_list[32+3+SHADERSTATICPARMS_COUNT+1];
4173         const char *geomstrings_list[32+3+SHADERSTATICPARMS_COUNT+1];
4174         const char *fragstrings_list[32+3+SHADERSTATICPARMS_COUNT+1];
4175
4176         if (p->compiled)
4177                 return;
4178         p->compiled = true;
4179         p->vprogram = NULL;
4180         p->fprogram = NULL;
4181
4182         permutationname[0] = 0;
4183         cachename[0] = 0;
4184         vertexstring   = R_CG_GetText(modeinfo->vertexfilename, true);
4185         geometrystring = R_CG_GetText(modeinfo->geometryfilename, false);
4186         fragmentstring = R_CG_GetText(modeinfo->fragmentfilename, false);
4187
4188         strlcat(permutationname, modeinfo->vertexfilename, sizeof(permutationname));
4189         strlcat(cachename, "cg/", sizeof(cachename));
4190
4191         // the first pretext is which type of shader to compile as
4192         // (later these will all be bound together as a program object)
4193         vertstrings_list[vertstrings_count++] = "#define VERTEX_SHADER\n";
4194         geomstrings_list[geomstrings_count++] = "#define GEOMETRY_SHADER\n";
4195         fragstrings_list[fragstrings_count++] = "#define FRAGMENT_SHADER\n";
4196
4197         // the second pretext is the mode (for example a light source)
4198         vertstrings_list[vertstrings_count++] = modeinfo->pretext;
4199         geomstrings_list[geomstrings_count++] = modeinfo->pretext;
4200         fragstrings_list[fragstrings_count++] = modeinfo->pretext;
4201         strlcat(permutationname, modeinfo->name, sizeof(permutationname));
4202         strlcat(cachename, modeinfo->name, sizeof(cachename));
4203
4204         // now add all the permutation pretexts
4205         for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
4206         {
4207                 if (permutation & (1<<i))
4208                 {
4209                         vertstrings_list[vertstrings_count++] = shaderpermutationinfo[i].pretext;
4210                         geomstrings_list[geomstrings_count++] = shaderpermutationinfo[i].pretext;
4211                         fragstrings_list[fragstrings_count++] = shaderpermutationinfo[i].pretext;
4212                         strlcat(permutationname, shaderpermutationinfo[i].name, sizeof(permutationname));
4213                         strlcat(cachename, shaderpermutationinfo[i].name, sizeof(cachename));
4214                 }
4215                 else
4216                 {
4217                         // keep line numbers correct
4218                         vertstrings_list[vertstrings_count++] = "\n";
4219                         geomstrings_list[geomstrings_count++] = "\n";
4220                         fragstrings_list[fragstrings_count++] = "\n";
4221                 }
4222         }
4223
4224         // add static parms
4225         R_CompileShader_AddStaticParms(mode, permutation);
4226         memcpy(vertstrings_list + vertstrings_count, shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
4227         vertstrings_count += shaderstaticparms_count;
4228         memcpy(geomstrings_list + geomstrings_count, shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
4229         geomstrings_count += shaderstaticparms_count;
4230         memcpy(fragstrings_list + fragstrings_count, shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
4231         fragstrings_count += shaderstaticparms_count;
4232
4233         // replace spaces in the cachename with _ characters
4234         for (i = 0;cachename[i];i++)
4235                 if (cachename[i] == ' ')
4236                         cachename[i] = '_';
4237
4238         // now append the shader text itself
4239         vertstrings_list[vertstrings_count++] = vertexstring;
4240         geomstrings_list[geomstrings_count++] = geometrystring;
4241         fragstrings_list[fragstrings_count++] = fragmentstring;
4242
4243         // if any sources were NULL, clear the respective list
4244         if (!vertexstring)
4245                 vertstrings_count = 0;
4246         if (!geometrystring)
4247                 geomstrings_count = 0;
4248         if (!fragmentstring)
4249                 fragstrings_count = 0;
4250
4251         vertstring_length = 0;
4252         for (i = 0;i < vertstrings_count;i++)
4253                 vertstring_length += strlen(vertstrings_list[i]);
4254         vertstring = t = Mem_Alloc(tempmempool, vertstring_length + 1);
4255         for (i = 0;i < vertstrings_count;t += strlen(vertstrings_list[i]), i++)
4256                 memcpy(t, vertstrings_list[i], strlen(vertstrings_list[i]));
4257
4258         geomstring_length = 0;
4259         for (i = 0;i < geomstrings_count;i++)
4260                 geomstring_length += strlen(geomstrings_list[i]);
4261         geomstring = t = Mem_Alloc(tempmempool, geomstring_length + 1);
4262         for (i = 0;i < geomstrings_count;t += strlen(geomstrings_list[i]), i++)
4263                 memcpy(t, geomstrings_list[i], strlen(geomstrings_list[i]));
4264
4265         fragstring_length = 0;
4266         for (i = 0;i < fragstrings_count;i++)
4267                 fragstring_length += strlen(fragstrings_list[i]);
4268         fragstring = t = Mem_Alloc(tempmempool, fragstring_length + 1);
4269         for (i = 0;i < fragstrings_count;t += strlen(fragstrings_list[i]), i++)
4270                 memcpy(t, fragstrings_list[i], strlen(fragstrings_list[i]));
4271
4272         CHECKGLERROR
4273         CHECKCGERROR
4274         //vertexProfile = CG_PROFILE_ARBVP1;
4275         //fragmentProfile = CG_PROFILE_ARBFP1;
4276         vertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);CHECKCGERROR
4277         fragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);CHECKCGERROR
4278         //cgGLSetOptimalOptions(vertexProfile);CHECKCGERROR
4279         //cgGLSetOptimalOptions(fragmentProfile);CHECKCGERROR
4280         //cgSetAutoCompile(vid.cgcontext, CG_COMPILE_MANUAL);CHECKCGERROR
4281         CHECKGLERROR
4282
4283         // try to load the cached shader, or generate one
4284         R_CG_CacheShader(p, cachename, vertstring, fragstring);
4285
4286         // if caching failed, do a dynamic compile for now
4287         CHECKCGERROR
4288         if (vertstring[0] && !p->vprogram)
4289                 p->vprogram = cgCreateProgram(vid.cgcontext, CG_SOURCE, vertstring, vertexProfile, NULL, NULL);
4290         CHECKCGERROR
4291         if (fragstring[0] && !p->fprogram)
4292                 p->fprogram = cgCreateProgram(vid.cgcontext, CG_SOURCE, fragstring, fragmentProfile, NULL, NULL);
4293         CHECKCGERROR
4294
4295         // look up all the uniform variable names we care about, so we don't
4296         // have to look them up every time we set them
4297         if (p->vprogram)
4298         {
4299                 CHECKCGERROR
4300                 cgGLLoadProgram(p->vprogram);CHECKCGERROR CHECKGLERROR
4301                 cgGLEnableProfile(vertexProfile);CHECKCGERROR CHECKGLERROR
4302                 p->vp_EyePosition                = cgGetNamedParameter(p->vprogram, "EyePosition");
4303                 p->vp_FogPlane                   = cgGetNamedParameter(p->vprogram, "FogPlane");
4304                 p->vp_LightDir                   = cgGetNamedParameter(p->vprogram, "LightDir");
4305                 p->vp_LightPosition              = cgGetNamedParameter(p->vprogram, "LightPosition");
4306                 p->vp_ModelToLight               = cgGetNamedParameter(p->vprogram, "ModelToLight");
4307                 p->vp_TexMatrix                  = cgGetNamedParameter(p->vprogram, "TexMatrix");
4308                 p->vp_BackgroundTexMatrix        = cgGetNamedParameter(p->vprogram, "BackgroundTexMatrix");
4309                 p->vp_ModelViewProjectionMatrix  = cgGetNamedParameter(p->vprogram, "ModelViewProjectionMatrix");
4310                 p->vp_ModelViewMatrix            = cgGetNamedParameter(p->vprogram, "ModelViewMatrix");
4311                 p->vp_ShadowMapMatrix            = cgGetNamedParameter(p->vprogram, "ShadowMapMatrix");
4312                 CHECKCGERROR
4313         }
4314         if (p->fprogram)
4315         {
4316                 CHECKCGERROR
4317                 cgGLLoadProgram(p->fprogram);CHECKCGERROR CHECKGLERROR
4318                 cgGLEnableProfile(fragmentProfile);CHECKCGERROR CHECKGLERROR
4319                 p->fp_Texture_First              = cgGetNamedParameter(p->fprogram, "Texture_First");
4320                 p->fp_Texture_Second             = cgGetNamedParameter(p->fprogram, "Texture_Second");
4321                 p->fp_Texture_GammaRamps         = cgGetNamedParameter(p->fprogram, "Texture_GammaRamps");
4322                 p->fp_Texture_Normal             = cgGetNamedParameter(p->fprogram, "Texture_Normal");
4323                 p->fp_Texture_Color              = cgGetNamedParameter(p->fprogram, "Texture_Color");
4324                 p->fp_Texture_Gloss              = cgGetNamedParameter(p->fprogram, "Texture_Gloss");
4325                 p->fp_Texture_Glow               = cgGetNamedParameter(p->fprogram, "Texture_Glow");
4326                 p->fp_Texture_SecondaryNormal    = cgGetNamedParameter(p->fprogram, "Texture_SecondaryNormal");
4327                 p->fp_Texture_SecondaryColor     = cgGetNamedParameter(p->fprogram, "Texture_SecondaryColor");
4328                 p->fp_Texture_SecondaryGloss     = cgGetNamedParameter(p->fprogram, "Texture_SecondaryGloss");
4329                 p->fp_Texture_SecondaryGlow      = cgGetNamedParameter(p->fprogram, "Texture_SecondaryGlow");
4330                 p->fp_Texture_Pants              = cgGetNamedParameter(p->fprogram, "Texture_Pants");
4331                 p->fp_Texture_Shirt              = cgGetNamedParameter(p->fprogram, "Texture_Shirt");
4332                 p->fp_Texture_FogHeightTexture   = cgGetNamedParameter(p->fprogram, "Texture_FogHeightTexture");
4333                 p->fp_Texture_FogMask            = cgGetNamedParameter(p->fprogram, "Texture_FogMask");
4334                 p->fp_Texture_Lightmap           = cgGetNamedParameter(p->fprogram, "Texture_Lightmap");
4335                 p->fp_Texture_Deluxemap          = cgGetNamedParameter(p->fprogram, "Texture_Deluxemap");
4336                 p->fp_Texture_Attenuation        = cgGetNamedParameter(p->fprogram, "Texture_Attenuation");
4337                 p->fp_Texture_Cube               = cgGetNamedParameter(p->fprogram, "Texture_Cube");
4338                 p->fp_Texture_Refraction         = cgGetNamedParameter(p->fprogram, "Texture_Refraction");
4339                 p->fp_Texture_Reflection         = cgGetNamedParameter(p->fprogram, "Texture_Reflection");
4340                 p->fp_Texture_ShadowMap2D        = cgGetNamedParameter(p->fprogram, "Texture_ShadowMap2D");
4341                 p->fp_Texture_CubeProjection     = cgGetNamedParameter(p->fprogram, "Texture_CubeProjection");
4342                 p->fp_Texture_ScreenDepth        = cgGetNamedParameter(p->fprogram, "Texture_ScreenDepth");
4343                 p->fp_Texture_ScreenNormalMap    = cgGetNamedParameter(p->fprogram, "Texture_ScreenNormalMap");
4344                 p->fp_Texture_ScreenDiffuse      = cgGetNamedParameter(p->fprogram, "Texture_ScreenDiffuse");
4345                 p->fp_Texture_ScreenSpecular     = cgGetNamedParameter(p->fprogram, "Texture_ScreenSpecular");
4346                 p->fp_Texture_ReflectMask        = cgGetNamedParameter(p->fprogram, "Texture_ReflectMask");
4347                 p->fp_Texture_ReflectCube        = cgGetNamedParameter(p->fprogram, "Texture_ReflectCube");
4348                 p->fp_Alpha                      = cgGetNamedParameter(p->fprogram, "Alpha");
4349                 p->fp_BloomBlur_Parameters       = cgGetNamedParameter(p->fprogram, "BloomBlur_Parameters");
4350                 p->fp_ClientTime                 = cgGetNamedParameter(p->fprogram, "ClientTime");
4351                 p->fp_Color_Ambient              = cgGetNamedParameter(p->fprogram, "Color_Ambient");
4352                 p->fp_Color_Diffuse              = cgGetNamedParameter(p->fprogram, "Color_Diffuse");
4353                 p->fp_Color_Specular             = cgGetNamedParameter(p->fprogram, "Color_Specular");
4354                 p->fp_Color_Glow                 = cgGetNamedParameter(p->fprogram, "Color_Glow");
4355                 p->fp_Color_Pants                = cgGetNamedParameter(p->fprogram, "Color_Pants");
4356                 p->fp_Color_Shirt                = cgGetNamedParameter(p->fprogram, "Color_Shirt");
4357                 p->fp_DeferredColor_Ambient      = cgGetNamedParameter(p->fprogram, "DeferredColor_Ambient");
4358                 p->fp_DeferredColor_Diffuse      = cgGetNamedParameter(p->fprogram, "DeferredColor_Diffuse");
4359                 p->fp_DeferredColor_Specular     = cgGetNamedParameter(p->fprogram, "DeferredColor_Specular");
4360                 p->fp_DeferredMod_Diffuse        = cgGetNamedParameter(p->fprogram, "DeferredMod_Diffuse");
4361                 p->fp_DeferredMod_Specular       = cgGetNamedParameter(p->fprogram, "DeferredMod_Specular");
4362                 p->fp_DistortScaleRefractReflect = cgGetNamedParameter(p->fprogram, "DistortScaleRefractReflect");
4363                 p->fp_EyePosition                = cgGetNamedParameter(p->fprogram, "EyePosition");
4364                 p->fp_FogColor                   = cgGetNamedParameter(p->fprogram, "FogColor");
4365                 p->fp_FogHeightFade              = cgGetNamedParameter(p->fprogram, "FogHeightFade");
4366                 p->fp_FogPlane                   = cgGetNamedParameter(p->fprogram, "FogPlane");
4367                 p->fp_FogPlaneViewDist           = cgGetNamedParameter(p->fprogram, "FogPlaneViewDist");
4368                 p->fp_FogRangeRecip              = cgGetNamedParameter(p->fprogram, "FogRangeRecip");
4369                 p->fp_LightColor                 = cgGetNamedParameter(p->fprogram, "LightColor");
4370                 p->fp_LightDir                   = cgGetNamedParameter(p->fprogram, "LightDir");
4371                 p->fp_LightPosition              = cgGetNamedParameter(p->fprogram, "LightPosition");
4372                 p->fp_OffsetMapping_Scale        = cgGetNamedParameter(p->fprogram, "OffsetMapping_Scale");
4373                 p->fp_PixelSize                  = cgGetNamedParameter(p->fprogram, "PixelSize");
4374                 p->fp_ReflectColor               = cgGetNamedParameter(p->fprogram, "ReflectColor");
4375                 p->fp_ReflectFactor              = cgGetNamedParameter(p->fprogram, "ReflectFactor");
4376                 p->fp_ReflectOffset              = cgGetNamedParameter(p->fprogram, "ReflectOffset");
4377                 p->fp_RefractColor               = cgGetNamedParameter(p->fprogram, "RefractColor");
4378                 p->fp_Saturation                 = cgGetNamedParameter(p->fprogram, "Saturation");
4379                 p->fp_ScreenCenterRefractReflect = cgGetNamedParameter(p->fprogram, "ScreenCenterRefractReflect");
4380                 p->fp_ScreenScaleRefractReflect  = cgGetNamedParameter(p->fprogram, "ScreenScaleRefractReflect");
4381                 p->fp_ScreenToDepth              = cgGetNamedParameter(p->fprogram, "ScreenToDepth");
4382                 p->fp_ShadowMap_Parameters       = cgGetNamedParameter(p->fprogram, "ShadowMap_Parameters");
4383                 p->fp_ShadowMap_TextureScale     = cgGetNamedParameter(p->fprogram, "ShadowMap_TextureScale");
4384                 p->fp_SpecularPower              = cgGetNamedParameter(p->fprogram, "SpecularPower");
4385                 p->fp_UserVec1                   = cgGetNamedParameter(p->fprogram, "UserVec1");
4386                 p->fp_UserVec2                   = cgGetNamedParameter(p->fprogram, "UserVec2");
4387                 p->fp_UserVec3                   = cgGetNamedParameter(p->fprogram, "UserVec3");
4388                 p->fp_UserVec4                   = cgGetNamedParameter(p->fprogram, "UserVec4");
4389                 p->fp_ViewTintColor              = cgGetNamedParameter(p->fprogram, "ViewTintColor");
4390                 p->fp_ViewToLight                = cgGetNamedParameter(p->fprogram, "ViewToLight");
4391                 p->fp_PixelToScreenTexCoord      = cgGetNamedParameter(p->fprogram, "PixelToScreenTexCoord");
4392                 p->fp_ModelToReflectCube         = cgGetNamedParameter(p->fprogram, "ModelToReflectCube");
4393                 p->fp_BloomColorSubtract         = cgGetNamedParameter(p->fprogram, "BloomColorSubtract");
4394                 p->fp_NormalmapScrollBlend       = cgGetNamedParameter(p->fprogram, "NormalmapScrollBlend");
4395                 CHECKCGERROR
4396         }
4397
4398         if ((p->vprogram || !vertstring[0]) && (p->fprogram || !fragstring[0]))
4399                 Con_DPrintf("^5CG shader %s compiled.\n", permutationname);
4400         else
4401                 Con_Printf("^1CG shader %s failed!  some features may not work properly.\n", permutationname);
4402
4403         // free the strings
4404         if (vertstring)
4405                 Mem_Free(vertstring);
4406         if (geomstring)
4407                 Mem_Free(geomstring);
4408         if (fragstring)
4409                 Mem_Free(fragstring);
4410         if (vertexstring)
4411                 Mem_Free(vertexstring);
4412         if (geometrystring)
4413                 Mem_Free(geometrystring);
4414         if (fragmentstring)
4415                 Mem_Free(fragmentstring);
4416 }
4417
4418 void R_SetupShader_SetPermutationCG(unsigned int mode, unsigned int permutation)
4419 {
4420         r_cg_permutation_t *perm = R_CG_FindPermutation(mode, permutation);
4421         CHECKGLERROR
4422         CHECKCGERROR
4423         if (r_cg_permutation != perm)
4424         {
4425                 r_cg_permutation = perm;
4426                 if (!r_cg_permutation->vprogram && !r_cg_permutation->fprogram)
4427                 {
4428                         if (!r_cg_permutation->compiled)
4429                                 R_CG_CompilePermutation(perm, mode, permutation);
4430                         if (!r_cg_permutation->vprogram && !r_cg_permutation->fprogram)
4431                         {
4432                                 // remove features until we find a valid permutation
4433                                 int i;
4434                                 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
4435                                 {
4436                                         // reduce i more quickly whenever it would not remove any bits
4437                                         int j = 1<<(SHADERPERMUTATION_COUNT-1-i);
4438                                         if (!(permutation & j))
4439                                                 continue;
4440                                         permutation -= j;
4441                                         r_cg_permutation = R_CG_FindPermutation(mode, permutation);
4442                                         if (!r_cg_permutation->compiled)
4443                                                 R_CG_CompilePermutation(perm, mode, permutation);
4444                                         if (r_cg_permutation->vprogram || r_cg_permutation->fprogram)
4445                                                 break;
4446                                 }
4447                                 if (i >= SHADERPERMUTATION_COUNT)
4448                                 {
4449                                         //Con_Printf("Could not find a working Cg shader for permutation %s %s\n", shadermodeinfo[mode].vertexfilename, shadermodeinfo[mode].pretext);
4450                                         r_cg_permutation = R_CG_FindPermutation(mode, permutation);
4451                                         return; // no bit left to clear, entire mode is broken
4452                                 }
4453                         }
4454                 }
4455                 CHECKGLERROR
4456                 CHECKCGERROR
4457                 if (r_cg_permutation->vprogram)
4458                 {