maxwell_3d.cpp 17 KB

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