文件
dayu-color/大鱼调色/Plugin/DYGrade.metal
T

81 行
3.7 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)]]) {
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);
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));
}