vk_rasterizer.cpp 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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/draw_manager.h"
  14. #include "video_core/engines/kepler_compute.h"
  15. #include "video_core/engines/maxwell_3d.h"
  16. #include "video_core/renderer_vulkan/blit_image.h"
  17. #include "video_core/renderer_vulkan/fixed_pipeline_state.h"
  18. #include "video_core/renderer_vulkan/maxwell_to_vk.h"
  19. #include "video_core/renderer_vulkan/renderer_vulkan.h"
  20. #include "video_core/renderer_vulkan/vk_buffer_cache.h"
  21. #include "video_core/renderer_vulkan/vk_compute_pipeline.h"
  22. #include "video_core/renderer_vulkan/vk_descriptor_pool.h"
  23. #include "video_core/renderer_vulkan/vk_pipeline_cache.h"
  24. #include "video_core/renderer_vulkan/vk_rasterizer.h"
  25. #include "video_core/renderer_vulkan/vk_scheduler.h"
  26. #include "video_core/renderer_vulkan/vk_staging_buffer_pool.h"
  27. #include "video_core/renderer_vulkan/vk_state_tracker.h"
  28. #include "video_core/renderer_vulkan/vk_texture_cache.h"
  29. #include "video_core/renderer_vulkan/vk_update_descriptor.h"
  30. #include "video_core/shader_cache.h"
  31. #include "video_core/texture_cache/texture_cache_base.h"
  32. #include "video_core/vulkan_common/vulkan_device.h"
  33. #include "video_core/vulkan_common/vulkan_wrapper.h"
  34. namespace Vulkan {
  35. using Maxwell = Tegra::Engines::Maxwell3D::Regs;
  36. using MaxwellDrawState = Tegra::Engines::DrawManager::State;
  37. using VideoCommon::ImageViewId;
  38. using VideoCommon::ImageViewType;
  39. MICROPROFILE_DEFINE(Vulkan_WaitForWorker, "Vulkan", "Wait for worker", MP_RGB(255, 192, 192));
  40. MICROPROFILE_DEFINE(Vulkan_Drawing, "Vulkan", "Record drawing", MP_RGB(192, 128, 128));
  41. MICROPROFILE_DEFINE(Vulkan_Compute, "Vulkan", "Record compute", MP_RGB(192, 128, 128));
  42. MICROPROFILE_DEFINE(Vulkan_Clearing, "Vulkan", "Record clearing", MP_RGB(192, 128, 128));
  43. MICROPROFILE_DEFINE(Vulkan_PipelineCache, "Vulkan", "Pipeline cache", MP_RGB(192, 128, 128));
  44. namespace {
  45. struct DrawParams {
  46. u32 base_instance;
  47. u32 num_instances;
  48. u32 base_vertex;
  49. u32 num_vertices;
  50. u32 first_index;
  51. bool is_indexed;
  52. };
  53. VkViewport GetViewportState(const Device& device, const Maxwell& regs, size_t index, float scale) {
  54. const auto& src = regs.viewport_transform[index];
  55. const auto conv = [scale](float value) {
  56. float new_value = value * scale;
  57. if (scale < 1.0f) {
  58. const bool sign = std::signbit(value);
  59. new_value = std::round(std::abs(new_value));
  60. new_value = sign ? -new_value : new_value;
  61. }
  62. return new_value;
  63. };
  64. const float x = conv(src.translate_x - src.scale_x);
  65. const float width = conv(src.scale_x * 2.0f);
  66. float y = conv(src.translate_y - src.scale_y);
  67. float height = conv(src.scale_y * 2.0f);
  68. bool y_negate = regs.window_origin.mode != Maxwell::WindowOrigin::Mode::UpperLeft;
  69. if (!device.IsNvViewportSwizzleSupported()) {
  70. y_negate = y_negate != (src.swizzle.y == Maxwell::ViewportSwizzle::NegativeY);
  71. }
  72. if (y_negate) {
  73. y += height;
  74. height = -height;
  75. }
  76. const float reduce_z = regs.depth_mode == Maxwell::DepthMode::MinusOneToOne ? 1.0f : 0.0f;
  77. VkViewport viewport{
  78. .x = x,
  79. .y = y,
  80. .width = width != 0.0f ? width : 1.0f,
  81. .height = height != 0.0f ? height : 1.0f,
  82. .minDepth = src.translate_z - src.scale_z * reduce_z,
  83. .maxDepth = src.translate_z + src.scale_z,
  84. };
  85. if (!device.IsExtDepthRangeUnrestrictedSupported()) {
  86. viewport.minDepth = std::clamp(viewport.minDepth, 0.0f, 1.0f);
  87. viewport.maxDepth = std::clamp(viewport.maxDepth, 0.0f, 1.0f);
  88. }
  89. return viewport;
  90. }
  91. VkRect2D GetScissorState(const Maxwell& regs, size_t index, u32 up_scale = 1, u32 down_shift = 0) {
  92. const auto& src = regs.scissor_test[index];
  93. VkRect2D scissor;
  94. const auto scale_up = [&](s32 value) -> s32 {
  95. if (value == 0) {
  96. return 0U;
  97. }
  98. const s32 upset = value * up_scale;
  99. s32 acumm = 0;
  100. if ((up_scale >> down_shift) == 0) {
  101. acumm = upset % 2;
  102. }
  103. const s32 converted_value = (value * up_scale) >> down_shift;
  104. return value < 0 ? std::min<s32>(converted_value - acumm, -1)
  105. : std::max<s32>(converted_value + acumm, 1);
  106. };
  107. if (src.enable) {
  108. scissor.offset.x = scale_up(static_cast<s32>(src.min_x));
  109. scissor.offset.y = scale_up(static_cast<s32>(src.min_y));
  110. scissor.extent.width = scale_up(src.max_x - src.min_x);
  111. scissor.extent.height = scale_up(src.max_y - src.min_y);
  112. } else {
  113. scissor.offset.x = 0;
  114. scissor.offset.y = 0;
  115. scissor.extent.width = std::numeric_limits<s32>::max();
  116. scissor.extent.height = std::numeric_limits<s32>::max();
  117. }
  118. return scissor;
  119. }
  120. DrawParams MakeDrawParams(const MaxwellDrawState& draw_state, u32 num_instances, bool is_indexed) {
  121. DrawParams params{
  122. .base_instance = draw_state.base_instance,
  123. .num_instances = num_instances,
  124. .base_vertex = is_indexed ? draw_state.base_index : draw_state.vertex_buffer.first,
  125. .num_vertices = is_indexed ? draw_state.index_buffer.count : draw_state.vertex_buffer.count,
  126. .first_index = is_indexed ? draw_state.index_buffer.first : 0,
  127. .is_indexed = is_indexed,
  128. };
  129. // 6 triangle vertices per quad, base vertex is part of the index
  130. // See BindQuadIndexBuffer for more details
  131. if (draw_state.topology == Maxwell::PrimitiveTopology::Quads) {
  132. params.num_vertices = (params.num_vertices / 4) * 6;
  133. params.base_vertex = 0;
  134. params.is_indexed = true;
  135. } else if (draw_state.topology == Maxwell::PrimitiveTopology::QuadStrip) {
  136. params.num_vertices = (params.num_vertices - 2) / 2 * 6;
  137. params.base_vertex = 0;
  138. params.is_indexed = true;
  139. }
  140. return params;
  141. }
  142. } // Anonymous namespace
  143. RasterizerVulkan::RasterizerVulkan(Core::Frontend::EmuWindow& emu_window_, Tegra::GPU& gpu_,
  144. Core::Memory::Memory& cpu_memory_, ScreenInfo& screen_info_,
  145. const Device& device_, MemoryAllocator& memory_allocator_,
  146. StateTracker& state_tracker_, Scheduler& scheduler_)
  147. : RasterizerAccelerated{cpu_memory_}, gpu{gpu_}, screen_info{screen_info_}, device{device_},
  148. memory_allocator{memory_allocator_}, state_tracker{state_tracker_}, scheduler{scheduler_},
  149. staging_pool(device, memory_allocator, scheduler), descriptor_pool(device, scheduler),
  150. guest_descriptor_queue(device, scheduler), compute_pass_descriptor_queue(device, scheduler),
  151. blit_image(device, scheduler, state_tracker, descriptor_pool), render_pass_cache(device),
  152. texture_cache_runtime{
  153. device, scheduler, memory_allocator, staging_pool,
  154. blit_image, render_pass_cache, descriptor_pool, compute_pass_descriptor_queue},
  155. texture_cache(texture_cache_runtime, *this),
  156. buffer_cache_runtime(device, memory_allocator, scheduler, staging_pool,
  157. guest_descriptor_queue, compute_pass_descriptor_queue, descriptor_pool),
  158. buffer_cache(*this, cpu_memory_, buffer_cache_runtime),
  159. pipeline_cache(*this, device, scheduler, descriptor_pool, guest_descriptor_queue,
  160. render_pass_cache, buffer_cache, texture_cache, gpu.ShaderNotify()),
  161. query_cache{*this, cpu_memory_, device, scheduler},
  162. accelerate_dma(buffer_cache, texture_cache, scheduler),
  163. fence_manager(*this, gpu, texture_cache, buffer_cache, query_cache, device, scheduler),
  164. wfi_event(device.GetLogical().CreateEvent()) {
  165. scheduler.SetQueryCache(query_cache);
  166. }
  167. RasterizerVulkan::~RasterizerVulkan() = default;
  168. template <typename Func>
  169. void RasterizerVulkan::PrepareDraw(bool is_indexed, Func&& draw_func) {
  170. MICROPROFILE_SCOPE(Vulkan_Drawing);
  171. SCOPE_EXIT({ gpu.TickWork(); });
  172. FlushWork();
  173. gpu_memory->FlushCaching();
  174. query_cache.UpdateCounters();
  175. GraphicsPipeline* const pipeline{pipeline_cache.CurrentGraphicsPipeline()};
  176. if (!pipeline) {
  177. return;
  178. }
  179. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  180. // update engine as channel may be different.
  181. pipeline->SetEngine(maxwell3d, gpu_memory);
  182. pipeline->Configure(is_indexed);
  183. BeginTransformFeedback();
  184. UpdateDynamicStates();
  185. draw_func();
  186. EndTransformFeedback();
  187. }
  188. void RasterizerVulkan::Draw(bool is_indexed, u32 instance_count) {
  189. PrepareDraw(is_indexed, [this, is_indexed, instance_count] {
  190. const auto& draw_state = maxwell3d->draw_manager->GetDrawState();
  191. const u32 num_instances{instance_count};
  192. const DrawParams draw_params{MakeDrawParams(draw_state, num_instances, is_indexed)};
  193. scheduler.Record([draw_params](vk::CommandBuffer cmdbuf) {
  194. if (draw_params.is_indexed) {
  195. cmdbuf.DrawIndexed(draw_params.num_vertices, draw_params.num_instances,
  196. draw_params.first_index, draw_params.base_vertex,
  197. draw_params.base_instance);
  198. } else {
  199. cmdbuf.Draw(draw_params.num_vertices, draw_params.num_instances,
  200. draw_params.base_vertex, draw_params.base_instance);
  201. }
  202. });
  203. });
  204. }
  205. void RasterizerVulkan::DrawIndirect() {
  206. const auto& params = maxwell3d->draw_manager->GetIndirectParams();
  207. buffer_cache.SetDrawIndirect(&params);
  208. PrepareDraw(params.is_indexed, [this, &params] {
  209. const auto indirect_buffer = buffer_cache.GetDrawIndirectBuffer();
  210. const auto& buffer = indirect_buffer.first;
  211. const auto& offset = indirect_buffer.second;
  212. if (params.include_count) {
  213. const auto count = buffer_cache.GetDrawIndirectCount();
  214. const auto& draw_buffer = count.first;
  215. const auto& offset_base = count.second;
  216. scheduler.Record([draw_buffer_obj = draw_buffer->Handle(),
  217. buffer_obj = buffer->Handle(), offset_base, offset,
  218. params](vk::CommandBuffer cmdbuf) {
  219. if (params.is_indexed) {
  220. cmdbuf.DrawIndexedIndirectCount(
  221. buffer_obj, offset, draw_buffer_obj, offset_base,
  222. static_cast<u32>(params.max_draw_counts), static_cast<u32>(params.stride));
  223. } else {
  224. cmdbuf.DrawIndirectCount(buffer_obj, offset, draw_buffer_obj, offset_base,
  225. static_cast<u32>(params.max_draw_counts),
  226. static_cast<u32>(params.stride));
  227. }
  228. });
  229. return;
  230. }
  231. scheduler.Record([buffer_obj = buffer->Handle(), offset, params](vk::CommandBuffer cmdbuf) {
  232. if (params.is_indexed) {
  233. cmdbuf.DrawIndexedIndirect(buffer_obj, offset,
  234. static_cast<u32>(params.max_draw_counts),
  235. static_cast<u32>(params.stride));
  236. } else {
  237. cmdbuf.DrawIndirect(buffer_obj, offset, static_cast<u32>(params.max_draw_counts),
  238. static_cast<u32>(params.stride));
  239. }
  240. });
  241. });
  242. buffer_cache.SetDrawIndirect(nullptr);
  243. }
  244. void RasterizerVulkan::DrawTexture() {
  245. MICROPROFILE_SCOPE(Vulkan_Drawing);
  246. SCOPE_EXIT({ gpu.TickWork(); });
  247. FlushWork();
  248. query_cache.UpdateCounters();
  249. texture_cache.SynchronizeGraphicsDescriptors();
  250. texture_cache.UpdateRenderTargets(false);
  251. UpdateDynamicStates();
  252. const auto& draw_texture_state = maxwell3d->draw_manager->GetDrawTextureState();
  253. const auto& sampler = texture_cache.GetGraphicsSampler(draw_texture_state.src_sampler);
  254. const auto& texture = texture_cache.GetImageView(draw_texture_state.src_texture);
  255. Region2D dst_region = {Offset2D{.x = static_cast<s32>(draw_texture_state.dst_x0),
  256. .y = static_cast<s32>(draw_texture_state.dst_y0)},
  257. Offset2D{.x = static_cast<s32>(draw_texture_state.dst_x1),
  258. .y = static_cast<s32>(draw_texture_state.dst_y1)}};
  259. Region2D src_region = {Offset2D{.x = static_cast<s32>(draw_texture_state.src_x0),
  260. .y = static_cast<s32>(draw_texture_state.src_y0)},
  261. Offset2D{.x = static_cast<s32>(draw_texture_state.src_x1),
  262. .y = static_cast<s32>(draw_texture_state.src_y1)}};
  263. blit_image.BlitColor(texture_cache.GetFramebuffer(), texture.RenderTarget(),
  264. texture.ImageHandle(), sampler->Handle(), dst_region, src_region,
  265. texture.size);
  266. }
  267. void RasterizerVulkan::Clear(u32 layer_count) {
  268. MICROPROFILE_SCOPE(Vulkan_Clearing);
  269. FlushWork();
  270. gpu_memory->FlushCaching();
  271. query_cache.UpdateCounters();
  272. auto& regs = maxwell3d->regs;
  273. const bool use_color = regs.clear_surface.R || regs.clear_surface.G || regs.clear_surface.B ||
  274. regs.clear_surface.A;
  275. const bool use_depth = regs.clear_surface.Z;
  276. const bool use_stencil = regs.clear_surface.S;
  277. if (!use_color && !use_depth && !use_stencil) {
  278. return;
  279. }
  280. std::scoped_lock lock{texture_cache.mutex};
  281. texture_cache.UpdateRenderTargets(true);
  282. const Framebuffer* const framebuffer = texture_cache.GetFramebuffer();
  283. const VkExtent2D render_area = framebuffer->RenderArea();
  284. scheduler.RequestRenderpass(framebuffer);
  285. u32 up_scale = 1;
  286. u32 down_shift = 0;
  287. if (texture_cache.IsRescaling()) {
  288. up_scale = Settings::values.resolution_info.up_scale;
  289. down_shift = Settings::values.resolution_info.down_shift;
  290. }
  291. UpdateViewportsState(regs);
  292. VkRect2D default_scissor;
  293. default_scissor.offset.x = 0;
  294. default_scissor.offset.y = 0;
  295. default_scissor.extent.width = std::numeric_limits<s32>::max();
  296. default_scissor.extent.height = std::numeric_limits<s32>::max();
  297. VkClearRect clear_rect{
  298. .rect = regs.clear_control.use_scissor ? GetScissorState(regs, 0, up_scale, down_shift)
  299. : default_scissor,
  300. .baseArrayLayer = regs.clear_surface.layer,
  301. .layerCount = layer_count,
  302. };
  303. if (clear_rect.rect.extent.width == 0 || clear_rect.rect.extent.height == 0) {
  304. return;
  305. }
  306. clear_rect.rect.extent = VkExtent2D{
  307. .width = std::min(clear_rect.rect.extent.width, render_area.width),
  308. .height = std::min(clear_rect.rect.extent.height, render_area.height),
  309. };
  310. const u32 color_attachment = regs.clear_surface.RT;
  311. if (use_color && framebuffer->HasAspectColorBit(color_attachment)) {
  312. VkClearValue clear_value;
  313. bool is_integer = false;
  314. bool is_signed = false;
  315. size_t int_size = 8;
  316. for (std::size_t i = 0; i < Tegra::Engines::Maxwell3D::Regs::NumRenderTargets; ++i) {
  317. const auto& this_rt = regs.rt[i];
  318. if (this_rt.Address() == 0) {
  319. continue;
  320. }
  321. if (this_rt.format == Tegra::RenderTargetFormat::NONE) {
  322. continue;
  323. }
  324. const auto format =
  325. VideoCore::Surface::PixelFormatFromRenderTargetFormat(this_rt.format);
  326. is_integer = IsPixelFormatInteger(format);
  327. is_signed = IsPixelFormatSignedInteger(format);
  328. int_size = PixelComponentSizeBitsInteger(format);
  329. break;
  330. }
  331. if (!is_integer) {
  332. std::memcpy(clear_value.color.float32, regs.clear_color.data(),
  333. regs.clear_color.size() * sizeof(f32));
  334. } else if (!is_signed) {
  335. for (size_t i = 0; i < 4; i++) {
  336. clear_value.color.uint32[i] = static_cast<u32>(
  337. static_cast<f32>(static_cast<u64>(int_size) << 1U) * regs.clear_color[i]);
  338. }
  339. } else {
  340. for (size_t i = 0; i < 4; i++) {
  341. clear_value.color.int32[i] =
  342. static_cast<s32>(static_cast<f32>(static_cast<s64>(int_size - 1) << 1) *
  343. (regs.clear_color[i] - 0.5f));
  344. }
  345. }
  346. if (regs.clear_surface.R && regs.clear_surface.G && regs.clear_surface.B &&
  347. regs.clear_surface.A) {
  348. scheduler.Record([color_attachment, clear_value, clear_rect](vk::CommandBuffer cmdbuf) {
  349. const VkClearAttachment attachment{
  350. .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
  351. .colorAttachment = color_attachment,
  352. .clearValue = clear_value,
  353. };
  354. cmdbuf.ClearAttachments(attachment, clear_rect);
  355. });
  356. } else {
  357. u8 color_mask = static_cast<u8>(regs.clear_surface.R | regs.clear_surface.G << 1 |
  358. regs.clear_surface.B << 2 | regs.clear_surface.A << 3);
  359. Region2D dst_region = {
  360. Offset2D{.x = clear_rect.rect.offset.x, .y = clear_rect.rect.offset.y},
  361. Offset2D{.x = clear_rect.rect.offset.x +
  362. static_cast<s32>(clear_rect.rect.extent.width),
  363. .y = clear_rect.rect.offset.y +
  364. static_cast<s32>(clear_rect.rect.extent.height)}};
  365. blit_image.ClearColor(framebuffer, color_mask, regs.clear_color, dst_region);
  366. }
  367. }
  368. if (!use_depth && !use_stencil) {
  369. return;
  370. }
  371. VkImageAspectFlags aspect_flags = 0;
  372. if (use_depth && framebuffer->HasAspectDepthBit()) {
  373. aspect_flags |= VK_IMAGE_ASPECT_DEPTH_BIT;
  374. }
  375. if (use_stencil && framebuffer->HasAspectStencilBit()) {
  376. aspect_flags |= VK_IMAGE_ASPECT_STENCIL_BIT;
  377. }
  378. if (aspect_flags == 0) {
  379. return;
  380. }
  381. scheduler.Record([clear_depth = regs.clear_depth, clear_stencil = regs.clear_stencil,
  382. clear_rect, aspect_flags](vk::CommandBuffer cmdbuf) {
  383. VkClearAttachment attachment;
  384. attachment.aspectMask = aspect_flags;
  385. attachment.colorAttachment = 0;
  386. attachment.clearValue.depthStencil.depth = clear_depth;
  387. attachment.clearValue.depthStencil.stencil = clear_stencil;
  388. cmdbuf.ClearAttachments(attachment, clear_rect);
  389. });
  390. }
  391. void RasterizerVulkan::DispatchCompute() {
  392. FlushWork();
  393. gpu_memory->FlushCaching();
  394. ComputePipeline* const pipeline{pipeline_cache.CurrentComputePipeline()};
  395. if (!pipeline) {
  396. return;
  397. }
  398. std::scoped_lock lock{texture_cache.mutex, buffer_cache.mutex};
  399. pipeline->Configure(*kepler_compute, *gpu_memory, scheduler, buffer_cache, texture_cache);
  400. const auto& qmd{kepler_compute->launch_description};
  401. const std::array<u32, 3> dim{qmd.grid_dim_x, qmd.grid_dim_y, qmd.grid_dim_z};
  402. scheduler.RequestOutsideRenderPassOperationContext();
  403. scheduler.Record([dim](vk::CommandBuffer cmdbuf) { cmdbuf.Dispatch(dim[0], dim[1], dim[2]); });
  404. }
  405. void RasterizerVulkan::ResetCounter(VideoCore::QueryType type) {
  406. query_cache.ResetCounter(type);
  407. }
  408. void RasterizerVulkan::Query(GPUVAddr gpu_addr, VideoCore::QueryType type,
  409. std::optional<u64> timestamp) {
  410. query_cache.Query(gpu_addr, type, timestamp);
  411. }
  412. void RasterizerVulkan::BindGraphicsUniformBuffer(size_t stage, u32 index, GPUVAddr gpu_addr,
  413. u32 size) {
  414. buffer_cache.BindGraphicsUniformBuffer(stage, index, gpu_addr, size);
  415. }
  416. void Vulkan::RasterizerVulkan::DisableGraphicsUniformBuffer(size_t stage, u32 index) {
  417. buffer_cache.DisableGraphicsUniformBuffer(stage, index);
  418. }
  419. void RasterizerVulkan::FlushAll() {}
  420. void RasterizerVulkan::FlushRegion(VAddr addr, u64 size, VideoCommon::CacheType which) {
  421. if (addr == 0 || size == 0) {
  422. return;
  423. }
  424. if (True(which & VideoCommon::CacheType::TextureCache)) {
  425. std::scoped_lock lock{texture_cache.mutex};
  426. texture_cache.DownloadMemory(addr, size);
  427. }
  428. if ((True(which & VideoCommon::CacheType::BufferCache))) {
  429. std::scoped_lock lock{buffer_cache.mutex};
  430. buffer_cache.DownloadMemory(addr, size);
  431. }
  432. if ((True(which & VideoCommon::CacheType::QueryCache))) {
  433. query_cache.FlushRegion(addr, size);
  434. }
  435. }
  436. bool RasterizerVulkan::MustFlushRegion(VAddr addr, u64 size, VideoCommon::CacheType which) {
  437. if ((True(which & VideoCommon::CacheType::BufferCache))) {
  438. std::scoped_lock lock{buffer_cache.mutex};
  439. if (buffer_cache.IsRegionGpuModified(addr, size)) {
  440. return true;
  441. }
  442. }
  443. if (!Settings::IsGPULevelHigh()) {
  444. return false;
  445. }
  446. if (True(which & VideoCommon::CacheType::TextureCache)) {
  447. std::scoped_lock lock{texture_cache.mutex};
  448. return texture_cache.IsRegionGpuModified(addr, size);
  449. }
  450. return false;
  451. }
  452. VideoCore::RasterizerDownloadArea RasterizerVulkan::GetFlushArea(VAddr addr, u64 size) {
  453. {
  454. std::scoped_lock lock{texture_cache.mutex};
  455. auto area = texture_cache.GetFlushArea(addr, size);
  456. if (area) {
  457. return *area;
  458. }
  459. }
  460. VideoCore::RasterizerDownloadArea new_area{
  461. .start_address = Common::AlignDown(addr, Core::Memory::YUZU_PAGESIZE),
  462. .end_address = Common::AlignUp(addr + size, Core::Memory::YUZU_PAGESIZE),
  463. .preemtive = true,
  464. };
  465. return new_area;
  466. }
  467. void RasterizerVulkan::InvalidateRegion(VAddr addr, u64 size, VideoCommon::CacheType which) {
  468. if (addr == 0 || size == 0) {
  469. return;
  470. }
  471. if (True(which & VideoCommon::CacheType::TextureCache)) {
  472. std::scoped_lock lock{texture_cache.mutex};
  473. texture_cache.WriteMemory(addr, size);
  474. }
  475. if ((True(which & VideoCommon::CacheType::BufferCache))) {
  476. std::scoped_lock lock{buffer_cache.mutex};
  477. buffer_cache.WriteMemory(addr, size);
  478. }
  479. if ((True(which & VideoCommon::CacheType::QueryCache))) {
  480. query_cache.InvalidateRegion(addr, size);
  481. }
  482. if ((True(which & VideoCommon::CacheType::ShaderCache))) {
  483. pipeline_cache.InvalidateRegion(addr, size);
  484. }
  485. }
  486. void RasterizerVulkan::InnerInvalidation(std::span<const std::pair<VAddr, std::size_t>> sequences) {
  487. {
  488. std::scoped_lock lock{texture_cache.mutex};
  489. for (const auto& [addr, size] : sequences) {
  490. texture_cache.WriteMemory(addr, size);
  491. }
  492. }
  493. {
  494. std::scoped_lock lock{buffer_cache.mutex};
  495. for (const auto& [addr, size] : sequences) {
  496. buffer_cache.WriteMemory(addr, size);
  497. }
  498. }
  499. {
  500. for (const auto& [addr, size] : sequences) {
  501. query_cache.InvalidateRegion(addr, size);
  502. pipeline_cache.InvalidateRegion(addr, size);
  503. }
  504. }
  505. }
  506. void RasterizerVulkan::OnCPUWrite(VAddr addr, u64 size) {
  507. if (addr == 0 || size == 0) {
  508. return;
  509. }
  510. pipeline_cache.OnCPUWrite(addr, size);
  511. {
  512. std::scoped_lock lock{texture_cache.mutex};
  513. texture_cache.WriteMemory(addr, size);
  514. }
  515. {
  516. std::scoped_lock lock{buffer_cache.mutex};
  517. buffer_cache.CachedWriteMemory(addr, size);
  518. }
  519. }
  520. void RasterizerVulkan::InvalidateGPUCache() {
  521. pipeline_cache.SyncGuestHost();
  522. {
  523. std::scoped_lock lock{buffer_cache.mutex};
  524. buffer_cache.FlushCachedWrites();
  525. }
  526. }
  527. void RasterizerVulkan::UnmapMemory(VAddr addr, u64 size) {
  528. {
  529. std::scoped_lock lock{texture_cache.mutex};
  530. texture_cache.UnmapMemory(addr, size);
  531. }
  532. {
  533. std::scoped_lock lock{buffer_cache.mutex};
  534. buffer_cache.WriteMemory(addr, size);
  535. }
  536. pipeline_cache.OnCPUWrite(addr, size);
  537. }
  538. void RasterizerVulkan::ModifyGPUMemory(size_t as_id, GPUVAddr addr, u64 size) {
  539. {
  540. std::scoped_lock lock{texture_cache.mutex};
  541. texture_cache.UnmapGPUMemory(as_id, addr, size);
  542. }
  543. }
  544. void RasterizerVulkan::SignalFence(std::function<void()>&& func) {
  545. fence_manager.SignalFence(std::move(func));
  546. }
  547. void RasterizerVulkan::SyncOperation(std::function<void()>&& func) {
  548. fence_manager.SyncOperation(std::move(func));
  549. }
  550. void RasterizerVulkan::SignalSyncPoint(u32 value) {
  551. fence_manager.SignalSyncPoint(value);
  552. }
  553. void RasterizerVulkan::SignalReference() {
  554. fence_manager.SignalReference();
  555. }
  556. void RasterizerVulkan::ReleaseFences() {
  557. fence_manager.WaitPendingFences();
  558. }
  559. void RasterizerVulkan::FlushAndInvalidateRegion(VAddr addr, u64 size,
  560. VideoCommon::CacheType which) {
  561. if (Settings::IsGPULevelExtreme()) {
  562. FlushRegion(addr, size, which);
  563. }
  564. InvalidateRegion(addr, size, which);
  565. }
  566. void RasterizerVulkan::WaitForIdle() {
  567. // Everything but wait pixel operations. This intentionally includes FRAGMENT_SHADER_BIT because
  568. // fragment shaders can still write storage buffers.
  569. VkPipelineStageFlags flags =
  570. VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT | VK_PIPELINE_STAGE_VERTEX_INPUT_BIT |
  571. VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT |
  572. VK_PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT |
  573. VK_PIPELINE_STAGE_GEOMETRY_SHADER_BIT | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT |
  574. VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT | VK_PIPELINE_STAGE_TRANSFER_BIT;
  575. if (device.IsExtTransformFeedbackSupported()) {
  576. flags |= VK_PIPELINE_STAGE_TRANSFORM_FEEDBACK_BIT_EXT;
  577. }
  578. scheduler.RequestOutsideRenderPassOperationContext();
  579. scheduler.Record([event = *wfi_event, flags](vk::CommandBuffer cmdbuf) {
  580. cmdbuf.SetEvent(event, flags);
  581. cmdbuf.WaitEvents(event, flags, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, {}, {}, {});
  582. });
  583. fence_manager.SignalOrdering();
  584. }
  585. void RasterizerVulkan::FragmentBarrier() {
  586. // We already put barriers when a render pass finishes
  587. scheduler.RequestOutsideRenderPassOperationContext();
  588. }
  589. void RasterizerVulkan::TiledCacheBarrier() {
  590. // TODO: Implementing tiled barriers requires rewriting a good chunk of the Vulkan backend
  591. }
  592. void RasterizerVulkan::FlushCommands() {
  593. if (draw_counter == 0) {
  594. return;
  595. }
  596. draw_counter = 0;
  597. scheduler.Flush();
  598. }
  599. void RasterizerVulkan::TickFrame() {
  600. draw_counter = 0;
  601. guest_descriptor_queue.TickFrame();
  602. compute_pass_descriptor_queue.TickFrame();
  603. fence_manager.TickFrame();
  604. staging_pool.TickFrame();
  605. {
  606. std::scoped_lock lock{texture_cache.mutex};
  607. texture_cache.TickFrame();
  608. }
  609. {
  610. std::scoped_lock lock{buffer_cache.mutex};
  611. buffer_cache.TickFrame();
  612. }
  613. }
  614. bool RasterizerVulkan::AccelerateConditionalRendering() {
  615. gpu_memory->FlushCaching();
  616. if (Settings::IsGPULevelHigh()) {
  617. // TODO(Blinkhawk): Reimplement Host conditional rendering.
  618. return false;
  619. }
  620. // Medium / Low Hack: stub any checks on queries written into the buffer cache.
  621. const GPUVAddr condition_address{maxwell3d->regs.render_enable.Address()};
  622. Maxwell::ReportSemaphore::Compare cmp;
  623. if (gpu_memory->IsMemoryDirty(condition_address, sizeof(cmp),
  624. VideoCommon::CacheType::BufferCache |
  625. VideoCommon::CacheType::QueryCache)) {
  626. return true;
  627. }
  628. return false;
  629. }
  630. bool RasterizerVulkan::AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Surface& src,
  631. const Tegra::Engines::Fermi2D::Surface& dst,
  632. const Tegra::Engines::Fermi2D::Config& copy_config) {
  633. std::scoped_lock lock{texture_cache.mutex};
  634. return texture_cache.BlitImage(dst, src, copy_config);
  635. }
  636. Tegra::Engines::AccelerateDMAInterface& RasterizerVulkan::AccessAccelerateDMA() {
  637. return accelerate_dma;
  638. }
  639. void RasterizerVulkan::AccelerateInlineToMemory(GPUVAddr address, size_t copy_size,
  640. std::span<const u8> memory) {
  641. auto cpu_addr = gpu_memory->GpuToCpuAddress(address);
  642. if (!cpu_addr) [[unlikely]] {
  643. gpu_memory->WriteBlock(address, memory.data(), copy_size);
  644. return;
  645. }
  646. gpu_memory->WriteBlockUnsafe(address, memory.data(), copy_size);
  647. {
  648. std::unique_lock<std::recursive_mutex> lock{buffer_cache.mutex};
  649. if (!buffer_cache.InlineMemory(*cpu_addr, copy_size, memory)) {
  650. buffer_cache.WriteMemory(*cpu_addr, copy_size);
  651. }
  652. }
  653. {
  654. std::scoped_lock lock_texture{texture_cache.mutex};
  655. texture_cache.WriteMemory(*cpu_addr, copy_size);
  656. }
  657. pipeline_cache.InvalidateRegion(*cpu_addr, copy_size);
  658. query_cache.InvalidateRegion(*cpu_addr, copy_size);
  659. }
  660. bool RasterizerVulkan::AccelerateDisplay(const Tegra::FramebufferConfig& config,
  661. VAddr framebuffer_addr, u32 pixel_stride) {
  662. if (!framebuffer_addr) {
  663. return false;
  664. }
  665. std::scoped_lock lock{texture_cache.mutex};
  666. ImageView* const image_view = texture_cache.TryFindFramebufferImageView(framebuffer_addr);
  667. if (!image_view) {
  668. return false;
  669. }
  670. screen_info.image = image_view->ImageHandle();
  671. screen_info.image_view = image_view->Handle(Shader::TextureType::Color2D);
  672. screen_info.width = image_view->size.width;
  673. screen_info.height = image_view->size.height;
  674. screen_info.is_srgb = VideoCore::Surface::IsPixelFormatSRGB(image_view->format);
  675. return true;
  676. }
  677. void RasterizerVulkan::LoadDiskResources(u64 title_id, std::stop_token stop_loading,
  678. const VideoCore::DiskResourceLoadCallback& callback) {
  679. pipeline_cache.LoadDiskResources(title_id, stop_loading, callback);
  680. }
  681. void RasterizerVulkan::FlushWork() {
  682. static constexpr u32 DRAWS_TO_DISPATCH = 4096;
  683. // Only check multiples of 8 draws
  684. static_assert(DRAWS_TO_DISPATCH % 8 == 0);
  685. if ((++draw_counter & 7) != 7) {
  686. return;
  687. }
  688. if (draw_counter < DRAWS_TO_DISPATCH) {
  689. // Send recorded tasks to the worker thread
  690. scheduler.DispatchWork();
  691. return;
  692. }
  693. // Otherwise (every certain number of draws) flush execution.
  694. // This submits commands to the Vulkan driver.
  695. scheduler.Flush();
  696. draw_counter = 0;
  697. }
  698. AccelerateDMA::AccelerateDMA(BufferCache& buffer_cache_, TextureCache& texture_cache_,
  699. Scheduler& scheduler_)
  700. : buffer_cache{buffer_cache_}, texture_cache{texture_cache_}, scheduler{scheduler_} {}
  701. bool AccelerateDMA::BufferClear(GPUVAddr src_address, u64 amount, u32 value) {
  702. std::scoped_lock lock{buffer_cache.mutex};
  703. return buffer_cache.DMAClear(src_address, amount, value);
  704. }
  705. bool AccelerateDMA::BufferCopy(GPUVAddr src_address, GPUVAddr dest_address, u64 amount) {
  706. std::scoped_lock lock{buffer_cache.mutex};
  707. return buffer_cache.DMACopy(src_address, dest_address, amount);
  708. }
  709. template <bool IS_IMAGE_UPLOAD>
  710. bool AccelerateDMA::DmaBufferImageCopy(const Tegra::DMA::ImageCopy& copy_info,
  711. const Tegra::DMA::BufferOperand& buffer_operand,
  712. const Tegra::DMA::ImageOperand& image_operand) {
  713. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  714. const auto image_id = texture_cache.DmaImageId(image_operand, IS_IMAGE_UPLOAD);
  715. if (image_id == VideoCommon::NULL_IMAGE_ID) {
  716. return false;
  717. }
  718. const u32 buffer_size = static_cast<u32>(buffer_operand.pitch * buffer_operand.height);
  719. static constexpr auto sync_info = VideoCommon::ObtainBufferSynchronize::FullSynchronize;
  720. const auto post_op = VideoCommon::ObtainBufferOperation::DoNothing;
  721. const auto [buffer, offset] =
  722. buffer_cache.ObtainBuffer(buffer_operand.address, buffer_size, sync_info, post_op);
  723. const auto [image, copy] = texture_cache.DmaBufferImageCopy(
  724. copy_info, buffer_operand, image_operand, image_id, IS_IMAGE_UPLOAD);
  725. const std::span copy_span{&copy, 1};
  726. if constexpr (IS_IMAGE_UPLOAD) {
  727. image->UploadMemory(buffer->Handle(), offset, copy_span);
  728. } else {
  729. texture_cache.DownloadImageIntoBuffer(image, buffer->Handle(), offset, copy_span,
  730. buffer_operand.address, buffer_size);
  731. }
  732. return true;
  733. }
  734. bool AccelerateDMA::ImageToBuffer(const Tegra::DMA::ImageCopy& copy_info,
  735. const Tegra::DMA::ImageOperand& image_operand,
  736. const Tegra::DMA::BufferOperand& buffer_operand) {
  737. return DmaBufferImageCopy<false>(copy_info, buffer_operand, image_operand);
  738. }
  739. bool AccelerateDMA::BufferToImage(const Tegra::DMA::ImageCopy& copy_info,
  740. const Tegra::DMA::BufferOperand& buffer_operand,
  741. const Tegra::DMA::ImageOperand& image_operand) {
  742. return DmaBufferImageCopy<true>(copy_info, buffer_operand, image_operand);
  743. }
  744. void RasterizerVulkan::UpdateDynamicStates() {
  745. auto& regs = maxwell3d->regs;
  746. UpdateViewportsState(regs);
  747. UpdateScissorsState(regs);
  748. UpdateDepthBias(regs);
  749. UpdateBlendConstants(regs);
  750. UpdateDepthBounds(regs);
  751. UpdateStencilFaces(regs);
  752. UpdateLineWidth(regs);
  753. if (device.IsExtExtendedDynamicStateSupported()) {
  754. UpdateCullMode(regs);
  755. UpdateDepthCompareOp(regs);
  756. UpdateFrontFace(regs);
  757. UpdateStencilOp(regs);
  758. if (device.IsExtVertexInputDynamicStateSupported()) {
  759. UpdateVertexInput(regs);
  760. }
  761. if (state_tracker.TouchStateEnable()) {
  762. UpdateDepthBoundsTestEnable(regs);
  763. UpdateDepthTestEnable(regs);
  764. UpdateDepthWriteEnable(regs);
  765. UpdateStencilTestEnable(regs);
  766. if (device.IsExtExtendedDynamicState2Supported()) {
  767. UpdatePrimitiveRestartEnable(regs);
  768. UpdateRasterizerDiscardEnable(regs);
  769. UpdateDepthBiasEnable(regs);
  770. }
  771. if (device.IsExtExtendedDynamicState3EnablesSupported()) {
  772. UpdateLogicOpEnable(regs);
  773. UpdateDepthClampEnable(regs);
  774. }
  775. }
  776. if (device.IsExtExtendedDynamicState2ExtrasSupported()) {
  777. UpdateLogicOp(regs);
  778. }
  779. if (device.IsExtExtendedDynamicState3Supported()) {
  780. UpdateBlending(regs);
  781. }
  782. }
  783. }
  784. void RasterizerVulkan::BeginTransformFeedback() {
  785. const auto& regs = maxwell3d->regs;
  786. if (regs.transform_feedback_enabled == 0) {
  787. return;
  788. }
  789. if (!device.IsExtTransformFeedbackSupported()) {
  790. LOG_ERROR(Render_Vulkan, "Transform feedbacks used but not supported");
  791. return;
  792. }
  793. UNIMPLEMENTED_IF(regs.IsShaderConfigEnabled(Maxwell::ShaderType::TessellationInit) ||
  794. regs.IsShaderConfigEnabled(Maxwell::ShaderType::Tessellation));
  795. scheduler.Record(
  796. [](vk::CommandBuffer cmdbuf) { cmdbuf.BeginTransformFeedbackEXT(0, 0, nullptr, nullptr); });
  797. }
  798. void RasterizerVulkan::EndTransformFeedback() {
  799. const auto& regs = maxwell3d->regs;
  800. if (regs.transform_feedback_enabled == 0) {
  801. return;
  802. }
  803. if (!device.IsExtTransformFeedbackSupported()) {
  804. return;
  805. }
  806. scheduler.Record(
  807. [](vk::CommandBuffer cmdbuf) { cmdbuf.EndTransformFeedbackEXT(0, 0, nullptr, nullptr); });
  808. }
  809. void RasterizerVulkan::UpdateViewportsState(Tegra::Engines::Maxwell3D::Regs& regs) {
  810. if (!state_tracker.TouchViewports()) {
  811. return;
  812. }
  813. if (!regs.viewport_scale_offset_enabled) {
  814. const auto x = static_cast<float>(regs.surface_clip.x);
  815. const auto y = static_cast<float>(regs.surface_clip.y);
  816. const auto width = static_cast<float>(regs.surface_clip.width);
  817. const auto height = static_cast<float>(regs.surface_clip.height);
  818. VkViewport viewport{
  819. .x = x,
  820. .y = y,
  821. .width = width != 0.0f ? width : 1.0f,
  822. .height = height != 0.0f ? height : 1.0f,
  823. .minDepth = 0.0f,
  824. .maxDepth = 1.0f,
  825. };
  826. scheduler.Record([viewport](vk::CommandBuffer cmdbuf) { cmdbuf.SetViewport(0, viewport); });
  827. return;
  828. }
  829. const bool is_rescaling{texture_cache.IsRescaling()};
  830. const float scale = is_rescaling ? Settings::values.resolution_info.up_factor : 1.0f;
  831. const std::array viewports{
  832. GetViewportState(device, regs, 0, scale), GetViewportState(device, regs, 1, scale),
  833. GetViewportState(device, regs, 2, scale), GetViewportState(device, regs, 3, scale),
  834. GetViewportState(device, regs, 4, scale), GetViewportState(device, regs, 5, scale),
  835. GetViewportState(device, regs, 6, scale), GetViewportState(device, regs, 7, scale),
  836. GetViewportState(device, regs, 8, scale), GetViewportState(device, regs, 9, scale),
  837. GetViewportState(device, regs, 10, scale), GetViewportState(device, regs, 11, scale),
  838. GetViewportState(device, regs, 12, scale), GetViewportState(device, regs, 13, scale),
  839. GetViewportState(device, regs, 14, scale), GetViewportState(device, regs, 15, scale),
  840. };
  841. scheduler.Record([viewports](vk::CommandBuffer cmdbuf) { cmdbuf.SetViewport(0, viewports); });
  842. }
  843. void RasterizerVulkan::UpdateScissorsState(Tegra::Engines::Maxwell3D::Regs& regs) {
  844. if (!state_tracker.TouchScissors()) {
  845. return;
  846. }
  847. u32 up_scale = 1;
  848. u32 down_shift = 0;
  849. if (texture_cache.IsRescaling()) {
  850. up_scale = Settings::values.resolution_info.up_scale;
  851. down_shift = Settings::values.resolution_info.down_shift;
  852. }
  853. const std::array scissors{
  854. GetScissorState(regs, 0, up_scale, down_shift),
  855. GetScissorState(regs, 1, up_scale, down_shift),
  856. GetScissorState(regs, 2, up_scale, down_shift),
  857. GetScissorState(regs, 3, up_scale, down_shift),
  858. GetScissorState(regs, 4, up_scale, down_shift),
  859. GetScissorState(regs, 5, up_scale, down_shift),
  860. GetScissorState(regs, 6, up_scale, down_shift),
  861. GetScissorState(regs, 7, up_scale, down_shift),
  862. GetScissorState(regs, 8, up_scale, down_shift),
  863. GetScissorState(regs, 9, up_scale, down_shift),
  864. GetScissorState(regs, 10, up_scale, down_shift),
  865. GetScissorState(regs, 11, up_scale, down_shift),
  866. GetScissorState(regs, 12, up_scale, down_shift),
  867. GetScissorState(regs, 13, up_scale, down_shift),
  868. GetScissorState(regs, 14, up_scale, down_shift),
  869. GetScissorState(regs, 15, up_scale, down_shift),
  870. };
  871. scheduler.Record([scissors](vk::CommandBuffer cmdbuf) { cmdbuf.SetScissor(0, scissors); });
  872. }
  873. void RasterizerVulkan::UpdateDepthBias(Tegra::Engines::Maxwell3D::Regs& regs) {
  874. if (!state_tracker.TouchDepthBias()) {
  875. return;
  876. }
  877. float units = regs.depth_bias / 2.0f;
  878. const bool is_d24 = regs.zeta.format == Tegra::DepthFormat::Z24_UNORM_S8_UINT ||
  879. regs.zeta.format == Tegra::DepthFormat::X8Z24_UNORM ||
  880. regs.zeta.format == Tegra::DepthFormat::S8Z24_UNORM ||
  881. regs.zeta.format == Tegra::DepthFormat::V8Z24_UNORM;
  882. if (is_d24 && !device.SupportsD24DepthBuffer()) {
  883. // the base formulas can be obtained from here:
  884. // https://docs.microsoft.com/en-us/windows/win32/direct3d11/d3d10-graphics-programming-guide-output-merger-stage-depth-bias
  885. const double rescale_factor =
  886. static_cast<double>(1ULL << (32 - 24)) / (static_cast<double>(0x1.ep+127));
  887. units = static_cast<float>(static_cast<double>(units) * rescale_factor);
  888. }
  889. scheduler.Record([constant = units, clamp = regs.depth_bias_clamp,
  890. factor = regs.slope_scale_depth_bias](vk::CommandBuffer cmdbuf) {
  891. cmdbuf.SetDepthBias(constant, clamp, factor);
  892. });
  893. }
  894. void RasterizerVulkan::UpdateBlendConstants(Tegra::Engines::Maxwell3D::Regs& regs) {
  895. if (!state_tracker.TouchBlendConstants()) {
  896. return;
  897. }
  898. const std::array blend_color = {regs.blend_color.r, regs.blend_color.g, regs.blend_color.b,
  899. regs.blend_color.a};
  900. scheduler.Record(
  901. [blend_color](vk::CommandBuffer cmdbuf) { cmdbuf.SetBlendConstants(blend_color.data()); });
  902. }
  903. void RasterizerVulkan::UpdateDepthBounds(Tegra::Engines::Maxwell3D::Regs& regs) {
  904. if (!state_tracker.TouchDepthBounds()) {
  905. return;
  906. }
  907. scheduler.Record([min = regs.depth_bounds[0], max = regs.depth_bounds[1]](
  908. vk::CommandBuffer cmdbuf) { cmdbuf.SetDepthBounds(min, max); });
  909. }
  910. void RasterizerVulkan::UpdateStencilFaces(Tegra::Engines::Maxwell3D::Regs& regs) {
  911. if (!state_tracker.TouchStencilProperties()) {
  912. return;
  913. }
  914. bool update_references = state_tracker.TouchStencilReference();
  915. bool update_write_mask = state_tracker.TouchStencilWriteMask();
  916. bool update_compare_masks = state_tracker.TouchStencilCompare();
  917. if (state_tracker.TouchStencilSide(regs.stencil_two_side_enable != 0)) {
  918. update_references = true;
  919. update_write_mask = true;
  920. update_compare_masks = true;
  921. }
  922. if (update_references) {
  923. [&]() {
  924. if (regs.stencil_two_side_enable) {
  925. if (!state_tracker.CheckStencilReferenceFront(regs.stencil_front_ref) &&
  926. !state_tracker.CheckStencilReferenceBack(regs.stencil_back_ref)) {
  927. return;
  928. }
  929. } else {
  930. if (!state_tracker.CheckStencilReferenceFront(regs.stencil_front_ref)) {
  931. return;
  932. }
  933. }
  934. scheduler.Record([front_ref = regs.stencil_front_ref, back_ref = regs.stencil_back_ref,
  935. two_sided = regs.stencil_two_side_enable](vk::CommandBuffer cmdbuf) {
  936. const bool set_back = two_sided && front_ref != back_ref;
  937. // Front face
  938. cmdbuf.SetStencilReference(set_back ? VK_STENCIL_FACE_FRONT_BIT
  939. : VK_STENCIL_FACE_FRONT_AND_BACK,
  940. front_ref);
  941. if (set_back) {
  942. cmdbuf.SetStencilReference(VK_STENCIL_FACE_BACK_BIT, back_ref);
  943. }
  944. });
  945. }();
  946. }
  947. if (update_write_mask) {
  948. [&]() {
  949. if (regs.stencil_two_side_enable) {
  950. if (!state_tracker.CheckStencilWriteMaskFront(regs.stencil_front_mask) &&
  951. !state_tracker.CheckStencilWriteMaskBack(regs.stencil_back_mask)) {
  952. return;
  953. }
  954. } else {
  955. if (!state_tracker.CheckStencilWriteMaskFront(regs.stencil_front_mask)) {
  956. return;
  957. }
  958. }
  959. scheduler.Record([front_write_mask = regs.stencil_front_mask,
  960. back_write_mask = regs.stencil_back_mask,
  961. two_sided = regs.stencil_two_side_enable](vk::CommandBuffer cmdbuf) {
  962. const bool set_back = two_sided && front_write_mask != back_write_mask;
  963. // Front face
  964. cmdbuf.SetStencilWriteMask(set_back ? VK_STENCIL_FACE_FRONT_BIT
  965. : VK_STENCIL_FACE_FRONT_AND_BACK,
  966. front_write_mask);
  967. if (set_back) {
  968. cmdbuf.SetStencilWriteMask(VK_STENCIL_FACE_BACK_BIT, back_write_mask);
  969. }
  970. });
  971. }();
  972. }
  973. if (update_compare_masks) {
  974. [&]() {
  975. if (regs.stencil_two_side_enable) {
  976. if (!state_tracker.CheckStencilCompareMaskFront(regs.stencil_front_func_mask) &&
  977. !state_tracker.CheckStencilCompareMaskBack(regs.stencil_back_func_mask)) {
  978. return;
  979. }
  980. } else {
  981. if (!state_tracker.CheckStencilCompareMaskFront(regs.stencil_front_func_mask)) {
  982. return;
  983. }
  984. }
  985. scheduler.Record([front_test_mask = regs.stencil_front_func_mask,
  986. back_test_mask = regs.stencil_back_func_mask,
  987. two_sided = regs.stencil_two_side_enable](vk::CommandBuffer cmdbuf) {
  988. const bool set_back = two_sided && front_test_mask != back_test_mask;
  989. // Front face
  990. cmdbuf.SetStencilCompareMask(set_back ? VK_STENCIL_FACE_FRONT_BIT
  991. : VK_STENCIL_FACE_FRONT_AND_BACK,
  992. front_test_mask);
  993. if (set_back) {
  994. cmdbuf.SetStencilCompareMask(VK_STENCIL_FACE_BACK_BIT, back_test_mask);
  995. }
  996. });
  997. }();
  998. }
  999. state_tracker.ClearStencilReset();
  1000. }
  1001. void RasterizerVulkan::UpdateLineWidth(Tegra::Engines::Maxwell3D::Regs& regs) {
  1002. if (!state_tracker.TouchLineWidth()) {
  1003. return;
  1004. }
  1005. const float width =
  1006. regs.line_anti_alias_enable ? regs.line_width_smooth : regs.line_width_aliased;
  1007. scheduler.Record([width](vk::CommandBuffer cmdbuf) { cmdbuf.SetLineWidth(width); });
  1008. }
  1009. void RasterizerVulkan::UpdateCullMode(Tegra::Engines::Maxwell3D::Regs& regs) {
  1010. if (!state_tracker.TouchCullMode()) {
  1011. return;
  1012. }
  1013. scheduler.Record([enabled = regs.gl_cull_test_enabled,
  1014. cull_face = regs.gl_cull_face](vk::CommandBuffer cmdbuf) {
  1015. cmdbuf.SetCullModeEXT(enabled ? MaxwellToVK::CullFace(cull_face) : VK_CULL_MODE_NONE);
  1016. });
  1017. }
  1018. void RasterizerVulkan::UpdateDepthBoundsTestEnable(Tegra::Engines::Maxwell3D::Regs& regs) {
  1019. if (!state_tracker.TouchDepthBoundsTestEnable()) {
  1020. return;
  1021. }
  1022. bool enabled = regs.depth_bounds_enable;
  1023. if (enabled && !device.IsDepthBoundsSupported()) {
  1024. LOG_WARNING(Render_Vulkan, "Depth bounds is enabled but not supported");
  1025. enabled = false;
  1026. }
  1027. scheduler.Record([enable = enabled](vk::CommandBuffer cmdbuf) {
  1028. cmdbuf.SetDepthBoundsTestEnableEXT(enable);
  1029. });
  1030. }
  1031. void RasterizerVulkan::UpdateDepthTestEnable(Tegra::Engines::Maxwell3D::Regs& regs) {
  1032. if (!state_tracker.TouchDepthTestEnable()) {
  1033. return;
  1034. }
  1035. scheduler.Record([enable = regs.depth_test_enable](vk::CommandBuffer cmdbuf) {
  1036. cmdbuf.SetDepthTestEnableEXT(enable);
  1037. });
  1038. }
  1039. void RasterizerVulkan::UpdateDepthWriteEnable(Tegra::Engines::Maxwell3D::Regs& regs) {
  1040. if (!state_tracker.TouchDepthWriteEnable()) {
  1041. return;
  1042. }
  1043. scheduler.Record([enable = regs.depth_write_enabled](vk::CommandBuffer cmdbuf) {
  1044. cmdbuf.SetDepthWriteEnableEXT(enable);
  1045. });
  1046. }
  1047. void RasterizerVulkan::UpdatePrimitiveRestartEnable(Tegra::Engines::Maxwell3D::Regs& regs) {
  1048. if (!state_tracker.TouchPrimitiveRestartEnable()) {
  1049. return;
  1050. }
  1051. scheduler.Record([enable = regs.primitive_restart.enabled](vk::CommandBuffer cmdbuf) {
  1052. cmdbuf.SetPrimitiveRestartEnableEXT(enable);
  1053. });
  1054. }
  1055. void RasterizerVulkan::UpdateRasterizerDiscardEnable(Tegra::Engines::Maxwell3D::Regs& regs) {
  1056. if (!state_tracker.TouchRasterizerDiscardEnable()) {
  1057. return;
  1058. }
  1059. scheduler.Record([disable = regs.rasterize_enable](vk::CommandBuffer cmdbuf) {
  1060. cmdbuf.SetRasterizerDiscardEnableEXT(disable == 0);
  1061. });
  1062. }
  1063. void RasterizerVulkan::UpdateDepthBiasEnable(Tegra::Engines::Maxwell3D::Regs& regs) {
  1064. if (!state_tracker.TouchDepthBiasEnable()) {
  1065. return;
  1066. }
  1067. constexpr size_t POINT = 0;
  1068. constexpr size_t LINE = 1;
  1069. constexpr size_t POLYGON = 2;
  1070. static constexpr std::array POLYGON_OFFSET_ENABLE_LUT = {
  1071. POINT, // Points
  1072. LINE, // Lines
  1073. LINE, // LineLoop
  1074. LINE, // LineStrip
  1075. POLYGON, // Triangles
  1076. POLYGON, // TriangleStrip
  1077. POLYGON, // TriangleFan
  1078. POLYGON, // Quads
  1079. POLYGON, // QuadStrip
  1080. POLYGON, // Polygon
  1081. LINE, // LinesAdjacency
  1082. LINE, // LineStripAdjacency
  1083. POLYGON, // TrianglesAdjacency
  1084. POLYGON, // TriangleStripAdjacency
  1085. POLYGON, // Patches
  1086. };
  1087. const std::array enabled_lut{
  1088. regs.polygon_offset_point_enable,
  1089. regs.polygon_offset_line_enable,
  1090. regs.polygon_offset_fill_enable,
  1091. };
  1092. const u32 topology_index = static_cast<u32>(maxwell3d->draw_manager->GetDrawState().topology);
  1093. const u32 enable = enabled_lut[POLYGON_OFFSET_ENABLE_LUT[topology_index]];
  1094. scheduler.Record(
  1095. [enable](vk::CommandBuffer cmdbuf) { cmdbuf.SetDepthBiasEnableEXT(enable != 0); });
  1096. }
  1097. void RasterizerVulkan::UpdateLogicOpEnable(Tegra::Engines::Maxwell3D::Regs& regs) {
  1098. if (!state_tracker.TouchLogicOpEnable()) {
  1099. return;
  1100. }
  1101. scheduler.Record([enable = regs.logic_op.enable](vk::CommandBuffer cmdbuf) {
  1102. cmdbuf.SetLogicOpEnableEXT(enable != 0);
  1103. });
  1104. }
  1105. void RasterizerVulkan::UpdateDepthClampEnable(Tegra::Engines::Maxwell3D::Regs& regs) {
  1106. if (!state_tracker.TouchDepthClampEnable()) {
  1107. return;
  1108. }
  1109. bool is_enabled = !(regs.viewport_clip_control.geometry_clip ==
  1110. Maxwell::ViewportClipControl::GeometryClip::Passthrough ||
  1111. regs.viewport_clip_control.geometry_clip ==
  1112. Maxwell::ViewportClipControl::GeometryClip::FrustumXYZ ||
  1113. regs.viewport_clip_control.geometry_clip ==
  1114. Maxwell::ViewportClipControl::GeometryClip::FrustumZ);
  1115. scheduler.Record(
  1116. [is_enabled](vk::CommandBuffer cmdbuf) { cmdbuf.SetDepthClampEnableEXT(is_enabled); });
  1117. }
  1118. void RasterizerVulkan::UpdateDepthCompareOp(Tegra::Engines::Maxwell3D::Regs& regs) {
  1119. if (!state_tracker.TouchDepthCompareOp()) {
  1120. return;
  1121. }
  1122. scheduler.Record([func = regs.depth_test_func](vk::CommandBuffer cmdbuf) {
  1123. cmdbuf.SetDepthCompareOpEXT(MaxwellToVK::ComparisonOp(func));
  1124. });
  1125. }
  1126. void RasterizerVulkan::UpdateFrontFace(Tegra::Engines::Maxwell3D::Regs& regs) {
  1127. if (!state_tracker.TouchFrontFace()) {
  1128. return;
  1129. }
  1130. VkFrontFace front_face = MaxwellToVK::FrontFace(regs.gl_front_face);
  1131. if (regs.window_origin.flip_y != 0) {
  1132. front_face = front_face == VK_FRONT_FACE_CLOCKWISE ? VK_FRONT_FACE_COUNTER_CLOCKWISE
  1133. : VK_FRONT_FACE_CLOCKWISE;
  1134. }
  1135. scheduler.Record(
  1136. [front_face](vk::CommandBuffer cmdbuf) { cmdbuf.SetFrontFaceEXT(front_face); });
  1137. }
  1138. void RasterizerVulkan::UpdateStencilOp(Tegra::Engines::Maxwell3D::Regs& regs) {
  1139. if (!state_tracker.TouchStencilOp()) {
  1140. return;
  1141. }
  1142. const Maxwell::StencilOp::Op fail = regs.stencil_front_op.fail;
  1143. const Maxwell::StencilOp::Op zfail = regs.stencil_front_op.zfail;
  1144. const Maxwell::StencilOp::Op zpass = regs.stencil_front_op.zpass;
  1145. const Maxwell::ComparisonOp compare = regs.stencil_front_op.func;
  1146. if (regs.stencil_two_side_enable) {
  1147. // Separate stencil op per face
  1148. const Maxwell::StencilOp::Op back_fail = regs.stencil_back_op.fail;
  1149. const Maxwell::StencilOp::Op back_zfail = regs.stencil_back_op.zfail;
  1150. const Maxwell::StencilOp::Op back_zpass = regs.stencil_back_op.zpass;
  1151. const Maxwell::ComparisonOp back_compare = regs.stencil_back_op.func;
  1152. scheduler.Record([fail, zfail, zpass, compare, back_fail, back_zfail, back_zpass,
  1153. back_compare](vk::CommandBuffer cmdbuf) {
  1154. cmdbuf.SetStencilOpEXT(VK_STENCIL_FACE_FRONT_BIT, MaxwellToVK::StencilOp(fail),
  1155. MaxwellToVK::StencilOp(zpass), MaxwellToVK::StencilOp(zfail),
  1156. MaxwellToVK::ComparisonOp(compare));
  1157. cmdbuf.SetStencilOpEXT(VK_STENCIL_FACE_BACK_BIT, MaxwellToVK::StencilOp(back_fail),
  1158. MaxwellToVK::StencilOp(back_zpass),
  1159. MaxwellToVK::StencilOp(back_zfail),
  1160. MaxwellToVK::ComparisonOp(back_compare));
  1161. });
  1162. } else {
  1163. // Front face defines the stencil op of both faces
  1164. scheduler.Record([fail, zfail, zpass, compare](vk::CommandBuffer cmdbuf) {
  1165. cmdbuf.SetStencilOpEXT(VK_STENCIL_FACE_FRONT_AND_BACK, MaxwellToVK::StencilOp(fail),
  1166. MaxwellToVK::StencilOp(zpass), MaxwellToVK::StencilOp(zfail),
  1167. MaxwellToVK::ComparisonOp(compare));
  1168. });
  1169. }
  1170. }
  1171. void RasterizerVulkan::UpdateLogicOp(Tegra::Engines::Maxwell3D::Regs& regs) {
  1172. if (!state_tracker.TouchLogicOp()) {
  1173. return;
  1174. }
  1175. const auto op_value = static_cast<u32>(regs.logic_op.op);
  1176. auto op = op_value >= 0x1500 && op_value < 0x1510 ? static_cast<VkLogicOp>(op_value - 0x1500)
  1177. : VK_LOGIC_OP_NO_OP;
  1178. scheduler.Record([op](vk::CommandBuffer cmdbuf) { cmdbuf.SetLogicOpEXT(op); });
  1179. }
  1180. void RasterizerVulkan::UpdateBlending(Tegra::Engines::Maxwell3D::Regs& regs) {
  1181. if (!state_tracker.TouchBlending()) {
  1182. return;
  1183. }
  1184. if (state_tracker.TouchColorMask()) {
  1185. std::array<VkColorComponentFlags, Maxwell::NumRenderTargets> setup_masks{};
  1186. for (size_t index = 0; index < Maxwell::NumRenderTargets; index++) {
  1187. const auto& mask = regs.color_mask[regs.color_mask_common ? 0 : index];
  1188. auto& current = setup_masks[index];
  1189. if (mask.R) {
  1190. current |= VK_COLOR_COMPONENT_R_BIT;
  1191. }
  1192. if (mask.G) {
  1193. current |= VK_COLOR_COMPONENT_G_BIT;
  1194. }
  1195. if (mask.B) {
  1196. current |= VK_COLOR_COMPONENT_B_BIT;
  1197. }
  1198. if (mask.A) {
  1199. current |= VK_COLOR_COMPONENT_A_BIT;
  1200. }
  1201. }
  1202. scheduler.Record([setup_masks](vk::CommandBuffer cmdbuf) {
  1203. cmdbuf.SetColorWriteMaskEXT(0, setup_masks);
  1204. });
  1205. }
  1206. if (state_tracker.TouchBlendEnable()) {
  1207. std::array<VkBool32, Maxwell::NumRenderTargets> setup_enables{};
  1208. std::ranges::transform(
  1209. regs.blend.enable, setup_enables.begin(),
  1210. [&](const auto& is_enabled) { return is_enabled != 0 ? VK_TRUE : VK_FALSE; });
  1211. scheduler.Record([setup_enables](vk::CommandBuffer cmdbuf) {
  1212. cmdbuf.SetColorBlendEnableEXT(0, setup_enables);
  1213. });
  1214. }
  1215. if (state_tracker.TouchBlendEquations()) {
  1216. std::array<VkColorBlendEquationEXT, Maxwell::NumRenderTargets> setup_blends{};
  1217. for (size_t index = 0; index < Maxwell::NumRenderTargets; index++) {
  1218. const auto blend_setup = [&]<typename T>(const T& guest_blend) {
  1219. auto& host_blend = setup_blends[index];
  1220. host_blend.srcColorBlendFactor = MaxwellToVK::BlendFactor(guest_blend.color_source);
  1221. host_blend.dstColorBlendFactor = MaxwellToVK::BlendFactor(guest_blend.color_dest);
  1222. host_blend.colorBlendOp = MaxwellToVK::BlendEquation(guest_blend.color_op);
  1223. host_blend.srcAlphaBlendFactor = MaxwellToVK::BlendFactor(guest_blend.alpha_source);
  1224. host_blend.dstAlphaBlendFactor = MaxwellToVK::BlendFactor(guest_blend.alpha_dest);
  1225. host_blend.alphaBlendOp = MaxwellToVK::BlendEquation(guest_blend.alpha_op);
  1226. };
  1227. if (!regs.blend_per_target_enabled) {
  1228. blend_setup(regs.blend);
  1229. continue;
  1230. }
  1231. blend_setup(regs.blend_per_target[index]);
  1232. }
  1233. scheduler.Record([setup_blends](vk::CommandBuffer cmdbuf) {
  1234. cmdbuf.SetColorBlendEquationEXT(0, setup_blends);
  1235. });
  1236. }
  1237. }
  1238. void RasterizerVulkan::UpdateStencilTestEnable(Tegra::Engines::Maxwell3D::Regs& regs) {
  1239. if (!state_tracker.TouchStencilTestEnable()) {
  1240. return;
  1241. }
  1242. scheduler.Record([enable = regs.stencil_enable](vk::CommandBuffer cmdbuf) {
  1243. cmdbuf.SetStencilTestEnableEXT(enable);
  1244. });
  1245. }
  1246. void RasterizerVulkan::UpdateVertexInput(Tegra::Engines::Maxwell3D::Regs& regs) {
  1247. auto& dirty{maxwell3d->dirty.flags};
  1248. if (!dirty[Dirty::VertexInput]) {
  1249. return;
  1250. }
  1251. dirty[Dirty::VertexInput] = false;
  1252. boost::container::static_vector<VkVertexInputBindingDescription2EXT, 32> bindings;
  1253. boost::container::static_vector<VkVertexInputAttributeDescription2EXT, 32> attributes;
  1254. // There seems to be a bug on Nvidia's driver where updating only higher attributes ends up
  1255. // generating dirty state. Track the highest dirty attribute and update all attributes until
  1256. // that one.
  1257. size_t highest_dirty_attr{};
  1258. for (size_t index = 0; index < Maxwell::NumVertexAttributes; ++index) {
  1259. if (dirty[Dirty::VertexAttribute0 + index]) {
  1260. highest_dirty_attr = index;
  1261. }
  1262. }
  1263. for (size_t index = 0; index < highest_dirty_attr; ++index) {
  1264. const Maxwell::VertexAttribute attribute{regs.vertex_attrib_format[index]};
  1265. const u32 binding{attribute.buffer};
  1266. dirty[Dirty::VertexAttribute0 + index] = false;
  1267. dirty[Dirty::VertexBinding0 + static_cast<size_t>(binding)] = true;
  1268. if (!attribute.constant) {
  1269. attributes.push_back({
  1270. .sType = VK_STRUCTURE_TYPE_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION_2_EXT,
  1271. .pNext = nullptr,
  1272. .location = static_cast<u32>(index),
  1273. .binding = binding,
  1274. .format = MaxwellToVK::VertexFormat(device, attribute.type, attribute.size),
  1275. .offset = attribute.offset,
  1276. });
  1277. }
  1278. }
  1279. for (size_t index = 0; index < Maxwell::NumVertexAttributes; ++index) {
  1280. if (!dirty[Dirty::VertexBinding0 + index]) {
  1281. continue;
  1282. }
  1283. dirty[Dirty::VertexBinding0 + index] = false;
  1284. const u32 binding{static_cast<u32>(index)};
  1285. const auto& input_binding{regs.vertex_streams[binding]};
  1286. const bool is_instanced{regs.vertex_stream_instances.IsInstancingEnabled(binding)};
  1287. bindings.push_back({
  1288. .sType = VK_STRUCTURE_TYPE_VERTEX_INPUT_BINDING_DESCRIPTION_2_EXT,
  1289. .pNext = nullptr,
  1290. .binding = binding,
  1291. .stride = input_binding.stride,
  1292. .inputRate = is_instanced ? VK_VERTEX_INPUT_RATE_INSTANCE : VK_VERTEX_INPUT_RATE_VERTEX,
  1293. .divisor = is_instanced ? input_binding.frequency : 1,
  1294. });
  1295. }
  1296. scheduler.Record([bindings, attributes](vk::CommandBuffer cmdbuf) {
  1297. cmdbuf.SetVertexInputEXT(bindings, attributes);
  1298. });
  1299. }
  1300. void RasterizerVulkan::InitializeChannel(Tegra::Control::ChannelState& channel) {
  1301. CreateChannel(channel);
  1302. {
  1303. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  1304. texture_cache.CreateChannel(channel);
  1305. buffer_cache.CreateChannel(channel);
  1306. }
  1307. pipeline_cache.CreateChannel(channel);
  1308. query_cache.CreateChannel(channel);
  1309. state_tracker.SetupTables(channel);
  1310. }
  1311. void RasterizerVulkan::BindChannel(Tegra::Control::ChannelState& channel) {
  1312. const s32 channel_id = channel.bind_id;
  1313. BindToChannel(channel_id);
  1314. {
  1315. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  1316. texture_cache.BindToChannel(channel_id);
  1317. buffer_cache.BindToChannel(channel_id);
  1318. }
  1319. pipeline_cache.BindToChannel(channel_id);
  1320. query_cache.BindToChannel(channel_id);
  1321. state_tracker.ChangeChannel(channel);
  1322. state_tracker.InvalidateState();
  1323. }
  1324. void RasterizerVulkan::ReleaseChannel(s32 channel_id) {
  1325. EraseChannel(channel_id);
  1326. {
  1327. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  1328. texture_cache.EraseChannel(channel_id);
  1329. buffer_cache.EraseChannel(channel_id);
  1330. }
  1331. pipeline_cache.EraseChannel(channel_id);
  1332. query_cache.EraseChannel(channel_id);
  1333. }
  1334. } // namespace Vulkan