WIP: integrate film and RGB mixer tools; 346 native and 38 reference checks pass before formal acceptance

这个提交包含在:
mac 于 2026-11-02 13:50:21 +08:00 提交
1 父节点 a249436776
当前提交 f575c01893
25 个文件已更改
+1321 -22

没有与搜索条件相匹配的文件

+1 -1
查看文件
@@ -20,7 +20,7 @@ CONTENT_TAGS={'scenenode','layer','group','filter','behavior','mask'}
def sha(data):return hashlib.sha256(data).hexdigest()
def model_identity():
files=['大鱼调色/Plugin/DYModel.h','大鱼调色/Plugin/DYModel.mm','大鱼调色/Plugin/DYUniforms.h','大鱼调色/Plugin/DYColorManagement.h','大鱼调色/Plugin/DYCSTTypes.h','大鱼调色/Plugin/DYSpiderState.h','大鱼调色/Plugin/SpiderCore/DYColorMath.h','大鱼调色/Plugin/SpiderCore/DYSpiderMesh.hpp','template/tools/dump_model.mm','大鱼调色/Plugin/DYToolState.h','大鱼调色/Plugin/DYToolState.mm','大鱼调色/Plugin/MaskCore/DYMaskOperator.h','大鱼调色/Plugin/MaskCore/DYMask.h']
files=['大鱼调色/Plugin/DYModel.h','大鱼调色/Plugin/DYModel.mm','大鱼调色/Plugin/DYUniforms.h','大鱼调色/Plugin/DYColorManagement.h','大鱼调色/Plugin/DYCSTTypes.h','大鱼调色/Plugin/DYSpiderState.h','大鱼调色/Plugin/SpiderCore/DYColorMath.h','大鱼调色/Plugin/SpiderCore/DYSpiderMesh.hpp','template/tools/dump_model.mm','大鱼调色/Plugin/DYFilmTypes.h','大鱼调色/Plugin/DYFilmMath.h','大鱼调色/Plugin/DYToolState.h','大鱼调色/Plugin/DYToolState.mm','大鱼调色/Plugin/MaskCore/DYMaskOperator.h','大鱼调色/Plugin/MaskCore/DYMask.h']
return {name:sha((ROOT/name).read_bytes()) for name in files}
def refresh_metadata():
+8 -2
查看文件
@@ -10,13 +10,19 @@ int main(int argc,const char **argv){@autoreleasepool{
void *handle=dlopen(path.fileSystemRepresentation,RTLD_NOW|RTLD_LOCAL);
if(!handle){fprintf(stderr,"%s\n",dlerror());return 1;}
NSMutableDictionary *out=[NSMutableDictionary dictionary];
for(NSString *name in @[@"FTChannelMixerFilter",@"FTFilmSaturationFilter",@"FTHalationFilter",@"FTBloomFilter",@"FTGrainFilter",@"FTGrainProcessor"]){
for(NSString *name in @[@"FTChannelMixerFilter",@"FTFilmSaturationFilter",@"FTHalationFilter",@"FTBloomFilter",@"FTGrainFilter",@"FTGrainProcessor",@"FTNoiseGeneratorSimple"]){
Class cls=NSClassFromString(name);NSMutableDictionary *entry=[NSMutableDictionary dictionary];
for(int meta=0;meta<2;meta++){
unsigned count=0;Method *methods=class_copyMethodList(meta?object_getClass(cls):cls,&count);NSMutableArray *list=[NSMutableArray array];
for(unsigned i=0;i<count;i++)[list addObject:@{@"selector":NSStringFromSelector(method_getName(methods[i])),@"encoding":@(method_getTypeEncoding(methods[i]))}];free(methods);entry[meta?@"class":@"instance"]=list;
}
if([cls isSubclassOfClass:CIFilter.class]){@try{CIFilter *filter=[[cls alloc]init];[filter setDefaults];entry[@"attributes"]=filter.attributes?:@{};entry[@"inputKeys"]=filter.inputKeys?:@[];}@catch(NSException *e){entry[@"exception"]=e.reason;}}
if([cls isSubclassOfClass:CIFilter.class]){@try{CIFilter *filter=[[cls alloc]init];[filter setDefaults];entry[@"attributes"]=filter.attributes?:@{};entry[@"inputKeys"]=filter.inputKeys?:@[];
NSMutableDictionary *values=[NSMutableDictionary dictionary];
for(NSString *key in @[@"gammaAll",@"gammaCyan",@"gammaMagenta",@"gammaYellow",@"density",@"densitySwitch",@"valueSwitch",@"scatter",@"dyeTransmission",@"boost",@"sensitivity",@"blurRadius",@"intensity",@"grainSize",@"filmResponse",@"correlation",@"stdRed",@"stdGreen",@"stdBlue",@"seed"]){@try{id value=[filter valueForKey:key];if([value isKindOfClass:NSNumber.class])values[key]=value;}@catch(NSException *e){}}
for(NSString *key in @[@"colorKernel",@"genericKernel"]){@try{id value=[filter valueForKey:key];if(value)values[key]=[value name]?:@"";}@catch(NSException *e){}}
entry[@"defaults"]=values;
}@catch(NSException *e){entry[@"exception"]=e.reason;}}
if([name isEqual:@"FTNoiseGeneratorSimple"]){id object=[[cls alloc]init];entry[@"defaults"]=@{@"radius":[object valueForKey:@"radius"],@"blurSize":[object valueForKey:@"blurSize"]};}
out[name]=entry;
}
NSData *json=[NSJSONSerialization dataWithJSONObject:out options:NSJSONWritingPrettyPrinted|NSJSONWritingSortedKeys error:nil];
+64
查看文件
@@ -0,0 +1,64 @@
// Executes the user's installed Color Finale only as a read-only test oracle.
// Product builds and product rendering never load these reference frameworks.
#import <Foundation/Foundation.h>
#import <CoreImage/CoreImage.h>
#import <Metal/Metal.h>
#import <MetalPerformanceShaders/MetalPerformanceShaders.h>
#include <dlfcn.h>
#include <vector>
#include <cmath>
#include "../大鱼调色/Plugin/DYFilmMath.h"
using Floats=std::vector<float>;
static id<MTLDevice> gpu;
static id<MTLCommandQueue> queue;
static CIContext *context;
static const unsigned W=64,H=48;
static void require(bool p,NSString *m){if(!p)@throw [NSException exceptionWithName:@"FilmReference" reason:m userInfo:nil];}
static float encode(float v){return v<=.0031308f?v*12.92f:1.055f*powf(fmaxf(0,v),1/2.4f)-.055f;}
static float decode(float v){return v<=.04045f?v/12.92f:powf(fmaxf(0,(v+.055f)/1.055f),2.4f);}
static id<MTLTexture> texture(const Floats& data={}){MTLTextureDescriptor *d=[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA32Float width:W height:H mipmapped:NO];d.storageMode=MTLStorageModeShared;d.usage=MTLTextureUsageShaderRead|MTLTextureUsageShaderWrite|MTLTextureUsageRenderTarget;id<MTLTexture>t=[gpu newTextureWithDescriptor:d];require(t!=nil,@"texture allocation");if(!data.empty())[t replaceRegion:MTLRegionMake2D(0,0,W,H) mipmapLevel:0 withBytes:data.data() bytesPerRow:W*16];return t;}
static Floats read(id<MTLTexture> t){Floats v(W*H*4);[t getBytes:v.data() bytesPerRow:W*16 fromRegion:MTLRegionMake2D(0,0,W,H) mipmapLevel:0];return v;}
static void finish(id<MTLCommandBuffer> c){[c commit];[c waitUntilCompleted];require(c.status==MTLCommandBufferStatusCompleted,c.error.description?:@"GPU command");}
static CIImage *image(id<MTLTexture> t){return [CIImage imageWithMTLTexture:t options:@{kCIImageColorSpace:NSNull.null}];}
static Floats render(CIImage *ci){require(ci!=nil,@"reference output image");Floats v(W*H*4);[context render:ci toBitmap:v.data() rowBytes:W*16 bounds:CGRectMake(0,0,W,H) format:kCIFormatRGBAf colorSpace:nil];Floats top(v.size());for(unsigned y=0;y<H;y++)memcpy(top.data()+y*W*4,v.data()+(H-1-y)*W*4,W*16);return top;}
static id<MTLComputePipelineState> pipeline(id<MTLLibrary> library,NSString *name){NSError *error=nil;id<MTLFunction> f=[library newFunctionWithName:name];require(f!=nil,name);id<MTLComputePipelineState>p=[gpu newComputePipelineStateWithFunction:f error:&error];require(p!=nil,error.description);return p;}
static void dispatch(id<MTLComputeCommandEncoder> e){[e dispatchThreads:MTLSizeMake(W,H,1) threadsPerThreadgroup:MTLSizeMake(16,16,1)];[e endEncoding];}
static Floats point(id<MTLLibrary> library,id<MTLTexture> src,id<MTLTexture> aux,DYUniforms u,int operation){id<MTLTexture>out=texture();id<MTLCommandBuffer>c=[queue commandBuffer];id<MTLComputeCommandEncoder>e=[c computeCommandEncoder];[e setComputePipelineState:pipeline(library,@"dyFilmPoint")];[e setTexture:src atIndex:0];[e setTexture:aux?:src atIndex:1];[e setTexture:out atIndex:2];[e setBytes:&u length:sizeof(u) atIndex:0];struct {int operation;float radius;unsigned frame,reserved;}p={operation,0,u.filmFrameKey,0};[e setBytes:&p length:sizeof(p) atIndex:1];dispatch(e);finish(c);return read(out);}
static NSDictionary *difference(const Floats&a,const Floats&b){double maximum=0,sum=0;unsigned count=0,nonfinite=0,different=0;for(size_t i=0;i<a.size();i++){if(i%4==3)continue;if(a[(i/4)*4+3]<=0)continue;double d=fabs(a[i]-b[i]);if(!std::isfinite(d))nonfinite++;else{maximum=fmax(maximum,d);sum+=d*d;count++;if(d>1e-5)different++;}}return @{@"maximum":@(maximum),@"rms":@(sqrt(sum/fmax(1,count))),@"nonfinite":@(nonfinite),@"different":@(different)};}
int main(int argc,const char **argv){@autoreleasepool{@try{
require(argc==2,@"Usage: verify_film_reference /absolute/path/default.metallib");gpu=MTLCreateSystemDefaultDevice();queue=[gpu newCommandQueue];context=[CIContext contextWithMTLDevice:gpu options:@{kCIContextWorkingColorSpace:NSNull.null,kCIContextOutputColorSpace:NSNull.null}];NSError *error=nil;
id<MTLLibrary> dayu=[gpu newLibraryWithURL:[NSURL fileURLWithPath:@(argv[1])] error:&error];require(dayu!=nil,error.description);
NSString *framework=@"/Applications/ColorFinale.app/Contents/PlugIns/ColorFinale.pluginkit/Contents/Frameworks/RenderingEngine.framework/Versions/A";
require(dlopen([[framework stringByAppendingPathComponent:@"RenderingEngine"]fileSystemRepresentation],RTLD_NOW|RTLD_LOCAL)!=nullptr,@"load installed reference framework");
id<MTLLibrary> original=[gpu newLibraryWithURL:[NSURL fileURLWithPath:[framework stringByAppendingPathComponent:@"Resources/metal_kernels.metallib"]] error:&error];require(original!=nil,error.description);
Floats linear(W*H*4),encoded(linear.size());for(unsigned y=0;y<H;y++)for(unsigned x=0;x<W;x++){unsigned i=(y*W+x)*4;float a=(x%11==0)?0:(x%3==0?.25f:1);for(int k=0;k<3;k++){float c=float((x*17+y*23+k*71)%257)/256;if(x%17==0)c=k==0?1.3f:k==1?-.02f:c;linear[i+k]=float((__fp16)(decode(c)*a));encoded[i+k]=encode(a>0?linear[i+k]/a:0)*a;}linear[i+3]=encoded[i+3]=a;}
id<MTLTexture>source=texture(linear),codes=texture(encoded);Floats probe=render(image(source));require([difference(linear,probe)[@"maximum"]doubleValue]==0,@"CI bitmap orientation control");id<MTLTexture>roundtrip=texture();id<MTLCommandBuffer>roundtripCommands=[queue commandBuffer];[context render:image(source) toMTLTexture:roundtrip commandBuffer:roundtripCommands bounds:CGRectMake(0,0,W,H) colorSpace:nil];finish(roundtripCommands);require([difference(linear,read(roundtrip))[@"maximum"]doubleValue]==0,@"CI texture orientation control");NSMutableArray *rows=[NSMutableArray array];bool passed=true;
auto check=[&](NSString*name,const Floats&a,const Floats&b,double tolerance){NSDictionary *d=difference(a,b);BOOL ok=[d[@"nonfinite"]intValue]==0&&[d[@"maximum"]doubleValue]<tolerance;NSMutableDictionary *r=[d mutableCopy];r[@"name"]=name;r[@"pass"]=@(ok);r[@"tolerance"]=@(tolerance);[rows addObject:r];passed&=ok;};
DYUniforms u={};u.mixerMix=1;u.mixerRed=(DYFloat4){.75,.3,-.05,0};u.mixerGreen=(DYFloat4){.15,.85,0,0};u.mixerBlue=(DYFloat4){.05,-.1,1.05,0};u.film.gammaAll=u.film.gammaCyan=u.film.gammaMagenta=u.film.gammaYellow=1;u.film.valueMode=6;
for(int options=0;options<4;options++){
u.mixerPreserveLuma=options&1;u.mixerPreserveSaturation=(options>>1)&1;CIFilter *f=[[NSClassFromString(@"FTChannelMixerFilter")alloc]init];[f setDefaults];[f setValue:image(codes) forKey:kCIInputImageKey];[f setValue:[CIVector vectorWithX:u.mixerRed.x Y:u.mixerRed.y Z:u.mixerRed.z]forKey:@"inputRVector"];[f setValue:[CIVector vectorWithX:u.mixerGreen.x Y:u.mixerGreen.y Z:u.mixerGreen.z]forKey:@"inputGVector"];[f setValue:[CIVector vectorWithX:u.mixerBlue.x Y:u.mixerBlue.y Z:u.mixerBlue.z]forKey:@"inputBVector"];[f setValue:@(u.mixerPreserveLuma>0) forKey:@"inputPreserveLuma"];[f setValue:@(u.mixerPreserveSaturation>0)forKey:@"inputPreserveSaturation"];
Floats expected=render(f.outputImage);for(size_t i=0;i<expected.size()/4;i++)for(int k=0;k<3;k++){float a=expected[i*4+3];expected[i*4+k]=a>0?decode(expected[i*4+k]/a)*a:0;}check([NSString stringWithFormat:@"original-mixer-options-%d",options],expected,point(dayu,source,nil,u,0),.00015);
}
for(int mode=1;mode<=2;mode++)for(float density:{-.4f,0.f,.65f})for(float gamma:{.75f,1.3f}){
u.film.densityMode=mode;u.film.density=density;u.film.gammaAll=gamma;u.film.gammaCyan=1.2;u.film.gammaMagenta=.9;u.film.gammaYellow=1.1;CIFilter *f=[[NSClassFromString(@"FTFilmSaturationFilter")alloc]init];[f setDefaults];[f setValue:image(codes)forKey:kCIInputImageKey];[f setValue:@(gamma)forKey:@"gammaAll"];[f setValue:@1.2 forKey:@"gammaCyan"];[f setValue:@.9 forKey:@"gammaMagenta"];[f setValue:@1.1 forKey:@"gammaYellow"];[f setValue:@(density)forKey:@"density"];[f setValue:@(mode)forKey:@"densitySwitch"];
Floats expected=render(f.outputImage);for(size_t i=0;i<expected.size()/4;i++)for(int k=0;k<3;k++){float a=expected[i*4+3];expected[i*4+k]=a>0?decode(expected[i*4+k]/a)*a:0;}check([NSString stringWithFormat:@"original-subtractive-%d-%.2f-%.2f",mode,density,gamma],expected,point(dayu,source,nil,u,1),.00012);
}
for(float scatter:{1.f,5.f,20.f}){
u.film.scatter=scatter;u.film.boost=1.7;u.film.dyeTransmission=70;CIFilter *f=[[NSClassFromString(@"FTHalationFilter")alloc]init];[f setDefaults];[f setValue:image(source)forKey:kCIInputImageKey];[f setValue:@(scatter)forKey:@"scatter"];[f setValue:@1.7 forKey:@"boost"];[f setValue:@(70.f/500)forKey:@"dyeTransmission"];
CIImage *blur=[[[image(source)imageByClampingToExtent]imageByApplyingFilter:@"CIGaussianBlur" withInputParameters:@{kCIInputRadiusKey:@(scatter)}]imageByCroppingToRect:CGRectMake(0,0,W,H)];id<MTLTexture>aux=texture(render(blur));check([NSString stringWithFormat:@"original-halation-radius-%.0f",scatter],render(f.outputImage),point(dayu,source,aux,u,2),.00012);
}
for(float sensitivity:{20.f,50.f,80.f}){
DYFilmSettings settings={};settings.sensitivity=sensitivity;settings.intensity=70;
CIFilter *filter=[[NSClassFromString(@"FTBloomFilter")alloc]init];[filter setValue:image(source)forKey:kCIInputImageKey];[filter setValue:@2.5 forKey:@"blurRadius"];[filter setValue:@(sensitivity/100)forKey:@"sensitivity"];[filter setValue:@.7 forKey:@"intensity"];
id<MTLTexture>horizontal=texture(),vertical=texture(),edges=texture();id<MTLCommandBuffer>commands=[queue commandBuffer];
for(int direction=0;direction<2;direction++){id<MTLComputeCommandEncoder>e=[commands computeCommandEncoder];[e setComputePipelineState:pipeline(dayu,@"dyFilmSparseBlur")];[e setTexture:direction?horizontal:source atIndex:0];[e setTexture:direction?vertical:horizontal atIndex:1];[e setBytes:&direction length:4 atIndex:0];dispatch(e);}
id<MTLComputeCommandEncoder>e=[commands computeCommandEncoder];[e setComputePipelineState:pipeline(dayu,@"dyFilmBloomEdges")];[e setTexture:vertical atIndex:0];[e setTexture:source atIndex:1];[e setTexture:edges atIndex:2];[e setBytes:&settings length:sizeof(settings) atIndex:0];dispatch(e);finish(commands);
Floats actual=read(edges),expected=render(filter.outputImage);unsigned inside=0,border=0;for(unsigned y=0;y<H;y++)for(unsigned x=0;x<W;x++)if(expected[(y*W+x)*4+3]>0&&fabs(actual[(y*W+x)*4]-expected[(y*W+x)*4])>.01){if(x>=8&&x<W-8&&y>=8&&y<H-8)inside++;else border++;}fprintf(stderr,"bloom %.0f mismatches interior=%u border=%u\n",sensitivity,inside,border);check([NSString stringWithFormat:@"original-bloom-edge-sensitivity-%.0f",sensitivity],expected,actual,.00001);
}
for(unsigned seed:{0u,1u,7919u,0xffffffffu}){
auto noise=[&](id<MTLLibrary>lib,NSString *name){id<MTLTexture>out=texture();id<MTLCommandBuffer>c=[queue commandBuffer];id<MTLComputeCommandEncoder>e=[c computeCommandEncoder];[e setComputePipelineState:pipeline(lib,name)];[e setTexture:out atIndex:0];[e setBytes:&seed length:4 atIndex:0];dispatch(e);finish(c);return out;};
id<MTLTexture>ref=noise(original,@"BoxMullerPCG"),ours=noise(dayu,@"dyFilmNoise");check([NSString stringWithFormat:@"original-pcg-box-muller-%u",seed],read(ref),read(ours),.00002);
for(float color:{0.f,.35f,1.f}){u.film.grainAmount=.5;u.film.grainSize=1;u.film.grainResponse=.4;u.film.grainColor=color;float corr=1-2*color,other=1-corr,norm=sqrtf(corr*corr+other*other);float params[9]={.4f,.6f,corr/norm,other/norm,corr/norm,other/norm,.025f,.025f,.025f};id<MTLTexture>referenceOutput=texture();id<MTLCommandBuffer>commands=[queue commandBuffer];id<MTLComputeCommandEncoder>encoder=[commands computeCommandEncoder];[encoder setComputePipelineState:pipeline(original,@"GrainFilter")];[encoder setTexture:codes atIndex:0];[encoder setTexture:ref atIndex:1];[encoder setTexture:referenceOutput atIndex:2];[encoder setBytes:params length:sizeof(params) atIndex:0];dispatch(encoder);finish(commands);Floats expected=read(referenceOutput);for(size_t i=0;i<expected.size()/4;i++)for(int k=0;k<3;k++){float a=expected[i*4+3];expected[i*4+k]=a>0?decode(expected[i*4+k]/a)*a:0;}check([NSString stringWithFormat:@"original-grain-%u-%.2f",seed,color],expected,point(dayu,source,ours,u,4),.0002);}
}
NSDictionary *result=@{@"pass":@(passed),@"checks":@(rows.count),@"gpu":gpu.name,@"referenceVersion":@"ColorFinale 2.16.2 (625)",@"scope":@"Independent installed-reference CI/Metal output versus authored production Metal point kernels; source grayscale/color/HDR/negative and fractional-alpha vectors; reference frame seed is explicit.",@"results":rows};NSData *json=[NSJSONSerialization dataWithJSONObject:result options:NSJSONWritingPrettyPrinted|NSJSONWritingSortedKeys error:nil];fwrite(json.bytes,1,json.length,stdout);puts("");return passed?0:1;
}@catch(NSException *e){fprintf(stderr,"%s\n",e.reason.UTF8String);return 2;}}}