浏览代码

HDREnderLoop: Convert CGPROGRAM to HLSLPROGRAM + Add double sided support

Double sided still not work yet
/main
sebastienlagarde 8 年前
当前提交
0c45d07f
共有 10 个文件被更改,包括 97 次插入49 次删除
  1. 2
      .gitignore
  2. 50
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/DisneyGGX.cs
  3. 12
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/DisneyGGX.shader
  4. 4
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/LightingDeferred.shader
  5. 4
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/PostProcess/FinalPass.shader
  6. 28
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderVariables.hlsl
  7. 31
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/TemplateDisneyGGX.hlsl
  8. 4
      Assets/ScriptableRenderLoop/ShaderLibrary/API/D3D11.hlsl
  9. 9
      Assets/ScriptableRenderLoop/ShaderLibrary/API/Validate.hlsl
  10. 2
      Assets/ScriptableRenderLoop/ShaderLibrary/CommonLighting.hlsl

2
.gitignore


*.exe
*.aspx
Assets/UnityHDRI.meta
Assets/UnityHDRI/Gareoult/GareoultWhiteBalanced.exr

50
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/DisneyGGX.cs


EditorGUI.showMixedValue = false;
}
static public void SetupMaterialWithBlendMode(Material material, bool alphaTestEnable, SurfaceType surfaceType, BlendMode blendMode, DoubleSidedMode doubleSidedMode)
static public void SetupMaterial(Material material)
if (alphaTestEnable)
material.EnableKeyword("_ALPHATEST_ON");
// Note: keywords must be based on Material value not on MaterialProperty due to multi-edit & material animation
// (MaterialProperty value might come from renderer material property block)
bool alphaTestEnable = material.GetFloat("_AlphaCutoffEnable") == 1.0;
SurfaceType surfaceType = (SurfaceType)material.GetFloat("_SurfaceType");
BlendMode blendMode = (BlendMode)material.GetFloat("_BlendMode");
DoubleSidedMode doubleSidedMode = (DoubleSidedMode)material.GetFloat("_DoubleSidedMode");
if (surfaceType == SurfaceType.Opaque)
{

material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
break;
}
}
}
if (doubleSidedMode != DoubleSidedMode.None)
{

if (doubleSidedMode == DoubleSidedMode.DoubleSided)
if (doubleSidedMode == DoubleSidedMode.DoubleSidedLightingFlip)
else
else if (doubleSidedMode == DoubleSidedMode.DoubleSidedLightingMirror)
}
}
static bool ShouldEmissionBeEnabled(Material mat, Color color)
{
//var realtimeEmission = (mat.globalIlluminationFlags & MaterialGlobalIlluminationFlags.RealtimeEmissive) > 0;
//return color.maxColorComponent > 0.1f / 255.0f || realtimeEmission;
return false;
}
}
else
{
material.DisableKeyword("_DOUBLESIDED_LIGHTING_FLIP");
material.DisableKeyword("_DOUBLESIDED_LIGHTING_MIRROR");
}
static void SetMaterialKeywords(Material material)
{
// Note: keywords must be based on Material value not on MaterialProperty due to multi-edit & material animation
// (MaterialProperty value might come from renderer material property block)
SetKeyword(material, "_ALPHATEST_ON", alphaTestEnable);
SetKeyword(material, "_NORMALMAP", material.GetTexture("_NormalMap"));
SetKeyword(material, "_NORMALMAP_TANGENT_SPACE", (NormalMapSpace)material.GetFloat("_NormalMapSpace") == NormalMapSpace.TangentSpace);
SetKeyword(material, "_MASKMAP", material.GetTexture("_MaskMap"));

*/
}
static bool ShouldEmissionBeEnabled(Material mat, Color color)
{
//var realtimeEmission = (mat.globalIlluminationFlags & MaterialGlobalIlluminationFlags.RealtimeEmissive) > 0;
//return color.maxColorComponent > 0.1f / 255.0f || realtimeEmission;
return false;
}
bool HasValidEmissiveKeyword (Material material)
{
/*

static void MaterialChanged(Material material)
{
SetupMaterialWithBlendMode(material, material.GetFloat("_AlphaCutoffEnable") == 1.0, (SurfaceType)material.GetFloat("_SurfaceType"), (BlendMode)material.GetFloat("_BlendMode"), (DoubleSidedMode)material.GetFloat("_DoubleSidedMode"));
SetMaterialKeywords(material);
SetupMaterial(material);
}
static void SetKeyword(Material m, string keyword, bool state)

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


[Enum(None, 0, DoubleSided, 1, DoubleSidedLigthingFlip, 2, DoubleSidedLigthingMirror, 3)] _DoubleSidedMode("Double sided mode", Float) = 1
}
CGINCLUDE
HLSLINCLUDE
#pragma shader_feature _ALPHATEST_ON
#pragma shader_feature _DOUBLESIDED_LIGHTING_FLIP _DOUBLESIDED_LIGHTING_MIRROR

#pragma shader_feature _HEIGHTMAP
#pragma shader_feature _HEIGHTMAP_AS_DISPLACEMENT
ENDCG
ENDHLSL
SubShader
{

ZWrite [_ZWrite]
Cull [_CullMode]
CGPROGRAM
HLSLPROGRAM
#pragma target 5.0
#pragma only_renderers d3d11 // TEMP: unitl we go futher in dev

return float4(diffuseLighting.rgb + specularLighting.rgb, 1.0);
}
ENDCG
ENDHLSL
}
// ------------------------------------------------------------------

Cull[_CullMode]
CGPROGRAM
HLSLPROGRAM
#pragma target 5.0
#pragma only_renderers d3d11 // TEMP: unitl we go futher in dev

ENCODE_INTO_GBUFFER(surfaceData, outGBuffer);
}
ENDCG
ENDHLSL
}
}

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


ZWrite Off
Blend[_SrcBlend][_DstBlend]
CGPROGRAM
HLSLPROGRAM
#pragma target 5.0
#pragma only_renderers d3d11 // TEMP: unitl we go futher in dev

return float4(diffuseLighting.rgb + specularLighting.rgb, 1.0);
}
ENDCG
ENDHLSL
}
}

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


Pass {
ZTest Always Cull Off ZWrite Off
CGPROGRAM
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment Frag
#pragma target 5.0

}
ENDCG
ENDHLSL
}
}

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


// UNITY_SHADER_NO_UPGRADE
#ifndef UNITY_SHADER_VARIABLES_INCLUDED
#define UNITY_SHADER_VARIABLES_INCLUDED
// CAUTION:
// Currently the shaders compiler always include regualr Unity shaderVariables, so I get a conflict here were UNITY_SHADER_VARIABLES_INCLUDED is already define, this need to be fixed.
// As I haven't change the variables name yet, I simply don't define anything, and I put the transform function at the end of the file outside the guard header.

#ifndef UNITY_SHADER_VARIABLES_INCLUDED
#define UNITY_SHADER_VARIABLES_INCLUDED
#define UNITY_MATRIX_M unity_ObjectToWorld
// These are updated per eye in VR

float4x4 unity_MatrixVP;
#endif
fixed4 glstate_lightmodel_ambient;
fixed4 unity_AmbientSky;
fixed4 unity_AmbientEquator;
fixed4 unity_AmbientGround;
fixed4 unity_IndirectSpecColor;
float4 glstate_lightmodel_ambient;
float4 unity_AmbientSky;
float4 unity_AmbientEquator;
float4 unity_AmbientGround;
float4 unity_IndirectSpecColor;
#endif // UNITY_SHADER_VARIABLES_INCLUDED
float4x4 GetObjectToWorldMatrix()
{

float4x4 GetClipToHWorldMatrix()
{
return glstate_matrix_inv_projection;
}
float GetOdddNegativeScale()
{
return unity_WorldTransformParams.w;
}

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;
float sign = tangentSign * GetOdddNegativeScale();
float3 binormal = cross(normal, tangent) * sign;
return float3x3(tangent, binormal, normal);

{
return normalize(_WorldSpaceCameraPos.xyz - positionWS);
}
#endif // UNITY_SHADER_VARIABLES_INCLUDED

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


float3 positionWS;
float2 texCoord0;
float4 tangentToWorld[3]; // [3x3:tangentToWorld | 1x3:viewDirForParallax]
#if defined(_DOUBLESIDED_LIGHTING_FLIP) || defined(_DOUBLESIDED_LIGHTING_MIRROR)
float cullFace;
#endif
};
struct PackedVaryings

#if defined(_DOUBLESIDED_LIGHTING_FLIP) || defined(_DOUBLESIDED_LIGHTING_MIRROR)
float cullFace : FACE;
#endif
};
// Function to pack data to use as few interpolator as possible, the ShaderGraph should generate these functions

output.tangentToWorld[0] = input.interpolators[1];
output.tangentToWorld[1] = input.interpolators[2];
output.tangentToWorld[2] = input.interpolators[3];
#if defined(_DOUBLESIDED_LIGHTING_FLIP) || defined(_DOUBLESIDED_LIGHTING_MIRROR)
output.cullFace = input.cullFace;
#endif
return output;
}

#endif
// TODO: think about using BC5
float3 vertexNormalWS = input.tangentToWorld[2].xyz;
#ifdef _NORMALMAP
#ifdef _NORMALMAP_TANGENT_SPACE
float3 normalTS = UnpackNormalDXT5nm(tex2D(_NormalMap, input.texCoord0));

#endif
#else
data.normalWS = normalize(input.tangentToWorld[2].xyz);
data.normalWS = vertexNormalWS;
#endif
/*
#if defined(_DOUBLESIDED_LIGHTING_FLIP) || defined(_DOUBLESIDED_LIGHTING_MIRROR)
#if defined(_DOUBLESIDED_LIGHTING_FLIP)
float oppositeNormalWS = -data.normalWS;
#else
// Mirror the normal with the plane define by vertex normal
float oppositeNormalWS = reflect(data.normalWS, vertexNormalWS);
#endif
// TODO : Test if GetOdddNegativeScale() is necessary here in case of normal map, as GetOdddNegativeScale is take into account in CreateTangentToWorld();
data.normalWS = (input.cullFace > 0.0 ? GetOdddNegativeScale() : -GetOdddNegativeScale()) >= 0.0 ? data.normalWS : oppositeNormalWS;
//data.normalWS = input.cullFace > 0.0 ? data.normalWS : data.normalWS;
*/
data.materialId = 0;

4
Assets/ScriptableRenderLoop/ShaderLibrary/API/D3D11.hlsl


#define UNITY_UV_STARTS_AT_TOP 1
#define UNITY_REVERSED_Z 1
#define UNITY_NEAR_CLIP_VALUE (1.0)
#define VFACE FACE
#define CBUFFER_START(name) cbuffer name {
#define CBUFFER_END };

9
Assets/ScriptableRenderLoop/ShaderLibrary/API/Validate.hlsl


// Upgrade NOTE: replaced 'defined in' with 'defined (in)'
// Wait for a fix from Trunk
// Wait for a fix from Trunk #error not supported yet
/*
#define REQUIRE_DEFINED(X_) \
#ifndef X_ \

REQUIRE_DEFINED(UNITY_UV_STARTS_AT_TOP)
REQUIRE_DEFINED(UNITY_REVERSED_Z)
REQUIRE_DEFINED(UNITY_NEAR_CLIP_VALUE)
REQUIRE_DEFINED(FACE)
REQUIRE_DEFINED(CBUFFER_START)
REQUIRE_DEFINED(CBUFFER_END)
*/

2
Assets/ScriptableRenderLoop/ShaderLibrary/CommonLighting.hlsl


#ifndef UNITY_COMMON_LIGHTING_INCLUDED
#define UNITY_COMMON_LIGHTING_INCLUDED
#include "Common.hlsl"
//-----------------------------------------------------------------------------
// Attenuation functions
//-----------------------------------------------------------------------------

正在加载...
取消
保存