浏览代码

Added PerFrame and PerCamera constant buffers.

/RenderPassXR_Sandbox
Felipe Lira 7 年前
当前提交
94a4d501
共有 5 个文件被更改,包括 99 次插入42 次删除
  1. 46
      ScriptableRenderPipeline/LightweightPipeline/LightweightPipeline.cs
  2. 54
      ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightInput.cginc
  3. 4
      ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPassLit.cginc
  4. 24
      ScriptableRenderPipeline/LightweightPipeline/LightweightConstantBuffer.cs
  5. 13
      ScriptableRenderPipeline/LightweightPipeline/LightweightConstantBuffer.cs.meta

46
ScriptableRenderPipeline/LightweightPipeline/LightweightPipeline.cs


m_Asset = asset;
BuildShadowSettings();
PerFrameBuffer._GlossyEnvironmentColor = Shader.PropertyToID("_GlossyEnvironmentColor");
PerFrameBuffer._AttenuationTexture = Shader.PropertyToID("_AttenuationTexture");
PerCameraBuffer._MainLightPosition = Shader.PropertyToID("_MainLightPosition");
PerCameraBuffer._MainLightColor = Shader.PropertyToID("_MainLightColor");
PerCameraBuffer._MainLightAttenuationParams = Shader.PropertyToID("_MainLightAttenuationParams");
PerCameraBuffer._MainLightSpotDir = Shader.PropertyToID("_MainLightSpotDir");
PerCameraBuffer._AdditionalLightCount = Shader.PropertyToID("_AdditionalLightCount");
PerCameraBuffer._AdditionalLightPosition = Shader.PropertyToID("_AdditionalLightPosition");
PerCameraBuffer._AdditionalLightColor = Shader.PropertyToID("_AdditionalLightColor");
PerCameraBuffer._AdditionalLightAttenuationParams = Shader.PropertyToID("_AdditionalLightAttenuationParams");
PerCameraBuffer._AdditionalLightSpotDir = Shader.PropertyToID("_AdditionalLightSpotDir");
m_ShadowMapTexture = Shader.PropertyToID("_ShadowMap");
m_CameraColorTexture = Shader.PropertyToID("_CameraRT");
m_CameraDepthTexture = Shader.PropertyToID("_CameraDepthTexture");

// TODO: This is at the moment required for all pipes. We should not implicitly change user project settings
// instead this should be forced when using SRP, since all SRP use linear lighting.
GraphicsSettings.lightsUseLinearIntensity = true;
SetupPerFrameShaderConstants(ref context);
// Sort cameras array by camera depth
Array.Sort(cameras, m_CameraComparer);

}
}
private void SetupShaderLightConstants(CommandBuffer cmd, VisibleLight[] lights, ref LightData lightData, ref CullResults cullResults, ref ScriptableRenderContext context)
private void SetupPerFrameShaderConstants(ref ScriptableRenderContext context)
cmd.SetGlobalVector("_GlossyEnvironmentColor", glossyEnvColor);
if (m_Asset.AttenuationTexture != null) cmd.SetGlobalTexture("_AttenuationTexture", m_Asset.AttenuationTexture);
CommandBuffer cmd = CommandBufferPool.Get("SetupPerFrameConstants");
cmd.SetGlobalVector(PerFrameBuffer._GlossyEnvironmentColor, glossyEnvColor);
if (m_Asset.AttenuationTexture != null) cmd.SetGlobalTexture(PerFrameBuffer._AttenuationTexture, m_Asset.AttenuationTexture);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release (cmd);
}
private void SetupShaderLightConstants(CommandBuffer cmd, VisibleLight[] lights, ref LightData lightData, ref CullResults cullResults, ref ScriptableRenderContext context)
{
// Main light has an optimized shader path for main light. This will benefit games that only care about a single light.
// Lightweight pipeline also supports only a single shadow light, if available it will be the main light.
if (lightData.mainLightIndex != -1)

Vector4 lightPos, lightColor, lightSpotDir, lightAttenuationParams;
InitializeLightConstants(lights, lightIndex, out lightPos, out lightColor, out lightSpotDir, out lightAttenuationParams);
cmd.SetGlobalVector("_LightPosition", lightPos);
cmd.SetGlobalColor("_LightColor", lightColor);
cmd.SetGlobalVector("_LightSpotDir", lightSpotDir);
cmd.SetGlobalVector("_LightAttenuationParams", lightAttenuationParams);
cmd.SetGlobalVector(PerCameraBuffer._MainLightPosition, lightPos);
cmd.SetGlobalColor(PerCameraBuffer._MainLightColor, lightColor);
cmd.SetGlobalVector(PerCameraBuffer._MainLightSpotDir, lightSpotDir);
cmd.SetGlobalVector(PerCameraBuffer._MainLightAttenuationParams, lightAttenuationParams);
}
private void SetupAdditionalListConstants(CommandBuffer cmd, VisibleLight[] lights, ref LightData lightData, ref ScriptableRenderContext context)

}
m_CullResults.SetLightIndexMap(lightIndexMap);
cmd.SetGlobalVector("globalLightCount", new Vector4 (lightData.additionalPixelLightsCount, lightData.vertexLightsCount, 0.0f, 0.0f));
cmd.SetGlobalVectorArray ("globalLightPos", m_LightPositions);
cmd.SetGlobalVectorArray ("globalLightColor", m_LightColors);
cmd.SetGlobalVectorArray ("globalLightAtten", m_LightAttenuations);
cmd.SetGlobalVectorArray ("globalLightSpotDir", m_LightSpotDirections);
cmd.SetGlobalVector(PerCameraBuffer._AdditionalLightCount, new Vector4 (lightData.additionalPixelLightsCount, lightData.vertexLightsCount, 0.0f, 0.0f));
cmd.SetGlobalVectorArray (PerCameraBuffer._AdditionalLightPosition, m_LightPositions);
cmd.SetGlobalVectorArray (PerCameraBuffer._AdditionalLightColor, m_LightColors);
cmd.SetGlobalVectorArray (PerCameraBuffer._AdditionalLightAttenuationParams, m_LightAttenuations);
cmd.SetGlobalVectorArray (PerCameraBuffer._AdditionalLightSpotDir, m_LightSpotDirections);
}
private void SetupShadowShaderConstants(CommandBuffer cmd, ref ScriptableRenderContext context, ref VisibleLight shadowLight, int cascadeCount)

