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

#include "Core/Headers/Console.h"
#include "Utility/Headers/Localization.h"
#include "Environment/Terrain/Headers/TerrainLoader.h"

namespace Divide {

void DeleteResource::operator()(CachedResource* res)
{
    _context.remove(res);
    if (res && res->getType() != ResourceType::GPU_OBJECT) {
        MemoryManager::DELETE(res);
    }
}

ResourceCache::ResourceCache(PlatformContext& context)
    : _context(context)
{
}

ResourceCache::~ResourceCache()
{
    Console::printfn(Locale::get(_ID("RESOURCE_CACHE_DELETE")));
    clear();
    // DELETE(_loadingPool);
}


void ResourceCache::clear() {
    WriteLock w_lock(_creationMutex);
    Console::printfn(Locale::get(_ID("STOP_RESOURCE_CACHE")));
    for (ResourceMap::iterator it = std::begin(_resDB); it != std::end(_resDB);)
    {
        if (it->second.expired()) {
            it = _resDB.erase(it);
        } else {
            ++it;
        }
    }
    assert(_resDB.empty());
}

void ResourceCache::add(CachedResource_wptr res) {
    CachedResource_ptr resource = res.lock();

    if (resource == nullptr) {
        Console::errorfn(Locale::get(_ID("ERROR_RESOURCE_CACHE_LOAD_RES")));
        return;
    }

    size_t hash = resource->getDescriptorHash();
    DIVIDE_ASSERT(hash != 0, "ResourceCache add error: Invalid resource hash!");

    Console::printfn(Locale::get(_ID("RESOURCE_CACHE_ADD")), resource->getName().c_str(), hash);
    WriteLock w_lock(_creationMutex);
    hashAlg::insert(_resDB, std::make_pair(hash, resource));
}

CachedResource_ptr ResourceCache::loadResource(size_t descriptorHash, const stringImpl& resourceName) {
    const CachedResource_ptr& resource = find(descriptorHash);
    if (resource) {
        WAIT_FOR_CONDITION(resource->getState() == ResourceState::RES_LOADED);
    } else {
        Console::printfn(Locale::get(_ID("RESOURCE_CACHE_GET_RES")), resourceName.c_str(), descriptorHash);
    }

    return resource;
}

CachedResource_ptr ResourceCache::find(size_t descriptorHash) {
    static CachedResource_ptr emptyResource;
    /// Search in our resource cache
    ReadLock r_lock(_creationMutex);
    ResourceMap::const_iterator it = _resDB.find(descriptorHash);
    if (it != std::end(_resDB)) {
        return it->second.lock();
    }

    return emptyResource;
}

void ResourceCache::remove(CachedResource* resource) {
    size_t resourceHash = resource->getDescriptorHash();
    const stringImpl& name = resource->getName();

    DIVIDE_ASSERT(resourceHash != 0, Locale::get(_ID("ERROR_RESOURCE_CACHE_INVALID_NAME")));

    bool resDBEmpty = false;
    {
        ReadLock r_lock(_creationMutex);
        DIVIDE_ASSERT(_resDB.find(resourceHash) != std::end(_resDB),
                      Locale::get(_ID("ERROR_RESOURCE_CACHE_UNKNOWN_RESOURCE")));
        resDBEmpty = _resDB.empty();
    }


    Console::printfn(Locale::get(_ID("RESOURCE_CACHE_REM_RES")), name.c_str(), resourceHash);
    resource->setState(ResourceState::RES_LOADING);
    if (resource->unload()) {
        resource->setState(ResourceState::RES_CREATED);
    } else {
        Console::errorfn(Locale::get(_ID("ERROR_RESOURCE_REM")), name.c_str());
        resource->setState(ResourceState::RES_UNKNOWN);
    }
    
    if (resDBEmpty) {
        Console::errorfn(Locale::get(_ID("RESOURCE_CACHE_REMOVE_NO_DB")), name.c_str());
    } else {
        WriteLock w_lock(_creationMutex);
        _resDB.erase(_resDB.find(resourceHash));
    }
}

};

Commits for Divide-Framework/trunk/Source Code/Core/Resources/ResourceCache.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.

830 Diff Diff IonutCava picture IonutCava Wed 25 Jan, 2017 17:29:44 +0000

[IonutCava]
- Add a new Platform element: File
— Move all predefined paths to FileManagement System
- Split MathHelper into MathHelper and StringHelper

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)
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

711 Diff Diff IonutCava picture IonutCava Mon 23 May, 2016 14:31:55 +0000

[IonutCava]
- More scene change fixes: pending tasks, GUI elements, etc
- Singleton removal: AIManager is a per-scene member instead of a global singleton now

709 Diff Diff IonutCava picture IonutCava Thu 19 May, 2016 16:21:46 +0000

[IonutCava]
- Massive rewrite of the resource system:
— ResourceCache now uses a map of weak pointers and passes shared pointers to the call point of CreateResource
— RemoveResource is no longer needed, but resource based smart pointers use a custom deleter that calls unload()
— Keep a shared_ptr of the resource in the entity that needs it and pass shared_ptr references from getter methods
-— All of this removed the need for calls to RemoveResource, REGISTER_TRACKED_DEPENDENCY and UNREGISTER_TRACKED_DEPENDENCY reducing the number of mutex locks and atomic exchanges
- Singleton cleanup:
— Removed ShaderManager singleton and merged it’s data and responsibilities in static methods in both ShaderProgram and Shader classes.
Explanation: Due to the complex interdependency chains in the system, copying, updating, tracking the cache in a thread safe way became a lot more slower than a straight forward smart pointer based implementation. (e.g. scene graph nodes have 3d objects and material instances, 3d objects have materials, materials have textures and shaders, etc)

707 Diff Diff IonutCava picture IonutCava Mon 16 May, 2016 16:34:18 +0000

[IonutCava]
- Fix a deadlock in the ResourceCache (occurred on destruction)
— Should really replace the system with a shared_ptr based solution
- Sky resource dependency fixes (Causes crash on shutdown after loading/unloading multiple scenes. Not fully fixed)

692 Diff Diff IonutCava picture IonutCava Wed 27 Apr, 2016 16:24:26 +0000

[IonutCava]
- Resource system cleanup:
— Resource name and resource file location (if any) is now passed only via explicit constructors! (ResourceDescriptors already contain this data, so why use setters?)
- Fix a bug with ortho projection matrix calculation (set m[3][3] to 1. this was skipped when the identity() call was changed to zero() in the ortho() function)
— This also fixed text rendering

623 Diff Diff IonutCava picture IonutCava Fri 15 Jan, 2016 17:23:31 +0000

[IonutCava]
- Some particle system optimizations (splitting tasks in async workloads)
- Faster colour conversion systems
- Faster bone lookup by name
- Some DEBUG build changes to improve interactivity:
— Debug heap can be re-enabled on demand to track down Heisenbugs if they show up
- GLIM buffer orphaning

622 IonutCava picture IonutCava Thu 14 Jan, 2016 17:18:49 +0000

[IonutCava]
- Remove onCameraUpdate call from LightManager and SceneGraph as it’s not needed
- Remove special threading handlers from Resource class as very few derived classes use them for now
— Use ResourceState to check current loading state
- Use insertion sort for particles as most entries should already be sorted from the previous frame
- Use async RenderBin sorting system