Merge branch 'llvm-cliptest-viewport'
[mesa.git] / src / mesa / state_tracker / st_cb_clear.c
index 74ec85107f446c377440fd2ce12eebd66054b737..bd1dd78b23c8c54fe2ffda174188fd803f1201d7 100644 (file)
 #include "main/glheader.h"
 #include "main/formats.h"
 #include "main/macros.h"
-#include "shader/prog_instruction.h"
+#include "program/prog_instruction.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_program.h"
-#include "st_public.h"
-#include "st_inlines.h"
 
 #include "pipe/p_context.h"
-#include "util/u_inlines.h"
+#include "pipe/p_shader_tokens.h"
 #include "pipe/p_state.h"
 #include "pipe/p_defines.h"
 #include "util/u_format.h"
+#include "util/u_inlines.h"
 #include "util/u_simple_shaders.h"
 #include "util/u_draw_quad.h"
 
 #include "cso_cache/cso_context.h"
 
 
+/**
+ * Do per-context initialization for glClear.
+ */
 void
 st_init_clear(struct st_context *st)
 {
    struct pipe_context *pipe = st->pipe;
+   struct pipe_screen *pscreen = st->pipe->screen;
 
-   memset(&st->clear.raster, 0, sizeof(st->clear.raster));
-   st->clear.raster.gl_rasterization_rules = 1;
+   memset(&st->clear, 0, sizeof(st->clear));
 
-   /* rasterizer state: bypass vertex shader, clipping and viewport */
-   st->clear.raster.bypass_vs_clip_and_viewport = 1;
+   st->clear.raster.gl_rasterization_rules = 1;
+   st->clear.enable_ds_separate = pscreen->get_param(pscreen, PIPE_CAP_DEPTHSTENCIL_CLEAR_SEPARATE);
 
    /* fragment shader state: color pass-through program */
-   st->clear.fs =
-      util_make_fragment_passthrough_shader(pipe);
+   st->clear.fs = util_make_fragment_passthrough_shader(pipe);
 
    /* vertex shader state: color/position pass-through */
    {
@@ -84,6 +85,9 @@ st_init_clear(struct st_context *st)
 }
 
 
+/**
+ * Free per-context state for glClear.
+ */
 void
 st_destroy_clear(struct st_context *st)
 {
@@ -96,7 +100,7 @@ st_destroy_clear(struct st_context *st)
       st->clear.vs = NULL;
    }
    if (st->clear.vbuf) {
-      pipe_buffer_reference(&st->clear.vbuf, NULL);
+      pipe_resource_reference(&st->clear.vbuf, NULL);
       st->clear.vbuf = NULL;
    }
 }
@@ -104,16 +108,13 @@ st_destroy_clear(struct st_context *st)
 
 /**
  * Draw a screen-aligned quadrilateral.
- * Coords are window coords with y=0=bottom.  These will be passed
- * through unmodified to the rasterizer as we have set
- * rasterizer->bypass_vs_clip_and_viewport.
+ * Coords are clip coords with y=0=bottom.
  */
 static void
