-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShaders.metal
238 lines (210 loc) · 7.36 KB
/
Shaders.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
//
// Shaders.metal
// MetalUnity
//
// Created by Tyler Payne on 6/20/16.
// Copyright © 2016 Tyler Payne. All rights reserved.
//
#include <metal_stdlib>
#include <metal_common>
#include <metal_geometric>
using namespace metal;
kernel void SobelGradient_Magnitude(texture2d<float, access::read> inTexture [[texture(0)]],
texture2d<float, access::read> x_weights [[texture(2)]],
texture2d<float, access::read> y_weights [[texture(3)]],
texture2d<float, access::write> outTexture [[texture(1)]],
uint2 gid [[thread_position_in_grid]])
{
float size = x_weights.get_width();
int radius = size / 2;
float4 xaccumColor(0,0,0,1);
float4 yaccumColor(0,0,0,1);
float4 outpix(0,0,0,1);
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
uint2 kernelIndex(i, j);
uint2 textureIndex(gid.x + (i - radius), gid.y + (j - radius));
float4 color = inTexture.read(textureIndex).rgba;
float xweight = x_weights.read(kernelIndex).r;
xaccumColor += xweight * color;
float yweight = y_weights.read(kernelIndex).r;
yaccumColor += yweight * color;
}
}
outpix = sqrt((xaccumColor*xaccumColor)+(yaccumColor*yaccumColor));
outTexture.write(outpix,gid);
}
kernel void SobelGradient_Sum(texture2d<float, access::read> inTexture [[texture(0)]],
texture2d<float, access::read> x_weights [[texture(2)]],
texture2d<float, access::read> y_weights [[texture(3)]],
texture2d<float, access::write> outTexture [[texture(1)]],
uint2 gid [[thread_position_in_grid]])
{
float size = x_weights.get_width();
int radius = size / 2;
float4 xaccumColor(0,0,0,1);
float4 yaccumColor(0,0,0,1);
float4 outpix(0,0,0,1);
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
uint2 kernelIndex(i, j);
uint2 textureIndex(gid.x + (i - radius), gid.y + (j - radius));
float4 color = inTexture.read(textureIndex).rgba;
float xweight = x_weights.read(kernelIndex).r;
xaccumColor += xweight * color;
float yweight = y_weights.read(kernelIndex).r;
yaccumColor += yweight * color;
}
}
outpix = xaccumColor + yaccumColor;
outTexture.write(outpix,gid);
}
kernel void Convolve(texture2d<float, access::read> inTexture [[texture(0)]],
texture2d<float, access::read> weights [[texture(2)]],
texture2d<float, access::write> outTexture [[texture(1)]],
uint2 gid [[thread_position_in_grid]])
{
float size = weights.get_width();
int radius = size / 2;
float4 accumColor(0,0,0,1);
float4 outpix(0,0,0,1);
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
uint2 kernelIndex(i, j);
uint2 textureIndex(gid.x + (i - radius), gid.y + (j - radius));
float4 color = inTexture.read(textureIndex).rgba;
float weight = weights.read(kernelIndex).r;
accumColor += weight * color;
}
}
outpix = accumColor;
outTexture.write(outpix,gid);
}
kernel void LocalMax_Constant(texture2d<float,access::read> inTexture [[texture(0)]],
texture2d<float,access::write> outTexture [[texture(1)]],
constant float* windowSize [[buffer(2)]] ,
uint2 gid [[thread_position_in_grid]])
{
float4 maxVal = float4(0,0,0,0);
uint2 maxIdx = uint2(0,0);
int size = int(windowSize[0]);
int radius = size/2;
outTexture.write(float4(0,0,0,1),gid);
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
uint2 textureIndex(gid.x + (i - radius), gid.y + (j - radius));
float4 color = inTexture.read(textureIndex).rgba;
if (length_squared(color) > length_squared(maxVal))
{
maxVal = color;
maxIdx = textureIndex;
}
}
}
outTexture.write(maxVal,maxIdx);
}
kernel void LocalMax(texture2d<float,access::read> inTexture [[texture(0)]],
texture2d<float,access::write> outTexture [[texture(1)]],
texture2d<float,access::read> windowSize [[texture(2)]] ,
uint2 gid [[thread_position_in_grid]])
{
float4 maxVal = float4(0,0,0,0);
uint2 maxIdx = uint2(0,0);
int size = int(windowSize.read(uint2(0,0)).r);
int radius = size/2;
outTexture.write(float4(0,0,0,1),gid);
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
uint2 textureIndex(gid.x + (i - radius), gid.y + (j - radius));
float4 color = inTexture.read(textureIndex).rgba;
if (length_squared(color) > length_squared(maxVal))
{
maxVal = color;
maxIdx = textureIndex;
}
}
}
outTexture.write(maxVal,maxIdx);
}
kernel void Add(texture2d<float, access::read> inTexture [[texture(0)]],
texture2d<float, access::read> weights [[texture(2)]],
texture2d<float, access::write> outTexture [[texture(1)]],
uint2 gid [[thread_position_in_grid]])
{
outTexture.write(float4(inTexture.read(gid).rgb+weights.read(gid).rgb,1),gid);
}
kernel void Subtract(texture2d<float, access::read> inTexture [[texture(0)]],
texture2d<float, access::read> weights [[texture(2)]],
texture2d<float, access::write> outTexture [[texture(1)]],
uint2 gid [[thread_position_in_grid]])
{
outTexture.write(float4(inTexture.read(gid).rgb-weights.read(gid).rgb,1),gid);
}
kernel void Multiply(texture2d<float, access::read> inTexture [[texture(0)]],
texture2d<float, access::read> weights [[texture(2)]],
texture2d<float, access::write> outTexture [[texture(1)]],
uint2 gid [[thread_position_in_grid]])
{
outTexture.write(float4(inTexture.read(gid).rgb*weights.read(gid).rgb,1),gid);
}
kernel void Multiply_Constant(texture2d<float, access::read> inTexture [[texture(0)]],
constant float *coefficient [[buffer(2)]],
texture2d<float, access::write> outTexture [[texture(1)]],
uint2 gid [[thread_position_in_grid]])
{
outTexture.write(float4(inTexture.read(gid).rgb*coefficient[0],1),gid);
}
kernel void Clip_Greater_Constant(texture2d<float, access::read> inTexture [[texture(0)]],
constant float *clipValue [[buffer(2)]],
texture2d<float, access::write> outTexture [[texture(1)]],
uint2 gid [[thread_position_in_grid]])
{
float4 outpix(0,0,0,0);
if (length_squared(inTexture.read(gid).rgb)<clipValue[0])
{
outpix = float4(inTexture.read(gid).rgb,1);
}
outTexture.write(float4(outpix.rgb,1),gid);
}
kernel void Clip_Less_Constant(texture2d<float, access::read> inTexture [[texture(0)]],
constant float *clipValue [[buffer(2)]],
texture2d<float, access::write> outTexture [[texture(1)]],
uint2 gid [[thread_position_in_grid]])
{
float4 outpix(0,0,0,0);
if (length_squared(inTexture.read(gid).rgb)>clipValue[0])
{
outpix = float4(inTexture.read(gid).rgb,1);
}
outTexture.write(float4(outpix.rgb,1),gid);
}
kernel void Clip_Less(texture2d<float, access::read> inTexture [[texture(0)]],
texture2d<float, access::read> clipValue [[texture(2)]],
texture2d<float, access::write> outTexture [[texture(1)]],
uint2 gid [[thread_position_in_grid]])
{
float4 outpix(0,0,0,0);
if (length_squared(inTexture.read(gid).rgb)>clipValue.read(uint2(0,0)).r)
{
outpix = float4(inTexture.read(gid).rgb,1);
}
outTexture.write(float4(outpix.rgb,1),gid);
}
kernel void Grayscale(texture2d<float, access::read> inTexture [[texture(0)]],
texture2d<float, access::write> outTexture [[texture(1)]],
uint2 gid [[thread_position_in_grid]])
{
float4 in_pixel(inTexture.read(gid).rgb,1);
float4 out_pixel(1,1,1,(in_pixel[0]*0.21) + (in_pixel[1]*0.72) + (in_pixel[2]*0.07));
outTexture.write(out_pixel,gid);
}