perf: defer unused GPU work and streamline preview rendering
这个提交包含在:
1 父节点
c9e4d3f330
当前提交
ccc34734e3
18 个文件已更改
+362
-115
没有与搜索条件相匹配的文件
+135
-29
@@ -70,13 +70,15 @@ static void bypassNeutralTools(DYUniforms *u){
|
||||
if(neutral)for(NSDictionary *row in DYDescriptors())if([row[@"pid"]isEqual:section[@"enabled"]])*(float*)((uint8_t*)u+[row[@"offset"]unsignedIntegerValue])=0;
|
||||
}
|
||||
}
|
||||
static BOOL hasLegacyGrade(const DYUniforms &u){
|
||||
if(u.curveSectionEnabled>.5||u.hslSectionEnabled>.5||u.colorGradingSectionEnabled>.5||u.primariesSectionEnabled>.5||u.levelsSectionEnabled>.5||u.blackWhiteSectionEnabled>.5||u.colorBalanceSectionEnabled>.5)return YES;
|
||||
if(u.preLUTStrength!=0||u.postLUTStrength!=0||simd_any(u.extraPostLUTStrengths!=0))return YES;
|
||||
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 YES;}
|
||||
return NO;
|
||||
}
|
||||
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;
|
||||
if(u->skinVisualize<=.5&&!hasLegacyGrade(*u))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
|
||||
@@ -124,6 +126,11 @@ static NSArray *DYPrimaryActiveFinishing(DYUniforms u,NSDictionary *paths,NSData
|
||||
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);
|
||||
}
|
||||
struct DYScopeReadback {
|
||||
id<MTLBuffer> buffer;
|
||||
NSUInteger generation;
|
||||
uint64_t serial;
|
||||
};
|
||||
@interface DYRenderer(){
|
||||
NSMutableDictionary *_devices,*_luts,*_preparedLUTs;
|
||||
NSSet *_activeLUTPaths;
|
||||
@@ -142,6 +149,9 @@ static NSArray *DYPrimaryActiveFinishing(DYUniforms u,NSDictionary *paths,NSData
|
||||
NSUInteger _scopeGeneration;
|
||||
DYScopeSample *_scopePending,*_scopeLatest;
|
||||
uint64_t _scopeCaptureSerial,_scopeLatestSerial;CMTime _scopeRequestedTime;
|
||||
FxImageTile *_scopeDestination; // borrowed for one locked host render
|
||||
CMTime _scopeRenderTime;
|
||||
BOOL _scopeCaptured;
|
||||
NSData *_scopePreview;
|
||||
NSDictionary *_scopeMetadata;
|
||||
DYColorProfile *_scopeProfile;
|
||||
@@ -149,6 +159,22 @@ static NSArray *DYPrimaryActiveFinishing(DYUniforms u,NSDictionary *paths,NSData
|
||||
}
|
||||
@end
|
||||
@implementation DYRenderer
|
||||
// The caller holds the renderer lock. Unused tools never load a GPU function.
|
||||
- (id<MTLComputePipelineState>)computePipeline:(NSString *)name entry:(NSMutableDictionary *)entry error:(NSError **)error {
|
||||
id<MTLComputePipelineState> pipeline=entry[name];
|
||||
if(pipeline)return pipeline;
|
||||
id<MTLLibrary> library=entry[@"library"];
|
||||
id<MTLDevice> device=entry[@"device"];
|
||||
id<MTLFunction> function=[library newFunctionWithName:name];
|
||||
if(!function){fail(error,[name stringByAppendingString:@" 缺失"]);return nil;}
|
||||
uint64_t span=DYBetaSpanBegin(@"gpu_pipeline",@{@"pipeline":name,@"gpu":device.name?:@"unknown"});
|
||||
pipeline=[device newComputePipelineStateWithFunction:function error:error];
|
||||
DYBetaSpanEnd(span,@{@"status":pipeline?@"passed":@"failed"});
|
||||
[function release];
|
||||
if(!pipeline)return nil;
|
||||
entry[name]=pipeline;
|
||||
return [pipeline autorelease];
|
||||
}
|
||||
#include "DYScopeCapture.inc.h"
|
||||
#include "DYFilmRender.inc.h"
|
||||
#include "DYPrimaryRender.inc.h"
|
||||
@@ -192,7 +218,6 @@ static NSArray *DYPrimaryActiveFinishing(DYUniforms u,NSDictionary *paths,NSData
|
||||
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.
|
||||
@@ -210,7 +235,7 @@ static NSArray *DYPrimaryActiveFinishing(DYUniforms u,NSDictionary *paths,NSData
|
||||
- (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);
|
||||
BOOL inputEnabled=u->inputSectionEnabled>.5&&!managed(*u,paths)&&u->preLUTStrength>0;
|
||||
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"];
|
||||
@@ -218,10 +243,20 @@ static NSArray *DYPrimaryActiveFinishing(DYUniforms u,NSDictionary *paths,NSData
|
||||
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;}}
|
||||
for(int i=0;i<5;i++){BOOL active=looksEnabled&&(i==0?u->postLUTStrength>0:u->extraLookSectionEnableds[i-1]>.5&&u->extraPostLUTStrengths[i-1]>0);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)renderHostSource:(FxImageTile*)src destination:(FxImageTile*)dst uniforms:(DYUniforms)u paths:(NSDictionary*)paths spider:(NSData*)spider tools:(NSDictionary*)tools colorManagement:(NSDictionary*)state hostPrimaries:(NSInteger)primaries atTime:(CMTime)time error:(NSError**)error {
|
||||
@synchronized(self){
|
||||
_scopeDestination=dst;_scopeRenderTime=time;_scopeCaptured=NO;
|
||||
@try{
|
||||
BOOL ok=[self renderSource:src destination:dst uniforms:u paths:paths spider:spider tools:tools colorManagement:state hostPrimaries:primaries error:error];
|
||||
if(ok&&!_scopeCaptured)[self captureScopeTile:dst atTime:time];
|
||||
return ok;
|
||||
}@finally{_scopeDestination=nil;}
|
||||
}
|
||||
}
|
||||
- (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];}
|
||||
@@ -232,8 +267,9 @@ static NSArray *DYPrimaryActiveFinishing(DYUniforms u,NSDictionary *paths,NSData
|
||||
|
||||
[_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);
|
||||
BOOL useCM=managed(u,paths)&&(!segmented||stageFirst==0)&&!identityCM(u)&&u.cm.mix>0;
|
||||
u=DYEffectiveFaceUniforms(u,paths,nil);
|
||||
float bwMix=u.blackWhiteSectionEnabled>.5?fmaxf(0,fminf(1,u.cm.blackWhiteMix)):0;
|
||||
// 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;
|
||||
@@ -245,30 +281,100 @@ static NSArray *DYPrimaryActiveFinishing(DYUniforms u,NSDictionary *paths,NSData
|
||||
}
|
||||
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;
|
||||
BOOL preview=NO;
|
||||
@synchronized(_previewLock){preview=(!segmented||stageFirst==0)&&!_exporting&&CACurrentMediaTime()-_lastPreview>.5;}
|
||||
BOOL neutral=!segmented&&!hasMasks&&!spiderBuffer&&u.skinVisualize<.5&&!hasLegacyGrade(u);
|
||||
FxRect s=src.tilePixelBounds,t=dst.tilePixelBounds;
|
||||
BOOL sameGrid=input.width==output.width&&input.height==output.height&&s.left==t.left&&s.right==t.right&&s.top==t.top&&s.bottom==t.bottom;
|
||||
BOOL direct=neutral&&!useCM&&!preview&&sameGrid&&input!=output;
|
||||
NSString *fragment=direct?@"dyNeutralInputFragment":neutral?@"dyNeutralOutputFragment":segmented?@"dySegmentFragment":hasMasks?@"dyMaskedFragment":@"dyFragment";
|
||||
NSMutableDictionary *ps=e[@"pipelines"];
|
||||
NSString *format=[NSString stringWithFormat:@"%lu-%d-%d",(unsigned long)output.pixelFormat,hasMasks,segmented];
|
||||
if(neutral)format=[NSString stringWithFormat:@"%@-%lu",fragment,(unsigned long)output.pixelFormat];
|
||||
id<MTLRenderPipelineState> pipeline=ps[format];
|
||||
if(!pipeline){
|
||||
MTLRenderPipelineDescriptor *desc=[[[MTLRenderPipelineDescriptor alloc]init]autorelease];
|
||||
id<MTLLibrary> library=e[@"library"];
|
||||
desc.vertexFunction=[[library newFunctionWithName:@"dyVertex"]autorelease];
|
||||
desc.fragmentFunction=[[library newFunctionWithName:fragment]autorelease];
|
||||
desc.colorAttachments[0].pixelFormat=output.pixelFormat;
|
||||
pipeline=[[d newRenderPipelineStateWithDescriptor:desc error:err]autorelease];
|
||||
if(!pipeline)return NO;
|
||||
ps[format]=pipeline;
|
||||
}
|
||||
NSArray *ts=neutral?nil:[self textures:paths uniforms:&u entry:e error:err];
|
||||
if(!neutral&&!ts)return NO;
|
||||
id<MTLCommandBuffer> cb=[e[@"queue"]commandBuffer];
|
||||
id<MTLTexture> straight=input;
|
||||
if(!direct){
|
||||
MTLTextureDescriptor *td=[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA16Float width:input.width height:input.height mipmapped:NO];
|
||||
td.storageMode=MTLStorageModePrivate;td.usage=MTLTextureUsageShaderRead|MTLTextureUsageShaderWrite;
|
||||
straight=segmented?nil:e[@"legacyStraight"];
|
||||
if(straight.width!=input.width||straight.height!=input.height){
|
||||
straight=[[d newTextureWithDescriptor:td]autorelease];
|
||||
if(straight&&!segmented)e[@"legacyStraight"]=straight;
|
||||
}
|
||||
if(!straight)return fail(err,@"无法分配调色缓冲区");
|
||||
MTLSize grid=MTLSizeMake(input.width,input.height,1),group=MTLSizeMake(16,16,1);
|
||||
BOOL fused=useCM&&!hasMasks;
|
||||
if(!fused){
|
||||
id<MTLComputePipelineState> unpremultiply=[self computePipeline:@"dyUnpremultiply" entry:e error:err];if(!unpremultiply)return NO;
|
||||
id<MTLComputeCommandEncoder> ce=[cb computeCommandEncoder];
|
||||
[ce setComputePipelineState:unpremultiply];[ce setTexture:input atIndex:0];[ce setTexture:straight atIndex:1];
|
||||
[ce dispatchThreads:grid threadsPerThreadgroup:group];[ce endEncoding];
|
||||
}
|
||||
if(useCM){
|
||||
DayuRenderParams cm=DYCMRender(u.cm);cm.stages=(cm.stages&~1)|(u.inputSectionEnabled>.5?1:0);
|
||||
id<MTLComputePipelineState> cst=[self computePipeline:@"dyColorManagement" entry:e error:err];if(!cst)return NO;
|
||||
id<MTLTexture> converted=fused?straight:[[d newTextureWithDescriptor:td]autorelease];
|
||||
if(!converted)return fail(err,@"无法分配 CST 缓冲区");
|
||||
uint32_t premultiplied=fused;
|
||||
id<MTLComputeCommandEncoder> ce=[cb computeCommandEncoder];
|
||||
[ce setComputePipelineState:cst];[ce setTexture:fused?input: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 setBytes:&premultiplied length:4 atIndex:2];
|
||||
[ce dispatchThreads:grid threadsPerThreadgroup:group];[ce endEncoding];
|
||||
if(hasMasks){
|
||||
id<MTLTexture> blended=[[d newTextureWithDescriptor:td]autorelease];if(!blended)return fail(err,@"无法分配一级蒙版缓冲区");
|
||||
maskHeader.section=0;maskHeader.premultiplied=0;
|
||||
if(![self encodeBlend:straight after:converted output:blended masks:maskBuffer header:maskHeader entry:e commands:cb error:err])return NO;
|
||||
converted=blended;
|
||||
}
|
||||
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,@"原生工具参考纹理不可用");}
|
||||
// Segments retain the original straight source for spatial tools and statistics.
|
||||
id<MTLTexture> nativeSource=segmented&&originalReference?[originalReference metalTextureForDevice:d]:straight;
|
||||
if(!nativeSource)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];}
|
||||
id<MTLBuffer> scene=nil;
|
||||
if(!neutral){
|
||||
float level=.5;
|
||||
scene=[[d newBufferWithBytes:&level length:4 options:MTLResourceStorageModeShared]autorelease];
|
||||
if(!scene)return fail(err,@"无法分配调色统计缓冲区");
|
||||
if(!_exporting&&(!segmented||(stageFirst<=2&&stageLast>=2))&&u.whites>0&&u.whitesEnabled>.5&&u.baseSectionEnabled>.5){
|
||||
id<MTLComputePipelineState> stats=[self computePipeline:@"dyWhitesSceneStatistics" entry:e error:err];if(!stats)return NO;
|
||||
id<MTLComputeCommandEncoder> ce=[cb computeCommandEncoder];
|
||||
[ce setComputePipelineState:stats];[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:0];
|
||||
[ce dispatchThreadgroups:MTLSizeMake(1,1,1) threadsPerThreadgroup:MTLSizeMake(256,1,1)];[ce endEncoding];
|
||||
}
|
||||
}
|
||||
id<MTLBuffer> pb=nil;
|
||||
if(preview){
|
||||
id<MTLComputePipelineState> analysis=[self computePipeline:@"dyInputAnalysis" entry:e error:err];if(!analysis)return NO;
|
||||
pb=[[d newBufferWithLength:320*180*16 options:MTLResourceStorageModeShared]autorelease];
|
||||
if(!pb)return fail(err,@"无法分配输入预览缓冲区");
|
||||
id<MTLComputeCommandEncoder> ce=[cb computeCommandEncoder];
|
||||
[ce setComputePipelineState:analysis];[ce setTexture:straight atIndex:0];[ce setTexture:neutral?e[@"identity"]: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;
|
||||
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;
|
||||
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;
|
||||
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];if(!neutral){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];BOOL ok=[self completeCommands:cb destination:dst texture:output entry:e];
|
||||
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
|
||||
@@ -281,7 +387,7 @@ static NSArray *DYPrimaryActiveFinishing(DYUniforms u,NSDictionary *paths,NSData
|
||||
}else _lookLUTPreview=[_preview retain];
|
||||
_lastPreview=CACurrentMediaTime();[self notifyPreviewChanged];
|
||||
}}
|
||||
if(!ok)fail(err,cb.error.localizedDescription);[pb release];[straight release];[scene release];return ok;
|
||||
if(!ok)fail(err,cb.error.localizedDescription);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){
|
||||
|
||||
在新工单中引用
屏蔽一个用户