UE4 IBL glsl

Started by
2 comments, last by lipsryme 9 years, 8 months ago

************** UPDATE **********************

I had a stupid issue due to the resolution of the texture, not the code below works as it should.

***********************************************

Hi there!

I'm trying to implement the IBL technique described in here: http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf

Now I'm still trying to get the envBRDF LUT (roughness / NdotV) to render properly as shown in the paper:

TDb6Dhh.png?1

What I have now is:

the usual Hammersley functions:



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
 }
 // http://holger.dammertz.org/stuff/notes_HammersleyOnHemisphere.html
 vec2 Hammersley(uint i, uint N) {
     return vec2(float(i)/float(N), radicalInverse_VdC(i));
 }
 

The ImportanceSampleGGX practically as reported in the paper:


vec3 importanceSampleGGX(vec2 sample, float m, vec3 N){
	float phi = sample.x * 2.0f * PI;
	float cosTheta = sqrt( (1.0f - sample.y) / 1.0f + (m*m - 1.0f) * sample.y ); 
	float sinTheta = sqrt(1.0f - cosTheta*cosTheta);

	vec3 vector = vec3(
		sinTheta * cos(phi),
		sinTheta * sin(phi),
		cosTheta
		);


	return vector;
	vec3 up = abs(N.z) < 0.999 ? vec3(0.0,0.0,1.0) : vec3(1.0,0.0,0.0);
	vec3 tangentX = normalize(cross(up,N));
	vec3 tangentY = normalize(cross(N,tangentX));
	// Project 
	return tangentX * vector.x + tangentY * vector.y + N * vector.z;
}

The integrateBRDF


 vec2 IntegrateBRDF( float Roughness, float NoV ){
 vec3 V;
 V.x = sqrt( 1.0f - NoV * NoV ); // sin
 V.y = 0.0;
 V.z = NoV;
 // cos
 float A = 0.0;
 float B = 0.0;

 for( uint i = 0u; i < NUMBER_OF_SAMPLES; i++ )
 {
 vec2 Xi = Hammersley( i, NUMBER_OF_SAMPLES );
 vec3 H = ImportanceSampleGGX( Xi, Roughness, vec3(0.0,0.0,1.0) );
 vec3 L = 2.0 * dot( V, H ) * H - V;
 float NoL = max( L.z, 0.0 );
 float NoH = max( H.z, 0.0 );
 float VoH = max( dot( V, H ), 0.0 );

 	if( NoL > 0 )
 	{
	 float G = G_Smith( Roughness, NoV, NoL );
	 float G_Vis = G * VoH / (NoH * NoV);
	 float Fc = pow( 1.0 - VoH, 5.0 );
	 A += (1.0 - Fc) * G_Vis;
	 B += Fc * G_Vis;
 	}
 }
return vec2(  (A)/float(NUMBER_OF_SAMPLES), (B)/float(NUMBER_OF_SAMPLES)) ;
}

As you can see they're almost a copy-paste from the reference, but what I got is completely different:

6b9aXlD.png

The roughness is the uv.x and uv.y of a quad that are calculated in the VS as:


	vTextureCoordinate = (aVertexPosition.xy+vec2(1,1))/2.0;

Am I missin something very stupid?

Advertisement

He is using Disney's roughness which is squared so that would be m*m inside the importanceSampleGGX function.

My HLSL code:


float3 ImportanceSampleGGX(float2 Xi, float Roughness, float3 N)
{
	float a = Roughness * Roughness; // DISNEY'S ROUGHNESS [see Burley'12 siggraph]

	// Compute distribution direction
	float Phi = 2 * PI * Xi.x;
	float CosTheta = sqrt((1 - Xi.y) / (1 + (a*a - 1) * Xi.y));		
	float SinTheta = sqrt(1 - CosTheta * CosTheta);

	// Convert to spherical direction
	float3 H;
	H.x = SinTheta * cos(Phi);
	H.y = SinTheta * sin(Phi);
	H.z = CosTheta;

	float3 UpVector = abs(N.z) < 0.999 ? float3(0, 0, 1) : float3(1, 0, 0);
	float3 TangentX = normalize(cross(UpVector, N));
	float3 TangentY = cross(N, TangentX);

	// Tangent to world space
	return TangentX * H.x + TangentY * H.y + N * H.z;
}

For completeness sake here's how I do G_Smith:


// http://graphicrants.blogspot.com.au/2013/08/specular-brdf-reference.html
float GGX(float NdotV, float a)
{
	float k = a / 2;
	return NdotV / (NdotV * (1.0f - k) + k);
}

// http://graphicrants.blogspot.com.au/2013/08/specular-brdf-reference.html
float G_Smith(float a, float nDotV, float nDotL)
{
	return GGX(nDotL, a * a) * GGX(nDotV, a * a);
}

Not sure about the TexCoord code you are using but you just need to render a regular post process quad and simply pass through the TexCoords as they are and then use them as inputs to the integrateBRDF function like so:


return IntegrateBRDF(input.TexCoord.x, input.TexCoord.y);

Probably unrelated but I'm using an R16G16_FLOAT texture like Brian Karis is suggesting.

Would you guys be able to share some details on how you're filtering your IBL cubemaps?
What roughness are you using at each mip?
What format do you use?

How do you put it all together?

Thanks!

Check out my project @ www.exitearth.com

Hi REF_Cracker. Sorry for the late answer I just happened to stumble upon the thread again (hadn't been following it).

The format I'm using is R16G16B16A16_FLOAT for each cube map face.

I calculate the roughness in the shader like this:


float Roughness = (CubeLOD) / (CubeLODCount);

Which would give you values like these:

0 (256x256)

0.166667 (128x128)

0.333333 (64x64)

0.5 (32x32)

0.666667 (16x16)

0.833333 (8x8)

1 (4x4)

CubeLOD is starting at 256x256 and going down 6 levels (so 7 in total / original + 6 mip levels) to 4x4 for the roughest one.

Maybe you should use less mips and make it end at 16x16 or so haven't done much testing here.

This topic is closed to new replies.

Advertisement