shader.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. OutputVertex OutputRegisters::ToVertex(const Regs::ShaderConfig& config) const {
  25. // Setup output data
  26. OutputVertex ret;
  27. // TODO(neobrain): Under some circumstances, up to 16 attributes may be output. We need to
  28. // figure out what those circumstances are and enable the remaining outputs then.
  29. unsigned index = 0;
  30. for (unsigned i = 0; i < 7; ++i) {
  31. if (index >= g_state.regs.vs_output_total)
  32. break;
  33. if ((config.output_mask & (1 << i)) == 0)
  34. continue;
  35. const auto& output_register_map = g_state.regs.vs_output_attributes[index];
  36. u32 semantics[4] = {output_register_map.map_x, output_register_map.map_y,
  37. output_register_map.map_z, output_register_map.map_w};
  38. for (unsigned comp = 0; comp < 4; ++comp) {
  39. float24* out = ((float24*)&ret) + semantics[comp];
  40. if (semantics[comp] != Regs::VSOutputAttributes::INVALID) {
  41. *out = value[i][comp];
  42. } else {
  43. // Zero output so that attributes which aren't output won't have denormals in them,
  44. // which would slow us down later.
  45. memset(out, 0, sizeof(*out));
  46. }
  47. }
  48. index++;
  49. }
  50. // The hardware takes the absolute and saturates vertex colors like this, *before* doing
  51. // interpolation
  52. for (unsigned i = 0; i < 4; ++i) {
  53. ret.color[i] = float24::FromFloat32(std::fmin(std::fabs(ret.color[i].ToFloat32()), 1.0f));
  54. }
  55. LOG_TRACE(HW_GPU, "Output vertex: pos(%.2f, %.2f, %.2f, %.2f), quat(%.2f, %.2f, %.2f, %.2f), "
  56. "col(%.2f, %.2f, %.2f, %.2f), tc0(%.2f, %.2f), view(%.2f, %.2f, %.2f)",
  57. ret.pos.x.ToFloat32(), ret.pos.y.ToFloat32(), ret.pos.z.ToFloat32(),
  58. ret.pos.w.ToFloat32(), ret.quat.x.ToFloat32(), ret.quat.y.ToFloat32(),
  59. ret.quat.z.ToFloat32(), ret.quat.w.ToFloat32(), ret.color.x.ToFloat32(),
  60. ret.color.y.ToFloat32(), ret.color.z.ToFloat32(), ret.color.w.ToFloat32(),
  61. ret.tc0.u().ToFloat32(), ret.tc0.v().ToFloat32(), ret.view.x.ToFloat32(),
  62. ret.view.y.ToFloat32(), ret.view.z.ToFloat32());
  63. return ret;
  64. }
  65. #ifdef ARCHITECTURE_x86_64
  66. static std::unordered_map<u64, std::unique_ptr<JitShader>> shader_map;
  67. static const JitShader* jit_shader;
  68. #endif // ARCHITECTURE_x86_64
  69. void ClearCache() {
  70. #ifdef ARCHITECTURE_x86_64
  71. shader_map.clear();
  72. #endif // ARCHITECTURE_x86_64
  73. }
  74. void ShaderSetup::Setup() {
  75. #ifdef ARCHITECTURE_x86_64
  76. if (VideoCore::g_shader_jit_enabled) {
  77. u64 cache_key =
  78. Common::ComputeHash64(&g_state.vs.program_code, sizeof(g_state.vs.program_code)) ^
  79. Common::ComputeHash64(&g_state.vs.swizzle_data, sizeof(g_state.vs.swizzle_data));
  80. auto iter = shader_map.find(cache_key);
  81. if (iter != shader_map.end()) {
  82. jit_shader = iter->second.get();
  83. } else {
  84. auto shader = std::make_unique<JitShader>();
  85. shader->Compile();
  86. jit_shader = shader.get();
  87. shader_map[cache_key] = std::move(shader);
  88. }
  89. }
  90. #endif // ARCHITECTURE_x86_64
  91. }
  92. MICROPROFILE_DEFINE(GPU_Shader, "GPU", "Shader", MP_RGB(50, 50, 240));
  93. void ShaderSetup::Run(UnitState& state, const InputVertex& input, int num_attributes) {
  94. auto& config = g_state.regs.vs;
  95. auto& setup = g_state.vs;
  96. MICROPROFILE_SCOPE(GPU_Shader);
  97. // Setup input register table
  98. const auto& attribute_register_map = config.input_register_map;
  99. for (int i = 0; i < num_attributes; i++)
  100. state.registers.input[attribute_register_map.GetRegisterForAttribute(i)] = input.attr[i];
  101. state.conditional_code[0] = false;
  102. state.conditional_code[1] = false;
  103. #ifdef ARCHITECTURE_x86_64
  104. if (VideoCore::g_shader_jit_enabled) {
  105. jit_shader->Run(setup, state, config.main_offset);
  106. } else {
  107. DebugData<false> dummy_debug_data;
  108. RunInterpreter(setup, state, dummy_debug_data, config.main_offset);
  109. }
  110. #else
  111. DebugData<false> dummy_debug_data;
  112. RunInterpreter(setup, state, dummy_debug_data, config.main_offset);
  113. #endif // ARCHITECTURE_x86_64
  114. }
  115. DebugData<true> ShaderSetup::ProduceDebugInfo(const InputVertex& input, int num_attributes,
  116. const Regs::ShaderConfig& config,
  117. const ShaderSetup& setup) {
  118. UnitState state;
  119. DebugData<true> debug_data;
  120. // Setup input register table
  121. boost::fill(state.registers.input, Math::Vec4<float24>::AssignToAll(float24::Zero()));
  122. const auto& attribute_register_map = config.input_register_map;
  123. for (int i = 0; i < num_attributes; i++)
  124. state.registers.input[attribute_register_map.GetRegisterForAttribute(i)] = input.attr[i];
  125. state.conditional_code[0] = false;
  126. state.conditional_code[1] = false;
  127. RunInterpreter(setup, state, debug_data, config.main_offset);
  128. return debug_data;
  129. }
  130. } // namespace Shader
  131. } // namespace Pica