No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

141 líneas
6.1 KiB

  1. unit uvkuBufferFactory;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils,
  6. Vulkan, uvkuBuffer, uvkuDevice, uvkuAllocationHandler, uvkuTypes;
  7. type
  8. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. TvkuBufferEx = class(TvkuBuffer)
  10. private
  11. fAllocHandler: TvkuAllocationHandler;
  12. fOwnsHandle: Boolean;
  13. public
  14. destructor Destroy; override;
  15. end;
  16. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. TvkuBufferFactory = class(TObject)
  18. private
  19. fDeviceCommands: TVkDeviceCommands;
  20. fFlags: VkBufferCreateFlags;
  21. fSize: VkDeviceSize;
  22. fUsage: VkBufferUsageFlags;
  23. fSharingMode: TVkSharingMode;
  24. fQueueFamilyIndices: TvkuUint32Arr;
  25. function GetQueueFamilyIndex(const aIndex: Integer): VkUint32;
  26. function GetQueueFamilyIndexCount: Integer;
  27. procedure SetQueueFamilyIndex(const aIndex: Integer; aValue: VkUint32);
  28. public
  29. property DeviceCommands: TVkDeviceCommands read fDeviceCommands;
  30. property Flags: VkBufferCreateFlags read fFlags write fFlags;
  31. property Size: VkDeviceSize read fSize write fSize;
  32. property Usage: VkBufferUsageFlags read fUsage write fUsage;
  33. property SharingMode: TVkSharingMode read fSharingMode write fSharingMode;
  34. property QueueFamilyIndices: TvkuUint32Arr read fQueueFamilyIndices;
  35. property QueueFamilyIndexCount: Integer read GetQueueFamilyIndexCount;
  36. property QueueFamilyIndex[const aIndex: Integer]: VkUint32 read GetQueueFamilyIndex write SetQueueFamilyIndex;
  37. function CreateBuffer: TvkuBufferEx;
  38. function CreateBuffer(
  39. const aAllocHandler: TvkuAllocationHandler;
  40. const aOwnsHandler: Boolean): TvkuBufferEx;
  41. constructor Create(const aDevice: TvkuDevice);
  42. constructor Create(const aDeviceCommands: TVkDeviceCommands);
  43. end;
  44. implementation
  45. uses
  46. uvkuUtils;
  47. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  48. //TvkuBufferEx///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  49. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  50. destructor TvkuBufferEx.Destroy;
  51. begin
  52. if fOwnsHandle then
  53. FreeAndNil(fAllocHandler);
  54. inherited Destroy;
  55. end;
  56. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. //TvkuBufferFactory//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  58. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  59. function TvkuBufferFactory.GetQueueFamilyIndex(const aIndex: Integer): VkUint32;
  60. begin
  61. if (aIndex < low(fQueueFamilyIndices)) or (aIndex > high(fQueueFamilyIndices)) then
  62. raise TvkuException.CreateFmt('index (%d) out of range (%d : %d)', [aIndex, Low(fQueueFamilyIndices), High(fQueueFamilyIndices)]);
  63. result := fQueueFamilyIndices[aIndex];
  64. end;
  65. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  66. function TvkuBufferFactory.GetQueueFamilyIndexCount: Integer;
  67. begin
  68. result := Length(fQueueFamilyIndices);
  69. end;
  70. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  71. procedure TvkuBufferFactory.SetQueueFamilyIndex(const aIndex: Integer; aValue: VkUint32);
  72. begin
  73. if (aIndex < low(fQueueFamilyIndices)) or (aIndex > high(fQueueFamilyIndices)) then
  74. raise TvkuException.CreateFmt('index (%d) out of range (%d : %d)', [aIndex, Low(fQueueFamilyIndices), High(fQueueFamilyIndices)]);
  75. fQueueFamilyIndices[aIndex] := aValue;
  76. end;
  77. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  78. function TvkuBufferFactory.CreateBuffer: TvkuBufferEx;
  79. begin
  80. result := CreateBuffer(nil, false);
  81. end;
  82. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  83. function TvkuBufferFactory.CreateBuffer(const aAllocHandler: TvkuAllocationHandler; const aOwnsHandler: Boolean): TvkuBufferEx;
  84. var
  85. CreateInfo: TVkBufferCreateInfo;
  86. begin
  87. FillByte(CreateInfo, 0, SizeOf(CreateInfo));
  88. CreateInfo.sType := VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  89. CreateInfo.pNext := nil;
  90. CreateInfo.flags := fFlags;
  91. CreateInfo.size := fSize;
  92. CreateInfo.usage := fUsage;
  93. CreateInfo.sharingMode := fSharingMode;
  94. CreateInfo.queueFamilyIndexCount := Length(fQueueFamilyIndices);
  95. CreateInfo.pQueueFamilyIndices := @fQueueFamilyIndices[0];
  96. if Assigned(aAllocHandler)
  97. then result := TvkuBufferEx.Create(CreateInfo, fDeviceCommands, aAllocHandler.GetAllocationCallbacksPtr)
  98. else result := TvkuBufferEx.Create(CreateInfo, fDeviceCommands);
  99. try
  100. TvkuBufferEx(result).fAllocHandler := aAllocHandler;
  101. TvkuBufferEx(result).fOwnsHandle := aOwnsHandler;
  102. except
  103. FreeAndNil(result);
  104. raise;
  105. end;
  106. end;
  107. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  108. constructor TvkuBufferFactory.Create(const aDevice: TvkuDevice);
  109. begin
  110. Create(aDevice.DeviceCommands);
  111. end;
  112. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  113. constructor TvkuBufferFactory.Create(const aDeviceCommands: TVkDeviceCommands);
  114. begin
  115. inherited Create;
  116. fDeviceCommands := aDeviceCommands;
  117. end;
  118. end.