Browse Source

initialized the draw buffer with the CPU

main
Thomas Johnson 7 months ago
parent
commit
f967fc39cd
  1. 1
      Cargo.toml
  2. 51
      src/render_loop.rs

1
Cargo.toml

@ -9,3 +9,4 @@ nalgebra = "0.30.1"
winit = "0.26"
wgpu = "0.12"
pollster = "0.2"
image = "0.24"

51
src/render_loop.rs

@ -1,6 +1,7 @@
use winit::{event_loop::{EventLoop, ControlFlow}, window::WindowBuilder, window::Window, event::{Event, WindowEvent, DeviceEvent}, dpi::{Size, PhysicalSize}};
use wgpu::{Instance, Surface, Backends, Features, Adapter, DeviceDescriptor, Device, Queue, Texture, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, Extent3d, CommandEncoder, CommandEncoderDescriptor, CommandBuffer, ImageCopyTexture, Origin3d, TextureAspect, SurfaceConfiguration, SurfaceTexture, PresentMode};
use wgpu::{Instance, Surface, Backends, Features, Adapter, DeviceDescriptor, Device, Queue, Texture, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, Extent3d, CommandEncoder, CommandEncoderDescriptor, CommandBuffer, ImageCopyTexture, Origin3d, TextureAspect, SurfaceConfiguration, SurfaceTexture, PresentMode, ImageDataLayout};
use image::{flat::{FlatSamples, SampleLayout}, ColorType};
const DEFAULT_DIMS: [u32; 2] = [100, 100];
@ -12,6 +13,7 @@ pub struct RenderLoop {
device: Device,
queue: Queue,
texture_buffer: Texture,
cpu_image: FlatSamples<Vec<u8>>,
}
impl RenderLoop {
@ -78,6 +80,19 @@ impl RenderLoop {
height: win_size.height,
present_mode: PresentMode::Mailbox,
});
let cpu_image = FlatSamples {
samples: vec![64u8; win_size.width as usize * win_size.height as usize * 4],
layout: SampleLayout {
channels: 4,
channel_stride: 1,
width: win_size.width,
width_stride: 4,
height: win_size.height,
height_stride: win_size.width as usize * 4,
},
color_hint: Some(ColorType::Rgba8),
};
Some((event_loop, RenderLoop {
window,
instance,
@ -86,6 +101,7 @@ impl RenderLoop {
device,
queue,
texture_buffer: texture,
cpu_image,
})
)
}
@ -115,6 +131,18 @@ impl RenderLoop {
height: win_size.height,
present_mode: PresentMode::Mailbox,
});
self.cpu_image = FlatSamples {
samples: vec![64u8; win_size.width as usize * win_size.height as usize * 4],
layout: SampleLayout {
channels: 4,
channel_stride: 1,
width: win_size.width,
width_stride: 4,
height: win_size.height,
height_stride: win_size.width as usize * 4,
},
color_hint: Some(ColorType::Rgba8),
};
}
async fn render(&mut self) -> Option<()>{
@ -126,8 +154,27 @@ impl RenderLoop {
self.rebuild().await;
}
};
let mut encoder = self.device.create_command_encoder(&CommandEncoderDescriptor { label: None });
let win_size = self.window.inner_size();
self.queue.write_texture(
ImageCopyTexture {
texture: &self.texture_buffer,
mip_level: 0,
origin: Origin3d::ZERO,
aspect: TextureAspect::All,
},
&self.cpu_image.samples,
ImageDataLayout {
offset: 0,
bytes_per_row: core::num::NonZeroU32::new(win_size.width * 4),
rows_per_image: None,
},
Extent3d {
width: win_size.width,
height: win_size.height,
depth_or_array_layers: 1,
}
);
let mut encoder = self.device.create_command_encoder(&CommandEncoderDescriptor { label: None });
encoder.copy_texture_to_texture(
ImageCopyTexture {
texture: &self.texture_buffer,

Loading…
Cancel
Save