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 // transform all corners that are infront of the nearclip plane
710 VectorNegate(r_refdef.view.frustum[4].normal, plane4f);
711 plane4f[3] = r_refdef.view.frustum[4].dist;
713 for (i = 0;i < 8;i++)
715 Vector4Set(corner[i], (i & 1) ? maxs[0] : mins[0], (i & 2) ? maxs[1] : mins[1], (i & 4) ? maxs[2] : mins[2], 1);
716 dist[i] = DotProduct4(corner[i], plane4f);
717 sign[i] = dist[i] > 0;
720 VectorCopy(corner[i], vertex[numvertices]);
724 // if some points are behind the nearclip, add clipped edge points to make
725 // sure that the scissor boundary is complete
726 if (numvertices > 0 && numvertices < 8)
728 // add clipped edge points
729 for (i = 0;i < 12;i++)
733 if (sign[j] != sign[k])
735 f = dist[j] / (dist[j] - dist[k]);
736 VectorLerp(corner[j], f, corner[k], vertex[numvertices]);
742 // if we have no points to check, it is behind the view plane
746 // if we have some points to transform, check what screen area is covered
747 x1 = y1 = x2 = y2 = 0;
749 //Con_Printf("%i vertices to transform...\n", numvertices);
750 for (i = 0;i < numvertices;i++)
752 VectorCopy(vertex[i], v);
753 R_Viewport_TransformToScreen(&r_refdef.view.viewport, v, v2);
754 //Con_Printf("%.3f %.3f %.3f %.3f transformed to %.3f %.3f %.3f %.3f\n", v[0], v[1], v[2], v[3], v2[0], v2[1], v2[2], v2[3]);
757 if (x1 > v2[0]) x1 = v2[0];
758 if (x2 < v2[0]) x2 = v2[0];
759 if (y1 > v2[1]) y1 = v2[1];
760 if (y2 < v2[1]) y2 = v2[1];
769 // now convert the scissor rectangle to integer screen coordinates
770 ix1 = (int)(x1 - 1.0f);
771 //iy1 = vid.height - (int)(y2 - 1.0f);
772 //iy1 = r_refdef.view.viewport.width + 2 * r_refdef.view.viewport.x - (int)(y2 - 1.0f);
773 iy1 = (int)(y1 - 1.0f);
774 ix2 = (int)(x2 + 1.0f);
775 //iy2 = vid.height - (int)(y1 + 1.0f);
776 //iy2 = r_refdef.view.viewport.height + 2 * r_refdef.view.viewport.y - (int)(y1 + 1.0f);
777 iy2 = (int)(y2 + 1.0f);
778 //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
780 // clamp it to the screen
781 if (ix1 < r_refdef.view.viewport.x) ix1 = r_refdef.view.viewport.x;
782 if (iy1 < r_refdef.view.viewport.y) iy1 = r_refdef.view.viewport.y;
783 if (ix2 > r_refdef.view.viewport.x + r_refdef.view.viewport.width) ix2 = r_refdef.view.viewport.x + r_refdef.view.viewport.width;
784 if (iy2 > r_refdef.view.viewport.y + r_refdef.view.viewport.height) iy2 = r_refdef.view.viewport.y + r_refdef.view.viewport.height;
786 // if it is inside out, it's not visible
787 if (ix2 <= ix1 || iy2 <= iy1)
790 // the light area is visible, set up the scissor rectangle
793 scissor[2] = ix2 - ix1;
794 scissor[3] = iy2 - iy1;
796 // D3D Y coordinate is top to bottom, OpenGL is bottom to top, fix the D3D one
797 switch(vid.renderpath)
799 case RENDERPATH_D3D9:
800 case RENDERPATH_D3D10:
801 case RENDERPATH_D3D11:
802 scissor[1] = vid.height - scissor[1] - scissor[3];
804 case RENDERPATH_GL11:
805 case RENDERPATH_GL13:
806 case RENDERPATH_GL20:
807 case RENDERPATH_SOFT:
808 case RENDERPATH_GLES1:
809 case RENDERPATH_GLES2:
817 static void R_Viewport_ApplyNearClipPlaneFloatGL(const r_viewport_t *v, float *m, float normalx, float normaly, float normalz, float dist)
821 float clipPlane[4], v3[3], v4[3];
824 // This is inspired by Oblique Depth Projection from http://www.terathon.com/code/oblique.php
826 VectorSet(normal, normalx, normaly, normalz);
827 Matrix4x4_Transform3x3(&v->viewmatrix, normal, clipPlane);
828 VectorScale(normal, -dist, v3);
829 Matrix4x4_Transform(&v->viewmatrix, v3, v4);
830 // FIXME: LordHavoc: I think this can be done more efficiently somehow but I can't remember the technique
831 clipPlane[3] = -DotProduct(v4, clipPlane);
835 // testing code for comparing results
837 VectorCopy4(clipPlane, clipPlane2);
838 R_EntityMatrix(&identitymatrix);
839 VectorSet(q, normal[0], normal[1], normal[2], -dist);
840 qglClipPlane(GL_CLIP_PLANE0, q);
841 qglGetClipPlane(GL_CLIP_PLANE0, q);
842 VectorCopy4(q, clipPlane);
846 // Calculate the clip-space corner point opposite the clipping plane
847 // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
848 // transform it into camera space by multiplying it
849 // by the inverse of the projection matrix
850 q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + m[8]) / m[0];
851 q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + m[9]) / m[5];
853 q[3] = (1.0f + m[10]) / m[14];
855 // Calculate the scaled plane vector
856 d = 2.0f / DotProduct4(clipPlane, q);
858 // Replace the third row of the projection matrix
859 m[2] = clipPlane[0] * d;
860 m[6] = clipPlane[1] * d;
861 m[10] = clipPlane[2] * d + 1.0f;
862 m[14] = clipPlane[3] * d;
865 void R_Viewport_InitOrtho(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, float x1, float y1, float x2, float y2, float nearclip, float farclip, const float *nearplane)
867 float left = x1, right = x2, bottom = y2, top = y1, zNear = nearclip, zFar = farclip;
869 memset(v, 0, sizeof(*v));
870 v->type = R_VIEWPORTTYPE_ORTHO;
871 v->cameramatrix = *cameramatrix;
878 memset(m, 0, sizeof(m));
879 m[0] = 2/(right - left);
880 m[5] = 2/(top - bottom);
881 m[10] = -2/(zFar - zNear);
882 m[12] = - (right + left)/(right - left);
883 m[13] = - (top + bottom)/(top - bottom);
884 m[14] = - (zFar + zNear)/(zFar - zNear);
886 switch(vid.renderpath)
888 case RENDERPATH_GL11:
889 case RENDERPATH_GL13:
890 case RENDERPATH_GL20:
891 case RENDERPATH_SOFT:
892 case RENDERPATH_GLES1:
893 case RENDERPATH_GLES2:
895 case RENDERPATH_D3D9:
896 case RENDERPATH_D3D10:
897 case RENDERPATH_D3D11:
898 m[10] = -1/(zFar - zNear);
899 m[14] = -zNear/(zFar-zNear);
902 v->screentodepth[0] = -farclip / (farclip - nearclip);
903 v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
905 Matrix4x4_Invert_Full(&v->viewmatrix, &v->cameramatrix);
908 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
910 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
916 Vector4Set(test1, (x1+x2)*0.5f, (y1+y2)*0.5f, 0.0f, 1.0f);
917 R_Viewport_TransformToScreen(v, test1, test2);
918 Con_Printf("%f %f %f -> %f %f %f\n", test1[0], test1[1], test1[2], test2[0], test2[1], test2[2]);
923 void R_Viewport_InitPerspective(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, float frustumx, float frustumy, float nearclip, float farclip, const float *nearplane)
925 matrix4x4_t tempmatrix, basematrix;
927 memset(v, 0, sizeof(*v));
929 v->type = R_VIEWPORTTYPE_PERSPECTIVE;
930 v->cameramatrix = *cameramatrix;
937 memset(m, 0, sizeof(m));
938 m[0] = 1.0 / frustumx;
939 m[5] = 1.0 / frustumy;
940 m[10] = -(farclip + nearclip) / (farclip - nearclip);
942 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
943 v->screentodepth[0] = -farclip / (farclip - nearclip);
944 v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
946 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
947 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
948 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
949 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
952 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
954 if(v_flipped.integer)
962 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
965 void R_Viewport_InitPerspectiveInfinite(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, float frustumx, float frustumy, float nearclip, const float *nearplane)
967 matrix4x4_t tempmatrix, basematrix;
968 const float nudge = 1.0 - 1.0 / (1<<23);
970 memset(v, 0, sizeof(*v));
972 v->type = R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP;
973 v->cameramatrix = *cameramatrix;
980 memset(m, 0, sizeof(m));
981 m[ 0] = 1.0 / frustumx;
982 m[ 5] = 1.0 / frustumy;
985 m[14] = -2 * nearclip * nudge;
986 v->screentodepth[0] = (m[10] + 1) * 0.5 - 1;
987 v->screentodepth[1] = m[14] * -0.5;
989 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
990 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
991 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
992 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
995 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
997 if(v_flipped.integer)
1005 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
1008 float cubeviewmatrix[6][16] =
1010 // standard cubemap projections
1048 float rectviewmatrix[6][16] =
1050 // sign-preserving cubemap projections
1089 void R_Viewport_InitCubeSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, float nearclip, float farclip, const float *nearplane)
1091 matrix4x4_t tempmatrix, basematrix;
1093 memset(v, 0, sizeof(*v));
1094 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
1095 v->cameramatrix = *cameramatrix;
1099 memset(m, 0, sizeof(m));
1101 m[10] = -(farclip + nearclip) / (farclip - nearclip);
1103 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
1105 Matrix4x4_FromArrayFloatGL(&basematrix, cubeviewmatrix[side]);
1106 Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
1107 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
1110 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
1112 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
1115 void R_Viewport_InitRectSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, int border, float nearclip, float farclip, const float *nearplane)
1117 matrix4x4_t tempmatrix, basematrix;
1119 memset(v, 0, sizeof(*v));
1120 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
1121 v->cameramatrix = *cameramatrix;
1122 v->x = (side & 1) * size;
1123 v->y = (side >> 1) * size;
1127 memset(m, 0, sizeof(m));
1128 m[0] = m[5] = 1.0f * ((float)size - border) / size;
1129 m[10] = -(farclip + nearclip) / (farclip - nearclip);
1131 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
1133 Matrix4x4_FromArrayFloatGL(&basematrix, rectviewmatrix[side]);
1134 Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
1135 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
1137 switch(vid.renderpath)
1139 case RENDERPATH_GL20:
1140 case RENDERPATH_GL13:
1141 case RENDERPATH_GL11:
1142 case RENDERPATH_SOFT:
1143 case RENDERPATH_GLES1:
1144 case RENDERPATH_GLES2:
1146 case RENDERPATH_D3D9:
1149 case RENDERPATH_D3D10:
1150 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1152 case RENDERPATH_D3D11:
1153 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1158 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
1160 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
1163 void R_SetViewport(const r_viewport_t *v)
1168 // FIXME: v_flipped_state is evil, this probably breaks somewhere
1169 GL_SetMirrorState(v_flipped.integer && (v->type == R_VIEWPORTTYPE_PERSPECTIVE || v->type == R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP));
1171 // copy over the matrices to our state
1172 gl_viewmatrix = v->viewmatrix;
1173 gl_projectionmatrix = v->projectmatrix;
1175 switch(vid.renderpath)
1177 case RENDERPATH_GL13:
1178 case RENDERPATH_GL11:
1179 case RENDERPATH_GLES1:
1180 #ifdef GL_PROJECTION
1182 qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
1183 // Load the projection matrix into OpenGL
1184 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
1185 Matrix4x4_ToArrayFloatGL(&gl_projectionmatrix, m);
1186 qglLoadMatrixf(m);CHECKGLERROR
1187 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1190 case RENDERPATH_D3D9:
1193 D3DVIEWPORT9 d3dviewport;
1194 d3dviewport.X = gl_viewport.x;
1195 d3dviewport.Y = gl_viewport.y;
1196 d3dviewport.Width = gl_viewport.width;
1197 d3dviewport.Height = gl_viewport.height;
1198 d3dviewport.MinZ = gl_state.depthrange[0];
1199 d3dviewport.MaxZ = gl_state.depthrange[1];
1200 IDirect3DDevice9_SetViewport(vid_d3d9dev, &d3dviewport);
1204 case RENDERPATH_D3D10:
1205 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1207 case RENDERPATH_D3D11:
1208 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1210 case RENDERPATH_SOFT:
1211 DPSOFTRAST_Viewport(v->x, v->y, v->width, v->height);
1213 case RENDERPATH_GL20:
1214 case RENDERPATH_GLES2:
1216 qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
1220 // force an update of the derived matrices
1221 gl_modelmatrixchanged = true;
1222 R_EntityMatrix(&gl_modelmatrix);
1225 void R_GetViewport(r_viewport_t *v)
1230 static void GL_BindVBO(int bufferobject)
1232 if (gl_state.vertexbufferobject != bufferobject)
1234 gl_state.vertexbufferobject = bufferobject;
1236 qglBindBufferARB(GL_ARRAY_BUFFER, bufferobject);CHECKGLERROR
1240 static void GL_BindEBO(int bufferobject)
1242 if (gl_state.elementbufferobject != bufferobject)
1244 gl_state.elementbufferobject = bufferobject;
1246 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, bufferobject);CHECKGLERROR
1250 static const GLuint drawbuffers[4] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3};
1251 int R_Mesh_CreateFramebufferObject(rtexture_t *depthtexture, rtexture_t *colortexture, rtexture_t *colortexture2, rtexture_t *colortexture3, rtexture_t *colortexture4)
1253 switch(vid.renderpath)
1255 case RENDERPATH_GL11:
1256 case RENDERPATH_GL13:
1257 case RENDERPATH_GL20:
1258 case RENDERPATH_GLES1:
1259 case RENDERPATH_GLES2:
1260 if (vid.support.arb_framebuffer_object)
1264 qglGenFramebuffers(1, (GLuint*)&temp);CHECKGLERROR
1265 R_Mesh_SetRenderTargets(temp, NULL, NULL, NULL, NULL, NULL);
1266 // GL_ARB_framebuffer_object (GL3-class hardware) - depth stencil attachment
1268 // FIXME: separate stencil attachment on GLES
1269 if (depthtexture && depthtexture->texnum ) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT , depthtexture->gltexturetypeenum , depthtexture->texnum , 0);CHECKGLERROR
1271 if (depthtexture && depthtexture->texnum ) qglFramebufferTexture2D(GL_FRAMEBUFFER, depthtexture->glisdepthstencil ? GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT , depthtexture->gltexturetypeenum , depthtexture->texnum , 0);CHECKGLERROR
1273 if (depthtexture && depthtexture->renderbuffernum ) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, depthtexture->glisdepthstencil ? GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT , GL_RENDERBUFFER, depthtexture->renderbuffernum );CHECKGLERROR
1274 if (colortexture && colortexture->texnum ) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , colortexture->gltexturetypeenum , colortexture->texnum , 0);CHECKGLERROR
1275 if (colortexture2 && colortexture2->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 , colortexture2->gltexturetypeenum, colortexture2->texnum, 0);CHECKGLERROR
1276 if (colortexture3 && colortexture3->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2 , colortexture3->gltexturetypeenum, colortexture3->texnum, 0);CHECKGLERROR
1277 if (colortexture4 && colortexture4->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3 , colortexture4->gltexturetypeenum, colortexture4->texnum, 0);CHECKGLERROR
1278 if (colortexture && colortexture->renderbuffernum ) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , GL_RENDERBUFFER, colortexture->renderbuffernum );CHECKGLERROR
1279 if (colortexture2 && colortexture2->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 , GL_RENDERBUFFER, colortexture2->renderbuffernum);CHECKGLERROR
1280 if (colortexture3 && colortexture3->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2 , GL_RENDERBUFFER, colortexture3->renderbuffernum);CHECKGLERROR
1281 if (colortexture4 && colortexture4->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3 , GL_RENDERBUFFER, colortexture4->renderbuffernum);CHECKGLERROR
1284 if (colortexture4 && qglDrawBuffersARB)
1286 qglDrawBuffersARB(4, drawbuffers);CHECKGLERROR
1287 qglReadBuffer(GL_NONE);CHECKGLERROR
1289 else if (colortexture3 && qglDrawBuffersARB)
1291 qglDrawBuffersARB(3, drawbuffers);CHECKGLERROR
1292 qglReadBuffer(GL_NONE);CHECKGLERROR
1294 else if (colortexture2 && qglDrawBuffersARB)
1296 qglDrawBuffersARB(2, drawbuffers);CHECKGLERROR
1297 qglReadBuffer(GL_NONE);CHECKGLERROR
1299 else if (colortexture && qglDrawBuffer)
1301 qglDrawBuffer(GL_COLOR_ATTACHMENT0);CHECKGLERROR
1302 qglReadBuffer(GL_COLOR_ATTACHMENT0);CHECKGLERROR
1304 else if (qglDrawBuffer)
1306 qglDrawBuffer(GL_NONE);CHECKGLERROR
1307 qglReadBuffer(GL_NONE);CHECKGLERROR
1310 status = qglCheckFramebufferStatus(GL_FRAMEBUFFER);CHECKGLERROR
1311 if (status != GL_FRAMEBUFFER_COMPLETE)
1313 Con_Printf("R_Mesh_CreateFramebufferObject: glCheckFramebufferStatus returned %i\n", status);
1314 qglDeleteFramebuffers(1, (GLuint*)&temp);
1319 else if (vid.support.ext_framebuffer_object)
1323 qglGenFramebuffers(1, (GLuint*)&temp);CHECKGLERROR
1324 R_Mesh_SetRenderTargets(temp, NULL, NULL, NULL, NULL, NULL);
1325 // GL_EXT_framebuffer_object (GL2-class hardware) - no depth stencil attachment, let it break stencil
1326 if (depthtexture && depthtexture->texnum ) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT , depthtexture->gltexturetypeenum , depthtexture->texnum , 0);CHECKGLERROR
1327 if (depthtexture && depthtexture->renderbuffernum ) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT , GL_RENDERBUFFER, depthtexture->renderbuffernum );CHECKGLERROR
1328 if (colortexture && colortexture->texnum ) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , colortexture->gltexturetypeenum , colortexture->texnum , 0);CHECKGLERROR
1329 if (colortexture2 && colortexture2->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 , colortexture2->gltexturetypeenum, colortexture2->texnum, 0);CHECKGLERROR
1330 if (colortexture3 && colortexture3->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2 , colortexture3->gltexturetypeenum, colortexture3->texnum, 0);CHECKGLERROR
1331 if (colortexture4 && colortexture4->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3 , colortexture4->gltexturetypeenum, colortexture4->texnum, 0);CHECKGLERROR
1332 if (colortexture && colortexture->renderbuffernum ) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , GL_RENDERBUFFER, colortexture->renderbuffernum );CHECKGLERROR
1333 if (colortexture2 && colortexture2->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 , GL_RENDERBUFFER, colortexture2->renderbuffernum);CHECKGLERROR
1334 if (colortexture3 && colortexture3->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2 , GL_RENDERBUFFER, colortexture3->renderbuffernum);CHECKGLERROR
1335 if (colortexture4 && colortexture4->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3 , GL_RENDERBUFFER, colortexture4->renderbuffernum);CHECKGLERROR
1338 if (colortexture4 && qglDrawBuffersARB)
1340 qglDrawBuffersARB(4, drawbuffers);CHECKGLERROR
1341 qglReadBuffer(GL_NONE);CHECKGLERROR
1343 else if (colortexture3 && qglDrawBuffersARB)
1345 qglDrawBuffersARB(3, drawbuffers);CHECKGLERROR
1346 qglReadBuffer(GL_NONE);CHECKGLERROR
1348 else if (colortexture2 && qglDrawBuffersARB)
1350 qglDrawBuffersARB(2, drawbuffers);CHECKGLERROR
1351 qglReadBuffer(GL_NONE);CHECKGLERROR
1353 else if (colortexture && qglDrawBuffer)
1355 qglDrawBuffer(GL_COLOR_ATTACHMENT0);CHECKGLERROR
1356 qglReadBuffer(GL_COLOR_ATTACHMENT0);CHECKGLERROR
1358 else if (qglDrawBuffer)
1360 qglDrawBuffer(GL_NONE);CHECKGLERROR
1361 qglReadBuffer(GL_NONE);CHECKGLERROR
1364 status = qglCheckFramebufferStatus(GL_FRAMEBUFFER);CHECKGLERROR
1365 if (status != GL_FRAMEBUFFER_COMPLETE)
1367 Con_Printf("R_Mesh_CreateFramebufferObject: glCheckFramebufferStatus returned %i\n", status);
1368 qglDeleteFramebuffers(1, (GLuint*)&temp);
1374 case RENDERPATH_D3D9:
1375 case RENDERPATH_D3D10:
1376 case RENDERPATH_D3D11:
1378 case RENDERPATH_SOFT:
1384 void R_Mesh_DestroyFramebufferObject(int fbo)
1386 switch(vid.renderpath)
1388 case RENDERPATH_GL11:
1389 case RENDERPATH_GL13:
1390 case RENDERPATH_GL20:
1391 case RENDERPATH_GLES1:
1392 case RENDERPATH_GLES2:
1394 qglDeleteFramebuffers(1, (GLuint*)&fbo);
1396 case RENDERPATH_D3D9:
1397 case RENDERPATH_D3D10:
1398 case RENDERPATH_D3D11:
1400 case RENDERPATH_SOFT:
1406 void R_Mesh_SetRenderTargetsD3D9(IDirect3DSurface9 *depthsurface, IDirect3DSurface9 *colorsurface0, IDirect3DSurface9 *colorsurface1, IDirect3DSurface9 *colorsurface2, IDirect3DSurface9 *colorsurface3)
1408 gl_state.framebufferobject = depthsurface != gl_state.d3drt_backbufferdepthsurface || colorsurface0 != gl_state.d3drt_backbuffercolorsurface;
1409 if (gl_state.d3drt_depthsurface != depthsurface)
1411 gl_state.d3drt_depthsurface = depthsurface;
1412 IDirect3DDevice9_SetDepthStencilSurface(vid_d3d9dev, gl_state.d3drt_depthsurface);
1414 if (gl_state.d3drt_colorsurfaces[0] != colorsurface0)
1416 gl_state.d3drt_colorsurfaces[0] = colorsurface0;
1417 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 0, gl_state.d3drt_colorsurfaces[0]);
1419 if (gl_state.d3drt_colorsurfaces[1] != colorsurface1)
1421 gl_state.d3drt_colorsurfaces[1] = colorsurface1;
1422 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 1, gl_state.d3drt_colorsurfaces[1]);
1424 if (gl_state.d3drt_colorsurfaces[2] != colorsurface2)
1426 gl_state.d3drt_colorsurfaces[2] = colorsurface2;
1427 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 2, gl_state.d3drt_colorsurfaces[2]);
1429 if (gl_state.d3drt_colorsurfaces[3] != colorsurface3)
1431 gl_state.d3drt_colorsurfaces[3] = colorsurface3;
1432 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 3, gl_state.d3drt_colorsurfaces[3]);
1437 void R_Mesh_SetRenderTargets(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture, rtexture_t *colortexture2, rtexture_t *colortexture3, rtexture_t *colortexture4)
1441 rtexture_t *textures[5];
1442 Vector4Set(textures, colortexture, colortexture2, colortexture3, colortexture4);
1443 textures[4] = depthtexture;
1444 // unbind any matching textures immediately, otherwise D3D will complain about a bound texture being used as a render target
1445 for (j = 0;j < 5;j++)
1447 for (i = 0;i < vid.teximageunits;i++)
1448 if (gl_state.units[i].texture == textures[j])
1449 R_Mesh_TexBind(i, NULL);
1450 // set up framebuffer object or render targets for the active rendering API
1451 switch(vid.renderpath)
1453 case RENDERPATH_GL11:
1454 case RENDERPATH_GL13:
1455 case RENDERPATH_GL20:
1456 case RENDERPATH_GLES1:
1457 case RENDERPATH_GLES2:
1458 if (gl_state.framebufferobject != fbo)
1460 gl_state.framebufferobject = fbo;
1461 qglBindFramebuffer(GL_FRAMEBUFFER, gl_state.framebufferobject ? gl_state.framebufferobject : gl_state.defaultframebufferobject);
1464 case RENDERPATH_D3D9:
1466 // set up the new render targets, a NULL depthtexture intentionally binds nothing
1467 // TODO: optimize: keep surface pointer around in rtexture_t until texture is freed or lost
1470 IDirect3DSurface9 *surfaces[5];
1471 for (i = 0;i < 5;i++)
1476 if (textures[i]->d3dsurface)
1477 surfaces[i] = (IDirect3DSurface9 *)textures[i]->d3dsurface;
1479 IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9 *)textures[i]->d3dtexture, 0, &surfaces[i]);
1482 // set the render targets for real
1483 R_Mesh_SetRenderTargetsD3D9(surfaces[4], surfaces[0], surfaces[1], surfaces[2], surfaces[3]);
1484 // release the texture surface levels (they won't be lost while bound...)
1485 for (i = 0;i < 5;i++)
1486 if (textures[i] && !textures[i]->d3dsurface)
1487 IDirect3DSurface9_Release(surfaces[i]);
1490 R_Mesh_SetRenderTargetsD3D9(gl_state.d3drt_backbufferdepthsurface, gl_state.d3drt_backbuffercolorsurface, NULL, NULL, NULL);
1493 case RENDERPATH_D3D10:
1494 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1496 case RENDERPATH_D3D11:
1497 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1499 case RENDERPATH_SOFT:
1503 unsigned int *pointers[5];
1504 memset(pointers, 0, sizeof(pointers));
1505 for (i = 0;i < 5;i++)
1506 pointers[i] = textures[i] ? (unsigned int *)DPSOFTRAST_Texture_GetPixelPointer(textures[i]->texnum, 0) : NULL;
1507 width = DPSOFTRAST_Texture_GetWidth(textures[0] ? textures[0]->texnum : textures[4]->texnum, 0);
1508 height = DPSOFTRAST_Texture_GetHeight(textures[0] ? textures[0]->texnum : textures[4]->texnum, 0);
1509 DPSOFTRAST_SetRenderTargets(width, height, pointers[4], pointers[0], pointers[1], pointers[2], pointers[3]);
1512 DPSOFTRAST_SetRenderTargets(vid.width, vid.height, vid.softdepthpixels, vid.softpixels, NULL, NULL, NULL);
1518 static int d3dcmpforglfunc(int f)
1522 case GL_NEVER: return D3DCMP_NEVER;
1523 case GL_LESS: return D3DCMP_LESS;
1524 case GL_EQUAL: return D3DCMP_EQUAL;
1525 case GL_LEQUAL: return D3DCMP_LESSEQUAL;
1526 case GL_GREATER: return D3DCMP_GREATER;
1527 case GL_NOTEQUAL: return D3DCMP_NOTEQUAL;
1528 case GL_GEQUAL: return D3DCMP_GREATEREQUAL;
1529 case GL_ALWAYS: return D3DCMP_ALWAYS;
1530 default: Con_DPrintf("Unknown GL_DepthFunc\n");return D3DCMP_ALWAYS;
1534 static int d3dstencilopforglfunc(int f)
1538 case GL_KEEP: return D3DSTENCILOP_KEEP;
1539 case GL_INCR: return D3DSTENCILOP_INCR; // note: GL_INCR is clamped, D3DSTENCILOP_INCR wraps
1540 case GL_DECR: return D3DSTENCILOP_DECR; // note: GL_DECR is clamped, D3DSTENCILOP_DECR wraps
1541 default: Con_DPrintf("Unknown GL_StencilFunc\n");return D3DSTENCILOP_KEEP;
1546 static void GL_Backend_ResetState(void)
1549 gl_state.active = true;
1550 gl_state.depthtest = true;
1551 gl_state.alphatest = false;
1552 gl_state.alphafunc = GL_GEQUAL;
1553 gl_state.alphafuncvalue = 0.5f;
1554 gl_state.alphatocoverage = false;
1555 gl_state.blendfunc1 = GL_ONE;
1556 gl_state.blendfunc2 = GL_ZERO;
1557 gl_state.blend = false;
1558 gl_state.depthmask = GL_TRUE;
1559 gl_state.colormask = 15;
1560 gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
1561 gl_state.lockrange_first = 0;
1562 gl_state.lockrange_count = 0;
1563 gl_state.cullface = GL_FRONT;
1564 gl_state.cullfaceenable = false;
1565 gl_state.polygonoffset[0] = 0;
1566 gl_state.polygonoffset[1] = 0;
1567 gl_state.framebufferobject = 0;
1568 gl_state.depthfunc = GL_LEQUAL;
1570 switch(vid.renderpath)
1572 case RENDERPATH_D3D9:
1575 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_COLORWRITEENABLE, gl_state.colormask);
1576 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_NONE);
1577 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZFUNC, d3dcmpforglfunc(gl_state.depthfunc));
1578 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZENABLE, gl_state.depthtest);
1579 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZWRITEENABLE, gl_state.depthmask);
1580 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SLOPESCALEDEPTHBIAS, gl_state.polygonoffset[0]);
1581 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DEPTHBIAS, gl_state.polygonoffset[1] * (1.0f / 16777216.0f));
1585 case RENDERPATH_D3D10:
1586 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1588 case RENDERPATH_D3D11:
1589 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1591 case RENDERPATH_GL11:
1592 case RENDERPATH_GL13:
1593 case RENDERPATH_GLES1:
1594 #ifdef GL_ALPHA_TEST
1597 qglColorMask(1, 1, 1, 1);CHECKGLERROR
1598 qglAlphaFunc(gl_state.alphafunc, gl_state.alphafuncvalue);CHECKGLERROR
1599 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
1600 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1601 qglDisable(GL_BLEND);CHECKGLERROR
1602 qglCullFace(gl_state.cullface);CHECKGLERROR
1603 qglDisable(GL_CULL_FACE);CHECKGLERROR
1604 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1605 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1606 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1607 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1609 if (vid.support.arb_vertex_buffer_object)
1611 qglBindBufferARB(GL_ARRAY_BUFFER, 0);
1612 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, 0);
1615 if (vid.support.ext_framebuffer_object)
1617 //qglBindRenderbuffer(GL_RENDERBUFFER, 0);
1618 qglBindFramebuffer(GL_FRAMEBUFFER, 0);
1621 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
1622 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1624 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
1625 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1626 qglColor4f(1, 1, 1, 1);CHECKGLERROR
1628 if (vid.support.ext_framebuffer_object)
1629 qglBindFramebuffer(GL_FRAMEBUFFER, gl_state.framebufferobject);
1631 gl_state.unit = MAX_TEXTUREUNITS;
1632 gl_state.clientunit = MAX_TEXTUREUNITS;
1633 for (i = 0;i < vid.texunits;i++)
1635 GL_ActiveTexture(i);
1636 GL_ClientActiveTexture(i);
1637 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1638 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1639 if (vid.support.ext_texture_3d)
1641 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1642 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1644 if (vid.support.arb_texture_cube_map)
1646 qglDisable(GL_TEXTURE_CUBE_MAP);CHECKGLERROR
1647 qglBindTexture(GL_TEXTURE_CUBE_MAP, 0);CHECKGLERROR
1650 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
1651 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1652 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1653 qglLoadIdentity();CHECKGLERROR
1654 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1655 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
1660 case RENDERPATH_SOFT:
1661 DPSOFTRAST_ColorMask(1,1,1,1);
1662 DPSOFTRAST_BlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);
1663 DPSOFTRAST_CullFace(gl_state.cullface);
1664 DPSOFTRAST_DepthFunc(gl_state.depthfunc);
1665 DPSOFTRAST_DepthMask(gl_state.depthmask);
1666 DPSOFTRAST_PolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1667 DPSOFTRAST_SetRenderTargets(vid.width, vid.height, vid.softdepthpixels, vid.softpixels, NULL, NULL, NULL);
1668 DPSOFTRAST_Viewport(0, 0, vid.width, vid.height);
1670 case RENDERPATH_GL20:
1671 case RENDERPATH_GLES2:
1673 qglColorMask(1, 1, 1, 1);CHECKGLERROR
1674 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1675 qglDisable(GL_BLEND);CHECKGLERROR
1676 qglCullFace(gl_state.cullface);CHECKGLERROR
1677 qglDisable(GL_CULL_FACE);CHECKGLERROR
1678 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1679 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1680 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1681 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1682 if (vid.support.arb_vertex_buffer_object)
1684 qglBindBufferARB(GL_ARRAY_BUFFER, 0);
1685 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, 0);
1687 if (vid.support.ext_framebuffer_object)
1688 qglBindFramebuffer(GL_FRAMEBUFFER, gl_state.defaultframebufferobject);
1689 qglEnableVertexAttribArray(GLSLATTRIB_POSITION);
1690 qglVertexAttribPointer(GLSLATTRIB_POSITION, 3, GL_FLOAT, false, sizeof(float[3]), NULL);CHECKGLERROR
1691 qglDisableVertexAttribArray(GLSLATTRIB_COLOR);
1692 qglVertexAttribPointer(GLSLATTRIB_COLOR, 4, GL_FLOAT, false, sizeof(float[4]), NULL);CHECKGLERROR
1693 qglVertexAttrib4f(GLSLATTRIB_COLOR, 1, 1, 1, 1);
1694 gl_state.unit = MAX_TEXTUREUNITS;
1695 gl_state.clientunit = MAX_TEXTUREUNITS;
1696 for (i = 0;i < vid.teximageunits;i++)
1698 GL_ActiveTexture(i);
1699 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1700 if (vid.support.ext_texture_3d)
1702 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1704 if (vid.support.arb_texture_cube_map)
1706 qglBindTexture(GL_TEXTURE_CUBE_MAP, 0);CHECKGLERROR
1709 for (i = 0;i < vid.texarrayunits;i++)
1712 qglVertexAttribPointer(i+GLSLATTRIB_TEXCOORD0, 2, GL_FLOAT, false, sizeof(float[2]), NULL);CHECKGLERROR
1713 qglDisableVertexAttribArray(i+GLSLATTRIB_TEXCOORD0);CHECKGLERROR
1720 void GL_ActiveTexture(unsigned int num)
1722 if (gl_state.unit != num)
1724 gl_state.unit = num;
1725 switch(vid.renderpath)
1727 case RENDERPATH_GL11:
1728 case RENDERPATH_GL13:
1729 case RENDERPATH_GL20:
1730 case RENDERPATH_GLES1:
1731 case RENDERPATH_GLES2:
1732 if (qglActiveTexture)
1735 qglActiveTexture(GL_TEXTURE0 + gl_state.unit);
1739 case RENDERPATH_D3D9:
1740 case RENDERPATH_D3D10:
1741 case RENDERPATH_D3D11:
1743 case RENDERPATH_SOFT:
1749 void GL_ClientActiveTexture(unsigned int num)
1751 if (gl_state.clientunit != num)
1753 gl_state.clientunit = num;
1754 switch(vid.renderpath)
1756 case RENDERPATH_GL11:
1757 case RENDERPATH_GL13:
1758 case RENDERPATH_GLES1:
1759 if (qglActiveTexture)
1762 qglClientActiveTexture(GL_TEXTURE0 + gl_state.clientunit);
1766 case RENDERPATH_D3D9:
1767 case RENDERPATH_D3D10:
1768 case RENDERPATH_D3D11:
1770 case RENDERPATH_SOFT:
1772 case RENDERPATH_GL20:
1773 case RENDERPATH_GLES2:
1779 void GL_BlendFunc(int blendfunc1, int blendfunc2)
1781 if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
1783 qboolean blendenable;
1784 gl_state.blendfunc1 = blendfunc1;
1785 gl_state.blendfunc2 = blendfunc2;
1786 blendenable = (gl_state.blendfunc1 != GL_ONE || gl_state.blendfunc2 != GL_ZERO);
1787 switch(vid.renderpath)
1789 case RENDERPATH_GL11:
1790 case RENDERPATH_GL13:
1791 case RENDERPATH_GL20:
1792 case RENDERPATH_GLES1:
1793 case RENDERPATH_GLES2:
1795 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1796 if (gl_state.blend != blendenable)
1798 gl_state.blend = blendenable;
1799 if (!gl_state.blend)
1801 qglDisable(GL_BLEND);CHECKGLERROR
1805 qglEnable(GL_BLEND);CHECKGLERROR
1809 case RENDERPATH_D3D9:
1814 D3DBLEND d3dblendfunc[2];
1815 glblendfunc[0] = gl_state.blendfunc1;
1816 glblendfunc[1] = gl_state.blendfunc2;
1817 for (i = 0;i < 2;i++)
1819 switch(glblendfunc[i])
1821 case GL_ZERO: d3dblendfunc[i] = D3DBLEND_ZERO;break;
1822 case GL_ONE: d3dblendfunc[i] = D3DBLEND_ONE;break;
1823 case GL_SRC_COLOR: d3dblendfunc[i] = D3DBLEND_SRCCOLOR;break;
1824 case GL_ONE_MINUS_SRC_COLOR: d3dblendfunc[i] = D3DBLEND_INVSRCCOLOR;break;
1825 case GL_SRC_ALPHA: d3dblendfunc[i] = D3DBLEND_SRCALPHA;break;
1826 case GL_ONE_MINUS_SRC_ALPHA: d3dblendfunc[i] = D3DBLEND_INVSRCALPHA;break;
1827 case GL_DST_ALPHA: d3dblendfunc[i] = D3DBLEND_DESTALPHA;break;
1828 case GL_ONE_MINUS_DST_ALPHA: d3dblendfunc[i] = D3DBLEND_INVDESTALPHA;break;
1829 case GL_DST_COLOR: d3dblendfunc[i] = D3DBLEND_DESTCOLOR;break;
1830 case GL_ONE_MINUS_DST_COLOR: d3dblendfunc[i] = D3DBLEND_INVDESTCOLOR;break;
1833 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SRCBLEND, d3dblendfunc[0]);
1834 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DESTBLEND, d3dblendfunc[1]);
1835 if (gl_state.blend != blendenable)
1837 gl_state.blend = blendenable;
1838 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHABLENDENABLE, gl_state.blend);
1843 case RENDERPATH_D3D10:
1844 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1846 case RENDERPATH_D3D11:
1847 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1849 case RENDERPATH_SOFT:
1850 DPSOFTRAST_BlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);
1856 void GL_DepthMask(int state)
1858 if (gl_state.depthmask != state)
1860 gl_state.depthmask = state;
1861 switch(vid.renderpath)
1863 case RENDERPATH_GL11:
1864 case RENDERPATH_GL13:
1865 case RENDERPATH_GL20:
1866 case RENDERPATH_GLES1:
1867 case RENDERPATH_GLES2:
1869 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1871 case RENDERPATH_D3D9:
1873 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZWRITEENABLE, gl_state.depthmask);
1876 case RENDERPATH_D3D10:
1877 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1879 case RENDERPATH_D3D11:
1880 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1882 case RENDERPATH_SOFT:
1883 DPSOFTRAST_DepthMask(gl_state.depthmask);
1889 void GL_DepthTest(int state)
1891 if (gl_state.depthtest != state)
1893 gl_state.depthtest = state;
1894 switch(vid.renderpath)
1896 case RENDERPATH_GL11:
1897 case RENDERPATH_GL13:
1898 case RENDERPATH_GL20:
1899 case RENDERPATH_GLES1:
1900 case RENDERPATH_GLES2:
1902 if (gl_state.depthtest)
1904 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1908 qglDisable(GL_DEPTH_TEST);CHECKGLERROR
1911 case RENDERPATH_D3D9:
1913 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZENABLE, gl_state.depthtest);
1916 case RENDERPATH_D3D10:
1917 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1919 case RENDERPATH_D3D11:
1920 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1922 case RENDERPATH_SOFT:
1923 DPSOFTRAST_DepthTest(gl_state.depthtest);
1929 void GL_DepthFunc(int state)
1931 if (gl_state.depthfunc != state)
1933 gl_state.depthfunc = state;
1934 switch(vid.renderpath)
1936 case RENDERPATH_GL11:
1937 case RENDERPATH_GL13:
1938 case RENDERPATH_GL20:
1939 case RENDERPATH_GLES1:
1940 case RENDERPATH_GLES2:
1942 qglDepthFunc(gl_state.depthfunc);CHECKGLERROR
1944 case RENDERPATH_D3D9:
1946 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZFUNC, d3dcmpforglfunc(gl_state.depthfunc));
1949 case RENDERPATH_D3D10:
1950 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1952 case RENDERPATH_D3D11:
1953 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1955 case RENDERPATH_SOFT:
1956 DPSOFTRAST_DepthFunc(gl_state.depthfunc);
1962 void GL_DepthRange(float nearfrac, float farfrac)
1964 if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
1966 gl_state.depthrange[0] = nearfrac;
1967 gl_state.depthrange[1] = farfrac;
1968 switch(vid.renderpath)
1970 case RENDERPATH_GL11:
1971 case RENDERPATH_GL13:
1972 case RENDERPATH_GL20:
1973 case RENDERPATH_GLES1:
1974 case RENDERPATH_GLES2:
1976 qglDepthRangef(gl_state.depthrange[0], gl_state.depthrange[1]);
1978 qglDepthRange(gl_state.depthrange[0], gl_state.depthrange[1]);
1981 case RENDERPATH_D3D9:
1984 D3DVIEWPORT9 d3dviewport;
1985 d3dviewport.X = gl_viewport.x;
1986 d3dviewport.Y = gl_viewport.y;
1987 d3dviewport.Width = gl_viewport.width;
1988 d3dviewport.Height = gl_viewport.height;
1989 d3dviewport.MinZ = gl_state.depthrange[0];
1990 d3dviewport.MaxZ = gl_state.depthrange[1];
1991 IDirect3DDevice9_SetViewport(vid_d3d9dev, &d3dviewport);
1995 case RENDERPATH_D3D10:
1996 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1998 case RENDERPATH_D3D11:
1999 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2001 case RENDERPATH_SOFT:
2002 DPSOFTRAST_DepthRange(gl_state.depthrange[0], gl_state.depthrange[1]);
2008 void R_SetStencilSeparate(qboolean enable, int writemask, int frontfail, int frontzfail, int frontzpass, int backfail, int backzfail, int backzpass, int frontcompare, int backcompare, int comparereference, int comparemask)
2010 switch (vid.renderpath)
2012 case RENDERPATH_GL11:
2013 case RENDERPATH_GL13:
2014 case RENDERPATH_GL20:
2015 case RENDERPATH_GLES1:
2016 case RENDERPATH_GLES2:
2020 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
2024 qglDisable(GL_STENCIL_TEST);CHECKGLERROR
2026 if (vid.support.ati_separate_stencil)
2028 qglStencilMask(writemask);CHECKGLERROR
2029 qglStencilOpSeparate(GL_FRONT, frontfail, frontzfail, frontzpass);CHECKGLERROR
2030 qglStencilOpSeparate(GL_BACK, backfail, backzfail, backzpass);CHECKGLERROR
2031 qglStencilFuncSeparate(GL_FRONT, frontcompare, comparereference, comparereference);CHECKGLERROR
2032 qglStencilFuncSeparate(GL_BACK, backcompare, comparereference, comparereference);CHECKGLERROR
2034 else if (vid.support.ext_stencil_two_side)
2036 #ifdef GL_STENCIL_TEST_TWO_SIDE_EXT
2037 qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
2038 qglActiveStencilFaceEXT(GL_FRONT);CHECKGLERROR
2039 qglStencilMask(writemask);CHECKGLERROR
2040 qglStencilOp(frontfail, frontzfail, frontzpass);CHECKGLERROR
2041 qglStencilFunc(frontcompare, comparereference, comparemask);CHECKGLERROR
2042 qglActiveStencilFaceEXT(GL_BACK);CHECKGLERROR
2043 qglStencilMask(writemask);CHECKGLERROR
2044 qglStencilOp(backfail, backzfail, backzpass);CHECKGLERROR
2045 qglStencilFunc(backcompare, comparereference, comparemask);CHECKGLERROR
2049 case RENDERPATH_D3D9:
2051 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_TWOSIDEDSTENCILMODE, true);
2052 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILENABLE, enable);
2053 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILWRITEMASK, writemask);
2054 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFAIL, d3dstencilopforglfunc(frontfail));
2055 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILZFAIL, d3dstencilopforglfunc(frontzfail));
2056 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILPASS, d3dstencilopforglfunc(frontzpass));
2057 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFUNC, d3dcmpforglfunc(frontcompare));
2058 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILFAIL, d3dstencilopforglfunc(backfail));
2059 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILZFAIL, d3dstencilopforglfunc(backzfail));
2060 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILPASS, d3dstencilopforglfunc(backzpass));
2061 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILFUNC, d3dcmpforglfunc(backcompare));
2062 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILREF, comparereference);
2063 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILMASK, comparemask);
2066 case RENDERPATH_D3D10:
2067 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2069 case RENDERPATH_D3D11:
2070 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2072 case RENDERPATH_SOFT:
2073 //Con_DPrintf("FIXME SOFT %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2078 void R_SetStencil(qboolean enable, int writemask, int fail, int zfail, int zpass, int compare, int comparereference, int comparemask)
2080 switch (vid.renderpath)
2082 case RENDERPATH_GL11:
2083 case RENDERPATH_GL13:
2084 case RENDERPATH_GL20:
2085 case RENDERPATH_GLES1:
2086 case RENDERPATH_GLES2:
2090 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
2094 qglDisable(GL_STENCIL_TEST);CHECKGLERROR
2096 if (vid.support.ext_stencil_two_side)
2098 #ifdef GL_STENCIL_TEST_TWO_SIDE_EXT
2099 qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
2102 qglStencilMask(writemask);CHECKGLERROR
2103 qglStencilOp(fail, zfail, zpass);CHECKGLERROR
2104 qglStencilFunc(compare, comparereference, comparemask);CHECKGLERROR
2107 case RENDERPATH_D3D9:
2109 if (vid.support.ati_separate_stencil)
2110 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_TWOSIDEDSTENCILMODE, true);
2111 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILENABLE, enable);
2112 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILWRITEMASK, writemask);
2113 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFAIL, d3dstencilopforglfunc(fail));
2114 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILZFAIL, d3dstencilopforglfunc(zfail));
2115 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILPASS, d3dstencilopforglfunc(zpass));
2116 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFUNC, d3dcmpforglfunc(compare));
2117 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILREF, comparereference);
2118 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILMASK, comparemask);
2121 case RENDERPATH_D3D10:
2122 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2124 case RENDERPATH_D3D11:
2125 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2127 case RENDERPATH_SOFT:
2128 //Con_DPrintf("FIXME SOFT %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2133 void GL_PolygonOffset(float planeoffset, float depthoffset)
2135 if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
2137 gl_state.polygonoffset[0] = planeoffset;
2138 gl_state.polygonoffset[1] = depthoffset;
2139 switch(vid.renderpath)
2141 case RENDERPATH_GL11:
2142 case RENDERPATH_GL13:
2143 case RENDERPATH_GL20:
2144 case RENDERPATH_GLES1:
2145 case RENDERPATH_GLES2:
2146 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
2148 case RENDERPATH_D3D9:
2150 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SLOPESCALEDEPTHBIAS, gl_state.polygonoffset[0]);
2151 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DEPTHBIAS, gl_state.polygonoffset[1] * (1.0f / 16777216.0f));
2154 case RENDERPATH_D3D10:
2155 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2157 case RENDERPATH_D3D11:
2158 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2160 case RENDERPATH_SOFT:
2161 DPSOFTRAST_PolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
2167 void GL_SetMirrorState(qboolean state)
2169 if (v_flipped_state != state)
2171 v_flipped_state = state;
2172 if (gl_state.cullface == GL_BACK)
2173 gl_state.cullface = GL_FRONT;
2174 else if (gl_state.cullface == GL_FRONT)
2175 gl_state.cullface = GL_BACK;
2178 switch(vid.renderpath)
2180 case RENDERPATH_GL11:
2181 case RENDERPATH_GL13:
2182 case RENDERPATH_GL20:
2183 case RENDERPATH_GLES1:
2184 case RENDERPATH_GLES2:
2185 qglCullFace(gl_state.cullface);CHECKGLERROR
2187 case RENDERPATH_D3D9:
2189 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, gl_state.cullface == GL_FRONT ? D3DCULL_CCW : D3DCULL_CW);
2192 case RENDERPATH_D3D10:
2193 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2195 case RENDERPATH_D3D11:
2196 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2198 case RENDERPATH_SOFT:
2199 DPSOFTRAST_CullFace(gl_state.cullface);
2205 void GL_CullFace(int state)
2209 if(state == GL_FRONT)
2211 else if(state == GL_BACK)
2215 switch(vid.renderpath)
2217 case RENDERPATH_GL11:
2218 case RENDERPATH_GL13:
2219 case RENDERPATH_GL20:
2220 case RENDERPATH_GLES1:
2221 case RENDERPATH_GLES2:
2224 if (state != GL_NONE)
2226 if (!gl_state.cullfaceenable)
2228 gl_state.cullfaceenable = true;
2229 qglEnable(GL_CULL_FACE);CHECKGLERROR
2231 if (gl_state.cullface != state)
2233 gl_state.cullface = state;
2234 qglCullFace(gl_state.cullface);CHECKGLERROR
2239 if (gl_state.cullfaceenable)
2241 gl_state.cullfaceenable = false;
2242 qglDisable(GL_CULL_FACE);CHECKGLERROR
2246 case RENDERPATH_D3D9:
2248 if (gl_state.cullface != state)
2250 gl_state.cullface = state;
2251 switch(gl_state.cullface)
2254 gl_state.cullfaceenable = false;
2255 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_NONE);
2258 gl_state.cullfaceenable = true;
2259 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_CCW);
2262 gl_state.cullfaceenable = true;
2263 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_CW);
2269 case RENDERPATH_D3D10:
2270 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2272 case RENDERPATH_D3D11:
2273 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2275 case RENDERPATH_SOFT:
2276 if (gl_state.cullface != state)
2278 gl_state.cullface = state;
2279 gl_state.cullfaceenable = state != GL_NONE ? true : false;
2280 DPSOFTRAST_CullFace(gl_state.cullface);
2286 void GL_AlphaTest(int state)
2288 if (gl_state.alphatest != state)
2290 gl_state.alphatest = state;
2291 switch(vid.renderpath)
2293 case RENDERPATH_GL11:
2294 case RENDERPATH_GL13:
2295 case RENDERPATH_GLES1:
2296 #ifdef GL_ALPHA_TEST
2297 // only fixed function uses alpha test, other paths use pixel kill capability in shaders
2299 if (gl_state.alphatest)
2301 qglEnable(GL_ALPHA_TEST);CHECKGLERROR
2305 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
2309 case RENDERPATH_D3D9:
2310 case RENDERPATH_D3D10:
2311 case RENDERPATH_D3D11:
2312 case RENDERPATH_SOFT:
2313 case RENDERPATH_GL20:
2314 case RENDERPATH_GLES2:
2320 void GL_AlphaToCoverage(qboolean state)
2322 if (gl_state.alphatocoverage != state)
2324 gl_state.alphatocoverage = state;
2325 switch(vid.renderpath)
2327 case RENDERPATH_GL11:
2328 case RENDERPATH_GL13:
2329 case RENDERPATH_GLES1:
2330 case RENDERPATH_GLES2:
2331 case RENDERPATH_D3D9:
2332 case RENDERPATH_D3D10:
2333 case RENDERPATH_D3D11:
2334 case RENDERPATH_SOFT:
2336 case RENDERPATH_GL20:
2337 #ifdef GL_SAMPLE_ALPHA_TO_COVERAGE_ARB
2338 // alpha to coverage turns the alpha value of the pixel into 0%, 25%, 50%, 75% or 100% by masking the multisample fragments accordingly
2340 if (gl_state.alphatocoverage)
2342 qglEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);CHECKGLERROR
2343 // qglEnable(GL_MULTISAMPLE_ARB);CHECKGLERROR
2347 qglDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);CHECKGLERROR
2348 // qglDisable(GL_MULTISAMPLE_ARB);CHECKGLERROR
2356 void GL_ColorMask(int r, int g, int b, int a)
2358 // NOTE: this matches D3DCOLORWRITEENABLE_RED, GREEN, BLUE, ALPHA
2359 int state = (r ? 1 : 0) | (g ? 2 : 0) | (b ? 4 : 0) | (a ? 8 : 0);
2360 if (gl_state.colormask != state)
2362 gl_state.colormask = state;
2363 switch(vid.renderpath)
2365 case RENDERPATH_GL11:
2366 case RENDERPATH_GL13:
2367 case RENDERPATH_GL20:
2368 case RENDERPATH_GLES1:
2369 case RENDERPATH_GLES2:
2371 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
2373 case RENDERPATH_D3D9:
2375 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_COLORWRITEENABLE, state);
2378 case RENDERPATH_D3D10:
2379 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2381 case RENDERPATH_D3D11:
2382 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2384 case RENDERPATH_SOFT:
2385 DPSOFTRAST_ColorMask(r, g, b, a);
2391 void GL_Color(float cr, float cg, float cb, float ca)
2393 if (gl_state.pointer_color_enabled || gl_state.color4f[0] != cr || gl_state.color4f[1] != cg || gl_state.color4f[2] != cb || gl_state.color4f[3] != ca)
2395 gl_state.color4f[0] = cr;
2396 gl_state.color4f[1] = cg;
2397 gl_state.color4f[2] = cb;
2398 gl_state.color4f[3] = ca;
2399 switch(vid.renderpath)
2401 case RENDERPATH_GL11:
2402 case RENDERPATH_GL13:
2403 case RENDERPATH_GLES1:
2405 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
2408 case RENDERPATH_D3D9:
2409 case RENDERPATH_D3D10:
2410 case RENDERPATH_D3D11:
2411 // no equivalent in D3D
2413 case RENDERPATH_SOFT:
2414 DPSOFTRAST_Color4f(cr, cg, cb, ca);
2416 case RENDERPATH_GL20:
2417 case RENDERPATH_GLES2:
2418 qglVertexAttrib4f(GLSLATTRIB_COLOR, cr, cg, cb, ca);
2424 void GL_Scissor (int x, int y, int width, int height)
2426 switch(vid.renderpath)
2428 case RENDERPATH_GL11:
2429 case RENDERPATH_GL13:
2430 case RENDERPATH_GL20:
2431 case RENDERPATH_GLES1:
2432 case RENDERPATH_GLES2:
2434 qglScissor(x, y,width,height);
2437 case RENDERPATH_D3D9:
2443 d3drect.right = x + width;
2444 d3drect.bottom = y + height;
2445 IDirect3DDevice9_SetScissorRect(vid_d3d9dev, &d3drect);
2449 case RENDERPATH_D3D10:
2450 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2452 case RENDERPATH_D3D11:
2453 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2455 case RENDERPATH_SOFT:
2456 DPSOFTRAST_Scissor(x, y, width, height);
2461 void GL_ScissorTest(int state)
2463 if (gl_state.scissortest != state)
2465 gl_state.scissortest = state;
2466 switch(vid.renderpath)
2468 case RENDERPATH_GL11:
2469 case RENDERPATH_GL13:
2470 case RENDERPATH_GL20:
2471 case RENDERPATH_GLES1:
2472 case RENDERPATH_GLES2:
2474 if(gl_state.scissortest)
2475 qglEnable(GL_SCISSOR_TEST);
2477 qglDisable(GL_SCISSOR_TEST);
2480 case RENDERPATH_D3D9:
2482 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SCISSORTESTENABLE, gl_state.scissortest);
2485 case RENDERPATH_D3D10:
2486 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2488 case RENDERPATH_D3D11:
2489 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2491 case RENDERPATH_SOFT:
2492 DPSOFTRAST_ScissorTest(gl_state.scissortest);
2498 void GL_Clear(int mask, const float *colorvalue, float depthvalue, int stencilvalue)
2500 static const float blackcolor[4] = {0, 0, 0, 0};
2501 // prevent warnings when trying to clear a buffer that does not exist
2503 colorvalue = blackcolor;
2506 mask &= ~GL_STENCIL_BUFFER_BIT;
2509 switch(vid.renderpath)
2511 case RENDERPATH_GL11:
2512 case RENDERPATH_GL13:
2513 case RENDERPATH_GL20:
2514 case RENDERPATH_GLES1:
2515 case RENDERPATH_GLES2:
2517 if (mask & GL_COLOR_BUFFER_BIT)
2519 qglClearColor(colorvalue[0], colorvalue[1], colorvalue[2], colorvalue[3]);CHECKGLERROR
2521 if (mask & GL_DEPTH_BUFFER_BIT)
2524 qglClearDepthf(depthvalue);CHECKGLERROR
2526 qglClearDepth(depthvalue);CHECKGLERROR
2529 if (mask & GL_STENCIL_BUFFER_BIT)
2531 qglClearStencil(stencilvalue);CHECKGLERROR
2533 qglClear(mask);CHECKGLERROR
2535 case RENDERPATH_D3D9:
2537 IDirect3DDevice9_Clear(vid_d3d9dev, 0, NULL, ((mask & GL_COLOR_BUFFER_BIT) ? D3DCLEAR_TARGET : 0) | ((mask & GL_STENCIL_BUFFER_BIT) ? D3DCLEAR_STENCIL : 0) | ((mask & GL_DEPTH_BUFFER_BIT) ? D3DCLEAR_ZBUFFER : 0), D3DCOLOR_COLORVALUE(colorvalue[0], colorvalue[1], colorvalue[2], colorvalue[3]), depthvalue, stencilvalue);
2540 case RENDERPATH_D3D10:
2541 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2543 case RENDERPATH_D3D11:
2544 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2546 case RENDERPATH_SOFT:
2547 if (mask & GL_COLOR_BUFFER_BIT)
2548 DPSOFTRAST_ClearColor(colorvalue[0], colorvalue[1], colorvalue[2], colorvalue[3]);
2549 if (mask & GL_DEPTH_BUFFER_BIT)
2550 DPSOFTRAST_ClearDepth(depthvalue);
2555 void GL_ReadPixelsBGRA(int x, int y, int width, int height, unsigned char *outpixels)
2557 switch(vid.renderpath)
2559 case RENDERPATH_GL11:
2560 case RENDERPATH_GL13:
2561 case RENDERPATH_GL20:
2562 case RENDERPATH_GLES1:
2563 case RENDERPATH_GLES2:
2565 qglReadPixels(x, y, width, height, GL_BGRA, GL_UNSIGNED_BYTE, outpixels);CHECKGLERROR
2567 case RENDERPATH_D3D9:
2570 // LordHavoc: we can't directly download the backbuffer because it may be
2571 // multisampled, and it may not be lockable, so we blit it to a lockable
2572 // surface of the same dimensions (but without multisample) to resolve the
2573 // multisample buffer to a normal image, and then lock that...
2574 IDirect3DSurface9 *stretchsurface = NULL;
2575 if (!FAILED(IDirect3DDevice9_CreateRenderTarget(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, TRUE, &stretchsurface, NULL)))
2577 D3DLOCKED_RECT lockedrect;
2578 if (!FAILED(IDirect3DDevice9_StretchRect(vid_d3d9dev, gl_state.d3drt_backbuffercolorsurface, NULL, stretchsurface, NULL, D3DTEXF_POINT)))
2580 if (!FAILED(IDirect3DSurface9_LockRect(stretchsurface, &lockedrect, NULL, D3DLOCK_READONLY)))
2583 unsigned char *row = (unsigned char *)lockedrect.pBits + x * 4 + lockedrect.Pitch * (vid.height - 1 - y);
2584 for (line = 0;line < height;line++, row -= lockedrect.Pitch)
2585 memcpy(outpixels + line * width * 4, row, width * 4);
2586 IDirect3DSurface9_UnlockRect(stretchsurface);
2589 IDirect3DSurface9_Release(stretchsurface);
2592 //IDirect3DSurface9 *syssurface = NULL;
2593 //if (!FAILED(IDirect3DDevice9_CreateRenderTarget(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, FALSE, &stretchsurface, NULL)))
2594 //if (!FAILED(IDirect3DDevice9_CreateOffscreenPlainSurface(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &syssurface, NULL)))
2595 //IDirect3DDevice9_GetRenderTargetData(vid_d3d9dev, gl_state.d3drt_backbuffercolorsurface, syssurface);
2596 //if (!FAILED(IDirect3DDevice9_GetFrontBufferData(vid_d3d9dev, 0, syssurface)))
2597 //if (!FAILED(IDirect3DSurface9_LockRect(syssurface, &lockedrect, NULL, D3DLOCK_READONLY)))
2598 //IDirect3DSurface9_UnlockRect(syssurface);
2599 //IDirect3DSurface9_Release(syssurface);
2603 case RENDERPATH_D3D10:
2604 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2606 case RENDERPATH_D3D11:
2607 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2609 case RENDERPATH_SOFT:
2610 DPSOFTRAST_GetPixelsBGRA(x, y, width, height, outpixels);
2615 // called at beginning of frame
2616 void R_Mesh_Start(void)
2619 R_Mesh_SetRenderTargets(0, NULL, NULL, NULL, NULL, NULL);
2621 if (gl_printcheckerror.integer && !gl_paranoid.integer)
2623 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
2624 Cvar_SetValueQuick(&gl_paranoid, 1);
2628 static qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
2632 char compilelog[MAX_INPUTLINE];
2633 shaderobject = qglCreateShader(shadertypeenum);CHECKGLERROR
2636 qglShaderSource(shaderobject, numstrings, strings, NULL);CHECKGLERROR
2637 qglCompileShader(shaderobject);CHECKGLERROR
2638 qglGetShaderiv(shaderobject, GL_COMPILE_STATUS, &shadercompiled);CHECKGLERROR
2639 qglGetShaderInfoLog(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
2640 if (compilelog[0] && ((strstr(compilelog, "error") || strstr(compilelog, "ERROR") || strstr(compilelog, "Error")) || ((strstr(compilelog, "WARNING") || strstr(compilelog, "warning") || strstr(compilelog, "Warning")) && developer.integer) || developer_extra.integer))
2642 int i, j, pretextlines = 0;
2643 for (i = 0;i < numstrings - 1;i++)
2644 for (j = 0;strings[i][j];j++)
2645 if (strings[i][j] == '\n')
2647 Con_Printf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
2649 if (!shadercompiled)
2651 qglDeleteShader(shaderobject);CHECKGLERROR
2654 qglAttachShader(programobject, shaderobject);CHECKGLERROR
2655 qglDeleteShader(shaderobject);CHECKGLERROR
2659 unsigned int GL_Backend_CompileProgram(int vertexstrings_count, const char **vertexstrings_list, int geometrystrings_count, const char **geometrystrings_list, int fragmentstrings_count, const char **fragmentstrings_list)
2661 GLint programlinked;
2662 GLuint programobject = 0;
2663 char linklog[MAX_INPUTLINE];
2666 programobject = qglCreateProgram();CHECKGLERROR
2670 qglBindAttribLocation(programobject, GLSLATTRIB_POSITION , "Attrib_Position" );
2671 qglBindAttribLocation(programobject, GLSLATTRIB_COLOR , "Attrib_Color" );
2672 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD0, "Attrib_TexCoord0");
2673 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD1, "Attrib_TexCoord1");
2674 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD2, "Attrib_TexCoord2");
2675 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD3, "Attrib_TexCoord3");
2676 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD4, "Attrib_TexCoord4");
2677 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD5, "Attrib_TexCoord5");
2678 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD6, "Attrib_SkeletalIndex");
2679 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD7, "Attrib_SkeletalWeight");
2681 if(vid.support.gl20shaders130)
2682 qglBindFragDataLocation(programobject, 0, "dp_FragColor");
2685 if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER, "vertex", vertexstrings_count, vertexstrings_list))
2688 #ifdef GL_GEOMETRY_SHADER
2689 if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER, "geometry", geometrystrings_count, geometrystrings_list))
2693 if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER, "fragment", fragmentstrings_count, fragmentstrings_list))
2696 qglLinkProgram(programobject);CHECKGLERROR
2697 qglGetProgramiv(programobject, GL_LINK_STATUS, &programlinked);CHECKGLERROR
2698 qglGetProgramInfoLog(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
2701 if (strstr(linklog, "error") || strstr(linklog, "ERROR") || strstr(linklog, "Error") || strstr(linklog, "WARNING") || strstr(linklog, "warning") || strstr(linklog, "Warning") || developer_extra.integer)
2702 Con_DPrintf("program link log:\n%s\n", linklog);
2703 // software vertex shader is ok but software fragment shader is WAY
2704 // too slow, fail program if so.
2705 // NOTE: this string might be ATI specific, but that's ok because the
2706 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
2707 // software fragment shader due to low instruction and dependent
2709 if (strstr(linklog, "fragment shader will run in software"))
2710 programlinked = false;
2714 return programobject;
2716 qglDeleteProgram(programobject);CHECKGLERROR
2720 void GL_Backend_FreeProgram(unsigned int prog)
2723 qglDeleteProgram(prog);
2727 // renders triangles using vertices from the active arrays
2728 int paranoidblah = 0;
2729 void R_Mesh_Draw(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const r_meshbuffer_t *element3i_indexbuffer, size_t element3i_bufferoffset, const unsigned short *element3s, const r_meshbuffer_t *element3s_indexbuffer, size_t element3s_bufferoffset)
2731 unsigned int numelements = numtriangles * 3;
2733 size_t bufferoffset3i;
2735 size_t bufferoffset3s;
2736 if (numvertices < 3 || numtriangles < 1)
2738 if (numvertices < 0 || numtriangles < 0 || developer_extra.integer)
2739 Con_DPrintf("R_Mesh_Draw(%d, %d, %d, %d, %8p, %8p, %8x, %8p, %8p, %8x);\n", firstvertex, numvertices, firsttriangle, numtriangles, (void *)element3i, (void *)element3i_indexbuffer, (int)element3i_bufferoffset, (void *)element3s, (void *)element3s_indexbuffer, (int)element3s_bufferoffset);
2742 if (!gl_mesh_prefer_short_elements.integer)
2746 if (element3i_indexbuffer)
2747 element3i_indexbuffer = NULL;
2749 // adjust the pointers for firsttriangle
2751 element3i += firsttriangle * 3;
2752 if (element3i_indexbuffer)
2753 element3i_bufferoffset += firsttriangle * 3 * sizeof(*element3i);
2755 element3s += firsttriangle * 3;
2756 if (element3s_indexbuffer)
2757 element3s_bufferoffset += firsttriangle * 3 * sizeof(*element3s);
2758 switch(vid.renderpath)
2760 case RENDERPATH_GL11:
2761 case RENDERPATH_GL13:
2762 case RENDERPATH_GL20:
2763 case RENDERPATH_GLES1:
2764 case RENDERPATH_GLES2:
2765 // check if the user specified to ignore static index buffers
2766 if (!gl_state.usevbo_staticindex || (gl_vbo.integer == 3 && !vid.forcevbo && (element3i_bufferoffset || element3s_bufferoffset)))
2768 element3i_indexbuffer = NULL;
2769 element3s_indexbuffer = NULL;
2772 case RENDERPATH_D3D9:
2773 case RENDERPATH_D3D10:
2774 case RENDERPATH_D3D11:
2776 case RENDERPATH_SOFT:
2779 // upload a dynamic index buffer if needed
2782 if (!element3s_indexbuffer && gl_state.usevbo_dynamicindex)
2784 if (gl_state.draw_dynamicindexbuffer)
2785 R_Mesh_UpdateMeshBuffer(gl_state.draw_dynamicindexbuffer, (void *)element3s, numelements * sizeof(*element3s));
2787 gl_state.draw_dynamicindexbuffer = R_Mesh_CreateMeshBuffer((void *)element3s, numelements * sizeof(*element3s), "temporary", true, true, true);
2788 element3s_indexbuffer = gl_state.draw_dynamicindexbuffer;
2789 element3s_bufferoffset = 0;
2794 if (!element3i_indexbuffer && gl_state.usevbo_dynamicindex)
2796 if (gl_state.draw_dynamicindexbuffer)
2797 R_Mesh_UpdateMeshBuffer(gl_state.draw_dynamicindexbuffer, (void *)element3i, numelements * sizeof(*element3i));
2799 gl_state.draw_dynamicindexbuffer = R_Mesh_CreateMeshBuffer((void *)element3i, numelements * sizeof(*element3i), "temporary", true, true, false);
2800 element3i_indexbuffer = gl_state.draw_dynamicindexbuffer;
2801 element3i_bufferoffset = 0;
2804 bufferobject3i = element3i_indexbuffer ? element3i_indexbuffer->bufferobject : 0;
2805 bufferoffset3i = element3i_bufferoffset;
2806 bufferobject3s = element3s_indexbuffer ? element3s_indexbuffer->bufferobject : 0;
2807 bufferoffset3s = element3s_bufferoffset;
2808 r_refdef.stats.draws++;
2809 r_refdef.stats.draws_vertices += numvertices;
2810 r_refdef.stats.draws_elements += numelements;
2811 if (gl_paranoid.integer)
2814 // LordHavoc: disabled this - it needs to be updated to handle components and gltype and stride in each array
2816 unsigned int j, size;
2818 // note: there's no validation done here on buffer objects because it
2819 // is somewhat difficult to get at the data, and gl_paranoid can be
2820 // used without buffer objects if the need arises
2821 // (the data could be gotten using glMapBuffer but it would be very
2822 // slow due to uncachable video memory reads)
2823 if (!qglIsEnabled(GL_VERTEX_ARRAY))
2824 Con_Print("R_Mesh_Draw: vertex array not enabled\n");
2826 if (gl_state.pointer_vertex_pointer)
2827 for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
2829 if (gl_state.pointer_color_enabled)
2831 if (!qglIsEnabled(GL_COLOR_ARRAY))
2832 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
2834 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
2835 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
2838 for (i = 0;i < vid.texarrayunits;i++)
2840 if (gl_state.units[i].arrayenabled)
2842 GL_ClientActiveTexture(i);
2843 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
2844 Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
2846 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
2847 for (j = 0, size = numvertices * gl_state.units[i].arraycomponents, p = (int *)((float *)gl_state.units[i].pointer_texcoord + firstvertex * gl_state.units[i].arraycomponents);j < size;j++, p++)
2854 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2856 if (element3i[i] < firstvertex || element3i[i] >= firstvertex + numvertices)
2858 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3i array\n", element3i[i], firstvertex, firstvertex + numvertices);
2865 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2867 if (element3s[i] < firstvertex || element3s[i] >= firstvertex + numvertices)
2869 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3s array\n", element3s[i], firstvertex, firstvertex + numvertices);
2875 if (r_render.integer || r_refdef.draw2dstage)
2877 switch(vid.renderpath)
2879 case RENDERPATH_GL11:
2880 case RENDERPATH_GL13:
2881 case RENDERPATH_GL20:
2883 if (gl_mesh_testmanualfeeding.integer)
2886 unsigned int i, j, element;
2888 qglBegin(GL_TRIANGLES);
2889 if(vid.renderpath == RENDERPATH_GL20)
2891 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2894 element = element3i[i];
2896 element = element3s[i];
2898 element = firstvertex + i;
2899 for (j = 0;j < vid.texarrayunits;j++)
2901 if (gl_state.units[j].pointer_texcoord_pointer && gl_state.units[j].arrayenabled)
2903 if (gl_state.units[j].pointer_texcoord_gltype == GL_FLOAT)
2905 p = (const GLfloat *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2906 if (gl_state.units[j].pointer_texcoord_components == 4)
2907 qglVertexAttrib4f(GLSLATTRIB_TEXCOORD0 + j, p[0], p[1], p[2], p[3]);
2908 else if (gl_state.units[j].pointer_texcoord_components == 3)
2909 qglVertexAttrib3f(GLSLATTRIB_TEXCOORD0 + j, p[0], p[1], p[2]);
2910 else if (gl_state.units[j].pointer_texcoord_components == 2)
2911 qglVertexAttrib2f(GLSLATTRIB_TEXCOORD0 + j, p[0], p[1]);
2913 qglVertexAttrib1f(GLSLATTRIB_TEXCOORD0 + j, p[0]);
2915 else if (gl_state.units[j].pointer_texcoord_gltype == (int)(GL_SHORT | 0x80000000))
2917 const GLshort *s = (const GLshort *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2918 if (gl_state.units[j].pointer_texcoord_components == 4)
2919 qglVertexAttrib4f(GLSLATTRIB_TEXCOORD0 + j, s[0], s[1], s[2], s[3]);
2920 else if (gl_state.units[j].pointer_texcoord_components == 3)
2921 qglVertexAttrib3f(GLSLATTRIB_TEXCOORD0 + j, s[0], s[1], s[2]);
2922 else if (gl_state.units[j].pointer_texcoord_components == 2)
2923 qglVertexAttrib2f(GLSLATTRIB_TEXCOORD0 + j, s[0], s[1]);
2924 else if (gl_state.units[j].pointer_texcoord_components == 1)
2925 qglVertexAttrib1f(GLSLATTRIB_TEXCOORD0 + j, s[0]);
2927 else if (gl_state.units[j].pointer_texcoord_gltype == GL_BYTE)
2929 const GLbyte *sb = (const GLbyte *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2930 if (gl_state.units[j].pointer_texcoord_components == 4)
2931 qglVertexAttrib4f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 127.0f), sb[1] * (1.0f / 127.0f), sb[2] * (1.0f / 127.0f), sb[3] * (1.0f / 127.0f));
2932 else if (gl_state.units[j].pointer_texcoord_components == 3)
2933 qglVertexAttrib3f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 127.0f), sb[1] * (1.0f / 127.0f), sb[2] * (1.0f / 127.0f));
2934 else if (gl_state.units[j].pointer_texcoord_components == 2)
2935 qglVertexAttrib2f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 127.0f), sb[1] * (1.0f / 127.0f));
2936 else if (gl_state.units[j].pointer_texcoord_components == 1)
2937 qglVertexAttrib1f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 127.0f));
2939 else if (gl_state.units[j].pointer_texcoord_gltype == GL_UNSIGNED_BYTE)
2941 const GLubyte *sb = (const GLubyte *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2942 if (gl_state.units[j].pointer_texcoord_components == 4)
2943 qglVertexAttrib4f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 255.0f), sb[1] * (1.0f / 255.0f), sb[2] * (1.0f / 255.0f), sb[3] * (1.0f / 255.0f));
2944 else if (gl_state.units[j].pointer_texcoord_components == 3)
2945 qglVertexAttrib3f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 255.0f), sb[1] * (1.0f / 255.0f), sb[2] * (1.0f / 255.0f));
2946 else if (gl_state.units[j].pointer_texcoord_components == 2)
2947 qglVertexAttrib2f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 255.0f), sb[1] * (1.0f / 255.0f));
2948 else if (gl_state.units[j].pointer_texcoord_components == 1)
2949 qglVertexAttrib1f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 255.0f));
2951 else if (gl_state.units[j].pointer_texcoord_gltype == (int)(GL_UNSIGNED_BYTE | 0x80000000))
2953 const GLubyte *sb = (const GLubyte *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2954 if (gl_state.units[j].pointer_texcoord_components == 4)
2955 qglVertexAttrib4f(GLSLATTRIB_TEXCOORD0 + j, sb[0], sb[1], sb[2], sb[3]);
2956 else if (gl_state.units[j].pointer_texcoord_components == 3)
2957 qglVertexAttrib3f(GLSLATTRIB_TEXCOORD0 + j, sb[0], sb[1], sb[2]);
2958 else if (gl_state.units[j].pointer_texcoord_components == 2)
2959 qglVertexAttrib2f(GLSLATTRIB_TEXCOORD0 + j, sb[0], sb[1]);
2960 else if (gl_state.units[j].pointer_texcoord_components == 1)
2961 qglVertexAttrib1f(GLSLATTRIB_TEXCOORD0 + j, sb[0]);
2965 if (gl_state.pointer_color_pointer && gl_state.pointer_color_enabled && gl_state.pointer_color_components == 4)
2967 if (gl_state.pointer_color_gltype == GL_FLOAT)
2969 p = (const GLfloat *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
2970 qglVertexAttrib4f(GLSLATTRIB_COLOR, p[0], p[1], p[2], p[3]);
2972 else if (gl_state.pointer_color_gltype == GL_UNSIGNED_BYTE)
2974 const GLubyte *ub = (const GLubyte *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
2975 qglVertexAttrib4Nub(GLSLATTRIB_COLOR, ub[0], ub[1], ub[2], ub[3]);
2978 if (gl_state.pointer_vertex_gltype == GL_FLOAT)
2980 p = (const GLfloat *)((const unsigned char *)gl_state.pointer_vertex_pointer + element * gl_state.pointer_vertex_stride);
2981 if (gl_state.pointer_vertex_components == 4)
2982 qglVertexAttrib4f(GLSLATTRIB_POSITION, p[0], p[1], p[2], p[3]);
2983 else if (gl_state.pointer_vertex_components == 3)
2984 qglVertexAttrib3f(GLSLATTRIB_POSITION, p[0], p[1], p[2]);
2986 qglVertexAttrib2f(GLSLATTRIB_POSITION, p[0], p[1]);
2992 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2995 element = element3i[i];
2997 element = element3s[i];
2999 element = firstvertex + i;
3000 for (j = 0;j < vid.texarrayunits;j++)
3002 if (gl_state.units[j].pointer_texcoord_pointer && gl_state.units[j].arrayenabled)
3004 if (gl_state.units[j].pointer_texcoord_gltype == GL_FLOAT)
3006 p = (const GLfloat *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
3007 if (vid.texarrayunits > 1)
3009 if (gl_state.units[j].pointer_texcoord_components == 4)
3010 qglMultiTexCoord4f(GL_TEXTURE0 + j, p[0], p[1], p[2], p[3]);
3011 else if (gl_state.units[j].pointer_texcoord_components == 3)
3012 qglMultiTexCoord3f(GL_TEXTURE0 + j, p[0], p[1], p[2]);
3013 else if (gl_state.units[j].pointer_texcoord_components == 2)
3014 qglMultiTexCoord2f(GL_TEXTURE0 + j, p[0], p[1]);
3016 qglMultiTexCoord1f(GL_TEXTURE0 + j, p[0]);
3020 if (gl_state.units[j].pointer_texcoord_components == 4)
3021 qglTexCoord4f(p[0], p[1], p[2], p[3]);
3022 else if (gl_state.units[j].pointer_texcoord_components == 3)
3023 qglTexCoord3f(p[0], p[1], p[2]);
3024 else if (gl_state.units[j].pointer_texcoord_components == 2)
3025 qglTexCoord2f(p[0], p[1]);
3027 qglTexCoord1f(p[0]);
3030 else if (gl_state.units[j].pointer_texcoord_gltype == GL_SHORT)
3032 const GLshort *s = (const GLshort *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
3033 if (vid.texarrayunits > 1)
3035 if (gl_state.units[j].pointer_texcoord_components == 4)
3036 qglMultiTexCoord4f(GL_TEXTURE0 + j, s[0], s[1], s[2], s[3]);
3037 else if (gl_state.units[j].pointer_texcoord_components == 3)
3038 qglMultiTexCoord3f(GL_TEXTURE0 + j, s[0], s[1], s[2]);
3039 else if (gl_state.units[j].pointer_texcoord_components == 2)
3040 qglMultiTexCoord2f(GL_TEXTURE0 + j, s[0], s[1]);
3041 else if (gl_state.units[j].pointer_texcoord_components == 1)
3042 qglMultiTexCoord1f(GL_TEXTURE0 + j, s[0]);
3046 if (gl_state.units[j].pointer_texcoord_components == 4)
3047 qglTexCoord4f(s[0], s[1], s[2], s[3]);
3048 else if (gl_state.units[j].pointer_texcoord_components == 3)
3049 qglTexCoord3f(s[0], s[1], s[2]);
3050 else if (gl_state.units[j].pointer_texcoord_components == 2)
3051 qglTexCoord2f(s[0], s[1]);
3052 else if (gl_state.units[j].pointer_texcoord_components == 1)
3053 qglTexCoord1f(s[0]);
3056 else if (gl_state.units[j].pointer_texcoord_gltype == GL_BYTE)
3058 const GLbyte *sb = (const GLbyte *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
3059 if (vid.texarrayunits > 1)
3061 if (gl_state.units[j].pointer_texcoord_components == 4)
3062 qglMultiTexCoord4f(GL_TEXTURE0 + j, sb[0], sb[1], sb[2], sb[3]);
3063 else if (gl_state.units[j].pointer_texcoord_components == 3)
3064 qglMultiTexCoord3f(GL_TEXTURE0 + j, sb[0], sb[1], sb[2]);
3065 else if (gl_state.units[j].pointer_texcoord_components == 2)
3066 qglMultiTexCoord2f(GL_TEXTURE0 + j, sb[0], sb[1]);
3067 else if (gl_state.units[j].pointer_texcoord_components == 1)
3068 qglMultiTexCoord1f(GL_TEXTURE0 + j, sb[0]);
3072 if (gl_state.units[j].pointer_texcoord_components == 4)
3073 qglTexCoord4f(sb[0], sb[1], sb[2], sb[3]);
3074 else if (gl_state.units[j].pointer_texcoord_components == 3)
3075 qglTexCoord3f(sb[0], sb[1], sb[2]);
3076 else if (gl_state.units[j].pointer_texcoord_components == 2)
3077 qglTexCoord2f(sb[0], sb[1]);
3078 else if (gl_state.units[j].pointer_texcoord_components == 1)
3079 qglTexCoord1f(sb[0]);
3084 if (gl_state.pointer_color_pointer && gl_state.pointer_color_enabled && gl_state.pointer_color_components == 4)
3086 if (gl_state.pointer_color_gltype == GL_FLOAT)
3088 p = (const GLfloat *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
3089 qglColor4f(p[0], p[1], p[2], p[3]);
3091 else if (gl_state.pointer_color_gltype == GL_UNSIGNED_BYTE)
3093 const GLubyte *ub = (const GLubyte *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
3094 qglColor4ub(ub[0], ub[1], ub[2], ub[3]);
3097 if (gl_state.pointer_vertex_gltype == GL_FLOAT)
3099 p = (const GLfloat *)((const unsigned char *)gl_state.pointer_vertex_pointer + element * gl_state.pointer_vertex_stride);
3100 if (gl_state.pointer_vertex_components == 4)
3101 qglVertex4f(p[0], p[1], p[2], p[3]);
3102 else if (gl_state.pointer_vertex_components == 3)
3103 qglVertex3f(p[0], p[1], p[2]);
3105 qglVertex2f(p[0], p[1]);
3113 else if (bufferobject3s)
3115 GL_BindEBO(bufferobject3s);
3117 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
3119 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, (void *)bufferoffset3s);
3125 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
3129 else if (bufferobject3i)
3131 GL_BindEBO(bufferobject3i);
3133 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
3135 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, (void *)bufferoffset3i);
3141 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
3149 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
3151 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, element3s);
3157 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
3165 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
3167 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, element3i);
3173 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
3179 qglDrawArrays(GL_TRIANGLES, firstvertex, numvertices);
3183 case RENDERPATH_D3D9:
3185 if (gl_state.d3dvertexbuffer && ((element3s && element3s_indexbuffer) || (element3i && element3i_indexbuffer)))
3187 if (element3s_indexbuffer)
3189 IDirect3DDevice9_SetIndices(vid_d3d9dev, (IDirect3DIndexBuffer9 *)element3s_indexbuffer->devicebuffer);
3190 IDirect3DDevice9_DrawIndexedPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, 0, firstvertex, numvertices, element3s_bufferoffset>>1, numtriangles);
3192 else if (element3i_indexbuffer)
3194 IDirect3DDevice9_SetIndices(vid_d3d9dev, (IDirect3DIndexBuffer9 *)element3i_indexbuffer->devicebuffer);
3195 IDirect3DDevice9_DrawIndexedPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, 0, firstvertex, numvertices, element3i_bufferoffset>>2, numtriangles);
3198 IDirect3DDevice9_DrawPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, firstvertex, numvertices);
3203 IDirect3DDevice9_DrawIndexedPrimitiveUP(vid_d3d9dev, D3DPT_TRIANGLELIST, firstvertex, numvertices, numtriangles, element3s, D3DFMT_INDEX16, gl_state.d3dvertexdata, gl_state.d3dvertexsize);
3205 IDirect3DDevice9_DrawIndexedPrimitiveUP(vid_d3d9dev, D3DPT_TRIANGLELIST, firstvertex, numvertices, numtriangles, element3i, D3DFMT_INDEX32, gl_state.d3dvertexdata, gl_state.d3dvertexsize);
3207 IDirect3DDevice9_DrawPrimitiveUP(vid_d3d9dev, D3DPT_TRIANGLELIST, numvertices, (void *)gl_state.d3dvertexdata, gl_state.d3dvertexsize);
3211 case RENDERPATH_D3D10:
3212 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3214 case RENDERPATH_D3D11:
3215 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3217 case RENDERPATH_SOFT:
3218 DPSOFTRAST_DrawTriangles(firstvertex, numvertices, numtriangles, element3i, element3s);
3220 case RENDERPATH_GLES1:
3221 case RENDERPATH_GLES2:
3222 // GLES does not have glDrawRangeElements, and generally
3223 // underperforms with index buffers, so this code path is
3224 // relatively straightforward...
3226 if (gl_paranoid.integer)
3228 int r, prog, enabled, i;
3229 GLsizei attriblength;
3232 GLchar attribname[1024];
3233 r = qglCheckFramebufferStatusEXT(GL_FRAMEBUFFER);CHECKGLERROR
3234 if (r != GL_FRAMEBUFFER_COMPLETE)
3235 Con_DPrintf("fbo %i not complete (default %i)\n", gl_state.framebufferobject, gl_state.defaultframebufferobject);
3236 #ifndef GL_CURRENT_PROGRAM
3237 #define GL_CURRENT_PROGRAM 0x8B8D
3239 qglGetIntegerv(GL_CURRENT_PROGRAM, &r);CHECKGLERROR
3240 if (r < 0 || r > 10000)
3241 Con_DPrintf("GL_CURRENT_PROGRAM = %i\n", r);
3243 for (i = 0;i < 8;i++)
3245 qglGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &r);CHECKGLERROR
3248 qglGetActiveAttrib(prog, i, sizeof(attribname), &attriblength, &attribsize, &attribtype, attribname);CHECKGLERROR
3249 Con_DPrintf("prog %i position %i length %i size %04X type %i name \"%s\"\n", prog, i, (int)attriblength, (int)attribsize, (int)attribtype, (char *)attribname);
3255 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
3260 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
3265 qglDrawArrays(GL_TRIANGLES, firstvertex, numvertices);
3273 // restores backend state, used when done with 3D rendering
3274 void R_Mesh_Finish(void)
3276 R_Mesh_SetRenderTargets(0, NULL, NULL, NULL, NULL, NULL);
3279 r_meshbuffer_t *R_Mesh_CreateMeshBuffer(const void *data, size_t size, const char *name, qboolean isindexbuffer, qboolean isdynamic, qboolean isindex16)
3281 r_meshbuffer_t *buffer;
3282 if (!(isdynamic ? (isindexbuffer ? gl_state.usevbo_dynamicindex : gl_state.usevbo_dynamicvertex) : (isindexbuffer ? gl_state.usevbo_staticindex : gl_state.usevbo_staticvertex)))
3284 buffer = (r_meshbuffer_t *)Mem_ExpandableArray_AllocRecord(&gl_state.meshbufferarray);
3285 memset(buffer, 0, sizeof(*buffer));
3286 buffer->bufferobject = 0;
3287 buffer->devicebuffer = NULL;
3289 buffer->isindexbuffer = isindexbuffer;
3290 buffer->isdynamic = isdynamic;
3291 buffer->isindex16 = isindex16;
3292 strlcpy(buffer->name, name, sizeof(buffer->name));
3293 R_Mesh_UpdateMeshBuffer(buffer, data, size);
3297 void R_Mesh_UpdateMeshBuffer(r_meshbuffer_t *buffer, const void *data, size_t size)
3301 if (buffer->isindexbuffer)
3303 r_refdef.stats.indexbufferuploadcount++;
3304 r_refdef.stats.indexbufferuploadsize += size;
3308 r_refdef.stats.vertexbufferuploadcount++;
3309 r_refdef.stats.vertexbufferuploadsize += size;
3311 switch(vid.renderpath)
3313 case RENDERPATH_GL11:
3314 case RENDERPATH_GL13:
3315 case RENDERPATH_GL20:
3316 case RENDERPATH_GLES1:
3317 case RENDERPATH_GLES2:
3318 if (!buffer->bufferobject)
3319 qglGenBuffersARB(1, (GLuint *)&buffer->bufferobject);
3320 if (buffer->isindexbuffer)
3321 GL_BindEBO(buffer->bufferobject);
3323 GL_BindVBO(buffer->bufferobject);
3324 qglBufferDataARB(buffer->isindexbuffer ? GL_ELEMENT_ARRAY_BUFFER : GL_ARRAY_BUFFER, size, data, buffer->isdynamic ? GL_STREAM_DRAW : GL_STATIC_DRAW);
3326 case RENDERPATH_D3D9:
3330 void *datapointer = NULL;
3331 if (buffer->isindexbuffer)
3333 IDirect3DIndexBuffer9 *d3d9indexbuffer = (IDirect3DIndexBuffer9 *)buffer->devicebuffer;
3334 if (size > buffer->size || !buffer->devicebuffer)
3336 if (buffer->devicebuffer)
3337 IDirect3DIndexBuffer9_Release((IDirect3DIndexBuffer9*)buffer->devicebuffer);
3338 buffer->devicebuffer = NULL;
3339 if (FAILED(result = IDirect3DDevice9_CreateIndexBuffer(vid_d3d9dev, size, buffer->isdynamic ? D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC : 0, buffer->isindex16 ? D3DFMT_INDEX16 : D3DFMT_INDEX32, buffer->isdynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED, &d3d9indexbuffer, NULL)))
3340 Sys_Error("IDirect3DDevice9_CreateIndexBuffer(%p, %d, %x, %x, %x, %p, NULL) returned %x\n", vid_d3d9dev, (int)size, buffer->isdynamic ? (int)D3DUSAGE_DYNAMIC : 0, buffer->isindex16 ? (int)D3DFMT_INDEX16 : (int)D3DFMT_INDEX32, buffer->isdynamic ? (int)D3DPOOL_DEFAULT : (int)D3DPOOL_MANAGED, &d3d9indexbuffer, (int)result);
3341 buffer->devicebuffer = (void *)d3d9indexbuffer;
3342 buffer->size = size;
3344 if (!FAILED(IDirect3DIndexBuffer9_Lock(d3d9indexbuffer, 0, 0, &datapointer, buffer->isdynamic ? D3DLOCK_DISCARD : 0)))
3347 memcpy(datapointer, data, size);
3349 memset(datapointer, 0, size);
3350 IDirect3DIndexBuffer9_Unlock(d3d9indexbuffer);
3355 IDirect3DVertexBuffer9 *d3d9vertexbuffer = (IDirect3DVertexBuffer9 *)buffer->devicebuffer;
3356 if (size > buffer->size || !buffer->devicebuffer)
3358 if (buffer->devicebuffer)
3359 IDirect3DVertexBuffer9_Release((IDirect3DVertexBuffer9*)buffer->devicebuffer);
3360 buffer->devicebuffer = NULL;
3361 if (FAILED(result = IDirect3DDevice9_CreateVertexBuffer(vid_d3d9dev, size, buffer->isdynamic ? D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC : 0, 0, buffer->isdynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED, &d3d9vertexbuffer, NULL)))
3362 Sys_Error("IDirect3DDevice9_CreateVertexBuffer(%p, %d, %x, %x, %x, %p, NULL) returned %x\n", vid_d3d9dev, (int)size, buffer->isdynamic ? (int)D3DUSAGE_DYNAMIC : 0, 0, buffer->isdynamic ? (int)D3DPOOL_DEFAULT : (int)D3DPOOL_MANAGED, &d3d9vertexbuffer, (int)result);
3363 buffer->devicebuffer = (void *)d3d9vertexbuffer;
3364 buffer->size = size;
3366 if (!FAILED(IDirect3DVertexBuffer9_Lock(d3d9vertexbuffer, 0, 0, &datapointer, buffer->isdynamic ? D3DLOCK_DISCARD : 0)))
3369 memcpy(datapointer, data, size);
3371 memset(datapointer, 0, size);
3372 IDirect3DVertexBuffer9_Unlock(d3d9vertexbuffer);
3378 case RENDERPATH_D3D10:
3379 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3381 case RENDERPATH_D3D11:
3382 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3384 case RENDERPATH_SOFT:
3389 void R_Mesh_DestroyMeshBuffer(r_meshbuffer_t *buffer)
3393 switch(vid.renderpath)
3395 case RENDERPATH_GL11:
3396 case RENDERPATH_GL13:
3397 case RENDERPATH_GL20:
3398 case RENDERPATH_GLES1:
3399 case RENDERPATH_GLES2:
3400 qglDeleteBuffersARB(1, (GLuint *)&buffer->bufferobject);
3402 case RENDERPATH_D3D9:
3404 if (gl_state.d3dvertexbuffer == (void *)buffer)
3405 gl_state.d3dvertexbuffer = NULL;
3406 if (buffer->devicebuffer)
3408 if (buffer->isindexbuffer)
3409 IDirect3DIndexBuffer9_Release((IDirect3DIndexBuffer9 *)buffer->devicebuffer);
3411 IDirect3DVertexBuffer9_Release((IDirect3DVertexBuffer9 *)buffer->devicebuffer);
3412 buffer->devicebuffer = NULL;
3416 case RENDERPATH_D3D10:
3417 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3419 case RENDERPATH_D3D11:
3420 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3422 case RENDERPATH_SOFT:
3425 Mem_ExpandableArray_FreeRecord(&gl_state.meshbufferarray, (void *)buffer);
3428 void GL_Mesh_ListVBOs(qboolean printeach)
3431 size_t ebocount = 0, ebomemory = 0;
3432 size_t vbocount = 0, vbomemory = 0;
3433 r_meshbuffer_t *buffer;
3434 endindex = Mem_ExpandableArray_IndexRange(&gl_state.meshbufferarray);
3435 for (i = 0;i < endindex;i++)
3437 buffer = (r_meshbuffer_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.meshbufferarray, i);
3440 if (buffer->isindexbuffer) {ebocount++;ebomemory += buffer->size;if (printeach) Con_Printf("indexbuffer #%i %s = %i bytes%s\n", i, buffer->name, (int)buffer->size, buffer->isdynamic ? " (dynamic)" : " (static)");}
3441 else {vbocount++;vbomemory += buffer->size;if (printeach) Con_Printf("vertexbuffer #%i %s = %i bytes%s\n", i, buffer->name, (int)buffer->size, buffer->isdynamic ? " (dynamic)" : " (static)");}
3443 Con_Printf("vertex buffers: %i indexbuffers totalling %i bytes (%.3f MB), %i vertexbuffers totalling %i bytes (%.3f MB), combined %i bytes (%.3fMB)\n", (int)ebocount, (int)ebomemory, ebomemory / 1048576.0, (int)vbocount, (int)vbomemory, vbomemory / 1048576.0, (int)(ebomemory + vbomemory), (ebomemory + vbomemory) / 1048576.0);
3448 void R_Mesh_VertexPointer(int components, int gltype, size_t stride, const void *pointer, const r_meshbuffer_t *vertexbuffer, size_t bufferoffset)
3450 switch(vid.renderpath)
3452 case RENDERPATH_GL11:
3453 case RENDERPATH_GL13:
3454 case RENDERPATH_GLES1:
3455 if (gl_state.pointer_vertex_components != components || gl_state.pointer_vertex_gltype != gltype || gl_state.pointer_vertex_stride != stride || gl_state.pointer_vertex_pointer != pointer || gl_state.pointer_vertex_vertexbuffer != vertexbuffer || gl_state.pointer_vertex_offset != bufferoffset)
3457 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3458 gl_state.pointer_vertex_components = components;
3459 gl_state.pointer_vertex_gltype = gltype;
3460 gl_state.pointer_vertex_stride = stride;
3461 gl_state.pointer_vertex_pointer = pointer;
3462 gl_state.pointer_vertex_vertexbuffer = vertexbuffer;
3463 gl_state.pointer_vertex_offset = bufferoffset;
3465 GL_BindVBO(bufferobject);
3466 qglVertexPointer(components, gltype, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3469 case RENDERPATH_GL20:
3470 case RENDERPATH_GLES2:
3471 if (gl_state.pointer_vertex_components != components || gl_state.pointer_vertex_gltype != gltype || gl_state.pointer_vertex_stride != stride || gl_state.pointer_vertex_pointer != pointer || gl_state.pointer_vertex_vertexbuffer != vertexbuffer || gl_state.pointer_vertex_offset != bufferoffset)
3473 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3474 gl_state.pointer_vertex_components = components;
3475 gl_state.pointer_vertex_gltype = gltype;
3476 gl_state.pointer_vertex_stride = stride;
3477 gl_state.pointer_vertex_pointer = pointer;
3478 gl_state.pointer_vertex_vertexbuffer = vertexbuffer;
3479 gl_state.pointer_vertex_offset = bufferoffset;
3481 GL_BindVBO(bufferobject);
3482 // LordHavoc: special flag added to gltype for unnormalized types
3483 qglVertexAttribPointer(GLSLATTRIB_POSITION, components, gltype & ~0x80000000, (gltype & 0x80000000) == 0, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3486 case RENDERPATH_D3D9:
3487 case RENDERPATH_D3D10:
3488 case RENDERPATH_D3D11:
3489 case RENDERPATH_SOFT:
3494 void R_Mesh_ColorPointer(int components, int gltype, size_t stride, const void *pointer, const r_meshbuffer_t *vertexbuffer, size_t bufferoffset)
3496 // note: vertexbuffer may be non-NULL even if pointer is NULL, so check
3497 // the pointer only.
3498 switch(vid.renderpath)
3500 case RENDERPATH_GL11:
3501 case RENDERPATH_GL13:
3502 case RENDERPATH_GLES1:
3507 // caller wants color array enabled
3508 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3509 if (!gl_state.pointer_color_enabled)
3511 gl_state.pointer_color_enabled = true;
3513 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
3515 if (gl_state.pointer_color_components != components || gl_state.pointer_color_gltype != gltype || gl_state.pointer_color_stride != stride || gl_state.pointer_color_pointer != pointer || gl_state.pointer_color_vertexbuffer != vertexbuffer || gl_state.pointer_color_offset != bufferoffset)
3517 gl_state.pointer_color_components = components;
3518 gl_state.pointer_color_gltype = gltype;
3519 gl_state.pointer_color_stride = stride;
3520 gl_state.pointer_color_pointer = pointer;
3521 gl_state.pointer_color_vertexbuffer = vertexbuffer;
3522 gl_state.pointer_color_offset = bufferoffset;
3524 GL_BindVBO(bufferobject);
3525 qglColorPointer(components, gltype, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3530 // caller wants color array disabled
3531 if (gl_state.pointer_color_enabled)
3533 gl_state.pointer_color_enabled = false;
3535 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
3536 // when color array is on the glColor gets trashed, set it again
3537 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
3542 case RENDERPATH_GL20:
3543 case RENDERPATH_GLES2:
3547 // caller wants color array enabled
3548 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3549 if (!gl_state.pointer_color_enabled)
3551 gl_state.pointer_color_enabled = true;
3553 qglEnableVertexAttribArray(GLSLATTRIB_COLOR);CHECKGLERROR
3555 if (gl_state.pointer_color_components != components || gl_state.pointer_color_gltype != gltype || gl_state.pointer_color_stride != stride || gl_state.pointer_color_pointer != pointer || gl_state.pointer_color_vertexbuffer != vertexbuffer || gl_state.pointer_color_offset != bufferoffset)
3557 gl_state.pointer_color_components = components;
3558 gl_state.pointer_color_gltype = gltype;
3559 gl_state.pointer_color_stride = stride;
3560 gl_state.pointer_color_pointer = pointer;
3561 gl_state.pointer_color_vertexbuffer = vertexbuffer;
3562 gl_state.pointer_color_offset = bufferoffset;
3564 GL_BindVBO(bufferobject);
3565 // LordHavoc: special flag added to gltype for unnormalized types
3566 qglVertexAttribPointer(GLSLATTRIB_COLOR, components, gltype & ~0x80000000, (gltype & 0x80000000) == 0, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3571 // caller wants color array disabled
3572 if (gl_state.pointer_color_enabled)
3574 gl_state.pointer_color_enabled = false;
3576 qglDisableVertexAttribArray(GLSLATTRIB_COLOR);CHECKGLERROR
3577 // when color array is on the glColor gets trashed, set it again
3578 qglVertexAttrib4f(GLSLATTRIB_COLOR, gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
3582 case RENDERPATH_D3D9:
3583 case RENDERPATH_D3D10:
3584 case RENDERPATH_D3D11:
3585 case RENDERPATH_SOFT:
3590 void R_Mesh_TexCoordPointer(unsigned int unitnum, int components, int gltype, size_t stride, const void *pointer, const r_meshbuffer_t *vertexbuffer, size_t bufferoffset)
3592 gltextureunit_t *unit = gl_state.units + unitnum;
3593 // update array settings
3594 // note: there is no need to check bufferobject here because all cases
3595 // that involve a valid bufferobject also supply a texcoord array
3596 switch(vid.renderpath)
3598 case RENDERPATH_GL11:
3599 case RENDERPATH_GL13:
3600 case RENDERPATH_GLES1:
3605 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3606 // texture array unit is enabled, enable the array
3607 if (!unit->arrayenabled)
3609 unit->arrayenabled = true;
3610 GL_ClientActiveTexture(unitnum);
3611 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
3614 if (unit->pointer_texcoord_components != components || unit->pointer_texcoord_gltype != gltype || unit->pointer_texcoord_stride != stride || unit->pointer_texcoord_pointer != pointer || unit->pointer_texcoord_vertexbuffer != vertexbuffer || unit->pointer_texcoord_offset != bufferoffset)
3616 unit->pointer_texcoord_components = components;
3617 unit->pointer_texcoord_gltype = gltype;
3618 unit->pointer_texcoord_stride = stride;
3619 unit->pointer_texcoord_pointer = pointer;
3620 unit->pointer_texcoord_vertexbuffer = vertexbuffer;
3621 unit->pointer_texcoord_offset = bufferoffset;
3622 GL_ClientActiveTexture(unitnum);
3623 GL_BindVBO(bufferobject);
3624 qglTexCoordPointer(components, gltype, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3629 // texture array unit is disabled, disable the array
3630 if (unit->arrayenabled)
3632 unit->arrayenabled = false;
3633 GL_ClientActiveTexture(unitnum);
3634 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
3639 case RENDERPATH_GL20:
3640 case RENDERPATH_GLES2:
3644 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3645 // texture array unit is enabled, enable the array
3646 if (!unit->arrayenabled)
3648 unit->arrayenabled = true;
3649 qglEnableVertexAttribArray(unitnum+GLSLATTRIB_TEXCOORD0);CHECKGLERROR
3652 if (unit->pointer_texcoord_components != components || unit->pointer_texcoord_gltype != gltype || unit->pointer_texcoord_stride != stride || unit->pointer_texcoord_pointer != pointer || unit->pointer_texcoord_vertexbuffer != vertexbuffer || unit->pointer_texcoord_offset != bufferoffset)
3654 unit->pointer_texcoord_components = components;
3655 unit->pointer_texcoord_gltype = gltype;
3656 unit->pointer_texcoord_stride = stride;
3657 unit->pointer_texcoord_pointer = pointer;
3658 unit->pointer_texcoord_vertexbuffer = vertexbuffer;
3659 unit->pointer_texcoord_offset = bufferoffset;
3660 GL_BindVBO(bufferobject);
3661 // LordHavoc: special flag added to gltype for unnormalized types
3662 qglVertexAttribPointer(unitnum+GLSLATTRIB_TEXCOORD0, components, gltype & ~0x80000000, (gltype & 0x80000000) == 0, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3667 // texture array unit is disabled, disable the array
3668 if (unit->arrayenabled)
3670 unit->arrayenabled = false;
3671 qglDisableVertexAttribArray(unitnum+GLSLATTRIB_TEXCOORD0);CHECKGLERROR
3675 case RENDERPATH_D3D9:
3676 case RENDERPATH_D3D10:
3677 case RENDERPATH_D3D11:
3678 case RENDERPATH_SOFT:
3683 int R_Mesh_TexBound(unsigned int unitnum, int id)
3685 gltextureunit_t *unit = gl_state.units + unitnum;
3686 if (unitnum >= vid.teximageunits)
3688 if (id == GL_TEXTURE_2D)
3690 if (id == GL_TEXTURE_3D)
3692 if (id == GL_TEXTURE_CUBE_MAP)
3693 return unit->tcubemap;
3697 void R_Mesh_CopyToTexture(rtexture_t *tex, int tx, int ty, int sx, int sy, int width, int height)
3699 switch(vid.renderpath)
3701 case RENDERPATH_GL11:
3702 case RENDERPATH_GL13:
3703 case RENDERPATH_GL20:
3704 case RENDERPATH_GLES1:
3705 case RENDERPATH_GLES2:
3706 R_Mesh_TexBind(0, tex);
3707 GL_ActiveTexture(0);CHECKGLERROR
3708 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, tx, ty, sx, sy, width, height);CHECKGLERROR
3710 case RENDERPATH_D3D9:
3713 IDirect3DSurface9 *currentsurface = NULL;
3714 IDirect3DSurface9 *texturesurface = NULL;
3717 sourcerect.left = sx;
3718 sourcerect.top = sy;
3719 sourcerect.right = sx + width;
3720 sourcerect.bottom = sy + height;
3723 destrect.right = tx + width;
3724 destrect.bottom = ty + height;
3725 if (!FAILED(IDirect3DTexture9_GetSurfaceLevel(((IDirect3DTexture9 *)tex->d3dtexture), 0, &texturesurface)))
3727 if (!FAILED(IDirect3DDevice9_GetRenderTarget(vid_d3d9dev, 0, ¤tsurface)))
3729 IDirect3DDevice9_StretchRect(vid_d3d9dev, currentsurface, &sourcerect, texturesurface, &destrect, D3DTEXF_NONE);
3730 IDirect3DSurface9_Release(currentsurface);
3732 IDirect3DSurface9_Release(texturesurface);
3737 case RENDERPATH_D3D10:
3738 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3740 case RENDERPATH_D3D11:
3741 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3743 case RENDERPATH_SOFT:
3744 DPSOFTRAST_CopyRectangleToTexture(tex->texnum, 0, tx, ty, sx, sy, width, height);
3750 int d3drswrap[16] = {D3DRS_WRAP0, D3DRS_WRAP1, D3DRS_WRAP2, D3DRS_WRAP3, D3DRS_WRAP4, D3DRS_WRAP5, D3DRS_WRAP6, D3DRS_WRAP7, D3DRS_WRAP8, D3DRS_WRAP9, D3DRS_WRAP10, D3DRS_WRAP11, D3DRS_WRAP12, D3DRS_WRAP13, D3DRS_WRAP14, D3DRS_WRAP15};
3753 void R_Mesh_ClearBindingsForTexture(int texnum)
3755 gltextureunit_t *unit;
3756 unsigned int unitnum;
3757 // this doesn't really unbind the texture, but it does prevent a mistaken "do nothing" behavior on the next time this same texnum is bound on the same unit as the same type (this mainly affects r_shadow_bouncegrid because 3D textures are so rarely used)
3758 for (unitnum = 0;unitnum < vid.teximageunits;unitnum++)
3760 unit = gl_state.units + unitnum;
3761 if (unit->t2d == texnum)
3763 if (unit->t3d == texnum)
3765 if (unit->tcubemap == texnum)
3766 unit->tcubemap = -1;
3770 void R_Mesh_TexBind(unsigned int unitnum, rtexture_t *tex)
3772 gltextureunit_t *unit = gl_state.units + unitnum;
3773 int tex2d, tex3d, texcubemap, texnum;
3774 if (unitnum >= vid.teximageunits)
3776 // if (unit->texture == tex)
3778 switch(vid.renderpath)
3780 case RENDERPATH_GL20:
3781 case RENDERPATH_GLES2:
3784 tex = r_texture_white;
3785 // not initialized enough yet...
3789 unit->texture = tex;
3790 texnum = R_GetTexture(tex);
3791 switch(tex->gltexturetypeenum)
3793 case GL_TEXTURE_2D: if (unit->t2d != texnum) {GL_ActiveTexture(unitnum);unit->t2d = texnum;qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR}break;
3794 case GL_TEXTURE_3D: if (unit->t3d != texnum) {GL_ActiveTexture(unitnum);unit->t3d = texnum;qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR}break;
3795 case GL_TEXTURE_CUBE_MAP: if (unit->tcubemap != texnum) {GL_ActiveTexture(unitnum);unit->tcubemap = texnum;qglBindTexture(GL_TEXTURE_CUBE_MAP, unit->tcubemap);CHECKGLERROR}break;
3798 case RENDERPATH_GL11:
3799 case RENDERPATH_GL13:
3800 case RENDERPATH_GLES1:
3801 unit->texture = tex;
3807 texnum = R_GetTexture(tex);
3808 switch(tex->gltexturetypeenum)
3816 case GL_TEXTURE_CUBE_MAP:
3817 texcubemap = texnum;
3821 // update 2d texture binding
3822 if (unit->t2d != tex2d)
3824 GL_ActiveTexture(unitnum);
3829 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
3836 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
3840 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
3842 // update 3d texture binding
3843 if (unit->t3d != tex3d)
3845 GL_ActiveTexture(unitnum);
3850 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
3857 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
3861 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
3863 // update cubemap texture binding
3864 if (unit->tcubemap != texcubemap)
3866 GL_ActiveTexture(unitnum);
3869 if (unit->tcubemap == 0)
3871 qglEnable(GL_TEXTURE_CUBE_MAP);CHECKGLERROR
3878 qglDisable(GL_TEXTURE_CUBE_MAP);CHECKGLERROR
3881 unit->tcubemap = texcubemap;
3882 qglBindTexture(GL_TEXTURE_CUBE_MAP, unit->tcubemap);CHECKGLERROR
3885 case RENDERPATH_D3D9:
3888 extern cvar_t gl_texture_anisotropy;
3891 tex = r_texture_white;
3892 // not initialized enough yet...
3896 // upload texture if needed
3898 if (unit->texture == tex)
3900 unit->texture = tex;
3901 IDirect3DDevice9_SetTexture(vid_d3d9dev, unitnum, (IDirect3DBaseTexture9*)tex->d3dtexture);
3902 //IDirect3DDevice9_SetRenderState(vid_d3d9dev, d3drswrap[unitnum], (tex->flags & TEXF_CLAMP) ? (D3DWRAPCOORD_0 | D3DWRAPCOORD_1 | D3DWRAPCOORD_2) : 0);
3903 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSU, tex->d3daddressu);
3904 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSV, tex->d3daddressv);
3905 if (tex->d3daddressw)
3906 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSW, tex->d3daddressw);
3907 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAGFILTER, tex->d3dmagfilter);
3908 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MINFILTER, tex->d3dminfilter);
3909 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MIPFILTER, tex->d3dmipfilter);
3910 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MIPMAPLODBIAS, tex->d3dmipmaplodbias);
3911 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAXMIPLEVEL, tex->d3dmaxmiplevelfilter);
3912 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAXANISOTROPY, gl_texture_anisotropy.integer);
3916 case RENDERPATH_D3D10:
3917 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3919 case RENDERPATH_D3D11:
3920 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3922 case RENDERPATH_SOFT:
3925 tex = r_texture_white;
3926 // not initialized enough yet...
3930 texnum = R_GetTexture(tex);
3931 if (unit->texture == tex)
3933 unit->texture = tex;
3934 DPSOFTRAST_SetTexture(unitnum, texnum);
3939 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
3941 gltextureunit_t *unit = gl_state.units + unitnum;
3942 switch(vid.renderpath)
3944 case RENDERPATH_GL11:
3945 case RENDERPATH_GL13:
3946 case RENDERPATH_GL20:
3947 case RENDERPATH_GLES1:
3948 case RENDERPATH_GLES2:
3950 if (matrix && matrix->m[3][3])
3952 // texmatrix specified, check if it is different
3953 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
3956 unit->texmatrixenabled = true;
3957 unit->matrix = *matrix;
3959 Matrix4x4_ToArrayFloatGL(&unit->matrix, glmatrix);
3960 GL_ActiveTexture(unitnum);
3961 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3962 qglLoadMatrixf(glmatrix);CHECKGLERROR
3963 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3968 // no texmatrix specified, revert to identity
3969 if (unit->texmatrixenabled)
3971 unit->texmatrixenabled = false;
3972 unit->matrix = identitymatrix;
3974 GL_ActiveTexture(unitnum);
3975 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3976 qglLoadIdentity();CHECKGLERROR
3977 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3982 case RENDERPATH_D3D9:
3983 case RENDERPATH_D3D10:
3984 case RENDERPATH_D3D11:
3986 case RENDERPATH_SOFT:
3991 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
3993 gltextureunit_t *unit = gl_state.units + unitnum;
3995 switch(vid.renderpath)
3997 case RENDERPATH_GL20:
3998 case RENDERPATH_GLES2:
4001 case RENDERPATH_GL13:
4002 case RENDERPATH_GLES1:
4003 #ifdef GL_TEXTURE_ENV
4004 // GL_ARB_texture_env_combine
4006 combinergb = GL_MODULATE;
4008 combinealpha = GL_MODULATE;
4013 if (combinergb != combinealpha || rgbscale != 1 || alphascale != 1)
4015 if (combinergb == GL_DECAL)
4016 combinergb = GL_INTERPOLATE;
4017 if (unit->combine != GL_COMBINE)
4019 unit->combine = GL_COMBINE;
4020 GL_ActiveTexture(unitnum);
4021 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);CHECKGLERROR
4022 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE mode
4024 if (unit->combinergb != combinergb)
4026 unit->combinergb = combinergb;
4027 GL_ActiveTexture(unitnum);
4028 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, unit->combinergb);CHECKGLERROR
4030 if (unit->combinealpha != combinealpha)
4032 unit->combinealpha = combinealpha;
4033 GL_ActiveTexture(unitnum);
4034 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, unit->combinealpha);CHECKGLERROR
4036 if (unit->rgbscale != rgbscale)
4038 unit->rgbscale = rgbscale;
4039 GL_ActiveTexture(unitnum);
4040 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE, unit->rgbscale);CHECKGLERROR
4042 if (unit->alphascale != alphascale)
4044 unit->alphascale = alphascale;
4045 GL_ActiveTexture(unitnum);
4046 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, unit->alphascale);CHECKGLERROR
4051 if (unit->combine != combinergb)
4053 unit->combine = combinergb;
4054 GL_ActiveTexture(unitnum);
4055 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
4060 case RENDERPATH_GL11:
4062 #ifdef GL_TEXTURE_ENV
4064 combinergb = GL_MODULATE;
4065 if (unit->combine != combinergb)
4067 unit->combine = combinergb;
4068 GL_ActiveTexture(unitnum);
4069 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
4073 case RENDERPATH_D3D9:
4074 case RENDERPATH_D3D10:
4075 case RENDERPATH_D3D11:
4077 case RENDERPATH_SOFT:
4082 void R_Mesh_ResetTextureState(void)
4084 unsigned int unitnum;
4088 for (unitnum = 0;unitnum < vid.teximageunits;unitnum++)
4089 R_Mesh_TexBind(unitnum, NULL);
4090 for (unitnum = 0;unitnum < vid.texarrayunits;unitnum++)
4091 R_Mesh_TexCoordPointer(unitnum, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4092 switch(vid.renderpath)
4094 case RENDERPATH_GL20:
4095 case RENDERPATH_GLES2:
4096 case RENDERPATH_D3D9:
4097 case RENDERPATH_D3D10:
4098 case RENDERPATH_D3D11:
4099 case RENDERPATH_SOFT:
4101 case RENDERPATH_GL11:
4102 case RENDERPATH_GL13:
4103 case RENDERPATH_GLES1:
4104 for (unitnum = 0;unitnum < vid.texunits;unitnum++)
4106 R_Mesh_TexCombine(unitnum, GL_MODULATE, GL_MODULATE, 1, 1);
4107 R_Mesh_TexMatrix(unitnum, NULL);
4116 //#define r_vertex3f_d3d9fvf (D3DFVF_XYZ)
4117 //#define r_vertexgeneric_d3d9fvf (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
4118 //#define r_vertexmesh_d3d9fvf (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX5 | D3DFVF_TEXCOORDSIZE1(3) | D3DFVF_TEXCOORDSIZE2(3) | D3DFVF_TEXCOORDSIZE3(3))
4120 D3DVERTEXELEMENT9 r_vertex3f_d3d9elements[] =
4122 {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
4126 D3DVERTEXELEMENT9 r_vertexgeneric_d3d9elements[] =
4128 {0, (int)((size_t)&((r_vertexgeneric_t *)0)->vertex3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
4129 {0, (int)((size_t)&((r_vertexgeneric_t *)0)->color4f ), D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
4130 {0, (int)((size_t)&((r_vertexgeneric_t *)0)->texcoord2f), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
4134 D3DVERTEXELEMENT9 r_vertexmesh_d3d9elements[] =
4136 {0, (int)((size_t)&((r_vertexmesh_t *)0)->vertex3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
4137 {0, (int)((size_t)&((r_vertexmesh_t *)0)->color4f ), D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
4138 {0, (int)((size_t)&((r_vertexmesh_t *)0)->texcoordtexture2f ), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
4139 {0, (int)((size_t)&((r_vertexmesh_t *)0)->svector3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
4140 {0, (int)((size_t)&((r_vertexmesh_t *)0)->tvector3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 2},
4141 {0, (int)((size_t)&((r_vertexmesh_t *)0)->normal3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 3},
4142 {0, (int)((size_t)&((r_vertexmesh_t *)0)->texcoordlightmap2f), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 4},
4143 {0, (int)((size_t)&((r_vertexmesh_t *)0)->skeletalindex4ub ), D3DDECLTYPE_UBYTE4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 6},
4144 {0, (int)((size_t)&((r_vertexmesh_t *)0)->skeletalweight4ub ), D3DDECLTYPE_UBYTE4N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 7},
4148 IDirect3DVertexDeclaration9 *r_vertex3f_d3d9decl;
4149 IDirect3DVertexDeclaration9 *r_vertexgeneric_d3d9decl;
4150 IDirect3DVertexDeclaration9 *r_vertexmesh_d3d9decl;
4153 static void R_Mesh_InitVertexDeclarations(void)
4156 r_vertex3f_d3d9decl = NULL;
4157 r_vertexgeneric_d3d9decl = NULL;
4158 r_vertexmesh_d3d9decl = NULL;
4159 switch(vid.renderpath)
4161 case RENDERPATH_GL20:
4162 case RENDERPATH_GL13:
4163 case RENDERPATH_GL11:
4164 case RENDERPATH_GLES1:
4165 case RENDERPATH_GLES2:
4167 case RENDERPATH_D3D9:
4168 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertex3f_d3d9elements, &r_vertex3f_d3d9decl);
4169 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexgeneric_d3d9elements, &r_vertexgeneric_d3d9decl);
4170 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexmesh_d3d9elements, &r_vertexmesh_d3d9decl);
4172 case RENDERPATH_D3D10:
4173 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4175 case RENDERPATH_D3D11:
4176 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4178 case RENDERPATH_SOFT:
4184 static void R_Mesh_DestroyVertexDeclarations(void)
4187 if (r_vertex3f_d3d9decl)
4188 IDirect3DVertexDeclaration9_Release(r_vertex3f_d3d9decl);
4189 r_vertex3f_d3d9decl = NULL;
4190 if (r_vertexgeneric_d3d9decl)
4191 IDirect3DVertexDeclaration9_Release(r_vertexgeneric_d3d9decl);
4192 r_vertexgeneric_d3d9decl = NULL;
4193 if (r_vertexmesh_d3d9decl)
4194 IDirect3DVertexDeclaration9_Release(r_vertexmesh_d3d9decl);
4195 r_vertexmesh_d3d9decl = NULL;
4199 void R_Mesh_PrepareVertices_Vertex3f(int numvertices, const float *vertex3f, const r_meshbuffer_t *vertexbuffer)
4201 // upload temporary vertexbuffer for this rendering
4202 if (!gl_state.usevbo_staticvertex)
4203 vertexbuffer = NULL;
4204 if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
4206 if (gl_state.preparevertices_dynamicvertexbuffer)
4207 R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex3f, numvertices * sizeof(float[3]));
4209 gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex3f, numvertices * sizeof(float[3]), "temporary", false, true, false);
4210 vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
4212 switch(vid.renderpath)
4214 case RENDERPATH_GL20:
4215 case RENDERPATH_GLES2:
4218 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
4219 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4220 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4221 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4222 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4223 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4224 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4225 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4226 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4227 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4231 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
4232 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4233 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4234 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4235 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4236 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4237 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4238 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4239 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4240 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4243 case RENDERPATH_GL13:
4244 case RENDERPATH_GLES1:
4247 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
4248 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4249 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4250 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4254 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
4255 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4256 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4257 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4260 case RENDERPATH_GL11:
4263 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
4264 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4265 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4269 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
4270 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4271 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4274 case RENDERPATH_D3D9:
4276 IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertex3f_d3d9decl);
4278 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(float[3]));
4280 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, NULL, 0, 0);
4281 gl_state.d3dvertexbuffer = (void *)vertexbuffer;
4282 gl_state.d3dvertexdata = (void *)vertex3f;
4283 gl_state.d3dvertexsize = sizeof(float[3]);
4286 case RENDERPATH_D3D10:
4287 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4289 case RENDERPATH_D3D11:
4290 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4292 case RENDERPATH_SOFT:
4293 DPSOFTRAST_SetVertexPointer(vertex3f, sizeof(float[3]));
4294 DPSOFTRAST_SetColorPointer(NULL, 0);
4295 DPSOFTRAST_SetTexCoordPointer(0, 2, sizeof(float[2]), NULL);
4296 DPSOFTRAST_SetTexCoordPointer(1, 2, sizeof(float[2]), NULL);
4297 DPSOFTRAST_SetTexCoordPointer(2, 2, sizeof(float[2]), NULL);
4298 DPSOFTRAST_SetTexCoordPointer(3, 2, sizeof(float[2]), NULL);
4299 DPSOFTRAST_SetTexCoordPointer(4, 2, sizeof(float[2]), NULL);
4306 r_vertexgeneric_t *R_Mesh_PrepareVertices_Generic_Lock(int numvertices)
4309 size = sizeof(r_vertexgeneric_t) * numvertices;
4310 if (gl_state.preparevertices_tempdatamaxsize < size)
4312 gl_state.preparevertices_tempdatamaxsize = size;
4313 gl_state.preparevertices_tempdata = Mem_Realloc(r_main_mempool, gl_state.preparevertices_tempdata, gl_state.preparevertices_tempdatamaxsize);
4315 gl_state.preparevertices_vertexgeneric = (r_vertexgeneric_t *)gl_state.preparevertices_tempdata;
4316 gl_state.preparevertices_numvertices = numvertices;
4317 return gl_state.preparevertices_vertexgeneric;
4320 qboolean R_Mesh_PrepareVertices_Generic_Unlock(void)
4322 R_Mesh_PrepareVertices_Generic(gl_state.preparevertices_numvertices, gl_state.preparevertices_vertexgeneric, NULL);
4323 gl_state.preparevertices_vertexgeneric = NULL;
4324 gl_state.preparevertices_numvertices = 0;
4328 void R_Mesh_PrepareVertices_Generic_Arrays(int numvertices, const float *vertex3f, const float *color4f, const float *texcoord2f)
4331 r_vertexgeneric_t *vertex;
4332 switch(vid.renderpath)
4334 case RENDERPATH_GL20:
4335 case RENDERPATH_GLES2:
4336 if (!vid.useinterleavedarrays)
4338 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
4339 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
4340 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoord2f, NULL, 0);
4341 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4342 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4343 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4344 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4345 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4346 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4347 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4351 case RENDERPATH_GL11:
4352 case RENDERPATH_GL13:
4353 case RENDERPATH_GLES1:
4354 if (!vid.useinterleavedarrays)
4356 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
4357 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
4358 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoord2f, NULL, 0);
4359 if (vid.texunits >= 2)
4360 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4361 if (vid.texunits >= 3)
4362 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4366 case RENDERPATH_D3D9:
4367 case RENDERPATH_D3D10:
4368 case RENDERPATH_D3D11:
4370 case RENDERPATH_SOFT:
4371 DPSOFTRAST_SetVertexPointer(vertex3f, sizeof(float[3]));
4372 DPSOFTRAST_SetColorPointer(color4f, sizeof(float[4]));
4373 DPSOFTRAST_SetTexCoordPointer(0, 2, sizeof(float[2]), texcoord2f);
4374 DPSOFTRAST_SetTexCoordPointer(1, 2, sizeof(float[2]), NULL);
4375 DPSOFTRAST_SetTexCoordPointer(2, 2, sizeof(float[2]), NULL);
4376 DPSOFTRAST_SetTexCoordPointer(3, 2, sizeof(float[2]), NULL);
4377 DPSOFTRAST_SetTexCoordPointer(4, 2, sizeof(float[2]), NULL);
4381 // no quick path for this case, convert to vertex structs
4382 vertex = R_Mesh_PrepareVertices_Generic_Lock(numvertices);
4383 for (i = 0;i < numvertices;i++)
4384 VectorCopy(vertex3f + 3*i, vertex[i].vertex3f);
4387 for (i = 0;i < numvertices;i++)
4388 Vector4Copy(color4f + 4*i, vertex[i].color4f);
4392 for (i = 0;i < numvertices;i++)
4393 Vector4Copy(gl_state.color4f, vertex[i].color4f);
4396 for (i = 0;i < numvertices;i++)
4397 Vector2Copy(texcoord2f + 2*i, vertex[i].texcoord2f);
4398 R_Mesh_PrepareVertices_Generic_Unlock();
4399 R_Mesh_PrepareVertices_Generic(numvertices, vertex, NULL);
4402 void R_Mesh_PrepareVertices_Generic(int numvertices, const r_vertexgeneric_t *vertex, const r_meshbuffer_t *vertexbuffer)
4404 // upload temporary vertexbuffer for this rendering
4405 if (!gl_state.usevbo_staticvertex)
4406 vertexbuffer = NULL;
4407 if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
4409 if (gl_state.preparevertices_dynamicvertexbuffer)
4410 R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex, numvertices * sizeof(*vertex));
4412 gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex, numvertices * sizeof(*vertex), "temporary", false, true, false);
4413 vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
4415 switch(vid.renderpath)
4417 case RENDERPATH_GL20:
4418 case RENDERPATH_GLES2:
4421 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
4422 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , vertexbuffer, (int)((unsigned char *)vertex->color4f - (unsigned char *)vertex));
4423 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , vertexbuffer, (int)((unsigned char *)vertex->texcoord2f - (unsigned char *)vertex));
4424 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4425 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4426 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4427 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4428 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4429 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4430 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4434 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
4435 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , NULL, 0);
4436 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , NULL, 0);
4437 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4438 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4439 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4440 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4441 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4442 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4443 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4446 case RENDERPATH_GL13:
4447 case RENDERPATH_GLES1:
4450 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
4451 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , vertexbuffer, (int)((unsigned char *)vertex->color4f - (unsigned char *)vertex));
4452 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , vertexbuffer, (int)((unsigned char *)vertex->texcoord2f - (unsigned char *)vertex));
4453 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4457 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
4458 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , NULL, 0);
4459 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , NULL, 0);
4460 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4463 case RENDERPATH_GL11:
4466 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
4467 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , vertexbuffer, (int)((unsigned char *)vertex->color4f - (unsigned char *)vertex));
4468 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , vertexbuffer, (int)((unsigned char *)vertex->texcoord2f - (unsigned char *)vertex));
4472 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
4473 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , NULL, 0);
4474 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , NULL, 0);
4477 case RENDERPATH_D3D9:
4479 IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertexgeneric_d3d9decl);
4481 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(*vertex));
4483 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, NULL, 0, 0);
4484 gl_state.d3dvertexbuffer = (void *)vertexbuffer;
4485 gl_state.d3dvertexdata = (void *)vertex;
4486 gl_state.d3dvertexsize = sizeof(*vertex);
4489 case RENDERPATH_D3D10:
4490 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4492 case RENDERPATH_D3D11:
4493 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4495 case RENDERPATH_SOFT:
4496 DPSOFTRAST_SetVertexPointer(vertex->vertex3f, sizeof(*vertex));
4497 DPSOFTRAST_SetColorPointer(vertex->color4f, sizeof(*vertex));
4498 DPSOFTRAST_SetTexCoordPointer(0, 2, sizeof(*vertex), vertex->texcoord2f);
4499 DPSOFTRAST_SetTexCoordPointer(1, 2, sizeof(*vertex), NULL);
4500 DPSOFTRAST_SetTexCoordPointer(2, 2, sizeof(*vertex), NULL);
4501 DPSOFTRAST_SetTexCoordPointer(3, 2, sizeof(*vertex), NULL);
4502 DPSOFTRAST_SetTexCoordPointer(4, 2, sizeof(*vertex), NULL);
4509 r_vertexmesh_t *R_Mesh_PrepareVertices_Mesh_Lock(int numvertices)
4512 size = sizeof(r_vertexmesh_t) * numvertices;
4513 if (gl_state.preparevertices_tempdatamaxsize < size)
4515 gl_state.preparevertices_tempdatamaxsize = size;
4516 gl_state.preparevertices_tempdata = Mem_Realloc(r_main_mempool, gl_state.preparevertices_tempdata, gl_state.preparevertices_tempdatamaxsize);
4518 gl_state.preparevertices_vertexmesh = (r_vertexmesh_t *)gl_state.preparevertices_tempdata;
4519 gl_state.preparevertices_numvertices = numvertices;
4520 return gl_state.preparevertices_vertexmesh;
4523 qboolean R_Mesh_PrepareVertices_Mesh_Unlock(void)
4525 R_Mesh_PrepareVertices_Mesh(gl_state.preparevertices_numvertices, gl_state.preparevertices_vertexmesh, NULL);
4526 gl_state.preparevertices_vertexmesh = NULL;
4527 gl_state.preparevertices_numvertices = 0;
4531 void R_Mesh_PrepareVertices_Mesh_Arrays(int numvertices, const float *vertex3f, const float *svector3f, const float *tvector3f, const float *normal3f, const float *color4f, const float *texcoordtexture2f, const float *texcoordlightmap2f)
4534 r_vertexmesh_t *vertex;
4535 switch(vid.renderpath)
4537 case RENDERPATH_GL20:
4538 case RENDERPATH_GLES2:
4539 if (!vid.useinterleavedarrays)
4541 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
4542 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
4543 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoordtexture2f, NULL, 0);
4544 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT, sizeof(float[3]), svector3f, NULL, 0);
4545 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT, sizeof(float[3]), tvector3f, NULL, 0);
4546 R_Mesh_TexCoordPointer(3, 3, GL_FLOAT, sizeof(float[3]), normal3f, NULL, 0);
4547 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), texcoordlightmap2f, NULL, 0);
4548 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4549 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4550 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4554 case RENDERPATH_GL11:
4555 case RENDERPATH_GL13:
4556 case RENDERPATH_GLES1:
4557 if (!vid.useinterleavedarrays)
4559 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
4560 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
4561 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoordtexture2f, NULL, 0);
4562 if (vid.texunits >= 2)
4563 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), texcoordlightmap2f, NULL, 0);
4564 if (vid.texunits >= 3)
4565 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4569 case RENDERPATH_D3D9:
4570 case RENDERPATH_D3D10:
4571 case RENDERPATH_D3D11:
4573 case RENDERPATH_SOFT:
4574 DPSOFTRAST_SetVertexPointer(vertex3f, sizeof(float[3]));
4575 DPSOFTRAST_SetColorPointer(color4f, sizeof(float[4]));
4576 DPSOFTRAST_SetTexCoordPointer(0, 2, sizeof(float[2]), texcoordtexture2f);
4577 DPSOFTRAST_SetTexCoordPointer(1, 3, sizeof(float[3]), svector3f);
4578 DPSOFTRAST_SetTexCoordPointer(2, 3, sizeof(float[3]), tvector3f);
4579 DPSOFTRAST_SetTexCoordPointer(3, 3, sizeof(float[3]), normal3f);
4580 DPSOFTRAST_SetTexCoordPointer(4, 2, sizeof(float[2]), texcoordlightmap2f);
4584 vertex = R_Mesh_PrepareVertices_Mesh_Lock(numvertices);
4585 for (i = 0;i < numvertices;i++)
4586 VectorCopy(vertex3f + 3*i, vertex[i].vertex3f);
4588 for (i = 0;i < numvertices;i++)
4589 VectorCopy(svector3f + 3*i, vertex[i].svector3f);
4591 for (i = 0;i < numvertices;i++)
4592 VectorCopy(tvector3f + 3*i, vertex[i].tvector3f);
4594 for (i = 0;i < numvertices;i++)
4595 VectorCopy(normal3f + 3*i, vertex[i].normal3f);
4598 for (i = 0;i < numvertices;i++)
4599 Vector4Copy(color4f + 4*i, vertex[i].color4f);
4603 for (i = 0;i < numvertices;i++)
4604 Vector4Copy(gl_state.color4f, vertex[i].color4f);
4606 if (texcoordtexture2f)
4607 for (i = 0;i < numvertices;i++)
4608 Vector2Copy(texcoordtexture2f + 2*i, vertex[i].texcoordtexture2f);
4609 if (texcoordlightmap2f)
4610 for (i = 0;i < numvertices;i++)
4611 Vector2Copy(texcoordlightmap2f + 2*i, vertex[i].texcoordlightmap2f);
4612 R_Mesh_PrepareVertices_Mesh_Unlock();
4613 R_Mesh_PrepareVertices_Mesh(numvertices, vertex, NULL);
4616 void R_Mesh_PrepareVertices_Mesh(int numvertices, const r_vertexmesh_t *vertex, const r_meshbuffer_t *vertexbuffer)
4618 // upload temporary vertexbuffer for this rendering
4619 if (!gl_state.usevbo_staticvertex)
4620 vertexbuffer = NULL;
4621 if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
4623 if (gl_state.preparevertices_dynamicvertexbuffer)
4624 R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex, numvertices * sizeof(*vertex));
4626 gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex, numvertices * sizeof(*vertex), "temporary", false, true, false);
4627 vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
4629 switch(vid.renderpath)
4631 case RENDERPATH_GL20:
4632 case RENDERPATH_GLES2:
4635 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
4636 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , vertexbuffer, (int)((unsigned char *)vertex->color4f - (unsigned char *)vertex));
4637 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , vertexbuffer, (int)((unsigned char *)vertex->texcoordtexture2f - (unsigned char *)vertex));
4638 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT , sizeof(*vertex), vertex->svector3f , vertexbuffer, (int)((unsigned char *)vertex->svector3f - (unsigned char *)vertex));
4639 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT , sizeof(*vertex), vertex->tvector3f , vertexbuffer, (int)((unsigned char *)vertex->tvector3f - (unsigned char *)vertex));
4640 R_Mesh_TexCoordPointer(3, 3, GL_FLOAT , sizeof(*vertex), vertex->normal3f , vertexbuffer, (int)((unsigned char *)vertex->normal3f - (unsigned char *)vertex));
4641 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, vertexbuffer, (int)((unsigned char *)vertex->texcoordlightmap2f - (unsigned char *)vertex));
4642 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT , sizeof(*vertex), NULL, NULL, 0);
4643 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE | 0x80000000, sizeof(*vertex), vertex->skeletalindex4ub , vertexbuffer, (int)((unsigned char *)vertex->skeletalindex4ub - (unsigned char *)vertex));
4644 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->skeletalweight4ub , vertexbuffer, (int)((unsigned char *)vertex->skeletalweight4ub - (unsigned char *)vertex));
4648 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
4649 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , NULL, 0);
4650 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , NULL, 0);
4651 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT , sizeof(*vertex), vertex->svector3f , NULL, 0);
4652 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT , sizeof(*vertex), vertex->tvector3f , NULL, 0);
4653 R_Mesh_TexCoordPointer(3, 3, GL_FLOAT , sizeof(*vertex), vertex->normal3f , NULL, 0);
4654 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, NULL, 0);
4655 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT , sizeof(*vertex), NULL, NULL, 0);
4656 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE | 0x80000000, sizeof(*vertex), vertex->skeletalindex4ub , NULL, 0);
4657 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->skeletalweight4ub , NULL, 0);
4660 case RENDERPATH_GL13:
4661 case RENDERPATH_GLES1:
4664 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
4665 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , vertexbuffer, (int)((unsigned char *)vertex->color4f - (unsigned char *)vertex));
4666 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , vertexbuffer, (int)((unsigned char *)vertex->texcoordtexture2f - (unsigned char *)vertex));
4667 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, vertexbuffer, (int)((unsigned char *)vertex->texcoordlightmap2f - (unsigned char *)vertex));
4671 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
4672 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , NULL, 0);
4673 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , NULL, 0);
4674 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, NULL, 0);
4677 case RENDERPATH_GL11:
4680 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
4681 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , vertexbuffer, (int)((unsigned char *)vertex->color4f - (unsigned char *)vertex));
4682 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , vertexbuffer, (int)((unsigned char *)vertex->texcoordtexture2f - (unsigned char *)vertex));
4686 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
4687 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , NULL, 0);
4688 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , NULL, 0);
4691 case RENDERPATH_D3D9:
4693 IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertexmesh_d3d9decl);
4695 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(*vertex));
4697 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, NULL, 0, 0);
4698 gl_state.d3dvertexbuffer = (void *)vertexbuffer;
4699 gl_state.d3dvertexdata = (void *)vertex;
4700 gl_state.d3dvertexsize = sizeof(*vertex);
4703 case RENDERPATH_D3D10:
4704 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4706 case RENDERPATH_D3D11:
4707 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4709 case RENDERPATH_SOFT:
4710 DPSOFTRAST_SetVertexPointer(vertex->vertex3f, sizeof(*vertex));
4711 DPSOFTRAST_SetColorPointer(vertex->color4f, sizeof(*vertex));
4712 DPSOFTRAST_SetTexCoordPointer(0, 2, sizeof(*vertex), vertex->texcoordtexture2f);
4713 DPSOFTRAST_SetTexCoordPointer(1, 3, sizeof(*vertex), vertex->svector3f);
4714 DPSOFTRAST_SetTexCoordPointer(2, 3, sizeof(*vertex), vertex->tvector3f);
4715 DPSOFTRAST_SetTexCoordPointer(3, 3, sizeof(*vertex), vertex->normal3f);
4716 DPSOFTRAST_SetTexCoordPointer(4, 2, sizeof(*vertex), vertex->texcoordlightmap2f);
4721 void GL_BlendEquationSubtract(qboolean negated)
4725 switch(vid.renderpath)
4727 case RENDERPATH_GL11:
4728 case RENDERPATH_GL13:
4729 case RENDERPATH_GL20:
4730 case RENDERPATH_GLES1:
4731 case RENDERPATH_GLES2:
4732 qglBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT);
4734 case RENDERPATH_D3D9:
4736 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_BLENDOP, D3DBLENDOP_SUBTRACT);
4739 case RENDERPATH_D3D10:
4740 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4742 case RENDERPATH_D3D11:
4743 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4745 case RENDERPATH_SOFT:
4746 DPSOFTRAST_BlendSubtract(true);
4752 switch(vid.renderpath)
4754 case RENDERPATH_GL11:
4755 case RENDERPATH_GL13:
4756 case RENDERPATH_GL20:
4757 case RENDERPATH_GLES1:
4758 case RENDERPATH_GLES2:
4759 qglBlendEquationEXT(GL_FUNC_ADD);
4761 case RENDERPATH_D3D9:
4763 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_BLENDOP, D3DBLENDOP_ADD);
4766 case RENDERPATH_D3D10:
4767 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4769 case RENDERPATH_D3D11:
4770 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4772 case RENDERPATH_SOFT:
4773 DPSOFTRAST_BlendSubtract(false);