void glsl_program :: LinkProgram() { if (!m_num) { error("can't link zero shaders"); return; } if(GLEW_VERSION_2_0) LinkGL2(); else LinkARB(); //post("getting variables"); getVariables(); #ifdef __APPLE__ // call API to check if linked program is running on hardware or in software emulation GemCGLint vertexGPUProcessing, fragmentGPUProcessing; CGLGetParameter (CGLGetCurrentContext(), kCGLCPGPUVertexProcessing, &vertexGPUProcessing); CGLGetParameter (CGLGetCurrentContext(), kCGLCPGPUFragmentProcessing, &fragmentGPUProcessing); post("vertex shader running in %sware", vertexGPUProcessing?"hard":"soft"); post("fragment shader running in %sware", fragmentGPUProcessing?"hard":"soft"); #endif //__APPLE__ // send program ID to outlet /* JMZ: shouldn't this be only done, when we have a linked program? */ t_atom a; gem::utils::glsl::atom_setshader(a, (GLEW_VERSION_2_0)?m_program:m_programARB); outlet_list(m_outProgramID, 0, 1, &a); }
// Initialize GL //***************************************************************************** void InitGL(int* argc, char **argv) { // initialize GLUT glutInit(argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); glutInitWindowPosition (glutGet(GLUT_SCREEN_WIDTH)/2 - iGraphicsWinWidth/2, glutGet(GLUT_SCREEN_HEIGHT)/2 - iGraphicsWinHeight/2); glutInitWindowSize(iGraphicsWinWidth, iGraphicsWinHeight); iGLUTWindowHandle = glutCreateWindow("OpenCL for GPU RGB Sobel Filter Demo"); #if !(defined (__APPLE__) || defined(MACOSX)) glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS); #endif // register glut callbacks glutKeyboardFunc(KeyboardGL); glutDisplayFunc(DisplayGL); glutReshapeFunc(Reshape); glutIdleFunc(Idle); glutTimerFunc(REFRESH_DELAY, timerEvent,0); // create GLUT menu iGLUTMenuHandle = glutCreateMenu(MenuGL); glutAddMenuEntry("Toggle Filter On/Off <spacebar>", ' '); glutAddMenuEntry("Toggle Processing between GPU and CPU [p]", 'p'); glutAddMenuEntry("Toggle between Full Screen and Windowed [f]", 'f'); glutAddMenuEntry("Increase Threshold [+]", '+'); glutAddMenuEntry("Decrease Threshold [-]", '-'); glutAddMenuEntry("Quit <esc>", '\033'); glutAttachMenu(GLUT_RIGHT_BUTTON); // Set clear color glClearColor(0.f, 0.f, 0.f, 0.f); // Zoom with fixed aspect ratio float fAspects[2] = {(float)glutGet(GLUT_WINDOW_WIDTH)/(float)uiImageWidth , (float)glutGet(GLUT_WINDOW_HEIGHT)/(float)uiImageHeight}; fZoom = fAspects[0] > fAspects[1] ? fAspects[1] : fAspects[0]; glPixelZoom(fZoom, fZoom); glewInit(); // Disable vertical sync, if supported #ifdef _WIN32 if (wglewIsSupported("WGL_EXT_swap_control")) { iVsyncState = wglGetSwapIntervalEXT(); wglSwapIntervalEXT(0); } #else #if defined (__APPLE__) || defined(MACOSX) GLint VBL = 0; CGLGetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, &iVsyncState); CGLSetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, &VBL); #else if(glxewIsSupported("GLX_SGI_swap_control")) { glXSwapIntervalSGI(0); } #endif #endif }
static CGSSurfaceID attach_gl_context_to_window(CGLContextObj glCtx, CGSWindowID wid, int *width, int *height) { CFArrayRef wins; CFDictionaryRef win, bnd; GLint params = 0; Float64 w = 0, h = 0; CGSSurfaceID sid = 0; CGSConnectionID cid = CGSMainConnectionID(); printf("cid:%d wid:%d\n", cid, wid); /* determine window size */ /* FIXME/TODO - CGWindowListCopyWindowInfo was introduced on OSX 10.5, * find alternative for lower versions. */ wins = CGWindowListCopyWindowInfo(kCGWindowListOptionIncludingWindow, wid); /* expect one result only */ win = (CFDictionaryRef)CFArrayGetValueAtIndex(wins, 0); bnd = (CFDictionaryRef)CFDictionaryGetValue(win, kCGWindowBounds); CFNumberGetValue((CFNumberRef)CFDictionaryGetValue((CFDictionaryRef)bnd, CFSTR("Width")), kCFNumberFloat64Type, &w); CFNumberGetValue((CFNumberRef)CFDictionaryGetValue(bnd, CFSTR("Height")), kCFNumberFloat64Type, &h); CFRelease(wins); /* create a surface. */ if(CGSAddSurface(cid, wid, &sid) != kCGErrorSuccess) { printf("ERR: no surface\n"); } printf("sid:%d\n", sid); /* set surface size, and order it frontmost */ if(CGSSetSurfaceBounds(cid, wid, sid, CGRectMake(0, 0, w, h)) != kCGErrorSuccess) printf("ERR: cant set bounds\n"); if(CGSOrderSurface(cid, wid, sid, 1, 0) != kCGErrorSuccess) printf("ERR: cant order front\n"); /* attach context to the surface */ if(CGLSetSurface(glCtx, cid, wid, sid) != kCGErrorSuccess) { printf("ERR: cant set surface\n"); } /* check drawable */ CGLGetParameter(glCtx, kCGLCPHasDrawable, ¶ms); if(params != 1) { printf("ERR: no drawable\n"); } *width = (int)w; *height = (int)h; return sid; }
/* PsychOSSetVBLSyncLevel - Enable or disable synchronization of bufferswaps to * onset of vertical retrace. This is the OS-X version... */ void PsychOSSetVBLSyncLevel(PsychWindowRecordType *windowRecord, int swapInterval) { CGLError error; GLint myinterval = (GLint) swapInterval; psych_bool oldStyle = (PsychPrefStateGet_ConserveVRAM() & kPsychUseOldStyleAsyncFlips) ? TRUE : FALSE; // Store new setting also in internal helper variable, e.g., to allow workarounds to work: windowRecord->vSynced = (swapInterval > 0) ? TRUE : FALSE; error=CGLSetParameter((oldStyle || PsychIsMasterThread()) ? windowRecord->targetSpecific.contextObject : windowRecord->targetSpecific.glswapcontextObject, kCGLCPSwapInterval, &myinterval); if (error) { if (PsychPrefStateGet_Verbosity()>1) printf("\nPTB-WARNING: FAILED to %s synchronization to vertical retrace!\n\n", (swapInterval>0) ? "enable" : "disable"); } error=CGLGetParameter((oldStyle || PsychIsMasterThread()) ? windowRecord->targetSpecific.contextObject : windowRecord->targetSpecific.glswapcontextObject, kCGLCPSwapInterval, &myinterval); if (error || (myinterval != (GLint) swapInterval)) { if (PsychPrefStateGet_Verbosity()>1) printf("\nPTB-WARNING: FAILED to %s synchronization to vertical retrace (System ignored setting)!\n\n", (swapInterval>0) ? "enable" : "disable"); } }
// Setup function for GLUT parameters and loop //***************************************************************************** void InitGL(int* argc, char **argv) { // init GLUT glutInit(argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); glutInitWindowPosition (glutGet(GLUT_SCREEN_WIDTH)/2 - iGraphicsWinWidth/2, glutGet(GLUT_SCREEN_HEIGHT)/2 - iGraphicsWinHeight/2); glutInitWindowSize(iGraphicsWinWidth, iGraphicsWinHeight); iGLUTWindowHandle = glutCreateWindow("OpenCL for GPU Nbody Demo"); #if !(defined (__APPLE__) || defined(MACOSX) || defined(__EMSCRIPTEN__)) glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS); #endif // init GLEW #ifndef __EMSCRIPTEN__ glewInit(); GLboolean bGlew = glewIsSupported("GL_VERSION_2_0 " "GL_VERSION_1_5 " "GL_ARB_multitexture " "GL_ARB_vertex_buffer_object"); oclCheckErrorEX(bGlew, shrTRUE, pCleanup); #endif glEnable(GL_DEPTH_TEST); glClearColor(0.0, 0.0, 0.0, 1.0); renderer = new ParticleRenderer(); // check GL errors GLenum error; while ((error = glGetError()) != GL_NO_ERROR) { #ifdef __EMSCRIPTEN__ shrLog("InitGL: error - %d\n", error); #else shrLog("InitGL: error - %s\n", (char *)gluErrorString(error)); #endif } // Disable vertical sync, if supported #ifdef _WIN32 if (wglewIsSupported("WGL_EXT_swap_control")) { iVsyncState = wglGetSwapIntervalEXT(); wglSwapIntervalEXT(0); } #else #if defined (__APPLE__) || defined(MACOSX) GLint VBL = 0; CGLGetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, &iVsyncState); CGLSetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, &VBL); #elif __EMSCRIPTEN__ #else if(glxewIsSupported("GLX_SGI_swap_control")) { glXSwapIntervalSGI(0); } #endif #endif // create a new parameter list paramlist = new ParamListGL("sliders"); paramlist->bar_col_outer[0] = 0.8f; paramlist->bar_col_outer[1] = 0.8f; paramlist->bar_col_outer[2] = 0.0f; paramlist->bar_col_inner[0] = 0.8f; paramlist->bar_col_inner[1] = 0.8f; paramlist->bar_col_inner[2] = 0.0f; // add parameters to the list // Point Size paramlist->AddParam(new Param<float>("Point Size", activeParams.m_pointSize, 0.0f, 10.0f, 0.01f, &activeParams.m_pointSize)); // Velocity Damping paramlist->AddParam(new Param<float>("Velocity Damping", activeParams.m_damping, 0.5f, 1.0f, .0001f, &(activeParams.m_damping))); // Softening Factor paramlist->AddParam(new Param<float>("Softening Factor", activeParams.m_softening, 0.001f, 1.0f, .0001f, &(activeParams.m_softening))); // Time step size paramlist->AddParam(new Param<float>("Time Step", activeParams.m_timestep, 0.0f, 1.0f, .0001f, &(activeParams.m_timestep))); // Cluster scale (only affects starting configuration paramlist->AddParam(new Param<float>("Cluster Scale", activeParams.m_clusterScale, 0.0f, 10.0f, 0.01f, &(activeParams.m_clusterScale))); // Velocity scale (only affects starting configuration) paramlist->AddParam(new Param<float>("Velocity Scale", activeParams.m_velocityScale, 0.0f, 1000.0f, 0.1f, &activeParams.m_velocityScale)); }