svga: Set the rendered-to flag for dma transfers to surfaces
[mesa.git] / src / gallium / drivers / svga / svga_pipe_sampler.c
index 52c8a81af6d118a85f4ad1bf4f92465513661b51..f1e68dd726a3e6ba509e2428b749e300899d45b4 100644 (file)
@@ -183,8 +183,6 @@ define_sampler_state_object(struct svga_context *svga,
 
    COPY_4V(bcolor.value, ps->border_color.f);
 
-   ss->id = util_bitmask_add(svga->sampler_object_id_bm);
-
    assert(ps->min_lod <= ps->max_lod);
 
    if (ps->min_mip_filter == PIPE_TEX_MIPFILTER_NONE) {
@@ -196,24 +194,41 @@ define_sampler_state_object(struct svga_context *svga,
       max_lod = ps->max_lod;
    }
 
-   /* Loop in case command buffer is full and we need to flush and retry */
-   for (try = 0; try < 2; try++) {
-      enum pipe_error ret =
-         SVGA3D_vgpu10_DefineSamplerState(svga->swc,
-                                          ss->id,
-                                          filter,
-                                          ss->addressu,
-                                          ss->addressv,
-                                          ss->addressw,
-                                          ss->lod_bias, /* float */
-                                          max_aniso,
-                                          compare_func,
-                                          bcolor,
-                                          min_lod,       /* float */
-                                          max_lod);      /* float */
-      if (ret == PIPE_OK)
-         return;
-      svga_context_flush(svga, NULL);
+   /* If shadow comparisons are enabled, create two sampler states: one
+    * with the given shadow compare mode, another with shadow comparison off.
+    * We need the later because in some cases, we have to do the shadow
+    * compare in the shader.  So, we don't want to do it twice.
+    */
+   STATIC_ASSERT(PIPE_TEX_COMPARE_NONE == 0);
+   STATIC_ASSERT(PIPE_TEX_COMPARE_R_TO_TEXTURE == 1);
+   ss->id[1] = SVGA3D_INVALID_ID;
+
+   unsigned i;
+   for (i = 0; i <= ss->compare_mode; i++) {
+      ss->id[i] = util_bitmask_add(svga->sampler_object_id_bm);
+
+      /* Loop in case command buffer is full and we need to flush and retry */
+      for (try = 0; try < 2; try++) {
+         enum pipe_error ret =
+            SVGA3D_vgpu10_DefineSamplerState(svga->swc,
+                                             ss->id[i],
+                                             filter,
+                                             ss->addressu,
+                                             ss->addressv,
+                                             ss->addressw,
+                                             ss->lod_bias, /* float */
+                                             max_aniso,
+                                             compare_func,
+                                             bcolor,
+                                             min_lod,       /* float */
+                                             max_lod);      /* float */
+         if (ret == PIPE_OK)
+            break;
+         svga_context_flush(svga, NULL);
+      }
+
+      /* turn off the shadow compare option for second iteration */
+      filter &= ~SVGA3D_FILTER_COMPARE;
    }
 }
 
@@ -272,11 +287,14 @@ svga_create_sampler_state(struct pipe_context *pipe,
       define_sampler_state_object(svga, cso, sampler);
    }
 
-   SVGA_DBG(DEBUG_VIEWS, "min %u, view(min %u, max %u) lod, mipfilter %s\n",
+   SVGA_DBG(DEBUG_SAMPLERS,
+            "New sampler: min %u, view(min %u, max %u) lod, mipfilter %s\n",
             cso->min_lod, cso->view_min_lod, cso->view_max_lod,
             cso->mipfilter == SVGA3D_TEX_FILTER_NONE ? "SVGA3D_TEX_FILTER_NONE" : "SOMETHING");
 
    svga->hud.num_sampler_objects++;
+   SVGA_STATS_COUNT_INC(svga_screen(svga->pipe.screen)->sws,
+                        SVGA_STATS_COUNT_SAMPLER);
 
    return cso;
 }
@@ -284,7 +302,7 @@ svga_create_sampler_state(struct pipe_context *pipe,
 
 static void
 svga_bind_sampler_states(struct pipe_context *pipe,
-                         unsigned shader,
+                         enum pipe_shader_type shader,
                          unsigned start,
                          unsigned num,
                          void **samplers)
@@ -329,16 +347,21 @@ svga_delete_sampler_state(struct pipe_context *pipe, void *sampler)
    struct svga_context *svga = svga_context(pipe);
 
    if (svga_have_vgpu10(svga)) {
-      enum pipe_error ret;
+      unsigned i;
+      for (i = 0; i < 2; i++) {
+         enum pipe_error ret;
 
-      svga_hwtnl_flush_retry(svga);
+         if (ss->id[i] != SVGA3D_INVALID_ID) {
+            svga_hwtnl_flush_retry(svga);
 
-      ret = SVGA3D_vgpu10_DestroySamplerState(svga->swc, ss->id);
-      if (ret != PIPE_OK) {
-         svga_context_flush(svga, NULL);
-         ret = SVGA3D_vgpu10_DestroySamplerState(svga->swc, ss->id);
+            ret = SVGA3D_vgpu10_DestroySamplerState(svga->swc, ss->id[i]);
+            if (ret != PIPE_OK) {
+               svga_context_flush(svga, NULL);
+               ret = SVGA3D_vgpu10_DestroySamplerState(svga->swc, ss->id[i]);
+            }
+            util_bitmask_clear(svga->sampler_object_id_bm, ss->id[i]);
+         }
       }
-      util_bitmask_clear(svga->sampler_object_id_bm, ss->id);
    }
 
    FREE(sampler);
@@ -367,6 +390,8 @@ svga_create_sampler_view(struct pipe_context *pipe,
    sv->id = SVGA3D_INVALID_ID;
 
    svga->hud.num_samplerview_objects++;
+   SVGA_STATS_COUNT_INC(svga_screen(svga->pipe.screen)->sws,
+                        SVGA_STATS_COUNT_SAMPLERVIEW);
 
    return &sv->base;
 }
@@ -380,28 +405,18 @@ svga_sampler_view_destroy(struct pipe_context *pipe,
    struct svga_pipe_sampler_view *sv = svga_pipe_sampler_view(view);
 
    if (svga_have_vgpu10(svga) && sv->id != SVGA3D_INVALID_ID) {
-      if (view->context != pipe) {
-         /* The SVGA3D device will generate an error (and on Linux, cause
-          * us to abort) if we try to destroy a shader resource view from
-          * a context other than the one it was created with.  Skip the
-          * SVGA3D_vgpu10_DestroyShaderResourceView() and leak the sampler
-          * view for now.  This should only sometimes happen when a shared
-          * texture is deleted.
-          */
-         _debug_printf("context mismatch in %s\n", __func__);
-      }
-      else {
-         enum pipe_error ret;
+      enum pipe_error ret;
+
+      assert(view->context == pipe);
 
-         svga_hwtnl_flush_retry(svga); /* XXX is this needed? */
+      svga_hwtnl_flush_retry(svga);
 
+      ret = SVGA3D_vgpu10_DestroyShaderResourceView(svga->swc, sv->id);
+      if (ret != PIPE_OK) {
+         svga_context_flush(svga, NULL);
          ret = SVGA3D_vgpu10_DestroyShaderResourceView(svga->swc, sv->id);
-         if (ret != PIPE_OK) {
-            svga_context_flush(svga, NULL);
-            ret = SVGA3D_vgpu10_DestroyShaderResourceView(svga->swc, sv->id);
-         }
-         util_bitmask_clear(svga->sampler_view_id_bm, sv->id);
       }
+      util_bitmask_clear(svga->sampler_view_id_bm, sv->id);
    }
 
    pipe_resource_reference(&sv->base.texture, NULL);
@@ -413,7 +428,7 @@ svga_sampler_view_destroy(struct pipe_context *pipe,
 
 static void
 svga_set_sampler_views(struct pipe_context *pipe,
-                       unsigned shader,
+                       enum pipe_shader_type shader,
                        unsigned start,
                        unsigned num,
                        struct pipe_sampler_view **views)
@@ -425,19 +440,31 @@ svga_set_sampler_views(struct pipe_context *pipe,
    boolean any_change = FALSE;
 
    assert(shader < PIPE_SHADER_TYPES);
-   assert(start + num <= Elements(svga->curr.sampler_views[shader]));
+   assert(start + num <= ARRAY_SIZE(svga->curr.sampler_views[shader]));
 
    /* Pre-VGPU10 only supports FS textures */
    if (!svga_have_vgpu10(svga) && shader != PIPE_SHADER_FRAGMENT)
       return;
 
+   SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_SETSAMPLERVIEWS);
+
+   /* This bit of code works around a quirk in the CSO module.
+    * If start=num=0 it means all sampler views should be released.
+    * Note that the CSO module treats sampler views for fragment shaders
+    * differently than other shader types.
+    */
+   if (start == 0 && num == 0 && svga->curr.num_sampler_views[shader] > 0) {
+      for (i = 0; i < svga->curr.num_sampler_views[shader]; i++) {
+         pipe_sampler_view_reference(&svga->curr.sampler_views[shader][i],
+                                     NULL);
+      }
+      any_change = TRUE;
+   }
+
    for (i = 0; i < num; i++) {
+      enum pipe_texture_target target;
+
       if (svga->curr.sampler_views[shader][start + i] != views[i]) {
-         /* Note: we're using pipe_sampler_view_release() here to work around
-          * a possible crash when the old view belongs to another context that
-          * was already destroyed.
-          */
-         pipe_sampler_view_release(pipe, &svga->curr.sampler_views[shader][start + i]);
          pipe_sampler_view_reference(&svga->curr.sampler_views[shader][start + i],
                                      views[i]);
          any_change = TRUE;
@@ -449,12 +476,24 @@ svga_set_sampler_views(struct pipe_context *pipe,
       if (util_format_is_srgb(views[i]->format))
          flag_srgb |= 1 << (start + i);
 
-      if (views[i]->texture->target == PIPE_TEXTURE_1D)
+      target = views[i]->target;
+      if (target == PIPE_TEXTURE_1D) {
          flag_1d |= 1 << (start + i);
+      } else if (target == PIPE_TEXTURE_RECT) {
+         /* If the size of the bound texture changes, we need to emit new
+          * const buffer values.
+          */
+         svga->dirty |= SVGA_NEW_TEXTURE_CONSTS;
+      } else if (target == PIPE_BUFFER) {
+         /* If the size of the bound buffer changes, we need to emit new
+          * const buffer values.
+          */
+         svga->dirty |= SVGA_NEW_TEXTURE_CONSTS;
+      }
    }
 
    if (!any_change) {
-      return;
+      goto done;
    }
 
    /* find highest non-null sampler_views[] entry */
@@ -475,30 +514,46 @@ svga_set_sampler_views(struct pipe_context *pipe,
    }
 
    /* Check if any of the sampler view resources collide with the framebuffer
-    * color buffers or depth stencil resource. If so, enable the NEW_FRAME_BUFFER
+    * color buffers or depth stencil resource. If so, set the NEW_FRAME_BUFFER
     * dirty bit so that emit_framebuffer can be invoked to create backed view
     * for the conflicted surface view.
     */
-   for (i = 0; i < svga->curr.framebuffer.nr_cbufs; i++) {
-      if (svga->curr.framebuffer.cbufs[i]) {
-         struct svga_surface *s = svga_surface(svga->curr.framebuffer.cbufs[i]);
-         if (svga_check_sampler_view_resource_collision(svga, s->handle, shader)) {
-            svga->dirty |= SVGA_NEW_FRAME_BUFFER;
-            break;
-         }
-      }
+   if (svga_check_sampler_framebuffer_resource_collision(svga, shader)) {
+      svga->dirty |= SVGA_NEW_FRAME_BUFFER;
    }
 
-   if (svga->curr.framebuffer.zsbuf) {
-      struct svga_surface *s = svga_surface(svga->curr.framebuffer.zsbuf);
-      if (s) {
-         if (svga_check_sampler_view_resource_collision(svga, s->handle, shader)) {
-            svga->dirty |= SVGA_NEW_FRAME_BUFFER;
-         }
+done:
+   SVGA_STATS_TIME_POP(svga_sws(svga));
+}
+
+/**
+ * Clean up sampler, sampler view state at context destruction time
+ */
+void
+svga_cleanup_sampler_state(struct svga_context *svga)
+{
+   enum pipe_shader_type shader;
+
+   for (shader = 0; shader <= PIPE_SHADER_GEOMETRY; shader++) {
+      unsigned i;
+
+      for (i = 0; i < svga->state.hw_draw.num_sampler_views[shader]; i++) {
+         pipe_sampler_view_reference(&svga->state.hw_draw.sampler_views[shader][i],
+                                     NULL);
       }
    }
-}
+   
+   /* free polygon stipple state */
+   if (svga->polygon_stipple.sampler) {
+      svga->pipe.delete_sampler_state(&svga->pipe, svga->polygon_stipple.sampler);
+   }
 
+   if (svga->polygon_stipple.sampler_view) {
+      svga->pipe.sampler_view_destroy(&svga->pipe,
+                                      &svga->polygon_stipple.sampler_view->base);
+   }
+   pipe_resource_reference(&svga->polygon_stipple.texture, NULL);
+}
 
 void
 svga_init_sampler_functions( struct svga_context *svga )