Compute vertex position for image shader

This commit is contained in:
Jim Eckerlein 2023-09-24 15:24:08 +02:00
parent e197abe0aa
commit 5fb877ab59
2 changed files with 23 additions and 80 deletions

View file

@ -35,8 +35,6 @@ pub struct Pipeline {
vector_cache: RefCell<vector::Cache>,
pipeline: wgpu::RenderPipeline,
vertices: wgpu::Buffer,
indices: wgpu::Buffer,
sampler: wgpu::Sampler,
texture: wgpu::BindGroup,
texture_version: usize,
@ -128,20 +126,14 @@ impl Layer {
fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) {
render_pass.set_bind_group(0, &self.constants, &[]);
render_pass.set_vertex_buffer(1, self.instances.slice(..));
render_pass.set_vertex_buffer(0, self.instances.slice(..));
render_pass.draw_indexed(
0..QUAD_INDICES.len() as u32,
0,
0..self.instance_count as u32,
);
render_pass.draw(0..6, 0..self.instance_count as u32);
}
}
impl Pipeline {
pub fn new(device: &wgpu::Device, format: wgpu::TextureFormat) -> Self {
use wgpu::util::DeviceExt;
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
@ -207,7 +199,11 @@ impl Pipeline {
device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("iced_wgpu image shader"),
source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed(
include_str!("shader/image.wgsl"),
concat!(
include_str!("shader/vertex.wgsl"),
"\n",
include_str!("shader/image.wgsl"),
),
)),
});
@ -218,28 +214,17 @@ impl Pipeline {
vertex: wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
buffers: &[
wgpu::VertexBufferLayout {
array_stride: mem::size_of::<Vertex>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[wgpu::VertexAttribute {
shader_location: 0,
format: wgpu::VertexFormat::Float32x2,
offset: 0,
}],
},
wgpu::VertexBufferLayout {
array_stride: mem::size_of::<Instance>() as u64,
step_mode: wgpu::VertexStepMode::Instance,
attributes: &wgpu::vertex_attr_array!(
1 => Float32x2,
2 => Float32x2,
3 => Float32x2,
4 => Float32x2,
5 => Sint32,
),
},
],
buffers: &[wgpu::VertexBufferLayout {
array_stride: mem::size_of::<Instance>() as u64,
step_mode: wgpu::VertexStepMode::Instance,
attributes: &wgpu::vertex_attr_array!(
1 => Float32x2,
2 => Float32x2,
3 => Float32x2,
4 => Float32x2,
5 => Sint32,
),
}],
},
fragment: Some(wgpu::FragmentState {
module: &shader,
@ -275,20 +260,6 @@ impl Pipeline {
multiview: None,
});
let vertices =
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("iced_wgpu::image vertex buffer"),
contents: bytemuck::cast_slice(&QUAD_VERTICES),
usage: wgpu::BufferUsages::VERTEX,
});
let indices =
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("iced_wgpu::image index buffer"),
contents: bytemuck::cast_slice(&QUAD_INDICES),
usage: wgpu::BufferUsages::INDEX,
});
let texture_atlas = Atlas::new(device);
let texture = device.create_bind_group(&wgpu::BindGroupDescriptor {
@ -310,8 +281,6 @@ impl Pipeline {
vector_cache: RefCell::new(vector::Cache::default()),
pipeline,
vertices,
indices,
sampler,
texture,
texture_version: texture_atlas.layer_count(),
@ -469,11 +438,6 @@ impl Pipeline {
);
render_pass.set_bind_group(1, &self.texture, &[]);
render_pass.set_index_buffer(
self.indices.slice(..),
wgpu::IndexFormat::Uint16,
);
render_pass.set_vertex_buffer(0, self.vertices.slice(..));
layer.render(render_pass);
}
@ -490,29 +454,6 @@ impl Pipeline {
}
}
#[repr(C)]
#[derive(Clone, Copy, Zeroable, Pod)]
pub struct Vertex {
_position: [f32; 2],
}
const QUAD_INDICES: [u16; 6] = [0, 1, 2, 0, 2, 3];
const QUAD_VERTICES: [Vertex; 4] = [
Vertex {
_position: [0.0, 0.0],
},
Vertex {
_position: [1.0, 0.0],
},
Vertex {
_position: [1.0, 1.0],
},
Vertex {
_position: [0.0, 1.0],
},
];
#[repr(C)]
#[derive(Debug, Clone, Copy, Zeroable, Pod)]
struct Instance {

View file

@ -7,7 +7,7 @@ struct Globals {
@group(1) @binding(0) var u_texture: texture_2d_array<f32>;
struct VertexInput {
@location(0) v_pos: vec2<f32>,
@builtin(vertex_index) vertex_index: u32,
@location(1) pos: vec2<f32>,
@location(2) scale: vec2<f32>,
@location(3) atlas_pos: vec2<f32>,
@ -25,7 +25,9 @@ struct VertexOutput {
fn vs_main(input: VertexInput) -> VertexOutput {
var out: VertexOutput;
out.uv = vec2<f32>(input.v_pos * input.atlas_scale + input.atlas_pos);
let v_pos = vertex_position(input.vertex_index);
out.uv = vec2<f32>(v_pos * input.atlas_scale + input.atlas_pos);
out.layer = f32(input.layer);
var transform: mat4x4<f32> = mat4x4<f32>(
@ -35,7 +37,7 @@ fn vs_main(input: VertexInput) -> VertexOutput {
vec4<f32>(input.pos, 0.0, 1.0)
);
out.position = globals.transform * transform * vec4<f32>(input.v_pos, 0.0, 1.0);
out.position = globals.transform * transform * vec4<f32>(v_pos, 0.0, 1.0);
return out;
}