Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

142 linhas
5.8 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(
  21. const aQueueFamilyIndex: VkUint32;
  22. const aQueueIndex: VkUint32): VkQueue;
  23. function AllocateMemory(
  24. const aSize: VkUint32;
  25. const aMemoryTypeIndex: VkUint32;
  26. aAllocCallbacks: PVkAllocationCallbacks = nil): VkDeviceMemory;
  27. procedure FreeMemory(
  28. const aMemory: VkDeviceMemory;
  29. aAllocCallbacks: PVkAllocationCallbacks = nil);
  30. constructor Create(
  31. const aPhysicalDevice: VkPhysicalDevice;
  32. const aCreateInfo: TVkDeviceCreateInfo;
  33. const aInstanceCommands: TVkInstanceCommands;
  34. const aAllocCallbacks: PVkAllocationCallbacks = nil);
  35. constructor Create(
  36. const aHandle: VkDevice;
  37. const aOwnsHandle: Boolean;
  38. const aInstanceCommands: TVkInstanceCommands;
  39. const aAllocCallbacks: PVkAllocationCallbacks = nil);
  40. destructor Destroy; override;
  41. end;
  42. implementation
  43. uses
  44. uvkuUtils;
  45. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  46. //TvkuDevice/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  47. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  48. procedure TvkuDevice.CreateHandle(const aPhyDevice: VkPhysicalDevice; const aCreateInfo: PVkDeviceCreateInfo);
  49. var err: TVkResult;
  50. begin
  51. if not Assigned(fInstanceCommands.vkCreateDevice) then
  52. raise TvkuInvalidFuncPtrException.Create('vkCreateDevice');
  53. err := fInstanceCommands.vkCreateDevice(aPhyDevice, aCreateInfo, AllocCallbacks, @fHandle);
  54. if (err < VK_SUCCESS) then
  55. raise TvkuErrorException.Create('unable to create device', err);
  56. fDeviceCommands := LoadDeviceCommands(fInstanceCommands.Instance, fHandle);
  57. if not Assigned(fDeviceCommands) then
  58. raise TvkuException.Create('unable to load device commands');
  59. end;
  60. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  61. function TvkuDevice.GetQueue(const aQueueFamilyIndex: VkUint32; const aQueueIndex: VkUint32): VkQueue;
  62. begin
  63. if not Assigned(fDeviceCommands.vkGetDeviceQueue) then
  64. raise TvkuInvalidFuncPtrException.Create('vkGetDeviceQueue');
  65. fDeviceCommands.vkGetDeviceQueue(fHandle, aQueueFamilyIndex, aQueueIndex, @result);
  66. if (result = VK_INVALID_HANDLE) then
  67. raise TvkuException.Create('unable to get device queue');
  68. end;
  69. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  70. function TvkuDevice.AllocateMemory(const aSize: VkUint32; const aMemoryTypeIndex: VkUint32;
  71. aAllocCallbacks: PVkAllocationCallbacks): VkDeviceMemory;
  72. var
  73. AllocInfo: TVkMemoryAllocateInfo;
  74. err: TVkResult;
  75. begin
  76. FillByte(AllocInfo, SizeOf(AllocInfo), 0);
  77. AllocInfo.sType := VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  78. AllocInfo.pNext := nil;
  79. AllocInfo.allocationSize := aSize;
  80. AllocInfo.memoryTypeIndex := aMemoryTypeIndex;
  81. if not Assigned(aAllocCallbacks) then
  82. aAllocCallbacks := AllocCallbacks;
  83. err := fDeviceCommands.vkAllocateMemory(fHandle, @AllocInfo, aAllocCallbacks, @result);
  84. if (err < VK_SUCCESS) then
  85. raise TvkuErrorException.Create('unable to allocate device memory', err);
  86. end;
  87. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  88. procedure TvkuDevice.FreeMemory(const aMemory: VkDeviceMemory; aAllocCallbacks: PVkAllocationCallbacks);
  89. begin
  90. if not Assigned(aAllocCallbacks) then
  91. aAllocCallbacks := AllocCallbacks;
  92. fDeviceCommands.vkFreeMemory(fHandle, aMemory, aAllocCallbacks);
  93. end;
  94. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  95. constructor TvkuDevice.Create(const aPhysicalDevice: VkPhysicalDevice; const aCreateInfo: TVkDeviceCreateInfo;
  96. const aInstanceCommands: TVkInstanceCommands; const aAllocCallbacks: PVkAllocationCallbacks);
  97. begin
  98. inherited Create;
  99. fHandle := nil;
  100. fInstanceCommands := aInstanceCommands;
  101. SetAllocCallbacks(aAllocCallbacks);
  102. CreateHandle(aPhysicalDevice, @aCreateInfo);
  103. end;
  104. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  105. constructor TvkuDevice.Create(const aHandle: VkDevice; const aOwnsHandle: Boolean;
  106. const aInstanceCommands: TVkInstanceCommands; const aAllocCallbacks: PVkAllocationCallbacks);
  107. begin
  108. inherited Create;
  109. fHandle := aHandle;
  110. fInstanceCommands := aInstanceCommands;
  111. OwnsHandle := aOwnsHandle;
  112. SetAllocCallbacks(aAllocCallbacks);
  113. end;
  114. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  115. destructor TvkuDevice.Destroy;
  116. begin
  117. if OwnsHandle and (fHandle <> VK_INVALID_HANDLE) then
  118. fDeviceCommands.vkDestroyDevice(fHandle, AllocCallbacks);
  119. fHandle := VK_INVALID_HANDLE;
  120. FreeAndNil(fDeviceCommands);
  121. inherited Destroy;
  122. end;
  123. end.