now we can render stuff
parent
0e6b636f8e
commit
71d579641e
|
@ -8,30 +8,60 @@ use winit::{
|
|||
};
|
||||
|
||||
#[repr(C)]
|
||||
struct Vertex([f32; 3]);
|
||||
struct Vertex {
|
||||
position: [f32; 3],
|
||||
tex_coord: [f32; 2],
|
||||
}
|
||||
|
||||
impl Vertex {
|
||||
const fn desc() -> wgpu::VertexBufferLayout<'static> {
|
||||
wgpu::VertexBufferLayout {
|
||||
array_stride: std::mem::size_of::<Self>() as wgpu::BufferAddress,
|
||||
step_mode: wgpu::VertexStepMode::Vertex,
|
||||
attributes: &[wgpu::VertexAttribute {
|
||||
attributes: &[
|
||||
wgpu::VertexAttribute {
|
||||
format: wgpu::VertexFormat::Float32x3,
|
||||
offset: 0,
|
||||
shader_location: 0,
|
||||
}],
|
||||
},
|
||||
wgpu::VertexAttribute {
|
||||
format: wgpu::VertexFormat::Float32x2,
|
||||
// size_of::<[f32; 3]> because the tex coords are right after the vertex
|
||||
// positions
|
||||
offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
|
||||
shader_location: 1,
|
||||
},
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const FULL_QUAD_VERTS: &[Vertex] = &[
|
||||
Vertex([-1.0, -1.0, 0.0]),
|
||||
Vertex([-1.0, 1.0, 0.0]),
|
||||
Vertex([1.0, 1.0, 0.0]),
|
||||
Vertex {
|
||||
position: [-1.0, -1.0, 0.0],
|
||||
tex_coord: [0.0, 1.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [-1.0, 1.0, 0.0],
|
||||
tex_coord: [0.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [1.0, 1.0, 0.0],
|
||||
tex_coord: [1.0, 0.0],
|
||||
},
|
||||
//
|
||||
Vertex([-1.0, -1.0, 0.0]),
|
||||
Vertex([1.0, -1.0, 0.0]),
|
||||
Vertex([1.0, 1.0, 0.0]),
|
||||
Vertex {
|
||||
position: [-1.0, -1.0, 0.0],
|
||||
tex_coord: [0.0, 1.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [1.0, -1.0, 0.0],
|
||||
tex_coord: [1.0, 1.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [1.0, 1.0, 0.0],
|
||||
tex_coord: [1.0, 0.0],
|
||||
},
|
||||
];
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
@ -82,9 +112,62 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
},
|
||||
usage: wgpu::BufferUsages::VERTEX,
|
||||
});
|
||||
let texture_size = wgpu::Extent3d {
|
||||
width: 2,
|
||||
height: 2,
|
||||
depth_or_array_layers: 1,
|
||||
};
|
||||
let texture = device.create_texture(&wgpu::TextureDescriptor {
|
||||
label: None,
|
||||
size: texture_size,
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: wgpu::TextureFormat::Rgba8UnormSrgb,
|
||||
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
|
||||
view_formats: &[],
|
||||
});
|
||||
let texture_bind_group_layout =
|
||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: None,
|
||||
entries: &[
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||
ty: wgpu::BindingType::Texture {
|
||||
sample_type: wgpu::TextureSampleType::Float { filterable: true },
|
||||
view_dimension: wgpu::TextureViewDimension::D2,
|
||||
multisampled: false,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 1,
|
||||
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
|
||||
count: None,
|
||||
},
|
||||
],
|
||||
});
|
||||
let texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
let texture_sampler = device.create_sampler(&wgpu::SamplerDescriptor::default());
|
||||
let texture_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: None,
|
||||
layout: &texture_bind_group_layout,
|
||||
entries: &[
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: wgpu::BindingResource::TextureView(&texture_view),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: wgpu::BindingResource::Sampler(&texture_sampler),
|
||||
},
|
||||
],
|
||||
});
|
||||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: None,
|
||||
bind_group_layouts: &[],
|
||||
bind_group_layouts: &[&texture_bind_group_layout],
|
||||
push_constant_ranges: &[],
|
||||
});
|
||||
let swapchain_capabilities = surface.get_capabilities(&adapter);
|
||||
|
@ -160,10 +243,29 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
occlusion_query_set: None,
|
||||
});
|
||||
rpass.set_pipeline(&render_pipeline);
|
||||
rpass.set_bind_group(0, &texture_bind_group, &[]);
|
||||
rpass.set_vertex_buffer(0, vertex_buffer.slice(..));
|
||||
rpass.draw(0..FULL_QUAD_VERTS.len() as u32, 0..1);
|
||||
}
|
||||
|
||||
queue.write_texture(
|
||||
wgpu::ImageCopyTexture {
|
||||
texture: &texture,
|
||||
mip_level: 0,
|
||||
origin: wgpu::Origin3d::ZERO,
|
||||
aspect: wgpu::TextureAspect::All,
|
||||
},
|
||||
&[
|
||||
255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255, 255, 0, 255, 255,
|
||||
],
|
||||
wgpu::ImageDataLayout {
|
||||
offset: 0,
|
||||
bytes_per_row: Some(4 * texture_size.width),
|
||||
rows_per_image: Some(texture_size.height),
|
||||
},
|
||||
texture_size,
|
||||
);
|
||||
|
||||
queue.submit(Some(encoder.finish()));
|
||||
frame.present();
|
||||
|
||||
|
|
|
@ -1,22 +1,28 @@
|
|||
struct VertexInput {
|
||||
@location(0) position: vec3<f32>,
|
||||
@location(1) tex_coord: vec2<f32>,
|
||||
}
|
||||
|
||||
struct VertexOutput {
|
||||
@builtin(position) clip_position: vec4<f32>,
|
||||
@location(0) color: vec4<f32>,
|
||||
@location(0) tex_coord: vec2<f32>,
|
||||
}
|
||||
|
||||
@vertex
|
||||
fn vs_main(quad: VertexInput) -> VertexOutput {
|
||||
var out: VertexOutput;
|
||||
out.color = vec4(1.0, 0.0, 0.0, 1.0);
|
||||
out.tex_coord = quad.tex_coord;
|
||||
out.clip_position = vec4<f32>(quad.position, 1.0);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
@group(0) @binding(0)
|
||||
var texture: texture_2d<f32>;
|
||||
@group(0) @binding(1)
|
||||
var texture_sampler: sampler;
|
||||
|
||||
@fragment
|
||||
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
||||
return in.color;
|
||||
return textureSample(texture, texture_sampler, in.tex_coord);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue