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

#include "Headers/Kernel.h"
#include "Headers/ParamHandler.h"

#include "Core/Time/Headers/ApplicationTimer.h"

#include "Utility/Headers/MemoryTracker.h"

#include <thread>

namespace Divide {

bool MemoryManager::MemoryTracker::Ready = false;
bool MemoryManager::MemoryTracker::LogAllAllocations = false;
MemoryManager::MemoryTracker MemoryManager::AllocTracer;

std::thread::id Application::_threadID;

const std::thread::id& Application::mainThreadID() {
    return _threadID;
}

bool Application::isMainThread() {
    return (_threadID == std::this_thread::get_id());
}

void Application::mainThreadID(const std::thread::id& threadID) {
    _threadID = threadID;
}

Application::Application() : _kernel(nullptr),
                             _isInitialized(false)
{
    mainThreadID(std::this_thread::get_id());
    _requestShutdown = false;
    _mainLoopPaused = false;
    _mainLoopActive = false;
    _errorCode = ErrorCode::NO_ERR;

    if (Config::Build::IS_DEBUG_BUILD) {
        MemoryManager::MemoryTracker::Ready = true; //< faster way of disabling memory tracking
        MemoryManager::MemoryTracker::LogAllAllocations = false;
    }
}

Application::~Application()
{
    assert(!_isInitialized);
}

ErrorCode Application::start(const stringImpl& entryPoint, I32 argc, char** argv) {
    assert(!entryPoint.empty());

    _isInitialized = true;
    ErrorCode err = ErrorCode::NO_ERR;
    Console::start();
    ParamHandler::createInstance();
    Time::ApplicationTimer::createInstance();
    // Don't log parameter requests
    ParamHandler::instance().setDebugOutput(false);
    // Read language table
    err = Locale::init();
    if (err == ErrorCode::NO_ERR) {
        // Print a copyright notice in the log file
        Console::printCopyrightNotice();
        Console::toggleTimeStamps(true);
        Console::togglethreadID(true);
        Console::printfn(Locale::get(_ID("START_APPLICATION")));
        // Create a new kernel
        assert(_kernel == nullptr);
        _kernel = MemoryManager_NEW Kernel(argc, argv, this->instance());

        // and load it via an XML file config
        err = Attorney::KernelApplication::initialize(*_kernel, entryPoint);
    }

    // failed to start, so cleanup
    if (err != ErrorCode::NO_ERR) {
        throwError(err);
        stop();
    } else {
        warmup();
        mainLoopActive(true);
    }

    return err;
}

void Application::stop() {
    if (_isInitialized) {
        if (_kernel != nullptr) {
            Attorney::KernelApplication::shutdown(*_kernel);
        }
        for (DELEGATE_CBK<void>& cbk : _shutdownCallback) {
            cbk();
        }

        if (Config::Build::IS_DEBUG_BUILD) {
            MemoryManager::MemoryTracker::Ready = false;
            bool leakDetected = false;
            size_t sizeLeaked = 0;
            stringImpl allocLog =
                MemoryManager::AllocTracer.Dump(leakDetected, sizeLeaked);
            if (leakDetected) {
                Console::errorfn(Locale::get(_ID("ERROR_MEMORY_NEW_DELETE_MISMATCH")),
                    to_int(std::ceil(sizeLeaked / 1024.0f)));
            }
            std::ofstream memLog;
            memLog.open(_memLogBuffer.c_str());
            memLog << allocLog;
            memLog.close();
        }

        _windowManager.close();
        ParamHandler::destroyInstance();
        MemoryManager::DELETE(_kernel);
        Console::printfn(Locale::get(_ID("STOP_APPLICATION")));
        Locale::clear();
        Time::ApplicationTimer::destroyInstance();
        Console::stop();
        _isInitialized = false;
    }
}

void Application::warmup() {
    Console::printfn(Locale::get(_ID("START_MAIN_LOOP")));
    //Make sure we are displaying a splash screen
    _windowManager.getActiveWindow().type(WindowType::SPLASH);
    Attorney::KernelApplication::warmup(*_kernel);
    //Restore to normal window
    _windowManager.getActiveWindow().previousType();
}

bool Application::step() {
    if (onLoop()) {
        Attorney::KernelApplication::onLoop(*_kernel);
        return true;
    }

    return false;
}

bool Application::onLoop() {
    _windowManager.handleWindowEvent(WindowEvent::APP_LOOP, -1, -1, -1);

    {
        UpgradableReadLock ur_lock(_taskLock);
        bool isQueueEmpty = _mainThreadCallbacks.empty();
        if (!isQueueEmpty) {
            UpgradeToWriteLock w_lock(ur_lock);
            while(!_mainThreadCallbacks.empty()) {
                _mainThreadCallbacks.back()();
                _mainThreadCallbacks.pop_back();
            }
        }
    }

    return mainLoopActive();
}

void Application::setCursorPosition(I32 x, I32 y) const {
    _windowManager.setCursorPosition(x, y);
    Attorney::KernelApplication::setCursorPosition(*_kernel, x, y);
}

void Application::onChangeWindowSize(U16 w, U16 h) const {
    Attorney::KernelApplication::onChangeWindowSize(*_kernel, w, h);
}

void Application::onChangeRenderResolution(U16 w, U16 h) const {
    Attorney::KernelApplication::onChangeRenderResolution(*_kernel, w, h);
}

void Application::mainThreadTask(const DELEGATE_CBK<void>& task, bool wait) {
    std::atomic_bool done = false;
    if (wait) {
        WriteLock w_lock(_taskLock);
        _mainThreadCallbacks.push_back([&done, &task] { task(); done = true; });
    } else {
        WriteLock w_lock(_taskLock);
        _mainThreadCallbacks.push_back(task);
    }

    if (wait) {
        WAIT_FOR_CONDITION(done);
    }
}

void Attorney::ApplicationTask::syncThreadToGPU(const Application& app, const std::thread::id& threadID, bool beginSync) {
    Attorney::KernelApplication::syncThreadToGPU(*app._kernel, threadID, beginSync);
}


}; //namespace Divide

