|
| 1 | +#include <metal_stdlib> |
| 2 | +#include "TexelSamplingTypes.h" |
| 3 | + |
| 4 | +using namespace metal; |
| 5 | + |
| 6 | +#define s2(a, b) temp = a; a = min(a, b); b = max(temp, b); |
| 7 | +#define mn3(a, b, c) s2(a, b); s2(a, c); |
| 8 | +#define mx3(a, b, c) s2(b, c); s2(a, c); |
| 9 | + |
| 10 | +#define mnmx3(a, b, c) mx3(a, b, c); s2(a, b); // 3 exchanges |
| 11 | +#define mnmx4(a, b, c, d) s2(a, b); s2(c, d); s2(a, c); s2(b, d); // 4 exchanges |
| 12 | +#define mnmx5(a, b, c, d, e) s2(a, b); s2(c, d); mn3(a, c, e); mx3(b, d, e); // 6 exchanges |
| 13 | +#define mnmx6(a, b, c, d, e, f) s2(a, d); s2(b, e); s2(c, f); mn3(a, b, c); mx3(d, e, f); // 7 exchanges |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +fragment half4 medianFilter(NearbyTexelVertexIO fragmentInput [[stage_in]], |
| 18 | + texture2d<half> inputTexture [[texture(0)]]) { |
| 19 | + |
| 20 | +constexpr sampler quadSampler(coord::pixel); |
| 21 | + |
| 22 | + half3 v[6]; |
| 23 | + |
| 24 | + v[0] = inputTexture.sample(quadSampler, fragmentInput.bottomLeftTextureCoordinate).rgb; |
| 25 | + v[1] = inputTexture.sample(quadSampler, fragmentInput.topRightTextureCoordinate).rgb; |
| 26 | + v[2] = inputTexture.sample(quadSampler, fragmentInput.topLeftTextureCoordinate).rgb; |
| 27 | + v[3] = inputTexture.sample(quadSampler, fragmentInput.bottomRightTextureCoordinate).rgb; |
| 28 | + v[4] = inputTexture.sample(quadSampler, fragmentInput.leftTextureCoordinate).rgb; |
| 29 | + v[5] = inputTexture.sample(quadSampler, fragmentInput.rightTextureCoordinate).rgb; |
| 30 | + |
| 31 | + |
| 32 | + half3 temp; |
| 33 | + |
| 34 | + mnmx6(v[0], v[1], v[2], v[3], v[4], v[5]); |
| 35 | + |
| 36 | + v[5] = inputTexture.sample(quadSampler, fragmentInput.bottomTextureCoordinate).rgb; |
| 37 | + |
| 38 | + mnmx5(v[1], v[2], v[3], v[4], v[5]); |
| 39 | + |
| 40 | + v[5] = inputTexture.sample(quadSampler, fragmentInput.topTextureCoordinate).rgb; |
| 41 | + |
| 42 | + mnmx4(v[2], v[3], v[4], v[5]); |
| 43 | + |
| 44 | + v[5] = inputTexture.sample(quadSampler, fragmentInput.textureCoordinate).rgb; |
| 45 | + |
| 46 | + mnmx3(v[3], v[4], v[5]); |
| 47 | + |
| 48 | + return half4(v[4], 1.0); |
| 49 | + |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +/* |
| 54 | +
|
| 55 | +varying vec2 textureCoordinate; |
| 56 | + varying vec2 leftTextureCoordinate; |
| 57 | + varying vec2 rightTextureCoordinate; |
| 58 | + |
| 59 | + varying vec2 topTextureCoordinate; |
| 60 | + varying vec2 topLeftTextureCoordinate; |
| 61 | + varying vec2 topRightTextureCoordinate; |
| 62 | + |
| 63 | + varying vec2 bottomTextureCoordinate; |
| 64 | + varying vec2 bottomLeftTextureCoordinate; |
| 65 | + varying vec2 bottomRightTextureCoordinate; |
| 66 | + |
| 67 | + uniform sampler2D inputImageTexture; |
| 68 | + |
| 69 | +#define s2(a, b) temp = a; a = min(a, b); b = max(temp, b); |
| 70 | +#define mn3(a, b, c) s2(a, b); s2(a, c); |
| 71 | +#define mx3(a, b, c) s2(b, c); s2(a, c); |
| 72 | + |
| 73 | +#define mnmx3(a, b, c) mx3(a, b, c); s2(a, b); // 3 exchanges |
| 74 | +#define mnmx4(a, b, c, d) s2(a, b); s2(c, d); s2(a, c); s2(b, d); // 4 exchanges |
| 75 | +#define mnmx5(a, b, c, d, e) s2(a, b); s2(c, d); mn3(a, c, e); mx3(b, d, e); // 6 exchanges |
| 76 | +#define mnmx6(a, b, c, d, e, f) s2(a, d); s2(b, e); s2(c, f); mn3(a, b, c); mx3(d, e, f); // 7 exchanges |
| 77 | + |
| 78 | + void main() |
| 79 | + { |
| 80 | + vec3 v[6]; |
| 81 | + |
| 82 | + v[0] = texture2D(inputImageTexture, bottomLeftTextureCoordinate).rgb; |
| 83 | + v[1] = texture2D(inputImageTexture, topRightTextureCoordinate).rgb; |
| 84 | + v[2] = texture2D(inputImageTexture, topLeftTextureCoordinate).rgb; |
| 85 | + v[3] = texture2D(inputImageTexture, bottomRightTextureCoordinate).rgb; |
| 86 | + v[4] = texture2D(inputImageTexture, leftTextureCoordinate).rgb; |
| 87 | + v[5] = texture2D(inputImageTexture, rightTextureCoordinate).rgb; |
| 88 | + // v[6] = texture2D(inputImageTexture, bottomTextureCoordinate).rgb; |
| 89 | + // v[7] = texture2D(inputImageTexture, topTextureCoordinate).rgb; |
| 90 | + vec3 temp; |
| 91 | + |
| 92 | + mnmx6(v[0], v[1], v[2], v[3], v[4], v[5]); |
| 93 | + |
| 94 | + v[5] = texture2D(inputImageTexture, bottomTextureCoordinate).rgb; |
| 95 | + |
| 96 | + mnmx5(v[1], v[2], v[3], v[4], v[5]); |
| 97 | + |
| 98 | + v[5] = texture2D(inputImageTexture, topTextureCoordinate).rgb; |
| 99 | + |
| 100 | + mnmx4(v[2], v[3], v[4], v[5]); |
| 101 | + |
| 102 | + v[5] = texture2D(inputImageTexture, textureCoordinate).rgb; |
| 103 | + |
| 104 | + mnmx3(v[3], v[4], v[5]); |
| 105 | + |
| 106 | + gl_FragColor = vec4(v[4], 1.0); |
| 107 | + } |
| 108 | +
|
| 109 | +*/ |
0 commit comments