summaryrefslogtreecommitdiff
path: root/Shaders/alpha_as_luma.hlsl
blob: 525f3c658a063deee5349d5091649fe8a98ac6cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// https://stackoverflow.com/a/37624009
float inverseGamma(float t)
{
    return ((t <= 0.0404482362771076) ? (t/12.92) : pow((t + 0.055)/1.055, 2.4));
}

// CIE L*a*b* f function (used to convert XYZ to L*a*b*)
// http://en.wikipedia.org/wiki/Lab_color_space
float LABF(float t)
{
    return ((t >= 8.85645167903563082e-3) ? pow(t, (1.0/3.0)) : (841.0/108.0)*t + (4.0/29.0));
}

float rgbToCIEL(float3 p)
{
    float r = inverseGamma(p.r);
    float g = inverseGamma(p.g);
    float b = inverseGamma(p.b);

    // Observer = 2°, Illuminant = D65
    float y = 0.2125862307855955516*r + 0.7151703037034108499*g + 0.07220049864333622685*b;

    // At this point we've done RGBtoXYZ now do XYZ to Lab

    // y /= WHITEPOINT_Y; The white point for y in D65 is 1.0
    y = LABF(y);

    // This is the "normal" conversion which produces values scaled to 100
    // Lab.L = 116.0*y - 16.0;
    return 1.16*y - 0.16;
}

// ---- Created with 3Dmigoto v1.4.9 on Sun Jun 28 13:42:41 2026

cbuffer CBSystem : register(b3)
{
    uint4 iRegion : packoffset(c0);
    float fSourceMipLevel : packoffset(c1) = {0};
    float fGammaCorrect : packoffset(c1.y) = {0.454545468};
    float fGammaCorrectEx : packoffset(c1.z) = {1};
}

SamplerState SSSystemCopy : register(s0);
Texture2D<float4> tBaseMap : register(t0);

void main(
    float4 v0 : SV_POSITION0,
    float2 v1 : TexCoord0,
    out float4 o0 : SV_TARGET0)
{
    float4 r0;
    r0.xyz = tBaseMap.SampleLevel(SSSystemCopy, v1.xy, fSourceMipLevel).xyz;
    o0.w = rgbToCIEL(r0.xyz);
    //o0.w = sqrt(dot(r0.xyz, float3(0.298999995, 0.587000012, 0.114)));
    //o0.w = dot(r0.xyz, float3(0.298999995, 0.587000012, 0.114));
    o0.xyz = r0.xyz;
    return;
}