maxwell_3d.cpp 28 KB

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