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
//! CPU based encoding.

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

#[cfg(feature = "bc15")]
use self::bc1_to_5::BlockCompressorBC15;
#[cfg(feature = "bc6h")]
use self::bc6h::BlockCompressorBC6H;
#[cfg(feature = "bc7")]
use self::bc7::BlockCompressorBC7;
#[cfg(feature = "bc6h")]
use crate::BC6HSettings;
#[cfg(feature = "bc7")]
use crate::BC7Settings;
#[cfg(any(feature = "bc15", feature = "bc6h", feature = "bc7"))]
use crate::CompressionVariant;

/// Compresses raw RGBA8 data into using a texture block compression format.
///
/// It supports BC1 through BC7 compression formats and provides CPU-based texture compression
/// for RGBA8 data.
///
/// # Data Layout Requirements
/// The input data must be in RGBA8 format (8 bits per channel, 32 bits per pixel). The data is
/// expected to be in row-major order, with optional stride for padding between rows.
///
/// # Buffer Requirements
/// The destination buffer must have sufficient capacity to store the compressed blocks.
/// The required size can be calculated using [`CompressionVariant::blocks_byte_size()`].
///
/// For example:
/// ```ignore
/// let required_size = variant.blocks_byte_size(width, height);
/// assert!(blocks_buffer.len() >= required_size);
/// ```
///
/// # Arguments
/// * `variation` - The block compression format to use
/// * `rgba_data` - Source RGBA8 pixel data
/// * `blocks_buffer` - Destination buffer for the compressed blocks
/// * `width` - Width of the image in pixels
/// * `height` - Height of the image in pixels
/// * `stride` - Number of bytes per row in the source data (for padding).
///   Must be `width * 4` for tightly packed RGBA data.
///
/// # Panics
/// * If `width` or `height` is not a multiple of 4
/// * If the destination `blocks_buffer` is too small to hold the compressed data
///
/// # Example
/// ```
/// use block_compression::{encode::compress_rgba8, CompressionVariant};
///
/// let rgba_data = vec![0u8; 256 * 256 * 4]; // Your RGBA data
/// let width = 256;
/// let height = 256;
/// let stride = width * 4; // Tightly packed rows
/// let variant = CompressionVariant::BC1;
///
/// let mut blocks_buffer = vec![0u8; variant.blocks_byte_size(width, height)];
///
/// compress_rgba8(
///     variant,
///     &rgba_data,
///     &mut blocks_buffer,
///     width,
///     height,
///     stride,
/// );
/// ```
#[cfg(any(feature = "bc15", feature = "bc6h", feature = "bc7"))]
#[cfg_attr(
    docsrs,
    doc(cfg(any(feature = "bc15", feature = "bc6h", feature = "bc7")))
)]
pub fn compress_rgba8(
    variation: CompressionVariant,
    rgba_data: &[u8],
    blocks_buffer: &mut [u8],
    width: u32,
    height: u32,
    stride: u32,
) {
    assert_eq!(height % 4, 0);
    assert_eq!(width % 4, 0);

    let required_size = variation.blocks_byte_size(width, height);

    assert!(
        blocks_buffer.len() >= required_size,
        "blocks_buffer size ({}) is too small to hold compressed blocks. Required size: {}",
        blocks_buffer.len(),
        required_size
    );

    let stride = stride as usize;
    let block_width = (width as usize).div_ceil(4);
    let block_height = (height as usize).div_ceil(4);

    match variation {
        #[cfg(feature = "bc15")]
        CompressionVariant::BC1 => {
            compress_bc1(rgba_data, blocks_buffer, block_width, block_height, stride);
        }
        #[cfg(feature = "bc15")]
        CompressionVariant::BC2 => {
            compress_bc2(rgba_data, blocks_buffer, block_width, block_height, stride);
        }
        #[cfg(feature = "bc15")]
        CompressionVariant::BC3 => {
            compress_bc3(rgba_data, blocks_buffer, block_width, block_height, stride);
        }
        #[cfg(feature = "bc15")]
        CompressionVariant::BC4 => {
            compress_bc4(rgba_data, blocks_buffer, block_width, block_height, stride);
        }
        #[cfg(feature = "bc15")]
        CompressionVariant::BC5 => {
            compress_bc5(rgba_data, blocks_buffer, block_width, block_height, stride);
        }
        #[cfg(feature = "bc6h")]
        CompressionVariant::BC6H(settings) => {
            compress_bc6h_8bit(
                rgba_data,
                blocks_buffer,
                block_width,
                block_height,
                stride,
                &settings,
            );
        }
        #[cfg(feature = "bc7")]
        CompressionVariant::BC7(settings) => {
            compress_bc7(
                rgba_data,
                blocks_buffer,
                block_width,
                block_height,
                stride,
                &settings,
            );
        }
    }
}

