maxwell_3d.cpp 27 KB

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