• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 #include "../sfn_instrfactory.h"
3 
4 #include "../sfn_instr_alu.h"
5 #include "../sfn_instr_tex.h"
6 #include "../sfn_instr_export.h"
7 
8 #include "gtest/gtest.h"
9 #include <sstream>
10 
11 using namespace r600;
12 
13 using std::istringstream;
14 using std::string;
15 using std::vector;
16 
17 class TestShaderFromString : public ::testing::Test
18 {
19 public:
SetUp()20    void SetUp() override {
21       m_instr_factory = new InstrFactory();
22       init_pool();
23    }
24 
TearDown()25    void TearDown() override {
26       release_pool();
27    }
28 
29    TestShaderFromString();
30 
31    std::vector<PInst> from_string(const std::string& s);
32 
33 protected:
34    void check(const vector<PInst>& eval, const std::vector<PInst, Allocator<PInst>>& expect);
35 private:
36    InstrFactory *m_instr_factory = nullptr;
37 };
38 
39 
TEST_F(TestShaderFromString,test_simple_fs)40 TEST_F(TestShaderFromString, test_simple_fs)
41 {
42    auto init_str =
43          R"(
44 
45 # load constant color
46 ALU MOV R2000.x@group : L[0x38000000] {W}
47 ALU MOV R2000.y@group : L[0x0] {W}
48 ALU MOV R2000.z@group : L[0x0] {W}
49 ALU MOV R2000.w@group : L[0x38F00000] {WL}
50 
51 # write output
52 EXPORT_DONE PIXEL 0 R2000.xyzw
53 )";
54 
55 
56    auto shader = from_string(init_str);
57 
58    std::vector<PInst, Allocator<PInst>> expect;
59 
60    expect.push_back(new AluInstr(op1_mov,
61                                  new Register( 2000, 0, pin_group),
62                                  new LiteralConstant(0x38000000),
63                                  {alu_write}));
64 
65    expect.push_back(new AluInstr(op1_mov,
66                                  new Register( 2000, 1, pin_group),
67                                  new LiteralConstant( 0x0),
68                                  {alu_write}));
69 
70    expect.push_back(new AluInstr(op1_mov,
71                                  new Register( 2000, 2, pin_group),
72                                  new LiteralConstant( 0x0),
73                                  {alu_write}));
74 
75    expect.push_back(new AluInstr(op1_mov,
76                                  new Register( 2000, 3, pin_group),
77                                  new LiteralConstant( 0x38F00000),
78                                  {alu_write, alu_last_instr}));
79 
80    auto exp = new ExportInstr(
81             ExportInstr::pixel, 0, RegisterVec4(2000, false));
82    exp->set_is_last_export(true);
83    expect.push_back(exp);
84 
85    check(shader, expect);
86 
87 }
88 
89 
90 
TestShaderFromString()91 TestShaderFromString::TestShaderFromString()
92 {
93 
94 }
95 
from_string(const std::string & s)96 std::vector<PInst> TestShaderFromString::from_string(const std::string& s)
97 {
98    istringstream is(s);
99    string line;
100 
101    std::vector<PInst> shader;
102 
103    while (std::getline(is, line)) {
104       if (line.find_first_not_of(" \t") == std::string::npos)
105          continue;
106       if (line[0] == '#')
107          continue;
108 
109       shader.push_back(m_instr_factory->from_string(line, 0));
110    }
111 
112    return shader;
113 }
114 
check(const vector<PInst> & eval,const std::vector<PInst,Allocator<PInst>> & expect)115 void TestShaderFromString::check(const vector<PInst>& eval,
116                                  const std::vector<PInst, Allocator<PInst>>& expect)
117 {
118    ASSERT_EQ(eval.size(), expect.size());
119 
120    for (unsigned i = 0; i < eval.size(); ++i) {
121       EXPECT_EQ(*eval[i], *expect[i]);
122    }
123 }
124