|
|
@@ -1,5 +1,7 @@
|
|
|
use std::iter;
|
|
|
|
|
|
+use wgpu::util::{DeviceExt, RenderEncoder};
|
|
|
+
|
|
|
use winit::{
|
|
|
event::*,
|
|
|
event_loop::EventLoop,
|
|
|
@@ -7,8 +9,51 @@ use winit::{
|
|
|
window::{Window, WindowBuilder},
|
|
|
};
|
|
|
|
|
|
-#[cfg(target_arch = "wasm32")]
|
|
|
-use wasm_bindgen::prelude::*;
|
|
|
+#[repr(C)]
|
|
|
+#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
|
|
|
+struct Vertex {
|
|
|
+ position: [f32; 3],
|
|
|
+ color: [f32; 3],
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+impl Vertex {
|
|
|
+ fn desc() -> wgpu::VertexBufferLayout<'static> {
|
|
|
+ wgpu::VertexBufferLayout {
|
|
|
+ array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
|
|
|
+ step_mode: wgpu::VertexStepMode::Vertex,
|
|
|
+ attributes: &[
|
|
|
+ wgpu::VertexAttribute {
|
|
|
+ offset: 0,
|
|
|
+ shader_location: 0,
|
|
|
+ format: wgpu::VertexFormat::Float32x3,
|
|
|
+ },
|
|
|
+ wgpu::VertexAttribute {
|
|
|
+ offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
|
|
|
+ shader_location: 1,
|
|
|
+ format: wgpu::VertexFormat::Float32x3,
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+const VERTICES: &[Vertex] = &[
|
|
|
+ Vertex { position: [-0.0868241, 0.49240386, 0.0], color: [0.5, 0.0, 0.5] }, // A
|
|
|
+ Vertex { position: [-0.49513406, 0.06958647, 0.0], color: [0.5, 0.0, 0.5] }, // B
|
|
|
+ Vertex { position: [-0.21918549, -0.44939706, 0.0], color: [0.5, 0.0, 0.5] }, // C
|
|
|
+ Vertex { position: [0.35966998, -0.3473291, 0.0], color: [0.5, 0.0, 0.5] }, // D
|
|
|
+ Vertex { position: [0.44147372, 0.2347359, 0.0], color: [0.5, 0.0, 0.5] }, // E
|
|
|
+];
|
|
|
+
|
|
|
+const INDICES: &[u16] = &[
|
|
|
+ 0, 1, 4,
|
|
|
+ 1, 2, 4,
|
|
|
+ 2, 3, 4,
|
|
|
+];
|
|
|
+
|
|
|
+
|
|
|
|
|
|
struct State<'a> {
|
|
|
surface: wgpu::Surface<'a>,
|
|
|
@@ -20,6 +65,12 @@ struct State<'a> {
|
|
|
// it gets dropped after it as the surface contains
|
|
|
// unsafe references to the window's resources.
|
|
|
window: &'a Window,
|
|
|
+ render_pipeline: wgpu::RenderPipeline,
|
|
|
+
|
|
|
+ vertex_buffer: wgpu::Buffer,
|
|
|
+ index_buffer: wgpu::Buffer,
|
|
|
+ num_vertices: u32,
|
|
|
+ num_indices: u32,
|
|
|
}
|
|
|
|
|
|
impl<'a> State<'a> {
|
|
|
@@ -64,6 +115,7 @@ impl<'a> State<'a> {
|
|
|
.unwrap();
|
|
|
|
|
|
let surface_caps = surface.get_capabilities(&adapter);
|
|
|
+ //println!("{:?}", &surface_caps.present_modes);
|
|
|
// Shader code in this tutorial assumes an Srgb surface texture. Using a different
|
|
|
// one will result all the colors comming out darker. If you want to support non
|
|
|
// Srgb surfaces, you'll need to account for that when drawing to the frame.
|
|
|
@@ -85,6 +137,75 @@ impl<'a> State<'a> {
|
|
|
};
|
|
|
surface.configure(&device, &config);
|
|
|
|
|
|
+
|
|
|
+ let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
|
|
+ label: Some("Shader"),
|
|
|
+ source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()),
|
|
|
+ });
|
|
|
+
|
|
|
+ let render_pipeline_layout =
|
|
|
+ device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
|
|
+ label: Some("Render Pipeline Layout"),
|
|
|
+ bind_group_layouts: &[],
|
|
|
+ push_constant_ranges: &[],
|
|
|
+ });
|
|
|
+
|
|
|
+ let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor{
|
|
|
+ label: Some("Render Pipeline"),
|
|
|
+ layout: Some(&render_pipeline_layout),
|
|
|
+ vertex: wgpu::VertexState{
|
|
|
+ module: &shader,
|
|
|
+ entry_point: "vs_main",
|
|
|
+ buffers: &[
|
|
|
+ Vertex::desc()
|
|
|
+ ],
|
|
|
+ },
|
|
|
+ fragment: Some(wgpu::FragmentState{
|
|
|
+ module: &shader,
|
|
|
+ entry_point: "fs_main",
|
|
|
+ targets: &[Some(wgpu::ColorTargetState{
|
|
|
+ format: config.format,
|
|
|
+ blend: Some(wgpu::BlendState::REPLACE),
|
|
|
+ write_mask: wgpu::ColorWrites::ALL,
|
|
|
+ })],
|
|
|
+ }),
|
|
|
+ primitive: wgpu::PrimitiveState{
|
|
|
+ topology: wgpu::PrimitiveTopology::TriangleList,
|
|
|
+ strip_index_format: None,
|
|
|
+ front_face: wgpu::FrontFace::Ccw,
|
|
|
+ cull_mode: Some(wgpu::Face::Back),
|
|
|
+ polygon_mode: wgpu::PolygonMode::Fill,
|
|
|
+ unclipped_depth: false,
|
|
|
+ conservative: false,
|
|
|
+ },
|
|
|
+ depth_stencil: None,
|
|
|
+ multisample: wgpu::MultisampleState{
|
|
|
+ count: 1,
|
|
|
+ mask: !0,
|
|
|
+ alpha_to_coverage_enabled: false,
|
|
|
+ },
|
|
|
+ multiview: None,
|
|
|
+ });
|
|
|
+
|
|
|
+ // Create a buffer with the vertex data
|
|
|
+ let vertex_buffer = device.create_buffer_init(
|
|
|
+ &wgpu::util::BufferInitDescriptor{
|
|
|
+ label: Some("Vertex Buffer"),
|
|
|
+ contents: bytemuck::cast_slice(VERTICES),
|
|
|
+ usage: wgpu::BufferUsages::VERTEX,
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
+ let index_buffer = device.create_buffer_init(
|
|
|
+ &wgpu::util::BufferInitDescriptor{
|
|
|
+ label: Some("Indice Buffer"),
|
|
|
+ usage: wgpu::BufferUsages::INDEX,
|
|
|
+ contents: bytemuck::cast_slice(INDICES),
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
+ let num_vertices = VERTICES.len() as u32;
|
|
|
+ let num_indices = INDICES.len() as u32;
|
|
|
Self {
|
|
|
surface,
|
|
|
device,
|
|
|
@@ -92,6 +213,11 @@ impl<'a> State<'a> {
|
|
|
config,
|
|
|
size,
|
|
|
window,
|
|
|
+ render_pipeline,
|
|
|
+ vertex_buffer,
|
|
|
+ index_buffer,
|
|
|
+ num_vertices,
|
|
|
+ num_indices,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -128,7 +254,7 @@ impl<'a> State<'a> {
|
|
|
});
|
|
|
|
|
|
{
|
|
|
- let _render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
|
|
+ let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
|
|
label: Some("Render Pass"),
|
|
|
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
|
|
view: &view,
|
|
|
@@ -147,6 +273,12 @@ impl<'a> State<'a> {
|
|
|
occlusion_query_set: None,
|
|
|
timestamp_writes: None,
|
|
|
});
|
|
|
+
|
|
|
+ render_pass.set_pipeline(&self.render_pipeline);
|
|
|
+
|
|
|
+ render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
|
|
|
+ render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
|
|
|
+ render_pass.draw_indexed(0..self.num_indices,0, 0..1);
|
|
|
}
|
|
|
|
|
|
self.queue.submit(iter::once(encoder.finish()));
|