Core.hlsl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #ifndef UNIVERSAL_PIPELINE_CORE_INCLUDED
  2. #define UNIVERSAL_PIPELINE_CORE_INCLUDED
  3. // VT is not supported in URP (for now) this ensures any shaders using the VT
  4. // node work by falling to regular texture sampling.
  5. #define FORCE_VIRTUAL_TEXTURING_OFF 1
  6. #if defined(_CLUSTERED_RENDERING)
  7. #define _ADDITIONAL_LIGHTS 1
  8. #undef _ADDITIONAL_LIGHTS_VERTEX
  9. #define USE_CLUSTERED_LIGHTING 1
  10. #else
  11. #define USE_CLUSTERED_LIGHTING 0
  12. #endif
  13. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
  14. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Packing.hlsl"
  15. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Version.hlsl"
  16. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl"
  17. #if !defined(SHADER_HINT_NICE_QUALITY)
  18. #if defined(SHADER_API_MOBILE) || defined(SHADER_API_SWITCH)
  19. #define SHADER_HINT_NICE_QUALITY 0
  20. #else
  21. #define SHADER_HINT_NICE_QUALITY 1
  22. #endif
  23. #endif
  24. // Shader Quality Tiers in Universal.
  25. // SRP doesn't use Graphics Settings Quality Tiers.
  26. // We should expose shader quality tiers in the pipeline asset.
  27. // Meanwhile, it's forced to be:
  28. // High Quality: Non-mobile platforms or shader explicit defined SHADER_HINT_NICE_QUALITY
  29. // Medium: Mobile aside from GLES2
  30. // Low: GLES2
  31. #if SHADER_HINT_NICE_QUALITY
  32. #define SHADER_QUALITY_HIGH
  33. #elif defined(SHADER_API_GLES)
  34. #define SHADER_QUALITY_LOW
  35. #else
  36. #define SHADER_QUALITY_MEDIUM
  37. #endif
  38. #ifndef BUMP_SCALE_NOT_SUPPORTED
  39. #define BUMP_SCALE_NOT_SUPPORTED !SHADER_HINT_NICE_QUALITY
  40. #endif
  41. #if UNITY_REVERSED_Z
  42. // TODO: workaround. There's a bug where SHADER_API_GL_CORE gets erroneously defined on switch.
  43. #if (defined(SHADER_API_GLCORE) && !defined(SHADER_API_SWITCH)) || defined(SHADER_API_GLES) || defined(SHADER_API_GLES3)
  44. //GL with reversed z => z clip range is [near, -far] -> remapping to [0, far]
  45. #define UNITY_Z_0_FAR_FROM_CLIPSPACE(coord) max((coord - _ProjectionParams.y)/(-_ProjectionParams.z-_ProjectionParams.y)*_ProjectionParams.z, 0)
  46. #else
  47. //D3d with reversed Z => z clip range is [near, 0] -> remapping to [0, far]
  48. //max is required to protect ourselves from near plane not being correct/meaningful in case of oblique matrices.
  49. #define UNITY_Z_0_FAR_FROM_CLIPSPACE(coord) max(((1.0-(coord)/_ProjectionParams.y)*_ProjectionParams.z),0)
  50. #endif
  51. #elif UNITY_UV_STARTS_AT_TOP
  52. //D3d without reversed z => z clip range is [0, far] -> nothing to do
  53. #define UNITY_Z_0_FAR_FROM_CLIPSPACE(coord) (coord)
  54. #else
  55. //Opengl => z clip range is [-near, far] -> remapping to [0, far]
  56. #define UNITY_Z_0_FAR_FROM_CLIPSPACE(coord) max(((coord + _ProjectionParams.y)/(_ProjectionParams.z+_ProjectionParams.y))*_ProjectionParams.z, 0)
  57. #endif
  58. // Stereo-related bits
  59. #if defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED)
  60. #define SLICE_ARRAY_INDEX unity_StereoEyeIndex
  61. #define TEXTURE2D_X(textureName) TEXTURE2D_ARRAY(textureName)
  62. #define TEXTURE2D_X_PARAM(textureName, samplerName) TEXTURE2D_ARRAY_PARAM(textureName, samplerName)
  63. #define TEXTURE2D_X_ARGS(textureName, samplerName) TEXTURE2D_ARRAY_ARGS(textureName, samplerName)
  64. #define TEXTURE2D_X_HALF(textureName) TEXTURE2D_ARRAY_HALF(textureName)
  65. #define TEXTURE2D_X_FLOAT(textureName) TEXTURE2D_ARRAY_FLOAT(textureName)
  66. #define LOAD_TEXTURE2D_X(textureName, unCoord2) LOAD_TEXTURE2D_ARRAY(textureName, unCoord2, SLICE_ARRAY_INDEX)
  67. #define LOAD_TEXTURE2D_X_LOD(textureName, unCoord2, lod) LOAD_TEXTURE2D_ARRAY_LOD(textureName, unCoord2, SLICE_ARRAY_INDEX, lod)
  68. #define SAMPLE_TEXTURE2D_X(textureName, samplerName, coord2) SAMPLE_TEXTURE2D_ARRAY(textureName, samplerName, coord2, SLICE_ARRAY_INDEX)
  69. #define SAMPLE_TEXTURE2D_X_LOD(textureName, samplerName, coord2, lod) SAMPLE_TEXTURE2D_ARRAY_LOD(textureName, samplerName, coord2, SLICE_ARRAY_INDEX, lod)
  70. #define GATHER_TEXTURE2D_X(textureName, samplerName, coord2) GATHER_TEXTURE2D_ARRAY(textureName, samplerName, coord2, SLICE_ARRAY_INDEX)
  71. #define GATHER_RED_TEXTURE2D_X(textureName, samplerName, coord2) GATHER_RED_TEXTURE2D(textureName, samplerName, float3(coord2, SLICE_ARRAY_INDEX))
  72. #define GATHER_GREEN_TEXTURE2D_X(textureName, samplerName, coord2) GATHER_GREEN_TEXTURE2D(textureName, samplerName, float3(coord2, SLICE_ARRAY_INDEX))
  73. #define GATHER_BLUE_TEXTURE2D_X(textureName, samplerName, coord2) GATHER_BLUE_TEXTURE2D(textureName, samplerName, float3(coord2, SLICE_ARRAY_INDEX))
  74. #else
  75. #define SLICE_ARRAY_INDEX 0
  76. #define TEXTURE2D_X(textureName) TEXTURE2D(textureName)
  77. #define TEXTURE2D_X_PARAM(textureName, samplerName) TEXTURE2D_PARAM(textureName, samplerName)
  78. #define TEXTURE2D_X_ARGS(textureName, samplerName) TEXTURE2D_ARGS(textureName, samplerName)
  79. #define TEXTURE2D_X_HALF(textureName) TEXTURE2D_HALF(textureName)
  80. #define TEXTURE2D_X_FLOAT(textureName) TEXTURE2D_FLOAT(textureName)
  81. #define LOAD_TEXTURE2D_X(textureName, unCoord2) LOAD_TEXTURE2D(textureName, unCoord2)
  82. #define LOAD_TEXTURE2D_X_LOD(textureName, unCoord2, lod) LOAD_TEXTURE2D_LOD(textureName, unCoord2, lod)
  83. #define SAMPLE_TEXTURE2D_X(textureName, samplerName, coord2) SAMPLE_TEXTURE2D(textureName, samplerName, coord2)
  84. #define SAMPLE_TEXTURE2D_X_LOD(textureName, samplerName, coord2, lod) SAMPLE_TEXTURE2D_LOD(textureName, samplerName, coord2, lod)
  85. #define GATHER_TEXTURE2D_X(textureName, samplerName, coord2) GATHER_TEXTURE2D(textureName, samplerName, coord2)
  86. #define GATHER_RED_TEXTURE2D_X(textureName, samplerName, coord2) GATHER_RED_TEXTURE2D(textureName, samplerName, coord2)
  87. #define GATHER_GREEN_TEXTURE2D_X(textureName, samplerName, coord2) GATHER_GREEN_TEXTURE2D(textureName, samplerName, coord2)
  88. #define GATHER_BLUE_TEXTURE2D_X(textureName, samplerName, coord2) GATHER_BLUE_TEXTURE2D(textureName, samplerName, coord2)
  89. #endif
  90. ///
  91. /// Texture Sampling Macro Overrides for Scaling
  92. ///
  93. /// When mip bias is supported by the underlying platform, the following section redefines all 2d texturing operations to support a global mip bias feature.
  94. /// This feature is used to improve rendering quality when image scaling is active. It achieves this by adding a bias value to the standard mip lod calculation
  95. /// which allows us to select the mip level based on the final image resolution rather than the current rendering resolution.
  96. #ifdef PLATFORM_SAMPLE_TEXTURE2D_BIAS
  97. #ifdef SAMPLE_TEXTURE2D
  98. #undef SAMPLE_TEXTURE2D
  99. #define SAMPLE_TEXTURE2D(textureName, samplerName, coord2) \
  100. PLATFORM_SAMPLE_TEXTURE2D_BIAS(textureName, samplerName, coord2, _GlobalMipBias.x)
  101. #endif
  102. #ifdef SAMPLE_TEXTURE2D_BIAS
  103. #undef SAMPLE_TEXTURE2D_BIAS
  104. #define SAMPLE_TEXTURE2D_BIAS(textureName, samplerName, coord2, bias) \
  105. PLATFORM_SAMPLE_TEXTURE2D_BIAS(textureName, samplerName, coord2, (bias + _GlobalMipBias.x))
  106. #endif
  107. #endif
  108. #ifdef PLATFORM_SAMPLE_TEXTURE2D_GRAD
  109. #ifdef SAMPLE_TEXTURE2D_GRAD
  110. #undef SAMPLE_TEXTURE2D_GRAD
  111. #define SAMPLE_TEXTURE2D_GRAD(textureName, samplerName, coord2, dpdx, dpdy) \
  112. PLATFORM_SAMPLE_TEXTURE2D_GRAD(textureName, samplerName, coord2, (dpdx * _GlobalMipBias.y), (dpdy * _GlobalMipBias.y))
  113. #endif
  114. #endif
  115. #ifdef PLATFORM_SAMPLE_TEXTURE2D_ARRAY_BIAS
  116. #ifdef SAMPLE_TEXTURE2D_ARRAY
  117. #undef SAMPLE_TEXTURE2D_ARRAY
  118. #define SAMPLE_TEXTURE2D_ARRAY(textureName, samplerName, coord2, index) \
  119. PLATFORM_SAMPLE_TEXTURE2D_ARRAY_BIAS(textureName, samplerName, coord2, index, _GlobalMipBias.x)
  120. #endif
  121. #ifdef SAMPLE_TEXTURE2D_ARRAY_BIAS
  122. #undef SAMPLE_TEXTURE2D_ARRAY_BIAS
  123. #define SAMPLE_TEXTURE2D_ARRAY_BIAS(textureName, samplerName, coord2, index, bias) \
  124. PLATFORM_SAMPLE_TEXTURE2D_ARRAY_BIAS(textureName, samplerName, coord2, index, (bias + _GlobalMipBias.x))
  125. #endif
  126. #endif
  127. #ifdef PLATFORM_SAMPLE_TEXTURECUBE_BIAS
  128. #ifdef SAMPLE_TEXTURECUBE
  129. #undef SAMPLE_TEXTURECUBE
  130. #define SAMPLE_TEXTURECUBE(textureName, samplerName, coord3) \
  131. PLATFORM_SAMPLE_TEXTURECUBE_BIAS(textureName, samplerName, coord3, _GlobalMipBias.x)
  132. #endif
  133. #ifdef SAMPLE_TEXTURECUBE_BIAS
  134. #undef SAMPLE_TEXTURECUBE_BIAS
  135. #define SAMPLE_TEXTURECUBE_BIAS(textureName, samplerName, coord3, bias) \
  136. PLATFORM_SAMPLE_TEXTURECUBE_BIAS(textureName, samplerName, coord3, (bias + _GlobalMipBias.x))
  137. #endif
  138. #endif
  139. #ifdef PLATFORM_SAMPLE_TEXTURECUBE_ARRAY_BIAS
  140. #ifdef SAMPLE_TEXTURECUBE_ARRAY
  141. #undef SAMPLE_TEXTURECUBE_ARRAY
  142. #define SAMPLE_TEXTURECUBE_ARRAY(textureName, samplerName, coord3, index)\
  143. PLATFORM_SAMPLE_TEXTURECUBE_ARRAY_BIAS(textureName, samplerName, coord3, index, _GlobalMipBias.x)
  144. #endif
  145. #ifdef SAMPLE_TEXTURECUBE_ARRAY_BIAS
  146. #undef SAMPLE_TEXTURECUBE_ARRAY_BIAS
  147. #define SAMPLE_TEXTURECUBE_ARRAY_BIAS(textureName, samplerName, coord3, index, bias)\
  148. PLATFORM_SAMPLE_TEXTURECUBE_ARRAY_BIAS(textureName, samplerName, coord3, index, (bias + _GlobalMipBias.x))
  149. #endif
  150. #endif
  151. #define VT_GLOBAL_MIP_BIAS_MULTIPLIER (_GlobalMipBias.y)
  152. // Structs
  153. struct VertexPositionInputs
  154. {
  155. float3 positionWS; // World space position
  156. float3 positionVS; // View space position
  157. float4 positionCS; // Homogeneous clip space position
  158. float4 positionNDC;// Homogeneous normalized device coordinates
  159. };
  160. struct VertexNormalInputs
  161. {
  162. real3 tangentWS;
  163. real3 bitangentWS;
  164. float3 normalWS;
  165. };
  166. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl"
  167. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Deprecated.hlsl"
  168. #endif