浏览代码

Remove UnityPerCamera, introduce UnityPerView

/main
Evgenii Golubev 7 年前
当前提交
2202a083
共有 17 个文件被更改,包括 148 次插入102 次删除
  1. 84
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Camera/HDCamera.cs
  2. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugColorPicker.shader
  3. 7
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugFullScreen.shader
  4. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugViewTiles.shader
  5. 25
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs
  6. 20
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDStringConstants.cs
  7. 5
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightLoop/LightLoop.cs
  8. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Volumetrics/VolumetricLighting.cs
  9. 4
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl
  10. 6
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/SubsurfaceScattering/SubsurfaceScattering.compute
  11. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/SubsurfaceScattering/SubsurfaceScattering.shader
  12. 3
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/SubsurfaceScattering/SubsurfaceScatteringManager.cs
  13. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderPass/ShaderPassVelocity.hlsl
  14. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderPass/VaryingMesh.hlsl
  15. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderPass/VertMesh.hlsl
  16. 67
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderVariables.hlsl
  17. 15
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderVariablesFunctions.hlsl

84
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Camera/HDCamera.cs


public Matrix4x4 viewMatrix;
public Matrix4x4 projMatrix;
public Matrix4x4 nonJitteredProjMatrix;
public Vector4 screenSize;
public Frustum frustum;
public Vector4 cameraPositionWS;
public float viewParam;
public Vector4 screenSize;
public Frustum frustum;
public Camera camera;
public uint taaFrameIndex;
public Vector2 taaFrameRotation;
public Vector4 viewParam;
public Camera camera;
public uint taaFrameIndex;
public Vector2 taaFrameRotation;
public Vector4 depthBufferParam;
public PostProcessRenderContext postprocessRenderContext;
public Matrix4x4[] viewMatrixStereo;

