First shader and rendering pipeline

This commit is contained in:
Verox001 2025-05-04 01:37:26 +02:00
parent 78d0f59cc1
commit c01d323f4c
2 changed files with 77 additions and 16 deletions

View File

@ -75,6 +75,8 @@ struct State<'a> {
size: PhysicalSize<u32>, size: PhysicalSize<u32>,
window: Arc<Window>, window: Arc<Window>,
render_pipeline: wgpu::RenderPipeline,
} }
impl<'a> State<'a> { impl<'a> State<'a> {
@ -89,6 +91,8 @@ impl<'a> State<'a> {
let config = Self::create_surface_config(size, surface_caps); let config = Self::create_surface_config(size, surface_caps);
surface.configure(&device, &config); surface.configure(&device, &config);
let render_pipeline = Self::create_render_pipeline(&device, &config);
Self { Self {
surface, surface,
device, device,
@ -96,6 +100,8 @@ impl<'a> State<'a> {
config, config,
size, size,
window: window_arc, window: window_arc,
render_pipeline,
} }
} }
@ -107,6 +113,61 @@ impl<'a> State<'a> {
// TODO: Update logic here // TODO: Update logic here
} }
fn create_render_pipeline(device: &Device, config: &wgpu::SurfaceConfiguration) -> wgpu::RenderPipeline {
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("Shader"),
source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()),
});
let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[],
push_constant_ranges: &[],
});
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, // 1.
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
cull_mode: Some(wgpu::Face::Back),
// Setting this to anything other than Fill requires Features::NON_FILL_POLYGON_MODE
polygon_mode: wgpu::PolygonMode::Fill,
// Requires Features::DEPTH_CLIP_CONTROL
unclipped_depth: false,
// Requires Features::CONSERVATIVE_RASTERIZATION
conservative: false,
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview: None,
cache: None,
})
}
fn create_surface_config(size: PhysicalSize<u32>, capabilities: SurfaceCapabilities) -> wgpu::SurfaceConfiguration { fn create_surface_config(size: PhysicalSize<u32>, capabilities: SurfaceCapabilities) -> wgpu::SurfaceConfiguration {
let surface_format = capabilities.formats.iter() let surface_format = capabilities.formats.iter()
.find(|f| f.is_srgb()) .find(|f| f.is_srgb())
@ -173,7 +234,7 @@ impl<'a> State<'a> {
}); });
{ {
let _render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { let mut _render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Render Pass"), label: Some("Render Pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment { color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &view, view: &view,
@ -192,6 +253,9 @@ impl<'a> State<'a> {
occlusion_query_set: None, occlusion_query_set: None,
timestamp_writes: None, timestamp_writes: None,
}); });
_render_pass.set_pipeline(&self.render_pipeline);
_render_pass.draw(0..3, 0..1);
} }
self.queue.submit(std::iter::once(encoder.finish())); self.queue.submit(std::iter::once(encoder.finish()));

View File

@ -1,22 +1,19 @@
struct VertexInput {
@location(0) position: vec2<f32>,
@location(1) color: vec3<f32>,
};
struct VertexOutput { struct VertexOutput {
@builtin(position) position: vec4<f32>, @builtin(position) clip_position: vec4<f32>,
@location(0) color: vec3<f32>,
}; };
@vertex @vertex
fn vs_main(input: VertexInput) -> VertexOutput { fn vs_main(
var output: VertexOutput; @builtin(vertex_index) in_vertex_index: u32,
output.position = vec4<f32>(input.position, 0.0, 1.0); ) -> VertexOutput {
output.color = input.color; var out: VertexOutput;
return output; 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 @fragment
fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> { fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
return vec4<f32>(input.color, 1.0); return vec4<f32>(0.3, 0.2, 0.1, 1.0);
} }