Skip to content

Commit 6c1f466

Browse files
authored
default 16bit rgb/rgba textures to unorm instead of uint (#9611)
# Objective fix #8185, #6710 replace #7005 (closed) rgb and rgba 16 bit textures currently default to `Rgba16Uint`, the more common use is `Rgba16Unorm`, which also matches the default type of rgb8 and rgba8 textures. ## Solution Change default to `Rgba16Unorm`
1 parent 807d646 commit 6c1f466

File tree

2 files changed

+30
-44
lines changed

2 files changed

+30
-44
lines changed

crates/bevy_render/src/texture/dds.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub fn dds_format_to_texture_format(
8888
TextureFormat::Bc3RgbaUnorm
8989
}
9090
}
91-
D3DFormat::A16B16G16R16 => TextureFormat::Rgba16Uint,
91+
D3DFormat::A16B16G16R16 => TextureFormat::Rgba16Unorm,
9292
D3DFormat::Q16W16V16U16 => TextureFormat::Rgba16Sint,
9393
D3DFormat::R16F => TextureFormat::R16Float,
9494
D3DFormat::G16R16F => TextureFormat::Rg16Float,

crates/bevy_render/src/texture/image_texture_conversion.rs

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ impl Image {
1414
let format: TextureFormat;
1515

1616
match dyn_img {
17-
DynamicImage::ImageLuma8(i) => {
18-
let i = DynamicImage::ImageLuma8(i).into_rgba8();
17+
DynamicImage::ImageLuma8(image) => {
18+
let i = DynamicImage::ImageLuma8(image).into_rgba8();
1919
width = i.width();
2020
height = i.height();
2121
format = if is_srgb {
@@ -26,8 +26,8 @@ impl Image {
2626

2727
data = i.into_raw();
2828
}
29-
DynamicImage::ImageLumaA8(i) => {
30-
let i = DynamicImage::ImageLumaA8(i).into_rgba8();
29+
DynamicImage::ImageLumaA8(image) => {
30+
let i = DynamicImage::ImageLumaA8(image).into_rgba8();
3131
width = i.width();
3232
height = i.height();
3333
format = if is_srgb {
@@ -38,8 +38,8 @@ impl Image {
3838

3939
data = i.into_raw();
4040
}
41-
DynamicImage::ImageRgb8(i) => {
42-
let i = DynamicImage::ImageRgb8(i).into_rgba8();
41+
DynamicImage::ImageRgb8(image) => {
42+
let i = DynamicImage::ImageRgb8(image).into_rgba8();
4343
width = i.width();
4444
height = i.height();
4545
format = if is_srgb {
@@ -50,68 +50,54 @@ impl Image {
5050

5151
data = i.into_raw();
5252
}
53-
DynamicImage::ImageRgba8(i) => {
54-
width = i.width();
55-
height = i.height();
53+
DynamicImage::ImageRgba8(image) => {
54+
width = image.width();
55+
height = image.height();
5656
format = if is_srgb {
5757
TextureFormat::Rgba8UnormSrgb
5858
} else {
5959
TextureFormat::Rgba8Unorm
6060
};
6161

62-
data = i.into_raw();
62+
data = image.into_raw();
6363
}
64-
DynamicImage::ImageLuma16(i) => {
65-
width = i.width();
66-
height = i.height();
64+
DynamicImage::ImageLuma16(image) => {
65+
width = image.width();
66+
height = image.height();
6767
format = TextureFormat::R16Uint;
6868

69-
let raw_data = i.into_raw();
69+
let raw_data = image.into_raw();
7070

7171
data = cast_slice(&raw_data).to_owned();
7272
}
73-
DynamicImage::ImageLumaA16(i) => {
74-
width = i.width();
75-
height = i.height();
73+
DynamicImage::ImageLumaA16(image) => {
74+
width = image.width();
75+
height = image.height();
7676
format = TextureFormat::Rg16Uint;
7777

78-
let raw_data = i.into_raw();
78+
let raw_data = image.into_raw();
7979

8080
data = cast_slice(&raw_data).to_owned();
8181
}
8282
DynamicImage::ImageRgb16(image) => {
83-
width = image.width();
84-
height = image.height();
85-
format = TextureFormat::Rgba16Uint;
86-
87-
let mut local_data =
88-
Vec::with_capacity(width as usize * height as usize * format.pixel_size());
89-
90-
for pixel in image.into_raw().chunks_exact(3) {
91-
// TODO: use the array_chunks method once stabilised
92-
// https://github.com/rust-lang/rust/issues/74985
93-
let r = pixel[0];
94-
let g = pixel[1];
95-
let b = pixel[2];
96-
let a = u16::max_value();
97-
98-
local_data.extend_from_slice(&r.to_ne_bytes());
99-
local_data.extend_from_slice(&g.to_ne_bytes());
100-
local_data.extend_from_slice(&b.to_ne_bytes());
101-
local_data.extend_from_slice(&a.to_ne_bytes());
102-
}
103-
104-
data = local_data;
105-
}
106-
DynamicImage::ImageRgba16(i) => {
83+
let i = DynamicImage::ImageRgb16(image).into_rgba16();
10784
width = i.width();
10885
height = i.height();
109-
format = TextureFormat::Rgba16Uint;
86+
format = TextureFormat::Rgba16Unorm;
11087

11188
let raw_data = i.into_raw();
11289

11390
data = cast_slice(&raw_data).to_owned();
11491
}
92+
DynamicImage::ImageRgba16(image) => {
93+
width = image.width();
94+
height = image.height();
95+
format = TextureFormat::Rgba16Unorm;
96+
97+
let raw_data = image.into_raw();
98+
99+
data = cast_slice(&raw_data).to_owned();
100+
}
115101
DynamicImage::ImageRgb32F(image) => {
116102
width = image.width();
117103
height = image.height();

0 commit comments

Comments
 (0)