-draw_quad(GLcontext *ctx,
+draw_quad(struct st_context *st,
           float x0, float y0, float x1, float y1, GLfloat z,
           const GLfloat color[4])
 {
-   struct st_context *st = ctx->st;
    struct pipe_context *pipe = st->pipe;
 
    /* XXX: Need to improve buffer_write to allow NO_WAIT (as well as
@@ -130,12 +131,13 @@ draw_quad(GLcontext *ctx,
    GLuint i;
 
    if (st->clear.vbuf_slot >= max_slots) {
-      pipe_buffer_reference(&st->clear.vbuf, NULL);
+      pipe_resource_reference(&st->clear.vbuf, NULL);
       st->clear.vbuf_slot = 0;
    }
 
    if (!st->clear.vbuf) {
-      st->clear.vbuf = pipe_buffer_create(pipe->screen, 32, PIPE_BUFFER_USAGE_VERTEX,
+      st->clear.vbuf = pipe_buffer_create(pipe->screen,
+                                          PIPE_BIND_VERTEX_BUFFER,
                                           max_slots * sizeof(st->clear.vertices));
    }
 
@@ -163,8 +165,9 @@ draw_quad(GLcontext *ctx,
    }
 
    /* put vertex data into vbuf */
-   st_no_flush_pipe_buffer_write_nooverlap(st, st->clear.vbuf,
-                                           st->clear.vbuf_slot * sizeof(st->clear.vertices),
+   pipe_buffer_write_nooverlap(st->pipe, st->clear.vbuf,
+                                           st->clear.vbuf_slot
+                                             * sizeof(st->clear.vertices),
                                            sizeof(st->clear.vertices),
                                            st->clear.vertices);
 
@@ -188,22 +191,17 @@ draw_quad(GLcontext *ctx,
  * ctx->DrawBuffer->_X/Ymin/max fields.
  */
 static void
-clear_with_quad(GLcontext *ctx,
+clear_with_quad(struct gl_context *ctx,
                 GLboolean color, GLboolean depth, GLboolean stencil)
 {
-   struct st_context *st = ctx->st;
-   const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin;
-   const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax;
-   GLfloat y0, y1;
-
-   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);
-   }
-   else {
-      y0 = (GLfloat) ctx->DrawBuffer->_Ymin;
-      y1 = (GLfloat) ctx->DrawBuffer->_Ymax;
-   }
+   struct st_context *st = st_context(ctx);
+   const struct gl_framebuffer *fb = ctx->DrawBuffer;
+   const GLfloat fb_width = (GLfloat) fb->Width;
+   const GLfloat fb_height = (GLfloat) fb->Height;
+   const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin / fb_width * 2.0f - 1.0f;
+   const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax / fb_width * 2.0f - 1.0f;
+   const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin / fb_height * 2.0f - 1.0f;
+   const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax / fb_height * 2.0f - 1.0f;
 
    /*
    printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__, 
@@ -215,10 +213,14 @@ clear_with_quad(GLcontext *ctx,
    */
 
    cso_save_blend(st->cso_context);
+   cso_save_stencil_ref(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_clip(st->cso_context);
    cso_save_fragment_shader(st->cso_context);
    cso_save_vertex_shader(st->cso_context);
+   cso_save_vertex_elements(st->cso_context);
 
    /* blend state: RGBA masking */
    {
@@ -270,21 +272,43 @@ clear_with_quad(GLcontext *ctx,
       cso_set_depth_stencil_alpha(st->cso_context, &depth_stencil);
    }
 
+   cso_set_vertex_elements(st->cso_context, 2, st->velems_util_draw);
+
    cso_set_rasterizer(st->cso_context, &st->clear.raster);
 
+   /* viewport state: viewport matching window dims */
+   {
+      const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP);
+      struct pipe_viewport_state vp;
+      vp.scale[0] = 0.5f * fb_width;
+      vp.scale[1] = fb_height * (invert ? -0.5f : 0.5f);
+      vp.scale[2] = 1.0f;
+      vp.scale[3] = 1.0f;
+      vp.translate[0] = 0.5f * fb_width;
+      vp.translate[1] = 0.5f * fb_height;
+      vp.translate[2] = 0.0f;
+      vp.translate[3] = 0.0f;
+      cso_set_viewport(st->cso_context, &vp);
+   }
+
+   cso_set_clip(st->cso_context, &st->clear.clip);
    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, (GLfloat) ctx->Depth.Clear, ctx->Color.ClearColor);
+   draw_quad(st, x0, y0, x1, y1,
+             (GLfloat) ctx->Depth.Clear, ctx->Color.ClearColor);
 
    /* Restore pipe state */
    cso_restore_blend(st->cso_context);
+   cso_restore_stencil_ref(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_clip(st->cso_context);
    cso_restore_fragment_shader(st->cso_context);
    cso_restore_vertex_shader(st->cso_context);
-
+   cso_restore_vertex_elements(st->cso_context);
 }
 
 
@@ -292,27 +316,31 @@ clear_with_quad(GLcontext *ctx,
  * 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)
+check_clear_color_with_quad(struct gl_context *ctx, struct gl_renderbuffer *rb)
 {
    if (ctx->Scissor.Enabled &&
        (ctx->Scissor.X != 0 ||
         ctx->Scissor.Y != 0 ||
         ctx->Scissor.Width < rb->Width ||
         ctx->Scissor.Height < rb->Height))
-      return TRUE;
+      return GL_TRUE;
 
    if (!ctx->Color.ColorMask[0][0] ||
        !ctx->Color.ColorMask[0][1] ||
        !ctx->Color.ColorMask[0][2] ||
        !ctx->Color.ColorMask[0][3])
-      return TRUE;
+      return GL_TRUE;
 
-   return FALSE;
+   return GL_FALSE;
 }
 
 
+/**
+ * Determine if we need to clear the combiend depth/stencil buffer by
+ * drawing a quad.
+ */
 static INLINE GLboolean
-check_clear_depth_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
+check_clear_depth_stencil_with_quad(struct gl_context *ctx, struct gl_renderbuffer *rb)
 {
    const GLuint stencilMax = 0xff;
    GLboolean maskStencil
@@ -327,12 +355,12 @@ check_clear_depth_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
         ctx->Scissor.Y != 0 ||
         ctx->Scissor.Width < rb->Width ||
         ctx->Scissor.Height < rb->Height))
-      return TRUE;
+      return GL_TRUE;
 
    if (maskStencil)
-      return TRUE;
+      return GL_TRUE;
 
-   return FALSE;
+   return GL_FALSE;
 }
 
 
@@ -340,7 +368,8 @@ check_clear_depth_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
  * 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)
+check_clear_depth_with_quad(struct gl_context *ctx, struct gl_renderbuffer *rb,
+                            boolean ds_separate)
 {
    const struct st_renderbuffer *strb = st_renderbuffer(rb);
    const GLboolean isDS = util_format_is_depth_and_stencil(strb->surface->format);
@@ -350,13 +379,12 @@ check_clear_depth_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
         ctx->Scissor.Y != 0 ||
         ctx->Scissor.Width < rb->Width ||
         ctx->Scissor.Height < rb->Height))
