]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_backend.c
enable ODE dll support by default on windows
[xonotic/darkplaces.git] / gl_backend.c
index 15adfc73832cd2947119079c5f8cf942b7d5ea16..9423cc75ef47e8018585abb47e12cfba10b62060 100644 (file)
@@ -110,7 +110,7 @@ typedef struct gltextureunit_s
        size_t pointer_texcoord_offset;
 
        rtexture_t *texture;
-       int t2d, t3d, tcubemap, trectangle;
+       int t2d, t3d, tcubemap;
        int arrayenabled;
        int rgbscale, alphascale;
        int combine;
@@ -183,8 +183,8 @@ typedef struct gl_state_s
        qboolean active;
 
 #ifdef SUPPORTD3D
-       rtexture_t *d3drt_depthtexture;
-       rtexture_t *d3drt_colortextures[MAX_RENDERTARGETS];
+//     rtexture_t *d3drt_depthtexture;
+//     rtexture_t *d3drt_colortextures[MAX_RENDERTARGETS];
        IDirect3DSurface9 *d3drt_depthsurface;
        IDirect3DSurface9 *d3drt_colorsurfaces[MAX_RENDERTARGETS];
        IDirect3DSurface9 *d3drt_backbufferdepthsurface;
@@ -379,6 +379,26 @@ static void gl_backend_devicelost(void)
 #ifdef SUPPORTD3D
        gl_state.d3dvertexbuffer = NULL;
 #endif
