Merge branch 'master' into gallium-0.2
[mesa.git] / src / mesa / state_tracker / st_cb_clear.c
index d862f7ba4659e7c27defc259c0342ed65760c426..47ad3c2bc1264d5e56b8c42197ee6521e4e29371 100644 (file)
 #include "main/glheader.h"
 #include "main/macros.h"
 #include "shader/prog_instruction.h"
-#include "st_atom.h"
 #include "st_context.h"
+#include "st_atom.h"
+#include "st_cb_accum.h"
 #include "st_cb_clear.h"
+#include "st_cb_fbo.h"
+#include "st_draw.h"
 #include "st_program.h"
 #include "st_public.h"
+#include "st_mesa_to_tgsi.h"
+
 #include "pipe/p_context.h"
+#include "pipe/p_inlines.h"
+#include "pipe/p_state.h"
 #include "pipe/p_defines.h"
-#include "pipe/tgsi/mesa/mesa_to_tgsi.h"
-#include "vf/vf.h"
-
+#include "util/u_pack_color.h"
+#include "util/u_simple_shaders.h"
+#include "util/u_draw_quad.h"
 
+#include "cso_cache/cso_context.h"
 
 
-static GLuint
-color_value(GLuint pipeFormat, const GLfloat color[4])
+void
+st_init_clear(struct st_context *st)
 {
-   GLubyte r, g, b, a;
+   struct pipe_context *pipe = st->pipe;
+
+   /* rasterizer state: bypass clipping */
+   memset(&st->clear.raster, 0, sizeof(st->clear.raster));
+   st->clear.raster.gl_rasterization_rules = 1;
+   st->clear.raster.bypass_clipping = 1;
+
+   /* viewport state: identity since we're drawing in window coords */
+   st->clear.viewport.scale[0] = 1.0;
+   st->clear.viewport.scale[1] = 1.0;
+   st->clear.viewport.scale[2] = 1.0;
+   st->clear.viewport.scale[3] = 1.0;
+   st->clear.viewport.translate[0] = 0.0;
+   st->clear.viewport.translate[1] = 0.0;
+   st->clear.viewport.translate[2] = 0.0;
+   st->clear.viewport.translate[3] = 0.0;
 
-   UNCLAMPED_FLOAT_TO_UBYTE(r, color[0]);
-   UNCLAMPED_FLOAT_TO_UBYTE(g, color[1]);
-   UNCLAMPED_FLOAT_TO_UBYTE(b, color[2]);
-   UNCLAMPED_FLOAT_TO_UBYTE(a, color[3]);
+   /* fragment shader state: color pass-through program */
+   st->clear.fs =
+      util_make_fragment_passthrough_shader(pipe, &st->clear.frag_shader);
 
-   switch (pipeFormat) {
-   case PIPE_FORMAT_U_R8_G8_B8_A8:
-      return (r << 24) | (g << 16) | (b << 8) | a;
-   case PIPE_FORMAT_U_A8_R8_G8_B8:
-      return (a << 24) | (r << 16) | (g << 8) | b;
-   case PIPE_FORMAT_U_R5_G6_B5:
-      return ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3);
-   default:
-      return 0;
+   /* vertex shader state: color/position pass-through */
+   {
+      const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
+                                      TGSI_SEMANTIC_COLOR };
+      const uint semantic_indexes[] = { 0, 0 };
+      st->clear.vs = util_make_vertex_passthrough_shader(pipe, 2,
+                                                         semantic_names,
+                                                         semantic_indexes,
+                                                         &st->clear.vert_shader);
    }
 }
 
