gallium: remove unused winsys pointers in pipe_screen and pipe_context
[mesa.git] / src / gallium / drivers / i915 / i915_state.c
index f412626955df02bb7edc4e4da88808c66c1d4d80..f7ebfcdb88b90dcbb24a915708a145d841c0d59f 100644 (file)
@@ -177,7 +177,9 @@ static void i915_bind_blend_state(struct pipe_context *pipe,
                                   void *blend)
 {
    struct i915_context *i915 = i915_context(pipe);
-   draw_flush(i915->draw);
+
+   if (i915->blend == blend)
+      return;
 
    i915->blend = (struct i915_blend_state*)blend;
 
@@ -194,7 +196,9 @@ static void i915_set_blend_color( struct pipe_context *pipe,
                                   const struct pipe_blend_color *blend_color )
 {
    struct i915_context *i915 = i915_context(pipe);
-   draw_flush(i915->draw);
+
+   if (!blend_color)
+      return;
 
    i915->blend_color = *blend_color;
 
@@ -205,7 +209,6 @@ static void i915_set_stencil_ref( struct pipe_context *pipe,
                                   const struct pipe_stencil_ref *stencil_ref )
 {
    struct i915_context *i915 = i915_context(pipe);
-   draw_flush(i915->draw);
 
    i915->stencil_ref = *stencil_ref;
 
@@ -223,12 +226,12 @@ i915_create_sampler_state(struct pipe_context *pipe,
    unsigned minFilt, magFilt;
    unsigned mipFilt;
 
-   cso->templ = sampler;
+   cso->templ = *sampler;
 
    mipFilt = translate_mip_filter(sampler->min_mip_filter);
    minFilt = translate_img_filter( sampler->min_img_filter );
    magFilt = translate_img_filter( sampler->mag_img_filter );
-   
+
    if (sampler->max_anisotropy > 1)
       minFilt = magFilt = FILTER_ANISOTROPIC;
 
@@ -244,7 +247,7 @@ i915_create_sampler_state(struct pipe_context *pipe,
 
    /* Shadow:
     */
-   if (sampler->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) 
+   if (sampler->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE)
    {
       cso->state[0] |= (SS2_SHADOW_ENABLE |
                         i915_translate_shadow_compare_func(sampler->compare_func));
@@ -279,10 +282,10 @@ i915_create_sampler_state(struct pipe_context *pipe,
    }
 
    {
-      ubyte r = float_to_ubyte(sampler->border_color[0]);
-      ubyte g = float_to_ubyte(sampler->border_color[1]);
-      ubyte b = float_to_ubyte(sampler->border_color[2]);
-      ubyte a = float_to_ubyte(sampler->border_color[3]);
+      ubyte r = float_to_ubyte(sampler->border_color.f[0]);
+      ubyte g = float_to_ubyte(sampler->border_color.f[1]);
+      ubyte b = float_to_ubyte(sampler->border_color.f[2]);
+      ubyte a = float_to_ubyte(sampler->border_color.f[3]);
       cso->state[2] = I915PACKCOLOR8888(r, g, b, a);
    }
    return cso;
@@ -299,7 +302,36 @@ static void i915_fixup_bind_sampler_states(struct pipe_context *pipe,
    i915->saved_bind_sampler_states(pipe, num, sampler);
 }
 
-static void i915_bind_sampler_states(struct pipe_context *pipe,
+static void
+i915_bind_vertex_sampler_states(struct pipe_context *pipe,
+                                unsigned num_samplers,
+                                void **samplers)
+{
+   struct i915_context *i915 = i915_context(pipe);
+   unsigned i;
+
+   assert(num_samplers <= PIPE_MAX_VERTEX_SAMPLERS);
+
+   /* Check for no-op */
+   if (num_samplers == i915->num_vertex_samplers &&
+       !memcmp(i915->vertex_samplers, samplers, num_samplers * sizeof(void *)))
+      return;
+
+   for (i = 0; i < num_samplers; ++i)
+      i915->vertex_samplers[i] = samplers[i];
+   for (i = num_samplers; i < PIPE_MAX_VERTEX_SAMPLERS; ++i)
+      i915->vertex_samplers[i] = NULL;
+
+   i915->num_vertex_samplers = num_samplers;
+
+   draw_set_samplers(i915->draw,
+                     i915->vertex_samplers,
+                     i915->num_vertex_samplers);
+}
+
+
+
+static void i915_bind_fragment_sampler_states(struct pipe_context *pipe,
                                      unsigned num, void **sampler)
 {
    struct i915_context *i915 = i915_context(pipe);
@@ -310,8 +342,6 @@ static void i915_bind_sampler_states(struct pipe_context *pipe,
        !memcmp(i915->sampler, sampler, num * sizeof(void *)))
       return;
 
-   draw_flush(i915->draw);
-
    for (i = 0; i < num; ++i)
       i915->sampler[i] = sampler[i];
    for (i = num; i < PIPE_MAX_SAMPLERS; ++i)
@@ -329,6 +359,76 @@ static void i915_delete_sampler_state(struct pipe_context *pipe,
 }
 
 
+/**
+ * Called before drawing VBO to map vertex samplers and hand them to draw
+ */
+void
+i915_prepare_vertex_sampling(struct i915_context *i915)
+{
+   struct i915_winsys *iws = i915->iws;
+   unsigned i,j;
+   uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
+   uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
+   const void* data[PIPE_MAX_TEXTURE_LEVELS];
+   unsigned num = i915->num_vertex_sampler_views;
+   struct pipe_sampler_view **views = i915->vertex_sampler_views;
+
+   assert(num <= PIPE_MAX_VERTEX_SAMPLERS);
+   if (!num)
+      return;
+
+   for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
+      struct pipe_sampler_view *view = i < num ? views[i] : NULL;
+
+      if (view) {
+         struct pipe_resource *tex = view->texture;
+         struct i915_texture *i915_tex = i915_texture(tex);
+         ubyte *addr;
+
+         /* We're referencing the texture's internal data, so save a
+          * reference to it.
+          */
+         pipe_resource_reference(&i915->mapped_vs_tex[i], tex);
+
+        i915->mapped_vs_tex_buffer[i] = i915_tex->buffer;
+         addr = iws->buffer_map(iws,
+                                i915_tex->buffer,
+                                FALSE /* read only */);
+
+         /* Setup array of mipmap level pointers */
+        /* FIXME: handle 3D textures? */
+         for (j = view->u.tex.first_level; j <= tex->last_level; j++) {
+            unsigned offset = i915_texture_offset(i915_tex, j , 0 /* FIXME depth */);
+            data[j] = addr + offset;
+            row_stride[j] = i915_tex->stride;
+            img_stride[j] = 0; /* FIXME */;
+         }
+
+         draw_set_mapped_texture(i915->draw,
+                                 i,
+                                 tex->width0, tex->height0, tex->depth0,
+                                 view->u.tex.first_level, tex->last_level,
+                                 row_stride, img_stride, data);
+      } else
+         i915->mapped_vs_tex[i] = NULL;
+   }
+}
+
+void
+i915_cleanup_vertex_sampling(struct i915_context *i915)
+{
+   struct i915_winsys *iws = i915->iws;
+   unsigned i;
+   for (i = 0; i < Elements(i915->mapped_vs_tex); i++) {
+      if (i915->mapped_vs_tex_buffer[i]) { 
+         iws->buffer_unmap(iws, i915->mapped_vs_tex_buffer[i]);
+         pipe_resource_reference(&i915->mapped_vs_tex[i], NULL);
+      }
+   }
+}
+
+
+
 /** XXX move someday?  Or consolidate all these simple state setters
  * into one file.
  */
@@ -426,7 +526,9 @@ static void i915_bind_depth_stencil_state(struct pipe_context *pipe,
                                           void *depth_stencil)
 {
    struct i915_context *i915 = i915_context(pipe);
-   draw_flush(i915->draw);
+
+   if (i915->depth_stencil == depth_stencil)
+      return;
 
    i915->depth_stencil = (const struct i915_depth_stencil_state *)depth_stencil;
 
@@ -444,7 +546,6 @@ static void i915_set_scissor_state( struct pipe_context *pipe,
                                  const struct pipe_scissor_state *scissor )
 {
    struct i915_context *i915 = i915_context(pipe);
-   draw_flush(i915->draw);
 
    memcpy( &i915->scissor, scissor, sizeof(*scissor) );
    i915->dirty |= I915_NEW_SCISSOR;
@@ -452,7 +553,7 @@ static void i915_set_scissor_state( struct pipe_context *pipe,
 
 
 static void i915_set_polygon_stipple( struct pipe_context *pipe,
-                                   const struct pipe_poly_stipple *stipple )
+                                      const struct pipe_poly_stipple *stipple )
 {
 }
 
@@ -482,7 +583,9 @@ static void
 i915_fixup_bind_fs_state(struct pipe_context *pipe, void *shader)
 {
    struct i915_context *i915 = i915_context(pipe);
-   draw_flush(i915->draw);
+
+   if (i915->saved_fs == shader)
+      return;
 
    i915->saved_fs = shader;
 
@@ -493,7 +596,9 @@ static void
 i915_bind_fs_state(struct pipe_context *pipe, void *shader)
 {
    struct i915_context *i915 = i915_context(pipe);
-   draw_flush(i915->draw);
+
+   if (i915->fs == shader)
+      return;
 
    i915->fs = (struct i915_fragment_shader*) shader;
 
@@ -507,6 +612,11 @@ void i915_delete_fs_state(struct pipe_context *pipe, void *shader)
 {
    struct i915_fragment_shader *ifs = (struct i915_fragment_shader *) shader;
 
+   if (ifs->decl) {
+      FREE(ifs->decl);
+      ifs->decl = NULL;
+   }
+
    if (ifs->program) {
       FREE(ifs->program);
       ifs->program = NULL;
@@ -514,6 +624,7 @@ void i915_delete_fs_state(struct pipe_context *pipe, void *shader)
       ifs->state.tokens = NULL;
    }
    ifs->program_len = 0;
+   ifs->decl_len = 0;
 
    FREE(ifs);
 }
@@ -533,6 +644,9 @@ static void i915_bind_vs_state(struct pipe_context *pipe, void *shader)
 {
    struct i915_context *i915 = i915_context(pipe);
 
+   if (i915->saved_vs == shader)
+      return;
+
    i915->saved_vs = shader;
 
    /* just pass-through to draw module */
@@ -587,12 +701,6 @@ static void i915_set_constant_buffer(struct pipe_context *pipe,
       diff = i915->current.num_user_constants[shader] != 0;
    }
 
-   /*
-    * flush before updateing the state.
-    */
-   if (diff && shader == PIPE_SHADER_FRAGMENT)
-      draw_flush(i915->draw);
-
    pipe_resource_reference(&i915->constants[shader], buf);
    i915->current.num_user_constants[shader] = new_num;
 
@@ -636,9 +744,6 @@ static void i915_set_fragment_sampler_views(struct pipe_context *pipe,
        !memcmp(i915->fragment_sampler_views, views, num * sizeof(struct pipe_sampler_view *)))
       return;
 
-   /* Fixes wrong texture in texobj with VBUF */
-   draw_flush(i915->draw);
-
    for (i = 0; i < num; i++)
       pipe_sampler_view_reference(&i915->fragment_sampler_views[i],
                                   views[i]);
@@ -652,6 +757,35 @@ static void i915_set_fragment_sampler_views(struct pipe_context *pipe,
    i915->dirty |= I915_NEW_SAMPLER_VIEW;
 }
 
+static void
+i915_set_vertex_sampler_views(struct pipe_context *pipe,
+                              unsigned num,
+                              struct pipe_sampler_view **views)
+{
+   struct i915_context *i915 = i915_context(pipe);
+   uint i;
+
+   assert(num <= PIPE_MAX_VERTEX_SAMPLERS);
+
+   /* Check for no-op */
+   if (num == i915->num_vertex_sampler_views &&
+       !memcmp(i915->vertex_sampler_views, views, num * sizeof(struct pipe_sampler_view *))) {
+      return;
+   }
+
+   for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
+      struct pipe_sampler_view *view = i < num ? views[i] : NULL;
+
+      pipe_sampler_view_reference(&i915->vertex_sampler_views[i], view);
+   }
+
+   i915->num_vertex_sampler_views = num;
+
+   draw_set_sampler_views(i915->draw,
+                          i915->vertex_sampler_views,
+                          i915->num_vertex_sampler_views);
+}
+
 
 static struct pipe_sampler_view *
 i915_create_sampler_view(struct pipe_context *pipe,
@@ -687,8 +821,6 @@ static void i915_set_framebuffer_state(struct pipe_context *pipe,
    struct i915_context *i915 = i915_context(pipe);
    int i;
 
-   draw_flush(i915->draw);
-
    i915->framebuffer.width = fb->width;
    i915->framebuffer.height = fb->height;
    i915->framebuffer.nr_cbufs = fb->nr_cbufs;
@@ -707,7 +839,6 @@ static void i915_set_clip_state( struct pipe_context *pipe,
                             const struct pipe_clip_state *clip )
 {
    struct i915_context *i915 = i915_context(pipe);
-   draw_flush(i915->draw);
 
    i915->saved_clip = *clip;
 
@@ -808,6 +939,9 @@ static void i915_bind_rasterizer_state( struct pipe_context *pipe,
 {
    struct i915_context *i915 = i915_context(pipe);
 
+   if (i915->rasterizer == raster)
+      return;
+
    i915->rasterizer = (struct i915_rasterizer_state *)raster;
 
    /* pass-through to draw module */
@@ -875,6 +1009,9 @@ i915_bind_vertex_elements_state(struct pipe_context *pipe,
    struct i915_context *i915 = i915_context(pipe);
    struct i915_velems_state *i915_velems = (struct i915_velems_state *) velems;
 
+   if (i915->saved_velems == velems)
+      return;
+
    i915->saved_velems = velems;
 
    /* pass-through to draw module */
@@ -918,7 +1055,8 @@ i915_init_state_functions( struct i915_context *i915 )
    i915->base.delete_blend_state = i915_delete_blend_state;
 
    i915->base.create_sampler_state = i915_create_sampler_state;
-   i915->base.bind_fragment_sampler_states = i915_bind_sampler_states;
+   i915->base.bind_fragment_sampler_states = i915_bind_fragment_sampler_states;
+   i915->base.bind_vertex_sampler_states = i915_bind_vertex_sampler_states;
    i915->base.delete_sampler_state = i915_delete_sampler_state;
 
    i915->base.create_depth_stencil_alpha_state = i915_create_depth_stencil_state;
@@ -948,6 +1086,7 @@ i915_init_state_functions( struct i915_context *i915 )
    i915->base.set_polygon_stipple = i915_set_polygon_stipple;
    i915->base.set_scissor_state = i915_set_scissor_state;
    i915->base.set_fragment_sampler_views = i915_set_fragment_sampler_views;
+   i915->base.set_vertex_sampler_views = i915_set_vertex_sampler_views;
    i915->base.create_sampler_view = i915_create_sampler_view;
    i915->base.sampler_view_destroy = i915_sampler_view_destroy;
    i915->base.set_viewport_state = i915_set_viewport_state;