浏览代码

Very minimal loop done, display red shader

Very minimal loop done, display red shader
/main
sebastienlagarde 8 年前
当前提交
76f890ea
共有 13 个文件被更改,包括 287 次插入39 次删除
  1. 2
      Assets/ScriptableRenderLoop/ShaderLibrary/BSDF.hlsl
  2. 29
      Assets/ScriptableRenderLoop/ShaderLibrary/Common.hlsl
  3. 23
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/Material/Material.hlsl
  4. 59
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/ShaderVariables.hlsl
  5. 53
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/UnityStandard.shader
  6. 4
      Assets/ScriptableRenderLoop/UnityStandard/UnityStandardRenderLoop.cs
  7. 60
      Assets/ScriptableRenderLoop/ShaderLibrary/Filtering.hlsl
  8. 8
      Assets/ScriptableRenderLoop/ShaderLibrary/Filtering.hlsl.meta
  9. 13
      Assets/ScriptableRenderLoop/UnityStandard/UnityStandardRenderLoop.asset
  10. 8
      Assets/ScriptableRenderLoop/UnityStandard/UnityStandardRenderLoop.asset.meta
  11. 59
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/Lighting/Lighting.hlsl
  12. 8
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/Lighting/Lighting.hlsl.meta

2
Assets/ScriptableRenderLoop/ShaderLibrary/BSDF.hlsl


float D_GGX(float NdotH, float roughness)
{
roughness = max(m, UNITY_MIN_ROUGHNESS);
roughness = max(roughness, UNITY_MIN_ROUGHNESS);
float a2 = roughness * roughness;
float f = (NdotH * a2 - NdotH) * NdotH + 1.0f;
return INV_PI * a2 / (f * f);

29
Assets/ScriptableRenderLoop/ShaderLibrary/Common.hlsl


#ifndef UNITY_COMMON_INCLUDED
#define UNITY_COMMON_INCLUDED
// Convention:
// space at the end of the variable name
// WS: world space
// VS: view space
// OS: object space
// HS: Homogenous clip space
// CS: clips space
// Example: NormalWS
// normalized / unormalized vector
// normalized direction are almost everywhere, we tag unormalized vector with un.
// Example: unL for unormalized light vector
// use capital letter for regular vector, vector are always pointing outward the current pixel position (ready for lighting equation)
// capital letter mean the vector is normalize, unless we put un in front of it.
// V: View vector (no eye vector)
// L: Light vector
// N: Normal vector
// H: Half vector
// Include platform header
#if defined(SHADER_API_XBOXONE)
#include "Platform/XboxOne.hlsl"

#if defined(SHADER_API_D3D11)
#include "API/D3D11.hlsl"
#endif
#endif
// ----------------------------------------------------------------------------

// Eberly's odd polynomial degree 5 - respect bounds
// 4 VGPR, 14 FR (10 FR, 1 QR), 2 scalar
// input [0, infinity] and output [0, PI/2]
float FastATanPos(float inX)
float FastATanPos(float x)
{
float t0 = (x < 1.0f) ? x : 1.0f / x;
float t1 = t0 * t0;

float t0 = FastATanPos(abs(x));
return (x < 0.0f) ? -t0: t0;
}
// ----------------------------------------------------------------------------
// World position reconstruction / transformation
// ----------------------------------------------------------------------------
#endif // UNITY_COMMON_INCLUDED

23
Assets/ScriptableRenderLoop/UnityStandard/Shaders/Material/Material.hlsl


#define UNITY_MATERIAL_INCLUDED
#include "../../ShaderLibrary/Packing.hlsl"
#include "../../ShaderLibrary/BSDF.hlsl"
#include "../Lighting/Lighting.hlsl"
#define DisneyGGXSurfaceData SurfaceData
#define DisneyGGXBSDFData BSDFData

float roughness;
float3 normalWS;
float perceptualRoughness;
};
//-----------------------------------------------------------------------------

{
DisneyGGXBSDFData output;
output.diffuseColor = data.diffuseColor;
output.occlusion = data.occlusion;
output.diffuseColor = data.diffuseColor;
output.occlusion = data.occlusion;
output.fresnel0 = data.specularColor;
output.roughness = SmoothnessToRoughness(data.smoothness);
output.fresnel0 = data.specularColor;
output.roughness = SmoothnessToRoughness(data.smoothness);
output.normalWS = data.normal;
output.normalWS = data.normal;
output.perceptualRoughness = SmoothnessToPerceptualRoughness(data.smoothness);
return output;
}

