ispc_texcomp/
bc1.rs

1use more_asserts::assert_ge;
2
3use crate::bindings::kernel;
4use crate::{cal_block_count, RgbaSurface};
5
6#[inline(always)]
7pub fn calc_output_size(width: u32, height: u32) -> usize {
8    // BC1 uses 8 bytes to store each 4×4 block, giving it an average data rate of 0.5 bytes per pixel.
9    let block_count = cal_block_count(width, height, 4, 4) as usize;
10    block_count * 8
11}
12
13pub fn compress_blocks(surface: &RgbaSurface) -> Vec<u8> {
14    let output_size = calc_output_size(surface.width, surface.height);
15    let mut output = vec![0u8; output_size];
16    compress_blocks_into(surface, &mut output);
17    output
18}
19
20pub fn compress_blocks_into(surface: &RgbaSurface, blocks: &mut [u8]) {
21    assert_ge!(
22        blocks.len(),
23        calc_output_size(surface.width, surface.height)
24    );
25    let mut surface = kernel::rgba_surface {
26        width: surface.width as i32,
27        height: surface.height as i32,
28        stride: surface.stride as i32,
29        ptr: surface.data.as_ptr() as *mut u8,
30    };
31
32    unsafe {
33        kernel::CompressBlocksBC1_ispc(&mut surface, blocks.as_mut_ptr());
34    }
35}