maxwell_3d.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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 "video_core/debug_utils/debug_utils.h"
  10. #include "video_core/engines/maxwell_3d.h"
  11. #include "video_core/memory_manager.h"
  12. #include "video_core/rasterizer_interface.h"
  13. #include "video_core/textures/texture.h"
  14. namespace Tegra::Engines {
  15. /// First register id that is actually a Macro call.
  16. constexpr u32 MacroRegistersStart = 0xE00;
  17. Maxwell3D::Maxwell3D(Core::System& system, VideoCore::RasterizerInterface& rasterizer,
  18. MemoryManager& memory_manager)
  19. : system{system}, rasterizer{rasterizer}, memory_manager{memory_manager},
  20. macro_interpreter{*this}, upload_state{memory_manager, regs.upload} {
  21. InitDirtySettings();
  22. InitializeRegisterDefaults();
  23. }
  24. void Maxwell3D::InitializeRegisterDefaults() {
  25. // Initializes registers to their default values - what games expect them to be at boot. This is
  26. // for certain registers that may not be explicitly set by games.
  27. // Reset all registers to zero
  28. std::memset(&regs, 0, sizeof(regs));
  29. // Depth range near/far is not always set, but is expected to be the default 0.0f, 1.0f. This is
  30. // needed for ARMS.
  31. for (auto& viewport : regs.viewports) {
  32. viewport.depth_range_near = 0.0f;
  33. viewport.depth_range_far = 1.0f;
  34. }
  35. // Doom and Bomberman seems to use the uninitialized registers and just enable blend
  36. // so initialize blend registers with sane values
  37. regs.blend.equation_rgb = Regs::Blend::Equation::Add;
  38. regs.blend.factor_source_rgb = Regs::Blend::Factor::One;
  39. regs.blend.factor_dest_rgb = Regs::Blend::Factor::Zero;
  40. regs.blend.equation_a = Regs::Blend::Equation::Add;
  41. regs.blend.factor_source_a = Regs::Blend::Factor::One;
  42. regs.blend.factor_dest_a = Regs::Blend::Factor::Zero;
  43. for (auto& blend : regs.independent_blend) {
  44. blend.equation_rgb = Regs::Blend::Equation::Add;
  45. blend.factor_source_rgb = Regs::Blend::Factor::One;
  46. blend.factor_dest_rgb = Regs::Blend::Factor::Zero;
  47. blend.equation_a = Regs::Blend::Equation::Add;
  48. blend.factor_source_a = Regs::Blend::Factor::One;
  49. blend.factor_dest_a = Regs::Blend::Factor::Zero;
  50. }
  51. regs.stencil_front_op_fail = Regs::StencilOp::Keep;
  52. regs.stencil_front_op_zfail = Regs::StencilOp::Keep;
  53. regs.stencil_front_op_zpass = Regs::StencilOp::Keep;
  54. regs.stencil_front_func_func = Regs::ComparisonOp::Always;
  55. regs.stencil_front_func_mask = 0xFFFFFFFF;
  56. regs.stencil_front_mask = 0xFFFFFFFF;
  57. regs.stencil_two_side_enable = 1;
  58. regs.stencil_back_op_fail = Regs::StencilOp::Keep;
  59. regs.stencil_back_op_zfail = Regs::StencilOp::Keep;
  60. regs.stencil_back_op_zpass = Regs::StencilOp::Keep;
  61. regs.stencil_back_func_func = Regs::ComparisonOp::Always;
  62. regs.stencil_back_func_mask = 0xFFFFFFFF;
  63. regs.stencil_back_mask = 0xFFFFFFFF;
  64. regs.depth_test_func = Regs::ComparisonOp::Always;
  65. regs.cull.front_face = Regs::Cull::FrontFace::CounterClockWise;
  66. regs.cull.cull_face = Regs::Cull::CullFace::Back;
  67. // TODO(Rodrigo): Most games do not set a point size. I think this is a case of a
  68. // register carrying a default value. Assume it's OpenGL's default (1).
  69. regs.point_size = 1.0f;
  70. // TODO(bunnei): Some games do not initialize the color masks (e.g. Sonic Mania). Assuming a
  71. // default of enabled fixes rendering here.
  72. for (auto& color_mask : regs.color_mask) {
  73. color_mask.R.Assign(1);
  74. color_mask.G.Assign(1);
  75. color_mask.B.Assign(1);
  76. color_mask.A.Assign(1);
  77. }
  78. // Commercial games seem to assume this value is enabled and nouveau sets this value manually.
  79. regs.rt_separate_frag_data = 1;
  80. }
  81. #define DIRTY_REGS_POS(field_name) (offsetof(Maxwell3D::DirtyRegs, field_name))
  82. void Maxwell3D::InitDirtySettings() {
  83. const auto set_block = [this](const u32 start, const u32 range, const u8 position) {
  84. const auto start_itr = dirty_pointers.begin() + start;
  85. const auto end_itr = start_itr + range;
  86. std::fill(start_itr, end_itr, position);
  87. };
  88. dirty.regs.fill(true);
  89. // Init Render Targets
  90. constexpr u32 registers_per_rt = sizeof(regs.rt[0]) / sizeof(u32);
  91. constexpr u32 rt_start_reg = MAXWELL3D_REG_INDEX(rt);
  92. constexpr u32 rt_end_reg = rt_start_reg + registers_per_rt * 8;
  93. u32 rt_dirty_reg = DIRTY_REGS_POS(render_target);
  94. for (u32 rt_reg = rt_start_reg; rt_reg < rt_end_reg; rt_reg += registers_per_rt) {
  95. set_block(rt_reg, registers_per_rt, rt_dirty_reg);
  96. rt_dirty_reg++;
  97. }
  98. constexpr u32 depth_buffer_flag = DIRTY_REGS_POS(depth_buffer);
  99. dirty_pointers[MAXWELL3D_REG_INDEX(zeta_enable)] = depth_buffer_flag;
  100. dirty_pointers[MAXWELL3D_REG_INDEX(zeta_width)] = depth_buffer_flag;
  101. dirty_pointers[MAXWELL3D_REG_INDEX(zeta_height)] = depth_buffer_flag;
  102. constexpr u32 registers_in_zeta = sizeof(regs.zeta) / sizeof(u32);
  103. constexpr u32 zeta_reg = MAXWELL3D_REG_INDEX(zeta);
  104. set_block(zeta_reg, registers_in_zeta, depth_buffer_flag);
  105. // Init Vertex Arrays
  106. constexpr u32 vertex_array_start = MAXWELL3D_REG_INDEX(vertex_array);
  107. constexpr u32 vertex_array_size = sizeof(regs.vertex_array[0]) / sizeof(u32);
  108. constexpr u32 vertex_array_end = vertex_array_start + vertex_array_size * Regs::NumVertexArrays;
  109. u32 va_reg = DIRTY_REGS_POS(vertex_array);
  110. u32 vi_reg = DIRTY_REGS_POS(vertex_instance);
  111. for (u32 vertex_reg = vertex_array_start; vertex_reg < vertex_array_end;
  112. vertex_reg += vertex_array_size) {
  113. set_block(vertex_reg, 3, va_reg);
  114. // The divisor concerns vertex array instances
  115. dirty_pointers[vertex_reg + 3] = vi_reg;
  116. va_reg++;
  117. vi_reg++;
  118. }
  119. constexpr u32 vertex_limit_start = MAXWELL3D_REG_INDEX(vertex_array_limit);
  120. constexpr u32 vertex_limit_size = sizeof(regs.vertex_array_limit[0]) / sizeof(u32);
  121. constexpr u32 vertex_limit_end = vertex_limit_start + vertex_limit_size * Regs::NumVertexArrays;
  122. va_reg = DIRTY_REGS_POS(vertex_array);
  123. for (u32 vertex_reg = vertex_limit_start; vertex_reg < vertex_limit_end;
  124. vertex_reg += vertex_limit_size) {
  125. set_block(vertex_reg, vertex_limit_size, va_reg);
  126. va_reg++;
  127. }
  128. constexpr u32 vertex_instance_start = MAXWELL3D_REG_INDEX(instanced_arrays);
  129. constexpr u32 vertex_instance_size =
  130. sizeof(regs.instanced_arrays.is_instanced[0]) / sizeof(u32);
  131. constexpr u32 vertex_instance_end =
  132. vertex_instance_start + vertex_instance_size * Regs::NumVertexArrays;
  133. vi_reg = DIRTY_REGS_POS(vertex_instance);
  134. for (u32 vertex_reg = vertex_instance_start; vertex_reg < vertex_instance_end;
  135. vertex_reg += vertex_instance_size) {
  136. set_block(vertex_reg, vertex_instance_size, vi_reg);
  137. vi_reg++;
  138. }
  139. set_block(MAXWELL3D_REG_INDEX(vertex_attrib_format), regs.vertex_attrib_format.size(),
  140. DIRTY_REGS_POS(vertex_attrib_format));
  141. // Init Shaders
  142. constexpr u32 shader_registers_count =
  143. sizeof(regs.shader_config[0]) * Regs::MaxShaderProgram / sizeof(u32);
  144. set_block(MAXWELL3D_REG_INDEX(shader_config[0]), shader_registers_count,
  145. DIRTY_REGS_POS(shaders));
  146. // State
  147. // Viewport
  148. constexpr u32 viewport_dirty_reg = DIRTY_REGS_POS(viewport);
  149. constexpr u32 viewport_start = MAXWELL3D_REG_INDEX(viewports);
  150. constexpr u32 viewport_size = sizeof(regs.viewports) / sizeof(u32);
  151. set_block(viewport_start, viewport_size, viewport_dirty_reg);
  152. constexpr u32 view_volume_start = MAXWELL3D_REG_INDEX(view_volume_clip_control);
  153. constexpr u32 view_volume_size = sizeof(regs.view_volume_clip_control) / sizeof(u32);
  154. set_block(view_volume_start, view_volume_size, viewport_dirty_reg);
  155. // Viewport transformation
  156. constexpr u32 viewport_trans_start = MAXWELL3D_REG_INDEX(viewport_transform);
  157. constexpr u32 viewport_trans_size = sizeof(regs.viewport_transform) / sizeof(u32);
  158. set_block(viewport_trans_start, viewport_trans_size, DIRTY_REGS_POS(viewport_transform));
  159. // Cullmode
  160. constexpr u32 cull_mode_start = MAXWELL3D_REG_INDEX(cull);
  161. constexpr u32 cull_mode_size = sizeof(regs.cull) / sizeof(u32);
  162. set_block(cull_mode_start, cull_mode_size, DIRTY_REGS_POS(cull_mode));
  163. // Screen y control
  164. dirty_pointers[MAXWELL3D_REG_INDEX(screen_y_control)] = DIRTY_REGS_POS(screen_y_control);
  165. // Primitive Restart
  166. constexpr u32 primitive_restart_start = MAXWELL3D_REG_INDEX(primitive_restart);
  167. constexpr u32 primitive_restart_size = sizeof(regs.primitive_restart) / sizeof(u32);
  168. set_block(primitive_restart_start, primitive_restart_size, DIRTY_REGS_POS(primitive_restart));
  169. // Depth Test
  170. constexpr u32 depth_test_dirty_reg = DIRTY_REGS_POS(depth_test);
  171. dirty_pointers[MAXWELL3D_REG_INDEX(depth_test_enable)] = depth_test_dirty_reg;
  172. dirty_pointers[MAXWELL3D_REG_INDEX(depth_write_enabled)] = depth_test_dirty_reg;
  173. dirty_pointers[MAXWELL3D_REG_INDEX(depth_test_func)] = depth_test_dirty_reg;
  174. // Stencil Test
  175. constexpr u32 stencil_test_dirty_reg = DIRTY_REGS_POS(stencil_test);
  176. dirty_pointers[MAXWELL3D_REG_INDEX(stencil_enable)] = stencil_test_dirty_reg;
  177. dirty_pointers[MAXWELL3D_REG_INDEX(stencil_front_func_func)] = stencil_test_dirty_reg;
  178. dirty_pointers[MAXWELL3D_REG_INDEX(stencil_front_func_ref)] = stencil_test_dirty_reg;
  179. dirty_pointers[MAXWELL3D_REG_INDEX(stencil_front_func_mask)] = stencil_test_dirty_reg;
  180. dirty_pointers[MAXWELL3D_REG_INDEX(stencil_front_op_fail)] = stencil_test_dirty_reg;
  181. dirty_pointers[MAXWELL3D_REG_INDEX(stencil_front_op_zfail)] = stencil_test_dirty_reg;
  182. dirty_pointers[MAXWELL3D_REG_INDEX(stencil_front_op_zpass)] = stencil_test_dirty_reg;
  183. dirty_pointers[MAXWELL3D_REG_INDEX(stencil_front_mask)] = stencil_test_dirty_reg;
  184. dirty_pointers[MAXWELL3D_REG_INDEX(stencil_two_side_enable)] = stencil_test_dirty_reg;
  185. dirty_pointers[MAXWELL3D_REG_INDEX(stencil_back_func_func)] = stencil_test_dirty_reg;
  186. dirty_pointers[MAXWELL3D_REG_INDEX(stencil_back_func_ref)] = stencil_test_dirty_reg;
  187. dirty_pointers[MAXWELL3D_REG_INDEX(stencil_back_func_mask)] = stencil_test_dirty_reg;
  188. dirty_pointers[MAXWELL3D_REG_INDEX(stencil_back_op_fail)] = stencil_test_dirty_reg;
  189. dirty_pointers[MAXWELL3D_REG_INDEX(stencil_back_op_zfail)] = stencil_test_dirty_reg;
  190. dirty_pointers[MAXWELL3D_REG_INDEX(stencil_back_op_zpass)] = stencil_test_dirty_reg;
  191. dirty_pointers[MAXWELL3D_REG_INDEX(stencil_back_mask)] = stencil_test_dirty_reg;
  192. // Color Mask
  193. constexpr u32 color_mask_dirty_reg = DIRTY_REGS_POS(color_mask);
  194. dirty_pointers[MAXWELL3D_REG_INDEX(color_mask_common)] = color_mask_dirty_reg;
  195. set_block(MAXWELL3D_REG_INDEX(color_mask), sizeof(regs.color_mask) / sizeof(u32),
  196. color_mask_dirty_reg);
  197. // Blend State
  198. constexpr u32 blend_state_dirty_reg = DIRTY_REGS_POS(blend_state);
  199. set_block(MAXWELL3D_REG_INDEX(blend_color), sizeof(regs.blend_color) / sizeof(u32),
  200. blend_state_dirty_reg);
  201. dirty_pointers[MAXWELL3D_REG_INDEX(independent_blend_enable)] = blend_state_dirty_reg;
  202. set_block(MAXWELL3D_REG_INDEX(blend), sizeof(regs.blend) / sizeof(u32), blend_state_dirty_reg);
  203. set_block(MAXWELL3D_REG_INDEX(independent_blend), sizeof(regs.independent_blend) / sizeof(u32),
  204. blend_state_dirty_reg);
  205. // Scissor State
  206. constexpr u32 scissor_test_dirty_reg = DIRTY_REGS_POS(scissor_test);
  207. set_block(MAXWELL3D_REG_INDEX(scissor_test), sizeof(regs.scissor_test) / sizeof(u32),
  208. scissor_test_dirty_reg);
  209. // Polygon Offset
  210. constexpr u32 polygon_offset_dirty_reg = DIRTY_REGS_POS(polygon_offset);
  211. dirty_pointers[MAXWELL3D_REG_INDEX(polygon_offset_fill_enable)] = polygon_offset_dirty_reg;
  212. dirty_pointers[MAXWELL3D_REG_INDEX(polygon_offset_line_enable)] = polygon_offset_dirty_reg;
  213. dirty_pointers[MAXWELL3D_REG_INDEX(polygon_offset_point_enable)] = polygon_offset_dirty_reg;
  214. dirty_pointers[MAXWELL3D_REG_INDEX(polygon_offset_units)] = polygon_offset_dirty_reg;
  215. dirty_pointers[MAXWELL3D_REG_INDEX(polygon_offset_factor)] = polygon_offset_dirty_reg;
  216. dirty_pointers[MAXWELL3D_REG_INDEX(polygon_offset_clamp)] = polygon_offset_dirty_reg;
  217. }
  218. void Maxwell3D::CallMacroMethod(u32 method, std::vector<u32> parameters) {
  219. // Reset the current macro.
  220. executing_macro = 0;
  221. // Lookup the macro offset
  222. const u32 entry = ((method - MacroRegistersStart) >> 1) % macro_positions.size();
  223. // Execute the current macro.
  224. macro_interpreter.Execute(macro_positions[entry], std::move(parameters));
  225. }
  226. void Maxwell3D::CallMethod(const GPU::MethodCall& method_call) {
  227. auto debug_context = system.GetGPUDebugContext();
  228. const u32 method = method_call.method;
  229. if (method == cb_data_state.current) {
  230. regs.reg_array[method] = method_call.argument;
  231. ProcessCBData(method_call.argument);
  232. return;
  233. } else if (cb_data_state.current != null_cb_data) {
  234. FinishCBData();
  235. }
  236. // It is an error to write to a register other than the current macro's ARG register before it
  237. // has finished execution.
  238. if (executing_macro != 0) {
  239. ASSERT(method == executing_macro + 1);
  240. }
  241. // Methods after 0xE00 are special, they're actually triggers for some microcode that was
  242. // uploaded to the GPU during initialization.
  243. if (method >= MacroRegistersStart) {
  244. // We're trying to execute a macro
  245. if (executing_macro == 0) {
  246. // A macro call must begin by writing the macro method's register, not its argument.
  247. ASSERT_MSG((method % 2) == 0,
  248. "Can't start macro execution by writing to the ARGS register");
  249. executing_macro = method;
  250. }
  251. macro_params.push_back(method_call.argument);
  252. // Call the macro when there are no more parameters in the command buffer
  253. if (method_call.IsLastCall()) {
  254. CallMacroMethod(executing_macro, std::move(macro_params));
  255. }
  256. return;
  257. }
  258. ASSERT_MSG(method < Regs::NUM_REGS,
  259. "Invalid Maxwell3D register, increase the size of the Regs structure");
  260. if (debug_context) {
  261. debug_context->OnEvent(Tegra::DebugContext::Event::MaxwellCommandLoaded, nullptr);
  262. }
  263. if (regs.reg_array[method] != method_call.argument) {
  264. regs.reg_array[method] = method_call.argument;
  265. const std::size_t dirty_reg = dirty_pointers[method];
  266. if (dirty_reg) {
  267. dirty.regs[dirty_reg] = true;
  268. if (dirty_reg >= DIRTY_REGS_POS(vertex_array) &&
  269. dirty_reg < DIRTY_REGS_POS(vertex_array_buffers)) {
  270. dirty.vertex_array_buffers = true;
  271. } else if (dirty_reg >= DIRTY_REGS_POS(vertex_instance) &&
  272. dirty_reg < DIRTY_REGS_POS(vertex_instances)) {
  273. dirty.vertex_instances = true;
  274. } else if (dirty_reg >= DIRTY_REGS_POS(render_target) &&
  275. dirty_reg < DIRTY_REGS_POS(render_settings)) {
  276. dirty.render_settings = true;
  277. }
  278. }
  279. }
  280. switch (method) {
  281. case MAXWELL3D_REG_INDEX(macros.data): {
  282. ProcessMacroUpload(method_call.argument);
  283. break;
  284. }
  285. case MAXWELL3D_REG_INDEX(macros.bind): {
  286. ProcessMacroBind(method_call.argument);
  287. break;
  288. }
  289. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[0]):
  290. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[1]):
  291. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[2]):
  292. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[3]):
  293. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[4]):
  294. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[5]):
  295. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[6]):
  296. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[7]):
  297. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[8]):
  298. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[9]):
  299. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[10]):
  300. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[11]):
  301. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[12]):
  302. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[13]):
  303. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[14]):
  304. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[15]): {
  305. StartCBData(method);
  306. break;
  307. }
  308. case MAXWELL3D_REG_INDEX(cb_bind[0].raw_config): {
  309. ProcessCBBind(Regs::ShaderStage::Vertex);
  310. break;
  311. }
  312. case MAXWELL3D_REG_INDEX(cb_bind[1].raw_config): {
  313. ProcessCBBind(Regs::ShaderStage::TesselationControl);
  314. break;
  315. }
  316. case MAXWELL3D_REG_INDEX(cb_bind[2].raw_config): {
  317. ProcessCBBind(Regs::ShaderStage::TesselationEval);
  318. break;
  319. }
  320. case MAXWELL3D_REG_INDEX(cb_bind[3].raw_config): {
  321. ProcessCBBind(Regs::ShaderStage::Geometry);
  322. break;
  323. }
  324. case MAXWELL3D_REG_INDEX(cb_bind[4].raw_config): {
  325. ProcessCBBind(Regs::ShaderStage::Fragment);
  326. break;
  327. }
  328. case MAXWELL3D_REG_INDEX(draw.vertex_end_gl): {
  329. DrawArrays();
  330. break;
  331. }
  332. case MAXWELL3D_REG_INDEX(clear_buffers): {
  333. ProcessClearBuffers();
  334. break;
  335. }
  336. case MAXWELL3D_REG_INDEX(query.query_get): {
  337. ProcessQueryGet();
  338. break;
  339. }
  340. case MAXWELL3D_REG_INDEX(condition.mode): {
  341. ProcessQueryCondition();
  342. break;
  343. }
  344. case MAXWELL3D_REG_INDEX(sync_info): {
  345. ProcessSyncPoint();
  346. break;
  347. }
  348. case MAXWELL3D_REG_INDEX(exec_upload): {
  349. upload_state.ProcessExec(regs.exec_upload.linear != 0);
  350. break;
  351. }
  352. case MAXWELL3D_REG_INDEX(data_upload): {
  353. const bool is_last_call = method_call.IsLastCall();
  354. upload_state.ProcessData(method_call.argument, is_last_call);
  355. if (is_last_call) {
  356. dirty.OnMemoryWrite();
  357. }
  358. break;
  359. }
  360. default:
  361. break;
  362. }
  363. if (debug_context) {
  364. debug_context->OnEvent(Tegra::DebugContext::Event::MaxwellCommandProcessed, nullptr);
  365. }
  366. }
  367. void Maxwell3D::ProcessMacroUpload(u32 data) {
  368. ASSERT_MSG(regs.macros.upload_address < macro_memory.size(),
  369. "upload_address exceeded macro_memory size!");
  370. macro_memory[regs.macros.upload_address++] = data;
  371. }
  372. void Maxwell3D::ProcessMacroBind(u32 data) {
  373. macro_positions[regs.macros.entry++] = data;
  374. }
  375. void Maxwell3D::ProcessQueryGet() {
  376. const GPUVAddr sequence_address{regs.query.QueryAddress()};
  377. // Since the sequence address is given as a GPU VAddr, we have to convert it to an application
  378. // VAddr before writing.
  379. // TODO(Subv): Support the other query units.
  380. ASSERT_MSG(regs.query.query_get.unit == Regs::QueryUnit::Crop,
  381. "Units other than CROP are unimplemented");
  382. u64 result = 0;
  383. // TODO(Subv): Support the other query variables
  384. switch (regs.query.query_get.select) {
  385. case Regs::QuerySelect::Zero:
  386. // This seems to actually write the query sequence to the query address.
  387. result = regs.query.query_sequence;
  388. break;
  389. default:
  390. result = 1;
  391. UNIMPLEMENTED_MSG("Unimplemented query select type {}",
  392. static_cast<u32>(regs.query.query_get.select.Value()));
  393. }
  394. // TODO(Subv): Research and implement how query sync conditions work.
  395. struct LongQueryResult {
  396. u64_le value;
  397. u64_le timestamp;
  398. };
  399. static_assert(sizeof(LongQueryResult) == 16, "LongQueryResult has wrong size");
  400. switch (regs.query.query_get.mode) {
  401. case Regs::QueryMode::Write:
  402. case Regs::QueryMode::Write2: {
  403. u32 sequence = regs.query.query_sequence;
  404. if (regs.query.query_get.short_query) {
  405. // Write the current query sequence to the sequence address.
  406. // TODO(Subv): Find out what happens if you use a long query type but mark it as a short
  407. // query.
  408. memory_manager.Write<u32>(sequence_address, sequence);
  409. } else {
  410. // Write the 128-bit result structure in long mode. Note: We emulate an infinitely fast
  411. // GPU, this command may actually take a while to complete in real hardware due to GPU
  412. // wait queues.
  413. LongQueryResult query_result{};
  414. query_result.value = result;
  415. // TODO(Subv): Generate a real GPU timestamp and write it here instead of CoreTiming
  416. query_result.timestamp = system.CoreTiming().GetTicks();
  417. memory_manager.WriteBlock(sequence_address, &query_result, sizeof(query_result));
  418. }
  419. break;
  420. }
  421. default:
  422. UNIMPLEMENTED_MSG("Query mode {} not implemented",
  423. static_cast<u32>(regs.query.query_get.mode.Value()));
  424. }
  425. }
  426. void Maxwell3D::ProcessQueryCondition() {
  427. const GPUVAddr condition_address{regs.condition.Address()};
  428. switch (regs.condition.mode) {
  429. case Regs::ConditionMode::Always: {
  430. execute_on = true;
  431. break;
  432. }
  433. case Regs::ConditionMode::Never: {
  434. execute_on = false;
  435. break;
  436. }
  437. case Regs::ConditionMode::ResNonZero: {
  438. Regs::QueryCompare cmp;
  439. memory_manager.ReadBlockUnsafe(condition_address, &cmp, sizeof(cmp));
  440. execute_on = cmp.initial_sequence != 0U && cmp.initial_mode != 0U;
  441. break;
  442. }
  443. case Regs::ConditionMode::Equal: {
  444. Regs::QueryCompare cmp;
  445. memory_manager.ReadBlockUnsafe(condition_address, &cmp, sizeof(cmp));
  446. execute_on =
  447. cmp.initial_sequence == cmp.current_sequence && cmp.initial_mode == cmp.current_mode;
  448. break;
  449. }
  450. case Regs::ConditionMode::NotEqual: {
  451. Regs::QueryCompare cmp;
  452. memory_manager.ReadBlockUnsafe(condition_address, &cmp, sizeof(cmp));
  453. execute_on =
  454. cmp.initial_sequence != cmp.current_sequence || cmp.initial_mode != cmp.current_mode;
  455. break;
  456. }
  457. default: {
  458. UNIMPLEMENTED_MSG("Uninplemented Condition Mode!");
  459. execute_on = true;
  460. break;
  461. }
  462. }
  463. }
  464. void Maxwell3D::ProcessSyncPoint() {
  465. const u32 sync_point = regs.sync_info.sync_point.Value();
  466. const u32 increment = regs.sync_info.increment.Value();
  467. [[maybe_unused]] const u32 cache_flush = regs.sync_info.unknown.Value();
  468. if (increment) {
  469. system.GPU().IncrementSyncPoint(sync_point);
  470. }
  471. }
  472. void Maxwell3D::DrawArrays() {
  473. LOG_DEBUG(HW_GPU, "called, topology={}, count={}", static_cast<u32>(regs.draw.topology.Value()),
  474. regs.vertex_buffer.count);
  475. ASSERT_MSG(!(regs.index_array.count && regs.vertex_buffer.count), "Both indexed and direct?");
  476. auto debug_context = system.GetGPUDebugContext();
  477. if (debug_context) {
  478. debug_context->OnEvent(Tegra::DebugContext::Event::IncomingPrimitiveBatch, nullptr);
  479. }
  480. // Both instance configuration registers can not be set at the same time.
  481. ASSERT_MSG(!regs.draw.instance_next || !regs.draw.instance_cont,
  482. "Illegal combination of instancing parameters");
  483. if (regs.draw.instance_next) {
  484. // Increment the current instance *before* drawing.
  485. state.current_instance += 1;
  486. } else if (!regs.draw.instance_cont) {
  487. // Reset the current instance to 0.
  488. state.current_instance = 0;
  489. }
  490. const bool is_indexed{regs.index_array.count && !regs.vertex_buffer.count};
  491. rasterizer.AccelerateDrawBatch(is_indexed);
  492. if (debug_context) {
  493. debug_context->OnEvent(Tegra::DebugContext::Event::FinishedPrimitiveBatch, nullptr);
  494. }
  495. // TODO(bunnei): Below, we reset vertex count so that we can use these registers to determine if
  496. // the game is trying to draw indexed or direct mode. This needs to be verified on HW still -
  497. // it's possible that it is incorrect and that there is some other register used to specify the
  498. // drawing mode.
  499. if (is_indexed) {
  500. regs.index_array.count = 0;
  501. } else {
  502. regs.vertex_buffer.count = 0;
  503. }
  504. }
  505. void Maxwell3D::ProcessCBBind(Regs::ShaderStage stage) {
  506. // Bind the buffer currently in CB_ADDRESS to the specified index in the desired shader stage.
  507. auto& shader = state.shader_stages[static_cast<std::size_t>(stage)];
  508. auto& bind_data = regs.cb_bind[static_cast<std::size_t>(stage)];
  509. ASSERT(bind_data.index < Regs::MaxConstBuffers);
  510. auto& buffer = shader.const_buffers[bind_data.index];
  511. buffer.enabled = bind_data.valid.Value() != 0;
  512. buffer.address = regs.const_buffer.BufferAddress();
  513. buffer.size = regs.const_buffer.cb_size;
  514. }
  515. void Maxwell3D::ProcessCBData(u32 value) {
  516. const u32 id = cb_data_state.id;
  517. cb_data_state.buffer[id][cb_data_state.counter] = value;
  518. // Increment the current buffer position.
  519. regs.const_buffer.cb_pos = regs.const_buffer.cb_pos + 4;
  520. cb_data_state.counter++;
  521. }
  522. void Maxwell3D::StartCBData(u32 method) {
  523. constexpr u32 first_cb_data = MAXWELL3D_REG_INDEX(const_buffer.cb_data[0]);
  524. cb_data_state.start_pos = regs.const_buffer.cb_pos;
  525. cb_data_state.id = method - first_cb_data;
  526. cb_data_state.current = method;
  527. cb_data_state.counter = 0;
  528. ProcessCBData(regs.const_buffer.cb_data[cb_data_state.id]);
  529. }
  530. void Maxwell3D::FinishCBData() {
  531. // Write the input value to the current const buffer at the current position.
  532. const GPUVAddr buffer_address = regs.const_buffer.BufferAddress();
  533. ASSERT(buffer_address != 0);
  534. // Don't allow writing past the end of the buffer.
  535. ASSERT(regs.const_buffer.cb_pos <= regs.const_buffer.cb_size);
  536. const GPUVAddr address{buffer_address + cb_data_state.start_pos};
  537. const std::size_t size = regs.const_buffer.cb_pos - cb_data_state.start_pos;
  538. const u32 id = cb_data_state.id;
  539. memory_manager.WriteBlock(address, cb_data_state.buffer[id].data(), size);
  540. dirty.OnMemoryWrite();
  541. cb_data_state.id = null_cb_data;
  542. cb_data_state.current = null_cb_data;
  543. }
  544. Texture::TICEntry Maxwell3D::GetTICEntry(u32 tic_index) const {
  545. const GPUVAddr tic_address_gpu{regs.tic.TICAddress() + tic_index * sizeof(Texture::TICEntry)};
  546. Texture::TICEntry tic_entry;
  547. memory_manager.ReadBlockUnsafe(tic_address_gpu, &tic_entry, sizeof(Texture::TICEntry));
  548. [[maybe_unused]] const auto r_type{tic_entry.r_type.Value()};
  549. [[maybe_unused]] const auto g_type{tic_entry.g_type.Value()};
  550. [[maybe_unused]] const auto b_type{tic_entry.b_type.Value()};
  551. [[maybe_unused]] const auto a_type{tic_entry.a_type.Value()};
  552. // TODO(Subv): Different data types for separate components are not supported
  553. DEBUG_ASSERT(r_type == g_type && r_type == b_type && r_type == a_type);
  554. return tic_entry;
  555. }
  556. Texture::TSCEntry Maxwell3D::GetTSCEntry(u32 tsc_index) const {
  557. const GPUVAddr tsc_address_gpu{regs.tsc.TSCAddress() + tsc_index * sizeof(Texture::TSCEntry)};
  558. Texture::TSCEntry tsc_entry;
  559. memory_manager.ReadBlockUnsafe(tsc_address_gpu, &tsc_entry, sizeof(Texture::TSCEntry));
  560. return tsc_entry;
  561. }
  562. std::vector<Texture::FullTextureInfo> Maxwell3D::GetStageTextures(Regs::ShaderStage stage) const {
  563. std::vector<Texture::FullTextureInfo> textures;
  564. auto& fragment_shader = state.shader_stages[static_cast<std::size_t>(stage)];
  565. auto& tex_info_buffer = fragment_shader.const_buffers[regs.tex_cb_index];
  566. ASSERT(tex_info_buffer.enabled && tex_info_buffer.address != 0);
  567. GPUVAddr tex_info_buffer_end = tex_info_buffer.address + tex_info_buffer.size;
  568. // Offset into the texture constbuffer where the texture info begins.
  569. static constexpr std::size_t TextureInfoOffset = 0x20;
  570. for (GPUVAddr current_texture = tex_info_buffer.address + TextureInfoOffset;
  571. current_texture < tex_info_buffer_end; current_texture += sizeof(Texture::TextureHandle)) {
  572. const Texture::TextureHandle tex_handle{memory_manager.Read<u32>(current_texture)};
  573. Texture::FullTextureInfo tex_info{};
  574. // TODO(Subv): Use the shader to determine which textures are actually accessed.
  575. tex_info.index =
  576. static_cast<u32>(current_texture - tex_info_buffer.address - TextureInfoOffset) /
  577. sizeof(Texture::TextureHandle);
  578. // Load the TIC data.
  579. auto tic_entry = GetTICEntry(tex_handle.tic_id);
  580. // TODO(Subv): Workaround for BitField's move constructor being deleted.
  581. std::memcpy(&tex_info.tic, &tic_entry, sizeof(tic_entry));
  582. // Load the TSC data
  583. auto tsc_entry = GetTSCEntry(tex_handle.tsc_id);
  584. // TODO(Subv): Workaround for BitField's move constructor being deleted.
  585. std::memcpy(&tex_info.tsc, &tsc_entry, sizeof(tsc_entry));
  586. textures.push_back(tex_info);
  587. }
  588. return textures;
  589. }
  590. Texture::FullTextureInfo Maxwell3D::GetTextureInfo(const Texture::TextureHandle tex_handle,
  591. std::size_t offset) const {
  592. Texture::FullTextureInfo tex_info{};
  593. tex_info.index = static_cast<u32>(offset);
  594. // Load the TIC data.
  595. auto tic_entry = GetTICEntry(tex_handle.tic_id);
  596. // TODO(Subv): Workaround for BitField's move constructor being deleted.
  597. std::memcpy(&tex_info.tic, &tic_entry, sizeof(tic_entry));
  598. // Load the TSC data
  599. auto tsc_entry = GetTSCEntry(tex_handle.tsc_id);
  600. // TODO(Subv): Workaround for BitField's move constructor being deleted.
  601. std::memcpy(&tex_info.tsc, &tsc_entry, sizeof(tsc_entry));
  602. return tex_info;
  603. }
  604. Texture::FullTextureInfo Maxwell3D::GetStageTexture(Regs::ShaderStage stage,
  605. std::size_t offset) const {
  606. const auto& shader = state.shader_stages[static_cast<std::size_t>(stage)];
  607. const auto& tex_info_buffer = shader.const_buffers[regs.tex_cb_index];
  608. ASSERT(tex_info_buffer.enabled && tex_info_buffer.address != 0);
  609. const GPUVAddr tex_info_address =
  610. tex_info_buffer.address + offset * sizeof(Texture::TextureHandle);
  611. ASSERT(tex_info_address < tex_info_buffer.address + tex_info_buffer.size);
  612. const Texture::TextureHandle tex_handle{memory_manager.Read<u32>(tex_info_address)};
  613. return GetTextureInfo(tex_handle, offset);
  614. }
  615. u32 Maxwell3D::GetRegisterValue(u32 method) const {
  616. ASSERT_MSG(method < Regs::NUM_REGS, "Invalid Maxwell3D register");
  617. return regs.reg_array[method];
  618. }
  619. void Maxwell3D::ProcessClearBuffers() {
  620. ASSERT(regs.clear_buffers.R == regs.clear_buffers.G &&
  621. regs.clear_buffers.R == regs.clear_buffers.B &&
  622. regs.clear_buffers.R == regs.clear_buffers.A);
  623. rasterizer.Clear();
  624. }
  625. u32 Maxwell3D::AccessConstBuffer32(Regs::ShaderStage stage, u64 const_buffer, u64 offset) const {
  626. const auto& shader_stage = state.shader_stages[static_cast<std::size_t>(stage)];
  627. const auto& buffer = shader_stage.const_buffers[const_buffer];
  628. u32 result;
  629. std::memcpy(&result, memory_manager.GetPointer(buffer.address + offset), sizeof(u32));
  630. return result;
  631. }
  632. } // namespace Tegra::Engines