summaryrefslogtreecommitdiff
path: root/games
diff options
context:
space:
mode:
authorDominik Kaiser2026-11-27 18:16:00 +0100
committerDominik Kaiser2026-11-27 18:16:00 +0100
commit5a07760ea84580106e96ed2b4f9eb896486da7c1 (patch)
treec6e6767b901d00954d62309f607be8294adede18 /games
parente04c202435271f82fb93044e8972152f61d2677c (diff)
download20-games-challenge-5a07760ea84580106e96ed2b4f9eb896486da7c1.tar.gz
20-games-challenge-5a07760ea84580106e96ed2b4f9eb896486da7c1.zip
The Pipeline
Diffstat (limited to 'games')
-rw-r--r--games/00_triangle/Cargo.toml3
-rw-r--r--games/00_triangle/src/main.rs54
-rw-r--r--games/00_triangle/src/shader.wgsl19
3 files changed, 74 insertions, 2 deletions
diff --git a/games/00_triangle/Cargo.toml b/games/00_triangle/Cargo.toml
index 6e183e5..646903a 100644
--- a/games/00_triangle/Cargo.toml
+++ b/games/00_triangle/Cargo.toml
@@ -5,7 +5,8 @@ edition = "2024"
[dependencies]
anyhow = "1.0"
-winit = { version = "0.30", features = ["android-native-activity"] }
+# winit = { version = "0.30", features = ["android-native-activity"] }
+winit = { version = "0.30", default-features = false, features = ["wayland", "rwh_06"] }
env_logger = "0.10"
log = "0.4"
wgpu = "30.0"
diff --git a/games/00_triangle/src/main.rs b/games/00_triangle/src/main.rs
index fd4a166..e9de614 100644
--- a/games/00_triangle/src/main.rs
+++ b/games/00_triangle/src/main.rs
@@ -15,6 +15,7 @@ pub struct State {
config: wgpu::SurfaceConfiguration,
is_surface_configured: bool,
window: Arc<Window>,
+ render_pipeline: wgpu::RenderPipeline,
}
impl State {
@@ -70,12 +71,60 @@ impl State {
color_space: wgpu::SurfaceColorSpace::Auto,
};
+ 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: &[],
+ immediate_size: 0,
+ });
+
+ 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: Some("vs_main"),
+ buffers: &[],
+ compilation_options: wgpu::PipelineCompilationOptions::default(),
+ },
+ fragment: Some(wgpu::FragmentState {
+ module: &shader,
+ entry_point: Some("fs_main"),
+ targets: &[Some(wgpu::ColorTargetState {
+ format: config.format,
+ blend: Some(wgpu::BlendState::REPLACE),
+ write_mask: wgpu::ColorWrites::ALL,
+ })],
+ compilation_options: wgpu::PipelineCompilationOptions::default(),
+ }),
+ primitive: wgpu::PrimitiveState {
+ topology: wgpu::PrimitiveTopology::TriangleList,
+ strip_index_format: None,
+ front_face: wgpu::FrontFace::Ccw,
+ cull_mode: Some(wgpu::Face::Back),
+ polygon_mode: wgpu::PolygonMode::Fill,
+ unclipped_depth: false,
+ conservative: false,
+ },
+ depth_stencil: None,
+ multisample: wgpu::MultisampleState {
+ count: 1,
+ mask: !0,
+ alpha_to_coverage_enabled: false,
+ },
+ multiview_mask: None,
+ cache: None,
+ });
+
Ok(Self {
surface,
device,
queue,
config,
is_surface_configured: false,
+ render_pipeline,
window,
})
}
@@ -132,7 +181,7 @@ impl State {
});
{
- 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,
@@ -153,6 +202,9 @@ impl State {
timestamp_writes: None,
multiview_mask: None,
});
+
+ render_pass.set_pipeline(&self.render_pipeline);
+ render_pass.draw(0..3, 0..1);
}
self.queue.submit(std::iter::once(encoder.finish()));
diff --git a/games/00_triangle/src/shader.wgsl b/games/00_triangle/src/shader.wgsl
new file mode 100644
index 0000000..9ff0e3f
--- /dev/null
+++ b/games/00_triangle/src/shader.wgsl
@@ -0,0 +1,19 @@
+// 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);
+}