First half of buffers
This commit is contained in:
15
Cargo.lock
generated
15
Cargo.lock
generated
@@ -127,6 +127,20 @@ name = "bytemuck"
|
||||
version = "1.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72957246c41db82b8ef88a5486143830adeb8227ef9837740bdec67724cf2c5b"
|
||||
dependencies = [
|
||||
"bytemuck_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bytemuck_derive"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e215f8c2f9f79cb53c8335e687ffd07d5bfcb6fe5fc80723762d0be46e7cc54"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
@@ -1902,6 +1916,7 @@ dependencies = [
|
||||
name = "wgpu-rs-learning"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"cgmath",
|
||||
"env_logger",
|
||||
"image",
|
||||
|
||||
@@ -13,3 +13,4 @@ env_logger = "0.9"
|
||||
log = "0.4"
|
||||
wgpu = "0.9"
|
||||
pollster = "0.2"
|
||||
bytemuck = { version = "1.4", features = [ "derive" ] }
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
// Vertex shader
|
||||
|
||||
struct VertexOutput {
|
||||
[[builtin(position)]] clip_coordinates: vec4<f32>;
|
||||
[[location(0)]] position: vec2<f32>;
|
||||
};
|
||||
|
||||
[[stage(vertex)]]
|
||||
fn main(
|
||||
[[builtin(vertex_index)]] in_vertex_index: u32,
|
||||
) -> VertexOutput {
|
||||
fn main([[builtin(vertex_index)]] in_vertex_index: u32) -> VertexOutput {
|
||||
var out: VertexOutput;
|
||||
let x = f32(1 - i32(in_vertex_index)) * 0.5;
|
||||
let y = f32(i32(in_vertex_index & 1u) * 2 - 1) * 0.5;
|
||||
@@ -18,7 +15,6 @@ fn main(
|
||||
}
|
||||
|
||||
// Fragment shader
|
||||
|
||||
[[stage(fragment)]]
|
||||
fn main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
|
||||
let r = in.position.x;
|
||||
|
||||
@@ -5,6 +5,7 @@ use winit::{
|
||||
};
|
||||
|
||||
mod state;
|
||||
mod vertex;
|
||||
|
||||
use crate::state::State;
|
||||
|
||||
|
||||
@@ -1,23 +1,24 @@
|
||||
// Vertex shader
|
||||
struct VertexInput {
|
||||
[[location(0)]] position: vec3<f32>;
|
||||
[[location(1)]] color: vec3<f32>;
|
||||
};
|
||||
|
||||
struct VertexOutput {
|
||||
[[builtin(position)]] clip_position: vec4<f32>;
|
||||
[[builtin(position)]] clip_coordinate: vec4<f32>;
|
||||
[[location(0)]] color: vec3<f32>;
|
||||
};
|
||||
|
||||
[[stage(vertex)]]
|
||||
fn main(
|
||||
[[builtin(vertex_index)]] in_vertex_index: u32,
|
||||
) -> VertexOutput {
|
||||
fn main(model: VertexInput) -> VertexOutput {
|
||||
var out: VertexOutput;
|
||||
let x = f32(1 - i32(in_vertex_index)) * 0.5;
|
||||
let y = f32(i32(in_vertex_index & 1u) * 2 - 1) * 0.5;
|
||||
out.clip_position = vec4<f32>(x, y, 0.0, 1.0);
|
||||
out.clip_coordinate = vec4<f32>(model.position, 1.0);
|
||||
out.color = model.color;
|
||||
return out;
|
||||
}
|
||||
|
||||
// Fragment shader
|
||||
|
||||
[[stage(fragment)]]
|
||||
fn main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
|
||||
return vec4<f32>(0.3, 0.2, 0.1, 1.0);
|
||||
return vec4<f32>(in.color, 1.0);
|
||||
}
|
||||
|
||||
38
src/state.rs
38
src/state.rs
@@ -1,9 +1,14 @@
|
||||
use wgpu::PrimitiveTopology;
|
||||
use wgpu::{
|
||||
PrimitiveTopology,
|
||||
util::DeviceExt,
|
||||
};
|
||||
use winit::{
|
||||
event::*,
|
||||
window::Window,
|
||||
};
|
||||
|
||||
use crate::vertex::Vertex;
|
||||
|
||||
/// Hold state with important information
|
||||
pub struct State {
|
||||
surface: wgpu::Surface,
|
||||
@@ -14,8 +19,10 @@ pub struct State {
|
||||
pub size: winit::dpi::PhysicalSize<u32>,
|
||||
clear_color: wgpu::Color,
|
||||
render_pipeline: wgpu::RenderPipeline,
|
||||
challenge_render_pipeline: wgpu::RenderPipeline,
|
||||
//challenge_render_pipeline: wgpu::RenderPipeline,
|
||||
use_challenge_render_pipeline: bool,
|
||||
vertex_buffer: wgpu::Buffer,
|
||||
num_vertices: u32,
|
||||
}
|
||||
|
||||
impl State {
|
||||
@@ -93,8 +100,8 @@ impl State {
|
||||
module: &shader,
|
||||
// function name in shader.wgsl for [[stage(vertex)]]
|
||||
entry_point: "main",
|
||||
// already specified in the shader
|
||||
buffers: &[],
|
||||
// specify memory layout
|
||||
buffers: &[Vertex::desc()],
|
||||
|
||||
},
|
||||
// needed to sotre color data to swap_chain
|
||||
@@ -129,7 +136,7 @@ impl State {
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
/*
|
||||
// overwrite challenge shader file to shader
|
||||
let shader = device.create_shader_module(
|
||||
&wgpu::ShaderModuleDescriptor {
|
||||
@@ -194,6 +201,17 @@ impl State {
|
||||
}
|
||||
}
|
||||
);
|
||||
*/
|
||||
|
||||
let vertex_buffer = device.create_buffer_init(
|
||||
&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("Vertex Buffer"),
|
||||
contents: bytemuck::cast_slice(crate::vertex::VERTICES),
|
||||
usage: wgpu::BufferUsage::VERTEX,
|
||||
}
|
||||
);
|
||||
|
||||
let num_vertices = crate::vertex::VERTICES.len() as u32;
|
||||
|
||||
Self {
|
||||
surface,
|
||||
@@ -204,8 +222,10 @@ impl State {
|
||||
size,
|
||||
clear_color: wgpu::Color { r: 0.6, g: 0.6, b: 0.1, a: 1.0 },
|
||||
render_pipeline,
|
||||
challenge_render_pipeline,
|
||||
//challenge_render_pipeline,
|
||||
use_challenge_render_pipeline: false,
|
||||
vertex_buffer,
|
||||
num_vertices,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -277,13 +297,17 @@ impl State {
|
||||
});
|
||||
|
||||
// set pipeline
|
||||
/*
|
||||
if self.use_challenge_render_pipeline {
|
||||
render_pass.set_pipeline(&self.challenge_render_pipeline);
|
||||
} else {
|
||||
render_pass.set_pipeline(&self.render_pipeline);
|
||||
}
|
||||
*/
|
||||
render_pass.set_pipeline(&self.render_pipeline);
|
||||
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
|
||||
// draw triangle
|
||||
render_pass.draw(0..3, 0..1);
|
||||
render_pass.draw(0..self.num_vertices, 0..1);
|
||||
|
||||
// drop so encoder isn't borrowed mutually anymore
|
||||
drop(render_pass);
|
||||
|
||||
47
src/vertex.rs
Normal file
47
src/vertex.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
pub struct Vertex {
|
||||
position: [f32; 3],
|
||||
color: [f32; 3],
|
||||
}
|
||||
|
||||
impl Vertex {
|
||||
pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
|
||||
wgpu::VertexBufferLayout {
|
||||
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
|
||||
step_mode: wgpu::InputStepMode::Vertex,
|
||||
// this oculd be shortened with:
|
||||
// attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x3],
|
||||
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,
|
||||
},
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub const VERTICES: &[Vertex] = &[
|
||||
// Top
|
||||
Vertex {
|
||||
position: [0.0, 0.5, 0.0],
|
||||
color: [1.0, 0.0, 0.0],
|
||||
},
|
||||
// Left
|
||||
Vertex {
|
||||
position: [-0.5, -0.5, 0.0],
|
||||
color: [0.0, 1.0, 0.0],
|
||||
},
|
||||
// Right
|
||||
Vertex {
|
||||
position: [0.5, -0.5, 0.0],
|
||||
color: [0.0, 0.0, 1.0],
|
||||
},
|
||||
];
|
||||
Reference in New Issue
Block a user