浏览代码

Merge pull request #363 from Unity-Technologies/LighweightPipelineXR

Add XR Support to LightweightPipeline
/RenderPassXR_Sandbox
GitHub 7 年前
当前提交
63179c80
共有 4 个文件被更改,包括 77 次插入25 次删除
  1. 89
      Assets/ScriptableRenderPipeline/LightweightPipeline/LightweightPipeline.cs
  2. 4
      Assets/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipeline.shader
  3. 2
      Assets/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipelineCore.cginc
  4. 7
      Assets/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightUnlit.shader

89
Assets/ScriptableRenderPipeline/LightweightPipeline/LightweightPipeline.cs


using System;
using UnityEngine.Rendering;
using UnityEngine.XR;
namespace UnityEngine.Experimental.Rendering.LightweightPipeline
{

private RenderTargetIdentifier m_CameraRTID;
private bool m_RenderToIntermediateTarget = false;
private bool m_IntermediateTextureArray = false;
private const int kShadowDepthBufferBits = 16;
private const int kCameraDepthBufferBits = 32;

{
base.Render(context, cameras);
bool stereoEnabled = XRSettings.isDeviceActive;
if (!CullResults.GetCullingParameters(camera, out cullingParameters))
if (!CullResults.GetCullingParameters(camera, stereoEnabled, out cullingParameters))
continue;
cullingParameters.shadowDistance = Mathf.Min(m_ShadowSettings.maxShadowDistance, camera.farClipPlane);

lightData.shadowsRendered = RenderShadows(ref m_CullResults, ref visibleLights[lightData.shadowLightIndex], lightData.shadowLightIndex, ref context);
// Setup camera matrices and RT
context.SetupCameraProperties(camera);
context.SetupCameraProperties(camera, stereoEnabled);
// Setup light and shadow shader constants
SetupShaderLightConstants(visibleLights, ref lightData, ref m_CullResults, ref context);

if (!lightData.isSingleDirectionalLight)
configuration |= RendererConfiguration.PerObjectLightIndices8;
BeginForwardRendering(camera, ref context);
BeginForwardRendering(camera, ref context, stereoEnabled);
// Render Opaques
var litSettings = new DrawRendererSettings(m_CullResults, camera, m_LitPassName);

context.DrawRenderers(ref litSettings);
// Release temporary RT
var discardRT = CommandBufferPool.Get();
discardRT.ReleaseTemporaryRT(m_ShadowMapProperty);
discardRT.ReleaseTemporaryRT(m_CameraRTProperty);
context.ExecuteCommandBuffer(discardRT);
CommandBufferPool.Release(discardRT);
// TODO: Check skybox shader
context.DrawSkybox(camera);

context.DrawRenderers(ref litSettings);
context.DrawRenderers(ref unlitSettings);
EndForwardRendering(camera, ref context);
EndForwardRendering(camera, ref context, stereoEnabled);
// Release temporary RT
var discardRT = CommandBufferPool.Get();
discardRT.ReleaseTemporaryRT(m_ShadowMapProperty);
discardRT.ReleaseTemporaryRT(m_CameraRTProperty);
context.ExecuteCommandBuffer(discardRT);
CommandBufferPool.Release(discardRT);
}
context.Submit();

return (type == LightType.Directional || type == LightType.Spot);
}
private void BeginForwardRendering(Camera camera, ref ScriptableRenderContext context)
private void BeginForwardRendering(Camera camera, ref ScriptableRenderContext context, bool stereoEnabled)
if (stereoEnabled)
context.StartMultiEye(camera);
m_RenderToIntermediateTarget = GetRenderToIntermediateTarget(camera);
var cmd = CommandBufferPool.Get("SetCameraRenderTarget");

