mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-25 08:51:05 +01:00
Move validation from Device::create_texture_from_hal() to Device::create_texture() (#2001)
* Move validation from `Device::create_texture_from_hal()` to `Device::create_texture()` --- Users encountered a segfault upon creating a texture when mip_level_count was set to 0 with (at least) Vulkan. This is due to wgpu's validation taking place after the API's texture is created. This change moves wgpu's validation to before the API's texture is created, introducing a friendly error instead of a segfault. * Make `Device::create_texture_from_hal()` infallible * run cargo fmt
This commit is contained in:
1 parent
13b374ef5b
commit
dbfdbf4e32
1 file changed
+50
-43
+50
-43
@@ -549,13 +549,50 @@ impl<A: HalApi> Device<A> {
|
||||
&self,
|
||||
hal_texture: A::Texture,
|
||||
self_id: id::DeviceId,
|
||||
adapter: &crate::instance::Adapter<A>,
|
||||
desc: &resource::TextureDescriptor,
|
||||
) -> Result<resource::Texture<A>, resource::CreateTextureError> {
|
||||
format_features: wgt::TextureFormatFeatures,
|
||||
) -> resource::Texture<A> {
|
||||
debug_assert_eq!(self_id.backend(), A::VARIANT);
|
||||
|
||||
let hal_usage = conv::map_texture_usage(desc.usage, desc.format.into());
|
||||
|
||||
resource::Texture {
|
||||
inner: resource::TextureInner::Native {
|
||||
raw: Some(hal_texture),
|
||||
},
|
||||
device_id: Stored {
|
||||
value: id::Valid(self_id),
|
||||
ref_count: self.life_guard.add_ref(),
|
||||
},
|
||||
desc: desc.map_label(|_| ()),
|
||||
hal_usage,
|
||||
format_features,
|
||||
full_range: TextureSelector {
|
||||
levels: 0..desc.mip_level_count,
|
||||
layers: 0..desc.array_layer_count(),
|
||||
},
|
||||
life_guard: LifeGuard::new(desc.label.borrow_or_default()),
|
||||
}
|
||||
}
|
||||
|
||||
fn create_texture(
|
||||
&self,
|
||||
self_id: id::DeviceId,
|
||||
adapter: &crate::instance::Adapter<A>,
|
||||
desc: &resource::TextureDescriptor,
|
||||
) -> Result<resource::Texture<A>, resource::CreateTextureError> {
|
||||
let hal_usage = conv::map_texture_usage(desc.usage, desc.format.into());
|
||||
let hal_desc = hal::TextureDescriptor {
|
||||
label: desc.label.borrow_option(),
|
||||
size: desc.size,
|
||||
mip_level_count: desc.mip_level_count,
|
||||
sample_count: desc.sample_count,
|
||||
dimension: desc.dimension,
|
||||
format: desc.format,
|
||||
usage: hal_usage,
|
||||
memory_flags: hal::MemoryFlags::empty(),
|
||||
};
|
||||
|
||||
let format_features = self
|
||||
.describe_format_features(adapter, desc.format)
|
||||
.map_err(|error| resource::CreateTextureError::MissingFeatures(desc.format, error))?;
|
||||
@@ -597,49 +634,13 @@ impl<A: HalApi> Device<A> {
|
||||
return Err(resource::CreateTextureError::InvalidMipLevelCount(mips));
|
||||
}
|
||||
|
||||
Ok(resource::Texture {
|
||||
inner: resource::TextureInner::Native {
|
||||
raw: Some(hal_texture),
|
||||
},
|
||||
device_id: Stored {
|
||||
value: id::Valid(self_id),
|
||||
ref_count: self.life_guard.add_ref(),
|
||||
},
|
||||
desc: desc.map_label(|_| ()),
|
||||
hal_usage,
|
||||
format_features,
|
||||
full_range: TextureSelector {
|
||||
levels: 0..desc.mip_level_count,
|
||||
layers: 0..desc.array_layer_count(),
|
||||
},
|
||||
life_guard: LifeGuard::new(desc.label.borrow_or_default()),
|
||||
})
|
||||
}
|
||||
|
||||
fn create_texture(
|
||||
&self,
|
||||
self_id: id::DeviceId,
|
||||
adapter: &crate::instance::Adapter<A>,
|
||||
desc: &resource::TextureDescriptor,
|
||||
) -> Result<resource::Texture<A>, resource::CreateTextureError> {
|
||||
let hal_usage = conv::map_texture_usage(desc.usage, desc.format.into());
|
||||
let hal_desc = hal::TextureDescriptor {
|
||||
label: desc.label.borrow_option(),
|
||||
size: desc.size,
|
||||
mip_level_count: desc.mip_level_count,
|
||||
sample_count: desc.sample_count,
|
||||
dimension: desc.dimension,
|
||||
format: desc.format,
|
||||
usage: hal_usage,
|
||||
memory_flags: hal::MemoryFlags::empty(),
|
||||
};
|
||||
let raw = unsafe {
|
||||
self.raw
|
||||
.create_texture(&hal_desc)
|
||||
.map_err(DeviceError::from)?
|
||||
};
|
||||
|
||||
self.create_texture_from_hal(raw, self_id, adapter, desc)
|
||||
Ok(self.create_texture_from_hal(raw, self_id, desc, format_features))
|
||||
}
|
||||
|
||||
fn create_texture_view(
|
||||
@@ -3118,11 +3119,17 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
|
||||
}
|
||||
|
||||
let adapter = &adapter_guard[device.adapter_id.value];
|
||||
|
||||
let format_features = match device
|
||||
.describe_format_features(adapter, desc.format)
|
||||
.map_err(|error| resource::CreateTextureError::MissingFeatures(desc.format, error))
|
||||
{
|
||||
Ok(features) => features,
|
||||
Err(error) => break error,
|
||||
};
|
||||
|
||||
let texture =
|
||||
match device.create_texture_from_hal(hal_texture, device_id, adapter, desc) {
|
||||
Ok(texture) => texture,
|
||||
Err(error) => break error,
|
||||
};
|
||||
device.create_texture_from_hal(hal_texture, device_id, desc, format_features);
|
||||
let num_levels = texture.full_range.levels.end;
|
||||
let num_layers = texture.full_range.layers.end;
|
||||
let ref_count = texture.life_guard.add_ref();
|
||||
|
||||
Reference in new issue
Block a user