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