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

88 行
5.7 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);
}
kernel void dyToolEncodeGamma22(texture2d<float,access::read> source [[texture(0)]],texture2d<float,access::write> 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<float> source [[texture(0)]],device uchar* 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]=uchar((rgb.r*9798u+rgb.g*19235u+rgb.b*3735u+16384u)>>15);
}
// Reuse the analysis source texture for the lightweight color preview.
inline uchar4 dyTrackingPreviewPixel(texture2d<float> source,uint2 pos,uint3 info){
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;
return uchar4(uchar3(rint(saturate(source.sample(filter,uv).rgb)*255)),255);
}
kernel void dyToolTrackingPreview(texture2d<float> 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;
pixels[pos.y*info.x+pos.x]=dyTrackingPreviewPixel(source,pos,info);
}
kernel void dyToolVisionPreview(texture2d<float> source [[texture(0)]],
texture2d<float,access::write> image [[texture(1)]],
constant uint3 &info [[buffer(0)]],
constant uint &reverseRows [[buffer(1)]],
uint2 pos [[thread_position_in_grid]]){
if(any(pos>=info.xy))return;
// Reverse the same discrete row as the CPU packer, keeping sampling and
// quantization identical for both image origins and odd sizes.
uint2 sample=uint2(pos.x,reverseRows?info.y-1-pos.y:pos.y);
image.write(float4(dyTrackingPreviewPixel(source,sample,info))/255.f,pos);
}