Repository navigation
Expand file tree
/
Copy pathMainWindow.xaml.cpp
More file actions
2592 lines (2297 loc) · 110 KB
/
Copy pathMainWindow.xaml.cpp
File metadata and controls
2592 lines (2297 loc) · 110 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
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
#include "pch.h"
#include "MainWindow.xaml.h"
#if __has_include("MainWindow.g.cpp")
#include "MainWindow.g.cpp"
#endif
#include "Rendering/DeviceStack.h"
#include "Rendering/FramePacer.h"
#include "Rendering/PipelineFormat.h"
#include "Rendering/IccProfileParser.h"
#include "Support/EffectGraphFile.h"
#include "Rendering/PixelReadback.h"
#include "Rendering/CaptureNode.h"
#include "Rendering/ImageCapture.h"
#include "Graph/ClockModel.h"
#include "Graph/SourceKind.h"
#include "Graph/GraphEdits.h"
#include "Graph/ValueFormat.h"
#include "Effects/EffectUpgrade.h"
#include "Engine/Utf8.h"
#include "Effects/ShaderLabEffects.h"
#include "Sources/CaptureSources.h"
#include "Sources/DxgiDuplicationSourceProvider.h"
#include "Effects/Performance.h"
#include "Rendering/HostRenderSettings.h"
#include "Engine/EngineBootstrap.h"
#include "Version.h"
#include <microsoft.ui.xaml.media.dxinterop.h>
#include <winrt/Windows.Graphics.Capture.h>
#include <shlobj.h>
#include <KnownFolders.h>
#include <DispatcherQueue.h>
#pragma comment(lib, "CoreMessaging.lib")
using namespace winrt;
using namespace Microsoft::UI::Xaml;
namespace winrt::ShaderLab::implementation
{
MainWindow::MainWindow()
{
InitializeComponent();
Title(std::wstring(L"ShaderLab v") + ::ShaderLab::VersionString + L" \u2014 HDR Shader Effect Development");
// After Title() we'll refresh from RefreshTitleBar() once a graph
// is loaded; the initial state is "Untitled".
m_hwnd = GetWindowHandle();
// Close all output windows and exit the process when the main window closes.
this->Closed([this](auto&&, auto&&)
{
m_isShuttingDown = true;
m_outputWindows.clear();
// Best-effort: remove temp directories we extracted media
// archives into so the user's %TEMP% doesn't accumulate
// stale .effectgraph payloads.
std::error_code ec;
for (const auto& d : m_extractedMediaDirs)
std::filesystem::remove_all(d, ec);
// Join the shader-compile workers now: the window may never be
// destroyed before the process exits. The render worker may still
// be running: GetOrCompile keeps compiling inline, but async
// RequestCompile calls are dropped, so a variant not yet built
// stays missing until exit.
::ShaderLab::Engine::ShutdownEngineServices();
// WinUI 3 keeps the process alive while any Window exists.
// Force exit so output windows don't keep us running.
::PostQuitMessage(0);
});
// Cancellable close: prompt the user when there are unsaved
// changes. AppWindow.Closing is the WinUI 3 hook that can take
// a deferral (so we can show an async dialog and decide whether
// to allow the close after the user picks an option).
this->AppWindow().Closing(
[this](auto&&, winrt::Microsoft::UI::Windowing::AppWindowClosingEventArgs const& args)
{
if (m_isShuttingDown || !m_unsavedChanges) return;
args.Cancel(true);
[](MainWindow* self) -> winrt::fire_and_forget
{
auto strong = self->get_strong();
int32_t choice = co_await self->PromptUnsavedChangesAsync();
if (choice == 2) co_return; // Cancel -> stay open
if (choice == 0) // Save then close
{
if (!self->m_currentFilePath.empty())
co_await self->RunSaveWithProgressAsync();
else
co_await self->SaveGraphAsAsync();
// If the user backed out of the picker, don't close.
if (self->m_unsavedChanges) co_return;
}
self->m_unsavedChanges = false;
self->Close();
}(this);
});
// Wire up event handlers (safe before panel is loaded).
PreviewPanel().SizeChanged({ this, &MainWindow::OnPreviewSizeChanged });
// Display scale changes (moving to a monitor with different scaling)
// change the pixel size without changing the DIP size, so SizeChanged
// does not fire: resize the buffer and refresh the inverse matrix here.
PreviewPanel().CompositionScaleChanged([this](auto&&, auto&&)
{
if (!m_renderEngine.IsInitialized()) return;
auto panel = PreviewPanel();
const float sx = panel.CompositionScaleX(), sy = panel.CompositionScaleY();
m_renderEngine.SetCompositionScale(sx, sy);
m_renderEngine.Resize(
static_cast<uint32_t>((std::max)(1.0, panel.ActualWidth() * sx)),
static_cast<uint32_t>((std::max)(1.0, panel.ActualHeight() * sy)));
m_forceRender = true;
});
PreviewPanel().KeyDown({ this, &MainWindow::OnPreviewKeyDown });
PreviewPanel().IsTabStop(true);
PopulateDisplayProfileSelector();
DisplayProfileSelector().SelectionChanged({ this, &MainWindow::OnDisplayProfileSelectionChanged });
{
::ShaderLab::Controls::PreviewHost host;
host.imageBounds = [this] { return GetPreviewImageBounds(); };
host.backBufferSize = [this]
{
return D2D1_SIZE_F{ static_cast<float>(m_renderEngine.BackBufferWidth()),
static_cast<float>(m_renderEngine.BackBufferHeight()) };
};
host.requestRender = [this] { m_forceRender = true; };
host.clicked = [this](const ::ShaderLab::Controls::PreviewClick& click) { TracePixelAt(click); };
m_preview.Attach(PreviewPanel(), std::move(host));
}
m_tracePanel.Attach(PixelTracePanel(), TracePositionText(), TraceUnitSelector(), CrosshairCanvas());
SaveGraphButton().Click({ this, &MainWindow::OnSaveGraphClicked });
LoadGraphButton().Click({ this, &MainWindow::OnLoadGraphClicked });
AutoArrangeButton().Click([this](auto&&, auto&&)
{
// Reset viewport so the laid-out nodes are visible even if the
// user had zoomed/panned the canvas off-screen.
m_nodeGraphController.SetZoom(1.0f);
m_nodeGraphController.SetPanOffset(0.0f, 0.0f);
m_nodeGraphController.RequestAutoLayout();
m_forceRender = true;
});
UpdateAllEffectsButton().Click([this](auto&&, auto&&)
{
// Graph writes and layout belong to the render thread.
m_renderDispatcher.DispatchSync([this]
{
for (const uint32_t id : ::ShaderLab::Effects::UpgradeOutdatedNodes(Model()))
Evaluator().InvalidateNode(id);
m_nodeGraphController.RebuildLayout();
});
m_forceRender = true;
PopulatePreviewNodeSelector();
m_propertiesPanel.Rebuild();
UpdateOutdatedEffectsButton();
});
// Register user effects before the flyout is built and before the render worker exists.
{
auto& library = ::ShaderLab::Effects::ShaderLabEffects::Instance();
const auto& report = library.LoadUserEffects(
::ShaderLab::Effects::ShaderLabEffects::DefaultUserEffectsDirectory());
for (const auto& name : report.loaded)
OutputDebugStringW((L"[UserEffects] loaded " + name + L"\n").c_str());
for (const auto& err : report.errors)
OutputDebugStringW((L"[UserEffects] " + err + L"\n").c_str());
}
// Populate the Add Node flyout with effects from the registry.
PopulateAddNodeFlyout();
BottomTabView().SelectionChanged([this](auto&&, auto&&)
{
if (m_isShuttingDown) return;
m_tracePanel.UpdateCrosshair(Preview().View(), BottomTabView().SelectedIndex() == 1);
});
// Defer GPU initialization until the SwapChainPanel is in the visual tree.
PreviewPanel().Loaded([this](auto&&, auto&&) { OnPreviewPanelLoaded(); });
NodeGraphPanel().SizeChanged([this](auto&&, winrt::Microsoft::UI::Xaml::SizeChangedEventArgs const& e)
{
ResizeGraphPanel(
static_cast<float>(e.NewSize().Width),
static_cast<float>(e.NewSize().Height));
});
NodeGraphPanel().CompositionScaleChanged([this](auto&&, auto&&)
{
UpdateGraphPanelScale();
ResizeGraphPanel(
static_cast<float>(NodeGraphPanel().ActualWidth()),
static_cast<float>(NodeGraphPanel().ActualHeight()));
});
AttachCanvasInput();
// Save flyout: the reference frame of the saved file is an
// explicit choice, not an inference — a heuristic would darken
// the plain image→PNG round trip or wash presentation-graded
// output (see SaveReference in the header).
{
winrt::Microsoft::UI::Xaml::Controls::MenuFlyout saveFlyout;
auto addSaveItem = [this, &saveFlyout](
winrt::hstring const& text, SaveReference ref)
{
winrt::Microsoft::UI::Xaml::Controls::MenuFlyoutItem item;
item.Text(text);
item.Click([this, ref](auto&&, auto&&) { SaveImageAsync(ref); });
saveFlyout.Items().Append(item);
};
addSaveItem(L"PNG (SDR) — from presentation white", SaveReference::PresentationPng);
addSaveItem(L"PNG (SDR) — file-referenced as-is", SaveReference::FilePng);
addSaveItem(L"JPEG XR (HDR) — scene-referred", SaveReference::HdrJxr);
SaveImageButton().Flyout(saveFlyout);
}
EffectDesignerButton().Click([this](auto&&, auto&&) { OpenEffectDesigner(); });
// The Properties panel reads the published snapshot and writes the
// graph on the render thread, like every other UI edit.
{
::ShaderLab::Controls::PropertiesPanelHost host;
host.snapshot = [this] { return CurrentGraphSnapshot(); };
host.selectedNodeId = [this] { return SelectedNodeId(); };
host.editGraph = [this](const std::function<bool(::ShaderLab::Controls::PanelGraphEdit&)>& edit)
{
return m_renderDispatcher.DispatchSync([this, &edit]
{
::ShaderLab::Controls::PanelGraphEdit context{
Model(),
[this] { m_nodeGraphController.RebuildLayout(); },
[this](uint32_t id) { Evaluator().InvalidateNode(id); },
[this](::ShaderLab::Graph::EffectNode& node)
{
if (auto* dc = m_renderEngine.RenderD2DContext())
Sources().PrepareSourceNode(node, dc, m_renderEngine.D3DDevice(), m_renderEngine.D3DContext());
} };
return edit(context);
});
};
host.requestRender = [this] { m_forceRender = true; };
host.requestCanvasRedraw = [this] { m_nodeGraphController.SetNeedsRedraw(); };
host.refreshNodeList = [this] { PopulatePreviewNodeSelector(); };
host.fitPreview = [this] { Preview().RequestFit(/*keepFitted=*/false); };
host.openInDesigner = [this](uint32_t nodeId, const ::ShaderLab::Graph::CustomEffectDefinition& definition,
const std::wstring& name)
{
OpenEffectDesigner();
winrt::get_self<EffectDesignerWindow>(m_designerWindow)->LoadDefinition(nodeId, definition, name);
};
host.openLog = [this](uint32_t nodeId) { OpenLogWindow(nodeId); };
host.logInfo = [this](uint32_t nodeId, const std::wstring& text) { m_nodeLogs[nodeId].Info(text); };
host.windowHandle = [this] { return m_hwnd; };
host.isShuttingDown = [this] { return m_isShuttingDown.load(); };
host.keepAlive = [this]() -> winrt::Windows::Foundation::IInspectable { return *this; };
m_propertiesPanel.Attach(PropertiesPanel(), std::move(host));
}
// MCP server toggle.
McpServerToggle().Click([this](auto&&, auto&&)
{
bool checked = McpServerToggle().IsChecked().GetBoolean();
// The routes need the context; until the preview panel loads,
// OnPreviewPanelLoaded starts MCP itself.
if (checked && !m_context)
{
McpServerToggle().IsChecked(false);
return;
}
if (checked)
{
if (!m_mcpServer)
SetupMcpRoutes();
// Expose this window to MCP as a hub session; the broker
// (shim -> hub -> session) is the only transport.
StartMcpSession();
UpdateMcpStatusLabel();
McpExportConfigButton().Visibility(winrt::Microsoft::UI::Xaml::Visibility::Visible);
m_mcpIndicator.Reset();
UpdateMcpActivityIndicator();
}
else
{
StopMcpSession();
McpServerLabel().Text(L"MCP: off");
McpExportConfigButton().Visibility(winrt::Microsoft::UI::Xaml::Visibility::Collapsed);
m_mcpIndicator.Reset();
UpdateMcpActivityIndicator();
}
});
McpExportConfigButton().Click([this](auto&&, auto&&)
{
namespace DP = winrt::Windows::ApplicationModel::DataTransfer;
auto jsonEsc = [](const std::wstring& s) {
std::wstring o;
for (wchar_t c : s) { if (c == L'\\' || c == L'"') o += L'\\'; o += c; }
return o;
};
std::wstring config, note;
auto shim = EnsureShimDistributed();
if (!shim.empty())
{
// Preferred: stdio config pointing at the stable unpackaged
// shim copy. The --hub-aumid lets the shim activate the
// packaged hub on demand (client-driven bootstrap).
auto aumid = HubAumid();
std::wstring argsJson = aumid.empty()
? L"\"--stdio\""
: std::format(L"\"--stdio\", \"--hub-aumid\", \"{}\"", jsonEsc(aumid));
config = std::format(
L"{{\n"
L" \"mcpServers\": {{\n"
L" \"shaderlab\": {{\n"
L" \"command\": \"{}\",\n"
L" \"args\": [{}]\n"
L" }}\n"
L" }}\n"
L"}}", jsonEsc(shim), argsJson);
note = L"MCP stdio config copied to clipboard";
}
else
{
// No broker payload found (unexpected in a real build).
config.clear();
note = L"MCP shim not found — reinstall or rebuild ShaderLab.";
}
if (!config.empty())
{
auto pkg = DP::DataPackage();
pkg.SetText(config);
DP::Clipboard::SetContent(pkg);
}
PipelineFormatText().Text(note);
});
}
MainWindow::~MainWindow()
{
m_isShuttingDown = true;
m_renderShouldStop.store(true, std::memory_order_release);
// Stop the MCP session FIRST, while the render worker is still alive,
// so an in-flight session request drains rather than stranding on a
// joined worker (the 30 s stall the migration plan warns about).
// Then (below) the render dispatcher + worker.
StopMcpSession();
if (m_renderTimer)
{
m_renderTimer.Stop();
m_renderTimer = nullptr;
}
// Two-phase shutdown of the render worker:
m_renderDispatcher.Shutdown();
if (m_renderWorker.joinable())
{
m_renderWorker.request_stop();
m_renderWorker.join();
}
// Close all output windows.
m_outputWindows.clear();
// Release UI-side offscreen wrappers before tearing down render engine.
for (auto& b : m_offscreenSourceBitmapUi) b = nullptr;
// The worker is joined, so this thread may release the context.
m_context = nullptr;
m_displayMonitor.Shutdown();
m_renderEngine.Shutdown();
::ShaderLab::Engine::ShutdownEngineServices();
}
HWND MainWindow::GetWindowHandle()
{
HWND hwnd{ nullptr };
auto windowNative = this->try_as<::IWindowNative>();
if (windowNative)
{
windowNative->get_WindowHandle(&hwnd);
}
return hwnd;
}
void MainWindow::InitializeRendering()
{
// DisplayInformation::GetForWindow requires a running
// Windows.System.DispatcherQueue on this thread. WinUI 3 threads
// run Microsoft.UI.Dispatching.DispatcherQueue — a distinct type —
// so create the system one if absent (same pattern system-backdrop
// controllers use). It pumps via this thread's existing message
// loop; the controller must outlive the queue's consumers.
if (!m_systemDqController &&
!winrt::Windows::System::DispatcherQueue::GetForCurrentThread())
{
DispatcherQueueOptions options{
sizeof(DispatcherQueueOptions),
DQTYPE_THREAD_CURRENT,
DQTAT_COM_NONE };
ABI::Windows::System::IDispatcherQueueController* controller{ nullptr };
if (SUCCEEDED(::CreateDispatcherQueueController(options, &controller)))
m_systemDqController.attach(
reinterpret_cast<::IUnknown*>(controller));
}
m_displayMonitor.Initialize(m_hwnd);
// The device, then the context on it (created once, moved on an
// adapter switch), then the swap chain on the context's D2D device.
m_renderEngine.CreateDevice(m_devicePref);
if (!m_context)
{
EFFECTGRAPH_CONTEXT_DESC desc{};
// Compile on the cache's worker threads, not the render thread,
// so a slow compile cannot stall UI actions that dispatch to it.
desc.flags = EFFECTGRAPH_CONTEXT_FLAGS_ASYNC_COMPILE;
const std::wstring cacheRoot = BytecodeCacheRoot();
desc.shaderCacheDirectory = cacheRoot.c_str();
winrt::check_hresult(EffectGraphCreateContext(m_renderEngine.D3DDevice(), &desc,
__uuidof(IEffectGraphContextInternal), m_context.put_void()));
}
else
{
winrt::check_hresult(m_context->MoveToDevice(m_renderEngine.D3DDevice()));
}
m_renderEngine.Initialize(m_hwnd, PreviewPanel(), ::ShaderLab::Rendering::FormatScRgbFP16,
m_context->Devices().d2dDevice.get(), m_context->DeviceContext());
// The GUI's evaluator follows the process defaults, which the
// performance flyout and the /perf MCP routes edit. Skip unneeded
// readbacks: every frame the RenderTick installs the selected node
// (and its binding sources) as the CPU-analysis interest set, so its
// Properties panel stays live while other compute nodes skip the Map
// when their consumers are served on the GPU. The live loop never
// needs values the frame they are produced, so it collects them
// asynchronously instead of draining the GPU pipeline per compute
// node. MCP /analysis/{id} reads ask for a fresh blocking readback
// per request (GraphEvaluator::RequestFreshCpuReadback).
::ShaderLab::Performance::SetSkipUnneededCpuReadbackEnabled(true);
::ShaderLab::Performance::SetAsyncAnalysisReadbackEnabled(true);
// Per-node GPU attribution. The evaluator brackets each compute
// dispatch when this is set AND timing is enabled; no-op otherwise.
Evaluator().SetGpuTimer(&m_renderEngine.Timer());
// Option variants report on a status line rather than the compile dialog.
m_compileProgress.SetVariantStatusText(VariantCompileText());
// Reflect that default in the flyout, so the checkbox is not
// showing the opposite of what the engine is doing.
if (AnalysisRefreshCombo()) AnalysisRefreshCombo().SelectedIndex(0);
if (GpuTimingCheck()) GpuTimingCheck().IsChecked(false);
if (ForceRedrawCheck()) ForceRedrawCheck().IsChecked(false);
if (GpuBindingsCheck()) GpuBindingsCheck().IsChecked(::ShaderLab::Performance::IsGpuBindingsEnabled());
if (NodeGpuStatsCheck()) NodeGpuStatsCheck().IsChecked(false);
if (UnthrottledCheck()) UnthrottledCheck().IsChecked(::ShaderLab::Rendering::HostRenderSettings::IsUnthrottledRenderEnabled());
// Subscribe to display changes (AdvancedColorInfoChanged: HDR
// toggle, SDR-brightness slider, monitor move, profile sim).
// NO graph work here: the render worker's per-tick
// UpdateWorkingSpaceNodes reads ActiveProfile() and dirties the
// Working Space node when a field really moved — that dirty is
// the designed propagation to binding consumers, and nothing
// else in the graph depends on display state ("bind, don't
// hide"). Displays with adaptive color fire this event at
// sensor rate (several Hz, sub-nit deltas), so the UI refresh
// is coalesced behind a pending flag — an event storm results
// in at most one queued refresh at a time. Dirtying the graph here
// would turn that storm into continuous full re-evaluation.
m_displayMonitor.SetCallback([this](const ::ShaderLab::Rendering::DisplayCapabilities& /*newCaps*/)
{
if (m_displayUiRefreshPending.exchange(true))
return;
this->DispatcherQueue().TryEnqueue([this]()
{
m_displayUiRefreshPending = false;
// Pick up new refresh rate (e.g. user changed displays
// or switched modes from 60 Hz to 144 Hz).
UpdateRenderTimerInterval();
UpdateStatusBar();
});
});
}
// The on-disk bytecode cache, under %LOCALAPPDATA%; empty when that
// folder cannot be found, which leaves the cache in memory only.
std::wstring MainWindow::BytecodeCacheRoot()
{
wchar_t* localAppData = nullptr;
std::wstring root;
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &localAppData)) && localAppData)
root = std::wstring(localAppData) + L"\\ShaderLab\\bytecode";
::CoTaskMemFree(localAppData);
return root;
}
void MainWindow::OnPreviewPanelLoaded()
{
if (!m_hwnd || m_renderEngine.IsInitialized()) return;
InitializeRendering();
// Register the MCP routes; the session starts now unless auto-start is off.
SetupMcpRoutes();
if (m_autoStartMcp && m_mcpServer)
{
StartMcpSession();
McpServerToggle().IsChecked(true);
DispatcherQueue().TryEnqueue([this]()
{
UpdateMcpStatusLabel();
McpExportConfigButton().Visibility(winrt::Microsoft::UI::Xaml::Visibility::Visible);
m_mcpIndicator.Reset();
UpdateMcpActivityIndicator();
});
}
UpdateStatusBar();
// Refresh title bar now that the effect library is loaded so the
// title shows both the app and effect-library version.
RefreshTitleBar();
// UI-thread reads in the controller go through the per-frame snapshot
// the render worker publishes, never the live graph. See the THREADING
// RULE block in NodeGraphController.h. The worker is not running yet,
// so this thread lays out.
m_nodeGraphController.Attach(Model(), m_renderDispatcher, [this] { return CurrentGraphSnapshot(); });
m_nodeGraphController.RebuildLayout();
m_nodeGraphController.SetConnectionCallback(
[this](uint32_t srcId, uint32_t srcPin, uint32_t dstId, uint32_t dstPin, bool isData) {
const auto snapshot = CurrentGraphSnapshot();
auto* srcNode = snapshot ? snapshot->FindNode(srcId) : nullptr;
auto* dstNode = snapshot ? snapshot->FindNode(dstId) : nullptr;
std::wstring srcName = srcNode ? srcNode->name : std::format(L"Node {}", srcId);
std::wstring dstName = dstNode ? dstNode->name : std::format(L"Node {}", dstId);
if (isData)
{
m_nodeLogs[dstId].Info(std::format(L"Property bound from {} (data pin)", srcName));
m_nodeLogs[srcId].Info(std::format(L"Data output bound to {}", dstName));
}
else
{
m_nodeLogs[dstId].Info(std::format(L"Input {} connected from {}", dstPin, srcName));
m_nodeLogs[srcId].Info(std::format(L"Output {} connected to {}", srcPin, dstName));
}
MarkUnsaved();
});
// Start render loop. The interval tracks the monitor's refresh
// rate (clamped to 60..240 Hz), so 120 Hz / 144 Hz / 240 Hz panels
// and high-FPS video sources actually run at their native cadence.
m_fpsTimePoint = std::chrono::steady_clock::now();
m_lastRenderTick = m_fpsTimePoint;
m_renderTimer = DispatcherQueue().CreateTimer();
m_renderTimer.Tick({ this, &MainWindow::OnRenderTick });
UpdateRenderTimerInterval();
m_renderTimer.Start();
// Spawn the render-worker thread. Owns
// the engine D2D context and runs the graph + GPU loop independently
// of the UI thread, drawing into a double-buffered offscreen target.
// UI thread blits the latest published buffer into the SwapChainPanel-
// bound swap chain in OnRenderTick.
//
// Publish a snapshot before the worker exists so UI reads never see
// nullptr once it runs, and republish after every dispatched closure
// so a UI read that follows a dispatched write sees it.
PublishGraphSnapshot();
m_renderDispatcher.SetAfterSyncClosure([this] { PublishGraphSnapshot(); });
StartRenderWorker();
// Initialize the node graph editor panel.
InitializeGraphPanel();
// Sweep %TEMP% for ShaderLab-* media dirs left behind by a
// crashed prior instance and offer to delete them. Runs after
// panel init so the dialog has a XamlRoot to attach to.
ReapStaleMediaDirsAsync();
// If the app was launched via file double-click, load that
// graph now that rendering is wired up.
if (!m_pendingOpenPath.empty())
{
auto path = std::move(m_pendingOpenPath);
m_pendingOpenPath.clear();
LoadGraphFromPathAsync(winrt::hstring(path));
}
else
{
RefreshTitleBar();
}
}
void MainWindow::UpdateStatusBar()
{
auto caps = m_displayMonitor.CachedCapabilities();
if (m_displayMonitor.IsSimulated())
{
auto profile = m_displayMonitor.ActiveProfile();
DisplayModeText().Text(L"Display: Simulated \u2014 " + winrt::hstring(profile.profileName));
}
else
{
DisplayModeText().Text(L"Display: " + caps.ModeString());
}
DisplayLuminanceText().Text(L"Max Luminance: " + caps.LuminanceString());
PipelineFormatText().Text(L"Pipeline: " + m_renderEngine.ActiveFormat().name);
// GPU info.
if (m_renderEngine.IsInitialized())
{
if (m_renderEngine.IsWarp())
GpuInfoText().Text(L"GPU: Software (WARP)");
else
GpuInfoText().Text(L"GPU: " + m_renderEngine.AdapterName());
}
}
void MainWindow::UpdateMcpActivityIndicator()
{
UpdateMcpStatusLabel();
m_mcpIndicator.Update(m_sessionClient != nullptr, McpActivityDot(), McpActivityDotBrush(), McpServerToggle());
}
uint32_t MainWindow::QueryDisplayRefreshHz() const noexcept
{
// Use EnumDisplaySettings on the monitor that contains the app HWND.
// dmDisplayFrequency is reported in whole Hz; modern panels round to
// 60/120/144/165/240 etc. Returning 0 here is the "unknown" signal.
if (!m_hwnd)
return 0;
HMONITOR mon = MonitorFromWindow(m_hwnd, MONITOR_DEFAULTTOPRIMARY);
if (!mon)
return 0;
MONITORINFOEXW mi{};
mi.cbSize = sizeof(mi);
if (!::GetMonitorInfoW(mon, &mi))
return 0;
DEVMODEW dm{};
dm.dmSize = sizeof(dm);
if (!::EnumDisplaySettingsW(mi.szDevice, ENUM_CURRENT_SETTINGS, &dm))
return 0;
return dm.dmDisplayFrequency;
}
void MainWindow::UpdateRenderTimerInterval()
{
// Pick a target Hz from the active monitor, clamped to [60, 240].
// - 60 Hz floor: avoid laggy interactions on 30 Hz TV-out modes.
// - 240 Hz ceiling: dispatcher tick scheduling becomes noisy below
// ~4 ms, and rendering faster than panel refresh is wasted work.
// The render worker reads the same value for its FramePacer.
const uint32_t hz = ::ShaderLab::Rendering::FramePacer::ClampRate(QueryDisplayRefreshHz());
if (hz == m_targetRefreshHz.load(std::memory_order_relaxed) && m_renderTimer)
return;
m_targetRefreshHz.store(hz, std::memory_order_relaxed);
// Use microseconds so non-integer-ms periods (e.g. 144 Hz ~6.944 ms)
// don't get rounded to a slower or faster cadence than the panel.
const auto interval = std::chrono::microseconds(1'000'000 / hz);
if (m_renderTimer)
m_renderTimer.Interval(interval);
}
void MainWindow::OnGpuInfoTapped(
winrt::Windows::Foundation::IInspectable const& sender,
winrt::Microsoft::UI::Xaml::Input::TappedRoutedEventArgs const& /*args*/)
{
namespace Controls = winrt::Microsoft::UI::Xaml::Controls;
using namespace ::ShaderLab::Rendering;
auto adapters = RenderEngine::EnumerateAdapters();
if (adapters.empty()) return;
auto flyout = Controls::MenuFlyout();
for (size_t i = 0; i < adapters.size(); ++i)
{
auto& a = adapters[i];
auto item = Controls::MenuFlyoutItem();
std::wstring label = a.name;
if (a.dedicatedVideoMemoryMB > 0)
label += std::format(L" ({} MB)", a.dedicatedVideoMemoryMB);
if (a.isWarp)
label += L" [Software]";
// Mark current adapter.
if (a.name == m_renderEngine.AdapterName() ||
(a.isWarp && m_renderEngine.IsWarp()))
label = L"\u2713 " + label;
item.Text(winrt::hstring(label));
auto luid = a.luid;
bool isWarp = a.isWarp;
item.Click([this, luid, isWarp](auto&&, auto&&) {
SwitchAdapter(
isWarp ? DevicePreference::Warp : DevicePreference::Adapter,
luid);
});
flyout.Items().Append(item);
}
flyout.ShowAt(sender.as<winrt::Microsoft::UI::Xaml::FrameworkElement>());
}
void MainWindow::StartRenderWorker()
{
m_renderShouldStop.store(false, std::memory_order_release);
m_renderWorker = std::jthread([this](std::stop_token stop) { RenderWorkerLoop(stop); });
}
void MainWindow::StopRenderWorker()
{
m_renderShouldStop.store(true, std::memory_order_release);
m_renderDispatcher.Wake();
if (m_renderWorker.joinable())
{
m_renderWorker.request_stop();
m_renderWorker.join();
}
// No consumer now: this thread owns the graph until StartRenderWorker.
m_renderDispatcher.ResetConsumer();
}
// Drops the window's resources bound to the current device; the context
// drops its own when it moves. The worker must be stopped.
void MainWindow::TearDownDeviceResources()
{
m_nodeGraphController.ReleaseDeviceResources();
if (auto panelNative = NodeGraphPanel().try_as<ISwapChainPanelNative>())
panelNative->SetSwapChain(nullptr);
m_graphRenderTarget = nullptr;
m_graphSwapChain = nullptr;
m_graphGridBrush = nullptr;
for (auto& b : m_offscreenSourceBitmapUi) b = nullptr;
m_offscreenWrapperWidth = 0;
m_offscreenWrapperHeight = 0;
m_offscreenPublishedIdx.store(-1, std::memory_order_release);
ReleaseUiD2dContext();
for (auto& w : m_outputWindows) w->Close();
m_outputWindows.clear();
{
std::scoped_lock lock(m_outputSinksMutex);
m_outputSinks.clear();
}
for (auto& w : m_logWindows) w->Close();
m_logWindows.clear();
m_displayMonitor.SetCallback(nullptr);
m_displayMonitor.Shutdown();
m_renderEngine.Shutdown();
}
// Brings the device stack up on the preferred adapter, or on the default
// one when that fails. Returns the error when neither comes up.
std::optional<std::wstring> MainWindow::RebuildDeviceResources(
::ShaderLab::Rendering::DevicePreference pref, LUID adapterLuid)
{
m_devicePref = pref;
if (pref == ::ShaderLab::Rendering::DevicePreference::Adapter)
m_renderEngine.SetPreferredAdapterLuid(adapterLuid);
auto tryInitialize = [this]() -> std::optional<std::wstring>
{
try
{
InitializeRendering();
return std::nullopt;
}
catch (const winrt::hresult_error& error)
{
return std::wstring(error.message());
}
catch (const std::exception& error)
{
return ::ShaderLab::Utf8ToWide(error.what());
}
};
if (auto error = tryInitialize())
{
OutputDebugStringW((L"[GPU Switch] " + *error + L"; falling back to the default adapter\n").c_str());
// Whatever the failed attempt created goes before the retry.
m_displayMonitor.Shutdown();
m_renderEngine.Shutdown();
m_devicePref = ::ShaderLab::Rendering::DevicePreference::Default;
if (auto fallbackError = tryInitialize())
{
m_displayMonitor.Shutdown();
m_renderEngine.Shutdown();
return fallbackError;
}
}
InitializeGraphPanel();
return std::nullopt;
}
// Re-prepares the sources on the new device, on the render thread with its
// own context. Videos are reopened afterwards, once the device has settled.
void MainWindow::ReloadSourcesAfterSwitch()
{
const auto videoNodeIds = m_renderDispatcher.DispatchSync([this]
{
std::vector<uint32_t> videoIds;
auto* dc = m_renderEngine.RenderD2DContext();
if (!dc) return videoIds;
for (auto& node : Model().MutableNodes())
{
if (node.type != ::ShaderLab::Graph::NodeType::Source) continue;
::ShaderLab::Graph::MarkNodeChanged(node);
if (::ShaderLab::Graph::ClassifySource(node) == ::ShaderLab::Graph::SourceKind::Video)
{
videoIds.push_back(node.id);
continue;
}
Sources().PrepareSourceNode(node, dc, m_renderEngine.D3DDevice(), m_renderEngine.D3DContext());
}
return videoIds;
});
for (uint32_t videoNodeId : videoNodeIds)
{
DispatcherQueue().TryEnqueue([this, videoNodeId]
{
m_renderDispatcher.DispatchSync([this, videoNodeId]
{
auto* videoNode = Model().FindNode(videoNodeId);
auto* dc = m_renderEngine.RenderD2DContext();
if (!videoNode || !dc) return;
Sources().PrepareSourceNode(*videoNode, dc, m_renderEngine.D3DDevice(), m_renderEngine.D3DContext());
});
});
}
// After the first frames: sources that failed on the new device retry.
DispatcherQueue().TryEnqueue(winrt::Microsoft::UI::Dispatching::DispatcherQueuePriority::Low, [this]
{
m_renderDispatcher.DispatchSync([this]
{
for (auto& node : Model().MutableNodes())
{
if (node.type == ::ShaderLab::Graph::NodeType::Source && !node.runtimeError.empty())
::ShaderLab::Graph::RetryNode(Model(), node.id);
}
});
m_forceRender = true;
});
}
void MainWindow::ReopenOutputWindows()
{
const auto snapshot = CurrentGraphSnapshot();
if (!snapshot) return;
for (const auto& node : snapshot->nodes)
{
if (node.type != ::ShaderLab::Graph::NodeType::Output) continue;
try
{
OpenOutputWindow(node.id);
}
catch (const winrt::hresult_error& error)
{
// The node stays without a window, as after the user closes one.
GetNodeLog(node.id).Error(L"Its output window could not be opened: " + std::wstring(error.message()));
}
catch (const std::exception& error)
{
GetNodeLog(node.id).Error(L"Its output window could not be opened: " + ::ShaderLab::Utf8ToWide(error.what()));
}
}
}
void MainWindow::SwitchAdapter(::ShaderLab::Rendering::DevicePreference pref, LUID adapterLuid)
{
// MCP requests answer 503 until the switch is over
// (GuiEngineCommandSink::Dispatch).
m_adapterSwitchInProgress.store(true, std::memory_order_release);
if (m_renderTimer) m_renderTimer.Stop();
StopRenderWorker();
TearDownDeviceResources();
if (const auto error = RebuildDeviceResources(pref, adapterLuid))
{
// No device: the graph is kept in the context, nothing renders,
// and the UI tick tries again in a few seconds.
PipelineFormatText().Text(L"Error: no GPU device (" + *error + L"); retrying");
m_deviceUnavailable.store(true, std::memory_order_release);
m_nextDeviceRetry = std::chrono::steady_clock::now() + std::chrono::seconds(5);
if (m_renderTimer) m_renderTimer.Start();
m_adapterSwitchInProgress.store(false, std::memory_order_release);
return;
}
m_deviceUnavailable.store(false, std::memory_order_release);
m_deviceLost.store(S_OK, std::memory_order_release);
// The graph, view and selection are as they were; the context left
// every node dirty.
PublishGraphSnapshot();
m_forceRender = true;
UpdateStatusBar();
StartRenderWorker();
ReloadSourcesAfterSwitch();
ReopenOutputWindows();
if (m_renderTimer) m_renderTimer.Start();
m_adapterSwitchInProgress.store(false, std::memory_order_release);
}
void MainWindow::RecoverDevice()
{
const HRESULT reason = m_deviceLost.exchange(S_OK, std::memory_order_acq_rel);
if (FAILED(reason))
OutputDebugStringW(std::format(L"[GPU] Device removed (0x{:08X}); recovering\n",
static_cast<uint32_t>(reason)).c_str());
SwitchAdapter(m_devicePref, m_renderEngine.PreferredAdapterLuid());
if (!m_deviceUnavailable.load(std::memory_order_acquire))
{
PipelineFormatText().Text(FAILED(reason)
? winrt::hstring(std::format(L"Recovered from a lost GPU device (0x{:08X})", static_cast<uint32_t>(reason)))
: winrt::hstring(L"GPU device available again"));
}
}
// -----------------------------------------------------------------------
// Drag and drop — create source nodes from dropped files
// -----------------------------------------------------------------------
void MainWindow::OnNodeGraphDragOver(
winrt::Windows::Foundation::IInspectable const& /*sender*/,
winrt::Microsoft::UI::Xaml::DragEventArgs const& args)
{
if (args.DataView().Contains(winrt::Windows::ApplicationModel::DataTransfer::StandardDataFormats::StorageItems()))
args.AcceptedOperation(winrt::Windows::ApplicationModel::DataTransfer::DataPackageOperation::Copy);
}
winrt::fire_and_forget MainWindow::OnNodeGraphDrop(
winrt::Windows::Foundation::IInspectable const& /*sender*/,
winrt::Microsoft::UI::Xaml::DragEventArgs const& args)
{
auto strong = get_strong();
if (!args.DataView().Contains(winrt::Windows::ApplicationModel::DataTransfer::StandardDataFormats::StorageItems()))
co_return;
auto items = co_await args.DataView().GetStorageItemsAsync();
// The nodes are added and prepared on the render thread: the graph
// is the worker's, and the bitmaps belong to its D2D context.
std::vector<std::wstring> paths;
for (const auto& item : items)
{
if (auto file = item.try_as<winrt::Windows::Storage::StorageFile>())
paths.emplace_back(std::wstring(file.Path()));
}
if (paths.empty()) co_return;
try {
m_renderDispatcher.DispatchSync([this, &paths] {
auto* dc = m_renderEngine.RenderD2DContext();
for (const auto& path : paths)
{
bool isVideo = ::ShaderLab::Effects::SourceNodeFactory::IsVideoFile(path);
::ShaderLab::Graph::EffectNode node = isVideo
? ::ShaderLab::Effects::SourceNodeFactory::CreateVideoSourceNode(path)
: ::ShaderLab::Effects::SourceNodeFactory::CreateImageSourceNode(path);
auto id = Model().AddNode(std::move(node));
m_nodeLogs[id].Info(std::format(L"Dropped: {}",
std::filesystem::path(path).filename().wstring()));
auto* addedNode = Model().FindNode(id);
if (addedNode && dc)
Sources().PrepareSourceNode(*addedNode, dc, m_renderEngine.D3DDevice(), m_renderEngine.D3DContext());
}
m_nodeGraphController.AutoLayout();
return 0;
});
}
catch (const winrt::hresult_error& error)
{
PipelineFormatText().Text(L"Error: Failed to add the dropped files: " + error.message());
}
catch (const std::exception& error) // DispatchSync timed out or is shutting down
{
PipelineFormatText().Text(L"Error: Failed to add the dropped files: " + ::ShaderLab::Utf8ToWide(error.what()));
}
PopulatePreviewNodeSelector();
m_forceRender = true;
}
void MainWindow::OnPreviewSizeChanged(
winrt::Windows::Foundation::IInspectable const& /*sender*/,
winrt::Microsoft::UI::Xaml::SizeChangedEventArgs const& args)