Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

137 linhas
6.6 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. private
  11. fAllocCallbacks: PVkAllocationCallbacks;
  12. protected
  13. function AllocateMemory(const aSize: VkSize; const aAlignment: VkSize; const aScope: TVkSystemAllocationScope): PVkVoid; virtual;
  14. function ReallocateMemory(const aOriginal: PVkVoid; const aSize: VkSize; const aAlignment: VkSize; const aScope: TVkSystemAllocationScope): PVkVoid; virtual;
  15. procedure FreeMemory(const aMemory: PVkVoid); virtual;
  16. procedure InternalAllocationNotification(const aSize: VkSize; const aType: TVkInternalAllocationType; const aScope: TVkSystemAllocationScope);
  17. procedure InternalFreeNotification(const aSize: VkSize; const aType: TVkInternalAllocationType; const aScope: TVkSystemAllocationScope);
  18. public
  19. function GetAllocationCallbacks: TVkAllocationCallbacks;
  20. function GetAllocationCallbacksPtr: PVkAllocationCallbacks;
  21. constructor Create;
  22. destructor Destroy; override;
  23. end;
  24. implementation
  25. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  26. function AllocateMemoryCallback(aUserData: PVkVoid; aSize: VkSize; aAlignment: VkSize; aScope: TVkSystemAllocationScope): PVkVoid; stdcall;
  27. begin
  28. result := TvkuAllocationHandler(aUserData).AllocateMemory(aSize, aAlignment, aScope);
  29. end;
  30. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  31. function ReallocateMemoryCallback(aUserData: PVkVoid; aOriginal: PVkVoid; aSize: VkSize;
  32. aAlignment: VkSize; aScope: TVkSystemAllocationScope): PVkVoid; stdcall;
  33. begin
  34. result := TvkuAllocationHandler(aUserData).ReallocateMemory(aOriginal, aSize, aAlignment, aScope);
  35. end;
  36. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  37. procedure FreeMemoryCallback(aUserData: PVkVoid; aMemory: PVkVoid); stdcall;
  38. begin
  39. TvkuAllocationHandler(aUserData).FreeMemory(aMemory);
  40. end;
  41. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  42. procedure InternalAllocationCallback(aUserData: PVkVoid; aSize: VkSize; aType: TVkInternalAllocationType; aScope: TVkSystemAllocationScope); stdcall;
  43. begin
  44. TvkuAllocationHandler(aUserData).InternalAllocationNotification(aSize, aType, aScope);
  45. end;
  46. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  47. procedure InternalFreeCallback(aUserData: PVkVoid; aSize: VkSize; aType: TVkInternalAllocationType; aScope: TVkSystemAllocationScope); stdcall;
  48. begin
  49. TvkuAllocationHandler(aUserData).InternalFreeNotification(aSize, aType, aScope);
  50. end;
  51. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  52. //TvkuAllocationHandler//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  53. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  54. function TvkuAllocationHandler.AllocateMemory(const aSize: VkSize; const aAlignment: VkSize; const aScope: TVkSystemAllocationScope): PVkVoid;
  55. begin
  56. result := System.GetMemory(aSize);
  57. end;
  58. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  59. function TvkuAllocationHandler.ReallocateMemory(const aOriginal: PVkVoid; const aSize: VkSize; const aAlignment: VkSize; const aScope: TVkSystemAllocationScope): PVkVoid;
  60. begin
  61. result := System.ReAllocMemory(aOriginal, aSize);
  62. end;
  63. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  64. procedure TvkuAllocationHandler.FreeMemory(const aMemory: PVkVoid);
  65. begin
  66. System.FreeMemory(aMemory);
  67. end;
  68. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  69. procedure TvkuAllocationHandler.InternalAllocationNotification(const aSize: VkSize; const aType: TVkInternalAllocationType; const aScope: TVkSystemAllocationScope);
  70. begin
  71. // DUMMY
  72. end;
  73. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  74. procedure TvkuAllocationHandler.InternalFreeNotification(const aSize: VkSize; const aType: TVkInternalAllocationType; const aScope: TVkSystemAllocationScope);
  75. begin
  76. // DUMMY
  77. end;
  78. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  79. function TvkuAllocationHandler.GetAllocationCallbacks: TVkAllocationCallbacks;
  80. begin
  81. result := GetAllocationCallbacksPtr^;
  82. end;
  83. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  84. function TvkuAllocationHandler.GetAllocationCallbacksPtr: PVkAllocationCallbacks;
  85. begin
  86. if not Assigned(fAllocCallbacks) then begin
  87. new(fAllocCallbacks);
  88. FillByte(fAllocCallbacks^, SizeOf(result), 0);
  89. fAllocCallbacks^.pUserData := self;
  90. fAllocCallbacks^.pfnAllocation := @AllocateMemoryCallback;
  91. fAllocCallbacks^.pfnReallocation := @ReallocateMemoryCallback;
  92. fAllocCallbacks^.pfnFree := @FreeMemoryCallback;
  93. fAllocCallbacks^.pfnInternalAllocation := @InternalAllocationCallback;
  94. fAllocCallbacks^.pfnInternalFree := @InternalFreeCallback;
  95. end;
  96. result := fAllocCallbacks;
  97. end;
  98. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  99. constructor TvkuAllocationHandler.Create;
  100. begin
  101. inherited Create;
  102. fAllocCallbacks := nil;
  103. end;
  104. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  105. destructor TvkuAllocationHandler.Destroy;
  106. begin
  107. if Assigned(fAllocCallbacks) then begin
  108. Dispose(fAllocCallbacks);
  109. fAllocCallbacks := nil;
  110. end;
  111. inherited Destroy;
  112. end;
  113. end.