浏览代码

Make deferred rendering wortk and match forward

/main
sebastienlagarde 8 年前
当前提交
a333a206
共有 8 个文件被更改,包括 34 次插入77 次删除
  1. 27
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs
  2. 2
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/DisneyGGX.shader
  3. 4
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/Lighting.hlsl
  4. 29
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/LightingDeferred.hlsl
  5. 12
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/LightingDeferred.shader
  6. 3
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/TemplateDisneyGGX.hlsl
  7. 30
      Assets/ScriptableRenderLoop/ShaderLibrary/Common.hlsl
  8. 4
      Assets/ScriptableRenderLoop/fptl/Internal-DeferredReflections.shader

27
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs


renderLoop.DrawRenderers(ref settings);
}
void RenderDeferredLighting(Camera camera, RenderLoop renderLoop)
Matrix4x4 GetViewProjectionMatrix(Camera camera)
Matrix4x4 view = camera.worldToCameraMatrix;
// Unity projection matrix project z in -1 .. 1, so change it to reconstruct from a z with 0..1
Matrix4x4 temp = new Matrix4x4();
temp.SetRow(0, new Vector4(1.0f, 0.0f, 0.0f, 0.0f));
temp.SetRow(1, new Vector4(0.0f, 1.0f, 0.0f, 0.0f));
temp.SetRow(2, new Vector4(0.0f, 0.0f, 0.5f, 0.5f));
temp.SetRow(3, new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
Matrix4x4 projh = temp * proj;
Matrix4x4 invProjh = projh.inverse;
m_DeferredMaterial.SetMatrix("_InvProjMatrix", invProjh);
// The actual projection matrix used in shaders is actually massaged a bit to work across all platforms
// (different Z value ranges etc.)
Matrix4x4 gpuProj = GL.GetGPUProjectionMatrix(proj, false);
Matrix4x4 gpuVP = gpuProj * view;
return camera.projectionMatrix * camera.worldToCameraMatrix;
}
void RenderDeferredLighting(Camera camera, RenderLoop renderLoop)
{
Matrix4x4 invViewProj = GetViewProjectionMatrix(camera).inverse;
m_DeferredMaterial.SetMatrix("_InvViewProjMatrix", invViewProj);
Vector4 screenSize = new Vector4();
screenSize.x = camera.pixelWidth;

RenderDeferredLighting(camera, renderLoop);
// RenderForward(cullResults, camera, renderLoop);
RenderForward(cullResults, camera, renderLoop);
FinalPass(renderLoop);

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


#pragma vertex VertDefault
#pragma fragment FragForward
#define UNITY_SHADERRENDERPASS UNITY_SHADERRENDERPASS_FORWARD
#include "TemplateDisneyGGX.hlsl"

#pragma vertex VertDefault
#pragma fragment FragDeferred
#define UNITY_SHADERRENDERPASS UNITY_SHADERRENDERPASS_GBUFFER
#include "TemplateDisneyGGX.hlsl"
void FragDeferred( PackedVaryings packedInput,

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


#include "Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Material.hlsl"
#if UNITY_SHADERRENDERPASS == UNITY_SHADERRENDERPASS_FORWARD
#elif UNITY_SHADERRENDERPASS == UNITY_SHADERRENDERPASS_DEFERRED
#include "LightingDeferred.hlsl"
#endif
#endif // UNITY_LIGHTING_INCLUDED

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


#ifndef UNITY_LIGHTING_DEFERRED_INCLUDED
#define UNITY_LIGHTING_DEFERRED_INCLUDED
//-----------------------------------------------------------------------------
// Simple deferred loop architecture
//-----------------------------------------------------------------------------
StructuredBuffer<PunctualLightData> g_punctualLightList;
int g_punctualLightCount;
// Currently just a copy/paste of forward ligthing as it is just for validation
void DeferredLighting(float3 V, float3 positionWS, BSDFData material,
out float4 diffuseLighting,
out float4 specularLighting)
{
diffuseLighting = float4(0.0, 0.0, 0.0, 0.0);
specularLighting = float4(0.0, 0.0, 0.0, 0.0);
for (int i = 0; i < g_punctualLightCount; ++i)
{
float4 localDiffuseLighting;
float4 localSpecularLighting;
EvaluateBSDF_Punctual(V, positionWS, g_punctualLightList[i], material, localDiffuseLighting, localSpecularLighting);
diffuseLighting += localDiffuseLighting;
specularLighting += localSpecularLighting;
}
}
#endif

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


#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.hlsl" // This include Material.hlsl

Texture2D _CameraDepthTexture;
float4 _ScreenSize;
float4x4 _InvProjMatrix;
float4x4 _InvViewProjMatrix;
struct Attributes
{

// No need to manage inverse depth, this is handled by the projection matrix
float depth = _CameraDepthTexture.Load(uint3(coord.unPositionSS, 0)).x;
//#if UNITY_REVERSED_Z
//depth = 1.0 - depth; // This should be in the proj matrix ?
//#endif
float3 positionWS = UnprojectToWorld(depth, coord.positionSS, _InvProjMatrix);
float3 positionWS = UnprojectToWorld(depth, coord.positionSS, _InvViewProjMatrix);
float3 V = GetWorldSpaceNormalizeViewDir(positionWS);
FETCH_GBUFFER(gbuffer, _CameraGBufferTexture, coord.unPositionSS);

float4 specularLighting;
ForwardLighting(V, positionWS, bsdfData, diffuseLighting, specularLighting);
//return float4(diffuseLighting.rgb + specularLighting.rgb, 1.0);
return float4(saturate(positionWS), 1.0f);
return float4(diffuseLighting.rgb + specularLighting.rgb, 1.0);
//return float4(positionWS.y / 10, 0, 0, 1.0);
}
ENDCG

3
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_GBUFFER || UNITY_SHADERRENDERPASS == UNITY_SHADERRENDERPASS_FORWARD
// Forward
struct Attributes

return PackVaryings(output);
}
#endif
//-------------------------------------------------------------------------------------
// Fill SurfaceData/Lighting data function

30
Assets/ScriptableRenderLoop/ShaderLibrary/Common.hlsl


return coord;
}
// screenPos is screen coordinate in [0..1]
// depth come directly from the depth buffer without any transformation
float4 GetClipPosition(float depth, float2 screenPos)
{
float4 positionCS = float4(screenPos.xy, depth, 1.0);
positionCS.xyz = positionCS.xyz * 2.0f - 1.0f;
#if UNITY_UV_STARTS_AT_TOP == 1
positionCS.y = -positionCS.y;
#endif
return positionCS;
}
// screenPos is screen coordinate in [0..1]
// (can be provide by VPOS and divide by screen size)
// screenPos is screen coordinate in [0..1] (return by Coordinate.positionSS)
// depth must be the depth from the raw depth buffer. This allow to handle all kind of depth automatically with the inverse view projection matrix.
// For information. In Unity Depth is always in range 0..1 (even on OpenGL) but can be reversed.
float4 positionCS = GetClipPosition(depth, screenPos);
float4 hpositionWS = mul(invViewProjectionMatrix, positionCS);
// CAUTION: The camera projection we get with Unity don't include the reverse stuff and the depth need to be remap manually to -1..1
// We should simplify the process and have a way to do Camera.Projection matrix that return the projection martix use to project depth.
#if UNITY_REVERSED_Z
depth = 1.0 - depth;
#endif
depth = depth * 2.0 - 1.0;
float4 positionHS = float4(screenPos.xy * 2.0 - 1.0, depth, 1.0);
float4 hpositionWS = mul(invViewProjectionMatrix, positionHS);
return hpositionWS.xyz / hpositionWS.w;
}

4
Assets/ScriptableRenderLoop/fptl/Internal-DeferredReflections.shader


sampleDir = worldNormalRefl;
Unity_GlossyEnvironmentData g;
g.perceptualRoughness = SmoothnessToPerceptualRoughness(data.smoothness);
g.roughness = SmoothnessToPerceptualRoughness(data.smoothness);
g.reflUVW = sampleDir;
half3 env0 = Unity_GlossyEnvironment(UNITY_PASS_TEXCUBEARRAY(_reflCubeTextures), lgtDat.iSliceIndex, float4(lgtDat.fLightIntensity, lgtDat.fDecodeExp, 0.0, 0.0), g);

// TODO: remove pow, store cubemap mips differently
half perceptualRoughness = pow(glossIn.perceptualRoughness, 3.0/4.0);
#else
half perceptualRoughness = glossIn.perceptualRoughness; // MM: switched to this
half perceptualRoughness = glossIn.roughness; // MM: switched to this
#endif
//perceptualRoughness = sqrt(sqrt(2/(64.0+2))); // spec power to the square root of real roughness

正在加载...
取消
保存