-      return TRUE;
+      return GL_TRUE;
 
-   if (isDS && 
-       ctx->DrawBuffer->Visual.stencilBits > 0)
-      return TRUE;
+   if (!ds_separate && isDS && ctx->DrawBuffer->Visual.stencilBits > 0)
+      return GL_TRUE;
 
-   return FALSE;
+   return GL_FALSE;
 }
 
 
@@ -364,7 +392,8 @@ check_clear_depth_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
  * 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)
+check_clear_stencil_with_quad(struct gl_context *ctx, struct gl_renderbuffer *rb,
+                              boolean ds_separate)
 {
    const struct st_renderbuffer *strb = st_renderbuffer(rb);
    const GLboolean isDS = util_format_is_depth_and_stencil(strb->surface->format);
@@ -377,35 +406,38 @@ check_clear_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
           rb->Format == MESA_FORMAT_S8_Z24);
 
    if (maskStencil) 
-      return TRUE;
+      return GL_TRUE;
 
    if (ctx->Scissor.Enabled &&
        (ctx->Scissor.X != 0 ||
         ctx->Scissor.Y != 0 ||
         ctx->Scissor.Width < rb->Width ||
         ctx->Scissor.Height < rb->Height))
-      return TRUE;
+      return GL_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 && 
-       ctx->DrawBuffer->Visual.depthBits > 0)
-      return TRUE;
+   if (!ds_separate && isDS && ctx->DrawBuffer->Visual.depthBits > 0)
+      return GL_TRUE;
 
-   return FALSE;
+   return GL_FALSE;
 }
 
 
 
-void st_flush_clear( struct st_context *st )
+/**
+ * Called when we need to flush.
+ */
+void
+st_flush_clear(struct st_context *st)
 {
    /* Release vertex buffer to avoid synchronous rendering if we were
     * to map it in the next frame.
     */
-   pipe_buffer_reference(&st->clear.vbuf, NULL);
+   pipe_resource_reference(&st->clear.vbuf, NULL);
    st->clear.vbuf_slot = 0;
 }
  
