mirror of
https://github.com/guilhermewerner/wgpu-renderer
synced 2025-06-19 15:24:19 +00:00
Update project
This commit is contained in:
206
Examples/Triangle.rs
Normal file
206
Examples/Triangle.rs
Normal file
@ -0,0 +1,206 @@
|
||||
#![allow(non_snake_case)]
|
||||
#![allow(unused_variables)]
|
||||
|
||||
use anyhow::Result;
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use std::time::Duration;
|
||||
use wgpu::util::DeviceExt;
|
||||
use winit::event::*;
|
||||
use Graphics::Render::*;
|
||||
use Graphics::{Display, Runtime, State};
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Debug, Pod, Zeroable)]
|
||||
struct TriangleVertex {
|
||||
position: [f32; 3],
|
||||
color: [f32; 3],
|
||||
}
|
||||
|
||||
impl Vertex for TriangleVertex {
|
||||
fn GetDescriptor<'a>() -> wgpu::VertexBufferLayout<'a> {
|
||||
wgpu::VertexBufferLayout {
|
||||
array_stride: std::mem::size_of::<TriangleVertex>() 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,
|
||||
},
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
const VERTICES: &[TriangleVertex] = &[
|
||||
TriangleVertex { position: [0.0, 0.5, 0.0], color: [1.0, 0.0, 0.0] },
|
||||
TriangleVertex { position: [-0.5, -0.5, 0.0], color: [0.0, 1.0, 0.0] },
|
||||
TriangleVertex { position: [0.5, -0.5, 0.0], color: [0.0, 0.0, 1.0] },
|
||||
];
|
||||
|
||||
#[rustfmt::skip]
|
||||
const INDICES: &[u16] = &[
|
||||
0, 1, 2,
|
||||
];
|
||||
|
||||
struct Triangle {
|
||||
render_pipeline: wgpu::RenderPipeline,
|
||||
vertex_buffer: wgpu::Buffer,
|
||||
index_buffer: wgpu::Buffer,
|
||||
num_indices: u32,
|
||||
}
|
||||
|
||||
impl State for Triangle {
|
||||
fn Init(display: &Display) -> Result<Self> {
|
||||
// Shader
|
||||
|
||||
let shader = display
|
||||
.device
|
||||
.create_shader_module(&wgpu::ShaderModuleDescriptor {
|
||||
label: Some("Shader"),
|
||||
source: wgpu::ShaderSource::Wgsl(include_str!("../Shaders/Triangle.wgsl").into()),
|
||||
});
|
||||
|
||||
// Pipeline
|
||||
|
||||
let render_pipeline_layout =
|
||||
display
|
||||
.device
|
||||
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("Render Pipeline Layout"),
|
||||
bind_group_layouts: &[],
|
||||
push_constant_ranges: &[],
|
||||
});
|
||||
|
||||
let render_pipeline =
|
||||
display
|
||||
.device
|
||||
.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Render Pipeline"),
|
||||
layout: Some(&render_pipeline_layout),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &shader,
|
||||
entry_point: "main",
|
||||
buffers: &[TriangleVertex::GetDescriptor()],
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
// 3.
|
||||
module: &shader,
|
||||
entry_point: "main",
|
||||
targets: &[wgpu::ColorTargetState {
|
||||
// 4.
|
||||
format: display.config.format,
|
||||
blend: Some(wgpu::BlendState::REPLACE),
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
}],
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState {
|
||||
topology: wgpu::PrimitiveTopology::TriangleList, // 1.
|
||||
strip_index_format: None,
|
||||
front_face: wgpu::FrontFace::Ccw, // 2.
|
||||
cull_mode: Some(wgpu::Face::Back),
|
||||
// Setting this to anything other than Fill requires Features::NON_FILL_POLYGON_MODE
|
||||
polygon_mode: wgpu::PolygonMode::Fill,
|
||||
// Requires Features::DEPTH_CLAMPING
|
||||
clamp_depth: false,
|
||||
// Requires Features::CONSERVATIVE_RASTERIZATION
|
||||
conservative: false,
|
||||
},
|
||||
depth_stencil: None, // 1.
|
||||
multisample: wgpu::MultisampleState {
|
||||
count: 1, // 2.
|
||||
mask: !0, // 3.
|
||||
alpha_to_coverage_enabled: false, // 4.
|
||||
},
|
||||
});
|
||||
|
||||
let vertex_buffer = display
|
||||
.device
|
||||
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("Vertex Buffer"),
|
||||
contents: bytemuck::cast_slice(VERTICES),
|
||||
usage: wgpu::BufferUsages::VERTEX,
|
||||
});
|
||||
|
||||
let index_buffer = display
|
||||
.device
|
||||
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("Index Buffer"),
|
||||
contents: bytemuck::cast_slice(INDICES),
|
||||
usage: wgpu::BufferUsages::INDEX,
|
||||
});
|
||||
|
||||
let num_indices = INDICES.len() as u32;
|
||||
|
||||
Ok(Self {
|
||||
render_pipeline,
|
||||
vertex_buffer,
|
||||
index_buffer,
|
||||
num_indices,
|
||||
})
|
||||
}
|
||||
|
||||
fn Input(&mut self, display: &Display, event: &WindowEvent) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn Update(&mut self, display: &Display, delta: Duration) {}
|
||||
|
||||
fn Resize(&mut self, display: &Display) {}
|
||||
|
||||
fn Draw(&mut self, display: &mut Display) -> Result<(), wgpu::SurfaceError> {
|
||||
let output = display.surface.get_current_texture().unwrap();
|
||||
let view = output
|
||||
.texture
|
||||
.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
|
||||
let mut encoder = display
|
||||
.device
|
||||
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
||||
label: Some("Render Encoder"),
|
||||
});
|
||||
|
||||
{
|
||||
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: Some("Render Pass"),
|
||||
color_attachments: &[
|
||||
// This is what [[location(0)]] in the fragment shader targets
|
||||
wgpu::RenderPassColorAttachment {
|
||||
view: &view,
|
||||
resolve_target: None,
|
||||
ops: wgpu::Operations {
|
||||
load: wgpu::LoadOp::Clear(wgpu::Color {
|
||||
r: 0.0,
|
||||
g: 0.0,
|
||||
b: 0.0,
|
||||
a: 1.0,
|
||||
}),
|
||||
store: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
depth_stencil_attachment: 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);
|
||||
}
|
||||
|
||||
display.queue.submit(std::iter::once(encoder.finish()));
|
||||
output.present();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
Runtime::Execute::<Triangle>()
|
||||
}
|
Reference in New Issue
Block a user