文件
dayu-color/大鱼调色/Plugin/DYTracking.metal
T

61 行
4.5 KiB
Metal

#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>=12.f)&&p.x<w-13&&p.y<h-13;}
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 21x21 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[14],gx[14],gy[14],mean=0,hxx=0,hxy=0,hyy=0;
for(uint j=0;j<14;j++) {
uint k=lane+j*32;ref[j]=gx[j]=gy[j]=0;if(k>=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);
}
// Full host frame -> top-first display luma. Low-pass the source footprint
// before decimation so 4K fabric detail cannot alias into inconsistent motion.
// This analysis-only image never changes the full-resolution color render.
kernel void dyToolCapture(texture2d<float> 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;constexpr sampler f(coord::normalized,address::clamp_to_edge,filter::linear);uint2 taps=clamp(uint2(ceil(float2(source.get_width(),source.get_height())/float2(info.xy))),uint2(1),uint2(8));float sum=0;
for(uint y=0;y<taps.y;y++)for(uint x=0;x<taps.x;x++){float2 uv=(float2(pos)+(float2(x,y)+.5f)/float2(taps))/float2(info.xy);if(info.z)uv.y=1-uv.y;float4 sample=source.sample(f,uv);float3 linear=sample.a>0?sample.rgb/sample.a:float3(0);float3 encoded=select(1.055f*pow(max(linear,float3(0)),float3(1.f/2.4f))-.055f,linear*12.92f,linear<=.0031308f);sum+=dot(clamp(encoded,0.f,1.f),float3(.299f,.587f,.114f));}
pixels[pos.y*info.x+pos.x]=sum/float(taps.x*taps.y);
}