文件
dayu-color/大鱼调色/Plugin/DYCanvasToolsDiagnostics.inc.h
T

109 行
18 KiB
C++

// Exercise actual OSC hit geometry, host writes and production Metal output.
{
DYDiagnosticManager *base=[[[DYDiagnosticManager alloc]init]autorelease];DYCreateParameters(base,nil);
DYToolCanvasProbe *host=[[[DYToolCanvasProbe alloc]init]autorelease];host->_base=base;host->_time=kCMTimeZero;
id<DYViewerParameterProbe>bridge=(id)[[NSClassFromString(@"DYViewerParameterBridge")alloc]initWithAPIManager:host];
id<DYViewerControlsProbe>panel=(id)[NSClassFromString(@"DYViewerParameterPanel")new];panel.bridge=bridge;
auto drain=[](){double until=NSProcessInfo.processInfo.systemUptime+.06;while(NSProcessInfo.processInfo.systemUptime<until)[[NSRunLoop mainRunLoop]runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:.002]];};
auto publish=[&](){DYUniforms u;DYReadUniforms(host,host->_time,&u);[bridge publishUniforms:&u document:DYReadTools(host,host->_time).document atTime:host->_time];[bridge publishAssets:DYReadAssets(host,host->_time)atTime:host->_time];[panel prepareWithSize:NSMakeSize(1280,720)scale:1 atTime:host->_time];};
auto area=[&](NSString *kind)->NSDictionary*{for(NSDictionary*a in [(id)panel valueForKey:@"areas"])if([a[@"kind"]isEqual:kind])return [[a copy]autorelease];return nil;};
auto drag=[&](NSPoint a,NSPoint b){[panel event:NSEventTypeLeftMouseDown point:a atTime:host->_time];[panel event:NSEventTypeLeftMouseDragged point:b atTime:host->_time];drain();[panel event:NSEventTypeLeftMouseUp point:b atTime:host->_time];drain();publish();};
[(id)panel setValue:@"hsl" forKey:@"toolType"];publish();NSRect plot=[area(@"hslPlot")[@"rect"]rectValue];int undo=base->_undoStarts;
NSPoint a=NSMakePoint(NSMinX(plot)+plot.size.width*2.5/8,NSMidY(plot)),b=NSMakePoint(a.x,NSMinY(plot)-40);drag(a,b);
DYUniforms edited;DYReadUniforms(host,kCMTimeZero,&edited);run.record(@"canvas-hsl-plot-captures-outside-and-commits-one-undo",edited.hslHueA[2]==100&&base->_undoStarts==undo+1&&base->_undoEnds==undo+1);
NSMutableDictionary *doc=DYMutablePlist(DYReadTools(host,kCMTimeZero).document);NSString *layerID=DYInsertTool(doc,@"hsl",@"base:hsl")[@"id"];DYWriteTools(host,doc,kCMTimeZero);publish();[(id)panel setValue:layerID forKey:@"nodeID"];publish();plot=[area(@"hslPlot")[@"rect"]rectValue];a=NSMakePoint(NSMinX(plot)+plot.size.width*2.5/8,NSMidY(plot));b=NSMakePoint(a.x,NSMidY(plot)-plot.size.height*.25);drag(a,b);DYReadUniforms(host,kCMTimeZero,&edited);
run.record(@"canvas-hsl-independent-uuid-does-not-edit-base",edited.hslHueA[2]==100&&fabs([DYToolWithID(DYReadTools(host,kCMTimeZero).document,layerID)[@"values"][@"hsl0_2"]doubleValue]-50)<1e-5);
[(id)panel setValue:@"spider" forKey:@"toolType"];publish();plot=[area(@"spiderPlot")[@"rect"]rectValue];auto mesh=dy::neutralMesh();int selected=dy::meshIndex(mesh,0,2);a=NSMakePoint(plot.origin.x+mesh.x[selected]*plot.size.width,plot.origin.y+mesh.y[selected]*plot.size.height);b=NSMakePoint(a.x+9,a.y-6);undo=base->_undoStarts;drag(a,b);
NSData *saved=DYReadAssets(host,kCMTimeZero).spiderMesh;auto changed=DYSpiderMesh(saved);run.record(@"canvas-spider-drags-real-mesh-and-closes-one-undo",saved.length&&fabs(changed.x[selected]-mesh.x[selected])>.001&&base->_undoStarts==undo+1&&base->_undoEnds==undo+1);
doc=DYMutablePlist(DYReadTools(host,kCMTimeZero).document);NSString *spiderID=DYInsertTool(doc,@"spider",@"base:spider")[@"id"];DYWriteTools(host,doc,kCMTimeZero);publish();[(id)panel setValue:spiderID forKey:@"nodeID"];publish();plot=[area(@"spiderPlot")[@"rect"]rectValue];a=NSMakePoint(plot.origin.x+mesh.x[selected]*plot.size.width,plot.origin.y+mesh.y[selected]*plot.size.height);b=NSMakePoint(a.x-8,a.y+5);drag(a,b);
run.record(@"canvas-spider-uuid-persists-mesh-without-touching-base",[saved isEqual:DYReadAssets(host,kCMTimeZero).spiderMesh]&&[DYToolWithID(DYReadTools(host,kCMTimeZero).document,spiderID)[@"spider"]length]>0);
NSData *current=DYToolWithID(DYReadTools(host,kCMTimeZero).document,spiderID)[@"spider"];[bridge beginSpider:current node:spiderID enabled:1 atTime:kCMTimeZero];drain();doc=DYMutablePlist(DYReadTools(host,kCMTimeZero).document);auto external=dy::neutralMesh(12,12);NSData *externalData=[NSData dataWithBytes:&external length:sizeof(external)];((NSMutableDictionary*)DYToolWithID(doc,spiderID))[@"spider"]=externalData;DYWriteTools(host,doc,kCMTimeZero);[bridge changeSpider:current];[bridge endGesture];drain();
run.record(@"canvas-spider-external-topology-change-wins",[DYToolWithID(DYReadTools(host,kCMTimeZero).document,spiderID)[@"spider"]isEqual:externalData]);
[panel finishCapture];panel.bridge=nil;[bridge invalidate];drain();[(id)panel release];[(id)bridge release];
}
{
const NSUInteger w=64,h=48;DYDiagnosticTile *src=[[[DYDiagnosticTile alloc]initWithDevice:gpu width:w height:h]autorelease],*dst=[[[DYDiagnosticTile alloc]initWithDevice:gpu width:w height:h]autorelease];Pixels grid(w*h*4);
for(NSUInteger y=0;y<h;y++)for(NSUInteger x=0;x<w;x++){NSUInteger i=(y*w+x)*4;grid[i]=x/double(w-1)*.5;grid[i+1]=y/double(h-1)*.5;grid[i+2]=.1;grid[i+3]=1;}
[[src metalTextureForDevice:gpu]replaceRegion:MTLRegionMake2D(0,0,w,h)mipmapLevel:0 withBytes:grid.data()bytesPerRow:w*8];DYRenderer *lens=[[[DYRenderer alloc]init]autorelease];DYUniforms u=DYToolUniforms(@{@"type":@"lens"});
Pixels identity=render(run,lens,src,dst,gpu,u,@{},@"lens-neutral-production-render");run.record(@"lens-neutral-is-identity-with-existing-prefix",difference(identity,grid).maximum<.002&&offsetof(DYUniforms,lensSectionEnabled)==1312);
u.lensStrength=80;Pixels positive=render(run,lens,src,dst,gpu,u,@{},@"lens-positive-production-render");u.lensStrength=-80;Pixels negative=render(run,lens,src,dst,gpu,u,@{},@"lens-negative-production-render");NSUInteger sample=(h/2*w+w*3/4)*4;
run.record(@"lens-directions-move-source-in-opposite-directions",positive[sample]>identity[sample]&&negative[sample]<identity[sample]&&difference(positive,negative).maximum>.01);
BOOL finite=YES,alpha=YES;for(NSUInteger i=0;i<w*h;i++){for(int c=0;c<4;c++)finite&=std::isfinite((float)positive[i*4+c])&&std::isfinite((float)negative[i*4+c]);alpha&=positive[i*4+3]>=0&&positive[i*4+3]<=1;}
run.record(@"lens-keeps-finite-premultiplied-border-alpha",finite&&alpha&&positive[3]<1);
double worst=0;for(NSUInteger y=4;y<h-4;y+=5)for(NSUInteger x=4;x<w-4;x+=7){double aspect=w/double(h),qx=(x+.5)/w-.5,qy=(y+.5)/h-.5,r2=(qx*qx*aspect*aspect+qy*qy)/(.25*(aspect*aspect+1)),sx=.5+qx*(1-.2*r2),sy=.5+qy*(1-.2*r2);double expectedX=(sx*w-.5)/(w-1)*.5,expectedY=(sy*h-.5)/(h-1)*.5;worst=MAX(worst,fabs((float)negative[(y*w+x)*4]-expectedX));worst=MAX(worst,fabs((float)negative[(y*w+x)*4+1]-expectedY));}
run.record(@"lens-gpu-matches-independent-inverse-radial-oracle",worst<.003,@{@"maximumError":@(worst)});
NSURL *cube=[NSURL fileURLWithPath:[runtime stringByAppendingPathComponent:@"unsupported-lens.cube"]];NSError *failure=nil;run.record(@"lens-spatial-correction-is-refused-by-cube-export",![lens exportLUT:cube uniforms:u paths:@{}spider:nil tools:[[[DYToolDocument alloc]init]autorelease].document error:&failure]&&failure);
}
NSString *faceFixture=NSProcessInfo.processInfo.environment[@"DY_DIAGNOSTIC_FACE_FIXTURE"];
if(faceFixture.length){
NSBitmapImageRep *bitmap=[NSBitmapImageRep imageRepWithContentsOfFile:faceFixture];run.need(@"face-real-portrait-fixture-loaded",bitmap.pixelsWide==512&&bitmap.pixelsHigh==512);
const NSUInteger w=512,h=512;DYDiagnosticTile *src=[[[DYDiagnosticTile alloc]initWithDevice:gpu width:w height:h]autorelease],*dst=[[[DYDiagnosticTile alloc]initWithDevice:gpu width:w height:h]autorelease];Pixels portrait(w*h*4);
auto linear=[](double c){return c<=.04045?c/12.92:pow((c+.055)/1.055,2.4);};
for(NSUInteger y=0;y<h;y++)for(NSUInteger x=0;x<w;x++){NSColor *c=[[bitmap colorAtX:x y:y]colorUsingColorSpace:NSColorSpace.sRGBColorSpace];NSUInteger i=(y*w+x)*4;portrait[i]=linear(c.redComponent);portrait[i+1]=linear(c.greenComponent);portrait[i+2]=linear(c.blueComponent);portrait[i+3]=1;}
// Same hue as a cheek, deliberately outside the face, must remain unprotected.
for(NSUInteger y=400;y<480;y++)for(NSUInteger x=400;x<480;x++)for(int c=0;c<3;c++)portrait[(y*w+x)*4+c]=portrait[(115*w+220)*4+c];
auto upload=[&](const Pixels& pixels){[[src metalTextureForDevice:gpu]replaceRegion:MTLRegionMake2D(0,0,w,h)mipmapLevel:0 withBytes:pixels.data()bytesPerRow:w*8];};upload(portrait);
DYRenderer *face=[[[DYRenderer alloc]init]autorelease];DYUniforms u=DYToolUniforms(@{@"type":@"hsl"});u.hslHueA=(DYFloat4){40,40,40,40};u.hslHueB=(DYFloat4){40,40,40,40};u.skinProtection=(DYFloat3){1,100,28};u.skinFaceOnly=1;
Pixels protectedFrame=render(run,face,src,dst,gpu,u,@{},@"face-protected-production-render");NSData *entries=[face valueForKey:@"faceEntries"];NSInteger faces=0;const DYToolMaskEntry *values=(const DYToolMaskEntry*)entries.bytes;for(NSUInteger i=0;i<entries.length/sizeof(*values);i++)faces+=values[i].section==-100;
double faceX=0,faceY=0;for(NSUInteger i=0;i<entries.length/sizeof(*values);i++)if(values[i].section==-100){faceX=values[i].mask.parameters.centerX;faceY=values[i].mask.parameters.centerY;break;}run.record(@"face-vision-finds-real-portrait-and-builds-support",faces>=1,@{@"faces":@(faces),@"centerX":@(faceX),@"centerY":@(faceY)});u.skinProtection.x=0;Pixels unprotected=render(run,face,src,dst,gpu,u,@{},@"face-unprotected-reference-render");double inside=0,outside=0;
for(NSUInteger y=65;y<185;y++)for(NSUInteger x=175;x<275;x++)for(int c=0;c<3;c++)inside=MAX(inside,fabs((float)protectedFrame[(y*w+x)*4+c]-(float)unprotected[(y*w+x)*4+c]));for(NSUInteger y=410;y<470;y++)for(NSUInteger x=410;x<470;x++)for(int c=0;c<3;c++)outside=MAX(outside,fabs((float)protectedFrame[(y*w+x)*4+c]-(float)unprotected[(y*w+x)*4+c]));
run.record(@"face-protects-face-color-without-same-hue-background",inside>.005&&outside<.002,@{@"faceDifference":@(inside),@"backgroundDifference":@(outside)});
u.skinProtection.x=1;u.skinVisualize=1;Pixels selection=render(run,face,src,dst,gpu,u,@{},@"face-actual-selection-preview-render");double white=0,black=0;for(NSUInteger y=65;y<185;y++)for(NSUInteger x=175;x<275;x++)white=MAX(white,(float)selection[(y*w+x)*4]);for(NSUInteger y=410;y<470;y++)for(NSUInteger x=410;x<470;x++)black=MAX(black,(float)selection[(y*w+x)*4]);
double maximum=0;NSUInteger maxX=0,maxY=0;for(NSUInteger y=0;y<h;y++)for(NSUInteger x=0;x<w;x++)if(selection[(y*w+x)*4]>maximum){maximum=selection[(y*w+x)*4];maxX=x;maxY=y;}
run.record(@"face-preview-shows-actual-weight-and-excludes-background",white>.01&&black<.001,@{@"faceWeight":@(white),@"backgroundWeight":@(black),@"maximum":@(maximum),@"maxX":@(maxX),@"maxY":@(maxY)});
// Both FxPlug row conventions must resolve the same visual face.
Pixels flipped(portrait.size());for(NSUInteger y=0;y<h;y++)memcpy(flipped.data()+y*w*4,portrait.data()+(h-1-y)*w*4,w*8);upload(flipped);[src setDiagnosticImageOrigin:kFxImageOrigin_BOTTOM_LEFT];[dst setDiagnosticImageOrigin:kFxImageOrigin_BOTTOM_LEFT];u.skinVisualize=0;Pixels bottom=render(run,face,src,dst,gpu,u,@{},@"face-bottom-origin-production-render");double flipError=0;for(NSUInteger y=0;y<h;y++)for(NSUInteger x=0;x<w*4;x++)flipError=MAX(flipError,fabs((float)bottom[(h-1-y)*w*4+x]-(float)protectedFrame[y*w*4+x]));run.record(@"face-top-and-bottom-origins-protect-identical-visual-region",flipError<.003,@{@"maximumError":@(flipError)});[src setDiagnosticImageOrigin:kFxImageOrigin_TOP_LEFT];[dst setDiagnosticImageOrigin:kFxImageOrigin_TOP_LEFT];upload(portrait);
NSMutableDictionary *masked=DYMutablePlist([[[DYToolDocument alloc]init]autorelease].document);NSMutableDictionary *zero=DYMutablePlist(DYNewMask(DYMaskRectangle));DYMaskParameters zeroP=DYMaskAtTime(zero,kCMTimeZero);zeroP.strength=0;zero[@"parameters"]=[NSData dataWithBytes:&zeroP length:sizeof(zeroP)];masked[@"baseMasks"][@"skin"]=[NSMutableArray arrayWithObject:zero];u.skinVisualize=1;NSError *maskError=nil;BOOL maskOK=[face renderSource:src destination:dst uniforms:u paths:@{}spider:nil tools:masked error:&maskError];Pixels empty=read(dst,gpu);double emptyMax=0;for(NSUInteger i=0;i<w*h;i++)emptyMax=MAX(emptyMax,(float)empty[i*4]);run.record(@"face-preview-respects-authored-zero-strength-mask",maskOK&&emptyMax<.001);
u.skinVisualize=0;u.skinProtection.x=0;NSMutableDictionary *layers=DYMutablePlist([[[DYToolDocument alloc]init]autorelease].document);NSMutableDictionary *skin=(id)DYInsertTool(layers,@"skin",@"base:skin");skin[@"values"][@"skinStrength"]=@100;skin[@"values"][@"skinVisualize"]=@1;BOOL layerOK=[face renderSource:src destination:dst uniforms:u paths:@{}spider:nil tools:layers error:&maskError];Pixels layerMask=read(dst,gpu);double layerFace=0,layerBackground=0;for(NSUInteger y=65;y<185;y++)for(NSUInteger x=175;x<275;x++)layerFace=MAX(layerFace,(float)layerMask[(y*w+x)*4]);for(NSUInteger y=410;y<470;y++)for(NSUInteger x=410;x<470;x++)layerBackground=MAX(layerBackground,(float)layerMask[(y*w+x)*4]);run.record(@"face-independent-layer-uses-own-preview-and-face-mask",layerOK&&layerFace>.01&&layerBackground<.001);
DYDiagnosticTile *twoSource=[[[DYDiagnosticTile alloc]initWithDevice:gpu width:1024 height:512]autorelease],*twoDest=[[[DYDiagnosticTile alloc]initWithDevice:gpu width:1024 height:512]autorelease];Pixels two(1024*512*4);for(NSUInteger y=0;y<512;y++)for(NSUInteger x=0;x<1024;x++)memcpy(two.data()+(y*1024+x)*4,portrait.data()+(y*512+x%512)*4,8);[[twoSource metalTextureForDevice:gpu]replaceRegion:MTLRegionMake2D(0,0,1024,512)mipmapLevel:0 withBytes:two.data()bytesPerRow:1024*8];u.skinProtection.x=1;BOOL twoOK=[face renderSource:twoSource destination:twoDest uniforms:u paths:@{}error:&maskError];NSData *twoEntries=[face valueForKey:@"faceEntries"];const DYToolMaskEntry *twoMasks=(const DYToolMaskEntry*)twoEntries.bytes;NSInteger faceCount=0;for(NSUInteger i=0;i<twoEntries.length/sizeof(*twoMasks);i++)faceCount+=twoMasks[i].section==-100;run.record(@"face-multiple-people-build-independent-support-regions",twoOK&&faceCount==2,@{@"faces":@(faceCount)});
Pixels noFace(w*h*4);for(NSUInteger i=0;i<w*h;i++){noFace[i*4]=.35;noFace[i*4+1]=.18;noFace[i*4+2]=.1;noFace[i*4+3]=1;}upload(noFace);u.skinVisualize=0;Pixels noFaceProtected=render(run,face,src,dst,gpu,u,@{},@"face-none-protected-render");u.skinProtection.x=0;Pixels noFacePlain=render(run,face,src,dst,gpu,u,@{},@"face-none-unprotected-reference");run.record(@"face-no-detection-clears-prior-frame-and-fails-closed",difference(noFaceProtected,noFacePlain).maximum<.002);
}
// Verify the real Metal input stage with a moving target and a fixed source.
{
const NSUInteger w=64,h=48;
DYDiagnosticTile *src=[[[DYDiagnosticTile alloc]initWithDevice:gpu width:w height:h]autorelease];
DYDiagnosticTile *dst=[[[DYDiagnosticTile alloc]initWithDevice:gpu width:w height:h]autorelease];
Pixels input(w*h*4);
for(NSUInteger y=0;y<h;y++)for(NSUInteger x=0;x<w;x++){
NSUInteger i=(y*w+x)*4;
input[i]=x/128.f;input[i+1]=y/96.f;input[i+2]=.15f;input[i+3]=1;
}
[[src metalTextureForDevice:gpu]replaceRegion:MTLRegionMake2D(0,0,w,h) mipmapLevel:0 withBytes:input.data() bytesPerRow:w*8];
DYRenderer *renderer=[[[DYRenderer alloc]init]autorelease];
DYUniforms u=DYToolUniforms(@{@"type":@"lens"});
Pixels baseline=render(run,renderer,src,dst,gpu,u,@{},@"clone-stamp-reference-render");
NSMutableDictionary *doc=DYMutablePlist([[[DYToolDocument alloc]init]autorelease].document);
NSMutableDictionary *stamp=(id)DYAddSharedMask(doc,DYMaskEllipse);
DYMaskParameters p=DYMaskAtTime(stamp,kCMTimeZero);
p.centerX=48.5f/w;p.centerY=24.5f/h;p.width=.25f;p.height=.25f;
stamp[@"parameters"]=[NSData dataWithBytes:&p length:sizeof(p)];
stamp[@"stampSource"]=@[@(16.5f/w),@(12.5f/h),@.25,@.25];
CMTime next=CMTimeMake(1,25);DYMaskParameters moved=p;moved.centerY=34.5f/h;moved.width=.5f;moved.height=.12f;moved.angle=.65f;
run.record(@"clone-stamp-preserves-source-through-tracking-merge",DYMergeTracking(stamp,@[DYMaskKey(kCMTimeZero,p,NO,1),DYMaskKey(next,moved,NO,1)])&&[stamp[@"stampSource"] count]==4);
auto frame=[&](CMTime t,NSString *label){
NSError *failure=nil;
BOOL ok=[renderer renderSource:src destination:dst uniforms:u paths:@{} spider:nil tools:DYToolRenderSnapshot(doc,t) colorManagement:nil hostPrimaries:kFxColorPrimaries_Rec709 error:&failure];
run.need(label,ok,@{@"error":reason(failure)});return read(dst,gpu);
};
auto pixel=[&](const Pixels &a,NSUInteger x,NSUInteger y,const Pixels &b,NSUInteger u,NSUInteger v){
double delta=0;for(int c=0;c<4;c++)delta=MAX(delta,fabs((float)a[(y*w+x)*4+c]-(float)b[(v*w+u)*4+c]));return delta;
};
Pixels first=frame(kCMTimeZero,@"clone-stamp-production-render");
run.record(@"clone-stamp-copies-pixels-and-leaves-exterior-intact",pixel(first,48,24,baseline,16,12)<.002&&pixel(first,0,0,baseline,0,0)==0&&pixel(first,16,12,baseline,16,12)==0);
Pixels second=frame(next,@"clone-stamp-tracked-production-render");
run.record(@"clone-stamp-tracked-target-moves-while-sampling-the-same-source",pixel(second,48,34,baseline,16,12)<.002&&pixel(second,48,24,baseline,48,24)==0);
DYMaskParameters translated=DYMaskAtTime(stamp,next);
run.record(@"clone-stamp-ignores-legacy-tracking-scale-and-rotation",translated.angle==0&&translated.width==p.width&&translated.height==p.height&&translated.centerY==moved.centerY&&pixel(second,51,35,baseline,19,13)==0);
p.width=.5f;p.height=.35f;p.angle=.55f; // A legacy authored rotation must not rotate copied pixels.
stamp[@"parameters"]=[NSData dataWithBytes:&p length:sizeof(p)];
Pixels expanded=frame(kCMTimeZero,@"clone-stamp-resized-production-render");
run.record(@"clone-stamp-resize-extends-coverage-with-pixel-exact-translation",pixel(expanded,51,25,baseline,19,13)==0&&pixel(expanded,58,24,baseline,26,12)==0&&pixel(first,58,24,baseline,58,24)==0);
stamp[@"enabled"]=@NO;Pixels disabled=frame(next,@"clone-stamp-disabled-render");
run.record(@"clone-stamp-disabled-is-identical",difference(disabled,baseline).maximum==0);
stamp[@"enabled"]=@YES;NSError *failure=nil;
NSURL *cube=[NSURL fileURLWithPath:[runtime stringByAppendingPathComponent:@"unsupported-clone.cube"]];
run.record(@"clone-stamp-spatial-state-refuses-static-cube-export",![renderer exportLUT:cube uniforms:u paths:@{} spider:nil tools:doc error:&failure]&&failure);
}