maxwell_3d.cpp 28 KB

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