Window.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. using Ryujinx.Graphics.GAL;
  2. using Silk.NET.Vulkan;
  3. using System;
  4. using System.Linq;
  5. using VkFormat = Silk.NET.Vulkan.Format;
  6. namespace Ryujinx.Graphics.Vulkan
  7. {
  8. class Window : WindowBase, IDisposable
  9. {
  10. private const int SurfaceWidth = 1280;
  11. private const int SurfaceHeight = 720;
  12. private readonly VulkanRenderer _gd;
  13. private readonly SurfaceKHR _surface;
  14. private readonly PhysicalDevice _physicalDevice;
  15. private readonly Device _device;
  16. private SwapchainKHR _swapchain;
  17. private Image[] _swapchainImages;
  18. private Auto<DisposableImageView>[] _swapchainImageViews;
  19. private Semaphore _imageAvailableSemaphore;
  20. private Semaphore _renderFinishedSemaphore;
  21. private int _width;
  22. private int _height;
  23. private bool _vsyncEnabled;
  24. private bool _vsyncModeChanged;
  25. private VkFormat _format;
  26. public unsafe Window(VulkanRenderer gd, SurfaceKHR surface, PhysicalDevice physicalDevice, Device device)
  27. {
  28. _gd = gd;
  29. _physicalDevice = physicalDevice;
  30. _device = device;
  31. _surface = surface;
  32. CreateSwapchain();
  33. var semaphoreCreateInfo = new SemaphoreCreateInfo()
  34. {
  35. SType = StructureType.SemaphoreCreateInfo
  36. };
  37. gd.Api.CreateSemaphore(device, semaphoreCreateInfo, null, out _imageAvailableSemaphore).ThrowOnError();
  38. gd.Api.CreateSemaphore(device, semaphoreCreateInfo, null, out _renderFinishedSemaphore).ThrowOnError();
  39. }
  40. private void RecreateSwapchain()
  41. {
  42. _vsyncModeChanged = false;
  43. for (int i = 0; i < _swapchainImageViews.Length; i++)
  44. {
  45. _swapchainImageViews[i].Dispose();
  46. }
  47. CreateSwapchain();
  48. }
  49. private unsafe void CreateSwapchain()
  50. {
  51. _gd.SurfaceApi.GetPhysicalDeviceSurfaceCapabilities(_physicalDevice, _surface, out var capabilities);
  52. uint surfaceFormatsCount;
  53. _gd.SurfaceApi.GetPhysicalDeviceSurfaceFormats(_physicalDevice, _surface, &surfaceFormatsCount, null);
  54. var surfaceFormats = new SurfaceFormatKHR[surfaceFormatsCount];
  55. fixed (SurfaceFormatKHR* pSurfaceFormats = surfaceFormats)
  56. {
  57. _gd.SurfaceApi.GetPhysicalDeviceSurfaceFormats(_physicalDevice, _surface, &surfaceFormatsCount, pSurfaceFormats);
  58. }
  59. uint presentModesCount;
  60. _gd.SurfaceApi.GetPhysicalDeviceSurfacePresentModes(_physicalDevice, _surface, &presentModesCount, null);
  61. var presentModes = new PresentModeKHR[presentModesCount];
  62. fixed (PresentModeKHR* pPresentModes = presentModes)
  63. {
  64. _gd.SurfaceApi.GetPhysicalDeviceSurfacePresentModes(_physicalDevice, _surface, &presentModesCount, pPresentModes);
  65. }
  66. uint imageCount = capabilities.MinImageCount + 1;
  67. if (capabilities.MaxImageCount > 0 && imageCount > capabilities.MaxImageCount)
  68. {
  69. imageCount = capabilities.MaxImageCount;
  70. }
  71. var surfaceFormat = ChooseSwapSurfaceFormat(surfaceFormats);
  72. var extent = ChooseSwapExtent(capabilities);
  73. _width = (int)extent.Width;
  74. _height = (int)extent.Height;
  75. _format = surfaceFormat.Format;
  76. var oldSwapchain = _swapchain;
  77. var swapchainCreateInfo = new SwapchainCreateInfoKHR()
  78. {
  79. SType = StructureType.SwapchainCreateInfoKhr,
  80. Surface = _surface,
  81. MinImageCount = imageCount,
  82. ImageFormat = surfaceFormat.Format,
  83. ImageColorSpace = surfaceFormat.ColorSpace,
  84. ImageExtent = extent,
  85. ImageUsage = ImageUsageFlags.ColorAttachmentBit | ImageUsageFlags.TransferDstBit,
  86. ImageSharingMode = SharingMode.Exclusive,
  87. ImageArrayLayers = 1,
  88. PreTransform = capabilities.CurrentTransform,
  89. CompositeAlpha = ChooseCompositeAlpha(capabilities.SupportedCompositeAlpha),
  90. PresentMode = ChooseSwapPresentMode(presentModes, _vsyncEnabled),
  91. Clipped = true,
  92. OldSwapchain = oldSwapchain
  93. };
  94. _gd.SwapchainApi.CreateSwapchain(_device, swapchainCreateInfo, null, out _swapchain).ThrowOnError();
  95. _gd.SwapchainApi.GetSwapchainImages(_device, _swapchain, &imageCount, null);
  96. _swapchainImages = new Image[imageCount];
  97. fixed (Image* pSwapchainImages = _swapchainImages)
  98. {
  99. _gd.SwapchainApi.GetSwapchainImages(_device, _swapchain, &imageCount, pSwapchainImages);
  100. }
  101. _swapchainImageViews = new Auto<DisposableImageView>[imageCount];
  102. for (int i = 0; i < imageCount; i++)
  103. {
  104. _swapchainImageViews[i] = CreateSwapchainImageView(_swapchainImages[i], surfaceFormat.Format);
  105. }
  106. }
  107. private unsafe Auto<DisposableImageView> CreateSwapchainImageView(Image swapchainImage, VkFormat format)
  108. {
  109. var componentMapping = new ComponentMapping(
  110. ComponentSwizzle.R,
  111. ComponentSwizzle.G,
  112. ComponentSwizzle.B,
  113. ComponentSwizzle.A);
  114. var aspectFlags = ImageAspectFlags.ColorBit;
  115. var subresourceRange = new ImageSubresourceRange(aspectFlags, 0, 1, 0, 1);
  116. var imageCreateInfo = new ImageViewCreateInfo()
  117. {
  118. SType = StructureType.ImageViewCreateInfo,
  119. Image = swapchainImage,
  120. ViewType = ImageViewType.Type2D,
  121. Format = format,
  122. Components = componentMapping,
  123. SubresourceRange = subresourceRange
  124. };
  125. _gd.Api.CreateImageView(_device, imageCreateInfo, null, out var imageView).ThrowOnError();
  126. return new Auto<DisposableImageView>(new DisposableImageView(_gd.Api, _device, imageView));
  127. }
  128. private static SurfaceFormatKHR ChooseSwapSurfaceFormat(SurfaceFormatKHR[] availableFormats)
  129. {
  130. if (availableFormats.Length == 1 && availableFormats[0].Format == VkFormat.Undefined)
  131. {
  132. return new SurfaceFormatKHR(VkFormat.B8G8R8A8Unorm, ColorSpaceKHR.PaceSrgbNonlinearKhr);
  133. }
  134. foreach (var format in availableFormats)
  135. {
  136. if (format.Format == VkFormat.B8G8R8A8Unorm && format.ColorSpace == ColorSpaceKHR.PaceSrgbNonlinearKhr)
  137. {
  138. return format;
  139. }
  140. }
  141. return availableFormats[0];
  142. }
  143. private static CompositeAlphaFlagsKHR ChooseCompositeAlpha(CompositeAlphaFlagsKHR supportedFlags)
  144. {
  145. if (supportedFlags.HasFlag(CompositeAlphaFlagsKHR.OpaqueBitKhr))
  146. {
  147. return CompositeAlphaFlagsKHR.OpaqueBitKhr;
  148. }
  149. else if (supportedFlags.HasFlag(CompositeAlphaFlagsKHR.PreMultipliedBitKhr))
  150. {
  151. return CompositeAlphaFlagsKHR.PreMultipliedBitKhr;
  152. }
  153. else
  154. {
  155. return CompositeAlphaFlagsKHR.InheritBitKhr;
  156. }
  157. }
  158. private static PresentModeKHR ChooseSwapPresentMode(PresentModeKHR[] availablePresentModes, bool vsyncEnabled)
  159. {
  160. if (!vsyncEnabled && availablePresentModes.Contains(PresentModeKHR.ImmediateKhr))
  161. {
  162. return PresentModeKHR.ImmediateKhr;
  163. }
  164. else if (availablePresentModes.Contains(PresentModeKHR.MailboxKhr))
  165. {
  166. return PresentModeKHR.MailboxKhr;
  167. }
  168. else
  169. {
  170. return PresentModeKHR.FifoKhr;
  171. }
  172. }
  173. public static Extent2D ChooseSwapExtent(SurfaceCapabilitiesKHR capabilities)
  174. {
  175. if (capabilities.CurrentExtent.Width != uint.MaxValue)
  176. {
  177. return capabilities.CurrentExtent;
  178. }
  179. else
  180. {
  181. uint width = Math.Max(capabilities.MinImageExtent.Width, Math.Min(capabilities.MaxImageExtent.Width, SurfaceWidth));
  182. uint height = Math.Max(capabilities.MinImageExtent.Height, Math.Min(capabilities.MaxImageExtent.Height, SurfaceHeight));
  183. return new Extent2D(width, height);
  184. }
  185. }
  186. public unsafe override void Present(ITexture texture, ImageCrop crop, Action swapBuffersCallback)
  187. {
  188. uint nextImage = 0;
  189. while (true)
  190. {
  191. var acquireResult = _gd.SwapchainApi.AcquireNextImage(
  192. _device,
  193. _swapchain,
  194. ulong.MaxValue,
  195. _imageAvailableSemaphore,
  196. new Fence(),
  197. ref nextImage);
  198. if (acquireResult == Result.ErrorOutOfDateKhr ||
  199. acquireResult == Result.SuboptimalKhr ||
  200. _vsyncModeChanged)
  201. {
  202. RecreateSwapchain();
  203. }
  204. else
  205. {
  206. acquireResult.ThrowOnError();
  207. break;
  208. }
  209. }
  210. var swapchainImage = _swapchainImages[nextImage];
  211. _gd.FlushAllCommands();
  212. var cbs = _gd.CommandBufferPool.Rent();
  213. Transition(
  214. cbs.CommandBuffer,
  215. swapchainImage,
  216. 0,
  217. AccessFlags.TransferWriteBit,
  218. ImageLayout.Undefined,
  219. ImageLayout.General);
  220. var view = (TextureView)texture;
  221. int srcX0, srcX1, srcY0, srcY1;
  222. float scale = view.ScaleFactor;
  223. if (crop.Left == 0 && crop.Right == 0)
  224. {
  225. srcX0 = 0;
  226. srcX1 = (int)(view.Width / scale);
  227. }
  228. else
  229. {
  230. srcX0 = crop.Left;
  231. srcX1 = crop.Right;
  232. }
  233. if (crop.Top == 0 && crop.Bottom == 0)
  234. {
  235. srcY0 = 0;
  236. srcY1 = (int)(view.Height / scale);
  237. }
  238. else
  239. {
  240. srcY0 = crop.Top;
  241. srcY1 = crop.Bottom;
  242. }
  243. if (scale != 1f)
  244. {
  245. srcX0 = (int)(srcX0 * scale);
  246. srcY0 = (int)(srcY0 * scale);
  247. srcX1 = (int)Math.Ceiling(srcX1 * scale);
  248. srcY1 = (int)Math.Ceiling(srcY1 * scale);
  249. }
  250. if (ScreenCaptureRequested)
  251. {
  252. CaptureFrame(view, srcX0, srcY0, srcX1 - srcX0, srcY1 - srcY0, view.Info.Format.IsBgr(), crop.FlipX, crop.FlipY);
  253. ScreenCaptureRequested = false;
  254. }
  255. float ratioX = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _height * crop.AspectRatioX / (_width * crop.AspectRatioY));
  256. float ratioY = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _width * crop.AspectRatioY / (_height * crop.AspectRatioX));
  257. int dstWidth = (int)(_width * ratioX);
  258. int dstHeight = (int)(_height * ratioY);
  259. int dstPaddingX = (_width - dstWidth) / 2;
  260. int dstPaddingY = (_height - dstHeight) / 2;
  261. int dstX0 = crop.FlipX ? _width - dstPaddingX : dstPaddingX;
  262. int dstX1 = crop.FlipX ? dstPaddingX : _width - dstPaddingX;
  263. int dstY0 = crop.FlipY ? dstPaddingY : _height - dstPaddingY;
  264. int dstY1 = crop.FlipY ? _height - dstPaddingY : dstPaddingY;
  265. _gd.HelperShader.Blit(
  266. _gd,
  267. cbs,
  268. view,
  269. _swapchainImageViews[nextImage],
  270. _width,
  271. _height,
  272. _format,
  273. new Extents2D(srcX0, srcY0, srcX1, srcY1),
  274. new Extents2D(dstX0, dstY1, dstX1, dstY0),
  275. true,
  276. true);
  277. Transition(
  278. cbs.CommandBuffer,
  279. swapchainImage,
  280. 0,
  281. 0,
  282. ImageLayout.General,
  283. ImageLayout.PresentSrcKhr);
  284. _gd.CommandBufferPool.Return(
  285. cbs,
  286. stackalloc[] { _imageAvailableSemaphore },
  287. stackalloc[] { PipelineStageFlags.ColorAttachmentOutputBit },
  288. stackalloc[] { _renderFinishedSemaphore });
  289. // TODO: Present queue.
  290. var semaphore = _renderFinishedSemaphore;
  291. var swapchain = _swapchain;
  292. Result result;
  293. var presentInfo = new PresentInfoKHR()
  294. {
  295. SType = StructureType.PresentInfoKhr,
  296. WaitSemaphoreCount = 1,
  297. PWaitSemaphores = &semaphore,
  298. SwapchainCount = 1,
  299. PSwapchains = &swapchain,
  300. PImageIndices = &nextImage,
  301. PResults = &result
  302. };
  303. lock (_gd.QueueLock)
  304. {
  305. _gd.SwapchainApi.QueuePresent(_gd.Queue, presentInfo);
  306. }
  307. }
  308. private unsafe void Transition(
  309. CommandBuffer commandBuffer,
  310. Image image,
  311. AccessFlags srcAccess,
  312. AccessFlags dstAccess,
  313. ImageLayout srcLayout,
  314. ImageLayout dstLayout)
  315. {
  316. var subresourceRange = new ImageSubresourceRange(ImageAspectFlags.ColorBit, 0, 1, 0, 1);
  317. var barrier = new ImageMemoryBarrier()
  318. {
  319. SType = StructureType.ImageMemoryBarrier,
  320. SrcAccessMask = srcAccess,
  321. DstAccessMask = dstAccess,
  322. OldLayout = srcLayout,
  323. NewLayout = dstLayout,
  324. SrcQueueFamilyIndex = Vk.QueueFamilyIgnored,
  325. DstQueueFamilyIndex = Vk.QueueFamilyIgnored,
  326. Image = image,
  327. SubresourceRange = subresourceRange
  328. };
  329. _gd.Api.CmdPipelineBarrier(
  330. commandBuffer,
  331. PipelineStageFlags.TopOfPipeBit,
  332. PipelineStageFlags.AllCommandsBit,
  333. 0,
  334. 0,
  335. null,
  336. 0,
  337. null,
  338. 1,
  339. barrier);
  340. }
  341. private void CaptureFrame(TextureView texture, int x, int y, int width, int height, bool isBgra, bool flipX, bool flipY)
  342. {
  343. byte[] bitmap = texture.GetData(x, y, width, height);
  344. _gd.OnScreenCaptured(new ScreenCaptureImageInfo(width, height, isBgra, bitmap, flipX, flipY));
  345. }
  346. public override void SetSize(int width, int height)
  347. {
  348. // Not needed as we can get the size from the surface.
  349. }
  350. public override void ChangeVSyncMode(bool vsyncEnabled)
  351. {
  352. _vsyncEnabled = vsyncEnabled;
  353. _vsyncModeChanged = true;
  354. }
  355. protected virtual void Dispose(bool disposing)
  356. {
  357. if (disposing)
  358. {
  359. unsafe
  360. {
  361. _gd.Api.DestroySemaphore(_device, _renderFinishedSemaphore, null);
  362. _gd.Api.DestroySemaphore(_device, _imageAvailableSemaphore, null);
  363. for (int i = 0; i < _swapchainImageViews.Length; i++)
  364. {
  365. _swapchainImageViews[i].Dispose();
  366. }
  367. _gd.SwapchainApi.DestroySwapchain(_device, _swapchain, null);
  368. }
  369. }
  370. }
  371. public override void Dispose()
  372. {
  373. Dispose(true);
  374. }
  375. }
  376. }