1 /*
2 * Copyright (c) 2024, 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 package shade.shaders;
26
27 import hat.Accelerator;
28 import hat.ComputeContext;
29
30 import hat.Accelerator.Compute;
31 import hat.ComputeContext.Kernel;
32 import hat.KernelContext;
33 import hat.NDRange;
34 import hat.backend.Backend;
35 import hat.buffer.F32Array;
36 import hat.types.vec2;
37 import hat.types.vec4;
38 import jdk.incubator.code.Reflect;
39 import optkl.ifacemapper.MappableIface;
40 import hat.buffer.Uniforms;
41 import shade.ShaderViewer;
42 import java.lang.invoke.MethodHandles;
43
44 import static hat.types.vec4.vec4;
45
46 //https://www.shadertoy.com/view/Md23DV
47 public class MouseSensitiveShader {
48 @Reflect
49 public static vec4 createPixel(vec2 fres, float ftime, vec2 fmouse, vec2 fragCoord){
50 float w = fres.x();
51 float wDiv3 = fres.x() / 3;
52 float h = fres.y();
53 float hDiv3 = fres.y() / 3;
54 boolean midx = (fragCoord.x() > wDiv3 && fragCoord.x() < (w - wDiv3));
55 boolean midy = (fragCoord.y() > hDiv3 && fragCoord.y() < (h - hDiv3));
56 if (fmouse.x() > wDiv3) {
57 if (midx && midy) {
58 return vec4(fragCoord.x(), .0f, fragCoord.y(), 0.f);
59 } else {
60 return vec4(0f, 0f, .5f, 0f);
61 }
62 } else {
63 return vec4(1f, 1f, .5f, 0f);
64 }
65 }
66
67 @Reflect public static vec4 mainImage(Uniforms uniforms, vec4 fragColor, vec2 fragCoord) {
68 return createPixel(vec2.vec2(uniforms.iResolution().x(),uniforms.iResolution().y()),uniforms.iTime(),vec2.vec2(uniforms.iMouse().x(),uniforms.iMouse().y()),fragCoord);
69
70 }
71
72
73 @Reflect
74 public static void penumbra(@MappableIface.RO KernelContext kc, @MappableIface.RO Uniforms uniforms, @MappableIface.RW F32Array f32Array) {
75 int width = (int) uniforms.iResolution().x();
76 int height = (int) uniforms.iResolution().y();
77 var fragColor = mainImage(uniforms, vec4.vec4(0f), vec2.vec2((float)(kc.gix % width), (float)(height-(kc.gix / width))));
78 f32Array.array(kc.gix * 3, fragColor.x());
79 f32Array.array(kc.gix * 3+1, fragColor.y());
80 f32Array.array(kc.gix * 3+2, fragColor.z());
81 }
82
83 @Reflect
84 static public void compute(final ComputeContext computeContext, @MappableIface.RO Uniforms uniforms, @MappableIface.RO F32Array image, int width, int height) {
85 computeContext.dispatchKernel(NDRange.of1D(width * height), (@Reflect Kernel) kc -> penumbra(kc, uniforms, image));
86 }
87
88 public static void update( Accelerator acc, Uniforms uniforms, F32Array f32Array, int width, int height) {
89 acc.compute((@Reflect Compute) cc -> compute(cc, uniforms, f32Array, width, height));
90 }
91
92 static void main(String[] args) {
93 var acc = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
94 var shader = ShaderViewer.of(acc, MouseSensitiveShader.class,1024, 1024);
95 shader.startLoop((uniforms, f32Array) -> update( acc, uniforms, f32Array, shader.view.getWidth(), shader.view.getHeight()));
96 }
97 }