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