vk_rasterizer.cpp 48 KB

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