-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathAppGui_Shadows.cpp
501 lines (426 loc) · 16 KB
/
AppGui_Shadows.cpp
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#include "AppGui.h"
#include "settings.h"
#include "RenderConst.h"
#include "GraphicsSystem.h"
#include <OgreRoot.h>
#include <OgreWindow.h>
#include <OgreOverlay.h>
#include <OgreOverlayContainer.h>
#include <OgreOverlayManager.h>
#include <OgreHlmsManager.h>
#include "HlmsPbs2.h"
#include <OgreHlmsUnlitDatablock.h>
#include <OgreHlmsTerra.h>
#include <OgreHlmsCompute.h>
#include <OgreHlmsCommon.h>
#include <Compositor/OgreCompositorManager2.h>
#include <Compositor/OgreCompositorNodeDef.h>
#include <Compositor/OgreCompositorWorkspace.h>
#include <Compositor/OgreCompositorWorkspaceDef.h>
#include <Compositor/OgreCompositorShadowNode.h>
#include <Compositor/OgreCompositorShadowNodeDef.h>
#include <Compositor/Pass/PassScene/OgreCompositorPassScene.h>
#include <Compositor/Pass/PassIblSpecular/OgreCompositorPassIblSpecularDef.h>
#include "Utils/MiscUtils.h"
#include "settings_com.h"
using namespace Ogre;
using namespace std;
// #define USE_STATIC_BRANCHING_FOR_SHADOWMAP_LIGHTS //-
//-----------------------------------------------------------------------------------
void AppGui::setupESM()
{
// For ESM, setup the filter settings (radius and gaussian deviation).
// It controls how blurry the shadows will look.
Ogre::HlmsManager *hlmsManager = Ogre::Root::getSingleton().getHlmsManager();
Ogre::HlmsCompute *hlmsCompute = hlmsManager->getComputeHlms();
Ogre::uint8 R = 8; // kernelRadius
float gauss = 0.5f; // gaussianDeviationFactor
Ogre::uint16 K = 80;
Ogre::HlmsComputeJob *job = 0;
// Setup compute shader filter (faster for large kernels; but
// beware of mobile hardware where compute shaders are slow)
// For reference large kernels means kernelRadius > 2 (approx)
using namespace Demo;
job = hlmsCompute->findComputeJob( "ESM/GaussianLogFilterH" );
MiscUtils::setGaussianLogFilterParams( job, R, gauss, K );
job = hlmsCompute->findComputeJob( "ESM/GaussianLogFilterV" );
MiscUtils::setGaussianLogFilterParams( job, R, gauss, K );
// Setup pixel shader filter (faster for small kernels,
// also to use as a fallback on GPUs with no or slow compute shaders).
MiscUtils::setGaussianLogFilterParams(
"ESM/GaussianLogFilterH", R, gauss, K );
MiscUtils::setGaussianLogFilterParams(
"ESM/GaussianLogFilterV", R, gauss, K );
}
// 🆕 create PCF
//-----------------------------------------------------------------------------------
void AppGui::createPcfShadowNode()
{
CompositorManager2 *mgr = mRoot->getCompositorManager2();
RenderSystem *rs = mRoot->getRenderSystem();
ShadowNodeHelper::ShadowParamVec spv;
ShadowNodeHelper::ShadowParam sp;
silent_memset( &sp, 0, sizeof(sp) );
// light 1, directional
const uint32
size1 = pSet->GetTexSize(pSet->g.shadow_size),
size2 = size1 / 2u; // par /?
// SHADOWMAP_FOCUSED, SHADOWMAP_PLANEOPTIMAL, // for 1
sp.technique = pSet->g.shadow_count > 1 ?
SHADOWMAP_PSSM : SHADOWMAP_FOCUSED;
sp.numPssmSplits = min(4, max(2, pSet->g.shadow_count ));
sp.resolution[0].x = size1; sp.resolution[0].y = size1;
for (size_t i = 1u; i < 4u; ++i)
{ sp.resolution[i].x = size2; sp.resolution[i].y = size2; }
// [0 ]
// [ ]
// [][][] 1 2 3 ?
sp.atlasStart[0].x = 0u; sp.atlasStart[0].y = 0u;
sp.atlasStart[1].x = 0u; sp.atlasStart[1].y = size1;
sp.atlasStart[2].x = size2; sp.atlasStart[2].y = size1;
sp.atlasStart[3].x = size1; sp.atlasStart[3].y = size1;
sp.supportedLightTypes = 0u;
sp.addLightType( Light::LT_DIRECTIONAL );
spv.push_back( sp );
if (pSet->li.front_shdw) // any.. or >1 sun planets..
{
// light 2 dir, spot or point
#ifdef USE_STATIC_BRANCHING_FOR_SHADOWMAP_LIGHTS
sp.atlasId = 1;
#endif
sp.technique = SHADOWMAP_FOCUSED;
sp.resolution[0].x = size1; sp.resolution[0].y = size1;
sp.atlasStart[0].x = 0u;
#ifdef USE_STATIC_BRANCHING_FOR_SHADOWMAP_LIGHTS
sp.atlasStart[0].y = 0u;
#else
sp.atlasStart[0].y = size1 + size2;
#endif
sp.supportedLightTypes = 0u;
#ifdef USE_STATIC_BRANCHING_FOR_SHADOWMAP_LIGHTS
sp.addLightType( Light::LT_DIRECTIONAL );
#endif
sp.addLightType( Light::LT_POINT );
sp.addLightType( Light::LT_SPOTLIGHT );
spv.push_back( sp );
// light 3 dir, spot or point
#ifdef USE_STATIC_BRANCHING_FOR_SHADOWMAP_LIGHTS
sp.atlasStart[0].y = size1;
#else
sp.atlasStart[0].y = size1 + size2 + size1;
#endif
spv.push_back( sp );
#ifdef USE_STATIC_BRANCHING_FOR_SHADOWMAP_LIGHTS
// light 4 -
sp.atlasStart[0].x = size1; sp.atlasStart[0].y = 0u;
spv.push_back( sp );
// light 5
sp.atlasStart[0].x = size1; sp.atlasStart[0].y = size1;
spv.push_back( sp );
#endif
}
// bool esm = pSet->g.shadow_type >= Sh_Soft;
ShadowNodeHelper::createShadowNodeWithSettings(
mgr, rs->getCapabilities(),
// esm ? chooseEsmShadowNode() :
csShadow, spv,
false, // esm ?, // fixme ..
size2, // pointLight CubemapRes = 1024u,
0.95f, // pssmLambda = 0.95f,
1.0f, // splitPadding = 1.0f,
0.125f, // splitBlend = 0.125f,
0.313f, // splitFade = 0.313f,
0, // numStableSplits = 0, //** par ?? would be awesome if it worked, has artifacts..?
VisibilityFlags::RESERVED_VISIBILITY_FLAGS, // visibilityMask
1.5f, // xyPadding = 1.5f,
RQG_Sky, // firstRq = 0u,
RQG_Fluid-1 //** par
// RQG_PipeGlass // lastRq = 255u
);
mSceneMgr->setShadowDirectionalLightExtrusionDistance( pSet->g.shadow_dist );
mSceneMgr->setShadowFarDistance( pSet->g.shadow_dist );
// HlmsDatablock::mShadowConstantBias.
updShadowFilter();
}
// Filtering PCF ----
void AppGui::updShadowFilter()
{
HlmsPbs::ShadowFilter fil =
pSet->g.shadow_type == Sh_Soft ? HlmsPbs::ExponentialShadowMaps : // ESM
(HlmsPbs::ShadowFilter)(pSet->g.shadow_filter); // HlmsPbs::PCF_2x2 .. PCF_6x6,
HlmsManager *hlmsMgr = mRoot->getHlmsManager();
HlmsPbs *hlmsPbs = (HlmsPbs*)hlmsMgr->getHlms( HLMS_PBS );
hlmsPbs->setShadowSettings(fil);
HlmsTerra *hlmsTerra = (HlmsTerra*)hlmsMgr->getHlms( HLMS_USER3 );
hlmsTerra->setShadowSettings(fil);
}
// 🆕 create ESM
//-----------------------------------------------------------------------------------
void AppGui::createEsmShadowNodes()
{
CompositorManager2 *mgr = mRoot->getCompositorManager2();
RenderSystem *rs = mRoot->getRenderSystem();
ShadowNodeHelper::ShadowParamVec spv;
ShadowNodeHelper::ShadowParam sp;
silent_memset( &sp, 0, sizeof( sp ) );
const uint32
size1 = pSet->GetTexSize(pSet->g.shadow_size), size2 = size1 / 2u; // par /?
// SHADOWMAP_PLANEOPTIMAL, // for 1?
// SHADOWMAP_FOCUSED,
sp.technique = SHADOWMAP_PSSM;
sp.numPssmSplits = 3; // fixme why only 3 works ..
// sp.numPssmSplits = min(4, max(2, pSet->g.shadow_count )); // 3u;
// light 1 directional
sp.technique = SHADOWMAP_PSSM;
sp.resolution[0].x = size2; sp.resolution[0].y = size2;
sp.resolution[1].x = size1; sp.resolution[1].y = size1;
sp.resolution[2].x = size2; sp.resolution[2].y = size2;
sp.atlasStart[0].x = 0u; sp.atlasStart[0].y = 0u;
sp.atlasStart[1].x = 0u; sp.atlasStart[1].y = size2;
sp.atlasStart[2].x = size2; sp.atlasStart[2].y = 0u;
sp.supportedLightTypes = 0u;
sp.addLightType( Light::LT_DIRECTIONAL );
spv.push_back( sp );
// light 2 dir, spot or point
#ifdef USE_STATIC_BRANCHING_FOR_SHADOWMAP_LIGHTS
sp.atlasId = 1;
#endif
sp.technique = SHADOWMAP_FOCUSED;
sp.resolution[0].x = size2;
sp.resolution[0].y = size2;
sp.atlasStart[0].x = 0u;
sp.atlasStart[0].y = size1 + size2;
sp.supportedLightTypes = 0u;
sp.addLightType( Light::LT_DIRECTIONAL );
sp.addLightType( Light::LT_POINT );
sp.addLightType( Light::LT_SPOTLIGHT );
spv.push_back( sp );
// light 3 dir, spot or point
sp.atlasStart[0].x = size2;
spv.push_back( sp );
#ifdef USE_STATIC_BRANCHING_FOR_SHADOWMAP_LIGHTS
// light 4 -
sp.atlasStart[0].x = size2;
sp.atlasStart[0].y = 0u;
spv.push_back( sp );
// light 5
sp.atlasStart[0].x = size2;
sp.atlasStart[0].y = size2;
spv.push_back( sp );
#endif
const auto *caps = rs->getCapabilities();
RenderSystemCapabilities capsCopy = *caps;
// Force the utility to create ESM shadow node with compute filters.
// Otherwise it'd create using what's supported by the current GPU.
capsCopy.setCapability( RSC_COMPUTE_PROGRAM );
ShadowNodeHelper::createShadowNodeWithSettings(
mgr, &capsCopy,
"ShadowMapFromCodeEsmShadowNodeCompute", spv,
true );
// Force the utility to create ESM shadow node with graphics filters.
// Otherwise it'd create using what's supported by the current GPU.
capsCopy.unsetCapability( RSC_COMPUTE_PROGRAM );
ShadowNodeHelper::createShadowNodeWithSettings(
mgr, &capsCopy,
"ShadowMapFromCodeEsmShadowNodePixelShader", spv,
true );
}
//-----------------------------------------------------------------------------------
CompositorWorkspace *AppGui::setupShadowCompositor()
{
CompositorManager2 *mgr = mRoot->getCompositorManager2();
const String workspaceName( "ShadowMapFromCodeWorkspace" );
// if (mgr->hasWorkspaceDefinition( workspaceName ) )
// mgr->removeWorkspaceDefinition( workspaceName );
if( !mgr->hasWorkspaceDefinition( workspaceName ) )
{
mgr->createBasicWorkspaceDef(
workspaceName, ColourValue(0,0,0,0), IdString() );
const String nodeDefName =
"AutoGen " + IdString( workspaceName + "/Node" ).getReleaseText();
CompositorNodeDef *nodeDef =
mgr->getNodeDefinitionNonConst( nodeDefName );
CompositorTargetDef *targetDef = nodeDef->getTargetPass( 0 );
const CompositorPassDefVec &passes = targetDef->getCompositorPasses();
assert( dynamic_cast<CompositorPassSceneDef *>( passes[0] ) );
CompositorPassSceneDef *passSceneDef =
static_cast<CompositorPassSceneDef *>( passes[0] );
passSceneDef->mShadowNode = csShadow;
createPcfShadowNode();
// createEsmShadowNodes();
}
// add Workspace
LogO("--++ WS add: Shadows, all: "+toStr(mgr->getNumWorkspaces()));
auto* mWorkspace = mgr->addWorkspace(
mSceneMgr, mWindow->getTexture(),
mCamera, "ShadowMapFromCodeWorkspace", true );
return mWorkspace;
}
//-----------------------------------------------------------------------------------
#ifdef USE_STATIC_BRANCHING_FOR_SHADOWMAP_LIGHTS
void AppGui::registerHlms() override
{
GraphicsSystem::registerHlms();
Root *root = getRoot();
Hlms *hlms = root->getHlmsManager()->getHlms( HLMS_PBS );
assert( dynamic_cast<HlmsPbs2 *>( hlms ) );
HlmsPbs2 *pbs = static_cast<HlmsPbs2 *>( hlms );
if( pbs )
pbs->setStaticBranchingLights( true );
}
#endif
//-----------------------------------------------------------------------------------
const char *AppGui::chooseEsmShadowNode()
{
Root *root = mGraphicsSystem->getRoot();
RenderSystem *rs = root->getRenderSystem();
const auto *caps = rs->getCapabilities();
bool hasCompute = caps->hasCapability( RSC_COMPUTE_PROGRAM );
if( !hasCompute )
{
return "ShadowMapFromCodeEsmShadowNodePixelShader";
}else
{
#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE_IOS
// On iOS, the A7 GPUs have slow compute shaders.
DriverVersion driverVersion = caps->getDriverVersion();
if( driverVersion.major == 1 )
;
return "ShadowMapFromCodeEsmShadowNodePixelShader";
#endif
return "ShadowMapFromCodeEsmShadowNodeCompute";
}
}
#if 0 // debug, didnt work anyway
//-----------------------------------------------------------------------------------
// toggle-
void AppGui::setupShadowNode( bool forEsm )
{
Root *root = mGraphicsSystem->getRoot();
CompositorManager2 *mgr = root->getCompositorManager2();
const String nodeDefName =
"AutoGen " + IdString( "ShadowMapFromCodeWorkspace/Node" ).getReleaseText();
CompositorNodeDef *nodeDef = mgr->getNodeDefinitionNonConst( nodeDefName );
CompositorTargetDef *targetDef = nodeDef->getTargetPass( 0 );
const CompositorPassDefVec &passes = targetDef->getCompositorPasses();
assert( dynamic_cast<CompositorPassSceneDef *>( passes[0] ) );
CompositorPassSceneDef *ps =
static_cast<CompositorPassSceneDef *>( passes[0] );
if( forEsm && ps->mShadowNode == csShadow )
{
destroyShadowMapDebugOverlays();
mGraphicsSystem->stopCompositor();
ps->mShadowNode = chooseEsmShadowNode();
mGraphicsSystem->restartCompositor();
createShadowMapDebugOverlays();
}
else if( !forEsm && ps->mShadowNode != csShadow )
{
destroyShadowMapDebugOverlays();
mGraphicsSystem->stopCompositor();
ps->mShadowNode = csShadow;
mGraphicsSystem->restartCompositor();
createShadowMapDebugOverlays();
}
}
//-----------------------------------------------------------------------------------
void AppGui::createShadowMapDebugOverlays()
{
destroyShadowMapDebugOverlays();
Root *root = mGraphicsSystem->getRoot();
// mWorkspaces[0]..
CompositorWorkspace *workspace = mGraphicsSystem->getCompositorWorkspace();
Hlms *hlmsUnlit = root->getHlmsManager()->getHlms( HLMS_UNLIT );
HlmsMacroblock macroblock;
macroblock.mDepthCheck = false;
HlmsBlendblock blendblock;
bool isUsingEsm = false;
{
Hlms *hlms = root->getHlmsManager()->getHlms( HLMS_PBS );
assert( dynamic_cast<HlmsPbs2 *>( hlms ) );
HlmsPbs2 *pbs = static_cast<HlmsPbs2 *>( hlms );
isUsingEsm = pbs->getShadowFilter() == HlmsPbs::ExponentialShadowMaps;
}
const String shadowNodeName =
isUsingEsm ? chooseEsmShadowNode() : csShadow;
CompositorShadowNode *shadowNode = workspace->findShadowNode( shadowNodeName );
const CompositorShadowNodeDef *shadowNodeDef = shadowNode->getDefinition();
for (size_t i = 0u; i < 5u; ++i)
{
const String datablockName( "depthShadow" + StringConverter::toString( i ) );
HlmsUnlitDatablock *depthShadow =
(HlmsUnlitDatablock *)hlmsUnlit->getDatablock( datablockName );
if( !depthShadow )
depthShadow = (HlmsUnlitDatablock *)hlmsUnlit->createDatablock(
datablockName, datablockName, macroblock, blendblock, HlmsParamVec() );
const auto *shadowTexDef = shadowNodeDef->getShadowTextureDefinition( i );
TextureGpu *tex = shadowNode->getDefinedTexture( shadowTexDef->getTextureNameStr() );
depthShadow->setTexture( 0, tex );
// If it's an UV atlas, then only display the relevant section.
Matrix4 uvOffsetScale;
uvOffsetScale.makeTransform(
Vector3( shadowTexDef->uvOffset.x, shadowTexDef->uvOffset.y, 0.0f ),
Vector3( shadowTexDef->uvLength.x, shadowTexDef->uvLength.y, 1.0f ),
Quaternion::IDENTITY );
depthShadow->setEnableAnimationMatrix( 0, true );
depthShadow->setAnimationMatrix( 0, uvOffsetScale );
}
v1::OverlayManager &mgr = v1::OverlayManager::getSingleton();
// Create an overlay
mDebugOverlayPSSM = mgr.create( "PSSM Overlays" );
mDebugOverlaySpotlights = mgr.create( "Spotlight overlays" );
for (int i = 0; i < 3; ++i)
{
// Create a panel
v1::OverlayContainer *panel =
static_cast<v1::OverlayContainer *>( mgr.createOverlayElement(
"Panel", "PanelName" + StringConverter::toString( i ) ) );
panel->setMetricsMode( v1::GMM_RELATIVE_ASPECT_ADJUSTED );
panel->setPosition( 100 + Real( i ) * 1600, 10000 - 1600 );
panel->setDimensions( 1500, 1500 );
panel->setMaterialName( "depthShadow" + StringConverter::toString( i ) );
mDebugOverlayPSSM->add2D( panel );
}
for (int i = 3; i < 5; ++i)
{
// Create a panel
v1::OverlayContainer *panel =
static_cast<v1::OverlayContainer *>( mgr.createOverlayElement(
"Panel", "PanelName" + StringConverter::toString( i ) ) );
panel->setMetricsMode( v1::GMM_RELATIVE_ASPECT_ADJUSTED );
panel->setPosition( 100 + Real( i ) * 1600, 10000 - 1600 );
panel->setDimensions( 1500, 1500 );
panel->setMaterialName( "depthShadow" + StringConverter::toString( i ) );
mDebugOverlaySpotlights->add2D( panel );
}
mDebugOverlayPSSM->show();
mDebugOverlaySpotlights->show();
}
//-----------------------------------------------------------------------------------
void AppGui::destroyShadowMapDebugOverlays()
{
v1::OverlayManager &mgr = v1::OverlayManager::getSingleton();
if( mDebugOverlayPSSM )
{
auto itor = mDebugOverlayPSSM->get2DElementsIterator();
while( itor.hasMoreElements() )
{
v1::OverlayContainer *panel = itor.getNext();
mgr.destroyOverlayElement( panel );
}
mgr.destroy( mDebugOverlayPSSM );
mDebugOverlayPSSM = 0;
}
if( mDebugOverlaySpotlights )
{
auto itor = mDebugOverlaySpotlights->get2DElementsIterator();
while( itor.hasMoreElements() )
{
v1::OverlayContainer *panel = itor.getNext();
mgr.destroyOverlayElement( panel );
}
mgr.destroy( mDebugOverlaySpotlights );
mDebugOverlaySpotlights = 0;
}
}
#endif