nouveau: nv10: keep 24/8 depth/stencil value, don't check for depth bits
[mesa.git] / src / mesa / drivers / dri / nouveau / nv10_state.c
index f6e47b7f4800346b184fad59b068228111649cb4..09fc07556d92cbd2c8181863fead08e1da00ada2 100644 (file)
@@ -34,6 +34,29 @@ USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include "mtypes.h"
 #include "colormac.h"
 
+static void nv10ViewportScale(nouveauContextPtr nmesa)
+{
+       GLcontext *ctx = nmesa->glCtx;
+       GLuint w = ctx->Viewport.Width;
+       GLuint h = ctx->Viewport.Height;
+
+       GLfloat max_depth = (ctx->Viewport.Near + ctx->Viewport.Far) * 0.5;
+       switch (ctx->DrawBuffer->_DepthBuffer->DepthBits) {
+               case 16:
+                       max_depth *= 32767.0;
+                       break;
+               case 24:
+                       max_depth *= 16777215.0;
+                       break;
+       }
+
+       BEGIN_RING_CACHE(NvSub3D, NV10_TCL_PRIMITIVE_3D_VIEWPORT_SCALE_X, 4);
+       OUT_RING_CACHEf ((((GLfloat) w) * 0.5) - 2048.0);
+       OUT_RING_CACHEf ((((GLfloat) h) * 0.5) - 2048.0);
+       OUT_RING_CACHEf (max_depth);
+       OUT_RING_CACHEf (0.0);
+}
+
 static void nv10AlphaFunc(GLcontext *ctx, GLenum func, GLfloat ref)
 {
        nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
@@ -98,16 +121,27 @@ static void nv10ClearColor(GLcontext *ctx, const GLfloat color[4])
 
 static void nv10ClearDepth(GLcontext *ctx, GLclampd d)
 {
-       /* FIXME: check if 16 or 24/32 bits depth buffer */
        nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
-       nmesa->clear_value=((nmesa->clear_value&0x000000FF)|(((uint32_t)(d*0xFFFFFF))<<8));
+
+/*     switch (ctx->DrawBuffer->_DepthBuffer->DepthBits) {
+               case 16:
+                       nmesa->clear_value = (uint32_t)(d*0x7FFF);
+                       break;
+               case 24:*/
+                       nmesa->clear_value = ((nmesa->clear_value&0x000000FF) |
+                               (((uint32_t)(d*0xFFFFFF))<<8));
+/*                     break;
+       }*/
 }
 
 static void nv10ClearStencil(GLcontext *ctx, GLint s)
 {
-       /* FIXME: not valid for 16 bits depth buffer (0 stencil bits) */
        nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
-       nmesa->clear_value=((nmesa->clear_value&0xFFFFFF00)|(s&0x000000FF));
+
+/*     if (ctx->DrawBuffer->_DepthBuffer->DepthBits == 24) {*/
+               nmesa->clear_value = ((nmesa->clear_value&0xFFFFFF00)|
+                       (s&0x000000FF));
+/*     }*/
 }
 
 static void nv10ClipPlane(GLcontext *ctx, GLenum plane, const GLfloat *equation)
@@ -164,9 +198,17 @@ static void nv10DepthMask(GLcontext *ctx, GLboolean flag)
 static void nv10DepthRange(GLcontext *ctx, GLclampd nearval, GLclampd farval)
 {
        nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
+
+       GLfloat depth_scale = 16777216.0;
+       if (ctx->DrawBuffer->_DepthBuffer->DepthBits == 16) {
+               depth_scale = 32768.0;
+       }
+
        BEGIN_RING_CACHE(NvSub3D, NV10_TCL_PRIMITIVE_3D_DEPTH_RANGE_NEAR, 2);
-       OUT_RING_CACHEf(nearval);
-       OUT_RING_CACHEf(farval);
+       OUT_RING_CACHEf(nearval * depth_scale);
+       OUT_RING_CACHEf(farval * depth_scale);
+
+       nv10ViewportScale(nmesa);
 }
 
 /** Specify the current buffer for writing */
@@ -612,11 +654,16 @@ static void nv10WindowMoved(nouveauContextPtr nmesa)
         OUT_RING_CACHE((h << 16) | y);
 
        /* something to do with clears, possibly doesn't belong here */
+       BEGIN_RING_SIZE(NvSub3D, 0x02b4, 1);
+       OUT_RING(0);
+
+       BEGIN_RING_CACHE(NvSub3D,
+             NV10_TCL_PRIMITIVE_3D_VIEWPORT_CLIP_HORIZ(0), 1);
+        OUT_RING_CACHE(((w+x-1) << 16) | x | 0x08000800);
        BEGIN_RING_CACHE(NvSub3D,
-             NV10_TCL_PRIMITIVE_3D_VIEWPORT_CLIP_HORIZ(0), 2);
-        OUT_RING_CACHE(((w+x) << 16) | x | 0x800);
-        OUT_RING_CACHE(((h+y) << 16) | y | 0x800);
-       for (i=1; i<7; i++) {
+             NV10_TCL_PRIMITIVE_3D_VIEWPORT_CLIP_VERT(0), 1);
+        OUT_RING_CACHE(((h+y-1) << 16) | y | 0x08000800);
+       for (i=1; i<8; i++) {
                BEGIN_RING_CACHE(NvSub3D,
                      NV10_TCL_PRIMITIVE_3D_VIEWPORT_CLIP_HORIZ(i), 1);
                OUT_RING_CACHE(0);
@@ -625,18 +672,7 @@ static void nv10WindowMoved(nouveauContextPtr nmesa)
                OUT_RING_CACHE(0);
        }
 
-       /* viewport transform */
-       BEGIN_RING_CACHE(NvSub3D, NV10_TCL_PRIMITIVE_3D_VIEWPORT_ORIGIN_X, 4);
-       OUT_RING_CACHEf ((GLfloat) x);
-       OUT_RING_CACHEf ((GLfloat) (y+h));
-       OUT_RING_CACHEf (0.0);
-       OUT_RING_CACHEf (0.0);
-
-       BEGIN_RING_CACHE(NvSub3D, NV10_TCL_PRIMITIVE_3D_VIEWPORT_SCALE_X, 4);
-       OUT_RING_CACHEf ((((GLfloat) w) * 0.5) - 2048.0);
-       OUT_RING_CACHEf ((((GLfloat) h) * 0.5) - 2048.0);
-       OUT_RING_CACHEf (16777215.0 * 0.5);
-       OUT_RING_CACHEf (0.0);
+       nv10ViewportScale(nmesa);
 }
 
 /* Initialise any card-specific non-GL related state */
