浏览代码

Implement the pass combining specular with diffuse and SSS

/main
Evgenii Golubev 8 年前
当前提交
4cf1213b
共有 5 个文件被更改,包括 69 次插入26 次删除
  1. 65
      Assets/ScriptableRenderLoop/HDRenderPipeline/HDRenderPipeline.cs
  2. 6
      Assets/ScriptableRenderLoop/HDRenderPipeline/Lighting/Resources/Deferred.shader
  3. 6
      Assets/ScriptableRenderLoop/HDRenderPipeline/Lighting/TilePass/Resources/shadeopaque.compute
  4. 2
      Assets/ScriptableRenderLoop/HDRenderPipeline/Lighting/TilePass/TilePass.cs
  5. 16
      Assets/ScriptableRenderLoop/HDRenderPipeline/ShaderPass/ShaderPassForward.hlsl

65
Assets/ScriptableRenderLoop/HDRenderPipeline/HDRenderPipeline.cs


ShadowRenderPass m_ShadowPass;
ComputeShader m_CombineSubsurfaceScatteringCS = null;
int m_FilterVerticalKernel = -1;
int m_FilterHorizontalAndCombineKernel = -1;
[SerializeField]
TextureSettings m_TextureSettings = TextureSettings.Default;

int m_VelocityBuffer;
int m_DistortionBuffer;
// 'm_CameraColorBuffer' contains only the specular lighting until the SSS pass, and the combined lighting afterwards.
RenderTargetIdentifier m_CameraColorBufferRT;
RenderTargetIdentifier m_CameraDiffuseLightingBufferRT;
RenderTargetIdentifier m_CameraDepthBufferRT;

m_DebugViewMaterialGBuffer = Utilities.CreateEngineMaterial("Hidden/HDRenderPipeline/DebugViewMaterialGBuffer");
m_ShadowPass = new ShadowRenderPass(m_ShadowSettings);
m_CombineSubsurfaceScatteringCS = Resources.Load<ComputeShader>("CombineSubsurfaceScattering");
m_FilterVerticalKernel = m_CombineSubsurfaceScatteringCS.FindKernel("FilterVertical");
m_FilterHorizontalAndCombineKernel = m_CombineSubsurfaceScatteringCS.FindKernel("FilterHorizontalAndCombine");
// Init Gbuffer description

renderContext.ExecuteCommandBuffer(cmd);
cmd.Dispose();
RenderTargetIdentifier[] colorRTs = { m_CameraColorBuffer, m_CameraDiffuseLightingBufferRT };
Utilities.SetRenderTarget(renderContext, colorRTs, m_CameraDepthBufferRT, ClearFlag.ClearDepth);
Utilities.SetRenderTarget(renderContext, m_CameraColorBufferRT, m_CameraDepthBufferRT, ClearFlag.ClearDepth);
}
// TEMP: As we are in development and have not all the setup pass we still clear the color in emissive buffer and gbuffer, but this will be removed later.

