shader.cpp 9.0 KB

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