Skip to content

Commit

Permalink
[Shaders] Removed the manual gamma decoding on texture read
Browse files Browse the repository at this point in the history
- As the color-purposed textures should now properly be treated as sRGB(A), it is not needed anymore

- Added a temporary gamma encoding after each shader when missing, as now everything is properly computed linearly and needs to be corrected before being displayed
  - This will later be replaced by a final render pass
  • Loading branch information
Razakhel committed May 26, 2024
1 parent db04337 commit f5ce32b
Showing 4 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion examples/deferredDemo.cpp
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@ constexpr std::string_view geomFragSource = R"(
layout(location = 1) out vec4 fragNormal;
void main() {
vec3 albedo = pow(texture(uniMaterial.baseColorMap, vertMeshInfo.vertTexcoords).rgb, vec3(2.2)) * uniMaterial.baseColor;
vec3 albedo = texture(uniMaterial.baseColorMap, vertMeshInfo.vertTexcoords).rgb * uniMaterial.baseColor;
vec3 emissive = texture(uniMaterial.emissiveMap, vertMeshInfo.vertTexcoords).rgb * uniMaterial.emissive;
float metallic = texture(uniMaterial.metallicMap, vertMeshInfo.vertTexcoords).r * uniMaterial.metallicFactor;
float roughness = texture(uniMaterial.roughnessMap, vertMeshInfo.vertTexcoords).r * uniMaterial.roughnessFactor;
10 changes: 7 additions & 3 deletions shaders/blinn-phong.frag
Original file line number Diff line number Diff line change
@@ -91,10 +91,14 @@ void main() {
specular += specFactor * pow(halfAngle, 32.0) * radiance;
}

vec3 ambient = color * 0.05;
vec3 emissive = texture(uniMaterial.emissiveMap, vertMeshInfo.vertTexcoords).rgb * uniMaterial.emissive;
vec3 ambient = color * 0.05;
vec3 emissive = texture(uniMaterial.emissiveMap, vertMeshInfo.vertTexcoords).rgb * uniMaterial.emissive;
vec3 finalColor = ambient + diffuse + specular + emissive;

fragColor = vec4(ambient + diffuse + specular + emissive, alpha);
// Gamma correction; this is temporary and will be removed later
finalColor = pow(finalColor, vec3(1.0 / 2.2));

fragColor = vec4(finalColor, alpha);
fragNormal = normal * 0.5 + 0.5;
fragSpecular = vec4(specFactor, 1.0 - max(specFactor.x, max(specFactor.y, specFactor.z)));
}
18 changes: 9 additions & 9 deletions shaders/cook-torrance.frag
Original file line number Diff line number Diff line change
@@ -96,7 +96,7 @@ void main() {
if (baseColor.a < 0.1)
discard;

vec3 albedo = pow(baseColor.rgb, vec3(2.2)) * uniMaterial.baseColor;
vec3 albedo = baseColor.rgb * uniMaterial.baseColor;
float metallic = texture(uniMaterial.metallicMap, vertMeshInfo.vertTexcoords).r * uniMaterial.metallicFactor;
float roughness = texture(uniMaterial.roughnessMap, vertMeshInfo.vertTexcoords).r * uniMaterial.roughnessFactor;
float ambOcc = texture(uniMaterial.ambientMap, vertMeshInfo.vertTexcoords).r;
@@ -151,16 +151,16 @@ void main() {
lightRadiance += (diffuse * albedoFactor + specular) * radiance * lightAngle;
}

vec3 ambient = vec3(0.03) * albedo * ambOcc;
vec3 emissive = pow(texture(uniMaterial.emissiveMap, vertMeshInfo.vertTexcoords).rgb, vec3(2.2)) * uniMaterial.emissive;
vec3 color = ambient + lightRadiance + emissive;
vec3 ambient = vec3(0.03) * albedo * ambOcc;
vec3 emissive = texture(uniMaterial.emissiveMap, vertMeshInfo.vertTexcoords).rgb * uniMaterial.emissive;
vec3 finalColor = ambient + lightRadiance + emissive;

// HDR tone mapping
color = color / (color + vec3(1.0));
// Gamma correction
color = pow(color, vec3(1.0 / 2.2));
// Reinhard tone mapping; this is temporary and will be removed later
finalColor = finalColor / (finalColor + vec3(1.0));
// Gamma correction; this is temporary and will be removed later
finalColor = pow(finalColor, vec3(1.0 / 2.2));

fragColor = vec4(color, baseColor.a);
fragColor = vec4(finalColor, baseColor.a);
fragNormal = normal * 0.5 + 0.5;
fragSpecular = vec4(baseReflectivity, roughness);
}
10 changes: 7 additions & 3 deletions shaders/lambert.frag
Original file line number Diff line number Diff line change
@@ -72,9 +72,13 @@ void main() {
lightHitAngle = max(lightHitAngle, clamp(dot(lightDir, normal), 0.0, 1.0));
}

vec3 diffuse = lightHitAngle * baseColor.rgb * uniMaterial.baseColor;
vec3 emissive = texture(uniMaterial.emissiveMap, vertMeshInfo.vertTexcoords).rgb * uniMaterial.emissive;
vec3 diffuse = lightHitAngle * baseColor.rgb * uniMaterial.baseColor;
vec3 emissive = texture(uniMaterial.emissiveMap, vertMeshInfo.vertTexcoords).rgb * uniMaterial.emissive;
vec3 finalColor = diffuse + emissive;

fragColor = vec4(diffuse + emissive, alpha);
// Gamma correction; this is temporary and will be removed later
finalColor = pow(finalColor, vec3(1.0 / 2.2));

fragColor = vec4(finalColor, alpha);
fragNormal = normal;
}

0 comments on commit f5ce32b

Please sign in to comment.