You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
5.8 KiB

  1. unit uvkuFrameBufferFactory;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils,
  6. Vulkan, uvkuFrameBuffer, uvkuFactoryBase, uvkuAllocationHandler, uvkuTypes;
  7. type
  8. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. TvkuFrameBufferEx = class(TvkuFrameBuffer)
  10. private
  11. fAllocHandler: TvkuAllocationHandler;
  12. fOwnsHandler: Boolean;
  13. public
  14. destructor Destroy; override;
  15. end;
  16. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. TvkuFrameBufferFactory = class(TvkuDeviceObjFactory)
  18. private
  19. fFlags: VkFramebufferCreateFlags;
  20. fWidth: VkUint32;
  21. fHeight: VkUint32;
  22. fLayers: VkUint32;
  23. fAttachments: TvkuImageViewArr;
  24. function GetAttachment(const aIndex: Integer): VkImageView;
  25. function GetAttachmentCount: Integer;
  26. procedure SetAttachment(const aIndex: Integer; aValue: VkImageView);
  27. procedure SetAttachmentCount(aValue: Integer);
  28. public
  29. property Flags: VkFramebufferCreateFlags read fFlags write fFlags;
  30. property Width: VkUint32 read fWidth write fWidth;
  31. property Height: VkUint32 read fHeight write fHeight;
  32. property Layers: VkUint32 read fLayers write fLayers;
  33. property Attachments: TvkuImageViewArr read fAttachments write fAttachments;
  34. property AttachmentCount: Integer read GetAttachmentCount write SetAttachmentCount;
  35. property Attachment[const aIndex: Integer]: VkImageView read GetAttachment write SetAttachment;
  36. function CreateFrameBuffer(
  37. const aRenderPass: VkRenderPass): TvkuFrameBufferEx;
  38. function CreateFrameBuffer(
  39. const aRenderPass: VkRenderPass;
  40. const aAllocHandler: TvkuAllocationHandler;
  41. const aOwnsHandler: Boolean): TvkuFrameBufferEx;
  42. end;
  43. implementation
  44. uses
  45. uvkuUtils;
  46. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  47. //TvkuFrameBufferEx//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  48. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  49. destructor TvkuFrameBufferEx.Destroy;
  50. begin
  51. inherited Destroy;
  52. if fOwnsHandler then
  53. FreeAndNil(fAllocHandler);
  54. end;
  55. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  56. //TvkuFrameBufferFactory/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  58. function TvkuFrameBufferFactory.GetAttachment(const aIndex: Integer): VkImageView;
  59. begin
  60. if (aIndex < low(fAttachments)) or (aIndex > high(fAttachments)) then
  61. raise TvkuException.CreateFmt('index (%d) out of range (%d : %d)', [aIndex, Low(fAttachments), High(fAttachments)]);
  62. result := fAttachments[aIndex];
  63. end;
  64. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  65. function TvkuFrameBufferFactory.GetAttachmentCount: Integer;
  66. begin
  67. result := Length(fAttachments);
  68. end;
  69. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  70. procedure TvkuFrameBufferFactory.SetAttachment(const aIndex: Integer; aValue: VkImageView);
  71. begin
  72. if (aIndex < low(fAttachments)) or (aIndex > high(fAttachments)) then
  73. raise TvkuException.CreateFmt('index (%d) out of range (%d : %d)', [aIndex, Low(fAttachments), High(fAttachments)]);
  74. fAttachments[aIndex] := aValue;
  75. end;
  76. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  77. procedure TvkuFrameBufferFactory.SetAttachmentCount(aValue: Integer);
  78. begin
  79. SetLength(fAttachments, aValue);
  80. end;
  81. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  82. function TvkuFrameBufferFactory.CreateFrameBuffer(const aRenderPass: VkRenderPass): TvkuFrameBufferEx;
  83. begin
  84. result := CreateFrameBuffer(aRenderPass, nil, false);
  85. end;
  86. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  87. function TvkuFrameBufferFactory.CreateFrameBuffer(const aRenderPass: VkRenderPass;
  88. const aAllocHandler: TvkuAllocationHandler; const aOwnsHandler: Boolean): TvkuFrameBufferEx;
  89. var
  90. CreateInfo: TVkFramebufferCreateInfo;
  91. begin
  92. FillByte(CreateInfo, SizeOf(CreateInfo), 0);
  93. CreateInfo.sType := VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
  94. CreateInfo.pNext := nil;
  95. CreateInfo.flags := fFlags;
  96. CreateInfo.renderPass := aRenderPass;
  97. CreateInfo.attachmentCount := Length(fAttachments);
  98. CreateInfo.pAttachments := @fAttachments[0];
  99. CreateInfo.width := fWidth;
  100. CreateInfo.height := fHeight;
  101. CreateInfo.layers := fLayers;
  102. if Assigned(aAllocHandler)
  103. then result := TvkuFrameBufferEx.Create(CreateInfo, DeviceCommands, aAllocHandler.GetAllocationCallbacksPtr)
  104. else result := TvkuFrameBufferEx.Create(CreateInfo, DeviceCommands);
  105. try
  106. TvkuFrameBufferEx(result).fAllocHandler := aAllocHandler;
  107. TvkuFrameBufferEx(result).fOwnsHandler := aOwnsHandler;
  108. except
  109. FreeAndNil(result);
  110. raise;
  111. end;
  112. end;
  113. end.