LiquidBridge.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using UnityEngine.Experimental.Rendering;
  4. using UnityEngine.Rendering;
  5. namespace com.zibra.liquid.Bridge
  6. {
  7. internal static class LiquidBridge
  8. {
  9. #if UNITY_EDITOR
  10. // Editor library selection
  11. #if UNITY_EDITOR_WIN
  12. public const String PluginLibraryName = "ZibraLiquidNative_Win_Editor";
  13. #elif UNITY_EDITOR_OSX
  14. public const String PluginLibraryName = "ZibraLiquidNative_Mac_Editor";
  15. #else
  16. #error Unsupported platform
  17. #endif
  18. #else
  19. // Player library selection
  20. #if UNITY_IOS || UNITY_TVOS
  21. public const String PluginLibraryName = "__Internal";
  22. #elif UNITY_WSA
  23. public const String PluginLibraryName = "ZibraLiquidNative_WSA";
  24. #elif UNITY_STANDALONE_OSX
  25. public const String PluginLibraryName = "ZibraLiquidNative_Mac";
  26. #elif UNITY_STANDALONE_WIN
  27. public const String PluginLibraryName = "ZibraLiquidNative_Win";
  28. #elif UNITY_ANDROID
  29. public const String PluginLibraryName = "ZibraLiquidNative_Android";
  30. #else
  31. #error Unsupported platform
  32. #endif
  33. #endif
  34. #if ZIBRA_EFFECTS_PROFILING_ENABLED
  35. [StructLayout(LayoutKind.Sequential)]
  36. public struct DebugTimestampItem
  37. {
  38. public uint EventType;
  39. public float ExecutionTime;
  40. }
  41. #endif
  42. [DllImport(PluginLibraryName)]
  43. public static extern IntPtr ZibraLiquid_GetRenderEventWithDataFunc();
  44. [DllImport(PluginLibraryName)]
  45. public static extern IntPtr ZibraLiquid_GPUReadbackGetData(Int32 InstanceID, UInt32 size);
  46. [DllImport(PluginLibraryName)]
  47. public static extern Int32 ZibraLiquid_IsLoaded();
  48. [DllImport(PluginLibraryName)]
  49. public static extern void ZibraLiquid_WaitLoad();
  50. [DllImport(PluginLibraryName)]
  51. public static extern Int32 ZibraLiquid_GarbageCollect();
  52. #if ZIBRA_EFFECTS_PROFILING_ENABLED
  53. [DllImport(PluginLibraryName, CallingConvention = CallingConvention.Cdecl)]
  54. public static extern uint ZibraLiquid_GetDebugTimestamps(Int32 InstanceID,
  55. [In, Out] DebugTimestampItem[] timestampsItems);
  56. #endif
  57. #if UNITY_EDITOR
  58. [DllImport(PluginLibraryName)]
  59. public static extern Int32 ZibraLiquid_GetCurrentAffineBufferIndex(Int32 InstanceID);
  60. [DllImport(PluginLibraryName)]
  61. private static extern IntPtr ZibraLiquid_GetHardwareID();
  62. #if !ZIBRA_EFFECTS_NO_LICENSE_CHECK
  63. [DllImport(PluginLibraryName)]
  64. public static extern Int32 ZibraLiquid_GetRandomNumber();
  65. [DllImport(PluginLibraryName)]
  66. public static extern void ZibraLiquid_ValidateLicense([MarshalAs(UnmanagedType.LPStr)] string serverResponse,
  67. Int32 responseSize);
  68. #endif
  69. #endif
  70. public enum EventID : int
  71. {
  72. None = 0,
  73. StepPhysics = 1,
  74. DrawLiquid = 2,
  75. UpdateLiquidParameters = 3,
  76. UpdateManipulatorParameters = 4,
  77. ClearSDFAndID = 5,
  78. CreateFluidInstance = 6,
  79. RegisterParticlesBuffers = 7,
  80. SetCameraParameters = 8,
  81. SetRenderParameters = 9,
  82. RegisterManipulators = 10,
  83. RegisterSolverBuffers = 11,
  84. RegisterRenderResources = 12,
  85. ReleaseResources = 13,
  86. InitializeGpuReadback = 14,
  87. UpdateReadback = 15,
  88. SetCameraParams = 16,
  89. UpdateMeshRenderGlobalParameters = 17,
  90. InitializeGraphicsPipeline = 18,
  91. UpdateSolverParameters = 19,
  92. UpdateSDFObjects = 20,
  93. RenderSDF = 21,
  94. DrawEffectParticles = 22
  95. }
  96. internal struct EventData
  97. {
  98. public int InstanceID;
  99. public IntPtr ExtraData;
  100. };
  101. public enum TextureFormat
  102. {
  103. None,
  104. R8G8B8A8_SNorm,
  105. R16G16B16A16_SFloat,
  106. R32G32B32A32_SFloat,
  107. R16_SFloat,
  108. R32_SFloat,
  109. }
  110. public static TextureFormat ToBridgeTextureFormat(GraphicsFormat format)
  111. {
  112. switch (format)
  113. {
  114. case GraphicsFormat.R8G8B8A8_UNorm:
  115. return TextureFormat.R8G8B8A8_SNorm;
  116. case GraphicsFormat.R16G16B16A16_SFloat:
  117. return TextureFormat.R16G16B16A16_SFloat;
  118. case GraphicsFormat.R32G32B32A32_SFloat:
  119. return TextureFormat.R32G32B32A32_SFloat;
  120. case GraphicsFormat.R16_SFloat:
  121. return TextureFormat.R16_SFloat;
  122. case GraphicsFormat.R32_SFloat:
  123. return TextureFormat.R32_SFloat;
  124. default:
  125. return TextureFormat.None;
  126. }
  127. }
  128. #if UNITY_EDITOR
  129. public static string GetHardwareIDWrapper()
  130. {
  131. IntPtr cstr = ZibraLiquid_GetHardwareID();
  132. string result = Marshal.PtrToStringAnsi(cstr);
  133. return result;
  134. }
  135. #endif
  136. public static int EventAndInstanceID(EventID eventID, int InstanceID)
  137. {
  138. return (int)eventID | (InstanceID << 8);
  139. }
  140. public static void SubmitInstanceEvent(CommandBuffer cmd, int instanceID, EventID eventID,
  141. IntPtr data = default)
  142. {
  143. EventData eventData;
  144. eventData.InstanceID = instanceID;
  145. eventData.ExtraData = data;
  146. IntPtr eventDataNative = Marshal.AllocHGlobal(Marshal.SizeOf(eventData));
  147. Marshal.StructureToPtr(eventData, eventDataNative, true);
  148. cmd.IssuePluginEventAndData(ZibraLiquid_GetRenderEventWithDataFunc(), (int)eventID, eventDataNative);
  149. }
  150. public static bool NeedGarbageCollect()
  151. {
  152. switch (UnityEngine.SystemInfo.graphicsDeviceType)
  153. {
  154. case GraphicsDeviceType.Vulkan:
  155. case GraphicsDeviceType.Direct3D12:
  156. case GraphicsDeviceType.XboxOneD3D12:
  157. case GraphicsDeviceType.GameCoreXboxOne:
  158. case GraphicsDeviceType.GameCoreXboxSeries:
  159. return true;
  160. default:
  161. return false;
  162. }
  163. }
  164. }
  165. }