Repository navigation
Expand file tree
/
Copy pathV3dModelManager.cpp
More file actions
executable file
·2192 lines (1846 loc) · 91.2 KB
/
Copy pathV3dModelManager.cpp
File metadata and controls
executable file
·2192 lines (1846 loc) · 91.2 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 "V3dModelManager.h"
#include <QApplication>
#include <QColorSpace>
#include <QScrollBar>
#include <QPainter>
#include <QStandardPaths>
#include <QWindow>
#include <QScreen>
#include <cstdlib>
#include <fstream>
#include <mutex>
#include <set>
#include <QTimer>
#include <QThread>
#include <generator.h>
#include <gui/priorities.h>
#include <utils.h>
#include "Utility/EventFilter.h"
#include "Utility/ApplicationEventFilter.h"
#include "Utility/ProtectedFunctionCaller.h"
#include "seconds.h"
#include "rgba.h"
#include "bezierpatch.h"
namespace camp {
glm::dmat4 projViewMat{ 1.0 };
glm::dmat3 normMat{ 1.0 };
const glm::dmat4& getProjViewMat()
{
// static glm::dmat4 dummy;
return projViewMat;
}
const glm::dmat3& getNormMat() { return normMat; }
}
namespace {
// Every v3d render -- from any document, on any thread -- clears and
// fills the same process-global Asymptote scene state: the camp::
// vertex buffers (materialData/colorData/lineData/transparentData/
// pointData, defined in the vendored Asymptote sources), camp::materials,
// camp::pixelsPerBp, camp::projViewMat, camp::normMat, and the
// static lastPage/lastModel locals in RenderModel. The per-manager
// m_ModelsMutex only serializes renders of ONE document; with two
// v3d-bearing documents open (two PixmapGenerationThreads), or a
// standalone .v3d on the main thread plus a PDF on a worker, two
// RenderModel calls run concurrently under different managers' mutexes
// and tear the shared buffers (clear() while another thread
// push_back's -> realloc UAF, the "multiple documents crash").
//
// This single process-wide mutex serializes the global-buffer section
// of RenderModel (buffer clear + QueueMesh + HeadlessRenderer::render,
// which reads the same buffers) across all documents.
std::mutex v3dRenderGlobalsMutex;
}
bool fileExists(const std::string& path) {
std::ifstream f{ path.c_str() };
return f.good();
}
// Old v3d files may store an absolute path in the IBL image name; reduce it
// to the file name (relative paths, e.g. "snowyField/refl0", are preserved)
// and drop trailing slashes, so the name is a relative path within the image
// library.
static std::string normalizeIBLName(const std::string& imageName)
{
std::string name = imageName;
if (!name.empty() && name[0] == '/') {
size_t lastSlash = name.find_last_of('/');
name = (lastSlash != 0) ? name.substr(lastSlash + 1) : name.substr(1);
}
while (!name.empty() && name.back() == '/') name.pop_back();
return name;
}
// IBL image names come from untrusted v3d documents and must resolve within
// the image library directory -- locally and on the server. Reject '%'
// outright (ambiguous in URLs), a leading separator, '..' path components
// (either separator), and control characters (a NUL in particular would let a
// "..\0" bypass the ".." check below and be truncated to ".." at the OS
// layer); relative subdirectories are otherwise allowed. Keep in sync with
// settings::iblNameOK in the asymptote repo.
static bool iblNameOK(const std::string& name)
{
if (name.find('%') != std::string::npos)
return false;
// Control characters are never valid in a file path (and a NUL would let a
// "..\0" slip past the ".." check and truncate to ".." via the OS).
for (unsigned char c : name) {
if (c < 0x20 || c == 0x7f)
return false;
}
// Must be a relative path within the image library: no leading separator.
if (!name.empty() && (name[0] == '/' || name[0] == '\\'))
return false;
size_t start = 0;
while (true) {
size_t end = name.find_first_of("/\\", start);
size_t len = (end == std::string::npos) ? std::string::npos : end - start;
if (name.substr(start, len) == "..")
return false;
if (end == std::string::npos)
break;
start = end + 1;
}
return true;
}
// Log a rejected IBL name safely, for debugging. The name is untrusted document
// data, so non-printable bytes are escaped as \xHH -- the log must never emit
// raw control characters or terminal escape sequences taken from the file. It
// is gated behind the debug flag (LOG) so a hostile document cannot spam the
// console on the per-render path.
static void logRejectedIBLName(const std::string& name)
{
static const char hex[] = "0123456789abcdef";
std::string safe;
safe.reserve(name.size());
for (unsigned char c : name) {
if (c >= 0x20 && c <= 0x7e) {
safe += static_cast<char>(c);
} else {
safe += '\\';
safe += 'x';
safe += hex[(c >> 4) & 0xf];
safe += hex[c & 0xf];
}
}
LOG("[v3d] IBL image name rejected (not a safe image-library path): %s\n", safe.c_str());
}
// Custom event for marshaling IBL download prompts from background threads to main thread.
class IBLPromptEvent : public QEvent {
public:
using Callback = std::function<bool()>;
explicit IBLPromptEvent(Callback&& cb)
: QEvent(QEvent::Type(QEvent::User + 1)), m_Callback(std::move(cb)) {}
Callback m_Callback;
};
// Custom event requesting a PageView re-resolution on the main thread
// (posted by cacheRequestUnlocked() from any thread, handled by
// ApplicationEventFilter).
class PageViewRebindEvent : public QEvent {
public:
explicit PageViewRebindEvent()
: QEvent(QEvent::Type(QEvent::User + 2)) {}
};
// Event filter installed on qApp to catch IBLPromptEvent and run the callback on the main thread.
//
// Lifetime anchor for the IBL prompt: this filter is a child of the manager's
// m_FilterParent and is removed from qApp + destroyed in ~V3dModelManager, so it
// can never outlive the manager it points at. The callback captured in the event
// therefore only ever runs on a live manager; the m_Manager check below makes that
// guarantee explicit and keeps it safe even if the destructor's filter removal is
// ever reordered.
class IBLEventFilter : public QObject {
public:
// NO Q_OBJECT macro -- we only need eventFilter, no signals/slots.
explicit IBLEventFilter(QObject* parent, V3dModelManager* manager)
: QObject(parent), m_Manager(manager) {}
bool eventFilter(QObject* /*object*/, QEvent* event) override {
if (event->type() == QEvent::Type(QEvent::User + 1)) {
IBLPromptEvent* promptEvent = static_cast<IBLPromptEvent*>(event);
if (m_Manager != nullptr && !m_Manager->isDestroying()) {
promptEvent->m_Callback();
}
// Do NOT delete event here -- Qt owns it and will delete it after
// the filter chain returns. Deleting it ourselves causes use-after-free
// when sendPostedEvents tries to clean up its smart pointer to the event.
return true;
}
return false;
}
private:
V3dModelManager* m_Manager;
};
V3dModelManager::V3dModelManager(const Okular::Document* document)
: m_Document(document)
, m_StartTime(std::chrono::system_clock::now()) {
// The HeadlessRenderer (shader probing + glslang + Vulkan
// instance/device) is created lazily in ensureHeadlessRenderer() on
// the first AddModel, so a document without 3D content never pays for
// it at open time.
// Initial binding: with several windows open this binds to the
// PageView of the window whose part opened this document's url. If
// this document's own window's PageView does not exist yet, it binds
// to the other window's view; rebindPageView() re-resolves on this
// document's first pixmap request, which arrives before the user can
// interact with the document.
m_PageView = GetPageViewWidget();
// Parent filters to our own QObject so we fully control their lifetime.
// Do NOT parent them to m_PageView or qApp -- those parents would delete
// the filters independently during Qt teardown, causing double-frees.
if (m_PageView) {
m_EventFilter = new EventFilter(&m_FilterParent, this);
m_PageView->viewport()->installEventFilter(m_EventFilter);
}
m_ApplicationEventFilter = new ApplicationEventFilter(&m_FilterParent, this);
qApp->installEventFilter(m_ApplicationEventFilter);
// Install IBL event filter on qApp to handle cross-thread IBL download prompts.
// The filter holds a back-pointer to the manager; it is a child of
// m_FilterParent and destroyed with the manager, so that back-pointer is
// always valid for the filter's lifetime.
m_IBLEventFilter = new IBLEventFilter(&m_FilterParent, this);
qApp->installEventFilter(m_IBLEventFilter);
}
V3dModelManager::~V3dModelManager() {
// Set before anything below can be observed: the deferred-refresh timers and
// the IBL prompt event read this to refuse to touch the manager once it is gone.
m_Destroying = true;
// The deferred-refresh QTimers are children of m_FilterParent, so they are
// destroyed together with the manager (member destruction, below) and can never
// fire on freed state; they also delete themselves on fire, so nothing leaks.
// Null out back-references first so in-flight events are safe.
if (m_EventFilter) {
m_EventFilter->modelManager = nullptr;
}
if (m_PresentationEventFilter) {
m_PresentationEventFilter->modelManager = nullptr;
}
if (m_ApplicationEventFilter) {
m_ApplicationEventFilter->modelManager = nullptr;
}
// Unregister from Qt event chains before the filters are destroyed.
QCoreApplication* app = QCoreApplication::instance();
if (app && m_ApplicationEventFilter) {
app->removeEventFilter(m_ApplicationEventFilter);
}
if (app && m_IBLEventFilter) {
app->removeEventFilter(m_IBLEventFilter);
}
if (m_PageView && m_EventFilter) {
m_PageView->viewport()->removeEventFilter(m_EventFilter);
}
if (m_PresentationWidget && m_PresentationEventFilter) {
m_PresentationWidget->removeEventFilter(m_PresentationEventFilter);
}
// m_FilterParent destroys both filters as its children.
}
void V3dModelManager::ensureHeadlessRenderer() {
if (m_HeadlessRenderer || m_HeadlessRendererMissing) {
return;
}
const std::vector<std::string> shaderSearchPaths {
"./",
"/usr/lib64/qt6/plugins/okular_generators/",
"/usr/lib/x86_64-linux-gnu/qt6/plugins/okular/generators/",
"C:\\Program Files\\Okular\\bin\\"
};
// Every shader file HeadlessRenderer compiles (createShaderModules).
// Require all of them up front so a partial shader installation degrades
// to a blank model at first render, instead of only failing mid-render.
const std::vector<std::string> requiredShaders {
"vertex.glsl", "fragment.glsl", "count.glsl", "screen.glsl",
"blend.glsl", "sum1.glsl", "sum2.glsl", "sum3.glsl"
};
std::string shaderPath = "";
for (const auto& path : shaderSearchPaths) {
bool complete = true;
for (const auto& shader : requiredShaders) {
if (!fileExists(path + shader)) {
complete = false;
break;
}
}
if (complete) {
shaderPath = path;
break;
}
}
if (shaderPath == "") {
std::cout << "Shaders could not be found, disabling v3d rendering." << std::endl;
// Do NOT call std::exit() -- this runs for every 3D document opened.
m_HeadlessRendererMissing = true;
} else {
m_HeadlessRenderer = std::make_unique<HeadlessRenderer>(shaderPath);
}
}
void V3dModelManager::AddModel(V3dModel model, size_t pageNumber) {
QMutexLocker locker(&m_ModelsMutex);
// AddModel is the first call for a document that actually contains 3D
// models: pay for the renderer here (main thread, before any render)
// instead of in the constructor, where every opened document paid.
ensureHeadlessRenderer();
if (m_Models.size() < pageNumber + 1) {
m_Models.resize(pageNumber + 1);
}
m_Models[pageNumber].emplace_back(std::move(model));
m_Models[pageNumber].back().initProjection();
if (m_ModelImages.size() < pageNumber + 1) {
m_ModelImages.resize(pageNumber + 1);
}
m_ModelImages[pageNumber].push_back(QImage{ });
}
QImage V3dModelManager::RenderModel(size_t pageNumber, size_t modelIndex, int imageWidth, int imageHeight) {
QMutexLocker locker(&m_ModelsMutex);
// AddModel (which always precedes any render for a document) has already
// created the renderer; this is only reached if that invariant ever
// changes (the guard makes RenderModel self-sufficient either way).
ensureHeadlessRenderer();
// If the shaders were missing (or renderer init failed), rendering is disabled.
if (!m_HeadlessRenderer) {
LOG("[v3d] RenderModel: EXIT - no HeadlessRenderer (shaders missing)\n");
QImage image{ imageWidth, imageHeight, QImage::Format_ARGB32 };
image.fill(Qt::black);
return image;
}
// Guard against zero/negative dimensions (tiny models, small requests) which
// produce NaN projection matrices or invalid Vulkan extents.
if (imageWidth <= 0 || imageHeight <= 0) {
LOG("[v3d] RenderModel: EXIT - zero/negative dimensions\n");
QImage image{ std::max(imageWidth, 1), std::max(imageHeight, 1), QImage::Format_ARGB32 };
image.fill(Qt::black);
return image;
}
// Check IBL availability early. If the model needs IBL but the files
// aren't cached yet, render the no-IBL fallback so the scene is visible
// immediately (and shows what a "No" would look like while the download
// prompt is pending); the model is tracked and re-rendered with IBL once
// the files arrive.
std::string imageName = m_Models[pageNumber][modelIndex].file->headerInfo.imageName;
bool useIBL = false;
std::string iblPath = "";
if (!imageName.empty()) {
// A name that could escape the image library can never resolve and
// must not prompt or download: render the scene without IBL instead.
if (!iblNameOK(normalizeIBLName(imageName))) {
logRejectedIBLName(imageName);
} else {
iblPath = resolveIBLPath(imageName);
if (!iblPath.empty()) {
// Present locally (downloaded earlier or placed by hand): use
// it even if a download was declined before -- the decline
// refused the download, not a file that exists now.
useIBL = true;
} else if (m_IBLDeclinedNames.count(normalizeIBLName(imageName))) {
// Download declined for this image name: render the no-IBL
// fallback, keep the model untracked, and don't re-prompt.
// Other image names are unaffected.
} else {
// IBL missing locally -- track this model so it is re-rendered
// once the files arrive.
m_ModelsWithMissingIBL.insert({pageNumber, modelIndex});
// Marshal the download prompt to the main thread via custom event.
// Post-and-forget: we can't wait synchronously because Okular's
// background worker thread doesn't process events during PDF loading.
// The popup will appear once Okular returns to its main event loop.
// Debounced per image, so this fires at most once per image name.
std::string base = getIBLBase();
scheduleIBLPrompt(imageName, base);
}
}
}
if (!m_Models[pageNumber][modelIndex].m_HasChanged &&
m_ModelImages[pageNumber][modelIndex].width() == imageWidth &&
m_ModelImages[pageNumber][modelIndex].height() == imageHeight) {
return m_ModelImages[pageNumber][modelIndex];
}
// Projection
EnsureCachedRequestSize(pageNumber);
glm::vec2 canvasSize = {
(m_Models[pageNumber][modelIndex].maxBound.x - m_Models[pageNumber][modelIndex].minBound.x) * m_CachedRequestSizes[pageNumber].size.x,
(m_Models[pageNumber][modelIndex].maxBound.y - m_Models[pageNumber][modelIndex].minBound.y) * m_CachedRequestSizes[pageNumber].size.y,
};
// Guard against zero canvas size which causes division-by-zero in setProjection.
if (canvasSize.x <= 0.0f || canvasSize.y <= 0.0f) {
LOG("[v3d] RenderModel: EXIT - zero canvas size (cachedRequestSize=%dx%d)\n",
m_CachedRequestSizes.size() > pageNumber ? m_CachedRequestSizes[pageNumber].size.x : 0,
m_CachedRequestSizes.size() > pageNumber ? m_CachedRequestSizes[pageNumber].size.y : 0);
QImage image{ imageWidth, imageHeight, QImage::Format_ARGB32 };
image.fill(Qt::black);
return image;
}
m_Models[pageNumber][modelIndex].setProjection(canvasSize);
// Scene bounds for QueueMesh culling (use headerInfo when frustum params invalid)
float Zmin = m_Models[pageNumber][modelIndex].file->headerInfo.minBound.z;
float Zmax = m_Models[pageNumber][modelIndex].file->headerInfo.maxBound.z;
triple sceneMinBound(
m_Models[pageNumber][modelIndex].xmin,
m_Models[pageNumber][modelIndex].ymin,
Zmin);
triple sceneMaxBound(
m_Models[pageNumber][modelIndex].xmax,
m_Models[pageNumber][modelIndex].ymax,
Zmax);
if(sceneMinBound.getx() >= sceneMaxBound.getx() || sceneMinBound.gety() >= sceneMaxBound.gety() || sceneMinBound.getz() >= sceneMaxBound.getz()) {
sceneMinBound = m_Models[pageNumber][modelIndex].file->headerInfo.minBound;
sceneMaxBound = m_Models[pageNumber][modelIndex].file->headerInfo.maxBound;
}
using namespace std;
using namespace camp;
// Serialize the process-global Asymptote state across documents: from
// here (first write of a camp global) through QueueMesh (fills the
// buffers) and HeadlessRenderer::render (reads them) no other RenderModel
// may run. The per-manager mutex is already held; this is the
// cross-document one (see v3dRenderGlobalsMutex above). The guard
// releases on every return path, including the !hasRenderableData early
// out below.
std::lock_guard<std::mutex> renderGlobalsLock{ v3dRenderGlobalsMutex };
// Match vkrender.cc: projViewMat = projMat * viewMat
projViewMat = m_Models[pageNumber][modelIndex].projMat * m_Models[pageNumber][modelIndex].viewMat;
normMat = glm::dmat3{ glm::inverse(m_Models[pageNumber][modelIndex].viewMat) };
// Match Asymptote prepareScene(): when display is triggered (m_HasChanged=true),
// always clearData() + pic->render(). The per-object fast path (!remesh && onscreen)
// avoids re-tessellation -- algorithm \ref{cull}.
// When switching models (PDFs with multiple v3d images), force remesh so all
// objects rebuild their persistent state for the new scene.
static size_t lastPage = 0, lastModel = 0;
bool switchedModel = (pageNumber != lastPage || modelIndex != lastModel);
if (switchedModel) {
m_Models[pageNumber][modelIndex].remesh = true;
// Sync renderer draw mode to match the entering model, so the correct
// pipelines are used. Without this, model 1 could render with model 0's
// draw mode (e.g., WIREFRAME rendered as NORMAL).
if (m_HeadlessRenderer)
m_HeadlessRenderer->currentDrawMode = m_Models[pageNumber][modelIndex].drawMode;
lastPage = pageNumber;
lastModel = modelIndex;
}
// Match Asymptote updateHandler()/zoom()/fitscreen(): whenever the rendered
// resolution changes, force a full re-tessellation (remesh=true) so every figure
// re-queues with culling/resolution matching the new frustum. On initial load
// Okular requests pixmaps at several sizes (background preload, then on-screen
// display). Only the FIRST render of a model starts with remesh=true and it is
// consumed after one render (see below). Without this, a later render at a
// different size would take the per-object fast path (!remesh && onscreen) and
// reuse geometry that was culled/tessellated for the old frustum -- which can
// drop a transparent surface until the user forces a remesh (e.g. presses 'h').
// Forcing remesh on a size change mirrors the reference, where any resize/zoom
// sets remesh=true so all figures rebuild their persistent state.
const QImage& cachedImage = m_ModelImages[pageNumber][modelIndex];
if (cachedImage.width() != imageWidth || cachedImage.height() != imageHeight) {
m_Models[pageNumber][modelIndex].remesh = true;
}
// clearData() -- matches Asymptote prepareScene()
materialData.clear();
colorData.clear();
lineData.clear();
transparentData.clear();
pointData.clear();
bool orthographic = m_Models[pageNumber][modelIndex].file->headerInfo.orthographic;
// pic->render(remesh) -- matches Asymptote prepareScene() exactly. Do NOT centrally
// reset renderCount here: the reference resets it via notRendered() inside each
// object's queue()/clear() during QueueMesh, and the per-object fast path
// (!remesh && Onscreen) intentionally leaves renderCount untouched so stable
// geometry is not re-uploaded. Forcing remesh=true on a page/image switch (above)
// makes every object re-queue -> notRendered() -> full re-upload, matching the
// single-scene reference where remesh drives the same behavior.
m_Models[pageNumber][modelIndex].file->QueueMesh(imageWidth, imageHeight, sceneMinBound, sceneMaxBound, m_Models[pageNumber][modelIndex].remesh, orthographic, m_Models[pageNumber][modelIndex].drawMode);
// Check if any VertexBuffer has data to render (vkrender.cc: drawBuffer skips empty).
bool hasRenderableData = !materialData.indices.empty() || !colorData.indices.empty() || !lineData.indices.empty() || !transparentData.indices.empty() || !pointData.indices.empty();
if (!hasRenderableData) {
LOG("[v3d] RenderModel: EXIT - no renderable data after QueueMesh (culled?)\n");
// Match the reference (vkrender.cc): the frame is still cleared every
// render, so an all-culled scene (e.g. translated fully offscreen) shows
// the v3d background color, not black.
const auto& bg = m_Models[pageNumber][modelIndex].file->headerInfo.background;
QImage image{ imageWidth, imageHeight, QImage::Format_RGB32 };
image.fill(QColor(qBound(0, int(bg.r * 255.0f + 0.5f), 255),
qBound(0, int(bg.g * 255.0f + 0.5f), 255),
qBound(0, int(bg.b * 255.0f + 0.5f), 255)));
image.setColorSpace(QColorSpace());
return image;
}
// Match Asymptote prepareScene(): remesh is consumed after one non-OUTLINE render.
if (m_Models[pageNumber][modelIndex].drawMode != DRAWMODE_OUTLINE)
m_Models[pageNumber][modelIndex].remesh = false;
m_Models[pageNumber][modelIndex].initialized = true;
VkSubresourceLayout imageSubresourceLayout;
glm::vec4 bgColor{
m_Models[pageNumber][modelIndex].file->headerInfo.background.r,
m_Models[pageNumber][modelIndex].file->headerInfo.background.g,
m_Models[pageNumber][modelIndex].file->headerInfo.background.b,
m_Models[pageNumber][modelIndex].file->headerInfo.background.a
};
// IBL is enabled when the v3d header contains an environment map name.
// Already resolved above -- use the values from the early check.
// Derive pipeline mode from global VertexBuffers (vkrender.cc: each data type
// drawn independently; no intermediate Mesh struct needed).
MeshPipelineMode pipelineMode = MeshPipelineMode::MaterialOnly;
if (!colorData.indices.empty() && !materialData.indices.empty()) {
pipelineMode = MeshPipelineMode::Mixed;
} else if (!colorData.indices.empty()) {
pipelineMode = MeshPipelineMode::ColorOnly;
}
unsigned char* imageData = m_HeadlessRenderer->render(
glm::ivec2{ imageWidth, imageHeight },
&imageSubresourceLayout,
m_Models[pageNumber][modelIndex].viewMat,
m_Models[pageNumber][modelIndex].projMat,
m_Models[pageNumber][modelIndex].normMat,
m_Models[pageNumber][modelIndex].file->materials,
m_Models[pageNumber][modelIndex].file->headerInfo.lights,
pipelineMode,
bgColor,
m_Models[pageNumber][modelIndex].file->headerInfo.orthographic,
useIBL,
iblPath,
m_Models[pageNumber][modelIndex].drawMode,
m_Models[pageNumber][modelIndex].remesh
);
if (!imageData) {
LOG("[v3d] RenderModel: EXIT - HeadlessRenderer::render returned nullptr\n");
QImage image{ imageWidth, imageHeight, QImage::Format_ARGB32 };
image.fill(Qt::black);
return image;
}
unsigned char* imgDataTmp = imageData;
// renderheadless.cpp clamps targetSize to maxFramebufferWidth/Height.
// Derive the actual (clamped) dimensions from the subresource layout,
// capped at the requested size. Both the row copy below and the QImage
// built from it use these, so the deep copy never reads past the end of
// vectorData when a request exceeds the device framebuffer clamp.
int32_t actualWidth = std::min((int32_t)(imageSubresourceLayout.rowPitch / 4), imageWidth);
int32_t actualHeight = std::min((int32_t)(imageSubresourceLayout.size / imageSubresourceLayout.rowPitch), imageHeight);
std::vector<unsigned char> vectorData;
vectorData.reserve((size_t)actualWidth * actualHeight * 4);
for (int32_t y = 0; y < actualHeight; y++) {
unsigned int *row = (unsigned int*)imgDataTmp;
size_t rowBytes = (size_t)actualWidth * 4;
vectorData.resize(vectorData.size() + rowBytes);
std::memcpy(vectorData.data() + vectorData.size() - rowBytes, (unsigned char*)row, rowBytes);
imgDataTmp += imageSubresourceLayout.rowPitch;
}
delete[] imageData;
// Vulkan outputs BGRA bytes (VK_FORMAT_B8G8R8A8_UNORM).
// On little-endian x86, QImage::Format_ARGB32 stores bytes as B,G,R,A --
// so the byte order matches directly. No swap needed.
QImage image{ vectorData.data(), actualWidth, actualHeight, QImage::Format_ARGB32 };
image = image.copy(); // Deep copy so we don't alias vectorData's buffer
// Convert to RGB32 (no alpha channel) so Qt/Okular draws the pixels
// directly without any compositing over its paperColor background.
// The A-buffer blend shader already composited all transparent fragments
// against the v3d background color, so the final pixel colors are correct
// and no further alpha blending is needed.
image = image.convertToFormat(QImage::Format_RGB32);
// Prevent Qt/Okular from applying sRGB gamma correction on display.
// The shader handles color space (OUTPUT_AS_SRGB when srgb=true).
// With no managed color space, Qt treats pixel values as-is.
image.setColorSpace(QColorSpace());
// No vertical mirror needed: viewport {0,H,W,-H} maps NDC Y=+1 to
// framebuffer row 0, and QImage row 0 = top of screen.
// So scene +Y naturally appears at the top.
m_Models[pageNumber][modelIndex].m_HasChanged = false;
m_ModelImages[pageNumber][modelIndex] = image;
return image;
}
V3dModel& V3dModelManager::Model(size_t pageNumber, size_t modelIndex) {
QMutexLocker locker(&m_ModelsMutex);
if (pageNumber >= m_Models.size()) {
qWarning() << "V3dModelManager::Model: pageNumber" << pageNumber << "out of range (size =" << m_Models.size() << ")";
return m_EmptyModel;
}
if (modelIndex >= m_Models[pageNumber].size()) {
qWarning() << "V3dModelManager::Model: modelIndex" << modelIndex << "out of range for page" << pageNumber << "(size =" << m_Models[pageNumber].size() << ")";
return m_EmptyModel;
}
return m_Models[pageNumber][modelIndex];
}
std::vector<V3dModelManager::ModelBounds> V3dModelManager::ModelBoundsSnapshot(size_t pageNumber) const {
std::vector<ModelBounds> snapshot;
QMutexLocker locker(&m_ModelsMutex);
if (pageNumber >= m_Models.size()) {
return snapshot;
}
for (const V3dModel& model : m_Models[pageNumber]) {
snapshot.push_back(ModelBounds{ model.minBound, model.maxBound });
}
return snapshot;
}
bool V3dModelManager::Empty() {
QMutexLocker locker(&m_ModelsMutex);
return m_Models.empty();
}
bool V3dModelManager::ShouldSuppressPageViewRender() const {
// Event-driven state (see m_SuppressPageViewRender): true only between
// presentation-only refreshes and the matching mouse release (or
// PresentationWidget destroy). No clock, no window.
return m_SuppressPageViewRender.load(std::memory_order_acquire);
}
QImage V3dModelManager::LastRenderedModelImage(size_t pageNumber, size_t modelIndex) const {
QMutexLocker locker(&m_ModelsMutex);
if (pageNumber >= m_ModelImages.size() || modelIndex >= m_ModelImages[pageNumber].size()) {
return QImage();
}
// Implicitly shared copy (no pixel data duplication).
return m_ModelImages[pageNumber][modelIndex];
}
QImage V3dModelManager::CachedModelImage(size_t pageNumber, size_t modelIndex, int width, int height) const {
QMutexLocker locker(&m_ModelsMutex);
if (pageNumber >= m_ModelImages.size() || modelIndex >= m_ModelImages[pageNumber].size()) {
return QImage();
}
QImage cached = m_ModelImages[pageNumber][modelIndex];
if (cached.isNull()) {
return QImage();
}
if (width > 0 && height > 0 && (cached.width() != width || cached.height() != height)) {
cached = cached.scaled(width, height, Qt::IgnoreAspectRatio, Qt::FastTransformation);
}
return cached;
}
glm::vec2 V3dModelManager::GetCanvasSize(size_t pageNumber) {
if (pageNumber >= m_Models.size() || m_Models[pageNumber].empty()) {
qWarning() << "V3dModelManager::GetCanvasSize: pageNumber" << pageNumber << "invalid or empty";
return glm::vec2{ 0.0f, 0.0f };
}
return glm::vec2{ m_Models[pageNumber][0].file->headerInfo.canvasWidth, m_Models[pageNumber][0].file->headerInfo.canvasHeight };
}
void V3dModelManager::SetDocument(const Okular::Document* document) {
// A (new or different) document is about to be loaded into this manager:
// drop the previous document's models and cached state first. Without this,
// a generator reused for a new document composites the old document's 3D
// models onto the new one -- e.g. closing a 3D PDF and opening a 2D PDF
// still shows the 3D model, and it keeps intercepting mouse input. Runs on
// the main thread, before any AddModel() for the new document.
Clear();
m_Document = document;
}
void V3dModelManager::Clear() {
QMutexLocker locker(&m_ModelsMutex);
// Per-document content and caches (all guarded by m_ModelsMutex).
m_Models.clear();
m_ModelImages.clear();
m_Pages.clear();
m_CachedRequestSizes.clear();
m_ModelsWithMissingIBL.clear();
m_PresentationRequestSizes.clear();
// Interaction / input state from the previous document. Clear() runs on
// the main thread (from SetDocument), so these main-thread-only fields are
// safe to reset here; m_PendingRebindPosted is guarded by m_ModelsMutex.
m_Dragging = false;
m_ActiveModel = nullptr;
m_ActiveModelPage = -1;
m_LastEventSource = nullptr;
m_PendingRebindPosted = false;
m_SuppressPageViewRender.store(false, std::memory_order_release);
// The HeadlessRenderer is intentionally retained: it is reusable across
// documents and re-creating a Vulkan device per load is expensive.
}
bool V3dModelManager::mouseMoveEvent(QMouseEvent* event) {
if (m_Models.size() == 0 || !m_PageView) {
// If the document has no models, this is just a plain PDF document, no need for any special interaction
return false;
}
// Always keep an updated record of the mouse position even if the mouse is not down
m_MousePosition.x = (int)event->position().x();
m_MousePosition.y = (int)event->position().y();
LOG("[v3d] mouseMoveEvent: pos=(%d,%d) dragging=%d pageViewVisible=%d\n",
m_MousePosition.x, m_MousePosition.y, (int)m_Dragging, m_PageView ? (int)m_PageView->isVisible() : 0);
if (!m_Dragging) { m_LastMousePosition = m_MousePosition; return false; }
{
QMutexLocker locker(&m_ModelsMutex);
glm::vec2 normalizedMousePositionOnPage = GetNormalizedPositionRelativeToPage(m_MousePosition, m_ActiveModelPage);
glm::vec2 lastNormalizedMousePositionOnPage = GetNormalizedPositionRelativeToPage(m_LastMousePosition, m_ActiveModelPage);
V3dModel& model = *m_ActiveModel;
#ifdef MOUSE_BOUNDARIES
if (m_ActiveModelPage >= 0) {
float dpr = GetDevicePixelRatio();
int pg = GetPageMouseIsOver();
EnsureCachedRequestSize((size_t)pg);
int leftPixel = model.minBound.x * m_CachedRequestSizes[pg].size.x;
int rightPixel = leftPixel + (model.maxBound.x - model.minBound.x) * m_CachedRequestSizes[pg].size.x;
int topPixel = model.minBound.y * m_CachedRequestSizes[pg].size.y;
int bottomPixel = topPixel + (model.maxBound.y - model.minBound.y) * m_CachedRequestSizes[pg].size.y;
glm::vec2 mousePositionPixelSpace = normalizedMousePositionOnPage * glm::vec2{ m_CachedRequestSizes[pg].size };
m_MouseBoundaryLines[m_ActiveModelPage].push_back(Line{ glm::vec2{ leftPixel, topPixel }, glm::vec2{ rightPixel, topPixel } });
m_MouseBoundaryLines[m_ActiveModelPage].push_back(Line{ glm::vec2{ leftPixel, topPixel }, glm::vec2{ leftPixel, bottomPixel } });
m_MouseBoundaryLines[m_ActiveModelPage].push_back(Line{ glm::vec2{ rightPixel, topPixel }, glm::vec2{ rightPixel, bottomPixel } });
m_MouseBoundaryLines[m_ActiveModelPage].push_back(Line{ glm::vec2{ leftPixel, bottomPixel }, glm::vec2{ rightPixel, bottomPixel } });
m_MouseBoundaryPoints[m_ActiveModelPage].push_back(Point{ glm::vec2{ mousePositionPixelSpace } });
}
#endif
glm::vec2 normalizedPositionOnModel = {
(normalizedMousePositionOnPage.x - model.minBound.x) / (model.maxBound.x - model.minBound.x),
(normalizedMousePositionOnPage.y - model.minBound.y) / (model.maxBound.y - model.minBound.y)
};
glm::vec2 lastNormalizedPositionOnModel = {
(lastNormalizedMousePositionOnPage.x - model.minBound.x) / (model.maxBound.x - model.minBound.x),
(lastNormalizedMousePositionOnPage.y - model.minBound.y) / (model.maxBound.y - model.minBound.y)
};
glm::vec2 pageViewSize = GetActiveViewportSize();
bool controlKey = event->modifiers() & Qt::ControlModifier;
bool shiftKey = event->modifiers() & Qt::ShiftModifier;
bool altKey = event->modifiers() & Qt::AltModifier;
if (controlKey && !shiftKey && !altKey) {
EnsureCachedRequestSize((size_t)m_ActiveModelPage);
glm::vec2 canvasSize = {
(model.maxBound.x - model.minBound.x) * (m_CachedRequestSizes[m_ActiveModelPage].size.x),
(model.maxBound.y - model.minBound.y) * (m_CachedRequestSizes[m_ActiveModelPage].size.y),
};
model.dragModeShift(normalizedPositionOnModel, lastNormalizedPositionOnModel, canvasSize);
} else if (!controlKey && shiftKey && !altKey) {
model.dragModeZoom(normalizedPositionOnModel, lastNormalizedPositionOnModel, pageViewSize);
} else if (!controlKey && !shiftKey && altKey) {
model.dragModePan(normalizedPositionOnModel, lastNormalizedPositionOnModel, pageViewSize);
} else {
model.dragModeRotate(normalizedPositionOnModel, lastNormalizedPositionOnModel, pageViewSize);
}
} // QMutexLocker scope -- release BEFORE requesting refresh
// requestPixmapRefresh must be called OUTSIDE the mutex. If held while
// refreshPixmap() calls deletePixmaps(), Okular may request new pixmaps
// from another thread/context which calls RenderModel() -- that also acquires
// m_ModelsMutex, causing a deadlock (QMutex is non-recursive) or event-loop
// corruption that stops MouseMove routing.
requestPixmapRefresh(m_ActiveModelPage);
m_LastMousePosition = m_MousePosition;
return true;
}
bool V3dModelManager::mouseButtonPressEvent(QMouseEvent* event) {
LOG("[v3d] mouseButtonPressEvent: button=%d pos=(%d,%d)\n", (int)event->button(), (int)event->position().x(), (int)event->position().y());
if (m_Models.size() == 0 || !m_PageView) {
// If the document has no models, this is just a plain PDF document, no need for any special interaction
return false;
}
// Always keep an updated record of the mouse position even if the click is not the left button
m_MousePosition.x = (int)event->position().x();
m_MousePosition.y = (int)event->position().y();
if (!(event->button() & Qt::MouseButton::LeftButton)) {
return false;
}
int pageMouseIsOver = GetPageMouseIsOver();
LOG("[v3d] mouseButtonPressEvent: hit page=%d (mousePos=%d,%d, lastSource=%s)\n",
pageMouseIsOver, m_MousePosition.x, m_MousePosition.y,
m_LastEventSource ? m_LastEventSource->metaObject()->className() : "(null)");
if (pageMouseIsOver == -1 || pageMouseIsOver >= (int)m_Models.size()) return false;
glm::vec2 normalizedMousePositionOnPage = GetNormalizedPositionRelativeToPage(m_MousePosition, pageMouseIsOver);
V3dModel* modelMouseIsOver = nullptr;
{
QMutexLocker locker(&m_ModelsMutex);
for (auto& model : m_Models[pageMouseIsOver]) {
bool horizontallyOnModel = normalizedMousePositionOnPage.x > model.minBound.x && normalizedMousePositionOnPage.x < model.maxBound.x;
bool verticallyOnModel = normalizedMousePositionOnPage.y > model.minBound.y && normalizedMousePositionOnPage.y < model.maxBound.y;
if (!(horizontallyOnModel && verticallyOnModel)) continue; // Making the assumption no two models overlap
modelMouseIsOver = &model;
break;
} // QMutexLocker scope
}
if (modelMouseIsOver != nullptr) {
m_Dragging = true;
m_LastMousePosition = m_MousePosition;
m_ActiveModel = modelMouseIsOver;
m_ActiveModelPage = pageMouseIsOver;
return true;
}
return false;
}
bool V3dModelManager::mouseButtonReleaseEvent(QMouseEvent* event) {
if (m_Models.size() == 0 || !m_PageView) {
// If the document has no models, this is just a plain PDF document, no need for any special interaction
return false;
}
if (!(event->button() & Qt::MouseButton::LeftButton)) {
return false;
}
m_Dragging = false;
m_ActiveModel = nullptr;
m_ActiveModelPage = -1;
// End of a gesture: stop suppressing PageView renders (if a presentation
// drag was in progress). No catch-up refresh here: the frozen window keeps
// its last frame until the user next interacts with it, at which point its
// first frame re-renders it at its own size. Forcing a cross-size render on
// every gesture end triggered full renderer resource recreations that
// proved fragile (Vulkan device loss observed on some hardware, especially
// with transparent scenes).
m_SuppressPageViewRender.store(false, std::memory_order_release);
return false;
}
bool V3dModelManager::wheelEvent(QWheelEvent* event) {
LOG("[v3d] wheelEvent: delta=(%d,%d) models=%zu\n", event->angleDelta().x(), event->angleDelta().y(), m_Models.size());
if (m_Models.size() == 0 || !m_PageView) {
// If the document has no models, this is just a plain PDF document, no need for any special interaction
return false;
}
// Only allow wheel zoom for standalone v3d files
// (single model that fills the entire page). For PDFs with embedded
// models, wheel always scrolls to avoid fighting Okular's scroll.
V3dModel* targetModel = nullptr;
int targetPage = -1;
if (m_Models.size() == 1 && m_Models[0].size() == 1) {
const auto& model = m_Models[0][0];
if (model.minBound.x <= 0.0f && model.maxBound.x >= 1.0f &&
model.minBound.y <= 0.0f && model.maxBound.y >= 1.0f) {
// Standalone v3d: zoom directly
targetModel = &m_Models[0][0];
targetPage = 0;
}
}
// If already actively dragging a model, use that instead
if (m_Dragging && m_ActiveModel) {
targetModel = m_ActiveModel;
targetPage = m_ActiveModelPage;
}
if (targetModel == nullptr || targetPage == -1) {
return false;
}
{
QMutexLocker locker(&m_ModelsMutex);
glm::vec2 normalizedPositionOnPage = GetNormalizedPositionRelativeToPage(m_MousePosition, targetPage);
bool horizontallyOnModel = normalizedPositionOnPage.x > targetModel->minBound.x && normalizedPositionOnPage.x < targetModel->maxBound.x;
bool verticallyOnModel = normalizedPositionOnPage.y > targetModel->minBound.y && normalizedPositionOnPage.y < targetModel->maxBound.y;
if (!horizontallyOnModel || !verticallyOnModel) {
return false;
}
// Asymptote ignores shift on scroll -- just use whichever axis has the delta.
// Qt converts vertical->horizontal when shift is held, zeroing .y().
int deltaY = event->angleDelta().y();
if (deltaY == 0)
deltaY = event->angleDelta().x();
if (deltaY < 0) {
targetModel->Zoom /= targetModel->file->headerInfo.zoomFactor;
} else {
targetModel->Zoom *= targetModel->file->headerInfo.zoomFactor;
}
float maxZoom = std::sqrt(std::numeric_limits<float>::max());
float minZoom = 1 / maxZoom;
if (targetModel->Zoom < minZoom) {
targetModel->Zoom = minZoom;
} else if (targetModel->Zoom > maxZoom) {
targetModel->Zoom = maxZoom;
}
float dpr = GetDevicePixelRatio();
EnsureCachedRequestSize((size_t)targetPage);
glm::vec2 canvasSize = {
(targetModel->maxBound.x - targetModel->minBound.x) * (m_CachedRequestSizes[targetPage].size.x / dpr),
(targetModel->maxBound.y - targetModel->minBound.y) * (m_CachedRequestSizes[targetPage].size.y / dpr),
};
targetModel->setProjection(canvasSize);
} // QMutexLocker scope
requestPixmapRefresh((size_t)targetPage);
return true;
}
bool V3dModelManager::keyPressEvent(QKeyEvent* event) {
if (m_Models.size() == 0 || !m_PageView) {
return false;
}