maxwell_3d.cpp 26 KB

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