Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added texture metadata #3346

Merged
merged 5 commits into from
Jan 5, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 106 additions & 2 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ pub struct Buffer {
map_context: Mutex<MapContext>,
size: wgt::BufferAddress,
usage: BufferUsages,
// Todo: missing map_state https://www.w3.org/TR/webgpu/#dom-gpubuffer-mapstate
}
static_assertions::assert_impl_all!(Buffer: Send, Sync);

Expand Down Expand Up @@ -227,6 +228,7 @@ pub struct Texture {
id: ObjectId,
data: Box<Data>,
owned: bool,
descriptor: TextureDescriptor<'static>,
}
static_assertions::assert_impl_all!(Texture: Send, Sync);

Expand Down Expand Up @@ -282,6 +284,7 @@ pub struct Surface {
context: Arc<C>,
id: ObjectId,
data: Box<Data>,
config: Mutex<Option<SurfaceConfiguration>>,
kurtkuehnert marked this conversation as resolved.
Show resolved Hide resolved
}
static_assertions::assert_impl_all!(Surface: Send, Sync);

Expand Down Expand Up @@ -1448,6 +1451,7 @@ impl Instance {
context: Arc::clone(&self.context),
id,
data,
config: Mutex::new(None),
})
}

Expand All @@ -1472,6 +1476,7 @@ impl Instance {
context: Arc::clone(&self.context),
id: ObjectId::from(surface.id()),
data: Box::new(surface),
config: Mutex::new(None),
}
}

Expand All @@ -1493,6 +1498,7 @@ impl Instance {
context: Arc::clone(&self.context),
id: ObjectId::from(surface.id()),
data: Box::new(surface),
config: Mutex::new(None),
}
}

Expand Down Expand Up @@ -1528,6 +1534,8 @@ impl Instance {
id: ObjectId::from(surface),
#[cfg(all(target_arch = "wasm32", not(feature = "webgl")))]
data: Box::new(()),
#[cfg(all(target_arch = "wasm32", not(feature = "webgl")))]
config: Mutex::new(None),
})
}

Expand Down Expand Up @@ -1563,6 +1571,8 @@ impl Instance {
id: ObjectId::from(surface),
#[cfg(all(target_arch = "wasm32", not(feature = "webgl")))]
data: Box::new(()),
#[cfg(all(target_arch = "wasm32", not(feature = "webgl")))]
kurtkuehnert marked this conversation as resolved.
Show resolved Hide resolved
config: Mutex::new(None),
})
}

Expand Down Expand Up @@ -2038,6 +2048,10 @@ impl Device {
id,
data,
owned: true,
descriptor: TextureDescriptor {
label: None,
..desc.clone()
},
}
}

Expand Down Expand Up @@ -2070,6 +2084,10 @@ impl Device {
id: ObjectId::from(texture.id()),
data: Box::new(texture),
owned: true,
descriptor: TextureDescriptor {
label: None,
..desc.clone()
},
}
}

Expand Down Expand Up @@ -2386,7 +2404,7 @@ impl Buffer {
/// Returns the length of the buffer allocation in bytes.
///
/// This is always equal to the `size` that was specified when creating the buffer.
pub fn size(&self) -> wgt::BufferAddress {
pub fn size(&self) -> BufferAddress {
self.size
}

Expand Down Expand Up @@ -2522,6 +2540,69 @@ impl Texture {
aspect: TextureAspect::All,
}
}

/// Returns the size of this `Texture`.
///
/// This is always equal to the `size` that was specified when creating the texture.
pub fn size(&self) -> Extent3d {
self.descriptor.size
}

/// Returns the width of this `Texture`.
///
/// This is always equal to the `size.width` that was specified when creating the texture.
pub fn width(&self) -> u32 {
self.descriptor.size.width
}

/// Returns the height of this `Texture`.
///
/// This is always equal to the `size.height` that was specified when creating the texture.
pub fn height(&self) -> u32 {
self.descriptor.size.height
}

/// Returns the depth or layer count of this `Texture`.
///
/// This is always equal to the `size.depth_or_array_layers` that was specified when creating the texture.
pub fn depth_or_array_layers(&self) -> u32 {
self.descriptor.size.depth_or_array_layers
}

/// Returns the mip_level_count of this `Texture`.
///
/// This is always equal to the `mip_level_count` that was specified when creating the texture.
pub fn mip_level_count(&self) -> u32 {
self.descriptor.mip_level_count
}

/// Returns the sample_count of this `Texture`.
///
/// This is always equal to the `sample_count` that was specified when creating the texture.
pub fn sample_count(&self) -> u32 {
self.descriptor.sample_count
}

/// Returns the dimension of this `Texture`.
///
/// This is always equal to the `dimension` that was specified when creating the texture.
pub fn dimension(&self) -> TextureDimension {
self.descriptor.dimension
}

/// Returns the format of this `Texture`.
///
/// This is always equal to the `format` that was specified when creating the texture.
pub fn format(&self) -> TextureFormat {
self.descriptor.format
}

/// Returns the allowed usages of this `Texture`.
///
/// This is always equal to the `usage` that was specified when creating the texture.
pub fn usage(&self) -> TextureUsages {
self.descriptor.usage
}
}

impl Drop for Texture {
Expand Down Expand Up @@ -3959,7 +4040,10 @@ impl Surface {
&device.id,
device.data.as_ref(),
config,
)
);

let mut conf = self.config.lock();
*conf = Some(config.clone());
}

/// Returns the next texture to be presented by the swapchain for drawing.
Expand All @@ -3982,6 +4066,25 @@ impl Surface {
SurfaceStatus::Lost => return Err(SurfaceError::Lost),
};

let config = self
.config
.lock()
.expect("This surface has not been configured yet.");
cwfitzgerald marked this conversation as resolved.
Show resolved Hide resolved

let descriptor = TextureDescriptor {
label: None,
size: Extent3d {
width: config.width,
height: config.height,
depth_or_array_layers: 1,
},
format: config.format,
usage: config.usage,
mip_level_count: 1,
sample_count: 1,
dimension: TextureDimension::D2,
};

texture_id
.zip(texture_data)
.map(|(id, data)| SurfaceTexture {
Expand All @@ -3990,6 +4093,7 @@ impl Surface {
id,
data,
owned: false,
descriptor,
},
suboptimal,
presented: false,
Expand Down