shader.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <atomic>
  5. #include <cmath>
  6. #include <cstring>
  7. #include <unordered_map>
  8. #include <utility>
  9. #include <boost/range/algorithm/fill.hpp>
  10. #include "common/bit_field.h"
  11. #include "common/hash.h"
  12. #include "common/logging/log.h"
  13. #include "common/microprofile.h"
  14. #include "video_core/pica.h"
  15. #include "video_core/pica_state.h"
  16. #include "video_core/shader/shader.h"
  17. #include "video_core/shader/shader_interpreter.h"
  18. #ifdef ARCHITECTURE_x86_64
  19. #include "video_core/shader/shader_jit_x64.h"
  20. #endif // ARCHITECTURE_x86_64
  21. #include "video_core/video_core.h"
  22. namespace Pica {
  23. namespace Shader {
  24. #ifdef ARCHITECTURE_x86_64
  25. static std::unordered_map<u64, std::unique_ptr<JitShader>> shader_map;
  26. static const JitShader* jit_shader;
  27. #endif // ARCHITECTURE_x86_64
  28. void ClearCache() {
  29. #ifdef ARCHITECTURE_x86_64
  30. shader_map.clear();
  31. #endif // ARCHITECTURE_x86_64
  32. }
  33. void ShaderSetup::Setup() {
  34. #ifdef ARCHITECTURE_x86_64
  35. if (VideoCore::g_shader_jit_enabled) {
  36. u64 cache_key = (Common::ComputeHash64(&g_state.vs.program_code, sizeof(g_state.vs.program_code)) ^
  37. Common::ComputeHash64(&g_state.vs.swizzle_data, sizeof(g_state.vs.swizzle_data)));
  38. auto iter = shader_map.find(cache_key);
  39. if (iter != shader_map.end()) {
  40. jit_shader = iter->second.get();
  41. } else {
  42. auto shader = std::make_unique<JitShader>();
  43. shader->Compile();
  44. jit_shader = shader.get();
  45. shader_map[cache_key] = std::move(shader);
  46. }
  47. }
  48. #endif // ARCHITECTURE_x86_64
  49. }
  50. MICROPROFILE_DEFINE(GPU_Shader, "GPU", "Shader", MP_RGB(50, 50, 240));
  51. OutputVertex ShaderSetup::Run(UnitState<false>& state, const InputVertex& input, int num_attributes) {
  52. auto& config = g_state.regs.vs;
  53. MICROPROFILE_SCOPE(GPU_Shader);
  54. state.program_counter = config.main_offset;
  55. state.debug.max_offset = 0;
  56. state.debug.max_opdesc_id = 0;
  57. // Setup input register table
  58. const auto& attribute_register_map = config.input_register_map;
  59. for (unsigned i = 0; i < num_attributes; i++)
  60. state.registers.input[attribute_register_map.GetRegisterForAttribute(i)] = input.attr[i];
  61. state.conditional_code[0] = false;
  62. state.conditional_code[1] = false;
  63. #ifdef ARCHITECTURE_x86_64
  64. if (VideoCore::g_shader_jit_enabled)
  65. jit_shader->Run(&state.registers, g_state.regs.vs.main_offset);
  66. else
  67. RunInterpreter(state);
  68. #else
  69. RunInterpreter(state);
  70. #endif // ARCHITECTURE_x86_64
  71. // Setup output data
  72. OutputVertex ret;
  73. // TODO(neobrain): Under some circumstances, up to 16 attributes may be output. We need to
  74. // figure out what those circumstances are and enable the remaining outputs then.
  75. unsigned index = 0;
  76. for (unsigned i = 0; i < 7; ++i) {
  77. if (index >= g_state.regs.vs_output_total)
  78. break;
  79. if ((g_state.regs.vs.output_mask & (1 << i)) == 0)
  80. continue;
  81. const auto& output_register_map = g_state.regs.vs_output_attributes[index]; // TODO: Don't hardcode VS here
  82. u32 semantics[4] = {
  83. output_register_map.map_x, output_register_map.map_y,
  84. output_register_map.map_z, output_register_map.map_w
  85. };
  86. for (unsigned comp = 0; comp < 4; ++comp) {
  87. float24* out = ((float24*)&ret) + semantics[comp];
  88. if (semantics[comp] != Regs::VSOutputAttributes::INVALID) {
  89. *out = state.registers.output[i][comp];
  90. } else {
  91. // Zero output so that attributes which aren't output won't have denormals in them,
  92. // which would slow us down later.
  93. memset(out, 0, sizeof(*out));
  94. }
  95. }
  96. index++;
  97. }
  98. // The hardware takes the absolute and saturates vertex colors like this, *before* doing interpolation
  99. for (unsigned i = 0; i < 4; ++i) {
  100. ret.color[i] = float24::FromFloat32(
  101. std::fmin(std::fabs(ret.color[i].ToFloat32()), 1.0f));
  102. }
  103. LOG_TRACE(HW_GPU, "Output vertex: pos(%.2f, %.2f, %.2f, %.2f), quat(%.2f, %.2f, %.2f, %.2f), "
  104. "col(%.2f, %.2f, %.2f, %.2f), tc0(%.2f, %.2f), view(%.2f, %.2f, %.2f)",
  105. ret.pos.x.ToFloat32(), ret.pos.y.ToFloat32(), ret.pos.z.ToFloat32(), ret.pos.w.ToFloat32(),
  106. ret.quat.x.ToFloat32(), ret.quat.y.ToFloat32(), ret.quat.z.ToFloat32(), ret.quat.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. ret.view.x.ToFloat32(), ret.view.y.ToFloat32(), ret.view.z.ToFloat32());
  110. return ret;
  111. }
  112. DebugData<true> ShaderSetup::ProduceDebugInfo(const InputVertex& input, int num_attributes, const Regs::ShaderConfig& config, const ShaderSetup& setup) {
  113. UnitState<true> state;
  114. state.program_counter = config.main_offset;
  115. state.debug.max_offset = 0;
  116. state.debug.max_opdesc_id = 0;
  117. // Setup input register table
  118. const auto& attribute_register_map = config.input_register_map;
  119. float24 dummy_register;
  120. boost::fill(state.registers.input, &dummy_register);
  121. for (unsigned i = 0; i < num_attributes; i++)
  122. state.registers.input[attribute_register_map.GetRegisterForAttribute(i)] = input.attr[i];
  123. state.conditional_code[0] = false;
  124. state.conditional_code[1] = false;
  125. RunInterpreter(state);
  126. return state.debug;
  127. }
  128. } // namespace Shader
  129. } // namespace Pica