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_paranoid = {0, "gl_paranoid", "0", "enables OpenGL error checking and other tests"};
104 cvar_t gl_printcheckerror = {0, "gl_printcheckerror", "0", "prints all OpenGL error checks, useful to identify location of driver crashes"};
106 cvar_t r_render = {0, "r_render", "1", "enables rendering 3D views (you want this on!)"};
107 cvar_t r_renderview = {0, "r_renderview", "1", "enables rendering 3D views (you want this on!)"};
108 cvar_t r_waterwarp = {CVAR_SAVE, "r_waterwarp", "1", "warp view while underwater"};
109 cvar_t gl_polyblend = {CVAR_SAVE, "gl_polyblend", "1", "tints view while underwater, hurt, etc"};
110 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1", "enables OpenGL dithering (16bit looks bad with this off)"};
111 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)"};
112 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"};
113 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"};
114 cvar_t gl_fbo = {CVAR_SAVE, "gl_fbo", "1", "make use of GL_ARB_framebuffer_object extension to enable shadowmaps and other features using pixel formats different from the framebuffer"};
116 cvar_t v_flipped = {0, "v_flipped", "0", "mirror the screen (poor man's left handed mode)"};
117 qboolean v_flipped_state = false;
119 r_viewport_t gl_viewport;
120 matrix4x4_t gl_modelmatrix;
121 matrix4x4_t gl_viewmatrix;
122 matrix4x4_t gl_modelviewmatrix;
123 matrix4x4_t gl_projectionmatrix;
124 matrix4x4_t gl_modelviewprojectionmatrix;
125 float gl_modelview16f[16];
126 float gl_modelviewprojection16f[16];
127 qboolean gl_modelmatrixchanged;
129 int gl_maxdrawrangeelementsvertices;
130 int gl_maxdrawrangeelementsindices;
135 void GL_PrintError(int errornumber, const char *filename, int linenumber)
139 #ifdef GL_INVALID_ENUM
140 case GL_INVALID_ENUM:
141 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
144 #ifdef GL_INVALID_VALUE
145 case GL_INVALID_VALUE:
146 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
149 #ifdef GL_INVALID_OPERATION
150 case GL_INVALID_OPERATION:
151 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
154 #ifdef GL_STACK_OVERFLOW
155 case GL_STACK_OVERFLOW:
156 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
159 #ifdef GL_STACK_UNDERFLOW
160 case GL_STACK_UNDERFLOW:
161 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
164 #ifdef GL_OUT_OF_MEMORY
165 case GL_OUT_OF_MEMORY:
166 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
169 #ifdef GL_TABLE_TOO_LARGE
170 case GL_TABLE_TOO_LARGE:
171 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
174 #ifdef GL_INVALID_FRAMEBUFFER_OPERATION
175 case GL_INVALID_FRAMEBUFFER_OPERATION:
176 Con_Printf("GL_INVALID_FRAMEBUFFER_OPERATION at %s:%i\n", filename, linenumber);
180 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
186 #define BACKENDACTIVECHECK if (!gl_state.active) Sys_Error("GL backend function called when backend is not active");
188 void SCR_ScreenShot_f (void);
190 typedef struct gltextureunit_s
192 int pointer_texcoord_components;
193 int pointer_texcoord_gltype;
194 size_t pointer_texcoord_stride;
195 const void *pointer_texcoord_pointer;
196 const r_meshbuffer_t *pointer_texcoord_vertexbuffer;
197 size_t pointer_texcoord_offset;
200 int t2d, t3d, tcubemap;
202 int rgbscale, alphascale;
204 int combinergb, combinealpha;
205 // texmatrixenabled exists only to avoid unnecessary texmatrix compares
206 int texmatrixenabled;
211 typedef struct gl_state_s
219 int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
223 float polygonoffset[2];
226 float alphafuncvalue;
227 qboolean alphatocoverage;
230 unsigned int clientunit;
231 gltextureunit_t units[MAX_TEXTUREUNITS];
235 int vertexbufferobject;
236 int elementbufferobject;
237 int uniformbufferobject;
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_vertexgeneric_t *preparevertices_vertexgeneric;
259 r_vertexmesh_t *preparevertices_vertexmesh;
260 int preparevertices_numvertices;
262 qboolean usevbo_staticvertex;
263 qboolean usevbo_staticindex;
264 qboolean usevbo_dynamicvertex;
265 qboolean usevbo_dynamicindex;
267 memexpandablearray_t meshbufferarray;
272 // rtexture_t *d3drt_depthtexture;
273 // rtexture_t *d3drt_colortextures[MAX_RENDERTARGETS];
274 IDirect3DSurface9 *d3drt_depthsurface;
275 IDirect3DSurface9 *d3drt_colorsurfaces[MAX_RENDERTARGETS];
276 IDirect3DSurface9 *d3drt_backbufferdepthsurface;
277 IDirect3DSurface9 *d3drt_backbuffercolorsurface;
278 void *d3dvertexbuffer;
285 static gl_state_t gl_state;
289 note: here's strip order for a terrain row:
296 A0B, 01B, B1C, 12C, C2D, 23D, D3E, 34E
298 *elements++ = i + row;
300 *elements++ = i + row + 1;
303 *elements++ = i + row + 1;
306 for (y = 0;y < rows - 1;y++)
308 for (x = 0;x < columns - 1;x++)
311 *elements++ = i + columns;
313 *elements++ = i + columns + 1;
316 *elements++ = i + columns + 1;
327 for (y = 0;y < rows - 1;y++)
329 for (x = 0;x < columns - 1;x++)
333 *elements++ = i + columns;
334 *elements++ = i + columns + 1;
335 *elements++ = i + columns;
336 *elements++ = i + columns + 1;
342 int polygonelement3i[(POLYGONELEMENTS_MAXPOINTS-2)*3];
343 unsigned short polygonelement3s[(POLYGONELEMENTS_MAXPOINTS-2)*3];
344 int quadelement3i[QUADELEMENTS_MAXQUADS*6];
345 unsigned short quadelement3s[QUADELEMENTS_MAXQUADS*6];
347 static void GL_VBOStats_f(void)
349 GL_Mesh_ListVBOs(true);
352 static void GL_Backend_ResetState(void);
354 static void R_Mesh_InitVertexDeclarations(void);
355 static void R_Mesh_DestroyVertexDeclarations(void);
357 static void R_Mesh_SetUseVBO(void)
359 switch(vid.renderpath)
361 case RENDERPATH_GL11:
362 case RENDERPATH_GL13:
363 case RENDERPATH_GL20:
364 case RENDERPATH_GLES1:
365 gl_state.usevbo_staticvertex = (vid.support.arb_vertex_buffer_object && gl_vbo.integer) || vid.forcevbo;
366 gl_state.usevbo_staticindex = (vid.support.arb_vertex_buffer_object && (gl_vbo.integer == 1 || gl_vbo.integer == 3)) || vid.forcevbo;
367 gl_state.usevbo_dynamicvertex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicvertex.integer && gl_vbo.integer) || vid.forcevbo;
368 gl_state.usevbo_dynamicindex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicindex.integer && gl_vbo.integer) || vid.forcevbo;
370 case RENDERPATH_D3D9:
371 gl_state.usevbo_staticvertex = gl_state.usevbo_staticindex = (vid.support.arb_vertex_buffer_object && gl_vbo.integer) || vid.forcevbo;
372 gl_state.usevbo_dynamicvertex = gl_state.usevbo_dynamicindex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicvertex.integer && gl_vbo_dynamicindex.integer && gl_vbo.integer) || vid.forcevbo;
374 case RENDERPATH_D3D10:
375 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
377 case RENDERPATH_D3D11:
378 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
380 case RENDERPATH_SOFT:
381 gl_state.usevbo_staticvertex = false;
382 gl_state.usevbo_staticindex = false;
383 gl_state.usevbo_dynamicvertex = false;
384 gl_state.usevbo_dynamicindex = false;
386 case RENDERPATH_GLES2:
387 gl_state.usevbo_staticvertex = (vid.support.arb_vertex_buffer_object && gl_vbo.integer) || vid.forcevbo;
388 gl_state.usevbo_staticindex = (vid.support.arb_vertex_buffer_object && gl_vbo.integer) || vid.forcevbo;
389 gl_state.usevbo_dynamicvertex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicvertex.integer) || vid.forcevbo;
390 gl_state.usevbo_dynamicindex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicindex.integer) || vid.forcevbo;
395 static void gl_backend_start(void)
397 memset(&gl_state, 0, sizeof(gl_state));
399 R_Mesh_InitVertexDeclarations();
402 Mem_ExpandableArray_NewArray(&gl_state.meshbufferarray, r_main_mempool, sizeof(r_meshbuffer_t), 128);
404 Con_DPrintf("OpenGL backend started.\n");
408 GL_Backend_ResetState();
410 switch(vid.renderpath)
412 case RENDERPATH_GL11:
413 case RENDERPATH_GL13:
414 case RENDERPATH_GL20:
415 case RENDERPATH_GLES1:
416 case RENDERPATH_GLES2:
417 // fetch current fbo here (default fbo is not 0 on some GLES devices)
418 if (vid.support.ext_framebuffer_object)
419 qglGetIntegerv(GL_FRAMEBUFFER_BINDING, &gl_state.defaultframebufferobject);
421 case RENDERPATH_D3D9:
423 IDirect3DDevice9_GetDepthStencilSurface(vid_d3d9dev, &gl_state.d3drt_backbufferdepthsurface);
424 IDirect3DDevice9_GetRenderTarget(vid_d3d9dev, 0, &gl_state.d3drt_backbuffercolorsurface);
427 case RENDERPATH_D3D10:
428 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
430 case RENDERPATH_D3D11:
431 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
433 case RENDERPATH_SOFT:
438 static void gl_backend_shutdown(void)
440 Con_DPrint("OpenGL Backend shutting down\n");
442 switch(vid.renderpath)
444 case RENDERPATH_GL11:
445 case RENDERPATH_GL13:
446 case RENDERPATH_GL20:
447 case RENDERPATH_SOFT:
448 case RENDERPATH_GLES1:
449 case RENDERPATH_GLES2:
451 case RENDERPATH_D3D9:
453 IDirect3DSurface9_Release(gl_state.d3drt_backbufferdepthsurface);
454 IDirect3DSurface9_Release(gl_state.d3drt_backbuffercolorsurface);
457 case RENDERPATH_D3D10:
458 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
460 case RENDERPATH_D3D11:
461 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
465 if (gl_state.preparevertices_tempdata)
466 Mem_Free(gl_state.preparevertices_tempdata);
468 Mem_ExpandableArray_FreeArray(&gl_state.meshbufferarray);
470 R_Mesh_DestroyVertexDeclarations();
472 memset(&gl_state, 0, sizeof(gl_state));
475 static void gl_backend_newmap(void)
479 static void gl_backend_devicelost(void)
482 r_meshbuffer_t *buffer;
484 gl_state.d3dvertexbuffer = NULL;
486 switch(vid.renderpath)
488 case RENDERPATH_GL11:
489 case RENDERPATH_GL13:
490 case RENDERPATH_GL20:
491 case RENDERPATH_SOFT:
492 case RENDERPATH_GLES1:
493 case RENDERPATH_GLES2:
495 case RENDERPATH_D3D9:
497 IDirect3DSurface9_Release(gl_state.d3drt_backbufferdepthsurface);
498 IDirect3DSurface9_Release(gl_state.d3drt_backbuffercolorsurface);
501 case RENDERPATH_D3D10:
502 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
504 case RENDERPATH_D3D11:
505 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
508 endindex = (int)Mem_ExpandableArray_IndexRange(&gl_state.meshbufferarray);
509 for (i = 0;i < endindex;i++)
511 buffer = (r_meshbuffer_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.meshbufferarray, i);
512 if (!buffer || !buffer->isdynamic)
514 switch(vid.renderpath)
516 case RENDERPATH_GL11:
517 case RENDERPATH_GL13:
518 case RENDERPATH_GL20:
519 case RENDERPATH_SOFT:
520 case RENDERPATH_GLES1:
521 case RENDERPATH_GLES2:
523 case RENDERPATH_D3D9:
525 if (buffer->devicebuffer)
527 if (buffer->isindexbuffer)
528 IDirect3DIndexBuffer9_Release((IDirect3DIndexBuffer9*)buffer->devicebuffer);
530 IDirect3DVertexBuffer9_Release((IDirect3DVertexBuffer9*)buffer->devicebuffer);
531 buffer->devicebuffer = NULL;
535 case RENDERPATH_D3D10:
536 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
538 case RENDERPATH_D3D11:
539 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
545 static void gl_backend_devicerestored(void)
547 switch(vid.renderpath)
549 case RENDERPATH_GL11:
550 case RENDERPATH_GL13:
551 case RENDERPATH_GL20:
552 case RENDERPATH_SOFT:
553 case RENDERPATH_GLES1:
554 case RENDERPATH_GLES2:
556 case RENDERPATH_D3D9:
558 IDirect3DDevice9_GetDepthStencilSurface(vid_d3d9dev, &gl_state.d3drt_backbufferdepthsurface);
559 IDirect3DDevice9_GetRenderTarget(vid_d3d9dev, 0, &gl_state.d3drt_backbuffercolorsurface);
562 case RENDERPATH_D3D10:
563 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
565 case RENDERPATH_D3D11:
566 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
571 void gl_backend_init(void)
575 for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
577 polygonelement3s[i * 3 + 0] = 0;
578 polygonelement3s[i * 3 + 1] = i + 1;
579 polygonelement3s[i * 3 + 2] = i + 2;
581 // elements for rendering a series of quads as triangles
582 for (i = 0;i < QUADELEMENTS_MAXQUADS;i++)
584 quadelement3s[i * 6 + 0] = i * 4;
585 quadelement3s[i * 6 + 1] = i * 4 + 1;
586 quadelement3s[i * 6 + 2] = i * 4 + 2;
587 quadelement3s[i * 6 + 3] = i * 4;
588 quadelement3s[i * 6 + 4] = i * 4 + 2;
589 quadelement3s[i * 6 + 5] = i * 4 + 3;
592 for (i = 0;i < (POLYGONELEMENTS_MAXPOINTS - 2)*3;i++)
593 polygonelement3i[i] = polygonelement3s[i];
594 for (i = 0;i < QUADELEMENTS_MAXQUADS*6;i++)
595 quadelement3i[i] = quadelement3s[i];
597 Cvar_RegisterVariable(&r_render);
598 Cvar_RegisterVariable(&r_renderview);
599 Cvar_RegisterVariable(&r_waterwarp);
600 Cvar_RegisterVariable(&gl_polyblend);
601 Cvar_RegisterVariable(&v_flipped);
602 Cvar_RegisterVariable(&gl_dither);
603 Cvar_RegisterVariable(&gl_vbo);
604 Cvar_RegisterVariable(&gl_vbo_dynamicvertex);
605 Cvar_RegisterVariable(&gl_vbo_dynamicindex);
606 Cvar_RegisterVariable(&gl_paranoid);
607 Cvar_RegisterVariable(&gl_printcheckerror);
609 Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
610 Cvar_RegisterVariable(&gl_mesh_testmanualfeeding);
612 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");
614 R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap, gl_backend_devicelost, gl_backend_devicerestored);
617 void GL_SetMirrorState(qboolean state);
619 void R_Viewport_TransformToScreen(const r_viewport_t *v, const vec4_t in, vec4_t out)
623 Matrix4x4_Transform4 (&v->viewmatrix, in, temp);
624 Matrix4x4_Transform4 (&v->projectmatrix, temp, out);
626 out[0] = v->x + (out[0] * iw + 1.0f) * v->width * 0.5f;
628 // for an odd reason, inverting this is wrong for R_Shadow_ScissorForBBox (we then get badly scissored lights)
629 //out[1] = v->y + v->height - (out[1] * iw + 1.0f) * v->height * 0.5f;
630 out[1] = v->y + (out[1] * iw + 1.0f) * v->height * 0.5f;
632 out[2] = v->z + (out[2] * iw + 1.0f) * v->depth * 0.5f;
637 switch(vid.renderpath)
639 case RENDERPATH_GL11:
640 case RENDERPATH_GL13:
641 case RENDERPATH_GL20:
642 case RENDERPATH_GLES1:
643 case RENDERPATH_GLES2:
646 case RENDERPATH_D3D9:
647 //Con_DPrintf("FIXME D3D9 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
649 case RENDERPATH_D3D10:
650 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
652 case RENDERPATH_D3D11:
653 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
655 case RENDERPATH_SOFT:
661 static int bboxedges[12][2] =
680 qboolean R_ScissorForBBox(const float *mins, const float *maxs, int *scissor)
682 int i, ix1, iy1, ix2, iy2;
683 float x1, y1, x2, y2;
694 scissor[0] = r_refdef.view.viewport.x;
695 scissor[1] = r_refdef.view.viewport.y;
696 scissor[2] = r_refdef.view.viewport.width;
697 scissor[3] = r_refdef.view.viewport.height;
699 // if view is inside the box, just say yes it's visible
700 if (BoxesOverlap(r_refdef.view.origin, r_refdef.view.origin, mins, maxs))
703 // transform all corners that are infront of the nearclip plane
704 VectorNegate(r_refdef.view.frustum[4].normal, plane4f);
705 plane4f[3] = r_refdef.view.frustum[4].dist;
707 for (i = 0;i < 8;i++)
709 Vector4Set(corner[i], (i & 1) ? maxs[0] : mins[0], (i & 2) ? maxs[1] : mins[1], (i & 4) ? maxs[2] : mins[2], 1);
710 dist[i] = DotProduct4(corner[i], plane4f);
711 sign[i] = dist[i] > 0;
714 VectorCopy(corner[i], vertex[numvertices]);
718 // if some points are behind the nearclip, add clipped edge points to make
719 // sure that the scissor boundary is complete
720 if (numvertices > 0 && numvertices < 8)
722 // add clipped edge points
723 for (i = 0;i < 12;i++)
727 if (sign[j] != sign[k])
729 f = dist[j] / (dist[j] - dist[k]);
730 VectorLerp(corner[j], f, corner[k], vertex[numvertices]);
736 // if we have no points to check, it is behind the view plane
740 // if we have some points to transform, check what screen area is covered
741 x1 = y1 = x2 = y2 = 0;
743 //Con_Printf("%i vertices to transform...\n", numvertices);
744 for (i = 0;i < numvertices;i++)
746 VectorCopy(vertex[i], v);
747 R_Viewport_TransformToScreen(&r_refdef.view.viewport, v, v2);
748 //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]);
751 if (x1 > v2[0]) x1 = v2[0];
752 if (x2 < v2[0]) x2 = v2[0];
753 if (y1 > v2[1]) y1 = v2[1];
754 if (y2 < v2[1]) y2 = v2[1];
763 // now convert the scissor rectangle to integer screen coordinates
764 ix1 = (int)(x1 - 1.0f);
765 //iy1 = vid.height - (int)(y2 - 1.0f);
766 //iy1 = r_refdef.view.viewport.width + 2 * r_refdef.view.viewport.x - (int)(y2 - 1.0f);
767 iy1 = (int)(y1 - 1.0f);
768 ix2 = (int)(x2 + 1.0f);
769 //iy2 = vid.height - (int)(y1 + 1.0f);
770 //iy2 = r_refdef.view.viewport.height + 2 * r_refdef.view.viewport.y - (int)(y1 + 1.0f);
771 iy2 = (int)(y2 + 1.0f);
772 //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
774 // clamp it to the screen
775 if (ix1 < r_refdef.view.viewport.x) ix1 = r_refdef.view.viewport.x;
776 if (iy1 < r_refdef.view.viewport.y) iy1 = r_refdef.view.viewport.y;
777 if (ix2 > r_refdef.view.viewport.x + r_refdef.view.viewport.width) ix2 = r_refdef.view.viewport.x + r_refdef.view.viewport.width;
778 if (iy2 > r_refdef.view.viewport.y + r_refdef.view.viewport.height) iy2 = r_refdef.view.viewport.y + r_refdef.view.viewport.height;
780 // if it is inside out, it's not visible
781 if (ix2 <= ix1 || iy2 <= iy1)
784 // the light area is visible, set up the scissor rectangle
787 scissor[2] = ix2 - ix1;
788 scissor[3] = iy2 - iy1;
790 // D3D Y coordinate is top to bottom, OpenGL is bottom to top, fix the D3D one
791 switch(vid.renderpath)
793 case RENDERPATH_D3D9:
794 case RENDERPATH_D3D10:
795 case RENDERPATH_D3D11:
796 scissor[1] = vid.height - scissor[1] - scissor[3];
798 case RENDERPATH_GL11:
799 case RENDERPATH_GL13:
800 case RENDERPATH_GL20:
801 case RENDERPATH_SOFT:
802 case RENDERPATH_GLES1:
803 case RENDERPATH_GLES2:
811 static void R_Viewport_ApplyNearClipPlaneFloatGL(const r_viewport_t *v, float *m, float normalx, float normaly, float normalz, float dist)
815 float clipPlane[4], v3[3], v4[3];
818 // This is inspired by Oblique Depth Projection from http://www.terathon.com/code/oblique.php
820 VectorSet(normal, normalx, normaly, normalz);
821 Matrix4x4_Transform3x3(&v->viewmatrix, normal, clipPlane);
822 VectorScale(normal, -dist, v3);
823 Matrix4x4_Transform(&v->viewmatrix, v3, v4);
824 // FIXME: LordHavoc: I think this can be done more efficiently somehow but I can't remember the technique
825 clipPlane[3] = -DotProduct(v4, clipPlane);
829 // testing code for comparing results
831 VectorCopy4(clipPlane, clipPlane2);
832 R_EntityMatrix(&identitymatrix);
833 VectorSet(q, normal[0], normal[1], normal[2], -dist);
834 qglClipPlane(GL_CLIP_PLANE0, q);
835 qglGetClipPlane(GL_CLIP_PLANE0, q);
836 VectorCopy4(q, clipPlane);
840 // Calculate the clip-space corner point opposite the clipping plane
841 // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
842 // transform it into camera space by multiplying it
843 // by the inverse of the projection matrix
844 q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + m[8]) / m[0];
845 q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + m[9]) / m[5];
847 q[3] = (1.0f + m[10]) / m[14];
849 // Calculate the scaled plane vector
850 d = 2.0f / DotProduct4(clipPlane, q);
852 // Replace the third row of the projection matrix
853 m[2] = clipPlane[0] * d;
854 m[6] = clipPlane[1] * d;
855 m[10] = clipPlane[2] * d + 1.0f;
856 m[14] = clipPlane[3] * d;
859 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)
861 float left = x1, right = x2, bottom = y2, top = y1, zNear = nearclip, zFar = farclip;
863 memset(v, 0, sizeof(*v));
864 v->type = R_VIEWPORTTYPE_ORTHO;
865 v->cameramatrix = *cameramatrix;
872 memset(m, 0, sizeof(m));
873 m[0] = 2/(right - left);
874 m[5] = 2/(top - bottom);
875 m[10] = -2/(zFar - zNear);
876 m[12] = - (right + left)/(right - left);
877 m[13] = - (top + bottom)/(top - bottom);
878 m[14] = - (zFar + zNear)/(zFar - zNear);
880 switch(vid.renderpath)
882 case RENDERPATH_GL11:
883 case RENDERPATH_GL13:
884 case RENDERPATH_GL20:
885 case RENDERPATH_SOFT:
886 case RENDERPATH_GLES1:
887 case RENDERPATH_GLES2:
889 case RENDERPATH_D3D9:
890 case RENDERPATH_D3D10:
891 case RENDERPATH_D3D11:
892 m[10] = -1/(zFar - zNear);
893 m[14] = -zNear/(zFar-zNear);
896 v->screentodepth[0] = -farclip / (farclip - nearclip);
897 v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
899 Matrix4x4_Invert_Full(&v->viewmatrix, &v->cameramatrix);
902 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
904 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
910 Vector4Set(test1, (x1+x2)*0.5f, (y1+y2)*0.5f, 0.0f, 1.0f);
911 R_Viewport_TransformToScreen(v, test1, test2);
912 Con_Printf("%f %f %f -> %f %f %f\n", test1[0], test1[1], test1[2], test2[0], test2[1], test2[2]);
917 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)
919 matrix4x4_t tempmatrix, basematrix;
921 memset(v, 0, sizeof(*v));
923 v->type = R_VIEWPORTTYPE_PERSPECTIVE;
924 v->cameramatrix = *cameramatrix;
931 memset(m, 0, sizeof(m));
932 m[0] = 1.0 / frustumx;
933 m[5] = 1.0 / frustumy;
934 m[10] = -(farclip + nearclip) / (farclip - nearclip);
936 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
937 v->screentodepth[0] = -farclip / (farclip - nearclip);
938 v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
940 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
941 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
942 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
943 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
946 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
948 if(v_flipped.integer)
956 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
959 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)
961 matrix4x4_t tempmatrix, basematrix;
962 const float nudge = 1.0 - 1.0 / (1<<23);
964 memset(v, 0, sizeof(*v));
966 v->type = R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP;
967 v->cameramatrix = *cameramatrix;
974 memset(m, 0, sizeof(m));
975 m[ 0] = 1.0 / frustumx;
976 m[ 5] = 1.0 / frustumy;
979 m[14] = -2 * nearclip * nudge;
980 v->screentodepth[0] = (m[10] + 1) * 0.5 - 1;
981 v->screentodepth[1] = m[14] * -0.5;
983 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
984 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
985 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
986 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
989 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
991 if(v_flipped.integer)
999 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
1002 float cubeviewmatrix[6][16] =
1004 // standard cubemap projections
1042 float rectviewmatrix[6][16] =
1044 // sign-preserving cubemap projections
1083 void R_Viewport_InitCubeSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, float nearclip, float farclip, const float *nearplane)
1085 matrix4x4_t tempmatrix, basematrix;
1087 memset(v, 0, sizeof(*v));
1088 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
1089 v->cameramatrix = *cameramatrix;
1093 memset(m, 0, sizeof(m));
1095 m[10] = -(farclip + nearclip) / (farclip - nearclip);
1097 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
1099 Matrix4x4_FromArrayFloatGL(&basematrix, cubeviewmatrix[side]);
1100 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
1101 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
1104 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
1106 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
1109 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)
1111 matrix4x4_t tempmatrix, basematrix;
1113 memset(v, 0, sizeof(*v));
1114 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
1115 v->cameramatrix = *cameramatrix;
1116 v->x = (side & 1) * size;
1117 v->y = (side >> 1) * size;
1121 memset(m, 0, sizeof(m));
1122 m[0] = m[5] = 1.0f * ((float)size - border) / size;
1123 m[10] = -(farclip + nearclip) / (farclip - nearclip);
1125 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
1127 Matrix4x4_FromArrayFloatGL(&basematrix, rectviewmatrix[side]);
1128 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
1129 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
1131 switch(vid.renderpath)
1133 case RENDERPATH_GL20:
1134 case RENDERPATH_GL13:
1135 case RENDERPATH_GL11:
1136 case RENDERPATH_SOFT:
1137 case RENDERPATH_GLES1:
1138 case RENDERPATH_GLES2:
1140 case RENDERPATH_D3D9:
1143 case RENDERPATH_D3D10:
1144 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1146 case RENDERPATH_D3D11:
1147 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1152 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
1154 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
1157 void R_SetViewport(const r_viewport_t *v)
1161 // FIXME: v_flipped_state is evil, this probably breaks somewhere
1162 GL_SetMirrorState(v_flipped.integer && (v->type == R_VIEWPORTTYPE_PERSPECTIVE || v->type == R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP));
1164 // copy over the matrices to our state
1165 gl_viewmatrix = v->viewmatrix;
1166 gl_projectionmatrix = v->projectmatrix;
1168 switch(vid.renderpath)
1170 case RENDERPATH_GL13:
1171 case RENDERPATH_GL11:
1172 case RENDERPATH_GLES1:
1177 qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
1178 // Load the projection matrix into OpenGL
1179 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
1180 Matrix4x4_ToArrayFloatGL(&gl_projectionmatrix, m);
1181 qglLoadMatrixf(m);CHECKGLERROR
1182 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1186 case RENDERPATH_D3D9:
1189 D3DVIEWPORT9 d3dviewport;
1190 d3dviewport.X = gl_viewport.x;
1191 d3dviewport.Y = gl_viewport.y;
1192 d3dviewport.Width = gl_viewport.width;
1193 d3dviewport.Height = gl_viewport.height;
1194 d3dviewport.MinZ = gl_state.depthrange[0];
1195 d3dviewport.MaxZ = gl_state.depthrange[1];
1196 IDirect3DDevice9_SetViewport(vid_d3d9dev, &d3dviewport);
1200 case RENDERPATH_D3D10:
1201 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1203 case RENDERPATH_D3D11:
1204 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1206 case RENDERPATH_SOFT:
1207 DPSOFTRAST_Viewport(v->x, v->y, v->width, v->height);
1209 case RENDERPATH_GL20:
1210 case RENDERPATH_GLES2:
1212 qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
1216 // force an update of the derived matrices
1217 gl_modelmatrixchanged = true;
1218 R_EntityMatrix(&gl_modelmatrix);
1221 void R_GetViewport(r_viewport_t *v)
1226 static void GL_BindVBO(int bufferobject)
1228 if (gl_state.vertexbufferobject != bufferobject)
1230 gl_state.vertexbufferobject = bufferobject;
1232 qglBindBufferARB(GL_ARRAY_BUFFER, bufferobject);CHECKGLERROR
1236 static void GL_BindEBO(int bufferobject)
1238 if (gl_state.elementbufferobject != bufferobject)
1240 gl_state.elementbufferobject = bufferobject;
1242 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, bufferobject);CHECKGLERROR
1246 static void GL_BindUBO(int bufferobject)
1248 if (gl_state.uniformbufferobject != bufferobject)
1250 gl_state.uniformbufferobject = bufferobject;
1251 #ifdef GL_UNIFORM_BUFFER
1253 qglBindBufferARB(GL_UNIFORM_BUFFER, bufferobject);CHECKGLERROR
1258 static const GLuint drawbuffers[4] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3};
1259 int R_Mesh_CreateFramebufferObject(rtexture_t *depthtexture, rtexture_t *colortexture, rtexture_t *colortexture2, rtexture_t *colortexture3, rtexture_t *colortexture4)
1261 switch(vid.renderpath)
1263 case RENDERPATH_GL11:
1264 case RENDERPATH_GL13:
1265 case RENDERPATH_GL20:
1266 case RENDERPATH_GLES1:
1267 case RENDERPATH_GLES2:
1268 if (vid.support.arb_framebuffer_object)
1272 qglGenFramebuffers(1, (GLuint*)&temp);CHECKGLERROR
1273 R_Mesh_SetRenderTargets(temp, NULL, NULL, NULL, NULL, NULL);
1274 // GL_ARB_framebuffer_object (GL3-class hardware) - depth stencil attachment
1276 // FIXME: separate stencil attachment on GLES
1277 if (depthtexture && depthtexture->texnum ) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT , depthtexture->gltexturetypeenum , depthtexture->texnum , 0);CHECKGLERROR
1278 if (depthtexture && depthtexture->renderbuffernum ) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT , GL_RENDERBUFFER, depthtexture->renderbuffernum );CHECKGLERROR
1280 if (depthtexture && depthtexture->texnum )
1282 qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT , depthtexture->gltexturetypeenum , depthtexture->texnum , 0);CHECKGLERROR
1283 if (depthtexture->glisdepthstencil) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT , depthtexture->gltexturetypeenum , depthtexture->texnum , 0);CHECKGLERROR
1285 if (depthtexture && depthtexture->renderbuffernum )
1287 qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT , GL_RENDERBUFFER, depthtexture->renderbuffernum );CHECKGLERROR
1288 if (depthtexture->glisdepthstencil) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT , GL_RENDERBUFFER, depthtexture->renderbuffernum );CHECKGLERROR
1291 if (colortexture && colortexture->texnum ) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , colortexture->gltexturetypeenum , colortexture->texnum , 0);CHECKGLERROR
1292 if (colortexture2 && colortexture2->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 , colortexture2->gltexturetypeenum, colortexture2->texnum, 0);CHECKGLERROR
1293 if (colortexture3 && colortexture3->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2 , colortexture3->gltexturetypeenum, colortexture3->texnum, 0);CHECKGLERROR
1294 if (colortexture4 && colortexture4->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3 , colortexture4->gltexturetypeenum, colortexture4->texnum, 0);CHECKGLERROR
1295 if (colortexture && colortexture->renderbuffernum ) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , GL_RENDERBUFFER, colortexture->renderbuffernum );CHECKGLERROR
1296 if (colortexture2 && colortexture2->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 , GL_RENDERBUFFER, colortexture2->renderbuffernum);CHECKGLERROR
1297 if (colortexture3 && colortexture3->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2 , GL_RENDERBUFFER, colortexture3->renderbuffernum);CHECKGLERROR
1298 if (colortexture4 && colortexture4->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3 , GL_RENDERBUFFER, colortexture4->renderbuffernum);CHECKGLERROR
1301 if (colortexture4 && qglDrawBuffersARB)
1303 qglDrawBuffersARB(4, drawbuffers);CHECKGLERROR
1304 qglReadBuffer(GL_NONE);CHECKGLERROR
1306 else if (colortexture3 && qglDrawBuffersARB)
1308 qglDrawBuffersARB(3, drawbuffers);CHECKGLERROR
1309 qglReadBuffer(GL_NONE);CHECKGLERROR
1311 else if (colortexture2 && qglDrawBuffersARB)
1313 qglDrawBuffersARB(2, drawbuffers);CHECKGLERROR
1314 qglReadBuffer(GL_NONE);CHECKGLERROR
1316 else if (colortexture && qglDrawBuffer)
1318 qglDrawBuffer(GL_COLOR_ATTACHMENT0);CHECKGLERROR
1319 qglReadBuffer(GL_COLOR_ATTACHMENT0);CHECKGLERROR
1321 else if (qglDrawBuffer)
1323 qglDrawBuffer(GL_NONE);CHECKGLERROR
1324 qglReadBuffer(GL_NONE);CHECKGLERROR
1327 status = qglCheckFramebufferStatus(GL_FRAMEBUFFER);CHECKGLERROR
1328 if (status != GL_FRAMEBUFFER_COMPLETE)
1330 Con_Printf("R_Mesh_CreateFramebufferObject: glCheckFramebufferStatus returned %i\n", status);
1331 gl_state.framebufferobject = 0; // GL unbinds it for us
1332 qglDeleteFramebuffers(1, (GLuint*)&temp);
1337 else if (vid.support.ext_framebuffer_object)
1341 qglGenFramebuffers(1, (GLuint*)&temp);CHECKGLERROR
1342 R_Mesh_SetRenderTargets(temp, NULL, NULL, NULL, NULL, NULL);
1343 // GL_EXT_framebuffer_object (GL2-class hardware) - no depth stencil attachment, let it break stencil
1344 if (depthtexture && depthtexture->texnum ) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT , depthtexture->gltexturetypeenum , depthtexture->texnum , 0);CHECKGLERROR
1345 if (depthtexture && depthtexture->renderbuffernum ) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT , GL_RENDERBUFFER, depthtexture->renderbuffernum );CHECKGLERROR
1346 if (colortexture && colortexture->texnum ) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , colortexture->gltexturetypeenum , colortexture->texnum , 0);CHECKGLERROR
1347 if (colortexture2 && colortexture2->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 , colortexture2->gltexturetypeenum, colortexture2->texnum, 0);CHECKGLERROR
1348 if (colortexture3 && colortexture3->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2 , colortexture3->gltexturetypeenum, colortexture3->texnum, 0);CHECKGLERROR
1349 if (colortexture4 && colortexture4->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3 , colortexture4->gltexturetypeenum, colortexture4->texnum, 0);CHECKGLERROR
1350 if (colortexture && colortexture->renderbuffernum ) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , GL_RENDERBUFFER, colortexture->renderbuffernum );CHECKGLERROR
1351 if (colortexture2 && colortexture2->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 , GL_RENDERBUFFER, colortexture2->renderbuffernum);CHECKGLERROR
1352 if (colortexture3 && colortexture3->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2 , GL_RENDERBUFFER, colortexture3->renderbuffernum);CHECKGLERROR
1353 if (colortexture4 && colortexture4->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3 , GL_RENDERBUFFER, colortexture4->renderbuffernum);CHECKGLERROR
1356 if (colortexture4 && qglDrawBuffersARB)
1358 qglDrawBuffersARB(4, drawbuffers);CHECKGLERROR
1359 qglReadBuffer(GL_NONE);CHECKGLERROR
1361 else if (colortexture3 && qglDrawBuffersARB)
1363 qglDrawBuffersARB(3, drawbuffers);CHECKGLERROR
1364 qglReadBuffer(GL_NONE);CHECKGLERROR
1366 else if (colortexture2 && qglDrawBuffersARB)
1368 qglDrawBuffersARB(2, drawbuffers);CHECKGLERROR
1369 qglReadBuffer(GL_NONE);CHECKGLERROR
1371 else if (colortexture && qglDrawBuffer)
1373 qglDrawBuffer(GL_COLOR_ATTACHMENT0);CHECKGLERROR
1374 qglReadBuffer(GL_COLOR_ATTACHMENT0);CHECKGLERROR
1376 else if (qglDrawBuffer)
1378 qglDrawBuffer(GL_NONE);CHECKGLERROR
1379 qglReadBuffer(GL_NONE);CHECKGLERROR
1382 status = qglCheckFramebufferStatus(GL_FRAMEBUFFER);CHECKGLERROR
1383 if (status != GL_FRAMEBUFFER_COMPLETE)
1385 Con_Printf("R_Mesh_CreateFramebufferObject: glCheckFramebufferStatus returned %i\n", status);
1386 gl_state.framebufferobject = 0; // GL unbinds it for us
1387 qglDeleteFramebuffers(1, (GLuint*)&temp);
1393 case RENDERPATH_D3D9:
1394 case RENDERPATH_D3D10:
1395 case RENDERPATH_D3D11:
1397 case RENDERPATH_SOFT:
1403 void R_Mesh_DestroyFramebufferObject(int fbo)
1405 switch(vid.renderpath)
1407 case RENDERPATH_GL11:
1408 case RENDERPATH_GL13:
1409 case RENDERPATH_GL20:
1410 case RENDERPATH_GLES1:
1411 case RENDERPATH_GLES2:
1414 // GL clears the binding if we delete something bound
1415 if (gl_state.framebufferobject == fbo)
1416 gl_state.framebufferobject = 0;
1417 qglDeleteFramebuffers(1, (GLuint*)&fbo);
1420 case RENDERPATH_D3D9:
1421 case RENDERPATH_D3D10:
1422 case RENDERPATH_D3D11:
1424 case RENDERPATH_SOFT:
1430 void R_Mesh_SetRenderTargetsD3D9(IDirect3DSurface9 *depthsurface, IDirect3DSurface9 *colorsurface0, IDirect3DSurface9 *colorsurface1, IDirect3DSurface9 *colorsurface2, IDirect3DSurface9 *colorsurface3)
1432 gl_state.framebufferobject = depthsurface != gl_state.d3drt_backbufferdepthsurface || colorsurface0 != gl_state.d3drt_backbuffercolorsurface;
1433 if (gl_state.d3drt_depthsurface != depthsurface)
1435 gl_state.d3drt_depthsurface = depthsurface;
1436 IDirect3DDevice9_SetDepthStencilSurface(vid_d3d9dev, gl_state.d3drt_depthsurface);
1438 if (gl_state.d3drt_colorsurfaces[0] != colorsurface0)
1440 gl_state.d3drt_colorsurfaces[0] = colorsurface0;
1441 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 0, gl_state.d3drt_colorsurfaces[0]);
1443 if (gl_state.d3drt_colorsurfaces[1] != colorsurface1)
1445 gl_state.d3drt_colorsurfaces[1] = colorsurface1;
1446 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 1, gl_state.d3drt_colorsurfaces[1]);
1448 if (gl_state.d3drt_colorsurfaces[2] != colorsurface2)
1450 gl_state.d3drt_colorsurfaces[2] = colorsurface2;
1451 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 2, gl_state.d3drt_colorsurfaces[2]);
1453 if (gl_state.d3drt_colorsurfaces[3] != colorsurface3)
1455 gl_state.d3drt_colorsurfaces[3] = colorsurface3;
1456 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 3, gl_state.d3drt_colorsurfaces[3]);
1461 void R_Mesh_SetRenderTargets(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture, rtexture_t *colortexture2, rtexture_t *colortexture3, rtexture_t *colortexture4)
1465 rtexture_t *textures[5];
1466 Vector4Set(textures, colortexture, colortexture2, colortexture3, colortexture4);
1467 textures[4] = depthtexture;
1468 // unbind any matching textures immediately, otherwise D3D will complain about a bound texture being used as a render target
1469 for (j = 0;j < 5;j++)
1471 for (i = 0;i < vid.teximageunits;i++)
1472 if (gl_state.units[i].texture == textures[j])
1473 R_Mesh_TexBind(i, NULL);
1474 // set up framebuffer object or render targets for the active rendering API
1475 switch(vid.renderpath)
1477 case RENDERPATH_GL11:
1478 case RENDERPATH_GL13:
1479 case RENDERPATH_GL20:
1480 case RENDERPATH_GLES1:
1481 case RENDERPATH_GLES2:
1482 if (gl_state.framebufferobject != fbo)
1484 gl_state.framebufferobject = fbo;
1485 qglBindFramebuffer(GL_FRAMEBUFFER, gl_state.framebufferobject ? gl_state.framebufferobject : gl_state.defaultframebufferobject);
1488 case RENDERPATH_D3D9:
1490 // set up the new render targets, a NULL depthtexture intentionally binds nothing
1491 // TODO: optimize: keep surface pointer around in rtexture_t until texture is freed or lost
1494 IDirect3DSurface9 *surfaces[5];
1495 for (i = 0;i < 5;i++)
1500 if (textures[i]->d3dsurface)
1501 surfaces[i] = (IDirect3DSurface9 *)textures[i]->d3dsurface;
1503 IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9 *)textures[i]->d3dtexture, 0, &surfaces[i]);
1506 // set the render targets for real
1507 R_Mesh_SetRenderTargetsD3D9(surfaces[4], surfaces[0], surfaces[1], surfaces[2], surfaces[3]);
1508 // release the texture surface levels (they won't be lost while bound...)
1509 for (i = 0;i < 5;i++)
1510 if (textures[i] && !textures[i]->d3dsurface)
1511 IDirect3DSurface9_Release(surfaces[i]);
1514 R_Mesh_SetRenderTargetsD3D9(gl_state.d3drt_backbufferdepthsurface, gl_state.d3drt_backbuffercolorsurface, NULL, NULL, NULL);
1517 case RENDERPATH_D3D10:
1518 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1520 case RENDERPATH_D3D11:
1521 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1523 case RENDERPATH_SOFT:
1527 unsigned int *pointers[5];
1528 memset(pointers, 0, sizeof(pointers));
1529 for (i = 0;i < 5;i++)
1530 pointers[i] = textures[i] ? (unsigned int *)DPSOFTRAST_Texture_GetPixelPointer(textures[i]->texnum, 0) : NULL;
1531 width = DPSOFTRAST_Texture_GetWidth(textures[0] ? textures[0]->texnum : textures[4]->texnum, 0);
1532 height = DPSOFTRAST_Texture_GetHeight(textures[0] ? textures[0]->texnum : textures[4]->texnum, 0);
1533 DPSOFTRAST_SetRenderTargets(width, height, pointers[4], pointers[0], pointers[1], pointers[2], pointers[3]);
1536 DPSOFTRAST_SetRenderTargets(vid.width, vid.height, vid.softdepthpixels, vid.softpixels, NULL, NULL, NULL);
1542 static int d3dcmpforglfunc(int f)
1546 case GL_NEVER: return D3DCMP_NEVER;
1547 case GL_LESS: return D3DCMP_LESS;
1548 case GL_EQUAL: return D3DCMP_EQUAL;
1549 case GL_LEQUAL: return D3DCMP_LESSEQUAL;
1550 case GL_GREATER: return D3DCMP_GREATER;
1551 case GL_NOTEQUAL: return D3DCMP_NOTEQUAL;
1552 case GL_GEQUAL: return D3DCMP_GREATEREQUAL;
1553 case GL_ALWAYS: return D3DCMP_ALWAYS;
1554 default: Con_DPrintf("Unknown GL_DepthFunc\n");return D3DCMP_ALWAYS;
1558 static int d3dstencilopforglfunc(int f)
1562 case GL_KEEP: return D3DSTENCILOP_KEEP;
1563 case GL_INCR: return D3DSTENCILOP_INCR; // note: GL_INCR is clamped, D3DSTENCILOP_INCR wraps
1564 case GL_DECR: return D3DSTENCILOP_DECR; // note: GL_DECR is clamped, D3DSTENCILOP_DECR wraps
1565 default: Con_DPrintf("Unknown GL_StencilFunc\n");return D3DSTENCILOP_KEEP;
1570 static void GL_Backend_ResetState(void)
1573 gl_state.active = true;
1574 gl_state.depthtest = true;
1575 gl_state.alphatest = false;
1576 gl_state.alphafunc = GL_GEQUAL;
1577 gl_state.alphafuncvalue = 0.5f;
1578 gl_state.alphatocoverage = false;
1579 gl_state.blendfunc1 = GL_ONE;
1580 gl_state.blendfunc2 = GL_ZERO;
1581 gl_state.blend = false;
1582 gl_state.depthmask = GL_TRUE;
1583 gl_state.colormask = 15;
1584 gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
1585 gl_state.lockrange_first = 0;
1586 gl_state.lockrange_count = 0;
1587 gl_state.cullface = GL_FRONT;
1588 gl_state.cullfaceenable = false;
1589 gl_state.polygonoffset[0] = 0;
1590 gl_state.polygonoffset[1] = 0;
1591 gl_state.framebufferobject = 0;
1592 gl_state.depthfunc = GL_LEQUAL;
1594 switch(vid.renderpath)
1596 case RENDERPATH_D3D9:
1599 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_COLORWRITEENABLE, gl_state.colormask);
1600 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_NONE);
1601 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZFUNC, d3dcmpforglfunc(gl_state.depthfunc));
1602 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZENABLE, gl_state.depthtest);
1603 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZWRITEENABLE, gl_state.depthmask);
1604 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SLOPESCALEDEPTHBIAS, gl_state.polygonoffset[0]);
1605 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DEPTHBIAS, gl_state.polygonoffset[1] * (1.0f / 16777216.0f));
1609 case RENDERPATH_D3D10:
1610 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1612 case RENDERPATH_D3D11:
1613 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1615 case RENDERPATH_GL11:
1616 case RENDERPATH_GL13:
1617 case RENDERPATH_GLES1:
1621 qglColorMask(1, 1, 1, 1);CHECKGLERROR
1622 qglAlphaFunc(gl_state.alphafunc, gl_state.alphafuncvalue);CHECKGLERROR
1623 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
1624 if (qglBlendFuncSeparate)
1626 qglBlendFuncSeparate(gl_state.blendfunc1, gl_state.blendfunc2, GL_ZERO, GL_ONE);CHECKGLERROR // ELUAN: Adreno 225 (and others) compositing workaround
1630 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1632 qglDisable(GL_BLEND);CHECKGLERROR
1633 qglCullFace(gl_state.cullface);CHECKGLERROR
1634 qglDisable(GL_CULL_FACE);CHECKGLERROR
1635 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1636 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1637 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1638 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1640 if (vid.support.arb_vertex_buffer_object)
1642 qglBindBufferARB(GL_ARRAY_BUFFER, 0);
1643 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, 0);
1646 if (vid.support.ext_framebuffer_object)
1648 //qglBindRenderbuffer(GL_RENDERBUFFER, 0);
1649 qglBindFramebuffer(GL_FRAMEBUFFER, 0);
1652 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
1653 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1655 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
1656 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1657 qglColor4f(1, 1, 1, 1);CHECKGLERROR
1659 if (vid.support.ext_framebuffer_object)
1660 qglBindFramebuffer(GL_FRAMEBUFFER, gl_state.framebufferobject);
1662 gl_state.unit = MAX_TEXTUREUNITS;
1663 gl_state.clientunit = MAX_TEXTUREUNITS;
1664 for (i = 0;i < vid.texunits;i++)
1666 GL_ActiveTexture(i);
1667 GL_ClientActiveTexture(i);
1668 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1669 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1670 if (vid.support.ext_texture_3d)
1672 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1673 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1675 if (vid.support.arb_texture_cube_map)
1677 qglDisable(GL_TEXTURE_CUBE_MAP);CHECKGLERROR
1678 qglBindTexture(GL_TEXTURE_CUBE_MAP, 0);CHECKGLERROR
1681 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
1682 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1683 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1684 qglLoadIdentity();CHECKGLERROR
1685 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1686 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
1691 case RENDERPATH_SOFT:
1692 DPSOFTRAST_ColorMask(1,1,1,1);
1693 DPSOFTRAST_BlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);
1694 DPSOFTRAST_CullFace(gl_state.cullface);
1695 DPSOFTRAST_DepthFunc(gl_state.depthfunc);
1696 DPSOFTRAST_DepthMask(gl_state.depthmask);
1697 DPSOFTRAST_PolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1698 DPSOFTRAST_SetRenderTargets(vid.width, vid.height, vid.softdepthpixels, vid.softpixels, NULL, NULL, NULL);
1699 DPSOFTRAST_Viewport(0, 0, vid.width, vid.height);
1701 case RENDERPATH_GL20:
1702 case RENDERPATH_GLES2:
1704 qglColorMask(1, 1, 1, 1);CHECKGLERROR
1705 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1706 qglDisable(GL_BLEND);CHECKGLERROR
1707 qglCullFace(gl_state.cullface);CHECKGLERROR
1708 qglDisable(GL_CULL_FACE);CHECKGLERROR
1709 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1710 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1711 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1712 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1713 if (vid.support.arb_vertex_buffer_object)
1715 qglBindBufferARB(GL_ARRAY_BUFFER, 0);
1716 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, 0);
1718 if (vid.support.ext_framebuffer_object)
1719 qglBindFramebuffer(GL_FRAMEBUFFER, gl_state.defaultframebufferobject);
1720 qglEnableVertexAttribArray(GLSLATTRIB_POSITION);
1721 qglVertexAttribPointer(GLSLATTRIB_POSITION, 3, GL_FLOAT, false, sizeof(float[3]), NULL);CHECKGLERROR
1722 qglDisableVertexAttribArray(GLSLATTRIB_COLOR);
1723 qglVertexAttribPointer(GLSLATTRIB_COLOR, 4, GL_FLOAT, false, sizeof(float[4]), NULL);CHECKGLERROR
1724 qglVertexAttrib4f(GLSLATTRIB_COLOR, 1, 1, 1, 1);
1725 gl_state.unit = MAX_TEXTUREUNITS;
1726 gl_state.clientunit = MAX_TEXTUREUNITS;
1727 for (i = 0;i < vid.teximageunits;i++)
1729 GL_ActiveTexture(i);
1730 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1731 if (vid.support.ext_texture_3d)
1733 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1735 if (vid.support.arb_texture_cube_map)
1737 qglBindTexture(GL_TEXTURE_CUBE_MAP, 0);CHECKGLERROR
1740 for (i = 0;i < vid.texarrayunits;i++)
1743 qglVertexAttribPointer(i+GLSLATTRIB_TEXCOORD0, 2, GL_FLOAT, false, sizeof(float[2]), NULL);CHECKGLERROR
1744 qglDisableVertexAttribArray(i+GLSLATTRIB_TEXCOORD0);CHECKGLERROR
1751 void GL_ActiveTexture(unsigned int num)
1753 if (gl_state.unit != num)
1755 gl_state.unit = num;
1756 switch(vid.renderpath)
1758 case RENDERPATH_GL11:
1759 case RENDERPATH_GL13:
1760 case RENDERPATH_GL20:
1761 case RENDERPATH_GLES1:
1762 case RENDERPATH_GLES2:
1763 if (qglActiveTexture)
1766 qglActiveTexture(GL_TEXTURE0 + gl_state.unit);
1770 case RENDERPATH_D3D9:
1771 case RENDERPATH_D3D10:
1772 case RENDERPATH_D3D11:
1774 case RENDERPATH_SOFT:
1780 void GL_ClientActiveTexture(unsigned int num)
1782 if (gl_state.clientunit != num)
1784 gl_state.clientunit = num;
1785 switch(vid.renderpath)
1787 case RENDERPATH_GL11:
1788 case RENDERPATH_GL13:
1789 case RENDERPATH_GLES1:
1791 if (qglActiveTexture)
1794 qglClientActiveTexture(GL_TEXTURE0 + gl_state.clientunit);
1799 case RENDERPATH_D3D9:
1800 case RENDERPATH_D3D10:
1801 case RENDERPATH_D3D11:
1803 case RENDERPATH_SOFT:
1805 case RENDERPATH_GL20:
1806 case RENDERPATH_GLES2:
1812 void GL_BlendFunc(int blendfunc1, int blendfunc2)
1814 if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
1816 qboolean blendenable;
1817 gl_state.blendfunc1 = blendfunc1;
1818 gl_state.blendfunc2 = blendfunc2;
1819 blendenable = (gl_state.blendfunc1 != GL_ONE || gl_state.blendfunc2 != GL_ZERO);
1820 switch(vid.renderpath)
1822 case RENDERPATH_GL11:
1823 case RENDERPATH_GL13:
1824 case RENDERPATH_GL20:
1825 case RENDERPATH_GLES1:
1826 case RENDERPATH_GLES2:
1828 if (qglBlendFuncSeparate)
1830 qglBlendFuncSeparate(gl_state.blendfunc1, gl_state.blendfunc2, GL_ZERO, GL_ONE);CHECKGLERROR // ELUAN: Adreno 225 (and others) compositing workaround
1834 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1836 if (gl_state.blend != blendenable)
1838 gl_state.blend = blendenable;
1839 if (!gl_state.blend)
1841 qglDisable(GL_BLEND);CHECKGLERROR
1845 qglEnable(GL_BLEND);CHECKGLERROR
1849 case RENDERPATH_D3D9:
1854 D3DBLEND d3dblendfunc[2];
1855 glblendfunc[0] = gl_state.blendfunc1;
1856 glblendfunc[1] = gl_state.blendfunc2;
1857 for (i = 0;i < 2;i++)
1859 switch(glblendfunc[i])
1861 case GL_ZERO: d3dblendfunc[i] = D3DBLEND_ZERO;break;
1862 case GL_ONE: d3dblendfunc[i] = D3DBLEND_ONE;break;
1863 case GL_SRC_COLOR: d3dblendfunc[i] = D3DBLEND_SRCCOLOR;break;
1864 case GL_ONE_MINUS_SRC_COLOR: d3dblendfunc[i] = D3DBLEND_INVSRCCOLOR;break;
1865 case GL_SRC_ALPHA: d3dblendfunc[i] = D3DBLEND_SRCALPHA;break;
1866 case GL_ONE_MINUS_SRC_ALPHA: d3dblendfunc[i] = D3DBLEND_INVSRCALPHA;break;
1867 case GL_DST_ALPHA: d3dblendfunc[i] = D3DBLEND_DESTALPHA;break;
1868 case GL_ONE_MINUS_DST_ALPHA: d3dblendfunc[i] = D3DBLEND_INVDESTALPHA;break;
1869 case GL_DST_COLOR: d3dblendfunc[i] = D3DBLEND_DESTCOLOR;break;
1870 case GL_ONE_MINUS_DST_COLOR: d3dblendfunc[i] = D3DBLEND_INVDESTCOLOR;break;
1873 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SRCBLEND, d3dblendfunc[0]);
1874 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DESTBLEND, d3dblendfunc[1]);
1875 if (gl_state.blend != blendenable)
1877 gl_state.blend = blendenable;
1878 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHABLENDENABLE, gl_state.blend);
1883 case RENDERPATH_D3D10:
1884 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1886 case RENDERPATH_D3D11:
1887 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1889 case RENDERPATH_SOFT:
1890 DPSOFTRAST_BlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);
1896 void GL_DepthMask(int state)
1898 if (gl_state.depthmask != state)
1900 gl_state.depthmask = state;
1901 switch(vid.renderpath)
1903 case RENDERPATH_GL11:
1904 case RENDERPATH_GL13:
1905 case RENDERPATH_GL20:
1906 case RENDERPATH_GLES1:
1907 case RENDERPATH_GLES2:
1909 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1911 case RENDERPATH_D3D9:
1913 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZWRITEENABLE, gl_state.depthmask);
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_DepthMask(gl_state.depthmask);
1929 void GL_DepthTest(int state)
1931 if (gl_state.depthtest != state)
1933 gl_state.depthtest = 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 if (gl_state.depthtest)
1944 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1948 qglDisable(GL_DEPTH_TEST);CHECKGLERROR
1951 case RENDERPATH_D3D9:
1953 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZENABLE, gl_state.depthtest);
1956 case RENDERPATH_D3D10:
1957 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1959 case RENDERPATH_D3D11:
1960 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1962 case RENDERPATH_SOFT:
1963 DPSOFTRAST_DepthTest(gl_state.depthtest);
1969 void GL_DepthFunc(int state)
1971 if (gl_state.depthfunc != state)
1973 gl_state.depthfunc = state;
1974 switch(vid.renderpath)
1976 case RENDERPATH_GL11:
1977 case RENDERPATH_GL13:
1978 case RENDERPATH_GL20:
1979 case RENDERPATH_GLES1:
1980 case RENDERPATH_GLES2:
1982 qglDepthFunc(gl_state.depthfunc);CHECKGLERROR
1984 case RENDERPATH_D3D9:
1986 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZFUNC, d3dcmpforglfunc(gl_state.depthfunc));
1989 case RENDERPATH_D3D10:
1990 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1992 case RENDERPATH_D3D11:
1993 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1995 case RENDERPATH_SOFT:
1996 DPSOFTRAST_DepthFunc(gl_state.depthfunc);
2002 void GL_DepthRange(float nearfrac, float farfrac)
2004 if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
2006 gl_state.depthrange[0] = nearfrac;
2007 gl_state.depthrange[1] = farfrac;
2008 switch(vid.renderpath)
2010 case RENDERPATH_GL11:
2011 case RENDERPATH_GL13:
2012 case RENDERPATH_GL20:
2013 case RENDERPATH_GLES1:
2014 case RENDERPATH_GLES2:
2016 qglDepthRangef(gl_state.depthrange[0], gl_state.depthrange[1]);
2018 qglDepthRange(gl_state.depthrange[0], gl_state.depthrange[1]);
2021 case RENDERPATH_D3D9:
2024 D3DVIEWPORT9 d3dviewport;
2025 d3dviewport.X = gl_viewport.x;
2026 d3dviewport.Y = gl_viewport.y;
2027 d3dviewport.Width = gl_viewport.width;
2028 d3dviewport.Height = gl_viewport.height;
2029 d3dviewport.MinZ = gl_state.depthrange[0];
2030 d3dviewport.MaxZ = gl_state.depthrange[1];
2031 IDirect3DDevice9_SetViewport(vid_d3d9dev, &d3dviewport);
2035 case RENDERPATH_D3D10:
2036 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2038 case RENDERPATH_D3D11:
2039 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2041 case RENDERPATH_SOFT:
2042 DPSOFTRAST_DepthRange(gl_state.depthrange[0], gl_state.depthrange[1]);
2048 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)
2050 switch (vid.renderpath)
2052 case RENDERPATH_GL11:
2053 case RENDERPATH_GL13:
2054 case RENDERPATH_GL20:
2055 case RENDERPATH_GLES1:
2056 case RENDERPATH_GLES2:
2060 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
2064 qglDisable(GL_STENCIL_TEST);CHECKGLERROR
2066 if (vid.support.ati_separate_stencil)
2068 qglStencilMask(writemask);CHECKGLERROR
2069 qglStencilOpSeparate(GL_FRONT, frontfail, frontzfail, frontzpass);CHECKGLERROR
2070 qglStencilOpSeparate(GL_BACK, backfail, backzfail, backzpass);CHECKGLERROR
2071 qglStencilFuncSeparate(GL_FRONT, frontcompare, comparereference, comparereference);CHECKGLERROR
2072 qglStencilFuncSeparate(GL_BACK, backcompare, comparereference, comparereference);CHECKGLERROR
2074 else if (vid.support.ext_stencil_two_side)
2076 #if defined(GL_STENCIL_TEST_TWO_SIDE_EXT) && !defined(USE_GLES2)
2077 qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
2078 qglActiveStencilFaceEXT(GL_FRONT);CHECKGLERROR
2079 qglStencilMask(writemask);CHECKGLERROR
2080 qglStencilOp(frontfail, frontzfail, frontzpass);CHECKGLERROR
2081 qglStencilFunc(frontcompare, comparereference, comparemask);CHECKGLERROR
2082 qglActiveStencilFaceEXT(GL_BACK);CHECKGLERROR
2083 qglStencilMask(writemask);CHECKGLERROR
2084 qglStencilOp(backfail, backzfail, backzpass);CHECKGLERROR
2085 qglStencilFunc(backcompare, comparereference, comparemask);CHECKGLERROR
2089 case RENDERPATH_D3D9:
2091 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_TWOSIDEDSTENCILMODE, true);
2092 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILENABLE, enable);
2093 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILWRITEMASK, writemask);
2094 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFAIL, d3dstencilopforglfunc(frontfail));
2095 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILZFAIL, d3dstencilopforglfunc(frontzfail));
2096 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILPASS, d3dstencilopforglfunc(frontzpass));
2097 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFUNC, d3dcmpforglfunc(frontcompare));
2098 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILFAIL, d3dstencilopforglfunc(backfail));
2099 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILZFAIL, d3dstencilopforglfunc(backzfail));
2100 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILPASS, d3dstencilopforglfunc(backzpass));
2101 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILFUNC, d3dcmpforglfunc(backcompare));
2102 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILREF, comparereference);
2103 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILMASK, comparemask);
2106 case RENDERPATH_D3D10:
2107 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2109 case RENDERPATH_D3D11:
2110 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2112 case RENDERPATH_SOFT:
2113 //Con_DPrintf("FIXME SOFT %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2118 void R_SetStencil(qboolean enable, int writemask, int fail, int zfail, int zpass, int compare, int comparereference, int comparemask)
2120 switch (vid.renderpath)
2122 case RENDERPATH_GL11:
2123 case RENDERPATH_GL13:
2124 case RENDERPATH_GL20:
2125 case RENDERPATH_GLES1:
2126 case RENDERPATH_GLES2:
2130 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
2134 qglDisable(GL_STENCIL_TEST);CHECKGLERROR
2136 if (vid.support.ext_stencil_two_side)
2138 #ifdef GL_STENCIL_TEST_TWO_SIDE_EXT
2139 qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
2142 qglStencilMask(writemask);CHECKGLERROR
2143 qglStencilOp(fail, zfail, zpass);CHECKGLERROR
2144 qglStencilFunc(compare, comparereference, comparemask);CHECKGLERROR
2147 case RENDERPATH_D3D9:
2149 if (vid.support.ati_separate_stencil)
2150 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_TWOSIDEDSTENCILMODE, true);
2151 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILENABLE, enable);
2152 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILWRITEMASK, writemask);
2153 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFAIL, d3dstencilopforglfunc(fail));
2154 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILZFAIL, d3dstencilopforglfunc(zfail));
2155 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILPASS, d3dstencilopforglfunc(zpass));
2156 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFUNC, d3dcmpforglfunc(compare));
2157 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILREF, comparereference);
2158 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILMASK, comparemask);
2161 case RENDERPATH_D3D10:
2162 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2164 case RENDERPATH_D3D11:
2165 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2167 case RENDERPATH_SOFT:
2168 //Con_DPrintf("FIXME SOFT %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2173 void GL_PolygonOffset(float planeoffset, float depthoffset)
2175 if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
2177 gl_state.polygonoffset[0] = planeoffset;
2178 gl_state.polygonoffset[1] = depthoffset;
2179 switch(vid.renderpath)
2181 case RENDERPATH_GL11:
2182 case RENDERPATH_GL13:
2183 case RENDERPATH_GL20:
2184 case RENDERPATH_GLES1:
2185 case RENDERPATH_GLES2:
2186 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
2188 case RENDERPATH_D3D9:
2190 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SLOPESCALEDEPTHBIAS, gl_state.polygonoffset[0]);
2191 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DEPTHBIAS, gl_state.polygonoffset[1] * (1.0f / 16777216.0f));
2194 case RENDERPATH_D3D10:
2195 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2197 case RENDERPATH_D3D11:
2198 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2200 case RENDERPATH_SOFT:
2201 DPSOFTRAST_PolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
2207 void GL_SetMirrorState(qboolean state)
2209 if (v_flipped_state != state)
2211 v_flipped_state = state;
2212 if (gl_state.cullface == GL_BACK)
2213 gl_state.cullface = GL_FRONT;
2214 else if (gl_state.cullface == GL_FRONT)
2215 gl_state.cullface = GL_BACK;
2218 switch(vid.renderpath)
2220 case RENDERPATH_GL11:
2221 case RENDERPATH_GL13:
2222 case RENDERPATH_GL20:
2223 case RENDERPATH_GLES1:
2224 case RENDERPATH_GLES2:
2225 qglCullFace(gl_state.cullface);CHECKGLERROR
2227 case RENDERPATH_D3D9:
2229 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, gl_state.cullface == GL_FRONT ? D3DCULL_CCW : D3DCULL_CW);
2232 case RENDERPATH_D3D10:
2233 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2235 case RENDERPATH_D3D11:
2236 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2238 case RENDERPATH_SOFT:
2239 DPSOFTRAST_CullFace(gl_state.cullface);
2245 void GL_CullFace(int state)
2249 if(state == GL_FRONT)
2251 else if(state == GL_BACK)
2255 switch(vid.renderpath)
2257 case RENDERPATH_GL11:
2258 case RENDERPATH_GL13:
2259 case RENDERPATH_GL20:
2260 case RENDERPATH_GLES1:
2261 case RENDERPATH_GLES2:
2264 if (state != GL_NONE)
2266 if (!gl_state.cullfaceenable)
2268 gl_state.cullfaceenable = true;
2269 qglEnable(GL_CULL_FACE);CHECKGLERROR
2271 if (gl_state.cullface != state)
2273 gl_state.cullface = state;
2274 qglCullFace(gl_state.cullface);CHECKGLERROR
2279 if (gl_state.cullfaceenable)
2281 gl_state.cullfaceenable = false;
2282 qglDisable(GL_CULL_FACE);CHECKGLERROR
2286 case RENDERPATH_D3D9:
2288 if (gl_state.cullface != state)
2290 gl_state.cullface = state;
2291 switch(gl_state.cullface)
2294 gl_state.cullfaceenable = false;
2295 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_NONE);
2298 gl_state.cullfaceenable = true;
2299 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_CCW);
2302 gl_state.cullfaceenable = true;
2303 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_CW);
2309 case RENDERPATH_D3D10:
2310 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2312 case RENDERPATH_D3D11:
2313 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2315 case RENDERPATH_SOFT:
2316 if (gl_state.cullface != state)
2318 gl_state.cullface = state;
2319 gl_state.cullfaceenable = state != GL_NONE ? true : false;
2320 DPSOFTRAST_CullFace(gl_state.cullface);
2326 void GL_AlphaTest(int state)
2328 if (gl_state.alphatest != state)
2330 gl_state.alphatest = state;
2331 switch(vid.renderpath)
2333 case RENDERPATH_GL11:
2334 case RENDERPATH_GL13:
2335 case RENDERPATH_GLES1:
2336 #ifdef GL_ALPHA_TEST
2337 // only fixed function uses alpha test, other paths use pixel kill capability in shaders
2339 if (gl_state.alphatest)
2341 qglEnable(GL_ALPHA_TEST);CHECKGLERROR
2345 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
2349 case RENDERPATH_D3D9:
2350 case RENDERPATH_D3D10:
2351 case RENDERPATH_D3D11:
2352 case RENDERPATH_SOFT:
2353 case RENDERPATH_GL20:
2354 case RENDERPATH_GLES2:
2360 void GL_AlphaToCoverage(qboolean state)
2362 if (gl_state.alphatocoverage != state)
2364 gl_state.alphatocoverage = state;
2365 switch(vid.renderpath)
2367 case RENDERPATH_GL11:
2368 case RENDERPATH_GL13:
2369 case RENDERPATH_GLES1:
2370 case RENDERPATH_GLES2:
2371 case RENDERPATH_D3D9:
2372 case RENDERPATH_D3D10:
2373 case RENDERPATH_D3D11:
2374 case RENDERPATH_SOFT:
2376 case RENDERPATH_GL20:
2377 #ifdef GL_SAMPLE_ALPHA_TO_COVERAGE_ARB
2378 // alpha to coverage turns the alpha value of the pixel into 0%, 25%, 50%, 75% or 100% by masking the multisample fragments accordingly
2380 if (gl_state.alphatocoverage)
2382 qglEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);CHECKGLERROR
2383 // qglEnable(GL_MULTISAMPLE_ARB);CHECKGLERROR
2387 qglDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);CHECKGLERROR
2388 // qglDisable(GL_MULTISAMPLE_ARB);CHECKGLERROR
2396 void GL_ColorMask(int r, int g, int b, int a)
2398 // NOTE: this matches D3DCOLORWRITEENABLE_RED, GREEN, BLUE, ALPHA
2399 int state = (r ? 1 : 0) | (g ? 2 : 0) | (b ? 4 : 0) | (a ? 8 : 0);
2400 if (gl_state.colormask != state)
2402 gl_state.colormask = state;
2403 switch(vid.renderpath)
2405 case RENDERPATH_GL11:
2406 case RENDERPATH_GL13:
2407 case RENDERPATH_GL20:
2408 case RENDERPATH_GLES1:
2409 case RENDERPATH_GLES2:
2411 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
2413 case RENDERPATH_D3D9:
2415 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_COLORWRITEENABLE, state);
2418 case RENDERPATH_D3D10:
2419 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2421 case RENDERPATH_D3D11:
2422 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2424 case RENDERPATH_SOFT:
2425 DPSOFTRAST_ColorMask(r, g, b, a);
2431 void GL_Color(float cr, float cg, float cb, float ca)
2433 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)
2435 gl_state.color4f[0] = cr;
2436 gl_state.color4f[1] = cg;
2437 gl_state.color4f[2] = cb;
2438 gl_state.color4f[3] = ca;
2439 switch(vid.renderpath)
2441 case RENDERPATH_GL11:
2442 case RENDERPATH_GL13:
2443 case RENDERPATH_GLES1:
2446 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
2450 case RENDERPATH_D3D9:
2451 case RENDERPATH_D3D10:
2452 case RENDERPATH_D3D11:
2453 // no equivalent in D3D
2455 case RENDERPATH_SOFT:
2456 DPSOFTRAST_Color4f(cr, cg, cb, ca);
2458 case RENDERPATH_GL20:
2459 case RENDERPATH_GLES2:
2460 qglVertexAttrib4f(GLSLATTRIB_COLOR, cr, cg, cb, ca);
2466 void GL_Scissor (int x, int y, int width, int height)
2468 switch(vid.renderpath)
2470 case RENDERPATH_GL11:
2471 case RENDERPATH_GL13:
2472 case RENDERPATH_GL20:
2473 case RENDERPATH_GLES1:
2474 case RENDERPATH_GLES2:
2476 qglScissor(x, y,width,height);
2479 case RENDERPATH_D3D9:
2485 d3drect.right = x + width;
2486 d3drect.bottom = y + height;
2487 IDirect3DDevice9_SetScissorRect(vid_d3d9dev, &d3drect);
2491 case RENDERPATH_D3D10:
2492 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2494 case RENDERPATH_D3D11:
2495 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2497 case RENDERPATH_SOFT:
2498 DPSOFTRAST_Scissor(x, y, width, height);
2503 void GL_ScissorTest(int state)
2505 if (gl_state.scissortest != state)
2507 gl_state.scissortest = state;
2508 switch(vid.renderpath)
2510 case RENDERPATH_GL11:
2511 case RENDERPATH_GL13:
2512 case RENDERPATH_GL20:
2513 case RENDERPATH_GLES1:
2514 case RENDERPATH_GLES2:
2516 if(gl_state.scissortest)
2517 qglEnable(GL_SCISSOR_TEST);
2519 qglDisable(GL_SCISSOR_TEST);
2522 case RENDERPATH_D3D9:
2524 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SCISSORTESTENABLE, gl_state.scissortest);
2527 case RENDERPATH_D3D10:
2528 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2530 case RENDERPATH_D3D11:
2531 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2533 case RENDERPATH_SOFT:
2534 DPSOFTRAST_ScissorTest(gl_state.scissortest);
2540 void GL_Clear(int mask, const float *colorvalue, float depthvalue, int stencilvalue)
2542 // opaque black - if you want transparent black, you'll need to pass in a colorvalue
2543 static const float blackcolor[4] = {0.0f, 0.0f, 0.0f, 1.0f};
2544 // prevent warnings when trying to clear a buffer that does not exist
2546 colorvalue = blackcolor;
2549 mask &= ~GL_STENCIL_BUFFER_BIT;
2552 switch(vid.renderpath)
2554 case RENDERPATH_GL11:
2555 case RENDERPATH_GL13:
2556 case RENDERPATH_GL20:
2557 case RENDERPATH_GLES1:
2558 case RENDERPATH_GLES2:
2560 if (mask & GL_COLOR_BUFFER_BIT)
2562 qglClearColor(colorvalue[0], colorvalue[1], colorvalue[2], colorvalue[3]);CHECKGLERROR
2564 if (mask & GL_DEPTH_BUFFER_BIT)
2567 qglClearDepthf(depthvalue);CHECKGLERROR
2569 qglClearDepth(depthvalue);CHECKGLERROR
2572 if (mask & GL_STENCIL_BUFFER_BIT)
2574 qglClearStencil(stencilvalue);CHECKGLERROR
2576 qglClear(mask);CHECKGLERROR
2578 case RENDERPATH_D3D9:
2580 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);
2583 case RENDERPATH_D3D10:
2584 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2586 case RENDERPATH_D3D11:
2587 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2589 case RENDERPATH_SOFT:
2590 if (mask & GL_COLOR_BUFFER_BIT)
2591 DPSOFTRAST_ClearColor(colorvalue[0], colorvalue[1], colorvalue[2], colorvalue[3]);
2592 if (mask & GL_DEPTH_BUFFER_BIT)
2593 DPSOFTRAST_ClearDepth(depthvalue);
2598 void GL_ReadPixelsBGRA(int x, int y, int width, int height, unsigned char *outpixels)
2600 switch(vid.renderpath)
2602 case RENDERPATH_GL11:
2603 case RENDERPATH_GL13:
2604 case RENDERPATH_GL20:
2605 case RENDERPATH_GLES1:
2606 case RENDERPATH_GLES2:
2615 qglReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, outpixels);CHECKGLERROR
2616 for (i = 0;i < width * height * 4;i += 4)
2619 // g = outpixels[i+1];
2621 // a = outpixels[i+3];
2623 // outpixels[i+1] = g;
2625 // outpixels[i+3] = a;
2629 qglReadPixels(x, y, width, height, GL_BGRA, GL_UNSIGNED_BYTE, outpixels);CHECKGLERROR
2632 case RENDERPATH_D3D9:
2635 // LordHavoc: we can't directly download the backbuffer because it may be
2636 // multisampled, and it may not be lockable, so we blit it to a lockable
2637 // surface of the same dimensions (but without multisample) to resolve the
2638 // multisample buffer to a normal image, and then lock that...
2639 IDirect3DSurface9 *stretchsurface = NULL;
2640 if (!FAILED(IDirect3DDevice9_CreateRenderTarget(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, TRUE, &stretchsurface, NULL)))
2642 D3DLOCKED_RECT lockedrect;
2643 if (!FAILED(IDirect3DDevice9_StretchRect(vid_d3d9dev, gl_state.d3drt_backbuffercolorsurface, NULL, stretchsurface, NULL, D3DTEXF_POINT)))
2645 if (!FAILED(IDirect3DSurface9_LockRect(stretchsurface, &lockedrect, NULL, D3DLOCK_READONLY)))
2648 unsigned char *row = (unsigned char *)lockedrect.pBits + x * 4 + lockedrect.Pitch * (vid.height - 1 - y);
2649 for (line = 0;line < height;line++, row -= lockedrect.Pitch)
2650 memcpy(outpixels + line * width * 4, row, width * 4);
2651 IDirect3DSurface9_UnlockRect(stretchsurface);
2654 IDirect3DSurface9_Release(stretchsurface);
2657 //IDirect3DSurface9 *syssurface = NULL;
2658 //if (!FAILED(IDirect3DDevice9_CreateRenderTarget(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, FALSE, &stretchsurface, NULL)))
2659 //if (!FAILED(IDirect3DDevice9_CreateOffscreenPlainSurface(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &syssurface, NULL)))
2660 //IDirect3DDevice9_GetRenderTargetData(vid_d3d9dev, gl_state.d3drt_backbuffercolorsurface, syssurface);
2661 //if (!FAILED(IDirect3DDevice9_GetFrontBufferData(vid_d3d9dev, 0, syssurface)))
2662 //if (!FAILED(IDirect3DSurface9_LockRect(syssurface, &lockedrect, NULL, D3DLOCK_READONLY)))
2663 //IDirect3DSurface9_UnlockRect(syssurface);
2664 //IDirect3DSurface9_Release(syssurface);
2668 case RENDERPATH_D3D10:
2669 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2671 case RENDERPATH_D3D11:
2672 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2674 case RENDERPATH_SOFT:
2675 DPSOFTRAST_GetPixelsBGRA(x, y, width, height, outpixels);
2680 // called at beginning of frame
2681 void R_Mesh_Start(void)
2684 R_Mesh_SetRenderTargets(0, NULL, NULL, NULL, NULL, NULL);
2686 if (gl_printcheckerror.integer && !gl_paranoid.integer)
2688 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
2689 Cvar_SetValueQuick(&gl_paranoid, 1);
2693 static qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
2697 char compilelog[MAX_INPUTLINE];
2698 shaderobject = qglCreateShader(shadertypeenum);CHECKGLERROR
2701 qglShaderSource(shaderobject, numstrings, strings, NULL);CHECKGLERROR
2702 qglCompileShader(shaderobject);CHECKGLERROR
2703 qglGetShaderiv(shaderobject, GL_COMPILE_STATUS, &shadercompiled);CHECKGLERROR
2704 qglGetShaderInfoLog(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
2705 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))
2707 int i, j, pretextlines = 0;
2708 for (i = 0;i < numstrings - 1;i++)
2709 for (j = 0;strings[i][j];j++)
2710 if (strings[i][j] == '\n')
2712 Con_Printf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
2714 if (!shadercompiled)
2716 qglDeleteShader(shaderobject);CHECKGLERROR
2719 qglAttachShader(programobject, shaderobject);CHECKGLERROR
2720 qglDeleteShader(shaderobject);CHECKGLERROR
2724 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)
2726 GLint programlinked;
2727 GLuint programobject = 0;
2728 char linklog[MAX_INPUTLINE];
2731 programobject = qglCreateProgram();CHECKGLERROR
2735 qglBindAttribLocation(programobject, GLSLATTRIB_POSITION , "Attrib_Position" );
2736 qglBindAttribLocation(programobject, GLSLATTRIB_COLOR , "Attrib_Color" );
2737 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD0, "Attrib_TexCoord0");
2738 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD1, "Attrib_TexCoord1");
2739 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD2, "Attrib_TexCoord2");
2740 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD3, "Attrib_TexCoord3");
2741 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD4, "Attrib_TexCoord4");
2742 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD5, "Attrib_TexCoord5");
2743 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD6, "Attrib_SkeletalIndex");
2744 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD7, "Attrib_SkeletalWeight");
2746 if(vid.support.gl20shaders130)
2747 qglBindFragDataLocation(programobject, 0, "dp_FragColor");
2750 if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER, "vertex", vertexstrings_count, vertexstrings_list))
2753 #if defined(GL_GEOMETRY_SHADER) && !defined(USE_GLES2)
2754 if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER, "geometry", geometrystrings_count, geometrystrings_list))
2758 if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER, "fragment", fragmentstrings_count, fragmentstrings_list))
2761 qglLinkProgram(programobject);CHECKGLERROR
2762 qglGetProgramiv(programobject, GL_LINK_STATUS, &programlinked);CHECKGLERROR
2763 qglGetProgramInfoLog(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
2768 if (strstr(linklog, "error") || strstr(linklog, "ERROR") || strstr(linklog, "Error") || strstr(linklog, "WARNING") || strstr(linklog, "warning") || strstr(linklog, "Warning") || developer_extra.integer)
2769 Con_DPrintf("program link log:\n%s\n", linklog);
2771 // software vertex shader is ok but software fragment shader is WAY
2772 // too slow, fail program if so.
2773 // NOTE: this string might be ATI specific, but that's ok because the
2774 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
2775 // software fragment shader due to low instruction and dependent
2777 if (strstr(linklog, "fragment shader will run in software"))
2778 programlinked = false;
2784 return programobject;
2786 qglDeleteProgram(programobject);CHECKGLERROR
2790 void GL_Backend_FreeProgram(unsigned int prog)
2793 qglDeleteProgram(prog);
2797 // renders triangles using vertices from the active arrays
2798 int paranoidblah = 0;
2799 void R_Mesh_Draw(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const r_meshbuffer_t *element3i_indexbuffer, int element3i_bufferoffset, const unsigned short *element3s, const r_meshbuffer_t *element3s_indexbuffer, int element3s_bufferoffset)
2801 unsigned int numelements = numtriangles * 3;
2803 size_t bufferoffset3i;
2805 size_t bufferoffset3s;
2806 if (numvertices < 3 || numtriangles < 1)
2808 if (numvertices < 0 || numtriangles < 0 || developer_extra.integer)
2809 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);
2812 // adjust the pointers for firsttriangle
2814 element3i += firsttriangle * 3;
2815 if (element3i_indexbuffer)
2816 element3i_bufferoffset += firsttriangle * 3 * sizeof(*element3i);
2818 element3s += firsttriangle * 3;
2819 if (element3s_indexbuffer)
2820 element3s_bufferoffset += firsttriangle * 3 * sizeof(*element3s);
2821 switch(vid.renderpath)
2823 case RENDERPATH_GL11:
2824 case RENDERPATH_GL13:
2825 case RENDERPATH_GL20:
2826 case RENDERPATH_GLES1:
2827 case RENDERPATH_GLES2:
2828 // check if the user specified to ignore static index buffers
2829 if (!gl_state.usevbo_staticindex || (gl_vbo.integer == 3 && !vid.forcevbo && (element3i_bufferoffset || element3s_bufferoffset)))
2831 element3i_indexbuffer = NULL;
2832 element3s_indexbuffer = NULL;
2835 case RENDERPATH_D3D9:
2836 case RENDERPATH_D3D10:
2837 case RENDERPATH_D3D11:
2839 case RENDERPATH_SOFT:
2842 // upload a dynamic index buffer if needed
2845 if (!element3s_indexbuffer && gl_state.usevbo_dynamicindex)
2846 element3s_indexbuffer = R_BufferData_Store(numelements * sizeof(*element3s), (void *)element3s, R_BUFFERDATA_INDEX16, &element3s_bufferoffset);
2850 if (!element3i_indexbuffer && gl_state.usevbo_dynamicindex)
2851 element3i_indexbuffer = R_BufferData_Store(numelements * sizeof(*element3i), (void *)element3i, R_BUFFERDATA_INDEX32, &element3i_bufferoffset);
2853 bufferobject3i = element3i_indexbuffer ? element3i_indexbuffer->bufferobject : 0;
2854 bufferoffset3i = element3i_bufferoffset;
2855 bufferobject3s = element3s_indexbuffer ? element3s_indexbuffer->bufferobject : 0;
2856 bufferoffset3s = element3s_bufferoffset;
2857 r_refdef.stats[r_stat_draws]++;
2858 r_refdef.stats[r_stat_draws_vertices] += numvertices;
2859 r_refdef.stats[r_stat_draws_elements] += numelements;
2860 if (gl_paranoid.integer)
2863 // LordHavoc: disabled this - it needs to be updated to handle components and gltype and stride in each array
2865 unsigned int j, size;
2867 // note: there's no validation done here on buffer objects because it
2868 // is somewhat difficult to get at the data, and gl_paranoid can be
2869 // used without buffer objects if the need arises
2870 // (the data could be gotten using glMapBuffer but it would be very
2871 // slow due to uncachable video memory reads)
2872 if (!qglIsEnabled(GL_VERTEX_ARRAY))
2873 Con_Print("R_Mesh_Draw: vertex array not enabled\n");
2875 if (gl_state.pointer_vertex_pointer)
2876 for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
2878 if (gl_state.pointer_color_enabled)
2880 if (!qglIsEnabled(GL_COLOR_ARRAY))
2881 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
2883 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
2884 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
2887 for (i = 0;i < vid.texarrayunits;i++)
2889 if (gl_state.units[i].arrayenabled)
2891 GL_ClientActiveTexture(i);
2892 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
2893 Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
2895 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
2896 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++)
2903 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2905 if (element3i[i] < firstvertex || element3i[i] >= firstvertex + numvertices)
2907 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3i array\n", element3i[i], firstvertex, firstvertex + numvertices);
2914 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2916 if (element3s[i] < firstvertex || element3s[i] >= firstvertex + numvertices)
2918 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3s array\n", element3s[i], firstvertex, firstvertex + numvertices);
2924 if (r_render.integer || r_refdef.draw2dstage)
2926 switch(vid.renderpath)
2928 case RENDERPATH_GL11:
2929 case RENDERPATH_GL13:
2930 case RENDERPATH_GL20:
2932 if (gl_mesh_testmanualfeeding.integer)
2935 unsigned int i, j, element;
2937 qglBegin(GL_TRIANGLES);
2938 if(vid.renderpath == RENDERPATH_GL20)
2940 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2943 element = element3i[i];
2945 element = element3s[i];
2947 element = firstvertex + i;
2948 for (j = 0;j < vid.texarrayunits;j++)
2950 if (gl_state.units[j].pointer_texcoord_pointer && gl_state.units[j].arrayenabled)
2952 if (gl_state.units[j].pointer_texcoord_gltype == GL_FLOAT)
2954 p = (const GLfloat *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2955 if (gl_state.units[j].pointer_texcoord_components == 4)
2956 qglVertexAttrib4f(GLSLATTRIB_TEXCOORD0 + j, p[0], p[1], p[2], p[3]);
2957 else if (gl_state.units[j].pointer_texcoord_components == 3)
2958 qglVertexAttrib3f(GLSLATTRIB_TEXCOORD0 + j, p[0], p[1], p[2]);
2959 else if (gl_state.units[j].pointer_texcoord_components == 2)
2960 qglVertexAttrib2f(GLSLATTRIB_TEXCOORD0 + j, p[0], p[1]);
2962 qglVertexAttrib1f(GLSLATTRIB_TEXCOORD0 + j, p[0]);
2964 else if (gl_state.units[j].pointer_texcoord_gltype == (int)(GL_SHORT | 0x80000000))
2966 const GLshort *s = (const GLshort *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2967 if (gl_state.units[j].pointer_texcoord_components == 4)
2968 qglVertexAttrib4f(GLSLATTRIB_TEXCOORD0 + j, s[0], s[1], s[2], s[3]);
2969 else if (gl_state.units[j].pointer_texcoord_components == 3)
2970 qglVertexAttrib3f(GLSLATTRIB_TEXCOORD0 + j, s[0], s[1], s[2]);
2971 else if (gl_state.units[j].pointer_texcoord_components == 2)
2972 qglVertexAttrib2f(GLSLATTRIB_TEXCOORD0 + j, s[0], s[1]);
2973 else if (gl_state.units[j].pointer_texcoord_components == 1)
2974 qglVertexAttrib1f(GLSLATTRIB_TEXCOORD0 + j, s[0]);
2976 else if (gl_state.units[j].pointer_texcoord_gltype == GL_BYTE)
2978 const GLbyte *sb = (const GLbyte *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2979 if (gl_state.units[j].pointer_texcoord_components == 4)
2980 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));
2981 else if (gl_state.units[j].pointer_texcoord_components == 3)
2982 qglVertexAttrib3f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 127.0f), sb[1] * (1.0f / 127.0f), sb[2] * (1.0f / 127.0f));
2983 else if (gl_state.units[j].pointer_texcoord_components == 2)
2984 qglVertexAttrib2f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 127.0f), sb[1] * (1.0f / 127.0f));
2985 else if (gl_state.units[j].pointer_texcoord_components == 1)
2986 qglVertexAttrib1f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 127.0f));
2988 else if (gl_state.units[j].pointer_texcoord_gltype == GL_UNSIGNED_BYTE)
2990 const GLubyte *sb = (const GLubyte *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2991 if (gl_state.units[j].pointer_texcoord_components == 4)
2992 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));
2993 else if (gl_state.units[j].pointer_texcoord_components == 3)
2994 qglVertexAttrib3f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 255.0f), sb[1] * (1.0f / 255.0f), sb[2] * (1.0f / 255.0f));
2995 else if (gl_state.units[j].pointer_texcoord_components == 2)
2996 qglVertexAttrib2f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 255.0f), sb[1] * (1.0f / 255.0f));
2997 else if (gl_state.units[j].pointer_texcoord_components == 1)
2998 qglVertexAttrib1f(GLSLATTRIB_TEXCOORD0 + j, sb[0] * (1.0f / 255.0f));
3000 else if (gl_state.units[j].pointer_texcoord_gltype == (int)(GL_UNSIGNED_BYTE | 0x80000000))
3002 const GLubyte *sb = (const GLubyte *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
3003 if (gl_state.units[j].pointer_texcoord_components == 4)
3004 qglVertexAttrib4f(GLSLATTRIB_TEXCOORD0 + j, sb[0], sb[1], sb[2], sb[3]);
3005 else if (gl_state.units[j].pointer_texcoord_components == 3)
3006 qglVertexAttrib3f(GLSLATTRIB_TEXCOORD0 + j, sb[0], sb[1], sb[2]);
3007 else if (gl_state.units[j].pointer_texcoord_components == 2)
3008 qglVertexAttrib2f(GLSLATTRIB_TEXCOORD0 + j, sb[0], sb[1]);
3009 else if (gl_state.units[j].pointer_texcoord_components == 1)
3010 qglVertexAttrib1f(GLSLATTRIB_TEXCOORD0 + j, sb[0]);
3014 if (gl_state.pointer_color_pointer && gl_state.pointer_color_enabled && gl_state.pointer_color_components == 4)
3016 if (gl_state.pointer_color_gltype == GL_FLOAT)
3018 p = (const GLfloat *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
3019 qglVertexAttrib4f(GLSLATTRIB_COLOR, p[0], p[1], p[2], p[3]);
3021 else if (gl_state.pointer_color_gltype == GL_UNSIGNED_BYTE)
3023 const GLubyte *ub = (const GLubyte *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
3024 qglVertexAttrib4Nub(GLSLATTRIB_COLOR, ub[0], ub[1], ub[2], ub[3]);
3027 if (gl_state.pointer_vertex_gltype == GL_FLOAT)
3029 p = (const GLfloat *)((const unsigned char *)gl_state.pointer_vertex_pointer + element * gl_state.pointer_vertex_stride);
3030 if (gl_state.pointer_vertex_components == 4)
3031 qglVertexAttrib4f(GLSLATTRIB_POSITION, p[0], p[1], p[2], p[3]);
3032 else if (gl_state.pointer_vertex_components == 3)
3033 qglVertexAttrib3f(GLSLATTRIB_POSITION, p[0], p[1], p[2]);
3035 qglVertexAttrib2f(GLSLATTRIB_POSITION, p[0], p[1]);
3041 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
3044 element = element3i[i];
3046 element = element3s[i];
3048 element = firstvertex + i;
3049 for (j = 0;j < vid.texarrayunits;j++)
3051 if (gl_state.units[j].pointer_texcoord_pointer && gl_state.units[j].arrayenabled)
3053 if (gl_state.units[j].pointer_texcoord_gltype == GL_FLOAT)
3055 p = (const GLfloat *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
3056 if (vid.texarrayunits > 1)
3058 if (gl_state.units[j].pointer_texcoord_components == 4)
3059 qglMultiTexCoord4f(GL_TEXTURE0 + j, p[0], p[1], p[2], p[3]);
3060 else if (gl_state.units[j].pointer_texcoord_components == 3)
3061 qglMultiTexCoord3f(GL_TEXTURE0 + j, p[0], p[1], p[2]);
3062 else if (gl_state.units[j].pointer_texcoord_components == 2)
3063 qglMultiTexCoord2f(GL_TEXTURE0 + j, p[0], p[1]);
3065 qglMultiTexCoord1f(GL_TEXTURE0 + j, p[0]);
3069 if (gl_state.units[j].pointer_texcoord_components == 4)
3070 qglTexCoord4f(p[0], p[1], p[2], p[3]);
3071 else if (gl_state.units[j].pointer_texcoord_components == 3)
3072 qglTexCoord3f(p[0], p[1], p[2]);
3073 else if (gl_state.units[j].pointer_texcoord_components == 2)
3074 qglTexCoord2f(p[0], p[1]);
3076 qglTexCoord1f(p[0]);
3079 else if (gl_state.units[j].pointer_texcoord_gltype == GL_SHORT)
3081 const GLshort *s = (const GLshort *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
3082 if (vid.texarrayunits > 1)
3084 if (gl_state.units[j].pointer_texcoord_components == 4)
3085 qglMultiTexCoord4f(GL_TEXTURE0 + j, s[0], s[1], s[2], s[3]);
3086 else if (gl_state.units[j].pointer_texcoord_components == 3)
3087 qglMultiTexCoord3f(GL_TEXTURE0 + j, s[0], s[1], s[2]);
3088 else if (gl_state.units[j].pointer_texcoord_components == 2)
3089 qglMultiTexCoord2f(GL_TEXTURE0 + j, s[0], s[1]);
3090 else if (gl_state.units[j].pointer_texcoord_components == 1)
3091 qglMultiTexCoord1f(GL_TEXTURE0 + j, s[0]);
3095 if (gl_state.units[j].pointer_texcoord_components == 4)
3096 qglTexCoord4f(s[0], s[1], s[2], s[3]);
3097 else if (gl_state.units[j].pointer_texcoord_components == 3)
3098 qglTexCoord3f(s[0], s[1], s[2]);
3099 else if (gl_state.units[j].pointer_texcoord_components == 2)
3100 qglTexCoord2f(s[0], s[1]);
3101 else if (gl_state.units[j].pointer_texcoord_components == 1)
3102 qglTexCoord1f(s[0]);
3105 else if (gl_state.units[j].pointer_texcoord_gltype == GL_BYTE)
3107 const GLbyte *sb = (const GLbyte *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
3108 if (vid.texarrayunits > 1)
3110 if (gl_state.units[j].pointer_texcoord_components == 4)
3111 qglMultiTexCoord4f(GL_TEXTURE0 + j, sb[0], sb[1], sb[2], sb[3]);
3112 else if (gl_state.units[j].pointer_texcoord_components == 3)
3113 qglMultiTexCoord3f(GL_TEXTURE0 + j, sb[0], sb[1], sb[2]);
3114 else if (gl_state.units[j].pointer_texcoord_components == 2)
3115 qglMultiTexCoord2f(GL_TEXTURE0 + j, sb[0], sb[1]);
3116 else if (gl_state.units[j].pointer_texcoord_components == 1)
3117 qglMultiTexCoord1f(GL_TEXTURE0 + j, sb[0]);
3121 if (gl_state.units[j].pointer_texcoord_components == 4)
3122 qglTexCoord4f(sb[0], sb[1], sb[2], sb[3]);
3123 else if (gl_state.units[j].pointer_texcoord_components == 3)
3124 qglTexCoord3f(sb[0], sb[1], sb[2]);
3125 else if (gl_state.units[j].pointer_texcoord_components == 2)
3126 qglTexCoord2f(sb[0], sb[1]);
3127 else if (gl_state.units[j].pointer_texcoord_components == 1)
3128 qglTexCoord1f(sb[0]);
3133 if (gl_state.pointer_color_pointer && gl_state.pointer_color_enabled && gl_state.pointer_color_components == 4)
3135 if (gl_state.pointer_color_gltype == GL_FLOAT)
3137 p = (const GLfloat *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
3138 qglColor4f(p[0], p[1], p[2], p[3]);
3140 else if (gl_state.pointer_color_gltype == GL_UNSIGNED_BYTE)
3142 const GLubyte *ub = (const GLubyte *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
3143 qglColor4ub(ub[0], ub[1], ub[2], ub[3]);
3146 if (gl_state.pointer_vertex_gltype == GL_FLOAT)
3148 p = (const GLfloat *)((const unsigned char *)gl_state.pointer_vertex_pointer + element * gl_state.pointer_vertex_stride);
3149 if (gl_state.pointer_vertex_components == 4)
3150 qglVertex4f(p[0], p[1], p[2], p[3]);
3151 else if (gl_state.pointer_vertex_components == 3)
3152 qglVertex3f(p[0], p[1], p[2]);
3154 qglVertex2f(p[0], p[1]);
3162 else if (bufferobject3s)
3164 GL_BindEBO(bufferobject3s);
3166 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
3168 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, (void *)bufferoffset3s);
3174 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, (void *)bufferoffset3s);
3178 else if (bufferobject3i)
3180 GL_BindEBO(bufferobject3i);
3182 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
3184 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, (void *)bufferoffset3i);
3190 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (void *)bufferoffset3i);
3198 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
3200 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, element3s);
3206 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
3214 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
3216 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, element3i);
3222 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
3228 qglDrawArrays(GL_TRIANGLES, firstvertex, numvertices);
3232 case RENDERPATH_D3D9:
3234 if (gl_state.d3dvertexbuffer && ((element3s && element3s_indexbuffer) || (element3i && element3i_indexbuffer)))
3236 if (element3s_indexbuffer)
3238 IDirect3DDevice9_SetIndices(vid_d3d9dev, (IDirect3DIndexBuffer9 *)element3s_indexbuffer->devicebuffer);
3239 IDirect3DDevice9_DrawIndexedPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, 0, firstvertex, numvertices, element3s_bufferoffset>>1, numtriangles);
3241 else if (element3i_indexbuffer)
3243 IDirect3DDevice9_SetIndices(vid_d3d9dev, (IDirect3DIndexBuffer9 *)element3i_indexbuffer->devicebuffer);
3244 IDirect3DDevice9_DrawIndexedPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, 0, firstvertex, numvertices, element3i_bufferoffset>>2, numtriangles);
3247 IDirect3DDevice9_DrawPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, firstvertex, numvertices);
3252 IDirect3DDevice9_DrawIndexedPrimitiveUP(vid_d3d9dev, D3DPT_TRIANGLELIST, firstvertex, numvertices, numtriangles, element3s, D3DFMT_INDEX16, gl_state.d3dvertexdata, gl_state.d3dvertexsize);
3254 IDirect3DDevice9_DrawIndexedPrimitiveUP(vid_d3d9dev, D3DPT_TRIANGLELIST, firstvertex, numvertices, numtriangles, element3i, D3DFMT_INDEX32, gl_state.d3dvertexdata, gl_state.d3dvertexsize);
3256 IDirect3DDevice9_DrawPrimitiveUP(vid_d3d9dev, D3DPT_TRIANGLELIST, numvertices, (void *)gl_state.d3dvertexdata, gl_state.d3dvertexsize);
3260 case RENDERPATH_D3D10:
3261 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3263 case RENDERPATH_D3D11:
3264 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3266 case RENDERPATH_SOFT:
3267 DPSOFTRAST_DrawTriangles(firstvertex, numvertices, numtriangles, element3i, element3s);
3269 case RENDERPATH_GLES1:
3270 case RENDERPATH_GLES2:
3271 // GLES does not have glDrawRangeElements so this is a bit shorter than the GL20 path
3274 GL_BindEBO(bufferobject3s);
3275 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, (void *)bufferoffset3s);
3278 else if (bufferobject3i)
3280 GL_BindEBO(bufferobject3i);
3281 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (void *)bufferoffset3i);
3287 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
3293 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
3298 qglDrawArrays(GL_TRIANGLES, firstvertex, numvertices);
3306 // restores backend state, used when done with 3D rendering
3307 void R_Mesh_Finish(void)
3309 R_Mesh_SetRenderTargets(0, NULL, NULL, NULL, NULL, NULL);
3312 r_meshbuffer_t *R_Mesh_CreateMeshBuffer(const void *data, size_t size, const char *name, qboolean isindexbuffer, qboolean isuniformbuffer, qboolean isdynamic, qboolean isindex16)
3314 r_meshbuffer_t *buffer;
3315 if (isuniformbuffer)
3317 if (!vid.support.arb_uniform_buffer_object)
3322 if (!vid.support.arb_vertex_buffer_object)
3324 if (!isdynamic && !(isindexbuffer ? gl_state.usevbo_staticindex : gl_state.usevbo_staticvertex))
3327 buffer = (r_meshbuffer_t *)Mem_ExpandableArray_AllocRecord(&gl_state.meshbufferarray);
3328 memset(buffer, 0, sizeof(*buffer));
3329 buffer->bufferobject = 0;
3330 buffer->devicebuffer = NULL;
3331 buffer->size = size;
3332 buffer->isindexbuffer = isindexbuffer;
3333 buffer->isuniformbuffer = isuniformbuffer;
3334 buffer->isdynamic = isdynamic;
3335 buffer->isindex16 = isindex16;
3336 strlcpy(buffer->name, name, sizeof(buffer->name));
3337 R_Mesh_UpdateMeshBuffer(buffer, data, size, false, 0);
3341 void R_Mesh_UpdateMeshBuffer(r_meshbuffer_t *buffer, const void *data, size_t size, qboolean subdata, size_t offset)
3345 if (buffer->isindexbuffer)
3347 r_refdef.stats[r_stat_indexbufferuploadcount]++;
3348 r_refdef.stats[r_stat_indexbufferuploadsize] += (int)size;
3352 r_refdef.stats[r_stat_vertexbufferuploadcount]++;
3353 r_refdef.stats[r_stat_vertexbufferuploadsize] += (int)size;
3356 buffer->size = size;
3357 switch(vid.renderpath)
3359 case RENDERPATH_GL11:
3360 case RENDERPATH_GL13:
3361 case RENDERPATH_GL20:
3362 case RENDERPATH_GLES1:
3363 case RENDERPATH_GLES2:
3364 if (!buffer->bufferobject)
3365 qglGenBuffersARB(1, (GLuint *)&buffer->bufferobject);
3366 if (buffer->isuniformbuffer)
3367 GL_BindUBO(buffer->bufferobject);
3368 else if (buffer->isindexbuffer)
3369 GL_BindEBO(buffer->bufferobject);
3371 GL_BindVBO(buffer->bufferobject);
3375 buffertype = buffer->isindexbuffer ? GL_ELEMENT_ARRAY_BUFFER : GL_ARRAY_BUFFER;
3376 #ifdef GL_UNIFORM_BUFFER
3377 if (buffer->isuniformbuffer)
3378 buffertype = GL_UNIFORM_BUFFER;
3381 qglBufferSubDataARB(buffertype, offset, size, data);
3383 qglBufferDataARB(buffertype, size, data, buffer->isdynamic ? GL_STREAM_DRAW : GL_STATIC_DRAW);
3385 if (buffer->isuniformbuffer)
3388 case RENDERPATH_D3D9:
3392 void *datapointer = NULL;
3393 if (buffer->isindexbuffer)
3395 IDirect3DIndexBuffer9 *d3d9indexbuffer = (IDirect3DIndexBuffer9 *)buffer->devicebuffer;
3396 if (offset+size > buffer->size || !buffer->devicebuffer)
3398 if (buffer->devicebuffer)
3399 IDirect3DIndexBuffer9_Release((IDirect3DIndexBuffer9*)buffer->devicebuffer);
3400 buffer->devicebuffer = NULL;
3401 if (FAILED(result = IDirect3DDevice9_CreateIndexBuffer(vid_d3d9dev, (unsigned int)(offset+size), buffer->isdynamic ? D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC : 0, buffer->isindex16 ? D3DFMT_INDEX16 : D3DFMT_INDEX32, buffer->isdynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED, &d3d9indexbuffer, NULL)))
3402 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);
3403 buffer->devicebuffer = (void *)d3d9indexbuffer;
3404 buffer->size = offset+size;
3406 if (!FAILED(IDirect3DIndexBuffer9_Lock(d3d9indexbuffer, (unsigned int)offset, (unsigned int)size, &datapointer, buffer->isdynamic ? D3DLOCK_DISCARD : 0)))
3409 memcpy(datapointer, data, size);
3411 memset(datapointer, 0, size);
3412 IDirect3DIndexBuffer9_Unlock(d3d9indexbuffer);
3417 IDirect3DVertexBuffer9 *d3d9vertexbuffer = (IDirect3DVertexBuffer9 *)buffer->devicebuffer;
3418 if (offset+size > buffer->size || !buffer->devicebuffer)
3420 if (buffer->devicebuffer)
3421 IDirect3DVertexBuffer9_Release((IDirect3DVertexBuffer9*)buffer->devicebuffer);
3422 buffer->devicebuffer = NULL;
3423 if (FAILED(result = IDirect3DDevice9_CreateVertexBuffer(vid_d3d9dev, (unsigned int)(offset+size), buffer->isdynamic ? D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC : 0, 0, buffer->isdynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED, &d3d9vertexbuffer, NULL)))
3424 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);
3425 buffer->devicebuffer = (void *)d3d9vertexbuffer;
3426 buffer->size = offset+size;
3428 if (!FAILED(IDirect3DVertexBuffer9_Lock(d3d9vertexbuffer, (unsigned int)offset, (unsigned int)size, &datapointer, buffer->isdynamic ? D3DLOCK_DISCARD : 0)))
3431 memcpy(datapointer, data, size);
3433 memset(datapointer, 0, size);
3434 IDirect3DVertexBuffer9_Unlock(d3d9vertexbuffer);
3440 case RENDERPATH_D3D10:
3441 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3443 case RENDERPATH_D3D11:
3444 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3446 case RENDERPATH_SOFT:
3451 void R_Mesh_DestroyMeshBuffer(r_meshbuffer_t *buffer)
3455 switch(vid.renderpath)
3457 case RENDERPATH_GL11:
3458 case RENDERPATH_GL13:
3459 case RENDERPATH_GL20:
3460 case RENDERPATH_GLES1:
3461 case RENDERPATH_GLES2:
3462 // GL clears the binding if we delete something bound
3463 if (gl_state.uniformbufferobject == buffer->bufferobject)
3464 gl_state.uniformbufferobject = 0;
3465 if (gl_state.vertexbufferobject == buffer->bufferobject)
3466 gl_state.vertexbufferobject = 0;
3467 if (gl_state.elementbufferobject == buffer->bufferobject)
3468 gl_state.elementbufferobject = 0;
3469 qglDeleteBuffersARB(1, (GLuint *)&buffer->bufferobject);
3471 case RENDERPATH_D3D9:
3473 if (gl_state.d3dvertexbuffer == (void *)buffer)
3474 gl_state.d3dvertexbuffer = NULL;
3475 if (buffer->devicebuffer)
3477 if (buffer->isindexbuffer)
3478 IDirect3DIndexBuffer9_Release((IDirect3DIndexBuffer9 *)buffer->devicebuffer);
3480 IDirect3DVertexBuffer9_Release((IDirect3DVertexBuffer9 *)buffer->devicebuffer);
3481 buffer->devicebuffer = NULL;
3485 case RENDERPATH_D3D10:
3486 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3488 case RENDERPATH_D3D11:
3489 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3491 case RENDERPATH_SOFT:
3494 Mem_ExpandableArray_FreeRecord(&gl_state.meshbufferarray, (void *)buffer);
3497 static const char *buffertypename[R_BUFFERDATA_COUNT] = {"vertex", "index16", "index32", "uniform"};
3498 void GL_Mesh_ListVBOs(qboolean printeach)
3503 int index16count, index16mem;
3504 int index32count, index32mem;
3505 int vertexcount, vertexmem;
3506 int uniformcount, uniformmem;
3507 int totalcount, totalmem;
3508 size_t bufferstat[R_BUFFERDATA_COUNT][2][2];
3509 r_meshbuffer_t *buffer;
3510 memset(bufferstat, 0, sizeof(bufferstat));
3511 endindex = (int)Mem_ExpandableArray_IndexRange(&gl_state.meshbufferarray);
3512 for (i = 0;i < endindex;i++)
3514 buffer = (r_meshbuffer_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.meshbufferarray, i);
3517 if (buffer->isuniformbuffer)
3518 type = R_BUFFERDATA_UNIFORM;
3519 else if (buffer->isindexbuffer && buffer->isindex16)
3520 type = R_BUFFERDATA_INDEX16;
3521 else if (buffer->isindexbuffer)
3522 type = R_BUFFERDATA_INDEX32;
3524 type = R_BUFFERDATA_VERTEX;
3525 isdynamic = buffer->isdynamic;
3526 bufferstat[type][isdynamic][0]++;
3527 bufferstat[type][isdynamic][1] += buffer->size;
3529 Con_Printf("buffer #%i %s = %i bytes (%s %s)\n", i, buffer->name, (int)buffer->size, isdynamic ? "dynamic" : "static", buffertypename[type]);
3531 index16count = (int)(bufferstat[R_BUFFERDATA_INDEX16][0][0] + bufferstat[R_BUFFERDATA_INDEX16][1][0]);
3532 index16mem = (int)(bufferstat[R_BUFFERDATA_INDEX16][0][1] + bufferstat[R_BUFFERDATA_INDEX16][1][1]);
3533 index32count = (int)(bufferstat[R_BUFFERDATA_INDEX32][0][0] + bufferstat[R_BUFFERDATA_INDEX32][1][0]);
3534 index32mem = (int)(bufferstat[R_BUFFERDATA_INDEX32][0][1] + bufferstat[R_BUFFERDATA_INDEX32][1][1]);
3535 vertexcount = (int)(bufferstat[R_BUFFERDATA_VERTEX ][0][0] + bufferstat[R_BUFFERDATA_VERTEX ][1][0]);
3536 vertexmem = (int)(bufferstat[R_BUFFERDATA_VERTEX ][0][1] + bufferstat[R_BUFFERDATA_VERTEX ][1][1]);
3537 uniformcount = (int)(bufferstat[R_BUFFERDATA_UNIFORM][0][0] + bufferstat[R_BUFFERDATA_UNIFORM][1][0]);
3538 uniformmem = (int)(bufferstat[R_BUFFERDATA_UNIFORM][0][1] + bufferstat[R_BUFFERDATA_UNIFORM][1][1]);
3539 totalcount = index16count + index32count + vertexcount + uniformcount;
3540 totalmem = index16mem + index32mem + vertexmem + uniformmem;
3541 Con_Printf("%i 16bit indexbuffers totalling %i bytes (%.3f MB)\n%i 32bit indexbuffers totalling %i bytes (%.3f MB)\n%i vertexbuffers totalling %i bytes (%.3f MB)\n%i uniformbuffers totalling %i bytes (%.3f MB)\ncombined %i buffers totalling %i bytes (%.3fMB)\n", index16count, index16mem, index16mem / 10248576.0, index32count, index32mem, index32mem / 10248576.0, vertexcount, vertexmem, vertexmem / 10248576.0, uniformcount, uniformmem, uniformmem / 10248576.0, totalcount, totalmem, totalmem / 10248576.0);
3546 void R_Mesh_VertexPointer(int components, int gltype, size_t stride, const void *pointer, const r_meshbuffer_t *vertexbuffer, size_t bufferoffset)
3548 switch(vid.renderpath)
3550 case RENDERPATH_GL11:
3551 case RENDERPATH_GL13:
3552 case RENDERPATH_GLES1:
3554 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)
3556 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3557 gl_state.pointer_vertex_components = components;
3558 gl_state.pointer_vertex_gltype = gltype;
3559 gl_state.pointer_vertex_stride = stride;
3560 gl_state.pointer_vertex_pointer = pointer;
3561 gl_state.pointer_vertex_vertexbuffer = vertexbuffer;
3562 gl_state.pointer_vertex_offset = bufferoffset;
3564 GL_BindVBO(bufferobject);
3565 qglVertexPointer(components, gltype, (GLsizei)stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3569 case RENDERPATH_GL20:
3570 case RENDERPATH_GLES2:
3571 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)
3573 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3574 gl_state.pointer_vertex_components = components;
3575 gl_state.pointer_vertex_gltype = gltype;
3576 gl_state.pointer_vertex_stride = stride;
3577 gl_state.pointer_vertex_pointer = pointer;
3578 gl_state.pointer_vertex_vertexbuffer = vertexbuffer;
3579 gl_state.pointer_vertex_offset = bufferoffset;
3581 GL_BindVBO(bufferobject);
3582 // LordHavoc: special flag added to gltype for unnormalized types
3583 qglVertexAttribPointer(GLSLATTRIB_POSITION, components, gltype & ~0x80000000, (gltype & 0x80000000) == 0, (GLsizei)stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3586 case RENDERPATH_D3D9:
3587 case RENDERPATH_D3D10:
3588 case RENDERPATH_D3D11:
3589 case RENDERPATH_SOFT:
3594 void R_Mesh_ColorPointer(int components, int gltype, size_t stride, const void *pointer, const r_meshbuffer_t *vertexbuffer, size_t bufferoffset)
3596 // note: vertexbuffer may be non-NULL even if pointer is NULL, so check
3597 // the pointer only.
3598 switch(vid.renderpath)
3600 case RENDERPATH_GL11:
3601 case RENDERPATH_GL13:
3602 case RENDERPATH_GLES1:
3607 // caller wants color array enabled
3608 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3609 if (!gl_state.pointer_color_enabled)
3611 gl_state.pointer_color_enabled = true;
3613 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
3615 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)
3617 gl_state.pointer_color_components = components;
3618 gl_state.pointer_color_gltype = gltype;
3619 gl_state.pointer_color_stride = stride;
3620 gl_state.pointer_color_pointer = pointer;
3621 gl_state.pointer_color_vertexbuffer = vertexbuffer;
3622 gl_state.pointer_color_offset = bufferoffset;
3624 GL_BindVBO(bufferobject);
3625 qglColorPointer(components, gltype, (GLsizei)stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3630 // caller wants color array disabled
3631 if (gl_state.pointer_color_enabled)
3633 gl_state.pointer_color_enabled = false;
3635 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
3636 // when color array is on the glColor gets trashed, set it again
3637 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
3642 case RENDERPATH_GL20:
3643 case RENDERPATH_GLES2:
3647 // caller wants color array enabled
3648 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3649 if (!gl_state.pointer_color_enabled)
3651 gl_state.pointer_color_enabled = true;
3653 qglEnableVertexAttribArray(GLSLATTRIB_COLOR);CHECKGLERROR
3655 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)
3657 gl_state.pointer_color_components = components;
3658 gl_state.pointer_color_gltype = gltype;
3659 gl_state.pointer_color_stride = stride;
3660 gl_state.pointer_color_pointer = pointer;
3661 gl_state.pointer_color_vertexbuffer = vertexbuffer;
3662 gl_state.pointer_color_offset = bufferoffset;
3664 GL_BindVBO(bufferobject);
3665 // LordHavoc: special flag added to gltype for unnormalized types
3666 qglVertexAttribPointer(GLSLATTRIB_COLOR, components, gltype & ~0x80000000, (gltype & 0x80000000) == 0, (GLsizei)stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3671 // caller wants color array disabled
3672 if (gl_state.pointer_color_enabled)
3674 gl_state.pointer_color_enabled = false;
3676 qglDisableVertexAttribArray(GLSLATTRIB_COLOR);CHECKGLERROR
3677 // when color array is on the glColor gets trashed, set it again
3678 qglVertexAttrib4f(GLSLATTRIB_COLOR, gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
3682 case RENDERPATH_D3D9:
3683 case RENDERPATH_D3D10:
3684 case RENDERPATH_D3D11:
3685 case RENDERPATH_SOFT:
3690 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)
3692 gltextureunit_t *unit = gl_state.units + unitnum;
3693 // update array settings
3694 // note: there is no need to check bufferobject here because all cases
3695 // that involve a valid bufferobject also supply a texcoord array
3696 switch(vid.renderpath)
3698 case RENDERPATH_GL11:
3699 case RENDERPATH_GL13:
3700 case RENDERPATH_GLES1:
3705 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3706 // texture array unit is enabled, enable the array
3707 if (!unit->arrayenabled)
3709 unit->arrayenabled = true;
3710 GL_ClientActiveTexture(unitnum);
3711 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
3714 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)
3716 unit->pointer_texcoord_components = components;
3717 unit->pointer_texcoord_gltype = gltype;
3718 unit->pointer_texcoord_stride = stride;
3719 unit->pointer_texcoord_pointer = pointer;
3720 unit->pointer_texcoord_vertexbuffer = vertexbuffer;
3721 unit->pointer_texcoord_offset = bufferoffset;
3722 GL_ClientActiveTexture(unitnum);
3723 GL_BindVBO(bufferobject);
3724 qglTexCoordPointer(components, gltype, (GLsizei)stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3729 // texture array unit is disabled, disable the array
3730 if (unit->arrayenabled)
3732 unit->arrayenabled = false;
3733 GL_ClientActiveTexture(unitnum);
3734 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
3739 case RENDERPATH_GL20:
3740 case RENDERPATH_GLES2:
3744 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
3745 // texture array unit is enabled, enable the array
3746 if (!unit->arrayenabled)
3748 unit->arrayenabled = true;
3749 qglEnableVertexAttribArray(unitnum+GLSLATTRIB_TEXCOORD0);CHECKGLERROR
3752 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)
3754 unit->pointer_texcoord_components = components;
3755 unit->pointer_texcoord_gltype = gltype;
3756 unit->pointer_texcoord_stride = stride;
3757 unit->pointer_texcoord_pointer = pointer;
3758 unit->pointer_texcoord_vertexbuffer = vertexbuffer;
3759 unit->pointer_texcoord_offset = bufferoffset;
3760 GL_BindVBO(bufferobject);
3761 // LordHavoc: special flag added to gltype for unnormalized types
3762 qglVertexAttribPointer(unitnum+GLSLATTRIB_TEXCOORD0, components, gltype & ~0x80000000, (gltype & 0x80000000) == 0, (GLsizei)stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
3767 // texture array unit is disabled, disable the array
3768 if (unit->arrayenabled)
3770 unit->arrayenabled = false;
3771 qglDisableVertexAttribArray(unitnum+GLSLATTRIB_TEXCOORD0);CHECKGLERROR
3775 case RENDERPATH_D3D9:
3776 case RENDERPATH_D3D10:
3777 case RENDERPATH_D3D11:
3778 case RENDERPATH_SOFT:
3783 int R_Mesh_TexBound(unsigned int unitnum, int id)
3785 gltextureunit_t *unit = gl_state.units + unitnum;
3786 if (unitnum >= vid.teximageunits)
3788 if (id == GL_TEXTURE_2D)
3790 if (id == GL_TEXTURE_3D)
3792 if (id == GL_TEXTURE_CUBE_MAP)
3793 return unit->tcubemap;
3797 void R_Mesh_CopyToTexture(rtexture_t *tex, int tx, int ty, int sx, int sy, int width, int height)
3799 switch(vid.renderpath)
3801 case RENDERPATH_GL11:
3802 case RENDERPATH_GL13:
3803 case RENDERPATH_GL20:
3804 case RENDERPATH_GLES1:
3805 case RENDERPATH_GLES2:
3806 R_Mesh_TexBind(0, tex);
3807 GL_ActiveTexture(0);CHECKGLERROR
3808 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, tx, ty, sx, sy, width, height);CHECKGLERROR
3810 case RENDERPATH_D3D9:
3813 IDirect3DSurface9 *currentsurface = NULL;
3814 IDirect3DSurface9 *texturesurface = NULL;
3817 sourcerect.left = sx;
3818 sourcerect.top = sy;
3819 sourcerect.right = sx + width;
3820 sourcerect.bottom = sy + height;
3823 destrect.right = tx + width;
3824 destrect.bottom = ty + height;
3825 if (!FAILED(IDirect3DTexture9_GetSurfaceLevel(((IDirect3DTexture9 *)tex->d3dtexture), 0, &texturesurface)))
3827 if (!FAILED(IDirect3DDevice9_GetRenderTarget(vid_d3d9dev, 0, ¤tsurface)))
3829 IDirect3DDevice9_StretchRect(vid_d3d9dev, currentsurface, &sourcerect, texturesurface, &destrect, D3DTEXF_NONE);
3830 IDirect3DSurface9_Release(currentsurface);
3832 IDirect3DSurface9_Release(texturesurface);
3837 case RENDERPATH_D3D10:
3838 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3840 case RENDERPATH_D3D11:
3841 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3843 case RENDERPATH_SOFT:
3844 DPSOFTRAST_CopyRectangleToTexture(tex->texnum, 0, tx, ty, sx, sy, width, height);
3850 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};
3853 void R_Mesh_ClearBindingsForTexture(int texnum)
3855 gltextureunit_t *unit;
3856 unsigned int unitnum;
3857 // 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)
3858 for (unitnum = 0;unitnum < vid.teximageunits;unitnum++)
3860 unit = gl_state.units + unitnum;
3861 if (unit->t2d == texnum)
3863 if (unit->t3d == texnum)
3865 if (unit->tcubemap == texnum)
3866 unit->tcubemap = -1;
3870 void R_Mesh_TexBind(unsigned int unitnum, rtexture_t *tex)
3872 gltextureunit_t *unit = gl_state.units + unitnum;
3873 int tex2d, tex3d, texcubemap, texnum;
3874 if (unitnum >= vid.teximageunits)
3876 // if (unit->texture == tex)
3878 switch(vid.renderpath)
3880 case RENDERPATH_GL20:
3881 case RENDERPATH_GLES2:
3884 tex = r_texture_white;
3885 // not initialized enough yet...
3889 unit->texture = tex;
3890 texnum = R_GetTexture(tex);
3891 switch(tex->gltexturetypeenum)
3893 case GL_TEXTURE_2D: if (unit->t2d != texnum) {GL_ActiveTexture(unitnum);unit->t2d = texnum;qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR}break;
3894 case GL_TEXTURE_3D: if (unit->t3d != texnum) {GL_ActiveTexture(unitnum);unit->t3d = texnum;qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR}break;
3895 case GL_TEXTURE_CUBE_MAP: if (unit->tcubemap != texnum) {GL_ActiveTexture(unitnum);unit->tcubemap = texnum;qglBindTexture(GL_TEXTURE_CUBE_MAP, unit->tcubemap);CHECKGLERROR}break;
3898 case RENDERPATH_GL11:
3899 case RENDERPATH_GL13:
3900 case RENDERPATH_GLES1:
3901 unit->texture = tex;
3907 texnum = R_GetTexture(tex);
3908 switch(tex->gltexturetypeenum)
3916 case GL_TEXTURE_CUBE_MAP:
3917 texcubemap = texnum;
3921 // update 2d texture binding
3922 if (unit->t2d != tex2d)
3924 GL_ActiveTexture(unitnum);
3929 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
3936 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
3940 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
3942 // update 3d texture binding
3943 if (unit->t3d != tex3d)
3945 GL_ActiveTexture(unitnum);
3950 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
3957 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
3961 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
3963 // update cubemap texture binding
3964 if (unit->tcubemap != texcubemap)
3966 GL_ActiveTexture(unitnum);
3969 if (unit->tcubemap == 0)
3971 qglEnable(GL_TEXTURE_CUBE_MAP);CHECKGLERROR
3978 qglDisable(GL_TEXTURE_CUBE_MAP);CHECKGLERROR
3981 unit->tcubemap = texcubemap;
3982 qglBindTexture(GL_TEXTURE_CUBE_MAP, unit->tcubemap);CHECKGLERROR
3985 case RENDERPATH_D3D9:
3988 extern cvar_t gl_texture_anisotropy;
3991 tex = r_texture_white;
3992 // not initialized enough yet...
3996 // upload texture if needed
3998 if (unit->texture == tex)
4000 unit->texture = tex;
4001 IDirect3DDevice9_SetTexture(vid_d3d9dev, unitnum, (IDirect3DBaseTexture9*)tex->d3dtexture);
4002 //IDirect3DDevice9_SetRenderState(vid_d3d9dev, d3drswrap[unitnum], (tex->flags & TEXF_CLAMP) ? (D3DWRAPCOORD_0 | D3DWRAPCOORD_1 | D3DWRAPCOORD_2) : 0);
4003 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSU, tex->d3daddressu);
4004 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSV, tex->d3daddressv);
4005 if (tex->d3daddressw)
4006 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSW, tex->d3daddressw);
4007 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAGFILTER, tex->d3dmagfilter);
4008 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MINFILTER, tex->d3dminfilter);
4009 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MIPFILTER, tex->d3dmipfilter);
4010 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MIPMAPLODBIAS, tex->d3dmipmaplodbias);
4011 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAXMIPLEVEL, tex->d3dmaxmiplevelfilter);
4012 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAXANISOTROPY, gl_texture_anisotropy.integer);
4016 case RENDERPATH_D3D10:
4017 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4019 case RENDERPATH_D3D11:
4020 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4022 case RENDERPATH_SOFT:
4025 tex = r_texture_white;
4026 // not initialized enough yet...
4030 texnum = R_GetTexture(tex);
4031 if (unit->texture == tex)
4033 unit->texture = tex;
4034 DPSOFTRAST_SetTexture(unitnum, texnum);
4039 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
4041 switch(vid.renderpath)
4043 case RENDERPATH_GL11:
4044 case RENDERPATH_GL13:
4045 case RENDERPATH_GL20:
4046 case RENDERPATH_GLES1:
4047 case RENDERPATH_GLES2:
4049 if (matrix && matrix->m[3][3])
4051 gltextureunit_t *unit = gl_state.units + unitnum;
4052 // texmatrix specified, check if it is different
4053 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
4056 unit->texmatrixenabled = true;
4057 unit->matrix = *matrix;
4059 Matrix4x4_ToArrayFloatGL(&unit->matrix, glmatrix);
4060 GL_ActiveTexture(unitnum);
4061 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
4062 qglLoadMatrixf(glmatrix);CHECKGLERROR
4063 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
4068 // no texmatrix specified, revert to identity
4069 gltextureunit_t *unit = gl_state.units + unitnum;
4070 if (unit->texmatrixenabled)
4072 unit->texmatrixenabled = false;
4073 unit->matrix = identitymatrix;
4075 GL_ActiveTexture(unitnum);
4076 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
4077 qglLoadIdentity();CHECKGLERROR
4078 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
4083 case RENDERPATH_D3D9:
4084 case RENDERPATH_D3D10:
4085 case RENDERPATH_D3D11:
4087 case RENDERPATH_SOFT:
4092 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
4094 #if defined(GL_TEXTURE_ENV) && !defined(USE_GLES2)
4095 gltextureunit_t *unit = gl_state.units + unitnum;
4097 switch(vid.renderpath)
4099 case RENDERPATH_GL20:
4100 case RENDERPATH_GLES2:
4103 case RENDERPATH_GL13:
4104 case RENDERPATH_GLES1:
4105 // GL_ARB_texture_env_combine
4107 combinergb = GL_MODULATE;
4109 combinealpha = GL_MODULATE;
4114 if (combinergb != combinealpha || rgbscale != 1 || alphascale != 1)
4116 if (combinergb == GL_DECAL)
4117 combinergb = GL_INTERPOLATE;
4118 if (unit->combine != GL_COMBINE)
4120 unit->combine = GL_COMBINE;
4121 GL_ActiveTexture(unitnum);
4122 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);CHECKGLERROR
4123 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE mode
4125 if (unit->combinergb != combinergb)
4127 unit->combinergb = combinergb;
4128 GL_ActiveTexture(unitnum);
4129 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, unit->combinergb);CHECKGLERROR
4131 if (unit->combinealpha != combinealpha)
4133 unit->combinealpha = combinealpha;
4134 GL_ActiveTexture(unitnum);
4135 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, unit->combinealpha);CHECKGLERROR
4137 if (unit->rgbscale != rgbscale)
4139 unit->rgbscale = rgbscale;
4140 GL_ActiveTexture(unitnum);
4141 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE, unit->rgbscale);CHECKGLERROR
4143 if (unit->alphascale != alphascale)
4145 unit->alphascale = alphascale;
4146 GL_ActiveTexture(unitnum);
4147 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, unit->alphascale);CHECKGLERROR
4152 if (unit->combine != combinergb)
4154 unit->combine = combinergb;
4155 GL_ActiveTexture(unitnum);
4156 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
4160 case RENDERPATH_GL11:
4163 combinergb = GL_MODULATE;
4164 if (unit->combine != combinergb)
4166 unit->combine = combinergb;
4167 GL_ActiveTexture(unitnum);
4168 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
4171 case RENDERPATH_D3D9:
4172 case RENDERPATH_D3D10:
4173 case RENDERPATH_D3D11:
4175 case RENDERPATH_SOFT:
4181 void R_Mesh_ResetTextureState(void)
4183 unsigned int unitnum;
4187 for (unitnum = 0;unitnum < vid.teximageunits;unitnum++)
4188 R_Mesh_TexBind(unitnum, NULL);
4189 for (unitnum = 0;unitnum < vid.texarrayunits;unitnum++)
4190 R_Mesh_TexCoordPointer(unitnum, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4191 switch(vid.renderpath)
4193 case RENDERPATH_GL20:
4194 case RENDERPATH_GLES2:
4195 case RENDERPATH_D3D9:
4196 case RENDERPATH_D3D10:
4197 case RENDERPATH_D3D11:
4198 case RENDERPATH_SOFT:
4200 case RENDERPATH_GL11:
4201 case RENDERPATH_GL13:
4202 case RENDERPATH_GLES1:
4203 for (unitnum = 0;unitnum < vid.texunits;unitnum++)
4205 R_Mesh_TexCombine(unitnum, GL_MODULATE, GL_MODULATE, 1, 1);
4206 R_Mesh_TexMatrix(unitnum, NULL);
4215 //#define r_vertex3f_d3d9fvf (D3DFVF_XYZ)
4216 //#define r_vertexgeneric_d3d9fvf (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
4217 //#define r_vertexmesh_d3d9fvf (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX5 | D3DFVF_TEXCOORDSIZE1(3) | D3DFVF_TEXCOORDSIZE2(3) | D3DFVF_TEXCOORDSIZE3(3))
4219 D3DVERTEXELEMENT9 r_vertex3f_d3d9elements[] =
4221 {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
4225 D3DVERTEXELEMENT9 r_vertexgeneric_d3d9elements[] =
4227 {0, (int)((size_t)&((r_vertexgeneric_t *)0)->vertex3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
4228 {0, (int)((size_t)&((r_vertexgeneric_t *)0)->color4f ), D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
4229 {0, (int)((size_t)&((r_vertexgeneric_t *)0)->texcoord2f), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
4233 D3DVERTEXELEMENT9 r_vertexmesh_d3d9elements[] =
4235 {0, (int)((size_t)&((r_vertexmesh_t *)0)->vertex3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
4236 {0, (int)((size_t)&((r_vertexmesh_t *)0)->color4f ), D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
4237 {0, (int)((size_t)&((r_vertexmesh_t *)0)->texcoordtexture2f ), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
4238 {0, (int)((size_t)&((r_vertexmesh_t *)0)->svector3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
4239 {0, (int)((size_t)&((r_vertexmesh_t *)0)->tvector3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 2},
4240 {0, (int)((size_t)&((r_vertexmesh_t *)0)->normal3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 3},
4241 {0, (int)((size_t)&((r_vertexmesh_t *)0)->texcoordlightmap2f), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 4},
4242 {0, (int)((size_t)&((r_vertexmesh_t *)0)->skeletalindex4ub ), D3DDECLTYPE_UBYTE4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 6},
4243 {0, (int)((size_t)&((r_vertexmesh_t *)0)->skeletalweight4ub ), D3DDECLTYPE_UBYTE4N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 7},
4247 IDirect3DVertexDeclaration9 *r_vertex3f_d3d9decl;
4248 IDirect3DVertexDeclaration9 *r_vertexgeneric_d3d9decl;
4249 IDirect3DVertexDeclaration9 *r_vertexmesh_d3d9decl;
4252 static void R_Mesh_InitVertexDeclarations(void)
4255 r_vertex3f_d3d9decl = NULL;
4256 r_vertexgeneric_d3d9decl = NULL;
4257 r_vertexmesh_d3d9decl = NULL;
4258 switch(vid.renderpath)
4260 case RENDERPATH_GL20:
4261 case RENDERPATH_GL13:
4262 case RENDERPATH_GL11:
4263 case RENDERPATH_GLES1:
4264 case RENDERPATH_GLES2:
4266 case RENDERPATH_D3D9:
4267 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertex3f_d3d9elements, &r_vertex3f_d3d9decl);
4268 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexgeneric_d3d9elements, &r_vertexgeneric_d3d9decl);
4269 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexmesh_d3d9elements, &r_vertexmesh_d3d9decl);
4271 case RENDERPATH_D3D10:
4272 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4274 case RENDERPATH_D3D11:
4275 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4277 case RENDERPATH_SOFT:
4283 static void R_Mesh_DestroyVertexDeclarations(void)
4286 if (r_vertex3f_d3d9decl)
4287 IDirect3DVertexDeclaration9_Release(r_vertex3f_d3d9decl);
4288 r_vertex3f_d3d9decl = NULL;
4289 if (r_vertexgeneric_d3d9decl)
4290 IDirect3DVertexDeclaration9_Release(r_vertexgeneric_d3d9decl);
4291 r_vertexgeneric_d3d9decl = NULL;
4292 if (r_vertexmesh_d3d9decl)
4293 IDirect3DVertexDeclaration9_Release(r_vertexmesh_d3d9decl);
4294 r_vertexmesh_d3d9decl = NULL;
4298 void R_Mesh_PrepareVertices_Vertex3f(int numvertices, const float *vertex3f, const r_meshbuffer_t *vertexbuffer, int bufferoffset)
4300 // upload temporary vertexbuffer for this rendering
4301 if (!gl_state.usevbo_staticvertex)
4302 vertexbuffer = NULL;
4303 if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
4304 vertexbuffer = R_BufferData_Store(numvertices * sizeof(float[3]), (void *)vertex3f, R_BUFFERDATA_VERTEX, &bufferoffset);
4305 switch(vid.renderpath)
4307 case RENDERPATH_GL20:
4308 case RENDERPATH_GLES2:
4311 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, bufferoffset);
4312 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4313 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4314 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4315 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4316 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4317 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4318 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4319 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4320 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4324 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
4325 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4326 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4327 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4328 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4329 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4330 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4331 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4332 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4333 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4336 case RENDERPATH_GL13:
4337 case RENDERPATH_GLES1:
4340 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, bufferoffset);
4341 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4342 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4343 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4347 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
4348 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4349 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4350 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4353 case RENDERPATH_GL11:
4356 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, bufferoffset);
4357 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4358 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4362 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
4363 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
4364 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4367 case RENDERPATH_D3D9:
4369 IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertex3f_d3d9decl);
4371 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, bufferoffset, sizeof(float[3]));
4373 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, NULL, 0, 0);
4374 gl_state.d3dvertexbuffer = (void *)vertexbuffer;
4375 gl_state.d3dvertexdata = (void *)vertex3f;
4376 gl_state.d3dvertexsize = sizeof(float[3]);
4379 case RENDERPATH_D3D10:
4380 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4382 case RENDERPATH_D3D11:
4383 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4385 case RENDERPATH_SOFT:
4386 DPSOFTRAST_SetVertexPointer(vertex3f, sizeof(float[3]));
4387 DPSOFTRAST_SetColorPointer(NULL, 0);
4388 DPSOFTRAST_SetTexCoordPointer(0, 2, sizeof(float[2]), NULL);
4389 DPSOFTRAST_SetTexCoordPointer(1, 2, sizeof(float[2]), NULL);
4390 DPSOFTRAST_SetTexCoordPointer(2, 2, sizeof(float[2]), NULL);
4391 DPSOFTRAST_SetTexCoordPointer(3, 2, sizeof(float[2]), NULL);
4392 DPSOFTRAST_SetTexCoordPointer(4, 2, sizeof(float[2]), NULL);
4399 r_vertexgeneric_t *R_Mesh_PrepareVertices_Generic_Lock(int numvertices)
4402 size = sizeof(r_vertexgeneric_t) * numvertices;
4403 if (gl_state.preparevertices_tempdatamaxsize < size)
4405 gl_state.preparevertices_tempdatamaxsize = size;
4406 gl_state.preparevertices_tempdata = Mem_Realloc(r_main_mempool, gl_state.preparevertices_tempdata, gl_state.preparevertices_tempdatamaxsize);
4408 gl_state.preparevertices_vertexgeneric = (r_vertexgeneric_t *)gl_state.preparevertices_tempdata;
4409 gl_state.preparevertices_numvertices = numvertices;
4410 return gl_state.preparevertices_vertexgeneric;
4413 qboolean R_Mesh_PrepareVertices_Generic_Unlock(void)
4415 R_Mesh_PrepareVertices_Generic(gl_state.preparevertices_numvertices, gl_state.preparevertices_vertexgeneric, NULL, 0);
4416 gl_state.preparevertices_vertexgeneric = NULL;
4417 gl_state.preparevertices_numvertices = 0;
4421 void R_Mesh_PrepareVertices_Generic_Arrays(int numvertices, const float *vertex3f, const float *color4f, const float *texcoord2f)
4424 r_vertexgeneric_t *vertex;
4425 switch(vid.renderpath)
4427 case RENDERPATH_GL20:
4428 case RENDERPATH_GLES2:
4429 if (gl_state.usevbo_dynamicvertex)
4431 r_meshbuffer_t *buffer_vertex3f = NULL;
4432 r_meshbuffer_t *buffer_color4f = NULL;
4433 r_meshbuffer_t *buffer_texcoord2f = NULL;
4434 int bufferoffset_vertex3f = 0;
4435 int bufferoffset_color4f = 0;
4436 int bufferoffset_texcoord2f = 0;
4437 buffer_color4f = R_BufferData_Store(numvertices * sizeof(float[4]), color4f , R_BUFFERDATA_VERTEX, &bufferoffset_color4f );
4438 buffer_vertex3f = R_BufferData_Store(numvertices * sizeof(float[3]), vertex3f , R_BUFFERDATA_VERTEX, &bufferoffset_vertex3f );
4439 buffer_texcoord2f = R_BufferData_Store(numvertices * sizeof(float[2]), texcoord2f, R_BUFFERDATA_VERTEX, &bufferoffset_texcoord2f);
4440 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(float[3]) , vertex3f , buffer_vertex3f , bufferoffset_vertex3f );
4441 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(float[4]) , color4f , buffer_color4f , bufferoffset_color4f );
4442 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(float[2]) , texcoord2f , buffer_texcoord2f , bufferoffset_texcoord2f );
4443 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT , sizeof(float[3]) , NULL , NULL , 0 );
4444 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT , sizeof(float[3]) , NULL , NULL , 0 );
4445 R_Mesh_TexCoordPointer(3, 3, GL_FLOAT , sizeof(float[3]) , NULL , NULL , 0 );
4446 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT , sizeof(float[2]) , NULL , NULL , 0 );
4447 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT , sizeof(float[2]) , NULL , NULL , 0 );
4448 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL , NULL , 0 );
4449 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL , NULL , 0 );
4451 else if (!vid.useinterleavedarrays)
4453 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
4454 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
4455 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoord2f, NULL, 0);
4456 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4457 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4458 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4459 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4460 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4461 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4462 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4466 case RENDERPATH_GL11:
4467 case RENDERPATH_GL13:
4468 case RENDERPATH_GLES1:
4469 if (!vid.useinterleavedarrays)
4471 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
4472 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
4473 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoord2f, NULL, 0);
4474 if (vid.texunits >= 2)
4475 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4476 if (vid.texunits >= 3)
4477 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4481 case RENDERPATH_D3D9:
4482 case RENDERPATH_D3D10:
4483 case RENDERPATH_D3D11:
4485 case RENDERPATH_SOFT:
4486 DPSOFTRAST_SetVertexPointer(vertex3f, sizeof(float[3]));
4487 DPSOFTRAST_SetColorPointer(color4f, sizeof(float[4]));
4488 DPSOFTRAST_SetTexCoordPointer(0, 2, sizeof(float[2]), texcoord2f);
4489 DPSOFTRAST_SetTexCoordPointer(1, 2, sizeof(float[2]), NULL);
4490 DPSOFTRAST_SetTexCoordPointer(2, 2, sizeof(float[2]), NULL);
4491 DPSOFTRAST_SetTexCoordPointer(3, 2, sizeof(float[2]), NULL);
4492 DPSOFTRAST_SetTexCoordPointer(4, 2, sizeof(float[2]), NULL);
4496 // no quick path for this case, convert to vertex structs
4497 vertex = R_Mesh_PrepareVertices_Generic_Lock(numvertices);
4498 for (i = 0;i < numvertices;i++)
4499 VectorCopy(vertex3f + 3*i, vertex[i].vertex3f);
4502 for (i = 0;i < numvertices;i++)
4503 Vector4Copy(color4f + 4*i, vertex[i].color4f);
4507 for (i = 0;i < numvertices;i++)
4508 Vector4Copy(gl_state.color4f, vertex[i].color4f);
4511 for (i = 0;i < numvertices;i++)
4512 Vector2Copy(texcoord2f + 2*i, vertex[i].texcoord2f);
4513 R_Mesh_PrepareVertices_Generic_Unlock();
4514 R_Mesh_PrepareVertices_Generic(numvertices, vertex, NULL, 0);
4517 void R_Mesh_PrepareVertices_Generic(int numvertices, const r_vertexgeneric_t *vertex, const r_meshbuffer_t *vertexbuffer, int bufferoffset)
4519 // upload temporary vertexbuffer for this rendering
4520 if (!gl_state.usevbo_staticvertex)
4521 vertexbuffer = NULL;
4522 if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
4523 vertexbuffer = R_BufferData_Store(numvertices * sizeof(*vertex), (void *)vertex, R_BUFFERDATA_VERTEX, &bufferoffset);
4524 switch(vid.renderpath)
4526 case RENDERPATH_GL20:
4527 case RENDERPATH_GLES2:
4530 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
4531 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->color4f - (unsigned char *)vertex));
4532 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->texcoord2f - (unsigned char *)vertex));
4533 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4534 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4535 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4536 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4537 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4538 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4539 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4543 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
4544 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , NULL, 0);
4545 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , NULL, 0);
4546 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4547 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4548 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4549 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4550 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4551 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4552 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4555 case RENDERPATH_GL13:
4556 case RENDERPATH_GLES1:
4559 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
4560 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->color4f - (unsigned char *)vertex));
4561 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->texcoord2f - (unsigned char *)vertex));
4562 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4566 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
4567 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , NULL, 0);
4568 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , NULL, 0);
4569 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4572 case RENDERPATH_GL11:
4575 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
4576 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->color4f - (unsigned char *)vertex));
4577 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->texcoord2f - (unsigned char *)vertex));
4581 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
4582 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , NULL, 0);
4583 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , NULL, 0);
4586 case RENDERPATH_D3D9:
4588 IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertexgeneric_d3d9decl);
4590 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, bufferoffset, sizeof(*vertex));
4592 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, NULL, 0, 0);
4593 gl_state.d3dvertexbuffer = (void *)vertexbuffer;
4594 gl_state.d3dvertexdata = (void *)vertex;
4595 gl_state.d3dvertexsize = sizeof(*vertex);
4598 case RENDERPATH_D3D10:
4599 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4601 case RENDERPATH_D3D11:
4602 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4604 case RENDERPATH_SOFT:
4605 DPSOFTRAST_SetVertexPointer(vertex->vertex3f, sizeof(*vertex));
4606 DPSOFTRAST_SetColorPointer(vertex->color4f, sizeof(*vertex));
4607 DPSOFTRAST_SetTexCoordPointer(0, 2, sizeof(*vertex), vertex->texcoord2f);
4608 DPSOFTRAST_SetTexCoordPointer(1, 2, sizeof(*vertex), NULL);
4609 DPSOFTRAST_SetTexCoordPointer(2, 2, sizeof(*vertex), NULL);
4610 DPSOFTRAST_SetTexCoordPointer(3, 2, sizeof(*vertex), NULL);
4611 DPSOFTRAST_SetTexCoordPointer(4, 2, sizeof(*vertex), NULL);
4618 r_vertexmesh_t *R_Mesh_PrepareVertices_Mesh_Lock(int numvertices)
4621 size = sizeof(r_vertexmesh_t) * numvertices;
4622 if (gl_state.preparevertices_tempdatamaxsize < size)
4624 gl_state.preparevertices_tempdatamaxsize = size;
4625 gl_state.preparevertices_tempdata = Mem_Realloc(r_main_mempool, gl_state.preparevertices_tempdata, gl_state.preparevertices_tempdatamaxsize);
4627 gl_state.preparevertices_vertexmesh = (r_vertexmesh_t *)gl_state.preparevertices_tempdata;
4628 gl_state.preparevertices_numvertices = numvertices;
4629 return gl_state.preparevertices_vertexmesh;
4632 qboolean R_Mesh_PrepareVertices_Mesh_Unlock(void)
4634 R_Mesh_PrepareVertices_Mesh(gl_state.preparevertices_numvertices, gl_state.preparevertices_vertexmesh, NULL, 0);
4635 gl_state.preparevertices_vertexmesh = NULL;
4636 gl_state.preparevertices_numvertices = 0;
4640 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)
4643 r_vertexmesh_t *vertex;
4644 switch(vid.renderpath)
4646 case RENDERPATH_GL20:
4647 case RENDERPATH_GLES2:
4648 if (gl_state.usevbo_dynamicvertex)
4650 r_meshbuffer_t *buffer_vertex3f = NULL;
4651 r_meshbuffer_t *buffer_color4f = NULL;
4652 r_meshbuffer_t *buffer_texcoordtexture2f = NULL;
4653 r_meshbuffer_t *buffer_svector3f = NULL;
4654 r_meshbuffer_t *buffer_tvector3f = NULL;
4655 r_meshbuffer_t *buffer_normal3f = NULL;
4656 r_meshbuffer_t *buffer_texcoordlightmap2f = NULL;
4657 int bufferoffset_vertex3f = 0;
4658 int bufferoffset_color4f = 0;
4659 int bufferoffset_texcoordtexture2f = 0;
4660 int bufferoffset_svector3f = 0;
4661 int bufferoffset_tvector3f = 0;
4662 int bufferoffset_normal3f = 0;
4663 int bufferoffset_texcoordlightmap2f = 0;
4664 buffer_color4f = R_BufferData_Store(numvertices * sizeof(float[4]), color4f , R_BUFFERDATA_VERTEX, &bufferoffset_color4f );
4665 buffer_vertex3f = R_BufferData_Store(numvertices * sizeof(float[3]), vertex3f , R_BUFFERDATA_VERTEX, &bufferoffset_vertex3f );
4666 buffer_svector3f = R_BufferData_Store(numvertices * sizeof(float[3]), svector3f , R_BUFFERDATA_VERTEX, &bufferoffset_svector3f );
4667 buffer_tvector3f = R_BufferData_Store(numvertices * sizeof(float[3]), tvector3f , R_BUFFERDATA_VERTEX, &bufferoffset_tvector3f );
4668 buffer_normal3f = R_BufferData_Store(numvertices * sizeof(float[3]), normal3f , R_BUFFERDATA_VERTEX, &bufferoffset_normal3f );
4669 buffer_texcoordtexture2f = R_BufferData_Store(numvertices * sizeof(float[2]), texcoordtexture2f , R_BUFFERDATA_VERTEX, &bufferoffset_texcoordtexture2f );
4670 buffer_texcoordlightmap2f = R_BufferData_Store(numvertices * sizeof(float[2]), texcoordlightmap2f, R_BUFFERDATA_VERTEX, &bufferoffset_texcoordlightmap2f);
4671 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(float[3]) , vertex3f , buffer_vertex3f , bufferoffset_vertex3f );
4672 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(float[4]) , color4f , buffer_color4f , bufferoffset_color4f );
4673 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(float[2]) , texcoordtexture2f , buffer_texcoordtexture2f , bufferoffset_texcoordtexture2f );
4674 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT , sizeof(float[3]) , svector3f , buffer_svector3f , bufferoffset_svector3f );
4675 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT , sizeof(float[3]) , tvector3f , buffer_tvector3f , bufferoffset_tvector3f );
4676 R_Mesh_TexCoordPointer(3, 3, GL_FLOAT , sizeof(float[3]) , normal3f , buffer_normal3f , bufferoffset_normal3f );
4677 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT , sizeof(float[2]) , texcoordlightmap2f, buffer_texcoordlightmap2f, bufferoffset_texcoordlightmap2f);
4678 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT , sizeof(float[2]) , NULL , NULL , 0 );
4679 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL , NULL , 0 );
4680 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL , NULL , 0 );
4682 else if (!vid.useinterleavedarrays)
4684 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
4685 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
4686 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoordtexture2f, NULL, 0);
4687 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT, sizeof(float[3]), svector3f, NULL, 0);
4688 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT, sizeof(float[3]), tvector3f, NULL, 0);
4689 R_Mesh_TexCoordPointer(3, 3, GL_FLOAT, sizeof(float[3]), normal3f, NULL, 0);
4690 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), texcoordlightmap2f, NULL, 0);
4691 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4692 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4693 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
4697 case RENDERPATH_GL11:
4698 case RENDERPATH_GL13:
4699 case RENDERPATH_GLES1:
4700 if (!vid.useinterleavedarrays)
4702 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
4703 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
4704 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoordtexture2f, NULL, 0);
4705 if (vid.texunits >= 2)
4706 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), texcoordlightmap2f, NULL, 0);
4707 if (vid.texunits >= 3)
4708 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
4712 case RENDERPATH_D3D9:
4713 case RENDERPATH_D3D10:
4714 case RENDERPATH_D3D11:
4716 case RENDERPATH_SOFT:
4717 DPSOFTRAST_SetVertexPointer(vertex3f, sizeof(float[3]));
4718 DPSOFTRAST_SetColorPointer(color4f, sizeof(float[4]));
4719 DPSOFTRAST_SetTexCoordPointer(0, 2, sizeof(float[2]), texcoordtexture2f);
4720 DPSOFTRAST_SetTexCoordPointer(1, 3, sizeof(float[3]), svector3f);
4721 DPSOFTRAST_SetTexCoordPointer(2, 3, sizeof(float[3]), tvector3f);
4722 DPSOFTRAST_SetTexCoordPointer(3, 3, sizeof(float[3]), normal3f);
4723 DPSOFTRAST_SetTexCoordPointer(4, 2, sizeof(float[2]), texcoordlightmap2f);
4727 vertex = R_Mesh_PrepareVertices_Mesh_Lock(numvertices);
4728 for (i = 0;i < numvertices;i++)
4729 VectorCopy(vertex3f + 3*i, vertex[i].vertex3f);
4731 for (i = 0;i < numvertices;i++)
4732 VectorCopy(svector3f + 3*i, vertex[i].svector3f);
4734 for (i = 0;i < numvertices;i++)
4735 VectorCopy(tvector3f + 3*i, vertex[i].tvector3f);
4737 for (i = 0;i < numvertices;i++)
4738 VectorCopy(normal3f + 3*i, vertex[i].normal3f);
4741 for (i = 0;i < numvertices;i++)
4742 Vector4Copy(color4f + 4*i, vertex[i].color4f);
4746 for (i = 0;i < numvertices;i++)
4747 Vector4Copy(gl_state.color4f, vertex[i].color4f);
4749 if (texcoordtexture2f)
4750 for (i = 0;i < numvertices;i++)
4751 Vector2Copy(texcoordtexture2f + 2*i, vertex[i].texcoordtexture2f);
4752 if (texcoordlightmap2f)
4753 for (i = 0;i < numvertices;i++)
4754 Vector2Copy(texcoordlightmap2f + 2*i, vertex[i].texcoordlightmap2f);
4755 R_Mesh_PrepareVertices_Mesh_Unlock();
4756 R_Mesh_PrepareVertices_Mesh(numvertices, vertex, NULL, 0);
4759 void R_Mesh_PrepareVertices_Mesh(int numvertices, const r_vertexmesh_t *vertex, const r_meshbuffer_t *vertexbuffer, int bufferoffset)
4761 // upload temporary vertexbuffer for this rendering
4762 if (!gl_state.usevbo_staticvertex)
4763 vertexbuffer = NULL;
4764 if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
4765 vertexbuffer = R_BufferData_Store(numvertices * sizeof(*vertex), (void *)vertex, R_BUFFERDATA_VERTEX, &bufferoffset);
4766 switch(vid.renderpath)
4768 case RENDERPATH_GL20:
4769 case RENDERPATH_GLES2:
4772 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
4773 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->color4f - (unsigned char *)vertex));
4774 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->texcoordtexture2f - (unsigned char *)vertex));
4775 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT , sizeof(*vertex), vertex->svector3f , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->svector3f - (unsigned char *)vertex));
4776 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT , sizeof(*vertex), vertex->tvector3f , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->tvector3f - (unsigned char *)vertex));
4777 R_Mesh_TexCoordPointer(3, 3, GL_FLOAT , sizeof(*vertex), vertex->normal3f , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->normal3f - (unsigned char *)vertex));
4778 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->texcoordlightmap2f - (unsigned char *)vertex));
4779 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT , sizeof(*vertex), NULL, NULL, 0);
4780 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE | 0x80000000, sizeof(*vertex), vertex->skeletalindex4ub , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->skeletalindex4ub - (unsigned char *)vertex));
4781 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->skeletalweight4ub , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->skeletalweight4ub - (unsigned char *)vertex));
4785 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
4786 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , NULL, 0);
4787 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , NULL, 0);
4788 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT , sizeof(*vertex), vertex->svector3f , NULL, 0);
4789 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT , sizeof(*vertex), vertex->tvector3f , NULL, 0);
4790 R_Mesh_TexCoordPointer(3, 3, GL_FLOAT , sizeof(*vertex), vertex->normal3f , NULL, 0);
4791 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, NULL, 0);
4792 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT , sizeof(*vertex), NULL, NULL, 0);
4793 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE | 0x80000000, sizeof(*vertex), vertex->skeletalindex4ub , NULL, 0);
4794 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->skeletalweight4ub , NULL, 0);
4797 case RENDERPATH_GL13:
4798 case RENDERPATH_GLES1:
4801 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
4802 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->color4f - (unsigned char *)vertex));
4803 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->texcoordtexture2f - (unsigned char *)vertex));
4804 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->texcoordlightmap2f - (unsigned char *)vertex));
4808 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
4809 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , NULL, 0);
4810 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , NULL, 0);
4811 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, NULL, 0);
4814 case RENDERPATH_GL11:
4817 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
4818 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->color4f - (unsigned char *)vertex));
4819 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , vertexbuffer, bufferoffset + (int)((unsigned char *)vertex->texcoordtexture2f - (unsigned char *)vertex));
4823 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
4824 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(*vertex), vertex->color4f , NULL, 0);
4825 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , NULL, 0);
4828 case RENDERPATH_D3D9:
4830 IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertexmesh_d3d9decl);
4832 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, bufferoffset, sizeof(*vertex));
4834 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, NULL, 0, 0);
4835 gl_state.d3dvertexbuffer = (void *)vertexbuffer;
4836 gl_state.d3dvertexdata = (void *)vertex;
4837 gl_state.d3dvertexsize = sizeof(*vertex);
4840 case RENDERPATH_D3D10:
4841 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4843 case RENDERPATH_D3D11:
4844 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4846 case RENDERPATH_SOFT:
4847 DPSOFTRAST_SetVertexPointer(vertex->vertex3f, sizeof(*vertex));
4848 DPSOFTRAST_SetColorPointer(vertex->color4f, sizeof(*vertex));
4849 DPSOFTRAST_SetTexCoordPointer(0, 2, sizeof(*vertex), vertex->texcoordtexture2f);
4850 DPSOFTRAST_SetTexCoordPointer(1, 3, sizeof(*vertex), vertex->svector3f);
4851 DPSOFTRAST_SetTexCoordPointer(2, 3, sizeof(*vertex), vertex->tvector3f);
4852 DPSOFTRAST_SetTexCoordPointer(3, 3, sizeof(*vertex), vertex->normal3f);
4853 DPSOFTRAST_SetTexCoordPointer(4, 2, sizeof(*vertex), vertex->texcoordlightmap2f);
4858 void GL_BlendEquationSubtract(qboolean negated)
4862 switch(vid.renderpath)
4864 case RENDERPATH_GL11:
4865 case RENDERPATH_GL13:
4866 case RENDERPATH_GL20:
4867 case RENDERPATH_GLES1:
4868 case RENDERPATH_GLES2:
4869 qglBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT);
4871 case RENDERPATH_D3D9:
4873 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_BLENDOP, D3DBLENDOP_SUBTRACT);
4876 case RENDERPATH_D3D10:
4877 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4879 case RENDERPATH_D3D11:
4880 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4882 case RENDERPATH_SOFT:
4883 DPSOFTRAST_BlendSubtract(true);
4889 switch(vid.renderpath)
4891 case RENDERPATH_GL11:
4892 case RENDERPATH_GL13:
4893 case RENDERPATH_GL20:
4894 case RENDERPATH_GLES1:
4895 case RENDERPATH_GLES2:
4896 qglBlendEquationEXT(GL_FUNC_ADD);
4898 case RENDERPATH_D3D9:
4900 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_BLENDOP, D3DBLENDOP_ADD);
4903 case RENDERPATH_D3D10:
4904 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4906 case RENDERPATH_D3D11:
4907 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4909 case RENDERPATH_SOFT:
4910 DPSOFTRAST_BlendSubtract(false);