]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - gl_backend.c
UNMERGE
[xonotic/darkplaces.git] / gl_backend.c
1
2 #include "quakedef.h"
3 #include "cl_collision.h"
4 #ifdef SUPPORTD3D
5 #include <d3d9.h>
6 extern LPDIRECT3DDEVICE9 vid_d3d9dev;
7 extern D3DCAPS9 vid_d3d9caps;
8 #endif
9
10 #define MAX_RENDERTARGETS 4
11
12 cvar_t gl_mesh_drawrangeelements = {0, "gl_mesh_drawrangeelements", "1", "use glDrawRangeElements function if available instead of glDrawElements (for performance comparisons or bug testing)"};
13 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)"};
14 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"};
15 cvar_t gl_mesh_separatearrays = {0, "gl_mesh_separatearrays", "1", "use several separate vertex arrays rather than one combined stream"};
16 cvar_t gl_paranoid = {0, "gl_paranoid", "0", "enables OpenGL error checking and other tests"};
17 cvar_t gl_printcheckerror = {0, "gl_printcheckerror", "0", "prints all OpenGL error checks, useful to identify location of driver crashes"};
18
19 cvar_t r_render = {0, "r_render", "1", "enables rendering 3D views (you want this on!)"};
20 cvar_t r_renderview = {0, "r_renderview", "1", "enables rendering 3D views (you want this on!)"};
21 cvar_t r_waterwarp = {CVAR_SAVE, "r_waterwarp", "1", "warp view while underwater"};
22 cvar_t gl_polyblend = {CVAR_SAVE, "gl_polyblend", "1", "tints view while underwater, hurt, etc"};
23 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1", "enables OpenGL dithering (16bit looks bad with this off)"};
24 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)"};
25 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"};
26 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"};
27 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"};
28
29 cvar_t v_flipped = {0, "v_flipped", "0", "mirror the screen (poor man's left handed mode)"};
30 qboolean v_flipped_state = false;
31
32 r_viewport_t gl_viewport;
33 matrix4x4_t gl_modelmatrix;
34 matrix4x4_t gl_viewmatrix;
35 matrix4x4_t gl_modelviewmatrix;
36 matrix4x4_t gl_projectionmatrix;
37 matrix4x4_t gl_modelviewprojectionmatrix;
38 float gl_modelview16f[16];
39 float gl_modelviewprojection16f[16];
40 qboolean gl_modelmatrixchanged;
41
42 int gl_maxdrawrangeelementsvertices;
43 int gl_maxdrawrangeelementsindices;
44
45 #ifdef DEBUGGL
46 int errornumber = 0;
47
48 void GL_PrintError(int errornumber, char *filename, int linenumber)
49 {
50         switch(errornumber)
51         {
52 #ifdef GL_INVALID_ENUM
53         case GL_INVALID_ENUM:
54                 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
55                 break;
56 #endif
57 #ifdef GL_INVALID_VALUE
58         case GL_INVALID_VALUE:
59                 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
60                 break;
61 #endif
62 #ifdef GL_INVALID_OPERATION
63         case GL_INVALID_OPERATION:
64                 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
65                 break;
66 #endif
67 #ifdef GL_STACK_OVERFLOW
68         case GL_STACK_OVERFLOW:
69                 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
70                 break;
71 #endif
72 #ifdef GL_STACK_UNDERFLOW
73         case GL_STACK_UNDERFLOW:
74                 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
75                 break;
76 #endif
77 #ifdef GL_OUT_OF_MEMORY
78         case GL_OUT_OF_MEMORY:
79                 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
80                 break;
81 #endif
82 #ifdef GL_TABLE_TOO_LARGE
83         case GL_TABLE_TOO_LARGE:
84                 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
85                 break;
86 #endif
87 #ifdef GL_INVALID_FRAMEBUFFER_OPERATION_EXT
88         case GL_INVALID_FRAMEBUFFER_OPERATION_EXT:
89                 Con_Printf("GL_INVALID_FRAMEBUFFER_OPERATION at %s:%i\n", filename, linenumber);
90                 break;
91 #endif
92         default:
93                 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
94                 break;
95         }
96 }
97 #endif
98
99 #define BACKENDACTIVECHECK if (!gl_state.active) Sys_Error("GL backend function called when backend is not active");
100
101 void SCR_ScreenShot_f (void);
102
103 typedef struct gltextureunit_s
104 {
105         int pointer_texcoord_components;
106         int pointer_texcoord_gltype;
107         size_t pointer_texcoord_stride;
108         const void *pointer_texcoord_pointer;
109         const r_meshbuffer_t *pointer_texcoord_vertexbuffer;
110         size_t pointer_texcoord_offset;
111
112         rtexture_t *texture;
113         int t2d, t3d, tcubemap, trectangle;
114         int arrayenabled;
115         int rgbscale, alphascale;
116         int combine;
117         int combinergb, combinealpha;
118         // texmatrixenabled exists only to avoid unnecessary texmatrix compares
119         int texmatrixenabled;
120         matrix4x4_t matrix;
121 }
122 gltextureunit_t;
123
124 typedef struct gl_state_s
125 {
126         int cullface;
127         int cullfaceenable;
128         int blendfunc1;
129         int blendfunc2;
130         qboolean blend;
131         GLboolean depthmask;
132         int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
133         int depthtest;
134         int depthfunc;
135         float depthrange[2];
136         float polygonoffset[2];
137         int alphatest;
138         int alphafunc;
139         float alphafuncvalue;
140         int scissortest;
141         unsigned int unit;
142         unsigned int clientunit;
143         gltextureunit_t units[MAX_TEXTUREUNITS];
144         float color4f[4];
145         int lockrange_first;
146         int lockrange_count;
147         int vertexbufferobject;
148         int elementbufferobject;
149         int framebufferobject;
150         qboolean pointer_color_enabled;
151
152         int pointer_vertex_components;
153         int pointer_vertex_gltype;
154         size_t pointer_vertex_stride;
155         const void *pointer_vertex_pointer;
156         const r_meshbuffer_t *pointer_vertex_vertexbuffer;
157         size_t pointer_vertex_offset;
158
159         int pointer_color_components;
160         int pointer_color_gltype;
161         size_t pointer_color_stride;
162         const void *pointer_color_pointer;
163         const r_meshbuffer_t *pointer_color_vertexbuffer;
164         size_t pointer_color_offset;
165
166         void *preparevertices_tempdata;
167         size_t preparevertices_tempdatamaxsize;
168         r_meshbuffer_t *preparevertices_dynamicvertexbuffer;
169         r_vertexposition_t *preparevertices_vertexposition;
170         r_vertexgeneric_t *preparevertices_vertexgeneric;
171         r_vertexmesh_t *preparevertices_vertexmesh;
172         int preparevertices_numvertices;
173
174         r_meshbuffer_t *draw_dynamicindexbuffer;
175
176         qboolean usevbo_staticvertex;
177         qboolean usevbo_staticindex;
178         qboolean usevbo_dynamicvertex;
179         qboolean usevbo_dynamicindex;
180
181         memexpandablearray_t meshbufferarray;
182
183         qboolean active;
184
185 #ifdef SUPPORTD3D
186         rtexture_t *d3drt_depthtexture;
187         rtexture_t *d3drt_colortextures[MAX_RENDERTARGETS];
188         IDirect3DSurface9 *d3drt_depthsurface;
189         IDirect3DSurface9 *d3drt_colorsurfaces[MAX_RENDERTARGETS];
190         IDirect3DSurface9 *d3drt_backbufferdepthsurface;
191         IDirect3DSurface9 *d3drt_backbuffercolorsurface;
192 #endif
193 }
194 gl_state_t;
195
196 static gl_state_t gl_state;
197
198
199 /*
200 note: here's strip order for a terrain row:
201 0--1--2--3--4
202 |\ |\ |\ |\ |
203 | \| \| \| \|
204 A--B--C--D--E
205 clockwise
206
207 A0B, 01B, B1C, 12C, C2D, 23D, D3E, 34E
208
209 *elements++ = i + row;
210 *elements++ = i;
211 *elements++ = i + row + 1;
212 *elements++ = i;
213 *elements++ = i + 1;
214 *elements++ = i + row + 1;
215
216
217 for (y = 0;y < rows - 1;y++)
218 {
219         for (x = 0;x < columns - 1;x++)
220         {
221                 i = y * rows + x;
222                 *elements++ = i + columns;
223                 *elements++ = i;
224                 *elements++ = i + columns + 1;
225                 *elements++ = i;
226                 *elements++ = i + 1;
227                 *elements++ = i + columns + 1;
228         }
229 }
230
231 alternative:
232 0--1--2--3--4
233 | /| /|\ | /|
234 |/ |/ | \|/ |
235 A--B--C--D--E
236 counterclockwise
237
238 for (y = 0;y < rows - 1;y++)
239 {
240         for (x = 0;x < columns - 1;x++)
241         {
242                 i = y * rows + x;
243                 *elements++ = i;
244                 *elements++ = i + columns;
245                 *elements++ = i + columns + 1;
246                 *elements++ = i + columns;
247                 *elements++ = i + columns + 1;
248                 *elements++ = i + 1;
249         }
250 }
251 */
252
253 int polygonelement3i[(POLYGONELEMENTS_MAXPOINTS-2)*3];
254 unsigned short polygonelement3s[(POLYGONELEMENTS_MAXPOINTS-2)*3];
255 int quadelement3i[QUADELEMENTS_MAXQUADS*6];
256 unsigned short quadelement3s[QUADELEMENTS_MAXQUADS*6];
257
258 void GL_VBOStats_f(void)
259 {
260         GL_Mesh_ListVBOs(true);
261 }
262
263 static void GL_Backend_ResetState(void);
264
265 static void R_Mesh_InitVertexDeclarations(void);
266 static void R_Mesh_DestroyVertexDeclarations(void);
267
268 static void gl_backend_start(void)
269 {
270         memset(&gl_state, 0, sizeof(gl_state));
271
272         R_Mesh_InitVertexDeclarations();
273
274         gl_state.usevbo_staticvertex = (vid.support.arb_vertex_buffer_object && gl_vbo.integer) || vid.forcevbo;
275         gl_state.usevbo_staticindex = (vid.support.arb_vertex_buffer_object && (gl_vbo.integer == 1 || gl_vbo.integer == 3)) || vid.forcevbo;
276         gl_state.usevbo_dynamicvertex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicvertex.integer) || vid.forcevbo;
277         gl_state.usevbo_dynamicindex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicindex.integer) || vid.forcevbo;
278         Mem_ExpandableArray_NewArray(&gl_state.meshbufferarray, r_main_mempool, sizeof(r_meshbuffer_t), 128);
279
280         Con_DPrintf("OpenGL backend started.\n");
281
282         CHECKGLERROR
283
284         GL_Backend_ResetState();
285
286         switch(vid.renderpath)
287         {
288         case RENDERPATH_GL11:
289         case RENDERPATH_GL13:
290         case RENDERPATH_GL20:
291         case RENDERPATH_CGGL:
292                 break;
293         case RENDERPATH_D3D9:
294 #ifdef SUPPORTD3D
295                 IDirect3DDevice9_GetDepthStencilSurface(vid_d3d9dev, &gl_state.d3drt_backbufferdepthsurface);
296                 IDirect3DDevice9_GetRenderTarget(vid_d3d9dev, 0, &gl_state.d3drt_backbuffercolorsurface);
297 #endif
298                 break;
299         case RENDERPATH_D3D10:
300                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
301                 break;
302         case RENDERPATH_D3D11:
303                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
304                 break;
305         }
306 }
307
308 static void gl_backend_shutdown(void)
309 {
310         Con_DPrint("OpenGL Backend shutting down\n");
311
312         switch(vid.renderpath)
313         {
314         case RENDERPATH_GL11:
315         case RENDERPATH_GL13:
316         case RENDERPATH_GL20:
317         case RENDERPATH_CGGL:
318                 break;
319         case RENDERPATH_D3D9:
320 #ifdef SUPPORTD3D
321                 IDirect3DSurface9_Release(gl_state.d3drt_backbufferdepthsurface);
322                 IDirect3DSurface9_Release(gl_state.d3drt_backbuffercolorsurface);
323 #endif
324                 break;
325         case RENDERPATH_D3D10:
326                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
327                 break;
328         case RENDERPATH_D3D11:
329                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
330                 break;
331         }
332
333         if (gl_state.preparevertices_tempdata)
334                 Mem_Free(gl_state.preparevertices_tempdata);
335         if (gl_state.preparevertices_dynamicvertexbuffer)
336                 R_Mesh_DestroyMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer);
337
338         Mem_ExpandableArray_FreeArray(&gl_state.meshbufferarray);
339
340         R_Mesh_DestroyVertexDeclarations();
341
342         memset(&gl_state, 0, sizeof(gl_state));
343 }
344
345 static void gl_backend_newmap(void)
346 {
347 }
348
349 static void gl_backend_devicelost(void)
350 {
351         int i, endindex;
352         r_meshbuffer_t *buffer;
353         endindex = Mem_ExpandableArray_IndexRange(&gl_state.meshbufferarray);
354         for (i = 0;i < endindex;i++)
355         {
356                 buffer = (r_meshbuffer_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.meshbufferarray, i);
357                 if (!buffer || !buffer->isdynamic)
358                         continue;
359                 switch(vid.renderpath)
360                 {
361                 case RENDERPATH_GL11:
362                 case RENDERPATH_GL13:
363                 case RENDERPATH_GL20:
364                 case RENDERPATH_CGGL:
365                         break;
366                 case RENDERPATH_D3D9:
367 #ifdef SUPPORTD3D
368                         if (buffer->devicebuffer)
369                         {
370                                 if (buffer->isindexbuffer)
371                                         IDirect3DIndexBuffer9_Release((IDirect3DIndexBuffer9*)buffer->devicebuffer);
372                                 else
373                                         IDirect3DVertexBuffer9_Release((IDirect3DVertexBuffer9*)buffer->devicebuffer);
374                                 buffer->devicebuffer = NULL;
375                         }
376 #endif
377                         break;
378                 case RENDERPATH_D3D10:
379                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
380                         break;
381                 case RENDERPATH_D3D11:
382                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
383                         break;
384                 }
385         }
386 }
387
388 static void gl_backend_devicerestored(void)
389 {
390 }
391
392 void gl_backend_init(void)
393 {
394         int i;
395
396         for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
397         {
398                 polygonelement3s[i * 3 + 0] = 0;
399                 polygonelement3s[i * 3 + 1] = i + 1;
400                 polygonelement3s[i * 3 + 2] = i + 2;
401         }
402         // elements for rendering a series of quads as triangles
403         for (i = 0;i < QUADELEMENTS_MAXQUADS;i++)
404         {
405                 quadelement3s[i * 6 + 0] = i * 4;
406                 quadelement3s[i * 6 + 1] = i * 4 + 1;
407                 quadelement3s[i * 6 + 2] = i * 4 + 2;
408                 quadelement3s[i * 6 + 3] = i * 4;
409                 quadelement3s[i * 6 + 4] = i * 4 + 2;
410                 quadelement3s[i * 6 + 5] = i * 4 + 3;
411         }
412
413         for (i = 0;i < (POLYGONELEMENTS_MAXPOINTS - 2)*3;i++)
414                 polygonelement3i[i] = polygonelement3s[i];
415         for (i = 0;i < QUADELEMENTS_MAXQUADS*3;i++)
416                 quadelement3i[i] = quadelement3s[i];
417
418         Cvar_RegisterVariable(&r_render);
419         Cvar_RegisterVariable(&r_renderview);
420         Cvar_RegisterVariable(&r_waterwarp);
421         Cvar_RegisterVariable(&gl_polyblend);
422         Cvar_RegisterVariable(&v_flipped);
423         Cvar_RegisterVariable(&gl_dither);
424         Cvar_RegisterVariable(&gl_vbo);
425         Cvar_RegisterVariable(&gl_vbo_dynamicvertex);
426         Cvar_RegisterVariable(&gl_vbo_dynamicindex);
427         Cvar_RegisterVariable(&gl_paranoid);
428         Cvar_RegisterVariable(&gl_printcheckerror);
429
430         Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
431         Cvar_RegisterVariable(&gl_mesh_testmanualfeeding);
432         Cvar_RegisterVariable(&gl_mesh_prefer_short_elements);
433         Cvar_RegisterVariable(&gl_mesh_separatearrays);
434
435         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");
436
437         R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap, gl_backend_devicelost, gl_backend_devicerestored);
438 }
439
440 void GL_SetMirrorState(qboolean state);
441
442 void R_Viewport_TransformToScreen(const r_viewport_t *v, const vec4_t in, vec4_t out)
443 {
444         vec4_t temp;
445         float iw;
446         Matrix4x4_Transform4 (&v->viewmatrix, in, temp);
447         Matrix4x4_Transform4 (&v->projectmatrix, temp, out);
448         iw = 1.0f / out[3];
449         out[0] = v->x + (out[0] * iw + 1.0f) * v->width * 0.5f;
450         out[1] = v->y + v->height - (out[1] * iw + 1.0f) * v->height * 0.5f;
451         out[2] = v->z + (out[2] * iw + 1.0f) * v->depth * 0.5f;
452 }
453
454 static void R_Viewport_ApplyNearClipPlaneFloatGL(const r_viewport_t *v, float *m, float normalx, float normaly, float normalz, float dist)
455 {
456         float q[4];
457         float d;
458         float clipPlane[4], v3[3], v4[3];
459         float normal[3];
460
461         // This is inspired by Oblique Depth Projection from http://www.terathon.com/code/oblique.php
462
463         VectorSet(normal, normalx, normaly, normalz);
464         Matrix4x4_Transform3x3(&v->viewmatrix, normal, clipPlane);
465         VectorScale(normal, dist, v3);
466         Matrix4x4_Transform(&v->viewmatrix, v3, v4);
467         // FIXME: LordHavoc: I think this can be done more efficiently somehow but I can't remember the technique
468         clipPlane[3] = -DotProduct(v4, clipPlane);
469
470 #if 0
471 {
472         // testing code for comparing results
473         float clipPlane2[4];
474         VectorCopy4(clipPlane, clipPlane2);
475         R_EntityMatrix(&identitymatrix);
476         VectorSet(q, normal[0], normal[1], normal[2], -dist);
477         qglClipPlane(GL_CLIP_PLANE0, q);
478         qglGetClipPlane(GL_CLIP_PLANE0, q);
479         VectorCopy4(q, clipPlane);
480 }
481 #endif
482
483         // Calculate the clip-space corner point opposite the clipping plane
484         // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
485         // transform it into camera space by multiplying it
486         // by the inverse of the projection matrix
487         q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + m[8]) / m[0];
488         q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + m[9]) / m[5];
489         q[2] = -1.0f;
490         q[3] = (1.0f + m[10]) / m[14];
491
492         // Calculate the scaled plane vector
493         d = 2.0f / DotProduct4(clipPlane, q);
494
495         // Replace the third row of the projection matrix
496         m[2] = clipPlane[0] * d;
497         m[6] = clipPlane[1] * d;
498         m[10] = clipPlane[2] * d + 1.0f;
499         m[14] = clipPlane[3] * d;
500 }
501
502 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)
503 {
504         float left = x1, right = x2, bottom = y2, top = y1, zNear = nearclip, zFar = farclip;
505         float m[16];
506         memset(v, 0, sizeof(*v));
507         v->type = R_VIEWPORTTYPE_ORTHO;
508         v->cameramatrix = *cameramatrix;
509         v->x = x;
510         v->y = y;
511         v->z = 0;
512         v->width = width;
513         v->height = height;
514         v->depth = 1;
515         memset(m, 0, sizeof(m));
516         m[0]  = 2/(right - left);
517         m[5]  = 2/(top - bottom);
518         m[10] = -2/(zFar - zNear);
519         m[12] = - (right + left)/(right - left);
520         m[13] = - (top + bottom)/(top - bottom);
521         m[14] = - (zFar + zNear)/(zFar - zNear);
522         m[15] = 1;
523         switch(vid.renderpath)
524         {
525         case RENDERPATH_GL11:
526         case RENDERPATH_GL13:
527         case RENDERPATH_GL20:
528         case RENDERPATH_CGGL:
529                 break;
530         case RENDERPATH_D3D9:
531         case RENDERPATH_D3D10:
532         case RENDERPATH_D3D11:
533                 m[10] = -1/(zFar - zNear);
534                 m[14] = -zNear/(zFar-zNear);
535                 break;
536         }
537         v->screentodepth[0] = -farclip / (farclip - nearclip);
538         v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
539
540         Matrix4x4_Invert_Full(&v->viewmatrix, &v->cameramatrix);
541
542         if (nearplane)
543                 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
544
545         Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
546
547 #if 0
548         {
549                 vec4_t test1;
550                 vec4_t test2;
551                 Vector4Set(test1, (x1+x2)*0.5f, (y1+y2)*0.5f, 0.0f, 1.0f);
552                 R_Viewport_TransformToScreen(v, test1, test2);
553                 Con_Printf("%f %f %f -> %f %f %f\n", test1[0], test1[1], test1[2], test2[0], test2[1], test2[2]);
554         }
555 #endif
556 }
557
558 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)
559 {
560         matrix4x4_t tempmatrix, basematrix;
561         float m[16];
562         memset(v, 0, sizeof(*v));
563
564         v->type = R_VIEWPORTTYPE_PERSPECTIVE;
565         v->cameramatrix = *cameramatrix;
566         v->x = x;
567         v->y = y;
568         v->z = 0;
569         v->width = width;
570         v->height = height;
571         v->depth = 1;
572         memset(m, 0, sizeof(m));
573         m[0]  = 1.0 / frustumx;
574         m[5]  = 1.0 / frustumy;
575         m[10] = -(farclip + nearclip) / (farclip - nearclip);
576         m[11] = -1;
577         m[14] = -2 * nearclip * farclip / (farclip - nearclip);
578         v->screentodepth[0] = -farclip / (farclip - nearclip);
579         v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
580
581         Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
582         Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
583         Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
584         Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
585
586         if (nearplane)
587                 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
588
589         if(v_flipped.integer)
590         {
591                 m[0] = -m[0];
592                 m[4] = -m[4];
593                 m[8] = -m[8];
594                 m[12] = -m[12];
595         }
596
597         Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
598 }
599
600 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)
601 {
602         matrix4x4_t tempmatrix, basematrix;
603         const float nudge = 1.0 - 1.0 / (1<<23);
604         float m[16];
605         memset(v, 0, sizeof(*v));
606
607         v->type = R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP;
608         v->cameramatrix = *cameramatrix;
609         v->x = x;
610         v->y = y;
611         v->z = 0;
612         v->width = width;
613         v->height = height;
614         v->depth = 1;
615         memset(m, 0, sizeof(m));
616         m[ 0] = 1.0 / frustumx;
617         m[ 5] = 1.0 / frustumy;
618         m[10] = -nudge;
619         m[11] = -1;
620         m[14] = -2 * nearclip * nudge;
621         v->screentodepth[0] = (m[10] + 1) * 0.5 - 1;
622         v->screentodepth[1] = m[14] * -0.5;
623
624         Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
625         Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
626         Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
627         Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
628
629         if (nearplane)
630                 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
631
632         if(v_flipped.integer)
633         {
634                 m[0] = -m[0];
635                 m[4] = -m[4];
636                 m[8] = -m[8];
637                 m[12] = -m[12];
638         }
639
640         Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
641 }
642
643 float cubeviewmatrix[6][16] =
644 {
645     // standard cubemap projections
646     { // +X
647          0, 0,-1, 0,
648          0,-1, 0, 0,
649         -1, 0, 0, 0,
650          0, 0, 0, 1,
651     },
652     { // -X
653          0, 0, 1, 0,
654          0,-1, 0, 0,
655          1, 0, 0, 0,
656          0, 0, 0, 1,
657     },
658     { // +Y
659          1, 0, 0, 0,
660          0, 0,-1, 0,
661          0, 1, 0, 0,
662          0, 0, 0, 1,
663     },
664     { // -Y
665          1, 0, 0, 0,
666          0, 0, 1, 0,
667          0,-1, 0, 0,
668          0, 0, 0, 1,
669     },
670     { // +Z
671          1, 0, 0, 0,
672          0,-1, 0, 0,
673          0, 0,-1, 0,
674          0, 0, 0, 1,
675     },
676     { // -Z
677         -1, 0, 0, 0,
678          0,-1, 0, 0,
679          0, 0, 1, 0,
680          0, 0, 0, 1,
681     },
682 };
683 float rectviewmatrix[6][16] =
684 {
685     // sign-preserving cubemap projections
686     { // +X
687          0, 0,-1, 0,
688          0, 1, 0, 0,
689          1, 0, 0, 0,
690          0, 0, 0, 1,
691     },
692     { // -X
693          0, 0, 1, 0,
694          0, 1, 0, 0,
695          1, 0, 0, 0,
696          0, 0, 0, 1,
697     },
698     { // +Y
699          1, 0, 0, 0,
700          0, 0,-1, 0,
701          0, 1, 0, 0,
702          0, 0, 0, 1,
703     },
704     { // -Y
705          1, 0, 0, 0,
706          0, 0, 1, 0,
707          0, 1, 0, 0,
708          0, 0, 0, 1,
709     },
710     { // +Z
711          1, 0, 0, 0,
712          0, 1, 0, 0,
713          0, 0,-1, 0,
714          0, 0, 0, 1,
715     },
716     { // -Z
717          1, 0, 0, 0,
718          0, 1, 0, 0,
719          0, 0, 1, 0,
720          0, 0, 0, 1,
721     },
722 };
723
724 void R_Viewport_InitCubeSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, float nearclip, float farclip, const float *nearplane)
725 {
726         matrix4x4_t tempmatrix, basematrix;
727         float m[16];
728         memset(v, 0, sizeof(*v));
729         v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
730         v->cameramatrix = *cameramatrix;
731         v->width = size;
732         v->height = size;
733         v->depth = 1;
734         memset(m, 0, sizeof(m));
735         m[0] = m[5] = 1.0f;
736         m[10] = -(farclip + nearclip) / (farclip - nearclip);
737         m[11] = -1;
738         m[14] = -2 * nearclip * farclip / (farclip - nearclip);
739
740         Matrix4x4_FromArrayFloatGL(&basematrix, cubeviewmatrix[side]);
741         Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
742         Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
743
744         if (nearplane)
745                 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
746
747         Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
748 }
749
750 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)
751 {
752         matrix4x4_t tempmatrix, basematrix;
753         float m[16];
754         memset(v, 0, sizeof(*v));
755         v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
756         v->cameramatrix = *cameramatrix;
757         v->x = (side & 1) * size;
758         v->y = (side >> 1) * size;
759         v->width = size;
760         v->height = size;
761         v->depth = 1;
762         memset(m, 0, sizeof(m));
763         m[0] = m[5] = 1.0f * ((float)size - border) / size;
764         m[10] = -(farclip + nearclip) / (farclip - nearclip);
765         m[11] = -1;
766         m[14] = -2 * nearclip * farclip / (farclip - nearclip);
767
768         Matrix4x4_FromArrayFloatGL(&basematrix, rectviewmatrix[side]);
769         Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
770         Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
771
772         switch(vid.renderpath)
773         {
774         case RENDERPATH_GL20:
775         case RENDERPATH_CGGL:
776         case RENDERPATH_GL13:
777         case RENDERPATH_GL11:
778                 break;
779         case RENDERPATH_D3D9:
780                 m[5] *= -1;
781                 break;
782         case RENDERPATH_D3D10:
783                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
784                 break;
785         case RENDERPATH_D3D11:
786                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
787                 break;
788         }
789
790         if (nearplane)
791                 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
792
793         Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
794 }
795
796 void R_SetViewport(const r_viewport_t *v)
797 {
798         float m[16];
799         gl_viewport = *v;
800
801         // FIXME: v_flipped_state is evil, this probably breaks somewhere
802         GL_SetMirrorState(v_flipped.integer && (v->type == R_VIEWPORTTYPE_PERSPECTIVE || v->type == R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP));
803
804         // copy over the matrices to our state
805         gl_viewmatrix = v->viewmatrix;
806         gl_projectionmatrix = v->projectmatrix;
807
808         switch(vid.renderpath)
809         {
810         case RENDERPATH_GL20:
811         case RENDERPATH_CGGL:
812 //              CHECKGLERROR
813 //              qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
814 //              break;
815         case RENDERPATH_GL13:
816         case RENDERPATH_GL11:
817                 CHECKGLERROR
818                 qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
819                 // Load the projection matrix into OpenGL
820                 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
821                 Matrix4x4_ToArrayFloatGL(&gl_projectionmatrix, m);
822                 qglLoadMatrixf(m);CHECKGLERROR
823                 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
824                 break;
825         case RENDERPATH_D3D9:
826 #ifdef SUPPORTD3D
827                 {
828                         D3DVIEWPORT9 d3dviewport;
829                         d3dviewport.X = gl_viewport.x;
830                         d3dviewport.Y = gl_viewport.y;
831                         d3dviewport.Width = gl_viewport.width;
832                         d3dviewport.Height = gl_viewport.height;
833                         d3dviewport.MinZ = gl_state.depthrange[0];
834                         d3dviewport.MaxZ = gl_state.depthrange[1];
835                         IDirect3DDevice9_SetViewport(vid_d3d9dev, &d3dviewport);
836                 }
837 #endif
838                 break;
839         case RENDERPATH_D3D10:
840                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
841                 break;
842         case RENDERPATH_D3D11:
843                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
844                 break;
845         }
846
847         // force an update of the derived matrices
848         gl_modelmatrixchanged = true;
849         R_EntityMatrix(&gl_modelmatrix);
850 }
851
852 void R_GetViewport(r_viewport_t *v)
853 {
854         *v = gl_viewport;
855 }
856
857 static void GL_BindVBO(int bufferobject)
858 {
859         if (gl_state.vertexbufferobject != bufferobject)
860         {
861                 gl_state.vertexbufferobject = bufferobject;
862                 CHECKGLERROR
863                 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferobject);CHECKGLERROR
864         }
865 }
866
867 static void GL_BindEBO(int bufferobject)
868 {
869         if (gl_state.elementbufferobject != bufferobject)
870         {
871                 gl_state.elementbufferobject = bufferobject;
872                 CHECKGLERROR
873                 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferobject);CHECKGLERROR
874         }
875 }
876
877 int R_Mesh_CreateFramebufferObject(rtexture_t *depthtexture, rtexture_t *colortexture, rtexture_t *colortexture2, rtexture_t *colortexture3, rtexture_t *colortexture4)
878 {
879         int temp;
880         switch(vid.renderpath)
881         {
882         case RENDERPATH_GL11:
883         case RENDERPATH_GL13:
884         case RENDERPATH_GL20:
885         case RENDERPATH_CGGL:
886                 if (!vid.support.ext_framebuffer_object)
887                         return 0;
888                 qglGenFramebuffersEXT(1, (GLuint*)&temp);CHECKGLERROR
889                 R_Mesh_SetRenderTargets(temp, NULL, NULL, NULL, NULL, NULL);
890                 if (depthtexture) qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, depthtexture->gltexturetypeenum, R_GetTexture(depthtexture), 0);CHECKGLERROR
891                 if (colortexture) qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, colortexture->gltexturetypeenum, R_GetTexture(colortexture), 0);CHECKGLERROR
892                 if (colortexture2) qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, colortexture2->gltexturetypeenum, R_GetTexture(colortexture2), 0);CHECKGLERROR
893                 if (colortexture3) qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, colortexture3->gltexturetypeenum, R_GetTexture(colortexture3), 0);CHECKGLERROR
894                 if (colortexture4) qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT3_EXT, colortexture4->gltexturetypeenum, R_GetTexture(colortexture4), 0);CHECKGLERROR
895                 return temp;
896         case RENDERPATH_D3D9:
897         case RENDERPATH_D3D10:
898         case RENDERPATH_D3D11:
899                 return 1;
900         }
901         return 0;
902 }
903
904 void R_Mesh_DestroyFramebufferObject(int fbo)
905 {
906         switch(vid.renderpath)
907         {
908         case RENDERPATH_GL11:
909         case RENDERPATH_GL13:
910         case RENDERPATH_GL20:
911         case RENDERPATH_CGGL:
912                 if (fbo)
913                         qglDeleteFramebuffersEXT(1, (GLuint*)&fbo);
914                 break;
915         case RENDERPATH_D3D9:
916         case RENDERPATH_D3D10:
917         case RENDERPATH_D3D11:
918                 break;
919         }
920 }
921
922 void R_Mesh_ResetRenderTargets(void)
923 {
924         switch(vid.renderpath)
925         {
926         case RENDERPATH_GL11:
927         case RENDERPATH_GL13:
928         case RENDERPATH_GL20:
929         case RENDERPATH_CGGL:
930                 if (gl_state.framebufferobject)
931                 {
932                         gl_state.framebufferobject = 0;
933                         qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, gl_state.framebufferobject);
934                 }
935                 break;
936         case RENDERPATH_D3D9:
937 #ifdef SUPPORTD3D
938                 if (gl_state.framebufferobject)
939                 {
940                         unsigned int i;
941                         gl_state.framebufferobject = 0;
942                         IDirect3DDevice9_SetDepthStencilSurface(vid_d3d9dev, gl_state.d3drt_backbufferdepthsurface);
943                         IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 0, gl_state.d3drt_backbuffercolorsurface);
944                         gl_state.d3drt_depthsurface = NULL;
945                         for (i = 1;i < vid.maxdrawbuffers;i++)
946                         {
947                                 if (gl_state.d3drt_colorsurfaces[i])
948                                 {
949                                         gl_state.d3drt_colorsurfaces[i] = NULL;
950                                         IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, i, NULL);
951                                 }
952                         }
953                 }
954 #endif
955                 break;
956         case RENDERPATH_D3D10:
957                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
958                 break;
959         case RENDERPATH_D3D11:
960                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
961                 break;
962         }
963 }
964
965 void R_Mesh_SetRenderTargets(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture, rtexture_t *colortexture2, rtexture_t *colortexture3, rtexture_t *colortexture4)
966 {
967         unsigned int i;
968         unsigned int j;
969         rtexture_t *textures[5];
970         Vector4Set(textures, colortexture, colortexture2, colortexture3, colortexture4);
971         textures[4] = depthtexture;
972         // unbind any matching textures immediately, otherwise D3D will complain about a bound texture being used as a render target
973         for (j = 0;j < 5;j++)
974                 if (textures[j])
975                         for (i = 0;i < vid.teximageunits;i++)
976                                 if (gl_state.units[i].texture == textures[j])
977                                         R_Mesh_TexBind(i, NULL);
978         // set up framebuffer object or render targets for the active rendering API
979         switch(vid.renderpath)
980         {
981         case RENDERPATH_GL11:
982         case RENDERPATH_GL13:
983         case RENDERPATH_GL20:
984         case RENDERPATH_CGGL:
985                 if (gl_state.framebufferobject != fbo)
986                 {
987                         gl_state.framebufferobject = fbo;
988                         qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, gl_state.framebufferobject);
989                 }
990                 break;
991         case RENDERPATH_D3D9:
992 #ifdef SUPPORTD3D
993                 // set up the new render targets, a NULL depthtexture intentionally binds nothing
994                 // TODO: optimize: keep surface pointer around in rtexture_t until texture is freed or lost
995                 if (fbo)
996                 {
997                         gl_state.framebufferobject = 1;
998                         gl_state.d3drt_depthtexture = depthtexture;
999                         if (gl_state.d3drt_depthtexture)
1000                         {
1001                                 IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9 *)gl_state.d3drt_depthtexture->d3dtexture, 0, &gl_state.d3drt_depthsurface);
1002                                 IDirect3DDevice9_SetDepthStencilSurface(vid_d3d9dev, gl_state.d3drt_depthsurface);
1003                                 IDirect3DSurface9_Release(gl_state.d3drt_depthsurface);
1004                         }
1005                         else
1006                                 IDirect3DDevice9_SetDepthStencilSurface(vid_d3d9dev, NULL);
1007                         for (i = 0;i < vid.maxdrawbuffers;i++)
1008                         {
1009                                 gl_state.d3drt_colortextures[i] = textures[i];
1010                                 if (gl_state.d3drt_colortextures[i])
1011                                 {
1012                                         IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9 *)gl_state.d3drt_colortextures[i]->d3dtexture, 0, &gl_state.d3drt_colorsurfaces[i]);
1013                                         IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, i, gl_state.d3drt_colorsurfaces[i]);
1014                                         IDirect3DSurface9_Release(gl_state.d3drt_colorsurfaces[i]);
1015                                 }
1016                                 else
1017                                         IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, i, NULL);
1018                         }
1019                 }
1020                 else
1021                         R_Mesh_ResetRenderTargets();
1022 #endif
1023                 break;
1024         case RENDERPATH_D3D10:
1025                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1026                 break;
1027         case RENDERPATH_D3D11:
1028                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1029                 break;
1030         }
1031 }
1032
1033 #ifdef SUPPORTD3D
1034 static int d3dcmpforglfunc(int f)
1035 {
1036         switch(f)
1037         {
1038         case GL_NEVER: return D3DCMP_NEVER;
1039         case GL_LESS: return D3DCMP_LESS;
1040         case GL_EQUAL: return D3DCMP_EQUAL;
1041         case GL_LEQUAL: return D3DCMP_LESSEQUAL;
1042         case GL_GREATER: return D3DCMP_GREATER;
1043         case GL_NOTEQUAL: return D3DCMP_NOTEQUAL;
1044         case GL_GEQUAL: return D3DCMP_GREATEREQUAL;
1045         case GL_ALWAYS: return D3DCMP_ALWAYS;
1046         default: Con_DPrintf("Unknown GL_DepthFunc\n");return D3DCMP_ALWAYS;
1047         }
1048 }
1049
1050 static int d3dstencilopforglfunc(int f)
1051 {
1052         switch(f)
1053         {
1054         case GL_KEEP: return D3DSTENCILOP_KEEP;
1055         case GL_INCR: return D3DSTENCILOP_INCR; // note: GL_INCR is clamped, D3DSTENCILOP_INCR wraps
1056         case GL_DECR: return D3DSTENCILOP_DECR; // note: GL_DECR is clamped, D3DSTENCILOP_DECR wraps
1057         default: Con_DPrintf("Unknown GL_StencilFunc\n");return D3DSTENCILOP_KEEP;
1058         }
1059 }
1060 #endif
1061
1062
1063 static void GL_Backend_ResetState(void)
1064 {
1065         unsigned int i;
1066         gl_state.active = true;
1067         gl_state.depthtest = true;
1068         gl_state.alphatest = false;
1069         gl_state.alphafunc = GL_GEQUAL;
1070         gl_state.alphafuncvalue = 0.5f;
1071         gl_state.blendfunc1 = GL_ONE;
1072         gl_state.blendfunc2 = GL_ZERO;
1073         gl_state.blend = false;
1074         gl_state.depthmask = GL_TRUE;
1075         gl_state.colormask = 15;
1076         gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
1077         gl_state.lockrange_first = 0;
1078         gl_state.lockrange_count = 0;
1079         gl_state.cullface = GL_NONE;
1080         gl_state.cullfaceenable = false;
1081         gl_state.polygonoffset[0] = 0;
1082         gl_state.polygonoffset[1] = 0;
1083         gl_state.framebufferobject = 0;
1084         gl_state.depthfunc = GL_LEQUAL;
1085
1086         switch(vid.renderpath)
1087         {
1088         case RENDERPATH_D3D9:
1089 #ifdef SUPPORTD3D
1090                 {
1091                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_COLORWRITEENABLE, gl_state.colormask);
1092                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHATESTENABLE, gl_state.alphatest);
1093                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHAFUNC, d3dcmpforglfunc(gl_state.alphafunc));
1094                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHAREF, (int)bound(0, gl_state.alphafuncvalue * 256.0f, 255));
1095                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_NONE);
1096                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZFUNC, d3dcmpforglfunc(gl_state.depthfunc));
1097                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZENABLE, gl_state.depthtest);
1098                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZWRITEENABLE, gl_state.depthmask);
1099                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SLOPESCALEDEPTHBIAS, gl_state.polygonoffset[0]);
1100                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DEPTHBIAS, gl_state.polygonoffset[1] * (1.0f / 16777216.0f));
1101                 }
1102 #endif
1103                 break;
1104         case RENDERPATH_D3D10:
1105                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1106                 break;
1107         case RENDERPATH_D3D11:
1108                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1109                 break;
1110         case RENDERPATH_GL20:
1111         case RENDERPATH_CGGL:
1112                 CHECKGLERROR
1113
1114                 qglColorMask(1, 1, 1, 1);CHECKGLERROR
1115                 qglAlphaFunc(gl_state.alphafunc, gl_state.alphafuncvalue);CHECKGLERROR
1116                 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
1117                 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1118                 qglDisable(GL_BLEND);CHECKGLERROR
1119                 qglCullFace(gl_state.cullface);CHECKGLERROR
1120                 qglDisable(GL_CULL_FACE);CHECKGLERROR
1121                 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1122                 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1123                 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1124                 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1125
1126                 if (vid.support.arb_vertex_buffer_object)
1127                 {
1128                         qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
1129                         qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
1130                 }
1131
1132                 if (vid.support.ext_framebuffer_object)
1133                 {
1134                         qglBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
1135                         qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
1136                 }
1137
1138                 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
1139                 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1140
1141                 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
1142                 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1143                 qglColor4f(1, 1, 1, 1);CHECKGLERROR
1144
1145                 if (vid.support.ext_framebuffer_object)
1146                         qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, gl_state.framebufferobject);
1147
1148                 gl_state.unit = MAX_TEXTUREUNITS;
1149                 gl_state.clientunit = MAX_TEXTUREUNITS;
1150                 for (i = 0;i < vid.teximageunits;i++)
1151                 {
1152                         GL_ActiveTexture(i);
1153                         qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1154                         if (vid.support.ext_texture_3d)
1155                         {
1156                                 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1157                         }
1158                         if (vid.support.arb_texture_cube_map)
1159                         {
1160                                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
1161                         }
1162                         if (vid.support.arb_texture_rectangle)
1163                         {
1164                                 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
1165                         }
1166                 }
1167
1168                 for (i = 0;i < vid.texarrayunits;i++)
1169                 {
1170                         GL_ClientActiveTexture(i);
1171                         GL_BindVBO(0);
1172                         qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
1173                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1174                 }
1175                 CHECKGLERROR
1176                 break;
1177         case RENDERPATH_GL13:
1178         case RENDERPATH_GL11:
1179                 CHECKGLERROR
1180
1181                 qglColorMask(1, 1, 1, 1);CHECKGLERROR
1182                 qglAlphaFunc(gl_state.alphafunc, gl_state.alphafuncvalue);CHECKGLERROR
1183                 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
1184                 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1185                 qglDisable(GL_BLEND);CHECKGLERROR
1186                 qglCullFace(gl_state.cullface);CHECKGLERROR
1187                 qglDisable(GL_CULL_FACE);CHECKGLERROR
1188                 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1189                 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1190                 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1191                 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1192
1193                 if (vid.support.arb_vertex_buffer_object)
1194                 {
1195                         qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
1196                         qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
1197                 }
1198
1199                 if (vid.support.ext_framebuffer_object)
1200                 {
1201                         qglBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
1202                         qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
1203                 }
1204
1205                 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
1206                 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1207
1208                 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
1209                 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1210                 qglColor4f(1, 1, 1, 1);CHECKGLERROR
1211
1212                 if (vid.support.ext_framebuffer_object)
1213                         qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, gl_state.framebufferobject);
1214
1215                 gl_state.unit = MAX_TEXTUREUNITS;
1216                 gl_state.clientunit = MAX_TEXTUREUNITS;
1217                 for (i = 0;i < vid.texunits;i++)
1218                 {
1219                         GL_ActiveTexture(i);
1220                         GL_ClientActiveTexture(i);
1221                         qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1222                         qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1223                         if (vid.support.ext_texture_3d)
1224                         {
1225                                 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1226                                 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1227                         }
1228                         if (vid.support.arb_texture_cube_map)
1229                         {
1230                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1231                                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
1232                         }
1233                         if (vid.support.arb_texture_rectangle)
1234                         {
1235                                 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1236                                 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
1237                         }
1238                         GL_BindVBO(0);
1239                         qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
1240                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1241                         qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1242                         qglLoadIdentity();CHECKGLERROR
1243                         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1244                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
1245                 }
1246                 CHECKGLERROR
1247                 break;
1248         }
1249 }
1250
1251 void GL_ActiveTexture(unsigned int num)
1252 {
1253         if (gl_state.unit != num)
1254         {
1255                 gl_state.unit = num;
1256                 switch(vid.renderpath)
1257                 {
1258                 case RENDERPATH_GL11:
1259                 case RENDERPATH_GL13:
1260                 case RENDERPATH_GL20:
1261                 case RENDERPATH_CGGL:
1262                         if (qglActiveTexture)
1263                         {
1264                                 CHECKGLERROR
1265                                 qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
1266                                 CHECKGLERROR
1267                         }
1268                         break;
1269                 case RENDERPATH_D3D9:
1270                 case RENDERPATH_D3D10:
1271                 case RENDERPATH_D3D11:
1272                         break;
1273                 }
1274         }
1275 }
1276
1277 void GL_ClientActiveTexture(unsigned int num)
1278 {
1279         if (gl_state.clientunit != num)
1280         {
1281                 gl_state.clientunit = num;
1282                 switch(vid.renderpath)
1283                 {
1284                 case RENDERPATH_GL11:
1285                 case RENDERPATH_GL13:
1286                 case RENDERPATH_GL20:
1287                 case RENDERPATH_CGGL:
1288                         if (qglActiveTexture)
1289                         {
1290                                 CHECKGLERROR
1291                                 qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.clientunit);
1292                                 CHECKGLERROR
1293                         }
1294                         break;
1295                 case RENDERPATH_D3D9:
1296                 case RENDERPATH_D3D10:
1297                 case RENDERPATH_D3D11:
1298                         break;
1299                 }
1300         }
1301 }
1302
1303 void GL_BlendFunc(int blendfunc1, int blendfunc2)
1304 {
1305         if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
1306         {
1307                 qboolean blendenable;
1308                 gl_state.blendfunc1 = blendfunc1;
1309                 gl_state.blendfunc2 = blendfunc2;
1310                 blendenable = (gl_state.blendfunc1 != GL_ONE || gl_state.blendfunc2 != GL_ZERO);
1311                 switch(vid.renderpath)
1312                 {
1313                 case RENDERPATH_GL11:
1314                 case RENDERPATH_GL13:
1315                 case RENDERPATH_GL20:
1316                 case RENDERPATH_CGGL:
1317                         CHECKGLERROR
1318                         qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1319                         if (gl_state.blend != blendenable)
1320                         {
1321                                 gl_state.blend = blendenable;
1322                                 if (!gl_state.blend)
1323                                 {
1324                                         qglDisable(GL_BLEND);CHECKGLERROR
1325                                 }
1326                                 else
1327                                 {
1328                                         qglEnable(GL_BLEND);CHECKGLERROR
1329                                 }
1330                         }
1331                         break;
1332                 case RENDERPATH_D3D9:
1333 #ifdef SUPPORTD3D
1334                         {
1335                                 int i;
1336                                 int glblendfunc[2];
1337                                 D3DBLEND d3dblendfunc[2];
1338                                 glblendfunc[0] = gl_state.blendfunc1;
1339                                 glblendfunc[1] = gl_state.blendfunc2;
1340                                 for (i = 0;i < 2;i++)
1341                                 {
1342                                         switch(glblendfunc[i])
1343                                         {
1344                                         case GL_ZERO: d3dblendfunc[i] = D3DBLEND_ZERO;break;
1345                                         case GL_ONE: d3dblendfunc[i] = D3DBLEND_ONE;break;
1346                                         case GL_SRC_COLOR: d3dblendfunc[i] = D3DBLEND_SRCCOLOR;break;
1347                                         case GL_ONE_MINUS_SRC_COLOR: d3dblendfunc[i] = D3DBLEND_INVSRCCOLOR;break;
1348                                         case GL_SRC_ALPHA: d3dblendfunc[i] = D3DBLEND_SRCALPHA;break;
1349                                         case GL_ONE_MINUS_SRC_ALPHA: d3dblendfunc[i] = D3DBLEND_INVSRCALPHA;break;
1350                                         case GL_DST_ALPHA: d3dblendfunc[i] = D3DBLEND_DESTALPHA;break;
1351                                         case GL_ONE_MINUS_DST_ALPHA: d3dblendfunc[i] = D3DBLEND_INVDESTALPHA;break;
1352                                         case GL_DST_COLOR: d3dblendfunc[i] = D3DBLEND_DESTCOLOR;break;
1353                                         case GL_ONE_MINUS_DST_COLOR: d3dblendfunc[i] = D3DBLEND_INVDESTCOLOR;break;
1354                                         }
1355                                 }
1356                                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SRCBLEND, d3dblendfunc[0]);
1357                                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DESTBLEND, d3dblendfunc[1]);
1358                                 if (gl_state.blend != blendenable)
1359                                 {
1360                                         gl_state.blend = blendenable;
1361                                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHABLENDENABLE, gl_state.blend);
1362                                 }
1363                         }
1364 #endif
1365                         break;
1366                 case RENDERPATH_D3D10:
1367                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1368                         break;
1369                 case RENDERPATH_D3D11:
1370                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1371                         break;
1372                 }
1373         }
1374 }
1375
1376 void GL_DepthMask(int state)
1377 {
1378         if (gl_state.depthmask != state)
1379         {
1380                 gl_state.depthmask = state;
1381                 switch(vid.renderpath)
1382                 {
1383                 case RENDERPATH_GL11:
1384                 case RENDERPATH_GL13:
1385                 case RENDERPATH_GL20:
1386                 case RENDERPATH_CGGL:
1387                         CHECKGLERROR
1388                         qglDepthMask(gl_state.depthmask);CHECKGLERROR
1389                         break;
1390                 case RENDERPATH_D3D9:
1391 #ifdef SUPPORTD3D
1392                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZWRITEENABLE, gl_state.depthmask);
1393 #endif
1394                         break;
1395                 case RENDERPATH_D3D10:
1396                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1397                         break;
1398                 case RENDERPATH_D3D11:
1399                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1400                         break;
1401                 }
1402         }
1403 }
1404
1405 void GL_DepthTest(int state)
1406 {
1407         if (gl_state.depthtest != state)
1408         {
1409                 gl_state.depthtest = state;
1410                 switch(vid.renderpath)
1411                 {
1412                 case RENDERPATH_GL11:
1413                 case RENDERPATH_GL13:
1414                 case RENDERPATH_GL20:
1415                 case RENDERPATH_CGGL:
1416                         CHECKGLERROR
1417                         if (gl_state.depthtest)
1418                         {
1419                                 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1420                         }
1421                         else
1422                         {
1423                                 qglDisable(GL_DEPTH_TEST);CHECKGLERROR
1424                         }
1425                         break;
1426                 case RENDERPATH_D3D9:
1427 #ifdef SUPPORTD3D
1428                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZENABLE, gl_state.depthtest);
1429 #endif
1430                         break;
1431                 case RENDERPATH_D3D10:
1432                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1433                         break;
1434                 case RENDERPATH_D3D11:
1435                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1436                         break;
1437                 }
1438         }
1439 }
1440
1441 void GL_DepthFunc(int state)
1442 {
1443         if (gl_state.depthfunc != state)
1444         {
1445                 gl_state.depthfunc = state;
1446                 switch(vid.renderpath)
1447                 {
1448                 case RENDERPATH_GL11:
1449                 case RENDERPATH_GL13:
1450                 case RENDERPATH_GL20:
1451                 case RENDERPATH_CGGL:
1452                         CHECKGLERROR
1453                         qglDepthFunc(gl_state.depthfunc);CHECKGLERROR
1454                         break;
1455                 case RENDERPATH_D3D9:
1456 #ifdef SUPPORTD3D
1457                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZFUNC, d3dcmpforglfunc(gl_state.depthfunc));
1458 #endif
1459                         break;
1460                 case RENDERPATH_D3D10:
1461                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1462                         break;
1463                 case RENDERPATH_D3D11:
1464                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1465                         break;
1466                 }
1467         }
1468 }
1469
1470 void GL_DepthRange(float nearfrac, float farfrac)
1471 {
1472         if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
1473         {
1474                 gl_state.depthrange[0] = nearfrac;
1475                 gl_state.depthrange[1] = farfrac;
1476                 switch(vid.renderpath)
1477                 {
1478                 case RENDERPATH_GL11:
1479                 case RENDERPATH_GL13:
1480                 case RENDERPATH_GL20:
1481                 case RENDERPATH_CGGL:
1482                         qglDepthRange(gl_state.depthrange[0], gl_state.depthrange[1]);
1483                         break;
1484                 case RENDERPATH_D3D9:
1485 #ifdef SUPPORTD3D
1486                         {
1487                                 D3DVIEWPORT9 d3dviewport;
1488                                 d3dviewport.X = gl_viewport.x;
1489                                 d3dviewport.Y = gl_viewport.y;
1490                                 d3dviewport.Width = gl_viewport.width;
1491                                 d3dviewport.Height = gl_viewport.height;
1492                                 d3dviewport.MinZ = gl_state.depthrange[0];
1493                                 d3dviewport.MaxZ = gl_state.depthrange[1];
1494                                 IDirect3DDevice9_SetViewport(vid_d3d9dev, &d3dviewport);
1495                         }
1496 #endif
1497                         break;
1498                 case RENDERPATH_D3D10:
1499                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1500                         break;
1501                 case RENDERPATH_D3D11:
1502                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1503                         break;
1504                 }
1505         }
1506 }
1507
1508 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)
1509 {
1510         switch (vid.renderpath)
1511         {
1512         case RENDERPATH_GL11:
1513         case RENDERPATH_GL13:
1514         case RENDERPATH_GL20:
1515         case RENDERPATH_CGGL:
1516                 CHECKGLERROR
1517                 if (enable)
1518                 {
1519                         qglEnable(GL_STENCIL_TEST);CHECKGLERROR
1520                 }
1521                 else
1522                 {
1523                         qglDisable(GL_STENCIL_TEST);CHECKGLERROR
1524                 }
1525                 if (vid.support.ati_separate_stencil)
1526                 {
1527                         qglStencilMask(writemask);CHECKGLERROR
1528                         qglStencilOpSeparate(GL_FRONT, frontfail, frontzfail, frontzpass);CHECKGLERROR
1529                         qglStencilOpSeparate(GL_BACK, backfail, backzfail, backzpass);CHECKGLERROR
1530                         qglStencilFuncSeparate(frontcompare, backcompare, comparereference, comparereference);CHECKGLERROR
1531                 }
1532                 else if (vid.support.ext_stencil_two_side)
1533                 {
1534                         qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
1535                         qglActiveStencilFaceEXT(GL_FRONT);CHECKGLERROR
1536                         qglStencilMask(writemask);CHECKGLERROR
1537                         qglStencilOp(frontfail, frontzfail, frontzpass);CHECKGLERROR
1538                         qglStencilFunc(frontcompare, comparereference, comparemask);CHECKGLERROR
1539                         qglActiveStencilFaceEXT(GL_BACK);CHECKGLERROR
1540                         qglStencilMask(writemask);CHECKGLERROR
1541                         qglStencilOp(backfail, backzfail, backzpass);CHECKGLERROR
1542                         qglStencilFunc(backcompare, comparereference, comparemask);CHECKGLERROR
1543                 }
1544                 break;
1545         case RENDERPATH_D3D9:
1546 #ifdef SUPPORTD3D
1547                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_TWOSIDEDSTENCILMODE, true);
1548                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILENABLE, enable);
1549                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILWRITEMASK, writemask);
1550                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFAIL, d3dstencilopforglfunc(frontfail));
1551                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILZFAIL, d3dstencilopforglfunc(frontzfail));
1552                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILPASS, d3dstencilopforglfunc(frontzpass));
1553                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFUNC, frontcompare);
1554                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILFAIL, d3dstencilopforglfunc(backfail));
1555                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILZFAIL, d3dstencilopforglfunc(backzfail));
1556                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILPASS, d3dstencilopforglfunc(backzpass));
1557                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILFUNC, backcompare);
1558                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILREF, comparereference);
1559                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILMASK, comparemask);
1560 #endif
1561                 break;
1562         case RENDERPATH_D3D10:
1563                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1564                 break;
1565         case RENDERPATH_D3D11:
1566                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1567                 break;
1568         }
1569 }
1570
1571 void R_SetStencil(qboolean enable, int writemask, int fail, int zfail, int zpass, int compare, int comparereference, int comparemask)
1572 {
1573         switch (vid.renderpath)
1574         {
1575         case RENDERPATH_GL11:
1576         case RENDERPATH_GL13:
1577         case RENDERPATH_GL20:
1578         case RENDERPATH_CGGL:
1579                 CHECKGLERROR
1580                 if (enable)
1581                 {
1582                         qglEnable(GL_STENCIL_TEST);CHECKGLERROR
1583                 }
1584                 else
1585                 {
1586                         qglDisable(GL_STENCIL_TEST);CHECKGLERROR
1587                 }
1588                 if (vid.support.ext_stencil_two_side)
1589                 {
1590                         qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
1591                 }
1592                 qglStencilMask(writemask);CHECKGLERROR
1593                 qglStencilOp(fail, zfail, zpass);CHECKGLERROR
1594                 qglStencilFunc(compare, comparereference, comparemask);CHECKGLERROR
1595                 CHECKGLERROR
1596                 break;
1597         case RENDERPATH_D3D9:
1598 #ifdef SUPPORTD3D
1599                 if (vid.support.ati_separate_stencil)
1600                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_TWOSIDEDSTENCILMODE, true);
1601                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILENABLE, enable);
1602                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILWRITEMASK, writemask);
1603                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFAIL, d3dstencilopforglfunc(fail));
1604                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILZFAIL, d3dstencilopforglfunc(zfail));
1605                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILPASS, d3dstencilopforglfunc(zpass));
1606                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFUNC, compare);
1607                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILREF, comparereference);
1608                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILMASK, comparemask);
1609 #endif
1610                 break;
1611         case RENDERPATH_D3D10:
1612                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1613                 break;
1614         case RENDERPATH_D3D11:
1615                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1616                 break;
1617         }
1618 }
1619
1620 void GL_PolygonOffset(float planeoffset, float depthoffset)
1621 {
1622         if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
1623         {
1624                 gl_state.polygonoffset[0] = planeoffset;
1625                 gl_state.polygonoffset[1] = depthoffset;
1626                 switch(vid.renderpath)
1627                 {
1628                 case RENDERPATH_GL11:
1629                 case RENDERPATH_GL13:
1630                 case RENDERPATH_GL20:
1631                 case RENDERPATH_CGGL:
1632                         qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1633                         break;
1634                 case RENDERPATH_D3D9:
1635 #ifdef SUPPORTD3D
1636                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SLOPESCALEDEPTHBIAS, gl_state.polygonoffset[0]);
1637                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DEPTHBIAS, gl_state.polygonoffset[1] * (1.0f / 16777216.0f));
1638 #endif
1639                         break;
1640                 case RENDERPATH_D3D10:
1641                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1642                         break;
1643                 case RENDERPATH_D3D11:
1644                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1645                         break;
1646                 }
1647         }
1648 }
1649
1650 void GL_SetMirrorState(qboolean state)
1651 {
1652         if (v_flipped_state != state)
1653         {
1654                 v_flipped_state = state;
1655                 if (gl_state.cullface == GL_BACK)
1656                         gl_state.cullface = GL_FRONT;
1657                 else if (gl_state.cullface == GL_FRONT)
1658                         gl_state.cullface = GL_BACK;
1659                 else
1660                         return;
1661                 switch(vid.renderpath)
1662                 {
1663                 case RENDERPATH_GL11:
1664                 case RENDERPATH_GL13:
1665                 case RENDERPATH_GL20:
1666                 case RENDERPATH_CGGL:
1667                         qglCullFace(gl_state.cullface);
1668                         break;
1669                 case RENDERPATH_D3D9:
1670 #ifdef SUPPORTD3D
1671                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, gl_state.cullface == GL_FRONT ? D3DCULL_CCW : D3DCULL_CW);
1672 #endif
1673                         break;
1674                 case RENDERPATH_D3D10:
1675                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1676                         break;
1677                 case RENDERPATH_D3D11:
1678                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1679                         break;
1680                 }
1681         }
1682 }
1683
1684 void GL_CullFace(int state)
1685 {
1686         if(v_flipped_state)
1687         {
1688                 if(state == GL_FRONT)
1689                         state = GL_BACK;
1690                 else if(state == GL_BACK)
1691                         state = GL_FRONT;
1692         }
1693
1694         switch(vid.renderpath)
1695         {
1696         case RENDERPATH_GL11:
1697         case RENDERPATH_GL13:
1698         case RENDERPATH_GL20:
1699         case RENDERPATH_CGGL:
1700                 CHECKGLERROR
1701
1702                 if (state != GL_NONE)
1703                 {
1704                         if (!gl_state.cullfaceenable)
1705                         {
1706                                 gl_state.cullfaceenable = true;
1707                                 qglEnable(GL_CULL_FACE);CHECKGLERROR
1708                         }
1709                         if (gl_state.cullface != state)
1710                         {
1711                                 gl_state.cullface = state;
1712                                 qglCullFace(gl_state.cullface);CHECKGLERROR
1713                         }
1714                 }
1715                 else
1716                 {
1717                         if (gl_state.cullfaceenable)
1718                         {
1719                                 gl_state.cullfaceenable = false;
1720                                 qglDisable(GL_CULL_FACE);CHECKGLERROR
1721                         }
1722                 }
1723                 break;
1724         case RENDERPATH_D3D9:
1725 #ifdef SUPPORTD3D
1726                 if (gl_state.cullface != state)
1727                 {
1728                         gl_state.cullface = state;
1729                         switch(gl_state.cullface)
1730                         {
1731                         case GL_NONE:
1732                                 gl_state.cullfaceenable = false;
1733                                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_NONE);
1734                                 break;
1735                         case GL_FRONT:
1736                                 gl_state.cullfaceenable = true;
1737                                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_CCW);
1738                                 break;
1739                         case GL_BACK:
1740                                 gl_state.cullfaceenable = true;
1741                                 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_CW);
1742                                 break;
1743                         }
1744                 }
1745 #endif
1746                 break;
1747         case RENDERPATH_D3D10:
1748                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1749                 break;
1750         case RENDERPATH_D3D11:
1751                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1752                 break;
1753         }
1754 }
1755
1756 void GL_AlphaTest(int state)
1757 {
1758         if (gl_state.alphatest != state)
1759         {
1760                 gl_state.alphatest = state;
1761                 switch(vid.renderpath)
1762                 {
1763                 case RENDERPATH_GL11:
1764                 case RENDERPATH_GL13:
1765                 case RENDERPATH_GL20:
1766                 case RENDERPATH_CGGL:
1767                         CHECKGLERROR
1768                         if (gl_state.alphatest)
1769                         {
1770                                 qglEnable(GL_ALPHA_TEST);CHECKGLERROR
1771                         }
1772                         else
1773                         {
1774                                 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
1775                         }
1776                         break;
1777                 case RENDERPATH_D3D9:
1778 #ifdef SUPPORTD3D
1779                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHATESTENABLE, gl_state.alphatest);
1780 #endif
1781                         break;
1782                 case RENDERPATH_D3D10:
1783                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1784                         break;
1785                 case RENDERPATH_D3D11:
1786                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1787                         break;
1788                 }
1789         }
1790 }
1791
1792 void GL_AlphaFunc(int state, float value)
1793 {
1794         if (gl_state.alphafunc != state || gl_state.alphafuncvalue != value)
1795         {
1796                 gl_state.alphafunc = state;
1797                 gl_state.alphafuncvalue = value;
1798                 switch(vid.renderpath)
1799                 {
1800                 case RENDERPATH_GL11:
1801                 case RENDERPATH_GL13:
1802                 case RENDERPATH_GL20:
1803                 case RENDERPATH_CGGL:
1804                         CHECKGLERROR
1805                         qglAlphaFunc(gl_state.alphafunc, gl_state.alphafuncvalue);CHECKGLERROR
1806                         break;
1807                 case RENDERPATH_D3D9:
1808 #ifdef SUPPORTD3D
1809                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHAFUNC, d3dcmpforglfunc(gl_state.alphafunc));
1810                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHAREF, (int)bound(0, value * 256.0f, 255));
1811 #endif
1812                         break;
1813                 case RENDERPATH_D3D10:
1814                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1815                         break;
1816                 case RENDERPATH_D3D11:
1817                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1818                         break;
1819                 }
1820         }
1821 }
1822
1823 void GL_ColorMask(int r, int g, int b, int a)
1824 {
1825         // NOTE: this matches D3DCOLORWRITEENABLE_RED, GREEN, BLUE, ALPHA
1826         int state = (r ? 1 : 0) | (g ? 2 : 0) | (b ? 4 : 0) | (a ? 8 : 0);
1827         if (gl_state.colormask != state)
1828         {
1829                 gl_state.colormask = state;
1830                 switch(vid.renderpath)
1831                 {
1832                 case RENDERPATH_GL11:
1833                 case RENDERPATH_GL13:
1834                 case RENDERPATH_GL20:
1835                 case RENDERPATH_CGGL:
1836                         CHECKGLERROR
1837                         qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
1838                         break;
1839                 case RENDERPATH_D3D9:
1840 #ifdef SUPPORTD3D
1841                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_COLORWRITEENABLE, state);
1842 #endif
1843                         break;
1844                 case RENDERPATH_D3D10:
1845                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1846                         break;
1847                 case RENDERPATH_D3D11:
1848                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1849                         break;
1850                 }
1851         }
1852 }
1853
1854 void GL_Color(float cr, float cg, float cb, float ca)
1855 {
1856         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)
1857         {
1858                 gl_state.color4f[0] = cr;
1859                 gl_state.color4f[1] = cg;
1860                 gl_state.color4f[2] = cb;
1861                 gl_state.color4f[3] = ca;
1862                 switch(vid.renderpath)
1863                 {
1864                 case RENDERPATH_GL11:
1865                 case RENDERPATH_GL13:
1866                 case RENDERPATH_GL20:
1867                 case RENDERPATH_CGGL:
1868                         CHECKGLERROR
1869                         qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
1870                         CHECKGLERROR
1871                         break;
1872                 case RENDERPATH_D3D9:
1873                 case RENDERPATH_D3D10:
1874                 case RENDERPATH_D3D11:
1875                         // no equivalent in D3D
1876                         break;
1877                 }
1878         }
1879 }
1880
1881 void GL_Scissor (int x, int y, int width, int height)
1882 {
1883         switch(vid.renderpath)
1884         {
1885         case RENDERPATH_GL11:
1886         case RENDERPATH_GL13:
1887         case RENDERPATH_GL20:
1888         case RENDERPATH_CGGL:
1889                 CHECKGLERROR
1890                 qglScissor(x, y,width,height);
1891                 CHECKGLERROR
1892                 break;
1893         case RENDERPATH_D3D9:
1894 #ifdef SUPPORTD3D
1895                 {
1896                         RECT d3drect;
1897                         d3drect.left = x;
1898                         d3drect.top = y;
1899                         d3drect.right = x + width;
1900                         d3drect.bottom = y + height;
1901                         IDirect3DDevice9_SetScissorRect(vid_d3d9dev, &d3drect);
1902                 }
1903 #endif
1904                 break;
1905         case RENDERPATH_D3D10:
1906                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1907                 break;
1908         case RENDERPATH_D3D11:
1909                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1910                 break;
1911         }
1912 }
1913
1914 void GL_ScissorTest(int state)
1915 {
1916         if (gl_state.scissortest != state)
1917         {
1918                 gl_state.scissortest = state;
1919                 switch(vid.renderpath)
1920                 {
1921                 case RENDERPATH_GL11:
1922                 case RENDERPATH_GL13:
1923                 case RENDERPATH_GL20:
1924                 case RENDERPATH_CGGL:
1925                         CHECKGLERROR
1926                         if(gl_state.scissortest)
1927                                 qglEnable(GL_SCISSOR_TEST);
1928                         else
1929                                 qglDisable(GL_SCISSOR_TEST);
1930                         CHECKGLERROR
1931                         break;
1932                 case RENDERPATH_D3D9:
1933 #ifdef SUPPORTD3D
1934                         IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SCISSORTESTENABLE, gl_state.scissortest);
1935 #endif
1936                         break;
1937                 case RENDERPATH_D3D10:
1938                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1939                         break;
1940                 case RENDERPATH_D3D11:
1941                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1942                         break;
1943                 }
1944         }
1945 }
1946
1947 void GL_Clear(int mask, const float *colorvalue, float depthvalue, int stencilvalue)
1948 {
1949         static const float blackcolor[4] = {0, 0, 0, 0};
1950         // prevent warnings when trying to clear a buffer that does not exist
1951         if (!colorvalue)
1952                 colorvalue = blackcolor;
1953         if (!vid.stencil)
1954         {
1955                 mask &= ~GL_STENCIL_BUFFER_BIT;
1956                 stencilvalue = 0;
1957         }
1958         switch(vid.renderpath)
1959         {
1960         case RENDERPATH_GL11:
1961         case RENDERPATH_GL13:
1962         case RENDERPATH_GL20:
1963         case RENDERPATH_CGGL:
1964                 CHECKGLERROR
1965                 if (mask & GL_COLOR_BUFFER_BIT)
1966                 {
1967                         qglClearColor(colorvalue[0], colorvalue[1], colorvalue[2], colorvalue[3]);CHECKGLERROR
1968                 }
1969                 if (mask & GL_DEPTH_BUFFER_BIT)
1970                 {
1971                         qglClearDepth(depthvalue);CHECKGLERROR
1972                 }
1973                 if (mask & GL_STENCIL_BUFFER_BIT)
1974                 {
1975                         qglClearStencil(stencilvalue);CHECKGLERROR
1976                 }
1977                 qglClear(mask);CHECKGLERROR
1978                 break;
1979         case RENDERPATH_D3D9:
1980 #ifdef SUPPORTD3D
1981                 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);
1982 #endif
1983                 break;
1984         case RENDERPATH_D3D10:
1985                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1986                 break;
1987         case RENDERPATH_D3D11:
1988                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1989                 break;
1990         }
1991 }
1992
1993 void GL_ReadPixelsBGRA(int x, int y, int width, int height, unsigned char *outpixels)
1994 {
1995         switch(vid.renderpath)
1996         {
1997         case RENDERPATH_GL11:
1998         case RENDERPATH_GL13:
1999         case RENDERPATH_GL20:
2000         case RENDERPATH_CGGL:
2001                 CHECKGLERROR
2002                 qglReadPixels(x, y, width, height, GL_BGRA, GL_UNSIGNED_BYTE, outpixels);CHECKGLERROR
2003                 break;
2004         case RENDERPATH_D3D9:
2005 #ifdef SUPPORTD3D
2006                 {
2007                         // LordHavoc: we can't directly download the backbuffer because it may be
2008                         // multisampled, and it may not be lockable, so we blit it to a lockable
2009                         // surface of the same dimensions (but without multisample) to resolve the
2010                         // multisample buffer to a normal image, and then lock that...
2011                         IDirect3DSurface9 *stretchsurface = NULL;
2012                         if (!FAILED(IDirect3DDevice9_CreateRenderTarget(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, TRUE, &stretchsurface, NULL)))
2013                         {
2014                                 D3DLOCKED_RECT lockedrect;
2015                                 if (!FAILED(IDirect3DDevice9_StretchRect(vid_d3d9dev, gl_state.d3drt_backbuffercolorsurface, NULL, stretchsurface, NULL, D3DTEXF_POINT)))
2016                                 {
2017                                         if (!FAILED(IDirect3DSurface9_LockRect(stretchsurface, &lockedrect, NULL, D3DLOCK_READONLY)))
2018                                         {
2019                                                 int line;
2020                                                 unsigned char *row = (unsigned char *)lockedrect.pBits + x * 4 + lockedrect.Pitch * (vid.height - 1 - y);
2021                                                 for (line = 0;line < height;line++, row -= lockedrect.Pitch)
2022                                                         memcpy(outpixels + line * width * 4, row, width * 4);
2023                                                 IDirect3DSurface9_UnlockRect(stretchsurface);
2024                                         }
2025                                 }
2026                                 IDirect3DSurface9_Release(stretchsurface);
2027                         }
2028                         // code scraps
2029                         //IDirect3DSurface9 *syssurface = NULL;
2030                         //if (!FAILED(IDirect3DDevice9_CreateRenderTarget(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, FALSE, &stretchsurface, NULL)))
2031                         //if (!FAILED(IDirect3DDevice9_CreateOffscreenPlainSurface(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &syssurface, NULL)))
2032                         //IDirect3DDevice9_GetRenderTargetData(vid_d3d9dev, gl_state.d3drt_backbuffercolorsurface, syssurface);
2033                         //if (!FAILED(IDirect3DDevice9_GetFrontBufferData(vid_d3d9dev, 0, syssurface)))
2034                         //if (!FAILED(IDirect3DSurface9_LockRect(syssurface, &lockedrect, NULL, D3DLOCK_READONLY)))
2035                         //IDirect3DSurface9_UnlockRect(syssurface);
2036                         //IDirect3DSurface9_Release(syssurface);
2037                 }
2038 #endif
2039                 break;
2040         case RENDERPATH_D3D10:
2041                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2042                 break;
2043         case RENDERPATH_D3D11:
2044                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2045                 break;
2046         }
2047 }
2048
2049 // called at beginning of frame
2050 void R_Mesh_Start(void)
2051 {
2052         BACKENDACTIVECHECK
2053         gl_state.usevbo_staticvertex = (vid.support.arb_vertex_buffer_object && gl_vbo.integer) || vid.forcevbo;
2054         gl_state.usevbo_staticindex = (vid.support.arb_vertex_buffer_object && (gl_vbo.integer == 1 || gl_vbo.integer == 3)) || vid.forcevbo;
2055         gl_state.usevbo_dynamicvertex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicvertex.integer) || vid.forcevbo;
2056         gl_state.usevbo_dynamicindex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicindex.integer) || vid.forcevbo;
2057         if (gl_printcheckerror.integer && !gl_paranoid.integer)
2058         {
2059                 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
2060                 Cvar_SetValueQuick(&gl_paranoid, 1);
2061         }
2062 }
2063
2064 qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
2065 {
2066         int shaderobject;
2067         int shadercompiled;
2068         char compilelog[MAX_INPUTLINE];
2069         shaderobject = qglCreateShaderObjectARB(shadertypeenum);CHECKGLERROR
2070         if (!shaderobject)
2071                 return false;
2072         qglShaderSourceARB(shaderobject, numstrings, strings, NULL);CHECKGLERROR
2073         qglCompileShaderARB(shaderobject);CHECKGLERROR
2074         qglGetObjectParameterivARB(shaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &shadercompiled);CHECKGLERROR
2075         qglGetInfoLogARB(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
2076         if (compilelog[0] && (strstr(compilelog, "error") || strstr(compilelog, "ERROR") || strstr(compilelog, "Error") || strstr(compilelog, "WARNING") || strstr(compilelog, "warning") || strstr(compilelog, "Warning")))
2077         {
2078                 int i, j, pretextlines = 0;
2079                 for (i = 0;i < numstrings - 1;i++)
2080                         for (j = 0;strings[i][j];j++)
2081                                 if (strings[i][j] == '\n')
2082                                         pretextlines++;
2083                 Con_Printf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
2084         }
2085         if (!shadercompiled)
2086         {
2087                 qglDeleteObjectARB(shaderobject);CHECKGLERROR
2088                 return false;
2089         }
2090         qglAttachObjectARB(programobject, shaderobject);CHECKGLERROR
2091         qglDeleteObjectARB(shaderobject);CHECKGLERROR
2092         return true;
2093 }
2094
2095 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)
2096 {
2097         GLint programlinked;
2098         GLuint programobject = 0;
2099         char linklog[MAX_INPUTLINE];
2100         CHECKGLERROR
2101
2102         programobject = qglCreateProgramObjectARB();CHECKGLERROR
2103         if (!programobject)
2104                 return 0;
2105
2106         if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER_ARB, "vertex", vertexstrings_count, vertexstrings_list))
2107                 goto cleanup;
2108
2109 #ifdef GL_GEOMETRY_SHADER_ARB
2110         if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER_ARB, "geometry", geometrystrings_count, geometrystrings_list))
2111                 goto cleanup;
2112 #endif
2113
2114         if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER_ARB, "fragment", fragmentstrings_count, fragmentstrings_list))
2115                 goto cleanup;
2116
2117         qglLinkProgramARB(programobject);CHECKGLERROR
2118         qglGetObjectParameterivARB(programobject, GL_OBJECT_LINK_STATUS_ARB, &programlinked);CHECKGLERROR
2119         qglGetInfoLogARB(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
2120         if (linklog[0])
2121         {
2122                 if (strstr(linklog, "error") || strstr(linklog, "ERROR") || strstr(linklog, "Error") || strstr(linklog, "WARNING") || strstr(linklog, "warning") || strstr(linklog, "Warning"))
2123                         Con_DPrintf("program link log:\n%s\n", linklog);
2124                 // software vertex shader is ok but software fragment shader is WAY
2125                 // too slow, fail program if so.
2126                 // NOTE: this string might be ATI specific, but that's ok because the
2127                 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
2128                 // software fragment shader due to low instruction and dependent
2129                 // texture limits.
2130                 if (strstr(linklog, "fragment shader will run in software"))
2131                         programlinked = false;
2132         }
2133         if (!programlinked)
2134                 goto cleanup;
2135         return programobject;
2136 cleanup:
2137         qglDeleteObjectARB(programobject);CHECKGLERROR
2138         return 0;
2139 }
2140
2141 void GL_Backend_FreeProgram(unsigned int prog)
2142 {
2143         CHECKGLERROR
2144         qglDeleteObjectARB(prog);
2145         CHECKGLERROR
2146 }
2147
2148 void GL_Backend_RenumberElements(int *out, int count, const int *in, int offset)
2149 {
2150         int i;
2151         if (offset)
2152         {
2153                 for (i = 0;i < count;i++)
2154                         *out++ = *in++ + offset;
2155         }
2156         else
2157                 memcpy(out, in, sizeof(*out) * count);
2158 }
2159
2160 // renders triangles using vertices from the active arrays
2161 int paranoidblah = 0;
2162 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)
2163 {
2164         unsigned int numelements = numtriangles * 3;
2165         int bufferobject3i;
2166         size_t bufferoffset3i;
2167         int bufferobject3s;
2168         size_t bufferoffset3s;
2169         if (numvertices < 3 || numtriangles < 1)
2170         {
2171                 if (numvertices < 0 || numtriangles < 0 || developer_extra.integer)
2172                         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);
2173                 return;
2174         }
2175         if (!gl_mesh_prefer_short_elements.integer)
2176         {
2177                 if (element3i)
2178                         element3s = NULL;
2179                 if (element3i_indexbuffer)
2180                         element3i_indexbuffer = NULL;
2181         }
2182         // adjust the pointers for firsttriangle
2183         if (element3i)
2184                 element3i += firsttriangle * 3;
2185         if (element3i_indexbuffer)
2186                 element3i_bufferoffset += firsttriangle * 3 * sizeof(*element3i);
2187         if (element3s)
2188                 element3s += firsttriangle * 3;
2189         if (element3s_indexbuffer)
2190                 element3s_bufferoffset += firsttriangle * 3 * sizeof(*element3s);
2191         // check if the user specified to ignore static index buffers
2192         if (!gl_state.usevbo_staticindex || (gl_vbo.integer == 3 && !vid.forcevbo && (element3i_bufferoffset || element3s_bufferoffset)))
2193         {
2194                 element3i_indexbuffer = NULL;
2195                 element3s_indexbuffer = NULL;
2196         }
2197         // upload a dynamic index buffer if needed
2198         if (element3s)
2199         {
2200                 if (!element3s_indexbuffer && gl_state.usevbo_dynamicindex)
2201                 {
2202                         if (gl_state.draw_dynamicindexbuffer)
2203                                 R_Mesh_UpdateMeshBuffer(gl_state.draw_dynamicindexbuffer, (void *)element3s, numelements * sizeof(*element3s));
2204                         else
2205                                 gl_state.draw_dynamicindexbuffer = R_Mesh_CreateMeshBuffer((void *)element3s, numelements * sizeof(*element3s), "temporary", true, true, true);
2206                         element3s_indexbuffer = gl_state.draw_dynamicindexbuffer;
2207                         element3s_bufferoffset = 0;
2208                 }
2209         }
2210         else if (element3i)
2211         {
2212                 if (!element3i_indexbuffer && gl_state.usevbo_dynamicindex)
2213                 {
2214                         if (gl_state.draw_dynamicindexbuffer)
2215                                 R_Mesh_UpdateMeshBuffer(gl_state.draw_dynamicindexbuffer, (void *)element3i, numelements * sizeof(*element3i));
2216                         else
2217                                 gl_state.draw_dynamicindexbuffer = R_Mesh_CreateMeshBuffer((void *)element3i, numelements * sizeof(*element3i), "temporary", true, true, false);
2218                         element3i_indexbuffer = gl_state.draw_dynamicindexbuffer;
2219                         element3i_bufferoffset = 0;
2220                 }
2221         }
2222         bufferobject3i = element3i_indexbuffer ? element3i_indexbuffer->bufferobject : 0;
2223         bufferoffset3i = element3i_bufferoffset;
2224         bufferobject3s = element3s_indexbuffer ? element3s_indexbuffer->bufferobject : 0;
2225         bufferoffset3s = element3s_bufferoffset;
2226         r_refdef.stats.meshes++;
2227         r_refdef.stats.meshes_elements += numelements;
2228         if (gl_paranoid.integer)
2229         {
2230                 unsigned int i;
2231                 // LordHavoc: disabled this - it needs to be updated to handle components and gltype and stride in each array
2232 #if 0
2233                 unsigned int j, size;
2234                 const int *p;
2235                 // note: there's no validation done here on buffer objects because it
2236                 // is somewhat difficult to get at the data, and gl_paranoid can be
2237                 // used without buffer objects if the need arises
2238                 // (the data could be gotten using glMapBuffer but it would be very
2239                 //  slow due to uncachable video memory reads)
2240                 if (!qglIsEnabled(GL_VERTEX_ARRAY))
2241                         Con_Print("R_Mesh_Draw: vertex array not enabled\n");
2242                 CHECKGLERROR
2243                 if (gl_state.pointer_vertex_pointer)
2244                         for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
2245                                 paranoidblah += *p;
2246                 if (gl_state.pointer_color_enabled)
2247                 {
2248                         if (!qglIsEnabled(GL_COLOR_ARRAY))
2249                                 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
2250                         CHECKGLERROR
2251                         if (gl_state.pointer_color && gl_state.pointer_color_enabled)
2252                                 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
2253                                         paranoidblah += *p;
2254                 }
2255                 for (i = 0;i < vid.texarrayunits;i++)
2256                 {
2257                         if (gl_state.units[i].arrayenabled)
2258                         {
2259                                 GL_ClientActiveTexture(i);
2260                                 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
2261                                         Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
2262                                 CHECKGLERROR
2263                                 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
2264                                         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++)
2265                                                 paranoidblah += *p;
2266                         }
2267                 }
2268 #endif
2269                 if (element3i)
2270                 {
2271                         for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2272                         {
2273                                 if (element3i[i] < firstvertex || element3i[i] >= firstvertex + numvertices)
2274                                 {
2275                                         Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3i array\n", element3i[i], firstvertex, firstvertex + numvertices);
2276                                         return;
2277                                 }
2278                         }
2279                 }
2280                 if (element3s)
2281                 {
2282                         for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2283                         {
2284                                 if (element3s[i] < firstvertex || element3s[i] >= firstvertex + numvertices)
2285                                 {
2286                                         Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3s array\n", element3s[i], firstvertex, firstvertex + numvertices);
2287                                         return;
2288                                 }
2289                         }
2290                 }
2291         }
2292         if (r_render.integer || r_refdef.draw2dstage)
2293         {
2294                 switch(vid.renderpath)
2295                 {
2296                 case RENDERPATH_GL11:
2297                 case RENDERPATH_GL13:
2298                 case RENDERPATH_GL20:
2299                 case RENDERPATH_CGGL:
2300                         CHECKGLERROR
2301                         if (gl_mesh_testmanualfeeding.integer)
2302                         {
2303                                 unsigned int i, j, element;
2304                                 const GLfloat *p;
2305                                 qglBegin(GL_TRIANGLES);
2306                                 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2307                                 {
2308                                         if (element3i)
2309                                                 element = element3i[i];
2310                                         else if (element3s)
2311                                                 element = element3s[i];
2312                                         else
2313                                                 element = firstvertex + i;
2314                                         for (j = 0;j < vid.texarrayunits;j++)
2315                                         {
2316                                                 if (gl_state.units[j].pointer_texcoord_pointer && gl_state.units[j].arrayenabled)
2317                                                 {
2318                                                         if (gl_state.units[j].pointer_texcoord_gltype == GL_FLOAT)
2319                                                         {
2320                                                                 p = (const GLfloat *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2321                                                                 if (vid.texarrayunits > 1)
2322                                                                 {
2323                                                                         if (gl_state.units[j].pointer_texcoord_components == 4)
2324                                                                                 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2], p[3]);
2325                                                                         else if (gl_state.units[j].pointer_texcoord_components == 3)
2326                                                                                 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2]);
2327                                                                         else if (gl_state.units[j].pointer_texcoord_components == 2)
2328                                                                                 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, p[0], p[1]);
2329                                                                         else
2330                                                                                 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, p[0]);
2331                                                                 }
2332                                                                 else
2333                                                                 {
2334                                                                         if (gl_state.units[j].pointer_texcoord_components == 4)
2335                                                                                 qglTexCoord4f(p[0], p[1], p[2], p[3]);
2336                                                                         else if (gl_state.units[j].pointer_texcoord_components == 3)
2337                                                                                 qglTexCoord3f(p[0], p[1], p[2]);
2338                                                                         else if (gl_state.units[j].pointer_texcoord_components == 2)
2339                                                                                 qglTexCoord2f(p[0], p[1]);
2340                                                                         else
2341                                                                                 qglTexCoord1f(p[0]);
2342                                                                 }
2343                                                         }
2344                                                         else if (gl_state.units[j].pointer_texcoord_gltype == GL_SHORT)
2345                                                         {
2346                                                                 const GLshort *s = (const GLshort *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2347                                                                 if (vid.texarrayunits > 1)
2348                                                                 {
2349                                                                         if (gl_state.units[j].pointer_texcoord_components == 4)
2350                                                                                 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, s[0], s[1], s[2], s[3]);
2351                                                                         else if (gl_state.units[j].pointer_texcoord_components == 3)
2352                                                                                 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, s[0], s[1], s[2]);
2353                                                                         else if (gl_state.units[j].pointer_texcoord_components == 2)
2354                                                                                 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, s[0], s[1]);
2355                                                                         else if (gl_state.units[j].pointer_texcoord_components == 1)
2356                                                                                 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, s[0]);
2357                                                                 }
2358                                                                 else
2359                                                                 {
2360                                                                         if (gl_state.units[j].pointer_texcoord_components == 4)
2361                                                                                 qglTexCoord4f(s[0], s[1], s[2], s[3]);
2362                                                                         else if (gl_state.units[j].pointer_texcoord_components == 3)
2363                                                                                 qglTexCoord3f(s[0], s[1], s[2]);
2364                                                                         else if (gl_state.units[j].pointer_texcoord_components == 2)
2365                                                                                 qglTexCoord2f(s[0], s[1]);
2366                                                                         else if (gl_state.units[j].pointer_texcoord_components == 1)
2367                                                                                 qglTexCoord1f(s[0]);
2368                                                                 }
2369                                                         }
2370                                                         else if (gl_state.units[j].pointer_texcoord_gltype == GL_BYTE)
2371                                                         {
2372                                                                 const GLbyte *sb = (const GLbyte *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2373                                                                 if (vid.texarrayunits > 1)
2374                                                                 {
2375                                                                         if (gl_state.units[j].pointer_texcoord_components == 4)
2376                                                                                 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, sb[0], sb[1], sb[2], sb[3]);
2377                                                                         else if (gl_state.units[j].pointer_texcoord_components == 3)
2378                                                                                 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, sb[0], sb[1], sb[2]);
2379                                                                         else if (gl_state.units[j].pointer_texcoord_components == 2)
2380                                                                                 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, sb[0], sb[1]);
2381                                                                         else if (gl_state.units[j].pointer_texcoord_components == 1)
2382                                                                                 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, sb[0]);
2383                                                                 }
2384                                                                 else
2385                                                                 {
2386                                                                         if (gl_state.units[j].pointer_texcoord_components == 4)
2387                                                                                 qglTexCoord4f(sb[0], sb[1], sb[2], sb[3]);
2388                                                                         else if (gl_state.units[j].pointer_texcoord_components == 3)
2389                                                                                 qglTexCoord3f(sb[0], sb[1], sb[2]);
2390                                                                         else if (gl_state.units[j].pointer_texcoord_components == 2)
2391                                                                                 qglTexCoord2f(sb[0], sb[1]);
2392                                                                         else if (gl_state.units[j].pointer_texcoord_components == 1)
2393                                                                                 qglTexCoord1f(sb[0]);
2394                                                                 }
2395                                                         }
2396                                                 }
2397                                         }
2398                                         if (gl_state.pointer_color_pointer && gl_state.pointer_color_enabled && gl_state.pointer_color_components == 4)
2399                                         {
2400                                                 if (gl_state.pointer_color_gltype == GL_FLOAT)
2401                                                 {
2402                                                         p = (const GLfloat *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
2403                                                         qglColor4f(p[0], p[1], p[2], p[3]);
2404                                                 }
2405                                                 else if (gl_state.pointer_color_gltype == GL_UNSIGNED_BYTE)
2406                                                 {
2407                                                         const GLubyte *ub = (const GLubyte *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
2408                                                         qglColor4ub(ub[0], ub[1], ub[2], ub[3]);
2409                                                 }
2410                                         }
2411                                         if (gl_state.pointer_vertex_gltype == GL_FLOAT)
2412                                         {
2413                                                 p = (const GLfloat *)((const unsigned char *)gl_state.pointer_vertex_pointer + element * gl_state.pointer_vertex_stride);
2414                                                 if (gl_state.pointer_vertex_components == 4)
2415                                                         qglVertex4f(p[0], p[1], p[2], p[3]);
2416                                                 else if (gl_state.pointer_vertex_components == 3)
2417                                                         qglVertex3f(p[0], p[1], p[2]);
2418                                                 else
2419                                                         qglVertex2f(p[0], p[1]);
2420                                         }
2421                                 }
2422                                 qglEnd();
2423                                 CHECKGLERROR
2424                         }
2425                         else if (bufferobject3s)
2426                         {
2427                                 GL_BindEBO(bufferobject3s);
2428                                 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
2429                                 {
2430                                         qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, (void *)bufferoffset3s);
2431                                         CHECKGLERROR
2432                                 }
2433                                 else
2434                                 {
2435                                         qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
2436                                         CHECKGLERROR
2437                                 }
2438                         }
2439                         else if (bufferobject3i)
2440                         {
2441                                 GL_BindEBO(bufferobject3i);
2442                                 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
2443                                 {
2444                                         qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, (void *)bufferoffset3i);
2445                                         CHECKGLERROR
2446                                 }
2447                                 else
2448                                 {
2449                                         qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
2450                                         CHECKGLERROR
2451                                 }
2452                         }
2453                         else if (element3s)
2454                         {
2455                                 GL_BindEBO(0);
2456                                 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
2457                                 {
2458                                         qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, element3s);
2459                                         CHECKGLERROR
2460                                 }
2461                                 else
2462                                 {
2463                                         qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
2464                                         CHECKGLERROR
2465                                 }
2466                         }
2467                         else if (element3i)
2468                         {
2469                                 GL_BindEBO(0);
2470                                 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
2471                                 {
2472                                         qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, element3i);
2473                                         CHECKGLERROR
2474                                 }
2475                                 else
2476                                 {
2477                                         qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
2478                                         CHECKGLERROR
2479                                 }
2480                         }
2481                         else
2482                         {
2483                                 qglDrawArrays(GL_TRIANGLES, firstvertex, numvertices);
2484                                 CHECKGLERROR
2485                         }
2486                         break;
2487                 case RENDERPATH_D3D9:
2488 #ifdef SUPPORTD3D
2489                         {
2490                                 if (element3s_indexbuffer)
2491                                 {
2492                                         IDirect3DDevice9_SetIndices(vid_d3d9dev, (IDirect3DIndexBuffer9 *)element3s_indexbuffer->devicebuffer);
2493                                         IDirect3DDevice9_DrawIndexedPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, 0, firstvertex, numvertices, element3s_bufferoffset>>1, numtriangles);
2494                                 }
2495                                 else if (element3i_indexbuffer)
2496                                 {
2497                                         IDirect3DDevice9_SetIndices(vid_d3d9dev, (IDirect3DIndexBuffer9 *)element3i_indexbuffer->devicebuffer);
2498                                         IDirect3DDevice9_DrawIndexedPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, 0, firstvertex, numvertices, element3i_bufferoffset>>2, numtriangles);
2499                                 }
2500                                 else
2501                                         IDirect3DDevice9_DrawPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, firstvertex, numvertices);
2502                         }
2503 #endif
2504                         break;
2505                 case RENDERPATH_D3D10:
2506                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2507                         break;
2508                 case RENDERPATH_D3D11:
2509                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2510                         break;
2511                 }
2512         }
2513 }
2514
2515 // restores backend state, used when done with 3D rendering
2516 void R_Mesh_Finish(void)
2517 {
2518 }
2519
2520 r_meshbuffer_t *R_Mesh_CreateMeshBuffer(const void *data, size_t size, const char *name, qboolean isindexbuffer, qboolean isdynamic, qboolean isindex16)
2521 {
2522         r_meshbuffer_t *buffer;
2523         if (!(isdynamic ? (isindexbuffer ? gl_state.usevbo_dynamicindex : gl_state.usevbo_dynamicvertex) : (isindexbuffer ? gl_state.usevbo_staticindex : gl_state.usevbo_staticvertex)))
2524                 return NULL;
2525         buffer = (r_meshbuffer_t *)Mem_ExpandableArray_AllocRecord(&gl_state.meshbufferarray);
2526         memset(buffer, 0, sizeof(*buffer));
2527         buffer->bufferobject = 0;
2528         buffer->devicebuffer = NULL;
2529         buffer->size = 0;
2530         buffer->isindexbuffer = isindexbuffer;
2531         buffer->isdynamic = isdynamic;
2532         buffer->isindex16 = isindex16;
2533         strlcpy(buffer->name, name, sizeof(buffer->name));
2534         R_Mesh_UpdateMeshBuffer(buffer, data, size);
2535         return buffer;
2536 }
2537
2538 void R_Mesh_UpdateMeshBuffer(r_meshbuffer_t *buffer, const void *data, size_t size)
2539 {
2540         if (!buffer)
2541                 return;
2542         if (buffer->isindexbuffer)
2543         {
2544                 r_refdef.stats.indexbufferuploadcount++;
2545                 r_refdef.stats.indexbufferuploadsize += size;
2546         }
2547         else
2548         {
2549                 r_refdef.stats.vertexbufferuploadcount++;
2550                 r_refdef.stats.vertexbufferuploadsize += size;
2551         }
2552         switch(vid.renderpath)
2553         {
2554         case RENDERPATH_GL11:
2555         case RENDERPATH_GL13:
2556         case RENDERPATH_GL20:
2557         case RENDERPATH_CGGL:
2558                 if (!buffer->bufferobject)
2559                         qglGenBuffersARB(1, (GLuint *)&buffer->bufferobject);
2560                 if (buffer->isindexbuffer)
2561                         GL_BindEBO(buffer->bufferobject);
2562                 else
2563                         GL_BindVBO(buffer->bufferobject);
2564                 qglBufferDataARB(buffer->isindexbuffer ? GL_ELEMENT_ARRAY_BUFFER_ARB : GL_ARRAY_BUFFER_ARB, size, data, buffer->isdynamic ? GL_STREAM_DRAW_ARB : GL_STATIC_DRAW_ARB);
2565                 break;
2566         case RENDERPATH_D3D9:
2567 #ifdef SUPPORTD3D
2568                 {
2569                         int result;
2570                         void *datapointer = NULL;
2571                         if (buffer->isindexbuffer)
2572                         {
2573                                 IDirect3DIndexBuffer9 *d3d9indexbuffer = (IDirect3DIndexBuffer9 *)buffer->devicebuffer;
2574                                 if (size != buffer->size || !buffer->devicebuffer)
2575                                 {
2576                                         if (buffer->devicebuffer)
2577                                                 IDirect3DIndexBuffer9_Release((IDirect3DIndexBuffer9*)buffer->devicebuffer);
2578                                         buffer->devicebuffer = NULL;
2579                                         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)))
2580                                                 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);
2581                                         buffer->devicebuffer = (void *)d3d9indexbuffer;
2582                                         buffer->size = size;
2583                                 }
2584                                 if (!FAILED(IDirect3DIndexBuffer9_Lock(d3d9indexbuffer, 0, 0, &datapointer, buffer->isdynamic ? D3DLOCK_DISCARD : 0)))
2585                                 {
2586                                         if (data)
2587                                                 memcpy(datapointer, data, size);
2588                                         else
2589                                                 memset(datapointer, 0, size);
2590                                         IDirect3DIndexBuffer9_Unlock(d3d9indexbuffer);
2591                                 }
2592                         }
2593                         else
2594                         {
2595                                 IDirect3DVertexBuffer9 *d3d9vertexbuffer = (IDirect3DVertexBuffer9 *)buffer->devicebuffer;
2596                                 if (size != buffer->size || !buffer->devicebuffer)
2597                                 {
2598                                         if (buffer->devicebuffer)
2599                                                 IDirect3DVertexBuffer9_Release((IDirect3DVertexBuffer9*)buffer->devicebuffer);
2600                                         buffer->devicebuffer = NULL;
2601                                         if (FAILED(result = IDirect3DDevice9_CreateVertexBuffer(vid_d3d9dev, size, buffer->isdynamic ? D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC : 0, 0, buffer->isdynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED, &d3d9vertexbuffer, NULL)))
2602                                                 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);
2603                                         buffer->devicebuffer = (void *)d3d9vertexbuffer;
2604                                         buffer->size = size;
2605                                 }
2606                                 if (!FAILED(IDirect3DVertexBuffer9_Lock(d3d9vertexbuffer, 0, 0, &datapointer, buffer->isdynamic ? D3DLOCK_DISCARD : 0)))
2607                                 {
2608                                         if (data)
2609                                                 memcpy(datapointer, data, size);
2610                                         else
2611                                                 memset(datapointer, 0, size);
2612                                         IDirect3DVertexBuffer9_Unlock(d3d9vertexbuffer);
2613                                 }
2614                         }
2615                 }
2616 #endif
2617                 break;
2618         case RENDERPATH_D3D10:
2619                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2620                 break;
2621         case RENDERPATH_D3D11:
2622                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2623                 break;
2624         }
2625 }
2626
2627 void R_Mesh_DestroyMeshBuffer(r_meshbuffer_t *buffer)
2628 {
2629         if (!buffer)
2630                 return;
2631         switch(vid.renderpath)
2632         {
2633         case RENDERPATH_GL11:
2634         case RENDERPATH_GL13:
2635         case RENDERPATH_GL20:
2636         case RENDERPATH_CGGL:
2637                 qglDeleteBuffersARB(1, (GLuint *)&buffer->bufferobject);
2638                 break;
2639         case RENDERPATH_D3D9:
2640 #ifdef SUPPORTD3D
2641                 if (buffer->devicebuffer)
2642                 {
2643                         if (buffer->isindexbuffer)
2644                                 IDirect3DIndexBuffer9_Release((IDirect3DIndexBuffer9 *)buffer->devicebuffer);
2645                         else
2646                                 IDirect3DVertexBuffer9_Release((IDirect3DVertexBuffer9 *)buffer->devicebuffer);
2647                 }
2648 #endif
2649                 break;
2650         case RENDERPATH_D3D10:
2651                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2652                 break;
2653         case RENDERPATH_D3D11:
2654                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2655                 break;
2656         }
2657         Mem_ExpandableArray_FreeRecord(&gl_state.meshbufferarray, (void *)buffer);
2658 }
2659
2660 void GL_Mesh_ListVBOs(qboolean printeach)
2661 {
2662         int i, endindex;
2663         size_t ebocount = 0, ebomemory = 0;
2664         size_t vbocount = 0, vbomemory = 0;
2665         r_meshbuffer_t *buffer;
2666         endindex = Mem_ExpandableArray_IndexRange(&gl_state.meshbufferarray);
2667         for (i = 0;i < endindex;i++)
2668         {
2669                 buffer = (r_meshbuffer_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.meshbufferarray, i);
2670                 if (!buffer)
2671                         continue;
2672                 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)");}
2673                 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)");}
2674         }
2675         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);
2676 }
2677
2678
2679
2680 void R_Mesh_VertexPointer(int components, int gltype, size_t stride, const void *pointer, const r_meshbuffer_t *vertexbuffer, size_t bufferoffset)
2681 {
2682         int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
2683         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)
2684         {
2685                 gl_state.pointer_vertex_components = components;
2686                 gl_state.pointer_vertex_gltype = gltype;
2687                 gl_state.pointer_vertex_stride = stride;
2688                 gl_state.pointer_vertex_pointer = pointer;
2689                 gl_state.pointer_vertex_vertexbuffer = vertexbuffer;
2690                 gl_state.pointer_vertex_offset = bufferoffset;
2691                 CHECKGLERROR
2692                 GL_BindVBO(bufferobject);
2693                 qglVertexPointer(components, gltype, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
2694         }
2695 }
2696
2697 void R_Mesh_ColorPointer(int components, int gltype, size_t stride, const void *pointer, const r_meshbuffer_t *vertexbuffer, size_t bufferoffset)
2698 {
2699         // note: vertexbuffer may be non-NULL even if pointer is NULL, so check
2700         // the pointer only.
2701         if (pointer)
2702         {
2703                 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
2704                 // caller wants color array enabled
2705                 if (!gl_state.pointer_color_enabled)
2706                 {
2707                         gl_state.pointer_color_enabled = true;
2708                         CHECKGLERROR
2709                         qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
2710                 }
2711                 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)
2712                 {
2713                         gl_state.pointer_color_components = components;
2714                         gl_state.pointer_color_gltype = gltype;
2715                         gl_state.pointer_color_stride = stride;
2716                         gl_state.pointer_color_pointer = pointer;
2717                         gl_state.pointer_color_vertexbuffer = vertexbuffer;
2718                         gl_state.pointer_color_offset = bufferoffset;
2719                         CHECKGLERROR
2720                         GL_BindVBO(bufferobject);
2721                         qglColorPointer(components, gltype, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
2722                 }
2723         }
2724         else
2725         {
2726                 // caller wants color array disabled
2727                 if (gl_state.pointer_color_enabled)
2728                 {
2729                         gl_state.pointer_color_enabled = false;
2730                         CHECKGLERROR
2731                         qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
2732                         // when color array is on the glColor gets trashed, set it again
2733                         qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
2734                 }
2735         }
2736 }
2737
2738 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)
2739 {
2740         gltextureunit_t *unit = gl_state.units + unitnum;
2741         // update array settings
2742         CHECKGLERROR
2743         // note: there is no need to check bufferobject here because all cases
2744         // that involve a valid bufferobject also supply a texcoord array
2745         if (pointer)
2746         {
2747                 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
2748                 // texture array unit is enabled, enable the array
2749                 if (!unit->arrayenabled)
2750                 {
2751                         unit->arrayenabled = true;
2752                         GL_ClientActiveTexture(unitnum);
2753                         qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
2754                 }
2755                 // texcoord array
2756                 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)
2757                 {
2758                         unit->pointer_texcoord_components = components;
2759                         unit->pointer_texcoord_gltype = gltype;
2760                         unit->pointer_texcoord_stride = stride;
2761                         unit->pointer_texcoord_pointer = pointer;
2762                         unit->pointer_texcoord_vertexbuffer = vertexbuffer;
2763                         unit->pointer_texcoord_offset = bufferoffset;
2764                         GL_ClientActiveTexture(unitnum);
2765                         GL_BindVBO(bufferobject);
2766                         qglTexCoordPointer(components, gltype, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
2767                 }
2768         }
2769         else
2770         {
2771                 // texture array unit is disabled, disable the array
2772                 if (unit->arrayenabled)
2773                 {
2774                         unit->arrayenabled = false;
2775                         GL_ClientActiveTexture(unitnum);
2776                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
2777                 }
2778         }
2779 }
2780
2781 int R_Mesh_TexBound(unsigned int unitnum, int id)
2782 {
2783         gltextureunit_t *unit = gl_state.units + unitnum;
2784         if (unitnum >= vid.teximageunits)
2785                 return 0;
2786         if (id == GL_TEXTURE_2D)
2787                 return unit->t2d;
2788         if (id == GL_TEXTURE_3D)
2789                 return unit->t3d;
2790         if (id == GL_TEXTURE_CUBE_MAP_ARB)
2791                 return unit->tcubemap;
2792         if (id == GL_TEXTURE_RECTANGLE_ARB)
2793                 return unit->trectangle;
2794         return 0;
2795 }
2796
2797 void R_Mesh_CopyToTexture(rtexture_t *tex, int tx, int ty, int sx, int sy, int width, int height)
2798 {
2799         switch(vid.renderpath)
2800         {
2801         case RENDERPATH_GL11:
2802         case RENDERPATH_GL13:
2803         case RENDERPATH_GL20:
2804         case RENDERPATH_CGGL:
2805                 R_Mesh_TexBind(0, tex);
2806                 GL_ActiveTexture(0);CHECKGLERROR
2807                 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, tx, ty, sx, sy, width, height);CHECKGLERROR
2808                 break;
2809         case RENDERPATH_D3D9:
2810 #ifdef SUPPORTD3D
2811                 {
2812                         IDirect3DSurface9 *currentsurface = NULL;
2813                         IDirect3DSurface9 *texturesurface = NULL;
2814                         RECT sourcerect;
2815                         RECT destrect;
2816                         sourcerect.left = sx;
2817                         sourcerect.top = sy;
2818                         sourcerect.right = sx + width;
2819                         sourcerect.bottom = sy + height;
2820                         destrect.left = tx;
2821                         destrect.top = ty;
2822                         destrect.right = tx + width;
2823                         destrect.bottom = ty + height;
2824                         if (!FAILED(IDirect3DTexture9_GetSurfaceLevel(((IDirect3DTexture9 *)tex->d3dtexture), 0, &texturesurface)))
2825                         {
2826                                 if (!FAILED(IDirect3DDevice9_GetRenderTarget(vid_d3d9dev, 0, &currentsurface)))
2827                                 {
2828                                         IDirect3DDevice9_StretchRect(vid_d3d9dev, currentsurface, &sourcerect, texturesurface, &destrect, D3DTEXF_NONE);
2829                                         IDirect3DSurface9_Release(currentsurface);
2830                                 }
2831                                 IDirect3DSurface9_Release(texturesurface);
2832                         }
2833                 }
2834 #endif
2835                 break;
2836         case RENDERPATH_D3D10:
2837                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2838                 break;
2839         case RENDERPATH_D3D11:
2840                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2841                 break;
2842         }
2843 }
2844
2845 #ifdef SUPPORTD3D
2846 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};
2847 #endif
2848
2849 void R_Mesh_TexBind(unsigned int unitnum, rtexture_t *tex)
2850 {
2851         gltextureunit_t *unit = gl_state.units + unitnum;
2852         int tex2d, tex3d, texcubemap, texnum;
2853         if (unitnum >= vid.teximageunits)
2854                 return;
2855 //      if (unit->texture == tex)
2856 //              return;
2857         switch(vid.renderpath)
2858         {
2859         case RENDERPATH_GL20:
2860         case RENDERPATH_CGGL:
2861                 if (!tex)
2862                 {
2863                         tex = r_texture_white;
2864                         // not initialized enough yet...
2865                         if (!tex)
2866                                 return;
2867                 }
2868                 unit->texture = tex;
2869                 texnum = R_GetTexture(tex);
2870                 switch(tex->gltexturetypeenum)
2871                 {
2872                 case GL_TEXTURE_2D: if (unit->t2d != texnum) {GL_ActiveTexture(unitnum);unit->t2d = texnum;qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR}break;
2873                 case GL_TEXTURE_3D: if (unit->t3d != texnum) {GL_ActiveTexture(unitnum);unit->t3d = texnum;qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR}break;
2874                 case GL_TEXTURE_CUBE_MAP_ARB: if (unit->tcubemap != texnum) {GL_ActiveTexture(unitnum);unit->tcubemap = texnum;qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR}break;
2875                 case GL_TEXTURE_RECTANGLE_ARB: if (unit->trectangle != texnum) {GL_ActiveTexture(unitnum);unit->trectangle = texnum;qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR}break;
2876                 }
2877                 break;
2878         case RENDERPATH_GL13:
2879         case RENDERPATH_GL11:
2880                 unit->texture = tex;
2881                 tex2d = 0;
2882                 tex3d = 0;
2883                 texcubemap = 0;
2884                 if (tex)
2885                 {
2886                         texnum = R_GetTexture(tex);
2887                         switch(tex->gltexturetypeenum)
2888                         {
2889                         case GL_TEXTURE_2D:
2890                                 tex2d = texnum;
2891                                 break;
2892                         case GL_TEXTURE_3D:
2893                                 tex3d = texnum;
2894                                 break;
2895                         case GL_TEXTURE_CUBE_MAP_ARB:
2896                                 texcubemap = texnum;
2897                                 break;
2898                         }
2899                 }
2900                 // update 2d texture binding
2901                 if (unit->t2d != tex2d)
2902                 {
2903                         GL_ActiveTexture(unitnum);
2904                         if (tex2d)
2905                         {
2906                                 if (unit->t2d == 0)
2907                                 {
2908                                         qglEnable(GL_TEXTURE_2D);CHECKGLERROR
2909                                 }
2910                         }
2911                         else
2912                         {
2913                                 if (unit->t2d)
2914                                 {
2915                                         qglDisable(GL_TEXTURE_2D);CHECKGLERROR
2916                                 }
2917                         }
2918                         unit->t2d = tex2d;
2919                         qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
2920                 }
2921                 // update 3d texture binding
2922                 if (unit->t3d != tex3d)
2923                 {
2924                         GL_ActiveTexture(unitnum);
2925                         if (tex3d)
2926                         {
2927                                 if (unit->t3d == 0)
2928                                 {
2929                                         qglEnable(GL_TEXTURE_3D);CHECKGLERROR
2930                                 }
2931                         }
2932                         else
2933                         {
2934                                 if (unit->t3d)
2935                                 {
2936                                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
2937                                 }
2938                         }
2939                         unit->t3d = tex3d;
2940                         qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
2941                 }
2942                 // update cubemap texture binding
2943                 if (unit->tcubemap != texcubemap)
2944                 {
2945                         GL_ActiveTexture(unitnum);
2946                         if (texcubemap)
2947                         {
2948                                 if (unit->tcubemap == 0)
2949                                 {
2950                                         qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2951                                 }
2952                         }
2953                         else
2954                         {
2955                                 if (unit->tcubemap)
2956                                 {
2957                                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2958                                 }
2959                         }
2960                         unit->tcubemap = texcubemap;
2961                         qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
2962                 }
2963                 break;
2964         case RENDERPATH_D3D9:
2965 #ifdef SUPPORTD3D
2966                 {
2967                         extern cvar_t gl_texture_anisotropy;
2968                         if (!tex)
2969                         {
2970                                 tex = r_texture_white;
2971                                 // not initialized enough yet...
2972                                 if (!tex)
2973                                         return;
2974                         }
2975                         if (unit->texture == tex)
2976                                 return;
2977                         unit->texture = tex;
2978                         // upload texture if needed
2979                         if (tex->dirty)
2980                                 R_RealGetTexture(tex);
2981                         IDirect3DDevice9_SetTexture(vid_d3d9dev, unitnum, (IDirect3DBaseTexture9*)tex->d3dtexture);
2982                         //IDirect3DDevice9_SetRenderState(vid_d3d9dev, d3drswrap[unitnum], (tex->flags & TEXF_CLAMP) ? (D3DWRAPCOORD_0 | D3DWRAPCOORD_1 | D3DWRAPCOORD_2) : 0);
2983                         IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSU, tex->d3daddressu);
2984                         IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSV, tex->d3daddressv);
2985                         if (tex->d3daddressw)
2986                                 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSW,  tex->d3daddressw);
2987                         IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAGFILTER, tex->d3dmagfilter);
2988                         IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MINFILTER, tex->d3dminfilter);
2989                         IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MIPFILTER, tex->d3dmipfilter);
2990                         IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MIPMAPLODBIAS, tex->d3dmipmaplodbias);
2991                         IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAXMIPLEVEL, tex->d3dmaxmiplevelfilter);
2992                         IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAXANISOTROPY, gl_texture_anisotropy.integer);
2993                 }
2994 #endif
2995                 break;
2996         case RENDERPATH_D3D10:
2997                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2998                 break;
2999         case RENDERPATH_D3D11:
3000                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3001                 break;
3002         }
3003 }
3004
3005 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
3006 {
3007         gltextureunit_t *unit = gl_state.units + unitnum;
3008         switch(vid.renderpath)
3009         {
3010         case RENDERPATH_GL11:
3011         case RENDERPATH_GL13:
3012         case RENDERPATH_GL20:
3013         case RENDERPATH_CGGL:
3014                 if (matrix && matrix->m[3][3])
3015                 {
3016                         // texmatrix specified, check if it is different
3017                         if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
3018                         {
3019                                 float glmatrix[16];
3020                                 unit->texmatrixenabled = true;
3021                                 unit->matrix = *matrix;
3022                                 CHECKGLERROR
3023                                 Matrix4x4_ToArrayFloatGL(&unit->matrix, glmatrix);
3024                                 GL_ActiveTexture(unitnum);
3025                                 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3026                                 qglLoadMatrixf(glmatrix);CHECKGLERROR
3027                                 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3028                         }
3029                 }
3030                 else
3031                 {
3032                         // no texmatrix specified, revert to identity
3033                         if (unit->texmatrixenabled)
3034                         {
3035                                 unit->texmatrixenabled = false;
3036                                 unit->matrix = identitymatrix;
3037                                 CHECKGLERROR
3038                                 GL_ActiveTexture(unitnum);
3039                                 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3040                                 qglLoadIdentity();CHECKGLERROR
3041                                 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3042                         }
3043                 }
3044                 break;
3045         case RENDERPATH_D3D9:
3046         case RENDERPATH_D3D10:
3047         case RENDERPATH_D3D11:
3048                 break;
3049         }
3050 }
3051
3052 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
3053 {
3054         gltextureunit_t *unit = gl_state.units + unitnum;
3055         CHECKGLERROR
3056         switch(vid.renderpath)
3057         {
3058         case RENDERPATH_GL20:
3059         case RENDERPATH_CGGL:
3060                 // do nothing
3061                 break;
3062         case RENDERPATH_GL13:
3063                 // GL_ARB_texture_env_combine
3064                 if (!combinergb)
3065                         combinergb = GL_MODULATE;
3066                 if (!combinealpha)
3067                         combinealpha = GL_MODULATE;
3068                 if (!rgbscale)
3069                         rgbscale = 1;
3070                 if (!alphascale)
3071                         alphascale = 1;
3072                 if (combinergb != combinealpha || rgbscale != 1 || alphascale != 1)
3073                 {
3074                         if (combinergb == GL_DECAL)
3075                                 combinergb = GL_INTERPOLATE_ARB;
3076                         if (unit->combine != GL_COMBINE_ARB)
3077                         {
3078                                 unit->combine = GL_COMBINE_ARB;
3079                                 GL_ActiveTexture(unitnum);
3080                                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
3081                                 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE_ARB mode
3082                         }
3083                         if (unit->combinergb != combinergb)
3084                         {
3085                                 unit->combinergb = combinergb;
3086                                 GL_ActiveTexture(unitnum);
3087                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
3088                         }
3089                         if (unit->combinealpha != combinealpha)
3090                         {
3091                                 unit->combinealpha = combinealpha;
3092                                 GL_ActiveTexture(unitnum);
3093                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
3094                         }
3095                         if (unit->rgbscale != rgbscale)
3096                         {
3097                                 unit->rgbscale = rgbscale;
3098                                 GL_ActiveTexture(unitnum);
3099                                 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, unit->rgbscale);CHECKGLERROR
3100                         }
3101                         if (unit->alphascale != alphascale)
3102                         {
3103                                 unit->alphascale = alphascale;
3104                                 GL_ActiveTexture(unitnum);
3105                                 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, unit->alphascale);CHECKGLERROR
3106                         }
3107                 }
3108                 else
3109                 {
3110                         if (unit->combine != combinergb)
3111                         {
3112                                 unit->combine = combinergb;
3113                                 GL_ActiveTexture(unitnum);
3114                                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
3115                         }
3116                 }
3117                 break;
3118         case RENDERPATH_GL11:
3119                 // normal GL texenv
3120                 if (!combinergb)
3121                         combinergb = GL_MODULATE;
3122                 if (unit->combine != combinergb)
3123                 {
3124                         unit->combine = combinergb;
3125                         GL_ActiveTexture(unitnum);
3126                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
3127                 }
3128                 break;
3129         case RENDERPATH_D3D9:
3130         case RENDERPATH_D3D10:
3131         case RENDERPATH_D3D11:
3132                 break;
3133         }
3134 }
3135
3136 void R_Mesh_ResetTextureState(void)
3137 {
3138         unsigned int unitnum;
3139
3140         BACKENDACTIVECHECK
3141
3142         CHECKGLERROR
3143         switch(vid.renderpath)
3144         {
3145         case RENDERPATH_GL20:
3146         case RENDERPATH_CGGL:
3147                 for (unitnum = 0;unitnum < vid.teximageunits;unitnum++)
3148                 {
3149                         gltextureunit_t *unit = gl_state.units + unitnum;
3150                         if (unit->t2d)
3151                         {
3152                                 unit->t2d = 0;
3153                                 GL_ActiveTexture(unitnum);
3154                                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
3155                         }
3156                         if (unit->t3d)
3157                         {
3158                                 unit->t3d = 0;
3159                                 GL_ActiveTexture(unitnum);
3160                                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
3161                         }
3162                         if (unit->tcubemap)
3163                         {
3164                                 unit->tcubemap = 0;
3165                                 GL_ActiveTexture(unitnum);
3166                                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
3167                         }
3168                         if (unit->trectangle)
3169                         {
3170                                 unit->trectangle = 0;
3171                                 GL_ActiveTexture(unitnum);
3172                                 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
3173                         }
3174                 }
3175                 for (unitnum = 0;unitnum < vid.texarrayunits;unitnum++)
3176                 {
3177                         gltextureunit_t *unit = gl_state.units + unitnum;
3178                         if (unit->arrayenabled)
3179                         {
3180                                 unit->arrayenabled = false;
3181                                 GL_ClientActiveTexture(unitnum);
3182                                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
3183                         }
3184                 }
3185                 for (unitnum = 0;unitnum < vid.texunits;unitnum++)
3186                 {
3187                         gltextureunit_t *unit = gl_state.units + unitnum;
3188                         if (unit->texmatrixenabled)
3189                         {
3190                                 unit->texmatrixenabled = false;
3191                                 unit->matrix = identitymatrix;
3192                                 CHECKGLERROR
3193                                 GL_ActiveTexture(unitnum);
3194                                 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3195                                 qglLoadIdentity();CHECKGLERROR
3196                                 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3197                         }
3198                 }
3199                 break;
3200         case RENDERPATH_GL13:
3201         case RENDERPATH_GL11:
3202                 for (unitnum = 0;unitnum < vid.texunits;unitnum++)
3203                 {
3204                         gltextureunit_t *unit = gl_state.units + unitnum;
3205                         if (unit->t2d)
3206                         {
3207                                 unit->t2d = 0;
3208                                 GL_ActiveTexture(unitnum);
3209                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
3210                                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
3211                         }
3212                         if (unit->t3d)
3213                         {
3214                                 unit->t3d = 0;
3215                                 GL_ActiveTexture(unitnum);
3216                                 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
3217                                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
3218                         }
3219                         if (unit->tcubemap)
3220                         {
3221                                 unit->tcubemap = 0;
3222                                 GL_ActiveTexture(unitnum);
3223                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
3224                                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
3225                         }
3226                         if (unit->trectangle)
3227                         {
3228                                 unit->trectangle = 0;
3229                                 GL_ActiveTexture(unitnum);
3230                                 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
3231                                 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
3232                         }
3233                         if (unit->arrayenabled)
3234                         {
3235                                 unit->arrayenabled = false;
3236                                 GL_ClientActiveTexture(unitnum);
3237                                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
3238                         }
3239                         if (unit->texmatrixenabled)
3240                         {
3241                                 unit->texmatrixenabled = false;
3242                                 unit->matrix = identitymatrix;
3243                                 CHECKGLERROR
3244                                 GL_ActiveTexture(unitnum);
3245                                 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3246                                 qglLoadIdentity();CHECKGLERROR
3247                                 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3248                         }
3249                         if (unit->combine != GL_MODULATE)
3250                         {
3251                                 unit->combine = GL_MODULATE;
3252                                 GL_ActiveTexture(unitnum);
3253                                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
3254                         }
3255                 }
3256                 break;
3257         case RENDERPATH_D3D9:
3258         case RENDERPATH_D3D10:
3259         case RENDERPATH_D3D11:
3260                 break;
3261         }
3262 }
3263
3264
3265
3266 #ifdef SUPPORTD3D
3267 //#define r_vertexposition_d3d9fvf (D3DFVF_XYZ)
3268 //#define r_vertexgeneric_d3d9fvf (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
3269 //#define r_vertexmesh_d3d9fvf (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX5 | D3DFVF_TEXCOORDSIZE1(3) | D3DFVF_TEXCOORDSIZE2(3) | D3DFVF_TEXCOORDSIZE3(3))
3270
3271 D3DVERTEXELEMENT9 r_vertexposition_d3d9elements[] =
3272 {
3273         {0, (int)((size_t)&((r_vertexposition_t *)0)->vertex3f), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
3274         D3DDECL_END()
3275 };
3276
3277 D3DVERTEXELEMENT9 r_vertexgeneric_d3d9elements[] =
3278 {
3279         {0, (int)((size_t)&((r_vertexgeneric_t *)0)->vertex3f  ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
3280         {0, (int)((size_t)&((r_vertexgeneric_t *)0)->color4ub  ), D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
3281         {0, (int)((size_t)&((r_vertexgeneric_t *)0)->texcoord2f), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
3282         D3DDECL_END()
3283 };
3284
3285 D3DVERTEXELEMENT9 r_vertexmesh_d3d9elements[] =
3286 {
3287         {0, (int)((size_t)&((r_vertexmesh_t *)0)->vertex3f          ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
3288         {0, (int)((size_t)&((r_vertexmesh_t *)0)->color4ub          ), D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
3289         {0, (int)((size_t)&((r_vertexmesh_t *)0)->texcoordtexture2f ), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
3290         {0, (int)((size_t)&((r_vertexmesh_t *)0)->svector3f         ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
3291         {0, (int)((size_t)&((r_vertexmesh_t *)0)->tvector3f         ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 2},
3292         {0, (int)((size_t)&((r_vertexmesh_t *)0)->normal3f          ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 3},
3293         {0, (int)((size_t)&((r_vertexmesh_t *)0)->texcoordlightmap2f), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 4},
3294         D3DDECL_END()
3295 };
3296
3297 IDirect3DVertexDeclaration9 *r_vertexposition_d3d9decl;
3298 IDirect3DVertexDeclaration9 *r_vertexgeneric_d3d9decl;
3299 IDirect3DVertexDeclaration9 *r_vertexmesh_d3d9decl;
3300 #endif
3301
3302 static void R_Mesh_InitVertexDeclarations(void)
3303 {
3304 #ifdef SUPPORTD3D
3305         r_vertexposition_d3d9decl = NULL;
3306         r_vertexgeneric_d3d9decl = NULL;
3307         r_vertexmesh_d3d9decl = NULL;
3308         switch(vid.renderpath)
3309         {
3310         case RENDERPATH_GL20:
3311         case RENDERPATH_CGGL:
3312         case RENDERPATH_GL13:
3313         case RENDERPATH_GL11:
3314                 break;
3315         case RENDERPATH_D3D9:
3316                 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexposition_d3d9elements, &r_vertexposition_d3d9decl);
3317                 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexgeneric_d3d9elements, &r_vertexgeneric_d3d9decl);
3318                 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexmesh_d3d9elements, &r_vertexmesh_d3d9decl);
3319                 break;
3320         case RENDERPATH_D3D10:
3321                 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3322                 break;
3323         case RENDERPATH_D3D11:
3324                 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3325                 break;
3326         }
3327 #endif
3328 }
3329
3330 static void R_Mesh_DestroyVertexDeclarations(void)
3331 {
3332 #ifdef SUPPORTD3D
3333         if (r_vertexposition_d3d9decl)
3334                 IDirect3DVertexDeclaration9_Release(r_vertexposition_d3d9decl);
3335         r_vertexposition_d3d9decl = NULL;
3336         if (r_vertexgeneric_d3d9decl)
3337                 IDirect3DVertexDeclaration9_Release(r_vertexgeneric_d3d9decl);
3338         r_vertexgeneric_d3d9decl = NULL;
3339         if (r_vertexmesh_d3d9decl)
3340                 IDirect3DVertexDeclaration9_Release(r_vertexmesh_d3d9decl);
3341         r_vertexmesh_d3d9decl = NULL;
3342 #endif
3343 }
3344
3345 r_vertexposition_t *R_Mesh_PrepareVertices_Position_Lock(int numvertices)
3346 {
3347         size_t size;
3348         size = sizeof(r_vertexposition_t) * numvertices;
3349         if (gl_state.preparevertices_tempdatamaxsize < size)
3350         {
3351                 gl_state.preparevertices_tempdatamaxsize = size;
3352                 gl_state.preparevertices_tempdata = Mem_Realloc(r_main_mempool, gl_state.preparevertices_tempdata, gl_state.preparevertices_tempdatamaxsize);
3353         }
3354         gl_state.preparevertices_vertexposition = (r_vertexposition_t *)gl_state.preparevertices_tempdata;
3355         gl_state.preparevertices_numvertices = numvertices;
3356         return gl_state.preparevertices_vertexposition;
3357 }
3358
3359 qboolean R_Mesh_PrepareVertices_Position_Unlock(void)
3360 {
3361         R_Mesh_PrepareVertices_Position(gl_state.preparevertices_numvertices, gl_state.preparevertices_vertexposition, NULL);
3362         gl_state.preparevertices_vertexposition = NULL;
3363         gl_state.preparevertices_numvertices = 0;
3364         return true;
3365 }
3366
3367 void R_Mesh_PrepareVertices_Position_Arrays(int numvertices, const float *vertex3f)
3368 {
3369         int i;
3370         r_vertexposition_t *vertex;
3371         switch(vid.renderpath)
3372         {
3373         case RENDERPATH_GL20:
3374         case RENDERPATH_CGGL:
3375                 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3376                 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
3377                 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3378                 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3379                 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3380                 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3381                 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3382                 return;
3383         case RENDERPATH_GL13:
3384         case RENDERPATH_GL11:
3385                 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3386                 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
3387                 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3388                 if (vid.texunits >= 2)
3389                         R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3390                 if (vid.texunits >= 3)
3391                         R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3392                 return;
3393         case RENDERPATH_D3D9:
3394         case RENDERPATH_D3D10:
3395         case RENDERPATH_D3D11:
3396                 break;
3397         }
3398
3399         // no quick path for this case, convert to vertex structs
3400         vertex = R_Mesh_PrepareVertices_Position_Lock(numvertices);
3401         for (i = 0;i < numvertices;i++)
3402                 VectorCopy(vertex3f + 3*i, vertex[i].vertex3f);
3403         R_Mesh_PrepareVertices_Position_Unlock();
3404         R_Mesh_PrepareVertices_Position(numvertices, vertex, NULL);
3405 }
3406
3407 void R_Mesh_PrepareVertices_Position(int numvertices, const r_vertexposition_t *vertex, const r_meshbuffer_t *vertexbuffer)
3408 {
3409         // upload temporary vertexbuffer for this rendering
3410         if (!gl_state.usevbo_staticvertex)
3411                 vertexbuffer = NULL;
3412         if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
3413         {
3414                 if (gl_state.preparevertices_dynamicvertexbuffer)
3415                         R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex, numvertices * sizeof(*vertex));
3416                 else
3417                         gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex, numvertices * sizeof(*vertex), "temporary", false, true, false);
3418                 vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
3419         }
3420         if (vertexbuffer)
3421         {
3422                 switch(vid.renderpath)
3423                 {
3424                 case RENDERPATH_GL20:
3425                 case RENDERPATH_CGGL:
3426                         R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3427                         R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3428                         R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3429                 case RENDERPATH_GL13:
3430                         R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3431                 case RENDERPATH_GL11:
3432                         R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , vertexbuffer, (int)((unsigned char *)vertex->vertex3f           - (unsigned char *)vertex));
3433                         R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
3434                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3435                         break;
3436                 case RENDERPATH_D3D9:
3437 #ifdef SUPPORTD3D
3438                         IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertexposition_d3d9decl);
3439                         IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(*vertex));
3440 #endif
3441                         break;
3442                 case RENDERPATH_D3D10:
3443                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3444                         break;
3445                 case RENDERPATH_D3D11:
3446                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3447                         break;
3448                 }
3449                 return;
3450         }
3451         switch(vid.renderpath)
3452         {
3453         case RENDERPATH_GL20:
3454         case RENDERPATH_CGGL:
3455                 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3456                 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3457                 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3458         case RENDERPATH_GL13:
3459                 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3460         case RENDERPATH_GL11:
3461                 R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , NULL, 0);
3462                 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
3463                 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3464                 break;
3465         case RENDERPATH_D3D9:
3466         case RENDERPATH_D3D10:
3467         case RENDERPATH_D3D11:
3468                 break;
3469         }
3470 }
3471
3472
3473
3474 r_vertexgeneric_t *R_Mesh_PrepareVertices_Generic_Lock(int numvertices)
3475 {
3476         size_t size;
3477         size = sizeof(r_vertexgeneric_t) * numvertices;
3478         if (gl_state.preparevertices_tempdatamaxsize < size)
3479         {
3480                 gl_state.preparevertices_tempdatamaxsize = size;
3481                 gl_state.preparevertices_tempdata = Mem_Realloc(r_main_mempool, gl_state.preparevertices_tempdata, gl_state.preparevertices_tempdatamaxsize);
3482         }
3483         gl_state.preparevertices_vertexgeneric = (r_vertexgeneric_t *)gl_state.preparevertices_tempdata;
3484         gl_state.preparevertices_numvertices = numvertices;
3485         return gl_state.preparevertices_vertexgeneric;
3486 }
3487
3488 qboolean R_Mesh_PrepareVertices_Generic_Unlock(void)
3489 {
3490         R_Mesh_PrepareVertices_Generic(gl_state.preparevertices_numvertices, gl_state.preparevertices_vertexgeneric, NULL);
3491         gl_state.preparevertices_vertexgeneric = NULL;
3492         gl_state.preparevertices_numvertices = 0;
3493         return true;
3494 }
3495
3496 void R_Mesh_PrepareVertices_Generic_Arrays(int numvertices, const float *vertex3f, const float *color4f, const float *texcoord2f)
3497 {
3498         int i;
3499         r_vertexgeneric_t *vertex;
3500         switch(vid.renderpath)
3501         {
3502         case RENDERPATH_GL20:
3503         case RENDERPATH_CGGL:
3504                 if (gl_mesh_separatearrays.integer)
3505                 {
3506                         R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3507                         R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
3508                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoord2f, NULL, 0);
3509                         R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3510                         R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3511                         R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3512                         R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3513                         return;
3514                 }
3515                 break;
3516         case RENDERPATH_GL13:
3517         case RENDERPATH_GL11:
3518                 if (gl_mesh_separatearrays.integer)
3519                 {
3520                         R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3521                         R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
3522                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoord2f, NULL, 0);
3523                         if (vid.texunits >= 2)
3524                                 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3525                         if (vid.texunits >= 3)
3526                                 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3527                         return;
3528                 }
3529                 break;
3530         case RENDERPATH_D3D9:
3531         case RENDERPATH_D3D10:
3532         case RENDERPATH_D3D11:
3533                 break;
3534         }
3535
3536         // no quick path for this case, convert to vertex structs
3537         vertex = R_Mesh_PrepareVertices_Generic_Lock(numvertices);
3538         for (i = 0;i < numvertices;i++)
3539                 VectorCopy(vertex3f + 3*i, vertex[i].vertex3f);
3540         if (color4f)
3541         {
3542                 for (i = 0;i < numvertices;i++)
3543                         Vector4Scale(color4f + 4*i, 255.0f, vertex[i].color4ub);
3544         }
3545         else
3546         {
3547                 float tempcolor4f[4];
3548                 unsigned char tempcolor4ub[4];
3549                 Vector4Scale(gl_state.color4f, 255.0f, tempcolor4f);
3550                 tempcolor4ub[0] = (unsigned char)bound(0.0f, tempcolor4f[0], 255.0f);
3551                 tempcolor4ub[1] = (unsigned char)bound(0.0f, tempcolor4f[1], 255.0f);
3552                 tempcolor4ub[2] = (unsigned char)bound(0.0f, tempcolor4f[2], 255.0f);
3553                 tempcolor4ub[3] = (unsigned char)bound(0.0f, tempcolor4f[3], 255.0f);
3554                 for (i = 0;i < numvertices;i++)
3555                         Vector4Copy(tempcolor4ub, vertex[i].color4ub);
3556         }
3557         if (texcoord2f)
3558                 for (i = 0;i < numvertices;i++)
3559                         Vector2Copy(texcoord2f + 2*i, vertex[i].texcoord2f);
3560         R_Mesh_PrepareVertices_Generic_Unlock();
3561         R_Mesh_PrepareVertices_Generic(numvertices, vertex, NULL);
3562 }
3563
3564 void R_Mesh_PrepareVertices_Generic(int numvertices, const r_vertexgeneric_t *vertex, const r_meshbuffer_t *vertexbuffer)
3565 {
3566         // upload temporary vertexbuffer for this rendering
3567         if (!gl_state.usevbo_staticvertex)
3568                 vertexbuffer = NULL;
3569         if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
3570         {
3571                 if (gl_state.preparevertices_dynamicvertexbuffer)
3572                         R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex, numvertices * sizeof(*vertex));
3573                 else
3574                         gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex, numvertices * sizeof(*vertex), "temporary", false, true, false);
3575                 vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
3576         }
3577         if (vertexbuffer)
3578         {
3579                 switch(vid.renderpath)
3580                 {
3581                 case RENDERPATH_GL20:
3582                 case RENDERPATH_CGGL:
3583                         R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3584                         R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3585                         R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3586                 case RENDERPATH_GL13:
3587                         R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3588                 case RENDERPATH_GL11:
3589                         R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , vertexbuffer, (int)((unsigned char *)vertex->vertex3f           - (unsigned char *)vertex));
3590                         R_Mesh_ColorPointer(      4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub          , vertexbuffer, (int)((unsigned char *)vertex->color4ub           - (unsigned char *)vertex));
3591                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoord2f        , vertexbuffer, (int)((unsigned char *)vertex->texcoord2f         - (unsigned char *)vertex));
3592                         break;
3593                 case RENDERPATH_D3D9:
3594 #ifdef SUPPORTD3D
3595                         IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertexgeneric_d3d9decl);
3596                         IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(*vertex));
3597 #endif
3598                         break;
3599                 case RENDERPATH_D3D10:
3600                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3601                         break;
3602                 case RENDERPATH_D3D11:
3603                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3604                         break;
3605                 }
3606                 return;
3607         }
3608         switch(vid.renderpath)
3609         {
3610         case RENDERPATH_GL20:
3611         case RENDERPATH_CGGL:
3612                 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3613                 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3614                 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3615         case RENDERPATH_GL13:
3616                 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3617         case RENDERPATH_GL11:
3618                 R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , NULL, 0);
3619                 R_Mesh_ColorPointer(      4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub          , NULL, 0);
3620                 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoord2f        , NULL, 0);
3621                 break;
3622         case RENDERPATH_D3D9:
3623         case RENDERPATH_D3D10:
3624         case RENDERPATH_D3D11:
3625                 break;
3626         }
3627 }
3628
3629
3630
3631 r_vertexmesh_t *R_Mesh_PrepareVertices_Mesh_Lock(int numvertices)
3632 {
3633         size_t size;
3634         size = sizeof(r_vertexmesh_t) * numvertices;
3635         if (gl_state.preparevertices_tempdatamaxsize < size)
3636         {
3637                 gl_state.preparevertices_tempdatamaxsize = size;
3638                 gl_state.preparevertices_tempdata = Mem_Realloc(r_main_mempool, gl_state.preparevertices_tempdata, gl_state.preparevertices_tempdatamaxsize);
3639         }
3640         gl_state.preparevertices_vertexmesh = (r_vertexmesh_t *)gl_state.preparevertices_tempdata;
3641         gl_state.preparevertices_numvertices = numvertices;
3642         return gl_state.preparevertices_vertexmesh;
3643 }
3644
3645 qboolean R_Mesh_PrepareVertices_Mesh_Unlock(void)
3646 {
3647         R_Mesh_PrepareVertices_Mesh(gl_state.preparevertices_numvertices, gl_state.preparevertices_vertexmesh, NULL);
3648         gl_state.preparevertices_vertexmesh = NULL;
3649         gl_state.preparevertices_numvertices = 0;
3650         return true;
3651 }
3652
3653 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)
3654 {
3655         int i;
3656         r_vertexmesh_t *vertex;
3657         switch(vid.renderpath)
3658         {
3659         case RENDERPATH_GL20:
3660         case RENDERPATH_CGGL:
3661                 if (gl_mesh_separatearrays.integer)
3662                 {
3663                         R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3664                         R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
3665                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoordtexture2f, NULL, 0);
3666                         R_Mesh_TexCoordPointer(1, 3, GL_FLOAT, sizeof(float[3]), svector3f, NULL, 0);
3667                         R_Mesh_TexCoordPointer(2, 3, GL_FLOAT, sizeof(float[3]), tvector3f, NULL, 0);
3668                         R_Mesh_TexCoordPointer(3, 3, GL_FLOAT, sizeof(float[3]), normal3f, NULL, 0);
3669                         R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), texcoordlightmap2f, NULL, 0);
3670                         return;
3671                 }
3672                 break;
3673         case RENDERPATH_GL13:
3674         case RENDERPATH_GL11:
3675                 if (gl_mesh_separatearrays.integer)
3676                 {
3677                         R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3678                         R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
3679                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoordtexture2f, NULL, 0);
3680                         if (vid.texunits >= 2)
3681                                 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), texcoordlightmap2f, NULL, 0);
3682                         if (vid.texunits >= 3)
3683                                 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3684                         return;
3685                 }
3686                 break;
3687         case RENDERPATH_D3D9:
3688         case RENDERPATH_D3D10:
3689         case RENDERPATH_D3D11:
3690                 break;
3691         }
3692
3693         vertex = R_Mesh_PrepareVertices_Mesh_Lock(numvertices);
3694         for (i = 0;i < numvertices;i++)
3695                 VectorCopy(vertex3f + 3*i, vertex[i].vertex3f);
3696         if (svector3f)
3697                 for (i = 0;i < numvertices;i++)
3698                         VectorCopy(svector3f + 3*i, vertex[i].svector3f);
3699         if (tvector3f)
3700                 for (i = 0;i < numvertices;i++)
3701                         VectorCopy(tvector3f + 3*i, vertex[i].tvector3f);
3702         if (normal3f)
3703                 for (i = 0;i < numvertices;i++)
3704                         VectorCopy(normal3f + 3*i, vertex[i].normal3f);
3705         if (color4f)
3706         {
3707                 for (i = 0;i < numvertices;i++)
3708                         Vector4Scale(color4f + 4*i, 255.0f, vertex[i].color4ub);
3709         }
3710         else
3711         {
3712                 float tempcolor4f[4];
3713                 unsigned char tempcolor4ub[4];
3714                 Vector4Scale(gl_state.color4f, 255.0f, tempcolor4f);
3715                 tempcolor4ub[0] = (unsigned char)bound(0.0f, tempcolor4f[0], 255.0f);
3716                 tempcolor4ub[1] = (unsigned char)bound(0.0f, tempcolor4f[1], 255.0f);
3717                 tempcolor4ub[2] = (unsigned char)bound(0.0f, tempcolor4f[2], 255.0f);
3718                 tempcolor4ub[3] = (unsigned char)bound(0.0f, tempcolor4f[3], 255.0f);
3719                 for (i = 0;i < numvertices;i++)
3720                         Vector4Copy(tempcolor4ub, vertex[i].color4ub);
3721         }
3722         if (texcoordtexture2f)
3723                 for (i = 0;i < numvertices;i++)
3724                         Vector2Copy(texcoordtexture2f + 2*i, vertex[i].texcoordtexture2f);
3725         if (texcoordlightmap2f)
3726                 for (i = 0;i < numvertices;i++)
3727                         Vector2Copy(texcoordlightmap2f + 2*i, vertex[i].texcoordlightmap2f);
3728         R_Mesh_PrepareVertices_Mesh_Unlock();
3729         R_Mesh_PrepareVertices_Mesh(numvertices, vertex, NULL);
3730 }
3731
3732 void R_Mesh_PrepareVertices_Mesh(int numvertices, const r_vertexmesh_t *vertex, const r_meshbuffer_t *vertexbuffer)
3733 {
3734         // upload temporary vertexbuffer for this rendering
3735         if (!gl_state.usevbo_staticvertex)
3736                 vertexbuffer = NULL;
3737         if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
3738         {
3739                 if (gl_state.preparevertices_dynamicvertexbuffer)
3740                         R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex, numvertices * sizeof(*vertex));
3741                 else
3742                         gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex, numvertices * sizeof(*vertex), "temporary", false, true, false);
3743                 vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
3744         }
3745         if (vertexbuffer)
3746         {
3747                 switch(vid.renderpath)
3748                 {
3749                 case RENDERPATH_GL20:
3750                 case RENDERPATH_CGGL:
3751                         R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , vertexbuffer, (int)((unsigned char *)vertex->vertex3f           - (unsigned char *)vertex));
3752                         R_Mesh_ColorPointer(      4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub          , vertexbuffer, (int)((unsigned char *)vertex->color4ub           - (unsigned char *)vertex));
3753                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoordtexture2f , vertexbuffer, (int)((unsigned char *)vertex->texcoordtexture2f  - (unsigned char *)vertex));
3754                         R_Mesh_TexCoordPointer(1, 3, GL_FLOAT        , sizeof(*vertex), vertex->svector3f         , vertexbuffer, (int)((unsigned char *)vertex->svector3f          - (unsigned char *)vertex));
3755                         R_Mesh_TexCoordPointer(2, 3, GL_FLOAT        , sizeof(*vertex), vertex->tvector3f         , vertexbuffer, (int)((unsigned char *)vertex->tvector3f          - (unsigned char *)vertex));
3756                         R_Mesh_TexCoordPointer(3, 4, GL_FLOAT        , sizeof(*vertex), vertex->normal3f          , vertexbuffer, (int)((unsigned char *)vertex->normal3f           - (unsigned char *)vertex));
3757                         R_Mesh_TexCoordPointer(4, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoordlightmap2f, vertexbuffer, (int)((unsigned char *)vertex->texcoordlightmap2f - (unsigned char *)vertex));
3758                         break;
3759                 case RENDERPATH_GL13:
3760                         R_Mesh_TexCoordPointer(1, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoordlightmap2f, vertexbuffer, (int)((unsigned char *)vertex->texcoordlightmap2f - (unsigned char *)vertex));
3761                 case RENDERPATH_GL11:
3762                         R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , vertexbuffer, (int)((unsigned char *)vertex->vertex3f           - (unsigned char *)vertex));
3763                         R_Mesh_ColorPointer(      4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub          , vertexbuffer, (int)((unsigned char *)vertex->color4ub           - (unsigned char *)vertex));
3764                         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoordtexture2f , vertexbuffer, (int)((unsigned char *)vertex->texcoordtexture2f  - (unsigned char *)vertex));
3765                         break;
3766                 case RENDERPATH_D3D9:
3767 #ifdef SUPPORTD3D
3768                         IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertexmesh_d3d9decl);
3769                         IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(*vertex));
3770 #endif
3771                         break;
3772                 case RENDERPATH_D3D10:
3773                         Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3774                         break;
3775                 case RENDERPATH_D3D11:
3776                         Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3777                         break;
3778                 }
3779                 return;
3780         }
3781         switch(vid.renderpath)
3782         {
3783         case RENDERPATH_GL20:
3784         case RENDERPATH_CGGL:
3785                 R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , NULL, 0);
3786                 R_Mesh_ColorPointer(      4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub          , NULL, 0);
3787                 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoordtexture2f , NULL, 0);
3788                 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT        , sizeof(*vertex), vertex->svector3f         , NULL, 0);
3789                 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT        , sizeof(*vertex), vertex->tvector3f         , NULL, 0);
3790                 R_Mesh_TexCoordPointer(3, 4, GL_FLOAT        , sizeof(*vertex), vertex->normal3f          , NULL, 0);
3791                 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoordlightmap2f, NULL, 0);
3792                 break;
3793         case RENDERPATH_GL13:
3794                 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoordlightmap2f, NULL, 0);
3795         case RENDERPATH_GL11:
3796                 R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , NULL, 0);
3797                 R_Mesh_ColorPointer(      4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub          , NULL, 0);
3798                 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT        , sizeof(*vertex), vertex->texcoordtexture2f , NULL, 0);
3799                 break;
3800         case RENDERPATH_D3D9:
3801         case RENDERPATH_D3D10:
3802         case RENDERPATH_D3D11:
3803                 break;
3804         }
3805 }