WIP: independent tool documents, native panel routing and per-stage GPU masks; host canvas and tracking pending

这个提交包含在:
mac 于 2026-11-01 21:40:54 +08:00 提交
1 父节点 edfb74b171
当前提交 f72aa2609d
21 个文件已更改
+1067 -45

没有与搜索条件相匹配的文件

+51
查看文件
@@ -0,0 +1,51 @@
#include <metal_stdlib>
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>=6.f)&&p.x<w-7&&p.y<h-7;}
inline float pixel(device const float*f,int w,float2 p) {
int2 a=int2(p);float2 d=p-float2(a);int idx=a.y*w+a.x;
return mix(mix(f[idx],f[idx+1],d.x),mix(f[idx+w],f[idx+w+1],d.x),d.y);
}
// One SIMD group per feature: lanes cooperatively reduce the 9x9 patch.
// Forward and backward checks stay in the same dispatch and reuse the resident pyramids.
bool trackFlow(device const float*prev,device const float*next,constant PyramidInfo&i,float2 p,float2 guess,thread float2&q,thread float&error,uint lane) {
int level=i.levels-1;for(;level>0;level--){float s=float(1<<level);if(inside(p/s,i.width[level],i.height[level])&&inside(guess/s,i.width[level],i.height[level]))break;}
q=guess/float(1<<level);error=0;
for(;level>=0;level--) {
float2 pp=p/float(1<<level);int w=i.width[level],h=i.height[level];if(!inside(pp,w,h))return false;
device const float*a=prev+i.offset[level];device const float*b=next+i.offset[level];
float ref[3],gx[3],gy[3],mean=0,hxx=0,hxy=0,hyy=0;
for(uint j=0;j<3;j++) {
uint k=lane+j*32;ref[j]=gx[j]=gy[j]=0;if(k>=81)continue;
float2 pos=pp+float2(int(k%9)-4,int(k/9)-4);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)/81;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<20;iteration++) {
if(!inside(q,w,h))return false;float target[3],tm=0;
for(uint j=0;j<3;j++){uint k=lane+j*32;target[j]=k<81?pixel(b,w,q+float2(int(k%9)-4,int(k/9)-4)):0;tm+=target[j];}
tm=simd_sum(tm)/81;float bx=0,by=0,sse=0;
for(uint j=0;j<3;j++)if(lane+j*32<81){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/81);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);
}