/// Compresses raw RGBA16 (half-float) data using the BC6H texture block compression format.
///
/// It supports only BC6H compression format and provides CPU-based texture compression
/// for RGBA16 (half-float) data.
///
/// # Data Layout Requirements
/// The input data must be in RGBA16 format (16 bits per channel using half-float). The data is
/// expected to be in row-major order, with optional stride for padding between rows.
///
/// # Buffer Requirements
/// The destination buffer must have sufficient capacity to store the compressed blocks.
/// The required size can be calculated using [`CompressionVariant::blocks_byte_size()`].
///
/// For example:
/// ```ignore
/// let required_size = variant.blocks_byte_size(width, height);
/// assert!(blocks_buffer.len() >= required_size);
/// ```
///
/// # Arguments
/// * `variation` - The block compression format to use (must be BC6H)
/// * `rgb_data` - Source RGBA16 pixel data in half-float format
/// * `blocks_buffer` - Destination buffer for the compressed blocks
/// * `width` - Width of the image in pixels
/// * `height` - Height of the image in pixels
/// * `stride` - Number of half-float elements per row in the source data (for padding).
///   Must be `width * 4` for tightly packed RGBA data.
///
/// # Panics
/// * If `width` or `height` is not a multiple of 4
/// * If the destination `blocks_buffer` is too small to hold the compressed data
/// * If `variation` is not `CompressionVariant::BC6H`
///
/// # Example
/// ```
/// use block_compression::{encode::compress_rgba16, BC6HSettings, CompressionVariant};
/// use half::f16;
///
/// let rgba_data = vec![f16::ZERO; 256 * 256 * 4]; // Your RGBA16 data
/// let width = 256;
/// let height = 256;
/// let stride = width * 4; // Tightly packed rows
/// let settings = BC6HSettings::very_slow();
/// let variant = CompressionVariant::BC6H(settings);
///
/// let mut blocks_buffer = vec![0u8; variant.blocks_byte_size(width, height)];
///
/// compress_rgba16(
///     variant,
///     &rgba_data,
///     &mut blocks_buffer,
///     width,
///     height,
///     stride,
/// );
/// ```
#[cfg(feature = "bc6h")]
#[cfg_attr(docsrs, doc(cfg(feature = "bc6h")))]
pub fn compress_rgba16(
    variation: CompressionVariant,
    rgba_data: &[half::f16],
    blocks_buffer: &mut [u8],
    width: u32,
    height: u32,
    stride: u32,
) {
    assert_eq!(height % 4, 0);
    assert_eq!(width % 4, 0);

    let required_size = variation.blocks_byte_size(width, height);

    assert!(
        blocks_buffer.len() >= required_size,
        "blocks_buffer size ({}) is too small to hold compressed blocks. Required size: {}",
        blocks_buffer.len(),
        required_size
    );

    let stride = stride as usize;
    let block_width = (width as usize).div_ceil(4);
    let block_height = (height as usize).div_ceil(4);

    match variation {
        CompressionVariant::BC6H(settings) => {
            compress_bc6h_16bit(
                rgba_data,
                blocks_buffer,
                block_width,
                block_height,
                stride,
                &settings,
            );
        }
        #[allow(unreachable_patterns)]
        _ => {
            panic!("only BC6H is supported for calling compress_rgba16");
        }
    }
}

#[cfg(feature = "bc15")]
fn compress_bc1(
    rgba_data: &[u8],
    blocks_buffer: &mut [u8],
    block_width: usize,
    block_height: usize,
    stride: usize,
) {
    for yy in 0..block_height {
        for xx in 0..block_width {
            let mut block_compressor = BlockCompressorBC15::default();

            block_compressor.load_block_interleaved_rgba(rgba_data, xx, yy, stride);
            let color_result = block_compressor.compress_block_bc1_core();
            block_compressor.store_data(blocks_buffer, block_width, xx, yy, &color_result);
        }
    }
}

#[cfg(feature = "bc15")]
fn compress_bc2(
    rgba_data: &[u8],
    blocks_buffer: &mut [u8],
    block_width: usize,
    block_height: usize,
    stride: usize,
) {
    for yy in 0..block_height {
        for xx in 0..block_width {
            let mut block_compressor = BlockCompressorBC15::default();
            let mut compressed_data = [0; 4];

            let alpha_result = block_compressor.load_block_alpha_4bit(rgba_data, xx, yy, stride);

            compressed_data[0] = alpha_result[0];
            compressed_data[1] = alpha_result[1];

            block_compressor.load_block_interleaved_rgba(rgba_data, xx, yy, stride);

            let color_result = block_compressor.compress_block_bc1_core();
            compressed_data[2] = color_result[0];
            compressed_data[3] = color_result[1];

            block_compressor.store_data(blocks_buffer, block_width, xx, yy, &compressed_data);
        }
    }
}