+       switch(vid.renderpath)
+       {
+       case RENDERPATH_GL11:
+       case RENDERPATH_GL13:
+       case RENDERPATH_GL20:
+       case RENDERPATH_CGGL:
+               break;
+       case RENDERPATH_D3D9:
+#ifdef SUPPORTD3D
+               IDirect3DSurface9_Release(gl_state.d3drt_backbufferdepthsurface);
+               IDirect3DSurface9_Release(gl_state.d3drt_backbuffercolorsurface);
+#endif
+               break;
+       case RENDERPATH_D3D10:
+               Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
+               break;
+       case RENDERPATH_D3D11:
+               Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
+               break;
+       }
        endindex = Mem_ExpandableArray_IndexRange(&gl_state.meshbufferarray);
        for (i = 0;i < endindex;i++)
        {
@@ -416,6 +436,26 @@ static void gl_backend_devicelost(void)
 
 static void gl_backend_devicerestored(void)
 {
+       switch(vid.renderpath)
+       {
+       case RENDERPATH_GL11:
+       case RENDERPATH_GL13:
+       case RENDERPATH_GL20:
+       case RENDERPATH_CGGL:
+               break;
+       case RENDERPATH_D3D9:
+#ifdef SUPPORTD3D
+               IDirect3DDevice9_GetDepthStencilSurface(vid_d3d9dev, &gl_state.d3drt_backbufferdepthsurface);
+               IDirect3DDevice9_GetRenderTarget(vid_d3d9dev, 0, &gl_state.d3drt_backbuffercolorsurface);
+#endif
+               break;
+       case RENDERPATH_D3D10:
+               Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
+               break;
+       case RENDERPATH_D3D11:
+               Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
+               break;
+       }
 }
 
 void gl_backend_init(void)
@@ -476,10 +516,164 @@ void R_Viewport_TransformToScreen(const r_viewport_t *v, const vec4_t in, vec4_t
        Matrix4x4_Transform4 (&v->projectmatrix, temp, out);
        iw = 1.0f / out[3];
        out[0] = v->x + (out[0] * iw + 1.0f) * v->width * 0.5f;
-       out[1] = v->y + v->height - (out[1] * iw + 1.0f) * v->height * 0.5f;
+
+       // for an odd reason, inverting this is wrong for R_Shadow_ScissorForBBox (we then get badly scissored lights)
+       //out[1] = v->y + v->height - (out[1] * iw + 1.0f) * v->height * 0.5f;
+       out[1] = v->y + (out[1] * iw + 1.0f) * v->height * 0.5f;
+
        out[2] = v->z + (out[2] * iw + 1.0f) * v->depth * 0.5f;
 }
 
+static int bboxedges[12][2] =
+{
+       // top
+       {0, 1}, // +X
+       {0, 2}, // +Y
+       {1, 3}, // Y, +X
+       {2, 3}, // X, +Y
+       // bottom
+       {4, 5}, // +X
+       {4, 6}, // +Y
+       {5, 7}, // Y, +X
+       {6, 7}, // X, +Y
+       // verticals
+       {0, 4}, // +Z
+       {1, 5}, // X, +Z
+       {2, 6}, // Y, +Z
+       {3, 7}, // XY, +Z
+};
+
+qboolean R_ScissorForBBox(const float *mins, const float *maxs, int *scissor)
+{
+       int i, ix1, iy1, ix2, iy2;
+       float x1, y1, x2, y2;
+       vec4_t v, v2;
+       float vertex[20][3];
+       int j, k;
+       vec4_t plane4f;
+       int numvertices;
+       float corner[8][4];
+       float dist[8];
+       int sign[8];
+       float f;
+
+       scissor[0] = r_refdef.view.viewport.x;
+       scissor[1] = r_refdef.view.viewport.y;
+       scissor[2] = r_refdef.view.viewport.width;
+       scissor[3] = r_refdef.view.viewport.height;
+
+       // if view is inside the box, just say yes it's visible
+       if (BoxesOverlap(r_refdef.view.origin, r_refdef.view.origin, mins, maxs))
+               return false;
+
+       x1 = y1 = x2 = y2 = 0;
+
+       // transform all corners that are infront of the nearclip plane
+       VectorNegate(r_refdef.view.frustum[4].normal, plane4f);
+       plane4f[3] = r_refdef.view.frustum[4].dist;
+       numvertices = 0;
+       for (i = 0;i < 8;i++)
+       {
+               Vector4Set(corner[i], (i & 1) ? maxs[0] : mins[0], (i & 2) ? maxs[1] : mins[1], (i & 4) ? maxs[2] : mins[2], 1);
+               dist[i] = DotProduct4(corner[i], plane4f);
+               sign[i] = dist[i] > 0;
+               if (!sign[i])
+               {
+                       VectorCopy(corner[i], vertex[numvertices]);
+                       numvertices++;
+               }
+       }
+       // if some points are behind the nearclip, add clipped edge points to make
+       // sure that the scissor boundary is complete
+       if (numvertices > 0 && numvertices < 8)
+       {
+               // add clipped edge points
+               for (i = 0;i < 12;i++)
+               {
+                       j = bboxedges[i][0];
+                       k = bboxedges[i][1];
+                       if (sign[j] != sign[k])
+                       {
+                               f = dist[j] / (dist[j] - dist[k]);
+                               VectorLerp(corner[j], f, corner[k], vertex[numvertices]);
+                               numvertices++;
+                       }
+               }
+       }
+
+       // if we have no points to check, it is behind the view plane
+       if (!numvertices)
+               return true;
+
+       // if we have some points to transform, check what screen area is covered
+       x1 = y1 = x2 = y2 = 0;
+       v[3] = 1.0f;
+       //Con_Printf("%i vertices to transform...\n", numvertices);
+       for (i = 0;i < numvertices;i++)
+       {
+               VectorCopy(vertex[i], v);
+               R_Viewport_TransformToScreen(&r_refdef.view.viewport, v, v2);
+               //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]);
+               if (i)
+               {
+                       if (x1 > v2[0]) x1 = v2[0];
+                       if (x2 < v2[0]) x2 = v2[0];
+                       if (y1 > v2[1]) y1 = v2[1];
+                       if (y2 < v2[1]) y2 = v2[1];
+               }
+               else
+               {
+                       x1 = x2 = v2[0];
+                       y1 = y2 = v2[1];
+               }
+       }
+
+       // now convert the scissor rectangle to integer screen coordinates
+       ix1 = (int)(x1 - 1.0f);
+       //iy1 = vid.height - (int)(y2 - 1.0f);
+       //iy1 = r_refdef.view.viewport.width + 2 * r_refdef.view.viewport.x - (int)(y2 - 1.0f);
+       iy1 = (int)(y1 - 1.0f);
+       ix2 = (int)(x2 + 1.0f);
+       //iy2 = vid.height - (int)(y1 + 1.0f);
+       //iy2 = r_refdef.view.viewport.height + 2 * r_refdef.view.viewport.y - (int)(y1 + 1.0f);
+       iy2 = (int)(y2 + 1.0f);
+       //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
+
+       // clamp it to the screen
+       if (ix1 < r_refdef.view.viewport.x) ix1 = r_refdef.view.viewport.x;
+       if (iy1 < r_refdef.view.viewport.y) iy1 = r_refdef.view.viewport.y;
+       if (ix2 > r_refdef.view.viewport.x + r_refdef.view.viewport.width) ix2 = r_refdef.view.viewport.x + r_refdef.view.viewport.width;
+       if (iy2 > r_refdef.view.viewport.y + r_refdef.view.viewport.height) iy2 = r_refdef.view.viewport.y + r_refdef.view.viewport.height;
+
+       // if it is inside out, it's not visible
+       if (ix2 <= ix1 || iy2 <= iy1)
+               return true;
+
+       // the light area is visible, set up the scissor rectangle
+       scissor[0] = ix1;
+       scissor[1] = iy1;
+       scissor[2] = ix2 - ix1;
+       scissor[3] = iy2 - iy1;
+
+       // D3D Y coordinate is top to bottom, OpenGL is bottom to top, fix the D3D one
+       switch(vid.renderpath)
+       {
+       case RENDERPATH_D3D9:
+       case RENDERPATH_D3D10:
+       case RENDERPATH_D3D11:
+               scissor[1] = vid.height - scissor[1] - scissor[3];
+               break;
+       case RENDERPATH_GL11:
+       case RENDERPATH_GL13:
+       case RENDERPATH_GL20:
+       case RENDERPATH_CGGL:
+               break;
+       }
+
+       return false;
+}
+
+
 static void R_Viewport_ApplyNearClipPlaneFloatGL(const r_viewport_t *v, float *m, float normalx, float normaly, float normalz, float dist)
 {
        float q[4];
@@ -948,6 +1142,42 @@ void R_Mesh_DestroyFramebufferObject(int fbo)
        }
 }
 
