3 #include "cl_collision.h"
4 #include "dpsoftrast.h"
7 extern LPDIRECT3DDEVICE9 vid_d3d9dev;
8 extern D3DCAPS9 vid_d3d9caps;
11 // on GLES we have to use some proper #define's
12 #ifndef GL_FRAMEBUFFER
13 #define GL_FRAMEBUFFER 0x8D40
14 #define GL_DEPTH_ATTACHMENT 0x8D00
15 #define GL_COLOR_ATTACHMENT0 0x8CE0
16 #define GL_INVALID_FRAMEBUFFER_OPERATION 0x0506
18 #ifndef GL_COLOR_ATTACHMENT1
19 #define GL_COLOR_ATTACHMENT1 0x8CE1
20 #define GL_COLOR_ATTACHMENT2 0x8CE2
21 #define GL_COLOR_ATTACHMENT3 0x8CE3
22 #define GL_COLOR_ATTACHMENT4 0x8CE4
23 #define GL_COLOR_ATTACHMENT5 0x8CE5
24 #define GL_COLOR_ATTACHMENT6 0x8CE6
25 #define GL_COLOR_ATTACHMENT7 0x8CE7
26 #define GL_COLOR_ATTACHMENT8 0x8CE8
27 #define GL_COLOR_ATTACHMENT9 0x8CE9
28 #define GL_COLOR_ATTACHMENT10 0x8CEA
29 #define GL_COLOR_ATTACHMENT11 0x8CEB
30 #define GL_COLOR_ATTACHMENT12 0x8CEC
31 #define GL_COLOR_ATTACHMENT13 0x8CED
32 #define GL_COLOR_ATTACHMENT14 0x8CEE
33 #define GL_COLOR_ATTACHMENT15 0x8CEF
35 #ifndef GL_ARRAY_BUFFER
36 #define GL_ARRAY_BUFFER 0x8892
37 #define GL_ELEMENT_ARRAY_BUFFER 0x8893
39 //#ifndef GL_VERTEX_ARRAY
40 //#define GL_VERTEX_ARRAY 0x8074
41 //#define GL_COLOR_ARRAY 0x8076
42 //#define GL_TEXTURE_COORD_ARRAY 0x8078
45 #define GL_TEXTURE0 0x84C0
46 #define GL_TEXTURE1 0x84C1
47 #define GL_TEXTURE2 0x84C2
48 #define GL_TEXTURE3 0x84C3
49 #define GL_TEXTURE4 0x84C4
50 #define GL_TEXTURE5 0x84C5
51 #define GL_TEXTURE6 0x84C6
52 #define GL_TEXTURE7 0x84C7
53 #define GL_TEXTURE8 0x84C8
54 #define GL_TEXTURE9 0x84C9
55 #define GL_TEXTURE10 0x84CA
56 #define GL_TEXTURE11 0x84CB
57 #define GL_TEXTURE12 0x84CC
58 #define GL_TEXTURE13 0x84CD
59 #define GL_TEXTURE14 0x84CE
60 #define GL_TEXTURE15 0x84CF
61 #define GL_TEXTURE16 0x84D0
62 #define GL_TEXTURE17 0x84D1
63 #define GL_TEXTURE18 0x84D2
64 #define GL_TEXTURE19 0x84D3
65 #define GL_TEXTURE20 0x84D4
66 #define GL_TEXTURE21 0x84D5
67 #define GL_TEXTURE22 0x84D6
68 #define GL_TEXTURE23 0x84D7
69 #define GL_TEXTURE24 0x84D8
70 #define GL_TEXTURE25 0x84D9
71 #define GL_TEXTURE26 0x84DA
72 #define GL_TEXTURE27 0x84DB
73 #define GL_TEXTURE28 0x84DC
74 #define GL_TEXTURE29 0x84DD
75 #define GL_TEXTURE30 0x84DE
76 #define GL_TEXTURE31 0x84DF
80 #define GL_TEXTURE_3D 0x806F
82 #ifndef GL_TEXTURE_CUBE_MAP
83 #define GL_TEXTURE_CUBE_MAP 0x8513
85 //#ifndef GL_MODELVIEW
86 //#define GL_MODELVIEW 0x1700
88 //#ifndef GL_PROJECTION
89 //#define GL_PROJECTION 0x1701
92 //#define GL_DECAL 0x2101
94 //#ifndef GL_INTERPOLATE
95 //#define GL_INTERPOLATE 0x8575
99 #define MAX_RENDERTARGETS 4
101 cvar_t gl_mesh_drawrangeelements = {0, "gl_mesh_drawrangeelements", "1", "use glDrawRangeElements function if available instead of glDrawElements (for performance comparisons or bug testing)"};
102 cvar_t gl_mesh_testmanualfeeding = {0, "gl_mesh_testmanualfeeding", "0", "use glBegin(GL_TRIANGLES);glTexCoord2f();glVertex3f();glEnd(); primitives instead of glDrawElements (useful to test for driver bugs with glDrawElements)"};
103 cvar_t gl_mesh_prefer_short_elements = {CVAR_SAVE, "gl_mesh_prefer_short_elements", "1", "use GL_UNSIGNED_SHORT element arrays instead of GL_UNSIGNED_INT"};
104 cvar_t gl_paranoid = {0, "gl_paranoid", "0", "enables OpenGL error checking and other tests"};
105 cvar_t gl_printcheckerror = {0, "gl_printcheckerror", "0", "prints all OpenGL error checks, useful to identify location of driver crashes"};
107 cvar_t r_render = {0, "r_render", "1", "enables rendering 3D views (you want this on!)"};
108 cvar_t r_renderview = {0, "r_renderview", "1", "enables rendering 3D views (you want this on!)"};
109 cvar_t r_waterwarp = {CVAR_SAVE, "r_waterwarp", "1", "warp view while underwater"};
110 cvar_t gl_polyblend = {CVAR_SAVE, "gl_polyblend", "1", "tints view while underwater, hurt, etc"};
111 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1", "enables OpenGL dithering (16bit looks bad with this off)"};
112 cvar_t gl_vbo = {CVAR_SAVE, "gl_vbo", "3", "make use of GL_ARB_vertex_buffer_object extension to store static geometry in video memory for faster rendering, 0 disables VBO allocation or use, 1 enables VBOs for vertex and triangle data, 2 only for vertex data, 3 for vertex data and triangle data of simple meshes (ones with only one surface)"};
113 cvar_t gl_vbo_dynamicvertex = {CVAR_SAVE, "gl_vbo_dynamicvertex", "0", "make use of GL_ARB_vertex_buffer_object extension when rendering dynamic (animated/procedural) geometry such as text and particles"};
114 cvar_t gl_vbo_dynamicindex = {CVAR_SAVE, "gl_vbo_dynamicindex", "0", "make use of GL_ARB_vertex_buffer_object extension when rendering dynamic (animated/procedural) geometry such as text and particles"};
115 cvar_t gl_fbo = {CVAR_SAVE, "gl_fbo", "1", "make use of GL_ARB_framebuffer_object extension to enable shadowmaps and other features using pixel formats different from the framebuffer"};
117 cvar_t v_flipped = {0, "v_flipped", "0", "mirror the screen (poor man's left handed mode)"};
118 qboolean v_flipped_state = false;
120 r_viewport_t gl_viewport;
121 matrix4x4_t gl_modelmatrix;
122 matrix4x4_t gl_viewmatrix;
123 matrix4x4_t gl_modelviewmatrix;
124 matrix4x4_t gl_projectionmatrix;
125 matrix4x4_t gl_modelviewprojectionmatrix;
126 float gl_modelview16f[16];
127 float gl_modelviewprojection16f[16];
128 qboolean gl_modelmatrixchanged;
130 int gl_maxdrawrangeelementsvertices;
131 int gl_maxdrawrangeelementsindices;
136 void GL_PrintError(int errornumber, const char *filename, int linenumber)
140 #ifdef GL_INVALID_ENUM
141 case GL_INVALID_ENUM:
142 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
145 #ifdef GL_INVALID_VALUE
146 case GL_INVALID_VALUE:
147 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
150 #ifdef GL_INVALID_OPERATION
151 case GL_INVALID_OPERATION:
152 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
155 #ifdef GL_STACK_OVERFLOW
156 case GL_STACK_OVERFLOW:
157 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
160 #ifdef GL_STACK_UNDERFLOW
161 case GL_STACK_UNDERFLOW:
162 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
165 #ifdef GL_OUT_OF_MEMORY
166 case GL_OUT_OF_MEMORY:
167 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
170 #ifdef GL_TABLE_TOO_LARGE
171 case GL_TABLE_TOO_LARGE:
172 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
175 #ifdef GL_INVALID_FRAMEBUFFER_OPERATION
176 case GL_INVALID_FRAMEBUFFER_OPERATION:
177 Con_Printf("GL_INVALID_FRAMEBUFFER_OPERATION at %s:%i\n", filename, linenumber);
181 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
187 #define BACKENDACTIVECHECK if (!gl_state.active) Sys_Error("GL backend function called when backend is not active");
189 void SCR_ScreenShot_f (void);
191 typedef struct gltextureunit_s
193 int pointer_texcoord_components;
194 int pointer_texcoord_gltype;
195 size_t pointer_texcoord_stride;
196 const void *pointer_texcoord_pointer;
197 const r_meshbuffer_t *pointer_texcoord_vertexbuffer;
198 size_t pointer_texcoord_offset;
201 int t2d, t3d, tcubemap;
203 int rgbscale, alphascale;
205 int combinergb, combinealpha;
206 // texmatrixenabled exists only to avoid unnecessary texmatrix compares
207 int texmatrixenabled;
212 typedef struct gl_state_s
220 int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
224 float polygonoffset[2];
227 float alphafuncvalue;
228 qboolean alphatocoverage;
231 unsigned int clientunit;
232 gltextureunit_t units[MAX_TEXTUREUNITS];
236 int vertexbufferobject;
237 int elementbufferobject;
238 int framebufferobject;
239 int defaultframebufferobject; // deal with platforms that use a non-zero default fbo
240 qboolean pointer_color_enabled;
242 int pointer_vertex_components;
243 int pointer_vertex_gltype;
244 size_t pointer_vertex_stride;
245 const void *pointer_vertex_pointer;
246 const r_meshbuffer_t *pointer_vertex_vertexbuffer;
247 size_t pointer_vertex_offset;
249 int pointer_color_components;
250 int pointer_color_gltype;
251 size_t pointer_color_stride;
252 const void *pointer_color_pointer;
253 const r_meshbuffer_t *pointer_color_vertexbuffer;
254 size_t pointer_color_offset;
256 void *preparevertices_tempdata;
257 size_t preparevertices_tempdatamaxsize;
258 r_meshbuffer_t *preparevertices_dynamicvertexbuffer;
259 r_vertexgeneric_t *preparevertices_vertexgeneric;
260 r_vertexmesh_t *preparevertices_vertexmesh;
261 int preparevertices_numvertices;
263 r_meshbuffer_t *draw_dynamicindexbuffer;
265 qboolean usevbo_staticvertex;
266 qboolean usevbo_staticindex;
267 qboolean usevbo_dynamicvertex;
268 qboolean usevbo_dynamicindex;
270 memexpandablearray_t meshbufferarray;
275 // rtexture_t *d3drt_depthtexture;
276 // rtexture_t *d3drt_colortextures[MAX_RENDERTARGETS];
277 IDirect3DSurface9 *d3drt_depthsurface;
278 IDirect3DSurface9 *d3drt_colorsurfaces[MAX_RENDERTARGETS];
279 IDirect3DSurface9 *d3drt_backbufferdepthsurface;
280 IDirect3DSurface9 *d3drt_backbuffercolorsurface;
281 void *d3dvertexbuffer;
283 size_t d3dvertexsize;
288 static gl_state_t gl_state;
292 note: here's strip order for a terrain row:
299 A0B, 01B, B1C, 12C, C2D, 23D, D3E, 34E
301 *elements++ = i + row;
303 *elements++ = i + row + 1;
306 *elements++ = i + row + 1;
309 for (y = 0;y < rows - 1;y++)
311 for (x = 0;x < columns - 1;x++)
314 *elements++ = i + columns;
316 *elements++ = i + columns + 1;
319 *elements++ = i + columns + 1;
330 for (y = 0;y < rows - 1;y++)
332 for (x = 0;x < columns - 1;x++)
336 *elements++ = i + columns;
337 *elements++ = i + columns + 1;
338 *elements++ = i + columns;
339 *elements++ = i + columns + 1;
345 int polygonelement3i[(POLYGONELEMENTS_MAXPOINTS-2)*3];
346 unsigned short polygonelement3s[(POLYGONELEMENTS_MAXPOINTS-2)*3];
347 int quadelement3i[QUADELEMENTS_MAXQUADS*6];
348 unsigned short quadelement3s[QUADELEMENTS_MAXQUADS*6];
350 static void GL_VBOStats_f(void)
352 GL_Mesh_ListVBOs(true);
355 static void GL_Backend_ResetState(void);
357 static void R_Mesh_InitVertexDeclarations(void);
358 static void R_Mesh_DestroyVertexDeclarations(void);
360 static void R_Mesh_SetUseVBO(void)
362 switch(vid.renderpath)
364 case RENDERPATH_GL11:
365 case RENDERPATH_GL13:
366 case RENDERPATH_GL20:
367 case RENDERPATH_GLES1:
368 gl_state.usevbo_staticvertex = (vid.support.arb_vertex_buffer_object && gl_vbo.integer) || vid.forcevbo;
369 gl_state.usevbo_staticindex = (vid.support.arb_vertex_buffer_object && (gl_vbo.integer == 1 || gl_vbo.integer == 3)) || vid.forcevbo;
370 gl_state.usevbo_dynamicvertex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicvertex.integer) || vid.forcevbo;
371 gl_state.usevbo_dynamicindex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicindex.integer) || vid.forcevbo;
373 case RENDERPATH_D3D9:
374 gl_state.usevbo_staticvertex = gl_state.usevbo_staticindex = (vid.support.arb_vertex_buffer_object && gl_vbo.integer) || vid.forcevbo;
375 gl_state.usevbo_dynamicvertex = gl_state.usevbo_dynamicindex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicvertex.integer && gl_vbo_dynamicindex.integer) || vid.forcevbo;
377 case RENDERPATH_D3D10:
378 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
380 case RENDERPATH_D3D11:
381 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
383 case RENDERPATH_SOFT:
384 gl_state.usevbo_staticvertex = false;
385 gl_state.usevbo_staticindex = false;
386 gl_state.usevbo_dynamicvertex = false;
387 gl_state.usevbo_dynamicindex = false;
389 case RENDERPATH_GLES2:
390 gl_state.usevbo_staticvertex = (vid.support.arb_vertex_buffer_object && gl_vbo.integer) || vid.forcevbo;
391 gl_state.usevbo_staticindex = false;
392 gl_state.usevbo_dynamicvertex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicvertex.integer) || vid.forcevbo;
393 gl_state.usevbo_dynamicindex = false;
398 static void gl_backend_start(void)
400 memset(&gl_state, 0, sizeof(gl_state));
402 R_Mesh_InitVertexDeclarations();
405 Mem_ExpandableArray_NewArray(&gl_state.meshbufferarray, r_main_mempool, sizeof(r_meshbuffer_t), 128);
407 Con_DPrintf("OpenGL backend started.\n");
411 GL_Backend_ResetState();
413 switch(vid.renderpath)
415 case RENDERPATH_GL11:
416 case RENDERPATH_GL13:
417 case RENDERPATH_GL20:
418 case RENDERPATH_GLES1:
419 case RENDERPATH_GLES2:
420 // fetch current fbo here (default fbo is not 0 on some GLES devices)
421 if (vid.support.ext_framebuffer_object)
422 qglGetIntegerv(GL_FRAMEBUFFER_BINDING, &gl_state.defaultframebufferobject);
424 case RENDERPATH_D3D9:
426 IDirect3DDevice9_GetDepthStencilSurface(vid_d3d9dev, &gl_state.d3drt_backbufferdepthsurface);
427 IDirect3DDevice9_GetRenderTarget(vid_d3d9dev, 0, &gl_state.d3drt_backbuffercolorsurface);
430 case RENDERPATH_D3D10:
431 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
433 case RENDERPATH_D3D11:
434 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
436 case RENDERPATH_SOFT:
441 static void gl_backend_shutdown(void)
443 Con_DPrint("OpenGL Backend shutting down\n");
445 switch(vid.renderpath)
447 case RENDERPATH_GL11:
448 case RENDERPATH_GL13:
449 case RENDERPATH_GL20:
450 case RENDERPATH_SOFT:
451 case RENDERPATH_GLES1:
452 case RENDERPATH_GLES2:
454 case RENDERPATH_D3D9:
456 IDirect3DSurface9_Release(gl_state.d3drt_backbufferdepthsurface);
457 IDirect3DSurface9_Release(gl_state.d3drt_backbuffercolorsurface);
460 case RENDERPATH_D3D10:
461 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
463 case RENDERPATH_D3D11:
464 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
468 if (gl_state.preparevertices_tempdata)
469 Mem_Free(gl_state.preparevertices_tempdata);
470 if (gl_state.preparevertices_dynamicvertexbuffer)
471 R_Mesh_DestroyMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer);
473 Mem_ExpandableArray_FreeArray(&gl_state.meshbufferarray);
475 R_Mesh_DestroyVertexDeclarations();
477 memset(&gl_state, 0, sizeof(gl_state));
480 static void gl_backend_newmap(void)
484 static void gl_backend_devicelost(void)
487 r_meshbuffer_t *buffer;
489 gl_state.d3dvertexbuffer = NULL;
491 switch(vid.renderpath)
493 case RENDERPATH_GL11:
494 case RENDERPATH_GL13:
495 case RENDERPATH_GL20:
496 case RENDERPATH_SOFT:
497 case RENDERPATH_GLES1:
498 case RENDERPATH_GLES2:
500 case RENDERPATH_D3D9:
502 IDirect3DSurface9_Release(gl_state.d3drt_backbufferdepthsurface);
503 IDirect3DSurface9_Release(gl_state.d3drt_backbuffercolorsurface);
506 case RENDERPATH_D3D10:
507 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
509 case RENDERPATH_D3D11:
510 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
513 endindex = Mem_ExpandableArray_IndexRange(&gl_state.meshbufferarray);
514 for (i = 0;i < endindex;i++)
516 buffer = (r_meshbuffer_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.meshbufferarray, i);
517 if (!buffer || !buffer->isdynamic)
519 switch(vid.renderpath)
521 case RENDERPATH_GL11:
522 case RENDERPATH_GL13:
523 case RENDERPATH_GL20:
524 case RENDERPATH_SOFT:
525 case RENDERPATH_GLES1:
526 case RENDERPATH_GLES2:
528 case RENDERPATH_D3D9:
530 if (buffer->devicebuffer)
532 if (buffer->isindexbuffer)
533 IDirect3DIndexBuffer9_Release((IDirect3DIndexBuffer9*)buffer->devicebuffer);
535 IDirect3DVertexBuffer9_Release((IDirect3DVertexBuffer9*)buffer->devicebuffer);
536 buffer->devicebuffer = NULL;
540 case RENDERPATH_D3D10:
541 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
543 case RENDERPATH_D3D11:
544 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
550 static void gl_backend_devicerestored(void)
552 switch(vid.renderpath)
554 case RENDERPATH_GL11:
555 case RENDERPATH_GL13:
556 case RENDERPATH_GL20:
557 case RENDERPATH_SOFT:
558 case RENDERPATH_GLES1:
559 case RENDERPATH_GLES2:
561 case RENDERPATH_D3D9:
563 IDirect3DDevice9_GetDepthStencilSurface(vid_d3d9dev, &gl_state.d3drt_backbufferdepthsurface);
564 IDirect3DDevice9_GetRenderTarget(vid_d3d9dev, 0, &gl_state.d3drt_backbuffercolorsurface);
567 case RENDERPATH_D3D10:
568 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
570 case RENDERPATH_D3D11:
571 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
576 void gl_backend_init(void)
580 for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
582 polygonelement3s[i * 3 + 0] = 0;
583 polygonelement3s[i * 3 + 1] = i + 1;
584 polygonelement3s[i * 3 + 2] = i + 2;
586 // elements for rendering a series of quads as triangles
587 for (i = 0;i < QUADELEMENTS_MAXQUADS;i++)
589 quadelement3s[i * 6 + 0] = i * 4;
590 quadelement3s[i * 6 + 1] = i * 4 + 1;
591 quadelement3s[i * 6 + 2] = i * 4 + 2;
592 quadelement3s[i * 6 + 3] = i * 4;
593 quadelement3s[i * 6 + 4] = i * 4 + 2;
594 quadelement3s[i * 6 + 5] = i * 4 + 3;
597 for (i = 0;i < (POLYGONELEMENTS_MAXPOINTS - 2)*3;i++)
598 polygonelement3i[i] = polygonelement3s[i];
599 for (i = 0;i < QUADELEMENTS_MAXQUADS*6;i++)
600 quadelement3i[i] = quadelement3s[i];
602 Cvar_RegisterVariable(&r_render);
603 Cvar_RegisterVariable(&r_renderview);
604 Cvar_RegisterVariable(&r_waterwarp);
605 Cvar_RegisterVariable(&gl_polyblend);
606 Cvar_RegisterVariable(&v_flipped);
607 Cvar_RegisterVariable(&gl_dither);
608 Cvar_RegisterVariable(&gl_vbo);
609 Cvar_RegisterVariable(&gl_vbo_dynamicvertex);
610 Cvar_RegisterVariable(&gl_vbo_dynamicindex);
611 Cvar_RegisterVariable(&gl_paranoid);
612 Cvar_RegisterVariable(&gl_printcheckerror);
614 Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
615 Cvar_RegisterVariable(&gl_mesh_testmanualfeeding);
616 Cvar_RegisterVariable(&gl_mesh_prefer_short_elements);
618 Cmd_AddCommand("gl_vbostats", GL_VBOStats_f, "prints a list of all buffer objects (vertex data and triangle elements) and total video memory used by them");
620 R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap, gl_backend_devicelost, gl_backend_devicerestored);
623 void GL_SetMirrorState(qboolean state);
625 void R_Viewport_TransformToScreen(const r_viewport_t *v, const vec4_t in, vec4_t out)
629 Matrix4x4_Transform4 (&v->viewmatrix, in, temp);
630 Matrix4x4_Transform4 (&v->projectmatrix, temp, out);
632 out[0] = v->x + (out[0] * iw + 1.0f) * v->width * 0.5f;
634 // for an odd reason, inverting this is wrong for R_Shadow_ScissorForBBox (we then get badly scissored lights)
635 //out[1] = v->y + v->height - (out[1] * iw + 1.0f) * v->height * 0.5f;
636 out[1] = v->y + (out[1] * iw + 1.0f) * v->height * 0.5f;
638 out[2] = v->z + (out[2] * iw + 1.0f) * v->depth * 0.5f;
643 switch(vid.renderpath)
645 case RENDERPATH_GL11:
646 case RENDERPATH_GL13:
647 case RENDERPATH_GL20:
648 case RENDERPATH_GLES1:
649 case RENDERPATH_GLES2:
652 case RENDERPATH_D3D9:
653 //Con_DPrintf("FIXME D3D9 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
655 case RENDERPATH_D3D10:
656 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
658 case RENDERPATH_D3D11:
659 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
661 case RENDERPATH_SOFT:
667 static int bboxedges[12][2] =
686 qboolean R_ScissorForBBox(const float *mins, const float *maxs, int *scissor)
688 int i, ix1, iy1, ix2, iy2;
689 float x1, y1, x2, y2;
700 scissor[0] = r_refdef.view.viewport.x;
701 scissor[1] = r_refdef.view.viewport.y;
702 scissor[2] = r_refdef.view.viewport.width;
703 scissor[3] = r_refdef.view.viewport.height;
705 // if view is inside the box, just say yes it's visible
706 if (BoxesOverlap(r_refdef.view.origin, r_refdef.view.origin, mins, maxs))
709 x1 = y1 = x2 = y2 = 0;
711 // transform all corners that are infront of the nearclip plane
712 VectorNegate(r_refdef.view.frustum[4].normal, plane4f);
713 plane4f[3] = r_refdef.view.frustum[4].dist;
715 for (i = 0;i < 8;i++)
717 Vector4Set(corner[i], (i & 1) ? maxs[0] : mins[0], (i & 2) ? maxs[1] : mins[1], (i & 4) ? maxs[2] : mins[2], 1);
718 dist[i] = DotProduct4(corner[i], plane4f);
719 sign[i] = dist[i] > 0;
722 VectorCopy(corner[i], vertex[numvertices]);
726 // if some points are behind the nearclip, add clipped edge points to make
727 // sure that the scissor boundary is complete
728 if (numvertices > 0 && numvertices < 8)
730 // add clipped edge points
731 for (i = 0;i < 12;i++)
735 if (sign[j] != sign[k])
737 f = dist[j] / (dist[j] - dist[k]);
738 VectorLerp(corner[j], f, corner[k], vertex[numvertices]);
744 // if we have no points to check, it is behind the view plane
748 // if we have some points to transform, check what screen area is covered
749 x1 = y1 = x2 = y2 = 0;
751 //Con_Printf("%i vertices to transform...\n", numvertices);
752 for (i = 0;i < numvertices;i++)
754 VectorCopy(vertex[i], v);
755 R_Viewport_TransformToScreen(&r_refdef.view.viewport, v, v2);
756 //Con_Printf("%.3f %.3f %.3f %.3f transformed to %.3f %.3f %.3f %.3f\n", v[0], v[1], v[2], v[3], v2[0], v2[1], v2[2], v2[3]);
759 if (x1 > v2[0]) x1 = v2[0];
760 if (x2 < v2[0]) x2 = v2[0];
761 if (y1 > v2[1]) y1 = v2[1];
762 if (y2 < v2[1]) y2 = v2[1];
771 // now convert the scissor rectangle to integer screen coordinates
772 ix1 = (int)(x1 - 1.0f);
773 //iy1 = vid.height - (int)(y2 - 1.0f);
774 //iy1 = r_refdef.view.viewport.width + 2 * r_refdef.view.viewport.x - (int)(y2 - 1.0f);
775 iy1 = (int)(y1 - 1.0f);
776 ix2 = (int)(x2 + 1.0f);
777 //iy2 = vid.height - (int)(y1 + 1.0f);
778 //iy2 = r_refdef.view.viewport.height + 2 * r_refdef.view.viewport.y - (int)(y1 + 1.0f);
779 iy2 = (int)(y2 + 1.0f);
780 //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
782 // clamp it to the screen
783 if (ix1 < r_refdef.view.viewport.x) ix1 = r_refdef.view.viewport.x;
784 if (iy1 < r_refdef.view.viewport.y) iy1 = r_refdef.view.viewport.y;
785 if (ix2 > r_refdef.view.viewport.x + r_refdef.view.viewport.width) ix2 = r_refdef.view.viewport.x + r_refdef.view.viewport.width;
786 if (iy2 > r_refdef.view.viewport.y + r_refdef.view.viewport.height) iy2 = r_refdef.view.viewport.y + r_refdef.view.viewport.height;
788 // if it is inside out, it's not visible
789 if (ix2 <= ix1 || iy2 <= iy1)
792 // the light area is visible, set up the scissor rectangle
795 scissor[2] = ix2 - ix1;
796 scissor[3] = iy2 - iy1;
798 // D3D Y coordinate is top to bottom, OpenGL is bottom to top, fix the D3D one
799 switch(vid.renderpath)
801 case RENDERPATH_D3D9:
802 case RENDERPATH_D3D10:
803 case RENDERPATH_D3D11:
804 scissor[1] = vid.height - scissor[1] - scissor[3];
806 case RENDERPATH_GL11:
807 case RENDERPATH_GL13:
808 case RENDERPATH_GL20:
809 case RENDERPATH_SOFT:
810 case RENDERPATH_GLES1:
811 case RENDERPATH_GLES2:
819 static void R_Viewport_ApplyNearClipPlaneFloatGL(const r_viewport_t *v, float *m, float normalx, float normaly, float normalz, float dist)
823 float clipPlane[4], v3[3], v4[3];
826 // This is inspired by Oblique Depth Projection from http://www.terathon.com/code/oblique.php
828 VectorSet(normal, normalx, normaly, normalz);
829 Matrix4x4_Transform3x3(&v->viewmatrix, normal, clipPlane);
830 VectorScale(normal, -dist, v3);
831 Matrix4x4_Transform(&v->viewmatrix, v3, v4);
832 // FIXME: LordHavoc: I think this can be done more efficiently somehow but I can't remember the technique
833 clipPlane[3] = -DotProduct(v4, clipPlane);
837 // testing code for comparing results
839 VectorCopy4(clipPlane, clipPlane2);
840 R_EntityMatrix(&identitymatrix);
841 VectorSet(q, normal[0], normal[1], normal[2], -dist);
842 qglClipPlane(GL_CLIP_PLANE0, q);
843 qglGetClipPlane(GL_CLIP_PLANE0, q);
844 VectorCopy4(q, clipPlane);
848 // Calculate the clip-space corner point opposite the clipping plane
849 // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
850 // transform it into camera space by multiplying it
851 // by the inverse of the projection matrix
852 q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + m[8]) / m[0];
853 q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + m[9]) / m[5];
855 q[3] = (1.0f + m[10]) / m[14];
857 // Calculate the scaled plane vector
858 d = 2.0f / DotProduct4(clipPlane, q);
860 // Replace the third row of the projection matrix
861 m[2] = clipPlane[0] * d;
862 m[6] = clipPlane[1] * d;
863 m[10] = clipPlane[2] * d + 1.0f;
864 m[14] = clipPlane[3] * d;
867 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)
869 float left = x1, right = x2, bottom = y2, top = y1, zNear = nearclip, zFar = farclip;
871 memset(v, 0, sizeof(*v));
872 v->type = R_VIEWPORTTYPE_ORTHO;
873 v->cameramatrix = *cameramatrix;
880 memset(m, 0, sizeof(m));
881 m[0] = 2/(right - left);
882 m[5] = 2/(top - bottom);
883 m[10] = -2/(zFar - zNear);
884 m[12] = - (right + left)/(right - left);
885 m[13] = - (top + bottom)/(top - bottom);
886 m[14] = - (zFar + zNear)/(zFar - zNear);
888 switch(vid.renderpath)
890 case RENDERPATH_GL11:
891 case RENDERPATH_GL13:
892 case RENDERPATH_GL20:
893 case RENDERPATH_SOFT:
894 case RENDERPATH_GLES1:
895 case RENDERPATH_GLES2:
897 case RENDERPATH_D3D9:
898 case RENDERPATH_D3D10:
899 case RENDERPATH_D3D11:
900 m[10] = -1/(zFar - zNear);
901 m[14] = -zNear/(zFar-zNear);
904 v->screentodepth[0] = -farclip / (farclip - nearclip);
905 v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
907 Matrix4x4_Invert_Full(&v->viewmatrix, &v->cameramatrix);
910 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
912 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
918 Vector4Set(test1, (x1+x2)*0.5f, (y1+y2)*0.5f, 0.0f, 1.0f);
919 R_Viewport_TransformToScreen(v, test1, test2);
920 Con_Printf("%f %f %f -> %f %f %f\n", test1[0], test1[1], test1[2], test2[0], test2[1], test2[2]);
925 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)
927 matrix4x4_t tempmatrix, basematrix;
929 memset(v, 0, sizeof(*v));
931 v->type = R_VIEWPORTTYPE_PERSPECTIVE;
932 v->cameramatrix = *cameramatrix;
939 memset(m, 0, sizeof(m));
940 m[0] = 1.0 / frustumx;
941 m[5] = 1.0 / frustumy;
942 m[10] = -(farclip + nearclip) / (farclip - nearclip);
944 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
945 v->screentodepth[0] = -farclip / (farclip - nearclip);
946 v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
948 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
949 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
950 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
951 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
954 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
956 if(v_flipped.integer)
964 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
967 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)
969 matrix4x4_t tempmatrix, basematrix;
970 const float nudge = 1.0 - 1.0 / (1<<23);
972 memset(v, 0, sizeof(*v));
974 v->type = R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP;
975 v->cameramatrix = *cameramatrix;
982 memset(m, 0, sizeof(m));
983 m[ 0] = 1.0 / frustumx;
984 m[ 5] = 1.0 / frustumy;
987 m[14] = -2 * nearclip * nudge;
988 v->screentodepth[0] = (m[10] + 1) * 0.5 - 1;
989 v->screentodepth[1] = m[14] * -0.5;
991 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
992 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
993 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
994 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
997 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
999 if(v_flipped.integer)
1007 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
1010 float cubeviewmatrix[6][16] =
1012 // standard cubemap projections
1050 float rectviewmatrix[6][16] =
1052 // sign-preserving cubemap projections
1091 void R_Viewport_InitCubeSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, float nearclip, float farclip, const float *nearplane)
1093 matrix4x4_t tempmatrix, basematrix;
1095 memset(v, 0, sizeof(*v));
1096 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
1097 v->cameramatrix = *cameramatrix;
1101 memset(m, 0, sizeof(m));
1103 m[10] = -(farclip + nearclip) / (farclip - nearclip);
1105 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
1107 Matrix4x4_FromArrayFloatGL(&basematrix, cubeviewmatrix[side]);
1108 Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
1109 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
1112 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
1114 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
1117 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)
1119 matrix4x4_t tempmatrix, basematrix;
1121 memset(v, 0, sizeof(*v));
1122 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
1123 v->cameramatrix = *cameramatrix;
1124 v->x = (side & 1) * size;
1125 v->y = (side >> 1) * size;
1129 memset(m, 0, sizeof(m));
1130 m[0] = m[5] = 1.0f * ((float)size - border) / size;
1131 m[10] = -(farclip + nearclip) / (farclip - nearclip);
1133 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
1135 Matrix4x4_FromArrayFloatGL(&basematrix, rectviewmatrix[side]);
1136 Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
1137 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
1139 switch(vid.renderpath)
1141 case RENDERPATH_GL20:
1142 case RENDERPATH_GL13:
1143 case RENDERPATH_GL11:
1144 case RENDERPATH_SOFT:
1145 case RENDERPATH_GLES1:
1146 case RENDERPATH_GLES2:
1148 case RENDERPATH_D3D9:
1151 case RENDERPATH_D3D10:
1152 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1154 case RENDERPATH_D3D11:
1155 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1160 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
1162 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
1165 void R_SetViewport(const r_viewport_t *v)
1170 // FIXME: v_flipped_state is evil, this probably breaks somewhere
1171 GL_SetMirrorState(v_flipped.integer && (v->type == R_VIEWPORTTYPE_PERSPECTIVE || v->type == R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP));
1173 // copy over the matrices to our state
1174 gl_viewmatrix = v->viewmatrix;
1175 gl_projectionmatrix = v->projectmatrix;
1177 switch(vid.renderpath)
1179 case RENDERPATH_GL13:
1180 case RENDERPATH_GL11:
1181 case RENDERPATH_GLES1:
1182 #ifdef GL_PROJECTION
1184 qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
1185 // Load the projection matrix into OpenGL
1186 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
1187 Matrix4x4_ToArrayFloatGL(&gl_projectionmatrix, m);
1188 qglLoadMatrixf(m);CHECKGLERROR
1189 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1192 case RENDERPATH_D3D9:
1195 D3DVIEWPORT9 d3dviewport;
1196 d3dviewport.X = gl_viewport.x;
1197 d3dviewport.Y = gl_viewport.y;
1198 d3dviewport.Width = gl_viewport.width;
1199 d3dviewport.Height = gl_viewport.height;
1200 d3dviewport.MinZ = gl_state.depthrange[0];
1201 d3dviewport.MaxZ = gl_state.depthrange[1];
1202 IDirect3DDevice9_SetViewport(vid_d3d9dev, &d3dviewport);
1206 case RENDERPATH_D3D10:
1207 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1209 case RENDERPATH_D3D11:
1210 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1212 case RENDERPATH_SOFT:
1213 DPSOFTRAST_Viewport(v->x, v->y, v->width, v->height);
1215 case RENDERPATH_GL20:
1216 case RENDERPATH_GLES2:
1218 qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
1222 // force an update of the derived matrices
1223 gl_modelmatrixchanged = true;
1224 R_EntityMatrix(&gl_modelmatrix);
1227 void R_GetViewport(r_viewport_t *v)
1232 static void GL_BindVBO(int bufferobject)
1234 if (gl_state.vertexbufferobject != bufferobject)
1236 gl_state.vertexbufferobject = bufferobject;
1238 qglBindBufferARB(GL_ARRAY_BUFFER, bufferobject);CHECKGLERROR
1242 static void GL_BindEBO(int bufferobject)
1244 if (gl_state.elementbufferobject != bufferobject)
1246 gl_state.elementbufferobject = bufferobject;
1248 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, bufferobject);CHECKGLERROR
1252 static const GLuint drawbuffers[4] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3};
1253 int R_Mesh_CreateFramebufferObject(rtexture_t *depthtexture, rtexture_t *colortexture, rtexture_t *colortexture2, rtexture_t *colortexture3, rtexture_t *colortexture4)
1255 switch(vid.renderpath)
1257 case RENDERPATH_GL11:
1258 case RENDERPATH_GL13:
1259 case RENDERPATH_GL20:
1260 case RENDERPATH_GLES1:
1261 case RENDERPATH_GLES2:
1262 if (vid.support.arb_framebuffer_object)
1266 qglGenFramebuffers(1, (GLuint*)&temp);CHECKGLERROR
1267 R_Mesh_SetRenderTargets(temp, NULL, NULL, NULL, NULL, NULL);
1268 // GL_ARB_framebuffer_object (GL3-class hardware) - depth stencil attachment
1269 if (depthtexture && depthtexture->texnum ) qglFramebufferTexture2D(GL_FRAMEBUFFER, depthtexture->glisdepthstencil ? GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT , depthtexture->gltexturetypeenum , depthtexture->texnum , 0);CHECKGLERROR
1270 if (depthtexture && depthtexture->renderbuffernum ) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, depthtexture->glisdepthstencil ? GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT , GL_RENDERBUFFER, depthtexture->renderbuffernum );CHECKGLERROR
1271 if (colortexture && colortexture->texnum ) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , colortexture->gltexturetypeenum , colortexture->texnum , 0);CHECKGLERROR
1272 if (colortexture2 && colortexture2->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 , colortexture2->gltexturetypeenum, colortexture2->texnum, 0);CHECKGLERROR
1273 if (colortexture3 && colortexture3->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2 , colortexture3->gltexturetypeenum, colortexture3->texnum, 0);CHECKGLERROR
1274 if (colortexture4 && colortexture4->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3 , colortexture4->gltexturetypeenum, colortexture4->texnum, 0);CHECKGLERROR
1275 if (colortexture && colortexture->renderbuffernum ) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , GL_RENDERBUFFER, colortexture->renderbuffernum );CHECKGLERROR
1276 if (colortexture2 && colortexture2->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 , GL_RENDERBUFFER, colortexture2->renderbuffernum);CHECKGLERROR
1277 if (colortexture3 && colortexture3->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2 , GL_RENDERBUFFER, colortexture3->renderbuffernum);CHECKGLERROR
1278 if (colortexture4 && colortexture4->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3 , GL_RENDERBUFFER, colortexture4->renderbuffernum);CHECKGLERROR
1280 if (colortexture4 && qglDrawBuffersARB)
1282 qglDrawBuffersARB(4, drawbuffers);CHECKGLERROR
1283 qglReadBuffer(GL_NONE);CHECKGLERROR
1285 else if (colortexture3 && qglDrawBuffersARB)
1287 qglDrawBuffersARB(3, drawbuffers);CHECKGLERROR
1288 qglReadBuffer(GL_NONE);CHECKGLERROR
1290 else if (colortexture2 && qglDrawBuffersARB)
1292 qglDrawBuffersARB(2, drawbuffers);CHECKGLERROR
1293 qglReadBuffer(GL_NONE);CHECKGLERROR
1295 else if (colortexture && qglDrawBuffer)
1297 qglDrawBuffer(GL_COLOR_ATTACHMENT0);CHECKGLERROR
1298 qglReadBuffer(GL_COLOR_ATTACHMENT0);CHECKGLERROR
1300 else if (qglDrawBuffer)
1302 qglDrawBuffer(GL_NONE);CHECKGLERROR
1303 qglReadBuffer(GL_NONE);CHECKGLERROR
1305 status = qglCheckFramebufferStatus(GL_FRAMEBUFFER);CHECKGLERROR
1306 if (status != GL_FRAMEBUFFER_COMPLETE)
1308 Con_Printf("R_Mesh_CreateFramebufferObject: glCheckFramebufferStatus returned %i\n", status);
1309 qglDeleteFramebuffers(1, (GLuint*)&temp);
1314 else if (vid.support.ext_framebuffer_object)
1318 qglGenFramebuffers(1, (GLuint*)&temp);CHECKGLERROR
1319 R_Mesh_SetRenderTargets(temp, NULL, NULL, NULL, NULL, NULL);
1320 // GL_EXT_framebuffer_object (GL2-class hardware) - no depth stencil attachment, let it break stencil
1321 if (depthtexture && depthtexture->texnum ) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT , depthtexture->gltexturetypeenum , depthtexture->texnum , 0);CHECKGLERROR
1322 if (depthtexture && depthtexture->renderbuffernum ) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT , GL_RENDERBUFFER, depthtexture->renderbuffernum );CHECKGLERROR
1323 if (colortexture && colortexture->texnum ) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , colortexture->gltexturetypeenum , colortexture->texnum , 0);CHECKGLERROR
1324 if (colortexture2 && colortexture2->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 , colortexture2->gltexturetypeenum, colortexture2->texnum, 0);CHECKGLERROR
1325 if (colortexture3 && colortexture3->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2 , colortexture3->gltexturetypeenum, colortexture3->texnum, 0);CHECKGLERROR
1326 if (colortexture4 && colortexture4->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3 , colortexture4->gltexturetypeenum, colortexture4->texnum, 0);CHECKGLERROR
1327 if (colortexture && colortexture->renderbuffernum ) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , GL_RENDERBUFFER, colortexture->renderbuffernum );CHECKGLERROR
1328 if (colortexture2 && colortexture2->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 , GL_RENDERBUFFER, colortexture2->renderbuffernum);CHECKGLERROR
1329 if (colortexture3 && colortexture3->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2 , GL_RENDERBUFFER, colortexture3->renderbuffernum);CHECKGLERROR
1330 if (colortexture4 && colortexture4->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3 , GL_RENDERBUFFER, colortexture4->renderbuffernum);CHECKGLERROR
1332 if (colortexture4 && qglDrawBuffersARB)
1334 qglDrawBuffersARB(4, drawbuffers);CHECKGLERROR
1335 qglReadBuffer(GL_NONE);CHECKGLERROR
1337 else if (colortexture3 && qglDrawBuffersARB)
1339 qglDrawBuffersARB(3, drawbuffers);CHECKGLERROR
1340 qglReadBuffer(GL_NONE);CHECKGLERROR
1342 else if (colortexture2 && qglDrawBuffersARB)
1344 qglDrawBuffersARB(2, drawbuffers);CHECKGLERROR
1345 qglReadBuffer(GL_NONE);CHECKGLERROR
1347 else if (colortexture && qglDrawBuffer)
1349 qglDrawBuffer(GL_COLOR_ATTACHMENT0);CHECKGLERROR
1350 qglReadBuffer(GL_COLOR_ATTACHMENT0);CHECKGLERROR
1352 else if (qglDrawBuffer)
1354 qglDrawBuffer(GL_NONE);CHECKGLERROR
1355 qglReadBuffer(GL_NONE);CHECKGLERROR
1357 status = qglCheckFramebufferStatus(GL_FRAMEBUFFER);CHECKGLERROR
1358 if (status != GL_FRAMEBUFFER_COMPLETE)
1360 Con_Printf("R_Mesh_CreateFramebufferObject: glCheckFramebufferStatus returned %i\n", status);
1361 qglDeleteFramebuffers(1, (GLuint*)&temp);
1367 case RENDERPATH_D3D9:
1368 case RENDERPATH_D3D10:
1369 case RENDERPATH_D3D11:
1371 case RENDERPATH_SOFT:
1377 void R_Mesh_DestroyFramebufferObject(int fbo)
1379 switch(vid.renderpath)
1381 case RENDERPATH_GL11:
1382 case RENDERPATH_GL13:
1383 case RENDERPATH_GL20:
1384 case RENDERPATH_GLES1:
1385 case RENDERPATH_GLES2:
1387 qglDeleteFramebuffers(1, (GLuint*)&fbo);
1389 case RENDERPATH_D3D9:
1390 case RENDERPATH_D3D10:
1391 case RENDERPATH_D3D11:
1393 case RENDERPATH_SOFT:
1399 void R_Mesh_SetRenderTargetsD3D9(IDirect3DSurface9 *depthsurface, IDirect3DSurface9 *colorsurface0, IDirect3DSurface9 *colorsurface1, IDirect3DSurface9 *colorsurface2, IDirect3DSurface9 *colorsurface3)
1401 gl_state.framebufferobject = depthsurface != gl_state.d3drt_backbufferdepthsurface || colorsurface0 != gl_state.d3drt_backbuffercolorsurface;
1402 if (gl_state.d3drt_depthsurface != depthsurface)
1404 gl_state.d3drt_depthsurface = depthsurface;
1405 IDirect3DDevice9_SetDepthStencilSurface(vid_d3d9dev, gl_state.d3drt_depthsurface);
1407 if (gl_state.d3drt_colorsurfaces[0] != colorsurface0)
1409 gl_state.d3drt_colorsurfaces[0] = colorsurface0;
1410 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 0, gl_state.d3drt_colorsurfaces[0]);
1412 if (gl_state.d3drt_colorsurfaces[1] != colorsurface1)
1414 gl_state.d3drt_colorsurfaces[1] = colorsurface1;
1415 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 1, gl_state.d3drt_colorsurfaces[1]);
1417 if (gl_state.d3drt_colorsurfaces[2] != colorsurface2)
1419 gl_state.d3drt_colorsurfaces[2] = colorsurface2;
1420 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 2, gl_state.d3drt_colorsurfaces[2]);
1422 if (gl_state.d3drt_colorsurfaces[3] != colorsurface3)
1424 gl_state.d3drt_colorsurfaces[3] = colorsurface3;
1425 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 3, gl_state.d3drt_colorsurfaces[3]);
1430 void R_Mesh_SetRenderTargets(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture, rtexture_t *colortexture2, rtexture_t *colortexture3, rtexture_t *colortexture4)
1434 rtexture_t *textures[5];
1435 Vector4Set(textures, colortexture, colortexture2, colortexture3, colortexture4);
1436 textures[4] = depthtexture;
1437 // unbind any matching textures immediately, otherwise D3D will complain about a bound texture being used as a render target
1438 for (j = 0;j < 5;j++)
1440 for (i = 0;i < vid.teximageunits;i++)
1441 if (gl_state.units[i].texture == textures[j])
1442 R_Mesh_TexBind(i, NULL);
1443 // set up framebuffer object or render targets for the active rendering API
1444 switch(vid.renderpath)
1446 case RENDERPATH_GL11:
1447 case RENDERPATH_GL13:
1448 case RENDERPATH_GL20:
1449 case RENDERPATH_GLES1:
1450 case RENDERPATH_GLES2:
1451 if (gl_state.framebufferobject != fbo)
1453 gl_state.framebufferobject = fbo;
1454 qglBindFramebuffer(GL_FRAMEBUFFER, gl_state.framebufferobject ? gl_state.framebufferobject : gl_state.defaultframebufferobject);
1457 case RENDERPATH_D3D9:
1459 // set up the new render targets, a NULL depthtexture intentionally binds nothing
1460 // TODO: optimize: keep surface pointer around in rtexture_t until texture is freed or lost
1463 IDirect3DSurface9 *surfaces[5];
1464 for (i = 0;i < 5;i++)
1469 if (textures[i]->d3dsurface)
1470 surfaces[i] = (IDirect3DSurface9 *)textures[i]->d3dsurface;
1472 IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9 *)textures[i]->d3dtexture, 0, &surfaces[i]);
1475 // set the render targets for real
1476 R_Mesh_SetRenderTargetsD3D9(surfaces[4], surfaces[0], surfaces[1], surfaces[2], surfaces[3]);
1477 // release the texture surface levels (they won't be lost while bound...)
1478 for (i = 0;i < 5;i++)
1479 if (textures[i] && !textures[i]->d3dsurface)
1480 IDirect3DSurface9_Release(surfaces[i]);
1483 R_Mesh_SetRenderTargetsD3D9(gl_state.d3drt_backbufferdepthsurface, gl_state.d3drt_backbuffercolorsurface, NULL, NULL, NULL);
1486 case RENDERPATH_D3D10:
1487 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1489 case RENDERPATH_D3D11:
1490 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1492 case RENDERPATH_SOFT:
1496 unsigned int *pointers[5];
1497 memset(pointers, 0, sizeof(pointers));
1498 for (i = 0;i < 5;i++)
1499 pointers[i] = textures[i] ? (unsigned int *)DPSOFTRAST_Texture_GetPixelPointer(textures[i]->texnum, 0) : NULL;
1500 width = DPSOFTRAST_Texture_GetWidth(textures[0] ? textures[0]->texnum : textures[4]->texnum, 0);
1501 height = DPSOFTRAST_Texture_GetHeight(textures[0] ? textures[0]->texnum : textures[4]->texnum, 0);
1502 DPSOFTRAST_SetRenderTargets(width, height, pointers[4], pointers[0], pointers[1], pointers[2], pointers[3]);
1505 DPSOFTRAST_SetRenderTargets(vid.width, vid.height, vid.softdepthpixels, vid.softpixels, NULL, NULL, NULL);
1511 static int d3dcmpforglfunc(int f)
1515 case GL_NEVER: return D3DCMP_NEVER;
1516 case GL_LESS: return D3DCMP_LESS;
1517 case GL_EQUAL: return D3DCMP_EQUAL;
1518 case GL_LEQUAL: return D3DCMP_LESSEQUAL;
1519 case GL_GREATER: return D3DCMP_GREATER;
1520 case GL_NOTEQUAL: return D3DCMP_NOTEQUAL;
1521 case GL_GEQUAL: return D3DCMP_GREATEREQUAL;
1522 case GL_ALWAYS: return D3DCMP_ALWAYS;
1523 default: Con_DPrintf("Unknown GL_DepthFunc\n");return D3DCMP_ALWAYS;
1527 static int d3dstencilopforglfunc(int f)
1531 case GL_KEEP: return D3DSTENCILOP_KEEP;
1532 case GL_INCR: return D3DSTENCILOP_INCR; // note: GL_INCR is clamped, D3DSTENCILOP_INCR wraps
1533 case GL_DECR: return D3DSTENCILOP_DECR; // note: GL_DECR is clamped, D3DSTENCILOP_DECR wraps
1534 default: Con_DPrintf("Unknown GL_StencilFunc\n");return D3DSTENCILOP_KEEP;
1539 static void GL_Backend_ResetState(void)
1542 gl_state.active = true;
1543 gl_state.depthtest = true;
1544 gl_state.alphatest = false;
1545 gl_state.alphafunc = GL_GEQUAL;
1546 gl_state.alphafuncvalue = 0.5f;
1547 gl_state.alphatocoverage = false;
1548 gl_state.blendfunc1 = GL_ONE;
1549 gl_state.blendfunc2 = GL_ZERO;
1550 gl_state.blend = false;
1551 gl_state.depthmask = GL_TRUE;
1552 gl_state.colormask = 15;
1553 gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
1554 gl_state.lockrange_first = 0;
1555 gl_state.lockrange_count = 0;
1556 gl_state.cullface = GL_FRONT;
1557 gl_state.cullfaceenable = false;
1558 gl_state.polygonoffset[0] = 0;
1559 gl_state.polygonoffset[1] = 0;
1560 gl_state.framebufferobject = 0;
1561 gl_state.depthfunc = GL_LEQUAL;
1563 switch(vid.renderpath)
1565 case RENDERPATH_D3D9:
1568 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_COLORWRITEENABLE, gl_state.colormask);
1569 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_NONE);
1570 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZFUNC, d3dcmpforglfunc(gl_state.depthfunc));
1571 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZENABLE, gl_state.depthtest);
1572 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZWRITEENABLE, gl_state.depthmask);
1573 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SLOPESCALEDEPTHBIAS, gl_state.polygonoffset[0]);
1574 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DEPTHBIAS, gl_state.polygonoffset[1] * (1.0f / 16777216.0f));
1578 case RENDERPATH_D3D10:
1579 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1581 case RENDERPATH_D3D11:
1582 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1584 case RENDERPATH_GL11:
1585 case RENDERPATH_GL13:
1586 case RENDERPATH_GLES1:
1587 #ifdef GL_ALPHA_TEST
1590 qglColorMask(1, 1, 1, 1);CHECKGLERROR
1591 qglAlphaFunc(gl_state.alphafunc, gl_state.alphafuncvalue);CHECKGLERROR
1592 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
1593 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1594 qglDisable(GL_BLEND);CHECKGLERROR
1595 qglCullFace(gl_state.cullface);CHECKGLERROR
1596 qglDisable(GL_CULL_FACE);CHECKGLERROR
1597 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1598 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1599 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1600 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1602 if (vid.support.arb_vertex_buffer_object)
1604 qglBindBufferARB(GL_ARRAY_BUFFER, 0);
1605 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, 0);
1608 if (vid.support.ext_framebuffer_object)
1610 //qglBindRenderbuffer(GL_RENDERBUFFER, 0);
1611 qglBindFramebuffer(GL_FRAMEBUFFER, 0);
1614 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
1615 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1617 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
1618 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1619 qglColor4f(1, 1, 1, 1);CHECKGLERROR
1621 if (vid.support.ext_framebuffer_object)
1622 qglBindFramebuffer(GL_FRAMEBUFFER, gl_state.framebufferobject);
1624 gl_state.unit = MAX_TEXTUREUNITS;
1625 gl_state.clientunit = MAX_TEXTUREUNITS;
1626 for (i = 0;i < vid.texunits;i++)
1628 GL_ActiveTexture(i);
1629 GL_ClientActiveTexture(i);
1630 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1631 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1632 if (vid.support.ext_texture_3d)
1634 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1635 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1637 if (vid.support.arb_texture_cube_map)
1639 qglDisable(GL_TEXTURE_CUBE_MAP);CHECKGLERROR
1640 qglBindTexture(GL_TEXTURE_CUBE_MAP, 0);CHECKGLERROR
1643 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
1644 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1645 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1646 qglLoadIdentity();CHECKGLERROR
1647 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1648 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
1653 case RENDERPATH_SOFT:
1654 DPSOFTRAST_ColorMask(1,1,1,1);
1655 DPSOFTRAST_BlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);
1656 DPSOFTRAST_CullFace(gl_state.cullface);
1657 DPSOFTRAST_DepthFunc(gl_state.depthfunc);
1658 DPSOFTRAST_DepthMask(gl_state.depthmask);
1659 DPSOFTRAST_PolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1660 DPSOFTRAST_SetRenderTargets(vid.width, vid.height, vid.softdepthpixels, vid.softpixels, NULL, NULL, NULL);
1661 DPSOFTRAST_Viewport(0, 0, vid.width, vid.height);
1663 case RENDERPATH_GL20:
1664 case RENDERPATH_GLES2:
1666 qglColorMask(1, 1, 1, 1);CHECKGLERROR
1667 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1668 qglDisable(GL_BLEND);CHECKGLERROR
1669 qglCullFace(gl_state.cullface);CHECKGLERROR
1670 qglDisable(GL_CULL_FACE);CHECKGLERROR
1671 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1672 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1673 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1674 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1675 if (vid.support.arb_vertex_buffer_object)
1677 qglBindBufferARB(GL_ARRAY_BUFFER, 0);
1678 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, 0);
1680 if (vid.support.ext_framebuffer_object)
1681 qglBindFramebuffer(GL_FRAMEBUFFER, gl_state.defaultframebufferobject);
1682 qglEnableVertexAttribArray(GLSLATTRIB_POSITION);
1683 qglVertexAttribPointer(GLSLATTRIB_POSITION, 3, GL_FLOAT, false, sizeof(float[3]), NULL);CHECKGLERROR
1684 qglDisableVertexAttribArray(GLSLATTRIB_COLOR);
1685 qglVertexAttribPointer(GLSLATTRIB_COLOR, 4, GL_FLOAT, false, sizeof(float[4]), NULL);CHECKGLERROR
1686 qglVertexAttrib4f(GLSLATTRIB_COLOR, 1, 1, 1, 1);
1687 gl_state.unit = MAX_TEXTUREUNITS;
1688 gl_state.clientunit = MAX_TEXTUREUNITS;
1689 for (i = 0;i < vid.teximageunits;i++)
1691 GL_ActiveTexture(i);
1692 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1693 if (vid.support.ext_texture_3d)
1695 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1697 if (vid.support.arb_texture_cube_map)
1699 qglBindTexture(GL_TEXTURE_CUBE_MAP, 0);CHECKGLERROR
1702 for (i = 0;i < vid.texarrayunits;i++)
1705 qglVertexAttribPointer(i+GLSLATTRIB_TEXCOORD0, 2, GL_FLOAT, false, sizeof(float[2]), NULL);CHECKGLERROR
1706 qglDisableVertexAttribArray(i+GLSLATTRIB_TEXCOORD0);CHECKGLERROR
1713 void GL_ActiveTexture(unsigned int num)
1715 if (gl_state.unit != num)
1717 gl_state.unit = num;
1718 switch(vid.renderpath)
1720 case RENDERPATH_GL11:
1721 case RENDERPATH_GL13:
1722 case RENDERPATH_GL20:
1723 case RENDERPATH_GLES1:
1724 case RENDERPATH_GLES2:
1725 if (qglActiveTexture)
1728 qglActiveTexture(GL_TEXTURE0 + gl_state.unit);
1732 case RENDERPATH_D3D9:
1733 case RENDERPATH_D3D10:
1734 case RENDERPATH_D3D11:
1736 case RENDERPATH_SOFT:
1742 void GL_ClientActiveTexture(unsigned int num)
1744 if (gl_state.clientunit != num)
1746 gl_state.clientunit = num;
1747 switch(vid.renderpath)
1749 case RENDERPATH_GL11:
1750 case RENDERPATH_GL13:
1751 case RENDERPATH_GLES1:
1752 if (qglActiveTexture)
1755 qglClientActiveTexture(GL_TEXTURE0 + gl_state.clientunit);
1759 case RENDERPATH_D3D9:
1760 case RENDERPATH_D3D10:
1761 case RENDERPATH_D3D11:
1763 case RENDERPATH_SOFT:
1765 case RENDERPATH_GL20:
1766 case RENDERPATH_GLES2:
1772 void GL_BlendFunc(int blendfunc1, int blendfunc2)
1774 if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
1776 qboolean blendenable;
1777 gl_state.blendfunc1 = blendfunc1;
1778 gl_state.blendfunc2 = blendfunc2;
1779 blendenable = (gl_state.blendfunc1 != GL_ONE || gl_state.blendfunc2 != GL_ZERO);
1780 switch(vid.renderpath)
1782 case RENDERPATH_GL11:
1783 case RENDERPATH_GL13:
1784 case RENDERPATH_GL20:
1785 case RENDERPATH_GLES1:
1786 case RENDERPATH_GLES2:
1788 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1789 if (gl_state.blend != blendenable)
1791 gl_state.blend = blendenable;
1792 if (!gl_state.blend)
1794 qglDisable(GL_BLEND);CHECKGLERROR
1798 qglEnable(GL_BLEND);CHECKGLERROR
1802 case RENDERPATH_D3D9:
1807 D3DBLEND d3dblendfunc[2];
1808 glblendfunc[0] = gl_state.blendfunc1;
1809 glblendfunc[1] = gl_state.blendfunc2;
1810 for (i = 0;i < 2;i++)
1812 switch(glblendfunc[i])
1814 case GL_ZERO: d3dblendfunc[i] = D3DBLEND_ZERO;break;
1815 case GL_ONE: d3dblendfunc[i] = D3DBLEND_ONE;break;
1816 case GL_SRC_COLOR: d3dblendfunc[i] = D3DBLEND_SRCCOLOR;break;
1817 case GL_ONE_MINUS_SRC_COLOR: d3dblendfunc[i] = D3DBLEND_INVSRCCOLOR;break;
1818 case GL_SRC_ALPHA: d3dblendfunc[i] = D3DBLEND_SRCALPHA;break;
1819 case GL_ONE_MINUS_SRC_ALPHA: d3dblendfunc[i] = D3DBLEND_INVSRCALPHA;break;
1820 case GL_DST_ALPHA: d3dblendfunc[i] = D3DBLEND_DESTALPHA;break;
1821 case GL_ONE_MINUS_DST_ALPHA: d3dblendfunc[i] = D3DBLEND_INVDESTALPHA;break;
1822 case GL_DST_COLOR: d3dblendfunc[i] = D3DBLEND_DESTCOLOR;break;
1823 case GL_ONE_MINUS_DST_COLOR: d3dblendfunc[i] = D3DBLEND_INVDESTCOLOR;break;
1826 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SRCBLEND, d3dblendfunc[0]);
1827 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DESTBLEND, d3dblendfunc[1]);
1828 if (gl_state.blend != blendenable)
1830 gl_state.blend = blendenable;
1831 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHABLENDENABLE, gl_state.blend);
1836 case RENDERPATH_D3D10:
1837 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1839 case RENDERPATH_D3D11:
1840 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1842 case RENDERPATH_SOFT:
1843 DPSOFTRAST_BlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);
1849 void GL_DepthMask(int state)
1851 if (gl_state.depthmask != state)
1853 gl_state.depthmask = state;
1854 switch(vid.renderpath)
1856 case RENDERPATH_GL11:
1857 case RENDERPATH_GL13:
1858 case RENDERPATH_GL20:
1859 case RENDERPATH_GLES1:
1860 case RENDERPATH_GLES2:
1862 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1864 case RENDERPATH_D3D9:
1866 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZWRITEENABLE, gl_state.depthmask);
1869 case RENDERPATH_D3D10:
1870 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1872 case RENDERPATH_D3D11:
1873 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1875 case RENDERPATH_SOFT:
1876 DPSOFTRAST_DepthMask(gl_state.depthmask);
1882 void GL_DepthTest(int state)
1884 if (gl_state.depthtest != state)
1886 gl_state.depthtest = state;
1887 switch(vid.renderpath)
1889 case RENDERPATH_GL11:
1890 case RENDERPATH_GL13:
1891 case RENDERPATH_GL20:
1892 case RENDERPATH_GLES1:
1893 case RENDERPATH_GLES2:
1895 if (gl_state.depthtest)
1897 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1901 qglDisable(GL_DEPTH_TEST);CHECKGLERROR
1904 case RENDERPATH_D3D9:
1906 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZENABLE, gl_state.depthtest);
1909 case RENDERPATH_D3D10:
1910 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1912 case RENDERPATH_D3D11:
1913 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1915 case RENDERPATH_SOFT:
1916 DPSOFTRAST_DepthTest(gl_state.depthtest);
1922 void GL_DepthFunc(int state)
1924 if (gl_state.depthfunc != state)
1926 gl_state.depthfunc = state;
1927 switch(vid.renderpath)
1929 case RENDERPATH_GL11:
1930 case RENDERPATH_GL13:
1931 case RENDERPATH_GL20:
1932 case RENDERPATH_GLES1:
1933 case RENDERPATH_GLES2:
1935 qglDepthFunc(gl_state.depthfunc);CHECKGLERROR
1937 case RENDERPATH_D3D9:
1939 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZFUNC, d3dcmpforglfunc(gl_state.depthfunc));
1942 case RENDERPATH_D3D10:
1943 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1945 case RENDERPATH_D3D11:
1946 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1948 case RENDERPATH_SOFT:
1949 DPSOFTRAST_DepthFunc(gl_state.depthfunc);
1955 void GL_DepthRange(float nearfrac, float farfrac)
1957 if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
1959 gl_state.depthrange[0] = nearfrac;
1960 gl_state.depthrange[1] = farfrac;
1961 switch(vid.renderpath)
1963 case RENDERPATH_GL11:
1964 case RENDERPATH_GL13:
1965 case RENDERPATH_GL20:
1966 case RENDERPATH_GLES1:
1967 case RENDERPATH_GLES2:
1969 qglDepthRangef(gl_state.depthrange[0], gl_state.depthrange[1]);
1971 qglDepthRange(gl_state.depthrange[0], gl_state.depthrange[1]);
1974 case RENDERPATH_D3D9:
1977 D3DVIEWPORT9 d3dviewport;
1978 d3dviewport.X = gl_viewport.x;
1979 d3dviewport.Y = gl_viewport.y;
1980 d3dviewport.Width = gl_viewport.width;
1981 d3dviewport.Height = gl_viewport.height;
1982 d3dviewport.MinZ = gl_state.depthrange[0];
1983 d3dviewport.MaxZ = gl_state.depthrange[1];
1984 IDirect3DDevice9_SetViewport(vid_d3d9dev, &d3dviewport);
1988 case RENDERPATH_D3D10:
1989 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1991 case RENDERPATH_D3D11:
1992 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1994 case RENDERPATH_SOFT:
1995 DPSOFTRAST_DepthRange(gl_state.depthrange[0], gl_state.depthrange[1]);
2001 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)
2003 switch (vid.renderpath)
2005 case RENDERPATH_GL11:
2006 case RENDERPATH_GL13:
2007 case RENDERPATH_GL20:
2008 case RENDERPATH_GLES1:
2009 case RENDERPATH_GLES2:
2013 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
2017 qglDisable(GL_STENCIL_TEST);CHECKGLERROR
2019 if (vid.support.ati_separate_stencil)
2021 qglStencilMask(writemask);CHECKGLERROR
2022 qglStencilOpSeparate(GL_FRONT, frontfail, frontzfail, frontzpass);CHECKGLERROR
2023 qglStencilOpSeparate(GL_BACK, backfail, backzfail, backzpass);CHECKGLERROR
2024 qglStencilFuncSeparate(GL_FRONT, frontcompare, comparereference, comparereference);CHECKGLERROR
2025 qglStencilFuncSeparate(GL_BACK, backcompare, comparereference, comparereference);CHECKGLERROR
2027 else if (vid.support.ext_stencil_two_side)
2029 #ifdef GL_STENCIL_TEST_TWO_SIDE_EXT
2030 qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
2031 qglActiveStencilFaceEXT(GL_FRONT);CHECKGLERROR
2032 qglStencilMask(writemask);CHECKGLERROR
2033 qglStencilOp(frontfail, frontzfail, frontzpass);CHECKGLERROR
2034 qglStencilFunc(frontcompare, comparereference, comparemask);CHECKGLERROR
2035 qglActiveStencilFaceEXT(GL_BACK);CHECKGLERROR
2036 qglStencilMask(writemask);CHECKGLERROR
2037 qglStencilOp(backfail, backzfail, backzpass);CHECKGLERROR
2038 qglStencilFunc(backcompare, comparereference, comparemask);CHECKGLERROR
2042 case RENDERPATH_D3D9:
2044 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_TWOSIDEDSTENCILMODE, true);
2045 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILENABLE, enable);
2046 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILWRITEMASK, writemask);
2047 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFAIL, d3dstencilopforglfunc(frontfail));
2048 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILZFAIL, d3dstencilopforglfunc(frontzfail));
2049 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILPASS, d3dstencilopforglfunc(frontzpass));
2050 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFUNC, d3dcmpforglfunc(frontcompare));
2051 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILFAIL, d3dstencilopforglfunc(backfail));
2052 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILZFAIL, d3dstencilopforglfunc(backzfail));
2053 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILPASS, d3dstencilopforglfunc(backzpass));
2054 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILFUNC, d3dcmpforglfunc(backcompare));
2055 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILREF, comparereference);
2056 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILMASK, comparemask);
2059 case RENDERPATH_D3D10:
2060 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2062 case RENDERPATH_D3D11:
2063 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2065 case RENDERPATH_SOFT:
2066 //Con_DPrintf("FIXME SOFT %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2071 void R_SetStencil(qboolean enable, int writemask, int fail, int zfail, int zpass, int compare, int comparereference, int comparemask)
2073 switch (vid.renderpath)
2075 case RENDERPATH_GL11:
2076 case RENDERPATH_GL13:
2077 case RENDERPATH_GL20:
2078 case RENDERPATH_GLES1:
2079 case RENDERPATH_GLES2:
2083 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
2087 qglDisable(GL_STENCIL_TEST);CHECKGLERROR
2089 if (vid.support.ext_stencil_two_side)
2091 #ifdef GL_STENCIL_TEST_TWO_SIDE_EXT
2092 qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
2095 qglStencilMask(writemask);CHECKGLERROR
2096 qglStencilOp(fail, zfail, zpass);CHECKGLERROR
2097 qglStencilFunc(compare, comparereference, comparemask);CHECKGLERROR
2100 case RENDERPATH_D3D9:
2102 if (vid.support.ati_separate_stencil)
2103 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_TWOSIDEDSTENCILMODE, true);
2104 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILENABLE, enable);
2105 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILWRITEMASK, writemask);
2106 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFAIL, d3dstencilopforglfunc(fail));
2107 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILZFAIL, d3dstencilopforglfunc(zfail));
2108 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILPASS, d3dstencilopforglfunc(zpass));
2109 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFUNC, d3dcmpforglfunc(compare));
2110 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILREF, comparereference);
2111 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILMASK, comparemask);
2114 case RENDERPATH_D3D10:
2115 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2117 case RENDERPATH_D3D11:
2118 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2120 case RENDERPATH_SOFT:
2121 //Con_DPrintf("FIXME SOFT %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2126 void GL_PolygonOffset(float planeoffset, float depthoffset)
2128 if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
2130 gl_state.polygonoffset[0] = planeoffset;
2131 gl_state.polygonoffset[1] = depthoffset;
2132 switch(vid.renderpath)
2134 case RENDERPATH_GL11:
2135 case RENDERPATH_GL13:
2136 case RENDERPATH_GL20:
2137 case RENDERPATH_GLES1:
2138 case RENDERPATH_GLES2:
2139 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
2141 case RENDERPATH_D3D9:
2143 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SLOPESCALEDEPTHBIAS, gl_state.polygonoffset[0]);
2144 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DEPTHBIAS, gl_state.polygonoffset[1] * (1.0f / 16777216.0f));
2147 case RENDERPATH_D3D10:
2148 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2150 case RENDERPATH_D3D11:
2151 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2153 case RENDERPATH_SOFT:
2154 DPSOFTRAST_PolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
2160 void GL_SetMirrorState(qboolean state)
2162 if (v_flipped_state != state)
2164 v_flipped_state = state;
2165 if (gl_state.cullface == GL_BACK)
2166 gl_state.cullface = GL_FRONT;
2167 else if (gl_state.cullface == GL_FRONT)
2168 gl_state.cullface = GL_BACK;
2171 switch(vid.renderpath)
2173 case RENDERPATH_GL11:
2174 case RENDERPATH_GL13:
2175 case RENDERPATH_GL20:
2176 case RENDERPATH_GLES1:
2177 case RENDERPATH_GLES2:
2178 qglCullFace(gl_state.cullface);CHECKGLERROR
2180 case RENDERPATH_D3D9:
2182 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, gl_state.cullface == GL_FRONT ? D3DCULL_CCW : D3DCULL_CW);
2185 case RENDERPATH_D3D10:
2186 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2188 case RENDERPATH_D3D11:
2189 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2191 case RENDERPATH_SOFT:
2192 DPSOFTRAST_CullFace(gl_state.cullface);
2198 void GL_CullFace(int state)
2202 if(state == GL_FRONT)
2204 else if(state == GL_BACK)
2208 switch(vid.renderpath)
2210 case RENDERPATH_GL11:
2211 case RENDERPATH_GL13:
2212 case RENDERPATH_GL20:
2213 case RENDERPATH_GLES1:
2214 case RENDERPATH_GLES2:
2217 if (state != GL_NONE)
2219 if (!gl_state.cullfaceenable)
2221 gl_state.cullfaceenable = true;
2222 qglEnable(GL_CULL_FACE);CHECKGLERROR
2224 if (gl_state.cullface != state)
2226 gl_state.cullface = state;
2227 qglCullFace(gl_state.cullface);CHECKGLERROR
2232 if (gl_state.cullfaceenable)
2234 gl_state.cullfaceenable = false;
2235 qglDisable(GL_CULL_FACE);CHECKGLERROR
2239 case RENDERPATH_D3D9:
2241 if (gl_state.cullface != state)
2243 gl_state.cullface = state;
2244 switch(gl_state.cullface)
2247 gl_state.cullfaceenable = false;
2248 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_NONE);
2251 gl_state.cullfaceenable = true;
2252 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_CCW);
2255 gl_state.cullfaceenable = true;
2256 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_CW);
2262 case RENDERPATH_D3D10:
2263 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2265 case RENDERPATH_D3D11:
2266 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2268 case RENDERPATH_SOFT:
2269 if (gl_state.cullface != state)
2271 gl_state.cullface = state;
2272 gl_state.cullfaceenable = state != GL_NONE ? true : false;
2273 DPSOFTRAST_CullFace(gl_state.cullface);
2279 void GL_AlphaTest(int state)
2281 if (gl_state.alphatest != state)
2283 gl_state.alphatest = state;
2284 switch(vid.renderpath)
2286 case RENDERPATH_GL11:
2287 case RENDERPATH_GL13:
2288 case RENDERPATH_GLES1:
2289 #ifdef GL_ALPHA_TEST
2290 // only fixed function uses alpha test, other paths use pixel kill capability in shaders
2292 if (gl_state.alphatest)
2294 qglEnable(GL_ALPHA_TEST);CHECKGLERROR
2298 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
2302 case RENDERPATH_D3D9:
2303 case RENDERPATH_D3D10:
2304 case RENDERPATH_D3D11:
2305 case RENDERPATH_SOFT:
2306 case RENDERPATH_GL20:
2307 case RENDERPATH_GLES2:
2313 void GL_AlphaToCoverage(qboolean state)
2315 if (gl_state.alphatocoverage != state)
2317 gl_state.alphatocoverage = state;
2318 switch(vid.renderpath)
2320 case RENDERPATH_GL11:
2321 case RENDERPATH_GL13:
2322 case RENDERPATH_GLES1:
2323 case RENDERPATH_GLES2:
2324 case RENDERPATH_D3D9:
2325 case RENDERPATH_D3D10:
2326 case RENDERPATH_D3D11:
2327 case RENDERPATH_SOFT:
2329 case RENDERPATH_GL20:
2330 #ifdef GL_SAMPLE_ALPHA_TO_COVERAGE_ARB
2331 // alpha to coverage turns the alpha value of the pixel into 0%, 25%, 50%, 75% or 100% by masking the multisample fragments accordingly
2333 if (gl_state.alphatocoverage)
2335 qglEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);CHECKGLERROR
2336 // qglEnable(GL_MULTISAMPLE_ARB);CHECKGLERROR
2340 qglDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);CHECKGLERROR
2341 // qglDisable(GL_MULTISAMPLE_ARB);CHECKGLERROR
2349 void GL_ColorMask(int r, int g, int b, int a)
2351 // NOTE: this matches D3DCOLORWRITEENABLE_RED, GREEN, BLUE, ALPHA
2352 int state = (r ? 1 : 0) | (g ? 2 : 0) | (b ? 4 : 0) | (a ? 8 : 0);
2353 if (gl_state.colormask != state)
2355 gl_state.colormask = state;
2356 switch(vid.renderpath)
2358 case RENDERPATH_GL11:
2359 case RENDERPATH_GL13:
2360 case RENDERPATH_GL20:
2361 case RENDERPATH_GLES1:
2362 case RENDERPATH_GLES2:
2364 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
2366 case RENDERPATH_D3D9:
2368 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_COLORWRITEENABLE, state);
2371 case RENDERPATH_D3D10:
2372 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2374 case RENDERPATH_D3D11:
2375 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2377 case RENDERPATH_SOFT:
2378 DPSOFTRAST_ColorMask(r, g, b, a);
2384 void GL_Color(float cr, float cg, float cb, float ca)
2386 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)
2388 gl_state.color4f[0] = cr;
2389 gl_state.color4f[1] = cg;
2390 gl_state.color4f[2] = cb;
2391 gl_state.color4f[3] = ca;
2392 switch(vid.renderpath)
2394 case RENDERPATH_GL11:
2395 case RENDERPATH_GL13:
2396 case RENDERPATH_GLES1:
2398 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
2401 case RENDERPATH_D3D9:
2402 case RENDERPATH_D3D10:
2403 case RENDERPATH_D3D11:
2404 // no equivalent in D3D
2406 case RENDERPATH_SOFT:
2407 DPSOFTRAST_Color4f(cr, cg, cb, ca);
2409 case RENDERPATH_GL20:
2410 case RENDERPATH_GLES2:
2411 qglVertexAttrib4f(GLSLATTRIB_COLOR, cr, cg, cb, ca);
2417 void GL_Scissor (int x, int y, int width, int height)
2419 switch(vid.renderpath)
2421 case RENDERPATH_GL11:
2422 case RENDERPATH_GL13:
2423 case RENDERPATH_GL20:
2424 case RENDERPATH_GLES1:
2425 case RENDERPATH_GLES2:
2427 qglScissor(x, y,width,height);
2430 case RENDERPATH_D3D9:
2436 d3drect.right = x + width;
2437 d3drect.bottom = y + height;
2438 IDirect3DDevice9_SetScissorRect(vid_d3d9dev, &d3drect);
2442 case RENDERPATH_D3D10:
2443 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2445 case RENDERPATH_D3D11:
2446 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2448 case RENDERPATH_SOFT:
2449 DPSOFTRAST_Scissor(x, y, width, height);
2454 void GL_ScissorTest(int state)
2456 if (gl_state.scissortest != state)
2458 gl_state.scissortest = state;
2459 switch(vid.renderpath)
2461 case RENDERPATH_GL11:
2462 case RENDERPATH_GL13:
2463 case RENDERPATH_GL20:
2464 case RENDERPATH_GLES1:
2465 case RENDERPATH_GLES2:
2467 if(gl_state.scissortest)
2468 qglEnable(GL_SCISSOR_TEST);
2470 qglDisable(GL_SCISSOR_TEST);
2473 case RENDERPATH_D3D9:
2475 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SCISSORTESTENABLE, gl_state.scissortest);
2478 case RENDERPATH_D3D10:
2479 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2481 case RENDERPATH_D3D11:
2482 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2484 case RENDERPATH_SOFT:
2485 DPSOFTRAST_ScissorTest(gl_state.scissortest);
2491 void GL_Clear(int mask, const float *colorvalue, float depthvalue, int stencilvalue)
2493 static const float blackcolor[4] = {0, 0, 0, 0};
2494 // prevent warnings when trying to clear a buffer that does not exist
2496 colorvalue = blackcolor;
2499 mask &= ~GL_STENCIL_BUFFER_BIT;
2502 switch(vid.renderpath)
2504 case RENDERPATH_GL11:
2505 case RENDERPATH_GL13:
2506 case RENDERPATH_GL20:
2507 case RENDERPATH_GLES1:
2508 case RENDERPATH_GLES2:
2510 if (mask & GL_COLOR_BUFFER_BIT)
2512 qglClearColor(colorvalue[0], colorvalue[1], colorvalue[2], colorvalue[3]);CHECKGLERROR
2514 if (mask & GL_DEPTH_BUFFER_BIT)
2517 qglClearDepthf(depthvalue);CHECKGLERROR
2519 qglClearDepth(depthvalue);CHECKGLERROR
2522 if (mask & GL_STENCIL_BUFFER_BIT)
2524 qglClearStencil(stencilvalue);CHECKGLERROR
2526 qglClear(mask);CHECKGLERROR
2528 case RENDERPATH_D3D9:
2530 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);
2533 case RENDERPATH_D3D10:
2534 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2536 case RENDERPATH_D3D11:
2537 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2539 case RENDERPATH_SOFT:
2540 if (mask & GL_COLOR_BUFFER_BIT)
2541 DPSOFTRAST_ClearColor(colorvalue[0], colorvalue[1], colorvalue[2], colorvalue[3]);
2542 if (mask & GL_DEPTH_BUFFER_BIT)
2543 DPSOFTRAST_ClearDepth(depthvalue);
2548 void GL_ReadPixelsBGRA(int x, int y, int width, int height, unsigned char *outpixels)
2550 switch(vid.renderpath)
2552 case RENDERPATH_GL11:
2553 case RENDERPATH_GL13:
2554 case RENDERPATH_GL20:
2555 case RENDERPATH_GLES1:
2556 case RENDERPATH_GLES2:
2558 qglReadPixels(x, y, width, height, GL_BGRA, GL_UNSIGNED_BYTE, outpixels);CHECKGLERROR
2560 case RENDERPATH_D3D9:
2563 // LordHavoc: we can't directly download the backbuffer because it may be
2564 // multisampled, and it may not be lockable, so we blit it to a lockable
2565 // surface of the same dimensions (but without multisample) to resolve the
2566 // multisample buffer to a normal image, and then lock that...
2567 IDirect3DSurface9 *stretchsurface = NULL;
2568 if (!FAILED(IDirect3DDevice9_CreateRenderTarget(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, TRUE, &stretchsurface, NULL)))
2570 D3DLOCKED_RECT lockedrect;
2571 if (!FAILED(IDirect3DDevice9_StretchRect(vid_d3d9dev, gl_state.d3drt_backbuffercolorsurface, NULL, stretchsurface, NULL, D3DTEXF_POINT)))
2573 if (!FAILED(IDirect3DSurface9_LockRect(stretchsurface, &lockedrect, NULL, D3DLOCK_READONLY)))
2576 unsigned char *row = (unsigned char *)lockedrect.pBits + x * 4 + lockedrect.Pitch * (vid.height - 1 - y);
2577 for (line = 0;line < height;line++, row -= lockedrect.Pitch)
2578 memcpy(outpixels + line * width * 4, row, width * 4);
2579 IDirect3DSurface9_UnlockRect(stretchsurface);
2582 IDirect3DSurface9_Release(stretchsurface);
2585 //IDirect3DSurface9 *syssurface = NULL;
2586 //if (!FAILED(IDirect3DDevice9_CreateRenderTarget(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, FALSE, &stretchsurface, NULL)))
2587 //if (!FAILED(IDirect3DDevice9_CreateOffscreenPlainSurface(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &syssurface, NULL)))
2588 //IDirect3DDevice9_GetRenderTargetData(vid_d3d9dev, gl_state.d3drt_backbuffercolorsurface, syssurface);
2589 //if (!FAILED(IDirect3DDevice9_GetFrontBufferData(vid_d3d9dev, 0, syssurface)))
2590 //if (!FAILED(IDirect3DSurface9_LockRect(syssurface, &lockedrect, NULL, D3DLOCK_READONLY)))
2591 //IDirect3DSurface9_UnlockRect(syssurface);
2592 //IDirect3DSurface9_Release(syssurface);
2596 case RENDERPATH_D3D10:
2597 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2599 case RENDERPATH_D3D11:
2600 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2602 case RENDERPATH_SOFT:
2603 DPSOFTRAST_GetPixelsBGRA(x, y, width, height, outpixels);
2608 // called at beginning of frame
2609 void R_Mesh_Start(void)
2612 R_Mesh_SetRenderTargets(0, NULL, NULL, NULL, NULL, NULL);
2614 if (gl_printcheckerror.integer && !gl_paranoid.integer)
2616 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
2617 Cvar_SetValueQuick(&gl_paranoid, 1);
2621 static qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
2625 char compilelog[MAX_INPUTLINE];
2626 shaderobject = qglCreateShader(shadertypeenum);CHECKGLERROR
2629 qglShaderSource(shaderobject, numstrings, strings, NULL);CHECKGLERROR
2630 qglCompileShader(shaderobject);CHECKGLERROR
2631 qglGetShaderiv(shaderobject, GL_COMPILE_STATUS, &shadercompiled);CHECKGLERROR
2632 qglGetShaderInfoLog(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
2633 if (compilelog[0] && ((strstr(compilelog, "error") || strstr(compilelog, "ERROR") || strstr(compilelog, "Error")) || ((strstr(compilelog, "WARNING") || strstr(compilelog, "warning") || strstr(compilelog, "Warning")) && developer.integer) || developer_extra.integer))
2635 int i, j, pretextlines = 0;
2636 for (i = 0;i < numstrings - 1;i++)
2637 for (j = 0;strings[i][j];j++)
2638 if (strings[i][j] == '\n')
2640 Con_Printf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
2642 if (!shadercompiled)
2644 qglDeleteShader(shaderobject);CHECKGLERROR
2647 qglAttachShader(programobject, shaderobject);CHECKGLERROR
2648 qglDeleteShader(shaderobject);CHECKGLERROR
2652 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)
2654 GLint programlinked;
2655 GLuint programobject = 0;
2656 char linklog[MAX_INPUTLINE];
2659 programobject = qglCreateProgram();CHECKGLERROR
2663 qglBindAttribLocation(programobject, GLSLATTRIB_POSITION , "Attrib_Position" );
2664 qglBindAttribLocation(programobject, GLSLATTRIB_COLOR , "Attrib_Color" );
2665 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD0, "Attrib_TexCoord0");
2666 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD1, "Attrib_TexCoord1");
2667 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD2, "Attrib_TexCoord2");
2668 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD3, "Attrib_TexCoord3");
2669 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD4, "Attrib_TexCoord4");
2670 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD5, "Attrib_TexCoord5");
2671 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD6, "Attrib_TexCoord6");
2672 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD7, "Attrib_TexCoord7");
2674 if(vid.support.gl20shaders130)
2675 qglBindFragDataLocation(programobject, 0, "dp_FragColor");
2678 if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER, "vertex", vertexstrings_count, vertexstrings_list))
2681 #ifdef GL_GEOMETRY_SHADER
2682 if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER, "geometry", geometrystrings_count, geometrystrings_list))
2686 if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER, "fragment", fragmentstrings_count, fragmentstrings_list))
2689 qglLinkProgram(programobject);CHECKGLERROR
2690 qglGetProgramiv(programobject, GL_LINK_STATUS, &programlinked);CHECKGLERROR
2691 qglGetProgramInfoLog(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
2694 if (strstr(linklog, "error") || strstr(linklog, "ERROR") || strstr(linklog, "Error") || strstr(linklog, "WARNING") || strstr(linklog, "warning") || strstr(linklog, "Warning") || developer_extra.integer)
2695 Con_DPrintf("program link log:\n%s\n", linklog);
2696 // software vertex shader is ok but software fragment shader is WAY
2697 // too slow, fail program if so.
2698 // NOTE: this string might be ATI specific, but that's ok because the
2699 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
2700 // software fragment shader due to low instruction and dependent
2702 if (strstr(linklog, "fragment shader will run in software"))
2703 programlinked = false;
2707 return programobject;
2709 qglDeleteProgram(programobject);CHECKGLERROR
2713 void GL_Backend_FreeProgram(unsigned int prog)
2716 qglDeleteProgram(prog);
2720 // renders triangles using vertices from the active arrays
2721 int paranoidblah = 0;
2722 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)
2724 unsigned int numelements = numtriangles * 3;
2726 size_t bufferoffset3i;
2728 size_t bufferoffset3s;
2729 if (numvertices < 3 || numtriangles < 1)
2731 if (numvertices < 0 || numtriangles < 0 || developer_extra.integer)
2732 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);
2735 if (!gl_mesh_prefer_short_elements.integer)
2739 if (element3i_indexbuffer)
2740 element3i_indexbuffer = NULL;
2742 // adjust the pointers for firsttriangle
2744 element3i += firsttriangle * 3;
2745 if (element3i_indexbuffer)
2746 element3i_bufferoffset += firsttriangle * 3 * sizeof(*element3i);
2748 element3s += firsttriangle * 3;
2749 if (element3s_indexbuffer)
2750 element3s_bufferoffset += firsttriangle * 3 * sizeof(*element3s);
2751 switch(vid.renderpath)
2753 case RENDERPATH_GL11:
2754 case RENDERPATH_GL13:
2755 case RENDERPATH_GL20:
2756 case RENDERPATH_GLES1:
2757 case RENDERPATH_GLES2:
2758 // check if the user specified to ignore static index buffers
2759 if (!gl_state.usevbo_staticindex || (gl_vbo.integer == 3 && !vid.forcevbo && (element3i_bufferoffset || element3s_bufferoffset)))
2761 element3i_indexbuffer = NULL;
2762 element3s_indexbuffer = NULL;
2765 case RENDERPATH_D3D9:
2766 case RENDERPATH_D3D10:
2767 case RENDERPATH_D3D11:
2769 case RENDERPATH_SOFT:
2772 // upload a dynamic index buffer if needed
2775 if (!element3s_indexbuffer && gl_state.usevbo_dynamicindex)
2777 if (gl_state.draw_dynamicindexbuffer)
2778 R_Mesh_UpdateMeshBuffer(gl_state.draw_dynamicindexbuffer, (void *)element3s, numelements * sizeof(*element3s));
2780 gl_state.draw_dynamicindexbuffer = R_Mesh_CreateMeshBuffer((void *)element3s, numelements * sizeof(*element3s), "temporary", true, true, true);
2781 element3s_indexbuffer = gl_state.draw_dynamicindexbuffer;
2782 element3s_bufferoffset = 0;
2787 if (!element3i_indexbuffer && gl_state.usevbo_dynamicindex)
2789 if (gl_state.draw_dynamicindexbuffer)
2790 R_Mesh_UpdateMeshBuffer(gl_state.draw_dynamicindexbuffer, (void *)element3i, numelements * sizeof(*element3i));
2792 gl_state.draw_dynamicindexbuffer = R_Mesh_CreateMeshBuffer((void *)element3i, numelements * sizeof(*element3i), "temporary", true, true, false);
2793 element3i_indexbuffer = gl_state.draw_dynamicindexbuffer;
2794 element3i_bufferoffset = 0;
2797 bufferobject3i = element3i_indexbuffer ? element3i_indexbuffer->bufferobject : 0;
2798 bufferoffset3i = element3i_bufferoffset;
2799 bufferobject3s = element3s_indexbuffer ? element3s_indexbuffer->bufferobject : 0;
2800 bufferoffset3s = element3s_bufferoffset;
2801 r_refdef.stats.draws++;
2802 r_refdef.stats.draws_vertices += numvertices;
2803 r_refdef.stats.draws_elements += numelements;
2804 if (gl_paranoid.integer)
2807 // LordHavoc: disabled this - it needs to be updated to handle components and gltype and stride in each array
2809 unsigned int j, size;
2811 // note: there's no validation done here on buffer objects because it
2812 // is somewhat difficult to get at the data, and gl_paranoid can be
2813 // used without buffer objects if the need arises
2814 // (the data could be gotten using glMapBuffer but it would be very
2815 // slow due to uncachable video memory reads)
2816 if (!qglIsEnabled(GL_VERTEX_ARRAY))
2817 Con_Print("R_Mesh_Draw: vertex array not enabled\n");
2819 if (gl_state.pointer_vertex_pointer)
2820 for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
2822 if (gl_state.pointer_color_enabled)
2824 if (!qglIsEnabled(GL_COLOR_ARRAY))
2825 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
2827 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
2828 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
2831 for (i = 0;i < vid.texarrayunits;i++)
2833 if (gl_state.units[i].arrayenabled)
2835 GL_ClientActiveTexture(i);
2836 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
2837 Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
2839 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
2840 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++)
2847 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2849 if (element3i[i] < firstvertex || element3i[i] >= firstvertex + numvertices)
2851 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3i array\n", element3i[i], firstvertex, firstvertex + numvertices);
2858 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2860 if (element3s[i] < firstvertex || element3s[i] >= firstvertex + numvertices)
2862 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3s array\n", element3s[i], firstvertex, firstvertex + numvertices);
2868 if (r_render.integer || r_refdef.draw2dstage)
2870 switch(vid.renderpath)
2872 case RENDERPATH_GL11:
2873 case RENDERPATH_GL13:
2874 case RENDERPATH_GL20:
2876 if (gl_mesh_testmanualfeeding.integer)
2879 unsigned int i, j, element;
2881 qglBegin(GL_TRIANGLES);
2882 if(vid.renderpath == RENDERPATH_GL20)
2884 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2887 element = element3i[i];
2889 element = element3s[i];
2891 element = firstvertex + i;
2892 for (j = 0;j < vid.texarrayunits;j++)
2894 if (gl_state.units[j].pointer_texcoord_pointer && gl_state.units[j].arrayenabled)
2896 if (gl_state.units[j].pointer_texcoord_gltype == GL_FLOAT)
2898 p = (const GLfloat *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2899 if (gl_state.units[j].pointer_texcoord_components == 4)
2900 qglVertexAttrib4f(GLSLATTRIB_TEXCOORD0 + j, p[0], p[1], p[2], p[3]);
2901 else if (gl_state.units[j].pointer_texcoord_components == 3)
2902 qglVertexAttrib3f(GLSLATTRIB_TEXCOORD0 + j, p[0], p[1], p[2]);
2903 else if (gl_state.units[j].pointer_texcoord_components == 2)
2904 qglVertexAttrib2f(GLSLATTRIB_TEXCOORD0 + j, p[0], p[1]);
2906 qglVertexAttrib1f(GLSLATTRIB_TEXCOORD0 + j, p[0]);
2908 else if (gl_state.units[j].pointer_texcoord_gltype == GL_SHORT)
2910 const GLshort *s = (const GLshort *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2911 if (gl_state.units[j].pointer_texcoord_components == 4)
2912 qglVertexAttrib4f(GLSLATTRIB_TEXCOORD0 + j, s[0], s[1], s[2], s[3]);
2913 else if (gl_state.units[j].pointer_texcoord_components == 3)
2914 qglVertexAttrib3f(GLSLATTRIB_TEXCOORD0 + j, s[0], s[1], s[2]);
2915 else if (gl_state.units[j].pointer_texcoord_components == 2)
2916 qglVertexAttrib2f(GLSLATTRIB_TEXCOORD0 + j, s[0], s[1]);
2917 else if (gl_state.units[j].pointer_texcoord_components == 1)
2918 qglVertexAttrib1f(GLSLATTRIB_TEXCOORD0 + j, s[0]);
2920 else if (gl_state.units[j].pointer_texcoord_gltype == GL_BYTE)
2922 const GLbyte *sb = (const GLbyte *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2923 if (gl_state.units[j].pointer_texcoord_components == 4)
2924 qglVertexAttrib4f(GLSLATTRIB_TEXCOORD0 + j, sb[0], sb[1], sb[2], sb[3]);
2925 else if (gl_state.units[j].pointer_texcoord_components == 3)
2926 qglVertexAttrib3f(GLSLATTRIB_TEXCOORD0 + j, sb[0], sb[1], sb[2]);
2927 else if (gl_state.units[j].pointer_texcoord_components == 2)
2928 qglVertexAttrib2f(GLSLATTRIB_TEXCOORD0 + j, sb[0], sb[1]);
2929 else if (gl_state.units[j].pointer_texcoord_components == 1)
2930 qglVertexAttrib1f(GLSLATTRIB_TEXCOORD0 + j, sb[0]);
2934 if (gl_state.pointer_color_pointer && gl_state.pointer_color_enabled && gl_state.pointer_color_components == 4)
2936 if (gl_state.pointer_color_gltype == GL_FLOAT)
2938 p = (const GLfloat *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
2939 qglVertexAttrib4f(GLSLATTRIB_COLOR, p[0], p[1], p[2], p[3]);
2941 else if (gl_state.pointer_color_gltype == GL_UNSIGNED_BYTE)
2943 const GLubyte *ub = (const GLubyte *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
2944 qglVertexAttrib4Nub(GLSLATTRIB_COLOR, ub[0], ub[1], ub[2], ub[3]);
2947 if (gl_state.pointer_vertex_gltype == GL_FLOAT)
2949 p = (const GLfloat *)((const unsigned char *)gl_state.pointer_vertex_pointer + element * gl_state.pointer_vertex_stride);
2950 if (gl_state.pointer_vertex_components == 4)
2951 qglVertexAttrib4f(GLSLATTRIB_POSITION, p[0], p[1], p[2], p[3]);
2952 else if (gl_state.pointer_vertex_components == 3)
2953 qglVertexAttrib3f(GLSLATTRIB_POSITION, p[0], p[1], p[2]);
2955 qglVertexAttrib2f(GLSLATTRIB_POSITION, p[0], p[1]);
2961 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2964 element = element3i[i];
2966 element = element3s[i];
2968 element = firstvertex + i;
2969 for (j = 0;j < vid.texarrayunits;j++)
2971 if (gl_state.units[j].pointer_texcoord_pointer && gl_state.units[j].arrayenabled)
2973 if (gl_state.units[j].pointer_texcoord_gltype == GL_FLOAT)
2975 p = (const GLfloat *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2976 if (vid.texarrayunits > 1)
2978 if (gl_state.units[j].pointer_texcoord_components == 4)
2979 qglMultiTexCoord4f(GL_TEXTURE0 + j, p[0], p[1], p[2], p[3]);
2980 else if (gl_state.units[j].pointer_texcoord_components == 3)
2981 qglMultiTexCoord3f(GL_TEXTURE0 + j, p[0], p[1], p[2]);
2982 else if (gl_state.units[j].pointer_texcoord_components == 2)
2983 qglMultiTexCoord2f(GL_TEXTURE0 + j, p[0], p[1]);
2985 qglMultiTexCoord1f(GL_TEXTURE0 + j, p[0]);
2989 if (gl_state.units[j].pointer_texcoord_components == 4)
2990 qglTexCoord4f(p[0], p[1], p[2], p[3]);
2991 else if (gl_state.units[j].pointer_texcoord_components == 3)
2992 qglTexCoord3f(p[0], p[1], p[2]);
2993 else if (gl_state.units[j].pointer_texcoord_components == 2)
2994 qglTexCoord2f(p[0], p[1]);
2996 qglTexCoord1f(p[0]);
2999 else if (gl_state.units[j].pointer_texcoord_gltype == GL_SHORT)
3001 const GLshort *s = (const GLshort *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
3002 if (vid.texarrayunits > 1)
3004 if (gl_state.units[j].pointer_texcoord_components == 4)
3005 qglMultiTexCoord4f(GL_TEXTURE0 + j, s[0], s[1], s[2], s[3]);
3006 else if (gl_state.units[j].pointer_texcoord_components == 3)
3007 qglMultiTexCoord3f(GL_TEXTURE0 + j, s[0], s[1], s[2]);
3008 else if (gl_state.units[j].pointer_texcoord_components == 2)
3009 qglMultiTexCoord2f(GL_TEXTURE0 + j, s[0], s[1]);
3010 else if (gl_state.units[j].pointer_texcoord_components == 1)
3011 qglMultiTexCoord1f(GL_TEXTURE0 + j, s[0]);
3015 if (gl_state.units[j].pointer_texcoord_components == 4)
3016 qglTexCoord4f(s[0], s[1], s[2], s[3]);
3017 else if (gl_state.units[j].pointer_texcoord_components == 3)
3018 qglTexCoord3f(s[0], s[1], s[2]);
3019 else if (gl_state.units[j].pointer_texcoord_components == 2)
3020 qglTexCoord2f(s[0], s[1]);
3021 else if (gl_state.units[j].pointer_texcoord_components == 1)
3022 qglTexCoord1f(s[0]);
3025 else if (gl_state.units[j].pointer_texcoord_gltype == GL_BYTE)
3027 const GLbyte *sb = (const GLbyte *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
3028 if (vid.texarrayunits > 1)
3030 if (gl_state.units[j].pointer_texcoord_components == 4)
3031 qglMultiTexCoord4f(GL_TEXTURE0 + j, sb[0], sb[1], sb[2], sb[3]);
3032 else if (gl_state.units[j].pointer_texcoord_components == 3)
3033 qglMultiTexCoord3f(GL_TEXTURE0 + j, sb[0], sb[1], sb[2]);
3034 else if (gl_state.units[j].pointer_texcoord_components == 2)
3035 qglMultiTexCoord2f(GL_TEXTURE0 + j, sb[0], sb[1]);
3036 else if (gl_state.units[j].pointer_texcoord_components == 1)
3037 qglMultiTexCoord1f(GL_TEXTURE0 + j, sb[0]);
3041 if (gl_state.units[j].pointer_texcoord_components == 4)
3042 qglTexCoord4f(sb[0], sb[1], sb[2], sb[3]);
3043 else if (gl_state.units[j].pointer_texcoord_components == 3)
3044 qglTexCoord3f(sb[0], sb[1], sb[2]);
3045 else if (gl_state.units[j].pointer_texcoord_components == 2)
3046 qglTexCoord2f(sb[0], sb[1]);
3047 else if (gl_state.units[j].pointer_texcoord_components == 1)
3048 qglTexCoord1f(sb[0]);
3053 if (gl_state.pointer_color_pointer && gl_state.pointer_color_enabled && gl_state.pointer_color_components == 4)
3055 if (gl_state.pointer_color_gltype == GL_FLOAT)
3057 p = (const GLfloat *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
3058 qglColor4f(p[0], p[1], p[2], p[3]);
3060 else if (gl_state.pointer_color_gltype == GL_UNSIGNED_BYTE)
3062 const GLubyte *ub = (const GLubyte *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
3063 qglColor4ub(ub[0], ub[1], ub[2], ub[3]);
3066 if (gl_state.pointer_vertex_gltype == GL_FLOAT)
3068 p = (const GLfloat *)((const unsigned char *)gl_state.pointer_vertex_pointer + element * gl_state.pointer_vertex_stride);
3069 if (gl_state.pointer_vertex_components == 4)
3070 qglVertex4f(p[0], p[1], p[2], p[3]);
3071 else if (gl_state.pointer_vertex_components == 3)
3072 qglVertex3f(p[0], p[1], p[2]);
3074 qglVertex2f(p[0], p[1]);
3082 else if (bufferobject3s)
3084 GL_BindEBO(bufferobject3s);
3086 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
3088 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, (void *)bufferoffset3s);
3094 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
3098 else if (bufferobject3i)
3100 GL_BindEBO(bufferobject3i);
3102 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
3104 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, (void *)bufferoffset3i);
3110 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
3118 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
3120 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, element3s);
3126 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
3134 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
3136 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, element3i);
3142 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
3148 qglDrawArrays(GL_TRIANGLES, firstvertex, numvertices);
3152 case RENDERPATH_D3D9:
3154 if (gl_state.d3dvertexbuffer && ((element3s && element3s_indexbuffer) || (element3i && element3i_indexbuffer)))
3156 if (element3s_indexbuffer)
3158 IDirect3DDevice9_SetIndices(vid_d3d9dev, (IDirect3DIndexBuffer9 *)element3s_indexbuffer->devicebuffer);
3159 IDirect3DDevice9_DrawIndexedPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, 0, firstvertex, numvertices, element3s_bufferoffset>>1, numtriangles);
3161 else if (element3i_indexbuffer)
3163 IDirect3DDevice9_SetIndices(vid_d3d9dev, (IDirect3DIndexBuffer9 *)element3i_indexbuffer->devicebuffer);
3164 IDirect3DDevice9_DrawIndexedPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, 0, firstvertex, numvertices, element3i_bufferoffset>>2, numtriangles);
3167 IDirect3DDevice9_DrawPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, firstvertex, numvertices);
3172 IDirect3DDevice9_DrawIndexedPrimitiveUP(vid_d3d9dev, D3DPT_TRIANGLELIST, firstvertex, numvertices, numtriangles, element3s, D3DFMT_INDEX16, gl_state.d3dvertexdata, gl_state.d3dvertexsize);
3174 IDirect3DDevice9_DrawIndexedPrimitiveUP(vid_d3d9dev, D3DPT_TRIANGLELIST, firstvertex, numvertices, numtriangles, element3i, D3DFMT_INDEX32, gl_state.d3dvertexdata, gl_state.d3dvertexsize);
3176 IDirect3DDevice9_DrawPrimitiveUP(vid_d3d9dev, D3DPT_TRIANGLELIST, numvertices, (void *)gl_state.d3dvertexdata, gl_state.d3dvertexsize);
3180 case RENDERPATH_D3D10:
3181 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3183 case RENDERPATH_D3D11:
3184 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3186 case RENDERPATH_SOFT:
3187 DPSOFTRAST_DrawTriangles(firstvertex, numvertices, numtriangles, element3i, element3s);
3189 case RENDERPATH_GLES1:
3190 case RENDERPATH_GLES2:
3191 // GLES does not have glDrawRangeElements, and generally
3192 // underperforms with index buffers, so this code path is
3193 // relatively straightforward...
3195 if (gl_paranoid.integer)
3197 int r, prog, enabled, i;
3198 GLsizei attriblength;
3201 GLchar attribname[1024];
3202 r = qglCheckFramebufferStatusEXT(GL_FRAMEBUFFER);CHECKGLERROR
3203 if (r != GL_FRAMEBUFFER_COMPLETE)
3204 Con_DPrintf("fbo %i not complete (default %i)\n", gl_state.framebufferobject, gl_state.defaultframebufferobject);
3205 #ifndef GL_CURRENT_PROGRAM
3206 #define GL_CURRENT_PROGRAM 0x8B8D
3208 qglGetIntegerv(GL_CURRENT_PROGRAM, &r);CHECKGLERROR
3209 if (r < 0 || r > 10000)
3210 Con_DPrintf("GL_CURRENT_PROGRAM = %i\n", r);
3212 for (i = 0;i < 8;i++)
3214 qglGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &r);CHECKGLERROR
3217 qglGetActiveAttrib(prog, i, sizeof(attribname), &attriblength, &attribsize, &attribtype, attribname);CHECKGLERROR
3218 Con_DPrintf("prog %i position %i length %i size %04X type %i name \"%s\"\n", prog, i, (int)attriblength, (int)attribsize, (int)attribtype, (char *)attribname);
3224 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
3229 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
3234 qglDrawArrays(GL_TRIANGLES, firstvertex, numvertices);
3242 // restores backend state, used when done with 3D rendering
3243 void R_Mesh_Finish(void)
3245 R_Mesh_SetRenderTargets(0, NULL, NULL, NULL, NULL, NULL);
3248 r_meshbuffer_t *R_Mesh_CreateMeshBuffer(const void *data, size_t size, const char *name, qboolean isindexbuffer, qboolean isdynamic, qboolean isindex16)
3250 r_meshbuffer_t *buffer;
3251 if (!(isdynamic ? (isindexbuffer ? gl_state.usevbo_dynamicindex : gl_state.usevbo_dynamicvertex) : (isindexbuffer ? gl_state.usevbo_staticindex : gl_state.usevbo_staticvertex)))
3253 buffer = (r_meshbuffer_t *)Mem_ExpandableArray_AllocRecord(&gl_state.meshbufferarray);
3254 memset(buffer, 0, sizeof(*buffer));
3255 buffer->bufferobject = 0;
3256 buffer->devicebuffer = NULL;
3258 buffer->isindexbuffer = isindexbuffer;
3259 buffer->isdynamic = isdynamic;
3260 buffer->isindex16 = isindex16;
3261 strlcpy(buffer->name, name, sizeof(buffer->name));
3262 R_Mesh_UpdateMeshBuffer(buffer, data, size);
3266 void R_Mesh_UpdateMeshBuffer(r_meshbuffer_t *buffer, const void *data, size_t size)
3270 if (buffer->isindexbuffer)
3272 r_refdef.stats.indexbufferuploadcount++;
3273 r_refdef.stats.indexbufferuploadsize += size;
3277 r_refdef.stats.vertexbufferuploadcount++;
3278 r_refdef.stats.vertexbufferuploadsize += size;
3280 switch(vid.renderpath)
3282 case RENDERPATH_GL11:
3283 case RENDERPATH_GL13:
3284 case RENDERPATH_GL20:
3285 case RENDERPATH_GLES1:
3286 case RENDERPATH_GLES2:
3287 if (!buffer->bufferobject)
3288 qglGenBuffersARB(1, (GLuint *)&buffer->bufferobject);
3289 if (buffer->isindexbuffer)
3290 GL_BindEBO(buffer->bufferobject);
3292 GL_BindVBO(buffer->bufferobject);
3293 qglBufferDataARB(buffer->isindexbuffer ? GL_ELEMENT_ARRAY_BUFFER : GL_ARRAY_BUFFER, size, data, buffer->isdynamic ? GL_STREAM_DRAW : GL_STATIC_DRAW);
3295 case RENDERPATH_D3D9:
3299 void *datapointer = NULL;
3300 if (buffer->isindexbuffer)
3302 IDirect3DIndexBuffer9 *d3d9indexbuffer = (IDirect3DIndexBuffer9 *)buffer->devicebuffer;
3303 if (size > buffer->size || !buffer->devicebuffer)
3305 if (buffer->devicebuffer)
3306 IDirect3DIndexBuffer9_Release((IDirect3DIndexBuffer9*)buffer->devicebuffer);
3307 buffer->devicebuffer = NULL;
3308 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)))
3309 Sys_Error("IDirect3DDevice9_CreateIndexBuffer(%p, %d, %x, %x, %x, %p, NULL) returned %x\n", vid_d3d9dev, (int)size, buffer->isdynamic ? (int)D3DUSAGE_DYNAMIC : 0, buffer->isindex16 ? (int)D3DFMT_INDEX16 : (int)D3DFMT_INDEX32, buffer->isdynamic ? (int)D3DPOOL_DEFAULT : (int)D3DPOOL_MANAGED, &d3d9indexbuffer, (int)result);
3310 buffer->devicebuffer = (void *)d3d9indexbuffer;
3311 buffer->size = size;
3313 if (!FAILED(IDirect3DIndexBuffer9_Lock(d3d9indexbuffer, 0, 0, &datapointer, buffer->isdynamic ? D3DLOCK_DISCARD : 0)))
3316 memcpy(datapointer, data, size);
3318 memset(datapointer, 0, size);
3319 IDirect3DIndexBuffer9_Unlock(d3d9indexbuffer);
3324 IDirect3DVertexBuffer9 *d3d9vertexbuffer = (IDirect3DVertexBuffer9 *)buffer->devicebuffer;
3325 if (size > buffer->size || !buffer->devicebuffer)
3327 if (buffer->devicebuffer)
3328 IDirect3DVertexBuffer9_Release((IDirect3DVertexBuffer9*)buffer->devicebuffer);
3329 buffer->devicebuffer = NULL;
3330 if (FAILED(result = IDirect3DDevice9_CreateVertexBuffer(vid_d3d9dev, size, buffer->isdynamic ? D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC : 0, 0, buffer->isdynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED, &d3d9vertexbuffer, NULL)))
3331 Sys_Error("IDirect3DDevice9_CreateVertexBuffer(%p, %d, %x, %x, %x, %p, NULL) returned %x\n", vid_d3d9dev, (int)size, buffer->isdynamic ? (int)D3DUSAGE_DYNAMIC : 0, 0, buffer->isdynamic ? (int)D3DPOOL_DEFAULT : (int)D3DPOOL_MANAGED, &d3d9vertexbuffer, (int)result);
3332 buffer->devicebuffer = (void *)d3d9vertexbuffer;
3333 buffer->size = size;
3335 if (!FAILED(IDirect3DVertexBuffer9_Lock(d3d9vertexbuffer, 0, 0, &datapointer, buffer->isdynamic ? D3DLOCK_DISCARD : 0)))
3338 memcpy(datapointer, data, size);
3340 memset(datapointer, 0, size);
3341 IDirect3DVertexBuffer9_Unlock(d3d9vertexbuffer);
3347 case RENDERPATH_D3D10:
3348 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3350 case RENDERPATH_D3D11:
3351 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3353 case RENDERPATH_SOFT:
3358 void R_Mesh_DestroyMeshBuffer(r_meshbuffer_t *buffer)
3362 switch(vid.renderpath)
3364 case RENDERPATH_GL11:
3365 case RENDERPATH_GL13:
3366 case RENDERPATH_GL20:
3367 case RENDERPATH_GLES1:
3368 case RENDERPATH_GLES2:
3369 qglDeleteBuffersARB(1, (GLuint *)&buffer->bufferobject);
3371 case RENDERPATH_D3D9:
3373 if (gl_state.d3dvertexbuffer == (void *)buffer)
3374 gl_state.d3dvertexbuffer = NULL;
3375 if (buffer->devicebuffer)
3377 if (buffer->isindexbuffer)
3378 IDirect3DIndexBuffer9_Release((IDirect3DIndexBuffer9 *)buffer->devicebuffer);
3380 IDirect3DVertexBuffer9_Release((IDirect3DVertexBuffer9 *)buffer->devicebuffer);
3381 buffer->devicebuffer = NULL;
3385 case RENDERPATH_D3D10:
3386 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3388 case RENDERPATH_D3D11:
3389 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3391 case RENDERPATH_SOFT:
3394 Mem_ExpandableArray_FreeRecord(&gl_state.meshbufferarray, (void *)buffer);
3397 void GL_Mesh_ListVBOs(qboolean printeach)
3400 size_t ebocount = 0, ebomemory = 0;
3401 size_t vbocount = 0, vbomemory = 0;
3402 r_meshbuffer_t *buffer;
3403 endindex = Mem_ExpandableArray_IndexRange(&gl_state.meshbufferarray);
3404 for (i = 0;i < endindex;i++)
3406 buffer = (r_meshbuffer_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.meshbufferarray, i);
3409 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)");}
3410 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)");}
3412 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);
3417 void R_Mesh_VertexPointer(int components, int gltype, size_t stride, const void *pointer, const r_meshbuffer_t *vertexbuffer, size_t bufferoffset)
3419 switch(vid.renderpath)
3421 case RENDERPATH_GL11:
3422 case RENDERPATH_GL13:
3423 case RENDERPATH_GLES1:
3424 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)
3426 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3427 gl_state.pointer_vertex_components = components;
3428 gl_state.pointer_vertex_gltype = gltype;
3429 gl_state.pointer_vertex_stride = stride;
3430 gl_state.pointer_vertex_pointer = pointer;
3431 gl_state.pointer_vertex_vertexbuffer = vertexbuffer;
3432 gl_state.pointer_vertex_offset = bufferoffset;
3434 GL_BindVBO(bufferobject);
3435 qglVertexPointer(components, gltype, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3438 case RENDERPATH_GL20:
3439 case RENDERPATH_GLES2:
3440 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)
3442 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3443 gl_state.pointer_vertex_components = components;
3444 gl_state.pointer_vertex_gltype = gltype;
3445 gl_state.pointer_vertex_stride = stride;
3446 gl_state.pointer_vertex_pointer = pointer;
3447 gl_state.pointer_vertex_vertexbuffer = vertexbuffer;
3448 gl_state.pointer_vertex_offset = bufferoffset;
3450 GL_BindVBO(bufferobject);
3451 qglVertexAttribPointer(GLSLATTRIB_POSITION, components, gltype, false, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3454 case RENDERPATH_D3D9:
3455 case RENDERPATH_D3D10:
3456 case RENDERPATH_D3D11:
3457 case RENDERPATH_SOFT:
3462 void R_Mesh_ColorPointer(int components, int gltype, size_t stride, const void *pointer, const r_meshbuffer_t *vertexbuffer, size_t bufferoffset)
3464 // note: vertexbuffer may be non-NULL even if pointer is NULL, so check
3465 // the pointer only.
3466 switch(vid.renderpath)
3468 case RENDERPATH_GL11:
3469 case RENDERPATH_GL13:
3470 case RENDERPATH_GLES1:
3475 // caller wants color array enabled
3476 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3477 if (!gl_state.pointer_color_enabled)
3479 gl_state.pointer_color_enabled = true;
3481 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
3483 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)
3485 gl_state.pointer_color_components = components;
3486 gl_state.pointer_color_gltype = gltype;
3487 gl_state.pointer_color_stride = stride;
3488 gl_state.pointer_color_pointer = pointer;
3489 gl_state.pointer_color_vertexbuffer = vertexbuffer;
3490 gl_state.pointer_color_offset = bufferoffset;
3492 GL_BindVBO(bufferobject);
3493 qglColorPointer(components, gltype, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3498 // caller wants color array disabled
3499 if (gl_state.pointer_color_enabled)
3501 gl_state.pointer_color_enabled = false;
3503 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
3504 // when color array is on the glColor gets trashed, set it again
3505 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
3510 case RENDERPATH_GL20:
3511 case RENDERPATH_GLES2:
3515 // caller wants color array enabled
3516 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3517 if (!gl_state.pointer_color_enabled)
3519 gl_state.pointer_color_enabled = true;
3521 qglEnableVertexAttribArray(GLSLATTRIB_COLOR);CHECKGLERROR
3523 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)
3525 gl_state.pointer_color_components = components;
3526 gl_state.pointer_color_gltype = gltype;
3527 gl_state.pointer_color_stride = stride;
3528 gl_state.pointer_color_pointer = pointer;
3529 gl_state.pointer_color_vertexbuffer = vertexbuffer;
3530 gl_state.pointer_color_offset = bufferoffset;
3532 GL_BindVBO(bufferobject);
3533 qglVertexAttribPointer(GLSLATTRIB_COLOR, components, gltype, false, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3538 // caller wants color array disabled
3539 if (gl_state.pointer_color_enabled)
3541 gl_state.pointer_color_enabled = false;
3543 qglDisableVertexAttribArray(GLSLATTRIB_COLOR);CHECKGLERROR
3544 // when color array is on the glColor gets trashed, set it again
3545 qglVertexAttrib4f(GLSLATTRIB_COLOR, gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
3549 case RENDERPATH_D3D9:
3550 case RENDERPATH_D3D10:
3551 case RENDERPATH_D3D11:
3552 case RENDERPATH_SOFT:
3557 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)
3559 gltextureunit_t *unit = gl_state.units + unitnum;
3560 // update array settings
3561 // note: there is no need to check bufferobject here because all cases
3562 // that involve a valid bufferobject also supply a texcoord array
3563 switch(vid.renderpath)
3565 case RENDERPATH_GL11:
3566 case RENDERPATH_GL13:
3567 case RENDERPATH_GLES1:
3572 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3573 // texture array unit is enabled, enable the array
3574 if (!unit->arrayenabled)
3576 unit->arrayenabled = true;
3577 GL_ClientActiveTexture(unitnum);
3578 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
3581 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)
3583 unit->pointer_texcoord_components = components;
3584 unit->pointer_texcoord_gltype = gltype;
3585 unit->pointer_texcoord_stride = stride;
3586 unit->pointer_texcoord_pointer = pointer;
3587 unit->pointer_texcoord_vertexbuffer = vertexbuffer;
3588 unit->pointer_texcoord_offset = bufferoffset;
3589 GL_ClientActiveTexture(unitnum);
3590 GL_BindVBO(bufferobject);
3591 qglTexCoordPointer(components, gltype, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3596 // texture array unit is disabled, disable the array
3597 if (unit->arrayenabled)
3599 unit->arrayenabled = false;
3600 GL_ClientActiveTexture(unitnum);
3601 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
3606 case RENDERPATH_GL20:
3607 case RENDERPATH_GLES2:
3611 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3612 // texture array unit is enabled, enable the array
3613 if (!unit->arrayenabled)
3615 unit->arrayenabled = true;
3616 qglEnableVertexAttribArray(unitnum+GLSLATTRIB_TEXCOORD0);CHECKGLERROR
3619 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)
3621 unit->pointer_texcoord_components = components;
3622 unit->pointer_texcoord_gltype = gltype;
3623 unit->pointer_texcoord_stride = stride;
3624 unit->pointer_texcoord_pointer = pointer;
3625 unit->pointer_texcoord_vertexbuffer = vertexbuffer;
3626 unit->pointer_texcoord_offset = bufferoffset;
3627 GL_BindVBO(bufferobject);
3628 qglVertexAttribPointer(unitnum+GLSLATTRIB_TEXCOORD0, components, gltype, false, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3633 // texture array unit is disabled, disable the array
3634 if (unit->arrayenabled)
3636 unit->arrayenabled = false;
3637 qglDisableVertexAttribArray(unitnum+GLSLATTRIB_TEXCOORD0);CHECKGLERROR
3641 case RENDERPATH_D3D9:
3642 case RENDERPATH_D3D10:
3643 case RENDERPATH_D3D11:
3644 case RENDERPATH_SOFT:
3649 int R_Mesh_TexBound(unsigned int unitnum, int id)
3651 gltextureunit_t *unit = gl_state.units + unitnum;
3652 if (unitnum >= vid.teximageunits)
3654 if (id == GL_TEXTURE_2D)
3656 if (id == GL_TEXTURE_3D)
3658 if (id == GL_TEXTURE_CUBE_MAP)
3659 return unit->tcubemap;
3663 void R_Mesh_CopyToTexture(rtexture_t *tex, int tx, int ty, int sx, int sy, int width, int height)
3665 switch(vid.renderpath)
3667 case RENDERPATH_GL11:
3668 case RENDERPATH_GL13:
3669 case RENDERPATH_GL20:
3670 case RENDERPATH_GLES1:
3671 case RENDERPATH_GLES2:
3672 R_Mesh_TexBind(0, tex);
3673 GL_ActiveTexture(0);CHECKGLERROR
3674 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, tx, ty, sx, sy, width, height);CHECKGLERROR
3676 case RENDERPATH_D3D9:
3679 IDirect3DSurface9 *currentsurface = NULL;
3680 IDirect3DSurface9 *texturesurface = NULL;
3683 sourcerect.left = sx;
3684 sourcerect.top = sy;
3685 sourcerect.right = sx + width;
3686 sourcerect.bottom = sy + height;
3689 destrect.right = tx + width;
3690 destrect.bottom = ty + height;
3691 if (!FAILED(IDirect3DTexture9_GetSurfaceLevel(((IDirect3DTexture9 *)tex->d3dtexture), 0, &texturesurface)))
3693 if (!FAILED(IDirect3DDevice9_GetRenderTarget(vid_d3d9dev, 0, ¤tsurface)))
3695 IDirect3DDevice9_StretchRect(vid_d3d9dev, currentsurface, &sourcerect, texturesurface, &destrect, D3DTEXF_NONE);
3696 IDirect3DSurface9_Release(currentsurface);
3698 IDirect3DSurface9_Release(texturesurface);
3703 case RENDERPATH_D3D10:
3704 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3706 case RENDERPATH_D3D11:
3707 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3709 case RENDERPATH_SOFT:
3710 DPSOFTRAST_CopyRectangleToTexture(tex->texnum, 0, tx, ty, sx, sy, width, height);
3716 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};
3719 void R_Mesh_ClearBindingsForTexture(int texnum)
3721 gltextureunit_t *unit;
3722 unsigned int unitnum;
3723 // this doesn't really unbind the texture, but it does prevent a mistaken "do nothing" behavior on the next time this same texnum is bound on the same unit as the same type (this mainly affects r_shadow_bouncegrid because 3D textures are so rarely used)
3724 for (unitnum = 0;unitnum < vid.teximageunits;unitnum++)
3726 unit = gl_state.units + unitnum;
3727 if (unit->t2d == texnum)
3729 if (unit->t3d == texnum)
3731 if (unit->tcubemap == texnum)
3732 unit->tcubemap = -1;
3736 void R_Mesh_TexBind(unsigned int unitnum, rtexture_t *tex)
3738 gltextureunit_t *unit = gl_state.units + unitnum;
3739 int tex2d, tex3d, texcubemap, texnum;
3740 if (unitnum >= vid.teximageunits)
3742 // if (unit->texture == tex)
3744 switch(vid.renderpath)
3746 case RENDERPATH_GL20:
3747 case RENDERPATH_GLES2:
3750 tex = r_texture_white;
3751 // not initialized enough yet...
3755 unit->texture = tex;
3756 texnum = R_GetTexture(tex);
3757 switch(tex->gltexturetypeenum)
3759 case GL_TEXTURE_2D: if (unit->t2d != texnum) {GL_ActiveTexture(unitnum);unit->t2d = texnum;qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR}break;
3760 case GL_TEXTURE_3D: if (unit->t3d != texnum) {GL_ActiveTexture(unitnum);unit->t3d = texnum;qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR}break;
3761 case GL_TEXTURE_CUBE_MAP: if (unit->tcubemap != texnum) {GL_ActiveTexture(unitnum);unit->tcubemap = texnum;qglBindTexture(GL_TEXTURE_CUBE_MAP, unit->tcubemap);CHECKGLERROR}break;
3764 case RENDERPATH_GL11:
3765 case RENDERPATH_GL13:
3766 case RENDERPATH_GLES1:
3767 unit->texture = tex;
3773 texnum = R_GetTexture(tex);
3774 switch(tex->gltexturetypeenum)
3782 case GL_TEXTURE_CUBE_MAP:
3783 texcubemap = texnum;
3787 // update 2d texture binding
3788 if (unit->t2d != tex2d)
3790 GL_ActiveTexture(unitnum);
3795 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
3802 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
3806 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
3808 // update 3d texture binding
3809 if (unit->t3d != tex3d)
3811 GL_ActiveTexture(unitnum);
3816 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
3823 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
3827 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
3829 // update cubemap texture binding
3830 if (unit->tcubemap != texcubemap)
3832 GL_ActiveTexture(unitnum);
3835 if (unit->tcubemap == 0)
3837 qglEnable(GL_TEXTURE_CUBE_MAP);CHECKGLERROR
3844 qglDisable(GL_TEXTURE_CUBE_MAP);CHECKGLERROR
3847 unit->tcubemap = texcubemap;
3848 qglBindTexture(GL_TEXTURE_CUBE_MAP, unit->tcubemap);CHECKGLERROR
3851 case RENDERPATH_D3D9:
3854 extern cvar_t gl_texture_anisotropy;
3857 tex = r_texture_white;
3858 // not initialized enough yet...
3862 // upload texture if needed
3864 if (unit->texture == tex)
3866 unit->texture = tex;
3867 IDirect3DDevice9_SetTexture(vid_d3d9dev, unitnum, (IDirect3DBaseTexture9*)tex->d3dtexture);
3868 //IDirect3DDevice9_SetRenderState(vid_d3d9dev, d3drswrap[unitnum], (tex->flags & TEXF_CLAMP) ? (D3DWRAPCOORD_0 | D3DWRAPCOORD_1 | D3DWRAPCOORD_2) : 0);
3869 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSU, tex->d3daddressu);
3870 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSV, tex->d3daddressv);
3871 if (tex->d3daddressw)
3872 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSW, tex->d3daddressw);
3873 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAGFILTER, tex->d3dmagfilter);
3874 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MINFILTER, tex->d3dminfilter);
3875 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MIPFILTER, tex->d3dmipfilter);
3876 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MIPMAPLODBIAS, tex->d3dmipmaplodbias);
3877 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAXMIPLEVEL, tex->d3dmaxmiplevelfilter);
3878 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAXANISOTROPY, gl_texture_anisotropy.integer);
3882 case RENDERPATH_D3D10:
3883 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3885 case RENDERPATH_D3D11:
3886 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3888 case RENDERPATH_SOFT:
3891 tex = r_texture_white;
3892 // not initialized enough yet...
3896 texnum = R_GetTexture(tex);
3897 if (unit->texture == tex)
3899 unit->texture = tex;
3900 DPSOFTRAST_SetTexture(unitnum, texnum);
3905 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
3907 gltextureunit_t *unit = gl_state.units + unitnum;
3908 switch(vid.renderpath)
3910 case RENDERPATH_GL11:
3911 case RENDERPATH_GL13:
3912 case RENDERPATH_GL20:
3913 case RENDERPATH_GLES1:
3914 case RENDERPATH_GLES2:
3916 if (matrix && matrix->m[3][3])
3918 // texmatrix specified, check if it is different
3919 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
3922 unit->texmatrixenabled = true;
3923 unit->matrix = *matrix;
3925 Matrix4x4_ToArrayFloatGL(&unit->matrix, glmatrix);
3926 GL_ActiveTexture(unitnum);
3927 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3928 qglLoadMatrixf(glmatrix);CHECKGLERROR
3929 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3934 // no texmatrix specified, revert to identity
3935 if (unit->texmatrixenabled)
3937 unit->texmatrixenabled = false;
3938 unit->matrix = identitymatrix;
3940 GL_ActiveTexture(unitnum);
3941 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3942 qglLoadIdentity();CHECKGLERROR
3943 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3948 case RENDERPATH_D3D9:
3949 case RENDERPATH_D3D10:
3950 case RENDERPATH_D3D11:
3952 case RENDERPATH_SOFT:
3957 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
3959 gltextureunit_t *unit = gl_state.units + unitnum;
3961 switch(vid.renderpath)
3963 case RENDERPATH_GL20:
3964 case RENDERPATH_GLES2:
3967 case RENDERPATH_GL13:
3968 case RENDERPATH_GLES1:
3969 #ifdef GL_TEXTURE_ENV
3970 // GL_ARB_texture_env_combine
3972 combinergb = GL_MODULATE;
3974 combinealpha = GL_MODULATE;
3979 if (combinergb != combinealpha || rgbscale != 1 || alphascale != 1)
3981 if (combinergb == GL_DECAL)
3982 combinergb = GL_INTERPOLATE;
3983 if (unit->combine != GL_COMBINE)
3985 unit->combine = GL_COMBINE;
3986 GL_ActiveTexture(unitnum);
3987 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);CHECKGLERROR
3988 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE mode
3990 if (unit->combinergb != combinergb)
3992 unit->combinergb = combinergb;
3993 GL_ActiveTexture(unitnum);
3994 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, unit->combinergb);CHECKGLERROR
3996 if (unit->combinealpha != combinealpha)
3998 unit->combinealpha = combinealpha;
3999 GL_ActiveTexture(unitnum);
4000 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, unit->combinealpha);CHECKGLERROR
4002 if (unit->rgbscale != rgbscale)
4004 unit->rgbscale = rgbscale;
4005 GL_ActiveTexture(unitnum);
4006 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE, unit->rgbscale);CHECKGLERROR
4008 if (unit->alphascale != alphascale)
4010 unit->alphascale = alphascale;
4011 GL_ActiveTexture(unitnum);
4012 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, unit->alphascale);CHECKGLERROR
4017 if (unit->combine != combinergb)
4019 unit->combine = combinergb;
4020 GL_ActiveTexture(unitnum);
4021 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
4026 case RENDERPATH_GL11:
4028 #ifdef GL_TEXTURE_ENV
4030 combinergb = GL_MODULATE;
4031 if (unit->combine != combinergb)
4033 unit->combine = combinergb;
4034 GL_ActiveTexture(unitnum);
4035 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
4039 case RENDERPATH_D3D9:
4040 case RENDERPATH_D3D10:
4041 case RENDERPATH_D3D11:
4043 case RENDERPATH_SOFT:
4048 void R_Mesh_ResetTextureState(void)
4050 unsigned int unitnum;
4054 for (unitnum = 0;unitnum < vid.teximageunits;unitnum++)
4055 R_Mesh_TexBind(unitnum, NULL);
4056 for (unitnum = 0;unitnum < vid.texarrayunits;unitnum++)
4057 R_Mesh_TexCoordPointer(unitnum, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4058 switch(vid.renderpath)
4060 case RENDERPATH_GL20:
4061 case RENDERPATH_GLES2:
4062 case RENDERPATH_D3D9:
4063 case RENDERPATH_D3D10:
4064 case RENDERPATH_D3D11:
4065 case RENDERPATH_SOFT:
4067 case RENDERPATH_GL11:
4068 case RENDERPATH_GL13:
4069 case RENDERPATH_GLES1:
4070 for (unitnum = 0;unitnum < vid.texunits;unitnum++)
4072 R_Mesh_TexCombine(unitnum, GL_MODULATE, GL_MODULATE, 1, 1);
4073 R_Mesh_TexMatrix(unitnum, NULL);
4082 //#define r_vertex3f_d3d9fvf (D3DFVF_XYZ)
4083 //#define r_vertexgeneric_d3d9fvf (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
4084 //#define r_vertexmesh_d3d9fvf (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX5 | D3DFVF_TEXCOORDSIZE1(3) | D3DFVF_TEXCOORDSIZE2(3) | D3DFVF_TEXCOORDSIZE3(3))
4086 D3DVERTEXELEMENT9 r_vertex3f_d3d9elements[] =
4088 {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
4092 D3DVERTEXELEMENT9 r_vertexgeneric_d3d9elements[] =
4094 {0, (int)((size_t)&((r_vertexgeneric_t *)0)->vertex3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
4095 {0, (int)((size_t)&((r_vertexgeneric_t *)0)->color4f ), D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
4096 {0, (int)((size_t)&((r_vertexgeneric_t *)0)->texcoord2f), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
4100 D3DVERTEXELEMENT9 r_vertexmesh_d3d9elements[] =
4102 {0, (int)((size_t)&((r_vertexmesh_t *)0)->vertex3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
4103 {0, (int)((size_t)&((r_vertexmesh_t *)0)->color4f ), D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
4104 {0, (int)((size_t)&((r_vertexmesh_t *)0)->texcoordtexture2f ), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
4105 {0, (int)((size_t)&((r_vertexmesh_t *)0)->svector3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
4106 {0, (int)((size_t)&((r_vertexmesh_t *)0)->tvector3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 2},
4107 {0, (int)((size_t)&((r_vertexmesh_t *)0)->normal3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 3},
4108 {0, (int)((size_t)&((r_vertexmesh_t *)0)->texcoordlightmap2f), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 4},
4112 IDirect3DVertexDeclaration9 *r_vertex3f_d3d9decl;
4113 IDirect3DVertexDeclaration9 *r_vertexgeneric_d3d9decl;
4114 IDirect3DVertexDeclaration9 *r_vertexmesh_d3d9decl;
4117 static void R_Mesh_InitVertexDeclarations(void)
4120 r_vertex3f_d3d9decl = NULL;
4121 r_vertexgeneric_d3d9decl = NULL;
4122 r_vertexmesh_d3d9decl = NULL;
4123 switch(vid.renderpath)
4125 case RENDERPATH_GL20:
4126 case RENDERPATH_GL13:
4127 case RENDERPATH_GL11:
4128 case RENDERPATH_GLES1:
4129 case RENDERPATH_GLES2:
4131 case RENDERPATH_D3D9:
4132 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertex3f_d3d9elements, &r_vertex3f_d3d9decl);
4133 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexgeneric_d3d9elements, &r_vertexgeneric_d3d9decl);
4134 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexmesh_d3d9elements, &r_vertexmesh_d3d9decl);
4136 case RENDERPATH_D3D10:
4137 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4139 case RENDERPATH_D3D11:
4140 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4142 case RENDERPATH_SOFT:
4148 static void R_Mesh_DestroyVertexDeclarations(void)
4151 if (r_vertex3f_d3d9decl)
4152 IDirect3DVertexDeclaration9_Release(r_vertex3f_d3d9decl);
4153 r_vertex3f_d3d9decl = NULL;
4154 if (r_vertexgeneric_d3d9decl)
4155 IDirect3DVertexDeclaration9_Release(r_vertexgeneric_d3d9decl);
4156 r_vertexgeneric_d3d9decl = NULL;
4157 if (r_vertexmesh_d3d9decl)
4158 IDirect3DVertexDeclaration9_Release(r_vertexmesh_d3d9decl);
4159 r_vertexmesh_d3d9decl = NULL;
4163 void R_Mesh_PrepareVertices_Vertex3f(int numvertices, const float *vertex3f, const r_meshbuffer_t *vertexbuffer)
4165 // upload temporary vertexbuffer for this rendering
4166 if (!gl_state.usevbo_staticvertex)
4167 vertexbuffer = NULL;
4168 if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
4170 if (gl_state.preparevertices_dynamicvertexbuffer)
4171 R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex3f, numvertices * sizeof(float[3]));
4173 gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex3f, numvertices * sizeof(float[3]), "temporary", false, true, false);
4174 vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
4176 switch(vid.renderpath)
4178 case RENDERPATH_GL20:
4179 case RENDERPATH_GLES2:
4182 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
4183 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4184 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4185 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4186 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4187 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4188 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4192 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
4193 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4194 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4195 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4196 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4197 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4198 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4201 case RENDERPATH_GL13:
4202 case RENDERPATH_GLES1:
4205 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
4206 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4207 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4208 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4212 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
4213 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4214 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4215 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4218 case RENDERPATH_GL11:
4221 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
4222 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4223 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4227 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
4228 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4229 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4232 case RENDERPATH_D3D9:
4234 IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertex3f_d3d9decl);
4236 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(float[3]));
4238 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, NULL, 0, 0);
4239 gl_state.d3dvertexbuffer = (void *)vertexbuffer;
4240 gl_state.d3dvertexdata = (void *)vertex3f;
4241 gl_state.d3dvertexsize = sizeof(float[3]);
4244 case RENDERPATH_D3D10:
4245 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4247 case RENDERPATH_D3D11:
4248 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4250 case RENDERPATH_SOFT:
4251 DPSOFTRAST_SetVertexPointer(vertex3f, sizeof(float[3]));
4252 DPSOFTRAST_SetColorPointer(NULL, 0);
4253 DPSOFTRAST_SetTexCoordPointer(0, 2, sizeof(float[2]), NULL);
4254 DPSOFTRAST_SetTexCoordPointer(1, 2, sizeof(float[2]), NULL);
4255 DPSOFTRAST_SetTexCoordPointer(2, 2, sizeof(float[2]), NULL);
4256 DPSOFTRAST_SetTexCoordPointer(3, 2, sizeof(float[2]), NULL);
4257 DPSOFTRAST_SetTexCoordPointer(4, 2, sizeof(float[2]), NULL);
4264 r_vertexgeneric_t *R_Mesh_PrepareVertices_Generic_Lock(int numvertices)
4267 size = sizeof(r_vertexgeneric_t) * numvertices;
4268 if (gl_state.preparevertices_tempdatamaxsize < size)
4270 gl_state.preparevertices_tempdatamaxsize = size;
4271 gl_state.preparevertices_tempdata = Mem_Realloc(r_main_mempool, gl_state.preparevertices_tempdata, gl_state.preparevertices_tempdatamaxsize);
4273 gl_state.preparevertices_vertexgeneric = (r_vertexgeneric_t *)gl_state.preparevertices_tempdata;
4274 gl_state.preparevertices_numvertices = numvertices;
4275 return gl_state.preparevertices_vertexgeneric;
4278 qboolean R_Mesh_PrepareVertices_Generic_Unlock(void)
4280 R_Mesh_PrepareVertices_Generic(gl_state.preparevertices_numvertices, gl_state.preparevertices_vertexgeneric, NULL);
4281 gl_state.preparevertices_vertexgeneric = NULL;
4282 gl_state.preparevertices_numvertices = 0;
4286 void R_Mesh_PrepareVertices_Generic_Arrays(int numvertices, const float *vertex3f, const float *color4f, const float *texcoord2f)
4289 r_vertexgeneric_t *vertex;
4290 switch(vid.renderpath)
4292 case RENDERPATH_GL20:
4293 case RENDERPATH_GLES2:
4294 if (!vid.useinterleavedarrays)
4296 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
4297 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
4298 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoord2f, NULL, 0);
4299 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4300 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4301 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4302 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4306 case RENDERPATH_GL11:
4307 case RENDERPATH_GL13:
4308 case RENDERPATH_GLES1:
4309 if (!vid.useinterleavedarrays)
4311 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
4312 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
4313 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoord2f, NULL, 0);
4314 if (vid.texunits >= 2)
4315 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4316 if (vid.texunits >= 3)
4317 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4321 case RENDERPATH_D3D9:
4322 case RENDERPATH_D3D10:
4323 case RENDERPATH_D3D11:
4325 case RENDERPATH_SOFT:
4326 DPSOFTRAST_SetVertexPointer(vertex3f, sizeof(float[3]));
4327 DPSOFTRAST_SetColorPointer(color4f, sizeof(float[4]));
4328 DPSOFTRAST_SetTexCoordPointer(0, 2, sizeof(float[2]), texcoord2f);
4329 DPSOFTRAST_SetTexCoordPointer(1, 2, sizeof(float[2]), NULL);
4330 DPSOFTRAST_SetTexCoordPointer(2, 2, sizeof(float[2]), NULL);
4331 DPSOFTRAST_SetTexCoordPointer(3, 2, sizeof(float[2]), NULL);
4332 DPSOFTRAST_SetTexCoordPointer(4, 2, sizeof(float[2]), NULL);
4336 // no quick path for this case, convert to vertex structs
4337 vertex = R_Mesh_PrepareVertices_Generic_Lock(numvertices);
4338 for (i = 0;i < numvertices;i++)
4339 VectorCopy(vertex3f + 3*i, vertex[i].vertex3f);
4342 for (i = 0;i < numvertices;i++)
4343 Vector4Copy(color4f + 4*i, vertex[i].color4f);
4347 for (i = 0;i < numvertices;i++)
4348 Vector4Copy(gl_state.color4f, vertex[i].color4f);
4351 for (i = 0;i < numvertices;i++)
4352 Vector2Copy(texcoord2f + 2*i, vertex[i].texcoord2f);
4353 R_Mesh_PrepareVertices_Generic_Unlock();
4354 R_Mesh_PrepareVertices_Generic(numvertices, vertex, NULL);
4357 void R_Mesh_PrepareVertices_Generic(int numvertices, const r_vertexgeneric_t *vertex, const r_meshbuffer_t *vertexbuffer)
4359 // upload temporary vertexbuffer for this rendering
4360 if (!gl_state.usevbo_staticvertex)
4361 vertexbuffer = NULL;
4362 if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
4364 if (gl_state.preparevertices_dynamicvertexbuffer)
4365 R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex, numvertices * sizeof(*vertex));
4367 gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex, numvertices * sizeof(*vertex), "temporary", false, true, false);
4368 vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
4370 switch(vid.renderpath)
4372 case RENDERPATH_GL20:
4373 case RENDERPATH_GLES2:
4376 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
4377 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , vertexbuffer, (int)((unsigned char *)vertex->color4f - (unsigned char *)vertex));
4378 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , vertexbuffer, (int)((unsigned char *)vertex->texcoord2f - (unsigned char *)vertex));
4379 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4380 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4381 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4382 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4386 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
4387 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , NULL, 0);
4388 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , NULL, 0);
4389 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4390 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4391 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4392 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4395 case RENDERPATH_GL13:
4396 case RENDERPATH_GLES1:
4399 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
4400 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , vertexbuffer, (int)((unsigned char *)vertex->color4f - (unsigned char *)vertex));
4401 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , vertexbuffer, (int)((unsigned char *)vertex->texcoord2f - (unsigned char *)vertex));
4402 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4406 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
4407 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , NULL, 0);
4408 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , NULL, 0);
4409 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4412 case RENDERPATH_GL11:
4415 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
4416 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , vertexbuffer, (int)((unsigned char *)vertex->color4f - (unsigned char *)vertex));
4417 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , vertexbuffer, (int)((unsigned char *)vertex->texcoord2f - (unsigned char *)vertex));
4421 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
4422 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , NULL, 0);
4423 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , NULL, 0);
4426 case RENDERPATH_D3D9:
4428 IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertexgeneric_d3d9decl);
4430 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(*vertex));
4432 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, NULL, 0, 0);
4433 gl_state.d3dvertexbuffer = (void *)vertexbuffer;
4434 gl_state.d3dvertexdata = (void *)vertex;
4435 gl_state.d3dvertexsize = sizeof(*vertex);
4438 case RENDERPATH_D3D10:
4439 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4441 case RENDERPATH_D3D11:
4442 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4444 case RENDERPATH_SOFT:
4445 DPSOFTRAST_SetVertexPointer(vertex->vertex3f, sizeof(*vertex));
4446 DPSOFTRAST_SetColorPointer(vertex->color4f, sizeof(*vertex));
4447 DPSOFTRAST_SetTexCoordPointer(0, 2, sizeof(*vertex), vertex->texcoord2f);
4448 DPSOFTRAST_SetTexCoordPointer(1, 2, sizeof(*vertex), NULL);
4449 DPSOFTRAST_SetTexCoordPointer(2, 2, sizeof(*vertex), NULL);
4450 DPSOFTRAST_SetTexCoordPointer(3, 2, sizeof(*vertex), NULL);
4451 DPSOFTRAST_SetTexCoordPointer(4, 2, sizeof(*vertex), NULL);
4458 r_vertexmesh_t *R_Mesh_PrepareVertices_Mesh_Lock(int numvertices)
4461 size = sizeof(r_vertexmesh_t) * numvertices;
4462 if (gl_state.preparevertices_tempdatamaxsize < size)
4464 gl_state.preparevertices_tempdatamaxsize = size;
4465 gl_state.preparevertices_tempdata = Mem_Realloc(r_main_mempool, gl_state.preparevertices_tempdata, gl_state.preparevertices_tempdatamaxsize);
4467 gl_state.preparevertices_vertexmesh = (r_vertexmesh_t *)gl_state.preparevertices_tempdata;
4468 gl_state.preparevertices_numvertices = numvertices;
4469 return gl_state.preparevertices_vertexmesh;
4472 qboolean R_Mesh_PrepareVertices_Mesh_Unlock(void)
4474 R_Mesh_PrepareVertices_Mesh(gl_state.preparevertices_numvertices, gl_state.preparevertices_vertexmesh, NULL);
4475 gl_state.preparevertices_vertexmesh = NULL;
4476 gl_state.preparevertices_numvertices = 0;
4480 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)
4483 r_vertexmesh_t *vertex;
4484 switch(vid.renderpath)
4486 case RENDERPATH_GL20:
4487 case RENDERPATH_GLES2:
4488 if (!vid.useinterleavedarrays)
4490 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
4491 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
4492 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoordtexture2f, NULL, 0);
4493 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT, sizeof(float[3]), svector3f, NULL, 0);
4494 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT, sizeof(float[3]), tvector3f, NULL, 0);
4495 R_Mesh_TexCoordPointer(3, 3, GL_FLOAT, sizeof(float[3]), normal3f, NULL, 0);
4496 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), texcoordlightmap2f, NULL, 0);
4500 case RENDERPATH_GL11:
4501 case RENDERPATH_GL13:
4502 case RENDERPATH_GLES1:
4503 if (!vid.useinterleavedarrays)
4505 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
4506 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
4507 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoordtexture2f, NULL, 0);
4508 if (vid.texunits >= 2)
4509 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), texcoordlightmap2f, NULL, 0);
4510 if (vid.texunits >= 3)
4511 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4515 case RENDERPATH_D3D9:
4516 case RENDERPATH_D3D10:
4517 case RENDERPATH_D3D11:
4519 case RENDERPATH_SOFT:
4520 DPSOFTRAST_SetVertexPointer(vertex3f, sizeof(float[3]));
4521 DPSOFTRAST_SetColorPointer(color4f, sizeof(float[4]));
4522 DPSOFTRAST_SetTexCoordPointer(0, 2, sizeof(float[2]), texcoordtexture2f);
4523 DPSOFTRAST_SetTexCoordPointer(1, 3, sizeof(float[3]), svector3f);
4524 DPSOFTRAST_SetTexCoordPointer(2, 3, sizeof(float[3]), tvector3f);
4525 DPSOFTRAST_SetTexCoordPointer(3, 3, sizeof(float[3]), normal3f);
4526 DPSOFTRAST_SetTexCoordPointer(4, 2, sizeof(float[2]), texcoordlightmap2f);
4530 vertex = R_Mesh_PrepareVertices_Mesh_Lock(numvertices);
4531 for (i = 0;i < numvertices;i++)
4532 VectorCopy(vertex3f + 3*i, vertex[i].vertex3f);
4534 for (i = 0;i < numvertices;i++)
4535 VectorCopy(svector3f + 3*i, vertex[i].svector3f);
4537 for (i = 0;i < numvertices;i++)
4538 VectorCopy(tvector3f + 3*i, vertex[i].tvector3f);
4540 for (i = 0;i < numvertices;i++)
4541 VectorCopy(normal3f + 3*i, vertex[i].normal3f);
4544 for (i = 0;i < numvertices;i++)
4545 Vector4Copy(color4f + 4*i, vertex[i].color4f);
4549 for (i = 0;i < numvertices;i++)
4550 Vector4Copy(gl_state.color4f, vertex[i].color4f);
4552 if (texcoordtexture2f)
4553 for (i = 0;i < numvertices;i++)
4554 Vector2Copy(texcoordtexture2f + 2*i, vertex[i].texcoordtexture2f);
4555 if (texcoordlightmap2f)
4556 for (i = 0;i < numvertices;i++)
4557 Vector2Copy(texcoordlightmap2f + 2*i, vertex[i].texcoordlightmap2f);
4558 R_Mesh_PrepareVertices_Mesh_Unlock();
4559 R_Mesh_PrepareVertices_Mesh(numvertices, vertex, NULL);
4562 void R_Mesh_PrepareVertices_Mesh(int numvertices, const r_vertexmesh_t *vertex, const r_meshbuffer_t *vertexbuffer)
4564 // upload temporary vertexbuffer for this rendering
4565 if (!gl_state.usevbo_staticvertex)
4566 vertexbuffer = NULL;
4567 if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
4569 if (gl_state.preparevertices_dynamicvertexbuffer)
4570 R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex, numvertices * sizeof(*vertex));
4572 gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex, numvertices * sizeof(*vertex), "temporary", false, true, false);
4573 vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
4575 switch(vid.renderpath)
4577 case RENDERPATH_GL20:
4578 case RENDERPATH_GLES2:
4581 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
4582 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , vertexbuffer, (int)((unsigned char *)vertex->color4f - (unsigned char *)vertex));
4583 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , vertexbuffer, (int)((unsigned char *)vertex->texcoordtexture2f - (unsigned char *)vertex));
4584 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT , sizeof(*vertex), vertex->svector3f , vertexbuffer, (int)((unsigned char *)vertex->svector3f - (unsigned char *)vertex));
4585 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT , sizeof(*vertex), vertex->tvector3f , vertexbuffer, (int)((unsigned char *)vertex->tvector3f - (unsigned char *)vertex));
4586 R_Mesh_TexCoordPointer(3, 3, GL_FLOAT , sizeof(*vertex), vertex->normal3f , vertexbuffer, (int)((unsigned char *)vertex->normal3f - (unsigned char *)vertex));
4587 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, vertexbuffer, (int)((unsigned char *)vertex->texcoordlightmap2f - (unsigned char *)vertex));
4591 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
4592 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , NULL, 0);
4593 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , NULL, 0);
4594 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT , sizeof(*vertex), vertex->svector3f , NULL, 0);
4595 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT , sizeof(*vertex), vertex->tvector3f , NULL, 0);
4596 R_Mesh_TexCoordPointer(3, 3, GL_FLOAT , sizeof(*vertex), vertex->normal3f , NULL, 0);
4597 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, NULL, 0);
4600 case RENDERPATH_GL13:
4601 case RENDERPATH_GLES1:
4604 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
4605 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , vertexbuffer, (int)((unsigned char *)vertex->color4f - (unsigned char *)vertex));
4606 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , vertexbuffer, (int)((unsigned char *)vertex->texcoordtexture2f - (unsigned char *)vertex));
4607 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, vertexbuffer, (int)((unsigned char *)vertex->texcoordlightmap2f - (unsigned char *)vertex));
4611 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
4612 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , NULL, 0);
4613 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , NULL, 0);
4614 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, NULL, 0);
4617 case RENDERPATH_GL11:
4620 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
4621 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , vertexbuffer, (int)((unsigned char *)vertex->color4f - (unsigned char *)vertex));
4622 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , vertexbuffer, (int)((unsigned char *)vertex->texcoordtexture2f - (unsigned char *)vertex));
4626 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
4627 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , NULL, 0);
4628 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , NULL, 0);
4631 case RENDERPATH_D3D9:
4633 IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertexmesh_d3d9decl);
4635 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(*vertex));
4637 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, NULL, 0, 0);
4638 gl_state.d3dvertexbuffer = (void *)vertexbuffer;
4639 gl_state.d3dvertexdata = (void *)vertex;
4640 gl_state.d3dvertexsize = sizeof(*vertex);
4643 case RENDERPATH_D3D10:
4644 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4646 case RENDERPATH_D3D11:
4647 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4649 case RENDERPATH_SOFT:
4650 DPSOFTRAST_SetVertexPointer(vertex->vertex3f, sizeof(*vertex));
4651 DPSOFTRAST_SetColorPointer(vertex->color4f, sizeof(*vertex));
4652 DPSOFTRAST_SetTexCoordPointer(0, 2, sizeof(*vertex), vertex->texcoordtexture2f);
4653 DPSOFTRAST_SetTexCoordPointer(1, 3, sizeof(*vertex), vertex->svector3f);
4654 DPSOFTRAST_SetTexCoordPointer(2, 3, sizeof(*vertex), vertex->tvector3f);
4655 DPSOFTRAST_SetTexCoordPointer(3, 3, sizeof(*vertex), vertex->normal3f);
4656 DPSOFTRAST_SetTexCoordPointer(4, 2, sizeof(*vertex), vertex->texcoordlightmap2f);
4661 void GL_BlendEquationSubtract(qboolean negated)
4665 switch(vid.renderpath)
4667 case RENDERPATH_GL11:
4668 case RENDERPATH_GL13:
4669 case RENDERPATH_GL20:
4670 case RENDERPATH_GLES1:
4671 case RENDERPATH_GLES2:
4672 qglBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT);
4674 case RENDERPATH_D3D9:
4676 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_BLENDOP, D3DBLENDOP_SUBTRACT);
4679 case RENDERPATH_D3D10:
4680 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4682 case RENDERPATH_D3D11:
4683 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4685 case RENDERPATH_SOFT:
4686 DPSOFTRAST_BlendSubtract(true);
4692 switch(vid.renderpath)
4694 case RENDERPATH_GL11:
4695 case RENDERPATH_GL13:
4696 case RENDERPATH_GL20:
4697 case RENDERPATH_GLES1:
4698 case RENDERPATH_GLES2:
4699 qglBlendEquationEXT(GL_FUNC_ADD);
4701 case RENDERPATH_D3D9:
4703 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_BLENDOP, D3DBLENDOP_ADD);
4706 case RENDERPATH_D3D10:
4707 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4709 case RENDERPATH_D3D11:
4710 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4712 case RENDERPATH_SOFT:
4713 DPSOFTRAST_BlendSubtract(false);