説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

UnityInput.hlsl 11KB

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