Skip to content

Commit

Permalink
Make ImageFormat::from_wgpu's signature non-total (#783)
Browse files Browse the repository at this point in the history
Fixes #781
  • Loading branch information
DJMcNab authored Jan 7, 2025
1 parent de6c60b commit 382e5d8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
34 changes: 25 additions & 9 deletions vello/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,8 @@ impl Renderer {
engine.build_shaders_if_needed(device, options.num_init_threads);
let blit = options
.surface_format
.map(|surface_format| BlitPipeline::new(device, surface_format, &mut engine));
.map(|surface_format| BlitPipeline::new(device, surface_format, &mut engine))
.transpose()?;
#[cfg(feature = "debug_layers")]
let debug = options
.surface_format
Expand Down Expand Up @@ -520,11 +521,17 @@ impl Renderer {
.as_ref()
.expect("renderer should have configured surface_format to use on a surface");
let mut recording = Recording::default();
let target_proxy = ImageProxy::new(width, height, ImageFormat::from_wgpu(target.format));
let target_proxy = ImageProxy::new(
width,
height,
ImageFormat::from_wgpu(target.format)
.expect("`TargetTexture` always has a supported texture format"),
);
let surface_proxy = ImageProxy::new(
width,
height,
ImageFormat::from_wgpu(surface.texture.format()),
ImageFormat::from_wgpu(surface.texture.format())
.ok_or(Error::UnsupportedSurfaceFormat)?,
);
recording.draw(recording::DrawParams {
shader_id: blit.0,
Expand Down Expand Up @@ -597,7 +604,8 @@ impl Renderer {
let blit = self
.options
.surface_format
.map(|surface_format| BlitPipeline::new(device, surface_format, &mut engine));
.map(|surface_format| BlitPipeline::new(device, surface_format, &mut engine))
.transpose()?;
#[cfg(feature = "debug_layers")]
let debug = self
.options
Expand Down Expand Up @@ -782,11 +790,17 @@ impl Renderer {
.as_ref()
.expect("renderer should have configured surface_format to use on a surface");
let mut recording = Recording::default();
let target_proxy = ImageProxy::new(width, height, ImageFormat::from_wgpu(target.format));
let target_proxy = ImageProxy::new(
width,
height,
ImageFormat::from_wgpu(target.format)
.expect("`TargetTexture` always has a supported texture format"),
);
let surface_proxy = ImageProxy::new(
width,
height,
ImageFormat::from_wgpu(surface.texture.format()),
ImageFormat::from_wgpu(surface.texture.format())
.ok_or(Error::UnsupportedSurfaceFormat)?,
);
recording.draw(recording::DrawParams {
shader_id: blit.0,
Expand Down Expand Up @@ -899,7 +913,7 @@ struct BlitPipeline(ShaderId);

#[cfg(feature = "wgpu")]
impl BlitPipeline {
fn new(device: &Device, format: TextureFormat, engine: &mut WgpuEngine) -> Self {
fn new(device: &Device, format: TextureFormat, engine: &mut WgpuEngine) -> Result<Self> {
const SHADERS: &str = r#"
@vertex
fn vs_main(@builtin(vertex_index) ix: u32) -> @builtin(position) vec4<f32> {
Expand Down Expand Up @@ -947,11 +961,13 @@ impl BlitPipeline {
},
None,
&[(
BindType::ImageRead(ImageFormat::from_wgpu(format)),
BindType::ImageRead(
ImageFormat::from_wgpu(format).ok_or(Error::UnsupportedSurfaceFormat)?,
),
wgpu::ShaderStages::FRAGMENT,
)],
);
Self(shader_id)
Ok(Self(shader_id))
}
}

Expand Down
8 changes: 4 additions & 4 deletions vello/src/recording.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,11 @@ impl ImageFormat {
}

#[cfg(feature = "wgpu")]
pub fn from_wgpu(format: wgpu::TextureFormat) -> Self {
pub fn from_wgpu(format: wgpu::TextureFormat) -> Option<Self> {
match format {
wgpu::TextureFormat::Rgba8Unorm => Self::Rgba8,
wgpu::TextureFormat::Bgra8Unorm => Self::Bgra8,
_ => unimplemented!(),
wgpu::TextureFormat::Rgba8Unorm => Some(Self::Rgba8),
wgpu::TextureFormat::Bgra8Unorm => Some(Self::Bgra8),
_ => None,
}
}
}
Expand Down

0 comments on commit 382e5d8

Please sign in to comment.