shader.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <memory>
  5. #include <unordered_map>
  6. #include "common/hash.h"
  7. #include "common/make_unique.h"
  8. #include "common/profiler.h"
  9. #include "video_core/debug_utils/debug_utils.h"
  10. #include "video_core/pica.h"
  11. #include "video_core/video_core.h"
  12. #include "shader.h"
  13. #include "shader_interpreter.h"
  14. #ifdef ARCHITECTURE_x86_64
  15. #include "shader_jit_x64.h"
  16. #endif // ARCHITECTURE_x86_64
  17. namespace Pica {
  18. namespace Shader {
  19. #ifdef ARCHITECTURE_x86_64
  20. static std::unordered_map<u64, CompiledShader*> shader_map;
  21. static JitCompiler jit;
  22. static CompiledShader* jit_shader;
  23. #endif // ARCHITECTURE_x86_64
  24. void Setup(UnitState& state) {
  25. #ifdef ARCHITECTURE_x86_64
  26. if (VideoCore::g_shader_jit_enabled) {
  27. u64 cache_key = (Common::ComputeHash64(&g_state.vs.program_code, sizeof(g_state.vs.program_code)) ^
  28. Common::ComputeHash64(&g_state.vs.swizzle_data, sizeof(g_state.vs.swizzle_data)) ^
  29. g_state.regs.vs.main_offset);
  30. auto iter = shader_map.find(cache_key);
  31. if (iter != shader_map.end()) {
  32. jit_shader = iter->second;
  33. } else {
  34. jit_shader = jit.Compile();
  35. shader_map.emplace(cache_key, jit_shader);
  36. }
  37. }
  38. #endif // ARCHITECTURE_x86_64
  39. }
  40. void Shutdown() {
  41. shader_map.clear();
  42. }
  43. static Common::Profiling::TimingCategory shader_category("Vertex Shader");
  44. OutputVertex Run(UnitState& state, const InputVertex& input, int num_attributes) {
  45. auto& config = g_state.regs.vs;
  46. auto& setup = g_state.vs;
  47. Common::Profiling::ScopeTimer timer(shader_category);
  48. state.program_counter = config.main_offset;
  49. state.debug.max_offset = 0;
  50. state.debug.max_opdesc_id = 0;
  51. // Setup input register table
  52. const auto& attribute_register_map = config.input_register_map;
  53. if (num_attributes > 0) state.registers.input[attribute_register_map.attribute0_register] = input.attr[0];
  54. if (num_attributes > 1) state.registers.input[attribute_register_map.attribute1_register] = input.attr[1];
  55. if (num_attributes > 2) state.registers.input[attribute_register_map.attribute2_register] = input.attr[2];
  56. if (num_attributes > 3) state.registers.input[attribute_register_map.attribute3_register] = input.attr[3];
  57. if (num_attributes > 4) state.registers.input[attribute_register_map.attribute4_register] = input.attr[4];
  58. if (num_attributes > 5) state.registers.input[attribute_register_map.attribute5_register] = input.attr[5];
  59. if (num_attributes > 6) state.registers.input[attribute_register_map.attribute6_register] = input.attr[6];
  60. if (num_attributes > 7) state.registers.input[attribute_register_map.attribute7_register] = input.attr[7];
  61. if (num_attributes > 8) state.registers.input[attribute_register_map.attribute8_register] = input.attr[8];
  62. if (num_attributes > 9) state.registers.input[attribute_register_map.attribute9_register] = input.attr[9];
  63. if (num_attributes > 10) state.registers.input[attribute_register_map.attribute10_register] = input.attr[10];
  64. if (num_attributes > 11) state.registers.input[attribute_register_map.attribute11_register] = input.attr[11];
  65. if (num_attributes > 12) state.registers.input[attribute_register_map.attribute12_register] = input.attr[12];
  66. if (num_attributes > 13) state.registers.input[attribute_register_map.attribute13_register] = input.attr[13];
  67. if (num_attributes > 14) state.registers.input[attribute_register_map.attribute14_register] = input.attr[14];
  68. if (num_attributes > 15) state.registers.input[attribute_register_map.attribute15_register] = input.attr[15];
  69. state.conditional_code[0] = false;
  70. state.conditional_code[1] = false;
  71. #ifdef ARCHITECTURE_x86_64
  72. if (VideoCore::g_shader_jit_enabled)
  73. jit_shader(&state.registers);
  74. else
  75. RunInterpreter(state);
  76. #else
  77. RunInterpreter(state);
  78. #endif // ARCHITECTURE_x86_64
  79. // Setup output data
  80. OutputVertex ret;
  81. // TODO(neobrain): Under some circumstances, up to 16 attributes may be output. We need to
  82. // figure out what those circumstances are and enable the remaining outputs then.
  83. for (int i = 0; i < 7; ++i) {
  84. const auto& output_register_map = g_state.regs.vs_output_attributes[i]; // TODO: Don't hardcode VS here
  85. u32 semantics[4] = {
  86. output_register_map.map_x, output_register_map.map_y,
  87. output_register_map.map_z, output_register_map.map_w
  88. };
  89. for (int comp = 0; comp < 4; ++comp) {
  90. float24* out = ((float24*)&ret) + semantics[comp];
  91. if (semantics[comp] != Regs::VSOutputAttributes::INVALID) {
  92. *out = state.registers.output[i][comp];
  93. } else {
  94. // Zero output so that attributes which aren't output won't have denormals in them,
  95. // which would slow us down later.
  96. memset(out, 0, sizeof(*out));
  97. }
  98. }
  99. }
  100. // The hardware takes the absolute and saturates vertex colors like this, *before* doing interpolation
  101. for (int i = 0; i < 4; ++i) {
  102. ret.color[i] = float24::FromFloat32(
  103. std::fmin(std::fabs(ret.color[i].ToFloat32()), 1.0f));
  104. }
  105. LOG_TRACE(Render_Software, "Output vertex: pos (%.2f, %.2f, %.2f, %.2f), col(%.2f, %.2f, %.2f, %.2f), tc0(%.2f, %.2f)",
  106. ret.pos.x.ToFloat32(), ret.pos.y.ToFloat32(), ret.pos.z.ToFloat32(), ret.pos.w.ToFloat32(),
  107. ret.color.x.ToFloat32(), ret.color.y.ToFloat32(), ret.color.z.ToFloat32(), ret.color.w.ToFloat32(),
  108. ret.tc0.u().ToFloat32(), ret.tc0.v().ToFloat32());
  109. return ret;
  110. }
  111. } // namespace Shader
  112. } // namespace Pica