Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

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
#include "Headers/MeshImporter.h"
#include "Headers/DVDConverter.h"

#include "Core/Headers/Console.h"
#include "Core/Headers/PlatformContext.h"
#include "Core/Time/Headers/ProfileTimer.h"
#include "Core/Resources/Headers/ResourceCache.h"

#include "Geometry/Shapes/Headers/Mesh.h"
#include "Geometry/Shapes/Headers/SkinnedSubMesh.h"
#include "Utility/Headers/Localization.h"

#include "Platform/Video/Buffers/VertexBuffer/Headers/VertexBuffer.h"

namespace Divide {

namespace {
    const stringImpl g_parsedAssetLocation("parsed");
    const stringImpl g_parsedAssetGeometryExt("DVDGeom");
    const stringImpl g_parsedAssetAnimationExt("DVDAnim");
};

namespace Import {
    ImportData::~ImportData()
    {
    }

    bool ImportData::saveToFile(PlatformContext& context, const stringImpl& fileName) {
        ACKNOWLEDGE_UNUSED(context);

        ByteBuffer tempBuffer;
        assert(_vertexBuffer != nullptr);
        tempBuffer << _ID("BufferEntryPoint");
        tempBuffer << _modelName;
        tempBuffer << _modelPath;
        if (_vertexBuffer->serialize(tempBuffer)) {
            tempBuffer << to_uint(_subMeshData.size());
            for (const SubMeshData& subMesh : _subMeshData) {
                if (!subMesh.serialize(tempBuffer)) {
                    //handle error
                }
            }
            tempBuffer << _hasAnimations;
            // Animations are handled by the SceneAnimator I/O
            return tempBuffer.dumpToFile(fileName + "." + g_parsedAssetGeometryExt);
        }

        return false;
    }

    bool ImportData::loadFromFile(PlatformContext& context, const stringImpl& fileName) {
        ByteBuffer tempBuffer;
        if (tempBuffer.loadFromFile(fileName + "." + g_parsedAssetGeometryExt)) {
            U64 signature;
            tempBuffer >> signature;
            if (signature != _ID("BufferEntryPoint")) {
                return false;
            }
            tempBuffer >> _modelName;
            tempBuffer >> _modelPath;
            _vertexBuffer = context.gfx().newVB();
            if (_vertexBuffer->deserialize(tempBuffer)) {
                U32 subMeshCount = 0;
                tempBuffer >> subMeshCount;
                _subMeshData.resize(subMeshCount);
                for (SubMeshData& subMesh : _subMeshData) {
                    if (!subMesh.deserialize(tempBuffer)) {
                        //handle error
                        DIVIDE_UNEXPECTED_CALL();
                    }
                }
                tempBuffer >> _hasAnimations;
                _loadedFromFile = true;
                return true;
            }
        }
        return false;
    }

    bool SubMeshData::serialize(ByteBuffer& dataOut) const {
        dataOut << _name;
        dataOut << _index;
        dataOut << _boneCount;
        dataOut << _partitionOffset;
        dataOut << _minPos;
        dataOut << _maxPos;
        dataOut << _triangles;
        return _material.serialize(dataOut);
    }

    bool SubMeshData::deserialize(ByteBuffer& dataIn) {
        dataIn >> _name;
        dataIn >> _index;
        dataIn >> _boneCount;
        dataIn >> _partitionOffset;
        dataIn >> _minPos;
        dataIn >> _maxPos;
        dataIn >> _triangles;
        return _material.deserialize(dataIn);
    }

    bool MaterialData::serialize(ByteBuffer& dataOut) const {
        dataOut << _ignoreAlpha;
        dataOut << _doubleSided;
        dataOut << _name;
        dataOut << _colourData._diffuse;
        dataOut << _colourData._specular;
        dataOut << _colourData._emissive;
        dataOut << _colourData._shininess;
        dataOut << to_uint(_shadingMode);
        dataOut << to_uint(_bumpMethod);
        for (const TextureEntry& texture : _textures) {
            if (!texture.serialize(dataOut)) {
                //handle error
                DIVIDE_UNEXPECTED_CALL();
            }
        }

        return true;
    }

    bool MaterialData::deserialize(ByteBuffer& dataIn) {
        U32 temp = 0;
        dataIn >> _ignoreAlpha;
        dataIn >> _doubleSided;
        dataIn >> _name;
        dataIn >> _colourData._diffuse;
        dataIn >> _colourData._specular;
        dataIn >> _colourData._emissive;
        dataIn >> _colourData._shininess;
        dataIn >> temp;
        _shadingMode = static_cast<Material::ShadingMode>(temp);
        dataIn >> temp;
        _bumpMethod = static_cast<Material::BumpMethod>(temp);
        for (TextureEntry& texture : _textures) {
            if (!texture.deserialize(dataIn)) {
                //handle error
                DIVIDE_UNEXPECTED_CALL();
            }
        }

        return true;
    }

