浏览代码
Merge pull request #1505 from Unity-Technologies/normal-buffer-support
Merge pull request #1505 from Unity-Technologies/normal-buffer-support
Add export of normal+Roughness during depth prepass for forward/main
GitHub
7 年前
当前提交
84c9e15c
共有 27 个文件被更改,包括 375 次插入 和 82 次删除
-
5com.unity.render-pipelines.core/CoreRP/ShaderLibrary/CommonMaterial.hlsl
-
16com.unity.render-pipelines.high-definition/HDRP/Editor/Material/Lit/LitShaderPreprocessor.cs
-
24com.unity.render-pipelines.high-definition/HDRP/Lighting/DeferredDirectionalShadow.compute
-
8com.unity.render-pipelines.high-definition/HDRP/Lighting/LightLoop/LightLoop.cs
-
12com.unity.render-pipelines.high-definition/HDRP/Material/LayeredLit/LayeredLit.shader
-
12com.unity.render-pipelines.high-definition/HDRP/Material/LayeredLit/LayeredLitTessellation.shader
-
62com.unity.render-pipelines.high-definition/HDRP/Material/Lit/Lit.hlsl
-
12com.unity.render-pipelines.high-definition/HDRP/Material/Lit/Lit.shader
-
2com.unity.render-pipelines.high-definition/HDRP/Material/Lit/LitBuiltinData.hlsl
-
12com.unity.render-pipelines.high-definition/HDRP/Material/Lit/LitTessellation.shader
-
27com.unity.render-pipelines.high-definition/HDRP/Material/StackLit/StackLit.hlsl
-
4com.unity.render-pipelines.high-definition/HDRP/Material/StackLit/StackLit.shader
-
2com.unity.render-pipelines.high-definition/HDRP/Material/StackLit/StackLitData.hlsl
-
4com.unity.render-pipelines.high-definition/HDRP/Material/SubsurfaceScattering/SubsurfaceScattering.hlsl
-
2com.unity.render-pipelines.high-definition/HDRP/Material/Unlit/Unlit.hlsl
-
2com.unity.render-pipelines.high-definition/HDRP/Material/Unlit/Unlit.shader
-
64com.unity.render-pipelines.high-definition/HDRP/RenderPipeline/HDRenderPipeline.cs
-
8com.unity.render-pipelines.high-definition/HDRP/RenderPipeline/HDStringConstants.cs
-
15com.unity.render-pipelines.high-definition/HDRP/ShaderPass/ShaderPassDepthOnly.hlsl
-
2com.unity.render-pipelines.high-definition/HDRP/ShaderPass/ShaderPassForward.hlsl
-
2com.unity.render-pipelines.high-definition/HDRP/ShaderPass/ShaderPassForwardUnlit.hlsl
-
2com.unity.render-pipelines.high-definition/HDRP/ShaderPass/ShaderPassGBuffer.hlsl
-
2com.unity.render-pipelines.high-definition/HDRP/ShaderPass/ShaderPassLightTransport.hlsl
-
55com.unity.render-pipelines.high-definition/HDRP/Material/NormalBuffer.hlsl
-
9com.unity.render-pipelines.high-definition/HDRP/Material/NormalBuffer.hlsl.meta
-
81com.unity.render-pipelines.high-definition/HDRP/Material/NormalBufferManager.cs
-
11com.unity.render-pipelines.high-definition/HDRP/Material/NormalBufferManager.cs.meta
|
|||
#include "CoreRP/ShaderLibrary/Packing.hlsl" |
|||
#include "CoreRP/ShaderLibrary/CommonMaterial.hlsl" |
|||
|
|||
// ---------------------------------------------------------------------------- |
|||
// Encoding/decoding normal buffer functions |
|||
// ---------------------------------------------------------------------------- |
|||
|
|||
struct NormalData |
|||
{ |
|||
float3 normalWS; |
|||
float perceptualRoughness; |
|||
}; |
|||
|
|||
#define NormalBufferType0 float4 // Must match GBufferType1 in deferred |
|||
|
|||
// SSSBuffer texture declaration |
|||
TEXTURE2D(_NormalBufferTexture0); |
|||
|
|||
void EncodeIntoNormalBuffer(NormalData normalData, uint2 positionSS, out NormalBufferType0 outNormalBuffer0) |
|||
{ |
|||
// The sign of the Z component of the normal MUST round-trip through the G-Buffer, otherwise |
|||
// the reconstruction of the tangent frame for anisotropic GGX creates a seam along the Z axis. |
|||
// The constant was eye-balled to not cause artifacts. |
|||
// TODO: find a proper solution. E.g. we could re-shuffle the faces of the octahedron |
|||
// s.t. the sign of the Z component round-trips. |
|||
const float seamThreshold = 1.0 / 1024.0; |
|||
normalData.normalWS.z = CopySign(max(seamThreshold, abs(normalData.normalWS.z)), normalData.normalWS.z); |
|||
|
|||
// RT1 - 8:8:8:8 |
|||
// Our tangent encoding is based on our normal. |
|||
float2 octNormalWS = PackNormalOctQuadEncode(normalData.normalWS); |
|||
float3 packNormalWS = PackFloat2To888(saturate(octNormalWS * 0.5 + 0.5)); |
|||
// We store perceptualRoughness instead of roughness because it is perceptually linear. |
|||
outNormalBuffer0 = float4(packNormalWS, normalData.perceptualRoughness); |
|||
} |
|||
|
|||
void DecodeFromNormalBuffer(float4 normalBuffer, uint2 positionSS, out NormalData normalData) |
|||
{ |
|||
float3 packNormalWS = normalBuffer.rgb; |
|||
float2 octNormalWS = Unpack888ToFloat2(packNormalWS); |
|||
normalData.normalWS = UnpackNormalOctQuadEncode(octNormalWS * 2.0 - 1.0); |
|||
normalData.perceptualRoughness = normalBuffer.a; |
|||
} |
|||
|
|||
void DecodeFromNormalBuffer(uint2 positionSS, out NormalData normalData) |
|||
{ |
|||
float4 normalBuffer = LOAD_TEXTURE2D(_NormalBufferTexture0, positionSS); |
|||
DecodeFromNormalBuffer(normalBuffer, positionSS, normalData); |
|||
} |
|||
|
|||
// OUTPUT_NORMAL_NORMALBUFFER start from SV_Target0 as it is used during depth prepass where there is no color buffer |
|||
#define OUTPUT_NORMALBUFFER(NAME) out NormalBufferType0 MERGE_NAME(NAME, 0) : SV_Target0 |
|||
#define ENCODE_INTO_NORMALBUFFER(SURFACE_DATA, UNPOSITIONSS, NAME) EncodeIntoNormalBuffer(ConvertSurfaceDataToNormalData(SURFACE_DATA), UNPOSITIONSS, MERGE_NAME(NAME, 0)) |
|||
|
|||
#define DECODE_FROM_NORMALBUFFER(UNPOSITIONSS, NORMAL_DATA) DecodeFromNormalBuffer(UNPOSITIONSS, NORMAL_DATA) |
|
|||
fileFormatVersion: 2 |
|||
guid: c575b09819842744aa411a49f5a26660 |
|||
ShaderImporter: |
|||
externalObjects: {} |
|||
defaultTextures: [] |
|||
nonModifiableTextures: [] |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using UnityEngine.Rendering; |
|||
using System; |
|||
|
|||
namespace UnityEngine.Experimental.Rendering.HDPipeline |
|||
{ |
|||
public class NormalBufferManager |
|||
{ |
|||
// Currently we only support NormalBuffer with one buffer. If the shader code change, it may require to update the shader manager
|
|||
public const int k_MaxNormalBuffer = 1; |
|||
|
|||
public int normalBufferCount { get { return k_MaxNormalBuffer; } } |
|||
|
|||
RTHandleSystem.RTHandle[] m_ColorMRTs = new RTHandleSystem.RTHandle[k_MaxNormalBuffer]; |
|||
protected RenderTargetIdentifier[] m_RTIDs = new RenderTargetIdentifier[k_MaxNormalBuffer]; |
|||
bool[] m_ExternalBuffer = new bool[k_MaxNormalBuffer]; |
|||
|
|||
RTHandleSystem.RTHandle m_HTile; |
|||
|
|||
public NormalBufferManager() |
|||
{ |
|||
} |
|||
|
|||
public void InitNormalBuffers(GBufferManager gbufferManager, RenderPipelineSettings settings) |
|||
{ |
|||
if (settings.supportOnlyForward) |
|||
{ |
|||
// In case of full forward we must allocate the render target for normal buffer (or reuse one already existing)
|
|||
// TODO: Provide a way to reuse a render target
|
|||
m_ColorMRTs[0] = RTHandles.Alloc(Vector2.one, filterMode: FilterMode.Point, colorFormat: RenderTextureFormat.ARGB32, sRGB: false, name: "NormalBuffer"); |
|||
m_ExternalBuffer[0] = false; |
|||
} |
|||
else |
|||
{ |
|||
// In case of deferred, we must be in sync with NormalBuffer.hlsl and lit.hlsl files and setup the correct buffers
|
|||
m_ColorMRTs[0] = gbufferManager.GetBuffer(1); // Normal + Roughness is GBuffer(1)
|
|||
m_ExternalBuffer[0] = true; |
|||
} |
|||
} |
|||
|
|||
public RenderTargetIdentifier[] GetBuffersRTI() |
|||
{ |
|||
// nameID can change from one frame to another depending on the msaa flag so so we need to update this array to be sure it's up to date.
|
|||
for (int i = 0; i < normalBufferCount; ++i) |
|||
{ |
|||
m_RTIDs[i] = m_ColorMRTs[i].nameID; |
|||
} |
|||
|
|||
return m_RTIDs; |
|||
} |
|||
|
|||
public RTHandleSystem.RTHandle GetNormalBuffer(int index) |
|||
{ |
|||
Debug.Assert(index < normalBufferCount); |
|||
return m_ColorMRTs[index]; |
|||
} |
|||
|
|||
public void Build(HDRenderPipelineAsset hdAsset) |
|||
{ |
|||
} |
|||
|
|||
public void Cleanup() |
|||
{ |
|||
for (int i = 0; i < k_MaxNormalBuffer; ++i) |
|||
{ |
|||
if (!m_ExternalBuffer[i]) |
|||
{ |
|||
RTHandles.Release(m_ColorMRTs[i]); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public void BindNormalBuffers(CommandBuffer cmd) |
|||
{ |
|||
// NormalBuffer can be access in forward shader, so need to set global texture
|
|||
for (int i = 0; i < normalBufferCount; ++i) |
|||
{ |
|||
cmd.SetGlobalTexture(HDShaderIDs._NormalBufferTexture[i], GetNormalBuffer(i)); |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 37fd75386a4957c43b0e2d0311efe2c4 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue