summaryrefslogtreecommitdiff
path: root/src/video_core/shader/node.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/video_core/shader/node.h')
-rw-r--r--src/video_core/shader/node.h514
1 files changed, 514 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..c002f90f9
--- /dev/null
+++ b/src/video_core/shader/node.h
@@ -0,0 +1,514 @@
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
21enum class OperationCode {
22 Assign, /// (float& dest, float src) -> void
23
24 Select, /// (MetaArithmetic, bool pred, float a, float b) -> float
25
26 FAdd, /// (MetaArithmetic, float a, float b) -> float
27 FMul, /// (MetaArithmetic, float a, float b) -> float
28 FDiv, /// (MetaArithmetic, float a, float b) -> float
29 FFma, /// (MetaArithmetic, float a, float b, float c) -> float
30 FNegate, /// (MetaArithmetic, float a) -> float
31 FAbsolute, /// (MetaArithmetic, float a) -> float
32 FClamp, /// (MetaArithmetic, float value, float min, float max) -> float
33 FMin, /// (MetaArithmetic, float a, float b) -> float
34 FMax, /// (MetaArithmetic, float a, float b) -> float
35 FCos, /// (MetaArithmetic, float a) -> float
36 FSin, /// (MetaArithmetic, float a) -> float
37 FExp2, /// (MetaArithmetic, float a) -> float
38 FLog2, /// (MetaArithmetic, float a) -> float
39 FInverseSqrt, /// (MetaArithmetic, float a) -> float
40 FSqrt, /// (MetaArithmetic, float a) -> float
41 FRoundEven, /// (MetaArithmetic, float a) -> float
42 FFloor, /// (MetaArithmetic, float a) -> float
43 FCeil, /// (MetaArithmetic, float a) -> float
44 FTrunc, /// (MetaArithmetic, float a) -> float
45 FCastInteger, /// (MetaArithmetic, int a) -> float
46 FCastUInteger, /// (MetaArithmetic, uint a) -> float
47
48 IAdd, /// (MetaArithmetic, int a, int b) -> int
49 IMul, /// (MetaArithmetic, int a, int b) -> int
50 IDiv, /// (MetaArithmetic, int a, int b) -> int
51 INegate, /// (MetaArithmetic, int a) -> int
52 IAbsolute, /// (MetaArithmetic, int a) -> int
53 IMin, /// (MetaArithmetic, int a, int b) -> int
54 IMax, /// (MetaArithmetic, int a, int b) -> int
55 ICastFloat, /// (MetaArithmetic, float a) -> int
56 ICastUnsigned, /// (MetaArithmetic, uint a) -> int
57 ILogicalShiftLeft, /// (MetaArithmetic, int a, uint b) -> int
58 ILogicalShiftRight, /// (MetaArithmetic, int a, uint b) -> int
59 IArithmeticShiftRight, /// (MetaArithmetic, int a, uint b) -> int
60 IBitwiseAnd, /// (MetaArithmetic, int a, int b) -> int
61 IBitwiseOr, /// (MetaArithmetic, int a, int b) -> int
62 IBitwiseXor, /// (MetaArithmetic, int a, int b) -> int
63 IBitwiseNot, /// (MetaArithmetic, int a) -> int
64 IBitfieldInsert, /// (MetaArithmetic, int base, int insert, int offset, int bits) -> int
65 IBitfieldExtract, /// (MetaArithmetic, int value, int offset, int offset) -> int
66 IBitCount, /// (MetaArithmetic, int) -> int
67
68 UAdd, /// (MetaArithmetic, uint a, uint b) -> uint
69 UMul, /// (MetaArithmetic, uint a, uint b) -> uint
70 UDiv, /// (MetaArithmetic, uint a, uint b) -> uint
71 UMin, /// (MetaArithmetic, uint a, uint b) -> uint
72 UMax, /// (MetaArithmetic, uint a, uint b) -> uint
73 UCastFloat, /// (MetaArithmetic, float a) -> uint
74 UCastSigned, /// (MetaArithmetic, int a) -> uint
75 ULogicalShiftLeft, /// (MetaArithmetic, uint a, uint b) -> uint
76 ULogicalShiftRight, /// (MetaArithmetic, uint a, uint b) -> uint
77 UArithmeticShiftRight, /// (MetaArithmetic, uint a, uint b) -> uint
78 UBitwiseAnd, /// (MetaArithmetic, uint a, uint b) -> uint
79 UBitwiseOr, /// (MetaArithmetic, uint a, uint b) -> uint
80 UBitwiseXor, /// (MetaArithmetic, uint a, uint b) -> uint
81 UBitwiseNot, /// (MetaArithmetic, uint a) -> uint
82 UBitfieldInsert, /// (MetaArithmetic, uint base, uint insert, int offset, int bits) -> uint
83 UBitfieldExtract, /// (MetaArithmetic, uint value, int offset, int offset) -> uint
84 UBitCount, /// (MetaArithmetic, uint) -> uint
85
86 HAdd, /// (MetaArithmetic, f16vec2 a, f16vec2 b) -> f16vec2
87 HMul, /// (MetaArithmetic, f16vec2 a, f16vec2 b) -> f16vec2
88 HFma, /// (MetaArithmetic, f16vec2 a, f16vec2 b, f16vec2 c) -> f16vec2
89 HAbsolute, /// (f16vec2 a) -> f16vec2
90 HNegate, /// (f16vec2 a, bool first, bool second) -> f16vec2
91 HClamp, /// (f16vec2 src, float min, float max) -> f16vec2
92 HUnpack, /// (Tegra::Shader::HalfType, T value) -> f16vec2
93 HMergeF32, /// (f16vec2 src) -> float
94 HMergeH0, /// (f16vec2 dest, f16vec2 src) -> f16vec2
95 HMergeH1, /// (f16vec2 dest, f16vec2 src) -> f16vec2
96 HPack2, /// (float a, float b) -> f16vec2
97
98 LogicalAssign, /// (bool& dst, bool src) -> void
99 LogicalAnd, /// (bool a, bool b) -> bool
100 LogicalOr, /// (bool a, bool b) -> bool
101 LogicalXor, /// (bool a, bool b) -> bool
102 LogicalNegate, /// (bool a) -> bool
103 LogicalPick2, /// (bool2 pair, uint index) -> bool
104 LogicalAll2, /// (bool2 a) -> bool
105 LogicalAny2, /// (bool2 a) -> bool
106
107 LogicalFLessThan, /// (float a, float b) -> bool
108 LogicalFEqual, /// (float a, float b) -> bool
109 LogicalFLessEqual, /// (float a, float b) -> bool
110 LogicalFGreaterThan, /// (float a, float b) -> bool
111 LogicalFNotEqual, /// (float a, float b) -> bool
112 LogicalFGreaterEqual, /// (float a, float b) -> bool
113 LogicalFIsNan, /// (float a) -> bool
114
115 LogicalILessThan, /// (int a, int b) -> bool
116 LogicalIEqual, /// (int a, int b) -> bool
117 LogicalILessEqual, /// (int a, int b) -> bool
118 LogicalIGreaterThan, /// (int a, int b) -> bool
119 LogicalINotEqual, /// (int a, int b) -> bool
120 LogicalIGreaterEqual, /// (int a, int b) -> bool
121
122 LogicalULessThan, /// (uint a, uint b) -> bool
123 LogicalUEqual, /// (uint a, uint b) -> bool
124 LogicalULessEqual, /// (uint a, uint b) -> bool
125 LogicalUGreaterThan, /// (uint a, uint b) -> bool
126 LogicalUNotEqual, /// (uint a, uint b) -> bool
127 LogicalUGreaterEqual, /// (uint a, uint b) -> bool
128
129 Logical2HLessThan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
130 Logical2HEqual, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
131 Logical2HLessEqual, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
132 Logical2HGreaterThan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
133 Logical2HNotEqual, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
134 Logical2HGreaterEqual, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
135 Logical2HLessThanWithNan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
136 Logical2HEqualWithNan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
137 Logical2HLessEqualWithNan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
138 Logical2HGreaterThanWithNan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
139 Logical2HNotEqualWithNan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
140 Logical2HGreaterEqualWithNan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
141
142 Texture, /// (MetaTexture, float[N] coords) -> float4
143 TextureLod, /// (MetaTexture, float[N] coords) -> float4
144 TextureGather, /// (MetaTexture, float[N] coords) -> float4
145 TextureQueryDimensions, /// (MetaTexture, float a) -> float4
146 TextureQueryLod, /// (MetaTexture, float[N] coords) -> float4
147 TexelFetch, /// (MetaTexture, int[N], int) -> float4
148
149 Branch, /// (uint branch_target) -> void
150 PushFlowStack, /// (uint branch_target) -> void
151 PopFlowStack, /// () -> void
152 Exit, /// () -> void
153 Discard, /// () -> void
154
155 EmitVertex, /// () -> void
156 EndPrimitive, /// () -> void
157
158 YNegate, /// () -> float
159 LocalInvocationIdX, /// () -> uint
160 LocalInvocationIdY, /// () -> uint
161 LocalInvocationIdZ, /// () -> uint
162 WorkGroupIdX, /// () -> uint
163 WorkGroupIdY, /// () -> uint
164 WorkGroupIdZ, /// () -> uint
165
166 Amount,
167};
168
169enum class InternalFlag {
170 Zero = 0,
171 Sign = 1,
172 Carry = 2,
173 Overflow = 3,
174 Amount = 4,
175};
176
177class OperationNode;
178class ConditionalNode;
179class GprNode;
180class ImmediateNode;
181class InternalFlagNode;
182class PredicateNode;
183class AbufNode;
184class CbufNode;
185class LmemNode;
186class GmemNode;
187class CommentNode;
188
189using NodeData =
190 std::variant<OperationNode, ConditionalNode, GprNode, ImmediateNode, InternalFlagNode,
191 PredicateNode, AbufNode, CbufNode, LmemNode, GmemNode, CommentNode>;
192using Node = std::shared_ptr<NodeData>;
193using Node4 = std::array<Node, 4>;
194using NodeBlock = std::vector<Node>;
195
196class Sampler {
197public:
198 /// This constructor is for bound 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 /// This constructor is 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 /// This constructor is 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
270/// Parameters describing an arithmetic operation
271struct MetaArithmetic {
272 bool precise{}; ///< Whether the operation can be constraint or not
273};
274
275/// Parameters describing a texture sampler
276struct MetaTexture {
277 const Sampler& sampler;
278 Node array;
279 Node depth_compare;
280 std::vector<Node> aoffi;
281 Node bias;
282 Node lod;
283 Node component{};
284 u32 element{};
285};
286
287/// Parameters that modify an operation but are not part of any particular operand
288using Meta = std::variant<MetaArithmetic, MetaTexture, Tegra::Shader::HalfType>;
289
290/// Holds any kind of operation that can be done in the IR
291class OperationNode final {
292public:
293 explicit OperationNode(OperationCode code) : OperationNode(code, Meta{}) {}
294
295 explicit OperationNode(OperationCode code, Meta meta)
296 : OperationNode(code, meta, std::vector<Node>{}) {}
297
298 explicit OperationNode(OperationCode code, std::vector<Node> operands)
299 : OperationNode(code, Meta{}, std::move(operands)) {}
300
301 explicit OperationNode(OperationCode code, Meta meta, std::vector<Node> operands)
302 : code{code}, meta{std::move(meta)}, operands{std::move(operands)} {}
303
304 template <typename... Args>
305 explicit OperationNode(OperationCode code, Meta meta, Args&&... operands)
306 : code{code}, meta{std::move(meta)}, operands{operands...} {}
307
308 OperationCode GetCode() const {
309 return code;
310 }
311
312 const Meta& GetMeta() const {
313 return meta;
314 }
315
316 std::size_t GetOperandsCount() const {
317 return operands.size();
318 }
319
320 const Node& operator[](std::size_t operand_index) const {
321 return operands.at(operand_index);
322 }
323
324private:
325 OperationCode code{};
326 Meta meta{};
327 std::vector<Node> operands;
328};
329
330/// Encloses inside any kind of node that returns a boolean conditionally-executed code
331class ConditionalNode final {
332public:
333 explicit ConditionalNode(Node condition, std::vector<Node>&& code)
334 : condition{std::move(condition)}, code{std::move(code)} {}
335
336 const Node& GetCondition() const {
337 return condition;
338 }
339
340 const std::vector<Node>& GetCode() const {
341 return code;
342 }
343
344private:
345 Node condition; ///< Condition to be satisfied
346 std::vector<Node> code; ///< Code to execute
347};
348
349/// A general purpose register
350class GprNode final {
351public:
352 explicit constexpr GprNode(Tegra::Shader::Register index) : index{index} {}
353
354 u32 GetIndex() const {
355 return static_cast<u32>(index);
356 }
357
358private:
359 Tegra::Shader::Register index{};
360};
361
362/// A 32-bits value that represents an immediate value
363class ImmediateNode final {
364public:
365 explicit constexpr ImmediateNode(u32 value) : value{value} {}
366
367 u32 GetValue() const {
368 return value;
369 }
370
371private:
372 u32 value{};
373};
374
375/// One of Maxwell's internal flags
376class InternalFlagNode final {
377public:
378 explicit constexpr InternalFlagNode(InternalFlag flag) : flag{flag} {}
379
380 InternalFlag GetFlag() const {
381 return flag;
382 }
383
384private:
385 InternalFlag flag{};
386};
387
388/// A predicate register, it can be negated without additional nodes
389class PredicateNode final {
390public:
391 explicit constexpr PredicateNode(Tegra::Shader::Pred index, bool negated)
392 : index{index}, negated{negated} {}
393
394 Tegra::Shader::Pred GetIndex() const {
395 return index;
396 }
397
398 bool IsNegated() const {
399 return negated;
400 }
401
402private:
403 Tegra::Shader::Pred index{};
404 bool negated{};
405};
406
407/// Attribute buffer memory (known as attributes or varyings in GLSL terms)
408class AbufNode final {
409public:
410 // Initialize for standard attributes (index is explicit).
411 explicit AbufNode(Tegra::Shader::Attribute::Index index, u32 element, Node buffer = {})
412 : buffer{std::move(buffer)}, index{index}, element{element} {}
413
414 // Initialize for physical attributes (index is a variable value).
415 explicit AbufNode(Node physical_address, Node buffer = {})
416 : physical_address{std::move(physical_address)}, buffer{std::move(buffer)} {}
417
418 Tegra::Shader::Attribute::Index GetIndex() const {
419 return index;
420 }
421
422 u32 GetElement() const {
423 return element;
424 }
425
426 const Node& GetBuffer() const {
427 return buffer;
428 }
429
430 bool IsPhysicalBuffer() const {
431 return static_cast<bool>(physical_address);
432 }
433
434 const Node& GetPhysicalAddress() const {
435 return physical_address;
436 }
437
438private:
439 Node physical_address;
440 Node buffer;
441 Tegra::Shader::Attribute::Index index{};
442 u32 element{};
443};
444
445/// Constant buffer node, usually mapped to uniform buffers in GLSL
446class CbufNode final {
447public:
448 explicit CbufNode(u32 index, Node offset) : index{index}, offset{std::move(offset)} {}
449
450 u32 GetIndex() const {
451 return index;
452 }
453
454 const Node& GetOffset() const {
455 return offset;
456 }
457
458private:
459 u32 index{};
460 Node offset;
461};
462
463/// Local memory node
464class LmemNode final {
465public:
466 explicit LmemNode(Node address) : address{std::move(address)} {}
467
468 const Node& GetAddress() const {
469 return address;
470 }
471
472private:
473 Node address;
474};
475
476/// Global memory node
477class GmemNode final {
478public:
479 explicit GmemNode(Node real_address, Node base_address, const GlobalMemoryBase& descriptor)
480 : real_address{std::move(real_address)}, base_address{std::move(base_address)},
481 descriptor{descriptor} {}
482
483 const Node& GetRealAddress() const {
484 return real_address;
485 }
486
487 const Node& GetBaseAddress() const {
488 return base_address;
489 }
490
491 const GlobalMemoryBase& GetDescriptor() const {
492 return descriptor;
493 }
494
495private:
496 Node real_address;
497 Node base_address;
498 GlobalMemoryBase descriptor;
499};
500
501/// Commentary, can be dropped
502class CommentNode final {
503public:
504 explicit CommentNode(std::string text) : text{std::move(text)} {}
505
506 const std::string& GetText() const {
507 return text;
508 }
509
510private:
511 std::string text;
512};
513
514} // namespace VideoCommon::Shader