Subversion Repositories Kolibri OS

Rev

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

  1. /**
  2.  * Copyright (C) 2010 Jorge Jimenez (jorge@iryoku.com)
  3.  * Copyright (C) 2010 Belen Masia (bmasia@unizar.es)
  4.  * Copyright (C) 2010 Jose I. Echevarria (joseignacioechevarria@gmail.com)
  5.  * Copyright (C) 2010 Fernando Navarro (fernandn@microsoft.com)
  6.  * Copyright (C) 2010 Diego Gutierrez (diegog@unizar.es)
  7.  * Copyright (C) 2011 Lauri Kasanen (cand@gmx.com)
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions are met:
  12.  *
  13.  *    1. Redistributions of source code must retain the above copyright notice,
  14.  *       this list of conditions and the following disclaimer.
  15.  *
  16.  *    2. Redistributions in binary form must reproduce the following statement:
  17.  *
  18.  *       "Uses Jimenez's MLAA. Copyright (C) 2010 by Jorge Jimenez, Belen Masia,
  19.  *        Jose I. Echevarria, Fernando Navarro and Diego Gutierrez."
  20.  *
  21.  *       Only for use in the Mesa project, this point 2 is filled by naming the
  22.  *       technique Jimenez's MLAA in the Mesa config options.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
  25.  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  26.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
  28.  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34.  * POSSIBILITY OF SUCH DAMAGE.
  35.  *
  36.  * The views and conclusions contained in the software and documentation are
  37.  * those of the authors and should not be interpreted as representing official
  38.  * policies, either expressed or implied, of the copyright holders.
  39.  */
  40.  
  41. #include "pipe/p_compiler.h"
  42.  
  43. #include "postprocess/postprocess.h"
  44. #include "postprocess/pp_mlaa.h"
  45. #include "postprocess/pp_filters.h"
  46. #include "util/u_box.h"
  47. #include "util/u_sampler.h"
  48. #include "util/u_inlines.h"
  49. #include "util/u_memory.h"
  50. #include "util/u_string.h"
  51. #include "pipe/p_screen.h"
  52.  
  53. #define IMM_SPACE 80
  54.  
  55. static float constants[] = { 1, 1, 0, 0 };
  56. static unsigned int dimensions[2] = { 0, 0 };
  57.  
  58. /** Upload the constants. */
  59. static void
  60. up_consts(struct pp_queue_t *ppq)
  61. {
  62.    struct pipe_context *pipe = ppq->p->pipe;
  63.    struct pipe_box box;
  64.  
  65.    u_box_2d(0, 0, sizeof(constants), 1, &box);
  66.  
  67.    pipe->transfer_inline_write(pipe, ppq->constbuf, 0, PIPE_TRANSFER_WRITE,
  68.                                &box, constants, sizeof(constants),
  69.                                sizeof(constants));
  70. }
  71.  
  72. /** Run function of the MLAA filter. */
  73. static void
  74. pp_jimenezmlaa_run(struct pp_queue_t *ppq, struct pipe_resource *in,
  75.                    struct pipe_resource *out, unsigned int n, bool iscolor)
  76. {
  77.  
  78.    struct program *p = ppq->p;
  79.  
  80.    struct pipe_depth_stencil_alpha_state mstencil;
  81.    struct pipe_sampler_view v_tmp, *arr[3];
  82.  
  83.    unsigned int w = 0;
  84.    unsigned int h = 0;
  85.  
  86.    const struct pipe_stencil_ref ref = { {1} };
  87.  
  88.    /* Insufficient initialization checks. */
  89.    assert(p);
  90.    assert(ppq);
  91.    assert(ppq->constbuf);
  92.    assert(ppq->areamaptex);
  93.    assert(ppq->inner_tmp);
  94.    assert(ppq->shaders[n]);
  95.  
  96.    w = p->framebuffer.width;
  97.    h = p->framebuffer.height;
  98.  
  99.    memset(&mstencil, 0, sizeof(mstencil));
  100.  
  101.    cso_set_stencil_ref(p->cso, &ref);
  102.  
  103.    /* Init the pixel size constant */
  104.    if (dimensions[0] != p->framebuffer.width ||
  105.        dimensions[1] != p->framebuffer.height) {
  106.       constants[0] = 1.0f / p->framebuffer.width;
  107.       constants[1] = 1.0f / p->framebuffer.height;
  108.  
  109.       up_consts(ppq);
  110.       dimensions[0] = p->framebuffer.width;
  111.       dimensions[1] = p->framebuffer.height;
  112.    }
  113.  
  114.    cso_set_constant_buffer_resource(p->cso, PIPE_SHADER_VERTEX,
  115.                                     0, ppq->constbuf);
  116.    cso_set_constant_buffer_resource(p->cso, PIPE_SHADER_FRAGMENT,
  117.                                     0, ppq->constbuf);
  118.  
  119.    mstencil.stencil[0].enabled = 1;
  120.    mstencil.stencil[0].valuemask = mstencil.stencil[0].writemask = ~0;
  121.    mstencil.stencil[0].func = PIPE_FUNC_ALWAYS;
  122.    mstencil.stencil[0].fail_op = PIPE_STENCIL_OP_KEEP;
  123.    mstencil.stencil[0].zfail_op = PIPE_STENCIL_OP_KEEP;
  124.    mstencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
  125.  
  126.    p->framebuffer.zsbuf = ppq->stencils;
  127.  
  128.    /* First pass: depth edge detection */
  129.    if (iscolor)
  130.       pp_filter_setup_in(p, in);
  131.    else
  132.       pp_filter_setup_in(p, ppq->depth);
  133.  
  134.    pp_filter_setup_out(p, ppq->inner_tmp[0]);
  135.  
  136.    pp_filter_set_fb(p);
  137.    pp_filter_misc_state(p);
  138.    cso_set_depth_stencil_alpha(p->cso, &mstencil);
  139.    p->pipe->clear(p->pipe, PIPE_CLEAR_STENCIL | PIPE_CLEAR_COLOR,
  140.                   &p->clear_color, 0, 0);
  141.  
  142.    cso_single_sampler(p->cso, PIPE_SHADER_FRAGMENT, 0, &p->sampler_point);
  143.    cso_single_sampler_done(p->cso, PIPE_SHADER_FRAGMENT);
  144.    cso_set_sampler_views(p->cso, PIPE_SHADER_FRAGMENT, 1, &p->view);
  145.  
  146.    cso_set_vertex_shader_handle(p->cso, ppq->shaders[n][1]);    /* offsetvs */
  147.    cso_set_fragment_shader_handle(p->cso, ppq->shaders[n][2]);
  148.  
  149.    pp_filter_draw(p);
  150.    pp_filter_end_pass(p);
  151.  
  152.  
  153.    /* Second pass: blend weights */
  154.    /* Sampler order: areamap, edgesmap, edgesmapL (reversed, thx compiler) */
  155.    mstencil.stencil[0].func = PIPE_FUNC_EQUAL;
  156.    mstencil.stencil[0].zpass_op = PIPE_STENCIL_OP_KEEP;
  157.    cso_set_depth_stencil_alpha(p->cso, &mstencil);
  158.  
  159.    pp_filter_setup_in(p, ppq->areamaptex);
  160.    pp_filter_setup_out(p, ppq->inner_tmp[1]);
  161.  
  162.    u_sampler_view_default_template(&v_tmp, ppq->inner_tmp[0],
  163.                                    ppq->inner_tmp[0]->format);
  164.    arr[1] = arr[2] = p->pipe->create_sampler_view(p->pipe,
  165.                                                   ppq->inner_tmp[0], &v_tmp);
  166.  
  167.    pp_filter_set_clear_fb(p);
  168.  
  169.    cso_single_sampler(p->cso, PIPE_SHADER_FRAGMENT, 0, &p->sampler_point);
  170.    cso_single_sampler(p->cso, PIPE_SHADER_FRAGMENT, 1, &p->sampler_point);
  171.    cso_single_sampler(p->cso, PIPE_SHADER_FRAGMENT, 2, &p->sampler);
  172.    cso_single_sampler_done(p->cso, PIPE_SHADER_FRAGMENT);
  173.  
  174.    arr[0] = p->view;
  175.    cso_set_sampler_views(p->cso, PIPE_SHADER_FRAGMENT, 3, arr);
  176.  
  177.    cso_set_vertex_shader_handle(p->cso, ppq->shaders[n][0]);    /* passvs */
  178.    cso_set_fragment_shader_handle(p->cso, ppq->shaders[n][3]);
  179.  
  180.    pp_filter_draw(p);
  181.    pp_filter_end_pass(p);
  182.    pipe_sampler_view_reference(&arr[1], NULL);
  183.  
  184.  
  185.    /* Third pass: smoothed edges */
  186.    /* Sampler order: colormap, blendmap (wtf compiler) */
  187.    pp_filter_setup_in(p, ppq->inner_tmp[1]);
  188.    pp_filter_setup_out(p, out);
  189.  
  190.    pp_filter_set_fb(p);
  191.  
  192.    /* Blit the input to the output */
  193.    pp_blit(p->pipe, in, 0, 0,
  194.            w, h, 0, p->framebuffer.cbufs[0],
  195.            0, 0, w, h);
  196.  
  197.    u_sampler_view_default_template(&v_tmp, in, in->format);
  198.    arr[0] = p->pipe->create_sampler_view(p->pipe, in, &v_tmp);
  199.  
  200.    cso_single_sampler(p->cso, PIPE_SHADER_FRAGMENT, 0, &p->sampler_point);
  201.    cso_single_sampler(p->cso, PIPE_SHADER_FRAGMENT, 1, &p->sampler_point);
  202.    cso_single_sampler_done(p->cso, PIPE_SHADER_FRAGMENT);
  203.  
  204.    arr[1] = p->view;
  205.    cso_set_sampler_views(p->cso, PIPE_SHADER_FRAGMENT, 2, arr);
  206.  
  207.    cso_set_vertex_shader_handle(p->cso, ppq->shaders[n][1]);    /* offsetvs */
  208.    cso_set_fragment_shader_handle(p->cso, ppq->shaders[n][4]);
  209.  
  210.    p->blend.rt[0].blend_enable = 1;
  211.    cso_set_blend(p->cso, &p->blend);
  212.  
  213.    pp_filter_draw(p);
  214.    pp_filter_end_pass(p);
  215.    pipe_sampler_view_reference(&arr[0], NULL);
  216.  
  217.    p->blend.rt[0].blend_enable = 0;
  218.    p->framebuffer.zsbuf = NULL;
  219. }
  220.  
  221. /** The init function of the MLAA filter. */
  222. static bool
  223. pp_jimenezmlaa_init_run(struct pp_queue_t *ppq, unsigned int n,
  224.                         unsigned int val, bool iscolor)
  225. {
  226.  
  227.    struct pipe_box box;
  228.    struct pipe_resource res;
  229.    char *tmp_text = NULL;
  230.  
  231.    tmp_text = CALLOC(sizeof(blend2fs_1) + sizeof(blend2fs_2) +
  232.                      IMM_SPACE, sizeof(char));
  233.  
  234.    if (tmp_text == NULL) {
  235.       pp_debug("Failed to allocate shader space\n");
  236.       return FALSE;
  237.    }
  238.  
  239.    ppq->constbuf = pipe_buffer_create(ppq->p->screen,
  240.                                       PIPE_BIND_CONSTANT_BUFFER,
  241.                                       PIPE_USAGE_STATIC,
  242.                                       sizeof(constants));
  243.    if (ppq->constbuf == NULL) {
  244.       pp_debug("Failed to allocate constant buffer\n");
  245.       goto fail;
  246.    }
  247.  
  248.    pp_debug("mlaa: using %u max search steps\n", val);
  249.  
  250.    util_sprintf(tmp_text, "%s"
  251.                 "IMM FLT32 {    %.8f,     0.0000,     0.0000,     0.0000}\n"
  252.                 "%s\n", blend2fs_1, (float) val, blend2fs_2);
  253.  
  254.    memset(&res, 0, sizeof(res));
  255.  
  256.    res.target = PIPE_TEXTURE_2D;
  257.    res.format = PIPE_FORMAT_R8G8_UNORM;
  258.    res.width0 = res.height0 = 165;
  259.    res.bind = PIPE_BIND_SAMPLER_VIEW;
  260.    res.usage = PIPE_USAGE_STATIC;
  261.    res.depth0 = res.array_size = res.nr_samples = 1;
  262.  
  263.    if (!ppq->p->screen->is_format_supported(ppq->p->screen, res.format,
  264.                                             res.target, 1, res.bind))
  265.       pp_debug("Areamap format not supported\n");
  266.  
  267.    ppq->areamaptex = ppq->p->screen->resource_create(ppq->p->screen, &res);
  268.    
  269.    if (ppq->areamaptex == NULL) {
  270.       pp_debug("Failed to allocate area map texture\n");
  271.       goto fail;
  272.    }
  273.    
  274.    u_box_2d(0, 0, 165, 165, &box);
  275.  
  276.    ppq->p->pipe->transfer_inline_write(ppq->p->pipe, ppq->areamaptex, 0,
  277.                                        PIPE_TRANSFER_WRITE, &box,
  278.                                        areamap, 165 * 2, sizeof(areamap));
  279.  
  280.    ppq->shaders[n][1] = pp_tgsi_to_state(ppq->p->pipe, offsetvs, true,
  281.                                          "offsetvs");
  282.    if (iscolor)
  283.       ppq->shaders[n][2] = pp_tgsi_to_state(ppq->p->pipe, color1fs,
  284.                                             false, "color1fs");
  285.    else
  286.       ppq->shaders[n][2] = pp_tgsi_to_state(ppq->p->pipe, depth1fs,
  287.                                             false, "depth1fs");
  288.    ppq->shaders[n][3] = pp_tgsi_to_state(ppq->p->pipe, tmp_text, false,
  289.                                          "blend2fs");
  290.    ppq->shaders[n][4] = pp_tgsi_to_state(ppq->p->pipe, neigh3fs, false,
  291.                                          "neigh3fs");
  292.  
  293.    FREE(tmp_text);
  294.  
  295.    return TRUE;
  296.  
  297.  fail:
  298.    
  299.    FREE(tmp_text);
  300.  
  301.    /*
  302.     * Call the common free function for destruction of partially initialized
  303.     * resources.
  304.     */
  305.    pp_jimenezmlaa_free(ppq, n);
  306.  
  307.    return FALSE;
  308. }
  309.  
  310. /** Short wrapper to init the depth version. */
  311. bool
  312. pp_jimenezmlaa_init(struct pp_queue_t *ppq, unsigned int n, unsigned int val)
  313. {
  314.    return pp_jimenezmlaa_init_run(ppq, n, val, false);
  315. }
  316.  
  317. /** Short wrapper to init the color version. */
  318. bool
  319. pp_jimenezmlaa_init_color(struct pp_queue_t *ppq, unsigned int n,
  320.                           unsigned int val)
  321. {
  322.    return pp_jimenezmlaa_init_run(ppq, n, val, true);
  323. }
  324.  
  325. /** Short wrapper to run the depth version. */
  326. void
  327. pp_jimenezmlaa(struct pp_queue_t *ppq, struct pipe_resource *in,
  328.                struct pipe_resource *out, unsigned int n)
  329. {
  330.    pp_jimenezmlaa_run(ppq, in, out, n, false);
  331. }
  332.  
  333. /** Short wrapper to run the color version. */
  334. void
  335. pp_jimenezmlaa_color(struct pp_queue_t *ppq, struct pipe_resource *in,
  336.                      struct pipe_resource *out, unsigned int n)
  337. {
  338.    pp_jimenezmlaa_run(ppq, in, out, n, true);
  339. }
  340.  
  341.  
  342. /**
  343.  * Short wrapper to free the mlaa filter resources. Shaders are freed in
  344.  * the common code in pp_free.
  345.  */
  346. void
  347. pp_jimenezmlaa_free(struct pp_queue_t *ppq, unsigned int n)
  348. {
  349.    if (ppq->areamaptex) {
  350.       ppq->p->screen->resource_destroy(ppq->p->screen, ppq->areamaptex);
  351.       ppq->areamaptex = NULL;
  352.    }
  353.  
  354.    if (ppq->constbuf) {
  355.       ppq->p->screen->resource_destroy(ppq->p->screen, ppq->constbuf);
  356.       ppq->constbuf = NULL;
  357.    }
  358. }
  359.  
  360.