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.

123 regels
5.7 KiB

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