{
cmd.GetTemporaryRT(m_CameraRTProperty, Screen.width, Screen.height, kCameraDepthBufferBits,
FilterMode.Bilinear, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default, m_Asset.MSAASampleCount);
cmd.SetRenderTarget(m_CameraRTID);
m_IntermediateTextureArray = false;
if (stereoEnabled)
{
RenderTextureDescriptor xrDesc = XRSettings.eyeTextureDesc;
xrDesc.depthBufferBits = kCameraDepthBufferBits;
xrDesc.colorFormat = RenderTextureFormat.ARGB32;
xrDesc.msaaSamples = m_Asset.MSAASampleCount;
m_IntermediateTextureArray = (xrDesc.dimension == TextureDimension.Tex2DArray);
cmd.GetTemporaryRT(m_CameraRTProperty, xrDesc, FilterMode.Bilinear);
}
else
{
cmd.GetTemporaryRT(m_CameraRTProperty, Screen.width, Screen.height, kCameraDepthBufferBits,
FilterMode.Bilinear, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default, m_Asset.MSAASampleCount);
}
if (m_IntermediateTextureArray)
cmd.SetRenderTarget(m_CameraRTID, 0, CubemapFace.Unknown, -1);
else
cmd.SetRenderTarget(m_CameraRTID);
}
else
{

else
{
cmd.SetRenderTarget(BuiltinRenderTextureType.None);
cmd.SetRenderTarget(BuiltinRenderTextureType.CurrentActive);
}
// Clear RenderTarget to avoid tile initialization on mobile GPUs

CommandBufferPool.Release(cmd);
}
private void EndForwardRendering(Camera camera, ref ScriptableRenderContext context)
private void EndForwardRendering(Camera camera, ref ScriptableRenderContext context, bool stereoEnabled)
if (!m_RenderToIntermediateTarget)
return;
if (m_RenderToIntermediateTarget)
{
var cmd = CommandBufferPool.Get("Blit");
if (m_IntermediateTextureArray)
{
cmd.SetRenderTarget(BuiltinRenderTextureType.CameraTarget, 0, CubemapFace.Unknown, -1);
cmd.Blit(m_CameraRTID, BuiltinRenderTextureType.CurrentActive);
}
else
cmd.Blit(BuiltinRenderTextureType.CurrentActive, BuiltinRenderTextureType.CameraTarget);
if (camera.cameraType == CameraType.SceneView)
cmd.SetRenderTarget(BuiltinRenderTextureType.CameraTarget);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
var cmd = CommandBufferPool.Get("Blit");
cmd.Blit(BuiltinRenderTextureType.CurrentActive, BuiltinRenderTextureType.CameraTarget);
if (camera.cameraType == CameraType.SceneView)
cmd.SetRenderTarget(BuiltinRenderTextureType.CameraTarget);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
if (stereoEnabled)
{
context.StopMultiEye(camera);
context.StereoEndRender(camera);
}
}
private bool GetRenderToIntermediateTarget(Camera camera)

4
Assets/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipeline.shader


#pragma shader_feature _EMISSION
#pragma shader_feature _ _REFLECTION_CUBEMAP _REFLECTION_PROBE
#pragma multi_compile _ UNITY_SINGLE_PASS_STEREO STEREO_INSTANCING_ON STEREO_MULTIVIEW_ON
#pragma multi_compile _ _SINGLE_DIRECTIONAL_LIGHT
#pragma multi_compile _ LIGHTMAP_ON
#pragma multi_compile _ _LIGHT_PROBES_ON

v2f vert(LightweightVertexInput v)
{
v2f o = (v2f)0;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.uv01.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
o.uv01.zw = v.lightmapUV * unity_LightmapST.xy + unity_LightmapST.zw;

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


float4 tangent : TANGENT;
float3 texcoord : TEXCOORD0;
float2 lightmapUV : TEXCOORD1;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f

half4 viewDir : TEXCOORD5; // xyz: viewDir
UNITY_FOG_COORDS_PACKED(6, half4) // x: fogCoord, yzw: vertexColor
float4 hpos : SV_POSITION;
UNITY_VERTEX_OUTPUT_STEREO
};
// Per object light list data

7
Assets/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightUnlit.shader


CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile _ UNITY_SINGLE_PASS_STEREO STEREO_INSTANCING_ON STEREO_MULTIVIEW_ON
#pragma multi_compile_fog
#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON

{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f

float4 vertex : SV_POSITION;
UNITY_VERTEX_OUTPUT_STEREO
};
sampler2D _MainTex;

v2f vert(appdata v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);

正在加载...
取消
保存