MNE-CPP  0.1.9
A Framework for Electrophysiology
customframegraph.cpp
Go to the documentation of this file.
1 //=============================================================================================================
36 //=============================================================================================================
37 // INCLUDES
38 //=============================================================================================================
39 
40 #include "customframegraph.h"
41 
42 //=============================================================================================================
43 // QT INCLUDES
44 //=============================================================================================================
45 
46 #include <Qt3DCore/QNode>
47 #include <Qt3DRender/QRenderSurfaceSelector>
48 #include <Qt3DRender/QClearBuffers>
49 #include <Qt3DRender/QNoDraw>
50 #include <Qt3DRender/QDispatchCompute>
51 #include <Qt3DRender/QTechniqueFilter>
52 #include <Qt3DRender/QCameraSelector>
53 #include <Qt3DRender/QMemoryBarrier>
54 #include <Qt3DRender/QFilterKey>
55 #include <Qt3DRender/QCamera>
56 #include <Qt3DRender/QSortPolicy>
57 #include <Qt3DRender/QRenderStateSet>
58 #include <Qt3DRender/QDepthTest>
59 #include <Qt3DRender/QNoDepthMask>
60 #include <Qt3DRender/QBlendEquation>
61 #include <Qt3DRender/QBlendEquationArguments>
62 #include <Qt3DRender/QCullFace>
63 #include <Qt3DRender/QRenderCapture>
64 #include <QOpenGLVersionProfile>
65 
66 //=============================================================================================================
67 // EIGEN INCLUDES
68 //=============================================================================================================
69 
70 //=============================================================================================================
71 // USED NAMESPACES
72 //=============================================================================================================
73 
74 using namespace DISP3DLIB;
75 using namespace Qt3DRender;
76 
77 //=============================================================================================================
78 // DEFINE GLOBAL METHODS
79 //=============================================================================================================
80 
81 //=============================================================================================================
82 // DEFINE MEMBER METHODS
83 //=============================================================================================================
84 
85 CustomFrameGraph::CustomFrameGraph(Qt3DCore::QNode *parent)
86 : QViewport(parent)
87 , m_pForwardTranspKey(new QFilterKey)
88 , m_pForwardKey(new QFilterKey)
89 , m_pForwardSortedKey(new QFilterKey)
90 , m_pComputeKey(new QFilterKey)
91 , m_pDepthTest(new QDepthTest)
92 , m_pCullFace(new QCullFace)
93 , m_pBlendEquation(new QBlendEquation)
94 , m_pBlendArguments(new QBlendEquationArguments)
95 , m_pNoDepthMask( new QNoDepthMask)
96 , m_bUseOpenGl4_3(false)
97 {
98  //Test for OpenGL version 4.3
99  QOpenGLVersionProfile profile;
100  auto version = profile.version();
101  if ((version.first == 4 && version.second >= 3) || (version.first > 4)) {
102  m_bUseOpenGl4_3 = true;
103  }
104 
105  init();
106 }
107 
108 //=============================================================================================================
109 
111 {
112  m_pSurfaceSelector->deleteLater();
113  m_pClearBuffers->deleteLater();
114  m_pNoDraw->deleteLater();
115  m_pDispatchCompute->deleteLater();
116  m_pComputeFilter->deleteLater();
117  m_pCameraSelector->deleteLater();
118  m_pForwardFilter->deleteLater();
119  m_pSortPolicy->deleteLater();
120  m_pForwardKey->deleteLater();
121  m_pComputeKey->deleteLater();
122  m_pForwardState->deleteLater();
123  m_pTransparentState->deleteLater();
124  m_pForwardSortedFilter->deleteLater();
125  m_pForwardSortedKey->deleteLater();
126  m_pForwardTranspFilter->deleteLater();
127  m_pForwardTranspKey->deleteLater();
128  m_pDepthTest->deleteLater();
129  m_pCullFace->deleteLater();
130  m_pBlendEquation->deleteLater();
131  m_pBlendArguments->deleteLater();
132  m_pNoDepthMask->deleteLater();
133  if(m_bUseOpenGl4_3){
134  m_pMemoryBarrier->deleteLater();
135  }
136 }
137 
138 //=============================================================================================================
139 
140 void CustomFrameGraph::setCamera(QCamera *tCamera)
141 {
142  m_pCameraSelector->setCamera(tCamera);
143 }
144 
145 //=============================================================================================================
146 
147 void CustomFrameGraph::setWorkGroupSize(const uint tX, const uint tY, const uint tZ)
148 {
149  m_pDispatchCompute->setWorkGroupX(tX);
150  m_pDispatchCompute->setWorkGroupY(tY);
151  m_pDispatchCompute->setWorkGroupZ(tZ);
152 }
153 
154 //=============================================================================================================
155 
156 void CustomFrameGraph::setClearColor(const QColor &tColor)
157 {
158  m_pClearBuffers->setClearColor(tColor);
159 }
160 
161 //=============================================================================================================
162 
164 {
165  return m_pCapture->requestCapture();
166 }
167 
168 //=============================================================================================================
169 
170 void CustomFrameGraph::init()
171 {
172  //Build the frame graph
173  m_pSurfaceSelector = new QRenderSurfaceSelector(this);
174 
175  //Clear buffer branch
176  m_pClearBuffers = new QClearBuffers(m_pSurfaceSelector);
177  m_pNoDraw = new QNoDraw(m_pClearBuffers);
178 
179  //Compute branch
180  m_pDispatchCompute = new QDispatchCompute(m_pSurfaceSelector);
181  m_pComputeFilter = new QTechniqueFilter(m_pDispatchCompute);
182 
183  // Forward render branch
184  m_pCameraSelector = new QCameraSelector(m_pSurfaceSelector);
185 
186  if(m_bUseOpenGl4_3) {
187  m_pMemoryBarrier = new QMemoryBarrier(m_pCameraSelector);
188  m_pForwardState = new QRenderStateSet(m_pMemoryBarrier);
189 
190  //Set Memory Barrier it ensures the finishing of the compute shader run before drawing the scene.
191  m_pMemoryBarrier->setWaitOperations(QMemoryBarrier::VertexAttributeArray);
192  } else {
193  //don't use memory barrier
194  m_pForwardState = new QRenderStateSet(m_pCameraSelector);
195  }
196 
197  m_pForwardFilter = new QTechniqueFilter(m_pForwardState);
198 
199  //Transparent forward render branch
200  m_pTransparentState = new QRenderStateSet(m_pForwardState);
201  m_pForwardTranspFilter = new QTechniqueFilter(m_pTransparentState);
202 
203  //Transparent sorted forward render branch
204  m_pForwardSortedFilter = new QTechniqueFilter(m_pTransparentState);
205  m_pSortPolicy = new QSortPolicy(m_pForwardSortedFilter);
206  m_pCapture = new QRenderCapture(m_pSortPolicy);
207 
208  //Init frame graph nodes
209  this->setNormalizedRect(QRectF(0.0f, 0.0f, 1.0f, 1.0f));
210 
211  //Set ClearBuffer
212  m_pClearBuffers->setBuffers(QClearBuffers::ColorDepthBuffer);
213  m_pClearBuffers->setClearColor(Qt::black);
214 
215  //Set depth test
216  m_pDepthTest->setDepthFunction(QDepthTest::Less);
217  m_pForwardState->addRenderState(m_pDepthTest);
218  m_pCullFace->setMode(QCullFace::Back);
219  m_pForwardState->addRenderState(m_pCullFace);
220 
221  //Set Transparent states
222  m_pBlendArguments->setSourceRgb(QBlendEquationArguments::SourceAlpha);
223  m_pBlendArguments->setDestinationRgb(QBlendEquationArguments::OneMinusSourceAlpha);
224  m_pBlendEquation->setBlendFunction(QBlendEquation::Add);
225  m_pTransparentState->addRenderState(m_pBlendArguments);
226  m_pTransparentState->addRenderState(m_pBlendEquation);
227  m_pTransparentState->addRenderState(m_pNoDepthMask);
228 
229  //Set filter keys these need to match with the material.
230  m_pComputeKey->setName(QStringLiteral("renderingStyle"));
231  m_pComputeKey->setValue(QStringLiteral("compute"));
232 
233  m_pForwardTranspKey->setName(QStringLiteral("renderingStyle"));
234  m_pForwardTranspKey->setValue(QStringLiteral("forwardTransparent"));
235 
236  m_pForwardKey->setName(QStringLiteral("renderingStyle"));
237  m_pForwardKey->setValue(QStringLiteral("forward"));
238 
239  m_pForwardSortedKey->setName(QStringLiteral("renderingStyle"));
240  m_pForwardSortedKey->setValue(QStringLiteral("forwardSorted"));
241 
242  //Add Matches
243  m_pComputeFilter->addMatch(m_pComputeKey);
244  m_pForwardFilter->addMatch(m_pForwardKey);
245  m_pForwardSortedFilter->addMatch(m_pForwardSortedKey);
246  m_pForwardTranspFilter->addMatch(m_pForwardTranspKey);
247 
248  //Set draw policy
249  QVector<QSortPolicy::SortType> sortTypes = {QSortPolicy::StateChangeCost, QSortPolicy::BackToFront};
250  m_pSortPolicy->setSortTypes(sortTypes);
251 }
252 
253 //=============================================================================================================
DISP3DLIB::CustomFrameGraph::setClearColor
void setClearColor(const QColor &tColor)
Definition: customframegraph.cpp:156
DISP3DLIB::CustomFrameGraph::setWorkGroupSize
void setWorkGroupSize(const uint tX, const uint tY, const uint tZ)
Definition: customframegraph.cpp:147
DISP3DLIB::CustomFrameGraph::~CustomFrameGraph
~CustomFrameGraph()
Definition: customframegraph.cpp:110
DISP3DLIB::CustomFrameGraph::setCamera
void setCamera(Qt3DRender::QCamera *tCamera)
Definition: customframegraph.cpp:140
customframegraph.h
CustomFrameGraph class declaration.
DISP3DLIB::CustomFrameGraph::CustomFrameGraph
CustomFrameGraph(Qt3DCore::QNode *parent=0)
Definition: customframegraph.cpp:85
DISP3DLIB::CustomFrameGraph::requestRenderCaptureReply
Qt3DRender::QRenderCaptureReply * requestRenderCaptureReply()
Definition: customframegraph.cpp:163