maxwell_3d.cpp 33 KB

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