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