Hi,

I played with stencil and this patch is output.
I tested it only on stenciltst from http://www.sgi.com/products/software/opengl/examples/glut/examples/
This app doesn't use Z_TEST. It show correct output. Somene with more mesa and r300 knowledge should correct it (there are still problems).


Peter Zubaj
diff -u -r o/r300_ioctl.c n/r300_ioctl.c
--- o/r300_ioctl.c	2005-03-04 18:57:28.000000000 +0100
+++ n/r300_ioctl.c	2005-03-13 23:04:31.000000000 +0100
@@ -58,6 +58,7 @@
 
 #define CLEARBUFFER_COLOR	0x1
 #define CLEARBUFFER_DEPTH	0x2
+#define CLEARBUFFER_STENCIL	0x4
 
 static void r300ClearBuffer(r300ContextPtr r300, int flags, int buffer)
 {
@@ -205,8 +206,9 @@
 
 	R300_STATECHANGE(r300, zs);
 	if (flags & CLEARBUFFER_DEPTH) {
-		r300->hw.zs.cmd[R300_ZS_CNTL_0] = 0x6; // test and write
-		r300->hw.zs.cmd[R300_ZS_CNTL_1] = (R300_ZS_ALWAYS<<R300_RB3D_ZS1_DEPTH_FUNC_SHIFT);
+		r300->hw.zs.cmd[R300_ZS_CNTL_0] = (r300->hw.zs.cmd[R300_ZS_CNTL_0] & 1) | 0x6; // test and write
+		r300->hw.zs.cmd[R300_ZS_CNTL_1] &= ~(R300_ZS_MASK << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT);
+		r300->hw.zs.cmd[R300_ZS_CNTL_1] |= (R300_ZS_ALWAYS<<R300_RB3D_ZS1_DEPTH_FUNC_SHIFT);
 /*
 		R300_STATECHANGE(r300, zb);
 		r300->hw.zb.cmd[R300_ZB_OFFSET] =
@@ -217,8 +219,23 @@
 			r300->radeon.radeonScreen->depthPitch;
 */
 	} else {
-		r300->hw.zs.cmd[R300_ZS_CNTL_0] = 0; // disable
-		r300->hw.zs.cmd[R300_ZS_CNTL_1] = 0;
+		r300->hw.zs.cmd[R300_ZS_CNTL_0] = (r300->hw.zs.cmd[R300_ZS_CNTL_0] & 1) | R300_RB3D_Z_DISABLED_1; // disable
+		r300->hw.zs.cmd[R300_ZS_CNTL_1] &= ~(R300_ZS_MASK << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT);
+	}
+	
+	R300_STATECHANGE(r300, zs);
+	if (flags & CLEARBUFFER_STENCIL) {
+		r300->hw.zs.cmd[R300_ZS_CNTL_0] = (r300->hw.zs.cmd[R300_ZS_CNTL_0] & (~1)) | 0x1;
+		r300->hw.zs.cmd[R300_ZS_CNTL_1] &= ~((R300_ZS_MASK << R300_RB3D_ZS1_FRONT_FUNC_SHIFT) | (R300_ZS_MASK << R300_RB3D_ZS1_BACK_FUNC_SHIFT));
+		r300->hw.zs.cmd[R300_ZS_CNTL_1] |= 
+		    (R300_ZS_ALWAYS<<R300_RB3D_ZS1_FRONT_FUNC_SHIFT) | 
+		    (R300_ZS_REPLACE<<R300_RB3D_ZS1_FRONT_FAIL_OP_SHIFT) |
+		    (R300_ZS_REPLACE<<R300_RB3D_ZS1_FRONT_ZPASS_OP_SHIFT) |
+		    (R300_ZS_REPLACE<<R300_RB3D_ZS1_FRONT_ZFAIL_OP_SHIFT) |
+		    (R300_ZS_ALWAYS<<R300_RB3D_ZS1_BACK_FUNC_SHIFT) |
+		    (R300_ZS_REPLACE<<R300_RB3D_ZS1_BACK_FAIL_OP_SHIFT) |
+		    (R300_ZS_REPLACE<<R300_RB3D_ZS1_BACK_ZPASS_OP_SHIFT) |
+		    (R300_ZS_REPLACE<<R300_RB3D_ZS1_BACK_ZFAIL_OP_SHIFT) ;
 	}
 			
 	/* Make sure we have enough space */
@@ -314,6 +331,11 @@
 		bits |= CLEARBUFFER_DEPTH;
 		mask &= ~DD_DEPTH_BIT;
 	}