out float4 diffuseLighting,
out float4 specularLighting)
{
float3 unormalizedLightVector = light.pos - positionWS;
float3 L = normalize(unormalizedLightVector);
float3 unL = light.positionWS - positionWS;
float3 L = normalize(unL);
float attenuation = GetDistanceAttenuation(unormalizedLightVector, light.invSqrAttenuationRadius);
float attenuation = GetDistanceAttenuation(unL, light.invSqrAttenuationRadius);
// Always done, point and dir have it neutral
attenuation *= GetAngleAttenuation(L, light.forward, light.angleScale, light.angleOffset);
float illuminance = saturate(dot(material.normalWS, L)) * attenuation;

float NdotV = abs(dot(material.normalWS, V)) + 1e-5f; // TODO: check Eric idea about doing that when writting into the GBuffer (with our forward decal)
float3 H = normalize(V + L);
float LdotH = saturate(dot(L, H));
float NdotH = saturate(dot(material.normalWS, H));
float Vis = V_SmithGGX(NdotL, NdotV, material.roughness);
float Vis = V_SmithJointGGX(NdotL, NdotV, material.roughness);
float D = D_GGX(NdotH, material.roughness);
specularLighting.rgb = F * Vis * D;
float disneyDiffuse = DisneyDiffuse(NdotV, NdotL, LdotH, material.perceptualRoughness);

59
Assets/ScriptableRenderLoop/UnityStandard/Shaders/ShaderVariables.hlsl


#define UNITY_MATRIX_T_MV glstate_matrix_transpose_modelview0
#define UNITY_MATRIX_IT_MV glstate_matrix_invtrans_modelview0
// ----------------------------------------------------------------------------

CBUFFER_END
#endif // UNITY_SHADER_VARIABLES_INCLUDED
// ----------------------------------------------------------------------------
#endif // UNITY_SHADER_VARIABLES_INCLUDED
float4x4 GetObjectToWorldMatrix()
{
return unity_ObjectToWorld;
}
float4x4 GetWorldToObjectMatrix()
{
return unity_WorldToObject;
}
// Transform to homogenous clip space
float4x4 GetWorldToHClipMatrix()
{
return unity_MatrixVP;
}
float3 TransformObjectToWorld(float3 position)
{
return mul(GetObjectToWorldMatrix(), float4(position, 1.0));
}
float3 TransformObjectToWorldDir(float3 dir)
{
// Normalize to support uniform scaling
return normalize(mul((float3x3)GetObjectToWorldMatrix(), dir));
}
// Transforms normal from object to world space
float3 TransformObjectToWorldNormal(float3 norm)
{
#ifdef UNITY_ASSUME_UNIFORM_SCALING
return UnityObjectToWorldDir(norm);
#else
// Normal need to be multiply by inverse transpose
// mul(IT_M, norm) => mul(norm, I_M) => {dot(norm, I_M.col0), dot(norm, I_M.col1), dot(norm, I_M.col2)}
return normalize(mul(norm, (float3x3)GetWorldToObjectMatrix()));
#endif
}
// Tranforms position from view to homogenous space
float4 TransformWorldToHClip(float3 pos)
{
return mul(GetWorldToHClipMatrix(), float4(pos, 1.0));
}
float3x3 CreateTangentToWorld(float3 normal, float3 tangent, float tangentSign)
{
// For odd-negative scale transforms we need to flip the sign
float sign = tangentSign * unity_WorldTransformParams.w;
float3 binormal = cross(normal, tangent) * sign;
return float3x3(tangent, binormal, normal);
}

53
Assets/ScriptableRenderLoop/UnityStandard/Shaders/UnityStandard.shader


CGINCLUDE
#include "Material/Material.hlsl"
#include "ShaderVariables.hlsl"
ENDCG

CGPROGRAM
#pragma target 5.0
#pragma only_renderers d3d11 // TEMP: unitl we go futher in dev
// Include
#include "ShaderVariables.hlsl"
#pragma vertex MainVS
#pragma fragment MainPS

struct VSInput
{
float4 vertex : POSITION;
half3 normal : NORMAL;
float2 uv0 : TEXCOORD0;
//float2 uv1 : TEXCOORD1;
//#if defined(DYNAMICLIGHTMAP_ON) || defined(UNITY_PASS_META)
// float2 uv2 : TEXCOORD2;
//#endif
//#ifdef _TANGENT_TO_WORLD
half4 tangent : TANGENT;
//#endif
//UNITY_INSTANCE_ID
float4 positionOS : POSITION; // TODO: why do we provide w here ? putting constant to 1 will save a float
half3 normalOS : NORMAL;
float2 uv0 : TEXCOORD0;
half4 tangentOS : TANGENT;
float4 positionWS : SV_POSITION;
float4 positionHS : SV_POSITION;
float4 tangentToWorldAndParallax : TANGENT; // [3x3:tangentToWorld | 1x3:viewDirForParallax]
};
float4 tangentToWorld[3] : TEXCOORD1; // [3x3:tangentToWorld | 1x3:viewDirForParallax]
};
// TODO : here we must support anykind of vertex animation
PSInput o;
// TODO : here we must support anykind of vertex animation (GPU skinning, morphing) or better do it in compute.
VSOutput o;
float3 positionWS = TransformObjectToWorld(i.positionOS.xyz);
// TODO deal with camera center rendering and instancing (This is the reason why we always perform tow step transform to clip space + instancing matrix)
o.positionHS = TransformWorldToHClip(positionWS);
o.positionWS = UnityObjectToClipPos(v.vertex);
float3 normalWS = TransformObjectToWorldNormal(i.normalOS);
float3 positionWS = mul( unity_ObjectToWorld, i.vPositionOs.xyzw ).xyz;
o.positionWS.xyzw = mul( UNITY_MATRIX_MVP, float4( mul( unity_WorldToObject, float4( vPositionWs.xyz, 1.0 ) ).xyz, 1.0 ) );
o.texCoord0 = i.uv0;
#ifdef _TANGENT_TO_WORLD
float4 tangentWS = float4(TransformObjectToWorldDir(i.tangentOS.xyz), i.tangentOS.w);
float3x3 tangentToWorld = CreateTangentToWorld(normalWorld, tangentWorld.xyz, tangentWorld.w);
o.tangentToWorld[0].xyz = tangentToWorld[0];
o.tangentToWorld[1].xyz = tangentToWorld[1];
o.tangentToWorld[2].xyz = tangentToWorld[2];
#else
o.tangentToWorld[0].xyz = 0;
o.tangentToWorld[1].xyz = 0;
o.tangentToWorld[2].xyz = normalWS;
#endif
return o;
}

