Move Cook-Torrance functions to a separate module
This commit is contained in:
parent
b2fad27618
commit
bcdb0f98f0
|
|
@ -41,6 +41,7 @@ namespace Nz
|
|||
#include <Nazara/Graphics/Resources/Shaders/PhysicallyBasedMaterial.nzslb.h>
|
||||
};
|
||||
|
||||
// Modules
|
||||
const UInt8 r_instanceDataModule[] = {
|
||||
#include <Nazara/Graphics/Resources/Shaders/Modules/Engine/InstanceData.nzslb.h>
|
||||
};
|
||||
|
|
@ -52,6 +53,14 @@ namespace Nz
|
|||
const UInt8 r_viewerDataModule[] = {
|
||||
#include <Nazara/Graphics/Resources/Shaders/Modules/Engine/ViewerData.nzslb.h>
|
||||
};
|
||||
|
||||
const UInt8 r_mathConstantsModule[] = {
|
||||
#include <Nazara/Graphics/Resources/Shaders/Modules/Math/Constants.nzslb.h>
|
||||
};
|
||||
|
||||
const UInt8 r_mathCookTorrancePBRModule[] = {
|
||||
#include <Nazara/Graphics/Resources/Shaders/Modules/Math/CookTorrancePBR.nzslb.h>
|
||||
};
|
||||
}
|
||||
|
||||
/*!
|
||||
|
|
@ -218,11 +227,13 @@ namespace Nz
|
|||
RegisterEmbedShaderModule(r_basicMaterialShader);
|
||||
RegisterEmbedShaderModule(r_depthMaterialShader);
|
||||
RegisterEmbedShaderModule(r_fullscreenVertexShader);
|
||||
RegisterEmbedShaderModule(r_instanceDataModule);
|
||||
RegisterEmbedShaderModule(r_lightDataModule);
|
||||
RegisterEmbedShaderModule(r_mathConstantsModule);
|
||||
RegisterEmbedShaderModule(r_mathCookTorrancePBRModule);
|
||||
RegisterEmbedShaderModule(r_phongMaterialShader);
|
||||
RegisterEmbedShaderModule(r_physicallyBasedMaterialShader);
|
||||
RegisterEmbedShaderModule(r_textureBlitShader);
|
||||
RegisterEmbedShaderModule(r_instanceDataModule);
|
||||
RegisterEmbedShaderModule(r_lightDataModule);
|
||||
RegisterEmbedShaderModule(r_viewerDataModule);
|
||||
|
||||
#ifdef NAZARA_DEBUG
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
[nzsl_version("1.0")]
|
||||
module Engine.Constants;
|
||||
module Math.Constants;
|
||||
|
||||
[export]
|
||||
const Pi: f32 = 3.1415926535897932384626433832795;
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
[nzsl_version("1.0")]
|
||||
module Math.CookTorrancePBR;
|
||||
|
||||
import Pi from Math.Constants;
|
||||
|
||||
[export]
|
||||
fn DistributionGGX(N: vec3[f32], H: vec3[f32], roughness: f32) -> f32
|
||||
{
|
||||
let a = roughness * roughness;
|
||||
let a2 = a * a;
|
||||
|
||||
let NdotH = max(dot(N, H), 0.0);
|
||||
let NdotH2 = NdotH * NdotH;
|
||||
|
||||
let num = a2;
|
||||
let denom = (NdotH2 * (a2 - 1.0) + 1.0);
|
||||
denom = Pi * denom * denom;
|
||||
|
||||
return num / denom;
|
||||
}
|
||||
|
||||
[export]
|
||||
fn GeometrySchlickGGX(NdotV: f32, roughness: f32) -> f32
|
||||
{
|
||||
let r = (roughness + 1.0);
|
||||
let k = (r * r) / 8.0;
|
||||
|
||||
let num = NdotV;
|
||||
let denom = NdotV * (1.0 - k) + k;
|
||||
|
||||
return num / denom;
|
||||
}
|
||||
|
||||
[export]
|
||||
fn GeometrySmith(N: vec3[f32], V: vec3[f32], L: vec3[f32], roughness: f32) -> f32
|
||||
{
|
||||
let NdotV = max(dot(N, V), 0.0);
|
||||
let NdotL = max(dot(N, L), 0.0);
|
||||
let ggx2 = GeometrySchlickGGX(NdotV, roughness);
|
||||
let ggx1 = GeometrySchlickGGX(NdotL, roughness);
|
||||
|
||||
return ggx1 * ggx2;
|
||||
}
|
||||
|
||||
[export]
|
||||
fn FresnelSchlick(cosTheta: f32, F0: vec3[f32]) -> vec3[f32]
|
||||
{
|
||||
// TODO: Clamp
|
||||
return F0 + (vec3[f32](1.0, 1.0, 1.0) - F0) * pow(min(max(1.0 - cosTheta, 0.0), 1.0), 5.0);
|
||||
}
|
||||
|
|
@ -84,49 +84,8 @@ struct VertToFrag
|
|||
}
|
||||
|
||||
// Fragment stage
|
||||
import Pi from Engine.Constants;
|
||||
|
||||
fn DistributionGGX(N: vec3[f32], H: vec3[f32], roughness: f32) -> f32
|
||||
{
|
||||
let a = roughness * roughness;
|
||||
let a2 = a * a;
|
||||
|
||||
let NdotH = max(dot(N, H), 0.0);
|
||||
let NdotH2 = NdotH * NdotH;
|
||||
|
||||
let num = a2;
|
||||
let denom = (NdotH2 * (a2 - 1.0) + 1.0);
|
||||
denom = Pi * denom * denom;
|
||||
|
||||
return num / denom;
|
||||
}
|
||||
|
||||
fn GeometrySchlickGGX(NdotV: f32, roughness: f32) -> f32
|
||||
{
|
||||
let r = (roughness + 1.0);
|
||||
let k = (r * r) / 8.0;
|
||||
|
||||
let num = NdotV;
|
||||
let denom = NdotV * (1.0 - k) + k;
|
||||
|
||||
return num / denom;
|
||||
}
|
||||
|
||||
fn GeometrySmith(N: vec3[f32], V: vec3[f32], L: vec3[f32], roughness: f32) -> f32
|
||||
{
|
||||
let NdotV = max(dot(N, V), 0.0);
|
||||
let NdotL = max(dot(N, L), 0.0);
|
||||
let ggx2 = GeometrySchlickGGX(NdotV, roughness);
|
||||
let ggx1 = GeometrySchlickGGX(NdotL, roughness);
|
||||
|
||||
return ggx1 * ggx2;
|
||||
}
|
||||
|
||||
fn FresnelSchlick(cosTheta: f32, F0: vec3[f32]) -> vec3[f32]
|
||||
{
|
||||
// TODO: Clamp
|
||||
return F0 + (vec3[f32](1.0, 1.0, 1.0) - F0) * pow(min(max(1.0 - cosTheta, 0.0), 1.0), 5.0);
|
||||
}
|
||||
import DistributionGGX, GeometrySmith, FresnelSchlick from Math.CookTorrancePBR;
|
||||
import Pi from Math.Constants;
|
||||
|
||||
struct FragOut
|
||||
{
|
||||
|
|
@ -233,7 +192,7 @@ fn main(input: VertToFrag) -> FragOut
|
|||
|
||||
let halfDir = normalize(lightToPosNorm + eyeVec);
|
||||
|
||||
// cook-torrance brdf
|
||||
// Cook-Torrance BRDF
|
||||
let NDF = DistributionGGX(normal, halfDir, roughness);
|
||||
let G = GeometrySmith(normal, eyeVec, lightToPosNorm, roughness);
|
||||
let F = FresnelSchlick(max(dot(halfDir, eyeVec), 0.0), F0);
|
||||
|
|
|
|||
Loading…
Reference in New Issue