浏览代码

Added a utility to copy channels in a compute shader

/stochastic_alpha_test
Frédéric Vauchelles 7 年前
当前提交
ec4b0369
共有 9 个文件被更改,包括 99 次插入13 次删除
  1. 9
      ScriptableRenderPipeline/Core/CoreUtils.cs
  2. 8
      ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs
  3. 4
      ScriptableRenderPipeline/HDRenderPipeline/HDStringConstants.cs
  4. 8
      ScriptableRenderPipeline/HDRenderPipeline/RenderPipelineResources/DepthDownsample.compute
  5. 1
      ScriptableRenderPipeline/HDRenderPipeline/RenderPipelineResources/HDRenderPipelineResources.asset
  6. 14
      ScriptableRenderPipeline/HDRenderPipeline/RenderPipelineResources/RenderPipelineResources.cs
  7. 58
      ScriptableRenderPipeline/HDRenderPipeline/RenderPipelineResources/CopyChannel.compute
  8. 10
      ScriptableRenderPipeline/HDRenderPipeline/RenderPipelineResources/CopyChannel.compute.meta

9
ScriptableRenderPipeline/Core/CoreUtils.cs


cmd.ClearRenderTarget((clearFlag & ClearFlag.Depth) != 0, (clearFlag & ClearFlag.Color) != 0, clearColor);
}
public static void SampleCopyChannel_xyzw2x(CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier target, Vector2 size, RenderPipelineResources resources)
{
var s = new Vector4(size.x, size.y, 1f / size.x, 1f / size.y);
cmd.SetComputeVectorParam(resources.copyChannelCS, HDShaderIDs._Size, s);
cmd.SetComputeTextureParam(resources.copyChannelCS, resources.copyChannelKernel_xyzw2x, HDShaderIDs._Source4, source);
cmd.SetComputeTextureParam(resources.copyChannelCS, resources.copyChannelKernel_xyzw2x, HDShaderIDs._Result1, target);
cmd.DispatchCompute(resources.copyChannelCS, resources.copyChannelKernel_xyzw2x, (int)(size.x) / 8, (int)(size.y) / 8, 1);
}
public static void ClearCubemap(CommandBuffer cmd, RenderTargetIdentifier buffer, Color clearColor)
{
for(int i = 0; i < 6; ++i)

8
ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs


int m_GaussianPyramidKernel;
ComputeShader m_DepthPyramidCS { get { return m_Asset.renderPipelineResources.depthPyramidCS; } }
int m_DepthPyramidKernel;
int m_DepthPyramidCopyKernel;
Material m_CameraMotionVectorsMaterial;

m_DistortionBuffer = HDShaderIDs._DistortionTexture;
m_DistortionBufferRT = new RenderTargetIdentifier(m_DistortionBuffer);
m_GaussianPyramidKernel = m_GaussianPyramidCS.FindKernel("KMain");
m_GaussianPyramidColorBuffer = HDShaderIDs._GaussianPyramidColorTexture;
m_GaussianPyramidColorBufferRT = new RenderTargetIdentifier(m_GaussianPyramidColorBuffer);
m_GaussianPyramidColorBufferDesc = new RenderTextureDescriptor(2, 2, RenderTextureFormat.ARGBHalf, 0)

};
m_DepthPyramidKernel = m_DepthPyramidCS.FindKernel("KMain");
m_DepthPyramidBuffer = HDShaderIDs._DepthPyramidTexture;
m_DepthPyramidBufferRT = new RenderTargetIdentifier(m_DepthPyramidBuffer);
m_DepthPyramidBufferDesc = new RenderTextureDescriptor(2, 2, RenderTextureFormat.RFloat, 0)

