Skip to content

Commit

Permalink
Add integer texture support, for creating render targets
Browse files Browse the repository at this point in the history
  • Loading branch information
thatcomputerguy0101 committed Oct 1, 2024
1 parent ee83d80 commit 973bce5
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/core/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,13 @@ fn set_parameters(
}
}

fn calculate_number_of_mip_maps<T: TextureDataType>(
fn calculate_number_of_mip_maps<T: DataType>(
mip_map_filter: Option<Interpolation>,
width: u32,
height: u32,
depth: Option<u32>,
) -> u32 {
// Cannot generate mip maps for RGB textures using non-normalized data formats on web (OpenGL ES3.0 Table 3.13, https://registry.khronos.org/webgl/extensions/EXT_color_buffer_float/)
// Cannot generate mip maps for RGB textures using non-byte data formats on web (OpenGL ES3.0 Table 3.13, https://registry.khronos.org/webgl/extensions/EXT_color_buffer_float/)
if T::size() == 3 && T::data_type() != crate::context::UNSIGNED_BYTE {
return 1;
}
Expand Down Expand Up @@ -389,7 +389,7 @@ fn interpolation_from(interpolation: Interpolation) -> i32 {
}) as i32
}

fn check_data_length<T: TextureDataType>(
fn check_data_length<T: DataType>(
width: u32,
height: u32,
depth: u32,
Expand Down
19 changes: 17 additions & 2 deletions src/core/texture/texture2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Texture2D {
///
/// **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>(
pub fn new_empty<T: BufferDataType>(
context: &Context,
width: u32,
height: u32,
Expand Down Expand Up @@ -118,6 +118,21 @@ impl Texture2D {
/// It is therefore necessary to create a new texture if the texture size or format has changed.
///
pub fn fill<T: TextureDataType>(&mut self, data: &[T]) {
self.fill_format(data, normalized_format_from_data_type::<T>())
}

///
/// Fills this texture with the given data, using the given data type.
///
/// # Panic
/// Will panic if the length of the data does not correspond to the width, height and format specified at construction.
/// It is therefore necessary to create a new texture if the texture size or format has changed.
///
pub fn fill_buffer<T: BufferDataType>(&mut self, data: &[T]) {
self.fill_format(data, format_from_data_type::<T>())
}

fn fill_format<T: BufferDataType>(&mut self, data: &[T], format: u32) {
check_data_length::<T>(self.width, self.height, 1, self.data_byte_size, data.len());
self.bind();
let mut data = data.to_owned();
Expand All @@ -130,7 +145,7 @@ impl Texture2D {
0,
self.width as i32,
self.height as i32,
normalized_format_from_data_type::<T>(),
format,
T::data_type(),
crate::context::PixelUnpackData::Slice(to_byte_slice(&data)),
);
Expand Down
30 changes: 29 additions & 1 deletion src/core/texture/texture2d_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,19 @@ impl Texture2DArray {
}
}

///
/// Fills the texture array with the given pixel data in the given format.
///
/// # Panic
/// Will panic if the data does not correspond to the width, height, depth and format specified at construction.
/// It is therefore necessary to create a new texture if the texture size or format has changed.
///
pub fn fill_buffer<T: BufferDataType>(&mut self, data: &[&[T]]) {
for (i, data) in data.iter().enumerate() {
self.fill_layer_buffer(i as u32, data);
}
}

///
/// Fills the given layer in the texture array with the given pixel data.
///
Expand All @@ -222,6 +235,21 @@ impl Texture2DArray {
/// It is therefore necessary to create a new texture if the texture size or format has changed.
///
pub fn fill_layer<T: TextureDataType>(&mut self, layer: u32, data: &[T]) {
self.fill_layer_format(layer, data, normalized_format_from_data_type::<T>());
}

///
/// Fills the given layer in the texture array with the given pixel data in the given format.
///
/// # Panic
/// Will panic if the layer number is bigger than the number of layers or if the data does not correspond to the width, height and format specified at construction.
/// It is therefore necessary to create a new texture if the texture size or format has changed.
///
pub fn fill_layer_buffer<T: BufferDataType>(&mut self, layer: u32, data: &[T]) {
self.fill_layer_format(layer, data, format_from_data_type::<T>());
}

fn fill_layer_format<T: BufferDataType>(&mut self, layer: u32, data: &[T], format: u32) {
if layer >= self.depth {
panic!(
"cannot fill the layer {} with data, since there are only {} layers in the texture array",
Expand All @@ -242,7 +270,7 @@ impl Texture2DArray {
self.width as i32,
self.height as i32,
1,
normalized_format_from_data_type::<T>(),
format,
T::data_type(),
crate::context::PixelUnpackData::Slice(to_byte_slice(&data)),
);
Expand Down
2 changes: 1 addition & 1 deletion src/core/texture/texture2d_multisample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Texture2DMultisample {
}

impl Texture2DMultisample {
pub fn new<T: TextureDataType>(
pub fn new<T: BufferDataType>(
context: &Context,
width: u32,
height: u32,
Expand Down
19 changes: 17 additions & 2 deletions src/core/texture/texture3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Texture3D {
///
/// **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>(
pub fn new_empty<T: BufferDataType>(
context: &Context,
width: u32,
height: u32,
Expand Down Expand Up @@ -122,6 +122,21 @@ impl Texture3D {
/// It is therefore necessary to create a new texture if the texture size or format has changed.
///
pub fn fill<T: TextureDataType>(&mut self, data: &[T]) {
self.fill_format(data, normalized_format_from_data_type::<T>());
}

///
/// Fills this texture with the given data in the given data format.
///
/// # Panic
/// Will panic if the length of the data does not correspond to the width, height, depth and format specified at construction.
/// It is therefore necessary to create a new texture if the texture size or format has changed.
///
pub fn fill_buffer<T: TextureDataType>(&mut self, data: &[T]) {
self.fill_format(data, format_from_data_type::<T>());
}

fn fill_format<T: TextureDataType>(&mut self, data: &[T], format: u32) {
check_data_length::<T>(
self.width,
self.height,
Expand All @@ -140,7 +155,7 @@ impl Texture3D {
self.width as i32,
self.height as i32,
self.depth as i32,
normalized_format_from_data_type::<T>(),
format,
T::data_type(),
crate::context::PixelUnpackData::Slice(to_byte_slice(data)),
);
Expand Down
36 changes: 34 additions & 2 deletions src/core/texture/texture_cube_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ impl TextureCubeMap {
///
/// **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>(
pub fn new_empty<T: BufferDataType>(
context: &Context,
width: u32,
height: u32,
Expand Down Expand Up @@ -368,6 +368,38 @@ impl TextureCubeMap {
bottom_data: &[T],
front_data: &[T],
back_data: &[T],
) {
self.fill_format(right_data, left_data, top_data, bottom_data, front_data, back_data, normalized_format_from_data_type::<T>());
}

///
/// Fills the cube map texture with the given pixel data in the given format for the 6 images.
///
/// # Panic
/// Will panic if the length of the data for all 6 images does not correspond to the width, height and format specified at construction.
/// It is therefore necessary to create a new texture if the texture size or format has changed.
///
pub fn fill_buffer<T: BufferDataType>(
&mut self,
right_data: &[T],
left_data: &[T],
top_data: &[T],
bottom_data: &[T],
front_data: &[T],
back_data: &[T],
) {
self.fill_format(right_data, left_data, top_data, bottom_data, front_data, back_data, format_from_data_type::<T>());
}

fn fill_format<T: BufferDataType>(
&mut self,
right_data: &[T],
left_data: &[T],
top_data: &[T],
bottom_data: &[T],
front_data: &[T],
back_data: &[T],
format: u32,
) {
check_data_length::<T>(
self.width,
Expand Down Expand Up @@ -430,7 +462,7 @@ impl TextureCubeMap {
0,
self.width as i32,
self.height as i32,
normalized_format_from_data_type::<T>(),
format,
T::data_type(),
crate::context::PixelUnpackData::Slice(to_byte_slice(data)),
);
Expand Down

0 comments on commit 973bce5

Please sign in to comment.