303 行
35 KiB
Plaintext
303 行
35 KiB
Plaintext
#import <os/log.h>
|
|
#import "DYManagedColor.h"
|
|
#import "../../ColorManagement/Sources/DYColorManagementGPU.h"
|
|
#import "../../ColorManagement/Sources/DYColorProfile.h"
|
|
#include <map>
|
|
#include <mutex>
|
|
#import "DYRenderer.h"
|
|
#import "../Shared/DYBetaDiagnostics.h"
|
|
#import "DYModel.h"
|
|
#include "DYSpiderState.h"
|
|
#include "DYToolGPU.h"
|
|
#include "DYFilmMath.h"
|
|
#import <CoreImage/CoreImage.h>
|
|
#import <Vision/Vision.h>
|
|
#import <ImageIO/ImageIO.h>
|
|
#import <MetalPerformanceShaders/MetalPerformanceShaders.h>
|
|
#import "DYColorTables.h"
|
|
#import "DYLUTLibrary.h"
|
|
#import <QuartzCore/QuartzCore.h>
|
|
#include <zlib.h>
|
|
#include <vector>
|
|
#include <cmath>
|
|
#include "DYScopeMath.hpp"
|
|
#import "DYScopeSample.h"
|
|
#include "DYLevelGeometry.h"
|
|
static BOOL fail(NSError**e,NSString*m){if(e)*e=[NSError errorWithDomain:@"com.dayu.color" code:1 userInfo:@{NSLocalizedDescriptionKey:m?:@"Metal 调色失败"}];return NO;}
|
|
// MRC NSError out-parameters must outlive every nested render pool. Preserve
|
|
// the same pointer across nested closures, then autorelease in the caller pool.
|
|
template<class F> static BOOL DYRenderWithPool(NSError **error,F body){
|
|
if(error)*error=nil;NSError *held=nil;BOOL ok=NO;
|
|
@autoreleasepool{@try{ok=body();}@catch(NSException *exception){ok=fail(error,exception.reason?:@"渲染异常");}if(!ok&&error)held=[*error retain];}
|
|
if(error)*error=[held autorelease];return ok;
|
|
}
|
|
static NSArray *inputFiles(){return @[@"",@"apple_log_to_rec709",@"arri_logc3_to_rec709",@"arri_logc4_to_rec709",@"blackmagic_film_gen5_to_rec709",@"canon_clog_bt709_to_rec709",@"canon_clog2_cinema_gamut_to_rec709",@"canon_clog3_cinema_gamut_to_rec709",@"dji_dlog_to_rec709",@"dji_dlog_m_to_rec709",@"fujifilm_flog_to_rec709",@"fujifilm_flog2_to_rec709",@"nikon_nlog_to_rec709",@"panasonic_vlog_vgamut_to_rec709",@"red_log3g10_rwg_to_rec709",@"sony_slog2_sgamut_to_rec709",@"sony_slog3_sgamut3cine_to_rec709",@"sony_hlg_bt2020_to_rec709"];}
|
|
static id<MTLTexture> texture3D(id<MTLDevice>d,const float*values,NSUInteger n){MTLTextureDescriptor*t=[MTLTextureDescriptor new];t.textureType=MTLTextureType3D;t.pixelFormat=MTLPixelFormatRGBA32Float;t.width=n;t.height=n;t.depth=n;t.storageMode=MTLStorageModeShared;t.usage=MTLTextureUsageShaderRead;id<MTLTexture>x=[d newTextureWithDescriptor:t];[t release];if(x)[x replaceRegion:MTLRegionMake3D(0,0,0,n,n,n) mipmapLevel:0 slice:0 withBytes:values bytesPerRow:n*16 bytesPerImage:n*n*16];return [x autorelease];}
|
|
// A synthetic texture lattice uses exactly the production render path for Cube export.
|
|
@interface DYExportTile : FxImageTile { id<MTLTexture> _tex; uint64_t _registry; FxRect _bounds;FxImageOrigin _origin; double _pixelAspect; CGColorSpaceRef _tileSpace; BOOL _hasTileSpace; }
|
|
- (instancetype)initWithTexture:(id<MTLTexture>)texture registry:(uint64_t)registry;
|
|
- (void)setBounds:(FxRect)bounds origin:(FxImageOrigin)origin;
|
|
- (void)setPixelAspect:(double)aspect;
|
|
- (void)inheritColorSpace:(CGColorSpaceRef)space;
|
|
@end
|
|
@implementation DYExportTile
|
|
- (instancetype)initWithTexture:(id<MTLTexture>)texture registry:(uint64_t)registry{if((self=[super init])){_tex=[texture retain];_registry=registry;_bounds=(FxRect){0,0,(SInt32)texture.width,(SInt32)texture.height};_origin=kFxImageOrigin_TOP_LEFT;_pixelAspect=1;}return self;}
|
|
- (void)dealloc{[_tex release];if(_tileSpace)CGColorSpaceRelease(_tileSpace);[super dealloc];}
|
|
- (void)inheritColorSpace:(CGColorSpaceRef)space{if(space)CGColorSpaceRetain(space);if(_tileSpace)CGColorSpaceRelease(_tileSpace);_tileSpace=space;_hasTileSpace=YES;}
|
|
- (id<MTLTexture>)metalTextureForDevice:(id<MTLDevice>)d{return d.registryID==_registry?_tex:nil;}
|
|
- (void)setBounds:(FxRect)bounds origin:(FxImageOrigin)origin{_bounds=bounds;_origin=origin;}
|
|
- (CGColorSpaceRef)colorSpace{if(_hasTileSpace)return _tileSpace;static CGColorSpaceRef s;static dispatch_once_t once;dispatch_once(&once,^{s=CGColorSpaceCreateWithName(kCGColorSpaceExtendedLinearSRGB);});return s;}
|
|
- (FxRect)tilePixelBounds{return _bounds;}
|
|
- (FxRect)imagePixelBounds{return self.tilePixelBounds;}
|
|
- (uint64_t)deviceRegistryID{return _registry;}
|
|
- (FxImageOrigin)imageOrigin{return _origin;}
|
|
- (double)pixelAspect{return _pixelAspect;}
|
|
- (void)setPixelAspect:(double)aspect{_pixelAspect=aspect>0?aspect:1;}
|
|
@end
|
|
static BOOL managed(DYUniforms u,NSDictionary *paths){return u.cm.configured>=0&&(u.cm.configured>.5||(![paths[@"input"]length]&&u.inputProfile<.5));}
|
|
static BOOL identityCM(DYUniforms u){
|
|
DYCMSettings s=u.cm;
|
|
BOOL neutral=s.basisEnabled<.5||(s.exposure==0&&s.balanceStrength==0&&s.saturation==1&&s.contrast==0&&s.hiContrast==0&&s.shadowContrast==0&&s.white==1&&s.highlights==0&&s.midtones==0&&s.shadows==0&&s.black==0&&s.toe==.5f&&s.shoulder==.5f&&s.chromaMix==.5f);
|
|
BOOL transforms=(u.inputSectionEnabled>.5)==(s.outputEnabled>.5)&&((u.inputSectionEnabled<.5)||(s.inputTransfer==s.outputTransfer&&s.inputGamut==s.outputGamut&&s.toneMapping<.5));
|
|
return neutral&&transforms;
|
|
}
|
|
// An enabled but untouched display tool is mathematically neutral. Bypass its
|
|
// finite-domain lookup so that the default "all eyes on" does not clip CST HDR.
|
|
static void bypassNeutralTools(DYUniforms *u){
|
|
for(NSDictionary *section in DYSections()){
|
|
if(![@[@"curve",@"hsl",@"grading",@"primaries",@"levels",@"balance"]containsObject:section[@"key"]])continue;
|
|
BOOL neutral=YES;for(NSDictionary *row in section[@"rows"]){NSInteger offset=[row[@"offset"]integerValue];if(offset<0)continue;if(*(float*)((uint8_t*)u+offset)!=[row[@"default"]floatValue]){neutral=NO;break;}}
|
|
if(neutral)for(NSDictionary *row in DYDescriptors())if([row[@"pid"]isEqual:section[@"enabled"]])*(float*)((uint8_t*)u+[row[@"offset"]unsignedIntegerValue])=0;
|
|
}
|
|
}
|
|
static void bypassIdleSkin(DYUniforms *u){
|
|
if(u->skinVisualize>.5)return;
|
|
if(u->curveSectionEnabled>.5||u->hslSectionEnabled>.5||u->colorGradingSectionEnabled>.5||u->primariesSectionEnabled>.5||u->levelsSectionEnabled>.5||u->blackWhiteSectionEnabled>.5||u->colorBalanceSectionEnabled>.5)return;
|
|
if(u->preLUTStrength!=0||u->postLUTStrength!=0||simd_any(u->extraPostLUTStrengths!=0))return;
|
|
const float *values=&u->exposure,*enabled=&u->exposureEnabled;
|
|
for(int i=0;i<13;i++){BOOL group=(i>=6&&i<=9)?u->colorSectionEnabled>.5:u->baseSectionEnabled>.5;if(group&&enabled[i]>.5&&values[i]!=0)return;}
|
|
u->skinProtection.x=0;
|
|
}
|
|
// Decide face work from the same effective operations that textures: and the
|
|
// legacy renderer actually execute. Saved strengths remain 1 for empty LUT
|
|
// slots, and B&W's eye remains on at a zero mix; neither changes the image.
|
|
static DYUniforms DYEffectiveFaceUniforms(DYUniforms u,NSDictionary *paths,NSDictionary *tools){
|
|
DYApplyRemovedBaseTools(&u,tools);
|
|
paths=DYVisibleBaseToolPaths(paths,tools);
|
|
BOOL input=u.primarySectionEnabled>.5&&u.inputSectionEnabled>.5&&!managed(u,paths);
|
|
if(!input||(![paths[@"input"]length]&&u.inputProfile<.5))u.preLUTStrength=0;
|
|
for(int i=0;i<5;i++)if(u.lookSectionEnabled<.5||(i&&u.extraLookSectionEnableds[i-1]<.5)||![paths[[NSString stringWithFormat:@"look%d",i]]length]){
|
|
if(i==0)u.postLUTStrength=0;else u.extraPostLUTStrengths[i-1]=0;
|
|
}
|
|
bypassNeutralTools(&u);
|
|
if(u.cm.blackWhiteMix<=0)u.blackWhiteSectionEnabled=0;
|
|
bypassIdleSkin(&u);
|
|
return u;
|
|
}
|
|
// Admission and identity bypass for the explicitly bounded finishing domain.
|
|
static NSArray *DYManagedActiveFinishing(DYUniforms u,NSDictionary *paths,NSData *spider,NSDictionary *tools){
|
|
bypassNeutralTools(&u);bypassIdleSkin(&u);if(u.cm.blackWhiteMix<=0)u.blackWhiteSectionEnabled=0;
|
|
NSMutableArray *active=[NSMutableArray array];
|
|
for(NSString *type in DYToolTypes()){
|
|
if([type isEqual:@"primary"])continue;
|
|
NSMutableDictionary *values=[NSMutableDictionary dictionary],*localPaths=[NSMutableDictionary dictionary];
|
|
for(NSDictionary *d in DYBaseToolDescriptors(type))if([d[@"offset"]integerValue]>=0)values[d[@"key"]]=@(*(float*)((uint8_t*)&u+[d[@"offset"]unsignedIntegerValue]));
|
|
if([type isEqual:@"look"]&&u.lookSectionEnabled>.5)for(NSString *key in paths)if([key hasPrefix:@"look"])localPaths[key]=paths[key];
|
|
NSMutableDictionary *node=[NSMutableDictionary dictionaryWithDictionary:@{@"type":type,@"values":values,@"paths":localPaths}];if([type isEqual:@"spider"]&&spider)node[@"spider"]=spider;
|
|
if(!DYToolIsNeutral(node))[active addObject:type];
|
|
}
|
|
for(NSDictionary *node in tools[@"layers"])if(!DYToolIsNeutral(node))[active addObject:node[@"type"]];return active;
|
|
}
|
|
// V3 eligibility is based on active math, not differences in disabled switches.
|
|
// Keep the historical V2 predicate/render behavior unchanged.
|
|
static NSArray *DYPrimaryActiveFinishing(DYUniforms u,NSDictionary *paths,NSData *spider,NSDictionary *tools){
|
|
auto normalize=[](DYUniforms v){
|
|
float *values=&v.exposure,*flags=&v.exposureEnabled;
|
|
for(int i=0;i<13;i++){BOOL group=i>=6&&i<=9?v.colorSectionEnabled>.5:v.baseSectionEnabled>.5;if(!group||flags[i]<.5)values[i]=0;flags[i]=1;}
|
|
for(NSDictionary *section in DYSections())for(NSDictionary *row in section[@"rows"])if(row[@"enabled"]){
|
|
for(NSDictionary *flag in DYDescriptors())if([flag[@"pid"]isEqual:row[@"enabled"]]&&[flag[@"offset"]integerValue]>=0){float*f=(float*)((uint8_t*)&v+[flag[@"offset"]unsignedIntegerValue]);if(*f<.5&&[row[@"offset"]integerValue]>=0)*(float*)((uint8_t*)&v+[row[@"offset"]unsignedIntegerValue])=[row[@"default"]floatValue];*f=[flag[@"default"]floatValue];}
|
|
}
|
|
v.postLUTStrength=1;for(int i=0;i<4;i++){v.extraPostLUTStrengths[i]=1;v.extraLookSectionEnableds[i]=1;}
|
|
return v;
|
|
};
|
|
auto visiblePaths=[](NSDictionary*p,DYUniforms v){NSMutableDictionary*out=[[p mutableCopy]autorelease]?:[NSMutableDictionary dictionary];for(int i=0;i<5;i++)if(v.lookSectionEnabled<.5||(i>0&&v.extraLookSectionEnableds[i-1]<.5)||(i==0?v.postLUTStrength:v.extraPostLUTStrengths[i-1])<=0)[out removeObjectForKey:[NSString stringWithFormat:@"look%d",i]];return out;};
|
|
NSDictionary*rootPaths=visiblePaths(paths,u);u=normalize(u);NSMutableDictionary*doc=DYMutablePlist(tools);for(NSMutableDictionary*node in doc[@"layers"]){DYUniforms original=DYToolUniforms(node),v=normalize(original);NSMutableDictionary*values=[NSMutableDictionary dictionary];for(NSDictionary*d in DYBaseToolDescriptors(node[@"type"]))if([d[@"offset"]integerValue]>=0)values[d[@"key"]]=@(*(float*)((uint8_t*)&v+[d[@"offset"]unsignedIntegerValue]));node[@"values"]=values;node[@"paths"]=visiblePaths(node[@"paths"],original);}
|
|
return DYManagedActiveFinishing(u,rootPaths,spider,doc);
|
|
}
|
|
@interface DYRenderer(){
|
|
NSMutableDictionary *_devices,*_luts,*_preparedLUTs;
|
|
NSSet *_activeLUTPaths;
|
|
dispatch_queue_t _lutPreparationQueue;
|
|
DYColorTables *_tables;
|
|
NSObject *_previewLock;
|
|
NSData *_preview,*_lookLUTPreview;
|
|
CFTimeInterval _lastPreview;
|
|
BOOL _exporting;
|
|
NSDictionary *_toolDocument,*_colorContext,*_exportManagedState;
|
|
BOOL _managedFinishing;NSData *_faceEntries,*_faceInput,*_faceCachedEntries;CIContext *_faceContext;NSData *_mosaicRaster,*_mosaicEntries;NSDictionary *_mosaicFaceSnapshot;
|
|
dispatch_queue_t _scopeQueue;
|
|
BOOL _scopeEnabled,_scopeWorkerRunning,_outputObserved,_outputNotificationPending,_spiderScopeWorkerRunning;
|
|
NSData *_spiderScopeRaster;
|
|
uint64_t _spiderScopeSerial;
|
|
NSUInteger _scopeGeneration;
|
|
DYScopeSample *_scopePending,*_scopeLatest;
|
|
uint64_t _scopeCaptureSerial,_scopeLatestSerial;CMTime _scopeRequestedTime;
|
|
NSData *_scopePreview;
|
|
NSDictionary *_scopeMetadata;
|
|
DYColorProfile *_scopeProfile;
|
|
}
|
|
@end
|
|
@implementation DYRenderer
|
|
#include "DYScopeCapture.inc.h"
|
|
#include "DYFilmRender.inc.h"
|
|
#include "DYPrimaryRender.inc.h"
|
|
#include "DYUnifiedRender.inc.h"
|
|
#include "DYMosaicRender.inc.h"
|
|
#include "DYManagedRender.inc.h"
|
|
#include "DYFaceRender.inc.h"
|
|
#include "DYToolRender.inc.h"
|
|
- (instancetype)init{if((self=[super init])){_previewLock=[NSObject new];_scopeQueue=dispatch_queue_create("com.dayu.color.final-output-scopes",DISPATCH_QUEUE_SERIAL);_devices=[NSMutableDictionary new];_luts=[NSMutableDictionary new];_preparedLUTs=[NSMutableDictionary new];_activeLUTPaths=[NSSet new];_lutPreparationQueue=dispatch_queue_create("com.dayu.color.lut-upload",DISPATCH_QUEUE_SERIAL);dispatch_set_target_queue(_lutPreparationQueue,dispatch_get_global_queue(QOS_CLASS_USER_INITIATED,0));_tables=[DYColorTables new];}return self;}
|
|
- (void)dealloc{[_mosaicRaster release];[_mosaicEntries release];[_mosaicFaceSnapshot release];[_faceEntries release];[_faceInput release];[_faceCachedEntries release];[_faceContext release];[_scopePending release];[_scopeLatest release];[_spiderScopeRaster release];[_scopePreview release];[_scopeMetadata release];[_scopeProfile release];dispatch_release(_scopeQueue);[_devices release];[_luts release];[_preparedLUTs release];[_activeLUTPaths release];dispatch_release(_lutPreparationQueue);[_tables release];[_preview release];[_lookLUTPreview release];[_colorContext release];[_previewLock release];[super dealloc];}
|
|
- (void)trimPreparedLUTsKeeping:(NSString *)path {
|
|
if(_preparedLUTs.count<=12)return;
|
|
for(NSString *key in _preparedLUTs.allKeys)if(![_activeLUTPaths containsObject:key]&&![key isEqualToString:path])[_preparedLUTs removeObjectForKey:key];
|
|
}
|
|
- (id<MTLTexture>)textureForTable:(DYLUTPreparedTable *)table entry:(NSDictionary *)entry error:(NSError **)error {
|
|
NSString *key=[NSString stringWithFormat:@"%llu|%@",[entry[@"device"]registryID],table.revision];id<MTLTexture>texture=_luts[key];
|
|
if(!texture){texture=texture3D(entry[@"device"],table.rgbaValues,table.dimension);if(!texture){fail(error,@"LUT 纹理分配失败");return nil;}if(_luts.count>=12)[_luts removeAllObjects];_luts[key]=texture;}return texture;
|
|
}
|
|
- (NSProgress *)prepareLUTPath:(NSString *)path completion:(void (^)(NSError *))completion {
|
|
NSProgress *progress=[NSProgress discreteProgressWithTotalUnitCount:2];progress.localizedDescription=@"正在载入 LUT…";path=[[path copy]autorelease];
|
|
dispatch_async(_lutPreparationQueue,^{@autoreleasepool{
|
|
NSError *error=nil;DYLUTPreparedTable *table=progress.cancelled?nil:DYLUTReadPreparedTable(path,progress,&error);progress.completedUnitCount=1;
|
|
if(table&&!progress.cancelled){@synchronized(self){
|
|
if(!_devices.count)[self device:0 error:&error];
|
|
if(!_devices.count&&!error)fail(&error,@"没有可用的 LUT 渲染设备");
|
|
BOOL ready=error==nil;for(NSDictionary *entry in _devices.allValues)if(!progress.cancelled&&![self textureForTable:table entry:entry error:&error]){ready=NO;break;}
|
|
if(ready&&!progress.cancelled){_preparedLUTs[path]=table;[self trimPreparedLUTsKeeping:path];}
|
|
}}
|
|
if(progress.cancelled)error=[NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:@{NSLocalizedDescriptionKey:@"已取消 LUT 加载"}];
|
|
progress.completedUnitCount=2;dispatch_async(dispatch_get_main_queue(),^{NSError *result=progress.cancelled?[NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:nil]:error;if(completion)completion(result);});
|
|
}});return progress;
|
|
}
|
|
- (void)notifyPreviewChanged{dispatch_async(dispatch_get_main_queue(),^{[NSNotificationCenter.defaultCenter postNotificationName:@"DYPreviewDidChange" object:self];});}
|
|
- (void)invalidatePreview{@synchronized(_previewLock){[_preview release];_preview=nil;[_lookLUTPreview release];_lookLUTPreview=nil;_lastPreview=0;}dispatch_async(dispatch_get_main_queue(),^{[NSNotificationCenter.defaultCenter postNotificationName:@"DYPreviewDidChange" object:self];});}
|
|
- (NSData*)preview{@synchronized(_previewLock){return [[_preview retain]autorelease];}}
|
|
- (NSData*)lookLUTPreview{@synchronized(_previewLock){return [[_lookLUTPreview retain]autorelease];}}
|
|
- (NSMutableDictionary*)device:(uint64_t)registry error:(NSError**)err{
|
|
NSNumber*k=@(registry);NSMutableDictionary*entry=_devices[k];if(entry)return entry;
|
|
uint64_t beta=DYBetaSpanBegin(@"device_prepare",nil);@try{
|
|
id<MTLDevice>d=nil;NSArray*all=MTLCopyAllDevices();for(id<MTLDevice>candidate in all)if(candidate.registryID==registry||!registry){d=[candidate retain];break;}[all release];if(!d){fail(err,@"宿主的 Metal 设备不可用");return nil;}
|
|
NSURL*url=[[NSBundle bundleForClass:self.class]URLForResource:@"default" withExtension:@"metallib"];
|
|
id<MTLLibrary>lib=[d newLibraryWithURL:url error:err];if(!lib){[d release];return nil;}
|
|
id<MTLCommandQueue>q=[d newCommandQueue];entry=[NSMutableDictionary dictionaryWithDictionary:@{@"device":d,@"library":lib,@"queue":q,@"pipelines":[NSMutableDictionary dictionary]}];
|
|
for(NSString*fn in @[@"dyLensRepair",@"dyUnpremultiply",@"dyWhitesSceneStatistics",@"dyLUTExportKernel",@"dyInputAnalysis",@"dyScopeSample",@"dyCloneStamps",@"dyColorManagement",@"dyToolBlend",@"dyToolSkin",@"dyFilmPoint",@"dyFilmNoise",@"dyFilmSparseBlur",@"dyFilmBloomEdges",@"dyFilmMix",@"dyFilmPrint"]){id<MTLFunction>f=[lib newFunctionWithName:fn];if(!f){fail(err,[fn stringByAppendingString:@" 缺失"]);[lib release];[q release];[d release];return nil;}uint64_t pipelineSpan=DYBetaSpanBegin(@"gpu_pipeline",@{@"pipeline":fn,@"gpu":d.name?:@"unknown"});id<MTLComputePipelineState>p=[d newComputePipelineStateWithFunction:f error:err];DYBetaSpanEnd(pipelineSpan,@{@"status":p?@"passed":@"failed"});[f release];if(!p){[lib release];[q release];[d release];return nil;}entry[fn]=p;[p release];}
|
|
float identity[32];for(int z=0;z<2;z++)for(int y=0;y<2;y++)for(int x=0;x<2;x++){int i=((z*2+y)*2+x)*4;identity[i]=x;identity[i+1]=y;identity[i+2]=z;identity[i+3]=1;}id<MTLTexture>neutral=texture3D(d,identity,2);if(!neutral){fail(err,@"无法分配中性 LUT");return nil;}entry[@"identity"]=neutral;
|
|
// Preparing before the first frame may use registry 0. Reuse those pipelines
|
|
// when the host subsequently supplies that device's actual registry ID.
|
|
_devices[k]=entry;_devices[@(d.registryID)]=entry;[d release];[lib release];[q release];return entry;
|
|
}@finally{DYBetaSpanEnd(beta,nil);}
|
|
}
|
|
- (id<MTLTexture>)lut:(NSString*)path entry:(NSDictionary*)e error:(NSError**)err{
|
|
if(!path.length)return e[@"identity"];
|
|
DYLUTPreparedTable *table=_preparedLUTs[path];
|
|
// Restored projects load once on their render worker. Explicit selections
|
|
// arrive prepared, so neither metadata I/O nor text parsing occurs here.
|
|
if(!table){table=DYLUTReadPreparedTable(path,nil,err);if(!table)return nil;_preparedLUTs[path]=table;[self trimPreparedLUTsKeeping:path];}
|
|
return [self textureForTable:table entry:e error:err];
|
|
}
|
|
- (NSArray*)textures:(NSDictionary*)paths uniforms:(DYUniforms*)u entry:(NSDictionary*)e error:(NSError**)err{
|
|
// Normalize only this render copy. Disabled groups retain their host parameters
|
|
// and paths, while inactive LUTs neither load files nor affect any layer.
|
|
BOOL inputEnabled=u->inputSectionEnabled>.5&&!managed(*u,paths);
|
|
NSString*input=inputEnabled?paths[@"input"]:nil;int profile=inputEnabled?(int)roundf(u->inputProfile):0;
|
|
if(!inputEnabled){u->inputProfile=0;u->preLUTStrength=0;}
|
|
if(!input.length&&profile>0&&profile<(int)inputFiles().count)input=[[NSBundle bundleForClass:self.class]pathForResource:inputFiles()[profile] ofType:@"dylut" inDirectory:@"InputLUTs"];
|
|
if(profile>0&&!input.length){fail(err,@"相机输入转换资源缺失");return nil;}
|
|
NSMutableArray*ts=[NSMutableArray array];id<MTLTexture>t=[self lut:input entry:e error:err];if(!t)return nil;[ts addObject:t];if(!input.length)u->preLUTStrength=0;
|
|
BOOL looksEnabled=u->lookSectionEnabled>.5;
|
|
if(!looksEnabled)u->extraLookSectionEnableds=(DYFloat4){0,0,0,0};
|
|
for(int i=0;i<5;i++){BOOL active=looksEnabled&&(i==0||u->extraLookSectionEnableds[i-1]>.5);NSString*p=active?paths[[NSString stringWithFormat:@"look%d",i]]:nil;t=[self lut:p entry:e error:err];if(!t)return nil;[ts addObject:t];if(!p.length){if(i==0)u->postLUTStrength=0;else u->extraPostLUTStrengths[i-1]=0;}}
|
|
[DYColorTables fillPrimaries:u];id<MTLDevice>d=e[@"device"];
|
|
id<MTLTexture>h=[_tables hslForDevice:d uniforms:u],w=[_tables whiteBalanceForDevice:d uniforms:u],g=[_tables gradingForDevice:d uniforms:u];if(!h||!w||!g){fail(err,_tables.lastError?:@"颜色查表构建失败");return nil;}[ts addObjectsFromArray:@[h,w,g]];return ts;
|
|
}
|
|
- (BOOL)renderSource:(FxImageTile*)src destination:(FxImageTile*)dst uniforms:(DYUniforms)u paths:(NSDictionary*)paths error:(NSError**)err{return [self renderSource:src destination:dst uniforms:u paths:paths spider:nil error:err];}
|
|
- (BOOL)renderSource:(FxImageTile*)src destination:(FxImageTile*)dst uniforms:(DYUniforms)u paths:(NSDictionary*)paths spider:(NSData *)spider error:(NSError**)err{return [self renderSource:src destination:dst uniforms:u paths:paths spider:spider tools:nil error:err];}
|
|
- (BOOL)renderLegacy:(FxImageTile*)src destination:(FxImageTile*)dst uniforms:(DYUniforms)u paths:(NSDictionary*)paths spider:(NSData *)spider masks:(NSDictionary*)masks error:(NSError**)err{return [self renderLegacy:src destination:dst uniforms:u paths:paths spider:spider masks:masks stageFirst:-1 stageLast:-1 skinReference:nil originalReference:nil captureOriginal:nullptr suppressBlackWhite:NO error:err];}
|
|
- (BOOL)renderLegacy:(FxImageTile*)src destination:(FxImageTile*)dst uniforms:(DYUniforms)u paths:(NSDictionary*)paths spider:(NSData *)spider masks:(NSDictionary*)masks stageFirst:(NSInteger)stageFirst stageLast:(NSInteger)stageLast skinReference:(FxImageTile*)skinReference originalReference:(FxImageTile*)originalReference captureOriginal:(DYExportTile**)captureOriginal suppressBlackWhite:(BOOL)suppressBlackWhite error:(NSError**)err{@synchronized(self){return DYRenderWithPool(err,[&]()->BOOL{
|
|
BOOL segmented=stageFirst>=0;if(segmented&&(stageFirst>stageLast||stageLast>11))return fail(err,@"原生工具分段范围无效");
|
|
if(spider.length&&!DYSpiderMeshIsValid(spider))return fail(err,@"蜘蛛盘数据无效");
|
|
if(u.primarySectionEnabled<.5){u.inputSectionEnabled=0;u.cm.basisEnabled=0;u.cm.outputEnabled=0;}
|
|
|
|
[_activeLUTPaths release];_activeLUTPaths=[[NSSet alloc]initWithArray:paths.allValues?:@[]];
|
|
NSMutableDictionary*e=[self device:dst.deviceRegistryID error:err];if(!e)return NO;id<MTLDevice>d=e[@"device"];id<MTLTexture>input=[src metalTextureForDevice:d],output=[dst metalTextureForDevice:d];if(!input||!output)return fail(err,@"宿主图像纹理不可用");
|
|
BOOL useCM=managed(u,paths)&&(!segmented||stageFirst==0);bypassNeutralTools(&u);float bwMix=fmaxf(0,fminf(1,u.cm.blackWhiteMix));if(bwMix<=0)u.blackWhiteSectionEnabled=0;
|
|
NSArray*ts=[self textures:paths uniforms:&u entry:e error:err];if(!ts)return NO;bypassIdleSkin(&u);
|
|
// Only a deformed active mesh allocates samples; device entries retain the
|
|
// current buffer across frames. Neutral grids are exact identity at all densities.
|
|
simd_int2 spiderSize={0,0};id<MTLBuffer>spiderBuffer=nil;
|
|
if(u.spiderSectionEnabled>.5&&u.spiderMix>0&&spider.length){
|
|
if(![e[@"spiderData"]isEqual:spider]){auto mesh=DYSpiderMesh(spider);auto samples=dy::meshSamples(mesh);BOOL changed=NO;for(float value:samples)changed|=value!=0;
|
|
e[@"spiderData"]=spider;e[@"spiderActive"]=@(changed);if(changed){id<MTLBuffer>b=[d newBufferWithBytes:samples.data() length:samples.size()*sizeof(float) options:MTLResourceStorageModeShared];if(!b)return fail(err,@"无法分配蜘蛛盘缓冲区");e[@"spiderBuffer"]=b;[b release];e[@"spiderHues"]=@(mesh.hues);e[@"spiderRings"]=@(mesh.rings);}else[e removeObjectForKey:@"spiderBuffer"];
|
|
}
|
|
if([e[@"spiderActive"]boolValue]){spiderBuffer=e[@"spiderBuffer"];spiderSize={ [e[@"spiderHues"]intValue],[e[@"spiderRings"]intValue]};}
|
|
}
|
|
BOOL faceOnly=u.skinProtection.x>.5&&(u.skinProtection.y>0||u.skinVisualize>.5)&&u.skinFaceOnly>.5;
|
|
DYToolMaskHeader maskHeader{};id<MTLBuffer>maskBuffer=(masks.count||segmented||faceOnly||_faceEntries.length)?[self maskBuffer:masks tile:src device:d header:&maskHeader]:nil;maskHeader.faceOnly=faceOnly;BOOL hasMasks=maskHeader.count>0||maskHeader.faceOnly;if((hasMasks||segmented)&&!maskBuffer)return fail(err,@"无法分配蒙版缓冲区");
|
|
NSMutableDictionary*ps=e[@"pipelines"];NSString*format=[NSString stringWithFormat:@"%lu-%d-%d",(unsigned long)output.pixelFormat,hasMasks,segmented];id<MTLRenderPipelineState>pipeline=ps[format];if(!pipeline){MTLRenderPipelineDescriptor*p=[MTLRenderPipelineDescriptor new];p.vertexFunction=[e[@"library"]newFunctionWithName:@"dyVertex"];p.fragmentFunction=[e[@"library"]newFunctionWithName:segmented?@"dySegmentFragment":hasMasks?@"dyMaskedFragment":@"dyFragment"];p.colorAttachments[0].pixelFormat=output.pixelFormat;pipeline=[d newRenderPipelineStateWithDescriptor:p error:err];[p.vertexFunction release];[p.fragmentFunction release];[p release];if(!pipeline)return NO;ps[format]=pipeline;[pipeline release];}
|
|
MTLTextureDescriptor*td=[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA16Float width:input.width height:input.height mipmapped:NO];td.storageMode=MTLStorageModePrivate;td.usage=MTLTextureUsageShaderRead|MTLTextureUsageShaderWrite;id<MTLTexture>straight=[d newTextureWithDescriptor:td];if(!straight)return fail(err,@"无法分配调色缓冲区");
|
|
id<MTLCommandBuffer>cb=[e[@"queue"]commandBuffer];id<MTLComputeCommandEncoder>ce=[cb computeCommandEncoder];[ce setComputePipelineState:e[@"dyUnpremultiply"]];[ce setTexture:input atIndex:0];[ce setTexture:straight atIndex:1];[ce dispatchThreads:MTLSizeMake(input.width,input.height,1) threadsPerThreadgroup:MTLSizeMake(16,16,1)];[ce endEncoding];
|
|
if(useCM&&!identityCM(u)&&u.cm.mix>0){
|
|
DayuRenderParams cm=DYCMRender(u.cm);cm.stages=(cm.stages&~1)|(u.inputSectionEnabled>.5?1:0);
|
|
if(cm.stages){id<MTLTexture>converted=[d newTextureWithDescriptor:td];if(!converted){[straight release];return fail(err,@"无法分配 CST 缓冲区");}
|
|
ce=[cb computeCommandEncoder];[ce setComputePipelineState:e[@"dyColorManagement"]];[ce setTexture:straight atIndex:0];[ce setTexture:converted atIndex:1];[ce setBytes:&cm length:sizeof(cm) atIndex:0];[ce setBytes:&u.cm.mix length:4 atIndex:1];[ce dispatchThreads:MTLSizeMake(input.width,input.height,1) threadsPerThreadgroup:MTLSizeMake(16,16,1)];[ce endEncoding];if(hasMasks){id<MTLTexture>blended=[d newTextureWithDescriptor:td];if(!blended){[converted release];[straight release];return fail(err,@"无法分配一级蒙版缓冲区");}maskHeader.section=0;maskHeader.premultiplied=0;[self encodeBlend:straight after:converted output:blended masks:maskBuffer header:maskHeader entry:e commands:cb];[converted release];converted=blended;}[straight release];straight=converted;
|
|
}
|
|
}
|
|
// Keep the actual post-CST straight source alive across all native segments.
|
|
// Both native spatial helpers and whites statistics historically read this
|
|
// source; the current segment's center color is a different input.
|
|
id<MTLTexture>nativeSource=segmented&&originalReference?[originalReference metalTextureForDevice:d]:straight;
|
|
if(!nativeSource){[straight release];return fail(err,@"原生工具参考纹理不可用");}
|
|
if(segmented&&captureOriginal&&!*captureOriginal){DYExportTile *reference=[[DYExportTile alloc]initWithTexture:nativeSource registry:d.registryID];[reference setBounds:src.tilePixelBounds origin:src.imageOrigin];[reference setPixelAspect:src.pixelAspect];*captureOriginal=reference;}
|
|
float level=.5;id<MTLBuffer>scene=[d newBufferWithBytes:&level length:4 options:MTLResourceStorageModeShared];
|
|
if(!_exporting&&(!segmented||(stageFirst<=2&&stageLast>=2))&&u.whites>0&&u.whitesEnabled>.5&&u.baseSectionEnabled>.5){ce=[cb computeCommandEncoder];[ce setComputePipelineState:e[@"dyWhitesSceneStatistics"]];[ce setTexture:nativeSource atIndex:0];[ce setTexture:ts[7] atIndex:1];[ce setBytes:&u length:sizeof(u) atIndex:0];[ce setBuffer:scene offset:0 atIndex:1];[ce dispatchThreadgroups:MTLSizeMake(1,1,1) threadsPerThreadgroup:MTLSizeMake(256,1,1)];[ce endEncoding];}
|
|
BOOL preview=NO;@synchronized(_previewLock){preview=(!segmented||stageFirst==0)&&!_exporting&&CACurrentMediaTime()-_lastPreview>.5;}id<MTLBuffer>pb=nil;if(preview){pb=[d newBufferWithLength:320*180*16 options:MTLResourceStorageModeShared];ce=[cb computeCommandEncoder];[ce setComputePipelineState:e[@"dyInputAnalysis"]];[ce setTexture:straight atIndex:0];[ce setTexture:ts[0] atIndex:1];float inputStrength=u.inputSectionEnabled>.5?u.preLUTStrength:0;[ce setBytes:&inputStrength length:4 atIndex:1];[ce setBuffer:pb offset:0 atIndex:0];[ce dispatchThreads:MTLSizeMake(320,180,1) threadsPerThreadgroup:MTLSizeMake(8,8,1)];[ce endEncoding];}
|
|
MTLRenderPassDescriptor*rp=[MTLRenderPassDescriptor renderPassDescriptor];rp.colorAttachments[0].texture=output;rp.colorAttachments[0].loadAction=MTLLoadActionDontCare;rp.colorAttachments[0].storeAction=MTLStoreActionStore;id<MTLRenderCommandEncoder>re=[cb renderCommandEncoderWithDescriptor:rp];[re setRenderPipelineState:pipeline];
|
|
float w=output.width,h=output.height;FxRect s=src.tilePixelBounds,t=dst.tilePixelBounds;float sw=s.right-s.left,sh=s.top-s.bottom;float l=(t.left-s.left)/sw,r=(t.right-s.left)/sw,top=(s.top-t.top)/sh,bottom=(s.top-t.bottom)/sh;
|
|
if(src.imageOrigin==kFxImageOrigin_BOTTOM_LEFT){top=1-top;bottom=1-bottom;}
|
|
DYVertex2D vs[]={{{w/2,-h/2},{r,bottom}},{{-w/2,-h/2},{l,bottom}},{{w/2,h/2},{r,top}},{{-w/2,h/2},{l,top}}};
|
|
if(dst.imageOrigin==kFxImageOrigin_BOTTOM_LEFT)for(int i=0;i<4;i++)vs[i].position.y=-vs[i].position.y;
|
|
vector_uint2 vp={(unsigned)output.width,(unsigned)output.height};[re setVertexBytes:vs length:sizeof(vs) atIndex:0];[re setVertexBytes:&vp length:sizeof(vp) atIndex:1];[re setViewport:(MTLViewport){0,0,w,h,0,1}];[re setFragmentTexture:straight atIndex:0];for(NSUInteger i=0;i<ts.count;i++)[re setFragmentTexture:ts[i] atIndex:i+1];[re setFragmentBytes:&u length:sizeof(u) atIndex:0];[re setFragmentBuffer:scene offset:0 atIndex:1];DYRenderOptions options={1,bwMix,0,0};[re setFragmentBytes:&options length:sizeof(options) atIndex:2];DYUniforms color=u;color.blackWhiteSectionEnabled=0;[re setFragmentBytes:&color length:sizeof(color) atIndex:3];if(spiderBuffer)[re setFragmentBuffer:spiderBuffer offset:0 atIndex:4];else{float empty[2]={0,0};[re setFragmentBytes:empty length:sizeof(empty) atIndex:4];}[re setFragmentBytes:&spiderSize length:sizeof(spiderSize) atIndex:5];if(hasMasks||segmented){[re setFragmentBuffer:maskBuffer offset:0 atIndex:6];[re setFragmentBytes:&maskHeader length:sizeof(maskHeader) atIndex:7];}if(segmented){DYToolStageRange range={(int)stageFirst,(int)stageLast,(int)suppressBlackWhite};[re setFragmentBytes:&range length:sizeof(range) atIndex:8];[re setFragmentTexture:skinReference?[skinReference metalTextureForDevice:d]:input atIndex:10];[re setFragmentTexture:nativeSource atIndex:11];}[re drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];[re endEncoding];[cb commit];[cb waitUntilCompleted];BOOL ok=cb.status==MTLCommandBufferStatusCompleted;
|
|
if(ok&&pb){@synchronized(_previewLock){
|
|
[_preview release];_preview=[[NSData alloc]initWithBytes:pb.contents length:320*180*16];
|
|
// The analysis kernel reads physical texture rows. Its result remains intact
|
|
// for auto grade; only the browser copy is normalized to top-first bitmap rows.
|
|
[_lookLUTPreview release];
|
|
if(src.imageOrigin==kFxImageOrigin_BOTTOM_LEFT){
|
|
const NSUInteger rowBytes=320*16;NSMutableData*topFirst=[NSMutableData dataWithLength:_preview.length];
|
|
for(NSUInteger y=0;y<180;y++)memcpy((uint8_t*)topFirst.mutableBytes+y*rowBytes,(const uint8_t*)_preview.bytes+(179-y)*rowBytes,rowBytes);
|
|
_lookLUTPreview=[topFirst copy];
|
|
}else _lookLUTPreview=[_preview retain];
|
|
_lastPreview=CACurrentMediaTime();[self notifyPreviewChanged];
|
|
}}
|
|
if(!ok)fail(err,cb.error.localizedDescription);[pb release];[straight release];[scene release];return ok;
|
|
});}}
|
|
- (BOOL)exportLUT:(NSURL*)url uniforms:(DYUniforms)u paths:(NSDictionary*)paths error:(NSError**)err{return [self exportLUT:url uniforms:u paths:paths spider:nil error:err];}
|
|
- (BOOL)exportLUT:(NSURL*)url uniforms:(DYUniforms)u paths:(NSDictionary*)paths spider:(NSData*)spider error:(NSError**)err{@synchronized(self){
|
|
if(DYFilmToolActive(u)&&DYFilmSpatialActive(u.film))return fail(err,@"胶片空间和颗粒处理不能保存到静态 Cube,请保存完整预设。");
|
|
NSMutableDictionary *e=[self device:0 error:err];if(!e)return NO;id<MTLDevice>d=e[@"device"];const NSUInteger n=_exportManagedState?65:33,count=n*n*n,width=n*n,height=n;
|
|
MTLTextureDescriptor *desc=[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:(_exportManagedState?MTLPixelFormatRGBA32Float:MTLPixelFormatRGBA16Float) width:width height:height mipmapped:NO];desc.storageMode=MTLStorageModeShared;desc.usage=MTLTextureUsageShaderRead|MTLTextureUsageShaderWrite|MTLTextureUsageRenderTarget;
|
|
id<MTLTexture>input=[d newTextureWithDescriptor:desc],output=[d newTextureWithDescriptor:desc];if(!input||!output){[input release];[output release];return fail(err,@"无法分配 LUT 色格");}
|
|
std::vector<__fp16>values(count*4);std::vector<float>precise(_exportManagedState?count*4:0);for(NSUInteger i=0;i<count;i++){float v[3]={(float)(i%n)/(n-1),(float)((i/n)%n)/(n-1),(float)(i/(n*n))/(n-1)};for(int c=0;c<3;c++)values[i*4+c]=v[c]<=.04045f?v[c]/12.92f:powf((v[c]+.055f)/1.055f,2.4f);values[i*4+3]=1;if(_exportManagedState){for(int c=0;c<3;c++)precise[i*4+c]=v[c]<=.04045f?v[c]/12.92f:powf((v[c]+.055f)/1.055f,2.4f);precise[i*4+3]=1;}}
|
|
[input replaceRegion:MTLRegionMake2D(0,0,width,height) mipmapLevel:0 withBytes:_exportManagedState?(const void*)precise.data():(const void*)values.data() bytesPerRow:width*(_exportManagedState?16:8)];
|
|
DYExportTile *source=[[[DYExportTile alloc]initWithTexture:input registry:d.registryID]autorelease],*dest=[[[DYExportTile alloc]initWithTexture:output registry:d.registryID]autorelease];
|
|
u.textureEnabled=0;u.clarityEnabled=0;u.dehazeEnabled=0;BOOL ok=NO;_exporting=YES;
|
|
@try{ok=[self renderSource:source destination:dest uniforms:u paths:paths spider:spider tools:_toolDocument colorManagement:_exportManagedState hostPrimaries:kFxColorPrimaries_Rec709 error:err];}@finally{_exporting=NO;}
|
|
if(ok)[output getBytes:_exportManagedState?(void*)precise.data():(void*)values.data() bytesPerRow:width*(_exportManagedState?16:8) fromRegion:MTLRegionMake2D(0,0,width,height) mipmapLevel:0];[input release];[output release];if(!ok)return NO;
|
|
NSMutableString *text=[NSMutableString stringWithString:@"TITLE \"大鱼调色\"\n# 包含 CST 与逐像素颜色;不包含纹理、清晰度和逐画面分析。\nLUT_3D_SIZE 33\nDOMAIN_MIN 0 0 0\nDOMAIN_MAX 1 1 1\n"];
|
|
for(NSUInteger i=0;i<count;i++){float rgb[3];for(int c=0;c<3;c++){float v=_exportManagedState?precise[i*4+c]:(float)values[i*4+c];rgb[c]=v<=.0031308f?v*12.92f:1.055f*powf(v,1/2.4f)-.055f;if(!std::isfinite(rgb[c]))return fail(err,@"LUT 输出含无效值");}[text appendFormat:@"%.8f %.8f %.8f\n",rgb[0],rgb[1],rgb[2]];}
|
|
if(_exportManagedState){NSData *metadata=[NSJSONSerialization dataWithJSONObject:_exportManagedState options:NSJSONWritingSortedKeys error:nil];if(metadata)[text insertString:[NSString stringWithFormat:@"# DAYU_CMS_STATE %@\n",[[[NSString alloc]initWithData:metadata encoding:NSUTF8StringEncoding]autorelease]] atIndex:0];[text replaceOccurrencesOfString:@"LUT_3D_SIZE 33" withString:@"LUT_3D_SIZE 65" options:0 range:NSMakeRange(0,text.length)];if(DYPrimaryColorState(_exportManagedState)){[text insertString:[NSString stringWithFormat:@"# V3 input/output: code values defined by enabled primary CST stages; a disabled primary passes unmanaged host code values.\n# PRIMARY_CST enabled=%d input=%d/%d working=%d/%d output=%d/%d mapping=%d\n",u.primarySectionEnabled>.5,(int)(u.inputSectionEnabled>.5?u.cm.inputTransfer:u.cm.workingTransfer),(int)(u.inputSectionEnabled>.5?u.cm.inputGamut:u.cm.workingGamut),(int)u.cm.workingTransfer,(int)u.cm.workingGamut,(int)(u.cm.outputEnabled>.5?u.cm.outputTransfer:u.cm.workingTransfer),(int)(u.cm.outputEnabled>.5?u.cm.outputGamut:u.cm.workingGamut),(int)u.cm.toneMapping] atIndex:0];}else [text insertString:@"# V2 input: declared camera/LUT code values, or sRGB in host mode. Output: sRGB / Rec.709.\n" atIndex:0];}
|
|
return [text writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:err];
|
|
}}
|
|
@end
|