Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

131 Zeilen
5.4 KiB

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