maxwell_3d.cpp 32 KB

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