void gravity_update_async() { int result; if(ngrav_enable) { pthread_mutex_lock(&gravmutex); result = grav_ready; if(result) //Did the gravity thread finish? { if (!sys_pause||framerender){ //Only update if not paused //Switch the full size gravmaps, we don't really need the two above any more float *tmpf; if (gravity_cleared) { memset(th_gravx, 0, (XRES/CELL)*(YRES/CELL)*sizeof(float)); memset(th_gravy, 0, (XRES/CELL)*(YRES/CELL)*sizeof(float)); memset(th_gravp, 0, (XRES/CELL)*(YRES/CELL)*sizeof(float)); memset(th_ogravmap, 0, (XRES/CELL)*(YRES/CELL)*sizeof(float)); gravity_cleared = 0; } if(th_gravchanged) { #if !defined(GRAVFFT) && defined(GRAV_DIFF) memcpy(gravy, th_gravy, (XRES/CELL)*(YRES/CELL)*sizeof(float)); memcpy(gravx, th_gravx, (XRES/CELL)*(YRES/CELL)*sizeof(float)); memcpy(gravp, th_gravp, (XRES/CELL)*(YRES/CELL)*sizeof(float)); #else tmpf = gravy; gravy = th_gravy; th_gravy = tmpf; tmpf = gravx; gravx = th_gravx; th_gravx = tmpf; tmpf = gravp; gravp = th_gravp; th_gravp = tmpf; #endif } tmpf = gravmap; gravmap = th_gravmap; th_gravmap = tmpf; grav_ready = 0; //Tell the other thread that we're ready for it to continue pthread_cond_signal(&gravcv); } } pthread_mutex_unlock(&gravmutex); //Apply the gravity mask membwand(gravy, gravmask, (XRES/CELL)*(YRES/CELL)*sizeof(float), (XRES/CELL)*(YRES/CELL)*sizeof(unsigned)); membwand(gravx, gravmask, (XRES/CELL)*(YRES/CELL)*sizeof(float), (XRES/CELL)*(YRES/CELL)*sizeof(unsigned)); memset(gravmap, 0, (XRES/CELL)*(YRES/CELL)*sizeof(float)); } }
void Gravity::gravity_update_async() { int result; if (ngrav_enable) { bool signal_grav = false; { std::unique_lock<std::mutex> l(gravmutex, std::defer_lock); if (l.try_lock()) { result = grav_ready; if (result) //Did the gravity thread finish? { float *tmpf; if (th_gravchanged && !ignoreNextResult) { #if !defined(GRAVFFT) && defined(GRAV_DIFF) memcpy(gravy, th_gravy, (XRES/CELL)*(YRES/CELL)*sizeof(float)); memcpy(gravx, th_gravx, (XRES/CELL)*(YRES/CELL)*sizeof(float)); memcpy(gravp, th_gravp, (XRES/CELL)*(YRES/CELL)*sizeof(float)); #else tmpf = gravy; gravy = th_gravy; th_gravy = tmpf; tmpf = gravx; gravx = th_gravx; th_gravx = tmpf; tmpf = gravp; gravp = th_gravp; th_gravp = tmpf; #endif } ignoreNextResult = false; tmpf = gravmap; gravmap = th_gravmap; th_gravmap = tmpf; grav_ready = 0; //Tell the other thread that we're ready for it to continue signal_grav = true; } } } if (signal_grav) { gravcv.notify_one(); } //Apply the gravity mask membwand(gravy, gravmask, (XRES/CELL)*(YRES/CELL)*sizeof(float), (XRES/CELL)*(YRES/CELL)*sizeof(unsigned)); membwand(gravx, gravmask, (XRES/CELL)*(YRES/CELL)*sizeof(float), (XRES/CELL)*(YRES/CELL)*sizeof(unsigned)); memset(gravmap, 0, (XRES/CELL)*(YRES/CELL)*sizeof(float)); } }
void Gravity::update_grav(void) { int x, y, i, j, changed = 0; float val, distance; th_gravchanged = 0; #ifndef GRAV_DIFF //Find any changed cells for (i=0; i<YRES/CELL; i++) { if(changed) break; for (j=0; j<XRES/CELL; j++) { if(th_ogravmap[i*(XRES/CELL)+j]!=th_gravmap[i*(XRES/CELL)+j]){ changed = 1; break; } } } if(!changed) goto fin; memset(th_gravy, 0, (XRES/CELL)*(YRES/CELL)*sizeof(float)); memset(th_gravx, 0, (XRES/CELL)*(YRES/CELL)*sizeof(float)); #endif th_gravchanged = 1; membwand(th_gravmap, gravmask, (XRES/CELL)*(YRES/CELL)*sizeof(float), (XRES/CELL)*(YRES/CELL)*sizeof(unsigned)); for (i = 0; i < YRES / CELL; i++) { for (j = 0; j < XRES / CELL; j++) { #ifdef GRAV_DIFF if (th_ogravmap[i*(XRES/CELL)+j] != th_gravmap[i*(XRES/CELL)+j]) { #else if (th_gravmap[i*(XRES/CELL)+j] > 0.0001f || th_gravmap[i*(XRES/CELL)+j]<-0.0001f) //Only calculate with populated or changed cells. { #endif for (y = 0; y < YRES / CELL; y++) { for (x = 0; x < XRES / CELL; x++) { if (x == j && y == i)//Ensure it doesn't calculate with itself continue; distance = sqrt(pow(j - x, 2.0f) + pow(i - y, 2.0f)); #ifdef GRAV_DIFF val = th_gravmap[i*(XRES/CELL)+j] - th_ogravmap[i*(XRES/CELL)+j]; #else val = th_gravmap[i*(XRES/CELL)+j]; #endif th_gravx[y*(XRES/CELL)+x] += M_GRAV * val * (j - x) / pow(distance, 3.0f); th_gravy[y*(XRES/CELL)+x] += M_GRAV * val * (i - y) / pow(distance, 3.0f); th_gravp[y*(XRES/CELL)+x] += M_GRAV * val / pow(distance, 2.0f); } } } } } fin: memcpy(th_ogravmap, th_gravmap, (XRES/CELL)*(YRES/CELL)*sizeof(float)); memcpy(obmap, bmap, (XRES/CELL)*(YRES/CELL)*sizeof(unsigned char)); } #endif void Gravity::grav_mask_r(int x, int y, char checkmap[YRES/CELL][XRES/CELL], char shape[YRES/CELL][XRES/CELL], char *shapeout) { if(x < 0 || x >= XRES/CELL || y < 0 || y >= YRES/CELL) return; if(x == 0 || y ==0 || y == (YRES/CELL)-1 || x == (XRES/CELL)-1) *shapeout = 1; int x1 = x, x2 = x; while (x1 >= 1) { if(checkmap[y][x1-1] || bmap[y][x1-1]==WL_GRAV) break; x1--; } while (x2 < (XRES/CELL)-1) { if(checkmap[y][x2+1] || bmap[y][x2+1]==WL_GRAV) break; x2++; } // fill span for (x = x1; x <= x2; x++) checkmap[y][x] = shape[y][x] = 1; if(y >= 1) for(x = x1; x <= x2; x++) if(!checkmap[y-1][x] && bmap[y-1][x]!=WL_GRAV) grav_mask_r(x, y-1, checkmap, shape, shapeout); if(y < (YRES/CELL)-1) for(x = x1; x <= x2; x++) if(!checkmap[y+1][x] && bmap[y+1][x]!=WL_GRAV) grav_mask_r(x, y+1, checkmap, shape, shapeout); return; } void Gravity::mask_free(mask_el *c_mask_el){ if(c_mask_el==NULL) return; if(c_mask_el->next!=NULL) mask_free((mask_el*)c_mask_el->next); free(c_mask_el->shape); free(c_mask_el); } void Gravity::gravity_mask() { char checkmap[YRES/CELL][XRES/CELL]; int x = 0, y = 0; unsigned maskvalue; mask_el *t_mask_el = NULL; mask_el *c_mask_el = NULL; if(!gravmask) return; memset(checkmap, 0, sizeof(checkmap)); for(x = 0; x < XRES/CELL; x++) { for(y = 0; y < YRES/CELL; y++) { if(bmap[y][x]!=WL_GRAV && checkmap[y][x] == 0) { //Create a new shape if(t_mask_el==NULL){ t_mask_el = (mask_el *)malloc(sizeof(mask_el)); t_mask_el->shape = (char *)malloc((XRES/CELL)*(YRES/CELL)); memset(t_mask_el->shape, 0, (XRES/CELL)*(YRES/CELL)); t_mask_el->shapeout = 0; t_mask_el->next = NULL; c_mask_el = t_mask_el; } else { c_mask_el->next = (mask_el *)malloc(sizeof(mask_el)); c_mask_el = (mask_el *)c_mask_el->next; c_mask_el->shape = (char *)malloc((XRES/CELL)*(YRES/CELL)); memset(c_mask_el->shape, 0, (XRES/CELL)*(YRES/CELL)); c_mask_el->shapeout = 0; c_mask_el->next = NULL; } //Fill the shape grav_mask_r(x, y, (char (*)[XRES/CELL])checkmap, (char (*)[XRES/CELL])c_mask_el->shape, (char*)&c_mask_el->shapeout); } } } c_mask_el = t_mask_el; memset(gravmask, 0, (XRES/CELL)*(YRES/CELL)*sizeof(unsigned)); while(c_mask_el!=NULL) { char *cshape = c_mask_el->shape; for(x = 0; x < XRES/CELL; x++) { for(y = 0; y < YRES/CELL; y++) { if(cshape[y*(XRES/CELL)+x]){ if(c_mask_el->shapeout) maskvalue = 0xFFFFFFFF; else maskvalue = 0x00000000; gravmask[y*(XRES/CELL)+x] = maskvalue; } } } c_mask_el = (mask_el*)c_mask_el->next; } mask_free(t_mask_el); }
void Gravity::update_grav() { int x, y, changed = 0; int xblock2 = XRES/CELL*2, yblock2 = YRES/CELL*2; int i, fft_tsize = (xblock2/2+1)*yblock2; float mr, mc, pr, pc, gr, gc; for (y=0; y<YRES/CELL; y++) { if(changed) break; for (x=0; x<XRES/CELL; x++) { if(th_ogravmap[y*(XRES/CELL)+x] != th_gravmap[y*(XRES/CELL)+x] || bmap[y][x] != obmap[y][x]){ changed = 1; break; } } } if(changed) { th_gravchanged = 1; membwand(th_gravmap, gravmask, (XRES/CELL)*(YRES/CELL)*sizeof(float), (XRES/CELL)*(YRES/CELL)*sizeof(unsigned)); //copy gravmap into padded gravmap array for (y=0; y<YRES/CELL; y++) { for (x=0; x<XRES/CELL; x++) { th_gravmapbig[(y+YRES/CELL)*xblock2+XRES/CELL+x] = th_gravmap[y*(XRES/CELL)+x]; } } //transform gravmap fftwf_execute(plan_gravmap); //do convolution (multiply the complex numbers) for (i=0; i<fft_tsize; i++) { mr = th_gravmapbigt[i][0]; mc = th_gravmapbigt[i][1]; pr = th_ptgravxt[i][0]; pc = th_ptgravxt[i][1]; gr = mr*pr-mc*pc; gc = mr*pc+mc*pr; th_gravxbigt[i][0] = gr; th_gravxbigt[i][1] = gc; pr = th_ptgravyt[i][0]; pc = th_ptgravyt[i][1]; gr = mr*pr-mc*pc; gc = mr*pc+mc*pr; th_gravybigt[i][0] = gr; th_gravybigt[i][1] = gc; } //inverse transform, and copy from padded arrays into normal velocity maps fftwf_execute(plan_gravx_inverse); fftwf_execute(plan_gravy_inverse); for (y=0; y<YRES/CELL; y++) { for (x=0; x<XRES/CELL; x++) { th_gravx[y*(XRES/CELL)+x] = th_gravxbig[y*xblock2+x]; th_gravy[y*(XRES/CELL)+x] = th_gravybig[y*xblock2+x]; th_gravp[y*(XRES/CELL)+x] = sqrtf(pow(th_gravxbig[y*xblock2+x],2)+pow(th_gravybig[y*xblock2+x],2)); } } } else { th_gravchanged = 0; } memcpy(th_ogravmap, th_gravmap, (XRES/CELL)*(YRES/CELL)*sizeof(float)); memcpy(obmap, bmap, (XRES/CELL)*(YRES/CELL)*sizeof(unsigned char)); }