25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

105 satır
4.0 KiB

  1. unit uvkuDevice;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils,
  6. Vulkan;
  7. type
  8. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. TvkuDevice = class(TObject)
  10. private
  11. fHandle: VkDevice;
  12. fPhysicalDevice: VkPhysicalDevice;
  13. fAllocCallbacks: PVkAllocationCallbacks;
  14. fInstanceCommands: TVkInstanceCommands;
  15. fDeviceCommands: TVkDeviceCommands;
  16. procedure CreateHandle(
  17. const aCreateInfo: PVkDeviceCreateInfo;
  18. const aAllocCallbacks: PVkAllocationCallbacks);
  19. public
  20. property Handle: VkDevice read fHandle;
  21. property PhysicalDevice: VkPhysicalDevice read fPhysicalDevice;
  22. property DeviceCommands: TVkDeviceCommands read fDeviceCommands;
  23. constructor Create(
  24. const aPhysicalDevice: VkPhysicalDevice;
  25. const aCreateInfo: TVkDeviceCreateInfo;
  26. const aInstanceCommands: TVkInstanceCommands);
  27. constructor Create(
  28. const aPhysicalDevice: VkPhysicalDevice;
  29. const aCreateInfo: TVkDeviceCreateInfo;
  30. const aInstanceCommands: TVkInstanceCommands;
  31. const aAllocCallbacks: TVkAllocationCallbacks);
  32. destructor Destroy; override;
  33. end;
  34. implementation
  35. uses
  36. uvkuUtils;
  37. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  38. //TvkuDevice/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  39. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  40. procedure TvkuDevice.CreateHandle(const aCreateInfo: PVkDeviceCreateInfo; const aAllocCallbacks: PVkAllocationCallbacks);
  41. var err: TVkResult;
  42. begin
  43. if not Assigned(fInstanceCommands.vkCreateDevice) then
  44. raise TvkuException.Create('invalid function pointer: vkCreateDevice; did you load the vulkan core commands?');
  45. if Assigned(aAllocCallbacks) then begin
  46. new(fAllocCallbacks);
  47. fAllocCallbacks^ := aAllocCallbacks^;
  48. end;
  49. err := fInstanceCommands.vkCreateDevice(fPhysicalDevice, aCreateInfo, fAllocCallbacks, @fHandle);
  50. if (err < VK_SUCCESS) then
  51. raise TvkuErrorException.Create('unable to create device', err);
  52. fDeviceCommands := LoadDeviceCommands(fInstanceCommands.Instance, fHandle);
  53. if not Assigned(fDeviceCommands) then
  54. raise TvkuException.Create('unable to load device commands');
  55. end;
  56. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. constructor TvkuDevice.Create(const aPhysicalDevice: VkPhysicalDevice; const aCreateInfo: TVkDeviceCreateInfo;
  58. const aInstanceCommands: TVkInstanceCommands);
  59. begin
  60. inherited Create;
  61. fHandle := nil;
  62. fPhysicalDevice := aPhysicalDevice;
  63. fInstanceCommands := aInstanceCommands;
  64. CreateHandle(@aCreateInfo, nil);
  65. end;
  66. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  67. constructor TvkuDevice.Create(const aPhysicalDevice: VkPhysicalDevice; const aCreateInfo: TVkDeviceCreateInfo;
  68. const aInstanceCommands: TVkInstanceCommands; const aAllocCallbacks: TVkAllocationCallbacks);
  69. begin
  70. inherited Create;
  71. fHandle := nil;
  72. fPhysicalDevice := aPhysicalDevice;
  73. fInstanceCommands := aInstanceCommands;
  74. CreateHandle(@aCreateInfo, @aAllocCallbacks);
  75. end;
  76. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  77. destructor TvkuDevice.Destroy;
  78. begin
  79. if (fHandle <> VK_INVALID_HANDLE) then begin
  80. fDeviceCommands.vkDestroyDevice(fHandle, fAllocCallbacks);
  81. fHandle := VK_INVALID_HANDLE;
  82. end;
  83. if Assigned(fAllocCallbacks) then begin
  84. Dispose(fAllocCallbacks);
  85. fAllocCallbacks := nil;
  86. end;
  87. FreeAndNil(fDeviceCommands);
  88. inherited Destroy;
  89. end;
  90. end.