-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
engine.cpp
1990 lines (1666 loc) · 57.8 KB
/
engine.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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright 2016 Pixar
//
// Licensed under the terms set forth in the LICENSE.txt file available at
// https://openusd.org/license.
//
#include "pxr/usdImaging/usdImagingGL/engine.h"
#include "pxr/usdImaging/usdImaging/delegate.h"
#include "pxr/usdImaging/usdImaging/selectionSceneIndex.h"
#include "pxr/usdImaging/usdImaging/stageSceneIndex.h"
#include "pxr/usdImaging/usdImaging/rootOverridesSceneIndex.h"
#include "pxr/usdImaging/usdImaging/sceneIndices.h"
#include "pxr/usd/usdGeom/tokens.h"
#include "pxr/usd/usdGeom/camera.h"
#include "pxr/usd/usdRender/tokens.h"
#include "pxr/usd/usdRender/settings.h"
#include "pxr/imaging/hd/materialBindingsSchema.h"
#include "pxr/imaging/hd/light.h"
#include "pxr/imaging/hd/rendererPlugin.h"
#include "pxr/imaging/hd/rendererPluginRegistry.h"
#include "pxr/imaging/hd/retainedDataSource.h"
#include "pxr/imaging/hd/sceneIndexPluginRegistry.h"
#include "pxr/imaging/hd/systemMessages.h"
#include "pxr/imaging/hd/utils.h"
#include "pxr/imaging/hdsi/primTypePruningSceneIndex.h"
#include "pxr/imaging/hdsi/legacyDisplayStyleOverrideSceneIndex.h"
#include "pxr/imaging/hdsi/sceneGlobalsSceneIndex.h"
#include "pxr/imaging/hdx/pickTask.h"
#include "pxr/imaging/hdx/taskController.h"
#include "pxr/imaging/hdx/tokens.h"
#include "pxr/imaging/hgi/hgi.h"
#include "pxr/imaging/hgi/tokens.h"
#include "pxr/base/tf/envSetting.h"
#include "pxr/base/tf/getenv.h"
#include "pxr/base/tf/staticData.h"
#include "pxr/base/tf/stl.h"
#include "pxr/base/gf/matrix4d.h"
#include "pxr/base/gf/vec3d.h"
#include <string>
PXR_NAMESPACE_OPEN_SCOPE
TF_DEFINE_ENV_SETTING(USDIMAGINGGL_ENGINE_DEBUG_SCENE_DELEGATE_ID, "/",
"Default usdImaging scene delegate id");
TF_DEFINE_ENV_SETTING(USDIMAGINGGL_ENGINE_ENABLE_SCENE_INDEX, false,
"Use Scene Index API for imaging scene input");
namespace UsdImagingGLEngine_Impl
{
// Struct that holds application scene indices created via the
// scene index plugin registration callback facility.
struct _AppSceneIndices {
HdsiSceneGlobalsSceneIndexRefPtr sceneGlobalsSceneIndex;
};
};
namespace {
// Use a static tracker to accommodate the use-case where an application spawns
// multiple engines.
using _RenderInstanceAppSceneIndicesTracker =
HdUtils::RenderInstanceTracker<UsdImagingGLEngine_Impl::_AppSceneIndices>;
TfStaticData<_RenderInstanceAppSceneIndicesTracker>
s_renderInstanceTracker;
// ----------------------------------------------------------------------------
SdfPath const&
_GetUsdImagingDelegateId()
{
static SdfPath const delegateId =
SdfPath(TfGetEnvSetting(USDIMAGINGGL_ENGINE_DEBUG_SCENE_DELEGATE_ID));
return delegateId;
}
bool
_GetUseSceneIndices()
{
// Use UsdImagingStageSceneIndex for input if:
// - USDIMAGINGGL_ENGINE_ENABLE_SCENE_INDEX is true (feature flag)
// - HdRenderIndex has scene index emulation enabled (otherwise,
// AddInputScene won't work).
static bool useSceneIndices =
HdRenderIndex::IsSceneIndexEmulationEnabled() &&
(TfGetEnvSetting(USDIMAGINGGL_ENGINE_ENABLE_SCENE_INDEX) == true);
return useSceneIndices;
}
std::string
_GetPlatformDependentRendererDisplayName(HfPluginDesc const &pluginDescriptor)
{
#if defined(__APPLE__)
// Rendering for Storm is delegated to Hgi. We override the
// display name for macOS since the Hgi implementation for
// macOS uses Metal instead of GL. Eventually, this should
// properly delegate to using Hgi to determine the display
// name for Storm.
static const TfToken _stormRendererPluginName("HdStormRendererPlugin");
if (pluginDescriptor.id == _stormRendererPluginName) {
return "Metal";
}
#endif
return pluginDescriptor.displayName;
}
} // anonymous namespace
//----------------------------------------------------------------------------
// Construction
//----------------------------------------------------------------------------
UsdImagingGLEngine::UsdImagingGLEngine(
const Parameters ¶ms)
: UsdImagingGLEngine(
params.rootPath,
params.excludedPaths,
params.invisedPaths,
params.sceneDelegateID,
params.driver,
params.rendererPluginId,
params.gpuEnabled,
params.displayUnloadedPrimsWithBounds,
params.allowAsynchronousSceneProcessing)
{
}
UsdImagingGLEngine::UsdImagingGLEngine(
const HdDriver& driver,
const TfToken& rendererPluginId,
const bool gpuEnabled)
: UsdImagingGLEngine(SdfPath::AbsoluteRootPath(),
{},
{},
_GetUsdImagingDelegateId(),
driver,
rendererPluginId,
gpuEnabled)
{
}
UsdImagingGLEngine::UsdImagingGLEngine(
const SdfPath& rootPath,
const SdfPathVector& excludedPaths,
const SdfPathVector& invisedPaths,
const SdfPath& sceneDelegateID,
const HdDriver& driver,
const TfToken& rendererPluginId,
const bool gpuEnabled,
const bool displayUnloadedPrimsWithBounds,
const bool allowAsynchronousSceneProcessing)
: _hgi()
, _hgiDriver(driver)
, _displayUnloadedPrimsWithBounds(displayUnloadedPrimsWithBounds)
, _gpuEnabled(gpuEnabled)
, _sceneDelegateId(sceneDelegateID)
, _selTracker(std::make_shared<HdxSelectionTracker>())
, _selectionColor(1.0f, 1.0f, 0.0f, 1.0f)
, _domeLightCameraVisibility(true)
, _rootPath(rootPath)
, _excludedPrimPaths(excludedPaths)
, _invisedPrimPaths(invisedPaths)
, _isPopulated(false)
, _allowAsynchronousSceneProcessing(allowAsynchronousSceneProcessing)
{
if (!_gpuEnabled && _hgiDriver.name == HgiTokens->renderDriver &&
_hgiDriver.driver.IsHolding<Hgi*>()) {
TF_WARN("Trying to share GPU resources while disabling the GPU.");
_gpuEnabled = true;
}
// _renderIndex, _taskController, and _sceneDelegate/_sceneIndex
// are initialized by the plugin system.
if (!SetRendererPlugin(!rendererPluginId.IsEmpty() ?
rendererPluginId : _GetDefaultRendererPluginId())) {
TF_CODING_ERROR("No renderer plugins found!");
}
}
void
UsdImagingGLEngine::_DestroyHydraObjects()
{
// Destroy objects in opposite order of construction.
_engine = nullptr;
_taskController = nullptr;
if (_GetUseSceneIndices()) {
if (_renderIndex && _sceneIndex) {
_renderIndex->RemoveSceneIndex(_sceneIndex);
_stageSceneIndex = nullptr;
_rootOverridesSceneIndex = nullptr;
_selectionSceneIndex = nullptr;
_displayStyleSceneIndex = nullptr;
_sceneIndex = nullptr;
}
} else {
_sceneDelegate = nullptr;
}
// Drop the reference to application scene indices so they are destroyed
// during render index destruction.
{
_appSceneIndices = nullptr;
if (_renderIndex) {
s_renderInstanceTracker->UnregisterInstance(
_renderIndex->GetInstanceName());
}
}
_renderIndex = nullptr;
_renderDelegate = nullptr;
}
UsdImagingGLEngine::~UsdImagingGLEngine()
{
TF_PY_ALLOW_THREADS_IN_SCOPE();
_DestroyHydraObjects();
}
//----------------------------------------------------------------------------
// Rendering
//----------------------------------------------------------------------------
void
UsdImagingGLEngine::PrepareBatch(
const UsdPrim& root,
const UsdImagingGLRenderParams& params)
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return;
}
HD_TRACE_FUNCTION();
if (_CanPrepare(root)) {
if (!_isPopulated) {
auto stage = root.GetStage();
if (_GetUseSceneIndices()) {
TF_VERIFY(_stageSceneIndex);
_stageSceneIndex->SetStage(stage);
// XXX(USD-7113): Add pruning based on _rootPath,
// _excludedPrimPaths
// XXX(USD-7114): Add draw mode support based on
// params.enableUsdDrawModes.
// XXX(USD-7115): Add invis overrides from _invisedPrimPaths.
} else {
TF_VERIFY(_sceneDelegate);
_sceneDelegate->SetUsdDrawModesEnabled(
params.enableUsdDrawModes);
_sceneDelegate->Populate(
stage->GetPrimAtPath(_rootPath),
_excludedPrimPaths);
_sceneDelegate->SetInvisedPrimPaths(_invisedPrimPaths);
// This is only necessary when using the legacy scene delegate.
// The stage scene index provides this functionality.
_SetActiveRenderSettingsPrimFromStageMetadata(stage);
}
_isPopulated = true;
}
_PreSetTime(params);
// SetTime will only react if time actually changes.
if (_GetUseSceneIndices()) {
_stageSceneIndex->SetTime(params.frame);
} else {
_sceneDelegate->SetTime(params.frame);
}
_SetSceneGlobalsCurrentFrame(params.frame);
_PostSetTime(params);
}
}
void
UsdImagingGLEngine::_PrepareRender(const UsdImagingGLRenderParams& params)
{
TF_VERIFY(_taskController);
_taskController->SetFreeCameraClipPlanes(params.clipPlanes);
TfTokenVector renderTags;
_ComputeRenderTags(params, &renderTags);
_taskController->SetRenderTags(renderTags);
_taskController->SetRenderParams(
_MakeHydraUsdImagingGLRenderParams(params));
// Forward scene materials enable option.
if (_GetUseSceneIndices()) {
if (_materialPruningSceneIndex) {
_materialPruningSceneIndex->SetEnabled(
!params.enableSceneMaterials);
}
if (_lightPruningSceneIndex) {
_lightPruningSceneIndex->SetEnabled(
!params.enableSceneLights);
}
} else {
_sceneDelegate->SetSceneMaterialsEnabled(params.enableSceneMaterials);
_sceneDelegate->SetSceneLightsEnabled(params.enableSceneLights);
}
}
void
UsdImagingGLEngine::_SetActiveRenderSettingsPrimFromStageMetadata(
UsdStageWeakPtr stage)
{
if (!TF_VERIFY(_renderIndex) || !TF_VERIFY(stage)) {
return;
}
// If we already have an opinion, skip the stage metadata.
if (!HdUtils::HasActiveRenderSettingsPrim(
_renderIndex->GetTerminalSceneIndex())) {
std::string pathStr;
if (stage->HasAuthoredMetadata(
UsdRenderTokens->renderSettingsPrimPath)) {
stage->GetMetadata(
UsdRenderTokens->renderSettingsPrimPath, &pathStr);
}
// Add the delegateId prefix since the scene globals scene index is
// inserted into the merging scene index.
if (!pathStr.empty()) {
SetActiveRenderSettingsPrimPath(
SdfPath(pathStr).ReplacePrefix(
SdfPath::AbsoluteRootPath(), _sceneDelegateId));
}
}
}
void
UsdImagingGLEngine::_UpdateDomeLightCameraVisibility()
{
if (!_renderIndex->IsSprimTypeSupported(HdPrimTypeTokens->domeLight)) {
return;
}
// Check to see if the dome light camera visibility has changed, and mark
// the dome light prim as dirty if it has.
//
// Note: The dome light camera visibility setting is handled via the
// HdRenderSettingsMap on the HdRenderDelegate because this ensures all
// backends can access this setting when they need to.
// The absence of a setting in the map is the same as camera visibility
// being on.
const bool domeLightCamVisSetting = _renderDelegate->
GetRenderSetting<bool>(
HdRenderSettingsTokens->domeLightCameraVisibility,
true);
if (_domeLightCameraVisibility != domeLightCamVisSetting) {
// Camera visibility state changed, so we need to mark any dome lights
// as dirty to ensure they have the proper state on all backends.
_domeLightCameraVisibility = domeLightCamVisSetting;
SdfPathVector domeLights = _renderIndex->GetSprimSubtree(
HdPrimTypeTokens->domeLight, SdfPath::AbsoluteRootPath());
for (SdfPathVector::iterator domeLightIt = domeLights.begin();
domeLightIt != domeLights.end();
++domeLightIt) {
_renderIndex->GetChangeTracker().MarkSprimDirty(
*domeLightIt, HdLight::DirtyParams);
}
}
}
void
UsdImagingGLEngine::_SetBBoxParams(
const BBoxVector& bboxes,
const GfVec4f& bboxLineColor,
float bboxLineDashSize)
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return;
}
HdxBoundingBoxTaskParams params;
params.bboxes = bboxes;
params.color = bboxLineColor;
params.dashSize = bboxLineDashSize;
_taskController->SetBBoxParams(params);
}
void
UsdImagingGLEngine::RenderBatch(
const SdfPathVector& paths,
const UsdImagingGLRenderParams& params)
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return;
}
_UpdateHydraCollection(&_renderCollection, paths, params);
_taskController->SetCollection(_renderCollection);
_PrepareRender(params);
SetColorCorrectionSettings(params.colorCorrectionMode, params.ocioDisplay,
params.ocioView, params.ocioColorSpace, params.ocioLook);
_SetBBoxParams(params.bboxes, params.bboxLineColor, params.bboxLineDashSize);
// XXX App sets the clear color via 'params' instead of setting up Aovs
// that has clearColor in their descriptor. So for now we must pass this
// clear color to the color AOV.
HdAovDescriptor colorAovDesc =
_taskController->GetRenderOutputSettings(HdAovTokens->color);
if (colorAovDesc.format != HdFormatInvalid) {
colorAovDesc.clearValue = VtValue(params.clearColor);
_taskController->SetRenderOutputSettings(
HdAovTokens->color, colorAovDesc);
}
_taskController->SetEnableSelection(params.highlight);
VtValue selectionValue(_selTracker);
_engine->SetTaskContextData(HdxTokens->selectionState, selectionValue);
_UpdateDomeLightCameraVisibility();
_Execute(params, _taskController->GetRenderingTasks());
}
void
UsdImagingGLEngine::Render(
const UsdPrim& root,
const UsdImagingGLRenderParams ¶ms)
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return;
}
TF_PY_ALLOW_THREADS_IN_SCOPE();
PrepareBatch(root, params);
// XXX(UsdImagingPaths): This bit is weird: we get the stage from "root",
// gate population by _rootPath (which may be different), and then pass
// root.GetPath() to hydra as the root to draw from. Note that this
// produces incorrect results in UsdImagingDelegate for native instancing.
const SdfPathVector paths = {
root.GetPath().ReplacePrefix(
SdfPath::AbsoluteRootPath(), _sceneDelegateId)
};
RenderBatch(paths, params);
}
bool
UsdImagingGLEngine::IsConverged() const
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return true;
}
return _taskController->IsConverged();
}
//----------------------------------------------------------------------------
// Root and Transform Visibility
//----------------------------------------------------------------------------
void
UsdImagingGLEngine::SetRootTransform(GfMatrix4d const& xf)
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return;
}
if (_GetUseSceneIndices()) {
_rootOverridesSceneIndex->SetRootTransform(xf);
} else {
_sceneDelegate->SetRootTransform(xf);
}
}
void
UsdImagingGLEngine::SetRootVisibility(const bool isVisible)
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return;
}
if (_GetUseSceneIndices()) {
_rootOverridesSceneIndex->SetRootVisibility(isVisible);
} else {
_sceneDelegate->SetRootVisibility(isVisible);
}
}
//----------------------------------------------------------------------------
// Camera and Light State
//----------------------------------------------------------------------------
void
UsdImagingGLEngine::SetRenderViewport(GfVec4d const& viewport)
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return;
}
_taskController->SetRenderViewport(viewport);
}
void
UsdImagingGLEngine::SetFraming(CameraUtilFraming const& framing)
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return;
}
_taskController->SetFraming(framing);
}
void
UsdImagingGLEngine::SetOverrideWindowPolicy(
const std::optional<CameraUtilConformWindowPolicy> &policy)
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return;
}
_taskController->SetOverrideWindowPolicy(policy);
}
void
UsdImagingGLEngine::SetRenderBufferSize(GfVec2i const& size)
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return;
}
_taskController->SetRenderBufferSize(size);
}
void
UsdImagingGLEngine::SetWindowPolicy(CameraUtilConformWindowPolicy policy)
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return;
}
// Note: Free cam uses SetCameraState, which expects the frustum to be
// pre-adjusted for the viewport size.
if (_GetUseSceneIndices()) {
// XXX(USD-7115): window policy
} else {
// The usdImagingDelegate manages the window policy for scene cameras.
_sceneDelegate->SetWindowPolicy(policy);
}
}
void
UsdImagingGLEngine::SetCameraPath(SdfPath const& id)
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return;
}
_taskController->SetCameraPath(id);
// The camera that is set for viewing will also be used for
// time sampling.
// XXX(HYD-2304): motion blur shutter window.
if (!_GetUseSceneIndices()) {
_sceneDelegate->SetCameraForSampling(id);
}
}
void
UsdImagingGLEngine::SetCameraState(const GfMatrix4d& viewMatrix,
const GfMatrix4d& projectionMatrix)
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return;
}
_taskController->SetFreeCameraMatrices(viewMatrix, projectionMatrix);
}
void
UsdImagingGLEngine::SetLightingState(GlfSimpleLightingContextPtr const &src)
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return;
}
_taskController->SetLightingState(src);
}
void
UsdImagingGLEngine::SetLightingState(
GlfSimpleLightVector const &lights,
GlfSimpleMaterial const &material,
GfVec4f const &sceneAmbient)
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return;
}
// we still use _lightingContextForOpenGLState for convenience, but
// set the values directly.
if (!_lightingContextForOpenGLState) {
_lightingContextForOpenGLState = GlfSimpleLightingContext::New();
}
_lightingContextForOpenGLState->SetLights(lights);
_lightingContextForOpenGLState->SetMaterial(material);
_lightingContextForOpenGLState->SetSceneAmbient(sceneAmbient);
_lightingContextForOpenGLState->SetUseLighting(lights.size() > 0);
_taskController->SetLightingState(_lightingContextForOpenGLState);
}
//----------------------------------------------------------------------------
// Selection Highlighting
//----------------------------------------------------------------------------
void
UsdImagingGLEngine::SetSelected(SdfPathVector const& paths)
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return;
}
if (_GetUseSceneIndices()) {
_selectionSceneIndex->ClearSelection();
for (const SdfPath &path : paths) {
_selectionSceneIndex->AddSelection(path);
}
return;
}
TF_VERIFY(_sceneDelegate);
// populate new selection
HdSelectionSharedPtr const selection = std::make_shared<HdSelection>();
// XXX: Usdview currently supports selection on click. If we extend to
// rollover (locate) selection, we need to pass that mode here.
static const HdSelection::HighlightMode mode =
HdSelection::HighlightModeSelect;
for (SdfPath const& path : paths) {
_sceneDelegate->PopulateSelection(mode,
path,
UsdImagingDelegate::ALL_INSTANCES,
selection);
}
// set the result back to selection tracker
_selTracker->SetSelection(selection);
}
void
UsdImagingGLEngine::ClearSelected()
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return;
}
if (_GetUseSceneIndices()) {
_selectionSceneIndex->ClearSelection();
return;
}
TF_VERIFY(_selTracker);
_selTracker->SetSelection(std::make_shared<HdSelection>());
}
HdSelectionSharedPtr
UsdImagingGLEngine::_GetSelection() const
{
if (HdSelectionSharedPtr const selection = _selTracker->GetSelectionMap()) {
return selection;
}
return std::make_shared<HdSelection>();
}
void
UsdImagingGLEngine::AddSelected(SdfPath const &path, int instanceIndex)
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return;
}
if (_GetUseSceneIndices()) {
_selectionSceneIndex->AddSelection(path);
return;
}
TF_VERIFY(_sceneDelegate);
HdSelectionSharedPtr const selection = _GetSelection();
// XXX: Usdview currently supports selection on click. If we extend to
// rollover (locate) selection, we need to pass that mode here.
static const HdSelection::HighlightMode mode =
HdSelection::HighlightModeSelect;
_sceneDelegate->PopulateSelection(mode, path, instanceIndex, selection);
// set the result back to selection tracker
_selTracker->SetSelection(selection);
}
void
UsdImagingGLEngine::SetSelectionColor(GfVec4f const& color)
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return;
}
_selectionColor = color;
_taskController->SetSelectionColor(_selectionColor);
}
//----------------------------------------------------------------------------
// Picking
//----------------------------------------------------------------------------
bool
UsdImagingGLEngine::TestIntersection(
const GfMatrix4d &viewMatrix,
const GfMatrix4d &projectionMatrix,
const UsdPrim& root,
const UsdImagingGLRenderParams& params,
GfVec3d *outHitPoint,
GfVec3d *outHitNormal,
SdfPath *outHitPrimPath,
SdfPath *outHitInstancerPath,
int *outHitInstanceIndex,
HdInstancerContext *outInstancerContext)
{
PickParams pickParams = { HdxPickTokens->resolveNearestToCenter };
IntersectionResultVector results;
if (TestIntersection(pickParams, viewMatrix, projectionMatrix,
root, params, &results)) {
if (results.size() != 1) {
// Since we are in nearest-hit mode, we expect allHits to have a
// single point in it.
return false;
}
IntersectionResult &result = results.front();
if (outHitPoint) {
*outHitPoint = result.hitPoint;
}
if (outHitNormal) {
*outHitNormal = result.hitNormal;
}
if (outHitPrimPath) {
*outHitPrimPath = result.hitPrimPath;
}
if (outHitInstancerPath) {
*outHitInstancerPath = result.hitInstancerPath;
}
if (outHitInstanceIndex) {
*outHitInstanceIndex = result.hitInstanceIndex;
}
if (outInstancerContext) {
*outInstancerContext = result.instancerContext;
}
return true;
}
return false;
}
bool
UsdImagingGLEngine::TestIntersection(
const PickParams& pickParams,
const GfMatrix4d& viewMatrix,
const GfMatrix4d& projectionMatrix,
const UsdPrim& root,
const UsdImagingGLRenderParams& params,
IntersectionResultVector* outResults)
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return false;
}
PrepareBatch(root, params);
// XXX(UsdImagingPaths): This is incorrect... "Root" points to a USD
// subtree, but the subtree in the hydra namespace might be very different
// (e.g. for native instancing). We need a translation step.
const SdfPathVector paths = {
root.GetPath().ReplacePrefix(
SdfPath::AbsoluteRootPath(), _sceneDelegateId)
};
_UpdateHydraCollection(&_intersectCollection, paths, params);
_PrepareRender(params);
HdxPickHitVector allHits;
HdxPickTaskContextParams pickCtxParams;
pickCtxParams.resolveMode = pickParams.resolveMode;
pickCtxParams.viewMatrix = viewMatrix;
pickCtxParams.projectionMatrix = projectionMatrix;
pickCtxParams.clipPlanes = params.clipPlanes;
pickCtxParams.collection = _intersectCollection;
pickCtxParams.outHits = &allHits;
const VtValue vtPickCtxParams(pickCtxParams);
_engine->SetTaskContextData(HdxPickTokens->pickParams, vtPickCtxParams);
_Execute(params, _taskController->GetPickingTasks());
// return false if there were no hits
if (allHits.size() == 0) {
return false;
}
for(HdxPickHit& hit : allHits)
{
IntersectionResult res;
if (_sceneDelegate) {
res.hitPrimPath = _sceneDelegate->GetScenePrimPath(
hit.objectId, hit.instanceIndex, &(res.instancerContext));
res.hitInstancerPath = _sceneDelegate->ConvertIndexPathToCachePath(
hit.instancerId).GetAbsoluteRootOrPrimPath();
} else {
const HdxPrimOriginInfo info = HdxPrimOriginInfo::FromPickHit(
_renderIndex.get(), hit);
res.hitPrimPath = info.GetFullPath();
res.hitInstancerPath = hit.instancerId.ReplacePrefix(
_sceneDelegateId, SdfPath::AbsoluteRootPath());
res.instancerContext = info.ComputeInstancerContext();
}
res.hitPoint = hit.worldSpaceHitPoint;
res.hitNormal = hit.worldSpaceHitNormal;
res.hitInstanceIndex = hit.instanceIndex;
if (outResults) {
outResults->push_back(res);
}
}
return true;
}
bool
UsdImagingGLEngine::DecodeIntersection(
unsigned char const primIdColor[4],
unsigned char const instanceIdColor[4],
SdfPath *outHitPrimPath,
SdfPath *outHitInstancerPath,
int *outHitInstanceIndex,
HdInstancerContext *outInstancerContext)
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return false;
}
const int primId = HdxPickTask::DecodeIDRenderColor(primIdColor);
const int instanceIdx = HdxPickTask::DecodeIDRenderColor(instanceIdColor);
SdfPath primPath = _renderIndex->GetRprimPathFromPrimId(primId);
if (primPath.IsEmpty()) {
return false;
}
SdfPath delegateId, instancerId;
_renderIndex->GetSceneDelegateAndInstancerIds(
primPath, &delegateId, &instancerId);
if (_sceneDelegate) {
primPath = _sceneDelegate->GetScenePrimPath(
primPath, instanceIdx, outInstancerContext);
instancerId = _sceneDelegate->ConvertIndexPathToCachePath(instancerId)
.GetAbsoluteRootOrPrimPath();
} else {
HdxPickHit hit;
hit.delegateId = delegateId;
hit.objectId = primPath;
hit.instancerId = instancerId;
hit.instanceIndex = instanceIdx;
const HdxPrimOriginInfo info = HdxPrimOriginInfo::FromPickHit(
_renderIndex.get(), hit);
primPath = info.GetFullPath();
instancerId = instancerId.ReplacePrefix(_sceneDelegateId,
SdfPath::AbsoluteRootPath());
if (outInstancerContext) {
*outInstancerContext = info.ComputeInstancerContext();
}
}
if (outHitPrimPath) {
*outHitPrimPath = primPath;
}
if (outHitInstancerPath) {
*outHitInstancerPath = instancerId;
}
if (outHitInstanceIndex) {
*outHitInstanceIndex = instanceIdx;
}
return true;
}
//----------------------------------------------------------------------------
// Renderer Plugin Management
//----------------------------------------------------------------------------
/* static */
TfTokenVector
UsdImagingGLEngine::GetRendererPlugins()
{
HfPluginDescVector pluginDescriptors;
HdRendererPluginRegistry::GetInstance().GetPluginDescs(&pluginDescriptors);
TfTokenVector plugins;
for(size_t i = 0; i < pluginDescriptors.size(); ++i) {
plugins.push_back(pluginDescriptors[i].id);
}
return plugins;
}
/* static */
std::string
UsdImagingGLEngine::GetRendererDisplayName(TfToken const &id)
{
HfPluginDesc pluginDescriptor;
bool foundPlugin = HdRendererPluginRegistry::GetInstance().
GetPluginDesc(id, &pluginDescriptor);
if (!foundPlugin) {
return std::string();
}
return _GetPlatformDependentRendererDisplayName(pluginDescriptor);
}
bool
UsdImagingGLEngine::GetGPUEnabled() const
{
return _gpuEnabled;
}
TfToken
UsdImagingGLEngine::GetCurrentRendererId() const
{
if (ARCH_UNLIKELY(!_renderDelegate)) {
return TfToken();
}
return _renderDelegate.GetPluginId();
}
void
UsdImagingGLEngine::_InitializeHgiIfNecessary()
{
// If the client of UsdImagingGLEngine does not provide a HdDriver, we
// construct a default one that is owned by UsdImagingGLEngine.
// The cleanest pattern is for the client app to provide this since you
// may have multiple UsdImagingGLEngines in one app that ideally all use
// the same HdDriver and Hgi to share GPU resources.
if (_gpuEnabled && _hgiDriver.driver.IsEmpty()) {
_hgi = Hgi::CreatePlatformDefaultHgi();
_hgiDriver.name = HgiTokens->renderDriver;
_hgiDriver.driver = VtValue(_hgi.get());
}
}
bool
UsdImagingGLEngine::SetRendererPlugin(TfToken const &id)
{
_InitializeHgiIfNecessary();
HdRendererPluginRegistry ®istry =
HdRendererPluginRegistry::GetInstance();
TfToken resolvedId;
if (id.IsEmpty()) {
// Special case: id == TfToken() selects the first supported plugin in
// the list.
resolvedId = registry.GetDefaultPluginId(_gpuEnabled);
} else {
HdRendererPluginHandle plugin = registry.GetOrCreateRendererPlugin(id);