maxwell_3d.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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. dirty.flags.flip();
  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. for (auto& viewport : regs.viewport_transform) {
  39. viewport.swizzle.x.Assign(Regs::ViewportSwizzle::PositiveX);
  40. viewport.swizzle.y.Assign(Regs::ViewportSwizzle::PositiveY);
  41. viewport.swizzle.z.Assign(Regs::ViewportSwizzle::PositiveZ);
  42. viewport.swizzle.w.Assign(Regs::ViewportSwizzle::PositiveW);
  43. }
  44. // Doom and Bomberman seems to use the uninitialized registers and just enable blend
  45. // so initialize blend registers with sane values
  46. regs.blend.equation_rgb = Regs::Blend::Equation::Add;
  47. regs.blend.factor_source_rgb = Regs::Blend::Factor::One;
  48. regs.blend.factor_dest_rgb = Regs::Blend::Factor::Zero;
  49. regs.blend.equation_a = Regs::Blend::Equation::Add;
  50. regs.blend.factor_source_a = Regs::Blend::Factor::One;
  51. regs.blend.factor_dest_a = Regs::Blend::Factor::Zero;
  52. for (auto& blend : regs.independent_blend) {
  53. blend.equation_rgb = Regs::Blend::Equation::Add;
  54. blend.factor_source_rgb = Regs::Blend::Factor::One;
  55. blend.factor_dest_rgb = Regs::Blend::Factor::Zero;
  56. blend.equation_a = Regs::Blend::Equation::Add;
  57. blend.factor_source_a = Regs::Blend::Factor::One;
  58. blend.factor_dest_a = Regs::Blend::Factor::Zero;
  59. }
  60. regs.stencil_front_op_fail = Regs::StencilOp::Keep;
  61. regs.stencil_front_op_zfail = Regs::StencilOp::Keep;
  62. regs.stencil_front_op_zpass = Regs::StencilOp::Keep;
  63. regs.stencil_front_func_func = Regs::ComparisonOp::Always;
  64. regs.stencil_front_func_mask = 0xFFFFFFFF;
  65. regs.stencil_front_mask = 0xFFFFFFFF;
  66. regs.stencil_two_side_enable = 1;
  67. regs.stencil_back_op_fail = Regs::StencilOp::Keep;
  68. regs.stencil_back_op_zfail = Regs::StencilOp::Keep;
  69. regs.stencil_back_op_zpass = Regs::StencilOp::Keep;
  70. regs.stencil_back_func_func = Regs::ComparisonOp::Always;
  71. regs.stencil_back_func_mask = 0xFFFFFFFF;
  72. regs.stencil_back_mask = 0xFFFFFFFF;
  73. regs.depth_test_func = Regs::ComparisonOp::Always;
  74. regs.front_face = Regs::FrontFace::CounterClockWise;
  75. regs.cull_face = Regs::CullFace::Back;
  76. // TODO(Rodrigo): Most games do not set a point size. I think this is a case of a
  77. // register carrying a default value. Assume it's OpenGL's default (1).
  78. regs.point_size = 1.0f;
  79. // TODO(bunnei): Some games do not initialize the color masks (e.g. Sonic Mania). Assuming a
  80. // default of enabled fixes rendering here.
  81. for (auto& color_mask : regs.color_mask) {
  82. color_mask.R.Assign(1);
  83. color_mask.G.Assign(1);
  84. color_mask.B.Assign(1);
  85. color_mask.A.Assign(1);
  86. }
  87. for (auto& format : regs.vertex_attrib_format) {
  88. format.constant.Assign(1);
  89. }
  90. // NVN games expect these values to be enabled at boot
  91. regs.rasterize_enable = 1;
  92. regs.rt_separate_frag_data = 1;
  93. regs.framebuffer_srgb = 1;
  94. regs.front_face = Maxwell3D::Regs::FrontFace::ClockWise;
  95. shadow_state = regs;
  96. mme_inline[MAXWELL3D_REG_INDEX(draw.vertex_end_gl)] = true;
  97. mme_inline[MAXWELL3D_REG_INDEX(draw.vertex_begin_gl)] = true;
  98. mme_inline[MAXWELL3D_REG_INDEX(vertex_buffer.count)] = true;
  99. mme_inline[MAXWELL3D_REG_INDEX(index_array.count)] = true;
  100. }
  101. void Maxwell3D::CallMacroMethod(u32 method, std::size_t num_parameters, const u32* parameters) {
  102. // Reset the current macro.
  103. executing_macro = 0;
  104. // Lookup the macro offset
  105. const u32 entry =
  106. ((method - MacroRegistersStart) >> 1) % static_cast<u32>(macro_positions.size());
  107. // Execute the current macro.
  108. macro_interpreter.Execute(macro_positions[entry], num_parameters, parameters);
  109. if (mme_draw.current_mode != MMEDrawMode::Undefined) {
  110. FlushMMEInlineDraw();
  111. }
  112. }
  113. void Maxwell3D::CallMethod(u32 method, u32 method_argument, bool is_last_call) {
  114. if (method == cb_data_state.current) {
  115. regs.reg_array[method] = method_argument;
  116. ProcessCBData(method_argument);
  117. return;
  118. } else if (cb_data_state.current != null_cb_data) {
  119. FinishCBData();
  120. }
  121. // It is an error to write to a register other than the current macro's ARG register before it
  122. // has finished execution.
  123. if (executing_macro != 0) {
  124. ASSERT(method == executing_macro + 1);
  125. }
  126. // Methods after 0xE00 are special, they're actually triggers for some microcode that was
  127. // uploaded to the GPU during initialization.
  128. if (method >= MacroRegistersStart) {
  129. // We're trying to execute a macro
  130. if (executing_macro == 0) {
  131. // A macro call must begin by writing the macro method's register, not its argument.
  132. ASSERT_MSG((method % 2) == 0,
  133. "Can't start macro execution by writing to the ARGS register");
  134. executing_macro = method;
  135. }
  136. macro_params.push_back(method_argument);
  137. // Call the macro when there are no more parameters in the command buffer
  138. if (is_last_call) {
  139. CallMacroMethod(executing_macro, macro_params.size(), macro_params.data());
  140. macro_params.clear();
  141. }
  142. return;
  143. }
  144. ASSERT_MSG(method < Regs::NUM_REGS,
  145. "Invalid Maxwell3D register, increase the size of the Regs structure");
  146. u32 arg = method_argument;
  147. // Keep track of the register value in shadow_state when requested.
  148. if (shadow_state.shadow_ram_control == Regs::ShadowRamControl::Track ||
  149. shadow_state.shadow_ram_control == Regs::ShadowRamControl::TrackWithFilter) {
  150. shadow_state.reg_array[method] = arg;
  151. } else if (shadow_state.shadow_ram_control == Regs::ShadowRamControl::Replay) {
  152. arg = shadow_state.reg_array[method];
  153. }
  154. if (regs.reg_array[method] != arg) {
  155. regs.reg_array[method] = arg;
  156. for (const auto& table : dirty.tables) {
  157. dirty.flags[table[method]] = true;
  158. }
  159. }
  160. switch (method) {
  161. case MAXWELL3D_REG_INDEX(wait_for_idle): {
  162. rasterizer.WaitForIdle();
  163. break;
  164. }
  165. case MAXWELL3D_REG_INDEX(shadow_ram_control): {
  166. shadow_state.shadow_ram_control = static_cast<Regs::ShadowRamControl>(method_argument);
  167. break;
  168. }
  169. case MAXWELL3D_REG_INDEX(macros.data): {
  170. ProcessMacroUpload(arg);
  171. break;
  172. }
  173. case MAXWELL3D_REG_INDEX(macros.bind): {
  174. ProcessMacroBind(arg);
  175. break;
  176. }
  177. case MAXWELL3D_REG_INDEX(firmware[4]): {
  178. ProcessFirmwareCall4();
  179. break;
  180. }
  181. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[0]):
  182. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[1]):
  183. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[2]):
  184. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[3]):
  185. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[4]):
  186. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[5]):
  187. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[6]):
  188. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[7]):
  189. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[8]):
  190. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[9]):
  191. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[10]):
  192. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[11]):
  193. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[12]):
  194. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[13]):
  195. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[14]):
  196. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[15]): {
  197. StartCBData(method);
  198. break;
  199. }
  200. case MAXWELL3D_REG_INDEX(cb_bind[0]): {
  201. ProcessCBBind(0);
  202. break;
  203. }
  204. case MAXWELL3D_REG_INDEX(cb_bind[1]): {
  205. ProcessCBBind(1);
  206. break;
  207. }
  208. case MAXWELL3D_REG_INDEX(cb_bind[2]): {
  209. ProcessCBBind(2);
  210. break;
  211. }
  212. case MAXWELL3D_REG_INDEX(cb_bind[3]): {
  213. ProcessCBBind(3);
  214. break;
  215. }
  216. case MAXWELL3D_REG_INDEX(cb_bind[4]): {
  217. ProcessCBBind(4);
  218. break;
  219. }
  220. case MAXWELL3D_REG_INDEX(draw.vertex_end_gl): {
  221. DrawArrays();
  222. break;
  223. }
  224. case MAXWELL3D_REG_INDEX(clear_buffers): {
  225. ProcessClearBuffers();
  226. break;
  227. }
  228. case MAXWELL3D_REG_INDEX(query.query_get): {
  229. ProcessQueryGet();
  230. break;
  231. }
  232. case MAXWELL3D_REG_INDEX(condition.mode): {
  233. ProcessQueryCondition();
  234. break;
  235. }
  236. case MAXWELL3D_REG_INDEX(counter_reset): {
  237. ProcessCounterReset();
  238. break;
  239. }
  240. case MAXWELL3D_REG_INDEX(sync_info): {
  241. ProcessSyncPoint();
  242. break;
  243. }
  244. case MAXWELL3D_REG_INDEX(exec_upload): {
  245. upload_state.ProcessExec(regs.exec_upload.linear != 0);
  246. break;
  247. }
  248. case MAXWELL3D_REG_INDEX(data_upload): {
  249. upload_state.ProcessData(arg, is_last_call);
  250. if (is_last_call) {
  251. OnMemoryWrite();
  252. }
  253. break;
  254. }
  255. default:
  256. break;
  257. }
  258. }
  259. void Maxwell3D::CallMultiMethod(u32 method, const u32* base_start, u32 amount,
  260. u32 methods_pending) {
  261. // Methods after 0xE00 are special, they're actually triggers for some microcode that was
  262. // uploaded to the GPU during initialization.
  263. if (method >= MacroRegistersStart) {
  264. // We're trying to execute a macro
  265. if (executing_macro == 0) {
  266. // A macro call must begin by writing the macro method's register, not its argument.
  267. ASSERT_MSG((method % 2) == 0,
  268. "Can't start macro execution by writing to the ARGS register");
  269. executing_macro = method;
  270. }
  271. for (std::size_t i = 0; i < amount; i++) {
  272. macro_params.push_back(base_start[i]);
  273. }
  274. // Call the macro when there are no more parameters in the command buffer
  275. if (amount == methods_pending) {
  276. CallMacroMethod(executing_macro, macro_params.size(), macro_params.data());
  277. macro_params.clear();
  278. }
  279. return;
  280. }
  281. switch (method) {
  282. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[0]):
  283. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[1]):
  284. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[2]):
  285. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[3]):
  286. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[4]):
  287. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[5]):
  288. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[6]):
  289. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[7]):
  290. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[8]):
  291. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[9]):
  292. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[10]):
  293. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[11]):
  294. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[12]):
  295. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[13]):
  296. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[14]):
  297. case MAXWELL3D_REG_INDEX(const_buffer.cb_data[15]): {
  298. ProcessCBMultiData(method, base_start, amount);
  299. break;
  300. }
  301. default: {
  302. for (std::size_t i = 0; i < amount; i++) {
  303. CallMethod(method, base_start[i], methods_pending - static_cast<u32>(i) <= 1);
  304. }
  305. }
  306. }
  307. }
  308. void Maxwell3D::StepInstance(const MMEDrawMode expected_mode, const u32 count) {
  309. if (mme_draw.current_mode == MMEDrawMode::Undefined) {
  310. if (mme_draw.gl_begin_consume) {
  311. mme_draw.current_mode = expected_mode;
  312. mme_draw.current_count = count;
  313. mme_draw.instance_count = 1;
  314. mme_draw.gl_begin_consume = false;
  315. mme_draw.gl_end_count = 0;
  316. }
  317. return;
  318. } else {
  319. if (mme_draw.current_mode == expected_mode && count == mme_draw.current_count &&
  320. mme_draw.instance_mode && mme_draw.gl_begin_consume) {
  321. mme_draw.instance_count++;
  322. mme_draw.gl_begin_consume = false;
  323. return;
  324. } else {
  325. FlushMMEInlineDraw();
  326. }
  327. }
  328. // Tail call in case it needs to retry.
  329. StepInstance(expected_mode, count);
  330. }
  331. void Maxwell3D::CallMethodFromMME(u32 method, u32 method_argument) {
  332. if (mme_inline[method]) {
  333. regs.reg_array[method] = method_argument;
  334. if (method == MAXWELL3D_REG_INDEX(vertex_buffer.count) ||
  335. method == MAXWELL3D_REG_INDEX(index_array.count)) {
  336. const MMEDrawMode expected_mode = method == MAXWELL3D_REG_INDEX(vertex_buffer.count)
  337. ? MMEDrawMode::Array
  338. : MMEDrawMode::Indexed;
  339. StepInstance(expected_mode, method_argument);
  340. } else if (method == MAXWELL3D_REG_INDEX(draw.vertex_begin_gl)) {
  341. mme_draw.instance_mode =
  342. (regs.draw.instance_next != 0) || (regs.draw.instance_cont != 0);
  343. mme_draw.gl_begin_consume = true;
  344. } else {
  345. mme_draw.gl_end_count++;
  346. }
  347. } else {
  348. if (mme_draw.current_mode != MMEDrawMode::Undefined) {
  349. FlushMMEInlineDraw();
  350. }
  351. CallMethod(method, method_argument, true);
  352. }
  353. }
  354. void Maxwell3D::FlushMMEInlineDraw() {
  355. LOG_TRACE(HW_GPU, "called, topology={}, count={}", static_cast<u32>(regs.draw.topology.Value()),
  356. regs.vertex_buffer.count);
  357. ASSERT_MSG(!(regs.index_array.count && regs.vertex_buffer.count), "Both indexed and direct?");
  358. ASSERT(mme_draw.instance_count == mme_draw.gl_end_count);
  359. // Both instance configuration registers can not be set at the same time.
  360. ASSERT_MSG(!regs.draw.instance_next || !regs.draw.instance_cont,
  361. "Illegal combination of instancing parameters");
  362. const bool is_indexed = mme_draw.current_mode == MMEDrawMode::Indexed;
  363. if (ShouldExecute()) {
  364. rasterizer.Draw(is_indexed, true);
  365. }
  366. // TODO(bunnei): Below, we reset vertex count so that we can use these registers to determine if
  367. // the game is trying to draw indexed or direct mode. This needs to be verified on HW still -
  368. // it's possible that it is incorrect and that there is some other register used to specify the
  369. // drawing mode.
  370. if (is_indexed) {
  371. regs.index_array.count = 0;
  372. } else {
  373. regs.vertex_buffer.count = 0;
  374. }
  375. mme_draw.current_mode = MMEDrawMode::Undefined;
  376. mme_draw.current_count = 0;
  377. mme_draw.instance_count = 0;
  378. mme_draw.instance_mode = false;
  379. mme_draw.gl_begin_consume = false;
  380. mme_draw.gl_end_count = 0;
  381. }
  382. void Maxwell3D::ProcessMacroUpload(u32 data) {
  383. ASSERT_MSG(regs.macros.upload_address < macro_memory.size(),
  384. "upload_address exceeded macro_memory size!");
  385. macro_memory[regs.macros.upload_address++] = data;
  386. }
  387. void Maxwell3D::ProcessMacroBind(u32 data) {
  388. macro_positions[regs.macros.entry++] = data;
  389. }
  390. void Maxwell3D::ProcessFirmwareCall4() {
  391. LOG_WARNING(HW_GPU, "(STUBBED) called");
  392. // Firmware call 4 is a blob that changes some registers depending on its parameters.
  393. // These registers don't affect emulation and so are stubbed by setting 0xd00 to 1.
  394. regs.reg_array[0xd00] = 1;
  395. }
  396. void Maxwell3D::StampQueryResult(u64 payload, bool long_query) {
  397. struct LongQueryResult {
  398. u64_le value;
  399. u64_le timestamp;
  400. };
  401. static_assert(sizeof(LongQueryResult) == 16, "LongQueryResult has wrong size");
  402. const GPUVAddr sequence_address{regs.query.QueryAddress()};
  403. if (long_query) {
  404. // Write the 128-bit result structure in long mode. Note: We emulate an infinitely fast
  405. // GPU, this command may actually take a while to complete in real hardware due to GPU
  406. // wait queues.
  407. LongQueryResult query_result{payload, system.GPU().GetTicks()};
  408. memory_manager.WriteBlock(sequence_address, &query_result, sizeof(query_result));
  409. } else {
  410. memory_manager.Write<u32>(sequence_address, static_cast<u32>(payload));
  411. }
  412. }
  413. void Maxwell3D::ProcessQueryGet() {
  414. // TODO(Subv): Support the other query units.
  415. ASSERT_MSG(regs.query.query_get.unit == Regs::QueryUnit::Crop,
  416. "Units other than CROP are unimplemented");
  417. switch (regs.query.query_get.operation) {
  418. case Regs::QueryOperation::Release:
  419. if (regs.query.query_get.fence == 1) {
  420. rasterizer.SignalSemaphore(regs.query.QueryAddress(), regs.query.query_sequence);
  421. } else {
  422. StampQueryResult(regs.query.query_sequence, regs.query.query_get.short_query == 0);
  423. }
  424. break;
  425. case Regs::QueryOperation::Acquire:
  426. // TODO(Blinkhawk): Under this operation, the GPU waits for the CPU to write a value that
  427. // matches the current payload.
  428. UNIMPLEMENTED_MSG("Unimplemented query operation ACQUIRE");
  429. break;
  430. case Regs::QueryOperation::Counter:
  431. if (const std::optional<u64> result = GetQueryResult()) {
  432. // If the query returns an empty optional it means it's cached and deferred.
  433. // In this case we have a non-empty result, so we stamp it immediately.
  434. StampQueryResult(*result, regs.query.query_get.short_query == 0);
  435. }
  436. break;
  437. case Regs::QueryOperation::Trap:
  438. UNIMPLEMENTED_MSG("Unimplemented query operation TRAP");
  439. break;
  440. default:
  441. UNIMPLEMENTED_MSG("Unknown query operation");
  442. break;
  443. }
  444. }
  445. void Maxwell3D::ProcessQueryCondition() {
  446. const GPUVAddr condition_address{regs.condition.Address()};
  447. switch (regs.condition.mode) {
  448. case Regs::ConditionMode::Always: {
  449. execute_on = true;
  450. break;
  451. }
  452. case Regs::ConditionMode::Never: {
  453. execute_on = false;
  454. break;
  455. }
  456. case Regs::ConditionMode::ResNonZero: {
  457. Regs::QueryCompare cmp;
  458. memory_manager.ReadBlock(condition_address, &cmp, sizeof(cmp));
  459. execute_on = cmp.initial_sequence != 0U && cmp.initial_mode != 0U;
  460. break;
  461. }
  462. case Regs::ConditionMode::Equal: {
  463. Regs::QueryCompare cmp;
  464. memory_manager.ReadBlock(condition_address, &cmp, sizeof(cmp));
  465. execute_on =
  466. cmp.initial_sequence == cmp.current_sequence && cmp.initial_mode == cmp.current_mode;
  467. break;
  468. }
  469. case Regs::ConditionMode::NotEqual: {
  470. Regs::QueryCompare cmp;
  471. memory_manager.ReadBlock(condition_address, &cmp, sizeof(cmp));
  472. execute_on =
  473. cmp.initial_sequence != cmp.current_sequence || cmp.initial_mode != cmp.current_mode;
  474. break;
  475. }
  476. default: {
  477. UNIMPLEMENTED_MSG("Uninplemented Condition Mode!");
  478. execute_on = true;
  479. break;
  480. }
  481. }
  482. }
  483. void Maxwell3D::ProcessCounterReset() {
  484. switch (regs.counter_reset) {
  485. case Regs::CounterReset::SampleCnt:
  486. rasterizer.ResetCounter(QueryType::SamplesPassed);
  487. break;
  488. default:
  489. LOG_WARNING(Render_OpenGL, "Unimplemented counter reset={}",
  490. static_cast<int>(regs.counter_reset));
  491. break;
  492. }
  493. }
  494. void Maxwell3D::ProcessSyncPoint() {
  495. const u32 sync_point = regs.sync_info.sync_point.Value();
  496. const u32 increment = regs.sync_info.increment.Value();
  497. [[maybe_unused]] const u32 cache_flush = regs.sync_info.unknown.Value();
  498. if (increment) {
  499. rasterizer.SignalSyncPoint(sync_point);
  500. }
  501. }
  502. void Maxwell3D::DrawArrays() {
  503. LOG_TRACE(HW_GPU, "called, topology={}, count={}", static_cast<u32>(regs.draw.topology.Value()),
  504. regs.vertex_buffer.count);
  505. ASSERT_MSG(!(regs.index_array.count && regs.vertex_buffer.count), "Both indexed and direct?");
  506. // Both instance configuration registers can not be set at the same time.
  507. ASSERT_MSG(!regs.draw.instance_next || !regs.draw.instance_cont,
  508. "Illegal combination of instancing parameters");
  509. if (regs.draw.instance_next) {
  510. // Increment the current instance *before* drawing.
  511. state.current_instance += 1;
  512. } else if (!regs.draw.instance_cont) {
  513. // Reset the current instance to 0.
  514. state.current_instance = 0;
  515. }
  516. const bool is_indexed{regs.index_array.count && !regs.vertex_buffer.count};
  517. if (ShouldExecute()) {
  518. rasterizer.Draw(is_indexed, false);
  519. }
  520. // TODO(bunnei): Below, we reset vertex count so that we can use these registers to determine if
  521. // the game is trying to draw indexed or direct mode. This needs to be verified on HW still -
  522. // it's possible that it is incorrect and that there is some other register used to specify the
  523. // drawing mode.
  524. if (is_indexed) {
  525. regs.index_array.count = 0;
  526. } else {
  527. regs.vertex_buffer.count = 0;
  528. }
  529. }
  530. std::optional<u64> Maxwell3D::GetQueryResult() {
  531. switch (regs.query.query_get.select) {
  532. case Regs::QuerySelect::Zero:
  533. return 0;
  534. case Regs::QuerySelect::SamplesPassed:
  535. // Deferred.
  536. rasterizer.Query(regs.query.QueryAddress(), VideoCore::QueryType::SamplesPassed,
  537. system.GPU().GetTicks());
  538. return {};
  539. default:
  540. UNIMPLEMENTED_MSG("Unimplemented query select type {}",
  541. static_cast<u32>(regs.query.query_get.select.Value()));
  542. return 1;
  543. }
  544. }
  545. void Maxwell3D::ProcessCBBind(std::size_t stage_index) {
  546. // Bind the buffer currently in CB_ADDRESS to the specified index in the desired shader stage.
  547. auto& shader = state.shader_stages[stage_index];
  548. auto& bind_data = regs.cb_bind[stage_index];
  549. ASSERT(bind_data.index < Regs::MaxConstBuffers);
  550. auto& buffer = shader.const_buffers[bind_data.index];
  551. buffer.enabled = bind_data.valid.Value() != 0;
  552. buffer.address = regs.const_buffer.BufferAddress();
  553. buffer.size = regs.const_buffer.cb_size;
  554. }
  555. void Maxwell3D::ProcessCBData(u32 value) {
  556. const u32 id = cb_data_state.id;
  557. cb_data_state.buffer[id][cb_data_state.counter] = value;
  558. // Increment the current buffer position.
  559. regs.const_buffer.cb_pos = regs.const_buffer.cb_pos + 4;
  560. cb_data_state.counter++;
  561. }
  562. void Maxwell3D::StartCBData(u32 method) {
  563. constexpr u32 first_cb_data = MAXWELL3D_REG_INDEX(const_buffer.cb_data[0]);
  564. cb_data_state.start_pos = regs.const_buffer.cb_pos;
  565. cb_data_state.id = method - first_cb_data;
  566. cb_data_state.current = method;
  567. cb_data_state.counter = 0;
  568. ProcessCBData(regs.const_buffer.cb_data[cb_data_state.id]);
  569. }
  570. void Maxwell3D::ProcessCBMultiData(u32 method, const u32* start_base, u32 amount) {
  571. if (cb_data_state.current != method) {
  572. if (cb_data_state.current != null_cb_data) {
  573. FinishCBData();
  574. }
  575. constexpr u32 first_cb_data = MAXWELL3D_REG_INDEX(const_buffer.cb_data[0]);
  576. cb_data_state.start_pos = regs.const_buffer.cb_pos;
  577. cb_data_state.id = method - first_cb_data;
  578. cb_data_state.current = method;
  579. cb_data_state.counter = 0;
  580. }
  581. const std::size_t id = cb_data_state.id;
  582. const std::size_t size = amount;
  583. std::size_t i = 0;
  584. for (; i < size; i++) {
  585. cb_data_state.buffer[id][cb_data_state.counter] = start_base[i];
  586. cb_data_state.counter++;
  587. }
  588. // Increment the current buffer position.
  589. regs.const_buffer.cb_pos = regs.const_buffer.cb_pos + 4 * amount;
  590. }
  591. void Maxwell3D::FinishCBData() {
  592. // Write the input value to the current const buffer at the current position.
  593. const GPUVAddr buffer_address = regs.const_buffer.BufferAddress();
  594. ASSERT(buffer_address != 0);
  595. // Don't allow writing past the end of the buffer.
  596. ASSERT(regs.const_buffer.cb_pos <= regs.const_buffer.cb_size);
  597. const GPUVAddr address{buffer_address + cb_data_state.start_pos};
  598. const std::size_t size = regs.const_buffer.cb_pos - cb_data_state.start_pos;
  599. const u32 id = cb_data_state.id;
  600. memory_manager.WriteBlock(address, cb_data_state.buffer[id].data(), size);
  601. OnMemoryWrite();
  602. cb_data_state.id = null_cb_data;
  603. cb_data_state.current = null_cb_data;
  604. }
  605. Texture::TICEntry Maxwell3D::GetTICEntry(u32 tic_index) const {
  606. const GPUVAddr tic_address_gpu{regs.tic.TICAddress() + tic_index * sizeof(Texture::TICEntry)};
  607. Texture::TICEntry tic_entry;
  608. memory_manager.ReadBlockUnsafe(tic_address_gpu, &tic_entry, sizeof(Texture::TICEntry));
  609. return tic_entry;
  610. }
  611. Texture::TSCEntry Maxwell3D::GetTSCEntry(u32 tsc_index) const {
  612. const GPUVAddr tsc_address_gpu{regs.tsc.TSCAddress() + tsc_index * sizeof(Texture::TSCEntry)};
  613. Texture::TSCEntry tsc_entry;
  614. memory_manager.ReadBlockUnsafe(tsc_address_gpu, &tsc_entry, sizeof(Texture::TSCEntry));
  615. return tsc_entry;
  616. }
  617. Texture::FullTextureInfo Maxwell3D::GetTextureInfo(Texture::TextureHandle tex_handle) const {
  618. return Texture::FullTextureInfo{GetTICEntry(tex_handle.tic_id), GetTSCEntry(tex_handle.tsc_id)};
  619. }
  620. Texture::FullTextureInfo Maxwell3D::GetStageTexture(ShaderType stage, std::size_t offset) const {
  621. const auto stage_index = static_cast<std::size_t>(stage);
  622. const auto& shader = state.shader_stages[stage_index];
  623. const auto& tex_info_buffer = shader.const_buffers[regs.tex_cb_index];
  624. ASSERT(tex_info_buffer.enabled && tex_info_buffer.address != 0);
  625. const GPUVAddr tex_info_address =
  626. tex_info_buffer.address + offset * sizeof(Texture::TextureHandle);
  627. ASSERT(tex_info_address < tex_info_buffer.address + tex_info_buffer.size);
  628. const Texture::TextureHandle tex_handle{memory_manager.Read<u32>(tex_info_address)};
  629. return GetTextureInfo(tex_handle);
  630. }
  631. u32 Maxwell3D::GetRegisterValue(u32 method) const {
  632. ASSERT_MSG(method < Regs::NUM_REGS, "Invalid Maxwell3D register");
  633. return regs.reg_array[method];
  634. }
  635. void Maxwell3D::ProcessClearBuffers() {
  636. ASSERT(regs.clear_buffers.R == regs.clear_buffers.G &&
  637. regs.clear_buffers.R == regs.clear_buffers.B &&
  638. regs.clear_buffers.R == regs.clear_buffers.A);
  639. rasterizer.Clear();
  640. }
  641. u32 Maxwell3D::AccessConstBuffer32(ShaderType stage, u64 const_buffer, u64 offset) const {
  642. ASSERT(stage != ShaderType::Compute);
  643. const auto& shader_stage = state.shader_stages[static_cast<std::size_t>(stage)];
  644. const auto& buffer = shader_stage.const_buffers[const_buffer];
  645. u32 result;
  646. std::memcpy(&result, memory_manager.GetPointer(buffer.address + offset), sizeof(u32));
  647. return result;
  648. }
  649. SamplerDescriptor Maxwell3D::AccessBoundSampler(ShaderType stage, u64 offset) const {
  650. return AccessBindlessSampler(stage, regs.tex_cb_index, offset * sizeof(Texture::TextureHandle));
  651. }
  652. SamplerDescriptor Maxwell3D::AccessBindlessSampler(ShaderType stage, u64 const_buffer,
  653. u64 offset) const {
  654. ASSERT(stage != ShaderType::Compute);
  655. const auto& shader = state.shader_stages[static_cast<std::size_t>(stage)];
  656. const auto& tex_info_buffer = shader.const_buffers[const_buffer];
  657. const GPUVAddr tex_info_address = tex_info_buffer.address + offset;
  658. const Texture::TextureHandle tex_handle{memory_manager.Read<u32>(tex_info_address)};
  659. const Texture::FullTextureInfo tex_info = GetTextureInfo(tex_handle);
  660. SamplerDescriptor result = SamplerDescriptor::FromTIC(tex_info.tic);
  661. result.is_shadow.Assign(tex_info.tsc.depth_compare_enabled.Value());
  662. return result;
  663. }
  664. VideoCore::GuestDriverProfile& Maxwell3D::AccessGuestDriverProfile() {
  665. return rasterizer.AccessGuestDriverProfile();
  666. }
  667. const VideoCore::GuestDriverProfile& Maxwell3D::AccessGuestDriverProfile() const {
  668. return rasterizer.AccessGuestDriverProfile();
  669. }
  670. } // namespace Tegra::Engines