4
Assets/ScriptableRenderLoop/UnityStandard/UnityStandardRenderLoop.cs


{
public class UnityStandardRenderLoop : ScriptableRenderLoop
{
[MenuItem("Renderloop/UnityStandardRenderLoop")]
[MenuItem("Renderloop/CreateUnityStandardRenderLoop")]
static void CreateUnityStandardRenderLoop()
{
var instance = ScriptableObject.CreateInstance<UnityStandardRenderLoop>();

renderLoop.SetupCameraProperties (camera);
UpdateLightConstants(cullResults.culledLights, ref shadows);
//UpdateLightConstants(cullResults.culledLights, ref shadows);
DrawRendererSettings settings = new DrawRendererSettings (cullResults, camera, new ShaderPassName("Forward"));
settings.rendererConfiguration = RendererConfiguration.ConfigureOneLightProbePerRenderer | RendererConfiguration.ConfigureReflectionProbesProbePerRenderer;

60
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)
{
// 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
// location [1, 1] in the grid, where [0, 0] is the top left corner.
float2 samplePos = uv * texSize;
float2 texPos1 = floor(samplePos - 0.5f) + 0.5f;
// Compute the fractional offset from our starting texel to our original sample location, which we'll
// feed into the Catmull-Rom spline function to get our filter weights.
float2 f = samplePos - texPos1;
float2 f2 = f * f;
float2 f3 = f2 * f;
// Compute the Catmull-Rom weights using the fractional offset that we calculated earlier.
// These equations are pre-expanded based on our knowledge of where the texels will be located,
// which lets us avoid having to evaluate a piece-wise function.
float2 w0 = (1.0f / 6.0f) * (-3.0f * f3 + 6.0f * f2 - 3.0f * f);
float2 w1 = (1.0f / 6.0f) * (9.0f * f3 - 15.0f * f2 + 6.0f);
float2 w2 = (1.0f / 6.0f) * (-9.0f * f3 + 12.0f * f2 + 3.0f * f);
float2 w3 = (1.0f / 6.0f) * (3.0f * f3 - 3.0f * f2);
// Otim by Vlad, to test
// float2 w0 = (1.0 / 2.0) * f * (-1.0 + f * (2.0 - f));
// float2 w1 = (1.0 / 6.0) * f2 * (-15.0 + 9.0 * f)) + 1.0;
// float2 w2 = (1.0 / 6.0) * f * (3.0 + f * (12.0 - f * 9.0));
// float2 w3 = (1.0 / 2.0) * f2 * (f - 1.0);
// Work out weighting factors and sampling offsets that will let us use bilinear filtering to
// simultaneously evaluate the middle 2 samples from the 4x4 grid.
float2 w12 = w1 + w2;
float2 offset12 = w2 / (w1 + w2);
// Compute the final UV coordinates we'll use for sampling the texture
float2 texPos0 = texPos1 - 1;
float2 texPos3 = texPos1 + 2;
float2 texPos12 = texPos1 + offset12;
texPos0 /= texSize;
texPos3 /= texSize;
texPos12 /= texSize;
float4 result = 0.0f;
result += tex.SampleLevel(linearSampler, float2(texPos0.x, texPos0.y), 0.0f) * w0.x * w0.y;
result += tex.SampleLevel(linearSampler, float2(texPos12.x, texPos0.y), 0.0f) * w12.x * w0.y;
result += tex.SampleLevel(linearSampler, float2(texPos3.x, texPos0.y), 0.0f) * w3.x * w0.y;
result += tex.SampleLevel(linearSampler, float2(texPos0.x, texPos12.y), 0.0f) * w0.x * w12.y;
result += tex.SampleLevel(linearSampler, float2(texPos12.x, texPos12.y), 0.0f) * w12.x * w12.y;
result += tex.SampleLevel(linearSampler, float2(texPos3.x, texPos12.y), 0.0f) * w3.x * w12.y;
result += tex.SampleLevel(linearSampler, float2(texPos0.x, texPos3.y), 0.0f) * w0.x * w3.y;
result += tex.SampleLevel(linearSampler, float2(texPos12.x, texPos3.y), 0.0f) * w12.x * w3.y;
result += tex.SampleLevel(linearSampler, float2(texPos3.x, texPos3.y), 0.0f) * w3.x * w3.y;
return result;
}

8
Assets/ScriptableRenderLoop/ShaderLibrary/Filtering.hlsl.meta


fileFormatVersion: 2
guid: dac42c6a533d53b41b931e02431564fa
timeCreated: 1474355143
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

13
Assets/ScriptableRenderLoop/UnityStandard/UnityStandardRenderLoop.asset


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e5c35d625d1d6324a845b5d88af0ae92, type: 3}
m_Name: UnityStandardRenderLoop
m_EditorClassIdentifier:

8
Assets/ScriptableRenderLoop/UnityStandard/UnityStandardRenderLoop.asset.meta


fileFormatVersion: 2
guid: bbb9d949788aad640a70f108f114474d
timeCreated: 1474382158
licenseType: Pro
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

59
Assets/ScriptableRenderLoop/UnityStandard/Shaders/Lighting/Lighting.hlsl


#ifndef UNITY_LIGHTING_INCLUDED
#define UNITY_LIGHTING_INCLUDED
struct PunctualLightData
{
float3 positionWS;
float invSqrAttenuationRadius;
float3 color;
float unused;
float3 forward;
float diffuseScale;
float3 up;
float specularScale;
float3 right;
float shadowDimmer;
float angleScale;
float angleOffset;
float2 unused2;
};
//-----------------------------------------------------------------------------
// Attenuation functions
//-----------------------------------------------------------------------------
// Ref: Moving Frostbite to PBR
float SmoothDistanceAttenuation(float squaredDistance, float invSqrAttenuationRadius)
{
float factor = squaredDistance * invSqrAttenuationRadius;
float smoothFactor = saturate(1.0f - factor * factor);
return smoothFactor * smoothFactor;
}
#define PUNCTUAL_LIGHT_THRESHOLD 0.01 // 1cm (in Unity 1 is 1m)
float GetDistanceAttenuation(float3 unL, float invSqrAttenuationRadius)
{
float sqrDist = dot(unL, unL);
float attenuation = 1.0f / (max(PUNCTUAL_LIGHT_THRESHOLD * PUNCTUAL_LIGHT_THRESHOLD, sqrDist));
// Non physically based hack to limit light influence to attenuationRadius.
attenuation *= SmoothDistanceAttenuation(sqrDist, invSqrAttenuationRadius);
return attenuation;
}
float GetAngleAttenuation(float3 L, float3 lightDir, float lightAngleScale, float lightAngleOffset)
{
float cd = dot(lightDir, L);
float attenuation = saturate(cd * lightAngleScale + lightAngleOffset);
// smooth the transition
attenuation *= attenuation;
return attenuation;
}
#endif

8
Assets/ScriptableRenderLoop/UnityStandard/Shaders/Lighting/Lighting.hlsl.meta


fileFormatVersion: 2
guid: 5b115c7d70db5004ab39a1cfbead4db7
timeCreated: 1474359714
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存