浏览代码

decal normal buffer works, 1st draft

/main
Paul Melamed 6 年前
当前提交
5f083851
共有 9 个文件被更改,包括 256 次插入8 次删除
  1. 3
      com.unity.render-pipelines.high-definition/HDRP/Editor/RenderPipeline/HDAssetFactory.cs
  2. 147
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/Decal.shader
  3. 2
      com.unity.render-pipelines.high-definition/HDRP/Material/GBufferManager.cs
  4. 8
      com.unity.render-pipelines.high-definition/HDRP/Material/Lit/Lit.shader
  5. 1
      com.unity.render-pipelines.high-definition/HDRP/RenderPipeline/HDCustomSamplerId.cs
  6. 14
      com.unity.render-pipelines.high-definition/HDRP/RenderPipeline/HDRenderPipeline.cs
  7. 3
      com.unity.render-pipelines.high-definition/HDRP/RenderPipelineResources/RenderPipelineResources.cs
  8. 77
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/DecalNormalBuffer.shader
  9. 9
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/DecalNormalBuffer.shader.meta

3
com.unity.render-pipelines.high-definition/HDRP/Editor/RenderPipeline/HDAssetFactory.cs


newAsset.shadowBlurMoments = Load<ComputeShader>(CorePath + "Shadow/ShadowBlurMoments.compute");
newAsset.debugShadowMapShader = Load<Shader>(CorePath + "Shadow/DebugDisplayShadowMap.shader");
// decal
newAsset.decalNormalBuffer = Load<Shader>(HDRenderPipelinePath + "Material/Decal/DecalNormalBuffer.shader");
AssetDatabase.CreateAsset(newAsset, pathName);
ProjectWindowUtil.ShowCreatedAsset(newAsset);
}

147
com.unity.render-pipelines.high-definition/HDRP/Material/Decal/Decal.shader


