vk_rasterizer.cpp 59 KB

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