Nessuna descrizione
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.

UnityInstancing.hlsl 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. #ifndef UNITY_INSTANCING_INCLUDED
  2. #define UNITY_INSTANCING_INCLUDED
  3. #if SHADER_TARGET >= 35 && (defined(SHADER_API_D3D11) || defined(SHADER_API_GLES3) || defined(SHADER_API_GLCORE) || defined(SHADER_API_XBOXONE) || defined(SHADER_API_GAMECORE) || defined(SHADER_API_PSSL) || defined(SHADER_API_VULKAN) || defined(SHADER_API_METAL) || defined(SHADER_API_WEBGPU))
  4. #define UNITY_SUPPORT_INSTANCING
  5. #endif
  6. #if defined(SHADER_API_SWITCH)
  7. #define UNITY_SUPPORT_INSTANCING
  8. #endif
  9. #if defined(SHADER_API_D3D11) || defined(SHADER_API_GLCORE) || defined(SHADER_API_GLES3) || defined(SHADER_API_VULKAN) || (defined(SHADER_API_METAL) && !defined(UNITY_COMPILER_DXC))
  10. #define UNITY_SUPPORT_STEREO_INSTANCING
  11. #endif
  12. // These platforms support dynamically adjusting the instancing CB size according to the current batch.
  13. #if defined(SHADER_API_D3D11) || defined(SHADER_API_GLCORE) || defined(SHADER_API_GLES3) || defined(SHADER_API_METAL) || defined(SHADER_API_PSSL) || defined(SHADER_API_VULKAN) || defined(SHADER_API_SWITCH) || defined(SHADER_API_WEBGPU)
  14. #define UNITY_INSTANCING_SUPPORT_FLEXIBLE_ARRAY_SIZE
  15. #endif
  16. #if defined(SHADER_TARGET_SURFACE_ANALYSIS) && defined(UNITY_SUPPORT_INSTANCING)
  17. #undef UNITY_SUPPORT_INSTANCING
  18. #endif
  19. ////////////////////////////////////////////////////////
  20. // instancing paths
  21. // - UNITY_INSTANCING_ENABLED Defined if instancing path is taken.
  22. // - UNITY_PROCEDURAL_INSTANCING_ENABLED Defined if procedural instancing path is taken.
  23. // - UNITY_STEREO_INSTANCING_ENABLED Defined if stereo instancing path is taken.
  24. // - UNITY_ANY_INSTANCING_ENABLED Defined if any instancing path is taken
  25. #if defined(UNITY_SUPPORT_INSTANCING) && defined(INSTANCING_ON)
  26. #define UNITY_INSTANCING_ENABLED
  27. #endif
  28. #if defined(UNITY_SUPPORT_INSTANCING) && defined(PROCEDURAL_INSTANCING_ON)
  29. #define UNITY_PROCEDURAL_INSTANCING_ENABLED
  30. #endif
  31. #if defined(UNITY_SUPPORT_INSTANCING) && defined(DOTS_INSTANCING_ON)
  32. #define UNITY_DOTS_INSTANCING_ENABLED
  33. // On GL & GLES, use UBO path, on every other platform use SSBO path (including Switch, even if it defines SHADER_API_GLCORE)
  34. #if (defined(SHADER_API_GLCORE) || defined(SHADER_API_GLES3)) && (!defined(SHADER_API_SWITCH))
  35. #define UNITY_DOTS_INSTANCING_UNIFORM_BUFFER
  36. #endif
  37. #endif
  38. #if defined(UNITY_SUPPORT_STEREO_INSTANCING) && defined(STEREO_INSTANCING_ON)
  39. #define UNITY_STEREO_INSTANCING_ENABLED
  40. #endif
  41. #if defined(UNITY_INSTANCING_ENABLED) || defined(UNITY_PROCEDURAL_INSTANCING_ENABLED) || defined(UNITY_DOTS_INSTANCING_ENABLED) || defined(UNITY_STEREO_INSTANCING_ENABLED)
  42. #define UNITY_ANY_INSTANCING_ENABLED 1
  43. #else
  44. #define UNITY_ANY_INSTANCING_ENABLED 0
  45. #endif
  46. #if defined(DOTS_INSTANCING_ON)
  47. #if defined(UNITY_DOTS_INSTANCING_UNIFORM_BUFFER)
  48. #if (SHADER_TARGET < 35)
  49. #error The DOTS_INSTANCING_ON keyword requires shader model 3.5 or greater ("#pragma target 3.5" or greater) on OpenGL. Make sure to use target 3.5 or greater in all SubShaders or variants that use DOTS_INSTANCING_ON, and to NOT use DOTS_INSTANCING_ON in any SubShaders that must use a lower target version.
  50. #endif
  51. #else
  52. // DOTS_INSTANCING_ON requires SM4.5 on D3D11, but we skip issuing this error for SM3.5 as a workaround to d3d11 being enabled
  53. // for SM2.0/SM3.5 subshaders in URP.
  54. #if (defined(SHADER_API_D3D11) && (SHADER_TARGET < 35)) || (!defined(SHADER_API_D3D11) && (SHADER_TARGET < 45))
  55. #error The DOTS_INSTANCING_ON keyword requires shader model 4.5 or greater ("#pragma target 4.5" or greater). Make sure to use target 4.5 or greater in all SubShaders or variants that use DOTS_INSTANCING_ON, and to NOT use DOTS_INSTANCING_ON in any SubShaders that must use a lower target version.
  56. #endif
  57. #endif
  58. #endif
  59. #if defined(SHADER_API_GLES3) || defined(SHADER_API_GLCORE) || defined(SHADER_API_METAL) || defined(SHADER_API_VULKAN) || defined(SHADER_API_WEBGPU)
  60. // These platforms have constant buffers disabled normally, but not here (see CBUFFER_START/CBUFFER_END in HLSLSupport.cginc).
  61. #define UNITY_INSTANCING_CBUFFER_SCOPE_BEGIN(name) cbuffer name {
  62. #define UNITY_INSTANCING_CBUFFER_SCOPE_END }
  63. #else
  64. #define UNITY_INSTANCING_CBUFFER_SCOPE_BEGIN(name) CBUFFER_START(name)
  65. #define UNITY_INSTANCING_CBUFFER_SCOPE_END CBUFFER_END
  66. #endif
  67. ////////////////////////////////////////////////////////
  68. // basic instancing setups
  69. // - UNITY_VERTEX_INPUT_INSTANCE_ID Declare instance ID field in vertex shader input / output struct.
  70. // - UNITY_GET_INSTANCE_ID (Internal) Get the instance ID from input struct.
  71. #if UNITY_ANY_INSTANCING_ENABLED
  72. // A global instance ID variable that functions can directly access.
  73. static uint unity_InstanceID;
  74. // Don't make UnityDrawCallInfo an actual CB on GL
  75. #if !defined(SHADER_API_GLES3) && !defined(SHADER_API_GLCORE)
  76. UNITY_INSTANCING_CBUFFER_SCOPE_BEGIN(UnityDrawCallInfo)
  77. #endif
  78. int unity_BaseInstanceID;
  79. int unity_InstanceCount;
  80. #if !defined(SHADER_API_GLES3) && !defined(SHADER_API_GLCORE)
  81. UNITY_INSTANCING_CBUFFER_SCOPE_END
  82. #endif
  83. #if defined(SHADER_STAGE_RAY_TRACING)
  84. #define DEFAULT_UNITY_VERTEX_INPUT_INSTANCE_ID uint instanceID;
  85. #define UNITY_GET_INSTANCE_ID(input) input.instanceID
  86. #else
  87. #ifdef SHADER_API_PSSL
  88. #define DEFAULT_UNITY_VERTEX_INPUT_INSTANCE_ID uint instanceID;
  89. #define UNITY_GET_INSTANCE_ID(input) _GETINSTANCEID(input)
  90. #else
  91. #define DEFAULT_UNITY_VERTEX_INPUT_INSTANCE_ID uint instanceID : SV_InstanceID;
  92. #define UNITY_GET_INSTANCE_ID(input) input.instanceID
  93. #endif
  94. #endif
  95. #else
  96. #define DEFAULT_UNITY_VERTEX_INPUT_INSTANCE_ID
  97. #endif // UNITY_INSTANCING_ENABLED || UNITY_PROCEDURAL_INSTANCING_ENABLED || UNITY_STEREO_INSTANCING_ENABLED
  98. #if !defined(UNITY_VERTEX_INPUT_INSTANCE_ID)
  99. # define UNITY_VERTEX_INPUT_INSTANCE_ID DEFAULT_UNITY_VERTEX_INPUT_INSTANCE_ID
  100. #endif
  101. ////////////////////////////////////////////////////////
  102. // basic stereo instancing setups
  103. // - UNITY_VERTEX_OUTPUT_STEREO Declare stereo target eye field in vertex shader output struct.
  104. // - UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO Assign the stereo target eye.
  105. // - UNITY_TRANSFER_VERTEX_OUTPUT_STEREO Copy stero target from input struct to output struct. Used in vertex shader.
  106. // - UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX
  107. #ifdef UNITY_STEREO_INSTANCING_ENABLED
  108. #if defined(SHADER_API_GLES3) || defined(SHADER_API_GLCORE)
  109. #define DEFAULT_UNITY_VERTEX_OUTPUT_STEREO uint stereoTargetEyeIndexAsRTArrayIdx : SV_RenderTargetArrayIndex; uint stereoTargetEyeIndexAsBlendIdx0 : BLENDINDICES0;
  110. #define DEFAULT_UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output) output.stereoTargetEyeIndexAsRTArrayIdx = unity_StereoEyeIndex; output.stereoTargetEyeIndexAsBlendIdx0 = unity_StereoEyeIndex;
  111. #define DEFAULT_UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(input, output) output.stereoTargetEyeIndexAsBlendIdx0 = input.stereoTargetEyeIndexAsBlendIdx0;
  112. #define DEFAULT_UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input) unity_StereoEyeIndex = input.stereoTargetEyeIndexAsBlendIdx0;
  113. #elif defined(SHADER_API_PSSL) && defined(TESSELLATION_ON)
  114. // Use of SV_RenderTargetArrayIndex is a little more complicated if we have tessellation stages involved
  115. // This will add an extra instructions which we might be able to optimize away in some stages if we are careful.
  116. #if defined(SHADER_STAGE_VERTEX)
  117. #define DEFAULT_UNITY_VERTEX_OUTPUT_STEREO uint stereoTargetEyeIndexAsBlendIdx0 : BLENDINDICES0;
  118. #define DEFAULT_UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output) output.stereoTargetEyeIndexAsBlendIdx0 = unity_StereoEyeIndex;
  119. #define DEFAULT_UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(input, output) output.stereoTargetEyeIndexAsBlendIdx0 = input.stereoTargetEyeIndexAsBlendIdx0;
  120. #define DEFAULT_UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input) unity_StereoEyeIndex = input.stereoTargetEyeIndexAsBlendIdx0;
  121. #else
  122. #define DEFAULT_UNITY_VERTEX_OUTPUT_STEREO uint stereoTargetEyeIndexAsRTArrayIdx : SV_RenderTargetArrayIndex; uint stereoTargetEyeIndexAsBlendIdx0 : BLENDINDICES0;
  123. #define DEFAULT_UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output) output.stereoTargetEyeIndexAsRTArrayIdx = unity_StereoEyeIndex; output.stereoTargetEyeIndexAsBlendIdx0 = unity_StereoEyeIndex;
  124. #define DEFAULT_UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(input, output) output.stereoTargetEyeIndexAsBlendIdx0 = input.stereoTargetEyeIndexAsBlendIdx0;
  125. #define DEFAULT_UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input) unity_StereoEyeIndex = input.stereoTargetEyeIndexAsBlendIdx0;
  126. #endif
  127. #else
  128. #define DEFAULT_UNITY_VERTEX_OUTPUT_STEREO uint stereoTargetEyeIndexAsRTArrayIdx : SV_RenderTargetArrayIndex;
  129. #define DEFAULT_UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output) output.stereoTargetEyeIndexAsRTArrayIdx = unity_StereoEyeIndex
  130. #define DEFAULT_UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(input, output) output.stereoTargetEyeIndexAsRTArrayIdx = input.stereoTargetEyeIndexAsRTArrayIdx;
  131. #define DEFAULT_UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input) unity_StereoEyeIndex = input.stereoTargetEyeIndexAsRTArrayIdx;
  132. #endif
  133. #elif defined(UNITY_STEREO_MULTIVIEW_ENABLED)
  134. #define DEFAULT_UNITY_VERTEX_OUTPUT_STEREO float stereoTargetEyeIndexAsBlendIdx0 : BLENDWEIGHT0;
  135. #define DEFAULT_UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output) output.stereoTargetEyeIndexAsBlendIdx0 = unity_StereoEyeIndex;
  136. #define DEFAULT_UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(input, output) output.stereoTargetEyeIndexAsBlendIdx0 = input.stereoTargetEyeIndexAsBlendIdx0;
  137. #if defined(SHADER_STAGE_VERTEX)
  138. #define DEFAULT_UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input)
  139. #else
  140. #define DEFAULT_UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input) unity_StereoEyeIndex = (uint) input.stereoTargetEyeIndexAsBlendIdx0;
  141. #endif
  142. #else
  143. #define DEFAULT_UNITY_VERTEX_OUTPUT_STEREO
  144. #define DEFAULT_UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output)
  145. #define DEFAULT_UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(input, output)
  146. #define DEFAULT_UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input)
  147. #endif
  148. #if !defined(UNITY_VERTEX_OUTPUT_STEREO)
  149. # define UNITY_VERTEX_OUTPUT_STEREO DEFAULT_UNITY_VERTEX_OUTPUT_STEREO
  150. #endif
  151. #if !defined(UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO)
  152. # define UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output) DEFAULT_UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output)
  153. #endif
  154. #if !defined(UNITY_TRANSFER_VERTEX_OUTPUT_STEREO)
  155. # define UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(input, output) DEFAULT_UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(input, output)
  156. #endif
  157. #if !defined(UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX)
  158. # define UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input) DEFAULT_UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input)
  159. #endif
  160. ////////////////////////////////////////////////////////
  161. // - UNITY_SETUP_INSTANCE_ID Should be used at the very beginning of the vertex shader / fragment shader / ray tracing hit shaders,
  162. // so that succeeding code can have access to the global unity_InstanceID.
  163. // Also procedural function is called to setup instance data.
  164. // - UNITY_TRANSFER_INSTANCE_ID Copy instance ID from input struct to output struct. Used in vertex shader.
  165. #if UNITY_ANY_INSTANCING_ENABLED
  166. void UnitySetupInstanceID(uint inputInstanceID)
  167. {
  168. #if defined(UNITY_SUPPORT_INSTANCING) && defined(DOTS_INSTANCING_ON)
  169. const int localBaseInstanceId = 0; // base instance id is always 0 in BRG (avoid using useless UnityDrawCallInfo cbuffer)
  170. #else
  171. const int localBaseInstanceId = unity_BaseInstanceID;
  172. #endif
  173. #ifdef UNITY_STEREO_INSTANCING_ENABLED
  174. #if !defined(SHADEROPTIONS_XR_MAX_VIEWS) || SHADEROPTIONS_XR_MAX_VIEWS <= 2
  175. #if defined(SHADER_API_GLES3)
  176. // We must calculate the stereo eye index differently for GLES3
  177. // because otherwise, the unity shader compiler will emit a bitfieldInsert function.
  178. // bitfieldInsert requires support for glsl version 400 or later. Therefore the
  179. // generated glsl code will fail to compile on lower end devices. By changing the
  180. // way we calculate the stereo eye index, we can help the shader compiler to avoid
  181. // emitting the bitfieldInsert function and thereby increase the number of devices we
  182. // can run stereo instancing on.
  183. unity_StereoEyeIndex = round(fmod(inputInstanceID, 2.0));
  184. unity_InstanceID = localBaseInstanceId + (inputInstanceID >> 1);
  185. #else
  186. // stereo eye index is automatically figured out from the instance ID
  187. unity_StereoEyeIndex = inputInstanceID & 0x01;
  188. unity_InstanceID = localBaseInstanceId + (inputInstanceID >> 1);
  189. #endif
  190. #else
  191. unity_StereoEyeIndex = inputInstanceID % _XRViewCount;
  192. unity_InstanceID = localBaseInstanceId + (inputInstanceID / _XRViewCount);
  193. #endif
  194. #elif defined(SHADER_STAGE_RAY_TRACING)
  195. // InstanceIndex() intrinsic is the global ray tracing instance index in the TLAS and unity_BaseInstanceID is where the array of instances starts in the TLAS
  196. unity_InstanceID = InstanceIndex() - localBaseInstanceId;
  197. #else
  198. unity_InstanceID = inputInstanceID + localBaseInstanceId;
  199. #endif
  200. }
  201. #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
  202. #ifndef UNITY_INSTANCING_PROCEDURAL_FUNC
  203. #error "UNITY_INSTANCING_PROCEDURAL_FUNC must be defined."
  204. #else
  205. void UNITY_INSTANCING_PROCEDURAL_FUNC(); // forward declaration of the procedural function
  206. #define DEFAULT_UNITY_SETUP_INSTANCE_ID(input) { UnitySetupInstanceID(UNITY_GET_INSTANCE_ID(input)); UNITY_INSTANCING_PROCEDURAL_FUNC();}
  207. #endif
  208. #elif defined(SHADER_STAGE_RAY_TRACING)
  209. #define DEFAULT_UNITY_SETUP_INSTANCE_ID { UnitySetupInstanceID(0);}
  210. #else
  211. #define DEFAULT_UNITY_SETUP_INSTANCE_ID(input) { UnitySetupInstanceID(UNITY_GET_INSTANCE_ID(input));}
  212. #endif
  213. #define UNITY_TRANSFER_INSTANCE_ID(input, output) output.instanceID = UNITY_GET_INSTANCE_ID(input)
  214. #elif defined(SHADER_STAGE_RAY_TRACING)
  215. #define DEFAULT_UNITY_SETUP_INSTANCE_ID
  216. #define UNITY_TRANSFER_INSTANCE_ID(input, output)
  217. #else
  218. #define DEFAULT_UNITY_SETUP_INSTANCE_ID(input)
  219. #define UNITY_TRANSFER_INSTANCE_ID(input, output)
  220. #endif
  221. #if !defined(UNITY_SETUP_INSTANCE_ID)
  222. #if defined(SHADER_STAGE_RAY_TRACING)
  223. #define UNITY_SETUP_INSTANCE_ID DEFAULT_UNITY_SETUP_INSTANCE_ID
  224. #else
  225. #define UNITY_SETUP_INSTANCE_ID(input) DEFAULT_UNITY_SETUP_INSTANCE_ID(input)
  226. #endif
  227. #endif
  228. ////////////////////////////////////////////////////////
  229. // instanced property arrays
  230. #if defined(UNITY_INSTANCING_ENABLED) || defined(UNITY_DOTS_INSTANCING_ENABLED)
  231. #ifdef UNITY_FORCE_MAX_INSTANCE_COUNT
  232. #define UNITY_INSTANCED_ARRAY_SIZE UNITY_FORCE_MAX_INSTANCE_COUNT
  233. #elif defined(UNITY_INSTANCING_SUPPORT_FLEXIBLE_ARRAY_SIZE)
  234. #define UNITY_INSTANCED_ARRAY_SIZE 2 // minimum array size that ensures dynamic indexing
  235. #elif defined(UNITY_MAX_INSTANCE_COUNT)
  236. #define UNITY_INSTANCED_ARRAY_SIZE UNITY_MAX_INSTANCE_COUNT
  237. #else
  238. #if (defined(SHADER_API_VULKAN) && defined(SHADER_API_MOBILE)) || defined(SHADER_API_SWITCH) || defined(SHADER_API_WEBGPU)
  239. #define UNITY_INSTANCED_ARRAY_SIZE 250
  240. #else
  241. #define UNITY_INSTANCED_ARRAY_SIZE 500
  242. #endif
  243. #endif
  244. #if defined(UNITY_DOTS_INSTANCING_ENABLED)
  245. #define UNITY_INSTANCING_BUFFER_START(buf) UNITY_INSTANCING_CBUFFER_SCOPE_BEGIN(UnityInstancing_##buf)
  246. #define UNITY_INSTANCING_BUFFER_END(arr) UNITY_INSTANCING_CBUFFER_SCOPE_END
  247. #define UNITY_DEFINE_INSTANCED_PROP(type, var) type var;
  248. #define UNITY_ACCESS_INSTANCED_PROP(arr, var) var
  249. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityDOTSInstancing.hlsl"
  250. #if defined(UNITY_SETUP_INSTANCE_ID)
  251. #undef UNITY_SETUP_INSTANCE_ID
  252. #define UNITY_SETUP_INSTANCE_ID(input) {\
  253. DEFAULT_UNITY_SETUP_INSTANCE_ID(input);\
  254. SetupDOTSVisibleInstancingData();\
  255. UNITY_SETUP_DOTS_MATERIAL_PROPERTY_CACHES();\
  256. UNITY_SETUP_DOTS_SH_COEFFS;\
  257. UNITY_SETUP_DOTS_RENDER_BOUNDS; }
  258. #endif
  259. #else
  260. #if defined(SHADER_STAGE_RAY_TRACING)
  261. #define UNITY_INSTANCING_BUFFER_START(buf)
  262. #define UNITY_INSTANCING_BUFFER_END(arr)
  263. #define UNITY_DEFINE_INSTANCED_PROP(type, var) StructuredBuffer<type> var;
  264. #define UNITY_ACCESS_INSTANCED_PROP(arr, var) var[unity_InstanceID]
  265. #else
  266. #define UNITY_INSTANCING_BUFFER_START(buf) UNITY_INSTANCING_CBUFFER_SCOPE_BEGIN(UnityInstancing_##buf) struct {
  267. #define UNITY_INSTANCING_BUFFER_END(arr) } arr##Array[UNITY_INSTANCED_ARRAY_SIZE]; UNITY_INSTANCING_CBUFFER_SCOPE_END
  268. #define UNITY_DEFINE_INSTANCED_PROP(type, var) type var;
  269. #define UNITY_ACCESS_INSTANCED_PROP(arr, var) arr##Array[unity_InstanceID].var
  270. #endif
  271. #define UNITY_DOTS_INSTANCING_START(name)
  272. #define UNITY_DOTS_INSTANCING_END(name)
  273. #define UNITY_DOTS_INSTANCED_PROP(type, name)
  274. #define UNITY_ACCESS_DOTS_INSTANCED_PROP(type, var) var
  275. #define UNITY_ACCESS_DOTS_AND_TRADITIONAL_INSTANCED_PROP(type, arr, var) UNITY_ACCESS_INSTANCED_PROP(arr, var)
  276. #define UNITY_ACCESS_DOTS_INSTANCED_PROP_WITH_DEFAULT(type, var) var
  277. #define UNITY_ACCESS_DOTS_AND_TRADITIONAL_INSTANCED_PROP_WITH_DEFAULT(type, arr, var) UNITY_ACCESS_INSTANCED_PROP(arr, var)
  278. #define UNITY_ACCESS_DOTS_INSTANCED_PROP_WITH_CUSTOM_DEFAULT(type, var, default_value) var
  279. #define UNITY_ACCESS_DOTS_AND_TRADITIONAL_INSTANCED_PROP_WITH_CUSTOM_DEFAULT(type, arr, var, default_value) UNITY_ACCESS_INSTANCED_PROP(arr, var)
  280. #endif
  281. // Put worldToObject array to a separate CB if UNITY_ASSUME_UNIFORM_SCALING is defined. Most of the time it will not be used.
  282. #ifdef UNITY_ASSUME_UNIFORM_SCALING
  283. #define UNITY_WORLDTOOBJECTARRAY_CB 1
  284. #else
  285. #define UNITY_WORLDTOOBJECTARRAY_CB 0
  286. #endif
  287. #if defined(UNITY_INSTANCED_LOD_FADE) && (defined(LOD_FADE_PERCENTAGE) || defined(LOD_FADE_CROSSFADE))
  288. #define UNITY_USE_LODFADE_ARRAY
  289. #endif
  290. #if defined(UNITY_INSTANCED_RENDERING_LAYER)
  291. #define UNITY_USE_RENDERINGLAYER_ARRAY
  292. #endif
  293. #ifdef UNITY_INSTANCED_LIGHTMAPSTS
  294. #ifdef LIGHTMAP_ON
  295. #define UNITY_USE_LIGHTMAPST_ARRAY
  296. #endif
  297. #ifdef DYNAMICLIGHTMAP_ON
  298. #define UNITY_USE_DYNAMICLIGHTMAPST_ARRAY
  299. #endif
  300. #endif
  301. #if defined(UNITY_INSTANCED_RENDERER_BOUNDS)
  302. #define UNITY_USE_RENDERER_BOUNDS
  303. #endif
  304. #if defined(UNITY_INSTANCED_SH) && !defined(LIGHTMAP_ON)
  305. #if !defined(DYNAMICLIGHTMAP_ON)
  306. #define UNITY_USE_SHCOEFFS_ARRAYS
  307. #endif
  308. #if defined(SHADOWS_SHADOWMASK)
  309. #define UNITY_USE_PROBESOCCLUSION_ARRAY
  310. #endif
  311. #endif
  312. #if !defined(UNITY_DOTS_INSTANCING_ENABLED)
  313. UNITY_INSTANCING_BUFFER_START(PerDraw0)
  314. #ifndef UNITY_DONT_INSTANCE_OBJECT_MATRICES
  315. UNITY_DEFINE_INSTANCED_PROP(float4x4, unity_ObjectToWorldArray)
  316. #if UNITY_WORLDTOOBJECTARRAY_CB == 0
  317. UNITY_DEFINE_INSTANCED_PROP(float4x4, unity_WorldToObjectArray)
  318. #endif
  319. #endif
  320. #if defined(UNITY_USE_LODFADE_ARRAY) && defined(UNITY_INSTANCING_SUPPORT_FLEXIBLE_ARRAY_SIZE)
  321. UNITY_DEFINE_INSTANCED_PROP(float2, unity_LODFadeArray)
  322. #define unity_LODFade UNITY_ACCESS_INSTANCED_PROP(unity_Builtins0, unity_LODFadeArray).xyxx
  323. #endif
  324. #if defined(UNITY_USE_RENDERINGLAYER_ARRAY) && defined(UNITY_INSTANCING_SUPPORT_FLEXIBLE_ARRAY_SIZE)
  325. UNITY_DEFINE_INSTANCED_PROP(float, unity_RenderingLayerArray)
  326. #define unity_RenderingLayer UNITY_ACCESS_INSTANCED_PROP(unity_Builtins0, unity_RenderingLayerArray).xxxx
  327. #endif
  328. UNITY_INSTANCING_BUFFER_END(unity_Builtins0)
  329. UNITY_INSTANCING_BUFFER_START(PerDraw1)
  330. #if !defined(UNITY_DONT_INSTANCE_OBJECT_MATRICES) && UNITY_WORLDTOOBJECTARRAY_CB == 1
  331. UNITY_DEFINE_INSTANCED_PROP(float4x4, unity_WorldToObjectArray)
  332. #endif
  333. #if defined(UNITY_USE_LODFADE_ARRAY) && !defined(UNITY_INSTANCING_SUPPORT_FLEXIBLE_ARRAY_SIZE)
  334. UNITY_DEFINE_INSTANCED_PROP(float2, unity_LODFadeArray)
  335. #define unity_LODFade UNITY_ACCESS_INSTANCED_PROP(unity_Builtins1, unity_LODFadeArray).xyxx
  336. #endif
  337. #if defined(UNITY_USE_RENDERINGLAYER_ARRAY) && !defined(UNITY_INSTANCING_SUPPORT_FLEXIBLE_ARRAY_SIZE)
  338. UNITY_DEFINE_INSTANCED_PROP(float, unity_RenderingLayerArray)
  339. #define unity_RenderingLayer UNITY_ACCESS_INSTANCED_PROP(unity_Builtins1, unity_RenderingLayerArray).xxxx
  340. #endif
  341. #if defined(UNITY_USE_RENDERER_BOUNDS)
  342. UNITY_DEFINE_INSTANCED_PROP(float4, unity_RendererBounds_MinArray)
  343. UNITY_DEFINE_INSTANCED_PROP(float4, unity_RendererBounds_MaxArray)
  344. #define unity_RendererBounds_Min UNITY_ACCESS_INSTANCED_PROP(unity_Builtins1, unity_RendererBounds_MinArray)
  345. #define unity_RendererBounds_Max UNITY_ACCESS_INSTANCED_PROP(unity_Builtins1, unity_RendererBounds_MaxArray)
  346. #endif
  347. UNITY_INSTANCING_BUFFER_END(unity_Builtins1)
  348. UNITY_INSTANCING_BUFFER_START(PerDraw2)
  349. #ifdef UNITY_USE_LIGHTMAPST_ARRAY
  350. UNITY_DEFINE_INSTANCED_PROP(float4, unity_LightmapSTArray)
  351. UNITY_DEFINE_INSTANCED_PROP(float4, unity_LightmapIndexArray)
  352. #define unity_LightmapST UNITY_ACCESS_INSTANCED_PROP(unity_Builtins2, unity_LightmapSTArray)
  353. #endif
  354. #ifdef UNITY_USE_DYNAMICLIGHTMAPST_ARRAY
  355. UNITY_DEFINE_INSTANCED_PROP(float4, unity_DynamicLightmapSTArray)
  356. #define unity_DynamicLightmapST UNITY_ACCESS_INSTANCED_PROP(unity_Builtins2, unity_DynamicLightmapSTArray)
  357. #endif
  358. #ifdef UNITY_USE_SHCOEFFS_ARRAYS
  359. UNITY_DEFINE_INSTANCED_PROP(half4, unity_SHArArray)
  360. UNITY_DEFINE_INSTANCED_PROP(half4, unity_SHAgArray)
  361. UNITY_DEFINE_INSTANCED_PROP(half4, unity_SHAbArray)
  362. UNITY_DEFINE_INSTANCED_PROP(half4, unity_SHBrArray)
  363. UNITY_DEFINE_INSTANCED_PROP(half4, unity_SHBgArray)
  364. UNITY_DEFINE_INSTANCED_PROP(half4, unity_SHBbArray)
  365. UNITY_DEFINE_INSTANCED_PROP(half4, unity_SHCArray)
  366. #define unity_SHAr UNITY_ACCESS_INSTANCED_PROP(unity_Builtins2, unity_SHArArray)
  367. #define unity_SHAg UNITY_ACCESS_INSTANCED_PROP(unity_Builtins2, unity_SHAgArray)
  368. #define unity_SHAb UNITY_ACCESS_INSTANCED_PROP(unity_Builtins2, unity_SHAbArray)
  369. #define unity_SHBr UNITY_ACCESS_INSTANCED_PROP(unity_Builtins2, unity_SHBrArray)
  370. #define unity_SHBg UNITY_ACCESS_INSTANCED_PROP(unity_Builtins2, unity_SHBgArray)
  371. #define unity_SHBb UNITY_ACCESS_INSTANCED_PROP(unity_Builtins2, unity_SHBbArray)
  372. #define unity_SHC UNITY_ACCESS_INSTANCED_PROP(unity_Builtins2, unity_SHCArray)
  373. #endif
  374. #ifdef UNITY_USE_PROBESOCCLUSION_ARRAY
  375. UNITY_DEFINE_INSTANCED_PROP(half4, unity_ProbesOcclusionArray)
  376. #define unity_ProbesOcclusion UNITY_ACCESS_INSTANCED_PROP(unity_Builtins2, unity_ProbesOcclusionArray)
  377. #endif
  378. UNITY_INSTANCING_BUFFER_END(unity_Builtins2)
  379. UNITY_INSTANCING_BUFFER_START(PerDraw3)
  380. UNITY_DEFINE_INSTANCED_PROP(float4x4, unity_PrevObjectToWorldArray)
  381. UNITY_DEFINE_INSTANCED_PROP(float4x4, unity_PrevWorldToObjectArray)
  382. UNITY_INSTANCING_BUFFER_END(unity_Builtins3)
  383. #endif
  384. #if defined(UNITY_DOTS_INSTANCING_ENABLED)
  385. #undef UNITY_MATRIX_M
  386. #undef UNITY_MATRIX_I_M
  387. #undef UNITY_PREV_MATRIX_M
  388. #undef UNITY_PREV_MATRIX_I_M
  389. #define UNITY_DOTS_MATRIX_M LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME(float3x4, unity_ObjectToWorld))
  390. #define UNITY_DOTS_MATRIX_I_M LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME(float3x4, unity_WorldToObject))
  391. #define UNITY_DOTS_PREV_MATRIX_M LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME(float3x4, unity_MatrixPreviousM))
  392. #define UNITY_DOTS_PREV_MATRIX_I_M LoadDOTSInstancedData_float4x4_from_float3x4(UNITY_DOTS_INSTANCED_METADATA_NAME(float3x4, unity_MatrixPreviousMI))
  393. #ifdef MODIFY_MATRIX_FOR_CAMERA_RELATIVE_RENDERING
  394. #define UNITY_MATRIX_M ApplyCameraTranslationToMatrix(UNITY_DOTS_MATRIX_M)
  395. #define UNITY_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(UNITY_DOTS_MATRIX_I_M)
  396. #define UNITY_PREV_MATRIX_M ApplyCameraTranslationToMatrix(UNITY_DOTS_PREV_MATRIX_M)
  397. #define UNITY_PREV_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(UNITY_DOTS_PREV_MATRIX_I_M)
  398. #else
  399. #define UNITY_MATRIX_M UNITY_DOTS_MATRIX_M
  400. #define UNITY_MATRIX_I_M UNITY_DOTS_MATRIX_I_M
  401. #define UNITY_PREV_MATRIX_M UNITY_DOTS_PREV_MATRIX_M
  402. #define UNITY_PREV_MATRIX_I_M UNITY_DOTS_PREV_MATRIX_I_M
  403. #endif
  404. #else
  405. #ifndef UNITY_DONT_INSTANCE_OBJECT_MATRICES
  406. #undef UNITY_MATRIX_M
  407. #undef UNITY_MATRIX_I_M
  408. #undef UNITY_PREV_MATRIX_M
  409. #undef UNITY_PREV_MATRIX_I_M
  410. // Use #if instead of preprocessor concatenation to avoid really hard to debug
  411. // preprocessing issues in some cases.
  412. #if UNITY_WORLDTOOBJECTARRAY_CB == 0
  413. #define UNITY_BUILTINS_WITH_WORLDTOOBJECTARRAY unity_Builtins0
  414. #else
  415. #define UNITY_BUILTINS_WITH_WORLDTOOBJECTARRAY unity_Builtins1
  416. #endif
  417. #ifdef MODIFY_MATRIX_FOR_CAMERA_RELATIVE_RENDERING
  418. #define UNITY_MATRIX_M ApplyCameraTranslationToMatrix(UNITY_ACCESS_INSTANCED_PROP(unity_Builtins0, unity_ObjectToWorldArray))
  419. #define UNITY_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(UNITY_ACCESS_INSTANCED_PROP(UNITY_BUILTINS_WITH_WORLDTOOBJECTARRAY, unity_WorldToObjectArray))
  420. #define UNITY_PREV_MATRIX_M ApplyCameraTranslationToMatrix(UNITY_ACCESS_INSTANCED_PROP(unity_Builtins3, unity_PrevObjectToWorldArray))
  421. #define UNITY_PREV_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(UNITY_ACCESS_INSTANCED_PROP(unity_Builtins3, unity_PrevWorldToObjectArray))
  422. #else
  423. #define UNITY_MATRIX_M UNITY_ACCESS_INSTANCED_PROP(unity_Builtins0, unity_ObjectToWorldArray)
  424. #define UNITY_MATRIX_I_M UNITY_ACCESS_INSTANCED_PROP(UNITY_BUILTINS_WITH_WORLDTOOBJECTARRAY, unity_WorldToObjectArray)
  425. #define UNITY_PREV_MATRIX_M UNITY_ACCESS_INSTANCED_PROP(unity_Builtins3, unity_PrevObjectToWorldArray)
  426. #define UNITY_PREV_MATRIX_I_M UNITY_ACCESS_INSTANCED_PROP(unity_Builtins3, unity_PrevWorldToObjectArray)
  427. #endif
  428. #endif
  429. #endif
  430. #else // UNITY_INSTANCING_ENABLED
  431. // in procedural mode we don't need cbuffer, and properties are not uniforms
  432. #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
  433. #define UNITY_INSTANCING_BUFFER_START(buf)
  434. #define UNITY_INSTANCING_BUFFER_END(arr)
  435. #define UNITY_DEFINE_INSTANCED_PROP(type, var) static type var;
  436. #else
  437. #define UNITY_INSTANCING_BUFFER_START(buf) CBUFFER_START(buf)
  438. #define UNITY_INSTANCING_BUFFER_END(arr) CBUFFER_END
  439. #define UNITY_DEFINE_INSTANCED_PROP(type, var) type var;
  440. #endif
  441. #define UNITY_ACCESS_INSTANCED_PROP(arr, var) var
  442. #endif // UNITY_INSTANCING_ENABLED
  443. #endif // UNITY_INSTANCING_INCLUDED