浏览代码

Added a kernel for contact shadows

/main
Antoine Lelievre 6 年前
当前提交
a756ab16
共有 2 个文件被更改,包括 47 次插入14 次删除
  1. 22
      com.unity.render-pipelines.high-definition/HDRP/Lighting/DeferredDirectionalShadow.compute
  2. 39
      com.unity.render-pipelines.high-definition/HDRP/Lighting/LightLoop/LightLoop.cs

22
com.unity.render-pipelines.high-definition/HDRP/Lighting/DeferredDirectionalShadow.compute


// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel DeferredDirectionalShadow DEFERRED_DIRECTIONAL=DeferredDirectionalShadow
#pragma kernel DeferredDirectionalShadow_Contact DEFERRED_DIRECTIONAL=DeferredDirectionalShadow_Contact ENABLE_CONTACT_SHADOWS
#pragma kernel DeferredContactShadow
#ifdef SHADER_API_PSSL
# pragma argument( scheduler=minpressure ) // instruct the shader compiler to prefer minimizing vgpr usage

_DeferredShadowTextureUAV[pixelCoord] = float4(shadow, 0.0, 0.0, 0.0);
}
[numthreads(DEFERRED_SHADOW_TILE_SIZE, DEFERRED_SHADOW_TILE_SIZE, 1)]
void DeferredContactShadow(uint2 groupThreadId : SV_GroupThreadID, uint2 groupId : SV_GroupID)
{
uint2 pixelCoord = groupId * DEFERRED_SHADOW_TILE_SIZE + groupThreadId;
uint2 tileCoord = groupId;
float depth = LOAD_TEXTURE2D(_CameraDepthTexture, pixelCoord.xy).x;
PositionInputs posInput = GetPositionInput(pixelCoord.xy, _ScreenSize.zw, depth, UNITY_MATRIX_I_VP, UNITY_MATRIX_V, tileCoord);
float contactShadow = 1.0f;
float shadow = 1;
if (_ContactShadowLength > 0.0f)
{
float4 result = ScreenSpaceShadowRayCast(posInput.positionWS, normalize(_LightDirection), _ContactShadowLength * max(0.5, posInput.linearDepth * _ContactShadowDistanceScaleFactor));
contactShadow = 1.0 - result.x * saturate((_ContactShadowFadeEnd - posInput.linearDepth) * _ContactShadowFadeOneOverRange);
shadow *= contactShadow;
}
_DeferredShadowTextureUAV[pixelCoord] = float4(shadow, 0.0, 0.0, 0.0);
}

39
com.unity.render-pipelines.high-definition/HDRP/Lighting/LightLoop/LightLoop.cs


static int[] s_shadeOpaqueIndirectShadowMaskFptlKernels = new int[LightDefinitions.s_NumFeatureVariants];
static int s_deferredDirectionalShadowKernel;
static int s_deferredContactShadowkernel;
static int s_deferredDirectionalShadow_Contact_Kernel;
static ComputeBuffer s_LightVolumeDataBuffer = null;

Light m_CurrentSunLight;
int m_CurrentSunLightShadowIndex = -1;
int m_DominantLightIndex = -1;
public Light GetCurrentSunLight() { return m_CurrentSunLight; }

s_deferredDirectionalShadowKernel = deferredDirectionalShadowComputeShader.FindKernel("DeferredDirectionalShadow");
s_deferredDirectionalShadow_Contact_Kernel = deferredDirectionalShadowComputeShader.FindKernel("DeferredDirectionalShadow_Contact");
s_deferredContactShadowkernel = deferredDirectionalShadowComputeShader.FindKernel("DeferredContactShadow");
for (int variant = 0; variant < LightDefinitions.s_NumFeatureVariants; variant++)
{

//TODO: move this upwards
float biggestLight = 0;
int dominantLightIndex = -1;
for (int sortIndex = 0; sortIndex < sortCount; ++sortIndex)
{

if (GetDirectionalLightData(cmd, shadowSettings, gpuLightType, light, additionalLightData, additionalShadowData, lightIndex))
{
directionalLightcount++;
if (additionalShadowData != null && additionalShadowData.contactShadows)
{
dominantLightIndex = lightIndex;
biggestLight = Single.PositiveInfinity;
}
// We make the light position camera-relative as late as possible in order
// to allow the preceding code to work with the absolute world space coordinates.

if (additionalShadowData != null && additionalShadowData.contactShadows && lightDimensions.magnitude > biggestLight)
{
dominantLightIndex = lightIndex;
m_DominantLightIndex = lightIndex;
biggestLight = lightDimensions.magnitude;
}

public void RenderDeferredDirectionalShadow(HDCamera hdCamera, RTHandleSystem.RTHandle deferredShadowRT, RenderTargetIdentifier depthTexture, CommandBuffer cmd)
{
if (m_CurrentSunLight == null || m_CurrentSunLight.GetComponent<AdditionalShadowData>() == null || m_CurrentSunLightShadowIndex < 0)
if ((m_CurrentSunLight == null || m_CurrentSunLight.GetComponent<AdditionalShadowData>() == null || m_CurrentSunLightShadowIndex < 0) && m_DominantLightIndex == -1)
{
cmd.SetGlobalTexture(HDShaderIDs._DeferredShadowTexture, RuntimeUtilities.blackTexture);
return;

{
ContactShadows contactShadows = VolumeManager.instance.stack.GetComponent<ContactShadows>();
ContactShadows contactShadows = VolumeManager.instance.stack.GetComponent<ContactShadows>();
bool enableContactShadows = m_FrameSettings.enableContactShadows && contactShadows.enable && contactShadows.length > 0.0f;
Vector3 lightDirection;
int kernel;
bool enableContactShadows = m_FrameSettings.enableContactShadows && contactShadows.enable && contactShadows.length > 0.0f;
int kernel;
// Debug.Log("contactShadowsSettings: " + contactShadows.enable);
kernel = s_deferredDirectionalShadow_Contact_Kernel;
{
Debug.Log("Contact shadows enabled !");
if (m_DominantLightIndex != -1)
{
kernel = s_deferredContactShadowkernel;
}
else
kernel = s_deferredDirectionalShadow_Contact_Kernel;
}
if (m_CurrentSunLight != null)
lightDirection = -m_CurrentSunLight.transform.forward;
else
lightDirection = Vector3.one; //TODO: put dominant light direction here
m_ShadowMgr.BindResources(cmd, deferredDirectionalShadowComputeShader, kernel);

}
cmd.SetComputeIntParam(deferredDirectionalShadowComputeShader, HDShaderIDs._DirectionalShadowIndex, m_CurrentSunLightShadowIndex);
cmd.SetComputeVectorParam(deferredDirectionalShadowComputeShader, HDShaderIDs._DirectionalLightDirection, -m_CurrentSunLight.transform.forward);
cmd.SetComputeVectorParam(deferredDirectionalShadowComputeShader, HDShaderIDs._DirectionalLightDirection, lightDirection);
cmd.SetComputeTextureParam(deferredDirectionalShadowComputeShader, kernel, HDShaderIDs._DeferredShadowTextureUAV, deferredShadowRT);
cmd.SetComputeTextureParam(deferredDirectionalShadowComputeShader, kernel, HDShaderIDs._CameraDepthTexture, depthTexture);

正在加载...
取消
保存