shader.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <boost/range/algorithm/fill.hpp>
  7. #include "common/hash.h"
  8. #include "common/make_unique.h"
  9. #include "common/microprofile.h"
  10. #include "common/profiler.h"
  11. #include "video_core/debug_utils/debug_utils.h"
  12. #include "video_core/pica.h"
  13. #include "video_core/pica_state.h"
  14. #include "video_core/video_core.h"
  15. #include "shader.h"
  16. #include "shader_interpreter.h"
  17. #ifdef ARCHITECTURE_x86_64
  18. #include "shader_jit_x64.h"
  19. #endif // ARCHITECTURE_x86_64
  20. namespace Pica {
  21. namespace Shader {
  22. #ifdef ARCHITECTURE_x86_64
  23. static std::unordered_map<u64, CompiledShader*> shader_map;
  24. static JitCompiler jit;
  25. static CompiledShader* jit_shader;
  26. #endif // ARCHITECTURE_x86_64
  27. void Setup(UnitState<false>& state) {
  28. #ifdef ARCHITECTURE_x86_64
  29. if (VideoCore::g_shader_jit_enabled) {
  30. u64 cache_key = (Common::ComputeHash64(&g_state.vs.program_code, sizeof(g_state.vs.program_code)) ^
  31. Common::ComputeHash64(&g_state.vs.swizzle_data, sizeof(g_state.vs.swizzle_data)) ^
  32. g_state.regs.vs.main_offset);
  33. auto iter = shader_map.find(cache_key);
  34. if (iter != shader_map.end()) {
  35. jit_shader = iter->second;
  36. } else {
  37. jit_shader = jit.Compile();
  38. shader_map.emplace(cache_key, jit_shader);
  39. }
  40. }
  41. #endif // ARCHITECTURE_x86_64
  42. }
  43. void Shutdown() {
  44. #ifdef ARCHITECTURE_x86_64
  45. shader_map.clear();
  46. #endif // ARCHITECTURE_x86_64
  47. }
  48. static Common::Profiling::TimingCategory shader_category("Vertex Shader");
  49. MICROPROFILE_DEFINE(GPU_VertexShader, "GPU", "Vertex Shader", MP_RGB(50, 50, 240));
  50. OutputVertex Run(UnitState<false>& state, const InputVertex& input, int num_attributes) {
  51. auto& config = g_state.regs.vs;
  52. Common::Profiling::ScopeTimer timer(shader_category);
  53. MICROPROFILE_SCOPE(GPU_VertexShader);
  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. // TODO: Instead of this cumbersome logic, just load the input data directly like
  60. // for (int attr = 0; attr < num_attributes; ++attr) { input_attr[0] = state.registers.input[attribute_register_map.attribute0_register]; }
  61. if (num_attributes > 0) state.registers.input[attribute_register_map.attribute0_register] = input.attr[0];
  62. if (num_attributes > 1) state.registers.input[attribute_register_map.attribute1_register] = input.attr[1];
  63. if (num_attributes > 2) state.registers.input[attribute_register_map.attribute2_register] = input.attr[2];
  64. if (num_attributes > 3) state.registers.input[attribute_register_map.attribute3_register] = input.attr[3];
  65. if (num_attributes > 4) state.registers.input[attribute_register_map.attribute4_register] = input.attr[4];
  66. if (num_attributes > 5) state.registers.input[attribute_register_map.attribute5_register] = input.attr[5];
  67. if (num_attributes > 6) state.registers.input[attribute_register_map.attribute6_register] = input.attr[6];
  68. if (num_attributes > 7) state.registers.input[attribute_register_map.attribute7_register] = input.attr[7];
  69. if (num_attributes > 8) state.registers.input[attribute_register_map.attribute8_register] = input.attr[8];
  70. if (num_attributes > 9) state.registers.input[attribute_register_map.attribute9_register] = input.attr[9];
  71. if (num_attributes > 10) state.registers.input[attribute_register_map.attribute10_register] = input.attr[10];
  72. if (num_attributes > 11) state.registers.input[attribute_register_map.attribute11_register] = input.attr[11];
  73. if (num_attributes > 12) state.registers.input[attribute_register_map.attribute12_register] = input.attr[12];
  74. if (num_attributes > 13) state.registers.input[attribute_register_map.attribute13_register] = input.attr[13];
  75. if (num_attributes > 14) state.registers.input[attribute_register_map.attribute14_register] = input.attr[14];
  76. if (num_attributes > 15) state.registers.input[attribute_register_map.attribute15_register] = input.attr[15];
  77. state.conditional_code[0] = false;
  78. state.conditional_code[1] = false;
  79. #ifdef ARCHITECTURE_x86_64
  80. if (VideoCore::g_shader_jit_enabled)
  81. jit_shader(&state.registers);
  82. else
  83. RunInterpreter(state);
  84. #else
  85. RunInterpreter(state);
  86. #endif // ARCHITECTURE_x86_64
  87. // Setup output data
  88. OutputVertex ret;
  89. // TODO(neobrain): Under some circumstances, up to 16 attributes may be output. We need to
  90. // figure out what those circumstances are and enable the remaining outputs then.
  91. unsigned index = 0;
  92. for (unsigned i = 0; i < 7; ++i) {
  93. if (index >= g_state.regs.vs_output_total)
  94. break;
  95. if ((g_state.regs.vs.output_mask & (1 << i)) == 0)
  96. continue;
  97. const auto& output_register_map = g_state.regs.vs_output_attributes[index]; // TODO: Don't hardcode VS here
  98. u32 semantics[4] = {
  99. output_register_map.map_x, output_register_map.map_y,
  100. output_register_map.map_z, output_register_map.map_w
  101. };
  102. for (unsigned comp = 0; comp < 4; ++comp) {
  103. float24* out = ((float24*)&ret) + semantics[comp];
  104. if (semantics[comp] != Regs::VSOutputAttributes::INVALID) {
  105. *out = state.registers.output[i][comp];
  106. } else {
  107. // Zero output so that attributes which aren't output won't have denormals in them,
  108. // which would slow us down later.
  109. memset(out, 0, sizeof(*out));
  110. }
  111. }
  112. index++;
  113. }
  114. // The hardware takes the absolute and saturates vertex colors like this, *before* doing interpolation
  115. for (unsigned i = 0; i < 4; ++i) {
  116. ret.color[i] = float24::FromFloat32(
  117. std::fmin(std::fabs(ret.color[i].ToFloat32()), 1.0f));
  118. }
  119. LOG_TRACE(Render_Software, "Output vertex: pos(%.2f, %.2f, %.2f, %.2f), quat(%.2f, %.2f, %.2f, %.2f), "
  120. "col(%.2f, %.2f, %.2f, %.2f), tc0(%.2f, %.2f), view(%.2f, %.2f, %.2f)",
  121. ret.pos.x.ToFloat32(), ret.pos.y.ToFloat32(), ret.pos.z.ToFloat32(), ret.pos.w.ToFloat32(),
  122. ret.quat.x.ToFloat32(), ret.quat.y.ToFloat32(), ret.quat.z.ToFloat32(), ret.quat.w.ToFloat32(),
  123. ret.color.x.ToFloat32(), ret.color.y.ToFloat32(), ret.color.z.ToFloat32(), ret.color.w.ToFloat32(),
  124. ret.tc0.u().ToFloat32(), ret.tc0.v().ToFloat32(),
  125. ret.view.x.ToFloat32(), ret.view.y.ToFloat32(), ret.view.z.ToFloat32());
  126. return ret;
  127. }
  128. DebugData<true> ProduceDebugInfo(const InputVertex& input, int num_attributes, const Regs::ShaderConfig& config, const ShaderSetup& setup) {
  129. UnitState<true> state;
  130. state.program_counter = config.main_offset;
  131. state.debug.max_offset = 0;
  132. state.debug.max_opdesc_id = 0;
  133. // Setup input register table
  134. const auto& attribute_register_map = config.input_register_map;
  135. float24 dummy_register;
  136. boost::fill(state.registers.input, &dummy_register);
  137. if (num_attributes > 0) state.registers.input[attribute_register_map.attribute0_register] = &input.attr[0].x;
  138. if (num_attributes > 1) state.registers.input[attribute_register_map.attribute1_register] = &input.attr[1].x;
  139. if (num_attributes > 2) state.registers.input[attribute_register_map.attribute2_register] = &input.attr[2].x;
  140. if (num_attributes > 3) state.registers.input[attribute_register_map.attribute3_register] = &input.attr[3].x;
  141. if (num_attributes > 4) state.registers.input[attribute_register_map.attribute4_register] = &input.attr[4].x;
  142. if (num_attributes > 5) state.registers.input[attribute_register_map.attribute5_register] = &input.attr[5].x;
  143. if (num_attributes > 6) state.registers.input[attribute_register_map.attribute6_register] = &input.attr[6].x;
  144. if (num_attributes > 7) state.registers.input[attribute_register_map.attribute7_register] = &input.attr[7].x;
  145. if (num_attributes > 8) state.registers.input[attribute_register_map.attribute8_register] = &input.attr[8].x;
  146. if (num_attributes > 9) state.registers.input[attribute_register_map.attribute9_register] = &input.attr[9].x;
  147. if (num_attributes > 10) state.registers.input[attribute_register_map.attribute10_register] = &input.attr[10].x;
  148. if (num_attributes > 11) state.registers.input[attribute_register_map.attribute11_register] = &input.attr[11].x;
  149. if (num_attributes > 12) state.registers.input[attribute_register_map.attribute12_register] = &input.attr[12].x;
  150. if (num_attributes > 13) state.registers.input[attribute_register_map.attribute13_register] = &input.attr[13].x;
  151. if (num_attributes > 14) state.registers.input[attribute_register_map.attribute14_register] = &input.attr[14].x;
  152. if (num_attributes > 15) state.registers.input[attribute_register_map.attribute15_register] = &input.attr[15].x;
  153. state.conditional_code[0] = false;
  154. state.conditional_code[1] = false;
  155. RunInterpreter(state);
  156. return state.debug;
  157. }
  158. } // namespace Shader
  159. } // namespace Pica