+#ifdef SUPPORTD3D
+void R_Mesh_SetRenderTargetsD3D9(IDirect3DSurface9 *depthsurface, IDirect3DSurface9 *colorsurface0, IDirect3DSurface9 *colorsurface1, IDirect3DSurface9 *colorsurface2, IDirect3DSurface9 *colorsurface3)
+{
+// LordHavoc: for some weird reason the redundant SetDepthStencilSurface calls are necessary (otherwise the lights fail depth test, as if they were using the shadowmap depth surface and render target still)
+//     if (gl_state.d3drt_depthsurface == depthsurface && gl_state.d3drt_colorsurfaces[0] == colorsurface0 && gl_state.d3drt_colorsurfaces[1] == colorsurface1 && gl_state.d3drt_colorsurfaces[2] == colorsurface2 && gl_state.d3drt_colorsurfaces[3] == colorsurface3)
+//             return;
+
+       gl_state.framebufferobject = depthsurface != gl_state.d3drt_backbufferdepthsurface || colorsurface0 != gl_state.d3drt_backbuffercolorsurface;
+//     if (gl_state.d3drt_depthsurface != depthsurface)
+       {
+               gl_state.d3drt_depthsurface = depthsurface;
+               IDirect3DDevice9_SetDepthStencilSurface(vid_d3d9dev, gl_state.d3drt_depthsurface);
+       }
+       if (gl_state.d3drt_colorsurfaces[0] != colorsurface0)
+       {
+               gl_state.d3drt_colorsurfaces[0] = colorsurface0;
+               IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 0, gl_state.d3drt_colorsurfaces[0]);
+       }
+       if (gl_state.d3drt_colorsurfaces[1] != colorsurface1)
+       {
+               gl_state.d3drt_colorsurfaces[1] = colorsurface1;
+               IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 1, gl_state.d3drt_colorsurfaces[1]);
+       }
+       if (gl_state.d3drt_colorsurfaces[2] != colorsurface2)
+       {
+               gl_state.d3drt_colorsurfaces[2] = colorsurface2;
+               IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 2, gl_state.d3drt_colorsurfaces[2]);
+       }
+       if (gl_state.d3drt_colorsurfaces[3] != colorsurface3)
+       {
+               gl_state.d3drt_colorsurfaces[3] = colorsurface3;
+               IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 3, gl_state.d3drt_colorsurfaces[3]);
+       }
+}
+#endif
+
 void R_Mesh_ResetRenderTargets(void)
 {
        switch(vid.renderpath)
@@ -964,22 +1194,7 @@ void R_Mesh_ResetRenderTargets(void)
                break;
        case RENDERPATH_D3D9:
 #ifdef SUPPORTD3D
-               if (gl_state.framebufferobject)
-               {
-                       unsigned int i;
-                       gl_state.framebufferobject = 0;
-                       IDirect3DDevice9_SetDepthStencilSurface(vid_d3d9dev, gl_state.d3drt_backbufferdepthsurface);
-                       IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 0, gl_state.d3drt_backbuffercolorsurface);
-                       gl_state.d3drt_depthsurface = NULL;
-                       for (i = 1;i < vid.maxdrawbuffers;i++)
-                       {
-                               if (gl_state.d3drt_colorsurfaces[i])
-                               {
-                                       gl_state.d3drt_colorsurfaces[i] = NULL;
-                                       IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, i, NULL);
-                               }
-                       }
-               }
+               R_Mesh_SetRenderTargetsD3D9(gl_state.d3drt_backbufferdepthsurface, gl_state.d3drt_backbuffercolorsurface, NULL, NULL, NULL);
 #endif
                break;
        case RENDERPATH_D3D10:
