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.
 
 

105 lines
3.9 KiB

  1. unit uvkuSurface;
  2. {$mode objfpc}{$H+}
  3. {$I Vulkan.inc}
  4. interface
  5. uses
  6. Classes, SysUtils,
  7. Vulkan;
  8. type
  9. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. TvkuSurface = class(TObject)
  11. private
  12. fHandle: VkSurfaceKHR;
  13. fInstance: VkInstance;
  14. fAllocCallbacks: PVkAllocationCallbacks;
  15. fInstanceCommands: TVkInstanceCommands;
  16. procedure CreateHandle(const aCreateInfo: Pointer; const aAllocCallbacks: PVkAllocationCallbacks);
  17. public
  18. property Handle: VkSurfaceKHR read fHandle;
  19. property Instance: VkInstance read fInstance;
  20. property InstanceCommands: TVkInstanceCommands read fInstanceCommands;
  21. {$IF DEFINED(VK_USE_PLATFORM_WIN32_KHR)}
  22. constructor Create(
  23. const aInstance: VkInstance;
  24. const aCreateInfo: TVkWin32SurfaceCreateInfoKHR;
  25. const aInstanceCommands: TVkInstanceCommands);
  26. constructor Create(
  27. const aInstance: VkInstance;
  28. const aCreateInfo: TVkWin32SurfaceCreateInfoKHR;
  29. const aInstanceCommands: TVkInstanceCommands;
  30. const aAllocCallbacks: TVkAllocationCallbacks);
  31. {$ELSE}
  32. {$ERROR 'unknown or unsupportet platform'}
  33. {$ENDIF}
  34. destructor Destroy; override;
  35. end;
  36. implementation
  37. uses
  38. uvkuUtils;
  39. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  40. //TvkuSurface////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  41. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  42. procedure TvkuSurface.CreateHandle(const aCreateInfo: Pointer; const aAllocCallbacks: PVkAllocationCallbacks);
  43. var err: TVkResult;
  44. begin
  45. if Assigned(aAllocCallbacks) then begin
  46. new(fAllocCallbacks);
  47. fAllocCallbacks^ := aAllocCallbacks^;
  48. end;
  49. {$IF DEFINED(VK_USE_PLATFORM_WIN32_KHR)}
  50. if not Assigned(fInstanceCommands.vkCreateWin32SurfaceKHR) then
  51. raise TvkuException.Create('invalid function pointer: vkCreateWin32SurfaceKHR; did you enabled the VK_KHR_win32_surface extension?');
  52. err := fInstanceCommands.vkCreateWin32SurfaceKHR(fInstance, aCreateInfo, fAllocCallbacks, @fHandle);
  53. {$ENDIF}
  54. if (err < VK_SUCCESS) then
  55. raise TvkuErrorException.Create('unable to create surface', err);
  56. end;
  57. {$IF DEFINED(VK_USE_PLATFORM_WIN32_KHR)}
  58. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  59. constructor TvkuSurface.Create(const aInstance: VkInstance; const aCreateInfo: TVkWin32SurfaceCreateInfoKHR;
  60. const aInstanceCommands: TVkInstanceCommands);
  61. begin
  62. inherited Create;
  63. fInstance := aInstance;
  64. fInstanceCommands := aInstanceCommands;
  65. CreateHandle(@aCreateInfo, nil);
  66. end;
  67. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  68. constructor TvkuSurface.Create(const aInstance: VkInstance; const aCreateInfo: TVkWin32SurfaceCreateInfoKHR;
  69. const aInstanceCommands: TVkInstanceCommands; const aAllocCallbacks: TVkAllocationCallbacks);
  70. begin
  71. inherited Create;
  72. fInstance := aInstance;
  73. fInstanceCommands := aInstanceCommands;
  74. CreateHandle(@aCreateInfo, @aAllocCallbacks);
  75. end;
  76. {$ENDIF}
  77. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  78. destructor TvkuSurface.Destroy;
  79. begin
  80. if (fHandle <> VK_INVALID_NDP_HANDLE) then begin
  81. fInstanceCommands.vkDestroySurfaceKHR(fInstance, fHandle, fAllocCallbacks);
  82. fHandle := 0;
  83. end;
  84. if Assigned(fAllocCallbacks) then begin
  85. Dispose(fAllocCallbacks);
  86. fAllocCallbacks := nil;
  87. end;
  88. inherited Destroy;
  89. end;
  90. end.