140 行
7.2 KiB
Metal
140 行
7.2 KiB
Metal
#include <metal_stdlib>
|
|
#include "DYUniforms.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);
|
|
|
|
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)]]) {
|
|
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));
|
|
}
|
|
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.
|
|
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;
|
|
float4 sample = source.read(pixel);
|
|
if (!(sample.a > 0.0f) || !all(isfinite(sample))) {
|
|
destination.write(half4(0.0h), pixel);
|
|
return;
|
|
}
|
|
destination.write(half4(half3(sample.rgb / sample.a), half(sample.a)), pixel);
|
|
}
|
|
|
|
// 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));
|
|
}
|
|
|
|
// Native auto-grade statistics consume the input-converted RGB8 preview.
|
|
// Quantization before and after the LUT mirrors the reference NSImage path.
|
|
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;
|
|
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));
|
|
}
|
|
|
|
// 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)]],uint2 pos [[thread_position_in_grid]]) {
|
|
if(pos.x>=source.get_width()||pos.y>=source.get_height())return;
|
|
float4 sample=source.read(pos);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);
|
|
}
|