changed: gcc8 base update
[opensg.git] / Source / System / Cluster / Window / SortFirst / OSGSortFirstWindow.cpp
blobe681309a54f6b2cc14e0cb1f5f53fe1875587274
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include "OSGConfig.h"
46 #include "OSGGL.h"
47 #include "OSGTileCameraDecorator.h"
48 #include "OSGViewport.h"
49 #include "OSGGeometry.h"
50 #include "OSGStereoBufferViewport.h"
51 #include "OSGSortFirstWindow.h"
52 #include "OSGClusterViewBuffer.h"
53 #include "OSGConnection.h"
54 #include "OSGRenderNode.h"
55 #include "OSGTileGeometryLoad.h"
56 #include "OSGClusterNetwork.h"
57 #include "OSGRenderActionBase.h"
59 OSG_USING_NAMESPACE
61 ClusterViewBuffer SortFirstWindow::_bufferHandler;
63 // #define USE_VPORT_SLICES
65 /*! \class OSG::SortFirstWindow
66 \ingroup GrpSystemCluster
67 Cluster rendering configuration for sort first image composition
70 /*----------------------- constructors & destructors ----------------------*/
72 //! Constructor
74 SortFirstWindow::SortFirstWindow(void) :
75 Inherited(),
76 _tileLoadBalancer(NULL),
77 _renderNode(NULL)
81 //! Copy Constructor
83 SortFirstWindow::SortFirstWindow(const SortFirstWindow &source) :
84 Inherited(source),
85 _tileLoadBalancer(NULL),
86 _renderNode(NULL)
90 //! Destructor
92 SortFirstWindow::~SortFirstWindow(void)
94 if(_tileLoadBalancer)
95 delete _tileLoadBalancer;
96 if(_renderNode)
97 delete _renderNode;
100 /*----------------------------- class specific ----------------------------*/
102 //! initialize the static features of the class, e.g. action callbacks
104 void SortFirstWindow::initMethod(InitPhase ePhase)
106 Inherited::initMethod(ePhase);
109 //! react to field changes
111 void SortFirstWindow::changed(ConstFieldMaskArg whichField,
112 UInt32 origin,
113 BitVector details)
115 Inherited::changed(whichField, origin, details);
118 //! output the instance for debug purposes
120 void SortFirstWindow::dump( UInt32 ,
121 const BitVector ) const
123 SLOG << "Dump SortFirstWindow NI" << std::endl;
126 /*----------------------------- server methods ----------------------------*/
128 /** transfer server cababilities to the client
131 void SortFirstWindow::serverInit(Window *serverWindow,
132 UInt32 id)
134 #if USE_VPORT_SLICES
136 #else
137 UInt32 sync;
138 RenderNode renderNode;
139 Connection *connection=getNetwork()->getMainConnection();
141 // create cluster node information
142 // get performance
143 renderNode.determinePerformance(serverWindow);
144 renderNode.dump();
145 // transfer to client for load balancing
146 connection->putValue(id);
147 renderNode.copyToBin(*connection);
148 connection->flush();
149 connection->selectChannel();
150 connection->getValue(sync);
151 #endif
154 #ifdef OSG_OLD_RENDER_ACTION
155 /** update server window
157 * todo: enamble frustum culling if error is removed
159 void SortFirstWindow::serverRender( Window *serverWindow,
160 UInt32 id,
161 DrawActionBase *action )
163 TileCameraDecoratorPtr deco;
164 ViewportPtr serverPort;
165 ViewportPtr clientPort;
166 UInt32 sv,cv,regionStart;
167 UInt32 vpWidth;
168 UInt32 vpHeight;
170 // duplicate viewports
171 for(cv=0,sv=0;cv<getPort().size();cv++)
173 clientPort = getPort()[cv];
174 if(serverWindow->getPort().size() <= sv)
176 // create new port
177 //serverPort = StereoBufferViewport::create();
178 serverPort = dynamic_cast<ViewportPtr>(clientPort->shallowCopy());
179 deco=TileCameraDecorator::create();
181 serverWindow->addPort(serverPort);
182 serverPort->setCamera(deco);
184 else
186 serverPort = serverWindow->getPort()[sv];
187 deco=dynamic_cast<TileCameraDecoratorPtr>(serverPort->getCamera());
188 if(serverWindow->getPort()[sv]->getType() !=
189 clientPort->getType())
191 // there is a viewport with the wrong type
192 serverPort = dynamic_cast<ViewportPtr>(clientPort->shallowCopy());
193 serverWindow->replacePort(sv, serverPort);//[sv] = serverPort;
194 serverPort->setCamera(deco);
196 else
198 deco=dynamic_cast<TileCameraDecoratorPtr>(serverPort->getCamera());
200 //serverPort = serverWindow->getPort()[sv];
201 //deco=TileCameraDecoratorPtr::dcast(serverPort->getCamera());
204 // duplicate values
205 regionStart=cv * getServers().size() * 4 + id * 4;
206 serverPort->setSize(
207 Real32(getRegion()[regionStart+0] + clientPort->getPixelLeft()),
208 Real32(getRegion()[regionStart+1] + clientPort->getPixelBottom()),
209 Real32(getRegion()[regionStart+2] + clientPort->getPixelLeft()),
210 Real32(getRegion()[regionStart+3] + clientPort->getPixelBottom()));
212 serverPort->setRoot ( clientPort->getRoot() );
213 serverPort->setBackground( clientPort->getBackground() );
215 serverPort->assignForegrounds(clientPort->getForegrounds());
217 serverPort->setTravMask ( clientPort->getTravMask() );
219 // calculate tile parameters
220 vpWidth =clientPort->getPixelWidth();
221 vpHeight=clientPort->getPixelHeight();
223 deco->setFullWidth ( vpWidth );
224 deco->setFullHeight( vpHeight );
225 deco->setSize( getRegion()[ regionStart+0 ]/(float)vpWidth,
226 getRegion()[ regionStart+1 ]/(float)vpHeight,
227 getRegion()[ regionStart+2 ]/(float)vpWidth,
228 getRegion()[ regionStart+3 ]/(float)vpHeight );
229 deco->setDecoratee( clientPort->getCamera() );
231 sv++;
233 // remove unused ports
234 while(serverWindow->getPort().size()>sv)
236 serverWindow->subPort(sv);
239 Inherited::serverRender(serverWindow,id,action);
241 // compression type
242 if(getCompose())
244 if(getCompression().empty())
246 _bufferHandler.setImgTransType(NULL);
248 else
250 _bufferHandler.setImgTransType(getCompression().c_str());
252 if(getSubtileSize())
254 _bufferHandler.setSubtileSize(getSubtileSize());
258 #if 1
259 glDisable(GL_SCISSOR_TEST);
260 glClearColor(0,0,0,0);
261 glClear(GL_COLOR_BUFFER_BIT);
262 #endif
264 // render the viewports
265 serverWindow->activate();
266 serverWindow->frameInit();
267 action->setWindow( serverWindow );
268 for(sv=0;sv<serverWindow->getPort().size();++sv)
270 ViewportPtr vp=serverWindow->getPort()[sv];
271 vp->render( action );
273 // send resulting image
274 if(getCompose())
276 // activate the appropriate viewport to retrieve image
277 vp->activate();
279 // send image
280 _bufferHandler.send(
281 *getNetwork()->getMainPointConnection(),
282 ClusterViewBuffer::RGB,
283 vp->getPixelLeft(),
284 vp->getPixelBottom(),
285 vp->getPixelRight(),
286 vp->getPixelTop(),
287 0,0);
289 // deactivate the viewport
290 vp->deactivate();
294 #endif
296 void SortFirstWindow::serverRender( Window *serverWindow,
297 UInt32 id,
298 RenderActionBase *action )
300 TileCameraDecoratorUnrecPtr deco;
301 ViewportUnrecPtr serverPort;
302 Viewport *clientPort;
303 UInt32 sv,cv,regionStart;
304 UInt32 vpWidth;
305 UInt32 vpHeight;
307 // duplicate viewports
308 for(cv=0,sv=0;cv<getMFPort()->size();cv++)
310 clientPort = getPort(cv);
311 if(serverWindow->getMFPort()->size() <= sv)
313 // create new port
314 //serverPort = StereoBufferViewport::create();
315 serverPort =
316 dynamic_pointer_cast<Viewport>(clientPort->shallowCopy());
317 deco=TileCameraDecorator::create();
319 serverWindow->addPort(serverPort);
320 serverPort->setCamera(deco);
322 else
324 serverPort = serverWindow->getPort(sv);
325 deco=dynamic_cast<TileCameraDecorator *>(serverPort->getCamera());
326 if(serverWindow->getPort(sv)->getType() !=
327 clientPort->getType())
329 // there is a viewport with the wrong type
330 serverPort = dynamic_pointer_cast<Viewport>(
331 clientPort->shallowCopy());
333 serverWindow->replacePort(sv,
334 serverPort);//[sv] = serverPort;
335 serverPort->setCamera(deco);
337 else
339 deco=dynamic_cast<TileCameraDecorator *>(
340 serverPort->getCamera());
342 //serverPort = serverWindow->getPort()[sv];
343 //deco=TileCameraDecoratorPtr::dcast(serverPort->getCamera());
346 // duplicate values
347 regionStart=cv * getMFServers()->size32() * 4 + id * 4;
348 serverPort->setSize(
349 Real32(getRegion(regionStart+0) + clientPort->calcPixelLeft()),
350 Real32(getRegion(regionStart+1) + clientPort->calcPixelBottom()),
351 Real32(getRegion(regionStart+2) + clientPort->calcPixelLeft()),
352 Real32(getRegion(regionStart+3) + clientPort->calcPixelBottom()));
354 serverPort->setRoot ( clientPort->getRoot() );
355 serverPort->setBackground( clientPort->getBackground() );
357 serverPort->assignForegrounds(*(clientPort->getMFForegrounds()));
359 serverPort->setTravMask ( clientPort->getTravMask() );
361 // calculate tile parameters
362 vpWidth =clientPort->calcPixelWidth();
363 vpHeight=clientPort->calcPixelHeight();
365 deco->setFullWidth ( vpWidth );
366 deco->setFullHeight( vpHeight );
367 deco->setSize( getRegion( regionStart+0 )/Real32(vpWidth),
368 getRegion( regionStart+1 )/Real32(vpHeight),
369 getRegion( regionStart+2 )/Real32(vpWidth),
370 getRegion( regionStart+3 )/Real32(vpHeight) );
371 deco->setDecoratee( clientPort->getCamera() );
373 sv++;
375 // remove unused ports
376 while(serverWindow->getMFPort()->size()>sv)
378 serverWindow->subPort(sv);
381 Inherited::serverRender(serverWindow,id,action);
383 // compression type
384 if(getCompose())
386 if(getCompression().empty())
388 _bufferHandler.setImgTransType(NULL);
390 else
392 _bufferHandler.setImgTransType(getCompression().c_str());
394 if(getSubtileSize())
396 _bufferHandler.setSubtileSize(getSubtileSize());
400 #if 1
401 glDisable(GL_SCISSOR_TEST);
402 glClearColor(0,0,0,0);
403 glClear(GL_COLOR_BUFFER_BIT);
404 #endif
406 // render the viewports
407 serverWindow->activate();
408 serverWindow->frameInit();
409 action->setWindow( serverWindow );
410 for(sv=0;sv<serverWindow->getMFPort()->size();++sv)
412 Viewport *vp=serverWindow->getPort(sv);
413 vp->render( action );
415 // send resulting image
416 if(getCompose())
418 // activate the appropriate viewport to retrieve image
419 vp->activate();
421 // send image
422 _bufferHandler.send(
423 *getNetwork()->getMainPointConnection(),
424 ClusterViewBuffer::RGB,
425 vp->calcPixelLeft(),
426 vp->calcPixelBottom(),
427 vp->calcPixelRight(),
428 vp->calcPixelTop(),
429 0,0);
431 // deactivate the viewport
432 vp->deactivate();
437 /*! send image to client
439 void SortFirstWindow::serverSwap( Window *window,
440 UInt32 )
442 if(!getCompose())
444 Connection *connection=getNetwork()->getMainConnection();
445 connection->signal();
446 connection->wait();
448 window->swap();
451 /*----------------------------- client methods ----------------------------*/
453 /*! read server cababilities
456 void SortFirstWindow::clientInit( void )
458 #if USE_VPORT_SLICES
460 #else
461 UInt32 id;
462 RenderNode renderNode;
463 GroupConnection *connection = getNetwork()->getMainGroupConnection();
464 Connection::Channel channel;
466 _tileLoadBalancer=new TileLoadBalancer(getUseFaceDistribution());
467 // read all node infos
468 for(UInt32 i=0;i<connection->getChannelCount();++i)
470 printf("%d\n",i);
471 channel = connection->selectChannel();
472 connection->subSelection(channel);
473 connection->getValue(id);
474 renderNode.copyFromBin(*connection);
475 renderNode.dump();
476 _tileLoadBalancer->addRenderNode(renderNode,id);
478 connection->resetSelection();
479 printf("end\n");
480 // sync servers
481 connection->putValue(id);
482 connection->flush();
483 #endif
485 Inherited::clientInit();
488 /*! client frame init
491 void SortFirstWindow::clientPreSync( void )
493 if(getCompose())
495 // get window size from client window
496 if(getClientWindow() != NULL)
498 if(getWidth() != getClientWindow()->getWidth() ||
499 getHeight() != getClientWindow()->getHeight())
501 setSize(getClientWindow()->getWidth(),
502 getClientWindow()->getHeight());
506 #if 0
507 else
509 setSize(1280,
510 1024);
512 #endif
513 UInt32 i;
514 UInt32 cv;
515 TileLoadBalancer::ResultT region;
517 editMFRegion()->clear();
518 #if USE_VPORT_SLICES
519 for(cv=0;cv<getPort().size();cv++)
521 int s=getServers().size();
522 for(i=0;i<s;i++)
524 editRegion().push_back(i/float(s)*getWidth());
525 editRegion().push_back(0);
526 editRegion().push_back((i+1)/float(s)*getWidth());
527 editRegion().push_back(1*getHeight());
530 #else
531 for(cv=0;cv<getMFPort()->size();cv++)
533 _tileLoadBalancer->update( getPort(cv)->getRoot() );
534 _tileLoadBalancer->balance(getPort(cv),
535 false,
536 region);
537 for(i=0;i<getMFServers()->size();i++)
539 editMFRegion()->push_back(region[i].x1);
540 editMFRegion()->push_back(region[i].y1);
541 editMFRegion()->push_back(region[i].x2);
542 editMFRegion()->push_back(region[i].y2);
545 #endif
547 Inherited::clientPreSync();
550 #ifdef OSG_OLD_RENDER_ACTION
551 /*! client rendering
553 * one tile is rendered by the client
556 void SortFirstWindow::clientRender( RenderActionBase * /* action */ )
558 // Inherited::clientRender(action);
560 #endif
562 void SortFirstWindow::clientRender(RenderActionBase * /* action */)
564 // Inherited::clientRender(action);
567 /*! show data
570 void SortFirstWindow::clientSwap( void )
572 UInt32 cv;
573 GroupConnection *connection=getNetwork()->getMainGroupConnection();
574 if(getCompose())
576 if(getClientWindow()!=NULL)
578 // receive all viewports
579 for(cv=0;cv<getMFPort()->size();++cv)
581 Viewport *vp=getPort(cv);
583 // activate the appropriate viewport to receive image
584 vp->activate();
586 // receive image
587 _bufferHandler.recv(*connection);
589 // deactivate the viewport
590 vp->deactivate();
592 Inherited::clientSwap();
595 else
597 connection->wait();
598 connection->signal();