No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

104 líneas
3.9 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. constructor Create(
  23. const aPhysicalDevice: VkPhysicalDevice;
  24. const aCreateInfo: TVkDeviceCreateInfo;
  25. const aInstanceCommands: TVkInstanceCommands);
  26. constructor Create(
  27. const aPhysicalDevice: VkPhysicalDevice;
  28. const aCreateInfo: TVkDeviceCreateInfo;
  29. const aInstanceCommands: TVkInstanceCommands;
  30. const aAllocCallbacks: TVkAllocationCallbacks);
  31. destructor Destroy; override;
  32. end;
  33. implementation
  34. uses
  35. uvkuUtils;
  36. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  37. //TvkuDevice/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  38. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  39. procedure TvkuDevice.CreateHandle(const aCreateInfo: PVkDeviceCreateInfo; const aAllocCallbacks: PVkAllocationCallbacks);
  40. var err: TVkResult;
  41. begin
  42. if not Assigned(fInstanceCommands.vkCreateDevice) then
  43. raise TvkuException.Create('invalid function pointer: vkCreateDevice; did you load the vulkan core commands?');
  44. if Assigned(aAllocCallbacks) then begin
  45. new(fAllocCallbacks);
  46. fAllocCallbacks^ := aAllocCallbacks^;
  47. end;
  48. err := fInstanceCommands.vkCreateDevice(fPhysicalDevice, aCreateInfo, fAllocCallbacks, @fHandle);
  49. if (err < VK_SUCCESS) then
  50. raise TvkuErrorException.Create('unable to create device', err);
  51. fDeviceCommands := LoadDeviceCommands(fInstanceCommands.Instance, fHandle);
  52. if not Assigned(fDeviceCommands) then
  53. raise TvkuException.Create('unable to load device commands');
  54. end;
  55. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  56. constructor TvkuDevice.Create(const aPhysicalDevice: VkPhysicalDevice; const aCreateInfo: TVkDeviceCreateInfo;
  57. const aInstanceCommands: TVkInstanceCommands);
  58. begin
  59. inherited Create;
  60. fHandle := nil;
  61. fPhysicalDevice := aPhysicalDevice;
  62. fInstanceCommands := aInstanceCommands;
  63. CreateHandle(@aCreateInfo, nil);
  64. end;
  65. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  66. constructor TvkuDevice.Create(const aPhysicalDevice: VkPhysicalDevice; const aCreateInfo: TVkDeviceCreateInfo;
  67. const aInstanceCommands: TVkInstanceCommands; const aAllocCallbacks: TVkAllocationCallbacks);
  68. begin
  69. inherited Create;
  70. fHandle := nil;
  71. fPhysicalDevice := aPhysicalDevice;
  72. fInstanceCommands := aInstanceCommands;
  73. CreateHandle(@aCreateInfo, @aAllocCallbacks);
  74. end;
  75. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  76. destructor TvkuDevice.Destroy;
  77. begin
  78. if (fHandle <> VK_INVALID_HANDLE) then begin
  79. fDeviceCommands.vkDestroyDevice(fHandle, fAllocCallbacks);
  80. fHandle := nil;
  81. end;
  82. if Assigned(fAllocCallbacks) then begin
  83. Dispose(fAllocCallbacks);
  84. fAllocCallbacks := nil;
  85. end;
  86. FreeAndNil(fDeviceCommands);
  87. inherited Destroy;
  88. end;
  89. end.