fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Cluster / Window / SortLast / OSGBinarySwapComposer.cpp
blob4bb9de939ab1771c36657706a4dbf1e9c070956b
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>
46 #include "OSGConfig.h"
48 #include "OSGBinarySwapComposer.h"
50 OSG_USING_NAMESPACE
53 /*! \class OSG::BinarySwapComposer
55 This composer implements the binary swap composition.
58 void BinarySwapComposer::initMethod(InitPhase ePhase)
60 Inherited::initMethod(ePhase);
64 /*----------------------- constructors & destructors ----------------------*/
66 BinarySwapComposer::BinarySwapComposer(void) :
67 Inherited ( ),
68 _writer (NULL ),
69 _barrier (NULL ),
70 _stopWriter (false ),
71 _swapWith (0 ),
72 _swapConnection(NULL ),
73 _tilesX (0 ),
74 _tilesY (0 ),
75 _tileBufferSize(0 ),
76 _tile ( ),
77 _readTile ( ),
78 _writeLeft (0 ),
79 _writeRight (0 ),
80 _writeBottom (0 ),
81 _writeTop (0 ),
82 _usableServers (0 ),
83 _statistics ( ),
84 _intDepthMax (0xffffffff),
85 _shortDepthMax (0xffff )
89 BinarySwapComposer::BinarySwapComposer(const BinarySwapComposer &source) :
90 Inherited (source ),
91 _writer (NULL ),
92 _barrier (NULL ),
93 _stopWriter (false ),
94 _swapWith (0 ),
95 _swapConnection(NULL ),
96 _tilesX (0 ),
97 _tilesY (0 ),
98 _tileBufferSize(0 ),
99 _tile ( ),
100 _readTile ( ),
101 _writeLeft (0 ),
102 _writeRight (0 ),
103 _writeBottom (0 ),
104 _writeTop (0 ),
105 _usableServers (0 ),
106 _statistics ( ),
107 _intDepthMax (0xffffffff),
108 _shortDepthMax (0xffff )
112 BinarySwapComposer::~BinarySwapComposer(void)
116 /*----------------------------- class specific ----------------------------*/
118 void BinarySwapComposer::changed(ConstFieldMaskArg whichField,
119 UInt32 origin,
120 BitVector details)
122 Inherited::changed(whichField, origin, details);
125 void BinarySwapComposer::dump( UInt32 ,
126 const BitVector ) const
128 SLOG << "Dump BinarySwapComposer NI" << std::endl;
131 /*----------------------------- features ---------------------------------*/
133 /*! Currently it is not possible to do rendering on the client
135 bool BinarySwapComposer::clientRendering()
137 return false;
140 /*----------------------------- composition -------------------------------*/
142 void BinarySwapComposer::open()
144 // determine usable servers
145 if(osgIsPower2(serverCount()))
146 _usableServers = serverCount();
147 else
148 _usableServers = osgNextPower2(serverCount()) / 2;
150 // _usableServers = 1;
152 // create server cross connection
153 _clusterWindow->getNetwork()->connectAllPointToPoint(clusterId(),
154 "StreamSock");
155 // do not buffer any data
156 for(UInt32 i=0 ; i <= serverCount() ; ++i)
157 clusterWindow()->getNetwork()->getConnection(i)->forceDirectIO();
160 // create barrier
161 _barrier = Barrier::get(NULL);
162 // create writer thread
163 _writer = BaseThread::get(NULL);
166 // read whole buffer
167 if(!_isClient && clusterId() < _usableServers)
169 // create barrier
170 _barrier = Barrier::get(NULL, false);
171 // create writer thread
172 _writer = BaseThread::get(NULL, false);
173 // start writer thread
174 _writer->runFunction( writeProc, this );
176 else
178 _writer = NULL;
182 void BinarySwapComposer::composeViewport(Viewport *port)
184 // setup viewport
185 GLint
186 pl=port->calcPixelLeft(),
187 pr=port->calcPixelRight(),
188 pb=port->calcPixelBottom(),
189 pt=port->calcPixelTop();
190 GLint pw=pr-pl+1,ph=pt-pb+1;
191 bool full = port->calcIsFullWindow();
192 glViewport(pl, pb, pw, ph);
193 glScissor(pl, pb, pw, ph);
194 if(! full)
195 glEnable(GL_SCISSOR_TEST);
197 GLboolean depth = glIsEnabled(GL_DEPTH_TEST);
198 GLboolean blend = glIsEnabled(GL_BLEND);
200 glDisable(GL_DEPTH_TEST);
201 glPushMatrix();
202 glLoadIdentity();
203 glMatrixMode(GL_PROJECTION);
204 glPushMatrix();
205 glLoadIdentity();
206 glOrtho(0, port->calcPixelWidth(),
207 0, port->calcPixelHeight(),-1,1);
209 // printf("max %x,%x\n",_intDepthMax,_shortDepthMax);
211 if(getAlpha())
213 glEnable(GL_BLEND);
214 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
216 _tilesX = port->calcPixelWidth() / getTileSize() + 1;
217 _tilesY = port->calcPixelHeight() / getTileSize() + 1;
219 _tileBufferSize = getTileSize()*getTileSize()*8+sizeof(TileBuffer);
220 _readTile.resize(_tileBufferSize);
222 _statistics.bytesIn = 0;
223 _statistics.bytesOut = 0;
225 if(isClient())
227 if(getShort())
229 UInt16 colorDummy;
230 UInt32 depthDummy;
231 recvFromServers(depthDummy,colorDummy,
232 GL_RGB,
233 GL_UNSIGNED_SHORT_5_6_5,
234 port);
236 else
238 if(getAlpha())
240 UInt32 colorDummy;
241 UInt32 depthDummy;
242 recvFromServers(depthDummy,colorDummy,
243 GL_RGBA,
244 GL_UNSIGNED_BYTE,
245 port);
247 else
249 RGBValue colorDummy;
250 UInt32 depthDummy;
251 recvFromServers(depthDummy,colorDummy,
252 GL_RGB,
253 GL_UNSIGNED_BYTE,
254 port);
257 if(getStatistics())
259 UInt32 maxIn = _statistics.bytesIn;
260 UInt32 maxOut = _statistics.bytesOut;
261 UInt32 maxIO = maxIn + maxOut;
262 UInt32 sumOut = _statistics.bytesOut;
263 UInt32 missing = _usableServers;
264 double composeTime = 1e32;
266 Connection *server;
267 Statistics statistics;
268 for(UInt32 i=0 ; i<_usableServers ;++i)
270 server = clusterWindow()->getNetwork()->getConnection(i);
271 server->selectChannel();
272 server->get(&statistics,sizeof(Statistics));
273 sumOut += statistics.bytesOut;
274 if(statistics.composeTime < composeTime)
275 composeTime = statistics.composeTime;
276 if(statistics.bytesOut > maxOut)
277 maxOut = statistics.bytesOut;
278 if(statistics.bytesIn > maxIn)
279 maxIn = statistics.bytesIn;
280 if(statistics.bytesIn + statistics.bytesOut > maxIO)
281 maxIO = statistics.bytesIn + statistics.bytesOut;
282 missing--;
284 printf("compose Time : %1.5lf\n",composeTime);
285 printf("Transfered bytes : %10d\n",sumOut);
286 printf("Max out : %10d\n",maxOut);
287 printf("Max in : %10d\n",maxIn);
288 printf("Max io : %10d\n",maxIO);
291 else
293 if(clusterId() < _usableServers)
295 _statistics.composeTime = -getSystemTime();
296 _tile.resize(_tileBufferSize * _tilesX * _tilesY);
297 if(getShort())
299 UInt16 colorDummy;
300 UInt32 depthDummy=_shortDepthMax;
301 // UInt32 depthDummy=_intDepthMax;
302 startReader(depthDummy,colorDummy,
303 // GL_UNSIGNED_SHORT,
304 GL_UNSIGNED_INT,
305 GL_RGB,
306 GL_UNSIGNED_SHORT_5_6_5,
307 port);
309 else
311 if(getAlpha())
313 UInt32 colorDummy;
314 UInt32 depthDummy=_intDepthMax;
315 startReader(depthDummy,colorDummy,
316 GL_UNSIGNED_INT,
317 GL_RGBA,
318 GL_UNSIGNED_BYTE,
319 port);
321 else
323 RGBValue colorDummy;
324 UInt32 depthDummy=_intDepthMax;
325 startReader(depthDummy,colorDummy,
326 GL_UNSIGNED_INT,
327 GL_RGB,
328 GL_UNSIGNED_BYTE,
329 port);
332 _statistics.composeTime += getSystemTime();
333 if(getStatistics())
335 Connection *client = clusterWindow()->getNetwork()->getConnection(serverCount());
336 client->put(&_statistics,sizeof(Statistics));
337 client->flush();
341 // max depth value !! find a better way
342 glClear(GL_DEPTH_BUFFER_BIT);
343 glReadPixels(0, 0, 1, 1,
344 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT,
345 &_intDepthMax);
346 glReadPixels(0, 0, 1, 1,
347 GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT,
348 &_shortDepthMax);
352 glPopMatrix();
353 glMatrixMode(GL_MODELVIEW);
354 glPopMatrix();
355 glEnable(GL_DEPTH_TEST);
357 // reset state
358 if(depth && !glIsEnabled(GL_DEPTH_TEST))
359 glEnable(GL_DEPTH_TEST);
360 if(!blend && glIsEnabled(GL_BLEND))
361 glDisable(GL_BLEND);
364 void BinarySwapComposer::close(void)
366 if(!isClient() && _writer)
368 _stopWriter = true;
369 _barrier->enter(2);
370 BaseThread::join(_writer);
371 _writer = NULL;
372 _barrier = NULL;
376 /*----------------------------- features ---------------------------------*/
378 /*! it is not possible to do rendering on the client with binary swap
379 currently ;-)
381 bool BinarySwapComposer::getClientRendering()
383 return false;
386 /*! use only 2^n servers
388 UInt32 BinarySwapComposer::getUsableServers()
390 return _usableServers;
393 /*----------------------------- helper ------------------------------------*/
395 BinarySwapComposer::TileBuffer *BinarySwapComposer::getTileBuffer(UInt32 x,UInt32 y)
397 return reinterpret_cast<TileBuffer*>(
398 &_tile[(y*_tilesX + x)*_tileBufferSize]);
401 BinarySwapComposer::TileBuffer *BinarySwapComposer::getTileReadBuffer(void)
403 return reinterpret_cast<TileBuffer*>(&_readTile[0]);
406 /*----------------------------- thread proc -------------------------------*/
409 void BinarySwapComposer::writeProc(void *arg)
411 BinarySwapComposer *the=static_cast<BinarySwapComposer*>(arg);
412 for(;;)
414 the->_barrier->enter(2);
415 if(the->_stopWriter)
416 break;
417 if(the->getShort())
419 UInt16 colorDummy;
420 UInt32 depthDummy;
421 the->writeCombine(depthDummy,colorDummy);
423 else
425 if(the->getAlpha())
427 RGBValue colorDummy;
428 UInt32 depthDummy;
429 the->writeCombine(depthDummy,colorDummy);
431 else
433 UInt32 colorDummy;
434 UInt32 depthDummy;
435 the->writeCombine(depthDummy,colorDummy);