Skip to content

Commit 2601910

Browse files
committed
feat: set up render pipeline
1 parent 11b800e commit 2601910

File tree

2 files changed

+106
-6
lines changed

2 files changed

+106
-6
lines changed

src/lib.rs

+84-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ use wasm_bindgen::prelude::*;
1414

1515

1616
struct State {
17-
surface : wgpu::Surface,
18-
device : wgpu::Device,
19-
queue : wgpu::Queue,
20-
config : wgpu::SurfaceConfiguration,
21-
size : winit::dpi::PhysicalSize<u32>,
22-
window : Window,
17+
surface : wgpu::Surface,
18+
device : wgpu::Device,
19+
queue : wgpu::Queue,
20+
config : wgpu::SurfaceConfiguration,
21+
size : winit::dpi::PhysicalSize<u32>,
22+
window : Window,
23+
render_pipeline : wgpu::RenderPipeline,
2324
}
2425

2526
// ============================================================
@@ -89,13 +90,90 @@ impl State {
8990

9091
surface.configure(&device, &config);
9192

93+
let shader = device.create_shader_module(
94+
wgpu::include_wgsl!("shader.wgsl"),
95+
);
96+
97+
let render_pipeline_layout = device.create_pipeline_layout(
98+
&wgpu::PipelineLayoutDescriptor {
99+
label : Some("Render pipeline layout"),
100+
bind_group_layouts : &[],
101+
push_constant_ranges: &[],
102+
},
103+
);
104+
105+
let render_pipeline = device.create_render_pipeline(
106+
&wgpu::RenderPipelineDescriptor {
107+
label : Some("Render pipeline"),
108+
layout: Some(&render_pipeline_layout),
109+
110+
vertex: wgpu::VertexState {
111+
module : &shader,
112+
entry_point : "vs_main",
113+
114+
// This tells `wgpu` what type of vertices to pass to the
115+
// vertex shader. Since we're specifying the vertices in the vertex
116+
// shader itself, empty is OK
117+
buffers : &[],
118+
},
119+
120+
// Stores color data to the `surface`
121+
fragment: Some(wgpu::FragmentState {
122+
module : &shader,
123+
entry_point : "fs_main",
124+
125+
// Tells `wgpu` what color outputs it should set up. Currently,
126+
// we only need one for the surface.
127+
targets: &[Some(wgpu::ColorTargetState {
128+
129+
// Uses the `surface` format so copying to it is easy
130+
format: config.format,
131+
132+
// Tells the blending to replace old pixel data with new data
133+
blend: Some(wgpu::BlendState::REPLACE),
134+
135+
// Tells `wgpu` to write to all colors: red, blue, green, alpha
136+
write_mask: wgpu::ColorWrites::ALL,
137+
})],
138+
}),
139+
140+
primitive: wgpu::PrimitiveState {
141+
142+
// Makes every three vertices correspond to one triangle
143+
topology : wgpu::PrimitiveTopology::TriangleList,
144+
strip_index_format: None,
145+
146+
// Defines the forward direction
147+
front_face: wgpu::FrontFace::Ccw,
148+
149+
// Culls triangles which are not facing forward
150+
cull_mode: Some(wgpu::Face::Back),
151+
152+
unclipped_depth : false,
153+
polygon_mode : wgpu::PolygonMode::Fill,
154+
conservative : false,
155+
},
156+
157+
depth_stencil : None,
158+
159+
multisample: wgpu::MultisampleState {
160+
count : 1,
161+
mask : !0,
162+
alpha_to_coverage_enabled: false,
163+
},
164+
165+
multiview: None,
166+
},
167+
);
168+
92169
Self {
93170
window,
94171
surface,
95172
device,
96173
queue,
97174
config,
98175
size,
176+
render_pipeline,
99177
}
100178
}
101179

src/shader.wgsl

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
// Vertex shader
3+
struct VertexOutput {
4+
@builtin(position) clip_position: vec4<f32>,
5+
};
6+
7+
@vertex
8+
fn vs_main(
9+
@builtin(vertex_index) in_vertex_index: u32,
10+
) -> VertexOutput {
11+
var output: VertexOutput;
12+
let x = f32(1 - i32(in_vertex_index)) * 0.5;
13+
let y = f32(i32(in_vertex_index & 1u) * 2 - 1) * 0.5;
14+
output.clip_position = vec4<f32>(x, y, 0.0, 1.0);
15+
return output;
16+
}
17+
18+
// Fragment shader
19+
@fragment
20+
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
21+
return vec4<f32>(0.3, 0.2, 0.1, 1.0);
22+
}

0 commit comments

Comments
 (0)