25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
4.2 KiB

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