Você não pode selecionar mais de 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.

362 linhas
16 KiB

  1. unit uvkuRenderPassFactory;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils,
  6. Vulkan, uvkuFactoryBase, uvkuRenderPass, uvkuTypes;
  7. type
  8. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. TvkuAttachmentDescription = class(TvkuStructure)
  10. public
  11. Flags: VkAttachmentDescriptionFlags;
  12. Format: TVkFormat;
  13. Samples: TVkSampleCountFlagBits;
  14. LoadOp: TVkAttachmentLoadOp;
  15. StoreOp: TVkAttachmentStoreOp;
  16. StencilLoadOp: TVkAttachmentLoadOp;
  17. StencilStoreOp: TVkAttachmentStoreOp;
  18. InitialLayout: TVkImageLayout;
  19. FinalLayout: TVkImageLayout;
  20. function GetStructure: TVkAttachmentDescription;
  21. procedure SetStructure(const aData: TVkAttachmentDescription);
  22. end;
  23. TvkuAttachmentDescriptionList = specialize TvkuObjList<TvkuAttachmentDescription>;
  24. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  25. TvkuAttachmentReference = class(TvkuStructure)
  26. public
  27. Attachment: VkUint32;
  28. Layout: TVkImageLayout;
  29. function GetStructure: TVkAttachmentReference;
  30. procedure SetStructure(const aData: TVkAttachmentReference);
  31. end;
  32. TvkuAttachmentReferenceList = specialize TvkuObjList<TvkuAttachmentReference>;
  33. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  34. TvkuSubpassDescription = class(TvkuStructure)
  35. private
  36. fInputAttachments: TvkuAttachmentReferenceList;
  37. fColorAttachments: TvkuAttachmentReferenceList;
  38. fResolveAttachments: TvkuAttachmentReferenceList;
  39. fDepthStencilAttachments: TvkuAttachmentReferenceList;
  40. fPreserveAttachments: TVkUint32List;
  41. fvkInputAttachments: TVkAttachmentReferenceArr;
  42. fvkColorAttachments: TVkAttachmentReferenceArr;
  43. fvkResolveAttachments: TVkAttachmentReferenceArr;
  44. fvkDepthStencilAttachments: TVkAttachmentReferenceArr;
  45. public
  46. Flags: VkSubpassDescriptionFlags;
  47. PipelineBindPoint: TVkPipelineBindPoint;
  48. property InputAttachments: TvkuAttachmentReferenceList read fInputAttachments;
  49. property ColorAttachments: TvkuAttachmentReferenceList read fColorAttachments;
  50. property ResolveAttachments: TvkuAttachmentReferenceList read fResolveAttachments;
  51. property DepthStencilAttachments: TvkuAttachmentReferenceList read fDepthStencilAttachments;
  52. property PreserveAttachments: TVkUint32List read fPreserveAttachments;
  53. function GetStructure: TVkSubpassDescription;
  54. procedure SetStructure(const aData: TVkSubpassDescription);
  55. procedure AfterConstruction; override;
  56. procedure BeforeDestruction; override;
  57. end;
  58. TvkuSubpassDescriptionList = specialize TvkuObjList<TvkuSubpassDescription>;
  59. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  60. TvkuSubpassDependency = class(TvkuStructure)
  61. public
  62. SrcSubpass: VkUint32;
  63. DstSubpass: VkUint32;
  64. SrcStageMask: VkPipelineStageFlags;
  65. DstStageMask: VkPipelineStageFlags;
  66. SrcAccessMask: VkAccessFlags;
  67. DstAccessMask: VkAccessFlags;
  68. DependencyFlags: VkDependencyFlags;
  69. function GetStructure: TVkSubpassDependency;
  70. procedure SetStructure(const aData: TVkSubpassDependency);
  71. end;
  72. TvkuSubpassDependencyList = specialize TvkuObjList<TvkuSubpassDependency>;
  73. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  74. TvkuRenderPassFactory = class(TvkuDeviceObjFactory)
  75. private
  76. fFlags: VkRenderPassCreateFlags;
  77. fAttachments: TvkuAttachmentDescriptionList;
  78. fSubpasses: TvkuSubpassDescriptionList;
  79. fDependencies: TvkuSubpassDependencyList;
  80. fvkAttachments: TVkAttachmentDescriptionArr;
  81. fvkSubpasses: TVkSubpassDescriptionArr;
  82. fvkDependencies: TVkSubpassDependencyArr;
  83. public
  84. property Flags: VkRenderPassCreateFlags read fFlags write fFlags;
  85. property Attachments: TvkuAttachmentDescriptionList read fAttachments;
  86. property Subpasses: TvkuSubpassDescriptionList read fSubpasses;
  87. property Dependencies: TvkuSubpassDependencyList read fDependencies;
  88. function GetStructure: TVkRenderPassCreateInfo;
  89. procedure SetStructure(const aData: TVkRenderPassCreateInfo);
  90. function CreateRenderPass: TvkuRenderPass;
  91. procedure AfterConstruction; override;
  92. procedure BeforeDestruction; override;
  93. end;
  94. implementation
  95. uses
  96. uvkuUtils;
  97. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  98. //TvkuAttachmentDescription//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  99. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  100. function TvkuAttachmentDescription.GetStructure: TVkAttachmentDescription;
  101. begin
  102. result.flags := Flags;
  103. result.format := Format;
  104. result.samples := Samples;
  105. result.loadOp := LoadOp;
  106. result.storeOp := StoreOp;
  107. result.stencilLoadOp := StencilLoadOp;
  108. result.stencilStoreOp := StencilStoreOp;
  109. result.initialLayout := InitialLayout;
  110. result.finalLayout := FinalLayout;
  111. end;
  112. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  113. procedure TvkuAttachmentDescription.SetStructure(const aData: TVkAttachmentDescription);
  114. begin
  115. Flags := aData.flags;
  116. Format := aData.format;
  117. Samples := aData.samples;
  118. LoadOp := aData.loadOp;
  119. StoreOp := aData.storeOp;
  120. StencilLoadOp := aData.stencilLoadOp;
  121. StencilStoreOp := aData.stencilStoreOp;
  122. InitialLayout := aData.initialLayout;
  123. FinalLayout := aData.finalLayout;
  124. end;
  125. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  126. //TvkuAttachmentReference////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  127. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  128. function TvkuAttachmentReference.GetStructure: TVkAttachmentReference;
  129. begin
  130. FillByte(result, SizeOf(result), 0);
  131. result.attachment := Attachment;
  132. result.layout := Layout;
  133. end;
  134. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  135. procedure TvkuAttachmentReference.SetStructure(const aData: TVkAttachmentReference);
  136. begin
  137. Attachment := aData.attachment;
  138. Layout := aData.layout;
  139. end;
  140. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  141. //TvkuSubpassDescription/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  142. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  143. function TvkuSubpassDescription.GetStructure: TVkSubpassDescription;
  144. var i: Integer;
  145. begin
  146. if (fResolveAttachments.Length <> 0) and
  147. (fResolveAttachments.Length <> fColorAttachments.Length) then
  148. raise TvkuException.Create('''ResolveAttachments'' must be empty or have the same size as ''ColorAttachments''');
  149. if (fDepthStencilAttachments.Length <> fColorAttachments.Length) then
  150. raise TvkuException.Create('''DepthStencilAttachments'' must have the same size as ''ColorAttachments''');
  151. SetLength(fvkInputAttachments, fInputAttachments.Length);
  152. for i := low(fvkInputAttachments) to high(fvkInputAttachments) do
  153. fvkInputAttachments[i] := fInputAttachments[i].GetStructure;
  154. SetLength(fvkColorAttachments, fColorAttachments.Length);
  155. for i := low(fvkColorAttachments) to high(fvkColorAttachments) do
  156. fvkColorAttachments[i] := fColorAttachments[i].GetStructure;
  157. SetLength(fvkResolveAttachments, fResolveAttachments.Length);
  158. for i := low(fvkResolveAttachments) to high(fvkResolveAttachments) do
  159. fvkResolveAttachments[i] := fResolveAttachments[i].GetStructure;
  160. SetLength(fvkDepthStencilAttachments, fDepthStencilAttachments.Length);
  161. for i := low(fvkDepthStencilAttachments) to high(fvkDepthStencilAttachments) do
  162. fvkDepthStencilAttachments[i] := fDepthStencilAttachments[i].GetStructure;
  163. result.flags := Flags;
  164. result.pipelineBindPoint := PipelineBindPoint;
  165. result.inputAttachmentCount := Length(fvkInputAttachments);
  166. result.pInputAttachments := @fvkInputAttachments[0];
  167. result.colorAttachmentCount := Length(fvkColorAttachments);
  168. result.pColorAttachments := @fvkColorAttachments[0];
  169. result.pResolveAttachments := @fvkResolveAttachments[0];
  170. result.pDepthStencilAttachment := @fvkDepthStencilAttachments[0];
  171. result.preserveAttachmentCount := fPreserveAttachments.Length;
  172. result.pPreserveAttachments := fPreserveAttachments.PData;
  173. end;
  174. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  175. procedure TvkuSubpassDescription.SetStructure(const aData: TVkSubpassDescription);
  176. var i: Integer;
  177. begin
  178. Flags := aData.flags;
  179. PipelineBindPoint := aData.pipelineBindPoint;
  180. if Assigned(aData.pInputAttachments) then begin
  181. fInputAttachments.Length := aData.inputAttachmentCount;
  182. for i := 0 to aData.inputAttachmentCount-1 do
  183. fInputAttachments[i].SetStructure((aData.pInputAttachments + i)^);
  184. end else
  185. fInputAttachments.Length := 0;
  186. if Assigned(aData.pResolveAttachments) then begin
  187. fResolveAttachments.Length := aData.colorAttachmentCount;
  188. for i := 0 to aData.colorAttachmentCount-1 do
  189. fResolveAttachments[i].SetStructure((aData.pResolveAttachments + i)^);
  190. end else
  191. fResolveAttachments.Length := 0;
  192. if Assigned(aData.pDepthStencilAttachment) then begin
  193. fDepthStencilAttachments.Length := aData.colorAttachmentCount;
  194. for i := 0 to aData.colorAttachmentCount-1 do
  195. fDepthStencilAttachments[i].SetStructure((aData.pDepthStencilAttachment + i)^);
  196. end else
  197. fDepthStencilAttachments.Length := 0;
  198. fPreserveAttachments.SetData(aData.pPreserveAttachments, aData.preserveAttachmentCount);
  199. end;
  200. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  201. procedure TvkuSubpassDescription.AfterConstruction;
  202. begin
  203. inherited AfterConstruction;
  204. fInputAttachments := TvkuAttachmentReferenceList.Create;
  205. fColorAttachments := TvkuAttachmentReferenceList.Create;
  206. fResolveAttachments := TvkuAttachmentReferenceList.Create;
  207. fDepthStencilAttachments := TvkuAttachmentReferenceList.Create;
  208. fPreserveAttachments := TVkUint32List.Create;
  209. end;
  210. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  211. procedure TvkuSubpassDescription.BeforeDestruction;
  212. begin
  213. FreeAndNil(fPreserveAttachments);
  214. FreeAndNil(fDepthStencilAttachments);
  215. FreeAndNil(fResolveAttachments);
  216. FreeAndNil(fColorAttachments);
  217. FreeAndNil(fInputAttachments);
  218. inherited BeforeDestruction;
  219. end;
  220. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  221. //TvkuSubpassDependency//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  222. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  223. function TvkuSubpassDependency.GetStructure: TVkSubpassDependency;
  224. begin
  225. result.srcSubpass := SrcSubpass;
  226. result.dstSubpass := DstSubpass;
  227. result.srcStageMask := SrcStageMask;
  228. result.dstStageMask := DstStageMask;
  229. result.srcAccessMask := SrcAccessMask;
  230. result.dstAccessMask := DstAccessMask;
  231. result.dependencyFlags := DependencyFlags;
  232. end;
  233. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  234. procedure TvkuSubpassDependency.SetStructure(const aData: TVkSubpassDependency);
  235. begin
  236. SrcSubpass := aData.srcSubpass;
  237. DstSubpass := aData.dstSubpass;
  238. SrcStageMask := aData.srcStageMask;
  239. DstStageMask := aData.dstStageMask;
  240. SrcAccessMask := aData.srcAccessMask;
  241. DstAccessMask := aData.dstAccessMask;
  242. DependencyFlags := aData.dependencyFlags;
  243. end;
  244. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  245. //TvkuRenderPassFactory//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  246. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  247. function TvkuRenderPassFactory.GetStructure: TVkRenderPassCreateInfo;
  248. var i: Integer;
  249. begin
  250. SetLength(fvkAttachments, fAttachments.Length);
  251. for i := low(fvkAttachments) to high(fvkAttachments) do
  252. fvkAttachments[i] := fAttachments[i].GetStructure;
  253. SetLength(fvkSubpasses, fSubpasses.Length);
  254. for i := low(fvkSubpasses) to high(fvkSubpasses) do
  255. fvkSubpasses[i] := fSubpasses[i].GetStructure;
  256. SetLength(fvkDependencies, fDependencies.Length);
  257. for i := low(fvkDependencies) to high(fvkDependencies) do
  258. fvkDependencies[i] := fDependencies[i].GetStructure;
  259. FillByte(result, SizeOf(result), 0);
  260. result.sType := VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
  261. result.pNext := nil;
  262. result.flags := fFlags;
  263. result.attachmentCount := Length(fvkAttachments);
  264. result.pAttachments := @fvkAttachments[0];
  265. result.subpassCount := Length(fvkSubpasses);
  266. result.pSubpasses := @fvkSubpasses[0];
  267. result.dependencyCount := Length(fvkDependencies);
  268. result.pDependencies := @fvkDependencies[0];
  269. end;
  270. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  271. procedure TvkuRenderPassFactory.SetStructure(const aData: TVkRenderPassCreateInfo);
  272. var i: Integer;
  273. begin
  274. fFlags := aData.flags;
  275. fAttachments.Length := aData.attachmentCount;
  276. for i := 0 to fAttachments.Length-1 do
  277. fAttachments[i].SetStructure((aData.pAttachments + i)^);
  278. fSubpasses.Length := aData.subpassCount;
  279. for i := 0 to fSubpasses.Length-1 do
  280. fSubpasses[i].SetStructure((aData.pSubpasses + i)^);
  281. fDependencies.Length := aData.dependencyCount;
  282. for i := 0 to fDependencies.Length-1 do
  283. fDependencies[i].SetStructure((aData.pDependencies + i)^);
  284. end;
  285. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  286. function TvkuRenderPassFactory.CreateRenderPass: TvkuRenderPass;
  287. begin
  288. result := TvkuRenderPass.Create(GetStructure, DeviceCommands, AllocCallbacks);
  289. end;
  290. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  291. procedure TvkuRenderPassFactory.AfterConstruction;
  292. begin
  293. inherited AfterConstruction;
  294. fAttachments := TvkuAttachmentDescriptionList.Create;
  295. fSubpasses := TvkuSubpassDescriptionList.Create;
  296. fDependencies := TvkuSubpassDependencyList.Create;
  297. end;
  298. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  299. procedure TvkuRenderPassFactory.BeforeDestruction;
  300. begin
  301. FreeAndNil(fDependencies);
  302. FreeAndNil(fSubpasses);
  303. FreeAndNil(fAttachments);
  304. inherited BeforeDestruction;
  305. end;
  306. end.