maxwell_3d.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/assert.h"
  5. #include "video_core/engines/maxwell_3d.h"
  6. namespace Tegra {
  7. namespace Engines {
  8. /// First register id that is actually a Macro call.
  9. constexpr u32 MacroRegistersStart = 0xE00;
  10. const std::unordered_map<u32, Maxwell3D::MethodInfo> Maxwell3D::method_handlers = {
  11. {0xE24, {"SetShader", 5, &Maxwell3D::SetShader}},
  12. {0xE2A, {"BindStorageBuffer", 1, &Maxwell3D::BindStorageBuffer}},
  13. };
  14. Maxwell3D::Maxwell3D(MemoryManager& memory_manager) : memory_manager(memory_manager) {}
  15. void Maxwell3D::SubmitMacroCode(u32 entry, std::vector<u32> code) {
  16. uploaded_macros[entry * 2 + MacroRegistersStart] = std::move(code);
  17. }
  18. void Maxwell3D::CallMacroMethod(u32 method, const std::vector<u32>& parameters) {
  19. // TODO(Subv): Write an interpreter for the macros uploaded via registers 0x45 and 0x47
  20. // The requested macro must have been uploaded already.
  21. ASSERT_MSG(uploaded_macros.find(method) != uploaded_macros.end(), "Macro %08X was not uploaded",
  22. method);
  23. auto itr = method_handlers.find(method);
  24. ASSERT_MSG(itr != method_handlers.end(), "Unhandled method call %08X", method);
  25. ASSERT(itr->second.arguments == parameters.size());
  26. (this->*itr->second.handler)(parameters);
  27. // Reset the current macro and its parameters.
  28. executing_macro = 0;
  29. macro_params.clear();
  30. }
  31. void Maxwell3D::WriteReg(u32 method, u32 value, u32 remaining_params) {
  32. ASSERT_MSG(method < Regs::NUM_REGS,
  33. "Invalid Maxwell3D register, increase the size of the Regs structure");
  34. // It is an error to write to a register other than the current macro's ARG register before it
  35. // has finished execution.
  36. if (executing_macro != 0) {
  37. ASSERT(method == executing_macro + 1);
  38. }
  39. // Methods after 0xE00 are special, they're actually triggers for some microcode that was
  40. // uploaded to the GPU during initialization.
  41. if (method >= MacroRegistersStart) {
  42. // We're trying to execute a macro
  43. if (executing_macro == 0) {
  44. // A macro call must begin by writing the macro method's register, not its argument.
  45. ASSERT_MSG((method % 2) == 0,
  46. "Can't start macro execution by writing to the ARGS register");
  47. executing_macro = method;
  48. }
  49. macro_params.push_back(value);
  50. // Call the macro when there are no more parameters in the command buffer
  51. if (remaining_params == 0) {
  52. CallMacroMethod(executing_macro, macro_params);
  53. }
  54. return;
  55. }
  56. regs.reg_array[method] = value;
  57. #define MAXWELL3D_REG_INDEX(field_name) (offsetof(Regs, field_name) / sizeof(u32))
  58. switch (method) {
  59. case MAXWELL3D_REG_INDEX(code_address.code_address_high):
  60. case MAXWELL3D_REG_INDEX(code_address.code_address_low): {
  61. // Note: For some reason games (like Puyo Puyo Tetris) seem to write 0 to the CODE_ADDRESS
  62. // register, we do not currently know if that's intended or a bug, so we assert it lest
  63. // stuff breaks in other places (like the shader address calculation).
  64. ASSERT_MSG(regs.code_address.CodeAddress() == 0, "Unexpected CODE_ADDRESS register value.");
  65. break;
  66. }
  67. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[0]):
  68. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[1]):
  69. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[2]):
  70. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[3]):
  71. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[4]):
  72. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[5]):
  73. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[6]):
  74. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[7]):
  75. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[8]):
  76. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[9]):
  77. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[10]):
  78. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[11]):
  79. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[12]):
  80. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[13]):
  81. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[14]):
  82. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[15]): {
  83. ProcessCBData(value);
  84. break;
  85. }
  86. case MAXWELL3D_REG_INDEX(cb_bind[0].raw_config): {
  87. ProcessCBBind(Regs::ShaderStage::Vertex);
  88. break;
  89. }
  90. case MAXWELL3D_REG_INDEX(cb_bind[1].raw_config): {
  91. ProcessCBBind(Regs::ShaderStage::TesselationControl);
  92. break;
  93. }
  94. case MAXWELL3D_REG_INDEX(cb_bind[2].raw_config): {
  95. ProcessCBBind(Regs::ShaderStage::TesselationEval);
  96. break;
  97. }
  98. case MAXWELL3D_REG_INDEX(cb_bind[3].raw_config): {
  99. ProcessCBBind(Regs::ShaderStage::Geometry);
  100. break;
  101. }
  102. case MAXWELL3D_REG_INDEX(cb_bind[4].raw_config): {
  103. ProcessCBBind(Regs::ShaderStage::Fragment);
  104. break;
  105. }
  106. case MAXWELL3D_REG_INDEX(draw.vertex_end_gl): {
  107. DrawArrays();
  108. break;
  109. }
  110. case MAXWELL3D_REG_INDEX(query.query_get): {
  111. ProcessQueryGet();
  112. break;
  113. }
  114. default:
  115. break;
  116. }
  117. #undef MAXWELL3D_REG_INDEX
  118. }
  119. void Maxwell3D::ProcessQueryGet() {
  120. GPUVAddr sequence_address = regs.query.QueryAddress();
  121. // Since the sequence address is given as a GPU VAddr, we have to convert it to an application
  122. // VAddr before writing.
  123. VAddr address = memory_manager.PhysicalToVirtualAddress(sequence_address);
  124. switch (regs.query.query_get.mode) {
  125. case Regs::QueryMode::Write: {
  126. // Write the current query sequence to the sequence address.
  127. u32 sequence = regs.query.query_sequence;
  128. Memory::Write32(address, sequence);
  129. break;
  130. }
  131. default:
  132. UNIMPLEMENTED_MSG("Query mode %u not implemented", regs.query.query_get.mode.Value());
  133. }
  134. }
  135. void Maxwell3D::DrawArrays() {
  136. LOG_WARNING(HW_GPU, "Game requested a DrawArrays, ignoring");
  137. }
  138. void Maxwell3D::SetShader(const std::vector<u32>& parameters) {
  139. /**
  140. * Parameters description:
  141. * [0] = Shader Program.
  142. * [1] = Unknown, presumably the shader id.
  143. * [2] = Offset to the start of the shader, after the 0x30 bytes header.
  144. * [3] = Shader Stage.
  145. * [4] = Const Buffer Address >> 8.
  146. */
  147. auto shader_program = static_cast<Regs::ShaderProgram>(parameters[0]);
  148. // TODO(Subv): This address is probably an offset from the CODE_ADDRESS register.
  149. GPUVAddr address = parameters[2];
  150. auto shader_stage = static_cast<Regs::ShaderStage>(parameters[3]);
  151. GPUVAddr cb_address = parameters[4] << 8;
  152. auto& shader = state.shader_programs[static_cast<size_t>(shader_program)];
  153. shader.program = shader_program;
  154. shader.stage = shader_stage;
  155. shader.address = address;
  156. // Perform the same operations as the real macro code.
  157. // TODO(Subv): Early exit if register 0xD1C + shader_program contains the same as params[1].
  158. auto& shader_regs = regs.shader_config[static_cast<size_t>(shader_program)];
  159. shader_regs.start_id = address;
  160. // TODO(Subv): Write params[1] to register 0xD1C + shader_program.
  161. // TODO(Subv): Write params[2] to register 0xD22 + shader_program.
  162. // Note: This value is hardcoded in the macro's code.
  163. static constexpr u32 DefaultCBSize = 0x10000;
  164. regs.const_buffer.cb_size = DefaultCBSize;
  165. regs.const_buffer.cb_address_high = cb_address >> 32;
  166. regs.const_buffer.cb_address_low = cb_address & 0xFFFFFFFF;
  167. // Write a hardcoded 0x11 to CB_BIND, this binds the current const buffer to buffer c1[] in the
  168. // shader. It's likely that these are the constants for the shader.
  169. regs.cb_bind[static_cast<size_t>(shader_stage)].valid.Assign(1);
  170. regs.cb_bind[static_cast<size_t>(shader_stage)].index.Assign(1);
  171. ProcessCBBind(shader_stage);
  172. }
  173. void Maxwell3D::BindStorageBuffer(const std::vector<u32>& parameters) {
  174. /**
  175. * Parameters description:
  176. * [0] = Buffer offset >> 2
  177. */
  178. u32 buffer_offset = parameters[0] << 2;
  179. // Perform the same operations as the real macro code.
  180. // Note: This value is hardcoded in the macro's code.
  181. static constexpr u32 DefaultCBSize = 0x5F00;
  182. regs.const_buffer.cb_size = DefaultCBSize;
  183. GPUVAddr address = regs.ssbo_info.BufferAddress();
  184. regs.const_buffer.cb_address_high = address >> 32;
  185. regs.const_buffer.cb_address_low = address & 0xFFFFFFFF;
  186. regs.const_buffer.cb_pos = buffer_offset;
  187. }
  188. void Maxwell3D::ProcessCBBind(Regs::ShaderStage stage) {
  189. // Bind the buffer currently in CB_ADDRESS to the specified index in the desired shader stage.
  190. auto& shader = state.shader_stages[static_cast<size_t>(stage)];
  191. auto& bind_data = regs.cb_bind[static_cast<size_t>(stage)];
  192. auto& buffer = shader.const_buffers[bind_data.index];
  193. buffer.enabled = bind_data.valid.Value() != 0;
  194. buffer.index = bind_data.index;
  195. buffer.address = regs.const_buffer.BufferAddress();
  196. buffer.size = regs.const_buffer.cb_size;
  197. }
  198. void Maxwell3D::ProcessCBData(u32 value) {
  199. // Write the input value to the current const buffer at the current position.
  200. GPUVAddr buffer_address = regs.const_buffer.BufferAddress();
  201. ASSERT(buffer_address != 0);
  202. // Don't allow writing past the end of the buffer.
  203. ASSERT(regs.const_buffer.cb_pos + sizeof(u32) <= regs.const_buffer.cb_size);
  204. VAddr address =
  205. memory_manager.PhysicalToVirtualAddress(buffer_address + regs.const_buffer.cb_pos);
  206. Memory::Write32(address, value);
  207. // Increment the current buffer position.
  208. regs.const_buffer.cb_pos = regs.const_buffer.cb_pos + 4;
  209. }
  210. } // namespace Engines
  211. } // namespace Tegra