Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright © 2014 Intel Corporation
  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.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8.  * and/or sell copies of the Software, and to permit persons to whom the
  9.  * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
  18.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21.  * IN THE SOFTWARE.
  22.  */
  23.  
  24. #include <gtest/gtest.h>
  25. #include "brw_vec4.h"
  26. #include "brw_vs.h"
  27.  
  28. using namespace brw;
  29.  
  30. int ret = 0;
  31.  
  32. class copy_propagation_test : public ::testing::Test {
  33.    virtual void SetUp();
  34.  
  35. public:
  36.    struct brw_context *brw;
  37.    struct brw_device_info *devinfo;
  38.    struct gl_context *ctx;
  39.    struct gl_shader_program *shader_prog;
  40.    struct brw_vertex_program *vp;
  41.    vec4_visitor *v;
  42. };
  43.  
  44. class copy_propagation_vec4_visitor : public vec4_visitor
  45. {
  46. public:
  47.    copy_propagation_vec4_visitor(struct brw_context *brw,
  48.                                   struct gl_shader_program *shader_prog)
  49.       : vec4_visitor(brw, NULL, NULL, NULL, NULL, shader_prog,
  50.                      MESA_SHADER_VERTEX, NULL,
  51.                      false /* no_spills */,
  52.                      ST_NONE, ST_NONE, ST_NONE)
  53.    {
  54.    }
  55.  
  56. protected:
  57.    virtual dst_reg *make_reg_for_system_value(ir_variable *ir)
  58.    {
  59.       unreachable("Not reached");
  60.    }
  61.  
  62.    virtual void setup_payload()
  63.    {
  64.       unreachable("Not reached");
  65.    }
  66.  
  67.    virtual void emit_prolog()
  68.    {
  69.       unreachable("Not reached");
  70.    }
  71.  
  72.    virtual void emit_program_code()
  73.    {
  74.       unreachable("Not reached");
  75.    }
  76.  
  77.    virtual void emit_thread_end()
  78.    {
  79.       unreachable("Not reached");
  80.    }
  81.  
  82.    virtual void emit_urb_write_header(int mrf)
  83.    {
  84.       unreachable("Not reached");
  85.    }
  86.  
  87.    virtual vec4_instruction *emit_urb_write_opcode(bool complete)
  88.    {
  89.       unreachable("Not reached");
  90.    }
  91. };
  92.  
  93.  
  94. void copy_propagation_test::SetUp()
  95. {
  96.    brw = (struct brw_context *)calloc(1, sizeof(*brw));
  97.    devinfo = (struct brw_device_info *)calloc(1, sizeof(*brw));
  98.    brw->intelScreen = (struct intel_screen *)calloc(1, sizeof(*brw->intelScreen));
  99.    brw->intelScreen->devinfo = devinfo;
  100.    ctx = &brw->ctx;
  101.  
  102.    vp = ralloc(NULL, struct brw_vertex_program);
  103.  
  104.    shader_prog = ralloc(NULL, struct gl_shader_program);
  105.  
  106.    v = new copy_propagation_vec4_visitor(brw, shader_prog);
  107.  
  108.    _mesa_init_vertex_program(ctx, &vp->program, GL_VERTEX_SHADER, 0);
  109.  
  110.    brw->gen = devinfo->gen = 4;
  111. }
  112.  
  113. static void
  114. copy_propagation(vec4_visitor *v)
  115. {
  116.    bool print = false;
  117.  
  118.    if (print) {
  119.       fprintf(stderr, "instructions before:\n");
  120.       v->dump_instructions();
  121.    }
  122.  
  123.    v->calculate_cfg();
  124.    v->opt_copy_propagation();
  125.  
  126.    if (print) {
  127.       fprintf(stderr, "instructions after:\n");
  128.       v->dump_instructions();
  129.    }
  130. }
  131.  
  132. TEST_F(copy_propagation_test, test_swizzle_swizzle)
  133. {
  134.    dst_reg a = dst_reg(v, glsl_type::vec4_type);
  135.    dst_reg b = dst_reg(v, glsl_type::vec4_type);
  136.    dst_reg c = dst_reg(v, glsl_type::vec4_type);
  137.  
  138.    v->emit(v->ADD(a, src_reg(a), src_reg(a)));
  139.  
  140.    v->emit(v->MOV(b, swizzle(src_reg(a), BRW_SWIZZLE4(SWIZZLE_Y,
  141.                                                       SWIZZLE_Z,
  142.                                                       SWIZZLE_W,
  143.                                                       SWIZZLE_X))));
  144.  
  145.    vec4_instruction *test_mov =
  146.       v->MOV(c, swizzle(src_reg(b), BRW_SWIZZLE4(SWIZZLE_Y,
  147.                                                  SWIZZLE_Z,
  148.                                                  SWIZZLE_W,
  149.                                                  SWIZZLE_X)));
  150.    v->emit(test_mov);
  151.  
  152.    copy_propagation(v);
  153.  
  154.    EXPECT_EQ(test_mov->src[0].reg, a.reg);
  155.    EXPECT_EQ(test_mov->src[0].swizzle, BRW_SWIZZLE4(SWIZZLE_Z,
  156.                                                     SWIZZLE_W,
  157.                                                     SWIZZLE_X,
  158.                                                     SWIZZLE_Y));
  159. }
  160.  
  161. TEST_F(copy_propagation_test, test_swizzle_writemask)
  162. {
  163.    dst_reg a = dst_reg(v, glsl_type::vec4_type);
  164.    dst_reg b = dst_reg(v, glsl_type::vec4_type);
  165.    dst_reg c = dst_reg(v, glsl_type::vec4_type);
  166.  
  167.    v->emit(v->MOV(b, swizzle(src_reg(a), BRW_SWIZZLE4(SWIZZLE_X,
  168.                                                       SWIZZLE_Y,
  169.                                                       SWIZZLE_X,
  170.                                                       SWIZZLE_Z))));
  171.  
  172.    v->emit(v->MOV(writemask(a, WRITEMASK_XYZ), src_reg(1.0f)));
  173.  
  174.    vec4_instruction *test_mov =
  175.       v->MOV(c, swizzle(src_reg(b), BRW_SWIZZLE4(SWIZZLE_W,
  176.                                                  SWIZZLE_W,
  177.                                                  SWIZZLE_W,
  178.                                                  SWIZZLE_W)));
  179.    v->emit(test_mov);
  180.  
  181.    copy_propagation(v);
  182.  
  183.    /* should not copy propagate */
  184.    EXPECT_EQ(test_mov->src[0].reg, b.reg);
  185.    EXPECT_EQ(test_mov->src[0].swizzle, BRW_SWIZZLE4(SWIZZLE_W,
  186.                                                     SWIZZLE_W,
  187.                                                     SWIZZLE_W,
  188.                                                     SWIZZLE_W));
  189. }
  190.