選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

144 行
5.6 KiB

  1. unit uvkuBuffer;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils,
  6. Vulkan, uvkuBase, uvkuDeviceMemory;
  7. type
  8. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. TvkuBuffer = class(TvkuAllocHandle)
  10. private
  11. fHandle: VkBuffer;
  12. fDeviceCommands: TVkDeviceCommands;
  13. fMemory: VkDeviceMemory;
  14. fBufferMemory: TvkuDeviceMemory;
  15. procedure CreateHandle(const aCreateInfo: TVkBufferCreateInfo);
  16. public
  17. property Handle: VkBuffer read fHandle;
  18. property DeviceCommands: TVkDeviceCommands read fDeviceCommands;
  19. property Memory: VkDeviceMemory read fMemory;
  20. procedure BindMemory(
  21. const aMemory: VkDeviceMemory;
  22. const aOffset: VkDeviceSize);
  23. function GetMemoryRequirements: TVkMemoryRequirements;
  24. procedure AllocateAndBindBufferMemory(
  25. const aMemoryProperties: TVkPhysicalDeviceMemoryProperties;
  26. aAllocCallbacks: PVkAllocationCallbacks = nil);
  27. constructor Create(
  28. const aCreateInfo: TVkBufferCreateInfo;
  29. const aDeviceCommands: TVkDeviceCommands;
  30. const aAllocCallbacks: PVkAllocationCallbacks = nil);
  31. constructor Create(
  32. const aHandle: VkBuffer;
  33. const aOwnsHandle: Boolean;
  34. const aDeviceCommands: TVkDeviceCommands;
  35. const aAllocCallbacks: PVkAllocationCallbacks = nil);
  36. destructor Destroy; override;
  37. end;
  38. implementation
  39. uses
  40. uvkuUtils;
  41. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  42. //TvkuBuffer/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  43. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  44. procedure TvkuBuffer.CreateHandle(const aCreateInfo: TVkBufferCreateInfo);
  45. var err: TVkResult;
  46. begin
  47. err := fDeviceCommands.vkCreateBuffer(fDeviceCommands.Device, @aCreateInfo, AllocCallbacks, @fHandle);
  48. if (err <> VK_SUCCESS) then
  49. raise TvkuErrorException.Create('unable to create buffer', err);
  50. end;
  51. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  52. procedure TvkuBuffer.BindMemory(const aMemory: VkDeviceMemory; const aOffset: VkDeviceSize);
  53. var err: TVkResult;
  54. begin
  55. err := fDeviceCommands.vkBindBufferMemory(fDeviceCommands.Device, fHandle, aMemory, aOffset);
  56. if (err <> VK_SUCCESS) then
  57. raise TvkuErrorException.Create('unable to bind buffer memory', err);
  58. fMemory := aMemory;
  59. end;
  60. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  61. function TvkuBuffer.GetMemoryRequirements: TVkMemoryRequirements;
  62. begin
  63. fDeviceCommands.vkGetBufferMemoryRequirements(fDeviceCommands.Device, fHandle, @result);
  64. end;
  65. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  66. procedure TvkuBuffer.AllocateAndBindBufferMemory(
  67. const aMemoryProperties: TVkPhysicalDeviceMemoryProperties;
  68. aAllocCallbacks: PVkAllocationCallbacks);
  69. var
  70. i: Integer;
  71. bits: VkUint32;
  72. MemReq: TVkMemoryRequirements;
  73. Mem: VkDeviceMemory;
  74. MemAllocInfo: TVkMemoryAllocateInfo;
  75. err: TVkResult;
  76. begin
  77. MemReq := GetMemoryRequirements;
  78. FillByte(MemAllocInfo, SizeOf(MemAllocInfo), 0);
  79. MemAllocInfo.sType := VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  80. MemAllocInfo.pNext := nil;
  81. MemAllocInfo.allocationSize := MemReq.size;
  82. MemAllocInfo.memoryTypeIndex := vkuGetMemoryTypeIndex(
  83. aMemoryProperties,
  84. MemReq.memoryTypeBits,
  85. VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
  86. if not Assigned(aAllocCallbacks) then
  87. aAllocCallbacks := AllocCallbacks;
  88. err := fDeviceCommands.vkAllocateMemory(fDeviceCommands.Device, @MemAllocInfo, aAllocCallbacks, @Mem);
  89. if (err <> VK_SUCCESS) then
  90. raise TvkuErrorException.Create('unable to allocate buffer memory', err);
  91. fBufferMemory := TvkuDeviceMemory.Create(Mem, true, fDeviceCommands, aAllocCallbacks);
  92. BindMemory(fBufferMemory.Handle, 0);
  93. end;
  94. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  95. constructor TvkuBuffer.Create(const aCreateInfo: TVkBufferCreateInfo; const aDeviceCommands: TVkDeviceCommands;
  96. const aAllocCallbacks: PVkAllocationCallbacks);
  97. begin
  98. inherited Create;
  99. fDeviceCommands := aDeviceCommands;
  100. SetAllocCallbacks(aAllocCallbacks);
  101. CreateHandle(aCreateInfo);
  102. end;
  103. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  104. constructor TvkuBuffer.Create(const aHandle: VkBuffer; const aOwnsHandle: Boolean;
  105. const aDeviceCommands: TVkDeviceCommands; const aAllocCallbacks: PVkAllocationCallbacks);
  106. begin
  107. inherited Create;
  108. fHandle := aHandle;
  109. fDeviceCommands := aDeviceCommands;
  110. OwnsHandle := aOwnsHandle;
  111. SetAllocCallbacks(aAllocCallbacks);
  112. end;
  113. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  114. destructor TvkuBuffer.Destroy;
  115. begin
  116. if OwnsHandle and (fHandle <> VK_INVALID_NDP_HANDLE) then
  117. fDeviceCommands.vkDestroyBuffer(fDeviceCommands.Device, fHandle, AllocCallbacks);
  118. fHandle := VK_INVALID_NDP_HANDLE;
  119. FreeAndNil(fBufferMemory);
  120. inherited Destroy;
  121. end;
  122. end.