perf: defer unused GPU work and streamline preview rendering

这个提交包含在:
mac 于 2026-11-08 16:10:13 +08:00 提交
1 父节点 c9e4d3f330
当前提交 ccc34734e3
18 个文件已更改
+362 -115

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

+66 -27
查看文件
@@ -80,37 +80,76 @@
}});
return raster;
}
- (void)captureScopeTile:(FxImageTile *)tile atTime:(CMTime)time{
NSUInteger generation;uint64_t serial;@synchronized(_previewLock){if(_exporting||!tile)return;generation=_scopeGeneration;serial=++_scopeCaptureSerial;}
@autoreleasepool{@try{
NSDictionary *entry=nil,*context=nil;@synchronized(self){entry=[[self device:tile.deviceRegistryID error:nil]retain];context=[_colorContext copy];}
id<MTLDevice>device=entry[@"device"];id<MTLTexture>texture=[tile metalTextureForDevice:device];
if(!entry||!texture||!texture.width||!texture.height||texture.framebufferOnly){[entry release];[context release];return;}
id<MTLBuffer>buffer=[device newBufferWithLength:dyscope::Width*dyscope::Height*sizeof(dyscope::RGBA) options:MTLResourceStorageModeShared];
id<MTLCommandBuffer>command=[entry[@"queue"]commandBuffer];id<MTLComputeCommandEncoder>encoder=[command computeCommandEncoder];
if(!buffer||!encoder){[buffer release];[entry release];[context release];return;}
uint32_t flip=tile.imageOrigin==kFxImageOrigin_BOTTOM_LEFT;[encoder setComputePipelineState:entry[@"dyScopeSample"]];[encoder setTexture:texture atIndex:0];[encoder setBuffer:buffer offset:0 atIndex:0];[encoder setBytes:&flip length:sizeof(flip) atIndex:1];
[encoder dispatchThreads:MTLSizeMake(dyscope::Width,dyscope::Height,1) threadsPerThreadgroup:MTLSizeMake(8,8,1)];[encoder endEncoding];[command commit];
// The host may recycle its destination as soon as render returns. Complete
// only this 320x180 readback while that ownership is still guaranteed.
// CPU profile conversion, plotting and UI notification do not block render.
[command waitUntilCompleted];
if(command.status!=MTLCommandBufferStatusCompleted){[buffer release];[entry release];[context release];return;}
DYScopeSample *sample=[DYScopeSample new];sample.pixels=[NSData dataWithBytes:buffer.contents length:buffer.length];sample.generation=generation;
CGColorSpaceRef sourceSpace=DYManagedTileSpace(tile,[context[@"primaries"]integerValue]);sample.space=sourceSpace;if(sourceSpace)CGColorSpaceRelease(sourceSpace);
FxRect bounds=tile.tilePixelBounds,image=tile.imagePixelBounds;BOOL whole=bounds.left==image.left&&bounds.right==image.right&&bounds.top==image.top&&bounds.bottom==image.bottom;
sample.metadata=@{@"time":@(CMTimeGetSeconds(time)),@"timeValue":@(time.value),@"timeScale":@(time.timescale),@"timeEpoch":@(time.epoch),@"source":context[@"destination"]?:@"宿主 RGB",@"wholeImage":@(whole),@"region":whole?@"整帧取样":@"仅当前宿主输出分块",@"width":@(dyscope::Width),@"height":@(dyscope::Height)};
[buffer release];[entry release];[context release];
BOOL start=NO,notifyOutput=NO;@synchronized(_previewLock){
if(serial>=_scopeLatestSerial){
[_scopeLatest release];_scopeLatest=[sample retain];_scopeLatestSerial=serial;
// The caller owns the destination until command completion. Only the small
// result buffer is copied out of that lifetime, even when the scope is closed.
- (DYScopeReadback)encodeScopeTile:(FxImageTile *)tile texture:(id<MTLTexture>)texture entry:(NSMutableDictionary *)entry commands:(id<MTLCommandBuffer>)command {
if(_exporting||!tile||!texture.width||!texture.height||texture.framebufferOnly)return {};
id<MTLComputePipelineState> pipeline=[self computePipeline:@"dyScopeSample" entry:entry error:nil];
if(!pipeline)return {};
id<MTLBuffer> buffer=entry[@"scopeBuffer"];
if(!buffer){
id<MTLDevice> device=entry[@"device"];
buffer=[[device newBufferWithLength:dyscope::Width*dyscope::Height*sizeof(dyscope::RGBA) options:MTLResourceStorageModeShared]autorelease];
if(!buffer)return {};
entry[@"scopeBuffer"]=buffer;
}
id<MTLComputeCommandEncoder> encoder=[command computeCommandEncoder];
if(!encoder)return {};
uint32_t flip=tile.imageOrigin==kFxImageOrigin_BOTTOM_LEFT;
[encoder setComputePipelineState:pipeline];[encoder setTexture:texture atIndex:0];
[encoder setBuffer:buffer offset:0 atIndex:0];[encoder setBytes:&flip length:sizeof(flip) atIndex:1];
[encoder dispatchThreads:MTLSizeMake(dyscope::Width,dyscope::Height,1) threadsPerThreadgroup:MTLSizeMake(8,8,1)];
[encoder endEncoding];
@synchronized(_previewLock){return {buffer,_scopeGeneration,++_scopeCaptureSerial};}
}
- (void)publishScopeReadback:(DYScopeReadback)readback tile:(FxImageTile *)tile atTime:(CMTime)time {
if(!readback.buffer)return;
@try{
DYScopeSample *sample=[[[DYScopeSample alloc]init]autorelease];
sample.pixels=[NSData dataWithBytes:readback.buffer.contents length:readback.buffer.length];
sample.generation=readback.generation;
CGColorSpaceRef space=DYManagedTileSpace(tile,[_colorContext[@"primaries"]integerValue]);
sample.space=space;if(space)CGColorSpaceRelease(space);
FxRect bounds=tile.tilePixelBounds,image=tile.imagePixelBounds;
BOOL whole=bounds.left==image.left&&bounds.right==image.right&&bounds.top==image.top&&bounds.bottom==image.bottom;
sample.metadata=@{@"time":@(CMTimeGetSeconds(time)),@"timeValue":@(time.value),@"timeScale":@(time.timescale),@"timeEpoch":@(time.epoch),@"source":_colorContext[@"destination"]?:@"宿主 RGB",@"wholeImage":@(whole),@"region":whole?@"整帧取样":@"仅当前宿主输出分块",@"width":@(dyscope::Width),@"height":@(dyscope::Height)};
BOOL start=NO,notifyOutput=NO;
@synchronized(_previewLock){
if(readback.serial>=_scopeLatestSerial){
[_scopeLatest release];_scopeLatest=[sample retain];_scopeLatestSerial=readback.serial;
if(_outputObserved&&!_outputNotificationPending){_outputNotificationPending=YES;notifyOutput=YES;}
}
if(_scopeEnabled&&serial==_scopeLatestSerial&&(generation==_scopeGeneration||!CMTIME_IS_NUMERIC(_scopeRequestedTime)||CMTimeCompare(time,_scopeRequestedTime)==0)){sample.generation=_scopeGeneration;[_scopePending release];_scopePending=[sample retain];if(!_scopeWorkerRunning){_scopeWorkerRunning=YES;start=YES;}}
}[sample release];
if(_scopeEnabled&&readback.serial==_scopeLatestSerial&&(readback.generation==_scopeGeneration||!CMTIME_IS_NUMERIC(_scopeRequestedTime)||CMTimeCompare(time,_scopeRequestedTime)==0)){
sample.generation=_scopeGeneration;[_scopePending release];_scopePending=[sample retain];
if(!_scopeWorkerRunning){_scopeWorkerRunning=YES;start=YES;}
}
}
if(notifyOutput)[self queueOutputNotification];
if(start)dispatch_async(_scopeQueue,^{[self processPendingScopeSamples];});
}@catch(NSException *exception){/* Scope diagnostics must never fail rendering. */}}
}@catch(NSException *exception){/* Scope diagnostics must never fail rendering. */}
}
- (BOOL)completeCommands:(id<MTLCommandBuffer>)command destination:(FxImageTile *)destination texture:(id<MTLTexture>)texture entry:(NSMutableDictionary *)entry {
DYScopeReadback readback{};
if(destination==_scopeDestination){
@try{readback=[self encodeScopeTile:destination texture:texture entry:entry commands:command];}
@catch(NSException *exception){/* Fall back to separate sampling. */}
}
[command commit];[command waitUntilCompleted];
if(command.status!=MTLCommandBufferStatusCompleted)return NO;
if(readback.buffer){[self publishScopeReadback:readback tile:destination atTime:_scopeRenderTime];_scopeCaptured=YES;}
return YES;
}
- (void)captureScopeTile:(FxImageTile *)tile atTime:(CMTime)time {
@synchronized(self){@autoreleasepool{@try{
if(!tile||_exporting)return;
NSMutableDictionary *entry=[self device:tile.deviceRegistryID error:nil];
id<MTLTexture> texture=[tile metalTextureForDevice:entry[@"device"]];
id<MTLCommandBuffer> command=[entry[@"queue"]commandBuffer];
DYScopeReadback readback=[self encodeScopeTile:tile texture:texture entry:entry commands:command];
if(!readback.buffer)return;
[command commit];[command waitUntilCompleted];
if(command.status==MTLCommandBufferStatusCompleted)[self publishScopeReadback:readback tile:tile atTime:time];
}@catch(NSException *exception){/* Scope diagnostics must never fail rendering. */}}}
}
- (void)processPendingScopeSamples{
for(;;){@autoreleasepool{