    bool TextureEntry::serialize(ByteBuffer& dataOut) const {
        dataOut << _textureName;
        dataOut << _texturePath;
        dataOut << _srgbSpace;
        dataOut << to_uint(_wrapU);
        dataOut << to_uint(_wrapV);
        dataOut << to_uint(_wrapW);
        dataOut << to_uint(_operation);
        return true;
    }

    bool TextureEntry::deserialize(ByteBuffer& dataIn) {
        U32 data = 0;
        dataIn >> _textureName;
        dataIn >> _texturePath;
        dataIn >> _srgbSpace;
        dataIn >> data;
        _wrapU = static_cast<TextureWrap>(data);
        dataIn >> data;
        _wrapV = static_cast<TextureWrap>(data);
        dataIn >> data;
        _wrapW = static_cast<TextureWrap>(data);
        dataIn >> data;
        _operation = static_cast<Material::TextureOperation>(data);
        return true;
    }
};
    bool MeshImporter::loadMeshDataFromFile(PlatformContext& context, ResourceCache& cache, Import::ImportData& dataOut) {
        Time::ProfileTimer importTimer;
        importTimer.start();

        stringImpl modelName = dataOut._modelPath.substr(dataOut._modelPath.find_last_of('/') + 1);
        stringImpl path = dataOut._modelPath.substr(0, dataOut._modelPath.find_last_of('/'));

        bool success = false;
        if (!dataOut.loadFromFile(context, path + "/" + g_parsedAssetLocation + "/" + modelName)) {
            Console::printfn(Locale::get(_ID("MESH_NOT_LOADED_FROM_FILE")), modelName.c_str());

            DVDConverter converter(context, dataOut, dataOut._modelPath, success);
            if (success) {
                dataOut._modelName = modelName;
                dataOut._modelPath = path;
                if (dataOut.saveToFile(context, path + "/" + g_parsedAssetLocation + "/" + modelName)) {
                    Console::printfn(Locale::get(_ID("MESH_SAVED_TO_FILE")), modelName.c_str());
                } else {
                    Console::printfn(Locale::get(_ID("MESH_NOT_SAVED_TO_FILE")), modelName.c_str());
                }
            }
        } else {
            Console::printfn(Locale::get(_ID("MESH_LOADED_FROM_FILE")), modelName.c_str());
            success = true;
        }

        importTimer.stop();
        Console::d_printfn(Locale::get(_ID("LOAD_MESH_TIME")),
                           modelName.c_str(),
                           Time::MicrosecondsToSeconds<F32>(importTimer.get()));

        return success;
    }

