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

default 16bit rgb/rgba textures to unorm instead of uint #9611

Merged
merged 2 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion crates/bevy_render/src/texture/dds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub fn dds_format_to_texture_format(
TextureFormat::Bc3RgbaUnorm
}
}
D3DFormat::A16B16G16R16 => TextureFormat::Rgba16Uint,
D3DFormat::A16B16G16R16 => TextureFormat::Rgba16Unorm,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked the microsoft docs and it seems like A16B16G16R16 is an old dx9 format and the dx11 equivalent is DXGI_FORMAT_R16G16B16A16_UNORM, so this seems correct.

D3DFormat::Q16W16V16U16 => TextureFormat::Rgba16Sint,
D3DFormat::R16F => TextureFormat::R16Float,
D3DFormat::G16R16F => TextureFormat::Rg16Float,
Expand Down
30 changes: 8 additions & 22 deletions crates/bevy_render/src/texture/image_texture_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,34 +79,20 @@ impl Image {

data = cast_slice(&raw_data).to_owned();
}
DynamicImage::ImageRgb16(image) => {
width = image.width();
height = image.height();
format = TextureFormat::Rgba16Uint;

let mut local_data =
Vec::with_capacity(width as usize * height as usize * format.pixel_size());

for pixel in image.into_raw().chunks_exact(3) {
// TODO: use the array_chunks method once stabilised
// https://github.com/rust-lang/rust/issues/74985
let r = pixel[0];
let g = pixel[1];
let b = pixel[2];
let a = u16::max_value();
DynamicImage::ImageRgb16(i) => {
robtfm marked this conversation as resolved.
Show resolved Hide resolved
let i = DynamicImage::ImageRgb16(i).into_rgba16();
width = i.width();
height = i.height();
format = TextureFormat::Rgba16Unorm;

local_data.extend_from_slice(&r.to_ne_bytes());
local_data.extend_from_slice(&g.to_ne_bytes());
local_data.extend_from_slice(&b.to_ne_bytes());
local_data.extend_from_slice(&a.to_ne_bytes());
}
let raw_data = i.into_raw();

data = local_data;
data = cast_slice(&raw_data).to_owned();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this potentially be in an unsafe block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean make Image::from_dynamic unsafe? i don't think it's necessary, the worst that'll happen is garbage pixeldata in your image.

}
DynamicImage::ImageRgba16(i) => {
width = i.width();
height = i.height();
format = TextureFormat::Rgba16Uint;
format = TextureFormat::Rgba16Unorm;

let raw_data = i.into_raw();

Expand Down