Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

162 linhas
7.3 KiB

  1. unit uvkuImageFactory;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils,
  6. Vulkan, uvkuImage, uvkuDevice, uvkuAllocationHandler, uvkuTypes;
  7. type
  8. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. TvkuImageEx = class(TvkuImage)
  10. private
  11. fAllocHandler: TvkuAllocationHandler;
  12. fOwnsHandle: Boolean;
  13. public
  14. destructor Destroy; override;
  15. end;
  16. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. TvkuImageFactory = class(TObject)
  18. private
  19. fDeviceCommands: TVkDeviceCommands;
  20. fFlags: VkImageCreateFlags;
  21. fImageType: TVkImageType;
  22. fFormat: TVkFormat;
  23. fExtent: TVkExtent3D;
  24. fMipLevels: VkUint32;
  25. fArrayLayers: VkUint32;
  26. fSamples: TVkSampleCountFlagBits;
  27. fTiling: TVkImageTiling;
  28. fUsage: VkImageUsageFlags;
  29. fSharingMode: TVkSharingMode;
  30. fQueueFamilyIndices: TvkuUint32Arr;
  31. fInitialLayout: TVkImageLayout;
  32. function GetQueueFamilyIndex(const aIndex: Integer): VkUint32;
  33. function GetQueueFamilyIndexCount: Integer;
  34. procedure SetQueueFamilyIndex(const aIndex: Integer; aValue: VkUint32);
  35. public
  36. property DeviceCommands: TVkDeviceCommands read fDeviceCommands;
  37. property Flags: VkImageCreateFlags read fFlags write fFlags;
  38. property ImageType: TVkImageType read fImageType write fImageType;
  39. property Format: TVkFormat read fFormat write fFormat;
  40. property Extent: TVkExtent3D read fExtent write fExtent;
  41. property MipLevels: VkUint32 read fMipLevels write fMipLevels;
  42. property ArrayLayers: VkUint32 read fArrayLayers write fArrayLayers;
  43. property Samples: TVkSampleCountFlagBits read fSamples write fSamples;
  44. property Tiling: TVkImageTiling read fTiling write fTiling;
  45. property Usage: VkImageUsageFlags read fUsage write fUsage;
  46. property SharingMode: TVkSharingMode read fSharingMode write fSharingMode;
  47. property InitialLayout: TVkImageLayout read fInitialLayout write fInitialLayout;
  48. property QueueFamilyIndices: TvkuUint32Arr read fQueueFamilyIndices;
  49. property QueueFamilyIndexCount: Integer read GetQueueFamilyIndexCount;
  50. property QueueFamilyIndex[const aIndex: Integer]: VkUint32 read GetQueueFamilyIndex write SetQueueFamilyIndex;
  51. function CreateImage: TvkuImageEx;
  52. function CreateImage(
  53. const aAllocHandler: TvkuAllocationHandler;
  54. const aOwnsHandler: Boolean): TvkuImageEx;
  55. constructor Create(const aDevice: TvkuDevice);
  56. constructor Create(const aDeviceCommands: TVkDeviceCommands);
  57. end;
  58. implementation
  59. uses
  60. uvkuUtils;
  61. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  62. //TvkuImageEx////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  63. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  64. destructor TvkuImageEx.Destroy;
  65. begin
  66. if fOwnsHandle then
  67. FreeAndNil(fAllocHandler);
  68. inherited Destroy;
  69. end;
  70. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  71. //TvkuImageFactory///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  72. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  73. function TvkuImageFactory.GetQueueFamilyIndex(const aIndex: Integer): VkUint32;
  74. begin
  75. if (aIndex < low(fQueueFamilyIndices)) or (aIndex > high(fQueueFamilyIndices)) then
  76. raise TvkuException.CreateFmt('index (%d) out of range (%d : %d)', [aIndex, Low(fQueueFamilyIndices), High(fQueueFamilyIndices)]);
  77. result := fQueueFamilyIndices[aIndex];
  78. end;
  79. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  80. function TvkuImageFactory.GetQueueFamilyIndexCount: Integer;
  81. begin
  82. result := Length(fQueueFamilyIndices);
  83. end;
  84. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  85. procedure TvkuImageFactory.SetQueueFamilyIndex(const aIndex: Integer; aValue: VkUint32);
  86. begin
  87. if (aIndex < low(fQueueFamilyIndices)) or (aIndex > high(fQueueFamilyIndices)) then
  88. raise TvkuException.CreateFmt('index (%d) out of range (%d : %d)', [aIndex, Low(fQueueFamilyIndices), High(fQueueFamilyIndices)]);
  89. fQueueFamilyIndices[aIndex] := aValue;
  90. end;
  91. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  92. function TvkuImageFactory.CreateImage: TvkuImageEx;
  93. begin
  94. result := CreateImage(nil, false);
  95. end;
  96. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  97. function TvkuImageFactory.CreateImage(const aAllocHandler: TvkuAllocationHandler; const aOwnsHandler: Boolean): TvkuImageEx;
  98. var
  99. CreateInfo: TVkImageCreateInfo;
  100. begin
  101. FillByte(CreateInfo, 0, SizeOf(CreateInfo));
  102. CreateInfo.sType := VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
  103. CreateInfo.pNext := nil;
  104. CreateInfo.flags := fFlags;
  105. CreateInfo.imageType := fImageType;
  106. CreateInfo.format := fFormat;
  107. CreateInfo.extent := fExtent;
  108. CreateInfo.mipLevels := fMipLevels;
  109. CreateInfo.arrayLayers := fArrayLayers;
  110. CreateInfo.samples := fSamples;
  111. CreateInfo.tiling := fTiling;
  112. CreateInfo.usage := fUsage;
  113. CreateInfo.sharingMode := fSharingMode;
  114. CreateInfo.queueFamilyIndexCount := Length(fQueueFamilyIndices);
  115. CreateInfo.pQueueFamilyIndices := @fQueueFamilyIndices[0];
  116. CreateInfo.initialLayout := fInitialLayout;
  117. if Assigned(aAllocHandler)
  118. then result := TvkuImageEx.Create(CreateInfo, fDeviceCommands, aAllocHandler.GetAllocationCallbacksPtr)
  119. else result := TvkuImageEx.Create(CreateInfo, fDeviceCommands);
  120. try
  121. TvkuImageEx(result).fAllocHandler := aAllocHandler;
  122. TvkuImageEx(result).fOwnsHandle := aOwnsHandler;
  123. except
  124. FreeAndNil(result);
  125. raise;
  126. end;
  127. end;
  128. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  129. constructor TvkuImageFactory.Create(const aDevice: TvkuDevice);
  130. begin
  131. Create(aDevice.DeviceCommands);
  132. end;
  133. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  134. constructor TvkuImageFactory.Create(const aDeviceCommands: TVkDeviceCommands);
  135. begin
  136. inherited Create;
  137. fDeviceCommands := aDeviceCommands;
  138. end;
  139. end.