Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

106 rindas
4.3 KiB

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