vk_rasterizer.cpp 41 KB

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