// In stereo, this corresponds to the center eye position
var pos = camera.transform.position;
cameraPositionWS = pos;
if (ShaderConfig.s_CameraRelativeRendering != 0)
{

projMatrix = gpuProj;
nonJitteredProjMatrix = gpuNonJitteredProj;
cameraPos = pos;
viewParam = new Vector4(viewMatrix.determinant, 0.0f, 0.0f, 0.0f);
viewParam = viewMatrix.determinant;
if (ShaderConfig.s_CameraRelativeRendering != 0)
{

frustum = Frustum.Create(viewProjMatrix, true, true);
float n = camera.nearClipPlane;
float f = camera.farClipPlane;
// Analyze the projection matrix.
// p[2][3] = (reverseZ ? 1 : -1) * (depth_0_1 ? 1 : 2) * (f * n) / (f - n)
float scale = projMatrix[2, 3] / (f * n) * (f - n);
bool depth_0_1 = Mathf.Abs(scale) < 1.5f;
bool reverseZ = scale > 0;
// http://www.humus.name/temp/Linearize%20depth.txt
if (reverseZ)
{
depthBufferParam = new Vector4(-1 + f/n, 1, -1/f + 1/n, 1/f);
}
else
{
depthBufferParam = new Vector4(1 - f/n, f/n, 1/f - 1/n, 1/n);
}
frustum = Frustum.Create(viewProjMatrix, depth_0_1, reverseZ);
// Left, right, top, bottom, near, far.
for (int i = 0; i < 6; i++)

var stereoCombinedProjMatrix = cullingParams.cullStereoProj;
projMatrix = GL.GetGPUProjectionMatrix(stereoCombinedProjMatrix, true);
viewParam = new Vector4(viewMatrix.determinant, 0.0f, 0.0f, 0.0f);
viewParam = viewMatrix.determinant;
frustum = Frustum.Create(viewProjMatrix, true, true);

s_Cleanup.Clear();
}
// Set up UnityPerView CBuffer.
cmd.SetGlobalMatrix(HDShaderIDs._ViewMatrix, viewMatrix);
cmd.SetGlobalMatrix(HDShaderIDs._InvViewMatrix, viewMatrix.inverse);
cmd.SetGlobalMatrix(HDShaderIDs._ProjMatrix, projMatrix);
cmd.SetGlobalMatrix(HDShaderIDs._InvProjMatrix, projMatrix.inverse);
cmd.SetGlobalMatrix(HDShaderIDs._ViewMatrix, viewMatrix);
cmd.SetGlobalMatrix(HDShaderIDs._InvViewMatrix, viewMatrix.inverse);
cmd.SetGlobalMatrix(HDShaderIDs._ProjMatrix, projMatrix);
cmd.SetGlobalMatrix(HDShaderIDs._InvProjMatrix, projMatrix.inverse);
cmd.SetGlobalMatrix(HDShaderIDs._ViewProjMatrix, viewProjMatrix);
cmd.SetGlobalMatrix(HDShaderIDs._InvViewProjMatrix, viewProjMatrix.inverse);
cmd.SetGlobalMatrix(HDShaderIDs._ViewProjMatrix, viewProjMatrix);
cmd.SetGlobalMatrix(HDShaderIDs._InvViewProjMatrix, viewProjMatrix.inverse);
cmd.SetGlobalVector(HDShaderIDs._ViewParam, viewParam);
cmd.SetGlobalVector(HDShaderIDs._InvProjParam, invProjParam);
cmd.SetGlobalVector(HDShaderIDs._ScreenSize, screenSize);
cmd.SetGlobalVector(HDShaderIDs._ScreenToTargetScale, scaleBias);
cmd.SetGlobalMatrix(HDShaderIDs._PrevViewProjMatrix, prevViewProjMatrix);
cmd.SetGlobalVectorArray(HDShaderIDs._FrustumPlanes, frustumPlaneEquations);
cmd.SetGlobalInt(HDShaderIDs._TaaFrameIndex, (int)taaFrameIndex);
cmd.SetGlobalVector(HDShaderIDs._TaaFrameRotation, taaFrameRotation);
cmd.SetGlobalMatrix(HDShaderIDs._PrevViewProjMatrix, prevViewProjMatrix);
cmd.SetGlobalVector(HDShaderIDs._CameraPositionWS, cameraPositionWS);
cmd.SetGlobalFloat( HDShaderIDs._ViewParam, viewParam);
cmd.SetGlobalVector(HDShaderIDs._ScreenSize, screenSize);
cmd.SetGlobalVector(HDShaderIDs._ScreenToTargetScale, scaleBias);
cmd.SetGlobalVector(HDShaderIDs._DepthBufferParam, depthBufferParam);
cmd.SetGlobalVector(HDShaderIDs._InvProjParam, invProjParam);
cmd.SetGlobalVector(HDShaderIDs._TaaFrameRotation, taaFrameRotation);
cmd.SetGlobalVectorArray(HDShaderIDs._FrustumPlanes, frustumPlaneEquations);
}
public void SetupGlobalStereoParams(CommandBuffer cmd)

cmd.SetGlobalMatrixArray(HDShaderIDs._InvViewMatrixStereo, invViewStereo);
cmd.SetGlobalMatrixArray(HDShaderIDs._InvProjMatrixStereo, invProjStereo);
cmd.SetGlobalMatrixArray(HDShaderIDs._InvViewProjMatrixStereo, invViewProjStereo);
}
// TODO: We should set all the value below globally and not let it under the control of Unity,
// Need to test that because we are not sure in which order these value are setup, but we need to have control on them, or rename them in our shader.
// For now, apply it for all our compute shader to make it work
public void SetupComputeShader(ComputeShader cs, CommandBuffer cmd)
{
// Copy values set by Unity which are not configured in scripts.
cmd.SetComputeVectorParam(cs, HDShaderIDs.unity_OrthoParams, Shader.GetGlobalVector(HDShaderIDs.unity_OrthoParams));
cmd.SetComputeVectorParam(cs, HDShaderIDs._ProjectionParams, Shader.GetGlobalVector(HDShaderIDs._ProjectionParams));
cmd.SetComputeVectorParam(cs, HDShaderIDs._ScreenParams, Shader.GetGlobalVector(HDShaderIDs._ScreenParams));
cmd.SetComputeVectorParam(cs, HDShaderIDs._ZBufferParams, Shader.GetGlobalVector(HDShaderIDs._ZBufferParams));
cmd.SetComputeVectorParam(cs, HDShaderIDs._WorldSpaceCameraPos, Shader.GetGlobalVector(HDShaderIDs._WorldSpaceCameraPos));
}
}
}

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugColorPicker.shader


