unit uvkuSwapChain; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Vulkan, uvkuBase, uvkuTypes; type ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TvkuSwapChain = class(TvkuAllocHandle) private fHandle: VkSwapchainKHR; fDeviceCommands: TVkDeviceCommands; procedure CreateHandle(const aCreateInfo: PVkSwapchainCreateInfoKHR); public property Handle: VkSwapchainKHR read fHandle; property DeviceCommands: TVkDeviceCommands read fDeviceCommands; function GetImages: TVkImageArr; function AcquireNextImage(const aTimeout: VkUint64; const aSemaphore: VkSemaphore; const aFence: VkFence): VkUint32; constructor Create( const aCreateInfo: TVkSwapchainCreateInfoKHR; const aDeviceCommands: TVkDeviceCommands; const aAllocCallbacks: PVkAllocationCallbacks = nil); overload; constructor Create( const aHandle: VkSwapchainKHR; const aOwnsHandle: Boolean; const aDeviceCommands: TVkDeviceCommands; const aAllocCallbacks: PVkAllocationCallbacks = nil); overload; destructor Destroy; override; end; implementation uses uvkuUtils; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //TvkuSwapChain////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TvkuSwapChain.CreateHandle(const aCreateInfo: PVkSwapchainCreateInfoKHR); var err: TVkResult; begin if not Assigned(fDeviceCommands.vkCreateSwapchainKHR) then raise TvkuException.Create('invalid function pointer: vkCreateSwapchainKHR; did you enabled the VK_KHR_swapchain extension?'); err := fDeviceCommands.vkCreateSwapchainKHR(fDeviceCommands.Device, aCreateInfo, AllocCallbacks, @fHandle); if (err < VK_SUCCESS) then raise TvkuErrorException.Create('unable to create swap chain', err); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TvkuSwapChain.GetImages: TVkImageArr; var c: VkUint32; err: TVkResult; begin if not Assigned(fDeviceCommands.vkGetSwapchainImagesKHR) then raise TvkuInvalidFuncPtrException.Create('vkGetSwapchainImagesKHR', 'VK_KHR_swapchain'); err := fDeviceCommands.vkGetSwapchainImagesKHR(fDeviceCommands.Device, fHandle, @c, nil); if (err < VK_SUCCESS) then raise TvkuErrorException.Create('unable to get swap chain image count', err); SetLength(result, c); err := fDeviceCommands.vkGetSwapchainImagesKHR(fDeviceCommands.Device, fHandle, @c, @result[0]); if (err < VK_SUCCESS) then raise TvkuErrorException.Create('unable to get swap chain images', err); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TvkuSwapChain.AcquireNextImage(const aTimeout: VkUint64; const aSemaphore: VkSemaphore; const aFence: VkFence): VkUint32; var err: TVkResult; begin if not Assigned(fDeviceCommands.vkAcquireNextImageKHR) then raise TvkuInvalidFuncPtrException.Create('vkAcquireNextImageKHR', 'VK_KHR_swapchain'); err := fDeviceCommands.vkAcquireNextImageKHR(fDeviceCommands.Device, fHandle, aTimeout, aSemaphore, aFence, @result); if (err < VK_SUCCESS) then raise TvkuErrorException.Create('unable to aquire next image', err); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// constructor TvkuSwapChain.Create(const aCreateInfo: TVkSwapchainCreateInfoKHR; const aDeviceCommands: TVkDeviceCommands; const aAllocCallbacks: PVkAllocationCallbacks); begin inherited Create; fDeviceCommands := aDeviceCommands; SetAllocCallbacks(aAllocCallbacks); CreateHandle(@aCreateInfo); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// constructor TvkuSwapChain.Create(const aHandle: VkSwapchainKHR; const aOwnsHandle: Boolean; const aDeviceCommands: TVkDeviceCommands; const aAllocCallbacks: PVkAllocationCallbacks); begin inherited Create; fHandle := aHandle; fDeviceCommands := aDeviceCommands; OwnsHandle := aOwnsHandle; SetAllocCallbacks(aAllocCallbacks); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// destructor TvkuSwapChain.Destroy; begin if OwnsHandle and (fHandle <> VK_INVALID_NDP_HANDLE) then fDeviceCommands.vkDestroySwapchainKHR(fDeviceCommands.Device, fHandle, AllocCallbacks); fHandle := VK_INVALID_NDP_HANDLE; inherited Destroy; end; end.