@@ -1023,27 +1238,22 @@ void R_Mesh_SetRenderTargets(int fbo, rtexture_t *depthtexture, rtexture_t *colo
                // TODO: optimize: keep surface pointer around in rtexture_t until texture is freed or lost
                if (fbo)
                {
-                       gl_state.framebufferobject = 1;
-                       gl_state.d3drt_depthtexture = depthtexture;
-                       if (gl_state.d3drt_depthtexture)
-                               IDirect3DDevice9_SetDepthStencilSurface(vid_d3d9dev, (IDirect3DSurface9 *)gl_state.d3drt_depthtexture->d3dtexture);
-                       else
-                               IDirect3DDevice9_SetDepthStencilSurface(vid_d3d9dev, NULL);
-                       for (i = 0;i < vid.maxdrawbuffers;i++)
+                       IDirect3DSurface9 *colorsurfaces[4];
+                       for (i = 0;i < 4;i++)
                        {
-                               gl_state.d3drt_colortextures[i] = textures[i];
-                               if (gl_state.d3drt_colortextures[i])
-                               {
-                                       IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9 *)gl_state.d3drt_colortextures[i]->d3dtexture, 0, &gl_state.d3drt_colorsurfaces[i]);
-                                       IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, i, gl_state.d3drt_colorsurfaces[i]);
-                                       IDirect3DSurface9_Release(gl_state.d3drt_colorsurfaces[i]);
-                               }
-                               else
-                                       IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, i, i ? NULL : gl_state.d3drt_backbuffercolorsurface);
+                               colorsurfaces[i] = NULL;
+                               if (textures[i])
+                                       IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9 *)textures[i]->d3dtexture, 0, &colorsurfaces[i]);
                        }
+                       // set the render targets for real
+                       R_Mesh_SetRenderTargetsD3D9(depthtexture ? (IDirect3DSurface9 *)depthtexture->d3dtexture : NULL, colorsurfaces[0], colorsurfaces[1], colorsurfaces[2], colorsurfaces[3]);
+                       // release the texture surface levels (they won't be lost while bound...)
+                       for (i = 0;i < 4;i++)
+                               if (textures[i])
+                                       IDirect3DSurface9_Release(colorsurfaces[i]);
                }
                else
