maxwell_3d.cpp 26 KB

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