shader.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_state.h"
  10. #include "video_core/regs_rasterizer.h"
  11. #include "video_core/regs_shader.h"
  12. #include "video_core/shader/shader.h"
  13. #include "video_core/shader/shader_interpreter.h"
  14. #ifdef ARCHITECTURE_x86_64
  15. #include "video_core/shader/shader_jit_x64.h"
  16. #endif // ARCHITECTURE_x86_64
  17. #include "video_core/video_core.h"
  18. namespace Pica {
  19. namespace Shader {
  20. OutputVertex OutputVertex::FromAttributeBuffer(const RasterizerRegs& regs,
  21. const AttributeBuffer& input) {
  22. // Setup output data
  23. union {
  24. OutputVertex ret{};
  25. std::array<float24, 24> vertex_slots;
  26. };
  27. static_assert(sizeof(vertex_slots) == sizeof(ret), "Struct and array have different sizes.");
  28. unsigned int num_attributes = regs.vs_output_total;
  29. ASSERT(num_attributes <= 7);
  30. for (unsigned int i = 0; i < num_attributes; ++i) {
  31. const auto& output_register_map = regs.vs_output_attributes[i];
  32. RasterizerRegs::VSOutputAttributes::Semantic semantics[4] = {
  33. output_register_map.map_x, output_register_map.map_y, output_register_map.map_z,
  34. output_register_map.map_w};
  35. for (unsigned comp = 0; comp < 4; ++comp) {
  36. RasterizerRegs::VSOutputAttributes::Semantic semantic = semantics[comp];
  37. if (semantic < vertex_slots.size()) {
  38. vertex_slots[semantic] = input.attr[i][comp];
  39. } else if (semantic != RasterizerRegs::VSOutputAttributes::INVALID) {
  40. LOG_ERROR(HW_GPU, "Invalid/unknown semantic id: %u", (unsigned int)semantic);
  41. }
  42. }
  43. }
  44. // The hardware takes the absolute and saturates vertex colors like this, *before* doing
  45. // interpolation
  46. for (unsigned i = 0; i < 4; ++i) {
  47. float c = std::fabs(ret.color[i].ToFloat32());
  48. ret.color[i] = float24::FromFloat32(c < 1.0f ? c : 1.0f);
  49. }
  50. LOG_TRACE(HW_GPU, "Output vertex: pos(%.2f, %.2f, %.2f, %.2f), quat(%.2f, %.2f, %.2f, %.2f), "
  51. "col(%.2f, %.2f, %.2f, %.2f), tc0(%.2f, %.2f), view(%.2f, %.2f, %.2f)",
  52. ret.pos.x.ToFloat32(), ret.pos.y.ToFloat32(), ret.pos.z.ToFloat32(),
  53. ret.pos.w.ToFloat32(), ret.quat.x.ToFloat32(), ret.quat.y.ToFloat32(),
  54. ret.quat.z.ToFloat32(), ret.quat.w.ToFloat32(), ret.color.x.ToFloat32(),
  55. ret.color.y.ToFloat32(), ret.color.z.ToFloat32(), ret.color.w.ToFloat32(),
  56. ret.tc0.u().ToFloat32(), ret.tc0.v().ToFloat32(), ret.view.x.ToFloat32(),
  57. ret.view.y.ToFloat32(), ret.view.z.ToFloat32());
  58. return ret;
  59. }
  60. void UnitState::LoadInput(const ShaderRegs& config, const AttributeBuffer& input) {
  61. const unsigned max_attribute = config.max_input_attribute_index;
  62. for (unsigned attr = 0; attr <= max_attribute; ++attr) {
  63. unsigned reg = config.GetRegisterForAttribute(attr);
  64. registers.input[reg] = input.attr[attr];
  65. }
  66. }
  67. void UnitState::WriteOutput(const ShaderRegs& config, AttributeBuffer& output) {
  68. unsigned int output_i = 0;
  69. for (unsigned int reg : Common::BitSet<u32>(config.output_mask)) {
  70. output.attr[output_i++] = registers.output[reg];
  71. }
  72. }
  73. UnitState::UnitState(GSEmitter* emitter) : emitter_ptr(emitter) {}
  74. GSEmitter::GSEmitter() {
  75. handlers = new Handlers;
  76. }
  77. GSEmitter::~GSEmitter() {
  78. delete handlers;
  79. }
  80. void GSEmitter::Emit(Math::Vec4<float24> (&vertex)[16]) {
  81. ASSERT(vertex_id < 3);
  82. std::copy(std::begin(vertex), std::end(vertex), buffer[vertex_id].begin());
  83. if (prim_emit) {
  84. if (winding)
  85. handlers->winding_setter();
  86. for (size_t i = 0; i < buffer.size(); ++i) {
  87. AttributeBuffer output;
  88. unsigned int output_i = 0;
  89. for (unsigned int reg : Common::BitSet<u32>(output_mask)) {
  90. output.attr[output_i++] = buffer[i][reg];
  91. }
  92. handlers->vertex_handler(output);
  93. }
  94. }
  95. }
  96. GSUnitState::GSUnitState() : UnitState(&emitter) {}
  97. void GSUnitState::SetVertexHandler(VertexHandler vertex_handler, WindingSetter winding_setter) {
  98. emitter.handlers->vertex_handler = std::move(vertex_handler);
  99. emitter.handlers->winding_setter = std::move(winding_setter);
  100. }
  101. void GSUnitState::ConfigOutput(const ShaderRegs& config) {
  102. emitter.output_mask = config.output_mask;
  103. }
  104. MICROPROFILE_DEFINE(GPU_Shader, "GPU", "Shader", MP_RGB(50, 50, 240));
  105. #ifdef ARCHITECTURE_x86_64
  106. static std::unique_ptr<JitX64Engine> jit_engine;
  107. #endif // ARCHITECTURE_x86_64
  108. static InterpreterEngine interpreter_engine;
  109. ShaderEngine* GetEngine() {
  110. #ifdef ARCHITECTURE_x86_64
  111. // TODO(yuriks): Re-initialize on each change rather than being persistent
  112. if (VideoCore::g_shader_jit_enabled) {
  113. if (jit_engine == nullptr) {
  114. jit_engine = std::make_unique<JitX64Engine>();
  115. }
  116. return jit_engine.get();
  117. }
  118. #endif // ARCHITECTURE_x86_64
  119. return &interpreter_engine;
  120. }
  121. void Shutdown() {
  122. #ifdef ARCHITECTURE_x86_64
  123. jit_engine = nullptr;
  124. #endif // ARCHITECTURE_x86_64
  125. }
  126. } // namespace Shader
  127. } // namespace Pica