vk_rasterizer.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <array>
  6. #include <memory>
  7. #include <mutex>
  8. #include <vector>
  9. #include <boost/container/static_vector.hpp>
  10. #include <boost/functional/hash.hpp>
  11. #include "common/alignment.h"
  12. #include "common/assert.h"
  13. #include "common/logging/log.h"
  14. #include "common/microprofile.h"
  15. #include "core/core.h"
  16. #include "core/memory.h"
  17. #include "video_core/engines/kepler_compute.h"
  18. #include "video_core/engines/maxwell_3d.h"
  19. #include "video_core/renderer_vulkan/declarations.h"
  20. #include "video_core/renderer_vulkan/fixed_pipeline_state.h"
  21. #include "video_core/renderer_vulkan/maxwell_to_vk.h"
  22. #include "video_core/renderer_vulkan/renderer_vulkan.h"
  23. #include "video_core/renderer_vulkan/vk_buffer_cache.h"
  24. #include "video_core/renderer_vulkan/vk_compute_pass.h"
  25. #include "video_core/renderer_vulkan/vk_compute_pipeline.h"
  26. #include "video_core/renderer_vulkan/vk_descriptor_pool.h"
  27. #include "video_core/renderer_vulkan/vk_device.h"
  28. #include "video_core/renderer_vulkan/vk_graphics_pipeline.h"
  29. #include "video_core/renderer_vulkan/vk_pipeline_cache.h"
  30. #include "video_core/renderer_vulkan/vk_rasterizer.h"
  31. #include "video_core/renderer_vulkan/vk_renderpass_cache.h"
  32. #include "video_core/renderer_vulkan/vk_resource_manager.h"
  33. #include "video_core/renderer_vulkan/vk_sampler_cache.h"
  34. #include "video_core/renderer_vulkan/vk_scheduler.h"
  35. #include "video_core/renderer_vulkan/vk_staging_buffer_pool.h"
  36. #include "video_core/renderer_vulkan/vk_state_tracker.h"
  37. #include "video_core/renderer_vulkan/vk_texture_cache.h"
  38. #include "video_core/renderer_vulkan/vk_update_descriptor.h"
  39. namespace Vulkan {
  40. using Maxwell = Tegra::Engines::Maxwell3D::Regs;
  41. MICROPROFILE_DEFINE(Vulkan_WaitForWorker, "Vulkan", "Wait for worker", MP_RGB(255, 192, 192));
  42. MICROPROFILE_DEFINE(Vulkan_Drawing, "Vulkan", "Record drawing", MP_RGB(192, 128, 128));
  43. MICROPROFILE_DEFINE(Vulkan_Compute, "Vulkan", "Record compute", MP_RGB(192, 128, 128));
  44. MICROPROFILE_DEFINE(Vulkan_Clearing, "Vulkan", "Record clearing", MP_RGB(192, 128, 128));
  45. MICROPROFILE_DEFINE(Vulkan_Geometry, "Vulkan", "Setup geometry", MP_RGB(192, 128, 128));
  46. MICROPROFILE_DEFINE(Vulkan_ConstBuffers, "Vulkan", "Setup constant buffers", MP_RGB(192, 128, 128));
  47. MICROPROFILE_DEFINE(Vulkan_GlobalBuffers, "Vulkan", "Setup global buffers", MP_RGB(192, 128, 128));
  48. MICROPROFILE_DEFINE(Vulkan_RenderTargets, "Vulkan", "Setup render targets", MP_RGB(192, 128, 128));
  49. MICROPROFILE_DEFINE(Vulkan_Textures, "Vulkan", "Setup textures", MP_RGB(192, 128, 128));
  50. MICROPROFILE_DEFINE(Vulkan_Images, "Vulkan", "Setup images", MP_RGB(192, 128, 128));
  51. MICROPROFILE_DEFINE(Vulkan_PipelineCache, "Vulkan", "Pipeline cache", MP_RGB(192, 128, 128));
  52. namespace {
  53. constexpr auto ComputeShaderIndex = static_cast<std::size_t>(Tegra::Engines::ShaderType::Compute);
  54. vk::Viewport GetViewportState(const VKDevice& device, const Maxwell& regs, std::size_t index) {
  55. const auto& viewport = regs.viewport_transform[index];
  56. const float x = viewport.translate_x - viewport.scale_x;
  57. const float y = viewport.translate_y - viewport.scale_y;
  58. const float width = viewport.scale_x * 2.0f;
  59. const float height = viewport.scale_y * 2.0f;
  60. const float reduce_z = regs.depth_mode == Maxwell::DepthMode::MinusOneToOne;
  61. float near = viewport.translate_z - viewport.scale_z * reduce_z;
  62. float far = viewport.translate_z + viewport.scale_z;
  63. if (!device.IsExtDepthRangeUnrestrictedSupported()) {
  64. near = std::clamp(near, 0.0f, 1.0f);
  65. far = std::clamp(far, 0.0f, 1.0f);
  66. }
  67. return vk::Viewport(x, y, width != 0 ? width : 1.0f, height != 0 ? height : 1.0f, near, far);
  68. }
  69. constexpr vk::Rect2D GetScissorState(const Maxwell& regs, std::size_t index) {
  70. const auto& scissor = regs.scissor_test[index];
  71. if (!scissor.enable) {
  72. return {{0, 0}, {INT32_MAX, INT32_MAX}};
  73. }
  74. const u32 width = scissor.max_x - scissor.min_x;
  75. const u32 height = scissor.max_y - scissor.min_y;
  76. return {{static_cast<s32>(scissor.min_x), static_cast<s32>(scissor.min_y)}, {width, height}};
  77. }
  78. std::array<GPUVAddr, Maxwell::MaxShaderProgram> GetShaderAddresses(
  79. const std::array<Shader, Maxwell::MaxShaderProgram>& shaders) {
  80. std::array<GPUVAddr, Maxwell::MaxShaderProgram> addresses;
  81. for (std::size_t i = 0; i < std::size(addresses); ++i) {
  82. addresses[i] = shaders[i] ? shaders[i]->GetGpuAddr() : 0;
  83. }
  84. return addresses;
  85. }
  86. void TransitionImages(const std::vector<ImageView>& views, vk::PipelineStageFlags pipeline_stage,
  87. vk::AccessFlags access) {
  88. for (auto& [view, layout] : views) {
  89. view->Transition(*layout, pipeline_stage, access);
  90. }
  91. }
  92. template <typename Engine, typename Entry>
  93. Tegra::Texture::FullTextureInfo GetTextureInfo(const Engine& engine, const Entry& entry,
  94. std::size_t stage, std::size_t index = 0) {
  95. const auto stage_type = static_cast<Tegra::Engines::ShaderType>(stage);
  96. if (entry.IsBindless()) {
  97. const Tegra::Texture::TextureHandle tex_handle =
  98. engine.AccessConstBuffer32(stage_type, entry.GetBuffer(), entry.GetOffset());
  99. return engine.GetTextureInfo(tex_handle);
  100. }
  101. const auto& gpu_profile = engine.AccessGuestDriverProfile();
  102. const u32 entry_offset = static_cast<u32>(index * gpu_profile.GetTextureHandlerSize());
  103. const u32 offset = entry.GetOffset() + entry_offset;
  104. if constexpr (std::is_same_v<Engine, Tegra::Engines::Maxwell3D>) {
  105. return engine.GetStageTexture(stage_type, offset);
  106. } else {
  107. return engine.GetTexture(offset);
  108. }
  109. }
  110. } // Anonymous namespace
  111. class BufferBindings final {
  112. public:
  113. void AddVertexBinding(const vk::Buffer* buffer, vk::DeviceSize offset) {
  114. vertex.buffer_ptrs[vertex.num_buffers] = buffer;
  115. vertex.offsets[vertex.num_buffers] = offset;
  116. ++vertex.num_buffers;
  117. }
  118. void SetIndexBinding(const vk::Buffer* buffer, vk::DeviceSize offset, vk::IndexType type) {
  119. index.buffer = buffer;
  120. index.offset = offset;
  121. index.type = type;
  122. }
  123. void Bind(VKScheduler& scheduler) const {
  124. // Use this large switch case to avoid dispatching more memory in the record lambda than
  125. // what we need. It looks horrible, but it's the best we can do on standard C++.
  126. switch (vertex.num_buffers) {
  127. case 0:
  128. return BindStatic<0>(scheduler);
  129. case 1:
  130. return BindStatic<1>(scheduler);
  131. case 2:
  132. return BindStatic<2>(scheduler);
  133. case 3:
  134. return BindStatic<3>(scheduler);
  135. case 4:
  136. return BindStatic<4>(scheduler);
  137. case 5:
  138. return BindStatic<5>(scheduler);
  139. case 6:
  140. return BindStatic<6>(scheduler);
  141. case 7:
  142. return BindStatic<7>(scheduler);
  143. case 8:
  144. return BindStatic<8>(scheduler);
  145. case 9:
  146. return BindStatic<9>(scheduler);
  147. case 10:
  148. return BindStatic<10>(scheduler);
  149. case 11:
  150. return BindStatic<11>(scheduler);
  151. case 12:
  152. return BindStatic<12>(scheduler);
  153. case 13:
  154. return BindStatic<13>(scheduler);
  155. case 14:
  156. return BindStatic<14>(scheduler);
  157. case 15:
  158. return BindStatic<15>(scheduler);
  159. case 16:
  160. return BindStatic<16>(scheduler);
  161. case 17:
  162. return BindStatic<17>(scheduler);
  163. case 18:
  164. return BindStatic<18>(scheduler);
  165. case 19:
  166. return BindStatic<19>(scheduler);
  167. case 20:
  168. return BindStatic<20>(scheduler);
  169. case 21:
  170. return BindStatic<21>(scheduler);
  171. case 22:
  172. return BindStatic<22>(scheduler);
  173. case 23:
  174. return BindStatic<23>(scheduler);
  175. case 24:
  176. return BindStatic<24>(scheduler);
  177. case 25:
  178. return BindStatic<25>(scheduler);
  179. case 26:
  180. return BindStatic<26>(scheduler);
  181. case 27:
  182. return BindStatic<27>(scheduler);
  183. case 28:
  184. return BindStatic<28>(scheduler);
  185. case 29:
  186. return BindStatic<29>(scheduler);
  187. case 30:
  188. return BindStatic<30>(scheduler);
  189. case 31:
  190. return BindStatic<31>(scheduler);
  191. case 32:
  192. return BindStatic<32>(scheduler);
  193. }
  194. UNREACHABLE();
  195. }
  196. private:
  197. // Some of these fields are intentionally left uninitialized to avoid initializing them twice.
  198. struct {
  199. std::size_t num_buffers = 0;
  200. std::array<const vk::Buffer*, Maxwell::NumVertexArrays> buffer_ptrs;
  201. std::array<vk::DeviceSize, Maxwell::NumVertexArrays> offsets;
  202. } vertex;
  203. struct {
  204. const vk::Buffer* buffer = nullptr;
  205. vk::DeviceSize offset;
  206. vk::IndexType type;
  207. } index;
  208. template <std::size_t N>
  209. void BindStatic(VKScheduler& scheduler) const {
  210. if (index.buffer != nullptr) {
  211. BindStatic<N, true>(scheduler);
  212. } else {
  213. BindStatic<N, false>(scheduler);
  214. }
  215. }
  216. template <std::size_t N, bool is_indexed>
  217. void BindStatic(VKScheduler& scheduler) const {
  218. static_assert(N <= Maxwell::NumVertexArrays);
  219. if constexpr (N == 0) {
  220. return;
  221. }
  222. std::array<vk::Buffer, N> buffers;
  223. std::transform(vertex.buffer_ptrs.begin(), vertex.buffer_ptrs.begin() + N, buffers.begin(),
  224. [](const auto ptr) { return *ptr; });
  225. std::array<vk::DeviceSize, N> offsets;
  226. std::copy(vertex.offsets.begin(), vertex.offsets.begin() + N, offsets.begin());
  227. if constexpr (is_indexed) {
  228. // Indexed draw
  229. scheduler.Record([buffers, offsets, index_buffer = *index.buffer,
  230. index_offset = index.offset,
  231. index_type = index.type](auto cmdbuf, auto& dld) {
  232. cmdbuf.bindIndexBuffer(index_buffer, index_offset, index_type, dld);
  233. cmdbuf.bindVertexBuffers(0, static_cast<u32>(N), buffers.data(), offsets.data(),
  234. dld);
  235. });
  236. } else {
  237. // Array draw
  238. scheduler.Record([buffers, offsets](auto cmdbuf, auto& dld) {
  239. cmdbuf.bindVertexBuffers(0, static_cast<u32>(N), buffers.data(), offsets.data(),
  240. dld);
  241. });
  242. }
  243. }
  244. };
  245. void RasterizerVulkan::DrawParameters::Draw(vk::CommandBuffer cmdbuf,
  246. const vk::DispatchLoaderDynamic& dld) const {
  247. if (is_indexed) {
  248. cmdbuf.drawIndexed(num_vertices, num_instances, 0, base_vertex, base_instance, dld);
  249. } else {
  250. cmdbuf.draw(num_vertices, num_instances, base_vertex, base_instance, dld);
  251. }
  252. }
  253. RasterizerVulkan::RasterizerVulkan(Core::System& system, Core::Frontend::EmuWindow& renderer,
  254. VKScreenInfo& screen_info, const VKDevice& device,
  255. VKResourceManager& resource_manager,
  256. VKMemoryManager& memory_manager, StateTracker& state_tracker,
  257. VKScheduler& scheduler)
  258. : RasterizerAccelerated{system.Memory()}, system{system}, render_window{renderer},
  259. screen_info{screen_info}, device{device}, resource_manager{resource_manager},
  260. memory_manager{memory_manager}, state_tracker{state_tracker}, scheduler{scheduler},
  261. staging_pool(device, memory_manager, scheduler), descriptor_pool(device),
  262. update_descriptor_queue(device, scheduler), renderpass_cache(device),
  263. quad_array_pass(device, scheduler, descriptor_pool, staging_pool, update_descriptor_queue),
  264. uint8_pass(device, scheduler, descriptor_pool, staging_pool, update_descriptor_queue),
  265. texture_cache(system, *this, device, resource_manager, memory_manager, scheduler,
  266. staging_pool),
  267. pipeline_cache(system, *this, device, scheduler, descriptor_pool, update_descriptor_queue,
  268. renderpass_cache),
  269. buffer_cache(*this, system, device, memory_manager, scheduler, staging_pool),
  270. sampler_cache(device), query_cache(system, *this, device, scheduler) {
  271. scheduler.SetQueryCache(query_cache);
  272. }
  273. RasterizerVulkan::~RasterizerVulkan() = default;
  274. void RasterizerVulkan::Draw(bool is_indexed, bool is_instanced) {
  275. MICROPROFILE_SCOPE(Vulkan_Drawing);
  276. FlushWork();
  277. query_cache.UpdateCounters();
  278. const auto& gpu = system.GPU().Maxwell3D();
  279. GraphicsPipelineCacheKey key{GetFixedPipelineState(gpu.regs)};
  280. buffer_cache.Map(CalculateGraphicsStreamBufferSize(is_indexed));
  281. BufferBindings buffer_bindings;
  282. const DrawParameters draw_params =
  283. SetupGeometry(key.fixed_state, buffer_bindings, is_indexed, is_instanced);
  284. update_descriptor_queue.Acquire();
  285. sampled_views.clear();
  286. image_views.clear();
  287. const auto shaders = pipeline_cache.GetShaders();
  288. key.shaders = GetShaderAddresses(shaders);
  289. SetupShaderDescriptors(shaders);
  290. buffer_cache.Unmap();
  291. const auto texceptions = UpdateAttachments();
  292. SetupImageTransitions(texceptions, color_attachments, zeta_attachment);
  293. key.renderpass_params = GetRenderPassParams(texceptions);
  294. auto& pipeline = pipeline_cache.GetGraphicsPipeline(key);
  295. scheduler.BindGraphicsPipeline(pipeline.GetHandle());
  296. const auto renderpass = pipeline.GetRenderPass();
  297. const auto [framebuffer, render_area] = ConfigureFramebuffers(renderpass);
  298. scheduler.RequestRenderpass({renderpass, framebuffer, {{0, 0}, render_area}, 0, nullptr});
  299. UpdateDynamicStates();
  300. buffer_bindings.Bind(scheduler);
  301. if (device.IsNvDeviceDiagnosticCheckpoints()) {
  302. scheduler.Record(
  303. [&pipeline](auto cmdbuf, auto& dld) { cmdbuf.setCheckpointNV(&pipeline, dld); });
  304. }
  305. BeginTransformFeedback();
  306. const auto pipeline_layout = pipeline.GetLayout();
  307. const auto descriptor_set = pipeline.CommitDescriptorSet();
  308. scheduler.Record([pipeline_layout, descriptor_set, draw_params](auto cmdbuf, auto& dld) {
  309. if (descriptor_set) {
  310. cmdbuf.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipeline_layout,
  311. DESCRIPTOR_SET, 1, &descriptor_set, 0, nullptr, dld);
  312. }
  313. draw_params.Draw(cmdbuf, dld);
  314. });
  315. EndTransformFeedback();
  316. }
  317. void RasterizerVulkan::Clear() {
  318. MICROPROFILE_SCOPE(Vulkan_Clearing);
  319. const auto& gpu = system.GPU().Maxwell3D();
  320. if (!system.GPU().Maxwell3D().ShouldExecute()) {
  321. return;
  322. }
  323. sampled_views.clear();
  324. image_views.clear();
  325. query_cache.UpdateCounters();
  326. const auto& regs = gpu.regs;
  327. const bool use_color = regs.clear_buffers.R || regs.clear_buffers.G || regs.clear_buffers.B ||
  328. regs.clear_buffers.A;
  329. const bool use_depth = regs.clear_buffers.Z;
  330. const bool use_stencil = regs.clear_buffers.S;
  331. if (!use_color && !use_depth && !use_stencil) {
  332. return;
  333. }
  334. [[maybe_unused]] const auto texceptions = UpdateAttachments();
  335. DEBUG_ASSERT(texceptions.none());
  336. SetupImageTransitions(0, color_attachments, zeta_attachment);
  337. const vk::RenderPass renderpass = renderpass_cache.GetRenderPass(GetRenderPassParams(0));
  338. const auto [framebuffer, render_area] = ConfigureFramebuffers(renderpass);
  339. scheduler.RequestRenderpass({renderpass, framebuffer, {{0, 0}, render_area}, 0, nullptr});
  340. const auto& scissor = regs.scissor_test[0];
  341. const vk::Offset2D scissor_offset(scissor.min_x, scissor.min_y);
  342. vk::Extent2D scissor_extent{scissor.max_x - scissor.min_x, scissor.max_y - scissor.min_y};
  343. scissor_extent.width = std::min(scissor_extent.width, render_area.width);
  344. scissor_extent.height = std::min(scissor_extent.height, render_area.height);
  345. const u32 layer = regs.clear_buffers.layer;
  346. const vk::ClearRect clear_rect({scissor_offset, scissor_extent}, layer, 1);
  347. if (use_color) {
  348. const std::array clear_color = {regs.clear_color[0], regs.clear_color[1],
  349. regs.clear_color[2], regs.clear_color[3]};
  350. const vk::ClearValue clear_value{clear_color};
  351. const u32 color_attachment = regs.clear_buffers.RT;
  352. scheduler.Record([color_attachment, clear_value, clear_rect](auto cmdbuf, auto& dld) {
  353. const vk::ClearAttachment attachment(vk::ImageAspectFlagBits::eColor, color_attachment,
  354. clear_value);
  355. cmdbuf.clearAttachments(1, &attachment, 1, &clear_rect, dld);
  356. });
  357. }
  358. if (!use_depth && !use_stencil) {
  359. return;
  360. }
  361. vk::ImageAspectFlags aspect_flags;
  362. if (use_depth) {
  363. aspect_flags |= vk::ImageAspectFlagBits::eDepth;
  364. }
  365. if (use_stencil) {
  366. aspect_flags |= vk::ImageAspectFlagBits::eStencil;
  367. }
  368. scheduler.Record([clear_depth = regs.clear_depth, clear_stencil = regs.clear_stencil,
  369. clear_rect, aspect_flags](auto cmdbuf, auto& dld) {
  370. const vk::ClearDepthStencilValue clear_zeta(clear_depth, clear_stencil);
  371. const vk::ClearValue clear_value{clear_zeta};
  372. const vk::ClearAttachment attachment(aspect_flags, 0, clear_value);
  373. cmdbuf.clearAttachments(1, &attachment, 1, &clear_rect, dld);
  374. });
  375. }
  376. void RasterizerVulkan::DispatchCompute(GPUVAddr code_addr) {
  377. MICROPROFILE_SCOPE(Vulkan_Compute);
  378. update_descriptor_queue.Acquire();
  379. sampled_views.clear();
  380. image_views.clear();
  381. query_cache.UpdateCounters();
  382. const auto& launch_desc = system.GPU().KeplerCompute().launch_description;
  383. const ComputePipelineCacheKey key{
  384. code_addr,
  385. launch_desc.shared_alloc,
  386. {launch_desc.block_dim_x, launch_desc.block_dim_y, launch_desc.block_dim_z}};
  387. auto& pipeline = pipeline_cache.GetComputePipeline(key);
  388. // Compute dispatches can't be executed inside a renderpass
  389. scheduler.RequestOutsideRenderPassOperationContext();
  390. buffer_cache.Map(CalculateComputeStreamBufferSize());
  391. const auto& entries = pipeline.GetEntries();
  392. SetupComputeConstBuffers(entries);
  393. SetupComputeGlobalBuffers(entries);
  394. SetupComputeTexelBuffers(entries);
  395. SetupComputeTextures(entries);
  396. SetupComputeImages(entries);
  397. buffer_cache.Unmap();
  398. TransitionImages(sampled_views, vk::PipelineStageFlagBits::eComputeShader,
  399. vk::AccessFlagBits::eShaderRead);
  400. TransitionImages(image_views, vk::PipelineStageFlagBits::eComputeShader,
  401. vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite);
  402. if (device.IsNvDeviceDiagnosticCheckpoints()) {
  403. scheduler.Record(
  404. [&pipeline](auto cmdbuf, auto& dld) { cmdbuf.setCheckpointNV(nullptr, dld); });
  405. }
  406. scheduler.Record([grid_x = launch_desc.grid_dim_x, grid_y = launch_desc.grid_dim_y,
  407. grid_z = launch_desc.grid_dim_z, pipeline_handle = pipeline.GetHandle(),
  408. layout = pipeline.GetLayout(),
  409. descriptor_set = pipeline.CommitDescriptorSet()](auto cmdbuf, auto& dld) {
  410. cmdbuf.bindPipeline(vk::PipelineBindPoint::eCompute, pipeline_handle, dld);
  411. cmdbuf.bindDescriptorSets(vk::PipelineBindPoint::eCompute, layout, DESCRIPTOR_SET, 1,
  412. &descriptor_set, 0, nullptr, dld);
  413. cmdbuf.dispatch(grid_x, grid_y, grid_z, dld);
  414. });
  415. }
  416. void RasterizerVulkan::ResetCounter(VideoCore::QueryType type) {
  417. query_cache.ResetCounter(type);
  418. }
  419. void RasterizerVulkan::Query(GPUVAddr gpu_addr, VideoCore::QueryType type,
  420. std::optional<u64> timestamp) {
  421. query_cache.Query(gpu_addr, type, timestamp);
  422. }
  423. void RasterizerVulkan::FlushAll() {}
  424. void RasterizerVulkan::FlushRegion(CacheAddr addr, u64 size) {
  425. texture_cache.FlushRegion(addr, size);
  426. buffer_cache.FlushRegion(addr, size);
  427. query_cache.FlushRegion(addr, size);
  428. }
  429. void RasterizerVulkan::InvalidateRegion(CacheAddr addr, u64 size) {
  430. texture_cache.InvalidateRegion(addr, size);
  431. pipeline_cache.InvalidateRegion(addr, size);
  432. buffer_cache.InvalidateRegion(addr, size);
  433. query_cache.InvalidateRegion(addr, size);
  434. }
  435. void RasterizerVulkan::FlushAndInvalidateRegion(CacheAddr addr, u64 size) {
  436. FlushRegion(addr, size);
  437. InvalidateRegion(addr, size);
  438. }
  439. void RasterizerVulkan::FlushCommands() {
  440. if (draw_counter > 0) {
  441. draw_counter = 0;
  442. scheduler.Flush();
  443. }
  444. }
  445. void RasterizerVulkan::TickFrame() {
  446. draw_counter = 0;
  447. update_descriptor_queue.TickFrame();
  448. buffer_cache.TickFrame();
  449. staging_pool.TickFrame();
  450. }
  451. bool RasterizerVulkan::AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Regs::Surface& src,
  452. const Tegra::Engines::Fermi2D::Regs::Surface& dst,
  453. const Tegra::Engines::Fermi2D::Config& copy_config) {
  454. texture_cache.DoFermiCopy(src, dst, copy_config);
  455. return true;
  456. }
  457. bool RasterizerVulkan::AccelerateDisplay(const Tegra::FramebufferConfig& config,
  458. VAddr framebuffer_addr, u32 pixel_stride) {
  459. if (!framebuffer_addr) {
  460. return false;
  461. }
  462. const u8* host_ptr{system.Memory().GetPointer(framebuffer_addr)};
  463. const auto surface{texture_cache.TryFindFramebufferSurface(host_ptr)};
  464. if (!surface) {
  465. return false;
  466. }
  467. // Verify that the cached surface is the same size and format as the requested framebuffer
  468. const auto& params{surface->GetSurfaceParams()};
  469. ASSERT_MSG(params.width == config.width, "Framebuffer width is different");
  470. ASSERT_MSG(params.height == config.height, "Framebuffer height is different");
  471. screen_info.image = &surface->GetImage();
  472. screen_info.width = params.width;
  473. screen_info.height = params.height;
  474. screen_info.is_srgb = surface->GetSurfaceParams().srgb_conversion;
  475. return true;
  476. }
  477. void RasterizerVulkan::SetupDirtyFlags() {
  478. state_tracker.Initialize();
  479. }
  480. void RasterizerVulkan::FlushWork() {
  481. static constexpr u32 DRAWS_TO_DISPATCH = 4096;
  482. // Only check multiples of 8 draws
  483. static_assert(DRAWS_TO_DISPATCH % 8 == 0);
  484. if ((++draw_counter & 7) != 7) {
  485. return;
  486. }
  487. if (draw_counter < DRAWS_TO_DISPATCH) {
  488. // Send recorded tasks to the worker thread
  489. scheduler.DispatchWork();
  490. return;
  491. }
  492. // Otherwise (every certain number of draws) flush execution.
  493. // This submits commands to the Vulkan driver.
  494. scheduler.Flush();
  495. draw_counter = 0;
  496. }
  497. RasterizerVulkan::Texceptions RasterizerVulkan::UpdateAttachments() {
  498. MICROPROFILE_SCOPE(Vulkan_RenderTargets);
  499. auto& dirty = system.GPU().Maxwell3D().dirty.flags;
  500. const bool update_rendertargets = dirty[VideoCommon::Dirty::RenderTargets];
  501. dirty[VideoCommon::Dirty::RenderTargets] = false;
  502. texture_cache.GuardRenderTargets(true);
  503. Texceptions texceptions;
  504. for (std::size_t rt = 0; rt < Maxwell::NumRenderTargets; ++rt) {
  505. if (update_rendertargets) {
  506. color_attachments[rt] = texture_cache.GetColorBufferSurface(rt, true);
  507. }
  508. if (color_attachments[rt] && WalkAttachmentOverlaps(*color_attachments[rt])) {
  509. texceptions[rt] = true;
  510. }
  511. }
  512. if (update_rendertargets) {
  513. zeta_attachment = texture_cache.GetDepthBufferSurface(true);
  514. }
  515. if (zeta_attachment && WalkAttachmentOverlaps(*zeta_attachment)) {
  516. texceptions[ZETA_TEXCEPTION_INDEX] = true;
  517. }
  518. texture_cache.GuardRenderTargets(false);
  519. return texceptions;
  520. }
  521. bool RasterizerVulkan::WalkAttachmentOverlaps(const CachedSurfaceView& attachment) {
  522. bool overlap = false;
  523. for (auto& [view, layout] : sampled_views) {
  524. if (!attachment.IsSameSurface(*view)) {
  525. continue;
  526. }
  527. overlap = true;
  528. *layout = vk::ImageLayout::eGeneral;
  529. }
  530. return overlap;
  531. }
  532. std::tuple<vk::Framebuffer, vk::Extent2D> RasterizerVulkan::ConfigureFramebuffers(
  533. vk::RenderPass renderpass) {
  534. FramebufferCacheKey key{renderpass, std::numeric_limits<u32>::max(),
  535. std::numeric_limits<u32>::max(), std::numeric_limits<u32>::max()};
  536. const auto try_push = [&](const View& view) {
  537. if (!view) {
  538. return false;
  539. }
  540. key.views.push_back(view->GetHandle());
  541. key.width = std::min(key.width, view->GetWidth());
  542. key.height = std::min(key.height, view->GetHeight());
  543. key.layers = std::min(key.layers, view->GetNumLayers());
  544. return true;
  545. };
  546. for (std::size_t index = 0; index < std::size(color_attachments); ++index) {
  547. if (try_push(color_attachments[index])) {
  548. texture_cache.MarkColorBufferInUse(index);
  549. }
  550. }
  551. if (try_push(zeta_attachment)) {
  552. texture_cache.MarkDepthBufferInUse();
  553. }
  554. const auto [fbentry, is_cache_miss] = framebuffer_cache.try_emplace(key);
  555. auto& framebuffer = fbentry->second;
  556. if (is_cache_miss) {
  557. const vk::FramebufferCreateInfo framebuffer_ci(
  558. {}, key.renderpass, static_cast<u32>(key.views.size()), key.views.data(), key.width,
  559. key.height, key.layers);
  560. const auto dev = device.GetLogical();
  561. const auto& dld = device.GetDispatchLoader();
  562. framebuffer = dev.createFramebufferUnique(framebuffer_ci, nullptr, dld);
  563. }
  564. return {*framebuffer, vk::Extent2D{key.width, key.height}};
  565. }
  566. RasterizerVulkan::DrawParameters RasterizerVulkan::SetupGeometry(FixedPipelineState& fixed_state,
  567. BufferBindings& buffer_bindings,
  568. bool is_indexed,
  569. bool is_instanced) {
  570. MICROPROFILE_SCOPE(Vulkan_Geometry);
  571. const auto& gpu = system.GPU().Maxwell3D();
  572. const auto& regs = gpu.regs;
  573. SetupVertexArrays(fixed_state.vertex_input, buffer_bindings);
  574. const u32 base_instance = regs.vb_base_instance;
  575. const u32 num_instances = is_instanced ? gpu.mme_draw.instance_count : 1;
  576. const u32 base_vertex = is_indexed ? regs.vb_element_base : regs.vertex_buffer.first;
  577. const u32 num_vertices = is_indexed ? regs.index_array.count : regs.vertex_buffer.count;
  578. DrawParameters params{base_instance, num_instances, base_vertex, num_vertices, is_indexed};
  579. SetupIndexBuffer(buffer_bindings, params, is_indexed);
  580. return params;
  581. }
  582. void RasterizerVulkan::SetupShaderDescriptors(
  583. const std::array<Shader, Maxwell::MaxShaderProgram>& shaders) {
  584. texture_cache.GuardSamplers(true);
  585. for (std::size_t stage = 0; stage < Maxwell::MaxShaderStage; ++stage) {
  586. // Skip VertexA stage
  587. const auto& shader = shaders[stage + 1];
  588. if (!shader) {
  589. continue;
  590. }
  591. const auto& entries = shader->GetEntries();
  592. SetupGraphicsConstBuffers(entries, stage);
  593. SetupGraphicsGlobalBuffers(entries, stage);
  594. SetupGraphicsTexelBuffers(entries, stage);
  595. SetupGraphicsTextures(entries, stage);
  596. SetupGraphicsImages(entries, stage);
  597. }
  598. texture_cache.GuardSamplers(false);
  599. }
  600. void RasterizerVulkan::SetupImageTransitions(
  601. Texceptions texceptions, const std::array<View, Maxwell::NumRenderTargets>& color_attachments,
  602. const View& zeta_attachment) {
  603. TransitionImages(sampled_views, vk::PipelineStageFlagBits::eAllGraphics,
  604. vk::AccessFlagBits::eShaderRead);
  605. TransitionImages(image_views, vk::PipelineStageFlagBits::eAllGraphics,
  606. vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite);
  607. for (std::size_t rt = 0; rt < std::size(color_attachments); ++rt) {
  608. const auto color_attachment = color_attachments[rt];
  609. if (color_attachment == nullptr) {
  610. continue;
  611. }
  612. const auto image_layout =
  613. texceptions[rt] ? vk::ImageLayout::eGeneral : vk::ImageLayout::eColorAttachmentOptimal;
  614. color_attachment->Transition(
  615. image_layout, vk::PipelineStageFlagBits::eColorAttachmentOutput,
  616. vk::AccessFlagBits::eColorAttachmentRead | vk::AccessFlagBits::eColorAttachmentWrite);
  617. }
  618. if (zeta_attachment != nullptr) {
  619. const auto image_layout = texceptions[ZETA_TEXCEPTION_INDEX]
  620. ? vk::ImageLayout::eGeneral
  621. : vk::ImageLayout::eDepthStencilAttachmentOptimal;
  622. zeta_attachment->Transition(image_layout, vk::PipelineStageFlagBits::eLateFragmentTests,
  623. vk::AccessFlagBits::eDepthStencilAttachmentRead |
  624. vk::AccessFlagBits::eDepthStencilAttachmentWrite);
  625. }
  626. }
  627. void RasterizerVulkan::UpdateDynamicStates() {
  628. auto& regs = system.GPU().Maxwell3D().regs;
  629. UpdateViewportsState(regs);
  630. UpdateScissorsState(regs);
  631. UpdateDepthBias(regs);
  632. UpdateBlendConstants(regs);
  633. UpdateDepthBounds(regs);
  634. UpdateStencilFaces(regs);
  635. }
  636. void RasterizerVulkan::BeginTransformFeedback() {
  637. const auto& regs = system.GPU().Maxwell3D().regs;
  638. if (regs.tfb_enabled == 0) {
  639. return;
  640. }
  641. UNIMPLEMENTED_IF(regs.IsShaderConfigEnabled(Maxwell::ShaderProgram::TesselationControl) ||
  642. regs.IsShaderConfigEnabled(Maxwell::ShaderProgram::TesselationEval) ||
  643. regs.IsShaderConfigEnabled(Maxwell::ShaderProgram::Geometry));
  644. UNIMPLEMENTED_IF(regs.tfb_bindings[1].buffer_enable);
  645. UNIMPLEMENTED_IF(regs.tfb_bindings[2].buffer_enable);
  646. UNIMPLEMENTED_IF(regs.tfb_bindings[3].buffer_enable);
  647. const auto& binding = regs.tfb_bindings[0];
  648. UNIMPLEMENTED_IF(binding.buffer_enable == 0);
  649. UNIMPLEMENTED_IF(binding.buffer_offset != 0);
  650. const GPUVAddr gpu_addr = binding.Address();
  651. const std::size_t size = binding.buffer_size;
  652. const auto [buffer, offset] = buffer_cache.UploadMemory(gpu_addr, size, 4, true);
  653. scheduler.Record([buffer = *buffer, offset = offset, size](auto cmdbuf, auto& dld) {
  654. cmdbuf.bindTransformFeedbackBuffersEXT(0, {buffer}, {offset}, {size}, dld);
  655. cmdbuf.beginTransformFeedbackEXT(0, {}, {}, dld);
  656. });
  657. }
  658. void RasterizerVulkan::EndTransformFeedback() {
  659. const auto& regs = system.GPU().Maxwell3D().regs;
  660. if (regs.tfb_enabled == 0) {
  661. return;
  662. }
  663. scheduler.Record(
  664. [](auto cmdbuf, auto& dld) { cmdbuf.endTransformFeedbackEXT(0, {}, {}, dld); });
  665. }
  666. void RasterizerVulkan::SetupVertexArrays(FixedPipelineState::VertexInput& vertex_input,
  667. BufferBindings& buffer_bindings) {
  668. const auto& regs = system.GPU().Maxwell3D().regs;
  669. for (u32 index = 0; index < static_cast<u32>(Maxwell::NumVertexAttributes); ++index) {
  670. const auto& attrib = regs.vertex_attrib_format[index];
  671. if (!attrib.IsValid()) {
  672. continue;
  673. }
  674. const auto& buffer = regs.vertex_array[attrib.buffer];
  675. ASSERT(buffer.IsEnabled());
  676. vertex_input.attributes[vertex_input.num_attributes++] =
  677. FixedPipelineState::VertexAttribute(index, attrib.buffer, attrib.type, attrib.size,
  678. attrib.offset);
  679. }
  680. for (u32 index = 0; index < static_cast<u32>(Maxwell::NumVertexArrays); ++index) {
  681. const auto& vertex_array = regs.vertex_array[index];
  682. if (!vertex_array.IsEnabled()) {
  683. continue;
  684. }
  685. const GPUVAddr start{vertex_array.StartAddress()};
  686. const GPUVAddr end{regs.vertex_array_limit[index].LimitAddress()};
  687. ASSERT(end > start);
  688. const std::size_t size{end - start + 1};
  689. const auto [buffer, offset] = buffer_cache.UploadMemory(start, size);
  690. vertex_input.bindings[vertex_input.num_bindings++] = FixedPipelineState::VertexBinding(
  691. index, vertex_array.stride,
  692. regs.instanced_arrays.IsInstancingEnabled(index) ? vertex_array.divisor : 0);
  693. buffer_bindings.AddVertexBinding(buffer, offset);
  694. }
  695. }
  696. void RasterizerVulkan::SetupIndexBuffer(BufferBindings& buffer_bindings, DrawParameters& params,
  697. bool is_indexed) {
  698. const auto& regs = system.GPU().Maxwell3D().regs;
  699. switch (regs.draw.topology) {
  700. case Maxwell::PrimitiveTopology::Quads:
  701. if (params.is_indexed) {
  702. UNIMPLEMENTED();
  703. } else {
  704. const auto [buffer, offset] =
  705. quad_array_pass.Assemble(params.num_vertices, params.base_vertex);
  706. buffer_bindings.SetIndexBinding(&buffer, offset, vk::IndexType::eUint32);
  707. params.base_vertex = 0;
  708. params.num_vertices = params.num_vertices * 6 / 4;
  709. params.is_indexed = true;
  710. }
  711. break;
  712. default: {
  713. if (!is_indexed) {
  714. break;
  715. }
  716. const GPUVAddr gpu_addr = regs.index_array.IndexStart();
  717. auto [buffer, offset] = buffer_cache.UploadMemory(gpu_addr, CalculateIndexBufferSize());
  718. auto format = regs.index_array.format;
  719. const bool is_uint8 = format == Maxwell::IndexFormat::UnsignedByte;
  720. if (is_uint8 && !device.IsExtIndexTypeUint8Supported()) {
  721. std::tie(buffer, offset) = uint8_pass.Assemble(params.num_vertices, *buffer, offset);
  722. format = Maxwell::IndexFormat::UnsignedShort;
  723. }
  724. buffer_bindings.SetIndexBinding(buffer, offset, MaxwellToVK::IndexFormat(device, format));
  725. break;
  726. }
  727. }
  728. }
  729. void RasterizerVulkan::SetupGraphicsConstBuffers(const ShaderEntries& entries, std::size_t stage) {
  730. MICROPROFILE_SCOPE(Vulkan_ConstBuffers);
  731. const auto& gpu = system.GPU().Maxwell3D();
  732. const auto& shader_stage = gpu.state.shader_stages[stage];
  733. for (const auto& entry : entries.const_buffers) {
  734. SetupConstBuffer(entry, shader_stage.const_buffers[entry.GetIndex()]);
  735. }
  736. }
  737. void RasterizerVulkan::SetupGraphicsGlobalBuffers(const ShaderEntries& entries, std::size_t stage) {
  738. MICROPROFILE_SCOPE(Vulkan_GlobalBuffers);
  739. auto& gpu{system.GPU()};
  740. const auto cbufs{gpu.Maxwell3D().state.shader_stages[stage]};
  741. for (const auto& entry : entries.global_buffers) {
  742. const auto addr = cbufs.const_buffers[entry.GetCbufIndex()].address + entry.GetCbufOffset();
  743. SetupGlobalBuffer(entry, addr);
  744. }
  745. }
  746. void RasterizerVulkan::SetupGraphicsTexelBuffers(const ShaderEntries& entries, std::size_t stage) {
  747. MICROPROFILE_SCOPE(Vulkan_Textures);
  748. const auto& gpu = system.GPU().Maxwell3D();
  749. for (const auto& entry : entries.texel_buffers) {
  750. const auto image = GetTextureInfo(gpu, entry, stage).tic;
  751. SetupTexelBuffer(image, entry);
  752. }
  753. }
  754. void RasterizerVulkan::SetupGraphicsTextures(const ShaderEntries& entries, std::size_t stage) {
  755. MICROPROFILE_SCOPE(Vulkan_Textures);
  756. const auto& gpu = system.GPU().Maxwell3D();
  757. for (const auto& entry : entries.samplers) {
  758. for (std::size_t i = 0; i < entry.Size(); ++i) {
  759. const auto texture = GetTextureInfo(gpu, entry, stage, i);
  760. SetupTexture(texture, entry);
  761. }
  762. }
  763. }
  764. void RasterizerVulkan::SetupGraphicsImages(const ShaderEntries& entries, std::size_t stage) {
  765. MICROPROFILE_SCOPE(Vulkan_Images);
  766. const auto& gpu = system.GPU().Maxwell3D();
  767. for (const auto& entry : entries.images) {
  768. const auto tic = GetTextureInfo(gpu, entry, stage).tic;
  769. SetupImage(tic, entry);
  770. }
  771. }
  772. void RasterizerVulkan::SetupComputeConstBuffers(const ShaderEntries& entries) {
  773. MICROPROFILE_SCOPE(Vulkan_ConstBuffers);
  774. const auto& launch_desc = system.GPU().KeplerCompute().launch_description;
  775. for (const auto& entry : entries.const_buffers) {
  776. const auto& config = launch_desc.const_buffer_config[entry.GetIndex()];
  777. const std::bitset<8> mask = launch_desc.const_buffer_enable_mask.Value();
  778. Tegra::Engines::ConstBufferInfo buffer;
  779. buffer.address = config.Address();
  780. buffer.size = config.size;
  781. buffer.enabled = mask[entry.GetIndex()];
  782. SetupConstBuffer(entry, buffer);
  783. }
  784. }
  785. void RasterizerVulkan::SetupComputeGlobalBuffers(const ShaderEntries& entries) {
  786. MICROPROFILE_SCOPE(Vulkan_GlobalBuffers);
  787. const auto cbufs{system.GPU().KeplerCompute().launch_description.const_buffer_config};
  788. for (const auto& entry : entries.global_buffers) {
  789. const auto addr{cbufs[entry.GetCbufIndex()].Address() + entry.GetCbufOffset()};
  790. SetupGlobalBuffer(entry, addr);
  791. }
  792. }
  793. void RasterizerVulkan::SetupComputeTexelBuffers(const ShaderEntries& entries) {
  794. MICROPROFILE_SCOPE(Vulkan_Textures);
  795. const auto& gpu = system.GPU().KeplerCompute();
  796. for (const auto& entry : entries.texel_buffers) {
  797. const auto image = GetTextureInfo(gpu, entry, ComputeShaderIndex).tic;
  798. SetupTexelBuffer(image, entry);
  799. }
  800. }
  801. void RasterizerVulkan::SetupComputeTextures(const ShaderEntries& entries) {
  802. MICROPROFILE_SCOPE(Vulkan_Textures);
  803. const auto& gpu = system.GPU().KeplerCompute();
  804. for (const auto& entry : entries.samplers) {
  805. for (std::size_t i = 0; i < entry.Size(); ++i) {
  806. const auto texture = GetTextureInfo(gpu, entry, ComputeShaderIndex, i);
  807. SetupTexture(texture, entry);
  808. }
  809. }
  810. }
  811. void RasterizerVulkan::SetupComputeImages(const ShaderEntries& entries) {
  812. MICROPROFILE_SCOPE(Vulkan_Images);
  813. const auto& gpu = system.GPU().KeplerCompute();
  814. for (const auto& entry : entries.images) {
  815. const auto tic = GetTextureInfo(gpu, entry, ComputeShaderIndex).tic;
  816. SetupImage(tic, entry);
  817. }
  818. }
  819. void RasterizerVulkan::SetupConstBuffer(const ConstBufferEntry& entry,
  820. const Tegra::Engines::ConstBufferInfo& buffer) {
  821. if (!buffer.enabled) {
  822. // Set values to zero to unbind buffers
  823. update_descriptor_queue.AddBuffer(buffer_cache.GetEmptyBuffer(sizeof(float)), 0,
  824. sizeof(float));
  825. return;
  826. }
  827. // Align the size to avoid bad std140 interactions
  828. const std::size_t size =
  829. Common::AlignUp(CalculateConstBufferSize(entry, buffer), 4 * sizeof(float));
  830. ASSERT(size <= MaxConstbufferSize);
  831. const auto [buffer_handle, offset] =
  832. buffer_cache.UploadMemory(buffer.address, size, device.GetUniformBufferAlignment());
  833. update_descriptor_queue.AddBuffer(buffer_handle, offset, size);
  834. }
  835. void RasterizerVulkan::SetupGlobalBuffer(const GlobalBufferEntry& entry, GPUVAddr address) {
  836. auto& memory_manager{system.GPU().MemoryManager()};
  837. const auto actual_addr = memory_manager.Read<u64>(address);
  838. const auto size = memory_manager.Read<u32>(address + 8);
  839. if (size == 0) {
  840. // Sometimes global memory pointers don't have a proper size. Upload a dummy entry because
  841. // Vulkan doesn't like empty buffers.
  842. constexpr std::size_t dummy_size = 4;
  843. const auto buffer = buffer_cache.GetEmptyBuffer(dummy_size);
  844. update_descriptor_queue.AddBuffer(buffer, 0, dummy_size);
  845. return;
  846. }
  847. const auto [buffer, offset] = buffer_cache.UploadMemory(
  848. actual_addr, size, device.GetStorageBufferAlignment(), entry.IsWritten());
  849. update_descriptor_queue.AddBuffer(buffer, offset, size);
  850. }
  851. void RasterizerVulkan::SetupTexelBuffer(const Tegra::Texture::TICEntry& tic,
  852. const TexelBufferEntry& entry) {
  853. const auto view = texture_cache.GetTextureSurface(tic, entry);
  854. ASSERT(view->IsBufferView());
  855. update_descriptor_queue.AddTexelBuffer(view->GetBufferView());
  856. }
  857. void RasterizerVulkan::SetupTexture(const Tegra::Texture::FullTextureInfo& texture,
  858. const SamplerEntry& entry) {
  859. auto view = texture_cache.GetTextureSurface(texture.tic, entry);
  860. ASSERT(!view->IsBufferView());
  861. const auto image_view = view->GetHandle(texture.tic.x_source, texture.tic.y_source,
  862. texture.tic.z_source, texture.tic.w_source);
  863. const auto sampler = sampler_cache.GetSampler(texture.tsc);
  864. update_descriptor_queue.AddSampledImage(sampler, image_view);
  865. const auto image_layout = update_descriptor_queue.GetLastImageLayout();
  866. *image_layout = vk::ImageLayout::eShaderReadOnlyOptimal;
  867. sampled_views.push_back(ImageView{std::move(view), image_layout});
  868. }
  869. void RasterizerVulkan::SetupImage(const Tegra::Texture::TICEntry& tic, const ImageEntry& entry) {
  870. auto view = texture_cache.GetImageSurface(tic, entry);
  871. if (entry.IsWritten()) {
  872. view->MarkAsModified(texture_cache.Tick());
  873. }
  874. UNIMPLEMENTED_IF(tic.IsBuffer());
  875. const auto image_view = view->GetHandle(tic.x_source, tic.y_source, tic.z_source, tic.w_source);
  876. update_descriptor_queue.AddImage(image_view);
  877. const auto image_layout = update_descriptor_queue.GetLastImageLayout();
  878. *image_layout = vk::ImageLayout::eGeneral;
  879. image_views.push_back(ImageView{std::move(view), image_layout});
  880. }
  881. void RasterizerVulkan::UpdateViewportsState(Tegra::Engines::Maxwell3D::Regs& regs) {
  882. if (!state_tracker.TouchViewports()) {
  883. return;
  884. }
  885. const std::array viewports{
  886. GetViewportState(device, regs, 0), GetViewportState(device, regs, 1),
  887. GetViewportState(device, regs, 2), GetViewportState(device, regs, 3),
  888. GetViewportState(device, regs, 4), GetViewportState(device, regs, 5),
  889. GetViewportState(device, regs, 6), GetViewportState(device, regs, 7),
  890. GetViewportState(device, regs, 8), GetViewportState(device, regs, 9),
  891. GetViewportState(device, regs, 10), GetViewportState(device, regs, 11),
  892. GetViewportState(device, regs, 12), GetViewportState(device, regs, 13),
  893. GetViewportState(device, regs, 14), GetViewportState(device, regs, 15)};
  894. scheduler.Record([viewports](auto cmdbuf, auto& dld) {
  895. cmdbuf.setViewport(0, static_cast<u32>(viewports.size()), viewports.data(), dld);
  896. });
  897. }
  898. void RasterizerVulkan::UpdateScissorsState(Tegra::Engines::Maxwell3D::Regs& regs) {
  899. if (!state_tracker.TouchScissors()) {
  900. return;
  901. }
  902. const std::array scissors = {
  903. GetScissorState(regs, 0), GetScissorState(regs, 1), GetScissorState(regs, 2),
  904. GetScissorState(regs, 3), GetScissorState(regs, 4), GetScissorState(regs, 5),
  905. GetScissorState(regs, 6), GetScissorState(regs, 7), GetScissorState(regs, 8),
  906. GetScissorState(regs, 9), GetScissorState(regs, 10), GetScissorState(regs, 11),
  907. GetScissorState(regs, 12), GetScissorState(regs, 13), GetScissorState(regs, 14),
  908. GetScissorState(regs, 15)};
  909. scheduler.Record([scissors](auto cmdbuf, auto& dld) {
  910. cmdbuf.setScissor(0, static_cast<u32>(scissors.size()), scissors.data(), dld);
  911. });
  912. }
  913. void RasterizerVulkan::UpdateDepthBias(Tegra::Engines::Maxwell3D::Regs& regs) {
  914. if (!state_tracker.TouchDepthBias()) {
  915. return;
  916. }
  917. scheduler.Record([constant = regs.polygon_offset_units, clamp = regs.polygon_offset_clamp,
  918. factor = regs.polygon_offset_factor](auto cmdbuf, auto& dld) {
  919. cmdbuf.setDepthBias(constant, clamp, factor / 2.0f, dld);
  920. });
  921. }
  922. void RasterizerVulkan::UpdateBlendConstants(Tegra::Engines::Maxwell3D::Regs& regs) {
  923. if (!state_tracker.TouchBlendConstants()) {
  924. return;
  925. }
  926. const std::array blend_color = {regs.blend_color.r, regs.blend_color.g, regs.blend_color.b,
  927. regs.blend_color.a};
  928. scheduler.Record([blend_color](auto cmdbuf, auto& dld) {
  929. cmdbuf.setBlendConstants(blend_color.data(), dld);
  930. });
  931. }
  932. void RasterizerVulkan::UpdateDepthBounds(Tegra::Engines::Maxwell3D::Regs& regs) {
  933. if (!state_tracker.TouchDepthBounds()) {
  934. return;
  935. }
  936. scheduler.Record([min = regs.depth_bounds[0], max = regs.depth_bounds[1]](
  937. auto cmdbuf, auto& dld) { cmdbuf.setDepthBounds(min, max, dld); });
  938. }
  939. void RasterizerVulkan::UpdateStencilFaces(Tegra::Engines::Maxwell3D::Regs& regs) {
  940. if (!state_tracker.TouchStencilProperties()) {
  941. return;
  942. }
  943. if (regs.stencil_two_side_enable) {
  944. // Separate values per face
  945. scheduler.Record(
  946. [front_ref = regs.stencil_front_func_ref, front_write_mask = regs.stencil_front_mask,
  947. front_test_mask = regs.stencil_front_func_mask, back_ref = regs.stencil_back_func_ref,
  948. back_write_mask = regs.stencil_back_mask,
  949. back_test_mask = regs.stencil_back_func_mask](auto cmdbuf, auto& dld) {
  950. // Front face
  951. cmdbuf.setStencilReference(vk::StencilFaceFlagBits::eFront, front_ref, dld);
  952. cmdbuf.setStencilWriteMask(vk::StencilFaceFlagBits::eFront, front_write_mask, dld);
  953. cmdbuf.setStencilCompareMask(vk::StencilFaceFlagBits::eFront, front_test_mask, dld);
  954. // Back face
  955. cmdbuf.setStencilReference(vk::StencilFaceFlagBits::eBack, back_ref, dld);
  956. cmdbuf.setStencilWriteMask(vk::StencilFaceFlagBits::eBack, back_write_mask, dld);
  957. cmdbuf.setStencilCompareMask(vk::StencilFaceFlagBits::eBack, back_test_mask, dld);
  958. });
  959. } else {
  960. // Front face defines both faces
  961. scheduler.Record([ref = regs.stencil_back_func_ref, write_mask = regs.stencil_back_mask,
  962. test_mask = regs.stencil_back_func_mask](auto cmdbuf, auto& dld) {
  963. cmdbuf.setStencilReference(vk::StencilFaceFlagBits::eFrontAndBack, ref, dld);
  964. cmdbuf.setStencilWriteMask(vk::StencilFaceFlagBits::eFrontAndBack, write_mask, dld);
  965. cmdbuf.setStencilCompareMask(vk::StencilFaceFlagBits::eFrontAndBack, test_mask, dld);
  966. });
  967. }
  968. }
  969. std::size_t RasterizerVulkan::CalculateGraphicsStreamBufferSize(bool is_indexed) const {
  970. std::size_t size = CalculateVertexArraysSize();
  971. if (is_indexed) {
  972. size = Common::AlignUp(size, 4) + CalculateIndexBufferSize();
  973. }
  974. size += Maxwell::MaxConstBuffers * (MaxConstbufferSize + device.GetUniformBufferAlignment());
  975. return size;
  976. }
  977. std::size_t RasterizerVulkan::CalculateComputeStreamBufferSize() const {
  978. return Tegra::Engines::KeplerCompute::NumConstBuffers *
  979. (Maxwell::MaxConstBufferSize + device.GetUniformBufferAlignment());
  980. }
  981. std::size_t RasterizerVulkan::CalculateVertexArraysSize() const {
  982. const auto& regs = system.GPU().Maxwell3D().regs;
  983. std::size_t size = 0;
  984. for (u32 index = 0; index < Maxwell::NumVertexArrays; ++index) {
  985. // This implementation assumes that all attributes are used in the shader.
  986. const GPUVAddr start{regs.vertex_array[index].StartAddress()};
  987. const GPUVAddr end{regs.vertex_array_limit[index].LimitAddress()};
  988. DEBUG_ASSERT(end >= start);
  989. size += (end - start + 1) * regs.vertex_array[index].enable;
  990. }
  991. return size;
  992. }
  993. std::size_t RasterizerVulkan::CalculateIndexBufferSize() const {
  994. const auto& regs = system.GPU().Maxwell3D().regs;
  995. return static_cast<std::size_t>(regs.index_array.count) *
  996. static_cast<std::size_t>(regs.index_array.FormatSizeInBytes());
  997. }
  998. std::size_t RasterizerVulkan::CalculateConstBufferSize(
  999. const ConstBufferEntry& entry, const Tegra::Engines::ConstBufferInfo& buffer) const {
  1000. if (entry.IsIndirect()) {
  1001. // Buffer is accessed indirectly, so upload the entire thing
  1002. return buffer.size;
  1003. } else {
  1004. // Buffer is accessed directly, upload just what we use
  1005. return entry.GetSize();
  1006. }
  1007. }
  1008. RenderPassParams RasterizerVulkan::GetRenderPassParams(Texceptions texceptions) const {
  1009. using namespace VideoCore::Surface;
  1010. const auto& regs = system.GPU().Maxwell3D().regs;
  1011. RenderPassParams renderpass_params;
  1012. for (std::size_t rt = 0; rt < static_cast<std::size_t>(regs.rt_control.count); ++rt) {
  1013. const auto& rendertarget = regs.rt[rt];
  1014. if (rendertarget.Address() == 0 || rendertarget.format == Tegra::RenderTargetFormat::NONE) {
  1015. continue;
  1016. }
  1017. renderpass_params.color_attachments.push_back(RenderPassParams::ColorAttachment{
  1018. static_cast<u32>(rt), PixelFormatFromRenderTargetFormat(rendertarget.format),
  1019. texceptions[rt]});
  1020. }
  1021. renderpass_params.has_zeta = regs.zeta_enable;
  1022. if (renderpass_params.has_zeta) {
  1023. renderpass_params.zeta_pixel_format = PixelFormatFromDepthFormat(regs.zeta.format);
  1024. renderpass_params.zeta_texception = texceptions[ZETA_TEXCEPTION_INDEX];
  1025. }
  1026. return renderpass_params;
  1027. }
  1028. } // namespace Vulkan