r600/sfn: Add a basic nir shader backend
[mesa.git] / src / gallium / drivers / r600 / sfn / sfn_value_gpr.h
1 /* -*- mesa-c++ -*-
2 *
3 * Copyright (c) 2019 Collabora LTD
4 *
5 * Author: Gert Wollny <gert.wollny@collabora.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * on the rights to use, copy, modify, merge, publish, distribute, sub
11 * license, and/or sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27 #ifndef SFN_GPRARRAY_H
28 #define SFN_GPRARRAY_H
29
30 #include "sfn_value.h"
31 #include <vector>
32
33 namespace r600 {
34
35 class ValuePool;
36 class ValueMap;
37
38 class GPRValue : public Value {
39 public:
40 GPRValue() = default;
41 GPRValue(GPRValue&& orig) = default;
42 GPRValue(const GPRValue& orig) = default;
43
44 GPRValue(uint32_t sel, uint32_t chan, int base_offset);
45
46 GPRValue(uint32_t sel, uint32_t chan);
47
48 GPRValue& operator = (const GPRValue& orig) = default;
49 GPRValue& operator = (GPRValue&& orig) = default;
50
51 uint32_t sel() const override final;
52
53 void set_as_input(){ m_input = true; }
54 bool is_input() const {return m_input; }
55 void set_pin_to_channel() { m_pin_to_channel = true;}
56 bool pin_to_channel() const { return m_pin_to_channel;}
57
58 private:
59 void do_print(std::ostream& os) const override;
60 void do_print(std::ostream& os, const PrintFlags& flags) const override;
61 bool is_equal_to(const Value& other) const override;
62 uint32_t m_sel;
63 bool m_base_offset;
64 bool m_input;
65 bool m_pin_to_channel;
66 };
67
68 class GPRVector : public Value {
69 public:
70 using Swizzle = std::array<uint32_t,4>;
71 using Values = std::array<PValue,4>;
72 GPRVector() = default;
73 GPRVector(GPRVector&& orig) = default;
74 GPRVector(const GPRVector& orig);
75
76 GPRVector(const GPRVector& orig, const std::array<uint8_t, 4>& swizzle);
77 GPRVector(std::array<PValue,4> elms);
78 GPRVector(uint32_t sel, std::array<uint32_t,4> swizzle);
79
80 GPRVector& operator = (const GPRVector& orig) = default;
81 GPRVector& operator = (GPRVector&& orig) = default;
82
83 void swizzle(const Swizzle& swz);
84
85 uint32_t sel() const override final;
86
87 void set_reg_i(int i, PValue reg);
88
89 unsigned chan_i(int i) const {return m_elms[i]->chan();}
90 PValue reg_i(int i) const {return m_elms[i];}
91 PValue operator [] (int i) const {return m_elms[i];}
92 PValue& operator [] (int i) {return m_elms[i];}
93
94
95 PValue x() const {return m_elms[0];}
96 PValue y() const {return m_elms[1];}
97 PValue z() const {return m_elms[2];}
98 PValue w() const {return m_elms[3];}
99
100
101 private:
102 void do_print(std::ostream& os) const override;
103 bool is_equal_to(const Value& other) const override;
104 void validate() const;
105
106 Values m_elms;
107 mutable bool m_valid;
108 };
109
110
111 class GPRArray : public Value
112 {
113 public:
114 using Pointer = std::shared_ptr<GPRArray>;
115
116 GPRArray(int base, int size, int comp_mask, int frac);
117
118 uint32_t sel() const override;
119
120 size_t size() const {return m_values.size();}
121
122 PValue get_indirect(unsigned index, PValue indirect, unsigned component);
123
124 void collect_registers(ValueMap& output) const;
125
126 private:
127 void do_print(std::ostream& os) const override;
128
129 bool is_equal_to(const Value& other) const override;
130
131 int m_base_index;
132 int m_component_mask;
133 int m_frac;
134
135 std::vector<GPRVector> m_values;
136 };
137
138 using PGPRArray = GPRArray::Pointer;
139
140 class GPRArrayValue :public Value {
141 public:
142 GPRArrayValue(PValue value, GPRArray *array);
143 GPRArrayValue(PValue value, PValue index, GPRArray *array);
144
145 size_t array_size() const;
146 uint32_t sel() const override;
147
148 PValue value() {return m_value;}
149
150 void reset_value(PValue new_value);
151 void reset_addr(PValue new_addr);
152
153 Value::Pointer indirect() const {return m_addr;}
154
155 private:
156
157 void do_print(std::ostream& os) const override;
158
159 bool is_equal_to(const Value& other) const override;
160
161 PValue m_value;
162 PValue m_addr;
163 GPRArray *m_array;
164 };
165
166 inline size_t GPRArrayValue::array_size() const
167 {
168 return m_array->size();
169 }
170
171 inline GPRVector::Swizzle swizzle_from_mask(unsigned ncomp)
172 {
173 GPRVector::Swizzle swz = {0,1,2,3};
174 for (int i = ncomp; i < 4; ++i)
175 swz[i] = 7;
176 return swz;
177 }
178
179
180 }
181
182 #endif // SFN_GPRARRAY_H