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.

101 lines
4.1 KiB

  1. unit uvkuDeviceMemory;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils,
  6. Vulkan, uvkuBase;
  7. type
  8. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. TvkuDeviceMemory = class(TvkuAllocHandle)
  10. private
  11. fHandle: VkDeviceMemory;
  12. fDeviceCommands: TVkDeviceCommands;
  13. procedure CreateHandle(const aAllocInfo: TVkMemoryAllocateInfo);
  14. public
  15. property Handle: VkDeviceMemory read fHandle;
  16. property DeviceCommands: TVkDeviceCommands read fDeviceCommands;
  17. function Map(const aOffset: VkDeviceSize; aSize: VkDeviceSize; aFlags: VkMemoryMapFlags): PVkVoid;
  18. procedure Unmap;
  19. constructor Create(const aAllocInfo: TVkMemoryAllocateInfo; const aDeviceCommands: TVkDeviceCommands;
  20. const aAllocCallbacks: PVkAllocationCallbacks);
  21. constructor Create(
  22. const aHandle: VkDeviceMemory;
  23. const aOwnsHandle: Boolean;
  24. const aDeviceCommands: TVkDeviceCommands;
  25. const aAllocCallbacks: PVkAllocationCallbacks = nil);
  26. destructor Destroy; override;
  27. end;
  28. implementation
  29. uses
  30. uvkuUtils;
  31. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  32. //TvkuDeviceMemory///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  33. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  34. procedure TvkuDeviceMemory.CreateHandle(const aAllocInfo: TVkMemoryAllocateInfo);
  35. var err: TVkResult;
  36. begin
  37. if not Assigned(fDeviceCommands.vkAllocateMemory) then
  38. raise TvkuInvalidFuncPtrException.Create('vkAllocateMemory');
  39. err := fDeviceCommands.vkAllocateMemory(fDeviceCommands.Device, @aAllocInfo, AllocCallbacks, @fHandle);
  40. if (err < VK_SUCCESS) then
  41. raise TvkuErrorException.Create('unable to allocate device memory', err);
  42. end;
  43. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  44. function TvkuDeviceMemory.Map(const aOffset: VkDeviceSize; aSize: VkDeviceSize; aFlags: VkMemoryMapFlags): PVkVoid;
  45. var err: TVkResult;
  46. begin
  47. err := fDeviceCommands.vkMapMemory(fDeviceCommands.Device, fHandle, aOffset, aSize, aFlags, @result);
  48. if (err < VK_SUCCESS) then
  49. raise TvkuErrorException.Create('unable to map device memory', err);
  50. end;
  51. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  52. procedure TvkuDeviceMemory.Unmap;
  53. begin
  54. fDeviceCommands.vkUnmapMemory(fDeviceCommands.Device, fHandle);
  55. end;
  56. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. constructor TvkuDeviceMemory.Create(const aAllocInfo: TVkMemoryAllocateInfo; const aDeviceCommands: TVkDeviceCommands;
  58. const aAllocCallbacks: PVkAllocationCallbacks);
  59. begin
  60. inherited Create;
  61. fDeviceCommands := aDeviceCommands;
  62. SetAllocCallbacks(aAllocCallbacks);
  63. CreateHandle(aAllocInfo);
  64. end;
  65. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  66. constructor TvkuDeviceMemory.Create(const aHandle: VkDeviceMemory; const aOwnsHandle: Boolean;
  67. const aDeviceCommands: TVkDeviceCommands; const aAllocCallbacks: PVkAllocationCallbacks);
  68. begin
  69. inherited Create;
  70. fHandle := aHandle;
  71. fDeviceCommands := aDeviceCommands;
  72. OwnsHandle := aOwnsHandle;
  73. SetAllocCallbacks(aAllocCallbacks);
  74. end;
  75. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  76. destructor TvkuDeviceMemory.Destroy;
  77. begin
  78. if OwnsHandle and (fHandle <> VK_INVALID_NDP_HANDLE) then
  79. fDeviceCommands.vkFreeMemory(fDeviceCommands.Device, fHandle, AllocCallbacks);
  80. fHandle := VK_INVALID_NDP_HANDLE;
  81. inherited Destroy;
  82. end;
  83. end.