UnityInput.hlsl 8.5 KB

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