Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /**********************************************************
  2.  * Copyright 2008-2009 VMware, Inc.  All rights reserved.
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person
  5.  * obtaining a copy of this software and associated documentation
  6.  * files (the "Software"), to deal in the Software without
  7.  * restriction, including without limitation the rights to use, copy,
  8.  * modify, merge, publish, distribute, sublicense, and/or sell copies
  9.  * of the Software, and to permit persons to whom the Software is
  10.  * furnished to do so, subject to the following conditions:
  11.  *
  12.  * The above copyright notice and this permission notice shall be
  13.  * included in all copies or substantial portions of the Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16.  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18.  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19.  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20.  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22.  * SOFTWARE.
  23.  *
  24.  **********************************************************/
  25.  
  26. #include "svga_cmd.h"
  27.  
  28. #include "pipe/p_state.h"
  29. #include "pipe/p_defines.h"
  30. #include "util/u_inlines.h"
  31. #include "os/os_thread.h"
  32. #include "util/u_format.h"
  33. #include "util/u_math.h"
  34. #include "util/u_memory.h"
  35. #include "util/u_string.h"
  36.  
  37. #include "svga_format.h"
  38. #include "svga_screen.h"
  39. #include "svga_context.h"
  40. #include "svga_resource_texture.h"
  41. #include "svga_sampler_view.h"
  42. #include "svga_debug.h"
  43. #include "svga_surface.h"
  44.  
  45.  
  46. void
  47. svga_debug_describe_sampler_view(char *buf, const struct svga_sampler_view *sv)
  48. {
  49.    char res[128];
  50.    debug_describe_resource(res, sv->texture);
  51.    util_sprintf(buf, "svga_sampler_view<%s,[%u,%u]>", res, sv->min_lod, sv->max_lod);
  52. }
  53.  
  54. struct svga_sampler_view *
  55. svga_get_tex_sampler_view(struct pipe_context *pipe,
  56.                           struct pipe_resource *pt,
  57.                           unsigned min_lod, unsigned max_lod)
  58. {
  59.    struct svga_context *svga = svga_context(pipe);
  60.    struct svga_screen *ss = svga_screen(pipe->screen);
  61.    struct svga_texture *tex = svga_texture(pt);
  62.    struct svga_sampler_view *sv = NULL;
  63.    SVGA3dSurfaceFlags flags = SVGA3D_SURFACE_HINT_TEXTURE;
  64.    SVGA3dSurfaceFormat format = svga_translate_format(ss, pt->format, PIPE_BIND_SAMPLER_VIEW);
  65.    boolean view = TRUE;
  66.  
  67.    assert(pt);
  68.    assert(min_lod <= max_lod);
  69.    assert(max_lod <= pt->last_level);
  70.  
  71.  
  72.    /* Is a view needed */
  73.    {
  74.       /*
  75.        * Can't control max lod. For first level views and when we only
  76.        * look at one level we disable mip filtering to achive the same
  77.        * results as a view.
  78.        */
  79.       if (min_lod == 0 && max_lod >= pt->last_level)
  80.          view = FALSE;
  81.  
  82.       if (ss->debug.no_sampler_view)
  83.          view = FALSE;
  84.  
  85.       if (ss->debug.force_sampler_view)
  86.          view = TRUE;
  87.    }
  88.  
  89.    /* First try the cache */
  90.    if (view) {
  91.       pipe_mutex_lock(ss->tex_mutex);
  92.       if (tex->cached_view &&
  93.           tex->cached_view->min_lod == min_lod &&
  94.           tex->cached_view->max_lod == max_lod) {
  95.          svga_sampler_view_reference(&sv, tex->cached_view);
  96.          pipe_mutex_unlock(ss->tex_mutex);
  97.          SVGA_DBG(DEBUG_VIEWS, "svga: Sampler view: reuse %p, %u %u, last %u\n",
  98.                               pt, min_lod, max_lod, pt->last_level);
  99.          svga_validate_sampler_view(svga_context(pipe), sv);
  100.          return sv;
  101.       }
  102.       pipe_mutex_unlock(ss->tex_mutex);
  103.    }
  104.  
  105.    sv = CALLOC_STRUCT(svga_sampler_view);
  106.    if (!sv)
  107.       return NULL;
  108.  
  109.    pipe_reference_init(&sv->reference, 1);
  110.  
  111.    /* Note: we're not refcounting the texture resource here to avoid
  112.     * a circular dependency.
  113.     */
  114.    sv->texture = pt;
  115.  
  116.    sv->min_lod = min_lod;
  117.    sv->max_lod = max_lod;
  118.  
  119.    /* No view needed just use the whole texture */
  120.    if (!view) {
  121.       SVGA_DBG(DEBUG_VIEWS,
  122.                "svga: Sampler view: no %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
  123.                pt, min_lod, max_lod,
  124.                max_lod - min_lod + 1,
  125.                pt->width0,
  126.                pt->height0,
  127.                pt->depth0,
  128.                pt->last_level);
  129.       sv->key.cachable = 0;
  130.       sv->handle = tex->handle;
  131.       debug_reference(&sv->reference,
  132.                       (debug_reference_descriptor)svga_debug_describe_sampler_view, 0);
  133.       return sv;
  134.    }
  135.  
  136.    SVGA_DBG(DEBUG_VIEWS,
  137.             "svga: Sampler view: yes %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
  138.             pt, min_lod, max_lod,
  139.             max_lod - min_lod + 1,
  140.             pt->width0,
  141.             pt->height0,
  142.             pt->depth0,
  143.             pt->last_level);
  144.  
  145.    sv->age = tex->age;
  146.    sv->handle = svga_texture_view_surface(svga, tex, flags, format,
  147.                                           min_lod,
  148.                                           max_lod - min_lod + 1,
  149.                                           -1, -1,
  150.                                           &sv->key);
  151.  
  152.    if (!sv->handle) {
  153.       assert(0);
  154.       sv->key.cachable = 0;
  155.       sv->handle = tex->handle;
  156.       debug_reference(&sv->reference,
  157.                       (debug_reference_descriptor)svga_debug_describe_sampler_view, 0);
  158.       return sv;
  159.    }
  160.  
  161.    pipe_mutex_lock(ss->tex_mutex);
  162.    svga_sampler_view_reference(&tex->cached_view, sv);
  163.    pipe_mutex_unlock(ss->tex_mutex);
  164.  
  165.    debug_reference(&sv->reference,
  166.                    (debug_reference_descriptor)svga_debug_describe_sampler_view, 0);
  167.  
  168.    return sv;
  169. }
  170.  
  171. void
  172. svga_validate_sampler_view(struct svga_context *svga, struct svga_sampler_view *v)
  173. {
  174.    struct svga_texture *tex = svga_texture(v->texture);
  175.    unsigned numFaces;
  176.    unsigned age = 0;
  177.    int i;
  178.    unsigned k;
  179.  
  180.    assert(svga);
  181.  
  182.    if (v->handle == tex->handle)
  183.       return;
  184.  
  185.    age = tex->age;
  186.  
  187.    if(tex->b.b.target == PIPE_TEXTURE_CUBE)
  188.       numFaces = 6;
  189.    else
  190.       numFaces = 1;
  191.  
  192.    for (i = v->min_lod; i <= v->max_lod; i++) {
  193.       for (k = 0; k < numFaces; k++) {
  194.          assert(i < Elements(tex->view_age));
  195.          if (v->age < tex->view_age[i])
  196.             svga_texture_copy_handle(svga,
  197.                                      tex->handle, 0, 0, 0, i, k,
  198.                                      v->handle, 0, 0, 0, i - v->min_lod, k,
  199.                                      u_minify(tex->b.b.width0, i),
  200.                                      u_minify(tex->b.b.height0, i),
  201.                                      u_minify(tex->b.b.depth0, i));
  202.       }
  203.    }
  204.  
  205.    v->age = age;
  206. }
  207.  
  208. void
  209. svga_destroy_sampler_view_priv(struct svga_sampler_view *v)
  210. {
  211.    struct svga_texture *tex = svga_texture(v->texture);
  212.  
  213.    if(v->handle != tex->handle) {
  214.       struct svga_screen *ss = svga_screen(v->texture->screen);
  215.       SVGA_DBG(DEBUG_DMA, "unref sid %p (sampler view)\n", v->handle);
  216.       svga_screen_surface_destroy(ss, &v->key, &v->handle);
  217.    }
  218.  
  219.    /* Note: we're not refcounting the texture resource here to avoid
  220.     * a circular dependency.
  221.     */
  222.    v->texture = NULL;
  223.  
  224.    FREE(v);
  225. }
  226.