Skip to content

Commit

Permalink
Implemented a pipeline and first shader
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasJowett committed Feb 23, 2024
1 parent 22da1cf commit 0397e22
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 8 deletions.
67 changes: 59 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct State {
queue: wgpu::Queue,
config: wgpu::SurfaceConfiguration,
size: winit::dpi::PhysicalSize<u32>,
render_pipeline: wgpu::RenderPipeline,
window: Window,
}

Expand Down Expand Up @@ -74,12 +75,57 @@ impl State {
};
surface.configure(&device, &config);

let shader = device.create_shader_module(wgpu::include_wgsl!("shader.wgsl"));

let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[],
push_constant_ranges: &[],
});

let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Render Pipeline"),
layout: Some(&render_pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
buffers: &[],
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: "fs_main",
targets: &[Some(wgpu::ColorTargetState {
format: config.format,
blend: Some(wgpu::BlendState::REPLACE),
write_mask: wgpu::ColorWrites::ALL,
})],
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
cull_mode: Some(wgpu::Face::Back),
polygon_mode: wgpu::PolygonMode::Fill,
unclipped_depth: false,
conservative: false,
front_face: wgpu::FrontFace::Ccw,
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview: None,
});

Self {
surface,
device,
queue,
config,
size,
render_pipeline,
window,
}
}
Expand Down Expand Up @@ -118,18 +164,20 @@ impl State {
label: Some("Render Encoder"),
});

let _render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Render Pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color {
r: 0.1,
g: 0.2,
b: 0.3,
a: 1.0,
}),
load: wgpu::LoadOp::Clear(
wgpu::Color {
r: 0.1,
g: 0.2,
b: 0.3,
a: 1.0,
}
),
store: wgpu::StoreOp::Store,
},
})],
Expand All @@ -138,7 +186,10 @@ impl State {
timestamp_writes: None,
});

drop(_render_pass);
render_pass.set_pipeline(&self.render_pipeline);
render_pass.draw(0..3, 0..1);

drop(render_pass);

self.queue.submit(std::iter::once(encoder.finish()));
output.present();
Expand Down
22 changes: 22 additions & 0 deletions src/shader.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Vertex shader

struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
};

@vertex
fn vs_main(
@builtin(vertex_index) in_vertex_index: u32,
) -> VertexOutput {
var out: VertexOutput;
let x = f32(1 - i32(in_vertex_index)) * 0.5;
let y = f32(i32(in_vertex_index & 1u) * 2 - 1) * 0.5;
out.clip_position = vec4<f32>(x, y, 0.0, 1.0);
return out;
}

// Fragment shader
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
return vec4<f32>(0.3, 0.2, 0.1, 1.0);
}

0 comments on commit 0397e22

Please sign in to comment.