bool cfxDataFloat2::apply(cfxParam* param) { cgSetParameter2f(param->getParameter(), data.f0, data.f1); return true; }
bool cfxDataBool2::apply(cfxParam* param) { cgSetParameter2f(param->getParameter(), (float)data.b0, (float)data.b1); return true; }
bool cfxDataInt2::apply(cfxParam* param) { cgSetParameter2f(param->getParameter(), (float)data.i0, (float)data.i1); return true; }
void CgEffect::SetVector(const char* name, Vector2& v) { cgSetParameter2f(this->retrieveParameter(name), v.GetX(), v.GetY()); }
real DrawVoronoi(real* xx) { int i,j; real fEnergy = 1e20; GLfloat *buffer_screen = new GLfloat[screenwidth*screenheight*4]; ////////////////////////////////////////////// // First pass - Render the initial sites // ////////////////////////////////////////////// glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FB_objects); glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, fbo_attachments[0], GL_TEXTURE_RECTANGLE_NV, Processed_Texture[0], 0); CheckFramebufferStatus(); glClearColor(-1, -1, -1, -1); glClear(GL_COLOR_BUFFER_BIT); glDrawBuffer(fbo_attachments[0]); cgGLEnableProfile(VertexProfile); cgGLEnableProfile(FragmentProfile); cgGLBindProgram(VP_DrawSites); cgGLBindProgram(FP_DrawSites); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(1, screenwidth+1, 1, screenheight+1); glViewport(1, 1, screenwidth, screenheight); DrawSites(xx, NULL); // glReadBuffer(fbo_attachments[0]); // imdebugPixelsf(0, 0, screenwidth+2, screenheight+2, GL_RGBA); Current_Buffer = 1; ///////////////////////////////////// // Second pass - Flood the sites // ///////////////////////////////////// cgGLBindProgram(VP_Flood); cgGLBindProgram(FP_Flood); if (VP_Flood_Size != NULL) cgSetParameter2f(VP_Flood_Size, screenwidth, screenheight); bool ExitLoop = false; bool SecondExit; int steplength;; SecondExit = (additional_passes==0); bool PassesBeforeJFA; PassesBeforeJFA = (additional_passes_before>0); if (PassesBeforeJFA) steplength = pow(2.0, (additional_passes_before-1)); else steplength = (screenwidth>screenheight ? screenwidth : screenheight)/2; while (!ExitLoop) { glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, fbo_attachments[Current_Buffer], GL_TEXTURE_RECTANGLE_NV, Processed_Texture[Current_Buffer], 0); CheckFramebufferStatus(); glDrawBuffer(fbo_attachments[Current_Buffer]); glClearColor(-1, -1, -1, -1); glClear(GL_COLOR_BUFFER_BIT); //Bind & enable shadow map texture glActiveTextureARB(GL_TEXTURE0_ARB); glBindTexture(GL_TEXTURE_RECTANGLE_NV, Processed_Texture[1-Current_Buffer]); if (VP_Flood_Steplength != NULL) cgSetParameter1d(VP_Flood_Steplength, steplength); glBegin(GL_QUADS); glVertex2f(1.0, 1.0); glVertex2f(1.0, float(screenheight+1)); glVertex2f(float(screenwidth+1), float(screenheight+1)); glVertex2f(float(screenwidth+1), 1.0); glEnd(); glReadBuffer(fbo_attachments[Current_Buffer]); // imdebugPixelsf(0, 0, screenwidth+2, screenheight+2, GL_RGBA); if (steplength==1 && PassesBeforeJFA) { steplength = (screenwidth>screenheight ? screenwidth : screenheight)/2; PassesBeforeJFA = false; } else if (steplength>1) steplength /= 2; else if (SecondExit) ExitLoop = true; else { steplength = pow(2.0, (additional_passes-1)); SecondExit = true; } Current_Buffer = 1-Current_Buffer; } glReadPixels(1,1,screenwidth,screenheight,GL_RGBA,GL_FLOAT,buffer_screen); /////////////////////////////// // Test pass, Compute energy // /////////////////////////////// int Current_Energy_Buffer = 0; glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, fbo_attachments[0], GL_TEXTURE_RECTANGLE_NV, Energy_Texture[Current_Energy_Buffer], 0); CheckFramebufferStatus(); glDrawBuffer(fbo_attachments[0]); glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT); cgGLBindProgram(VP_ComputeEnergyCentroid); cgGLBindProgram(FP_ComputeEnergyCentroid); if (FP_ComputeEnergyCentroid_Size != NULL) cgSetParameter2f(FP_ComputeEnergyCentroid_Size, screenwidth, screenheight); glActiveTextureARB(GL_TEXTURE0_ARB); glBindTexture(GL_TEXTURE_RECTANGLE_ARB, Processed_Texture[1-Current_Buffer]); glBegin(GL_QUADS); glVertex2f(1.0, 1.0); glVertex2f(float(screenwidth+1), 1.0); glVertex2f(float(screenwidth+1), float(screenheight+1)); glVertex2f(1.0, float(screenheight+1)); glEnd(); // glReadBuffer(fbo_attachments[0]); // imdebugPixelsf(0, 0, screenwidth+2, screenheight+2, GL_RGBA); Current_Energy_Buffer = 1-Current_Energy_Buffer; ////////////////////// // perform reduction ////////////////////// cgGLBindProgram(VP_Deduction); cgGLBindProgram(FP_Deduction); bool ExitEnergyLoop = false; int quad_size = int((screenwidth>screenheight?screenwidth:screenheight)/2.0+0.5); while (!ExitEnergyLoop) { glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, fbo_attachments[0], GL_TEXTURE_RECTANGLE_NV, Energy_Texture[Current_Energy_Buffer], 0); CheckFramebufferStatus(); glDrawBuffer(fbo_attachments[0]); glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT); //Bind & enable shadow map texture glActiveTextureARB(GL_TEXTURE0_ARB); glBindTexture(GL_TEXTURE_RECTANGLE_ARB, Energy_Texture[1-Current_Energy_Buffer]); glBegin(GL_QUADS); glVertex2f(1.0, 1.0); glVertex2f(float(quad_size+1), 1.0); glVertex2f(float(quad_size+1), float(quad_size+1)); glVertex2f(1.0, float(quad_size+1)); glEnd(); // glReadBuffer(fbo_attachments[0]); // imdebugPixelsf(0, 0, screenwidth+2, screenheight+2, GL_RGBA); if (quad_size>1) { int temp = quad_size/2; quad_size = temp*2==quad_size ? temp : temp+1; } else ExitEnergyLoop = true; Current_Energy_Buffer = 1-Current_Energy_Buffer; } float total_sum[4]; // glReadBuffer(fbo_attachments[0]); // imdebugPixelsf(0, 0, screenwidth+2, screenheight+2, GL_RGBA); glReadPixels(1, 1, 1, 1, GL_RGBA, GL_FLOAT, &total_sum); printf("Energy: %f\n", total_sum[0]); fEnergy = total_sum[0]; ////////////////////////////////////////// // Third pass - Scatter points to sites // ////////////////////////////////////////// cgGLBindProgram(VP_ScatterCentroid); cgGLBindProgram(FP_ScatterCentroid); glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, fbo_attachments[0], GL_TEXTURE_RECTANGLE_NV, Site_Texture, 0); CheckFramebufferStatus(); glDrawBuffer(buffers[0]); glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT); if (VP_ScatterCentroid_Size != NULL) cgSetParameter2f(VP_ScatterCentroid_Size, screenwidth, screenheight); //Bind & enable shadow map texture glActiveTextureARB(GL_TEXTURE0_ARB); glBindTexture(GL_TEXTURE_RECTANGLE_NV, Processed_Texture[1-Current_Buffer]); glEnable(GL_BLEND); glBlendFunc(GL_ONE, GL_ONE); glBegin(GL_POINTS); for (i=0; i<screenwidth; i++) for (j=0; j<screenheight; j++) glVertex2f(i+1.5, j+1.5); glEnd(); glDisable(GL_BLEND); Current_Buffer = 1-Current_Buffer; /////////////////////////////////////// // Fourth pass - Test stop condition // /////////////////////////////////////// cgGLBindProgram(VP_DrawSitesOQ); cgGLBindProgram(FP_DrawSitesOQ); glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, fbo_attachments[2], GL_RENDERBUFFER_EXT, RB_object); CheckFramebufferStatus(); glDrawBuffer(fbo_attachments[2]); glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT); if (VP_DrawSitesOQ_Size != NULL) cgSetParameter2f(VP_DrawSitesOQ_Size, screenwidth, screenheight); //Bind & enable shadow map texture glActiveTextureARB(GL_TEXTURE0_ARB); glBindTexture(GL_TEXTURE_RECTANGLE_NV, Site_Texture); glActiveTextureARB(GL_TEXTURE1_ARB); glBindTexture(GL_TEXTURE_RECTANGLE_NV, Processed_Texture[Current_Buffer]); glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); glDepthMask(GL_FALSE); glBeginQueryARB(GL_SAMPLES_PASSED_ARB, occlusion_query); glBegin(GL_POINTS); for (i=0; i<point_num; i++) { float xx, yy; xx = i%screenwidth+1.5; yy = i/screenheight+1.5; glTexCoord1f(i); glVertex2f(xx, yy); } glEnd(); glEndQueryARB(GL_SAMPLES_PASSED_ARB); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glDepthMask(GL_TRUE); // glReadBuffer(fbo_attachments[2]); // imdebugPixelsf(0, 0, screenwidth+2, screenheight+2, GL_RGBA); do{ glGetQueryObjectivARB(occlusion_query, GL_QUERY_RESULT_AVAILABLE_ARB, &oq_available); }while(oq_available); glGetQueryObjectuivARB(occlusion_query, GL_QUERY_RESULT_ARB, &sampleCount); printf("sample count: %d\n", sampleCount); cgGLDisableProfile(VertexProfile); cgGLDisableProfile(FragmentProfile); //////////////////// // compute measures //////////////////// bool *bOnBoundary = new bool[point_num]; bool *bIsHexagon = new bool[point_num]; int *nNeighbors = new int[point_num*7]; real *dDiameter = new real[point_num]; real *dNeighborDist = new real[point_num]; float site_pos[2], x, y, dist, neighbor_pos[2]; int id, drow, dcol, nrow, ncol, neighbor_id, k; real dMaxDiameter, chi_id, chi; int nHex, nVC; for (id=0; id<point_num; id++) { bOnBoundary[id] = false; bIsHexagon[id] = true; nNeighbors[id*7] = 0; for (k=1; k<7; k++) nNeighbors[id*7+k] = -1; dDiameter[id] = -1; dNeighborDist[id] = 2*(screenwidth+screenheight); } dMaxDiameter = -1; chi = -1; nHex = nVC = 0; for (i=0; i<screenheight; i++) { for (j=0; j<screenwidth; j++) { site_pos[0] = buffer_screen[i*screenwidth*4+j*4]; site_pos[1] = buffer_screen[i*screenwidth*4+j*4+1]; id = int(buffer_screen[i*screenwidth*4+j*4+2]); x = j+1.5; y = i+1.5; site_pos[0] = (site_pos[0]-1)/screenwidth*2-1; site_pos[1] = (site_pos[1]-1)/screenheight*2-1; x = (x-1)/screenwidth*2-1; y = (y-1)/screenheight*2-1; dist = (x-site_pos[0])*(x-site_pos[0])+(y-site_pos[1])*(y-site_pos[1]); dist = sqrt(dist); dDiameter[id] = dDiameter[id]<dist ? dist : dDiameter[id]; // traverse 9 neighbors for (drow=-1; drow<=1; drow++) { for (dcol=-1; dcol<=1; dcol++) { if (drow==0 && dcol==0) continue; nrow = i+drow; ncol = j+dcol; if (nrow<0 || nrow>=screenheight || ncol<0 || ncol>=screenwidth) { bOnBoundary[id] = true; continue; } neighbor_pos[0] = buffer_screen[nrow*screenwidth*4+ncol*4]; neighbor_pos[1] = buffer_screen[nrow*screenwidth*4+ncol*4+1]; neighbor_id = int(buffer_screen[nrow*screenwidth*4+ncol*4+2]); neighbor_pos[0] = (neighbor_pos[0]-1)/screenwidth*2-1; neighbor_pos[1] = (neighbor_pos[1]-1)/screenheight*2-1; if (neighbor_id==id) continue; dist = (neighbor_pos[0]-site_pos[0])*(neighbor_pos[0]-site_pos[0]) +(neighbor_pos[1]-site_pos[1])*(neighbor_pos[1]-site_pos[1]); dist = sqrt(dist); dNeighborDist[id] = dNeighborDist[id]>dist ? dist : dNeighborDist[id]; for (k=1; k<7; k++) { if (nNeighbors[id*7+k]<0) { nNeighbors[id*7+k] = neighbor_id; nNeighbors[id*7]++; break; } else if (nNeighbors[id*7+k]==neighbor_id) break; } if (k==7) bIsHexagon[id] = false; } } } } for (id=0; id<point_num; id++) { if (nNeighbors[id*7]!=6) bIsHexagon[id] = false; } for (id=0; id<point_num; id++) { dMaxDiameter = dMaxDiameter<dDiameter[id] ? dDiameter[id] : dMaxDiameter; chi_id = 2*dDiameter[id]/dNeighborDist[id]; chi = chi<chi_id ? chi_id : chi; if (!bOnBoundary[id]) { nVC++; } if (bIsHexagon[id]) { nHex++; } } printf("\n==== measures ====\n"); printf("Number of VC in the middle: %d\n", nVC); printf("Number of hexagons: %d\n", nHex); printf("h: %f\n", dMaxDiameter); printf("chi: %f\n", chi); printf("==== measures ====\n\n"); //////////////////// // Fill Octagon & another //////////////////// GLubyte *ColorTexImage = new GLubyte[screenwidth*screenheight*4]; for (i=0; i<screenheight; i++) { for (j=0; j<screenwidth; j++) { int id = i*screenwidth+j; if (id<point_num) { if (bIsHexagon[id]) { ColorTexImage[id*4] = 255; ColorTexImage[id*4+1] = 255; ColorTexImage[id*4+2] = 255; ColorTexImage[id*4+3] = 255; } else { ColorTexImage[id*4] = 192; ColorTexImage[id*4+1] = 192; ColorTexImage[id*4+2] = 192; ColorTexImage[id*4+3] = 255; } } else { ColorTexImage[id*4] = ColorTexImage[id*4+1] = ColorTexImage[id*4+2] = ColorTexImage[id*4+3] = 0.0; } } } glActiveTextureARB(GL_TEXTURE2_ARB); glGenTextures(1, &Color_Texture); glBindTexture(GL_TEXTURE_RECTANGLE_NV, Color_Texture); glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_RGBA, screenwidth, screenheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, ColorTexImage); delete ColorTexImage; delete [] buffer_screen; delete [] bOnBoundary; delete [] bIsHexagon; delete [] nNeighbors; delete [] dDiameter; delete [] dNeighborDist; /////////////////////////////////// // Last pass, Display the result // /////////////////////////////////// glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, screenwidth-1, 0, screenheight-1); glViewport(0, 0, screenwidth, screenheight); glActiveTextureARB(GL_TEXTURE0_ARB); glBindTexture(GL_TEXTURE_RECTANGLE_NV, Processed_Texture[Current_Buffer]); glActiveTextureARB(GL_TEXTURE1_ARB); glBindTexture(GL_TEXTURE_RECTANGLE_NV, Site_Texture); cgGLEnableProfile(VertexProfile); cgGLEnableProfile(FragmentProfile); cgGLBindProgram(VP_FinalRender); cgGLBindProgram(FP_FinalRender); if (FP_FinalRender_Size != NULL) cgSetParameter2f(FP_FinalRender_Size, screenwidth, screenheight); // Set parameters of fragment program glBegin(GL_QUADS); glVertex2f(0.0, 0.0); glVertex2f(0.0, float(screenheight)); glVertex2f(float(screenwidth), float(screenheight)); glVertex2f(float(screenwidth), 0.0); glEnd(); cgGLDisableProfile(VertexProfile); cgGLDisableProfile(FragmentProfile); DrawSites(xx, NULL); return fEnergy; }
// Function of L-BFGS-B void funcgrad(real* x, real& f, real* g, const cudaStream_t& stream) { int i,j; get_timestamp(start_time_func); ////////////////////////////////////////////// // First pass - Render the initial sites // ////////////////////////////////////////////// // FB_objects에 Processed_Texture[0]을 반영 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FB_objects); // 결과가 Processed_Texture[0]에 반영되도록 설정 // fbo_attachments는 ColorAttachemnt 번호 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, fbo_attachments[0], GL_TEXTURE_RECTANGLE_NV, Processed_Texture[0], 0); CheckFramebufferStatus(); // 출력할 버퍼를 선정 glDrawBuffer(fbo_attachments[0]); glClearColor(-1, -1, -1, -1); glClear(GL_COLOR_BUFFER_BIT); cgGLEnableProfile(VertexProfile); cgGLEnableProfile(FragmentProfile); cgGLBindProgram(VP_DrawSites); cgGLBindProgram(FP_DrawSites); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(1, screenwidth+1, 1, screenheight+1); glViewport(1, 1, screenwidth, screenheight); DrawSites(x, stream); // glReadBuffer(fbo_attachments[0]); // imdebugPixelsf(0, 0, screenwidth+2, screenheight+2, GL_RGBA, "funcgrad - First Pass"); Current_Buffer = 1; // 다음에 사용할 Texture를 선정 ///////////////////////////////////// // Second pass - Flood the sites // ///////////////////////////////////// cgGLBindProgram(VP_Flood); cgGLBindProgram(FP_Flood); if (VP_Flood_Size != NULL) cgSetParameter2f(VP_Flood_Size, screenwidth, screenheight); bool ExitLoop = false; bool SecondExit; int steplength;; SecondExit = (additional_passes==0); bool PassesBeforeJFA; PassesBeforeJFA = (additional_passes_before>0); if (PassesBeforeJFA) steplength = pow(2.0, (additional_passes_before-1)); else steplength = (screenwidth>screenheight ? screenwidth : screenheight)/2; while (!ExitLoop) { // Pixel 단위로 값을 저장하고 있는 Processed_Texture를 VertextBuffer로 사용??? glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, fbo_attachments[Current_Buffer], GL_TEXTURE_RECTANGLE_NV, Processed_Texture[Current_Buffer], 0); CheckFramebufferStatus(); glDrawBuffer(fbo_attachments[Current_Buffer]); glClearColor(-1, -1, -1, -1); glClear(GL_COLOR_BUFFER_BIT); //Bind & enable shadow map texture glActiveTextureARB(GL_TEXTURE0_ARB); glBindTexture(GL_TEXTURE_RECTANGLE_NV, Processed_Texture[1-Current_Buffer]); if (VP_Flood_Steplength != NULL) cgSetParameter1d(VP_Flood_Steplength, steplength); glBegin(GL_QUADS); glVertex2f(1.0, 1.0); glVertex2f(1.0, float(screenheight+1)); glVertex2f(float(screenwidth+1), float(screenheight+1)); glVertex2f(float(screenwidth+1), 1.0); glEnd(); // glReadBuffer(fbo_attachments[Current_Buffer]); // imdebugPixelsf(0, 0, screenwidth+2, screenheight+2, GL_RGBA); // funcgrad가 2번 이상 호출하는 경우를 위한 초기화 코드 if (steplength==1 && PassesBeforeJFA) { steplength = (screenwidth>screenheight ? screenwidth : screenheight)/2; PassesBeforeJFA = false; } else if (steplength>1) steplength /= 2; else if (SecondExit) ExitLoop = true; else { steplength = pow(2.0, (additional_passes-1)); SecondExit = true; } // Loop를 돌 때마다 다른 버퍼를 사용하도록 처리 Current_Buffer = 1-Current_Buffer; } //////////////////////////////// // Third pass, Compute energy // //////////////////////////////// cgGLBindProgram(VP_Scatter); cgGLBindProgram(FP_Scatter); glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, fbo_attachments[0], GL_TEXTURE_RECTANGLE_NV, Site_Texture, 0); CheckFramebufferStatus(); glDrawBuffer(fbo_attachments[0]); glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT); if (VP_Scatter_Size != NULL) cgSetParameter2f(VP_Scatter_Size, screenwidth, screenheight); //Bind & enable shadow map texture glActiveTextureARB(GL_TEXTURE0_ARB); // Loop 종료시 CurrentBuffer를 변경하기 때문에 // 완료된 버퍼를 사용하기 위해 1-Current_Buffer를 선택 glBindTexture(GL_TEXTURE_RECTANGLE_NV, Processed_Texture[1-Current_Buffer]); glEnable(GL_BLEND); glBlendFunc(GL_ONE, GL_ONE); glCallList(ScreenPointsList); glDisable(GL_BLEND); Energyf(grSite, g, f_tb_dev, screenwidth, iSiteTextureHeight, point_num, stream); lbfgsbcuda::CheckBuffer(g, point_num * 2, point_num * 2); glFinish(); get_timestamp(end_time_func); elapsed_time_func = (end_time_func-start_time_func); total_time_func += elapsed_time_func; // energyf()의 결과를 반환 f = *f_tb_host; }
void CCgUniform2f::update(const void* data, const SMaterial& material) const { f32* Data = (f32*)data; cgSetParameter2f(Parameter, *Data, *(Data+1)); }
// // SetFloat2 // void CCgUniform::SetFloat2( GLfloat x, GLfloat y ) { cgSetParameter2f( m_Parameter, x, y ); }