浏览代码

[ColorPyramid] (wip) refactoring of color pyramid

/feature-ScreenSpaceProjection
Frédéric Vauchelles 7 年前
当前提交
e9e53a3d
共有 3 个文件被更改,包括 135 次插入50 次删除
  1. 63
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs
  2. 111
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/RenderPipelineResources/ColorPyramid.cs
  3. 11
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/RenderPipelineResources/ColorPyramid.cs.meta

63
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs


Material m_CopyStencilForNoLighting;
GPUCopy m_GPUCopy;
DepthPyramid m_DepthPyramid;
ColorPyramid m_ColorPyramid;
IBLFilterGGX m_IBLFilterGGX = null;

m_CameraMotionVectorsMaterial = CoreUtils.CreateEngineMaterial(asset.renderPipelineResources.cameraMotionVectors);
InitializeDebugMaterials();
// Require m_Blit in InitializeDebugMaterials();
m_ColorPyramid = new ColorPyramid(
asset.renderPipelineResources.gaussianPyramidCS,
m_GPUCopy,
m_Blit,
HDShaderIDs._BlitTexture,
HDShaderIDs._GaussianPyramidColorMips);
m_VelocityBuffer = HDShaderIDs._VelocityTexture;
m_VelocityBufferRT = new RenderTargetIdentifier(m_VelocityBuffer);

}
using (new ProfilingSample(cmd, "Gaussian Pyramid Color", CustomSamplerId.GaussianPyramidColor.GetSampler()))
{
var colorPyramidDesc = m_GaussianPyramidColorBufferDesc;
var pyramidSideSize = GetPyramidSize(colorPyramidDesc);
// The Gaussian pyramid compute works in blocks of 8x8 so make sure the last lod has a
// minimum size of 8x8
int lodCount = Mathf.FloorToInt(Mathf.Log(pyramidSideSize, 2f) - 3f);
if (lodCount > HDShaderIDs._GaussianPyramidColorMips.Length)
{
Debug.LogWarningFormat("Cannot compute all mipmaps of the color pyramid, max texture size supported: {0}", (2 << HDShaderIDs._GaussianPyramidColorMips.Length).ToString());
lodCount = HDShaderIDs._GaussianPyramidColorMips.Length;
}
cmd.SetGlobalVector(HDShaderIDs._GaussianPyramidColorMipSize, new Vector4(pyramidSideSize, pyramidSideSize, lodCount, 0));
cmd.SetGlobalTexture(HDShaderIDs._BlitTexture, m_CameraColorBufferRT);
CoreUtils.DrawFullScreen(cmd, m_Blit, m_GaussianPyramidColorBufferRT, null, 1); // Bilinear filtering
var last = m_GaussianPyramidColorBuffer;
colorPyramidDesc.sRGB = false;
colorPyramidDesc.enableRandomWrite = true;
colorPyramidDesc.useMipMap = false;
for (int i = 0; i < lodCount; i++)
{
colorPyramidDesc.width = colorPyramidDesc.width >> 1;
colorPyramidDesc.height = colorPyramidDesc.height >> 1;
// TODO: Add proper stereo support to the compute job
m_ColorPyramid.RenderPyramidColor(hdCamera, cmd, renderContext, m_CameraColorBufferRT, m_GaussianPyramidColorBufferRT);
cmd.ReleaseTemporaryRT(HDShaderIDs._GaussianPyramidColorMips[i + 1]);
cmd.GetTemporaryRT(HDShaderIDs._GaussianPyramidColorMips[i + 1], colorPyramidDesc, FilterMode.Bilinear);
cmd.SetComputeTextureParam(m_GaussianPyramidCS, m_GaussianPyramidKernel, "_Source", last);
cmd.SetComputeTextureParam(m_GaussianPyramidCS, m_GaussianPyramidKernel, "_Result", HDShaderIDs._GaussianPyramidColorMips[i + 1]);
cmd.SetComputeVectorParam(m_GaussianPyramidCS, "_Size", new Vector4(colorPyramidDesc.width, colorPyramidDesc.height, 1f / colorPyramidDesc.width, 1f / colorPyramidDesc.height));
cmd.DispatchCompute(m_GaussianPyramidCS, m_GaussianPyramidKernel, colorPyramidDesc.width / 8, colorPyramidDesc.height / 8, 1);
cmd.CopyTexture(HDShaderIDs._GaussianPyramidColorMips[i + 1], 0, 0, m_GaussianPyramidColorBufferRT, 0, i + 1);
last = HDShaderIDs._GaussianPyramidColorMips[i + 1];
}
PushFullScreenDebugTextureMip(cmd, m_GaussianPyramidColorBufferRT, lodCount, m_GaussianPyramidColorBufferDesc, hdCamera, isPreRefraction ? FullScreenDebugMode.PreRefractionColorPyramid : FullScreenDebugMode.FinalColorPyramid);
cmd.SetGlobalTexture(HDShaderIDs._GaussianPyramidColorTexture, m_GaussianPyramidColorBuffer);
for (int i = 0; i < lodCount; i++)
cmd.ReleaseTemporaryRT(HDShaderIDs._GaussianPyramidColorMips[i + 1]);
// TODO: Why don't we use _GaussianPyramidColorMips[0]?
}
var size = new Vector4(m_ColorPyramid.renderTextureDescriptor.width, m_ColorPyramid.renderTextureDescriptor.height, m_ColorPyramid.usedMipMapCount, 0);
cmd.SetGlobalVector(HDShaderIDs._GaussianPyramidColorMipSize, size);
PushFullScreenDebugTextureMip(cmd, m_GaussianPyramidColorBufferRT, m_ColorPyramid.usedMipMapCount, m_GaussianPyramidColorBufferDesc, hdCamera, isPreRefraction ? FullScreenDebugMode.PreRefractionColorPyramid : FullScreenDebugMode.FinalColorPyramid);
}
void RenderPyramidDepth(HDCamera hdCamera, CommandBuffer cmd, ScriptableRenderContext renderContext, FullScreenDebugMode debugMode)

