// Independent, headless oracle check. Reference application is read only and // never used by production builds. See tools/kernels/README.md for scope. #import #import #include #include #include #include #include #include "../大鱼调色/Plugin/DYUniforms.h" static id gpu; static id queue; using Pixels = std::vector<__fp16>; static void need(bool valid, NSString* message) { if (!valid) @throw [NSException exceptionWithName:@"GPUVerification" reason:message userInfo:nil]; } static void finish(id commands) { [commands commit]; [commands waitUntilCompleted]; need(commands.status == MTLCommandBufferStatusCompleted, commands.error.description ?: @"GPU command failed"); } static NSString* digest(NSString* path) { NSData* data = [NSData dataWithContentsOfFile:path]; need(data != nil, path); unsigned char hash[CC_SHA256_DIGEST_LENGTH]; CC_SHA256(data.bytes, (CC_LONG)data.length, hash); NSMutableString* result = [NSMutableString string]; for (unsigned char byte : hash) [result appendFormat:@"%02x", byte]; return result; } static DYUniforms defaults() { DYUniforms u = {}; u.exposureEnabled=u.contrastEnabled=u.highlightsEnabled=u.shadowsEnabled=1; u.whitesEnabled=u.blacksEnabled=u.temperatureEnabled=u.tintEnabled=1; u.saturationEnabled=u.vibranceEnabled=u.textureEnabled=u.clarityEnabled=u.dehazeEnabled=1; u.inputSectionEnabled=u.baseSectionEnabled=u.colorSectionEnabled=u.lookSectionEnabled=1; u.extraLookSectionEnableds = (DYFloat4){1,1,1,1}; u.curveSectionEnabled = u.hslSectionEnabled = u.colorGradingSectionEnabled = 1; u.primariesSectionEnabled = u.levelsSectionEnabled = u.colorBalanceSectionEnabled = 1; u.levelsGamma = (DYFloat4){1,1,1,1}; u.levelsInputWhite = u.levelsOutputWhite = (DYFloat4){255,255,255,255}; u.curveX = (DYFloat4){.2f,.4f,.6f,.8f}; u.redCurveX = u.greenCurveX = u.blueCurveX = (DYFloat2){.25f,.75f}; u.curveExtraX = u.redCurveExtraX = u.greenCurveExtraX = u.blueCurveExtraX = (DYFloat4){-1,-1,-1,-1}; u.primariesM0 = (DYFloat3){1,0,0}; u.primariesM1 = (DYFloat3){0,1,0}; u.primariesM2 = (DYFloat3){0,0,1}; u.colorBalancePreserveLuminosity = 1; u.hslCustomRange = (DYFloat3){.08f,.4f,.4f}; return u; } static id image(const Pixels& values, NSUInteger width, NSUInteger height) { MTLTextureDescriptor* d = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA16Float width:width height:height mipmapped:NO]; d.storageMode = MTLStorageModeShared; d.usage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget | MTLTextureUsageShaderWrite; id result = [gpu newTextureWithDescriptor:d]; need(result != nil, @"source texture"); if (!values.empty()) [result replaceRegion:MTLRegionMake2D(0,0,width,height) mipmapLevel:0 withBytes:values.data() bytesPerRow:width*8]; return result; } static id lut(bool transformed) { const NSUInteger n = 17; MTLTextureDescriptor* d = [[MTLTextureDescriptor alloc] init]; d.textureType = MTLTextureType3D; d.pixelFormat = MTLPixelFormatRGBA32Float; d.width = d.height = d.depth = n; d.storageMode = MTLStorageModeShared; d.usage = MTLTextureUsageShaderRead; std::vector values(n*n*n*4); for (NSUInteger z=0; z result = [gpu newTextureWithDescriptor:d]; need(result != nil, @"LUT texture"); [result replaceRegion:MTLRegionMake3D(0,0,0,n,n,n) mipmapLevel:0 slice:0 withBytes:values.data() bytesPerRow:n*16 bytesPerImage:n*n*16]; return result; } static id renderPipeline(id library, bool dayu) { MTLRenderPipelineDescriptor* d = [[MTLRenderPipelineDescriptor alloc] init]; d.vertexFunction = [library newFunctionWithName:dayu ? @"dyVertex" : @"vertexShader"]; d.fragmentFunction = [library newFunctionWithName:dayu ? @"dyFragment" : @"fragmentShader"]; d.colorAttachments[0].pixelFormat = MTLPixelFormatRGBA16Float; NSError* error = nil; id p = [gpu newRenderPipelineStateWithDescriptor:d error:&error]; need(p != nil, error.description ?: @"render pipeline"); return p; } static Pixels render(id pipeline, bool dayu, id source, NSArray>* textures, const DYUniforms& u, NSUInteger width, NSUInteger height, float u0=0, float u1=1, bool premultiplied=false) { id destination=image({},width,height); MTLRenderPassDescriptor* pass=[MTLRenderPassDescriptor renderPassDescriptor]; pass.colorAttachments[0].texture=destination; pass.colorAttachments[0].loadAction=MTLLoadActionClear; pass.colorAttachments[0].storeAction=MTLStoreActionStore; id commands=[queue commandBuffer]; id e=[commands renderCommandEncoderWithDescriptor:pass]; [e setRenderPipelineState:pipeline]; float hw=width*.5f, hh=height*.5f; DYVertex2D vertices[]={{{hw,-hh},{u1,1}},{{-hw,-hh},{u0,1}},{{hw,hh},{u1,0}},{{-hw,hh},{u0,0}}}; vector_uint2 viewport={(unsigned)width,(unsigned)height}; [e setVertexBytes:vertices length:sizeof(vertices) atIndex:0]; [e setVertexBytes:&viewport length:sizeof(viewport) atIndex:1]; [e setFragmentTexture:source atIndex:0]; for (NSUInteger i=0;itolerance)++different; maxError=std::max(maxError,delta); } return @{@"name":name,@"components":@(compared),@"differentAboveTolerance":@(different),@"maxAbsoluteError":@(maxError),@"tolerance":@(tolerance),@"pass":@(different==0)}; } static NSDictionary* statistics(id reference,id dayu,id source,id identity,const DYUniforms& u) { float values[2]={}; id libraries[]={reference,dayu}; NSString* names[]={@"whitesSceneStatistics",@"dyWhitesSceneStatistics"}; for(int i=0;i<2;++i) { NSError* error=nil; id p=[gpu newComputePipelineStateWithFunction:[libraries[i] newFunctionWithName:names[i]] error:&error]; need(p!=nil,error.description); id result=[gpu newBufferWithLength:4 options:MTLResourceStorageModeShared]; id cb=[queue commandBuffer];id e=[cb computeCommandEncoder];[e setComputePipelineState:p]; [e setTexture:source atIndex:0];[e setTexture:identity atIndex:1];[e setBytes:&u length:sizeof(u) atIndex:0];[e setBuffer:result offset:0 atIndex:1]; [e dispatchThreadgroups:MTLSizeMake(1,1,1) threadsPerThreadgroup:MTLSizeMake(256,1,1)];[e endEncoding];finish(cb);memcpy(&values[i],result.contents,4); } return @{@"name":@"scene-statistics",@"reference":@(values[0]),@"dayu":@(values[1]),@"pass":@(memcmp(values,values+1,4)==0)}; } static NSDictionary* exportLUT(id reference,id dayu,NSArray>* textures,const DYUniforms& u) { const unsigned n=17; const size_t components=n*n*n*3;std::vector outputs[2]; id libraries[]={reference,dayu};NSString* names[]={@"lutExportKernel",@"dyLUTExportKernel"}; for(int i=0;i<2;++i) { NSError* error=nil; id p=[gpu newComputePipelineStateWithFunction:[libraries[i] newFunctionWithName:names[i]] error:&error];need(p!=nil,error.description); id result=[gpu newBufferWithLength:components*4 options:MTLResourceStorageModeShared]; id cb=[queue commandBuffer];id e=[cb computeCommandEncoder];[e setComputePipelineState:p]; for(NSUInteger t=0;t0)++differences;maxError=std::max(maxError,d);} return @{@"name":@"LUT-export-17",@"components":@(components),@"differentComponents":@(differences),@"maxAbsoluteError":@(maxError),@"pass":@(differences==0)}; } static Pixels unpremultiply(id library,id source) { NSError* error=nil; auto pipeline=[gpu newComputePipelineStateWithFunction:[library newFunctionWithName:@"dyUnpremultiply"] error:&error];need(pipeline!=nil,error.description); auto destination=image({},source.width,source.height); auto cb=[queue commandBuffer];auto e=[cb computeCommandEncoder];[e setComputePipelineState:pipeline]; [e setTexture:source atIndex:0];[e setTexture:destination atIndex:1]; [e dispatchThreads:MTLSizeMake(source.width,source.height,1) threadsPerThreadgroup:MTLSizeMake(16,16,1)];[e endEncoding];finish(cb); Pixels output(source.width*source.height*4);[destination getBytes:output.data() bytesPerRow:source.width*8 fromRegion:MTLRegionMake2D(0,0,source.width,source.height) mipmapLevel:0];return output; } static NSDictionary* preview(id library,bool analysis=false) { const size_t width=analysis?320:64,height=analysis?180:64; NSError* error=nil;auto pipeline=[gpu newComputePipelineStateWithFunction:[library newFunctionWithName:analysis?@"dyAnalysis":@"dyPreview"] error:&error];need(pipeline!=nil,error.description); size_t compared=0,different=0;double maxError=0; for(float level:{-.1f,0.f,.0031308f,.18f,1.f,2.f}) { Pixels input(8*8*4);for(size_t p=0;p2e-6)++different;maxError=std::max(maxError,d);} } return @{@"name":analysis?@"320x180-analysis-sRGB-alpha-HDR-negative":@"64x64-preview-sRGB-alpha-HDR-negative",@"components":@(compared),@"differentAboveTolerance":@(different),@"maxAbsoluteError":@(maxError),@"tolerance":@(2e-6),@"pass":@(different==0)}; } int main(int argc,const char* argv[]) { @autoreleasepool { @try { need(argc==4,@"Usage: verify_gpu /absolute/dayu.metallib /absolute/report.json /absolute/reference.metallib"); gpu=MTLCreateSystemDefaultDevice();need(gpu!=nil,@"Metal unavailable");queue=[gpu newCommandQueue]; NSString* original=[NSString stringWithUTF8String:argv[3]]; NSString* current=[NSString stringWithUTF8String:argv[1]];NSError* error=nil; id ref=[gpu newLibraryWithURL:[NSURL fileURLWithPath:original] error:&error];need(ref!=nil,error.description); id ours=[gpu newLibraryWithURL:[NSURL fileURLWithPath:current] error:&error];need(ours!=nil,error.description); auto rp=renderPipeline(ref,false), dp=renderPipeline(ours,true);auto identity=lut(false), altered=lut(true); NSArray* textures=@[identity,identity,identity,identity,identity,identity,identity,identity,identity]; const NSUInteger w=256,h=128;Pixels input(w*h*4);uint32_t rng=0x75423981; for(NSUInteger y=0;y>8)*(1.0f/16777215);if(y<32)v=float(x)/255;if(y>=96)v=v*2-.1f;input[p+c]=(__fp16)v;}input[p+3]=1;} auto source=image(input,w,h);NSMutableArray* cases=[NSMutableArray array]; auto runCase=[&](NSString* name,DYUniforms u,NSArray* tex){Pixels a=render(rp,false,source,tex,u,w,h),b=render(dp,true,source,tex,u,w,h);[cases addObject:compare(name,a,b)];}; DYUniforms u=defaults();runCase(@"neutral",u,textures); for(float v:{-5.f,-1.25f,.65f,5.f}){u=defaults();u.exposure=v;runCase([NSString stringWithFormat:@"exposure-%g",v],u,textures);} u=defaults();u.contrast=55;u.highlights=-48;u.shadows=62;u.whites=70;u.blacks=-65;runCase(@"tone-combined",u,textures); u=defaults();u.contrast=-65;u.highlights=72;u.shadows=-55;u.whites=-72;u.blacks=48;runCase(@"tone-opposite",u,textures); for(float v:{-65.f,65.f}){u=defaults();u.texture=v;runCase([NSString stringWithFormat:@"texture-%g",v],u,textures);u=defaults();u.clarity=v;runCase([NSString stringWithFormat:@"clarity-%g",v],u,textures);u=defaults();u.dehaze=v;runCase([NSString stringWithFormat:@"dehaze-%g",v],u,textures);} u=defaults();u.saturation=38;u.vibrance=72;runCase(@"saturation-vibrance",u,textures); u=defaults();u.curveShadows=-12;u.curveDarks=8;u.curveLights=15;u.curveHighlights=-8;u.redCurveShadows=13;u.greenCurveHighlights=-9;u.blueCurveShadows=5;u.curveExtraX=(DYFloat4){.1f,.3f,-1,-1};u.curveExtra=(DYFloat4){3,-6,0,0};runCase(@"master-RGB-extra-curves",u,textures); u=defaults();u.levelsInputBlack=(DYFloat4){5,8,3,12};u.levelsInputWhite=(DYFloat4){246,238,241,245};u.levelsGamma=(DYFloat4){1.1f,.87f,1.25f,.94f};u.levelsOutputBlack=(DYFloat4){2,3,0,4};u.levelsOutputWhite=(DYFloat4){250,248,240,252};runCase(@"master-RGB-levels",u,textures); u=defaults();u.hslCustomSample=(DYFloat3){.05f,.5f,.5f};u.hslCustomAdjust=(DYFloat3){25,-35,20};runCase(@"custom-HSL",u,textures);u.hslCustomVisualize=1;runCase(@"custom-HSL-visualization",u,textures); u=defaults();u.blackWhiteSectionEnabled=1;u.blackWhiteMixA=(DYFloat4){25,-15,40,-30};u.blackWhiteMixB=(DYFloat2){15,-10};u.blackWhiteTint=(DYFloat2){.13f,.2f};runCase(@"black-white-mixer-tint",u,textures); u=defaults();u.colorBalanceShadows=(DYFloat3){18,-12,6};u.colorBalanceMidtones=(DYFloat3){-9,14,-6};u.colorBalanceHighlights=(DYFloat3){8,2,-15};runCase(@"color-balance-preserve",u,textures);u.colorBalancePreserveLuminosity=0;runCase(@"color-balance",u,textures); u=defaults();u.redPrimaryHue=25;u.primariesM0=(DYFloat3){.9f,.08f,.02f};u.primariesM1=(DYFloat3){.04f,.92f,.04f};u.primariesM2=(DYFloat3){.02f,.06f,.92f};runCase(@"primary-matrix",u,textures); NSArray* lookTextures=@[altered,altered,altered,altered,altered,altered,identity,identity,identity]; u=defaults();u.preLUTStrength=.43f;u.postLUTStrength=.61f;u.extraPostLUTStrengths=(DYFloat4){.2f,.4f,.6f,.8f};runCase(@"pre-LUT-and-five-look-mixes",u,lookTextures); u.skinProtection=(DYFloat3){.65f,.4f,.2f};u.exposure=.7f;u.contrast=20;u.curveDarks=-5;u.texture=25;u.clarity=-15;u.saturation=10;runCase(@"mixed-chain-skin-protection",u,lookTextures); [cases addObject:statistics(ref,ours,source,identity,u)];[cases addObject:exportLUT(ref,ours,lookTextures,u)]; // Real cropped input tile with a 32 px halo, not a resized full-frame sample. Pixels crop(192*h*4);for(NSUInteger y=0;y