Home | History | Annotate | Download | only in util
      1 /**************************************************************************
      2  *
      3  * Copyright 2009 Marek Olk <maraeo (at) gmail.com>
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining a
      6  * copy of this software and associated documentation files (the
      7  * "Software"), to deal in the Software without restriction, including
      8  * without limitation the rights to use, copy, modify, merge, publish,
      9  * distribute, sub license, and/or sell copies of the Software, and to
     10  * permit persons to whom the Software is furnished to do so, subject to
     11  * the following conditions:
     12  *
     13  * The above copyright notice and this permission notice (including the
     14  * next paragraph) shall be included in all copies or substantial portions
     15  * of the Software.
     16  *
     17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
     20  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
     21  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     24  *
     25  **************************************************************************/
     26 
     27 /**
     28  * @file
     29  * Blitter utility to facilitate acceleration of the clear, clear_render_target,
     30  * clear_depth_stencil, resource_copy_region, and blit functions.
     31  *
     32  * @author Marek Olk
     33  */
     34 
     35 #include "pipe/p_context.h"
     36 #include "pipe/p_defines.h"
     37 #include "util/u_inlines.h"
     38 #include "pipe/p_shader_tokens.h"
     39 #include "pipe/p_state.h"
     40 
     41 #include "util/u_format.h"
     42 #include "util/u_memory.h"
     43 #include "util/u_math.h"
     44 #include "util/u_blitter.h"
     45 #include "util/u_draw_quad.h"
     46 #include "util/u_sampler.h"
     47 #include "util/u_simple_shaders.h"
     48 #include "util/u_surface.h"
     49 #include "util/u_texture.h"
     50 #include "util/u_upload_mgr.h"
     51 
     52 #define INVALID_PTR ((void*)~0)
     53 
     54 #define GET_CLEAR_BLEND_STATE_IDX(clear_buffers) \
     55    ((clear_buffers) / PIPE_CLEAR_COLOR0)
     56 
     57 #define NUM_RESOLVE_FRAG_SHADERS 5 /* MSAA 2x, 4x, 8x, 16x, 32x */
     58 #define GET_MSAA_RESOLVE_FS_IDX(nr_samples) (util_logbase2(nr_samples)-1)
     59 
     60 struct blitter_context_priv
     61 {
     62    struct blitter_context base;
     63 
     64    float vertices[4][2][4];   /**< {pos, color} or {pos, texcoord} */
     65 
     66    /* Templates for various state objects. */
     67 
     68    /* Constant state objects. */
     69    /* Vertex shaders. */
     70    void *vs; /**< Vertex shader which passes {pos, generic} to the output.*/
     71    void *vs_nogeneric;
     72    void *vs_pos_only[4]; /**< Vertex shader which passes pos to the output
     73                               for clear_buffer/copy_buffer.*/
     74    void *vs_layered; /**< Vertex shader which sets LAYER = INSTANCEID. */
     75 
     76    /* Fragment shaders. */
     77    void *fs_empty;
     78    void *fs_write_one_cbuf;
     79    void *fs_write_all_cbufs;
     80 
     81    /* FS which outputs a color from a texture where
     82     * the 1st index indicates the texture type / destination type,
     83     * the 2nd index is the PIPE_TEXTURE_* to be sampled,
     84     * the 3rd index is 0 = use TEX, 1 = use TXF.
     85     */
     86    void *fs_texfetch_col[5][PIPE_MAX_TEXTURE_TYPES][2];
     87 
     88    /* FS which outputs a depth from a texture, where
     89     * the 1st index is the PIPE_TEXTURE_* to be sampled,
     90     * the 2nd index is 0 = use TEX, 1 = use TXF.
     91     */
     92    void *fs_texfetch_depth[PIPE_MAX_TEXTURE_TYPES][2];
     93    void *fs_texfetch_depthstencil[PIPE_MAX_TEXTURE_TYPES][2];
     94    void *fs_texfetch_stencil[PIPE_MAX_TEXTURE_TYPES][2];
     95 
     96    /* FS which outputs one sample from a multisample texture. */
     97    void *fs_texfetch_col_msaa[5][PIPE_MAX_TEXTURE_TYPES];
     98    void *fs_texfetch_depth_msaa[PIPE_MAX_TEXTURE_TYPES];
     99    void *fs_texfetch_depthstencil_msaa[PIPE_MAX_TEXTURE_TYPES];
    100    void *fs_texfetch_stencil_msaa[PIPE_MAX_TEXTURE_TYPES];
    101 
    102    /* FS which outputs an average of all samples. */
    103    void *fs_resolve[PIPE_MAX_TEXTURE_TYPES][NUM_RESOLVE_FRAG_SHADERS][2];
    104 
    105    /* Blend state. */
    106    void *blend[PIPE_MASK_RGBA+1][2]; /**< blend state with writemask */
    107    void *blend_clear[GET_CLEAR_BLEND_STATE_IDX(PIPE_CLEAR_COLOR)+1];
    108 
    109    /* Depth stencil alpha state. */
    110    void *dsa_write_depth_stencil;
    111    void *dsa_write_depth_keep_stencil;
    112    void *dsa_keep_depth_stencil;
    113    void *dsa_keep_depth_write_stencil;
    114 
    115    /* Vertex elements states. */
    116    void *velem_state;
    117    void *velem_state_readbuf[4]; /**< X, XY, XYZ, XYZW */
    118 
    119    /* Sampler state. */
    120    void *sampler_state;
    121    void *sampler_state_linear;
    122    void *sampler_state_rect;
    123    void *sampler_state_rect_linear;
    124 
    125    /* Rasterizer state. */
    126    void *rs_state, *rs_state_scissor, *rs_discard_state;
    127 
    128    /* Destination surface dimensions. */
    129    unsigned dst_width;
    130    unsigned dst_height;
    131 
    132    bool has_geometry_shader;
    133    bool has_tessellation;
    134    bool has_layered;
    135    bool has_stream_out;
    136    bool has_stencil_export;
    137    bool has_texture_multisample;
    138    bool has_tex_lz;
    139    bool has_txf;
    140    bool cube_as_2darray;
    141    bool cached_all_shaders;
    142 
    143    /* The Draw module overrides these functions.
    144     * Always create the blitter before Draw. */
    145    void   (*bind_fs_state)(struct pipe_context *, void *);
    146    void   (*delete_fs_state)(struct pipe_context *, void *);
    147 };
    148 
    149 struct blitter_context *util_blitter_create(struct pipe_context *pipe)
    150 {
    151    struct blitter_context_priv *ctx;
    152    struct pipe_blend_state blend;
    153    struct pipe_depth_stencil_alpha_state dsa;
    154    struct pipe_rasterizer_state rs_state;
    155    struct pipe_sampler_state sampler_state;
    156    struct pipe_vertex_element velem[2];
    157    unsigned i, j;
    158 
    159    ctx = CALLOC_STRUCT(blitter_context_priv);
    160    if (!ctx)
    161       return NULL;
    162 
    163    ctx->base.pipe = pipe;
    164    ctx->base.draw_rectangle = util_blitter_draw_rectangle;
    165 
    166    ctx->bind_fs_state = pipe->bind_fs_state;
    167    ctx->delete_fs_state = pipe->delete_fs_state;
    168 
    169    /* init state objects for them to be considered invalid */
    170    ctx->base.saved_blend_state = INVALID_PTR;
    171    ctx->base.saved_dsa_state = INVALID_PTR;
    172    ctx->base.saved_rs_state = INVALID_PTR;
    173    ctx->base.saved_fs = INVALID_PTR;
    174    ctx->base.saved_vs = INVALID_PTR;
    175    ctx->base.saved_gs = INVALID_PTR;
    176    ctx->base.saved_velem_state = INVALID_PTR;
    177    ctx->base.saved_fb_state.nr_cbufs = ~0;
    178    ctx->base.saved_num_sampler_views = ~0;
    179    ctx->base.saved_num_sampler_states = ~0;
    180    ctx->base.saved_num_so_targets = ~0;
    181 
    182    ctx->has_geometry_shader =
    183       pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_GEOMETRY,
    184                                      PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0;
    185 
    186    ctx->has_tessellation =
    187       pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_TESS_CTRL,
    188                                      PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0;
    189 
    190    ctx->has_stream_out =
    191       pipe->screen->get_param(pipe->screen,
    192                               PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS) != 0;
    193 
    194    ctx->has_stencil_export =
    195          pipe->screen->get_param(pipe->screen,
    196                                  PIPE_CAP_SHADER_STENCIL_EXPORT);
    197 
    198    ctx->has_texture_multisample =
    199       pipe->screen->get_param(pipe->screen, PIPE_CAP_TEXTURE_MULTISAMPLE);
    200 
    201    ctx->has_tex_lz = pipe->screen->get_param(pipe->screen,
    202                                              PIPE_CAP_TGSI_TEX_TXF_LZ);
    203    ctx->has_txf = pipe->screen->get_param(pipe->screen,
    204                                           PIPE_CAP_GLSL_FEATURE_LEVEL) > 130;
    205    ctx->cube_as_2darray = pipe->screen->get_param(pipe->screen,
    206                                                   PIPE_CAP_SAMPLER_VIEW_TARGET);
    207 
    208    /* blend state objects */
    209    memset(&blend, 0, sizeof(blend));
    210 
    211    for (i = 0; i <= PIPE_MASK_RGBA; i++) {
    212       for (j = 0; j < 2; j++) {
    213          memset(&blend.rt[0], 0, sizeof(blend.rt[0]));
    214          blend.rt[0].colormask = i;
    215          if (j) {
    216             blend.rt[0].blend_enable = 1;
    217             blend.rt[0].rgb_func = PIPE_BLEND_ADD;
    218             blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;
    219             blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA;
    220             blend.rt[0].alpha_func = PIPE_BLEND_ADD;
    221             blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;
    222             blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA;
    223          }
    224          ctx->blend[i][j] = pipe->create_blend_state(pipe, &blend);
    225       }
    226    }
    227 
    228    /* depth stencil alpha state objects */
    229    memset(&dsa, 0, sizeof(dsa));
    230    ctx->dsa_keep_depth_stencil =
    231       pipe->create_depth_stencil_alpha_state(pipe, &dsa);
    232 
    233    dsa.depth.enabled = 1;
    234    dsa.depth.writemask = 1;
    235    dsa.depth.func = PIPE_FUNC_ALWAYS;
    236    ctx->dsa_write_depth_keep_stencil =
    237       pipe->create_depth_stencil_alpha_state(pipe, &dsa);
    238 
    239    dsa.stencil[0].enabled = 1;
    240    dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
    241    dsa.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
    242    dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
    243    dsa.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
    244    dsa.stencil[0].valuemask = 0xff;
    245    dsa.stencil[0].writemask = 0xff;
    246    ctx->dsa_write_depth_stencil =
    247       pipe->create_depth_stencil_alpha_state(pipe, &dsa);
    248 
    249    dsa.depth.enabled = 0;
    250    dsa.depth.writemask = 0;
    251    ctx->dsa_keep_depth_write_stencil =
    252       pipe->create_depth_stencil_alpha_state(pipe, &dsa);
    253 
    254    /* sampler state */
    255    memset(&sampler_state, 0, sizeof(sampler_state));
    256    sampler_state.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
    257    sampler_state.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
    258    sampler_state.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
    259    sampler_state.normalized_coords = 1;
    260    ctx->sampler_state = pipe->create_sampler_state(pipe, &sampler_state);
    261    sampler_state.normalized_coords = 0;
    262    ctx->sampler_state_rect = pipe->create_sampler_state(pipe, &sampler_state);
    263 
    264    sampler_state.min_img_filter = PIPE_TEX_FILTER_LINEAR;
    265    sampler_state.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
    266    sampler_state.normalized_coords = 1;
    267    ctx->sampler_state_linear = pipe->create_sampler_state(pipe, &sampler_state);
    268    sampler_state.normalized_coords = 0;
    269    ctx->sampler_state_rect_linear = pipe->create_sampler_state(pipe, &sampler_state);
    270 
    271    /* rasterizer state */
    272    memset(&rs_state, 0, sizeof(rs_state));
    273    rs_state.cull_face = PIPE_FACE_NONE;
    274    rs_state.half_pixel_center = 1;
    275    rs_state.bottom_edge_rule = 1;
    276    rs_state.flatshade = 1;
    277    rs_state.depth_clip = 1;
    278    ctx->rs_state = pipe->create_rasterizer_state(pipe, &rs_state);
    279 
    280    rs_state.scissor = 1;
    281    ctx->rs_state_scissor = pipe->create_rasterizer_state(pipe, &rs_state);
    282 
    283    if (ctx->has_stream_out) {
    284       rs_state.scissor = 0;
    285       rs_state.rasterizer_discard = 1;
    286       ctx->rs_discard_state = pipe->create_rasterizer_state(pipe, &rs_state);
    287    }
    288 
    289    ctx->base.cb_slot = 0; /* 0 for now */
    290    ctx->base.vb_slot = 0; /* 0 for now */
    291 
    292    /* vertex elements states */
    293    memset(&velem[0], 0, sizeof(velem[0]) * 2);
    294    for (i = 0; i < 2; i++) {
    295       velem[i].src_offset = i * 4 * sizeof(float);
    296       velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
    297       velem[i].vertex_buffer_index = ctx->base.vb_slot;
    298    }
    299    ctx->velem_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]);
    300 
    301    if (ctx->has_stream_out) {
    302       static enum pipe_format formats[4] = {
    303          PIPE_FORMAT_R32_UINT,
    304          PIPE_FORMAT_R32G32_UINT,
    305          PIPE_FORMAT_R32G32B32_UINT,
    306          PIPE_FORMAT_R32G32B32A32_UINT
    307       };
    308 
    309       for (i = 0; i < 4; i++) {
    310          velem[0].src_format = formats[i];
    311          velem[0].vertex_buffer_index = ctx->base.vb_slot;
    312          ctx->velem_state_readbuf[i] =
    313                pipe->create_vertex_elements_state(pipe, 1, &velem[0]);
    314       }
    315    }
    316 
    317    ctx->has_layered =
    318       pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_INSTANCEID) &&
    319       pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT);
    320 
    321    /* set invariant vertex coordinates */
    322    for (i = 0; i < 4; i++)
    323       ctx->vertices[i][0][3] = 1; /*v.w*/
    324 
    325    return &ctx->base;
    326 }
    327 
    328 static void bind_vs_pos_only(struct blitter_context_priv *ctx,
    329                              unsigned num_so_channels)
    330 {
    331    struct pipe_context *pipe = ctx->base.pipe;
    332    int index = num_so_channels ? num_so_channels - 1 : 0;
    333 
    334    if (!ctx->vs_pos_only[index]) {
    335       struct pipe_stream_output_info so;
    336       const uint semantic_names[] = { TGSI_SEMANTIC_POSITION };
    337       const uint semantic_indices[] = { 0 };
    338 
    339       memset(&so, 0, sizeof(so));
    340       so.num_outputs = 1;
    341       so.output[0].num_components = num_so_channels;
    342       so.stride[0] = num_so_channels;
    343 
    344       ctx->vs_pos_only[index] =
    345          util_make_vertex_passthrough_shader_with_so(pipe, 1, semantic_names,
    346                                                      semantic_indices, false,
    347                                                      false, &so);
    348    }
    349 
    350    pipe->bind_vs_state(pipe, ctx->vs_pos_only[index]);
    351 }
    352 
    353 static void *get_vs_passthrough_pos_generic(struct blitter_context *blitter)
    354 {
    355    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    356    struct pipe_context *pipe = ctx->base.pipe;
    357 
    358    if (!ctx->vs) {
    359       const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
    360                                       TGSI_SEMANTIC_GENERIC };
    361       const uint semantic_indices[] = { 0, 0 };
    362       ctx->vs =
    363          util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
    364                                              semantic_indices, false);
    365    }
    366    return ctx->vs;
    367 }
    368 
    369 static void *get_vs_passthrough_pos(struct blitter_context *blitter)
    370 {
    371    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    372    struct pipe_context *pipe = ctx->base.pipe;
    373 
    374    if (!ctx->vs_nogeneric) {
    375       const uint semantic_names[] = { TGSI_SEMANTIC_POSITION };
    376       const uint semantic_indices[] = { 0 };
    377 
    378       ctx->vs_nogeneric =
    379          util_make_vertex_passthrough_shader(pipe, 1,
    380                                              semantic_names,
    381                                              semantic_indices, false);
    382    }
    383    return ctx->vs_nogeneric;
    384 }
    385 
    386 static void *get_vs_layered(struct blitter_context *blitter)
    387 {
    388    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    389    struct pipe_context *pipe = ctx->base.pipe;
    390 
    391    if (!ctx->vs_layered) {
    392       ctx->vs_layered = util_make_layered_clear_vertex_shader(pipe);
    393    }
    394    return ctx->vs_layered;
    395 }
    396 
    397 static void bind_fs_empty(struct blitter_context_priv *ctx)
    398 {
    399    struct pipe_context *pipe = ctx->base.pipe;
    400 
    401    if (!ctx->fs_empty) {
    402       assert(!ctx->cached_all_shaders);
    403       ctx->fs_empty = util_make_empty_fragment_shader(pipe);
    404    }
    405 
    406    ctx->bind_fs_state(pipe, ctx->fs_empty);
    407 }
    408 
    409 static void bind_fs_write_one_cbuf(struct blitter_context_priv *ctx)
    410 {
    411    struct pipe_context *pipe = ctx->base.pipe;
    412 
    413    if (!ctx->fs_write_one_cbuf) {
    414       assert(!ctx->cached_all_shaders);
    415       ctx->fs_write_one_cbuf =
    416          util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
    417                                                TGSI_INTERPOLATE_CONSTANT, false);
    418    }
    419 
    420    ctx->bind_fs_state(pipe, ctx->fs_write_one_cbuf);
    421 }
    422 
    423 static void bind_fs_write_all_cbufs(struct blitter_context_priv *ctx)
    424 {
    425    struct pipe_context *pipe = ctx->base.pipe;
    426 
    427    if (!ctx->fs_write_all_cbufs) {
    428       assert(!ctx->cached_all_shaders);
    429       ctx->fs_write_all_cbufs =
    430          util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
    431                                                TGSI_INTERPOLATE_CONSTANT, true);
    432    }
    433 
    434    ctx->bind_fs_state(pipe, ctx->fs_write_all_cbufs);
    435 }
    436 
    437 void util_blitter_destroy(struct blitter_context *blitter)
    438 {
    439    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    440    struct pipe_context *pipe = blitter->pipe;
    441    unsigned i, j, f;
    442 
    443    for (i = 0; i <= PIPE_MASK_RGBA; i++)
    444       for (j = 0; j < 2; j++)
    445          pipe->delete_blend_state(pipe, ctx->blend[i][j]);
    446 
    447    for (i = 0; i < ARRAY_SIZE(ctx->blend_clear); i++) {
    448       if (ctx->blend_clear[i])
    449          pipe->delete_blend_state(pipe, ctx->blend_clear[i]);
    450    }
    451    pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
    452    pipe->delete_depth_stencil_alpha_state(pipe,
    453                                           ctx->dsa_write_depth_keep_stencil);
    454    pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
    455    pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
    456 
    457    pipe->delete_rasterizer_state(pipe, ctx->rs_state);
    458    pipe->delete_rasterizer_state(pipe, ctx->rs_state_scissor);
    459    if (ctx->rs_discard_state)
    460       pipe->delete_rasterizer_state(pipe, ctx->rs_discard_state);
    461    if (ctx->vs)
    462       pipe->delete_vs_state(pipe, ctx->vs);
    463    if (ctx->vs_nogeneric)
    464       pipe->delete_vs_state(pipe, ctx->vs_nogeneric);
    465    for (i = 0; i < 4; i++)
    466       if (ctx->vs_pos_only[i])
    467          pipe->delete_vs_state(pipe, ctx->vs_pos_only[i]);
    468    if (ctx->vs_layered)
    469       pipe->delete_vs_state(pipe, ctx->vs_layered);
    470    pipe->delete_vertex_elements_state(pipe, ctx->velem_state);
    471    for (i = 0; i < 4; i++) {
    472       if (ctx->velem_state_readbuf[i]) {
    473          pipe->delete_vertex_elements_state(pipe, ctx->velem_state_readbuf[i]);
    474       }
    475    }
    476 
    477    for (i = 0; i < PIPE_MAX_TEXTURE_TYPES; i++) {
    478       for (unsigned type = 0; type < ARRAY_SIZE(ctx->fs_texfetch_col); ++type) {
    479          for (unsigned inst = 0; inst < 2; inst++) {
    480             if (ctx->fs_texfetch_col[type][i][inst])
    481                ctx->delete_fs_state(pipe, ctx->fs_texfetch_col[type][i][inst]);
    482          }
    483          if (ctx->fs_texfetch_col_msaa[type][i])
    484             ctx->delete_fs_state(pipe, ctx->fs_texfetch_col_msaa[type][i]);
    485       }
    486 
    487       for (unsigned inst = 0; inst < 2; inst++) {
    488          if (ctx->fs_texfetch_depth[i][inst])
    489             ctx->delete_fs_state(pipe, ctx->fs_texfetch_depth[i][inst]);
    490          if (ctx->fs_texfetch_depthstencil[i][inst])
    491             ctx->delete_fs_state(pipe, ctx->fs_texfetch_depthstencil[i][inst]);
    492          if (ctx->fs_texfetch_stencil[i][inst])
    493             ctx->delete_fs_state(pipe, ctx->fs_texfetch_stencil[i][inst]);
    494       }
    495 
    496       if (ctx->fs_texfetch_depth_msaa[i])
    497          ctx->delete_fs_state(pipe, ctx->fs_texfetch_depth_msaa[i]);
    498       if (ctx->fs_texfetch_depthstencil_msaa[i])
    499          ctx->delete_fs_state(pipe, ctx->fs_texfetch_depthstencil_msaa[i]);
    500       if (ctx->fs_texfetch_stencil_msaa[i])
    501          ctx->delete_fs_state(pipe, ctx->fs_texfetch_stencil_msaa[i]);
    502 
    503       for (j = 0; j< ARRAY_SIZE(ctx->fs_resolve[i]); j++)
    504          for (f = 0; f < 2; f++)
    505             if (ctx->fs_resolve[i][j][f])
    506                ctx->delete_fs_state(pipe, ctx->fs_resolve[i][j][f]);
    507    }
    508 
    509    if (ctx->fs_empty)
    510       ctx->delete_fs_state(pipe, ctx->fs_empty);
    511    if (ctx->fs_write_one_cbuf)
    512       ctx->delete_fs_state(pipe, ctx->fs_write_one_cbuf);
    513    if (ctx->fs_write_all_cbufs)
    514       ctx->delete_fs_state(pipe, ctx->fs_write_all_cbufs);
    515 
    516    pipe->delete_sampler_state(pipe, ctx->sampler_state_rect_linear);
    517    pipe->delete_sampler_state(pipe, ctx->sampler_state_rect);
    518    pipe->delete_sampler_state(pipe, ctx->sampler_state_linear);
    519    pipe->delete_sampler_state(pipe, ctx->sampler_state);
    520    FREE(ctx);
    521 }
    522 
    523 void util_blitter_set_texture_multisample(struct blitter_context *blitter,
    524                                           bool supported)
    525 {
    526    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    527 
    528    ctx->has_texture_multisample = supported;
    529 }
    530 
    531 void util_blitter_set_running_flag(struct blitter_context *blitter)
    532 {
    533    if (blitter->running) {
    534       _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
    535                     __LINE__);
    536    }
    537    blitter->running = true;
    538 
    539    blitter->pipe->set_active_query_state(blitter->pipe, false);
    540 }
    541 
    542 void util_blitter_unset_running_flag(struct blitter_context *blitter)
    543 {
    544    if (!blitter->running) {
    545       _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
    546                     __LINE__);
    547    }
    548    blitter->running = false;
    549 
    550    blitter->pipe->set_active_query_state(blitter->pipe, true);
    551 }
    552 
    553 static void blitter_check_saved_vertex_states(MAYBE_UNUSED struct blitter_context_priv *ctx)
    554 {
    555    assert(ctx->base.saved_vs != INVALID_PTR);
    556    assert(!ctx->has_geometry_shader || ctx->base.saved_gs != INVALID_PTR);
    557    assert(!ctx->has_tessellation || ctx->base.saved_tcs != INVALID_PTR);
    558    assert(!ctx->has_tessellation || ctx->base.saved_tes != INVALID_PTR);
    559    assert(!ctx->has_stream_out || ctx->base.saved_num_so_targets != ~0u);
    560    assert(ctx->base.saved_rs_state != INVALID_PTR);
    561 }
    562 
    563 void util_blitter_restore_vertex_states(struct blitter_context *blitter)
    564 {
    565    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    566    struct pipe_context *pipe = ctx->base.pipe;
    567    unsigned i;
    568 
    569    /* Vertex buffer. */
    570    if (ctx->base.saved_vertex_buffer.buffer.resource) {
    571       pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1,
    572                                &ctx->base.saved_vertex_buffer);
    573       pipe_vertex_buffer_unreference(&ctx->base.saved_vertex_buffer);
    574    }
    575 
    576    /* Vertex elements. */
    577    if (ctx->base.saved_velem_state != INVALID_PTR) {
    578       pipe->bind_vertex_elements_state(pipe, ctx->base.saved_velem_state);
    579       ctx->base.saved_velem_state = INVALID_PTR;
    580    }
    581 
    582    /* Vertex shader. */
    583    pipe->bind_vs_state(pipe, ctx->base.saved_vs);
    584    ctx->base.saved_vs = INVALID_PTR;
    585 
    586    /* Geometry shader. */
    587    if (ctx->has_geometry_shader) {
    588       pipe->bind_gs_state(pipe, ctx->base.saved_gs);
    589       ctx->base.saved_gs = INVALID_PTR;
    590    }
    591 
    592    if (ctx->has_tessellation) {
    593       pipe->bind_tcs_state(pipe, ctx->base.saved_tcs);
    594       pipe->bind_tes_state(pipe, ctx->base.saved_tes);
    595       ctx->base.saved_tcs = INVALID_PTR;
    596       ctx->base.saved_tes = INVALID_PTR;
    597    }
    598 
    599    /* Stream outputs. */
    600    if (ctx->has_stream_out) {
    601       unsigned offsets[PIPE_MAX_SO_BUFFERS];
    602       for (i = 0; i < ctx->base.saved_num_so_targets; i++)
    603          offsets[i] = (unsigned)-1;
    604       pipe->set_stream_output_targets(pipe,
    605                                       ctx->base.saved_num_so_targets,
    606                                       ctx->base.saved_so_targets, offsets);
    607 
    608       for (i = 0; i < ctx->base.saved_num_so_targets; i++)
    609          pipe_so_target_reference(&ctx->base.saved_so_targets[i], NULL);
    610 
    611       ctx->base.saved_num_so_targets = ~0;
    612    }
    613 
    614    /* Rasterizer. */
    615    pipe->bind_rasterizer_state(pipe, ctx->base.saved_rs_state);
    616    ctx->base.saved_rs_state = INVALID_PTR;
    617 }
    618 
    619 static void blitter_check_saved_fragment_states(MAYBE_UNUSED struct blitter_context_priv *ctx)
    620 {
    621    assert(ctx->base.saved_fs != INVALID_PTR);
    622    assert(ctx->base.saved_dsa_state != INVALID_PTR);
    623    assert(ctx->base.saved_blend_state != INVALID_PTR);
    624 }
    625 
    626 void util_blitter_restore_fragment_states(struct blitter_context *blitter)
    627 {
    628    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    629    struct pipe_context *pipe = ctx->base.pipe;
    630 
    631    /* Fragment shader. */
    632    ctx->bind_fs_state(pipe, ctx->base.saved_fs);
    633    ctx->base.saved_fs = INVALID_PTR;
    634 
    635    /* Depth, stencil, alpha. */
    636    pipe->bind_depth_stencil_alpha_state(pipe, ctx->base.saved_dsa_state);
    637    ctx->base.saved_dsa_state = INVALID_PTR;
    638 
    639    /* Blend state. */
    640    pipe->bind_blend_state(pipe, ctx->base.saved_blend_state);
    641    ctx->base.saved_blend_state = INVALID_PTR;
    642 
    643    /* Sample mask. */
    644    if (ctx->base.is_sample_mask_saved) {
    645       pipe->set_sample_mask(pipe, ctx->base.saved_sample_mask);
    646       ctx->base.is_sample_mask_saved = false;
    647    }
    648 
    649    /* Miscellaneous states. */
    650    /* XXX check whether these are saved and whether they need to be restored
    651     * (depending on the operation) */
    652    pipe->set_stencil_ref(pipe, &ctx->base.saved_stencil_ref);
    653 
    654    if (!blitter->skip_viewport_restore)
    655       pipe->set_viewport_states(pipe, 0, 1, &ctx->base.saved_viewport);
    656 }
    657 
    658 static void blitter_check_saved_fb_state(MAYBE_UNUSED struct blitter_context_priv *ctx)
    659 {
    660    assert(ctx->base.saved_fb_state.nr_cbufs != ~0u);
    661 }
    662 
    663 static void blitter_disable_render_cond(struct blitter_context_priv *ctx)
    664 {
    665    struct pipe_context *pipe = ctx->base.pipe;
    666 
    667    if (ctx->base.saved_render_cond_query) {
    668       pipe->render_condition(pipe, NULL, false, 0);
    669    }
    670 }
    671 
    672 void util_blitter_restore_render_cond(struct blitter_context *blitter)
    673 {
    674    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    675    struct pipe_context *pipe = ctx->base.pipe;
    676 
    677    if (ctx->base.saved_render_cond_query) {
    678       pipe->render_condition(pipe, ctx->base.saved_render_cond_query,
    679                              ctx->base.saved_render_cond_cond,
    680                              ctx->base.saved_render_cond_mode);
    681       ctx->base.saved_render_cond_query = NULL;
    682    }
    683 }
    684 
    685 void util_blitter_restore_fb_state(struct blitter_context *blitter)
    686 {
    687    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    688    struct pipe_context *pipe = ctx->base.pipe;
    689 
    690    pipe->set_framebuffer_state(pipe, &ctx->base.saved_fb_state);
    691    util_unreference_framebuffer_state(&ctx->base.saved_fb_state);
    692 }
    693 
    694 static void blitter_check_saved_textures(MAYBE_UNUSED struct blitter_context_priv *ctx)
    695 {
    696    assert(ctx->base.saved_num_sampler_states != ~0u);
    697    assert(ctx->base.saved_num_sampler_views != ~0u);
    698 }
    699 
    700 void util_blitter_restore_textures(struct blitter_context *blitter)
    701 {
    702    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    703    struct pipe_context *pipe = ctx->base.pipe;
    704    unsigned i;
    705 
    706    /* Fragment sampler states. */
    707    pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
    708                              ctx->base.saved_num_sampler_states,
    709                              ctx->base.saved_sampler_states);
    710 
    711    ctx->base.saved_num_sampler_states = ~0;
    712 
    713    /* Fragment sampler views. */
    714    pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,
    715                            ctx->base.saved_num_sampler_views,
    716                            ctx->base.saved_sampler_views);
    717 
    718    for (i = 0; i < ctx->base.saved_num_sampler_views; i++)
    719       pipe_sampler_view_reference(&ctx->base.saved_sampler_views[i], NULL);
    720 
    721    ctx->base.saved_num_sampler_views = ~0;
    722 }
    723 
    724 void util_blitter_restore_constant_buffer_state(struct blitter_context *blitter)
    725 {
    726    struct pipe_context *pipe = blitter->pipe;
    727 
    728    pipe->set_constant_buffer(pipe, PIPE_SHADER_FRAGMENT, blitter->cb_slot,
    729                             &blitter->saved_fs_constant_buffer);
    730    pipe_resource_reference(&blitter->saved_fs_constant_buffer.buffer, NULL);
    731 }
    732 
    733 static void blitter_set_rectangle(struct blitter_context_priv *ctx,
    734                                   int x1, int y1, int x2, int y2,
    735                                   float depth)
    736 {
    737    int i;
    738 
    739    /* set vertex positions */
    740    ctx->vertices[0][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v0.x*/
    741    ctx->vertices[0][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v0.y*/
    742 
    743    ctx->vertices[1][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v1.x*/
    744    ctx->vertices[1][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v1.y*/
    745 
    746    ctx->vertices[2][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v2.x*/
    747    ctx->vertices[2][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v2.y*/
    748 
    749    ctx->vertices[3][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v3.x*/
    750    ctx->vertices[3][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v3.y*/
    751 
    752    for (i = 0; i < 4; i++)
    753       ctx->vertices[i][0][2] = depth; /*z*/
    754 
    755    /* viewport */
    756    struct pipe_viewport_state viewport;
    757    viewport.scale[0] = 0.5f * ctx->dst_width;
    758    viewport.scale[1] = 0.5f * ctx->dst_height;
    759    viewport.scale[2] = 1.0f;
    760    viewport.translate[0] = 0.5f * ctx->dst_width;
    761    viewport.translate[1] = 0.5f * ctx->dst_height;
    762    viewport.translate[2] = 0.0f;
    763    ctx->base.pipe->set_viewport_states(ctx->base.pipe, 0, 1, &viewport);
    764 }
    765 
    766 static void blitter_set_clear_color(struct blitter_context_priv *ctx,
    767                                     const float color[4])
    768 {
    769    int i;
    770 
    771    if (color) {
    772       for (i = 0; i < 4; i++)
    773          memcpy(&ctx->vertices[i][1][0], color, sizeof(uint32_t) * 4);
    774    } else {
    775       for (i = 0; i < 4; i++)
    776          memset(&ctx->vertices[i][1][0], 0, sizeof(uint32_t) * 4);
    777    }
    778 }
    779 
    780 static void get_texcoords(struct pipe_sampler_view *src,
    781                           unsigned src_width0, unsigned src_height0,
    782                           int x1, int y1, int x2, int y2,
    783                           float layer, unsigned sample,
    784                           bool uses_txf, union blitter_attrib *out)
    785 {
    786    unsigned level = src->u.tex.first_level;
    787    bool normalized = !uses_txf &&
    788                         src->target != PIPE_TEXTURE_RECT &&
    789                         src->texture->nr_samples <= 1;
    790 
    791    if (normalized) {
    792       out->texcoord.x1 = x1 / (float)u_minify(src_width0,  level);
    793       out->texcoord.y1 = y1 / (float)u_minify(src_height0, level);
    794       out->texcoord.x2 = x2 / (float)u_minify(src_width0,  level);
    795       out->texcoord.y2 = y2 / (float)u_minify(src_height0, level);
    796    } else {
    797       out->texcoord.x1 = x1;
    798       out->texcoord.y1 = y1;
    799       out->texcoord.x2 = x2;
    800       out->texcoord.y2 = y2;
    801    }
    802 
    803    out->texcoord.z = 0;
    804    out->texcoord.w = 0;
    805 
    806    /* Set the layer. */
    807    switch (src->target) {
    808    case PIPE_TEXTURE_3D:
    809       {
    810          float r = layer;
    811 
    812          if (!uses_txf)
    813             r /= u_minify(src->texture->depth0, src->u.tex.first_level);
    814 
    815          out->texcoord.z = r;
    816       }
    817       break;
    818 
    819    case PIPE_TEXTURE_1D_ARRAY:
    820       out->texcoord.y1 = out->texcoord.y2 = layer;
    821       break;
    822 
    823    case PIPE_TEXTURE_2D_ARRAY:
    824       out->texcoord.z = layer;
    825       out->texcoord.w = sample;
    826       break;
    827 
    828    case PIPE_TEXTURE_CUBE_ARRAY:
    829       out->texcoord.w = (unsigned)layer / 6;
    830       break;
    831 
    832    case PIPE_TEXTURE_2D:
    833       out->texcoord.w = sample;
    834       break;
    835 
    836    default:;
    837    }
    838 }
    839 
    840 static void blitter_set_dst_dimensions(struct blitter_context_priv *ctx,
    841                                        unsigned width, unsigned height)
    842 {
    843    ctx->dst_width = width;
    844    ctx->dst_height = height;
    845 }
    846 
    847 static void set_texcoords_in_vertices(const union blitter_attrib *attrib,
    848                                       float *out, unsigned stride)
    849 {
    850    out[0] = attrib->texcoord.x1;
    851    out[1] = attrib->texcoord.y1;
    852    out += stride;
    853    out[0] = attrib->texcoord.x2;
    854    out[1] = attrib->texcoord.y1;
    855    out += stride;
    856    out[0] = attrib->texcoord.x2;
    857    out[1] = attrib->texcoord.y2;
    858    out += stride;
    859    out[0] = attrib->texcoord.x1;
    860    out[1] = attrib->texcoord.y2;
    861 }
    862 
    863 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx,
    864                                          enum pipe_format src_format,
    865                                          enum pipe_format dst_format,
    866                                          enum pipe_texture_target target,
    867                                          unsigned src_nr_samples,
    868                                          unsigned dst_nr_samples,
    869                                          unsigned filter,
    870                                          bool use_txf)
    871 {
    872    struct pipe_context *pipe = ctx->base.pipe;
    873    enum tgsi_texture_type tgsi_tex =
    874       util_pipe_tex_to_tgsi_tex(target, src_nr_samples);
    875    enum tgsi_return_type stype;
    876    enum tgsi_return_type dtype;
    877    unsigned type;
    878 
    879    assert(target < PIPE_MAX_TEXTURE_TYPES);
    880 
    881    if (util_format_is_pure_uint(src_format)) {
    882       stype = TGSI_RETURN_TYPE_UINT;
    883       if (util_format_is_pure_uint(dst_format)) {
    884          dtype = TGSI_RETURN_TYPE_UINT;
    885          type = 0;
    886       } else {
    887          assert(util_format_is_pure_sint(dst_format));
    888          dtype = TGSI_RETURN_TYPE_SINT;
    889          type = 1;
    890       }
    891    } else if (util_format_is_pure_sint(src_format)) {
    892       stype = TGSI_RETURN_TYPE_SINT;
    893       if (util_format_is_pure_sint(dst_format)) {
    894          dtype = TGSI_RETURN_TYPE_SINT;
    895          type = 2;
    896       } else {
    897          assert(util_format_is_pure_uint(dst_format));
    898          dtype = TGSI_RETURN_TYPE_UINT;
    899          type = 3;
    900       }
    901    } else {
    902       assert(!util_format_is_pure_uint(dst_format) &&
    903              !util_format_is_pure_sint(dst_format));
    904       dtype = stype = TGSI_RETURN_TYPE_FLOAT;
    905       type = 4;
    906    }
    907 
    908    if (src_nr_samples > 1) {
    909       void **shader;
    910 
    911       /* OpenGL requires that integer textures just copy 1 sample instead
    912        * of averaging.
    913        */
    914       if (dst_nr_samples <= 1 &&
    915           stype != TGSI_RETURN_TYPE_UINT &&
    916           stype != TGSI_RETURN_TYPE_SINT) {
    917          /* The destination has one sample, so we'll do color resolve. */
    918          unsigned index = GET_MSAA_RESOLVE_FS_IDX(src_nr_samples);
    919 
    920          assert(filter < 2);
    921 
    922          shader = &ctx->fs_resolve[target][index][filter];
    923 
    924          if (!*shader) {
    925             assert(!ctx->cached_all_shaders);
    926             if (filter == PIPE_TEX_FILTER_LINEAR) {
    927                *shader = util_make_fs_msaa_resolve_bilinear(pipe, tgsi_tex,
    928                                                    src_nr_samples,
    929                                                    stype);
    930             }
    931             else {
    932                *shader = util_make_fs_msaa_resolve(pipe, tgsi_tex,
    933                                                    src_nr_samples,
    934                                                    stype);
    935             }
    936          }
    937       }
    938       else {
    939          /* The destination has multiple samples, we'll do
    940           * an MSAA->MSAA copy.
    941           */
    942          shader = &ctx->fs_texfetch_col_msaa[type][target];
    943 
    944          /* Create the fragment shader on-demand. */
    945          if (!*shader) {
    946             assert(!ctx->cached_all_shaders);
    947             *shader = util_make_fs_blit_msaa_color(pipe, tgsi_tex, stype, dtype);
    948          }
    949       }
    950 
    951       return *shader;
    952    } else {
    953       void **shader;
    954 
    955       if (use_txf)
    956          shader = &ctx->fs_texfetch_col[type][target][1];
    957       else
    958          shader = &ctx->fs_texfetch_col[type][target][0];
    959 
    960       /* Create the fragment shader on-demand. */
    961       if (!*shader) {
    962          assert(!ctx->cached_all_shaders);
    963          *shader = util_make_fragment_tex_shader(pipe, tgsi_tex,
    964                                                  TGSI_INTERPOLATE_LINEAR,
    965                                                  stype, dtype,
    966                                                  ctx->has_tex_lz, use_txf);
    967       }
    968 
    969       return *shader;
    970    }
    971 }
    972 
    973 static inline
    974 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx,
    975                                     enum pipe_texture_target target,
    976                                     unsigned nr_samples,
    977                                     bool use_txf)
    978 {
    979    struct pipe_context *pipe = ctx->base.pipe;
    980 
    981    assert(target < PIPE_MAX_TEXTURE_TYPES);
    982 
    983    if (nr_samples > 1) {
    984       void **shader = &ctx->fs_texfetch_depth_msaa[target];
    985 
    986       /* Create the fragment shader on-demand. */
    987       if (!*shader) {
    988          enum tgsi_texture_type tgsi_tex;
    989          assert(!ctx->cached_all_shaders);
    990          tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
    991          *shader = util_make_fs_blit_msaa_depth(pipe, tgsi_tex);
    992       }
    993 
    994       return *shader;
    995    } else {
    996       void **shader;
    997 
    998       if (use_txf)
    999          shader = &ctx->fs_texfetch_depth[target][1];
   1000       else
   1001          shader = &ctx->fs_texfetch_depth[target][0];
   1002 
   1003       /* Create the fragment shader on-demand. */
   1004       if (!*shader) {
   1005          enum tgsi_texture_type tgsi_tex;
   1006          assert(!ctx->cached_all_shaders);
   1007          tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
   1008          *shader =
   1009             util_make_fragment_tex_shader_writedepth(pipe, tgsi_tex,
   1010                                                      TGSI_INTERPOLATE_LINEAR,
   1011                                                      ctx->has_tex_lz, use_txf);
   1012       }
   1013 
   1014       return *shader;
   1015    }
   1016 }
   1017 
   1018 static inline
   1019 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv *ctx,
   1020                                            enum pipe_texture_target target,
   1021                                            unsigned nr_samples,
   1022                                            bool use_txf)
   1023 {
   1024    struct pipe_context *pipe = ctx->base.pipe;
   1025 
   1026    assert(target < PIPE_MAX_TEXTURE_TYPES);
   1027 
   1028    if (nr_samples > 1) {
   1029       void **shader = &ctx->fs_texfetch_depthstencil_msaa[target];
   1030 
   1031       /* Create the fragment shader on-demand. */
   1032       if (!*shader) {
   1033          enum tgsi_texture_type tgsi_tex;
   1034          assert(!ctx->cached_all_shaders);
   1035          tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
   1036          *shader = util_make_fs_blit_msaa_depthstencil(pipe, tgsi_tex);
   1037       }
   1038 
   1039       return *shader;
   1040    } else {
   1041       void **shader;
   1042 
   1043       if (use_txf)
   1044          shader = &ctx->fs_texfetch_depthstencil[target][1];
   1045       else
   1046          shader = &ctx->fs_texfetch_depthstencil[target][0];
   1047 
   1048       /* Create the fragment shader on-demand. */
   1049       if (!*shader) {
   1050          enum tgsi_texture_type tgsi_tex;
   1051          assert(!ctx->cached_all_shaders);
   1052          tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
   1053          *shader =
   1054             util_make_fragment_tex_shader_writedepthstencil(pipe, tgsi_tex,
   1055                                                             TGSI_INTERPOLATE_LINEAR,
   1056                                                             ctx->has_tex_lz,
   1057                                                             use_txf);
   1058       }
   1059 
   1060       return *shader;
   1061    }
   1062 }
   1063 
   1064 static inline
   1065 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv *ctx,
   1066                                       enum pipe_texture_target target,
   1067                                       unsigned nr_samples,
   1068                                       bool use_txf)
   1069 {
   1070    struct pipe_context *pipe = ctx->base.pipe;
   1071 
   1072    assert(target < PIPE_MAX_TEXTURE_TYPES);
   1073 
   1074    if (nr_samples > 1) {
   1075       void **shader = &ctx->fs_texfetch_stencil_msaa[target];
   1076 
   1077       /* Create the fragment shader on-demand. */
   1078       if (!*shader) {
   1079          enum tgsi_texture_type tgsi_tex;
   1080          assert(!ctx->cached_all_shaders);
   1081          tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
   1082          *shader = util_make_fs_blit_msaa_stencil(pipe, tgsi_tex);
   1083       }
   1084 
   1085       return *shader;
   1086    } else {
   1087       void **shader;
   1088 
   1089       if (use_txf)
   1090          shader = &ctx->fs_texfetch_stencil[target][1];
   1091       else
   1092          shader = &ctx->fs_texfetch_stencil[target][0];
   1093 
   1094       /* Create the fragment shader on-demand. */
   1095       if (!*shader) {
   1096          enum tgsi_texture_type tgsi_tex;
   1097          assert(!ctx->cached_all_shaders);
   1098          tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
   1099          *shader =
   1100             util_make_fragment_tex_shader_writestencil(pipe, tgsi_tex,
   1101                                                        TGSI_INTERPOLATE_LINEAR,
   1102                                                        ctx->has_tex_lz, use_txf);
   1103       }
   1104 
   1105       return *shader;
   1106    }
   1107 }
   1108 
   1109 
   1110 /**
   1111  * Generate and save all fragment shaders that we will ever need for
   1112  * blitting.  Drivers which use the 'draw' fallbacks will typically use
   1113  * this to make sure we generate/use shaders that don't go through the
   1114  * draw module's wrapper functions.
   1115  */
   1116 void util_blitter_cache_all_shaders(struct blitter_context *blitter)
   1117 {
   1118    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
   1119    struct pipe_context *pipe = blitter->pipe;
   1120    struct pipe_screen *screen = pipe->screen;
   1121    unsigned samples, j, f, target, max_samples, use_txf;
   1122    bool has_arraytex, has_cubearraytex;
   1123 
   1124    max_samples = ctx->has_texture_multisample ? 2 : 1;
   1125    has_arraytex = screen->get_param(screen,
   1126                                     PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS) != 0;
   1127    has_cubearraytex = screen->get_param(screen,
   1128                                     PIPE_CAP_CUBE_MAP_ARRAY) != 0;
   1129 
   1130    /* It only matters if i <= 1 or > 1. */
   1131    for (samples = 1; samples <= max_samples; samples++) {
   1132       for (target = PIPE_TEXTURE_1D; target < PIPE_MAX_TEXTURE_TYPES; target++) {
   1133          for (use_txf = 0; use_txf <= ctx->has_txf; use_txf++) {
   1134             if (!has_arraytex &&
   1135                 (target == PIPE_TEXTURE_1D_ARRAY ||
   1136                  target == PIPE_TEXTURE_2D_ARRAY)) {
   1137                continue;
   1138             }
   1139             if (!has_cubearraytex &&
   1140                 (target == PIPE_TEXTURE_CUBE_ARRAY))
   1141                continue;
   1142 
   1143             if (samples > 1 &&
   1144                 (target != PIPE_TEXTURE_2D &&
   1145                  target != PIPE_TEXTURE_2D_ARRAY))
   1146                continue;
   1147 
   1148             if (samples > 1 && use_txf)
   1149                continue; /* TXF is the only option, use_txf has no effect */
   1150 
   1151             /* If samples == 1, the shaders read one texel. If samples >= 1,
   1152              * they read one sample.
   1153              */
   1154             blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT,
   1155                                         PIPE_FORMAT_R32_FLOAT, target,
   1156                                         samples, samples, 0, use_txf);
   1157             blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT,
   1158                                         PIPE_FORMAT_R32_UINT, target,
   1159                                         samples, samples, 0, use_txf);
   1160             blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT,
   1161                                         PIPE_FORMAT_R32_SINT, target,
   1162                                         samples, samples, 0, use_txf);
   1163             blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT,
   1164                                         PIPE_FORMAT_R32_SINT, target,
   1165                                         samples, samples, 0, use_txf);
   1166             blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT,
   1167                                         PIPE_FORMAT_R32_UINT, target,
   1168                                         samples, samples, 0, use_txf);
   1169             blitter_get_fs_texfetch_depth(ctx, target, samples, use_txf);
   1170             if (ctx->has_stencil_export) {
   1171                blitter_get_fs_texfetch_depthstencil(ctx, target, samples, use_txf);
   1172                blitter_get_fs_texfetch_stencil(ctx, target, samples, use_txf);
   1173             }
   1174 
   1175             if (samples == 1)
   1176                continue;
   1177 
   1178             /* MSAA resolve shaders. */
   1179             for (j = 2; j < 32; j++) {
   1180                if (!screen->is_format_supported(screen, PIPE_FORMAT_R32_FLOAT,
   1181                                                 target, j,
   1182                                                 PIPE_BIND_SAMPLER_VIEW)) {
   1183                   continue;
   1184                }
   1185 
   1186                for (f = 0; f < 2; f++) {
   1187                   if (f != PIPE_TEX_FILTER_NEAREST && use_txf)
   1188                      continue;
   1189 
   1190                   blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT,
   1191                                               PIPE_FORMAT_R32_FLOAT, target,
   1192                                               j, 1, f, use_txf);
   1193                   blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT,
   1194                                               PIPE_FORMAT_R32_UINT, target,
   1195                                               j, 1, f, use_txf);
   1196                   blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT,
   1197                                               PIPE_FORMAT_R32_SINT, target,
   1198                                               j, 1, f, use_txf);
   1199                }
   1200             }
   1201          }
   1202       }
   1203    }
   1204 
   1205    ctx->fs_empty = util_make_empty_fragment_shader(pipe);
   1206 
   1207    ctx->fs_write_one_cbuf =
   1208       util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
   1209                                             TGSI_INTERPOLATE_CONSTANT, false);
   1210 
   1211    ctx->fs_write_all_cbufs =
   1212       util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
   1213                                             TGSI_INTERPOLATE_CONSTANT, true);
   1214 
   1215    ctx->cached_all_shaders = true;
   1216 }
   1217 
   1218 static void blitter_set_common_draw_rect_state(struct blitter_context_priv *ctx,
   1219                                                bool scissor)
   1220 {
   1221    struct pipe_context *pipe = ctx->base.pipe;
   1222 
   1223    pipe->bind_rasterizer_state(pipe, scissor ? ctx->rs_state_scissor
   1224                                              : ctx->rs_state);
   1225    if (ctx->has_geometry_shader)
   1226       pipe->bind_gs_state(pipe, NULL);
   1227    if (ctx->has_tessellation) {
   1228       pipe->bind_tcs_state(pipe, NULL);
   1229       pipe->bind_tes_state(pipe, NULL);
   1230    }
   1231    if (ctx->has_stream_out)
   1232       pipe->set_stream_output_targets(pipe, 0, NULL, NULL);
   1233 }
   1234 
   1235 static void blitter_draw(struct blitter_context_priv *ctx,
   1236                          void *vertex_elements_cso,
   1237                          blitter_get_vs_func get_vs,
   1238                          int x1, int y1, int x2, int y2, float depth,
   1239                          unsigned num_instances)
   1240 {
   1241    struct pipe_context *pipe = ctx->base.pipe;
   1242    struct pipe_vertex_buffer vb = {0};
   1243 
   1244    blitter_set_rectangle(ctx, x1, y1, x2, y2, depth);
   1245 
   1246    vb.stride = 8 * sizeof(float);
   1247 
   1248    u_upload_data(pipe->stream_uploader, 0, sizeof(ctx->vertices), 4, ctx->vertices,
   1249                  &vb.buffer_offset, &vb.buffer.resource);
   1250    if (!vb.buffer.resource)
   1251       return;
   1252    u_upload_unmap(pipe->stream_uploader);
   1253 
   1254    pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
   1255    pipe->bind_vertex_elements_state(pipe, vertex_elements_cso);
   1256    pipe->bind_vs_state(pipe, get_vs(&ctx->base));
   1257    util_draw_arrays_instanced(pipe, PIPE_PRIM_TRIANGLE_FAN, 0, 4,
   1258                               0, num_instances);
   1259    pipe_resource_reference(&vb.buffer.resource, NULL);
   1260 }
   1261 
   1262 void util_blitter_draw_rectangle(struct blitter_context *blitter,
   1263                                  void *vertex_elements_cso,
   1264                                  blitter_get_vs_func get_vs,
   1265                                  int x1, int y1, int x2, int y2,
   1266                                  float depth, unsigned num_instances,
   1267                                  enum blitter_attrib_type type,
   1268                                  const union blitter_attrib *attrib)
   1269 {
   1270    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
   1271    unsigned i;
   1272 
   1273    switch (type) {
   1274       case UTIL_BLITTER_ATTRIB_COLOR:
   1275          blitter_set_clear_color(ctx, attrib->color);
   1276          break;
   1277 
   1278       case UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW:
   1279          for (i = 0; i < 4; i++) {
   1280             ctx->vertices[i][1][2] = attrib->texcoord.z;
   1281             ctx->vertices[i][1][3] = attrib->texcoord.w;
   1282          }
   1283          /* fall through */
   1284       case UTIL_BLITTER_ATTRIB_TEXCOORD_XY:
   1285          set_texcoords_in_vertices(attrib, &ctx->vertices[0][1][0], 8);
   1286          break;
   1287 
   1288       default:;
   1289    }
   1290 
   1291    blitter_draw(ctx, vertex_elements_cso, get_vs, x1, y1, x2, y2, depth,
   1292                 num_instances);
   1293 }
   1294 
   1295 static void *get_clear_blend_state(struct blitter_context_priv *ctx,
   1296                                    unsigned clear_buffers)
   1297 {
   1298    struct pipe_context *pipe = ctx->base.pipe;
   1299    int index;
   1300 
   1301    clear_buffers &= PIPE_CLEAR_COLOR;
   1302 
   1303    /* Return an existing blend state. */
   1304    if (!clear_buffers)
   1305       return ctx->blend[0][0];
   1306 
   1307    index = GET_CLEAR_BLEND_STATE_IDX(clear_buffers);
   1308 
   1309    if (ctx->blend_clear[index])
   1310       return ctx->blend_clear[index];
   1311 
   1312    /* Create a new one. */
   1313    {
   1314       struct pipe_blend_state blend = {0};
   1315       unsigned i;
   1316 
   1317       blend.independent_blend_enable = 1;
   1318 
   1319       for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
   1320          if (clear_buffers & (PIPE_CLEAR_COLOR0 << i)) {
   1321             blend.rt[i].colormask = PIPE_MASK_RGBA;
   1322          }
   1323       }
   1324 
   1325       ctx->blend_clear[index] = pipe->create_blend_state(pipe, &blend);
   1326    }
   1327    return ctx->blend_clear[index];
   1328 }
   1329 
   1330 void util_blitter_common_clear_setup(struct blitter_context *blitter,
   1331                                      unsigned width, unsigned height,
   1332                                      unsigned clear_buffers,
   1333                                      void *custom_blend, void *custom_dsa)
   1334 {
   1335    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
   1336    struct pipe_context *pipe = ctx->base.pipe;
   1337 
   1338    util_blitter_set_running_flag(blitter);
   1339    blitter_check_saved_vertex_states(ctx);
   1340    blitter_check_saved_fragment_states(ctx);
   1341    blitter_disable_render_cond(ctx);
   1342 
   1343    /* bind states */
   1344    if (custom_blend) {
   1345       pipe->bind_blend_state(pipe, custom_blend);
   1346    } else {
   1347       pipe->bind_blend_state(pipe, get_clear_blend_state(ctx, clear_buffers));
   1348    }
   1349 
   1350    if (custom_dsa) {
   1351       pipe->bind_depth_stencil_alpha_state(pipe, custom_dsa);
   1352    } else if ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
   1353       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
   1354    } else if (clear_buffers & PIPE_CLEAR_DEPTH) {
   1355       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
   1356    } else if (clear_buffers & PIPE_CLEAR_STENCIL) {
   1357       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
   1358    } else {
   1359       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
   1360    }
   1361 
   1362    pipe->set_sample_mask(pipe, ~0);
   1363    blitter_set_dst_dimensions(ctx, width, height);
   1364 }
   1365 
   1366 static void util_blitter_clear_custom(struct blitter_context *blitter,
   1367                                       unsigned width, unsigned height,
   1368                                       unsigned num_layers,
   1369                                       unsigned clear_buffers,
   1370                                       const union pipe_color_union *color,
   1371                                       double depth, unsigned stencil,
   1372                                       void *custom_blend, void *custom_dsa)
   1373 {
   1374    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
   1375    struct pipe_context *pipe = ctx->base.pipe;
   1376    struct pipe_stencil_ref sr = { { 0 } };
   1377 
   1378    assert(ctx->has_layered || num_layers <= 1);
   1379 
   1380    util_blitter_common_clear_setup(blitter, width, height, clear_buffers,
   1381                                    custom_blend, custom_dsa);
   1382 
   1383    sr.ref_value[0] = stencil & 0xff;
   1384    pipe->set_stencil_ref(pipe, &sr);
   1385 
   1386    bind_fs_write_all_cbufs(ctx);
   1387 
   1388    union blitter_attrib attrib;
   1389    memcpy(attrib.color, color->ui, sizeof(color->ui));
   1390 
   1391    bool pass_generic = (clear_buffers & PIPE_CLEAR_COLOR) != 0;
   1392    enum blitter_attrib_type type = pass_generic ? UTIL_BLITTER_ATTRIB_COLOR :
   1393                                                   UTIL_BLITTER_ATTRIB_NONE;
   1394 
   1395    if (num_layers > 1 && ctx->has_layered) {
   1396       blitter_get_vs_func get_vs = get_vs_layered;
   1397 
   1398       blitter_set_common_draw_rect_state(ctx, false);
   1399       blitter->draw_rectangle(blitter, ctx->velem_state, get_vs,
   1400                               0, 0, width, height,
   1401                               (float) depth, num_layers, type, &attrib);
   1402    } else {
   1403       blitter_get_vs_func get_vs;
   1404 
   1405       if (pass_generic)
   1406          get_vs = get_vs_passthrough_pos_generic;
   1407       else
   1408          get_vs = get_vs_passthrough_pos;
   1409 
   1410       blitter_set_common_draw_rect_state(ctx, false);
   1411       blitter->draw_rectangle(blitter, ctx->velem_state, get_vs,
   1412                               0, 0, width, height,
   1413                               (float) depth, 1, type, &attrib);
   1414    }
   1415 
   1416    util_blitter_restore_vertex_states(blitter);
   1417    util_blitter_restore_fragment_states(blitter);
   1418    util_blitter_restore_render_cond(blitter);
   1419    util_blitter_unset_running_flag(blitter);
   1420 }
   1421 
   1422 void util_blitter_clear(struct blitter_context *blitter,
   1423                         unsigned width, unsigned height, unsigned num_layers,
   1424                         unsigned clear_buffers,
   1425                         const union pipe_color_union *color,
   1426                         double depth, unsigned stencil)
   1427 {
   1428    util_blitter_clear_custom(blitter, width, height, num_layers,
   1429                              clear_buffers, color, depth, stencil,
   1430                              NULL, NULL);
   1431 }
   1432 
   1433 void util_blitter_custom_clear_depth(struct blitter_context *blitter,
   1434                                      unsigned width, unsigned height,
   1435                                      double depth, void *custom_dsa)
   1436 {
   1437    static const union pipe_color_union color;
   1438    util_blitter_clear_custom(blitter, width, height, 0, 0, &color, depth, 0,
   1439                              NULL, custom_dsa);
   1440 }
   1441 
   1442 void util_blitter_default_dst_texture(struct pipe_surface *dst_templ,
   1443                                       struct pipe_resource *dst,
   1444                                       unsigned dstlevel,
   1445                                       unsigned dstz)
   1446 {
   1447    memset(dst_templ, 0, sizeof(*dst_templ));
   1448    dst_templ->format = util_format_linear(dst->format);
   1449    dst_templ->u.tex.level = dstlevel;
   1450    dst_templ->u.tex.first_layer = dstz;
   1451    dst_templ->u.tex.last_layer = dstz;
   1452 }
   1453 
   1454 static struct pipe_surface *
   1455 util_blitter_get_next_surface_layer(struct pipe_context *pipe,
   1456                                     struct pipe_surface *surf)
   1457 {
   1458    struct pipe_surface dst_templ;
   1459 
   1460    memset(&dst_templ, 0, sizeof(dst_templ));
   1461    dst_templ.format = surf->format;
   1462    dst_templ.u.tex.level = surf->u.tex.level;
   1463    dst_templ.u.tex.first_layer = surf->u.tex.first_layer + 1;
   1464    dst_templ.u.tex.last_layer = surf->u.tex.last_layer + 1;
   1465 
   1466    return pipe->create_surface(pipe, surf->texture, &dst_templ);
   1467 }
   1468 
   1469 void util_blitter_default_src_texture(struct blitter_context *blitter,
   1470                                       struct pipe_sampler_view *src_templ,
   1471                                       struct pipe_resource *src,
   1472                                       unsigned srclevel)
   1473 {
   1474    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
   1475 
   1476    memset(src_templ, 0, sizeof(*src_templ));
   1477 
   1478    if (ctx->cube_as_2darray &&
   1479        (src->target == PIPE_TEXTURE_CUBE ||
   1480         src->target == PIPE_TEXTURE_CUBE_ARRAY))
   1481       src_templ->target = PIPE_TEXTURE_2D_ARRAY;
   1482    else
   1483       src_templ->target = src->target;
   1484 
   1485    src_templ->format = util_format_linear(src->format);
   1486    src_templ->u.tex.first_level = srclevel;
   1487    src_templ->u.tex.last_level = srclevel;
   1488    src_templ->u.tex.first_layer = 0;
   1489    src_templ->u.tex.last_layer =
   1490       src->target == PIPE_TEXTURE_3D ? u_minify(src->depth0, srclevel) - 1
   1491                                      : (unsigned)(src->array_size - 1);
   1492    src_templ->swizzle_r = PIPE_SWIZZLE_X;
   1493    src_templ->swizzle_g = PIPE_SWIZZLE_Y;
   1494    src_templ->swizzle_b = PIPE_SWIZZLE_Z;
   1495    src_templ->swizzle_a = PIPE_SWIZZLE_W;
   1496 }
   1497 
   1498 static bool is_blit_generic_supported(struct blitter_context *blitter,
   1499                                       const struct pipe_resource *dst,
   1500                                       enum pipe_format dst_format,
   1501                                       const struct pipe_resource *src,
   1502                                       enum pipe_format src_format,
   1503                                       unsigned mask)
   1504 {
   1505    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
   1506    struct pipe_screen *screen = ctx->base.pipe->screen;
   1507 
   1508    if (dst) {
   1509       unsigned bind;
   1510       const struct util_format_description *desc =
   1511             util_format_description(dst_format);
   1512       bool dst_has_stencil = util_format_has_stencil(desc);
   1513 
   1514       /* Stencil export must be supported for stencil copy. */
   1515       if ((mask & PIPE_MASK_S) && dst_has_stencil &&
   1516           !ctx->has_stencil_export) {
   1517          return false;
   1518       }
   1519 
   1520       if (dst_has_stencil || util_format_has_depth(desc))
   1521          bind = PIPE_BIND_DEPTH_STENCIL;
   1522       else
   1523          bind = PIPE_BIND_RENDER_TARGET;
   1524 
   1525       if (!screen->is_format_supported(screen, dst_format, dst->target,
   1526                                        dst->nr_samples, bind)) {
   1527          return false;
   1528       }
   1529    }
   1530 
   1531    if (src) {
   1532       if (src->nr_samples > 1 && !ctx->has_texture_multisample) {
   1533          return false;
   1534       }
   1535 
   1536       if (!screen->is_format_supported(screen, src_format, src->target,
   1537                                  src->nr_samples, PIPE_BIND_SAMPLER_VIEW)) {
   1538          return false;
   1539       }
   1540 
   1541       /* Check stencil sampler support for stencil copy. */
   1542       if (mask & PIPE_MASK_S) {
   1543          if (util_format_has_stencil(util_format_description(src_format))) {
   1544             enum pipe_format stencil_format =
   1545                util_format_stencil_only(src_format);
   1546             assert(stencil_format != PIPE_FORMAT_NONE);
   1547 
   1548             if (stencil_format != src_format &&
   1549                 !screen->is_format_supported(screen, stencil_format,
   1550                                              src->target, src->nr_samples,
   1551                                              PIPE_BIND_SAMPLER_VIEW)) {
   1552                return false;
   1553             }
   1554          }
   1555       }
   1556    }
   1557 
   1558    return true;
   1559 }
   1560 
   1561 bool util_blitter_is_copy_supported(struct blitter_context *blitter,
   1562                                     const struct pipe_resource *dst,
   1563                                     const struct pipe_resource *src)
   1564 {
   1565    return is_blit_generic_supported(blitter, dst, dst->format,
   1566                                     src, src->format, PIPE_MASK_RGBAZS);
   1567 }
   1568 
   1569 bool util_blitter_is_blit_supported(struct blitter_context *blitter,
   1570                                     const struct pipe_blit_info *info)
   1571 {
   1572    return is_blit_generic_supported(blitter,
   1573                                     info->dst.resource, info->dst.format,
   1574                                     info->src.resource, info->src.format,
   1575                                     info->mask);
   1576 }
   1577 
   1578 void util_blitter_copy_texture(struct blitter_context *blitter,
   1579                                struct pipe_resource *dst,
   1580                                unsigned dst_level,
   1581                                unsigned dstx, unsigned dsty, unsigned dstz,
   1582                                struct pipe_resource *src,
   1583                                unsigned src_level,
   1584                                const struct pipe_box *srcbox)
   1585 {
   1586    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
   1587    struct pipe_context *pipe = ctx->base.pipe;
   1588    struct pipe_surface *dst_view, dst_templ;
   1589    struct pipe_sampler_view src_templ, *src_view;
   1590    struct pipe_box dstbox;
   1591 
   1592    assert(dst && src);
   1593    assert(src->target < PIPE_MAX_TEXTURE_TYPES);
   1594 
   1595    u_box_3d(dstx, dsty, dstz, abs(srcbox->width), abs(srcbox->height),
   1596             abs(srcbox->depth), &dstbox);
   1597 
   1598    /* Initialize the surface. */
   1599    util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
   1600    dst_view = pipe->create_surface(pipe, dst, &dst_templ);
   1601 
   1602    /* Initialize the sampler view. */
   1603    util_blitter_default_src_texture(blitter, &src_templ, src, src_level);
   1604    src_view = pipe->create_sampler_view(pipe, src, &src_templ);
   1605 
   1606    /* Copy. */
   1607    util_blitter_blit_generic(blitter, dst_view, &dstbox,
   1608                              src_view, srcbox, src->width0, src->height0,
   1609                              PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
   1610                              false);
   1611 
   1612    pipe_surface_reference(&dst_view, NULL);
   1613    pipe_sampler_view_reference(&src_view, NULL);
   1614 }
   1615 
   1616 static void
   1617 blitter_draw_tex(struct blitter_context_priv *ctx,
   1618                  int dst_x1, int dst_y1, int dst_x2, int dst_y2,
   1619                  struct pipe_sampler_view *src,
   1620                  unsigned src_width0, unsigned src_height0,
   1621                  int src_x1, int src_y1, int src_x2, int src_y2,
   1622                  float layer, unsigned sample,
   1623                  bool uses_txf, enum blitter_attrib_type type)
   1624 {
   1625    union blitter_attrib coord;
   1626    blitter_get_vs_func get_vs = get_vs_passthrough_pos_generic;
   1627 
   1628    get_texcoords(src, src_width0, src_height0,
   1629                  src_x1, src_y1, src_x2, src_y2, layer, sample,
   1630                  uses_txf, &coord);
   1631 
   1632    if (src->target == PIPE_TEXTURE_CUBE ||
   1633        src->target == PIPE_TEXTURE_CUBE_ARRAY) {
   1634       float face_coord[4][2];
   1635 
   1636       set_texcoords_in_vertices(&coord, &face_coord[0][0], 2);
   1637       util_map_texcoords2d_onto_cubemap((unsigned)layer % 6,
   1638                                         /* pointer, stride in floats */
   1639                                         &face_coord[0][0], 2,
   1640                                         &ctx->vertices[0][1][0], 8,
   1641                                         false);
   1642       for (unsigned i = 0; i < 4; i++)
   1643          ctx->vertices[i][1][3] = coord.texcoord.w;
   1644 
   1645       /* Cubemaps don't use draw_rectangle. */
   1646       blitter_draw(ctx, ctx->velem_state, get_vs,
   1647                    dst_x1, dst_y1, dst_x2, dst_y2, 0, 1);
   1648    } else {
   1649       ctx->base.draw_rectangle(&ctx->base, ctx->velem_state, get_vs,
   1650                                dst_x1, dst_y1, dst_x2, dst_y2,
   1651                                0, 1, type, &coord);
   1652    }
   1653 }
   1654 
   1655 static void do_blits(struct blitter_context_priv *ctx,
   1656                      struct pipe_surface *dst,
   1657                      const struct pipe_box *dstbox,
   1658                      struct pipe_sampler_view *src,
   1659                      unsigned src_width0,
   1660                      unsigned src_height0,
   1661                      const struct pipe_box *srcbox,
   1662                      bool is_zsbuf,
   1663                      bool uses_txf)
   1664 {
   1665    struct pipe_context *pipe = ctx->base.pipe;
   1666    unsigned src_samples = src->texture->nr_samples;
   1667    unsigned dst_samples = dst->texture->nr_samples;
   1668    enum pipe_texture_target src_target = src->target;
   1669    struct pipe_framebuffer_state fb_state = {0};
   1670 
   1671    /* Initialize framebuffer state. */
   1672    fb_state.width = dst->width;
   1673    fb_state.height = dst->height;
   1674    fb_state.nr_cbufs = is_zsbuf ? 0 : 1;
   1675 
   1676    blitter_set_dst_dimensions(ctx, fb_state.width, fb_state.height);
   1677 
   1678    if ((src_target == PIPE_TEXTURE_1D ||
   1679         src_target == PIPE_TEXTURE_2D ||
   1680         src_target == PIPE_TEXTURE_RECT) &&
   1681        src_samples <= 1) {
   1682       /* Set framebuffer state. */
   1683       if (is_zsbuf) {
   1684          fb_state.zsbuf = dst;
   1685       } else {
   1686          fb_state.cbufs[0] = dst;
   1687       }
   1688       pipe->set_framebuffer_state(pipe, &fb_state);
   1689 
   1690       /* Draw. */
   1691       pipe->set_sample_mask(pipe, ~0);
   1692       blitter_draw_tex(ctx, dstbox->x, dstbox->y,
   1693                        dstbox->x + dstbox->width,
   1694                        dstbox->y + dstbox->height,
   1695                        src, src_width0, src_height0, srcbox->x, srcbox->y,
   1696                        srcbox->x + srcbox->width, srcbox->y + srcbox->height,
   1697                        0, 0, uses_txf, UTIL_BLITTER_ATTRIB_TEXCOORD_XY);
   1698    } else {
   1699       /* Draw the quad with the generic codepath. */
   1700       int dst_z;
   1701       for (dst_z = 0; dst_z < dstbox->depth; dst_z++) {
   1702          struct pipe_surface *old;
   1703          float dst2src_scale = srcbox->depth / (float)dstbox->depth;
   1704 
   1705          /* Scale Z properly if the blit is scaled.
   1706           *
   1707           * When downscaling, we want the coordinates centered, so that
   1708           * mipmapping works for 3D textures. For example, when generating
   1709           * a 4x4x4 level, this wouldn't average the pixels:
   1710           *
   1711           *   src Z:  0 1 2 3 4 5 6 7
   1712           *   dst Z:  0   1   2   3
   1713           *
   1714           * Because the pixels are not centered below the pixels of the higher
   1715           * level. Therefore, we want this:
   1716           *   src Z:  0 1 2 3 4 5 6 7
   1717           *   dst Z:   0   1   2   3
   1718           *
   1719           * dst_offset defines the offset needed for centering the pixels and
   1720           * it works with any scaling (not just 2x).
   1721           */
   1722          float dst_offset = ((srcbox->depth - 1) -
   1723                              (dstbox->depth - 1) * dst2src_scale) * 0.5;
   1724          float src_z = (dst_z + dst_offset) * dst2src_scale;
   1725 
   1726          /* Set framebuffer state. */
   1727          if (is_zsbuf) {
   1728             fb_state.zsbuf = dst;
   1729          } else {
   1730             fb_state.cbufs[0] = dst;
   1731          }
   1732          pipe->set_framebuffer_state(pipe, &fb_state);
   1733 
   1734          /* See if we need to blit a multisample or singlesample buffer. */
   1735          if (src_samples == dst_samples && dst_samples > 1) {
   1736             /* MSAA copy. */
   1737             unsigned i, max_sample = dst_samples - 1;
   1738 
   1739             for (i = 0; i <= max_sample; i++) {
   1740                pipe->set_sample_mask(pipe, 1 << i);
   1741                blitter_draw_tex(ctx, dstbox->x, dstbox->y,
   1742                                 dstbox->x + dstbox->width,
   1743                                 dstbox->y + dstbox->height,
   1744                                 src, src_width0, src_height0,
   1745                                 srcbox->x, srcbox->y,
   1746                                 srcbox->x + srcbox->width,
   1747                                 srcbox->y + srcbox->height,
   1748                                 srcbox->z + src_z, i, uses_txf,
   1749                                 UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW);
   1750             }
   1751          } else {
   1752             /* Normal copy, MSAA upsampling, or MSAA resolve. */
   1753             pipe->set_sample_mask(pipe, ~0);
   1754             blitter_draw_tex(ctx, dstbox->x, dstbox->y,
   1755                              dstbox->x + dstbox->width,
   1756                              dstbox->y + dstbox->height,
   1757                              src, src_width0, src_height0,
   1758                              srcbox->x, srcbox->y,
   1759                              srcbox->x + srcbox->width,
   1760                              srcbox->y + srcbox->height,
   1761                              srcbox->z + src_z, 0, uses_txf,
   1762                              UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW);
   1763          }
   1764 
   1765          /* Get the next surface or (if this is the last iteration)
   1766           * just unreference the last one. */
   1767          old = dst;
   1768          if (dst_z < dstbox->depth-1) {
   1769             dst = util_blitter_get_next_surface_layer(ctx->base.pipe, dst);
   1770          }
   1771          if (dst_z) {
   1772             pipe_surface_reference(&old, NULL);
   1773          }
   1774       }
   1775    }
   1776 }
   1777 
   1778 void util_blitter_blit_generic(struct blitter_context *blitter,
   1779                                struct pipe_surface *dst,
   1780                                const struct pipe_box *dstbox,
   1781                                struct pipe_sampler_view *src,
   1782                                const struct pipe_box *srcbox,
   1783                                unsigned src_width0, unsigned src_height0,
   1784                                unsigned mask, unsigned filter,
   1785                                const struct pipe_scissor_state *scissor,
   1786                                bool alpha_blend)
   1787 {
   1788    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
   1789    struct pipe_context *pipe = ctx->base.pipe;
   1790    enum pipe_texture_target src_target = src->target;
   1791    unsigned src_samples = src->texture->nr_samples;
   1792    unsigned dst_samples = dst->texture->nr_samples;
   1793    bool has_depth, has_stencil, has_color;
   1794    bool blit_stencil, blit_depth, blit_color;
   1795    void *sampler_state;
   1796    const struct util_format_description *src_desc =
   1797          util_format_description(src->format);
   1798    const struct util_format_description *dst_desc =
   1799          util_format_description(dst->format);
   1800 
   1801    has_color = src_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS &&
   1802                dst_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS;
   1803    has_depth = util_format_has_depth(src_desc) &&
   1804                util_format_has_depth(dst_desc);
   1805    has_stencil = util_format_has_stencil(src_desc) &&
   1806                  util_format_has_stencil(dst_desc);
   1807 
   1808    blit_color = has_color && (mask & PIPE_MASK_RGBA);
   1809    blit_depth = has_depth && (mask & PIPE_MASK_Z);
   1810    blit_stencil = has_stencil && (mask & PIPE_MASK_S) &&
   1811                   ctx->has_stencil_export;
   1812 
   1813    if (!blit_stencil && !blit_depth && !blit_color) {
   1814       return;
   1815    }
   1816 
   1817    bool is_scaled = dstbox->width != abs(srcbox->width) ||
   1818                     dstbox->height != abs(srcbox->height);
   1819 
   1820    if (blit_stencil || !is_scaled)
   1821       filter = PIPE_TEX_FILTER_NEAREST;
   1822 
   1823    bool use_txf = false;
   1824 
   1825    /* Don't support scaled blits. The TXF shader uses F2I for rounding. */
   1826    if (ctx->has_txf &&
   1827        !is_scaled &&
   1828        filter == PIPE_TEX_FILTER_NEAREST &&
   1829        src->target != PIPE_TEXTURE_CUBE &&
   1830        src->target != PIPE_TEXTURE_CUBE_ARRAY) {
   1831       int src_width = u_minify(src_width0, src->u.tex.first_level);
   1832       int src_height = u_minify(src_height0, src->u.tex.first_level);
   1833       int src_depth = src->u.tex.last_layer + 1;
   1834       struct pipe_box box = *srcbox;
   1835 
   1836       /* Eliminate negative width/height/depth. */
   1837       if (box.width < 0) {
   1838          box.x += box.width;
   1839          box.width *= -1;
   1840       }
   1841       if (box.height < 0) {
   1842          box.y += box.height;
   1843          box.height *= -1;
   1844       }
   1845       if (box.depth < 0) {
   1846          box.z += box.depth;
   1847          box.depth *= -1;
   1848       }
   1849 
   1850       /* See if srcbox is in bounds. TXF doesn't clamp the coordinates. */
   1851       use_txf =
   1852          box.x >= 0 && box.x < src_width &&
   1853          box.y >= 0 && box.y < src_height &&
   1854          box.z >= 0 && box.z < src_depth &&
   1855          box.x + box.width > 0 && box.x + box.width <= src_width &&
   1856          box.y + box.height > 0 && box.y + box.height <= src_height &&
   1857          box.z + box.depth > 0 && box.z + box.depth <= src_depth;
   1858    }
   1859 
   1860    /* Check whether the states are properly saved. */
   1861    util_blitter_set_running_flag(blitter);
   1862    blitter_check_saved_vertex_states(ctx);
   1863    blitter_check_saved_fragment_states(ctx);
   1864    blitter_check_saved_textures(ctx);
   1865    blitter_check_saved_fb_state(ctx);
   1866    blitter_disable_render_cond(ctx);
   1867 
   1868    if (blit_depth || blit_stencil) {
   1869       pipe->bind_blend_state(pipe, ctx->blend[0][0]);
   1870 
   1871       if (blit_depth && blit_stencil) {
   1872          pipe->bind_depth_stencil_alpha_state(pipe,
   1873                                               ctx->dsa_write_depth_stencil);
   1874          ctx->bind_fs_state(pipe,
   1875                blitter_get_fs_texfetch_depthstencil(ctx, src_target,
   1876                                                     src_samples, use_txf));
   1877       } else if (blit_depth) {
   1878          pipe->bind_depth_stencil_alpha_state(pipe,
   1879                                               ctx->dsa_write_depth_keep_stencil);
   1880          ctx->bind_fs_state(pipe,
   1881                blitter_get_fs_texfetch_depth(ctx, src_target,
   1882                                              src_samples, use_txf));
   1883       } else { /* is_stencil */
   1884          pipe->bind_depth_stencil_alpha_state(pipe,
   1885                                               ctx->dsa_keep_depth_write_stencil);
   1886          ctx->bind_fs_state(pipe,
   1887                blitter_get_fs_texfetch_stencil(ctx, src_target,
   1888                                                src_samples, use_txf));
   1889       }
   1890 
   1891    } else {
   1892       unsigned colormask = mask & PIPE_MASK_RGBA;
   1893 
   1894       pipe->bind_blend_state(pipe, ctx->blend[colormask][alpha_blend]);
   1895       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
   1896       ctx->bind_fs_state(pipe,
   1897             blitter_get_fs_texfetch_col(ctx, src->format, dst->format, src_target,
   1898                                         src_samples, dst_samples, filter,
   1899                                         use_txf));
   1900    }
   1901 
   1902    /* Set the linear filter only for scaled color non-MSAA blits. */
   1903    if (filter == PIPE_TEX_FILTER_LINEAR) {
   1904       if (src_target == PIPE_TEXTURE_RECT) {
   1905          sampler_state = ctx->sampler_state_rect_linear;
   1906       } else {
   1907          sampler_state = ctx->sampler_state_linear;
   1908       }
   1909    } else {
   1910       if (src_target == PIPE_TEXTURE_RECT) {
   1911          sampler_state = ctx->sampler_state_rect;
   1912       } else {
   1913          sampler_state = ctx->sampler_state;
   1914       }
   1915    }
   1916 
   1917    /* Set samplers. */
   1918    if (blit_depth && blit_stencil) {
   1919       /* Setup two samplers, one for depth and the other one for stencil. */
   1920       struct pipe_sampler_view templ;
   1921       struct pipe_sampler_view *views[2];
   1922       void *samplers[2] = {sampler_state, sampler_state};
   1923 
   1924       templ = *src;
   1925       templ.format = util_format_stencil_only(templ.format);
   1926       assert(templ.format != PIPE_FORMAT_NONE);
   1927 
   1928       views[0] = src;
   1929       views[1] = pipe->create_sampler_view(pipe, src->texture, &templ);
   1930 
   1931       pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 2, views);
   1932       pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0, 2, samplers);
   1933 
   1934       pipe_sampler_view_reference(&views[1], NULL);
   1935    } else if (blit_stencil) {
   1936       /* Set a stencil-only sampler view for it not to sample depth instead. */
   1937       struct pipe_sampler_view templ;
   1938       struct pipe_sampler_view *view;
   1939 
   1940       templ = *src;
   1941       templ.format = util_format_stencil_only(templ.format);
   1942       assert(templ.format != PIPE_FORMAT_NONE);
   1943 
   1944       view = pipe->create_sampler_view(pipe, src->texture, &templ);
   1945 
   1946       pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &view);
   1947       pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
   1948                                 0, 1, &sampler_state);
   1949 
   1950       pipe_sampler_view_reference(&view, NULL);
   1951    } else {
   1952       pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &src);
   1953       pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
   1954                                 0, 1, &sampler_state);
   1955    }
   1956 
   1957    if (scissor) {
   1958       pipe->set_scissor_states(pipe, 0, 1, scissor);
   1959    }
   1960 
   1961    blitter_set_common_draw_rect_state(ctx, scissor != NULL);
   1962 
   1963    do_blits(ctx, dst, dstbox, src, src_width0, src_height0,
   1964             srcbox, blit_depth || blit_stencil, use_txf);
   1965 
   1966    util_blitter_restore_vertex_states(blitter);
   1967    util_blitter_restore_fragment_states(blitter);
   1968    util_blitter_restore_textures(blitter);
   1969    util_blitter_restore_fb_state(blitter);
   1970    if (scissor) {
   1971       pipe->set_scissor_states(pipe, 0, 1, &ctx->base.saved_scissor);
   1972    }
   1973    util_blitter_restore_render_cond(blitter);
   1974    util_blitter_unset_running_flag(blitter);
   1975 }
   1976 
   1977 void
   1978 util_blitter_blit(struct blitter_context *blitter,
   1979                   const struct pipe_blit_info *info)
   1980 {
   1981    struct pipe_resource *dst = info->dst.resource;
   1982    struct pipe_resource *src = info->src.resource;
   1983    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
   1984    struct pipe_context *pipe = ctx->base.pipe;
   1985    struct pipe_surface *dst_view, dst_templ;
   1986    struct pipe_sampler_view src_templ, *src_view;
   1987 
   1988    /* Initialize the surface. */
   1989    util_blitter_default_dst_texture(&dst_templ, dst, info->dst.level,
   1990                                     info->dst.box.z);
   1991    dst_templ.format = info->dst.format;
   1992    dst_view = pipe->create_surface(pipe, dst, &dst_templ);
   1993 
   1994    /* Initialize the sampler view. */
   1995    util_blitter_default_src_texture(blitter, &src_templ, src, info->src.level);
   1996    src_templ.format = info->src.format;
   1997    src_view = pipe->create_sampler_view(pipe, src, &src_templ);
   1998 
   1999    /* Copy. */
   2000    util_blitter_blit_generic(blitter, dst_view, &info->dst.box,
   2001                              src_view, &info->src.box, src->width0, src->height0,
   2002                              info->mask, info->filter,
   2003                              info->scissor_enable ? &info->scissor : NULL,
   2004                              info->alpha_blend);
   2005 
   2006    pipe_surface_reference(&dst_view, NULL);
   2007    pipe_sampler_view_reference(&src_view, NULL);
   2008 }
   2009 
   2010 void util_blitter_generate_mipmap(struct blitter_context *blitter,
   2011                                   struct pipe_resource *tex,
   2012                                   enum pipe_format format,
   2013                                   unsigned base_level, unsigned last_level,
   2014                                   unsigned first_layer, unsigned last_layer)
   2015 {
   2016    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
   2017    struct pipe_context *pipe = ctx->base.pipe;
   2018    struct pipe_surface dst_templ, *dst_view;
   2019    struct pipe_sampler_view src_templ, *src_view;
   2020    bool is_depth;
   2021    void *sampler_state;
   2022    const struct util_format_description *desc =
   2023          util_format_description(format);
   2024    unsigned src_level;
   2025    unsigned target = tex->target;
   2026 
   2027    if (ctx->cube_as_2darray &&
   2028        (target == PIPE_TEXTURE_CUBE || target == PIPE_TEXTURE_CUBE_ARRAY))
   2029       target = PIPE_TEXTURE_2D_ARRAY;
   2030 
   2031    assert(tex->nr_samples <= 1);
   2032    assert(!util_format_has_stencil(desc));
   2033 
   2034    is_depth = desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS;
   2035 
   2036    /* Check whether the states are properly saved. */
   2037    util_blitter_set_running_flag(blitter);
   2038    blitter_check_saved_vertex_states(ctx);
   2039    blitter_check_saved_fragment_states(ctx);
   2040    blitter_check_saved_textures(ctx);
   2041    blitter_check_saved_fb_state(ctx);
   2042    blitter_disable_render_cond(ctx);
   2043 
   2044    /* Set states. */
   2045    if (is_depth) {
   2046       pipe->bind_blend_state(pipe, ctx->blend[0][0]);
   2047       pipe->bind_depth_stencil_alpha_state(pipe,
   2048                                            ctx->dsa_write_depth_keep_stencil);
   2049       ctx->bind_fs_state(pipe,
   2050                          blitter_get_fs_texfetch_depth(ctx, target, 1, false));
   2051    } else {
   2052       pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA][0]);
   2053       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
   2054       ctx->bind_fs_state(pipe,
   2055             blitter_get_fs_texfetch_col(ctx, tex->format, tex->format, target,
   2056                                         1, 1, PIPE_TEX_FILTER_LINEAR, false));
   2057    }
   2058 
   2059    if (target == PIPE_TEXTURE_RECT) {
   2060       sampler_state = ctx->sampler_state_rect_linear;
   2061    } else {
   2062       sampler_state = ctx->sampler_state_linear;
   2063    }
   2064    pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
   2065                              0, 1, &sampler_state);
   2066 
   2067    blitter_set_common_draw_rect_state(ctx, false);
   2068 
   2069    for (src_level = base_level; src_level < last_level; src_level++) {
   2070       struct pipe_box dstbox = {0}, srcbox = {0};
   2071       unsigned dst_level = src_level + 1;
   2072 
   2073       dstbox.width = u_minify(tex->width0, dst_level);
   2074       dstbox.height = u_minify(tex->height0, dst_level);
   2075 
   2076       srcbox.width = u_minify(tex->width0, src_level);
   2077       srcbox.height = u_minify(tex->height0, src_level);
   2078 
   2079       if (target == PIPE_TEXTURE_3D) {
   2080          dstbox.depth = util_num_layers(tex, dst_level);
   2081          srcbox.depth = util_num_layers(tex, src_level);
   2082       } else {
   2083          dstbox.z = srcbox.z = first_layer;
   2084          dstbox.depth = srcbox.depth = last_layer - first_layer + 1;
   2085       }
   2086 
   2087       /* Initialize the surface. */
   2088       util_blitter_default_dst_texture(&dst_templ, tex, dst_level,
   2089                                        first_layer);
   2090       dst_templ.format = format;
   2091       dst_view = pipe->create_surface(pipe, tex, &dst_templ);
   2092 
   2093       /* Initialize the sampler view. */
   2094       util_blitter_default_src_texture(blitter, &src_templ, tex, src_level);
   2095       src_templ.format = format;
   2096       src_view = pipe->create_sampler_view(pipe, tex, &src_templ);
   2097 
   2098       pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &src_view);
   2099 
   2100       do_blits(ctx, dst_view, &dstbox, src_view, tex->width0, tex->height0,
   2101                &srcbox, is_depth, false);
   2102 
   2103       pipe_surface_reference(&dst_view, NULL);
   2104       pipe_sampler_view_reference(&src_view, NULL);
   2105    }
   2106 
   2107    util_blitter_restore_vertex_states(blitter);
   2108    util_blitter_restore_fragment_states(blitter);
   2109    util_blitter_restore_textures(blitter);
   2110    util_blitter_restore_fb_state(blitter);
   2111    util_blitter_restore_render_cond(blitter);
   2112    util_blitter_unset_running_flag(blitter);
   2113 }
   2114 
   2115 /* Clear a region of a color surface to a constant value. */
   2116 void util_blitter_clear_render_target(struct blitter_context *blitter,
   2117                                       struct pipe_surface *dstsurf,
   2118                                       const union pipe_color_union *color,
   2119                                       unsigned dstx, unsigned dsty,
   2120                                       unsigned width, unsigned height)
   2121 {
   2122    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
   2123    struct pipe_context *pipe = ctx->base.pipe;
   2124    struct pipe_framebuffer_state fb_state;
   2125    unsigned num_layers;
   2126 
   2127    assert(dstsurf->texture);
   2128    if (!dstsurf->texture)
   2129       return;
   2130 
   2131    /* check the saved state */
   2132    util_blitter_set_running_flag(blitter);
   2133    blitter_check_saved_vertex_states(ctx);
   2134    blitter_check_saved_fragment_states(ctx);
   2135    blitter_check_saved_fb_state(ctx);
   2136    blitter_disable_render_cond(ctx);
   2137 
   2138    /* bind states */
   2139    pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA][0]);
   2140    pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
   2141    bind_fs_write_one_cbuf(ctx);
   2142 
   2143    /* set a framebuffer state */
   2144    fb_state.width = dstsurf->width;
   2145    fb_state.height = dstsurf->height;
   2146    fb_state.nr_cbufs = 1;
   2147    fb_state.cbufs[0] = dstsurf;
   2148    fb_state.zsbuf = 0;
   2149    pipe->set_framebuffer_state(pipe, &fb_state);
   2150    pipe->set_sample_mask(pipe, ~0);
   2151 
   2152    blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
   2153 
   2154    union blitter_attrib attrib;
   2155    memcpy(attrib.color, color->ui, sizeof(color->ui));
   2156 
   2157    num_layers = dstsurf->u.tex.last_layer - dstsurf->u.tex.first_layer + 1;
   2158    if (num_layers > 1 && ctx->has_layered) {
   2159       blitter_set_common_draw_rect_state(ctx, false);
   2160       blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_layered,
   2161                               dstx, dsty, dstx+width, dsty+height, 0,
   2162                               num_layers, UTIL_BLITTER_ATTRIB_COLOR, &attrib);
   2163    } else {
   2164       blitter_set_common_draw_rect_state(ctx, false);
   2165       blitter->draw_rectangle(blitter, ctx->velem_state,
   2166                               get_vs_passthrough_pos_generic,
   2167                               dstx, dsty, dstx+width, dsty+height, 0,
   2168                               1, UTIL_BLITTER_ATTRIB_COLOR, &attrib);
   2169    }
   2170 
   2171    util_blitter_restore_vertex_states(blitter);
   2172    util_blitter_restore_fragment_states(blitter);
   2173    util_blitter_restore_fb_state(blitter);
   2174    util_blitter_restore_render_cond(blitter);
   2175    util_blitter_unset_running_flag(blitter);
   2176 }
   2177 
   2178 /* Clear a region of a depth stencil surface. */
   2179 void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
   2180                                       struct pipe_surface *dstsurf,
   2181                                       unsigned clear_flags,
   2182                                       double depth,
   2183                                       unsigned stencil,
   2184                                       unsigned dstx, unsigned dsty,
   2185                                       unsigned width, unsigned height)
   2186 {
   2187    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
   2188    struct pipe_context *pipe = ctx->base.pipe;
   2189    struct pipe_framebuffer_state fb_state;
   2190    struct pipe_stencil_ref sr = { { 0 } };
   2191    unsigned num_layers;
   2192 
   2193    assert(dstsurf->texture);
   2194    if (!dstsurf->texture)
   2195       return;
   2196 
   2197    /* check the saved state */
   2198    util_blitter_set_running_flag(blitter);
   2199    blitter_check_saved_vertex_states(ctx);
   2200    blitter_check_saved_fragment_states(ctx);
   2201    blitter_check_saved_fb_state(ctx);
   2202    blitter_disable_render_cond(ctx);
   2203 
   2204    /* bind states */
   2205    pipe->bind_blend_state(pipe, ctx->blend[0][0]);
   2206    if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
   2207       sr.ref_value[0] = stencil & 0xff;
   2208       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
   2209       pipe->set_stencil_ref(pipe, &sr);
   2210    }
   2211    else if (clear_flags & PIPE_CLEAR_DEPTH) {
   2212       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
   2213    }
   2214    else if (clear_flags & PIPE_CLEAR_STENCIL) {
   2215       sr.ref_value[0] = stencil & 0xff;
   2216       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
   2217       pipe->set_stencil_ref(pipe, &sr);
   2218    }
   2219    else
   2220       /* hmm that should be illegal probably, or make it a no-op somewhere */
   2221       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
   2222 
   2223    bind_fs_empty(ctx);
   2224 
   2225    /* set a framebuffer state */
   2226    fb_state.width = dstsurf->width;
   2227    fb_state.height = dstsurf->height;
   2228    fb_state.nr_cbufs = 0;
   2229    fb_state.cbufs[0] = 0;
   2230    fb_state.zsbuf = dstsurf;
   2231    pipe->set_framebuffer_state(pipe, &fb_state);
   2232    pipe->set_sample_mask(pipe, ~0);
   2233 
   2234    blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
   2235 
   2236    num_layers = dstsurf->u.tex.last_layer - dstsurf->u.tex.first_layer + 1;
   2237    if (num_layers > 1 && ctx->has_layered) {
   2238       blitter_set_common_draw_rect_state(ctx, false);
   2239       blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_layered,
   2240                               dstx, dsty, dstx+width, dsty+height, depth,
   2241                               num_layers, UTIL_BLITTER_ATTRIB_NONE, NULL);
   2242    } else {
   2243       blitter_set_common_draw_rect_state(ctx, false);
   2244       blitter->draw_rectangle(blitter, ctx->velem_state,
   2245                               get_vs_passthrough_pos,
   2246                               dstx, dsty, dstx+width, dsty+height, depth, 1,
   2247                               UTIL_BLITTER_ATTRIB_NONE, NULL);
   2248    }
   2249 
   2250    util_blitter_restore_vertex_states(blitter);
   2251    util_blitter_restore_fragment_states(blitter);
   2252    util_blitter_restore_fb_state(blitter);
   2253    util_blitter_restore_render_cond(blitter);
   2254    util_blitter_unset_running_flag(blitter);
   2255 }
   2256 
   2257 /* draw a rectangle across a region using a custom dsa stage - for r600g */
   2258 void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
   2259                                        struct pipe_surface *zsurf,
   2260                                        struct pipe_surface *cbsurf,
   2261                                        unsigned sample_mask,
   2262                                        void *dsa_stage, float depth)
   2263 {
   2264    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
   2265    struct pipe_context *pipe = ctx->base.pipe;
   2266    struct pipe_framebuffer_state fb_state;
   2267 
   2268    assert(zsurf->texture);
   2269    if (!zsurf->texture)
   2270       return;
   2271 
   2272    /* check the saved state */
   2273    util_blitter_set_running_flag(blitter);
   2274    blitter_check_saved_vertex_states(ctx);
   2275    blitter_check_saved_fragment_states(ctx);
   2276    blitter_check_saved_fb_state(ctx);
   2277    blitter_disable_render_cond(ctx);
   2278 
   2279    /* bind states */
   2280    pipe->bind_blend_state(pipe, cbsurf ? ctx->blend[PIPE_MASK_RGBA][0] :
   2281                                          ctx->blend[0][0]);
   2282    pipe->bind_depth_stencil_alpha_state(pipe, dsa_stage);
   2283    if (cbsurf)
   2284       bind_fs_write_one_cbuf(ctx);
   2285    else
   2286       bind_fs_empty(ctx);
   2287 
   2288    /* set a framebuffer state */
   2289    fb_state.width = zsurf->width;
   2290    fb_state.height = zsurf->height;
   2291    fb_state.nr_cbufs = 1;
   2292    if (cbsurf) {
   2293       fb_state.cbufs[0] = cbsurf;
   2294       fb_state.nr_cbufs = 1;
   2295    } else {
   2296       fb_state.cbufs[0] = NULL;
   2297       fb_state.nr_cbufs = 0;
   2298    }
   2299    fb_state.zsbuf = zsurf;
   2300    pipe->set_framebuffer_state(pipe, &fb_state);
   2301    pipe->set_sample_mask(pipe, sample_mask);
   2302 
   2303    blitter_set_common_draw_rect_state(ctx, false);
   2304    blitter_set_dst_dimensions(ctx, zsurf->width, zsurf->height);
   2305    blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_passthrough_pos,
   2306                            0, 0, zsurf->width, zsurf->height, depth,
   2307                            1, UTIL_BLITTER_ATTRIB_NONE, NULL);
   2308 
   2309    util_blitter_restore_vertex_states(blitter);
   2310    util_blitter_restore_fragment_states(blitter);
   2311    util_blitter_restore_fb_state(blitter);
   2312    util_blitter_restore_render_cond(blitter);
   2313    util_blitter_unset_running_flag(blitter);
   2314 }
   2315 
   2316 void util_blitter_copy_buffer(struct blitter_context *blitter,
   2317                               struct pipe_resource *dst,
   2318                               unsigned dstx,
   2319                               struct pipe_resource *src,
   2320                               unsigned srcx,
   2321                               unsigned size)
   2322 {
   2323    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
   2324    struct pipe_context *pipe = ctx->base.pipe;
   2325    struct pipe_vertex_buffer vb;
   2326    struct pipe_stream_output_target *so_target;
   2327    unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0};
   2328 
   2329    if (srcx >= src->width0 ||
   2330        dstx >= dst->width0) {
   2331       return;
   2332    }
   2333    if (srcx + size > src->width0) {
   2334       size = src->width0 - srcx;
   2335    }
   2336    if (dstx + size > dst->width0) {
   2337       size = dst->width0 - dstx;
   2338    }
   2339 
   2340    /* Drivers not capable of Stream Out should not call this function
   2341     * in the first place. */
   2342    assert(ctx->has_stream_out);
   2343 
   2344    /* Some alignment is required. */
   2345    if (srcx % 4 != 0 || dstx % 4 != 0 || size % 4 != 0 ||
   2346        !ctx->has_stream_out) {
   2347       struct pipe_box box;
   2348       u_box_1d(srcx, size, &box);
   2349       util_resource_copy_region(pipe, dst, 0, dstx, 0, 0, src, 0, &box);
   2350       return;
   2351    }
   2352 
   2353    util_blitter_set_running_flag(blitter);
   2354    blitter_check_saved_vertex_states(ctx);
   2355    blitter_disable_render_cond(ctx);
   2356 
   2357    vb.is_user_buffer = false;
   2358    vb.buffer.resource = src;
   2359    vb.buffer_offset = srcx;
   2360    vb.stride = 4;
   2361 
   2362    pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
   2363    pipe->bind_vertex_elements_state(pipe, ctx->velem_state_readbuf[0]);
   2364    bind_vs_pos_only(ctx, 1);
   2365    if (ctx->has_geometry_shader)
   2366       pipe->bind_gs_state(pipe, NULL);
   2367    if (ctx->has_tessellation) {
   2368       pipe->bind_tcs_state(pipe, NULL);
   2369       pipe->bind_tes_state(pipe, NULL);
   2370    }
   2371    pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
   2372 
   2373    so_target = pipe->create_stream_output_target(pipe, dst, dstx, size);
   2374    pipe->set_stream_output_targets(pipe, 1, &so_target, offsets);
   2375 
   2376    util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
   2377 
   2378    util_blitter_restore_vertex_states(blitter);
   2379    util_blitter_restore_render_cond(blitter);
   2380    util_blitter_unset_running_flag(blitter);
   2381    pipe_so_target_reference(&so_target, NULL);
   2382 }
   2383 
   2384 void util_blitter_clear_buffer(struct blitter_context *blitter,
   2385                                struct pipe_resource *dst,
   2386                                unsigned offset, unsigned size,
   2387                                unsigned num_channels,
   2388                                const union pipe_color_union *clear_value)
   2389 {
   2390    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
   2391    struct pipe_context *pipe = ctx->base.pipe;
   2392    struct pipe_vertex_buffer vb = {0};
   2393    struct pipe_stream_output_target *so_target = NULL;
   2394    unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0};
   2395 
   2396    assert(num_channels >= 1);
   2397    assert(num_channels <= 4);
   2398 
   2399    /* IMPORTANT:  DON'T DO ANY BOUNDS CHECKING HERE!
   2400     *
   2401     * R600 uses this to initialize texture resources, so width0 might not be
   2402     * what you think it is.
   2403     */
   2404 
   2405    /* Streamout is required. */
   2406    if (!ctx->has_stream_out) {
   2407       assert(!"Streamout unsupported in util_blitter_clear_buffer()");
   2408       return;
   2409    }
   2410 
   2411    /* Some alignment is required. */
   2412    if (offset % 4 != 0 || size % 4 != 0) {
   2413       assert(!"Bad alignment in util_blitter_clear_buffer()");
   2414       return;
   2415    }
   2416 
   2417    u_upload_data(pipe->stream_uploader, 0, num_channels*4, 4, clear_value,
   2418                  &vb.buffer_offset, &vb.buffer.resource);
   2419    if (!vb.buffer.resource)
   2420       goto out;
   2421 
   2422    vb.stride = 0;
   2423 
   2424    util_blitter_set_running_flag(blitter);
   2425    blitter_check_saved_vertex_states(ctx);
   2426    blitter_disable_render_cond(ctx);
   2427 
   2428    pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
   2429    pipe->bind_vertex_elements_state(pipe,
   2430                                     ctx->velem_state_readbuf[num_channels-1]);
   2431    bind_vs_pos_only(ctx, num_channels);
   2432    if (ctx->has_geometry_shader)
   2433       pipe->bind_gs_state(pipe, NULL);
   2434    if (ctx->has_tessellation) {
   2435       pipe->bind_tcs_state(pipe, NULL);
   2436       pipe->bind_tes_state(pipe, NULL);
   2437    }
   2438    pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
   2439 
   2440    so_target = pipe->create_stream_output_target(pipe, dst, offset, size);
   2441    pipe->set_stream_output_targets(pipe, 1, &so_target, offsets);
   2442 
   2443    util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
   2444 
   2445 out:
   2446    util_blitter_restore_vertex_states(blitter);
   2447    util_blitter_restore_render_cond(blitter);
   2448    util_blitter_unset_running_flag(blitter);
   2449    pipe_so_target_reference(&so_target, NULL);
   2450    pipe_resource_reference(&vb.buffer.resource, NULL);
   2451 }
   2452 
   2453 /* probably radeon specific */
   2454 void util_blitter_custom_resolve_color(struct blitter_context *blitter,
   2455                                        struct pipe_resource *dst,
   2456                                        unsigned dst_level,
   2457                                        unsigned dst_layer,
   2458                                        struct pipe_resource *src,
   2459                                        unsigned src_layer,
   2460                                        unsigned sample_mask,
   2461                                        void *custom_blend,
   2462                                        enum pipe_format format)
   2463 {
   2464    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
   2465    struct pipe_context *pipe = ctx->base.pipe;
   2466    struct pipe_framebuffer_state fb_state;
   2467    struct pipe_surface *srcsurf, *dstsurf, surf_tmpl;
   2468 
   2469    util_blitter_set_running_flag(blitter);
   2470    blitter_check_saved_vertex_states(ctx);
   2471    blitter_check_saved_fragment_states(ctx);
   2472    blitter_disable_render_cond(ctx);
   2473 
   2474    /* bind states */
   2475    pipe->bind_blend_state(pipe, custom_blend);
   2476    pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
   2477    bind_fs_write_one_cbuf(ctx);
   2478    pipe->set_sample_mask(pipe, sample_mask);
   2479 
   2480    memset(&surf_tmpl, 0, sizeof(surf_tmpl));
   2481    surf_tmpl.format = format;
   2482    surf_tmpl.u.tex.level = dst_level;
   2483    surf_tmpl.u.tex.first_layer = dst_layer;
   2484    surf_tmpl.u.tex.last_layer = dst_layer;
   2485 
   2486    dstsurf = pipe->create_surface(pipe, dst, &surf_tmpl);
   2487 
   2488    surf_tmpl.u.tex.level = 0;
   2489    surf_tmpl.u.tex.first_layer = src_layer;
   2490    surf_tmpl.u.tex.last_layer = src_layer;
   2491 
   2492    srcsurf = pipe->create_surface(pipe, src, &surf_tmpl);
   2493 
   2494    /* set a framebuffer state */
   2495    fb_state.width = src->width0;
   2496    fb_state.height = src->height0;
   2497    fb_state.nr_cbufs = 2;
   2498    fb_state.cbufs[0] = srcsurf;
   2499    fb_state.cbufs[1] = dstsurf;
   2500    fb_state.zsbuf = NULL;
   2501    pipe->set_framebuffer_state(pipe, &fb_state);
   2502 
   2503    blitter_set_common_draw_rect_state(ctx, false);
   2504    blitter_set_dst_dimensions(ctx, src->width0, src->height0);
   2505    blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_passthrough_pos,
   2506                            0, 0, src->width0, src->height0,
   2507                            0, 1, UTIL_BLITTER_ATTRIB_NONE, NULL);
   2508    util_blitter_restore_fb_state(blitter);
   2509    util_blitter_restore_vertex_states(blitter);
   2510    util_blitter_restore_fragment_states(blitter);
   2511    util_blitter_restore_render_cond(blitter);
   2512    util_blitter_unset_running_flag(blitter);
   2513 
   2514    pipe_surface_reference(&srcsurf, NULL);
   2515    pipe_surface_reference(&dstsurf, NULL);
   2516 }
   2517 
   2518 void util_blitter_custom_color(struct blitter_context *blitter,
   2519                                struct pipe_surface *dstsurf,
   2520                                void *custom_blend)
   2521 {
   2522    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
   2523    struct pipe_context *pipe = ctx->base.pipe;
   2524    struct pipe_framebuffer_state fb_state;
   2525 
   2526    assert(dstsurf->texture);
   2527    if (!dstsurf->texture)
   2528       return;
   2529 
   2530    /* check the saved state */
   2531    util_blitter_set_running_flag(blitter);
   2532    blitter_check_saved_vertex_states(ctx);
   2533    blitter_check_saved_fragment_states(ctx);
   2534    blitter_check_saved_fb_state(ctx);
   2535    blitter_disable_render_cond(ctx);
   2536 
   2537    /* bind states */
   2538    pipe->bind_blend_state(pipe, custom_blend ? custom_blend
   2539                                              : ctx->blend[PIPE_MASK_RGBA][0]);
   2540    pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
   2541    bind_fs_write_one_cbuf(ctx);
   2542    pipe->set_sample_mask(pipe, (1ull << MAX2(1, dstsurf->texture->nr_samples)) - 1);
   2543 
   2544    /* set a framebuffer state */
   2545    fb_state.width = dstsurf->width;
   2546    fb_state.height = dstsurf->height;
   2547    fb_state.nr_cbufs = 1;
   2548    fb_state.cbufs[0] = dstsurf;
   2549    fb_state.zsbuf = 0;
   2550    pipe->set_framebuffer_state(pipe, &fb_state);
   2551    pipe->set_sample_mask(pipe, ~0);
   2552 
   2553    blitter_set_common_draw_rect_state(ctx, false);
   2554    blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
   2555    blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_passthrough_pos,
   2556                            0, 0, dstsurf->width, dstsurf->height,
   2557                            0, 1, UTIL_BLITTER_ATTRIB_NONE, NULL);
   2558 
   2559    util_blitter_restore_vertex_states(blitter);
   2560    util_blitter_restore_fragment_states(blitter);
   2561    util_blitter_restore_fb_state(blitter);
   2562    util_blitter_restore_render_cond(blitter);
   2563    util_blitter_unset_running_flag(blitter);
   2564 }
   2565