    bool MeshImporter::loadMesh(Mesh_ptr mesh, PlatformContext& context, ResourceCache& cache, const Import::ImportData& dataIn) {
        Time::ProfileTimer importTimer;
        importTimer.start();

        std::shared_ptr<SceneAnimator> animator;
        if (dataIn._hasAnimations) {
            mesh->setObjectFlag(Object3D::ObjectFlag::OBJECT_FLAG_SKINNED);

            ByteBuffer tempBuffer;
            animator.reset(new SceneAnimator());
            if (tempBuffer.loadFromFile(dataIn._modelPath + "/" + 
                                        g_parsedAssetLocation + "/" +
                                        dataIn._modelName + "." +
                                        g_parsedAssetAnimationExt)) {
                animator->load(context, tempBuffer);
            } else {
                if (!dataIn._loadedFromFile) {
                    Attorney::SceneAnimatorMeshImporter::registerAnimations(*animator, dataIn._animations);
                    animator->init(context, dataIn._skeleton, dataIn._bones);
                    animator->save(context, tempBuffer);
                    if (!tempBuffer.dumpToFile(dataIn._modelPath + "/" + 
                                               g_parsedAssetLocation + "/" +
                                               dataIn._modelName + "." +
                                               g_parsedAssetAnimationExt)) {
                        //handle error
                        DIVIDE_UNEXPECTED_CALL();
                    }
                } else {
                    //handle error. No ASSIMP animation data available
                    DIVIDE_UNEXPECTED_CALL();
                }
            }
            mesh->setAnimator(animator);
            animator = nullptr;
        }

        mesh->renderState().setDrawState(true);
        mesh->getGeometryVB()->fromBuffer(*dataIn._vertexBuffer);

        SubMesh_ptr tempSubMesh;
        for (const Import::SubMeshData& subMeshData : dataIn._subMeshData) {
            // Submesh is created as a resource when added to the scenegraph
            ResourceDescriptor submeshdesc(subMeshData._name);
            submeshdesc.setFlag(true);
            submeshdesc.setID(subMeshData._index);
            if (subMeshData._boneCount > 0) {
                submeshdesc.setEnumValue(to_const_uint(Object3D::ObjectFlag::OBJECT_FLAG_SKINNED));
            }

            tempSubMesh = CreateResource<SubMesh>(cache, submeshdesc);
            if (!tempSubMesh) {
                continue;
            }
            // it may already be loaded
            if (!tempSubMesh->getParentMesh()) {
                tempSubMesh->addTriangles(subMeshData._triangles);
                tempSubMesh->setGeometryPartitionID(subMeshData._partitionOffset);
                Attorney::SubMeshMeshImporter::setGeometryLimits(*tempSubMesh,
                                                                 subMeshData._minPos,
                                                                 subMeshData._maxPos);

                if (!tempSubMesh->getMaterialTpl()) {
                    tempSubMesh->setMaterialTpl(loadSubMeshMaterial(context, cache, subMeshData._material, subMeshData._boneCount > 0));
                }
            }

            mesh->addSubMesh(tempSubMesh);
        }

        mesh->getGeometryVB()->create(!mesh->getObjectFlag(Object3D::ObjectFlag::OBJECT_FLAG_SKINNED));
        
        importTimer.stop();
        Console::d_printfn(Locale::get(_ID("PARSE_MESH_TIME")),
                           dataIn._modelName.c_str(),
                           Time::MillisecondsToSeconds(importTimer.get()));

        return true;
    }

    /// Load the material for the current SubMesh
    Material_ptr MeshImporter::loadSubMeshMaterial(PlatformContext& context, ResourceCache& cache, const Import::MaterialData& importData, bool skinned) {
        // See if the material already exists in a cooked state (XML file)
        STUBBED("LOADING MATERIALS FROM XML IS DISABLED FOR NOW! - Ionut")
#if !defined(DEBUG)
            const bool DISABLE_MAT_FROM_FILE = true;
#else
            const bool DISABLE_MAT_FROM_FILE = true;
#endif

        Material_ptr tempMaterial;
        if (!DISABLE_MAT_FROM_FILE) {
            tempMaterial = XML::loadMaterial(context, importData._name);
            if (tempMaterial) {
                return tempMaterial;
            }
        }

        // If we found it in the Resource Cache, return a copy of it
        ResourceDescriptor materialDesc(importData._name);
        if (skinned) {
            materialDesc.setEnumValue(to_const_uint(Object3D::ObjectFlag::OBJECT_FLAG_SKINNED));
        }

        // If it's not defined in an XML File, see if it was previously loaded by
        // the Resource Cache
        tempMaterial = FindResourceImpl<Material>(cache, materialDesc.getHash());
        if (tempMaterial) {
            return tempMaterial;
        }

        tempMaterial = CreateResource<Material>(cache, materialDesc);

        tempMaterial->setColourData(importData._colourData);
        tempMaterial->setShadingMode(importData._shadingMode);
        tempMaterial->setBumpMethod(importData._bumpMethod);
        tempMaterial->setDoubleSided(importData._doubleSided);

        SamplerDescriptor textureSampler;
        for (U32 i = 0; i < to_const_uint(ShaderProgram::TextureUsage::COUNT); ++i) {
            const Import::TextureEntry& tex = importData._textures[i];
            if (!tex._textureName.empty()) {
                textureSampler.toggleSRGBColourSpace(tex._srgbSpace);
                textureSampler.setWrapMode(tex._wrapU, tex._wrapV, tex._wrapW);

                ResourceDescriptor texture(tex._textureName);
                texture.setResourceName(tex._textureName);
                texture.setResourceLocation(tex._texturePath);
                texture.setPropertyDescriptor<SamplerDescriptor>(textureSampler);
                texture.setEnumValue(to_const_uint(TextureType::TEXTURE_2D));
                Texture_ptr textureRes = CreateResource<Texture>(cache, texture);
                assert(textureRes != nullptr);

                tempMaterial->setTexture(static_cast<ShaderProgram::TextureUsage>(i), textureRes, tex._operation);
            }
        }

        // If we don't have a valid opacity map, try to find out whether the diffuse texture has any non-opaque pixels.
        // If we find a few, use it as opacity texture
        if (!importData._ignoreAlpha && importData._textures[to_const_uint(ShaderProgram::TextureUsage::OPACITY)]._textureName.empty()) {
            Texture_ptr diffuse = tempMaterial->getTexture(ShaderProgram::TextureUsage::UNIT0).lock();
            if (diffuse && diffuse->hasTransparency()) {
                Texture_ptr textureRes = CreateResource<Texture>(cache, ResourceDescriptor(diffuse->getName()));
                tempMaterial->setTexture(ShaderProgram::TextureUsage::OPACITY, textureRes, Material::TextureOperation::REPLACE);
            }
        }
        
        return tempMaterial;
    }
}; //namespace Divide