cmd.SetGlobalVector(HDShaderIDs._DepthPyramidMipSize, new Vector4(size, size, lodCount, 0));
cmd.GetTemporaryRT(HDShaderIDs._DepthPyramidMips[0], size, size, 0, FilterMode.Bilinear, RenderTextureFormat.RFloat, RenderTextureReadWrite.Linear, 1, true);
cmd.SetComputeTextureParam(m_DepthPyramidCS, m_DepthPyramidCopyKernel, "_SourceDepthStencil", GetDepthTexture());
cmd.SetComputeTextureParam(m_DepthPyramidCS, m_DepthPyramidCopyKernel, "_Result", HDShaderIDs._DepthPyramidMips[0]);
cmd.SetComputeVectorParam(m_DepthPyramidCS, "_Size", new Vector4(size, size, 1f / size, 1f / size));
cmd.DispatchCompute(m_DepthPyramidCS, m_DepthPyramidCopyKernel, size / 8, size / 8, 1);
Utilities.SampleCopyChannel_xyzw2x(cmd, GetDepthTexture(), HDShaderIDs._DepthPyramidMips[0], new Vector2(size, size), m_Asset.renderPipelineResources);
cmd.CopyTexture(HDShaderIDs._DepthPyramidMips[0], 0, 0, m_DepthPyramidBuffer, 0, 0);
for (int i = 0; i < lodCount; i++)

4
ScriptableRenderPipeline/HDRenderPipeline/HDStringConstants.cs


internal static readonly int _GlobalFog_Extinction = Shader.PropertyToID("_GlobalFog_Extinction");
internal static readonly int _GlobalFog_Asymmetry = Shader.PropertyToID("_GlobalFog_Asymmetry");
internal static readonly int _GlobalFog_Scattering = Shader.PropertyToID("_GlobalFog_Scattering");
internal static readonly int _Size = Shader.PropertyToID("_Size");
internal static readonly int _Source4 = Shader.PropertyToID("_Source4");
internal static readonly int _Result1 = Shader.PropertyToID("_Result1");
}
}

8
ScriptableRenderPipeline/HDRenderPipeline/RenderPipelineResources/DepthDownsample.compute


#include "../../Core/ShaderLibrary/Common.hlsl"
Texture2D<float4> _SourceDepthStencil;
Texture2D<float> _Source;
RWTexture2D<float> _Result;

float4 _Size;
CBUFFER_END
#pragma kernel KCopy
[numthreads(8, 8, 1)]
void KCopy(uint2 groupId : SV_GroupID, uint2 groupThreadId : SV_GroupThreadID, uint2 dispatchThreadId : SV_DispatchThreadID)
{
_Result[dispatchThreadId] = _SourceDepthStencil.SampleLevel(sampler_LinearClamp, float2(dispatchThreadId)*_Size.zw, 0.0).x;
}
#pragma kernel KMain
[numthreads(8, 8, 1)]

1
ScriptableRenderPipeline/HDRenderPipeline/RenderPipelineResources/HDRenderPipelineResources.asset


type: 3}
gaussianPyramidCS: {fileID: 7200000, guid: 6dba4103d23a7904fbc49099355aff3e, type: 3}
depthPyramidCS: {fileID: 7200000, guid: 64a553bb564274041906f78ffba955e4, type: 3}
copyChannelCS: {fileID: 7200000, guid: 4e910cec38a1ec640a03324015e036d0, type: 3}
clearDispatchIndirectShader: {fileID: 7200000, guid: fc1f553acb80a6446a32d33e403d0656,
type: 3}
buildDispatchIndirectShader: {fileID: 7200000, guid: 4eb1b418be7044c40bb5200496c50f14,

14
ScriptableRenderPipeline/HDRenderPipeline/RenderPipelineResources/RenderPipelineResources.cs


instance.volumetricLightingCS = UnityEditor.AssetDatabase.LoadAssetAtPath<ComputeShader>(HDRenderPipelinePath + "Lighting/Volumetrics/Resources/VolumetricLighting.compute");
instance.gaussianPyramidCS = UnityEditor.AssetDatabase.LoadAssetAtPath<ComputeShader>(PostProcessingPath + "Shaders/Builtins/GaussianDownsample.compute");
instance.depthPyramidCS = UnityEditor.AssetDatabase.LoadAssetAtPath<ComputeShader>(HDRenderPipelinePath + "RenderPipelineResources/DepthDownsample.compute");
instance.copyChannelCS = UnityEditor.AssetDatabase.LoadAssetAtPath<ComputeShader>(HDRenderPipelinePath + "RenderPipelineResources/CopyChannel.compute");
instance.clearDispatchIndirectShader = UnityEditor.AssetDatabase.LoadAssetAtPath<ComputeShader>(HDRenderPipelinePath + "Lighting/TilePass/cleardispatchindirect.compute");
instance.buildDispatchIndirectShader = UnityEditor.AssetDatabase.LoadAssetAtPath<ComputeShader>(HDRenderPipelinePath + "Lighting/TilePass/builddispatchindirect.compute");

public ComputeShader volumetricLightingCS;
public ComputeShader gaussianPyramidCS;
public ComputeShader depthPyramidCS;
public ComputeShader copyChannelCS;
// Lighting tile pass resources
public ComputeShader clearDispatchIndirectShader;

public Shader GGXConvolve;
public Shader skyboxCubemap;
public int copyChannelKernel_xyzw2x { get; private set; }
public void OnEnable()
{
copyChannelKernel_xyzw2x = -1;
if (copyChannelCS != null)
{
copyChannelKernel_xyzw2x = copyChannelCS.FindKernel("KSampleCopy4_1_x");
}
}
}
}