Pass
{
Name "DBufferMesh_3RT" // Name is not used
Tags{"LightMode" = "DBufferMesh_3RT"} // Smoothness
Tags{"LightMode" = "DBufferMesh_3RT"} // Smoothness
Stencil
{
WriteMask 255
Ref[_StencilRef]
Comp Always
Pass IncrSat
}
ZWrite Off
ZTest LEqual
// using alpha compositing https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch23.html

Pass
{
Name "DBufferProjector_M" // Name is not used
Tags{"LightMode" = "DBufferProjector_M"} // Metalness
Tags{"LightMode" = "DBufferProjector_M"} // Metalness
Stencil
{
WriteMask 255
Ref[_StencilRef]
Comp Always
Pass IncrSat
}
// back faces with zfail, for cases when camera is inside the decal volume
Cull Front
ZWrite Off

Name "DBufferProjector_AO" // Name is not used
Tags{"LightMode" = "DBufferProjector_AO"} // AO only
// back faces with zfail, for cases when camera is inside the decal volume
Stencil
{
WriteMask 255
Ref[_StencilRef]
Comp Always
Pass IncrSat
}
Cull Front
ZWrite Off
ZTest Greater

Name "DBufferProjector_MAO" // Name is not used
Tags{"LightMode" = "DBufferProjector_MAO"} // AO + Metalness
// back faces with zfail, for cases when camera is inside the decal volume
Stencil
{
WriteMask 255
Ref[_StencilRef]
Comp Always
Pass IncrSat
}
Cull Front
ZWrite Off
ZTest Greater

Pass
{
Name "DBufferProjector_S" // Name is not used
Tags{"LightMode" = "DBufferProjector_S"} // Smoothness
Tags{"LightMode" = "DBufferProjector_S"} // Smoothness
Stencil
{
WriteMask 255
Ref[_StencilRef]
Comp Always
Pass IncrSat
}
// back faces with zfail, for cases when camera is inside the decal volume
Cull Front
ZWrite Off

Pass
{
Name "DBufferProjector_MS" // Name is not used
Tags{"LightMode" = "DBufferProjector_MS"} // Smoothness and Metalness
Tags{"LightMode" = "DBufferProjector_MS"} // Smoothness and Metalness
Stencil
{
WriteMask 255
Ref[_StencilRef]
Comp Always
Pass IncrSat
}
// back faces with zfail, for cases when camera is inside the decal volume
Cull Front
ZWrite Off

{
Name "DBufferProjector_AOS" // Name is not used
Tags{"LightMode" = "DBufferProjector_AOS"} // AO + Smoothness
Stencil
{
WriteMask 255
Ref[_StencilRef]
Comp Always
Pass IncrSat
}
// back faces with zfail, for cases when camera is inside the decal volume
Cull Front
ZWrite Off

{
Name "DBufferProjector_MAOS" // Name is not used
Tags { "LightMode" = "DBufferProjector_MAOS" } // Metalness AO and Smoothness
Stencil
{
WriteMask 255
Ref[_StencilRef]
Comp Always
Pass IncrSat
}
// back faces with zfail, for cases when camera is inside the decal volume
Cull Front
ZWrite Off

Pass
{
Name "DBufferMesh_M" // Name is not used
Tags{"LightMode" = "DBufferMesh_M"} // Metalness
Tags{"LightMode" = "DBufferMesh_M"} // Metalness
Stencil
{
WriteMask 255
Ref[_StencilRef]
Comp Always
Pass IncrSat
}
ZWrite Off
ZTest LEqual
// using alpha compositing https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch23.html

{
Name "DBufferMesh_AO" // Name is not used
Tags{"LightMode" = "DBufferMesh_AO"} // AO only
Stencil
{
WriteMask 255
Ref[_StencilRef]
Comp Always
Pass IncrSat
}
ZWrite Off
ZTest LEqual
// using alpha compositing https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch23.html

{
Name "DBufferMesh_MAO" // Name is not used
Tags{"LightMode" = "DBufferMesh_MAO"} // AO + Metalness
Stencil
{
WriteMask 255
Ref[_StencilRef]
Comp Always
Pass IncrSat
}
ZWrite Off
ZTest LEqual
// using alpha compositing https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch23.html

Pass
{
Name "DBufferMesh_S" // Name is not used
Tags{"LightMode" = "DBufferMesh_S"} // Smoothness
Tags{"LightMode" = "DBufferMesh_S"} // Smoothness
Stencil
{
WriteMask 255
Ref[_StencilRef]
Comp Always
Pass IncrSat
}
ZWrite Off
ZTest LEqual
// using alpha compositing https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch23.html

Pass
{
Name "DBufferMesh_MS" // Name is not used
Tags{"LightMode" = "DBufferMesh_MS"} // Smoothness and Metalness
Tags{"LightMode" = "DBufferMesh_MS"} // Smoothness and Metalness
Stencil
{
WriteMask 255
Ref[_StencilRef]
Comp Always
Pass IncrSat
}
ZWrite Off
ZTest LEqual
// using alpha compositing https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch23.html

{
Name "DBufferMesh_AOS" // Name is not used
Tags{"LightMode" = "DBufferMesh_AOS"} // AO + Smoothness
Stencil
{
WriteMask 255
Ref[_StencilRef]
Comp Always
Pass IncrSat
}
ZWrite Off
ZTest LEqual
// using alpha compositing https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch23.html

{
Name "DBufferMesh_MAOS" // Name is not used
Tags{"LightMode" = "DBufferMesh_MAOS"} // Metalness AO and Smoothness
Stencil
{
WriteMask 255
Ref[_StencilRef]
Comp Always
Pass IncrSat
}
ZWrite Off
ZTest LEqual
// using alpha compositing https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch23.html

2
com.unity.render-pipelines.high-definition/HDRP/Material/GBufferManager.cs


for (int gbufferIndex = 0; gbufferIndex < m_BufferCount; ++gbufferIndex)
{
m_RTs[gbufferIndex] = RTHandles.Alloc(Vector2.one, colorFormat: rtFormat[gbufferIndex], sRGB: sRGBFlags[gbufferIndex], filterMode: FilterMode.Point, name: string.Format("GBuffer{0}", gbufferIndex));
m_RTs[gbufferIndex] = RTHandles.Alloc(Vector2.one, colorFormat: rtFormat[gbufferIndex], sRGB: sRGBFlags[gbufferIndex], filterMode: FilterMode.Point, name: string.Format("GBuffer{0}", gbufferIndex), enableRandomWrite: gbufferIndex == 1);
m_RTIDs[gbufferIndex] = m_RTs[gbufferIndex].nameID;
m_TextureShaderIDs[gbufferIndex] = HDShaderIDs._GBufferTexture[gbufferIndex];

8
com.unity.render-pipelines.high-definition/HDRP/Material/Lit/Lit.shader


ZWrite On
Stencil
{
WriteMask 255
Ref[_StencilRef]
Comp Always
Pass IncrSat
}
HLSLPROGRAM
// In deferred, depth only pass don't output anything.

1
com.unity.render-pipelines.high-definition/HDRP/RenderPipeline/HDCustomSamplerId.cs


GBuffer,
DBufferRender,
DBufferPrepareDrawData,
DBufferNormal,
DisplayDebugDecalsAtlas,
DisplayDebugViewMaterial,
DebugViewMaterialGBuffer,

14
com.unity.render-pipelines.high-definition/HDRP/RenderPipeline/HDRenderPipeline.cs


int m_applyDistortionKernel;
Material m_CameraMotionVectorsMaterial;
Material m_DecalNormalBufferMaterial;
// Debug material
Material m_DebugViewMaterialGBuffer;

m_CopyStencilForNoLighting.SetInt(HDShaderIDs._StencilRef, (int)StencilLightingUsage.NoLighting);
m_CopyStencilForNoLighting.SetInt(HDShaderIDs._StencilMask, (int)StencilBitMask.LightingMask);
m_CameraMotionVectorsMaterial = CoreUtils.CreateEngineMaterial(asset.renderPipelineResources.cameraMotionVectors);
m_DecalNormalBufferMaterial = CoreUtils.CreateEngineMaterial(asset.renderPipelineResources.decalNormalBuffer);
m_CopyDepth = CoreUtils.CreateEngineMaterial(asset.renderPipelineResources.copyDepthBuffer);

CoreUtils.Destroy(m_CopyStencilForNoLighting);
CoreUtils.Destroy(m_CameraMotionVectorsMaterial);
CoreUtils.Destroy(m_DecalNormalBufferMaterial);
CoreUtils.Destroy(m_DebugViewMaterialGBuffer);
CoreUtils.Destroy(m_DebugViewMaterialGBufferShadowMask);

// We can now bind the normal buffer to be use by any effect
m_NormalBufferManager.BindNormalBuffers(cmd);
if (hdCamera.frameSettings.enableForwardRenderingOnly)
{
using (new ProfilingSample(cmd, "DBuffer Normal (forward)", CustomSamplerId.DBufferNormal.GetSampler()))
{
HDUtils.SetRenderTarget(cmd, hdCamera, m_CameraDepthStencilBuffer);
cmd.SetRandomWriteTarget(1, m_NormalBufferManager.GetNormalBuffer(0));
cmd.DrawProcedural(Matrix4x4.identity, m_DecalNormalBufferMaterial, 0, MeshTopology.Triangles, 3, 1);
cmd.ClearRandomWriteTargets();
}
}
// In both forward and deferred, everything opaque should have been rendered at this point so we can safely copy the depth buffer for later processing.
CopyDepthBufferIfNeeded(cmd);

3
com.unity.render-pipelines.high-definition/HDRP/RenderPipelineResources/RenderPipelineResources.cs


public Shader shadowClearShader;
public ComputeShader shadowBlurMoments;
public Shader debugShadowMapShader;
// Decal
public Shader decalNormalBuffer;
}
}

77
com.unity.render-pipelines.high-definition/HDRP/Material/Decal/DecalNormalBuffer.shader


Shader "Hidden/HDRenderPipeline/Material/Decal/DecalNormalBuffer"
{
HLSLINCLUDE
#pragma target 4.5
#pragma only_renderers d3d11 ps4 xboxone vulkan metal switch
#include "CoreRP/ShaderLibrary/Common.hlsl"
#include "../../ShaderVariables.hlsl"
#include "Decal.hlsl"
#include "../NormalBuffer.hlsl"
TEXTURE2D(_DBufferTexture1);
RW_TEXTURE2D(float4, _NormalBuffer);
SamplerState sampler_PointClamp;
struct Attributes
{
uint vertexID : SV_VertexID;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 texcoord : TEXCOORD0;
};
Varyings Vert(Attributes input)
{
Varyings output;
output.positionCS = GetFullScreenTriangleVertexPosition(input.vertexID);
output.texcoord = GetFullScreenTriangleTexCoord(input.vertexID);// *_BlitScaleBias.xy + _BlitScaleBias.zw;
return output;
}
float4 FragNearest(Varyings input) : SV_Target
{
float4 DBufferNormal = SAMPLE_TEXTURE2D(_DBufferTexture1, sampler_PointClamp, input.texcoord) * float4(2.0f, 2.0f, 2.0f, 1.0f) - float4(1.0f, 1.0f, 1.0f, 0.0f);
float4 GBufferNormal = _NormalBuffer[input.texcoord * _ScreenSize.xy];
NormalData normalData;
DecodeFromNormalBuffer(GBufferNormal, uint2(0, 0), normalData);
normalData.normalWS.xyz = normalize(normalData.normalWS.xyz * DBufferNormal.w + DBufferNormal.xyz);
EncodeIntoNormalBuffer(normalData, uint2(0, 0), GBufferNormal);
_NormalBuffer[input.texcoord * _ScreenSize.xy] = GBufferNormal;
return GBufferNormal;
}
ENDHLSL
SubShader
{
Tags{ "RenderPipeline" = "HDRenderPipeline" }
Pass
{
ZWrite Off
ZTest Always
Blend Off
Cull Off
Stencil
{
WriteMask 255
Ref 2
Comp Equal
Pass Zero
}
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment FragNearest
ENDHLSL
}
}
Fallback Off
}

9
com.unity.render-pipelines.high-definition/HDRP/Material/Decal/DecalNormalBuffer.shader.meta


fileFormatVersion: 2
guid: fd532bf1795188c4daaa66ea798b8b0a
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存