Struct gpgpu::GpuImage [−][src]
pub struct GpuImage<'fw, P> { /* fields omitted */ }
Expand description
2D-image of homogeneous pixels.
Equivalent to write-only OpenCL’s Image objects.
More information about its shader representation is
under the DescriptorSet::bind_image
documentation.
Implementations
Pulls some elements from the GpuImage
into buf
, returning how many pixels were read.
Blocking version of GpuImage::read()
.
Examples found in repository
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
fn main() {
let fw = Framework::default();
// Camera initilization. Config may not work if not same cam as the Thinkpad T480 one.
// Change parameters accordingly
let mut camera = {
let camera_format = CameraFormat::new(
Resolution {
width_x: WIDTH as u32,
height_y: HEIGHT as u32,
},
nokhwa::FrameFormat::MJPEG,
30,
);
Camera::new(0, Some(camera_format)).unwrap()
};
// Window initialization
let mut window = Window::new(
"gpgpu webcam example",
WIDTH,
HEIGHT,
WindowOptions::default(),
)
.unwrap();
camera.open_stream().unwrap();
window.limit_update_rate(Some(std::time::Duration::from_secs_f32(1.0 / 60.0)));
// Since the same GPU resources could be used during the whole execution
// of the program, they are outside of the event loop
let gpu_input = GpuConstImage::<Rgba8UintNorm>::new(&fw, WIDTH as u32, HEIGHT as u32); // Cam frame texture
let buf_time = GpuUniformBuffer::<f32>::with_capacity(&fw, 1); // Elapsed time buffer (single element) for fancy shaders 😁
let gpu_output = GpuImage::<Rgba8UintNorm>::new(&fw, WIDTH as u32, HEIGHT as u32); // Shader output
let shader = gpgpu::Shader::from_wgsl_file(&fw, "examples/webcam/shader.wgsl").unwrap();
let desc = DescriptorSet::default()
.bind_const_image(&gpu_input)
.bind_image(&gpu_output)
.bind_uniform_buffer(&buf_time);
let program = gpgpu::Program::new(&shader, "main").add_descriptor_set(desc);
let kernel = gpgpu::Kernel::new(&fw, program);
let time = std::time::Instant::now();
let mut frame_buffer = vec![0u32; WIDTH * HEIGHT * 4];
let mut total = 0.0;
let mut count = 0;
while window.is_open() && !window.is_key_down(Key::Escape) {
let fps = std::time::Instant::now();
let cam_buf = camera.frame().unwrap(); // Obtain cam current frame
gpu_input.write_image_buffer(&cam_buf.convert()).unwrap(); // Upload cam frame into the cam frame texture
buf_time.write(&[time.elapsed().as_secs_f32()]).unwrap(); // Upload elapsed time into elapsed time buffer
kernel.enqueue(WIDTH as u32 / 32, HEIGHT as u32 / 31, 1);
gpu_output
.read_blocking(bytemuck::cast_slice_mut(&mut frame_buffer))
.unwrap();
// Write processed cam frame into window frame buffer
window
.update_with_buffer(&frame_buffer, WIDTH, HEIGHT)
.unwrap();
print_fps(fps.elapsed().as_secs_f32(), &mut total, &mut count);
}
}
Blocking version of GpuImage::read_vec()
.
Examples found in repository
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
fn main() {
let fw = gpgpu::Framework::default();
let shader = gpgpu::Shader::from_wgsl_file(&fw, "examples/mirror-image/shader.wgsl").unwrap();
let dynamic_img = image::open("examples/mirror-image/monke.jpg").unwrap(); // RGB8 image ...
let rgba = dynamic_img.into_rgba8(); // ... converted to RGBA8
let (width, height) = rgba.dimensions();
// GPU image creation
let input_img = gpgpu::GpuConstImage::<Rgba8Uint>::new(&fw, width, height); // Input
let output_img = gpgpu::GpuImage::<Rgba8Uint>::new(&fw, width, height); // Output
// Write input image into the GPU
input_img.write(&rgba).unwrap();
let desc = gpgpu::DescriptorSet::default()
.bind_const_image(&input_img)
.bind_image(&output_img);
let program = gpgpu::Program::new(&shader, "main").add_descriptor_set(desc);
gpgpu::Kernel::new(&fw, program).enqueue(width / 32, height / 32, 1); // Since the kernel workgroup size is (32, 32, 1) dims are divided
let output_bytes = output_img.read_vec_blocking().unwrap();
image::save_buffer(
"examples/mirror-image/mirror-monke.png",
&output_bytes,
width,
height,
image::ColorType::Rgba8,
)
.unwrap();
}
Writes a buffer into this GpuImage
, returning how many pixels were written. The operation is instantly offloaded.
This function will attempt to write the entire contents of buf
, unless its capacity
exceeds the one of the image, in which case the first width * height
pixels are written.
Trait Implementations
Constructs an image from a wgpu::Texture
and its [wgpu::Extent3d
].
Safety
If any of the following conditions are not satisfied, the image will panic at any time during its usage.
texture
needs to bewgpu::TextureUsages::STORAGE_BINDING
,wgpu::TextureUsages::COPY_SRC
, and `wgpu::TextureUsages::COPY_DST`` usable.T
needs to be the exact same codificationtexture
is.dimensions
needs to have the exactwidth
andheight
oftexture
anddepth_or_array_layers = 1
Returns a wgpu::BindingResource
of the image.
Returns the wgpu::Texture
that handles the GPU image.
Returns a [wgpu::Extent3d
] of the image. Convenience function.
Constructs an empty image with the desired width
and height
.
Construct a new image from a bytes source data
and its width
and height
. Read more
Decomposes an image into a wgpu::Texture
and its [wgpu::Extent3d
].