您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

646 行
21 KiB

#ifndef UNITY_COMMON_INCLUDED
#define UNITY_COMMON_INCLUDED
// Convention:
// Unity is Y up - left handed
// space at the end of the variable name
// WS: world space
// VS: view space
// OS: object space
// CS: Homogenous clip spaces
// TS: tangent space
// TXS: texture space
// Example: NormalWS
// normalized / unormalized vector
// normalized direction are almost everywhere, we tag unormalized vector with un.
// Example: unL for unormalized light vector
// use capital letter for regular vector, vector are always pointing outward the current pixel position (ready for lighting equation)
// capital letter mean the vector is normalize, unless we put 'un' in front of it.
// V: View vector (no eye vector)
// L: Light vector
// N: Normal vector
// H: Half vector
// Input/Outputs structs in PascalCase and prefixed by entry type
// struct AttributesDefault
// struct VaryingsDefault
// use input/output as variable name when using these structures
// Entry program name
// VertDefault
// FragDefault / FragForward / FragDeferred
// constant floating number written as 1.0 (not 1, not 1.0, not 1.0h)
// uniform have _ as prefix + uppercase _LowercaseThenCamelCase
// All uniforms should be in contant buffer (nothing in the global namespace).
// The reason is that for compute shader we need to guarantee that the layout of CBs is consistent across kernels. Something that we can't control with the global namespace (uniforms get optimized out if not used, modifying the global CBuffer layout per kernel)
// Structure definition that are share between C# and hlsl.
// These structures need to be align on real4 to respect various packing rules from sahder language.
// This mean that these structure need to be padded.
// Do not use "in", only "out" or "inout" as califier, not "inline" keyword either, useless.
// The lighting code assume that 1 Unity unit (1uu) == 1 meters. This is very important regarding physically based light unit and inverse square attenuation
// When declaring "out" argument of function, they are always last
// headers from ShaderLibrary do not include "common.hlsl", this should be included in the .shader using it (or Material.hlsl)
// Rules: When doing an array for constant buffer variables, we always use real4 to avoid any packing issue, particularly between compute shader and pixel shaders
// i.e don't use SetGlobalFloatArray or SetComputeFloatParams
// The array can be alias in hlsl. Exemple:
// uniform real4 packedArray[3];
// static real unpackedArray[12] = (real[12]packedArray;
#ifndef real
#define real float
#define real2 float2
#define real3 float3
#define real4 float4
#define real2x2 float2x2
#define real2x3 float2x3
#define real3x2 float3x2
#define real3x3 float3x3
#define real3x4 float3x4
#define real4x3 float4x3
#define real4x4 float4x4
#define REAL_MIN FLT_MIN
#define REAL_MAX FLT_MAX
#endif
// Include language header
#if defined(SHADER_API_D3D11)
#include "API/D3D11.hlsl"
#elif defined(SHADER_API_PSSL)
#include "API/PSSL.hlsl"
#elif defined(SHADER_API_XBOXONE)
#include "API/XBoxOne.hlsl"
#elif defined(SHADER_API_METAL)
#include "API/Metal.hlsl"
#elif defined(SHADER_API_VULKAN)
#include "API/Vulkan.hlsl"
#elif defined(SHADER_API_GLCORE)
#include "API/GLCore.hlsl"
#elif defined(SHADER_API_GLES3)
#include "API/GLES3.hlsl"
#elif defined(SHADER_API_GLES)
#include "API/GLES2.hlsl"
#else
#error unsupported shader api
#endif
#include "API/Validate.hlsl"
#include "Macros.hlsl"
#include "Random.hlsl"
// ----------------------------------------------------------------------------
// Common intrinsic (general implementation of intrinsic available on some platform)
// ----------------------------------------------------------------------------
// Error on GLES2 undefined functions
#ifdef SHADER_API_GLES
#define BitFieldExtract ERROR_ON_UNSUPPORTED_FUNC(BitFieldExtract)
#define IsBitSet ERROR_ON_UNSUPPORTED_FUNC(IsBitSet)
#define SetBit ERROR_ON_UNSUPPORTED_FUNC(SetBit)
#define ClearBit ERROR_ON_UNSUPPORTED_FUNC(ClearBit)
#define ToggleBit ERROR_ON_UNSUPPORTED_FUNC(ToggleBit)
#define FastMulBySignOfNegZero ERROR_ON_UNSUPPORTED_FUNC(FastMulBySignOfNegZero)
#define LODDitheringTransition ERROR_ON_UNSUPPORTED_FUNC(LODDitheringTransition)
#endif
#if !defined(SHADER_API_GLES)
#ifndef INTRINSIC_BITFIELD_EXTRACT
// Unsigned integer bit field extraction.
// Note that the intrinsic itself generates a vector instruction.
// Wrap this function with WaveReadFirstLane() to get scalar output.
uint BitFieldExtract(uint data, uint offset, uint numBits)
{
uint mask = (1u << numBits) - 1u;
return (data >> offset) & mask;
}
#endif // INTRINSIC_BITFIELD_EXTRACT
#ifndef INTRINSIC_BITFIELD_EXTRACT_SIGN_EXTEND
// Integer bit field extraction with sign extension.
// Note that the intrinsic itself generates a vector instruction.
// Wrap this function with WaveReadFirstLane() to get scalar output.
int BitFieldExtractSignExtend(int data, uint offset, uint numBits)
{
int shifted = data >> offset; // Sign-extending (arithmetic) shift
int signBit = shifted & (1u << (numBits - 1u));
uint mask = (1u << numBits) - 1u;
return -signBit | (shifted & mask); // Use 2-complement for negation to replicate the sign bit
}
#endif // INTRINSIC_BITFIELD_EXTRACT_SIGN_EXTEND
#ifndef INTRINSIC_BITFIELD_INSERT
// Inserts the bits indicated by 'mask' from 'src' into 'dst'.
uint BitFieldInsert(uint mask, uint src, uint dst)
{
return (src & mask) | (dst & ~mask);
}
#endif // INTRINSIC_BITFIELD_INSERT
bool IsBitSet(uint data, uint offset)
{
return BitFieldExtract(data, offset, 1u) != 0;
}
void SetBit(inout uint data, uint offset)
{
data |= 1u << offset;
}
void ClearBit(inout uint data, uint offset)
{
data &= ~(1u << offset);
}
void ToggleBit(inout uint data, uint offset)
{
data ^= 1u << offset;
}
#endif
#ifndef INTRINSIC_WAVEREADFIRSTLANE
// Warning: for correctness, the argument must have the same value across the wave!
TEMPLATE_1_FLT(WaveReadFirstLane, scalarValue, return scalarValue)
TEMPLATE_1_INT(WaveReadFirstLane, scalarValue, return scalarValue)
#endif
#ifndef INTRINSIC_MUL24
TEMPLATE_2_INT(Mul24, a, b, return a * b)
#endif // INTRINSIC_MUL24
#ifndef INTRINSIC_MAD24
TEMPLATE_3_INT(Mad24, a, b, c, return a * b + c)
#endif // INTRINSIC_MAD24
#ifndef INTRINSIC_MINMAX3
TEMPLATE_3_FLT(Min3, a, b, c, return min(min(a, b), c))
TEMPLATE_3_INT(Min3, a, b, c, return min(min(a, b), c))
TEMPLATE_3_FLT(Max3, a, b, c, return max(max(a, b), c))
TEMPLATE_3_INT(Max3, a, b, c, return max(max(a, b), c))
#endif // INTRINSIC_MINMAX3
TEMPLATE_SWAP(Swap) // Define a Swap(a, b) function for all types
#define CUBEMAPFACE_POSITIVE_X 0
#define CUBEMAPFACE_NEGATIVE_X 1
#define CUBEMAPFACE_POSITIVE_Y 2
#define CUBEMAPFACE_NEGATIVE_Y 3
#define CUBEMAPFACE_POSITIVE_Z 4
#define CUBEMAPFACE_NEGATIVE_Z 5
#ifndef INTRINSIC_CUBEMAP_FACE_ID
// TODO: implement this. Is the reference implementation of cubemapID provide by AMD the reverse of our ?
/*
real CubemapFaceID(real3 dir)
{
real faceID;
if (abs(dir.z) >= abs(dir.x) && abs(dir.z) >= abs(dir.y))
{
faceID = (dir.z < 0.0) ? 5.0 : 4.0;
}
else if (abs(dir.y) >= abs(dir.x))
{
faceID = (dir.y < 0.0) ? 3.0 : 2.0;
}
else
{
faceID = (dir.x < 0.0) ? 1.0 : 0.0;
}
return faceID;
}
*/
void GetCubeFaceID(real3 dir, out int faceIndex)
{
// TODO: Use faceID intrinsic on console
real3 adir = abs(dir);
// +Z -Z
faceIndex = dir.z > 0.0 ? CUBEMAPFACE_NEGATIVE_Z : CUBEMAPFACE_POSITIVE_Z;
// +X -X
if (adir.x > adir.y && adir.x > adir.z)
{
faceIndex = dir.x > 0.0 ? CUBEMAPFACE_NEGATIVE_X : CUBEMAPFACE_POSITIVE_X;
}
// +Y -Y
else if (adir.y > adir.x && adir.y > adir.z)
{
faceIndex = dir.y > 0.0 ? CUBEMAPFACE_NEGATIVE_Y : CUBEMAPFACE_POSITIVE_Y;
}
}
#endif // INTRINSIC_CUBEMAP_FACE_ID
// ----------------------------------------------------------------------------
// Common math functions
// ----------------------------------------------------------------------------
real DegToRad(real deg)
{
return deg * (PI / 180.0);
}
real RadToDeg(real rad)
{
return rad * (180.0 / PI);
}
// Square functions for cleaner code
TEMPLATE_1_FLT(Sq, x, return x * x)
TEMPLATE_1_INT(Sq, x, return x * x)
// Input [0, 1] and output [0, PI/2]
// 9 VALU
real FastACosPos(real inX)
{
real x = abs(inX);
real res = (0.0468878 * x + -0.203471) * x + 1.570796; // p(x)
res *= sqrt(1.0 - x);
return res;
}
// Ref: https://seblagarde.wordpress.com/2014/12/01/inverse-trigonometric-functions-gpu-optimization-for-amd-gcn-architecture/
// Input [-1, 1] and output [0, PI]
// 12 VALU
real FastACos(real inX)
{
real res = FastACosPos(inX);
return (inX >= 0) ? res : PI - res; // Undo range reduction
}
// Same cost as Acos + 1 FR
// Same error
// input [-1, 1] and output [-PI/2, PI/2]
real FastASin(real x)
{
return HALF_PI - FastACos(x);
}
// max absolute error 1.3x10^-3
// Eberly's odd polynomial degree 5 - respect bounds
// 4 VGPR, 14 FR (10 FR, 1 QR), 2 scalar
// input [0, infinity] and output [0, PI/2]
real FastATanPos(real x)
{
real t0 = (x < 1.0) ? x : 1.0 / x;
real t1 = t0 * t0;
real poly = 0.0872929;
poly = -0.301895 + poly * t1;
poly = 1.0 + poly * t1;
poly = poly * t0;
return (x < 1.0) ? poly : HALF_PI - poly;
}
#if (SHADER_TARGET >= 45)
uint FastLog2(uint x)
{
return firstbithigh(x) - 1u;
}
#endif
// 4 VGPR, 16 FR (12 FR, 1 QR), 2 scalar
// input [-infinity, infinity] and output [-PI/2, PI/2]
real FastATan(real x)
{
real t0 = FastATanPos(abs(x));
return (x < 0.0) ? -t0 : t0;
}
// Using pow often result to a warning like this
// "pow(f, e) will not work for negative f, use abs(f) or conditionally handle negative values if you expect them"
// PositivePow remove this warning when you know the value is positive and avoid inf/NAN.
TEMPLATE_2_FLT(PositivePow, base, power, return pow(max(abs(base), FLT_EPS), power))
// Computes (FastSign(s) * x) using 2x VALU.
// See the comment about FastSign() below.
real FastMulBySignOf(real s, real x, bool ignoreNegZero = true)
{
#if !defined(SHADER_API_GLES)
if (ignoreNegZero)
{
return (s >= 0) ? x : -x;
}
else
{
uint negZero = 0x80000000u;
uint signBit = negZero & asuint(s);
return asfloat(signBit ^ asuint(x));
}
#else
return (s >= 0) ? x : -x;
#endif
}
// Returns -1 for negative numbers and 1 for positive numbers.
// 0 can be handled in 2 different ways.
// The IEEE floating point standard defines 0 as signed: +0 and -0.
// However, mathematics typically treats 0 as unsigned.
// Therefore, we treat -0 as +0 by default: FastSign(+0) = FastSign(-0) = 1.
// If (ignoreNegZero = false), FastSign(-0, false) = -1.
// Note that the sign() function in HLSL implements signum, which returns 0 for 0.
real FastSign(real s, bool ignoreNegZero = true)
{
return FastMulBySignOf(s, 1.0, ignoreNegZero);
}
// Orthonormalizes the tangent frame using the Gram-Schmidt process.
// We assume that both the tangent and the normal are normalized.
// Returns the new tangent (the normal is unaffected).
real3 Orthonormalize(real3 tangent, real3 normal)
{
return normalize(tangent - dot(tangent, normal) * normal);
}
// Same as smoothstep except it assume 0, 1 interval for x
real Smoothstep01(real x)
{
return x * x * (3.0 - (2.0 * x));
}
// ----------------------------------------------------------------------------
// Texture utilities
// ----------------------------------------------------------------------------
real ComputeTextureLOD(real2 uv)
{
real2 ddx_ = ddx(uv);
real2 ddy_ = ddy(uv);
real d = max(dot(ddx_, ddx_), dot(ddy_, ddy_));
return max(0.5 * log2(d), 0.0);
}
// texelSize is Unity XXX_TexelSize feature parameters
// x contains 1.0/width, y contains 1.0 / height, z contains width, w contains height
real ComputeTextureLOD(real2 uv, real4 texelSize)
{
uv *= texelSize.zw;
return ComputeTextureLOD(uv);
}
// ----------------------------------------------------------------------------
// Texture format sampling
// ----------------------------------------------------------------------------
real2 DirectionToLatLongCoordinate(real3 unDir)
{
real3 dir = normalize(unDir);
// coordinate frame is (-Z, X) meaning negative Z is primary axis and X is secondary axis.
return real2(1.0 - 0.5 * INV_PI * atan2(dir.x, -dir.z), asin(dir.y) * INV_PI + 0.5);
}
real3 LatlongToDirectionCoordinate(real2 coord)
{
real theta = coord.y * PI;
real phi = (coord.x * 2.f * PI - PI*0.5f);
real cosTheta = cos(theta);
real sinTheta = sqrt(1.0 - min(1.0, cosTheta*cosTheta));
real cosPhi = cos(phi);
real sinPhi = sin(phi);
real3 direction = real3(sinTheta*cosPhi, cosTheta, sinTheta*sinPhi);
direction.xy *= -1.0;
return direction;
}
// ----------------------------------------------------------------------------
// Depth encoding/decoding
// ----------------------------------------------------------------------------
// Z buffer to linear 0..1 depth (0 at near plane, 1 at far plane).
// Does not correctly handle oblique view frustums.
real Linear01DepthFromNear(real depth, real4 zBufferParam)
{
return 1.0 / (zBufferParam.x + zBufferParam.y / depth);
}
// Z buffer to linear 0..1 depth (0 at camera position, 1 at far plane).
// Does not correctly handle oblique view frustums.
real Linear01Depth(real depth, real4 zBufferParam)
{
return 1.0 / (zBufferParam.x * depth + zBufferParam.y);
}
// Z buffer to linear depth.
// Does not correctly handle oblique view frustums.
real LinearEyeDepth(real depth, real4 zBufferParam)
{
return 1.0 / (zBufferParam.z * depth + zBufferParam.w);
}
// Z buffer to linear depth.
// Correctly handles oblique view frustums. Only valid for projection matrices!
// Ref: An Efficient Depth Linearization Method for Oblique View Frustums, Eq. 6.
real LinearEyeDepth(real2 positionNDC, real deviceDepth, real4 invProjParam)
{
real4 positionCS = real4(positionNDC * 2.0 - 1.0, deviceDepth, 1.0);
real viewSpaceZ = rcp(dot(positionCS, invProjParam));
// The view space uses a right-handed coordinate system.
return -viewSpaceZ;
}
// Z buffer to linear depth.
// Correctly handles oblique view frustums.
// Typically, this is the cheapest variant, provided you've already computed 'positionWS'.
real LinearEyeDepth(real3 positionWS, real4x4 viewProjMatrix)
{
return mul(viewProjMatrix, real4(positionWS, 1.0)).w;
}
// ----------------------------------------------------------------------------
// Space transformations
// ----------------------------------------------------------------------------
static const real3x3 k_identity3x3 = { 1, 0, 0,
0, 1, 0,
0, 0, 1};
static const real4x4 k_identity4x4 = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1};
// Use case examples:
// (position = positionCS) => (clipSpaceTransform = use default)
// (position = positionVS) => (clipSpaceTransform = UNITY_MATRIX_P)
// (position = positionWS) => (clipSpaceTransform = UNITY_MATRIX_VP)
real2 ComputeNormalizedDeviceCoordinates(real3 position, real4x4 clipSpaceTransform = k_identity4x4)
{
real4 positionCS = mul(clipSpaceTransform, real4(position, 1.0));
real2 positionNDC = positionCS.xy * (rcp(positionCS.w) * 0.5) + 0.5;
#if UNITY_UV_STARTS_AT_TOP
positionNDC.y = 1.0 - positionNDC.y;
#endif
return positionNDC;
}
real4 ComputeClipSpacePosition(real2 positionNDC, real deviceDepth)
{
#if UNITY_UV_STARTS_AT_TOP
positionNDC.y = 1.0 - positionNDC.y;
#endif
return real4(positionNDC * 2.0 - 1.0, deviceDepth, 1.0);
}
real3 ComputeViewSpacePosition(real2 positionNDC, real deviceDepth, real4x4 invProjMatrix)
{
real4 positionCS = ComputeClipSpacePosition(positionNDC, deviceDepth);
real4 positionVS = mul(invProjMatrix, positionCS);
// The view space uses a right-handed coordinate system.
positionVS.z = -positionVS.z;
return positionVS.xyz / positionVS.w;
}
real3 ComputeWorldSpacePosition(real2 positionNDC, real deviceDepth, real4x4 invViewProjMatrix)
{
real4 positionCS = ComputeClipSpacePosition(positionNDC, deviceDepth);
real4 hpositionWS = mul(invViewProjMatrix, positionCS);
return hpositionWS.xyz / hpositionWS.w;
}
// ----------------------------------------------------------------------------
// PositionInputs
// ----------------------------------------------------------------------------
struct PositionInputs
{
real3 positionWS; // World space position (could be camera-relative)
real2 positionNDC; // Normalized screen UVs : [0, 1) (with the real-pixel offset)
uint2 positionSS; // Screen space pixel coordinates : [0, NumPixels)
uint2 tileCoord; // Screen tile coordinates : [0, NumTiles)
real deviceDepth; // Depth from the depth buffer : [0, 1] (typically reversed)
real linearDepth; // View space Z coordinate : [Near, Far]
};
// This function is use to provide an easy way to sample into a screen texture, either from a pixel or a compute shaders.
// This allow to easily share code.
// If a compute shader call this function positionSS is an integer usually calculate like: uint2 positionSS = groupId.xy * BLOCK_SIZE + groupThreadId.xy
// else it is current unormalized screen coordinate like return by SV_Position
PositionInputs GetPositionInput(real2 positionSS, real2 invScreenSize, uint2 tileCoord) // Specify explicit tile coordinates so that we can easily make it lane invariant for compute evaluation.
{
PositionInputs posInput;
ZERO_INITIALIZE(PositionInputs, posInput);
posInput.positionNDC = positionSS;
#if SHADER_STAGE_COMPUTE
// In case of compute shader an extra real offset is added to the screenPos to shift the integer position to pixel center.
posInput.positionNDC.xy += real2(0.5, 0.5);
#endif
posInput.positionNDC *= invScreenSize;
posInput.positionSS = uint2(positionSS);
posInput.tileCoord = tileCoord;
return posInput;
}
PositionInputs GetPositionInput(real2 positionSS, real2 invScreenSize)
{
return GetPositionInput(positionSS, invScreenSize, uint2(0, 0));
}
// From forward
// deviceDepth and linearDepth come directly from .zw of SV_Position
void UpdatePositionInput(real deviceDepth, real linearDepth, real3 positionWS, inout PositionInputs posInput)
{
posInput.deviceDepth = deviceDepth;
posInput.linearDepth = linearDepth;
posInput.positionWS = positionWS;
}
// From deferred or compute shader
// depth must be the depth from the raw depth buffer. This allow to handle all kind of depth automatically with the inverse view projection matrix.
// For information. In Unity Depth is always in range 0..1 (even on OpenGL) but can be reversed.
void UpdatePositionInput(real deviceDepth, real4x4 invViewProjMatrix, real4x4 viewProjMatrix, inout PositionInputs posInput)
{
posInput.deviceDepth = deviceDepth;
posInput.positionWS = ComputeWorldSpacePosition(posInput.positionNDC, deviceDepth, invViewProjMatrix);
// The compiler should optimize this (less expensive than reconstruct depth VS from depth buffer)
posInput.linearDepth = mul(viewProjMatrix, real4(posInput.positionWS, 1.0)).w;
}
// The view direction 'V' points towards the camera.
// 'depthOffsetVS' is always applied in the opposite direction (-V).
void ApplyDepthOffsetPositionInput(real3 V, real depthOffsetVS, real4x4 viewProjMatrix, inout PositionInputs posInput)
{
posInput.positionWS += depthOffsetVS * (-V);
real4 positionCS = mul(viewProjMatrix, real4(posInput.positionWS, 1.0));
posInput.linearDepth = positionCS.w;
posInput.deviceDepth = positionCS.z / positionCS.w;
}
// ----------------------------------------------------------------------------
// Misc utilities
// ----------------------------------------------------------------------------
// Normalize that account for vectors with zero length
real3 SafeNormalize(real3 inVec)
{
real dp3 = max(REAL_MIN, dot(inVec, inVec));
return inVec * rsqrt(dp3);
}
// Generates a triangle in homogeneous clip space, s.t.
// v0 = (-1, -1, 1), v1 = (3, -1, 1), v2 = (-1, 3, 1).
real2 GetFullScreenTriangleTexCoord(uint vertexID)
{
#if UNITY_UV_STARTS_AT_TOP
return real2((vertexID << 1) & 2, 1.0 - (vertexID & 2));
#else
return real2((vertexID << 1) & 2, vertexID & 2);
#endif
}
real4 GetFullScreenTriangleVertexPosition(uint vertexID, real z = UNITY_NEAR_CLIP_VALUE)
{
real2 uv = real2((vertexID << 1) & 2, vertexID & 2);
return real4(uv * 2.0 - 1.0, z, 1.0);
}
#if !defined(SHADER_API_GLES)
// LOD dithering transition helper
// LOD0 must use this function with ditherFactor 1..0
// LOD1 must use this function with ditherFactor 0..1
void LODDitheringTransition(uint2 positionSS, real ditherFactor)
{
// Generate a spatially varying pattern.
// Unfortunately, varying the pattern with time confuses the TAA, increasing the amount of noise.
real p = GenerateHashedRandomFloat(positionSS);
// We want to have a symmetry between 0..0.5 ditherFactor and 0.5..1 so no pixels are transparent during the transition
// this is handled by this test which reverse the pattern
p = (ditherFactor >= 0.5) ? p : 1 - p;
clip(ditherFactor - p);
}
#endif
#endif // UNITY_COMMON_INCLUDED