]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_backend.c
the beginnings of a cgGL rendering path experiment, does not work yet
[xonotic/darkplaces.git] / gl_backend.c
index 529401fbdf95cde3f22b2b77dde62a7b82811805..ecc972d94951293c17857d73713a89684d1782c8 100644 (file)
@@ -22,6 +22,16 @@ cvar_t gl_fbo = {CVAR_SAVE, "gl_fbo", "1", "make use of GL_ARB_framebuffer_objec
 cvar_t v_flipped = {0, "v_flipped", "0", "mirror the screen (poor man's left handed mode)"};
 qboolean v_flipped_state = false;
 
+r_viewport_t gl_viewport;
+matrix4x4_t gl_modelmatrix;
+matrix4x4_t gl_viewmatrix;
+matrix4x4_t gl_modelviewmatrix;
+matrix4x4_t gl_projectionmatrix;
+matrix4x4_t gl_modelviewprojectionmatrix;
+float gl_modelview16f[16];
+float gl_modelviewprojection16f[16];
+qboolean gl_modelmatrixchanged;
+
 int gl_maxdrawrangeelementsvertices;
 int gl_maxdrawrangeelementsindices;
 
@@ -141,10 +151,6 @@ typedef struct gl_state_s
 
        memexpandablearray_t bufferobjectinfoarray;
 
-       r_viewport_t viewport;
-       matrix4x4_t modelmatrix;
-       matrix4x4_t modelviewmatrix;
-
        qboolean active;
 }
 gl_state_t;
@@ -306,10 +312,10 @@ void R_Viewport_TransformToScreen(const r_viewport_t *v, const vec4_t in, vec4_t
        out[2] = v->z + (out[2] * iw + 1.0f) * v->depth * 0.5f;
 }
 
