From 9e50691d4bfe1e68eec3df9bfb40473e218983e4 Mon Sep 17 00:00:00 2001 From: JoshuaBatty Date: Tue, 14 Jul 2020 18:08:52 +0200 Subject: [PATCH 1/2] fixes m_1_3_02 and m_1_3_03 gnerative examples --- .../random_and_noise/m_1_3_02.rs | 17 +++++---------- .../random_and_noise/m_1_3_03.rs | 21 +++++++------------ 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/generative_design/random_and_noise/m_1_3_02.rs b/generative_design/random_and_noise/m_1_3_02.rs index 12314d0fe..4035b3f54 100644 --- a/generative_design/random_and_noise/m_1_3_02.rs +++ b/generative_design/random_and_noise/m_1_3_02.rs @@ -54,7 +54,7 @@ fn model(app: &App) -> Model { let win = window.rect(); let texture = wgpu::TextureBuilder::new() .size([win.w() as u32, win.h() as u32]) - .format(Frame::TEXTURE_FORMAT) + .format(wgpu::TextureFormat::Rgba8Unorm) .usage(wgpu::TextureUsage::COPY_DST | wgpu::TextureUsage::SAMPLED) .build(window.swap_chain_device()); Model { @@ -70,31 +70,24 @@ fn view(app: &App, model: &Model, frame: Frame) { let mut rng = SmallRng::seed_from_u64(model.act_random_seed); let image = image::ImageBuffer::from_fn(win.w() as u32, win.h() as u32, |_x, _y| { - let r: u16 = rng.gen_range(0, std::u16::MAX); - nannou::image::Rgba([r, r, r, std::u16::MAX]) + let r: u8 = rng.gen_range(0, std::u8::MAX); + nannou::image::Rgba([r, r, r, std::u8::MAX]) }); let flat_samples = image.as_flat_samples(); - let img_bytes = slice_as_bytes(flat_samples.as_slice()); model.texture.upload_data( app.main_window().swap_chain_device(), &mut *frame.command_encoder(), - img_bytes, + &flat_samples.as_slice(), ); - let draw = app.draw(); + let draw = app.draw(); draw.texture(&model.texture); // Write to the window frame. draw.to_frame(app, &frame).unwrap(); } -fn slice_as_bytes(s: &[u16]) -> &[u8] { - let len = s.len() * std::mem::size_of::(); - let ptr = s.as_ptr() as *const u8; - unsafe { std::slice::from_raw_parts(ptr, len) } -} - fn mouse_pressed(_app: &App, model: &mut Model, _button: MouseButton) { model.act_random_seed = (random_f32() * 100000.0) as u64; } diff --git a/generative_design/random_and_noise/m_1_3_03.rs b/generative_design/random_and_noise/m_1_3_03.rs index 6b352434c..e284c08ff 100644 --- a/generative_design/random_and_noise/m_1_3_03.rs +++ b/generative_design/random_and_noise/m_1_3_03.rs @@ -61,7 +61,7 @@ fn model(app: &App) -> Model { let win = window.rect(); let texture = wgpu::TextureBuilder::new() .size([win.w() as u32, win.h() as u32]) - .format(Frame::TEXTURE_FORMAT) + .format(wgpu::TextureFormat::Rgba8Unorm) .usage(wgpu::TextureUsage::COPY_DST | wgpu::TextureUsage::SAMPLED) .build(window.swap_chain_device()); Model { @@ -96,7 +96,7 @@ fn view(app: &App, model: &Model, frame: Frame) { 1.0, -1.0, 0.0, - std::u16::MAX as f64, + std::u8::MAX as f64, ); } else if model.noise_mode == 2 { let n = map_range( @@ -104,20 +104,19 @@ fn view(app: &App, model: &Model, frame: Frame) { -1.0, 1.0, 0.0, - std::u16::MAX as f64 / 10.0, + std::u8::MAX as f64 / 10.0, ); - noise_value = (n - n.floor()) * std::u16::MAX as f64; + noise_value = (n - n.floor()) * std::u8::MAX as f64; } - let n = noise_value as u16; - nannou::image::Rgba([n, n, n, std::u16::MAX]) + let n = noise_value as u8; + nannou::image::Rgba([n, n, n, std::u8::MAX]) }); let flat_samples = image.as_flat_samples(); - let img_bytes = slice_as_bytes(flat_samples.as_slice()); model.texture.upload_data( app.main_window().swap_chain_device(), &mut *frame.command_encoder(), - img_bytes, + &flat_samples.as_slice(), ); let draw = app.draw(); @@ -127,12 +126,6 @@ fn view(app: &App, model: &Model, frame: Frame) { draw.to_frame(app, &frame).unwrap(); } -fn slice_as_bytes(s: &[u16]) -> &[u8] { - let len = s.len() * std::mem::size_of::(); - let ptr = s.as_ptr() as *const u8; - unsafe { std::slice::from_raw_parts(ptr, len) } -} - fn key_released(app: &App, model: &mut Model, key: Key) { match key { Key::S => { From cb0020250c13ce00b0e1d0524f8689e7a1e675de Mon Sep 17 00:00:00 2001 From: JoshuaBatty Date: Tue, 14 Jul 2020 18:17:46 +0200 Subject: [PATCH 2/2] cargo fmt --- generative_design/random_and_noise/m_1_3_02.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generative_design/random_and_noise/m_1_3_02.rs b/generative_design/random_and_noise/m_1_3_02.rs index 4035b3f54..dae08cc3e 100644 --- a/generative_design/random_and_noise/m_1_3_02.rs +++ b/generative_design/random_and_noise/m_1_3_02.rs @@ -81,7 +81,7 @@ fn view(app: &App, model: &Model, frame: Frame) { &flat_samples.as_slice(), ); - let draw = app.draw(); + let draw = app.draw(); draw.texture(&model.texture); // Write to the window frame.