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.
 
 

104 lines
5.4 KiB

  1. unit uvkuAllocationHandler;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils,
  6. Vulkan;
  7. type
  8. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. TvkuAllocationHandler = class(TObject)
  10. protected
  11. function AllocateMemory(const aSize: VkSize; const aAlignment: VkSize; const aScope: TVkSystemAllocationScope): PVkVoid; virtual;
  12. function ReallocateMemory(const aOriginal: PVkVoid; const aSize: VkSize; const aAlignment: VkSize; const aScope: TVkSystemAllocationScope): PVkVoid; virtual;
  13. procedure FreeMemory(const aMemory: PVkVoid); virtual;
  14. procedure InternalAllocationNotification(const aSize: VkSize; const aType: TVkInternalAllocationType; const aScope: TVkSystemAllocationScope);
  15. procedure InternalFreeNotification(const aSize: VkSize; const aType: TVkInternalAllocationType; const aScope: TVkSystemAllocationScope);
  16. public
  17. function GetAllocationCallbacks: TVkAllocationCallbacks;
  18. end;
  19. implementation
  20. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  21. function AllocateMemoryCallback(aUserData: PVkVoid; aSize: VkSize; aAlignment: VkSize; aScope: TVkSystemAllocationScope): PVkVoid; stdcall;
  22. begin
  23. result := TvkuAllocationHandler(aUserData).AllocateMemory(aSize, aAlignment, aScope);
  24. end;
  25. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  26. function ReallocateMemoryCallback(aUserData: PVkVoid; aOriginal: PVkVoid; aSize: VkSize;
  27. aAlignment: VkSize; aScope: TVkSystemAllocationScope): PVkVoid; stdcall;
  28. begin
  29. result := TvkuAllocationHandler(aUserData).ReallocateMemory(aOriginal, aSize, aAlignment, aScope);
  30. end;
  31. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  32. procedure FreeMemoryCallback(aUserData: PVkVoid; aMemory: PVkVoid); stdcall;
  33. begin
  34. TvkuAllocationHandler(aUserData).FreeMemory(aMemory);
  35. end;
  36. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  37. procedure InternalAllocationCallback(aUserData: PVkVoid; aSize: VkSize; aType: TVkInternalAllocationType; aScope: TVkSystemAllocationScope); stdcall;
  38. begin
  39. TvkuAllocationHandler(aUserData).InternalAllocationNotification(aSize, aType, aScope);
  40. end;
  41. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  42. procedure InternalFreeCallback(aUserData: PVkVoid; aSize: VkSize; aType: TVkInternalAllocationType; aScope: TVkSystemAllocationScope); stdcall;
  43. begin
  44. TvkuAllocationHandler(aUserData).InternalFreeNotification(aSize, aType, aScope);
  45. end;
  46. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  47. //TvkuAllocationHandler//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  48. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  49. function TvkuAllocationHandler.AllocateMemory(const aSize: VkSize; const aAlignment: VkSize; const aScope: TVkSystemAllocationScope): PVkVoid;
  50. begin
  51. result := System.GetMemory(aSize);
  52. end;
  53. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  54. function TvkuAllocationHandler.ReallocateMemory(const aOriginal: PVkVoid; const aSize: VkSize; const aAlignment: VkSize; const aScope: TVkSystemAllocationScope): PVkVoid;
  55. begin
  56. result := System.ReAllocMemory(aOriginal, aSize);
  57. end;
  58. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  59. procedure TvkuAllocationHandler.FreeMemory(const aMemory: PVkVoid);
  60. begin
  61. System.FreeMemory(aMemory);
  62. end;
  63. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  64. procedure TvkuAllocationHandler.InternalAllocationNotification(const aSize: VkSize; const aType: TVkInternalAllocationType; const aScope: TVkSystemAllocationScope);
  65. begin
  66. // DUMMY
  67. end;
  68. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  69. procedure TvkuAllocationHandler.InternalFreeNotification(const aSize: VkSize; const aType: TVkInternalAllocationType; const aScope: TVkSystemAllocationScope);
  70. begin
  71. // DUMMY
  72. end;
  73. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  74. function TvkuAllocationHandler.GetAllocationCallbacks: TVkAllocationCallbacks;
  75. begin
  76. FillByte(result, SizeOf(result), 0);
  77. result.pUserData := self;
  78. result.pfnAllocation := @AllocateMemoryCallback;
  79. result.pfnReallocation := @ReallocateMemoryCallback;
  80. result.pfnFree := @FreeMemoryCallback;
  81. result.pfnInternalAllocation := @InternalAllocationCallback;
  82. result.pfnInternalFree := @InternalFreeCallback;
  83. end;
  84. end.