shader.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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) {
  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] = {
  37. output_register_map.map_x, output_register_map.map_y,
  38. output_register_map.map_z, output_register_map.map_w
  39. };
  40. for (unsigned comp = 0; comp < 4; ++comp) {
  41. float24* out = ((float24*)&ret) + semantics[comp];
  42. if (semantics[comp] != Regs::VSOutputAttributes::INVALID) {
  43. *out = value[i][comp];
  44. } else {
  45. // Zero output so that attributes which aren't output won't have denormals in them,
  46. // which would slow us down later.
  47. memset(out, 0, sizeof(*out));
  48. }
  49. }
  50. index++;
  51. }
  52. // The hardware takes the absolute and saturates vertex colors like this, *before* doing interpolation
  53. for (unsigned i = 0; i < 4; ++i) {
  54. ret.color[i] = float24::FromFloat32(
  55. std::fmin(std::fabs(ret.color[i].ToFloat32()), 1.0f));
  56. }
  57. LOG_TRACE(HW_GPU, "Output vertex: pos(%.2f, %.2f, %.2f, %.2f), quat(%.2f, %.2f, %.2f, %.2f), "
  58. "col(%.2f, %.2f, %.2f, %.2f), tc0(%.2f, %.2f), view(%.2f, %.2f, %.2f)",
  59. ret.pos.x.ToFloat32(), ret.pos.y.ToFloat32(), ret.pos.z.ToFloat32(), ret.pos.w.ToFloat32(),
  60. ret.quat.x.ToFloat32(), ret.quat.y.ToFloat32(), ret.quat.z.ToFloat32(), ret.quat.w.ToFloat32(),
  61. ret.color.x.ToFloat32(), ret.color.y.ToFloat32(), ret.color.z.ToFloat32(), ret.color.w.ToFloat32(),
  62. ret.tc0.u().ToFloat32(), ret.tc0.v().ToFloat32(),
  63. ret.view.x.ToFloat32(), ret.view.y.ToFloat32(), ret.view.z.ToFloat32());
  64. return ret;
  65. }
  66. #ifdef ARCHITECTURE_x86_64
  67. static std::unordered_map<u64, std::unique_ptr<JitShader>> shader_map;
  68. static const JitShader* jit_shader;
  69. #endif // ARCHITECTURE_x86_64
  70. void ClearCache() {
  71. #ifdef ARCHITECTURE_x86_64
  72. shader_map.clear();
  73. #endif // ARCHITECTURE_x86_64
  74. }
  75. void ShaderSetup::Setup() {
  76. #ifdef ARCHITECTURE_x86_64
  77. if (VideoCore::g_shader_jit_enabled) {
  78. u64 cache_key = (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<false>& 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. state.debug.max_offset = 0;
  98. state.debug.max_opdesc_id = 0;
  99. // Setup input register table
  100. const auto& attribute_register_map = config.input_register_map;
  101. for (unsigned i = 0; i < num_attributes; i++)
  102. state.registers.input[attribute_register_map.GetRegisterForAttribute(i)] = input.attr[i];
  103. state.conditional_code[0] = false;
  104. state.conditional_code[1] = false;
  105. #ifdef ARCHITECTURE_x86_64
  106. if (VideoCore::g_shader_jit_enabled)
  107. jit_shader->Run(setup, state, config.main_offset);
  108. else
  109. RunInterpreter(setup, state, config.main_offset);
  110. #else
  111. RunInterpreter(setup, state, config.main_offset);
  112. #endif // ARCHITECTURE_x86_64
  113. }
  114. DebugData<true> ShaderSetup::ProduceDebugInfo(const InputVertex& input, int num_attributes, const Regs::ShaderConfig& config, const ShaderSetup& setup) {
  115. UnitState<true> state;
  116. state.debug.max_offset = 0;
  117. state.debug.max_opdesc_id = 0;
  118. // Setup input register table
  119. const auto& attribute_register_map = config.input_register_map;
  120. float24 dummy_register;
  121. boost::fill(state.registers.input, &dummy_register);
  122. for (unsigned i = 0; i < num_attributes; i++)
  123. state.registers.input[attribute_register_map.GetRegisterForAttribute(i)] = input.attr[i];
  124. state.conditional_code[0] = false;
  125. state.conditional_code[1] = false;
  126. RunInterpreter(setup, state, config.main_offset);
  127. return state.debug;
  128. }
  129. } // namespace Shader
  130. } // namespace Pica