Welcome to mirror list, hosted at ThFree Co, Russian Federation.

draco-compressor.cpp « src « draco « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3262a2c4d4d6a7650a6e7812a758d5515b982031 (plain)
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
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Implemententation for the Draco exporter from the C++ side.
 *
 * The python side uses the CTypes libary to open the DLL, load function
 * pointers add pass the data to the compressor as raw bytes.
 *
 * The compressor intercepts the regular GLTF exporter after data has been
 * gathered and right before the data is converted to a JSON representation,
 * which is going to be written out.
 *
 * The original uncompressed data is removed and replaces an extension,
 * pointing to the newly created buffer containing the compressed data.
 *
 * @author Jim Eckerlein <eckerlein@ux3d.io>
 * @date   2019-01-15
 */

#include <iostream>
#include <fstream>
#include <memory>
#include <sstream>

#include "draco/mesh/mesh.h"
#include "draco/point_cloud/point_cloud.h"
#include "draco/core/vector_d.h"
#include "draco/io/mesh_io.h"
#include "draco/compression/encode.h"

#if defined(_MSC_VER)
#define DLL_EXPORT(retType) extern "C" __declspec(dllexport) retType __cdecl
#else
#define DLL_EXPORT(retType) extern "C" retType
#endif

const char *logTag = "DRACO-COMPRESSOR";

/**
 * This tuple is opaquely exposed to Python through a pointer.
 * It encapsulates the complete current compressor state.
 *
 * A single instance is only intended to compress a single primitive.
 */
struct DracoCompressor {

    /**
     * All positions, normals and texture coordinates are appended to this mesh.
     */
    draco::Mesh mesh;

    /**
     * One data buffer per attribute.
     */
    std::vector<std::unique_ptr<draco::DataBuffer>> buffers;

    /**
     * The buffer the mesh is compressed into.
     */
    draco::EncoderBuffer encoderBuffer;

    /**
     * The id Draco assigns to the position attribute.
     * Required to be reported in the GLTF file.
     */
    uint32_t positionAttributeId = (uint32_t) -1;

    /**
     * The id Draco assigns to the normal attribute.
     * Required to be reported in the GLTF file.
     */
    uint32_t normalAttributeId = (uint32_t) -1;

    /**
     * The ids Draco assigns to the texture coordinate attributes.
     * Required to be reported in the GLTF file.
     */
    std::vector<uint32_t> texCoordAttributeIds;

    /**
     * Level of compression [0-10].
     * Higher values mean slower encoding.
     */
    uint32_t compressionLevel = 7;

    uint32_t quantizationBitsPosition = 14;
    uint32_t quantizationBitsNormal = 10;
    uint32_t quantizationBitsTexCoord = 12;
};

draco::GeometryAttribute createAttribute(
        draco::GeometryAttribute::Type type,
        draco::DataBuffer &buffer,
        uint8_t components
) {
    draco::GeometryAttribute attribute;
    attribute.Init(
            type,
            &buffer,
            components,
            draco::DataType::DT_FLOAT32,
            false,
            sizeof(float) * components,
            0
    );
    return attribute;
}

DLL_EXPORT(DracoCompressor *) createCompressor() {
    return new DracoCompressor;
}

DLL_EXPORT(void) setCompressionLevel(
        DracoCompressor *compressor,
        uint32_t compressionLevel
) {
    compressor->compressionLevel = compressionLevel;
}

DLL_EXPORT(void) setPositionQuantizationBits(
        DracoCompressor *compressor,
        uint32_t quantizationBitsPosition
) {
    compressor->quantizationBitsPosition = quantizationBitsPosition;
}

DLL_EXPORT(void) setNormalQuantizationBits(
        DracoCompressor *compressor,
        uint32_t quantizationBitsNormal
) {
    compressor->quantizationBitsNormal = quantizationBitsNormal;
}

DLL_EXPORT(void) setTexCoordQuantizationBits(
        DracoCompressor *compressor,
        uint32_t quantizationBitsTexCoord
) {
    compressor->quantizationBitsTexCoord = quantizationBitsTexCoord;
}

DLL_EXPORT(bool) compress(
        DracoCompressor *compressor
) {
    printf("%s: Compressing primitive:\n", logTag);
    printf("%s: Compression level [0-10]:   %d\n", logTag, compressor->compressionLevel);
    printf("%s: Position quantization bits: %d\n", logTag, compressor->quantizationBitsPosition);
    printf("%s: Normal quantization bits:   %d\n", logTag, compressor->quantizationBitsNormal);
    printf("%s: Position quantization bits: %d\n", logTag, compressor->quantizationBitsTexCoord);

    draco::Encoder encoder;

    encoder.SetSpeedOptions(10 - compressor->compressionLevel, 10 - compressor->compressionLevel);
    encoder.SetAttributeQuantization(draco::GeometryAttribute::POSITION, compressor->quantizationBitsPosition);
    encoder.SetAttributeQuantization(draco::GeometryAttribute::NORMAL, compressor->quantizationBitsNormal);
    encoder.SetAttributeQuantization(draco::GeometryAttribute::TEX_COORD, compressor->quantizationBitsTexCoord);

    draco::Status result = encoder.EncodeMeshToBuffer(compressor->mesh, &compressor->encoderBuffer);

    if(!result.ok()) {
        printf("%s: Could not compress mesh: %s\n", logTag, result.error_msg());
        return false;
    }
    else {
        return true;
    }
}

