Skip to content

Commit

Permalink
Don't generate mip maps for RGB16F and RGB32F textures
Browse files Browse the repository at this point in the history
  • Loading branch information
asny committed Feb 26, 2024
1 parent e39c5e2 commit e4058ae
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/core/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,19 @@ fn set_parameters(
}
}

fn calculate_number_of_mip_maps(
fn calculate_number_of_mip_maps<T: TextureDataType>(
mip_map_filter: Option<Interpolation>,
width: u32,
height: u32,
depth: Option<u32>,
) -> u32 {
// Cannot generate mip maps for RGB16F or RGB32F textures on web (https://registry.khronos.org/webgl/extensions/EXT_color_buffer_float/)
if (T::data_type() == crate::context::FLOAT || T::data_type() == crate::context::HALF_FLOAT)
&& T::size() == 3
{
return 1;
}

if mip_map_filter.is_some() {
let max_size = width.max(height).max(depth.unwrap_or(0));
let power_of_two = max_size.next_power_of_two();
Expand Down
7 changes: 6 additions & 1 deletion src/core/texture/texture2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ impl Texture2D {
///
/// Construcs a new texture with the given data.
///
/// **Note:** Mip maps will not be generated for RGB16F and RGB32F format, even if `mip_map_filter` is specified.
///
pub fn new(context: &Context, cpu_texture: &CpuTexture) -> Self {
match cpu_texture.data {
TextureData::RU8(ref data) => Self::new_with_data(context, cpu_texture, data),
Expand Down Expand Up @@ -57,6 +59,8 @@ impl Texture2D {
/// The format is determined by the generic [TextureDataType] parameter
/// (for example, if [u8; 4] is specified, the format is RGBA and the data type is byte).
///
/// **Note:** Mip maps will not be generated for RGB16F and RGB32F format, even if `mip_map_filter` is specified.
///
pub fn new_empty<T: TextureDataType>(
context: &Context,
width: u32,
Expand All @@ -68,7 +72,8 @@ impl Texture2D {
wrap_t: Wrapping,
) -> Self {
let id = generate(context);
let number_of_mip_maps = calculate_number_of_mip_maps(mip_map_filter, width, height, None);
let number_of_mip_maps =
calculate_number_of_mip_maps::<T>(mip_map_filter, width, height, None);
let texture = Self {
context: context.clone(),
id,
Expand Down
7 changes: 6 additions & 1 deletion src/core/texture/texture2d_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ impl Texture2DArray {
/// Creates a new texture array from the given [CpuTexture]s.
/// All of the cpu textures must contain data with the same [TextureDataType] and the same width and height.
///
/// **Note:** Mip maps will not be generated for RGB16F and RGB32F format, even if `mip_map_filter` is specified.
///
pub fn new(context: &Context, cpu_textures: &[&CpuTexture]) -> Self {
let cpu_texture = cpu_textures
.get(0)
Expand Down Expand Up @@ -145,6 +147,8 @@ impl Texture2DArray {
///
/// Creates a new array of 2D textures.
///
/// **Note:** Mip maps will not be generated for RGB16F and RGB32F format, even if `mip_map_filter` is specified.
///
pub fn new_empty<T: TextureDataType>(
context: &Context,
width: u32,
Expand All @@ -157,7 +161,8 @@ impl Texture2DArray {
wrap_t: Wrapping,
) -> Self {
let id = generate(context);
let number_of_mip_maps = calculate_number_of_mip_maps(mip_map_filter, width, height, None);
let number_of_mip_maps =
calculate_number_of_mip_maps::<T>(mip_map_filter, width, height, None);
let texture = Self {
context: context.clone(),
id,
Expand Down
6 changes: 5 additions & 1 deletion src/core/texture/texture3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ impl Texture3D {
///
/// Construcs a new 3D texture with the given data.
///
/// **Note:** Mip maps will not be generated for RGB16F and RGB32F format, even if `mip_map_filter` is specified.
///
pub fn new(context: &Context, cpu_texture: &CpuTexture3D) -> Self {
match cpu_texture.data {
TextureData::RU8(ref data) => Self::new_with_data(context, cpu_texture, data),
Expand Down Expand Up @@ -57,6 +59,8 @@ impl Texture3D {
///
/// Creates a new empty 3D color texture.
///
/// **Note:** Mip maps will not be generated for RGB16F and RGB32F format, even if `mip_map_filter` is specified.
///
pub fn new_empty<T: TextureDataType>(
context: &Context,
width: u32,
Expand All @@ -71,7 +75,7 @@ impl Texture3D {
) -> Self {
let id = generate(context);
let number_of_mip_maps =
calculate_number_of_mip_maps(mip_map_filter, width, height, Some(depth));
calculate_number_of_mip_maps::<T>(mip_map_filter, width, height, Some(depth));
let texture = Self {
context: context.clone(),
id,
Expand Down
7 changes: 6 additions & 1 deletion src/core/texture/texture_cube_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ impl TextureCubeMap {
/// Creates a new cube map texture from the given [CpuTexture]s.
/// All of the cpu textures must contain data with the same [TextureDataType].
///
/// **Note:** Mip maps will not be generated for RGB16F and RGB32F format, even if `mip_map_filter` is specified.
///
pub fn new(
context: &Context,
right: &CpuTexture,
Expand Down Expand Up @@ -299,6 +301,8 @@ impl TextureCubeMap {
///
/// Creates a new texture cube map.
///
/// **Note:** Mip maps will not be generated for RGB16F and RGB32F format, even if `mip_map_filter` is specified.
///
pub fn new_empty<T: TextureDataType>(
context: &Context,
width: u32,
Expand All @@ -311,7 +315,8 @@ impl TextureCubeMap {
wrap_r: Wrapping,
) -> Self {
let id = generate(context);
let number_of_mip_maps = calculate_number_of_mip_maps(mip_map_filter, width, height, None);
let number_of_mip_maps =
calculate_number_of_mip_maps::<T>(mip_map_filter, width, height, None);
let texture = Self {
context: context.clone(),
id,
Expand Down

0 comments on commit e4058ae

Please sign in to comment.