Backlog/engine/ui/shaders/text.frag.hlsl

92 lines
2.7 KiB
HLSL

#include "text_shared.hlsli"
#include "fragmentHelpers.hlsli"
Texture2D<float4> Texture0 : register(t0, space2);
SamplerState Sampler0 : register(s0, space2);
StructuredBuffer<FontInfo> fontBuffer: register(t1, space2);
float4 main(
uint Color : TEXCOORD0,
float2 UV : TEXCOORD1,
float2 pixelPosition: TEXCOORD2,
uint Instance: TEXCOORD3
) :SV_Target0
{
float4 o;
// vec4 tex = texture(tex, texCoord);
float4 tex = Texture0.Sample(Sampler0, UV);
uint isSdf = fontBuffer[Instance].isSdf;
float2 position = fontBuffer[Instance].position;
float2 size = fontBuffer[Instance].size;
if(!scissor(pixelPosition, position, size))
//if(pixelPosition.x > (position.x + size.x))
{
// return float4(pixelPosition.x / 1920, 0.0, 0.0, 1.0);
discard;
}
float4 textColor = float4(
getRedFromUint(Color),
getGreenFromUint(Color),
getBlueFromUint(Color),
getAlphaFromUint(Color));
if(isSdf == 1)
{
float dist = tex.r;
float width = fwidth(dist);
float outerEdge = 1.0f - (120.0f / 255.0f);
float alpha = contour(dist, outerEdge, width);
float dscale = 0.354; // half of 1/sqrt2; you can play with this
float2 uv = UV.xy;
float2 duv = dscale * (ddx(uv) + ddy(uv));
float4 box = float4(uv - duv, uv + duv);
float asum = getSample(Texture0, Sampler0, box.xy, outerEdge, width)
+ getSample(Texture0, Sampler0, box.zw, outerEdge, width)
+ getSample(Texture0, Sampler0, box.xw, outerEdge, width)
+ getSample(Texture0, Sampler0, box.zy, outerEdge, width);
// weighted average, with 4 extra points having 0.5 weight each,
// so 1 + 0.5*4 = 3 is the divisor
alpha = (alpha + 0.5 * asum) / 3.0;
textColor = float4(textColor.xyz, alpha * textColor.w);//textColor.* alpha);
textColor.xyz = pow(textColor.xyz, 2.2); // gamma correction
// Premultiplied alpha output.
o = textColor;
}
else
{
float alpha = 1.0;
float gray = dot(textColor.xyz, float3(0.2126, 0.7152, 0.0722));
o = float4(textColor.xyz , pow(tex.x / gray, 1/(2.2)) * textColor.w );//textColor.* alpha);
//outFragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
#if 0
float4 outFragColor = 0;
if(!rect(pixelPosition, position, size))
{
}
outFragColor = float4(1.0, 0.0, 0.0, textColor.w);
return outFragColor + o * 0.00001;
#else
//float alpha = 1.0;
// float gray = dot(textColor.xyz, float3(0.2126, 0.7152, 0.0722));
// o = float4(textColor.xyz, tex.x);//pow(tex.x / gray, 1/(2.2)) );//textColor.* alpha);
return o; // float4(1.0, 1.0, 1.0, tex.x) + o * 0.00001;
#endif
}