WIP: independent tool documents, native panel routing and per-stage GPU masks; host canvas and tracking pending

这个提交包含在:
mac 于 2026-11-01 21:40:54 +08:00 提交
1 父节点 edfb74b171
当前提交 f72aa2609d
21 个文件已更改
+1067 -45

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

+131
查看文件
@@ -1,5 +1,6 @@
#include <metal_stdlib>
#include "DYUniforms.h"
#include "DYToolGPU.h"
#include "SpiderCore/DYColorMath.h"
using namespace metal;
@@ -11,6 +12,12 @@ extern "C" half4 dy_grade(half4 sample, float2 uv, texture2d<half> source,
texture3d<float> hslMixer, texture3d<float> whiteBalance,
texture3d<float> colorGrading, constant DYUniforms& uniforms,
constant float& sceneLevel);
extern "C" half4 dy_gradeMasked(half4 sample, float2 uv, texture2d<half> source,
texture3d<float> preLUT, texture3d<float> look1, texture3d<float> look2,
texture3d<float> look3, texture3d<float> look4, texture3d<float> look5,
texture3d<float> hslMixer, texture3d<float> whiteBalance,
texture3d<float> colorGrading, constant DYUniforms& uniforms,
constant float& sceneLevel,float w0,float w1,float w2,float w3,float w4,float w5,float w6,float w7,float w8,float w9,float w10,float w11);
struct DYRaster {
float4 position [[position]];
@@ -61,6 +68,119 @@ fragment half4 dyFragment(DYRaster in [[stage_in]],
return result;
}
inline float dyToolMaskSoftStep(float low,float high,float value){if(high<=low)return value>=high?1:0;return dySmooth(low,high,value);}
inline float dyToolMaskRangeWeight(float value,thread const DYMaskRange& range){
float low=value>=range.low?1:dyToolMaskSoftStep(range.low-range.softLow,range.low,value);
float high=value<=range.high?1:1-dyToolMaskSoftStep(range.high,range.high+range.softHigh,value);
return low*high;
}
inline float dyToolMaskLeveledCoverage(float raw,thread const DYMaskParameters& p){
float a=dyClamp((raw-p.inputBlack)/(p.inputWhite-p.inputBlack),0,1);
if(p.inputGamma!=1&&a>0&&a<1)a=pow(a,1/p.inputGamma);
return p.outputBlack+(p.outputWhite-p.outputBlack)*a;
}
inline float dyToolMaskFinalCoverage(float a,thread const DYMaskParameters& p){
if(p.flags&DYMaskInvert)a=1-a;
return dyClamp(a*p.strength,0,1);
}
inline float dyToolMaskLevels(float raw,thread const DYMaskParameters& p){return dyToolMaskFinalCoverage(dyToolMaskLeveledCoverage(raw,p),p);}
inline float dyToolMaskHSLWeight(float3 source,thread const DYMaskParameters& p){
float3 c=dyLimit(source);float lo=c.x<c.y?(c.x<c.z?c.x:c.z):(c.y<c.z?c.y:c.z),hi=c.x>c.y?(c.x>c.z?c.x:c.z):(c.y>c.z?c.y:c.z);
// Same signed/HDR interval convention as the quick HSL tools. This is code
// RGB selection; use LumaKey levels when absolute HDR brightness is needed.
float base=lo<0?lo:0,scale=hi-base;if(scale<1)scale=1;
float3 hsl=dyRGBHSL((c-float3{base,base,base})/scale);float weight=1;
if(p.flags&DYMaskHueEnabled){
float distance=dyHueDistance(hsl.x,p.hueCenterTurns),outer=p.hueCoreHalfWidth+p.hueSoftWidth;if(outer>.5f)outer=.5f;
float hue=p.hueCoreHalfWidth>=.5f||distance<=p.hueCoreHalfWidth?1:1-dyToolMaskSoftStep(p.hueCoreHalfWidth,outer,distance);
float chroma=(hi-lo)/scale;weight*=hue*dyToolMaskSoftStep(p.neutralChromaLow,p.neutralChromaHigh,chroma);
}
if(p.flags&DYMaskSaturationEnabled)weight*=dyToolMaskRangeWeight(hsl.y,p.saturationRange);
if(p.flags&DYMaskLightnessEnabled)weight*=dyToolMaskRangeWeight(hsl.z,p.lightnessRange);
return weight;
}
inline float dyToolMaskShapeWeight(float x,float y,thread const DYMaskParameters& p,thread const DYMaskPath& path){
float dx=(x-p.centerX)*p.imageAspect,dy=y-p.centerY,ca=cos(p.angle),sa=sin(p.angle);
float px=dx*ca+dy*sa,py=-dx*sa+dy*ca;
if(p.kind==DYMaskLinearGradient){
if(p.gradientWidth==0)return px>=0?1:0;
return dyToolMaskSoftStep(0,1,.5f+px/(p.gradientWidth>1e-12f?p.gradientWidth:1e-12f));
}
float physicalWidth=p.width*p.imageAspect,physicalHeight=p.height;if(physicalWidth<1e-12f)physicalWidth=1e-12f;if(physicalHeight<1e-12f)physicalHeight=1e-12f;
if(p.kind==DYMaskEllipse){float u=px/(physicalWidth*.5f),v=py/(physicalHeight*.5f),r=sqrt(u*u+v*v);if(p.feather==0)return r<=1?1:0;if(p.version==2&&(p.flags&DYMaskFeatherOutsideFlag))return 1-dyToolMaskSoftStep(1,1+p.feather,r);return 1-dyToolMaskSoftStep(1-p.feather,1,r);}
float shorter=physicalWidth<physicalHeight?physicalWidth:physicalHeight;
if(p.kind==DYMaskRectangle){float ax=px<0?-px:px,ay=py<0?-py:py,a=physicalWidth*.5f-ax,b=physicalHeight*.5f-ay;if(p.version==2&&(p.flags&DYMaskFeatherOutsideFlag)){if(p.feather==0)return a>=0&&b>=0?1:0;return dyToolMaskSoftStep(-p.feather*.5f*shorter,0,a<b?a:b);}if(a<0||b<0)return 0;if(p.feather==0)return 1;return dyToolMaskSoftStep(0,p.feather*.5f,(a<b?a:b)/shorter);}
if(path.count<3||path.count>256)return 0;
float u=px/physicalWidth,v=py/physicalHeight,sx=physicalWidth/shorter,sy=physicalHeight/shorter,distance2=1e10f;bool inside=false;
for(int i=0,j=path.count-1;i<path.count;j=i++){
float ax=path.x[j],ay=path.y[j],bx=path.x[i],by=path.y[i];if((ay>v)!=(by>v))if(u<(bx-ax)*(v-ay)/(by-ay)+ax)inside=!inside;
float ex=(bx-ax)*sx,ey=(by-ay)*sy,xx=(u-ax)*sx,yy=(v-ay)*sy,den=ex*ex+ey*ey,t=den>1e-12f?dyClamp((xx*ex+yy*ey)/den,0,1):0;
xx-=t*ex;yy-=t*ey;float d=xx*xx+yy*yy;if(d<distance2)distance2=d;
}
if(!inside)return 0;if(p.feather==0)return 1;return dyToolMaskSoftStep(0,p.feather*.5f,sqrt(distance2));
}
inline float dyToolMaskRawCoverage(float x,float y,float3 source,thread const DYMaskParameters& p,thread const DYMaskPath& path){return p.kind==DYMaskHSLKey?dyToolMaskHSLWeight(source,p):(p.kind==DYMaskLumaKey?dyLuma(dyLimit(source)):dyToolMaskShapeWeight(x,y,p,path));}
inline float dyToolMaskOperator(float x,float y,float3 source,thread const DYMaskParameters& p,thread const DYMaskPath& path){
if((p.version!=1&&p.version!=2)||p.kind>DYMaskLumaKey)return 0;
float raw=dyToolMaskRawCoverage(x,y,source,p,path);
return dyToolMaskLevels(raw,p);
}
inline float dyToolMask(float x,float y,float3 source,device const DYResolvedMask& value){DYResolvedMask local=value;return dyToolMaskOperator(x,y,source,local.parameters,local.path);}
inline float3 dyToolEncode(float3 linear){return select(1.055f*pow(max(linear,float3(0)),float3(1.f/2.4f))-.055f,linear*12.92f,linear<=.0031308f);}
inline float3 dyToolDecode(float3 encoded){return select(pow(max((encoded+.055f)/1.055f,float3(0)),float3(2.4f)),encoded/12.92f,encoded<=.04045f);}
inline float dyToolCoverage(float2 uv,float3 source,device const DYToolMaskEntry* masks,constant DYToolMaskHeader& h,int section){
float x=h.offsetX+uv.x*h.scaleX,y=h.offsetY+uv.y*h.scaleY;float coverage=1;bool found=false;
for(uint i=0;i<h.count;i++){if(masks[i].section!=section)continue;
// Copy device data to thread storage; shared mask math accepts constant
// descriptors, so evaluate through the address-space-neutral copy below.
float a=dyToolMask(x,y,source,masks[i].mask);
if(!found){coverage=masks[i].combine==1?1-a:a;found=true;}
else if(masks[i].combine==1)coverage*=1-a;else if(masks[i].combine==2)coverage*=a;else coverage=1-(1-coverage)*(1-a);
}return clamp(coverage,0.f,1.f);
}
fragment half4 dyMaskedFragment(DYRaster in [[stage_in]],
texture2d<half> source [[texture(0)]],
texture3d<float> preLUT [[texture(1)]],
texture3d<float> look1 [[texture(2)]],
texture3d<float> look2 [[texture(3)]],
texture3d<float> look3 [[texture(4)]],
texture3d<float> look4 [[texture(5)]],
texture3d<float> look5 [[texture(6)]],
texture3d<float> hslMixer [[texture(7)]],
texture3d<float> whiteBalance [[texture(8)]],
texture3d<float> colorGrading [[texture(9)]],
constant DYUniforms& uniforms [[buffer(0)]],
constant float& sceneLevel [[buffer(1)]],
constant DYRenderOptions& options [[buffer(2)]],
constant DYUniforms& colorUniforms [[buffer(3)]],
device const float* spiderSamples [[buffer(4)]],
constant int2& spiderSize [[buffer(5)]],
device const DYToolMaskEntry* masks [[buffer(6)]],
constant DYToolMaskHeader& maskHeader [[buffer(7)]]) {
constexpr sampler sampleFilter(coord::normalized, address::clamp_to_edge, filter::linear);
half4 sample = source.sample(sampleFilter, in.uv);
if (!(sample.a > half(0.0))) return half4(0.0h);
float weights[12];float3 encoded=dyToolEncode(float3(sample.rgb));
for(int i=0;i<12;i++)weights[i]=dyToolCoverage(in.uv,encoded,masks,maskHeader,i);
half4 result = dy_gradeMasked(sample, in.uv, source, preLUT, look1, look2, look3,
look4, look5, hslMixer, whiteBalance, colorGrading, uniforms, sceneLevel,weights[0],weights[1],weights[2],weights[3],weights[4],weights[5],weights[6],weights[7],weights[8],weights[9],weights[10],weights[11]);
if(options.blackWhiteMix>0.0f && options.blackWhiteMix<1.0f) {
half4 color=dy_gradeMasked(sample,in.uv,source,preLUT,look1,look2,look3,look4,look5,hslMixer,whiteBalance,colorGrading,colorUniforms,sceneLevel,weights[0],weights[1],weights[2],weights[3],weights[4],weights[5],weights[6],weights[7],weights[8],weights[9],weights[10],weights[11]);
result.rgb=mix(color.rgb,result.rgb,half(options.blackWhiteMix));
}
if(spiderSize.x>0 && uniforms.spiderSectionEnabled>.5f && uniforms.spiderMix>0.0f){
float3 color=float3(result.rgb);float3 warped=dyVariableSpider(color,spiderSize.x,spiderSize.y,spiderSamples);
result.rgb=half3(dyLimit(mix(color,warped,uniforms.spiderMix*weights[1])));
}
result.a = sample.a;
if (options.outputPremultiplied) result.rgb *= result.a;
return result;
}
// Optional boundary conversion. Use this before both statistics and grading
// when the host supplies premultiplied RGB; all neighborhood samples then see
// the same straight RGB representation. Source and destination must differ.
@@ -144,3 +264,14 @@ kernel void dyColorManagement(texture2d<float,access::read> source [[texture(0)]
c.rgb=mix(raw.rgb,c.rgb,amount);
destination.write(half4(half3(dyDecode(c.rgb)),half(sample.a)),pos);
}
// Premultiplication is preserved by mixing the two equally-alpha images.
kernel void dyToolBlend(texture2d<float,access::read> before [[texture(0)]],texture2d<float,access::read> after [[texture(1)]],texture2d<half,access::write> output [[texture(2)]],device const DYToolMaskEntry* masks [[buffer(0)]],constant DYToolMaskHeader& h [[buffer(1)]],uint2 pos [[thread_position_in_grid]]){
if(pos.x>=output.get_width()||pos.y>=output.get_height())return;float4 a=before.read(pos),b=after.read(pos);float2 uv=(float2(pos)+.5f)/float2(output.get_width(),output.get_height());float amount=dyToolCoverage(uv,dyToolEncode(a.a>0?(h.premultiplied?a.rgb/a.a:a.rgb):float3(0)),masks,h,h.section);output.write(half4(mix(a,b,amount)),pos);
}
extern "C" float3 dy_skinExtra(float3 color,float3 reference,constant DYUniforms& uniforms);
kernel void dyToolSkin(texture2d<float,access::read> before [[texture(0)]],texture2d<float> reference [[texture(1)]],texture2d<half,access::write> output [[texture(2)]],constant DYUniforms& u [[buffer(0)]],uint2 pos [[thread_position_in_grid]]){
if(pos.x>=output.get_width()||pos.y>=output.get_height())return;constexpr sampler f(coord::normalized,address::clamp_to_edge,filter::linear);float4 a=before.read(pos),b=reference.sample(f,(float2(pos)+.5f)/float2(output.get_width(),output.get_height()));if(a.a>0){float3 c=dy_skinExtra(dyToolEncode(a.rgb/a.a),dyToolEncode(b.a>0?b.rgb/b.a:float3(0)),u);a.rgb=dyToolDecode(c)*a.a;}output.write(half4(a),pos);
}
fragment half4 dyToolCopyFragment(DYRaster in [[stage_in]],texture2d<half> source [[texture(0)]]){constexpr sampler f(coord::normalized,address::clamp_to_edge,filter::linear);return source.sample(f,in.uv);}