Bringing the general shaders into coding style

This commit is contained in:
Alexander Bock
2026-01-30 15:23:31 +01:00
parent 1c7db34d86
commit 9ae1bbc9df
7 changed files with 125 additions and 101 deletions
+7 -7
View File
@@ -31,8 +31,8 @@
* src is expressed in straight RGBA
*/
void normalBlend(inout vec4 dst, vec4 src) {
dst.rgb = dst.rgb + (1.f - dst.a) * src.a * src.rgb;
dst.a = dst.a + (1.f - dst.a) * src.a;
dst.rgb = dst.rgb + (1.0 - dst.a) * src.a * src.rgb;
dst.a = dst.a + (1.0 - dst.a) * src.a;
}
/**
@@ -41,7 +41,7 @@ void normalBlend(inout vec4 dst, vec4 src) {
* src is expressed in straight RGBA
*/
void additiveBlend(inout vec4 dst, vec4 src) {
dst.rgb = dst.rgb + (1.f - dst.a) * src.a * src.rgb;
dst.rgb = dst.rgb + (1.0 - dst.a) * src.a * src.rgb;
}
@@ -53,7 +53,7 @@ void additiveBlend(inout vec4 dst, vec4 src) {
* stepSize = 1: alpha becomes src.a
*/
void normalBlendStep(inout vec4 dst, vec4 src, float stepSize) {
src.a = 1.f - pow(1.f - src.a, stepSize);
src.a = 1.0 - pow(1.0 - src.a, stepSize);
normalBlend(dst, src);
}
@@ -75,8 +75,8 @@ void additiveBlendStep(inout vec4 dst, vec4 src, float stepSize) {
* src is expressed in straight RGBA
*/
void blend(inout vec4 dst, vec4 src) {
dst.rgb = dst.rgb + (1.f - dst.a) * src.a * src.rgb;
dst.a = dst.a + (1.f - dst.a) * src.a;
dst.rgb = dst.rgb + (1.0 - dst.a) * src.a * src.rgb;
dst.a = dst.a + (1.0 - dst.a) * src.a;
}
/**
@@ -87,7 +87,7 @@ void blend(inout vec4 dst, vec4 src) {
* stepSize = 1: alpha becomes src.a
*/
void blendStep(inout vec4 dst, vec4 src, float stepSize) {
src.a = 1.0 - pow(1.f - src.a, stepSize);
src.a = 1.0 - pow(1.0 - src.a, stepSize);
blend(dst, src);
}
+55 -38
View File
@@ -24,15 +24,12 @@
#version __CONTEXT__
#define EDGE_THRESHOLD_MIN 0.0312f
#define EDGE_THRESHOLD_MAX 0.125f
#define ITERATIONS 12
#define SUBPIXEL_QUALITY 0.75f
const float EdgeThresholdMin = 0.0312;
const float EdgeThresholdMax = 0.125;
const int Iterations = 12;
const float SubpixelQuality = 0.75;
const float[12] QUALITY = float[](1.f, 1.f, 1.f, 1.f, 1.f, 1.5f, 2.f, 2.f, 2.f, 2.f, 4.f, 8.f);
// const float[24] QUALITY = {2.f, 4.f, 6.f, 8.f, 10.f, 12.f, 12.f, 12.f, 12.f, 12.f, 14.f, 18.f,
// 18.f, 18.f, 18.f, 18.f, 18.f, 18.f, 18.f, 18.f, 18.f, 18.f,
// 18.f, 18.f};
const float[12] Quality = float[](1.f, 1.f, 1.f, 1.f, 1.f, 1.5f, 2.f, 2.f, 2.f, 2.f, 4.f, 8.f);
in vec2 texCoord;
layout (location = 0) out vec4 aaFinalColor;
@@ -43,7 +40,7 @@ uniform vec4 Viewport;
uniform vec2 Resolution;
// Relative luminance
float getLum(vec3 rgb){
float luminance(vec3 rgb) {
return dot(vec3(0.2126, 0.7152, 0.0722), rgb);
}
@@ -62,30 +59,42 @@ void main() {
vec4 colorCenter = texture(renderedTexture, st);
// Detecting where to apply AA
float pixelLumCenter = getLum(colorCenter.rgb);
float pixelLumDown = getLum(textureOffset(renderedTexture, st, ivec2(0,-1)).rgb);
float pixelLumUp = getLum(textureOffset(renderedTexture, st, ivec2(0,1)).rgb);
float pixelLumLeft = getLum(textureOffset(renderedTexture, st, ivec2(-1,0)).rgb);
float pixelLumRight = getLum(textureOffset(renderedTexture, st, ivec2(1,0)).rgb);
float pixelLumCenter = luminance(colorCenter.rgb);
float pixelLumDown = luminance(textureOffset(renderedTexture, st, ivec2(0, -1)).rgb);
float pixelLumUp = luminance(textureOffset(renderedTexture, st, ivec2(0, 1)).rgb);
float pixelLumLeft = luminance(textureOffset(renderedTexture, st, ivec2(-1, 0)).rgb);
float pixelLumRight = luminance(textureOffset(renderedTexture, st, ivec2(1, 0)).rgb);
float pixelLumMin = min(pixelLumCenter, min(min(pixelLumDown, pixelLumUp), min(pixelLumLeft, pixelLumRight)));
float pixelLumMax = max(pixelLumCenter, max(max(pixelLumDown, pixelLumUp), max(pixelLumLeft, pixelLumRight)));
float pixelLumMin = min(
pixelLumCenter, min(min(pixelLumDown, pixelLumUp), min(pixelLumLeft, pixelLumRight))
);
float pixelLumMax = max(
pixelLumCenter, max(max(pixelLumDown, pixelLumUp), max(pixelLumLeft, pixelLumRight))
);
// Delta
float pixelLumRange = pixelLumMax - pixelLumMin;
// If the pixelLum variation is lower that a threshold (or if we are in a really dark
// area), we are not on an edge, don't perform any AA.
if (pixelLumRange < max(EDGE_THRESHOLD_MIN, pixelLumMax * EDGE_THRESHOLD_MAX)) {
if (pixelLumRange < max(EdgeThresholdMin, pixelLumMax * EdgeThresholdMax)) {
aaFinalColor = colorCenter;
return;
}
// Estimating the gradient
float pixelLumDownLeft = getLum(textureOffset(renderedTexture, st, ivec2(-1,-1)).rgb);
float pixelLumUpRight = getLum(textureOffset(renderedTexture, st, ivec2(1,1)).rgb);
float pixelLumUpLeft = getLum(textureOffset(renderedTexture, st, ivec2(-1,1)).rgb);
float pixelLumDownRight = getLum(textureOffset(renderedTexture, st, ivec2(1,-1)).rgb);
float pixelLumDownLeft = luminance(
textureOffset(renderedTexture, st, ivec2(-1, -1)).rgb
);
float pixelLumUpRight = luminance(
textureOffset(renderedTexture, st, ivec2(1, 1)).rgb
);
float pixelLumUpLeft = luminance(
textureOffset(renderedTexture, st, ivec2(-1, 1)).rgb
);
float pixelLumDownRight = luminance(
textureOffset(renderedTexture, st, ivec2(1, -1)).rgb
);
float pixelLumDownUp = pixelLumDown + pixelLumUp;
float pixelLumLeftRight = pixelLumLeft + pixelLumRight;
@@ -96,14 +105,16 @@ void main() {
// Compute an estimation of the gradient
float edgeHorizontal = abs(-2.0 * pixelLumLeft + pixelLumLeftCorners) +
abs(-2.0 * pixelLumCenter + pixelLumDownUp) * 2.0 + abs(-2.0 * pixelLumRight + pixelLumRightCorners);
abs(-2.0 * pixelLumCenter + pixelLumDownUp) * 2.0 +
abs(-2.0 * pixelLumRight + pixelLumRightCorners);
float edgeVertical = abs(-2.0 * pixelLumUp + pixelLumUpCorners) +
abs(-2.0 * pixelLumCenter + pixelLumLeftRight) * 2.0 + abs(-2.0 * pixelLumDown + pixelLumDownCorners);
abs(-2.0 * pixelLumCenter + pixelLumLeftRight) * 2.0 +
abs(-2.0 * pixelLumDown + pixelLumDownCorners);
// Choosing Edge Orientation
bool isHorizontal = (edgeHorizontal >= edgeVertical);
float pixelLum1 = isHorizontal ? pixelLumDown : pixelLumLeft;
float pixelLum2 = isHorizontal ? pixelLumUp : pixelLumRight;
float pixelLum1 = isHorizontal ? pixelLumDown : pixelLumLeft;
float pixelLum2 = isHorizontal ? pixelLumUp : pixelLumRight;
// Gradients
float gradient1 = pixelLum1 - pixelLumCenter;
@@ -143,8 +154,8 @@ void main() {
// Read the pixelLums at both current extremities of the exploration segment,
// and compute the delta wrt to the local average pixelLum.
float pixelLumEnd1 = getLum(texture(renderedTexture, uv1).rgb);
float pixelLumEnd2 = getLum(texture(renderedTexture, uv2).rgb);
float pixelLumEnd1 = luminance(texture(renderedTexture, uv1).rgb);
float pixelLumEnd2 = luminance(texture(renderedTexture, uv2).rgb);
pixelLumEnd1 -= pixelLumLocalAverage;
pixelLumEnd2 -= pixelLumLocalAverage;
@@ -162,15 +173,15 @@ void main() {
// Still exploring
if (!reachedBoth) {
for (int i = 2; i < ITERATIONS; i++) {
for (int i = 2; i < Iterations; i++) {
// If needed, read pixelLum in 1st direction, compute delta.
if (!reached1) {
pixelLumEnd1 = getLum(texture(renderedTexture, uv1).rgb);
pixelLumEnd1 = luminance(texture(renderedTexture, uv1).rgb);
pixelLumEnd1 = pixelLumEnd1 - pixelLumLocalAverage;
}
// If needed, read pixelLum in opposite direction, compute delta.
if (!reached2) {
pixelLumEnd2 = getLum(texture(renderedTexture, uv2).rgb);
pixelLumEnd2 = luminance(texture(renderedTexture, uv2).rgb);
pixelLumEnd2 = pixelLumEnd2 - pixelLumLocalAverage;
}
reached1 = abs(pixelLumEnd1) >= gradientScaled;
@@ -179,11 +190,11 @@ void main() {
// If the side is not reached
if (!reached1) {
uv1 -= offset * QUALITY[i];
uv1 -= offset * Quality[i];
}
if (!reached2) {
uv2 += offset * QUALITY[i];
uv2 += offset * Quality[i];
}
// If both sides have been reached
@@ -205,22 +216,28 @@ void main() {
// Read in the direction of the closest side of the edge
float pixelOffset = - distanceFinal / edgeThickness + 0.5;
bool ispixelLumCenterSmaller = pixelLumCenter < pixelLumLocalAverage;
bool isPixelLumCenterSmaller = pixelLumCenter < pixelLumLocalAverage;
// If the pixelLum at center is smaller than at its neighbour, the delta pixelLum at
// each end should be positive (same variation).
bool correctVariation = ((isDirection1 ? pixelLumEnd1 : pixelLumEnd2) < 0.0) != ispixelLumCenterSmaller;
bool correctVariation =
((isDirection1 ? pixelLumEnd1 : pixelLumEnd2) < 0.0) != isPixelLumCenterSmaller;
// If the pixelLum variation is incorrect, do not offset.
float finalOffset = correctVariation ? pixelOffset : 0.0;
// Subpixel antialiasing
float pixelLumAverage = (1.0/12.0) * (2.0 * (pixelLumDownUp + pixelLumLeftRight) +
float pixelLumAverage = (1.0 / 12.0) * (2.0 * (pixelLumDownUp + pixelLumLeftRight) +
pixelLumLeftCorners + pixelLumRightCorners);
float subPixelOffset1 = clamp(abs(pixelLumAverage - pixelLumCenter) / pixelLumRange, 0.0, 1.0);
float subPixelOffset2 = (-2.0 * subPixelOffset1 + 3.0) * subPixelOffset1 * subPixelOffset1;
float subPixelOffsetFinal = subPixelOffset2 * subPixelOffset2 * SUBPIXEL_QUALITY;
float subPixelOffset1 =
clamp(abs(pixelLumAverage - pixelLumCenter) / pixelLumRange,
0.0,
1.0
);
float subPixelOffset2 =
(-2.0 * subPixelOffset1 + 3.0) * subPixelOffset1 * subPixelOffset1;
float subPixelOffsetFinal = subPixelOffset2 * subPixelOffset2 * SubpixelQuality;
// Biggest of the two offsets
finalOffset = max(finalOffset, subPixelOffsetFinal);
+5 -6
View File
@@ -24,16 +24,15 @@
#version __CONTEXT__
layout (location = 0) out vec4 finalColor;
in vec2 vTexCoord;
// flat in vec3 vPosition;
layout (location = 0) out vec4 finalColor;
uniform int currentSample;
uniform sampler2DMS pixelSizeTexture;
void main() {
finalColor = vec4(texelFetch(pixelSizeTexture, ivec2(vTexCoord), currentSample).rgb, 1.0);
//finalColor = vec4(0.5*vPosition.xy + 0.5, 1.0, 1.0);
//finalColor = vec4(vPosition.xy, 0.0, 1.0);
finalColor = vec4(
texelFetch(pixelSizeTexture, ivec2(vTexCoord), currentSample).rgb,
1.0
);
}
-4
View File
@@ -26,13 +26,9 @@
layout(location = 0) in vec4 position;
layout(location = 1) in vec2 texCoord;
out vec2 vTexCoord;
// flat out vec3 vPosition;
void main() {
vTexCoord = texCoord;
// vPosition = position.xyz;
gl_Position = position;
}
+27 -18
View File
@@ -45,7 +45,7 @@ uniform int rayCastSteps;
out vec4 finalColor;
#define ALPHA_LIMIT 0.99
const float AlphaLimit = 0.99;
#include <#{getEntryPath}>
@@ -56,7 +56,7 @@ void main() {
vec4 exitColorTexture = texture(exitColorTexture, texCoord);
// If we don't have an exit, discard the ray
if (exitColorTexture.a < 1.f || exitColorTexture.rgb == vec3(0.f)) {
if (exitColorTexture.a < 1.0 || exitColorTexture.rgb == vec3(0.0)) {
discard;
}
@@ -64,13 +64,13 @@ void main() {
vec3 exitPos = exitColorTexture.rgb;
float exitDepth = denormalizeFloat(texture(exitDepthTexture, texCoord).x);
float jitterFactor = 0.5f + 0.5f * rand(gl_FragCoord.xy); // should be between 0.5 and 1.0
float jitterFactor = 0.5 + 0.5 * rand(gl_FragCoord.xy); // should be between 0.5 and 1.0
vec3 entryPos;
float entryDepth;
vec3 entryPos = vec3(0.0);
float entryDepth = 0.0;
getEntry(entryPos, entryDepth);
// If we don't have an entry, discard the ray
if (entryPos == vec3(0.f)) {
if (entryPos == vec3(0.0)) {
discard;
}
@@ -81,29 +81,29 @@ void main() {
float raycastDepth = length(diff);
float geoDepth = denormalizeFloat((texture(mainDepthTexture, texCoord).x));
float geoRatio = clamp((geoDepth - entryDepth) / (exitDepth - entryDepth), 0.f, 1.f);
float geoRatio = clamp((geoDepth - entryDepth) / (exitDepth - entryDepth), 0.0, 1.0);
raycastDepth = geoRatio * raycastDepth;
float currentDepth = 0.f;
float currentDepth = 0.0;
float nextStepSize = stepSize#{id}(position, direction);
float currentStepSize;
float previousJitterDistance = 0.f;
float previousJitterDistance = 0.0;
int nSteps = 0;
int sampleIndex = 0;
float opacityDecay = 1.f;
float opacityDecay = 1.0;
vec3 accumulatedColor = vec3(0.f);
vec3 accumulatedAlpha = vec3(0.f);
vec3 accumulatedColor = vec3(0.0);
vec3 accumulatedAlpha = vec3(0.0);
for (nSteps = 0;
(accumulatedAlpha.r < ALPHA_LIMIT || accumulatedAlpha.g < ALPHA_LIMIT ||
accumulatedAlpha.b < ALPHA_LIMIT) && nSteps < rayCastSteps;
++nSteps)
(accumulatedAlpha.r < AlphaLimit || accumulatedAlpha.g < AlphaLimit ||
accumulatedAlpha.b < AlphaLimit) && nSteps < rayCastSteps;
nSteps++)
{
if (nextStepSize < raycastDepth / 10000000000.f) {
if (nextStepSize < raycastDepth / 10000000000.0) {
break;
}
@@ -114,7 +114,13 @@ void main() {
vec3 jitteredPosition = position + direction * jitteredStepSize;
position += direction * currentStepSize;
sample#{id}(jitteredPosition, direction, accumulatedColor, accumulatedAlpha, nextStepSize);
sample#{id}(
jitteredPosition,
direction,
accumulatedColor,
accumulatedAlpha,
nextStepSize
);
float sampleDistance = jitteredStepSize + previousJitterDistance;
previousJitterDistance = currentStepSize - jitteredStepSize;
@@ -123,7 +129,10 @@ void main() {
nextStepSize = min(nextStepSize, maxStepSize);
}
finalColor = vec4(accumulatedColor, (accumulatedAlpha.r + accumulatedAlpha.g + accumulatedAlpha.b) / 3.f);
finalColor = vec4(
accumulatedColor,
(accumulatedAlpha.r + accumulatedAlpha.g + accumulatedAlpha.b) / 3.0
);
finalColor.rgb /= finalColor.a;
+7 -9
View File
@@ -27,8 +27,8 @@
#define exposure #{rendererData.hdrExposure}
#define disableHDRPipeline #{rendererData.disableHDR}
#define DeltaError 0.013f
#define MaxValueColorBuffer 1E10
const float DeltaError = 0.013;
const float MaxValueColorBuffer = 1E10;
layout(location = 0) out vec4 _out_color_;
layout(location = 1) out vec4 gPosition;
@@ -42,7 +42,10 @@ void main() {
_out_color_ = f.color;
}
else {
_out_color_ = vec4((log2(vec3(1.0) - (f.color.rgb - vec3(DeltaError)))/(-exposure)), f.color.a);
_out_color_ = vec4(
log2(vec3(1.0) - (f.color.rgb - vec3(DeltaError))) / -exposure,
f.color.a
);
}
_out_color_.x = isnan(_out_color_.x) ? MaxValueColorBuffer : _out_color_.x;
@@ -52,10 +55,5 @@ void main() {
gPosition = f.gPosition;
gNormal = f.gNormal;
if (f.disableDepthNormalization) {
gl_FragDepth = f.depth;
}
else {
gl_FragDepth = normalizeFloat(f.depth);
}
gl_FragDepth = f.disableDepthNormalization ? f.depth : normalizeFloat(f.depth);
}
+24 -19
View File
@@ -22,8 +22,6 @@
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
const float HCV_EPSILON = 1e-7;
const float HSL_EPSILON = 1e-7;
const float HCY_EPSILON = 1e-7;
// White given by D65
@@ -110,45 +108,52 @@ See top of the file for full license terms.
// Converts from pure Hue to linear RGB
vec3 hue2rgb(float hue) {
float R = abs(hue * 6 - 3) - 1;
float G = 2 - abs(hue * 6 - 2);
float B = 2 - abs(hue * 6 - 4);
return clamp(vec3(R,G,B), 0, 1);
float r = abs(hue * 6 - 3) - 1;
float g = 2 - abs(hue * 6 - 2);
float b = 2 - abs(hue * 6 - 4);
return clamp(vec3(r, g, b), 0.0, 1.0);
}
// Converts a value from linear RGB to HCV (Hue, Chroma, Value)
vec3 rgb2hcv(vec3 rgb) {
const float HCV_EPSILON = 1e-7;
// Based on work by Sam Hocevar and Emil Persson
vec4 P = (rgb.g < rgb.b) ? vec4(rgb.bg, -1.0, 2.0/3.0) : vec4(rgb.gb, 0.0, -1.0/3.0);
vec4 Q = (rgb.r < P.x) ? vec4(P.xyw, rgb.r) : vec4(rgb.r, P.yzx);
float C = Q.x - min(Q.w, Q.y);
float H = abs((Q.w - Q.y) / (6 * C + HCV_EPSILON) + Q.z);
return vec3(H, C, Q.x);
vec4 p =
(rgb.g < rgb.b) ? vec4(rgb.bg, -1.0, 2.0 / 3.0) : vec4(rgb.gb, 0.0, -1.0 / 3.0);
vec4 q = (rgb.r < p.x) ? vec4(p.xyw, rgb.r) : vec4(rgb.r, p.yzx);
float c = q.x - min(q.w, q.y);
float h = abs((q.w - q.y) / (6 * c + HCV_EPSILON) + q.z);
return vec3(h, c, q.x);
}
// Converts from HSL to linear RGB
vec3 hsl2rgb(vec3 hsl) {
vec3 rgb = hue2rgb(hsl.x);
float C = (1 - abs(2 * hsl.z - 1)) * hsl.y;
return (rgb - 0.5) * C + hsl.z;
float c = (1.0 - abs(2.0 * hsl.z - 1.0)) * hsl.y;
return (rgb - 0.5) * c + hsl.z;
}
// Converts from linear rgb to HSL
vec3 rgb2hsl(vec3 rgb) {
const float HSL_EPSILON = 1e-7;
vec3 HCV = rgb2hcv(rgb);
float L = HCV.z - HCV.y * 0.5;
float S = HCV.y / (1 - abs(L * 2 - 1) + HSL_EPSILON);
float S = HCV.y / (1.0 - abs(L * 2.0 - 1.0) + HSL_EPSILON);
return vec3(HCV.x, S, L);
}
vec3 exponentialToneMapping(vec3 color, float exposure, float gamma) {
color *= exposure;
color.r = color.r < 1.413 ? pow(color.r * 0.38317, 1.0 / gamma) : 1.0 - exp2(-exposure * color.r);
color.g = color.g < 1.413 ? pow(color.g * 0.38317, 1.0 / gamma) : 1.0 - exp2(-exposure * color.g);
color.b = color.b < 1.413 ? pow(color.b * 0.38317, 1.0 / gamma) : 1.0 - exp2(-exposure * color.b);
float invGamma = 1.0 / gamma;
return color;
return vec3(
color.r < 1.413 ? pow(color.r * 0.38317, invGamma) : 1.0 - exp2(-exposure * color.r),
color.g < 1.413 ? pow(color.g * 0.38317, invGamma) : 1.0 - exp2(-exposure * color.g),
color.b < 1.413 ? pow(color.b * 0.38317, invGamma) : 1.0 - exp2(-exposure * color.b)
);
}
vec3 toneMappingOperator(vec3 color, float exposure) {
@@ -156,5 +161,5 @@ vec3 toneMappingOperator(vec3 color, float exposure) {
}
vec3 gammaCorrection(vec3 color, float gamma) {
return pow(color, vec3(1.0f / gamma));
return pow(color, vec3(1.0 / gamma));
}