1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2026-06-24 12:05:21 -04:00
Files
DankMaterialShell/quickshell/Shaders/frag/wp_fade.frag
T
hecate cantus 1a39b7f66c shader-based scrolling wallpaper mode (#1802)
* feat: parallax-scroll wallpaper

Add a `Scrolling` wallpaper fill mode that translates the wallpaper crop
with the active workspace — like Android home-screen parallax, but along
niri's vertical workspace axis.

The image is scaled to cover the screen on its non-scroll axis, and the
active workspace index drives a fractional offset into the cropped
overflow along the scroll axis. Scroll position is spring-animated
CPU-side and handed to a minimal single-texture shader as a UV offset.
Per-monitor scroll position is published into SessionData so the lock
screen renders the same crop as the active workspace, keeping visual
continuity across lock/unlock.

Two implementation details worth calling out for review:

- QSG_USE_SIMPLE_ANIMATION_DRIVER=1 is exported to the spawned quickshell
  process. The default animation driver advances in fixed ~16ms steps,
  capping the scroll at 60Hz and desyncing it from compositor motion on
  high-refresh displays; the simple driver advances by real elapsed time,
  restoring native-refresh pacing. Removing it visibly regresses to 60Hz.

- The wallpaper survives wl_output rebind cycles (e.g. OLED image-cleaning
  on DPMS soft-off), which otherwise leave a stuck or void background.
  Recovery re-anchors the scroll target on output-lifecycle signals,
  rebuilds the ShaderEffect against the current render context, and
  re-attaches the wallpaper-layer surface on unlock for parallax-active
  monitors — guarded against lock state so the shader gets reliable frame
  hints.

* simplify bindings and gate lock screen shader in a loader

---------

Co-authored-by: bbedward <bbedward@gmail.com>
2026-06-24 00:14:42 -04:00

117 lines
4.5 KiB
GLSL

// ===== wp_fade.frag =====
#version 450
layout(location = 0) in vec2 qt_TexCoord0;
layout(location = 0) out vec4 fragColor;
layout(binding = 1) uniform sampler2D source1;
layout(binding = 2) uniform sampler2D source2;
layout(std140, binding = 0) uniform buf {
mat4 qt_Matrix;
float qt_Opacity;
float progress;
// Fill mode parameters
float fillMode; // 0=stretch, 1=fit, 2=crop, 3=tile, 4=tileV, 5=tileH, 6=pad, 7=scroll
float imageWidth1; // Width of source1 image
float imageHeight1; // Height of source1 image
float imageWidth2; // Width of source2 image
float imageHeight2; // Height of source2 image
float screenWidth; // Screen width
float screenHeight; // Screen height
vec4 fillColor; // Fill color for empty areas (default: black)
// Scroll position (0-100 range, only used when fillMode >= 6.5)
float scrollX;
float scrollY;
} ubuf;
vec2 calculateUV(vec2 uv, float imgWidth, float imgHeight) {
vec2 transformedUV = uv;
if (ubuf.fillMode < 0.5) {
transformedUV = uv;
}
else if (ubuf.fillMode < 1.5) {
float scale = min(ubuf.screenWidth / imgWidth, ubuf.screenHeight / imgHeight);
vec2 scaledImageSize = vec2(imgWidth, imgHeight) * scale;
vec2 offset = (vec2(ubuf.screenWidth, ubuf.screenHeight) - scaledImageSize) * 0.5;
vec2 screenPixel = uv * vec2(ubuf.screenWidth, ubuf.screenHeight);
vec2 imagePixel = (screenPixel - offset) / scale;
transformedUV = imagePixel / vec2(imgWidth, imgHeight);
}
else if (ubuf.fillMode < 2.5) {
float scale = max(ubuf.screenWidth / imgWidth, ubuf.screenHeight / imgHeight);
vec2 scaledImageSize = vec2(imgWidth, imgHeight) * scale;
vec2 offset = (scaledImageSize - vec2(ubuf.screenWidth, ubuf.screenHeight)) / scaledImageSize;
transformedUV = uv * (vec2(1.0) - offset) + offset * 0.5;
}
else if (ubuf.fillMode < 3.5) {
transformedUV = fract(uv * vec2(ubuf.screenWidth, ubuf.screenHeight) / vec2(imgWidth, imgHeight));
}
else if (ubuf.fillMode < 4.5) {
vec2 tileUV = uv * vec2(ubuf.screenWidth, ubuf.screenHeight) / vec2(imgWidth, imgHeight);
transformedUV = vec2(uv.x, fract(tileUV.y));
}
else if (ubuf.fillMode < 5.5) {
vec2 tileUV = uv * vec2(ubuf.screenWidth, ubuf.screenHeight) / vec2(imgWidth, imgHeight);
transformedUV = vec2(fract(tileUV.x), uv.y);
}
else if (ubuf.fillMode < 6.5) {
// fillMode 6 = Pad
vec2 screenPixel = uv * vec2(ubuf.screenWidth, ubuf.screenHeight);
vec2 imageOffset = (vec2(ubuf.screenWidth, ubuf.screenHeight) - vec2(imgWidth, imgHeight)) * 0.5;
vec2 imagePixel = screenPixel - imageOffset;
transformedUV = imagePixel / vec2(imgWidth, imgHeight);
}
else {
// fillMode 7 = Scroll (Crop with variable offset)
float imageAspect = imgWidth / imgHeight;
float screenAspect = ubuf.screenWidth / ubuf.screenHeight;
float scale = max(ubuf.screenWidth / imgWidth, ubuf.screenHeight / imgHeight);
vec2 scaledImageSize = vec2(imgWidth, imgHeight) * scale;
vec2 offset = (scaledImageSize - vec2(ubuf.screenWidth, ubuf.screenHeight)) / scaledImageSize;
// Determine scroll axis based on aspect ratio
bool scrollHorizontal = imageAspect > screenAspect + 0.01;
bool scrollVertical = imageAspect < screenAspect - 0.01;
vec2 scrollOffset = vec2(
scrollHorizontal ? offset.x * (ubuf.scrollX / 100.0) : offset.x * 0.5,
scrollVertical ? offset.y * (ubuf.scrollY / 100.0) : offset.y * 0.5
);
transformedUV = uv * (vec2(1.0) - offset) + scrollOffset;
}
return transformedUV;
}
vec4 sampleWithFillMode(sampler2D tex, vec2 uv, float imgWidth, float imgHeight) {
vec2 transformedUV = calculateUV(uv, imgWidth, imgHeight);
if (ubuf.fillMode >= 2.5 && ubuf.fillMode <= 5.5) {
return texture(tex, transformedUV);
}
if (transformedUV.x < 0.0 || transformedUV.x > 1.0 ||
transformedUV.y < 0.0 || transformedUV.y > 1.0) {
return ubuf.fillColor;
}
return texture(tex, transformedUV);
}
void main() {
vec2 uv = qt_TexCoord0;
// Sample textures with fill mode
vec4 color1 = sampleWithFillMode(source1, uv, ubuf.imageWidth1, ubuf.imageHeight1);
vec4 color2 = sampleWithFillMode(source2, uv, ubuf.imageWidth2, ubuf.imageHeight2);
// Mix the two textures based on progress value
fragColor = mix(color1, color2, ubuf.progress) * ubuf.qt_Opacity;
}