浏览代码

HDRednerLoop: First draft of meta pass for lighttransport

/main
sebastienlagarde 8 年前
当前提交
a149a4db
共有 9 个文件被更改,包括 216 次插入5 次删除
  1. 11
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Builtin/BuiltinData.cs
  2. 14
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Builtin/BuiltinData.cs.hlsl
  3. 15
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Builtin/BuiltinData.hlsl
  4. 71
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/DefaultLit.shader
  5. 18
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/Lit.hlsl
  6. 6
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LitCommon.hlsl
  7. 5
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderVariables.hlsl
  8. 9
      Assets/ScriptableRenderLoop/ShaderLibrary/Color.hlsl
  9. 72
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderPass/ShaderPassLightTransport.hlsl

11
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Builtin/BuiltinData.cs


[SurfaceDataAttributes("Distortion Blur")]
public float distortionBlur; // Define the color buffer mipmap level to use
};
//-----------------------------------------------------------------------------
// LighTransportData
// This struct is use to store information for Enlighten/Progressive light mapper. both at runtime or off line.
//-----------------------------------------------------------------------------
[GenerateHLSL(PackingRules.Exact, false, true, 120)]
public struct LighTransportData
{
public Vector3 diffuseColor;
public Vector3 emissiveColor;
};
}
}

14
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Builtin/BuiltinData.cs.hlsl


#define DEBUGVIEW_BUILTIN_BUILTINDATA_DISTORTION (105)
#define DEBUGVIEW_BUILTIN_BUILTINDATA_DISTORTION_BLUR (106)
//
// UnityEngine.ScriptableRenderLoop.Builtin.LighTransportData: static fields
//
#define DEBUGVIEW_BUILTIN_LIGHTRANSPORTDATA_DIFFUSE_COLOR (120)
#define DEBUGVIEW_BUILTIN_LIGHTRANSPORTDATA_EMISSIVE_COLOR (121)
// Generated from UnityEngine.ScriptableRenderLoop.Builtin.BuiltinData
// PackingRules = Exact
struct BuiltinData

float2 velocity;
float2 distortion;
float distortionBlur;
};
// Generated from UnityEngine.ScriptableRenderLoop.Builtin.LighTransportData
// PackingRules = Exact
struct LighTransportData
{
float3 diffuseColor;
float3 emissiveColor;
};

15
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Builtin/BuiltinData.hlsl


break;
}
}
void GetLighTransportDataDebug(uint paramId, LighTransportData lightTransportData, inout float3 result, inout bool needLinearToSRGB)
{
switch (paramId)
{
case DEBUGVIEW_BUILTIN_LIGHTRANSPORTDATA_DIFFUSE_COLOR:
result = lightTransportData.diffuseColor; needLinearToSRGB = true;
break;
case DEBUGVIEW_BUILTIN_LIGHTRANSPORTDATA_EMISSIVE_COLOR:
// TODO: Need a tonemap ?
result = lightTransportData.emissiveColor;
break;
}
}
#endif // UNITY_BUILTIN_DATA_INCLUDED

71
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/DefaultLit.shader


}
// ------------------------------------------------------------------
// Extracts information for lightmapping, GI (emission, albedo, ...)
// This pass it not used during regular rendering.
Pass
{
Name "META"
Tags{ "LightMode" = "Meta" }
Cull Off
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment Frag
CBUFFER_START(UnityMetaPass)
// x = use uv1 as raster position
// y = use uv2 as raster position
bool4 unity_MetaVertexControl;
// x = return albedo
// y = return normal
bool4 unity_MetaFragmentControl;
CBUFFER_END
// This was not in constant buffer in original unity, so keep outiside. But should be in as ShaderRenderPass frequency
float unity_OneOverOutputBoost;
float unity_MaxOutputValue;
struct Varyings
{
float4 positionHS;
float2 texCoord1;
float2 texCoord2;
};
struct PackedVaryings
{
float4 positionHS : SV_Position;
float4 interpolators[1] : TEXCOORD0;
};
// Function to pack data to use as few interpolator as possible, the ShaderGraph should generate these functions
PackedVaryings PackVaryings(Varyings input)
{
PackedVaryings output;
output.positionHS = input.positionHS;
output.interpolators[0].xy = input.texCoord1;
output.interpolators[0].zw = input.texCoord2;
return output;
}
Varyings UnpackVaryings(PackedVaryings input)
{
Varyings output;
output.positionHS = input.positionHS;
output.texCoord0 = input.interpolators[0].xy;
output.texCoord1 = input.interpolators[0].zw;
return output;
}
#include "../../ShaderPass/ShaderPassLightTransport.hlsl"
ENDHLSL
}
// ------------------------------------------------------------------
// forward pass
Pass
{

18
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/Lit.hlsl


}
//-----------------------------------------------------------------------------
// light transport functions
//-----------------------------------------------------------------------------
LighTransportData GetLightTransportData(SurfaceData surfaceData, BuiltinData builtinData, BSDFData bsdfData)
{
LighTransportData lightTransportData;
// diffuseColor for lightmapping should basically be diffuse color.
// But rough metals (black diffuse) still scatter quite a lot of light around, so
// we want to take some of that into account too.
lightTransportData.diffuseColor = bsdfData.diffuseColor + bsdfData.fresnel0 * bsdfData.roughness * 0.5 * surfaceData.metalic;
lightTransportData.emissiveColor = builtinData.emissiveColor * builtinData.emissiveIntensity;
return lightTransportData;
}
//-----------------------------------------------------------------------------
// BSDF share between area light (reference) and punctual light
//-----------------------------------------------------------------------------

