Skip to content

Commit d5d8ed8

Browse files
feat(core): Expose descriptor validation separately from create_texture (gfx-rs#9979)
1 parent fa6d834 commit d5d8ed8

3 files changed

Lines changed: 45 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ Bottom level categories:
4747
#### General
4848

4949
- Support the `wasm64-unknown-unknown` target for the web backend. Building for wasm64 requires a nightly toolchain with `-Z build-std=std,panic_abort`. By @nickbabcock in [#9836](https://github.com/gfx-rs/wgpu/pull/9836).
50-
- `wgpu-core` now exposes a `validate_device_descriptor` function that validates a device descriptor as `request_device` would. This may be useful in conjunction with `create_device_from_hal`. By @andyleiserson in [#9967](https://github.com/gfx-rs/wgpu/pull/9967).
5150
- Many types now offer `pub const fn default()` in addition to implementing the `Default` trait, allowing constants to make use of default values. By @kpreid in [#9929](https://github.com/gfx-rs/wgpu/pull/9929).
51+
- `wgpu-core` now exposes `validate_device_descriptor` and `validate_texture_descriptor` functions that perform the same descriptor validation the corresponding resource creation APIs would, without actually creating a resource. This may be useful in conjunction with hal raw APIs. By @andyleiserson in [#9967](https://github.com/gfx-rs/wgpu/pull/9967) and [#9979](https://github.com/gfx-rs/wgpu/pull/9979).
5252

5353
#### Hal
5454

wgpu-core/src/device/global.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,18 @@ impl Global {
254254
(id, error)
255255
}
256256

257+
pub fn device_validate_texture_descriptor(
258+
&self,
259+
device_id: DeviceId,
260+
desc: &resource::TextureDescriptor,
261+
) -> Option<resource::CreateTextureError> {
262+
self.hub
263+
.devices
264+
.get(device_id)
265+
.validate_texture_descriptor(desc)
266+
.err()
267+
}
268+
257269
/// # Safety
258270
///
259271
/// - `hal_texture` must be created from `device_id` corresponding raw handle.

wgpu-core/src/device/resource.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,8 @@ impl Device {
15171517
desc: &resource::TextureDescriptor,
15181518
initial_state: wgt::TextureUses,
15191519
) -> Result<Arc<Texture>, resource::CreateTextureError> {
1520+
self.check_is_valid()?;
1521+
15201522
let format_features = self
15211523
.describe_format_features(desc.format)
15221524
.map_err(|error| resource::CreateTextureError::MissingFeatures(desc.format, error))?;
@@ -1662,10 +1664,29 @@ impl Device {
16621664
}
16631665
}
16641666

1665-
fn create_texture_inner(
1667+
/// Validate a texture descriptor.
1668+
///
1669+
/// This applies the same validation as [`Self::create_texture`], without
1670+
/// actually creating a texture.
1671+
pub fn validate_texture_descriptor(
16661672
self: &Arc<Self>,
16671673
desc: &resource::TextureDescriptor,
1668-
) -> Result<Arc<Texture>, resource::CreateTextureError> {
1674+
) -> Result<(), resource::CreateTextureError> {
1675+
self.validate_texture_descriptor_inner(desc)?;
1676+
Ok(())
1677+
}
1678+
1679+
/// Validate a texture descriptor.
1680+
///
1681+
/// Implements the [validating GPUTextureDescriptor] algorithm, with some
1682+
/// `wgpu` extensions.
1683+
///
1684+
/// [validating GPUTextureDescriptor]: https://www.w3.org/TR/webgpu/#abstract-opdef-validating-gputexturedescriptor
1685+
fn validate_texture_descriptor_inner(
1686+
self: &Arc<Self>,
1687+
desc: &resource::TextureDescriptor,
1688+
) -> Result<(wgt::TextureFormatFeatures, Vec<TextureFormat>), resource::CreateTextureError>
1689+
{
16691690
use resource::{CreateTextureError, TextureDimensionError};
16701691

16711692
self.check_is_valid()?;
@@ -1939,6 +1960,15 @@ impl Device {
19391960
self.require_downlevel_flags(wgt::DownlevelFlags::VIEW_FORMATS)?;
19401961
}
19411962

1963+
Ok((format_features, hal_view_formats))
1964+
}
1965+
1966+
fn create_texture_inner(
1967+
self: &Arc<Self>,
1968+
desc: &resource::TextureDescriptor,
1969+
) -> Result<Arc<Texture>, resource::CreateTextureError> {
1970+
let (format_features, hal_view_formats) = self.validate_texture_descriptor_inner(desc)?;
1971+
19421972
let hal_usage = conv::map_texture_usage_for_texture(desc, &format_features);
19431973

19441974
let hal_desc = hal::TextureDescriptor {

0 commit comments

Comments
 (0)