vsg 1.1.9
VulkanSceneGraph library
 
Loading...
Searching...
No Matches
Viewer.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2018 Robert Osfield
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
13</editor-fold> */
14
15#include <vsg/animation/AnimationManager.h>
16#include <vsg/app/CompileManager.h>
17#include <vsg/app/Presentation.h>
18#include <vsg/app/RecordAndSubmitTask.h>
19#include <vsg/app/UpdateOperations.h>
20#include <vsg/app/Window.h>
21#include <vsg/threading/Barrier.h>
22#include <vsg/threading/FrameBlock.h>
23#include <vsg/utils/Instrumentation.h>
24
25#include <map>
26
27namespace vsg
28{
29
32 class VSG_DECLSPEC Viewer : public Inherit<Object, Viewer>
33 {
34 public:
35 Viewer();
36
37 Viewer(const Viewer&) = delete;
38 Viewer& operator=(const Viewer& rhs) = delete;
39
41 virtual void addWindow(ref_ptr<Window> window);
42
44 virtual void removeWindow(ref_ptr<Window> window);
45
46 Windows& windows() { return _windows; }
47 const Windows& windows() const { return _windows; }
48
49 clock::time_point& start_point() { return _start_point; }
50 const clock::time_point& start_point() const { return _start_point; }
51
52 FrameStamp* getFrameStamp() { return _frameStamp; }
53 const FrameStamp* getFrameStamp() const { return _frameStamp; }
54
56 bool active() const;
57
59 virtual void close();
60
62 virtual bool pollEvents(bool discardPreviousEvents = true);
63
65 UIEvents& getEvents() { return _events; }
66
68 const UIEvents& getEvents() const { return _events; }
69
71 void addEventHandler(ref_ptr<Visitor> eventHandler) { _eventHandlers.emplace_back(eventHandler); }
72
73 void addEventHandlers(const EventHandlers& eventHandlers) { _eventHandlers.insert(_eventHandlers.end(), eventHandlers.begin(), eventHandlers.end()); }
74
76 EventHandlers& getEventHandlers() { return _eventHandlers; }
77
79 const EventHandlers& getEventHandlers() const { return _eventHandlers; }
80
83
85 void addUpdateOperation(ref_ptr<Operation> op, UpdateOperations::RunBehavior runBehavior = UpdateOperations::ONE_TIME)
86 {
87 updateOperations->add(op, runBehavior);
88 }
89
92
95
97 static constexpr double UseTimeSinceStartPoint = std::numeric_limits<double>::max();
98
102 virtual bool advanceToNextFrame(double simulationTime = UseTimeSinceStartPoint);
103
105 virtual void handleEvents();
106
107 virtual void compile(ref_ptr<ResourceHints> hints = {});
108
109 virtual bool acquireNextFrame();
110
113 virtual VkResult waitForFences(size_t relativeFrameIndex, uint64_t timeout);
114
115 // Manage the work to do each frame using RecordAndSubmitTasks. Those that need to present results need to be wired up to respective Presentation objects.
116 RecordAndSubmitTasks recordAndSubmitTasks;
117
118 // Manage the presentation of rendering using Presentation objects
119 using Presentations = std::vector<ref_ptr<Presentation>>;
120 Presentations presentations;
121
124 virtual void assignRecordAndSubmitTaskAndPresentation(CommandGraphs commandGraphs);
125
127 void addRecordAndSubmitTaskAndPresentation(CommandGraphs commandGraphs);
128
130 std::list<std::thread> threads;
131
132 void setupThreading();
133 void stopThreading();
134
135 virtual void update();
136
137 virtual void recordAndSubmit();
138
139 virtual void present();
140
142 virtual void deviceWaitIdle() const;
143
145 uint64_t frameReference = 0;
146 ref_ptr<Instrumentation> instrumentation;
147
150
151 protected:
152 virtual ~Viewer();
153
154 bool _close = false;
155
156 Windows _windows;
157
158 bool _firstFrame = true;
159 clock::time_point _start_point;
160 ref_ptr<FrameStamp> _frameStamp;
161
162 UIEvents _events;
163 EventHandlers _eventHandlers;
164
165 bool _threading = false;
166 ref_ptr<FrameBlock> _frameBlock;
167 ref_ptr<Barrier> _submissionCompleted;
168 };
169 VSG_type_name(vsg::Viewer);
170
172 extern VSG_DECLSPEC void updateViewer(Viewer& viewer, const CompileResult& compileResult);
173
174} // namespace vsg
RunBehavior
specification of whether update operation should be invoked once or on all frames
Definition UpdateOperations.h:31
Definition Viewer.h:33
virtual void deviceWaitIdle() const
Call vkDeviceWaitIdle on all the devices associated with this Viewer.
virtual bool advanceToNextFrame(double simulationTime=UseTimeSinceStartPoint)
void addRecordAndSubmitTaskAndPresentation(CommandGraphs commandGraphs)
Add command graphs creating RecordAndSubmitTask/Presentation objects where appropriate.
void assignInstrumentation(ref_ptr< Instrumentation > in_instrumentation)
Convenience method for assigning Instrumentation to the viewer and any associated objects.
virtual void handleEvents()
pass the Events into any registered EventHandlers
virtual VkResult waitForFences(size_t relativeFrameIndex, uint64_t timeout)
ref_ptr< UpdateOperations > updateOperations
thread safe container for update operations
Definition Viewer.h:82
virtual void assignRecordAndSubmitTaskAndPresentation(CommandGraphs commandGraphs)
static constexpr double UseTimeSinceStartPoint
hint for setting the FrameStamp::simulationTime to time since start_point()
Definition Viewer.h:97
ref_ptr< AnimationManager > animationManager
manager for starting and running animations
Definition Viewer.h:91
void addUpdateOperation(ref_ptr< Operation > op, UpdateOperations::RunBehavior runBehavior=UpdateOperations::ONE_TIME)
add an update operation
Definition Viewer.h:85
void addEventHandler(ref_ptr< Visitor > eventHandler)
add event handler
Definition Viewer.h:71
bool active() const
return true if viewer is valid and active
uint64_t frameReference
Hook for assigning Instrumentation to enable profiling of record traversal.
Definition Viewer.h:145
virtual bool pollEvents(bool discardPreviousEvents=true)
poll the events for all attached windows, return true if new events are available
UIEvents & getEvents()
get the current set of Events that are filled in by prior calls to pollEvents
Definition Viewer.h:65
virtual void close()
schedule closure of the viewer and associated windows, after a call to Viewer::close() the Viewer::ac...
virtual void removeWindow(ref_ptr< Window > window)
remove Window from Viewer
const EventHandlers & getEventHandlers() const
get the const list of EventHandlers
Definition Viewer.h:79
EventHandlers & getEventHandlers()
get the list of EventHandlers
Definition Viewer.h:76
const UIEvents & getEvents() const
get the const current set of Events that are filled in by prior calls to pollEvents
Definition Viewer.h:68
virtual void addWindow(ref_ptr< Window > window)
add Window to Viewer
ref_ptr< CompileManager > compileManager
compile manager provides thread safe support for compiling subgraphs
Definition Viewer.h:94
Definition ref_ptr.h:22
Definition CompileManager.h:27