浏览代码

Merge remote-tracking branch 'refs/remotes/origin/master' into Update-hlsl-generator

/RenderPassXR_Sandbox
sebastienlagarde 8 年前
当前提交
5acef24d
共有 7 个文件被更改,包括 1971 次插入97 次删除
  1. 129
      Assets/ScriptableRenderPipeline/LightweightPipeline/LightweightPipeline.cs
  2. 2
      Assets/ScriptableRenderPipeline/LightweightPipeline/LightweightPipelineAsset.asset
  3. 15
      Assets/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipeline.shader
  4. 16
      Assets/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipelineCore.cginc
  5. 1001
      Assets/GraphicsTests/RenderPipeline/LightweightPipeline/Scenes/MultiplePointLights.unity
  6. 8
      Assets/GraphicsTests/RenderPipeline/LightweightPipeline/Scenes/MultiplePointLights.unity.meta
  7. 897
      ImageTemplates/LightweightPipeline/Scenes/MultiplePointLights.unity.png

129
Assets/ScriptableRenderPipeline/LightweightPipeline/LightweightPipeline.cs


public int shadowResolution;
}
public class LightweightPipeline : RenderPipeline, IComparer<VisibleLight>
public class LightweightPipeline : RenderPipeline
// Max amount of visible lights. This controls the lights constant buffers in shader but not the max shaded lights.
// Lights are set per-object and the max shaded lights for each object are controlled by the max pixel lights in pipeline asset and kMaxVertexLights.
private static readonly int kMaxVisibleLights = 16;
private static readonly int kMaxVertexLights = 4;
private Vector4[] m_LightPositions = new Vector4[kMaxVisibleLights];
private Vector4[] m_LightColors = new Vector4[kMaxVisibleLights];
private Vector4[] m_LightAttenuations = new Vector4[kMaxVisibleLights];
private Vector4[] m_LightSpotDirections = new Vector4[kMaxVisibleLights];
// Amount of light indices buffer set per object.
// TODO: Change cullresults to return amount of renderers so we can allocate/reallocate enough buffer data
// As off now allocating a enough buffer to hold a scene that should enough for a small demo/game
private static readonly int kMaxLightIndices = 1024 * kMaxVisibleLights;
private ComputeBuffer m_LightIndexListBuffer;
private static readonly int kMaxLights = 8;
private static readonly int kMaxVertexLights = 4;
private int m_ShadowLightIndex = -1;
private readonly int[] m_LightTypePriority = new int[4] {2, 1, 2, 0}; // Spot and Point lights have max priority
private int m_ShadowLightIndex = -1;
private static readonly ShaderPassName m_ForwardBasePassName = new ShaderPassName("LightweightForward");
private Vector4[] m_LightPositions = new Vector4[kMaxLights];
private Vector4[] m_LightColors = new Vector4[kMaxLights];
private Vector4[] m_LightAttenuations = new Vector4[kMaxLights];
private Vector4[] m_LightSpotDirections = new Vector4[kMaxLights];
private static readonly ShaderPassName m_ForwardBasePassName = new ShaderPassName("LightweightForward") ;
public LightweightPipeline(LightweightPipelineAsset asset)
{
m_Asset = asset;

m_ShadowMapRTID = new RenderTargetIdentifier(m_ShadowMapProperty);
Shader.globalRenderPipeline = "LightweightPipeline";
// TODO: Change cullresults to return amount of renderers so we can allocate/reallocate enough buffer data
m_LightIndexListBuffer = new ComputeBuffer(kMaxLightIndices, sizeof(uint));
m_LightIndexListBuffer.Dispose();
base.Render(context, cameras);
base.Render(context, cameras);
foreach (Camera camera in cameras)
{

int pixelLightsCount, vertexLightsCount;
GetMaxSupportedLights(visibleLights.Length, out pixelLightsCount, out vertexLightsCount);
SortLights(ref visibleLights, pixelLightsCount);
InitializeMainShadowLightIndex(visibleLights);
if (m_ShadowLightIndex > -1)
shadowsRendered = RenderShadows(cull, visibleLights[m_ShadowLightIndex], context);

cmd.Dispose();
// Setup light and shadow shader constants
cull.FillLightIndices(m_LightIndexListBuffer);
SetupLightShaderVariables(visibleLights, pixelLightsCount, vertexLightsCount, context);
if (shadowsRendered)
SetupShadowShaderVariables(context, m_ShadowCasterCascadesCount);

if (m_Asset.EnableAmbientProbe)
settings.rendererConfiguration |= RendererConfiguration.PerObjectLightProbe;
settings.rendererConfiguration |= RendererConfiguration.ProvideLightIndices;
context.DrawRenderers(ref settings);

private void InitializeLightData()
{
for (int i = 0; i < kMaxLights; ++i)
for (int i = 0; i < kMaxVisibleLights; ++i)
{
m_LightPositions[i] = Vector4.zero;
m_LightColors[i] = Vector4.zero;

private void SetupLightShaderVariables(VisibleLight[] lights, int pixelLightCount, int vertexLightCount, ScriptableRenderContext context)
{
int totalLightCount = pixelLightCount + vertexLightCount;
for (int i = 0; i < totalLightCount; ++i)
int maxLights = Math.Min(kMaxVisibleLights, lights.Length);
for (int i = 0; i < maxLights; ++i)
{
VisibleLight currLight = lights[i];
if (currLight.lightType == LightType.Directional)

}
else
{
m_LightSpotDirections[i] = new Vector4(0.0f, 0.0f, 1.0f, 0.0f);
m_LightSpotDirections[i] = new Vector4(0.0f, 0.0f, 1.0f, 0.0f) ;
m_LightAttenuations[i] = new Vector4(-1.0f, 1.0f, quadAtten, rangeSq);
}
}

cmd.SetGlobalVectorArray("globalLightColor", m_LightColors);
cmd.SetGlobalVectorArray("globalLightAtten", m_LightAttenuations);
cmd.SetGlobalVectorArray("globalLightSpotDir", m_LightSpotDirections);
float shadowMinNormalBias = m_Asset.ShadowMinNormalBias;
float shadowNormalBias = m_Asset.ShadowNormalBias;
cmd.SetGlobalVector("globalLightData", new Vector4(pixelLightCount, totalLightCount, shadowMinNormalBias, shadowNormalBias));
cmd.SetGlobalBuffer("globalLightIndexList", m_LightIndexListBuffer);
cmd.SetGlobalVector("globalLightData", new Vector4(pixelLightCount, m_ShadowLightIndex, m_Asset.ShadowMinNormalBias, m_Asset.ShadowNormalBias));
SetShaderKeywords(cmd, vertexLightCount > 0);
context.ExecuteCommandBuffer(cmd);
cmd.Dispose();

return resolution;
}
void SetupShadowShaderVariables(ScriptableRenderContext context, int cascadeCount)
private void SetupShadowShaderVariables(ScriptableRenderContext context, int cascadeCount)
{
float shadowResolution = m_ShadowSlices[0].shadowResolution;

setupShadow.Dispose();
}
void SetShaderKeywords(CommandBuffer cmd, bool vertexLightSupport)
private void SetShaderKeywords(CommandBuffer cmd, bool vertexLightSupport)
{
if (vertexLightSupport)
cmd.EnableShaderKeyword("_VERTEX_LIGHTS");

cmd.DisableShaderKeyword("_LIGHT_PROBES_ON");
}
// Finds main light and main shadow casters and places them in the beginning of array.
// Sort the remaining array based on custom IComparer criteria.
private void SortLights(ref VisibleLight[] lights, int pixelLightsCount)
private void InitializeMainShadowLightIndex(VisibleLight[] lights)
if (lights.Length == 0)
return;
bool shadowsSupported = m_Asset.CurrShadowType != ShadowType.NO_SHADOW && pixelLightsCount > 0;
int mainLightIndex = -1;
float maxIntensity = -1;
VisibleLight currLight = lights[i];
if (currLight.lightType == LightType.Directional)
if (mainLightIndex == -1 || currLight.light.intensity > lights[mainLightIndex].light.intensity)
mainLightIndex = i;
if (shadowsSupported && (currLight.light.shadows != LightShadows.None) && IsSupportedShadowType(currLight.lightType))
// Prefer directional shadows, if not sort by intensity
if (m_ShadowLightIndex == -1 || currLight.lightType > lights[m_ShadowLightIndex].lightType)
m_ShadowLightIndex = i;
}
// If supports a single directional light only, main light is main shadow light.
if (pixelLightsCount == 1 && m_ShadowLightIndex > -1)
mainLightIndex = m_ShadowLightIndex;
int startIndex = 0;
if (mainLightIndex > -1)
{
SwapLights(ref lights, 0, mainLightIndex);
startIndex++;
}
if (mainLightIndex != m_ShadowLightIndex && m_ShadowLightIndex > 0)
{
SwapLights(ref lights, 1, m_ShadowLightIndex);
m_ShadowLightIndex = 1;
startIndex++;
Light light = lights[i].light;
if (light.shadows != LightShadows.None && IsSupportedShadowType(light.type) && light.intensity > maxIntensity)
{
m_ShadowLightIndex = i;
maxIntensity = light.intensity;
}
Array.Sort(lights, startIndex, lights.Length - startIndex, this);
}
private bool IsSupportedShadowType(LightType type)

private void SwapLights(ref VisibleLight[] lights, int lhsIndex, int rhsIndex)
{
if (lhsIndex == rhsIndex)
return;
VisibleLight temp = lights[lhsIndex];
lights[lhsIndex] = lights[rhsIndex];
lights[rhsIndex] = temp;
}
// Prioritizes Spot and Point lights by intensity. If any directional light, it will be the main
// light and will not be considered in the computation.
// TODO: Move to a better sorting solution, e.g, prioritize lights per object.
public int Compare(VisibleLight lhs, VisibleLight rhs)
{
int lhsLightTypePriority = m_LightTypePriority[(int)lhs.lightType];
int rhsLightTypePriority = m_LightTypePriority[(int)rhs.lightType];
if (lhsLightTypePriority != rhsLightTypePriority)
return rhsLightTypePriority - lhsLightTypePriority;
return (int)(rhs.light.intensity - lhs.light.intensity);
}
}
}