#[cfg(feature = "bc15")]
fn compress_bc3(
    rgba_data: &[u8],
    blocks_buffer: &mut [u8],
    block_width: usize,
    block_height: usize,
    stride: usize,
) {
    for yy in 0..block_height {
        for xx in 0..block_width {
            let mut block_compressor = BlockCompressorBC15::default();

            let mut compressed_data = [0; 4];

            block_compressor.load_block_interleaved_rgba(rgba_data, xx, yy, stride);

            let alpha_result = block_compressor.compress_block_bc3_alpha();
            compressed_data[0] = alpha_result[0];
            compressed_data[1] = alpha_result[1];

            let color_result = block_compressor.compress_block_bc1_core();
            compressed_data[2] = color_result[0];
            compressed_data[3] = color_result[1];

            block_compressor.store_data(blocks_buffer, block_width, xx, yy, &compressed_data);
        }
    }
}

#[cfg(feature = "bc15")]
fn compress_bc4(
    rgba_data: &[u8],
    blocks_buffer: &mut [u8],
    block_width: usize,
    block_height: usize,
    stride: usize,
) {
    for yy in 0..block_height {
        for xx in 0..block_width {
            let mut block_compressor = BlockCompressorBC15::default();

            let mut compressed_data = [0; 2];

            block_compressor.load_block_r_8bit(rgba_data, xx, yy, stride);

            let color_result = block_compressor.compress_block_bc3_alpha();
            compressed_data[0] = color_result[0];
            compressed_data[1] = color_result[1];

            block_compressor.store_data(blocks_buffer, block_width, xx, yy, &compressed_data);
        }
    }
}

#[cfg(feature = "bc15")]
fn compress_bc5(
    rgba_data: &[u8],
    blocks_buffer: &mut [u8],
    block_width: usize,
    block_height: usize,
    stride: usize,
) {
    for yy in 0..block_height {
        for xx in 0..block_width {
            let mut block_compressor = BlockCompressorBC15::default();

            let mut compressed_data = [0; 4];

            block_compressor.load_block_r_8bit(rgba_data, xx, yy, stride);

            let red_result = block_compressor.compress_block_bc3_alpha();
            compressed_data[0] = red_result[0];
            compressed_data[1] = red_result[1];

            block_compressor.load_block_g_8bit(rgba_data, xx, yy, stride);

            let green_result = block_compressor.compress_block_bc3_alpha();
            compressed_data[2] = green_result[0];
            compressed_data[3] = green_result[1];

            block_compressor.store_data(blocks_buffer, block_width, xx, yy, &compressed_data);
        }
    }
}

#[cfg(feature = "bc6h")]
fn compress_bc6h_8bit(
    rgba_data: &[u8],
    blocks_buffer: &mut [u8],
    block_width: usize,
    block_height: usize,
    stride: usize,
    settings: &BC6HSettings,
) {
    for yy in 0..block_height {
        for xx in 0..block_width {
            let mut block_compressor = BlockCompressorBC6H::new(settings);
            block_compressor.load_block_interleaved_8bit(rgba_data, xx, yy, stride);
            block_compressor.compress_bc6h_core();
            block_compressor.store_data(blocks_buffer, block_width, xx, yy);
        }
    }
}

#[cfg(feature = "bc6h")]
fn compress_bc6h_16bit(
    rgba_data: &[half::f16],
    blocks_buffer: &mut [u8],
    block_width: usize,
    block_height: usize,
    stride: usize,
    settings: &BC6HSettings,
) {
    for yy in 0..block_height {
        for xx in 0..block_width {
            let mut block_compressor = BlockCompressorBC6H::new(settings);
            block_compressor.load_block_interleaved_16bit(rgba_data, xx, yy, stride);
            block_compressor.compress_bc6h_core();
            block_compressor.store_data(blocks_buffer, block_width, xx, yy);
        }
    }
}

#[cfg(feature = "bc7")]
fn compress_bc7(
    rgba_data: &[u8],
    blocks_buffer: &mut [u8],
    block_width: usize,
    block_height: usize,
    stride: usize,
    settings: &BC7Settings,
) {
    for yy in 0..block_height {
        for xx in 0..block_width {
            let mut block_compressor = BlockCompressorBC7::new(settings);

            block_compressor.load_block_interleaved_rgba(rgba_data, xx, yy, stride);
            block_compressor.compute_opaque_err();
            block_compressor.compress_block_bc7_core();
            block_compressor.store_data(blocks_buffer, block_width, xx, yy);
        }
    }
}