u_blitter: rename blitter->base, add a way to get a pipe context from blitter
[mesa.git] / src / gallium / auxiliary / util / u_blitter.h
index 3da5a6ca52583b0ade3f3b06c9e4ae6897ac840c..ffcf5c79843b7909d90f8d9572acd8503644ed44 100644 (file)
@@ -27,6 +27,7 @@
 #ifndef U_BLITTER_H
 #define U_BLITTER_H
 
+#include "util/u_inlines.h"
 #include "util/u_memory.h"
 
 #include "pipe/p_state.h"
@@ -41,18 +42,27 @@ struct pipe_context;
 struct blitter_context
 {
    /* Private members, really. */
+   struct pipe_context *pipe; /**< pipe context */
+
    void *saved_blend_state;   /**< blend state */
    void *saved_dsa_state;     /**< depth stencil alpha state */
+   void *saved_velem_state;   /**< vertex elements state */
    void *saved_rs_state;      /**< rasterizer state */
    void *saved_fs, *saved_vs; /**< fragment shader, vertex shader */
 
    struct pipe_framebuffer_state saved_fb_state;  /**< framebuffer state */
+   struct pipe_stencil_ref saved_stencil_ref;     /**< stencil ref */
+   struct pipe_viewport_state saved_viewport;
+   struct pipe_clip_state saved_clip;
 
    int saved_num_sampler_states;
-   void *saved_sampler_states[32];
+   void *saved_sampler_states[PIPE_MAX_SAMPLERS];
+
+   int saved_num_sampler_views;
+   struct pipe_sampler_view *saved_sampler_views[PIPE_MAX_SAMPLERS];
 
-   int saved_num_textures;
-   struct pipe_texture *saved_textures[32]; /* is 32 enough? */
+   int saved_num_vertex_buffers;
+   struct pipe_vertex_buffer saved_vertex_buffers[PIPE_MAX_ATTRIBS];
 };
 
 /**
@@ -65,6 +75,15 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe);
  */
 void util_blitter_destroy(struct blitter_context *blitter);
 
+/**
+ * Return the pipe context associated with a blitter context.
+ */
+static INLINE
+struct pipe_context *util_blitter_get_pipe(struct blitter_context *blitter)
+{
+   return blitter->pipe;
+}
+
 /*
  * These CSOs must be saved before any of the following functions is called:
  * - blend state
@@ -105,52 +124,48 @@ void util_blitter_clear(struct blitter_context *blitter,
  * - fragment sampler states
  * - fragment sampler textures
  */
-void util_blitter_copy(struct blitter_context *blitter,
-                       struct pipe_surface *dst,
-                       unsigned dstx, unsigned dsty,
-                       struct pipe_surface *src,
-                       unsigned srcx, unsigned srcy,
-                       unsigned width, unsigned height,
-                       boolean ignore_stencil);
+void util_blitter_copy_region(struct blitter_context *blitter,
+                              struct pipe_resource *dst,
+                              struct pipe_subresource subdst,
+                              unsigned dstx, unsigned dsty, unsigned dstz,
+                              struct pipe_resource *src,
+                              struct pipe_subresource subsrc,
+                              unsigned srcx, unsigned srcy, unsigned srcz,
+                              unsigned width, unsigned height,
+                              boolean ignore_stencil);
 
 /**
- * Fill a region of a surface with a constant value.
- *
- * If the surface cannot be rendered to or it's a depth-stencil format,
- * a software fallback path is taken.
+ * Clear a region of a (color) surface to a constant value.
  *
  * These states must be saved in the blitter in addition to the state objects
  * already required to be saved:
  * - framebuffer state
  */
-void util_blitter_fill(struct blitter_context *blitter,
-                       struct pipe_surface *dst,
-                       unsigned dstx, unsigned dsty,
-                       unsigned width, unsigned height,
-                       unsigned value);
+void util_blitter_clear_render_target(struct blitter_context *blitter,
+                                      struct pipe_surface *dst,
+                                      const float *rgba,
+                                      unsigned dstx, unsigned dsty,
+                                      unsigned width, unsigned height);
 
 /**
- * Copy all pixels from one surface to another.
+ * Clear a region of a depth-stencil surface, both stencil and depth
+ * or only one of them if this is a combined depth-stencil surface.
  *
- * The rules are the same as in util_blitter_copy with the addition that
- * surfaces must have the same size.
+ * These states must be saved in the blitter in addition to the state objects
+ * already required to be saved:
+ * - framebuffer state
  */
-static INLINE
-void util_blitter_copy_surface(struct blitter_context *blitter,
-                               struct pipe_surface *dst,
-                               struct pipe_surface *src,
-                               boolean ignore_stencil)
-{
-   assert(dst->width == src->width && dst->height == src->height);
-
-   util_blitter_copy(blitter, dst, 0, 0, src, 0, 0, src->width, src->height,
-                     ignore_stencil);
-}
-
+void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
+                                      struct pipe_surface *dst,
+                                      unsigned clear_flags,
+                                      double depth,
+                                      unsigned stencil,
+                                      unsigned dstx, unsigned dsty,
+                                      unsigned width, unsigned height);
 
 /* The functions below should be used to save currently bound constant state
  * objects inside a driver. The objects are automatically restored at the end
- * of the util_blitter_{clear, fill, copy, copy_surface} functions and then
+ * of the util_blitter_{clear, copy_region, fill_region} functions and then
  * forgotten.
  *
  * CSOs not listed here are not affected by util_blitter. */