-static void R_Viewport_ApplyNearClipPlane(r_viewport_t *v, double normalx, double normaly, double normalz, double dist)
+static void R_Viewport_ApplyNearClipPlane(r_viewport_t *v, float normalx, float normaly, float normalz, float dist)
 {
-       double q[4];
-       double d;
+       float q[4];
+       float d;
        float clipPlane[4], v3[3], v4[3];
        float normal[3];
 
@@ -327,7 +333,7 @@ static void R_Viewport_ApplyNearClipPlane(r_viewport_t *v, double normalx, doubl
        // testing code for comparing results
        float clipPlane2[4];
        VectorCopy4(clipPlane, clipPlane2);
-       R_Mesh_Matrix(&identitymatrix);
+       R_EntityMatrix(&identitymatrix);
        VectorSet(q, normal[0], normal[1], normal[2], -dist);
        qglClipPlane(GL_CLIP_PLANE0, q);
        qglGetClipPlane(GL_CLIP_PLANE0, q);
@@ -354,7 +360,7 @@ static void R_Viewport_ApplyNearClipPlane(r_viewport_t *v, double normalx, doubl
        v->m[14] = clipPlane[3] * d;
 }
 
-void R_Viewport_InitOrtho(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, double x1, double y1, double x2, double y2, double nearclip, double farclip, const double *nearplane)
+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)
 {
        float left = x1, right = x2, bottom = y2, top = y1, zNear = nearclip, zFar = farclip;
        memset(v, 0, sizeof(*v));
@@ -373,9 +379,11 @@ void R_Viewport_InitOrtho(r_viewport_t *v, const matrix4x4_t *cameramatrix, int
        v->m[13] = - (top + bottom)/(top - bottom);
        v->m[14] = - (zFar + zNear)/(zFar - zNear);
        v->m[15] = 1;
+       v->screentodepth[0] = -farclip / (farclip - nearclip);
+       v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
 
        Matrix4x4_Invert_Full(&v->viewmatrix, &v->cameramatrix);
-       Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
+       Matrix4x4_FromArrayFloatGL(&v->projectmatrix, v->m);
 
        if (nearplane)
                R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
@@ -391,7 +399,7 @@ void R_Viewport_InitOrtho(r_viewport_t *v, const matrix4x4_t *cameramatrix, int
 #endif
 }
 
-void R_Viewport_InitPerspective(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, double frustumx, double frustumy, double nearclip, double farclip, const double *nearplane)
+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)
 {
        matrix4x4_t tempmatrix, basematrix;
        memset(v, 0, sizeof(*v));
@@ -412,22 +420,24 @@ void R_Viewport_InitPerspective(r_viewport_t *v, const matrix4x4_t *cameramatrix
        v->m[10] = -(farclip + nearclip) / (farclip - nearclip);
        v->m[11] = -1;
        v->m[14] = -2 * nearclip * farclip / (farclip - nearclip);
+       v->screentodepth[0] = -farclip / (farclip - nearclip);
+       v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
 
        Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
        Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
        Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
        Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
 
-       Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
+       Matrix4x4_FromArrayFloatGL(&v->projectmatrix, v->m);
 
        if (nearplane)
                R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
 }
 
-void R_Viewport_InitPerspectiveInfinite(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, double frustumx, double frustumy, double nearclip, const double *nearplane)
+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)
 {
        matrix4x4_t tempmatrix, basematrix;
-       const double nudge = 1.0 - 1.0 / (1<<23);
+       const float nudge = 1.0 - 1.0 / (1<<23);
        memset(v, 0, sizeof(*v));
 
        if(v_flipped.integer)
@@ -446,13 +456,15 @@ void R_Viewport_InitPerspectiveInfinite(r_viewport_t *v, const matrix4x4_t *came
        v->m[10] = -nudge;
        v->m[11] = -1;
        v->m[14] = -2 * nearclip * nudge;
+       v->screentodepth[0] = (v->m[10] + 1) * 0.5 - 1;
+       v->screentodepth[1] = v->m[14] * -0.5;
 
        Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
        Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
        Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
        Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
 
-       Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
+       Matrix4x4_FromArrayFloatGL(&v->projectmatrix, v->m);
 
        if (nearplane)
                R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
@@ -556,7 +568,7 @@ void R_Viewport_InitCubeSideView(r_viewport_t *v, const matrix4x4_t *cameramatri
        Matrix4x4_FromArrayFloatGL(&basematrix, cubeviewmatrix[side]);
        Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
        Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
-       Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
+       Matrix4x4_FromArrayFloatGL(&v->projectmatrix, v->m);
 
        if (nearplane)
                R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
@@ -581,7 +593,7 @@ void R_Viewport_InitRectSideView(r_viewport_t *v, const matrix4x4_t *cameramatri
        Matrix4x4_FromArrayFloatGL(&basematrix, rectviewmatrix[side]);
        Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
        Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
-       Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
+       Matrix4x4_FromArrayFloatGL(&v->projectmatrix, v->m);
 
        if (nearplane)
                R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
@@ -589,29 +601,40 @@ void R_Viewport_InitRectSideView(r_viewport_t *v, const matrix4x4_t *cameramatri
 
 void R_SetViewport(const r_viewport_t *v)
 {
-       float glmatrix[16];
-       gl_state.viewport = *v;
+       gl_viewport = *v;
 
        CHECKGLERROR
        qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
 
-       // Load the projection matrix into OpenGL
-       qglMatrixMode(GL_PROJECTION);CHECKGLERROR
-       qglLoadMatrixd(gl_state.viewport.m);CHECKGLERROR
-       qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
-
        // FIXME: v_flipped_state is evil, this probably breaks somewhere
        GL_SetMirrorState(v_flipped.integer && (v->type == R_VIEWPORTTYPE_PERSPECTIVE || v->type == R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP));
 
-       // directly force an update of the modelview matrix
-       Matrix4x4_Concat(&gl_state.modelviewmatrix, &gl_state.viewport.viewmatrix, &gl_state.modelmatrix);
-       Matrix4x4_ToArrayFloatGL(&gl_state.modelviewmatrix, glmatrix);
-       qglLoadMatrixf(glmatrix);CHECKGLERROR
+       // copy over the matrices to our state
+       gl_viewmatrix = v->viewmatrix;
+       gl_projectionmatrix = v->projectmatrix;
+
+       switch(vid.renderpath)
+       {
+       case RENDERPATH_GL20:
+       case RENDERPATH_CGGL:
+//             break;
+       case RENDERPATH_GL13:
+       case RENDERPATH_GL11:
+               // Load the projection matrix into OpenGL
+               qglMatrixMode(GL_PROJECTION);CHECKGLERROR
+               qglLoadMatrixf(gl_viewport.m);CHECKGLERROR
+               qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
+               break;
+       }
+
+       // force an update of the derived matrices
+       gl_modelmatrixchanged = true;
+       R_EntityMatrix(&gl_modelmatrix);
 }
 
 void R_GetViewport(r_viewport_t *v)
 {
-       *v = gl_state.viewport;
+       *v = gl_viewport;
 }
 
 static void GL_BindVBO(int bufferobject)
@@ -1020,7 +1043,7 @@ qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, cons
        qglCompileShaderARB(shaderobject);CHECKGLERROR
        qglGetObjectParameterivARB(shaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &shadercompiled);CHECKGLERROR
        qglGetInfoLogARB(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
-       if (compilelog[0] && developer.integer > 0)
+       if (compilelog[0] && developer.integer > 0 && (strstr(compilelog, "error") || strstr(compilelog, "ERROR") || strstr(compilelog, "Error") || strstr(compilelog, "WARNING") || strstr(compilelog, "warning") || strstr(compilelog, "Warning")))
        {
                int i, j, pretextlines = 0;
                for (i = 0;i < numstrings - 1;i++)
@@ -1066,7 +1089,8 @@ unsigned int GL_Backend_CompileProgram(int vertexstrings_count, const char **ver
        qglGetInfoLogARB(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
        if (linklog[0])
        {
-               Con_DPrintf("program link log:\n%s\n", linklog);
+               if (strstr(linklog, "error") || strstr(linklog, "ERROR") || strstr(linklog, "Error") || strstr(linklog, "WARNING") || strstr(linklog, "warning") || strstr(linklog, "Warning"))
+                       Con_DPrintf("program link log:\n%s\n", linklog);
                // software vertex shader is ok but software fragment shader is WAY
                // too slow, fail program if so.
                // NOTE: this string might be ATI specific, but that's ok because the
@@ -1428,19 +1452,6 @@ void GL_Mesh_ListVBOs(qboolean printeach)
        Con_Printf("vertex buffers: %i element buffers totalling %i bytes (%.3f MB), %i vertex buffers totalling %i bytes (%.3f MB), combined %i bytes (%.3fMB)\n", (int)ebocount, (int)ebomemory, ebomemory / 1048576.0, (int)vbocount, (int)vbomemory, vbomemory / 1048576.0, (int)(ebomemory + vbomemory), (ebomemory + vbomemory) / 1048576.0);
 }
 
-void R_Mesh_Matrix(const matrix4x4_t *matrix)
-{
-       if (memcmp(matrix, &gl_state.modelmatrix, sizeof(matrix4x4_t)))
-       {
-               float glmatrix[16];
-               gl_state.modelmatrix = *matrix;
-               Matrix4x4_Concat(&gl_state.modelviewmatrix, &gl_state.viewport.viewmatrix, &gl_state.modelmatrix);
-               Matrix4x4_ToArrayFloatGL(&gl_state.modelviewmatrix, glmatrix);
-               CHECKGLERROR
-               qglLoadMatrixf(glmatrix);CHECKGLERROR
-       }
-}
-
 void R_Mesh_VertexPointer(const float *vertex3f, int bufferobject, size_t bufferoffset)
 {
        if (!gl_vbo.integer || gl_mesh_testarrayelement.integer)
@@ -1538,6 +1549,29 @@ void R_Mesh_TexCoordPointer(unsigned int unitnum, unsigned int numcomponents, co
        }
 }
 
+int R_Mesh_TexBound(unsigned int unitnum, int id)
+{
+       gltextureunit_t *unit = gl_state.units + unitnum;
+       if (unitnum >= vid.teximageunits)
+               return 0;
+       if (id == GL_TEXTURE_2D)
+               return unit->t2d;
+       if (id == GL_TEXTURE_3D)
+               return unit->t3d;
+       if (id == GL_TEXTURE_CUBE_MAP_ARB)
+               return unit->tcubemap;
+       if (id == GL_TEXTURE_RECTANGLE_ARB)
+               return unit->trectangle;
+       return 0;
+}
+
+void R_Mesh_CopyToTexture(int texnum, int tx, int ty, int sx, int sy, int width, int height)
+{
+       R_Mesh_TexBind(0, texnum);
+       GL_ActiveTexture(0);CHECKGLERROR
+       qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, tx, ty, sx, sy, width, height);CHECKGLERROR
+}
+
 void R_Mesh_TexBindAll(unsigned int unitnum, int tex2d, int tex3d, int texcubemap, int texrectangle)
 {
        gltextureunit_t *unit = gl_state.units + unitnum;
@@ -1719,7 +1753,7 @@ static const float gl_identitymatrix[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
 {
        gltextureunit_t *unit = gl_state.units + unitnum;
-       if (matrix->m[3][3])
+       if (matrix && matrix->m[3][3])
        {
                // texmatrix specified, check if it is different
                if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
@@ -1758,6 +1792,7 @@ void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, i
        switch(vid.renderpath)
        {
        case RENDERPATH_GL20:
+       case RENDERPATH_CGGL:
                // do nothing
                break;
        case RENDERPATH_GL13:
@@ -1770,7 +1805,7 @@ void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, i
                        rgbscale = 1;
                if (!alphascale)
                        alphascale = 1;
-               if (combinergb != combinealpha || rgbscale != 1 || alphascale != 1 || combinergb == GL_DOT3_RGBA_ARB || combinergb == GL_DOT3_RGB_ARB)
+               if (combinergb != combinealpha || rgbscale != 1 || alphascale != 1)
                {
                        if (combinergb == GL_DECAL)
                                combinergb = GL_INTERPOLATE_ARB;
@@ -1830,30 +1865,6 @@ void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, i
        }
 }
 
-void R_Mesh_TextureState(const rmeshstate_t *m)
-{
-       unsigned int i;
-
-       BACKENDACTIVECHECK
-
-       CHECKGLERROR
-       for (i = 0;i < vid.teximageunits;i++)
-               R_Mesh_TexBindAll(i, m->tex[i], m->tex3d[i], m->texcubemap[i], m->texrectangle[i]);
-       for (i = 0;i < vid.texarrayunits;i++)
-       {
-               if (m->pointer_texcoord3f[i])
-                       R_Mesh_TexCoordPointer(i, 3, m->pointer_texcoord3f[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
-               else
-                       R_Mesh_TexCoordPointer(i, 2, m->pointer_texcoord[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
-       }
-       for (i = 0;i < vid.texunits;i++)
-       {
-               R_Mesh_TexMatrix(i, &m->texmatrix[i]);
-               R_Mesh_TexCombine(i, m->texcombinergb[i], m->texcombinealpha[i], m->texrgbscale[i], m->texalphascale[i]);
-       }
-       CHECKGLERROR
-}
-
 void R_Mesh_ResetTextureState(void)
 {
        unsigned int unitnum;
@@ -1864,6 +1875,7 @@ void R_Mesh_ResetTextureState(void)
        switch(vid.renderpath)
        {
        case RENDERPATH_GL20:
+       case RENDERPATH_CGGL:
                for (unitnum = 0;unitnum < vid.teximageunits;unitnum++)
                {
                        gltextureunit_t *unit = gl_state.units + unitnum;