58
ScriptableRenderPipeline/HDRenderPipeline/RenderPipelineResources/CopyChannel.compute


#include "../../Core/ShaderLibrary/Common.hlsl"
#define SOURCE(n) _Source##n
#define RESULT(n) _Result##n
#define SOURCE_DECLARATION(n) Texture2D<float##n> SOURCE(n)
#define RESULT_DECLARATION(n) RWTexture2D<float##n> RESULT(n)
SOURCE_DECLARATION(1);
SOURCE_DECLARATION(2);
SOURCE_DECLARATION(3);
SOURCE_DECLARATION(4);
RESULT_DECLARATION(1);
RESULT_DECLARATION(2);
RESULT_DECLARATION(3);
RESULT_DECLARATION(4);
SamplerState sampler_LinearClamp;
CBUFFER_START(cb)
float4 _Size;
CBUFFER_END
#define KERNEL_SAMPLECOPY(sourceN, resultN, subscript) [numthreads(8, 8, 1)]\
void KSampleCopy##sourceN##_##resultN##_##subscript(uint2 dispatchThreadId : SV_DispatchThreadID)\
{\
RESULT(resultN)[dispatchThreadId] = SOURCE(sourceN).SampleLevel(sampler_LinearClamp, float2(dispatchThreadId)*_Size.zw, 0.0).subscript;\
}
// Source R
#pragma kernel KSampleCopy1_1_x
KERNEL_SAMPLECOPY(1, 1, x);
// Source RG
// Result R
#pragma kernel KSampleCopy2_1_x
KERNEL_SAMPLECOPY(2, 1, x);
#pragma kernel KSampleCopy2_1_y
KERNEL_SAMPLECOPY(2, 1, y);
// Result RG
#pragma kernel KSampleCopy2_2_xx
KERNEL_SAMPLECOPY(2, 2, xx);
#pragma kernel KSampleCopy2_2_xy
KERNEL_SAMPLECOPY(2, 2, xy);
#pragma kernel KSampleCopy2_2_yx
KERNEL_SAMPLECOPY(2, 2, yx);
#pragma kernel KSampleCopy2_2_yy
KERNEL_SAMPLECOPY(2, 2, yy);
// Source RGBA
// Result R
#pragma kernel KSampleCopy4_1_x
KERNEL_SAMPLECOPY(4, 1, x);
#pragma kernel KSampleCopy4_1_y
KERNEL_SAMPLECOPY(4, 1, y);
#pragma kernel KSampleCopy4_1_z
KERNEL_SAMPLECOPY(4, 1, z);
#pragma kernel KSampleCopy4_1_w
KERNEL_SAMPLECOPY(4, 1, w);

10
ScriptableRenderPipeline/HDRenderPipeline/RenderPipelineResources/CopyChannel.compute.meta


fileFormatVersion: 2
guid: 4e910cec38a1ec640a03324015e036d0
timeCreated: 1506516404
licenseType: Pro
ComputeShaderImporter:
externalObjects: {}
currentAPIMask: 4
userData:
assetBundleName:
assetBundleVariant:

部分文件因为文件数量过多而无法显示

正在加载...
取消
保存