Skip to content

Commit 7a2067a

Browse files
committed
[wgpu-core] Create default ExternalTextureParams buffer
In upcoming patches, wgpu will allowing the creation of bind groups with either `TextureView`s or `ExternalTexture`s bound to a `BindingType::ExternalTexture` bind group layout entry. Wgpu-hal and the Naga-generated shaders must be able to handle both of these cases. For external textures they will be provided a uniform buffer containing the external texture's `ExternalTextureParams`. For the texture view case, we must therefore provide the same. To do this, we create a single buffer per device which can be shared between all texture views. We initialize it with the required values in Device::late_init_resources_with_queue(). We know that texture views must have a single RGBA plane, with no rotation or crop-rect. The only thing that can vary between them is their size. We will therefore use the value of [0, 0] in the params buffer to indicate to the shader that it should query the actual texture's size rather than using the value provided in the buffer.
1 parent 94b759a commit 7a2067a

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

wgpu-core/src/device/resource.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ pub struct Device {
173173
// Optional so that we can late-initialize this after the queue is created.
174174
pub(crate) timestamp_normalizer:
175175
OnceCellOrLock<crate::timestamp_normalization::TimestampNormalizer>,
176+
/// Uniform buffer containing [`ExternalTextureParams`] with values such
177+
/// that a [`TextureView`] bound to a [`wgt::BindingType::ExternalTexture`]
178+
/// binding point will be rendered correctly.
179+
pub(crate) default_external_texture_params_buffer: std::sync::OnceLock<Arc<Buffer>>,
176180
// needs to be dropped last
177181
#[cfg(feature = "trace")]
178182
pub(crate) trace: Mutex<Option<trace::Trace>>,
@@ -314,6 +318,7 @@ impl Device {
314318
adapter: adapter.clone(),
315319
queue: OnceCellOrLock::new(),
316320
zero_buffer: ManuallyDrop::new(zero_buffer),
321+
default_external_texture_params_buffer: std::sync::OnceLock::new(),
317322
label: desc.label.to_string(),
318323
command_allocator,
319324
command_indices: RwLock::new(
@@ -364,7 +369,7 @@ impl Device {
364369
})
365370
}
366371

367-
pub fn late_init_resources_with_queue(&self) -> Result<(), RequestDeviceError> {
372+
pub fn late_init_resources_with_queue(self: &Arc<Self>) -> Result<(), RequestDeviceError> {
368373
let queue = self.get_queue().unwrap();
369374

370375
let timestamp_normalizer = crate::timestamp_normalization::TimestampNormalizer::new(
@@ -376,6 +381,50 @@ impl Device {
376381
.set(timestamp_normalizer)
377382
.unwrap_or_else(|_| panic!("Called late_init_resources_with_queue twice"));
378383

384+
let params_data = ExternalTextureParams {
385+
#[rustfmt::skip]
386+
yuv_conversion_matrix: [
387+
1.0, 0.0, 0.0, 0.0,
388+
0.0, 1.0, 0.0, 0.0,
389+
0.0, 0.0, 1.0, 0.0,
390+
0.0, 0.0, 0.0, 1.0,
391+
],
392+
size: [0, 0],
393+
#[rustfmt::skip]
394+
sample_transform: [
395+
1.0, 0.0,
396+
0.0, 1.0,
397+
0.0, 0.0
398+
],
399+
num_planes: 1,
400+
};
401+
let params_buffer = self
402+
.create_buffer(&resource::BufferDescriptor {
403+
label: Some(Cow::Borrowed(
404+
"(wgpu internal) default external texture params buffer",
405+
)),
406+
size: size_of_val(&params_data) as wgt::BufferAddress,
407+
usage: wgt::BufferUsages::UNIFORM | wgt::BufferUsages::COPY_DST,
408+
mapped_at_creation: false,
409+
})
410+
.map_err(|err| match err {
411+
resource::CreateBufferError::Device(device) => RequestDeviceError::Device(device),
412+
_ => unreachable!("Error creating default external texture params buffer: {err:?}"),
413+
})?;
414+
queue
415+
.write_buffer(
416+
Fallible::Valid(params_buffer.clone()),
417+
0,
418+
bytemuck::bytes_of(&params_data),
419+
)
420+
.map_err(|err| match err {
421+
super::queue::QueueWriteError::Queue(device) => RequestDeviceError::Device(device),
422+
_ => unreachable!("Error writing default external texture params buffer: {err:?}"),
423+
})?;
424+
self.default_external_texture_params_buffer
425+
.set(params_buffer)
426+
.unwrap_or_else(|_| panic!("Called late_init_resources_with_queue twice"));
427+
379428
Ok(())
380429
}
381430

0 commit comments

Comments
 (0)