+	
+	if ( (mask & DD_STENCIL_BIT) && r300->state.hw_stencil) {
+		bits |= CLEARBUFFER_STENCIL;
+		mask &= ~DD_STENCIL_BIT;
+	}
 
 	if (mask) {
 		if (RADEON_DEBUG & DEBUG_FALLBACKS)
diff -u -r o/r300_reg.h n/r300_reg.h
--- o/r300_reg.h	2005-03-05 12:16:21.000000000 +0100
+++ n/r300_reg.h	2005-03-13 22:54:51.795679904 +0100
@@ -1116,7 +1116,7 @@
 #       define R300_RB3D_Z_TEST                  0x00000012
 #       define R300_RB3D_Z_TEST_AND_WRITE        0x00000016
 #       define R300_RB3D_Z_WRITE_ONLY        	 0x00000006
-#	define R300_RB3D_STENCIL_ENABLE		 (0<<1)  /* UNKNOWN yet.. */
+#	define R300_RB3D_STENCIL_ENABLE		 0x00000001  /* UNKNOWN yet.. */
 
 #define R300_RB3D_ZSTENCIL_CNTL_1                   0x4F04
 		/* functions */
diff -u -r o/r300_state.c n/r300_state.c
--- o/r300_state.c	2005-03-12 21:56:29.000000000 +0100
+++ n/r300_state.c	2005-03-13 23:00:46.000000000 +0100
@@ -483,9 +483,9 @@
 			else
 				newval = R300_RB3D_Z_TEST;
 		} else
-			newval = 0;
+			newval = R300_RB3D_Z_DISABLED_1;
 
-		r300->hw.zs.cmd[R300_ZS_CNTL_0] = newval;
+		r300->hw.zs.cmd[R300_ZS_CNTL_0] = (r300->hw.zs.cmd[R300_ZS_CNTL_0] & 1) | newval;
 		break;
 
 	case GL_STENCIL_TEST:
@@ -609,7 +609,6 @@
 		r300->hw.zs.cmd[R300_ZS_CNTL_1] |= R300_ZS_ALWAYS << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT;
 		break;
 	}
-
 }
 
 
@@ -621,13 +620,14 @@
 static void r300DepthMask(GLcontext* ctx, GLboolean mask)
 {
 	r300ContextPtr r300 = R300_CONTEXT(ctx);
+	uint32_t newval = 0;
 
 	if (!ctx->Depth.Test)
 		return;
 
 	R300_STATECHANGE(r300, zs);
-	r300->hw.zs.cmd[R300_ZS_CNTL_0] = mask
-		? R300_RB3D_Z_TEST_AND_WRITE : R300_RB3D_Z_TEST;
+	newval = mask ? R300_RB3D_Z_TEST_AND_WRITE : R300_RB3D_Z_TEST;
+	r300->hw.zs.cmd[R300_ZS_CNTL_0] = (r300->hw.zs.cmd[R300_ZS_CNTL_0] & 1) | newval;
 }
 
 
@@ -866,8 +866,7 @@
 {
 	r300ContextPtr rmesa = R300_CONTEXT(ctx);
 	GLuint refmask = ((ctx->Stencil.Ref[0] << R300_RB3D_ZS2_STENCIL_REF_SHIFT) |
-			  (ctx->Stencil.
-			   ValueMask[0] << R300_RB3D_ZS2_STENCIL_MASK_SHIFT));
+			  (ctx->Stencil.ValueMask[0] << R300_RB3D_ZS2_STENCIL_MASK_SHIFT));
 	GLuint flag;
 
 	R300_STATECHANGE(rmesa, zs);
@@ -902,7 +901,10 @@
 
 	R300_STATECHANGE(rmesa, zs);
 		/* It is easier to mask what's left.. */