@@ -651,14 +687,20 @@ static GLboolean nv10InitCard(nouveauContextPtr nmesa)
        OUT_RING(NvDmaFB);      /* 194 dma_in_memory2 */
        OUT_RING(NvDmaFB);      /* 198 dma_in_memory3 */
 
-       BEGIN_RING_SIZE(NvSub3D, 0x02b4, 1);
-       OUT_RING(0);
        BEGIN_RING_SIZE(NvSub3D, 0x0290, 1);
        OUT_RING(0x00100001);
        BEGIN_RING_SIZE(NvSub3D, 0x03f4, 1);
        OUT_RING(0);
 
-       return GL_FALSE;
+       /* not for nv10, only for >= nv11 */
+       if ((nmesa->screen->card->id>>4) >= 0x11) {
+               BEGIN_RING_SIZE(NvSub3D, 0x120, 3);
+               OUT_RING(0);
+               OUT_RING(1);
+               OUT_RING(2);
+       }
+
+       return GL_TRUE;
 }
 
 /* Update buffer offset/pitch/format */
@@ -667,7 +709,7 @@ static GLboolean nv10BindBuffers(nouveauContextPtr nmesa, int num_color,
                                 nouveau_renderbuffer *depth)
 {
        GLuint x, y, w, h;
-       GLuint pitch, format;
+       GLuint pitch, format, depth_pitch;
 
        w = color[0]->mesa.Width;
        h = color[0]->mesa.Height;
@@ -680,10 +722,8 @@ static GLboolean nv10BindBuffers(nouveauContextPtr nmesa, int num_color,
         BEGIN_RING_CACHE(NvSub3D, NV10_TCL_PRIMITIVE_3D_VIEWPORT_HORIZ, 6);
         OUT_RING_CACHE((w << 16) | x);
         OUT_RING_CACHE((h << 16) | y);
-       pitch = color[0]->pitch;
-       if (depth) {
-               pitch |= (depth->pitch << 16);
-       }
+       depth_pitch = (depth ? depth->pitch : color[0]->pitch);
+       pitch = (depth_pitch<<16) | color[0]->pitch;
        format = 0x108;
        if (color[0]->mesa._ActualFormat != GL_RGBA8) {
                format = 0x103; /* R5G6B5 color buffer */
@@ -693,6 +733,13 @@ static GLboolean nv10BindBuffers(nouveauContextPtr nmesa, int num_color,
        OUT_RING_CACHE(color[0]->offset);
        OUT_RING_CACHE(depth ? depth->offset : color[0]->offset);
 
+       /* Always set to bottom left of buffer */
+       BEGIN_RING_CACHE(NvSub3D, NV10_TCL_PRIMITIVE_3D_VIEWPORT_ORIGIN_X, 4);
+       OUT_RING_CACHEf (0.0);
+       OUT_RING_CACHEf ((GLfloat) h);
+       OUT_RING_CACHEf (0.0);
+       OUT_RING_CACHEf (0.0);
+
        return GL_TRUE;
 }