unit uvkuInstance; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Vulkan; type ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TvkuPhysicalDeviceArr = array of VkPhysicalDevice; TvkuInstance = class(TObject) private fHandle: VkInstance; fAllocCallbacks: PVkAllocationCallbacks; fPhysicalDevices: TvkuPhysicalDeviceArr; fInstanceCommands: TVkInstanceCommands; procedure UpdatePhysicalDevices; protected procedure CreateHandle(const aCreateInfo: PVkInstanceCreateInfo; const aAllocCallbacks: PVkAllocationCallbacks); {%H-}constructor Create; public property Handle: VkInstance read fHandle; property InstanceCommands: TVkInstanceCommands read fInstanceCommands; function GetPhysicalDevices: TvkuPhysicalDeviceArr; constructor Create( const aCreateInfo: TVkInstanceCreateInfo); constructor Create( const aCreateInfo: TVkInstanceCreateInfo; const aAllocCallbacks: TVkAllocationCallbacks); destructor Destroy; override; end; implementation uses uvkuUtils; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //TvkuInstance/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TvkuInstance.UpdatePhysicalDevices; var c: VkUint32; err: TVkResult; begin if (Length(fPhysicalDevices) > 0) then exit; err := InstanceCommands.vkEnumeratePhysicalDevices(fHandle, @c, nil); if (err < VK_SUCCESS) then raise TvkuErrorException.Create('unable to get physical device number', err); SetLength(fPhysicalDevices, c); err := InstanceCommands.vkEnumeratePhysicalDevices(fHandle, @c, @fPhysicalDevices[0]); if (err < VK_SUCCESS) then raise TvkuErrorException.Create('unable to get physical devices', err); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TvkuInstance.CreateHandle(const aCreateInfo: PVkInstanceCreateInfo; const aAllocCallbacks: PVkAllocationCallbacks); var e: TVkResult; begin if Assigned(aAllocCallbacks) then begin new(fAllocCallbacks); fAllocCallbacks^ := aAllocCallbacks^; end; e := vkCreateInstance(aCreateInfo, fAllocCallbacks, @fHandle); if (e < VK_SUCCESS) then raise TvkuErrorException.Create('unable to create vulkan instance', e); fInstanceCommands := LoadInstanceCommands(fHandle); if not Assigned(InstanceCommands) then raise TvkuException.Create('unable to load instance commands'); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// constructor TvkuInstance.Create; begin inherited Create; fHandle := nil; fAllocCallbacks := nil; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TvkuInstance.GetPhysicalDevices: TvkuPhysicalDeviceArr; begin UpdatePhysicalDevices; result := fPhysicalDevices; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// constructor TvkuInstance.Create(const aCreateInfo: TVkInstanceCreateInfo); begin Create; CreateHandle(@aCreateInfo, nil); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// constructor TvkuInstance.Create(const aCreateInfo: TVkInstanceCreateInfo; const aAllocCallbacks: TVkAllocationCallbacks); begin Create; CreateHandle(@aCreateInfo, @aAllocCallbacks); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// destructor TvkuInstance.Destroy; begin if (fHandle <> VK_INVALID_HANDLE) then begin InstanceCommands.vkDestroyInstance(fHandle, fAllocCallbacks); fHandle := VK_INVALID_HANDLE; end; if Assigned(fAllocCallbacks) then begin Dispose(fAllocCallbacks); fAllocCallbacks := nil; end; FreeAndNil(fInstanceCommands); inherited Destroy; end; end.