|
| 1 | +#include <metal_stdlib> |
| 2 | +using namespace metal; |
| 3 | + |
| 4 | +typedef struct |
| 5 | +{ |
| 6 | + float4 position [[position]]; |
| 7 | + |
| 8 | + float2 textureCoordinate [[user(textureCoordinate)]]; |
| 9 | + float2 leftTextureCoordinate [[user(leftTextureCoordinate)]]; |
| 10 | + float2 rightTextureCoordinate [[user(rightTextureCoordinate)]]; |
| 11 | + float2 topTextureCoordinate [[user(topTextureCoordinate)]]; |
| 12 | + float2 bottomTextureCoordinate [[user(bottomTextureCoordinate)]]; |
| 13 | +} SharpenVertexIO; |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +vertex SharpenVertexIO sharpenVertex(const device packed_float2 *position [[buffer(0)]], |
| 18 | + const device packed_float2 *textureCoordinate [[buffer(1)]], |
| 19 | + uint vid [[vertex_id]]) |
| 20 | +{ |
| 21 | + SharpenVertexIO outputVertices; |
| 22 | + |
| 23 | + outputVertices.position = float4(position[vid], 0, 1.0); |
| 24 | + |
| 25 | + float2 widthStep = float2(1.0, 0.0); |
| 26 | + float2 heightStep = float2(0.0, 1.0); |
| 27 | + |
| 28 | + outputVertices.textureCoordinate = textureCoordinate[vid]; |
| 29 | + outputVertices.leftTextureCoordinate = textureCoordinate[vid] - widthStep; |
| 30 | + outputVertices.rightTextureCoordinate = textureCoordinate[vid] + widthStep; |
| 31 | + outputVertices.topTextureCoordinate = textureCoordinate[vid] + heightStep; |
| 32 | + outputVertices.bottomTextureCoordinate = textureCoordinate[vid] - heightStep; |
| 33 | + |
| 34 | + return outputVertices; |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +// Vertex Shader |
| 39 | +/* |
| 40 | + attribute vec4 position; |
| 41 | + attribute vec4 inputTextureCoordinate; |
| 42 | +
|
| 43 | + uniform float texelWidth; |
| 44 | + uniform float texelHeight; |
| 45 | + uniform float sharpness; |
| 46 | +
|
| 47 | + varying vec2 textureCoordinate; |
| 48 | + varying vec2 leftTextureCoordinate; |
| 49 | + varying vec2 rightTextureCoordinate; |
| 50 | + varying vec2 topTextureCoordinate; |
| 51 | + varying vec2 bottomTextureCoordinate; |
| 52 | +
|
| 53 | + varying float centerMultiplier; |
| 54 | + varying float edgeMultiplier; |
| 55 | +
|
| 56 | + void main() |
| 57 | + { |
| 58 | + gl_Position = position; |
| 59 | + |
| 60 | + vec2 widthStep = vec2(texelWidth, 0.0); |
| 61 | + vec2 heightStep = vec2(0.0, texelHeight); |
| 62 | + |
| 63 | + textureCoordinate = inputTextureCoordinate.xy; |
| 64 | + leftTextureCoordinate = inputTextureCoordinate.xy - widthStep; |
| 65 | + rightTextureCoordinate = inputTextureCoordinate.xy + widthStep; |
| 66 | + topTextureCoordinate = inputTextureCoordinate.xy + heightStep; |
| 67 | + bottomTextureCoordinate = inputTextureCoordinate.xy - heightStep; |
| 68 | + |
| 69 | + centerMultiplier = 1.0 + 4.0 * sharpness; |
| 70 | + edgeMultiplier = sharpness; |
| 71 | + } |
| 72 | +
|
| 73 | + */ |
| 74 | + |
| 75 | +typedef struct { |
| 76 | + float sharpness; |
| 77 | +} SharpenUniform; |
| 78 | + |
| 79 | +fragment half4 sharpenFragment(SharpenVertexIO fragmentInput [[stage_in]], |
| 80 | + texture2d<half> inputTexture [[texture(0)]], |
| 81 | + constant SharpenUniform& uniform [[buffer(1)]]) |
| 82 | +{ |
| 83 | + constexpr sampler quadSampler(coord::pixel); |
| 84 | + half3 centerColor = inputTexture.sample(quadSampler, fragmentInput.textureCoordinate).rgb; |
| 85 | + half3 leftColor = inputTexture.sample(quadSampler, fragmentInput.leftTextureCoordinate).rgb; |
| 86 | + half3 rightColor = inputTexture.sample(quadSampler, fragmentInput.rightTextureCoordinate).rgb; |
| 87 | + half3 topColor = inputTexture.sample(quadSampler, fragmentInput.topTextureCoordinate).rgb; |
| 88 | + half3 bottomColor = inputTexture.sample(quadSampler, fragmentInput.bottomTextureCoordinate).rgb; |
| 89 | + |
| 90 | + half edgeMultiplier = half(uniform.sharpness); |
| 91 | + half centerMultiplier = 1.0 + 4.0 * edgeMultiplier; |
| 92 | + |
| 93 | + return half4((centerColor * centerMultiplier |
| 94 | + - (leftColor * edgeMultiplier + rightColor * edgeMultiplier+ topColor * edgeMultiplier + bottomColor * edgeMultiplier)), |
| 95 | + inputTexture.sample(quadSampler, fragmentInput.bottomTextureCoordinate).w); |
| 96 | + |
| 97 | +} |
| 98 | +// Fragment Shader |
| 99 | +/* |
| 100 | + varying vec2 textureCoordinate; |
| 101 | + varying vec2 leftTextureCoordinate; |
| 102 | + varying vec2 rightTextureCoordinate; |
| 103 | + varying vec2 topTextureCoordinate; |
| 104 | + varying vec2 bottomTextureCoordinate; |
| 105 | +
|
| 106 | + varying float centerMultiplier; |
| 107 | + varying float edgeMultiplier; |
| 108 | +
|
| 109 | + uniform sampler2D inputImageTexture; |
| 110 | +
|
| 111 | + void main() |
| 112 | + { |
| 113 | + vec3 textureColor = texture2D(inputImageTexture, textureCoordinate).rgb; |
| 114 | + vec3 leftTextureColor = texture2D(inputImageTexture, leftTextureCoordinate).rgb; |
| 115 | + vec3 rightTextureColor = texture2D(inputImageTexture, rightTextureCoordinate).rgb; |
| 116 | + vec3 topTextureColor = texture2D(inputImageTexture, topTextureCoordinate).rgb; |
| 117 | + vec3 bottomTextureColor = texture2D(inputImageTexture, bottomTextureCoordinate).rgb; |
| 118 | + |
| 119 | + gl_FragColor = vec4((textureColor * centerMultiplier - (leftTextureColor * edgeMultiplier + rightTextureColor * edgeMultiplier + topTextureColor * edgeMultiplier + bottomTextureColor * edgeMultiplier)), texture2D(inputImageTexture, bottomTextureCoordinate).w); |
| 120 | + } |
| 121 | +
|
| 122 | + */ |
0 commit comments