-
Notifications
You must be signed in to change notification settings - Fork 1
/
default_renderer.metal
168 lines (137 loc) · 4.32 KB
/
default_renderer.metal
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <metal_stdlib>
#include "render_uniforms.h"
#include "render_vertex_ins.h"
using namespace metal;
constant int VertexBufferIndexUniformPerScene = 1;
constant int VertexBufferIndexUniformPerInstance = 2;
constant int FragmentBufferIndexUniformSDFont = 4;
constant int TextureIndexSDFont = 3;
struct VertexOut {
float4 position [[ position ]];
float2 uv;
};
vertex VertexOut vertex_position_uv(
const VertexInPositionUV vertex_in [[ stage_in ]],
constant UniformPerScene& per_scene [[ buffer( VertexBufferIndexUniformPerScene ) ]],
constant UniformPerInstance* per_instance [[ buffer( VertexBufferIndexUniformPerInstance ) ]],
const ushort instance_id [[ instance_id ]]
) {
const float4 position = per_scene.projection_matrix
* per_scene.view_matrix
* per_instance[instance_id].model_matrix
* vertex_in.position;
const float2 uv = vertex_in.uv;
VertexOut out {
.position = position,
.uv = uv
};
return out;
}
static inline float sdfont_step( const float v, const float pos )
{
if ( v < pos ) {
return 0.0;
}
else {
return 1.0;
}
}
static inline float sdfont_slope_step( const float v, const float width, const float pos )
{
if ( v < pos - width * 0.5 ) {
return 0.0;
}
else if (v < pos + width * 0.5 ) {
return (v - ( pos - width * 0.5 ) ) / width;
}
else {
return 1.0;
}
}
static inline float sdfont_trapezoid( const float v, const float width, const float pos1, const float pos2 )
{
if ( v < pos1 - width ) {
return 0.0;
}
else if (v < pos1 ) {
return (v - (pos1 - width))/width;
}
else if (v < pos2 ) {
return 1.0;
}
else if (v < pos2 + width ) {
return 1.0 - (v - pos2) /width;
}
else {
return 0.0;
}
}
static inline float sdfont_twin_peaks( const float v, const float width, const float pos1, const float pos2 )
{
const float hw = 0.5 * width;
if ( v < pos1 - hw ) {
return 0.0;
}
else if (v < pos1 ) {
return (v - (pos1 - hw)) / hw;
}
else if (v < pos1 + hw ) {
return 1.0 - (v - pos1) / hw;
}
if ( v < pos2 - 0.5 * hw ) {
return 0.0;
}
else if (v < pos2 ) {
return (v - (pos2 - hw)) / hw;
}
else if (v < pos2 + hw ) {
return 1.0 - (v - pos2) /hw;
}
else {
return 0.0;
}
}
static inline float sdfont_halo( const float v, const float width, const float pos1, const float pos2 )
{
if (v > pos2) {
return 0.0; // cutoff at pos2
}
else {
return sdfont_slope_step( v, width, pos1 );
}
}
fragment float4 fragment_sdfont(
VertexOut in [[ stage_in ]],
constant UniformSDFont& sd_font [[ buffer( FragmentBufferIndexUniformSDFont ) ]],
sampler texture_sampler [[ sampler( 0 ) ]],
texture2d<float> texture_sdfont [[ texture( TextureIndexSDFont ) ]]
) {
float4 base_color = sd_font.foreground_color;
float sampleDistance = texture_sdfont.sample(texture_sampler, in.uv).r;
const auto func_type = (SDFontFunctionType)sd_font.func_type;
float alpha = 0.0;
switch (func_type) {
case SDFONT_PASS_THROUGH:
alpha = sampleDistance;
break;
case SDFONT_STEP:
alpha = sdfont_step( sampleDistance, sd_font.point1 );
break;
case SDFONT_SMOOTH_STEP:
alpha = smoothstep( sd_font.point1, sd_font.point2, sampleDistance );
break;
case SDFONT_SLOPE_STEP:
alpha = sdfont_slope_step( sampleDistance, sd_font.width, sd_font.point1 );
break;
case SDFONT_TRAPEZOID:
alpha = sdfont_trapezoid( sampleDistance, sd_font.width, sd_font.point1, sd_font.point2 );
break;
case SDFONT_TWIN_PEAKS:
alpha = sdfont_twin_peaks( sampleDistance, sd_font.width, sd_font.point1, sd_font.point2 );
break;
case SDFONT_HALO:
alpha = sdfont_halo( sampleDistance, sd_font.width, sd_font.point1, sd_font.point2 );
break;
}
return float4(base_color.r, base_color.g, base_color.b, alpha * base_color.a );
}