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.

135 regels
5.5 KiB

  1. unit uvkuDebugReporter;
  2. {$mode objfpc}{$H+}
  3. {$I Vulkan.inc}
  4. interface
  5. uses
  6. Classes, SysUtils,
  7. Vulkan, uvkuBase, uvkuAllocationHandler;
  8. type
  9. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. TvkuDebugReporter = class(TvkuAllocHandle)
  11. private
  12. fHandle: VkDebugReportCallbackEXT;
  13. fInstanceCommands: TVkInstanceCommands;
  14. protected
  15. function DebugMessage(
  16. aFlags: VkDebugReportFlagsEXT;
  17. aObjectType: TVkDebugReportObjectTypeEXT;
  18. aObject: VkUint64;
  19. aLocation: VkSize;
  20. aMessageCode: VkInt32;
  21. aLayerPrefix: String;
  22. aMessage: String): Boolean; virtual;
  23. public
  24. property Handle: VkDebugReportCallbackEXT read fHandle;
  25. constructor Create(
  26. const aInstanceCommands: TVkInstanceCommands;
  27. const aAllocHandler: TvkuAllocationHandler = nil);
  28. destructor Destroy; override;
  29. end;
  30. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  31. TvkuConsoleDebugReporter = class(TvkuDebugReporter)
  32. protected
  33. function DebugMessage(aFlags: VkDebugReportFlagsEXT; aObjectType: TVkDebugReportObjectTypeEXT; aObject: VkUint64;
  34. aLocation: VkSize; aMessageCode: VkInt32; aLayerPrefix: String; aMessage: String): Boolean; override;
  35. end;
  36. implementation
  37. uses
  38. uvkuUtils;
  39. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  40. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  41. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  42. function DebugReportCallback(
  43. flags: VkDebugReportFlagsEXT;
  44. objectType: TVkDebugReportObjectTypeEXT;
  45. uObject: VkUint64;
  46. location: VkSize;
  47. messageCode: VkInt32;
  48. pLayerPrefix: PVkChar;
  49. pMessage: PVkChar;
  50. pUserData: PVkVoid): VkBool32; {$IFDEF VK_CDECL}cdecl{$ELSE}stdcall{$ENDIF};
  51. var ret: Boolean;
  52. begin
  53. ret := TvkuDebugReporter(pUserData).DebugMessage(
  54. flags,
  55. objectType,
  56. uObject,
  57. location,
  58. messageCode,
  59. String(pLayerPrefix),
  60. String(pMessage));
  61. if ret
  62. then result := VK_TRUE
  63. else result := VK_FALSE;
  64. end;
  65. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  66. //TvkuDebugReporter//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  67. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  68. function TvkuDebugReporter.DebugMessage(aFlags: VkDebugReportFlagsEXT; aObjectType: TVkDebugReportObjectTypeEXT;
  69. aObject: VkUint64; aLocation: VkSize; aMessageCode: VkInt32; aLayerPrefix: String; aMessage: String): Boolean;
  70. begin
  71. result := true;
  72. end;
  73. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  74. constructor TvkuDebugReporter.Create(
  75. const aInstanceCommands: TVkInstanceCommands;
  76. const aAllocHandler: TvkuAllocationHandler);
  77. var
  78. CreateInfo: TVkDebugReportCallbackCreateInfoEXT;
  79. err: TVkResult;
  80. begin
  81. inherited Create;
  82. if Assigned(aAllocHandler) then
  83. SetAllocCallbacks(aAllocHandler.GetStructure);
  84. fInstanceCommands := aInstanceCommands;
  85. FillByte(CreateInfo, SizeOf(CreateInfo), 0);
  86. CreateInfo.sType := VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
  87. CreateInfo.pNext := nil;
  88. CreateInfo.pfnCallback := @DebugReportCallback;
  89. CreateInfo.pUserData := self;
  90. CreateInfo.flags := [
  91. VK_DEBUG_REPORT_INFORMATION_BIT_EXT,
  92. VK_DEBUG_REPORT_WARNING_BIT_EXT,
  93. VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
  94. VK_DEBUG_REPORT_ERROR_BIT_EXT,
  95. VK_DEBUG_REPORT_DEBUG_BIT_EXT
  96. ];
  97. err := fInstanceCommands.vkCreateDebugReportCallbackEXT(fInstanceCommands.Instance, @CreateInfo, AllocCallbacks, @fHandle);
  98. if (err <> VK_SUCCESS) then
  99. raise TvkuErrorException.Create('unable to create debug report handle', err);
  100. end;
  101. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  102. destructor TvkuDebugReporter.Destroy;
  103. begin
  104. if (fHandle <> VK_INVALID_NDP_HANDLE) then
  105. fInstanceCommands.vkDestroyDebugReportCallbackEXT(fInstanceCommands.Instance, fHandle, AllocCallbacks);
  106. fHandle := VK_INVALID_NDP_HANDLE;
  107. inherited Destroy;
  108. end;
  109. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  110. //TvkuConsoleDebugReporter///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  111. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  112. function TvkuConsoleDebugReporter.DebugMessage(
  113. aFlags: VkDebugReportFlagsEXT; aObjectType: TVkDebugReportObjectTypeEXT;
  114. aObject: VkUint64; aLocation: VkSize; aMessageCode: VkInt32; aLayerPrefix: String; aMessage: String): Boolean;
  115. begin
  116. WriteLn(Format(' DBG - [%03d] %10s: %s', [ Integer(aFlags), aLayerPrefix, aMessage ]));
  117. end;
  118. end.