1 #ifndef NETWORK_COMPRESSION_H
2 #define NETWORK_COMPRESSION_H
4 #include "vg/vg_platform.h"
7 typedef struct bitpack_ctx bitpack_ctx
;
16 u32 bytes
, buffer_len
;
19 static void bitpack_bytes( bitpack_ctx
*ctx
, u32 bytes
, void *data
){
21 for( u32 i
=0; i
<bytes
; i
++ ){
22 u32 index
= ctx
->bytes
+i
;
23 if( ctx
->mode
== k_bitpack_compress
){
24 if( index
< ctx
->buffer_len
)
25 ctx
->buffer
[index
] = ext
[i
];
28 if( index
< ctx
->buffer_len
)
29 ext
[i
] = ctx
->buffer
[index
];
37 static u32
bitpack_qf32( bitpack_ctx
*ctx
, u32 bits
,
38 f32 min
, f32 max
, f32
*v
){
39 u32 mask
= (0x1 << bits
) - 1;
41 if( ctx
->mode
== k_bitpack_compress
){
42 u32 a
= vg_quantf( *v
, bits
, min
, max
);
43 bitpack_bytes( ctx
, bits
/8, &a
);
48 bitpack_bytes( ctx
, bits
/8, &a
);
49 *v
= vg_dequantf( a
, bits
, min
, max
);
54 static void bitpack_qv2f( bitpack_ctx
*ctx
, u32 bits
,
55 f32 min
, f32 max
, v2f v
){
56 for( u32 i
=0; i
<2; i
++ )
57 bitpack_qf32( ctx
, bits
, min
, max
, v
+i
);
60 static void bitpack_qv3f( bitpack_ctx
*ctx
, u32 bits
,
61 f32 min
, f32 max
, v3f v
){
62 for( u32 i
=0; i
<3; i
++ )
63 bitpack_qf32( ctx
, bits
, min
, max
, v
+i
);
66 static void bitpack_qv4f( bitpack_ctx
*ctx
, u32 bits
,
67 f32 min
, f32 max
, v4f v
){
68 for( u32 i
=0; i
<4; i
++ )
69 bitpack_qf32( ctx
, bits
, min
, max
, v
+i
);
72 static void bitpack_qquat( bitpack_ctx
*ctx
, v4f quat
){
73 const f32 k_domain
= 0.70710678118f
;
75 if( ctx
->mode
== k_bitpack_compress
){
77 for( u32 i
=0; i
<4; i
++ )
78 qabs
[i
] = fabsf(quat
[i
]);
80 u32 lxy
= qabs
[1]>qabs
[0],
81 lzw
= (qabs
[3]>qabs
[2])+2,
82 l
= qabs
[lzw
]>qabs
[lxy
]? lzw
: lxy
;
84 f32 sign
= vg_signf(quat
[l
]);
87 for( u32 i
=0, j
=0; i
<4; i
++ )
89 smallest
[j
++] = vg_quantf( quat
[i
]*sign
, 10, -k_domain
, k_domain
);
91 u32 comp
= (smallest
[0]<<2) | (smallest
[1]<<12) | (smallest
[2]<<22) | l
;
92 bitpack_bytes( ctx
, 4, &comp
);
96 bitpack_bytes( ctx
, 4, &comp
);
98 u32 smallest
[3] = {(comp
>>2 )&0x3ff,
105 for( u32 i
=0, j
=0; i
<4; i
++ ){
107 quat
[i
] = vg_dequantf( smallest
[j
++], 10, -k_domain
, k_domain
);
108 m
-= quat
[i
]*quat
[i
];
117 #endif /* NETWORK_COMPRESSION_H */