Skip to content

Commit

Permalink
Refactor accelerated baker code to map more closely with the original…
Browse files Browse the repository at this point in the history
… baker
  • Loading branch information
riccardobl authored Sep 2, 2023
1 parent ce1b249 commit 4d75d0d
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 57 deletions.
21 changes: 14 additions & 7 deletions jme3-core/src/main/resources/Common/IBL/IBLKernels.frag
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* https://learnopengl.com/PBR/IBL/Specular-IBL
* - Riccardo Balbo
*/
#import "Common/IBL/Math.glsllib"
#import "Common/ShaderLib/GLSLCompat.glsllib"
#import "Common/IBL/Math.glsl"

out vec4 outFragColor;
in vec2 TexCoords;
in vec3 LocalPos;

Expand All @@ -17,6 +17,7 @@ uniform int m_FaceId;
void brdfKernel(){
float NdotV=TexCoords.x;
float m_Roughness=TexCoords.y;

vec3 V;
V.x = sqrt(1.0 - NdotV*NdotV);
V.y = 0.0;
Expand All @@ -26,8 +27,8 @@ void brdfKernel(){
vec3 N = vec3(0.0, 0.0, 1.0);
const uint SAMPLE_COUNT = 1024u;
for(uint i = 0u; i < SAMPLE_COUNT; i++){
vec2 Xi = Hammersley(i, SAMPLE_COUNT);
vec3 H = ImportanceSampleGGX(Xi, N, m_Roughness);
vec4 Xi = Hammersley(i, SAMPLE_COUNT);
vec3 H = ImportanceSampleGGX(Xi, m_Roughness, N);
vec3 L = normalize(2.0 * dot(V, H) * H - V);
float NdotL = max(L.z, 0.0);
float NdotH = max(H.z, 0.0);
Expand Down Expand Up @@ -73,13 +74,19 @@ void prefilteredEnvKernel(){
vec3 N = normalize(LocalPos);
vec3 R = N;
vec3 V = R;

// float a2 = m_Roughness;
float a2 = m_Roughness * m_Roughness; // jme impl, why?
a2 *= a2;

const uint SAMPLE_COUNT = 1024u;
float totalWeight = 0.0;
vec3 prefilteredColor = vec3(0.0);
for(uint i = 0u; i < SAMPLE_COUNT; ++i) {
vec2 Xi = Hammersley(i, SAMPLE_COUNT);
vec3 H = ImportanceSampleGGX(Xi, N, m_Roughness);
vec3 L = normalize(2.0 * dot(V, H) * H - V);
vec4 Xi = Hammersley(i, SAMPLE_COUNT);
vec3 H = ImportanceSampleGGX(Xi, a2, N);
float VoH = dot(V,H);
vec3 L = normalize(2.0 * VoH * H - V);
float NdotL = max(dot(N, L), 0.0);
if(NdotL > 0.0) {
// TODO: use mipmap
Expand Down
6 changes: 4 additions & 2 deletions jme3-core/src/main/resources/Common/IBL/IBLKernels.j3md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
MaterialDef IBLKernels {

MaterialParameters {
Int BoundDrawBuffer
TextureCubeMap EnvMap -LINEAR
Float Roughness
Int FaceId : 0
Expand All @@ -11,8 +12,8 @@ MaterialDef IBLKernels {

Technique {

VertexShader GLSL150: Common/IBL/IBLKernels.vert
FragmentShader GLSL150: Common/IBL/IBLKernels.frag
VertexShader GLSL300 GLSL150 : Common/IBL/IBLKernels.vert
FragmentShader GLSL300 GLSL150 : Common/IBL/IBLKernels.frag

WorldParameters {
WorldMatrix
Expand All @@ -28,6 +29,7 @@ MaterialDef IBLKernels {
}

Defines {
BOUND_DRAW_BUFFER: BoundDrawBuffer
BRDF:UseBRDF
IRRADIANCE: UseIrradiance
SIBL: UseSpecularIBL
Expand Down
2 changes: 2 additions & 0 deletions jme3-core/src/main/resources/Common/IBL/IBLKernels.vert
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#import "Common/ShaderLib/GLSLCompat.glsllib"

/**
* This code is based on the following articles:
* https://learnopengl.com/PBR/IBL/Diffuse-irradiance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,51 @@ float RadicalInverse_VdC(uint bits) {
return float(bits) * 2.3283064365386963e-10; // / 0x100000000
}

vec2 Hammersley(uint i, uint N){
return vec2(float(i)/float(N), RadicalInverse_VdC(i));
vec4 Hammersley(uint i, uint N){
vec4 store=vec4(0);
store.x = float(i) / float(N);
store.y = RadicalInverse_VdC(i);

float phi = 2.0 * PI *store.x;
store.z = cos(phi);
store.w = sin(phi);

return store;
}

/*
Compatible with GL ES 2
float VanDerCorput(uint n, uint base){
float invBase = 1.0 / float(base);
float denom = 1.0;
float result = 0.0;
// float VanDerCorput(uint n, uint base){
// float invBase = 1.0 / float(base);
// float denom = 1.0;
// float result = 0.0;

for(uint i = 0u; i < 32u; ++i)
{
if(n > 0u)
{
denom = mod(float(n), 2.0);
result += denom * invBase;
invBase = invBase / 2.0;
n = uint(float(n) / 2.0);
}
}
// for(uint i = 0u; i < 32u; ++i)
// {
// if(n > 0u)
// {
// denom = mod(float(n), 2.0);
// result += denom * invBase;
// invBase = invBase / 2.0;
// n = uint(float(n) / 2.0);
// }
// }

return result;
}
// return result;
// }

vec2 Hammersley(uint i, uint N){
return vec2(float(i)/float(N), VanDerCorput(i, 2u));
}
*/
// vec2 Hammersley(uint i, uint N){
// return vec2(float(i)/float(N), VanDerCorput(i, 2u));
// }


vec3 ImportanceSampleGGX(vec2 Xi, vec3 N, float roughness){
float a = roughness*roughness;
vec3 ImportanceSampleGGX(vec4 Xi, float a2, vec3 N){

float phi = 2.0 * PI * Xi.x;
float cosTheta = sqrt((1.0 - Xi.y) / (1.0 + (a*a - 1.0) * Xi.y));
float cosTheta = sqrt((1.0 - Xi.y) / (1.0 + (a2 - 1.0) * Xi.y));
float sinTheta = sqrt(1.0 - cosTheta*cosTheta);

// from spherical coordinates to cartesian coordinates
vec3 H;
H.x = cos(phi) * sinTheta;
H.y = sin(phi) * sinTheta;
H.x = Xi.z * sinTheta;
H.y = Xi.w * sinTheta;
H.z = cosTheta;

// from tangent-space vector to world-space sample vector
Expand Down
32 changes: 16 additions & 16 deletions jme3-core/src/main/resources/Common/IBLSphH/IBLSphH.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
* - Riccardo Balbo
*/
#import "Common/IBL/Math.glsllib"
#import "Common/ShaderLib/GLSLCompat.glsllib"
#import "Common/IBL/Math.glsl"

// #define NUM_SH_COEFFICIENT 9
#ifndef PI
#define PI 3.1415926535897932384626433832795
#endif

out vec4 outFragColor;
in vec2 TexCoords;
in vec3 LocalPos;

Expand All @@ -22,9 +22,9 @@ uniform vec2 m_Resolution;
uniform int m_FaceId;

const float sqrtPi = sqrt(PI);
const float sqrt3Pi = sqrt(3 / PI);
const float sqrt5Pi = sqrt(5 / PI);
const float sqrt15Pi = sqrt(15 / PI);
const float sqrt3Pi = sqrt(3. / PI);
const float sqrt5Pi = sqrt(5. / PI);
const float sqrt15Pi = sqrt(15. / PI);

#ifdef REMAP_MAX_VALUE
uniform float m_RemapMaxValue;
Expand All @@ -42,31 +42,31 @@ vec3 getVectorFromCubemapFaceTexCoord(float x, float y, float mapSize, int face)


// Warp texel centers in the proximity of the edges.
float a = pow(mapSize, 2.0) / pow(mapSize - 1, 3.0);
float a = pow(mapSize, 2.0) / pow(mapSize - 1., 3.0);

u = a * pow(u, 3) + u;
v = a * pow(v, 3) + v;
u = a * pow(u, 3.) + u;
v = a * pow(v, 3.) + v;
//compute vector depending on the face
// Code from Nvtt : https://github.com/castano/nvidia-texture-tools/blob/master/src/nvtt/CubeSurface.cpp#L101
vec3 o =vec3(0);
switch(face) {
case 0:
o= normalize(vec3(1, -v, -u));
o= normalize(vec3(1., -v, -u));
break;
case 1:
o= normalize(vec3(-1, -v, u));
o= normalize(vec3(-1., -v, u));
break;
case 2:
o= normalize(vec3(u, 1, v));
o= normalize(vec3(u, 1., v));
break;
case 3:
o= normalize(vec3(u, -1, -v));
o= normalize(vec3(u, -1., -v));
break;
case 4:
o= normalize(vec3(u, -v, 1));
o= normalize(vec3(u, -v, 1.));
break;
case 5:
o= normalize(vec3(-u, -v, -1.0));
o= normalize(vec3(-u, -v, -1.));
break;
}

Expand Down Expand Up @@ -148,8 +148,8 @@ void sphKernel() {
int width = int(m_Resolution.x);
int height = int(m_Resolution.y);
vec3 texelVect=vec3(0);
float shDir=0;
float weight=0;
float shDir=0.;
float weight=0.;
vec4 color=vec4(0);

int i=int(gl_FragCoord.x);
Expand Down
6 changes: 4 additions & 2 deletions jme3-core/src/main/resources/Common/IBLSphH/IBLSphH.j3md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
MaterialDef IBLSphH {

MaterialParameters {
Int BoundDrawBuffer
TextureCubeMap Texture -LINEAR
Int FaceId : 0
Texture2D ShCoef -LINEAR
Expand All @@ -10,8 +11,8 @@ MaterialDef IBLSphH {

Technique {

VertexShader GLSL150: Common/IBLSphH/IBLSphH.vert
FragmentShader GLSL150: Common/IBLSphH/IBLSphH.frag
VertexShader GLSL300 GLSL150 : Common/IBLSphH/IBLSphH.vert
FragmentShader GLSL300 GLSL150 : Common/IBLSphH/IBLSphH.frag

WorldParameters {
}
Expand All @@ -24,6 +25,7 @@ MaterialDef IBLSphH {
}

Defines {
BOUND_DRAW_BUFFER: BoundDrawBuffer
REMAP_MAX_VALUE: RemapMaxValue
SH_COEF: ShCoef
}
Expand Down
2 changes: 2 additions & 0 deletions jme3-core/src/main/resources/Common/IBLSphH/IBLSphH.vert
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#import "Common/ShaderLib/GLSLCompat.glsllib"

/**
*- Riccardo Balbo
*/
Expand Down

0 comments on commit 4d75d0d

Please sign in to comment.