maxwell_3d.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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/rasterizer_interface.h"
  10. #include "video_core/renderer_base.h"
  11. #include "video_core/textures/decoders.h"
  12. #include "video_core/textures/texture.h"
  13. #include "video_core/video_core.h"
  14. namespace Tegra {
  15. namespace Engines {
  16. /// First register id that is actually a Macro call.
  17. constexpr u32 MacroRegistersStart = 0xE00;
  18. Maxwell3D::Maxwell3D(MemoryManager& memory_manager)
  19. : memory_manager(memory_manager), macro_interpreter(*this) {}
  20. void Maxwell3D::CallMacroMethod(u32 method, std::vector<u32> parameters) {
  21. auto macro_code = uploaded_macros.find(method);
  22. // The requested macro must have been uploaded already.
  23. ASSERT_MSG(macro_code != uploaded_macros.end(), "Macro %08X was not uploaded", method);
  24. // Reset the current macro and execute it.
  25. executing_macro = 0;
  26. macro_interpreter.Execute(macro_code->second, std::move(parameters));
  27. }
  28. void Maxwell3D::WriteReg(u32 method, u32 value, u32 remaining_params) {
  29. auto debug_context = Core::System::GetInstance().GetGPUDebugContext();
  30. // It is an error to write to a register other than the current macro's ARG register before it
  31. // has finished execution.
  32. if (executing_macro != 0) {
  33. ASSERT(method == executing_macro + 1);
  34. }
  35. // Methods after 0xE00 are special, they're actually triggers for some microcode that was
  36. // uploaded to the GPU during initialization.
  37. if (method >= MacroRegistersStart) {
  38. // We're trying to execute a macro
  39. if (executing_macro == 0) {
  40. // A macro call must begin by writing the macro method's register, not its argument.
  41. ASSERT_MSG((method % 2) == 0,
  42. "Can't start macro execution by writing to the ARGS register");
  43. executing_macro = method;
  44. }
  45. macro_params.push_back(value);
  46. // Call the macro when there are no more parameters in the command buffer
  47. if (remaining_params == 0) {
  48. CallMacroMethod(executing_macro, std::move(macro_params));
  49. }
  50. return;
  51. }
  52. ASSERT_MSG(method < Regs::NUM_REGS,
  53. "Invalid Maxwell3D register, increase the size of the Regs structure");
  54. if (debug_context) {
  55. debug_context->OnEvent(Tegra::DebugContext::Event::MaxwellCommandLoaded, nullptr);
  56. }
  57. regs.reg_array[method] = value;
  58. switch (method) {
  59. case MAXWELL3D_REG_INDEX(macros.data): {
  60. ProcessMacroUpload(value);
  61. break;
  62. }
  63. case MAXWELL3D_REG_INDEX(code_address.code_address_high):
  64. case MAXWELL3D_REG_INDEX(code_address.code_address_low): {
  65. // Note: For some reason games (like Puyo Puyo Tetris) seem to write 0 to the CODE_ADDRESS
  66. // register, we do not currently know if that's intended or a bug, so we assert it lest
  67. // stuff breaks in other places (like the shader address calculation).
  68. ASSERT_MSG(regs.code_address.CodeAddress() == 0, "Unexpected CODE_ADDRESS register value.");
  69. break;
  70. }
  71. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[0]):
  72. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[1]):
  73. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[2]):
  74. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[3]):
  75. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[4]):
  76. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[5]):
  77. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[6]):
  78. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[7]):
  79. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[8]):
  80. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[9]):
  81. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[10]):
  82. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[11]):
  83. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[12]):
  84. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[13]):
  85. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[14]):
  86. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[15]): {
  87. ProcessCBData(value);
  88. break;
  89. }
  90. case MAXWELL3D_REG_INDEX(cb_bind[0].raw_config): {
  91. ProcessCBBind(Regs::ShaderStage::Vertex);
  92. break;
  93. }
  94. case MAXWELL3D_REG_INDEX(cb_bind[1].raw_config): {
  95. ProcessCBBind(Regs::ShaderStage::TesselationControl);
  96. break;
  97. }
  98. case MAXWELL3D_REG_INDEX(cb_bind[2].raw_config): {
  99. ProcessCBBind(Regs::ShaderStage::TesselationEval);
  100. break;
  101. }
  102. case MAXWELL3D_REG_INDEX(cb_bind[3].raw_config): {
  103. ProcessCBBind(Regs::ShaderStage::Geometry);
  104. break;
  105. }
  106. case MAXWELL3D_REG_INDEX(cb_bind[4].raw_config): {
  107. ProcessCBBind(Regs::ShaderStage::Fragment);
  108. break;
  109. }
  110. case MAXWELL3D_REG_INDEX(draw.vertex_end_gl): {
  111. DrawArrays();
  112. break;
  113. }
  114. case MAXWELL3D_REG_INDEX(query.query_get): {
  115. ProcessQueryGet();
  116. break;
  117. }
  118. default:
  119. break;
  120. }
  121. VideoCore::g_renderer->Rasterizer()->NotifyMaxwellRegisterChanged(method);
  122. if (debug_context) {
  123. debug_context->OnEvent(Tegra::DebugContext::Event::MaxwellCommandProcessed, nullptr);
  124. }
  125. }
  126. void Maxwell3D::ProcessMacroUpload(u32 data) {
  127. // Store the uploaded macro code to interpret them when they're called.
  128. auto& macro = uploaded_macros[regs.macros.entry * 2 + MacroRegistersStart];
  129. macro.push_back(data);
  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. boost::optional<VAddr> address = memory_manager.GpuToCpuAddress(sequence_address);
  136. // TODO(Subv): Support the other query units.
  137. ASSERT_MSG(regs.query.query_get.unit == Regs::QueryUnit::Crop,
  138. "Units other than CROP are unimplemented");
  139. u32 value = Memory::Read32(*address);
  140. u64 result = 0;
  141. // TODO(Subv): Support the other query variables
  142. switch (regs.query.query_get.select) {
  143. case Regs::QuerySelect::Zero:
  144. // This seems to actually write the query sequence to the query address.
  145. result = regs.query.query_sequence;
  146. break;
  147. default:
  148. UNIMPLEMENTED_MSG("Unimplemented query select type {}",
  149. static_cast<u32>(regs.query.query_get.select.Value()));
  150. }
  151. // TODO(Subv): Research and implement how query sync conditions work.
  152. struct LongQueryResult {
  153. u64_le value;
  154. u64_le timestamp;
  155. };
  156. static_assert(sizeof(LongQueryResult) == 16, "LongQueryResult has wrong size");
  157. switch (regs.query.query_get.mode) {
  158. case Regs::QueryMode::Write:
  159. case Regs::QueryMode::Write2: {
  160. u32 sequence = regs.query.query_sequence;
  161. if (regs.query.query_get.short_query) {
  162. // Write the current query sequence to the sequence address.
  163. // TODO(Subv): Find out what happens if you use a long query type but mark it as a short
  164. // query.
  165. Memory::Write32(*address, sequence);
  166. } else {
  167. // Write the 128-bit result structure in long mode. Note: We emulate an infinitely fast
  168. // GPU, this command may actually take a while to complete in real hardware due to GPU
  169. // wait queues.
  170. LongQueryResult query_result{};
  171. query_result.value = result;
  172. // TODO(Subv): Generate a real GPU timestamp and write it here instead of 0
  173. query_result.timestamp = 0;
  174. Memory::WriteBlock(*address, &query_result, sizeof(query_result));
  175. }
  176. break;
  177. }
  178. default:
  179. UNIMPLEMENTED_MSG("Query mode {} not implemented",
  180. static_cast<u32>(regs.query.query_get.mode.Value()));
  181. }
  182. }
  183. void Maxwell3D::DrawArrays() {
  184. LOG_DEBUG(HW_GPU, "called, topology={}, count={}", static_cast<u32>(regs.draw.topology.Value()),
  185. regs.vertex_buffer.count);
  186. ASSERT_MSG(!(regs.index_array.count && regs.vertex_buffer.count), "Both indexed and direct?");
  187. auto debug_context = Core::System::GetInstance().GetGPUDebugContext();
  188. if (debug_context) {
  189. debug_context->OnEvent(Tegra::DebugContext::Event::IncomingPrimitiveBatch, nullptr);
  190. }
  191. if (debug_context) {
  192. debug_context->OnEvent(Tegra::DebugContext::Event::FinishedPrimitiveBatch, nullptr);
  193. }
  194. const bool is_indexed{regs.index_array.count && !regs.vertex_buffer.count};
  195. VideoCore::g_renderer->Rasterizer()->AccelerateDrawBatch(is_indexed);
  196. // TODO(bunnei): Below, we reset vertex count so that we can use these registers to determine if
  197. // the game is trying to draw indexed or direct mode. This needs to be verified on HW still -
  198. // it's possible that it is incorrect and that there is some other register used to specify the
  199. // drawing mode.
  200. if (is_indexed) {
  201. regs.index_array.count = 0;
  202. } else {
  203. regs.vertex_buffer.count = 0;
  204. }
  205. }
  206. void Maxwell3D::ProcessCBBind(Regs::ShaderStage stage) {
  207. // Bind the buffer currently in CB_ADDRESS to the specified index in the desired shader stage.
  208. auto& shader = state.shader_stages[static_cast<size_t>(stage)];
  209. auto& bind_data = regs.cb_bind[static_cast<size_t>(stage)];
  210. auto& buffer = shader.const_buffers[bind_data.index];
  211. buffer.enabled = bind_data.valid.Value() != 0;
  212. buffer.index = bind_data.index;
  213. buffer.address = regs.const_buffer.BufferAddress();
  214. buffer.size = regs.const_buffer.cb_size;
  215. }
  216. void Maxwell3D::ProcessCBData(u32 value) {
  217. // Write the input value to the current const buffer at the current position.
  218. GPUVAddr buffer_address = regs.const_buffer.BufferAddress();
  219. ASSERT(buffer_address != 0);
  220. // Don't allow writing past the end of the buffer.
  221. ASSERT(regs.const_buffer.cb_pos + sizeof(u32) <= regs.const_buffer.cb_size);
  222. boost::optional<VAddr> address =
  223. memory_manager.GpuToCpuAddress(buffer_address + regs.const_buffer.cb_pos);
  224. Memory::Write32(*address, value);
  225. // Increment the current buffer position.
  226. regs.const_buffer.cb_pos = regs.const_buffer.cb_pos + 4;
  227. }
  228. Texture::TICEntry Maxwell3D::GetTICEntry(u32 tic_index) const {
  229. GPUVAddr tic_base_address = regs.tic.TICAddress();
  230. GPUVAddr tic_address_gpu = tic_base_address + tic_index * sizeof(Texture::TICEntry);
  231. boost::optional<VAddr> tic_address_cpu = memory_manager.GpuToCpuAddress(tic_address_gpu);
  232. Texture::TICEntry tic_entry;
  233. Memory::ReadBlock(*tic_address_cpu, &tic_entry, sizeof(Texture::TICEntry));
  234. ASSERT_MSG(tic_entry.header_version == Texture::TICHeaderVersion::BlockLinear ||
  235. tic_entry.header_version == Texture::TICHeaderVersion::Pitch,
  236. "TIC versions other than BlockLinear or Pitch are unimplemented");
  237. ASSERT_MSG((tic_entry.texture_type == Texture::TextureType::Texture2D) ||
  238. (tic_entry.texture_type == Texture::TextureType::Texture2DNoMipmap),
  239. "Texture types other than Texture2D are unimplemented");
  240. auto r_type = tic_entry.r_type.Value();
  241. auto g_type = tic_entry.g_type.Value();
  242. auto b_type = tic_entry.b_type.Value();
  243. auto a_type = tic_entry.a_type.Value();
  244. // TODO(Subv): Different data types for separate components are not supported
  245. ASSERT(r_type == g_type && r_type == b_type && r_type == a_type);
  246. // TODO(Subv): Only UNORM formats are supported for now.
  247. ASSERT(r_type == Texture::ComponentType::UNORM);
  248. return tic_entry;
  249. }
  250. Texture::TSCEntry Maxwell3D::GetTSCEntry(u32 tsc_index) const {
  251. GPUVAddr tsc_base_address = regs.tsc.TSCAddress();
  252. GPUVAddr tsc_address_gpu = tsc_base_address + tsc_index * sizeof(Texture::TSCEntry);
  253. boost::optional<VAddr> tsc_address_cpu = memory_manager.GpuToCpuAddress(tsc_address_gpu);
  254. Texture::TSCEntry tsc_entry;
  255. Memory::ReadBlock(*tsc_address_cpu, &tsc_entry, sizeof(Texture::TSCEntry));
  256. return tsc_entry;
  257. }
  258. std::vector<Texture::FullTextureInfo> Maxwell3D::GetStageTextures(Regs::ShaderStage stage) const {
  259. std::vector<Texture::FullTextureInfo> textures;
  260. auto& fragment_shader = state.shader_stages[static_cast<size_t>(stage)];
  261. auto& tex_info_buffer = fragment_shader.const_buffers[regs.tex_cb_index];
  262. ASSERT(tex_info_buffer.enabled && tex_info_buffer.address != 0);
  263. GPUVAddr tic_base_address = regs.tic.TICAddress();
  264. GPUVAddr tex_info_buffer_end = tex_info_buffer.address + tex_info_buffer.size;
  265. // Offset into the texture constbuffer where the texture info begins.
  266. static constexpr size_t TextureInfoOffset = 0x20;
  267. for (GPUVAddr current_texture = tex_info_buffer.address + TextureInfoOffset;
  268. current_texture < tex_info_buffer_end; current_texture += sizeof(Texture::TextureHandle)) {
  269. Texture::TextureHandle tex_handle{
  270. Memory::Read32(*memory_manager.GpuToCpuAddress(current_texture))};
  271. Texture::FullTextureInfo tex_info{};
  272. // TODO(Subv): Use the shader to determine which textures are actually accessed.
  273. tex_info.index =
  274. static_cast<u32>(current_texture - tex_info_buffer.address - TextureInfoOffset) /
  275. sizeof(Texture::TextureHandle);
  276. // Load the TIC data.
  277. if (tex_handle.tic_id != 0) {
  278. tex_info.enabled = true;
  279. auto tic_entry = GetTICEntry(tex_handle.tic_id);
  280. // TODO(Subv): Workaround for BitField's move constructor being deleted.
  281. std::memcpy(&tex_info.tic, &tic_entry, sizeof(tic_entry));
  282. }
  283. // Load the TSC data
  284. if (tex_handle.tsc_id != 0) {
  285. auto tsc_entry = GetTSCEntry(tex_handle.tsc_id);
  286. // TODO(Subv): Workaround for BitField's move constructor being deleted.
  287. std::memcpy(&tex_info.tsc, &tsc_entry, sizeof(tsc_entry));
  288. }
  289. if (tex_info.enabled)
  290. textures.push_back(tex_info);
  291. }
  292. return textures;
  293. }
  294. Texture::FullTextureInfo Maxwell3D::GetStageTexture(Regs::ShaderStage stage, size_t offset) const {
  295. auto& shader = state.shader_stages[static_cast<size_t>(stage)];
  296. auto& tex_info_buffer = shader.const_buffers[regs.tex_cb_index];
  297. ASSERT(tex_info_buffer.enabled && tex_info_buffer.address != 0);
  298. GPUVAddr tex_info_address = tex_info_buffer.address + offset * sizeof(Texture::TextureHandle);
  299. ASSERT(tex_info_address < tex_info_buffer.address + tex_info_buffer.size);
  300. boost::optional<VAddr> tex_address_cpu = memory_manager.GpuToCpuAddress(tex_info_address);
  301. Texture::TextureHandle tex_handle{Memory::Read32(*tex_address_cpu)};
  302. Texture::FullTextureInfo tex_info{};
  303. tex_info.index = static_cast<u32>(offset);
  304. // Load the TIC data.
  305. if (tex_handle.tic_id != 0) {
  306. tex_info.enabled = true;
  307. auto tic_entry = GetTICEntry(tex_handle.tic_id);
  308. // TODO(Subv): Workaround for BitField's move constructor being deleted.
  309. std::memcpy(&tex_info.tic, &tic_entry, sizeof(tic_entry));
  310. }
  311. // Load the TSC data
  312. if (tex_handle.tsc_id != 0) {
  313. auto tsc_entry = GetTSCEntry(tex_handle.tsc_id);
  314. // TODO(Subv): Workaround for BitField's move constructor being deleted.
  315. std::memcpy(&tex_info.tsc, &tsc_entry, sizeof(tsc_entry));
  316. }
  317. return tex_info;
  318. }
  319. u32 Maxwell3D::GetRegisterValue(u32 method) const {
  320. ASSERT_MSG(method < Regs::NUM_REGS, "Invalid Maxwell3D register");
  321. return regs.reg_array[method];
  322. }
  323. bool Maxwell3D::IsShaderStageEnabled(Regs::ShaderStage stage) const {
  324. // The Vertex stage is always enabled.
  325. if (stage == Regs::ShaderStage::Vertex)
  326. return true;
  327. switch (stage) {
  328. case Regs::ShaderStage::TesselationControl:
  329. return regs.shader_config[static_cast<size_t>(Regs::ShaderProgram::TesselationControl)]
  330. .enable != 0;
  331. case Regs::ShaderStage::TesselationEval:
  332. return regs.shader_config[static_cast<size_t>(Regs::ShaderProgram::TesselationEval)]
  333. .enable != 0;
  334. case Regs::ShaderStage::Geometry:
  335. return regs.shader_config[static_cast<size_t>(Regs::ShaderProgram::Geometry)].enable != 0;
  336. case Regs::ShaderStage::Fragment:
  337. return regs.shader_config[static_cast<size_t>(Regs::ShaderProgram::Fragment)].enable != 0;
  338. }
  339. UNREACHABLE();
  340. }
  341. } // namespace Engines
  342. } // namespace Tegra