You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

129 regels
4.6 KiB

  1. unit uvkuInstance;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils,
  6. Vulkan;
  7. type
  8. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. TvkuPhysicalDeviceArr = array of VkPhysicalDevice;
  10. TvkuInstance = class(TObject)
  11. private
  12. fHandle: VkInstance;
  13. fAllocCallbacks: PVkAllocationCallbacks;
  14. fPhysicalDevices: TvkuPhysicalDeviceArr;
  15. fInstanceCommands: TVkInstanceCommands;
  16. procedure UpdatePhysicalDevices;
  17. protected
  18. procedure CreateHandle(const aCreateInfo: PVkInstanceCreateInfo; const aAllocCallbacks: PVkAllocationCallbacks);
  19. {%H-}constructor Create;
  20. public
  21. property Handle: VkInstance read fHandle;
  22. property InstanceCommands: TVkInstanceCommands read fInstanceCommands;
  23. function GetPhysicalDevices: TvkuPhysicalDeviceArr;
  24. constructor Create(
  25. const aCreateInfo: TVkInstanceCreateInfo);
  26. constructor Create(
  27. const aCreateInfo: TVkInstanceCreateInfo;
  28. const aAllocCallbacks: TVkAllocationCallbacks);
  29. destructor Destroy; override;
  30. end;
  31. implementation
  32. uses
  33. uvkuUtils;
  34. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  35. //TvkuInstance///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  36. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  37. procedure TvkuInstance.UpdatePhysicalDevices;
  38. var
  39. c: VkUint32;
  40. err: TVkResult;
  41. begin
  42. if (Length(fPhysicalDevices) > 0) then
  43. exit;
  44. err := InstanceCommands.vkEnumeratePhysicalDevices(fHandle, @c, nil);
  45. if (err < VK_SUCCESS) then
  46. raise TvkuErrorException.Create('unable to get physical device number', err);
  47. SetLength(fPhysicalDevices, c);
  48. err := InstanceCommands.vkEnumeratePhysicalDevices(fHandle, @c, @fPhysicalDevices[0]);
  49. if (err < VK_SUCCESS) then
  50. raise TvkuErrorException.Create('unable to get physical devices', err);
  51. end;
  52. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  53. procedure TvkuInstance.CreateHandle(const aCreateInfo: PVkInstanceCreateInfo; const aAllocCallbacks: PVkAllocationCallbacks);
  54. var e: TVkResult;
  55. begin
  56. if Assigned(aAllocCallbacks) then begin
  57. new(fAllocCallbacks);
  58. fAllocCallbacks^ := aAllocCallbacks^;
  59. end;
  60. e := vkCreateInstance(aCreateInfo, fAllocCallbacks, @fHandle);
  61. if (e < VK_SUCCESS) then
  62. raise TvkuErrorException.Create('unable to create vulkan instance', e);
  63. fInstanceCommands := LoadInstanceCommands(fHandle);
  64. if not Assigned(InstanceCommands) then
  65. raise TvkuException.Create('unable to load instance commands');
  66. end;
  67. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  68. constructor TvkuInstance.Create;
  69. begin
  70. inherited Create;
  71. fHandle := nil;
  72. fAllocCallbacks := nil;
  73. end;
  74. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  75. function TvkuInstance.GetPhysicalDevices: TvkuPhysicalDeviceArr;
  76. begin
  77. UpdatePhysicalDevices;
  78. result := fPhysicalDevices;
  79. end;
  80. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  81. constructor TvkuInstance.Create(const aCreateInfo: TVkInstanceCreateInfo);
  82. begin
  83. Create;
  84. CreateHandle(@aCreateInfo, nil);
  85. end;
  86. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  87. constructor TvkuInstance.Create(const aCreateInfo: TVkInstanceCreateInfo; const aAllocCallbacks: TVkAllocationCallbacks);
  88. begin
  89. Create;
  90. CreateHandle(@aCreateInfo, @aAllocCallbacks);
  91. end;
  92. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  93. destructor TvkuInstance.Destroy;
  94. begin
  95. if (fHandle <> VK_INVALID_HANDLE) then begin
  96. InstanceCommands.vkDestroyInstance(fHandle, fAllocCallbacks);
  97. fHandle := nil;
  98. end;
  99. if Assigned(fAllocCallbacks) then begin
  100. Dispose(fAllocCallbacks);
  101. fAllocCallbacks := nil;
  102. end;
  103. FreeAndNil(fInstanceCommands);
  104. inherited Destroy;
  105. end;
  106. end.