Home | History | Annotate | Download | only in svga
      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 
     36 #include "svga_format.h"
     37 #include "svga_screen.h"
     38 #include "svga_context.h"
     39 #include "svga_resource_texture.h"
     40 #include "svga_surface.h"
     41 #include "svga_debug.h"
     42 
     43 
     44 void
     45 svga_texture_copy_handle(struct svga_context *svga,
     46                          struct svga_winsys_surface *src_handle,
     47                          unsigned src_x, unsigned src_y, unsigned src_z,
     48                          unsigned src_level, unsigned src_face,
     49                          struct svga_winsys_surface *dst_handle,
     50                          unsigned dst_x, unsigned dst_y, unsigned dst_z,
     51                          unsigned dst_level, unsigned dst_face,
     52                          unsigned width, unsigned height, unsigned depth)
     53 {
     54    struct svga_surface dst, src;
     55    enum pipe_error ret;
     56    SVGA3dCopyBox box, *boxes;
     57 
     58    assert(svga);
     59 
     60    src.handle = src_handle;
     61    src.real_level = src_level;
     62    src.real_face = src_face;
     63    src.real_zslice = 0;
     64 
     65    dst.handle = dst_handle;
     66    dst.real_level = dst_level;
     67    dst.real_face = dst_face;
     68    dst.real_zslice = 0;
     69 
     70    box.x = dst_x;
     71    box.y = dst_y;
     72    box.z = dst_z;
     73    box.w = width;
     74    box.h = height;
     75    box.d = depth;
     76    box.srcx = src_x;
     77    box.srcy = src_y;
     78    box.srcz = src_z;
     79 
     80 /*
     81    SVGA_DBG(DEBUG_VIEWS, "mipcopy src: %p %u (%ux%ux%u), dst: %p %u (%ux%ux%u)\n",
     82             src_handle, src_level, src_x, src_y, src_z,
     83             dst_handle, dst_level, dst_x, dst_y, dst_z);
     84 */
     85 
     86    ret = SVGA3D_BeginSurfaceCopy(svga->swc,
     87                                  &src.base,
     88                                  &dst.base,
     89                                  &boxes, 1);
     90    if (ret != PIPE_OK) {
     91       svga_context_flush(svga, NULL);
     92       ret = SVGA3D_BeginSurfaceCopy(svga->swc,
     93                                     &src.base,
     94                                     &dst.base,
     95                                     &boxes, 1);
     96       assert(ret == PIPE_OK);
     97    }
     98    *boxes = box;
     99    SVGA_FIFOCommitAll(svga->swc);
    100 }
    101 
    102 
    103 struct svga_winsys_surface *
    104 svga_texture_view_surface(struct svga_context *svga,
    105                           struct svga_texture *tex,
    106                           SVGA3dSurfaceFlags flags,
    107                           SVGA3dSurfaceFormat format,
    108                           unsigned start_mip,
    109                           unsigned num_mip,
    110                           int face_pick,
    111                           int zslice_pick,
    112                           struct svga_host_surface_cache_key *key) /* OUT */
    113 {
    114    struct svga_screen *ss = svga_screen(svga->pipe.screen);
    115    struct svga_winsys_surface *handle;
    116    uint32_t i, j;
    117    unsigned z_offset = 0;
    118 
    119    SVGA_DBG(DEBUG_PERF,
    120             "svga: Create surface view: face %d zslice %d mips %d..%d\n",
    121             face_pick, zslice_pick, start_mip, start_mip+num_mip-1);
    122 
    123    key->flags = flags;
    124    key->format = format;
    125    key->numMipLevels = num_mip;
    126    key->size.width = u_minify(tex->b.b.width0, start_mip);
    127    key->size.height = u_minify(tex->b.b.height0, start_mip);
    128    key->size.depth = zslice_pick < 0 ? u_minify(tex->b.b.depth0, start_mip) : 1;
    129    key->cachable = 1;
    130    assert(key->size.depth == 1);
    131 
    132    if (tex->b.b.target == PIPE_TEXTURE_CUBE && face_pick < 0) {
    133       key->flags |= SVGA3D_SURFACE_CUBEMAP;
    134       key->numFaces = 6;
    135    } else {
    136       key->numFaces = 1;
    137    }
    138 
    139    if (key->format == SVGA3D_FORMAT_INVALID) {
    140       key->cachable = 0;
    141       return NULL;
    142    }
    143 
    144    SVGA_DBG(DEBUG_DMA, "surface_create for texture view\n");
    145    handle = svga_screen_surface_create(ss, key);
    146    if (!handle) {
    147       key->cachable = 0;
    148       return NULL;
    149    }
    150 
    151    SVGA_DBG(DEBUG_DMA, " --> got sid %p (texture view)\n", handle);
    152 
    153    if (face_pick < 0)
    154       face_pick = 0;
    155 
    156    if (zslice_pick >= 0)
    157       z_offset = zslice_pick;
    158 
    159    for (i = 0; i < key->numMipLevels; i++) {
    160       for (j = 0; j < key->numFaces; j++) {
    161          if (tex->defined[j + face_pick][i + start_mip]) {
    162             unsigned depth = (zslice_pick < 0 ?
    163                               u_minify(tex->b.b.depth0, i + start_mip) :
    164                               1);
    165 
    166             svga_texture_copy_handle(svga,
    167                                      tex->handle,
    168                                      0, 0, z_offset,
    169                                      i + start_mip,
    170                                      j + face_pick,
    171                                      handle, 0, 0, 0, i, j,
    172                                      u_minify(tex->b.b.width0, i + start_mip),
    173                                      u_minify(tex->b.b.height0, i + start_mip),
    174                                      depth);
    175          }
    176       }
    177    }
    178 
    179    return handle;
    180 }
    181 
    182 
    183 static struct pipe_surface *
    184 svga_create_surface(struct pipe_context *pipe,
    185                     struct pipe_resource *pt,
    186                     const struct pipe_surface *surf_tmpl)
    187 {
    188    struct svga_context *svga = svga_context(pipe);
    189    struct svga_texture *tex = svga_texture(pt);
    190    struct pipe_screen *screen = pipe->screen;
    191    struct svga_screen *ss = svga_screen(screen);
    192    struct svga_surface *s;
    193    unsigned face, zslice;
    194    /* XXX surfaces should only be used for rendering purposes nowadays */
    195    boolean render = (surf_tmpl->usage & (PIPE_BIND_RENDER_TARGET |
    196                                          PIPE_BIND_DEPTH_STENCIL)) ? TRUE : FALSE;
    197    boolean view = FALSE;
    198    SVGA3dSurfaceFlags flags;
    199    SVGA3dSurfaceFormat format;
    200 
    201    assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
    202 
    203    s = CALLOC_STRUCT(svga_surface);
    204    if (!s)
    205       return NULL;
    206 
    207    if (pt->target == PIPE_TEXTURE_CUBE) {
    208       face = surf_tmpl->u.tex.first_layer;
    209       zslice = 0;
    210    }
    211    else {
    212       face = 0;
    213       zslice = surf_tmpl->u.tex.first_layer;
    214    }
    215 
    216    pipe_reference_init(&s->base.reference, 1);
    217    pipe_resource_reference(&s->base.texture, pt);
    218    s->base.context = pipe;
    219    s->base.format = surf_tmpl->format;
    220    s->base.width = u_minify(pt->width0, surf_tmpl->u.tex.level);
    221    s->base.height = u_minify(pt->height0, surf_tmpl->u.tex.level);
    222    s->base.usage = surf_tmpl->usage;
    223    s->base.u.tex.level = surf_tmpl->u.tex.level;
    224    s->base.u.tex.first_layer = surf_tmpl->u.tex.first_layer;
    225    s->base.u.tex.last_layer = surf_tmpl->u.tex.last_layer;
    226 
    227    if (!render) {
    228       flags = SVGA3D_SURFACE_HINT_TEXTURE;
    229    } else {
    230       if (surf_tmpl->usage & PIPE_BIND_RENDER_TARGET) {
    231          flags = SVGA3D_SURFACE_HINT_RENDERTARGET;
    232       }
    233       if (surf_tmpl->usage & PIPE_BIND_DEPTH_STENCIL) {
    234          flags = SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
    235       }
    236    }
    237 
    238    format = svga_translate_format(ss, surf_tmpl->format, surf_tmpl->usage);
    239    assert(format != SVGA3D_FORMAT_INVALID);
    240 
    241    if (svga_screen(screen)->debug.force_surface_view)
    242       view = TRUE;
    243 
    244    /* Currently only used for compressed textures */
    245    if (render &&
    246        format != svga_translate_format(ss, surf_tmpl->format, surf_tmpl->usage)) {
    247       view = TRUE;
    248    }
    249 
    250    if (surf_tmpl->u.tex.level != 0 &&
    251        svga_screen(screen)->debug.force_level_surface_view)
    252       view = TRUE;
    253 
    254    if (pt->target == PIPE_TEXTURE_3D)
    255       view = TRUE;
    256 
    257    if (svga_screen(screen)->debug.no_surface_view)
    258       view = FALSE;
    259 
    260    if (view) {
    261       SVGA_DBG(DEBUG_VIEWS, "svga: Surface view: yes %p, level %u face %u z %u, %p\n",
    262                pt, surf_tmpl->u.tex.level, face, zslice, s);
    263 
    264       s->handle = svga_texture_view_surface(svga, tex, flags, format,
    265                                             surf_tmpl->u.tex.level,
    266                                             1, face, zslice, &s->key);
    267       s->real_face = 0;
    268       s->real_level = 0;
    269       s->real_zslice = 0;
    270    } else {
    271       SVGA_DBG(DEBUG_VIEWS, "svga: Surface view: no %p, level %u, face %u, z %u, %p\n",
    272                pt, surf_tmpl->u.tex.level, face, zslice, s);
    273 
    274       memset(&s->key, 0, sizeof s->key);
    275       s->handle = tex->handle;
    276       s->real_face = face;
    277       s->real_zslice = zslice;
    278       s->real_level = surf_tmpl->u.tex.level;
    279    }
    280 
    281    return &s->base;
    282 }
    283 
    284 
    285 static void
    286 svga_surface_destroy(struct pipe_context *pipe,
    287                      struct pipe_surface *surf)
    288 {
    289    struct svga_surface *s = svga_surface(surf);
    290    struct svga_texture *t = svga_texture(surf->texture);
    291    struct svga_screen *ss = svga_screen(surf->texture->screen);
    292 
    293    if (s->handle != t->handle) {
    294       SVGA_DBG(DEBUG_DMA, "unref sid %p (tex surface)\n", s->handle);
    295       svga_screen_surface_destroy(ss, &s->key, &s->handle);
    296    }
    297 
    298    pipe_resource_reference(&surf->texture, NULL);
    299    FREE(surf);
    300 }
    301 
    302 
    303 static INLINE void
    304 svga_mark_surface_dirty(struct pipe_surface *surf)
    305 {
    306    struct svga_surface *s = svga_surface(surf);
    307 
    308    if (!s->dirty) {
    309       struct svga_texture *tex = svga_texture(surf->texture);
    310 
    311       s->dirty = TRUE;
    312 
    313       if (s->handle == tex->handle) {
    314          /* hmm so 3d textures always have all their slices marked ? */
    315          if (surf->texture->target == PIPE_TEXTURE_CUBE)
    316             tex->defined[surf->u.tex.first_layer][surf->u.tex.level] = TRUE;
    317          else
    318             tex->defined[0][surf->u.tex.level] = TRUE;
    319       }
    320       else {
    321          /* this will happen later in svga_propagate_surface */
    322       }
    323 
    324       /* Increment the view_age and texture age for this surface's slice
    325        * so that any sampler views into the texture are re-validated too.
    326        */
    327       tex->view_age[surf->u.tex.first_layer] = ++(tex->age);
    328    }
    329 }
    330 
    331 
    332 void
    333 svga_mark_surfaces_dirty(struct svga_context *svga)
    334 {
    335    unsigned i;
    336 
    337    for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
    338       if (svga->curr.framebuffer.cbufs[i])
    339          svga_mark_surface_dirty(svga->curr.framebuffer.cbufs[i]);
    340    }
    341    if (svga->curr.framebuffer.zsbuf)
    342       svga_mark_surface_dirty(svga->curr.framebuffer.zsbuf);
    343 }
    344 
    345 
    346 /**
    347  * Progagate any changes from surfaces to texture.
    348  * pipe is optional context to inline the blit command in.
    349  */
    350 void
    351 svga_propagate_surface(struct svga_context *svga, struct pipe_surface *surf)
    352 {
    353    struct svga_surface *s = svga_surface(surf);
    354    struct svga_texture *tex = svga_texture(surf->texture);
    355    struct svga_screen *ss = svga_screen(surf->texture->screen);
    356    unsigned zslice, face;
    357 
    358    if (!s->dirty)
    359       return;
    360 
    361    if (surf->texture->target == PIPE_TEXTURE_CUBE) {
    362       zslice = 0;
    363       face = surf->u.tex.first_layer;
    364    }
    365    else {
    366       zslice = surf->u.tex.first_layer;
    367       face = 0;
    368    }
    369 
    370    s->dirty = FALSE;
    371    ss->texture_timestamp++;
    372    tex->view_age[surf->u.tex.level] = ++(tex->age);
    373 
    374    if (s->handle != tex->handle) {
    375       SVGA_DBG(DEBUG_VIEWS,
    376                "svga: Surface propagate: tex %p, level %u, from %p\n",
    377                tex, surf->u.tex.level, surf);
    378       svga_texture_copy_handle(svga,
    379                                s->handle, 0, 0, 0, s->real_level, s->real_face,
    380                                tex->handle, 0, 0, zslice, surf->u.tex.level, face,
    381                                u_minify(tex->b.b.width0, surf->u.tex.level),
    382                                u_minify(tex->b.b.height0, surf->u.tex.level), 1);
    383       tex->defined[face][surf->u.tex.level] = TRUE;
    384    }
    385 }
    386 
    387 
    388 /**
    389  * Check if we should call svga_propagate_surface on the surface.
    390  */
    391 boolean
    392 svga_surface_needs_propagation(const struct pipe_surface *surf)
    393 {
    394    const struct svga_surface *s = svga_surface_const(surf);
    395    struct svga_texture *tex = svga_texture(surf->texture);
    396 
    397    return s->dirty && s->handle != tex->handle;
    398 }
    399 
    400 
    401 
    402 void
    403 svga_init_surface_functions(struct svga_context *svga)
    404 {
    405    svga->pipe.create_surface = svga_create_surface;
    406    svga->pipe.surface_destroy = svga_surface_destroy;
    407 }
    408