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

52 行
12 KiB
C++

// Actual production entry points and independent CPU/profile reference vectors.
{
DYRenderer *cmsRenderer=[[[DYRenderer alloc]init]autorelease];DYToolDocument *empty=[[[DYToolDocument alloc]init]autorelease];
NSMutableDictionary *state=[[DYManagedColorDefaults()mutableCopy]autorelease];
run.need(@"cms-versioned-state-valid",DYManagedColorValid(state,nil)&&!DYManagedColorValid(@"bad",nil));
DYAssetState *saved=[[[DYAssetState alloc]init]autorelease];saved.colorManagement=state;NSData *archive=[NSKeyedArchiver archivedDataWithRootObject:saved requiringSecureCoding:YES error:&error];DYAssetState *restored=[NSKeyedUnarchiver unarchivedObjectOfClass:DYAssetState.class fromData:archive error:&error];
run.record(@"cms-secure-archive-independent-opt-in",[saved isEqual:restored]&&[restored.colorManagement isEqual:state]&&![[[DYAssetState alloc]init]autorelease].colorManagement);
NSMutableDictionary *invalid=[[state mutableCopy]autorelease];invalid[@"output"]=@999;run.record(@"cms-rejects-unknown-output",!DYManagedColorValid(invalid,nil));
DYUniforms u=defaults;u.skinProtection.x=0; // No grade is applied by default in the new working domain.
Pixels probe(width*height*4);float vectors[][4]={{.18,.18,.18,1},{2,1.2,.3,1},{-.03,.1,.2,1},{.03,.01,.005,.1},{0,0,0,0},{.7,.2,.05,1}};
for(size_t i=0;i<width*height;i++)for(int c=0;c<4;c++)probe[i*4+c]=vectors[i%6][c];
auto upload=[&](const Pixels&p){[[source metalTextureForDevice:gpu]replaceRegion:MTLRegionMake2D(0,0,width,height) mipmapLevel:0 withBytes:p.data() bytesPerRow:width*8];};upload(probe);
auto managed=[&](NSDictionary *s,DYUniforms v,NSDictionary *doc,NSString *name){NSError *e=nil;BOOL ok=[cmsRenderer renderSource:source destination:destination uniforms:v paths:@{} spider:nil tools:doc colorManagement:s hostPrimaries:kFxColorPrimaries_Rec709 error:&e];run.need(name,ok,@{@"error":reason(e)});return read(destination,gpu);};
Pixels old=render(run,cmsRenderer,source,destination,gpu,u,@{},@"cms-v1-reference");Pixels compatibility=managed(nil,u,empty.document,@"cms-v1-absent-state");run.record(@"cms-v1-absent-state-bit-exact",difference(old,compatibility).maximum==0);
Pixels host=managed(state,u,empty.document,@"cms-host-profile-neutral-render");run.record(@"cms-host-mode-preserves-grey-negative-superwhite-alpha",difference(probe,host).maximum<.004&&difference(probe,host).nonfinite==0,measure(difference(probe,host)));
NSMutableDictionary *layers=DYMutablePlist(empty.document);for(int i=0;i<4;i++)DYInsertTool(layers,@"primary",@"base:primary");Pixels repeated=managed(state,u,layers,@"cms-four-neutral-primary-layers");run.record(@"cms-neutral-primary-layers-do-not-remap",difference(host,repeated).maximum==0);
// All real physical profiles are interpreted, and src/dst keys cannot alias.
CGColorSpaceRef a=CGColorSpaceCreateWithName(kCGColorSpaceExtendedLinearSRGB),b=CGColorSpaceCreateWithName(kCGColorSpaceExtendedLinearITUR_2020);[source setDiagnosticColorSpace:a];[destination setDiagnosticColorSpace:b];Pixels physical=managed(state,u,empty.document,@"cms-709-source-2020-destination");
DYColorProfile *p=[[[DYColorProfile alloc]initWithSource:a target:b error:&error]autorelease];std::vector<DYCompositeRGBA>reference(width*height);for(size_t i=0;i<reference.size();i++)reference[i]={(float)probe[i*4],(float)probe[i*4+1],(float)probe[i*4+2],(float)probe[i*4+3]};auto converted=[p convertPixels:reference alpha:dycm::Alpha::Premultiplied budget:64*1024*1024 cancelled:nullptr error:&error];Pixels expected(probe.size());if(converted)for(size_t i=0;i<reference.size();i++){auto c=(*converted)[i];expected[i*4]=c.r;expected[i*4+1]=c.g;expected[i*4+2]=c.b;expected[i*4+3]=c.a;}
NSDictionary *context=[cmsRenderer colorContext];run.record(@"cms-physical-profile-matrix-and-context-key",converted&&difference(physical,expected).maximum<.004&&![context[@"sourceKey"]isEqual:context[@"destinationKey"]],measure(difference(physical,expected)));[source setDiagnosticColorSpace:nil];[destination setDiagnosticColorSpace:nil];CGColorSpaceRelease(a);CGColorSpaceRelease(b);
// Working grade has one exposure operation; a zero mask is an exact bypass.
DYUniforms grade=u;grade.cm.exposure=1;Pixels exposed=managed(state,grade,empty.document,@"cms-working-exposure");run.record(@"cms-one-stop-working-grade-changes-pixels-preserves-alpha",difference(host,exposed).different>20&&difference(exposed,exposed).nonfinite==0&&exposed[3]==host[3]);
NSMutableDictionary *mask=DYMutablePlist(DYNewMask(DYMaskRectangle));DYMaskParameters mp=DYMaskAtTime(mask,kCMTimeZero);mp.strength=0;mask[@"parameters"]=[NSData dataWithBytes:&mp length:sizeof(mp)];NSMutableDictionary *masked=DYMutablePlist(empty.document);masked[@"baseMasks"][@"primary"]=@[mask];Pixels zero=managed(state,grade,masked,@"cms-zero-mask");run.record(@"cms-zero-primary-mask-preserves-input",difference(zero,host).maximum==0);
// Frame orientation is normalized at the actual host destination boundary.
[destination setDiagnosticImageOrigin:kFxImageOrigin_BOTTOM_LEFT];Pixels flipped=managed(state,u,empty.document,@"cms-opposite-origin");BOOL orientation=YES;for(NSUInteger y=0;y<height;y++)for(NSUInteger x=0;x<width*4;x++)orientation&=fabs((float)flipped[y*width*4+x]-(float)host[(height-1-y)*width*4+x])<.004;run.record(@"cms-source-destination-origin-contract",orientation);[destination setDiagnosticImageOrigin:kFxImageOrigin_TOP_LEFT];
// Declared code values -> OCIO input -> exactly one ODT -> physical host output.
// CPU reference omits all renderer code; this catches repeated display mapping.
Pixels codes(width*height*4);for(size_t i=0;i<width*height;i++){float v=.04f+.8f*float(i%31)/30;for(int c=0;c<3;c++){float channel=i%7?v:(.05f+.7f*float((i*(3+c*4)+c*7)%31)/30);codes[i*4+c]=powf((channel+.055f)/1.055f,2.4f);}codes[i*4+3]=i%7?.5:1;for(int c=0;c<3;c++)codes[i*4+c]*=codes[i*4+3];}upload(codes);state[@"mode"]=@2;
auto cpuInput=[&](){std::vector<DYCompositeRGBA>r(width*height);for(size_t i=0;i<r.size();i++){float alpha=codes[i*4+3],c[3];for(int k=0;k<3;k++){float v=(float)codes[i*4+k]/alpha;c[k]=v<=.0031308f?v*12.92f:1.055f*powf(v,1/2.4f)-.055f;}r[i]={c[0]*alpha,c[1]*alpha,c[2]*alpha,alpha};}dycm::Transform::convert(dycm::Space([state[@"input"]unsignedIntValue]),dycm::Space::ACEScct).apply(r,dycm::Alpha::Premultiplied);return r;};
auto cpuOutput=[&](std::vector<DYCompositeRGBA>r,int output){dycm::Transform::display(dycm::Space::ACEScct,dycm::Output(output),true).apply(r,dycm::Alpha::Premultiplied);CGColorSpaceRef target=CGColorSpaceCreateWithName(kCGColorSpaceExtendedLinearSRGB);DYColorProfile *profile=[[[DYColorProfile alloc]initWithDisplayOutput:dycm::Output(output) target:target error:&error]autorelease];CGColorSpaceRelease(target);auto out=[profile convertPixels:r alpha:dycm::Alpha::Premultiplied budget:64*1024*1024 cancelled:nullptr error:&error];Pixels result(codes.size());if(out)for(size_t i=0;i<r.size();i++){auto c=(*out)[i];result[i*4]=c.r;result[i*4+1]=c.g;result[i*4+2]=c.b;result[i*4+3]=c.a;}return result;};
for(int output=1;output<=10;output++){@autoreleasepool{state[@"output"]=@(output);Pixels gpuFrame=managed(state,u,empty.document,[NSString stringWithFormat:@"cms-output-%d-render",output]);Pixels ref=cpuOutput(cpuInput(),output);Difference d=difference(ref,gpuFrame);run.record([NSString stringWithFormat:@"cms-output-%d-single-ODT-cpu-parity",output],d.maximum<.02&&d.nonfinite==0,measure(d));}}
state[@"output"]=@1;
for(NSDictionary *inputSpace in DYManagedInputSpaces()){@autoreleasepool{state[@"input"]=inputSpace[@"id"];Pixels gpuFrame=managed(state,u,empty.document,[NSString stringWithFormat:@"cms-input-%@-render",inputSpace[@"id"]]);Difference delta=difference(gpuFrame,cpuOutput(cpuInput(),1));run.record([NSString stringWithFormat:@"cms-input-%@-cpu-parity",inputSpace[@"id"]],delta.nonfinite==0&&delta.maximum<.01,measure(delta));}}state[@"input"]=@109;
state[@"output"]=@3;DYUniforms print=u;print.filmPrint.x=1;print.filmPrint.y=.6;print.film.mix=.8;Pixels base=managed(state,u,empty.document,@"cms-print-baseline-p3"),printed=managed(state,print,empty.document,@"cms-print-before-p3-output");
NSMutableDictionary *printDoc=DYMutablePlist(empty.document),*printNode=(id)DYInsertTool(printDoc,@"film",@"base:film");printNode[@"values"][@"filmPrintLook"]=@1;printNode[@"values"][@"filmPrintStrength"]=@.6;printNode[@"values"][@"filmMix"]=@.8;Pixels extraPrint=managed(state,u,printDoc,@"cms-independent-print-before-output");run.record(@"cms-root-and-extra-print-same-pre-output-result",difference(printed,extraPrint).maximum==0&&difference(printed,base).different>20);
// A CPU tetrahedral lookup in AP1/ACEScct, mixed in AP1 linear, then ODT.
std::vector<float>look;NSUInteger lookSide=0;NSString *lookPath=[[NSBundle bundleForClass:renderer.class]pathForResource:@"kodak_2383" ofType:@"dylut" inDirectory:@"PrintLooks"];
BOOL readLook=DYLUTReadCube(lookPath,look,lookSide,&error);auto scene=cpuInput();
if(readLook)for(auto &pixel:scene){if(!(pixel.a>0))continue;float c[3]={pixel.r/pixel.a,pixel.g/pixel.a,pixel.b/pixel.a},f[3];int cell[3],axis[3]={0,1,2};for(int k=0;k<3;k++){float x=fmaxf(0,fminf(1,c[k]))*(lookSide-1);cell[k]=MIN((int)floorf(x),(int)lookSide-2);f[k]=x-cell[k];}std::sort(axis,axis+3,[&](int x,int y){return f[x]>f[y];});
float weights[4]={1-f[axis[0]],f[axis[0]]-f[axis[1]],f[axis[1]]-f[axis[2]],f[axis[2]]},value[3]={};for(int j=0;j<4;j++){size_t offset=((cell[2]*lookSide+cell[1])*lookSide+cell[0])*4;for(int k=0;k<3;k++)value[k]+=look[offset+k]*weights[j];if(j<3)cell[axis[j]]++;}float out[3];for(int k=0;k<3;k++)out[k]=dyPrintCCTEncode(dyPrintCCTDecode(c[k])*.52f+dyPrintCCTDecode(value[k])*.48f)*pixel.a;pixel.r=out[0];pixel.g=out[1];pixel.b=out[2];}
Difference printDelta=difference(printed,cpuOutput(scene,3));run.record(@"cms-print-cpu-reference-before-ODT-linear-mix",readLook&&printDelta.maximum<.01&&printDelta.nonfinite==0,measure(printDelta));
state[@"output"]=@6;DYUniforms bounded=u;bounded.hslHueA.x=20;NSError *blocked=nil;BOOL hdr=[cmsRenderer renderSource:source destination:destination uniforms:bounded paths:@{} spider:nil tools:empty.document colorManagement:state hostPrimaries:kFxColorPrimaries_Rec709 error:&blocked];run.record(@"cms-hdr-refuses-unvalidated-bounded-finishing",!hdr&&blocked!=nil);
state[@"output"]=@1;NSURL *cube=[NSURL fileURLWithPath:[runtime stringByAppendingPathComponent:@"cms-v2.cube"]];error=nil;BOOL exportOK=[cmsRenderer exportLUT:cube uniforms:u paths:@{} spider:nil tools:empty.document colorManagement:state error:&error];std::vector<float>table;NSUInteger size=0;BOOL parse=exportOK&&DYLUTReadCube(cube.path,table,size,&error);run.record(@"cms-v2-cube-65-declared-output-contract",parse&&size==65,@{@"error":reason(error),@"size":@(size)});
// Static Cube interpolation at off-grid values must match the complete path.
Pixels direct=managed(state,u,empty.document,@"cms-cube-off-grid-direct-reference");double cubeError=0;
if(parse)for(size_t i=0;i<width*height;i++){float alpha=codes[i*4+3];float coord[3],f[3];int cell[3];for(int k=0;k<3;k++){float linear=(float)codes[i*4+k]/alpha;coord[k]=linear<=.0031308f?linear*12.92f:1.055f*powf(linear,1/2.4f)-.055f;float x=coord[k]*(size-1);cell[k]=MIN((int)floorf(x),(int)size-2);f[k]=x-cell[k];}for(int k=0;k<3;k++){float value=0;for(int z=0;z<2;z++)for(int y=0;y<2;y++)for(int x=0;x<2;x++){float weight=(x?f[0]:1-f[0])*(y?f[1]:1-f[1])*(z?f[2]:1-f[2]);value+=table[(((cell[2]+z)*size+cell[1]+y)*size+cell[0]+x)*4+k]*weight;}float linear=(float)direct[i*4+k]/alpha;float expected=linear<=.0031308f?linear*12.92f:1.055f*powf(linear,1/2.4f)-.055f;cubeError=MAX(cubeError,fabs(value-expected));}}
run.record(@"cms-cube-off-grid-production-error",parse&&cubeError<.015,@{@"maxCodeValueError":@(cubeError)});
run.record(@"cms-one-stop-grey-photometric-check",fabs((float)exposed[0]-.36f)<.005,@{@"linearGrey":@((float)exposed[0])});
run.record(@"cms-preview-still-available",cmsRenderer.preview.length==320*180*16);
upload(input);[source setDiagnosticImageOrigin:kFxImageOrigin_TOP_LEFT];[destination setDiagnosticImageOrigin:kFxImageOrigin_TOP_LEFT];
}