block_compression 0.9.0

Texture block compression using WGPU compute shader
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! CPU based decoding.

#[cfg(any(feature = "bc15", feature = "bc6h", feature = "bc7"))]
mod block;

#[cfg(feature = "bc7")]
#[cfg_attr(docsrs, doc(cfg(feature = "bc7")))]
pub use self::block::decode_block_bc7;
#[cfg(feature = "bc15")]
#[cfg_attr(docsrs, doc(cfg(feature = "bc15")))]
pub use self::block::{
    decode_block_bc1, decode_block_bc2, decode_block_bc3, decode_block_bc4, decode_block_bc5,
};
#[cfg(feature = "bc6h")]
#[cfg_attr(docsrs, doc(cfg(feature = "bc6h")))]
pub use self::block::{decode_block_bc6h, decode_block_bc6h_float};
#[cfg(feature = "bc6h")]
use crate::BC6HSettings;
#[cfg(feature = "bc7")]
use crate::BC7Settings;
#[cfg(any(feature = "bc15", feature = "bc6h", feature = "bc7"))]
use crate::CompressionVariant;

/// Trait to decode a BC variant into RGBA8 data.
#[cfg(any(feature = "bc15", feature = "bc6h", feature = "bc7"))]
trait BlockRgba8Decoder {
    fn decode_block_rgba8(compressed: &[u8], decompressed: &mut [u8], pitch: usize);
    fn block_byte_size() -> u32;
}

/// Trait to decode a BC variant into RGBA16F data.
#[cfg(feature = "bc6h")]
trait BlockRgba16fDecoder {
    fn decode_block_rgba16f(compressed: &[u8], decompressed: &mut [half::f16], pitch: usize);
    fn block_byte_size() -> u32;
}

/// Trait to decode a BC variant into RGBA32F data.
#[cfg(feature = "bc6h")]
trait BlockRgba32fDecoder {
    fn decode_block_rgba32f(compressed: &[u8], decompressed: &mut [f32], pitch: usize);
    fn block_byte_size() -> u32;
}

#[cfg(feature = "bc15")]
struct BC1Decoder;
#[cfg(feature = "bc15")]
struct BC2Decoder;
#[cfg(feature = "bc15")]
struct BC3Decoder;
#[cfg(feature = "bc15")]
struct BC4Decoder;
#[cfg(feature = "bc15")]
struct BC5Decoder;
#[cfg(feature = "bc6h")]
struct BC6HDecoder;
#[cfg(feature = "bc7")]
struct BC7Decoder;

#[cfg(feature = "bc15")]
impl BlockRgba8Decoder for BC1Decoder {
    #[inline(always)]
    fn decode_block_rgba8(compressed: &[u8], decompressed: &mut [u8], pitch: usize) {
        decode_block_bc1(compressed, decompressed, pitch)
    }

    fn block_byte_size() -> u32 {
        CompressionVariant::BC1.block_byte_size()
    }
}

#[cfg(feature = "bc15")]
impl BlockRgba8Decoder for BC2Decoder {
    #[inline(always)]
    fn decode_block_rgba8(compressed: &[u8], decompressed: &mut [u8], pitch: usize) {
        decode_block_bc2(compressed, decompressed, pitch)
    }

    fn block_byte_size() -> u32 {
        CompressionVariant::BC2.block_byte_size()
    }
}

#[cfg(feature = "bc15")]
impl BlockRgba8Decoder for BC3Decoder {
    #[inline(always)]
    fn decode_block_rgba8(compressed: &[u8], decompressed: &mut [u8], pitch: usize) {
        decode_block_bc3(compressed, decompressed, pitch)
    }

    fn block_byte_size() -> u32 {
        CompressionVariant::BC3.block_byte_size()
    }
}

#[cfg(feature = "bc15")]
impl BlockRgba8Decoder for BC4Decoder {
    #[inline(always)]
    fn decode_block_rgba8(compressed: &[u8], decompressed: &mut [u8], pitch: usize) {
        const PITCH: usize = 4;
        let mut buffer = [0u8; 16];
        decode_block_bc4(compressed, &mut buffer, 4);

        // Convert R8 to RGBA8
        for y in 0..4 {
            for x in 0..4 {
                let out_pos = y * pitch + x * 4;
                let in_pos = y * PITCH + x;

                decompressed[out_pos] = buffer[in_pos];
                decompressed[out_pos + 1] = 0;
                decompressed[out_pos + 2] = 0;
                decompressed[out_pos + 3] = 0;
            }
        }
    }