float4 mousePixelCoord = _MousePixelCoord;
if (_RequireToFlipInputTexture > 0.0)
{
mousePixelCoord.y = _ScreenParams.y - mousePixelCoord.y;
mousePixelCoord.y = _ScreenSize.y - mousePixelCoord.y;
// Note: We must not flip the mousePixelCoord.w coordinate
}

7
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugFullScreen.shader


const float kGrid = 64.0;
// Arrow grid (aspect ratio is kept)
float rows = floor(kGrid * _ScreenParams.y / _ScreenParams.x);
float aspect = _ScreenSize.y * _ScreenSize.z;
float rows = floor(kGrid * aspect);
float2 size = _ScreenParams.xy / float2(cols, rows);
float2 size = _ScreenSize.xy / float2(cols, rows);
float body = min(size.x, size.y) / sqrt(2.0);
float2 texcoord = input.positionCS.xy;
float2 center = (floor(texcoord / size) + 0.5) * size;

float2 arrow_coord = center / _ScreenParams.xy;
float2 arrow_coord = center * _ScreenSize.zw;
if (_RequireToFlipInputTexture > 0.0)
{

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugViewTiles.shader


uint2 tileCoord = uint2(tileIndex & 0xFFFF, tileIndex >> 16);
uint2 pixelCoord = (tileCoord + uint2((quadVertex+1) & 1, (quadVertex >> 1) & 1)) * tileSize;
float2 clipCoord = (pixelCoord / _ScreenParams.xy) * 2.0 - 1.0;
float2 clipCoord = (pixelCoord * _ScreenSize.zw) * 2.0 - 1.0;
clipCoord.y *= -1;
Varyings output;

25
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs


int m_CurrentHeight;
// Use to detect frame changes
int m_FrameCount;
int m_FrameCount;
float m_TimeSinceLevelLoad;
public int GetCurrentShadowCount() { return m_LightLoop.GetCurrentShadowCount(); }
public int GetShadowAtlasCount() { return m_LightLoop.GetShadowAtlasCount(); }

{
using (new ProfilingSample(cmd, "Push Global Parameters", CustomSamplerId.PushGlobalParameters.GetSampler()))
{
hdCamera.SetupGlobalParams(cmd);
if (m_FrameSettings.enableStereo)
hdCamera.SetupGlobalStereoParams(cmd);
float ct = Time.timeSinceLevelLoad;
float pt = (m_TimeSinceLevelLoad > 0) ? m_TimeSinceLevelLoad : ct;
float dt = Time.deltaTime;
float sdt = Time.smoothDeltaTime;
// Set up UnityPerFrame CBuffer.
cmd.SetGlobalVector(HDShaderIDs._CurrentTime, new Vector4(ct * 0.05f, ct, ct * 2.0f, ct * 3.0f));
cmd.SetGlobalVector(HDShaderIDs._PreviousTime, new Vector4(pt * 0.05f, pt, pt * 2.0f, pt * 3.0f));
cmd.SetGlobalVector(HDShaderIDs._DeltaTime, new Vector4(dt, 1.0f / dt, sdt, 1.0f / sdt));
cmd.SetGlobalVector(HDShaderIDs._SinCurrentTime, new Vector4(Mathf.Sin(ct * 0.125f), Mathf.Sin(ct * 0.25f), Mathf.Sin(ct * 0.5f), Mathf.Sin(ct)));
cmd.SetGlobalVector(HDShaderIDs._CosCurrentTime, new Vector4(Mathf.Cos(ct * 0.125f), Mathf.Cos(ct * 0.25f), Mathf.Cos(ct * 0.5f), Mathf.Cos(ct)));
m_SSSBufferManager.PushGlobalParams(cmd, sssParameters, m_FrameSettings);

// Set up UnityPerView CBuffer.
hdCamera.SetupGlobalParams(cmd);
if (m_FrameSettings.enableStereo) hdCamera.SetupGlobalStereoParams(cmd);
// Update the current time.
m_TimeSinceLevelLoad = ct;
}
}

cmd.SetComputeTextureParam(m_applyDistortionCS, m_applyDistortionKernel, HDShaderIDs._GaussianPyramidColorTexture, m_BufferPyramid.colorPyramid);
cmd.SetComputeTextureParam(m_applyDistortionCS, m_applyDistortionKernel, HDShaderIDs._CameraColorTexture, m_CameraColorBuffer);
cmd.SetComputeVectorParam(m_applyDistortionCS, HDShaderIDs._Size, size);
cmd.SetComputeVectorParam(m_applyDistortionCS, HDShaderIDs._ZBufferParams, Shader.GetGlobalVector(HDShaderIDs._ZBufferParams));
cmd.DispatchCompute(m_applyDistortionCS, m_applyDistortionKernel, Mathf.CeilToInt(size.x / x), Mathf.CeilToInt(size.y / y), 1);
}

