You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

116 lines
4.6 KiB

  1. unit uMainForm;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
  6. Vulkan, uvkuInstance, uvkuInstanceFactory, uvkuPhysicalDevice, uvkuUtils, uvkuAllocationHandler, uvkuDevice, uvkuDeviceFactory;
  7. type
  8. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. TCustomAllocHandler = class(TvkuAllocationHandler)
  10. protected
  11. function AllocateMemory(const aSize: VkSize; const aAlignment: VkSize; const aScope: TVkSystemAllocationScope): PVkVoid; override;
  12. function ReallocateMemory(const aOriginal: PVkVoid; const aSize: VkSize; const aAlignment: VkSize; const aScope: TVkSystemAllocationScope): PVkVoid; override;
  13. procedure FreeMemory(const aMemory: PVkVoid); override;
  14. end;
  15. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. TMainForm = class(TForm)
  17. procedure FormCreate(Sender: TObject);
  18. procedure FormDestroy(Sender: TObject);
  19. private
  20. fAllocHandler: TCustomAllocHandler;
  21. fInstance: TvkuInstance;
  22. fDevice: TvkuDevice;
  23. public
  24. { public declarations }
  25. end;
  26. var
  27. MainForm: TMainForm;
  28. implementation
  29. {$R *.lfm}
  30. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  31. //TCustomAllocHandler////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  32. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  33. function TCustomAllocHandler.AllocateMemory(const aSize: VkSize; const aAlignment: VkSize; const aScope: TVkSystemAllocationScope): PVkVoid;
  34. begin
  35. result := inherited AllocateMemory(aSize, aAlignment, aScope);
  36. WriteLn(Format('%d bytes of memory allocated at %p', [ aSize, result ]));
  37. end;
  38. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  39. function TCustomAllocHandler.ReallocateMemory(const aOriginal: PVkVoid; const aSize: VkSize; const aAlignment: VkSize; const aScope: TVkSystemAllocationScope): PVkVoid;
  40. var p: PVkVoid;
  41. begin
  42. p := aOriginal;
  43. result := inherited ReallocateMemory(aOriginal, aSize, aAlignment, aScope);
  44. WriteLn(Format('%d bytes of memory (from %p) reallocated at %p', [ aSize, p, result ]));
  45. end;
  46. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  47. procedure TCustomAllocHandler.FreeMemory(const aMemory: PVkVoid);
  48. begin
  49. WriteLn(Format('free memory at %p', [ aMemory ]));
  50. inherited FreeMemory(aMemory);
  51. end;
  52. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  53. //TMainForm//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  54. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  55. procedure TMainForm.FormCreate(Sender: TObject);
  56. var
  57. PhyDevices: TvkuPhysicalDeviceArr;
  58. QueueFamProps: TVkQueueFamilyPropertiesArr;
  59. iFactory: TvkuInstanceFactory;
  60. dFactory: TvkuDeviceFactory;
  61. qci: TvkuQueueCreateInfo;
  62. begin
  63. fAllocHandler := TCustomAllocHandler.Create;
  64. WriteLn('create vulkan instance');
  65. iFactory := TvkuInstanceFactory.Create;
  66. try
  67. fInstance := iFactory.CreateInstance(fAllocHandler, false);
  68. finally
  69. FreeAndNil(iFactory);
  70. end;
  71. PhyDevices := fInstance.GetPhysicalDevices;
  72. if (Length(PhyDevices) <= 0) then
  73. raise Exception.Create('unable to get physical device');
  74. WriteLn('create vulkan device');
  75. dFactory := TvkuDeviceFactory.Create;
  76. try
  77. SetLength(qci.Priorities, 1);
  78. qci.Priorities[0] := 1.0;
  79. qci.FamilyIndex := 0;
  80. qci.Flags := 0;
  81. dFactory.QueueCreateInfoCount := 1;
  82. dFactory.QueueCreateInfo[0] := qci;
  83. fDevice := dFactory.CreateDevice(PhyDevices[0], fAllocHandler, false);
  84. finally
  85. FreeAndNil(dFactory);
  86. end;
  87. end;
  88. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  89. procedure TMainForm.FormDestroy(Sender: TObject);
  90. begin
  91. FreeAndNil(fDevice);
  92. FreeAndNil(fInstance);
  93. FreeAndNil(fAllocHandler);
  94. end;
  95. end.