Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions fidget-wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@
//! Users are expected to create one (of each) context object per thread or
//! worker, since GPU resources can't be shared.
//!
//! Context objects have two flavors of functions. At the highest level, `run`
//! and `run_async` functions perform rendering and copy data back to the CPU
//! (e.g. [`voxel::Context::run`] and [`run_async`](voxel::Context::run_async)).
//! To simply submit work to the GPU, use a `submit` function (e.g.
//! [`voxel::Context::submit`]).
//! Context objects have three flavors of functions:
//!
//! - At the highest level, `run` and `run_async` functions perform rendering
//! and copy data back to the CPU (e.g. [`voxel::Context::run`] and
//! [`run_async`](voxel::Context::run_async)).
//! - To simply submit work to the GPU, use a `submit` function (e.g.
//! [`voxel::Context::submit`]).
//! - At the lowest level, to encode a rendering operation into a WebGPU
//! `CommandEncoder`, use an `encode` function (e.g.
//! [`voxel::Context::encode`]). Note that encode-flavored functions may
//! still use the GPU device and queue from the context, e.g. to allocate
//! buffers or copy configuration blobs.
//!
//! ### Workspace objects
//! Workspace objects contain all of the buffers that are used when dispatching
Expand Down
235 changes: 128 additions & 107 deletions fidget-wgpu/src/pixel/effects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,24 @@ impl Context {
image: &FlexBuffer<PixelBufferTag>,
remove_nans: bool,
buf: &mut MergeWorkspace,
) -> Result<(), MergeError> {
let mut encoder = self.gpu.device.create_command_encoder(
&wgpu::CommandEncoderDescriptor {
label: Some("merge compute encoder"),
},
);
self.encode_merge(image, remove_nans, buf, &mut encoder)?;
self.gpu.queue.submit(Some(encoder.finish()));
Ok(())
}

/// Low-level function to encode a merge operation into a command encoder
pub fn encode_merge(
&self,
image: &FlexBuffer<PixelBufferTag>,
remove_nans: bool,
buf: &mut MergeWorkspace,
encoder: &mut wgpu::CommandEncoder,
Comment on lines +245 to +250
) -> Result<(), MergeError> {
let size = image.size();
if buf.image_count > 0 {
Expand All @@ -250,73 +268,64 @@ impl Context {
.map_err(MergeError::OutputSize)?;
}
buf.has_color = false;
let mut encoder = self.gpu.device.create_command_encoder(
&wgpu::CommandEncoderDescriptor {
label: Some("merge compute encoder"),
},
);
// Scope to bound the lifetime of compute_pass
let mut compute_pass =
encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("merge compute pass"),
timestamp_writes: None, // TODO add timestamps?
});
compute_pass.set_pipeline(&self.merge_pipeline);
let cfg = MergeConfig {
image_size: [size.width(), size.height()],
remove_nans: remove_nans as u32,
index_base: buf.image_count as u32,
};
{
let mut compute_pass =
encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("merge compute pass"),
timestamp_writes: None, // TODO add timestamps?
});
compute_pass.set_pipeline(&self.merge_pipeline);
let cfg = MergeConfig {
image_size: [size.width(), size.height()],
remove_nans: remove_nans as u32,
index_base: buf.image_count as u32,
};
{
let mut writer = self
.gpu
.queue
.write_buffer_with(
&buf.config,
0,
(std::mem::size_of::<MergeConfig>() as u64)
.try_into()
.unwrap(),
)
.unwrap();
writer.copy_from_slice(cfg.as_bytes());
}

let bg =
self.gpu
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("merge bind group"),
layout: &self.merge_bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: buf.config.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: image.bind_active(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: buf.distance.bind_active(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: buf.color.bind_active(),
},
],
});
compute_pass.set_bind_group(0, Some(&bg), &[]);
compute_pass.dispatch_workgroups(
size.width().div_ceil(8),
size.height().div_ceil(8),
1,
);
buf.image_count += 1;
let mut writer = self
.gpu
.queue
.write_buffer_with(
&buf.config,
0,
(std::mem::size_of::<MergeConfig>() as u64)
.try_into()
.unwrap(),
)
.unwrap();
writer.copy_from_slice(cfg.as_bytes());
}
self.gpu.queue.submit(Some(encoder.finish()));

let bg =
self.gpu
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("merge bind group"),
layout: &self.merge_bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: buf.config.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: image.bind_active(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: buf.distance.bind_active(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: buf.color.bind_active(),
},
],
});
compute_pass.set_bind_group(0, Some(&bg), &[]);
compute_pass.dispatch_workgroups(
size.width().div_ceil(8),
size.height().div_ceil(8),
1,
);
buf.image_count += 1;
Ok(())
}

Expand Down Expand Up @@ -379,9 +388,27 @@ impl Context {
shape: &ShapeColorBuffers,
bufs: &mut ColorWorkspace,
vars: &ShapeVars<f32>,
) -> Result<(), ColorError> {
let mut encoder = self.gpu.device.create_command_encoder(
&wgpu::CommandEncoderDescriptor { label: None },
);
self.encode_color(merge, settings, shape, bufs, vars, &mut encoder)?;
self.gpu.queue.submit(Some(encoder.finish()));
Ok(())
}

