返回首页

URP shader库中的一些常用宏

com.unity.render-pipelines.core/ShaderLibrary/Macros.hlsl

定义
TRANSFORM_TEX(tex, name) ((tex.xy) * name##_ST.xy + name##_ST.zw)
GET_TEXELSIZE_NAME(name) (name##_TexelSize)
PI 3.14159265358979323846
TWO_PI 6.28318530717958647693
FOUR_PI 12.5663706143591729538
INV_PI 0.31830988618379067154
INV_TWO_PI 0.15915494309189533577
INV_FOUR_PI 0.07957747154594766788
HALF_PI 1.57079632679489661923
INV_HALF_PI 0.63661977236758134308
LOG2_E 1.44269504088896340736
INV_SQRT2 0.70710678118654752440
PI_DIV_FOUR 0.78539816339744830961
FLT_INF asfloat(0x7F800000)
FLT_EPS 5.960464478e-8 // 2^-24, machine epsilon: 1 + EPS = 1 (half of the ULP for 1.0f)
FLT_MIN 1.175494351e-38 // Minimum normalized positive floating-point number
FLT_MAX 3.402823466e+38 // Maximum representable floating-point number
HALF_EPS 4.8828125e-4 // 2^-11, machine epsilon: 1 + EPS = 1 (half of the ULP for 1.0f)
HALF_MIN 6.103515625e-5 /* 2^-14, the same value for 10, 11 and 16-bit: https://www.khronos.org/opengl/wiki/Small_Float_Formats */
HALF_MIN_SQRT 0.0078125 // 2^-7 == sqrt(HALF_MIN), useful for ensuring HALF_MIN after x^2
HALF_MAX 65504.0
UINT_MAX 0xFFFFFFFFu
INT_MAX 0x7FFFFFFF
   

com.unity.render-pipelines.core/ShaderLibrary/API/GLES2.hlsl

// GLES2 might not have shadow hardware comparison support
#if defined(UNITY_ENABLE_NATIVE_SHADOWS_LOOKUPS)
  #define SHADOW2D_TEXTURE_AND_SAMPLER sampler2DShadow
  #define SHADOWCUBE_TEXTURE_AND_SAMPLER samplerCUBEShadow
  #define SHADOW2D_SAMPLE(textureName, samplerName, coord3) shadow2D(textureName, coord3)
  #define SHADOWCUBE_SAMPLE(textureName, samplerName, coord4) ((texCUBE(textureName,(coord4).xyz) < (coord4).w) ? 0.0 : 1.0)
#else
  // emulate hardware comparison
  #define SHADOW2D_TEXTURE_AND_SAMPLER sampler2D_float
  #define SHADOWCUBE_TEXTURE_AND_SAMPLER samplerCUBE_float
  #define SHADOW2D_SAMPLE(textureName, samplerName, coord3) ((SAMPLE_DEPTH_TEXTURE(textureName, samplerName, (coord3).xy) < (coord3).z) ? 0.0 : 1.0)
  #define SHADOWCUBE_SAMPLE(textureName, samplerName, coord4) ((texCUBE(textureName,(coord4).xyz).r < (coord4).w) ? 0.0 : 1.0)
#endif

纹理相关的宏

纹理类型的宏
D3D11.hlsl
GLCore.hlsl
GLES2.hlsl GLES3.hlsl Metal.hlsl Switch.hlsl Vulkan.hlsl
TEXTURE2D(textureName) Texture2D  textureName
Texture2D  textureName
sampler2D textureName Texture2D  textureName Texture2D  textureName Texture2D  textureName Texture2D  textureName
TEXTURE2D_ARRAY(textureName) Texture2D Array  textureName
Texture2D Array  textureName
samplerCUBE textureName /* No support to texture2DArray*/ Texture2D Array  textureName Texture2D Array  textureName Texture2D Array  textureName Texture2D Array  textureName
TEXTURECUBE(textureName) TextureCube textureName
TextureCube textureName
samplerCUBE textureName TextureCube textureName TextureCube textureName TextureCube textureName TextureCube textureName
TEXTURECUBE_ARRAY(textureName) TextureCubeArray  textureName
TextureCubeArray   textureName

samplerCUBE textureName

/* No supoport to textureCubeArray and can't emulate with texture2DArray*/

TextureCubeArray  textureName TextureCubeArray  textureName TextureCubeArray  textureName TextureCubeArray  textureName
TEXTURE3D(textureName) Texture3D  textureName
Texture3D  textureName
sampler3D textureName Texture3D  textureName Texture3D  textureName Texture3D  textureName Texture3D  textureName
TEXTURE2D_FLOAT(textureName) Texture2D  textureName
Texture2D  textureName
sampler2D_float textureName Texture2D  textureName Texture2D  textureName Texture2D  textureName Texture2D  textureName
TEXTURE2D_ARRAY_FLOAT(textureName) Texture2DArray  textureName
Texture2DArray  textureName
TEXTURECUBE_FLOAT(textureName) /* No support to texture2DArray*/ Texture2DArray  textureName Texture2DArray  textureName Texture2DArray  textureName Texture2DArray  textureName
TEXTURECUBE_FLOAT(textureName) TextureCube textureName
TextureCube textureName
samplerCUBE_float textureName TextureCube textureName TextureCube textureName TextureCube textureName TextureCube textureName
TEXTURECUBE_ARRAY_FLOAT(textureName) TextureCubeArray  textureName
TextureCubeArray  textureName
TEXTURECUBE_FLOAT(textureName) /* No support to texture2DArray*/ TextureCubeArray  textureName TextureCubeArray  textureName TextureCubeArray  textureName TextureCubeArray  textureName
TEXTURE3D_FLOAT(textureName) Texture3D  textureName
Texture3D  textureName
sampler3D_float textureName Texture3D  textureName Texture3D  textureName Texture3D  textureName Texture3D  textureName
TEXTURE2D_HALF(textureName) Texture2D  textureName
Texture2D  textureName
sampler2D_half textureName Texture2D  textureName Texture2D  textureName Texture2D  textureName Texture2D  textureName
TEXTURE2D_ARRAY_HALF(textureName) Texture2DArray  textureName
Texture2DArray  textureName
TEXTURECUBE_HALF(textureName) /* No support to texture2DArray*/ Texture2D Array  textureName Texture2DArray  textureName Texture2D Array  textureName Texture2DArray  textureName
TEXTURECUBE_HALF(textureName) TextureCube textureName
TextureCube textureName
samplerCUBE_half textureName TextureCube textureName TextureCube textureName TextureCube textureName TextureCube textureName
TEXTURECUBE_ARRAY_HALF(textureName) TextureCubeArray  textureName
TextureCubeArray  textureName
TEXTURECUBE_HALF(textureName) /* No support to texture2DArray*/ TextureCubeArray  textureName TextureCubeArray  textureName TextureCubeArray  textureName TextureCubeArray  textureName
TEXTURE3D_HALF(textureName) Texture3D  textureName
Texture3D  textureName
sampler3D_half textureName Texture3D  textureName Texture3D  textureName Texture3D  textureName Texture3D  textureName
SAMPLER(samplerName) SamplerState samplerName
SamplerState samplerName
  SamplerState samplerName SamplerState samplerName SamplerState samplerName SamplerState samplerName
TEXTURE2D_PARAM(textureName, samplerName) TEXTURE2D(textureName), SAMPLER(samplerName)
TEXTURE2D(textureName), SAMPLER(samplerName)
sampler2D textureName TEXTURE2D(textureName), SAMPLER(samplerName) TEXTURE2D(textureName), SAMPLER(samplerName) TEXTURE2D(textureName), SAMPLER(samplerName) TEXTURE2D(textureName), SAMPLER(samplerName)
TEXTURE2D_ARRAY_PARAM(textureName, samplerName)

TEXTURE2D_ARRAY(textureName),

SAMPLER(samplerName)

TEXTURE2D_ARRAY(textureName),

SAMPLER(samplerName)

samplerCUBE textureName

TEXTURE2D_ARRAY(textureName),

SAMPLER(samplerName)

TEXTURE2D_ARRAY(textureName),

SAMPLER(samplerName)

TEXTURE2D_ARRAY(textureName),

SAMPLER(samplerName)

TEXTURE2D_ARRAY(textureName),

SAMPLER(samplerName)

PLATFORM_SAMPLE_TEXTURE2D(textureName, samplerName, coord2) textureName.Sample(samplerName, coord2)
textureName.Sample(samplerName, coord2)
tex2D(textureName, coord2) textureName.Sample(samplerName, coord2) textureName.Sample(samplerName, coord2) textureName.Sample(samplerName, coord2) textureName.Sample(samplerName, coord2)
PLATFORM_SAMPLE_TEXTURE2D_LOD(textureName, samplerName, coord2, lod) textureName.SampleLevel(samplerName, coord2, lod)
textureName.SampleLevel(samplerName, coord2, lod)
tex2Dlod(textureName, float4(coord2, 0, lod)) textureName.SampleLevel(samplerName, coord2, lod) textureName.SampleLevel(samplerName, coord2, lod) textureName.SampleLevel(samplerName, coord2, lod) textureName.SampleLevel(samplerName, coord2, lod)
PLATFORM_SAMPLE_TEXTURE2D_BIAS(textureName, samplerName, coord2, bias) textureName.SampleBias(samplerName, coord2, bias)
textureName.SampleBias(samplerName, coord2, bias)
tex2Dbias(textureName, float4(coord2, 0, bias)) textureName.SampleBias(samplerName, coord2, bias) textureName.SampleBias(samplerName, coord2, bias) textureName.SampleBias(samplerName, coord2, bias) textureName.SampleBias(samplerName, coord2, bias)
PLATFORM_SAMPLE_TEXTURE2D_GRAD(textureName, samplerName, coord2, dpdx, dpdy) textureName.SampleGrad(samplerName, coord2, dpdx, dpdy)
textureName.SampleGrad(samplerName, coord2, dpdx, dpdy)
PLATFORM_SAMPLE_TEXTURE2D(textureName, samplerName, coord2) textureName.SampleGrad(samplerName, coord2, dpdx, dpdy) textureName.SampleGrad(samplerName, coord2, dpdx, dpdy) textureName.SampleGrad(samplerName, coord2, dpdx, dpdy) textureName.SampleGrad(samplerName, coord2, dpdx, dpdy)
SAMPLE_TEXTURE2D(textureName, samplerName, coord2) PLATFORM_SAMPLE_TEXTURE2D(textureName, samplerName, coord2)
PLATFORM_SAMPLE_TEXTURE2D(textureName, samplerName, coord2)
PLATFORM_SAMPLE_TEXTURE2D(textureName, samplerName, coord2) PLATFORM_SAMPLE_TEXTURE2D(textureName, samplerName, coord2) PLATFORM_SAMPLE_TEXTURE2D(textureName, samplerName, coord2) PLATFORM_SAMPLE_TEXTURE2D(textureName, samplerName, coord2) PLATFORM_SAMPLE_TEXTURE2D(textureName, samplerName, coord2)
SAMPLE_TEXTURE2D_LOD(textureName, samplerName, coord2, lod) PLATFORM_SAMPLE_TEXTURE2D_LOD(textureName, samplerName, coord2, lod)
PLATFORM_SAMPLE_TEXTURE2D_LOD(textureName, samplerName, coord2, lod)
PLATFORM_SAMPLE_TEXTURE2D_LOD(textureName, samplerName, coord2, lod) PLATFORM_SAMPLE_TEXTURE2D_LOD(textureName, samplerName, coord2, lod) PLATFORM_SAMPLE_TEXTURE2D_LOD(textureName, samplerName, coord2, lod) PLATFORM_SAMPLE_TEXTURE2D_LOD(textureName, samplerName, coord2, lod) PLATFORM_SAMPLE_TEXTURE2D_LOD(textureName, samplerName, coord2, lod)
SAMPLE_TEXTURE2D_BIAS(textureName, samplerName, coord2, bias) PLATFORM_SAMPLE_TEXTURE2D_BIAS(textureName, samplerName, coord2, bias) PLATFORM_SAMPLE_TEXTURE2D_BIAS(textureName, samplerName, coord2, bias) PLATFORM_SAMPLE_TEXTURE2D_BIAS(textureName, samplerName, coord2, bias) PLATFORM_SAMPLE_TEXTURE2D_BIAS(textureName, samplerName, coord2, bias) PLATFORM_SAMPLE_TEXTURE2D_BIAS(textureName, samplerName, coord2, bias) PLATFORM_SAMPLE_TEXTURE2D_BIAS(textureName, samplerName, coord2, bias) PLATFORM_SAMPLE_TEXTURE2D_BIAS(textureName, samplerName, coord2, bias)
SAMPLE_TEXTURE2D_GRAD(textureName, samplerName, coord2, dpdx, dpdy) PLATFORM_SAMPLE_TEXTURE2D_GRAD(textureName, samplerName, coord2, dpdx, dpdy)
PLATFORM_SAMPLE_TEXTURE2D_GRAD(textureName, samplerName, coord2, dpdx, dpdy)
PLATFORM_SAMPLE_TEXTURE2D_GRAD(textureName, samplerName, coord2, dpdx, dpdy) PLATFORM_SAMPLE_TEXTURE2D_GRAD(textureName, samplerName, coord2, dpdx, dpdy) PLATFORM_SAMPLE_TEXTURE2D_GRAD(textureName, samplerName, coord2, dpdx, dpdy) PLATFORM_SAMPLE_TEXTURE2D_GRAD(textureName, samplerName, coord2, dpdx, dpdy) PLATFORM_SAMPLE_TEXTURE2D_GRAD(textureName, samplerName, coord2, dpdx, dpdy)
SAMPLE_TEXTURE2D_SHADOW(textureName, samplerName, coord3) textureName.SampleCmpLevelZero(samplerName, (coord3).xy, (coord3).z) textureName.SampleCmpLevelZero(samplerName, (coord3).xy, (coord3).z) SHADOW2D_SAMPLE(textureName, samplerName, coord3) textureName.SampleCmpLevelZero(samplerName, (coord3).xy, (coord3).z) textureName.SampleCmpLevelZero(samplerName, (coord3).xy, (coord3).z) textureName.SampleCmpLevelZero(samplerName, (coord3).xy, (coord3).z) textureName.SampleCmpLevelZero(samplerName, (coord3).xy, (coord3).z)
SAMPLE_DEPTH_TEXTURE(textureName, samplerName, coord2) SAMPLE_TEXTURE2D(textureName, samplerName, coord2).r SAMPLE_TEXTURE2D(textureName, samplerName, coord2).r SAMPLE_TEXTURE2D(textureName, samplerName, coord2).r SAMPLE_TEXTURE2D(textureName, samplerName, coord2).r SAMPLE_TEXTURE2D(textureName, samplerName, coord2).r SAMPLE_TEXTURE2D(textureName, samplerName, coord2).r SAMPLE_TEXTURE2D(textureName, samplerName, coord2).r
SAMPLE_DEPTH_TEXTURE_LOD(textureName, samplerName, coord2, lod SAMPLE_TEXTURE2D_LOD(textureName, samplerName, coord2, lod).r SAMPLE_TEXTURE2D_LOD(textureName, samplerName, coord2, lod).r SAMPLE_TEXTURE2D_LOD(textureName, samplerName, coord2, lod).r SAMPLE_TEXTURE2D_LOD(textureName, samplerName, coord2, lod).r SAMPLE_TEXTURE2D_LOD(textureName, samplerName, coord2, lod).r SAMPLE_TEXTURE2D_LOD(textureName, samplerName, coord2, lod).r SAMPLE_TEXTURE2D_LOD(textureName, samplerName, coord2, lod).r
LOAD_TEXTURE2D(textureName, unCoord2 textureName.Load(int3(unCoord2, 0)) textureName.Load(int3(unCoord2, 0)) half4(0, 0, 0, 0) textureName.Load(int3(unCoord2, 0)) textureName.Load(int3(unCoord2, 0)) textureName.Load(int3(unCoord2, 0)) textureName.Load(int3(unCoord2, 0))