// Bind material data
m_LitRenderLoop.Bind();
RenderTargetIdentifier[] colorRTs = { m_CameraColorBuffer, m_CameraDiffuseLightingBufferRT };
RenderTargetIdentifier[] colorRTs = { m_CameraColorBufferRT, m_CameraDiffuseLightingBufferRT };
// Combines specular lighting and diffuse lighting with subsurface scattering.
void CombineSubsurfaceScattering(HDCamera hdCamera, ScriptableRenderContext context)
{
// Currently, forward-rendered objects do not output the split lighting information required for SSS.
if (debugParameters.ShouldUseForwardRenderingOnly()) return;
int screenWidth = (int)hdCamera.screenSize.x;
int screenHeight = (int)hdCamera.screenSize.y;
const int groupSize = 256;
int groupSizeHorizontal = (screenWidth + groupSize - 1) / groupSize;
int groupSizeVertical = (screenHeight + groupSize - 1) / groupSize;
var cmdBuf = new CommandBuffer() { name = "Combine Subsurface Scattering" };
// Set the common data.
cmdBuf.SetComputeIntParam(m_CombineSubsurfaceScatteringCS, "screenWidth", screenWidth);
cmdBuf.SetComputeIntParam(m_CombineSubsurfaceScatteringCS, "screenHeight", screenHeight);
// Perform the vertical SSS filtering pass.
// TODO.
// Perform the horizontal SSS filtering pass, and combine diffuse and specular lighting.
cmdBuf.SetComputeTextureParam(m_CombineSubsurfaceScatteringCS, m_FilterHorizontalAndCombineKernel,
"diffuseFilterSource", m_CameraDiffuseLightingBufferRT);
cmdBuf.SetComputeTextureParam(m_CombineSubsurfaceScatteringCS, m_FilterHorizontalAndCombineKernel,
"specularSourceAndColorTarget", m_CameraColorBufferRT);
cmdBuf.DispatchCompute(m_CombineSubsurfaceScatteringCS, m_FilterHorizontalAndCombineKernel, groupSizeHorizontal, screenHeight, 1);
context.ExecuteCommandBuffer(cmdBuf);
cmdBuf.Dispose();
}
void UpdateSkyEnvironment(HDCamera hdCamera, ScriptableRenderContext renderContext)
{
m_SkyManager.UpdateEnvironment(hdCamera, m_lightLoop.GetCurrentSunLight(), renderContext);

{
m_SkyManager.RenderSky(hdCamera, m_lightLoop.GetCurrentSunLight(), m_CameraColorBufferRT, m_CameraDepthBufferRT, renderContext);
}
void RenderForward(CullResults cullResults, Camera camera, ScriptableRenderContext renderContext, bool renderOpaque)
{
// TODO: Currently we can't render opaque object forward when deferred is enabled

// Bind material data
m_LitRenderLoop.Bind();
RenderTargetIdentifier[] colorRTs = { m_CameraColorBuffer, m_CameraDiffuseLightingBufferRT };
Utilities.SetRenderTarget(renderContext, colorRTs, m_CameraDepthBufferRT);
Utilities.SetRenderTarget(renderContext, m_CameraColorBufferRT, m_CameraDepthBufferRT);
m_lightLoop.RenderForward(camera, renderContext, renderOpaque);

// Bind material data
m_LitRenderLoop.Bind();
RenderTargetIdentifier[] colorRTs = { m_CameraColorBuffer, m_CameraDiffuseLightingBufferRT };
Utilities.SetRenderTarget(renderContext, colorRTs, m_CameraDepthBufferRT);
Utilities.SetRenderTarget(renderContext, m_CameraColorBufferRT, m_CameraDepthBufferRT);
m_lightLoop.RenderForward(camera, renderContext, true);
RenderOpaqueRenderList(cullResults, camera, renderContext, "ForwardOnlyOpaque");

}
// Caution: We require sun light here as some sky use the sun light to render, mean UpdateSkyEnvironment
// must be call after BuildGPULightLists.
// must be call after BuildGPULightLists.
UpdateSkyEnvironment(hdCamera, renderContext);
UpdateSkyEnvironment(hdCamera, renderContext);
// We compute subsurface scattering here. Therefore, no objects rendered afterwards will exhibit SSS.
// Currently, there is no efficient way to switch between SRT and MRT for the forward pass;
// therefore, forward-rendered objects do not output the split lighting information required for SSS.
CombineSubsurfaceScattering(hdCamera, renderContext);
// For opaque forward we have split rendering in two categories
// Material that are always forward and material that can be deferred or forward depends on render pipeline options (like switch to rendering forward only mode)

6
Assets/ScriptableRenderLoop/HDRenderPipeline/Lighting/Resources/Deferred.shader


struct Outputs
{
float4 combinedLighting : SV_Target0;
float4 specularLighting : SV_Target0;
float3 diffuseLighting : SV_Target1;
};

LightLoop(V, posInput, preLightData, bsdfData, bakeDiffuseLighting, diffuseLighting, specularLighting);
Outputs outputs;
outputs.combinedLighting = float4(diffuseLighting + specularLighting, 1.0);
outputs.diffuseLighting = float3(diffuseLighting);
outputs.specularLighting = float4(specularLighting, 1.0);
outputs.diffuseLighting = diffuseLighting;
return outputs;
}