    fn block_byte_size() -> u32 {
        CompressionVariant::BC4.block_byte_size()
    }
}

#[cfg(feature = "bc15")]
impl BlockRgba8Decoder for BC5Decoder {
    #[inline(always)]
    fn decode_block_rgba8(compressed: &[u8], decompressed: &mut [u8], pitch: usize) {
        const PITCH: usize = 8;
        let mut buffer = [0u8; 32];
        decode_block_bc5(compressed, &mut buffer, PITCH);

        // Convert RG8 to RGBA8
        for y in 0..4 {
            for x in 0..4 {
                let out_pos = y * pitch + x * 4;
                let in_pos = y * PITCH + x * 2;

                decompressed[out_pos] = buffer[in_pos];
                decompressed[out_pos + 1] = buffer[in_pos + 1];
                decompressed[out_pos + 2] = 0;
                decompressed[out_pos + 3] = 0;
            }
        }
    }

    fn block_byte_size() -> u32 {
        CompressionVariant::BC5.block_byte_size()
    }
}

#[cfg(feature = "bc6h")]
fn linear_to_srgb(linear: f32) -> u8 {
    let v = if linear <= 0.0031308 {
        linear * 12.92
    } else {
        1.055 * linear.powf(1.0 / 2.4) - 0.055
    };

    (v.clamp(0.0, 1.0) * 255.0).round() as u8
}

#[cfg(feature = "bc6h")]
impl BlockRgba8Decoder for BC6HDecoder {
    #[inline(always)]
    fn decode_block_rgba8(compressed: &[u8], decompressed: &mut [u8], pitch: usize) {
        const PITCH: usize = 12;
        let mut buffer = [0.0_f32; 48];
        decode_block_bc6h_float(compressed, &mut buffer, PITCH, false);

        // Convert RGB16F to RGBA8
        for y in 0..4 {
            for x in 0..4 {
                let out_pos = y * pitch + x * 4;
                let in_pos = y * PITCH + x * 3;

                decompressed[out_pos] = linear_to_srgb(buffer[in_pos]) as _;
                decompressed[out_pos + 1] = linear_to_srgb(buffer[in_pos + 1]) as _;
                decompressed[out_pos + 2] = linear_to_srgb(buffer[in_pos + 2]) as _;
                decompressed[out_pos + 3] = 0;
            }
        }
    }

    fn block_byte_size() -> u32 {
        CompressionVariant::BC6H(BC6HSettings::basic()).block_byte_size()
    }
}

#[cfg(feature = "bc7")]
impl BlockRgba8Decoder for BC7Decoder {
    #[inline(always)]
    fn decode_block_rgba8(compressed: &[u8], decompressed: &mut [u8], pitch: usize) {
        decode_block_bc7(compressed, decompressed, pitch)
    }

    fn block_byte_size() -> u32 {
        CompressionVariant::BC7(BC7Settings::alpha_basic()).block_byte_size()
    }
}

#[cfg(any(feature = "bc15", feature = "bc6h", feature = "bc7"))]
fn decompress_rgba8<D: BlockRgba8Decoder>(
    width: u32,
    height: u32,
    blocks_data: &[u8],
    rgba_data: &mut [u8],
) {
    let blocks_x = width.div_ceil(4);
    let blocks_y = height.div_ceil(4);
    let block_byte_size = D::block_byte_size() as usize;
    let output_row_pitch = width as usize * 4; // Always RGBA

    for by in 0..blocks_y {
        for bx in 0..blocks_x {
            let block_index = (by * blocks_x + bx) as usize;
            let block_offset = block_index * block_byte_size;

            if block_offset + block_byte_size > blocks_data.len() {
                break;
            }

            let output_offset = (by * 4 * output_row_pitch as u32 + bx * 16) as usize;

            if output_offset < rgba_data.len() {
                D::decode_block_rgba8(
                    &blocks_data[block_offset..block_offset + block_byte_size],
                    &mut rgba_data[output_offset..],
                    output_row_pitch,
                );
            }
        }
    }
}