20
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDStringConstants.cs


public static readonly int _DebugMipMapMode = Shader.PropertyToID("_DebugMipMapMode");
public static readonly int _UseTileLightList = Shader.PropertyToID("_UseTileLightList");
public static readonly int _Time = Shader.PropertyToID("_Time");
public static readonly int _SinTime = Shader.PropertyToID("_SinTime");
public static readonly int _CosTime = Shader.PropertyToID("_CosTime");
public static readonly int unity_DeltaTime = Shader.PropertyToID("unity_DeltaTime");
public static readonly int _CurrentTime = Shader.PropertyToID("_CurrentTime");
public static readonly int _PreviousTime = Shader.PropertyToID("_PreviousTime");
public static readonly int _SinCurrentTime = Shader.PropertyToID("_SinCurrentTime");
public static readonly int _CosCurrentTime = Shader.PropertyToID("_CosCurrentTime");
public static readonly int _DeltaTime = Shader.PropertyToID("_DeltaTime");
public static readonly int _EnvLightSkyEnabled = Shader.PropertyToID("_EnvLightSkyEnabled");
public static readonly int _AmbientOcclusionParam = Shader.PropertyToID("_AmbientOcclusionParam");
public static readonly int _SkyTexture = Shader.PropertyToID("_SkyTexture");

public static readonly int _DirectionalContactShadowSampleCount = Shader.PropertyToID("_SampleCount");
public static readonly int _DirectionalLightDirection = Shader.PropertyToID("_LightDirection");
public static readonly int unity_OrthoParams = Shader.PropertyToID("unity_OrthoParams");
public static readonly int _ZBufferParams = Shader.PropertyToID("_ZBufferParams");
public static readonly int _ScreenParams = Shader.PropertyToID("_ScreenParams");
public static readonly int _ProjectionParams = Shader.PropertyToID("_ProjectionParams");
public static readonly int _WorldSpaceCameraPos = Shader.PropertyToID("_WorldSpaceCameraPos");
public static readonly int _StencilMask = Shader.PropertyToID("_StencilMask");
public static readonly int _StencilRef = Shader.PropertyToID("_StencilRef");
public static readonly int _StencilCmp = Shader.PropertyToID("_StencilCmp");

public static readonly int _DecalCount = Shader.PropertyToID("_DecalCount");
public static readonly int _DecalDatas = Shader.PropertyToID("_DecalDatas");
public static readonly int _CameraPositionWS = Shader.PropertyToID("_CameraPositionWS");
public static readonly int _ViewMatrix = Shader.PropertyToID("_ViewMatrix");
public static readonly int _InvViewMatrix = Shader.PropertyToID("_InvViewMatrix");
public static readonly int _ProjMatrix = Shader.PropertyToID("_ProjMatrix");