/**
 * Returns the size of the compressed data in bytes.
 */
DLL_EXPORT(uint64_t) compressedSize(
        DracoCompressor *compressor
) {
    return compressor->encoderBuffer.size();
}

/**
 * Copies the compressed mesh into the given byte buffer.
 * @param[o_data] A Python `bytes` object.
 *
 */
DLL_EXPORT(void) copyToBytes(
        DracoCompressor *compressor,
        uint8_t *o_data
) {
    memcpy(o_data, compressor->encoderBuffer.data(), compressedSize(compressor));
}

DLL_EXPORT(uint32_t) getPositionAttributeId(
        DracoCompressor *compressor
) {
    return compressor->positionAttributeId;
}

DLL_EXPORT(uint32_t) getNormalAttributeId(
        DracoCompressor *compressor
) {
    return compressor->normalAttributeId;
}

DLL_EXPORT(uint32_t) getTexCoordAttributeIdCount(
        DracoCompressor *compressor
) {
    return (uint32_t) compressor->texCoordAttributeIds.size();
}

DLL_EXPORT(uint32_t) getTexCoordAttributeId(
        DracoCompressor *compressor,
        uint32_t index
) {
    return compressor->texCoordAttributeIds[index];
}

/**
 * Releases all memory allocated by the compressor.
 */
DLL_EXPORT(void) disposeCompressor(
        DracoCompressor *compressor
) {
    delete compressor;
}

template<class T>
void setFaces(
        draco::Mesh &mesh,
        int numIndices,
        T *indices
) {
    mesh.SetNumFaces((size_t) numIndices / 3);

    for (int i = 0; i < numIndices; i += 3)
    {
        const auto a = draco::PointIndex(indices[i]);
        const auto b = draco::PointIndex(indices[i + 1]);
        const auto c = draco::PointIndex(indices[i + 2]);
        mesh.SetFace(draco::FaceIndex((uint32_t) i), {a, b, c});
    }
}

DLL_EXPORT(void) setFaces(
        DracoCompressor *compressor,
        uint32_t numIndices,
        uint32_t bytesPerIndex,
        void *indices
) {
    switch (bytesPerIndex)
    {
        case 1:
        {
            setFaces(compressor->mesh, numIndices, (uint8_t *) indices);
            break;
        }
        case 2:
        {
            setFaces(compressor->mesh, numIndices, (uint16_t *) indices);
            break;
        }
        case 4:
        {
            setFaces(compressor->mesh, numIndices, (uint32_t *) indices);
            break;
        }
        default:
        {
            printf("%s: Unsupported index size %d\n", logTag, bytesPerIndex);
            break;
        }
    }
}

void addFloatAttribute(
        DracoCompressor *compressor,
        draco::GeometryAttribute::Type type,
        uint32_t count,
        uint8_t componentCount,
        float *source
) {
    auto buffer = std::make_unique<draco::DataBuffer>();

    const auto attribute = createAttribute(type, *buffer, componentCount);

    const auto id = (const uint32_t) compressor->mesh.AddAttribute(attribute, false, count);
    compressor->mesh.attribute(id)->SetIdentityMapping();

    switch (type)
    {
        case draco::GeometryAttribute::POSITION:
            compressor->positionAttributeId = id;
            break;
        case draco::GeometryAttribute::NORMAL:
            compressor->normalAttributeId = id;
            break;
        case draco::GeometryAttribute::TEX_COORD:
            compressor->texCoordAttributeIds.push_back(id);
            break;
        default:
            break;
    }

    for (uint32_t i = 0; i < count; i++)
    {
        compressor->mesh.attribute(id)->SetAttributeValue(
                draco::AttributeValueIndex(i),
                source + i * componentCount
        );
    }

    compressor->buffers.emplace_back(std::move(buffer));
}

DLL_EXPORT(void) addPositionAttribute(
        DracoCompressor *compressor,
        uint32_t count,
        float *source
) {
    addFloatAttribute(compressor, draco::GeometryAttribute::POSITION, count, 3, source);
}

DLL_EXPORT(void) addNormalAttribute(
        DracoCompressor *compressor,
        uint32_t count,
        float *source
) {
    addFloatAttribute(compressor, draco::GeometryAttribute::NORMAL, count, 3, source);
}

DLL_EXPORT(void) addTexCoordAttribute(
        DracoCompressor *compressor,
        uint32_t count,
        float *source
) {
    addFloatAttribute(compressor, draco::GeometryAttribute::TEX_COORD, count, 2, source);
}