Branchwell

PulseCanvas / app · pulsecanvas.ai / visionOS / VisionPresentation.swift

Raw 1.6 KB on 15681b389943bdb82ce0ee35ea3df6538f046361
1import Foundation
2
3/// One inexpensive, edge-aware pass, confined to headset presentation.
4/// Mac exports and the visualizers' source shaders are not modified.
5enum VisionPresentation {
6 static let width = 1920
7 static let height = 1080
8 static let smoothingShader = """
9 #include <metal_stdlib>
10 using namespace metal;
11 kernel void smoothPresentation(texture2d<float, access::sample> source [[texture(0)]],
12 texture2d<float, access::write> target [[texture(1)]],
13 constant uint &enabled [[buffer(0)]], uint2 p [[thread_position_in_grid]]) {
14 if (p.x >= target.get_width() || p.y >= target.get_height()) return;
15 float4 c = source.read(p);
16 if (!enabled) { target.write(c, p); return; }
17 float4 n = source.read(uint2(p.x, p.y == 0 ? 0 : p.y - 1));
18 float4 s = source.read(uint2(p.x, min(p.y + 1, target.get_height() - 1)));
19 float4 w = source.read(uint2(p.x == 0 ? 0 : p.x - 1, p.y));
20 float4 e = source.read(uint2(min(p.x + 1, target.get_width() - 1), p.y));
21 float3 weights = float3(0.2126, 0.7152, 0.0722);
22 float lc = dot(c.rgb, weights), ln = dot(n.rgb, weights), ls = dot(s.rgb, weights);
23 float lw = dot(w.rgb, weights), le = dot(e.rgb, weights);
24 float low = min(lc, min(min(ln, ls), min(lw, le)));
25 float high = max(lc, max(max(ln, ls), max(lw, le)));
26 float amount = smoothstep(max(0.04, high * 0.08), max(0.08, high * 0.3), high - low) * 0.65;
27 float4 result = mix(c, (c * 4.0 + n + s + w + e) / 8.0, amount);
28 target.write(float4(result.rgb, c.a), p);
29 }
30 """
31}