Skip to content

Commit

Permalink
Add view_formats in TextureDescriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
jinleili committed Nov 26, 2022
1 parent d7fe7ef commit 735a35f
Show file tree
Hide file tree
Showing 22 changed files with 76 additions and 6 deletions.
25 changes: 20 additions & 5 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,16 @@ impl<A: HalApi> Device<A> {
));
}

// TODO: validate missing TextureDescriptor::view_formats.
if let Some(ref view_formats) = desc.view_formats {
for format in view_formats {
if desc.format == *format {
continue;
}
if desc.format.remove_srgb_suffix() != format.remove_srgb_suffix() {
return Err(CreateTextureError::InvalidViewFormat(*format, desc.format));
}
}
}

// Enforce having COPY_DST/DEPTH_STENCIL_WRIT/COLOR_TARGET otherwise we
// wouldn't be able to initialize the texture.
Expand Down Expand Up @@ -1086,10 +1095,16 @@ impl<A: HalApi> Device<A> {
}
let format = desc.format.unwrap_or(texture.desc.format);
if format != texture.desc.format {
return Err(resource::CreateTextureViewError::FormatReinterpretation {
texture: texture.desc.format,
view: format,
});
let compatible = match texture.desc.view_formats {
Some(ref view_formats) => view_formats.contains(&format),
None => false,
};
if !compatible {
return Err(resource::CreateTextureViewError::FormatReinterpretation {
texture: texture.desc.format,
view: format,
});
}
}

// filter the usages based on the other criteria
Expand Down
1 change: 1 addition & 0 deletions wgpu-core/src/present.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
format: config.format,
dimension: wgt::TextureDimension::D2,
usage: config.usage,
view_formats: None,
},
hal_usage: conv::map_texture_usage(config.usage, config.format.into()),
format_features: wgt::TextureFormatFeatures {
Expand Down
2 changes: 2 additions & 0 deletions wgpu-core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,8 @@ pub enum CreateTextureError {
if *.2 { " due to downlevel restrictions" } else { "" }
)]
InvalidFormatUsages(wgt::TextureUsages, wgt::TextureFormat, bool),
#[error("The view format {0:?} is not compatible with texture format {1:?}.")]
InvalidViewFormat(wgt::TextureFormat, wgt::TextureFormat),
#[error("Texture usages {0:?} are not allowed on a texture of dimensions {1:?}")]
InvalidDimensionUsages(wgt::TextureUsages, wgt::TextureDimension),
#[error("Texture usage STORAGE_BINDING is not allowed for multisampled textures")]
Expand Down
29 changes: 28 additions & 1 deletion wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2522,6 +2522,29 @@ impl TextureFormat {
},
}
}

/// Get texture view format compatible none `Srgb` suffix format.
pub fn remove_srgb_suffix(&self) -> TextureFormat {
match *self {
Self::Rgba8UnormSrgb => Self::Rgba8Unorm,
Self::Bgra8UnormSrgb => Self::Bgra8Unorm,
Self::Bc1RgbaUnormSrgb => Self::Bc1RgbaUnorm,
Self::Bc2RgbaUnormSrgb => Self::Bc2RgbaUnorm,
Self::Bc3RgbaUnormSrgb => Self::Bc3RgbaUnorm,
Self::Bc7RgbaUnormSrgb => Self::Bc7RgbaUnorm,
Self::Etc2Rgb8UnormSrgb => Self::Etc2Rgb8Unorm,
Self::Etc2Rgb8A1UnormSrgb => Self::Etc2Rgb8A1Unorm,
Self::Etc2Rgba8UnormSrgb => Self::Etc2Rgba8Unorm,
Self::Astc {
block,
channel: AstcChannel::UnormSrgb,
} => Self::Astc {
block,
channel: AstcChannel::Unorm,
},
_ => *self,
}
}
}

#[test]
Expand Down Expand Up @@ -4248,7 +4271,10 @@ pub struct TextureDescriptor<L> {
pub format: TextureFormat,
/// Allowed usages of the texture. If used in other ways, the operation will panic.
pub usage: TextureUsages,
// TODO: missing view_formats https://www.w3.org/TR/webgpu/#dom-gputexturedescriptor-viewformats
/// Specifies what view format values will be allowed when calling create_view() on this texture.
/// Note: Adding a format to this list may have a significant performance impact,
/// so it is best to avoid adding formats unnecessarily.
pub view_formats: Option<Vec<TextureFormat>>,
}

impl<L> TextureDescriptor<L> {
Expand All @@ -4262,6 +4288,7 @@ impl<L> TextureDescriptor<L> {
dimension: self.dimension,
format: self.format,
usage: self.usage,
view_formats: self.view_formats.clone(),
}
}

Expand Down
1 change: 1 addition & 0 deletions wgpu/examples/bunnymark/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ impl framework::Example for Example {
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: None,
});
queue.write_texture(
texture.as_image_copy(),
Expand Down
1 change: 1 addition & 0 deletions wgpu/examples/capture/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ async fn create_red_image_with_dimensions(
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
label: None,
view_formats: None,
});

