Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

117 lignes
5.0 KiB

  1. unit uvkuSwapChain;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils,
  6. Vulkan, uvkuBase, uvkuTypes;
  7. type
  8. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. TvkuSwapChain = class(TvkuAllocHandle)
  10. private
  11. fHandle: VkSwapchainKHR;
  12. fDeviceCommands: TVkDeviceCommands;
  13. procedure CreateHandle(const aCreateInfo: PVkSwapchainCreateInfoKHR);
  14. public
  15. property Handle: VkSwapchainKHR read fHandle;
  16. property DeviceCommands: TVkDeviceCommands read fDeviceCommands;
  17. function GetImages: TvkuImageArr;
  18. function AcquireNextImage(const aTimeout: VkUint64; const aSemaphore: VkSemaphore; const aFence: VkFence): VkUint32;
  19. constructor Create(
  20. const aCreateInfo: TVkSwapchainCreateInfoKHR;
  21. const aDeviceCommands: TVkDeviceCommands;
  22. const aAllocCallbacks: PVkAllocationCallbacks = nil); overload;
  23. constructor Create(
  24. const aHandle: VkSwapchainKHR;
  25. const aOwnsHandle: Boolean;
  26. const aDeviceCommands: TVkDeviceCommands;
  27. const aAllocCallbacks: PVkAllocationCallbacks = nil); overload;
  28. destructor Destroy; override;
  29. end;
  30. implementation
  31. uses
  32. uvkuUtils;
  33. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  34. //TvkuSwapChain//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  35. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  36. procedure TvkuSwapChain.CreateHandle(const aCreateInfo: PVkSwapchainCreateInfoKHR);
  37. var err: TVkResult;
  38. begin
  39. if not Assigned(fDeviceCommands.vkCreateSwapchainKHR) then
  40. raise TvkuException.Create('invalid function pointer: vkCreateSwapchainKHR; did you enabled the VK_KHR_swapchain extension?');
  41. err := fDeviceCommands.vkCreateSwapchainKHR(fDeviceCommands.Device, aCreateInfo, AllocCallbacks, @fHandle);
  42. if (err < VK_SUCCESS) then
  43. raise TvkuErrorException.Create('unable to create swap chain', err);
  44. end;
  45. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  46. function TvkuSwapChain.GetImages: TvkuImageArr;
  47. var
  48. c: VkUint32;
  49. err: TVkResult;
  50. begin
  51. if not Assigned(fDeviceCommands.vkGetSwapchainImagesKHR) then
  52. raise TvkuInvalidFuncPtrException.Create('vkGetSwapchainImagesKHR', 'VK_KHR_swapchain');
  53. err := fDeviceCommands.vkGetSwapchainImagesKHR(fDeviceCommands.Device, fHandle, @c, nil);
  54. if (err < VK_SUCCESS) then
  55. raise TvkuErrorException.Create('unable to get swap chain image count', err);
  56. SetLength(result, c);
  57. err := fDeviceCommands.vkGetSwapchainImagesKHR(fDeviceCommands.Device, fHandle, @c, @result[0]);
  58. if (err < VK_SUCCESS) then
  59. raise TvkuErrorException.Create('unable to get swap chain images', err);
  60. end;
  61. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  62. function TvkuSwapChain.AcquireNextImage(const aTimeout: VkUint64; const aSemaphore: VkSemaphore; const aFence: VkFence): VkUint32;
  63. var
  64. err: TVkResult;
  65. begin
  66. if not Assigned(fDeviceCommands.vkAcquireNextImageKHR) then
  67. raise TvkuInvalidFuncPtrException.Create('vkAcquireNextImageKHR', 'VK_KHR_swapchain');
  68. err := fDeviceCommands.vkAcquireNextImageKHR(fDeviceCommands.Device, fHandle, aTimeout, aSemaphore, aFence, @result);
  69. if (err < VK_SUCCESS) then
  70. raise TvkuErrorException.Create('unable to aquire next image', err);
  71. end;
  72. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  73. constructor TvkuSwapChain.Create(const aCreateInfo: TVkSwapchainCreateInfoKHR; const aDeviceCommands: TVkDeviceCommands;
  74. const aAllocCallbacks: PVkAllocationCallbacks);
  75. begin
  76. inherited Create;
  77. fDeviceCommands := aDeviceCommands;
  78. SetAllocCallbacks(aAllocCallbacks);
  79. CreateHandle(@aCreateInfo);
  80. end;
  81. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  82. constructor TvkuSwapChain.Create(const aHandle: VkSwapchainKHR; const aOwnsHandle: Boolean;
  83. const aDeviceCommands: TVkDeviceCommands; const aAllocCallbacks: PVkAllocationCallbacks);
  84. begin
  85. inherited Create;
  86. fHandle := aHandle;
  87. fDeviceCommands := aDeviceCommands;
  88. OwnsHandle := aOwnsHandle;
  89. SetAllocCallbacks(aAllocCallbacks);
  90. end;
  91. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  92. destructor TvkuSwapChain.Destroy;
  93. begin
  94. if OwnsHandle and (fHandle <> VK_INVALID_NDP_HANDLE) then
  95. fDeviceCommands.vkDestroySwapchainKHR(fDeviceCommands.Device, fHandle, AllocCallbacks);
  96. fHandle := VK_INVALID_NDP_HANDLE;
  97. inherited Destroy;
  98. end;
  99. end.