6
Assets/ScriptableRenderLoop/HDRenderPipeline/Lighting/TilePass/Resources/shadeopaque.compute


TEXTURE2D(_CameraDepthTexture);
SAMPLER2D(sampler_CameraDepthTexture);
RWTexture2D<float4> combinedLightingUAV;
RWTexture2D<float4> specularLightingUAV;
RWTexture2D<float3> diffuseLightingUAV;
[numthreads(TILE_SIZE, TILE_SIZE, 1)]

float3 specularLighting;
LightLoop(V, posInput, preLightData, bsdfData, bakeDiffuseLighting, diffuseLighting, specularLighting);
combinedLightingUAV[pixelCoord] = float4(diffuseLighting + specularLighting, 1.0);
diffuseLightingUAV[pixelCoord] = float3(diffuseLighting);
specularLightingUAV[pixelCoord] = float4(diffuseLighting + specularLighting, 1.0);
diffuseLightingUAV[pixelCoord] = diffuseLighting;
}

2
Assets/ScriptableRenderLoop/HDRenderPipeline/Lighting/TilePass/TilePass.cs


cmd.SetComputeTextureParam(shadeOpaqueShader, kernel, "_IESArray", IESArrayTexture ? IESArrayTexture : m_DefaultTexture2DArray);
cmd.SetComputeTextureParam(shadeOpaqueShader, kernel, "_SkyTexture", skyTexture ? skyTexture : m_DefaultTexture2DArray);
cmd.SetComputeTextureParam(shadeOpaqueShader, kernel, "combinedLightingUAV", cameraColorBufferRTs[0]);
cmd.SetComputeTextureParam(shadeOpaqueShader, kernel, "specularLightingUAV", cameraColorBufferRTs[0]);
cmd.SetComputeTextureParam(shadeOpaqueShader, kernel, "diffuseLightingUAV", cameraColorBufferRTs[1]);
cmd.DispatchCompute(shadeOpaqueShader, kernel, numTilesX, numTilesY, 1);
}

16
Assets/ScriptableRenderLoop/HDRenderPipeline/ShaderPass/ShaderPassForward.hlsl


#endif // TESSELLATION_ON
void Frag( PackedVaryingsToPS packedInput,
out float4 outCombinedLighting : SV_Target0,
out float3 outDiffuseLighting : SV_Target1
#ifdef _DEPTHOFFSET_ON
, out float outputDepth : SV_Depth
#endif
)
void Frag(PackedVaryingsToPS packedInput,
out float4 outColor : SV_Target0
#ifdef _DEPTHOFFSET_ON
, out float outputDepth : SV_Depth
#endif
)
{
FragInputs input = UnpackVaryingsMeshToFragInputs(packedInput.vmesh);

float3 bakeDiffuseLighting = GetBakedDiffuseLigthing(surfaceData, builtinData, bsdfData, preLightData);
LightLoop(V, posInput, preLightData, bsdfData, bakeDiffuseLighting, diffuseLighting, specularLighting);
outCombinedLighting = float4(diffuseLighting + specularLighting, builtinData.opacity);
outDiffuseLighting = float3(diffuseLighting);
outColor = float4(diffuseLighting + specularLighting, builtinData.opacity);
#ifdef _DEPTHOFFSET_ON
outputDepth = posInput.depthRaw;

正在加载...
取消
保存