shader.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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. use miniquad::*;
  19. pub const GL_VERTEX: &str = r#"#version 100
  20. attribute vec2 in_pos;
  21. attribute vec4 in_color;
  22. attribute vec2 in_uv;
  23. varying lowp vec4 color;
  24. varying lowp vec2 uv;
  25. uniform mat4 Projection;
  26. uniform mat4 Model;
  27. void main() {
  28. gl_Position = Projection * Model * vec4(in_pos, 0, 1);
  29. color = in_color;
  30. uv = in_uv;
  31. }"#;
  32. pub const GL_FRAGMENT: &str = r#"#version 100
  33. varying lowp vec4 color;
  34. varying lowp vec2 uv;
  35. uniform sampler2D tex;
  36. void main() {
  37. gl_FragColor = color * texture2D(tex, uv);
  38. }"#;
  39. pub const METAL: &str = r#"
  40. #include <metal_stdlib>
  41. using namespace metal;
  42. struct Uniforms
  43. {
  44. float4x4 Projection;
  45. float4x4 Model;
  46. };
  47. struct Vertex
  48. {
  49. float2 in_pos [[attribute(0)]];
  50. float4 in_color [[attribute(1)]];
  51. float2 in_uv [[attribute(2)]];
  52. };
  53. struct RasterizerData
  54. {
  55. float4 position [[position]];
  56. float4 color [[user(locn0)]];
  57. float2 uv [[user(locn1)]];
  58. };
  59. vertex RasterizerData vertexShader(Vertex v [[stage_in]])
  60. {
  61. RasterizerData out;
  62. out.position = uniforms.Model * uniforms.Projection * float4(v.in_pos.xy, 0.0, 1.0);
  63. out.color = v.in_color;
  64. out.uv = v.texcoord;
  65. return out;
  66. }
  67. fragment float4 fragmentShader(RasterizerData in [[stage_in]], texture2d<float> tex [[texture(0)]], sampler texSmplr [[sampler(0)]])
  68. {
  69. return in.color * tex.sample(texSmplr, in.uv);
  70. }
  71. "#;
  72. pub fn meta() -> ShaderMeta {
  73. ShaderMeta {
  74. images: vec!["tex".to_string()],
  75. uniforms: UniformBlockLayout { uniforms: vec![] },
  76. }
  77. }