54
ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightInput.cginc


// Main light initialized without indexing
#define INITIALIZE_MAIN_LIGHT(light) \
light.pos = _LightPosition; \
light.color = _LightColor; \
light.atten = _LightAttenuationParams; \
light.spotDir = _LightSpotDir;
light.pos = _MainLightPosition; \
light.color = _MainLightColor; \
light.atten = _MainLightAttenuationParams; \
light.spotDir = _MainLightSpotDir;
light.pos = globalLightPos[lightIndex]; \
light.color = globalLightColor[lightIndex]; \
light.atten = globalLightAtten[lightIndex]; \
light.spotDir = globalLightSpotDir[lightIndex]
light.pos = _AdditionalLightPosition[lightIndex]; \
light.color = _AdditionalLightColor[lightIndex]; \
light.atten = _AdditionalLightAttenuationParams[lightIndex]; \
light.spotDir = _AdditionalLightSpotDir[lightIndex]
#if (defined(_MAIN_DIRECTIONAL_LIGHT) || defined(_MAIN_SPOT_LIGHT) || defined(_MAIN_POINT_LIGHT))
#define _MAIN_LIGHT

#endif
#if defined(UNITY_COLORSPACE_GAMMA) && defined(_LIGHTWEIGHT_FORCE_LINEAR)
// Ideally we want an approximation of gamma curve 2.0 to save ALU on GPU but as off now it won't match the GammaToLinear conversion of props in engine
//#define LIGHTWEIGHT_GAMMA_TO_LINEAR(sRGB) sRGB * (sRGB * (sRGB * 0.305306011h + 0.682171111h) + 0.012522878h)
//#define LIGHTWEIGHT_LINEAR_TO_GAMMA(linRGB) max(1.055h * pow(max(linRGB, 0.h), 0.416666667h) - 0.055h, 0.h)
struct LightInput
{

half4 spotDir;
};
sampler2D _AttenuationTexture;
// Per object light list data
CBUFFER_START(_PerObject)
half _Shininess;
CBUFFER_END
// 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 globalLightCount;
half4 globalLightColor[MAX_VISIBLE_LIGHTS];
float4 globalLightPos[MAX_VISIBLE_LIGHTS];
half4 globalLightSpotDir[MAX_VISIBLE_LIGHTS];
float4 globalLightAtten[MAX_VISIBLE_LIGHTS];
float4 _LightPosition;
half4 _LightColor;
float4 _LightAttenuationParams;
half4 _LightSpotDir;
CBUFFER_START(_PerCamera)
float4 _MainLightPosition;
half4 _MainLightColor;
float4 _MainLightAttenuationParams;
half4 _MainLightSpotDir;
half4 _AdditionalLightCount;
float4 _AdditionalLightPosition[MAX_VISIBLE_LIGHTS];
half4 _AdditionalLightColor[MAX_VISIBLE_LIGHTS];
float4 _AdditionalLightAttenuationParams[MAX_VISIBLE_LIGHTS];
half4 _AdditionalLightSpotDir[MAX_VISIBLE_LIGHTS];
CBUFFER_END
half _Shininess;
CBUFFER_START(_PerFrame)
sampler2D _AttenuationTexture;
CBUFFER_END
struct LightweightVertexInput
{

4
ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPassLit.cginc


#endif
#ifdef _ADDITIONAL_PIXEL_LIGHTS
int pixelLightCount = min(globalLightCount.x, unity_LightIndicesOffsetAndCount.y);
int pixelLightCount = min(_AdditionalLightCount.x, unity_LightIndicesOffsetAndCount.y);
for (int lightIter = 0; lightIter < pixelLightCount; ++lightIter)
{
LightInput light;

#endif
#ifdef _ADDITIONAL_PIXEL_LIGHTS
int pixelLightCount = min(globalLightCount.x, unity_LightIndicesOffsetAndCount.y);
int pixelLightCount = min(_AdditionalLightCount.x, unity_LightIndicesOffsetAndCount.y);
for (int lightIter = 0; lightIter < pixelLightCount; ++lightIter)
{
LightInput lightData;

24
ScriptableRenderPipeline/LightweightPipeline/LightweightConstantBuffer.cs


using UnityEngine;
namespace UnityEngine.Experimental.Rendering.LightweightPipeline
{
public static class PerFrameBuffer
{
public static int _GlossyEnvironmentColor;
public static int _AttenuationTexture;
}
public static class PerCameraBuffer
{
public static int _MainLightPosition;
public static int _MainLightColor;
public static int _MainLightAttenuationParams;
public static int _MainLightSpotDir;
public static int _AdditionalLightCount;
public static int _AdditionalLightPosition;
public static int _AdditionalLightColor;
public static int _AdditionalLightAttenuationParams;
public static int _AdditionalLightSpotDir;
}
}

13
ScriptableRenderPipeline/LightweightPipeline/LightweightConstantBuffer.cs.meta


fileFormatVersion: 2
guid: 2462b9b2044ad4b62a8c7fcd6f012f8e
timeCreated: 1506514389
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存