From 63c49b013588b8b74a4d5ecb2a9b1b076b2e6069 Mon Sep 17 00:00:00 2001 From: mac Date: Sun, 8 Nov 2026 19:11:01 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A9=20Metal=20=E7=9B=B4=E5=86=99=20Vision?= =?UTF-8?q?=20=E8=BF=BD=E8=B8=AA=E8=BE=93=E5=85=A5=E5=B9=B6=E5=8A=A0?= =?UTF-8?q?=E5=85=A5=20LUT=20=E9=AB=98=E6=B8=85=E5=88=86=E5=B1=8F=E9=A2=84?= =?UTF-8?q?=E8=A7=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 大鱼调色/Plugin/DYColorWorkspace.mm | 3 +- 大鱼调色/Plugin/DYDiagnostics.mm | 12 +- 大鱼调色/Plugin/DYGrade.metal | 35 +++- 大鱼调色/Plugin/DYInspector.mm | 3 +- 大鱼调色/Plugin/DYLUTBrowser.h | 1 + 大鱼调色/Plugin/DYLUTBrowser.mm | 192 +++++++++++++++--- 大鱼调色/Plugin/DYLookPreview.h | 3 + 大鱼调色/Plugin/DYManagedKernels.inc.h | 9 + 大鱼调色/Plugin/DYManagedRender.inc.h | 6 +- 大鱼调色/Plugin/DYRenderer.h | 2 + 大鱼调色/Plugin/DYRenderer.mm | 45 +++- 大鱼调色/Plugin/DYToolGrayCapture.inc.h | 75 ++++++- 大鱼调色/Plugin/DYToolHostIvars.inc.h | 2 +- 大鱼调色/Plugin/DYToolHostMethods.inc.h | 31 ++- .../Plugin/DYToolTrackingDiagnostics.inc.h | 37 ++++ 大鱼调色/Plugin/DYToolTrackingProbe.inc.h | 1 + 大鱼调色/Plugin/DYTracking.metal | 24 ++- 大鱼调色/Plugin/DYTrackingPreview.inc.h | 17 +- 大鱼调色/Plugin/Info.plist | 4 +- 大鱼调色/Plugin/MaskCore/DYObjectTracker.hpp | 70 +++++-- 大鱼调色/Wrapper Application/Info.plist | 4 +- 开发交接.md | 6 + 22 files changed, 496 insertions(+), 86 deletions(-) create mode 100644 大鱼调色/Plugin/DYLookPreview.h diff --git a/大鱼调色/Plugin/DYColorWorkspace.mm b/大鱼调色/Plugin/DYColorWorkspace.mm index f18ffbe..b476b74 100644 --- a/大鱼调色/Plugin/DYColorWorkspace.mm +++ b/大鱼调色/Plugin/DYColorWorkspace.mm @@ -235,7 +235,7 @@ static BOOL DYWorkspaceEnabled(DYUniforms u,NSString *type){if([type isEqual:@"b _status.stringValue=[NSString stringWithFormat:@"%@\n%@ · 有效 %@ · 透明 %@ · 非有限 %@ · 超出 −10%%/110%% %@/%@",([data[@"converted"]boolValue]?@"最终画面 · sRGB 显示参考;非 HDR / IRE 精密测量":@"空间未知:显示宿主 RGB 数值参考;不用于 HDR / IRE 测量"),frame,data[@"valid"],data[@"transparent"],data[@"nonfinite"],data[@"below"],data[@"above"]]; }else _status.stringValue=@"示波器采集当前效果的最终输出。播放、移动时间线或调整颜色后更新;透明像素不计入。"; } -- (void)previewChanged:(NSNotification *)notification{if(_visible&&_editorMode==2)[_browser updatePreview:[_renderer lookLUTPreview]];} +- (void)previewChanged:(NSNotification *)notification{if(_visible&&_editorMode==2){NSDictionary *frames=[_renderer lookLUTPreviewFrames];[_browser updatePreview:frames[@"standard"] highResolution:frames[@"high"]];}} - (void)themeChanged:(NSNotification *)notification{[self updatePalette];} - (void)updatePalette{ [_root setNeedsDisplay:YES];for(NSTextField *label in @[_heading,_description,_status,_lutExplanation])label.textColor=DYThemeColor(label==_heading?@"text":@"secondary");[_browser updatePalette:DYWorkspacePalette()]; @@ -245,6 +245,7 @@ static BOOL DYWorkspaceEnabled(DYUniforms u,NSString *type){if([type isEqual:@"b NSString *selected=nil;[action startAction:self];@try{selected=[DYReadAssets([self lutManager],action.currentTime).paths[@"look0"]copy];}@finally{[action endAction:self];} DYWorkspaceLifetime *token=_lifetime; _browser=[[DYLUTBrowser alloc]initWithLibrary:_library preview:[_renderer lookLUTPreview] selectedPath:selected palette:DYWorkspacePalette() selection:^NSProgress *(NSString *path,void(^completion)(NSError *)){DYColorWorkspace *owner=token.owner;if(owner&&!owner->_closed)return [owner selectLUT:path completion:completion];if(completion)completion([NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:nil]);return nil;}];[selected release]; + NSDictionary *frames=[_renderer lookLUTPreviewFrames];[_browser updatePreview:frames[@"standard"] highResolution:frames[@"high"]]; [_browser embedInView:_lutPane close:^{DYColorWorkspace *owner=token.owner;if(owner&&!owner->_closing&&!owner->_closed){owner->_editorMode=0;[owner pageChanged:nil];}}]; } - (NSDictionary *)selectionContextAtTime:(CMTime)time{ diff --git a/大鱼调色/Plugin/DYDiagnostics.mm b/大鱼调色/Plugin/DYDiagnostics.mm index b362175..e9054c5 100644 --- a/大鱼调色/Plugin/DYDiagnostics.mm +++ b/大鱼调色/Plugin/DYDiagnostics.mm @@ -14,6 +14,7 @@ #import "DYColorTables.h" #import "DYLUTLibrary.h" #import "DYLUTBrowser.h" +#include "DYLookPreview.h" #import "DYInspector.h" #import "DYTheme.h" #include "DYSpiderState.h" @@ -424,7 +425,16 @@ extern "C" int DYRunDiagnostics(void) { @autoreleasepool { for(NSUInteger y=0;y<180&&bottomOrientation;y++)bottomOrientation=memcmp((const uint8_t*)bottomLook.bytes+y*320*16,(const uint8_t*)bottomAnalysis.bytes+(179-y)*320*16,320*16)==0; } run.record(@"look-preview-bottom-left-flips-only-browser-rows",bottomOrientation,@{@"error":bottomRendered?@"":reason(error),@"analysisKeepsPhysicalRowOrder":@(bottomOrientation),@"all180RowsCompared":@(bottomOrientation)}); - [orientationRenderer invalidatePreview];run.record(@"preview-invalidation-clears-analysis-and-browser",![orientationRenderer preview]&&![orientationRenderer lookLUTPreview]); + NSData *high=[orientationRenderer lookLUTPreviewFrames][@"high"]; + bool highOrientation=high.length==DYLookPreviewHDWidth*DYLookPreviewHDHeight*16; + if(highOrientation){ + const float *p=static_cast(high.bytes); + const size_t bottom=(DYLookPreviewHDHeight-1)*DYLookPreviewHDWidth*4; + highOrientation=p[0]>p[2]&&p[bottom+2]>p[bottom]; + } + run.record(@"look-preview-hd-640x360-top-first",highOrientation,@{@"bytes":@(high.length)}); + [orientationRenderer invalidatePreview]; + run.record(@"preview-invalidation-clears-analysis-and-browser",![orientationRenderer preview]&&![orientationRenderer lookLUTPreview]&&![[orientationRenderer lookLUTPreviewFrames] count]); [source setDiagnosticImageOrigin:kFxImageOrigin_TOP_LEFT]; [[source metalTextureForDevice:gpu]replaceRegion:MTLRegionMake2D(0,0,width,height) mipmapLevel:0 withBytes:input.data() bytesPerRow:width*8]; DYUniforms cstExport=defaults;cstExport.cm.configured=1;cstExport.cm.exposure=.7f; diff --git a/大鱼调色/Plugin/DYGrade.metal b/大鱼调色/Plugin/DYGrade.metal index 290371a..266095c 100644 --- a/大鱼调色/Plugin/DYGrade.metal +++ b/大鱼调色/Plugin/DYGrade.metal @@ -312,21 +312,38 @@ kernel void dyScopeSample(texture2d source [[texture(0)]], // Native auto-grade statistics consume the input-converted RGB8 preview. // Quantization before and after the LUT mirrors the reference NSImage path. +inline float4 dyInputPreviewPixel(texture2d source, texture3d inputLUT, + float inputStrength, float2 uv, bool quantize) { + constexpr sampler f(coord::normalized,address::clamp_to_edge,filter::linear); + half4 sample=source.sample(f,uv); + float3 linear=float3(sample.rgb); + float3 power=1.055f*pow(max(linear,float3(0)),float3(1.0f/2.4f))-.055f; + float3 encoded=clamp(select(power,linear*12.92f,linear<=.0031308f),0.0f,1.0f); + if(quantize)encoded=round(encoded*255.0f)/255.0f; + float strength=clamp(inputStrength,0.0f,1.0f); + if(strength>0.0f){float n=float(inputLUT.get_width());float3 coord=encoded*((n-1.0f)/n)+.5f/n;encoded=mix(encoded,inputLUT.sample(f,coord).rgb,strength);} + encoded=clamp(encoded,0.0f,1.0f); + if(quantize)encoded=round(encoded*255.0f)/255.0f; + return float4(encoded,float(sample.a)); +} kernel void dyInputAnalysis(texture2d source [[texture(0)]], texture3d inputLUT [[texture(1)]], device float4* outRGBA [[buffer(0)]], constant float& inputStrength [[buffer(1)]], uint2 pixel [[thread_position_in_grid]]) { if (pixel.x >= 320 || pixel.y >= 180) return; - constexpr sampler f(coord::normalized,address::clamp_to_edge,filter::linear); - half4 sample=source.sample(f,(float2(pixel)+.5f)/float2(320,180)); - float3 linear=float3(sample.rgb); - float3 power=1.055f*pow(max(linear,float3(0)),float3(1.0f/2.4f))-.055f; - float3 encoded=round(clamp(select(power,linear*12.92f,linear<=.0031308f),0.0f,1.0f)*255.0f)/255.0f; - float strength=clamp(inputStrength,0.0f,1.0f); - if(strength>0.0f){float n=float(inputLUT.get_width());float3 coord=encoded*((n-1.0f)/n)+.5f/n;encoded=mix(encoded,inputLUT.sample(f,coord).rgb,strength);} - encoded=round(clamp(encoded,0.0f,1.0f)*255.0f)/255.0f; - outRGBA[pixel.y*320+pixel.x]=float4(encoded,float(sample.a)); + outRGBA[pixel.y*320+pixel.x]=dyInputPreviewPixel(source,inputLUT,inputStrength,(float2(pixel)+.5f)/float2(320,180),true); +} + +#include "DYLookPreview.h" +kernel void dyInputHighPreview(texture2d source [[texture(0)]], + texture3d inputLUT [[texture(1)]], device float4* outRGBA [[buffer(0)]], + constant float& inputStrength [[buffer(1)]],constant uint& flip [[buffer(2)]], + uint2 pixel [[thread_position_in_grid]]) { + constexpr uint2 size(DYLookPreviewHDWidth,DYLookPreviewHDHeight); + if(any(pixel>=size))return; + uint2 sample=uint2(pixel.x,flip?size.y-1-pixel.y:pixel.y); + outRGBA[pixel.y*size.x+pixel.x]=dyInputPreviewPixel(source,inputLUT,inputStrength,(float2(sample)+.5f)/float2(size),false); } // CST/BASIS arithmetic is reused verbatim from the versioned dayu-cst source. diff --git a/大鱼调色/Plugin/DYInspector.mm b/大鱼调色/Plugin/DYInspector.mm index fd94f9f..a261fb7 100644 --- a/大鱼调色/Plugin/DYInspector.mm +++ b/大鱼调色/Plugin/DYInspector.mm @@ -675,7 +675,7 @@ NSView *DYCreateWorkspaceMaskEditor(id [(DYCurvePanel *)graphic setHistogram:histogram]; } } -- (void)previewChanged:(NSNotification *)note {[_managedPanel refreshContext];if(_browser.visible)[_browser updatePreview:[_renderer lookLUTPreview]];if(_recentBrowser.visible)[_recentBrowser updatePreview:[_renderer lookLUTPreview]];} +- (void)previewChanged:(NSNotification *)note {[_managedPanel refreshContext];if(_browser.visible){NSDictionary *frames=[_renderer lookLUTPreviewFrames];[_browser updatePreview:frames[@"standard"] highResolution:frames[@"high"]];}if(_recentBrowser.visible)[_recentBrowser updatePreview:[_renderer lookLUTPreview]];} - (void)buildCards { NSInteger index=0; for(NSDictionary *section in _sections) { @@ -1258,6 +1258,7 @@ NSView *DYCreateWorkspaceMaskEditor(id id a=[self actionAPI];if(!a)return;[a startAction:self];NSString *selected=nil;@try{selected=[DYReadAssets(_manager,a.currentTime).paths[sender.identifier]copy];}@finally{[a endAction:self];} __block DYInspector *owner=self;NSString *key=[[sender.identifier copy]autorelease]; _browser=[[DYLUTBrowser alloc]initWithLibrary:_library preview:[_renderer lookLUTPreview] selectedPath:selected palette:DYThemePalette() selection:^NSProgress *(NSString *path,void (^completion)(NSError *)){return [owner setAssetPath:path forKey:key completion:completion];}];[selected release]; + NSDictionary *frames=[_renderer lookLUTPreviewFrames];[_browser updatePreview:frames[@"standard"] highResolution:frames[@"high"]]; // Remote-window APIs must be acquired and invoked inside the custom // parameter action, while the SDK's API permissions are still installed. [a startAction:self]; diff --git a/大鱼调色/Plugin/DYLUTBrowser.h b/大鱼调色/Plugin/DYLUTBrowser.h index 8e6ce39..d5fa83a 100644 --- a/大鱼调色/Plugin/DYLUTBrowser.h +++ b/大鱼调色/Plugin/DYLUTBrowser.h @@ -30,4 +30,5 @@ typedef NSProgress * (^DYLUTSelectionHandler)(NSString *path,void (^completion)( /// Replace the source frame after the renderer captures a new playhead image. /// Passing identical bytes keeps the current thumbnail jobs and cache intact. - (void)updatePreview:(NSData *)rgba; +- (void)updatePreview:(NSData *)rgba highResolution:(NSData *)high; @end diff --git a/大鱼调色/Plugin/DYLUTBrowser.mm b/大鱼调色/Plugin/DYLUTBrowser.mm index 7ef55cb..5b3c799 100644 --- a/大鱼调色/Plugin/DYLUTBrowser.mm +++ b/大鱼调色/Plugin/DYLUTBrowser.mm @@ -1,6 +1,8 @@ #import "DYLUTBrowser.h" +#import "DYLookPreview.h" #import "DYLUTLibrary.h" #import +#include #include static NSColor *DYBrowserColor(NSDictionary *palette, NSString *key) { @@ -13,6 +15,7 @@ static NSColor *DYBrowserColor(NSDictionary *palette, NSString *key) { return [NSColor colorWithWhite:.12 alpha:1]; } +static NSString *const DYBrowserHDPreference=@"com.dayu.color.lookLUTPreview.highResolution"; static NSString *const DYBrowserSizePreference=@"com.dayu.color.lookLUTPreview.thumbnailSize"; static NSString *const DYBrowserFolderPreference=@"com.dayu.color.lookLUTPreview.selectedFolder"; static NSString *const DYBrowserFramePreference=@"com.dayu.color.lookLUTPreview.windowFrame"; @@ -134,6 +137,60 @@ static void DYBrowserGeometrySample(NSView *root,NSString *phase){ } @end +@interface DYBrowserCompareView : NSView +@property(nonatomic) CGFloat position; +@property(nonatomic,assign) NSSlider *slider; +- (void)setOriginal:(NSImage *)original styled:(NSImage *)styled; +@end + +@implementation DYBrowserCompareView { + NSImage *_original,*_styled; +} +- (BOOL)isFlipped{return YES;} +- (void)dealloc{[_original release];[_styled release];[super dealloc];} +- (void)setOriginal:(NSImage *)original styled:(NSImage *)styled{ + if(_original!=original){[_original release];_original=[original retain];} + if(_styled!=styled){[_styled release];_styled=[styled retain];} + [self setNeedsDisplay:YES]; +} +- (void)setPosition:(CGFloat)position{ + _position=std::clamp(position,(CGFloat)0,(CGFloat)1); + self.slider.doubleValue=_position; + [self setNeedsDisplay:YES]; +} +- (NSRect)pictureRect{ + if(!_original)return self.bounds; + CGFloat scale=MIN(NSWidth(self.bounds)/_original.size.width,NSHeight(self.bounds)/_original.size.height); + NSSize size=NSMakeSize(_original.size.width*scale,_original.size.height*scale); + return NSMakeRect(NSMidX(self.bounds)-size.width*.5,NSMidY(self.bounds)-size.height*.5,size.width,size.height); +} +- (void)moveDivider:(NSEvent *)event{ + if(!_styled||!self.slider.enabled)return; + NSRect picture=[self pictureRect]; + if(NSWidth(picture)>0)self.position=([self convertPoint:event.locationInWindow fromView:nil].x-NSMinX(picture))/NSWidth(picture); +} +- (void)mouseDown:(NSEvent *)event{[self moveDivider:event];} +- (void)mouseDragged:(NSEvent *)event{[self moveDivider:event];} +- (void)drawRect:(NSRect)dirty{ + [NSColor.blackColor setFill];NSRectFill(NSIntersectionRect(dirty,self.bounds)); + if(!_original)return; + NSRect picture=[self pictureRect]; + [_original drawInRect:picture fromRect:NSZeroRect operation:NSCompositingOperationSourceOver fraction:1 respectFlipped:YES hints:nil]; + if(!_styled||!self.slider.enabled)return; + CGFloat divider=NSMinX(picture)+NSWidth(picture)*_position; + [NSGraphicsContext saveGraphicsState]; + NSRect styledArea=NSMakeRect(divider,NSMinY(picture),NSMaxX(picture)-divider,NSHeight(picture)); + NSRectClip(styledArea); + [NSColor.blackColor setFill];NSRectFill(styledArea); + [_styled drawInRect:picture fromRect:NSZeroRect operation:NSCompositingOperationSourceOver fraction:1 respectFlipped:YES hints:nil]; + [NSGraphicsContext restoreGraphicsState]; + if(_position>0&&_position<1){ + [[NSColor blackColor] setFill];NSRectFill(NSMakeRect(divider-1.5,NSMinY(picture),3,NSHeight(picture))); + [[NSColor whiteColor] setFill];NSRectFill(NSMakeRect(divider-.5,NSMinY(picture),1,NSHeight(picture))); + } +} +@end + @class DYLUTBrowser; /// The jobs own this token, never the browser or its host selection block. @interface DYBrowserWork : NSObject @@ -144,7 +201,7 @@ static void DYBrowserGeometrySample(NSView *root,NSString *phase){ @interface DYLUTBrowser () { DYLUTLibrary *_library; - NSData *_preview; + NSData *_preview,*_previewHD; NSString *_selectedPath,*_folder; NSDictionary *_palette; DYLUTSelectionHandler _selection; @@ -155,15 +212,18 @@ static void DYBrowserGeometrySample(NSView *root,NSString *phase){ NSScrollView *_folderScroll,*_gridScroll; NSTextField *_titleLabel,*_countLabel,*_statusLabel,*_sizeLabel,*_emptyLabel; NSSearchField *_search; - NSButton *_importFiles,*_importFolder,*_reveal,*_refresh,*_none,*_done; - NSSlider *_sizeSlider; + NSButton *_importFiles,*_importFolder,*_reveal,*_refresh,*_none,*_done,*_hd; + NSSlider *_sizeSlider,*_compareSlider; + DYBrowserCanvas *_comparePanel; + DYBrowserCompareView *_compareView; + NSTextField *_compareTitle,*_compareStatus,*_originalLabel,*_styledLabel; NSMutableArray *_folderButtons; NSMutableDictionary *_tiles; NSArray *_visibleEntries,*_folderPaths; NSMutableSet *_queuedPaths; NSCache *_thumbnailCache; - dispatch_queue_t _previewQueue; - DYBrowserWork *_work; + dispatch_queue_t _previewQueue,_compareQueue; + DYBrowserWork *_work,*_compareWork; BOOL _closed,_sizing,_visible,_remoteMode,_importing,_layingOut,_detached,_embedded,_compactRecent; NSArray *_recentSnapshot; NSProgress *_libraryTask,*_selectionTask; @@ -209,6 +269,21 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b _none=[BrowserButton(@"无 LUT",self,@selector(clearSelection:))retain]; _done=[BrowserButton(@"完成",self,@selector(done:))retain];_done.keyEquivalent=@"\r"; NSUserDefaults *preferences=NSUserDefaults.standardUserDefaults; + _hd=[[NSButton checkboxWithTitle:@"高清预览" target:self action:@selector(qualityChanged:)]retain]; + _hd.font=[NSFont systemFontOfSize:11];_hd.state=[preferences boolForKey:DYBrowserPreference(DYBrowserHDPreference,library)]?NSControlStateValueOn:NSControlStateValueOff; + _hd.toolTip=@"从源帧取样并以双倍像素生成缩略图"; + _comparePanel=[[DYBrowserCanvas alloc]initWithFrame:NSZeroRect];_comparePanel.fillKey=@"card"; + _compareTitle=[BrowserLabel(@"原图与 LUT 对比",13)retain];_compareTitle.font=[NSFont systemFontOfSize:13 weight:NSFontWeightSemibold]; + _compareStatus=[BrowserLabel(@"等待播放头画面",11)retain]; + _originalLabel=[BrowserLabel(@"原图",11)retain];_originalLabel.toolTip=@"同一帧加载此 LUT 之前的画面"; + _styledLabel=[BrowserLabel(@"LUT 效果 · 100%",11)retain];_styledLabel.alignment=NSTextAlignmentRight; + _compareView=[[DYBrowserCompareView alloc]initWithFrame:NSZeroRect];_compareView.accessibilityLabel=@"原图与 LUT 滑动对比图"; + _compareSlider=[[NSSlider alloc]initWithFrame:NSZeroRect];_compareSlider.minValue=0;_compareSlider.maxValue=1;_compareSlider.continuous=YES;_compareSlider.enabled=NO; + _compareSlider.target=self;_compareSlider.action=@selector(comparePositionChanged:);_compareSlider.accessibilityLabel=@"原图与 LUT 对比分界线"; + _compareView.slider=_compareSlider;_compareView.position=.5; + for(NSView *view in @[_compareTitle,_compareStatus,_compareView,_compareSlider,_originalLabel,_styledLabel])[_comparePanel addSubview:view]; + _compareQueue=dispatch_queue_create("com.dayu.color.lut-comparison",DISPATCH_QUEUE_SERIAL); + dispatch_set_target_queue(_compareQueue,dispatch_get_global_queue(QOS_CLASS_USER_INITIATED,0)); NSNumber *storedSize=[preferences objectForKey:DYBrowserPreference(DYBrowserSizePreference,library)]?:[preferences objectForKey:DYBrowserSizePreference];double size=storedSize?storedSize.doubleValue:120; _layoutMenu=[[NSPopUpButton alloc]initWithFrame:NSZeroRect pullsDown:NO];[_layoutMenu addItemsWithTitles:@[@"网格",@"列表"]];_layoutMenu.font=[NSFont systemFontOfSize:11];_layoutMenu.target=self;_layoutMenu.action=@selector(layoutChanged:);_layoutMenu.accessibilityLabel=@"LUT 浏览布局"; [_layoutMenu selectItemAtIndex:[preferences integerForKey:DYBrowserPreference(DYBrowserLayoutPreference,library)]==1?1:0]; @@ -225,7 +300,7 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(gridScrolled:) name:NSViewBoundsDidChangeNotification object:_gridScroll.contentView]; _splitView=[[NSSplitView alloc]initWithFrame:NSMakeRect(12,80,736,425)];_splitView.vertical=YES;_splitView.dividerStyle=NSSplitViewDividerStyleThin;_splitView.accessibilityLabel=@"调整 LUT 文件夹与预览宽度"; _folderScroll.frame=NSMakeRect(0,0,162,425);_gridScroll.frame=NSMakeRect(163,0,573,425);[_splitView addSubview:_folderScroll];[_splitView addSubview:_gridScroll]; - for(NSView *view in @[_titleLabel,_countLabel,_statusLabel,_sizeLabel,_search,_importFiles,_importFolder,_reveal,_refresh,_none,_done,_sizeSlider,_layoutMenu,_splitView])[_root addSubview:view]; + for(NSView *view in @[_titleLabel,_countLabel,_statusLabel,_sizeLabel,_search,_importFiles,_importFolder,_reveal,_refresh,_none,_done,_hd,_sizeSlider,_layoutMenu,_splitView,_comparePanel])[_root addSubview:view]; [_gridDocument addSubview:_emptyLabel]; _root.postsFrameChangedNotifications=YES;_root.postsBoundsChangedNotifications=YES; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(browserRootResized:) name:NSViewFrameDidChangeNotification object:_root]; @@ -240,12 +315,15 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b [_libraryTask cancel];[_libraryTask release];[_selectionTask cancel];[_selectionTask release];[_progressTimer invalidate];[_progressTimer release]; _work.cancelled=YES;_work.owner=nil;[_work release];self.window.delegate=nil; _popover.delegate=nil;[_popover close];[_popover release];[_root removeFromSuperview]; - [_library release];[_preview release];[_selectedPath release];[_folder release];[_palette release];[_selection release];[_closeAction release]; + _compareWork.cancelled=YES;_compareWork.owner=nil;[_compareWork release]; + [_library release];[_preview release];[_previewHD release];[_selectedPath release];[_folder release];[_palette release];[_selection release];[_closeAction release]; [_root release];[_folderDocument release];[_gridDocument release];[_folderScroll release];[_gridScroll release]; [_titleLabel release];[_countLabel release];[_statusLabel release];[_sizeLabel release];[_emptyLabel release];[_search release]; [_importFiles release];[_importFolder release];[_reveal release];[_refresh release];[_none release];[_done release];[_sizeSlider release];[_layoutMenu release];_splitView.delegate=nil;[_splitView release];[_presentationWindow release]; [_folderButtons release];[_folderPaths release];[_tiles release];[_visibleEntries release];[_queuedPaths release];[_thumbnailCache release]; - dispatch_release(_previewQueue);[super dealloc]; + _compareView.slider=nil;[_compareView release];[_compareSlider release];[_comparePanel release];[_hd release]; + [_compareTitle release];[_compareStatus release];[_originalLabel release];[_styledLabel release]; + dispatch_release(_compareQueue);dispatch_release(_previewQueue);[super dealloc]; } - (void)showRelativeToView:(NSView *)view{ if(_closed||_remoteMode||!view.window||_popover.shown)return; @@ -254,7 +332,7 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b _popover=[[NSPopover alloc]init];_popover.behavior=NSPopoverBehaviorSemitransient;_popover.animates=YES;_popover.delegate=self;_popover.contentSize=size; NSViewController *controller=[[[NSViewController alloc]init]autorelease];controller.title=@"风格 LUT";controller.view=_root;_popover.contentViewController=controller; [self layoutBrowser]; - @try {[_popover showRelativeToRect:view.bounds ofView:view preferredEdge:NSRectEdgeMaxX];_visible=_popover.shown;} + @try {[_popover showRelativeToRect:view.bounds ofView:view preferredEdge:NSRectEdgeMaxX];_visible=_popover.shown;[self refreshComparison];} @catch(NSException *exception){[self finishClosing];} } - (BOOL)visible{return !_closed&&((_remoteMode||_detached)?_visible:_popover.shown);} @@ -270,7 +348,7 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b [parent addSubview:_root];DYBrowserGeometrySample(_root,@"before-content-origin-alignment");[self fitRemoteParent]; DYBrowserGeometrySample(_root,@"attached-before-sdk-reply"); // Attach synchronously: the SDK reads the first child before reply returns. - dispatch_async(dispatch_get_main_queue(),^{if(!_closed){[self configurePresentationWindow];DYBrowserGeometrySample(_root,@"after-sdk-reply");}}); + dispatch_async(dispatch_get_main_queue(),^{if(!_closed){[self configurePresentationWindow];[self refreshComparison];DYBrowserGeometrySample(_root,@"after-sdk-reply");}}); if([NSUserDefaults.standardUserDefaults boolForKey:@"DYBrowserGeometryDiagnostics"])dispatch_after(dispatch_time(DISPATCH_TIME_NOW,NSEC_PER_SEC),dispatch_get_main_queue(),^{if(!_closed)DYBrowserGeometrySample(_root,@"settled");}); NSAccessibilityPostNotification(parent,NSAccessibilityLayoutChangedNotification); } @@ -283,7 +361,7 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b _root.accessibilityLabel=@"最近使用 LUT 快捷入口"; _titleLabel.stringValue=@"最近使用";_titleLabel.font=[NSFont systemFontOfSize:11 weight:NSFontWeightSemibold]; [_layoutMenu selectItemAtIndex:0];_sizeSlider.doubleValue=120; - for(NSView *view in @[_search,_importFiles,_importFolder,_reveal,_refresh,_none,_done,_sizeLabel,_sizeSlider,_layoutMenu,_splitView])view.hidden=YES; + for(NSView *view in @[_search,_importFiles,_importFolder,_reveal,_refresh,_none,_done,_hd,_comparePanel,_sizeLabel,_sizeSlider,_layoutMenu,_splitView])view.hidden=YES; [_root addSubview:_gridScroll];_gridScroll.hasHorizontalScroller=NO;_gridScroll.scrollerStyle=NSScrollerStyleOverlay; [self showInRemoteParent:parent close:nil];[self refreshRecentSelection:_selectedPath]; } @@ -366,7 +444,7 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b // The API owner's completion may release this controller synchronously. [self retain]; [NSObject cancelPreviousPerformRequestsWithTarget:self]; - _closed=YES;_visible=NO;_work.cancelled=YES;_work.owner=nil;[_libraryTask cancel];[_selectionTask cancel];[_progressTimer invalidate];[_progressTimer release];_progressTimer=nil; + _closed=YES;_visible=NO;_work.cancelled=YES;_work.owner=nil;_compareWork.cancelled=YES;_compareWork.owner=nil;[_libraryTask cancel];[_selectionTask cancel];[_progressTimer invalidate];[_progressTimer release];_progressTimer=nil; _popover.delegate=nil;if(_popover.shown)[_popover close]; if(_remoteMode&&_root.superview)for(NSNotificationName name in @[NSViewFrameDidChangeNotification,NSViewBoundsDidChangeNotification])[[NSNotificationCenter defaultCenter]removeObserver:self name:name object:_root.superview]; [_root removeFromSuperview];[_queuedPaths removeAllObjects];[_thumbnailCache removeAllObjects]; @@ -387,7 +465,8 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b _reveal.frame=NSMakeRect(w-153,9,139,27);_none.frame=NSMakeRect(14,h-44,75,27);_layoutMenu.frame=NSMakeRect(100,h-44,97,27); _sizeLabel.frame=NSMakeRect(211,h-39,74,18);_sizeSlider.frame=NSMakeRect(288,h-41,MIN(150,MAX(80,w-550)),20); _done.frame=NSMakeRect(w-98,h-44,78,27); - CGFloat statusX=NSMaxX(_sizeSlider.frame)+14;_statusLabel.frame=NSMakeRect(statusX,h-39,MAX(0,w-statusX-112),18); + CGFloat hdX=NSMaxX(_sizeSlider.frame)+12;_hd.frame=NSMakeRect(hdX,h-43,86,24); + CGFloat statusX=NSMaxX(_hd.frame)+6;_statusLabel.frame=NSMakeRect(statusX,h-39,MAX(0,w-statusX-112),18); if(_embedded&&!_compactRecent){ _search.frame=NSMakeRect(12,36,MAX(125,w-310),24); _importFiles.frame=NSMakeRect(w-284,34,88,28);_importFolder.frame=NSMakeRect(w-192,34,100,28);_refresh.frame=NSMakeRect(w-88,34,76,28); @@ -395,8 +474,28 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b _reveal.hidden=w<620;_titleLabel.frame=NSMakeRect(12,8,152,20);_countLabel.frame=NSMakeRect(166,10,100,18); _none.frame=NSMakeRect(10,h-34,70,27);_layoutMenu.frame=NSMakeRect(86,h-34,80,27);_done.frame=NSMakeRect(w-88,h-34,78,27); BOOL small=w<650;_sizeLabel.hidden=small;_sizeSlider.hidden=small;_statusLabel.hidden=small; - if(!small){_sizeLabel.frame=NSMakeRect(174,h-29,68,18);_sizeSlider.frame=NSMakeRect(246,h-31,100,20);_statusLabel.frame=NSMakeRect(354,h-29,MAX(0,w-454),18);} + _hd.frame=NSMakeRect(small?174:352,h-33,86,24); + if(!small){_sizeLabel.frame=NSMakeRect(174,h-29,68,18);_sizeSlider.frame=NSMakeRect(246,h-31,100,20);_statusLabel.frame=NSMakeRect(444,h-29,MAX(0,w-540),18);} } + NSRect browser=_splitView.frame,comparison=browser; + if(w>=620){ + CGFloat width=MIN(560,w*.42); + browser.size.width=MAX(1,NSWidth(browser)-width-10); + comparison.origin.x=NSMaxX(browser)+10;comparison.size.width=width; + }else{ + CGFloat height=MIN(NSHeight(browser)*.52,235); + browser.size.height=MAX(1,NSHeight(browser)-height-8); + comparison.origin.y=NSMaxY(browser)+8;comparison.size.height=height; + } + _splitView.frame=browser;[_splitView adjustSubviews]; + CGFloat left=MAX(100,MIN(_sidebarWidth,NSWidth(browser)*.41));[_splitView setPosition:left ofDividerAtIndex:0]; + _comparePanel.frame=comparison; + CGFloat cw=NSWidth(comparison),ch=NSHeight(comparison),margin=10,body=MAX(1,cw-2*margin); + _compareTitle.frame=NSMakeRect(margin,6,body,20);_compareStatus.frame=NSMakeRect(margin,25,body,16); + _compareView.frame=NSMakeRect(margin,46,body,MAX(1,ch-88)); + _compareSlider.frame=NSMakeRect(margin,MAX(47,ch-40),body,18); + _originalLabel.frame=NSMakeRect(margin,MAX(66,ch-20),body*.44,15); + _styledLabel.frame=NSMakeRect(margin+body*.44,MAX(66,ch-20),body*.56,15); } if(_compactRecent){ _titleLabel.frame=NSMakeRect(0,2,92,18);_countLabel.frame=NSMakeRect(94,2,100,18); @@ -443,23 +542,60 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b NSColor *background=[palette[@"background"]colorUsingColorSpace:NSColorSpace.sRGBColorSpace]; BOOL light=background&&background.redComponent>.7&&background.greenComponent>.7; // Keep the supplied neutral surface in both relief themes. - [_palette release];_palette=[browserPalette copy];_root.palette=_palette;_folderDocument.palette=_palette;_gridDocument.palette=_palette; + [_palette release];_palette=[browserPalette copy];_root.palette=_palette;_folderDocument.palette=_palette;_gridDocument.palette=_palette;_comparePanel.palette=_palette; self.window.backgroundColor=DYBrowserColor(_palette,@"background");self.window.appearance=[NSAppearance appearanceNamed:light?NSAppearanceNameAqua:NSAppearanceNameDarkAqua]; _root.appearance=[NSAppearance appearanceNamed:light?NSAppearanceNameAqua:NSAppearanceNameDarkAqua]; - for(NSTextField *label in @[_titleLabel,_countLabel,_statusLabel,_sizeLabel,_emptyLabel])label.textColor=DYBrowserColor(_palette,label==_titleLabel?@"text":@"secondary"); + for(NSTextField *label in @[_titleLabel,_countLabel,_statusLabel,_sizeLabel,_emptyLabel,_compareTitle,_compareStatus,_originalLabel,_styledLabel])label.textColor=DYBrowserColor(_palette,label==_titleLabel?@"text":@"secondary"); for(NSButton *button in @[_importFiles,_importFolder,_reveal,_refresh,_none,_done,_layoutMenu])button.contentTintColor=DYBrowserColor(_palette,@"accent"); _search.textColor=DYBrowserColor(_palette,@"text");_search.backgroundColor=DYBrowserColor(_palette,@"field"); for(DYBrowserFolderButton *button in _folderButtons){button.palette=_palette;[button setNeedsDisplay:YES];} for(DYBrowserTile *tile in _tiles.allValues){tile.palette=_palette;[tile setNeedsDisplay:YES];} [_root setNeedsDisplay:YES];[_folderDocument setNeedsDisplay:YES];[_gridDocument setNeedsDisplay:YES]; } -- (void)updatePreview:(NSData *)rgba{ - if(_closed||_preview==rgba||[_preview isEqualToData:rgba])return; - if(rgba&&rgba.length!=320*180*16)return; - [_preview release];_preview=[rgba copy];[_thumbnailCache removeAllObjects]; +- (void)updatePreview:(NSData *)rgba{[self updatePreview:rgba highResolution:nil];} +- (void)updatePreview:(NSData *)rgba highResolution:(NSData *)high{ + if(_closed||(rgba&&rgba.length!=320*180*16)||(high&&high.length!=DYLookPreviewHDWidth*DYLookPreviewHDHeight*16))return; + BOOL standardChanged=_preview!=rgba&&![_preview isEqualToData:rgba]; + BOOL highChanged=_previewHD!=high&&![_previewHD isEqualToData:high]; + if(!standardChanged&&!highChanged)return; + [_preview release];_preview=[rgba retain]; + [_previewHD release];_previewHD=[high retain]; + if(standardChanged||(highChanged&&_hd.state==NSControlStateValueOn)){ + [_thumbnailCache removeAllObjects]; + [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(refreshThumbnailSize) object:nil]; + [self refreshThumbnailSize]; + } + [self refreshComparison]; +} +- (void)qualityChanged:(id)sender{ + [NSUserDefaults.standardUserDefaults setBool:_hd.state==NSControlStateValueOn forKey:DYBrowserPreference(DYBrowserHDPreference,_library)]; [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(refreshThumbnailSize) object:nil]; [self refreshThumbnailSize]; } +- (void)comparePositionChanged:(id)sender{_compareView.position=_compareSlider.doubleValue;} +- (void)refreshComparison{ + if(_closed||_compactRecent||!self.visible)return; + _compareWork.cancelled=YES;_compareWork.owner=nil;[_compareWork release];_compareWork=nil; + NSData *frame=_previewHD?:_preview; + if(!frame){[_compareView setOriginal:nil styled:nil];_compareSlider.enabled=NO;_compareStatus.stringValue=@"等待播放头画面";return;} + NSUInteger w=_previewHD?DYLookPreviewHDWidth:320,h=_previewHD?DYLookPreviewHDHeight:180; + NSString *path=_selectedPath; + NSString *name=path.lastPathComponent.stringByDeletingPathExtension; + _compareWork=[DYBrowserWork new];_compareWork.owner=self;DYBrowserWork *work=_compareWork; + dispatch_async(_compareQueue,^{@autoreleasepool{ + if(work.cancelled)return; + NSImage *original=DYLUTThumbnail(nil,frame,w,h,NSMakeSize(w,h),nil); + if(work.cancelled)return; + NSImage *styled=path.length?DYLUTThumbnail(path,frame,w,h,NSMakeSize(w,h),nil):nil; + dispatch_async(dispatch_get_main_queue(),^{ + DYLUTBrowser *owner=work.owner;if(work.cancelled||!owner)return; + owner->_compareSlider.enabled=styled!=nil&&original!=nil; + [owner->_compareView setOriginal:original styled:styled]; + owner->_compareStatus.stringValue=path.length?(styled?name:@"无法预览当前 LUT"):@"选择一个 LUT 查看对比"; + owner->_compareStatus.toolTip=owner->_compareStatus.stringValue; + }); + }}); +} - (void)setStatus:(NSString *)status{_statusLabel.stringValue=status?:@"";_statusLabel.toolTip=status;} - (void)rebuildFolders{ NSMutableArray *paths=[NSMutableArray arrayWithObjects:DYBrowserRecentFolder,@"*",nil];NSMutableSet *seen=[NSMutableSet setWithArray:paths]; @@ -499,7 +635,7 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b [_thumbnailCache removeAllObjects];NSUInteger generation=++_libraryGeneration; NSProgress *task=[_library reloadAsync:^(NSError *error){ if(_closed||generation!=_libraryGeneration)return;[self endLibraryTask]; - [self rebuildFolders];[self rebuildGrid];[self setStatus:error?error.localizedDescription:@"单击缩略图应用 LUT"]; + [self rebuildFolders];[self rebuildGrid];[self refreshComparison];[self setStatus:error?error.localizedDescription:@"单击缩略图应用 LUT"]; }];[self beginLibraryTask:task]; } - (void)folderClicked:(NSButton *)sender{ @@ -526,19 +662,23 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b - (void)generateVisibleThumbnails{ if(_closed||!_work)return; NSRect visible=NSInsetRect(_gridScroll.contentView.bounds,-30,-180); + BOOL hd=!_compactRecent&&_hd.state==NSControlStateValueOn; NSUInteger width=MAX((NSUInteger)64,(NSUInteger)llround(_sizeSlider.doubleValue)),height=MAX((NSUInteger)40,(NSUInteger)llround(width*9./16)); + if(hd){width*=2;height*=2;} + NSData *frame=hd&&_previewHD?_previewHD:_preview; + NSUInteger sourceWidth=hd&&_previewHD?DYLookPreviewHDWidth:320,sourceHeight=hd&&_previewHD?DYLookPreviewHDHeight:180; for(NSString *path in _tiles.allKeys){ DYBrowserTile *tile=_tiles[path]; if(!NSIntersectsRect(tile.frame,visible)){if(tile.thumbnail){tile.thumbnail=nil;tile.enabled=NO;}continue;} if(tile.thumbnail||tile.failure.length)continue; - NSString *cacheKey=[NSString stringWithFormat:@"%lu|%@",(unsigned long)width,path]; + NSString *cacheKey=[NSString stringWithFormat:@"%d|%lu|%@",hd,(unsigned long)width,path]; NSDictionary *cached=[_thumbnailCache objectForKey:cacheKey]; if(cached){tile.thumbnail=cached[@"image"];tile.failure=cached[@"error"];tile.enabled=tile.thumbnail!=nil;if(tile.failure.length)tile.toolTip=tile.failure;[tile setNeedsDisplay:YES];continue;} if([_queuedPaths containsObject:cacheKey])continue;[_queuedPaths addObject:cacheKey]; - DYBrowserWork *work=_work;NSData *preview=_preview; + DYBrowserWork *work=_work; dispatch_async(_previewQueue,^{@autoreleasepool{ if(work.cancelled)return; - NSError *error=nil;NSImage *image=DYLUTThumbnail(path,preview,320,180,NSMakeSize(width,height),&error);NSString *failure=error.localizedDescription; + NSError *error=nil;NSImage *image=DYLUTThumbnail(path,frame,sourceWidth,sourceHeight,NSMakeSize(width,height),&error);NSString *failure=error.localizedDescription; dispatch_async(dispatch_get_main_queue(),^{ DYLUTBrowser *owner=work.owner;if(work.cancelled||!owner)return; [owner->_queuedPaths removeObject:cacheKey]; @@ -573,7 +713,7 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b void (^applied)(NSError *)=^(NSError *error){ if(_closed||generation!=_selectionGeneration)return;[_selectionTask release];_selectionTask=nil; if(error){[self setStatus:error.localizedDescription];return;} - [_selectedPath release];_selectedPath=[path copy];[_library recordUsedPath:path];[self rebuildGrid]; + [_selectedPath release];_selectedPath=[path copy];[_library recordUsedPath:path];[self rebuildGrid];[self refreshComparison]; for(DYBrowserTile *tile in _tiles.allValues){tile.chosen=[tile.identifier isEqualToString:path];[tile setNeedsDisplay:YES];} [self setStatus:[@"已选择 " stringByAppendingString:name]];if(closeAfter)[self close]; }; @@ -585,7 +725,7 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b void (^cleared)(NSError *)=^(NSError *error){ if(_closed||generation!=_selectionGeneration)return;[_selectionTask release];_selectionTask=nil; if(error){[self setStatus:error.localizedDescription];return;} - [_selectedPath release];_selectedPath=nil; + [_selectedPath release];_selectedPath=nil;[self refreshComparison]; for(DYBrowserTile *tile in _tiles.allValues){tile.chosen=NO;[tile setNeedsDisplay:YES];} [self setStatus:@"已移除 LUT"]; }; diff --git a/大鱼调色/Plugin/DYLookPreview.h b/大鱼调色/Plugin/DYLookPreview.h new file mode 100644 index 0000000..ebd2d48 --- /dev/null +++ b/大鱼调色/Plugin/DYLookPreview.h @@ -0,0 +1,3 @@ +#pragma once +// Full browser source sampling. The 320×180 auto-grade buffer stays separate. +enum { DYLookPreviewHDWidth=640, DYLookPreviewHDHeight=360 }; diff --git a/大鱼调色/Plugin/DYManagedKernels.inc.h b/大鱼调色/Plugin/DYManagedKernels.inc.h index 139b8e8..6929100 100644 --- a/大鱼调色/Plugin/DYManagedKernels.inc.h +++ b/大鱼调色/Plugin/DYManagedKernels.inc.h @@ -26,3 +26,12 @@ kernel void dyManagedCarrier(texture2d a [[texture(0)]],text fragment float4 dyManagedCopyFragment(DYRaster in [[stage_in]],texture2d source [[texture(0)]]){constexpr sampler f(coord::normalized,address::clamp_to_edge,filter::linear);return source.sample(f,in.uv);} kernel void dyManagedPreview(texture2d a [[texture(0)]],device float4* out [[buffer(0)]],uint2 p [[thread_position_in_grid]]){if(any(p>=uint2(320,180)))return;constexpr sampler s(coord::normalized,address::clamp_to_edge,filter::linear);float4 c=a.sample(s,(float2(p)+.5f)/float2(320,180));out[p.y*320+p.x]=float4(c.a>0?dyEncode(c.rgb/c.a):float3(0),c.a);} +kernel void dyManagedHighPreview(texture2d a [[texture(0)]],device float4* out [[buffer(0)]], + constant uint& flip [[buffer(2)]],uint2 p [[thread_position_in_grid]]){ + constexpr uint2 size(DYLookPreviewHDWidth,DYLookPreviewHDHeight); + if(any(p>=size))return; + uint2 sample=uint2(p.x,flip?size.y-1-p.y:p.y); + constexpr sampler s(coord::normalized,address::clamp_to_edge,filter::linear); + float4 c=a.sample(s,(float2(sample)+.5f)/float2(size)); + out[p.y*size.x+p.x]=float4(c.a>0?dyEncode(c.rgb/c.a):float3(0),c.a); +} diff --git a/大鱼调色/Plugin/DYManagedRender.inc.h b/大鱼调色/Plugin/DYManagedRender.inc.h index d9bee86..1c8fea8 100644 --- a/大鱼调色/Plugin/DYManagedRender.inc.h +++ b/大鱼调色/Plugin/DYManagedRender.inc.h @@ -2,8 +2,10 @@ @synchronized(_previewLock){if(_exporting||CACurrentMediaTime()-_lastPreview<=.5)return;} idp=[self computePipeline:@"dyManagedPreview" entry:entry error:nil];if(!p)return; idb=[entry[@"device"]newBufferWithLength:320*180*16 options:MTLResourceStorageModeShared];if(!b)return; - idcb=[entry[@"queue"]commandBuffer];idce=[cb computeCommandEncoder];[ce setComputePipelineState:p];[ce setTexture:texture atIndex:0];[ce setBuffer:b offset:0 atIndex:0];[ce dispatchThreads:MTLSizeMake(320,180,1) threadsPerThreadgroup:MTLSizeMake(8,8,1)];[ce endEncoding];[cb commit];[cb waitUntilCompleted]; - if(cb.status==MTLCommandBufferStatusCompleted){@synchronized(_previewLock){[_preview release];_preview=[[NSData alloc]initWithBytes:b.contents length:b.length];[_lookLUTPreview release];if(origin==kFxImageOrigin_BOTTOM_LEFT){NSMutableData *flipped=[NSMutableData dataWithLength:b.length];for(NSUInteger y=0;y<180;y++)memcpy((uint8_t*)flipped.mutableBytes+y*320*16,(uint8_t*)b.contents+(179-y)*320*16,320*16);_lookLUTPreview=[flipped copy];}else _lookLUTPreview=[_preview retain];_lastPreview=CACurrentMediaTime();}dispatch_async(dispatch_get_main_queue(),^{[NSNotificationCenter.defaultCenter postNotificationName:@"DYPreviewDidChange" object:self];});}[b release]; + idcb=[entry[@"queue"]commandBuffer];idce=[cb computeCommandEncoder];[ce setComputePipelineState:p];[ce setTexture:texture atIndex:0];[ce setBuffer:b offset:0 atIndex:0];[ce dispatchThreads:MTLSizeMake(320,180,1) threadsPerThreadgroup:MTLSizeMake(8,8,1)];[ce endEncoding]; + idlookHD=[self encodeLookHD:texture inputLUT:nil strength:0 origin:origin entry:entry commands:cb]; + [cb commit];[cb waitUntilCompleted]; + if(cb.status==MTLCommandBufferStatusCompleted){@synchronized(_previewLock){[_preview release];_preview=[[NSData alloc]initWithBytes:b.contents length:b.length];[_lookLUTPreview release];if(origin==kFxImageOrigin_BOTTOM_LEFT){NSMutableData *flipped=[NSMutableData dataWithLength:b.length];for(NSUInteger y=0;y<180;y++)memcpy((uint8_t*)flipped.mutableBytes+y*320*16,(uint8_t*)b.contents+(179-y)*320*16,320*16);_lookLUTPreview=[flipped copy];}else _lookLUTPreview=[_preview retain];[self replaceLookHDBuffer:lookHD];_lastPreview=CACurrentMediaTime();}dispatch_async(dispatch_get_main_queue(),^{[NSNotificationCenter.defaultCenter postNotificationName:@"DYPreviewDidChange" object:self];});}[b release]; } // Included in DYRenderer. V1 callers and their ABI remain on renderSource:...tools:error:. - (NSDictionary*)colorContext {@synchronized(_previewLock){return [[_colorContext copy]autorelease];}} diff --git a/大鱼调色/Plugin/DYRenderer.h b/大鱼调色/Plugin/DYRenderer.h index fc8f7be..a99a31f 100644 --- a/大鱼调色/Plugin/DYRenderer.h +++ b/大鱼调色/Plugin/DYRenderer.h @@ -27,6 +27,8 @@ /// Display-encoded float RGBA, 320×180, with the visually top row first. /// Analysis preview retains the host texture's original row order separately. - (NSData*)lookLUTPreview; +/// Standard and HD samples from the same capture, under one lock. +- (NSDictionary*)lookLUTPreviewFrames; - (void)invalidatePreview; /// Conversion/statistics run while visible; retain one small final-output /// sample per instance so opening a paused workspace needs no parameter edit. diff --git a/大鱼调色/Plugin/DYRenderer.mm b/大鱼调色/Plugin/DYRenderer.mm index f33005c..3696ccc 100644 --- a/大鱼调色/Plugin/DYRenderer.mm +++ b/大鱼调色/Plugin/DYRenderer.mm @@ -1,3 +1,4 @@ +#import "DYLookPreview.h" #include "DYScopeDisplayTime.h" #import #import "DYManagedColor.h" @@ -139,7 +140,7 @@ struct DYScopeReadback { dispatch_queue_t _lutPreparationQueue; DYColorTables *_tables; NSObject *_previewLock; - NSData *_preview,*_lookLUTPreview; + NSData *_preview,*_lookLUTPreview,*_lookLUTPreviewHD; CFTimeInterval _lastPreview; BOOL _exporting; NSDictionary *_toolDocument,*_colorContext,*_exportManagedState; @@ -183,11 +184,39 @@ struct DYScopeReadback { #include "DYPrimaryRender.inc.h" #include "DYUnifiedRender.inc.h" #include "DYMosaicRender.inc.h" +// HD is rendered alongside the existing throttled source sample. Its +// immutable shared buffer stays available when the playhead is paused. +- (id)encodeLookHD:(id)texture inputLUT:(id)lut strength:(float)strength + origin:(FxImageOrigin)origin entry:(NSMutableDictionary*)entry commands:(id)commands { + NSString *name=lut?@"dyInputHighPreview":@"dyManagedHighPreview"; + id pipeline=[self computePipeline:name entry:entry error:nil]; + if(!pipeline)return nil; + id buffer=[[entry[@"device"] newBufferWithLength:DYLookPreviewHDWidth*DYLookPreviewHDHeight*16 options:MTLResourceStorageModeShared]autorelease]; + id encoder=buffer?[commands computeCommandEncoder]:nil; + if(!encoder)return nil; + [encoder setComputePipelineState:pipeline];[encoder setTexture:texture atIndex:0]; + [encoder setBuffer:buffer offset:0 atIndex:0]; + if(lut){[encoder setTexture:lut atIndex:1];[encoder setBytes:&strength length:sizeof(strength) atIndex:1];} + uint32_t flip=origin==kFxImageOrigin_BOTTOM_LEFT; + [encoder setBytes:&flip length:sizeof(flip) atIndex:2]; + [encoder dispatchThreads:MTLSizeMake(DYLookPreviewHDWidth,DYLookPreviewHDHeight,1) threadsPerThreadgroup:MTLSizeMake(8,8,1)]; + [encoder endEncoding]; + return buffer; +} +- (void)replaceLookHDBuffer:(id)buffer { + [_lookLUTPreviewHD release];_lookLUTPreviewHD=nil; + if(!buffer)return; + // Each capture owns a different buffer. NSData retains this one until the + // browser's asynchronous image work has finished reading it. + [buffer retain]; + _lookLUTPreviewHD=[[NSData alloc]initWithBytesNoCopy:buffer.contents length:buffer.length + deallocator:^(void *bytes,NSUInteger length){[buffer release];}]; +} #include "DYManagedRender.inc.h" #include "DYFaceRender.inc.h" #include "DYToolRender.inc.h" - (instancetype)init{if((self=[super init])){_previewLock=[NSObject new];_scopeProfileLock=[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];[_scopeProfileLock 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)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];[_scopeProfileLock release];dispatch_release(_scopeQueue);[_devices release];[_luts release];[_preparedLUTs release];[_activeLUTPaths release];dispatch_release(_lutPreparationQueue);[_tables release];[_preview release];[_lookLUTPreview release];[_lookLUTPreviewHD 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]; @@ -211,9 +240,14 @@ struct DYScopeReadback { }});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];});} +- (void)invalidatePreview{@synchronized(_previewLock){[_preview release];_preview=nil;[_lookLUTPreview release];_lookLUTPreview=nil;[_lookLUTPreviewHD release];_lookLUTPreviewHD=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];}} +- (NSDictionary*)lookLUTPreviewFrames {@synchronized(_previewLock){ + NSMutableDictionary *frames=[NSMutableDictionary dictionary]; + frames[@"standard"]=_lookLUTPreview;frames[@"high"]=_lookLUTPreviewHD; + return frames; +}} - (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{ @@ -357,7 +391,7 @@ struct DYScopeReadback { [ce dispatchThreadgroups:MTLSizeMake(1,1,1) threadsPerThreadgroup:MTLSizeMake(256,1,1)];[ce endEncoding]; } } - id pb=nil; + id pb=nil,lookHD=nil; if(preview){ id analysis=[self computePipeline:@"dyInputAnalysis" entry:e error:err];if(!analysis)return NO; pb=[[d newBufferWithLength:320*180*16 options:MTLResourceStorageModeShared]autorelease]; @@ -367,6 +401,8 @@ struct DYScopeReadback { 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]; + lookHD=[self encodeLookHD:straight inputLUT:neutral?e[@"identity"]:ts[0] strength:inputStrength + origin:src.imageOrigin entry:e commands:cb]; } MTLRenderPassDescriptor*rp=[MTLRenderPassDescriptor renderPassDescriptor];rp.colorAttachments[0].texture=output;rp.colorAttachments[0].loadAction=MTLLoadActionDontCare;rp.colorAttachments[0].storeAction=MTLStoreActionStore;idre=[cb renderCommandEncoderWithDescriptor:rp];[re setRenderPipelineState:pipeline]; float w=output.width,h=output.height;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; @@ -384,6 +420,7 @@ struct DYScopeReadback { 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]; + [self replaceLookHDBuffer:lookHD]; _lastPreview=CACurrentMediaTime();[self notifyPreviewChanged]; }} if(!ok)fail(err,cb.error.localizedDescription);return ok; diff --git a/大鱼调色/Plugin/DYToolGrayCapture.inc.h b/大鱼调色/Plugin/DYToolGrayCapture.inc.h index 14ab0c9..1ba0dc0 100644 --- a/大鱼调色/Plugin/DYToolGrayCapture.inc.h +++ b/大鱼调色/Plugin/DYToolGrayCapture.inc.h @@ -13,7 +13,41 @@ [self captureTrackingTile:tile gray:&result preview:preview error:error]; return result; } +- (CVMetalTextureRef)newTrackingVisionTexture:(NSMutableDictionary*)entry size:(vector_uint3)size + image:(CVPixelBufferRef*)image { + CVMetalTextureCacheRef cache=(CVMetalTextureCacheRef)entry[@"visionTextureCache"]; + if(!cache){ + if(CVMetalTextureCacheCreate(kCFAllocatorDefault,nullptr,entry[@"device"],nullptr,&cache)!=kCVReturnSuccess)return nullptr; + entry[@"visionTextureCache"]=(id)cache; + CFRelease(cache); + } + CVPixelBufferPoolRef pool=(CVPixelBufferPoolRef)entry[@"visionPool"]; + if(!pool||[entry[@"visionWidth"]unsignedIntValue]!=size.x||[entry[@"visionHeight"]unsignedIntValue]!=size.y){ + NSDictionary *attributes=@{(id)kCVPixelBufferPixelFormatTypeKey:@(kCVPixelFormatType_32BGRA), + (id)kCVPixelBufferWidthKey:@(size.x),(id)kCVPixelBufferHeightKey:@(size.y), + (id)kCVPixelBufferIOSurfacePropertiesKey:@{},(id)kCVPixelBufferMetalCompatibilityKey:@YES}; + if(CVPixelBufferPoolCreate(kCFAllocatorDefault,nullptr,(CFDictionaryRef)attributes,&pool)!=kCVReturnSuccess)return nullptr; + entry[@"visionPool"]=(id)pool;CFRelease(pool); + entry[@"visionWidth"]=@(size.x);entry[@"visionHeight"]=@(size.y); + } + NSDictionary *limit=@{(id)kCVPixelBufferPoolAllocationThresholdKey:@6}; + if(CVPixelBufferPoolCreatePixelBufferWithAuxAttributes(kCFAllocatorDefault,pool,(CFDictionaryRef)limit,image)!=kCVReturnSuccess)return nullptr; + CVMetalTextureRef view=nullptr; + NSDictionary *usage=@{(id)kCVMetalTextureUsage:@(MTLTextureUsageShaderWrite)}; + CVReturn status=CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault,cache,*image,(CFDictionaryRef)usage, + MTLPixelFormatBGRA8Unorm,size.x,size.y,0,&view); + if(status!=kCVReturnSuccess||!view||!CVMetalTextureGetTexture(view)){ + if(view)CFRelease(view); + CVPixelBufferRelease(*image);*image=nullptr; + return nullptr; + } + return view; +} - (BOOL)captureTrackingTile:(FxImageTile*)tile gray:(dy::GrayFrame*)gray preview:(NSDictionary**)preview error:(NSError**)error { + return [self captureTrackingTile:tile gray:gray preview:preview visionFlip:nil error:error]; +} +- (BOOL)captureTrackingTile:(FxImageTile*)tile gray:(dy::GrayFrame*)gray preview:(NSDictionary**)preview + visionFlip:(NSNumber*)visionFlip error:(NSError**)error { @synchronized(_toolCaptureDevices){ FxRect bounds=tile.tilePixelBounds,image=tile.imagePixelBounds; if(bounds.left!=image.left||bounds.right!=image.right||bounds.bottom!=image.bottom||bounds.top!=image.top)return NO; @@ -26,6 +60,10 @@ idpipeline=function?[device newComputePipelineStateWithFunction:function error:nil]:nil; entry[@"previewPipeline"]=pipeline; [pipeline release];[function release]; + function=[library newFunctionWithName:@"dyToolVisionPreview"]; + pipeline=function?[device newComputePipelineStateWithFunction:function error:nil]:nil; + entry[@"visionPipeline"]=pipeline; + [pipeline release];[function release]; } [scale release];[queue release];[grayPipeline release];[gammaPipeline release];[gray release];[gamma release];[library release]; }[devices release];if(!entry)return NO; @@ -57,10 +95,28 @@ [encoder endEncoding]; } idpreviewBuffer=nil; + CVPixelBufferRef visionImage=nullptr; + CVMetalTextureRef visionTexture=nullptr; vector_uint3 previewInfo={0,0,flip}; - if(preview&&entry[@"previewPipeline"]){ + if(preview){ double scale=std::min({1.,640./width,360./height}); previewInfo.x=std::max(1u,(unsigned)(width*scale));previewInfo.y=std::max(1u,(unsigned)(height*scale)); + } + if(preview&&visionFlip&&entry[@"visionPipeline"]){ + visionTexture=[self newTrackingVisionTexture:entry size:previewInfo image:&visionImage]; + if(visionTexture){ + uint32_t reverseRows=visionFlip.boolValue; + encoder=[commands computeCommandEncoder]; + [encoder setComputePipelineState:entry[@"visionPipeline"]]; + [encoder setTexture:bytes atIndex:0]; + [encoder setTexture:CVMetalTextureGetTexture(visionTexture) atIndex:1]; + [encoder setBytes:&previewInfo length:sizeof(previewInfo) atIndex:0]; + [encoder setBytes:&reverseRows length:sizeof(reverseRows) atIndex:1]; + [encoder dispatchThreads:MTLSizeMake(previewInfo.x,previewInfo.y,1) threadsPerThreadgroup:MTLSizeMake(16,16,1)]; + [encoder endEncoding]; + } + } + if(preview&&!visionTexture&&entry[@"previewPipeline"]){ NSUInteger size=previewInfo.x*previewInfo.y*4; previewBuffer=entry[@"previewBuffer"]; if(previewBuffer.length!=size){ @@ -75,14 +131,21 @@ [encoder endEncoding]; } } - if(!gray&&!previewBuffer)return NO; + if(!gray&&!previewBuffer&&!visionTexture)return NO; [commands commit];[commands waitUntilCompleted]; - if(commands.status!=MTLCommandBufferStatusCompleted){if(error)*error=commands.error;return NO;} - if(previewBuffer){ + if(visionTexture)CFRelease(visionTexture); + if(commands.status!=MTLCommandBufferStatusCompleted){ + if(visionImage)CVPixelBufferRelease(visionImage); + if(error)*error=commands.error; + return NO; + } + if(previewBuffer||visionImage){ double pixelAspect=tile.pixelAspect; double aspect=double(width)/height*(std::isfinite(pixelAspect)&&pixelAspect>0?pixelAspect:1); - *preview=@{@"rgba":[NSData dataWithBytes:previewBuffer.contents length:previewBuffer.length], - @"width":@(previewInfo.x),@"height":@(previewInfo.y),@"aspect":@(aspect)}; + NSString *key=visionImage?@"visionImage":@"rgba"; + id pixels=visionImage?(id)visionImage:[NSData dataWithBytes:previewBuffer.contents length:previewBuffer.length]; + *preview=@{key:pixels,@"width":@(previewInfo.x),@"height":@(previewInfo.y),@"aspect":@(aspect)}; + if(visionImage)CVPixelBufferRelease(visionImage); } if(gray){ gray->width=int(width);gray->height=int(height); diff --git a/大鱼调色/Plugin/DYToolHostIvars.inc.h b/大鱼调色/Plugin/DYToolHostIvars.inc.h index 29a97e6..8d1492b 100644 --- a/大鱼调色/Plugin/DYToolHostIvars.inc.h +++ b/大鱼调色/Plugin/DYToolHostIvars.inc.h @@ -11,7 +11,7 @@ CMTime _toolSourceTime,_trackingReferenceTime,_trackingFrameDuration,_trackingPreviousTime; std::atomic_bool _trackingActive,_trackingFinishing,_trackingInFrame,_trackingCancelled; BOOL _trackingBackward,_trackingBoth,_trackingSingle,_trackingFlip,_trackingDiscard; - uint64_t _trackingGeneration;double _trackingStarted; + uint64_t _trackingGeneration;double _trackingStarted,_trackingRunStart;NSUInteger _trackingDirectFrames,_trackingCopiedFrames; NSString *_trackingNode,*_trackingMaskID,*_trackingMessage; NSDictionary *_trackingFrozenMask;NSMutableDictionary *_trackingCandidate; NSMutableArray *_trackingSamples; diff --git a/大鱼调色/Plugin/DYToolHostMethods.inc.h b/大鱼调色/Plugin/DYToolHostMethods.inc.h index 26f056a..ac4cf92 100644 --- a/大鱼调色/Plugin/DYToolHostMethods.inc.h +++ b/大鱼调色/Plugin/DYToolHostMethods.inc.h @@ -166,14 +166,14 @@ [_trackingNode release];_trackingNode=[node copy];[_trackingMaskID release];_trackingMaskID=[notification.userInfo[@"mask"]copy];[_trackingFrozenMask release];_trackingFrozenMask=[mask copy];[_trackingCandidate release];_trackingCandidate=[DYMutablePlist(mask)retain];[_trackingSamples release];_trackingSamples=[NSMutableArray new];[_trackingMessage release];_trackingMessage=nil; NSArray *source=DYStampSourceAtTime(mask,time); _trackingStampMotion=DYStampMotionAtTime(mask,time); - _trackingObjectMode=DYMosaicTracksMask(doc,mask[@"id"]);_trackingObjectPending=_trackingObjectRepaired=0; + _trackingObjectMode=DYMosaicTracksMask(doc,mask[@"id"]);_trackingObjectPending=_trackingObjectRepaired=0;_trackingDirectFrames=_trackingCopiedFrames=0; if(source){ _trackingStampMotion.active=true; _trackingStampSourceSeed={[source[0] floatValue],[source[1] floatValue], std::min(8.f,[source[2] floatValue]*_trackingStampMotion.sourceScaleX), std::min(8.f,[source[3] floatValue]*_trackingStampMotion.sourceScaleY),0}; } - _trackingReferenceTime=time;_trackingSeed=seed;_trackingBackward=direction<0;_trackingBoth=direction==0;_trackingSingle=std::abs((int)direction)==2;_trackingDiscard=NO;_trackingCancelled.store(false);_trackingFinishing.store(false);_trackingActive.store(true);++_trackingGeneration;_trackingStarted=NSProcessInfo.processInfo.systemUptime; + _trackingReferenceTime=time;_trackingSeed=seed;_trackingBackward=direction<0;_trackingBoth=direction==0;_trackingSingle=std::abs((int)direction)==2;_trackingDiscard=NO;_trackingCancelled.store(false);_trackingFinishing.store(false);_trackingActive.store(true);++_trackingGeneration;_trackingRunStart=_trackingStarted=NSProcessInfo.processInfo.systemUptime; _trackingPreviewInputRange=kCMTimeRangeInvalid;_trackingPreviewTotal=_trackingPreviewCompleted=0; @synchronized(self){[_trackingPreviewFrame release];_trackingPreviewFrame=nil;_trackingPreviewQueued=NO;} os_log(OS_LOG_DEFAULT,"dayu_tool_track_start direction=%{public}ld reference=%{public}.6f cached=%{public}d",(long)direction,CMTimeGetSeconds(time),_trackingReferenceFrame.valid()); @@ -247,22 +247,32 @@ // The object tracker consumes only the small color image. The gray frame // is useful at the seed only when checking orientation against a cached frame. const bool needsGray=!objectMode||(!_trackingSamples.count&&_trackingReferenceFrame.valid()); - if(![self captureTrackingTile:tile gray:needsGray?&frame:nullptr preview:(!_trackingSingle||objectMode)?&preview:nullptr error:error]) + // The seed may still need the legacy orientation comparison before the + // tracking flip is known. All subsequent object frames can go straight to Vision. + NSNumber *visionFlip=objectMode&&!needsGray?@(_trackingFlip):nil; + if(![self captureTrackingTile:tile gray:needsGray?&frame:nullptr + preview:(!_trackingSingle||objectMode)?&preview:nullptr visionFlip:visionFlip error:error]) return [self stopToolAnalysis:@"宿主未返回完整源画面" error:error]; if(!_trackingSamples.count){if(_trackingReferenceFrame.valid()&&frame.width==_trackingReferenceFrame.width&&frame.height==_trackingReferenceFrame.height){double same=0,flip=0;size_t count=0;for(int y=0;y640||height>640||rgba.length!=size_t(width)*height*4)return [self finishToolAnalysis:@"目标跟踪未取得完整分析画面,原轨迹已保留" error:error]; + if(width<16||height<16||width>640||height>640||(!visionImage&&rgba.length!=size_t(width)*height*4)) + return [self finishToolAnalysis:@"目标跟踪未取得完整分析画面,原轨迹已保留" error:error]; + if(visionImage)++_trackingDirectFrames;else ++_trackingCopiedFrames; if(!_trackingSamples.count){ dy::TrackerROI seed={_trackingSeed.centerX,_trackingSeed.centerY,_trackingSeed.width,_trackingSeed.height,_trackingSeed.angle}; dy::TrackerROI roi=dy::visionMaskBounds(seed,_trackingShapePolygon,float(width)/height);_trackingObjectROI=roi; os_log(OS_LOG_DEFAULT,"dayu_object_track_worker main=%{public}d",(int)NSThread.isMainThread); - if(!_objectTracker.reset((const uint8_t*)rgba.bytes,width,height,_trackingFlip,roi,1./CMTimeGetSeconds(_trackingFrameDuration)))return [self finishToolAnalysis:@"无法建立目标外观,请调整蒙版后重试" error:error]; + double fps=1./CMTimeGetSeconds(_trackingFrameDuration); + bool seeded=visionImage?_objectTracker.reset(visionImage,roi,fps): + _objectTracker.reset((const uint8_t*)rgba.bytes,width,height,_trackingFlip,roi,fps); + if(!seeded)return [self finishToolAnalysis:@"无法建立目标外观,请调整蒙版后重试" error:error]; objectStep={{roi.cx,roi.cy,roi.width,roi.height,1},true,false,false,0}; - }else objectStep=_objectTracker.track((const uint8_t*)rgba.bytes,width,height,_trackingFlip); + }else objectStep=visionImage?_objectTracker.track(visionImage):_objectTracker.track((const uint8_t*)rgba.bytes,width,height,_trackingFlip); target=_objectTracker.result(objectStep); target.cx+=(_trackingSeed.centerX-_trackingObjectROI.cx)*target.scale;target.cy+=(_trackingSeed.centerY-_trackingObjectROI.cy)*target.scale; if(!target.valid)return [self finishToolAnalysis:@"目标未能重新确认,已停止;请调整蒙版后继续" error:error]; @@ -324,7 +334,7 @@ } packet[@"shape"]=[NSData dataWithBytes:&covered length:sizeof(covered)];packet[@"predicted"]=@(objectStep.predicted); packet[@"nodes"]=_trackingFrozenMask[@"nodes"]; - packet[@"flip"]=@(_trackingFlip);packet[@"time"]=@(CMTimeGetSeconds(time)); + packet[@"flip"]=@(_trackingFlip&&!preview[@"visionImage"]);packet[@"time"]=@(CMTimeGetSeconds(time)); packet[@"completed"]=@(_trackingPreviewCompleted); packet[@"total"]=@(MAX(_trackingPreviewTotal,_trackingPreviewCompleted)); packet[@"backward"]=@(_trackingBackward); @@ -354,8 +364,13 @@ if(self->_trackingObjectRepaired)message=[message stringByAppendingFormat:@" · 补齐 %lu 帧短暂遮挡,请复核",(unsigned long)self->_trackingObjectRepaired]; if(hasResults&&!self->_trackingDiscard){ida=[self->_apiManager apiForProtocol:@protocol(FxCustomParameterActionAPI_v4)];if(a){[a startAction:self];@try{NSMutableDictionary *doc=DYMutablePlist(DYReadTools(self->_apiManager,a.currentTime).document);NSString *node=DYSharedMaskWithID(doc,self->_trackingMaskID)?@"shared":self->_trackingNode;NSMutableDictionary *mask=[self toolMask:self->_trackingMaskID node:node document:doc];if(mask&&[mask isEqual:self->_trackingFrozenMask]){self->_trackingCandidate[@"status"]=message;[mask setDictionary:self->_trackingCandidate];idundo=[self->_apiManager apiForProtocol:@protocol(FxUndoAPI)];[undo startUndoGroup:mask[@"stampSource"]?@"仿制图章跟踪":@"蒙版区域跟踪"];BOOL written=DYWriteTools(self->_apiManager,doc,a.currentTime);[undo endUndoGroup];if(!written)message=@"宿主未接受轨迹,原蒙版已保留";}else message=@"目标工具或蒙版已更改,已丢弃过期跟踪结果";}@finally{[a endAction:self];}}} CMTime stepTime=self->_trackingPreviousTime;BOOL advance=self->_trackingSingle&&changed&&!self->_trackingDiscard&&!self->_trackingCancelled.load()&&CMTIME_IS_NUMERIC(stepTime); - @synchronized(self->_toolCaptureDevices){for(NSMutableDictionary *entry in self->_toolCaptureDevices.allValues)[entry removeObjectsForKeys:@[@"encoded",@"bytes",@"buffer",@"previewBuffer"]];} + @synchronized(self->_toolCaptureDevices){for(NSMutableDictionary *entry in self->_toolCaptureDevices.allValues)[entry removeObjectsForKeys:@[@"encoded",@"bytes",@"buffer",@"previewBuffer",@"visionPool",@"visionWidth",@"visionHeight"]];} self->_trackingActive.store(false);if(advance){idseek=[self->_apiManager apiForProtocol:@protocol(FxCustomParameterActionAPI_v4)];if(seek){[seek startAction:self];@try{idtiming=[self->_apiManager apiForProtocol:@protocol(FxTimingAPI_v4)];CMTime timelineTime=kCMTimeInvalid;[timing timelineTime:&timelineTime fromInputTime:stepTime];idcommand=[self->_apiManager apiForProtocol:@protocol(FxCommandAPI_v2)];if(CMTIME_IS_NUMERIC(timelineTime)){NSError *seekError=nil;BOOL moved=[command movePlayheadToTime:timelineTime error:&seekError];if(!moved)os_log(OS_LOG_DEFAULT,"dayu_tool_track_seek timeline=%{public}.6f code=%{public}ld",CMTimeGetSeconds(timelineTime),(long)seekError.code);}else message=[message stringByAppendingString:@";宿主未提供有效播放头位置"];}@finally{[seek endAction:self];}}}[self->_toolAnalysisTimer invalidate];[self->_toolAnalysisTimer release];self->_toolAnalysisTimer=nil;[NSNotificationCenter.defaultCenter postNotificationName:DYModelChanged object:self->_apiManager]; + if(self->_trackingObjectMode) + os_log(OS_LOG_DEFAULT,"dayu_object_track_finished elapsed_ms=%{public}.2f direct=%{public}lu copied=%{public}lu keys=%{public}lu", + 1000*(NSProcessInfo.processInfo.systemUptime-self->_trackingRunStart), + (unsigned long)self->_trackingDirectFrames,(unsigned long)self->_trackingCopiedFrames, + (unsigned long)[self->_trackingCandidate[@"keys"]count]); if(self->_trackingSingle)[self toolStatus:message]; else [self postTrackingPreview:nil active:NO message:message]; });return YES; diff --git a/大鱼调色/Plugin/DYToolTrackingDiagnostics.inc.h b/大鱼调色/Plugin/DYToolTrackingDiagnostics.inc.h index 207a973..a2d94c7 100644 --- a/大鱼调色/Plugin/DYToolTrackingDiagnostics.inc.h +++ b/大鱼调色/Plugin/DYToolTrackingDiagnostics.inc.h @@ -75,6 +75,43 @@ BOOL noGrayBuffer=YES;for(NSDictionary *entry in [[previewOnly valueForKey:@"toolCaptureDevices"] allValues])noGrayBuffer&=entry[@"buffer"]==nil; run.record(@"mosaic-tracking-4k-color-input-matches-without-gray-buffer",colorOK&&full.valid()&&color[@"rgba"]&&[color isEqual:referenceColor]&&noGrayBuffer); } + { + const NSUInteger w=323,h=181; + DYDiagnosticTile *tile=[[[DYDiagnosticTile alloc]initWithDevice:gpu width:w height:h]autorelease]; + Pixels color(w*h*4); + for(NSUInteger y=0;y legacy=[(id)[[NSClassFromString(@"DYPlugIn") alloc] initWithAPIManager:host] autorelease]; + id direct=[(id)[[NSClassFromString(@"DYPlugIn") alloc] initWithAPIManager:host] autorelease]; + BOOL exact=tile!=nil;NSUInteger compared=0; + for(FxImageOrigin origin : {kFxImageOrigin_TOP_LEFT,kFxImageOrigin_BOTTOM_LEFT}){ + [tile setDiagnosticImageOrigin:origin]; + for(BOOL flip : {NO,YES}){@autoreleasepool{ + NSDictionary *rgba=nil,*packet=nil; + [legacy captureTrackingTile:tile gray:nullptr preview:&rgba error:nil]; + [direct captureTrackingTile:tile gray:nullptr preview:&packet visionFlip:@(flip) error:nil]; + CVPixelBufferRef image=(CVPixelBufferRef)packet[@"visionImage"]; + NSData *reference=rgba[@"rgba"]; + if(!image||!CVPixelBufferGetIOSurface(image)||packet[@"rgba"]||reference.length!=w*h*4|| + CVPixelBufferGetPixelFormatType(image)!=kCVPixelFormatType_32BGRA|| + CVPixelBufferLockBaseAddress(image,kCVPixelBufferLock_ReadOnly)!=kCVReturnSuccess){exact=NO;continue;} + const auto *actual=static_cast(CVPixelBufferGetBaseAddress(image)); + const auto *expected=static_cast(reference.bytes); + const size_t stride=CVPixelBufferGetBytesPerRow(image); + for(NSUInteger y=0;y source [[texture(0)]],device uchar* p } // Reuse the analysis source texture for the lightweight color preview. +inline uchar4 dyTrackingPreviewPixel(texture2d source,uint2 pos,uint3 info){ + constexpr sampler filter(coord::normalized,address::clamp_to_edge,filter::linear); + float2 uv=(float2(pos)+.5f)/float2(info.xy); + if(info.z)uv.y=1-uv.y; + return uchar4(uchar3(rint(saturate(source.sample(filter,uv).rgb)*255)),255); +} kernel void dyToolTrackingPreview(texture2d source [[texture(0)]], device uchar4 *pixels [[buffer(0)]], constant uint3 &info [[buffer(1)]], uint2 pos [[thread_position_in_grid]]){ if(any(pos>=info.xy))return; - constexpr sampler filter(coord::normalized,address::clamp_to_edge,filter::linear); - float2 uv=(float2(pos)+.5f)/float2(info.xy); - if(info.z)uv.y=1-uv.y; - float3 rgb=source.sample(filter,uv).rgb; - pixels[pos.y*info.x+pos.x]=uchar4(uchar3(rint(saturate(rgb)*255)),255); + pixels[pos.y*info.x+pos.x]=dyTrackingPreviewPixel(source,pos,info); +} + +kernel void dyToolVisionPreview(texture2d source [[texture(0)]], + texture2d image [[texture(1)]], + constant uint3 &info [[buffer(0)]], + constant uint &reverseRows [[buffer(1)]], + uint2 pos [[thread_position_in_grid]]){ + if(any(pos>=info.xy))return; + // Reverse the same discrete row as the CPU packer, keeping sampling and + // quantization identical for both image origins and odd sizes. + uint2 sample=uint2(pos.x,reverseRows?info.y-1-pos.y:pos.y); + image.write(float4(dyTrackingPreviewPixel(source,sample,info))/255.f,pos); } diff --git a/大鱼调色/Plugin/DYTrackingPreview.inc.h b/大鱼调色/Plugin/DYTrackingPreview.inc.h index 244900b..932d558 100644 --- a/大鱼调色/Plugin/DYTrackingPreview.inc.h +++ b/大鱼调色/Plugin/DYTrackingPreview.inc.h @@ -1,3 +1,5 @@ +#import + @interface DYTrackingPreviewImage : NSView - (void)showFrame:(NSDictionary *)frame; @end @@ -18,10 +20,21 @@ [_frame release];_frame=[frame copy];[_image release];_image=nil; NSData *pixels=frame[@"rgba"]; NSUInteger w=[frame[@"width"] unsignedIntegerValue],h=[frame[@"height"] unsignedIntegerValue]; - if(w&&h&&pixels.length==w*h*4){ + CVPixelBufferRef visionImage=(CVPixelBufferRef)frame[@"visionImage"]; + size_t stride=w*4; + CGBitmapInfo bitmapInfo=kCGImageAlphaNoneSkipLast; + // Only frames actually presented in the coalesced UI are copied to a + // display bitmap. The analysis worker passes the shared buffer directly. + if(visionImage&&CVPixelBufferLockBaseAddress(visionImage,kCVPixelBufferLock_ReadOnly)==kCVReturnSuccess){ + stride=CVPixelBufferGetBytesPerRow(visionImage); + pixels=[NSData dataWithBytes:CVPixelBufferGetBaseAddress(visionImage) length:stride*h]; + CVPixelBufferUnlockBaseAddress(visionImage,kCVPixelBufferLock_ReadOnly); + bitmapInfo=kCGBitmapByteOrder32Little|kCGImageAlphaNoneSkipFirst; + } + if(w&&h&&pixels.length==stride*h){ CGDataProviderRef provider=CGDataProviderCreateWithCFData((CFDataRef)pixels); CGColorSpaceRef space=CGColorSpaceCreateWithName(kCGColorSpaceSRGB); - CGImageRef cg=CGImageCreate(w,h,8,32,w*4,space,kCGImageAlphaNoneSkipLast,provider,nil,true,kCGRenderingIntentDefault); + CGImageRef cg=CGImageCreate(w,h,8,32,stride,space,bitmapInfo,provider,nil,true,kCGRenderingIntentDefault); if(cg){_image=[[NSImage alloc]initWithCGImage:cg size:NSMakeSize(w,h)];CGImageRelease(cg);} CGColorSpaceRelease(space);CGDataProviderRelease(provider); } diff --git a/大鱼调色/Plugin/Info.plist b/大鱼调色/Plugin/Info.plist index a383935..139b16d 100644 --- a/大鱼调色/Plugin/Info.plist +++ b/大鱼调色/Plugin/Info.plist @@ -17,9 +17,9 @@ CFBundlePackageType XPC! CFBundleShortVersionString - 1.21.2 + 1.21.3 CFBundleVersion - 119 + 120 NSHumanReadableCopyright PlugInKit diff --git a/大鱼调色/Plugin/MaskCore/DYObjectTracker.hpp b/大鱼调色/Plugin/MaskCore/DYObjectTracker.hpp index e1cc2aa..fe6ee7a 100644 --- a/大鱼调色/Plugin/MaskCore/DYObjectTracker.hpp +++ b/大鱼调色/Plugin/MaskCore/DYObjectTracker.hpp @@ -11,7 +11,7 @@ namespace dy { class ObjectTracker { VNSequenceRequestHandler *sequence_=nil; VNTrackObjectRequest *request_=nil; - CVPixelBufferRef current_=nullptr,trusted_=nullptr; + CVPixelBufferRef fallback_[2]{},trusted_=nullptr; ObjectBox trustedBox_{}; ObjectMotion motion_; TrackerROI seed_{}; @@ -30,35 +30,73 @@ class ObjectTracker { CGRect b=observation.boundingBox;box={float(CGRectGetMidX(b)),float(1-CGRectGetMidY(b)),float(b.size.width),float(b.size.height),observation.confidence}; request_.inputObservation=observation;return true; } - bool copyInput(const uint8_t *rgba,int w,int h,bool flip){ - if(!rgba||w<16||h<16||w>640||h>640)return false; - if(w!=width_||h!=height_)return false; - CVPixelBufferLockBaseAddress(current_,0);auto *out=(uint8_t*)CVPixelBufferGetBaseAddress(current_);size_t stride=CVPixelBufferGetBytesPerRow(current_); + CVPixelBufferRef copyInput(const uint8_t *rgba,int w,int h,bool flip){ + if(!rgba||!validSize(w,h)||w!=width_||h!=height_)return nullptr; + // A CPU fallback frame may become the trusted frame. Never write into + // that slot or into a Metal buffer still held by the preview. + auto &buffer=fallback_[0]&&fallback_[0]==trusted_?fallback_[1]:fallback_[0]; + if(!buffer&&CVPixelBufferCreate(kCFAllocatorDefault,w,h,kCVPixelFormatType_32BGRA, + (CFDictionaryRef)@{(id)kCVPixelBufferIOSurfacePropertiesKey:@{}},&buffer)!=kCVReturnSuccess)return nullptr; + if(CVPixelBufferLockBaseAddress(buffer,0)!=kCVReturnSuccess)return nullptr; + auto *out=(uint8_t*)CVPixelBufferGetBaseAddress(buffer);size_t stride=CVPixelBufferGetBytesPerRow(buffer); for(int y=0;y=16&&h>=16&&w<=640&&h<=640;} + bool accepts(CVPixelBufferRef image)const{ + return image&&CVPixelBufferGetPixelFormatType(image)==kCVPixelFormatType_32BGRA&& + CVPixelBufferGetWidth(image)==size_t(width_)&&CVPixelBufferGetHeight(image)==size_t(height_); + } + void trust(CVPixelBufferRef image,ObjectBox box){ + CVPixelBufferRetain(image); + if(trusted_)CVPixelBufferRelease(trusted_); + trusted_=image;trustedBox_=box; + } + bool begin(CVPixelBufferRef image,TrackerROI roi,double fps){ + seed_=roi; + ObjectBox box{roi.cx,roi.cy,roi.width,roi.height,1}; + motion_.reset(box,fps); + if(!accepts(image)||!motion_.active()){stop();return false;} + newSequence(box);ObjectBox first; + if(!perform(image,first)){stop();return false;} + trust(image,box);started_=true;return true; } public: ObjectTracker()=default; ObjectTracker(const ObjectTracker&)=delete; ObjectTracker& operator=(const ObjectTracker&)=delete; ~ObjectTracker(){stop();} - void stop(){[request_ release];request_=nil;[sequence_ release];sequence_=nil;if(current_)CVPixelBufferRelease(current_);if(trusted_)CVPixelBufferRelease(trusted_);current_=trusted_=nullptr;width_=height_=0;started_=false;} + void stop(){ + [request_ release];request_=nil;[sequence_ release];sequence_=nil; + if(trusted_)CVPixelBufferRelease(trusted_); + trusted_=nullptr; + for(auto &image:fallback_){if(image)CVPixelBufferRelease(image);image=nullptr;} + width_=height_=0;started_=false; + } bool reset(const uint8_t *rgba,int w,int h,bool flip,TrackerROI roi,double fps){ - stop();seed_=roi;width_=w;height_=h; - if(w<16||h<16||w>640||h>640)return false; - for(auto p:{¤t_,&trusted_})if(CVPixelBufferCreate(kCFAllocatorDefault,w,h,kCVPixelFormatType_32BGRA,(CFDictionaryRef)@{(id)kCVPixelBufferIOSurfacePropertiesKey:@{}},p)!=kCVReturnSuccess){stop();return false;} - trustedBox_={roi.cx,roi.cy,roi.width,roi.height,1};motion_.reset(trustedBox_,fps);if(!motion_.active()||!copyInput(rgba,w,h,flip)){stop();return false;} - newSequence(trustedBox_);ObjectBox first;if(!perform(current_,first)){stop();return false;} - std::swap(current_,trusted_);started_=true;return true; + stop();width_=w;height_=h; + return begin(copyInput(rgba,w,h,flip),roi,fps); + } + bool reset(CVPixelBufferRef image,TrackerROI roi,double fps){ + stop(); + if(!image)return false; + size_t w=CVPixelBufferGetWidth(image),h=CVPixelBufferGetHeight(image); + if(w>640||h>640||!validSize(int(w),int(h)))return false; + width_=int(w);height_=int(h); + return begin(image,roi,fps); } ObjectStep track(const uint8_t *rgba,int w,int h,bool flip){ - if(!started_||!copyInput(rgba,w,h,flip))return {}; + return started_?track(copyInput(rgba,w,h,flip)):ObjectStep{}; + } + ObjectStep track(CVPixelBufferRef image){ + if(!started_||!accepts(image))return {}; if(motion_.missing()){ newSequence(trustedBox_);ObjectBox ignored;if(!perform(trusted_,ignored))return {}; } - ObjectBox observed{};bool found=perform(current_,observed); + ObjectBox observed{};bool found=perform(image,observed); auto step=motion_.advance(observed,found); - if(step.valid&&!step.predicted){trustedBox_=observed;std::swap(current_,trusted_);} + if(step.valid&&!step.predicted)trust(image,observed); return step; } int pendingFrames()const{return motion_.missing();} diff --git a/大鱼调色/Wrapper Application/Info.plist b/大鱼调色/Wrapper Application/Info.plist index 7bc81fd..6ccb31b 100644 --- a/大鱼调色/Wrapper Application/Info.plist +++ b/大鱼调色/Wrapper Application/Info.plist @@ -17,9 +17,9 @@ CFBundlePackageType APPL CFBundleShortVersionString - 1.21.2 + 1.21.3 CFBundleVersion - 119 + 120 LSMinimumSystemVersion $(MACOSX_DEPLOYMENT_TARGET) NSHumanReadableCopyright diff --git a/开发交接.md b/开发交接.md index 3409001..faecd0c 100644 --- a/开发交接.md +++ b/开发交接.md @@ -2,6 +2,12 @@ 更新于 2026-11-08(Asia/Shanghai)。这是新对话的接手入口。历史设计和实现细节按需查阅专项交接,复现和交付证据以对应版本的报告为准。 +## 1.21.3(120)共享缓冲追踪与 LUT 对比预览 + +当前在候选验证阶段。目标追踪由 Metal 写入带 IOSurface 的 BGRA 像素缓冲,交给 Vision;必要时沿用原 CPU 转换。LUT 浏览器右侧显示可拖动的原图和所选 LUT 对比,缩略图可切换为 640×360 源帧的高清预览。 + +FCP 12.4 同一 4K 素材使用 UHD 25 fps 时间线,285 帧。正式 1.21.2 的热态完整追踪用时 3.651、3.717、3.682 秒,四次均保存 285 个键。基线已导出全部轨迹和 4K ProRes。待正式候选自检、公证安装、宿主逐键与逐帧对照及 LUT 浏览器验收后,再制作和发布 PKG。 + ## 1.21.2(119)局部马赛克与蒙版分析性能 2026-11-08。已核对正式 1.21.1(117)与大鱼远端基线。马赛克渲染只在色键使用颜色编码,钢笔路径在局部包围范围外跳过轮廓计算,反相与色阶仍照常应用。无人脸命中时跳过全帧马赛克通道。指定目标追踪沿用原 Vision 彩色输入,仅在有可对照的起始画面时需要整帧灰度;普通蒙版与图章保留灰度输入。