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"
|
version = "1.7.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "72957246c41db82b8ef88a5486143830adeb8227ef9837740bdec67724cf2c5b"
|
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]]
|
[[package]]
|
||||||
name = "byteorder"
|
name = "byteorder"
|
||||||
@@ -1902,6 +1916,7 @@ dependencies = [
|
|||||||
name = "wgpu-rs-learning"
|
name = "wgpu-rs-learning"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"bytemuck",
|
||||||
"cgmath",
|
"cgmath",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
"image",
|
"image",
|
||||||
|
|||||||
@@ -13,3 +13,4 @@ env_logger = "0.9"
|
|||||||
log = "0.4"
|
log = "0.4"
|
||||||
wgpu = "0.9"
|
wgpu = "0.9"
|
||||||
pollster = "0.2"
|
pollster = "0.2"
|
||||||
|
bytemuck = { version = "1.4", features = [ "derive" ] }
|
||||||
|
|||||||
@@ -1,27 +1,23 @@
|
|||||||
// Vertex shader
|
// Vertex shader
|
||||||
|
|
||||||
struct VertexOutput {
|
struct VertexOutput {
|
||||||
[[builtin(position)]] clip_coordinates: vec4<f32>;
|
[[builtin(position)]] clip_coordinates: vec4<f32>;
|
||||||
[[location(0)]] position: vec2<f32>;
|
[[location(0)]] position: vec2<f32>;
|
||||||
};
|
};
|
||||||
|
|
||||||
[[stage(vertex)]]
|
[[stage(vertex)]]
|
||||||
fn main(
|
fn main([[builtin(vertex_index)]] in_vertex_index: u32) -> VertexOutput {
|
||||||
[[builtin(vertex_index)]] in_vertex_index: u32,
|
var out: VertexOutput;
|
||||||
) -> VertexOutput {
|
let x = f32(1 - i32(in_vertex_index)) * 0.5;
|
||||||
var out: VertexOutput;
|
let y = f32(i32(in_vertex_index & 1u) * 2 - 1) * 0.5;
|
||||||
let x = f32(1 - i32(in_vertex_index)) * 0.5;
|
out.clip_coordinates = vec4<f32>(x, y, 0.0, 1.0);
|
||||||
let y = f32(i32(in_vertex_index & 1u) * 2 - 1) * 0.5;
|
out.position = vec2<f32>(x, y);
|
||||||
out.clip_coordinates = vec4<f32>(x, y, 0.0, 1.0);
|
return out;
|
||||||
out.position = vec2<f32>(x, y);
|
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fragment shader
|
// Fragment shader
|
||||||
|
|
||||||
[[stage(fragment)]]
|
[[stage(fragment)]]
|
||||||
fn main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
|
fn main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
|
||||||
let r = in.position.x;
|
let r = in.position.x;
|
||||||
let g = in.position.y;
|
let g = in.position.y;
|
||||||
return vec4<f32>(r, g, 0.1, 1.0);
|
return vec4<f32>(r, g, 0.1, 1.0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ use winit::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
mod state;
|
mod state;
|
||||||
|
mod vertex;
|
||||||
|
|
||||||
use crate::state::State;
|
use crate::state::State;
|
||||||
|
|
||||||
|
|||||||
@@ -1,23 +1,24 @@
|
|||||||
// Vertex shader
|
// Vertex shader
|
||||||
|
struct VertexInput {
|
||||||
|
[[location(0)]] position: vec3<f32>;
|
||||||
|
[[location(1)]] color: vec3<f32>;
|
||||||
|
};
|
||||||
|
|
||||||
struct VertexOutput {
|
struct VertexOutput {
|
||||||
[[builtin(position)]] clip_position: vec4<f32>;
|
[[builtin(position)]] clip_coordinate: vec4<f32>;
|
||||||
|
[[location(0)]] color: vec3<f32>;
|
||||||
};
|
};
|
||||||
|
|
||||||
[[stage(vertex)]]
|
[[stage(vertex)]]
|
||||||
fn main(
|
fn main(model: VertexInput) -> VertexOutput {
|
||||||
[[builtin(vertex_index)]] in_vertex_index: u32,
|
var out: VertexOutput;
|
||||||
) -> VertexOutput {
|
out.clip_coordinate = vec4<f32>(model.position, 1.0);
|
||||||
var out: VertexOutput;
|
out.color = model.color;
|
||||||
let x = f32(1 - i32(in_vertex_index)) * 0.5;
|
return out;
|
||||||
let y = f32(i32(in_vertex_index & 1u) * 2 - 1) * 0.5;
|
|
||||||
out.clip_position = vec4<f32>(x, y, 0.0, 1.0);
|
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fragment shader
|
// Fragment shader
|
||||||
|
|
||||||
[[stage(fragment)]]
|
[[stage(fragment)]]
|
||||||
fn main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
40
src/state.rs
40
src/state.rs
@@ -1,9 +1,14 @@
|
|||||||
use wgpu::PrimitiveTopology;
|
use wgpu::{
|
||||||
|
PrimitiveTopology,
|
||||||
|
util::DeviceExt,
|
||||||
|
};
|
||||||
use winit::{
|
use winit::{
|
||||||
event::*,
|
event::*,
|
||||||
window::Window,
|
window::Window,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::vertex::Vertex;
|
||||||
|
|
||||||
/// Hold state with important information
|
/// Hold state with important information
|
||||||
pub struct State {
|
pub struct State {
|
||||||
surface: wgpu::Surface,
|
surface: wgpu::Surface,
|
||||||
@@ -14,8 +19,10 @@ pub struct State {
|
|||||||
pub size: winit::dpi::PhysicalSize<u32>,
|
pub size: winit::dpi::PhysicalSize<u32>,
|
||||||
clear_color: wgpu::Color,
|
clear_color: wgpu::Color,
|
||||||
render_pipeline: wgpu::RenderPipeline,
|
render_pipeline: wgpu::RenderPipeline,
|
||||||
challenge_render_pipeline: wgpu::RenderPipeline,
|
//challenge_render_pipeline: wgpu::RenderPipeline,
|
||||||
use_challenge_render_pipeline: bool,
|
use_challenge_render_pipeline: bool,
|
||||||
|
vertex_buffer: wgpu::Buffer,
|
||||||
|
num_vertices: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
@@ -93,8 +100,8 @@ impl State {
|
|||||||
module: &shader,
|
module: &shader,
|
||||||
// function name in shader.wgsl for [[stage(vertex)]]
|
// function name in shader.wgsl for [[stage(vertex)]]
|
||||||
entry_point: "main",
|
entry_point: "main",
|
||||||
// already specified in the shader
|
// specify memory layout
|
||||||
buffers: &[],
|
buffers: &[Vertex::desc()],
|
||||||
|
|
||||||
},
|
},
|
||||||
// needed to sotre color data to swap_chain
|
// needed to sotre color data to swap_chain
|
||||||
@@ -129,7 +136,7 @@ impl State {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
/*
|
||||||
// overwrite challenge shader file to shader
|
// overwrite challenge shader file to shader
|
||||||
let shader = device.create_shader_module(
|
let shader = device.create_shader_module(
|
||||||
&wgpu::ShaderModuleDescriptor {
|
&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 {
|
Self {
|
||||||
surface,
|
surface,
|
||||||
@@ -204,8 +222,10 @@ impl State {
|
|||||||
size,
|
size,
|
||||||
clear_color: wgpu::Color { r: 0.6, g: 0.6, b: 0.1, a: 1.0 },
|
clear_color: wgpu::Color { r: 0.6, g: 0.6, b: 0.1, a: 1.0 },
|
||||||
render_pipeline,
|
render_pipeline,
|
||||||
challenge_render_pipeline,
|
//challenge_render_pipeline,
|
||||||
use_challenge_render_pipeline: false,
|
use_challenge_render_pipeline: false,
|
||||||
|
vertex_buffer,
|
||||||
|
num_vertices,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -277,13 +297,17 @@ impl State {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// set pipeline
|
// set pipeline
|
||||||
|
/*
|
||||||
if self.use_challenge_render_pipeline {
|
if self.use_challenge_render_pipeline {
|
||||||
render_pass.set_pipeline(&self.challenge_render_pipeline);
|
render_pass.set_pipeline(&self.challenge_render_pipeline);
|
||||||
} else {
|
} else {
|
||||||
render_pass.set_pipeline(&self.render_pipeline);
|
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
|
// 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 so encoder isn't borrowed mutually anymore
|
||||||
drop(render_pass);
|
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