mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-19 22:11:01 +01:00
419 lines
15 KiB
Rust
419 lines
15 KiB
Rust
#[path = "../framework.rs"]
|
|
mod framework;
|
|
|
|
use bytemuck::{Pod, Zeroable};
|
|
use std::{borrow::Cow, iter, mem};
|
|
use wgpu::util::DeviceExt;
|
|
|
|
#[repr(C)]
|
|
#[derive(Clone, Copy, Pod, Zeroable)]
|
|
struct Vertex {
|
|
_pos: [f32; 4],
|
|
_tex_coord: [f32; 2],
|
|
}
|
|
|
|
fn vertex(pos: [i8; 3], tc: [i8; 2]) -> Vertex {
|
|
Vertex {
|
|
_pos: [pos[0] as f32, pos[1] as f32, pos[2] as f32, 1.0],
|
|
_tex_coord: [tc[0] as f32, tc[1] as f32],
|
|
}
|
|
}
|
|
|
|
fn create_vertices() -> (Vec<Vertex>, Vec<u16>) {
|
|
let vertex_data = [
|
|
// top (0, 0, 1)
|
|
vertex([-1, -1, 1], [0, 0]),
|
|
vertex([1, -1, 1], [1, 0]),
|
|
vertex([1, 1, 1], [1, 1]),
|
|
vertex([-1, 1, 1], [0, 1]),
|
|
// bottom (0, 0, -1)
|
|
vertex([-1, 1, -1], [1, 0]),
|
|
vertex([1, 1, -1], [0, 0]),
|
|
vertex([1, -1, -1], [0, 1]),
|
|
vertex([-1, -1, -1], [1, 1]),
|
|
// right (1, 0, 0)
|
|
vertex([1, -1, -1], [0, 0]),
|
|
vertex([1, 1, -1], [1, 0]),
|
|
vertex([1, 1, 1], [1, 1]),
|
|
vertex([1, -1, 1], [0, 1]),
|
|
// left (-1, 0, 0)
|
|
vertex([-1, -1, 1], [1, 0]),
|
|
vertex([-1, 1, 1], [0, 0]),
|
|
vertex([-1, 1, -1], [0, 1]),
|
|
vertex([-1, -1, -1], [1, 1]),
|
|
// front (0, 1, 0)
|
|
vertex([1, 1, -1], [1, 0]),
|
|
vertex([-1, 1, -1], [0, 0]),
|
|
vertex([-1, 1, 1], [0, 1]),
|
|
vertex([1, 1, 1], [1, 1]),
|
|
// back (0, -1, 0)
|
|
vertex([1, -1, 1], [0, 0]),
|
|
vertex([-1, -1, 1], [1, 0]),
|
|
vertex([-1, -1, -1], [1, 1]),
|
|
vertex([1, -1, -1], [0, 1]),
|
|
];
|
|
|
|
let index_data: &[u16] = &[
|
|
0, 1, 2, 2, 3, 0, // top
|
|
4, 5, 6, 6, 7, 4, // bottom
|
|
8, 9, 10, 10, 11, 8, // right
|
|
12, 13, 14, 14, 15, 12, // left
|
|
16, 17, 18, 18, 19, 16, // front
|
|
20, 21, 22, 22, 23, 20, // back
|
|
];
|
|
|
|
(vertex_data.to_vec(), index_data.to_vec())
|
|
}
|
|
|
|
fn create_texels(size: usize) -> Vec<u8> {
|
|
(0..size * size)
|
|
.flat_map(|id| {
|
|
// get high five for recognizing this ;)
|
|
let cx = 3.0 * (id % size) as f32 / (size - 1) as f32 - 2.0;
|
|
let cy = 2.0 * (id / size) as f32 / (size - 1) as f32 - 1.0;
|
|
let (mut x, mut y, mut count) = (cx, cy, 0);
|
|
while count < 0xFF && x * x + y * y < 4.0 {
|
|
let old_x = x;
|
|
x = x * x - y * y + cx;
|
|
y = 2.0 * old_x * y + cy;
|
|
count += 1;
|
|
}
|
|
iter::once(0xFF - (count * 5) as u8)
|
|
.chain(iter::once(0xFF - (count * 15) as u8))
|
|
.chain(iter::once(0xFF - (count * 50) as u8))
|
|
.chain(iter::once(1))
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
struct Example {
|
|
vertex_buf: wgpu::Buffer,
|
|
index_buf: wgpu::Buffer,
|
|
index_count: usize,
|
|
bind_group: wgpu::BindGroup,
|
|
uniform_buf: wgpu::Buffer,
|
|
pipeline: wgpu::RenderPipeline,
|
|
pipeline_wire: Option<wgpu::RenderPipeline>,
|
|
}
|
|
|
|
impl Example {
|
|
fn generate_matrix(aspect_ratio: f32) -> cgmath::Matrix4<f32> {
|
|
let mx_projection = cgmath::perspective(cgmath::Deg(45f32), aspect_ratio, 1.0, 10.0);
|
|
let mx_view = cgmath::Matrix4::look_at_rh(
|
|
cgmath::Point3::new(1.5f32, -5.0, 3.0),
|
|
cgmath::Point3::new(0f32, 0.0, 0.0),
|
|
cgmath::Vector3::unit_z(),
|
|
);
|
|
let mx_correction = framework::OPENGL_TO_WGPU_MATRIX;
|
|
mx_correction * mx_projection * mx_view
|
|
}
|
|
}
|
|
|
|
impl framework::Example for Example {
|
|
fn optional_features() -> wgt::Features {
|
|
wgt::Features::NON_FILL_POLYGON_MODE
|
|
}
|
|
|
|
fn init(
|
|
sc_desc: &wgpu::SwapChainDescriptor,
|
|
adapter: &wgpu::Adapter,
|
|
device: &wgpu::Device,
|
|
queue: &wgpu::Queue,
|
|
) -> Self {
|
|
// Create the vertex and index buffers
|
|
let vertex_size = mem::size_of::<Vertex>();
|
|
let (vertex_data, index_data) = create_vertices();
|
|
|
|
let vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
label: Some("Vertex Buffer"),
|
|
contents: bytemuck::cast_slice(&vertex_data),
|
|
usage: wgpu::BufferUsage::VERTEX,
|
|
});
|
|
|
|
let index_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
label: Some("Index Buffer"),
|
|
contents: bytemuck::cast_slice(&index_data),
|
|
usage: wgpu::BufferUsage::INDEX,
|
|
});
|
|
|
|
// Create pipeline layout
|
|
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
|
label: None,
|
|
entries: &[
|
|
wgpu::BindGroupLayoutEntry {
|
|
binding: 0,
|
|
visibility: wgpu::ShaderStage::VERTEX,
|
|
ty: wgpu::BindingType::Buffer {
|
|
ty: wgpu::BufferBindingType::Uniform,
|
|
has_dynamic_offset: false,
|
|
min_binding_size: wgpu::BufferSize::new(64),
|
|
},
|
|
count: None,
|
|
},
|
|
wgpu::BindGroupLayoutEntry {
|
|
binding: 1,
|
|
visibility: wgpu::ShaderStage::FRAGMENT,
|
|
ty: wgpu::BindingType::Texture {
|
|
multisampled: false,
|
|
sample_type: wgpu::TextureSampleType::Float { filterable: true },
|
|
view_dimension: wgpu::TextureViewDimension::D2,
|
|
},
|
|
count: None,
|
|
},
|
|
wgpu::BindGroupLayoutEntry {
|
|
binding: 2,
|
|
visibility: wgpu::ShaderStage::FRAGMENT,
|
|
ty: wgpu::BindingType::Sampler {
|
|
comparison: false,
|
|
filtering: true,
|
|
},
|
|
count: None,
|
|
},
|
|
],
|
|
});
|
|
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
|
label: None,
|
|
bind_group_layouts: &[&bind_group_layout],
|
|
push_constant_ranges: &[],
|
|
});
|
|
|
|
// Create the texture
|
|
let size = 256u32;
|
|
let texels = create_texels(size as usize);
|
|
let texture_extent = wgpu::Extent3d {
|
|
width: size,
|
|
height: size,
|
|
depth_or_array_layers: 1,
|
|
};
|
|
let texture = device.create_texture(&wgpu::TextureDescriptor {
|
|
label: None,
|
|
size: texture_extent,
|
|
mip_level_count: 1,
|
|
sample_count: 1,
|
|
dimension: wgpu::TextureDimension::D2,
|
|
format: wgpu::TextureFormat::Rgba8UnormSrgb,
|
|
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST,
|
|
});
|
|
let texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default());
|
|
queue.write_texture(
|
|
wgpu::TextureCopyView {
|
|
texture: &texture,
|
|
mip_level: 0,
|
|
origin: wgpu::Origin3d::ZERO,
|
|
},
|
|
&texels,
|
|
wgpu::TextureDataLayout {
|
|
offset: 0,
|
|
bytes_per_row: 4 * size,
|
|
rows_per_image: 0,
|
|
},
|
|
texture_extent,
|
|
);
|
|
|
|
// Create other resources
|
|
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
|
|
address_mode_u: wgpu::AddressMode::ClampToEdge,
|
|
address_mode_v: wgpu::AddressMode::ClampToEdge,
|
|
address_mode_w: wgpu::AddressMode::ClampToEdge,
|
|
mag_filter: wgpu::FilterMode::Nearest,
|
|
min_filter: wgpu::FilterMode::Linear,
|
|
mipmap_filter: wgpu::FilterMode::Nearest,
|
|
..Default::default()
|
|
});
|
|
let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
|
|
let mx_ref: &[f32; 16] = mx_total.as_ref();
|
|
let uniform_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
label: Some("Uniform Buffer"),
|
|
contents: bytemuck::cast_slice(mx_ref),
|
|
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
|
});
|
|
|
|
// Create bind group
|
|
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
|
layout: &bind_group_layout,
|
|
entries: &[
|
|
wgpu::BindGroupEntry {
|
|
binding: 0,
|
|
resource: uniform_buf.as_entire_binding(),
|
|
},
|
|
wgpu::BindGroupEntry {
|
|
binding: 1,
|
|
resource: wgpu::BindingResource::TextureView(&texture_view),
|
|
},
|
|
wgpu::BindGroupEntry {
|
|
binding: 2,
|
|
resource: wgpu::BindingResource::Sampler(&sampler),
|
|
},
|
|
],
|
|
label: None,
|
|
});
|
|
|
|
let mut flags = wgpu::ShaderFlags::VALIDATION;
|
|
match adapter.get_info().backend {
|
|
wgpu::Backend::Metal | wgpu::Backend::Vulkan => {
|
|
flags |= wgpu::ShaderFlags::EXPERIMENTAL_TRANSLATION
|
|
}
|
|
_ => (), //TODO
|
|
}
|
|
let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
|
|
label: None,
|
|
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("shader.wgsl"))),
|
|
flags,
|
|
});
|
|
|
|
let vertex_buffers = [wgpu::VertexBufferLayout {
|
|
array_stride: vertex_size as wgpu::BufferAddress,
|
|
step_mode: wgpu::InputStepMode::Vertex,
|
|
attributes: &[
|
|
wgpu::VertexAttribute {
|
|
format: wgpu::VertexFormat::Float32x4,
|
|
offset: 0,
|
|
shader_location: 0,
|
|
},
|
|
wgpu::VertexAttribute {
|
|
format: wgpu::VertexFormat::Float32x2,
|
|
offset: 4 * 4,
|
|
shader_location: 1,
|
|
},
|
|
],
|
|
}];
|
|
|
|
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
|
label: None,
|
|
layout: Some(&pipeline_layout),
|
|
vertex: wgpu::VertexState {
|
|
module: &shader,
|
|
entry_point: "vs_main",
|
|
buffers: &vertex_buffers,
|
|
},
|
|
fragment: Some(wgpu::FragmentState {
|
|
module: &shader,
|
|
entry_point: "fs_main",
|
|
targets: &[sc_desc.format.into()],
|
|
}),
|
|
primitive: wgpu::PrimitiveState {
|
|
cull_mode: Some(wgpu::Face::Back),
|
|
..Default::default()
|
|
},
|
|
depth_stencil: None,
|
|
multisample: wgpu::MultisampleState::default(),
|
|
});
|
|
|
|
let pipeline_wire = if device
|
|
.features()
|
|
.contains(wgt::Features::NON_FILL_POLYGON_MODE)
|
|
{
|
|
let pipeline_wire = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
|
label: None,
|
|
layout: Some(&pipeline_layout),
|
|
vertex: wgpu::VertexState {
|
|
module: &shader,
|
|
entry_point: "vs_main",
|
|
buffers: &vertex_buffers,
|
|
},
|
|
fragment: Some(wgpu::FragmentState {
|
|
module: &shader,
|
|
entry_point: "fs_wire",
|
|
targets: &[wgpu::ColorTargetState {
|
|
format: sc_desc.format,
|
|
blend: Some(wgpu::BlendState {
|
|
color: wgpu::BlendComponent {
|
|
operation: wgpu::BlendOperation::Add,
|
|
src_factor: wgpu::BlendFactor::SrcAlpha,
|
|
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
|
|
},
|
|
alpha: wgpu::BlendComponent::REPLACE,
|
|
}),
|
|
write_mask: wgpu::ColorWrite::ALL,
|
|
}],
|
|
}),
|
|
primitive: wgpu::PrimitiveState {
|
|
front_face: wgpu::FrontFace::Ccw,
|
|
cull_mode: Some(wgpu::Face::Back),
|
|
polygon_mode: wgpu::PolygonMode::Line,
|
|
..Default::default()
|
|
},
|
|
depth_stencil: None,
|
|
multisample: wgpu::MultisampleState::default(),
|
|
});
|
|
Some(pipeline_wire)
|
|
} else {
|
|
None
|
|
};
|
|
|
|
// Done
|
|
Example {
|
|
vertex_buf,
|
|
index_buf,
|
|
index_count: index_data.len(),
|
|
bind_group,
|
|
uniform_buf,
|
|
pipeline,
|
|
pipeline_wire,
|
|
}
|
|
}
|
|
|
|
fn update(&mut self, _event: winit::event::WindowEvent) {
|
|
//empty
|
|
}
|
|
|
|
fn resize(
|
|
&mut self,
|
|
sc_desc: &wgpu::SwapChainDescriptor,
|
|
_device: &wgpu::Device,
|
|
queue: &wgpu::Queue,
|
|
) {
|
|
let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
|
|
let mx_ref: &[f32; 16] = mx_total.as_ref();
|
|
queue.write_buffer(&self.uniform_buf, 0, bytemuck::cast_slice(mx_ref));
|
|
}
|
|
|
|
fn render(
|
|
&mut self,
|
|
frame: &wgpu::SwapChainTexture,
|
|
device: &wgpu::Device,
|
|
queue: &wgpu::Queue,
|
|
_spawner: &framework::Spawner,
|
|
) {
|
|
let mut encoder =
|
|
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
|
|
{
|
|
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
|
label: None,
|
|
color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
|
|
attachment: &frame.view,
|
|
resolve_target: None,
|
|
ops: wgpu::Operations {
|
|
load: wgpu::LoadOp::Clear(wgpu::Color {
|
|
r: 0.1,
|
|
g: 0.2,
|
|
b: 0.3,
|
|
a: 1.0,
|
|
}),
|
|
store: true,
|
|
},
|
|
}],
|
|
depth_stencil_attachment: None,
|
|
});
|
|
rpass.push_debug_group("Prepare data for draw.");
|
|
rpass.set_pipeline(&self.pipeline);
|
|
rpass.set_bind_group(0, &self.bind_group, &[]);
|
|
rpass.set_index_buffer(self.index_buf.slice(..), wgpu::IndexFormat::Uint16);
|
|
rpass.set_vertex_buffer(0, self.vertex_buf.slice(..));
|
|
rpass.pop_debug_group();
|
|
rpass.insert_debug_marker("Draw!");
|
|
rpass.draw_indexed(0..self.index_count as u32, 0, 0..1);
|
|
if let Some(ref pipe) = self.pipeline_wire {
|
|
rpass.set_pipeline(pipe);
|
|
rpass.draw_indexed(0..self.index_count as u32, 0, 0..1);
|
|
}
|
|
}
|
|
|
|
queue.submit(Some(encoder.finish()));
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
framework::run::<Example>("cube");
|
|
}
|