vk_rasterizer.cpp 48 KB

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