unit uvkuBufferFactory; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Vulkan, uvkuBuffer, uvkuDevice, uvkuAllocationHandler, uvkuTypes, uvkuFactoryBase; type ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TvkuBufferEx = class(TvkuBuffer) private fAllocHandler: TvkuAllocationHandler; fOwnsHandle: Boolean; public destructor Destroy; override; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TvkuBufferFactory = class(TvkuDeviceObjFactory) private fFlags: VkBufferCreateFlags; fSize: VkDeviceSize; fUsage: VkBufferUsageFlags; fSharingMode: TVkSharingMode; fQueueFamilyIndices: TvkuUint32Arr; function GetQueueFamilyIndex(const aIndex: Integer): VkUint32; function GetQueueFamilyIndexCount: Integer; procedure SetQueueFamilyIndex(const aIndex: Integer; aValue: VkUint32); public property Flags: VkBufferCreateFlags read fFlags write fFlags; property Size: VkDeviceSize read fSize write fSize; property Usage: VkBufferUsageFlags read fUsage write fUsage; property SharingMode: TVkSharingMode read fSharingMode write fSharingMode; property QueueFamilyIndices: TvkuUint32Arr read fQueueFamilyIndices; property QueueFamilyIndexCount: Integer read GetQueueFamilyIndexCount; property QueueFamilyIndex[const aIndex: Integer]: VkUint32 read GetQueueFamilyIndex write SetQueueFamilyIndex; function CreateBuffer: TvkuBufferEx; function CreateBuffer( const aAllocHandler: TvkuAllocationHandler; const aOwnsHandler: Boolean): TvkuBufferEx; end; implementation uses uvkuUtils; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //TvkuBufferEx/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// destructor TvkuBufferEx.Destroy; begin inherited Destroy; if fOwnsHandle then FreeAndNil(fAllocHandler); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //TvkuBufferFactory////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TvkuBufferFactory.GetQueueFamilyIndex(const aIndex: Integer): VkUint32; begin if (aIndex < low(fQueueFamilyIndices)) or (aIndex > high(fQueueFamilyIndices)) then raise TvkuException.CreateFmt('index (%d) out of range (%d : %d)', [aIndex, Low(fQueueFamilyIndices), High(fQueueFamilyIndices)]); result := fQueueFamilyIndices[aIndex]; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TvkuBufferFactory.GetQueueFamilyIndexCount: Integer; begin result := Length(fQueueFamilyIndices); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TvkuBufferFactory.SetQueueFamilyIndex(const aIndex: Integer; aValue: VkUint32); begin if (aIndex < low(fQueueFamilyIndices)) or (aIndex > high(fQueueFamilyIndices)) then raise TvkuException.CreateFmt('index (%d) out of range (%d : %d)', [aIndex, Low(fQueueFamilyIndices), High(fQueueFamilyIndices)]); fQueueFamilyIndices[aIndex] := aValue; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TvkuBufferFactory.CreateBuffer: TvkuBufferEx; begin result := CreateBuffer(nil, false); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TvkuBufferFactory.CreateBuffer(const aAllocHandler: TvkuAllocationHandler; const aOwnsHandler: Boolean): TvkuBufferEx; var CreateInfo: TVkBufferCreateInfo; begin FillByte(CreateInfo, 0, SizeOf(CreateInfo)); CreateInfo.sType := VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; CreateInfo.pNext := nil; CreateInfo.flags := fFlags; CreateInfo.size := fSize; CreateInfo.usage := fUsage; CreateInfo.sharingMode := fSharingMode; CreateInfo.queueFamilyIndexCount := Length(fQueueFamilyIndices); CreateInfo.pQueueFamilyIndices := @fQueueFamilyIndices[0]; if Assigned(aAllocHandler) then result := TvkuBufferEx.Create(CreateInfo, DeviceCommands, aAllocHandler.GetAllocationCallbacksPtr) else result := TvkuBufferEx.Create(CreateInfo, DeviceCommands); try TvkuBufferEx(result).fAllocHandler := aAllocHandler; TvkuBufferEx(result).fOwnsHandle := aOwnsHandler; except FreeAndNil(result); raise; end; end; end.