Commits for Divide-Framework/trunk/Source Code/Geometry/Importer/MeshImporter.cpp

Diff revisions: vs.
Revision Author Commited Message
863 Diff Diff IonutCava picture IonutCava Wed 15 Feb, 2017 16:27:26 +0000

[IonutCava]
- Split Resource class into Resource and CachedResource
— CachedResource is the only resource held in a ResourceCache and loaded from a ResourceDescriptor
— CachedResource is mapped by its creation descriptor’s hash value, instead of the resource name
-— Multiple cached resources can now exist with the same name if the have different properties, for example.

859 Diff Diff IonutCava picture IonutCava Mon 13 Feb, 2017 17:18:06 +0000

[IonutCava]
- Multi-threaded mesh load
- ByteBuffer fixes and unit test

829 Diff Diff IonutCava picture IonutCava Tue 24 Jan, 2017 17:28:39 +0000

[IonutCava]
- Separate resourceLocation for resources into resourceLocation and resourceName.
- More XML config loading refactoring

828 Diff Diff IonutCava picture IonutCava Mon 23 Jan, 2017 17:19:22 +0000

[Ionut]
- XML loading/saving cleanup PART I

807 Diff Diff IonutCava picture IonutCava Mon 09 Jan, 2017 17:06:26 +0000

[IonutCava]
- Split PlatformDefines.h into 2. Move data type defines to PlatformDataTypes.h
- Continue to improve velocity map calculation
- Remove “destroy()” calls from most classes. Attempt to move as many classes to the RAII system.

804 Diff Diff IonutCava picture IonutCava Thu 01 Dec, 2016 17:20:59 +0000

[IonutCava]
- Singleton elimination update Part I: get it to compile
— The following classes are no longer Singletons: GFXDevice, GL_API, DX_API, SFXWrapper, FmodWrapper, SDLWrapper, ALWrapper, PXDevice, InputInterface, RenderPassManager, SceneManager and ResourceManager;
— Refactor system to a Context / Component based implementation (Pass relevant context to objects: e.g. GFXDevice object to Textures, GUI to GUIElements, etc)
— Make devices and managers components of the kernel
— Allow multiple Resource caches to co-exist. This may prove useful for later when a more fragmented memory model is need (per frame / per scene / global caches / etc)

  • next steps: part II – cleanup/refactor new code, part III – optimise code, part IV – remove remaining Singletons (e.g. Application, ParamHandler, FrameListenerManager, Recast, PostFX and DebugInterface)
801 Diff Diff IonutCava picture IonutCava Sun 27 Nov, 2016 21:28:01 +0000

[IonutCava]
- More performance analysis guided optimizations.
- Some refactoring to allow later removal of Singletons status for: GFXDevice, SFXDevice, PXDevice, GUI and Input.
— Pass a PlatformContext around objects to access these classes.

788 Diff Diff IonutCava picture IonutCava Fri 21 Oct, 2016 16:11:37 +0000

[IonutCava]
- Added support for the Arena Allocator by Mamasha Knows (http://www.codeproject.com/Articles/44850/Arena-Allocator-DTOR-and-Embedded-Preallocated-Buf)
— Used for GFX Related objects: Textures, shaders, etc

780 Diff Diff IonutCava picture IonutCava Tue 11 Oct, 2016 16:15:24 +0000

[IonutCava]
- AtomicCounter in glUniformBuffer is now based on a glGenericBuffer implementation
- GFXDevice <-> Camera relation cleaned up a bit
- Added a readback system for glGenericBuffer
- Fixed a bug with Z_PRE_PASS stage culling

776 IonutCava picture IonutCava Thu 06 Oct, 2016 15:57:56 +0000

[IonutCava]
- Improve the CommandBuffer system to include the notion of a RenderPass /RenderSubPass with output render target info stored in them (not used yet)