Skip to content

Commit 6903a94

Browse files
committed
get pixel size from wgpu (#6820)
# Objective - Get rid of giant match statement to get PixelInfo. - This will allow for supporting any texture that is uncompressed, instead of people needing to PR in any textures that are supported in wgpu, but not bevy. ## Solution - More conservative alternative to #6788, where we don't try to make some of the calculations correct for compressed types. - Delete `PixelInfo` and get the pixel_size directly from wgpu. Data from wgpu is here: https://docs.rs/wgpu-types/0.14.0/src/wgpu_types/lib.rs.html#2359 - Panic if the texture is a compressed type. An integer byte size of a pixel is no longer a valid concept when talking about compressed textures. - All internal usages use `pixel_size` and not `pixel_info` and are on uncompressed formats. Most of these usages are on either explicit texture formats or slightly indirectly through `TextureFormat::bevy_default()`. The other uses are in `TextureAtlas` and have other calculations that assumes the texture is uncompressed. ## Changelog - remove `PixelInfo` and get `pixel_size` from wgpu ## Migration Guide `PixelInfo` has been removed. `PixelInfo::components` is equivalent to `texture_format.describe().components`. `PixelInfo::type_size` can be gotten from `texture_format.describe().block_size/ texture_format.describe().components`. But note this can yield incorrect results for some texture types like Rg11b10Float.
1 parent 2e7925d commit 6903a94

File tree

1 file changed

+7
-123
lines changed

1 file changed

+7
-123
lines changed

crates/bevy_render/src/texture/image.rs

Lines changed: 7 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -464,134 +464,18 @@ impl Volume for Extent3d {
464464
}
465465
}
466466

467-
/// Information about the pixel size in bytes and the number of different components.
468-
pub struct PixelInfo {
469-
/// The size of a component of a pixel in bytes.
470-
pub type_size: usize,
471-
/// The amount of different components (color channels).
472-
pub num_components: usize,
473-
}
474-
475467
/// Extends the wgpu [`TextureFormat`] with information about the pixel.
476468
pub trait TextureFormatPixelInfo {
477-
/// Returns the pixel information of the format.
478-
fn pixel_info(&self) -> PixelInfo;
479-
/// Returns the size of a pixel of the format.
480-
fn pixel_size(&self) -> usize {
481-
let info = self.pixel_info();
482-
info.type_size * info.num_components
483-
}
469+
/// Returns the size of a pixel in bytes of the format.
470+
fn pixel_size(&self) -> usize;
484471
}
485472

