文件

54 行
13 KiB
C

// Nontrivial authoring and GPU regressions; runs as part of the signed CLI self-test.
{
DYToolDocument *empty=[[[DYToolDocument alloc]init]autorelease];NSMutableDictionary *doc=DYMutablePlist(empty.document);
NSMutableDictionary *first=DYMutablePlist(DYNewTool(@"hsl")),*second=DYMutablePlist(DYNewTool(@"hsl"));doc[@"layers"]=[NSMutableArray arrayWithObjects:first,second,nil];
run.need(@"tool-document-two-independent-hsl",DYValidToolDocument(doc)&&![first[@"id"]isEqual:second[@"id"]]);
first[@"values"][@"hsl0_0"]=@32;second[@"values"][@"hsl1_2"]=@55;
DYToolDocument *authored=[[[DYToolDocument alloc]init]autorelease];authored.document=doc;NSData *archive=[NSKeyedArchiver archivedDataWithRootObject:authored requiringSecureCoding:YES error:&error];DYToolDocument *restored=[NSKeyedUnarchiver unarchivedObjectOfClass:DYToolDocument.class fromData:archive error:&error];
run.record(@"tool-document-secure-archive-roundtrip",[restored isEqual:authored]&&restored.hash==authored.hash);
DYToolManager *one=[[[DYToolManager alloc]initWithHost:manager node:first]autorelease];DYWriteTools(manager,doc,kCMTimeZero);id<FxCustomParameterActionAPI_v4>action=(id)one;[action startAction:one];
[(id<FxParameterSettingAPI_v6>)one setFloatValue:17 toParameter:2068 atTime:kCMTimeZero];[(id<FxParameterSettingAPI_v6>)one setFloatValue:26 toParameter:2076 atTime:kCMTimeZero];[action endAction:one];NSDictionary *written=DYReadTools(manager,kCMTimeZero).document;
run.record(@"instance-proxy-batches-multiple-fields-without-clobber",[DYToolWithID(written,first[@"id"])[@"values"][@"hsl0_0"]isEqual:@17]&&[DYToolWithID(written,first[@"id"])[@"values"][@"hsl1_0"]isEqual:@26]&&[DYToolWithID(written,second[@"id"])[@"values"][@"hsl1_2"]isEqual:@55]);
// A cached action must see writes immediately, and a later action must see
// host-side undo/external edits. Exercise both the numeric snapshot and scope.
[action startAction:one];double before=0,after=0;
[(id<FxParameterRetrievalAPI_v6>)one getFloatValue:&before fromParameter:2068 atTime:kCMTimeZero];
[(id<FxParameterSettingAPI_v6>)one setFloatValue:19 toParameter:2068 atTime:kCMTimeZero];
[(id<FxParameterRetrievalAPI_v6>)one getFloatValue:&after fromParameter:2068 atTime:kCMTimeZero];
[action endAction:one];run.record(@"layer-snapshot-read-after-write-in-one-action",before==17&&after==19);
NSMutableDictionary *external=DYMutablePlist(DYReadTools(manager,kCMTimeZero).document);
((NSMutableDictionary*)DYToolWithID(external,first[@"id"]))[@"values"][@"hsl0_0"]=@23;
DYWriteTools(manager,external,kCMTimeZero);[action startAction:one];
[(id<FxParameterRetrievalAPI_v6>)one getFloatValue:&after fromParameter:2068 atTime:kCMTimeZero];[action endAction:one];
run.record(@"layer-snapshot-next-action-sees-external-edit",after==23);
DYWriteTools(manager,written,kCMTimeZero);
DYInspector *stackInspector=[[[DYInspector alloc]initWithAPIManager:manager renderer:renderer]autorelease];[stackInspector refresh];[stackInspector setFrameSize:NSMakeSize(360,780)];[stackInspector layoutSubtreeIfNeeded];NSDictionary *stackViews=[stackInspector valueForKey:@"_stackViews"];BOOL nativeViews=stackViews.count==2;for(NSString *ID in stackViews){NSArray *c=[stackViews[ID]valueForKey:@"_cards"];nativeViews&=c.count==1&&c[0][@"maskButton"]&&c[0][@"repeatButton"]&&[c[0][@"section"][@"key"]isEqual:@"hsl"];}run.record(@"two-native-hsl-panels-with-mask-and-repeat-buttons",nativeViews);
NSMutableDictionary *invalid=DYMutablePlist(doc);invalid[@"layers"][1][@"id"]=first[@"id"];run.record(@"tool-document-rejects-duplicate-identity",!DYValidToolDocument(invalid));invalid=DYMutablePlist(doc);invalid[@"layers"][0][@"values"][@"hsl0_0"]=@(NAN);run.record(@"tool-document-rejects-nonfinite",!DYValidToolDocument(invalid));
NSMutableDictionary *mask=DYMutablePlist(DYNewMask(DYMaskRectangle));DYMaskParameters p=DYMaskAtTime(mask,kCMTimeZero);p.feather=0;p.width=.5;p.height=.5;mask[@"parameters"]=[NSData dataWithBytes:&p length:sizeof(p)];CMTime a=CMTimeMake(1001,24000),b=CMTimeMake(2002,24000);DYMaskParameters moved=p;moved.centerX=.7;moved.width=.6;moved.angle=.2;mask[@"keys"]=@[DYMaskKey(a,p,YES,1),DYMaskKey(b,moved,NO,.9)];
DYMaskParameters mid=DYMaskAtTime(mask,CMTimeMake(3003,48000));run.record(@"mask-rational-time-interpolates-independent-geometry",fabs(mid.centerX-.6)<1e-6&&fabs(mid.width-.55)<1e-6&&fabs(mid.angle-.1)<1e-6);
DYMaskParameters replacement=p;replacement.centerX=.8;BOOL merged=DYMergeTracking(mask,@[DYMaskKey(a,replacement,NO,.8),DYMaskKey(b,replacement,NO,.8)]);run.record(@"tracking-merge-preserves-manual-keys",merged&&fabs(DYMaskAtTime(mask,a).centerX-.5)<1e-6&&fabs(DYMaskAtTime(mask,b).centerX-.8)<1e-6);
doc=DYMutablePlist(empty.document);doc[@"baseMasks"][@"hsl"]=[NSMutableArray arrayWithObject:mask];NSDictionary *snapshot=DYToolRenderSnapshot(doc,CMTimeMake(3003,48000));DYMaskParameters resolved=DYMaskAtTime(snapshot[@"baseMasks"][@"hsl"][0],kCMTimeZero);run.record(@"render-snapshot-freezes-time-without-author-keys",[snapshot[@"baseMasks"][@"hsl"][0][@"keys"]count]==0&&fabs(resolved.centerX-.65)<1e-6&&[mask[@"keys"]count]==2);
auto renderTools=[&](NSDictionary *tools,DYUniforms u,NSData *mesh,NSString *name){NSError *failure=nil;BOOL ok=[renderer renderSource:source destination:destination uniforms:u paths:@{} spider:mesh tools:tools error:&failure];run.need(name,ok,@{@"error":reason(failure)});return read(destination,gpu);};
[[source metalTextureForDevice:gpu]replaceRegion:MTLRegionMake2D(0,0,width,height) mipmapLevel:0 withBytes:input.data() bytesPerRow:width*8];
Pixels baseline=renderTools(empty.document,defaults,nil,@"empty-tool-document-render");run.record(@"empty-tool-document-preserves-legacy-pixels-exactly",difference(baseline,neutral).maximum==0);
DYUniforms newPrimary=DYToolUniforms(DYNewTool(@"primary")),oldPrimary=DYToolUniforms(@{@"type":@"primary",@"values":@{}});
run.record(@"new-primary-layer-neutral-old-explicit-mapping-preserved",newPrimary.cm.toneMapping==0&&oldPrimary.cm.toneMapping==0&&DYToolIsNeutral(DYNewTool(@"primary"))&&DYToolUniforms(@{@"type":@"primary",@"values":@{@"cmtoneMapping":@1}}).cm.toneMapping==1&&DYToolIsNeutral(@{@"type":@"primary",@"values":@{}}));
doc=DYMutablePlist(empty.document);for(NSString *type in DYToolTypes()){NSMutableDictionary *n=DYMutablePlist(DYNewTool(type));if([type isEqual:@"primary"])n[@"values"][@"cmtoneMapping"]=@0;[doc[@"layers"]addObject:n];}Pixels initialized=renderTools(doc,defaults,nil,@"all-initialized-tool-types-with-legacy-primary-mapping-render");run.record(@"legacy-neutral-repeated-tools-preserve-pixels",difference(initialized,neutral).maximum==0);
// Reference is two explicit isolated native operations, which catches shared
// uniform or lookup caches, ordering, repeated CST and accidental base replay.
DYUniforms layerOne=DYToolUniforms(first),layerTwo=DYToolUniforms(second);render(run,renderer,source,destination,gpu,layerOne,@{},@"hsl-stack-first-reference");DYDiagnosticTile *intermediate=[[[DYDiagnosticTile alloc]initWithDevice:gpu width:width height:height]autorelease];Pixels single=read(destination,gpu);[[intermediate metalTextureForDevice:gpu]replaceRegion:MTLRegionMake2D(0,0,width,height) mipmapLevel:0 withBytes:single.data() bytesPerRow:width*8];Pixels expected=render(run,renderer,intermediate,destination,gpu,layerTwo,@{},@"hsl-stack-second-reference");
doc=DYMutablePlist(empty.document);doc[@"layers"]=[NSMutableArray arrayWithObjects:first,second,nil];Pixels actual=renderTools(doc,defaults,nil,@"two-hsl-stack-render");run.record(@"two-hsl-stack-matches-independent-sequential-reference",difference(expected,actual).maximum<.003&&difference(actual,single).different>20,measure(difference(expected,actual)));
// A hard rectangle's exterior must match the bypassed tool and its interior
// must match the active tool; alpha cannot change on either side.
DYUniforms hu=defaults;hu.hslHueA.x=32;hu.skinProtection.x=0;Pixels unmasked=render(run,renderer,source,destination,gpu,hu,@{},@"mask-hsl-active-reference");DYUniforms off=hu;off.hslSectionEnabled=0;Pixels noHSL=render(run,renderer,source,destination,gpu,off,@{},@"mask-hsl-bypass-reference");
for(int kind=0;kind<6;kind++){@autoreleasepool{NSMutableDictionary *m=DYMutablePlist(DYNewMask(kind));DYMaskParameters params=DYMaskAtTime(m,kCMTimeZero);params.feather=0;params.width=.5;params.height=.5;params.angle=.21;if(kind==DYMaskLumaKey){params.inputBlack=.2;params.inputWhite=.8;}m[@"parameters"]=[NSData dataWithBytes:&params length:sizeof(params)];doc=DYMutablePlist(empty.document);doc[@"baseMasks"][@"hsl"]=[NSMutableArray arrayWithObject:m];Pixels pixels=renderTools(doc,hu,nil,[NSString stringWithFormat:@"mask-type-%d-gpu",kind]);BOOL finite=difference(pixels,pixels).nonfinite==0,alpha=YES;for(NSUInteger i=0;i<width*height;i++)alpha&=pixels[i*4+3]==input[i*4+3];run.record([NSString stringWithFormat:@"mask-type-%d-preserves-alpha-finite",kind],finite&&alpha);
DYMaskPath path=dyFlattenMask((const DYBezierNode*)[m[@"nodes"]bytes],[m[@"nodes"]length]/sizeof(DYBezierNode));params.imageAspect=float(width)/height;double worst=0;for(NSUInteger y=0;y<height;y++)for(NSUInteger x=0;x<width;x++){auto index=(y*width+x)*4;float a=input[index+3];if(!(a>0))continue;float3 code;auto encode=[](float v){return v<=.0031308f?v*12.92f:1.055f*powf(MAX(0.f,v),1/2.4f)-.055f;};for(int c=0;c<3;c++)code[c]=encode((float)(__fp16)((float)input[index+c]/a));float coverage=dyMaskOperator((x+.5f)/width,(y+.5f)/height,code,params,path);for(int c=0;c<3;c++){float before=encode((float)noHSL[index+c]/a),after=encode((float)unmasked[index+c]/a),mixed=before+(after-before)*coverage;float expected=(mixed<=.04045f?mixed/12.92f:powf(MAX(0.f,(mixed+.055f)/1.055f),2.4f))*a;worst=MAX(worst,fabs(expected-(float)pixels[index+c]));}}run.record([NSString stringWithFormat:@"mask-type-%d-independent-cpu-coverage",kind],worst<.004,@{@"maximumError":@(worst)});}}
mask=DYMutablePlist(DYNewMask(DYMaskRectangle));p=DYMaskAtTime(mask,kCMTimeZero);p.feather=0;p.width=p.height=.5;mask[@"parameters"]=[NSData dataWithBytes:&p length:sizeof(p)];doc=DYMutablePlist(empty.document);doc[@"baseMasks"][@"hsl"]=[NSMutableArray arrayWithObject:mask];actual=renderTools(doc,hu,nil,@"hsl-rectangle-isolated-render");BOOL region=YES;double maxRegion=0;for(NSUInteger y=0;y<height;y++)for(NSUInteger x=0;x<width;x++){BOOL inside=x>=width/4&&x<width*3/4&&y>=height/4&&y<height*3/4;for(int c=0;c<4;c++){auto index=(y*width+x)*4+c;double delta=fabs((float)actual[index]-(float)(inside?unmasked:noHSL)[index]);maxRegion=MAX(maxRegion,delta);region&=delta<.003;}}run.record(@"per-tool-mask-affects-only-own-region",region,@{@"maximumError":@(maxRegion)});
mask[@"enabled"]=@NO;actual=renderTools(doc,hu,nil,@"mask-eye-disabled-render");run.record(@"disabled-mask-restores-unmasked-tool",difference(actual,unmasked).maximum==0);
mask[@"enabled"]=@YES;p.width=p.height=4;mask[@"parameters"]=[NSData dataWithBytes:&p length:sizeof(p)];actual=renderTools(doc,hu,nil,@"all-one-mask-render");run.record(@"all-one-mask-preserves-native-operation",difference(actual,unmasked).maximum<.001);
p.strength=0;mask[@"parameters"]=[NSData dataWithBytes:&p length:sizeof(p)];actual=renderTools(doc,hu,nil,@"all-zero-mask-render");run.record(@"all-zero-mask-bypasses-only-own-tool",difference(actual,noHSL).maximum<.003,measure(difference(actual,noHSL)));
error=nil;NSURL *blocked=[NSURL fileURLWithPath:[runtime stringByAppendingPathComponent:@"masked-export-must-not-exist.cube"]];BOOL exported=[renderer exportLUT:blocked uniforms:hu paths:@{} spider:nil tools:doc error:&error];run.record(@"cube-export-refuses-spatial-mask-with-clear-error",!exported&&error&&![NSFileManager.defaultManager fileExistsAtPath:blocked.path]);
DYUniforms primary=defaults;primary.cm.exposure=.6;Pixels primaryFull=render(run,renderer,source,destination,gpu,primary,@{},@"primary-mask-full-reference");DYUniforms primaryOff=primary;primaryOff.primarySectionEnabled=0;Pixels primaryBypass=render(run,renderer,source,destination,gpu,primaryOff,@{},@"primary-mask-bypass-reference");mask=DYMutablePlist(DYNewMask(DYMaskRectangle));p=DYMaskAtTime(mask,kCMTimeZero);p.feather=0;p.width=p.height=.5;mask[@"parameters"]=[NSData dataWithBytes:&p length:sizeof(p)];doc=DYMutablePlist(empty.document);doc[@"baseMasks"][@"primary"]=[NSMutableArray arrayWithObject:mask];actual=renderTools(doc,primary,nil,@"primary-cst-mask-render");BOOL primaryRegion=YES;for(NSUInteger y=0;y<height;y++)for(NSUInteger x=0;x<width;x++)for(int c=0;c<4;c++){BOOL inside=x>=width/4&&x<width*3/4&&y>=height/4&&y<height*3/4;auto i=(y*width+x)*4+c;primaryRegion&=fabs((float)actual[i]-(float)(inside?primaryFull:primaryBypass)[i])<.003;}run.record(@"primary-mask-blends-cst-only-in-selected-region",primaryRegion);
DYWriteTools(manager,empty.document,kCMTimeZero);
}