Fix MSVC++ 2015 warnings about variable scope and some narrowing conversions without...
[xonotic/darkplaces.git] / gl_backend.c
1
2 #include "quakedef.h"
3 #include "cl_collision.h"
4 #include "dpsoftrast.h"
5 #ifdef SUPPORTD3D
6 #include <d3d9.h>
7 extern LPDIRECT3DDEVICE9 vid_d3d9dev;
8 extern D3DCAPS9 vid_d3d9caps;
9 #endif
10
11 // on GLES we have to use some proper #define's
12 #ifndef GL_FRAMEBUFFER
13 #define GL_FRAMEBUFFER                                   0x8D40
14 #define GL_DEPTH_ATTACHMENT                              0x8D00
15 #define GL_COLOR_ATTACHMENT0                             0x8CE0
16 #define GL_INVALID_FRAMEBUFFER_OPERATION                 0x0506
17 #endif
18 #ifndef GL_COLOR_ATTACHMENT1
19 #define GL_COLOR_ATTACHMENT1                             0x8CE1
20 #define GL_COLOR_ATTACHMENT2                             0x8CE2
21 #define GL_COLOR_ATTACHMENT3                             0x8CE3
22 #define GL_COLOR_ATTACHMENT4                             0x8CE4
23 #define GL_COLOR_ATTACHMENT5                             0x8CE5
24 #define GL_COLOR_ATTACHMENT6                             0x8CE6
25 #define GL_COLOR_ATTACHMENT7                             0x8CE7
26 #define GL_COLOR_ATTACHMENT8                             0x8CE8
27 #define GL_COLOR_ATTACHMENT9                             0x8CE9
28 #define GL_COLOR_ATTACHMENT10                            0x8CEA
29 #define GL_COLOR_ATTACHMENT11                            0x8CEB
30 #define GL_COLOR_ATTACHMENT12                            0x8CEC
31 #define GL_COLOR_ATTACHMENT13                            0x8CED
32 #define GL_COLOR_ATTACHMENT14                            0x8CEE
33 #define GL_COLOR_ATTACHMENT15                            0x8CEF
34 #endif
35 #ifndef GL_ARRAY_BUFFER
36 #define GL_ARRAY_BUFFER               0x8892
37 #define GL_ELEMENT_ARRAY_BUFFER       0x8893
38 #endif
39 //#ifndef GL_VERTEX_ARRAY
40 //#define GL_VERTEX_ARRAY                               0x8074
41 //#define GL_COLOR_ARRAY                                0x8076
42 //#define GL_TEXTURE_COORD_ARRAY                        0x8078
43 //#endif
44 #ifndef GL_TEXTURE0
45 #define GL_TEXTURE0                                     0x84C0
46 #define GL_TEXTURE1                                     0x84C1
47 #define GL_TEXTURE2                                     0x84C2
48 #define GL_TEXTURE3                                     0x84C3
49 #define GL_TEXTURE4                                     0x84C4
50 #define GL_TEXTURE5                                     0x84C5
51 #define GL_TEXTURE6                                     0x84C6
52 #define GL_TEXTURE7                                     0x84C7
53 #define GL_TEXTURE8                                     0x84C8
54 #define GL_TEXTURE9                                     0x84C9
55 #define GL_TEXTURE10                            0x84CA
56 #define GL_TEXTURE11                            0x84CB
57 #define GL_TEXTURE12                            0x84CC
58 #define GL_TEXTURE13                            0x84CD
59 #define GL_TEXTURE14                            0x84CE
60 #define GL_TEXTURE15                            0x84CF
61 #define GL_TEXTURE16                            0x84D0
62 #define GL_TEXTURE17                            0x84D1
63 #define GL_TEXTURE18                            0x84D2
64 #define GL_TEXTURE19                            0x84D3
65 #define GL_TEXTURE20                            0x84D4
66 #define GL_TEXTURE21                            0x84D5
67 #define GL_TEXTURE22                            0x84D6
68 #define GL_TEXTURE23                            0x84D7
69 #define GL_TEXTURE24                            0x84D8
70 #define GL_TEXTURE25                            0x84D9
71 #define GL_TEXTURE26                            0x84DA
72 #define GL_TEXTURE27                            0x84DB
73 #define GL_TEXTURE28                            0x84DC
74 #define GL_TEXTURE29                            0x84DD
75 #define GL_TEXTURE30                            0x84DE
76 #define GL_TEXTURE31                            0x84DF
77 #endif
78
79 #ifndef GL_TEXTURE_3D
80 #define GL_TEXTURE_3D                           0x806F
81 #endif
82 #ifndef GL_TEXTURE_CUBE_MAP
83 #define GL_TEXTURE_CUBE_MAP                 0x8513
84 #endif
85 //#ifndef GL_MODELVIEW
86 //#define GL_MODELVIEW                          0x1700
87 //#endif
88 //#ifndef GL_PROJECTION
89 //#define GL_PROJECTION                         0x1701
90 //#endif
91 //#ifndef GL_DECAL
92 //#define GL_DECAL                              0x2101
93 //#endif
94 //#ifndef GL_INTERPOLATE
95 //#define GL_INTERPOLATE                                0x8575
96 //#endif
97
98
99 #define MAX_RENDERTARGETS 4
100
101 cvar_t gl_mesh_drawrangeelements = {0, "gl_mesh_drawrangeelements", "1", "use glDrawRangeElements function if available instead of glDrawElements (for performance comparisons or bug testing)"};
102 cvar_t gl_mesh_testmanualfeeding = {0, "gl_mesh_testmanualfeeding", "0", "use glBegin(GL_TRIANGLES);glTexCoord2f();glVertex3f();glEnd(); primitives instead of glDrawElements (useful to test for driver bugs with glDrawElements)"};
103 cvar_t gl_paranoid = {0, "gl_paranoid", "0", "enables OpenGL error checking and other tests"};
104 cvar_t gl_printcheckerror = {0, "gl_printcheckerror", "0", "prints all OpenGL error checks, useful to identify location of driver crashes"};
105
106 cvar_t r_render = {0, "r_render", "1", "enables rendering 3D views (you want this on!)"};
107 cvar_t r_renderview = {0, "r_renderview", "1", "enables rendering 3D views (you want this on!)"};
108 cvar_t r_waterwarp = {CVAR_SAVE, "r_waterwarp", "1", "warp view while underwater"};
109 cvar_t gl_polyblend = {CVAR_SAVE, "gl_polyblend", "1", "tints view while underwater, hurt, etc"};
110 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1", "enables OpenGL dithering (16bit looks bad with this off)"};
111 cvar_t gl_vbo = {CVAR_SAVE, "gl_vbo", "3", "make use of GL_ARB_vertex_buffer_object extension to store static geometry in video memory for faster rendering, 0 disables VBO allocation or use, 1 enables VBOs for vertex and triangle data, 2 only for vertex data, 3 for vertex data and triangle data of simple meshes (ones with only one surface)"};
112 cvar_t gl_vbo_dynamicvertex = {CVAR_SAVE, "gl_vbo_dynamicvertex", "0", "make use of GL_ARB_vertex_buffer_object extension when rendering dynamic (animated/procedural) geometry such as text and particles"};
113 cvar_t gl_vbo_dynamicindex = {CVAR_SAVE, "gl_vbo_dynamicindex", "0", "make use of GL_ARB_vertex_buffer_object extension when rendering dynamic (animated/procedural) geometry such as text and particles"};
114 cvar_t gl_fbo = {CVAR_SAVE, "gl_fbo", "1", "make use of GL_ARB_framebuffer_object extension to enable shadowmaps and other features using pixel formats different from the framebuffer"};
115
116 cvar_t v_flipped = {0, "v_flipped", "0", "mirror the screen (poor man's left handed mode)"};
117 qboolean v_flipped_state = false;
118
119 r_viewport_t gl_viewport;
120 matrix4x4_t gl_modelmatrix;
121 matrix4x4_t gl_viewmatrix;
122 matrix4x4_t gl_modelviewmatrix;
123 matrix4x4_t gl_projectionmatrix;
124 matrix4x4_t gl_modelviewprojectionmatrix;
125 float gl_modelview16f[16];
126 float gl_modelviewprojection16f[16];
127 qboolean gl_modelmatrixchanged;
128
129 int gl_maxdrawrangeelementsvertices;
130 int gl_maxdrawrangeelementsindices;
131
132 #ifdef DEBUGGL
133 int gl_errornumber = 0;
134
135 void GL_PrintError(int errornumber, const char *filename, int linenumber)
136 {
137         switch(errornumber)
138         {
139 #ifdef GL_INVALID_ENUM
140         case GL_INVALID_ENUM:
141                 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
142                 break;
143 #endif
144 #ifdef GL_INVALID_VALUE
145         case GL_INVALID_VALUE:
146                 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
147                 break;
148 #endif
149 #ifdef GL_INVALID_OPERATION
150         case GL_INVALID_OPERATION:
151                 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
152                 break;
153 #endif
154 #ifdef GL_STACK_OVERFLOW
155         case GL_STACK_OVERFLOW:
156                 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
157                 break;
158 #endif
159 #ifdef GL_STACK_UNDERFLOW
160         case GL_STACK_UNDERFLOW:
161                 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
162                 break;
163 #endif
164 #ifdef GL_OUT_OF_MEMORY
165         case GL_OUT_OF_MEMORY:
166                 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
167                 break;
168 #endif
169 #ifdef GL_TABLE_TOO_LARGE
170         case GL_TABLE_TOO_LARGE:
171                 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
172                 break;
173 #endif
174 #ifdef GL_INVALID_FRAMEBUFFER_OPERATION
175         case GL_INVALID_FRAMEBUFFER_OPERATION:
176                 Con_Printf("GL_INVALID_FRAMEBUFFER_OPERATION at %s:%i\n", filename, linenumber);
177                 break;
178 #endif
179         default:
180                 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
181                 break;
182         }
183 }
184 #endif
185
186 #define BACKENDACTIVECHECK if (!gl_state.active) Sys_Error("GL backend function called when backend is not active");
187
188 void SCR_ScreenShot_f (void);
189
190 typedef struct gltextureunit_s
191 {
192         int pointer_texcoord_components;
193         int pointer_texcoord_gltype;
194         size_t pointer_texcoord_stride;
195         const void *pointer_texcoord_pointer;
196         const r_meshbuffer_t *pointer_texcoord_vertexbuffer;
197         size_t pointer_texcoord_offset;
198
199         rtexture_t *texture;
200         int t2d, t3d, tcubemap;
201         int arrayenabled;
202         int rgbscale, alphascale;
203         int combine;
204         int combinergb, combinealpha;
205         // texmatrixenabled exists only to avoid unnecessary texmatrix compares
206         int texmatrixenabled;
207         matrix4x4_t matrix;
208 }
209 gltextureunit_t;
210
211 typedef struct gl_state_s
212 {
213         int cullface;
214         int cullfaceenable;
215         int blendfunc1;
216         int blendfunc2;
217         qboolean blend;
218         GLboolean depthmask;
219         int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
220         int depthtest;
221         int depthfunc;
222         float depthrange[2];
223         float polygonoffset[2];
224         int alphatest;
225         int alphafunc;
226         float alphafuncvalue;
227         qboolean alphatocoverage;
228         int scissortest;
229         unsigned int unit;
230         unsigned int clientunit;
231         gltextureunit_t units[MAX_TEXTUREUNITS];
232         float color4f[4];
233         int lockrange_first;
234         int lockrange_count;
235         int vertexbufferobject;
236         int elementbufferobject;
237         int uniformbufferobject;
238         int framebufferobject;
239         int defaultframebufferobject; // deal with platforms that use a non-zero default fbo
240         qboolean pointer_color_enabled;
241
242         int pointer_vertex_components;
243         int pointer_vertex_gltype;
244         size_t pointer_vertex_stride;
245         const void *pointer_vertex_pointer;
246         const r_meshbuffer_t *pointer_vertex_vertexbuffer;
247         size_t pointer_vertex_offset;
248
249         int pointer_color_components;
250         int pointer_color_gltype;
251         size_t pointer_color_stride;
252         const void *pointer_color_pointer;
253         const r_meshbuffer_t *pointer_color_vertexbuffer;
254         size_t pointer_color_offset;
255
256         void *preparevertices_tempdata;
257         size_t preparevertices_tempdatamaxsize;
258         r_vertexgeneric_t *preparevertices_vertexgeneric;
259         r_vertexmesh_t *preparevertices_vertexmesh;
260         int preparevertices_numvertices;
261
262         qboolean usevbo_staticvertex;
263         qboolean usevbo_staticindex;
264         qboolean usevbo_dynamicvertex;
265         qboolean usevbo_dynamicindex;
266
267         memexpandablearray_t meshbufferarray;
268
269         qboolean active;
270
271 #ifdef SUPPORTD3D
272 //      rtexture_t *d3drt_depthtexture;
273 //      rtexture_t *d3drt_colortextures[MAX_RENDERTARGETS];
274         IDirect3DSurface9 *d3drt_depthsurface;
275         IDirect3DSurface9 *d3drt_colorsurfaces[MAX_RENDERTARGETS];
276         IDirect3DSurface9 *d3drt_backbufferdepthsurface;
277         IDirect3DSurface9 *d3drt_backbuffercolorsurface;
278         void *d3dvertexbuffer;
279         void *d3dvertexdata;
280         int d3dvertexsize;
281 #endif
282 }
283 gl_state_t;
284
285 static gl_state_t gl_state;
286
287
288 /*
289 note: here's strip order for a terrain row:
290 0--1--2--3--4
291 |\ |\ |\ |\ |
292 | \| \| \| \|
293 A--B--C--D--E
294 clockwise
295
296 A0B, 01B, B1C, 12C, C2D, 23D, D3E, 34E
297
298 *elements++ = i + row;
299 *elements++ = i;
300 *elements++ = i + row + 1;
301 *elements++ = i;
302 *elements++ = i + 1;
303 *elements++ = i + row + 1;
304
305
306 for (y = 0;y < rows - 1;y++)
307 {
308         for (x = 0;x < columns - 1;x++)
309         {
310                 i = y * rows + x;
311                 *elements++ = i + columns;
312                 *elements++ = i;
313                 *elements++ = i + columns + 1;
314                 *elements++ = i;
315                 *elements++ = i + 1;
316                 *elements++ = i + columns + 1;
317         }
318 }
319
320 alternative:
321 0--1--2--3--4
322 | /| /|\ | /|
323 |/ |/ | \|/ |
324 A--B--C--D--E
325 counterclockwise
326
327 for (y = 0;y < rows - 1;y++)
328 {
329         for (x = 0;x < columns - 1;x++)
330         {
331                 i = y * rows + x;
332                 *elements++ = i;
333                 *elements++ = i + columns;
334                 *elements++ = i + columns + 1;
335                 *elements++ = i + columns;
336                 *elements++ = i + columns + 1;
337                 *elements++ = i + 1;
338         }
339 }
340 */
341
342 int polygonelement3i[(POLYGONELEMENTS_MAXPOINTS-2)*3];
343 unsigned short polygonelement3s[(POLYGONELEMENTS_MAXPOINTS-2)*3];
344 int quadelement3i[QUADELEMENTS_MAXQUADS*6];
345 unsigned short quadelement3s[QUADELEMENTS_MAXQUADS*6];
346
347 static void GL_VBOStats_f(void)
348 {
349         GL_Mesh_ListVBOs(true);
350 }
351
352 static void GL_Backend_ResetState(void);
353
354 static void R_Mesh_InitVertexDeclarations(void);
355 static void R_Mesh_DestroyVertexDeclarations(void);
356
357 static void R_Mesh_SetUseVBO(void)
358 {
359         switch(vid.renderpath)
360         {
361         case RENDERPATH_GL11:
362         case RENDERPATH_GL13:
363         case RENDERPATH_GL20:
364         case RENDERPATH_GLES1:
365                 gl_state.usevbo_staticvertex = (vid.support.arb_vertex_buffer_object && gl_vbo.integer) || vid.forcevbo;
366                 gl_state.usevbo_staticindex = (vid.support.arb_vertex_buffer_object && (gl_vbo.integer == 1 || gl_vbo.integer == 3)) || vid.forcevbo;
367                 gl_state.usevbo_dynamicvertex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicvertex.integer && gl_vbo.integer) || vid.forcevbo;
368                 gl_state.usevbo_dynamicindex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicindex.integer && gl_vbo.integer) || vid.forcevbo;
369                 break;
370         case RENDERPATH_D3D9:
371                 gl_state.usevbo_staticvertex = gl_state.usevbo_staticindex = (vid.support.arb_vertex_buffer_object && gl_vbo.integer) || vid.forcevbo;
372                 gl_state.usevbo_dynamicvertex = gl_state.usevbo_dynamicindex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicvertex.integer && gl_vbo_dynamicindex.integer && gl_vbo.integer) || vid.forcevbo;
373                 break;
374         case RENDERPATH_D3D10:
375                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
376                 break;
377         case RENDERPATH_D3D11:
378                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
379                 break;
380         case RENDERPATH_SOFT:
381                 gl_state.usevbo_staticvertex = false;
382                 gl_state.usevbo_staticindex = false;
383                 gl_state.usevbo_dynamicvertex = false;
384                 gl_state.usevbo_dynamicindex = false;
385                 break;
386         case RENDERPATH_GLES2:
387                 gl_state.usevbo_staticvertex = (vid.support.arb_vertex_buffer_object && gl_vbo.integer) || vid.forcevbo;
388                 gl_state.usevbo_staticindex = (vid.support.arb_vertex_buffer_object && gl_vbo.integer) || vid.forcevbo;
389                 gl_state.usevbo_dynamicvertex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicvertex.integer) || vid.forcevbo;
390                 gl_state.usevbo_dynamicindex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicindex.integer) || vid.forcevbo;
391                 break;
392         }
393 }
394
395 static void gl_backend_start(void)
396 {
397         memset(&gl_state, 0, sizeof(gl_state));
398
399         R_Mesh_InitVertexDeclarations();
400
401         R_Mesh_SetUseVBO();
402         Mem_ExpandableArray_NewArray(&gl_state.meshbufferarray, r_main_mempool, sizeof(r_meshbuffer_t), 128);
403
404         Con_DPrintf("OpenGL backend started.\n");
405
406         CHECKGLERROR
407
408         GL_Backend_ResetState();
409
410         switch(vid.renderpath)
411         {
412         case RENDERPATH_GL11:
413         case RENDERPATH_GL13:
414         case RENDERPATH_GL20:
415         case RENDERPATH_GLES1:
416         case RENDERPATH_GLES2:
417                 // fetch current fbo here (default fbo is not 0 on some GLES devices)
418                 if (vid.support.ext_framebuffer_object)
419                         qglGetIntegerv(GL_FRAMEBUFFER_BINDING, &gl_state.defaultframebufferobject);
420                 break;
421         case RENDERPATH_D3D9:
422 #ifdef SUPPORTD3D
423                 IDirect3DDevice9_GetDepthStencilSurface(vid_d3d9dev, &gl_state.d3drt_backbufferdepthsurface);
424                 IDirect3DDevice9_GetRenderTarget(vid_d3d9dev, 0, &gl_state.d3drt_backbuffercolorsurface);
425 #endif
426                 break;
427         case RENDERPATH_D3D10:
428                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
429                 break;
430         case RENDERPATH_D3D11:
431                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
432                 break;
433         case RENDERPATH_SOFT:
434                 break;
435         }
436 }
437
438 static void gl_backend_shutdown(void)
439 {
440         Con_DPrint("OpenGL Backend shutting down\n");
441
442         switch(vid.renderpath)
443         {
444         case RENDERPATH_GL11:
445         case RENDERPATH_GL13:
446         case RENDERPATH_GL20:
447         case RENDERPATH_SOFT:
448         case RENDERPATH_GLES1:
449         case RENDERPATH_GLES2:
450                 break;
451         case RENDERPATH_D3D9:
452 #ifdef SUPPORTD3D
453                 IDirect3DSurface9_Release(gl_state.d3drt_backbufferdepthsurface);
454                 IDirect3DSurface9_Release(gl_state.d3drt_backbuffercolorsurface);
455 #endif
456                 break;
457         case RENDERPATH_D3D10:
458                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
459                 break;
460         case RENDERPATH_D3D11:
461                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
462                 break;
463         }
464
465         if (gl_state.preparevertices_tempdata)
466                 Mem_Free(gl_state.preparevertices_tempdata);
467
468         Mem_ExpandableArray_FreeArray(&gl_state.meshbufferarray);
469
470         R_Mesh_DestroyVertexDeclarations();
471
472         memset(&gl_state, 0, sizeof(gl_state));
473 }
474
475 static void gl_backend_newmap(void)
476 {
477 }
478
479 static void gl_backend_devicelost(void)
480 {
481         int i, endindex;
482         r_meshbuffer_t *buffer;
483 #ifdef SUPPORTD3D
484         gl_state.d3dvertexbuffer = NULL;
485 #endif
486         switch(vid.renderpath)
487         {
488         case RENDERPATH_GL11:
489         case RENDERPATH_GL13:
490         case RENDERPATH_GL20:
491         case RENDERPATH_SOFT:
492         case RENDERPATH_GLES1:
493         case RENDERPATH_GLES2:
494                 break;
495         case RENDERPATH_D3D9:
496 #ifdef SUPPORTD3D
497                 IDirect3DSurface9_Release(gl_state.d3drt_backbufferdepthsurface);
498                 IDirect3DSurface9_Release(gl_state.d3drt_backbuffercolorsurface);
499 #endif
500                 break;
501         case RENDERPATH_D3D10:
502                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
503                 break;
504         case RENDERPATH_D3D11:
505                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
506                 break;
507         }
508         endindex = (int)Mem_ExpandableArray_IndexRange(&gl_state.meshbufferarray);
509         for (i = 0;i < endindex;i++)
510         {
511                 buffer = (r_meshbuffer_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.meshbufferarray, i);
512                 if (!buffer || !buffer->isdynamic)
513                         continue;
514                 switch(vid.renderpath)
515                 {
516                 case RENDERPATH_GL11:
517                 case RENDERPATH_GL13:
518                 case RENDERPATH_GL20:
519                 case RENDERPATH_SOFT:
520                 case RENDERPATH_GLES1:
521                 case RENDERPATH_GLES2:
522                         break;
523                 case RENDERPATH_D3D9:
524 #ifdef SUPPORTD3D
525                         if (buffer->devicebuffer)
526                         {
527                                 if (buffer->isindexbuffer)
528                                         IDirect3DIndexBuffer9_Release((IDirect3DIndexBuffer9*)buffer->devicebuffer);
529                                 else
530                                         IDirect3DVertexBuffer9_Release((IDirect3DVertexBuffer9*)buffer->devicebuffer);
531                                 buffer->devicebuffer = NULL;
532                         }
533 #endif
534                         break;
535                 case RENDERPATH_D3D10:
536                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
537                         break;
538                 case RENDERPATH_D3D11:
539                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
540                         break;
541                 }
542         }
543 }
544
545 static void gl_backend_devicerestored(void)
546 {
547         switch(vid.renderpath)
548         {
549         case RENDERPATH_GL11:
550         case RENDERPATH_GL13:
551         case RENDERPATH_GL20:
552         case RENDERPATH_SOFT:
553         case RENDERPATH_GLES1:
554         case RENDERPATH_GLES2:
555                 break;
556         case RENDERPATH_D3D9:
557 #ifdef SUPPORTD3D
558                 IDirect3DDevice9_GetDepthStencilSurface(vid_d3d9dev, &gl_state.d3drt_backbufferdepthsurface);
559                 IDirect3DDevice9_GetRenderTarget(vid_d3d9dev, 0, &gl_state.d3drt_backbuffercolorsurface);
560 #endif
561                 break;
562         case RENDERPATH_D3D10:
563                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
564                 break;
565         case RENDERPATH_D3D11:
566                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
567                 break;
568         }
569 }
570
571 void gl_backend_init(void)
572 {
573         int i;
574
575         for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
576         {
577                 polygonelement3s[i * 3 + 0] = 0;
578                 polygonelement3s[i * 3 + 1] = i + 1;
579                 polygonelement3s[i * 3 + 2] = i + 2;
580         }
581         // elements for rendering a series of quads as triangles
582         for (i = 0;i < QUADELEMENTS_MAXQUADS;i++)
583         {
584                 quadelement3s[i * 6 + 0] = i * 4;
585                 quadelement3s[i * 6 + 1] = i * 4 + 1;
586                 quadelement3s[i * 6 + 2] = i * 4 + 2;
587                 quadelement3s[i * 6 + 3] = i * 4;
588                 quadelement3s[i * 6 + 4] = i * 4 + 2;
589                 quadelement3s[i * 6 + 5] = i * 4 + 3;
590         }
591
592         for (i = 0;i < (POLYGONELEMENTS_MAXPOINTS - 2)*3;i++)
593                 polygonelement3i[i] = polygonelement3s[i];
594         for (i = 0;i < QUADELEMENTS_MAXQUADS*6;i++)
595                 quadelement3i[i] = quadelement3s[i];
596
597         Cvar_RegisterVariable(&r_render);
598         Cvar_RegisterVariable(&r_renderview);
599         Cvar_RegisterVariable(&r_waterwarp);
600         Cvar_RegisterVariable(&gl_polyblend);
601         Cvar_RegisterVariable(&v_flipped);
602         Cvar_RegisterVariable(&gl_dither);
603         Cvar_RegisterVariable(&gl_vbo);
604         Cvar_RegisterVariable(&gl_vbo_dynamicvertex);
605         Cvar_RegisterVariable(&gl_vbo_dynamicindex);
606         Cvar_RegisterVariable(&gl_paranoid);
607         Cvar_RegisterVariable(&gl_printcheckerror);
608
609         Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
610         Cvar_RegisterVariable(&gl_mesh_testmanualfeeding);
611
612         Cmd_AddCommand("gl_vbostats", GL_VBOStats_f, "prints a list of all buffer objects (vertex data and triangle elements) and total video memory used by them");
613
614         R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap, gl_backend_devicelost, gl_backend_devicerestored);
615 }
616
617 void GL_SetMirrorState(qboolean state);
618
619 void R_Viewport_TransformToScreen(const r_viewport_t *v, const vec4_t in, vec4_t out)
620 {
621         vec4_t temp;
622         float iw;
623         Matrix4x4_Transform4 (&v->viewmatrix, in, temp);
624         Matrix4x4_Transform4 (&v->projectmatrix, temp, out);
625         iw = 1.0f / out[3];
626         out[0] = v->x + (out[0] * iw + 1.0f) * v->width * 0.5f;
627
628         // for an odd reason, inverting this is wrong for R_Shadow_ScissorForBBox (we then get badly scissored lights)
629         //out[1] = v->y + v->height - (out[1] * iw + 1.0f) * v->height * 0.5f;
630         out[1] = v->y + (out[1] * iw + 1.0f) * v->height * 0.5f;
631
632         out[2] = v->z + (out[2] * iw + 1.0f) * v->depth * 0.5f;
633 }
634
635 void GL_Finish(void)
636 {
637         switch(vid.renderpath)
638         {
639         case RENDERPATH_GL11:
640         case RENDERPATH_GL13:
641         case RENDERPATH_GL20:
642         case RENDERPATH_GLES1:
643         case RENDERPATH_GLES2:
644                 qglFinish();
645                 break;
646         case RENDERPATH_D3D9:
647                 //Con_DPrintf("FIXME D3D9 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
648                 break;
649         case RENDERPATH_D3D10:
650                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
651                 break;
652         case RENDERPATH_D3D11:
653                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
654                 break;
655         case RENDERPATH_SOFT:
656                 DPSOFTRAST_Finish();
657                 break;
658         }
659 }
660
661 static int bboxedges[12][2] =
662 {
663         // top
664         {0, 1}, // +X
665         {0, 2}, // +Y
666         {1, 3}, // Y, +X
667         {2, 3}, // X, +Y
668         // bottom
669         {4, 5}, // +X
670         {4, 6}, // +Y
671         {5, 7}, // Y, +X
672         {6, 7}, // X, +Y
673         // verticals
674         {0, 4}, // +Z
675         {1, 5}, // X, +Z
676         {2, 6}, // Y, +Z
677         {3, 7}, // XY, +Z
678 };
679
680 qboolean R_ScissorForBBox(const float *mins, const float *maxs, int *scissor)
681 {
682         int i, ix1, iy1, ix2, iy2;
683         float x1, y1, x2, y2;
684         vec4_t v, v2;
685         float vertex[20][3];
686         int j, k;
687         vec4_t plane4f;
688         int numvertices;
689         float corner[8][4];
690         float dist[8];
691         int sign[8];
692         float f;
693
694         scissor[0] = r_refdef.view.viewport.x;
695         scissor[1] = r_refdef.view.viewport.y;
696         scissor[2] = r_refdef.view.viewport.width;
697         scissor[3] = r_refdef.view.viewport.height;
698
699         // if view is inside the box, just say yes it's visible
700         if (BoxesOverlap(r_refdef.view.origin, r_refdef.view.origin, mins, maxs))
701                 return false;
702
703         // transform all corners that are infront of the nearclip plane
704         VectorNegate(r_refdef.view.frustum[4].normal, plane4f);
705         plane4f[3] = r_refdef.view.frustum[4].dist;
706         numvertices = 0;
707         for (i = 0;i < 8;i++)
708         {
709                 Vector4Set(corner[i], (i & 1) ? maxs[0] : mins[0], (i & 2) ? maxs[1] : mins[1], (i & 4) ? maxs[2] : mins[2], 1);
710                 dist[i] = DotProduct4(corner[i], plane4f);
711                 sign[i] = dist[i] > 0;
712                 if (!sign[i])
713                 {
714                         VectorCopy(corner[i], vertex[numvertices]);
715                         numvertices++;
716                 }
717         }
718         // if some points are behind the nearclip, add clipped edge points to make
719         // sure that the scissor boundary is complete
720         if (numvertices > 0 && numvertices < 8)
721         {
722                 // add clipped edge points
723                 for (i = 0;i < 12;i++)
724                 {
725                         j = bboxedges[i][0];
726                         k = bboxedges[i][1];
727                         if (sign[j] != sign[k])
728                         {
729                                 f = dist[j] / (dist[j] - dist[k]);
730                                 VectorLerp(corner[j], f, corner[k], vertex[numvertices]);
731                                 numvertices++;
732                         }
733                 }
734         }
735
736         // if we have no points to check, it is behind the view plane
737         if (!numvertices)
738                 return true;
739
740         // if we have some points to transform, check what screen area is covered
741         x1 = y1 = x2 = y2 = 0;
742         v[3] = 1.0f;
743         //Con_Printf("%i vertices to transform...\n", numvertices);
744         for (i = 0;i < numvertices;i++)
745         {
746                 VectorCopy(vertex[i], v);
747                 R_Viewport_TransformToScreen(&r_refdef.view.viewport, v, v2);
748                 //Con_Printf("%.3f %.3f %.3f %.3f transformed to %.3f %.3f %.3f %.3f\n", v[0], v[1], v[2], v[3], v2[0], v2[1], v2[2], v2[3]);
749                 if (i)
750                 {
751                         if (x1 > v2[0]) x1 = v2[0];
752                         if (x2 < v2[0]) x2 = v2[0];
753                         if (y1 > v2[1]) y1 = v2[1];
754                         if (y2 < v2[1]) y2 = v2[1];
755                 }
756                 else
757                 {
758                         x1 = x2 = v2[0];
759                         y1 = y2 = v2[1];
760                 }
761         }
762
763         // now convert the scissor rectangle to integer screen coordinates
764         ix1 = (int)(x1 - 1.0f);
765         //iy1 = vid.height - (int)(y2 - 1.0f);
766         //iy1 = r_refdef.view.viewport.width + 2 * r_refdef.view.viewport.x - (int)(y2 - 1.0f);
767         iy1 = (int)(y1 - 1.0f);
768         ix2 = (int)(x2 + 1.0f);
769         //iy2 = vid.height - (int)(y1 + 1.0f);
770         //iy2 = r_refdef.view.viewport.height + 2 * r_refdef.view.viewport.y - (int)(y1 + 1.0f);
771         iy2 = (int)(y2 + 1.0f);
772         //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
773
774         // clamp it to the screen
775         if (ix1 < r_refdef.view.viewport.x) ix1 = r_refdef.view.viewport.x;
776         if (iy1 < r_refdef.view.viewport.y) iy1 = r_refdef.view.viewport.y;
777         if (ix2 > r_refdef.view.viewport.x + r_refdef.view.viewport.width) ix2 = r_refdef.view.viewport.x + r_refdef.view.viewport.width;
778         if (iy2 > r_refdef.view.viewport.y + r_refdef.view.viewport.height) iy2 = r_refdef.view.viewport.y + r_refdef.view.viewport.height;
779
780         // if it is inside out, it's not visible
781         if (ix2 <= ix1 || iy2 <= iy1)
782                 return true;
783
784         // the light area is visible, set up the scissor rectangle
785         scissor[0] = ix1;
786         scissor[1] = iy1;
787         scissor[2] = ix2 - ix1;
788         scissor[3] = iy2 - iy1;
789
790         // D3D Y coordinate is top to bottom, OpenGL is bottom to top, fix the D3D one
791         switch(vid.renderpath)
792         {
793         case RENDERPATH_D3D9:
794         case RENDERPATH_D3D10:
795         case RENDERPATH_D3D11:
796                 scissor[1] = vid.height - scissor[1] - scissor[3];
797                 break;
798         case RENDERPATH_GL11:
799         case RENDERPATH_GL13:
800         case RENDERPATH_GL20:
801         case RENDERPATH_SOFT:
802         case RENDERPATH_GLES1:
803         case RENDERPATH_GLES2:
804                 break;
805         }
806
807         return false;
808 }
809
810
811 static void R_Viewport_ApplyNearClipPlaneFloatGL(const r_viewport_t *v, float *m, float normalx, float normaly, float normalz, float dist)
812 {
813         float q[4];
814         float d;
815         float clipPlane[4], v3[3], v4[3];
816         float normal[3];
817
818         // This is inspired by Oblique Depth Projection from http://www.terathon.com/code/oblique.php
819
820         VectorSet(normal, normalx, normaly, normalz);
821         Matrix4x4_Transform3x3(&v->viewmatrix, normal, clipPlane);
822         VectorScale(normal, -dist, v3);
823         Matrix4x4_Transform(&v->viewmatrix, v3, v4);
824         // FIXME: LordHavoc: I think this can be done more efficiently somehow but I can't remember the technique
825         clipPlane[3] = -DotProduct(v4, clipPlane);
826
827 #if 0
828 {
829         // testing code for comparing results
830         float clipPlane2[4];
831         VectorCopy4(clipPlane, clipPlane2);
832         R_EntityMatrix(&identitymatrix);
833         VectorSet(q, normal[0], normal[1], normal[2], -dist);
834         qglClipPlane(GL_CLIP_PLANE0, q);
835         qglGetClipPlane(GL_CLIP_PLANE0, q);
836         VectorCopy4(q, clipPlane);
837 }
838 #endif
839
840         // Calculate the clip-space corner point opposite the clipping plane
841         // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
842         // transform it into camera space by multiplying it
843         // by the inverse of the projection matrix
844         q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + m[8]) / m[0];
845         q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + m[9]) / m[5];
846         q[2] = -1.0f;
847         q[3] = (1.0f + m[10]) / m[14];
848
849         // Calculate the scaled plane vector
850         d = 2.0f / DotProduct4(clipPlane, q);
851
852         // Replace the third row of the projection matrix
853         m[2] = clipPlane[0] * d;
854         m[6] = clipPlane[1] * d;
855         m[10] = clipPlane[2] * d + 1.0f;
856         m[14] = clipPlane[3] * d;
857 }
858
859 void R_Viewport_InitOrtho(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, float x1, float y1, float x2, float y2, float nearclip, float farclip, const float *nearplane)
860 {
861         float left = x1, right = x2, bottom = y2, top = y1, zNear = nearclip, zFar = farclip;
862         float m[16];
863         memset(v, 0, sizeof(*v));
864         v->type = R_VIEWPORTTYPE_ORTHO;
865         v->cameramatrix = *cameramatrix;
866         v->x = x;
867         v->y = y;
868         v->z = 0;
869         v->width = width;
870         v->height = height;
871         v->depth = 1;
872         memset(m, 0, sizeof(m));
873         m[0]  = 2/(right - left);
874         m[5]  = 2/(top - bottom);
875         m[10] = -2/(zFar - zNear);
876         m[12] = - (right + left)/(right - left);
877         m[13] = - (top + bottom)/(top - bottom);
878         m[14] = - (zFar + zNear)/(zFar - zNear);
879         m[15] = 1;
880         switch(vid.renderpath)
881         {
882         case RENDERPATH_GL11:
883         case RENDERPATH_GL13:
884         case RENDERPATH_GL20:
885         case RENDERPATH_SOFT:
886         case RENDERPATH_GLES1:
887         case RENDERPATH_GLES2:
888                 break;
889         case RENDERPATH_D3D9:
890         case RENDERPATH_D3D10:
891         case RENDERPATH_D3D11:
892                 m[10] = -1/(zFar - zNear);
893                 m[14] = -zNear/(zFar-zNear);
894                 break;
895         }
896         v->screentodepth[0] = -farclip / (farclip - nearclip);
897         v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
898
899         Matrix4x4_Invert_Full(&v->viewmatrix, &v->cameramatrix);
900
901         if (nearplane)
902                 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
903
904         Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
905
906 #if 0
907         {
908                 vec4_t test1;
909                 vec4_t test2;
910                 Vector4Set(test1, (x1+x2)*0.5f, (y1+y2)*0.5f, 0.0f, 1.0f);
911                 R_Viewport_TransformToScreen(v, test1, test2);
912                 Con_Printf("%f %f %f -> %f %f %f\n", test1[0], test1[1], test1[2], test2[0], test2[1], test2[2]);
913         }
914 #endif
915 }
916
917 void R_Viewport_InitPerspective(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, float frustumx, float frustumy, float nearclip, float farclip, const float *nearplane)
918 {
919         matrix4x4_t tempmatrix, basematrix;
920         float m[16];
921         memset(v, 0, sizeof(*v));
922
923         v->type = R_VIEWPORTTYPE_PERSPECTIVE;
924         v->cameramatrix = *cameramatrix;
925         v->x = x;
926         v->y = y;
927         v->z = 0;
928         v->width = width;
929         v->height = height;
930         v->depth = 1;
931         memset(m, 0, sizeof(m));
932         m[0]  = 1.0 / frustumx;
933         m[5]  = 1.0 / frustumy;
934         m[10] = -(farclip + nearclip) / (farclip - nearclip);
935         m[11] = -1;
936         m[14] = -2 * nearclip * farclip / (farclip - nearclip);
937         v->screentodepth[0] = -farclip / (farclip - nearclip);
938         v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
939
940         Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
941         Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
942         Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
943         Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
944
945         if (nearplane)
946                 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
947
948         if(v_flipped.integer)
949         {
950                 m[0] = -m[0];
951                 m[4] = -m[4];
952                 m[8] = -m[8];
953                 m[12] = -m[12];
954         }
955
956         Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
957 }
958
959 void R_Viewport_InitPerspectiveInfinite(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, float frustumx, float frustumy, float nearclip, const float *nearplane)
960 {
961         matrix4x4_t tempmatrix, basematrix;
962         const float nudge = 1.0 - 1.0 / (1<<23);
963         float m[16];
964         memset(v, 0, sizeof(*v));
965
966         v->type = R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP;
967         v->cameramatrix = *cameramatrix;
968         v->x = x;
969         v->y = y;
970         v->z = 0;
971         v->width = width;
972         v->height = height;
973         v->depth = 1;
974         memset(m, 0, sizeof(m));
975         m[ 0] = 1.0 / frustumx;
976         m[ 5] = 1.0 / frustumy;
977         m[10] = -nudge;
978         m[11] = -1;
979         m[14] = -2 * nearclip * nudge;
980         v->screentodepth[0] = (m[10] + 1) * 0.5 - 1;
981         v->screentodepth[1] = m[14] * -0.5;
982
983         Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
984         Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
985         Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
986         Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
987
988         if (nearplane)
989                 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
990
991         if(v_flipped.integer)
992         {
993                 m[0] = -m[0];
994                 m[4] = -m[4];
995                 m[8] = -m[8];
996                 m[12] = -m[12];
997         }
998
999         Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
1000 }
1001
1002 float cubeviewmatrix[6][16] =
1003 {
1004     // standard cubemap projections
1005     { // +X
1006          0, 0,-1, 0,
1007          0,-1, 0, 0,
1008         -1, 0, 0, 0,
1009          0, 0, 0, 1,
1010     },
1011     { // -X
1012          0, 0, 1, 0,
1013          0,-1, 0, 0,
1014          1, 0, 0, 0,
1015          0, 0, 0, 1,
1016     },
1017     { // +Y
1018          1, 0, 0, 0,
1019          0, 0,-1, 0,
1020          0, 1, 0, 0,
1021          0, 0, 0, 1,
1022     },
1023     { // -Y
1024          1, 0, 0, 0,
1025          0, 0, 1, 0,
1026          0,-1, 0, 0,
1027          0, 0, 0, 1,
1028     },
1029     { // +Z
1030          1, 0, 0, 0,
1031          0,-1, 0, 0,
1032          0, 0,-1, 0,
1033          0, 0, 0, 1,
1034     },
1035     { // -Z
1036         -1, 0, 0, 0,
1037          0,-1, 0, 0,
1038          0, 0, 1, 0,
1039          0, 0, 0, 1,
1040     },
1041 };
1042 float rectviewmatrix[6][16] =
1043 {
1044     // sign-preserving cubemap projections
1045     { // +X
1046          0, 0,-1, 0,
1047          0, 1, 0, 0,
1048          1, 0, 0, 0,
1049          0, 0, 0, 1,
1050     },
1051     { // -X
1052          0, 0, 1, 0,
1053          0, 1, 0, 0,
1054          1, 0, 0, 0,
1055          0, 0, 0, 1,
1056     },
1057     { // +Y
1058          1, 0, 0, 0,
1059          0, 0,-1, 0,
1060          0, 1, 0, 0,
1061          0, 0, 0, 1,
1062     },
1063     { // -Y
1064          1, 0, 0, 0,
1065          0, 0, 1, 0,
1066          0, 1, 0, 0,
1067          0, 0, 0, 1,
1068     },
1069     { // +Z
1070          1, 0, 0, 0,
1071          0, 1, 0, 0,
1072          0, 0,-1, 0,
1073          0, 0, 0, 1,
1074     },
1075     { // -Z
1076          1, 0, 0, 0,
1077          0, 1, 0, 0,
1078          0, 0, 1, 0,
1079          0, 0, 0, 1,
1080     },
1081 };
1082
1083 void R_Viewport_InitCubeSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, float nearclip, float farclip, const float *nearplane)
1084 {
1085         matrix4x4_t tempmatrix, basematrix;
1086         float m[16];
1087         memset(v, 0, sizeof(*v));
1088         v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
1089         v->cameramatrix = *cameramatrix;
1090         v->width = size;
1091         v->height = size;
1092         v->depth = 1;
1093         memset(m, 0, sizeof(m));
1094         m[0] = m[5] = 1.0f;
1095         m[10] = -(farclip + nearclip) / (farclip - nearclip);
1096         m[11] = -1;
1097         m[14] = -2 * nearclip * farclip / (farclip - nearclip);
1098
1099         Matrix4x4_FromArrayFloatGL(&basematrix, cubeviewmatrix[side]);
1100         Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
1101         Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
1102
1103         if (nearplane)
1104                 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
1105
1106         Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
1107 }
1108
1109 void R_Viewport_InitRectSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, int border, float nearclip, float farclip, const float *nearplane)
1110 {
1111         matrix4x4_t tempmatrix, basematrix;
1112         float m[16];
1113         memset(v, 0, sizeof(*v));
1114         v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
1115         v->cameramatrix = *cameramatrix;
1116         v->x = (side & 1) * size;
1117         v->y = (side >> 1) * size;
1118         v->width = size;
1119         v->height = size;
1120         v->depth = 1;
1121         memset(m, 0, sizeof(m));
1122         m[0] = m[5] = 1.0f * ((float)size - border) / size;
1123         m[10] = -(farclip + nearclip) / (farclip - nearclip);
1124         m[11] = -1;
1125         m[14] = -2 * nearclip * farclip / (farclip - nearclip);
1126
1127         Matrix4x4_FromArrayFloatGL(&basematrix, rectviewmatrix[side]);
1128         Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
1129         Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
1130
1131         switch(vid.renderpath)
1132         {
1133         case RENDERPATH_GL20:
1134         case RENDERPATH_GL13:
1135         case RENDERPATH_GL11:
1136         case RENDERPATH_SOFT:
1137         case RENDERPATH_GLES1:
1138         case RENDERPATH_GLES2:
1139                 break;
1140         case RENDERPATH_D3D9:
1141                 m[5] *= -1;
1142                 break;
1143         case RENDERPATH_D3D10:
1144                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1145                 break;
1146         case RENDERPATH_D3D11:
1147                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1148                 break;
1149         }
1150
1151         if (nearplane)
1152                 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
1153
1154         Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
1155 }
1156
1157 void R_SetViewport(const r_viewport_t *v)
1158 {
1159         gl_viewport = *v;
1160
1161         // FIXME: v_flipped_state is evil, this probably breaks somewhere
1162         GL_SetMirrorState(v_flipped.integer && (v->type == R_VIEWPORTTYPE_PERSPECTIVE || v->type == R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP));
1163
1164         // copy over the matrices to our state
1165         gl_viewmatrix = v->viewmatrix;
1166         gl_projectionmatrix = v->projectmatrix;
1167
1168         switch(vid.renderpath)
1169         {
1170         case RENDERPATH_GL13:
1171         case RENDERPATH_GL11:
1172         case RENDERPATH_GLES1:
1173 #ifndef USE_GLES2
1174                 {
1175                         float m[16];
1176                         CHECKGLERROR
1177                         qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
1178                         // Load the projection matrix into OpenGL
1179                         qglMatrixMode(GL_PROJECTION);CHECKGLERROR
1180                         Matrix4x4_ToArrayFloatGL(&gl_projectionmatrix, m);
1181                         qglLoadMatrixf(m);CHECKGLERROR
1182                         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1183                 }
1184 #endif
1185                 break;
1186         case RENDERPATH_D3D9:
1187 #ifdef SUPPORTD3D
1188                 {
1189                         D3DVIEWPORT9 d3dviewport;
1190                         d3dviewport.X = gl_viewport.x;
1191                         d3dviewport.Y = gl_viewport.y;
1192                         d3dviewport.Width = gl_viewport.width;
1193                         d3dviewport.Height = gl_viewport.height;
1194                         d3dviewport.MinZ = gl_state.depthrange[0];
1195                         d3dviewport.MaxZ = gl_state.depthrange[1];
1196                         IDirect3DDevice9_SetViewport(vid_d3d9dev, &d3dviewport);
1197                 }
1198 #endif
1199                 break;
1200         case RENDERPATH_D3D10:
1201                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1202                 break;
1203         case RENDERPATH_D3D11:
1204                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1205                 break;
1206         case RENDERPATH_SOFT:
1207                 DPSOFTRAST_Viewport(v->x, v->y, v->width, v->height);
1208                 break;
1209         case RENDERPATH_GL20:
1210         case RENDERPATH_GLES2:
1211                 CHECKGLERROR
1212                 qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
1213                 break;
1214         }
1215
1216         // force an update of the derived matrices
1217         gl_modelmatrixchanged = true;
1218         R_EntityMatrix(&gl_modelmatrix);
1219 }
1220
1221 void R_GetViewport(r_viewport_t *v)
1222 {
1223         *v = gl_viewport;
1224 }
1225
1226 static void GL_BindVBO(int bufferobject)
1227 {
1228         if (gl_state.vertexbufferobject != bufferobject)
1229         {
1230                 gl_state.vertexbufferobject = bufferobject;
1231                 CHECKGLERROR
1232                 qglBindBufferARB(GL_ARRAY_BUFFER, bufferobject);CHECKGLERROR
1233         }
1234 }
1235
1236 static void GL_BindEBO(int bufferobject)
1237 {
1238         if (gl_state.elementbufferobject != bufferobject)
1239         {
1240                 gl_state.elementbufferobject = bufferobject;
1241                 CHECKGLERROR
1242                 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, bufferobject);CHECKGLERROR
1243         }
1244 }
1245
1246 static void GL_BindUBO(int bufferobject)
1247 {
1248         if (gl_state.uniformbufferobject != bufferobject)
1249         {
1250                 gl_state.uniformbufferobject = bufferobject;
1251 #ifdef GL_UNIFORM_BUFFER
1252                 CHECKGLERROR
1253                 qglBindBufferARB(GL_UNIFORM_BUFFER, bufferobject);CHECKGLERROR
1254 #endif
1255         }
1256 }
1257
1258 static const GLuint drawbuffers[4] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3};
1259 int R_Mesh_CreateFramebufferObject(rtexture_t *depthtexture, rtexture_t *colortexture, rtexture_t *colortexture2, rtexture_t *colortexture3, rtexture_t *colortexture4)
1260 {
1261         switch(vid.renderpath)
1262         {
1263         case RENDERPATH_GL11:
1264         case RENDERPATH_GL13:
1265         case RENDERPATH_GL20:
1266         case RENDERPATH_GLES1:
1267         case RENDERPATH_GLES2:
1268                 if (vid.support.arb_framebuffer_object)
1269                 {
1270                         int temp;
1271                         GLuint status;
1272                         qglGenFramebuffers(1, (GLuint*)&temp);CHECKGLERROR
1273                         R_Mesh_SetRenderTargets(temp, NULL, NULL, NULL, NULL, NULL);
1274                         // GL_ARB_framebuffer_object (GL3-class hardware) - depth stencil attachment
1275 #ifdef USE_GLES2
1276                         // FIXME: separate stencil attachment on GLES
1277                         if (depthtexture  && depthtexture->texnum ) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT  , depthtexture->gltexturetypeenum , depthtexture->texnum , 0);CHECKGLERROR
1278                         if (depthtexture  && depthtexture->renderbuffernum ) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT  , GL_RENDERBUFFER, depthtexture->renderbuffernum );CHECKGLERROR
1279 #else
1280                         if (depthtexture  && depthtexture->texnum )
1281                         {
1282                                 qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT  , depthtexture->gltexturetypeenum , depthtexture->texnum , 0);CHECKGLERROR
1283                                 if (depthtexture->glisdepthstencil) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT  , depthtexture->gltexturetypeenum , depthtexture->texnum , 0);CHECKGLERROR
1284                         }
1285                         if (depthtexture  && depthtexture->renderbuffernum )
1286                         {
1287                                 qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT  , GL_RENDERBUFFER, depthtexture->renderbuffernum );CHECKGLERROR
1288                                 if (depthtexture->glisdepthstencil) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT  , GL_RENDERBUFFER, depthtexture->renderbuffernum );CHECKGLERROR
1289                         }
1290 #endif
1291                         if (colortexture  && colortexture->texnum ) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , colortexture->gltexturetypeenum , colortexture->texnum , 0);CHECKGLERROR
1292                         if (colortexture2 && colortexture2->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 , colortexture2->gltexturetypeenum, colortexture2->texnum, 0);CHECKGLERROR
1293                         if (colortexture3 && colortexture3->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2 , colortexture3->gltexturetypeenum, colortexture3->texnum, 0);CHECKGLERROR
1294                         if (colortexture4 && colortexture4->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3 , colortexture4->gltexturetypeenum, colortexture4->texnum, 0);CHECKGLERROR
1295                         if (colortexture  && colortexture->renderbuffernum ) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , GL_RENDERBUFFER, colortexture->renderbuffernum );CHECKGLERROR
1296                         if (colortexture2 && colortexture2->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 , GL_RENDERBUFFER, colortexture2->renderbuffernum);CHECKGLERROR
1297                         if (colortexture3 && colortexture3->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2 , GL_RENDERBUFFER, colortexture3->renderbuffernum);CHECKGLERROR
1298                         if (colortexture4 && colortexture4->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3 , GL_RENDERBUFFER, colortexture4->renderbuffernum);CHECKGLERROR
1299
1300 #ifndef USE_GLES2
1301                         if (colortexture4 && qglDrawBuffersARB)
1302                         {
1303                                 qglDrawBuffersARB(4, drawbuffers);CHECKGLERROR
1304                                 qglReadBuffer(GL_NONE);CHECKGLERROR
1305                         }
1306                         else if (colortexture3 && qglDrawBuffersARB)
1307                         {
1308                                 qglDrawBuffersARB(3, drawbuffers);CHECKGLERROR
1309                                 qglReadBuffer(GL_NONE);CHECKGLERROR
1310                         }
1311                         else if (colortexture2 && qglDrawBuffersARB)
1312                         {
1313                                 qglDrawBuffersARB(2, drawbuffers);CHECKGLERROR
1314                                 qglReadBuffer(GL_NONE);CHECKGLERROR
1315                         }
1316                         else if (colortexture && qglDrawBuffer)
1317                         {
1318                                 qglDrawBuffer(GL_COLOR_ATTACHMENT0);CHECKGLERROR
1319                                 qglReadBuffer(GL_COLOR_ATTACHMENT0);CHECKGLERROR
1320                         }
1321                         else if (qglDrawBuffer)
1322                         {
1323                                 qglDrawBuffer(GL_NONE);CHECKGLERROR
1324                                 qglReadBuffer(GL_NONE);CHECKGLERROR
1325                         }
1326 #endif
1327                         status = qglCheckFramebufferStatus(GL_FRAMEBUFFER);CHECKGLERROR
1328                         if (status != GL_FRAMEBUFFER_COMPLETE)
1329                         {
1330                                 Con_Printf("R_Mesh_CreateFramebufferObject: glCheckFramebufferStatus returned %i\n", status);
1331                                 gl_state.framebufferobject = 0; // GL unbinds it for us
1332                                 qglDeleteFramebuffers(1, (GLuint*)&temp);
1333                                 temp = 0;
1334                         }
1335                         return temp;
1336                 }
1337                 else if (vid.support.ext_framebuffer_object)
1338                 {
1339                         int temp;
1340                         GLuint status;
1341                         qglGenFramebuffers(1, (GLuint*)&temp);CHECKGLERROR
1342                         R_Mesh_SetRenderTargets(temp, NULL, NULL, NULL, NULL, NULL);
1343                         // GL_EXT_framebuffer_object (GL2-class hardware) - no depth stencil attachment, let it break stencil
1344                         if (depthtexture  && depthtexture->texnum ) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT  , depthtexture->gltexturetypeenum , depthtexture->texnum , 0);CHECKGLERROR
1345                         if (depthtexture  && depthtexture->renderbuffernum ) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT  , GL_RENDERBUFFER, depthtexture->renderbuffernum );CHECKGLERROR
1346                         if (colortexture  && colortexture->texnum ) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , colortexture->gltexturetypeenum , colortexture->texnum , 0);CHECKGLERROR
1347                         if (colortexture2 && colortexture2->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 , colortexture2->gltexturetypeenum, colortexture2->texnum, 0);CHECKGLERROR
1348                         if (colortexture3 && colortexture3->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2 , colortexture3->gltexturetypeenum, colortexture3->texnum, 0);CHECKGLERROR
1349                         if (colortexture4 && colortexture4->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3 , colortexture4->gltexturetypeenum, colortexture4->texnum, 0);CHECKGLERROR
1350                         if (colortexture  && colortexture->renderbuffernum ) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , GL_RENDERBUFFER, colortexture->renderbuffernum );CHECKGLERROR
1351                         if (colortexture2 && colortexture2->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 , GL_RENDERBUFFER, colortexture2->renderbuffernum);CHECKGLERROR
1352                         if (colortexture3 && colortexture3->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2 , GL_RENDERBUFFER, colortexture3->renderbuffernum);CHECKGLERROR
1353                         if (colortexture4 && colortexture4->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3 , GL_RENDERBUFFER, colortexture4->renderbuffernum);CHECKGLERROR
1354
1355 #ifndef USE_GLES2
1356                         if (colortexture4 && qglDrawBuffersARB)
1357                         {
1358                                 qglDrawBuffersARB(4, drawbuffers);CHECKGLERROR
1359                                 qglReadBuffer(GL_NONE);CHECKGLERROR
1360                         }
1361                         else if (colortexture3 && qglDrawBuffersARB)
1362                         {
1363                                 qglDrawBuffersARB(3, drawbuffers);CHECKGLERROR
1364                                 qglReadBuffer(GL_NONE);CHECKGLERROR
1365                         }
1366                         else if (colortexture2 && qglDrawBuffersARB)
1367                         {
1368                                 qglDrawBuffersARB(2, drawbuffers);CHECKGLERROR
1369                                 qglReadBuffer(GL_NONE);CHECKGLERROR
1370                         }
1371                         else if (colortexture && qglDrawBuffer)
1372                         {
1373                                 qglDrawBuffer(GL_COLOR_ATTACHMENT0);CHECKGLERROR
1374                                 qglReadBuffer(GL_COLOR_ATTACHMENT0);CHECKGLERROR
1375                         }
1376                         else if (qglDrawBuffer)
1377                         {
1378                                 qglDrawBuffer(GL_NONE);CHECKGLERROR
1379                                 qglReadBuffer(GL_NONE);CHECKGLERROR
1380                         }
1381 #endif
1382                         status = qglCheckFramebufferStatus(GL_FRAMEBUFFER);CHECKGLERROR
1383                         if (status != GL_FRAMEBUFFER_COMPLETE)
1384                         {
1385                                 Con_Printf("R_Mesh_CreateFramebufferObject: glCheckFramebufferStatus returned %i\n", status);
1386                                 gl_state.framebufferobject = 0; // GL unbinds it for us
1387                                 qglDeleteFramebuffers(1, (GLuint*)&temp);
1388                                 temp = 0;
1389                         }
1390                         return temp;
1391                 }
1392                 return 0;
1393         case RENDERPATH_D3D9:
1394         case RENDERPATH_D3D10:
1395         case RENDERPATH_D3D11:
1396                 return 1;
1397         case RENDERPATH_SOFT:
1398                 return 1;
1399         }
1400         return 0;
1401 }
1402
1403 void R_Mesh_DestroyFramebufferObject(int fbo)
1404 {
1405         switch(vid.renderpath)
1406         {
1407         case RENDERPATH_GL11:
1408         case RENDERPATH_GL13:
1409         case RENDERPATH_GL20:
1410         case RENDERPATH_GLES1:
1411         case RENDERPATH_GLES2:
1412                 if (fbo)
1413                 {
1414                         // GL clears the binding if we delete something bound
1415                         if (gl_state.framebufferobject == fbo)
1416                                 gl_state.framebufferobject = 0;
1417                         qglDeleteFramebuffers(1, (GLuint*)&fbo);
1418                 }
1419                 break;
1420         case RENDERPATH_D3D9:
1421         case RENDERPATH_D3D10:
1422         case RENDERPATH_D3D11:
1423                 break;
1424         case RENDERPATH_SOFT:
1425                 break;
1426         }
1427 }
1428
1429 #ifdef SUPPORTD3D
1430 void R_Mesh_SetRenderTargetsD3D9(IDirect3DSurface9 *depthsurface, IDirect3DSurface9 *colorsurface0, IDirect3DSurface9 *colorsurface1, IDirect3DSurface9 *colorsurface2, IDirect3DSurface9 *colorsurface3)
1431 {
1432         gl_state.framebufferobject = depthsurface != gl_state.d3drt_backbufferdepthsurface || colorsurface0 != gl_state.d3drt_backbuffercolorsurface;
1433         if (gl_state.d3drt_depthsurface != depthsurface)
1434         {
1435                 gl_state.d3drt_depthsurface = depthsurface;
1436                 IDirect3DDevice9_SetDepthStencilSurface(vid_d3d9dev, gl_state.d3drt_depthsurface);
1437         }
1438         if (gl_state.d3drt_colorsurfaces[0] != colorsurface0)
1439         {
1440                 gl_state.d3drt_colorsurfaces[0] = colorsurface0;
1441                 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 0, gl_state.d3drt_colorsurfaces[0]);
1442         }
1443         if (gl_state.d3drt_colorsurfaces[1] != colorsurface1)
1444         {
1445                 gl_state.d3drt_colorsurfaces[1] = colorsurface1;
1446                 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 1, gl_state.d3drt_colorsurfaces[1]);
1447         }
1448         if (gl_state.d3drt_colorsurfaces[2] != colorsurface2)
1449         {
1450                 gl_state.d3drt_colorsurfaces[2] = colorsurface2;
1451                 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 2, gl_state.d3drt_colorsurfaces[2]);
1452         }
1453         if (gl_state.d3drt_colorsurfaces[3] != colorsurface3)
1454         {
1455                 gl_state.d3drt_colorsurfaces[3] = colorsurface3;
1456                 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 3, gl_state.d3drt_colorsurfaces[3]);
1457         }
1458 }
1459 #endif
1460
1461 void R_Mesh_SetRenderTargets(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture, rtexture_t *colortexture2, rtexture_t *colortexture3, rtexture_t *colortexture4)
1462 {
1463         unsigned int i;
1464         unsigned int j;
1465         rtexture_t *textures[5];
1466         Vector4Set(textures, colortexture, colortexture2, colortexture3, colortexture4);
1467         textures[4] = depthtexture;
1468         // unbind any matching textures immediately, otherwise D3D will complain about a bound texture being used as a render target
1469         for (j = 0;j < 5;j++)
1470                 if (textures[j])
1471                         for (i = 0;i < vid.teximageunits;i++)
1472                                 if (gl_state.units[i].texture == textures[j])
1473                                         R_Mesh_TexBind(i, NULL);
1474         // set up framebuffer object or render targets for the active rendering API
1475         switch(vid.renderpath)
1476         {
1477         case RENDERPATH_GL11:
1478         case RENDERPATH_GL13:
1479         case RENDERPATH_GL20:
1480         case RENDERPATH_GLES1:
1481         case RENDERPATH_GLES2:
1482                 if (gl_state.framebufferobject != fbo)
1483                 {
1484                         gl_state.framebufferobject = fbo;
1485                         qglBindFramebuffer(GL_FRAMEBUFFER, gl_state.framebufferobject ? gl_state.framebufferobject : gl_state.defaultframebufferobject);
1486                 }
1487                 break;
1488         case RENDERPATH_D3D9:
1489 #ifdef SUPPORTD3D
1490                 // set up the new render targets, a NULL depthtexture intentionally binds nothing
1491                 // TODO: optimize: keep surface pointer around in rtexture_t until texture is freed or lost
1492                 if (fbo)
1493                 {
1494                         IDirect3DSurface9 *surfaces[5];
1495                         for (i = 0;i < 5;i++)
1496                         {
1497                                 surfaces[i] = NULL;
1498                                 if (textures[i])
1499                                 {
1500                                         if (textures[i]->d3dsurface)
1501                                                 surfaces[i] = (IDirect3DSurface9 *)textures[i]->d3dsurface;
1502                                         else
1503                                                 IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9 *)textures[i]->d3dtexture, 0, &surfaces[i]);
1504                                 }
1505                         }
1506                         // set the render targets for real
1507                         R_Mesh_SetRenderTargetsD3D9(surfaces[4], surfaces[0], surfaces[1], surfaces[2], surfaces[3]);
1508                         // release the texture surface levels (they won't be lost while bound...)
1509                         for (i = 0;i < 5;i++)
1510                                 if (textures[i] && !textures[i]->d3dsurface)
1511                                         IDirect3DSurface9_Release(surfaces[i]);
1512                 }
1513                 else
1514                         R_Mesh_SetRenderTargetsD3D9(gl_state.d3drt_backbufferdepthsurface, gl_state.d3drt_backbuffercolorsurface, NULL, NULL, NULL);
1515 #endif
1516                 break;
1517         case RENDERPATH_D3D10:
1518                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1519                 break;
1520         case RENDERPATH_D3D11:
1521                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1522                 break;
1523         case RENDERPATH_SOFT:
1524                 if (fbo)
1525                 {
1526                         int width, height;
1527                         unsigned int *pointers[5];
1528                         memset(pointers, 0, sizeof(pointers));
1529                         for (i = 0;i < 5;i++)
1530                                 pointers[i] = textures[i] ? (unsigned int *)DPSOFTRAST_Texture_GetPixelPointer(textures[i]->texnum, 0) : NULL;
1531                         width = DPSOFTRAST_Texture_GetWidth(textures[0] ? textures[0]->texnum : textures[4]->texnum, 0);
1532                         height = DPSOFTRAST_Texture_GetHeight(textures[0] ? textures[0]->texnum : textures[4]->texnum, 0);
1533                         DPSOFTRAST_SetRenderTargets(width, height, pointers[4], pointers[0], pointers[1], pointers[2], pointers[3]);
1534                 }
1535                 else
1536                         DPSOFTRAST_SetRenderTargets(vid.width, vid.height, vid.softdepthpixels, vid.softpixels, NULL, NULL, NULL);
1537                 break;
1538         }
1539 }
1540
1541 #ifdef SUPPORTD3D
1542 static int d3dcmpforglfunc(int f)
1543 {
1544         switch(f)
1545         {
1546         case GL_NEVER: return D3DCMP_NEVER;
1547         case GL_LESS: return D3DCMP_LESS;
1548         case GL_EQUAL: return D3DCMP_EQUAL;
1549         case GL_LEQUAL: return D3DCMP_LESSEQUAL;
1550         case GL_GREATER: return D3DCMP_GREATER;
1551         case GL_NOTEQUAL: return D3DCMP_NOTEQUAL;
1552         case GL_GEQUAL: return D3DCMP_GREATEREQUAL;
1553         case GL_ALWAYS: return D3DCMP_ALWAYS;
1554         default: Con_DPrintf("Unknown GL_DepthFunc\n");return D3DCMP_ALWAYS;
1555         }
1556 }
1557
1558 static int d3dstencilopforglfunc(int f)
1559 {
1560         switch(f)
1561         {
1562         case GL_KEEP: return D3DSTENCILOP_KEEP;
1563         case GL_INCR: return D3DSTENCILOP_INCR; // note: GL_INCR is clamped, D3DSTENCILOP_INCR wraps
1564         case GL_DECR: return D3DSTENCILOP_DECR; // note: GL_DECR is clamped, D3DSTENCILOP_DECR wraps
1565         default: Con_DPrintf("Unknown GL_StencilFunc\n");return D3DSTENCILOP_KEEP;
1566         }
1567 }
1568 #endif
1569
1570 static void GL_Backend_ResetState(void)
1571 {
1572         unsigned int i;
1573         gl_state.active = true;
1574         gl_state.depthtest = true;
1575         gl_state.alphatest = false;
1576         gl_state.alphafunc = GL_GEQUAL;
1577         gl_state.alphafuncvalue = 0.5f;
1578         gl_state.alphatocoverage = false;
1579         gl_state.blendfunc1 = GL_ONE;
1580         gl_state.blendfunc2 = GL_ZERO;
1581         gl_state.blend = false;
1582         gl_state.depthmask = GL_TRUE;
1583         gl_state.colormask = 15;
1584         gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
1585         gl_state.lockrange_first = 0;
1586         gl_state.lockrange_count = 0;
1587         gl_state.cullface = GL_FRONT;
1588         gl_state.cullfaceenable = false;
1589         gl_state.polygonoffset[0] = 0;
1590         gl_state.polygonoffset[1] = 0;
1591         gl_state.framebufferobject = 0;
1592         gl_state.depthfunc = GL_LEQUAL;
1593
1594         switch(vid.renderpath)
1595         {
1596         case RENDERPATH_D3D9:
1597 #ifdef SUPPORTD3D
1598                 {
1599                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_COLORWRITEENABLE, gl_state.colormask);
1600                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_NONE);
1601                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZFUNC, d3dcmpforglfunc(gl_state.depthfunc));
1602                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZENABLE, gl_state.depthtest);
1603                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZWRITEENABLE, gl_state.depthmask);
1604                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SLOPESCALEDEPTHBIAS, gl_state.polygonoffset[0]);
1605                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DEPTHBIAS, gl_state.polygonoffset[1] * (1.0f / 16777216.0f));
1606                 }
1607 #endif
1608                 break;
1609         case RENDERPATH_D3D10:
1610                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1611                 break;
1612         case RENDERPATH_D3D11:
1613                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1614                 break;
1615         case RENDERPATH_GL11:
1616         case RENDERPATH_GL13:
1617         case RENDERPATH_GLES1:
1618 #ifndef USE_GLES2
1619                 CHECKGLERROR
1620
1621                 qglColorMask(1, 1, 1, 1);CHECKGLERROR
1622                 qglAlphaFunc(gl_state.alphafunc, gl_state.alphafuncvalue);CHECKGLERROR
1623                 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
1624                 if (qglBlendFuncSeparate)
1625                 {
1626                         qglBlendFuncSeparate(gl_state.blendfunc1, gl_state.blendfunc2, GL_ZERO, GL_ONE);CHECKGLERROR // ELUAN: Adreno 225 (and others) compositing workaround
1627                 }
1628                 else
1629                 {
1630                         qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1631                 }
1632                 qglDisable(GL_BLEND);CHECKGLERROR
1633                 qglCullFace(gl_state.cullface);CHECKGLERROR
1634                 qglDisable(GL_CULL_FACE);CHECKGLERROR
1635                 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1636                 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1637                 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1638                 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1639
1640                 if (vid.support.arb_vertex_buffer_object)
1641                 {
1642                         qglBindBufferARB(GL_ARRAY_BUFFER, 0);
1643                         qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, 0);
1644                 }
1645
1646                 if (vid.support.ext_framebuffer_object)
1647                 {
1648                         //qglBindRenderbuffer(GL_RENDERBUFFER, 0);
1649                         qglBindFramebuffer(GL_FRAMEBUFFER, 0);
1650                 }
1651
1652                 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
1653                 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1654
1655                 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
1656                 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1657                 qglColor4f(1, 1, 1, 1);CHECKGLERROR
1658
1659                 if (vid.support.ext_framebuffer_object)
1660                         qglBindFramebuffer(GL_FRAMEBUFFER, gl_state.framebufferobject);
1661
1662                 gl_state.unit = MAX_TEXTUREUNITS;
1663                 gl_state.clientunit = MAX_TEXTUREUNITS;
1664                 for (i = 0;i < vid.texunits;i++)
1665                 {
1666                         GL_ActiveTexture(i);
1667                         GL_ClientActiveTexture(i);
1668                         qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1669                         qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1670                         if (vid.support.ext_texture_3d)
1671                         {
1672                                 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1673                                 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1674                         }
1675                         if (vid.support.arb_texture_cube_map)
1676                         {
1677                                 qglDisable(GL_TEXTURE_CUBE_MAP);CHECKGLERROR
1678                                 qglBindTexture(GL_TEXTURE_CUBE_MAP, 0);CHECKGLERROR
1679                         }
1680                         GL_BindVBO(0);
1681                         qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
1682                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1683                         qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1684                         qglLoadIdentity();CHECKGLERROR
1685                         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1686                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
1687                 }
1688                 CHECKGLERROR
1689 #endif
1690                 break;
1691         case RENDERPATH_SOFT:
1692                 DPSOFTRAST_ColorMask(1,1,1,1);
1693                 DPSOFTRAST_BlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);
1694                 DPSOFTRAST_CullFace(gl_state.cullface);
1695                 DPSOFTRAST_DepthFunc(gl_state.depthfunc);
1696                 DPSOFTRAST_DepthMask(gl_state.depthmask);
1697                 DPSOFTRAST_PolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1698                 DPSOFTRAST_SetRenderTargets(vid.width, vid.height, vid.softdepthpixels, vid.softpixels, NULL, NULL, NULL);
1699                 DPSOFTRAST_Viewport(0, 0, vid.width, vid.height);
1700                 break;
1701         case RENDERPATH_GL20:
1702         case RENDERPATH_GLES2:
1703                 CHECKGLERROR
1704                 qglColorMask(1, 1, 1, 1);CHECKGLERROR
1705                 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1706                 qglDisable(GL_BLEND);CHECKGLERROR
1707                 qglCullFace(gl_state.cullface);CHECKGLERROR
1708                 qglDisable(GL_CULL_FACE);CHECKGLERROR
1709                 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1710                 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1711                 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1712                 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1713                 if (vid.support.arb_vertex_buffer_object)
1714                 {
1715                         qglBindBufferARB(GL_ARRAY_BUFFER, 0);
1716                         qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, 0);
1717                 }
1718                 if (vid.support.ext_framebuffer_object)
1719                         qglBindFramebuffer(GL_FRAMEBUFFER, gl_state.defaultframebufferobject);
1720                 qglEnableVertexAttribArray(GLSLATTRIB_POSITION);
1721                 qglVertexAttribPointer(GLSLATTRIB_POSITION, 3, GL_FLOAT, false, sizeof(float[3]), NULL);CHECKGLERROR
1722                 qglDisableVertexAttribArray(GLSLATTRIB_COLOR);
1723                 qglVertexAttribPointer(GLSLATTRIB_COLOR, 4, GL_FLOAT, false, sizeof(float[4]), NULL);CHECKGLERROR
1724                 qglVertexAttrib4f(GLSLATTRIB_COLOR, 1, 1, 1, 1);
1725                 gl_state.unit = MAX_TEXTUREUNITS;
1726                 gl_state.clientunit = MAX_TEXTUREUNITS;
1727                 for (i = 0;i < vid.teximageunits;i++)
1728                 {
1729                         GL_ActiveTexture(i);
1730                         qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1731                         if (vid.support.ext_texture_3d)
1732                         {
1733                                 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1734                         }
1735                         if (vid.support.arb_texture_cube_map)
1736                         {
1737                                 qglBindTexture(GL_TEXTURE_CUBE_MAP, 0);CHECKGLERROR
1738                         }
1739                 }
1740                 for (i = 0;i < vid.texarrayunits;i++)
1741                 {
1742                         GL_BindVBO(0);
1743                         qglVertexAttribPointer(i+GLSLATTRIB_TEXCOORD0, 2, GL_FLOAT, false, sizeof(float[2]), NULL);CHECKGLERROR
1744                         qglDisableVertexAttribArray(i+GLSLATTRIB_TEXCOORD0);CHECKGLERROR
1745                 }
1746                 CHECKGLERROR
1747                 break;
1748         }
1749 }
1750
1751 void GL_ActiveTexture(unsigned int num)
1752 {
1753         if (gl_state.unit != num)
1754         {
1755                 gl_state.unit = num;
1756                 switch(vid.renderpath)
1757                 {
1758                 case RENDERPATH_GL11:
1759                 case RENDERPATH_GL13:
1760                 case RENDERPATH_GL20:
1761                 case RENDERPATH_GLES1:
1762                 case RENDERPATH_GLES2:
1763                         if (qglActiveTexture)
1764                         {
1765                                 CHECKGLERROR
1766                                 qglActiveTexture(GL_TEXTURE0 + gl_state.unit);
1767                                 CHECKGLERROR
1768                         }
1769                         break;
1770                 case RENDERPATH_D3D9:
1771                 case RENDERPATH_D3D10:
1772                 case RENDERPATH_D3D11:
1773                         break;
1774                 case RENDERPATH_SOFT:
1775                         break;
1776                 }
1777         }
1778 }
1779
1780 void GL_ClientActiveTexture(unsigned int num)
1781 {
1782         if (gl_state.clientunit != num)
1783         {
1784                 gl_state.clientunit = num;
1785                 switch(vid.renderpath)
1786                 {
1787                 case RENDERPATH_GL11:
1788                 case RENDERPATH_GL13:
1789                 case RENDERPATH_GLES1:
1790 #ifndef USE_GLES2
1791                         if (qglActiveTexture)
1792                         {
1793                                 CHECKGLERROR
1794                                 qglClientActiveTexture(GL_TEXTURE0 + gl_state.clientunit);
1795                                 CHECKGLERROR
1796                         }
1797 #endif
1798                         break;
1799                 case RENDERPATH_D3D9:
1800                 case RENDERPATH_D3D10:
1801                 case RENDERPATH_D3D11:
1802                         break;
1803                 case RENDERPATH_SOFT:
1804                         break;
1805                 case RENDERPATH_GL20:
1806                 case RENDERPATH_GLES2:
1807                         break;
1808                 }
1809         }
1810 }
1811
1812 void GL_BlendFunc(int blendfunc1, int blendfunc2)
1813 {
1814         if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
1815         {
1816                 qboolean blendenable;
1817                 gl_state.blendfunc1 = blendfunc1;
1818                 gl_state.blendfunc2 = blendfunc2;
1819                 blendenable = (gl_state.blendfunc1 != GL_ONE || gl_state.blendfunc2 != GL_ZERO);
1820                 switch(vid.renderpath)
1821                 {
1822                 case RENDERPATH_GL11:
1823                 case RENDERPATH_GL13:
1824                 case RENDERPATH_GL20:
1825                 case RENDERPATH_GLES1:
1826                 case RENDERPATH_GLES2:
1827                         CHECKGLERROR
1828                         if (qglBlendFuncSeparate)
1829                         {
1830                                 qglBlendFuncSeparate(gl_state.blendfunc1, gl_state.blendfunc2, GL_ZERO, GL_ONE);CHECKGLERROR // ELUAN: Adreno 225 (and others) compositing workaround
1831                         }
1832                         else
1833                         {
1834                                 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1835                         }
1836                         if (gl_state.blend != blendenable)
1837                         {
1838                                 gl_state.blend = blendenable;
1839                                 if (!gl_state.blend)
1840                                 {
1841                                         qglDisable(GL_BLEND);CHECKGLERROR
1842                                 }
1843                                 else
1844                                 {
1845                                         qglEnable(GL_BLEND);CHECKGLERROR
1846                                 }
1847                         }
1848                         break;
1849                 case RENDERPATH_D3D9:
1850 #ifdef SUPPORTD3D
1851                         {
1852                                 int i;
1853                                 int glblendfunc[2];
1854                                 D3DBLEND d3dblendfunc[2];
1855                                 glblendfunc[0] = gl_state.blendfunc1;
1856                                 glblendfunc[1] = gl_state.blendfunc2;
1857                                 for (i = 0;i < 2;i++)
1858                                 {
1859                                         switch(glblendfunc[i])
1860                                         {
1861                                         case GL_ZERO: d3dblendfunc[i] = D3DBLEND_ZERO;break;
1862                                         case GL_ONE: d3dblendfunc[i] = D3DBLEND_ONE;break;
1863                                         case GL_SRC_COLOR: d3dblendfunc[i] = D3DBLEND_SRCCOLOR;break;
1864                                         case GL_ONE_MINUS_SRC_COLOR: d3dblendfunc[i] = D3DBLEND_INVSRCCOLOR;break;
1865                                         case GL_SRC_ALPHA: d3dblendfunc[i] = D3DBLEND_SRCALPHA;break;
1866                                         case GL_ONE_MINUS_SRC_ALPHA: d3dblendfunc[i] = D3DBLEND_INVSRCALPHA;break;
1867                                         case GL_DST_ALPHA: d3dblendfunc[i] = D3DBLEND_DESTALPHA;break;
1868                                         case GL_ONE_MINUS_DST_ALPHA: d3dblendfunc[i] = D3DBLEND_INVDESTALPHA;break;
1869                                         case GL_DST_COLOR: d3dblendfunc[i] = D3DBLEND_DESTCOLOR;break;
1870                                         case GL_ONE_MINUS_DST_COLOR: d3dblendfunc[i] = D3DBLEND_INVDESTCOLOR;break;
1871                                         }
1872                                 }
1873                                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SRCBLEND, d3dblendfunc[0]);
1874                                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DESTBLEND, d3dblendfunc[1]);
1875                                 if (gl_state.blend != blendenable)
1876                                 {
1877                                         gl_state.blend = blendenable;
1878                                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHABLENDENABLE, gl_state.blend);
1879                                 }
1880                         }
1881 #endif
1882                         break;
1883                 case RENDERPATH_D3D10:
1884                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1885                         break;
1886                 case RENDERPATH_D3D11:
1887                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1888                         break;
1889                 case RENDERPATH_SOFT:
1890                         DPSOFTRAST_BlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);
1891                         break;
1892                 }
1893         }
1894 }
1895
1896 void GL_DepthMask(int state)
1897 {
1898         if (gl_state.depthmask != state)
1899         {
1900                 gl_state.depthmask = state;
1901                 switch(vid.renderpath)
1902                 {
1903                 case RENDERPATH_GL11:
1904                 case RENDERPATH_GL13:
1905                 case RENDERPATH_GL20:
1906                 case RENDERPATH_GLES1:
1907                 case RENDERPATH_GLES2:
1908                         CHECKGLERROR
1909                         qglDepthMask(gl_state.depthmask);CHECKGLERROR
1910                         break;
1911                 case RENDERPATH_D3D9:
1912 #ifdef SUPPORTD3D
1913                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZWRITEENABLE, gl_state.depthmask);
1914 #endif
1915                         break;
1916                 case RENDERPATH_D3D10:
1917                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1918                         break;
1919                 case RENDERPATH_D3D11:
1920                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1921                         break;
1922                 case RENDERPATH_SOFT:
1923                         DPSOFTRAST_DepthMask(gl_state.depthmask);
1924                         break;
1925                 }
1926         }
1927 }
1928
1929 void GL_DepthTest(int state)
1930 {
1931         if (gl_state.depthtest != state)
1932         {
1933                 gl_state.depthtest = state;
1934                 switch(vid.renderpath)
1935                 {
1936                 case RENDERPATH_GL11:
1937                 case RENDERPATH_GL13:
1938                 case RENDERPATH_GL20:
1939                 case RENDERPATH_GLES1:
1940                 case RENDERPATH_GLES2:
1941                         CHECKGLERROR
1942                         if (gl_state.depthtest)
1943                         {
1944                                 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1945                         }
1946                         else
1947                         {
1948                                 qglDisable(GL_DEPTH_TEST);CHECKGLERROR
1949                         }
1950                         break;
1951                 case RENDERPATH_D3D9:
1952 #ifdef SUPPORTD3D
1953                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZENABLE, gl_state.depthtest);
1954 #endif
1955                         break;
1956                 case RENDERPATH_D3D10:
1957                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1958                         break;
1959                 case RENDERPATH_D3D11:
1960                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1961                         break;
1962                 case RENDERPATH_SOFT:
1963                         DPSOFTRAST_DepthTest(gl_state.depthtest);
1964                         break;
1965                 }
1966         }
1967 }
1968
1969 void GL_DepthFunc(int state)
1970 {
1971         if (gl_state.depthfunc != state)
1972         {
1973                 gl_state.depthfunc = state;
1974                 switch(vid.renderpath)
1975                 {
1976                 case RENDERPATH_GL11:
1977                 case RENDERPATH_GL13:
1978                 case RENDERPATH_GL20:
1979                 case RENDERPATH_GLES1:
1980                 case RENDERPATH_GLES2:
1981                         CHECKGLERROR
1982                         qglDepthFunc(gl_state.depthfunc);CHECKGLERROR
1983                         break;
1984                 case RENDERPATH_D3D9:
1985 #ifdef SUPPORTD3D
1986                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZFUNC, d3dcmpforglfunc(gl_state.depthfunc));
1987 #endif
1988                         break;
1989                 case RENDERPATH_D3D10:
1990                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1991                         break;
1992                 case RENDERPATH_D3D11:
1993                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1994                         break;
1995                 case RENDERPATH_SOFT:
1996                         DPSOFTRAST_DepthFunc(gl_state.depthfunc);
1997                         break;
1998                 }
1999         }
2000 }
2001
2002 void GL_DepthRange(float nearfrac, float farfrac)
2003 {
2004         if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
2005         {
2006                 gl_state.depthrange[0] = nearfrac;
2007                 gl_state.depthrange[1] = farfrac;
2008                 switch(vid.renderpath)
2009                 {
2010                 case RENDERPATH_GL11:
2011                 case RENDERPATH_GL13:
2012                 case RENDERPATH_GL20:
2013                 case RENDERPATH_GLES1:
2014                 case RENDERPATH_GLES2:
2015 #ifdef USE_GLES2
2016                         qglDepthRangef(gl_state.depthrange[0], gl_state.depthrange[1]);
2017 #else
2018                         qglDepthRange(gl_state.depthrange[0], gl_state.depthrange[1]);
2019 #endif
2020                         break;
2021                 case RENDERPATH_D3D9:
2022 #ifdef SUPPORTD3D
2023                         {
2024                                 D3DVIEWPORT9 d3dviewport;
2025                                 d3dviewport.X = gl_viewport.x;
2026                                 d3dviewport.Y = gl_viewport.y;
2027                                 d3dviewport.Width = gl_viewport.width;
2028                                 d3dviewport.Height = gl_viewport.height;
2029                                 d3dviewport.MinZ = gl_state.depthrange[0];
2030                                 d3dviewport.MaxZ = gl_state.depthrange[1];
2031                                 IDirect3DDevice9_SetViewport(vid_d3d9dev, &d3dviewport);
2032                         }
2033 #endif
2034                         break;
2035                 case RENDERPATH_D3D10:
2036                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2037                         break;
2038                 case RENDERPATH_D3D11:
2039                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2040                         break;
2041                 case RENDERPATH_SOFT:
2042                         DPSOFTRAST_DepthRange(gl_state.depthrange[0], gl_state.depthrange[1]);
2043                         break;
2044                 }
2045         }
2046 }
2047
2048 void R_SetStencilSeparate(qboolean enable, int writemask, int frontfail, int frontzfail, int frontzpass, int backfail, int backzfail, int backzpass, int frontcompare, int backcompare, int comparereference, int comparemask)
2049 {
2050         switch (vid.renderpath)
2051         {
2052         case RENDERPATH_GL11:
2053         case RENDERPATH_GL13:
2054         case RENDERPATH_GL20:
2055         case RENDERPATH_GLES1:
2056         case RENDERPATH_GLES2:
2057                 CHECKGLERROR
2058                 if (enable)
2059                 {
2060                         qglEnable(GL_STENCIL_TEST);CHECKGLERROR
2061                 }
2062                 else
2063                 {
2064                         qglDisable(GL_STENCIL_TEST);CHECKGLERROR
2065                 }
2066                 if (vid.support.ati_separate_stencil)
2067                 {
2068                         qglStencilMask(writemask);CHECKGLERROR
2069                         qglStencilOpSeparate(GL_FRONT, frontfail, frontzfail, frontzpass);CHECKGLERROR
2070                         qglStencilOpSeparate(GL_BACK, backfail, backzfail, backzpass);CHECKGLERROR
2071                         qglStencilFuncSeparate(GL_FRONT, frontcompare, comparereference, comparereference);CHECKGLERROR
2072                         qglStencilFuncSeparate(GL_BACK, backcompare, comparereference, comparereference);CHECKGLERROR
2073                 }
2074                 else if (vid.support.ext_stencil_two_side)
2075                 {
2076 #if defined(GL_STENCIL_TEST_TWO_SIDE_EXT) && !defined(USE_GLES2)
2077                         qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
2078                         qglActiveStencilFaceEXT(GL_FRONT);CHECKGLERROR
2079                         qglStencilMask(writemask);CHECKGLERROR
2080                         qglStencilOp(frontfail, frontzfail, frontzpass);CHECKGLERROR
2081                         qglStencilFunc(frontcompare, comparereference, comparemask);CHECKGLERROR
2082                         qglActiveStencilFaceEXT(GL_BACK);CHECKGLERROR
2083                         qglStencilMask(writemask);CHECKGLERROR
2084                         qglStencilOp(backfail, backzfail, backzpass);CHECKGLERROR
2085                         qglStencilFunc(backcompare, comparereference, comparemask);CHECKGLERROR
2086 #endif
2087                 }
2088                 break;
2089         case RENDERPATH_D3D9:
2090 #ifdef SUPPORTD3D
2091                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_TWOSIDEDSTENCILMODE, true);
2092                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILENABLE, enable);
2093                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILWRITEMASK, writemask);
2094                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFAIL, d3dstencilopforglfunc(frontfail));
2095                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILZFAIL, d3dstencilopforglfunc(frontzfail));
2096                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILPASS, d3dstencilopforglfunc(frontzpass));
2097                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFUNC, d3dcmpforglfunc(frontcompare));
2098                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILFAIL, d3dstencilopforglfunc(backfail));
2099                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILZFAIL, d3dstencilopforglfunc(backzfail));
2100                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILPASS, d3dstencilopforglfunc(backzpass));
2101                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILFUNC, d3dcmpforglfunc(backcompare));
2102                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILREF, comparereference);
2103                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILMASK, comparemask);
2104 #endif
2105                 break;
2106         case RENDERPATH_D3D10:
2107                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2108                 break;
2109         case RENDERPATH_D3D11:
2110                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2111                 break;
2112         case RENDERPATH_SOFT:
2113                 //Con_DPrintf("FIXME SOFT %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2114                 break;
2115         }
2116 }
2117
2118 void R_SetStencil(qboolean enable, int writemask, int fail, int zfail, int zpass, int compare, int comparereference, int comparemask)
2119 {
2120         switch (vid.renderpath)
2121         {
2122         case RENDERPATH_GL11:
2123         case RENDERPATH_GL13:
2124         case RENDERPATH_GL20:
2125         case RENDERPATH_GLES1:
2126         case RENDERPATH_GLES2:
2127                 CHECKGLERROR
2128                 if (enable)
2129                 {
2130                         qglEnable(GL_STENCIL_TEST);CHECKGLERROR
2131                 }
2132                 else
2133                 {
2134                         qglDisable(GL_STENCIL_TEST);CHECKGLERROR
2135                 }
2136                 if (vid.support.ext_stencil_two_side)
2137                 {
2138 #ifdef GL_STENCIL_TEST_TWO_SIDE_EXT
2139                         qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
2140 #endif
2141                 }
2142                 qglStencilMask(writemask);CHECKGLERROR
2143                 qglStencilOp(fail, zfail, zpass);CHECKGLERROR
2144                 qglStencilFunc(compare, comparereference, comparemask);CHECKGLERROR
2145                 CHECKGLERROR
2146                 break;
2147         case RENDERPATH_D3D9:
2148 #ifdef SUPPORTD3D
2149                 if (vid.support.ati_separate_stencil)
2150                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_TWOSIDEDSTENCILMODE, true);
2151                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILENABLE, enable);
2152                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILWRITEMASK, writemask);
2153                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFAIL, d3dstencilopforglfunc(fail));
2154                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILZFAIL, d3dstencilopforglfunc(zfail));
2155                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILPASS, d3dstencilopforglfunc(zpass));
2156                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFUNC, d3dcmpforglfunc(compare));
2157                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILREF, comparereference);
2158                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILMASK, comparemask);
2159 #endif
2160                 break;
2161         case RENDERPATH_D3D10:
2162                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2163                 break;
2164         case RENDERPATH_D3D11:
2165                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2166                 break;
2167         case RENDERPATH_SOFT:
2168                 //Con_DPrintf("FIXME SOFT %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2169                 break;
2170         }
2171 }
2172
2173 void GL_PolygonOffset(float planeoffset, float depthoffset)
2174 {
2175         if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
2176         {
2177                 gl_state.polygonoffset[0] = planeoffset;
2178                 gl_state.polygonoffset[1] = depthoffset;
2179                 switch(vid.renderpath)
2180                 {
2181                 case RENDERPATH_GL11:
2182                 case RENDERPATH_GL13:
2183                 case RENDERPATH_GL20:
2184                 case RENDERPATH_GLES1:
2185                 case RENDERPATH_GLES2:
2186                         qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
2187                         break;
2188                 case RENDERPATH_D3D9:
2189 #ifdef SUPPORTD3D
2190                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SLOPESCALEDEPTHBIAS, gl_state.polygonoffset[0]);
2191                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DEPTHBIAS, gl_state.polygonoffset[1] * (1.0f / 16777216.0f));
2192 #endif
2193                         break;
2194                 case RENDERPATH_D3D10:
2195                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2196                         break;
2197                 case RENDERPATH_D3D11:
2198                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2199                         break;
2200                 case RENDERPATH_SOFT:
2201                         DPSOFTRAST_PolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
2202                         break;
2203                 }
2204         }
2205 }
2206
2207 void GL_SetMirrorState(qboolean state)
2208 {
2209         if (v_flipped_state != state)
2210         {
2211                 v_flipped_state = state;
2212                 if (gl_state.cullface == GL_BACK)
2213                         gl_state.cullface = GL_FRONT;
2214                 else if (gl_state.cullface == GL_FRONT)
2215                         gl_state.cullface = GL_BACK;
2216                 else
2217                         return;
2218                 switch(vid.renderpath)
2219                 {
2220                 case RENDERPATH_GL11:
2221                 case RENDERPATH_GL13:
2222                 case RENDERPATH_GL20:
2223                 case RENDERPATH_GLES1:
2224                 case RENDERPATH_GLES2:
2225                         qglCullFace(gl_state.cullface);CHECKGLERROR
2226                         break;
2227                 case RENDERPATH_D3D9:
2228 #ifdef SUPPORTD3D
2229                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, gl_state.cullface == GL_FRONT ? D3DCULL_CCW : D3DCULL_CW);
2230 #endif
2231                         break;
2232                 case RENDERPATH_D3D10:
2233                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2234                         break;
2235                 case RENDERPATH_D3D11:
2236                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2237                         break;
2238                 case RENDERPATH_SOFT:
2239                         DPSOFTRAST_CullFace(gl_state.cullface);
2240                         break;
2241                 }
2242         }
2243 }
2244
2245 void GL_CullFace(int state)
2246 {
2247         if(v_flipped_state)
2248         {
2249                 if(state == GL_FRONT)
2250                         state = GL_BACK;
2251                 else if(state == GL_BACK)
2252                         state = GL_FRONT;
2253         }
2254
2255         switch(vid.renderpath)
2256         {
2257         case RENDERPATH_GL11:
2258         case RENDERPATH_GL13:
2259         case RENDERPATH_GL20:
2260         case RENDERPATH_GLES1:
2261         case RENDERPATH_GLES2:
2262                 CHECKGLERROR
2263
2264                 if (state != GL_NONE)
2265                 {
2266                         if (!gl_state.cullfaceenable)
2267                         {
2268                                 gl_state.cullfaceenable = true;
2269                                 qglEnable(GL_CULL_FACE);CHECKGLERROR
2270                         }
2271                         if (gl_state.cullface != state)
2272                         {
2273                                 gl_state.cullface = state;
2274                                 qglCullFace(gl_state.cullface);CHECKGLERROR
2275                         }
2276                 }
2277                 else
2278                 {
2279                         if (gl_state.cullfaceenable)
2280                         {
2281                                 gl_state.cullfaceenable = false;
2282                                 qglDisable(GL_CULL_FACE);CHECKGLERROR
2283                         }
2284                 }
2285                 break;
2286         case RENDERPATH_D3D9:
2287 #ifdef SUPPORTD3D
2288                 if (gl_state.cullface != state)
2289                 {
2290                         gl_state.cullface = state;
2291                         switch(gl_state.cullface)
2292                         {
2293                         case GL_NONE:
2294                                 gl_state.cullfaceenable = false;
2295                                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_NONE);
2296                                 break;
2297                         case GL_FRONT:
2298                                 gl_state.cullfaceenable = true;
2299                                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_CCW);
2300                                 break;
2301                         case GL_BACK:
2302                                 gl_state.cullfaceenable = true;
2303                                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_CW);
2304                                 break;
2305                         }
2306                 }
2307 #endif
2308                 break;
2309         case RENDERPATH_D3D10:
2310                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2311                 break;
2312         case RENDERPATH_D3D11:
2313                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2314                 break;
2315         case RENDERPATH_SOFT:
2316                 if (gl_state.cullface != state)
2317                 {
2318                         gl_state.cullface = state;
2319                         gl_state.cullfaceenable = state != GL_NONE ? true : false;
2320                         DPSOFTRAST_CullFace(gl_state.cullface);
2321                 }
2322                 break;
2323         }
2324 }
2325
2326 void GL_AlphaTest(int state)
2327 {
2328         if (gl_state.alphatest != state)
2329         {
2330                 gl_state.alphatest = state;
2331                 switch(vid.renderpath)
2332                 {
2333                 case RENDERPATH_GL11:
2334                 case RENDERPATH_GL13:
2335                 case RENDERPATH_GLES1:
2336 #ifdef GL_ALPHA_TEST
2337                         // only fixed function uses alpha test, other paths use pixel kill capability in shaders
2338                         CHECKGLERROR
2339                         if (gl_state.alphatest)
2340                         {
2341                                 qglEnable(GL_ALPHA_TEST);CHECKGLERROR
2342                         }
2343                         else
2344                         {
2345                                 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
2346                         }
2347 #endif
2348                         break;
2349                 case RENDERPATH_D3D9:
2350                 case RENDERPATH_D3D10:
2351                 case RENDERPATH_D3D11:
2352                 case RENDERPATH_SOFT:
2353                 case RENDERPATH_GL20:
2354                 case RENDERPATH_GLES2:
2355                         break;
2356                 }
2357         }
2358 }
2359
2360 void GL_AlphaToCoverage(qboolean state)
2361 {
2362         if (gl_state.alphatocoverage != state)
2363         {
2364                 gl_state.alphatocoverage = state;
2365                 switch(vid.renderpath)
2366                 {
2367                 case RENDERPATH_GL11:
2368                 case RENDERPATH_GL13:
2369                 case RENDERPATH_GLES1:
2370                 case RENDERPATH_GLES2:
2371                 case RENDERPATH_D3D9:
2372                 case RENDERPATH_D3D10:
2373                 case RENDERPATH_D3D11:
2374                 case RENDERPATH_SOFT:
2375                         break;
2376                 case RENDERPATH_GL20:
2377 #ifdef GL_SAMPLE_ALPHA_TO_COVERAGE_ARB
2378                         // alpha to coverage turns the alpha value of the pixel into 0%, 25%, 50%, 75% or 100% by masking the multisample fragments accordingly
2379                         CHECKGLERROR
2380                         if (gl_state.alphatocoverage)
2381                         {
2382                                 qglEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);CHECKGLERROR
2383 //                              qglEnable(GL_MULTISAMPLE_ARB);CHECKGLERROR
2384                         }
2385                         else
2386                         {
2387                                 qglDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);CHECKGLERROR
2388 //                              qglDisable(GL_MULTISAMPLE_ARB);CHECKGLERROR
2389                         }
2390 #endif
2391                         break;
2392                 }
2393         }
2394 }
2395
2396 void GL_ColorMask(int r, int g, int b, int a)
2397 {
2398         // NOTE: this matches D3DCOLORWRITEENABLE_RED, GREEN, BLUE, ALPHA
2399         int state = (r ? 1 : 0) | (g ? 2 : 0) | (b ? 4 : 0) | (a ? 8 : 0);
2400         if (gl_state.colormask != state)
2401         {
2402                 gl_state.colormask = state;
2403                 switch(vid.renderpath)
2404                 {
2405                 case RENDERPATH_GL11:
2406                 case RENDERPATH_GL13:
2407                 case RENDERPATH_GL20:
2408                 case RENDERPATH_GLES1:
2409                 case RENDERPATH_GLES2:
2410                         CHECKGLERROR
2411                         qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
2412                         break;
2413                 case RENDERPATH_D3D9:
2414 #ifdef SUPPORTD3D
2415                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_COLORWRITEENABLE, state);
2416 #endif
2417                         break;
2418                 case RENDERPATH_D3D10:
2419                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2420                         break;
2421                 case RENDERPATH_D3D11:
2422                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2423                         break;
2424                 case RENDERPATH_SOFT:
2425                         DPSOFTRAST_ColorMask(r, g, b, a);
2426                         break;
2427                 }
2428         }
2429 }
2430
2431 void GL_Color(float cr, float cg, float cb, float ca)
2432 {
2433         if (gl_state.pointer_color_enabled || gl_state.color4f[0] != cr || gl_state.color4f[1] != cg || gl_state.color4f[2] != cb || gl_state.color4f[3] != ca)
2434         {
2435                 gl_state.color4f[0] = cr;
2436                 gl_state.color4f[1] = cg;
2437                 gl_state.color4f[2] = cb;
2438                 gl_state.color4f[3] = ca;
2439                 switch(vid.renderpath)
2440                 {
2441                 case RENDERPATH_GL11:
2442                 case RENDERPATH_GL13:
2443                 case RENDERPATH_GLES1:
2444 #ifndef USE_GLES2
2445                         CHECKGLERROR
2446                         qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
2447                         CHECKGLERROR
2448 #endif
2449                         break;
2450                 case RENDERPATH_D3D9:
2451                 case RENDERPATH_D3D10:
2452                 case RENDERPATH_D3D11:
2453                         // no equivalent in D3D
2454                         break;
2455                 case RENDERPATH_SOFT:
2456                         DPSOFTRAST_Color4f(cr, cg, cb, ca);
2457                         break;
2458                 case RENDERPATH_GL20:
2459                 case RENDERPATH_GLES2:
2460                         qglVertexAttrib4f(GLSLATTRIB_COLOR, cr, cg, cb, ca);
2461                         break;
2462                 }
2463         }
2464 }
2465
2466 void GL_Scissor (int x, int y, int width, int height)
2467 {
2468         switch(vid.renderpath)
2469         {
2470         case RENDERPATH_GL11:
2471         case RENDERPATH_GL13:
2472         case RENDERPATH_GL20:
2473         case RENDERPATH_GLES1:
2474         case RENDERPATH_GLES2:
2475                 CHECKGLERROR
2476                 qglScissor(x, y,width,height);
2477                 CHECKGLERROR
2478                 break;
2479         case RENDERPATH_D3D9:
2480 #ifdef SUPPORTD3D
2481                 {
2482                         RECT d3drect;
2483                         d3drect.left = x;
2484                         d3drect.top = y;
2485                         d3drect.right = x + width;
2486                         d3drect.bottom = y + height;
2487                         IDirect3DDevice9_SetScissorRect(vid_d3d9dev, &d3drect);
2488                 }
2489 #endif
2490                 break;
2491         case RENDERPATH_D3D10:
2492                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2493                 break;
2494         case RENDERPATH_D3D11:
2495                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2496                 break;
2497         case RENDERPATH_SOFT:
2498                 DPSOFTRAST_Scissor(x, y, width, height);
2499                 break;
2500         }
2501 }
2502
2503 void GL_ScissorTest(int state)
2504 {
2505         if (gl_state.scissortest != state)
2506         {
2507                 gl_state.scissortest = state;
2508                 switch(vid.renderpath)
2509                 {
2510                 case RENDERPATH_GL11:
2511                 case RENDERPATH_GL13:
2512                 case RENDERPATH_GL20:
2513                 case RENDERPATH_GLES1:
2514                 case RENDERPATH_GLES2:
2515                         CHECKGLERROR
2516                         if(gl_state.scissortest)
2517                                 qglEnable(GL_SCISSOR_TEST);
2518                         else
2519                                 qglDisable(GL_SCISSOR_TEST);
2520                         CHECKGLERROR
2521                         break;
2522                 case RENDERPATH_D3D9:
2523 #ifdef SUPPORTD3D
2524                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SCISSORTESTENABLE, gl_state.scissortest);
2525 #endif
2526                         break;
2527                 case RENDERPATH_D3D10:
2528                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2529                         break;
2530                 case RENDERPATH_D3D11:
2531                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2532                         break;
2533                 case RENDERPATH_SOFT:
2534                         DPSOFTRAST_ScissorTest(gl_state.scissortest);
2535                         break;
2536                 }
2537         }
2538 }
2539
2540 void GL_Clear(int mask, const float *colorvalue, float depthvalue, int stencilvalue)
2541 {
2542         // opaque black - if you want transparent black, you'll need to pass in a colorvalue
2543         static const float blackcolor[4] = {0.0f, 0.0f, 0.0f, 1.0f};
2544         // prevent warnings when trying to clear a buffer that does not exist
2545         if (!colorvalue)
2546                 colorvalue = blackcolor;
2547         if (!vid.stencil)
2548         {
2549                 mask &= ~GL_STENCIL_BUFFER_BIT;
2550                 stencilvalue = 0;
2551         }
2552         switch(vid.renderpath)
2553         {
2554         case RENDERPATH_GL11:
2555         case RENDERPATH_GL13:
2556         case RENDERPATH_GL20:
2557         case RENDERPATH_GLES1:
2558         case RENDERPATH_GLES2:
2559                 CHECKGLERROR
2560                 if (mask & GL_COLOR_BUFFER_BIT)
2561                 {
2562                         qglClearColor(colorvalue[0], colorvalue[1], colorvalue[2], colorvalue[3]);CHECKGLERROR
2563                 }
2564                 if (mask & GL_DEPTH_BUFFER_BIT)
2565                 {
2566 #ifdef USE_GLES2
2567                         qglClearDepthf(depthvalue);CHECKGLERROR
2568 #else
2569                         qglClearDepth(depthvalue);CHECKGLERROR
2570 #endif
2571                 }
2572                 if (mask & GL_STENCIL_BUFFER_BIT)
2573                 {
2574                         qglClearStencil(stencilvalue);CHECKGLERROR
2575                 }
2576                 qglClear(mask);CHECKGLERROR
2577                 break;
2578         case RENDERPATH_D3D9:
2579 #ifdef SUPPORTD3D
2580                 IDirect3DDevice9_Clear(vid_d3d9dev, 0, NULL, ((mask & GL_COLOR_BUFFER_BIT) ? D3DCLEAR_TARGET : 0) | ((mask & GL_STENCIL_BUFFER_BIT) ? D3DCLEAR_STENCIL : 0) | ((mask & GL_DEPTH_BUFFER_BIT) ? D3DCLEAR_ZBUFFER : 0), D3DCOLOR_COLORVALUE(colorvalue[0], colorvalue[1], colorvalue[2], colorvalue[3]), depthvalue, stencilvalue);
2581 #endif
2582                 break;
2583         case RENDERPATH_D3D10:
2584                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2585                 break;
2586         case RENDERPATH_D3D11:
2587                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2588                 break;
2589         case RENDERPATH_SOFT:
2590                 if (mask & GL_COLOR_BUFFER_BIT)
2591                         DPSOFTRAST_ClearColor(colorvalue[0], colorvalue[1], colorvalue[2], colorvalue[3]);
2592                 if (mask & GL_DEPTH_BUFFER_BIT)
2593                         DPSOFTRAST_ClearDepth(depthvalue);
2594                 break;
2595         }
2596 }
2597
2598 void GL_ReadPixelsBGRA(int x, int y, int width, int height, unsigned char *outpixels)
2599 {
2600         switch(vid.renderpath)
2601         {
2602         case RENDERPATH_GL11:
2603         case RENDERPATH_GL13:
2604         case RENDERPATH_GL20:
2605         case RENDERPATH_GLES1:
2606         case RENDERPATH_GLES2:
2607                 CHECKGLERROR
2608 #ifndef GL_BGRA
2609                 {
2610                         int i;
2611                         int r;
2612                 //      int g;
2613                         int b;
2614                 //      int a;
2615                         qglReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, outpixels);CHECKGLERROR
2616                         for (i = 0;i < width * height * 4;i += 4)
2617                         {
2618                                 r = outpixels[i+0];
2619                 //              g = outpixels[i+1];
2620                                 b = outpixels[i+2];
2621                 //              a = outpixels[i+3];
2622                                 outpixels[i+0] = b;
2623                 //              outpixels[i+1] = g;
2624                                 outpixels[i+2] = r;
2625                 //              outpixels[i+3] = a;
2626                         }
2627                 }
2628 #else
2629                 qglReadPixels(x, y, width, height, GL_BGRA, GL_UNSIGNED_BYTE, outpixels);CHECKGLERROR
2630 #endif
2631                         break;
2632         case RENDERPATH_D3D9:
2633 #ifdef SUPPORTD3D
2634                 {
2635                         // LordHavoc: we can't directly download the backbuffer because it may be
2636                         // multisampled, and it may not be lockable, so we blit it to a lockable
2637                         // surface of the same dimensions (but without multisample) to resolve the
2638                         // multisample buffer to a normal image, and then lock that...
2639                         IDirect3DSurface9 *stretchsurface = NULL;
2640                         if (!FAILED(IDirect3DDevice9_CreateRenderTarget(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, TRUE, &stretchsurface, NULL)))
2641                         {
2642                                 D3DLOCKED_RECT lockedrect;
2643                                 if (!FAILED(IDirect3DDevice9_StretchRect(vid_d3d9dev, gl_state.d3drt_backbuffercolorsurface, NULL, stretchsurface, NULL, D3DTEXF_POINT)))
2644                                 {
2645                                         if (!FAILED(IDirect3DSurface9_LockRect(stretchsurface, &lockedrect, NULL, D3DLOCK_READONLY)))
2646                                         {
2647                                                 int line;
2648                                                 unsigned char *row = (unsigned char *)lockedrect.pBits + x * 4 + lockedrect.Pitch * (vid.height - 1 - y);
2649                                                 for (line = 0;line < height;line++, row -= lockedrect.Pitch)
2650                                                         memcpy(outpixels + line * width * 4, row, width * 4);
2651                                                 IDirect3DSurface9_UnlockRect(stretchsurface);
2652                                         }
2653                                 }
2654                                 IDirect3DSurface9_Release(stretchsurface);
2655                         }
2656                         // code scraps
2657                         //IDirect3DSurface9 *syssurface = NULL;
2658                         //if (!FAILED(IDirect3DDevice9_CreateRenderTarget(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, FALSE, &stretchsurface, NULL)))
2659                         //if (!FAILED(IDirect3DDevice9_CreateOffscreenPlainSurface(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &syssurface, NULL)))
2660                         //IDirect3DDevice9_GetRenderTargetData(vid_d3d9dev, gl_state.d3drt_backbuffercolorsurface, syssurface);
2661                         //if (!FAILED(IDirect3DDevice9_GetFrontBufferData(vid_d3d9dev, 0, syssurface)))
2662                         //if (!FAILED(IDirect3DSurface9_LockRect(syssurface, &lockedrect, NULL, D3DLOCK_READONLY)))
2663                         //IDirect3DSurface9_UnlockRect(syssurface);
2664                         //IDirect3DSurface9_Release(syssurface);
2665                 }
2666 #endif
2667                 break;
2668         case RENDERPATH_D3D10:
2669                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2670                 break;
2671         case RENDERPATH_D3D11:
2672                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2673                 break;
2674         case RENDERPATH_SOFT:
2675                 DPSOFTRAST_GetPixelsBGRA(x, y, width, height, outpixels);
2676                 break;
2677         }
2678 }
2679
2680 // called at beginning of frame
2681 void R_Mesh_Start(void)
2682 {
2683         BACKENDACTIVECHECK
2684         R_Mesh_SetRenderTargets(0, NULL, NULL, NULL, NULL, NULL);
2685         R_Mesh_SetUseVBO();
2686         if (gl_printcheckerror.integer && !gl_paranoid.integer)
2687         {
2688                 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
2689                 Cvar_SetValueQuick(&gl_paranoid, 1);
2690         }
2691 }
2692
2693 static qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
2694 {
2695         int shaderobject;
2696         int shadercompiled;
2697         char compilelog[MAX_INPUTLINE];
2698         shaderobject = qglCreateShader(shadertypeenum);CHECKGLERROR
2699         if (!shaderobject)
2700                 return false;
2701         qglShaderSource(shaderobject, numstrings, strings, NULL);CHECKGLERROR
2702         qglCompileShader(shaderobject);CHECKGLERROR
2703         qglGetShaderiv(shaderobject, GL_COMPILE_STATUS, &shadercompiled);CHECKGLERROR
2704         qglGetShaderInfoLog(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
2705         if (compilelog[0] && ((strstr(compilelog, "error") || strstr(compilelog, "ERROR") || strstr(compilelog, "Error")) || ((strstr(compilelog, "WARNING") || strstr(compilelog, "warning") || strstr(compilelog, "Warning")) && developer.integer) || developer_extra.integer))
2706         {
2707                 int i, j, pretextlines = 0;
2708                 for (i = 0;i < numstrings - 1;i++)
2709                         for (j = 0;strings[i][j];j++)
2710                                 if (strings[i][j] == '\n')
2711                                         pretextlines++;
2712                 Con_Printf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
2713         }
2714         if (!shadercompiled)
2715         {
2716                 qglDeleteShader(shaderobject);CHECKGLERROR
2717                 return false;
2718         }
2719         qglAttachShader(programobject, shaderobject);CHECKGLERROR
2720         qglDeleteShader(shaderobject);CHECKGLERROR
2721         return true;
2722 }
2723
2724 unsigned int GL_Backend_CompileProgram(int vertexstrings_count, const char **vertexstrings_list, int geometrystrings_count, const char **geometrystrings_list, int fragmentstrings_count, const char **fragmentstrings_list)
2725 {
2726         GLint programlinked;
2727         GLuint programobject = 0;
2728         char linklog[MAX_INPUTLINE];
2729         CHECKGLERROR
2730
2731         programobject = qglCreateProgram();CHECKGLERROR
2732         if (!programobject)
2733                 return 0;
2734
2735         qglBindAttribLocation(programobject, GLSLATTRIB_POSITION , "Attrib_Position" );
2736         qglBindAttribLocation(programobject, GLSLATTRIB_COLOR    , "Attrib_Color"    );
2737         qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD0, "Attrib_TexCoord0");
2738         qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD1, "Attrib_TexCoord1");
2739         qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD2, "Attrib_TexCoord2");
2740         qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD3, "Attrib_TexCoord3");
2741         qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD4, "Attrib_TexCoord4");
2742         qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD5, "Attrib_TexCoord5");
2743         qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD6, "Attrib_SkeletalIndex");
2744         qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD7, "Attrib_SkeletalWeight");
2745 #ifndef USE_GLES2
2746         if(vid.support.gl20shaders130)
2747                 qglBindFragDataLocation(programobject, 0, "dp_FragColor");
2748 #endif
2749
2750         if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER, "vertex", vertexstrings_count, vertexstrings_list))
2751                 goto cleanup;
2752
2753 #if defined(GL_GEOMETRY_SHADER) && !defined(USE_GLES2)
2754         if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER, "geometry", geometrystrings_count, geometrystrings_list))
2755                 goto cleanup;
2756 #endif
2757
2758         if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER, "fragment", fragmentstrings_count, fragmentstrings_list))
2759                 goto cleanup;
2760
2761         qglLinkProgram(programobject);CHECKGLERROR
2762         qglGetProgramiv(programobject, GL_LINK_STATUS, &programlinked);CHECKGLERROR
2763         qglGetProgramInfoLog(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
2764
2765         if (linklog[0])
2766         {
2767
2768                 if (strstr(linklog, "error") || strstr(linklog, "ERROR") || strstr(linklog, "Error") || strstr(linklog, "WARNING") || strstr(linklog, "warning") || strstr(linklog, "Warning") || developer_extra.integer)
2769                         Con_DPrintf("program link log:\n%s\n", linklog);
2770
2771                 // software vertex shader is ok but software fragment shader is WAY
2772                 // too slow, fail program if so.
2773                 // NOTE: this string might be ATI specific, but that's ok because the
2774                 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
2775                 // software fragment shader due to low instruction and dependent
2776                 // texture limits.
2777                 if (strstr(linklog, "fragment shader will run in software"))
2778                         programlinked = false;
2779         }
2780
2781         if (!programlinked)
2782                 goto cleanup;
2783
2784         return programobject;
2785 cleanup:
2786         qglDeleteProgram(programobject);CHECKGLERROR
2787         return 0;
2788 }
2789
2790 void GL_Backend_FreeProgram(unsigned int prog)
2791 {
2792         CHECKGLERROR
2793         qglDeleteProgram(prog);
2794         CHECKGLERROR
2795 }
2796
2797 // renders triangles using vertices from the active arrays
2798 int paranoidblah = 0;
2799 void R_Mesh_Draw(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const r_meshbuffer_t *element3i_indexbuffer, int element3i_bufferoffset, const unsigned short *element3s, const r_meshbuffer_t *element3s_indexbuffer, int element3s_bufferoffset)
2800 {
2801         unsigned int numelements = numtriangles * 3;
2802         int bufferobject3i;
2803         size_t bufferoffset3i;
2804         int bufferobject3s;
2805         size_t bufferoffset3s;
2806         if (numvertices < 3 || numtriangles < 1)
2807         {
2808                 if (numvertices < 0 || numtriangles < 0 || developer_extra.integer)
2809                         Con_DPrintf("R_Mesh_Draw(%d, %d, %d, %d, %8p, %8p, %8x, %8p, %8p, %8x);\n", firstvertex, numvertices, firsttriangle, numtriangles, (void *)element3i, (void *)element3i_indexbuffer, (int)element3i_bufferoffset, (void *)element3s, (void *)element3s_indexbuffer, (int)element3s_bufferoffset);
2810                 return;
2811         }
2812         // adjust the pointers for firsttriangle
2813         if (element3i)
2814                 element3i += firsttriangle * 3;
2815         if (element3i_indexbuffer)
2816                 element3i_bufferoffset += firsttriangle * 3 * sizeof(*element3i);
2817         if (element3s)
2818                 element3s += firsttriangle * 3;
2819         if (element3s_indexbuffer)
2820                 element3s_bufferoffset += firsttriangle * 3 * sizeof(*element3s);
2821         switch(vid.renderpath)
2822         {
2823         case RENDERPATH_GL11:
2824         case RENDERPATH_GL13:
2825         case RENDERPATH_GL20:
2826         case RENDERPATH_GLES1:
2827         case RENDERPATH_GLES2:
2828                 // check if the user specified to ignore static index buffers
2829                 if (!gl_state.usevbo_staticindex || (gl_vbo.integer == 3 && !vid.forcevbo && (element3i_bufferoffset || element3s_bufferoffset)))
2830                 {
2831                         element3i_indexbuffer = NULL;
2832                         element3s_indexbuffer = NULL;
2833                 }
2834                 break;
2835         case RENDERPATH_D3D9:
2836         case RENDERPATH_D3D10:
2837         case RENDERPATH_D3D11:
2838                 break;
2839         case RENDERPATH_SOFT:
2840                 break;
2841         }
2842         // upload a dynamic index buffer if needed
2843         if (element3s)
2844         {
2845                 if (!element3s_indexbuffer && gl_state.usevbo_dynamicindex)
2846                         element3s_indexbuffer = R_BufferData_Store(numelements * sizeof(*element3s), (void *)element3s, R_BUFFERDATA_INDEX16, &element3s_bufferoffset);
2847         }
2848         else if (element3i)
2849         {
2850                 if (!element3i_indexbuffer && gl_state.usevbo_dynamicindex)
2851                         element3i_indexbuffer = R_BufferData_Store(numelements * sizeof(*element3i), (void *)element3i, R_BUFFERDATA_INDEX32, &element3i_bufferoffset);
2852         }
2853         bufferobject3i = element3i_indexbuffer ? element3i_indexbuffer->bufferobject : 0;
2854         bufferoffset3i = element3i_bufferoffset;
2855         bufferobject3s = element3s_indexbuffer ? element3s_indexbuffer->bufferobject : 0;
2856         bufferoffset3s = element3s_bufferoffset;
2857         r_refdef.stats[r_stat_draws]++;
2858         r_refdef.stats[r_stat_draws_vertices] += numvertices;
2859         r_refdef.stats[r_stat_draws_elements] += numelements;
2860         if (gl_paranoid.integer)
2861         {
2862                 unsigned int i;
2863                 // LordHavoc: disabled this - it needs to be updated to handle components and gltype and stride in each array
2864 #if 0
2865                 unsigned int j, size;
2866                 const int *p;
2867                 // note: there's no validation done here on buffer objects because it
2868                 // is somewhat difficult to get at the data, and gl_paranoid can be
2869                 // used without buffer objects if the need arises
2870                 // (the data could be gotten using glMapBuffer but it would be very
2871                 //  slow due to uncachable video memory reads)
2872                 if (!qglIsEnabled(GL_VERTEX_ARRAY))
2873                         Con_Print("R_Mesh_Draw: vertex array not enabled\n");
2874                 CHECKGLERROR
2875                 if (gl_state.pointer_vertex_pointer)
2876                         for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
2877                                 paranoidblah += *p;
2878                 if (gl_state.pointer_color_enabled)
2879                 {
2880                         if (!qglIsEnabled(GL_COLOR_ARRAY))
2881                                 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
2882                         CHECKGLERROR
2883                         if (gl_state.pointer_color && gl_state.pointer_color_enabled)
2884                                 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
2885                                         paranoidblah += *p;
2886                 }
2887                 for (i = 0;i < vid.texarrayunits;i++)
2888                 {
2889                         if (gl_state.units[i].arrayenabled)
2890                         {
2891                                 GL_ClientActiveTexture(i);
2892                                 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
2893                                         Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
2894                                 CHECKGLERROR
2895                                 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
2896                                         for (j = 0, size = numvertices * gl_state.units[i].arraycomponents, p = (int *)((float *)gl_state.units[i].pointer_texcoord + firstvertex * gl_state.units[i].arraycomponents);j < size;j++, p++)
2897                                                 paranoidblah += *p;
2898                         }
2899                 }
2900 #endif
2901                 if (element3i)
2902                 {
2903                         for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2904                         {
2905                                 if (element3i[i] < firstvertex || element3i[i] >= firstvertex + numvertices)
2906                                 {
2907                                         Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3i array\n", element3i[i], firstvertex, firstvertex + numvertices);
2908                                         return;
2909                                 }
2910                         }
2911                 }
2912                 if (element3s)
2913                 {
2914                         for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2915                         {
2916                                 if (element3s[i] < firstvertex || element3s[i] >= firstvertex + numvertices)
2917                                 {
2918                                         Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3s array\n", element3s[i], firstvertex, firstvertex + numvertices);
2919                                         return;
2920                                 }
2921                         }
2922                 }
2923         }
2924         if (r_render.integer || r_refdef.draw2dstage)
2925         {
2926                 switch(vid.renderpath)
2927                 {
2928                 case RENDERPATH_GL11:
2929                 case RENDERPATH_GL13:
2930                 case RENDERPATH_GL20:
2931                         CHECKGLERROR
2932                         if (gl_mesh_testmanualfeeding.integer)
2933                         {
2934 #ifndef USE_GLES2
2935                                 unsigned int i, j, element;
2936                                 const GLfloat *p;
2937                                 qglBegin(GL_TRIANGLES);
2938                                 if(vid.renderpath == RENDERPATH_GL20)
2939                                 {
2940                                         for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2941                                         {
2942                                                 if (element3i)
2943                                                         element = element3i[i];
2944                                                 else if (element3s)
2945                                                         element = element3s[i];
2946                                                 else
2947                                                         element = firstvertex + i;
2948                                                 for (j = 0;j < vid.texarrayunits;j++)
2949                                                 {
2950                                                         if (gl_state.units[j].pointer_texcoord_pointer && gl_state.units[j].arrayenabled)
2951                                                         {
2952                                                                 if (gl_state.units[j].pointer_texcoord_gltype == GL_FLOAT)
2953                                                                 {
2954                                                                         p = (const GLfloat *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2955                                                                         if (gl_state.units[j].pointer_texcoord_components == 4)
2956                                                                                 qglVertexAttrib4f(GLSLATTRIB_TEXCOORD0 + j, p[0], p[1], p[2], p[3]);
2957                                                                         else if (gl_state.units[j].pointer_texcoord_components == 3)
2958                                                                                 qglVertexAttrib3f(GLSLATTRIB_TEXCOORD0 + j, p[0], p[1], p[2]);
2959                                                                         else if (gl_state.units[j].pointer_texcoord_components == 2)
2960                                                                                 qglVertexAttrib2f(GLSLATTRIB_TEXCOORD0 + j, p[0], p[1]);
2961                                                                         else
2962                                                                                 qglVertexAttrib1f(GLSLATTRIB_TEXCOORD0 + j, p[0]);
2963                                                                 }
2964                                                                 else if (gl_state.units[j].pointer_texcoord_gltype == (int)(GL_SHORT | 0x80000000))
2965                                                                 {
2966                                                                         const GLshort *s = (const GLshort *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2967                                                                         if (gl_state.units[j].pointer_texcoord_components == 4)
2968                                                                                 qglVertexAttrib4f(GLSLATTRIB_TEXCOORD0 + j, s[0], s[1], s[2], s[3]);
2969                                                                         else if (gl_state.units[j].pointer_texcoord_components == 3)
2970                                                                                 qglVertexAttrib3f(GLSLATTRIB_TEXCOORD0 + j, s[0], s[1], s[2]);
2971                                                                         else if (gl_state.units[j].pointer_texcoord_components == 2)
2972                                                                                 qglVertexAttrib2f(GLSLATTRIB_TEXCOORD0 + j, s[0], s[1]);
2973                                                                         else if (gl_state.units[j].pointer_texcoord_components == 1)
2974                                                                                 qglVertexAttrib1f(GLSLATTRIB_TEXCOORD0 + j, s[0]);
2975                                                                 }
2976                                                                 else if (gl_state.units[j].pointer_texcoord_gltype == GL_BYTE)
2977                                                                 {
2978                                                                         const GLbyte *sb = (const GLbyte *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2979                                                                         if (gl_state.units[j].pointer_texcoord_components == 4)
2980                                                                                 qglVertexAttrib4f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 127.0f), sb[1] * (1.0f / 127.0f), sb[2] * (1.0f / 127.0f), sb[3] * (1.0f / 127.0f));
2981                                                                         else if (gl_state.units[j].pointer_texcoord_components == 3)
2982                                                                                 qglVertexAttrib3f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 127.0f), sb[1] * (1.0f / 127.0f), sb[2] * (1.0f / 127.0f));
2983                                                                         else if (gl_state.units[j].pointer_texcoord_components == 2)
2984                                                                                 qglVertexAttrib2f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 127.0f), sb[1] * (1.0f / 127.0f));
2985                                                                         else if (gl_state.units[j].pointer_texcoord_components == 1)
2986                                                                                 qglVertexAttrib1f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 127.0f));
2987                                                                 }
2988                                                                 else if (gl_state.units[j].pointer_texcoord_gltype == GL_UNSIGNED_BYTE)
2989                                                                 {
2990                                                                         const GLubyte *sb = (const GLubyte *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2991                                                                         if (gl_state.units[j].pointer_texcoord_components == 4)
2992                                                                                 qglVertexAttrib4f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 255.0f), sb[1] * (1.0f / 255.0f), sb[2] * (1.0f / 255.0f), sb[3] * (1.0f / 255.0f));
2993                                                                         else if (gl_state.units[j].pointer_texcoord_components == 3)
2994                                                                                 qglVertexAttrib3f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 255.0f), sb[1] * (1.0f / 255.0f), sb[2] * (1.0f / 255.0f));
2995                                                                         else if (gl_state.units[j].pointer_texcoord_components == 2)
2996                                                                                 qglVertexAttrib2f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 255.0f), sb[1] * (1.0f / 255.0f));
2997                                                                         else if (gl_state.units[j].pointer_texcoord_components == 1)
2998                                                                                 qglVertexAttrib1f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 255.0f));
2999                                                                 }
3000                                                                 else if (gl_state.units[j].pointer_texcoord_gltype == (int)(GL_UNSIGNED_BYTE | 0x80000000))
3001                                                                 {
3002                                                                         const GLubyte *sb = (const GLubyte *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
3003                                                                         if (gl_state.units[j].pointer_texcoord_components == 4)
3004                                                                                 qglVertexAttrib4f(GLSLATTRIB_TEXCOORD0 + j, sb[0], sb[1], sb[2], sb[3]);
3005                                                                         else if (gl_state.units[j].pointer_texcoord_components == 3)
3006                                                                                 qglVertexAttrib3f(GLSLATTRIB_TEXCOORD0 + j, sb[0], sb[1], sb[2]);
3007                                                                         else if (gl_state.units[j].pointer_texcoord_components == 2)
3008                                                                                 qglVertexAttrib2f(GLSLATTRIB_TEXCOORD0 + j, sb[0], sb[1]);
3009                                                                         else if (gl_state.units[j].pointer_texcoord_components == 1)
3010                                                                                 qglVertexAttrib1f(GLSLATTRIB_TEXCOORD0 + j, sb[0]);
3011                                                                 }
3012                                                         }
3013                                                 }
3014                                                 if (gl_state.pointer_color_pointer && gl_state.pointer_color_enabled && gl_state.pointer_color_components == 4)
3015                                                 {
3016                                                         if (gl_state.pointer_color_gltype == GL_FLOAT)
3017                                                         {
3018                                                                 p = (const GLfloat *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
3019                                                                 qglVertexAttrib4f(GLSLATTRIB_COLOR, p[0], p[1], p[2], p[3]);
3020                                                         }
3021                                                         else if (gl_state.pointer_color_gltype == GL_UNSIGNED_BYTE)
3022                                                         {
3023                                                                 const GLubyte *ub = (const GLubyte *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
3024                                                                 qglVertexAttrib4Nub(GLSLATTRIB_COLOR, ub[0], ub[1], ub[2], ub[3]);
3025                                                         }
3026                                                 }
3027                                                 if (gl_state.pointer_vertex_gltype == GL_FLOAT)
3028                                                 {
3029                                                         p = (const GLfloat *)((const unsigned char *)gl_state.pointer_vertex_pointer + element * gl_state.pointer_vertex_stride);
3030                                                         if (gl_state.pointer_vertex_components == 4)
3031                                                                 qglVertexAttrib4f(GLSLATTRIB_POSITION, p[0], p[1], p[2], p[3]);
3032                                                         else if (gl_state.pointer_vertex_components == 3)
3033                                                                 qglVertexAttrib3f(GLSLATTRIB_POSITION, p[0], p[1], p[2]);
3034                                                         else
3035                                                                 qglVertexAttrib2f(GLSLATTRIB_POSITION, p[0], p[1]);
3036                                                 }
3037                                         }
3038                                 }
3039                                 else
3040                                 {
3041                                         for (i = 0;i < (unsigned int) numtriangles * 3;i++)
3042                                         {
3043                                                 if (element3i)
3044                                                         element = element3i[i];
3045                                                 else if (element3s)
3046                                                         element = element3s[i];
3047                                                 else
3048                                                         element = firstvertex + i;
3049                                                 for (j = 0;j < vid.texarrayunits;j++)
3050                                                 {
3051                                                         if (gl_state.units[j].pointer_texcoord_pointer && gl_state.units[j].arrayenabled)
3052                                                         {
3053                                                                 if (gl_state.units[j].pointer_texcoord_gltype == GL_FLOAT)
3054                                                                 {
3055                                                                         p = (const GLfloat *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
3056                                                                         if (vid.texarrayunits > 1)
3057                                                                         {
3058                                                                                 if (gl_state.units[j].pointer_texcoord_components == 4)
3059                                                                                         qglMultiTexCoord4f(GL_TEXTURE0 + j, p[0], p[1], p[2], p[3]);
3060                                                                                 else if (gl_state.units[j].pointer_texcoord_components == 3)
3061                                                                                         qglMultiTexCoord3f(GL_TEXTURE0 + j, p[0], p[1], p[2]);
3062                                                                                 else if (gl_state.units[j].pointer_texcoord_components == 2)
3063                                         &nbs