@@ -169,6 +184,20 @@ void util_blitter_save_depth_stencil_alpha(struct blitter_context *blitter,
    blitter->saved_dsa_state = state;
 }
 
+static INLINE
+void util_blitter_save_vertex_elements(struct blitter_context *blitter,
+                                       void *state)
+{
+   blitter->saved_velem_state = state;
+}
+
+static INLINE
+void util_blitter_save_stencil_ref(struct blitter_context *blitter,
+                                   const struct pipe_stencil_ref *state)
+{
+   blitter->saved_stencil_ref = *state;
+}
+
 static INLINE
 void util_blitter_save_rasterizer(struct blitter_context *blitter,
                                   void *state)
@@ -190,11 +219,59 @@ void util_blitter_save_vertex_shader(struct blitter_context *blitter,
    blitter->saved_vs = vs;
 }
 
+/* XXX This should probably be moved elsewhere. */
+static INLINE
+void util_assign_framebuffer_state(struct pipe_framebuffer_state *dst,
+                                   const struct pipe_framebuffer_state *src)
+{
+   unsigned i;
+
+   if (src) {
+      /* Reference all surfaces. */
+      for (i = 0; i < src->nr_cbufs; i++) {
+         pipe_surface_reference(&dst->cbufs[i], src->cbufs[i]);
+      }
+      for (; i < dst->nr_cbufs; i++) {
+         pipe_surface_reference(&dst->cbufs[i], NULL);
+      }
+
+      pipe_surface_reference(&dst->zsbuf, src->zsbuf);
+
+      dst->nr_cbufs = src->nr_cbufs;
+      dst->width = src->width;
+      dst->height = src->height;
+   } else {
+      /* Set all surfaces to NULL. */
+      for (i = 0; i < dst->nr_cbufs; i++) {
+         pipe_surface_reference(&dst->cbufs[i], NULL);
+      }
+
+      pipe_surface_reference(&dst->zsbuf, NULL);
+
+      dst->nr_cbufs = 0;
+   }
+}
+
 static INLINE
 void util_blitter_save_framebuffer(struct blitter_context *blitter,
-                                   struct pipe_framebuffer_state *state)
+                                   const struct pipe_framebuffer_state *state)
+{
+   blitter->saved_fb_state.nr_cbufs = 0; /* It's ~0 now, meaning it's unsaved. */
+   util_assign_framebuffer_state(&blitter->saved_fb_state, state);
+}
+
+static INLINE
+void util_blitter_save_viewport(struct blitter_context *blitter,
+                                struct pipe_viewport_state *state)
+{
+   blitter->saved_viewport = *state;
+}
+
+static INLINE
+void util_blitter_save_clip(struct blitter_context *blitter,
+                            struct pipe_clip_state *state)
 {
-   blitter->saved_fb_state = *state;
+   blitter->saved_clip = *state;
 }
 
 static INLINE
@@ -210,17 +287,40 @@ void util_blitter_save_fragment_sampler_states(
           num_sampler_states * sizeof(void *));
 }
 
-static INLINE
-void util_blitter_save_fragment_sampler_textures(
-                  struct blitter_context *blitter,
-                  int num_textures,
-                  struct pipe_texture **textures)
+static INLINE void
+util_blitter_save_fragment_sampler_views(struct blitter_context *blitter,
+                                         int num_views,
+                                         struct pipe_sampler_view **views)
 {
-   assert(num_textures <= Elements(blitter->saved_textures));
+   unsigned i;
+   assert(num_views <= Elements(blitter->saved_sampler_views));
+
+   blitter->saved_num_sampler_views = num_views;
+   for (i = 0; i < num_views; i++)
+      pipe_sampler_view_reference(&blitter->saved_sampler_views[i],
+                                  views[i]);
+}
+
+static INLINE void
+util_blitter_save_vertex_buffers(struct blitter_context *blitter,
+                                         int num_vertex_buffers,
+                                         struct pipe_vertex_buffer *vertex_buffers)
+{
+   unsigned i;
+   assert(num_vertex_buffers <= Elements(blitter->saved_vertex_buffers));
+
+   blitter->saved_num_vertex_buffers = num_vertex_buffers;
+
+   for (i = 0; i < num_vertex_buffers; i++) {
+      if (vertex_buffers[i].buffer) {
+         pipe_resource_reference(&blitter->saved_vertex_buffers[i].buffer,
+                                 vertex_buffers[i].buffer);
+      }
+   }
 
-   blitter->saved_num_textures = num_textures;
-   memcpy(blitter->saved_textures, textures,
-          num_textures * sizeof(struct pipe_texture *));
+   memcpy(blitter->saved_vertex_buffers,
+          vertex_buffers,
+          num_vertex_buffers * sizeof(struct pipe_vertex_buffer));
 }
 
 #ifdef __cplusplus