src/java.desktop/macosx/native/libawt_lwawt/awt/shaders.metal
author jdv
Fri, 21 Jun 2019 12:08:37 +0530
branchmetal-prototype-branch
changeset 57416 e153174dba06
permissions -rw-r--r--
Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch Reviewed-by: avu, prr, kcr Contributed-by: avu(Jetbrains), aghaisas, jdv
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57416
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     1
/*
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     4
 *
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    10
 *
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    15
 * accompanied this code).
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    16
 *
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    20
 *
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    23
 * questions.
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    24
 */
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    25
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    26
#include <simd/simd.h>
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    27
#include <metal_stdlib>
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    28
#include "common.h"
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    29
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    30
using namespace metal;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    31
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    32
struct VertexInput {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    33
    float3 position [[attribute(VertexAttributePosition)]];
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    34
};
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    35
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    36
struct TxtVertexInput {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    37
    float3 position [[attribute(VertexAttributePosition)]];
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    38
    float2 texCoords [[attribute(VertexAttributeTexPos)]];
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    39
};
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    40
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    41
struct ColShaderInOut {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    42
    float4 position [[position]];
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    43
    half4  color;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    44
};
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    45
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    46
struct TxtShaderInOut {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    47
    float4 position [[position]];
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    48
    float2 texCoords;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    49
};
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    50
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    51
struct GradShaderInOut {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    52
    float4 position [[position]];
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    53
};
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    54
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    55
vertex ColShaderInOut vert_col(VertexInput in [[stage_in]],
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    56
	   constant FrameUniforms& uniforms [[buffer(FrameUniformBuffer)]],
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    57
       constant TransformMatrix& transform [[buffer(MatrixBuffer)]]) {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    58
    ColShaderInOut out;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    59
    float4 pos4 = float4(in.position, 1.0);
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    60
    out.position = transform.transformMatrix*pos4;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    61
    out.color = half4(uniforms.color.r, uniforms.color.g, uniforms.color.b, uniforms.color.a);
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    62
    return out;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    63
}
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    64
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    65
vertex GradShaderInOut vert_grad(VertexInput in [[stage_in]], constant TransformMatrix& transform [[buffer(MatrixBuffer)]]) {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    66
    GradShaderInOut out;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    67
    float4 pos4 = float4(in.position, 1.0);
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    68
    out.position = transform.transformMatrix*pos4;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    69
    return out;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    70
}
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    71
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    72
vertex TxtShaderInOut vert_txt(TxtVertexInput in [[stage_in]], constant TransformMatrix& transform [[buffer(MatrixBuffer)]]) {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    73
    TxtShaderInOut out;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    74
    float4 pos4 = float4(in.position, 1.0);
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    75
    out.position = transform.transformMatrix*pos4;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    76
    out.texCoords = in.texCoords;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    77
    return out;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    78
}
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    79
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    80
fragment half4 frag_col(ColShaderInOut in [[stage_in]]) {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    81
    return in.color;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    82
}
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    83
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    84
fragment half4 frag_txt(
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    85
        TxtShaderInOut vert [[stage_in]],
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    86
        texture2d<float, access::sample> renderTexture [[texture(0)]]
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    87
        )
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    88
{
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    89
    constexpr sampler textureSampler (mag_filter::linear,
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    90
                                  min_filter::linear);
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    91
    float4 pixelColor = renderTexture.sample(textureSampler, vert.texCoords);
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    92
    return half4(pixelColor.r, pixelColor.g, pixelColor.b , pixelColor.a);
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    93
}
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    94
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    95
fragment half4 frag_grad(GradShaderInOut in [[stage_in]],
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    96
                         constant GradFrameUniforms& uniforms [[buffer(0)]]) {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    97
    float3 v = float3(in.position.x, in.position.y, 1);
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    98
    float  a = (dot(v,uniforms.params)-0.25)*2.0;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    99
    float4 c = mix(uniforms.color1, uniforms.color2, a);
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   100
    return half4(c);
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   101
}