1 /* 2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include <simd/simd.h> 27 #include <metal_stdlib> 28 #include "common.h" 29 30 using namespace metal; 31 32 struct VertexInput { 33 float2 position [[attribute(VertexAttributePosition)]]; 34 }; 35 36 struct TxtVertexInput { 37 float2 position [[attribute(VertexAttributePosition)]]; 38 float2 texCoords [[attribute(VertexAttributeTexPos)]]; 39 }; 40 41 struct ColShaderInOut { 42 float4 position [[position]]; 43 half4 color; 44 }; 45 46 struct StencilShaderInOut { 47 float4 position [[position]]; 48 char color; 49 }; 50 51 struct TxtShaderInOut { 52 float4 position [[position]]; 53 float2 texCoords; 54 float2 tpCoords; 55 }; 56 57 struct GradShaderInOut { 58 float4 position [[position]]; 59 float2 texCoords; 60 }; 61 62 vertex ColShaderInOut vert_col(VertexInput in [[stage_in]], 63 constant FrameUniforms& uniforms [[buffer(FrameUniformBuffer)]], 64 constant TransformMatrix& transform [[buffer(MatrixBuffer)]]) { 65 ColShaderInOut out; 66 float4 pos4 = float4(in.position, 0.0, 1.0); 67 out.position = transform.transformMatrix*pos4; 68 out.color = half4(uniforms.color.r, uniforms.color.g, uniforms.color.b, uniforms.color.a); 69 return out; 70 } 71 72 vertex StencilShaderInOut vert_stencil(VertexInput in [[stage_in]], 73 constant FrameUniforms& uniforms [[buffer(FrameUniformBuffer)]], 74 constant TransformMatrix& transform [[buffer(MatrixBuffer)]]) { 75 StencilShaderInOut out; 76 float4 pos4 = float4(in.position, 0.0, 1.0); 77 out.position = transform.transformMatrix * pos4; 78 out.color = 0xFF; 79 return out; 80 } 81 82 vertex GradShaderInOut vert_grad(VertexInput in [[stage_in]], constant TransformMatrix& transform [[buffer(MatrixBuffer)]]) { 83 GradShaderInOut out; 84 float4 pos4 = float4(in.position, 0.0, 1.0); 85 out.position = transform.transformMatrix*pos4; 86 return out; 87 } 88 89 vertex TxtShaderInOut vert_txt(TxtVertexInput in [[stage_in]], constant TransformMatrix& transform [[buffer(MatrixBuffer)]]) { 90 TxtShaderInOut out; 91 float4 pos4 = float4(in.position, 0.0, 1.0); 92 out.position = transform.transformMatrix*pos4; 93 out.texCoords = in.texCoords; 94 return out; 95 } 96 97 vertex TxtShaderInOut vert_txt_tp(TxtVertexInput in [[stage_in]], constant AnchorData& anchorData [[buffer(FrameUniformBuffer)]], constant TransformMatrix& transform [[buffer(MatrixBuffer)]]) 98 { 99 TxtShaderInOut out; 100 float4 pos4 = float4(in.position, 0.0, 1.0); 101 out.position = transform.transformMatrix * pos4; 102 103 // Compute texture coordinates here w.r.t. anchor rect of texture paint 104 out.tpCoords.x = (anchorData.xParams[0] * in.position.x) + 105 (anchorData.xParams[1] * in.position.y) + 106 (anchorData.xParams[2] * out.position.w); 107 out.tpCoords.y = (anchorData.yParams[0] * in.position.x) + 108 (anchorData.yParams[1] * in.position.y) + 109 (anchorData.yParams[2] * out.position.w); 110 out.texCoords = in.texCoords; 111 112 return out; 113 } 114 115 vertex GradShaderInOut vert_txt_grad(TxtVertexInput in [[stage_in]], 116 constant TransformMatrix& transform [[buffer(MatrixBuffer)]]) { 117 GradShaderInOut out; 118 float4 pos4 = float4(in.position, 0.0, 1.0); 119 out.position = transform.transformMatrix*pos4; 120 out.texCoords = in.texCoords; 121 return out; 122 } 123 124 fragment half4 frag_col(ColShaderInOut in [[stage_in]]) { 125 return in.color; 126 } 127 128 fragment unsigned int frag_stencil(StencilShaderInOut in [[stage_in]]) { 129 return in.color; 130 } 131 132 fragment half4 frag_txt( 133 TxtShaderInOut vert [[stage_in]], 134 texture2d<float, access::sample> renderTexture [[texture(0)]], 135 constant TxtFrameUniforms& uniforms [[buffer(1)]] 136 ) 137 { 138 constexpr sampler textureSampler (mag_filter::linear, 139 min_filter::linear); 140 float4 pixelColor = renderTexture.sample(textureSampler, vert.texCoords); 141 float srcA = uniforms.isSrcOpaque ? 1 : pixelColor.a; 142 // TODO: consider to make shaders without IF-conditions 143 if (uniforms.mode) { 144 float4 c = mix(pixelColor, uniforms.color, srcA); 145 return half4(c.r, c.g, c.b , c.a); 146 } 147 148 return half4(pixelColor.r, 149 pixelColor.g, 150 pixelColor.b, srcA*uniforms.extraAlpha); 151 } 152 153 fragment half4 frag_txt_tp(TxtShaderInOut vert [[stage_in]], 154 texture2d<float, access::sample> renderTexture [[texture(0)]], 155 texture2d<float, access::sample> paintTexture [[texture(1)]]) 156 { 157 constexpr sampler textureSampler (address::repeat, 158 mag_filter::nearest, 159 min_filter::nearest); 160 161 float4 renderColor = renderTexture.sample(textureSampler, vert.texCoords); 162 float4 paintColor = paintTexture.sample(textureSampler, vert.tpCoords); 163 return half4(paintColor.r*renderColor.a, 164 paintColor.g*renderColor.a, 165 paintColor.b*renderColor.a, 166 renderColor.a); 167 } 168 169 fragment half4 frag_txt_grad(GradShaderInOut in [[stage_in]], 170 constant GradFrameUniforms& uniforms [[buffer(0)]], 171 texture2d<float, access::sample> renderTexture [[texture(0)]]) 172 { 173 constexpr sampler textureSampler (address::repeat, mag_filter::nearest, 174 min_filter::nearest); 175 176 float4 renderColor = renderTexture.sample(textureSampler, in.texCoords); 177 178 float3 v = float3(in.position.x, in.position.y, 1); 179 float a = (dot(v,uniforms.params)-0.25)*2.0; 180 float4 c = mix(uniforms.color1, uniforms.color2, a); 181 return half4(c.r*renderColor.a, 182 c.g*renderColor.a, 183 c.b*renderColor.a, 184 renderColor.a); 185 } 186 187 fragment half4 aa_frag_txt( 188 TxtShaderInOut vert [[stage_in]], 189 texture2d<float, access::sample> renderTexture [[texture(0)]], 190 constant TxtFrameUniforms& uniforms [[buffer(1)]] 191 ) 192 { 193 constexpr sampler textureSampler (mag_filter::linear, min_filter::linear); 194 float4 pixelColor = renderTexture.sample(textureSampler, vert.texCoords); 195 return half4(pixelColor.r, pixelColor.g, pixelColor.b, pixelColor.a); 196 } 197 198 fragment half4 frag_grad(GradShaderInOut in [[stage_in]], 199 constant GradFrameUniforms& uniforms [[buffer(0)]]) { 200 float3 v = float3(in.position.x, in.position.y, 1); 201 float a = (dot(v,uniforms.params)-0.25)*2.0; 202 float4 c = mix(uniforms.color1, uniforms.color2, a); 203 return half4(c); 204 } 205 206 207 vertex TxtShaderInOut vert_tp(VertexInput in [[stage_in]], 208 constant AnchorData& anchorData [[buffer(FrameUniformBuffer)]], 209 constant TransformMatrix& transform [[buffer(MatrixBuffer)]]) 210 { 211 TxtShaderInOut out; 212 float4 pos4 = float4(in.position, 0.0, 1.0); 213 out.position = transform.transformMatrix * pos4; 214 215 // Compute texture coordinates here w.r.t. anchor rect of texture paint 216 out.texCoords.x = (anchorData.xParams[0] * in.position.x) + 217 (anchorData.xParams[1] * in.position.y) + 218 (anchorData.xParams[2] * out.position.w); 219 out.texCoords.y = (anchorData.yParams[0] * in.position.x) + 220 (anchorData.yParams[1] * in.position.y) + 221 (anchorData.yParams[2] * out.position.w); 222 223 return out; 224 } 225 226 fragment half4 frag_tp( 227 TxtShaderInOut vert [[stage_in]], 228 texture2d<float, access::sample> renderTexture [[texture(0)]]) 229 { 230 constexpr sampler textureSampler (address::repeat, 231 mag_filter::nearest, 232 min_filter::nearest); 233 234 float4 pixelColor = renderTexture.sample(textureSampler, vert.texCoords); 235 return half4(pixelColor.r, pixelColor.g, pixelColor.b, 1.0); 236 237 // This implementation defaults alpha to 1.0 as if source is opaque 238 //TODO : implement alpha component value if source is transparent 239 } 240 241 fragment half4 frag_tp_xorMode( 242 TxtShaderInOut vert [[stage_in]], 243 texture2d<float, access::sample> renderTexture [[texture(0)]], 244 constant int& xorColor[[buffer(0)]]) 245 { 246 constexpr sampler textureSampler (address::repeat, 247 mag_filter::nearest, 248 min_filter::nearest); 249 250 float4 pixelColor = renderTexture.sample(textureSampler, vert.texCoords); 251 252 pixelColor.r = float( (unsigned char)(pixelColor.r * 255.0) ^ ((xorColor >> 16) & 0xFF) ) / 255.0f; 253 pixelColor.g = float( (unsigned char)(pixelColor.g * 255.0) ^ ((xorColor >> 8) & 0xFF)) / 255.0f; 254 pixelColor.b = float( (unsigned char)(pixelColor.b * 255.0) ^ (xorColor & 0xFF)) / 255.0f; 255 pixelColor.a = 1.0; 256 257 return half4(pixelColor.r, pixelColor.g, pixelColor.b, 1.0); 258 259 // This implementation defaults alpha to 1.0 as if source is opaque 260 //TODO : implement alpha component value if source is transparent 261 } 262 263 /* The variables involved in the equation can be expressed as follows: 264 * 265 * Cs = Color component of the source (foreground color) [0.0, 1.0] 266 * Cd = Color component of the destination (background color) [0.0, 1.0] 267 * Cr = Color component to be written to the destination [0.0, 1.0] 268 * Ag = Glyph alpha (aka intensity or coverage) [0.0, 1.0] 269 * Ga = Gamma adjustment in the range [1.0, 2.5] 270 * (^ means raised to the power) 271 * 272 * And here is the theoretical equation approximated by this shader: 273 * 274 * Cr = (Ag*(Cs^Ga) + (1-Ag)*(Cd^Ga)) ^ (1/Ga) 275 */ 276 fragment float4 lcd_color( 277 TxtShaderInOut vert [[stage_in]], 278 texture2d<float, access::sample> glyphTexture [[texture(0)]], 279 texture2d<float, access::sample> dstTexture [[texture(1)]], 280 constant LCDFrameUniforms& uniforms [[buffer(1)]]) 281 { 282 float3 src_adj = uniforms.src_adj; 283 float3 gamma = uniforms.gamma; 284 float3 invgamma = uniforms.invgamma; 285 286 constexpr sampler glyphTextureSampler (mag_filter::linear, 287 min_filter::linear); 288 289 // load the RGB value from the glyph image at the current texcoord 290 float3 glyph_clr = float3(glyphTexture.sample(glyphTextureSampler, vert.texCoords)); 291 292 if (glyph_clr.r == 0.0f && glyph_clr.g == 0.0f && glyph_clr.b == 0.0f) { 293 // zero coverage, so skip this fragment 294 discard_fragment(); 295 } 296 constexpr sampler dstTextureSampler (mag_filter::linear, 297 min_filter::linear); 298 // load the RGB value from the corresponding destination pixel 299 float3 dst_clr = float3(dstTexture.sample(dstTextureSampler, vert.texCoords)); 300 301 // gamma adjust the dest color 302 float3 dst_adj = pow(dst_clr.rgb, gamma); 303 304 // linearly interpolate the three color values 305 float3 result = mix(dst_adj, src_adj, glyph_clr); 306 307 // gamma re-adjust the resulting color (alpha is always set to 1.0) 308 return float4(pow(result.rgb, invgamma), 1.0); 309 310 }