Optimizing GGX Shaders with dot(L,H) by John Hable

Memo of optimized GGX Shaders

/*
optimized-ggx.hlsl
This file describes several optimizations you can make when creating a GGX shader.

AUTHOR: John Hable

LICENSE:

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>
*/

//------------------------------------------------------------
// LightingFuncGGX_REF()
//   D : GGX Distribution
//   F : Schlick-Fresnel
//   V : Schlick-GGX ( Schlick approximation of Smith solved with GGX )
//   (JP: 標準的な物理ベースなシェーダ)

float G1V(float dotNV, float k)
{
	return 1.0f/(dotNV*(1.0f-k)+k);
}

float LightingFuncGGX_REF(float3 N, float3 V, float3 L, float roughness, float F0)
{
	float alpha = roughness*roughness;

	float3 H = normalize(V+L);

	float dotNL = saturate(dot(N,L));
	float dotNV = saturate(dot(N,V));
	float dotNH = saturate(dot(N,H));
	float dotLH = saturate(dot(L,H));

	float F, D, vis;

	// D
	float alphaSqr = alpha*alpha;
	float pi = 3.14159f;
	float denom = dotNH * dotNH *(alphaSqr-1.0) + 1.0f;
	D = alphaSqr/(pi * denom * denom);

	// F
	float dotLH5 = pow(1.0f-dotLH,5);
	F = F0 + (1.0-F0)*(dotLH5);

	// V
	float k = alpha/2.0f;
	vis = G1V(dotNL,k)*G1V(dotNV,k);

	float specular = dotNL * D * F * vis;
	return specular;
}

//------------------------------------------------------------
// LightingFuncGGX_OPT1()
//   V : dot_NL and dot_NV are replaced with dot_LH
//   (JP : dot_NL と dot_NV を, dot_LH で置換)

float LightingFuncGGX_OPT1(float3 N, float3 V, float3 L, float roughness, float F0)
{
        ...

	// V
	float k = alpha/2.0f;
	vis = G1V(dotLH,k)*G1V(dotLH,k);

	float specular = dotNL * D * F * vis;
	return specular;
}

//------------------------------------------------------------
// LightingFuncGGX_OPT2
//   V : Use a function that goes to 1 when at direct angles, and goes to 1/k^2 at grazing angles
//   (JP: 直接的な角度のときに 1.0 になって, かすめる角度のときの 1/k^2 になる関数で置き換える )

float LightingFuncGGX_OPT2(float3 N, float3 V, float3 L, float roughness, float F0)
{
        ...

	// V
	float k = alpha/2.0f;
	float k2 = k*k;
	float invK2 = 1.0f-k2;
	vis = rcp(dotLH*dotLH*invK2 + k2);

	float specular = dotNL * D * F * vis;
	return specular;
}

//------------------------------------------------------------
// LightingFuncGGX_OPT3()
//   Refactored shader before using "Visibility LUT"
//   "Visibility LUT" input  : dot(L,H) and roughness, output : V
//   (JP: Visibility LUT を使う前にリファクタリングしたもの )

float2 LightingFuncGGX_FV(float dotLH, float roughness)
{
	float alpha = roughness*roughness;

	// F
	float F_a, F_b;
	float dotLH5 = pow(1.0f-dotLH,5);
	F_a = 1.0f;
	F_b = dotLH5;

	// V
	float vis;
	float k = alpha/2.0f;
	float k2 = k*k;
	float invK2 = 1.0f-k2;
	vis = rcp(dotLH*dotLH*invK2 + k2);

	return float2(F_a*vis,F_b*vis);
}

float LightingFuncGGX_OPT3(float3 N, float3 V, float3 L, float roughness, float F0)
{
	float3 H = normalize(V+L);

	float dotNL = saturate(dot(N,L));
	float dotLH = saturate(dot(L,H));
	float dotNH = saturate(dot(N,H));

	float D = ...;
	float2 FV_helper = LightingFuncGGX_FV(dotLH,roughness);
	float FV = F0*FV_helper.x + (1.0f-F0)*FV_helper.y;
	float specular = dotNL * D * FV;

	return specular;
}

//------------------------------------------------------------
// LightingFuncGGX_OPT4()
//  D, V are using LUT
//   D : LUT    input dot_NH^4, roughness, output D
//   V : "Visibility LUT" input  : dot(L,H) and roughness, output V
// (JP : D,V で LUT を使用 )

float Pow4(float x)
{
	return x*x*x*x;
}

float LightingFuncGGX_OPT4(float3 N, float3 V, float3 L, float roughness, float F0)
{
	float3 H = normalize(V+L);

	float dotNL = saturate(dot(N,L));
	float dotLH = saturate(dot(L,H));
	float dotNH = saturate(dot(N,H));

	float D = 
            g_txGgxDFV.Sample(g_samLinearClamp,float2(Pow4(dotNH),roughness)).x;
	float2 FV_helper =
	    g_txGgxDFV.Sample(g_samLinearClamp,float2(dotLH,roughness)).yz;

	float FV = F0*FV_helper.x + (1.0f-F0)*FV_helper.y;
	float specular = dotNL * D * FV;

	return specular;
}