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.

137 rivejä
6.1 KiB

  1. unit uvkuDescriptorPool;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils,
  6. Vulkan, uvkuBase, uvkuTypes;
  7. type
  8. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. TvkuDescriptorPool = class(TvkuAllocHandle)
  10. private
  11. fHandle: VkDescriptorPool;
  12. fDeviceCommands: TVkDeviceCommands;
  13. procedure CreateHandle(const aCreateInfo: TVkDescriptorPoolCreateInfo);
  14. public
  15. property Handle: VkDescriptorPool read fHandle;
  16. property DeviceCommands: TVkDeviceCommands read fDeviceCommands;
  17. function AllocateDescriptorSet(
  18. const aDescriptorSetLayout: VkDescriptorSetLayout): VkDescriptorSet;
  19. function AllocateDescriptorSets(
  20. const aDescriptorSetLayouts: array of VkDescriptorSetLayout): TVkDescriptorSetArr;
  21. procedure FreeDescriptorSet(
  22. const aDescriptorSet: VkDescriptorSet);
  23. procedure FreeDescriptorSets(
  24. const aDescriptorSets: TVkDescriptorSetArr);
  25. constructor Create(
  26. const aCreateInfo: TVkDescriptorPoolCreateInfo;
  27. const aDeviceCommands: TVkDeviceCommands;
  28. const aAllocCallbacks: PVkAllocationCallbacks = nil);
  29. constructor Create(
  30. const aHandle: VkDescriptorPool;
  31. const aOwnsHandle: Boolean;
  32. const aDeviceCommands: TVkDeviceCommands;
  33. const aAllocCallbacks: PVkAllocationCallbacks = nil);
  34. destructor Destroy; override;
  35. end;
  36. implementation
  37. uses
  38. uvkuUtils;
  39. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  40. //TvkuDescriptorPool/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  41. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  42. procedure TvkuDescriptorPool.CreateHandle(const aCreateInfo: TVkDescriptorPoolCreateInfo);
  43. var err: TVkResult;
  44. begin
  45. if not Assigned(fDeviceCommands.vkCreateDescriptorPool) then
  46. raise TvkuInvalidFuncPtrException.Create('vkCreateDescriptorPool');
  47. err := fDeviceCommands.vkCreateDescriptorPool(fDeviceCommands.Device, @aCreateInfo, AllocCallbacks, @fHandle);
  48. if (err <> VK_SUCCESS) then
  49. raise TvkuErrorException.Create('unable to create descriptor pool', err);
  50. end;
  51. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  52. function TvkuDescriptorPool.AllocateDescriptorSet(const aDescriptorSetLayout: VkDescriptorSetLayout): VkDescriptorSet;
  53. begin
  54. result := AllocateDescriptorSets([ aDescriptorSetLayout ])[0];
  55. end;
  56. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. function TvkuDescriptorPool.AllocateDescriptorSets(const aDescriptorSetLayouts: array of VkDescriptorSetLayout): TVkDescriptorSetArr;
  58. var
  59. AllocInfo: TVkDescriptorSetAllocateInfo;
  60. err: TVkResult;
  61. begin
  62. if not Assigned(fDeviceCommands.vkAllocateDescriptorSets) then
  63. raise TvkuInvalidFuncPtrException.Create('vkAllocateDescriptorSets');
  64. FillByte(AllocInfo, SizeOf(AllocInfo), 0);
  65. AllocInfo.sType := VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
  66. AllocInfo.pNext := nil;
  67. AllocInfo.descriptorPool := fHandle;
  68. AllocInfo.descriptorSetCount := Length(aDescriptorSetLayouts);
  69. AllocInfo.pSetLayouts := @aDescriptorSetLayouts[0];
  70. SetLength(result, AllocInfo.descriptorSetCount);
  71. err := fDeviceCommands.vkAllocateDescriptorSets(fDeviceCommands.Device, @AllocInfo, @result[0]);
  72. if (err <> VK_SUCCESS) then
  73. raise TvkuErrorException.Create('unable to allocate descriptor sets', err);
  74. end;
  75. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  76. procedure TvkuDescriptorPool.FreeDescriptorSet(const aDescriptorSet: VkDescriptorSet);
  77. begin
  78. FreeDescriptorSets(TVkDescriptorSetArr.Create(aDescriptorSet));
  79. end;
  80. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  81. procedure TvkuDescriptorPool.FreeDescriptorSets(const aDescriptorSets: TVkDescriptorSetArr);
  82. var err: TVkResult;
  83. begin
  84. if not Assigned(fDeviceCommands.vkFreeDescriptorSets) then
  85. raise TvkuInvalidFuncPtrException.Create('vkFreeDescriptorSets');
  86. err := fDeviceCommands.vkFreeDescriptorSets(fDeviceCommands.Device, fHandle, Length(aDescriptorSets), @aDescriptorSets[0]);
  87. if (err <> VK_SUCCESS) then
  88. raise TvkuErrorException.Create('unable to free descriptor sets', err);
  89. end;
  90. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  91. constructor TvkuDescriptorPool.Create(const aCreateInfo: TVkDescriptorPoolCreateInfo;
  92. const aDeviceCommands: TVkDeviceCommands; const aAllocCallbacks: PVkAllocationCallbacks);
  93. begin
  94. inherited Create;
  95. fDeviceCommands := aDeviceCommands;
  96. SetAllocCallbacks(aAllocCallbacks);
  97. CreateHandle(aCreateInfo);
  98. end;
  99. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  100. constructor TvkuDescriptorPool.Create(const aHandle: VkDescriptorPool; const aOwnsHandle: Boolean;
  101. const aDeviceCommands: TVkDeviceCommands; const aAllocCallbacks: PVkAllocationCallbacks);
  102. begin
  103. inherited Create;
  104. fHandle := aHandle;
  105. fDeviceCommands := aDeviceCommands;
  106. OwnsHandle := aOwnsHandle;
  107. SetAllocCallbacks(aAllocCallbacks);
  108. end;
  109. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  110. destructor TvkuDescriptorPool.Destroy;
  111. begin
  112. if OwnsHandle and (fHandle <> VK_INVALID_NDP_HANDLE) then
  113. fDeviceCommands.vkDestroyDescriptorPool(fDeviceCommands.Device, fHandle, AllocCallbacks);
  114. fHandle := VK_INVALID_NDP_HANDLE;
  115. inherited Destroy;
  116. end;
  117. end.