2
Assets/ScriptableRenderPipeline/LightweightPipeline/LightweightPipelineAsset.asset


m_Script: {fileID: 11500000, guid: bf2edee5c58d82540a51f03df9d42094, type: 3}
m_Name: LightweightPipelineAsset
m_EditorClassIdentifier:
m_MaxPixelLights: 1
m_MaxPixelLights: 4
m_SupportsVertexLight: 1
m_EnableLightmaps: 1
m_EnableAmbientProbe: 1

15
Assets/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipeline.shader


#if defined(_VERTEX_LIGHTS)
half4 diffuseAndSpecular = half4(1.0, 1.0, 1.0, 1.0);
for (int lightIndex = globalLightData.x; lightIndex < globalLightData.y; ++lightIndex)
int vertexLightStart = unity_LightIndicesOffsetAndCount.x + globalLightData.x;
int vertexLightEnd = vertexLightStart + (unity_LightIndicesOffsetAndCount.y - globalLightData.x);
for (int lightIter = vertexLightStart; lightIter < vertexLightEnd; ++lightIter)
int lightIndex = globalLightIndexList[lightIter];
LightInput lightInput;
half NdotL;
INITIALIZE_LIGHT(lightInput, lightIndex);

half3 viewDir = i.viewDir.xyz;
// TODO: Restrict pixel lights by 4. This way we can keep moderate constrain for most LD project
// and can benefit from better data layout/avoid branching by doing vec math.
for (int lightIndex = 0; lightIndex < globalLightData.x; ++lightIndex)
int pixelLightEnd = unity_LightIndicesOffsetAndCount.x + min(globalLightData.x, unity_LightIndicesOffsetAndCount.y);
for (int lightIter = unity_LightIndicesOffsetAndCount.x; lightIter < pixelLightEnd; ++lightIter)
LightInput lightData;
int lightIndex = globalLightIndexList[lightIter];
LightInput lightData;
if (lightIndex == 0)
if (lightIndex == globalLightData.y)
{
#if _NORMALMAP
float3 vertexNormal = float3(i.tangentToWorld0.z, i.tangentToWorld1.z, i.tangentToWorld2.z);

16
Assets/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipelineCore.cginc


#define MAX_LIGHTS 8
#define MAX_VISIBLE_LIGHTS 16
#define INITIALIZE_LIGHT(light, lightIndex) \
light.pos = globalLightPos[lightIndex]; \

// The variables are very similar to built-in unity_LightColor, unity_LightPosition,
// unity_LightAtten, unity_SpotDirection as used by the VertexLit shaders, except here
// we use world space positions instead of view space.
half4 globalLightColor[MAX_LIGHTS];
float4 globalLightPos[MAX_LIGHTS];
half4 globalLightSpotDir[MAX_LIGHTS];
half4 globalLightAtten[MAX_LIGHTS];
float4 globalLightData; // x: pixelLightCount, y = totalLightCount (pixel + vert), z = minShadowNormalBiasOffset, w = shadowNormalBiasOffset
half4 globalLightColor[MAX_VISIBLE_LIGHTS];
float4 globalLightPos[MAX_VISIBLE_LIGHTS];
half4 globalLightSpotDir[MAX_VISIBLE_LIGHTS];
half4 globalLightAtten[MAX_VISIBLE_LIGHTS];
float4 globalLightData; // x: pixelLightCount, y = shadowLightIndex, z = minShadowNormalBiasOffset, w = shadowNormalBiasOffset
// Per object light list data
half4 unity_LightIndicesOffsetAndCount;
StructuredBuffer<uint> globalLightIndexList;
half _Shininess;
samplerCUBE _Cube;

1001
Assets/GraphicsTests/RenderPipeline/LightweightPipeline/Scenes/MultiplePointLights.unity
文件差异内容过多而无法显示
查看文件

8
Assets/GraphicsTests/RenderPipeline/LightweightPipeline/Scenes/MultiplePointLights.unity.meta


fileFormatVersion: 2
guid: 6a5fa7fc4cfca8b46bec48a8d2ff1ce5
timeCreated: 1496824103
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

897
ImageTemplates/LightweightPipeline/Scenes/MultiplePointLights.unity.png

之前 之后
宽度: 1280  |  高度: 720  |  大小: 304 KiB
正在加载...
取消
保存