Merge branch 'master' into feature/general-performance

This commit is contained in:
Alexander Bock
2020-08-24 15:09:45 +02:00
64 changed files with 860 additions and 251 deletions

View File

@@ -1368,6 +1368,28 @@ void RenderEngine::renderVersionInformation() {
glm::vec4(0.5, 0.5, 0.5, 1.f)
);
}
#ifdef TRACY_ENABLE
{
// If we have Tracy enabled, we should inform the user about it that the
// application will crash after a while if no profiler is attached
ZoneScopedN("Tracy Information")
const glm::vec2 tracyBox = _fontInfo->boundingBox("TRACY PROFILING ENABLED");
const glm::vec2 penPosition = glm::vec2(
fontResolution().x - tracyBox.x - 10.f,
versionBox.y + commitBox.y + 5.f
);
FR::defaultRenderer().render(
*_fontInfo,
penPosition,
"TRACY PROFILING ENABLED",
glm::vec4(0.8f, 0.2f, 0.15f, 1.f)
);
}
#endif // TRACY_ENABLE
}
void RenderEngine::renderScreenLog() {

View File

@@ -191,12 +191,6 @@ void AssetLoader::setUpAssetLuaTable(Asset* asset) {
lua_pushcclosure(*_luaState, &assetloader::require, 1);
lua_setfield(*_luaState, assetTableIndex, RequireFunctionName);
// Register request function
// Dependency request(string path)
lua_pushlightuserdata(*_luaState, asset);
lua_pushcclosure(*_luaState, &assetloader::request, 1);
lua_setfield(*_luaState, assetTableIndex, RequestFunctionName);
// Register exists function
// bool exists(string path)
lua_pushlightuserdata(*_luaState, asset);
@@ -473,14 +467,6 @@ int AssetLoader::onDeinitializeDependencyLua(Asset* dependant, Asset* dependency
return 0;
}
std::shared_ptr<Asset> AssetLoader::request(const std::string& identifier) {
std::shared_ptr<Asset> asset = getAsset(identifier);
Asset* parent = _currentAsset;
parent->request(asset);
assetRequested(parent, asset);
return asset;
}
void AssetLoader::unrequest(const std::string& identifier) {
std::shared_ptr<Asset> asset = has(identifier);
Asset* parent = _currentAsset;
@@ -501,7 +487,11 @@ std::shared_ptr<Asset> AssetLoader::add(const std::string& identifier) {
ZoneScoped
setCurrentAsset(_rootAsset.get());
return request(identifier);
std::shared_ptr<Asset> asset = getAsset(identifier);
Asset* parent = _currentAsset;
parent->request(asset);
assetRequested(parent, asset);
return asset;
}
void AssetLoader::remove(const std::string& identifier) {
@@ -714,32 +704,6 @@ int AssetLoader::requireLua(Asset* dependant) {
return 2;
}
int AssetLoader::requestLua(Asset* parent) {
ghoul::lua::checkArgumentsAndThrow(*_luaState, 1, "lua::request");
const std::string assetName = luaL_checkstring(*_luaState, 1);
lua_settop(*_luaState, 0);
std::shared_ptr<Asset> child = request(assetName);
addLuaDependencyTable(parent, child.get());
// Get the dependency table
lua_rawgeti(*_luaState, LUA_REGISTRYINDEX, _assetsTableRef);
lua_getfield(*_luaState, -1, child->id().c_str());
lua_getfield(*_luaState, -1, DependantsTableName);
lua_getfield(*_luaState, -1, parent->id().c_str());
const int dependencyTableIndex = lua_gettop(*_luaState);
lua_pushvalue(*_luaState, dependencyTableIndex);
lua_replace(*_luaState, 1);
lua_settop(*_luaState, 1);
ghoul_assert(lua_gettop(*_luaState) == 1, "Incorrect number of items left on stack");
return 1;
}
int AssetLoader::existsLua(Asset*) {
ghoul::lua::checkArgumentsAndThrow(*_luaState, 1, "lua::exists");

View File

@@ -71,7 +71,7 @@ int onDeinitializeDependency(lua_State* state) {
}
/**
* Requires rependency
* Requires dependency
* Gives access to
* AssetTable: Exported lua values
* Dependency: ...
@@ -82,17 +82,6 @@ int require(lua_State* state) {
return asset->loader()->requireLua(asset);
}
/**
* Requests rependency
* Gives access to
* Dependency: ...
* Usage: Dependency = asset.import(string assetIdentifier)
*/
int request(lua_State* state) {
Asset* asset = reinterpret_cast<Asset*>(lua_touserdata(state, lua_upvalueindex(1)));
return asset->loader()->requestLua(asset);
}
int exists(lua_State* state) {
Asset* asset = reinterpret_cast<Asset*>(lua_touserdata(state, lua_upvalueindex(1)));
return asset->loader()->existsLua(asset);

View File

@@ -45,6 +45,7 @@ namespace {
constexpr const char* headerProperty = "#Property";
constexpr const char* headerKeybinding = "#Keybinding";
constexpr const char* headerTime = "#Time";
constexpr const char* headerDeltaTimes = "#DeltaTimes";
constexpr const char* headerCamera = "#Camera";
constexpr const char* headerMarkNodes = "#MarkNodes";
constexpr const char* headerAdditionalScripts = "#AdditionalScripts";
@@ -91,6 +92,7 @@ namespace {
Property,
Keybinding,
Time,
DeltaTimes,
Camera,
MarkNodes,
AdditionalScripts
@@ -104,6 +106,7 @@ namespace {
if (line == headerProperty) { return Section::Property; }
if (line == headerKeybinding) { return Section::Keybinding; }
if (line == headerTime) { return Section::Time; }
if (line == headerDeltaTimes) { return Section::DeltaTimes; }
if (line == headerCamera) { return Section::Camera; }
if (line == headerMarkNodes) { return Section::MarkNodes; }
if (line == headerAdditionalScripts) { return Section::AdditionalScripts; }
@@ -318,6 +321,18 @@ namespace {
return time;
}
[[ nodiscard ]] double parseDeltaTime(const std::string& line, int lineNumber) {
try {
return std::stod(line);
}
catch (const std::invalid_argument&) {
throw ProfileParsingError(
lineNumber,
fmt::format("Expected a number for delta time entry, got '{}'", line)
);
}
}
[[ nodiscard ]] Profile::CameraType parseCamera(const std::string& line, int lineNumber) {
std::vector<std::string> fields = ghoul::tokenizeString(line, '\t');
Profile::CameraType camera = [&](const std::string& type) ->
@@ -480,6 +495,10 @@ void Profile::saveCurrentSettingsToProfile(const properties::PropertyOwner& root
t.type = Time::Type::Absolute;
time = std::move(t);
// Delta times
std::vector<double> dts = global::timeManager.deltaTimeSteps();
deltaTimes = std::move(dts);
// Camera
CameraNavState c;
@@ -652,6 +671,13 @@ std::string Profile::serialize() const {
}
}
if (!deltaTimes.empty()) {
output += fmt::format("\n{}\n", headerDeltaTimes);
for (const double d : deltaTimes) {
output += fmt::format("{}\n", d);
}
}
if (camera.has_value()) {
output += fmt::format("\n{}\n", headerCamera);
output += std::visit(
@@ -874,6 +900,12 @@ Profile::Profile(const std::vector<std::string>& content) {
time = parseTime(line, lineNum);
foundTime = true;
break;
case Section::DeltaTimes:
{
const double d = parseDeltaTime(line, lineNum);
deltaTimes.push_back(d);
break;
}
case Section::Camera:
if (foundCamera) {
throw ProfileParsingError(
@@ -985,11 +1017,20 @@ std::string Profile::convertToScene() const {
throw ghoul::MissingCaseException();
}
// Delta Times
{
std::string times;
for (const double d : deltaTimes) {
times += fmt::format("{} ,", d);
}
output += fmt::format("openspace.time.setDeltaTimeSteps({{ {} }});\n", times);
}
// Mark Nodes
{
std::string nodes;
for (const std::string& n : markNodes) {
nodes += fmt::format("[[ {} ]],", n);
nodes += fmt::format("[[{}]],", n);
}
output += fmt::format("openspace.markInterestingNodes({{ {} }});\n", nodes);
}

View File

@@ -143,6 +143,16 @@ scripting::LuaLibrary Time::luaLibrary() {
"Sets the amount of simulation time that happens "
"in one second of real time"
},
{
"setDeltaTimeSteps",
&luascriptfunctions::time_setDeltaTimeSteps,
{},
"List of numbers",
"Sets the list of discrete delta time steps for the simulation speed "
"that can be quickly jumped between. The list will be sorted to be in "
"increasing order. A negative verison of each specified time step will "
"be added per default as well."
},
{
"deltaTime",
&luascriptfunctions::time_deltaTime,
@@ -183,31 +193,74 @@ scripting::LuaLibrary Time::luaLibrary() {
&luascriptfunctions::time_interpolateTimeRelative,
{},
"number [, number]",
"Increments the current simulation time "
"by the specified number of seconds."
"Increments the current simulation time by the specified number of "
"seconds. If a second input value is given, the interpolation is done "
"over the specified number of seconds."
},
{
"interpolateDeltaTime",
&luascriptfunctions::time_interpolateDeltaTime,
{},
"number",
"Sets the amount of simulation time that happens "
"in one second of real time"
"number [, number]",
"Sets the amount of simulation time that happens in one second of real "
"time. If a second input value is given, the interpolation is done "
"over the specified number of seconds."
},
{
"setNextDeltaTimeStep",
&luascriptfunctions::time_setNextDeltaTimeStep,
{},
"",
"Immediately set the simulation speed to the first delta time step in "
"the list that is larger than the current choice of simulation speed, "
"if any."
},
{
"setPreviousDeltaTimeStep",
&luascriptfunctions::time_setPreviousDeltaTimeStep,
{},
"",
"Immediately set the simulation speed to the first delta time step in "
"the list that is smaller than the current choice of simulation speed. "
"if any."
},
{
"interpolateNextDeltaTimeStep",
&luascriptfunctions::time_interpolateNextDeltaTimeStep,
{},
"[number]",
"Interpolate the simulation speed to the first delta time step in the "
"list that is larger than the current simulation speed, if any. If an "
"input value is given, the interpolation is done over the specified "
"number of seconds."
},
{
"interpolatePreviousDeltaTimeStep",
&luascriptfunctions::time_interpolatePreviousDeltaTimeStep,
{},
"[number]",
"Interpolate the simulation speed to the first delta time step in the "
"list that is smaller than the current simulation speed, if any. If an "
"input value is given, the interpolation is done over the specified "
"number of seconds."
},
{
"interpolatePause",
&luascriptfunctions::time_interpolatePause,
{},
"bool",
"Pauses the simulation time or restores the delta time"
"bool [, number]",
"Pauses the simulation time or restores the delta time. If a second "
"input value is given, the interpolation is done over the specified "
"number of seconds."
},
{
"interpolateTogglePause",
&luascriptfunctions::time_interpolateTogglePause,
{},
"",
"[number]",
"Toggles the pause function, i.e. temporarily setting the delta time to 0"
" and restoring it afterwards"
" and restoring it afterwards. If an input value is given, the "
"interpolation is done over the specified number of seconds."
},
{
"currentTime",

View File

@@ -69,6 +69,156 @@ int time_setDeltaTime(lua_State* L) {
return 0;
}
/**
* \ingroup LuaScripts
* interpolateNextDeltaTimeStep(list of numbers):
* Sets the list of discrete delta time steps for the simulation speed.
*/
int time_setDeltaTimeSteps(lua_State* L) {
ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::time_setDeltaTimeSteps");
ghoul::Dictionary dict;
ghoul::lua::luaDictionaryFromState(L, dict);
const size_t nItems = dict.size();
std::vector<double> inputDeltaTimes;
inputDeltaTimes.reserve(nItems);
for (size_t i = 1; i <= nItems; ++i) {
std::string key = std::to_string(i);
if (dict.hasKeyAndValue<double>(key)) {
const double time = dict.value<double>(key);
inputDeltaTimes.push_back(time);
}
else {
const char* msg = lua_pushfstring(L,
"Error setting delta times. Expected list of numbers.");
return ghoul::lua::luaError(L, fmt::format("bad argument ({})", msg));
}
}
lua_pop(L, 1);
global::timeManager.setDeltaTimeSteps(inputDeltaTimes);
lua_settop(L, 0);
ghoul_assert(lua_gettop(L) == 0, "Incorrect number of items left on stack");
return 0;
}
/**
* \ingroup LuaScripts
* setNextDeltaTimeStep():
* Immediately set the simulation speed to the first delta time step in the list that is
* larger than the current choice of simulation speed, if any.
*/
int time_setNextDeltaTimeStep(lua_State* L) {
ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::time_setNextDeltaTimeStep");
global::timeManager.setNextDeltaTimeStep();
lua_settop(L, 0);
ghoul_assert(lua_gettop(L) == 0, "Incorrect number of items left on stack");
return 0;
}
/**
* \ingroup LuaScripts
* setPreviousDeltaTimeStep():
* Immediately set the simulation speed to the first delta time step in the list that is
* smaller than the current choice of simulation speed, if any.
*/
int time_setPreviousDeltaTimeStep(lua_State* L) {
ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::time_setPreviousDeltaTimeStep");
global::timeManager.setPreviousDeltaTimeStep();
lua_settop(L, 0);
ghoul_assert(lua_gettop(L) == 0, "Incorrect number of items left on stack");
return 0;
}
/**
* \ingroup LuaScripts
* interpolateNextDeltaTimeStep([interpolationDuration]):
* Interpolate the simulation speed to the next delta time step in the list. If an input
* value is given, the interpolation is done over the specified number of seconds.
* If interpolationDuration is not provided, the interpolation time will be based on the
* `defaultDeltaTimeInterpolationDuration` property of the TimeManager.
*/
int time_interpolateNextDeltaTimeStep(lua_State* L) {
ghoul::lua::checkArgumentsAndThrow(
L,
{ 0, 1 },
"lua::time_interpolateNextDeltaTimeStep"
);
double interpolationDuration =
global::timeManager.defaultDeltaTimeInterpolationDuration();
const int nArguments = lua_gettop(L);
if (nArguments == 1) {
const bool durationIsNumber = (lua_isnumber(L, 1) != 0);
if (!durationIsNumber) {
lua_settop(L, 0);
const char* msg = lua_pushfstring(
L,
"%s expected, got %s",
lua_typename(L, LUA_TNUMBER),
luaL_typename(L, -1)
);
return luaL_error(L, "bad argument #%d (%s)", 1, msg);
}
interpolationDuration = lua_tonumber(L, 1);
}
global::timeManager.interpolateNextDeltaTimeStep(interpolationDuration);
lua_settop(L, 0);
ghoul_assert(lua_gettop(L) == 0, "Incorrect number of items left on stack");
return 0;
}
/**
* \ingroup LuaScripts
* interpolatePreviousDeltaTimeStep([interpolationDuration]):
* Interpolate the simulation speed to the previous delta time step in the list. If an
* input value is given, the interpolation is done over the specified number of seconds.
* If interpolationDuration is not provided, the interpolation time will be based on the
* `defaultDeltaTimeInterpolationDuration` property of the TimeManager.
*/
int time_interpolatePreviousDeltaTimeStep(lua_State* L) {
ghoul::lua::checkArgumentsAndThrow(
L,
{ 0, 1 },
"lua::time_interpolatePreviousDeltaTimeStep"
);
double interpolationDuration =
global::timeManager.defaultDeltaTimeInterpolationDuration();
const int nArguments = lua_gettop(L);
if (nArguments == 1) {
const bool durationIsNumber = (lua_isnumber(L, 1) != 0);
if (!durationIsNumber) {
lua_settop(L, 0);
const char* msg = lua_pushfstring(
L,
"%s expected, got %s",
lua_typename(L, LUA_TNUMBER),
luaL_typename(L, -1)
);
return luaL_error(L, "bad argument #%d (%s)", 1, msg);
}
interpolationDuration = lua_tonumber(L, 1);
}
global::timeManager.interpolatePreviousDeltaTimeStep(interpolationDuration);
lua_settop(L, 0);
ghoul_assert(lua_gettop(L) == 0, "Incorrect number of items left on stack");
return 0;
}
/**
* \ingroup LuaScripts
* time_interpolateDeltaTime(number [, number]):

View File

@@ -159,6 +159,13 @@ void TimeManager::preSynchronization(double dt) {
it.second();
}
}
if (_deltaTimeStepsChanged) {
using K = const CallbackHandle;
using V = TimeChangeCallback;
for (const std::pair<K, V>& it : _deltaTimeStepsChangeCallbacks) {
it.second();
}
}
if (_timelineChanged) {
ZoneScopedN("timeline changed")
using K = const CallbackHandle;
@@ -173,6 +180,7 @@ void TimeManager::preSynchronization(double dt) {
_lastDeltaTime = _deltaTime;
_lastTargetDeltaTime = _targetDeltaTime;
_lastTimePaused = _timePaused;
_deltaTimeStepsChanged = false;
_timelineChanged = false;
}
@@ -386,6 +394,26 @@ void TimeManager::setDeltaTime(double deltaTime) {
interpolateDeltaTime(deltaTime, 0.0);
}
void TimeManager::setDeltaTimeSteps(std::vector<double> deltaTimes) {
std::vector<double> negatives;
negatives.reserve(deltaTimes.size());
std::transform(
deltaTimes.begin(),
deltaTimes.end(),
std::back_inserter(negatives),
[](double d) { return -d; }
);
deltaTimes.reserve(2 * deltaTimes.size());
deltaTimes.insert(deltaTimes.end(), negatives.begin(), negatives.end());
std::sort(deltaTimes.begin(), deltaTimes.end());
deltaTimes.erase(std::unique(deltaTimes.begin(), deltaTimes.end()), deltaTimes.end());
_deltaTimeSteps = std::move(deltaTimes);
_deltaTimeStepsChanged = true;
}
size_t TimeManager::nKeyframes() const {
return _timeline.nKeyframes();
}
@@ -419,6 +447,14 @@ TimeManager::CallbackHandle TimeManager::addDeltaTimeChangeCallback(TimeChangeCa
return handle;
}
TimeManager::CallbackHandle
TimeManager::addDeltaTimeStepsChangeCallback(TimeChangeCallback cb)
{
CallbackHandle handle = _nextCallbackHandle++;
_deltaTimeStepsChangeCallbacks.emplace_back(handle, std::move(cb));
return handle;
}
TimeManager::CallbackHandle TimeManager::addTimeJumpCallback(TimeChangeCallback cb) {
CallbackHandle handle = _nextCallbackHandle++;
_timeJumpCallbacks.emplace_back(handle, std::move(cb));
@@ -466,6 +502,23 @@ void TimeManager::removeDeltaTimeChangeCallback(CallbackHandle handle) {
_deltaTimeChangeCallbacks.erase(it);
}
void TimeManager::removeDeltaTimeStepsChangeCallback(CallbackHandle handle) {
const auto it = std::find_if(
_deltaTimeStepsChangeCallbacks.begin(),
_deltaTimeStepsChangeCallbacks.end(),
[handle](const std::pair<CallbackHandle, std::function<void()>>& cb) {
return cb.first == handle;
}
);
ghoul_assert(
it != _deltaTimeStepsChangeCallbacks.end(),
"handle must be a valid callback handle"
);
_deltaTimeStepsChangeCallbacks.erase(it);
}
void TimeManager::triggerPlaybackStart() {
_playbackModeEnabled = true;
}
@@ -532,6 +585,10 @@ double TimeManager::targetDeltaTime() const {
return _targetDeltaTime;
}
std::vector<double> TimeManager::deltaTimeSteps() const {
return _deltaTimeSteps;
}
void TimeManager::interpolateDeltaTime(double newDeltaTime, double interpolationDuration)
{
if (newDeltaTime == _targetDeltaTime) {
@@ -559,6 +616,79 @@ void TimeManager::interpolateDeltaTime(double newDeltaTime, double interpolation
addKeyframe(now + interpolationDuration, futureKeyframe);
}
std::optional<double> TimeManager::nextDeltaTimeStep() {
if (!hasNextDeltaTimeStep()) {
return std::nullopt;
}
std::vector<double>::iterator nextStepIterator = std::upper_bound(
_deltaTimeSteps.begin(),
_deltaTimeSteps.end(),
_targetDeltaTime
);
if (nextStepIterator == _deltaTimeSteps.end()) {
return std::nullopt; // should not get here
}
return *nextStepIterator;
}
std::optional<double> TimeManager::previousDeltaTimeStep() {
if (!hasPreviousDeltaTimeStep()) {
return std::nullopt;
}
std::vector<double>::iterator lowerBoundIterator = std::lower_bound(
_deltaTimeSteps.begin(),
_deltaTimeSteps.end(),
_targetDeltaTime
);
if (lowerBoundIterator == _deltaTimeSteps.begin()) {
return std::nullopt; // should not get here
}
std::vector<double>::iterator prevStepIterator = lowerBoundIterator - 1;
return *prevStepIterator;
}
bool TimeManager::hasNextDeltaTimeStep() const {
if (_deltaTimeSteps.empty())
return false;
return _targetDeltaTime < _deltaTimeSteps.back();
}
bool TimeManager::hasPreviousDeltaTimeStep() const {
if (_deltaTimeSteps.empty())
return false;
return _targetDeltaTime > _deltaTimeSteps.front();
}
void TimeManager::setNextDeltaTimeStep() {
interpolateNextDeltaTimeStep(0);
}
void TimeManager::setPreviousDeltaTimeStep() {
interpolatePreviousDeltaTimeStep(0);
}
void TimeManager::interpolateNextDeltaTimeStep(double durationSeconds) {
if (!hasNextDeltaTimeStep())
return;
double nextDeltaTime = nextDeltaTimeStep().value();
interpolateDeltaTime(nextDeltaTime, durationSeconds);
}
void TimeManager::interpolatePreviousDeltaTimeStep(double durationSeconds) {
if (!hasPreviousDeltaTimeStep())
return;
double previousDeltaTime = previousDeltaTimeStep().value();
interpolateDeltaTime(previousDeltaTime, durationSeconds);
}
void TimeManager::setPause(bool pause) {
interpolatePause(pause, 0);
}