// // Shaders.metal // MetalRenderer // // Created by Stuart Carnie on 5/31/18. // Copyright © 2018 Stuart Carnie. All rights reserved. // // File for Metal kernel and shader functions #include #include // Including header shared between this Metal shader code and Swift/C code executing Metal API commands #import "ShaderTypes.h" using namespace metal; #pragma mark - functions using projected coordinates vertex ColorInOut basic_vertex_proj_tex(const Vertex in [[ stage_in ]], const device Uniforms &uniforms [[ buffer(BufferIndexUniforms) ]]) { ColorInOut out; out.position = uniforms.projectionMatrix * float4(in.position, 1.0); out.texCoord = in.texCoord; return out; } fragment float4 basic_fragment_proj_tex(ColorInOut in [[stage_in]], constant Uniforms & uniforms [[ buffer(BufferIndexUniforms) ]], texture2d tex [[ texture(TextureIndexColor) ]], sampler samp [[ sampler(SamplerIndexDraw) ]]) { half4 colorSample = tex.sample(samp, in.texCoord.xy); return float4(colorSample); } #pragma mark - functions using normalized device coordinates vertex ColorInOut basic_vertex_ndc_tex(const Vertex in [[ stage_in ]]) { ColorInOut out; out.position = float4(in.position, 1.0); out.texCoord = in.texCoord; return out; } fragment float4 basic_fragment_ndc_tex(ColorInOut in [[stage_in]], texture2d tex [[ texture(TextureIndexColor) ]], sampler samp [[ sampler(SamplerIndexDraw) ]]) { half4 colorSample = tex.sample(samp, in.texCoord.xy); return float4(colorSample); } #pragma mark - functions for rendering fonts vertex FontFragmentIn font_vertex(const FontVertex in [[ stage_in ]], const device Uniforms &uniforms [[ buffer(BufferIndexUniforms) ]]) { FontFragmentIn out; out.position = uniforms.projectionMatrix * float4(in.position, 0, 1); out.texCoord = in.texCoord; out.color = in.color; return out; } fragment float4 font_fragment(FontFragmentIn in [[ stage_in ]], texture2d tex [[ texture(TextureIndexColor) ]], sampler samp [[ sampler(SamplerIndexDraw) ]]) { half4 colorSample = tex.sample(samp, in.texCoord.xy); return float4(in.color.rgb, in.color.a * colorSample.r); }