diff --git a/wgpu/examples/hello-triangle/main.rs b/wgpu/examples/hello-triangle/main.rs index 58e7ba1a90..16a96129e9 100644 --- a/wgpu/examples/hello-triangle/main.rs +++ b/wgpu/examples/hello-triangle/main.rs @@ -4,6 +4,7 @@ use winit::{ event_loop::{ControlFlow, EventLoop}, window::Window, }; +use wgpu::util::DeviceExt; async fn run(event_loop: EventLoop<()>, window: Window) { let size = window.inner_size(); @@ -40,11 +41,29 @@ async fn run(event_loop: EventLoop<()>, window: Window) { source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("shader.wgsl"))), }); - let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - label: None, - bind_group_layouts: &[], - push_constant_ranges: &[], - }); + let bind_group_layout = + device + .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: None, + entries: &[wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::VERTEX, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: None, + }, + count: None, + }], + }); + + let pipeline_layout = + device + .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: None, + bind_group_layouts: &[&bind_group_layout], + push_constant_ranges: &[], + }); let swapchain_capabilities = surface.get_capabilities(&adapter); let swapchain_format = swapchain_capabilities.formats[0]; @@ -85,7 +104,7 @@ async fn run(event_loop: EventLoop<()>, window: Window) { // the resources are properly cleaned up. let _ = (&instance, &adapter, &shader, &pipeline_layout); - *control_flow = ControlFlow::Wait; + *control_flow = ControlFlow::Poll; match event { Event::WindowEvent { event: WindowEvent::Resized(size), @@ -98,6 +117,9 @@ async fn run(event_loop: EventLoop<()>, window: Window) { // On macos the window needs to be redrawn manually after resizing window.request_redraw(); } + Event::MainEventsCleared => { + window.request_redraw(); + } Event::RedrawRequested(_) => { let frame = surface .get_current_texture() @@ -105,6 +127,23 @@ async fn run(event_loop: EventLoop<()>, window: Window) { let view = frame .texture .create_view(&wgpu::TextureViewDescriptor::default()); + let color: [f32; 4] = [1.0, 0.5, 0.25, 1.0]; + let buffer = + device + .create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: None, + contents: bytemuck::cast_slice(&[color]), + usage: wgpu::BufferUsages::UNIFORM, + }); + let bind_group = device + .create_bind_group(&wgpu::BindGroupDescriptor { + layout: &bind_group_layout, + entries: &[wgpu::BindGroupEntry { + binding: 0, + resource: buffer.as_entire_binding(), + }], + label: None, + }); let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); { @@ -121,11 +160,12 @@ async fn run(event_loop: EventLoop<()>, window: Window) { depth_stencil_attachment: None, }); rpass.set_pipeline(&render_pipeline); + rpass.set_bind_group(0, &bind_group, &[]); rpass.draw(0..3, 0..1); } - queue.submit(Some(encoder.finish())); frame.present(); + log::info!("Presented"); } Event::WindowEvent { event: WindowEvent::CloseRequested, diff --git a/wgpu/examples/hello-triangle/shader.wgsl b/wgpu/examples/hello-triangle/shader.wgsl index f84ccfe94d..b9b17a2042 100644 --- a/wgpu/examples/hello-triangle/shader.wgsl +++ b/wgpu/examples/hello-triangle/shader.wgsl @@ -1,11 +1,20 @@ +@group(0) +@binding(0) +var color: vec4; + +struct VertexOutput { + @location(0) color: vec4, + @builtin(position) position: vec4, +}; + @vertex -fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> @builtin(position) vec4 { +fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> VertexOutput { let x = f32(i32(in_vertex_index) - 1); let y = f32(i32(in_vertex_index & 1u) * 2 - 1); - return vec4(x, y, 0.0, 1.0); + return VertexOutput(color, vec4(x, y, 0.0, 1.0)); } @fragment -fn fs_main() -> @location(0) vec4 { - return vec4(1.0, 0.0, 0.0, 1.0); +fn fs_main(vertex: VertexOutput) -> @location(0) vec4 { + return vertex.color; }