Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Mesa 3-D graphics library
  3.  *
  4.  * Copyright (C) 2012-2013 LunarG, Inc.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the "Software"),
  8.  * to deal in the Software without restriction, including without limitation
  9.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10.  * and/or sell copies of the Software, and to permit persons to whom the
  11.  * Software is furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included
  14.  * in all copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  19.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22.  * DEALINGS IN THE SOFTWARE.
  23.  *
  24.  * Authors:
  25.  *    Chia-I Wu <olv@lunarg.com>
  26.  */
  27.  
  28. #include "util/u_framebuffer.h"
  29. #include "util/u_helpers.h"
  30. #include "util/u_upload_mgr.h"
  31.  
  32. #include "ilo_context.h"
  33. #include "ilo_resource.h"
  34. #include "ilo_shader.h"
  35. #include "ilo_state.h"
  36.  
  37. static void
  38. finalize_shader_states(struct ilo_context *ilo)
  39. {
  40.    unsigned type;
  41.  
  42.    for (type = 0; type < PIPE_SHADER_TYPES; type++) {
  43.       struct ilo_shader_state *shader;
  44.       uint32_t state;
  45.  
  46.       switch (type) {
  47.       case PIPE_SHADER_VERTEX:
  48.          shader = ilo->vs;
  49.          state = ILO_DIRTY_VS;
  50.          break;
  51.       case PIPE_SHADER_GEOMETRY:
  52.          shader = ilo->gs;
  53.          state = ILO_DIRTY_GS;
  54.          break;
  55.       case PIPE_SHADER_FRAGMENT:
  56.          shader = ilo->fs;
  57.          state = ILO_DIRTY_FS;
  58.          break;
  59.       default:
  60.          shader = NULL;
  61.          state = 0;
  62.          break;
  63.       }
  64.  
  65.       if (!shader)
  66.          continue;
  67.  
  68.       /* compile if the shader or the states it depends on changed */
  69.       if (ilo->dirty & state) {
  70.          ilo_shader_select_kernel(shader, ilo, ILO_DIRTY_ALL);
  71.       }
  72.       else if (ilo_shader_select_kernel(shader, ilo, ilo->dirty)) {
  73.          /* mark the state dirty if a new kernel is selected */
  74.          ilo->dirty |= state;
  75.       }
  76.  
  77.       /* need to setup SBE for FS */
  78.       if (type == PIPE_SHADER_FRAGMENT && ilo->dirty &
  79.             (state | ILO_DIRTY_GS | ILO_DIRTY_VS | ILO_DIRTY_RASTERIZER)) {
  80.          if (ilo_shader_select_kernel_routing(shader,
  81.                (ilo->gs) ? ilo->gs : ilo->vs, ilo->rasterizer))
  82.             ilo->dirty |= state;
  83.       }
  84.    }
  85. }
  86.  
  87. static void
  88. finalize_constant_buffers(struct ilo_context *ilo)
  89. {
  90.    int sh;
  91.  
  92.    if (!(ilo->dirty & ILO_DIRTY_CBUF))
  93.       return;
  94.  
  95.    /* TODO push constants? */
  96.    for (sh = 0; sh < PIPE_SHADER_TYPES; sh++) {
  97.       unsigned enabled_mask = ilo->cbuf[sh].enabled_mask;
  98.  
  99.       while (enabled_mask) {
  100.          struct ilo_cbuf_cso *cbuf;
  101.          int i;
  102.  
  103.          i = u_bit_scan(&enabled_mask);
  104.          cbuf = &ilo->cbuf[sh].cso[i];
  105.  
  106.          /* upload user buffer */
  107.          if (cbuf->user_buffer) {
  108.             const enum pipe_format elem_format =
  109.                PIPE_FORMAT_R32G32B32A32_FLOAT;
  110.             unsigned offset;
  111.  
  112.             u_upload_data(ilo->uploader, 0, cbuf->user_buffer_size,
  113.                   cbuf->user_buffer, &offset, &cbuf->resource);
  114.  
  115.             ilo_gpe_init_view_surface_for_buffer(ilo->dev,
  116.                   ilo_buffer(cbuf->resource),
  117.                   offset, cbuf->user_buffer_size,
  118.                   util_format_get_blocksize(elem_format), elem_format,
  119.                   false, false, &cbuf->surface);
  120.  
  121.             cbuf->user_buffer = NULL;
  122.             cbuf->user_buffer_size = 0;
  123.          }
  124.       }
  125.    }
  126. }
  127.  
  128. static void
  129. finalize_index_buffer(struct ilo_context *ilo)
  130. {
  131.    const struct pipe_resource *current_hw_res = ilo->ib.hw_resource;
  132.    const bool need_upload = (ilo->draw->indexed &&
  133.          (ilo->ib.user_buffer || ilo->ib.offset % ilo->ib.index_size));
  134.  
  135.    if (!(ilo->dirty & ILO_DIRTY_IB) && !need_upload)
  136.       return;
  137.  
  138.    if (need_upload) {
  139.       const unsigned offset = ilo->ib.index_size * ilo->draw->start;
  140.       const unsigned size = ilo->ib.index_size * ilo->draw->count;
  141.       unsigned hw_offset;
  142.  
  143.       if (ilo->ib.user_buffer) {
  144.          u_upload_data(ilo->uploader, 0, size,
  145.                ilo->ib.user_buffer + offset, &hw_offset, &ilo->ib.hw_resource);
  146.       }
  147.       else {
  148.          u_upload_buffer(ilo->uploader, 0, ilo->ib.offset + offset, size,
  149.                ilo->ib.buffer, &hw_offset, &ilo->ib.hw_resource);
  150.       }
  151.  
  152.       /* the HW offset should be aligned */
  153.       assert(hw_offset % ilo->ib.index_size == 0);
  154.       ilo->ib.draw_start_offset = hw_offset / ilo->ib.index_size;
  155.  
  156.       /*
  157.        * INDEX[ilo->draw->start] in the original buffer is INDEX[0] in the HW
  158.        * resource
  159.        */
  160.       ilo->ib.draw_start_offset -= ilo->draw->start;
  161.    }
  162.    else {
  163.       pipe_resource_reference(&ilo->ib.hw_resource, ilo->ib.buffer);
  164.  
  165.       /* note that index size may be zero when the draw is not indexed */
  166.       if (ilo->draw->indexed)
  167.          ilo->ib.draw_start_offset = ilo->ib.offset / ilo->ib.index_size;
  168.       else
  169.          ilo->ib.draw_start_offset = 0;
  170.    }
  171.  
  172.    /* treat the IB as clean if the HW states do not change */
  173.    if (ilo->ib.hw_resource == current_hw_res &&
  174.        ilo->ib.hw_index_size == ilo->ib.index_size)
  175.       ilo->dirty &= ~ILO_DIRTY_IB;
  176.    else
  177.       ilo->ib.hw_index_size = ilo->ib.index_size;
  178. }
  179.  
  180. /**
  181.  * Finalize states.  Some states depend on other states and are
  182.  * incomplete/invalid until finalized.
  183.  */
  184. void
  185. ilo_finalize_3d_states(struct ilo_context *ilo,
  186.                        const struct pipe_draw_info *draw)
  187. {
  188.    ilo->draw = draw;
  189.  
  190.    finalize_shader_states(ilo);
  191.    finalize_constant_buffers(ilo);
  192.    finalize_index_buffer(ilo);
  193.  
  194.    u_upload_unmap(ilo->uploader);
  195. }
  196.  
  197. static void *
  198. ilo_create_blend_state(struct pipe_context *pipe,
  199.                        const struct pipe_blend_state *state)
  200. {
  201.    struct ilo_context *ilo = ilo_context(pipe);
  202.    struct ilo_blend_state *blend;
  203.  
  204.    blend = MALLOC_STRUCT(ilo_blend_state);
  205.    assert(blend);
  206.  
  207.    ilo_gpe_init_blend(ilo->dev, state, blend);
  208.  
  209.    return blend;
  210. }
  211.  
  212. static void
  213. ilo_bind_blend_state(struct pipe_context *pipe, void *state)
  214. {
  215.    struct ilo_context *ilo = ilo_context(pipe);
  216.  
  217.    ilo->blend = state;
  218.  
  219.    ilo->dirty |= ILO_DIRTY_BLEND;
  220. }
  221.  
  222. static void
  223. ilo_delete_blend_state(struct pipe_context *pipe, void  *state)
  224. {
  225.    FREE(state);
  226. }
  227.  
  228. static void *
  229. ilo_create_sampler_state(struct pipe_context *pipe,
  230.                          const struct pipe_sampler_state *state)
  231. {
  232.    struct ilo_context *ilo = ilo_context(pipe);
  233.    struct ilo_sampler_cso *sampler;
  234.  
  235.    sampler = MALLOC_STRUCT(ilo_sampler_cso);
  236.    assert(sampler);
  237.  
  238.    ilo_gpe_init_sampler_cso(ilo->dev, state, sampler);
  239.  
  240.    return sampler;
  241. }
  242.  
  243. static void
  244. ilo_bind_sampler_states(struct pipe_context *pipe, unsigned shader,
  245.                         unsigned start, unsigned count, void **samplers)
  246. {
  247.    struct ilo_context *ilo = ilo_context(pipe);
  248.    struct ilo_sampler_state *dst = &ilo->sampler[shader];
  249.    unsigned i;
  250.  
  251.    assert(start + count <= Elements(dst->cso));
  252.  
  253.    if (likely(shader != PIPE_SHADER_COMPUTE)) {
  254.       if (!samplers) {
  255.          start = 0;
  256.          count = 0;
  257.       }
  258.  
  259.       /* samplers not in range are also unbound */
  260.       for (i = 0; i < start; i++)
  261.          dst->cso[i] = NULL;
  262.       for (; i < start + count; i++)
  263.          dst->cso[i] = samplers[i - start];
  264.       for (; i < dst->count; i++)
  265.          dst->cso[i] = NULL;
  266.  
  267.       dst->count = start + count;
  268.  
  269.       return;
  270.    }
  271.  
  272.    if (samplers) {
  273.       for (i = 0; i < count; i++)
  274.          dst->cso[start + i] = samplers[i];
  275.    }
  276.    else {
  277.       for (i = 0; i < count; i++)
  278.          dst->cso[start + i] = NULL;
  279.    }
  280.  
  281.    if (dst->count <= start + count) {
  282.       if (samplers)
  283.          count += start;
  284.       else
  285.          count = start;
  286.  
  287.       while (count > 0 && !dst->cso[count - 1])
  288.          count--;
  289.  
  290.       dst->count = count;
  291.    }
  292. }
  293.  
  294. static void
  295. ilo_bind_fragment_sampler_states(struct pipe_context *pipe,
  296.                                  unsigned num_samplers,
  297.                                  void **samplers)
  298. {
  299.    struct ilo_context *ilo = ilo_context(pipe);
  300.  
  301.    ilo_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
  302.          0, num_samplers, samplers);
  303.  
  304.    ilo->dirty |= ILO_DIRTY_SAMPLER_FS;
  305. }
  306.  
  307. static void
  308. ilo_bind_vertex_sampler_states(struct pipe_context *pipe,
  309.                                unsigned num_samplers,
  310.                                void **samplers)
  311. {
  312.    struct ilo_context *ilo = ilo_context(pipe);
  313.  
  314.    ilo_bind_sampler_states(pipe, PIPE_SHADER_VERTEX,
  315.          0, num_samplers, samplers);
  316.  
  317.    ilo->dirty |= ILO_DIRTY_SAMPLER_VS;
  318. }
  319.  
  320. static void
  321. ilo_bind_geometry_sampler_states(struct pipe_context *pipe,
  322.                                  unsigned num_samplers,
  323.                                  void **samplers)
  324. {
  325.    struct ilo_context *ilo = ilo_context(pipe);
  326.  
  327.    ilo_bind_sampler_states(pipe, PIPE_SHADER_GEOMETRY,
  328.          0, num_samplers, samplers);
  329.  
  330.    ilo->dirty |= ILO_DIRTY_SAMPLER_GS;
  331. }
  332.  
  333. static void
  334. ilo_bind_compute_sampler_states(struct pipe_context *pipe,
  335.                                 unsigned start_slot,
  336.                                 unsigned num_samplers,
  337.                                 void **samplers)
  338. {
  339.    struct ilo_context *ilo = ilo_context(pipe);
  340.  
  341.    ilo_bind_sampler_states(pipe, PIPE_SHADER_COMPUTE,
  342.          start_slot, num_samplers, samplers);
  343.  
  344.    ilo->dirty |= ILO_DIRTY_SAMPLER_CS;
  345. }
  346.  
  347. static void
  348. ilo_delete_sampler_state(struct pipe_context *pipe, void *state)
  349. {
  350.    FREE(state);
  351. }
  352.  
  353. static void *
  354. ilo_create_rasterizer_state(struct pipe_context *pipe,
  355.                             const struct pipe_rasterizer_state *state)
  356. {
  357.    struct ilo_context *ilo = ilo_context(pipe);
  358.    struct ilo_rasterizer_state *rast;
  359.  
  360.    rast = MALLOC_STRUCT(ilo_rasterizer_state);
  361.    assert(rast);
  362.  
  363.    rast->state = *state;
  364.    ilo_gpe_init_rasterizer(ilo->dev, state, rast);
  365.  
  366.    return rast;
  367. }
  368.  
  369. static void
  370. ilo_bind_rasterizer_state(struct pipe_context *pipe, void *state)
  371. {
  372.    struct ilo_context *ilo = ilo_context(pipe);
  373.  
  374.    ilo->rasterizer = state;
  375.  
  376.    ilo->dirty |= ILO_DIRTY_RASTERIZER;
  377. }
  378.  
  379. static void
  380. ilo_delete_rasterizer_state(struct pipe_context *pipe, void *state)
  381. {
  382.    FREE(state);
  383. }
  384.  
  385. static void *
  386. ilo_create_depth_stencil_alpha_state(struct pipe_context *pipe,
  387.                                      const struct pipe_depth_stencil_alpha_state *state)
  388. {
  389.    struct ilo_context *ilo = ilo_context(pipe);
  390.    struct ilo_dsa_state *dsa;
  391.  
  392.    dsa = MALLOC_STRUCT(ilo_dsa_state);
  393.    assert(dsa);
  394.  
  395.    ilo_gpe_init_dsa(ilo->dev, state, dsa);
  396.  
  397.    return dsa;
  398. }
  399.  
  400. static void
  401. ilo_bind_depth_stencil_alpha_state(struct pipe_context *pipe, void *state)
  402. {
  403.    struct ilo_context *ilo = ilo_context(pipe);
  404.  
  405.    ilo->dsa = state;
  406.  
  407.    ilo->dirty |= ILO_DIRTY_DSA;
  408. }
  409.  
  410. static void
  411. ilo_delete_depth_stencil_alpha_state(struct pipe_context *pipe, void *state)
  412. {
  413.    FREE(state);
  414. }
  415.  
  416. static void *
  417. ilo_create_fs_state(struct pipe_context *pipe,
  418.                     const struct pipe_shader_state *state)
  419. {
  420.    struct ilo_context *ilo = ilo_context(pipe);
  421.    struct ilo_shader_state *shader;
  422.  
  423.    shader = ilo_shader_create_fs(ilo->dev, state, ilo);
  424.    assert(shader);
  425.  
  426.    ilo_shader_cache_add(ilo->shader_cache, shader);
  427.  
  428.    return shader;
  429. }
  430.  
  431. static void
  432. ilo_bind_fs_state(struct pipe_context *pipe, void *state)
  433. {
  434.    struct ilo_context *ilo = ilo_context(pipe);
  435.  
  436.    ilo->fs = state;
  437.  
  438.    ilo->dirty |= ILO_DIRTY_FS;
  439. }
  440.  
  441. static void
  442. ilo_delete_fs_state(struct pipe_context *pipe, void *state)
  443. {
  444.    struct ilo_context *ilo = ilo_context(pipe);
  445.    struct ilo_shader_state *fs = (struct ilo_shader_state *) state;
  446.  
  447.    ilo_shader_cache_remove(ilo->shader_cache, fs);
  448.    ilo_shader_destroy(fs);
  449. }
  450.  
  451. static void *
  452. ilo_create_vs_state(struct pipe_context *pipe,
  453.                     const struct pipe_shader_state *state)
  454. {
  455.    struct ilo_context *ilo = ilo_context(pipe);
  456.    struct ilo_shader_state *shader;
  457.  
  458.    shader = ilo_shader_create_vs(ilo->dev, state, ilo);
  459.    assert(shader);
  460.  
  461.    ilo_shader_cache_add(ilo->shader_cache, shader);
  462.  
  463.    return shader;
  464. }
  465.  
  466. static void
  467. ilo_bind_vs_state(struct pipe_context *pipe, void *state)
  468. {
  469.    struct ilo_context *ilo = ilo_context(pipe);
  470.  
  471.    ilo->vs = state;
  472.  
  473.    ilo->dirty |= ILO_DIRTY_VS;
  474. }
  475.  
  476. static void
  477. ilo_delete_vs_state(struct pipe_context *pipe, void *state)
  478. {
  479.    struct ilo_context *ilo = ilo_context(pipe);
  480.    struct ilo_shader_state *vs = (struct ilo_shader_state *) state;
  481.  
  482.    ilo_shader_cache_remove(ilo->shader_cache, vs);
  483.    ilo_shader_destroy(vs);
  484. }
  485.  
  486. static void *
  487. ilo_create_gs_state(struct pipe_context *pipe,
  488.                     const struct pipe_shader_state *state)
  489. {
  490.    struct ilo_context *ilo = ilo_context(pipe);
  491.    struct ilo_shader_state *shader;
  492.  
  493.    shader = ilo_shader_create_gs(ilo->dev, state, ilo);
  494.    assert(shader);
  495.  
  496.    ilo_shader_cache_add(ilo->shader_cache, shader);
  497.  
  498.    return shader;
  499. }
  500.  
  501. static void
  502. ilo_bind_gs_state(struct pipe_context *pipe, void *state)
  503. {
  504.    struct ilo_context *ilo = ilo_context(pipe);
  505.  
  506.    /* util_blitter may set this unnecessarily */
  507.    if (ilo->gs == state)
  508.       return;
  509.  
  510.    ilo->gs = state;
  511.  
  512.    ilo->dirty |= ILO_DIRTY_GS;
  513. }
  514.  
  515. static void
  516. ilo_delete_gs_state(struct pipe_context *pipe, void *state)
  517. {
  518.    struct ilo_context *ilo = ilo_context(pipe);
  519.    struct ilo_shader_state *gs = (struct ilo_shader_state *) state;
  520.  
  521.    ilo_shader_cache_remove(ilo->shader_cache, gs);
  522.    ilo_shader_destroy(gs);
  523. }
  524.  
  525. static void *
  526. ilo_create_vertex_elements_state(struct pipe_context *pipe,
  527.                                  unsigned num_elements,
  528.                                  const struct pipe_vertex_element *elements)
  529. {
  530.    struct ilo_context *ilo = ilo_context(pipe);
  531.    struct ilo_ve_state *ve;
  532.  
  533.    ve = MALLOC_STRUCT(ilo_ve_state);
  534.    assert(ve);
  535.  
  536.    ilo_gpe_init_ve(ilo->dev, num_elements, elements, ve);
  537.  
  538.    return ve;
  539. }
  540.  
  541. static void
  542. ilo_bind_vertex_elements_state(struct pipe_context *pipe, void *state)
  543. {
  544.    struct ilo_context *ilo = ilo_context(pipe);
  545.  
  546.    ilo->ve = state;
  547.  
  548.    ilo->dirty |= ILO_DIRTY_VE;
  549. }
  550.  
  551. static void
  552. ilo_delete_vertex_elements_state(struct pipe_context *pipe, void *state)
  553. {
  554.    struct ilo_ve_state *ve = state;
  555.  
  556.    FREE(ve);
  557. }
  558.  
  559. static void
  560. ilo_set_blend_color(struct pipe_context *pipe,
  561.                     const struct pipe_blend_color *state)
  562. {
  563.    struct ilo_context *ilo = ilo_context(pipe);
  564.  
  565.    ilo->blend_color = *state;
  566.  
  567.    ilo->dirty |= ILO_DIRTY_BLEND_COLOR;
  568. }
  569.  
  570. static void
  571. ilo_set_stencil_ref(struct pipe_context *pipe,
  572.                     const struct pipe_stencil_ref *state)
  573. {
  574.    struct ilo_context *ilo = ilo_context(pipe);
  575.  
  576.    /* util_blitter may set this unnecessarily */
  577.    if (!memcpy(&ilo->stencil_ref, state, sizeof(*state)))
  578.       return;
  579.  
  580.    ilo->stencil_ref = *state;
  581.  
  582.    ilo->dirty |= ILO_DIRTY_STENCIL_REF;
  583. }
  584.  
  585. static void
  586. ilo_set_sample_mask(struct pipe_context *pipe,
  587.                     unsigned sample_mask)
  588. {
  589.    struct ilo_context *ilo = ilo_context(pipe);
  590.  
  591.    /* util_blitter may set this unnecessarily */
  592.    if (ilo->sample_mask == sample_mask)
  593.       return;
  594.  
  595.    ilo->sample_mask = sample_mask;
  596.  
  597.    ilo->dirty |= ILO_DIRTY_SAMPLE_MASK;
  598. }
  599.  
  600. static void
  601. ilo_set_clip_state(struct pipe_context *pipe,
  602.                    const struct pipe_clip_state *state)
  603. {
  604.    struct ilo_context *ilo = ilo_context(pipe);
  605.  
  606.    ilo->clip = *state;
  607.  
  608.    ilo->dirty |= ILO_DIRTY_CLIP;
  609. }
  610.  
  611. static void
  612. ilo_set_constant_buffer(struct pipe_context *pipe,
  613.                         uint shader, uint index,
  614.                         struct pipe_constant_buffer *buf)
  615. {
  616.    struct ilo_context *ilo = ilo_context(pipe);
  617.    struct ilo_cbuf_state *cbuf = &ilo->cbuf[shader];
  618.    const unsigned count = 1;
  619.    unsigned i;
  620.  
  621.    assert(shader < Elements(ilo->cbuf));
  622.    assert(index + count <= Elements(ilo->cbuf[shader].cso));
  623.  
  624.    if (buf) {
  625.       for (i = 0; i < count; i++) {
  626.          struct ilo_cbuf_cso *cso = &cbuf->cso[index + i];
  627.  
  628.          pipe_resource_reference(&cso->resource, buf[i].buffer);
  629.  
  630.          if (buf[i].buffer) {
  631.             const enum pipe_format elem_format =
  632.                PIPE_FORMAT_R32G32B32A32_FLOAT;
  633.  
  634.             ilo_gpe_init_view_surface_for_buffer(ilo->dev,
  635.                   ilo_buffer(buf[i].buffer),
  636.                   buf[i].buffer_offset, buf[i].buffer_size,
  637.                   util_format_get_blocksize(elem_format), elem_format,
  638.                   false, false, &cso->surface);
  639.  
  640.             cso->user_buffer = NULL;
  641.             cso->user_buffer_size = 0;
  642.  
  643.             cbuf->enabled_mask |= 1 << (index + i);
  644.          }
  645.          else if (buf[i].user_buffer) {
  646.             cso->surface.bo = NULL;
  647.  
  648.             /* buffer_offset does not apply for user buffer */
  649.             cso->user_buffer = buf[i].user_buffer;
  650.             cso->user_buffer_size = buf[i].buffer_size;
  651.  
  652.             cbuf->enabled_mask |= 1 << (index + i);
  653.          }
  654.          else {
  655.             cso->surface.bo = NULL;
  656.             cso->user_buffer = NULL;
  657.             cso->user_buffer_size = 0;
  658.  
  659.             cbuf->enabled_mask &= ~(1 << (index + i));
  660.          }
  661.       }
  662.    }
  663.    else {
  664.       for (i = 0; i < count; i++) {
  665.          struct ilo_cbuf_cso *cso = &cbuf->cso[index + i];
  666.  
  667.          pipe_resource_reference(&cso->resource, NULL);
  668.          cso->surface.bo = NULL;
  669.          cso->user_buffer = NULL;
  670.          cso->user_buffer_size = 0;
  671.  
  672.          cbuf->enabled_mask &= ~(1 << (index + i));
  673.       }
  674.    }
  675.  
  676.    ilo->dirty |= ILO_DIRTY_CBUF;
  677. }
  678.  
  679. static void
  680. ilo_set_framebuffer_state(struct pipe_context *pipe,
  681.                           const struct pipe_framebuffer_state *state)
  682. {
  683.    struct ilo_context *ilo = ilo_context(pipe);
  684.  
  685.    util_copy_framebuffer_state(&ilo->fb.state, state);
  686.  
  687.    if (state->nr_cbufs)
  688.       ilo->fb.num_samples = state->cbufs[0]->texture->nr_samples;
  689.    else if (state->zsbuf)
  690.       ilo->fb.num_samples = state->zsbuf->texture->nr_samples;
  691.    else
  692.       ilo->fb.num_samples = 1;
  693.  
  694.    if (!ilo->fb.num_samples)
  695.       ilo->fb.num_samples = 1;
  696.  
  697.    ilo->dirty |= ILO_DIRTY_FB;
  698. }
  699.  
  700. static void
  701. ilo_set_polygon_stipple(struct pipe_context *pipe,
  702.                         const struct pipe_poly_stipple *state)
  703. {
  704.    struct ilo_context *ilo = ilo_context(pipe);
  705.  
  706.    ilo->poly_stipple = *state;
  707.  
  708.    ilo->dirty |= ILO_DIRTY_POLY_STIPPLE;
  709. }
  710.  
  711. static void
  712. ilo_set_scissor_states(struct pipe_context *pipe,
  713.                        unsigned start_slot,
  714.                        unsigned num_scissors,
  715.                        const struct pipe_scissor_state *scissors)
  716. {
  717.    struct ilo_context *ilo = ilo_context(pipe);
  718.  
  719.    ilo_gpe_set_scissor(ilo->dev, start_slot, num_scissors,
  720.          scissors, &ilo->scissor);
  721.  
  722.    ilo->dirty |= ILO_DIRTY_SCISSOR;
  723. }
  724.  
  725. static void
  726. ilo_set_viewport_states(struct pipe_context *pipe,
  727.                         unsigned start_slot,
  728.                         unsigned num_viewports,
  729.                         const struct pipe_viewport_state *viewports)
  730. {
  731.    struct ilo_context *ilo = ilo_context(pipe);
  732.  
  733.    if (viewports) {
  734.       unsigned i;
  735.  
  736.       for (i = 0; i < num_viewports; i++) {
  737.          ilo_gpe_set_viewport_cso(ilo->dev, &viewports[i],
  738.                &ilo->viewport.cso[start_slot + i]);
  739.       }
  740.  
  741.       if (ilo->viewport.count < start_slot + num_viewports)
  742.          ilo->viewport.count = start_slot + num_viewports;
  743.  
  744.       /* need to save viewport 0 for util_blitter */
  745.       if (!start_slot && num_viewports)
  746.          ilo->viewport.viewport0 = viewports[0];
  747.    }
  748.    else {
  749.       if (ilo->viewport.count <= start_slot + num_viewports &&
  750.           ilo->viewport.count > start_slot)
  751.          ilo->viewport.count = start_slot;
  752.    }
  753.  
  754.    ilo->dirty |= ILO_DIRTY_VIEWPORT;
  755. }
  756.  
  757. static void
  758. ilo_set_sampler_views(struct pipe_context *pipe, unsigned shader,
  759.                       unsigned start, unsigned count,
  760.                       struct pipe_sampler_view **views)
  761. {
  762.    struct ilo_context *ilo = ilo_context(pipe);
  763.    struct ilo_view_state *dst = &ilo->view[shader];
  764.    unsigned i;
  765.  
  766.    assert(start + count <= Elements(dst->states));
  767.  
  768.    if (likely(shader != PIPE_SHADER_COMPUTE)) {
  769.       if (!views) {
  770.          start = 0;
  771.          count = 0;
  772.       }
  773.  
  774.       /* views not in range are also unbound */
  775.       for (i = 0; i < start; i++)
  776.          pipe_sampler_view_reference(&dst->states[i], NULL);
  777.       for (; i < start + count; i++)
  778.          pipe_sampler_view_reference(&dst->states[i], views[i - start]);
  779.       for (; i < dst->count; i++)
  780.          pipe_sampler_view_reference(&dst->states[i], NULL);
  781.  
  782.       dst->count = start + count;
  783.  
  784.       return;
  785.    }
  786.  
  787.    if (views) {
  788.       for (i = 0; i < count; i++)
  789.          pipe_sampler_view_reference(&dst->states[start + i], views[i]);
  790.    }
  791.    else {
  792.       for (i = 0; i < count; i++)
  793.          pipe_sampler_view_reference(&dst->states[start + i], NULL);
  794.    }
  795.  
  796.    if (dst->count <= start + count) {
  797.       if (views)
  798.          count += start;
  799.       else
  800.          count = start;
  801.  
  802.       while (count > 0 && !dst->states[count - 1])
  803.          count--;
  804.  
  805.       dst->count = count;
  806.    }
  807. }
  808.  
  809. static void
  810. ilo_set_fragment_sampler_views(struct pipe_context *pipe,
  811.                                unsigned num_views,
  812.                                struct pipe_sampler_view **views)
  813. {
  814.    struct ilo_context *ilo = ilo_context(pipe);
  815.  
  816.    ilo_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT,
  817.          0, num_views, views);
  818.  
  819.    ilo->dirty |= ILO_DIRTY_VIEW_FS;
  820. }
  821.  
  822. static void
  823. ilo_set_vertex_sampler_views(struct pipe_context *pipe,
  824.                              unsigned num_views,
  825.                              struct pipe_sampler_view **views)
  826. {
  827.    struct ilo_context *ilo = ilo_context(pipe);
  828.  
  829.    ilo_set_sampler_views(pipe, PIPE_SHADER_VERTEX,
  830.          0, num_views, views);
  831.  
  832.    ilo->dirty |= ILO_DIRTY_VIEW_VS;
  833. }
  834.  
  835. static void
  836. ilo_set_geometry_sampler_views(struct pipe_context *pipe,
  837.                                unsigned num_views,
  838.                                struct pipe_sampler_view **views)
  839. {
  840.    struct ilo_context *ilo = ilo_context(pipe);
  841.  
  842.    ilo_set_sampler_views(pipe, PIPE_SHADER_GEOMETRY,
  843.          0, num_views, views);
  844.  
  845.    ilo->dirty |= ILO_DIRTY_VIEW_GS;
  846. }
  847.  
  848. static void
  849. ilo_set_compute_sampler_views(struct pipe_context *pipe,
  850.                               unsigned start_slot, unsigned num_views,
  851.                               struct pipe_sampler_view **views)
  852. {
  853.    struct ilo_context *ilo = ilo_context(pipe);
  854.  
  855.    ilo_set_sampler_views(pipe, PIPE_SHADER_COMPUTE,
  856.          start_slot, num_views, views);
  857.  
  858.    ilo->dirty |= ILO_DIRTY_VIEW_CS;
  859. }
  860.  
  861. static void
  862. ilo_set_shader_resources(struct pipe_context *pipe,
  863.                          unsigned start, unsigned count,
  864.                          struct pipe_surface **surfaces)
  865. {
  866.    struct ilo_context *ilo = ilo_context(pipe);
  867.    struct ilo_resource_state *dst = &ilo->resource;
  868.    unsigned i;
  869.  
  870.    assert(start + count <= Elements(dst->states));
  871.  
  872.    if (surfaces) {
  873.       for (i = 0; i < count; i++)
  874.          pipe_surface_reference(&dst->states[start + i], surfaces[i]);
  875.    }
  876.    else {
  877.       for (i = 0; i < count; i++)
  878.          pipe_surface_reference(&dst->states[start + i], NULL);
  879.    }
  880.  
  881.    if (dst->count <= start + count) {
  882.       if (surfaces)
  883.          count += start;
  884.       else
  885.          count = start;
  886.  
  887.       while (count > 0 && !dst->states[count - 1])
  888.          count--;
  889.  
  890.       dst->count = count;
  891.    }
  892.  
  893.    ilo->dirty |= ILO_DIRTY_RESOURCE;
  894. }
  895.  
  896. static void
  897. ilo_set_vertex_buffers(struct pipe_context *pipe,
  898.                        unsigned start_slot, unsigned num_buffers,
  899.                        const struct pipe_vertex_buffer *buffers)
  900. {
  901.    struct ilo_context *ilo = ilo_context(pipe);
  902.    unsigned i;
  903.  
  904.    /* no PIPE_CAP_USER_VERTEX_BUFFERS */
  905.    if (buffers) {
  906.       for (i = 0; i < num_buffers; i++)
  907.          assert(!buffers[i].user_buffer);
  908.    }
  909.  
  910.    util_set_vertex_buffers_mask(ilo->vb.states,
  911.          &ilo->vb.enabled_mask, buffers, start_slot, num_buffers);
  912.  
  913.    ilo->dirty |= ILO_DIRTY_VB;
  914. }
  915.  
  916. static void
  917. ilo_set_index_buffer(struct pipe_context *pipe,
  918.                      const struct pipe_index_buffer *state)
  919. {
  920.    struct ilo_context *ilo = ilo_context(pipe);
  921.  
  922.    if (state) {
  923.       pipe_resource_reference(&ilo->ib.buffer, state->buffer);
  924.       ilo->ib.user_buffer = state->user_buffer;
  925.       ilo->ib.offset = state->offset;
  926.       ilo->ib.index_size = state->index_size;
  927.    }
  928.    else {
  929.       pipe_resource_reference(&ilo->ib.buffer, NULL);
  930.       ilo->ib.user_buffer = NULL;
  931.       ilo->ib.offset = 0;
  932.       ilo->ib.index_size = 0;
  933.    }
  934.  
  935.    ilo->dirty |= ILO_DIRTY_IB;
  936. }
  937.  
  938. static struct pipe_stream_output_target *
  939. ilo_create_stream_output_target(struct pipe_context *pipe,
  940.                                 struct pipe_resource *res,
  941.                                 unsigned buffer_offset,
  942.                                 unsigned buffer_size)
  943. {
  944.    struct pipe_stream_output_target *target;
  945.  
  946.    target = MALLOC_STRUCT(pipe_stream_output_target);
  947.    assert(target);
  948.  
  949.    pipe_reference_init(&target->reference, 1);
  950.    target->buffer = NULL;
  951.    pipe_resource_reference(&target->buffer, res);
  952.    target->context = pipe;
  953.    target->buffer_offset = buffer_offset;
  954.    target->buffer_size = buffer_size;
  955.  
  956.    return target;
  957. }
  958.  
  959. static void
  960. ilo_set_stream_output_targets(struct pipe_context *pipe,
  961.                               unsigned num_targets,
  962.                               struct pipe_stream_output_target **targets,
  963.                               unsigned append_bitmask)
  964. {
  965.    struct ilo_context *ilo = ilo_context(pipe);
  966.    unsigned i;
  967.  
  968.    if (!targets)
  969.       num_targets = 0;
  970.  
  971.    /* util_blitter may set this unnecessarily */
  972.    if (!ilo->so.count && !num_targets)
  973.       return;
  974.  
  975.    for (i = 0; i < num_targets; i++)
  976.       pipe_so_target_reference(&ilo->so.states[i], targets[i]);
  977.  
  978.    for (; i < ilo->so.count; i++)
  979.       pipe_so_target_reference(&ilo->so.states[i], NULL);
  980.  
  981.    ilo->so.count = num_targets;
  982.    ilo->so.append_bitmask = append_bitmask;
  983.  
  984.    ilo->so.enabled = (ilo->so.count > 0);
  985.  
  986.    ilo->dirty |= ILO_DIRTY_SO;
  987. }
  988.  
  989. static void
  990. ilo_stream_output_target_destroy(struct pipe_context *pipe,
  991.                                  struct pipe_stream_output_target *target)
  992. {
  993.    pipe_resource_reference(&target->buffer, NULL);
  994.    FREE(target);
  995. }
  996.  
  997. static struct pipe_sampler_view *
  998. ilo_create_sampler_view(struct pipe_context *pipe,
  999.                         struct pipe_resource *res,
  1000.                         const struct pipe_sampler_view *templ)
  1001. {
  1002.    struct ilo_context *ilo = ilo_context(pipe);
  1003.    struct ilo_view_cso *view;
  1004.  
  1005.    view = MALLOC_STRUCT(ilo_view_cso);
  1006.    assert(view);
  1007.  
  1008.    view->base = *templ;
  1009.    pipe_reference_init(&view->base.reference, 1);
  1010.    view->base.texture = NULL;
  1011.    pipe_resource_reference(&view->base.texture, res);
  1012.    view->base.context = pipe;
  1013.  
  1014.    if (res->target == PIPE_BUFFER) {
  1015.       const unsigned elem_size = util_format_get_blocksize(templ->format);
  1016.       const unsigned first_elem = templ->u.buf.first_element;
  1017.       const unsigned num_elems = templ->u.buf.last_element - first_elem + 1;
  1018.  
  1019.       ilo_gpe_init_view_surface_for_buffer(ilo->dev, ilo_buffer(res),
  1020.             first_elem * elem_size, num_elems * elem_size,
  1021.             elem_size, templ->format, false, false, &view->surface);
  1022.    }
  1023.    else {
  1024.       struct ilo_texture *tex = ilo_texture(res);
  1025.  
  1026.       /* warn about degraded performance because of a missing binding flag */
  1027.       if (tex->tiling == INTEL_TILING_NONE &&
  1028.           !(tex->base.bind & PIPE_BIND_SAMPLER_VIEW)) {
  1029.          ilo_warn("creating sampler view for a resource "
  1030.                   "not created for sampling\n");
  1031.       }
  1032.  
  1033.       ilo_gpe_init_view_surface_for_texture(ilo->dev, tex,
  1034.             templ->format,
  1035.             templ->u.tex.first_level,
  1036.             templ->u.tex.last_level - templ->u.tex.first_level + 1,
  1037.             templ->u.tex.first_layer,
  1038.             templ->u.tex.last_layer - templ->u.tex.first_layer + 1,
  1039.             false, false, &view->surface);
  1040.    }
  1041.  
  1042.    return &view->base;
  1043. }
  1044.  
  1045. static void
  1046. ilo_sampler_view_destroy(struct pipe_context *pipe,
  1047.                          struct pipe_sampler_view *view)
  1048. {
  1049.    pipe_resource_reference(&view->texture, NULL);
  1050.    FREE(view);
  1051. }
  1052.  
  1053. static struct pipe_surface *
  1054. ilo_create_surface(struct pipe_context *pipe,
  1055.                    struct pipe_resource *res,
  1056.                    const struct pipe_surface *templ)
  1057. {
  1058.    struct ilo_context *ilo = ilo_context(pipe);
  1059.    struct ilo_surface_cso *surf;
  1060.  
  1061.    surf = MALLOC_STRUCT(ilo_surface_cso);
  1062.    assert(surf);
  1063.  
  1064.    surf->base = *templ;
  1065.    pipe_reference_init(&surf->base.reference, 1);
  1066.    surf->base.texture = NULL;
  1067.    pipe_resource_reference(&surf->base.texture, res);
  1068.  
  1069.    surf->base.context = pipe;
  1070.    surf->base.width = u_minify(res->width0, templ->u.tex.level);
  1071.    surf->base.height = u_minify(res->height0, templ->u.tex.level);
  1072.  
  1073.    surf->is_rt = !util_format_is_depth_or_stencil(templ->format);
  1074.  
  1075.    if (surf->is_rt) {
  1076.       /* relax this? */
  1077.       assert(res->target != PIPE_BUFFER);
  1078.  
  1079.       /*
  1080.        * classic i965 sets render_cache_rw for constant buffers and sol
  1081.        * surfaces but not render buffers.  Why?
  1082.        */
  1083.       ilo_gpe_init_view_surface_for_texture(ilo->dev, ilo_texture(res),
  1084.             templ->format, templ->u.tex.level, 1,
  1085.             templ->u.tex.first_layer,
  1086.             templ->u.tex.last_layer - templ->u.tex.first_layer + 1,
  1087.             true, true, &surf->u.rt);
  1088.    }
  1089.    else {
  1090.       assert(res->target != PIPE_BUFFER);
  1091.  
  1092.       ilo_gpe_init_zs_surface(ilo->dev, ilo_texture(res),
  1093.             templ->format, templ->u.tex.level,
  1094.             templ->u.tex.first_layer,
  1095.             templ->u.tex.last_layer - templ->u.tex.first_layer + 1,
  1096.             &surf->u.zs);
  1097.    }
  1098.  
  1099.    return &surf->base;
  1100. }
  1101.  
  1102. static void
  1103. ilo_surface_destroy(struct pipe_context *pipe,
  1104.                     struct pipe_surface *surface)
  1105. {
  1106.    pipe_resource_reference(&surface->texture, NULL);
  1107.    FREE(surface);
  1108. }
  1109.  
  1110. static void *
  1111. ilo_create_compute_state(struct pipe_context *pipe,
  1112.                          const struct pipe_compute_state *state)
  1113. {
  1114.    struct ilo_context *ilo = ilo_context(pipe);
  1115.    struct ilo_shader_state *shader;
  1116.  
  1117.    shader = ilo_shader_create_cs(ilo->dev, state, ilo);
  1118.    assert(shader);
  1119.  
  1120.    ilo_shader_cache_add(ilo->shader_cache, shader);
  1121.  
  1122.    return shader;
  1123. }
  1124.  
  1125. static void
  1126. ilo_bind_compute_state(struct pipe_context *pipe, void *state)
  1127. {
  1128.    struct ilo_context *ilo = ilo_context(pipe);
  1129.  
  1130.    ilo->cs = state;
  1131.  
  1132.    ilo->dirty |= ILO_DIRTY_CS;
  1133. }
  1134.  
  1135. static void
  1136. ilo_delete_compute_state(struct pipe_context *pipe, void *state)
  1137. {
  1138.    struct ilo_context *ilo = ilo_context(pipe);
  1139.    struct ilo_shader_state *cs = (struct ilo_shader_state *) state;
  1140.  
  1141.    ilo_shader_cache_remove(ilo->shader_cache, cs);
  1142.    ilo_shader_destroy(cs);
  1143. }
  1144.  
  1145. static void
  1146. ilo_set_compute_resources(struct pipe_context *pipe,
  1147.                           unsigned start, unsigned count,
  1148.                           struct pipe_surface **surfaces)
  1149. {
  1150.    struct ilo_context *ilo = ilo_context(pipe);
  1151.    struct ilo_resource_state *dst = &ilo->cs_resource;
  1152.    unsigned i;
  1153.  
  1154.    assert(start + count <= Elements(dst->states));
  1155.  
  1156.    if (surfaces) {
  1157.       for (i = 0; i < count; i++)
  1158.          pipe_surface_reference(&dst->states[start + i], surfaces[i]);
  1159.    }
  1160.    else {
  1161.       for (i = 0; i < count; i++)
  1162.          pipe_surface_reference(&dst->states[start + i], NULL);
  1163.    }
  1164.  
  1165.    if (dst->count <= start + count) {
  1166.       if (surfaces)
  1167.          count += start;
  1168.       else
  1169.          count = start;
  1170.  
  1171.       while (count > 0 && !dst->states[count - 1])
  1172.          count--;
  1173.  
  1174.       dst->count = count;
  1175.    }
  1176.  
  1177.    ilo->dirty |= ILO_DIRTY_CS_RESOURCE;
  1178. }
  1179.  
  1180. static void
  1181. ilo_set_global_binding(struct pipe_context *pipe,
  1182.                        unsigned start, unsigned count,
  1183.                        struct pipe_resource **resources,
  1184.                        uint32_t **handles)
  1185. {
  1186.    struct ilo_context *ilo = ilo_context(pipe);
  1187.    struct ilo_global_binding *dst = &ilo->global_binding;
  1188.    unsigned i;
  1189.  
  1190.    assert(start + count <= Elements(dst->resources));
  1191.  
  1192.    if (resources) {
  1193.       for (i = 0; i < count; i++)
  1194.          pipe_resource_reference(&dst->resources[start + i], resources[i]);
  1195.    }
  1196.    else {
  1197.       for (i = 0; i < count; i++)
  1198.          pipe_resource_reference(&dst->resources[start + i], NULL);
  1199.    }
  1200.  
  1201.    if (dst->count <= start + count) {
  1202.       if (resources)
  1203.          count += start;
  1204.       else
  1205.          count = start;
  1206.  
  1207.       while (count > 0 && !dst->resources[count - 1])
  1208.          count--;
  1209.  
  1210.       dst->count = count;
  1211.    }
  1212.  
  1213.    ilo->dirty |= ILO_DIRTY_GLOBAL_BINDING;
  1214. }
  1215.  
  1216. /**
  1217.  * Initialize state-related functions.
  1218.  */
  1219. void
  1220. ilo_init_state_functions(struct ilo_context *ilo)
  1221. {
  1222.    STATIC_ASSERT(ILO_STATE_COUNT <= 32);
  1223.  
  1224.    ilo->base.create_blend_state = ilo_create_blend_state;
  1225.    ilo->base.bind_blend_state = ilo_bind_blend_state;
  1226.    ilo->base.delete_blend_state = ilo_delete_blend_state;
  1227.    ilo->base.create_sampler_state = ilo_create_sampler_state;
  1228.    ilo->base.bind_fragment_sampler_states = ilo_bind_fragment_sampler_states;
  1229.    ilo->base.bind_vertex_sampler_states = ilo_bind_vertex_sampler_states;
  1230.    ilo->base.bind_geometry_sampler_states = ilo_bind_geometry_sampler_states;
  1231.    ilo->base.bind_compute_sampler_states = ilo_bind_compute_sampler_states;
  1232.    ilo->base.delete_sampler_state = ilo_delete_sampler_state;
  1233.    ilo->base.create_rasterizer_state = ilo_create_rasterizer_state;
  1234.    ilo->base.bind_rasterizer_state = ilo_bind_rasterizer_state;
  1235.    ilo->base.delete_rasterizer_state = ilo_delete_rasterizer_state;
  1236.    ilo->base.create_depth_stencil_alpha_state = ilo_create_depth_stencil_alpha_state;
  1237.    ilo->base.bind_depth_stencil_alpha_state = ilo_bind_depth_stencil_alpha_state;
  1238.    ilo->base.delete_depth_stencil_alpha_state = ilo_delete_depth_stencil_alpha_state;
  1239.    ilo->base.create_fs_state = ilo_create_fs_state;
  1240.    ilo->base.bind_fs_state = ilo_bind_fs_state;
  1241.    ilo->base.delete_fs_state = ilo_delete_fs_state;
  1242.    ilo->base.create_vs_state = ilo_create_vs_state;
  1243.    ilo->base.bind_vs_state = ilo_bind_vs_state;
  1244.    ilo->base.delete_vs_state = ilo_delete_vs_state;
  1245.    ilo->base.create_gs_state = ilo_create_gs_state;
  1246.    ilo->base.bind_gs_state = ilo_bind_gs_state;
  1247.    ilo->base.delete_gs_state = ilo_delete_gs_state;
  1248.    ilo->base.create_vertex_elements_state = ilo_create_vertex_elements_state;
  1249.    ilo->base.bind_vertex_elements_state = ilo_bind_vertex_elements_state;
  1250.    ilo->base.delete_vertex_elements_state = ilo_delete_vertex_elements_state;
  1251.  
  1252.    ilo->base.set_blend_color = ilo_set_blend_color;
  1253.    ilo->base.set_stencil_ref = ilo_set_stencil_ref;
  1254.    ilo->base.set_sample_mask = ilo_set_sample_mask;
  1255.    ilo->base.set_clip_state = ilo_set_clip_state;
  1256.    ilo->base.set_constant_buffer = ilo_set_constant_buffer;
  1257.    ilo->base.set_framebuffer_state = ilo_set_framebuffer_state;
  1258.    ilo->base.set_polygon_stipple = ilo_set_polygon_stipple;
  1259.    ilo->base.set_scissor_states = ilo_set_scissor_states;
  1260.    ilo->base.set_viewport_states = ilo_set_viewport_states;
  1261.    ilo->base.set_fragment_sampler_views = ilo_set_fragment_sampler_views;
  1262.    ilo->base.set_vertex_sampler_views = ilo_set_vertex_sampler_views;
  1263.    ilo->base.set_geometry_sampler_views = ilo_set_geometry_sampler_views;
  1264.    ilo->base.set_compute_sampler_views = ilo_set_compute_sampler_views;
  1265.    ilo->base.set_shader_resources = ilo_set_shader_resources;
  1266.    ilo->base.set_vertex_buffers = ilo_set_vertex_buffers;
  1267.    ilo->base.set_index_buffer = ilo_set_index_buffer;
  1268.  
  1269.    ilo->base.create_stream_output_target = ilo_create_stream_output_target;
  1270.    ilo->base.stream_output_target_destroy = ilo_stream_output_target_destroy;
  1271.    ilo->base.set_stream_output_targets = ilo_set_stream_output_targets;
  1272.  
  1273.    ilo->base.create_sampler_view = ilo_create_sampler_view;
  1274.    ilo->base.sampler_view_destroy = ilo_sampler_view_destroy;
  1275.  
  1276.    ilo->base.create_surface = ilo_create_surface;
  1277.    ilo->base.surface_destroy = ilo_surface_destroy;
  1278.  
  1279.    ilo->base.create_compute_state = ilo_create_compute_state;
  1280.    ilo->base.bind_compute_state = ilo_bind_compute_state;
  1281.    ilo->base.delete_compute_state = ilo_delete_compute_state;
  1282.    ilo->base.set_compute_resources = ilo_set_compute_resources;
  1283.    ilo->base.set_global_binding = ilo_set_global_binding;
  1284. }
  1285.  
  1286. void
  1287. ilo_init_states(struct ilo_context *ilo)
  1288. {
  1289.    ilo_gpe_set_scissor_null(ilo->dev, &ilo->scissor);
  1290.  
  1291.    ilo_gpe_init_zs_surface(ilo->dev, NULL,
  1292.          PIPE_FORMAT_NONE, 0, 0, 1, &ilo->fb.null_zs);
  1293.  
  1294.    ilo->dirty = ILO_DIRTY_ALL;
  1295. }
  1296.  
  1297. void
  1298. ilo_cleanup_states(struct ilo_context *ilo)
  1299. {
  1300.    unsigned i, sh;
  1301.  
  1302.    for (i = 0; i < Elements(ilo->vb.states); i++) {
  1303.       if (ilo->vb.enabled_mask & (1 << i))
  1304.          pipe_resource_reference(&ilo->vb.states[i].buffer, NULL);
  1305.    }
  1306.  
  1307.    pipe_resource_reference(&ilo->ib.buffer, NULL);
  1308.    pipe_resource_reference(&ilo->ib.hw_resource, NULL);
  1309.  
  1310.    for (i = 0; i < ilo->so.count; i++)
  1311.       pipe_so_target_reference(&ilo->so.states[i], NULL);
  1312.  
  1313.    for (sh = 0; sh < PIPE_SHADER_TYPES; sh++) {
  1314.       for (i = 0; i < ilo->view[sh].count; i++) {
  1315.          struct pipe_sampler_view *view = ilo->view[sh].states[i];
  1316.          pipe_sampler_view_reference(&view, NULL);
  1317.       }
  1318.  
  1319.       for (i = 0; i < Elements(ilo->cbuf[sh].cso); i++) {
  1320.          struct ilo_cbuf_cso *cbuf = &ilo->cbuf[sh].cso[i];
  1321.          pipe_resource_reference(&cbuf->resource, NULL);
  1322.       }
  1323.    }
  1324.  
  1325.    for (i = 0; i < ilo->resource.count; i++)
  1326.       pipe_surface_reference(&ilo->resource.states[i], NULL);
  1327.  
  1328.    for (i = 0; i < ilo->fb.state.nr_cbufs; i++)
  1329.       pipe_surface_reference(&ilo->fb.state.cbufs[i], NULL);
  1330.  
  1331.    if (ilo->fb.state.zsbuf)
  1332.       pipe_surface_reference(&ilo->fb.state.zsbuf, NULL);
  1333.  
  1334.    for (i = 0; i < ilo->cs_resource.count; i++)
  1335.       pipe_surface_reference(&ilo->cs_resource.states[i], NULL);
  1336.  
  1337.    for (i = 0; i < ilo->global_binding.count; i++)
  1338.       pipe_resource_reference(&ilo->global_binding.resources[i], NULL);
  1339. }
  1340.  
  1341. /**
  1342.  * Mark all states that have the resource dirty.
  1343.  */
  1344. void
  1345. ilo_mark_states_with_resource_dirty(struct ilo_context *ilo,
  1346.                                     const struct pipe_resource *res)
  1347. {
  1348.    uint32_t states = 0;
  1349.    unsigned sh, i;
  1350.  
  1351.    if (res->target == PIPE_BUFFER) {
  1352.       uint32_t vb_mask = ilo->vb.enabled_mask;
  1353.  
  1354.       while (vb_mask) {
  1355.          const unsigned idx = u_bit_scan(&vb_mask);
  1356.  
  1357.          if (ilo->vb.states[idx].buffer == res) {
  1358.             states |= ILO_DIRTY_VB;
  1359.             break;
  1360.          }
  1361.       }
  1362.  
  1363.       if (ilo->ib.buffer == res) {
  1364.          states |= ILO_DIRTY_IB;
  1365.  
  1366.          /*
  1367.           * finalize_index_buffer() has an optimization that clears
  1368.           * ILO_DIRTY_IB when the HW states do not change.  However, it fails
  1369.           * to flush the VF cache when the HW states do not change, but the
  1370.           * contents of the IB has changed.  Here, we set the index size to an
  1371.           * invalid value to avoid the optimization.
  1372.           */
  1373.          ilo->ib.hw_index_size = 0;
  1374.       }
  1375.  
  1376.       for (i = 0; i < ilo->so.count; i++) {
  1377.          if (ilo->so.states[i]->buffer == res) {
  1378.             states |= ILO_DIRTY_SO;
  1379.             break;
  1380.          }
  1381.       }
  1382.    }
  1383.  
  1384.    for (sh = 0; sh < PIPE_SHADER_TYPES; sh++) {
  1385.       for (i = 0; i < ilo->view[sh].count; i++) {
  1386.          struct pipe_sampler_view *view = ilo->view[sh].states[i];
  1387.  
  1388.          if (view->texture == res) {
  1389.             static const unsigned view_dirty_bits[PIPE_SHADER_TYPES] = {
  1390.                [PIPE_SHADER_VERTEX]    = ILO_DIRTY_VIEW_VS,
  1391.                [PIPE_SHADER_FRAGMENT]  = ILO_DIRTY_VIEW_FS,
  1392.                [PIPE_SHADER_GEOMETRY]  = ILO_DIRTY_VIEW_GS,
  1393.                [PIPE_SHADER_COMPUTE]   = ILO_DIRTY_VIEW_CS,
  1394.             };
  1395.  
  1396.             states |= view_dirty_bits[sh];
  1397.             break;
  1398.          }
  1399.       }
  1400.  
  1401.       if (res->target == PIPE_BUFFER) {
  1402.          for (i = 0; i < Elements(ilo->cbuf[sh].cso); i++) {
  1403.             struct ilo_cbuf_cso *cbuf = &ilo->cbuf[sh].cso[i];
  1404.  
  1405.             if (cbuf->resource == res) {
  1406.                states |= ILO_DIRTY_CBUF;
  1407.                break;
  1408.             }
  1409.          }
  1410.       }
  1411.    }
  1412.  
  1413.    for (i = 0; i < ilo->resource.count; i++) {
  1414.       if (ilo->resource.states[i]->texture == res) {
  1415.          states |= ILO_DIRTY_RESOURCE;
  1416.          break;
  1417.       }
  1418.    }
  1419.  
  1420.    /* for now? */
  1421.    if (res->target != PIPE_BUFFER) {
  1422.       for (i = 0; i < ilo->fb.state.nr_cbufs; i++) {
  1423.          if (ilo->fb.state.cbufs[i]->texture == res) {
  1424.             states |= ILO_DIRTY_FB;
  1425.             break;
  1426.          }
  1427.       }
  1428.  
  1429.       if (ilo->fb.state.zsbuf && ilo->fb.state.zsbuf->texture == res)
  1430.          states |= ILO_DIRTY_FB;
  1431.    }
  1432.  
  1433.    for (i = 0; i < ilo->cs_resource.count; i++) {
  1434.       pipe_surface_reference(&ilo->cs_resource.states[i], NULL);
  1435.       if (ilo->cs_resource.states[i]->texture == res) {
  1436.          states |= ILO_DIRTY_CS_RESOURCE;
  1437.          break;
  1438.       }
  1439.    }
  1440.  
  1441.    for (i = 0; i < ilo->global_binding.count; i++) {
  1442.       if (ilo->global_binding.resources[i] == res) {
  1443.          states |= ILO_DIRTY_GLOBAL_BINDING;
  1444.          break;
  1445.       }
  1446.    }
  1447.  
  1448.    ilo->dirty |= states;
  1449. }
  1450.