vsg  1.1.0
VulkanSceneGraph library
Viewer.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2018 Robert Osfield
6 
7 Permission 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 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 THE 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/app/CompileManager.h>
16 #include <vsg/app/Presentation.h>
17 #include <vsg/app/RecordAndSubmitTask.h>
18 #include <vsg/app/UpdateOperations.h>
19 #include <vsg/app/Window.h>
20 #include <vsg/threading/Barrier.h>
21 #include <vsg/threading/FrameBlock.h>
22 
23 #include <map>
24 
25 namespace vsg
26 {
27 
30  class VSG_DECLSPEC Viewer : public Inherit<Object, Viewer>
31  {
32  public:
33  Viewer();
34 
35  Viewer(const Viewer&) = delete;
36  Viewer& operator=(const Viewer& rhs) = delete;
37 
39  virtual void addWindow(ref_ptr<Window> window);
40 
42  virtual void removeWindow(ref_ptr<Window> window);
43 
44  Windows& windows() { return _windows; }
45  const Windows& windows() const { return _windows; }
46 
47  clock::time_point& start_point() { return _start_point; }
48  const clock::time_point& start_point() const { return _start_point; }
49 
50  FrameStamp* getFrameStamp() { return _frameStamp; }
51  const FrameStamp* getFrameStamp() const { return _frameStamp; }
52 
54  bool active() const;
55 
57  virtual void close();
58 
60  virtual bool pollEvents(bool discardPreviousEvents = true);
61 
63  UIEvents& getEvents() { return _events; }
64 
66  const UIEvents& getEvents() const { return _events; }
67 
69  void addEventHandler(ref_ptr<Visitor> eventHandler) { _eventHandlers.emplace_back(eventHandler); }
70 
71  void addEventHandlers(const EventHandlers& eventHandlers) { _eventHandlers.insert(_eventHandlers.end(), eventHandlers.begin(), eventHandlers.end()); }
72 
74  EventHandlers& getEventHandlers() { return _eventHandlers; }
75 
77  const EventHandlers& getEventHandlers() const { return _eventHandlers; }
78 
81 
83  void addUpdateOperation(ref_ptr<Operation> op, UpdateOperations::RunBehavior runBehavior = UpdateOperations::ONE_TIME)
84  {
85  updateOperations->add(op, runBehavior);
86  }
87 
90 
94  virtual bool advanceToNextFrame();
95 
97  virtual void handleEvents();
98 
99  virtual void compile(ref_ptr<ResourceHints> hints = {});
100 
101  virtual bool acquireNextFrame();
102 
105  virtual VkResult waitForFences(size_t relativeFrameIndex, uint64_t timeout);
106 
107  // Manage the work to do each frame using RecordAndSubmitTasks. Those that need to present results need to be wired up to respective Presentation objects.
108  RecordAndSubmitTasks recordAndSubmitTasks;
109 
110  // Manage the presentation of rendering using Presentation objects
111  using Presentations = std::vector<ref_ptr<Presentation>>;
112  Presentations presentations;
113 
116  virtual void assignRecordAndSubmitTaskAndPresentation(CommandGraphs commandGraphs);
117 
119  void addRecordAndSubmitTaskAndPresentation(CommandGraphs commandGraphs);
120 
122  std::list<std::thread> threads;
123 
124  void setupThreading();
125  void stopThreading();
126 
127  virtual void update();
128 
129  virtual void recordAndSubmit();
130 
131  virtual void present();
132 
134  virtual void deviceWaitIdle() const;
135 
136  protected:
137  virtual ~Viewer();
138 
139  bool _close = false;
140 
141  ref_ptr<FrameStamp> _frameStamp;
142 
143  Windows _windows;
144 
145  clock::time_point _start_point;
146  UIEvents _events;
147  EventHandlers _eventHandlers;
148 
149  bool _threading = false;
150  ref_ptr<FrameBlock> _frameBlock;
151  ref_ptr<Barrier> _submissionCompleted;
152  };
153  VSG_type_name(vsg::Viewer);
154 
156  extern VSG_DECLSPEC void updateViewer(Viewer& viewer, const CompileResult& compileResult);
157 
158 } // namespace vsg
Definition: Inherit.h:28
RunBehavior
specification of whether update operation should be invoked once or on all frames
Definition: UpdateOperations.h:31
Definition: Viewer.h:31
virtual void deviceWaitIdle() const
Call vkDeviceWaitIdle on all the devices associated with this Viewer.
const EventHandlers & getEventHandlers() const
get the const list of EventHandlers
Definition: Viewer.h:77
void addRecordAndSubmitTaskAndPresentation(CommandGraphs commandGraphs)
Add command graphs creating RecordAndSubmitTask/Presentation objects where appropriate.
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:80
virtual void assignRecordAndSubmitTaskAndPresentation(CommandGraphs commandGraphs)
void addUpdateOperation(ref_ptr< Operation > op, UpdateOperations::RunBehavior runBehavior=UpdateOperations::ONE_TIME)
add an update operation
Definition: Viewer.h:83
void addEventHandler(ref_ptr< Visitor > eventHandler)
add event handler
Definition: Viewer.h:69
bool active() const
return true if viewer is valid and active
virtual bool advanceToNextFrame()
const UIEvents & getEvents() const
get the const current set of Events that are filled in by prior calls to pollEvents
Definition: Viewer.h:66
virtual bool pollEvents(bool discardPreviousEvents=true)
poll the events for all attached windows, return true if new events are available
virtual void close()
schedule closure of the viewer and associated windows, after a call to Viewer::close() the Viewer::ac...
UIEvents & getEvents()
get the current set of Events that are filled in by prior calls to pollEvents
Definition: Viewer.h:63
virtual void removeWindow(ref_ptr< Window > window)
remove Window from Viewer
EventHandlers & getEventHandlers()
get the list of EventHandlers
Definition: Viewer.h:74
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:89
Definition: ref_ptr.h:22
Definition: CompileManager.h:24