vsg  1.0.4
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 
41  Windows& windows() { return _windows; }
42  const Windows& windows() const { return _windows; }
43 
44  clock::time_point& start_point() { return _start_point; }
45  const clock::time_point& start_point() const { return _start_point; }
46 
47  FrameStamp* getFrameStamp() { return _frameStamp; }
48  const FrameStamp* getFrameStamp() const { return _frameStamp; }
49 
51  bool active() const;
52 
54  virtual void close();
55 
57  virtual bool pollEvents(bool discardPreviousEvents = true);
58 
60  UIEvents& getEvents() { return _events; }
61 
63  const UIEvents& getEvents() const { return _events; }
64 
66  void addEventHandler(ref_ptr<Visitor> eventHandler) { _eventHandlers.emplace_back(eventHandler); }
67 
68  void addEventHandlers(EventHandlers&& eventHandlers) { _eventHandlers.splice(_eventHandlers.end(), eventHandlers); }
69 
71  EventHandlers& getEventHandlers() { return _eventHandlers; }
72 
74  const EventHandlers& getEventHandlers() const { return _eventHandlers; }
75 
78 
80  void addUpdateOperation(ref_ptr<Operation> op, UpdateOperations::RunBehavior runBehavior = UpdateOperations::ONE_TIME)
81  {
82  updateOperations->add(op, runBehavior);
83  }
84 
87 
91  virtual bool advanceToNextFrame();
92 
94  virtual void handleEvents();
95 
96  virtual void compile(ref_ptr<ResourceHints> hints = {});
97 
98  virtual bool acquireNextFrame();
99 
102  virtual VkResult waitForFences(size_t relativeFrameIndex, uint64_t timeout);
103 
104  // Manage the work to do each frame using RecordAndSubmitTasks. those that need to present results to be wired up to respective Presentation object
105  RecordAndSubmitTasks recordAndSubmitTasks;
106 
107  // Manage the presentation of rendering using Presentation objects
108  using Presentations = std::vector<ref_ptr<Presentation>>;
109  Presentations presentations;
110 
112  virtual void assignRecordAndSubmitTaskAndPresentation(CommandGraphs commandGraphs);
113 
115  std::list<std::thread> threads;
116 
117  void setupThreading();
118  void stopThreading();
119 
120  virtual void update();
121 
122  virtual void recordAndSubmit();
123 
124  virtual void present();
125 
127  virtual void deviceWaitIdle() const;
128 
129  protected:
130  virtual ~Viewer();
131 
132  bool _close = false;
133 
134  ref_ptr<FrameStamp> _frameStamp;
135 
136  Windows _windows;
137 
138  clock::time_point _start_point;
139  UIEvents _events;
140  EventHandlers _eventHandlers;
141 
142  bool _threading = false;
143  ref_ptr<FrameBlock> _frameBlock;
144  ref_ptr<Barrier> _submissionCompleted;
145  };
146  VSG_type_name(vsg::Viewer);
147 
149  extern VSG_DECLSPEC void updateViewer(Viewer& viewer, const CompileResult& compileResult);
150 
151 } // 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:74
virtual void handleEvents()
pass the Events into the any register EventHandlers
virtual VkResult waitForFences(size_t relativeFrameIndex, uint64_t timeout)
ref_ptr< UpdateOperations > updateOperations
thread safe container for update operations
Definition: Viewer.h:77
virtual void assignRecordAndSubmitTaskAndPresentation(CommandGraphs commandGraphs)
create a RecordAndSubmitTask configured to manage specified commandGraphs and assign it to the viewer...
void addUpdateOperation(ref_ptr< Operation > op, UpdateOperations::RunBehavior runBehavior=UpdateOperations::ONE_TIME)
add an update operation
Definition: Viewer.h:80
void addEventHandler(ref_ptr< Visitor > eventHandler)
add event handler
Definition: Viewer.h:66
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:63
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:60
EventHandlers & getEventHandlers()
get the const list of EventHandlers
Definition: Viewer.h:71
virtual void addWindow(ref_ptr< Window > window)
add Window to Viewer
ref_ptr< CompileManager > compileManager
compile manager provides thread safe support for compiling subgraph
Definition: Viewer.h:86
Definition: ref_ptr.h:22
Definition: CompileManager.h:24