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 "util/u_framebuffer.h"
     27 #include "util/u_inlines.h"
     28 #include "util/u_pstipple.h"
     29 
     30 #include "svga_cmd.h"
     31 #include "svga_context.h"
     32 #include "svga_screen.h"
     33 #include "svga_surface.h"
     34 #include "svga_resource_texture.h"
     35 
     36 
     37 static void
     38 svga_set_scissor_states(struct pipe_context *pipe,
     39                         unsigned start_slot,
     40                         unsigned num_scissors,
     41                         const struct pipe_scissor_state *scissors)
     42 {
     43    struct svga_context *svga = svga_context(pipe);
     44 
     45    memcpy(&svga->curr.scissor, scissors, sizeof(*scissors));
     46    svga->dirty |= SVGA_NEW_SCISSOR;
     47 }
     48 
     49 
     50 static void
     51 svga_set_polygon_stipple(struct pipe_context *pipe,
     52                          const struct pipe_poly_stipple *stipple)
     53 {
     54    struct svga_context *svga = svga_context(pipe);
     55 
     56    /* release old texture */
     57    pipe_resource_reference(&svga->polygon_stipple.texture, NULL);
     58 
     59    /* release old sampler view */
     60    if (svga->polygon_stipple.sampler_view) {
     61       pipe->sampler_view_destroy(pipe,
     62                                  &svga->polygon_stipple.sampler_view->base);
     63    }
     64 
     65    /* create new stipple texture */
     66    svga->polygon_stipple.texture =
     67       util_pstipple_create_stipple_texture(pipe, stipple->stipple);
     68 
     69    /* create new sampler view */
     70    svga->polygon_stipple.sampler_view =
     71       (struct svga_pipe_sampler_view *)
     72       util_pstipple_create_sampler_view(pipe,
     73                                         svga->polygon_stipple.texture);
     74 
     75    /* allocate sampler state, if first time */
     76    if (!svga->polygon_stipple.sampler) {
     77       svga->polygon_stipple.sampler = util_pstipple_create_sampler(pipe);
     78    }
     79 
     80    svga->dirty |= SVGA_NEW_STIPPLE;
     81 }
     82 
     83 
     84 void
     85 svga_cleanup_framebuffer(struct svga_context *svga)
     86 {
     87    struct svga_screen *svgascreen = svga_screen(svga->pipe.screen);
     88    struct pipe_framebuffer_state *curr = &svga->curr.framebuffer;
     89    struct pipe_framebuffer_state *hw = &svga->state.hw_clear.framebuffer;
     90    unsigned i;
     91 
     92    for (i = 0; i < svgascreen->max_color_buffers; i++) {
     93       pipe_surface_reference(&curr->cbufs[i], NULL);
     94       pipe_surface_reference(&hw->cbufs[i], NULL);
     95    }
     96 
     97    pipe_surface_reference(&curr->zsbuf, NULL);
     98    pipe_surface_reference(&hw->zsbuf, NULL);
     99 }
    100 
    101 
    102 #define DEPTH_BIAS_SCALE_FACTOR_D16    ((float)(1<<15))
    103 #define DEPTH_BIAS_SCALE_FACTOR_D24S8  ((float)(1<<23))
    104 #define DEPTH_BIAS_SCALE_FACTOR_D32    ((float)(1<<31))
    105 
    106 
    107 static void
    108 svga_set_framebuffer_state(struct pipe_context *pipe,
    109                            const struct pipe_framebuffer_state *fb)
    110 {
    111    struct svga_context *svga = svga_context(pipe);
    112    struct pipe_framebuffer_state *dst = &svga->curr.framebuffer;
    113    unsigned i;
    114 
    115    /* make sure any pending drawing calls are flushed before changing
    116     * the framebuffer state
    117     */
    118    svga_hwtnl_flush_retry(svga);
    119 
    120    dst->width = fb->width;
    121    dst->height = fb->height;
    122    dst->nr_cbufs = fb->nr_cbufs;
    123 
    124    /* Check if we need to propagate any of the render targets which we may
    125     * be unbinding.
    126     */
    127    for (i = 0; i < dst->nr_cbufs; i++) {
    128       struct pipe_surface *s = i < fb->nr_cbufs ? fb->cbufs[i] : NULL;
    129       if (dst->cbufs[i] && dst->cbufs[i] != s) {
    130          if (svga_surface_needs_propagation(dst->cbufs[i])) {
    131             svga_propagate_surface(svga, dst->cbufs[i]);
    132          }
    133       }
    134    }
    135 
    136    /* Check that all surfaces are the same size.
    137     * Actually, the virtual hardware may support rendertargets with
    138     * different size, depending on the host API and driver,
    139     */
    140    {
    141       int width = 0, height = 0;
    142       if (fb->zsbuf) {
    143          width = fb->zsbuf->width;
    144          height = fb->zsbuf->height;
    145       }
    146       for (i = 0; i < fb->nr_cbufs; ++i) {
    147          if (fb->cbufs[i]) {
    148             if (width && height) {
    149                if (fb->cbufs[i]->width != width ||
    150                    fb->cbufs[i]->height != height) {
    151                   debug_warning("Mixed-size color and depth/stencil surfaces "
    152                                 "may not work properly");
    153                }
    154             }
    155             else {
    156                width = fb->cbufs[i]->width;
    157                height = fb->cbufs[i]->height;
    158             }
    159          }
    160       }
    161    }
    162 
    163    util_copy_framebuffer_state(dst, fb);
    164 
    165    /* Set the rendered-to flags */
    166    for (i = 0; i < dst->nr_cbufs; i++) {
    167       struct pipe_surface *s = dst->cbufs[i];
    168       if (s) {
    169          struct svga_texture *t = svga_texture(s->texture);
    170          svga_set_texture_rendered_to(t, s->u.tex.first_layer, s->u.tex.level);
    171       }
    172    }
    173 
    174    if (svga->curr.framebuffer.zsbuf) {
    175       switch (svga->curr.framebuffer.zsbuf->format) {
    176       case PIPE_FORMAT_Z16_UNORM:
    177          svga->curr.depthscale = 1.0f / DEPTH_BIAS_SCALE_FACTOR_D16;
    178          break;
    179       case PIPE_FORMAT_Z24_UNORM_S8_UINT:
    180       case PIPE_FORMAT_Z24X8_UNORM:
    181       case PIPE_FORMAT_S8_UINT_Z24_UNORM:
    182       case PIPE_FORMAT_X8Z24_UNORM:
    183          svga->curr.depthscale = 1.0f / DEPTH_BIAS_SCALE_FACTOR_D24S8;
    184          break;
    185       case PIPE_FORMAT_Z32_UNORM:
    186          svga->curr.depthscale = 1.0f / DEPTH_BIAS_SCALE_FACTOR_D32;
    187          break;
    188       case PIPE_FORMAT_Z32_FLOAT:
    189          svga->curr.depthscale = 1.0f / ((float)(1<<23));
    190          break;
    191       default:
    192          svga->curr.depthscale = 0.0f;
    193          break;
    194       }
    195 
    196       /* Set rendered-to flag */
    197       {
    198          struct pipe_surface *s = dst->zsbuf;
    199          struct svga_texture *t = svga_texture(s->texture);
    200          svga_set_texture_rendered_to(t, s->u.tex.first_layer, s->u.tex.level);
    201       }
    202    }
    203    else {
    204       svga->curr.depthscale = 0.0f;
    205    }
    206 
    207    svga->dirty |= SVGA_NEW_FRAME_BUFFER;
    208 }
    209 
    210 
    211 static void
    212 svga_set_clip_state(struct pipe_context *pipe,
    213                     const struct pipe_clip_state *clip)
    214 {
    215    struct svga_context *svga = svga_context(pipe);
    216 
    217    svga->curr.clip = *clip; /* struct copy */
    218 
    219    svga->dirty |= SVGA_NEW_CLIP;
    220 }
    221 
    222 
    223 static void
    224 svga_set_viewport_states(struct pipe_context *pipe,
    225                          unsigned start_slot,
    226                          unsigned num_viewports,
    227                          const struct pipe_viewport_state *viewports)
    228 {
    229    struct svga_context *svga = svga_context(pipe);
    230 
    231    svga->curr.viewport = *viewports; /* struct copy */
    232 
    233    svga->dirty |= SVGA_NEW_VIEWPORT;
    234 }
    235 
    236 
    237 /**
    238  * Called by state tracker to specify a callback function the driver
    239  * can use to report info back to the state tracker.
    240  */
    241 static void
    242 svga_set_debug_callback(struct pipe_context *pipe,
    243                         const struct pipe_debug_callback *cb)
    244 {
    245    struct svga_context *svga = svga_context(pipe);
    246 
    247    if (cb) {
    248       svga->debug.callback = *cb;
    249       svga->swc->debug_callback = &svga->debug.callback;
    250    } else {
    251       memset(&svga->debug.callback, 0, sizeof(svga->debug.callback));
    252       svga->swc->debug_callback = NULL;
    253    }
    254 }
    255 
    256 
    257 void
    258 svga_init_misc_functions(struct svga_context *svga)
    259 {
    260    svga->pipe.set_scissor_states = svga_set_scissor_states;
    261    svga->pipe.set_polygon_stipple = svga_set_polygon_stipple;
    262    svga->pipe.set_framebuffer_state = svga_set_framebuffer_state;
    263    svga->pipe.set_clip_state = svga_set_clip_state;
    264    svga->pipe.set_viewport_states = svga_set_viewport_states;
    265    svga->pipe.set_debug_callback = svga_set_debug_callback;
    266 }
    267