2004-09-17 17:25:59 +02:00
|
|
|
#include "patch.h"
|
2004-10-10 19:19:42 +02:00
|
|
|
#include <lzo1x.h>
|
|
|
|
#include <iostream>
|
|
|
|
using namespace std;
|
2004-09-17 17:25:59 +02:00
|
|
|
using namespace nxs;
|
|
|
|
|
2004-10-10 19:19:42 +02:00
|
|
|
static double wrkmem[LZO1X_999_MEM_COMPRESS/sizeof(double) +1];
|
|
|
|
|
2004-10-01 18:54:57 +02:00
|
|
|
void pad(unsigned int &size) {
|
|
|
|
while(size&0x3) size++;
|
|
|
|
}
|
2004-09-17 17:25:59 +02:00
|
|
|
|
2004-10-08 17:12:04 +02:00
|
|
|
Patch::Patch(Signature signature, char *s,
|
2004-09-17 17:25:59 +02:00
|
|
|
unsigned short nvert, unsigned short nface):
|
|
|
|
start(s) {
|
|
|
|
Init(signature, nvert, nface);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Patch::Init(Signature signature,
|
|
|
|
unsigned short nvert, unsigned short nface) {
|
|
|
|
nv = nvert;
|
|
|
|
nf = nface;
|
|
|
|
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_FACES)
|
2004-09-17 17:25:59 +02:00
|
|
|
vstart = (float *)(((char *)start) + nf * sizeof(unsigned short) * 3);
|
2004-09-30 02:27:42 +02:00
|
|
|
else if(signature & NXS_STRIP)
|
2004-09-17 17:25:59 +02:00
|
|
|
vstart = (float *)(((char *)start) + nf * sizeof(unsigned short));
|
|
|
|
else
|
|
|
|
vstart = (float *)start;
|
|
|
|
|
|
|
|
//align memory
|
|
|
|
if(((int)vstart) & 0x2) vstart = (float *)(((char *)vstart) + 2);
|
|
|
|
|
2004-10-01 18:54:57 +02:00
|
|
|
cstart = nv * 3;
|
|
|
|
if(signature & NXS_COLORS)
|
|
|
|
nstart = cstart + nv;
|
|
|
|
else
|
|
|
|
nstart = cstart;
|
|
|
|
|
|
|
|
if(signature & NXS_NORMALS_SHORT)
|
|
|
|
tstart = nstart + nv * 2;
|
|
|
|
else if(signature & NXS_NORMALS_FLOAT)
|
|
|
|
tstart = nstart + nv * 3;
|
|
|
|
else
|
|
|
|
tstart = nstart;
|
|
|
|
|
|
|
|
if(signature & NXS_TEXTURES_SHORT)
|
|
|
|
dstart = tstart + nv;
|
|
|
|
else if(signature & NXS_TEXTURES_FLOAT)
|
|
|
|
dstart = tstart + nv;
|
|
|
|
else
|
|
|
|
dstart = tstart;
|
2004-09-17 17:25:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Patch::ChunkSize(Signature signature,
|
|
|
|
unsigned short nvert,
|
2004-10-08 17:12:04 +02:00
|
|
|
unsigned short nface,
|
|
|
|
unsigned int chunk_size) {
|
2004-09-17 17:25:59 +02:00
|
|
|
unsigned int size = ByteSize(signature, nvert, nface);
|
2004-10-08 17:12:04 +02:00
|
|
|
size = (size/chunk_size + 1);
|
2004-09-17 17:25:59 +02:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Patch::ByteSize(Signature signature,
|
|
|
|
unsigned short nvert,
|
|
|
|
unsigned short nface) {
|
|
|
|
unsigned int size = 0;
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_STRIP)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nface * sizeof(unsigned short);
|
2004-09-30 02:27:42 +02:00
|
|
|
else if(signature & NXS_FACES)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nface * 3 * sizeof(unsigned short);
|
|
|
|
|
|
|
|
//memory alignment
|
2004-10-01 18:54:57 +02:00
|
|
|
pad(size);
|
2004-09-17 17:25:59 +02:00
|
|
|
|
|
|
|
size += nvert * sizeof(vcg::Point3f);
|
|
|
|
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_COLORS)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nvert * sizeof(unsigned int);
|
|
|
|
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_NORMALS_SHORT)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nvert * 4 * sizeof(short);
|
|
|
|
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_NORMALS_FLOAT)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nvert * 3 * sizeof(float);
|
|
|
|
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_TEXTURES_SHORT)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nvert * 2 * sizeof(short);
|
|
|
|
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_TEXTURES_FLOAT)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nvert * 2 * sizeof(float);
|
|
|
|
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_DATA8)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nvert * sizeof(char);
|
2004-10-01 18:54:57 +02:00
|
|
|
pad(size);
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_DATA16)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nvert * 2 * sizeof(char);
|
2004-10-01 18:54:57 +02:00
|
|
|
pad(size);
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_DATA32)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nvert * 4 * sizeof(char);
|
2004-09-30 02:27:42 +02:00
|
|
|
if(signature & NXS_DATA64)
|
2004-09-17 17:25:59 +02:00
|
|
|
size += nvert * 8 * sizeof(char);
|
|
|
|
|
|
|
|
|
|
|
|
//this condition should really rarely happen but helps save space
|
|
|
|
//during construction
|
|
|
|
if(size < nface * 3 * sizeof(unsigned int))
|
|
|
|
size = nface * 3 * sizeof(unsigned int);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
2004-10-10 19:19:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
char *Patch::Compress(unsigned int ram_size, unsigned int &size) {
|
|
|
|
//TODO use OVERLAP and test speed
|
|
|
|
//TODO fill chunk padding with zeroes?
|
|
|
|
//TODO compress only used memory!
|
|
|
|
size = ram_size + ram_size/64 + 23;
|
|
|
|
char *buffer = new char[size];
|
|
|
|
lzo1x_1_compress(((unsigned char *)start), ram_size,
|
|
|
|
(unsigned char *)buffer + sizeof(int), &size,
|
|
|
|
(char *)wrkmem);
|
|
|
|
|
|
|
|
*(int *)buffer = size;
|
|
|
|
size += sizeof(int);
|
|
|
|
|
|
|
|
|
|
|
|
// memcpy(buffer, start, ram_size);
|
|
|
|
// size = ram_size;
|
|
|
|
//TODO optimize!
|
|
|
|
// lzo1x_optimize((unsigned char *)entry.patch->start,
|
|
|
|
// entry.ram_size * chunk_size,
|
|
|
|
// compressed, &compressed_size, NULL);
|
|
|
|
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Patch::Decompress(unsigned int ram_size, void *src, unsigned int src_sz) {
|
|
|
|
|
|
|
|
unsigned int size = *(int *)src;
|
|
|
|
assert(size < src_sz + sizeof(int));
|
|
|
|
unsigned int dst_size = ram_size;
|
|
|
|
|
|
|
|
// memcpy(start, src, ram_size);
|
|
|
|
int ret = lzo1x_decompress_safe(((unsigned char *)src) + sizeof(int), size,
|
|
|
|
(unsigned char *)start, &dst_size, 0);
|
|
|
|
if(ret != 0) {
|
|
|
|
cerr << "Ret from decompress: " << ret << endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
assert(dst_size == ram_size);
|
|
|
|
//TODO add 3 to start... so we can use asm_fast decompressor
|
|
|
|
}
|
|
|
|
|