00001
00009 #include "lv3d1_server.h"
00010
00011
00012 int LV3D_Disable_Render_Prio;
00013 int LV3D_Verbosity;
00014
00019 unsigned int LV3D_build_priority(int frameNo,double prioAdj) {
00020 if (LV3D_Disable_Render_Prio) return 0;
00021 const double prioMax=1.0;
00022 if (!(prioAdj<prioMax)) prioAdj=prioMax;
00023 return 0xC0000000u+frameNo-(int)(1000*prioAdj);
00024 }
00025
00026
00027
00028
00029 class impl_LV3D_Array {
00030 LV3D_Array *array;
00031
00032 CkViewable *viewable;
00033
00034
00035
00037 CkView *view;
00038 CkVector3d lastCamera;
00039
00040 void flush(void) {
00041 if (view) view->unref();
00042 view=NULL;
00043 renderRequested=false;
00044 }
00045 bool renderRequested;
00046 CkViewpoint renderViewpoint;
00047 int renderFrameID;
00048 void renderUpdate(const LV3D_ViewpointMsg *m) {
00049 renderViewpoint=m->viewpoint;
00050 renderFrameID=m->frameID;
00051 }
00052
00053 CkViewableID makeViewableID(int vi) {
00054 CkViewableID ret;
00055 int i;
00056 for (i=0;i<array->thisIndexMax.nInts;i++)
00057 ret.id[i]=array->thisIndexMax.data()[i];
00058 for (;i<3;i++)
00059 ret.id[i]=0;
00060 ret.id[3]=vi;
00061 return ret;
00062 }
00063 void status(const char *where) {
00064
00065 }
00066 public:
00067 impl_LV3D_Array(LV3D_Array *array_) :array(array_) {
00068 viewable=NULL;
00069 view=NULL;
00070 renderRequested=false;
00071 }
00072 ~impl_LV3D_Array() {
00073
00074 flush();
00075 }
00076
00077 inline void add(CkViewable *v_) {
00078 if (viewable) CkAbort("LV3D_Array::addViewable> Added too many viewables!\n");
00079 viewable=v_;
00080 }
00081 inline void remove(CkViewable *v_) {
00082 if (!viewable) CkAbort("LV3D_Array::removeViewable> No viewable to remove!\n");
00083 if (viewable!=v_) CkAbort("LV3D_Array::removeViewable> Can't remove wrong viewable!\n");
00084 viewable=NULL;
00085 flush();
00086 }
00087
00088 inline void newClient(int clientID) {
00089
00090 flush();
00091 }
00092
00098 void viewpoint(const LV3D_ViewpointMsg *m) {
00099 if (renderRequested)
00100 {
00101 status("Updating pending render...\n");
00102 renderUpdate(m);
00103 }
00104 else if (viewable) {
00105 if ((!view) || viewable->shouldRender(m->viewpoint,*view))
00106 {
00107 status("Reqesting rendering...\n");
00108 renderRequested=true;
00109 renderUpdate(m);
00110 double sizeAdjust=viewable->getSize(renderViewpoint);
00111 LV3D_RenderMsg *rm= LV3D_RenderMsg::new_(
00112 m->clientID,m->frameID,0,sizeAdjust);
00113 array->thisProxy[array->thisIndexMax].LV3D_Render(rm);
00114 }
00115 }
00116 }
00117
00121 void render(LV3D_RenderMsg *m) {
00122 status("Rendering...\n");
00123 CkView *v=viewable->renderView(renderViewpoint);
00124 flush();
00125 if (v) {
00126 view=v;
00127 view->id=makeViewableID(0);
00128
00129 double sizeAdjust=viewable->getSize(renderViewpoint);
00130 view->prio=LV3D_build_priority(renderFrameID,sizeAdjust);
00131 LV3D0_Deposit(view,m->clientID);
00132 }
00133 LV3D_RenderMsg::delete_(m);
00134 }
00135
00136 #if LV3D_USE_FLAT
00137 void LV3D_FlatRender(liveVizRequestMsg *m,LV3D_Array *arr);
00138 #endif
00139
00140 };
00141
00142
00143 void LV3D_Array::init(void) {
00144 impl=new impl_LV3D_Array(this);
00145 usesAtSync=true;
00146 }
00147
00148 void LV3D_Array::addViewable(CkViewable *v) {
00149 impl->add(v);
00150 }
00151 void LV3D_Array::removeViewable(CkViewable *v) {
00152 impl->remove(v);
00153 }
00154 void LV3D_Array::pup(PUP::er &p) {
00155
00156 if (LV3D_Verbosity>1) {
00157 const int *data=thisIndexMax.data();
00158 CkPrintf("LV3DArray(%d,%d,%d) pup on PE %d\n",
00159 data[0],data[1],data[2],CkMyPe());
00160 }
00161 }
00162
00163 LV3D_Array::~LV3D_Array() {
00164 delete impl;
00165 impl=NULL;
00166 }
00167
00168
00172 void LV3D_Array::LV3D_NewClient(int clientID)
00173 {
00174 impl->newClient(clientID);
00175 }
00176
00178 void LV3D_Array::LV3D_DoBalance(void) {
00179 AtSync();
00180 }
00181
00187 void LV3D_Array::LV3D_Viewpoint(LV3D_ViewpointMsg *m)
00188 {
00189 LV3D_Prepare();
00190 impl->viewpoint(m);
00191 }
00192
00196 void LV3D_Array::LV3D_Render(LV3D_RenderMsg *m)
00197 {
00198 impl->render(m);
00199 }
00200
00205 void LV3D_Array::LV3D_FlatRender(liveVizRequestMsg *m)
00206 {
00207 #if LV3D_USE_FLAT
00208 LV3D_Prepare();
00209 impl->LV3D_FlatRender(m,this);
00210 #endif
00211 }
00212
00213 void LV3D_Array::LV3D_Prepare(void) {}
00214
00215 #if LV3D_USE_FLAT
00216 void impl_LV3D_Array::LV3D_FlatRender(liveVizRequestMsg *m,LV3D_Array *arr)
00217 {
00218 {
00219 if (!viewable) goto skipit;
00220 CkViewpoint vp;
00221 liveVizRequestUnpack(m,vp);
00222 CkQuadView *v=(CkQuadView *)viewable->renderView(vp);
00223 if (v==NULL) goto skipit;
00224 CkVector3d topLeft=vp.project(v->corners[0]);
00225 CkAllocImage &src=v->getImage();
00226 int w=src.getWidth(), h=src.getHeight();
00227 CkAllocImage dest(w, h, 3);
00228 int x,y;
00229 for (y=0;y<h;y++) {
00230 unsigned char *i=src.getPixel(0,y);
00231 const int ip=4;
00232 int r, g, b;
00233 if (src.getLayout()==CkImage::layout_reversed)
00234 {r=2; g=1; b=0;}
00235 else
00236 {r=1; g=2; b=3;}
00237 unsigned char *o=dest.getPixel(0,y);
00238 const int op=3;
00239 for (x=0;x<w;x++) {
00240 o[op*x+0] = i[x*ip+r];
00241 o[op*x+1] = i[x*ip+g];
00242 o[op*x+2] = i[x*ip+b];
00243 }
00244 }
00245 x=(int)(topLeft.x+0.5), y=(int)(topLeft.y+0.5);
00246 liveVizDeposit(m, x,y, w,h, dest.getData(), arr);
00247 delete v;
00248 return;
00249 }
00250 skipit:
00251
00252 liveVizDeposit(m, 0,0, 0,0, 0, arr);
00253 }
00254 #endif
00255
00256
00257
00258
00259
00260 class LV3D1_Map : public CkArrayMap {
00261 unsigned int shiftPE, numPE;
00262 void init(void) {
00263 shiftPE=0;
00264 numPE=CkNumPes();
00265 if (numPE>1) {
00266 shiftPE=1;
00267 numPE-=shiftPE;
00268 }
00269 }
00270 public:
00271 LV3D1_Map() {init();}
00272 LV3D1_Map(CkMigrateMessage *m):CkArrayMap(m){init();}
00273
00274 int procNum(int arrayHdl, const CkArrayIndex &i)
00275 {
00276 #if 1
00277 if (i.nInts==1) {
00278
00279 return shiftPE+((i.data()[0])%numPE);
00280 }
00281 else
00282 #endif
00283 {
00284
00285 unsigned int hash=(i.hash()+739)%1280107;
00286 return shiftPE+(hash % numPE);
00287 }
00288 }
00289 };
00290
00291 void LV3D1_Attach(CkArrayOptions &opts)
00292 {
00293 opts.setMap(CProxy_LV3D1_Map::ckNew());
00294 }
00295
00296
00297
00299 LV3D_RenderMsg *LV3D_RenderMsg::new_(
00300 int client,int frame,int viewable,double prioAdj)
00301 {
00302 int prioBits=8*sizeof(prioAdj);
00303 LV3D_RenderMsg *m=new (prioBits) LV3D_RenderMsg;
00304 m->clientID=client;
00305 m->frameID=frame;
00306 m->viewableID=viewable;
00307 unsigned int *p=(unsigned int *)CkPriorityPtr(m);
00308 p[0]=LV3D_build_priority(frame,prioAdj);
00309 if (LV3D_Disable_Render_Prio) p[0]=0;
00310 CkSetQueueing(m,CK_QUEUEING_BFIFO);
00311 return m;
00312 }
00313 void LV3D_RenderMsg::delete_(LV3D_RenderMsg *m) {
00314 delete m;
00315 }
00316
00317 void LV3D1_ServerMgr::doBalance(void)
00318 {
00319 a.LV3D_DoBalance();
00320 }
00321
00322 void LV3D1_Init(CkArrayID aid,LV3D_Universe *theUniverse)
00323 {
00324 LV3D0_Init(theUniverse,new LV3D1_ServerMgr(aid));
00325 #if LV3D_USE_FLAT
00326
00327 CkCallback flatUpdate(CkIndex_LV3D_Array::LV3D_FlatRender(0),aid);
00328 liveVizInit(liveVizConfig(true,false),aid,flatUpdate);
00329 #endif
00330 }
00331
00332
00333
00334
00335 #include "lv3d1.def.h"