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.
 
 

134 líneas
5.0 KiB

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