IUnityRenderingExtensions.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // Unity Native Plugin API copyright © 2015 Unity Technologies ApS
  2. //
  3. // Licensed under the Unity Companion License for Unity - dependent projects--see[Unity Companion License](http://www.unity3d.com/legal/licenses/Unity_Companion_License).
  4. //
  5. // Unless expressly provided otherwise, the Software under this license is made available strictly on an “AS IS” BASIS WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED.Please review the license for details on these and other terms and conditions.
  6. #pragma once
  7. #include "IUnityGraphics.h"
  8. /*
  9. Low-level Native Plugin Rendering Extensions
  10. ============================================
  11. On top of the Low-level native plugin interface, Unity also supports low level rendering extensions that can receive callbacks when certain events happen.
  12. This is mostly used to implement and control low-level rendering in your plugin and enable it to work with Unity’s multithreaded rendering.
  13. Due to the low-level nature of this extension the plugin might need to be preloaded before the devices get created.
  14. Currently the convention is name-based namely the plugin name must be prefixed by “GfxPlugin”. Example: GfxPluginMyFancyNativePlugin.
  15. <code>
  16. // Native plugin code example
  17. enum PluginCustomCommands
  18. {
  19. kPluginCustomCommandDownscale = kUnityRenderingExtUserEventsStart,
  20. kPluginCustomCommandUpscale,
  21. // insert your own events here
  22. kPluginCustomCommandCount
  23. };
  24. static IUnityInterfaces* s_UnityInterfaces = NULL;
  25. extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API
  26. UnityPluginLoad(IUnityInterfaces* unityInterfaces)
  27. {
  28. // initialization code here...
  29. }
  30. extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API
  31. UnityRenderingExtEvent(UnityRenderingExtEventType event, void* data)
  32. {
  33. switch (event)
  34. {
  35. case kUnityRenderingExtEventBeforeDrawCall:
  36. // do some stuff
  37. break;
  38. case kUnityRenderingExtEventAfterDrawCall:
  39. // undo some stuff
  40. break;
  41. case kPluginCustomCommandDownscale:
  42. // downscale some stuff
  43. break;
  44. case kPluginCustomCommandUpscale:
  45. // upscale some stuff
  46. break;
  47. }
  48. }
  49. </code>
  50. */
  51. // These events will be propagated to all plugins that implement void UnityRenderingExtEvent(UnityRenderingExtEventType event, void* data);
  52. typedef enum UnityRenderingExtEventType
  53. {
  54. kUnityRenderingExtEventSetStereoTarget, // issued during SetStereoTarget and carrying the current 'eye' index as parameter
  55. kUnityRenderingExtEventSetStereoEye, // issued during stereo rendering at the beginning of each eye's rendering loop. It carries the current 'eye' index as parameter
  56. kUnityRenderingExtEventStereoRenderingDone, // issued after the rendering has finished
  57. kUnityRenderingExtEventBeforeDrawCall, // issued during BeforeDrawCall and carrying UnityRenderingExtBeforeDrawCallParams as parameter
  58. kUnityRenderingExtEventAfterDrawCall, // issued during AfterDrawCall. This event doesn't carry any parameters
  59. kUnityRenderingExtEventCustomGrab, // issued during GrabIntoRenderTexture since we can't simply copy the resources
  60. // when custom rendering is used - we need to let plugin handle this. It carries over
  61. // a UnityRenderingExtCustomBlitParams params = { X, source, dest, 0, 0 } ( X means it's irrelevant )
  62. kUnityRenderingExtEventCustomBlit, // issued by plugin to insert custom blits. It carries over UnityRenderingExtCustomBlitParams as param.
  63. kUnityRenderingExtEventUpdateTextureBegin, // Deprecated.
  64. kUnityRenderingExtEventUpdateTextureEnd, // Deprecated.
  65. kUnityRenderingExtEventUpdateTextureBeginV1 = kUnityRenderingExtEventUpdateTextureBegin, // Deprecated. Issued to update a texture. It carries over UnityRenderingExtTextureUpdateParamsV1
  66. kUnityRenderingExtEventUpdateTextureEndV1 = kUnityRenderingExtEventUpdateTextureEnd, // Deprecated. Issued to signal the plugin that the texture update has finished. It carries over the same UnityRenderingExtTextureUpdateParamsV1 as kUnityRenderingExtEventUpdateTextureBeginV1
  67. kUnityRenderingExtEventUpdateTextureBeginV2, // Issued to update a texture. It carries over UnityRenderingExtTextureUpdateParamsV2
  68. kUnityRenderingExtEventUpdateTextureEndV2, // Issued to signal the plugin that the texture update has finished. It carries over the same UnityRenderingExtTextureUpdateParamsV2 as kUnityRenderingExtEventUpdateTextureBeginV2
  69. // keep this last
  70. kUnityRenderingExtEventCount,
  71. kUnityRenderingExtUserEventsStart = kUnityRenderingExtEventCount
  72. } UnityRenderingExtEventType;
  73. typedef enum UnityRenderingExtCustomBlitCommands
  74. {
  75. kUnityRenderingExtCustomBlitVRFlush, // This event is mostly used in multi GPU configurations ( SLI, etc ) in order to allow the plugin to flush all GPU's targets
  76. // keep this last
  77. kUnityRenderingExtCustomBlitCount,
  78. kUnityRenderingExtUserCustomBlitStart = kUnityRenderingExtCustomBlitCount
  79. } UnityRenderingExtCustomBlitCommands;
  80. /*
  81. This will be propagated to all plugins implementing UnityRenderingExtQuery.
  82. */
  83. typedef enum UnityRenderingExtQueryType
  84. {
  85. kUnityRenderingExtQueryOverrideViewport = 1 << 0, // The plugin handles setting up the viewport rects. Unity will skip its internal SetViewport calls
  86. kUnityRenderingExtQueryOverrideScissor = 1 << 1, // The plugin handles setting up the scissor rects. Unity will skip its internal SetScissor calls
  87. kUnityRenderingExtQueryOverrideVROcclussionMesh = 1 << 2, // The plugin handles its own VR occlusion mesh rendering. Unity will skip rendering its internal VR occlusion mask
  88. kUnityRenderingExtQueryOverrideVRSinglePass = 1 << 3, // The plugin uses its own single pass stereo technique. Unity will only traverse and render the render node graph once.
  89. // and it will clear the whole render target not just per-eye on demand.
  90. kUnityRenderingExtQueryKeepOriginalDoubleWideWidth_DEPRECATED = 1 << 4, // Instructs unity to keep the original double wide width. By default unity will try and have a power-of-two width for mip-mapping requirements.
  91. kUnityRenderingExtQueryRequestVRFlushCallback = 1 << 5, // Instructs unity to provide callbacks when the VR eye textures need flushing. Useful for multi GPU synchronization.
  92. kUnityRenderingExtQueryOverridePresentFrame = 1 << 6, // The plugin handles it's own SwapChain Present. Unity will skip its internal Present calls
  93. } UnityRenderingExtQueryType;
  94. typedef enum UnityRenderingExtTextureFormat
  95. {
  96. kUnityRenderingExtFormatNone = 0, kUnityRenderingExtFormatFirst = kUnityRenderingExtFormatNone,
  97. // sRGB formats
  98. kUnityRenderingExtFormatR8_SRGB,
  99. kUnityRenderingExtFormatR8G8_SRGB,
  100. kUnityRenderingExtFormatR8G8B8_SRGB,
  101. kUnityRenderingExtFormatR8G8B8A8_SRGB,
  102. // 8 bit integer formats
  103. kUnityRenderingExtFormatR8_UNorm,
  104. kUnityRenderingExtFormatR8G8_UNorm,
  105. kUnityRenderingExtFormatR8G8B8_UNorm,
  106. kUnityRenderingExtFormatR8G8B8A8_UNorm,
  107. kUnityRenderingExtFormatR8_SNorm,
  108. kUnityRenderingExtFormatR8G8_SNorm,
  109. kUnityRenderingExtFormatR8G8B8_SNorm,
  110. kUnityRenderingExtFormatR8G8B8A8_SNorm,
  111. kUnityRenderingExtFormatR8_UInt,
  112. kUnityRenderingExtFormatR8G8_UInt,
  113. kUnityRenderingExtFormatR8G8B8_UInt,
  114. kUnityRenderingExtFormatR8G8B8A8_UInt,
  115. kUnityRenderingExtFormatR8_SInt,
  116. kUnityRenderingExtFormatR8G8_SInt,
  117. kUnityRenderingExtFormatR8G8B8_SInt,
  118. kUnityRenderingExtFormatR8G8B8A8_SInt,
  119. // 16 bit integer formats
  120. kUnityRenderingExtFormatR16_UNorm,
  121. kUnityRenderingExtFormatR16G16_UNorm,
  122. kUnityRenderingExtFormatR16G16B16_UNorm,
  123. kUnityRenderingExtFormatR16G16B16A16_UNorm,
  124. kUnityRenderingExtFormatR16_SNorm,
  125. kUnityRenderingExtFormatR16G16_SNorm,
  126. kUnityRenderingExtFormatR16G16B16_SNorm,
  127. kUnityRenderingExtFormatR16G16B16A16_SNorm,
  128. kUnityRenderingExtFormatR16_UInt,
  129. kUnityRenderingExtFormatR16G16_UInt,
  130. kUnityRenderingExtFormatR16G16B16_UInt,
  131. kUnityRenderingExtFormatR16G16B16A16_UInt,
  132. kUnityRenderingExtFormatR16_SInt,
  133. kUnityRenderingExtFormatR16G16_SInt,
  134. kUnityRenderingExtFormatR16G16B16_SInt,
  135. kUnityRenderingExtFormatR16G16B16A16_SInt,
  136. // 32 bit integer formats
  137. kUnityRenderingExtFormatR32_UInt,
  138. kUnityRenderingExtFormatR32G32_UInt,
  139. kUnityRenderingExtFormatR32G32B32_UInt,
  140. kUnityRenderingExtFormatR32G32B32A32_UInt,
  141. kUnityRenderingExtFormatR32_SInt,
  142. kUnityRenderingExtFormatR32G32_SInt,
  143. kUnityRenderingExtFormatR32G32B32_SInt,
  144. kUnityRenderingExtFormatR32G32B32A32_SInt,
  145. // HDR formats
  146. kUnityRenderingExtFormatR16_SFloat,
  147. kUnityRenderingExtFormatR16G16_SFloat,
  148. kUnityRenderingExtFormatR16G16B16_SFloat,
  149. kUnityRenderingExtFormatR16G16B16A16_SFloat,
  150. kUnityRenderingExtFormatR32_SFloat,
  151. kUnityRenderingExtFormatR32G32_SFloat,
  152. kUnityRenderingExtFormatR32G32B32_SFloat,
  153. kUnityRenderingExtFormatR32G32B32A32_SFloat,
  154. // Luminance and Alpha format
  155. kUnityRenderingExtFormatL8_UNorm,
  156. kUnityRenderingExtFormatA8_UNorm,
  157. kUnityRenderingExtFormatA16_UNorm,
  158. // BGR formats
  159. kUnityRenderingExtFormatB8G8R8_SRGB,
  160. kUnityRenderingExtFormatB8G8R8A8_SRGB,
  161. kUnityRenderingExtFormatB8G8R8_UNorm,
  162. kUnityRenderingExtFormatB8G8R8A8_UNorm,
  163. kUnityRenderingExtFormatB8G8R8_SNorm,
  164. kUnityRenderingExtFormatB8G8R8A8_SNorm,
  165. kUnityRenderingExtFormatB8G8R8_UInt,
  166. kUnityRenderingExtFormatB8G8R8A8_UInt,
  167. kUnityRenderingExtFormatB8G8R8_SInt,
  168. kUnityRenderingExtFormatB8G8R8A8_SInt,
  169. // 16 bit packed formats
  170. kUnityRenderingExtFormatR4G4B4A4_UNormPack16,
  171. kUnityRenderingExtFormatB4G4R4A4_UNormPack16,
  172. kUnityRenderingExtFormatR5G6B5_UNormPack16,
  173. kUnityRenderingExtFormatB5G6R5_UNormPack16,
  174. kUnityRenderingExtFormatR5G5B5A1_UNormPack16,
  175. kUnityRenderingExtFormatB5G5R5A1_UNormPack16,
  176. kUnityRenderingExtFormatA1R5G5B5_UNormPack16,
  177. // Packed formats
  178. kUnityRenderingExtFormatE5B9G9R9_UFloatPack32,
  179. kUnityRenderingExtFormatB10G11R11_UFloatPack32,
  180. kUnityRenderingExtFormatA2B10G10R10_UNormPack32,
  181. kUnityRenderingExtFormatA2B10G10R10_UIntPack32,
  182. kUnityRenderingExtFormatA2B10G10R10_SIntPack32,
  183. kUnityRenderingExtFormatA2R10G10B10_UNormPack32,
  184. kUnityRenderingExtFormatA2R10G10B10_UIntPack32,
  185. kUnityRenderingExtFormatA2R10G10B10_SIntPack32,
  186. kUnityRenderingExtFormatA2R10G10B10_XRSRGBPack32,
  187. kUnityRenderingExtFormatA2R10G10B10_XRUNormPack32,
  188. kUnityRenderingExtFormatR10G10B10_XRSRGBPack32,
  189. kUnityRenderingExtFormatR10G10B10_XRUNormPack32,
  190. kUnityRenderingExtFormatA10R10G10B10_XRSRGBPack32,
  191. kUnityRenderingExtFormatA10R10G10B10_XRUNormPack32,
  192. // ARGB formats... TextureFormat legacy
  193. kUnityRenderingExtFormatA8R8G8B8_SRGB,
  194. kUnityRenderingExtFormatA8R8G8B8_UNorm,
  195. kUnityRenderingExtFormatA32R32G32B32_SFloat,
  196. // Depth Stencil for formats
  197. kUnityRenderingExtFormatD16_UNorm,
  198. kUnityRenderingExtFormatD24_UNorm,
  199. kUnityRenderingExtFormatD24_UNorm_S8_UInt,
  200. kUnityRenderingExtFormatD32_SFloat,
  201. kUnityRenderingExtFormatD32_SFloat_S8_UInt,
  202. kUnityRenderingExtFormatS8_UInt,
  203. // Compression formats
  204. kUnityRenderingExtFormatRGBA_DXT1_SRGB,
  205. kUnityRenderingExtFormatRGBA_DXT1_UNorm,
  206. kUnityRenderingExtFormatRGBA_DXT3_SRGB,
  207. kUnityRenderingExtFormatRGBA_DXT3_UNorm,
  208. kUnityRenderingExtFormatRGBA_DXT5_SRGB,
  209. kUnityRenderingExtFormatRGBA_DXT5_UNorm,
  210. kUnityRenderingExtFormatR_BC4_UNorm,
  211. kUnityRenderingExtFormatR_BC4_SNorm,
  212. kUnityRenderingExtFormatRG_BC5_UNorm,
  213. kUnityRenderingExtFormatRG_BC5_SNorm,
  214. kUnityRenderingExtFormatRGB_BC6H_UFloat,
  215. kUnityRenderingExtFormatRGB_BC6H_SFloat,
  216. kUnityRenderingExtFormatRGBA_BC7_SRGB,
  217. kUnityRenderingExtFormatRGBA_BC7_UNorm,
  218. kUnityRenderingExtFormatRGB_PVRTC_2Bpp_SRGB,
  219. kUnityRenderingExtFormatRGB_PVRTC_2Bpp_UNorm,
  220. kUnityRenderingExtFormatRGB_PVRTC_4Bpp_SRGB,
  221. kUnityRenderingExtFormatRGB_PVRTC_4Bpp_UNorm,
  222. kUnityRenderingExtFormatRGBA_PVRTC_2Bpp_SRGB,
  223. kUnityRenderingExtFormatRGBA_PVRTC_2Bpp_UNorm,
  224. kUnityRenderingExtFormatRGBA_PVRTC_4Bpp_SRGB,
  225. kUnityRenderingExtFormatRGBA_PVRTC_4Bpp_UNorm,
  226. kUnityRenderingExtFormatRGB_ETC_UNorm,
  227. kUnityRenderingExtFormatRGB_ETC2_SRGB,
  228. kUnityRenderingExtFormatRGB_ETC2_UNorm,
  229. kUnityRenderingExtFormatRGB_A1_ETC2_SRGB,
  230. kUnityRenderingExtFormatRGB_A1_ETC2_UNorm,
  231. kUnityRenderingExtFormatRGBA_ETC2_SRGB,
  232. kUnityRenderingExtFormatRGBA_ETC2_UNorm,
  233. kUnityRenderingExtFormatR_EAC_UNorm,
  234. kUnityRenderingExtFormatR_EAC_SNorm,
  235. kUnityRenderingExtFormatRG_EAC_UNorm,
  236. kUnityRenderingExtFormatRG_EAC_SNorm,
  237. kUnityRenderingExtFormatRGBA_ASTC4X4_SRGB,
  238. kUnityRenderingExtFormatRGBA_ASTC4X4_UNorm,
  239. kUnityRenderingExtFormatRGBA_ASTC5X5_SRGB,
  240. kUnityRenderingExtFormatRGBA_ASTC5X5_UNorm,
  241. kUnityRenderingExtFormatRGBA_ASTC6X6_SRGB,
  242. kUnityRenderingExtFormatRGBA_ASTC6X6_UNorm,
  243. kUnityRenderingExtFormatRGBA_ASTC8X8_SRGB,
  244. kUnityRenderingExtFormatRGBA_ASTC8X8_UNorm,
  245. kUnityRenderingExtFormatRGBA_ASTC10X10_SRGB,
  246. kUnityRenderingExtFormatRGBA_ASTC10X10_UNorm,
  247. kUnityRenderingExtFormatRGBA_ASTC12X12_SRGB,
  248. kUnityRenderingExtFormatRGBA_ASTC12X12_UNorm,
  249. // Video formats
  250. kUnityRenderingExtFormatYUV2,
  251. // Automatic formats, back-end decides
  252. kUnityRenderingExtFormatDepthAuto_removed_donotuse,
  253. kUnityRenderingExtFormatShadowAuto_removed_donotuse,
  254. kUnityRenderingExtFormatVideoAuto_removed_donotuse,
  255. // ASTC hdr profile
  256. kUnityRenderingExtFormatRGBA_ASTC4X4_UFloat,
  257. kUnityRenderingExtFormatRGBA_ASTC5X5_UFloat,
  258. kUnityRenderingExtFormatRGBA_ASTC6X6_UFloat,
  259. kUnityRenderingExtFormatRGBA_ASTC8X8_UFloat,
  260. kUnityRenderingExtFormatRGBA_ASTC10X10_UFloat,
  261. kUnityRenderingExtFormatRGBA_ASTC12X12_UFloat,
  262. kUnityRenderingExtFormatLast = kUnityRenderingExtFormatRGBA_ASTC12X12_UFloat, // Remove?
  263. } UnityRenderingExtTextureFormat;
  264. typedef struct UnityRenderingExtBeforeDrawCallParams
  265. {
  266. void* vertexShader; // bound vertex shader (platform dependent)
  267. void* fragmentShader; // bound fragment shader (platform dependent)
  268. void* geometryShader; // bound geometry shader (platform dependent)
  269. void* hullShader; // bound hull shader (platform dependent)
  270. void* domainShader; // bound domain shader (platform dependent)
  271. int eyeIndex; // the index of the current stereo "eye" being currently rendered.
  272. } UnityRenderingExtBeforeDrawCallParams;
  273. typedef struct UnityRenderingExtCustomBlitParams
  274. {
  275. UnityTextureID source; // source texture
  276. UnityRenderBuffer destination; // destination surface
  277. unsigned int command; // command for the custom blit - could be any UnityRenderingExtCustomBlitCommands command or custom ones.
  278. unsigned int commandParam; // custom parameters for the command
  279. unsigned int commandFlags; // custom flags for the command
  280. } UnityRenderingExtCustomBlitParams;
  281. // Deprecated. Use UnityRenderingExtTextureUpdateParamsV2 and CommandBuffer.IssuePluginCustomTextureUpdateV2 instead.
  282. // Only supports DX11, GLES, Metal
  283. typedef struct UnityRenderingExtTextureUpdateParamsV1
  284. {
  285. void* texData; // source data for the texture update. Must be set by the plugin
  286. unsigned int userData; // user defined data. Set by the plugin
  287. unsigned int textureID; // texture ID of the texture to be updated.
  288. UnityRenderingExtTextureFormat format; // format of the texture to be updated
  289. unsigned int width; // width of the texture
  290. unsigned int height; // height of the texture
  291. unsigned int bpp; // texture bytes per pixel.
  292. } UnityRenderingExtTextureUpdateParamsV1;
  293. // Deprecated. Use UnityRenderingExtTextureUpdateParamsV2 and CommandBuffer.IssuePluginCustomTextureUpdateV2 instead.
  294. // Only supports DX11, GLES, Metal
  295. typedef UnityRenderingExtTextureUpdateParamsV1 UnityRenderingExtTextureUpdateParams;
  296. // Type of the "data" parameter passed when callbacks registered with CommandBuffer.IssuePluginCustomTextureUpdateV2 are called.
  297. // Supports DX11, GLES, Metal, and Switch (also possibly PS4, PSVita in the future)
  298. typedef struct UnityRenderingExtTextureUpdateParamsV2
  299. {
  300. void* texData; // source data for the texture update. Must be set by the plugin
  301. intptr_t textureID; // texture ID of the texture to be updated.
  302. unsigned int userData; // user defined data. Set by the plugin
  303. UnityRenderingExtTextureFormat format; // format of the texture to be updated
  304. unsigned int width; // width of the texture
  305. unsigned int height; // height of the texture
  306. unsigned int bpp; // texture bytes per pixel.
  307. } UnityRenderingExtTextureUpdateParamsV2;
  308. // Certain Unity APIs (GL.IssuePluginEventAndData, CommandBuffer.IssuePluginEventAndData) can callback into native plugins.
  309. // Provide them with an address to a function of this signature.
  310. typedef void (UNITY_INTERFACE_API * UnityRenderingEventAndData)(int eventId, void* data);
  311. #ifdef __cplusplus
  312. extern "C" {
  313. #endif
  314. // If exported by a plugin, this function will be called for all the events in UnityRenderingExtEventType
  315. void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityRenderingExtEvent(UnityRenderingExtEventType event, void* data);
  316. // If exported by a plugin, this function will be called to query the plugin for the queries in UnityRenderingExtQueryType
  317. bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityRenderingExtQuery(UnityRenderingExtQueryType query);
  318. #ifdef __cplusplus
  319. }
  320. #endif