浏览代码

Update a bit the code for deferred + some notation update

/main
sebastienlagarde 8 年前
当前提交
016758b2
共有 14 个文件被更改,包括 166 次插入27 次删除
  1. 6
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs
  2. 14
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/DisneyGGX.shader
  3. 2
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/Lighting.hlsl
  4. 10
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/DisneyGGX.hlsl
  5. 2
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/PostProcess/FinalPass.shader
  6. 19
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderVariables.hlsl
  7. 6
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/TemplateDisneyGGX.hlsl
  8. 10
      Assets/ScriptableRenderLoop/ShaderLibrary/Color.hlsl
  9. 2
      Assets/ScriptableRenderLoop/ShaderLibrary/Filtering.hlsl
  10. 4
      Assets/ScriptableRenderLoop/ShaderLibrary/Packing.hlsl
  11. 0
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/LightingDeferred.hlsl
  12. 9
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/LightingDeferred.hlsl.meta
  13. 100
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/LightingDeferred.shader
  14. 9
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/LightingDeferred.shader.meta

6
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs


s_punctualLightList = new ComputeBuffer(MaxLights, System.Runtime.InteropServices.Marshal.SizeOf(typeof(PunctualLightData)));
// Shader deferredMaterial = Shader.Find("Hidden/Unity/DeferredShading") as Shader;
// m_DeferredMaterial = new Material(deferredMaterial);
// m_DeferredMaterial.hideFlags = HideFlags.HideAndDontSave;
Shader deferredMaterial = Shader.Find("Hidden/Unity/LightingDeferred") as Shader;
m_DeferredMaterial = new Material(deferredMaterial);
m_DeferredMaterial.hideFlags = HideFlags.HideAndDontSave;
Shader finalPassShader = Shader.Find("Hidden/Unity/FinalPass") as Shader;
m_FinalPassMaterial = new Material(finalPassShader);

14
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/DisneyGGX.shader


Pass
{
Name "Forward" // Name is not used
Tags { "LightMode" = "Forward" }
Tags { "LightMode" = "Forward" } // This will be only for transparent object based on the RenderQueue index
Blend [_SrcBlend] [_DstBlend]
ZWrite [_ZWrite]

Pass
{
Name "GBuffer" // Name is not used
Tags { "LightMode" = "GBuffer" }
Tags { "LightMode" = "GBuffer" } // This will be only for opaque object based on the RenderQueue index
CGPROGRAM
#pragma target 5.0

#pragma fragment FragDeferred
#define UNITY_SHADERRENDERPASS UNITY_SHADERRENDERPASS_DEFERRED
#define UNITY_SHADERRENDERPASS UNITY_SHADERRENDERPASS_GBUFFER
out half4 outGBuffer0 : SV_Target0,
out half4 outGBuffer1 : SV_Target1,
out half4 outGBuffer2 : SV_Target2,
out half4 outEmission : SV_Target3
out float4 outGBuffer0 : SV_Target0,
out float4 outGBuffer1 : SV_Target1,
out float4 outGBuffer2 : SV_Target2,
out float4 outEmission : SV_Target3
)
{
Varyings input = UnpackVaryings(packedInput);

2
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/Lighting.hlsl


#if UNITY_SHADERRENDERPASS == UNITY_SHADERRENDERPASS_FORWARD
#include "LightingForward.hlsl"
#elif UNITY_SHADERRENDERPASS == UNITY_SHADERRENDERPASS_DEFERRED
#include "LightingDeferred.hlsl"
#endif
#endif // UNITY_LIGHTING_INCLUDED

10
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/DisneyGGX.hlsl


}
// This will encode UnityStandardData into GBuffer
void EncodeIntoGBuffer(SurfaceData data, out half4 outGBuffer0, out half4 outGBuffer1, out half4 outGBuffer2)
void EncodeIntoGBuffer(SurfaceData data, out float4 outGBuffer0, out float4 outGBuffer1, out float4 outGBuffer2)
outGBuffer0 = half4(data.diffuseColor, data.occlusion);
outGBuffer0 = float4(data.diffuseColor, data.occlusion);
outGBuffer1 = half4(data.specularColor, SmoothnessToPerceptualRoughness(data.smoothness));
outGBuffer1 = float4(data.specularColor, SmoothnessToPerceptualRoughness(data.smoothness));
outGBuffer2 = half4(PackNormalCartesian(data.normal), 1.0f);
outGBuffer2 = float4(PackNormalCartesian(data.normal), 1.0f);
BSDFData DecodeFromGBuffer(half4 inGBuffer0, half4 inGBuffer1, half4 inGBuffer2)
BSDFData DecodeFromGBuffer(float4 inGBuffer0, float4 inGBuffer1, float4 inGBuffer2)
{
BSDFData output;

2
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/PostProcess/FinalPass.shader


return output;
}
fixed4 Frag(Varyings input) : SV_Target
float4 Frag(Varyings input) : SV_Target
{
float4 c = tex2D(_MainTex, input.texcoord);
// Gamma correction

19
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderVariables.hlsl


return unity_MatrixVP;
}
float4x4 GetObjectToWorldViewMatrix()
{
return glstate_matrix_modelview0;
}
}
float3 TransformObjectToView(float3 positionOS)
{
return mul(GetObjectToWorldViewMatrix(), float4(positionOS, 1.0));
}
float3 TransformObjectToWorldDir(float3 dirOS)

