maxwell_3d.cpp 16 KB

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