maxwell_3d.cpp 24 KB

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