#[cfg(feature = "bc6h")]
impl BlockRgba16fDecoder for BC6HDecoder {
    #[inline(always)]
    fn decode_block_rgba16f(compressed: &[u8], decompressed: &mut [half::f16], pitch: usize) {
        decode_block_bc6h(compressed, decompressed, pitch, false);
    }

    fn block_byte_size() -> u32 {
        CompressionVariant::BC6H(BC6HSettings::basic()).block_byte_size()
    }
}

#[cfg(feature = "bc6h")]
fn decompress_rgba16f<D: BlockRgba16fDecoder>(
    width: u32,
    height: u32,
    blocks_data: &[u8],
    rgba_data: &mut [half::f16],
) {
    let blocks_x = width.div_ceil(4);
    let blocks_y = height.div_ceil(4);
    let block_byte_size = D::block_byte_size() as usize;
    let output_row_pitch = width as usize * 4; // Always RGBA16f

    for by in 0..blocks_y {
        for bx in 0..blocks_x {
            let block_index = (by * blocks_x + bx) as usize;
            let block_offset = block_index * block_byte_size;

            if block_offset + block_byte_size > blocks_data.len() {
                break;
            }

            let output_offset = (by * 4 * output_row_pitch as u32 + bx * 16) as usize;

            if output_offset < rgba_data.len() {
                D::decode_block_rgba16f(
                    &blocks_data[block_offset..block_offset + block_byte_size],
                    &mut rgba_data[output_offset..],
                    output_row_pitch,
                );
            }
        }
    }
}

#[cfg(feature = "bc6h")]
impl BlockRgba32fDecoder for BC6HDecoder {
    #[inline(always)]
    fn decode_block_rgba32f(compressed: &[u8], decompressed: &mut [f32], pitch: usize) {
        decode_block_bc6h_float(compressed, decompressed, pitch, false);
    }

    fn block_byte_size() -> u32 {
        CompressionVariant::BC6H(BC6HSettings::basic()).block_byte_size()
    }
}

#[cfg(feature = "bc6h")]
fn decompress_rgba32f<D: BlockRgba32fDecoder>(
    width: u32,
    height: u32,
    blocks_data: &[u8],
    rgba_data: &mut [f32],
) {
    let blocks_x = width.div_ceil(4);
    let blocks_y = height.div_ceil(4);
    let block_byte_size = D::block_byte_size() as usize;
    let output_row_pitch = width as usize * 4; // Always RGBA32f

    for by in 0..blocks_y {
        for bx in 0..blocks_x {
            let block_index = (by * blocks_x + bx) as usize;
            let block_offset = block_index * block_byte_size;

            if block_offset + block_byte_size > blocks_data.len() {
                break;
            }

            let output_offset = (by * 4 * output_row_pitch as u32 + bx * 16) as usize;

            if output_offset < rgba_data.len() {
                D::decode_block_rgba32f(
                    &blocks_data[block_offset..block_offset + block_byte_size],
                    &mut rgba_data[output_offset..],
                    output_row_pitch,
                );
            }
        }
    }
}

