536 行
33 KiB
Metal
536 行
33 KiB
Metal
#include <metal_stdlib>
|
|
#include "DYUniforms.h"
|
|
#include "DYToolGPU.h"
|
|
#include "SpiderCore/DYColorMath.h"
|
|
using namespace metal;
|
|
|
|
// The linked, versioned AIR helper owns color arithmetic only. Its source is in
|
|
// tools/kernels; production builds never read the reference application.
|
|
extern "C" half4 dy_grade(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);
|
|
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,float skinVisualize);
|
|
extern "C" half4 dy_gradeSegment(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,float skinVisualize,uint stageFirst,uint stageLast,float3 skinReference,float3 originalReference);
|
|
|
|
struct DYRaster {
|
|
float4 position [[position]];
|
|
float2 uv;
|
|
};
|
|
|
|
vertex DYRaster dyVertex(uint vertexID [[vertex_id]],
|
|
constant DYVertex2D* vertices [[buffer(0)]],
|
|
constant uint2& viewportSize [[buffer(1)]]) {
|
|
DYRaster result;
|
|
result.position = float4(vertices[vertexID].position / (float2(viewportSize) * 0.5f), 0.0f, 1.0f);
|
|
result.uv = vertices[vertexID].textureCoordinate;
|
|
return result;
|
|
}
|
|
|
|
fragment half4 dyFragment(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)]]) {
|
|
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);
|
|
half4 result = dy_grade(sample, in.uv, source, preLUT, look1, look2, look3,
|
|
look4, look5, hslMixer, whiteBalance, colorGrading, uniforms, sceneLevel);
|
|
if(options.blackWhiteMix>0.0f && options.blackWhiteMix<1.0f) {
|
|
half4 color=dy_grade(sample,in.uv,source,preLUT,look1,look2,look3,look4,look5,hslMixer,whiteBalance,colorGrading,colorUniforms,sceneLevel);
|
|
result.rgb=mix(color.rgb,result.rgb,half(options.blackWhiteMix));
|
|
}
|
|
if(uniforms.skinVisualize<.5f && 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)));
|
|
}
|
|
result.a = sample.a;
|
|
if (options.outputPremultiplied) result.rgb *= result.a;
|
|
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,device const DYMaskPath& path,
|
|
float4 pathBounds=float4(-INFINITY,-INFINITY,INFINITY,INFINITY)){
|
|
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;
|
|
if(u<pathBounds.x||v<pathBounds.y||u>pathBounds.z||v>pathBounds.w)return 0;
|
|
float 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,device 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,device 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){DYMaskParameters parameters=value.parameters;return dyToolMaskOperator(x,y,source,parameters,value.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;
|
|
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);
|
|
}
|
|
if(section==3&&h.faceOnly){
|
|
float face=0;
|
|
for(uint i=0;i<h.count;i++){
|
|
device const DYToolMaskEntry &entry=masks[i];
|
|
if(entry.section==-100&&x>=entry.minX&&x<=entry.maxX&&y>=entry.minY&&y<=entry.maxY)
|
|
face=1-(1-face)*(1-dyToolMask(x,y,source,entry.mask));
|
|
}
|
|
if(face>0)for(uint i=0;i<h.count;i++)if(masks[i].section==-101)face*=1-dyToolMask(x,y,source,masks[i].mask);
|
|
coverage*=face;
|
|
}
|
|
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],(uniforms.skinProtection.x>.5?uniforms.skinVisualize:0));
|
|
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],(uniforms.skinProtection.x>.5?uniforms.skinVisualize:0));
|
|
result.rgb=mix(color.rgb,result.rgb,half(options.blackWhiteMix));
|
|
}
|
|
if(uniforms.skinVisualize<.5f && 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;
|
|
}
|
|
|
|
fragment half4 dySegmentFragment(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)]],
|
|
constant DYToolStageRange& stages [[buffer(8)]],
|
|
texture2d<half> skinReference [[texture(10)]],
|
|
texture2d<half> nativeSource [[texture(11)]]) {
|
|
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);
|
|
half4 originalSample=nativeSource.sample(sampleFilter,in.uv);
|
|
float weights[12];float3 encoded=dyToolEncode(float3(originalSample.rgb));
|
|
for(int i=0;i<12;i++)weights[i]=dyToolCoverage(in.uv,encoded,masks,maskHeader,i);
|
|
if(stages.suppressBlackWhite)weights[10]=0;
|
|
half4 reference=skinReference.sample(sampleFilter,in.uv);
|
|
float3 skinEncoded=dyToolEncode(reference.a>half(0)?float3(reference.rgb/reference.a):float3(0));
|
|
half4 result = dy_gradeSegment(sample, in.uv, nativeSource, 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],(uniforms.skinProtection.x>.5?uniforms.skinVisualize:0),uint(stages.first),uint(stages.last),skinEncoded,encoded);
|
|
if(stages.first<=9 && stages.last>=9 && options.blackWhiteMix>0.0f && options.blackWhiteMix<1.0f) {
|
|
half4 color=dy_gradeSegment(sample,in.uv,nativeSource,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],(uniforms.skinProtection.x>.5?uniforms.skinVisualize:0),uint(stages.first),uint(stages.last),skinEncoded,encoded);
|
|
result.rgb=mix(color.rgb,result.rgb,half(options.blackWhiteMix));
|
|
}
|
|
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.
|
|
inline half4 dyStraightSample(float4 sample) {
|
|
if (!(sample.a > 0.0f) || !all(isfinite(sample))) return half4(0.0h);
|
|
return half4(half3(sample.rgb / sample.a), half(sample.a));
|
|
}
|
|
|
|
kernel void dyUnpremultiply(texture2d<float, access::read> source [[texture(0)]],
|
|
texture2d<half, access::write> destination [[texture(1)]],
|
|
uint2 pixel [[thread_position_in_grid]]) {
|
|
if (pixel.x >= destination.get_width() || pixel.y >= destination.get_height()) return;
|
|
destination.write(dyStraightSample(source.read(pixel)), pixel);
|
|
}
|
|
|
|
// Match the historical half-precision alpha round trip without an intermediate
|
|
// texture when source and destination cover the same pixel grid.
|
|
fragment half4 dyNeutralInputFragment(DYRaster in [[stage_in]],texture2d<float,access::read> source [[texture(0)]]) {
|
|
uint2 size(source.get_width(),source.get_height());
|
|
half4 c=dyStraightSample(source.read(min(uint2(in.uv*float2(size)),size-1)));
|
|
return c.a>0.0h?half4(c.rgb*c.a,c.a):half4(0.0h);
|
|
}
|
|
|
|
fragment half4 dyNeutralOutputFragment(DYRaster in [[stage_in]],texture2d<half> source [[texture(0)]]) {
|
|
constexpr sampler f(coord::normalized,address::clamp_to_edge,filter::linear);
|
|
half4 c=source.sample(f,in.uv);
|
|
return c.a>0.0h?half4(c.rgb*c.a,c.a):half4(0.0h);
|
|
}
|
|
|
|
// Per-instance CPU analysis thumbnail. Host dispatches exactly 64x64 threads;
|
|
// RGB is sRGB encoded, alpha is unchanged. HDR and negative values are retained
|
|
// by the standard piecewise transfer rather than silently clipping the sample.
|
|
kernel void dyPreview(texture2d<half> source [[texture(0)]],
|
|
device float4* outRGBA [[buffer(0)]],
|
|
uint2 pixel [[thread_position_in_grid]]) {
|
|
if (pixel.x >= 64 || pixel.y >= 64) return;
|
|
constexpr sampler sampleFilter(coord::normalized, address::clamp_to_edge, filter::linear);
|
|
half4 sample = source.sample(sampleFilter, (float2(pixel) + 0.5f) / 64.0f);
|
|
float3 linear = float3(sample.rgb);
|
|
float3 power = 1.055f * pow(max(linear, float3(0.0f)), float3(1.0f / 2.4f)) - 0.055f;
|
|
float3 encoded = select(power, linear * 12.92f, linear <= 0.0031308f);
|
|
outRGBA[pixel.y * 64 + pixel.x] = float4(encoded, float(sample.a));
|
|
}
|
|
|
|
// Analysis resolution matches the reference auto-grade sample dimensions.
|
|
kernel void dyAnalysis(texture2d<half> source [[texture(0)]],
|
|
device float4* outRGBA [[buffer(0)]],
|
|
uint2 pixel [[thread_position_in_grid]]) {
|
|
if (pixel.x >= 320 || pixel.y >= 180) return;
|
|
constexpr sampler sampleFilter(coord::normalized, address::clamp_to_edge, filter::linear);
|
|
half4 sample = source.sample(sampleFilter, (float2(pixel) + 0.5f) / float2(320.0f, 180.0f));
|
|
float3 linear = float3(sample.rgb);
|
|
float3 power = 1.055f * pow(max(linear, float3(0.0f)), float3(1.0f / 2.4f)) - 0.055f;
|
|
float3 encoded = select(power, linear * 12.92f, linear <= 0.0031308f);
|
|
outRGBA[pixel.y * 320 + pixel.x] = float4(encoded, float(sample.a));
|
|
}
|
|
|
|
// Sample the actual final host destination before returning its tile. Point
|
|
// sampling avoids blending away nonfinite/out-of-range pixels. This is a
|
|
// bounded display-reference sample, not a full-resolution precision scope.
|
|
kernel void dyScopeSample(texture2d<float,access::read> source [[texture(0)]],
|
|
device float4* outRGBA [[buffer(0)]],constant uint& flip [[buffer(1)]],
|
|
uint2 pixel [[thread_position_in_grid]]) {
|
|
if(pixel.x>=320||pixel.y>=180)return;
|
|
uint2 xy=min(uint2((float2(pixel)+.5f)*float2(source.get_width(),source.get_height())/float2(320,180)),uint2(source.get_width()-1,source.get_height()-1));
|
|
if(flip)xy.y=source.get_height()-1-xy.y;
|
|
outRGBA[pixel.y*320+pixel.x]=source.read(xy);
|
|
}
|
|
|
|
// 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<half> source, texture3d<float> 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<half> source [[texture(0)]],
|
|
texture3d<float> 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;
|
|
outRGBA[pixel.y*320+pixel.x]=dyInputPreviewPixel(source,inputLUT,inputStrength,(float2(pixel)+.5f)/float2(320,180),true);
|
|
}
|
|
|
|
kernel void dyInputLookPreview(texture2d<half> source [[texture(0)]],
|
|
texture3d<float> inputLUT [[texture(1)]], device float4* outRGBA [[buffer(0)]],
|
|
constant float& inputStrength [[buffer(1)]],constant uint4& geometry [[buffer(2)]],
|
|
uint2 pixel [[thread_position_in_grid]]) {
|
|
uint2 size=geometry.xy;
|
|
if(any(pixel>=size))return;
|
|
uint2 sample=uint2(pixel.x,geometry.z?size.y-1-pixel.y:pixel.y);
|
|
outRGBA[pixel.y*size.x+pixel.x]=dyInputPreviewPixel(source,inputLUT,inputStrength,(float2(sample)+.5f)/float2(size),geometry.w!=0);
|
|
}
|
|
|
|
// CST/BASIS arithmetic is reused verbatim from the versioned dayu-cst source.
|
|
float4 cst_air(half4 sample, constant CSTParams& params);
|
|
float4 basis_air(half4 sample, constant BasisParams& params);
|
|
inline float3 dyEncode(float3 linear) {
|
|
return select(1.055f*pow(max(linear,float3(0)),float3(1.0f/2.4f))-.055f,linear*12.92f,linear<=.0031308f);
|
|
}
|
|
inline float3 dyDecode(float3 encoded) {
|
|
return select(pow(max((encoded+.055f)/1.055f,float3(0)),float3(2.4f)),encoded/12.92f,encoded<=.04045f);
|
|
}
|
|
kernel void dyColorManagement(texture2d<float,access::read> source [[texture(0)]],
|
|
texture2d<half,access::write> destination [[texture(1)]],
|
|
constant DayuRenderParams& p [[buffer(0)]],constant float& amount [[buffer(1)]],
|
|
constant uint& premultiplied [[buffer(2)]],uint2 pos [[thread_position_in_grid]]) {
|
|
if(pos.x>=source.get_width()||pos.y>=source.get_height())return;
|
|
float4 sample=source.read(pos);
|
|
if(premultiplied)sample=float4(dyStraightSample(sample));
|
|
if(!(sample.a>0)){destination.write(half4(0),pos);return;}
|
|
float4 raw=float4(dyEncode(sample.rgb),sample.a),c=raw;
|
|
if(p.stages&1)c=cst_air(half4(c),p.input);
|
|
if(p.stages&2){if(p.reserved&8)c=cst_air(half4(c),p.basisInput);c=basis_air(half4(c),p.basis);if(p.reserved&8)c=cst_air(half4(c),p.basisOutput);}
|
|
if(p.stages&4)c=cst_air(half4(c),p.output);
|
|
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());
|
|
float3 keyColor=float3(0);
|
|
if(h.keySources&&a.a>0)keyColor=dyToolEncode(h.premultiplied?a.rgb/a.a:a.rgb);
|
|
float amount=dyToolCoverage(uv,keyColor,masks,h,h.section);
|
|
output.write(half4(h.skinPreview?float4(b.rgb*amount,a.a):mix(a,b,amount)),pos);
|
|
}
|
|
extern "C" float dy_skinWeight(float3 color,float3 reference,constant DYUniforms& uniforms);
|
|
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=u.skinProtection.x>.5&&u.skinVisualize>.5?float3(dy_skinWeight(float3(0),dyToolEncode(b.a>0?b.rgb/b.a:float3(0)),u)*clamp(u.skinProtection.y*.01f,0.f,1.f)*a.a):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);}
|
|
|
|
#include "DYFilmKernels.inc.h"
|
|
#include "DYManagedKernels.inc.h"
|
|
#include "DYPrimaryKernels.inc.h"
|
|
|
|
// Inverse radial sampling; alpha stays premultiplied through interpolation.
|
|
kernel void dyLensRepair(texture2d<float> source [[texture(0)]],texture2d<float,access::write> output [[texture(1)]],constant DYUniforms& u [[buffer(0)]],constant float& aspect [[buffer(1)]],uint2 p [[thread_position_in_grid]]){
|
|
if(p.x>=output.get_width()||p.y>=output.get_height())return;float2 uv=(float2(p)+.5f)/float2(output.get_width(),output.get_height()),center=float2(u.lensCenterX,u.lensCenterY);float2 q=(uv-center)/max(1.f,u.lensScale),metric=q*float2(aspect,1);float2 corner=max(center,1-center)*float2(aspect,1);float radius=dot(metric,metric)/max(1e-6f,dot(corner,corner));float2 sample=center+q*(1+clamp(u.lensStrength,-100.f,100.f)*.0025f*radius);
|
|
constexpr sampler f(coord::normalized,address::clamp_to_zero,filter::linear);output.write(source.sample(f,sample),p);
|
|
}
|
|
|
|
kernel void dyCloneStamps(texture2d<float,access::read> input [[texture(0)]],
|
|
texture2d<float,access::write> output [[texture(1)]],
|
|
constant DYCloneStamp *stamps [[buffer(0)]],
|
|
constant uint &count [[buffer(1)]],
|
|
constant uint &flip [[buffer(2)]],
|
|
uint2 gid [[thread_position_in_grid]]) {
|
|
uint2 size(output.get_width(),output.get_height());
|
|
if(any(gid>=size))return;
|
|
float2 uv=(float2(gid)+.5f)/float2(size);
|
|
if(flip)uv.y=1-uv.y;
|
|
float4 color=input.read(gid);
|
|
for(uint i=0;i<count;i++){
|
|
constant DYCloneStamp &stamp=stamps[i];
|
|
float2 center=float2(stamp.centerX,stamp.centerY);
|
|
float2 local=(uv-center)*float2(stamp.inverseRadiusX,stamp.inverseRadiusY);
|
|
float radiusSquared=dot(local,local);
|
|
if(radiusSquared>1)continue;
|
|
float2 sampleCenter(stamp.sourceX,stamp.sourceY);
|
|
float2 sampleScale(stamp.samplingScaleX,stamp.samplingScaleY);
|
|
float4 sampled;
|
|
float sourceCoverage=1;
|
|
float2 offset=(sampleCenter-center+(uv-center)*(sampleScale-1))*float2(size);
|
|
if(flip)offset.y=-offset.y;
|
|
if(!stamp.subpixel){
|
|
// Preserve pixel values for stamps with translation alone.
|
|
int2 source=int2(gid)+int2(round(offset));
|
|
if(any(source<0)||any(source>=int2(size)))continue;
|
|
sampled=input.read(uint2(source));
|
|
}else{
|
|
// Follow fractional motion through unit zoom and fade the sample
|
|
// across the last pixel instead of dropping it at the image edge.
|
|
float2 source=float2(gid)+offset;
|
|
float2 edge=min(source+1,float2(size)-source);
|
|
sourceCoverage=saturate(min(edge.x,edge.y));
|
|
if(sourceCoverage<=0)continue;
|
|
float2 pixel=clamp(source,0.f,float2(size-1));
|
|
uint2 lo=uint2(floor(pixel)),hi=min(lo+1,size-1);
|
|
float2 blend=fract(pixel);
|
|
float4 top=mix(input.read(lo),input.read(uint2(hi.x,lo.y)),blend.x);
|
|
float4 bottom=mix(input.read(uint2(lo.x,hi.y)),input.read(hi),blend.x);
|
|
sampled=mix(top,bottom,blend.y);
|
|
}
|
|
float coverage=stamp.feather>0?1-smoothstep(1-stamp.feather,1.f,sqrt(radiusSquared)):1;
|
|
coverage=saturate((coverage-stamp.inputBlack)/(stamp.inputWhite-stamp.inputBlack));
|
|
if(stamp.inputGamma!=1)coverage=pow(coverage,1/stamp.inputGamma);
|
|
color=mix(color,sampled,coverage*stamp.strength*sourceCoverage);
|
|
}
|
|
output.write(color,gid);
|
|
}
|
|
|
|
// Block-centre sampling is stable in image coordinates and never feeds back.
|
|
kernel void dyLocalMosaic(texture2d<float,access::read> input [[texture(0)]],texture2d<float,access::write> output [[texture(1)]],device const DYToolMaskEntry* masks [[buffer(0)]],constant DYToolMaskHeader& h [[buffer(1)]],constant uint& block [[buffer(2)]],uint2 p [[thread_position_in_grid]]) {
|
|
uint2 size=uint2(output.get_width(),output.get_height());if(any(p>=size))return;
|
|
float4 original=input.read(p);
|
|
float2 uv=(float2(p)+.5f)/float2(size);
|
|
float x=h.offsetX+uv.x*h.scaleX,y=h.offsetY+uv.y*h.scaleY;
|
|
float coverage=0;
|
|
if(h.section==-102){
|
|
for(uint i=0;i<h.count;i++){
|
|
device const auto &entry=masks[i];
|
|
if(entry.section==-102&&x>=entry.minX&&x<=entry.maxX&&y>=entry.minY&&y<=entry.maxY)
|
|
coverage=max(coverage,dyToolMask(x,y,float3(0),entry.mask));
|
|
}
|
|
}else{
|
|
bool found=false,encoded=false;
|
|
float3 code=0;
|
|
for(uint i=0;i<h.count;i++){
|
|
device const auto &entry=masks[i];
|
|
if(entry.section!=h.section)continue;
|
|
DYMaskParameters parameters=entry.mask.parameters;
|
|
if(!encoded&¶meters.kind>=DYMaskHSLKey){
|
|
code=dyToolEncode(original.a>0?original.rgb/original.a:float3(0));
|
|
encoded=true;
|
|
}
|
|
// Apply levels and inversion even when the path has no geometric coverage.
|
|
float amount=parameters.kind==DYMaskBezier
|
|
?dyToolMaskLevels(dyToolMaskShapeWeight(x,y,parameters,entry.mask.path,float4(entry.minX,entry.minY,entry.maxX,entry.maxY)),parameters)
|
|
:dyToolMaskOperator(x,y,code,parameters,entry.mask.path);
|
|
coverage=found?1-(1-coverage)*(1-amount):amount;
|
|
found=true;
|
|
}
|
|
}
|
|
if(coverage<=0){output.write(original,p);return;}
|
|
uint2 q=min((p/block)*block+block/2,size-1);float4 sample=input.read(q);
|
|
// Preserve the pixel's original alpha and prevent transparent samples turning
|
|
// into arbitrary RGB on an otherwise opaque face.
|
|
float3 rgb=sample.a>0?sample.rgb/sample.a*original.a:original.rgb;
|
|
output.write(float4(mix(original.rgb,rgb,coverage),original.a),p);
|
|
}
|
|
|
|
// Extend bounded creative operators by their boundary delta. The float32
|
|
// residual carries negative / above-one values around the historical bounded
|
|
// kernels. Neutral tools skip this adapter entirely. This is a declared code
|
|
// domain extension, not a tone map and not an intermediate output transform.
|
|
kernel void dyUnifiedBridgeIn(texture2d<float,access::read> a [[texture(0)]],texture2d<float,access::write> b [[texture(1)]],uint2 p [[thread_position_in_grid]]){
|
|
if(any(p>=uint2(b.get_width(),b.get_height())))return;float4 c=a.read(p);if(c.a>0)c.rgb=dyToolDecode(clamp(c.rgb/c.a,0.f,1.f))*c.a;b.write(c,p);
|
|
}
|
|
kernel void dyUnifiedBridgeOut(texture2d<float,access::read> raw [[texture(0)]],texture2d<float,access::read> changed [[texture(1)]],texture2d<float,access::write> out [[texture(2)]],uint2 p [[thread_position_in_grid]]){
|
|
if(any(p>=uint2(out.get_width(),out.get_height())))return;float4 a=raw.read(p),b=changed.read(p);if(a.a>0&&b.a>0){float3 c=a.rgb/a.a;float3 delta=dyToolEncode(b.rgb/b.a)-clamp(c,0.f,1.f);a.rgb=(c+delta)*a.a;}out.write(a,p);
|
|
}
|
|
|
|
struct DYUnifiedMix {CSTParams toLinear,fromLinear,originalTo709;float amount;};
|
|
kernel void dyUnifiedBlend(texture2d<float,access::read> before [[texture(0)]],texture2d<float,access::read> after [[texture(1)]],texture2d<float,access::write> output [[texture(2)]],texture2d<float,access::read> original [[texture(3)]],device const DYToolMaskEntry* masks [[buffer(0)]],constant DYToolMaskHeader& h [[buffer(1)]],constant DYUnifiedMix& p [[buffer(2)]],uint2 xy [[thread_position_in_grid]]){
|
|
if(any(xy>=uint2(output.get_width(),output.get_height())))return;
|
|
float4 a=before.read(xy),b=after.read(xy);
|
|
if(!(a.a>0)||!(b.a>0)){output.write(a,xy);return;}
|
|
float3 ac=0,code=0,source=0;
|
|
if(h.keySources&DYToolMaskLayerKey){
|
|
ac=cst_float_air(float4(a.rgb/a.a,1),p.toLinear).rgb;
|
|
code=dyToolEncode(cst_float_air(float4(ac,1),dyPrimaryAP1To709).rgb);
|
|
}
|
|
if(h.keySources&DYToolMaskOriginalKey){
|
|
float4 o=original.read(xy);
|
|
if(o.a>0)source=dyToolEncode(cst_float_air(float4(o.rgb/o.a,1),p.originalTo709).rgb);
|
|
}
|
|
float2 uv=(float2(xy)+.5f)/float2(output.get_width(),output.get_height());
|
|
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==h.section){
|
|
float weight=dyToolMask(x,y,masks[i].mask.parameters.sourceStage==DYMaskSourceOriginal?source:code,masks[i].mask);
|
|
if(!found){coverage=masks[i].combine==1?1-weight:weight;found=true;}
|
|
else if(masks[i].combine==1)coverage*=1-weight;
|
|
else if(masks[i].combine==2)coverage*=weight;
|
|
else coverage=1-(1-coverage)*(1-weight);
|
|
}
|
|
float w=coverage*p.amount;
|
|
if(w<=0){output.write(a,xy);return;}
|
|
if(w>=1){output.write(b,xy);return;}
|
|
if(!(h.keySources&DYToolMaskLayerKey))ac=cst_float_air(float4(a.rgb/a.a,1),p.toLinear).rgb;
|
|
float3 bc=cst_float_air(float4(b.rgb/b.a,1),p.toLinear).rgb;
|
|
output.write(float4(cst_float_air(float4(mix(ac,bc,w),1),p.fromLinear).rgb*a.a,a.a),xy);
|
|
}
|
|
kernel void dyUnifiedExposure(texture2d<float,access::read> a [[texture(0)]],texture2d<float,access::write> b [[texture(1)]],constant float& gain [[buffer(0)]],uint2 p [[thread_position_in_grid]]){
|
|
if(any(p>=uint2(b.get_width(),b.get_height())))return;float4 c=a.read(p);if(c.a>0)c.rgb*=gain;b.write(c,p);
|
|
}
|