#include using namespace metal; struct PyramidInfo {int width[5],height[5],offset[5],levels;}; kernel void dyTrackPyramid(device float*f[[buffer(0)]],constant PyramidInfo&i[[buffer(1)]],constant int&level[[buffer(2)]],uint2 p[[thread_position_in_grid]]) { int w=i.width[level],h=i.height[level];if(p.x>=uint(w)||p.y>=uint(h))return; int sw=i.width[level-1],sh=i.height[level-1];float sum=0; for(int y=-1;y<=1;y++)for(int x=-1;x<=1;x++)sum+=f[i.offset[level-1]+clamp(int(p.y)*2+y,0,sh-1)*sw+clamp(int(p.x)*2+x,0,sw-1)]*(x==0?2.f:1.f)*(y==0?2.f:1.f); f[i.offset[level]+p.y*w+p.x]=sum/16.f; } inline bool inside(float2 p,int w,int h){return all(p>=12.f)&&p.x0;level--){float s=float(1<=0;level--) { float2 pp=p/float(1<=441)continue; float2 pos=pp+float2(int(k%21)-10,int(k/21)-10);ref[j]=pixel(a,w,pos);mean+=ref[j]; gx[j]=.5f*(pixel(a,w,pos+float2(1,0))-pixel(a,w,pos-float2(1,0))); gy[j]=.5f*(pixel(a,w,pos+float2(0,1))-pixel(a,w,pos-float2(0,1))); hxx+=gx[j]*gx[j];hxy+=gx[j]*gy[j];hyy+=gy[j]*gy[j]; } mean=simd_sum(mean)/441;hxx=simd_sum(hxx);hxy=simd_sum(hxy);hyy=simd_sum(hyy); float det=hxx*hyy-hxy*hxy;if(det<1e-8f||hxx+hyy<1e-5f)return false; for(int iteration=0;iteration<30;iteration++) { if(!inside(q,w,h))return false;float target[14],tm=0; for(uint j=0;j<14;j++){uint k=lane+j*32;target[j]=k<441?pixel(b,w,q+float2(int(k%21)-10,int(k/21)-10)):0;tm+=target[j];} tm=simd_sum(tm)/441;float bx=0,by=0,sse=0; for(uint j=0;j<14;j++)if(lane+j*32<441){float e=ref[j]-mean-target[j]+tm;bx+=gx[j]*e;by+=gy[j]*e;sse+=e*e;} bx=simd_sum(bx);by=simd_sum(by);sse=simd_sum(sse); float2 delta=float2(hyy*bx-hxy*by,hxx*by-hxy*bx)/det;if(!all(isfinite(delta))||length(delta)>8)return false; q+=delta;error=sqrt(sse/441);if(dot(delta,delta)<.0001f)break; } if(level>0)q*=2; } return inside(q,i.width[0],i.height[0])&&error<.12f; } kernel void dyTrackFlow(device const float*prev[[buffer(0)]],device const float*next[[buffer(1)]],device const float2*points[[buffer(2)]],device float4*results[[buffer(3)]],constant PyramidInfo&i[[buffer(4)]],uint gid[[thread_position_in_grid]],uint lane[[thread_index_in_simdgroup]]) { uint idx=gid/32;float2 p=points[idx],q=0,back=0;float error=0,backError=0; bool ok=trackFlow(prev,next,i,p,p,q,error,lane); if(ok)ok=trackFlow(next,prev,i,q,p,back,backError,lane)&&distance(back,p)<=1.1f; if(lane==0)results[idx]=float4(q,error,ok?1.f:0.f); } kernel void dyToolEncodeGamma22(texture2d source [[texture(0)]],texture2d output [[texture(1)]],uint2 pos [[thread_position_in_grid]]){ if(pos.x>=output.get_width()||pos.y>=output.get_height())return;float4 pixel=source.read(pos);float alpha=max(pixel.a,0.f);float3 straight=alpha>0?pixel.rgb/alpha:float3(0);output.write(float4(pow(max(straight,float3(0)),float3(1.f/2.2f))*alpha,alpha),pos); } // Lanczos RGBA8 -> top-first OpenCV Gray8. Analysis never changes color output. kernel void dyToolCapture(texture2d source [[texture(0)]],device float* pixels [[buffer(0)]],constant uint3& info [[buffer(1)]],uint2 pos [[thread_position_in_grid]]){ if(pos.x>=info.x||pos.y>=info.y)return;uint2 point=uint2(pos.x,info.z?info.y-1-pos.y:pos.y);uint3 rgb=uint3(rint(source.read(point).rgb*255.f)); pixels[pos.y*info.x+pos.x]=float((rgb.r*9798u+rgb.g*19235u+rgb.b*3735u+16384u)>>15)/255.f; } // Reuse the analysis source texture for the lightweight color preview. kernel void dyToolTrackingPreview(texture2d source [[texture(0)]], device uchar4 *pixels [[buffer(0)]], constant uint3 &info [[buffer(1)]], uint2 pos [[thread_position_in_grid]]){ if(any(pos>=info.xy))return; constexpr sampler filter(coord::normalized,address::clamp_to_edge,filter::linear); float2 uv=(float2(pos)+.5f)/float2(info.xy); if(info.z)uv.y=1-uv.y; float3 rgb=source.sample(filter,uv).rgb; pixels[pos.y*info.x+pos.x]=uchar4(uchar3(rint(saturate(rgb)*255)),255); }