浏览代码

Added support to ComputeBuffer for non mobile and non WebGL platforms.

/main
Felipe Lira 7 年前
当前提交
66f3ec92
共有 3 个文件被更改,包括 63 次插入3 次删除
  1. 48
      ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipeline.cs
  2. 13
      ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/Input.hlsl
  3. 5
      ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/Lighting.hlsl

48
ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipeline.cs


using System;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine.Assertions;
#if UNITY_EDITOR
using UnityEditor.Experimental.Rendering.LightweightPipeline;
#endif

private bool m_DepthRenderBuffer;
private MixedLightingSetup m_MixedLightingSetup;
List<int> m_LocalLightIndices = new List<int>(kMaxLocalPixelLightPerPass);
private ComputeBuffer m_LightIndicesBuffer;
private List<int> m_LocalLightIndices = new List<int>(kMaxLocalPixelLightPerPass);
private bool m_UseComputeBuffer;
// Pipeline pass names
private static readonly ShaderPassName m_DepthPrepass = new ShaderPassName("DepthOnly");

m_CopyTextureSupport = SystemInfo.copyTextureSupport;
// TODO: Profile performance of using ComputeBuffer on mobiles that support it vs
// fixed buffer size
if (Application.isMobilePlatform || Application.platform == RuntimePlatform.WebGLPlayer)
m_UseComputeBuffer = false;
else
m_UseComputeBuffer = true;
m_LightIndicesBuffer = null;
// Let engine know we have MSAA on for cases where we support MSAA backbuffer
if (QualitySettings.antiAliasing != m_Asset.MSAASampleCount)
QualitySettings.antiAliasing = m_Asset.MSAASampleCount;

#if UNITY_EDITOR
SceneViewDrawMode.ResetDrawMode();
#endif
if (m_LightIndicesBuffer != null)
{
m_LightIndicesBuffer.Release();
m_LightIndicesBuffer = null;
}
}
private void SetRenderingFeatures()

cmd.SetGlobalVector(PerCameraBuffer._AdditionalLightCount, new Vector4(lightData.pixelAdditionalLightsCount,
lightData.totalAdditionalLightsCount, 0.0f, 0.0f));
// if not using a compute buffer, engine will set indices in 2 vec4 constants
// unity_4LightIndices0 and unity_4LightIndices1
// TODO: We can benefit from better thread usage if we set LightIndex map and call
// FillLightIndices way earlier.
if (m_UseComputeBuffer)
{
int lightIndicesCount = m_CullResults.GetLightIndicesCount();
if (m_LightIndicesBuffer == null)
{
m_LightIndicesBuffer = new ComputeBuffer(lightIndicesCount, sizeof(int));
}
else if (m_LightIndicesBuffer.count < lightIndicesCount)
{
m_LightIndicesBuffer.Release();
m_LightIndicesBuffer = new ComputeBuffer(lightIndicesCount, sizeof(int));
}
m_CullResults.FillLightIndices(m_LightIndicesBuffer);
cmd.SetGlobalBuffer("_LightIndexBuffer", m_LightIndicesBuffer);
}
}
else
{

{
RendererConfiguration settings = RendererConfiguration.PerObjectReflectionProbes | RendererConfiguration.PerObjectLightmaps | RendererConfiguration.PerObjectLightProbe;
if (lightData.totalAdditionalLightsCount > 0)
settings |= RendererConfiguration.PerObjectLightIndices8;
{
if (m_UseComputeBuffer)
settings |= RendererConfiguration.ProvideLightIndices;
else
settings |= RendererConfiguration.PerObjectLightIndices8;
}
return settings;
}

13
ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/Input.hlsl


#define MAX_VISIBLE_LIGHTS 16
// Must match check of use compute buffer in LightweightPipeline.cs
// GLES check here because of WebGL 1.0 support
// TODO: check performance of using StructuredBuffer on mobile as well
#if defined(SHADER_API_MOBILE) || defined(SHADER_API_GLES)
#define USE_STRUCTURED_BUFFER_FOR_LIGHT_DATA 0
#else
#define USE_STRUCTURED_BUFFER_FOR_LIGHT_DATA 1
#endif
struct InputData
{
float3 positionWS;

half4 _AdditionalLightSpotAttenuation[MAX_VISIBLE_LIGHTS];
float4 _ScaledScreenParams;
#if USE_STRUCTURED_BUFFER_FOR_LIGHT_DATA
StructuredBuffer<int> _LightIndexBuffer;
#endif
CBUFFER_END
#define UNITY_MATRIX_M unity_ObjectToWorld

5
ScriptableRenderPipeline/LightweightPipeline/LWRP/ShaderLibrary/Lighting.hlsl


{
LightInput lightInput;
#if USE_STRUCTURED_BUFFER_FOR_LIGHT_DATA
int lightIndex = _LightIndexBuffer[unity_LightIndicesOffsetAndCount.x + i];
#else
#endif
// The following code will turn into a branching madhouse on platforms that don't support
// dynamic indexing. Ideally we need to configure light data at a cluster of

// TODO: we need to expose in SRP api an ability for the pipeline cap the amount of lights
// in the culling. This way we could do the loop branch with an uniform
// This would be helpful to support baking exceeding lights in SH as well
//return _AdditionalLightCount.x;
return min(_AdditionalLightCount.x, unity_LightIndicesOffsetAndCount.y);
}

正在加载...
取消
保存