|
- unit uvkuBufferFactory;
-
- {$mode objfpc}{$H+}
-
- interface
-
- uses
- Classes, SysUtils,
- Vulkan, uvkuBuffer, uvkuDevice, uvkuAllocationHandler, uvkuTypes;
-
- type
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- TvkuBufferEx = class(TvkuBuffer)
- private
- fAllocHandler: TvkuAllocationHandler;
- fOwnsHandle: Boolean;
- public
- destructor Destroy; override;
- end;
-
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- TvkuBufferFactory = class(TObject)
- private
- fDeviceCommands: TVkDeviceCommands;
- 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 DeviceCommands: TVkDeviceCommands read fDeviceCommands;
- 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;
-
- constructor Create(const aDevice: TvkuDevice);
- constructor Create(const aDeviceCommands: TVkDeviceCommands);
- end;
-
- implementation
-
- uses
- uvkuUtils;
-
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //TvkuBufferEx///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- destructor TvkuBufferEx.Destroy;
- begin
- if fOwnsHandle then
- FreeAndNil(fAllocHandler);
- inherited Destroy;
- 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, fDeviceCommands, aAllocHandler.GetAllocationCallbacksPtr)
- else result := TvkuBufferEx.Create(CreateInfo, fDeviceCommands);
- try
- TvkuBufferEx(result).fAllocHandler := aAllocHandler;
- TvkuBufferEx(result).fOwnsHandle := aOwnsHandler;
- except
- FreeAndNil(result);
- raise;
- end;
- end;
-
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- constructor TvkuBufferFactory.Create(const aDevice: TvkuDevice);
- begin
- Create(aDevice.DeviceCommands);
- end;
-
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- constructor TvkuBufferFactory.Create(const aDeviceCommands: TVkDeviceCommands);
- begin
- inherited Create;
- fDeviceCommands := aDeviceCommands;
- end;
-
- end.
|