public static readonly int _InvViewProjMatrix = Shader.PropertyToID("_InvViewProjMatrix");
public static readonly int _ViewParam = Shader.PropertyToID("_ViewParam");
public static readonly int _DepthBufferParam = Shader.PropertyToID("_DepthBufferParam");
public static readonly int _TaaFrameIndex = Shader.PropertyToID("_TaaFrameIndex");
public static readonly int _TaaFrameRotation = Shader.PropertyToID("_TaaFrameRotation");
public static readonly int _ViewMatrixStereo = Shader.PropertyToID("_ViewMatrixStereo");

5
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightLoop/LightLoop.cs


int numTilesX = (hdCamera.actualWidth + (deferredShadowTileSize - 1)) / deferredShadowTileSize;
int numTilesY = (hdCamera.actualHeight + (deferredShadowTileSize - 1)) / deferredShadowTileSize;
hdCamera.SetupComputeShader(deferredDirectionalShadowComputeShader, cmd);
// TODO: Update for stereo
cmd.DispatchCompute(deferredDirectionalShadowComputeShader, kernel, numTilesX, numTilesY, 1);

int numVariants = 1;
if (enableFeatureVariants)
numVariants = LightDefinitions.s_NumFeatureVariants;
hdCamera.SetupComputeShader(deferredComputeShader, cmd);
for (int variant = 0; variant < numVariants; variant++)
{

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Volumetrics/VolumetricLighting.cs


Vector4 resolution = new Vector4(w, h, 1.0f / w, 1.0f / h);
Matrix4x4 transform = HDUtils.ComputePixelCoordToWorldSpaceViewDirectionMatrix(vFoV, resolution, camera.viewMatrix, false);
camera.SetupComputeShader(m_VolumetricLightingCS, cmd);
Vector2[] xySeq = GetHexagonalClosePackedSpheres7();
// This is a sequence of 7 equidistant numbers from 1/14 to 13/14.

4
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl


// Get the depth of the approximated back plane
float pyramidDepth = LOAD_TEXTURE2D_LOD(_PyramidDepthTexture, posInputs.positionNDC * (depthSize >> 2), 2).r;
float depth = LinearEyeDepth(pyramidDepth, _ZBufferParams);
float depth = LinearEyeDepth(pyramidDepth, _DepthBufferParam);
// Distance from point to the back plane
float depthFromPositionInput = depth - posInputs.linearDepth;

// Calculate screen space coordinates of refracted point in back plane
float2 refractedBackPointNDC = ComputeNormalizedDeviceCoordinates(refractedBackPointWS, UNITY_MATRIX_VP);
uint2 depthSize = uint2(_PyramidDepthMipSize.xy);
float refractedBackPointDepth = LinearEyeDepth(LOAD_TEXTURE2D_LOD(_PyramidDepthTexture, refractedBackPointNDC * depthSize, 0).r, _ZBufferParams);
float refractedBackPointDepth = LinearEyeDepth(LOAD_TEXTURE2D_LOD(_PyramidDepthTexture, refractedBackPointNDC * depthSize, 0).r, _DepthBufferParam);
// Exit if texel is out of color buffer
// Or if the texel is from an object in front of the object

6
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/SubsurfaceScattering/SubsurfaceScattering.compute


float3 irradiance = LOAD_TEXTURE2D(_IrradianceSource, pixelCoord).rgb;
float depth = LOAD_TEXTURE2D(_DepthTexture, pixelCoord).r;
return float4(irradiance, LinearEyeDepth(depth, _ZBufferParams));
return float4(irradiance, LinearEyeDepth(depth, _DepthBufferParam));
}
// Returns {irradiance, linearDepth}.

UNITY_BRANCH if (passedStencilTest)
{
centerDepth = LOAD_TEXTURE2D(_DepthTexture, pixelCoord).r;
centerViewZ = LinearEyeDepth(centerDepth, _ZBufferParams);
centerViewZ = LinearEyeDepth(centerDepth, _DepthBufferParam);
}
#if SSS_USE_LDS_CACHE

// Save some bandwidth by only loading depth values for SSS pixels.
UNITY_BRANCH if (TestLightingForSSS(irradiance2))
{
viewZ2 = LinearEyeDepth(LOAD_TEXTURE2D(_DepthTexture, pixelCoord2).r, _ZBufferParams);
viewZ2 = LinearEyeDepth(LOAD_TEXTURE2D(_DepthTexture, pixelCoord2).r, _DepthBufferParam);
}
// Populate the border region of the LDS cache.

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/SubsurfaceScattering/SubsurfaceScattering.shader