-                       R_Mesh_ResetRenderTargets();
+                       R_Mesh_SetRenderTargetsD3D9(gl_state.d3drt_backbufferdepthsurface, gl_state.d3drt_backbuffercolorsurface, NULL, NULL, NULL);
 #endif
                break;
        case RENDERPATH_D3D10:
@@ -1184,10 +1394,6 @@ static void GL_Backend_ResetState(void)
                        {
                                qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
                        }
-                       if (vid.support.arb_texture_rectangle)
-                       {
-                               qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
-                       }
                }
 
                for (i = 0;i < vid.texarrayunits;i++)
@@ -1255,11 +1461,6 @@ static void GL_Backend_ResetState(void)
                                qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
                                qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
                        }
-                       if (vid.support.arb_texture_rectangle)
-                       {
-                               qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
-                               qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
-                       }
                        GL_BindVBO(0);
                        qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
                        qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
@@ -2075,6 +2276,7 @@ void GL_ReadPixelsBGRA(int x, int y, int width, int height, unsigned char *outpi
 void R_Mesh_Start(void)
 {
        BACKENDACTIVECHECK
+       R_Mesh_ResetRenderTargets();
        R_Mesh_SetUseVBO();
        if (gl_printcheckerror.integer && !gl_paranoid.integer)
        {
@@ -2559,6 +2761,7 @@ void R_Mesh_Draw(int firstvertex, int numvertices, int firsttriangle, int numtri
 // restores backend state, used when done with 3D rendering
 void R_Mesh_Finish(void)
 {
+       R_Mesh_ResetRenderTargets();
 }
 
 r_meshbuffer_t *R_Mesh_CreateMeshBuffer(const void *data, size_t size, const char *name, qboolean isindexbuffer, qboolean isdynamic, qboolean isindex16)
@@ -2836,8 +3039,6 @@ int R_Mesh_TexBound(unsigned int unitnum, int id)
                return unit->t3d;
        if (id == GL_TEXTURE_CUBE_MAP_ARB)
                return unit->tcubemap;
-       if (id == GL_TEXTURE_RECTANGLE_ARB)
-               return unit->trectangle;
        return 0;
 }
 
@@ -2919,7 +3120,6 @@ void R_Mesh_TexBind(unsigned int unitnum, rtexture_t *tex)
                case GL_TEXTURE_2D: if (unit->t2d != texnum) {GL_ActiveTexture(unitnum);unit->t2d = texnum;qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR}break;
                case GL_TEXTURE_3D: if (unit->t3d != texnum) {GL_ActiveTexture(unitnum);unit->t3d = texnum;qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR}break;
                case GL_TEXTURE_CUBE_MAP_ARB: if (unit->tcubemap != texnum) {GL_ActiveTexture(unitnum);unit->tcubemap = texnum;qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR}break;
-               case GL_TEXTURE_RECTANGLE_ARB: if (unit->trectangle != texnum) {GL_ActiveTexture(unitnum);unit->trectangle = texnum;qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR}break;
                }
                break;
        case RENDERPATH_GL13:
@@ -3212,12 +3412,6 @@ void R_Mesh_ResetTextureState(void)
                                GL_ActiveTexture(unitnum);
                                qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
                        }
-                       if (unit->trectangle)
-                       {
-                               unit->trectangle = 0;
-                               GL_ActiveTexture(unitnum);
-                               qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
-                       }
                }
                for (unitnum = 0;unitnum < vid.texarrayunits;unitnum++)
                {
@@ -3270,13 +3464,6 @@ void R_Mesh_ResetTextureState(void)
                                qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
                                qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
                        }
-                       if (unit->trectangle)
-                       {
-                               unit->trectangle = 0;
-                               GL_ActiveTexture(unitnum);
-                               qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
-                               qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
-                       }
                        if (unit->arrayenabled)
                        {
                                unit->arrayenabled = false;