float3 GetWorldSpaceNormalizeViewDir(float3 positionWS)
{
return normalize(_WorldSpaceCameraPos.xyz - positionWS);
}
float4 ComputeScreenPos(float4 positionH)
{
// TODO: upgrade this function to work for compute shader and pixel shaders
float4 positionSS = positionH * 0.5f;
positionSS.xy = float2(positionSS.x, positionSS.y * _ProjectionParams.x) + positionSS.w;
positionSS.zw = positionH.zw;
return positionSS;
}

6
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/TemplateDisneyGGX.hlsl


//-------------------------------------------------------------------------------------
// TODO: Check if we will have different Varyings based on different pass, not sure about that...
#if UNITY_SHADERRENDERPASS == UNITY_SHADERRENDERPASS_DEFERRED || UNITY_SHADERRENDERPASS == UNITY_SHADERRENDERPASS_FORWARD
#if UNITY_SHADERRENDERPASS == UNITY_SHADERRENDERPASS_GBUFFER || UNITY_SHADERRENDERPASS == UNITY_SHADERRENDERPASS_FORWARD
// Forward
struct Attributes

float2 uv0 : TEXCOORD0;
float4 tangentOS : TANGENT;
float4 tangentOS : TANGENT;
};
struct Varyings

{
Varyings output;
output.positionWS = TransformObjectToWorld(input.positionOS.xyz);
output.positionWS = TransformObjectToWorld(input.positionOS);
// TODO deal with camera center rendering and instancing (This is the reason why we always perform tow steps transform to clip space + instancing matrix)
output.positionHS = TransformWorldToHClip(output.positionWS);

10
Assets/ScriptableRenderLoop/ShaderLibrary/Color.hlsl


// Convert rgb to luminance
// with rgb in linear space with sRGB primaries and D65 white point
half Luminance(half3 linearRgb)
float Luminance(float3 linearRgb)
return dot(linearRgb, half3(0.2126729f, 0.7151522f, 0.0721750f));
return dot(linearRgb, float3(0.2126729f, 0.7151522f, 0.0721750f));
float4 PackLogLuv(in float3 vRGB)
float4 PackLogLuv(float3 vRGB)
{
// M matrix, for encoding
const float3x3 M = float3x3(

return vResult;
}
float3 UnpackLogLuv(in float4 vLogLuv)
float3 UnpackLogLuv(float4 vLogLuv)
{
// Inverse M matrix, for decoding
const float3x3 InverseM = float3x3(

// TODO: check what is really use by the lightmap... should be hardcoded
// This function must handle various crappy case of lightmap ?
half4 UnityEncodeRGBM (half3 rgb, float maxRGBM)
float4 UnityEncodeRGBM (float3 rgb, float maxRGBM)
{
float kOneOverRGBMMaxRange = 1.0 / maxRGBM;
const float kMinMultiplier = 2.0 * 1e-2;

2
Assets/ScriptableRenderLoop/ShaderLibrary/Filtering.hlsl


// Ref: https://gist.github.com/TheRealMJP/c83b8c0f46b63f3a88a5986f4fa982b1 from MJP
// Samples a texture with Catmull-Rom filtering, using 9 texture fetches instead of 16.
// See http://vec3.ca/bicubic-filtering-in-fewer-taps/ for more details
float4 SampleTextureCatmullRom(in Texture2D<float4> tex, in SamplerState linearSampler, in float2 uv, in float2 texSize)
float4 SampleTextureCatmullRom(Texture2D<float4> tex, SamplerState linearSampler, float2 uv, float2 texSize)
{
// We're going to sample a a 4x4 grid of texels surrounding the target UV coordinate. We'll do this by rounding
// down the sample location to get the exact center of our "starting" texel. The starting texel will be at

4
Assets/ScriptableRenderLoop/ShaderLibrary/Packing.hlsl


// Ref: http://jcgt.org/published/0003/02/01/paper.pdf
// Encode with Oct, this function work with any size of output
// return float between [-1, 1]
float2 PackNormalOctEncode(in float3 v)
float2 PackNormalOctEncode(float3 v)
{
float l1norm = abs(v.x) + abs(v.y) + abs(v.z);
float2 res0 = v.xy * (1.0f / l1norm);

}
float3 UnpackNormalOctEncode(in float x, in float y)
float3 UnpackNormalOctEncode(float x, float y)
{
float3 v = float3(x, y, 1.0f - abs(x) - abs(y));

0
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/LightingDeferred.hlsl

9
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/LightingDeferred.hlsl.meta


fileFormatVersion: 2
guid: 8ab751a0da67569489f8852db5423571
timeCreated: 1474899794
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

100
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/LightingDeferred.shader


Shader "Hidden/Unity/LightingDeferred" {
Properties{
_SrcBlend("", Float) = 1
_DstBlend("", Float) = 1
}
SubShader{
Pass{
ZWrite Off
Blend[_SrcBlend][_DstBlend]
CGPROGRAM
#pragma target 5.0
#pragma only_renderers d3d11 // TEMP: unitl we go futher in dev
#pragma vertex VertDeferred
#pragma fragment FragDeferred
#define UNITY_SHADERRENDERPASS UNITY_SHADERRENDERPASS_DEFERRED
// CAUTION: In case deferred lighting need to support various lighting model statically, we will require to do multicompile with different define like UNITY_MATERIAL_DISNEYGXX
#define UNITY_MATERIAL_DISNEYGXX // Need to be define before including Material.hlsl
#include "Lighting/Lighting.hlsl" // This include Material.hlsl
#include "ShaderVariables.hlsl"
sampler2D _CameraGBufferTexture0;
sampler2D _CameraGBufferTexture1;
sampler2D _CameraGBufferTexture2;
sampler2D _CameraGBufferTexture3;
sampler2D_float _CameraDepthTexture;
float _LightAsQuad;
struct Attributes
{
float3 positionOS : POSITION;
float3 normalOS : NORMAL;
};
struct Varyings
{
float4 positionHS : SV_POSITION;
float4 uv : TEXCOORD0;
float3 ray : TEXCOORD1;
};
Varyings VertDeferred(Attributes input)
{
Varyings output;
output.positionWS = TransformObjectToWorld(input.positionOS);
output.positionHS = TransformWorldToHClip(output.positionWS);
output.uv = ComputeScreenPos(o.positionOS);
output.ray = TransformObjectToView(input.positionOS) * float3(-1, -1, 1);
// normal contains a ray pointing from the camera to one of near plane's
// corners in camera space when we are drawing a full screen quad.
// Otherwise, when rendering 3D shapes, use the ray calculated here.
output.ray = lerp(output.ray, normalOS, _LightAsQuad);
return output;
}
float4 FragDeferred(Varyings input) : SV_Target
{
/*
input.ray = input.ray * tex2D(_CameraDepthTexture.z / input.ray.z);
float2 uv = input.uv.xy / input.uv.w;
// read depth and reconstruct world position
float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, uv);
depth = Linear01Depth(depth);
float4 vpos = float4(i.ray * depth, 1);
float3 wpos = mul(unity_CameraToWorld, vpos).xyz;
// read depth and reconstruct world position
float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, uv);
depth = Linear01Depth(depth);
float4 vpos = float4(i.ray * depth, 1);
float3 wpos = mul(unity_CameraToWorld, vpos).xyz;
// unpack Gbuffer
float4 gbuffer0 = tex2D(_CameraGBufferTexture0, input.uv);
float4 gbuffer1 = tex2D(_CameraGBufferTexture1, input.uv);
float4 gbuffer2 = tex2D(_CameraGBufferTexture2, input.uv);
float4 gbuffer3 = tex2D(_CameraGBufferTexture3, input.uv);
BSDFData bsdfData = DecodeFromGBuffer(gbuffer0, gbuffer1, gbuffer2);
return bsdfData.diffuseColor;
*/
return float4(1, 0, 0, 0);
}
ENDCG
}
}
Fallback Off
}

9
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/LightingDeferred.shader.meta


fileFormatVersion: 2
guid: 338228fc9e979ca41bf09a981c10c69e
timeCreated: 1474899949
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存