maxwell_3d.cpp 13 KB

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