summaryrefslogtreecommitdiff
path: root/src/shader_recompiler/backend/glsl/emit_context.cpp
diff options
context:
space:
mode:
authorGravatar ameerj2021-05-28 13:54:09 -0400
committerGravatar ameerj2021-07-22 21:51:36 -0400
commitf6bbc76336942454a862280e5b2158ceab49a173 (patch)
treeb81e925154bfa7dbbd1aedc50d167fa87905b3db /src/shader_recompiler/backend/glsl/emit_context.cpp
parentglsl: Fix bindings, add some CC ops (diff)
downloadyuzu-f6bbc76336942454a862280e5b2158ceab49a173.tar.gz
yuzu-f6bbc76336942454a862280e5b2158ceab49a173.tar.xz
yuzu-f6bbc76336942454a862280e5b2158ceab49a173.zip
glsl: WIP var forward declaration
to fix Loop control flow.
Diffstat (limited to 'src/shader_recompiler/backend/glsl/emit_context.cpp')
-rw-r--r--src/shader_recompiler/backend/glsl/emit_context.cpp64
1 files changed, 34 insertions, 30 deletions
diff --git a/src/shader_recompiler/backend/glsl/emit_context.cpp b/src/shader_recompiler/backend/glsl/emit_context.cpp
index 7bd6b3605..3530e89e5 100644
--- a/src/shader_recompiler/backend/glsl/emit_context.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_context.cpp
@@ -29,7 +29,10 @@ std::string_view SamplerType(TextureType type) {
29 return "sampler2DArray"; 29 return "sampler2DArray";
30 case TextureType::Color3D: 30 case TextureType::Color3D:
31 return "sampler3D"; 31 return "sampler3D";
32 case TextureType::ColorCube:
33 return "samplerCube";
32 default: 34 default:
35 fmt::print("Texture type: {}", type);
33 throw NotImplementedException("Texture type: {}", type); 36 throw NotImplementedException("Texture type: {}", type);
34 } 37 }
35} 38}
@@ -39,7 +42,6 @@ std::string_view SamplerType(TextureType type) {
39EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile& profile_, 42EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile& profile_,
40 const RuntimeInfo& runtime_info_) 43 const RuntimeInfo& runtime_info_)
41 : info{program.info}, profile{profile_}, runtime_info{runtime_info_} { 44 : info{program.info}, profile{profile_}, runtime_info{runtime_info_} {
42 std::string header = "";
43 SetupExtensions(header); 45 SetupExtensions(header);
44 stage = program.stage; 46 stage = program.stage;
45 switch (program.stage) { 47 switch (program.stage) {
@@ -67,24 +69,23 @@ EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile
67 program.workgroup_size[2]); 69 program.workgroup_size[2]);
68 break; 70 break;
69 } 71 }
70 code += header;
71 const std::string_view attr_stage{stage == Stage::Fragment ? "fragment" : "vertex"}; 72 const std::string_view attr_stage{stage == Stage::Fragment ? "fragment" : "vertex"};
72 for (size_t index = 0; index < info.input_generics.size(); ++index) { 73 for (size_t index = 0; index < info.input_generics.size(); ++index) {
73 const auto& generic{info.input_generics[index]}; 74 const auto& generic{info.input_generics[index]};
74 if (generic.used) { 75 if (generic.used) {
75 Add("layout(location={}) {} in vec4 in_attr{};", index, 76 header += fmt::format("layout(location={}) {} in vec4 in_attr{};", index,
76 InterpDecorator(generic.interpolation), index); 77 InterpDecorator(generic.interpolation), index);
77 } 78 }
78 } 79 }
79 for (size_t index = 0; index < info.stores_frag_color.size(); ++index) { 80 for (size_t index = 0; index < info.stores_frag_color.size(); ++index) {
80 if (!info.stores_frag_color[index]) { 81 if (!info.stores_frag_color[index]) {
81 continue; 82 continue;
82 } 83 }
83 Add("layout(location={})out vec4 frag_color{};", index, index); 84 header += fmt::format("layout(location={})out vec4 frag_color{};", index, index);
84 } 85 }
85 for (size_t index = 0; index < info.stores_generics.size(); ++index) { 86 for (size_t index = 0; index < info.stores_generics.size(); ++index) {
86 if (info.stores_generics[index]) { 87 if (info.stores_generics[index]) {
87 Add("layout(location={}) out vec4 out_attr{};", index, index); 88 header += fmt::format("layout(location={}) out vec4 out_attr{};", index, index);
88 } 89 }
89 } 90 }
90 DefineConstantBuffers(bindings); 91 DefineConstantBuffers(bindings);
@@ -92,14 +93,15 @@ EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile
92 SetupImages(bindings); 93 SetupImages(bindings);
93 DefineHelperFunctions(); 94 DefineHelperFunctions();
94 95
95 Add("void main(){{"); 96 header += "void main(){\n";
96 if (stage == Stage::VertexA || stage == Stage::VertexB) { 97 if (stage == Stage::VertexA || stage == Stage::VertexB) {
97 Add("gl_Position = vec4(0.0f, 0.0f, 0.0f, 1.0f);"); 98 Add("gl_Position = vec4(0.0f, 0.0f, 0.0f, 1.0f);");
98 } 99 }
99} 100}
100 101
101void EmitContext::SetupExtensions(std::string& header) { 102void EmitContext::SetupExtensions(std::string&) {
102 header += "#extension GL_ARB_separate_shader_objects : enable\n"; 103 header += "#extension GL_ARB_separate_shader_objects : enable\n";
104 // header += "#extension GL_ARB_texture_cube_map_array : enable\n";
103 if (info.uses_int64) { 105 if (info.uses_int64) {
104 header += "#extension GL_ARB_gpu_shader_int64 : enable\n"; 106 header += "#extension GL_ARB_gpu_shader_int64 : enable\n";
105 } 107 }
@@ -127,7 +129,8 @@ void EmitContext::DefineConstantBuffers(Bindings& bindings) {
127 return; 129 return;
128 } 130 }
129 for (const auto& desc : info.constant_buffer_descriptors) { 131 for (const auto& desc : info.constant_buffer_descriptors) {
130 Add("layout(std140,binding={}) uniform {}_cbuf_{}{{vec4 {}_cbuf{}[{}];}};", 132 header += fmt::format(
133 "layout(std140,binding={}) uniform {}_cbuf_{}{{vec4 {}_cbuf{}[{}];}};",
131 bindings.uniform_buffer, stage_name, desc.index, stage_name, desc.index, 4 * 1024); 134 bindings.uniform_buffer, stage_name, desc.index, stage_name, desc.index, 4 * 1024);
132 bindings.uniform_buffer += desc.count; 135 bindings.uniform_buffer += desc.count;
133 } 136 }
@@ -138,53 +141,53 @@ void EmitContext::DefineStorageBuffers(Bindings& bindings) {
138 return; 141 return;
139 } 142 }
140 for (const auto& desc : info.storage_buffers_descriptors) { 143 for (const auto& desc : info.storage_buffers_descriptors) {
141 Add("layout(std430,binding={}) buffer ssbo_{}{{uint ssbo{}[];}};", bindings.storage_buffer, 144 header += fmt::format("layout(std430,binding={}) buffer ssbo_{}{{uint ssbo{}[];}};",
142 bindings.storage_buffer, desc.cbuf_index); 145 bindings.storage_buffer, bindings.storage_buffer, desc.cbuf_index);
143 bindings.storage_buffer += desc.count; 146 bindings.storage_buffer += desc.count;
144 } 147 }
145} 148}
146 149
147void EmitContext::DefineHelperFunctions() { 150void EmitContext::DefineHelperFunctions() {
148 if (info.uses_global_increment) { 151 if (info.uses_global_increment) {
149 code += "uint CasIncrement(uint op_a,uint op_b){return(op_a>=op_b)?0u:(op_a+1u);}\n"; 152 header += "uint CasIncrement(uint op_a,uint op_b){return(op_a>=op_b)?0u:(op_a+1u);}\n";
150 } 153 }
151 if (info.uses_global_decrement) { 154 if (info.uses_global_decrement) {
152 code += 155 header +=
153 "uint CasDecrement(uint op_a,uint op_b){return(op_a==0||op_a>op_b)?op_b:(op_a-1u);}\n"; 156 "uint CasDecrement(uint op_a,uint op_b){return(op_a==0||op_a>op_b)?op_b:(op_a-1u);}\n";
154 } 157 }
155 if (info.uses_atomic_f32_add) { 158 if (info.uses_atomic_f32_add) {
156 code += "uint CasFloatAdd(uint op_a,float op_b){return " 159 header += "uint CasFloatAdd(uint op_a,float op_b){return "
157 "floatBitsToUint(uintBitsToFloat(op_a)+op_b);}\n"; 160 "floatBitsToUint(uintBitsToFloat(op_a)+op_b);}\n";
158 } 161 }
159 if (info.uses_atomic_f32x2_add) { 162 if (info.uses_atomic_f32x2_add) {
160 code += "uint CasFloatAdd32x2(uint op_a,vec2 op_b){return " 163 header += "uint CasFloatAdd32x2(uint op_a,vec2 op_b){return "
161 "packHalf2x16(unpackHalf2x16(op_a)+op_b);}\n"; 164 "packHalf2x16(unpackHalf2x16(op_a)+op_b);}\n";
162 } 165 }
163 if (info.uses_atomic_f32x2_min) { 166 if (info.uses_atomic_f32x2_min) {
164 code += "uint CasFloatMin32x2(uint op_a,vec2 op_b){return " 167 header += "uint CasFloatMin32x2(uint op_a,vec2 op_b){return "
165 "packHalf2x16(min(unpackHalf2x16(op_a),op_b));}\n"; 168 "packHalf2x16(min(unpackHalf2x16(op_a),op_b));}\n";
166 } 169 }
167 if (info.uses_atomic_f32x2_max) { 170 if (info.uses_atomic_f32x2_max) {
168 code += "uint CasFloatMax32x2(uint op_a,vec2 op_b){return " 171 header += "uint CasFloatMax32x2(uint op_a,vec2 op_b){return "
169 "packHalf2x16(max(unpackHalf2x16(op_a),op_b));}\n"; 172 "packHalf2x16(max(unpackHalf2x16(op_a),op_b));}\n";
170 } 173 }
171 if (info.uses_atomic_f16x2_add) { 174 if (info.uses_atomic_f16x2_add) {
172 code += "uint CasFloatAdd16x2(uint op_a,f16vec2 op_b){return " 175 header += "uint CasFloatAdd16x2(uint op_a,f16vec2 op_b){return "
173 "packFloat2x16(unpackFloat2x16(op_a)+op_b);}\n"; 176 "packFloat2x16(unpackFloat2x16(op_a)+op_b);}\n";
174 } 177 }
175 if (info.uses_atomic_f16x2_min) { 178 if (info.uses_atomic_f16x2_min) {
176 code += "uint CasFloatMin16x2(uint op_a,f16vec2 op_b){return " 179 header += "uint CasFloatMin16x2(uint op_a,f16vec2 op_b){return "
177 "packFloat2x16(min(unpackFloat2x16(op_a),op_b));}\n"; 180 "packFloat2x16(min(unpackFloat2x16(op_a),op_b));}\n";
178 } 181 }
179 if (info.uses_atomic_f16x2_max) { 182 if (info.uses_atomic_f16x2_max) {
180 code += "uint CasFloatMax16x2(uint op_a,f16vec2 op_b){return " 183 header += "uint CasFloatMax16x2(uint op_a,f16vec2 op_b){return "
181 "packFloat2x16(max(unpackFloat2x16(op_a),op_b));}\n"; 184 "packFloat2x16(max(unpackFloat2x16(op_a),op_b));}\n";
182 } 185 }
183 if (info.uses_atomic_s32_min) { 186 if (info.uses_atomic_s32_min) {
184 code += "uint CasMinS32(uint op_a,uint op_b){return uint(min(int(op_a),int(op_b)));}"; 187 header += "uint CasMinS32(uint op_a,uint op_b){return uint(min(int(op_a),int(op_b)));}";
185 } 188 }
186 if (info.uses_atomic_s32_max) { 189 if (info.uses_atomic_s32_max) {
187 code += "uint CasMaxS32(uint op_a,uint op_b){return uint(max(int(op_a),int(op_b)));}"; 190 header += "uint CasMaxS32(uint op_a,uint op_b){return uint(max(int(op_a),int(op_b)));}";
188 } 191 }
189} 192}
190 193
@@ -215,7 +218,8 @@ void EmitContext::SetupImages(Bindings& bindings) {
215 texture_bindings.push_back(bindings.texture); 218 texture_bindings.push_back(bindings.texture);
216 const auto indices{bindings.texture + desc.count}; 219 const auto indices{bindings.texture + desc.count};
217 for (u32 index = bindings.texture; index < indices; ++index) { 220 for (u32 index = bindings.texture; index < indices; ++index) {
218 Add("layout(binding={}) uniform {} tex{};", bindings.texture, sampler_type, index); 221 header += fmt::format("layout(binding={}) uniform {} tex{};", bindings.texture,
222 sampler_type, index);
219 } 223 }
220 bindings.texture += desc.count; 224 bindings.texture += desc.count;
221 } 225 }