maxwell_3d.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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 "common/bit_util.h"
  7. #include "common/scope_exit.h"
  8. #include "common/settings.h"
  9. #include "core/core.h"
  10. #include "core/core_timing.h"
  11. #include "core/memory.h"
  12. #include "video_core/dirty_flags.h"
  13. #include "video_core/engines/draw_manager.h"
  14. #include "video_core/engines/maxwell_3d.h"
  15. #include "video_core/gpu.h"
  16. #include "video_core/memory_manager.h"
  17. #include "video_core/rasterizer_interface.h"
  18. #include "video_core/textures/texture.h"
  19. namespace Tegra::Engines {
  20. using VideoCore::QueryType;
  21. /// First register id that is actually a Macro call.
  22. constexpr u32 MacroRegistersStart = 0xE00;
  23. Maxwell3D::Maxwell3D(Core::System& system_, MemoryManager& memory_manager_)
  24. : draw_manager{std::make_unique<DrawManager>(this)}, system{system_},
  25. memory_manager{memory_manager_}, macro_engine{GetMacroEngine(*this)}, upload_state{
  26. memory_manager,
  27. regs.upload} {
  28. dirty.flags.flip();
  29. InitializeRegisterDefaults();
  30. execution_mask.reset();
  31. for (size_t i = 0; i < execution_mask.size(); i++) {
  32. execution_mask[i] = IsMethodExecutable(static_cast<u32>(i));
  33. }
  34. }
  35. Maxwell3D::~Maxwell3D() = default;
  36. void Maxwell3D::BindRasterizer(VideoCore::RasterizerInterface* rasterizer_) {
  37. rasterizer = rasterizer_;
  38. upload_state.BindRasterizer(rasterizer_);
  39. }
  40. void Maxwell3D::InitializeRegisterDefaults() {
  41. // Initializes registers to their default values - what games expect them to be at boot. This is
  42. // for certain registers that may not be explicitly set by games.
  43. // Reset all registers to zero
  44. std::memset(&regs, 0, sizeof(regs));
  45. // Depth range near/far is not always set, but is expected to be the default 0.0f, 1.0f. This is
  46. // needed for ARMS.
  47. for (auto& viewport : regs.viewports) {
  48. viewport.depth_range_near = 0.0f;
  49. viewport.depth_range_far = 1.0f;
  50. }
  51. for (auto& viewport : regs.viewport_transform) {
  52. viewport.swizzle.x.Assign(Regs::ViewportSwizzle::PositiveX);
  53. viewport.swizzle.y.Assign(Regs::ViewportSwizzle::PositiveY);
  54. viewport.swizzle.z.Assign(Regs::ViewportSwizzle::PositiveZ);
  55. viewport.swizzle.w.Assign(Regs::ViewportSwizzle::PositiveW);
  56. }
  57. // Doom and Bomberman seems to use the uninitialized registers and just enable blend
  58. // so initialize blend registers with sane values
  59. regs.blend.color_op = Regs::Blend::Equation::Add_D3D;
  60. regs.blend.color_source = Regs::Blend::Factor::One_D3D;
  61. regs.blend.color_dest = Regs::Blend::Factor::Zero_D3D;
  62. regs.blend.alpha_op = Regs::Blend::Equation::Add_D3D;
  63. regs.blend.alpha_source = Regs::Blend::Factor::One_D3D;
  64. regs.blend.alpha_dest = Regs::Blend::Factor::Zero_D3D;
  65. for (auto& blend : regs.blend_per_target) {
  66. blend.color_op = Regs::Blend::Equation::Add_D3D;
  67. blend.color_source = Regs::Blend::Factor::One_D3D;
  68. blend.color_dest = Regs::Blend::Factor::Zero_D3D;
  69. blend.alpha_op = Regs::Blend::Equation::Add_D3D;
  70. blend.alpha_source = Regs::Blend::Factor::One_D3D;
  71. blend.alpha_dest = Regs::Blend::Factor::Zero_D3D;
  72. }
  73. regs.stencil_front_op.fail = Regs::StencilOp::Op::Keep_D3D;
  74. regs.stencil_front_op.zfail = Regs::StencilOp::Op::Keep_D3D;
  75. regs.stencil_front_op.zpass = Regs::StencilOp::Op::Keep_D3D;
  76. regs.stencil_front_op.func = Regs::ComparisonOp::Always_GL;
  77. regs.stencil_front_func_mask = 0xFFFFFFFF;
  78. regs.stencil_front_mask = 0xFFFFFFFF;
  79. regs.stencil_two_side_enable = 1;
  80. regs.stencil_back_op.fail = Regs::StencilOp::Op::Keep_D3D;
  81. regs.stencil_back_op.zfail = Regs::StencilOp::Op::Keep_D3D;
  82. regs.stencil_back_op.zpass = Regs::StencilOp::Op::Keep_D3D;
  83. regs.stencil_back_op.func = Regs::ComparisonOp::Always_GL;
  84. regs.stencil_back_func_mask = 0xFFFFFFFF;
  85. regs.stencil_back_mask = 0xFFFFFFFF;
  86. regs.depth_test_func = Regs::ComparisonOp::Always_GL;
  87. regs.gl_front_face = Regs::FrontFace::CounterClockWise;
  88. regs.gl_cull_face = Regs::CullFace::Back;
  89. // TODO(Rodrigo): Most games do not set a point size. I think this is a case of a
  90. // register carrying a default value. Assume it's OpenGL's default (1).
  91. regs.point_size = 1.0f;
  92. // TODO(bunnei): Some games do not initialize the color masks (e.g. Sonic Mania). Assuming a
  93. // default of enabled fixes rendering here.
  94. for (auto& color_mask : regs.color_mask) {
  95. color_mask.R.Assign(1);
  96. color_mask.G.Assign(1);
  97. color_mask.B.Assign(1);
  98. color_mask.A.Assign(1);
  99. }
  100. for (auto& format : regs.vertex_attrib_format) {
  101. format.constant.Assign(1);
  102. }
  103. // NVN games expect these values to be enabled at boot
  104. regs.rasterize_enable = 1;
  105. regs.color_target_mrt_enable = 1;
  106. regs.framebuffer_srgb = 1;
  107. regs.line_width_aliased = 1.0f;
  108. regs.line_width_smooth = 1.0f;
  109. regs.gl_front_face = Maxwell3D::Regs::FrontFace::ClockWise;
  110. regs.polygon_mode_back = Maxwell3D::Regs::PolygonMode::Fill;
  111. regs.polygon_mode_front = Maxwell3D::Regs::PolygonMode::Fill;
  112. shadow_state = regs;
  113. }
  114. bool Maxwell3D::IsMethodExecutable(u32 method) {
  115. if (method >= MacroRegistersStart) {
  116. return true;
  117. }
  118. switch (method) {
  119. case MAXWELL3D_REG_INDEX(draw.end):
  120. case MAXWELL3D_REG_INDEX(draw.begin):
  121. case MAXWELL3D_REG_INDEX(vertex_buffer.first):
  122. case MAXWELL3D_REG_INDEX(vertex_buffer.count):
  123. case MAXWELL3D_REG_INDEX(index_buffer.first):
  124. case MAXWELL3D_REG_INDEX(index_buffer.count):
  125. case MAXWELL3D_REG_INDEX(draw_inline_index):
  126. case MAXWELL3D_REG_INDEX(index_buffer32_subsequent):
  127. case MAXWELL3D_REG_INDEX(index_buffer16_subsequent):
  128. case MAXWELL3D_REG_INDEX(index_buffer8_subsequent):
  129. case MAXWELL3D_REG_INDEX(index_buffer32_first):
  130. case MAXWELL3D_REG_INDEX(index_buffer16_first):
  131. case MAXWELL3D_REG_INDEX(index_buffer8_first):
  132. case MAXWELL3D_REG_INDEX(inline_index_2x16.even):
  133. case MAXWELL3D_REG_INDEX(inline_index_4x8.index0):
  134. case MAXWELL3D_REG_INDEX(vertex_array_instance_first):
  135. case MAXWELL3D_REG_INDEX(vertex_array_instance_subsequent):
  136. case MAXWELL3D_REG_INDEX(draw_texture.src_y0):
  137. case MAXWELL3D_REG_INDEX(wait_for_idle):
  138. case MAXWELL3D_REG_INDEX(shadow_ram_control):
  139. case MAXWELL3D_REG_INDEX(load_mme.instruction_ptr):
  140. case MAXWELL3D_REG_INDEX(load_mme.instruction):
  141. case MAXWELL3D_REG_INDEX(load_mme.start_address):
  142. case MAXWELL3D_REG_INDEX(falcon[4]):
  143. case MAXWELL3D_REG_INDEX(const_buffer.buffer):
  144. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 1:
  145. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 2:
  146. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 3:
  147. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 4:
  148. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 5:
  149. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 6:
  150. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 7:
  151. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 8:
  152. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 9:
  153. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 10:
  154. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 11:
  155. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 12:
  156. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 13:
  157. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 14:
  158. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 15:
  159. case MAXWELL3D_REG_INDEX(bind_groups[0].raw_config):
  160. case MAXWELL3D_REG_INDEX(bind_groups[1].raw_config):
  161. case MAXWELL3D_REG_INDEX(bind_groups[2].raw_config):
  162. case MAXWELL3D_REG_INDEX(bind_groups[3].raw_config):
  163. case MAXWELL3D_REG_INDEX(bind_groups[4].raw_config):
  164. case MAXWELL3D_REG_INDEX(topology_override):
  165. case MAXWELL3D_REG_INDEX(clear_surface):
  166. case MAXWELL3D_REG_INDEX(report_semaphore.query):
  167. case MAXWELL3D_REG_INDEX(render_enable.mode):
  168. case MAXWELL3D_REG_INDEX(clear_report_value):
  169. case MAXWELL3D_REG_INDEX(sync_info):
  170. case MAXWELL3D_REG_INDEX(launch_dma):
  171. case MAXWELL3D_REG_INDEX(inline_data):
  172. case MAXWELL3D_REG_INDEX(fragment_barrier):
  173. case MAXWELL3D_REG_INDEX(invalidate_texture_data_cache):
  174. case MAXWELL3D_REG_INDEX(tiled_cache_barrier):
  175. return true;
  176. default:
  177. return false;
  178. }
  179. }
  180. void Maxwell3D::ProcessMacro(u32 method, const u32* base_start, u32 amount, bool is_last_call) {
  181. if (executing_macro == 0) {
  182. // A macro call must begin by writing the macro method's register, not its argument.
  183. ASSERT_MSG((method % 2) == 0,
  184. "Can't start macro execution by writing to the ARGS register");
  185. executing_macro = method;
  186. }
  187. macro_params.insert(macro_params.end(), base_start, base_start + amount);
  188. for (size_t i = 0; i < amount; i++) {
  189. macro_addresses.push_back(current_dma_segment + i * sizeof(u32));
  190. }
  191. macro_segments.emplace_back(current_dma_segment, amount);
  192. current_macro_dirty |= current_dirty;
  193. current_dirty = false;
  194. // Call the macro when there are no more parameters in the command buffer
  195. if (is_last_call) {
  196. ConsumeSink();
  197. CallMacroMethod(executing_macro, macro_params);
  198. macro_params.clear();
  199. macro_addresses.clear();
  200. macro_segments.clear();
  201. current_macro_dirty = false;
  202. }
  203. }
  204. void Maxwell3D::RefreshParametersImpl() {
  205. if (!Settings::IsGPULevelHigh()) {
  206. return;
  207. }
  208. size_t current_index = 0;
  209. for (auto& segment : macro_segments) {
  210. if (segment.first == 0) {
  211. current_index += segment.second;
  212. continue;
  213. }
  214. memory_manager.ReadBlock(segment.first, &macro_params[current_index],
  215. sizeof(u32) * segment.second);
  216. current_index += segment.second;
  217. }
  218. }
  219. u32 Maxwell3D::GetMaxCurrentVertices() {
  220. u32 num_vertices = 0;
  221. for (size_t index = 0; index < Regs::NumVertexArrays; ++index) {
  222. const auto& array = regs.vertex_streams[index];
  223. if (array.enable == 0) {
  224. continue;
  225. }
  226. const auto& attribute = regs.vertex_attrib_format[index];
  227. if (attribute.constant) {
  228. num_vertices = std::max(num_vertices, 1U);
  229. continue;
  230. }
  231. const auto& limit = regs.vertex_stream_limits[index];
  232. const GPUVAddr gpu_addr_begin = array.Address();
  233. const GPUVAddr gpu_addr_end = limit.Address() + 1;
  234. const u32 address_size = static_cast<u32>(gpu_addr_end - gpu_addr_begin);
  235. num_vertices = std::max(
  236. num_vertices, address_size / std::max(attribute.SizeInBytes(), array.stride.Value()));
  237. break;
  238. }
  239. return num_vertices;
  240. }
  241. size_t Maxwell3D::EstimateIndexBufferSize() {
  242. GPUVAddr start_address = regs.index_buffer.StartAddress();
  243. GPUVAddr end_address = regs.index_buffer.EndAddress();
  244. static constexpr std::array<size_t, 3> max_sizes = {std::numeric_limits<u8>::max(),
  245. std::numeric_limits<u16>::max(),
  246. std::numeric_limits<u32>::max()};
  247. const size_t byte_size = regs.index_buffer.FormatSizeInBytes();
  248. const size_t log2_byte_size = Common::Log2Ceil64(byte_size);
  249. const size_t cap{GetMaxCurrentVertices() * 3 * byte_size};
  250. const size_t lower_cap =
  251. std::min<size_t>(static_cast<size_t>(end_address - start_address), cap);
  252. return std::min<size_t>(
  253. memory_manager.GetMemoryLayoutSize(start_address, byte_size * max_sizes[log2_byte_size]) /
  254. byte_size,
  255. lower_cap);
  256. }
  257. u32 Maxwell3D::ProcessShadowRam(u32 method, u32 argument) {
  258. // Keep track of the register value in shadow_state when requested.
  259. const auto control = shadow_state.shadow_ram_control;
  260. if (control == Regs::ShadowRamControl::Track ||
  261. control == Regs::ShadowRamControl::TrackWithFilter) {
  262. shadow_state.reg_array[method] = argument;
  263. return argument;
  264. }
  265. if (control == Regs::ShadowRamControl::Replay) {
  266. return shadow_state.reg_array[method];
  267. }
  268. return argument;
  269. }
  270. void Maxwell3D::ConsumeSinkImpl() {
  271. SCOPE_EXIT({ method_sink.clear(); });
  272. const auto control = shadow_state.shadow_ram_control;
  273. if (control == Regs::ShadowRamControl::Track ||
  274. control == Regs::ShadowRamControl::TrackWithFilter) {
  275. for (auto [method, value] : method_sink) {
  276. shadow_state.reg_array[method] = value;
  277. ProcessDirtyRegisters(method, value);
  278. }
  279. return;
  280. }
  281. if (control == Regs::ShadowRamControl::Replay) {
  282. for (auto [method, value] : method_sink) {
  283. ProcessDirtyRegisters(method, shadow_state.reg_array[method]);
  284. }
  285. return;
  286. }
  287. for (auto [method, value] : method_sink) {
  288. ProcessDirtyRegisters(method, value);
  289. }
  290. }
  291. void Maxwell3D::ProcessDirtyRegisters(u32 method, u32 argument) {
  292. if (regs.reg_array[method] == argument) {
  293. return;
  294. }
  295. regs.reg_array[method] = argument;
  296. for (const auto& table : dirty.tables) {
  297. dirty.flags[table[method]] = true;
  298. }
  299. }
  300. void Maxwell3D::ProcessMethodCall(u32 method, u32 argument, u32 nonshadow_argument,
  301. bool is_last_call) {
  302. switch (method) {
  303. case MAXWELL3D_REG_INDEX(wait_for_idle):
  304. return rasterizer->WaitForIdle();
  305. case MAXWELL3D_REG_INDEX(shadow_ram_control):
  306. shadow_state.shadow_ram_control = static_cast<Regs::ShadowRamControl>(nonshadow_argument);
  307. return;
  308. case MAXWELL3D_REG_INDEX(load_mme.instruction_ptr):
  309. return macro_engine->ClearCode(regs.load_mme.instruction_ptr);
  310. case MAXWELL3D_REG_INDEX(load_mme.instruction):
  311. return macro_engine->AddCode(regs.load_mme.instruction_ptr, argument);
  312. case MAXWELL3D_REG_INDEX(load_mme.start_address):
  313. return ProcessMacroBind(argument);
  314. case MAXWELL3D_REG_INDEX(falcon[4]):
  315. return ProcessFirmwareCall4();
  316. case MAXWELL3D_REG_INDEX(const_buffer.buffer):
  317. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 1:
  318. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 2:
  319. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 3:
  320. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 4:
  321. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 5:
  322. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 6:
  323. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 7:
  324. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 8:
  325. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 9:
  326. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 10:
  327. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 11:
  328. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 12:
  329. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 13:
  330. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 14:
  331. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 15:
  332. return ProcessCBData(argument);
  333. case MAXWELL3D_REG_INDEX(bind_groups[0].raw_config):
  334. return ProcessCBBind(0);
  335. case MAXWELL3D_REG_INDEX(bind_groups[1].raw_config):
  336. return ProcessCBBind(1);
  337. case MAXWELL3D_REG_INDEX(bind_groups[2].raw_config):
  338. return ProcessCBBind(2);
  339. case MAXWELL3D_REG_INDEX(bind_groups[3].raw_config):
  340. return ProcessCBBind(3);
  341. case MAXWELL3D_REG_INDEX(bind_groups[4].raw_config):
  342. return ProcessCBBind(4);
  343. case MAXWELL3D_REG_INDEX(report_semaphore.query):
  344. return ProcessQueryGet();
  345. case MAXWELL3D_REG_INDEX(render_enable.mode):
  346. return ProcessQueryCondition();
  347. case MAXWELL3D_REG_INDEX(clear_report_value):
  348. return ProcessCounterReset();
  349. case MAXWELL3D_REG_INDEX(sync_info):
  350. return ProcessSyncPoint();
  351. case MAXWELL3D_REG_INDEX(launch_dma):
  352. return upload_state.ProcessExec(regs.launch_dma.memory_layout.Value() ==
  353. Regs::LaunchDMA::Layout::Pitch);
  354. case MAXWELL3D_REG_INDEX(inline_data):
  355. upload_state.ProcessData(argument, is_last_call);
  356. return;
  357. case MAXWELL3D_REG_INDEX(fragment_barrier):
  358. return rasterizer->FragmentBarrier();
  359. case MAXWELL3D_REG_INDEX(invalidate_texture_data_cache):
  360. rasterizer->InvalidateGPUCache();
  361. return rasterizer->WaitForIdle();
  362. case MAXWELL3D_REG_INDEX(tiled_cache_barrier):
  363. return rasterizer->TiledCacheBarrier();
  364. default:
  365. draw_manager->ProcessMethodCall(method, argument);
  366. break;
  367. }
  368. }
  369. void Maxwell3D::CallMacroMethod(u32 method, const std::vector<u32>& parameters) {
  370. // Reset the current macro.
  371. executing_macro = 0;
  372. // Lookup the macro offset
  373. const u32 entry =
  374. ((method - MacroRegistersStart) >> 1) % static_cast<u32>(macro_positions.size());
  375. // Execute the current macro.
  376. macro_engine->Execute(macro_positions[entry], parameters);
  377. draw_manager->DrawDeferred();
  378. }
  379. void Maxwell3D::CallMethod(u32 method, u32 method_argument, bool is_last_call) {
  380. // It is an error to write to a register other than the current macro's ARG register before
  381. // it has finished execution.
  382. if (executing_macro != 0) {
  383. ASSERT(method == executing_macro + 1);
  384. }
  385. // Methods after 0xE00 are special, they're actually triggers for some microcode that was
  386. // uploaded to the GPU during initialization.
  387. if (method >= MacroRegistersStart) {
  388. ProcessMacro(method, &method_argument, 1, is_last_call);
  389. return;
  390. }
  391. ASSERT_MSG(method < Regs::NUM_REGS,
  392. "Invalid Maxwell3D register, increase the size of the Regs structure");
  393. const u32 argument = ProcessShadowRam(method, method_argument);
  394. ProcessDirtyRegisters(method, argument);
  395. ProcessMethodCall(method, argument, method_argument, is_last_call);
  396. }
  397. void Maxwell3D::CallMultiMethod(u32 method, const u32* base_start, u32 amount,
  398. u32 methods_pending) {
  399. // Methods after 0xE00 are special, they're actually triggers for some microcode that was
  400. // uploaded to the GPU during initialization.
  401. if (method >= MacroRegistersStart) {
  402. ProcessMacro(method, base_start, amount, amount == methods_pending);
  403. return;
  404. }
  405. switch (method) {
  406. case MAXWELL3D_REG_INDEX(const_buffer.buffer):
  407. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 1:
  408. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 2:
  409. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 3:
  410. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 4:
  411. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 5:
  412. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 6:
  413. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 7:
  414. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 8:
  415. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 9:
  416. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 10:
  417. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 11:
  418. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 12:
  419. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 13:
  420. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 14:
  421. case MAXWELL3D_REG_INDEX(const_buffer.buffer) + 15:
  422. ProcessCBMultiData(base_start, amount);
  423. break;
  424. case MAXWELL3D_REG_INDEX(inline_data): {
  425. ASSERT(methods_pending == amount);
  426. upload_state.ProcessData(base_start, amount);
  427. return;
  428. }
  429. default:
  430. for (u32 i = 0; i < amount; i++) {
  431. CallMethod(method, base_start[i], methods_pending - i <= 1);
  432. }
  433. break;
  434. }
  435. }
  436. void Maxwell3D::ProcessMacroUpload(u32 data) {
  437. macro_engine->AddCode(regs.load_mme.instruction_ptr++, data);
  438. }
  439. void Maxwell3D::ProcessMacroBind(u32 data) {
  440. macro_positions[regs.load_mme.start_address_ptr++] = data;
  441. }
  442. void Maxwell3D::ProcessFirmwareCall4() {
  443. LOG_DEBUG(HW_GPU, "(STUBBED) called");
  444. // Firmware call 4 is a blob that changes some registers depending on its parameters.
  445. // These registers don't affect emulation and so are stubbed by setting 0xd00 to 1.
  446. regs.shadow_scratch[0] = 1;
  447. }
  448. void Maxwell3D::StampQueryResult(u64 payload, bool long_query) {
  449. const GPUVAddr sequence_address{regs.report_semaphore.Address()};
  450. if (long_query) {
  451. memory_manager.Write<u64>(sequence_address + sizeof(u64), system.GPU().GetTicks());
  452. memory_manager.Write<u64>(sequence_address, payload);
  453. } else {
  454. memory_manager.Write<u32>(sequence_address, static_cast<u32>(payload));
  455. }
  456. }
  457. void Maxwell3D::ProcessQueryGet() {
  458. switch (regs.report_semaphore.query.operation) {
  459. case Regs::ReportSemaphore::Operation::Release:
  460. if (regs.report_semaphore.query.short_query != 0) {
  461. const GPUVAddr sequence_address{regs.report_semaphore.Address()};
  462. const u32 payload = regs.report_semaphore.payload;
  463. std::function<void()> operation([this, sequence_address, payload] {
  464. memory_manager.Write<u32>(sequence_address, payload);
  465. });
  466. rasterizer->SignalFence(std::move(operation));
  467. } else {
  468. struct LongQueryResult {
  469. u64_le value;
  470. u64_le timestamp;
  471. };
  472. const GPUVAddr sequence_address{regs.report_semaphore.Address()};
  473. const u32 payload = regs.report_semaphore.payload;
  474. [this, sequence_address, payload] {
  475. memory_manager.Write<u64>(sequence_address + sizeof(u64), system.GPU().GetTicks());
  476. memory_manager.Write<u64>(sequence_address, payload);
  477. }();
  478. }
  479. break;
  480. case Regs::ReportSemaphore::Operation::Acquire:
  481. // TODO(Blinkhawk): Under this operation, the GPU waits for the CPU to write a value that
  482. // matches the current payload.
  483. UNIMPLEMENTED_MSG("Unimplemented query operation ACQUIRE");
  484. break;
  485. case Regs::ReportSemaphore::Operation::ReportOnly:
  486. if (const std::optional<u64> result = GetQueryResult()) {
  487. // If the query returns an empty optional it means it's cached and deferred.
  488. // In this case we have a non-empty result, so we stamp it immediately.
  489. StampQueryResult(*result, regs.report_semaphore.query.short_query == 0);
  490. }
  491. break;
  492. case Regs::ReportSemaphore::Operation::Trap:
  493. UNIMPLEMENTED_MSG("Unimplemented query operation TRAP");
  494. break;
  495. default:
  496. UNIMPLEMENTED_MSG("Unknown query operation");
  497. break;
  498. }
  499. }
  500. void Maxwell3D::ProcessQueryCondition() {
  501. const GPUVAddr condition_address{regs.render_enable.Address()};
  502. switch (regs.render_enable_override) {
  503. case Regs::RenderEnable::Override::AlwaysRender:
  504. execute_on = true;
  505. break;
  506. case Regs::RenderEnable::Override::NeverRender:
  507. execute_on = false;
  508. break;
  509. case Regs::RenderEnable::Override::UseRenderEnable: {
  510. if (rasterizer->AccelerateConditionalRendering()) {
  511. execute_on = true;
  512. return;
  513. }
  514. switch (regs.render_enable.mode) {
  515. case Regs::RenderEnable::Mode::True: {
  516. execute_on = true;
  517. break;
  518. }
  519. case Regs::RenderEnable::Mode::False: {
  520. execute_on = false;
  521. break;
  522. }
  523. case Regs::RenderEnable::Mode::Conditional: {
  524. Regs::ReportSemaphore::Compare cmp;
  525. memory_manager.ReadBlock(condition_address, &cmp, sizeof(cmp));
  526. execute_on = cmp.initial_sequence != 0U && cmp.initial_mode != 0U;
  527. break;
  528. }
  529. case Regs::RenderEnable::Mode::IfEqual: {
  530. Regs::ReportSemaphore::Compare cmp;
  531. memory_manager.ReadBlock(condition_address, &cmp, sizeof(cmp));
  532. execute_on = cmp.initial_sequence == cmp.current_sequence &&
  533. cmp.initial_mode == cmp.current_mode;
  534. break;
  535. }
  536. case Regs::RenderEnable::Mode::IfNotEqual: {
  537. Regs::ReportSemaphore::Compare cmp;
  538. memory_manager.ReadBlock(condition_address, &cmp, sizeof(cmp));
  539. execute_on = cmp.initial_sequence != cmp.current_sequence ||
  540. cmp.initial_mode != cmp.current_mode;
  541. break;
  542. }
  543. default: {
  544. UNIMPLEMENTED_MSG("Uninplemented Condition Mode!");
  545. execute_on = true;
  546. break;
  547. }
  548. }
  549. break;
  550. }
  551. }
  552. }
  553. void Maxwell3D::ProcessCounterReset() {
  554. #if ANDROID
  555. if (!Settings::IsGPULevelHigh()) {
  556. // This is problematic on Android, disable on GPU Normal.
  557. return;
  558. }
  559. #endif
  560. switch (regs.clear_report_value) {
  561. case Regs::ClearReport::ZPassPixelCount:
  562. rasterizer->ResetCounter(QueryType::SamplesPassed);
  563. break;
  564. default:
  565. LOG_DEBUG(Render_OpenGL, "Unimplemented counter reset={}", regs.clear_report_value);
  566. break;
  567. }
  568. }
  569. void Maxwell3D::ProcessSyncPoint() {
  570. const u32 sync_point = regs.sync_info.sync_point.Value();
  571. [[maybe_unused]] const u32 cache_flush = regs.sync_info.clean_l2.Value();
  572. rasterizer->SignalSyncPoint(sync_point);
  573. }
  574. std::optional<u64> Maxwell3D::GetQueryResult() {
  575. switch (regs.report_semaphore.query.report) {
  576. case Regs::ReportSemaphore::Report::Payload:
  577. return regs.report_semaphore.payload;
  578. case Regs::ReportSemaphore::Report::ZPassPixelCount64:
  579. #if ANDROID
  580. if (!Settings::IsGPULevelHigh()) {
  581. // This is problematic on Android, disable on GPU Normal.
  582. return 120;
  583. }
  584. #endif
  585. // Deferred.
  586. rasterizer->Query(regs.report_semaphore.Address(), QueryType::SamplesPassed,
  587. system.GPU().GetTicks());
  588. return std::nullopt;
  589. default:
  590. LOG_DEBUG(HW_GPU, "Unimplemented query report type {}",
  591. regs.report_semaphore.query.report.Value());
  592. return 1;
  593. }
  594. }
  595. void Maxwell3D::ProcessCBBind(size_t stage_index) {
  596. // Bind the buffer currently in CB_ADDRESS to the specified index in the desired shader
  597. // stage.
  598. const auto& bind_data = regs.bind_groups[stage_index];
  599. auto& buffer = state.shader_stages[stage_index].const_buffers[bind_data.shader_slot];
  600. buffer.enabled = bind_data.valid.Value() != 0;
  601. buffer.address = regs.const_buffer.Address();
  602. buffer.size = regs.const_buffer.size;
  603. const bool is_enabled = bind_data.valid.Value() != 0;
  604. if (!is_enabled) {
  605. rasterizer->DisableGraphicsUniformBuffer(stage_index, bind_data.shader_slot);
  606. return;
  607. }
  608. const GPUVAddr gpu_addr = regs.const_buffer.Address();
  609. const u32 size = regs.const_buffer.size;
  610. rasterizer->BindGraphicsUniformBuffer(stage_index, bind_data.shader_slot, gpu_addr, size);
  611. }
  612. void Maxwell3D::ProcessCBMultiData(const u32* start_base, u32 amount) {
  613. // Write the input value to the current const buffer at the current position.
  614. const GPUVAddr buffer_address = regs.const_buffer.Address();
  615. ASSERT(buffer_address != 0);
  616. // Don't allow writing past the end of the buffer.
  617. ASSERT(regs.const_buffer.offset <= regs.const_buffer.size);
  618. const GPUVAddr address{buffer_address + regs.const_buffer.offset};
  619. const size_t copy_size = amount * sizeof(u32);
  620. memory_manager.WriteBlockCached(address, start_base, copy_size);
  621. // Increment the current buffer position.
  622. regs.const_buffer.offset += static_cast<u32>(copy_size);
  623. }
  624. void Maxwell3D::ProcessCBData(u32 value) {
  625. ProcessCBMultiData(&value, 1);
  626. }
  627. Texture::TICEntry Maxwell3D::GetTICEntry(u32 tic_index) const {
  628. const GPUVAddr tic_address_gpu{regs.tex_header.Address() +
  629. tic_index * sizeof(Texture::TICEntry)};
  630. Texture::TICEntry tic_entry;
  631. memory_manager.ReadBlockUnsafe(tic_address_gpu, &tic_entry, sizeof(Texture::TICEntry));
  632. return tic_entry;
  633. }
  634. Texture::TSCEntry Maxwell3D::GetTSCEntry(u32 tsc_index) const {
  635. const GPUVAddr tsc_address_gpu{regs.tex_sampler.Address() +
  636. tsc_index * sizeof(Texture::TSCEntry)};
  637. Texture::TSCEntry tsc_entry;
  638. memory_manager.ReadBlockUnsafe(tsc_address_gpu, &tsc_entry, sizeof(Texture::TSCEntry));
  639. return tsc_entry;
  640. }
  641. u32 Maxwell3D::GetRegisterValue(u32 method) const {
  642. ASSERT_MSG(method < Regs::NUM_REGS, "Invalid Maxwell3D register");
  643. return regs.reg_array[method];
  644. }
  645. void Maxwell3D::SetHLEReplacementAttributeType(u32 bank, u32 offset,
  646. HLEReplacementAttributeType name) {
  647. const u64 key = (static_cast<u64>(bank) << 32) | offset;
  648. replace_table.emplace(key, name);
  649. }
  650. } // namespace Tegra::Engines