Repository navigation
Expand file tree
/
Copy pathtriangle.rs
More file actions
163 lines (133 loc) · 3.84 KB
/
Copy pathtriangle.rs
File metadata and controls
163 lines (133 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use miniquad::*;
#[repr(C)]
struct Vertex {
pos: [f32; 2],
color: [f32; 4],
}
struct Stage {
pipeline: Pipeline,
bindings: Bindings,
ctx: Box<dyn RenderingBackend>,
}
impl Stage {
pub fn new() -> Stage {
let mut ctx: Box<dyn RenderingBackend> = window::new_rendering_backend();
#[rustfmt::skip]
let vertices: [Vertex; 3] = [
Vertex { pos : [ -0.5, -0.5 ], color: [1., 0., 0., 1.] },
Vertex { pos : [ 0.5, -0.5 ], color: [0., 1., 0., 1.] },
Vertex { pos : [ 0.0, 0.5 ], color: [0., 0., 1., 1.] },
];
let vertex_buffer = ctx.new_buffer(
BufferType::VertexBuffer,
BufferUsage::Immutable,
BufferSource::slice(&vertices),
);
let indices: [u16; 3] = [0, 1, 2];
let index_buffer = ctx.new_buffer(
BufferType::IndexBuffer,
BufferUsage::Immutable,
BufferSource::slice(&indices),
);
let bindings = Bindings {
vertex_buffers: vec![vertex_buffer],
index_buffer: index_buffer,
images: vec![],
};
let shader = ctx
.new_shader(
match ctx.info().backend {
Backend::OpenGl => ShaderSource::Glsl {
vertex: shader::VERTEX,
fragment: shader::FRAGMENT,
},
Backend::Metal => ShaderSource::Msl {
program: shader::METAL,
},
},
shader::meta(),
)
.unwrap();
let pipeline = ctx.new_pipeline(
&[BufferLayout::default()],
&[
VertexAttribute::new("in_pos", VertexFormat::Float2),
VertexAttribute::new("in_color", VertexFormat::Float4),
],
shader,
PipelineParams::default(),
);
Stage {
pipeline,
bindings,
ctx,
}
}
}
impl EventHandler for Stage {
fn update(&mut self) {}
7E78
fn draw(&mut self) {
self.ctx.begin_default_pass(Default::default());
self.ctx.apply_pipeline(&self.pipeline);
self.ctx.apply_bindings(&self.bindings);
self.ctx.draw(0, 3, 1);
self.ctx.end_render_pass();
self.ctx.commit_frame();
}
}
fn main() {
let mut conf = conf::Conf::default();
let metal = std::env::args().nth(1).as_deref() == Some("metal");
conf.platform.apple_gfx_api = if metal {
conf::AppleGfxApi::Metal
} else {
conf::AppleGfxApi::OpenGl
};
miniquad::start(conf, move || Box::new(Stage::new()));
}
mod shader {
use miniquad::*;
pub const VERTEX: &str = r#"#version 100
attribute vec2 in_pos;
attribute vec4 in_color;
varying lowp vec4 color;
void main() {
gl_Position = vec4(in_pos, 0, 1);
color = in_color;
}"#;
pub const FRAGMENT: &str = r#"#version 100
varying lowp vec4 color;
void main() {
gl_FragColor = color;
}"#;
pub const METAL: &str = r#"
#include <metal_stdlib>
using namespace metal;
struct Vertex
{
float2 in_pos [[attribute(0)]];
float4 in_color [[attribute(1)]];
};
struct RasterizerData
{
float4 position [[position]];
float4 color [[user(locn0)]];
};
vertex RasterizerData vertexShader(Vertex v [[stage_in]])
{
RasterizerData out;
out.position = float4(v.in_pos.xy, 0.0, 1.0);
out.color = v.in_color;
return out;
}
fragment float4 fragmentShader(RasterizerData in [[stage_in]])
{
return in.color;
}"#;
pub fn meta() -> ShaderMeta {
ShaderMeta {
images: vec![],
uniforms: UniformBlockLayout { uniforms: vec![] },
}
}
}