-
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathRenderScriptingInterface.cpp
More file actions
464 lines (385 loc) · 17.9 KB
/
Copy pathRenderScriptingInterface.cpp
File metadata and controls
464 lines (385 loc) · 17.9 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//
// Created by Sam Gondelman on 5/16/19
// Copyright 2013-2019 High Fidelity, Inc.
// Copyright 2022-2023 Overte e.V.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// SPDX-License-Identifier: Apache-2.0
//
#include "RenderScriptingInterface.h"
#include <QScreen>
#include <QtQml>
#include <RenderCommonTask.h>
#include <ScriptEngineCast.h>
#include "LightingModel.h"
#include "Menu.h"
#include "ScreenName.h"
#include <procedural/Procedural.h>
STATIC_SCRIPT_TYPES_INITIALIZER((+[](ScriptManager* manager){
auto scriptEngine = manager->engine().get();
scriptRegisterMetaType<RenderScriptingInterface::RenderMethod, scriptValueFromEnumClass<RenderScriptingInterface::RenderMethod>, scriptValueToEnumClass<RenderScriptingInterface::RenderMethod> >(scriptEngine, "RenderMethod");
scriptRegisterMetaType<AntialiasingSetupConfig::Mode, scriptValueFromEnumClass<AntialiasingSetupConfig::Mode>, scriptValueToEnumClass<AntialiasingSetupConfig::Mode> >(scriptEngine, "Mode");
}));
STATIC_SCRIPT_INITIALIZER(+[](ScriptManager* manager){
auto scriptEngine = manager->engine().get();
auto scopeGuard = scriptEngine->getScopeGuard();
auto* sgp = scopeGuard.get();
scriptEngine->registerEnum(sgp, "Render.RenderMethod",QMetaEnum::fromType<RenderScriptingInterface::RenderMethod>());
scriptEngine->registerEnum(sgp, "AntialiasingMode",QMetaEnum::fromType<AntialiasingSetupConfig::Mode>());
});
RenderScriptingInterface* RenderScriptingInterface::getInstance() {
static RenderScriptingInterface sharedInstance;
return &sharedInstance;
}
std::once_flag RenderScriptingInterface::registry_flag;
RenderScriptingInterface::RenderScriptingInterface() {
std::call_once(registry_flag, [] {
qmlRegisterType<RenderScriptingInterface>("RenderEnums", 1, 0, "RenderEnums");
});
}
void RenderScriptingInterface::loadSettings() {
_renderSettingLock.withReadLock([&] {
_renderMethod = _renderMethodSetting.get();
_shadowsEnabled = _shadowsEnabledSetting.get();
_hazeEnabled = _hazeEnabledSetting.get();
_bloomEnabled = _bloomEnabledSetting.get();
_ambientOcclusionEnabled = _ambientOcclusionEnabledSetting.get();
_localLightingEnabled = _localLightingSetting.get();
_proceduralMaterialsEnabled = _proceduralMaterialsEnabledSetting.get();
_antialiasingMode = static_cast<AntialiasingSetupConfig::Mode>(_antialiasingModeSetting.get());
_viewportResolutionScale = _viewportResolutionScaleSetting.get();
_fullScreenScreen = _fullScreenScreenSetting.get();
});
// If full screen screen is not initialized, or set to an invalid value,
// set to the first screen.
auto screens = getScreens();
if (std::find(screens.begin(), screens.end(), _fullScreenScreen) == screens.end()) {
setFullScreenScreen(screens.first());
}
forceRenderMethod((RenderMethod)_renderMethod);
forceShadowsEnabled(_shadowsEnabled);
forceHazeEnabled(_hazeEnabled);
forceBloomEnabled(_bloomEnabled);
forceAmbientOcclusionEnabled(_ambientOcclusionEnabled);
forceLocalLightingEnabled(_localLightingEnabled);
forceProceduralMaterialsEnabled(_proceduralMaterialsEnabled);
forceAntialiasingMode(_antialiasingMode);
forceViewportResolutionScale(_viewportResolutionScale);
}
RenderScriptingInterface::RenderMethod RenderScriptingInterface::getRenderMethod() const {
return (RenderMethod) _renderMethod;
}
void RenderScriptingInterface::setRenderMethod(RenderMethod renderMethod) {
if (isValidRenderMethod(renderMethod) && (_renderMethod != (int) renderMethod)) {
forceRenderMethod(renderMethod);
emit settingsChanged();
}
}
// FIXME: other rendering settings like shadows and
// bloom aren't propagated through mirror views yet
void recursivelyUpdateMirrorRenderMethods(const QString& parentTaskName, int renderMethod, int depth) {
if (depth == RenderMirrorTask::MAX_MIRROR_DEPTH) {
return;
}
auto renderConfig = qApp->getRenderEngine()->getConfiguration();
for (size_t mirrorIndex = 0; mirrorIndex < RenderMirrorTask::MAX_MIRRORS_PER_LEVEL; mirrorIndex++) {
std::string mirrorTaskString = parentTaskName.toStdString() + ".RenderMirrorView" + std::to_string(mirrorIndex) + "Depth" + std::to_string(depth) + ".DeferredForwardSwitch";
auto mirrorConfig = dynamic_cast<render::SwitchConfig*>(renderConfig->getConfig(QString::fromStdString(mirrorTaskString)));
if (mirrorConfig) {
mirrorConfig->setBranch((int)renderMethod);
recursivelyUpdateMirrorRenderMethods(QString::fromStdString(mirrorTaskString) + (renderMethod == 1 ? ".RenderShadowsAndForwardTask.RenderForwardTask" : ".RenderShadowsAndDeferredTask.RenderDeferredTask"),
renderMethod, depth + 1);
}
}
}
void RenderScriptingInterface::forceRenderMethod(RenderMethod renderMethod) {
_renderSettingLock.withWriteLock([&] {
_renderMethod = (int)renderMethod;
_renderMethodSetting.set((int)renderMethod);
auto renderConfig = qApp->getRenderEngine()->getConfiguration();
QString configName = "RenderMainView.DeferredForwardSwitch";
auto config = dynamic_cast<render::SwitchConfig*>(renderConfig->getConfig(configName));
if (config) {
config->setBranch((int)renderMethod);
recursivelyUpdateMirrorRenderMethods(configName + (renderMethod == RenderMethod::FORWARD ? ".RenderShadowsAndForwardTask.RenderForwardTask" : ".RenderShadowsAndDeferredTask.RenderDeferredTask"),
(int)renderMethod, 0);
}
auto secondaryConfig = dynamic_cast<render::SwitchConfig*>(renderConfig->getConfig("RenderSecondView.DeferredForwardSwitch"));
if (secondaryConfig) {
secondaryConfig->setBranch((int)renderMethod);
}
});
}
QStringList RenderScriptingInterface::getRenderMethodNames() const {
static const QStringList refrenderMethodNames = { "DEFERRED", "FORWARD" };
return refrenderMethodNames;
}
void recursivelyUpdateLightingModel(const QString& parentTaskName, std::function<void(MakeLightingModelConfig *)> updateLambda, int depth = -1) {
auto renderConfig = qApp->getRenderEngine()->getConfiguration();
if (depth == -1) {
auto secondaryLightingModelConfig = renderConfig->getConfig<MakeLightingModel>("RenderSecondView.LightingModel");
if (secondaryLightingModelConfig) {
updateLambda(secondaryLightingModelConfig);
}
auto mainLightingModelConfig = renderConfig->getConfig<MakeLightingModel>("RenderMainView.LightingModel");
if (mainLightingModelConfig) {
updateLambda(mainLightingModelConfig);
}
recursivelyUpdateLightingModel("RenderMainView", updateLambda, depth + 1);
} else if (depth == RenderMirrorTask::MAX_MIRROR_DEPTH) {
return;
}
for (size_t mirrorIndex = 0; mirrorIndex < RenderMirrorTask::MAX_MIRRORS_PER_LEVEL; mirrorIndex++) {
std::string mirrorTaskString = parentTaskName.toStdString() + ".RenderMirrorView" + std::to_string(mirrorIndex) + "Depth" + std::to_string(depth);
auto lightingModelConfig = renderConfig->getConfig<MakeLightingModel>(mirrorTaskString + ".LightingModel");
if (lightingModelConfig) {
updateLambda(lightingModelConfig);
recursivelyUpdateLightingModel(QString::fromStdString(mirrorTaskString), updateLambda, depth + 1);
}
}
}
bool RenderScriptingInterface::getShadowsEnabled() const {
return _shadowsEnabled;
}
void RenderScriptingInterface::setShadowsEnabled(bool enabled) {
if (_shadowsEnabled != enabled) {
forceShadowsEnabled(enabled);
emit settingsChanged();
}
}
void RenderScriptingInterface::forceShadowsEnabled(bool enabled) {
_renderSettingLock.withWriteLock([&] {
_shadowsEnabled = (enabled);
_shadowsEnabledSetting.set(enabled);
Menu::getInstance()->setIsOptionChecked(MenuOption::Shadows, enabled);
recursivelyUpdateLightingModel("", [enabled] (MakeLightingModelConfig *config) { config->setShadow(enabled); });
});
}
bool RenderScriptingInterface::getHazeEnabled() const {
return _hazeEnabled;
}
void RenderScriptingInterface::setHazeEnabled(bool enabled) {
if (_hazeEnabled != enabled) {
forceHazeEnabled(enabled);
emit settingsChanged();
}
}
void RenderScriptingInterface::forceHazeEnabled(bool enabled) {
_renderSettingLock.withWriteLock([&] {
_hazeEnabled = (enabled);
_hazeEnabledSetting.set(enabled);
recursivelyUpdateLightingModel("", [enabled] (MakeLightingModelConfig *config) { config->setHaze(enabled); });
});
}
bool RenderScriptingInterface::getBloomEnabled() const {
return _bloomEnabled;
}
void RenderScriptingInterface::setBloomEnabled(bool enabled) {
if (_bloomEnabled != enabled) {
forceBloomEnabled(enabled);
emit settingsChanged();
}
}
void RenderScriptingInterface::forceBloomEnabled(bool enabled) {
_renderSettingLock.withWriteLock([&] {
_bloomEnabled = (enabled);
_bloomEnabledSetting.set(enabled);
recursivelyUpdateLightingModel("", [enabled] (MakeLightingModelConfig *config) { config->setBloom(enabled); });
});
}
bool RenderScriptingInterface::getAmbientOcclusionEnabled() const {
return _ambientOcclusionEnabled;
}
void RenderScriptingInterface::setAmbientOcclusionEnabled(bool enabled) {
if (_ambientOcclusionEnabled != enabled) {
forceAmbientOcclusionEnabled(enabled);
emit settingsChanged();
}
}
void RenderScriptingInterface::forceAmbientOcclusionEnabled(bool enabled) {
_renderSettingLock.withWriteLock([&] {
_ambientOcclusionEnabled = (enabled);
_ambientOcclusionEnabledSetting.set(enabled);
Menu::getInstance()->setIsOptionChecked(MenuOption::AmbientOcclusion, enabled);
recursivelyUpdateLightingModel("", [enabled] (MakeLightingModelConfig *config) { config->setAmbientOcclusion(enabled); });
});
}
bool RenderScriptingInterface::getLocalLightingEnabled() const {
return _localLightingEnabled;
}
void RenderScriptingInterface::setLocalLightingEnabled(bool enabled) {
if (_localLightingEnabled != enabled) {
forceLocalLightingEnabled(enabled);
emit settingsChanged();
}
}
void RenderScriptingInterface::forceLocalLightingEnabled(bool enabled) {
_renderSettingLock.withWriteLock([&] {
_localLightingEnabled = (enabled);
_localLightingSetting.set(enabled);
Menu::getInstance()->setIsOptionChecked(MenuOption::LocalLights, enabled);
recursivelyUpdateLightingModel("", [enabled] (MakeLightingModelConfig *config) { config->setLocalLighting(enabled); });
});
}
bool RenderScriptingInterface::getProceduralMaterialsEnabled() const {
return _proceduralMaterialsEnabled;
}
void RenderScriptingInterface::setProceduralMaterialsEnabled(bool enabled) {
if (_proceduralMaterialsEnabled != enabled) {
forceProceduralMaterialsEnabled(enabled);
emit settingsChanged();
}
}
void RenderScriptingInterface::forceProceduralMaterialsEnabled(bool enabled) {
_renderSettingLock.withWriteLock([&] {
_proceduralMaterialsEnabled = (enabled);
_proceduralMaterialsEnabledSetting.set(enabled);
Menu::getInstance()->setIsOptionChecked(MenuOption::MaterialProceduralShaders, enabled);
Procedural::enableProceduralShaders = enabled;
});
}
AntialiasingSetupConfig::Mode RenderScriptingInterface::getAntialiasingMode() const {
return _antialiasingMode;
}
void RenderScriptingInterface::setAntialiasingMode(AntialiasingSetupConfig::Mode mode) {
if (_antialiasingMode != mode) {
forceAntialiasingMode(mode);
emit settingsChanged();
}
}
void setAntialiasingModeForView(AntialiasingSetupConfig::Mode mode, AntialiasingSetupConfig *antialiasingSetupConfig, AntialiasingConfig *antialiasingConfig) {
switch (mode) {
case AntialiasingSetupConfig::Mode::NONE:
antialiasingSetupConfig->none();
antialiasingConfig->blend = 1;
antialiasingConfig->setDebugFXAA(false);
break;
case AntialiasingSetupConfig::Mode::TAA:
antialiasingSetupConfig->play();
antialiasingConfig->blend = 0.25;
antialiasingConfig->setDebugFXAA(false);
break;
case AntialiasingSetupConfig::Mode::FXAA:
antialiasingSetupConfig->none();
antialiasingConfig->blend = 0.25;
antialiasingConfig->setDebugFXAA(true);
break;
default:
antialiasingSetupConfig->none();
antialiasingConfig->blend = 1;
antialiasingConfig->setDebugFXAA(false);
break;
}
}
void recursivelyUpdateAntialiasingMode(const QString& parentTaskName, AntialiasingSetupConfig::Mode mode, int depth = -1) {
auto renderConfig = qApp->getRenderEngine()->getConfiguration();
if (depth == -1) {
auto secondViewJitterCamConfig = renderConfig->getConfig<AntialiasingSetup>("RenderSecondView.AntialiasingSetup");
auto secondViewAntialiasingConfig = renderConfig->getConfig<Antialiasing>("RenderSecondView.Antialiasing");
if (secondViewJitterCamConfig && secondViewAntialiasingConfig) {
setAntialiasingModeForView(mode, secondViewJitterCamConfig, secondViewAntialiasingConfig);
}
auto mainViewJitterCamConfig = renderConfig->getConfig<AntialiasingSetup>("RenderMainView.AntialiasingSetup");
auto mainViewAntialiasingConfig = renderConfig->getConfig<Antialiasing>("RenderMainView.Antialiasing");
if (mainViewJitterCamConfig && mainViewAntialiasingConfig) {
setAntialiasingModeForView( mode, mainViewJitterCamConfig, mainViewAntialiasingConfig);
}
recursivelyUpdateAntialiasingMode("RenderMainView", mode, depth + 1);
} else if (depth == RenderMirrorTask::MAX_MIRROR_DEPTH) {
return;
}
for (size_t mirrorIndex = 0; mirrorIndex < RenderMirrorTask::MAX_MIRRORS_PER_LEVEL; mirrorIndex++) {
std::string mirrorTaskString = parentTaskName.toStdString() + ".RenderMirrorView" + std::to_string(mirrorIndex) + "Depth" + std::to_string(depth);
auto jitterCamConfig = renderConfig->getConfig<AntialiasingSetup>(mirrorTaskString + ".AntialiasingSetup");
auto antialiasingConfig = renderConfig->getConfig<Antialiasing>(mirrorTaskString + ".Antialiasing");
if (jitterCamConfig && antialiasingConfig) {
setAntialiasingModeForView(mode, jitterCamConfig, antialiasingConfig);
recursivelyUpdateAntialiasingMode(QString::fromStdString(mirrorTaskString), mode, depth + 1);
}
}
}
void RenderScriptingInterface::forceAntialiasingMode(AntialiasingSetupConfig::Mode mode) {
if ((int)mode < 0 || mode >= AntialiasingSetupConfig::Mode::MODE_COUNT) {
mode = AntialiasingSetupConfig::Mode::NONE;
}
_renderSettingLock.withWriteLock([&] {
_antialiasingMode = mode;
_antialiasingModeSetting.set((int)_antialiasingMode);
recursivelyUpdateAntialiasingMode("", _antialiasingMode);
});
}
void RenderScriptingInterface::setVerticalFieldOfView(float fieldOfView) {
if (qApp->getFieldOfView() != fieldOfView) {
qApp->setFieldOfView(fieldOfView);
emit settingsChanged();
}
}
void RenderScriptingInterface::setCameraClippingEnabled(bool enabled) {
if (qApp->getCameraClippingEnabled() != enabled) {
qApp->setCameraClippingEnabled(enabled);
emit settingsChanged();
}
}
QStringList RenderScriptingInterface::getScreens() const {
QStringList screens;
for(QScreen *screen : qApp->screens()) {
screens << ScreenName::getNameForScreen(screen);
}
return screens;
}
bool RenderScriptingInterface::setFullScreenScreen(QString name) {
auto screens = getScreens();
if (std::find(screens.begin(), screens.end(), name) == screens.end()) {
// Screens can come and go and don't have a stable opaque ID, so we
// go by model here. For multiple screens with the same model we get names
// that include a serial number, so it works.
return false;
}
_renderSettingLock.withWriteLock([&] {
_fullScreenScreen = name;
_fullScreenScreenSetting.set(name);
});
emit settingsChanged();
return true;
}
QString RenderScriptingInterface::getFullScreenScreen() const {
return _fullScreenScreen;
}
float RenderScriptingInterface::getViewportResolutionScale() const {
return _viewportResolutionScale;
}
void RenderScriptingInterface::setViewportResolutionScale(float scale) {
if (_viewportResolutionScale != scale) {
forceViewportResolutionScale(scale);
emit settingsChanged();
}
}
void RenderScriptingInterface::forceViewportResolutionScale(float scale) {
// just not negative values or zero
if (scale <= 0.f) {
return;
}
_renderSettingLock.withWriteLock([&] {
_viewportResolutionScale = scale;
_viewportResolutionScaleSetting.set(scale);
auto renderConfig = qApp->getRenderEngine()->getConfiguration();
assert(renderConfig);
auto deferredView = renderConfig->getConfig("RenderMainView.RenderDeferredTask");
if (deferredView) {
deferredView->setProperty("resolutionScale", scale);
}
auto forwardView = renderConfig->getConfig("RenderMainView.RenderShadowsAndForwardTask.RenderForwardTask");
if (forwardView) {
forwardView->setProperty("resolutionScale", scale);
}
auto deferredSecondView = renderConfig->getConfig("RenderSecondView.RenderDeferredTask");
if (deferredSecondView) {
deferredSecondView->setProperty("resolutionScale", scale);
}
auto forwardSecondView = renderConfig->getConfig("RenderSecondView.RenderShadowsAndForwardTask.RenderForwardTask");
if (forwardSecondView) {
forwardSecondView->setProperty("resolutionScale", scale);
}
});
}