vk_rasterizer.cpp 50 KB

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