-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathMetal.metal
320 lines (244 loc) · 10.1 KB
/
Metal.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//
// Metal.metal
// ShaderMania
//
// Created by Markus Moenig on 25/8/20.
//
#include <metal_stdlib>
using namespace metal;
#import "Metal.h"
typedef struct
{
float4 clipSpacePosition [[position]];
float2 textureCoordinate;
} RasterizerData;
// Quad Vertex Function
vertex RasterizerData
m4mQuadVertexShader(uint vertexID [[ vertex_id ]],
constant VertexUniform *vertexArray [[ buffer(0) ]],
constant vector_uint2 *viewportSizePointer [[ buffer(1) ]])
{
RasterizerData out;
float2 pixelSpacePosition = vertexArray[vertexID].position.xy;
float2 viewportSize = float2(*viewportSizePointer);
out.clipSpacePosition.xy = pixelSpacePosition / (viewportSize / 2.0);
out.clipSpacePosition.z = 0.0;
out.clipSpacePosition.w = 1.0;
out.textureCoordinate = vertexArray[vertexID].textureCoordinate;
return out;
}
// --- SDF utilities
float m4mFillMask(float dist)
{
return clamp(-dist, 0.0, 1.0);
}
float m4mBorderMask(float dist, float width)
{
dist += 1.0;
return clamp(dist + width, 0.0, 1.0) - clamp(dist, 0.0, 1.0);
}
float2 m4mRotateCCW(float2 pos, float angle)
{
float ca = cos(angle), sa = sin(angle);
return pos * float2x2(ca, sa, -sa, ca);
}
float2 m4mRotateCCWPivot(float2 pos, float angle, float2 pivot)
{
float ca = cos(angle), sa = sin(angle);
return pivot + (pos-pivot) * float2x2(ca, sa, -sa, ca);
}
float2 m4mRotateCW(float2 pos, float angle)
{
float ca = cos(angle), sa = sin(angle);
return pos * float2x2(ca, -sa, sa, ca);
}
float2 m4mRotateCWPivot(float2 pos, float angle, float2 pivot)
{
float ca = cos(angle), sa = sin(angle);
return pivot + (pos-pivot) * float2x2(ca, -sa, sa, ca);
}
// Disc
fragment float4 m4mDiscDrawable(RasterizerData in [[stage_in]],
constant DiscUniform *data [[ buffer(0) ]],
texture2d<float> inTexture [[ texture(1) ]] )
{
float2 uv = in.textureCoordinate * float2( data->radius * 2 + data->borderSize, data->radius * 2 + data->borderSize);
uv -= float2( data->radius + data->borderSize / 2 );
float dist = length( uv ) - data->radius + data->onion;
if (data->onion > 0.0)
dist = abs(dist) - data->onion;
const float mask = m4mFillMask( dist );
float4 col = float4( data->fillColor.xyz, data->fillColor.w * mask);
float borderMask = m4mBorderMask(dist, data->borderSize);
float4 borderColor = data->borderColor;
borderColor.w *= borderMask;
col = mix( col, borderColor, borderMask );
if (data->hasTexture == 1 && col.w > 0.0) {
constexpr sampler textureSampler (mag_filter::linear,
min_filter::linear);
float2 uv = in.textureCoordinate;
uv.y = 1 - uv.y;
uv = m4mRotateCCWPivot(uv, data->rotation, 0.5);
float4 sample = float4(inTexture.sample(textureSampler, uv));
col.xyz = sample.xyz;
col.w = col.w * sample.w;
}
return col;
}
// Box
fragment float4 m4mBoxDrawable(RasterizerData in [[stage_in]],
constant BoxUniform *data [[ buffer(0) ]],
texture2d<float> inTexture [[ texture(1) ]] )
{
float2 uv = in.textureCoordinate * ( data->size );
uv -= float2( data->size / 2.0 );
float2 d = abs( uv ) - data->size / 2 + data->onion + data->round;
float dist = length(max(d,float2(0))) + min(max(d.x,d.y),0.0) - data->round;
if (data->onion > 0.0)
dist = abs(dist) - data->onion;
const float mask = m4mFillMask( dist );
float4 col = float4( data->fillColor.xyz, data->fillColor.w * mask);
float borderMask = m4mBorderMask(dist, data->borderSize);
float4 borderColor = data->borderColor;
borderColor.w *= borderMask;
col = mix( col, borderColor, borderMask );
if (data->hasTexture == 1 && col.w > 0.0) {
constexpr sampler textureSampler (mag_filter::linear,
min_filter::linear);
float2 uv = in.textureCoordinate;
uv.y = 1 - uv.y;
uv = m4mRotateCCWPivot(uv, data->rotation, 0.5);
float4 sample = float4(inTexture.sample(textureSampler, uv));
col.xyz = sample.xyz;
col.w = col.w * sample.w;
}
return col;
}
// Box
fragment float4 m4mLineDrawable(RasterizerData in [[stage_in]],
constant LineUniform *data [[ buffer(0) ]])
{
float2 uv = in.textureCoordinate * ( data->size + data->borderSize / 2.0);
uv -= float2(data->size / 2.0 + data->borderSize / 2.0);
float2 o = uv - data->sp;
float2 l = data->ep - data->sp;
float h = clamp( dot(o,l)/dot(l,l), 0.0, 1.0 );
float dist = -(data->width-distance(o,l*h));
float4 col = float4( data->fillColor.x, data->fillColor.y, data->fillColor.z, m4mFillMask( dist ) * data->fillColor.w );
col = mix( col, data->borderColor, m4mBorderMask( dist, data->borderSize ) );
return col;
}
// Rotated Box
fragment float4 m4mBoxDrawableExt(RasterizerData in [[stage_in]],
constant BoxUniform *data [[ buffer(0) ]],
texture2d<float> inTexture [[ texture(1) ]] )
{
float2 uv = in.textureCoordinate * data->screenSize;
uv.y = data->screenSize.y - uv.y;
uv -= float2(data->size / 2.0);
uv -= float2(data->pos.x, data->pos.y);
uv = m4mRotateCCW(uv, data->rotation);
float2 d = abs( uv ) - data->size / 2.0 + data->onion + data->round;// - data->borderSize;
float dist = length(max(d,float2(0))) + min(max(d.x,d.y),0.0) - data->round;
if (data->onion > 0.0)
dist = abs(dist) - data->onion;
const float mask = m4mFillMask( dist );//smoothstep(0.0, pixelSize, -dist);
float4 col = float4( data->fillColor.xyz, data->fillColor.w * mask);
const float borderMask = m4mBorderMask(dist, data->borderSize);
float4 borderColor = data->borderColor;
borderColor.w *= borderMask;
col = mix( col, borderColor, borderMask );
if (data->hasTexture == 1 && col.w > 0.0) {
constexpr sampler textureSampler (mag_filter::linear,
min_filter::linear);
float2 uv = in.textureCoordinate;
uv.y = 1 - uv.y;
uv -= data->pos / data->screenSize;
uv *= data->screenSize / data->size;
uv = m4mRotateCCWPivot(uv, data->rotation, (data->size / 2.0) / data->screenSize * (data->screenSize / data->size));
float4 sample = float4(inTexture.sample(textureSampler, uv));
col.xyz = sample.xyz;
col.w = col.w * sample.w;
}
return col;
}
// --- Box Drawable
fragment float4 m4mBoxPatternDrawable(RasterizerData in [[stage_in]],
constant BoxUniform *data [[ buffer(0) ]] )
{
float2 uv = in.textureCoordinate * ( data->screenSize );
uv -= float2( data->screenSize / 2.0 );
float2 d = abs( uv ) - data->size / 2.0;
float dist = length(max(d,float2(0))) + min(max(d.x,d.y),0.0);
float4 checkerColor1 = data->fillColor;
float4 checkerColor2 = data->borderColor;
//uv = fragCoord;
//uv -= float2( data->size / 2 );
float4 col = checkerColor1;
float cWidth = 12.0;
float cHeight = 12.0;
if ( fmod( floor( uv.x / cWidth ), 2.0 ) == 0.0 ) {
if ( fmod( floor( uv.y / cHeight ), 2.0 ) != 0.0 ) col=checkerColor2;
} else {
if ( fmod( floor( uv.y / cHeight ), 2.0 ) == 0.0 ) col=checkerColor2;
}
return float4( col.xyz, m4mFillMask( dist ) );
}
// Copy texture
fragment float4 m4mCopyTextureDrawable(RasterizerData in [[stage_in]],
constant TextureUniform *data [[ buffer(0) ]],
texture2d<half, access::read> inTexture [[ texture(1) ]])
{
float2 uv = in.textureCoordinate * data->size;
uv.y = data->size.y - uv.y;
const half4 colorSample = inTexture.read(uint2(uv));
float4 sample = float4( colorSample );
sample.w *= data->globalAlpha;
return float4(sample.x / sample.w, sample.y / sample.w, sample.z / sample.w, sample.w);
}
fragment float4 m4mTextureDrawable(RasterizerData in [[stage_in]],
constant TextureUniform *data [[ buffer(0) ]],
texture2d<half> inTexture [[ texture(1) ]])
{
//constexpr sampler textureSampler (mag_filter::linear,
// min_filter::linear);
constexpr sampler textureSampler (mag_filter::linear,
min_filter::linear);
float2 uv = in.textureCoordinate;
uv.y = 1 - uv.y;
uv.x *= data->size.x;
uv.y *= data->size.y;
uv.x += data->pos.x;
uv.y += data->pos.y;
float4 sample = float4(inTexture.sample(textureSampler, uv));
sample.w *= data->globalAlpha;
return sample;
}
float m4mMedian(float r, float g, float b) {
return max(min(r, g), min(max(r, g), b));
}
fragment float4 m4mTextDrawable(RasterizerData in [[stage_in]],
constant TextUniform *data [[ buffer(0) ]],
texture2d<float> inTexture [[ texture(1) ]])
{
constexpr sampler textureSampler (mag_filter::linear,
min_filter::linear);
float2 uv = in.textureCoordinate;
uv.y = 1 - uv.y;
uv /= data->atlasSize / data->fontSize;
uv += data->fontPos / data->atlasSize;
float4 sample = inTexture.sample(textureSampler, uv );
float d = m4mMedian(sample.r, sample.g, sample.b) - 0.5;
float w = clamp(d/fwidth(d) + 0.5, 0.0, 1.0);
return float4( data->color.x, data->color.y, data->color.z, w * data->color.w );
}
kernel void makeCGIImage(
texture2d<half, access::write> outTexture [[texture(0)]],
texture2d<half, access::read> inTexture [[texture(2)]],
uint2 gid [[thread_position_in_grid]])
{
//float2 size = float2( outTexture.get_width(), outTexture.get_height() );
half4 color = inTexture.read(gid).zyxw;
color.xyz = pow(color.xyz, 2.2);
outTexture.write(color, gid);
}