-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create examples folder and dedicated builder crate
- Loading branch information
Showing
27 changed files
with
665 additions
and
576 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,24 @@ | ||
[package] | ||
name = "wrach" | ||
version = "0.0.0" | ||
authors = [] | ||
edition = "2018" | ||
resolver = "2" | ||
|
||
[features] | ||
default = ["use-compiled-tools"] | ||
use-installed-tools = ["spirv-builder/use-installed-tools"] | ||
use-compiled-tools = ["spirv-builder/use-compiled-tools"] | ||
|
||
[dependencies] | ||
shaders = { path = "shaders" } | ||
futures = { version = "0.3", default-features = false, features = ["std", "executor"] } | ||
wgpu = { version = "0.11.0", features = ["spirv"] } | ||
wgpu-hal = "=0.11.2" | ||
winit = { version = "0.25", features = ["web-sys"] } | ||
bytemuck = "1.7.2" | ||
crevice = { version = "0.8.0", features = ["glam"] } | ||
rand = "0.7.2" | ||
cgmath = "0.18.0" | ||
env_logger = "0.9.0" | ||
async-executor = "1.4.1" | ||
pollster = "0.2.4" | ||
log = "0.4.14" | ||
cfg-if = "1.0.0" | ||
|
||
|
||
[build-dependencies] | ||
spirv-std = { git = "https://github.com/EmbarkStudios/rust-gpu", rev = "0866cf59", features = [ "glam" ] } | ||
spirv-builder = { git = "https://github.com/EmbarkStudios/rust-gpu", rev = "0866cf59", default-features = false } | ||
|
||
[workspace] | ||
members = ["shaders"] | ||
|
||
# Compile build-dependencies in release mode with | ||
# the same settings as regular dependencies. | ||
resolver = "2" | ||
members = [ | ||
"shaders/rust-gpu-compiler", | ||
"shaders/physics", | ||
"shaders/renderer", | ||
"runners/wgpu", | ||
"examples/basic" | ||
] | ||
|
||
# Enable incremental by default in release mode. | ||
[profile.release] | ||
incremental = true | ||
# HACK(eddyb) this is the default but without explicitly specifying it, Cargo | ||
# will treat the identical settings in `[profile.release.build-override]` below | ||
# as different sets of `rustc` flags and will not reuse artifacts between them. | ||
codegen-units = 256 | ||
|
||
# Compile build-dependencies in release mode with the same settings | ||
# as regular dependencies (including the incremental enabled above). | ||
[profile.release.build-override] | ||
opt-level = 3 | ||
codegen-units = 16 | ||
|
||
# HACK(eddyb) also compile debug mode's build-dependencies with optimizations, | ||
# because otherwise `rustc_codegen_spirv` (esspecially its linker) is too slow. | ||
# Also `spirv-opt` *alone* takes (just) over an hour to run, though this only | ||
# brings it down only to 10 minutes, so I've disabled it below, for now. | ||
[profile.dev.build-override] | ||
opt-level = 3 | ||
|
||
# HACK(eddyb) don't optimize the shader crate, to avoid `spirv-opt` taking | ||
# a long time (10 minutes if itself was optimized, over an hour otherwise). | ||
[profile.release.package."shaders"] | ||
opt-level = 0 | ||
incremental = true | ||
codegen-units = 256 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "wrach-physics-example" | ||
version = "0.0.0" | ||
authors = [] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
wrach-physics-shaders = { path = "../../shaders/physics" } | ||
wrach-renderer-shader = { path = "../../shaders/renderer" } | ||
wrach-wgpu = { path = "../../runners/wgpu" } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
use wgpu::util::DeviceExt; | ||
use wrach_physics_shaders as physics; | ||
use wrach_wgpu::event_loop::event_loop::{EventLoop, Renderer}; | ||
use wrach_wgpu::pipeline::builder::Builder; | ||
use wrach_wgpu::{bytemuck, gpu_manager, wgpu}; | ||
|
||
struct SquareVertex { | ||
pipeline: wgpu::RenderPipeline, | ||
vertices: wgpu::Buffer, | ||
} | ||
|
||
impl SquareVertex { | ||
fn init_render_pipeline( | ||
shader_module: &wgpu::ShaderModule, | ||
manager: &gpu_manager::GPUManager, | ||
) -> wgpu::RenderPipeline { | ||
let render_pipeline_layout = | ||
manager | ||
.device | ||
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { | ||
label: Some("render"), | ||
bind_group_layouts: &[], | ||
push_constant_ranges: &[], | ||
}); | ||
|
||
let particle_array_stride = | ||
std::mem::size_of::<physics::particle::Std140ParticleBasic>() as u64; | ||
|
||
let fragment = wgpu::FragmentState { | ||
module: shader_module, | ||
entry_point: "main_fs", | ||
targets: &[manager.config.format.into()], | ||
}; | ||
|
||
let pipeline_descriptor = wgpu::RenderPipelineDescriptor { | ||
label: None, | ||
layout: Some(&render_pipeline_layout), | ||
vertex: wgpu::VertexState { | ||
module: shader_module, | ||
entry_point: "main_vs", | ||
buffers: &[ | ||
wgpu::VertexBufferLayout { | ||
array_stride: particle_array_stride, | ||
step_mode: wgpu::VertexStepMode::Instance, | ||
attributes: &wgpu::vertex_attr_array![ | ||
0 => Float32x4, 1 => Float32x2, 2 => Float32x2, 3 => Float32x2 | ||
], | ||
}, | ||
wgpu::VertexBufferLayout { | ||
array_stride: 1 * 8, | ||
step_mode: wgpu::VertexStepMode::Vertex, | ||
attributes: &wgpu::vertex_attr_array![4 => Float32x2], | ||
}, | ||
], | ||
}, | ||
fragment: Some(fragment), | ||
primitive: wgpu::PrimitiveState::default(), | ||
depth_stencil: None, | ||
multisample: wgpu::MultisampleState::default(), | ||
}; | ||
|
||
manager.device.create_render_pipeline(&pipeline_descriptor) | ||
} | ||
|
||
fn init_vertices_buffer(manager: &gpu_manager::GPUManager) -> wgpu::Buffer { | ||
// A square made of 1 triangles | ||
#[rustfmt::skip] | ||
let vertex_buffer_data: Vec<f32> = [ | ||
// First triangle ---------------------- | ||
-1, -1, -1, 1, 1, 1, | ||
// Second triangle ---------------------- | ||
-1, -1, 1, 1, 1, -1, | ||
] | ||
.iter() | ||
.map(|x| 0.5 * physics::particle::PIXEL_SIZE * (*x as f32)) | ||
.collect(); | ||
let mut square = [0.0; 12]; | ||
for i in 0..12 { | ||
square[i] = vertex_buffer_data[i]; | ||
} | ||
manager | ||
.device | ||
.create_buffer_init(&wgpu::util::BufferInitDescriptor { | ||
label: Some("Vertex Buffer"), | ||
contents: bytemuck::bytes_of(&square), | ||
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, | ||
}) | ||
} | ||
|
||
fn init_render_pass<'a>( | ||
command_encoder: &'a mut wgpu::CommandEncoder, | ||
view: &'a wgpu::TextureView, | ||
) -> wgpu::RenderPass<'a> { | ||
let color_attachments = [wgpu::RenderPassColorAttachment { | ||
view, | ||
resolve_target: None, | ||
ops: wgpu::Operations { | ||
load: wgpu::LoadOp::Clear(wgpu::Color::BLACK), | ||
store: true, | ||
}, | ||
}]; | ||
|
||
let render_pass_descriptor = wgpu::RenderPassDescriptor { | ||
label: None, | ||
color_attachments: &color_attachments, | ||
depth_stencil_attachment: None, | ||
}; | ||
|
||
command_encoder.begin_render_pass(&render_pass_descriptor) | ||
} | ||
} | ||
|
||
impl Renderer for SquareVertex { | ||
fn new(manager: &gpu_manager::GPUManager) -> Self { | ||
let shader_module = Builder::shader(&manager.device, "shaders/renderer"); | ||
Self { | ||
pipeline: Self::init_render_pipeline(&shader_module, manager), | ||
vertices: Self::init_vertices_buffer(manager), | ||
} | ||
} | ||
|
||
fn render_pass<'a, T: Renderer>( | ||
&self, | ||
event_loop: &mut EventLoop<'_, T>, | ||
command_encoder: &'a mut wgpu::CommandEncoder, | ||
view: &'a wgpu::TextureView, | ||
) { | ||
let index = event_loop.bind_group_index_toggled(); | ||
let particle_buffer = event_loop.manager.pipeline.particle_buffers[index].slice(..); | ||
|
||
command_encoder.push_debug_group("render pixels"); | ||
{ | ||
let mut rpass = Self::init_render_pass(command_encoder, view); | ||
rpass.set_pipeline(&self.pipeline); | ||
rpass.set_vertex_buffer(0, particle_buffer); | ||
rpass.set_vertex_buffer(1, self.vertices.slice(..)); | ||
rpass.draw(0..6, 0..physics::world::NUM_PARTICLES as u32); | ||
} | ||
command_encoder.pop_debug_group(); | ||
} | ||
} | ||
|
||
fn main() { | ||
EventLoop::<SquareVertex>::start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[package] | ||
name = "wrach-wgpu" | ||
version = "0.0.0" | ||
authors = [] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
wrach-physics-shaders = { path = "../../shaders/physics" } | ||
rust-gpu-compiler = { path = "../../shaders/rust-gpu-compiler" } | ||
futures = { version = "0.3", default-features = false, features = ["std", "executor"] } | ||
wgpu = { version = "0.11.0", features = ["spirv"] } | ||
wgpu-hal = "=0.11.2" | ||
winit = { version = "0.25", features = ["web-sys"] } | ||
bytemuck = "1.7.2" | ||
crevice = { version = "0.8.0", features = ["glam"] } | ||
rand = "0.7.2" | ||
cgmath = "0.18.0" | ||
env_logger = "0.9.0" | ||
async-executor = "1.4.1" | ||
pollster = "0.2.4" | ||
log = "0.4.14" | ||
cfg-if = "1.0.0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
pub use bytemuck; | ||
pub use wgpu; | ||
|
||
pub mod event_loop; | ||
pub mod gpu_manager; | ||
pub mod pipeline; |
Oops, something went wrong.