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     float3 position [[attribute(VertexAttributePosition)]];
  34 };
  35 
  36 struct TxtVertexInput {
  37     float3 position [[attribute(VertexAttributePosition)]];
  38     float2 texCoords [[attribute(VertexAttributeTexPos)]];
  39 };
  40 
  41 struct ColShaderInOut {
  42     float4 position [[position]];
  43     half4  color;
  44 };
  45 
  46 struct TxtShaderInOut {
  47     float4 position [[position]];
  48     float2 texCoords;
  49 };
  50 
  51 vertex ColShaderInOut vert_col(VertexInput in [[stage_in]],
  52            constant FrameUniforms& uniforms [[buffer(FrameUniformBuffer)]]) {
  53     ColShaderInOut out;
  54     out.position = float4(in.position, 1.0);
  55     out.color = half4(uniforms.color.r, uniforms.color.g, uniforms.color.b, uniforms.color.a);
  56     return out;
  57 }
  58 
  59 vertex TxtShaderInOut vert_txt(TxtVertexInput in [[stage_in]],
  60            constant FrameUniforms& uniforms [[buffer(FrameUniformBuffer)]]) {
  61     TxtShaderInOut out;
  62     out.position = float4(in.position, 1.0);
  63     out.texCoords = in.texCoords;
  64     return out;
  65 }
  66 
  67 fragment half4 frag_col(ColShaderInOut in [[stage_in]]) {
  68     return in.color;
  69 }
  70 
  71 fragment half4 frag_txt(
  72         TxtShaderInOut vert [[stage_in]],
  73         texture2d<float, access::sample> renderTexture [[texture(0)]]
  74         )
  75 {
  76     constexpr sampler textureSampler (mag_filter::linear,
  77                                   min_filter::linear);
  78     float4 pixelColor = renderTexture.sample(textureSampler, vert.texCoords);
  79     return half4(pixelColor.r, pixelColor.g, pixelColor.b , pixelColor.a);
  80 }