st/mesa: implement EXT_transform_feedback and ARB_transform_feedback2
[mesa.git] / src / mesa / state_tracker / st_cb_clear.c
index 246ab2e9579db4e01af7b5199757f81cfc8f6685..23700eeb7ed1e3852df53998f98b51a174fb3d7e 100644 (file)
   */
 
 #include "main/glheader.h"
+#include "main/accum.h"
 #include "main/formats.h"
 #include "main/macros.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_format.h"
 #include "st_program.h"
 
 #include "pipe/p_context.h"
 void
 st_init_clear(struct st_context *st)
 {
-   struct pipe_context *pipe = st->pipe;
    struct pipe_screen *pscreen = st->pipe->screen;
 
    memset(&st->clear, 0, sizeof(st->clear));
 
    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);
-
-   /* 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);
-   }
 }
 
 
@@ -106,6 +93,42 @@ st_destroy_clear(struct st_context *st)
 }
 
 
+/**
+ * Helper function to set the fragment shaders.
+ */
+static INLINE void
+set_fragment_shader(struct st_context *st)
+{
+   if (!st->clear.fs)
+      st->clear.fs = util_make_fragment_passthrough_shader(st->pipe);
+
+   cso_set_fragment_shader_handle(st->cso_context, st->clear.fs);
+}
+
+
+/**
+ * Helper function to set the vertex shader.
+ */
+static INLINE void
+set_vertex_shader(struct st_context *st)
+{
+   /* vertex shader - still required to provide the linkage between
+    * fragment shader input semantics and vertex_element/buffers.
+    */
+   if (!st->clear.vs)
+   {
+      const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
+                                      TGSI_SEMANTIC_COLOR };
+      const uint semantic_indexes[] = { 0, 0 };
+      st->clear.vs = util_make_vertex_passthrough_shader(st->pipe, 2,
+                                                         semantic_names,
+                                                         semantic_indexes);
+   }
+
+   cso_set_vertex_shader_handle(st->cso_context, st->clear.vs);
+}
+
+
 /**
  * Draw a screen-aligned quadrilateral.
  * Coords are clip coords with y=0=bottom.
@@ -113,7 +136,7 @@ st_destroy_clear(struct st_context *st)
 static void
 draw_quad(struct st_context *st,
           float x0, float y0, float x1, float y1, GLfloat z,
-          const GLfloat color[4])
+          const union pipe_color_union *color)
 {
    struct pipe_context *pipe = st->pipe;
 
@@ -138,9 +161,15 @@ draw_quad(struct st_context *st,
    if (!st->clear.vbuf) {
       st->clear.vbuf = pipe_buffer_create(pipe->screen,
                                           PIPE_BIND_VERTEX_BUFFER,
+                                          PIPE_USAGE_STREAM,
                                           max_slots * sizeof(st->clear.vertices));
    }
 
+   if (!st->clear.vbuf) {
+      /* ran out of memory */
+      return;
+   }
+
    /* positions */
    st->clear.vertices[0][0][0] = x0;
    st->clear.vertices[0][0][1] = y0;
@@ -158,10 +187,10 @@ draw_quad(struct st_context *st,
    for (i = 0; i < 4; i++) {
       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];
+      st->clear.vertices[i][1][0] = color->f[0];
+      st->clear.vertices[i][1][1] = color->f[1];
+      st->clear.vertices[i][1][2] = color->f[2];
+      st->clear.vertices[i][1][3] = color->f[3];
    }
 
    /* put vertex data into vbuf */
@@ -172,7 +201,8 @@ draw_quad(struct st_context *st,
                                            st->clear.vertices);
 
    /* draw */
-   util_draw_vertex_buffer(pipe, 
+   util_draw_vertex_buffer(pipe,
+                           st->cso_context,
                            st->clear.vbuf, 
                            st->clear.vbuf_slot * sizeof(st->clear.vertices),
                            PIPE_PRIM_TRIANGLE_FAN,
@@ -191,7 +221,7 @@ draw_quad(struct st_context *st,
  * 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 = st_context(ctx);
@@ -202,6 +232,7 @@ clear_with_quad(GLcontext *ctx,
    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;
+   union pipe_color_union clearColor;
 
    /*
    printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__, 
@@ -219,8 +250,11 @@ clear_with_quad(GLcontext *ctx,
    cso_save_viewport(st->cso_context);
    cso_save_clip(st->cso_context);
    cso_save_fragment_shader(st->cso_context);
+   cso_save_stream_outputs(st->cso_context);
    cso_save_vertex_shader(st->cso_context);
+   cso_save_geometry_shader(st->cso_context);
    cso_save_vertex_elements(st->cso_context);
+   cso_save_vertex_buffers(st->cso_context);
 
    /* blend state: RGBA masking */
    {
@@ -273,6 +307,7 @@ clear_with_quad(GLcontext *ctx,
    }
 
    cso_set_vertex_elements(st->cso_context, 2, st->velems_util_draw);
+   cso_set_stream_outputs(st->cso_context, 0, NULL, 0);
 
    cso_set_rasterizer(st->cso_context, &st->clear.raster);
 
@@ -292,12 +327,18 @@ clear_with_quad(GLcontext *ctx,
    }
 
    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);
+   set_fragment_shader(st);
+   set_vertex_shader(st);
+   cso_set_geometry_shader_handle(st->cso_context, NULL);
+
+   if (ctx->DrawBuffer->_ColorDrawBuffers[0]) {
+      st_translate_color(ctx->Color.ClearColor.f,
+                               ctx->DrawBuffer->_ColorDrawBuffers[0]->_BaseFormat,
+                               clearColor.f);
+   }
 
-   /* draw quad matching scissor rect (XXX verify coord round-off) */
-   draw_quad(st, x0, y0, x1, y1,
-             (GLfloat) ctx->Depth.Clear, ctx->Color.ClearColor);
+   /* draw quad matching scissor rect */
+   draw_quad(st, x0, y0, x1, y1, (GLfloat) ctx->Depth.Clear, &clearColor);
 
    /* Restore pipe state */
    cso_restore_blend(st->cso_context);
@@ -308,7 +349,10 @@ clear_with_quad(GLcontext *ctx,
    cso_restore_clip(st->cso_context);
    cso_restore_fragment_shader(st->cso_context);
    cso_restore_vertex_shader(st->cso_context);
+   cso_restore_geometry_shader(st->cso_context);
    cso_restore_vertex_elements(st->cso_context);
+   cso_restore_vertex_buffers(st->cso_context);
+   cso_restore_stream_outputs(st->cso_context);
 }
 
 
@@ -316,7 +360,7 @@ 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 ||
@@ -340,7 +384,7 @@ check_clear_color_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
  * 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
@@ -348,7 +392,8 @@ check_clear_depth_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
 
    assert(rb->Format == MESA_FORMAT_S8 ||
           rb->Format == MESA_FORMAT_Z24_S8 ||
-          rb->Format == MESA_FORMAT_S8_Z24);
+          rb->Format == MESA_FORMAT_S8_Z24 ||
+          rb->Format == MESA_FORMAT_Z32_FLOAT_X24S8);
 
    if (ctx->Scissor.Enabled &&
        (ctx->Scissor.X != 0 ||
@@ -368,7 +413,7 @@ 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);
@@ -392,7 +437,7 @@ 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);
@@ -403,7 +448,8 @@ check_clear_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb,
 
    assert(rb->Format == MESA_FORMAT_S8 ||
           rb->Format == MESA_FORMAT_Z24_S8 ||
-          rb->Format == MESA_FORMAT_S8_Z24);
+          rb->Format == MESA_FORMAT_S8_Z24 ||
+          rb->Format == MESA_FORMAT_Z32_FLOAT_X24S8);
 
    if (maskStencil) 
       return GL_TRUE;
@@ -447,7 +493,7 @@ st_flush_clear(struct st_context *st)
  * Called via ctx->Driver.Clear()
  */
 static void
-st_Clear(GLcontext *ctx, GLbitfield mask)
+st_Clear(struct gl_context *ctx, GLbitfield mask)
 {
    static const GLbitfield BUFFER_BITS_DS
       = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
@@ -470,13 +516,9 @@ st_Clear(GLcontext *ctx, GLbitfield mask)
          if (mask & (1 << b)) {
             struct gl_renderbuffer *rb
                = ctx->DrawBuffer->Attachment[b].Renderbuffer;
-            struct st_renderbuffer *strb;
-
-            assert(rb);
+            struct st_renderbuffer *strb = st_renderbuffer(rb);
 
-            strb = st_renderbuffer(rb);
-
-            if (!strb->surface)
+            if (!strb || !strb->surface)
                continue;
 
             if (check_clear_color_with_quad( ctx, rb ))
@@ -541,18 +583,26 @@ st_Clear(GLcontext *ctx, GLbitfield mask)
        * required from the visual. Hence fix this up to avoid potential
        * read-modify-write in the driver.
        */
+      union pipe_color_union clearColor;
+
       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,
+
+      if (ctx->DrawBuffer->_ColorDrawBuffers[0]) {
+         st_translate_color(ctx->Color.ClearColor.f,
+                           ctx->DrawBuffer->_ColorDrawBuffers[0]->_BaseFormat,
+                           clearColor.f);
+      }
+
+      st->pipe->clear(st->pipe, clear_buffers, &clearColor,
                       ctx->Depth.Clear, ctx->Stencil.Clear);
    }
    if (mask & BUFFER_BIT_ACCUM)
-      st_clear_accum_buffer(ctx,
-                            ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
+      _mesa_clear_accum_buffer(ctx);
 }