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.
 
 

126 rivejä
4.7 KiB

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