emit_context.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "shader_recompiler/backend/bindings.h"
  5. #include "shader_recompiler/backend/glsl/emit_context.h"
  6. #include "shader_recompiler/frontend/ir/program.h"
  7. namespace Shader::Backend::GLSL {
  8. EmitContext::EmitContext(IR::Program& program, [[maybe_unused]] Bindings& bindings,
  9. const Profile& profile_)
  10. : info{program.info}, profile{profile_} {
  11. std::string header = "#version 450\n";
  12. SetupExtensions(header);
  13. if (program.stage == Stage::Compute) {
  14. header += fmt::format("layout(local_size_x={},local_size_y={},local_size_z={}) in;\n",
  15. program.workgroup_size[0], program.workgroup_size[1],
  16. program.workgroup_size[2]);
  17. }
  18. code += header;
  19. DefineConstantBuffers();
  20. DefineStorageBuffers();
  21. code += "void main(){\n";
  22. }
  23. void EmitContext::SetupExtensions(std::string& header) {
  24. if (info.uses_int64) {
  25. header += "#extension GL_ARB_gpu_shader_int64 : enable\n";
  26. }
  27. }
  28. void EmitContext::DefineConstantBuffers() {
  29. if (info.constant_buffer_descriptors.empty()) {
  30. return;
  31. }
  32. u32 binding{};
  33. for (const auto& desc : info.constant_buffer_descriptors) {
  34. Add("layout(std140,binding={}) uniform cbuf_{}{{vec4 cbuf{}[{}];}};", binding, binding,
  35. desc.index, 4 * 1024);
  36. ++binding;
  37. }
  38. }
  39. void EmitContext::DefineStorageBuffers() {
  40. if (info.storage_buffers_descriptors.empty()) {
  41. return;
  42. }
  43. u32 binding{};
  44. for (const auto& desc : info.storage_buffers_descriptors) {
  45. Add("layout(std430,binding={}) buffer ssbo_{}_u32{{uint ssbo{}_u32[];}};", binding, binding,
  46. desc.cbuf_index, desc.count);
  47. // TODO: Track ssbo data type usage
  48. Add("layout(std430,binding={}) buffer ssbo_{}_u64{{uvec2 ssbo{}_u64[];}};", binding,
  49. binding, desc.cbuf_index, desc.count);
  50. ++binding;
  51. }
  52. }
  53. } // namespace Shader::Backend::GLSL