// Set the background to be red
Expand Down
1 change: 1 addition & 0 deletions wgpu/examples/conservative-raster/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl Example {
format: RENDER_TARGET_FORMAT,
usage: wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::RENDER_ATTACHMENT,
view_formats: None,
})
.create_view(&Default::default());

Expand Down
1 change: 1 addition & 0 deletions wgpu/examples/cube/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ impl framework::Example for Example {
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::R8Uint,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: None,
});
let texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default());
queue.write_texture(
Expand Down
1 change: 1 addition & 0 deletions wgpu/examples/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ pub fn test<E: Example>(mut params: FrameworkRefTest) {
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
view_formats: None,
});

let dst_view = dst_texture.create_view(&wgpu::TextureViewDescriptor::default());
Expand Down
1 change: 1 addition & 0 deletions wgpu/examples/mipmap/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ impl framework::Example for Example {
| wgpu::TextureUsages::RENDER_ATTACHMENT
| wgpu::TextureUsages::COPY_DST,
label: None,
view_formats: None,
});
let texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default());
//Note: we could use queue.write_texture instead, and this is what other
Expand Down
1 change: 1 addition & 0 deletions wgpu/examples/msaa-line/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ impl Example {
format: config.format,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
label: None,
view_formats: None,
};

device
Expand Down
2 changes: 2 additions & 0 deletions wgpu/examples/shadow/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ impl Example {
format: Self::DEPTH_FORMAT,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
label: None,
view_formats: None,
});

depth_texture.create_view(&wgpu::TextureViewDescriptor::default())
Expand Down Expand Up @@ -385,6 +386,7 @@ impl framework::Example for Example {
format: Self::SHADOW_FORMAT,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
label: None,
view_formats: None,
});
let shadow_view = shadow_texture.create_view(&wgpu::TextureViewDescriptor::default());

Expand Down
2 changes: 2 additions & 0 deletions wgpu/examples/skybox/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl Skybox {
format: Self::DEPTH_FORMAT,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
label: None,
view_formats: None,
});

depth_texture.create_view(&wgpu::TextureViewDescriptor::default())
Expand Down Expand Up @@ -328,6 +329,7 @@ impl framework::Example for Skybox {
format: skybox_format,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
label: None,
view_formats: None,
},
&image.data,
);
Expand Down
5 changes: 5 additions & 0 deletions wgpu/examples/texture-arrays/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,26 @@ impl framework::Example for Example {
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
label: None,
view_formats: None,
};
let red_texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("red"),
view_formats: None,
..texture_descriptor
});
let green_texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("green"),
view_formats: None,
..texture_descriptor
});
let blue_texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("blue"),
view_formats: None,
..texture_descriptor
});
let white_texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("white"),
view_formats: None,
..texture_descriptor
});

Expand Down
2 changes: 2 additions & 0 deletions wgpu/examples/water/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ impl Example {
usage: wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::COPY_DST
| wgpu::TextureUsages::RENDER_ATTACHMENT,
view_formats: None,
});

let draw_depth_buffer = device.create_texture(&wgpu::TextureDescriptor {
Expand All @@ -209,6 +210,7 @@ impl Example {
usage: wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::COPY_DST
| wgpu::TextureUsages::RENDER_ATTACHMENT,
view_formats: None,
});

let color_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
Expand Down
1 change: 1 addition & 0 deletions wgpu/tests/clear_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ fn single_texture_clear_test(
// Forces internally the required usages to be able to clear it.
// This is not visible on the API level.
usage: wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: None,
});
let mut encoder = ctx
.device
Expand Down
1 change: 1 addition & 0 deletions wgpu/tests/queue_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn queue_write_texture_overflow() {
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba32Float,
usage: wgpu::TextureUsages::COPY_DST,
view_formats: None,
});

let data = vec![255; 128];
Expand Down
1 change: 1 addition & 0 deletions wgpu/tests/resource_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fn bad_texture() {
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsages::all(),
view_formats: None,
})
});

Expand Down
1 change: 1 addition & 0 deletions wgpu/tests/shader_primitive_index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ fn pulling_common(
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8Unorm,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
view_formats: None,
});
let color_view = color_texture.create_view(&wgpu::TextureViewDescriptor::default());

Expand Down
1 change: 1 addition & 0 deletions wgpu/tests/vertex_indices/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ fn pulling_common(
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8Unorm,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_DST,
view_formats: None,
},
&[0, 0, 0, 1],
)
Expand Down
1 change: 1 addition & 0 deletions wgpu/tests/write_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fn write_texture_subset() {
| wgpu::TextureUsages::TEXTURE_BINDING,
mip_level_count: 1,
sample_count: 1,
view_formats: None,
});
let data = vec![1u8; size as usize * 2];
// Write the first two rows
Expand Down
1 change: 1 addition & 0 deletions wgpu/tests/zero_init_texture_after_discard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ fn create_white_texture_and_readback_buffer(
| wgpu::TextureUsages::COPY_SRC
| wgpu::TextureUsages::RENDER_ATTACHMENT
},
view_formats: None,
});

// Clear using a write_texture operation. We could also clear using a render_pass clear.
Expand Down

0 comments on commit 735a35f

Please sign in to comment.