1#version 450
2#ifndef SINGLE_RESOURCE
3#extension GL_EXT_nonuniform_qualifier : require
4#endif
5// Draws a solid fill, a client dmabuf (sampled image) or a client wl_shm
6// buffer (B8G8R8A8 words in a storage buffer). Client pixels are
7// premultiplied, as Wayland buffers are: the pipeline blends ONE,
8// ONE_MINUS_SRC_ALPHA. Scaled draws filter linearly; 1:1 draws read the
9// texel under the fragment, pixel exact.
10
11layout(location = 0) in Parameters {
12 flat vec4 map;
13 flat vec4 mapy;
14 flat vec4 color;
15 flat vec4 crop;
16 flat uvec4 buf;
17 flat uvec4 misc;
18 flat uint resourceIndex;
19} d;
20
21#ifdef SINGLE_RESOURCE
22layout(set = 0, binding = 0) uniform sampler2D tex;
23layout(std430, set = 0, binding = 1) readonly buffer Pixels { uint pixels[]; };
24layout(set = 0, binding = 2) uniform sampler2D chromaTex;
25#define PIXEL(i) pixels[i]
26#else
27layout(constant_id = 0) const int resourceCount = 1;
28layout(set = 0, binding = 0) uniform sampler2D textures[resourceCount];
29layout(std430, set = 0, binding = 1) readonly buffer Pixels { uint pixels[]; } pixelBuffers[resourceCount];
30layout(set = 0, binding = 2) uniform sampler2D chromaTextures[resourceCount];
31#define tex textures[nonuniformEXT(d.resourceIndex)]
32#define chromaTex chromaTextures[nonuniformEXT(d.resourceIndex)]
33#define PIXEL(i) pixelBuffers[nonuniformEXT(d.resourceIndex)].pixels[i]
34#endif
35
36layout(location = 0) out vec4 color;
37
38const uint modeSolid = 0u;
39const uint modeImage = 1u;
40const uint flagOpaque = 1u;
41const uint flagExact = 2u;
42const uint flagPQ = 4u;
43const uint flagExtendedLinear = 8u;
44const uint flagYUV = 16u;
45const uint flagP010 = 32u;
46// flagSmooth averages four samples a quarter of a target pixel around the
47// point: a thumbnail shrunk under half size keeps thin lines and text.
48const uint flagSmooth = 64u;
49
50// PQ values are absolute nits. BT.2020 is converted in linear light to
51// BT.709, then clipped to the 8-bit sRGB intermediate. Highlights above
52// SDR reference white are lost (no tone mapping).
53vec3 pqToSRGB(vec3 v, float whiteNits) {
54 vec3 p = pow(clamp(v, 0.0, 1.0), vec3(32.0 / 2523.0));
55 vec3 nits = pow(max(p - vec3(3424.0 / 4096.0), vec3(0.0)) /
56 max(vec3(2413.0 / 128.0) - p * (2392.0 / 128.0), vec3(0.00001)), vec3(16384.0 / 2610.0)) * 10000.0;
57 vec3 rgb = vec3(
58 dot(nits, vec3(1.660491, -0.587641, -0.072850)),
59 dot(nits, vec3(-0.124550, 1.132900, -0.008349)),
60 dot(nits, vec3(-0.018151, -0.100579, 1.118730))
61 );
62 rgb = clamp(rgb / whiteNits, 0.0, 1.0);
63 return mix(rgb * 12.92, 1.055 * pow(rgb, vec3(1.0 / 2.4)) - 0.055, greaterThan(rgb, vec3(0.0031308)));
64}
65
66// H.273 Y'CbCr 4:2:0 reconstruction in electrical (non-linear) light.
67// Type 0 places chroma at x=even luma and halfway between luma rows.
68// vec2 coordinates are normalized to the chroma image for linear filtering.
69vec4 sampleYUV(vec2 src) {
70 ivec2 size = textureSize(tex, 0);
71 vec2 luma = clamp(src, d.crop.xy + 0.5, d.crop.zw - 0.5);
72 float y = texture(tex, luma / vec2(size)).r;
73 // Type 0 chroma is shifted half a luma pixel in x. Clamp in
74 // half-resolution texel space before filtering the chroma plane.
75 vec2 chroma = clamp((src + vec2(0.5, 0.0)) * 0.5,
76 d.crop.xy * 0.5 + 0.5, d.crop.zw * 0.5 - 0.5);
77 vec2 chromaUV = chroma * 2.0 / vec2(size);
78 vec2 cbcr = texture(chromaTex, chromaUV).rg;
79 bool tenBit = (d.misc.y & flagP010) != 0u;
80 bool limited = ((d.buf.w >> 8) & 255u) == 2u;
81 float maxCode = tenBit ? 1023.0 : 255.0;
82 // Packed 10-bit plane views normalize directly to the 0..1023 code range.
83 float yOff = limited ? (tenBit ? 64.0 : 16.0) / maxCode : 0.0;
84 float yScale = limited ? maxCode / (tenBit ? 876.0 : 219.0) : 1.0;
85 float cScale = limited ? maxCode / (tenBit ? 896.0 : 224.0) : 1.0;
86 float yy = (y - yOff) * yScale;
87 vec2 cc = (cbcr - vec2(tenBit ? 512.0/1023.0 : 128.0/255.0)) * cScale;
88 uint coeff = d.buf.w & 255u;
89 float kr = coeff == 4u ? 0.299 : coeff == 6u ? 0.2627 : 0.2126;
90 float kb = coeff == 4u ? 0.114 : coeff == 6u ? 0.0593 : 0.0722;
91 vec3 rgb = vec3(yy + 2.0*(1.0-kr)*cc.y,
92 yy - 2.0*kb*(1.0-kb)/(1.0-kr-kb)*cc.x - 2.0*kr*(1.0-kr)/(1.0-kr-kb)*cc.y,
93 yy + 2.0*(1.0-kb)*cc.x);
94 return vec4(rgb, 1.0);
95}
96
97// texel reads buffer pixel p, clamped to the buffer (clamp to edge).
98vec4 texel(ivec2 p) {
99 p = clamp(p, ivec2(floor(d.crop.xy)), min(ivec2(d.buf.yz) - 1, ivec2(ceil(d.crop.zw)) - 1));
100 // Little-endian B,G,R,A bytes: unpack yields (B, G, R, A).
101 return unpackUnorm4x8(PIXEL(d.buf.x + uint(p.y) * d.buf.y + uint(p.x))).zyxw;
102}
103
104// sampleBuffer reads the client buffer at src (buffer pixels).
105vec4 sampleBuffer(vec2 src, bool exact) {
106 if (d.misc.x == modeImage && (d.misc.y & flagYUV) != 0u) {
107 return sampleYUV(src);
108 }
109 if (d.misc.x == modeImage) {
110 ivec2 size = textureSize(tex, 0);
111 vec2 p = clamp(src, d.crop.xy + 0.5, d.crop.zw - 0.5);
112 return exact ? texelFetch(tex, clamp(ivec2(floor(p)), ivec2(0), size - 1), 0) : texture(tex, p / vec2(size));
113 }
114 if (exact) {
115 return texel(ivec2(floor(src)));
116 }
117 // Bilinear: four texel reads around the sample point.
118 vec2 p = src - 0.5;
119 ivec2 i = ivec2(floor(p));
120 vec2 f = p - vec2(i);
121 return mix(mix(texel(i), texel(i + ivec2(1, 0)), f.x), mix(texel(i + ivec2(0, 1)), texel(i + ivec2(1, 1)), f.x), f.y);
122}
123
124void main() {
125 vec2 src = d.map.xy + gl_FragCoord.x * d.map.zw + gl_FragCoord.y * d.mapy.xy;
126 bool exact = (d.misc.y & flagExact) != 0u;
127 vec4 c;
128 if (d.misc.x == modeSolid) {
129 c = d.color;
130 } else if ((d.misc.y & flagSmooth) != 0u) {
131 // Premultiplied samples average without fringes.
132 vec2 dx = d.map.zw * 0.25, dy = d.mapy.xy * 0.25;
133 c = (sampleBuffer(src - dx - dy, false) + sampleBuffer(src + dx - dy, false) +
134 sampleBuffer(src - dx + dy, false) + sampleBuffer(src + dx + dy, false)) * 0.25;
135 } else {
136 c = sampleBuffer(src, exact);
137 }
138 if ((d.misc.y & flagOpaque) != 0u) {
139 c.a = 1.0;
140 }
141 if (c.a > 0.0) {
142 if ((d.misc.y & flagPQ) != 0u) {
143 c.rgb = pqToSRGB(c.rgb / c.a, d.color.x) * c.a;
144 } else if ((d.misc.y & flagExtendedLinear) != 0u) {
145 vec3 v = clamp(c.rgb / c.a * (80.0 / 203.0), 0.0, 1.0);
146 c.rgb = mix(v * 12.92, 1.055 * pow(v, vec3(1.0 / 2.4)) - 0.055, greaterThan(v, vec3(0.0031308))) * c.a;
147 }
148 }
149 if (d.mapy.z > 0.0) {
150 // Focus pulse: a screen-like lift toward white, on premultiplied
151 // values (rgb/a + k * (1 - rgb/a)) * a. Black shows it most; white
152 // never saturates.
153 c.rgb += d.mapy.z * (c.a - c.rgb);
154 }
155 if (d.misc.x != modeSolid) {
156 // The surface's alpha modifier; premultiplied, it scales all four.
157 c *= d.color.a;
158 }
159 color = c;
160}