Skip to main content

basisu_c_sys/
utils.rs

1use crate::common;
2
3/// A enum that wraps `common::BTF_*`.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6#[repr(u32)]
7pub enum BasisTextureFormat {
8    Etc1s = common::BTF_ETC1S,
9    UastcLdr4x4 = common::BTF_UASTC_LDR_4X4,
10    UastcHdr4x4 = common::BTF_UASTC_HDR_4X4,
11    AstcHdr6x6 = common::BTF_ASTC_HDR_6X6,
12    UastcHdr6x6 = common::BTF_UASTC_HDR_6X6,
13    XuastcLdr4x4 = common::BTF_XUASTC_LDR_4X4,
14    XuastcLdr5x4 = common::BTF_XUASTC_LDR_5X4,
15    XuastcLdr5x5 = common::BTF_XUASTC_LDR_5X5,
16    XuastcLdr6x5 = common::BTF_XUASTC_LDR_6X5,
17    XuastcLdr6x6 = common::BTF_XUASTC_LDR_6X6,
18    XuastcLdr8x5 = common::BTF_XUASTC_LDR_8X5,
19    XuastcLdr8x6 = common::BTF_XUASTC_LDR_8X6,
20    XuastcLdr10x5 = common::BTF_XUASTC_LDR_10X5,
21    XuastcLdr10x6 = common::BTF_XUASTC_LDR_10X6,
22    XuastcLdr8x8 = common::BTF_XUASTC_LDR_8X8,
23    XuastcLdr10x8 = common::BTF_XUASTC_LDR_10X8,
24    XuastcLdr10x10 = common::BTF_XUASTC_LDR_10X10,
25    XuastcLdr12x10 = common::BTF_XUASTC_LDR_12X10,
26    XuastcLdr12x12 = common::BTF_XUASTC_LDR_12X12,
27    AstcLdr4x4 = common::BTF_ASTC_LDR_4X4,
28    AstcLdr5x4 = common::BTF_ASTC_LDR_5X4,
29    AstcLdr5x5 = common::BTF_ASTC_LDR_5X5,
30    AstcLdr6x5 = common::BTF_ASTC_LDR_6X5,
31    AstcLdr6x6 = common::BTF_ASTC_LDR_6X6,
32    AstcLdr8x5 = common::BTF_ASTC_LDR_8X5,
33    AstcLdr8x6 = common::BTF_ASTC_LDR_8X6,
34    AstcLdr10x5 = common::BTF_ASTC_LDR_10X5,
35    AstcLdr10x6 = common::BTF_ASTC_LDR_10X6,
36    AstcLdr8x8 = common::BTF_ASTC_LDR_8X8,
37    AstcLdr10x8 = common::BTF_ASTC_LDR_10X8,
38    AstcLdr10x10 = common::BTF_ASTC_LDR_10X10,
39    AstcLdr12x10 = common::BTF_ASTC_LDR_12X10,
40    AstcLdr12x12 = common::BTF_ASTC_LDR_12X12,
41}
42
43impl TryFrom<u32> for BasisTextureFormat {
44    type Error = ();
45
46    fn try_from(value: u32) -> Result<Self, Self::Error> {
47        if value > common::BTF_ASTC_LDR_12X12 {
48            Err(())
49        } else {
50            Ok(unsafe { core::mem::transmute::<u32, BasisTextureFormat>(value) })
51        }
52    }
53}
54
55/// A enum that wraps `common::TF_*`.
56#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
58#[repr(u32)]
59pub enum TranscodeTargetFormat {
60    Etc1Rgb = common::TF_ETC1_RGB,
61    Etc2Rgba = common::TF_ETC2_RGBA,
62    Bc1Rgb = common::TF_BC1_RGB,
63    Bc3Rgba = common::TF_BC3_RGBA,
64    Bc4R = common::TF_BC4_R,
65    Bc5Rg = common::TF_BC5_RG,
66    Bc7Rgba = common::TF_BC7_RGBA,
67    Pvrtc1_4Rgb = common::TF_PVRTC1_4_RGB,
68    Pvrtc1_4Rgba = common::TF_PVRTC1_4_RGBA,
69    AstcLdr4x4Rgba = common::TF_ASTC_LDR_4X4_RGBA,
70    AtcRgb = common::TF_ATC_RGB,
71    AtcRgba = common::TF_ATC_RGBA,
72    Fxt1Rgb = common::TF_FXT1_RGB,
73    Pvrtc2_4Rgb = common::TF_PVRTC2_4_RGB,
74    Pvrtc2_4Rgba = common::TF_PVRTC2_4_RGBA,
75    Etc2EacR11 = common::TF_ETC2_EAC_R11,
76    Etc2EacRg11 = common::TF_ETC2_EAC_RG11,
77    Bc6H = common::TF_BC6H,
78    AstcHdr4x4Rgba = common::TF_ASTC_HDR_4X4_RGBA,
79    RGBA32 = common::TF_RGBA32,
80    RGB565 = common::TF_RGB565,
81    BGR565 = common::TF_BGR565,
82    RGBA4444 = common::TF_RGBA4444,
83    RgbHalf = common::TF_RGB_HALF,
84    RgbaHalf = common::TF_RGBA_HALF,
85    Rgb9e5 = common::TF_RGB_9E5,
86    AstcHdr6x6Rgba = common::TF_ASTC_HDR_6X6_RGBA,
87    AstcLdr5x4Rgba = common::TF_ASTC_LDR_5X4_RGBA,
88    AstcLdr5x5Rgba = common::TF_ASTC_LDR_5X5_RGBA,
89    AstcLdr6x5Rgba = common::TF_ASTC_LDR_6X5_RGBA,
90    AstcLdr6x6Rgba = common::TF_ASTC_LDR_6X6_RGBA,
91    AstcLdr8x5Rgba = common::TF_ASTC_LDR_8X5_RGBA,
92    AstcLdr8x6Rgba = common::TF_ASTC_LDR_8X6_RGBA,
93    AstcLdr10x5Rgba = common::TF_ASTC_LDR_10X5_RGBA,
94    AstcLdr10x6Rgba = common::TF_ASTC_LDR_10X6_RGBA,
95    AstcLdr8x8Rgba = common::TF_ASTC_LDR_8X8_RGBA,
96    AstcLdr10x8Rgba = common::TF_ASTC_LDR_10X8_RGBA,
97    AstcLdr10x10Rgba = common::TF_ASTC_LDR_10X10_RGBA,
98    AstcLdr12x10Rgba = common::TF_ASTC_LDR_12X10_RGBA,
99    AstcLdr12x12Rgba = common::TF_ASTC_LDR_12X12_RGBA,
100}
101
102impl TryFrom<u32> for TranscodeTargetFormat {
103    type Error = ();
104
105    fn try_from(value: u32) -> Result<Self, Self::Error> {
106        if value >= common::TF_TOTAL_TEXTURE_FORMATS {
107            Err(())
108        } else {
109            Ok(unsafe { core::mem::transmute::<u32, TranscodeTargetFormat>(value) })
110        }
111    }
112}
113
114#[cfg(all(
115    target_arch = "wasm32",
116    target_vendor = "unknown",
117    target_os = "unknown",
118))]
119use crate::web as impls;
120
121#[cfg(not(all(
122    target_arch = "wasm32",
123    target_vendor = "unknown",
124    target_os = "unknown",
125)))]
126use crate::native as impls;
127
128/// Use this to copy memory between host and basisu.
129/// This is required on web where memory isn't shared.
130/// # Safety
131/// `basisu_ptr` must be valid pointer allocated by `bu_alloc` or `bt_alloc`
132/// and must be valid for writes of `data.len()` bytes.
133// Note: This is used for both transcoder and encoder and assumes their `alloc` and `free` are the same. If not (though it's unlikely), this should be changed.
134pub unsafe fn copy_host_memory_to_basisu(data: &[u8], basisu_ptr: u64) {
135    unsafe { impls::copy_host_memory_to_basisu_impl(data, basisu_ptr) };
136}
137
138/// Use this to copy memory between host and basisu.
139/// This is required on web where memory isn't shared.
140/// # Safety
141/// `basisu_ptr` must be valid pointer allocated by `bu_alloc` or `bt_alloc`
142/// and must be valid for reads of `count` bytes.
143// Note: This is used for both transcoder and encoder and assumes their `alloc` and `free` are the same. If not (though it's unlikely), this should be changed.
144pub unsafe fn copy_basisu_memory_to_host(basisu_ptr: u64, count: u64) -> alloc::vec::Vec<u8> {
145    unsafe { impls::copy_basisu_memory_to_host_impl(basisu_ptr, count) }
146}