summaryrefslogtreecommitdiff
path: root/src/video_core/shader/node.h
diff options
context:
space:
mode:
authorGravatar ReinUsesLisp2019-06-06 19:31:14 -0300
committerGravatar ReinUsesLisp2019-06-06 20:02:37 -0300
commite1b3be7ced2f6c036dce456de16886451b205791 (patch)
tree308d086d9b8c6716d499625979af2ef13c253a0c /src/video_core/shader/node.h
parentMerge pull request #2552 from ReinUsesLisp/shader-shared-ptr (diff)
downloadyuzu-e1b3be7ced2f6c036dce456de16886451b205791.tar.gz
yuzu-e1b3be7ced2f6c036dce456de16886451b205791.tar.xz
yuzu-e1b3be7ced2f6c036dce456de16886451b205791.zip
shader: Move Node declarations out of the shader IR header
Analysis passes do not have a good reason to depend on shader_ir.h to work on top of nodes. This splits node-related declarations to their own file and leaves the IR in shader_ir.h
Diffstat (limited to 'src/video_core/shader/node.h')
-rw-r--r--src/video_core/shader/node.h510
1 files changed, 510 insertions, 0 deletions
diff --git a/src/video_core/shader/node.h b/src/video_core/shader/node.h
new file mode 100644
index 000000000..1e7a5f699
--- /dev/null
+++ b/src/video_core/shader/node.h
@@ -0,0 +1,510 @@
1// Copyright 2019 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <array>
8#include <cstddef>
9#include <memory>
10#include <string>
11#include <tuple>
12#include <utility>
13#include <variant>
14#include <vector>
15
16#include "common/common_types.h"
17#include "video_core/engines/shader_bytecode.h"
18
19namespace VideoCommon::Shader {
20
21class OperationNode;
22class ConditionalNode;
23class GprNode;
24class ImmediateNode;
25class InternalFlagNode;
26class PredicateNode;
27class AbufNode; ///< Attribute buffer
28class CbufNode; ///< Constant buffer
29class LmemNode; ///< Local memory
30class GmemNode; ///< Global memory
31class CommentNode;
32
33using NodeData =
34 std::variant<OperationNode, ConditionalNode, GprNode, ImmediateNode, InternalFlagNode,
35 PredicateNode, AbufNode, CbufNode, LmemNode, GmemNode, CommentNode>;
36using Node = std::shared_ptr<NodeData>;
37using Node4 = std::array<Node, 4>;
38using NodeBlock = std::vector<Node>;
39
40enum class OperationCode {
41 Assign, /// (float& dest, float src) -> void
42
43 Select, /// (MetaArithmetic, bool pred, float a, float b) -> float
44
45 FAdd, /// (MetaArithmetic, float a, float b) -> float
46 FMul, /// (MetaArithmetic, float a, float b) -> float
47 FDiv, /// (MetaArithmetic, float a, float b) -> float
48 FFma, /// (MetaArithmetic, float a, float b, float c) -> float
49 FNegate, /// (MetaArithmetic, float a) -> float
50 FAbsolute, /// (MetaArithmetic, float a) -> float
51 FClamp, /// (MetaArithmetic, float value, float min, float max) -> float
52 FMin, /// (MetaArithmetic, float a, float b) -> float
53 FMax, /// (MetaArithmetic, float a, float b) -> float
54 FCos, /// (MetaArithmetic, float a) -> float
55 FSin, /// (MetaArithmetic, float a) -> float
56 FExp2, /// (MetaArithmetic, float a) -> float
57 FLog2, /// (MetaArithmetic, float a) -> float
58 FInverseSqrt, /// (MetaArithmetic, float a) -> float
59 FSqrt, /// (MetaArithmetic, float a) -> float
60 FRoundEven, /// (MetaArithmetic, float a) -> float
61 FFloor, /// (MetaArithmetic, float a) -> float
62 FCeil, /// (MetaArithmetic, float a) -> float
63 FTrunc, /// (MetaArithmetic, float a) -> float
64 FCastInteger, /// (MetaArithmetic, int a) -> float
65 FCastUInteger, /// (MetaArithmetic, uint a) -> float
66
67 IAdd, /// (MetaArithmetic, int a, int b) -> int
68 IMul, /// (MetaArithmetic, int a, int b) -> int
69 IDiv, /// (MetaArithmetic, int a, int b) -> int
70 INegate, /// (MetaArithmetic, int a) -> int
71 IAbsolute, /// (MetaArithmetic, int a) -> int
72 IMin, /// (MetaArithmetic, int a, int b) -> int
73 IMax, /// (MetaArithmetic, int a, int b) -> int
74 ICastFloat, /// (MetaArithmetic, float a) -> int
75 ICastUnsigned, /// (MetaArithmetic, uint a) -> int
76 ILogicalShiftLeft, /// (MetaArithmetic, int a, uint b) -> int
77 ILogicalShiftRight, /// (MetaArithmetic, int a, uint b) -> int
78 IArithmeticShiftRight, /// (MetaArithmetic, int a, uint b) -> int
79 IBitwiseAnd, /// (MetaArithmetic, int a, int b) -> int
80 IBitwiseOr, /// (MetaArithmetic, int a, int b) -> int
81 IBitwiseXor, /// (MetaArithmetic, int a, int b) -> int
82 IBitwiseNot, /// (MetaArithmetic, int a) -> int
83 IBitfieldInsert, /// (MetaArithmetic, int base, int insert, int offset, int bits) -> int
84 IBitfieldExtract, /// (MetaArithmetic, int value, int offset, int offset) -> int
85 IBitCount, /// (MetaArithmetic, int) -> int
86
87 UAdd, /// (MetaArithmetic, uint a, uint b) -> uint
88 UMul, /// (MetaArithmetic, uint a, uint b) -> uint
89 UDiv, /// (MetaArithmetic, uint a, uint b) -> uint
90 UMin, /// (MetaArithmetic, uint a, uint b) -> uint
91 UMax, /// (MetaArithmetic, uint a, uint b) -> uint
92 UCastFloat, /// (MetaArithmetic, float a) -> uint
93 UCastSigned, /// (MetaArithmetic, int a) -> uint
94 ULogicalShiftLeft, /// (MetaArithmetic, uint a, uint b) -> uint
95 ULogicalShiftRight, /// (MetaArithmetic, uint a, uint b) -> uint
96 UArithmeticShiftRight, /// (MetaArithmetic, uint a, uint b) -> uint
97 UBitwiseAnd, /// (MetaArithmetic, uint a, uint b) -> uint
98 UBitwiseOr, /// (MetaArithmetic, uint a, uint b) -> uint
99 UBitwiseXor, /// (MetaArithmetic, uint a, uint b) -> uint
100 UBitwiseNot, /// (MetaArithmetic, uint a) -> uint
101 UBitfieldInsert, /// (MetaArithmetic, uint base, uint insert, int offset, int bits) -> uint
102 UBitfieldExtract, /// (MetaArithmetic, uint value, int offset, int offset) -> uint
103 UBitCount, /// (MetaArithmetic, uint) -> uint
104
105 HAdd, /// (MetaArithmetic, f16vec2 a, f16vec2 b) -> f16vec2
106 HMul, /// (MetaArithmetic, f16vec2 a, f16vec2 b) -> f16vec2
107 HFma, /// (MetaArithmetic, f16vec2 a, f16vec2 b, f16vec2 c) -> f16vec2
108 HAbsolute, /// (f16vec2 a) -> f16vec2
109 HNegate, /// (f16vec2 a, bool first, bool second) -> f16vec2
110 HClamp, /// (f16vec2 src, float min, float max) -> f16vec2
111 HUnpack, /// (Tegra::Shader::HalfType, T value) -> f16vec2
112 HMergeF32, /// (f16vec2 src) -> float
113 HMergeH0, /// (f16vec2 dest, f16vec2 src) -> f16vec2
114 HMergeH1, /// (f16vec2 dest, f16vec2 src) -> f16vec2
115 HPack2, /// (float a, float b) -> f16vec2
116
117 LogicalAssign, /// (bool& dst, bool src) -> void
118 LogicalAnd, /// (bool a, bool b) -> bool
119 LogicalOr, /// (bool a, bool b) -> bool
120 LogicalXor, /// (bool a, bool b) -> bool
121 LogicalNegate, /// (bool a) -> bool
122 LogicalPick2, /// (bool2 pair, uint index) -> bool
123 LogicalAll2, /// (bool2 a) -> bool
124 LogicalAny2, /// (bool2 a) -> bool
125
126 LogicalFLessThan, /// (float a, float b) -> bool
127 LogicalFEqual, /// (float a, float b) -> bool
128 LogicalFLessEqual, /// (float a, float b) -> bool
129 LogicalFGreaterThan, /// (float a, float b) -> bool
130 LogicalFNotEqual, /// (float a, float b) -> bool
131 LogicalFGreaterEqual, /// (float a, float b) -> bool
132 LogicalFIsNan, /// (float a) -> bool
133
134 LogicalILessThan, /// (int a, int b) -> bool
135 LogicalIEqual, /// (int a, int b) -> bool
136 LogicalILessEqual, /// (int a, int b) -> bool
137 LogicalIGreaterThan, /// (int a, int b) -> bool
138 LogicalINotEqual, /// (int a, int b) -> bool
139 LogicalIGreaterEqual, /// (int a, int b) -> bool
140
141 LogicalULessThan, /// (uint a, uint b) -> bool
142 LogicalUEqual, /// (uint a, uint b) -> bool
143 LogicalULessEqual, /// (uint a, uint b) -> bool
144 LogicalUGreaterThan, /// (uint a, uint b) -> bool
145 LogicalUNotEqual, /// (uint a, uint b) -> bool
146 LogicalUGreaterEqual, /// (uint a, uint b) -> bool
147
148 Logical2HLessThan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
149 Logical2HEqual, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
150 Logical2HLessEqual, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
151 Logical2HGreaterThan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
152 Logical2HNotEqual, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
153 Logical2HGreaterEqual, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
154 Logical2HLessThanWithNan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
155 Logical2HEqualWithNan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
156 Logical2HLessEqualWithNan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
157 Logical2HGreaterThanWithNan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
158 Logical2HNotEqualWithNan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
159 Logical2HGreaterEqualWithNan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
160
161 Texture, /// (MetaTexture, float[N] coords) -> float4
162 TextureLod, /// (MetaTexture, float[N] coords) -> float4
163 TextureGather, /// (MetaTexture, float[N] coords) -> float4
164 TextureQueryDimensions, /// (MetaTexture, float a) -> float4
165 TextureQueryLod, /// (MetaTexture, float[N] coords) -> float4
166 TexelFetch, /// (MetaTexture, int[N], int) -> float4
167
168 Branch, /// (uint branch_target) -> void
169 PushFlowStack, /// (uint branch_target) -> void
170 PopFlowStack, /// () -> void
171 Exit, /// () -> void
172 Discard, /// () -> void
173
174 EmitVertex, /// () -> void
175 EndPrimitive, /// () -> void
176
177 YNegate, /// () -> float
178 LocalInvocationIdX, /// () -> uint
179 LocalInvocationIdY, /// () -> uint
180 LocalInvocationIdZ, /// () -> uint
181 WorkGroupIdX, /// () -> uint
182 WorkGroupIdY, /// () -> uint
183 WorkGroupIdZ, /// () -> uint
184
185 Amount,
186};
187
188enum class InternalFlag {
189 Zero = 0,
190 Sign = 1,
191 Carry = 2,
192 Overflow = 3,
193 Amount = 4,
194};
195
196class Sampler {
197public:
198 // Use this constructor for bounded Samplers
199 explicit Sampler(std::size_t offset, std::size_t index, Tegra::Shader::TextureType type,
200 bool is_array, bool is_shadow)
201 : offset{offset}, index{index}, type{type}, is_array{is_array}, is_shadow{is_shadow},
202 is_bindless{false} {}
203
204 // Use this constructor for bindless Samplers
205 explicit Sampler(u32 cbuf_index, u32 cbuf_offset, std::size_t index,
206 Tegra::Shader::TextureType type, bool is_array, bool is_shadow)
207 : offset{(static_cast<u64>(cbuf_index) << 32) | cbuf_offset}, index{index}, type{type},
208 is_array{is_array}, is_shadow{is_shadow}, is_bindless{true} {}
209
210 // Use this only for serialization/deserialization
211 explicit Sampler(std::size_t offset, std::size_t index, Tegra::Shader::TextureType type,
212 bool is_array, bool is_shadow, bool is_bindless)
213 : offset{offset}, index{index}, type{type}, is_array{is_array}, is_shadow{is_shadow},
214 is_bindless{is_bindless} {}
215
216 std::size_t GetOffset() const {
217 return offset;
218 }
219
220 std::size_t GetIndex() const {
221 return index;
222 }
223
224 Tegra::Shader::TextureType GetType() const {
225 return type;
226 }
227
228 bool IsArray() const {
229 return is_array;
230 }
231
232 bool IsShadow() const {
233 return is_shadow;
234 }
235
236 bool IsBindless() const {
237 return is_bindless;
238 }
239
240 std::pair<u32, u32> GetBindlessCBuf() const {
241 return {static_cast<u32>(offset >> 32), static_cast<u32>(offset)};
242 }
243
244 bool operator<(const Sampler& rhs) const {
245 return std::tie(index, offset, type, is_array, is_shadow, is_bindless) <
246 std::tie(rhs.index, rhs.offset, rhs.type, rhs.is_array, rhs.is_shadow,
247 rhs.is_bindless);
248 }
249
250private:
251 /// Offset in TSC memory from which to read the sampler object, as specified by the sampling
252 /// instruction.
253 std::size_t offset{};
254 std::size_t index{}; ///< Value used to index into the generated GLSL sampler array.
255 Tegra::Shader::TextureType type{}; ///< The type used to sample this texture (Texture2D, etc)
256 bool is_array{}; ///< Whether the texture is being sampled as an array texture or not.
257 bool is_shadow{}; ///< Whether the texture is being sampled as a depth texture or not.
258 bool is_bindless{}; ///< Whether this sampler belongs to a bindless texture or not.
259};
260
261struct GlobalMemoryBase {
262 u32 cbuf_index{};
263 u32 cbuf_offset{};
264
265 bool operator<(const GlobalMemoryBase& rhs) const {
266 return std::tie(cbuf_index, cbuf_offset) < std::tie(rhs.cbuf_index, rhs.cbuf_offset);
267 }
268};
269
270struct MetaArithmetic {
271 bool precise{};
272};
273
274struct MetaTexture {
275 const Sampler& sampler;
276 Node array{};
277 Node depth_compare{};
278 std::vector<Node> aoffi;
279 Node bias{};
280 Node lod{};
281 Node component{};
282 u32 element{};
283};
284
285using Meta = std::variant<MetaArithmetic, MetaTexture, Tegra::Shader::HalfType>;
286
287/// Holds any kind of operation that can be done in the IR
288class OperationNode final {
289public:
290 explicit OperationNode(OperationCode code) : OperationNode(code, Meta{}) {}
291
292 explicit OperationNode(OperationCode code, Meta meta)
293 : OperationNode(code, meta, std::vector<Node>{}) {}
294
295 explicit OperationNode(OperationCode code, std::vector<Node> operands)
296 : OperationNode(code, Meta{}, std::move(operands)) {}
297
298 explicit OperationNode(OperationCode code, Meta meta, std::vector<Node> operands)
299 : code{code}, meta{std::move(meta)}, operands{std::move(operands)} {}
300
301 template <typename... Args>
302 explicit OperationNode(OperationCode code, Meta meta, Args&&... operands)
303 : code{code}, meta{std::move(meta)}, operands{operands...} {}
304
305 OperationCode GetCode() const {
306 return code;
307 }
308
309 const Meta& GetMeta() const {
310 return meta;
311 }
312
313 std::size_t GetOperandsCount() const {
314 return operands.size();
315 }
316
317 const Node& operator[](std::size_t operand_index) const {
318 return operands.at(operand_index);
319 }
320
321private:
322 OperationCode code{};
323 Meta meta{};
324 std::vector<Node> operands;
325};
326
327/// Encloses inside any kind of node that returns a boolean conditionally-executed code
328class ConditionalNode final {
329public:
330 explicit ConditionalNode(Node condition, std::vector<Node>&& code)
331 : condition{condition}, code{std::move(code)} {}
332
333 Node GetCondition() const {
334 return condition;
335 }
336
337 const std::vector<Node>& GetCode() const {
338 return code;
339 }
340
341private:
342 const Node condition; ///< Condition to be satisfied
343 std::vector<Node> code; ///< Code to execute
344};
345
346/// A general purpose register
347class GprNode final {
348public:
349 explicit constexpr GprNode(Tegra::Shader::Register index) : index{index} {}
350
351 u32 GetIndex() const {
352 return static_cast<u32>(index);
353 }
354
355private:
356 const Tegra::Shader::Register index;
357};
358
359/// A 32-bits value that represents an immediate value
360class ImmediateNode final {
361public:
362 explicit constexpr ImmediateNode(u32 value) : value{value} {}
363
364 u32 GetValue() const {
365 return value;
366 }
367
368private:
369 const u32 value;
370};
371
372/// One of Maxwell's internal flags
373class InternalFlagNode final {
374public:
375 explicit constexpr InternalFlagNode(InternalFlag flag) : flag{flag} {}
376
377 InternalFlag GetFlag() const {
378 return flag;
379 }
380
381private:
382 const InternalFlag flag;
383};
384
385/// A predicate register, it can be negated without additional nodes
386class PredicateNode final {
387public:
388 explicit constexpr PredicateNode(Tegra::Shader::Pred index, bool negated)
389 : index{index}, negated{negated} {}
390
391 Tegra::Shader::Pred GetIndex() const {
392 return index;
393 }
394
395 bool IsNegated() const {
396 return negated;
397 }
398
399private:
400 const Tegra::Shader::Pred index;
401 const bool negated;
402};
403
404/// Attribute buffer memory (known as attributes or varyings in GLSL terms)
405class AbufNode final {
406public:
407 // Initialize for standard attributes (index is explicit).
408 explicit AbufNode(Tegra::Shader::Attribute::Index index, u32 element, Node buffer = {})
409 : buffer{std::move(buffer)}, index{index}, element{element} {}
410
411 // Initialize for physical attributes (index is a variable value).
412 explicit AbufNode(Node physical_address, Node buffer = {})
413 : physical_address{physical_address}, buffer{std::move(buffer)} {}
414
415 Tegra::Shader::Attribute::Index GetIndex() const {
416 return index;
417 }
418
419 u32 GetElement() const {
420 return element;
421 }
422
423 Node GetBuffer() const {
424 return buffer;
425 }
426
427 bool IsPhysicalBuffer() const {
428 return static_cast<bool>(physical_address);
429 }
430
431 const Node& GetPhysicalAddress() const {
432 return physical_address;
433 }
434
435private:
436 Node physical_address;
437 Node buffer;
438 Tegra::Shader::Attribute::Index index{};
439 u32 element{};
440};
441
442/// Constant buffer node, usually mapped to uniform buffers in GLSL
443class CbufNode final {
444public:
445 explicit CbufNode(u32 index, Node offset) : index{index}, offset{offset} {}
446
447 u32 GetIndex() const {
448 return index;
449 }
450
451 Node GetOffset() const {
452 return offset;
453 }
454
455private:
456 const u32 index;
457 const Node offset;
458};
459
460/// Local memory node
461class LmemNode final {
462public:
463 explicit LmemNode(Node address) : address{address} {}
464
465 Node GetAddress() const {
466 return address;
467 }
468
469private:
470 const Node address;
471};
472
473/// Global memory node
474class GmemNode final {
475public:
476 explicit GmemNode(Node real_address, Node base_address, const GlobalMemoryBase& descriptor)
477 : real_address{real_address}, base_address{base_address}, descriptor{descriptor} {}
478
479 Node GetRealAddress() const {
480 return real_address;
481 }
482
483 Node GetBaseAddress() const {
484 return base_address;
485 }
486
487 const GlobalMemoryBase& GetDescriptor() const {
488 return descriptor;
489 }
490
491private:
492 const Node real_address;
493 const Node base_address;
494 const GlobalMemoryBase descriptor;
495};
496
497/// Commentary, can be dropped
498class CommentNode final {
499public:
500 explicit CommentNode(std::string text) : text{std::move(text)} {}
501
502 const std::string& GetText() const {
503 return text;
504 }
505
506private:
507 std::string text;
508};
509
510} // namespace VideoCommon::Shader