@@ -413,20 +445,19 @@ void st_flush_clear( struct st_context *st )
 
 /**
  * Called via ctx->Driver.Clear()
- * XXX: doesn't pick up the differences between front/back/left/right
- * clears.  Need to sort that out...
  */
-static void st_clear(GLcontext *ctx, GLbitfield mask)
+static void
+st_Clear(struct gl_context *ctx, GLbitfield mask)
 {
    static const GLbitfield BUFFER_BITS_DS
       = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
-   struct st_context *st = ctx->st;
+   struct st_context *st = st_context(ctx);
    struct gl_renderbuffer *depthRb
       = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
    struct gl_renderbuffer *stencilRb
       = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
-   GLbitfield quad_buffers = 0;
-   GLbitfield clear_buffers = 0;
+   GLbitfield quad_buffers = 0x0;
+   GLbitfield clear_buffers = 0x0;
    GLuint i;
 
    /* This makes sure the pipe has the latest scissor, etc values */
@@ -469,24 +500,27 @@ static void st_clear(GLcontext *ctx, GLbitfield mask)
    }
    else {
       /* separate depth/stencil clears */
+      /* I don't think truly separate buffers are actually possible in gallium or hw? */
       if (mask & BUFFER_BIT_DEPTH) {
          struct st_renderbuffer *strb = st_renderbuffer(depthRb);
 
          if (strb->surface) {
-            if (check_clear_depth_with_quad(ctx, depthRb))
-               quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
+            if (check_clear_depth_with_quad(ctx, depthRb,
+                                            st->clear.enable_ds_separate))
+               quad_buffers |= PIPE_CLEAR_DEPTH;
             else
-               clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
+               clear_buffers |= PIPE_CLEAR_DEPTH;
          }
       }
       if (mask & BUFFER_BIT_STENCIL) {
          struct st_renderbuffer *strb = st_renderbuffer(stencilRb);
 
          if (strb->surface) {
-            if (check_clear_stencil_with_quad(ctx, stencilRb))
-               quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
+            if (check_clear_stencil_with_quad(ctx, stencilRb,
+                                              st->clear.enable_ds_separate))
+               quad_buffers |= PIPE_CLEAR_STENCIL;
             else
-               clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
+               clear_buffers |= PIPE_CLEAR_STENCIL;
          }
       }
    }
@@ -499,19 +533,31 @@ static void st_clear(GLcontext *ctx, GLbitfield mask)
       quad_buffers |= clear_buffers;
       clear_with_quad(ctx,
                       quad_buffers & PIPE_CLEAR_COLOR,
-                      mask & BUFFER_BIT_DEPTH,
-                      mask & BUFFER_BIT_STENCIL);
-   } else if (clear_buffers)
-      ctx->st->pipe->clear(ctx->st->pipe, clear_buffers, ctx->Color.ClearColor,
-                           ctx->Depth.Clear, ctx->Stencil.Clear);
-
+                      quad_buffers & PIPE_CLEAR_DEPTH,
+                      quad_buffers & PIPE_CLEAR_STENCIL);
+   } else if (clear_buffers) {
+      /* driver cannot know it can clear everything if the buffer
+       * is a combined depth/stencil buffer but this wasn't actually
+       * required from the visual. Hence fix this up to avoid potential
+       * read-modify-write in the driver.
+       */
+      if ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) &&
+          ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) &&
+          (depthRb == stencilRb) &&
+          (ctx->DrawBuffer->Visual.depthBits == 0 ||
+           ctx->DrawBuffer->Visual.stencilBits == 0))
+         clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
+      st->pipe->clear(st->pipe, clear_buffers, ctx->Color.ClearColor,
+                      ctx->Depth.Clear, ctx->Stencil.Clear);
+   }
    if (mask & BUFFER_BIT_ACCUM)
       st_clear_accum_buffer(ctx,
                             ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
 }
 
 
-void st_init_clear_functions(struct dd_function_table *functions)
+void
+st_init_clear_functions(struct dd_function_table *functions)
 {
-   functions->Clear = st_clear;
+   functions->Clear = st_Clear;
 }