/// Low-level function to encode a color rendering pass
pub fn encode_color(
&self,
merge: &mut MergeWorkspace,
settings: ColorSettings,
shape: &ShapeColorBuffers,
bufs: &mut ColorWorkspace,
vars: &ShapeVars<f32>,
encoder: &mut wgpu::CommandEncoder,
Comment on lines +401 to +408
) -> Result<(), ColorError> {
self.color_ctx
.submit(merge, settings, shape, bufs, vars, &self.gpu)
.encode(merge, settings, shape, bufs, vars, &self.gpu, encoder)
}

/// Returns a new workspace for color evaluation
Expand Down Expand Up @@ -500,14 +527,16 @@ impl ColorContext {
}
}

fn submit(
#[allow(clippy::too_many_arguments)]
fn encode(
&self,
image: &mut MergeWorkspace,
settings: ColorSettings,
shape: &ShapeColorBuffers,
bufs: &mut ColorWorkspace,
vars: &ShapeVars<f32>,
gpu: &Gpu,
encoder: &mut wgpu::CommandEncoder,
) -> Result<(), ColorError> {
if image.image_count != shape.shape_count() {
return Err(ColorError::BadShapeCount {
Expand Down Expand Up @@ -544,47 +573,39 @@ impl ColorContext {
let config_bg =
bufs.config_bind_group(&gpu.device, &self.config_bind_group_layout);

// Create a command encoder and dispatch the compute work
let mut encoder = gpu.device.create_command_encoder(
&wgpu::CommandEncoderDescriptor { label: None },
let mut compute_pass =
encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: None,
timestamp_writes: None, // TODO add timestamps?
});
compute_pass.set_bind_group(0, config_bg, &[]);

// TODO this creates a bind group for every evaluation, instead of
// caching it somewhere. However, *where* to cache it is not
// obvious, because it combines fields from two different buffer
// objects.
let image_bg =
gpu.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("color image bind group"),
layout: &self.image_bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: image.distance.bind_active(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: image.color.bind_active(),
},
],
});
compute_pass.set_bind_group(1, &image_bg, &[]);
compute_pass.set_pipeline(self.color_pipeline.get(shape.reg_count()));
compute_pass.dispatch_workgroups(
size.width().div_ceil(8),
size.height().div_ceil(8),
1,
);
{
let mut compute_pass =
encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: None,
timestamp_writes: None, // TODO add timestamps?
});
compute_pass.set_bind_group(0, config_bg, &[]);

// TODO this creates a bind group for every evaluation, instead of
// caching it somewhere. However, *where* to cache it is not
// obvious, because it combines fields from two different buffer
// objects.
let image_bg =
gpu.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("color image bind group"),
layout: &self.image_bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: image.distance.bind_active(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: image.color.bind_active(),
},
],
});
compute_pass.set_bind_group(1, &image_bg, &[]);
compute_pass
.set_pipeline(self.color_pipeline.get(shape.reg_count()));
compute_pass.dispatch_workgroups(
size.width().div_ceil(8),
size.height().div_ceil(8),
1,
);
}
gpu.queue.submit(Some(encoder.finish()));
image.has_color = true;
Ok(())
}
Expand Down
27 changes: 18 additions & 9 deletions fidget-wgpu/src/pixel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,23 @@ impl Context {
vars: &ShapeVars<f32>,
workspace: &mut Workspace,
settings: &RenderConfig,
) -> Result<(), SubmitError> {
let mut encoder = self.gpu.device.create_command_encoder(
&wgpu::CommandEncoderDescriptor { label: None },
);
self.encode(shape, vars, workspace, settings, &mut encoder)?;
self.gpu.queue.submit(Some(encoder.finish()));
Ok(())
}

/// Low-level function to encode pixel rendering to a command encoder
pub fn encode(
&self,
shape: &RenderShape,
vars: &ShapeVars<f32>,
workspace: &mut Workspace,
settings: &RenderConfig,
encoder: &mut wgpu::CommandEncoder,
Comment on lines +1117 to +1123
) -> Result<(), SubmitError> {
workspace.set_image_size(&self.gpu.device, settings.image_size)?;
let render_size = TileRenderSize::from(workspace.image_size);
Expand Down Expand Up @@ -1163,13 +1180,8 @@ impl Context {
workspace.bind_groups.common = Default::default();
}

// Create a command encoder and dispatch the compute work
let mut encoder = self.gpu.device.create_command_encoder(
&wgpu::CommandEncoderDescriptor { label: None },
);

// Initial buffer reset pass
self.reset_ctx.run(&mut encoder, workspace);
self.reset_ctx.run(encoder, workspace);

let mut compute_pass =
encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
Expand Down Expand Up @@ -1210,9 +1222,6 @@ impl Context {
&mut compute_pass,
);
drop(compute_pass);

// Submit the commands and wait for the GPU to complete
self.gpu.queue.submit(Some(encoder.finish()));
Ok(())
}
}
Expand Down
Loading
Loading