486473
impl TextureFormatPixelInfo for TextureFormat {
487-
#[allow(clippy::match_same_arms)]
488-
fn pixel_info(&self) -> PixelInfo {
489-
let type_size = match self {
490-
// 8bit
491-
TextureFormat::R8Unorm
492-
| TextureFormat::R8Snorm
493-
| TextureFormat::R8Uint
494-
| TextureFormat::R8Sint
495-
| TextureFormat::Rg8Unorm
496-
| TextureFormat::Rg8Snorm
497-
| TextureFormat::Rg8Uint
498-
| TextureFormat::Rg8Sint
499-
| TextureFormat::Rgba8Unorm
500-
| TextureFormat::Rgba8UnormSrgb
501-
| TextureFormat::Rgba8Snorm
502-
| TextureFormat::Rgba8Uint
503-
| TextureFormat::Rgba8Sint
504-
| TextureFormat::Bgra8Unorm
505-
| TextureFormat::Bgra8UnormSrgb => 1,
506-
507-
// 16bit
508-
TextureFormat::R16Uint
509-
| TextureFormat::R16Sint
510-
| TextureFormat::R16Float
511-
| TextureFormat::R16Unorm
512-
| TextureFormat::Rg16Uint
513-
| TextureFormat::Rg16Sint
514-
| TextureFormat::Rg16Unorm
515-
| TextureFormat::Rg16Float
516-
| TextureFormat::Rgba16Uint
517-
| TextureFormat::Rgba16Sint
518-
| TextureFormat::Rgba16Float => 2,
519-
520-
// 32bit
521-
TextureFormat::R32Uint
522-
| TextureFormat::R32Sint
523-
| TextureFormat::R32Float
524-
| TextureFormat::Rg32Uint
525-
| TextureFormat::Rg32Sint
526-
| TextureFormat::Rg32Float
527-
| TextureFormat::Rgba32Uint
528-
| TextureFormat::Rgba32Sint
529-
| TextureFormat::Rgba32Float
530-
| TextureFormat::Depth32Float => 4,
531-
532-
// special cases
533-
TextureFormat::Rgb9e5Ufloat => 4,
534-
TextureFormat::Rgb10a2Unorm => 4,
535-
TextureFormat::Rg11b10Float => 4,
536-
TextureFormat::Depth24Plus => 3, // FIXME is this correct?
537-
TextureFormat::Depth24PlusStencil8 => 4,
538-
// TODO: this is not good! this is a temporary step while porting bevy_render to direct wgpu usage
539-
_ => panic!("cannot get pixel info for type"),
540-
};
541-
542-
let components = match self {
543-
TextureFormat::R8Unorm
544-
| TextureFormat::R8Snorm
545-
| TextureFormat::R8Uint
546-
| TextureFormat::R8Sint
547-
| TextureFormat::R16Uint
548-
| TextureFormat::R16Sint
549-
| TextureFormat::R16Unorm
550-
| TextureFormat::R16Float
551-
| TextureFormat::R32Uint
552-
| TextureFormat::R32Sint
553-
| TextureFormat::R32Float => 1,
554-
555-
TextureFormat::Rg8Unorm
556-
| TextureFormat::Rg8Snorm
557-
| TextureFormat::Rg8Uint
558-
| TextureFormat::Rg8Sint
559-
| TextureFormat::Rg16Uint
560-
| TextureFormat::Rg16Sint
561-
| TextureFormat::Rg16Unorm
562-
| TextureFormat::Rg16Float
563-
| TextureFormat::Rg32Uint
564-
| TextureFormat::Rg32Sint
565-
| TextureFormat::Rg32Float => 2,
566-
567-
TextureFormat::Rgba8Unorm
568-
| TextureFormat::Rgba8UnormSrgb
569-
| TextureFormat::Rgba8Snorm
570-
| TextureFormat::Rgba8Uint
571-
| TextureFormat::Rgba8Sint
572-
| TextureFormat::Bgra8Unorm
573-
| TextureFormat::Bgra8UnormSrgb
574-
| TextureFormat::Rgba16Uint
575-
| TextureFormat::Rgba16Sint
576-
| TextureFormat::Rgba16Float
577-
| TextureFormat::Rgba32Uint
578-
| TextureFormat::Rgba32Sint
579-
| TextureFormat::Rgba32Float => 4,
580-
581-
// special cases
582-
TextureFormat::Rgb9e5Ufloat
583-
| TextureFormat::Rgb10a2Unorm
584-
| TextureFormat::Rg11b10Float
585-
| TextureFormat::Depth32Float
586-
| TextureFormat::Depth24Plus
587-
| TextureFormat::Depth24PlusStencil8 => 1,
588-
// TODO: this is not good! this is a temporary step while porting bevy_render to direct wgpu usage
589-
_ => panic!("cannot get pixel info for type"),
590-
};
591-
592-
PixelInfo {
593-
type_size,
594-
num_components: components,
474+
fn pixel_size(&self) -> usize {
475+
let info = self.describe();
476+
match info.block_dimensions {
477+
(1, 1) => info.block_size.into(),
478+
_ => panic!("Using pixel_size for compressed textures is invalid"),
595479
}
596480
}
597481
}

0 commit comments

Comments
 (0)