Skip to content

Commit

Permalink
Glam vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Dec 31, 2023
1 parent 158d8d6 commit d235196
Show file tree
Hide file tree
Showing 12 changed files with 323 additions and 92 deletions.
6 changes: 4 additions & 2 deletions dunge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ rust-version = "1.71"
dunge_macros = { version = "0.2.3", path = "../dunge_macros" }
dunge_shader = { version = "0.2.3", path = "../dunge_shader" }
bytemuck = { version = "1.13", features = ["derive"] }
glam = "0.25"
instant = "0.1"
log = "0.4"
wgpu = { version = "0.18", default-features = false, features = ["naga"] }

[dependencies.winit]
version = "0.29"
default-features = false
features = ["rwh_05", "x11", "wayland"]
features = ["rwh_05", "x11"]

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"] }
Expand All @@ -37,5 +36,8 @@ version = "0.29"
default-features = false
features = ["android-native-activity"]

[dev-dependencies]
futures = { version = "2.1", package = "futures-lite" }

[lints]
workspace = true
19 changes: 12 additions & 7 deletions dunge/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use {
crate::{
draw::Draw,
eval::IntoModule,
group::{self, Binder, Group, GroupHandler, UniqueGroupBinding, Update},
layer::Layer,
mesh::{self, Mesh},
shader::Shader,
sl::IntoModule,
state::{Render, State},
texture::{
self, CopyBuffer, CopyBufferView, DrawTexture, Format, Make, MapResult, Mapped, Sampler,
Expand All @@ -19,12 +19,10 @@ use {
pub struct Context(Arc<State>);

impl Context {
pub async fn new() -> Self {
use wgpu::Instance;

let instance = Instance::default();
let state = State::new(&instance).await;
Self(Arc::new(state))
pub async fn new() -> Result<Self, Error> {
let instance = wgpu::Instance::default();
let state = State::new(&instance).await?;
Ok(Self(Arc::new(state)))
}

pub fn make_shader<M, A>(&self, module: M) -> Shader<M::Vertex>
Expand Down Expand Up @@ -95,3 +93,10 @@ impl Context {
group::update(&self.0, uni, handler, group)
}
}

/// An error returned from the [`Context`] constructor.
#[derive(Debug)]
pub enum Error {
BackendSelection,
RequestDevice,
}
2 changes: 1 addition & 1 deletion dunge/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl<'a> BindTexture<'a> {
}
}

fn visit<'a, G>(group: &'a G) -> Vec<BindGroupEntry<'a>>
fn visit<G>(group: &G) -> Vec<BindGroupEntry>
where
G: Group,
{
Expand Down
2 changes: 1 addition & 1 deletion dunge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ pub mod texture;
#[allow(dead_code)]
mod time;

pub use dunge_shader::{eval, types, vertex};
pub use dunge_shader::{sl, vertex};
3 changes: 1 addition & 2 deletions dunge/src/shader.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use {
crate::{
eval::{InputInfo, IntoModule, Module, Stages},
group::TypedGroup,
sl::{GroupMemberType, InputInfo, IntoModule, Module, Stages, VectorType},
state::State,
types::{GroupMemberType, VectorType},
},
std::marker::PhantomData,
wgpu::{PipelineLayout, ShaderModule, VertexAttribute, VertexBufferLayout},
Expand Down
33 changes: 19 additions & 14 deletions dunge/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use {
crate::{
color::Rgba,
context::Error,
draw::Draw,
layer::{Layer, SetLayer},
texture::{CopyBuffer, CopyTexture, Format, Texture},
Expand All @@ -17,39 +18,45 @@ pub(crate) struct State {
}

impl State {
pub async fn new(instance: &Instance) -> Self {
use wgpu::*;

pub async fn new(instance: &Instance) -> Result<Self, Error> {
let adapter = {
use wgpu::{PowerPreference, RequestAdapterOptions};

let options = RequestAdapterOptions {
power_preference: PowerPreference::HighPerformance,
compatible_surface: None,
force_fallback_adapter: false,
..Default::default()
};

instance.request_adapter(&options).await.unwrap()
instance
.request_adapter(&options)
.await
.ok_or(Error::BackendSelection)?
};

let (device, queue) = {
use wgpu::{DeviceDescriptor, Limits};

let desc = DeviceDescriptor {
label: None,
features: Features::empty(),
limits: if cfg!(target_arch = "wasm32") {
Limits::downlevel_webgl2_defaults()
} else {
Limits::default()
},
..Default::default()
};

adapter.request_device(&desc, None).await.unwrap()
adapter
.request_device(&desc, None)
.await
.map_err(|_| Error::RequestDevice)?
};

Self {
Ok(Self {
adapter,
device,
queue,
shader_ids: AtomicUsize::default(),
}
})
}

#[allow(dead_code)]
Expand All @@ -66,9 +73,7 @@ impl State {
}

pub fn next_shader_id(&self) -> usize {
use atomic::Ordering;

self.shader_ids.fetch_add(1, Ordering::Relaxed)
self.shader_ids.fetch_add(1, atomic::Ordering::Relaxed)
}

pub fn draw<V, D>(&self, render: &mut Render, view: &V, draw: D)
Expand Down
7 changes: 7 additions & 0 deletions dunge/tests/triangle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[test]
fn render() {
use {dunge::context::Context, futures::future};

let cx = future::block_on(Context::new()).expect("create context");
_ = cx;
}
1 change: 1 addition & 0 deletions dunge_shader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ documentation = "https://docs.rs/dunge"
repository = "https://github.com/nanoqsh/dunge"

[dependencies]
glam = "0.25"
naga = "0.14"

[lints]
Expand Down
Loading

0 comments on commit d235196

Please sign in to comment.