Skip to content

Commit

Permalink
Broke the hello-triangle demo on intel graphics
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinnerbone committed Jan 11, 2023
1 parent 630c12f commit dfcc1ed
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
54 changes: 47 additions & 7 deletions wgpu/examples/hello-triangle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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),
Expand All @@ -98,13 +117,33 @@ 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()
.expect("Failed to acquire next swap chain texture");
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 });
{
Expand All @@ -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,
Expand Down
17 changes: 13 additions & 4 deletions wgpu/examples/hello-triangle/shader.wgsl
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
@group(0)
@binding(0)
var<uniform> color: vec4<f32>;

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

@vertex
fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> @builtin(position) vec4<f32> {
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<f32>(x, y, 0.0, 1.0);
return VertexOutput(color, vec4<f32>(x, y, 0.0, 1.0));
}

@fragment
fn fs_main() -> @location(0) vec4<f32> {
return vec4<f32>(1.0, 0.0, 0.0, 1.0);
fn fs_main(vertex: VertexOutput) -> @location(0) vec4<f32> {
return vertex.color;
}

0 comments on commit dfcc1ed

Please sign in to comment.