111
ScriptableRenderPipeline/HDRenderPipeline/HDRP/RenderPipelineResources/ColorPyramid.cs


using UnityEngine.Rendering;
namespace UnityEngine.Experimental.Rendering.HDPipeline
{
class ColorPyramid
{
const int k_ColorBlockSize = 4;
ComputeShader m_ColorPyramidCS;
GPUCopy m_GPUCopy;
Material m_Blit;
int m_BlitTextureId;
RenderTextureDescriptor m_RenderTextureDescriptor;
int[] m_ColorPyramidMips = new int[0];
int m_ColorPyramidKernel;
public RenderTextureDescriptor renderTextureDescriptor { get { return m_RenderTextureDescriptor; } }
public int usedMipMapCount { get { return Mathf.Min(bufferMipMapCount, m_ColorPyramidMips.Length); } }
public int bufferMipMapCount
{
get
{
var minSize = Mathf.Min(renderTextureDescriptor.width, renderTextureDescriptor.height);
return Mathf.FloorToInt(Mathf.Log(minSize, 2f));
}
}
public ColorPyramid(ComputeShader colorPyramidCS, GPUCopy gpuCopy, Material blit, int blitTextureId, int[] mipIds)
{
m_ColorPyramidCS = colorPyramidCS;
m_GPUCopy = gpuCopy;
m_Blit = blit;
m_BlitTextureId = blitTextureId;
m_ColorPyramidKernel = m_ColorPyramidCS.FindKernel("KMain");
m_ColorPyramidMips = mipIds;
}
public void RenderPyramidColor(
HDCamera hdCamera,
CommandBuffer cmd,
ScriptableRenderContext renderContext,
RenderTargetIdentifier colorTexture,
RenderTargetIdentifier targetTexture)
{
var colorPyramidDesc = renderTextureDescriptor;
var lodCount = bufferMipMapCount;
if (lodCount > m_ColorPyramidMips.Length)
{
Debug.LogWarningFormat("Cannot compute all mipmaps of the color pyramid, max texture size supported: {0}", (2 << m_ColorPyramidMips.Length).ToString());
lodCount = m_ColorPyramidMips.Length;
}
cmd.SetGlobalTexture(m_BlitTextureId, colorTexture);
CoreUtils.DrawFullScreen(cmd, m_Blit, colorTexture, null, 1); // Bilinear filtering
var last = colorTexture;
colorPyramidDesc.sRGB = false;
colorPyramidDesc.enableRandomWrite = true;
colorPyramidDesc.useMipMap = false;
for (var i = 0; i < lodCount; i++)
{
colorPyramidDesc.width = colorPyramidDesc.width >> 1;
colorPyramidDesc.height = colorPyramidDesc.height >> 1;
// TODO: Add proper stereo support to the compute job
cmd.ReleaseTemporaryRT(m_ColorPyramidMips[i + 1]);
cmd.GetTemporaryRT(m_ColorPyramidMips[i + 1], colorPyramidDesc, FilterMode.Bilinear);
cmd.SetComputeTextureParam(m_ColorPyramidCS, m_ColorPyramidKernel, "_Source", last);
cmd.SetComputeTextureParam(m_ColorPyramidCS, m_ColorPyramidKernel, "_Result", m_ColorPyramidMips[i + 1]);
cmd.SetComputeVectorParam(m_ColorPyramidCS, "_Size", new Vector4(colorPyramidDesc.width, colorPyramidDesc.height, 1f / colorPyramidDesc.width, 1f / colorPyramidDesc.height));
cmd.DispatchCompute(m_ColorPyramidCS, m_ColorPyramidKernel, colorPyramidDesc.width / 8, colorPyramidDesc.height / 8, 1);
cmd.CopyTexture(m_ColorPyramidMips[i + 1], 0, 0, targetTexture, 0, i + 1);
last = m_ColorPyramidMips[i + 1];
}
for (int i = 0; i < lodCount; i++)
cmd.ReleaseTemporaryRT(m_ColorPyramidMips[i + 1]);
}
public void Initialize(HDCamera hdCamera, bool enableStereo)
{
var desc = hdCamera.renderTextureDesc;
desc.colorFormat = RenderTextureFormat.RFloat;
desc.depthBufferBits = 0;
desc.useMipMap = true;
desc.autoGenerateMips = false;
desc.msaaSamples = 1; // These are approximation textures, they don't need MSAA
// for stereo double-wide, each half of the texture will represent a single eye's pyramid
//var widthModifier = 1;
//if (stereoEnabled && (desc.dimension != TextureDimension.Tex2DArray))
// widthModifier = 2; // double-wide
//desc.width = pyramidSize * widthModifier;
desc.width = (int)hdCamera.screenSize.x;
desc.height = (int)hdCamera.screenSize.y;
m_RenderTextureDescriptor = desc;
}
}
}

11
ScriptableRenderPipeline/HDRenderPipeline/HDRP/RenderPipelineResources/ColorPyramid.cs.meta


fileFormatVersion: 2
guid: 334384fee1fa7b44486f0503cf7b24a0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存