Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /**************************************************************************
  2.  *
  3.  * Copyright 2009 Marek Olšák <maraeo@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 Olšák
  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.    struct u_upload_mgr *upload;
  65.  
  66.    float vertices[4][2][4];   /**< {pos, color} or {pos, texcoord} */
  67.  
  68.    /* Templates for various state objects. */
  69.  
  70.    /* Constant state objects. */
  71.    /* Vertex shaders. */
  72.    void *vs; /**< Vertex shader which passes {pos, generic} to the output.*/
  73.    void *vs_pos_only; /**< Vertex shader which passes pos to the output.*/
  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,
  82.       where the index is PIPE_TEXTURE_* to be sampled. */
  83.    void *fs_texfetch_col[PIPE_MAX_TEXTURE_TYPES];
  84.  
  85.    /* FS which outputs a depth from a texture,
  86.       where the index is PIPE_TEXTURE_* to be sampled. */
  87.    void *fs_texfetch_depth[PIPE_MAX_TEXTURE_TYPES];
  88.    void *fs_texfetch_depthstencil[PIPE_MAX_TEXTURE_TYPES];
  89.    void *fs_texfetch_stencil[PIPE_MAX_TEXTURE_TYPES];
  90.  
  91.    /* FS which outputs one sample from a multisample texture. */
  92.    void *fs_texfetch_col_msaa[PIPE_MAX_TEXTURE_TYPES];
  93.    void *fs_texfetch_depth_msaa[PIPE_MAX_TEXTURE_TYPES];
  94.    void *fs_texfetch_depthstencil_msaa[PIPE_MAX_TEXTURE_TYPES];
  95.    void *fs_texfetch_stencil_msaa[PIPE_MAX_TEXTURE_TYPES];
  96.  
  97.    /* FS which outputs an average of all samples. */
  98.    void *fs_resolve[PIPE_MAX_TEXTURE_TYPES][NUM_RESOLVE_FRAG_SHADERS][2];
  99.    void *fs_resolve_sint[PIPE_MAX_TEXTURE_TYPES][NUM_RESOLVE_FRAG_SHADERS][2];
  100.    void *fs_resolve_uint[PIPE_MAX_TEXTURE_TYPES][NUM_RESOLVE_FRAG_SHADERS][2];
  101.  
  102.    /* Blend state. */
  103.    void *blend[PIPE_MASK_RGBA+1]; /**< blend state with writemask */
  104.    void *blend_clear[GET_CLEAR_BLEND_STATE_IDX(PIPE_CLEAR_COLOR)+1];
  105.  
  106.    /* Depth stencil alpha state. */
  107.    void *dsa_write_depth_stencil;
  108.    void *dsa_write_depth_keep_stencil;
  109.    void *dsa_keep_depth_stencil;
  110.    void *dsa_keep_depth_write_stencil;
  111.  
  112.    /* Vertex elements states. */
  113.    void *velem_state;
  114.    void *velem_state_readbuf[4]; /**< X, XY, XYZ, XYZW */
  115.  
  116.    /* Sampler state. */
  117.    void *sampler_state;
  118.    void *sampler_state_linear;
  119.    void *sampler_state_rect;
  120.    void *sampler_state_rect_linear;
  121.  
  122.    /* Rasterizer state. */
  123.    void *rs_state, *rs_state_scissor, *rs_discard_state;
  124.  
  125.    /* Viewport state. */
  126.    struct pipe_viewport_state viewport;
  127.  
  128.    /* Destination surface dimensions. */
  129.    unsigned dst_width;
  130.    unsigned dst_height;
  131.  
  132.    boolean has_geometry_shader;
  133.    boolean has_tessellation;
  134.    boolean has_layered;
  135.    boolean has_stream_out;
  136.    boolean has_stencil_export;
  137.    boolean has_texture_multisample;
  138.    boolean cached_all_shaders;
  139.  
  140.    /* The Draw module overrides these functions.
  141.     * Always create the blitter before Draw. */
  142.    void   (*bind_fs_state)(struct pipe_context *, void *);
  143.    void   (*delete_fs_state)(struct pipe_context *, void *);
  144. };
  145.  
  146. static struct pipe_surface *
  147. util_blitter_get_next_surface_layer(struct pipe_context *pipe,
  148.                                     struct pipe_surface *surf);
  149.  
  150. struct blitter_context *util_blitter_create(struct pipe_context *pipe)
  151. {
  152.    struct blitter_context_priv *ctx;
  153.    struct pipe_blend_state blend;
  154.    struct pipe_depth_stencil_alpha_state dsa;
  155.    struct pipe_rasterizer_state rs_state;
  156.    struct pipe_sampler_state sampler_state;
  157.    struct pipe_vertex_element velem[2];
  158.    unsigned i;
  159.  
  160.    ctx = CALLOC_STRUCT(blitter_context_priv);
  161.    if (!ctx)
  162.       return NULL;
  163.  
  164.    ctx->base.pipe = pipe;
  165.    ctx->base.draw_rectangle = util_blitter_draw_rectangle;
  166.    ctx->base.get_next_surface_layer = util_blitter_get_next_surface_layer;
  167.  
  168.    ctx->bind_fs_state = pipe->bind_fs_state;
  169.    ctx->delete_fs_state = pipe->delete_fs_state;
  170.  
  171.    /* init state objects for them to be considered invalid */
  172.    ctx->base.saved_blend_state = INVALID_PTR;
  173.    ctx->base.saved_dsa_state = INVALID_PTR;
  174.    ctx->base.saved_rs_state = INVALID_PTR;
  175.    ctx->base.saved_fs = INVALID_PTR;
  176.    ctx->base.saved_vs = INVALID_PTR;
  177.    ctx->base.saved_gs = INVALID_PTR;
  178.    ctx->base.saved_velem_state = INVALID_PTR;
  179.    ctx->base.saved_fb_state.nr_cbufs = ~0;
  180.    ctx->base.saved_num_sampler_views = ~0;
  181.    ctx->base.saved_num_sampler_states = ~0;
  182.    ctx->base.saved_num_so_targets = ~0;
  183.  
  184.    ctx->has_geometry_shader =
  185.       pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_GEOMETRY,
  186.                                      PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0;
  187.  
  188.    ctx->has_tessellation =
  189.       pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_TESS_CTRL,
  190.                                      PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0;
  191.  
  192.    ctx->has_stream_out =
  193.       pipe->screen->get_param(pipe->screen,
  194.                               PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS) != 0;
  195.  
  196.    ctx->has_stencil_export =
  197.          pipe->screen->get_param(pipe->screen,
  198.                                  PIPE_CAP_SHADER_STENCIL_EXPORT);
  199.  
  200.    ctx->has_texture_multisample =
  201.       pipe->screen->get_param(pipe->screen, PIPE_CAP_TEXTURE_MULTISAMPLE);
  202.  
  203.    /* blend state objects */
  204.    memset(&blend, 0, sizeof(blend));
  205.  
  206.    for (i = 0; i <= PIPE_MASK_RGBA; i++) {
  207.       blend.rt[0].colormask = i;
  208.       ctx->blend[i] = pipe->create_blend_state(pipe, &blend);
  209.    }
  210.  
  211.    /* depth stencil alpha state objects */
  212.    memset(&dsa, 0, sizeof(dsa));
  213.    ctx->dsa_keep_depth_stencil =
  214.       pipe->create_depth_stencil_alpha_state(pipe, &dsa);
  215.  
  216.    dsa.depth.enabled = 1;
  217.    dsa.depth.writemask = 1;
  218.    dsa.depth.func = PIPE_FUNC_ALWAYS;
  219.    ctx->dsa_write_depth_keep_stencil =
  220.       pipe->create_depth_stencil_alpha_state(pipe, &dsa);
  221.  
  222.    dsa.stencil[0].enabled = 1;
  223.    dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
  224.    dsa.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
  225.    dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
  226.    dsa.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
  227.    dsa.stencil[0].valuemask = 0xff;
  228.    dsa.stencil[0].writemask = 0xff;
  229.    ctx->dsa_write_depth_stencil =
  230.       pipe->create_depth_stencil_alpha_state(pipe, &dsa);
  231.  
  232.    dsa.depth.enabled = 0;
  233.    dsa.depth.writemask = 0;
  234.    ctx->dsa_keep_depth_write_stencil =
  235.       pipe->create_depth_stencil_alpha_state(pipe, &dsa);
  236.  
  237.    /* sampler state */
  238.    memset(&sampler_state, 0, sizeof(sampler_state));
  239.    sampler_state.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
  240.    sampler_state.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
  241.    sampler_state.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
  242.    sampler_state.normalized_coords = 1;
  243.    ctx->sampler_state = pipe->create_sampler_state(pipe, &sampler_state);
  244.    sampler_state.normalized_coords = 0;
  245.    ctx->sampler_state_rect = pipe->create_sampler_state(pipe, &sampler_state);
  246.  
  247.    sampler_state.min_img_filter = PIPE_TEX_FILTER_LINEAR;
  248.    sampler_state.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
  249.    sampler_state.normalized_coords = 1;
  250.    ctx->sampler_state_linear = pipe->create_sampler_state(pipe, &sampler_state);
  251.    sampler_state.normalized_coords = 0;
  252.    ctx->sampler_state_rect_linear = pipe->create_sampler_state(pipe, &sampler_state);
  253.  
  254.    /* rasterizer state */
  255.    memset(&rs_state, 0, sizeof(rs_state));
  256.    rs_state.cull_face = PIPE_FACE_NONE;
  257.    rs_state.half_pixel_center = 1;
  258.    rs_state.bottom_edge_rule = 1;
  259.    rs_state.flatshade = 1;
  260.    rs_state.depth_clip = 1;
  261.    ctx->rs_state = pipe->create_rasterizer_state(pipe, &rs_state);
  262.  
  263.    rs_state.scissor = 1;
  264.    ctx->rs_state_scissor = pipe->create_rasterizer_state(pipe, &rs_state);
  265.  
  266.    if (ctx->has_stream_out) {
  267.       rs_state.scissor = 0;
  268.       rs_state.rasterizer_discard = 1;
  269.       ctx->rs_discard_state = pipe->create_rasterizer_state(pipe, &rs_state);
  270.    }
  271.  
  272.    ctx->base.vb_slot = 0; /* 0 for now */
  273.  
  274.    /* vertex elements states */
  275.    memset(&velem[0], 0, sizeof(velem[0]) * 2);
  276.    for (i = 0; i < 2; i++) {
  277.       velem[i].src_offset = i * 4 * sizeof(float);
  278.       velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
  279.       velem[i].vertex_buffer_index = ctx->base.vb_slot;
  280.    }
  281.    ctx->velem_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]);
  282.  
  283.    if (ctx->has_stream_out) {
  284.       static enum pipe_format formats[4] = {
  285.          PIPE_FORMAT_R32_UINT,
  286.          PIPE_FORMAT_R32G32_UINT,
  287.          PIPE_FORMAT_R32G32B32_UINT,
  288.          PIPE_FORMAT_R32G32B32A32_UINT
  289.       };
  290.  
  291.       for (i = 0; i < 4; i++) {
  292.          velem[0].src_format = formats[i];
  293.          velem[0].vertex_buffer_index = ctx->base.vb_slot;
  294.          ctx->velem_state_readbuf[i] =
  295.                pipe->create_vertex_elements_state(pipe, 1, &velem[0]);
  296.       }
  297.    }
  298.  
  299.    ctx->has_layered =
  300.       pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_INSTANCEID) &&
  301.       pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT);
  302.  
  303.    /* set invariant vertex coordinates */
  304.    for (i = 0; i < 4; i++)
  305.       ctx->vertices[i][0][3] = 1; /*v.w*/
  306.  
  307.    ctx->upload = u_upload_create(pipe, 65536, 4, PIPE_BIND_VERTEX_BUFFER);
  308.  
  309.    return &ctx->base;
  310. }
  311.  
  312. static void bind_vs_pos_only(struct blitter_context_priv *ctx)
  313. {
  314.    struct pipe_context *pipe = ctx->base.pipe;
  315.  
  316.    if (!ctx->vs_pos_only) {
  317.       struct pipe_stream_output_info so;
  318.       const uint semantic_names[] = { TGSI_SEMANTIC_POSITION };
  319.       const uint semantic_indices[] = { 0 };
  320.  
  321.       memset(&so, 0, sizeof(so));
  322.       so.num_outputs = 1;
  323.       so.output[0].num_components = 1;
  324.       so.stride[0] = 1;
  325.  
  326.       ctx->vs_pos_only =
  327.          util_make_vertex_passthrough_shader_with_so(pipe, 1, semantic_names,
  328.                                                      semantic_indices, FALSE,
  329.                                                      &so);
  330.    }
  331.  
  332.    pipe->bind_vs_state(pipe, ctx->vs_pos_only);
  333. }
  334.  
  335. static void bind_vs_passthrough(struct blitter_context_priv *ctx)
  336. {
  337.    struct pipe_context *pipe = ctx->base.pipe;
  338.  
  339.    if (!ctx->vs) {
  340.       const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
  341.                                       TGSI_SEMANTIC_GENERIC };
  342.       const uint semantic_indices[] = { 0, 0 };
  343.       ctx->vs =
  344.          util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
  345.                                              semantic_indices, FALSE);
  346.    }
  347.  
  348.    pipe->bind_vs_state(pipe, ctx->vs);
  349. }
  350.  
  351. static void bind_vs_layered(struct blitter_context_priv *ctx)
  352. {
  353.    struct pipe_context *pipe = ctx->base.pipe;
  354.  
  355.    if (!ctx->vs_layered) {
  356.       ctx->vs_layered = util_make_layered_clear_vertex_shader(pipe);
  357.    }
  358.  
  359.    pipe->bind_vs_state(pipe, ctx->vs_layered);
  360. }
  361.  
  362. static void bind_fs_empty(struct blitter_context_priv *ctx)
  363. {
  364.    struct pipe_context *pipe = ctx->base.pipe;
  365.  
  366.    if (!ctx->fs_empty) {
  367.       assert(!ctx->cached_all_shaders);
  368.       ctx->fs_empty = util_make_empty_fragment_shader(pipe);
  369.    }
  370.  
  371.    ctx->bind_fs_state(pipe, ctx->fs_empty);
  372. }
  373.  
  374. static void bind_fs_write_one_cbuf(struct blitter_context_priv *ctx)
  375. {
  376.    struct pipe_context *pipe = ctx->base.pipe;
  377.  
  378.    if (!ctx->fs_write_one_cbuf) {
  379.       assert(!ctx->cached_all_shaders);
  380.       ctx->fs_write_one_cbuf =
  381.          util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
  382.                                                TGSI_INTERPOLATE_CONSTANT, FALSE);
  383.    }
  384.  
  385.    ctx->bind_fs_state(pipe, ctx->fs_write_one_cbuf);
  386. }
  387.  
  388. static void bind_fs_write_all_cbufs(struct blitter_context_priv *ctx)
  389. {
  390.    struct pipe_context *pipe = ctx->base.pipe;
  391.  
  392.    if (!ctx->fs_write_all_cbufs) {
  393.       assert(!ctx->cached_all_shaders);
  394.       ctx->fs_write_all_cbufs =
  395.          util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
  396.                                                TGSI_INTERPOLATE_CONSTANT, TRUE);
  397.    }
  398.  
  399.    ctx->bind_fs_state(pipe, ctx->fs_write_all_cbufs);
  400. }
  401.  
  402. void util_blitter_destroy(struct blitter_context *blitter)
  403. {
  404.    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
  405.    struct pipe_context *pipe = blitter->pipe;
  406.    int i, j, f;
  407.  
  408.    for (i = 0; i <= PIPE_MASK_RGBA; i++) {
  409.       pipe->delete_blend_state(pipe, ctx->blend[i]);
  410.    }
  411.    for (i = 0; i < Elements(ctx->blend_clear); i++) {
  412.       if (ctx->blend_clear[i])
  413.          pipe->delete_blend_state(pipe, ctx->blend_clear[i]);
  414.    }
  415.    pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
  416.    pipe->delete_depth_stencil_alpha_state(pipe,
  417.                                           ctx->dsa_write_depth_keep_stencil);
  418.    pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
  419.    pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
  420.  
  421.    pipe->delete_rasterizer_state(pipe, ctx->rs_state);
  422.    pipe->delete_rasterizer_state(pipe, ctx->rs_state_scissor);
  423.    if (ctx->rs_discard_state)
  424.       pipe->delete_rasterizer_state(pipe, ctx->rs_discard_state);
  425.    if (ctx->vs)
  426.       pipe->delete_vs_state(pipe, ctx->vs);
  427.    if (ctx->vs_pos_only)
  428.       pipe->delete_vs_state(pipe, ctx->vs_pos_only);
  429.    if (ctx->vs_layered)
  430.       pipe->delete_vs_state(pipe, ctx->vs_layered);
  431.    pipe->delete_vertex_elements_state(pipe, ctx->velem_state);
  432.    for (i = 0; i < 4; i++) {
  433.       if (ctx->velem_state_readbuf[i]) {
  434.          pipe->delete_vertex_elements_state(pipe, ctx->velem_state_readbuf[i]);
  435.       }
  436.    }
  437.  
  438.    for (i = 0; i < PIPE_MAX_TEXTURE_TYPES; i++) {
  439.       if (ctx->fs_texfetch_col[i])
  440.          ctx->delete_fs_state(pipe, ctx->fs_texfetch_col[i]);
  441.       if (ctx->fs_texfetch_depth[i])
  442.          ctx->delete_fs_state(pipe, ctx->fs_texfetch_depth[i]);
  443.       if (ctx->fs_texfetch_depthstencil[i])
  444.          ctx->delete_fs_state(pipe, ctx->fs_texfetch_depthstencil[i]);
  445.       if (ctx->fs_texfetch_stencil[i])
  446.          ctx->delete_fs_state(pipe, ctx->fs_texfetch_stencil[i]);
  447.  
  448.       if (ctx->fs_texfetch_col_msaa[i])
  449.          ctx->delete_fs_state(pipe, ctx->fs_texfetch_col_msaa[i]);
  450.       if (ctx->fs_texfetch_depth_msaa[i])
  451.          ctx->delete_fs_state(pipe, ctx->fs_texfetch_depth_msaa[i]);
  452.       if (ctx->fs_texfetch_depthstencil_msaa[i])
  453.          ctx->delete_fs_state(pipe, ctx->fs_texfetch_depthstencil_msaa[i]);
  454.       if (ctx->fs_texfetch_stencil_msaa[i])
  455.          ctx->delete_fs_state(pipe, ctx->fs_texfetch_stencil_msaa[i]);
  456.  
  457.       for (j = 0; j< Elements(ctx->fs_resolve[i]); j++)
  458.          for (f = 0; f < 2; f++)
  459.             if (ctx->fs_resolve[i][j][f])
  460.                ctx->delete_fs_state(pipe, ctx->fs_resolve[i][j][f]);
  461.  
  462.       for (j = 0; j< Elements(ctx->fs_resolve_sint[i]); j++)
  463.          for (f = 0; f < 2; f++)
  464.             if (ctx->fs_resolve_sint[i][j][f])
  465.                ctx->delete_fs_state(pipe, ctx->fs_resolve_sint[i][j][f]);
  466.  
  467.       for (j = 0; j< Elements(ctx->fs_resolve_uint[i]); j++)
  468.          for (f = 0; f < 2; f++)
  469.             if (ctx->fs_resolve_uint[i][j][f])
  470.                ctx->delete_fs_state(pipe, ctx->fs_resolve_uint[i][j][f]);
  471.    }
  472.  
  473.    if (ctx->fs_empty)
  474.       ctx->delete_fs_state(pipe, ctx->fs_empty);
  475.    if (ctx->fs_write_one_cbuf)
  476.       ctx->delete_fs_state(pipe, ctx->fs_write_one_cbuf);
  477.    if (ctx->fs_write_all_cbufs)
  478.       ctx->delete_fs_state(pipe, ctx->fs_write_all_cbufs);
  479.  
  480.    pipe->delete_sampler_state(pipe, ctx->sampler_state_rect_linear);
  481.    pipe->delete_sampler_state(pipe, ctx->sampler_state_rect);
  482.    pipe->delete_sampler_state(pipe, ctx->sampler_state_linear);
  483.    pipe->delete_sampler_state(pipe, ctx->sampler_state);
  484.    u_upload_destroy(ctx->upload);
  485.    FREE(ctx);
  486. }
  487.  
  488. void util_blitter_set_texture_multisample(struct blitter_context *blitter,
  489.                                           boolean supported)
  490. {
  491.    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
  492.  
  493.    ctx->has_texture_multisample = supported;
  494. }
  495.  
  496. static void blitter_set_running_flag(struct blitter_context_priv *ctx)
  497. {
  498.    if (ctx->base.running) {
  499.       _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
  500.                     __LINE__);
  501.    }
  502.    ctx->base.running = TRUE;
  503. }
  504.  
  505. static void blitter_unset_running_flag(struct blitter_context_priv *ctx)
  506. {
  507.    if (!ctx->base.running) {
  508.       _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
  509.                     __LINE__);
  510.    }
  511.    ctx->base.running = FALSE;
  512. }
  513.  
  514. static void blitter_check_saved_vertex_states(struct blitter_context_priv *ctx)
  515. {
  516.    assert(ctx->base.saved_velem_state != INVALID_PTR);
  517.    assert(ctx->base.saved_vs != INVALID_PTR);
  518.    assert(!ctx->has_geometry_shader || ctx->base.saved_gs != INVALID_PTR);
  519.    assert(!ctx->has_tessellation || ctx->base.saved_tcs != INVALID_PTR);
  520.    assert(!ctx->has_tessellation || ctx->base.saved_tes != INVALID_PTR);
  521.    assert(!ctx->has_stream_out || ctx->base.saved_num_so_targets != ~0);
  522.    assert(ctx->base.saved_rs_state != INVALID_PTR);
  523. }
  524.  
  525. static void blitter_restore_vertex_states(struct blitter_context_priv *ctx)
  526. {
  527.    struct pipe_context *pipe = ctx->base.pipe;
  528.    unsigned i;
  529.  
  530.    /* Vertex buffer. */
  531.    pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1,
  532.                             &ctx->base.saved_vertex_buffer);
  533.    pipe_resource_reference(&ctx->base.saved_vertex_buffer.buffer, NULL);
  534.  
  535.    /* Vertex elements. */
  536.    pipe->bind_vertex_elements_state(pipe, ctx->base.saved_velem_state);
  537.    ctx->base.saved_velem_state = INVALID_PTR;
  538.  
  539.    /* Vertex shader. */
  540.    pipe->bind_vs_state(pipe, ctx->base.saved_vs);
  541.    ctx->base.saved_vs = INVALID_PTR;
  542.  
  543.    /* Geometry shader. */
  544.    if (ctx->has_geometry_shader) {
  545.       pipe->bind_gs_state(pipe, ctx->base.saved_gs);
  546.       ctx->base.saved_gs = INVALID_PTR;
  547.    }
  548.  
  549.    if (ctx->has_tessellation) {
  550.       pipe->bind_tcs_state(pipe, ctx->base.saved_tcs);
  551.       pipe->bind_tes_state(pipe, ctx->base.saved_tes);
  552.       ctx->base.saved_tcs = INVALID_PTR;
  553.       ctx->base.saved_tes = INVALID_PTR;
  554.    }
  555.  
  556.    /* Stream outputs. */
  557.    if (ctx->has_stream_out) {
  558.       unsigned offsets[PIPE_MAX_SO_BUFFERS];
  559.       for (i = 0; i < ctx->base.saved_num_so_targets; i++)
  560.          offsets[i] = (unsigned)-1;
  561.       pipe->set_stream_output_targets(pipe,
  562.                                       ctx->base.saved_num_so_targets,
  563.                                       ctx->base.saved_so_targets, offsets);
  564.  
  565.       for (i = 0; i < ctx->base.saved_num_so_targets; i++)
  566.          pipe_so_target_reference(&ctx->base.saved_so_targets[i], NULL);
  567.  
  568.       ctx->base.saved_num_so_targets = ~0;
  569.    }
  570.  
  571.    /* Rasterizer. */
  572.    pipe->bind_rasterizer_state(pipe, ctx->base.saved_rs_state);
  573.    ctx->base.saved_rs_state = INVALID_PTR;
  574. }
  575.  
  576. static void blitter_check_saved_fragment_states(struct blitter_context_priv *ctx)
  577. {
  578.    assert(ctx->base.saved_fs != INVALID_PTR);
  579.    assert(ctx->base.saved_dsa_state != INVALID_PTR);
  580.    assert(ctx->base.saved_blend_state != INVALID_PTR);
  581. }
  582.  
  583. static void blitter_restore_fragment_states(struct blitter_context_priv *ctx)
  584. {
  585.    struct pipe_context *pipe = ctx->base.pipe;
  586.  
  587.    /* Fragment shader. */
  588.    ctx->bind_fs_state(pipe, ctx->base.saved_fs);
  589.    ctx->base.saved_fs = INVALID_PTR;
  590.  
  591.    /* Depth, stencil, alpha. */
  592.    pipe->bind_depth_stencil_alpha_state(pipe, ctx->base.saved_dsa_state);
  593.    ctx->base.saved_dsa_state = INVALID_PTR;
  594.  
  595.    /* Blend state. */
  596.    pipe->bind_blend_state(pipe, ctx->base.saved_blend_state);
  597.    ctx->base.saved_blend_state = INVALID_PTR;
  598.  
  599.    /* Sample mask. */
  600.    if (ctx->base.is_sample_mask_saved) {
  601.       pipe->set_sample_mask(pipe, ctx->base.saved_sample_mask);
  602.       ctx->base.is_sample_mask_saved = FALSE;
  603.    }
  604.  
  605.    /* Miscellaneous states. */
  606.    /* XXX check whether these are saved and whether they need to be restored
  607.     * (depending on the operation) */
  608.    pipe->set_stencil_ref(pipe, &ctx->base.saved_stencil_ref);
  609.    pipe->set_viewport_states(pipe, 0, 1, &ctx->base.saved_viewport);
  610. }
  611.  
  612. static void blitter_check_saved_fb_state(struct blitter_context_priv *ctx)
  613. {
  614.    assert(ctx->base.saved_fb_state.nr_cbufs != ~0);
  615. }
  616.  
  617. static void blitter_disable_render_cond(struct blitter_context_priv *ctx)
  618. {
  619.    struct pipe_context *pipe = ctx->base.pipe;
  620.  
  621.    if (ctx->base.saved_render_cond_query) {
  622.       pipe->render_condition(pipe, NULL, FALSE, 0);
  623.    }
  624. }
  625.  
  626. static void blitter_restore_render_cond(struct blitter_context_priv *ctx)
  627. {
  628.    struct pipe_context *pipe = ctx->base.pipe;
  629.  
  630.    if (ctx->base.saved_render_cond_query) {
  631.       pipe->render_condition(pipe, ctx->base.saved_render_cond_query,
  632.                              ctx->base.saved_render_cond_cond,
  633.                              ctx->base.saved_render_cond_mode);
  634.       ctx->base.saved_render_cond_query = NULL;
  635.    }
  636. }
  637.  
  638. static void blitter_restore_fb_state(struct blitter_context_priv *ctx)
  639. {
  640.    struct pipe_context *pipe = ctx->base.pipe;
  641.  
  642.    pipe->set_framebuffer_state(pipe, &ctx->base.saved_fb_state);
  643.    util_unreference_framebuffer_state(&ctx->base.saved_fb_state);
  644. }
  645.  
  646. static void blitter_check_saved_textures(struct blitter_context_priv *ctx)
  647. {
  648.    assert(ctx->base.saved_num_sampler_states != ~0);
  649.    assert(ctx->base.saved_num_sampler_views != ~0);
  650. }
  651.  
  652. static void blitter_restore_textures(struct blitter_context_priv *ctx)
  653. {
  654.    struct pipe_context *pipe = ctx->base.pipe;
  655.    unsigned i;
  656.  
  657.    /* Fragment sampler states. */
  658.    pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
  659.                              ctx->base.saved_num_sampler_states,
  660.                              ctx->base.saved_sampler_states);
  661.  
  662.    ctx->base.saved_num_sampler_states = ~0;
  663.  
  664.    /* Fragment sampler views. */
  665.    pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,
  666.                            ctx->base.saved_num_sampler_views,
  667.                            ctx->base.saved_sampler_views);
  668.  
  669.    for (i = 0; i < ctx->base.saved_num_sampler_views; i++)
  670.       pipe_sampler_view_reference(&ctx->base.saved_sampler_views[i], NULL);
  671.  
  672.    ctx->base.saved_num_sampler_views = ~0;
  673. }
  674.  
  675. static void blitter_set_rectangle(struct blitter_context_priv *ctx,
  676.                                   int x1, int y1, int x2, int y2,
  677.                                   float depth)
  678. {
  679.    int i;
  680.  
  681.    /* set vertex positions */
  682.    ctx->vertices[0][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v0.x*/
  683.    ctx->vertices[0][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v0.y*/
  684.  
  685.    ctx->vertices[1][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v1.x*/
  686.    ctx->vertices[1][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v1.y*/
  687.  
  688.    ctx->vertices[2][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v2.x*/
  689.    ctx->vertices[2][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v2.y*/
  690.  
  691.    ctx->vertices[3][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v3.x*/
  692.    ctx->vertices[3][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v3.y*/
  693.  
  694.    for (i = 0; i < 4; i++)
  695.       ctx->vertices[i][0][2] = depth; /*z*/
  696.  
  697.    /* viewport */
  698.    ctx->viewport.scale[0] = 0.5f * ctx->dst_width;
  699.    ctx->viewport.scale[1] = 0.5f * ctx->dst_height;
  700.    ctx->viewport.scale[2] = 1.0f;
  701.    ctx->viewport.translate[0] = 0.5f * ctx->dst_width;
  702.    ctx->viewport.translate[1] = 0.5f * ctx->dst_height;
  703.    ctx->viewport.translate[2] = 0.0f;
  704.    ctx->base.pipe->set_viewport_states(ctx->base.pipe, 0, 1, &ctx->viewport);
  705. }
  706.  
  707. static void blitter_set_clear_color(struct blitter_context_priv *ctx,
  708.                                     const union pipe_color_union *color)
  709. {
  710.    int i;
  711.  
  712.    if (color) {
  713.       for (i = 0; i < 4; i++) {
  714.          uint32_t *uiverts = (uint32_t *)ctx->vertices[i][1];
  715.          uiverts[0] = color->ui[0];
  716.          uiverts[1] = color->ui[1];
  717.          uiverts[2] = color->ui[2];
  718.          uiverts[3] = color->ui[3];
  719.       }
  720.    } else {
  721.       for (i = 0; i < 4; i++) {
  722.          ctx->vertices[i][1][0] = 0;
  723.          ctx->vertices[i][1][1] = 0;
  724.          ctx->vertices[i][1][2] = 0;
  725.          ctx->vertices[i][1][3] = 0;
  726.       }
  727.    }
  728. }
  729.  
  730. static void get_texcoords(struct pipe_sampler_view *src,
  731.                           unsigned src_width0, unsigned src_height0,
  732.                           int x1, int y1, int x2, int y2,
  733.                           float out[4])
  734. {
  735.    struct pipe_resource *tex = src->texture;
  736.    unsigned level = src->u.tex.first_level;
  737.    boolean normalized = tex->target != PIPE_TEXTURE_RECT &&
  738.                         tex->nr_samples <= 1;
  739.  
  740.    if (normalized) {
  741.       out[0] = x1 / (float)u_minify(src_width0,  level);
  742.       out[1] = y1 / (float)u_minify(src_height0, level);
  743.       out[2] = x2 / (float)u_minify(src_width0,  level);
  744.       out[3] = y2 / (float)u_minify(src_height0, level);
  745.    } else {
  746.       out[0] = (float) x1;
  747.       out[1] = (float) y1;
  748.       out[2] = (float) x2;
  749.       out[3] = (float) y2;
  750.    }
  751. }
  752.  
  753. static void set_texcoords_in_vertices(const float coord[4],
  754.                                       float *out, unsigned stride)
  755. {
  756.    out[0] = coord[0]; /*t0.s*/
  757.    out[1] = coord[1]; /*t0.t*/
  758.    out += stride;
  759.    out[0] = coord[2]; /*t1.s*/
  760.    out[1] = coord[1]; /*t1.t*/
  761.    out += stride;
  762.    out[0] = coord[2]; /*t2.s*/
  763.    out[1] = coord[3]; /*t2.t*/
  764.    out += stride;
  765.    out[0] = coord[0]; /*t3.s*/
  766.    out[1] = coord[3]; /*t3.t*/
  767. }
  768.  
  769. static void blitter_set_texcoords(struct blitter_context_priv *ctx,
  770.                                   struct pipe_sampler_view *src,
  771.                                   unsigned src_width0, unsigned src_height0,
  772.                                   float layer, unsigned sample,
  773.                                   int x1, int y1, int x2, int y2)
  774. {
  775.    unsigned i;
  776.    float coord[4];
  777.    float face_coord[4][2];
  778.  
  779.    get_texcoords(src, src_width0, src_height0, x1, y1, x2, y2, coord);
  780.  
  781.    if (src->texture->target == PIPE_TEXTURE_CUBE ||
  782.        src->texture->target == PIPE_TEXTURE_CUBE_ARRAY) {
  783.       set_texcoords_in_vertices(coord, &face_coord[0][0], 2);
  784.       util_map_texcoords2d_onto_cubemap((unsigned)layer % 6,
  785.                                         /* pointer, stride in floats */
  786.                                         &face_coord[0][0], 2,
  787.                                         &ctx->vertices[0][1][0], 8,
  788.                                         FALSE);
  789.    } else {
  790.       set_texcoords_in_vertices(coord, &ctx->vertices[0][1][0], 8);
  791.    }
  792.  
  793.    /* Set the layer. */
  794.    switch (src->texture->target) {
  795.    case PIPE_TEXTURE_3D:
  796.       {
  797.          float r = layer / (float)u_minify(src->texture->depth0,
  798.                                            src->u.tex.first_level);
  799.          for (i = 0; i < 4; i++)
  800.             ctx->vertices[i][1][2] = r; /*r*/
  801.       }
  802.       break;
  803.  
  804.    case PIPE_TEXTURE_1D_ARRAY:
  805.       for (i = 0; i < 4; i++)
  806.          ctx->vertices[i][1][1] = (float) layer; /*t*/
  807.       break;
  808.  
  809.    case PIPE_TEXTURE_2D_ARRAY:
  810.       for (i = 0; i < 4; i++) {
  811.          ctx->vertices[i][1][2] = (float) layer;  /*r*/
  812.          ctx->vertices[i][1][3] = (float) sample; /*q*/
  813.       }
  814.       break;
  815.  
  816.    case PIPE_TEXTURE_CUBE_ARRAY:
  817.       for (i = 0; i < 4; i++)
  818.          ctx->vertices[i][1][3] = (float) ((unsigned)layer / 6); /*w*/
  819.       break;
  820.  
  821.    case PIPE_TEXTURE_2D:
  822.       for (i = 0; i < 4; i++) {
  823.          ctx->vertices[i][1][3] = (float) sample; /*r*/
  824.       }
  825.       break;
  826.  
  827.    default:;
  828.    }
  829. }
  830.  
  831. static void blitter_set_dst_dimensions(struct blitter_context_priv *ctx,
  832.                                        unsigned width, unsigned height)
  833. {
  834.    ctx->dst_width = width;
  835.    ctx->dst_height = height;
  836. }
  837.  
  838. static void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx,
  839.                                          enum pipe_format format,
  840.                                          enum pipe_texture_target target,
  841.                                          unsigned src_nr_samples,
  842.                                          unsigned dst_nr_samples,
  843.                                          unsigned filter)
  844. {
  845.    struct pipe_context *pipe = ctx->base.pipe;
  846.    unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(target, src_nr_samples);
  847.  
  848.    assert(target < PIPE_MAX_TEXTURE_TYPES);
  849.  
  850.    if (src_nr_samples > 1) {
  851.       void **shader;
  852.  
  853.       if (dst_nr_samples <= 1) {
  854.          /* The destination has one sample, so we'll do color resolve. */
  855.          boolean is_uint, is_sint;
  856.          unsigned index = GET_MSAA_RESOLVE_FS_IDX(src_nr_samples);
  857.  
  858.          is_uint = util_format_is_pure_uint(format);
  859.          is_sint = util_format_is_pure_sint(format);
  860.  
  861.          assert(filter < 2);
  862.  
  863.          if (is_uint)
  864.             shader = &ctx->fs_resolve_uint[target][index][filter];
  865.          else if (is_sint)
  866.             shader = &ctx->fs_resolve_sint[target][index][filter];
  867.          else
  868.             shader = &ctx->fs_resolve[target][index][filter];
  869.  
  870.          if (!*shader) {
  871.             assert(!ctx->cached_all_shaders);
  872.             if (filter == PIPE_TEX_FILTER_LINEAR) {
  873.                *shader = util_make_fs_msaa_resolve_bilinear(pipe, tgsi_tex,
  874.                                                    src_nr_samples,
  875.                                                    is_uint, is_sint);
  876.             }
  877.             else {
  878.                *shader = util_make_fs_msaa_resolve(pipe, tgsi_tex,
  879.                                                    src_nr_samples,
  880.                                                    is_uint, is_sint);
  881.             }
  882.          }
  883.       }
  884.       else {
  885.          /* The destination has multiple samples, we'll do
  886.           * an MSAA->MSAA copy.
  887.           */
  888.          shader = &ctx->fs_texfetch_col_msaa[target];
  889.  
  890.          /* Create the fragment shader on-demand. */
  891.          if (!*shader) {
  892.             assert(!ctx->cached_all_shaders);
  893.             *shader = util_make_fs_blit_msaa_color(pipe, tgsi_tex);
  894.          }
  895.       }
  896.  
  897.       return *shader;
  898.    } else {
  899.       void **shader = &ctx->fs_texfetch_col[target];
  900.  
  901.       /* Create the fragment shader on-demand. */
  902.       if (!*shader) {
  903.          assert(!ctx->cached_all_shaders);
  904.          *shader = util_make_fragment_tex_shader(pipe, tgsi_tex,
  905.                                                  TGSI_INTERPOLATE_LINEAR);
  906.       }
  907.  
  908.       return *shader;
  909.    }
  910. }
  911.  
  912. static INLINE
  913. void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx,
  914.                                     enum pipe_texture_target target,
  915.                                     unsigned nr_samples)
  916. {
  917.    struct pipe_context *pipe = ctx->base.pipe;
  918.  
  919.    assert(target < PIPE_MAX_TEXTURE_TYPES);
  920.  
  921.    if (nr_samples > 1) {
  922.       void **shader = &ctx->fs_texfetch_depth_msaa[target];
  923.  
  924.       /* Create the fragment shader on-demand. */
  925.       if (!*shader) {
  926.          unsigned tgsi_tex;
  927.          assert(!ctx->cached_all_shaders);
  928.          tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
  929.          *shader = util_make_fs_blit_msaa_depth(pipe, tgsi_tex);
  930.       }
  931.  
  932.       return *shader;
  933.    } else {
  934.       void **shader = &ctx->fs_texfetch_depth[target];
  935.  
  936.       /* Create the fragment shader on-demand. */
  937.       if (!*shader) {
  938.          unsigned tgsi_tex;
  939.          assert(!ctx->cached_all_shaders);
  940.          tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
  941.          *shader =
  942.             util_make_fragment_tex_shader_writedepth(pipe, tgsi_tex,
  943.                                                      TGSI_INTERPOLATE_LINEAR);
  944.       }
  945.  
  946.       return *shader;
  947.    }
  948. }
  949.  
  950. static INLINE
  951. void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv *ctx,
  952.                                            enum pipe_texture_target target,
  953.                                            unsigned nr_samples)
  954. {
  955.    struct pipe_context *pipe = ctx->base.pipe;
  956.  
  957.    assert(target < PIPE_MAX_TEXTURE_TYPES);
  958.  
  959.    if (nr_samples > 1) {
  960.       void **shader = &ctx->fs_texfetch_depthstencil_msaa[target];
  961.  
  962.       /* Create the fragment shader on-demand. */
  963.       if (!*shader) {
  964.          unsigned tgsi_tex;
  965.          assert(!ctx->cached_all_shaders);
  966.          tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
  967.          *shader = util_make_fs_blit_msaa_depthstencil(pipe, tgsi_tex);
  968.       }
  969.  
  970.       return *shader;
  971.    } else {
  972.       void **shader = &ctx->fs_texfetch_depthstencil[target];
  973.  
  974.       /* Create the fragment shader on-demand. */
  975.       if (!*shader) {
  976.          unsigned tgsi_tex;
  977.          assert(!ctx->cached_all_shaders);
  978.          tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
  979.          *shader =
  980.             util_make_fragment_tex_shader_writedepthstencil(pipe, tgsi_tex,
  981.                                                      TGSI_INTERPOLATE_LINEAR);
  982.       }
  983.  
  984.       return *shader;
  985.    }
  986. }
  987.  
  988. static INLINE
  989. void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv *ctx,
  990.                                       enum pipe_texture_target target,
  991.                                       unsigned nr_samples)
  992. {
  993.    struct pipe_context *pipe = ctx->base.pipe;
  994.  
  995.    assert(target < PIPE_MAX_TEXTURE_TYPES);
  996.  
  997.    if (nr_samples > 1) {
  998.       void **shader = &ctx->fs_texfetch_stencil_msaa[target];
  999.  
  1000.       /* Create the fragment shader on-demand. */
  1001.       if (!*shader) {
  1002.          unsigned tgsi_tex;
  1003.          assert(!ctx->cached_all_shaders);
  1004.          tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
  1005.          *shader = util_make_fs_blit_msaa_stencil(pipe, tgsi_tex);
  1006.       }
  1007.  
  1008.       return *shader;
  1009.    } else {
  1010.       void **shader = &ctx->fs_texfetch_stencil[target];
  1011.  
  1012.       /* Create the fragment shader on-demand. */
  1013.       if (!*shader) {
  1014.          unsigned tgsi_tex;
  1015.          assert(!ctx->cached_all_shaders);
  1016.          tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
  1017.          *shader =
  1018.             util_make_fragment_tex_shader_writestencil(pipe, tgsi_tex,
  1019.                                                        TGSI_INTERPOLATE_LINEAR);
  1020.       }
  1021.  
  1022.       return *shader;
  1023.    }
  1024. }
  1025.  
  1026.  
  1027. /**
  1028.  * Generate and save all fragment shaders that we will ever need for
  1029.  * blitting.  Drivers which use the 'draw' fallbacks will typically use
  1030.  * this to make sure we generate/use shaders that don't go through the
  1031.  * draw module's wrapper functions.
  1032.  */
  1033. void util_blitter_cache_all_shaders(struct blitter_context *blitter)
  1034. {
  1035.    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
  1036.    struct pipe_context *pipe = blitter->pipe;
  1037.    struct pipe_screen *screen = pipe->screen;
  1038.    unsigned samples, j, f, target, max_samples;
  1039.    boolean has_arraytex, has_cubearraytex;
  1040.  
  1041.    max_samples = ctx->has_texture_multisample ? 2 : 1;
  1042.    has_arraytex = screen->get_param(screen,
  1043.                                     PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS) != 0;
  1044.    has_cubearraytex = screen->get_param(screen,
  1045.                                     PIPE_CAP_CUBE_MAP_ARRAY) != 0;
  1046.  
  1047.    /* It only matters if i <= 1 or > 1. */
  1048.    for (samples = 1; samples <= max_samples; samples++) {
  1049.       for (target = PIPE_TEXTURE_1D; target < PIPE_MAX_TEXTURE_TYPES; target++) {
  1050.          if (!has_arraytex &&
  1051.              (target == PIPE_TEXTURE_1D_ARRAY ||
  1052.               target == PIPE_TEXTURE_2D_ARRAY)) {
  1053.             continue;
  1054.          }
  1055.          if (!has_cubearraytex &&
  1056.              (target == PIPE_TEXTURE_CUBE_ARRAY))
  1057.             continue;
  1058.  
  1059.          if (samples > 1 &&
  1060.              (target != PIPE_TEXTURE_2D &&
  1061.               target != PIPE_TEXTURE_2D_ARRAY))
  1062.             continue;
  1063.  
  1064.          /* If samples == 1, the shaders read one texel. If samples >= 1,
  1065.           * they read one sample.
  1066.           */
  1067.          blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT, target,
  1068.                                      samples, samples, 0);
  1069.          blitter_get_fs_texfetch_depth(ctx, target, samples);
  1070.          if (ctx->has_stencil_export) {
  1071.             blitter_get_fs_texfetch_depthstencil(ctx, target, samples);
  1072.             blitter_get_fs_texfetch_stencil(ctx, target, samples);
  1073.          }
  1074.  
  1075.          if (samples == 1)
  1076.             continue;
  1077.  
  1078.          /* MSAA resolve shaders. */
  1079.          for (j = 2; j < 32; j++) {
  1080.             if (!screen->is_format_supported(screen, PIPE_FORMAT_R32_FLOAT,
  1081.                                              target, j,
  1082.                                              PIPE_BIND_SAMPLER_VIEW)) {
  1083.                continue;
  1084.             }
  1085.  
  1086.             for (f = 0; f < 2; f++) {
  1087.                blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT, target,
  1088.                                            j, 1, f);
  1089.                blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT, target,
  1090.                                            j, 1, f);
  1091.                blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT, target,
  1092.                                            j, 1, f);
  1093.             }
  1094.          }
  1095.       }
  1096.    }
  1097.  
  1098.    ctx->fs_empty = util_make_empty_fragment_shader(pipe);
  1099.  
  1100.    ctx->fs_write_one_cbuf =
  1101.       util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
  1102.                                             TGSI_INTERPOLATE_CONSTANT, FALSE);
  1103.  
  1104.    ctx->fs_write_all_cbufs =
  1105.       util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
  1106.                                             TGSI_INTERPOLATE_CONSTANT, TRUE);
  1107.  
  1108.    ctx->cached_all_shaders = TRUE;
  1109. }
  1110.  
  1111. static void blitter_set_common_draw_rect_state(struct blitter_context_priv *ctx,
  1112.                                                boolean scissor,
  1113.                                                boolean vs_layered)
  1114. {
  1115.    struct pipe_context *pipe = ctx->base.pipe;
  1116.  
  1117.    pipe->bind_rasterizer_state(pipe, scissor ? ctx->rs_state_scissor
  1118.                                              : ctx->rs_state);
  1119.    if (vs_layered)
  1120.       bind_vs_layered(ctx);
  1121.    else
  1122.       bind_vs_passthrough(ctx);
  1123.  
  1124.    if (ctx->has_geometry_shader)
  1125.       pipe->bind_gs_state(pipe, NULL);
  1126.    if (ctx->has_tessellation) {
  1127.       pipe->bind_tcs_state(pipe, NULL);
  1128.       pipe->bind_tes_state(pipe, NULL);
  1129.    }
  1130.    if (ctx->has_stream_out)
  1131.       pipe->set_stream_output_targets(pipe, 0, NULL, NULL);
  1132. }
  1133.  
  1134. static void blitter_draw(struct blitter_context_priv *ctx,
  1135.                          int x1, int y1, int x2, int y2, float depth,
  1136.                          unsigned num_instances)
  1137. {
  1138.    struct pipe_context *pipe = ctx->base.pipe;
  1139.    struct pipe_vertex_buffer vb = {0};
  1140.  
  1141.    blitter_set_rectangle(ctx, x1, y1, x2, y2, depth);
  1142.  
  1143.    vb.stride = 8 * sizeof(float);
  1144.  
  1145.    u_upload_data(ctx->upload, 0, sizeof(ctx->vertices), ctx->vertices,
  1146.                  &vb.buffer_offset, &vb.buffer);
  1147.    u_upload_unmap(ctx->upload);
  1148.  
  1149.    pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
  1150.    util_draw_arrays_instanced(pipe, PIPE_PRIM_TRIANGLE_FAN, 0, 4,
  1151.                               0, num_instances);
  1152.    pipe_resource_reference(&vb.buffer, NULL);
  1153. }
  1154.  
  1155. void util_blitter_draw_rectangle(struct blitter_context *blitter,
  1156.                                  int x1, int y1, int x2, int y2, float depth,
  1157.                                  enum blitter_attrib_type type,
  1158.                                  const union pipe_color_union *attrib)
  1159. {
  1160.    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
  1161.  
  1162.    switch (type) {
  1163.       case UTIL_BLITTER_ATTRIB_COLOR:
  1164.          blitter_set_clear_color(ctx, attrib);
  1165.          break;
  1166.  
  1167.       case UTIL_BLITTER_ATTRIB_TEXCOORD:
  1168.          set_texcoords_in_vertices(attrib->f, &ctx->vertices[0][1][0], 8);
  1169.          break;
  1170.  
  1171.       default:;
  1172.    }
  1173.  
  1174.    blitter_draw(ctx, x1, y1, x2, y2, depth, 1);
  1175. }
  1176.  
  1177. static void *get_clear_blend_state(struct blitter_context_priv *ctx,
  1178.                                    unsigned clear_buffers)
  1179. {
  1180.    struct pipe_context *pipe = ctx->base.pipe;
  1181.    int index;
  1182.  
  1183.    clear_buffers &= PIPE_CLEAR_COLOR;
  1184.  
  1185.    /* Return an existing blend state. */
  1186.    if (!clear_buffers)
  1187.       return ctx->blend[0];
  1188.  
  1189.    index = GET_CLEAR_BLEND_STATE_IDX(clear_buffers);
  1190.  
  1191.    if (ctx->blend_clear[index])
  1192.       return ctx->blend_clear[index];
  1193.  
  1194.    /* Create a new one. */
  1195.    {
  1196.       struct pipe_blend_state blend = {0};
  1197.       unsigned i;
  1198.  
  1199.       blend.independent_blend_enable = 1;
  1200.  
  1201.       for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
  1202.          if (clear_buffers & (PIPE_CLEAR_COLOR0 << i)) {
  1203.             blend.rt[i].colormask = PIPE_MASK_RGBA;
  1204.          }
  1205.       }
  1206.  
  1207.       ctx->blend_clear[index] = pipe->create_blend_state(pipe, &blend);
  1208.    }
  1209.    return ctx->blend_clear[index];
  1210. }
  1211.  
  1212. static void util_blitter_clear_custom(struct blitter_context *blitter,
  1213.                                       unsigned width, unsigned height,
  1214.                                       unsigned num_layers,
  1215.                                       unsigned clear_buffers,
  1216.                                       const union pipe_color_union *color,
  1217.                                       double depth, unsigned stencil,
  1218.                                       void *custom_blend, void *custom_dsa)
  1219. {
  1220.    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
  1221.    struct pipe_context *pipe = ctx->base.pipe;
  1222.    struct pipe_stencil_ref sr = { { 0 } };
  1223.  
  1224.    assert(ctx->has_layered || num_layers <= 1);
  1225.  
  1226.    blitter_set_running_flag(ctx);
  1227.    blitter_check_saved_vertex_states(ctx);
  1228.    blitter_check_saved_fragment_states(ctx);
  1229.    blitter_disable_render_cond(ctx);
  1230.  
  1231.    /* bind states */
  1232.    if (custom_blend) {
  1233.       pipe->bind_blend_state(pipe, custom_blend);
  1234.    } else {
  1235.       pipe->bind_blend_state(pipe, get_clear_blend_state(ctx, clear_buffers));
  1236.    }
  1237.  
  1238.    if (custom_dsa) {
  1239.       pipe->bind_depth_stencil_alpha_state(pipe, custom_dsa);
  1240.    } else if ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
  1241.       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
  1242.    } else if (clear_buffers & PIPE_CLEAR_DEPTH) {
  1243.       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
  1244.    } else if (clear_buffers & PIPE_CLEAR_STENCIL) {
  1245.       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
  1246.    } else {
  1247.       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
  1248.    }
  1249.  
  1250.    sr.ref_value[0] = stencil & 0xff;
  1251.    pipe->set_stencil_ref(pipe, &sr);
  1252.  
  1253.    pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
  1254.    bind_fs_write_all_cbufs(ctx);
  1255.    pipe->set_sample_mask(pipe, ~0);
  1256.  
  1257.    blitter_set_dst_dimensions(ctx, width, height);
  1258.  
  1259.    if (num_layers > 1 && ctx->has_layered) {
  1260.       blitter_set_common_draw_rect_state(ctx, FALSE, TRUE);
  1261.       blitter_set_clear_color(ctx, color);
  1262.       blitter_draw(ctx, 0, 0, width, height, depth, num_layers);
  1263.    }
  1264.    else {
  1265.       blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
  1266.       blitter->draw_rectangle(blitter, 0, 0, width, height, (float) depth,
  1267.                               UTIL_BLITTER_ATTRIB_COLOR, color);
  1268.    }
  1269.  
  1270.    blitter_restore_vertex_states(ctx);
  1271.    blitter_restore_fragment_states(ctx);
  1272.    blitter_restore_render_cond(ctx);
  1273.    blitter_unset_running_flag(ctx);
  1274. }
  1275.  
  1276. void util_blitter_clear(struct blitter_context *blitter,
  1277.                         unsigned width, unsigned height, unsigned num_layers,
  1278.                         unsigned clear_buffers,
  1279.                         const union pipe_color_union *color,
  1280.                         double depth, unsigned stencil)
  1281. {
  1282.    util_blitter_clear_custom(blitter, width, height, num_layers,
  1283.                              clear_buffers, color, depth, stencil,
  1284.                              NULL, NULL);
  1285. }
  1286.  
  1287. void util_blitter_custom_clear_depth(struct blitter_context *blitter,
  1288.                                      unsigned width, unsigned height,
  1289.                                      double depth, void *custom_dsa)
  1290. {
  1291.     static const union pipe_color_union color;
  1292.     util_blitter_clear_custom(blitter, width, height, 0, 0, &color, depth, 0,
  1293.                               NULL, custom_dsa);
  1294. }
  1295.  
  1296. void util_blitter_default_dst_texture(struct pipe_surface *dst_templ,
  1297.                                       struct pipe_resource *dst,
  1298.                                       unsigned dstlevel,
  1299.                                       unsigned dstz)
  1300. {
  1301.     memset(dst_templ, 0, sizeof(*dst_templ));
  1302.     dst_templ->format = util_format_linear(dst->format);
  1303.     dst_templ->u.tex.level = dstlevel;
  1304.     dst_templ->u.tex.first_layer = dstz;
  1305.     dst_templ->u.tex.last_layer = dstz;
  1306. }
  1307.  
  1308. static struct pipe_surface *
  1309. util_blitter_get_next_surface_layer(struct pipe_context *pipe,
  1310.                                     struct pipe_surface *surf)
  1311. {
  1312.    struct pipe_surface dst_templ;
  1313.  
  1314.    memset(&dst_templ, 0, sizeof(dst_templ));
  1315.    dst_templ.format = surf->format;
  1316.    dst_templ.u.tex.level = surf->u.tex.level;
  1317.    dst_templ.u.tex.first_layer = surf->u.tex.first_layer + 1;
  1318.    dst_templ.u.tex.last_layer = surf->u.tex.last_layer + 1;
  1319.  
  1320.    return pipe->create_surface(pipe, surf->texture, &dst_templ);
  1321. }
  1322.  
  1323. void util_blitter_default_src_texture(struct pipe_sampler_view *src_templ,
  1324.                                       struct pipe_resource *src,
  1325.                                       unsigned srclevel)
  1326. {
  1327.     memset(src_templ, 0, sizeof(*src_templ));
  1328.     src_templ->target = src->target;
  1329.     src_templ->format = util_format_linear(src->format);
  1330.     src_templ->u.tex.first_level = srclevel;
  1331.     src_templ->u.tex.last_level = srclevel;
  1332.     src_templ->u.tex.first_layer = 0;
  1333.     src_templ->u.tex.last_layer =
  1334.         src->target == PIPE_TEXTURE_3D ? u_minify(src->depth0, srclevel) - 1
  1335.                                        : src->array_size - 1;
  1336.     src_templ->swizzle_r = PIPE_SWIZZLE_RED;
  1337.     src_templ->swizzle_g = PIPE_SWIZZLE_GREEN;
  1338.     src_templ->swizzle_b = PIPE_SWIZZLE_BLUE;
  1339.     src_templ->swizzle_a = PIPE_SWIZZLE_ALPHA;
  1340. }
  1341.  
  1342. static boolean is_blit_generic_supported(struct blitter_context *blitter,
  1343.                                          const struct pipe_resource *dst,
  1344.                                          enum pipe_format dst_format,
  1345.                                          const struct pipe_resource *src,
  1346.                                          enum pipe_format src_format,
  1347.                                          unsigned mask)
  1348. {
  1349.    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
  1350.    struct pipe_screen *screen = ctx->base.pipe->screen;
  1351.  
  1352.    if (dst) {
  1353.       unsigned bind;
  1354.       const struct util_format_description *desc =
  1355.             util_format_description(dst_format);
  1356.       boolean dst_has_stencil = util_format_has_stencil(desc);
  1357.  
  1358.       /* Stencil export must be supported for stencil copy. */
  1359.       if ((mask & PIPE_MASK_S) && dst_has_stencil &&
  1360.           !ctx->has_stencil_export) {
  1361.          return FALSE;
  1362.       }
  1363.  
  1364.       if (dst_has_stencil || util_format_has_depth(desc))
  1365.          bind = PIPE_BIND_DEPTH_STENCIL;
  1366.       else
  1367.          bind = PIPE_BIND_RENDER_TARGET;
  1368.  
  1369.       if (!screen->is_format_supported(screen, dst_format, dst->target,
  1370.                                        dst->nr_samples, bind)) {
  1371.          return FALSE;
  1372.       }
  1373.    }
  1374.  
  1375.    if (src) {
  1376.       if (src->nr_samples > 1 && !ctx->has_texture_multisample) {
  1377.          return FALSE;
  1378.       }
  1379.  
  1380.       if (!screen->is_format_supported(screen, src_format, src->target,
  1381.                                  src->nr_samples, PIPE_BIND_SAMPLER_VIEW)) {
  1382.          return FALSE;
  1383.       }
  1384.  
  1385.       /* Check stencil sampler support for stencil copy. */
  1386.       if (mask & PIPE_MASK_S) {
  1387.          if (util_format_has_stencil(util_format_description(src_format))) {
  1388.             enum pipe_format stencil_format =
  1389.                util_format_stencil_only(src_format);
  1390.             assert(stencil_format != PIPE_FORMAT_NONE);
  1391.  
  1392.             if (stencil_format != src_format &&
  1393.                 !screen->is_format_supported(screen, stencil_format,
  1394.                                              src->target, src->nr_samples,
  1395.                                              PIPE_BIND_SAMPLER_VIEW)) {
  1396.                return FALSE;
  1397.             }
  1398.          }
  1399.       }
  1400.    }
  1401.  
  1402.    return TRUE;
  1403. }
  1404.  
  1405. boolean util_blitter_is_copy_supported(struct blitter_context *blitter,
  1406.                                        const struct pipe_resource *dst,
  1407.                                        const struct pipe_resource *src)
  1408. {
  1409.    return is_blit_generic_supported(blitter, dst, dst->format,
  1410.                                     src, src->format, PIPE_MASK_RGBAZS);
  1411. }
  1412.  
  1413. boolean util_blitter_is_blit_supported(struct blitter_context *blitter,
  1414.                                        const struct pipe_blit_info *info)
  1415. {
  1416.    return is_blit_generic_supported(blitter,
  1417.                                     info->dst.resource, info->dst.format,
  1418.                                     info->src.resource, info->src.format,
  1419.                                     info->mask);
  1420. }
  1421.  
  1422. void util_blitter_copy_texture(struct blitter_context *blitter,
  1423.                                struct pipe_resource *dst,
  1424.                                unsigned dst_level,
  1425.                                unsigned dstx, unsigned dsty, unsigned dstz,
  1426.                                struct pipe_resource *src,
  1427.                                unsigned src_level,
  1428.                                const struct pipe_box *srcbox)
  1429. {
  1430.    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
  1431.    struct pipe_context *pipe = ctx->base.pipe;
  1432.    struct pipe_surface *dst_view, dst_templ;
  1433.    struct pipe_sampler_view src_templ, *src_view;
  1434.    struct pipe_box dstbox;
  1435.  
  1436.    assert(dst && src);
  1437.    assert(src->target < PIPE_MAX_TEXTURE_TYPES);
  1438.  
  1439.    u_box_3d(dstx, dsty, dstz, abs(srcbox->width), abs(srcbox->height),
  1440.             abs(srcbox->depth), &dstbox);
  1441.  
  1442.    /* Initialize the surface. */
  1443.    util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
  1444.    dst_view = pipe->create_surface(pipe, dst, &dst_templ);
  1445.  
  1446.    /* Initialize the sampler view. */
  1447.    util_blitter_default_src_texture(&src_templ, src, src_level);
  1448.    src_view = pipe->create_sampler_view(pipe, src, &src_templ);
  1449.  
  1450.    /* Copy. */
  1451.    util_blitter_blit_generic(blitter, dst_view, &dstbox,
  1452.                              src_view, srcbox, src->width0, src->height0,
  1453.                              PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL);
  1454.  
  1455.    pipe_surface_reference(&dst_view, NULL);
  1456.    pipe_sampler_view_reference(&src_view, NULL);
  1457. }
  1458.  
  1459. void util_blitter_blit_generic(struct blitter_context *blitter,
  1460.                                struct pipe_surface *dst,
  1461.                                const struct pipe_box *dstbox,
  1462.                                struct pipe_sampler_view *src,
  1463.                                const struct pipe_box *srcbox,
  1464.                                unsigned src_width0, unsigned src_height0,
  1465.                                unsigned mask, unsigned filter,
  1466.                                const struct pipe_scissor_state *scissor)
  1467. {
  1468.    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
  1469.    struct pipe_context *pipe = ctx->base.pipe;
  1470.    struct pipe_framebuffer_state fb_state;
  1471.    enum pipe_texture_target src_target = src->texture->target;
  1472.    unsigned src_samples = src->texture->nr_samples;
  1473.    unsigned dst_samples = dst->texture->nr_samples;
  1474.    boolean has_depth, has_stencil, has_color;
  1475.    boolean blit_stencil, blit_depth, blit_color;
  1476.    void *sampler_state;
  1477.    const struct util_format_description *src_desc =
  1478.          util_format_description(src->format);
  1479.    const struct util_format_description *dst_desc =
  1480.          util_format_description(dst->format);
  1481.  
  1482.    has_color = src_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS &&
  1483.                dst_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS;
  1484.    has_depth = util_format_has_depth(src_desc) &&
  1485.                util_format_has_depth(dst_desc);
  1486.    has_stencil = util_format_has_stencil(src_desc) &&
  1487.                  util_format_has_stencil(dst_desc);
  1488.  
  1489.    blit_color = has_color && (mask & PIPE_MASK_RGBA);
  1490.    blit_depth = has_depth && (mask & PIPE_MASK_Z);
  1491.    blit_stencil = has_stencil && (mask & PIPE_MASK_S) &&
  1492.                   ctx->has_stencil_export;
  1493.  
  1494.    if (!blit_stencil && !blit_depth && !blit_color) {
  1495.       return;
  1496.    }
  1497.  
  1498.    if (blit_stencil ||
  1499.        (dstbox->width == abs(srcbox->width) &&
  1500.         dstbox->height == abs(srcbox->height))) {
  1501.       filter = PIPE_TEX_FILTER_NEAREST;
  1502.    }
  1503.  
  1504.    /* Check whether the states are properly saved. */
  1505.    blitter_set_running_flag(ctx);
  1506.    blitter_check_saved_vertex_states(ctx);
  1507.    blitter_check_saved_fragment_states(ctx);
  1508.    blitter_check_saved_textures(ctx);
  1509.    blitter_check_saved_fb_state(ctx);
  1510.    blitter_disable_render_cond(ctx);
  1511.  
  1512.    /* Initialize framebuffer state. */
  1513.    fb_state.width = dst->width;
  1514.    fb_state.height = dst->height;
  1515.    fb_state.nr_cbufs = blit_depth || blit_stencil ? 0 : 1;
  1516.    fb_state.cbufs[0] = NULL;
  1517.    fb_state.zsbuf = NULL;
  1518.  
  1519.    if (blit_depth || blit_stencil) {
  1520.       pipe->bind_blend_state(pipe, ctx->blend[0]);
  1521.  
  1522.       if (blit_depth && blit_stencil) {
  1523.          pipe->bind_depth_stencil_alpha_state(pipe,
  1524.                                               ctx->dsa_write_depth_stencil);
  1525.          ctx->bind_fs_state(pipe,
  1526.                blitter_get_fs_texfetch_depthstencil(ctx, src_target,
  1527.                                                     src_samples));
  1528.       } else if (blit_depth) {
  1529.          pipe->bind_depth_stencil_alpha_state(pipe,
  1530.                                               ctx->dsa_write_depth_keep_stencil);
  1531.          ctx->bind_fs_state(pipe,
  1532.                blitter_get_fs_texfetch_depth(ctx, src_target,
  1533.                                              src_samples));
  1534.       } else { /* is_stencil */
  1535.          pipe->bind_depth_stencil_alpha_state(pipe,
  1536.                                               ctx->dsa_keep_depth_write_stencil);
  1537.          ctx->bind_fs_state(pipe,
  1538.                blitter_get_fs_texfetch_stencil(ctx, src_target,
  1539.                                                src_samples));
  1540.       }
  1541.  
  1542.    } else {
  1543.       pipe->bind_blend_state(pipe, ctx->blend[mask & PIPE_MASK_RGBA]);
  1544.       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
  1545.       ctx->bind_fs_state(pipe,
  1546.             blitter_get_fs_texfetch_col(ctx, src->format, src_target,
  1547.                                         src_samples, dst_samples, filter));
  1548.    }
  1549.  
  1550.    /* Set the linear filter only for scaled color non-MSAA blits. */
  1551.    if (filter == PIPE_TEX_FILTER_LINEAR) {
  1552.       if (src_target == PIPE_TEXTURE_RECT) {
  1553.          sampler_state = ctx->sampler_state_rect_linear;
  1554.       } else {
  1555.          sampler_state = ctx->sampler_state_linear;
  1556.       }
  1557.    } else {
  1558.       if (src_target == PIPE_TEXTURE_RECT) {
  1559.          sampler_state = ctx->sampler_state_rect;
  1560.       } else {
  1561.          sampler_state = ctx->sampler_state;
  1562.       }
  1563.    }
  1564.  
  1565.    /* Set samplers. */
  1566.    if (blit_depth && blit_stencil) {
  1567.       /* Setup two samplers, one for depth and the other one for stencil. */
  1568.       struct pipe_sampler_view templ;
  1569.       struct pipe_sampler_view *views[2];
  1570.       void *samplers[2] = {sampler_state, sampler_state};
  1571.  
  1572.       templ = *src;
  1573.       templ.format = util_format_stencil_only(templ.format);
  1574.       assert(templ.format != PIPE_FORMAT_NONE);
  1575.  
  1576.       views[0] = src;
  1577.       views[1] = pipe->create_sampler_view(pipe, src->texture, &templ);
  1578.  
  1579.       pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 2, views);
  1580.       pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0, 2, samplers);
  1581.  
  1582.       pipe_sampler_view_reference(&views[1], NULL);
  1583.    } else if (blit_stencil) {
  1584.       /* Set a stencil-only sampler view for it not to sample depth instead. */
  1585.       struct pipe_sampler_view templ;
  1586.       struct pipe_sampler_view *view;
  1587.  
  1588.       templ = *src;
  1589.       templ.format = util_format_stencil_only(templ.format);
  1590.       assert(templ.format != PIPE_FORMAT_NONE);
  1591.  
  1592.       view = pipe->create_sampler_view(pipe, src->texture, &templ);
  1593.  
  1594.       pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &view);
  1595.       pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
  1596.                                 0, 1, &sampler_state);
  1597.  
  1598.       pipe_sampler_view_reference(&view, NULL);
  1599.    } else {
  1600.       pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &src);
  1601.       pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
  1602.                                 0, 1, &sampler_state);
  1603.    }
  1604.  
  1605.    pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
  1606.    if (scissor) {
  1607.       pipe->set_scissor_states(pipe, 0, 1, scissor);
  1608.    }
  1609.  
  1610.    blitter_set_common_draw_rect_state(ctx, scissor != NULL, FALSE);
  1611.    blitter_set_dst_dimensions(ctx, dst->width, dst->height);
  1612.  
  1613.    if ((src_target == PIPE_TEXTURE_1D ||
  1614.         src_target == PIPE_TEXTURE_2D ||
  1615.         src_target == PIPE_TEXTURE_RECT) &&
  1616.        src_samples <= 1) {
  1617.       /* Draw the quad with the draw_rectangle callback. */
  1618.  
  1619.       /* Set texture coordinates. - use a pipe color union
  1620.        * for interface purposes.
  1621.        * XXX pipe_color_union is a wrong name since we use that to set
  1622.        * texture coordinates too.
  1623.        */
  1624.       union pipe_color_union coord;
  1625.       get_texcoords(src, src_width0, src_height0, srcbox->x, srcbox->y,
  1626.                     srcbox->x+srcbox->width, srcbox->y+srcbox->height, coord.f);
  1627.  
  1628.       /* Set framebuffer state. */
  1629.       if (blit_depth || blit_stencil) {
  1630.          fb_state.zsbuf = dst;
  1631.       } else {
  1632.          fb_state.cbufs[0] = dst;
  1633.       }
  1634.       pipe->set_framebuffer_state(pipe, &fb_state);
  1635.  
  1636.       /* Draw. */
  1637.       pipe->set_sample_mask(pipe, ~0);
  1638.       blitter->draw_rectangle(blitter, dstbox->x, dstbox->y,
  1639.                               dstbox->x + dstbox->width,
  1640.                               dstbox->y + dstbox->height, 0,
  1641.                               UTIL_BLITTER_ATTRIB_TEXCOORD, &coord);
  1642.    } else {
  1643.       /* Draw the quad with the generic codepath. */
  1644.       int dst_z;
  1645.       for (dst_z = 0; dst_z < dstbox->depth; dst_z++) {
  1646.          struct pipe_surface *old;
  1647.          float dst2src_scale = srcbox->depth / (float)dstbox->depth;
  1648.  
  1649.          /* Scale Z properly if the blit is scaled.
  1650.           *
  1651.           * When downscaling, we want the coordinates centered, so that
  1652.           * mipmapping works for 3D textures. For example, when generating
  1653.           * a 4x4x4 level, this wouldn't average the pixels:
  1654.           *
  1655.           *   src Z:  0 1 2 3 4 5 6 7
  1656.           *   dst Z:  0   1   2   3
  1657.           *
  1658.           * Because the pixels are not centered below the pixels of the higher
  1659.           * level. Therefore, we want this:
  1660.           *   src Z:  0 1 2 3 4 5 6 7
  1661.           *   dst Z:   0   1   2   3
  1662.           *
  1663.           * dst_offset defines the offset needed for centering the pixels and
  1664.           * it works with any scaling (not just 2x).
  1665.           */
  1666.          float dst_offset = ((srcbox->depth - 1) -
  1667.                              (dstbox->depth - 1) * dst2src_scale) * 0.5;
  1668.          float src_z = (dst_z + dst_offset) * dst2src_scale;
  1669.  
  1670.          /* Set framebuffer state. */
  1671.          if (blit_depth || blit_stencil) {
  1672.             fb_state.zsbuf = dst;
  1673.          } else {
  1674.             fb_state.cbufs[0] = dst;
  1675.          }
  1676.          pipe->set_framebuffer_state(pipe, &fb_state);
  1677.  
  1678.          /* See if we need to blit a multisample or singlesample buffer. */
  1679.          if (src_samples == dst_samples && dst_samples > 1) {
  1680.             /* MSAA copy. */
  1681.             unsigned i, max_sample = dst_samples - 1;
  1682.  
  1683.             for (i = 0; i <= max_sample; i++) {
  1684.                pipe->set_sample_mask(pipe, 1 << i);
  1685.                blitter_set_texcoords(ctx, src, src_width0, src_height0,
  1686.                                      srcbox->z + src_z,
  1687.                                      i, srcbox->x, srcbox->y,
  1688.                                      srcbox->x + srcbox->width,
  1689.                                      srcbox->y + srcbox->height);
  1690.                blitter_draw(ctx, dstbox->x, dstbox->y,
  1691.                             dstbox->x + dstbox->width,
  1692.                             dstbox->y + dstbox->height, 0, 1);
  1693.             }
  1694.          } else {
  1695.             /* Normal copy, MSAA upsampling, or MSAA resolve. */
  1696.             pipe->set_sample_mask(pipe, ~0);
  1697.             blitter_set_texcoords(ctx, src, src_width0, src_height0,
  1698.                                   srcbox->z + src_z, 0,
  1699.                                   srcbox->x, srcbox->y,
  1700.                                   srcbox->x + srcbox->width,
  1701.                                   srcbox->y + srcbox->height);
  1702.             blitter_draw(ctx, dstbox->x, dstbox->y,
  1703.                          dstbox->x + dstbox->width,
  1704.                          dstbox->y + dstbox->height, 0, 1);
  1705.          }
  1706.  
  1707.          /* Get the next surface or (if this is the last iteration)
  1708.           * just unreference the last one. */
  1709.          old = dst;
  1710.          if (dst_z < dstbox->depth-1) {
  1711.             dst = ctx->base.get_next_surface_layer(ctx->base.pipe, dst);
  1712.          }
  1713.          if (dst_z) {
  1714.             pipe_surface_reference(&old, NULL);
  1715.          }
  1716.       }
  1717.    }
  1718.  
  1719.    blitter_restore_vertex_states(ctx);
  1720.    blitter_restore_fragment_states(ctx);
  1721.    blitter_restore_textures(ctx);
  1722.    blitter_restore_fb_state(ctx);
  1723.    if (scissor) {
  1724.       pipe->set_scissor_states(pipe, 0, 1, &ctx->base.saved_scissor);
  1725.    }
  1726.    blitter_restore_render_cond(ctx);
  1727.    blitter_unset_running_flag(ctx);
  1728. }
  1729.  
  1730. void
  1731. util_blitter_blit(struct blitter_context *blitter,
  1732.                   const struct pipe_blit_info *info)
  1733. {
  1734.    struct pipe_resource *dst = info->dst.resource;
  1735.    struct pipe_resource *src = info->src.resource;
  1736.    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
  1737.    struct pipe_context *pipe = ctx->base.pipe;
  1738.    struct pipe_surface *dst_view, dst_templ;
  1739.    struct pipe_sampler_view src_templ, *src_view;
  1740.  
  1741.    /* Initialize the surface. */
  1742.    util_blitter_default_dst_texture(&dst_templ, dst, info->dst.level,
  1743.                                     info->dst.box.z);
  1744.    dst_templ.format = info->dst.format;
  1745.    dst_view = pipe->create_surface(pipe, dst, &dst_templ);
  1746.  
  1747.    /* Initialize the sampler view. */
  1748.    util_blitter_default_src_texture(&src_templ, src, info->src.level);
  1749.    src_templ.format = info->src.format;
  1750.    src_view = pipe->create_sampler_view(pipe, src, &src_templ);
  1751.  
  1752.    /* Copy. */
  1753.    util_blitter_blit_generic(blitter, dst_view, &info->dst.box,
  1754.                              src_view, &info->src.box, src->width0, src->height0,
  1755.                              info->mask, info->filter,
  1756.                              info->scissor_enable ? &info->scissor : NULL);
  1757.  
  1758.    pipe_surface_reference(&dst_view, NULL);
  1759.    pipe_sampler_view_reference(&src_view, NULL);
  1760. }
  1761.  
  1762. /* Clear a region of a color surface to a constant value. */
  1763. void util_blitter_clear_render_target(struct blitter_context *blitter,
  1764.                                       struct pipe_surface *dstsurf,
  1765.                                       const union pipe_color_union *color,
  1766.                                       unsigned dstx, unsigned dsty,
  1767.                                       unsigned width, unsigned height)
  1768. {
  1769.    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
  1770.    struct pipe_context *pipe = ctx->base.pipe;
  1771.    struct pipe_framebuffer_state fb_state;
  1772.  
  1773.    assert(dstsurf->texture);
  1774.    if (!dstsurf->texture)
  1775.       return;
  1776.  
  1777.    /* check the saved state */
  1778.    blitter_set_running_flag(ctx);
  1779.    blitter_check_saved_vertex_states(ctx);
  1780.    blitter_check_saved_fragment_states(ctx);
  1781.    blitter_check_saved_fb_state(ctx);
  1782.    blitter_disable_render_cond(ctx);
  1783.  
  1784.    /* bind states */
  1785.    pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA]);
  1786.    pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
  1787.    bind_fs_write_one_cbuf(ctx);
  1788.    pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
  1789.  
  1790.    /* set a framebuffer state */
  1791.    fb_state.width = dstsurf->width;
  1792.    fb_state.height = dstsurf->height;
  1793.    fb_state.nr_cbufs = 1;
  1794.    fb_state.cbufs[0] = dstsurf;
  1795.    fb_state.zsbuf = 0;
  1796.    pipe->set_framebuffer_state(pipe, &fb_state);
  1797.    pipe->set_sample_mask(pipe, ~0);
  1798.  
  1799.    blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
  1800.    blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
  1801.    blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0,
  1802.                            UTIL_BLITTER_ATTRIB_COLOR, color);
  1803.  
  1804.    blitter_restore_vertex_states(ctx);
  1805.    blitter_restore_fragment_states(ctx);
  1806.    blitter_restore_fb_state(ctx);
  1807.    blitter_restore_render_cond(ctx);
  1808.    blitter_unset_running_flag(ctx);
  1809. }
  1810.  
  1811. /* Clear a region of a depth stencil surface. */
  1812. void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
  1813.                                       struct pipe_surface *dstsurf,
  1814.                                       unsigned clear_flags,
  1815.                                       double depth,
  1816.                                       unsigned stencil,
  1817.                                       unsigned dstx, unsigned dsty,
  1818.                                       unsigned width, unsigned height)
  1819. {
  1820.    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
  1821.    struct pipe_context *pipe = ctx->base.pipe;
  1822.    struct pipe_framebuffer_state fb_state;
  1823.    struct pipe_stencil_ref sr = { { 0 } };
  1824.  
  1825.    assert(dstsurf->texture);
  1826.    if (!dstsurf->texture)
  1827.       return;
  1828.  
  1829.    /* check the saved state */
  1830.    blitter_set_running_flag(ctx);
  1831.    blitter_check_saved_vertex_states(ctx);
  1832.    blitter_check_saved_fragment_states(ctx);
  1833.    blitter_check_saved_fb_state(ctx);
  1834.    blitter_disable_render_cond(ctx);
  1835.  
  1836.    /* bind states */
  1837.    pipe->bind_blend_state(pipe, ctx->blend[0]);
  1838.    if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
  1839.       sr.ref_value[0] = stencil & 0xff;
  1840.       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
  1841.       pipe->set_stencil_ref(pipe, &sr);
  1842.    }
  1843.    else if (clear_flags & PIPE_CLEAR_DEPTH) {
  1844.       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
  1845.    }
  1846.    else if (clear_flags & PIPE_CLEAR_STENCIL) {
  1847.       sr.ref_value[0] = stencil & 0xff;
  1848.       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
  1849.       pipe->set_stencil_ref(pipe, &sr);
  1850.    }
  1851.    else
  1852.       /* hmm that should be illegal probably, or make it a no-op somewhere */
  1853.       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
  1854.  
  1855.    bind_fs_empty(ctx);
  1856.    pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
  1857.  
  1858.    /* set a framebuffer state */
  1859.    fb_state.width = dstsurf->width;
  1860.    fb_state.height = dstsurf->height;
  1861.    fb_state.nr_cbufs = 0;
  1862.    fb_state.cbufs[0] = 0;
  1863.    fb_state.zsbuf = dstsurf;
  1864.    pipe->set_framebuffer_state(pipe, &fb_state);
  1865.    pipe->set_sample_mask(pipe, ~0);
  1866.  
  1867.    blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
  1868.    blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
  1869.    blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height,
  1870.                            (float) depth,
  1871.                            UTIL_BLITTER_ATTRIB_NONE, NULL);
  1872.  
  1873.    blitter_restore_vertex_states(ctx);
  1874.    blitter_restore_fragment_states(ctx);
  1875.    blitter_restore_fb_state(ctx);
  1876.    blitter_restore_render_cond(ctx);
  1877.    blitter_unset_running_flag(ctx);
  1878. }
  1879.  
  1880. /* draw a rectangle across a region using a custom dsa stage - for r600g */
  1881. void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
  1882.                                        struct pipe_surface *zsurf,
  1883.                                        struct pipe_surface *cbsurf,
  1884.                                        unsigned sample_mask,
  1885.                                        void *dsa_stage, float depth)
  1886. {
  1887.    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
  1888.    struct pipe_context *pipe = ctx->base.pipe;
  1889.    struct pipe_framebuffer_state fb_state;
  1890.  
  1891.    assert(zsurf->texture);
  1892.    if (!zsurf->texture)
  1893.       return;
  1894.  
  1895.    /* check the saved state */
  1896.    blitter_set_running_flag(ctx);
  1897.    blitter_check_saved_vertex_states(ctx);
  1898.    blitter_check_saved_fragment_states(ctx);
  1899.    blitter_check_saved_fb_state(ctx);
  1900.    blitter_disable_render_cond(ctx);
  1901.  
  1902.    /* bind states */
  1903.    pipe->bind_blend_state(pipe, cbsurf ? ctx->blend[PIPE_MASK_RGBA] :
  1904.                                          ctx->blend[0]);
  1905.    pipe->bind_depth_stencil_alpha_state(pipe, dsa_stage);
  1906.    if (cbsurf)
  1907.       bind_fs_write_one_cbuf(ctx);
  1908.    else
  1909.       bind_fs_empty(ctx);
  1910.    pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
  1911.  
  1912.    /* set a framebuffer state */
  1913.    fb_state.width = zsurf->width;
  1914.    fb_state.height = zsurf->height;
  1915.    fb_state.nr_cbufs = 1;
  1916.    if (cbsurf) {
  1917.            fb_state.cbufs[0] = cbsurf;
  1918.            fb_state.nr_cbufs = 1;
  1919.    } else {
  1920.            fb_state.cbufs[0] = NULL;
  1921.            fb_state.nr_cbufs = 0;
  1922.    }
  1923.    fb_state.zsbuf = zsurf;
  1924.    pipe->set_framebuffer_state(pipe, &fb_state);
  1925.    pipe->set_sample_mask(pipe, sample_mask);
  1926.  
  1927.    blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
  1928.    blitter_set_dst_dimensions(ctx, zsurf->width, zsurf->height);
  1929.    blitter->draw_rectangle(blitter, 0, 0, zsurf->width, zsurf->height, depth,
  1930.                            UTIL_BLITTER_ATTRIB_NONE, NULL);
  1931.  
  1932.    blitter_restore_vertex_states(ctx);
  1933.    blitter_restore_fragment_states(ctx);
  1934.    blitter_restore_fb_state(ctx);
  1935.    blitter_restore_render_cond(ctx);
  1936.    blitter_unset_running_flag(ctx);
  1937. }
  1938.  
  1939. void util_blitter_copy_buffer(struct blitter_context *blitter,
  1940.                               struct pipe_resource *dst,
  1941.                               unsigned dstx,
  1942.                               struct pipe_resource *src,
  1943.                               unsigned srcx,
  1944.                               unsigned size)
  1945. {
  1946.    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
  1947.    struct pipe_context *pipe = ctx->base.pipe;
  1948.    struct pipe_vertex_buffer vb;
  1949.    struct pipe_stream_output_target *so_target;
  1950.    unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0};
  1951.  
  1952.    if (srcx >= src->width0 ||
  1953.        dstx >= dst->width0) {
  1954.       return;
  1955.    }
  1956.    if (srcx + size > src->width0) {
  1957.       size = src->width0 - srcx;
  1958.    }
  1959.    if (dstx + size > dst->width0) {
  1960.       size = dst->width0 - dstx;
  1961.    }
  1962.  
  1963.    /* Drivers not capable of Stream Out should not call this function
  1964.     * in the first place. */
  1965.    assert(ctx->has_stream_out);
  1966.  
  1967.    /* Some alignment is required. */
  1968.    if (srcx % 4 != 0 || dstx % 4 != 0 || size % 4 != 0 ||
  1969.        !ctx->has_stream_out) {
  1970.       struct pipe_box box;
  1971.       u_box_1d(srcx, size, &box);
  1972.       util_resource_copy_region(pipe, dst, 0, dstx, 0, 0, src, 0, &box);
  1973.       return;
  1974.    }
  1975.  
  1976.    blitter_set_running_flag(ctx);
  1977.    blitter_check_saved_vertex_states(ctx);
  1978.    blitter_disable_render_cond(ctx);
  1979.  
  1980.    vb.buffer = src;
  1981.    vb.buffer_offset = srcx;
  1982.    vb.stride = 4;
  1983.  
  1984.    pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
  1985.    pipe->bind_vertex_elements_state(pipe, ctx->velem_state_readbuf[0]);
  1986.    bind_vs_pos_only(ctx);
  1987.    if (ctx->has_geometry_shader)
  1988.       pipe->bind_gs_state(pipe, NULL);
  1989.    if (ctx->has_tessellation) {
  1990.       pipe->bind_tcs_state(pipe, NULL);
  1991.       pipe->bind_tes_state(pipe, NULL);
  1992.    }
  1993.    pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
  1994.  
  1995.    so_target = pipe->create_stream_output_target(pipe, dst, dstx, size);
  1996.    pipe->set_stream_output_targets(pipe, 1, &so_target, offsets);
  1997.  
  1998.    util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
  1999.  
  2000.    blitter_restore_vertex_states(ctx);
  2001.    blitter_restore_render_cond(ctx);
  2002.    blitter_unset_running_flag(ctx);
  2003.    pipe_so_target_reference(&so_target, NULL);
  2004. }
  2005.  
  2006. void util_blitter_clear_buffer(struct blitter_context *blitter,
  2007.                                struct pipe_resource *dst,
  2008.                                unsigned offset, unsigned size,
  2009.                                unsigned num_channels,
  2010.                                const union pipe_color_union *clear_value)
  2011. {
  2012.    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
  2013.    struct pipe_context *pipe = ctx->base.pipe;
  2014.    struct pipe_vertex_buffer vb = {0};
  2015.    struct pipe_stream_output_target *so_target;
  2016.    unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0};
  2017.  
  2018.    assert(num_channels >= 1);
  2019.    assert(num_channels <= 4);
  2020.  
  2021.    /* IMPORTANT:  DON'T DO ANY BOUNDS CHECKING HERE!
  2022.     *
  2023.     * R600 uses this to initialize texture resources, so width0 might not be
  2024.     * what you think it is.
  2025.     */
  2026.  
  2027.    /* Streamout is required. */
  2028.    if (!ctx->has_stream_out) {
  2029.       assert(!"Streamout unsupported in util_blitter_clear_buffer()");
  2030.       return;
  2031.    }
  2032.  
  2033.    /* Some alignment is required. */
  2034.    if (offset % 4 != 0 || size % 4 != 0) {
  2035.       assert(!"Bad alignment in util_blitter_clear_buffer()");
  2036.       return;
  2037.    }
  2038.  
  2039.    u_upload_data(ctx->upload, 0, num_channels*4, clear_value,
  2040.                  &vb.buffer_offset, &vb.buffer);
  2041.    vb.stride = 0;
  2042.  
  2043.    blitter_set_running_flag(ctx);
  2044.    blitter_check_saved_vertex_states(ctx);
  2045.    blitter_disable_render_cond(ctx);
  2046.  
  2047.    pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
  2048.    pipe->bind_vertex_elements_state(pipe,
  2049.                                     ctx->velem_state_readbuf[num_channels-1]);
  2050.    bind_vs_pos_only(ctx);
  2051.    if (ctx->has_geometry_shader)
  2052.       pipe->bind_gs_state(pipe, NULL);
  2053.    if (ctx->has_tessellation) {
  2054.       pipe->bind_tcs_state(pipe, NULL);
  2055.       pipe->bind_tes_state(pipe, NULL);
  2056.    }
  2057.    pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
  2058.  
  2059.    so_target = pipe->create_stream_output_target(pipe, dst, offset, size);
  2060.    pipe->set_stream_output_targets(pipe, 1, &so_target, offsets);
  2061.  
  2062.    util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
  2063.  
  2064.    blitter_restore_vertex_states(ctx);
  2065.    blitter_restore_render_cond(ctx);
  2066.    blitter_unset_running_flag(ctx);
  2067.    pipe_so_target_reference(&so_target, NULL);
  2068.    pipe_resource_reference(&vb.buffer, NULL);
  2069. }
  2070.  
  2071. /* probably radeon specific */
  2072. void util_blitter_custom_resolve_color(struct blitter_context *blitter,
  2073.                                        struct pipe_resource *dst,
  2074.                                        unsigned dst_level,
  2075.                                        unsigned dst_layer,
  2076.                                        struct pipe_resource *src,
  2077.                                        unsigned src_layer,
  2078.                                        unsigned sample_mask,
  2079.                                        void *custom_blend,
  2080.                                        enum pipe_format format)
  2081. {
  2082.    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
  2083.    struct pipe_context *pipe = ctx->base.pipe;
  2084.    struct pipe_framebuffer_state fb_state;
  2085.    struct pipe_surface *srcsurf, *dstsurf, surf_tmpl;
  2086.  
  2087.    blitter_set_running_flag(ctx);
  2088.    blitter_check_saved_vertex_states(ctx);
  2089.    blitter_check_saved_fragment_states(ctx);
  2090.    blitter_disable_render_cond(ctx);
  2091.  
  2092.    /* bind states */
  2093.    pipe->bind_blend_state(pipe, custom_blend);
  2094.    pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
  2095.    pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
  2096.    bind_fs_write_one_cbuf(ctx);
  2097.    pipe->set_sample_mask(pipe, sample_mask);
  2098.  
  2099.    memset(&surf_tmpl, 0, sizeof(surf_tmpl));
  2100.    surf_tmpl.format = format;
  2101.    surf_tmpl.u.tex.level = dst_level;
  2102.    surf_tmpl.u.tex.first_layer = dst_layer;
  2103.    surf_tmpl.u.tex.last_layer = dst_layer;
  2104.  
  2105.    dstsurf = pipe->create_surface(pipe, dst, &surf_tmpl);
  2106.  
  2107.    surf_tmpl.u.tex.level = 0;
  2108.    surf_tmpl.u.tex.first_layer = src_layer;
  2109.    surf_tmpl.u.tex.last_layer = src_layer;
  2110.  
  2111.    srcsurf = pipe->create_surface(pipe, src, &surf_tmpl);
  2112.  
  2113.    /* set a framebuffer state */
  2114.    fb_state.width = src->width0;
  2115.    fb_state.height = src->height0;
  2116.    fb_state.nr_cbufs = 2;
  2117.    fb_state.cbufs[0] = srcsurf;
  2118.    fb_state.cbufs[1] = dstsurf;
  2119.    fb_state.zsbuf = NULL;
  2120.    pipe->set_framebuffer_state(pipe, &fb_state);
  2121.  
  2122.    blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
  2123.    blitter_set_dst_dimensions(ctx, src->width0, src->height0);
  2124.    blitter->draw_rectangle(blitter, 0, 0, src->width0, src->height0,
  2125.                            0, 0, NULL);
  2126.    blitter_restore_fb_state(ctx);
  2127.    blitter_restore_vertex_states(ctx);
  2128.    blitter_restore_fragment_states(ctx);
  2129.    blitter_restore_render_cond(ctx);
  2130.    blitter_unset_running_flag(ctx);
  2131.  
  2132.    pipe_surface_reference(&srcsurf, NULL);
  2133.    pipe_surface_reference(&dstsurf, NULL);
  2134. }
  2135.  
  2136. void util_blitter_custom_color(struct blitter_context *blitter,
  2137.                                struct pipe_surface *dstsurf,
  2138.                                void *custom_blend)
  2139. {
  2140.    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
  2141.    struct pipe_context *pipe = ctx->base.pipe;
  2142.    struct pipe_framebuffer_state fb_state;
  2143.  
  2144.    assert(dstsurf->texture);
  2145.    if (!dstsurf->texture)
  2146.       return;
  2147.  
  2148.    /* check the saved state */
  2149.    blitter_set_running_flag(ctx);
  2150.    blitter_check_saved_vertex_states(ctx);
  2151.    blitter_check_saved_fragment_states(ctx);
  2152.    blitter_check_saved_fb_state(ctx);
  2153.    blitter_disable_render_cond(ctx);
  2154.  
  2155.    /* bind states */
  2156.    pipe->bind_blend_state(pipe, custom_blend ? custom_blend
  2157.                                              : ctx->blend[PIPE_MASK_RGBA]);
  2158.    pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
  2159.    bind_fs_write_one_cbuf(ctx);
  2160.    pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
  2161.    pipe->set_sample_mask(pipe, (1ull << MAX2(1, dstsurf->texture->nr_samples)) - 1);
  2162.  
  2163.    /* set a framebuffer state */
  2164.    fb_state.width = dstsurf->width;
  2165.    fb_state.height = dstsurf->height;
  2166.    fb_state.nr_cbufs = 1;
  2167.    fb_state.cbufs[0] = dstsurf;
  2168.    fb_state.zsbuf = 0;
  2169.    pipe->set_framebuffer_state(pipe, &fb_state);
  2170.    pipe->set_sample_mask(pipe, ~0);
  2171.  
  2172.    blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
  2173.    blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
  2174.    blitter->draw_rectangle(blitter, 0, 0, dstsurf->width, dstsurf->height,
  2175.                            0, 0, NULL);
  2176.  
  2177.    blitter_restore_vertex_states(ctx);
  2178.    blitter_restore_fragment_states(ctx);
  2179.    blitter_restore_fb_state(ctx);
  2180.    blitter_restore_render_cond(ctx);
  2181.    blitter_unset_running_flag(ctx);
  2182. }
  2183.