Home | History | Annotate | Download | only in svga
      1 
      2 /**********************************************************
      3  * Copyright 2008-2009 VMware, Inc.  All rights reserved.
      4  *
      5  * Permission is hereby granted, free of charge, to any person
      6  * obtaining a copy of this software and associated documentation
      7  * files (the "Software"), to deal in the Software without
      8  * restriction, including without limitation the rights to use, copy,
      9  * modify, merge, publish, distribute, sublicense, and/or sell copies
     10  * of the Software, and to permit persons to whom the Software is
     11  * furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be
     14  * included in all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
     20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
     21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
     22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     23  * SOFTWARE.
     24  *
     25  **********************************************************/
     26 
     27 #include "util/u_format.h"
     28 #include "util/u_inlines.h"
     29 #include "util/u_memory.h"
     30 #include "pipe/p_defines.h"
     31 #include "util/u_upload_mgr.h"
     32 
     33 #include "svga_screen.h"
     34 #include "svga_context.h"
     35 #include "svga_state.h"
     36 #include "svga_cmd.h"
     37 #include "svga_tgsi.h"
     38 #include "svga_debug.h"
     39 #include "svga_resource_buffer.h"
     40 #include "svga_shader.h"
     41 
     42 #include "svga_hw_reg.h"
     43 
     44 
     45 /*
     46  * Don't try to send more than 4kb of successive constants.
     47  */
     48 #define MAX_CONST_REG_COUNT 256  /**< number of float[4] constants */
     49 
     50 /**
     51  * Extra space for svga-specific VS/PS constants (such as texcoord
     52  * scale factors, vertex transformation scale/translation).
     53  */
     54 #define MAX_EXTRA_CONSTS 32
     55 
     56 /** Guest-backed surface constant buffers must be this size */
     57 #define GB_CONSTBUF_SIZE (SVGA3D_CONSTREG_MAX)
     58 
     59 
     60 /**
     61  * Emit any extra shader-type-independent shader constants into the buffer
     62  * pointed to by 'dest'.
     63  * \return number of float[4] constants put into the 'dest' buffer
     64  */
     65 static unsigned
     66 svga_get_extra_constants_common(struct svga_context *svga,
     67                                 const struct svga_shader_variant *variant,
     68                                 enum pipe_shader_type shader, float *dest)
     69 {
     70    uint32_t *dest_u = (uint32_t *) dest;  // uint version of dest
     71    unsigned i;
     72    unsigned count = 0;
     73 
     74    for (i = 0; i < variant->key.num_textures; i++) {
     75       struct pipe_sampler_view *sv = svga->curr.sampler_views[shader][i];
     76       if (sv) {
     77          struct pipe_resource *tex = sv->texture;
     78          /* Scaling factors needed for handling unnormalized texture coordinates
     79           * for texture rectangles.
     80           */
     81          if (variant->key.tex[i].unnormalized) {
     82             /* debug/sanity check */
     83             assert(variant->key.tex[i].width_height_idx == count);
     84 
     85             *dest++ = 1.0 / (float)tex->width0;
     86             *dest++ = 1.0 / (float)tex->height0;
     87             *dest++ = 1.0;
     88             *dest++ = 1.0;
     89 
     90             count++;
     91          }
     92 
     93          /* Store the sizes for texture buffers.
     94          */
     95          if (tex->target == PIPE_BUFFER) {
     96             unsigned bytes_per_element = util_format_get_blocksize(sv->format);
     97             *dest_u++ = tex->width0 / bytes_per_element;
     98             *dest_u++ = 1;
     99             *dest_u++ = 1;
    100             *dest_u++ = 1;
    101 
    102             count++;
    103          }
    104       }
    105    }
    106 
    107    return count;
    108 }
    109 
    110 
    111 /**
    112  * Emit any extra fragment shader constants into the buffer pointed
    113  * to by 'dest'.
    114  * \return number of float[4] constants put into the dest buffer
    115  */
    116 static unsigned
    117 svga_get_extra_fs_constants(struct svga_context *svga, float *dest)
    118 {
    119    const struct svga_shader_variant *variant = svga->state.hw_draw.fs;
    120    unsigned count = 0;
    121 
    122    count += svga_get_extra_constants_common(svga, variant,
    123                                             PIPE_SHADER_FRAGMENT, dest);
    124 
    125    assert(count <= MAX_EXTRA_CONSTS);
    126 
    127    return count;
    128 }
    129 
    130 /**
    131  * Emit extra constants needed for prescale computation into the
    132  * the buffer pointed to by '*dest'. The updated buffer pointer
    133  * will be returned in 'dest'.
    134  */
    135 static unsigned
    136 svga_get_prescale_constants(struct svga_context *svga, float **dest)
    137 {
    138    memcpy(*dest, svga->state.hw_clear.prescale.scale, 4 * sizeof(float));
    139    *dest += 4;
    140 
    141    memcpy(*dest, svga->state.hw_clear.prescale.translate, 4 * sizeof(float));
    142    *dest += 4;
    143 
    144    return 2;
    145 }
    146 
    147 /**
    148  * Emit extra constants needed for point sprite emulation.
    149  */
    150 static unsigned
    151 svga_get_pt_sprite_constants(struct svga_context *svga, float **dest)
    152 {
    153    struct svga_screen *screen = svga_screen(svga->pipe.screen);
    154    float *dst = *dest;
    155 
    156    dst[0] = 1.0 / (svga->curr.viewport.scale[0] * 2);
    157    dst[1] = 1.0 / (svga->curr.viewport.scale[1] * 2);
    158    dst[2] = svga->curr.rast->pointsize;
    159    dst[3] = screen->maxPointSize;
    160    *dest = *dest + 4;
    161    return 1;
    162 }
    163 
    164 /**
    165  * Emit user-defined clip plane coefficients into the buffer pointed to
    166  * by '*dest'. The updated buffer pointer will be returned in 'dest'.
    167  */
    168 static unsigned
    169 svga_get_clip_plane_constants(struct svga_context *svga,
    170                               const struct svga_shader_variant *variant,
    171                               float **dest)
    172 {
    173    unsigned count = 0;
    174 
    175    /* SVGA_NEW_CLIP */
    176    if (svga_have_vgpu10(svga)) {
    177       /* append user-defined clip plane coefficients onto constant buffer */
    178       unsigned clip_planes = variant->key.clip_plane_enable;
    179       while (clip_planes) {
    180          int i = u_bit_scan(&clip_planes);
    181          COPY_4V(*dest, svga->curr.clip.ucp[i]);
    182          *dest += 4;
    183          count += 1;
    184       }
    185    }
    186    return count;
    187 }
    188 
    189 /**
    190  * Emit any extra vertex shader constants into the buffer pointed
    191  * to by 'dest'.
    192  * In particular, these would be the scale and bias factors computed
    193  * from the framebuffer size which are used to copy with differences in
    194  * GL vs D3D coordinate spaces.  See svga_tgsi_insn.c for more info.
    195  * \return number of float[4] constants put into the dest buffer
    196  */
    197 static unsigned
    198 svga_get_extra_vs_constants(struct svga_context *svga, float *dest)
    199 {
    200    const struct svga_shader_variant *variant = svga->state.hw_draw.vs;
    201    unsigned count = 0;
    202 
    203    /* SVGA_NEW_VS_VARIANT
    204     */
    205    if (variant->key.vs.need_prescale) {
    206       count += svga_get_prescale_constants(svga, &dest);
    207    }
    208 
    209    if (variant->key.vs.undo_viewport) {
    210       /* Used to convert window coords back to NDC coords */
    211       dest[0] = 1.0f / svga->curr.viewport.scale[0];
    212       dest[1] = 1.0f / svga->curr.viewport.scale[1];
    213       dest[2] = -svga->curr.viewport.translate[0];
    214       dest[3] = -svga->curr.viewport.translate[1];
    215       dest += 4;
    216       count += 1;
    217    }
    218 
    219    /* SVGA_NEW_CLIP */
    220    count += svga_get_clip_plane_constants(svga, variant, &dest);
    221 
    222    /* common constants */
    223    count += svga_get_extra_constants_common(svga, variant,
    224                                             PIPE_SHADER_VERTEX, dest);
    225 
    226    assert(count <= MAX_EXTRA_CONSTS);
    227 
    228    return count;
    229 }
    230 
    231 /**
    232  * Emit any extra geometry shader constants into the buffer pointed
    233  * to by 'dest'.
    234  */
    235 static unsigned
    236 svga_get_extra_gs_constants(struct svga_context *svga, float *dest)
    237 {
    238    const struct svga_shader_variant *variant = svga->state.hw_draw.gs;
    239    unsigned count = 0;
    240 
    241    /* SVGA_NEW_GS_VARIANT
    242     */
    243 
    244    /* Constants for point sprite
    245     * These are used in the transformed gs that supports point sprite.
    246     * They need to be added before the prescale constants.
    247     */
    248    if (variant->key.gs.wide_point) {
    249       count += svga_get_pt_sprite_constants(svga, &dest);
    250    }
    251 
    252    if (variant->key.gs.need_prescale) {
    253       count += svga_get_prescale_constants(svga, &dest);
    254    }
    255 
    256    /* SVGA_NEW_CLIP */
    257    count += svga_get_clip_plane_constants(svga, variant, &dest);
    258 
    259    /* common constants */
    260    count += svga_get_extra_constants_common(svga, variant,
    261                                             PIPE_SHADER_GEOMETRY, dest);
    262 
    263    assert(count <= MAX_EXTRA_CONSTS);
    264    return count;
    265 }
    266 
    267 /**
    268  * Check and emit one shader constant register.
    269  * \param shader  PIPE_SHADER_FRAGMENT or PIPE_SHADER_VERTEX
    270  * \param i  which float[4] constant to change
    271  * \param value  the new float[4] value
    272  */
    273 static enum pipe_error
    274 emit_const(struct svga_context *svga, enum pipe_shader_type shader, unsigned i,
    275            const float *value)
    276 {
    277    enum pipe_error ret = PIPE_OK;
    278 
    279    assert(shader < PIPE_SHADER_TYPES);
    280    assert(i < SVGA3D_CONSTREG_MAX);
    281    assert(!svga_have_vgpu10(svga));
    282 
    283    if (memcmp(svga->state.hw_draw.cb[shader][i], value,
    284               4 * sizeof(float)) != 0) {
    285       if (SVGA_DEBUG & DEBUG_CONSTS)
    286          debug_printf("%s %s %u: %f %f %f %f\n",
    287                       __FUNCTION__,
    288                       shader == PIPE_SHADER_VERTEX ? "VERT" : "FRAG",
    289                       i,
    290                       value[0],
    291                       value[1],
    292                       value[2],
    293                       value[3]);
    294 
    295       ret = SVGA3D_SetShaderConst( svga->swc,
    296                                    i,
    297                                    svga_shader_type(shader),
    298                                    SVGA3D_CONST_TYPE_FLOAT,
    299                                    value );
    300       if (ret != PIPE_OK)
    301          return ret;
    302 
    303       memcpy(svga->state.hw_draw.cb[shader][i], value, 4 * sizeof(float));
    304 
    305       svga->hud.num_const_updates++;
    306    }
    307 
    308    return ret;
    309 }
    310 
    311 
    312 /*
    313  * Check and emit a range of shader constant registers, trying to coalesce
    314  * successive shader constant updates in a single command in order to save
    315  * space on the command buffer.  This is a HWv8 feature.
    316  */
    317 static enum pipe_error
    318 emit_const_range(struct svga_context *svga,
    319                  enum pipe_shader_type shader,
    320                  unsigned offset,
    321                  unsigned count,
    322                  const float (*values)[4])
    323 {
    324    unsigned i, j;
    325    enum pipe_error ret;
    326 
    327    assert(shader == PIPE_SHADER_VERTEX ||
    328           shader == PIPE_SHADER_FRAGMENT);
    329    assert(!svga_have_vgpu10(svga));
    330 
    331 #ifdef DEBUG
    332    if (offset + count > SVGA3D_CONSTREG_MAX) {
    333       debug_printf("svga: too many constants (offset %u + count %u = %u (max = %u))\n",
    334                    offset, count, offset + count, SVGA3D_CONSTREG_MAX);
    335    }
    336 #endif
    337 
    338    if (offset > SVGA3D_CONSTREG_MAX) {
    339       /* This isn't OK, but if we propagate an error all the way up we'll
    340        * just get into more trouble.
    341        * XXX note that offset is always zero at this time so this is moot.
    342        */
    343       return PIPE_OK;
    344    }
    345 
    346    if (offset + count > SVGA3D_CONSTREG_MAX) {
    347       /* Just drop the extra constants for now.
    348        * Ideally we should not have allowed the app to create a shader
    349        * that exceeds our constant buffer size but there's no way to
    350        * express that in gallium at this time.
    351        */
    352       count = SVGA3D_CONSTREG_MAX - offset;
    353    }
    354 
    355    i = 0;
    356    while (i < count) {
    357       if (memcmp(svga->state.hw_draw.cb[shader][offset + i],
    358                  values[i],
    359                  4 * sizeof(float)) != 0) {
    360          /* Found one dirty constant
    361           */
    362          if (SVGA_DEBUG & DEBUG_CONSTS)
    363             debug_printf("%s %s %d: %f %f %f %f\n",
    364                          __FUNCTION__,
    365                          shader == PIPE_SHADER_VERTEX ? "VERT" : "FRAG",
    366                          offset + i,
    367                          values[i][0],
    368                          values[i][1],
    369                          values[i][2],
    370                          values[i][3]);
    371 
    372          /* Look for more consecutive dirty constants.
    373           */
    374          j = i + 1;
    375          while (j < count &&
    376                 j < i + MAX_CONST_REG_COUNT &&
    377                 memcmp(svga->state.hw_draw.cb[shader][offset + j],
    378                        values[j],
    379                        4 * sizeof(float)) != 0) {
    380 
    381             if (SVGA_DEBUG & DEBUG_CONSTS)
    382                debug_printf("%s %s %d: %f %f %f %f\n",
    383                             __FUNCTION__,
    384                             shader == PIPE_SHADER_VERTEX ? "VERT" : "FRAG",
    385                             offset + j,
    386                             values[j][0],
    387                             values[j][1],
    388                             values[j][2],
    389                             values[j][3]);
    390 
    391             ++j;
    392          }
    393 
    394          assert(j >= i + 1);
    395 
    396          /* Send them all together.
    397           */
    398          if (svga_have_gb_objects(svga)) {
    399             ret = SVGA3D_SetGBShaderConstsInline(svga->swc,
    400                                                  offset + i, /* start */
    401                                                  j - i,  /* count */
    402                                                  svga_shader_type(shader),
    403                                                  SVGA3D_CONST_TYPE_FLOAT,
    404                                                  values + i);
    405          }
    406          else {
    407             ret = SVGA3D_SetShaderConsts(svga->swc,
    408                                          offset + i, j - i,
    409                                          svga_shader_type(shader),
    410                                          SVGA3D_CONST_TYPE_FLOAT,
    411                                          values + i);
    412          }
    413          if (ret != PIPE_OK) {
    414             return ret;
    415          }
    416 
    417          /*
    418           * Local copy of the hardware state.
    419           */
    420          memcpy(svga->state.hw_draw.cb[shader][offset + i],
    421                 values[i],
    422                 (j - i) * 4 * sizeof(float));
    423 
    424          i = j + 1;
    425 
    426          svga->hud.num_const_updates++;
    427 
    428       } else {
    429          ++i;
    430       }
    431    }
    432 
    433    return PIPE_OK;
    434 }
    435 
    436 
    437 /**
    438  * Emit all the constants in a constant buffer for a shader stage.
    439  * On VGPU10, emit_consts_vgpu10 is used instead.
    440  */
    441 static enum pipe_error
    442 emit_consts_vgpu9(struct svga_context *svga, enum pipe_shader_type shader)
    443 {
    444    const struct pipe_constant_buffer *cbuf;
    445    struct svga_screen *ss = svga_screen(svga->pipe.screen);
    446    struct pipe_transfer *transfer = NULL;
    447    unsigned count;
    448    const float (*data)[4] = NULL;
    449    unsigned i;
    450    enum pipe_error ret = PIPE_OK;
    451    const unsigned offset = 0;
    452 
    453    assert(shader < PIPE_SHADER_TYPES);
    454    assert(!svga_have_vgpu10(svga));
    455    /* Only one constant buffer per shader is supported before VGPU10.
    456     * This is only an approximate check against that.
    457     */
    458    assert(svga->curr.constbufs[shader][1].buffer == NULL);
    459 
    460    cbuf = &svga->curr.constbufs[shader][0];
    461 
    462    if (svga->curr.constbufs[shader][0].buffer) {
    463       /* emit user-provided constants */
    464       data = (const float (*)[4])
    465          pipe_buffer_map(&svga->pipe, svga->curr.constbufs[shader][0].buffer,
    466                          PIPE_TRANSFER_READ, &transfer);
    467       if (!data) {
    468          return PIPE_ERROR_OUT_OF_MEMORY;
    469       }
    470 
    471       /* sanity check */
    472       assert(cbuf->buffer->width0 >=
    473              cbuf->buffer_size);
    474 
    475       /* Use/apply the constant buffer size and offsets here */
    476       count = cbuf->buffer_size / (4 * sizeof(float));
    477       data += cbuf->buffer_offset / (4 * sizeof(float));
    478 
    479       if (ss->hw_version >= SVGA3D_HWVERSION_WS8_B1) {
    480          ret = emit_const_range( svga, shader, offset, count, data );
    481       }
    482       else {
    483          for (i = 0; i < count; i++) {
    484             ret = emit_const( svga, shader, offset + i, data[i] );
    485             if (ret != PIPE_OK) {
    486                break;
    487             }
    488          }
    489       }
    490 
    491       pipe_buffer_unmap(&svga->pipe, transfer);
    492 
    493       if (ret != PIPE_OK) {
    494          return ret;
    495       }
    496    }
    497 
    498    /* emit extra shader constants */
    499    {
    500       const struct svga_shader_variant *variant = NULL;
    501       unsigned offset;
    502       float extras[MAX_EXTRA_CONSTS][4];
    503       unsigned count, i;
    504 
    505       switch (shader) {
    506       case PIPE_SHADER_VERTEX:
    507          variant = svga->state.hw_draw.vs;
    508          count = svga_get_extra_vs_constants(svga, (float *) extras);
    509          break;
    510       case PIPE_SHADER_FRAGMENT:
    511          variant = svga->state.hw_draw.fs;
    512          count = svga_get_extra_fs_constants(svga, (float *) extras);
    513          break;
    514       default:
    515          assert(!"Unexpected shader type");
    516          count = 0;
    517       }
    518 
    519       assert(variant);
    520       offset = variant->shader->info.file_max[TGSI_FILE_CONSTANT] + 1;
    521       assert(count <= ARRAY_SIZE(extras));
    522 
    523       if (count > 0) {
    524          if (ss->hw_version >= SVGA3D_HWVERSION_WS8_B1) {
    525             ret = emit_const_range(svga, shader, offset, count,
    526                                    (const float (*) [4])extras);
    527          }
    528          else {
    529             for (i = 0; i < count; i++) {
    530                ret = emit_const(svga, shader, offset + i, extras[i]);
    531                if (ret != PIPE_OK)
    532                   return ret;
    533             }
    534          }
    535       }
    536    }
    537 
    538    return ret;
    539 }
    540 
    541 
    542 
    543 static enum pipe_error
    544 emit_constbuf_vgpu10(struct svga_context *svga, enum pipe_shader_type shader)
    545 {
    546    const struct pipe_constant_buffer *cbuf;
    547    struct pipe_resource *dst_buffer = NULL;
    548    enum pipe_error ret = PIPE_OK;
    549    struct pipe_transfer *src_transfer;
    550    struct svga_winsys_surface *dst_handle;
    551    float extras[MAX_EXTRA_CONSTS][4];
    552    unsigned extra_count, extra_size, extra_offset;
    553    unsigned new_buf_size;
    554    void *src_map = NULL, *dst_map;
    555    unsigned offset;
    556    const struct svga_shader_variant *variant;
    557    unsigned alloc_buf_size;
    558 
    559    assert(shader == PIPE_SHADER_VERTEX ||
    560           shader == PIPE_SHADER_GEOMETRY ||
    561           shader == PIPE_SHADER_FRAGMENT);
    562 
    563    cbuf = &svga->curr.constbufs[shader][0];
    564 
    565    switch (shader) {
    566    case PIPE_SHADER_VERTEX:
    567       variant = svga->state.hw_draw.vs;
    568       extra_count = svga_get_extra_vs_constants(svga, (float *) extras);
    569       break;
    570    case PIPE_SHADER_FRAGMENT:
    571       variant = svga->state.hw_draw.fs;
    572       extra_count = svga_get_extra_fs_constants(svga, (float *) extras);
    573       break;
    574    case PIPE_SHADER_GEOMETRY:
    575       variant = svga->state.hw_draw.gs;
    576       extra_count = svga_get_extra_gs_constants(svga, (float *) extras);
    577       break;
    578    default:
    579       assert(!"Unexpected shader type");
    580       /* Don't return an error code since we don't want to keep re-trying
    581        * this function and getting stuck in an infinite loop.
    582        */
    583       return PIPE_OK;
    584    }
    585 
    586    assert(variant);
    587 
    588    /* Compute extra constants size and offset in bytes */
    589    extra_size = extra_count * 4 * sizeof(float);
    590    extra_offset = 4 * sizeof(float) * variant->extra_const_start;
    591 
    592    if (cbuf->buffer_size + extra_size == 0)
    593       return PIPE_OK;  /* nothing to do */
    594 
    595    /* Typically, the cbuf->buffer here is a user-space buffer so mapping
    596     * it is really cheap.  If we ever get real HW buffers for constants
    597     * we should void mapping and instead use a ResourceCopy command.
    598     */
    599    if (cbuf->buffer_size > 0) {
    600       src_map = pipe_buffer_map_range(&svga->pipe, cbuf->buffer,
    601                                       cbuf->buffer_offset, cbuf->buffer_size,
    602                                       PIPE_TRANSFER_READ, &src_transfer);
    603       assert(src_map);
    604       if (!src_map) {
    605          return PIPE_ERROR_OUT_OF_MEMORY;
    606       }
    607    }
    608 
    609    /* The new/dest buffer's size must be large enough to hold the original,
    610     * user-specified constants, plus the extra constants.
    611     * The size of the original constant buffer _should_ agree with what the
    612     * shader is expecting, but it might not (it's not enforced anywhere by
    613     * gallium).
    614     */
    615    new_buf_size = MAX2(cbuf->buffer_size, extra_offset) + extra_size;
    616 
    617    /* According to the DX10 spec, the constant buffer size must be
    618     * in multiples of 16.
    619     */
    620    new_buf_size = align(new_buf_size, 16);
    621 
    622    /* Constant buffer size in the upload buffer must be in multiples of 256.
    623     * In order to maximize the chance of merging the upload buffer chunks
    624     * when svga_buffer_add_range() is called,
    625     * the allocate buffer size needs to be in multiples of 256 as well.
    626     * Otherwise, since there is gap between each dirty range of the upload buffer,
    627     * each dirty range will end up in its own UPDATE_GB_IMAGE command.
    628     */
    629    alloc_buf_size = align(new_buf_size, CONST0_UPLOAD_ALIGNMENT);
    630 
    631    u_upload_alloc(svga->const0_upload, 0, alloc_buf_size,
    632                   CONST0_UPLOAD_ALIGNMENT, &offset,
    633                   &dst_buffer, &dst_map);
    634    if (!dst_map) {
    635       if (src_map)
    636          pipe_buffer_unmap(&svga->pipe, src_transfer);
    637       return PIPE_ERROR_OUT_OF_MEMORY;
    638    }
    639 
    640    if (src_map) {
    641       memcpy(dst_map, src_map, cbuf->buffer_size);
    642       pipe_buffer_unmap(&svga->pipe, src_transfer);
    643    }
    644 
    645    if (extra_size) {
    646       assert(extra_offset + extra_size <= new_buf_size);
    647       memcpy((char *) dst_map + extra_offset, extras, extra_size);
    648    }
    649 
    650    /* Get winsys handle for the constant buffer */
    651    if (svga->state.hw_draw.const0_buffer == dst_buffer &&
    652        svga->state.hw_draw.const0_handle) {
    653       /* re-reference already mapped buffer */
    654       dst_handle = svga->state.hw_draw.const0_handle;
    655    }
    656    else {
    657       /* we must unmap the buffer before getting the winsys handle */
    658       u_upload_unmap(svga->const0_upload);
    659 
    660       dst_handle = svga_buffer_handle(svga, dst_buffer);
    661       if (!dst_handle) {
    662          pipe_resource_reference(&dst_buffer, NULL);
    663          return PIPE_ERROR_OUT_OF_MEMORY;
    664       }
    665 
    666       /* save the buffer / handle for next time */
    667       pipe_resource_reference(&svga->state.hw_draw.const0_buffer, dst_buffer);
    668       svga->state.hw_draw.const0_handle = dst_handle;
    669    }
    670 
    671    /* Issue the SetSingleConstantBuffer command */
    672    assert(new_buf_size % 16 == 0);
    673    ret = SVGA3D_vgpu10_SetSingleConstantBuffer(svga->swc,
    674                                                0, /* index */
    675                                                svga_shader_type(shader),
    676                                                dst_handle,
    677                                                offset,
    678                                                new_buf_size);
    679 
    680    if (ret != PIPE_OK) {
    681       pipe_resource_reference(&dst_buffer, NULL);
    682       return ret;
    683    }
    684 
    685    /* Save this const buffer until it's replaced in the future.
    686     * Otherwise, all references to the buffer will go away after the
    687     * command buffer is submitted, it'll get recycled and we will have
    688     * incorrect constant buffer bindings.
    689     */
    690    pipe_resource_reference(&svga->state.hw_draw.constbuf[shader], dst_buffer);
    691 
    692    svga->state.hw_draw.default_constbuf_size[shader] = new_buf_size;
    693 
    694    pipe_resource_reference(&dst_buffer, NULL);
    695 
    696    svga->hud.num_const_buf_updates++;
    697 
    698    return ret;
    699 }
    700 
    701 
    702 static enum pipe_error
    703 emit_consts_vgpu10(struct svga_context *svga, enum pipe_shader_type shader)
    704 {
    705    enum pipe_error ret;
    706    unsigned dirty_constbufs;
    707    unsigned enabled_constbufs;
    708 
    709    /* Emit 0th constant buffer (with extra constants) */
    710    ret = emit_constbuf_vgpu10(svga, shader);
    711    if (ret != PIPE_OK) {
    712       return ret;
    713    }
    714 
    715    enabled_constbufs = svga->state.hw_draw.enabled_constbufs[shader] | 1u;
    716 
    717    /* Emit other constant buffers (UBOs) */
    718    dirty_constbufs = svga->state.dirty_constbufs[shader] & ~1u;
    719 
    720    while (dirty_constbufs) {
    721       unsigned index = u_bit_scan(&dirty_constbufs);
    722       unsigned offset = svga->curr.constbufs[shader][index].buffer_offset;
    723       unsigned size = svga->curr.constbufs[shader][index].buffer_size;
    724       struct svga_buffer *buffer =
    725          svga_buffer(svga->curr.constbufs[shader][index].buffer);
    726       struct svga_winsys_surface *handle;
    727 
    728       if (buffer) {
    729          handle = svga_buffer_handle(svga, &buffer->b.b);
    730          enabled_constbufs |= 1 << index;
    731       }
    732       else {
    733          handle = NULL;
    734          enabled_constbufs &= ~(1 << index);
    735          assert(offset == 0);
    736          assert(size == 0);
    737       }
    738 
    739       if (size % 16 != 0) {
    740          /* GL's buffer range sizes can be any number of bytes but the
    741           * SVGA3D device requires a multiple of 16 bytes.
    742           */
    743          const unsigned total_size = buffer->b.b.width0;
    744 
    745          if (offset + align(size, 16) <= total_size) {
    746             /* round up size to multiple of 16 */
    747             size = align(size, 16);
    748          }
    749          else {
    750             /* round down to mulitple of 16 (this may cause rendering problems
    751              * but should avoid a device error).
    752              */
    753             size &= ~15;
    754          }
    755       }
    756 
    757       assert(size % 16 == 0);
    758       ret = SVGA3D_vgpu10_SetSingleConstantBuffer(svga->swc,
    759                                                   index,
    760                                                   svga_shader_type(shader),
    761                                                   handle,
    762                                                   offset,
    763                                                   size);
    764       if (ret != PIPE_OK)
    765          return ret;
    766 
    767       svga->hud.num_const_buf_updates++;
    768    }
    769 
    770    svga->state.hw_draw.enabled_constbufs[shader] = enabled_constbufs;
    771    svga->state.dirty_constbufs[shader] = 0;
    772 
    773    return ret;
    774 }
    775 
    776 static enum pipe_error
    777 emit_fs_consts(struct svga_context *svga, unsigned dirty)
    778 {
    779    const struct svga_shader_variant *variant = svga->state.hw_draw.fs;
    780    enum pipe_error ret = PIPE_OK;
    781 
    782    /* SVGA_NEW_FS_VARIANT
    783     */
    784    if (!variant)
    785       return PIPE_OK;
    786 
    787    /* SVGA_NEW_FS_CONST_BUFFER
    788     */
    789    if (svga_have_vgpu10(svga)) {
    790       ret = emit_consts_vgpu10(svga, PIPE_SHADER_FRAGMENT);
    791    }
    792    else {
    793       ret = emit_consts_vgpu9(svga, PIPE_SHADER_FRAGMENT);
    794    }
    795 
    796    return ret;
    797 }
    798 
    799 
    800 struct svga_tracked_state svga_hw_fs_constants =
    801 {
    802    "hw fs params",
    803    (SVGA_NEW_FS_CONST_BUFFER |
    804     SVGA_NEW_FS_VARIANT |
    805     SVGA_NEW_TEXTURE_CONSTS),
    806    emit_fs_consts
    807 };
    808 
    809 
    810 
    811 static enum pipe_error
    812 emit_vs_consts(struct svga_context *svga, unsigned dirty)
    813 {
    814    const struct svga_shader_variant *variant = svga->state.hw_draw.vs;
    815    enum pipe_error ret = PIPE_OK;
    816 
    817    /* SVGA_NEW_VS_VARIANT
    818     */
    819    if (!variant)
    820       return PIPE_OK;
    821 
    822    /* SVGA_NEW_VS_CONST_BUFFER
    823     */
    824    if (svga_have_vgpu10(svga)) {
    825       ret = emit_consts_vgpu10(svga, PIPE_SHADER_VERTEX);
    826    }
    827    else {
    828       ret = emit_consts_vgpu9(svga, PIPE_SHADER_VERTEX);
    829    }
    830 
    831    return ret;
    832 }
    833 
    834 
    835 struct svga_tracked_state svga_hw_vs_constants =
    836 {
    837    "hw vs params",
    838    (SVGA_NEW_PRESCALE |
    839     SVGA_NEW_VS_CONST_BUFFER |
    840     SVGA_NEW_VS_VARIANT),
    841    emit_vs_consts
    842 };
    843 
    844 
    845 static enum pipe_error
    846 emit_gs_consts(struct svga_context *svga, unsigned dirty)
    847 {
    848    const struct svga_shader_variant *variant = svga->state.hw_draw.gs;
    849    enum pipe_error ret = PIPE_OK;
    850 
    851    /* SVGA_NEW_GS_VARIANT
    852     */
    853    if (!variant)
    854       return PIPE_OK;
    855 
    856    /* SVGA_NEW_GS_CONST_BUFFER
    857     */
    858    if (svga_have_vgpu10(svga)) {
    859       /**
    860        * If only the rasterizer state has changed and the current geometry
    861        * shader does not emit wide points, then there is no reason to
    862        * re-emit the GS constants, so skip it.
    863        */
    864       if (dirty == SVGA_NEW_RAST && !variant->key.gs.wide_point)
    865          return PIPE_OK;
    866 
    867       ret = emit_consts_vgpu10(svga, PIPE_SHADER_GEOMETRY);
    868    }
    869 
    870    return ret;
    871 }
    872 
    873 
    874 struct svga_tracked_state svga_hw_gs_constants =
    875 {
    876    "hw gs params",
    877    (SVGA_NEW_GS_CONST_BUFFER |
    878     SVGA_NEW_RAST |
    879     SVGA_NEW_GS_VARIANT),
    880    emit_gs_consts
    881 };
    882