6
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LitCommon.hlsl


float3 positionOS : POSITION;
float3 normalOS : NORMAL;
float2 uv0 : TEXCOORD0;
// float2 uv1 : TEXCOORD1;
//#if defined(DYNAMICLIGHTMAP_ON) || defined(UNITY_PASS_META)
// float2 uv2 : TEXCOORD2;
//#endif
// UNITY_INSTANCE_ID
};
struct Varyings

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


CBUFFER_END
CBUFFER_START(UnityLightmaps)
float4 unity_LightmapST;
float4 unity_DynamicLightmapST;
CBUFFER_END
// ----------------------------------------------------------------------------
// TODO: move this to constant buffer by Pass

9
Assets/ScriptableRenderLoop/ShaderLibrary/Color.hlsl


return max(vRGB, float3(0.0, 0.0, 0.0));
}
// TODO: check what is really use by the lightmap... should be hardcoded
// This function must handle various crappy case of lightmap ?
float4 UnityEncodeRGBM (float3 rgb, float maxRGBM)
// TODO: This function is used with the LightTransport pass to encode lightmap or emissive
float4 PackRGBM(float3 rgb, float maxRGBM)
{
float kOneOverRGBMMaxRange = 1.0 / maxRGBM;
const float kMinMultiplier = 2.0 * 1e-2;

// Alternative...
#define RGBMRANGE (8.0)
float4 packRGBM(float3 color)
float4 PackRGBM(float3 color)
{
float4 rgbm;
color *= (1.0 / RGBMRANGE);

return rgbm;
}
float3 unpackRGBM(float4 rgbm)
float3 UnpackRGBM(float4 rgbm)
{
return RGBMRANGE * rgbm.rgb * rgbm.a;
}

72
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderPass/ShaderPassLightTransport.hlsl


float4 UnityMetaVertexPosition(float4 vertex, float2 uv1, float2 uv2, float4 lightmapST, float4 dynlightmapST)
{
if (unity_MetaVertexControl.x)
{
vertex.xy = uv1 * lightmapST.xy + lightmapST.zw;
// OpenGL right now needs to actually use incoming vertex position,
// so use it in a very dummy way
vertex.z = vertex.z > 0 ? 1.0e-4f : 0.0f;
}
if (unity_MetaVertexControl.y)
{
vertex.xy = uv2 * dynlightmapST.xy + dynlightmapST.zw;
// OpenGL right now needs to actually use incoming vertex position,
// so use it in a very dummy way
vertex.z = vertex.z > 0 ? 1.0e-4f : 0.0f;
}
return UnityObjectToClipPos(vertex);
}
v2f_meta vert_meta(Attributes v)
{
Varyings output;
// Output UV coordinate in vertex shader
output.positionHS = UnityMetaVertexPosition(v.positionOS, v.uv1.xy, v.uv2.xy, unity_LightmapST, unity_DynamicLightmapST);
output.texCoord0 = v.uv0;
output.texCoord1 = v.uv1;
return PackVaryings(output);
}
#if SHADER_STAGE_FRAGMENT
// TODO: This is the max value allowed for emissive (bad name - but keep for now to retrieve it) (It is 8^2.2 (gamma) and 8 is the limit of punctual light slider...), comme from UnityCg.cginc. Fix it!
// Ask Jesper if this can be change for HDRenderLoop
#define EMISSIVE_RGBM_SCALE 97.0
float4 Frag(PackedVaryings packedInput) : SV_Target
{
Varyings input = UnpackVaryings(packedInput);
float3 V = GetWorldSpaceNormalizeViewDir(input.positionWS);
float3 positionWS = input.positionWS;
SurfaceData surfaceData;
BuiltinData builtinData;
GetSurfaceAndBuiltinData(input, surfaceData, builtinData);
BSDFData bsdfData = ConvertSurfaceDataToBSDFData(surfaceData);
LighTransportData lightTransportData = GetLightTransportData(surfaceData, builtinData, bsdfData);
// This shader is call two time. Once for getting emissiveColor, the other time to get diffuseColor
// We use unity_MetaFragmentControl to make the distinction.
float4 res = float4(0.0, 0.0, 0.0, 1.0);
// TODO: No if / else in original code from Unity, why ? keep like original code but should be either diffuse or emissive
if (unity_MetaFragmentControl.x)
{
// Apply diffuseColor Boost from LightmapSettings.
res.rgb = clamp(pow(lightTransportData.diffuseColor, saturate(unity_OneOverOutputBoost)), 0, unity_MaxOutputValue);
}
if (unity_MetaFragmentControl.y)
{
// TODO: THIS LIMIT MUST BE REMOVE, IT IS NOT HDR, change when RGB9e5 is here.
// Do we assume here that emission is [0..1] ?
res = PackRGBM(lightTransportData.emissiveColor, EMISSIVE_RGBM_SCALE);
}
return res;
}
#endif
正在加载...
取消
保存