maxwell_3d.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cinttypes>
  5. #include <cstring>
  6. #include "common/assert.h"
  7. #include "core/core.h"
  8. #include "core/core_timing.h"
  9. #include "video_core/debug_utils/debug_utils.h"
  10. #include "video_core/engines/maxwell_3d.h"
  11. #include "video_core/memory_manager.h"
  12. #include "video_core/rasterizer_interface.h"
  13. #include "video_core/textures/texture.h"
  14. namespace Tegra::Engines {
  15. /// First register id that is actually a Macro call.
  16. constexpr u32 MacroRegistersStart = 0xE00;
  17. Maxwell3D::Maxwell3D(Core::System& system, VideoCore::RasterizerInterface& rasterizer,
  18. MemoryManager& memory_manager)
  19. : system{system}, rasterizer{rasterizer}, memory_manager{memory_manager},
  20. macro_interpreter{*this}, upload_state{memory_manager, regs.upload} {
  21. InitDirtySettings();
  22. InitializeRegisterDefaults();
  23. }
  24. void Maxwell3D::InitializeRegisterDefaults() {
  25. // Initializes registers to their default values - what games expect them to be at boot. This is
  26. // for certain registers that may not be explicitly set by games.
  27. // Reset all registers to zero
  28. std::memset(&regs, 0, sizeof(regs));
  29. // Depth range near/far is not always set, but is expected to be the default 0.0f, 1.0f. This is
  30. // needed for ARMS.
  31. for (auto& viewport : regs.viewports) {
  32. viewport.depth_range_near = 0.0f;
  33. viewport.depth_range_far = 1.0f;
  34. }
  35. // Doom and Bomberman seems to use the uninitialized registers and just enable blend
  36. // so initialize blend registers with sane values
  37. regs.blend.equation_rgb = Regs::Blend::Equation::Add;
  38. regs.blend.factor_source_rgb = Regs::Blend::Factor::One;
  39. regs.blend.factor_dest_rgb = Regs::Blend::Factor::Zero;
  40. regs.blend.equation_a = Regs::Blend::Equation::Add;
  41. regs.blend.factor_source_a = Regs::Blend::Factor::One;
  42. regs.blend.factor_dest_a = Regs::Blend::Factor::Zero;
  43. for (auto& blend : regs.independent_blend) {
  44. blend.equation_rgb = Regs::Blend::Equation::Add;
  45. blend.factor_source_rgb = Regs::Blend::Factor::One;
  46. blend.factor_dest_rgb = Regs::Blend::Factor::Zero;
  47. blend.equation_a = Regs::Blend::Equation::Add;
  48. blend.factor_source_a = Regs::Blend::Factor::One;
  49. blend.factor_dest_a = Regs::Blend::Factor::Zero;
  50. }
  51. regs.stencil_front_op_fail = Regs::StencilOp::Keep;
  52. regs.stencil_front_op_zfail = Regs::StencilOp::Keep;
  53. regs.stencil_front_op_zpass = Regs::StencilOp::Keep;
  54. regs.stencil_front_func_func = Regs::ComparisonOp::Always;
  55. regs.stencil_front_func_mask = 0xFFFFFFFF;
  56. regs.stencil_front_mask = 0xFFFFFFFF;
  57. regs.stencil_two_side_enable = 1;
  58. regs.stencil_back_op_fail = Regs::StencilOp::Keep;
  59. regs.stencil_back_op_zfail = Regs::StencilOp::Keep;
  60. regs.stencil_back_op_zpass = Regs::StencilOp::Keep;
  61. regs.stencil_back_func_func = Regs::ComparisonOp::Always;
  62. regs.stencil_back_func_mask = 0xFFFFFFFF;
  63. regs.stencil_back_mask = 0xFFFFFFFF;
  64. regs.depth_test_func = Regs::ComparisonOp::Always;
  65. regs.cull.front_face = Regs::Cull::FrontFace::CounterClockWise;
  66. regs.cull.cull_face = Regs::Cull::CullFace::Back;
  67. // TODO(Rodrigo): Most games do not set a point size. I think this is a case of a
  68. // register carrying a default value. Assume it's OpenGL's default (1).
  69. regs.point_size = 1.0f;
  70. // TODO(bunnei): Some games do not initialize the color masks (e.g. Sonic Mania). Assuming a
  71. // default of enabled fixes rendering here.
  72. for (auto& color_mask : regs.color_mask) {
  73. color_mask.R.Assign(1);
  74. color_mask.G.Assign(1);
  75. color_mask.B.Assign(1);
  76. color_mask.A.Assign(1);
  77. }
  78. // Commercial games seem to assume this value is enabled and nouveau sets this value manually.
  79. regs.rt_separate_frag_data = 1;
  80. // Some games (like Super Mario Odyssey) assume that SRGB is enabled.
  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) (offsetof(Maxwell3D::DirtyRegs, field_name))
  88. void Maxwell3D::InitDirtySettings() {
  89. const auto set_block = [this](const std::size_t start, const std::size_t range,
  90. const 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. u32 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. u32 va_reg = DIRTY_REGS_POS(vertex_array);
  117. u32 vi_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_reg);
  121. // The divisor concerns vertex array instances
  122. dirty_pointers[vertex_reg + 3] = vi_reg;
  123. va_reg++;
  124. vi_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_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_reg);
  133. va_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_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_reg);
  144. vi_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 u32 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 u32 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 u32 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 u32 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 u32 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 u32 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 u32 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 = ((method - MacroRegistersStart) >> 1) % 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. auto debug_context = system.GetGPUDebugContext();
  242. const u32 method = method_call.method;
  243. if (method == cb_data_state.current) {
  244. regs.reg_array[method] = method_call.argument;
  245. ProcessCBData(method_call.argument);
  246. return;
  247. } else if (cb_data_state.current != null_cb_data) {
  248. FinishCBData();
  249. }
  250. // It is an error to write to a register other than the current macro's ARG register before it
  251. // has finished execution.
  252. if (executing_macro != 0) {
  253. ASSERT(method == executing_macro + 1);
  254. }
  255. // Methods after 0xE00 are special, they're actually triggers for some microcode that was
  256. // uploaded to the GPU during initialization.
  257. if (method >= MacroRegistersStart) {
  258. // We're trying to execute a macro
  259. if (executing_macro == 0) {
  260. // A macro call must begin by writing the macro method's register, not its argument.
  261. ASSERT_MSG((method % 2) == 0,
  262. "Can't start macro execution by writing to the ARGS register");
  263. executing_macro = method;
  264. }
  265. macro_params.push_back(method_call.argument);
  266. // Call the macro when there are no more parameters in the command buffer
  267. if (method_call.IsLastCall()) {
  268. CallMacroMethod(executing_macro, macro_params.size(), macro_params.data());
  269. macro_params.clear();
  270. }
  271. return;
  272. }
  273. ASSERT_MSG(method < Regs::NUM_REGS,
  274. "Invalid Maxwell3D register, increase the size of the Regs structure");
  275. if (debug_context) {
  276. debug_context->OnEvent(Tegra::DebugContext::Event::MaxwellCommandLoaded, nullptr);
  277. }
  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].raw_config): {
  328. ProcessCBBind(Regs::ShaderStage::Vertex);
  329. break;
  330. }
  331. case MAXWELL3D_REG_INDEX(cb_bind[1].raw_config): {
  332. ProcessCBBind(Regs::ShaderStage::TesselationControl);
  333. break;
  334. }
  335. case MAXWELL3D_REG_INDEX(cb_bind[2].raw_config): {
  336. ProcessCBBind(Regs::ShaderStage::TesselationEval);
  337. break;
  338. }
  339. case MAXWELL3D_REG_INDEX(cb_bind[3].raw_config): {
  340. ProcessCBBind(Regs::ShaderStage::Geometry);
  341. break;
  342. }
  343. case MAXWELL3D_REG_INDEX(cb_bind[4].raw_config): {
  344. ProcessCBBind(Regs::ShaderStage::Fragment);
  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(sync_info): {
  364. ProcessSyncPoint();
  365. break;
  366. }
  367. case MAXWELL3D_REG_INDEX(exec_upload): {
  368. upload_state.ProcessExec(regs.exec_upload.linear != 0);
  369. break;
  370. }
  371. case MAXWELL3D_REG_INDEX(data_upload): {
  372. const bool is_last_call = method_call.IsLastCall();
  373. upload_state.ProcessData(method_call.argument, is_last_call);
  374. if (is_last_call) {
  375. dirty.OnMemoryWrite();
  376. }
  377. break;
  378. }
  379. default:
  380. break;
  381. }
  382. if (debug_context) {
  383. debug_context->OnEvent(Tegra::DebugContext::Event::MaxwellCommandProcessed, nullptr);
  384. }
  385. }
  386. void Maxwell3D::StepInstance(const MMEDrawMode expected_mode, const u32 count) {
  387. if (mme_draw.current_mode == MMEDrawMode::Undefined) {
  388. if (mme_draw.gl_begin_consume) {
  389. mme_draw.current_mode = expected_mode;
  390. mme_draw.current_count = count;
  391. mme_draw.instance_count = 1;
  392. mme_draw.gl_begin_consume = false;
  393. mme_draw.gl_end_count = 0;
  394. }
  395. return;
  396. } else {
  397. if (mme_draw.current_mode == expected_mode && count == mme_draw.current_count &&
  398. mme_draw.instance_mode && mme_draw.gl_begin_consume) {
  399. mme_draw.instance_count++;
  400. mme_draw.gl_begin_consume = false;
  401. return;
  402. } else {
  403. FlushMMEInlineDraw();
  404. }
  405. }
  406. // Tail call in case it needs to retry.
  407. StepInstance(expected_mode, count);
  408. }
  409. void Maxwell3D::CallMethodFromMME(const GPU::MethodCall& method_call) {
  410. const u32 method = method_call.method;
  411. if (mme_inline[method]) {
  412. regs.reg_array[method] = method_call.argument;
  413. if (method == MAXWELL3D_REG_INDEX(vertex_buffer.count) ||
  414. method == MAXWELL3D_REG_INDEX(index_array.count)) {
  415. const MMEDrawMode expected_mode = method == MAXWELL3D_REG_INDEX(vertex_buffer.count)
  416. ? MMEDrawMode::Array
  417. : MMEDrawMode::Indexed;
  418. StepInstance(expected_mode, method_call.argument);
  419. } else if (method == MAXWELL3D_REG_INDEX(draw.vertex_begin_gl)) {
  420. mme_draw.instance_mode =
  421. (regs.draw.instance_next != 0) || (regs.draw.instance_cont != 0);
  422. mme_draw.gl_begin_consume = true;
  423. } else {
  424. mme_draw.gl_end_count++;
  425. }
  426. } else {
  427. if (mme_draw.current_mode != MMEDrawMode::Undefined) {
  428. FlushMMEInlineDraw();
  429. }
  430. CallMethod(method_call);
  431. }
  432. }
  433. void Maxwell3D::FlushMMEInlineDraw() {
  434. LOG_TRACE(HW_GPU, "called, topology={}, count={}", static_cast<u32>(regs.draw.topology.Value()),
  435. regs.vertex_buffer.count);
  436. ASSERT_MSG(!(regs.index_array.count && regs.vertex_buffer.count), "Both indexed and direct?");
  437. ASSERT(mme_draw.instance_count == mme_draw.gl_end_count);
  438. auto debug_context = system.GetGPUDebugContext();
  439. if (debug_context) {
  440. debug_context->OnEvent(Tegra::DebugContext::Event::IncomingPrimitiveBatch, nullptr);
  441. }
  442. // Both instance configuration registers can not be set at the same time.
  443. ASSERT_MSG(!regs.draw.instance_next || !regs.draw.instance_cont,
  444. "Illegal combination of instancing parameters");
  445. const bool is_indexed = mme_draw.current_mode == MMEDrawMode::Indexed;
  446. if (ShouldExecute()) {
  447. rasterizer.DrawMultiBatch(is_indexed);
  448. }
  449. if (debug_context) {
  450. debug_context->OnEvent(Tegra::DebugContext::Event::FinishedPrimitiveBatch, nullptr);
  451. }
  452. // TODO(bunnei): Below, we reset vertex count so that we can use these registers to determine if
  453. // the game is trying to draw indexed or direct mode. This needs to be verified on HW still -
  454. // it's possible that it is incorrect and that there is some other register used to specify the
  455. // drawing mode.
  456. if (is_indexed) {
  457. regs.index_array.count = 0;
  458. } else {
  459. regs.vertex_buffer.count = 0;
  460. }
  461. mme_draw.current_mode = MMEDrawMode::Undefined;
  462. mme_draw.current_count = 0;
  463. mme_draw.instance_count = 0;
  464. mme_draw.instance_mode = false;
  465. mme_draw.gl_begin_consume = false;
  466. mme_draw.gl_end_count = 0;
  467. }
  468. void Maxwell3D::ProcessMacroUpload(u32 data) {
  469. ASSERT_MSG(regs.macros.upload_address < macro_memory.size(),
  470. "upload_address exceeded macro_memory size!");
  471. macro_memory[regs.macros.upload_address++] = data;
  472. }
  473. void Maxwell3D::ProcessMacroBind(u32 data) {
  474. macro_positions[regs.macros.entry++] = data;
  475. }
  476. void Maxwell3D::ProcessFirmwareCall4() {
  477. LOG_WARNING(HW_GPU, "(STUBBED) called");
  478. // Firmware call 4 is a blob that changes some registers depending on its parameters.
  479. // These registers don't affect emulation and so are stubbed by setting 0xd00 to 1.
  480. regs.reg_array[0xd00] = 1;
  481. }
  482. void Maxwell3D::ProcessQueryGet() {
  483. const GPUVAddr sequence_address{regs.query.QueryAddress()};
  484. // Since the sequence address is given as a GPU VAddr, we have to convert it to an application
  485. // VAddr before writing.
  486. // TODO(Subv): Support the other query units.
  487. ASSERT_MSG(regs.query.query_get.unit == Regs::QueryUnit::Crop,
  488. "Units other than CROP are unimplemented");
  489. u64 result = 0;
  490. // TODO(Subv): Support the other query variables
  491. switch (regs.query.query_get.select) {
  492. case Regs::QuerySelect::Zero:
  493. // This seems to actually write the query sequence to the query address.
  494. result = regs.query.query_sequence;
  495. break;
  496. default:
  497. result = 1;
  498. UNIMPLEMENTED_MSG("Unimplemented query select type {}",
  499. static_cast<u32>(regs.query.query_get.select.Value()));
  500. }
  501. // TODO(Subv): Research and implement how query sync conditions work.
  502. struct LongQueryResult {
  503. u64_le value;
  504. u64_le timestamp;
  505. };
  506. static_assert(sizeof(LongQueryResult) == 16, "LongQueryResult has wrong size");
  507. switch (regs.query.query_get.mode) {
  508. case Regs::QueryMode::Write:
  509. case Regs::QueryMode::Write2: {
  510. u32 sequence = regs.query.query_sequence;
  511. if (regs.query.query_get.short_query) {
  512. // Write the current query sequence to the sequence address.
  513. // TODO(Subv): Find out what happens if you use a long query type but mark it as a short
  514. // query.
  515. memory_manager.Write<u32>(sequence_address, sequence);
  516. } else {
  517. // Write the 128-bit result structure in long mode. Note: We emulate an infinitely fast
  518. // GPU, this command may actually take a while to complete in real hardware due to GPU
  519. // wait queues.
  520. LongQueryResult query_result{};
  521. query_result.value = result;
  522. // TODO(Subv): Generate a real GPU timestamp and write it here instead of CoreTiming
  523. query_result.timestamp = system.CoreTiming().GetTicks();
  524. memory_manager.WriteBlock(sequence_address, &query_result, sizeof(query_result));
  525. }
  526. break;
  527. }
  528. default:
  529. UNIMPLEMENTED_MSG("Query mode {} not implemented",
  530. static_cast<u32>(regs.query.query_get.mode.Value()));
  531. }
  532. }
  533. void Maxwell3D::ProcessQueryCondition() {
  534. const GPUVAddr condition_address{regs.condition.Address()};
  535. switch (regs.condition.mode) {
  536. case Regs::ConditionMode::Always: {
  537. execute_on = true;
  538. break;
  539. }
  540. case Regs::ConditionMode::Never: {
  541. execute_on = false;
  542. break;
  543. }
  544. case Regs::ConditionMode::ResNonZero: {
  545. Regs::QueryCompare cmp;
  546. memory_manager.ReadBlockUnsafe(condition_address, &cmp, sizeof(cmp));
  547. execute_on = cmp.initial_sequence != 0U && cmp.initial_mode != 0U;
  548. break;
  549. }
  550. case Regs::ConditionMode::Equal: {
  551. Regs::QueryCompare cmp;
  552. memory_manager.ReadBlockUnsafe(condition_address, &cmp, sizeof(cmp));
  553. execute_on =
  554. cmp.initial_sequence == cmp.current_sequence && cmp.initial_mode == cmp.current_mode;
  555. break;
  556. }
  557. case Regs::ConditionMode::NotEqual: {
  558. Regs::QueryCompare cmp;
  559. memory_manager.ReadBlockUnsafe(condition_address, &cmp, sizeof(cmp));
  560. execute_on =
  561. cmp.initial_sequence != cmp.current_sequence || cmp.initial_mode != cmp.current_mode;
  562. break;
  563. }
  564. default: {
  565. UNIMPLEMENTED_MSG("Uninplemented Condition Mode!");
  566. execute_on = true;
  567. break;
  568. }
  569. }
  570. }
  571. void Maxwell3D::ProcessSyncPoint() {
  572. const u32 sync_point = regs.sync_info.sync_point.Value();
  573. const u32 increment = regs.sync_info.increment.Value();
  574. [[maybe_unused]] const u32 cache_flush = regs.sync_info.unknown.Value();
  575. if (increment) {
  576. system.GPU().IncrementSyncPoint(sync_point);
  577. }
  578. }
  579. void Maxwell3D::DrawArrays() {
  580. LOG_TRACE(HW_GPU, "called, topology={}, count={}", static_cast<u32>(regs.draw.topology.Value()),
  581. regs.vertex_buffer.count);
  582. ASSERT_MSG(!(regs.index_array.count && regs.vertex_buffer.count), "Both indexed and direct?");
  583. auto debug_context = system.GetGPUDebugContext();
  584. if (debug_context) {
  585. debug_context->OnEvent(Tegra::DebugContext::Event::IncomingPrimitiveBatch, nullptr);
  586. }
  587. // Both instance configuration registers can not be set at the same time.
  588. ASSERT_MSG(!regs.draw.instance_next || !regs.draw.instance_cont,
  589. "Illegal combination of instancing parameters");
  590. if (regs.draw.instance_next) {
  591. // Increment the current instance *before* drawing.
  592. state.current_instance += 1;
  593. } else if (!regs.draw.instance_cont) {
  594. // Reset the current instance to 0.
  595. state.current_instance = 0;
  596. }
  597. const bool is_indexed{regs.index_array.count && !regs.vertex_buffer.count};
  598. if (ShouldExecute()) {
  599. rasterizer.DrawBatch(is_indexed);
  600. }
  601. if (debug_context) {
  602. debug_context->OnEvent(Tegra::DebugContext::Event::FinishedPrimitiveBatch, nullptr);
  603. }
  604. // TODO(bunnei): Below, we reset vertex count so that we can use these registers to determine if
  605. // the game is trying to draw indexed or direct mode. This needs to be verified on HW still -
  606. // it's possible that it is incorrect and that there is some other register used to specify the
  607. // drawing mode.
  608. if (is_indexed) {
  609. regs.index_array.count = 0;
  610. } else {
  611. regs.vertex_buffer.count = 0;
  612. }
  613. }
  614. void Maxwell3D::ProcessCBBind(Regs::ShaderStage stage) {
  615. // Bind the buffer currently in CB_ADDRESS to the specified index in the desired shader stage.
  616. auto& shader = state.shader_stages[static_cast<std::size_t>(stage)];
  617. auto& bind_data = regs.cb_bind[static_cast<std::size_t>(stage)];
  618. ASSERT(bind_data.index < Regs::MaxConstBuffers);
  619. auto& buffer = shader.const_buffers[bind_data.index];
  620. buffer.enabled = bind_data.valid.Value() != 0;
  621. buffer.address = regs.const_buffer.BufferAddress();
  622. buffer.size = regs.const_buffer.cb_size;
  623. }
  624. void Maxwell3D::ProcessCBData(u32 value) {
  625. const u32 id = cb_data_state.id;
  626. cb_data_state.buffer[id][cb_data_state.counter] = value;
  627. // Increment the current buffer position.
  628. regs.const_buffer.cb_pos = regs.const_buffer.cb_pos + 4;
  629. cb_data_state.counter++;
  630. }
  631. void Maxwell3D::StartCBData(u32 method) {
  632. constexpr u32 first_cb_data = MAXWELL3D_REG_INDEX(const_buffer.cb_data[0]);
  633. cb_data_state.start_pos = regs.const_buffer.cb_pos;
  634. cb_data_state.id = method - first_cb_data;
  635. cb_data_state.current = method;
  636. cb_data_state.counter = 0;
  637. ProcessCBData(regs.const_buffer.cb_data[cb_data_state.id]);
  638. }
  639. void Maxwell3D::FinishCBData() {
  640. // Write the input value to the current const buffer at the current position.
  641. const GPUVAddr buffer_address = regs.const_buffer.BufferAddress();
  642. ASSERT(buffer_address != 0);
  643. // Don't allow writing past the end of the buffer.
  644. ASSERT(regs.const_buffer.cb_pos <= regs.const_buffer.cb_size);
  645. const GPUVAddr address{buffer_address + cb_data_state.start_pos};
  646. const std::size_t size = regs.const_buffer.cb_pos - cb_data_state.start_pos;
  647. const u32 id = cb_data_state.id;
  648. memory_manager.WriteBlock(address, cb_data_state.buffer[id].data(), size);
  649. dirty.OnMemoryWrite();
  650. cb_data_state.id = null_cb_data;
  651. cb_data_state.current = null_cb_data;
  652. }
  653. Texture::TICEntry Maxwell3D::GetTICEntry(u32 tic_index) const {
  654. const GPUVAddr tic_address_gpu{regs.tic.TICAddress() + tic_index * sizeof(Texture::TICEntry)};
  655. Texture::TICEntry tic_entry;
  656. memory_manager.ReadBlockUnsafe(tic_address_gpu, &tic_entry, sizeof(Texture::TICEntry));
  657. [[maybe_unused]] const auto r_type{tic_entry.r_type.Value()};
  658. [[maybe_unused]] const auto g_type{tic_entry.g_type.Value()};
  659. [[maybe_unused]] const auto b_type{tic_entry.b_type.Value()};
  660. [[maybe_unused]] const auto a_type{tic_entry.a_type.Value()};
  661. // TODO(Subv): Different data types for separate components are not supported
  662. DEBUG_ASSERT(r_type == g_type && r_type == b_type && r_type == a_type);
  663. return tic_entry;
  664. }
  665. Texture::TSCEntry Maxwell3D::GetTSCEntry(u32 tsc_index) const {
  666. const GPUVAddr tsc_address_gpu{regs.tsc.TSCAddress() + tsc_index * sizeof(Texture::TSCEntry)};
  667. Texture::TSCEntry tsc_entry;
  668. memory_manager.ReadBlockUnsafe(tsc_address_gpu, &tsc_entry, sizeof(Texture::TSCEntry));
  669. return tsc_entry;
  670. }
  671. std::vector<Texture::FullTextureInfo> Maxwell3D::GetStageTextures(Regs::ShaderStage stage) const {
  672. std::vector<Texture::FullTextureInfo> textures;
  673. auto& fragment_shader = state.shader_stages[static_cast<std::size_t>(stage)];
  674. auto& tex_info_buffer = fragment_shader.const_buffers[regs.tex_cb_index];
  675. ASSERT(tex_info_buffer.enabled && tex_info_buffer.address != 0);
  676. GPUVAddr tex_info_buffer_end = tex_info_buffer.address + tex_info_buffer.size;
  677. // Offset into the texture constbuffer where the texture info begins.
  678. static constexpr std::size_t TextureInfoOffset = 0x20;
  679. for (GPUVAddr current_texture = tex_info_buffer.address + TextureInfoOffset;
  680. current_texture < tex_info_buffer_end; current_texture += sizeof(Texture::TextureHandle)) {
  681. const Texture::TextureHandle tex_handle{memory_manager.Read<u32>(current_texture)};
  682. Texture::FullTextureInfo tex_info{};
  683. // TODO(Subv): Use the shader to determine which textures are actually accessed.
  684. tex_info.index =
  685. static_cast<u32>(current_texture - tex_info_buffer.address - TextureInfoOffset) /
  686. sizeof(Texture::TextureHandle);
  687. // Load the TIC data.
  688. auto tic_entry = GetTICEntry(tex_handle.tic_id);
  689. // TODO(Subv): Workaround for BitField's move constructor being deleted.
  690. std::memcpy(&tex_info.tic, &tic_entry, sizeof(tic_entry));
  691. // Load the TSC data
  692. auto tsc_entry = GetTSCEntry(tex_handle.tsc_id);
  693. // TODO(Subv): Workaround for BitField's move constructor being deleted.
  694. std::memcpy(&tex_info.tsc, &tsc_entry, sizeof(tsc_entry));
  695. textures.push_back(tex_info);
  696. }
  697. return textures;
  698. }
  699. Texture::FullTextureInfo Maxwell3D::GetTextureInfo(const Texture::TextureHandle tex_handle,
  700. std::size_t offset) const {
  701. Texture::FullTextureInfo tex_info{};
  702. tex_info.index = static_cast<u32>(offset);
  703. // Load the TIC data.
  704. auto tic_entry = GetTICEntry(tex_handle.tic_id);
  705. // TODO(Subv): Workaround for BitField's move constructor being deleted.
  706. std::memcpy(&tex_info.tic, &tic_entry, sizeof(tic_entry));
  707. // Load the TSC data
  708. auto tsc_entry = GetTSCEntry(tex_handle.tsc_id);
  709. // TODO(Subv): Workaround for BitField's move constructor being deleted.
  710. std::memcpy(&tex_info.tsc, &tsc_entry, sizeof(tsc_entry));
  711. return tex_info;
  712. }
  713. Texture::FullTextureInfo Maxwell3D::GetStageTexture(Regs::ShaderStage stage,
  714. std::size_t offset) const {
  715. const auto& shader = state.shader_stages[static_cast<std::size_t>(stage)];
  716. const auto& tex_info_buffer = shader.const_buffers[regs.tex_cb_index];
  717. ASSERT(tex_info_buffer.enabled && tex_info_buffer.address != 0);
  718. const GPUVAddr tex_info_address =
  719. tex_info_buffer.address + offset * sizeof(Texture::TextureHandle);
  720. ASSERT(tex_info_address < tex_info_buffer.address + tex_info_buffer.size);
  721. const Texture::TextureHandle tex_handle{memory_manager.Read<u32>(tex_info_address)};
  722. return GetTextureInfo(tex_handle, offset);
  723. }
  724. u32 Maxwell3D::GetRegisterValue(u32 method) const {
  725. ASSERT_MSG(method < Regs::NUM_REGS, "Invalid Maxwell3D register");
  726. return regs.reg_array[method];
  727. }
  728. void Maxwell3D::ProcessClearBuffers() {
  729. ASSERT(regs.clear_buffers.R == regs.clear_buffers.G &&
  730. regs.clear_buffers.R == regs.clear_buffers.B &&
  731. regs.clear_buffers.R == regs.clear_buffers.A);
  732. rasterizer.Clear();
  733. }
  734. u32 Maxwell3D::AccessConstBuffer32(ShaderType stage, u64 const_buffer, u64 offset) const {
  735. ASSERT(stage != ShaderType::Compute);
  736. const auto& shader_stage = state.shader_stages[static_cast<std::size_t>(stage)];
  737. const auto& buffer = shader_stage.const_buffers[const_buffer];
  738. u32 result;
  739. std::memcpy(&result, memory_manager.GetPointer(buffer.address + offset), sizeof(u32));
  740. return result;
  741. }
  742. SamplerDescriptor Maxwell3D::AccessBoundSampler(ShaderType stage, u64 offset) const {
  743. return AccessBindlessSampler(stage, regs.tex_cb_index, offset * sizeof(Texture::TextureHandle));
  744. }
  745. SamplerDescriptor Maxwell3D::AccessBindlessSampler(ShaderType stage, u64 const_buffer,
  746. u64 offset) const {
  747. ASSERT(stage != ShaderType::Compute);
  748. const auto& shader = state.shader_stages[static_cast<std::size_t>(stage)];
  749. const auto& tex_info_buffer = shader.const_buffers[const_buffer];
  750. const GPUVAddr tex_info_address = tex_info_buffer.address + offset;
  751. const Texture::TextureHandle tex_handle{memory_manager.Read<u32>(tex_info_address)};
  752. const Texture::FullTextureInfo tex_info = GetTextureInfo(tex_handle, offset);
  753. SamplerDescriptor result = SamplerDescriptor::FromTicTexture(tex_info.tic.texture_type.Value());
  754. result.is_shadow.Assign(tex_info.tsc.depth_compare_enabled.Value());
  755. return result;
  756. }
  757. } // namespace Tegra::Engines