maxwell_3d.cpp 33 KB

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