Skip to content

Commit

Permalink
Added groups
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-beqiraj committed Feb 5, 2025
1 parent 9474f9b commit 2860d5b
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 4 deletions.
5 changes: 3 additions & 2 deletions Engine/Shaders/HelperHeaderPipeline/light_func.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ struct AreaData
{
vec4 position[4];
vec4 diffuse;
vec4 padding;
sampler2D depthMap;
vec2 padding;
};


Expand All @@ -39,7 +40,7 @@ layout(std430, binding = 3) buffer Omni
OmniData omniData[];
};

layout(std430, binding = 18) buffer Area
layout(std430, binding = 31) buffer Area
{
ivec4 lenArea;
AreaData areaData[];
Expand Down
35 changes: 35 additions & 0 deletions Engine/Shaders/PbrHeaderPipeline/pbr_calculation.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,44 @@ vec3 pbrCalculation(vec3 FragPos, vec3 N, vec3 albedo, vec4 aoSpecular,float rou
}
}

float dotNV = clamp(dot(N, V), 0.0f, 1.0f);

// use roughness and sqrt(1-cos_theta) to sample M_texture
vec2 uv = vec2(roughness, sqrt(1.0f - dotNV));
uv = uv*LUT_SCALE + LUT_BIAS;

// get 4 parameters for inverse_M
vec4 t1 = texture(textureM, uv);

// Get 2 parameters for Fresnel calculation
vec4 t2 = texture(textureLut, uv);

mat3 Minv = mat3(
vec3(t1.x, 0, t1.y),
vec3( 0, 1, 0),
vec3(t1.z, 0, t1.w)
);
vec3 mSpecular = ToLinear(vec3(0.23f, 0.23f, 0.23f)); // mDiffuse
for (int i = 0; i < lenArea.r; i++) {
// Evaluate LTC shading

vec3 position[4];
position[0]=vec3(areaData[i].position[0]);
position[1]=vec3(areaData[i].position[1]);
position[2]=vec3(areaData[i].position[2]);
position[3]=vec3(areaData[i].position[3]);


vec3 diffuse = LTC_Evaluate(N, V, FragPos, mat3(1), position, false);
vec3 specular = LTC_Evaluate(N, V, FragPos, Minv, position, false);

// GGX BRDF shadowing and Fresnel
// t2.x: shadowedF90 (F90 normally it should be 1.0)
// t2.y: Smith function for Geometric Attenuation Term, it is dot(V or L, H).
specular *= mSpecular*t2.x + (1.0f - mSpecular) * t2.y;

// Add contribution
Lo += vec3(areaData[i].diffuse) * (specular + albedo * diffuse);
}

// Locating which cluster this fragment is part of
Expand Down
95 changes: 95 additions & 0 deletions Engine/Shaders/PbrHeaderPipeline/pbr_func.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ layout(std140, binding = 3) uniform FragmentData
};


const float LUT_SIZE = 64.0; // ltc_texture size
const float LUT_SCALE = (LUT_SIZE - 1.0)/LUT_SIZE;
const float LUT_BIAS = 0.5/LUT_SIZE;


MaterialData currentMaterial;

#if defined(ANIMATE)
Expand All @@ -37,6 +42,96 @@ layout(std430, binding = 0) readonly buffer Material
};
#endif

// Vector form without project to the plane (dot with the normal)
// Use for proxy sphere clipping
vec3 IntegrateEdgeVec(vec3 v1, vec3 v2)
{
// Using built-in acos() function will result flaws
// Using fitting result for calculating acos()
float x = dot(v1, v2);
float y = abs(x);

float a = 0.8543985 + (0.4965155 + 0.0145206*y)*y;
float b = 3.4175940 + (4.1616724 + y)*y;
float v = a / b;

float theta_sintheta = (x > 0.0) ? v : 0.5*inversesqrt(max(1.0 - x*x, 1e-7)) - v;

return cross(v1, v2)*theta_sintheta;
}


// P is fragPos in world space (LTC distribution)
vec3 LTC_Evaluate(vec3 N, vec3 V, vec3 P, mat3 Minv, vec3 points[4], bool twoSided)
{
// construct orthonormal basis around N
vec3 T1, T2;
T1 = normalize(V - N * dot(V, N));
T2 = cross(N, T1);

// rotate area light in (T1, T2, N) basis
Minv = Minv * transpose(mat3(T1, T2, N));
//Minv = Minv * transpose(mat3(N, T2, T1));

// polygon (allocate 4 vertices for clipping)
vec3 L[4];
// transform polygon from LTC back to origin Do (cosine weighted)
L[0] = Minv * (points[0] - P);
L[1] = Minv * (points[1] - P);
L[2] = Minv * (points[2] - P);
L[3] = Minv * (points[3] - P);

// use tabulated horizon-clipped sphere
// check if the shading point is behind the light
vec3 dir = points[0] - P; // LTC space
vec3 lightNormal = cross(points[1] - points[0], points[3] - points[0]);
bool behind = (dot(dir, lightNormal) < 0.0);

// cos weighted space
L[0] = normalize(L[0]);
L[1] = normalize(L[1]);
L[2] = normalize(L[2]);
L[3] = normalize(L[3]);

// integrate
vec3 vsum = vec3(0.0);
vsum += IntegrateEdgeVec(L[0], L[1]);
vsum += IntegrateEdgeVec(L[1], L[2]);
vsum += IntegrateEdgeVec(L[2], L[3]);
vsum += IntegrateEdgeVec(L[3], L[0]);

// form factor of the polygon in direction vsum
float len = length(vsum);

float z = vsum.z/len;
if (behind)
z = -z;

vec2 uv = vec2(z*0.5f + 0.5f, len); // range [0, 1]
uv = uv*LUT_SCALE + LUT_BIAS;

// Fetch the form factor for horizon clipping
float scale = texture(textureLut, uv).w;

float sum = len*scale;
if (!behind && !twoSided)
sum = 0.0;

// Outgoing radiance (solid angle) for the entire polygon
vec3 Lo_i = vec3(sum, sum, sum);
return Lo_i;
}

// PBR-maps for roughness (and metallic) are usually stored in non-linear
// color space (sRGB), so we use these functions to convert into linear RGB.
vec3 PowVec3(vec3 v, float p)
{
return vec3(pow(v.x, p), pow(v.y, p), pow(v.z, p));
}

const float gamma = 2.2;
vec3 ToLinear(vec3 v) { return PowVec3(v, gamma); }

float DistributionGGX(vec3 N, vec3 H, float roughness)
{
float a = roughness * roughness;
Expand Down
3 changes: 1 addition & 2 deletions Engine/src/Handlers/LightHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Prisma::LightHandler::LightHandler()
m_dirCSM = std::make_shared<SSBO>(9);
m_dirCSM->resize(16 * sizeof(float) + sizeof(glm::vec4));

m_areaLights = std::make_shared<SSBO>(18);
m_areaLights = std::make_shared<SSBO>(31);
m_areaLights->resize(MAX_AREA_LIGHTS * sizeof(LightType::LightArea) + sizeof(glm::vec4));

glm::vec3 size = ClusterCalculation::grids();
Expand Down Expand Up @@ -106,7 +106,6 @@ void Prisma::LightHandler::updateArea()
areaLength.r = numVisible;
m_areaLights->modifyData(0, sizeof(glm::vec4),
value_ptr(areaLength));

m_areaLights->modifyData(sizeof(glm::vec4), numVisible * sizeof(LightType::LightArea),
m_dataArea->lights.data());
}
Expand Down

0 comments on commit 2860d5b

Please sign in to comment.