/// Helper function to easily decompress block data into RGBA8 data.
///
/// # Panics
/// - The `blocks_data` has not the expected size (`variant.blocks_byte_size()`)
/// - The `rgba_data` has not the expected size (`width * height * 4`)
#[cfg(any(feature = "bc15", feature = "bc6h", feature = "bc7"))]
#[cfg_attr(
    docsrs,
    doc(cfg(any(feature = "bc15", feature = "bc6h", feature = "bc7")))
)]
pub fn decompress_blocks_as_rgba8(
    variant: CompressionVariant,
    width: u32,
    height: u32,
    blocks_data: &[u8],
    rgba_data: &mut [u8],
) {
    let expected_input_size = variant.blocks_byte_size(width, height);
    assert_eq!(
        blocks_data.len(),
        expected_input_size,
        "the input bitstream slice has not the expected size"
    );

    let expected_output_size = width as usize * height as usize * 4;
    assert_eq!(
        rgba_data.len(),
        expected_output_size,
        "the output slice has not the expected size"
    );

    match variant {
        #[cfg(feature = "bc15")]
        CompressionVariant::BC1 => {
            decompress_rgba8::<BC1Decoder>(width, height, blocks_data, rgba_data)
        }
        #[cfg(feature = "bc15")]
        CompressionVariant::BC2 => {
            decompress_rgba8::<BC2Decoder>(width, height, blocks_data, rgba_data)
        }
        #[cfg(feature = "bc15")]
        CompressionVariant::BC3 => {
            decompress_rgba8::<BC3Decoder>(width, height, blocks_data, rgba_data)
        }
        #[cfg(feature = "bc15")]
        CompressionVariant::BC4 => {
            decompress_rgba8::<BC4Decoder>(width, height, blocks_data, rgba_data)
        }
        #[cfg(feature = "bc15")]
        CompressionVariant::BC5 => {
            decompress_rgba8::<BC5Decoder>(width, height, blocks_data, rgba_data)
        }
        #[cfg(feature = "bc6h")]
        CompressionVariant::BC6H(..) => {
            decompress_rgba8::<BC6HDecoder>(width, height, blocks_data, rgba_data)
        }
        #[cfg(feature = "bc7")]
        CompressionVariant::BC7(..) => {
            decompress_rgba8::<BC7Decoder>(width, height, blocks_data, rgba_data)
        }
    }
}

/// Helper function to easily decompress block data into RGBA16F data. Only BCH6 is currently supported.
///
/// # Panics
/// - The `blocks_data` has not the expected size (`variant.blocks_byte_size()`)
/// - The `rgba_data` has not the expected size (`width * height * 4`)
/// - If `variant` is any other value than BC6H.
#[cfg(feature = "bc6h")]
#[cfg_attr(docsrs, doc(cfg(feature = "bc6h")))]
pub fn decompress_blocks_as_rgba16f(
    variant: CompressionVariant,
    width: u32,
    height: u32,
    blocks_data: &[u8],
    rgba_data: &mut [half::f16],
) {
    let expected_input_size = variant.blocks_byte_size(width, height);

    assert_eq!(
        blocks_data.len(),
        expected_input_size,
        "the input bitstream slice has not the expected size"
    );

    let expected_output_size = width as usize * height as usize * 4;
    assert_eq!(
        rgba_data.len(),
        expected_output_size,
        "the output slice has not the expected size"
    );

    match variant {
        CompressionVariant::BC6H(..) => {
            decompress_rgba16f::<BC6HDecoder>(width, height, blocks_data, rgba_data)
        }
        #[allow(unreachable_patterns)]
        _ => {
            panic!("unsupported compression variant");
        }
    }
}

/// Helper function to easily decompress block data into RGBA32F data. Only BCH6 is currently supported.
///
/// # Panics
/// - The `blocks_data` has not the expected size (`variant.blocks_byte_size()`)
/// - The `rgba_data` has not the expected size (`width * height * 4`)
/// - If `variant` is any other value than BC6H.
#[cfg(feature = "bc6h")]
#[cfg_attr(docsrs, doc(cfg(feature = "bc6h")))]
pub fn decompress_blocks_as_rgba32f(
    variant: CompressionVariant,
    width: u32,
    height: u32,
    blocks_data: &[u8],
    rgba_data: &mut [f32],
) {
    let expected_input_size = variant.blocks_byte_size(width, height);
    assert_eq!(
        blocks_data.len(),
        expected_input_size,
        "the input bitstream slice has not the expected size"
    );

    let expected_output_size = width as usize * height as usize * 4;
    assert_eq!(
        rgba_data.len(),
        expected_output_size,
        "the output slice has not the expected size"
    );

    match variant {
        CompressionVariant::BC6H(..) => {
            decompress_rgba32f::<BC6HDecoder>(width, height, blocks_data, rgba_data)
        }
        #[allow(unreachable_patterns)]
        _ => {
            panic!("unsupported compression variant");
        }
    }
}