Commits for Divide-Framework/trunk/Source Code/Core/Application.cpp

Diff revisions: vs.
Revision Author Commited Message
842 Diff Diff IonutCava picture IonutCava Wed 01 Feb, 2017 17:25:15 +0000

[IonutCava]
- Start to implement scripting support via ChaiScript: http://chaiscript.com/
- Cleanup DELEGATE_CBK alias

835 Diff Diff IonutCava picture IonutCava Fri 27 Jan, 2017 14:58:07 +0000

[IonutCava]
- Split Engine lib into Core and Engine lib.
- Fix Server build issues

805 Diff Diff IonutCava picture IonutCava Fri 02 Dec, 2016 16:05:59 +0000

[IonutCava]
- Singleton elimination update Part I.I: Correct Part I
— Small corrections to previous commit: Fix all asserts, memory leaks and errors that appeared during the following test: Start app -> Load War Scene -> Return to Default Scene -> Quit

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.

790 Diff Diff IonutCava picture IonutCava Wed 02 Nov, 2016 17:06:20 +0000

[IonutCava]
- Fix a few crashes:
— Missing language file during startup causes crash instead of log + exit
— Closing application causes crash due to RenderTarget cleanup of the GPU Object arena
-— Still not fully fixed: leaking graphics resources on shutdown
- Make mat and vec constructors and assignment operators “noexcept” (helps with usage in containers)
- Add a “NonMovable” class, similar to the “NonCopyable” class to help with readability in certain situations

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

777 Diff Diff IonutCava picture IonutCava Fri 07 Oct, 2016 16:14:48 +0000

[IonutCava]
- Continue to implement CommandBuffer / RenderPass / RenderSubPass system.
— Rendering artefacts are still present.

768 Diff Diff IonutCava picture IonutCava Fri 09 Sep, 2016 14:38:51 +0000

[IonutCava]
- Refactored framework entry points (init, step, shutdown) to allow the addition of hot code reloading in the future

762 Diff Diff IonutCava picture IonutCava Thu 11 Aug, 2016 16:18:35 +0000

[IonutCava]
- Removed a lot of high level GFX state and merged it back on a lower level:
— depth mask is now a framebuffer specific toggle controlled by the RenderPolicy
— rasterization is now a per-buffer draw switch toggled by a rendering flag in GenericDrawCommand
- Replaced old style GL texture binding code with DSA style glBindTextures and glBindSamplers even for single textures
— completely removed the concept of a active texture unit in the GL code
- Fixed some draw policy settings that were clearing the depth buffer in the PostFX passes
- More build type flag replacement of macros
- Render pass system bug fixing
- Add option to skip occlusion culling for certain nodes

749 IonutCava picture IonutCava Mon 04 Jul, 2016 16:01:34 +0000

[IonutCava]
- Added initial environment mapping code:
— Allow adding, per scene, of multiple environment probes that generate cube map reflections
— Support 2 types of probes: infinite (such as sky only probes) and local. Local probes use an AABB that will later be used for parallax correction
— Probes are held in a scene specific pool, can be updated at different rates and their results can be passed on to materials (if materials don’t need specific reflection systems such as for water or mirrors)
- ToDo:
— Blend between probes (currently, only the closes one is selected)
— Reduce VRAM usage
— Set as fallback for screen space reflections