//-------------------------------------------------------------- void testApp::update(){ mousePos.x = ofGetMouseX(); mousePos.y = ofGetMouseY(); dimensions.x = ofGetWidth(); dimensions.y = ofGetHeight(); opencl.kernel("updateParticle")->setArg(2, mousePos); opencl.kernel("updateParticle")->setArg(3, dimensions); opencl.kernel("updateParticle")->run1D(NUM_PARTICLES); }
//-------------------------------------------------------------- void ofApp::update(){ mousePos.x = ofGetMouseX(); mousePos.y = ofGetMouseY(); dimensions.x = ofGetWidth(); dimensions.y = ofGetHeight(); opencl.kernel("updateParticle")->setArg(2, mousePos);//.getPtr(), sizeof(float2)); opencl.kernel("updateParticle")->setArg(3, dimensions);//.getPtr(), sizeof(float2) ); glFlush(); opencl.kernel("updateParticle")->run1D(NUM_PARTICLES); }
//-------------------------------------------------------------- void ofApp::draw(){ opencl.finish(); glColor3f(1.0f, 1.0f, 1.0f); glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo); opencl.finish(); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(2, GL_FLOAT, 0, 0); glDrawArrays(GL_POINTS, 0, NUM_PARTICLES); glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); glColor3f(1, 1, 1); string info = "fps: " + ofToString(ofGetFrameRate()) + "\nnumber of particles: " + ofToString(NUM_PARTICLES); ofDrawBitmapString(info, 20, 20); }
//-------------------------------------------------------------- void testApp::draw(){ glColor3f(1.0f, 1.0f, 1.0f); glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo[0]); #ifdef USE_OPENGL_CONTEXT opencl.finish(); #else opencl.readBuffer(sizeof(Vec2) * NUM_PARTICLES, clMemPosVBO, particlesPos); glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(Vec2) * NUM_PARTICLES, particlesPos); #endif glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(2, GL_FLOAT, 0, 0); glDrawArrays(GL_POINTS, 0, NUM_PARTICLES); glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); glColor3f(1, 1, 1); string info = "fps: " + ofToString(ofGetFrameRate()) + "\nnumber of particles: " + ofToString(NUM_PARTICLES); ofDrawBitmapString(info, 20, 20); }
void ofxKinectTracking::init(){ openCL.setupFromOpenGL(); #ifndef NORMAL_CAMERA kinect = new ofxKinect(); kinect->init(); kinect->setVerbose(true); kinect->open(); //The depth image clImage[0].initFromTexture(kinect->getDepthTextureReference(), CL_MEM_READ_ONLY, 0); #else videoGrabber.initGrabber(640, 480, true); clImage[0].initWithTexture(640, 480, GL_LUMINANCE, CL_MEM_READ_ONLY); pixels = new unsigned char[640*480]; #endif //The debug image clImage[1].initWithTexture(640, 480, GL_RGBA); //Intialize the ants buffer resetBufferData(); clAntsBuffer.initBuffer(sizeof(Ant)*NUM_ANTS, CL_MEM_READ_WRITE, ants, true); //Buffer of shared variables sharedVariables[0] = 0; clSharedBuffer.initBuffer(sizeof(int)*1, CL_MEM_READ_WRITE, sharedVariables, true); openCL.loadProgramFromFile("../../../../../addons/ofxKinectTracking/src/KinectTracking.cl"); openCL.loadKernel("preUpdate"); openCL.loadKernel("update"); openCL.loadKernel("postUpdate"); }
//-------------------------------------------------------------- void testApp::setup(){ ofBackground(50, 50, 50); // dump everything to console ofSetLogLevel(OF_LOG_VERBOSE); // disable vsync (to allow >60fps) ofSetVerticalSync(false); // init grabber videoGrabber.initGrabber(640, 480); vidWidth = videoGrabber.getWidth(); vidHeight = videoGrabber.getHeight(); // allocate temp buffer pixels = new unsigned char[vidWidth * vidHeight * 4]; // init OpenCL from OpenGL context to enable GL-CL data sharing openCL.setupFromOpenGL(); // create OpenCL textures and related OpenGL textures clImage[0].initWithTexture(vidWidth, vidHeight, GL_RGBA); clImage[1].initWithTexture(vidWidth, vidHeight, GL_RGBA); // load and compile OpenCL program openCL.loadProgramFromFile("MSAOpenCL/ImageProcessing.cl"); // load kernels openCL.loadKernel("msa_boxblur"); openCL.loadKernel("msa_flipx"); openCL.loadKernel("msa_flipy"); openCL.loadKernel("msa_greyscale"); openCL.loadKernel("msa_invert"); openCL.loadKernel("msa_threshold"); }
//-------------------------------------------------------------- void testApp::draw(){ videoGrabber.draw(0, 200); // make sure all OpenCL kernels have finished executing before drawing openCL.finish(); // draw the OpenGL texture (which was mapped to the OpenCL image) clImage[activeImageIndex].getTexture().draw(vidWidth, 200); ofDrawBitmapString( " app FPS : " + ofToString(ofGetFrameRate(), 2) + "\n capture FPS : " + ofToString(captureFPS, 2) + "\n" + "\n doBlur (b) : " + (doBlur ? "X" : "") + "\n blurAmount (1-9) : " + ofToString(blurAmount) + "\n doFlipX (x) : " + (doFlipX ? "X" : "") + "\n doFlipY (y) : " + (doFlipY ? "X" : "") + "\n doGreyscale (g) : " + (doGreyscale ? "X" : "") + "\n doInvert (i) : " + (doInvert ? "X" : "") + "\n doThreshold (t) : " + (doThreshold ? "X" : "") + "\n threshLevel ([]) : " + ofToString(threshLevel, 2) , 30, 30); }
//-------------------------------------------------------------- void testApp::setup(){ ofBackground(0, 0, 0); ofSetLogLevel(OF_LOG_VERBOSE); ofSetVerticalSync(false); opencl.setupFromOpenGL(); // create vbo glGenBuffersARB(1, &vbo); glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo); glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(float2) * NUM_PARTICLES, 0, GL_DYNAMIC_COPY_ARB); glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); // init host and CL buffers particles.initBuffer(NUM_PARTICLES); particlePos.initFromGLObject(vbo, NUM_PARTICLES); // init data for(int i=0; i<NUM_PARTICLES; i++) { Particle &p = particles[i]; p.vel.set(0, 0); p.mass = ofRandom(0.5, 1); particlePos[i].set(ofRandomWidth(), ofRandomHeight()); } particles.writeToDevice(); particlePos.writeToDevice(); opencl.loadProgramFromFile("MSAOpenCL/Particle.cl"); opencl.loadKernel("updateParticle"); opencl.kernel("updateParticle")->setArg(0, particles.getCLMem()); opencl.kernel("updateParticle")->setArg(1, particlePos.getCLMem()); opencl.kernel("updateParticle")->setArg(2, mousePos); opencl.kernel("updateParticle")->setArg(3, dimensions); glPointSize(1); }
//-------------------------------------------------------------- void testApp::update(){ // grab new frame videoGrabber.update(); // if there is a new frame.... if(videoGrabber.isFrameNew()) { // RGB textures don't seem to work well. so need to copy the vidgrabber data into a RGBA texture int pixelIndex = 0; for(int i=0; i<vidWidth; i++) { for(int j=0; j<vidHeight; j++) { int indexRGB = pixelIndex * 3; int indexRGBA = pixelIndex * 4; pixels[indexRGBA ] = videoGrabber.getPixels()[indexRGB ]; pixels[indexRGBA+1] = videoGrabber.getPixels()[indexRGB+1]; pixels[indexRGBA+2] = videoGrabber.getPixels()[indexRGB+2]; pixels[indexRGBA+3] = 255; pixelIndex++; } } // write the new pixel data into the OpenCL Image (and thus the OpenGL texture) clImage[activeImageIndex].write(pixels); if(doBlur) { msa::OpenCLKernel *kernel = openCL.kernel("msa_boxblur"); for(int i=0; i<blurAmount; i++) { cl_int offset = i * i / 2 + 1; kernel->setArg(0, clImage[activeImageIndex].getCLMem()); kernel->setArg(1, clImage[1-activeImageIndex].getCLMem()); kernel->setArg(2, offset); kernel->run2D(vidWidth, vidHeight); activeImageIndex = 1 - activeImageIndex; } } if(doFlipX) { msa::OpenCLKernel *kernel = openCL.kernel("msa_flipx"); kernel->setArg(0, clImage[activeImageIndex].getCLMem()); kernel->setArg(1, clImage[1-activeImageIndex].getCLMem()); kernel->run2D(vidWidth, vidHeight); activeImageIndex = 1 - activeImageIndex; } if(doFlipY) { msa::OpenCLKernel *kernel = openCL.kernel("msa_flipy"); kernel->setArg(0, clImage[activeImageIndex].getCLMem()); kernel->setArg(1, clImage[1-activeImageIndex].getCLMem()); kernel->run2D(vidWidth, vidHeight); activeImageIndex = 1 - activeImageIndex; } if(doGreyscale) { msa::OpenCLKernel *kernel = openCL.kernel("msa_greyscale"); kernel->setArg(0, clImage[activeImageIndex].getCLMem()); kernel->setArg(1, clImage[1-activeImageIndex].getCLMem()); kernel->run2D(vidWidth, vidHeight); activeImageIndex = 1 - activeImageIndex; } if(doInvert) { msa::OpenCLKernel *kernel = openCL.kernel("msa_invert"); kernel->setArg(0, clImage[activeImageIndex].getCLMem()); kernel->setArg(1, clImage[1-activeImageIndex].getCLMem()); kernel->run2D(vidWidth, vidHeight); activeImageIndex = 1 - activeImageIndex; } if(doThreshold) { msa::OpenCLKernel *kernel = openCL.kernel("msa_threshold"); kernel->setArg(0, clImage[activeImageIndex].getCLMem()); kernel->setArg(1, clImage[1-activeImageIndex].getCLMem()); kernel->setArg(2, threshLevel); kernel->run2D(vidWidth, vidHeight); activeImageIndex = 1 - activeImageIndex; } // calculate capture fps static float lastTime = 0; float nowTime = ofGetElapsedTimef(); float timeDiff = nowTime - lastTime; if(timeDiff > 0 ) captureFPS = 0.9f * captureFPS + 0.1f / timeDiff; lastTime = nowTime; } }
void ofxKinectTracking::drawDepth(int x, int y, int w, int h){ ofSetColor(255, 255, 255); openCL.finish(); #ifndef NORMAL_CAMERA kinect->drawDepth(x,y,w,h); #else clImage[0].draw(x,y,w,h); #endif clImage[1].draw(x+w, y, w,h); ofSetColor(255, 0, 0); ofDrawBitmapString(ofToString(ofGetFrameRate(), 1), ofPoint(20,20)); ofEnableAlphaBlending(); glTranslated(0, 480*2, 0); float scale = 5000.0; { ofSetColor(255, 255, 255,100); double avg = 0; glBegin(GL_LINE_STRIP); for(int i=10;i<totalTime.size();i++){ glVertex2d(i*3, -totalTime[i]*scale); avg += totalTime[i]; } glEnd(); avg /=totalTime.size()-10; ofSetColor(255, 100, 255,255); ofLine(0, -avg*scale, 640*2, -avg*scale); ofDrawBitmapString("Avg: "+ofToString(avg*10000, 2), ofPoint(5,-avg*scale-10)); double percentage = ((avg)/(1/30.0))*100; ofSetColor(255, 255, 255); ofDrawBitmapString("Time percentage of one frame used on openCL: "+ofToString(percentage, 2)+"%", ofPoint(5,-460)); } { ofSetColor(255, 0, 0,150); double avg = 0; glBegin(GL_LINE_STRIP); for(int i=10;i<killingTime.size();i++){ // cout<<totalTime[i]*10000.0<<endl; glVertex2d(i*3, -killingTime[i]*scale); avg += killingTime[i]; } glEnd(); avg /=killingTime.size()-10; ofSetColor(255, 100, 0); ofLine(0, -avg*scale, 640*2, -avg*scale); ofDrawBitmapString("K Avg: "+ofToString(avg*10000, 2), ofPoint(5,-avg*scale-10)); } { ofSetColor(0, 255, 0,150); double avg = 0; glBegin(GL_LINE_STRIP); for(int i=10;i<spawningTime.size();i++){ // cout<<totalTime[i]*10000.0<<endl; glVertex2d(i*3, -spawningTime[i]*scale); avg += spawningTime[i]; } glEnd(); avg /=spawningTime.size()-10; ofSetColor(100, 255, 0); ofLine(0, -avg*scale, 640*2, -avg*scale); ofDrawBitmapString("S Avg: "+ofToString(avg*10000, 2), ofPoint(5,-avg*scale-10)); } { ofSetColor(0, 0, 255,150); double avg = 0; glBegin(GL_LINE_STRIP); for(int i=10;i<updateTime.size();i++){ // cout<<totalTime[i]*10000.0<<endl; glVertex2d(i*3, -updateTime[i]*scale); avg += updateTime[i]; } glEnd(); avg /=updateTime.size()-10; ofSetColor(0, 100, 255); ofLine(0, -avg*scale, 640*2, -avg*scale); ofDrawBitmapString("U Avg: "+ofToString(avg*10000, 2), ofPoint(5,-avg*scale-10)); } }
void ofxKinectTracking::update(){ bool newFrame; #ifndef NORMAL_CAMERA kinect->update(); newFrame = kinect->isFrameNew(); #else videoGrabber.update(); newFrame = videoGrabber.isFrameNew(); if(newFrame){ int pixelIndex = 0; for(int i=0; i<640; i++) { for(int j=0; j<480; j++) { int indexRGB = pixelIndex * 3; int indexL = pixelIndex; pixels[indexL] = videoGrabber.getPixels()[indexRGB+1 ]; pixelIndex++; } } // write the new pixel data into the OpenCL Image (and thus the OpenGL texture) clImage[0].write(pixels); } #endif if(newFrame){ cl_int2 spawnCoord = {int(ofRandom(0, 640)), int(ofRandom(0, 480))}; if(ofRandom(0, 1) < 0.500){ spawnCoord[0] = -1; spawnCoord[0] = -1; } MSA::OpenCLKernel *preKernel = openCL.kernel("preUpdate"); MSA::OpenCLKernel *updateKernel = openCL.kernel("update"); MSA::OpenCLKernel *postKernel = openCL.kernel("postUpdate"); //Prepare timetaking double totalTimeTmp; uint64_t mbeg, mend; openCL.finish(); mbeg = mach_absolute_time(); //Run pre update kernel preKernel->setArg(0, clImage[0].getCLMem()); preKernel->setArg(1, clAntsBuffer.getCLMem()); preKernel->run2D(640, 480); //Calculate time openCL.finish(); mend = mach_absolute_time(); killingTime.push_back(machcore(mend, mbeg)); totalTimeTmp = machcore(mend, mbeg); mbeg = mach_absolute_time(); //Run update kernel size_t shared_size = (1 * 64) * sizeof(int); updateKernel->setArg(0, clAntsBuffer.getCLMem()); updateKernel->setArg(1, clSharedBuffer.getCLMem()); updateKernel->setArg(2, spawnCoord); clSetKernelArg(updateKernel->getCLKernel(), 3, shared_size, NULL); for(int i=0;i<NUM_ITERATIONS;i++){ updateKernel->run2D(640, 480); } //Calculate time openCL.finish(); mend = mach_absolute_time(); spawningTime.push_back(machcore(mend, mbeg)); totalTimeTmp += machcore(mend, mbeg); mbeg = mach_absolute_time(); //Run post update kernel postKernel->setArg(0, clImage[0].getCLMem()); postKernel->setArg(1, clImage[1].getCLMem()); postKernel->setArg(2, clAntsBuffer.getCLMem()); postKernel->setArg(3, clSharedBuffer.getCLMem()); postKernel->setArg(4, spawnCoord); postKernel->run2D(640, 480); //Calculate time openCL.finish(); mend = mach_absolute_time(); updateTime.push_back(machcore(mend, mbeg)); totalTimeTmp += machcore(mend, mbeg); totalTime.push_back(totalTimeTmp); if(totalTime.size() > (640*2)/3){ totalTime.erase(totalTime.begin()); killingTime.erase(killingTime.begin()); spawningTime.erase(spawningTime.begin()); updateTime.erase(updateTime.begin()); } } }
//-------------------------------------------------------------- void testApp::setup(){ ofBackground(0, 0, 0); ofSetLogLevel(OF_LOG_VERBOSE); ofSetVerticalSync(false); #ifdef USE_OPENGL_CONTEXT opencl.setupFromOpenGL(); #else opencl.setup(CL_DEVICE_TYPE_CPU, 2); #endif for(int i=0; i<NUM_PARTICLES; i++) { Particle &p = particles[i]; p.vel.set(0, 0); p.mass = ofRandom(0.5, 1); particlesPos[i].set(ofRandomWidth(), ofRandomHeight()); } glGenBuffersARB(1, vbo); glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo[0]); glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(float2) * NUM_PARTICLES, particlesPos, GL_DYNAMIC_COPY_ARB); glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); opencl.loadProgramFromFile("MSAOpenCL/Particle.cl"); kernelUpdate = opencl.loadKernel("updateParticle"); clMemParticles.initBuffer(sizeof(Particle) * NUM_PARTICLES, CL_MEM_READ_WRITE, particles); #ifdef USE_OPENGL_CONTEXT clMemPosVBO.initFromGLObject(vbo[0]); #else clMemPosVBO.initBuffer(sizeof(Vec2) * NUM_PARTICLES, CL_MEM_READ_WRITE, particlesPos); #endif kernelUpdate->setArg(0, clMemParticles.getCLMem()); kernelUpdate->setArg(1, clMemPosVBO.getCLMem()); kernelUpdate->setArg(2, mousePos); kernelUpdate->setArg(3, dimensions); glPointSize(1); }