summaryrefslogtreecommitdiff
path: root/src/shader_recompiler/backend/glsl/var_alloc.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/shader_recompiler/backend/glsl/var_alloc.h')
-rw-r--r--src/shader_recompiler/backend/glsl/var_alloc.h105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/shader_recompiler/backend/glsl/var_alloc.h b/src/shader_recompiler/backend/glsl/var_alloc.h
new file mode 100644
index 000000000..8b49f32a6
--- /dev/null
+++ b/src/shader_recompiler/backend/glsl/var_alloc.h
@@ -0,0 +1,105 @@
1// Copyright 2021 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 <bitset>
8#include <string>
9#include <vector>
10
11#include "common/bit_field.h"
12#include "common/common_types.h"
13
14namespace Shader::IR {
15class Inst;
16class Value;
17enum class Type;
18} // namespace Shader::IR
19
20namespace Shader::Backend::GLSL {
21enum class GlslVarType : u32 {
22 U1,
23 F16x2,
24 U32,
25 F32,
26 U64,
27 F64,
28 U32x2,
29 F32x2,
30 U32x3,
31 F32x3,
32 U32x4,
33 F32x4,
34 PrecF32,
35 PrecF64,
36 Void,
37};
38
39struct Id {
40 union {
41 u32 raw;
42 BitField<0, 1, u32> is_valid;
43 BitField<1, 4, GlslVarType> type;
44 BitField<6, 26, u32> index;
45 };
46
47 bool operator==(Id rhs) const noexcept {
48 return raw == rhs.raw;
49 }
50 bool operator!=(Id rhs) const noexcept {
51 return !operator==(rhs);
52 }
53};
54static_assert(sizeof(Id) == sizeof(u32));
55
56class VarAlloc {
57public:
58 struct UseTracker {
59 bool uses_temp{};
60 size_t num_used{};
61 std::vector<bool> var_use;
62 };
63
64 /// Used for explicit usages of variables, may revert to temporaries
65 std::string Define(IR::Inst& inst, GlslVarType type);
66 std::string Define(IR::Inst& inst, IR::Type type);
67
68 /// Used to assign variables used by the IR. May return a blank string if
69 /// the instruction's result is unused in the IR.
70 std::string AddDefine(IR::Inst& inst, GlslVarType type);
71 std::string PhiDefine(IR::Inst& inst, IR::Type type);
72
73 std::string Consume(const IR::Value& value);
74 std::string ConsumeInst(IR::Inst& inst);
75
76 std::string GetGlslType(GlslVarType type) const;
77 std::string GetGlslType(IR::Type type) const;
78
79 const UseTracker& GetUseTracker(GlslVarType type) const;
80 std::string Representation(u32 index, GlslVarType type) const;
81
82private:
83 GlslVarType RegType(IR::Type type) const;
84 Id Alloc(GlslVarType type);
85 void Free(Id id);
86 UseTracker& GetUseTracker(GlslVarType type);
87 std::string Representation(Id id) const;
88
89 UseTracker var_bool{};
90 UseTracker var_f16x2{};
91 UseTracker var_u32{};
92 UseTracker var_u32x2{};
93 UseTracker var_u32x3{};
94 UseTracker var_u32x4{};
95 UseTracker var_f32{};
96 UseTracker var_f32x2{};
97 UseTracker var_f32x3{};
98 UseTracker var_f32x4{};
99 UseTracker var_u64{};
100 UseTracker var_f64{};
101 UseTracker var_precf32{};
102 UseTracker var_precf64{};
103};
104
105} // namespace Shader::Backend::GLSL