maxwell_3d.cpp 14 KB

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