]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - gl_backend.c
8415c146e22d8d5895574f5ccb5c709d96ea6a27
[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                                                                                         qglMultiTexCoord3f(GL_TEXTURE0 + j, s[0], s[1], s[2]);
3039                                                                                 else if (gl_state.units[j].pointer_texcoord_components == 2)
3040                                                                                         qglMultiTexCoord2f(GL_TEXTURE0 + j, s[0], s[1]);
3041                                                                                 else if (gl_state.units[j].pointer_texcoord_components == 1)
3042                                                                                         qglMultiTexCoord1f(GL_TEXTURE0 + j, s[0]);
3043                                                                         }
3044                                                                         else
3045                                                                         {
3046                                                                                 if (gl_state.units[j].pointer_texcoord_components == 4)
3047                                                                                         qglTexCoord4f(s[0], s[1], s[2], s[3]);
3048                                                                                 else if (gl_state.units[j].pointer_texcoord_components == 3)
3049                                                                                         qglTexCoord3f(s[0], s[1], s[2]);
3050                                                                                 else if (gl_state.units[j].pointer_texcoord_components == 2)
3051                                                                                         qglTexCoord2f(s[0], s[1]);
3052                                                                                 else if (gl_state.units[j].pointer_texcoord_components == 1)
3053                                                                                         qglTexCoord1f(s[0]);
3054                                                                         }
3055                                                                 }
3056                                                                 else if (gl_state.units[j].pointer_texcoord_gltype == GL_BYTE)
3057                                                                 {
3058                                                                         const GLbyte *sb = (const GLbyte *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
3059                                                                         if (vid.texarrayunits > 1)
3060                                                                         {
3061                                                                                 if (gl_state.units[j].pointer_texcoord_components == 4)
3062                                                                                         qglMultiTexCoord4f(GL_TEXTURE0 + j, sb[0], sb[1], sb[2], sb[3]);
3063                                                                                 else if (gl_state.units[j].pointer_texcoord_components == 3)
3064                                                                                         qglMultiTexCoord3f(GL_TEXTURE0 + j, sb[0], sb[1], sb[2]);
3065                                                                                 else if (gl_state.units[j].pointer_texcoord_components == 2)
3066                                                                                         qglMultiTexCoord2f(GL_TEXTURE0 + j, sb[0], sb[1]);
3067                                                                                 else if (gl_state.units[j].pointer_texcoord_components == 1)
3068                                                                                         qglMultiTexCoord1f(GL_TEXTURE0 + j, sb[0]);
3069                                                                         }
3070                                                                         else
3071                                                                         {
3072                                                                                 if (gl_state.units[j].pointer_texcoord_components == 4)
3073                                                                                         qglTexCoord4f(sb[0], sb[1], sb[2], sb[3]);
3074                                                                                 else if (gl_state.units[j].pointer_texcoord_components == 3)
3075                                                                                         qglTexCoord3f(sb[0], sb[1], sb[2]);
3076                                                                                 else if (gl_state.units[j].pointer_texcoord_components == 2)
3077                                                                                         qglTexCoord2f(sb[0], sb[1]);
3078                                                                                 else if (gl_state.units[j].pointer_texcoord_components == 1)
3079                                                                                         qglTexCoord1f(sb[0]);
3080                                                                         }
3081                                                                 }
3082                                                         }
3083                                                 }
3084                                                 if (gl_state.pointer_color_pointer && gl_state.pointer_color_enabled && gl_state.pointer_color_components == 4)
3085                                                 {
3086                                                         if (gl_state.pointer_color_gltype == GL_FLOAT)
3087                                                         {
3088                                                                 p = (const GLfloat *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
3089                                                                 qglColor4f(p[0], p[1], p[2], p[3]);
3090                                                         }
3091                                                         else if (gl_state.pointer_color_gltype == GL_UNSIGNED_BYTE)
3092                                                         {
3093                                                                 const GLubyte *ub = (const GLubyte *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
3094                                                                 qglColor4ub(ub[0], ub[1], ub[2], ub[3]);
3095                                                         }
3096                                                 }
3097                                                 if (gl_state.pointer_vertex_gltype == GL_FLOAT)
3098                                                 {
3099                                                         p = (const GLfloat *)((const unsigned char *)gl_state.pointer_vertex_pointer + element * gl_state.pointer_vertex_stride);
3100                                                         if (gl_state.pointer_vertex_components == 4)
3101                                                                 qglVertex4f(p[0], p[1], p[2], p[3]);
3102                                                         else if (gl_state.pointer_vertex_components == 3)
3103                                                                 qglVertex3f(p[0], p[1], p[2]);
3104                                                         else
3105                                                                 qglVertex2f(p[0], p[1]);
3106                                                 }
3107                                         }
3108                                 }
3109                                 qglEnd();
3110                                 CHECKGLERROR
3111 #endif
3112                         }
3113                         else if (bufferobject3s)
3114                         {
3115                                 GL_BindEBO(bufferobject3s);
3116 #ifndef USE_GLES2
3117                                 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
3118                                 {
3119                                         qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, (void *)bufferoffset3s);
3120                                         CHECKGLERROR
3121                                 }
3122                                 else
3123 #endif
3124                                 {
3125                                         qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
3126                                         CHECKGLERROR
3127                                 }
3128                         }
3129                         else if (bufferobject3i)
3130                         {
3131                                 GL_BindEBO(bufferobject3i);
3132 #ifndef USE_GLES2
3133                                 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
3134                                 {
3135                                         qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, (void *)bufferoffset3i);
3136                                         CHECKGLERROR
3137                                 }
3138                                 else
3139 #endif
3140                                 {
3141                                         qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
3142                                         CHECKGLERROR
3143                                 }
3144                         }
3145                         else if (element3s)
3146                         {
3147                                 GL_BindEBO(0);
3148 #ifndef USE_GLES2
3149                                 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
3150                                 {
3151                                         qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, element3s);
3152                                         CHECKGLERROR
3153                                 }
3154                                 else
3155 #endif
3156                                 {
3157                                         qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
3158                                         CHECKGLERROR
3159                                 }
3160                         }
3161                         else if (element3i)
3162                         {
3163                                 GL_BindEBO(0);
3164 #ifndef USE_GLES2
3165                                 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
3166                                 {
3167                                         qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, element3i);
3168                                         CHECKGLERROR
3169                                 }
3170                                 else
3171 #endif
3172                                 {
3173                                         qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
3174                                         CHECKGLERROR
3175                                 }
3176                         }
3177                         else
3178                         {
3179                                 qglDrawArrays(GL_TRIANGLES, firstvertex, numvertices);
3180                                 CHECKGLERROR
3181                         }
3182                         break;
3183                 case RENDERPATH_D3D9:
3184 #ifdef SUPPORTD3D
3185                         if (gl_state.d3dvertexbuffer && ((element3s && element3s_indexbuffer) || (element3i && element3i_indexbuffer)))
3186                         {
3187                                 if (element3s_indexbuffer)
3188                                 {
3189                                         IDirect3DDevice9_SetIndices(vid_d3d9dev, (IDirect3DIndexBuffer9 *)element3s_indexbuffer->devicebuffer);
3190                                         IDirect3DDevice9_DrawIndexedPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, 0, firstvertex, numvertices, element3s_bufferoffset>>1, numtriangles);
3191                                 }
3192                                 else if (element3i_indexbuffer)
3193                                 {
3194                                         IDirect3DDevice9_SetIndices(vid_d3d9dev, (IDirect3DIndexBuffer9 *)element3i_indexbuffer->devicebuffer);
3195                                         IDirect3DDevice9_DrawIndexedPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, 0, firstvertex, numvertices, element3i_bufferoffset>>2, numtriangles);
3196                                 }
3197                                 else
3198                                         IDirect3DDevice9_DrawPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, firstvertex, numvertices);
3199                         }
3200                         else
3201                         {
3202                                 if (element3s)
3203                                         IDirect3DDevice9_DrawIndexedPrimitiveUP(vid_d3d9dev, D3DPT_TRIANGLELIST, firstvertex, numvertices, numtriangles, element3s, D3DFMT_INDEX16, gl_state.d3dvertexdata, gl_state.d3dvertexsize);
3204                                 else if (element3i)
3205                                         IDirect3DDevice9_DrawIndexedPrimitiveUP(vid_d3d9dev, D3DPT_TRIANGLELIST, firstvertex, numvertices, numtriangles, element3i, D3DFMT_INDEX32, gl_state.d3dvertexdata, gl_state.d3dvertexsize);
3206                                 else
3207                                         IDirect3DDevice9_DrawPrimitiveUP(vid_d3d9dev, D3DPT_TRIANGLELIST, numvertices, (void *)gl_state.d3dvertexdata, gl_state.d3dvertexsize);
3208                         }
3209 #endif
3210                         break;
3211                 case RENDERPATH_D3D10:
3212                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3213                         break;
3214                 case RENDERPATH_D3D11:
3215                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3216                         break;
3217                 case RENDERPATH_SOFT:
3218                         DPSOFTRAST_DrawTriangles(firstvertex, numvertices, numtriangles, element3i, element3s);
3219                         break;
3220                 case RENDERPATH_GLES1:
3221                 case RENDERPATH_GLES2:
3222                         // GLES does not have glDrawRangeElements, and generally
3223                         // underperforms with index buffers, so this code path is
3224                         // relatively straightforward...
3225 #if 0
3226                         if (gl_paranoid.integer)
3227                         {
3228                                 int r, prog, enabled, i;
3229                                 GLsizei         attriblength;
3230                                 GLint           attribsize;
3231                                 GLenum          attribtype;
3232                                 GLchar          attribname[1024];
3233                                 r = qglCheckFramebufferStatusEXT(GL_FRAMEBUFFER);CHECKGLERROR
3234                                 if (r != GL_FRAMEBUFFER_COMPLETE)
3235                                         Con_DPrintf("fbo %i not complete (default %i)\n", gl_state.framebufferobject, gl_state.defaultframebufferobject);
3236 #ifndef GL_CURRENT_PROGRAM
3237 #define GL_CURRENT_PROGRAM 0x8B8D
3238 #endif
3239                                 qglGetIntegerv(GL_CURRENT_PROGRAM, &r);CHECKGLERROR
3240                                 if (r < 0 || r > 10000)
3241                                         Con_DPrintf("GL_CURRENT_PROGRAM = %i\n", r);
3242                                 prog = r;
3243                                 for (i = 0;i < 8;i++)
3244                                 {
3245                                         qglGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &r);CHECKGLERROR
3246                                         if (!r)
3247                                                 continue;
3248                                         qglGetActiveAttrib(prog, i, sizeof(attribname), &attriblength, &attribsize, &attribtype, attribname);CHECKGLERROR
3249                                         Con_DPrintf("prog %i position %i length %i size %04X type %i name \"%s\"\n", prog, i, (int)attriblength, (int)attribsize, (int)attribtype, (char *)attribname);
3250                                 }
3251                         }
3252 #endif
3253                         if (element3s)
3254                         {
3255                                 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
3256                                 CHECKGLERROR
3257                         }
3258                         else if (element3i)
3259                         {
3260                                 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
3261                                 CHECKGLERROR
3262                         }
3263                         else
3264                         {
3265                                 qglDrawArrays(GL_TRIANGLES, firstvertex, numvertices);
3266                                 CHECKGLERROR
3267                         }
3268                         break;
3269                 }
3270         }
3271 }
3272
3273 // restores backend state, used when done with 3D rendering
3274 void R_Mesh_Finish(void)
3275 {
3276         R_Mesh_SetRenderTargets(0, NULL, NULL, NULL, NULL, NULL);
3277 }
3278
3279 r_meshbuffer_t *R_Mesh_CreateMeshBuffer(const void *data, size_t size, const char *name, qboolean isindexbuffer, qboolean isdynamic, qboolean isindex16)
3280 {
3281         r_meshbuffer_t *buffer;
3282         if (!(isdynamic ? (isindexbuffer ? gl_state.usevbo_dynamicindex : gl_state.usevbo_dynamicvertex) : (isindexbuffer ? gl_state.usevbo_staticindex : gl_state.usevbo_staticvertex)))
3283                 return NULL;
3284         buffer = (r_meshbuffer_t *)Mem_ExpandableArray_AllocRecord(&gl_state.meshbufferarray);
3285         memset(buffer, 0, sizeof(*buffer));
3286         buffer->bufferobject = 0;
3287         buffer->devicebuffer = NULL;
3288         buffer->size = 0;
3289         buffer->isindexbuffer = isindexbuffer;
3290         buffer->isdynamic = isdynamic;
3291         buffer->isindex16 = isindex16;
3292         strlcpy(buffer->name, name, sizeof(buffer->name));
3293         R_Mesh_UpdateMeshBuffer(buffer, data, size);
3294         return buffer;
3295 }
3296
3297 void R_Mesh_UpdateMeshBuffer(r_meshbuffer_t *buffer, const void *data, size_t size)
3298 {
3299         if (!buffer)
3300                 return;
3301         if (buffer->isindexbuffer)
3302         {
3303                 r_refdef.stats.indexbufferuploadcount++;
3304                 r_refdef.stats.indexbufferuploadsize += size;
3305         }
3306         else
3307         {
3308                 r_refdef.stats.vertexbufferuploadcount++;
3309                 r_refdef.stats.vertexbufferuploadsize += size;
3310         }
3311         switch(vid.renderpath)
3312         {
3313         case RENDERPATH_GL11:
3314         case RENDERPATH_GL13:
3315         case RENDERPATH_GL20:
3316         case RENDERPATH_GLES1:
3317         case RENDERPATH_GLES2:
3318                 if (!buffer->bufferobject)
3319                         qglGenBuffersARB(1, (GLuint *)&buffer->bufferobject);
3320                 if (buffer->isindexbuffer)
3321                         GL_BindEBO(buffer->bufferobject);
3322                 else
3323                         GL_BindVBO(buffer->bufferobject);
3324                 qglBufferDataARB(buffer->isindexbuffer ? GL_ELEMENT_ARRAY_BUFFER : GL_ARRAY_BUFFER, size, data, buffer->isdynamic ? GL_STREAM_DRAW : GL_STATIC_DRAW);
3325                 break;
3326         case RENDERPATH_D3D9:
3327 #ifdef SUPPORTD3D
3328                 {
3329                         int result;
3330                         void *datapointer = NULL;
3331                         if (buffer->isindexbuffer)
3332                         {
3333                                 IDirect3DIndexBuffer9 *d3d9indexbuffer = (IDirect3DIndexBuffer9 *)buffer->devicebuffer;
3334                                 if (size > buffer->size || !buffer->devicebuffer)
3335                                 {
3336                                         if (buffer->devicebuffer)
3337                                                 IDirect3DIndexBuffer9_Release((IDirect3DIndexBuffer9*)buffer->devicebuffer);
3338                                         buffer->devicebuffer = NULL;
3339                                         if (FAILED(result = IDirect3DDevice9_CreateIndexBuffer(vid_d3d9dev, size, buffer->isdynamic ? D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC : 0, buffer->isindex16 ? D3DFMT_INDEX16 : D3DFMT_INDEX32, buffer->isdynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED, &d3d9indexbuffer, NULL)))
3340                                                 Sys_Error("IDirect3DDevice9_CreateIndexBuffer(%p, %d, %x, %x, %x, %p, NULL) returned %x\n", vid_d3d9dev, (int)size, buffer->isdynamic ? (int)D3DUSAGE_DYNAMIC : 0, buffer->isindex16 ? (int)D3DFMT_INDEX16 : (int)D3DFMT_INDEX32, buffer->isdynamic ? (int)D3DPOOL_DEFAULT : (int)D3DPOOL_MANAGED, &d3d9indexbuffer, (int)result);
3341                                         buffer->devicebuffer = (void *)d3d9indexbuffer;
3342                                         buffer->size = size;
3343                                 }
3344                                 if (!FAILED(IDirect3DIndexBuffer9_Lock(d3d9indexbuffer, 0, 0, &datapointer, buffer->isdynamic ? D3DLOCK_DISCARD : 0)))
3345                                 {
3346                                         if (data)
3347                                                 memcpy(datapointer, data, size);
3348                                         else
3349                                                 memset(datapointer, 0, size);
3350                                         IDirect3DIndexBuffer9_Unlock(d3d9indexbuffer);
3351                                 }
3352                         }
3353                         else
3354                         {
3355                                 IDirect3DVertexBuffer9 *d3d9vertexbuffer = (IDirect3DVertexBuffer9 *)buffer->devicebuffer;
3356                                 if (size > buffer->size || !buffer->devicebuffer)
3357                                 {
3358                                         if (buffer->devicebuffer)
3359                                                 IDirect3DVertexBuffer9_Release((IDirect3DVertexBuffer9*)buffer->devicebuffer);
3360                                         buffer->devicebuffer = NULL;
3361                                         if (FAILED(result = IDirect3DDevice9_CreateVertexBuffer(vid_d3d9dev, size, buffer->isdynamic ? D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC : 0, 0, buffer->isdynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED, &d3d9vertexbuffer, NULL)))
3362                                                 Sys_Error("IDirect3DDevice9_CreateVertexBuffer(%p, %d, %x, %x, %x, %p, NULL) returned %x\n", vid_d3d9dev, (int)size, buffer->isdynamic ? (int)D3DUSAGE_DYNAMIC : 0, 0, buffer->isdynamic ? (int)D3DPOOL_DEFAULT : (int)D3DPOOL_MANAGED, &d3d9vertexbuffer, (int)result);
3363                                         buffer->devicebuffer = (void *)d3d9vertexbuffer;
3364                                         buffer->size = size;
3365                                 }
3366                                 if (!FAILED(IDirect3DVertexBuffer9_Lock(d3d9vertexbuffer, 0, 0, &datapointer, buffer->isdynamic ? D3DLOCK_DISCARD : 0)))
3367                                 {
3368                                         if (data)
3369                                                 memcpy(datapointer, data, size);
3370                                         else
3371                                                 memset(datapointer, 0, size);
3372                                         IDirect3DVertexBuffer9_Unlock(d3d9vertexbuffer);
3373                                 }
3374                         }
3375                 }
3376 #endif
3377                 break;
3378         case RENDERPATH_D3D10:
3379                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3380                 break;
3381         case RENDERPATH_D3D11:
3382                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3383                 break;
3384         case RENDERPATH_SOFT:
3385                 break;
3386         }
3387 }
3388
3389 void R_Mesh_DestroyMeshBuffer(r_meshbuffer_t *buffer)
3390 {
3391         if (!buffer)
3392                 return;
3393         switch(vid.renderpath)
3394         {
3395         case RENDERPATH_GL11:
3396         case RENDERPATH_GL13:
3397         case RENDERPATH_GL20:
3398         case RENDERPATH_GLES1:
3399         case RENDERPATH_GLES2:
3400                 qglDeleteBuffersARB(1, (GLuint *)&buffer->bufferobject);
3401                 break;
3402         case RENDERPATH_D3D9:
3403 #ifdef SUPPORTD3D
3404                 if (gl_state.d3dvertexbuffer == (void *)buffer)
3405                         gl_state.d3dvertexbuffer = NULL;
3406                 if (buffer->devicebuffer)
3407                 {
3408                         if (buffer->isindexbuffer)
3409                                 IDirect3DIndexBuffer9_Release((IDirect3DIndexBuffer9 *)buffer->devicebuffer);
3410                         else
3411                                 IDirect3DVertexBuffer9_Release((IDirect3DVertexBuffer9 *)buffer->devicebuffer);
3412                         buffer->devicebuffer = NULL;
3413                 }
3414 #endif
3415                 break;
3416         case RENDERPATH_D3D10:
3417                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3418                 break;
3419         case RENDERPATH_D3D11:
3420                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3421                 break;
3422         case RENDERPATH_SOFT:
3423                 break;
3424         }
3425         Mem_ExpandableArray_FreeRecord(&gl_state.meshbufferarray, (void *)buffer);
3426 }
3427
3428 void GL_Mesh_ListVBOs(qboolean printeach)
3429 {
3430         int i, endindex;
3431         size_t ebocount = 0, ebomemory = 0;
3432         size_t vbocount = 0, vbomemory = 0;
3433         r_meshbuffer_t *buffer;
3434         endindex = Mem_ExpandableArray_IndexRange(&gl_state.meshbufferarray);
3435         for (i = 0;i < endindex;i++)
3436         {
3437                 buffer = (r_meshbuffer_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.meshbufferarray, i);
3438                 if (!buffer)
3439                         continue;
3440                 if (buffer->isindexbuffer) {ebocount++;ebomemory += buffer->size;if (printeach) Con_Printf("indexbuffer #%i %s = %i bytes%s\n", i, buffer->name, (int)buffer->size, buffer->isdynamic ? " (dynamic)" : " (static)");}
3441                 else                       {vbocount++;vbomemory += buffer->size;if (printeach) Con_Printf("vertexbuffer #%i %s = %i bytes%s\n", i, buffer->name, (int)buffer->size, buffer->isdynamic ? " (dynamic)" : " (static)");}
3442         }
3443         Con_Printf("vertex buffers: %i indexbuffers totalling %i bytes (%.3f MB), %i vertexbuffers totalling %i bytes (%.3f MB), combined %i bytes (%.3fMB)\n", (int)ebocount, (int)ebomemory, ebomemory / 1048576.0, (int)vbocount, (int)vbomemory, vbomemory / 1048576.0, (int)(ebomemory + vbomemory), (ebomemory + vbomemory) / 1048576.0);
3444 }
3445
3446
3447
3448 void R_Mesh_VertexPointer(int components, int gltype, size_t stride, const void *pointer, const r_meshbuffer_t *vertexbuffer, size_t bufferoffset)
3449 {
3450         switch(vid.renderpath)
3451         {
3452         case RENDERPATH_GL11:
3453         case RENDERPATH_GL13:
3454         case RENDERPATH_GLES1:
3455                 if (gl_state.pointer_vertex_components != components || gl_state.pointer_vertex_gltype != gltype || gl_state.pointer_vertex_stride != stride || gl_state.pointer_vertex_pointer != pointer || gl_state.pointer_vertex_vertexbuffer != vertexbuffer || gl_state.pointer_vertex_offset != bufferoffset)
3456                 {
3457                         int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3458                         gl_state.pointer_vertex_components = components;
3459                         gl_state.pointer_vertex_gltype = gltype;
3460                         gl_state.pointer_vertex_stride = stride;
3461                         gl_state.pointer_vertex_pointer = pointer;
3462                         gl_state.pointer_vertex_vertexbuffer = vertexbuffer;
3463                         gl_state.pointer_vertex_offset = bufferoffset;
3464                         CHECKGLERROR
3465                         GL_BindVBO(bufferobject);
3466                         qglVertexPointer(components, gltype, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3467                 }
3468                 break;
3469         case RENDERPATH_GL20:
3470         case RENDERPATH_GLES2:
3471                 if (gl_state.pointer_vertex_components != components || gl_state.pointer_vertex_gltype != gltype || gl_state.pointer_vertex_stride != stride || gl_state.pointer_vertex_pointer != pointer || gl_state.pointer_vertex_vertexbuffer != vertexbuffer || gl_state.pointer_vertex_offset != bufferoffset)
3472                 {
3473                         int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3474                         gl_state.pointer_vertex_components = components;
3475                         gl_state.pointer_vertex_gltype = gltype;
3476                         gl_state.pointer_vertex_stride = stride;
3477                         gl_state.pointer_vertex_pointer = pointer;
3478                         gl_state.pointer_vertex_vertexbuffer = vertexbuffer;
3479                         gl_state.pointer_vertex_offset = bufferoffset;
3480                         CHECKGLERROR
3481                         GL_BindVBO(bufferobject);
3482                         // LordHavoc: special flag added to gltype for unnormalized types
3483                         qglVertexAttribPointer(GLSLATTRIB_POSITION, components, gltype & ~0x80000000, (gltype & 0x80000000) == 0, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3484                 }
3485                 break;
3486         case RENDERPATH_D3D9:
3487         case RENDERPATH_D3D10:
3488         case RENDERPATH_D3D11:
3489         case RENDERPATH_SOFT:
3490                 break;
3491         }
3492 }
3493
3494 void R_Mesh_ColorPointer(int components, int gltype, size_t stride, const void *pointer, const r_meshbuffer_t *vertexbuffer, size_t bufferoffset)
3495 {
3496         // note: vertexbuffer may be non-NULL even if pointer is NULL, so check
3497         // the pointer only.
3498         switch(vid.renderpath)
3499         {
3500         case RENDERPATH_GL11:
3501         case RENDERPATH_GL13:
3502         case RENDERPATH_GLES1:
3503 #ifdef GL_MODELVIEW
3504                 CHECKGLERROR
3505                 if (pointer)
3506                 {
3507                         // caller wants color array enabled
3508                         int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3509                         if (!gl_state.pointer_color_enabled)
3510                         {
3511                                 gl_state.pointer_color_enabled = true;
3512                                 CHECKGLERROR
3513                                 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
3514                         }
3515                         if (gl_state.pointer_color_components != components || gl_state.pointer_color_gltype != gltype || gl_state.pointer_color_stride != stride || gl_state.pointer_color_pointer != pointer || gl_state.pointer_color_vertexbuffer != vertexbuffer || gl_state.pointer_color_offset != bufferoffset)
3516                         {
3517                                 gl_state.pointer_color_components = components;
3518                                 gl_state.pointer_color_gltype = gltype;
3519                                 gl_state.pointer_color_stride = stride;
3520                                 gl_state.pointer_color_pointer = pointer;
3521                                 gl_state.pointer_color_vertexbuffer = vertexbuffer;
3522                                 gl_state.pointer_color_offset = bufferoffset;
3523                                 CHECKGLERROR
3524                                 GL_BindVBO(bufferobject);
3525                                 qglColorPointer(components, gltype, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3526                         }
3527                 }
3528                 else
3529                 {
3530                         // caller wants color array disabled
3531                         if (gl_state.pointer_color_enabled)
3532                         {
3533                                 gl_state.pointer_color_enabled = false;
3534                                 CHECKGLERROR
3535                                 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
3536                                 // when color array is on the glColor gets trashed, set it again
3537                                 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
3538                         }
3539                 }
3540 #endif
3541                 break;
3542         case RENDERPATH_GL20:
3543         case RENDERPATH_GLES2:
3544                 CHECKGLERROR
3545                 if (pointer)
3546                 {
3547                         // caller wants color array enabled
3548                         int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3549                         if (!gl_state.pointer_color_enabled)
3550                         {
3551                                 gl_state.pointer_color_enabled = true;
3552                                 CHECKGLERROR
3553                                 qglEnableVertexAttribArray(GLSLATTRIB_COLOR);CHECKGLERROR
3554                         }
3555                         if (gl_state.pointer_color_components != components || gl_state.pointer_color_gltype != gltype || gl_state.pointer_color_stride != stride || gl_state.pointer_color_pointer != pointer || gl_state.pointer_color_vertexbuffer != vertexbuffer || gl_state.pointer_color_offset != bufferoffset)
3556                         {
3557                                 gl_state.pointer_color_components = components;
3558                                 gl_state.pointer_color_gltype = gltype;
3559                                 gl_state.pointer_color_stride = stride;
3560                                 gl_state.pointer_color_pointer = pointer;
3561                                 gl_state.pointer_color_vertexbuffer = vertexbuffer;
3562                                 gl_state.pointer_color_offset = bufferoffset;
3563                                 CHECKGLERROR
3564                                 GL_BindVBO(bufferobject);
3565                                 // LordHavoc: special flag added to gltype for unnormalized types
3566                                 qglVertexAttribPointer(GLSLATTRIB_COLOR, components, gltype & ~0x80000000, (gltype & 0x80000000) == 0, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3567                         }
3568                 }
3569                 else
3570                 {
3571                         // caller wants color array disabled
3572                         if (gl_state.pointer_color_enabled)
3573                         {
3574                                 gl_state.pointer_color_enabled = false;
3575                                 CHECKGLERROR
3576                                 qglDisableVertexAttribArray(GLSLATTRIB_COLOR);CHECKGLERROR
3577                                 // when color array is on the glColor gets trashed, set it again
3578                                 qglVertexAttrib4f(GLSLATTRIB_COLOR, gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
3579                         }
3580                 }
3581                 break;
3582         case RENDERPATH_D3D9:
3583         case RENDERPATH_D3D10:
3584         case RENDERPATH_D3D11:
3585         case RENDERPATH_SOFT:
3586                 break;
3587         }
3588 }
3589
3590 void R_Mesh_TexCoordPointer(unsigned int unitnum, int components, int gltype, size_t stride, const void *pointer, const r_meshbuffer_t *vertexbuffer, size_t bufferoffset)
3591 {
3592         gltextureunit_t *unit = gl_state.units + unitnum;
3593         // update array settings
3594         // note: there is no need to check bufferobject here because all cases
3595         // that involve a valid bufferobject also supply a texcoord array
3596         switch(vid.renderpath)
3597         {
3598         case RENDERPATH_GL11:
3599         case RENDERPATH_GL13:
3600         case RENDERPATH_GLES1:
3601 #ifdef GL_MODELVIEW
3602                 CHECKGLERROR
3603                 if (pointer)
3604                 {
3605                         int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3606                         // texture array unit is enabled, enable the array
3607                         if (!unit->arrayenabled)
3608                         {
3609                                 unit->arrayenabled = true;
3610                                 GL_ClientActiveTexture(unitnum);
3611                                 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
3612                         }
3613                         // texcoord array
3614                         if (unit->pointer_texcoord_components != components || unit->pointer_texcoord_gltype != gltype || unit->pointer_texcoord_stride != stride || unit->pointer_texcoord_pointer != pointer || unit->pointer_texcoord_vertexbuffer != vertexbuffer || unit->pointer_texcoord_offset != bufferoffset)
3615                         {
3616                                 unit->pointer_texcoord_components = components;
3617                                 unit->pointer_texcoord_gltype = gltype;
3618                                 unit->pointer_texcoord_stride = stride;
3619                                 unit->pointer_texcoord_pointer = pointer;
3620                                 unit->pointer_texcoord_vertexbuffer = vertexbuffer;
3621                                 unit->pointer_texcoord_offset = bufferoffset;
3622                                 GL_ClientActiveTexture(unitnum);
3623                                 GL_BindVBO(bufferobject);
3624                                 qglTexCoordPointer(components, gltype, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3625                         }
3626                 }
3627                 else
3628                 {
3629                         // texture array unit is disabled, disable the array
3630                         if (unit->arrayenabled)
3631                         {
3632                                 unit->arrayenabled = false;
3633                                 GL_ClientActiveTexture(unitnum);
3634                                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
3635                         }
3636                 }
3637 #endif
3638                 break;
3639         case RENDERPATH_GL20:
3640         case RENDERPATH_GLES2:
3641                 CHECKGLERROR
3642                 if (pointer)
3643                 {
3644                         int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3645                         // texture array unit is enabled, enable the array
3646                         if (!unit->arrayenabled)
3647                         {
3648                                 unit->arrayenabled = true;
3649                                 qglEnableVertexAttribArray(unitnum+GLSLATTRIB_TEXCOORD0);CHECKGLERROR
3650                         }
3651                         // texcoord array
3652                         if (unit->pointer_texcoord_components != components || unit->pointer_texcoord_gltype != gltype || unit->pointer_texcoord_stride != stride || unit->pointer_texcoord_pointer != pointer || unit->pointer_texcoord_vertexbuffer != vertexbuffer || unit->pointer_texcoord_offset != bufferoffset)
3653                         {
3654                                 unit->pointer_texcoord_components = components;
3655                                 unit->pointer_texcoord_gltype = gltype;
3656                                 unit->pointer_texcoord_stride = stride;
3657                                 unit->pointer_texcoord_pointer = pointer;
3658                                 unit->pointer_texcoord_vertexbuffer = vertexbuffer;
3659                                 unit->pointer_texcoord_offset = bufferoffset;
3660                                 GL_BindVBO(bufferobject);
3661                                 // LordHavoc: special flag added to gltype for unnormalized types
3662                                 qglVertexAttribPointer(unitnum+GLSLATTRIB_TEXCOORD0, components, gltype & ~0x80000000, (gltype & 0x80000000) == 0, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3663                         }
3664                 }
3665                 else
3666                 {
3667                         // texture array unit is disabled, disable the array
3668                         if (unit->arrayenabled)
3669                         {
3670                                 unit->arrayenabled = false;
3671                                 qglDisableVertexAttribArray(unitnum+GLSLATTRIB_TEXCOORD0);CHECKGLERROR
3672                         }
3673                 }
3674                 break;
3675         case RENDERPATH_D3D9:
3676         case RENDERPATH_D3D10:
3677         case RENDERPATH_D3D11:
3678         case RENDERPATH_SOFT:
3679                 break;
3680         }
3681 }
3682
3683 int R_Mesh_TexBound(unsigned int unitnum, int id)
3684 {
3685         gltextureunit_t *unit = gl_state.units + unitnum;
3686         if (unitnum >= vid.teximageunits)
3687                 return 0;
3688         if (id == GL_TEXTURE_2D)
3689                 return unit->t2d;
3690         if (id == GL_TEXTURE_3D)
3691                 return unit->t3d;
3692         if (id == GL_TEXTURE_CUBE_MAP)
3693                 return unit->tcubemap;
3694         return 0;
3695 }
3696
3697 void R_Mesh_CopyToTexture(rtexture_t *tex, int tx, int ty, int sx, int sy, int width, int height)
3698 {
3699         switch(vid.renderpath)
3700         {
3701         case RENDERPATH_GL11:
3702         case RENDERPATH_GL13:
3703         case RENDERPATH_GL20:
3704         case RENDERPATH_GLES1:
3705         case RENDERPATH_GLES2:
3706                 R_Mesh_TexBind(0, tex);
3707                 GL_ActiveTexture(0);CHECKGLERROR
3708                 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, tx, ty, sx, sy, width, height);CHECKGLERROR
3709                 break;
3710         case RENDERPATH_D3D9:
3711 #ifdef SUPPORTD3D
3712                 {
3713                         IDirect3DSurface9 *currentsurface = NULL;
3714                         IDirect3DSurface9 *texturesurface = NULL;
3715                         RECT sourcerect;
3716                         RECT destrect;
3717                         sourcerect.left = sx;
3718                         sourcerect.top = sy;
3719                         sourcerect.right = sx + width;
3720                         sourcerect.bottom = sy + height;
3721                         destrect.left = tx;
3722                         destrect.top = ty;
3723                         destrect.right = tx + width;
3724                         destrect.bottom = ty + height;
3725                         if (!FAILED(IDirect3DTexture9_GetSurfaceLevel(((IDirect3DTexture9 *)tex->d3dtexture), 0, &texturesurface)))
3726                         {
3727                                 if (!FAILED(IDirect3DDevice9_GetRenderTarget(vid_d3d9dev, 0, &currentsurface)))
3728                                 {
3729                                         IDirect3DDevice9_StretchRect(vid_d3d9dev, currentsurface, &sourcerect, texturesurface, &destrect, D3DTEXF_NONE);
3730                                         IDirect3DSurface9_Release(currentsurface);
3731                                 }
3732                                 IDirect3DSurface9_Release(texturesurface);
3733                         }
3734                 }
3735 #endif
3736                 break;
3737         case RENDERPATH_D3D10:
3738                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3739                 break;
3740         case RENDERPATH_D3D11:
3741                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3742                 break;
3743         case RENDERPATH_SOFT:
3744                 DPSOFTRAST_CopyRectangleToTexture(tex->texnum, 0, tx, ty, sx, sy, width, height);
3745                 break;
3746         }
3747 }
3748
3749 #ifdef SUPPORTD3D
3750 int d3drswrap[16] = {D3DRS_WRAP0, D3DRS_WRAP1, D3DRS_WRAP2, D3DRS_WRAP3, D3DRS_WRAP4, D3DRS_WRAP5, D3DRS_WRAP6, D3DRS_WRAP7, D3DRS_WRAP8, D3DRS_WRAP9, D3DRS_WRAP10, D3DRS_WRAP11, D3DRS_WRAP12, D3DRS_WRAP13, D3DRS_WRAP14, D3DRS_WRAP15};
3751 #endif
3752
3753 void R_Mesh_ClearBindingsForTexture(int texnum)
3754 {
3755         gltextureunit_t *unit;
3756         unsigned int unitnum;
3757         // this doesn't really unbind the texture, but it does prevent a mistaken "do nothing" behavior on the next time this same texnum is bound on the same unit as the same type (this mainly affects r_shadow_bouncegrid because 3D textures are so rarely used)
3758         for (unitnum = 0;unitnum < vid.teximageunits;unitnum++)
3759         {
3760                 unit = gl_state.units + unitnum;
3761                 if (unit->t2d == texnum)
3762                         unit->t2d = -1;
3763                 if (unit->t3d == texnum)
3764                         unit->t3d = -1;
3765                 if (unit->tcubemap == texnum)
3766                         unit->tcubemap = -1;
3767         }
3768 }
3769
3770 void R_Mesh_TexBind(unsigned int unitnum, rtexture_t *tex)
3771 {
3772         gltextureunit_t *unit = gl_state.units + unitnum;
3773         int tex2d, tex3d, texcubemap, texnum;
3774         if (unitnum >= vid.teximageunits)
3775                 return;
3776 //      if (unit->texture == tex)
3777 //              return;
3778         switch(vid.renderpath)
3779         {
3780         case RENDERPATH_GL20:
3781         case RENDERPATH_GLES2:
3782                 if (!tex)
3783                 {
3784                         tex = r_texture_white;
3785                         // not initialized enough yet...
3786                         if (!tex)
3787                                 return;
3788                 }
3789                 unit->texture = tex;
3790                 texnum = R_GetTexture(tex);
3791                 switch(tex->gltexturetypeenum)
3792                 {
3793                 case GL_TEXTURE_2D: if (unit->t2d != texnum) {GL_ActiveTexture(unitnum);unit->t2d = texnum;qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR}break;
3794                 case GL_TEXTURE_3D: if (unit->t3d != texnum) {GL_ActiveTexture(unitnum);unit->t3d = texnum;qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR}break;
3795                 case GL_TEXTURE_CUBE_MAP: if (unit->tcubemap != texnum) {GL_ActiveTexture(unitnum);unit->tcubemap = texnum;qglBindTexture(GL_TEXTURE_CUBE_MAP, unit->tcubemap);CHECKGLERROR}break;
3796                 }
3797                 break;
3798         case RENDERPATH_GL11:
3799         case RENDERPATH_GL13:
3800         case RENDERPATH_GLES1:
3801                 unit->texture = tex;
3802                 tex2d = 0;
3803                 tex3d = 0;
3804                 texcubemap = 0;
3805                 if (tex)
3806                 {
3807                         texnum = R_GetTexture(tex);
3808                         switch(tex->gltexturetypeenum)
3809                         {
3810                         case GL_TEXTURE_2D:
3811                                 tex2d = texnum;
3812                                 break;
3813                         case GL_TEXTURE_3D:
3814                                 tex3d = texnum;
3815                                 break;
3816                         case GL_TEXTURE_CUBE_MAP:
3817                                 texcubemap = texnum;
3818                                 break;
3819                         }
3820                 }
3821                 // update 2d texture binding
3822                 if (unit->t2d != tex2d)
3823                 {
3824                         GL_ActiveTexture(unitnum);
3825                         if (tex2d)
3826                         {
3827                                 if (unit->t2d == 0)
3828                                 {
3829                                         qglEnable(GL_TEXTURE_2D);CHECKGLERROR
3830                                 }
3831                         }
3832                         else
3833                         {
3834                                 if (unit->t2d)
3835                                 {
3836                                         qglDisable(GL_TEXTURE_2D);CHECKGLERROR
3837                                 }
3838                         }
3839                         unit->t2d = tex2d;
3840                         qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
3841                 }
3842                 // update 3d texture binding
3843                 if (unit->t3d != tex3d)
3844                 {
3845                         GL_ActiveTexture(unitnum);
3846                         if (tex3d)
3847                         {
3848                                 if (unit->t3d == 0)
3849                                 {
3850                                         qglEnable(GL_TEXTURE_3D);CHECKGLERROR
3851                                 }
3852                         }
3853                         else
3854                         {
3855                                 if (unit->t3d)
3856                                 {
3857                                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
3858                                 }
3859                         }
3860                         unit->t3d = tex3d;
3861                         qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
3862                 }
3863                 // update cubemap texture binding
3864                 if (unit->tcubemap != texcubemap)
3865                 {
3866                         GL_ActiveTexture(unitnum);
3867                         if (texcubemap)
3868                         {
3869                                 if (unit->tcubemap == 0)
3870                                 {
3871                                         qglEnable(GL_TEXTURE_CUBE_MAP);CHECKGLERROR
3872                                 }
3873                         }
3874                         else
3875                         {
3876                                 if (unit->tcubemap)
3877                                 {
3878                                         qglDisable(GL_TEXTURE_CUBE_MAP);CHECKGLERROR
3879                                 }
3880                         }
3881                         unit->tcubemap = texcubemap;
3882                         qglBindTexture(GL_TEXTURE_CUBE_MAP, unit->tcubemap);CHECKGLERROR
3883                 }
3884                 break;
3885         case RENDERPATH_D3D9:
3886 #ifdef SUPPORTD3D
3887                 {
3888                         extern cvar_t gl_texture_anisotropy;
3889                         if (!tex)
3890                         {
3891                                 tex = r_texture_white;
3892                                 // not initialized enough yet...
3893                                 if (!tex)
3894                                         return;
3895                         }
3896                         // upload texture if needed
3897                         R_GetTexture(tex);
3898                         if (unit->texture == tex)
3899                                 return;
3900                         unit->texture = tex;
3901                         IDirect3DDevice9_SetTexture(vid_d3d9dev, unitnum, (IDirect3DBaseTexture9*)tex->d3dtexture);
3902                         //IDirect3DDevice9_SetRenderState(vid_d3d9dev, d3drswrap[unitnum], (tex->flags & TEXF_CLAMP) ? (D3DWRAPCOORD_0 | D3DWRAPCOORD_1 | D3DWRAPCOORD_2) : 0);
3903                         IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSU, tex->d3daddressu);
3904                         IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSV, tex->d3daddressv);
3905                         if (tex->d3daddressw)
3906                                 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSW,  tex->d3daddressw);
3907                         IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAGFILTER, tex->d3dmagfilter);
3908                         IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MINFILTER, tex->d3dminfilter);
3909                         IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MIPFILTER, tex->d3dmipfilter);
3910                         IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MIPMAPLODBIAS, tex->d3dmipmaplodbias);
3911                         IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAXMIPLEVEL, tex->d3dmaxmiplevelfilter);
3912                         IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAXANISOTROPY, gl_texture_anisotropy.integer);
3913                 }
3914 #endif
3915                 break;
3916         case RENDERPATH_D3D10:
3917                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3918                 break;
3919         case RENDERPATH_D3D11:
3920                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3921                 break;
3922         case RENDERPATH_SOFT:
3923                 if (!tex)
3924                 {
3925                         tex = r_texture_white;
3926                         // not initialized enough yet...
3927                         if (!tex)
3928                                 return;
3929                 }
3930                 texnum = R_GetTexture(tex);
3931                 if (unit->texture == tex)
3932                         return;
3933                 unit->texture = tex;
3934                 DPSOFTRAST_SetTexture(unitnum, texnum);
3935                 break;
3936         }
3937 }
3938
3939 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
3940 {
3941         gltextureunit_t *unit = gl_state.units + unitnum;
3942         switch(vid.renderpath)
3943         {
3944         case RENDERPATH_GL11:
3945         case RENDERPATH_GL13:
3946         case RENDERPATH_GL20:
3947         case RENDERPATH_GLES1:
3948         case RENDERPATH_GLES2:
3949 #ifdef GL_MODELVIEW
3950                 if (matrix && matrix->m[3][3])
3951                 {
3952                         // texmatrix specified, check if it is different
3953                         if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
3954                         {
3955                                 float glmatrix[16];
3956                                 unit->texmatrixenabled = true;
3957                                 unit->matrix = *matrix;
3958                                 CHECKGLERROR
3959                                 Matrix4x4_ToArrayFloatGL(&unit->matrix, glmatrix);
3960                                 GL_ActiveTexture(unitnum);
3961                                 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3962                                 qglLoadMatrixf(glmatrix);CHECKGLERROR
3963                                 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3964                         }
3965                 }
3966                 else
3967                 {
3968                         // no texmatrix specified, revert to identity
3969                         if (unit->texmatrixenabled)
3970                         {
3971                                 unit->texmatrixenabled = false;
3972                                 unit->matrix = identitymatrix;
3973                                 CHECKGLERROR
3974                                 GL_ActiveTexture(unitnum);
3975                                 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3976                                 qglLoadIdentity();CHECKGLERROR
3977                                 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3978                         }
3979                 }
3980 #endif
3981                 break;
3982         case RENDERPATH_D3D9:
3983         case RENDERPATH_D3D10:
3984         case RENDERPATH_D3D11:
3985                 break;
3986         case RENDERPATH_SOFT:
3987                 break;
3988         }
3989 }
3990
3991 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
3992 {
3993         gltextureunit_t *unit = gl_state.units + unitnum;
3994         CHECKGLERROR
3995         switch(vid.renderpath)
3996         {
3997         case RENDERPATH_GL20:
3998         case RENDERPATH_GLES2:
3999                 // do nothing
4000                 break;
4001         case RENDERPATH_GL13:
4002         case RENDERPATH_GLES1:
4003 #ifdef GL_TEXTURE_ENV
4004                 // GL_ARB_texture_env_combine
4005                 if (!combinergb)
4006                         combinergb = GL_MODULATE;
4007                 if (!combinealpha)
4008                         combinealpha = GL_MODULATE;
4009                 if (!rgbscale)
4010                         rgbscale = 1;
4011                 if (!alphascale)
4012                         alphascale = 1;
4013                 if (combinergb != combinealpha || rgbscale != 1 || alphascale != 1)
4014                 {
4015                         if (combinergb == GL_DECAL)
4016                                 combinergb = GL_INTERPOLATE;
4017                         if (unit->combine != GL_COMBINE)
4018                         {
4019                                 unit->combine = GL_COMBINE;
4020                                 GL_ActiveTexture(unitnum);
4021                                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);CHECKGLERROR
4022                                 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE mode
4023                         }
4024                         if (unit->combinergb != combinergb)
4025                         {
4026                                 unit->combinergb = combinergb;
4027                                 GL_ActiveTexture(unitnum);
4028                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, unit->combinergb);CHECKGLERROR
4029                         }
4030                         if (unit->combinealpha != combinealpha)
4031                         {
4032                                 unit->combinealpha = combinealpha;
4033                                 GL_ActiveTexture(unitnum);
4034                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, unit->combinealpha);CHECKGLERROR
4035                         }
4036                         if (unit->rgbscale != rgbscale)
4037                         {
4038                                 unit->rgbscale = rgbscale;
4039                                 GL_ActiveTexture(unitnum);
4040                                 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE, unit->rgbscale);CHECKGLERROR
4041                         }
4042                         if (unit->alphascale != alphascale)
4043                         {
4044                                 unit->alphascale = alphascale;
4045                                 GL_ActiveTexture(unitnum);
4046                                 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, unit->alphascale);CHECKGLERROR
4047                         }
4048                 }
4049                 else
4050                 {
4051                         if (unit->combine != combinergb)
4052                         {
4053                                 unit->combine = combinergb;
4054                                 GL_ActiveTexture(unitnum);
4055                                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
4056                         }
4057                 }
4058 #endif
4059                 break;
4060         case RENDERPATH_GL11:
4061                 // normal GL texenv
4062 #ifdef GL_TEXTURE_ENV
4063                 if (!combinergb)
4064                         combinergb = GL_MODULATE;
4065                 if (unit->combine != combinergb)
4066                 {
4067                         unit->combine = combinergb;
4068                         GL_ActiveTexture(unitnum);
4069                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
4070                 }
4071 #endif
4072                 break;
4073         case RENDERPATH_D3D9:
4074         case RENDERPATH_D3D10:
4075         case RENDERPATH_D3D11:
4076                 break;
4077         case RENDERPATH_SOFT:
4078                 break;
4079         }
4080 }
4081
4082 void R_Mesh_ResetTextureState(void)
4083 {
4084         unsigned int unitnum;
4085
4086         BACKENDACTIVECHECK
4087
4088         for (unitnum = 0;unitnum < vid.teximageunits;unitnum++)
4089                 R_Mesh_TexBind(unitnum, NULL);
4090         for (unitnum = 0;unitnum < vid.texarrayunits;unitnum++)
4091                 R_Mesh_TexCoordPointer(unitnum, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4092         switch(vid.renderpath)
4093         {
4094         case RENDERPATH_GL20:
4095         case RENDERPATH_GLES2:
4096         case RENDERPATH_D3D9:
4097         case RENDERPATH_D3D10:
4098         case RENDERPATH_D3D11:
4099         case RENDERPATH_SOFT:
4100                 break;
4101         case RENDERPATH_GL11:
4102         case RENDERPATH_GL13:
4103         case RENDERPATH_GLES1:
4104                 for (unitnum = 0;unitnum < vid.texunits;unitnum++)
4105                 {
4106                         R_Mesh_TexCombine(unitnum, GL_MODULATE, GL_MODULATE, 1, 1);
4107                         R_Mesh_TexMatrix(unitnum, NULL);
4108                 }
4109                 break;
4110         }
4111 }
4112
4113
4114
4115 #ifdef SUPPORTD3D
4116 //#define r_vertex3f_d3d9fvf (D3DFVF_XYZ)
4117 //#define r_vertexgeneric_d3d9fvf (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
4118 //#define r_vertexmesh_d3d9fvf (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX5 | D3DFVF_TEXCOORDSIZE1(3) | D3DFVF_TEXCOORDSIZE2(3) | D3DFVF_TEXCOORDSIZE3(3))
4119
4120 D3DVERTEXELEMENT9 r_vertex3f_d3d9elements[] =
4121 {
4122         {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
4123         D3DDECL_END()
4124 };
4125
4126 D3DVERTEXELEMENT9 r_vertexgeneric_d3d9elements[] =
4127 {
4128         {0, (int)((size_t)&((r_vertexgeneric_t *)0)->vertex3f  ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
4129         {0, (int)((size_t)&((r_vertexgeneric_t *)0)->color4f   ), D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
4130         {0, (int)((size_t)&((r_vertexgeneric_t *)0)->texcoord2f), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
4131         D3DDECL_END()
4132 };
4133
4134 D3DVERTEXELEMENT9 r_vertexmesh_d3d9elements[] =
4135 {
4136         {0, (int)((size_t)&((r_vertexmesh_t *)0)->vertex3f          ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
4137         {0, (int)((size_t)&((r_vertexmesh_t *)0)->color4f           ), D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
4138         {0, (int)((size_t)&((r_vertexmesh_t *)0)->texcoordtexture2f ), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
4139         {0, (int)((size_t)&((r_vertexmesh_t *)0)->svector3f         ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
4140         {0, (int)((size_t)&((r_vertexmesh_t *)0)->tvector3f         ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 2},
4141         {0, (int)((size_t)&((r_vertexmesh_t *)0)->normal3f          ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 3},
4142         {0, (int)((size_t)&((r_vertexmesh_t *)0)->texcoordlightmap2f), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 4},
4143         {0, (int)((size_t)&((r_vertexmesh_t *)0)->skeletalindex4ub  ), D3DDECLTYPE_UBYTE4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 6},
4144         {0, (int)((size_t)&((r_vertexmesh_t *)0)->skeletalweight4ub ), D3DDECLTYPE_UBYTE4N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 7},
4145         D3DDECL_END()
4146 };
4147
4148 IDirect3DVertexDeclaration9 *r_vertex3f_d3d9decl;
4149 IDirect3DVertexDeclaration9 *r_vertexgeneric_d3d9decl;
4150 IDirect3DVertexDeclaration9 *r_vertexmesh_d3d9decl;
4151 #endif
4152
4153 static void R_Mesh_InitVertexDeclarations(void)
4154 {
4155 #ifdef SUPPORTD3D
4156         r_vertex3f_d3d9decl = NULL;
4157         r_vertexgeneric_d3d9decl = NULL;
4158         r_vertexmesh_d3d9decl = NULL;
4159         switch(vid.renderpath)
4160         {
4161         case RENDERPATH_GL20:
4162         case RENDERPATH_GL13:
4163         case RENDERPATH_GL11:
4164         case RENDERPATH_GLES1:
4165         case RENDERPATH_GLES2:
4166                 break;
4167         case RENDERPATH_D3D9:
4168                 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertex3f_d3d9elements, &r_vertex3f_d3d9decl);
4169                 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexgeneric_d3d9elements, &r_vertexgeneric_d3d9decl);
4170                 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexmesh_d3d9elements, &r_vertexmesh_d3d9decl);
4171                 break;
4172         case RENDERPATH_D3D10:
4173                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4174                 break;
4175         case RENDERPATH_D3D11:
4176                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4177                 break;
4178         case RENDERPATH_SOFT:
4179                 break;
4180         }
4181 #endif
4182 }
4183
4184 static void R_Mesh_DestroyVertexDeclarations(void)
4185 {
4186 #ifdef SUPPORTD3D
4187         if (r_vertex3f_d3d9decl)
4188                 IDirect3DVertexDeclaration9_Release(r_vertex3f_d3d9decl);
4189         r_vertex3f_d3d9decl = NULL;
4190         if (r_vertexgeneric_d3d9decl)
4191                 IDirect3DVertexDeclaration9_Release(r_vertexgeneric_d3d9decl);
4192         r_vertexgeneric_d3d9decl = NULL;
4193         if (r_vertexmesh_d3d9decl)
4194                 IDirect3DVertexDeclaration9_Release(r_vertexmesh_d3d9decl);
4195         r_vertexmesh_d3d9decl = NULL;
4196 #endif
4197 }
4198
4199 void R_Mesh_PrepareVertices_Vertex3f(int numvertices, const float *vertex3f, const r_meshbuffer_t *vertexbuffer)
4200 {
4201         // upload temporary vertexbuffer for this rendering
4202         if (!gl_state.usevbo_staticvertex)
4203                 vertexbuffer = NULL;
4204         if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
4205         {
4206                 if (gl_state.preparevertices_dynamicvertexbuffer)
4207                         R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex3f, numvertices * sizeof(float[3]));
4208                 else
4209                         gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex3f, numvertices * sizeof(float[3]), "temporary", false, true, false);
4210                 vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
4211         }
4212         switch(vid.renderpath)
4213         {
4214         case RENDERPATH_GL20:
4215         case RENDERPATH_GLES2:
4216                 if (vertexbuffer)
4217                 {
4218                         R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
4219                         R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4220                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4221                         R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4222                         R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4223                         R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4224                         R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4225                         R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4226                         R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4227                         R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4228                 }
4229                 else
4230                 {
4231                         R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
4232                         R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4233                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4234                         R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4235                         R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4236                         R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4237                         R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4238                         R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4239                         R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4240                         R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4241                 }
4242                 break;
4243         case RENDERPATH_GL13:
4244         case RENDERPATH_GLES1:
4245                 if (vertexbuffer)
4246                 {
4247                         R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
4248                         R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4249                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4250                         R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4251                 }
4252                 else
4253                 {
4254                         R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
4255                         R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4256                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4257                         R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4258                 }
4259                 break;
4260         case RENDERPATH_GL11:
4261                 if (vertexbuffer)
4262                 {
4263                         R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
4264                         R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4265                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4266                 }
4267                 else
4268                 {
4269                         R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
4270                         R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4271                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4272                 }
4273                 break;
4274         case RENDERPATH_D3D9:
4275 #ifdef SUPPORTD3D
4276                 IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertex3f_d3d9decl);
4277                 if (vertexbuffer)
4278                         IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(float[3]));
4279                 else
4280                         IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, NULL, 0, 0);
4281                 gl_state.d3dvertexbuffer = (void *)vertexbuffer;
4282                 gl_state.d3dvertexdata = (void *)vertex3f;
4283                 gl_state.d3dvertexsize = sizeof(float[3]);
4284 #endif
4285                 break;
4286         case RENDERPATH_D3D10:
4287                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4288                 break;
4289         case RENDERPATH_D3D11:
4290                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4291                 break;
4292         case RENDERPATH_SOFT:
4293                 DPSOFTRAST_SetVertexPointer(vertex3f, sizeof(float[3]));
4294                 DPSOFTRAST_SetColorPointer(NULL, 0);
4295                 DPSOFTRAST_SetTexCoordPointer(0, 2, sizeof(float[2]), NULL);
4296                 DPSOFTRAST_SetTexCoordPointer(1, 2, sizeof(float[2]), NULL);
4297                 DPSOFTRAST_SetTexCoordPointer(2, 2, sizeof(float[2]), NULL);
4298                 DPSOFTRAST_SetTexCoordPointer(3, 2, sizeof(float[2]), NULL);
4299                 DPSOFTRAST_SetTexCoordPointer(4, 2, sizeof(float[2]), NULL);
4300                 break;
4301         }
4302 }
4303
4304
4305
4306 r_vertexgeneric_t *R_Mesh_PrepareVertices_Generic_Lock(int numvertices)
4307 {
4308         size_t size;
4309         size = sizeof(r_vertexgeneric_t) * numvertices;
4310         if (gl_state.preparevertices_tempdatamaxsize < size)
4311         {
4312                 gl_state.preparevertices_tempdatamaxsize = size;
4313                 gl_state.preparevertices_tempdata = Mem_Realloc(r_main_mempool, gl_state.preparevertices_tempdata, gl_state.preparevertices_tempdatamaxsize);
4314         }
4315         gl_state.preparevertices_vertexgeneric = (r_vertexgeneric_t *)gl_state.preparevertices_tempdata;
4316         gl_state.preparevertices_numvertices = numvertices;
4317         return gl_state.preparevertices_vertexgeneric;
4318 }
4319
4320 qboolean R_Mesh_PrepareVertices_Generic_Unlock(void)
4321 {
4322         R_Mesh_PrepareVertices_Generic(gl_state.preparevertices_numvertices, gl_state.preparevertices_vertexgeneric, NULL);
4323         gl_state.preparevertices_vertexgeneric = NULL;
4324         gl_state.preparevertices_numvertices = 0;
4325         return true;
4326 }
4327
4328 void R_Mesh_PrepareVertices_Generic_Arrays(int numvertices, const float *vertex3f, const float *color4f, const float *texcoord2f)
4329 {
4330         int i;
4331         r_vertexgeneric_t *vertex;
4332         switch(vid.renderpath)
4333         {
4334         case RENDERPATH_GL20:
4335         case RENDERPATH_GLES2:
4336                 if (!vid.useinterleavedarrays)
4337                 {
4338                         R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
4339                         R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
4340                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoord2f, NULL, 0);
4341                         R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4342                         R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4343                         R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4344                         R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4345                         R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4346                         R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4347                         R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4348                         return;
4349                 }
4350                 break;
4351         case RENDERPATH_GL11:
4352         case RENDERPATH_GL13:
4353         case RENDERPATH_GLES1:
4354                 if (!vid.useinterleavedarrays)
4355                 {
4356                         R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
4357                         R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
4358                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoord2f, NULL, 0);
4359                         if (vid.texunits >= 2)
4360                                 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4361                         if (vid.texunits >= 3)
4362                                 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4363                         return;
4364                 }
4365                 break;
4366         case RENDERPATH_D3D9:
4367         case RENDERPATH_D3D10:
4368         case RENDERPATH_D3D11:
4369                 break;
4370         case RENDERPATH_SOFT:
4371                 DPSOFTRAST_SetVertexPointer(vertex3f, sizeof(float[3]));
4372                 DPSOFTRAST_SetColorPointer(color4f, sizeof(float[4]));
4373                 DPSOFTRAST_SetTexCoordPointer(0, 2, sizeof(float[2]), texcoord2f);
4374                 DPSOFTRAST_SetTexCoordPointer(1, 2, sizeof(float[2]), NULL);
4375                 DPSOFTRAST_SetTexCoordPointer(2, 2, sizeof(float[2]), NULL);
4376                 DPSOFTRAST_SetTexCoordPointer(3, 2, sizeof(float[2]), NULL);
4377                 DPSOFTRAST_SetTexCoordPointer(4, 2, sizeof(float[2]), NULL);
4378                 return;
4379         }
4380
4381         // no quick path for this case, convert to vertex structs
4382         vertex = R_Mesh_PrepareVertices_Generic_Lock(numvertices);
4383         for (i = 0;i < numvertices;i++)
4384                 VectorCopy(vertex3f + 3*i, vertex[i].vertex3f);
4385         if (color4f)
4386         {
4387                 for (i = 0;i < numvertices;i++)
4388                         Vector4Copy(color4f + 4*i, vertex[i].color4f);
4389         }
4390         else
4391         {
4392                 for (i = 0;i < numvertices;i++)
4393                         Vector4Copy(gl_state.color4f, vertex[i].color4f);
4394         }
4395         if (texcoord2f)
4396                 for (i = 0;i < numvertices;i++)
4397                         Vector2Copy(texcoord2f + 2*i, vertex[i].texcoord2f);
4398         R_Mesh_PrepareVertices_Generic_Unlock();
4399         R_Mesh_PrepareVertices_Generic(numvertices, vertex, NULL);
4400 }
4401
4402 void R_Mesh_PrepareVertices_Generic(int numvertices, const r_vertexgeneric_t *vertex, const r_meshbuffer_t *vertexbuffer)
4403 {
4404         // upload temporary vertexbuffer for this rendering
4405         if (!gl_state.usevbo_staticvertex)
4406                 vertexbuffer = NULL;
4407         if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
4408         {
4409                 if (gl_state.preparevertices_dynamicvertexbuffer)
4410                         R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex, numvertices * sizeof(*vertex));
4411                 else
4412                         gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex, numvertices * sizeof(*vertex), "temporary", false, true, false);
4413                 vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
4414         }
4415         switch(vid.renderpath)
4416         {
4417         case RENDERPATH_GL20:
4418         case RENDERPATH_GLES2:
4419                 if (vertexbuffer)
4420                 {
4421                         R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , vertexbuffer, (int)((unsigned char *)vertex->vertex3f           - (unsigned char *)vertex));
4422                         R_Mesh_ColorPointer(      4, GL_FLOAT        , sizeof(*vertex), vertex->color4f           , vertexbuffer, (int)((unsigned char *)vertex->color4f            - (unsigned char *)vertex));
4423                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoord2f        , vertexbuffer, (int)((unsigned char *)vertex->texcoord2f         - (unsigned char *)vertex));
4424                         R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4425                         R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4426                         R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4427                         R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4428                         R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4429                         R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4430                         R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4431                 }
4432                 else
4433                 {
4434                         R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , NULL, 0);
4435                         R_Mesh_ColorPointer(      4, GL_FLOAT        , sizeof(*vertex), vertex->color4f           , NULL, 0);
4436                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoord2f        , NULL, 0);
4437                         R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4438                         R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4439                         R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4440                         R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4441                         R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4442                         R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4443                         R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4444                 }
4445                 break;
4446         case RENDERPATH_GL13:
4447         case RENDERPATH_GLES1:
4448                 if (vertexbuffer)
4449                 {
4450                         R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , vertexbuffer, (int)((unsigned char *)vertex->vertex3f           - (unsigned char *)vertex));
4451                         R_Mesh_ColorPointer(      4, GL_FLOAT        , sizeof(*vertex), vertex->color4f           , vertexbuffer, (int)((unsigned char *)vertex->color4f            - (unsigned char *)vertex));
4452                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoord2f        , vertexbuffer, (int)((unsigned char *)vertex->texcoord2f         - (unsigned char *)vertex));
4453                         R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4454                 }
4455                 else
4456                 {
4457                         R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , NULL, 0);
4458                         R_Mesh_ColorPointer(      4, GL_FLOAT        , sizeof(*vertex), vertex->color4f           , NULL, 0);
4459                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoord2f        , NULL, 0);
4460                         R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4461                 }
4462                 break;
4463         case RENDERPATH_GL11:
4464                 if (vertexbuffer)
4465                 {
4466                         R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , vertexbuffer, (int)((unsigned char *)vertex->vertex3f           - (unsigned char *)vertex));
4467                         R_Mesh_ColorPointer(      4, GL_FLOAT        , sizeof(*vertex), vertex->color4f           , vertexbuffer, (int)((unsigned char *)vertex->color4f            - (unsigned char *)vertex));
4468                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoord2f        , vertexbuffer, (int)((unsigned char *)vertex->texcoord2f         - (unsigned char *)vertex));
4469                 }
4470                 else
4471                 {
4472                         R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , NULL, 0);
4473                         R_Mesh_ColorPointer(      4, GL_FLOAT        , sizeof(*vertex), vertex->color4f           , NULL, 0);
4474                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoord2f        , NULL, 0);
4475                 }
4476                 break;
4477         case RENDERPATH_D3D9:
4478 #ifdef SUPPORTD3D
4479                 IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertexgeneric_d3d9decl);
4480                 if (vertexbuffer)
4481                         IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(*vertex));
4482                 else
4483                         IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, NULL, 0, 0);
4484                 gl_state.d3dvertexbuffer = (void *)vertexbuffer;
4485                 gl_state.d3dvertexdata = (void *)vertex;
4486                 gl_state.d3dvertexsize = sizeof(*vertex);
4487 #endif
4488                 break;
4489         case RENDERPATH_D3D10:
4490                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4491                 break;
4492         case RENDERPATH_D3D11:
4493                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4494                 break;
4495         case RENDERPATH_SOFT:
4496                 DPSOFTRAST_SetVertexPointer(vertex->vertex3f, sizeof(*vertex));
4497                 DPSOFTRAST_SetColorPointer(vertex->color4f, sizeof(*vertex));
4498                 DPSOFTRAST_SetTexCoordPointer(0, 2, sizeof(*vertex), vertex->texcoord2f);
4499                 DPSOFTRAST_SetTexCoordPointer(1, 2, sizeof(*vertex), NULL);
4500                 DPSOFTRAST_SetTexCoordPointer(2, 2, sizeof(*vertex), NULL);
4501                 DPSOFTRAST_SetTexCoordPointer(3, 2, sizeof(*vertex), NULL);
4502                 DPSOFTRAST_SetTexCoordPointer(4, 2, sizeof(*vertex), NULL);
4503                 break;
4504         }
4505 }
4506
4507
4508
4509 r_vertexmesh_t *R_Mesh_PrepareVertices_Mesh_Lock(int numvertices)
4510 {
4511         size_t size;
4512         size = sizeof(r_vertexmesh_t) * numvertices;
4513         if (gl_state.preparevertices_tempdatamaxsize < size)
4514         {
4515                 gl_state.preparevertices_tempdatamaxsize = size;
4516                 gl_state.preparevertices_tempdata = Mem_Realloc(r_main_mempool, gl_state.preparevertices_tempdata, gl_state.preparevertices_tempdatamaxsize);
4517         }
4518         gl_state.preparevertices_vertexmesh = (r_vertexmesh_t *)gl_state.preparevertices_tempdata;
4519         gl_state.preparevertices_numvertices = numvertices;
4520         return gl_state.preparevertices_vertexmesh;
4521 }
4522
4523 qboolean R_Mesh_PrepareVertices_Mesh_Unlock(void)
4524 {
4525         R_Mesh_PrepareVertices_Mesh(gl_state.preparevertices_numvertices, gl_state.preparevertices_vertexmesh, NULL);
4526         gl_state.preparevertices_vertexmesh = NULL;
4527         gl_state.preparevertices_numvertices = 0;
4528         return true;
4529 }
4530
4531 void R_Mesh_PrepareVertices_Mesh_Arrays(int numvertices, const float *vertex3f, const float *svector3f, const float *tvector3f, const float *normal3f, const float *color4f, const float *texcoordtexture2f, const float *texcoordlightmap2f)
4532 {
4533         int i;
4534         r_vertexmesh_t *vertex;
4535         switch(vid.renderpath)
4536         {
4537         case RENDERPATH_GL20:
4538         case RENDERPATH_GLES2:
4539                 if (!vid.useinterleavedarrays)
4540                 {
4541                         R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
4542                         R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
4543                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoordtexture2f, NULL, 0);
4544                         R_Mesh_TexCoordPointer(1, 3, GL_FLOAT, sizeof(float[3]), svector3f, NULL, 0);
4545                         R_Mesh_TexCoordPointer(2, 3, GL_FLOAT, sizeof(float[3]), tvector3f, NULL, 0);
4546                         R_Mesh_TexCoordPointer(3, 3, GL_FLOAT, sizeof(float[3]), normal3f, NULL, 0);
4547                         R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), texcoordlightmap2f, NULL, 0);
4548                         R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4549                         R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4550                         R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4551                         return;
4552                 }
4553                 break;
4554         case RENDERPATH_GL11:
4555         case RENDERPATH_GL13:
4556         case RENDERPATH_GLES1:
4557                 if (!vid.useinterleavedarrays)
4558                 {
4559                         R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
4560                         R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
4561                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoordtexture2f, NULL, 0);
4562                         if (vid.texunits >= 2)
4563                                 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), texcoordlightmap2f, NULL, 0);
4564                         if (vid.texunits >= 3)
4565                                 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4566                         return;
4567                 }
4568                 break;
4569         case RENDERPATH_D3D9:
4570         case RENDERPATH_D3D10:
4571         case RENDERPATH_D3D11:
4572                 break;
4573         case RENDERPATH_SOFT:
4574                 DPSOFTRAST_SetVertexPointer(vertex3f, sizeof(float[3]));
4575                 DPSOFTRAST_SetColorPointer(color4f, sizeof(float[4]));
4576                 DPSOFTRAST_SetTexCoordPointer(0, 2, sizeof(float[2]), texcoordtexture2f);
4577                 DPSOFTRAST_SetTexCoordPointer(1, 3, sizeof(float[3]), svector3f);
4578                 DPSOFTRAST_SetTexCoordPointer(2, 3, sizeof(float[3]), tvector3f);
4579                 DPSOFTRAST_SetTexCoordPointer(3, 3, sizeof(float[3]), normal3f);
4580                 DPSOFTRAST_SetTexCoordPointer(4, 2, sizeof(float[2]), texcoordlightmap2f);
4581                 return;
4582         }
4583
4584         vertex = R_Mesh_PrepareVertices_Mesh_Lock(numvertices);
4585         for (i = 0;i < numvertices;i++)
4586                 VectorCopy(vertex3f + 3*i, vertex[i].vertex3f);
4587         if (svector3f)
4588                 for (i = 0;i < numvertices;i++)
4589                         VectorCopy(svector3f + 3*i, vertex[i].svector3f);
4590         if (tvector3f)
4591                 for (i = 0;i < numvertices;i++)
4592                         VectorCopy(tvector3f + 3*i, vertex[i].tvector3f);
4593         if (normal3f)
4594                 for (i = 0;i < numvertices;i++)
4595                         VectorCopy(normal3f + 3*i, vertex[i].normal3f);
4596         if (color4f)
4597         {
4598                 for (i = 0;i < numvertices;i++)
4599                         Vector4Copy(color4f + 4*i, vertex[i].color4f);
4600         }
4601         else
4602         {
4603                 for (i = 0;i < numvertices;i++)
4604                         Vector4Copy(gl_state.color4f, vertex[i].color4f);
4605         }
4606         if (texcoordtexture2f)
4607                 for (i = 0;i < numvertices;i++)
4608                         Vector2Copy(texcoordtexture2f + 2*i, vertex[i].texcoordtexture2f);
4609         if (texcoordlightmap2f)
4610                 for (i = 0;i < numvertices;i++)
4611                         Vector2Copy(texcoordlightmap2f + 2*i, vertex[i].texcoordlightmap2f);
4612         R_Mesh_PrepareVertices_Mesh_Unlock();
4613         R_Mesh_PrepareVertices_Mesh(numvertices, vertex, NULL);
4614 }
4615
4616 void R_Mesh_PrepareVertices_Mesh(int numvertices, const r_vertexmesh_t *vertex, const r_meshbuffer_t *vertexbuffer)
4617 {
4618         // upload temporary vertexbuffer for this rendering
4619         if (!gl_state.usevbo_staticvertex)
4620                 vertexbuffer = NULL;
4621         if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
4622         {
4623                 if (gl_state.preparevertices_dynamicvertexbuffer)
4624                         R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex, numvertices * sizeof(*vertex));
4625                 else
4626                         gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex, numvertices * sizeof(*vertex), "temporary", false, true, false);
4627                 vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
4628         }
4629         switch(vid.renderpath)
4630         {
4631         case RENDERPATH_GL20:
4632         case RENDERPATH_GLES2:
4633                 if (vertexbuffer)
4634                 {
4635                         R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , vertexbuffer, (int)((unsigned char *)vertex->vertex3f           - (unsigned char *)vertex));
4636                         R_Mesh_ColorPointer(      4, GL_FLOAT        , sizeof(*vertex), vertex->color4f           , vertexbuffer, (int)((unsigned char *)vertex->color4f            - (unsigned char *)vertex));
4637                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoordtexture2f , vertexbuffer, (int)((unsigned char *)vertex->texcoordtexture2f  - (unsigned char *)vertex));
4638                         R_Mesh_TexCoordPointer(1, 3, GL_FLOAT        , sizeof(*vertex), vertex->svector3f         , vertexbuffer, (int)((unsigned char *)vertex->svector3f          - (unsigned char *)vertex));
4639                         R_Mesh_TexCoordPointer(2, 3, GL_FLOAT        , sizeof(*vertex), vertex->tvector3f         , vertexbuffer, (int)((unsigned char *)vertex->tvector3f          - (unsigned char *)vertex));
4640                         R_Mesh_TexCoordPointer(3, 3, GL_FLOAT        , sizeof(*vertex), vertex->normal3f          , vertexbuffer, (int)((unsigned char *)vertex->normal3f           - (unsigned char *)vertex));
4641                         R_Mesh_TexCoordPointer(4, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoordlightmap2f, vertexbuffer, (int)((unsigned char *)vertex->texcoordlightmap2f - (unsigned char *)vertex));
4642                         R_Mesh_TexCoordPointer(5, 2, GL_FLOAT        , sizeof(*vertex), NULL, NULL, 0);
4643                         R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE | 0x80000000, sizeof(*vertex), vertex->skeletalindex4ub  , vertexbuffer, (int)((unsigned char *)vertex->skeletalindex4ub   - (unsigned char *)vertex));
4644                         R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->skeletalweight4ub , vertexbuffer, (int)((unsigned char *)vertex->skeletalweight4ub  - (unsigned char *)vertex));
4645                 }
4646                 else
4647                 {
4648                         R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , NULL, 0);
4649                         R_Mesh_ColorPointer(      4, GL_FLOAT        , sizeof(*vertex), vertex->color4f           , NULL, 0);
4650                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoordtexture2f , NULL, 0);
4651                         R_Mesh_TexCoordPointer(1, 3, GL_FLOAT        , sizeof(*vertex), vertex->svector3f         , NULL, 0);
4652                         R_Mesh_TexCoordPointer(2, 3, GL_FLOAT        , sizeof(*vertex), vertex->tvector3f         , NULL, 0);
4653                         R_Mesh_TexCoordPointer(3, 3, GL_FLOAT        , sizeof(*vertex), vertex->normal3f          , NULL, 0);
4654                         R_Mesh_TexCoordPointer(4, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoordlightmap2f, NULL, 0);
4655                         R_Mesh_TexCoordPointer(5, 2, GL_FLOAT        , sizeof(*vertex), NULL, NULL, 0);
4656                         R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE | 0x80000000, sizeof(*vertex), vertex->skeletalindex4ub  , NULL, 0);
4657                         R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->skeletalweight4ub , NULL, 0);
4658                 }
4659                 break;
4660         case RENDERPATH_GL13:
4661         case RENDERPATH_GLES1:
4662                 if (vertexbuffer)
4663                 {
4664                         R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , vertexbuffer, (int)((unsigned char *)vertex->vertex3f           - (unsigned char *)vertex));
4665                         R_Mesh_ColorPointer(      4, GL_FLOAT        , sizeof(*vertex), vertex->color4f           , vertexbuffer, (int)((unsigned char *)vertex->color4f            - (unsigned char *)vertex));
4666                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoordtexture2f , vertexbuffer, (int)((unsigned char *)vertex->texcoordtexture2f  - (unsigned char *)vertex));
4667                         R_Mesh_TexCoordPointer(1, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoordlightmap2f, vertexbuffer, (int)((unsigned char *)vertex->texcoordlightmap2f - (unsigned char *)vertex));
4668                 }
4669                 else
4670                 {
4671                         R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , NULL, 0);
4672                         R_Mesh_ColorPointer(      4, GL_FLOAT        , sizeof(*vertex), vertex->color4f           , NULL, 0);
4673                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoordtexture2f , NULL, 0);
4674                         R_Mesh_TexCoordPointer(1, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoordlightmap2f, NULL, 0);
4675                 }
4676                 break;
4677         case RENDERPATH_GL11:
4678                 if (vertexbuffer)
4679                 {
4680                         R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , vertexbuffer, (int)((unsigned char *)vertex->vertex3f           - (unsigned char *)vertex));
4681                         R_Mesh_ColorPointer(      4, GL_FLOAT        , sizeof(*vertex), vertex->color4f           , vertexbuffer, (int)((unsigned char *)vertex->color4f            - (unsigned char *)vertex));
4682                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoordtexture2f , vertexbuffer, (int)((unsigned char *)vertex->texcoordtexture2f  - (unsigned char *)vertex));
4683                 }
4684                 else
4685                 {
4686                         R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , NULL, 0);
4687                         R_Mesh_ColorPointer(      4, GL_FLOAT        , sizeof(*vertex), vertex->color4f           , NULL, 0);
4688                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoordtexture2f , NULL, 0);
4689                 }
4690                 break;
4691         case RENDERPATH_D3D9:
4692 #ifdef SUPPORTD3D
4693                 IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertexmesh_d3d9decl);
4694                 if (vertexbuffer)
4695                         IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(*vertex));
4696                 else
4697                         IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, NULL, 0, 0);
4698                 gl_state.d3dvertexbuffer = (void *)vertexbuffer;
4699                 gl_state.d3dvertexdata = (void *)vertex;
4700                 gl_state.d3dvertexsize = sizeof(*vertex);
4701 #endif
4702                 break;
4703         case RENDERPATH_D3D10:
4704                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4705                 break;
4706         case RENDERPATH_D3D11:
4707                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4708                 break;
4709         case RENDERPATH_SOFT:
4710                 DPSOFTRAST_SetVertexPointer(vertex->vertex3f, sizeof(*vertex));
4711                 DPSOFTRAST_SetColorPointer(vertex->color4f, sizeof(*vertex));
4712                 DPSOFTRAST_SetTexCoordPointer(0, 2, sizeof(*vertex), vertex->texcoordtexture2f);
4713                 DPSOFTRAST_SetTexCoordPointer(1, 3, sizeof(*vertex), vertex->svector3f);
4714                 DPSOFTRAST_SetTexCoordPointer(2, 3, sizeof(*vertex), vertex->tvector3f);
4715                 DPSOFTRAST_SetTexCoordPointer(3, 3, sizeof(*vertex), vertex->normal3f);
4716                 DPSOFTRAST_SetTexCoordPointer(4, 2, sizeof(*vertex), vertex->texcoordlightmap2f);
4717                 break;
4718         }
4719 }
4720
4721 void GL_BlendEquationSubtract(qboolean negated)
4722 {
4723         if(negated)
4724         {
4725                 switch(vid.renderpath)
4726                 {
4727                 case RENDERPATH_GL11:
4728                 case RENDERPATH_GL13:
4729                 case RENDERPATH_GL20:
4730                 case RENDERPATH_GLES1:
4731                 case RENDERPATH_GLES2:
4732                         qglBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT);
4733                         break;
4734                 case RENDERPATH_D3D9:
4735 #ifdef SUPPORTD3D
4736                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_BLENDOP, D3DBLENDOP_SUBTRACT);
4737 #endif
4738                         break;
4739                 case RENDERPATH_D3D10:
4740                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4741                         break;
4742                 case RENDERPATH_D3D11:
4743                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4744                         break;
4745                 case RENDERPATH_SOFT:
4746                         DPSOFTRAST_BlendSubtract(true);
4747                         break;
4748                 }
4749         }
4750         else
4751         {
4752                 switch(vid.renderpath)
4753                 {
4754                 case RENDERPATH_GL11:
4755                 case RENDERPATH_GL13:
4756                 case RENDERPATH_GL20:
4757                 case RENDERPATH_GLES1:
4758                 case RENDERPATH_GLES2:
4759                         qglBlendEquationEXT(GL_FUNC_ADD);
4760                         break;
4761                 case RENDERPATH_D3D9:
4762 #ifdef SUPPORTD3D
4763                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_BLENDOP, D3DBLENDOP_ADD);
4764 #endif
4765                         break;
4766                 case RENDERPATH_D3D10:
4767                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4768                         break;
4769                 case RENDERPATH_D3D11:
4770                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4771                         break;
4772                 case RENDERPATH_SOFT:
4773                         DPSOFTRAST_BlendSubtract(false);
4774                         break;
4775                 }
4776         }
4777 }