// Ref #1: Skin Rendering by Pseudo–Separable Cross Bilateral Filtering.
// Ref #2: Separable SSS, Supplementary Materials, Section E.
float rawDepth = LOAD_TEXTURE2D(_MainDepthTexture, samplePosition).r;
float sampleDepth = LinearEyeDepth(rawDepth, _ZBufferParams);
float sampleDepth = LinearEyeDepth(rawDepth, _DepthBufferParam);
float zDistance = centimPerUnit * sampleDepth - (centimPerUnit * centerPosVS.z);
sampleWeight *= exp(-zDistance * zDistance * halfRcpVariance);

3
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/SubsurfaceScattering/SubsurfaceScatteringManager.cs


cmd.ClearRandomWriteTargets();
}
// TODO: Remove this once fix, see comment inside the function
hdCamera.SetupComputeShader(m_SubsurfaceScatteringCS, cmd);
unsafe
{
// Warning: Unity is not able to losslessly transfer integers larger than 2^24 to the shader system.

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderPass/ShaderPassVelocity.hlsl


#endif
#if defined(HAVE_VERTEX_MODIFICATION)
ApplyVertexModification(inputMesh, normalWS, previousPositionWS, _LastTime);
ApplyVertexModification(inputMesh, normalWS, previousPositionWS, _PreviousTime);
#endif
//Need this since we are using the current position from VertMesh()

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderPass/VaryingMesh.hlsl


output.isFrontFace = IS_FRONT_VFACE(input.cullFace, true, false);
// Handle handness of the view matrix (In Unity view matrix default to a determinant of -1)
// when we render a cubemap the view matrix handness is flipped (due to convention used for cubemap) we have a determinant of +1
output.isFrontFace = _ViewParam.x < 0.0 ? output.isFrontFace : !output.isFrontFace;
output.isFrontFace = _ViewParam < 0.0 ? output.isFrontFace : !output.isFrontFace;
#endif
return output;

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderPass/VertMesh.hlsl


// TODO: deal with camera center rendering and instancing (This is the reason why we always perform two steps transform to clip space + instancing matrix)
#if defined(HAVE_VERTEX_MODIFICATION)
ApplyVertexModification(input, normalWS, positionWS, _Time);
ApplyVertexModification(input, normalWS, positionWS, _CurrentTime);
#endif
positionWS = GetCameraRelativePositionWS(positionWS);

67
ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderVariables.hlsl


// ----------------------------------------------------------------------------
/*
*****************************************************
* *
* UnityPerCamera has been deprecated. Do NOT use! *
* Refer to UnityPerFrame and UnityPerView instead. *
* *
*****************************************************
CBUFFER_START(UnityPerCamera)
// Time (t = time since current level load) values from Unity
float4 _Time; // (t/20, t, t*2, t*3)

// w = 1.0 if camera is ortho, 0.0 if perspective
float4 unity_OrthoParams;
CBUFFER_END
*/
CBUFFER_START(UnityPerCameraRare)

#endif
float4 unity_ShadowColor;
float2 _TaaFrameRotation; // {x = sin(_TaaFrameIndex * PI/2), y = cos(_TaaFrameIndex * PI/2), z = unused}
uint _TaaFrameIndex; // [0, 7]
// Time (t = time since current level load) values from Unity.
float4 _CurrentTime; // { t/20, t, t*2, t*3 }
float4 _PreviousTime; // { t/20, t, t*2, t*3 }
float4 _DeltaTime; // { dt, 1/dt, smoothdt, 1/smoothdt }
float4 _SinCurrentTime; // { sin(t/8), sin(t/4), sin(t/2), sin(t) }
float4 _CosCurrentTime; // { cos(t/8), cos(t/4), cos(t/2), cos(t) }
// Volumetric lighting.
float4 _AmbientProbeCoeffs[7]; // 3 bands of SH, packed, rescaled and convolved with the phase function
float _GlobalAsymmetry;

// ----------------------------------------------------------------------------
// TODO: all affine matrices should be 3x4.
// TODO: sort these vars by the frequency of use (descending), and put commonly used vars together.
CBUFFER_START(UnityPerPass)
float4x4 _PrevViewProjMatrix; // non-jittered
float4x4 _ViewProjMatrix;
float4x4 _NonJitteredViewProjMatrix;
float4x4 _ViewMatrix;
float4x4 _ProjMatrix;
float4x4 _InvViewProjMatrix;
float4x4 _InvViewMatrix;
float4x4 _InvProjMatrix;
float4 _ViewParam; // .x = ViewMatrix determinant
float4 _InvProjParam;
float4 _ScreenSize; // {w, h, 1/w, 1/h}
float4 _FrustumPlanes[6]; // {(a, b, c) = N, d = -dot(N, P)} [L, R, T, B, N, F]
CBUFFER_START(UnityPerView)
float4x4 _ViewMatrix;
float4x4 _InvViewMatrix;
float4x4 _ProjMatrix;
float4x4 _InvProjMatrix;
float4x4 _ViewProjMatrix;
float4x4 _InvViewProjMatrix;
float4x4 _NonJitteredViewProjMatrix;
float4x4 _PrevViewProjMatrix; // non-jittered
// TODO: sort the vars below by the frequency of use (descending), and put commonly used ones together.
// Note: a matrix is 4 * 4 * 4 = 64 bytes (1x cache line), so no need to sort those.
float3 _CameraPositionWS;
float _ViewParam; // determinant(_ViewMatrix)
float4 _ScreenSize; // { w, h, 1 / w, 1 / h }
float4 _ScreenToTargetScale; // { w / RTHandle.maxWidth, h / RTHandle.maxHeight, 0, 0 }
// Values used to linearize the Z buffer (http://www.humus.name/temp/Linearize%20depth.txt)
// x = 1-far/near
// y = far/near
// z = x/far
// w = y/far
// or in case of a reversed depth buffer (UNITY_REVERSED_Z is 1)
// x = -1+far/near
// y = 1
// z = x/far
// w = 1/far
float4 _DepthBufferParam;
float4 _InvProjParam;
// TAA Frame Index ranges from 0 to 7. This gives you two rotations per cycle.
float4 _TaaFrameRotation; // { sin(taaFrame * PI/2), cos(taaFrame * PI/2), 0, 0 }
float4 _FrustumPlanes[6]; // { (a, b, c) = N, d = -dot(N, P) } [L, R, T, B, N, F]
CBUFFER_END
// Custom generated by HDRP, not from Unity Engine (passed in via HDCamera)

15
ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderVariablesFunctions.hlsl


float3 GetAbsolutePositionWS(float3 positionWS)
{
#if (SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0)
positionWS += _WorldSpaceCameraPos;
positionWS += _CameraPositionWS;
#endif
return positionWS;
}

#if (SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0)
positionWS -= _WorldSpaceCameraPos;
positionWS -= _CameraPositionWS;
// Note: '_WorldSpaceCameraPos' is set by the legacy Unity code.
// Note: '_CameraPositionWS' is set by the legacy Unity code.
return _WorldSpaceCameraPos;
return _CameraPositionWS;
#endif
}

return GetPrimaryCameraPosition();
#else
// This is a generic solution.
// However, for the primary camera, using '_WorldSpaceCameraPos' is better for cache locality,
// However, for the primary camera, using '_CameraPositionWS' is better for cache locality,
// and in case we enable camera-relative rendering, we can statically set the position is 0.
return UNITY_MATRIX_I_V._14_24_34;
#endif

// Returns 'true' if the current view performs a perspective projection.
bool IsPerspectiveProjection()
{
#if defined(SHADERPASS) && (SHADERPASS != SHADERPASS_SHADOWS)
return (unity_OrthoParams.w == 0);
#else
// TODO: set 'unity_OrthoParams' during the shadow pass.
#endif
}
// Computes the world space view direction (pointing towards the viewer).

正在加载...
取消
保存