shader.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cmath>
  5. #include <cstring>
  6. #include "common/bit_set.h"
  7. #include "common/logging/log.h"
  8. #include "common/microprofile.h"
  9. #include "video_core/pica.h"
  10. #include "video_core/pica_state.h"
  11. #include "video_core/shader/shader.h"
  12. #include "video_core/shader/shader_interpreter.h"
  13. #ifdef ARCHITECTURE_x86_64
  14. #include "video_core/shader/shader_jit_x64.h"
  15. #endif // ARCHITECTURE_x86_64
  16. #include "video_core/video_core.h"
  17. namespace Pica {
  18. namespace Shader {
  19. OutputVertex OutputVertex::FromAttributeBuffer(const RasterizerRegs& regs, AttributeBuffer& input) {
  20. // Setup output data
  21. union {
  22. OutputVertex ret{};
  23. std::array<float24, 24> vertex_slots;
  24. };
  25. static_assert(sizeof(vertex_slots) == sizeof(ret), "Struct and array have different sizes.");
  26. unsigned int num_attributes = regs.vs_output_total;
  27. ASSERT(num_attributes <= 7);
  28. for (unsigned int i = 0; i < num_attributes; ++i) {
  29. const auto& output_register_map = regs.vs_output_attributes[i];
  30. RasterizerRegs::VSOutputAttributes::Semantic semantics[4] = {
  31. output_register_map.map_x, output_register_map.map_y, output_register_map.map_z,
  32. output_register_map.map_w};
  33. for (unsigned comp = 0; comp < 4; ++comp) {
  34. RasterizerRegs::VSOutputAttributes::Semantic semantic = semantics[comp];
  35. float24* out = &vertex_slots[semantic];
  36. if (semantic < vertex_slots.size()) {
  37. *out = input.attr[i][comp];
  38. } else if (semantic != RasterizerRegs::VSOutputAttributes::INVALID) {
  39. LOG_ERROR(HW_GPU, "Invalid/unknown semantic id: %u", (unsigned int)semantic);
  40. }
  41. }
  42. }
  43. // The hardware takes the absolute and saturates vertex colors like this, *before* doing
  44. // interpolation
  45. for (unsigned i = 0; i < 4; ++i) {
  46. ret.color[i] = float24::FromFloat32(std::fmin(std::fabs(ret.color[i].ToFloat32()), 1.0f));
  47. }
  48. LOG_TRACE(HW_GPU, "Output vertex: pos(%.2f, %.2f, %.2f, %.2f), quat(%.2f, %.2f, %.2f, %.2f), "
  49. "col(%.2f, %.2f, %.2f, %.2f), tc0(%.2f, %.2f), view(%.2f, %.2f, %.2f)",
  50. ret.pos.x.ToFloat32(), ret.pos.y.ToFloat32(), ret.pos.z.ToFloat32(),
  51. ret.pos.w.ToFloat32(), ret.quat.x.ToFloat32(), ret.quat.y.ToFloat32(),
  52. ret.quat.z.ToFloat32(), ret.quat.w.ToFloat32(), ret.color.x.ToFloat32(),
  53. ret.color.y.ToFloat32(), ret.color.z.ToFloat32(), ret.color.w.ToFloat32(),
  54. ret.tc0.u().ToFloat32(), ret.tc0.v().ToFloat32(), ret.view.x.ToFloat32(),
  55. ret.view.y.ToFloat32(), ret.view.z.ToFloat32());
  56. return ret;
  57. }
  58. void UnitState::LoadInput(const Regs::ShaderConfig& config, const AttributeBuffer& input) {
  59. const unsigned max_attribute = config.max_input_attribute_index;
  60. for (unsigned attr = 0; attr <= max_attribute; ++attr) {
  61. unsigned reg = config.GetRegisterForAttribute(attr);
  62. registers.input[reg] = input.attr[attr];
  63. }
  64. }
  65. void UnitState::WriteOutput(const Regs::ShaderConfig& config, AttributeBuffer& output) {
  66. unsigned int output_i = 0;
  67. for (unsigned int reg : Common::BitSet<u32>(config.output_mask)) {
  68. output.attr[output_i++] = registers.output[reg];
  69. }
  70. }
  71. MICROPROFILE_DEFINE(GPU_Shader, "GPU", "Shader", MP_RGB(50, 50, 240));
  72. #ifdef ARCHITECTURE_x86_64
  73. static std::unique_ptr<JitX64Engine> jit_engine;
  74. #endif // ARCHITECTURE_x86_64
  75. static InterpreterEngine interpreter_engine;
  76. ShaderEngine* GetEngine() {
  77. #ifdef ARCHITECTURE_x86_64
  78. // TODO(yuriks): Re-initialize on each change rather than being persistent
  79. if (VideoCore::g_shader_jit_enabled) {
  80. if (jit_engine == nullptr) {
  81. jit_engine = std::make_unique<JitX64Engine>();
  82. }
  83. return jit_engine.get();
  84. }
  85. #endif // ARCHITECTURE_x86_64
  86. return &interpreter_engine;
  87. }
  88. void Shutdown() {
  89. #ifdef ARCHITECTURE_x86_64
  90. jit_engine = nullptr;
  91. #endif // ARCHITECTURE_x86_64
  92. }
  93. } // namespace Shader
  94. } // namespace Pica