using System.Numerics;
namespace XFEEDToolBox.Core.Rendering;
///
/// Pinned 4.4.2.0 constructor defaults for the original final HDR pass.
/// These are an explicit offline fallback, not a snapshot of runtime exposure,
/// graphics options, bloom, colour grading, or per-view parameter overrides.
///
public static class GalaxyOriginalHdr
{
public const float WriteScale = 1f / 256;
public const float ReadScale = 256;
public const float ExposureEv = 0;
public const float Gamma = 2.2f;
public const int ToneMapPass = 5;
public const bool FullRestored = false;
// 351F8E0 overwrites the standard Hable defaults with these exact bits.
public static float ShoulderStrength => BitConverter.Int32BitsToSingle(0x3efb645a);
public static float LinearStrength => BitConverter.Int32BitsToSingle(0x3f34fdf4);
public static float LinearAngle => BitConverter.Int32BitsToSingle(0x3f343958);
public static float ToeStrength => BitConverter.Int32BitsToSingle(0x3f97ae14);
public static float ToeNumerator => BitConverter.Int32BitsToSingle(0x3fb04189);
public static float ToeDenominator => BitConverter.Int32BitsToSingle(0x3ffced91);
public static float LinearWhite => BitConverter.Int32BitsToSingle(0x40a7be77);
public static float Curve(float value)
{
var a=ShoulderStrength;var b=LinearStrength;var c=LinearAngle;
var d=ToeStrength;var e=ToeNumerator;var f=ToeDenominator;
return ((a*value+b*c)*value+d*e)/((a*value+b)*value+d*f)-e/f;
}
/// Original final PS math for the default U2 manual-exposure pass.
public static Vector3 ToneMap(Vector3 hdrStoredRgb)
{
var white=Curve(LinearWhite);
float Channel(float stored)
{
var linear=Math.Clamp(stored*ReadScale,0,64500*ReadScale);
// EV0, contrast1, saturation1, brightness1; colour adjustment is
// enabled by35225B0 and its mad_sat clamps after reciprocal gamma.
return Math.Clamp(MathF.Pow(Curve(linear)/white,1/Gamma),0,1);
}
return new(Channel(hdrStoredRgb.X),Channel(hdrStoredRgb.Y),Channel(hdrStoredRgb.Z));
}
///
/// Original CB2 layout, data starts at reflection byte80. Suitable for the
/// original U2 PS or the translated b7 pass. Unused fields stay zero.
///
public static Vector4[] Constants(int width,int height)
{
var values=new Vector4[11];
values[2]=new(ReadScale,.0001f,width,height);
values[3]=new(ShoulderStrength,LinearStrength,LinearAngle,ToeStrength);
values[4]=new(ToeNumerator,ToeDenominator,Curve(LinearWhite),0);
values[7]=new(1,0,1,1); // exp2(EV0), then unused auto-exposure fields.
values[9]=new(0,0,1/Gamma,0); // pivot*(1-contrast), reciprocal gamma.
values[10]=new(1,1,1,Gamma); // brightness,contrast,saturation,gamma.
return values;
}
}