-	rmesa->hw.zs.cmd[R300_ZS_CNTL_1] &= (R300_ZS_MASK << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT);
+	rmesa->hw.zs.cmd[R300_ZS_CNTL_1] &= 
+	    (R300_ZS_MASK << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT) | 
+	    (R300_ZS_MASK << R300_RB3D_ZS1_FRONT_FUNC_SHIFT) | 
+	    (R300_ZS_MASK << R300_RB3D_ZS1_BACK_FUNC_SHIFT);
 
 	rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |=
 		 (translate_stencil_op(ctx->Stencil.FailFunc[0]) << R300_RB3D_ZS1_FRONT_FAIL_OP_SHIFT)
@@ -911,7 +913,6 @@
 		|(translate_stencil_op(ctx->Stencil.FailFunc[0]) << R300_RB3D_ZS1_BACK_FAIL_OP_SHIFT)
 		|(translate_stencil_op(ctx->Stencil.ZFailFunc[0]) << R300_RB3D_ZS1_BACK_ZFAIL_OP_SHIFT)
 		|(translate_stencil_op(ctx->Stencil.ZPassFunc[0]) << R300_RB3D_ZS1_BACK_ZPASS_OP_SHIFT);
-
 }
 
 static void r300ClearStencil(GLcontext * ctx, GLint s)
@@ -922,8 +923,8 @@
 	R300_STATECHANGE(rmesa, zs);
 	rmesa->hw.zs.cmd[R300_ZS_CNTL_2] =
 	    ((GLuint) ctx->Stencil.Clear |
-	     (0xff << R200_STENCIL_MASK_SHIFT) |
-	     (ctx->Stencil.WriteMask[0] << R200_STENCIL_WRITEMASK_SHIFT));
+	     (0xff << R300_RB3D_ZS2_STENCIL_MASK_SHIFT) |
+	     (ctx->Stencil.WriteMask[0] << R300_RB3D_ZS2_STENCIL_WRITE_MASK_SHIFT));
 }
 
 /* =============================================================
@@ -1978,8 +1979,19 @@
 		   have bitfields accessed by different functions
 		   and not all bits are used */
 #if 0
+	/* initialize similiar to r200 */
 	r300->hw.zs.cmd[R300_ZS_CNTL_0] = 0;
-	r300->hw.zs.cmd[R300_ZS_CNTL_1] = 0;
+	r300->hw.zs.cmd[R300_ZS_CNTL_1] = 
+	    (R300_ZS_ALWAYS << R300_RB3D_ZS1_FRONT_FUNC_SHIFT) |
+	    (R300_ZS_KEEP << R300_RB3D_ZS1_FRONT_FAIL_OP_SHIFT) |
+	    (R300_ZS_KEEP << R300_RB3D_ZS1_FRONT_ZPASS_OP_SHIFT) |
+	    (R300_ZS_KEEP << R300_RB3D_ZS1_FRONT_ZFAIL_OP_SHIFT) |
+	    (R300_ZS_ALWAYS << R300_RB3D_ZS1_BACK_FUNC_SHIFT) |
+	    (R300_ZS_KEEP << R300_RB3D_ZS1_BACK_FAIL_OP_SHIFT) |
+	    (R300_ZS_KEEP << R300_RB3D_ZS1_BACK_ZPASS_OP_SHIFT) |
+	    (R300_ZS_KEEP << R300_RB3D_ZS1_BACK_ZFAIL_OP_SHIFT);
+	    
+	    printf("si :%08x\n", r300->hw.zs.cmd[R300_ZS_CNTL_1]);
 	r300->hw.zs.cmd[R300_ZS_CNTL_2] = 0xffff00;
 #endif
 
@@ -1996,6 +2008,12 @@
 	r300Enable(ctx, GL_DEPTH_TEST, ctx->Depth.Test);
 	r300DepthMask(ctx, ctx->Depth.Mask);
 	r300DepthFunc(ctx, ctx->Depth.Func);
+	
+	/* stencil */
+	r300Enable(ctx, GL_STENCIL_TEST, ctx->Stencil.Enabled);
+	r300StencilMask(ctx, ctx->Stencil.WriteMask[0]);
+	r300StencilFunc(ctx, ctx->Stencil.Function[0], ctx->Stencil.Ref[0], ctx->Stencil.ValueMask[0]);
+	r300StencilOp(ctx, ctx->Stencil.FailFunc[0], ctx->Stencil.ZFailFunc[0], ctx->Stencil.ZPassFunc[0]);
 
 	r300UpdateCulling(ctx);
 

Reply via email to