|
- unit uvkuDevice;
-
- {$mode objfpc}{$H+}
-
- interface
-
- uses
- Classes, SysUtils,
- Vulkan;
-
- type
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- TvkuDevice = class(TObject)
- private
- fHandle: VkDevice;
- fPhysicalDevice: VkPhysicalDevice;
- fAllocCallbacks: PVkAllocationCallbacks;
- fInstanceCommands: TVkInstanceCommands;
- fDeviceCommands: TVkDeviceCommands;
-
- procedure CreateHandle(
- const aCreateInfo: PVkDeviceCreateInfo;
- const aAllocCallbacks: PVkAllocationCallbacks);
- public
- property Handle: VkDevice read fHandle;
- property PhysicalDevice: VkPhysicalDevice read fPhysicalDevice;
- property DeviceCommands: TVkDeviceCommands read fDeviceCommands;
-
- constructor Create(
- const aPhysicalDevice: VkPhysicalDevice;
- const aCreateInfo: TVkDeviceCreateInfo;
- const aInstanceCommands: TVkInstanceCommands);
- constructor Create(
- const aPhysicalDevice: VkPhysicalDevice;
- const aCreateInfo: TVkDeviceCreateInfo;
- const aInstanceCommands: TVkInstanceCommands;
- const aAllocCallbacks: TVkAllocationCallbacks);
- destructor Destroy; override;
- end;
-
- implementation
-
- uses
- uvkuUtils;
-
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //TvkuDevice/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TvkuDevice.CreateHandle(const aCreateInfo: PVkDeviceCreateInfo; const aAllocCallbacks: PVkAllocationCallbacks);
- var err: TVkResult;
- begin
- if not Assigned(fInstanceCommands.vkCreateDevice) then
- raise TvkuException.Create('invalid function pointer: vkCreateDevice; did you load the vulkan core commands?');
- if Assigned(aAllocCallbacks) then begin
- new(fAllocCallbacks);
- fAllocCallbacks^ := aAllocCallbacks^;
- end;
- err := fInstanceCommands.vkCreateDevice(fPhysicalDevice, aCreateInfo, fAllocCallbacks, @fHandle);
- if (err < VK_SUCCESS) then
- raise TvkuErrorException.Create('unable to create device', err);
- fDeviceCommands := LoadDeviceCommands(fInstanceCommands.Instance, fHandle);
- if not Assigned(fDeviceCommands) then
- raise TvkuException.Create('unable to load device commands');
- end;
-
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- constructor TvkuDevice.Create(const aPhysicalDevice: VkPhysicalDevice; const aCreateInfo: TVkDeviceCreateInfo;
- const aInstanceCommands: TVkInstanceCommands);
- begin
- inherited Create;
- fHandle := nil;
- fPhysicalDevice := aPhysicalDevice;
- fInstanceCommands := aInstanceCommands;
- CreateHandle(@aCreateInfo, nil);
- end;
-
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- constructor TvkuDevice.Create(const aPhysicalDevice: VkPhysicalDevice; const aCreateInfo: TVkDeviceCreateInfo;
- const aInstanceCommands: TVkInstanceCommands; const aAllocCallbacks: TVkAllocationCallbacks);
- begin
- inherited Create;
- fHandle := nil;
- fPhysicalDevice := aPhysicalDevice;
- fInstanceCommands := aInstanceCommands;
- CreateHandle(@aCreateInfo, @aAllocCallbacks);
- end;
-
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- destructor TvkuDevice.Destroy;
- begin
- if (fHandle <> VK_INVALID_HANDLE) then begin
- fDeviceCommands.vkDestroyDevice(fHandle, fAllocCallbacks);
- fHandle := VK_INVALID_HANDLE;
- end;
- if Assigned(fAllocCallbacks) then begin
- Dispose(fAllocCallbacks);
- fAllocCallbacks := nil;
- end;
- FreeAndNil(fDeviceCommands);
- inherited Destroy;
- end;
-
- end.
|