Skip to content

Commit

Permalink
Use inline const
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Jul 4, 2024
1 parent 4c5589b commit 578e1c0
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 189 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ Also create a triangle mesh that we're going to draw:
```rust
// Create a mesh from vertices
let mesh = {
const VERTS: MeshData<'static, Vert> = MeshData::from_verts(&[
Vert { pos: [-0.5, -0.5], col: [1., 0., 0.] },
Vert { pos: [ 0.5, -0.5], col: [0., 1., 0.] },
Vert { pos: [ 0. , 0.5], col: [0., 0., 1.] },
]);

cx.make_mesh(&VERTS)
let data = const {
MeshData::from_verts(&[
Vert { pos: [-0.5, -0.5], col: [1., 0., 0.] },
Vert { pos: [ 0.5, -0.5], col: [0., 1., 0.] },
Vert { pos: [ 0. , 0.5], col: [0., 0., 1.] },
])
};

cx.make_mesh(&data)
};
```

Expand Down
13 changes: 8 additions & 5 deletions dunge/src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,17 @@ impl CopyBuffer {
pub(crate) fn new(state: &State, (width, height): (u32, u32)) -> Self {
use wgpu::*;

const PIXEL_SIZE: u32 = mem::size_of::<u32>() as u32;
const ALIGNMENT: u32 = wgpu::COPY_BYTES_PER_ROW_ALIGNMENT / PIXEL_SIZE;
let (pixel_size, alignment) = const {
let pixel_size = mem::size_of::<u32>() as u32;
let alignment = wgpu::COPY_BYTES_PER_ROW_ALIGNMENT / pixel_size;
(pixel_size, alignment)
};

let actual_width = util::align_to(width, ALIGNMENT);
let actual_width = util::align_to(width, alignment);
let buf = {
let desc = BufferDescriptor {
label: None,
size: BufferAddress::from(actual_width * height * PIXEL_SIZE),
size: BufferAddress::from(actual_width * height * pixel_size),
usage: BufferUsages::MAP_READ | BufferUsages::COPY_DST,
mapped_at_creation: false,
};
Expand All @@ -246,7 +249,7 @@ impl CopyBuffer {
Self {
buf,
size: (actual_width, height),
pixel_size: PIXEL_SIZE,
pixel_size,
}
}

Expand Down
4 changes: 1 addition & 3 deletions dunge/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ pub(crate) struct Fps {

impl Fps {
pub fn count(&mut self, delta_time: Duration) -> Option<u32> {
const SECOND: Duration = Duration::from_secs(1);

self.timer += delta_time;
self.counter += 1;
if self.timer > SECOND {
if self.timer > const { Duration::from_secs(1) } {
self.timer = Duration::ZERO;
let n = self.counter;
self.counter = 0;
Expand Down
16 changes: 9 additions & 7 deletions dunge/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,18 +254,20 @@ impl Inner {
fn new(state: &State, window: window::Window) -> Result<Self, Error> {
use wgpu::*;

const SUPPORTED_FORMATS: [Format; 4] = [
Format::SrgbAlpha,
Format::SbgrAlpha,
Format::RgbAlpha,
Format::BgrAlpha,
];
let supported_formats = const {
[
Format::SrgbAlpha,
Format::SbgrAlpha,
Format::RgbAlpha,
Format::BgrAlpha,
]
};

let window = Arc::new(window);
let surface = state.instance().create_surface(Arc::clone(&window))?;
let conf = {
let caps = surface.get_capabilities(state.adapter());
let format = SUPPORTED_FORMATS.into_iter().find_map(|format| {
let format = supported_formats.into_iter().find_map(|format| {
let format = format.wgpu();
caps.formats.contains(&format).then_some(format)
});
Expand Down
42 changes: 21 additions & 21 deletions dunge/tests/triangle_discard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ fn render() -> Result<(), Error> {
std::fs,
};

const SIZE: (u32, u32) = (300, 300);

#[repr(C)]
#[derive(Vertex)]
struct Vert {
Expand Down Expand Up @@ -66,36 +64,38 @@ fn render() -> Result<(), Error> {
binder.into_binding()
};

let size = const { (300, 300) };
let layer = cx.make_layer(&shader, Format::SrgbAlpha);
let view = {
let data = TextureData::empty(SIZE, Format::SrgbAlpha)?
let data = TextureData::empty(size, Format::SrgbAlpha)?
.with_draw()
.with_copy();

cx.make_texture(data)
};

let mesh = {
const VERTS: [Vert; 3] = [
Vert {
pos: [0., -0.75],
tex: [0., 1.],
},
Vert {
pos: [0.866, 0.75],
tex: [1., 1.],
},
Vert {
pos: [-0.866, 0.75],
tex: [1., 0.],
},
];

let data = MeshData::from_verts(&VERTS);
let data = const {
MeshData::from_verts(&[
Vert {
pos: [0., -0.75],
tex: [0., 1.],
},
Vert {
pos: [0.866, 0.75],
tex: [1., 1.],
},
Vert {
pos: [-0.866, 0.75],
tex: [1., 0.],
},
])
};

cx.make_mesh(&data)
};

let buffer = cx.make_copy_buffer(SIZE);
let buffer = cx.make_copy_buffer(size);
let opts = Rgba::from_standard([1., 0., 0., 1.]);
let draw = dunge::draw(|mut frame| {
frame.layer(&layer, opts).bind(&map).draw(&mesh);
Expand All @@ -109,7 +109,7 @@ fn render() -> Result<(), Error> {
});

let data = mapped.data();
let image = Image::from_fn(SIZE, |x, y| {
let image = Image::from_fn(size, |x, y| {
let (width, _) = buffer.size();
let idx = x + y * width;
data[idx as usize]
Expand Down
42 changes: 21 additions & 21 deletions dunge/tests/triangle_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ fn render() -> Result<(), Error> {
std::fs,
};

const SIZE: (u32, u32) = (300, 300);

#[repr(C)]
#[derive(Vertex)]
struct Vert {
Expand Down Expand Up @@ -62,36 +60,38 @@ fn render() -> Result<(), Error> {
binder.into_binding()
};

let size = const { (300, 300) };
let layer = cx.make_layer(&shader, Format::SrgbAlpha);
let view = {
let data = TextureData::empty(SIZE, Format::SrgbAlpha)?
let data = TextureData::empty(size, Format::SrgbAlpha)?
.with_draw()
.with_copy();

cx.make_texture(data)
};

let mesh = {
const VERTS: [Vert; 3] = [
Vert {
pos: [0., -0.75],
tex: [0., 1.],
},
Vert {
pos: [0.866, 0.75],
tex: [1., 1.],
},
Vert {
pos: [-0.866, 0.75],
tex: [1., 0.],
},
];

let data = MeshData::from_verts(&VERTS);
let data = const {
MeshData::from_verts(&[
Vert {
pos: [0., -0.75],
tex: [0., 1.],
},
Vert {
pos: [0.866, 0.75],
tex: [1., 1.],
},
Vert {
pos: [-0.866, 0.75],
tex: [1., 0.],
},
])
};

cx.make_mesh(&data)
};

let buffer = cx.make_copy_buffer(SIZE);
let buffer = cx.make_copy_buffer(size);
let opts = Rgba::from_standard([0., 0., 0., 1.]);
let draw = dunge::draw(|mut frame| {
frame.layer(&layer, opts).bind(&map).draw(&mesh);
Expand All @@ -105,7 +105,7 @@ fn render() -> Result<(), Error> {
});

let data = mapped.data();
let image = Image::from_fn(SIZE, |x, y| {
let image = Image::from_fn(size, |x, y| {
let (width, _) = buffer.size();
let idx = x + y * width;
data[idx as usize]
Expand Down
24 changes: 12 additions & 12 deletions dunge/tests/triangle_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,34 @@ fn render() -> Result<(), Error> {
std::{f32::consts, fs},
};

const SIZE: (u32, u32) = (300, 300);
const COLOR: Vec4 = Vec4::new(1., 0., 0., 1.);
const THIRD: f32 = consts::TAU / 3.;
const R_OFFSET: f32 = -consts::TAU / 4.;
const Y_OFFSET: f32 = 0.25;

let triangle = |Index(index): Index| {
let i = sl::thunk(sl::f32(index) * THIRD + R_OFFSET);
let color = const { Vec4::new(1., 0., 0., 1.) };
let third = const { consts::TAU / 3. };
let r_offset = const { -consts::TAU / 4. };
let y_offset = 0.25;

let i = sl::thunk(sl::f32(index) * third + r_offset);
Out {
place: sl::vec4(sl::cos(i.clone()), sl::sin(i) + Y_OFFSET, 0., 1.),
color: COLOR,
place: sl::vec4(sl::cos(i.clone()), sl::sin(i) + y_offset, 0., 1.),
color,
}
};

let cx = helpers::block_on(dunge::context())?;
let shader = cx.make_shader(triangle);
helpers::eq_lines(shader.debug_wgsl(), include_str!("triangle_index.wgsl"));

let size = const { (300, 300) };
let layer = cx.make_layer(&shader, Format::SrgbAlpha);
let view = {
let data = TextureData::empty(SIZE, Format::SrgbAlpha)?
let data = TextureData::empty(size, Format::SrgbAlpha)?
.with_draw()
.with_copy();

cx.make_texture(data)
};

let buffer = cx.make_copy_buffer(SIZE);
let buffer = cx.make_copy_buffer(size);
let opts = Rgba::from_standard([0., 0., 0., 1.]);
let draw = dunge::draw(|mut frame| {
frame.layer(&layer, opts).bind_empty().draw_points(3);
Expand All @@ -57,7 +57,7 @@ fn render() -> Result<(), Error> {
});

let data = mapped.data();
let image = Image::from_fn(SIZE, |x, y| {
let image = Image::from_fn(size, |x, y| {
let (width, _) = buffer.size();
let idx = x + y * width;
data[idx as usize]
Expand Down
26 changes: 13 additions & 13 deletions dunge/tests/triangle_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@ fn render() -> Result<(), Error> {
std::{f32::consts, fs},
};

const SIZE: (u32, u32) = (300, 300);
const TRIANGLE_SIZE: f32 = 0.4;
const THIRD: f32 = consts::TAU / 3.;
const R_OFFSET: f32 = -consts::TAU / 4.;

#[derive(Instance)]
struct Transform(Row<[f32; 2]>, Row<[f32; 3]>);

let triangle = |t: InInstance<Transform>, Index(index): Index| {
let i = sl::thunk(sl::f32(index) * THIRD + R_OFFSET);
let p = sl::vec2(sl::cos(i.clone()), sl::sin(i)) * TRIANGLE_SIZE + t.0;
let triangle_size = 0.4;
let third = const { consts::TAU / 3. };
let r_offset = const { -consts::TAU / 4. };

let i = sl::thunk(sl::f32(index) * third + r_offset);
let p = sl::vec2(sl::cos(i.clone()), sl::sin(i)) * triangle_size + t.0;
Out {
place: sl::vec4_concat(p, Vec2::new(0., 1.)),
color: sl::vec4_with(sl::fragment(t.1), 1.),
Expand All @@ -38,23 +37,24 @@ fn render() -> Result<(), Error> {
let shader = cx.make_shader(triangle);
helpers::eq_lines(shader.debug_wgsl(), include_str!("triangle_instance.wgsl"));

let size = const { (300, 300) };
let layer = cx.make_layer(&shader, Format::SrgbAlpha);
let view = {
let data = TextureData::empty(SIZE, Format::SrgbAlpha)?
let data = TextureData::empty(size, Format::SrgbAlpha)?
.with_draw()
.with_copy();

cx.make_texture(data)
};

let transform = {
const POS: [[f32; 2]; 3] = [[0.0, -0.375], [0.433, 0.375], [-0.433, 0.375]];
const COL: [[f32; 3]; 3] = [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]];
let pos = const { [[0.0, -0.375], [0.433, 0.375], [-0.433, 0.375]] };
let col = const { [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]] };

Transform(cx.make_row(&POS), cx.make_row(&COL))
Transform(cx.make_row(&pos), cx.make_row(&col))
};

let buffer = cx.make_copy_buffer(SIZE);
let buffer = cx.make_copy_buffer(size);
let opts = Rgba::from_standard([0., 0., 0., 1.]);
let draw = dunge::draw(|mut frame| {
frame
Expand All @@ -73,7 +73,7 @@ fn render() -> Result<(), Error> {
});

let data = mapped.data();
let image = Image::from_fn(SIZE, |x, y| {
let image = Image::from_fn(size, |x, y| {
let (width, _) = buffer.size();
let idx = x + y * width;
data[idx as usize]
Expand Down
Loading

0 comments on commit 578e1c0

Please sign in to comment.