maxwell_3d.cpp 27 KB

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