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