Review BRDF texture generation
Actually, that function should be redesigned...
This commit is contained in:
parent
5c614f6975
commit
f4fe7f4d4c
2 changed files with 38 additions and 42 deletions
|
@ -1,12 +1,15 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* rPBR [shader] - Bidirectional reflectance distribution function fragment shader
|
||||
* BRDF LUT Generation - Bidirectional reflectance distribution function fragment shader
|
||||
*
|
||||
* REF: https://github.com/HectorMF/BRDFGenerator
|
||||
*
|
||||
* Copyright (c) 2017 Victor Fisac
|
||||
*
|
||||
**********************************************************************************************/
|
||||
|
||||
#version 330
|
||||
|
||||
#define MAX_SAMPLES 1024u
|
||||
|
||||
// Input vertex attributes (from vertex shader)
|
||||
|
@ -18,43 +21,30 @@ const float PI = 3.14159265359;
|
|||
// Output fragment color
|
||||
out vec4 finalColor;
|
||||
|
||||
float DistributionGGX(vec3 N, vec3 H, float roughness);
|
||||
float RadicalInverse_VdC(uint bits);
|
||||
vec2 Hammersley(uint i, uint N);
|
||||
vec3 ImportanceSampleGGX(vec2 Xi, vec3 N, float roughness);
|
||||
float RadicalInverseVdC(uint bits);
|
||||
float GeometrySchlickGGX(float NdotV, float roughness);
|
||||
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness);
|
||||
vec3 ImportanceSampleGGX(vec2 Xi, vec3 N, float roughness);
|
||||
vec2 IntegrateBRDF(float NdotV, float roughness);
|
||||
|
||||
float DistributionGGX(vec3 N, vec3 H, float roughness)
|
||||
float RadicalInverseVdC(uint bits)
|
||||
{
|
||||
float a = roughness*roughness;
|
||||
float a2 = a*a;
|
||||
float NdotH = max(dot(N, H), 0.0);
|
||||
float NdotH2 = NdotH*NdotH;
|
||||
|
||||
float nom = a2;
|
||||
float denom = (NdotH2*(a2 - 1.0) + 1.0);
|
||||
denom = PI*denom*denom;
|
||||
|
||||
return nom/denom;
|
||||
}
|
||||
|
||||
float RadicalInverse_VdC(uint bits)
|
||||
{
|
||||
bits = (bits << 16u) | (bits >> 16u);
|
||||
bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
|
||||
bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
|
||||
bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
|
||||
bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
|
||||
return float(bits) * 2.3283064365386963e-10; // / 0x100000000
|
||||
bits = (bits << 16u) | (bits >> 16u);
|
||||
bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
|
||||
bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
|
||||
bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
|
||||
bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
|
||||
return float(bits) * 2.3283064365386963e-10; // / 0x100000000
|
||||
}
|
||||
|
||||
// Compute Hammersley coordinates
|
||||
vec2 Hammersley(uint i, uint N)
|
||||
{
|
||||
return vec2(float(i)/float(N), RadicalInverse_VdC(i));
|
||||
return vec2(float(i)/float(N), RadicalInverseVdC(i));
|
||||
}
|
||||
|
||||
// Integrate number of importance samples for (roughness and NoV)
|
||||
vec3 ImportanceSampleGGX(vec2 Xi, vec3 N, float roughness)
|
||||
{
|
||||
float a = roughness*roughness;
|
||||
|
@ -85,6 +75,7 @@ float GeometrySchlickGGX(float NdotV, float roughness)
|
|||
return nom/denom;
|
||||
}
|
||||
|
||||
// Compute the geometry term for the BRDF given roughness squared, NoV, NoL
|
||||
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
|
||||
{
|
||||
float NdotV = max(dot(N, V), 0.0);
|
||||
|
@ -97,29 +88,31 @@ float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
|
|||
|
||||
vec2 IntegrateBRDF(float NdotV, float roughness)
|
||||
{
|
||||
vec3 V = vec3(sqrt(1.0 - NdotV*NdotV), 0.0, NdotV);
|
||||
float A = 0.0;
|
||||
float B = 0.0;
|
||||
float B = 0.0;
|
||||
vec3 V = vec3(sqrt(1.0 - NdotV*NdotV), 0.0, NdotV);
|
||||
vec3 N = vec3(0.0, 0.0, 1.0);
|
||||
|
||||
for(uint i = 0u; i < MAX_SAMPLES; i++)
|
||||
for (int i = 0; i < MAX_SAMPLES; i++)
|
||||
{
|
||||
// Generate a sample vector that's biased towards the preferred alignment direction (importance sampling)
|
||||
vec2 Xi = Hammersley(i, MAX_SAMPLES);
|
||||
vec3 H = ImportanceSampleGGX(Xi, N, roughness);
|
||||
vec3 L = normalize(2.0*dot(V, H)*H - V);
|
||||
float NdotL = max(L.z, 0.0);
|
||||
float NdotH = max(H.z, 0.0);
|
||||
float VdotH = max(dot(V, H), 0.0);
|
||||
|
||||
vec2 Xi = Hammersley(i, MAX_SAMPLES); // Compute a Hammersely coordinate
|
||||
vec3 H = ImportanceSampleGGX(Xi, N, roughness); // Integrate number of importance samples for (roughness and NoV)
|
||||
vec3 L = normalize(2.0*dot(V, H)*H - V); // Compute reflection vector L
|
||||
|
||||
float NdotL = max(L.z, 0.0); // Compute normal dot light
|
||||
float NdotH = max(H.z, 0.0); // Compute normal dot half
|
||||
float VdotH = max(dot(V, H), 0.0); // Compute view dot half
|
||||
|
||||
if (NdotL > 0.0)
|
||||
{
|
||||
float G = GeometrySmith(N, V, L, roughness);
|
||||
float G_Vis = (G*VdotH)/(NdotH*NdotV);
|
||||
float Fc = pow(1.0 - VdotH, 5.0);
|
||||
float G = GeometrySmith(N, V, L, roughness); // Compute the geometry term for the BRDF given roughness squared, NoV, NoL
|
||||
float GVis = (G*VdotH)/(NdotH*NdotV); // Compute the visibility term given G, VoH, NoH, NoV, NoL
|
||||
float Fc = pow(1.0 - VdotH, 5.0); // Compute the fresnel term given VoH
|
||||
|
||||
A += (1.0 - Fc)*G_Vis;
|
||||
B += Fc*G_Vis;
|
||||
A += (1.0 - Fc)*GVis; // Sum the result given fresnel, geometry, visibility
|
||||
B += Fc*GVis;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3221,6 +3221,7 @@ Texture2D GenTexturePrefilter(Shader shader, Texture2D cubemap, int size)
|
|||
|
||||
// Generate BRDF texture using cubemap data
|
||||
// NOTE: OpenGL ES 2.0 does not support GL_RGB16F texture format, neither GL_DEPTH_COMPONENT24
|
||||
// TODO: Review implementation: https://github.com/HectorMF/BRDFGenerator
|
||||
Texture2D GenTextureBRDF(Shader shader, int size)
|
||||
{
|
||||
Texture2D brdf = { 0 };
|
||||
|
@ -3229,9 +3230,9 @@ Texture2D GenTextureBRDF(Shader shader, int size)
|
|||
glGenTextures(1, &brdf.id);
|
||||
glBindTexture(GL_TEXTURE_2D, brdf.id);
|
||||
#if defined(GRAPHICS_API_OPENGL_33)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, size, size, 0, GL_RG, GL_FLOAT, NULL);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, size, size, 0, GL_RGB, GL_FLOAT, NULL);
|
||||
#elif defined(GRAPHICS_API_OPENGL_ES2)
|
||||
if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_RG, size, size, 0, GL_RG, GL_FLOAT, NULL);
|
||||
if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, size, size, 0, GL_RGB, GL_FLOAT, NULL);
|
||||
#endif
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
|
@ -3269,6 +3270,8 @@ Texture2D GenTextureBRDF(Shader shader, int size)
|
|||
|
||||
brdf.width = size;
|
||||
brdf.height = size;
|
||||
brdf.mipmaps = 1;
|
||||
brdf.format = UNCOMPRESSED_R32G32B32;
|
||||
#endif
|
||||
return brdf;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue