vk_rasterizer.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. // SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <array>
  5. #include <memory>
  6. #include <mutex>
  7. #include "common/assert.h"
  8. #include "common/logging/log.h"
  9. #include "common/microprofile.h"
  10. #include "common/scope_exit.h"
  11. #include "common/settings.h"
  12. #include "video_core/control/channel_state.h"
  13. #include "video_core/engines/kepler_compute.h"
  14. #include "video_core/engines/maxwell_3d.h"
  15. #include "video_core/renderer_vulkan/blit_image.h"
  16. #include "video_core/renderer_vulkan/fixed_pipeline_state.h"
  17. #include "video_core/renderer_vulkan/maxwell_to_vk.h"
  18. #include "video_core/renderer_vulkan/renderer_vulkan.h"
  19. #include "video_core/renderer_vulkan/vk_buffer_cache.h"
  20. #include "video_core/renderer_vulkan/vk_compute_pipeline.h"
  21. #include "video_core/renderer_vulkan/vk_descriptor_pool.h"
  22. #include "video_core/renderer_vulkan/vk_pipeline_cache.h"
  23. #include "video_core/renderer_vulkan/vk_rasterizer.h"
  24. #include "video_core/renderer_vulkan/vk_scheduler.h"
  25. #include "video_core/renderer_vulkan/vk_staging_buffer_pool.h"
  26. #include "video_core/renderer_vulkan/vk_state_tracker.h"
  27. #include "video_core/renderer_vulkan/vk_texture_cache.h"
  28. #include "video_core/renderer_vulkan/vk_update_descriptor.h"
  29. #include "video_core/shader_cache.h"
  30. #include "video_core/texture_cache/texture_cache_base.h"
  31. #include "video_core/vulkan_common/vulkan_device.h"
  32. #include "video_core/vulkan_common/vulkan_wrapper.h"
  33. namespace Vulkan {
  34. using Maxwell = Tegra::Engines::Maxwell3D::Regs;
  35. using VideoCommon::ImageViewId;
  36. using VideoCommon::ImageViewType;
  37. MICROPROFILE_DEFINE(Vulkan_WaitForWorker, "Vulkan", "Wait for worker", MP_RGB(255, 192, 192));
  38. MICROPROFILE_DEFINE(Vulkan_Drawing, "Vulkan", "Record drawing", MP_RGB(192, 128, 128));
  39. MICROPROFILE_DEFINE(Vulkan_Compute, "Vulkan", "Record compute", MP_RGB(192, 128, 128));
  40. MICROPROFILE_DEFINE(Vulkan_Clearing, "Vulkan", "Record clearing", MP_RGB(192, 128, 128));
  41. MICROPROFILE_DEFINE(Vulkan_PipelineCache, "Vulkan", "Pipeline cache", MP_RGB(192, 128, 128));
  42. namespace {
  43. struct DrawParams {
  44. u32 base_instance;
  45. u32 num_instances;
  46. u32 base_vertex;
  47. u32 num_vertices;
  48. u32 first_index;
  49. bool is_indexed;
  50. };
  51. VkViewport GetViewportState(const Device& device, const Maxwell& regs, size_t index, float scale) {
  52. const auto& src = regs.viewport_transform[index];
  53. const auto conv = [scale](float value) {
  54. float new_value = value * scale;
  55. if (scale < 1.0f) {
  56. const bool sign = std::signbit(value);
  57. new_value = std::round(std::abs(new_value));
  58. new_value = sign ? -new_value : new_value;
  59. }
  60. return new_value;
  61. };
  62. const float x = conv(src.translate_x - src.scale_x);
  63. const float width = conv(src.scale_x * 2.0f);
  64. float y = conv(src.translate_y - src.scale_y);
  65. float height = conv(src.scale_y * 2.0f);
  66. bool y_negate = regs.window_origin.mode != Maxwell::WindowOrigin::Mode::UpperLeft;
  67. if (!device.IsNvViewportSwizzleSupported()) {
  68. y_negate = y_negate != (src.swizzle.y == Maxwell::ViewportSwizzle::NegativeY);
  69. }
  70. if (y_negate) {
  71. y += height;
  72. height = -height;
  73. }
  74. const float reduce_z = regs.depth_mode == Maxwell::DepthMode::MinusOneToOne ? 1.0f : 0.0f;
  75. VkViewport viewport{
  76. .x = x,
  77. .y = y,
  78. .width = width != 0.0f ? width : 1.0f,
  79. .height = height != 0.0f ? height : 1.0f,
  80. .minDepth = src.translate_z - src.scale_z * reduce_z,
  81. .maxDepth = src.translate_z + src.scale_z,
  82. };
  83. if (!device.IsExtDepthRangeUnrestrictedSupported()) {
  84. viewport.minDepth = std::clamp(viewport.minDepth, 0.0f, 1.0f);
  85. viewport.maxDepth = std::clamp(viewport.maxDepth, 0.0f, 1.0f);
  86. }
  87. return viewport;
  88. }
  89. VkRect2D GetScissorState(const Maxwell& regs, size_t index, u32 up_scale = 1, u32 down_shift = 0) {
  90. const auto& src = regs.scissor_test[index];
  91. VkRect2D scissor;
  92. const auto scale_up = [&](s32 value) -> s32 {
  93. if (value == 0) {
  94. return 0U;
  95. }
  96. const s32 upset = value * up_scale;
  97. s32 acumm = 0;
  98. if ((up_scale >> down_shift) == 0) {
  99. acumm = upset % 2;
  100. }
  101. const s32 converted_value = (value * up_scale) >> down_shift;
  102. return value < 0 ? std::min<s32>(converted_value - acumm, -1)
  103. : std::max<s32>(converted_value + acumm, 1);
  104. };
  105. if (src.enable) {
  106. scissor.offset.x = scale_up(static_cast<s32>(src.min_x));
  107. scissor.offset.y = scale_up(static_cast<s32>(src.min_y));
  108. scissor.extent.width = scale_up(src.max_x - src.min_x);
  109. scissor.extent.height = scale_up(src.max_y - src.min_y);
  110. } else {
  111. scissor.offset.x = 0;
  112. scissor.offset.y = 0;
  113. scissor.extent.width = std::numeric_limits<s32>::max();
  114. scissor.extent.height = std::numeric_limits<s32>::max();
  115. }
  116. return scissor;
  117. }
  118. DrawParams MakeDrawParams(const Maxwell& regs, u32 num_instances, bool is_instanced,
  119. bool is_indexed) {
  120. DrawParams params{
  121. .base_instance = regs.global_base_instance_index,
  122. .num_instances = is_instanced ? num_instances : 1,
  123. .base_vertex = is_indexed ? regs.global_base_vertex_index : regs.vertex_buffer.first,
  124. .num_vertices = is_indexed ? regs.index_buffer.count : regs.vertex_buffer.count,
  125. .first_index = is_indexed ? regs.index_buffer.first : 0,
  126. .is_indexed = is_indexed,
  127. };
  128. if (regs.draw.topology == Maxwell::PrimitiveTopology::Quads) {
  129. // 6 triangle vertices per quad, base vertex is part of the index
  130. // See BindQuadArrayIndexBuffer for more details
  131. params.num_vertices = (params.num_vertices / 4) * 6;
  132. params.base_vertex = 0;
  133. params.is_indexed = true;
  134. }
  135. return params;
  136. }
  137. } // Anonymous namespace
  138. RasterizerVulkan::RasterizerVulkan(Core::Frontend::EmuWindow& emu_window_, Tegra::GPU& gpu_,
  139. Core::Memory::Memory& cpu_memory_, ScreenInfo& screen_info_,
  140. const Device& device_, MemoryAllocator& memory_allocator_,
  141. StateTracker& state_tracker_, Scheduler& scheduler_)
  142. : RasterizerAccelerated{cpu_memory_}, gpu{gpu_}, screen_info{screen_info_}, device{device_},
  143. memory_allocator{memory_allocator_}, state_tracker{state_tracker_}, scheduler{scheduler_},
  144. staging_pool(device, memory_allocator, scheduler), descriptor_pool(device, scheduler),
  145. update_descriptor_queue(device, scheduler),
  146. blit_image(device, scheduler, state_tracker, descriptor_pool),
  147. astc_decoder_pass(device, scheduler, descriptor_pool, staging_pool, update_descriptor_queue,
  148. memory_allocator),
  149. render_pass_cache(device), texture_cache_runtime{device, scheduler,
  150. memory_allocator, staging_pool,
  151. blit_image, astc_decoder_pass,
  152. render_pass_cache},
  153. texture_cache(texture_cache_runtime, *this),
  154. buffer_cache_runtime(device, memory_allocator, scheduler, staging_pool,
  155. update_descriptor_queue, descriptor_pool),
  156. buffer_cache(*this, cpu_memory_, buffer_cache_runtime),
  157. pipeline_cache(*this, device, scheduler, descriptor_pool, update_descriptor_queue,
  158. render_pass_cache, buffer_cache, texture_cache, gpu.ShaderNotify()),
  159. query_cache{*this, device, scheduler}, accelerate_dma{buffer_cache},
  160. fence_manager(*this, gpu, texture_cache, buffer_cache, query_cache, device, scheduler),
  161. wfi_event(device.GetLogical().CreateEvent()) {
  162. scheduler.SetQueryCache(query_cache);
  163. }
  164. RasterizerVulkan::~RasterizerVulkan() = default;
  165. void RasterizerVulkan::Draw(bool is_indexed, bool is_instanced) {
  166. MICROPROFILE_SCOPE(Vulkan_Drawing);
  167. SCOPE_EXIT({ gpu.TickWork(); });
  168. FlushWork();
  169. query_cache.UpdateCounters();
  170. GraphicsPipeline* const pipeline{pipeline_cache.CurrentGraphicsPipeline()};
  171. if (!pipeline) {
  172. return;
  173. }
  174. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  175. // update engine as channel may be different.
  176. pipeline->SetEngine(maxwell3d, gpu_memory);
  177. pipeline->Configure(is_indexed);
  178. BeginTransformFeedback();
  179. UpdateDynamicStates();
  180. const auto& regs{maxwell3d->regs};
  181. const u32 num_instances{maxwell3d->mme_draw.instance_count};
  182. const DrawParams draw_params{MakeDrawParams(regs, num_instances, is_instanced, is_indexed)};
  183. scheduler.Record([draw_params](vk::CommandBuffer cmdbuf) {
  184. if (draw_params.is_indexed) {
  185. cmdbuf.DrawIndexed(draw_params.num_vertices, draw_params.num_instances,
  186. draw_params.first_index, draw_params.base_vertex,
  187. draw_params.base_instance);
  188. } else {
  189. cmdbuf.Draw(draw_params.num_vertices, draw_params.num_instances,
  190. draw_params.base_vertex, draw_params.base_instance);
  191. }
  192. });
  193. EndTransformFeedback();
  194. }
  195. void RasterizerVulkan::Clear() {
  196. MICROPROFILE_SCOPE(Vulkan_Clearing);
  197. if (!maxwell3d->ShouldExecute()) {
  198. return;
  199. }
  200. FlushWork();
  201. query_cache.UpdateCounters();
  202. auto& regs = maxwell3d->regs;
  203. const bool use_color = regs.clear_surface.R || regs.clear_surface.G || regs.clear_surface.B ||
  204. regs.clear_surface.A;
  205. const bool use_depth = regs.clear_surface.Z;
  206. const bool use_stencil = regs.clear_surface.S;
  207. if (!use_color && !use_depth && !use_stencil) {
  208. return;
  209. }
  210. std::scoped_lock lock{texture_cache.mutex};
  211. texture_cache.UpdateRenderTargets(true);
  212. const Framebuffer* const framebuffer = texture_cache.GetFramebuffer();
  213. const VkExtent2D render_area = framebuffer->RenderArea();
  214. scheduler.RequestRenderpass(framebuffer);
  215. u32 up_scale = 1;
  216. u32 down_shift = 0;
  217. if (texture_cache.IsRescaling()) {
  218. up_scale = Settings::values.resolution_info.up_scale;
  219. down_shift = Settings::values.resolution_info.down_shift;
  220. }
  221. UpdateViewportsState(regs);
  222. VkRect2D default_scissor;
  223. default_scissor.offset.x = 0;
  224. default_scissor.offset.y = 0;
  225. default_scissor.extent.width = std::numeric_limits<s32>::max();
  226. default_scissor.extent.height = std::numeric_limits<s32>::max();
  227. VkClearRect clear_rect{
  228. .rect = regs.clear_control.use_scissor ? GetScissorState(regs, 0, up_scale, down_shift)
  229. : default_scissor,
  230. .baseArrayLayer = regs.clear_surface.layer,
  231. .layerCount = 1,
  232. };
  233. if (clear_rect.rect.extent.width == 0 || clear_rect.rect.extent.height == 0) {
  234. return;
  235. }
  236. clear_rect.rect.extent = VkExtent2D{
  237. .width = std::min(clear_rect.rect.extent.width, render_area.width),
  238. .height = std::min(clear_rect.rect.extent.height, render_area.height),
  239. };
  240. const u32 color_attachment = regs.clear_surface.RT;
  241. if (use_color && framebuffer->HasAspectColorBit(color_attachment)) {
  242. VkClearValue clear_value;
  243. bool is_integer = false;
  244. bool is_signed = false;
  245. size_t int_size = 8;
  246. for (std::size_t i = 0; i < Tegra::Engines::Maxwell3D::Regs::NumRenderTargets; ++i) {
  247. const auto& this_rt = regs.rt[i];
  248. if (this_rt.Address() == 0) {
  249. continue;
  250. }
  251. if (this_rt.format == Tegra::RenderTargetFormat::NONE) {
  252. continue;
  253. }
  254. const auto format =
  255. VideoCore::Surface::PixelFormatFromRenderTargetFormat(this_rt.format);
  256. is_integer = IsPixelFormatInteger(format);
  257. is_signed = IsPixelFormatSignedInteger(format);
  258. int_size = PixelComponentSizeBitsInteger(format);
  259. break;
  260. }
  261. if (!is_integer) {
  262. std::memcpy(clear_value.color.float32, regs.clear_color.data(),
  263. regs.clear_color.size() * sizeof(f32));
  264. } else if (!is_signed) {
  265. for (size_t i = 0; i < 4; i++) {
  266. clear_value.color.uint32[i] = static_cast<u32>(
  267. static_cast<f32>(static_cast<u64>(int_size) << 1U) * regs.clear_color[i]);
  268. }
  269. } else {
  270. for (size_t i = 0; i < 4; i++) {
  271. clear_value.color.int32[i] =
  272. static_cast<s32>(static_cast<f32>(static_cast<s64>(int_size - 1) << 1) *
  273. (regs.clear_color[i] - 0.5f));
  274. }
  275. }
  276. scheduler.Record([color_attachment, clear_value, clear_rect](vk::CommandBuffer cmdbuf) {
  277. const VkClearAttachment attachment{
  278. .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
  279. .colorAttachment = color_attachment,
  280. .clearValue = clear_value,
  281. };
  282. cmdbuf.ClearAttachments(attachment, clear_rect);
  283. });
  284. }
  285. if (!use_depth && !use_stencil) {
  286. return;
  287. }
  288. VkImageAspectFlags aspect_flags = 0;
  289. if (use_depth && framebuffer->HasAspectDepthBit()) {
  290. aspect_flags |= VK_IMAGE_ASPECT_DEPTH_BIT;
  291. }
  292. if (use_stencil && framebuffer->HasAspectStencilBit()) {
  293. aspect_flags |= VK_IMAGE_ASPECT_STENCIL_BIT;
  294. }
  295. if (aspect_flags == 0) {
  296. return;
  297. }
  298. scheduler.Record([clear_depth = regs.clear_depth, clear_stencil = regs.clear_stencil,
  299. clear_rect, aspect_flags](vk::CommandBuffer cmdbuf) {
  300. VkClearAttachment attachment;
  301. attachment.aspectMask = aspect_flags;
  302. attachment.colorAttachment = 0;
  303. attachment.clearValue.depthStencil.depth = clear_depth;
  304. attachment.clearValue.depthStencil.stencil = clear_stencil;
  305. cmdbuf.ClearAttachments(attachment, clear_rect);
  306. });
  307. }
  308. void RasterizerVulkan::DispatchCompute() {
  309. FlushWork();
  310. ComputePipeline* const pipeline{pipeline_cache.CurrentComputePipeline()};
  311. if (!pipeline) {
  312. return;
  313. }
  314. std::scoped_lock lock{texture_cache.mutex, buffer_cache.mutex};
  315. pipeline->Configure(*kepler_compute, *gpu_memory, scheduler, buffer_cache, texture_cache);
  316. const auto& qmd{kepler_compute->launch_description};
  317. const std::array<u32, 3> dim{qmd.grid_dim_x, qmd.grid_dim_y, qmd.grid_dim_z};
  318. scheduler.RequestOutsideRenderPassOperationContext();
  319. scheduler.Record([dim](vk::CommandBuffer cmdbuf) { cmdbuf.Dispatch(dim[0], dim[1], dim[2]); });
  320. }
  321. void RasterizerVulkan::ResetCounter(VideoCore::QueryType type) {
  322. query_cache.ResetCounter(type);
  323. }
  324. void RasterizerVulkan::Query(GPUVAddr gpu_addr, VideoCore::QueryType type,
  325. std::optional<u64> timestamp) {
  326. query_cache.Query(gpu_addr, type, timestamp);
  327. }
  328. void RasterizerVulkan::BindGraphicsUniformBuffer(size_t stage, u32 index, GPUVAddr gpu_addr,
  329. u32 size) {
  330. buffer_cache.BindGraphicsUniformBuffer(stage, index, gpu_addr, size);
  331. }
  332. void Vulkan::RasterizerVulkan::DisableGraphicsUniformBuffer(size_t stage, u32 index) {
  333. buffer_cache.DisableGraphicsUniformBuffer(stage, index);
  334. }
  335. void RasterizerVulkan::FlushAll() {}
  336. void RasterizerVulkan::FlushRegion(VAddr addr, u64 size) {
  337. if (addr == 0 || size == 0) {
  338. return;
  339. }
  340. {
  341. std::scoped_lock lock{texture_cache.mutex};
  342. texture_cache.DownloadMemory(addr, size);
  343. }
  344. {
  345. std::scoped_lock lock{buffer_cache.mutex};
  346. buffer_cache.DownloadMemory(addr, size);
  347. }
  348. query_cache.FlushRegion(addr, size);
  349. }
  350. bool RasterizerVulkan::MustFlushRegion(VAddr addr, u64 size) {
  351. std::scoped_lock lock{texture_cache.mutex, buffer_cache.mutex};
  352. if (!Settings::IsGPULevelHigh()) {
  353. return buffer_cache.IsRegionGpuModified(addr, size);
  354. }
  355. return texture_cache.IsRegionGpuModified(addr, size) ||
  356. buffer_cache.IsRegionGpuModified(addr, size);
  357. }
  358. void RasterizerVulkan::InvalidateRegion(VAddr addr, u64 size) {
  359. if (addr == 0 || size == 0) {
  360. return;
  361. }
  362. {
  363. std::scoped_lock lock{texture_cache.mutex};
  364. texture_cache.WriteMemory(addr, size);
  365. }
  366. {
  367. std::scoped_lock lock{buffer_cache.mutex};
  368. buffer_cache.WriteMemory(addr, size);
  369. }
  370. pipeline_cache.InvalidateRegion(addr, size);
  371. query_cache.InvalidateRegion(addr, size);
  372. }
  373. void RasterizerVulkan::OnCPUWrite(VAddr addr, u64 size) {
  374. if (addr == 0 || size == 0) {
  375. return;
  376. }
  377. pipeline_cache.OnCPUWrite(addr, size);
  378. {
  379. std::scoped_lock lock{texture_cache.mutex};
  380. texture_cache.WriteMemory(addr, size);
  381. }
  382. {
  383. std::scoped_lock lock{buffer_cache.mutex};
  384. buffer_cache.CachedWriteMemory(addr, size);
  385. }
  386. }
  387. void RasterizerVulkan::InvalidateGPUCache() {
  388. pipeline_cache.SyncGuestHost();
  389. {
  390. std::scoped_lock lock{buffer_cache.mutex};
  391. buffer_cache.FlushCachedWrites();
  392. }
  393. }
  394. void RasterizerVulkan::UnmapMemory(VAddr addr, u64 size) {
  395. {
  396. std::scoped_lock lock{texture_cache.mutex};
  397. texture_cache.UnmapMemory(addr, size);
  398. }
  399. {
  400. std::scoped_lock lock{buffer_cache.mutex};
  401. buffer_cache.WriteMemory(addr, size);
  402. }
  403. pipeline_cache.OnCPUWrite(addr, size);
  404. }
  405. void RasterizerVulkan::ModifyGPUMemory(size_t as_id, GPUVAddr addr, u64 size) {
  406. {
  407. std::scoped_lock lock{texture_cache.mutex};
  408. texture_cache.UnmapGPUMemory(as_id, addr, size);
  409. }
  410. }
  411. void RasterizerVulkan::SignalFence(std::function<void()>&& func) {
  412. fence_manager.SignalFence(std::move(func));
  413. }
  414. void RasterizerVulkan::SyncOperation(std::function<void()>&& func) {
  415. fence_manager.SyncOperation(std::move(func));
  416. }
  417. void RasterizerVulkan::SignalSyncPoint(u32 value) {
  418. fence_manager.SignalSyncPoint(value);
  419. }
  420. void RasterizerVulkan::SignalReference() {
  421. fence_manager.SignalOrdering();
  422. }
  423. void RasterizerVulkan::ReleaseFences() {
  424. fence_manager.WaitPendingFences();
  425. }
  426. void RasterizerVulkan::FlushAndInvalidateRegion(VAddr addr, u64 size) {
  427. if (Settings::IsGPULevelExtreme()) {
  428. FlushRegion(addr, size);
  429. }
  430. InvalidateRegion(addr, size);
  431. }
  432. void RasterizerVulkan::WaitForIdle() {
  433. // Everything but wait pixel operations. This intentionally includes FRAGMENT_SHADER_BIT because
  434. // fragment shaders can still write storage buffers.
  435. VkPipelineStageFlags flags =
  436. VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT | VK_PIPELINE_STAGE_VERTEX_INPUT_BIT |
  437. VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT |
  438. VK_PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT |
  439. VK_PIPELINE_STAGE_GEOMETRY_SHADER_BIT | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT |
  440. VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT | VK_PIPELINE_STAGE_TRANSFER_BIT;
  441. if (device.IsExtTransformFeedbackSupported()) {
  442. flags |= VK_PIPELINE_STAGE_TRANSFORM_FEEDBACK_BIT_EXT;
  443. }
  444. scheduler.RequestOutsideRenderPassOperationContext();
  445. scheduler.Record([event = *wfi_event, flags](vk::CommandBuffer cmdbuf) {
  446. cmdbuf.SetEvent(event, flags);
  447. cmdbuf.WaitEvents(event, flags, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, {}, {}, {});
  448. });
  449. SignalReference();
  450. }
  451. void RasterizerVulkan::FragmentBarrier() {
  452. // We already put barriers when a render pass finishes
  453. scheduler.RequestOutsideRenderPassOperationContext();
  454. }
  455. void RasterizerVulkan::TiledCacheBarrier() {
  456. // TODO: Implementing tiled barriers requires rewriting a good chunk of the Vulkan backend
  457. }
  458. void RasterizerVulkan::FlushCommands() {
  459. if (draw_counter == 0) {
  460. return;
  461. }
  462. draw_counter = 0;
  463. scheduler.Flush();
  464. }
  465. void RasterizerVulkan::TickFrame() {
  466. draw_counter = 0;
  467. update_descriptor_queue.TickFrame();
  468. fence_manager.TickFrame();
  469. staging_pool.TickFrame();
  470. {
  471. std::scoped_lock lock{texture_cache.mutex};
  472. texture_cache.TickFrame();
  473. }
  474. {
  475. std::scoped_lock lock{buffer_cache.mutex};
  476. buffer_cache.TickFrame();
  477. }
  478. }
  479. bool RasterizerVulkan::AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Surface& src,
  480. const Tegra::Engines::Fermi2D::Surface& dst,
  481. const Tegra::Engines::Fermi2D::Config& copy_config) {
  482. std::scoped_lock lock{texture_cache.mutex};
  483. texture_cache.BlitImage(dst, src, copy_config);
  484. return true;
  485. }
  486. Tegra::Engines::AccelerateDMAInterface& RasterizerVulkan::AccessAccelerateDMA() {
  487. return accelerate_dma;
  488. }
  489. void RasterizerVulkan::AccelerateInlineToMemory(GPUVAddr address, size_t copy_size,
  490. std::span<const u8> memory) {
  491. auto cpu_addr = gpu_memory->GpuToCpuAddress(address);
  492. if (!cpu_addr) [[unlikely]] {
  493. gpu_memory->WriteBlock(address, memory.data(), copy_size);
  494. return;
  495. }
  496. gpu_memory->WriteBlockUnsafe(address, memory.data(), copy_size);
  497. {
  498. std::unique_lock<std::mutex> lock{buffer_cache.mutex};
  499. if (!buffer_cache.InlineMemory(*cpu_addr, copy_size, memory)) {
  500. buffer_cache.WriteMemory(*cpu_addr, copy_size);
  501. }
  502. }
  503. {
  504. std::scoped_lock lock_texture{texture_cache.mutex};
  505. texture_cache.WriteMemory(*cpu_addr, copy_size);
  506. }
  507. pipeline_cache.InvalidateRegion(*cpu_addr, copy_size);
  508. query_cache.InvalidateRegion(*cpu_addr, copy_size);
  509. }
  510. bool RasterizerVulkan::AccelerateDisplay(const Tegra::FramebufferConfig& config,
  511. VAddr framebuffer_addr, u32 pixel_stride) {
  512. if (!framebuffer_addr) {
  513. return false;
  514. }
  515. std::scoped_lock lock{texture_cache.mutex};
  516. ImageView* const image_view = texture_cache.TryFindFramebufferImageView(framebuffer_addr);
  517. if (!image_view) {
  518. return false;
  519. }
  520. screen_info.image_view = image_view->Handle(Shader::TextureType::Color2D);
  521. screen_info.width = image_view->size.width;
  522. screen_info.height = image_view->size.height;
  523. screen_info.is_srgb = VideoCore::Surface::IsPixelFormatSRGB(image_view->format);
  524. return true;
  525. }
  526. void RasterizerVulkan::LoadDiskResources(u64 title_id, std::stop_token stop_loading,
  527. const VideoCore::DiskResourceLoadCallback& callback) {
  528. pipeline_cache.LoadDiskResources(title_id, stop_loading, callback);
  529. }
  530. void RasterizerVulkan::FlushWork() {
  531. static constexpr u32 DRAWS_TO_DISPATCH = 4096;
  532. // Only check multiples of 8 draws
  533. static_assert(DRAWS_TO_DISPATCH % 8 == 0);
  534. if ((++draw_counter & 7) != 7) {
  535. return;
  536. }
  537. if (draw_counter < DRAWS_TO_DISPATCH) {
  538. // Send recorded tasks to the worker thread
  539. scheduler.DispatchWork();
  540. return;
  541. }
  542. // Otherwise (every certain number of draws) flush execution.
  543. // This submits commands to the Vulkan driver.
  544. scheduler.Flush();
  545. draw_counter = 0;
  546. }
  547. AccelerateDMA::AccelerateDMA(BufferCache& buffer_cache_) : buffer_cache{buffer_cache_} {}
  548. bool AccelerateDMA::BufferClear(GPUVAddr src_address, u64 amount, u32 value) {
  549. std::scoped_lock lock{buffer_cache.mutex};
  550. return buffer_cache.DMAClear(src_address, amount, value);
  551. }
  552. bool AccelerateDMA::BufferCopy(GPUVAddr src_address, GPUVAddr dest_address, u64 amount) {
  553. std::scoped_lock lock{buffer_cache.mutex};
  554. return buffer_cache.DMACopy(src_address, dest_address, amount);
  555. }
  556. void RasterizerVulkan::UpdateDynamicStates() {
  557. auto& regs = maxwell3d->regs;
  558. UpdateViewportsState(regs);
  559. UpdateScissorsState(regs);
  560. UpdateDepthBias(regs);
  561. UpdateBlendConstants(regs);
  562. UpdateDepthBounds(regs);
  563. UpdateStencilFaces(regs);
  564. UpdateLineWidth(regs);
  565. if (device.IsExtExtendedDynamicStateSupported()) {
  566. UpdateCullMode(regs);
  567. UpdateDepthBoundsTestEnable(regs);
  568. UpdateDepthTestEnable(regs);
  569. UpdateDepthWriteEnable(regs);
  570. UpdateDepthCompareOp(regs);
  571. UpdateFrontFace(regs);
  572. UpdateStencilOp(regs);
  573. UpdateStencilTestEnable(regs);
  574. if (device.IsExtVertexInputDynamicStateSupported()) {
  575. UpdateVertexInput(regs);
  576. }
  577. }
  578. }
  579. void RasterizerVulkan::BeginTransformFeedback() {
  580. const auto& regs = maxwell3d->regs;
  581. if (regs.transform_feedback_enabled == 0) {
  582. return;
  583. }
  584. if (!device.IsExtTransformFeedbackSupported()) {
  585. LOG_ERROR(Render_Vulkan, "Transform feedbacks used but not supported");
  586. return;
  587. }
  588. UNIMPLEMENTED_IF(regs.IsShaderConfigEnabled(Maxwell::ShaderType::TessellationInit) ||
  589. regs.IsShaderConfigEnabled(Maxwell::ShaderType::Tessellation) ||
  590. regs.IsShaderConfigEnabled(Maxwell::ShaderType::Geometry));
  591. scheduler.Record(
  592. [](vk::CommandBuffer cmdbuf) { cmdbuf.BeginTransformFeedbackEXT(0, 0, nullptr, nullptr); });
  593. }
  594. void RasterizerVulkan::EndTransformFeedback() {
  595. const auto& regs = maxwell3d->regs;
  596. if (regs.transform_feedback_enabled == 0) {
  597. return;
  598. }
  599. if (!device.IsExtTransformFeedbackSupported()) {
  600. return;
  601. }
  602. scheduler.Record(
  603. [](vk::CommandBuffer cmdbuf) { cmdbuf.EndTransformFeedbackEXT(0, 0, nullptr, nullptr); });
  604. }
  605. void RasterizerVulkan::UpdateViewportsState(Tegra::Engines::Maxwell3D::Regs& regs) {
  606. if (!state_tracker.TouchViewports()) {
  607. return;
  608. }
  609. const bool is_rescaling{texture_cache.IsRescaling()};
  610. const float scale = is_rescaling ? Settings::values.resolution_info.up_factor : 1.0f;
  611. const std::array viewports{
  612. GetViewportState(device, regs, 0, scale), GetViewportState(device, regs, 1, scale),
  613. GetViewportState(device, regs, 2, scale), GetViewportState(device, regs, 3, scale),
  614. GetViewportState(device, regs, 4, scale), GetViewportState(device, regs, 5, scale),
  615. GetViewportState(device, regs, 6, scale), GetViewportState(device, regs, 7, scale),
  616. GetViewportState(device, regs, 8, scale), GetViewportState(device, regs, 9, scale),
  617. GetViewportState(device, regs, 10, scale), GetViewportState(device, regs, 11, scale),
  618. GetViewportState(device, regs, 12, scale), GetViewportState(device, regs, 13, scale),
  619. GetViewportState(device, regs, 14, scale), GetViewportState(device, regs, 15, scale),
  620. };
  621. scheduler.Record([viewports](vk::CommandBuffer cmdbuf) { cmdbuf.SetViewport(0, viewports); });
  622. }
  623. void RasterizerVulkan::UpdateScissorsState(Tegra::Engines::Maxwell3D::Regs& regs) {
  624. if (!state_tracker.TouchScissors()) {
  625. return;
  626. }
  627. u32 up_scale = 1;
  628. u32 down_shift = 0;
  629. if (texture_cache.IsRescaling()) {
  630. up_scale = Settings::values.resolution_info.up_scale;
  631. down_shift = Settings::values.resolution_info.down_shift;
  632. }
  633. const std::array scissors{
  634. GetScissorState(regs, 0, up_scale, down_shift),
  635. GetScissorState(regs, 1, up_scale, down_shift),
  636. GetScissorState(regs, 2, up_scale, down_shift),
  637. GetScissorState(regs, 3, up_scale, down_shift),
  638. GetScissorState(regs, 4, up_scale, down_shift),
  639. GetScissorState(regs, 5, up_scale, down_shift),
  640. GetScissorState(regs, 6, up_scale, down_shift),
  641. GetScissorState(regs, 7, up_scale, down_shift),
  642. GetScissorState(regs, 8, up_scale, down_shift),
  643. GetScissorState(regs, 9, up_scale, down_shift),
  644. GetScissorState(regs, 10, up_scale, down_shift),
  645. GetScissorState(regs, 11, up_scale, down_shift),
  646. GetScissorState(regs, 12, up_scale, down_shift),
  647. GetScissorState(regs, 13, up_scale, down_shift),
  648. GetScissorState(regs, 14, up_scale, down_shift),
  649. GetScissorState(regs, 15, up_scale, down_shift),
  650. };
  651. scheduler.Record([scissors](vk::CommandBuffer cmdbuf) { cmdbuf.SetScissor(0, scissors); });
  652. }
  653. void RasterizerVulkan::UpdateDepthBias(Tegra::Engines::Maxwell3D::Regs& regs) {
  654. if (!state_tracker.TouchDepthBias()) {
  655. return;
  656. }
  657. float units = regs.depth_bias / 2.0f;
  658. const bool is_d24 = regs.zeta.format == Tegra::DepthFormat::Z24_UNORM_S8_UINT ||
  659. regs.zeta.format == Tegra::DepthFormat::X8Z24_UNORM ||
  660. regs.zeta.format == Tegra::DepthFormat::S8Z24_UNORM ||
  661. regs.zeta.format == Tegra::DepthFormat::V8Z24_UNORM;
  662. if (is_d24 && !device.SupportsD24DepthBuffer()) {
  663. // the base formulas can be obtained from here:
  664. // https://docs.microsoft.com/en-us/windows/win32/direct3d11/d3d10-graphics-programming-guide-output-merger-stage-depth-bias
  665. const double rescale_factor =
  666. static_cast<double>(1ULL << (32 - 24)) / (static_cast<double>(0x1.ep+127));
  667. units = static_cast<float>(static_cast<double>(units) * rescale_factor);
  668. }
  669. scheduler.Record([constant = units, clamp = regs.depth_bias_clamp,
  670. factor = regs.slope_scale_depth_bias](vk::CommandBuffer cmdbuf) {
  671. cmdbuf.SetDepthBias(constant, clamp, factor);
  672. });
  673. }
  674. void RasterizerVulkan::UpdateBlendConstants(Tegra::Engines::Maxwell3D::Regs& regs) {
  675. if (!state_tracker.TouchBlendConstants()) {
  676. return;
  677. }
  678. const std::array blend_color = {regs.blend_color.r, regs.blend_color.g, regs.blend_color.b,
  679. regs.blend_color.a};
  680. scheduler.Record(
  681. [blend_color](vk::CommandBuffer cmdbuf) { cmdbuf.SetBlendConstants(blend_color.data()); });
  682. }
  683. void RasterizerVulkan::UpdateDepthBounds(Tegra::Engines::Maxwell3D::Regs& regs) {
  684. if (!state_tracker.TouchDepthBounds()) {
  685. return;
  686. }
  687. scheduler.Record([min = regs.depth_bounds[0], max = regs.depth_bounds[1]](
  688. vk::CommandBuffer cmdbuf) { cmdbuf.SetDepthBounds(min, max); });
  689. }
  690. void RasterizerVulkan::UpdateStencilFaces(Tegra::Engines::Maxwell3D::Regs& regs) {
  691. if (!state_tracker.TouchStencilProperties()) {
  692. return;
  693. }
  694. if (regs.stencil_two_side_enable) {
  695. // Separate values per face
  696. scheduler.Record(
  697. [front_ref = regs.stencil_front_ref, front_write_mask = regs.stencil_front_mask,
  698. front_test_mask = regs.stencil_front_func_mask, back_ref = regs.stencil_back_ref,
  699. back_write_mask = regs.stencil_back_mask,
  700. back_test_mask = regs.stencil_back_func_mask](vk::CommandBuffer cmdbuf) {
  701. // Front face
  702. cmdbuf.SetStencilReference(VK_STENCIL_FACE_FRONT_BIT, front_ref);
  703. cmdbuf.SetStencilWriteMask(VK_STENCIL_FACE_FRONT_BIT, front_write_mask);
  704. cmdbuf.SetStencilCompareMask(VK_STENCIL_FACE_FRONT_BIT, front_test_mask);
  705. // Back face
  706. cmdbuf.SetStencilReference(VK_STENCIL_FACE_BACK_BIT, back_ref);
  707. cmdbuf.SetStencilWriteMask(VK_STENCIL_FACE_BACK_BIT, back_write_mask);
  708. cmdbuf.SetStencilCompareMask(VK_STENCIL_FACE_BACK_BIT, back_test_mask);
  709. });
  710. } else {
  711. // Front face defines both faces
  712. scheduler.Record([ref = regs.stencil_front_ref, write_mask = regs.stencil_front_mask,
  713. test_mask = regs.stencil_front_func_mask](vk::CommandBuffer cmdbuf) {
  714. cmdbuf.SetStencilReference(VK_STENCIL_FACE_FRONT_AND_BACK, ref);
  715. cmdbuf.SetStencilWriteMask(VK_STENCIL_FACE_FRONT_AND_BACK, write_mask);
  716. cmdbuf.SetStencilCompareMask(VK_STENCIL_FACE_FRONT_AND_BACK, test_mask);
  717. });
  718. }
  719. }
  720. void RasterizerVulkan::UpdateLineWidth(Tegra::Engines::Maxwell3D::Regs& regs) {
  721. if (!state_tracker.TouchLineWidth()) {
  722. return;
  723. }
  724. const float width =
  725. regs.line_anti_alias_enable ? regs.line_width_smooth : regs.line_width_aliased;
  726. scheduler.Record([width](vk::CommandBuffer cmdbuf) { cmdbuf.SetLineWidth(width); });
  727. }
  728. void RasterizerVulkan::UpdateCullMode(Tegra::Engines::Maxwell3D::Regs& regs) {
  729. if (!state_tracker.TouchCullMode()) {
  730. return;
  731. }
  732. scheduler.Record([enabled = regs.gl_cull_test_enabled,
  733. cull_face = regs.gl_cull_face](vk::CommandBuffer cmdbuf) {
  734. cmdbuf.SetCullModeEXT(enabled ? MaxwellToVK::CullFace(cull_face) : VK_CULL_MODE_NONE);
  735. });
  736. }
  737. void RasterizerVulkan::UpdateDepthBoundsTestEnable(Tegra::Engines::Maxwell3D::Regs& regs) {
  738. if (!state_tracker.TouchDepthBoundsTestEnable()) {
  739. return;
  740. }
  741. bool enabled = regs.depth_bounds_enable;
  742. if (enabled && !device.IsDepthBoundsSupported()) {
  743. LOG_WARNING(Render_Vulkan, "Depth bounds is enabled but not supported");
  744. enabled = false;
  745. }
  746. scheduler.Record([enable = regs.depth_bounds_enable](vk::CommandBuffer cmdbuf) {
  747. cmdbuf.SetDepthBoundsTestEnableEXT(enable);
  748. });
  749. }
  750. void RasterizerVulkan::UpdateDepthTestEnable(Tegra::Engines::Maxwell3D::Regs& regs) {
  751. if (!state_tracker.TouchDepthTestEnable()) {
  752. return;
  753. }
  754. scheduler.Record([enable = regs.depth_test_enable](vk::CommandBuffer cmdbuf) {
  755. cmdbuf.SetDepthTestEnableEXT(enable);
  756. });
  757. }
  758. void RasterizerVulkan::UpdateDepthWriteEnable(Tegra::Engines::Maxwell3D::Regs& regs) {
  759. if (!state_tracker.TouchDepthWriteEnable()) {
  760. return;
  761. }
  762. scheduler.Record([enable = regs.depth_write_enabled](vk::CommandBuffer cmdbuf) {
  763. cmdbuf.SetDepthWriteEnableEXT(enable);
  764. });
  765. }
  766. void RasterizerVulkan::UpdateDepthCompareOp(Tegra::Engines::Maxwell3D::Regs& regs) {
  767. if (!state_tracker.TouchDepthCompareOp()) {
  768. return;
  769. }
  770. scheduler.Record([func = regs.depth_test_func](vk::CommandBuffer cmdbuf) {
  771. cmdbuf.SetDepthCompareOpEXT(MaxwellToVK::ComparisonOp(func));
  772. });
  773. }
  774. void RasterizerVulkan::UpdateFrontFace(Tegra::Engines::Maxwell3D::Regs& regs) {
  775. if (!state_tracker.TouchFrontFace()) {
  776. return;
  777. }
  778. VkFrontFace front_face = MaxwellToVK::FrontFace(regs.gl_front_face);
  779. if (regs.window_origin.flip_y != 0) {
  780. front_face = front_face == VK_FRONT_FACE_CLOCKWISE ? VK_FRONT_FACE_COUNTER_CLOCKWISE
  781. : VK_FRONT_FACE_CLOCKWISE;
  782. }
  783. scheduler.Record(
  784. [front_face](vk::CommandBuffer cmdbuf) { cmdbuf.SetFrontFaceEXT(front_face); });
  785. }
  786. void RasterizerVulkan::UpdateStencilOp(Tegra::Engines::Maxwell3D::Regs& regs) {
  787. if (!state_tracker.TouchStencilOp()) {
  788. return;
  789. }
  790. const Maxwell::StencilOp::Op fail = regs.stencil_front_op.fail;
  791. const Maxwell::StencilOp::Op zfail = regs.stencil_front_op.zfail;
  792. const Maxwell::StencilOp::Op zpass = regs.stencil_front_op.zpass;
  793. const Maxwell::ComparisonOp compare = regs.stencil_front_op.func;
  794. if (regs.stencil_two_side_enable) {
  795. // Separate stencil op per face
  796. const Maxwell::StencilOp::Op back_fail = regs.stencil_back_op.fail;
  797. const Maxwell::StencilOp::Op back_zfail = regs.stencil_back_op.zfail;
  798. const Maxwell::StencilOp::Op back_zpass = regs.stencil_back_op.zpass;
  799. const Maxwell::ComparisonOp back_compare = regs.stencil_back_op.func;
  800. scheduler.Record([fail, zfail, zpass, compare, back_fail, back_zfail, back_zpass,
  801. back_compare](vk::CommandBuffer cmdbuf) {
  802. cmdbuf.SetStencilOpEXT(VK_STENCIL_FACE_FRONT_BIT, MaxwellToVK::StencilOp(fail),
  803. MaxwellToVK::StencilOp(zpass), MaxwellToVK::StencilOp(zfail),
  804. MaxwellToVK::ComparisonOp(compare));
  805. cmdbuf.SetStencilOpEXT(VK_STENCIL_FACE_BACK_BIT, MaxwellToVK::StencilOp(back_fail),
  806. MaxwellToVK::StencilOp(back_zpass),
  807. MaxwellToVK::StencilOp(back_zfail),
  808. MaxwellToVK::ComparisonOp(back_compare));
  809. });
  810. } else {
  811. // Front face defines the stencil op of both faces
  812. scheduler.Record([fail, zfail, zpass, compare](vk::CommandBuffer cmdbuf) {
  813. cmdbuf.SetStencilOpEXT(VK_STENCIL_FACE_FRONT_AND_BACK, MaxwellToVK::StencilOp(fail),
  814. MaxwellToVK::StencilOp(zpass), MaxwellToVK::StencilOp(zfail),
  815. MaxwellToVK::ComparisonOp(compare));
  816. });
  817. }
  818. }
  819. void RasterizerVulkan::UpdateStencilTestEnable(Tegra::Engines::Maxwell3D::Regs& regs) {
  820. if (!state_tracker.TouchStencilTestEnable()) {
  821. return;
  822. }
  823. scheduler.Record([enable = regs.stencil_enable](vk::CommandBuffer cmdbuf) {
  824. cmdbuf.SetStencilTestEnableEXT(enable);
  825. });
  826. }
  827. void RasterizerVulkan::UpdateVertexInput(Tegra::Engines::Maxwell3D::Regs& regs) {
  828. auto& dirty{maxwell3d->dirty.flags};
  829. if (!dirty[Dirty::VertexInput]) {
  830. return;
  831. }
  832. dirty[Dirty::VertexInput] = false;
  833. boost::container::static_vector<VkVertexInputBindingDescription2EXT, 32> bindings;
  834. boost::container::static_vector<VkVertexInputAttributeDescription2EXT, 32> attributes;
  835. // There seems to be a bug on Nvidia's driver where updating only higher attributes ends up
  836. // generating dirty state. Track the highest dirty attribute and update all attributes until
  837. // that one.
  838. size_t highest_dirty_attr{};
  839. for (size_t index = 0; index < Maxwell::NumVertexAttributes; ++index) {
  840. if (dirty[Dirty::VertexAttribute0 + index]) {
  841. highest_dirty_attr = index;
  842. }
  843. }
  844. for (size_t index = 0; index < highest_dirty_attr; ++index) {
  845. const Maxwell::VertexAttribute attribute{regs.vertex_attrib_format[index]};
  846. const u32 binding{attribute.buffer};
  847. dirty[Dirty::VertexAttribute0 + index] = false;
  848. dirty[Dirty::VertexBinding0 + static_cast<size_t>(binding)] = true;
  849. if (!attribute.constant) {
  850. attributes.push_back({
  851. .sType = VK_STRUCTURE_TYPE_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION_2_EXT,
  852. .pNext = nullptr,
  853. .location = static_cast<u32>(index),
  854. .binding = binding,
  855. .format = MaxwellToVK::VertexFormat(device, attribute.type, attribute.size),
  856. .offset = attribute.offset,
  857. });
  858. }
  859. }
  860. for (size_t index = 0; index < Maxwell::NumVertexAttributes; ++index) {
  861. if (!dirty[Dirty::VertexBinding0 + index]) {
  862. continue;
  863. }
  864. dirty[Dirty::VertexBinding0 + index] = false;
  865. const u32 binding{static_cast<u32>(index)};
  866. const auto& input_binding{regs.vertex_streams[binding]};
  867. const bool is_instanced{regs.vertex_stream_instances.IsInstancingEnabled(binding)};
  868. bindings.push_back({
  869. .sType = VK_STRUCTURE_TYPE_VERTEX_INPUT_BINDING_DESCRIPTION_2_EXT,
  870. .pNext = nullptr,
  871. .binding = binding,
  872. .stride = input_binding.stride,
  873. .inputRate = is_instanced ? VK_VERTEX_INPUT_RATE_INSTANCE : VK_VERTEX_INPUT_RATE_VERTEX,
  874. .divisor = is_instanced ? input_binding.frequency : 1,
  875. });
  876. }
  877. scheduler.Record([bindings, attributes](vk::CommandBuffer cmdbuf) {
  878. cmdbuf.SetVertexInputEXT(bindings, attributes);
  879. });
  880. }
  881. void RasterizerVulkan::InitializeChannel(Tegra::Control::ChannelState& channel) {
  882. CreateChannel(channel);
  883. {
  884. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  885. texture_cache.CreateChannel(channel);
  886. buffer_cache.CreateChannel(channel);
  887. }
  888. pipeline_cache.CreateChannel(channel);
  889. query_cache.CreateChannel(channel);
  890. state_tracker.SetupTables(channel);
  891. }
  892. void RasterizerVulkan::BindChannel(Tegra::Control::ChannelState& channel) {
  893. const s32 channel_id = channel.bind_id;
  894. BindToChannel(channel_id);
  895. {
  896. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  897. texture_cache.BindToChannel(channel_id);
  898. buffer_cache.BindToChannel(channel_id);
  899. }
  900. pipeline_cache.BindToChannel(channel_id);
  901. query_cache.BindToChannel(channel_id);
  902. state_tracker.ChangeChannel(channel);
  903. state_tracker.InvalidateState();
  904. }
  905. void RasterizerVulkan::ReleaseChannel(s32 channel_id) {
  906. EraseChannel(channel_id);
  907. {
  908. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  909. texture_cache.EraseChannel(channel_id);
  910. buffer_cache.EraseChannel(channel_id);
  911. }
  912. pipeline_cache.EraseChannel(channel_id);
  913. query_cache.EraseChannel(channel_id);
  914. }
  915. } // namespace Vulkan