/ bin / app / src / gfx / shader.rs
shader.rs
 1  /* This file is part of DarkFi (https://dark.fi)
 2   *
 3   * Copyright (C) 2020-2025 Dyne.org foundation
 4   *
 5   * This program is free software: you can redistribute it and/or modify
 6   * it under the terms of the GNU Affero General Public License as
 7   * published by the Free Software Foundation, either version 3 of the
 8   * License, or (at your option) any later version.
 9   *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Affero General Public License for more details.
14   *
15   * You should have received a copy of the GNU Affero General Public License
16   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17   */
18  
19  use miniquad::*;
20  
21  pub const GL_VERTEX: &str = r#"#version 100
22  attribute vec2 in_pos;
23  attribute vec4 in_color;
24  attribute vec2 in_uv;
25  
26  varying lowp vec4 color;
27  varying lowp vec2 uv;
28  
29  uniform mat4 Projection;
30  uniform mat4 Model;
31  
32  void main() {
33      gl_Position = Projection * Model * vec4(in_pos, 0, 1);
34      color = in_color;
35      uv = in_uv;
36  }"#;
37  
38  pub const GL_FRAGMENT: &str = r#"#version 100
39  varying lowp vec4 color;
40  varying lowp vec2 uv;
41  
42  uniform sampler2D tex;
43  
44  void main() {
45      gl_FragColor = color * texture2D(tex, uv);
46  }"#;
47  
48  pub const METAL: &str = r#"
49  #include <metal_stdlib>
50  
51  using namespace metal;
52  
53  struct Uniforms
54  {
55      float4x4 Projection;
56      float4x4 Model;
57  };
58  
59  struct Vertex
60  {
61      float2 in_pos   [[attribute(0)]];
62      float4 in_color [[attribute(1)]];
63      float2 in_uv    [[attribute(2)]];
64  };
65  
66  struct RasterizerData
67  {
68      float4 position [[position]];
69      float4 color [[user(locn0)]];
70      float2 uv [[user(locn1)]];
71  };
72  
73  vertex RasterizerData vertexShader(Vertex v [[stage_in]])
74  {
75      RasterizerData out;
76  
77      out.position = uniforms.Model * uniforms.Projection * float4(v.in_pos.xy, 0.0, 1.0);
78      out.color = v.in_color;
79      out.uv = v.texcoord;
80  
81      return out
82  }
83  
84  fragment float4 fragmentShader(RasterizerData in [[stage_in]], texture2d<float> tex [[texture(0)]], sampler texSmplr [[sampler(0)]])
85  {
86      return in.color * tex.sample(texSmplr, in.uv)
87  }
88  
89  "#;
90  
91  pub fn meta() -> ShaderMeta {
92      ShaderMeta {
93          images: vec!["tex".to_string()],
94          uniforms: UniformBlockLayout { uniforms: vec![] },
95      }
96  }