Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a
  5.  * copy of this software and associated documentation files (the "Software"),
  6.  * to deal in the Software without restriction, including without limitation
  7.  * on the rights to use, copy, modify, merge, publish, distribute, sub
  8.  * license, and/or sell copies of the Software, and to permit persons to whom
  9.  * the Software is furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice (including the next
  12.  * paragraph) shall be included in all copies or substantial portions of the
  13.  * Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  18.  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
  19.  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  20.  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  21.  * USE OR OTHER DEALINGS IN THE SOFTWARE.
  22.  */
  23.  
  24. #include "si_pipe.h"
  25. #include "util/u_format.h"
  26. #include "util/u_surface.h"
  27.  
  28. enum si_blitter_op /* bitmask */
  29. {
  30.         SI_SAVE_TEXTURES      = 1,
  31.         SI_SAVE_FRAMEBUFFER   = 2,
  32.         SI_DISABLE_RENDER_COND = 4,
  33.  
  34.         SI_CLEAR         = 0,
  35.  
  36.         SI_CLEAR_SURFACE = SI_SAVE_FRAMEBUFFER,
  37.  
  38.         SI_COPY          = SI_SAVE_FRAMEBUFFER | SI_SAVE_TEXTURES |
  39.                            SI_DISABLE_RENDER_COND,
  40.  
  41.         SI_BLIT          = SI_SAVE_FRAMEBUFFER | SI_SAVE_TEXTURES,
  42.  
  43.         SI_DECOMPRESS    = SI_SAVE_FRAMEBUFFER | SI_DISABLE_RENDER_COND,
  44.  
  45.         SI_COLOR_RESOLVE = SI_SAVE_FRAMEBUFFER
  46. };
  47.  
  48. static void si_blitter_begin(struct pipe_context *ctx, enum si_blitter_op op)
  49. {
  50.         struct si_context *sctx = (struct si_context *)ctx;
  51.  
  52.         r600_suspend_nontimer_queries(&sctx->b);
  53.  
  54.         util_blitter_save_blend(sctx->blitter, sctx->queued.named.blend);
  55.         util_blitter_save_depth_stencil_alpha(sctx->blitter, sctx->queued.named.dsa);
  56.         util_blitter_save_stencil_ref(sctx->blitter, &sctx->stencil_ref);
  57.         util_blitter_save_rasterizer(sctx->blitter, sctx->queued.named.rasterizer);
  58.         util_blitter_save_fragment_shader(sctx->blitter, sctx->ps_shader);
  59.         util_blitter_save_geometry_shader(sctx->blitter, sctx->gs_shader);
  60.         util_blitter_save_vertex_shader(sctx->blitter, sctx->vs_shader);
  61.         util_blitter_save_vertex_elements(sctx->blitter, sctx->vertex_elements);
  62.         if (sctx->queued.named.sample_mask) {
  63.                 util_blitter_save_sample_mask(sctx->blitter,
  64.                                               sctx->queued.named.sample_mask->sample_mask);
  65.         }
  66.         if (sctx->queued.named.viewport) {
  67.                 util_blitter_save_viewport(sctx->blitter, &sctx->queued.named.viewport->viewport);
  68.         }
  69.         if (sctx->queued.named.scissor) {
  70.                 util_blitter_save_scissor(sctx->blitter, &sctx->queued.named.scissor->scissor);
  71.         }
  72.         util_blitter_save_vertex_buffer_slot(sctx->blitter, sctx->vertex_buffer);
  73.         util_blitter_save_so_targets(sctx->blitter, sctx->b.streamout.num_targets,
  74.                                      (struct pipe_stream_output_target**)sctx->b.streamout.targets);
  75.  
  76.         if (op & SI_SAVE_FRAMEBUFFER)
  77.                 util_blitter_save_framebuffer(sctx->blitter, &sctx->framebuffer.state);
  78.  
  79.         if (op & SI_SAVE_TEXTURES) {
  80.                 util_blitter_save_fragment_sampler_states(
  81.                         sctx->blitter, 2,
  82.                         sctx->samplers[PIPE_SHADER_FRAGMENT].states.saved_states);
  83.  
  84.                 util_blitter_save_fragment_sampler_views(sctx->blitter, 2,
  85.                         sctx->samplers[PIPE_SHADER_FRAGMENT].views.views);
  86.         }
  87.  
  88.         if ((op & SI_DISABLE_RENDER_COND) && sctx->b.current_render_cond) {
  89.                 util_blitter_save_render_condition(sctx->blitter,
  90.                                                    sctx->b.current_render_cond,
  91.                                                    sctx->b.current_render_cond_cond,
  92.                                                    sctx->b.current_render_cond_mode);
  93.         }
  94. }
  95.  
  96. static void si_blitter_end(struct pipe_context *ctx)
  97. {
  98.         struct si_context *sctx = (struct si_context *)ctx;
  99.         r600_resume_nontimer_queries(&sctx->b);
  100. }
  101.  
  102. static unsigned u_max_sample(struct pipe_resource *r)
  103. {
  104.         return r->nr_samples ? r->nr_samples - 1 : 0;
  105. }
  106.  
  107. static void si_blit_decompress_depth(struct pipe_context *ctx,
  108.                                      struct r600_texture *texture,
  109.                                      struct r600_texture *staging,
  110.                                      unsigned first_level, unsigned last_level,
  111.                                      unsigned first_layer, unsigned last_layer,
  112.                                      unsigned first_sample, unsigned last_sample)
  113. {
  114.         struct si_context *sctx = (struct si_context *)ctx;
  115.         unsigned layer, level, sample, checked_last_layer, max_layer, max_sample;
  116.         float depth = 1.0f;
  117.         const struct util_format_description *desc;
  118.         struct r600_texture *flushed_depth_texture = staging ?
  119.                         staging : texture->flushed_depth_texture;
  120.  
  121.         if (!staging && !texture->dirty_level_mask)
  122.                 return;
  123.  
  124.         max_sample = u_max_sample(&texture->resource.b.b);
  125.  
  126.         desc = util_format_description(flushed_depth_texture->resource.b.b.format);
  127.  
  128.         if (util_format_has_depth(desc))
  129.                 sctx->dbcb_depth_copy_enabled = true;
  130.         if (util_format_has_stencil(desc))
  131.                 sctx->dbcb_stencil_copy_enabled = true;
  132.  
  133.         assert(sctx->dbcb_depth_copy_enabled || sctx->dbcb_stencil_copy_enabled);
  134.  
  135.         for (level = first_level; level <= last_level; level++) {
  136.                 if (!staging && !(texture->dirty_level_mask & (1 << level)))
  137.                         continue;
  138.  
  139.                 /* The smaller the mipmap level, the less layers there are
  140.                  * as far as 3D textures are concerned. */
  141.                 max_layer = util_max_layer(&texture->resource.b.b, level);
  142.                 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
  143.  
  144.                 for (layer = first_layer; layer <= checked_last_layer; layer++) {
  145.                         for (sample = first_sample; sample <= last_sample; sample++) {
  146.                                 struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
  147.  
  148.                                 sctx->dbcb_copy_sample = sample;
  149.                                 sctx->db_render_state.dirty = true;
  150.  
  151.                                 surf_tmpl.format = texture->resource.b.b.format;
  152.                                 surf_tmpl.u.tex.level = level;
  153.                                 surf_tmpl.u.tex.first_layer = layer;
  154.                                 surf_tmpl.u.tex.last_layer = layer;
  155.  
  156.                                 zsurf = ctx->create_surface(ctx, &texture->resource.b.b, &surf_tmpl);
  157.  
  158.                                 surf_tmpl.format = flushed_depth_texture->resource.b.b.format;
  159.                                 cbsurf = ctx->create_surface(ctx,
  160.                                                 (struct pipe_resource*)flushed_depth_texture, &surf_tmpl);
  161.  
  162.                                 si_blitter_begin(ctx, SI_DECOMPRESS);
  163.                                 util_blitter_custom_depth_stencil(sctx->blitter, zsurf, cbsurf, 1 << sample,
  164.                                                                   sctx->custom_dsa_flush, depth);
  165.                                 si_blitter_end(ctx);
  166.  
  167.                                 pipe_surface_reference(&zsurf, NULL);
  168.                                 pipe_surface_reference(&cbsurf, NULL);
  169.                         }
  170.                 }
  171.  
  172.                 /* The texture will always be dirty if some layers aren't flushed.
  173.                  * I don't think this case can occur though. */
  174.                 if (!staging &&
  175.                     first_layer == 0 && last_layer == max_layer &&
  176.                     first_sample == 0 && last_sample == max_sample) {
  177.                         texture->dirty_level_mask &= ~(1 << level);
  178.                 }
  179.         }
  180.  
  181.         sctx->dbcb_depth_copy_enabled = false;
  182.         sctx->dbcb_stencil_copy_enabled = false;
  183.         sctx->db_render_state.dirty = true;
  184. }
  185.  
  186. static void si_blit_decompress_depth_in_place(struct si_context *sctx,
  187.                                               struct r600_texture *texture,
  188.                                               unsigned first_level, unsigned last_level,
  189.                                               unsigned first_layer, unsigned last_layer)
  190. {
  191.         struct pipe_surface *zsurf, surf_tmpl = {{0}};
  192.         unsigned layer, max_layer, checked_last_layer, level;
  193.  
  194.         sctx->db_inplace_flush_enabled = true;
  195.         sctx->db_render_state.dirty = true;
  196.  
  197.         surf_tmpl.format = texture->resource.b.b.format;
  198.  
  199.         for (level = first_level; level <= last_level; level++) {
  200.                 if (!(texture->dirty_level_mask & (1 << level)))
  201.                         continue;
  202.  
  203.                 surf_tmpl.u.tex.level = level;
  204.  
  205.                 /* The smaller the mipmap level, the less layers there are
  206.                  * as far as 3D textures are concerned. */
  207.                 max_layer = util_max_layer(&texture->resource.b.b, level);
  208.                 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
  209.  
  210.                 for (layer = first_layer; layer <= checked_last_layer; layer++) {
  211.                         surf_tmpl.u.tex.first_layer = layer;
  212.                         surf_tmpl.u.tex.last_layer = layer;
  213.  
  214.                         zsurf = sctx->b.b.create_surface(&sctx->b.b, &texture->resource.b.b, &surf_tmpl);
  215.  
  216.                         si_blitter_begin(&sctx->b.b, SI_DECOMPRESS);
  217.                         util_blitter_custom_depth_stencil(sctx->blitter, zsurf, NULL, ~0,
  218.                                                           sctx->custom_dsa_flush,
  219.                                                           1.0f);
  220.                         si_blitter_end(&sctx->b.b);
  221.  
  222.                         pipe_surface_reference(&zsurf, NULL);
  223.                 }
  224.  
  225.                 /* The texture will always be dirty if some layers aren't flushed.
  226.                  * I don't think this case occurs often though. */
  227.                 if (first_layer == 0 && last_layer == max_layer) {
  228.                         texture->dirty_level_mask &= ~(1 << level);
  229.                 }
  230.         }
  231.  
  232.         sctx->db_inplace_flush_enabled = false;
  233.         sctx->db_render_state.dirty = true;
  234. }
  235.  
  236. void si_flush_depth_textures(struct si_context *sctx,
  237.                              struct si_textures_info *textures)
  238. {
  239.         unsigned i;
  240.         unsigned mask = textures->depth_texture_mask;
  241.  
  242.         while (mask) {
  243.                 struct pipe_sampler_view *view;
  244.                 struct r600_texture *tex;
  245.  
  246.                 i = u_bit_scan(&mask);
  247.  
  248.                 view = textures->views.views[i];
  249.                 assert(view);
  250.  
  251.                 tex = (struct r600_texture *)view->texture;
  252.                 assert(tex->is_depth && !tex->is_flushing_texture);
  253.  
  254.                 si_blit_decompress_depth_in_place(sctx, tex,
  255.                                                   view->u.tex.first_level, view->u.tex.last_level,
  256.                                                   0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
  257.         }
  258. }
  259.  
  260. static void si_blit_decompress_color(struct pipe_context *ctx,
  261.                 struct r600_texture *rtex,
  262.                 unsigned first_level, unsigned last_level,
  263.                 unsigned first_layer, unsigned last_layer)
  264. {
  265.         struct si_context *sctx = (struct si_context *)ctx;
  266.         unsigned layer, level, checked_last_layer, max_layer;
  267.  
  268.         if (!rtex->dirty_level_mask)
  269.                 return;
  270.  
  271.         for (level = first_level; level <= last_level; level++) {
  272.                 if (!(rtex->dirty_level_mask & (1 << level)))
  273.                         continue;
  274.  
  275.                 /* The smaller the mipmap level, the less layers there are
  276.                  * as far as 3D textures are concerned. */
  277.                 max_layer = util_max_layer(&rtex->resource.b.b, level);
  278.                 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
  279.  
  280.                 for (layer = first_layer; layer <= checked_last_layer; layer++) {
  281.                         struct pipe_surface *cbsurf, surf_tmpl;
  282.  
  283.                         surf_tmpl.format = rtex->resource.b.b.format;
  284.                         surf_tmpl.u.tex.level = level;
  285.                         surf_tmpl.u.tex.first_layer = layer;
  286.                         surf_tmpl.u.tex.last_layer = layer;
  287.                         cbsurf = ctx->create_surface(ctx, &rtex->resource.b.b, &surf_tmpl);
  288.  
  289.                         si_blitter_begin(ctx, SI_DECOMPRESS);
  290.                         util_blitter_custom_color(sctx->blitter, cbsurf,
  291.                                 rtex->fmask.size ? sctx->custom_blend_decompress :
  292.                                                    sctx->custom_blend_fastclear);
  293.                         si_blitter_end(ctx);
  294.  
  295.                         pipe_surface_reference(&cbsurf, NULL);
  296.                 }
  297.  
  298.                 /* The texture will always be dirty if some layers aren't flushed.
  299.                  * I don't think this case occurs often though. */
  300.                 if (first_layer == 0 && last_layer == max_layer) {
  301.                         rtex->dirty_level_mask &= ~(1 << level);
  302.                 }
  303.         }
  304. }
  305.  
  306. void si_decompress_color_textures(struct si_context *sctx,
  307.                                   struct si_textures_info *textures)
  308. {
  309.         unsigned i;
  310.         unsigned mask = textures->compressed_colortex_mask;
  311.  
  312.         while (mask) {
  313.                 struct pipe_sampler_view *view;
  314.                 struct r600_texture *tex;
  315.  
  316.                 i = u_bit_scan(&mask);
  317.  
  318.                 view = textures->views.views[i];
  319.                 assert(view);
  320.  
  321.                 tex = (struct r600_texture *)view->texture;
  322.                 assert(tex->cmask.size || tex->fmask.size);
  323.  
  324.                 si_blit_decompress_color(&sctx->b.b, tex,
  325.                                          view->u.tex.first_level, view->u.tex.last_level,
  326.                                          0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
  327.         }
  328. }
  329.  
  330. static void si_clear(struct pipe_context *ctx, unsigned buffers,
  331.                      const union pipe_color_union *color,
  332.                      double depth, unsigned stencil)
  333. {
  334.         struct si_context *sctx = (struct si_context *)ctx;
  335.         struct pipe_framebuffer_state *fb = &sctx->framebuffer.state;
  336.         struct pipe_surface *zsbuf = fb->zsbuf;
  337.         struct r600_texture *zstex =
  338.                 zsbuf ? (struct r600_texture*)zsbuf->texture : NULL;
  339.  
  340.         if (buffers & PIPE_CLEAR_COLOR) {
  341.                 evergreen_do_fast_color_clear(&sctx->b, fb, &sctx->framebuffer.atom,
  342.                                               &buffers, color);
  343.         }
  344.  
  345.         if (buffers & PIPE_CLEAR_COLOR) {
  346.                 int i;
  347.  
  348.                 /* These buffers cannot use fast clear, make sure to disable expansion. */
  349.                 for (i = 0; i < fb->nr_cbufs; i++) {
  350.                         struct r600_texture *tex;
  351.  
  352.                         /* If not clearing this buffer, skip. */
  353.                         if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
  354.                                 continue;
  355.  
  356.                         if (!fb->cbufs[i])
  357.                                 continue;
  358.  
  359.                         tex = (struct r600_texture *)fb->cbufs[i]->texture;
  360.                         if (tex->fmask.size == 0)
  361.                                 tex->dirty_level_mask &= ~(1 << fb->cbufs[i]->u.tex.level);
  362.                 }
  363.         }
  364.  
  365.         if (buffers & PIPE_CLEAR_DEPTH &&
  366.             zstex && zstex->htile_buffer &&
  367.             zsbuf->u.tex.level == 0 &&
  368.             zsbuf->u.tex.first_layer == 0 &&
  369.             zsbuf->u.tex.last_layer == util_max_layer(&zstex->resource.b.b, 0)) {
  370.                 /* Need to disable EXPCLEAR temporarily if clearing
  371.                  * to a new value. */
  372.                 if (zstex->depth_cleared && zstex->depth_clear_value != depth) {
  373.                         sctx->db_depth_disable_expclear = true;
  374.                 }
  375.  
  376.                 zstex->depth_clear_value = depth;
  377.                 sctx->framebuffer.atom.dirty = true; /* updates DB_DEPTH_CLEAR */
  378.                 sctx->db_depth_clear = true;
  379.                 sctx->db_render_state.dirty = true;
  380.         }
  381.  
  382.         si_blitter_begin(ctx, SI_CLEAR);
  383.         util_blitter_clear(sctx->blitter, fb->width, fb->height,
  384.                            util_framebuffer_get_num_layers(fb),
  385.                            buffers, color, depth, stencil);
  386.         si_blitter_end(ctx);
  387.  
  388.         if (sctx->db_depth_clear) {
  389.                 sctx->db_depth_clear = false;
  390.                 sctx->db_depth_disable_expclear = false;
  391.                 zstex->depth_cleared = true;
  392.                 sctx->db_render_state.dirty = true;
  393.         }
  394. }
  395.  
  396. static void si_clear_render_target(struct pipe_context *ctx,
  397.                                    struct pipe_surface *dst,
  398.                                    const union pipe_color_union *color,
  399.                                    unsigned dstx, unsigned dsty,
  400.                                    unsigned width, unsigned height)
  401. {
  402.         struct si_context *sctx = (struct si_context *)ctx;
  403.  
  404.         si_blitter_begin(ctx, SI_CLEAR_SURFACE);
  405.         util_blitter_clear_render_target(sctx->blitter, dst, color,
  406.                                          dstx, dsty, width, height);
  407.         si_blitter_end(ctx);
  408. }
  409.  
  410. static void si_clear_depth_stencil(struct pipe_context *ctx,
  411.                                    struct pipe_surface *dst,
  412.                                    unsigned clear_flags,
  413.                                    double depth,
  414.                                    unsigned stencil,
  415.                                    unsigned dstx, unsigned dsty,
  416.                                    unsigned width, unsigned height)
  417. {
  418.         struct si_context *sctx = (struct si_context *)ctx;
  419.  
  420.         si_blitter_begin(ctx, SI_CLEAR_SURFACE);
  421.         util_blitter_clear_depth_stencil(sctx->blitter, dst, clear_flags, depth, stencil,
  422.                                          dstx, dsty, width, height);
  423.         si_blitter_end(ctx);
  424. }
  425.  
  426. /* Helper for decompressing a portion of a color or depth resource before
  427.  * blitting if any decompression is needed.
  428.  * The driver doesn't decompress resources automatically while u_blitter is
  429.  * rendering. */
  430. static void si_decompress_subresource(struct pipe_context *ctx,
  431.                                       struct pipe_resource *tex,
  432.                                       unsigned level,
  433.                                       unsigned first_layer, unsigned last_layer)
  434. {
  435.         struct si_context *sctx = (struct si_context *)ctx;
  436.         struct r600_texture *rtex = (struct r600_texture*)tex;
  437.  
  438.         if (rtex->is_depth && !rtex->is_flushing_texture) {
  439.                 si_blit_decompress_depth_in_place(sctx, rtex,
  440.                                                   level, level,
  441.                                                   first_layer, last_layer);
  442.         } else if (rtex->fmask.size || rtex->cmask.size) {
  443.                 si_blit_decompress_color(ctx, rtex, level, level,
  444.                                          first_layer, last_layer);
  445.         }
  446. }
  447.  
  448. struct texture_orig_info {
  449.         unsigned format;
  450.         unsigned width0;
  451.         unsigned height0;
  452.         unsigned npix_x;
  453.         unsigned npix_y;
  454.         unsigned npix0_x;
  455.         unsigned npix0_y;
  456. };
  457.  
  458. static void si_compressed_to_blittable(struct pipe_resource *tex,
  459.                                        unsigned level,
  460.                                        struct texture_orig_info *orig)
  461. {
  462.         struct r600_texture *rtex = (struct r600_texture*)tex;
  463.         unsigned pixsize = util_format_get_blocksize(rtex->resource.b.b.format);
  464.         int new_format;
  465.         int new_height, new_width;
  466.  
  467.         orig->format = tex->format;
  468.         orig->width0 = tex->width0;
  469.         orig->height0 = tex->height0;
  470.         orig->npix0_x = rtex->surface.level[0].npix_x;
  471.         orig->npix0_y = rtex->surface.level[0].npix_y;
  472.         orig->npix_x = rtex->surface.level[level].npix_x;
  473.         orig->npix_y = rtex->surface.level[level].npix_y;
  474.  
  475.         if (pixsize == 8)
  476.                 new_format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
  477.         else
  478.                 new_format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
  479.  
  480.         new_width = util_format_get_nblocksx(tex->format, orig->width0);
  481.         new_height = util_format_get_nblocksy(tex->format, orig->height0);
  482.  
  483.         tex->width0 = new_width;
  484.         tex->height0 = new_height;
  485.         tex->format = new_format;
  486.         rtex->surface.level[0].npix_x = util_format_get_nblocksx(orig->format, orig->npix0_x);
  487.         rtex->surface.level[0].npix_y = util_format_get_nblocksy(orig->format, orig->npix0_y);
  488.         rtex->surface.level[level].npix_x = util_format_get_nblocksx(orig->format, orig->npix_x);
  489.         rtex->surface.level[level].npix_y = util_format_get_nblocksy(orig->format, orig->npix_y);
  490.  
  491.         /* By dividing the dimensions by 4, we effectively decrement
  492.          * last_level by 2, therefore the last 2 mipmap levels disappear and
  493.          * aren't blittable. Note that the last 3 mipmap levels (4x4, 2x2,
  494.          * 1x1) have equal slice sizes, which is an important assumption
  495.          * for this to work.
  496.          *
  497.          * In order to make the last 2 mipmap levels blittable, we have to
  498.          * add the slice size of the last mipmap level to the texture
  499.          * address, so that even though the hw thinks it reads last_level-2,
  500.          * it will actually read last_level-1, and if we add the slice size*2,
  501.          * it will read last_level. That's how this workaround works.
  502.          */
  503.         if (level > rtex->resource.b.b.last_level-2)
  504.                 rtex->mipmap_shift = level - (rtex->resource.b.b.last_level-2);
  505. }
  506.  
  507. static void si_change_format(struct pipe_resource *tex,
  508.                              unsigned level,
  509.                              struct texture_orig_info *orig,
  510.                              enum pipe_format format)
  511. {
  512.         struct r600_texture *rtex = (struct r600_texture*)tex;
  513.  
  514.         orig->format = tex->format;
  515.         orig->width0 = tex->width0;
  516.         orig->height0 = tex->height0;
  517.         orig->npix0_x = rtex->surface.level[0].npix_x;
  518.         orig->npix0_y = rtex->surface.level[0].npix_y;
  519.         orig->npix_x = rtex->surface.level[level].npix_x;
  520.         orig->npix_y = rtex->surface.level[level].npix_y;
  521.  
  522.         tex->format = format;
  523. }
  524.  
  525. static void si_reset_blittable_to_orig(struct pipe_resource *tex,
  526.                                        unsigned level,
  527.                                        struct texture_orig_info *orig)
  528. {
  529.         struct r600_texture *rtex = (struct r600_texture*)tex;
  530.  
  531.         tex->format = orig->format;
  532.         tex->width0 = orig->width0;
  533.         tex->height0 = orig->height0;
  534.         rtex->surface.level[0].npix_x = orig->npix0_x;
  535.         rtex->surface.level[0].npix_y = orig->npix0_y;
  536.         rtex->surface.level[level].npix_x = orig->npix_x;
  537.         rtex->surface.level[level].npix_y = orig->npix_y;
  538.         rtex->mipmap_shift = 0;
  539. }
  540.  
  541. void si_resource_copy_region(struct pipe_context *ctx,
  542.                              struct pipe_resource *dst,
  543.                              unsigned dst_level,
  544.                              unsigned dstx, unsigned dsty, unsigned dstz,
  545.                              struct pipe_resource *src,
  546.                              unsigned src_level,
  547.                              const struct pipe_box *src_box)
  548. {
  549.         struct si_context *sctx = (struct si_context *)ctx;
  550.         struct r600_texture *rdst = (struct r600_texture*)dst;
  551.         struct pipe_surface *dst_view, dst_templ;
  552.         struct pipe_sampler_view src_templ, *src_view;
  553.         struct texture_orig_info orig_info[2];
  554.         struct pipe_box sbox, dstbox;
  555.         boolean restore_orig[2];
  556.  
  557.         /* Fallback for buffers. */
  558.         if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
  559.                 si_copy_buffer(sctx, dst, src, dstx, src_box->x, src_box->width, false);
  560.                 return;
  561.         }
  562.  
  563.         memset(orig_info, 0, sizeof(orig_info));
  564.  
  565.         /* The driver doesn't decompress resources automatically while
  566.          * u_blitter is rendering. */
  567.         si_decompress_subresource(ctx, src, src_level,
  568.                                   src_box->z, src_box->z + src_box->depth - 1);
  569.  
  570.         restore_orig[0] = restore_orig[1] = FALSE;
  571.  
  572.         if (util_format_is_compressed(src->format) &&
  573.             util_format_is_compressed(dst->format)) {
  574.                 si_compressed_to_blittable(src, src_level, &orig_info[0]);
  575.                 restore_orig[0] = TRUE;
  576.                 sbox.x = util_format_get_nblocksx(orig_info[0].format, src_box->x);
  577.                 sbox.y = util_format_get_nblocksy(orig_info[0].format, src_box->y);
  578.                 sbox.z = src_box->z;
  579.                 sbox.width = util_format_get_nblocksx(orig_info[0].format, src_box->width);
  580.                 sbox.height = util_format_get_nblocksy(orig_info[0].format, src_box->height);
  581.                 sbox.depth = src_box->depth;
  582.                 src_box = &sbox;
  583.  
  584.                 si_compressed_to_blittable(dst, dst_level, &orig_info[1]);
  585.                 restore_orig[1] = TRUE;
  586.                 /* translate the dst box as well */
  587.                 dstx = util_format_get_nblocksx(orig_info[1].format, dstx);
  588.                 dsty = util_format_get_nblocksy(orig_info[1].format, dsty);
  589.         } else if (!util_blitter_is_copy_supported(sctx->blitter, dst, src)) {
  590.                 if (util_format_is_subsampled_422(src->format)) {
  591.                         /* XXX untested */
  592.                         si_change_format(src, src_level, &orig_info[0],
  593.                                          PIPE_FORMAT_R8G8B8A8_UINT);
  594.                         si_change_format(dst, dst_level, &orig_info[1],
  595.                                          PIPE_FORMAT_R8G8B8A8_UINT);
  596.  
  597.                         sbox = *src_box;
  598.                         sbox.x = util_format_get_nblocksx(orig_info[0].format, src_box->x);
  599.                         sbox.width = util_format_get_nblocksx(orig_info[0].format, src_box->width);
  600.                         src_box = &sbox;
  601.                         dstx = util_format_get_nblocksx(orig_info[1].format, dstx);
  602.  
  603.                         restore_orig[0] = TRUE;
  604.                         restore_orig[1] = TRUE;
  605.                 } else {
  606.                         unsigned blocksize = util_format_get_blocksize(src->format);
  607.  
  608.                         switch (blocksize) {
  609.                         case 1:
  610.                                 si_change_format(src, src_level, &orig_info[0],
  611.                                                 PIPE_FORMAT_R8_UNORM);
  612.                                 si_change_format(dst, dst_level, &orig_info[1],
  613.                                                 PIPE_FORMAT_R8_UNORM);
  614.                                 break;
  615.                         case 2:
  616.                                 si_change_format(src, src_level, &orig_info[0],
  617.                                                 PIPE_FORMAT_R8G8_UNORM);
  618.                                 si_change_format(dst, dst_level, &orig_info[1],
  619.                                                 PIPE_FORMAT_R8G8_UNORM);
  620.                                 break;
  621.                         case 4:
  622.                                 si_change_format(src, src_level, &orig_info[0],
  623.                                                 PIPE_FORMAT_R8G8B8A8_UNORM);
  624.                                 si_change_format(dst, dst_level, &orig_info[1],
  625.                                                 PIPE_FORMAT_R8G8B8A8_UNORM);
  626.                                 break;
  627.                         case 8:
  628.                                 si_change_format(src, src_level, &orig_info[0],
  629.                                                 PIPE_FORMAT_R16G16B16A16_UINT);
  630.                                 si_change_format(dst, dst_level, &orig_info[1],
  631.                                                 PIPE_FORMAT_R16G16B16A16_UINT);
  632.                                 break;
  633.                         case 16:
  634.                                 si_change_format(src, src_level, &orig_info[0],
  635.                                                 PIPE_FORMAT_R32G32B32A32_UINT);
  636.                                 si_change_format(dst, dst_level, &orig_info[1],
  637.                                                 PIPE_FORMAT_R32G32B32A32_UINT);
  638.                                 break;
  639.                         default:
  640.                                 fprintf(stderr, "Unhandled format %s with blocksize %u\n",
  641.                                         util_format_short_name(src->format), blocksize);
  642.                                 assert(0);
  643.                         }
  644.                         restore_orig[0] = TRUE;
  645.                         restore_orig[1] = TRUE;
  646.                 }
  647.         }
  648.  
  649.         /* Initialize the surface. */
  650.         util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
  651.         dst_view = r600_create_surface_custom(ctx, dst, &dst_templ,
  652.                                               rdst->surface.level[dst_level].npix_x,
  653.                                               rdst->surface.level[dst_level].npix_y);
  654.  
  655.         /* Initialize the sampler view. */
  656.         util_blitter_default_src_texture(&src_templ, src, src_level);
  657.         src_view = ctx->create_sampler_view(ctx, src, &src_templ);
  658.  
  659.         u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),
  660.                  abs(src_box->depth), &dstbox);
  661.  
  662.         /* Copy. */
  663.         si_blitter_begin(ctx, SI_COPY);
  664.         util_blitter_blit_generic(sctx->blitter, dst_view, &dstbox,
  665.                                   src_view, src_box, src->width0, src->height0,
  666.                                   PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL);
  667.         si_blitter_end(ctx);
  668.  
  669.         pipe_surface_reference(&dst_view, NULL);
  670.         pipe_sampler_view_reference(&src_view, NULL);
  671.  
  672.         if (restore_orig[0])
  673.                 si_reset_blittable_to_orig(src, src_level, &orig_info[0]);
  674.  
  675.         if (restore_orig[1])
  676.                 si_reset_blittable_to_orig(dst, dst_level, &orig_info[1]);
  677. }
  678.  
  679. /* For MSAA integer resolving to work, we change the format to NORM using this function. */
  680. static enum pipe_format int_to_norm_format(enum pipe_format format)
  681. {
  682.         switch (format) {
  683. #define REPLACE_FORMAT_SIGN(format,sign) \
  684.         case PIPE_FORMAT_##format##_##sign##INT: \
  685.                 return PIPE_FORMAT_##format##_##sign##NORM
  686. #define REPLACE_FORMAT(format) \
  687.                 REPLACE_FORMAT_SIGN(format, U); \
  688.                 REPLACE_FORMAT_SIGN(format, S)
  689.  
  690.         REPLACE_FORMAT_SIGN(B10G10R10A2, U);
  691.         REPLACE_FORMAT(R8);
  692.         REPLACE_FORMAT(R8G8);
  693.         REPLACE_FORMAT(R8G8B8X8);
  694.         REPLACE_FORMAT(R8G8B8A8);
  695.         REPLACE_FORMAT(A8);
  696.         REPLACE_FORMAT(I8);
  697.         REPLACE_FORMAT(L8);
  698.         REPLACE_FORMAT(L8A8);
  699.         REPLACE_FORMAT(R16);
  700.         REPLACE_FORMAT(R16G16);
  701.         REPLACE_FORMAT(R16G16B16X16);
  702.         REPLACE_FORMAT(R16G16B16A16);
  703.         REPLACE_FORMAT(A16);
  704.         REPLACE_FORMAT(I16);
  705.         REPLACE_FORMAT(L16);
  706.         REPLACE_FORMAT(L16A16);
  707.  
  708. #undef REPLACE_FORMAT
  709. #undef REPLACE_FORMAT_SIGN
  710.         default:
  711.                 return format;
  712.         }
  713. }
  714.  
  715. static bool do_hardware_msaa_resolve(struct pipe_context *ctx,
  716.                                      const struct pipe_blit_info *info)
  717. {
  718.         struct si_context *sctx = (struct si_context*)ctx;
  719.         struct r600_texture *dst = (struct r600_texture*)info->dst.resource;
  720.         unsigned dst_width = u_minify(info->dst.resource->width0, info->dst.level);
  721.         unsigned dst_height = u_minify(info->dst.resource->height0, info->dst.level);
  722.         enum pipe_format format = int_to_norm_format(info->dst.format);
  723.         unsigned sample_mask = ~0;
  724.  
  725.         if (info->src.resource->nr_samples > 1 &&
  726.             info->dst.resource->nr_samples <= 1 &&
  727.             util_max_layer(info->src.resource, 0) == 0 &&
  728.             util_max_layer(info->dst.resource, info->dst.level) == 0 &&
  729.             info->dst.format == info->src.format &&
  730.             !util_format_is_pure_integer(format) &&
  731.             !util_format_is_depth_or_stencil(format) &&
  732.             !info->scissor_enable &&
  733.             (info->mask & PIPE_MASK_RGBA) == PIPE_MASK_RGBA &&
  734.             dst_width == info->src.resource->width0 &&
  735.             dst_height == info->src.resource->height0 &&
  736.             info->dst.box.x == 0 &&
  737.             info->dst.box.y == 0 &&
  738.             info->dst.box.width == dst_width &&
  739.             info->dst.box.height == dst_height &&
  740.             info->dst.box.depth == 1 &&
  741.             info->src.box.x == 0 &&
  742.             info->src.box.y == 0 &&
  743.             info->src.box.width == dst_width &&
  744.             info->src.box.height == dst_height &&
  745.             info->src.box.depth == 1 &&
  746.             dst->surface.level[info->dst.level].mode >= RADEON_SURF_MODE_1D &&
  747.             !(dst->surface.flags & RADEON_SURF_SCANOUT) &&
  748.             (!dst->cmask.size || !dst->dirty_level_mask) /* dst cannot be fast-cleared */) {
  749.                 si_blitter_begin(ctx, SI_COLOR_RESOLVE |
  750.                                  (info->render_condition_enable ? 0 : SI_DISABLE_RENDER_COND));
  751.                 util_blitter_custom_resolve_color(sctx->blitter,
  752.                                                   info->dst.resource, info->dst.level,
  753.                                                   info->dst.box.z,
  754.                                                   info->src.resource, info->src.box.z,
  755.                                                   sample_mask, sctx->custom_blend_resolve,
  756.                                                   format);
  757.                 si_blitter_end(ctx);
  758.                 return true;
  759.         }
  760.         return false;
  761. }
  762.  
  763. static void si_blit(struct pipe_context *ctx,
  764.                     const struct pipe_blit_info *info)
  765. {
  766.         struct si_context *sctx = (struct si_context*)ctx;
  767.  
  768.         if (do_hardware_msaa_resolve(ctx, info)) {
  769.                 return;
  770.         }
  771.  
  772.         assert(util_blitter_is_blit_supported(sctx->blitter, info));
  773.  
  774.         /* The driver doesn't decompress resources automatically while
  775.          * u_blitter is rendering. */
  776.         si_decompress_subresource(ctx, info->src.resource, info->src.level,
  777.                                   info->src.box.z,
  778.                                   info->src.box.z + info->src.box.depth - 1);
  779.  
  780.         if (sctx->screen->b.debug_flags & DBG_FORCE_DMA &&
  781.             util_try_blit_via_copy_region(ctx, info))
  782.                 return;
  783.  
  784.         si_blitter_begin(ctx, SI_BLIT |
  785.                          (info->render_condition_enable ? 0 : SI_DISABLE_RENDER_COND));
  786.         util_blitter_blit(sctx->blitter, info);
  787.         si_blitter_end(ctx);
  788. }
  789.  
  790. static void si_flush_resource(struct pipe_context *ctx,
  791.                               struct pipe_resource *res)
  792. {
  793.         struct r600_texture *rtex = (struct r600_texture*)res;
  794.  
  795.         assert(res->target != PIPE_BUFFER);
  796.  
  797.         if (!rtex->is_depth && rtex->cmask.size) {
  798.                 si_blit_decompress_color(ctx, rtex, 0, res->last_level,
  799.                                          0, util_max_layer(res, 0));
  800.         }
  801. }
  802.  
  803. void si_init_blit_functions(struct si_context *sctx)
  804. {
  805.         sctx->b.b.clear = si_clear;
  806.         sctx->b.b.clear_render_target = si_clear_render_target;
  807.         sctx->b.b.clear_depth_stencil = si_clear_depth_stencil;
  808.         sctx->b.b.resource_copy_region = si_resource_copy_region;
  809.         sctx->b.b.blit = si_blit;
  810.         sctx->b.b.flush_resource = si_flush_resource;
  811.         sctx->b.blit_decompress_depth = si_blit_decompress_depth;
  812. }
  813.