aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/scenegraph/d3d12/shaders/textmask.hlsl
blob: f9d92e8ee9f92ad244b223ffd015b47b8a5280e2 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
struct VSInput
{
    float4 position : POSITION;
    float2 coord : TEXCOORD0;
};

cbuffer ConstantBuffer : register(b0)
{
    float4x4 mvp;
    float2 textureScale;
    float dpr;
    float color; // for TextMask24 and 32
    float4 colorVec; // for TextMask8 and Styled and Outlined
    float2 shift; // for Styled
    float4 styleColor; // for Styled and Outlined
};

struct PSInput
{
    float4 position : SV_POSITION;
    float2 coord : TEXCOORD0;
};

Texture2D tex : register(t0);
SamplerState samp : register(s0);

PSInput VS_TextMask(VSInput input)
{
    PSInput result;
    result.position = mul(mvp, floor(input.position * dpr + 0.5) / dpr);
    result.coord = input.coord * textureScale;
    return result;
}

float4 PS_TextMask24(PSInput input) : SV_TARGET
{
    float4 glyph = tex.Sample(samp, input.coord);
    return float4(glyph.rgb * color, glyph.a);
}

float4 PS_TextMask32(PSInput input) : SV_TARGET
{
    return tex.Sample(samp, input.coord) * color;
}

float4 PS_TextMask8(PSInput input) : SV_TARGET
{
    return colorVec * tex.Sample(samp, input.coord).r;
}

struct StyledPSInput
{
    float4 position : SV_POSITION;
    float2 coord : TEXCOORD0;
    float2 shiftedCoord : TEXCOORD1;
};

StyledPSInput VS_StyledText(VSInput input)
{
    StyledPSInput result;
    result.position = mul(mvp, floor(input.position * dpr + 0.5) / dpr);
    result.coord = input.coord * textureScale;
    result.shiftedCoord = (input.coord - shift) * textureScale;
    return result;
}

float4 PS_StyledText(StyledPSInput input) : SV_TARGET
{
    float glyph = tex.Sample(samp, input.coord).r;
    float style = clamp(tex.Sample(samp, input.shiftedCoord).r - glyph, 0.0, 1.0);
    return style * styleColor + glyph * colorVec;
}

struct OutlinedPSInput
{
    float4 position : SV_POSITION;
    float2 coord : TEXCOORD0;
    float2 coordUp : TEXCOORD1;
    float2 coordDown : TEXCOORD2;
    float2 coordLeft : TEXCOORD3;
    float2 coordRight : TEXCOORD4;
};

OutlinedPSInput VS_OutlinedText(VSInput input)
{
    OutlinedPSInput result;
    result.position = mul(mvp, floor(input.position * dpr + 0.5) / dpr);
    result.coord = input.coord * textureScale;
    result.coordUp = (input.coord - float2(0.0, -1.0)) * textureScale;
    result.coordDown = (input.coord - float2(0.0, 1.0)) * textureScale;
    result.coordLeft = (input.coord - float2(-1.0, 0.0)) * textureScale;
    result.coordRight = (input.coord - float2(1.0, 0.0)) * textureScale;
    return result;
}

float4 PS_OutlinedText(OutlinedPSInput input) : SV_TARGET
{
    float glyph = tex.Sample(samp, input.coord).r;
    float outline = clamp(clamp(tex.Sample(samp, input.coordUp).r
        + tex.Sample(samp, input.coordDown).r
        + tex.Sample(samp, input.coordLeft).r
        + tex.Sample(samp, input.coordRight).r, 0.0, 1.0) - glyph, 0.0, 1.0);
    return outline * styleColor + glyph * colorVec;
}