summaryrefslogtreecommitdiff
path: root/src/shader_recompiler/backend/glsl/glsl_emit_context.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/shader_recompiler/backend/glsl/glsl_emit_context.h')
-rw-r--r--src/shader_recompiler/backend/glsl/glsl_emit_context.h174
1 files changed, 174 insertions, 0 deletions
diff --git a/src/shader_recompiler/backend/glsl/glsl_emit_context.h b/src/shader_recompiler/backend/glsl/glsl_emit_context.h
new file mode 100644
index 000000000..d9b639d29
--- /dev/null
+++ b/src/shader_recompiler/backend/glsl/glsl_emit_context.h
@@ -0,0 +1,174 @@
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 <string>
8#include <utility>
9#include <vector>
10
11#include <fmt/format.h>
12
13#include "shader_recompiler/backend/glsl/var_alloc.h"
14#include "shader_recompiler/stage.h"
15
16namespace Shader {
17struct Info;
18struct Profile;
19struct RuntimeInfo;
20} // namespace Shader
21
22namespace Shader::Backend {
23struct Bindings;
24}
25
26namespace Shader::IR {
27class Inst;
28struct Program;
29} // namespace Shader::IR
30
31namespace Shader::Backend::GLSL {
32
33struct GenericElementInfo {
34 std::string name;
35 u32 first_element{};
36 u32 num_components{};
37};
38
39struct TextureImageDefinition {
40 u32 binding;
41 u32 count;
42};
43
44class EmitContext {
45public:
46 explicit EmitContext(IR::Program& program, Bindings& bindings, const Profile& profile_,
47 const RuntimeInfo& runtime_info_);
48
49 template <GlslVarType type, typename... Args>
50 void Add(const char* format_str, IR::Inst& inst, Args&&... args) {
51 const auto var_def{var_alloc.AddDefine(inst, type)};
52 if (var_def.empty()) {
53 // skip assigment.
54 code += fmt::format(fmt::runtime(format_str + 3), std::forward<Args>(args)...);
55 } else {
56 code += fmt::format(fmt::runtime(format_str), var_def, std::forward<Args>(args)...);
57 }
58 // TODO: Remove this
59 code += '\n';
60 }
61
62 template <typename... Args>
63 void AddU1(const char* format_str, IR::Inst& inst, Args&&... args) {
64 Add<GlslVarType::U1>(format_str, inst, args...);
65 }
66
67 template <typename... Args>
68 void AddF16x2(const char* format_str, IR::Inst& inst, Args&&... args) {
69 Add<GlslVarType::F16x2>(format_str, inst, args...);
70 }
71
72 template <typename... Args>
73 void AddU32(const char* format_str, IR::Inst& inst, Args&&... args) {
74 Add<GlslVarType::U32>(format_str, inst, args...);
75 }
76
77 template <typename... Args>
78 void AddF32(const char* format_str, IR::Inst& inst, Args&&... args) {
79 Add<GlslVarType::F32>(format_str, inst, args...);
80 }
81
82 template <typename... Args>
83 void AddU64(const char* format_str, IR::Inst& inst, Args&&... args) {
84 Add<GlslVarType::U64>(format_str, inst, args...);
85 }
86
87 template <typename... Args>
88 void AddF64(const char* format_str, IR::Inst& inst, Args&&... args) {
89 Add<GlslVarType::F64>(format_str, inst, args...);
90 }
91
92 template <typename... Args>
93 void AddU32x2(const char* format_str, IR::Inst& inst, Args&&... args) {
94 Add<GlslVarType::U32x2>(format_str, inst, args...);
95 }
96
97 template <typename... Args>
98 void AddF32x2(const char* format_str, IR::Inst& inst, Args&&... args) {
99 Add<GlslVarType::F32x2>(format_str, inst, args...);
100 }
101
102 template <typename... Args>
103 void AddU32x3(const char* format_str, IR::Inst& inst, Args&&... args) {
104 Add<GlslVarType::U32x3>(format_str, inst, args...);
105 }
106
107 template <typename... Args>
108 void AddF32x3(const char* format_str, IR::Inst& inst, Args&&... args) {
109 Add<GlslVarType::F32x3>(format_str, inst, args...);
110 }
111
112 template <typename... Args>
113 void AddU32x4(const char* format_str, IR::Inst& inst, Args&&... args) {
114 Add<GlslVarType::U32x4>(format_str, inst, args...);
115 }
116
117 template <typename... Args>
118 void AddF32x4(const char* format_str, IR::Inst& inst, Args&&... args) {
119 Add<GlslVarType::F32x4>(format_str, inst, args...);
120 }
121
122 template <typename... Args>
123 void AddPrecF32(const char* format_str, IR::Inst& inst, Args&&... args) {
124 Add<GlslVarType::PrecF32>(format_str, inst, args...);
125 }
126
127 template <typename... Args>
128 void AddPrecF64(const char* format_str, IR::Inst& inst, Args&&... args) {
129 Add<GlslVarType::PrecF64>(format_str, inst, args...);
130 }
131
132 template <typename... Args>
133 void Add(const char* format_str, Args&&... args) {
134 code += fmt::format(fmt::runtime(format_str), std::forward<Args>(args)...);
135 // TODO: Remove this
136 code += '\n';
137 }
138
139 std::string header;
140 std::string code;
141 VarAlloc var_alloc;
142 const Info& info;
143 const Profile& profile;
144 const RuntimeInfo& runtime_info;
145
146 Stage stage{};
147 std::string_view stage_name = "invalid";
148 std::string_view position_name = "gl_Position";
149
150 std::vector<TextureImageDefinition> texture_buffers;
151 std::vector<TextureImageDefinition> image_buffers;
152 std::vector<TextureImageDefinition> textures;
153 std::vector<TextureImageDefinition> images;
154 std::array<std::array<GenericElementInfo, 4>, 32> output_generics{};
155
156 u32 num_safety_loop_vars{};
157
158 bool uses_y_direction{};
159 bool uses_cc_carry{};
160 bool uses_geometry_passthrough{};
161
162private:
163 void SetupExtensions();
164 void DefineConstantBuffers(Bindings& bindings);
165 void DefineStorageBuffers(Bindings& bindings);
166 void DefineGenericOutput(size_t index, u32 invocations);
167 void DefineHelperFunctions();
168 void DefineConstants();
169 std::string DefineGlobalMemoryFunctions();
170 void SetupImages(Bindings& bindings);
171 void SetupTextures(Bindings& bindings);
172};
173
174} // namespace Shader::Backend::GLSL