maxwell_3d.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <cstring>
  4. #include <optional>
  5. #include "common/assert.h"
  6. #include "core/core.h"
  7. #include "core/core_timing.h"
  8. #include "video_core/dirty_flags.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. upload_state.BindRasterizer(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.color_op = Regs::Blend::Equation::Add_D3D;
  49. regs.blend.color_source = Regs::Blend::Factor::One_D3D;
  50. regs.blend.color_dest = Regs::Blend::Factor::Zero_D3D;
  51. regs.blend.alpha_op = Regs::Blend::Equation::Add_D3D;
  52. regs.blend.alpha_source = Regs::Blend::Factor::One_D3D;
  53. regs.blend.alpha_dest = Regs::Blend::Factor::Zero_D3D;
  54. for (auto& blend : regs.blend_per_target) {
  55. blend.color_op = Regs::Blend::Equation::Add_D3D;
  56. blend.color_source = Regs::Blend::Factor::One_D3D;
  57. blend.color_dest = Regs::Blend::Factor::Zero_D3D;
  58. blend.alpha_op = Regs::Blend::Equation::Add_D3D;
  59. blend.alpha_source = Regs::Blend::Factor::One_D3D;
  60. blend.alpha_dest = Regs::Blend::Factor::Zero_D3D;
  61. }
  62. regs.stencil_front_op.fail = Regs::StencilOp::Op::Keep_D3D;
  63. regs.stencil_front_op.zfail = Regs::StencilOp::Op::Keep_D3D;
  64. regs.stencil_front_op.zpass = Regs::StencilOp::Op::Keep_D3D;
  65. regs.stencil_front_op.func = Regs::ComparisonOp::Always_GL;
  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::Op::Keep_D3D;
  70. regs.stencil_back_op.zfail = Regs::StencilOp::Op::Keep_D3D;
  71. regs.stencil_back_op.zpass = Regs::StencilOp::Op::Keep_D3D;
  72. regs.stencil_back_op.func = Regs::ComparisonOp::Always_GL;
  73. regs.stencil_back_func_mask = 0xFFFFFFFF;
  74. regs.stencil_back_mask = 0xFFFFFFFF;
  75. regs.depth_test_func = Regs::ComparisonOp::Always_GL;
  76. regs.gl_front_face = Regs::FrontFace::CounterClockWise;
  77. regs.gl_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.color_target_mrt_enable = 1;
  95. regs.framebuffer_srgb = 1;
  96. regs.line_width_aliased = 1.0f;
  97. regs.line_width_smooth = 1.0f;
  98. regs.gl_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. draw_command[MAXWELL3D_REG_INDEX(draw.end)] = true;
  103. draw_command[MAXWELL3D_REG_INDEX(draw.begin)] = true;
  104. draw_command[MAXWELL3D_REG_INDEX(vertex_buffer.first)] = true;
  105. draw_command[MAXWELL3D_REG_INDEX(vertex_buffer.count)] = true;
  106. draw_command[MAXWELL3D_REG_INDEX(index_buffer.first)] = true;
  107. draw_command[MAXWELL3D_REG_INDEX(index_buffer.count)] = true;
  108. draw_command[MAXWELL3D_REG_INDEX(draw_inline_index)] = true;
  109. draw_command[MAXWELL3D_REG_INDEX(inline_index_2x16.even)] = true;
  110. draw_command[MAXWELL3D_REG_INDEX(inline_index_4x8.index0)] = true;
  111. draw_command[MAXWELL3D_REG_INDEX(draw.instance_id)] = true;
  112. }
  113. void Maxwell3D::ProcessMacro(u32 method, const u32* base_start, u32 amount, bool is_last_call) {
  114. if (executing_macro == 0) {
  115. // A macro call must begin by writing the macro method's register, not its argument.
  116. ASSERT_MSG((method % 2) == 0,
  117. "Can't start macro execution by writing to the ARGS register");
  118. executing_macro = method;
  119. }
  120. macro_params.insert(macro_params.end(), base_start, base_start + amount);
  121. // Call the macro when there are no more parameters in the command buffer
  122. if (is_last_call) {
  123. CallMacroMethod(executing_macro, macro_params);
  124. macro_params.clear();
  125. }
  126. }
  127. u32 Maxwell3D::ProcessShadowRam(u32 method, u32 argument) {
  128. // Keep track of the register value in shadow_state when requested.
  129. const auto control = shadow_state.shadow_ram_control;
  130. if (control == Regs::ShadowRamControl::Track ||
  131. control == Regs::ShadowRamControl::TrackWithFilter) {
  132. shadow_state.reg_array[method] = argument;
  133. return argument;
  134. }
  135. if (control == Regs::ShadowRamControl::Replay) {
  136. return shadow_state.reg_array[method];
  137. }
  138. return argument;
  139. }
  140. void Maxwell3D::ProcessDirtyRegisters(u32 method, u32 argument) {
  141. if (regs.reg_array[method] == argument) {
  142. return;
  143. }
  144. regs.reg_array[method] = argument;
  145. for (const auto& table : dirty.tables) {
  146. dirty.flags[table[method]] = true;
  147. }
  148. }
  149. void Maxwell3D::ProcessMethodCall(u32 method, u32 argument, u32 nonshadow_argument,
  150. bool is_last_call) {
  151. switch (method) {
  152. case MAXWELL3D_REG_INDEX(wait_for_idle):
  153. return rasterizer->WaitForIdle();
  154. case MAXWELL3D_REG_INDEX(shadow_ram_control):
  155. shadow_state.shadow_ram_control = static_cast<Regs::ShadowRamControl>(nonshadow_argument);
  156. return;
  157. case MAXWELL3D_REG_INDEX(load_mme.instruction_ptr):
  158. return macro_engine->ClearCode(regs.load_mme.instruction_ptr);
  159. case MAXWELL3D_REG_INDEX(load_mme.instruction):
  160. return macro_engine->AddCode(regs.load_mme.instruction_ptr, argument);
  161. case MAXWELL3D_REG_INDEX(load_mme.start_address):
  162. return ProcessMacroBind(argument);
  163. case MAXWELL3D_REG_INDEX(falcon[4]):
  164. return ProcessFirmwareCall4();
  165. case MAXWELL3D_REG_INDEX(const_buffer.buffer):
  166. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 1:
  167. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 2:
  168. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 3:
  169. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 4:
  170. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 5:
  171. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 6:
  172. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 7:
  173. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 8:
  174. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 9:
  175. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 10:
  176. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 11:
  177. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 12:
  178. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 13:
  179. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 14:
  180. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 15:
  181. return ProcessCBData(argument);
  182. case MAXWELL3D_REG_INDEX(bind_groups[0].raw_config):
  183. return ProcessCBBind(0);
  184. case MAXWELL3D_REG_INDEX(bind_groups[1].raw_config):
  185. return ProcessCBBind(1);
  186. case MAXWELL3D_REG_INDEX(bind_groups[2].raw_config):
  187. return ProcessCBBind(2);
  188. case MAXWELL3D_REG_INDEX(bind_groups[3].raw_config):
  189. return ProcessCBBind(3);
  190. case MAXWELL3D_REG_INDEX(bind_groups[4].raw_config):
  191. return ProcessCBBind(4);
  192. case MAXWELL3D_REG_INDEX(index_buffer32_first):
  193. regs.index_buffer.count = regs.index_buffer32_first.count;
  194. regs.index_buffer.first = regs.index_buffer32_first.first;
  195. dirty.flags[VideoCommon::Dirty::IndexBuffer] = true;
  196. return ProcessDraw();
  197. case MAXWELL3D_REG_INDEX(index_buffer16_first):
  198. regs.index_buffer.count = regs.index_buffer16_first.count;
  199. regs.index_buffer.first = regs.index_buffer16_first.first;
  200. dirty.flags[VideoCommon::Dirty::IndexBuffer] = true;
  201. return ProcessDraw();
  202. case MAXWELL3D_REG_INDEX(index_buffer8_first):
  203. regs.index_buffer.count = regs.index_buffer8_first.count;
  204. regs.index_buffer.first = regs.index_buffer8_first.first;
  205. dirty.flags[VideoCommon::Dirty::IndexBuffer] = true;
  206. return ProcessDraw();
  207. case MAXWELL3D_REG_INDEX(topology_override):
  208. use_topology_override = true;
  209. return;
  210. case MAXWELL3D_REG_INDEX(clear_surface):
  211. return ProcessClearBuffers(1);
  212. case MAXWELL3D_REG_INDEX(report_semaphore.query):
  213. return ProcessQueryGet();
  214. case MAXWELL3D_REG_INDEX(render_enable.mode):
  215. return ProcessQueryCondition();
  216. case MAXWELL3D_REG_INDEX(clear_report_value):
  217. return ProcessCounterReset();
  218. case MAXWELL3D_REG_INDEX(sync_info):
  219. return ProcessSyncPoint();
  220. case MAXWELL3D_REG_INDEX(launch_dma):
  221. return upload_state.ProcessExec(regs.launch_dma.memory_layout.Value() ==
  222. Regs::LaunchDMA::Layout::Pitch);
  223. case MAXWELL3D_REG_INDEX(inline_data):
  224. upload_state.ProcessData(argument, is_last_call);
  225. return;
  226. case MAXWELL3D_REG_INDEX(fragment_barrier):
  227. return rasterizer->FragmentBarrier();
  228. case MAXWELL3D_REG_INDEX(invalidate_texture_data_cache):
  229. rasterizer->InvalidateGPUCache();
  230. return rasterizer->WaitForIdle();
  231. case MAXWELL3D_REG_INDEX(tiled_cache_barrier):
  232. return rasterizer->TiledCacheBarrier();
  233. }
  234. }
  235. void Maxwell3D::CallMacroMethod(u32 method, const std::vector<u32>& parameters) {
  236. // Reset the current macro.
  237. executing_macro = 0;
  238. // Lookup the macro offset
  239. const u32 entry =
  240. ((method - MacroRegistersStart) >> 1) % static_cast<u32>(macro_positions.size());
  241. // Execute the current macro.
  242. macro_engine->Execute(macro_positions[entry], parameters);
  243. ProcessDeferredDraw();
  244. }
  245. void Maxwell3D::CallMethod(u32 method, u32 method_argument, bool is_last_call) {
  246. // It is an error to write to a register other than the current macro's ARG register before
  247. // it has finished execution.
  248. if (executing_macro != 0) {
  249. ASSERT(method == executing_macro + 1);
  250. }
  251. // Methods after 0xE00 are special, they're actually triggers for some microcode that was
  252. // uploaded to the GPU during initialization.
  253. if (method >= MacroRegistersStart) {
  254. ProcessMacro(method, &method_argument, 1, is_last_call);
  255. return;
  256. }
  257. ASSERT_MSG(method < Regs::NUM_REGS,
  258. "Invalid Maxwell3D register, increase the size of the Regs structure");
  259. const u32 argument = ProcessShadowRam(method, method_argument);
  260. ProcessDirtyRegisters(method, argument);
  261. if (draw_command[method]) {
  262. regs.reg_array[method] = method_argument;
  263. deferred_draw_method.push_back(method);
  264. auto update_inline_index = [&](const u32 index) {
  265. inline_index_draw_indexes.push_back(static_cast<u8>(index & 0x000000ff));
  266. inline_index_draw_indexes.push_back(static_cast<u8>((index & 0x0000ff00) >> 8));
  267. inline_index_draw_indexes.push_back(static_cast<u8>((index & 0x00ff0000) >> 16));
  268. inline_index_draw_indexes.push_back(static_cast<u8>((index & 0xff000000) >> 24));
  269. draw_mode = DrawMode::InlineIndex;
  270. };
  271. switch (method) {
  272. case MAXWELL3D_REG_INDEX(draw.end):
  273. switch (draw_mode) {
  274. case DrawMode::General:
  275. ProcessDraw(1);
  276. break;
  277. case DrawMode::InlineIndex:
  278. regs.index_buffer.count = static_cast<u32>(inline_index_draw_indexes.size() / 4);
  279. regs.index_buffer.format = Regs::IndexFormat::UnsignedInt;
  280. ProcessDraw(1);
  281. inline_index_draw_indexes.clear();
  282. break;
  283. case DrawMode::Instance:
  284. break;
  285. }
  286. break;
  287. case MAXWELL3D_REG_INDEX(draw_inline_index):
  288. update_inline_index(method_argument);
  289. break;
  290. case MAXWELL3D_REG_INDEX(inline_index_2x16.even):
  291. update_inline_index(regs.inline_index_2x16.even);
  292. update_inline_index(regs.inline_index_2x16.odd);
  293. break;
  294. case MAXWELL3D_REG_INDEX(inline_index_4x8.index0):
  295. update_inline_index(regs.inline_index_4x8.index0);
  296. update_inline_index(regs.inline_index_4x8.index1);
  297. update_inline_index(regs.inline_index_4x8.index2);
  298. update_inline_index(regs.inline_index_4x8.index3);
  299. break;
  300. case MAXWELL3D_REG_INDEX(draw.instance_id):
  301. draw_mode =
  302. (regs.draw.instance_id == Maxwell3D::Regs::Draw::InstanceId::Subsequent) ||
  303. (regs.draw.instance_id == Maxwell3D::Regs::Draw::InstanceId::Unchanged)
  304. ? DrawMode::Instance
  305. : DrawMode::General;
  306. break;
  307. }
  308. } else {
  309. ProcessDeferredDraw();
  310. ProcessMethodCall(method, argument, method_argument, is_last_call);
  311. }
  312. }
  313. void Maxwell3D::CallMultiMethod(u32 method, const u32* base_start, u32 amount,
  314. u32 methods_pending) {
  315. // Methods after 0xE00 are special, they're actually triggers for some microcode that was
  316. // uploaded to the GPU during initialization.
  317. if (method >= MacroRegistersStart) {
  318. ProcessMacro(method, base_start, amount, amount == methods_pending);
  319. return;
  320. }
  321. switch (method) {
  322. case MAXWELL3D_REG_INDEX(const_buffer.buffer):
  323. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 1:
  324. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 2:
  325. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 3:
  326. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 4:
  327. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 5:
  328. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 6:
  329. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 7:
  330. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 8:
  331. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 9:
  332. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 10:
  333. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 11:
  334. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 12:
  335. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 13:
  336. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 14:
  337. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 15:
  338. ProcessCBMultiData(base_start, amount);
  339. break;
  340. case MAXWELL3D_REG_INDEX(inline_data):
  341. upload_state.ProcessData(base_start, static_cast<size_t>(amount));
  342. return;
  343. default:
  344. for (std::size_t i = 0; i < amount; i++) {
  345. CallMethod(method, base_start[i], methods_pending - static_cast<u32>(i) <= 1);
  346. }
  347. break;
  348. }
  349. }
  350. void Maxwell3D::ProcessTopologyOverride() {
  351. using PrimitiveTopology = Maxwell3D::Regs::PrimitiveTopology;
  352. using PrimitiveTopologyOverride = Maxwell3D::Regs::PrimitiveTopologyOverride;
  353. PrimitiveTopology topology{};
  354. switch (regs.topology_override) {
  355. case PrimitiveTopologyOverride::None:
  356. topology = regs.draw.topology;
  357. break;
  358. case PrimitiveTopologyOverride::Points:
  359. topology = PrimitiveTopology::Points;
  360. break;
  361. case PrimitiveTopologyOverride::Lines:
  362. topology = PrimitiveTopology::Lines;
  363. break;
  364. case PrimitiveTopologyOverride::LineStrip:
  365. topology = PrimitiveTopology::LineStrip;
  366. break;
  367. default:
  368. topology = static_cast<PrimitiveTopology>(regs.topology_override);
  369. break;
  370. }
  371. if (use_topology_override) {
  372. regs.draw.topology.Assign(topology);
  373. }
  374. }
  375. void Maxwell3D::ProcessMacroUpload(u32 data) {
  376. macro_engine->AddCode(regs.load_mme.instruction_ptr++, data);
  377. }
  378. void Maxwell3D::ProcessMacroBind(u32 data) {
  379. macro_positions[regs.load_mme.start_address_ptr++] = data;
  380. }
  381. void Maxwell3D::ProcessFirmwareCall4() {
  382. LOG_WARNING(HW_GPU, "(STUBBED) called");
  383. // Firmware call 4 is a blob that changes some registers depending on its parameters.
  384. // These registers don't affect emulation and so are stubbed by setting 0xd00 to 1.
  385. regs.shadow_scratch[0] = 1;
  386. }
  387. void Maxwell3D::StampQueryResult(u64 payload, bool long_query) {
  388. const GPUVAddr sequence_address{regs.report_semaphore.Address()};
  389. if (long_query) {
  390. memory_manager.Write<u64>(sequence_address + sizeof(u64), system.GPU().GetTicks());
  391. memory_manager.Write<u64>(sequence_address, payload);
  392. } else {
  393. memory_manager.Write<u32>(sequence_address, static_cast<u32>(payload));
  394. }
  395. }
  396. void Maxwell3D::ProcessQueryGet() {
  397. // TODO(Subv): Support the other query units.
  398. if (regs.report_semaphore.query.location != Regs::ReportSemaphore::Location::All) {
  399. LOG_DEBUG(HW_GPU, "Locations other than ALL are unimplemented");
  400. }
  401. switch (regs.report_semaphore.query.operation) {
  402. case Regs::ReportSemaphore::Operation::Release:
  403. if (regs.report_semaphore.query.short_query != 0) {
  404. const GPUVAddr sequence_address{regs.report_semaphore.Address()};
  405. const u32 payload = regs.report_semaphore.payload;
  406. std::function<void()> operation([this, sequence_address, payload] {
  407. memory_manager.Write<u32>(sequence_address, payload);
  408. });
  409. rasterizer->SignalFence(std::move(operation));
  410. } else {
  411. struct LongQueryResult {
  412. u64_le value;
  413. u64_le timestamp;
  414. };
  415. const GPUVAddr sequence_address{regs.report_semaphore.Address()};
  416. const u32 payload = regs.report_semaphore.payload;
  417. [this, sequence_address, payload] {
  418. memory_manager.Write<u64>(sequence_address + sizeof(u64), system.GPU().GetTicks());
  419. memory_manager.Write<u64>(sequence_address, payload);
  420. }();
  421. }
  422. break;
  423. case Regs::ReportSemaphore::Operation::Acquire:
  424. // TODO(Blinkhawk): Under this operation, the GPU waits for the CPU to write a value that
  425. // matches the current payload.
  426. UNIMPLEMENTED_MSG("Unimplemented query operation ACQUIRE");
  427. break;
  428. case Regs::ReportSemaphore::Operation::ReportOnly:
  429. if (const std::optional<u64> result = GetQueryResult()) {
  430. // If the query returns an empty optional it means it's cached and deferred.
  431. // In this case we have a non-empty result, so we stamp it immediately.
  432. StampQueryResult(*result, regs.report_semaphore.query.short_query == 0);
  433. }
  434. break;
  435. case Regs::ReportSemaphore::Operation::Trap:
  436. UNIMPLEMENTED_MSG("Unimplemented query operation TRAP");
  437. break;
  438. default:
  439. UNIMPLEMENTED_MSG("Unknown query operation");
  440. break;
  441. }
  442. }
  443. void Maxwell3D::ProcessQueryCondition() {
  444. const GPUVAddr condition_address{regs.render_enable.Address()};
  445. switch (regs.render_enable.mode) {
  446. case Regs::RenderEnable::Mode::True: {
  447. execute_on = true;
  448. break;
  449. }
  450. case Regs::RenderEnable::Mode::False: {
  451. execute_on = false;
  452. break;
  453. }
  454. case Regs::RenderEnable::Mode::Conditional: {
  455. Regs::ReportSemaphore::Compare cmp;
  456. memory_manager.ReadBlock(condition_address, &cmp, sizeof(cmp));
  457. execute_on = cmp.initial_sequence != 0U && cmp.initial_mode != 0U;
  458. break;
  459. }
  460. case Regs::RenderEnable::Mode::IfEqual: {
  461. Regs::ReportSemaphore::Compare cmp;
  462. memory_manager.ReadBlock(condition_address, &cmp, sizeof(cmp));
  463. execute_on =
  464. cmp.initial_sequence == cmp.current_sequence && cmp.initial_mode == cmp.current_mode;
  465. break;
  466. }
  467. case Regs::RenderEnable::Mode::IfNotEqual: {
  468. Regs::ReportSemaphore::Compare cmp;
  469. memory_manager.ReadBlock(condition_address, &cmp, sizeof(cmp));
  470. execute_on =
  471. cmp.initial_sequence != cmp.current_sequence || cmp.initial_mode != cmp.current_mode;
  472. break;
  473. }
  474. default: {
  475. UNIMPLEMENTED_MSG("Uninplemented Condition Mode!");
  476. execute_on = true;
  477. break;
  478. }
  479. }
  480. }
  481. void Maxwell3D::ProcessCounterReset() {
  482. switch (regs.clear_report_value) {
  483. case Regs::ClearReport::ZPassPixelCount:
  484. rasterizer->ResetCounter(QueryType::SamplesPassed);
  485. break;
  486. default:
  487. LOG_DEBUG(Render_OpenGL, "Unimplemented counter reset={}", regs.clear_report_value);
  488. break;
  489. }
  490. }
  491. void Maxwell3D::ProcessSyncPoint() {
  492. const u32 sync_point = regs.sync_info.sync_point.Value();
  493. const u32 cache_flush = regs.sync_info.clean_l2.Value();
  494. if (cache_flush != 0) {
  495. rasterizer->InvalidateGPUCache();
  496. }
  497. rasterizer->SignalSyncPoint(sync_point);
  498. }
  499. std::optional<u64> Maxwell3D::GetQueryResult() {
  500. switch (regs.report_semaphore.query.report) {
  501. case Regs::ReportSemaphore::Report::Payload:
  502. return regs.report_semaphore.payload;
  503. case Regs::ReportSemaphore::Report::ZPassPixelCount64:
  504. // Deferred.
  505. rasterizer->Query(regs.report_semaphore.Address(), QueryType::SamplesPassed,
  506. system.GPU().GetTicks());
  507. return std::nullopt;
  508. default:
  509. LOG_DEBUG(HW_GPU, "Unimplemented query report type {}",
  510. regs.report_semaphore.query.report.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.bind_groups[stage_index];
  517. auto& buffer = state.shader_stages[stage_index].const_buffers[bind_data.shader_slot];
  518. buffer.enabled = bind_data.valid.Value() != 0;
  519. buffer.address = regs.const_buffer.Address();
  520. buffer.size = regs.const_buffer.size;
  521. const bool is_enabled = bind_data.valid.Value() != 0;
  522. if (!is_enabled) {
  523. rasterizer->DisableGraphicsUniformBuffer(stage_index, bind_data.shader_slot);
  524. return;
  525. }
  526. const GPUVAddr gpu_addr = regs.const_buffer.Address();
  527. const u32 size = regs.const_buffer.size;
  528. rasterizer->BindGraphicsUniformBuffer(stage_index, bind_data.shader_slot, gpu_addr, size);
  529. }
  530. void Maxwell3D::ProcessCBMultiData(const u32* start_base, u32 amount) {
  531. // Write the input value to the current const buffer at the current position.
  532. const GPUVAddr buffer_address = regs.const_buffer.Address();
  533. ASSERT(buffer_address != 0);
  534. // Don't allow writing past the end of the buffer.
  535. ASSERT(regs.const_buffer.offset <= regs.const_buffer.size);
  536. const GPUVAddr address{buffer_address + regs.const_buffer.offset};
  537. const size_t copy_size = amount * sizeof(u32);
  538. memory_manager.WriteBlock(address, start_base, copy_size);
  539. // Increment the current buffer position.
  540. regs.const_buffer.offset += static_cast<u32>(copy_size);
  541. }
  542. void Maxwell3D::ProcessCBData(u32 value) {
  543. ProcessCBMultiData(&value, 1);
  544. }
  545. Texture::TICEntry Maxwell3D::GetTICEntry(u32 tic_index) const {
  546. const GPUVAddr tic_address_gpu{regs.tex_header.Address() +
  547. tic_index * sizeof(Texture::TICEntry)};
  548. Texture::TICEntry tic_entry;
  549. memory_manager.ReadBlockUnsafe(tic_address_gpu, &tic_entry, sizeof(Texture::TICEntry));
  550. return tic_entry;
  551. }
  552. Texture::TSCEntry Maxwell3D::GetTSCEntry(u32 tsc_index) const {
  553. const GPUVAddr tsc_address_gpu{regs.tex_sampler.Address() +
  554. tsc_index * sizeof(Texture::TSCEntry)};
  555. Texture::TSCEntry tsc_entry;
  556. memory_manager.ReadBlockUnsafe(tsc_address_gpu, &tsc_entry, sizeof(Texture::TSCEntry));
  557. return tsc_entry;
  558. }
  559. u32 Maxwell3D::GetRegisterValue(u32 method) const {
  560. ASSERT_MSG(method < Regs::NUM_REGS, "Invalid Maxwell3D register");
  561. return regs.reg_array[method];
  562. }
  563. void Maxwell3D::ProcessClearBuffers(u32 layer_count) {
  564. rasterizer->Clear(layer_count);
  565. }
  566. void Maxwell3D::ProcessDraw(u32 instance_count) {
  567. LOG_TRACE(HW_GPU, "called, topology={}, count={}", regs.draw.topology.Value(),
  568. regs.vertex_buffer.count);
  569. ASSERT_MSG(!(regs.index_buffer.count && regs.vertex_buffer.count), "Both indexed and direct?");
  570. // Both instance configuration registers can not be set at the same time.
  571. ASSERT_MSG(regs.draw.instance_id == Maxwell3D::Regs::Draw::InstanceId::First ||
  572. regs.draw.instance_id != Maxwell3D::Regs::Draw::InstanceId::Unchanged,
  573. "Illegal combination of instancing parameters");
  574. ProcessTopologyOverride();
  575. const bool is_indexed = regs.index_buffer.count && !regs.vertex_buffer.count;
  576. if (ShouldExecute()) {
  577. rasterizer->Draw(is_indexed, instance_count);
  578. }
  579. if (is_indexed) {
  580. regs.index_buffer.count = 0;
  581. } else {
  582. regs.vertex_buffer.count = 0;
  583. }
  584. }
  585. void Maxwell3D::ProcessDeferredDraw() {
  586. if (draw_mode != DrawMode::Instance || deferred_draw_method.empty()) {
  587. return;
  588. }
  589. u32 method_count = static_cast<u32>(deferred_draw_method.size());
  590. u32 instance_count = 1;
  591. u32 vertex_buffer_count = 0;
  592. u32 index_buffer_count = 0;
  593. for (u32 index = 0; index < method_count; ++index) {
  594. u32 method = deferred_draw_method[index];
  595. if (method == MAXWELL3D_REG_INDEX(vertex_buffer.count)) {
  596. instance_count = ++vertex_buffer_count;
  597. } else if (method == MAXWELL3D_REG_INDEX(index_buffer.count)) {
  598. instance_count = ++index_buffer_count;
  599. }
  600. }
  601. ASSERT_MSG(!(vertex_buffer_count && index_buffer_count), "Instance both indexed and direct?");
  602. ProcessDraw(instance_count);
  603. deferred_draw_method.clear();
  604. }
  605. } // namespace Tegra::Engines