-static GLuint
-depth_value(GLuint pipeFormat, GLfloat value)
+
+void
+st_destroy_clear(struct st_context *st)
 {
-   GLuint val;
-   switch (pipeFormat) {
-   case PIPE_FORMAT_U_Z16:
-      val = (GLuint) (value * 0xffffff);
-      break;
-   case PIPE_FORMAT_U_Z32:
-      val = (GLuint) (value * 0xffffffff);
-      break;
-   case PIPE_FORMAT_S8_Z24:
-   /*case PIPE_FORMAT_Z24_S8:*/
-      val = (GLuint) (value * 0xffffff);
-      break;
-   default:
-      assert(0);
+   struct pipe_context *pipe = st->pipe;
+
+   if (st->clear.vert_shader.tokens) {
+      FREE((void *) st->clear.vert_shader.tokens);
+      st->clear.vert_shader.tokens = NULL;
+   }
+
+   if (st->clear.frag_shader.tokens) {
+      FREE((void *) st->clear.frag_shader.tokens);
+      st->clear.frag_shader.tokens = NULL;
+   }
+
+   if (st->clear.fs) {
+      cso_delete_fragment_shader(st->cso_context, st->clear.fs);
+      st->clear.fs = NULL;
+   }
+   if (st->clear.vs) {
+      cso_delete_vertex_shader(st->cso_context, st->clear.vs);
+      st->clear.vs = NULL;
+   }
+   if (st->clear.vbuf) {
+      pipe_buffer_destroy(pipe->screen, st->clear.vbuf);
+      st->clear.vbuf = NULL;
    }
-   return val;
 }
 
 
 static GLboolean
-is_depth_stencil_format(GLuint pipeFormat)
+is_depth_stencil_format(enum pipe_format pipeFormat)
 {
    switch (pipeFormat) {
-   case PIPE_FORMAT_S8_Z24:
-   /*case PIPE_FORMAT_Z24_S8:*/
+   case PIPE_FORMAT_S8Z24_UNORM:
+   case PIPE_FORMAT_Z24S8_UNORM:
       return GL_TRUE;
    default:
       return GL_FALSE;
@@ -106,232 +136,289 @@ is_depth_stencil_format(GLuint pipeFormat)
 
 
 
-/**
- * Create a simple fragment shader that just passes through the fragment color.
- */
-static struct st_fragment_program *
-make_color_shader(struct st_context *st)
-{
-   GLcontext *ctx = st->ctx;
-   struct st_fragment_program *stfp;
-   struct gl_program *p;
-   GLboolean b;
-
-   p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
-   if (!p)
-      return NULL;
-
-   p->NumInstructions = 2;
-   p->Instructions = _mesa_alloc_instructions(2);
-   if (!p->Instructions) {
-      ctx->Driver.DeleteProgram(ctx, p);
-      return NULL;
-   }
-   _mesa_init_instructions(p->Instructions, 2);
-   p->Instructions[0].Opcode = OPCODE_MOV;
-   p->Instructions[0].DstReg.File = PROGRAM_OUTPUT;
-   p->Instructions[0].DstReg.Index = 0;
-   p->Instructions[0].SrcReg[0].File = PROGRAM_INPUT;
-   p->Instructions[0].SrcReg[0].Index = FRAG_ATTRIB_COL0;
-   p->Instructions[1].Opcode = OPCODE_END;
-
-   p->InputsRead = FRAG_BIT_COL0;
-   p->OutputsWritten = (1 << FRAG_RESULT_COLR);
-
-   stfp = (struct st_fragment_program *) p;
-   /* compile into tgsi format */
-   b = tgsi_mesa_compile_fp_program(&stfp->Base,
-                                    stfp->tokens, ST_FP_MAX_TOKENS);
-   assert(b);
-
-   return stfp;
-}
-
-
 /**
  * Draw a screen-aligned quadrilateral.
- * Coords are window coords.
+ * Coords are window coords with y=0=bottom.  These coords will be transformed
+ * by the vertex shader and viewport transform (which will flip Y if needed).
  */
 static void
 draw_quad(GLcontext *ctx,
           float x0, float y0, float x1, float y1, GLfloat z,
           const GLfloat color[4])
 {
-   static const GLuint attribs[2] = {
-      VF_ATTRIB_POS,
-      VF_ATTRIB_COLOR0
-   };
-   GLfloat verts[4][2][4]; /* four verts, two attribs, XYZW */
+   struct st_context *st = ctx->st;
+   struct pipe_context *pipe = st->pipe;
    GLuint i;
+   void *buf;
+
+   if (!st->clear.vbuf) {
+      st->clear.vbuf = pipe_buffer_create(pipe->screen, 32, PIPE_BUFFER_USAGE_VERTEX,
+                                          sizeof(st->clear.vertices));
+   }
 
    /* positions */
-   verts[0][0][0] = x0;
-   verts[0][0][1] = y0;
+   st->clear.vertices[0][0][0] = x0;
+   st->clear.vertices[0][0][1] = y0;
 
-   verts[1][0][0] = x1;
-   verts[1][0][1] = y0;
+   st->clear.vertices[1][0][0] = x1;
+   st->clear.vertices[1][0][1] = y0;
 
-   verts[2][0][0] = x1;
-   verts[2][0][1] = y1;
+   st->clear.vertices[2][0][0] = x1;
+   st->clear.vertices[2][0][1] = y1;
 
-   verts[3][0][0] = x0;
-   verts[3][0][1] = y1;
+   st->clear.vertices[3][0][0] = x0;
+   st->clear.vertices[3][0][1] = y1;
 
    /* same for all verts: */
    for (i = 0; i < 4; i++) {
-      verts[i][0][2] = z;
-      verts[i][0][3] = 1.0;
-      verts[i][1][0] = color[0];
-      verts[i][1][1] = color[1];
-      verts[i][1][2] = color[2];
-      verts[i][1][3] = color[3];
+      st->clear.vertices[i][0][2] = z;
+      st->clear.vertices[i][0][3] = 1.0;
+      st->clear.vertices[i][1][0] = color[0];
+      st->clear.vertices[i][1][1] = color[1];
+      st->clear.vertices[i][1][2] = color[2];
+      st->clear.vertices[i][1][3] = color[3];
    }
 
-   ctx->st->pipe->draw_vertices(ctx->st->pipe, GL_QUADS,
-                                4, (GLfloat *) verts, 2, attribs);
+   /* put vertex data into vbuf */
+   buf = pipe_buffer_map(pipe->screen, st->clear.vbuf, PIPE_BUFFER_USAGE_CPU_WRITE);
+   memcpy(buf, st->clear.vertices, sizeof(st->clear.vertices));
+   pipe_buffer_unmap(pipe->screen, st->clear.vbuf);
+
+   /* draw */
+   util_draw_vertex_buffer(pipe, st->clear.vbuf,
+                           PIPE_PRIM_TRIANGLE_FAN,
+                           4,  /* verts */
+                           2); /* attribs/vert */
 }
 
 
 
 /**
  * Do glClear by drawing a quadrilateral.
+ * The vertices of the quad will be computed from the
+ * ctx->DrawBuffer->_X/Ymin/max fields.
  */
 static void
-clear_with_quad(GLcontext *ctx, GLuint x0, GLuint y0,
-                GLuint x1, GLuint y1,
+clear_with_quad(GLcontext *ctx,
                 GLboolean color, GLboolean depth, GLboolean stencil)
 {
-   static struct st_fragment_program *stfp = NULL;
    struct st_context *st = ctx->st;
-   struct pipe_alpha_test_state alpha_test;
-   struct pipe_blend_state blend;
-   struct pipe_depth_state depth_test;
-   struct pipe_stencil_state stencil_test;
-   struct pipe_setup_state setup;
-   struct pipe_fs_state fs;
-
-   /* alpha state: disabled */
-   memset(&alpha_test, 0, sizeof(alpha_test));
-   st->pipe->set_alpha_test_state(st->pipe, &alpha_test);
+   const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin;
+   const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax;
+   GLfloat y0, y1;
 
-   /* blend state: RGBA masking */
-   memset(&blend, 0, sizeof(blend));
-   if (color) {
-      if (ctx->Color.ColorMask[0])
-         blend.colormask |= PIPE_MASK_R;
-      if (ctx->Color.ColorMask[1])
-         blend.colormask |= PIPE_MASK_G;
-      if (ctx->Color.ColorMask[2])
-         blend.colormask |= PIPE_MASK_B;
-      if (ctx->Color.ColorMask[3])
-         blend.colormask |= PIPE_MASK_A;
-      if (st->ctx->Color.DitherFlag)
-         blend.dither = 1;
+   if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
+      y0 = (GLfloat) (ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymax);
+      y1 = (GLfloat) (ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymin);
    }
-   st->pipe->set_blend_state(st->pipe, &blend);
-
-   /* depth state: always pass */
-   memset(&depth_test, 0, sizeof(depth_test));
-   if (depth) {
-      depth_test.enabled = 1;
-      depth_test.writemask = 1;
-      depth_test.func = PIPE_FUNC_ALWAYS;
+   else {
+      y0 = (GLfloat) ctx->DrawBuffer->_Ymin;
+      y1 = (GLfloat) ctx->DrawBuffer->_Ymax;
    }
-   st->pipe->set_depth_state(st->pipe, &depth_test);
 
-   /* setup state: nothing */
-   memset(&setup, 0, sizeof(setup));
-   if (ctx->Scissor.Enabled)
-      setup.scissor = 1;
-   st->pipe->set_setup_state(st->pipe, &setup);
-
-   /* stencil state: always set to ref value */
-   memset(&stencil_test, 0, sizeof(stencil_test));
-   if (stencil) {
-      stencil_test.front_enabled = 1;
-      stencil_test.front_func = PIPE_FUNC_ALWAYS;
-      stencil_test.front_fail_op = PIPE_STENCIL_OP_REPLACE;
-      stencil_test.front_zpass_op = PIPE_STENCIL_OP_REPLACE;
-      stencil_test.front_zfail_op = PIPE_STENCIL_OP_REPLACE;
-      stencil_test.ref_value[0] = ctx->Stencil.Clear;
-      stencil_test.value_mask[0] = 0xff;
-      stencil_test.write_mask[0] = ctx->Stencil.WriteMask[0] & 0xff;
+   /*
+   printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__, 
+         color ? "color, " : "",
+         depth ? "depth, " : "",
+         stencil ? "stencil" : "",
+         x0, y0,
+         x1, y1);
+   */
+
+   cso_save_blend(st->cso_context);
+   cso_save_depth_stencil_alpha(st->cso_context);
+   cso_save_rasterizer(st->cso_context);
+   cso_save_viewport(st->cso_context);
+   cso_save_fragment_shader(st->cso_context);
+   cso_save_vertex_shader(st->cso_context);
+
+   /* blend state: RGBA masking */
+   {
+      struct pipe_blend_state blend;
+      memset(&blend, 0, sizeof(blend));
+      blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
+      blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
+      blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
+      blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
+      if (color) {
+         if (ctx->Color.ColorMask[0])
+            blend.colormask |= PIPE_MASK_R;
+         if (ctx->Color.ColorMask[1])
+            blend.colormask |= PIPE_MASK_G;
+         if (ctx->Color.ColorMask[2])
+            blend.colormask |= PIPE_MASK_B;
+         if (ctx->Color.ColorMask[3])
+            blend.colormask |= PIPE_MASK_A;
+         if (st->ctx->Color.DitherFlag)
+            blend.dither = 1;
+      }
+      cso_set_blend(st->cso_context, &blend);
    }
-   st->pipe->set_stencil_state(st->pipe, &stencil_test);
 
-   /* fragment shader state: color pass-through program */
-   if (!stfp) {
-      stfp = make_color_shader(st);
+   /* depth_stencil state: always pass/set to ref value */
+   {
+      struct pipe_depth_stencil_alpha_state depth_stencil;
+      memset(&depth_stencil, 0, sizeof(depth_stencil));
+      if (depth) {
+         depth_stencil.depth.enabled = 1;
+         depth_stencil.depth.writemask = 1;
+         depth_stencil.depth.func = PIPE_FUNC_ALWAYS;
+      }
+
+      if (stencil) {
+         depth_stencil.stencil[0].enabled = 1;
+         depth_stencil.stencil[0].func = PIPE_FUNC_ALWAYS;
+         depth_stencil.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
+         depth_stencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
+         depth_stencil.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
+         depth_stencil.stencil[0].ref_value = ctx->Stencil.Clear;
+         depth_stencil.stencil[0].value_mask = 0xff;
+         depth_stencil.stencil[0].write_mask = ctx->Stencil.WriteMask[0] & 0xff;
+      }
+
+      cso_set_depth_stencil_alpha(st->cso_context, &depth_stencil);
    }
-   memset(&fs, 0, sizeof(fs));
-   fs.inputs_read = stfp->Base.Base.InputsRead;
-   fs.tokens = &stfp->tokens[0];
-   fs.constants = NULL;
-   st->pipe->set_fs_state(st->pipe, &fs);
+
+   cso_set_rasterizer(st->cso_context, &st->clear.raster);
+   cso_set_viewport(st->cso_context, &st->clear.viewport);
+
+   cso_set_fragment_shader_handle(st->cso_context, st->clear.fs);
+   cso_set_vertex_shader_handle(st->cso_context, st->clear.vs);
 
    /* draw quad matching scissor rect (XXX verify coord round-off) */
-   draw_quad(ctx, x0, y0, x1, y1, ctx->Depth.Clear, ctx->Color.ClearColor);
+   draw_quad(ctx, x0, y0, x1, y1, (GLfloat) ctx->Depth.Clear, ctx->Color.ClearColor);
 
    /* Restore pipe state */
-   st->pipe->set_alpha_test_state(st->pipe, &st->state.alpha_test);
-   st->pipe->set_blend_state(st->pipe, &st->state.blend);
-   st->pipe->set_depth_state(st->pipe, &st->state.depth);
-   st->pipe->set_fs_state(st->pipe, &st->state.fs);
-   st->pipe->set_setup_state(st->pipe, &st->state.setup);
-   st->pipe->set_stencil_state(st->pipe, &st->state.stencil);
-   /* OR:
-   st_invalidate_state(ctx, _NEW_COLOR | _NEW_DEPTH | _NEW_STENCIL);
-   */
+   cso_restore_blend(st->cso_context);
+   cso_restore_depth_stencil_alpha(st->cso_context);
+   cso_restore_rasterizer(st->cso_context);
+   cso_restore_viewport(st->cso_context);
+   cso_restore_fragment_shader(st->cso_context);
+   cso_restore_vertex_shader(st->cso_context);
 }
 
 
-static void
-clear_color_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
+/**
+ * Determine if we need to clear the depth buffer by drawing a quad.
+ */
+static INLINE GLboolean
+check_clear_color_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
 {
-   if (ctx->Color.ColorMask[0] &&
-       ctx->Color.ColorMask[1] &&
-       ctx->Color.ColorMask[2] &&
-       ctx->Color.ColorMask[3] &&
-       !ctx->Scissor.Enabled)
-   {
-      /* clear whole buffer w/out masking */
-      GLuint clearValue
-         = color_value(rb->surface->format, ctx->Color.ClearColor);
-      ctx->st->pipe->clear(ctx->st->pipe, rb->surface, clearValue);
-   }
-   else {
-      /* masking or scissoring */
-      clear_with_quad(ctx,
-                      ctx->DrawBuffer->_Xmin,
-                      ctx->DrawBuffer->_Xmin,
-                      ctx->DrawBuffer->_Xmax,
-                      ctx->DrawBuffer->_Ymax,
-                      GL_TRUE, GL_FALSE, GL_FALSE);
-   }
+   const struct st_renderbuffer *strb = st_renderbuffer(rb);
+
+   if (strb->surface->status == PIPE_SURFACE_STATUS_UNDEFINED)
+      return FALSE;
+
+   if (ctx->Scissor.Enabled)
+      return TRUE;
+
+   if (!ctx->Color.ColorMask[0] ||
+       !ctx->Color.ColorMask[1] ||
+       !ctx->Color.ColorMask[2] ||
+       !ctx->Color.ColorMask[3])
+      return TRUE;
+
+   return FALSE;
+}
+
+
+static INLINE GLboolean
+check_clear_depth_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
+{
+   const struct st_renderbuffer *strb = st_renderbuffer(rb);
+   const GLuint stencilMax = (1 << rb->StencilBits) - 1;
+   GLboolean maskStencil
+      = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
+
+   if (strb->surface->status == PIPE_SURFACE_STATUS_UNDEFINED)
+      return FALSE;
+
+   if (ctx->Scissor.Enabled)
+      return TRUE;
+
+   if (maskStencil)
+      return TRUE;
+
+   return FALSE;
+}
+
+
+/**
+ * Determine if we need to clear the depth buffer by drawing a quad.
+ */
+static INLINE GLboolean
+check_clear_depth_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
+{
+   const struct st_renderbuffer *strb = st_renderbuffer(rb);
+   const GLboolean isDS = is_depth_stencil_format(strb->surface->format);
+
+   if (strb->surface->status == PIPE_SURFACE_STATUS_UNDEFINED)
+      return FALSE;
+
+   if (ctx->Scissor.Enabled)
+      return TRUE;
+
+   if (isDS && 
+       strb->surface->status == PIPE_SURFACE_STATUS_DEFINED &&
+       ctx->DrawBuffer->Visual.stencilBits > 0)
+      return TRUE;
+
+   return FALSE;
+}
+
+
+/**
+ * Determine if we need to clear the stencil buffer by drawing a quad.
+ */
+static INLINE GLboolean
+check_clear_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
+{
+   const struct st_renderbuffer *strb = st_renderbuffer(rb);
+   const GLboolean isDS = is_depth_stencil_format(strb->surface->format);
+   const GLuint stencilMax = (1 << rb->StencilBits) - 1;
+   const GLboolean maskStencil
+      = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
+
+   if (strb->surface->status == PIPE_SURFACE_STATUS_UNDEFINED)
+      return FALSE;
+
+   if (maskStencil) 
+      return TRUE;
+
+   if (ctx->Scissor.Enabled)
+      return TRUE;
+
+   /* This is correct, but it is necessary to look at the depth clear
+    * value held in the surface when it comes time to issue the clear,
+    * rather than taking depth and stencil clear values from the
+    * current state.
+    */
+   if (isDS && 
+       strb->surface->status == PIPE_SURFACE_STATUS_DEFINED &&
+       ctx->DrawBuffer->Visual.depthBits > 0)
+      return TRUE;
+
+   return FALSE;
 }
 
 
+
 static void
-clear_accum_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
+clear_color_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
 {
-   if (!ctx->Scissor.Enabled) {
-      /* clear whole buffer w/out masking */
-      GLuint clearValue
-         = color_value(rb->surface->format, ctx->Accum.ClearColor);
-      /* Note that clearValue is 32 bits but the accum buffer will
-       * typically be 64bpp...
-       */
-      ctx->st->pipe->clear(ctx->st->pipe, rb->surface, clearValue);
+   if (check_clear_color_with_quad( ctx, rb )) {
+      /* masking or scissoring */
+      clear_with_quad(ctx, GL_TRUE, GL_FALSE, GL_FALSE);
    }
    else {
-      /* scissoring */
-      /* XXX point framebuffer.cbufs[0] at the accum buffer */
-      clear_with_quad(ctx,
-                      ctx->DrawBuffer->_Xmin,
-                      ctx->DrawBuffer->_Xmin,
-                      ctx->DrawBuffer->_Xmax,
-                      ctx->DrawBuffer->_Ymax,
-                      GL_TRUE, GL_FALSE, GL_FALSE);
+      /* clear whole buffer w/out masking */
+      struct st_renderbuffer *strb = st_renderbuffer(rb);
+      uint clearValue;
+      /* NOTE: we always pass the clear color as PIPE_FORMAT_A8R8G8B8_UNORM
+       * at this time!
+       */
+      util_pack_color(ctx->Color.ClearColor, PIPE_FORMAT_A8R8G8B8_UNORM, &clearValue);
+      ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
    }
 }
 
@@ -339,20 +426,16 @@ clear_accum_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
 static void
 clear_depth_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
 {
-   if (!ctx->Scissor.Enabled &&
-       !is_depth_stencil_format(rb->surface->format)) {
-      /* clear whole depth buffer w/out masking */
-      GLuint clearValue = depth_value(rb->surface->format, ctx->Depth.Clear);
-      ctx->st->pipe->clear(ctx->st->pipe, rb->surface, clearValue);
+   if (check_clear_depth_with_quad(ctx, rb)) {
+      /* scissoring or we have a combined depth/stencil buffer */
+      clear_with_quad(ctx, GL_FALSE, GL_TRUE, GL_FALSE);
    }
    else {
-      /* masking or scissoring or combined z/stencil buffer */
-      clear_with_quad(ctx,
-                      ctx->DrawBuffer->_Xmin,
-                      ctx->DrawBuffer->_Xmin,
-                      ctx->DrawBuffer->_Xmax,
-                      ctx->DrawBuffer->_Ymax,
-                      GL_FALSE, GL_TRUE, GL_FALSE);
+      struct st_renderbuffer *strb = st_renderbuffer(rb);
+
+      /* simple clear of whole buffer */
+      uint clearValue = util_pack_z(strb->surface->format, ctx->Depth.Clear);
+      ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
    }
 }
 
@@ -360,23 +443,25 @@ clear_depth_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
 static void
 clear_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
 {
-   const GLuint stencilMax = (1 << rb->StencilBits) - 1;
-   GLboolean maskStencil = ctx->Stencil.WriteMask[0] != stencilMax;
-
-   if (!maskStencil && !ctx->Scissor.Enabled &&
-       !is_depth_stencil_format(rb->surface->format)) {
-      /* clear whole stencil buffer w/out masking */
-      GLuint clearValue = ctx->Stencil.Clear;
-      ctx->st->pipe->clear(ctx->st->pipe, rb->surface, clearValue);
+   if (check_clear_stencil_with_quad(ctx, rb)) {
+      /* masking or scissoring or combined depth/stencil buffer */
+      clear_with_quad(ctx, GL_FALSE, GL_FALSE, GL_TRUE);
    }
    else {
-      /* masking or scissoring */
-      clear_with_quad(ctx,
-                      ctx->DrawBuffer->_Xmin,
-                      ctx->DrawBuffer->_Xmin,
-                      ctx->DrawBuffer->_Xmax,
-                      ctx->DrawBuffer->_Ymax,
-                      GL_FALSE, GL_FALSE, GL_TRUE);
+      struct st_renderbuffer *strb = st_renderbuffer(rb);
+
+      /* simple clear of whole buffer */
+      GLuint clearValue = ctx->Stencil.Clear;
+
+      switch (strb->surface->format) {
+      case PIPE_FORMAT_S8Z24_UNORM:
+         clearValue <<= 24;
+         break;
+      default:
+         ; /* no-op, stencil value is in least significant bits */
+      }  
+
+      ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
    }
 }
 
@@ -384,38 +469,29 @@ clear_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
 static void
 clear_depth_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
 {
-   const GLuint stencilMax = 1 << rb->StencilBits;
-   GLboolean maskStencil = ctx->Stencil.WriteMask[0] != stencilMax;
 
-   assert(is_depth_stencil_format(rb->surface->format));
+   if (check_clear_depth_stencil_with_quad(ctx, rb)) {
+      /* masking or scissoring */
+      clear_with_quad(ctx, GL_FALSE, GL_TRUE, GL_TRUE);
+   }
+   else {
+      struct st_renderbuffer *strb = st_renderbuffer(rb);
 
-   if (!maskStencil && !ctx->Scissor.Enabled) {
       /* clear whole buffer w/out masking */
-      GLuint clearValue = depth_value(rb->surface->format, ctx->Depth.Clear);
+      GLuint clearValue = util_pack_z(strb->surface->format, ctx->Depth.Clear);
 
-      switch (rb->surface->format) {
-      case PIPE_FORMAT_S8_Z24:
+      switch (strb->surface->format) {
+      case PIPE_FORMAT_S8Z24_UNORM:
          clearValue |= ctx->Stencil.Clear << 24;
          break;
-#if 0
-      case PIPE_FORMAT_Z24_S8:
-         clearValue = (clearValue << 8) | clearVal;
+      case PIPE_FORMAT_Z24S8_UNORM:
+         clearValue |= ctx->Stencil.Clear;
          break;
-#endif
       default:
          assert(0);
       }  
 
-      ctx->st->pipe->clear(ctx->st->pipe, rb->surface, clearValue);
-   }
-   else {
-      /* masking or scissoring */
-      clear_with_quad(ctx,
-                      ctx->DrawBuffer->_Xmin,
-                      ctx->DrawBuffer->_Xmin,
-                      ctx->DrawBuffer->_Xmax,
-                      ctx->DrawBuffer->_Ymax,
-                      GL_FALSE, GL_TRUE, GL_TRUE);
+      ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
    }
 }
 
@@ -435,6 +511,7 @@ static void st_clear(GLcontext *ctx, GLbitfield mask)
       = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
    struct gl_renderbuffer *stencilRb
       = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
+   GLbitfield cmask = mask & BUFFER_BITS_COLOR;
 
    /* This makes sure the softpipe has the latest scissor, etc values */
    st_validate_state( st );
@@ -447,19 +524,23 @@ static void st_clear(GLcontext *ctx, GLbitfield mask)
     * color/depth/stencil individually...
     */
 
-   if (mask & BUFFER_BITS_COLOR) {
+   if (cmask) {
       GLuint b;
-      for (b = 0; b < BUFFER_COUNT; b++) {
-         if (BUFFER_BITS_COLOR & mask & (1 << b)) {
-            clear_color_buffer(ctx,
-                               ctx->DrawBuffer->Attachment[b].Renderbuffer);
+      for (b = 0; cmask; b++) {
+         if (cmask & (1 << b)) {
+            struct gl_renderbuffer *rb
+               = ctx->DrawBuffer->Attachment[b].Renderbuffer;
+            assert(rb);
+            clear_color_buffer(ctx, rb);
+            cmask &= ~(1 << b); /* turn off bit */
          }
+         assert(b < BUFFER_COUNT);
       }
    }
 
    if (mask & BUFFER_BIT_ACCUM) {
-      clear_accum_buffer(ctx,
-                     ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
+      st_clear_accum_buffer(ctx,
+                       ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
    }
 
    if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {