Merge branch 'gallium-wgl-rework' into gallium-0.2
[mesa.git] / src / mesa / state_tracker / st_cb_clear.c
index ec8d3e102215c731219bcf24785f5d702f3d7b4f..668c3f9ebf4aa6a85da09bcbc00333ffcd38a20d 100644 (file)
@@ -34,8 +34,8 @@
 #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"
@@ -45,9 +45,9 @@
 #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/p_winsys.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"
 
 
-/* XXX for testing draw module vertex passthrough: */
-/* XXX this hack is broken now */
-#define TEST_DRAW_PASSTHROUGH 0
+void
+st_init_clear(struct st_context *st)
+{
+   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;
+
+   /* fragment shader state: color pass-through program */
+   st->clear.fs =
+      util_make_fragment_passthrough_shader(pipe, &st->clear.frag_shader);
+
+   /* 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);
+   }
+}
 
 
 void
@@ -65,16 +97,26 @@ st_destroy_clear(struct st_context *st)
 {
    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) {
-      pipe->delete_fs_state(pipe, st->clear.fs);
+      cso_delete_fragment_shader(st->cso_context, st->clear.fs);
       st->clear.fs = NULL;
    }
    if (st->clear.vs) {
-      pipe->delete_vs_state(pipe, st->clear.vs);
+      cso_delete_vertex_shader(st->cso_context, st->clear.vs);
       st->clear.vs = NULL;
    }
    if (st->clear.vbuf) {
-      pipe->winsys->buffer_destroy(pipe->winsys, st->clear.vbuf);
+      pipe_buffer_reference(pipe->screen, &st->clear.vbuf, NULL);
       st->clear.vbuf = NULL;
    }
 }
@@ -106,13 +148,18 @@ draw_quad(GLcontext *ctx,
 {
    struct st_context *st = ctx->st;
    struct pipe_context *pipe = st->pipe;
+   const GLuint max_slots = 1024 / sizeof(st->clear.vertices);
    GLuint i;
    void *buf;
 
+   if (st->clear.vbuf_slot >= max_slots) {
+      pipe_buffer_reference(pipe->screen, &st->clear.vbuf, NULL);
+      st->clear.vbuf_slot = 0;
+   }
+
    if (!st->clear.vbuf) {
-      st->clear.vbuf = pipe->winsys->buffer_create(pipe->winsys, 32,
-                                                   PIPE_BUFFER_USAGE_VERTEX,
-                                                   sizeof(st->clear.vertices));
+      st->clear.vbuf = pipe_buffer_create(pipe->screen, 32, PIPE_BUFFER_USAGE_VERTEX,
+                                          max_slots * sizeof(st->clear.vertices));
    }
 
    /* positions */
@@ -139,16 +186,24 @@ draw_quad(GLcontext *ctx,
    }
 
    /* put vertex data into vbuf */
-   buf = pipe->winsys->buffer_map(pipe->winsys, st->clear.vbuf,
-                                  PIPE_BUFFER_USAGE_CPU_WRITE);
-   memcpy(buf, st->clear.vertices, sizeof(st->clear.vertices));
-   pipe->winsys->buffer_unmap(pipe->winsys, st->clear.vbuf);
+   buf = pipe_buffer_map(pipe->screen, st->clear.vbuf, PIPE_BUFFER_USAGE_CPU_WRITE);
+
+   memcpy((char *)buf + st->clear.vbuf_slot * sizeof(st->clear.vertices), 
+          st->clear.vertices, 
+          sizeof(st->clear.vertices));
+
+   pipe_buffer_unmap(pipe->screen, st->clear.vbuf);
 
    /* draw */
-   util_draw_vertex_buffer(pipe, st->clear.vbuf,
+   util_draw_vertex_buffer(pipe, 
+                           st->clear.vbuf, 
+                           st->clear.vbuf_slot * sizeof(st->clear.vertices),
                            PIPE_PRIM_TRIANGLE_FAN,
                            4,  /* verts */
                            2); /* attribs/vert */
+
+   /* Increment slot */
+   st->clear.vbuf_slot++;
 }
 
 
@@ -163,18 +218,17 @@ clear_with_quad(GLcontext *ctx,
                 GLboolean color, GLboolean depth, GLboolean stencil)
 {
    struct st_context *st = ctx->st;
-   struct pipe_context *pipe = st->pipe;
-   const GLfloat x0 = ctx->DrawBuffer->_Xmin;
-   const GLfloat x1 = ctx->DrawBuffer->_Xmax;
+   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 = ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymax;
-      y1 = ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymin;
+      y0 = (GLfloat) (ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymax);
+      y1 = (GLfloat) (ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymin);
    }
    else {
-      y0 = ctx->DrawBuffer->_Ymin;
-      y1 = ctx->DrawBuffer->_Ymax;
+      y0 = (GLfloat) ctx->DrawBuffer->_Ymin;
+      y1 = (GLfloat) ctx->DrawBuffer->_Ymax;
    }
 
    /*
@@ -190,6 +244,8 @@ clear_with_quad(GLcontext *ctx,
    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 */
    {
@@ -231,74 +287,29 @@ clear_with_quad(GLcontext *ctx,
          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;
+         depth_stencil.stencil[0].valuemask = 0xff;
+         depth_stencil.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
       }
 
       cso_set_depth_stencil_alpha(st->cso_context, &depth_stencil);
    }
 
-   /* rasterizer state: bypass clipping */
-   {
-      struct pipe_rasterizer_state raster;
-      memset(&raster, 0, sizeof(raster));
-      raster.bypass_clipping = 1;
-#if TEST_DRAW_PASSTHROUGH
-      raster.bypass_vs = 1;
-#endif
-      cso_set_rasterizer(st->cso_context, &raster);
-   }
-
-   /* fragment shader state: color pass-through program */
-   if (!st->clear.fs) {
-      st->clear.fs = util_make_fragment_passthrough_shader(pipe, &st->clear.frag_shader);
-   }
-   pipe->bind_fs_state(pipe, st->clear.fs);
-
-
-#if !TEST_DRAW_PASSTHROUGH
-   /* vertex shader state: color/position pass-through */
-   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(pipe, 2,
-                                                         semantic_names,
-                                                         semantic_indexes,
-                                                         &st->clear.vert_shader);
-   }
-   pipe->bind_vs_state(pipe, st->clear.vs);
-#endif
+   cso_set_rasterizer(st->cso_context, &st->clear.raster);
+   cso_set_viewport(st->cso_context, &st->clear.viewport);
 
-#if !TEST_DRAW_PASSTHROUGH
-   /* viewport state: viewport matching window dims */
-   {
-      const float width = ctx->DrawBuffer->Width;
-      const float height = ctx->DrawBuffer->Height;
-      struct pipe_viewport_state vp;
-      vp.scale[0] =  0.5 * width;
-      vp.scale[1] = -0.5 * height;
-      vp.scale[2] = 1.0;
-      vp.scale[3] = 1.0;
-      vp.translate[0] = 0.5 * width;
-      vp.translate[1] = 0.5 * height;
-      vp.translate[2] = 0.0;
-      vp.translate[3] = 0.0;
-      cso_set_viewport(st->cso_context, &vp);
-   }
-#endif
+   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 */
    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);
-   /* these don't go through cso yet */
-   pipe->bind_fs_state(pipe, st->fp->driver_shader);
-   pipe->bind_vs_state(pipe, st->vp->driver_shader);
+   cso_restore_fragment_shader(st->cso_context);
+   cso_restore_vertex_shader(st->cso_context);
 }
 
 
@@ -410,15 +421,22 @@ check_clear_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
 static void
 clear_color_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
 {
+   struct st_renderbuffer *strb = st_renderbuffer(rb);
+
+   if (!strb->surface)
+      return;
+
    if (check_clear_color_with_quad( ctx, rb )) {
       /* masking or scissoring */
       clear_with_quad(ctx, GL_TRUE, GL_FALSE, GL_FALSE);
    }
    else {
       /* clear whole buffer w/out masking */
-      struct st_renderbuffer *strb = st_renderbuffer(rb);
       uint clearValue;
-      util_pack_color(ctx->Color.ClearColor, strb->surface->format, &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);
    }
 }
@@ -427,13 +445,16 @@ clear_color_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
 static void
 clear_depth_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
 {
+   struct st_renderbuffer *strb = st_renderbuffer(rb);
+
+   if (!strb->surface)
+      return;
+
    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 {
-      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);
@@ -444,13 +465,16 @@ clear_depth_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
 static void
 clear_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
 {
+   struct st_renderbuffer *strb = st_renderbuffer(rb);
+
+   if (!strb->surface)
+      return;
+
    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 {
-      struct st_renderbuffer *strb = st_renderbuffer(rb);
-
       /* simple clear of whole buffer */
       GLuint clearValue = ctx->Stencil.Clear;
 
@@ -470,14 +494,16 @@ clear_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
 static void
 clear_depth_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
 {
+   struct st_renderbuffer *strb = st_renderbuffer(rb);
+
+   if (!strb->surface)
+      return;
 
    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);
-
       /* clear whole buffer w/out masking */
       GLuint clearValue = util_pack_z(strb->surface->format, ctx->Depth.Clear);
 
@@ -497,6 +523,16 @@ clear_depth_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
 }
 
 
+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->pipe->screen, &st->clear.vbuf, NULL);
+   st->clear.vbuf_slot = 0;
+}
+
 
 /**
  * Called via ctx->Driver.Clear()