UnityInput.hlsl 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // UNITY_SHADER_NO_UPGRADE
  2. #ifndef UNIVERSAL_SHADER_VARIABLES_INCLUDED
  3. #define UNIVERSAL_SHADER_VARIABLES_INCLUDED
  4. #if defined(STEREO_INSTANCING_ON) && (defined(SHADER_API_D3D11) || defined(SHADER_API_GLES3) || defined(SHADER_API_GLCORE) || defined(SHADER_API_PSSL) || defined(SHADER_API_VULKAN))
  5. #define UNITY_STEREO_INSTANCING_ENABLED
  6. #endif
  7. #if defined(STEREO_MULTIVIEW_ON) && (defined(SHADER_API_GLES3) || defined(SHADER_API_GLCORE) || defined(SHADER_API_VULKAN)) && !(defined(SHADER_API_SWITCH))
  8. #define UNITY_STEREO_MULTIVIEW_ENABLED
  9. #endif
  10. #if defined(UNITY_SINGLE_PASS_STEREO) || defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED)
  11. #define USING_STEREO_MATRICES
  12. #endif
  13. #if defined(USING_STEREO_MATRICES)
  14. // Current pass transforms.
  15. #define glstate_matrix_projection unity_StereoMatrixP[unity_StereoEyeIndex] // goes through GL.GetGPUProjectionMatrix()
  16. #define unity_MatrixV unity_StereoMatrixV[unity_StereoEyeIndex]
  17. #define unity_MatrixInvV unity_StereoMatrixInvV[unity_StereoEyeIndex]
  18. #define unity_MatrixInvP unity_StereoMatrixInvP[unity_StereoEyeIndex]
  19. #define unity_MatrixVP unity_StereoMatrixVP[unity_StereoEyeIndex]
  20. #define unity_MatrixInvVP unity_StereoMatrixInvVP[unity_StereoEyeIndex]
  21. #define unity_MatrixPrevVP unity_StereoMatrixPrevVP[unity_StereoEyeIndex]
  22. // Camera transform (but the same as pass transform for XR).
  23. #define unity_CameraProjection unity_StereoCameraProjection[unity_StereoEyeIndex] // Does not go through GL.GetGPUProjectionMatrix()
  24. #define unity_CameraInvProjection unity_StereoCameraInvProjection[unity_StereoEyeIndex]
  25. #define unity_WorldToCamera unity_StereoMatrixV[unity_StereoEyeIndex] // Should be unity_StereoWorldToCamera but no use-case in XR pass
  26. #define unity_CameraToWorld unity_StereoMatrixInvV[unity_StereoEyeIndex] // Should be unity_StereoCameraToWorld but no use-case in XR pass
  27. #define _WorldSpaceCameraPos unity_StereoWorldSpaceCameraPos[unity_StereoEyeIndex]
  28. #endif
  29. #define UNITY_LIGHTMODEL_AMBIENT (glstate_lightmodel_ambient * 2)
  30. // ----------------------------------------------------------------------------
  31. // Time (t = time since current level load) values from Unity
  32. float4 _Time; // (t/20, t, t*2, t*3)
  33. float4 _SinTime; // sin(t/8), sin(t/4), sin(t/2), sin(t)
  34. float4 _CosTime; // cos(t/8), cos(t/4), cos(t/2), cos(t)
  35. float4 unity_DeltaTime; // dt, 1/dt, smoothdt, 1/smoothdt
  36. float4 _TimeParameters; // t, sin(t), cos(t)
  37. #if !defined(USING_STEREO_MATRICES)
  38. float3 _WorldSpaceCameraPos;
  39. #endif
  40. // x = 1 or -1 (-1 if projection is flipped)
  41. // y = near plane
  42. // z = far plane
  43. // w = 1/far plane
  44. float4 _ProjectionParams;
  45. // x = width
  46. // y = height
  47. // z = 1 + 1.0/width
  48. // w = 1 + 1.0/height
  49. float4 _ScreenParams;
  50. // x = Mip Bias
  51. // y = 2.0 ^ [Mip Bias]
  52. float2 _GlobalMipBias;
  53. // Values used to linearize the Z buffer (http://www.humus.name/temp/Linearize%20depth.txt)
  54. // x = 1-far/near
  55. // y = far/near
  56. // z = x/far
  57. // w = y/far
  58. // or in case of a reversed depth buffer (UNITY_REVERSED_Z is 1)
  59. // x = -1+far/near
  60. // y = 1
  61. // z = x/far
  62. // w = 1/far
  63. float4 _ZBufferParams;
  64. // x = orthographic camera's width
  65. // y = orthographic camera's height
  66. // z = unused
  67. // w = 1.0 if camera is ortho, 0.0 if perspective
  68. float4 unity_OrthoParams;
  69. // scaleBias.x = flipSign
  70. // scaleBias.y = scale
  71. // scaleBias.z = bias
  72. // scaleBias.w = unused
  73. uniform float4 _ScaleBias;
  74. uniform float4 _ScaleBiasRt;
  75. float4 unity_CameraWorldClipPlanes[6];
  76. #if !defined(USING_STEREO_MATRICES)
  77. // Projection matrices of the camera. Note that this might be different from projection matrix
  78. // that is set right now, e.g. while rendering shadows the matrices below are still the projection
  79. // of original camera.
  80. float4x4 unity_CameraProjection;
  81. float4x4 unity_CameraInvProjection;
  82. float4x4 unity_WorldToCamera;
  83. float4x4 unity_CameraToWorld;
  84. #endif
  85. // ----------------------------------------------------------------------------
  86. // Block Layout should be respected due to SRP Batcher
  87. CBUFFER_START(UnityPerDraw)
  88. // Space block Feature
  89. float4x4 unity_ObjectToWorld;
  90. float4x4 unity_WorldToObject;
  91. float4 unity_LODFade; // x is the fade value ranging within [0,1]. y is x quantized into 16 levels
  92. real4 unity_WorldTransformParams; // w is usually 1.0, or -1.0 for odd-negative scale transforms
  93. // Render Layer block feature
  94. // Only the first channel (x) contains valid data and the float must be reinterpreted using asuint() to extract the original 32 bits values.
  95. float4 unity_RenderingLayer;
  96. // Light Indices block feature
  97. // These are set internally by the engine upon request by RendererConfiguration.
  98. half4 unity_LightData;
  99. half4 unity_LightIndices[2];
  100. half4 unity_ProbesOcclusion;
  101. // Reflection Probe 0 block feature
  102. // HDR environment map decode instructions
  103. real4 unity_SpecCube0_HDR;
  104. real4 unity_SpecCube1_HDR;
  105. float4 unity_SpecCube0_BoxMax; // w contains the blend distance
  106. float4 unity_SpecCube0_BoxMin; // w contains the lerp value
  107. float4 unity_SpecCube0_ProbePosition; // w is set to 1 for box projection
  108. float4 unity_SpecCube1_BoxMax; // w contains the blend distance
  109. float4 unity_SpecCube1_BoxMin; // w contains the sign of (SpecCube0.importance - SpecCube1.importance)
  110. float4 unity_SpecCube1_ProbePosition; // w is set to 1 for box projection
  111. // Lightmap block feature
  112. float4 unity_LightmapST;
  113. float4 unity_DynamicLightmapST;
  114. // SH block feature
  115. real4 unity_SHAr;
  116. real4 unity_SHAg;
  117. real4 unity_SHAb;
  118. real4 unity_SHBr;
  119. real4 unity_SHBg;
  120. real4 unity_SHBb;
  121. real4 unity_SHC;
  122. // Velocity
  123. float4x4 unity_MatrixPreviousM;
  124. float4x4 unity_MatrixPreviousMI;
  125. //X : Use last frame positions (right now skinned meshes are the only objects that use this
  126. //Y : Force No Motion
  127. //Z : Z bias value
  128. //W : Camera only
  129. float4 unity_MotionVectorsParams;
  130. CBUFFER_END
  131. #if defined(USING_STEREO_MATRICES)
  132. CBUFFER_START(UnityStereoViewBuffer)
  133. float4x4 unity_StereoMatrixP[2];
  134. float4x4 unity_StereoMatrixInvP[2];
  135. float4x4 unity_StereoMatrixV[2];
  136. float4x4 unity_StereoMatrixInvV[2];
  137. float4x4 unity_StereoMatrixVP[2];
  138. float4x4 unity_StereoMatrixPrevVP[2];
  139. float4x4 unity_StereoMatrixInvVP[2];
  140. float4x4 unity_StereoCameraProjection[2];
  141. float4x4 unity_StereoCameraInvProjection[2];
  142. float3 unity_StereoWorldSpaceCameraPos[2];
  143. float4 unity_StereoScaleOffset[2];
  144. CBUFFER_END
  145. #endif
  146. #if defined(UNITY_STEREO_MULTIVIEW_ENABLED) && defined(SHADER_STAGE_VERTEX)
  147. // OVR_multiview
  148. // In order to convey this info over the DX compiler, we wrap it into a cbuffer.
  149. #if !defined(UNITY_DECLARE_MULTIVIEW)
  150. #define UNITY_DECLARE_MULTIVIEW(number_of_views) CBUFFER_START(OVR_multiview) uint gl_ViewID; uint numViews_##number_of_views; CBUFFER_END
  151. #define UNITY_VIEWID gl_ViewID
  152. #endif
  153. #endif
  154. #if defined(UNITY_STEREO_MULTIVIEW_ENABLED) && defined(SHADER_STAGE_VERTEX)
  155. #define unity_StereoEyeIndex UNITY_VIEWID
  156. UNITY_DECLARE_MULTIVIEW(2);
  157. #elif defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED)
  158. static uint unity_StereoEyeIndex;
  159. #elif defined(UNITY_SINGLE_PASS_STEREO)
  160. CBUFFER_START(UnityStereoEyeIndex)
  161. int unity_StereoEyeIndex;
  162. CBUFFER_END
  163. #endif
  164. float4x4 glstate_matrix_transpose_modelview0;
  165. // ----------------------------------------------------------------------------
  166. real4 glstate_lightmodel_ambient;
  167. real4 unity_AmbientSky;
  168. real4 unity_AmbientEquator;
  169. real4 unity_AmbientGround;
  170. real4 unity_IndirectSpecColor;
  171. float4 unity_FogParams;
  172. real4 unity_FogColor;
  173. #if !defined(USING_STEREO_MATRICES)
  174. float4x4 glstate_matrix_projection;
  175. float4x4 unity_MatrixV;
  176. float4x4 unity_MatrixInvV;
  177. float4x4 unity_MatrixInvP;
  178. float4x4 unity_MatrixVP;
  179. float4x4 unity_MatrixPrevVP;
  180. float4x4 unity_MatrixInvVP;
  181. float4 unity_StereoScaleOffset;
  182. int unity_StereoEyeIndex;
  183. #endif
  184. real4 unity_ShadowColor;
  185. // ----------------------------------------------------------------------------
  186. // Unity specific
  187. TEXTURECUBE(unity_SpecCube0);
  188. SAMPLER(samplerunity_SpecCube0);
  189. TEXTURECUBE(unity_SpecCube1);
  190. SAMPLER(samplerunity_SpecCube1);
  191. // Main lightmap
  192. TEXTURE2D(unity_Lightmap);
  193. SAMPLER(samplerunity_Lightmap);
  194. TEXTURE2D_ARRAY(unity_Lightmaps);
  195. SAMPLER(samplerunity_Lightmaps);
  196. // Dynamic lightmap
  197. TEXTURE2D(unity_DynamicLightmap);
  198. SAMPLER(samplerunity_DynamicLightmap);
  199. // TODO ENLIGHTEN: Instanced GI
  200. // Dual or directional lightmap (always used with unity_Lightmap, so can share sampler)
  201. TEXTURE2D(unity_LightmapInd);
  202. TEXTURE2D_ARRAY(unity_LightmapsInd);
  203. TEXTURE2D(unity_DynamicDirectionality);
  204. // TODO ENLIGHTEN: Instanced GI
  205. // TEXTURE2D_ARRAY(unity_DynamicDirectionality);
  206. TEXTURE2D(unity_ShadowMask);
  207. SAMPLER(samplerunity_ShadowMask);
  208. TEXTURE2D_ARRAY(unity_ShadowMasks);
  209. SAMPLER(samplerunity_ShadowMasks);
  210. // ----------------------------------------------------------------------------
  211. // TODO: all affine matrices should be 3x4.
  212. // TODO: sort these vars by the frequency of use (descending), and put commonly used vars together.
  213. // Note: please use UNITY_MATRIX_X macros instead of referencing matrix variables directly.
  214. float4x4 _PrevViewProjMatrix;
  215. float4x4 _ViewProjMatrix;
  216. float4x4 _NonJitteredViewProjMatrix;
  217. float4x4 _ViewMatrix;
  218. float4x4 _ProjMatrix;
  219. float4x4 _InvViewProjMatrix;
  220. float4x4 _InvViewMatrix;
  221. float4x4 _InvProjMatrix;
  222. float4 _InvProjParam;
  223. float4 _ScreenSize; // {w, h, 1/w, 1/h}
  224. float4 _FrustumPlanes[6]; // {(a, b, c) = N, d = -dot(N, P)} [L, R, T, B, N, F]
  225. float4x4 OptimizeProjectionMatrix(float4x4 M)
  226. {
  227. // Matrix format (x = non-constant value).
  228. // Orthographic Perspective Combined(OR)
  229. // | x 0 0 x | | x 0 x 0 | | x 0 x x |
  230. // | 0 x 0 x | | 0 x x 0 | | 0 x x x |
  231. // | x x x x | | x x x x | | x x x x | <- oblique projection row
  232. // | 0 0 0 1 | | 0 0 x 0 | | 0 0 x x |
  233. // Notice that some values are always 0.
  234. // We can avoid loading and doing math with constants.
  235. M._21_41 = 0;
  236. M._12_42 = 0;
  237. return M;
  238. }
  239. #endif // UNIVERSAL_SHADER_VARIABLES_INCLUDED