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