ispc_texcomp/
bc5.rs

1use more_asserts::assert_ge;
2
3use crate::bindings::kernel;
4use crate::RgbaSurface;
5
6#[inline(always)]
7pub fn calc_output_size(width: u32, height: u32) -> usize {
8    // BC5 uses 16 bytes to store each 4×4 block, giving it an average data rate of 1 byte per pixel.
9    let block_count = crate::cal_block_count(width, height, 4, 4) as usize;
10    block_count * 16
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
26    let mut rg_data = vec![0_u8; (surface.width * surface.height * 2) as usize];
27    let pitch = (surface.width * 32 + 7) / 8;
28    let mut offset = 0_u32;
29
30    for y in 0..surface.height {
31        for x in 0..surface.width {
32            // Copy R and G bytes over
33            rg_data[offset as usize] = surface.data[(x * 4 + y * pitch) as usize];
34            rg_data[(offset + 1) as usize] = surface.data[(x * 4 + y * pitch + 1) as usize];
35            offset += 2;
36        }
37    }
38
39    let mut surface = kernel::rgba_surface {
40        width: surface.width as i32,
41        height: surface.height as i32,
42        stride: (surface.stride / 2) as i32,
43        ptr: (&rg_data).as_ptr() as *mut u8,
44    };
45
46    unsafe {
47        kernel::CompressBlocksBC5_ispc(&mut surface, blocks.as_mut_ptr());
48    }
49}