     // ============================
     // Iterations:
     // ============================
-    // Compute offset (for each iteration step) in the right direction.
     vec2 offset = isHorizontal ? vec2(inverseScreenSize.x, 0.0) : vec2(0.0, inverseScreenSize.y);
     
-    // Compute UVs to explore on each side of the edge, orthogonally. 
-    // The QUALITY allows us to step faster.
     vec2 uv1 = currentUv - offset;
     vec2 uv2 = currentUv + offset;
 
-    // Read the lumas at both current extremities of the exploration segment, 
-    // and compute the delta wrt to the local average luma.
-    float lumaEnd1 = getLum(texture(renderedTexture, uv1).rgb);
-    float lumaEnd2 = getLum(texture(renderedTexture, uv2).rgb);
-    lumaEnd1 -= lumaLocalAverage;
-    lumaEnd2 -= lumaLocalAverage;
-
-    // If the luma deltas at the current extremities are larger than the local gradient, 
-    // we have reached the side of the edge.
-    bool reached1 = abs(lumaEnd1) >= gradientScaled;
-    bool reached2 = abs(lumaEnd2) >= gradientScaled;
+    // 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);
+    pixelLumEnd1 -= pixelLumLocalAverage;
+    pixelLumEnd2 -= pixelLumLocalAverage;
+
+    bool reached1 = abs(pixelLumEnd1) >= gradientScaled;
+    bool reached2 = abs(pixelLumEnd2) >= gradientScaled;
     bool reachedBoth = reached1 && reached2;
 
-    // If the side is not reached, we continue to explore in this direction.
     if (!reached1) {
         uv1 -= offset;
     }

    // Detecting where to apply AA:
    // ============================

    // Luma at the current fragment
    float lumaCenter = getLum(colorCenter.rgb);

    // Luma at the four direct neighbours of the current fragment.
    float lumaDown  = getLum(textureOffset(renderedTexture, texCoord, ivec2(0,-1)).rgb);
    float lumaUp    = getLum(textureOffset(renderedTexture, texCoord, ivec2(0,1)).rgb);
    float lumaLeft  = getLum(textureOffset(renderedTexture, texCoord, ivec2(-1,0)).rgb);
    float lumaRight = getLum(textureOffset(renderedTexture, texCoord, ivec2(1,0)).rgb);
    
    // Find the maximum and minimum luma around the current fragment.
    float lumaMin = min(lumaCenter, min(min(lumaDown, lumaUp), min(lumaLeft, lumaRight)));
    float lumaMax = max(lumaCenter, max(max(lumaDown, lumaUp), max(lumaLeft, lumaRight)));

    // Compute the delta.
    float lumaRange = lumaMax - lumaMin;

    // If the luma 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 (lumaRange < max(EDGE_THRESHOLD_MIN, lumaMax * EDGE_THRESHOLD_MAX)) {
        aaFinalColor = colorCenter;
        return;
    }

    // ============================
    // Estimating the offset:
    // ============================
    float distance1 = isHorizontal ? (texCoord.x - uv1.x) : (texCoord.y - uv1.y);
    float distance2 = isHorizontal ? (uv2.x - texCoord.x) : (uv2.y - texCoord.y);

    bool isDirection1 = distance1 < distance2;
    float distanceFinal = min(distance1, distance2);

    float edgeThickness = (distance1 + distance2);

    // Read in the direction of the closest side of the edge.
    float pixelOffset = - distanceFinal / edgeThickness + 0.5;

    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;

    // 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) + 
        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;

    // Biggest of the two offsets.
    finalOffset = max(finalOffset, subPixelOffsetFinal);

    vec2 finalUV = texCoord;
    if (isHorizontal) {
        finalUV.y += finalOffset * stepLength;
    } else {
        finalUV.x += finalOffset * stepLength;
    }

    aaFinalColor = texture(renderedTexture, finalUV);
}