#include #include #include #include #include #include #include #include #include #include #include using namespace std; GLXContext gl_context; Display *display; GLuint vbo; GLuint indicies; cl_context cl_c; cl_command_queue queue; cl_program program; cl_kernel kernel; cl_mem cl_m; cl_mem cl_m2; timeval now,old,res; const int grid_size = 2048; const char source[] = "#include \"kernel.cl\""; const char *kernel_source[] = { source }; float rott = 0; const int w = 1024; const int h = 768; void InitGL() { glClearColor(0.0f, 0.5f, 0.0f, 0.0f); glViewport(0,0,w,h); glClearDepth(1.0f); glMatrixMode(GL_PROJECTION); glLoadIdentity(); //glFrustum(-0.5, 500, -0.5, 500.0, 1.0, 100.0); gluPerspective(40.0f, (float)w/h, 0.1f, 100.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); float *grid = new float[grid_size*grid_size*4]; for(int i=0;i 1000) { cout << "FPS: " << fps << endl; fps = 0; last_time = SDL_GetTicks(); } fps++; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef( 0.0f, 0.0f, -20.0f ); glRotatef(rott, 1.0f, 0.0f, 0.0f); rott += 0.4f; /*glBegin(GL_QUADS); glVertex3f(0.0f, 0.0f, 0.0f); glVertex3f(5.0f, 0.0f, 0.0f); glVertex3f(5.0f, 5.0f, 0.0f); glVertex3f(0.0f, 5.0f, 0.0f); glEnd();*/ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicies); glBindBuffer(GL_ARRAY_BUFFER, vbo); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(4, GL_FLOAT, 0, 0); glDrawElements(GL_TRIANGLES, (grid_size-1)*(grid_size-1)*6, GL_UNSIGNED_INT, NULL); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glDisableClientState(GL_VERTEX_ARRAY); //printf("%d\n", glGetError()); SDL_GL_SwapBuffers(); //glFinish(); } int main ( int argc, char** argv ) { // initialize SDL video if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { printf( "Unable to init SDL: %s\n", SDL_GetError() ); return 1; } // make sure SDL cleans up before exit atexit(SDL_Quit); // create a new window //SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); //SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8); SDL_Surface* screen = SDL_SetVideoMode(w, h, 32, SDL_OPENGL|SDL_GL_DOUBLEBUFFER|SDL_HWSURFACE); if ( !screen ) { printf("Unable to set 640x480 video: %s\n", SDL_GetError()); return 1; } SDL_WM_SetCaption("OpenCL - OpenGL interoperability example", NULL); GLint tex_size; glGetIntegerv(GL_MAX_TEXTURE_SIZE, &tex_size); cout << "Maximum texture size " << tex_size << endl; gl_context = glXGetCurrentContext(); display = glXGetCurrentDisplay(); InitCL(); InitGL(); cl_int err_code; cl_m = clCreateFromGLBuffer(cl_c, CL_MEM_READ_WRITE, vbo, &err_code); size_t size; clGetMemObjectInfo(cl_m, CL_MEM_SIZE, sizeof(size_t), &size, NULL); cout << cl_m << " " << size/1024/1024 << " MiB" << endl; if(err_code) { printf("Unable share GL object %d\n", err_code); } // program main loop bool done = false; while (!done) { // message processing loop SDL_Event event; while (SDL_PollEvent(&event)) { // check for messages switch (event.type) { // exit if the window is closed case SDL_QUIT: done = true; cout << "quit" << endl; break; // check for keypresses case SDL_KEYDOWN: { if(event.key.keysym.sym == SDLK_r) { RunCL(); } // exit if ESCAPE is pressed if (event.key.keysym.sym == SDLK_ESCAPE) done = true; break; } } // end switch } // end of message processing DrawGL(); //RunCL(); //SDL_Delay(10); } // end main loop return 0; } /********************************* kernel.cl *********************************/ //#pragma OPENCL EXTENSION cl_khr_gl_sharing : enable __kernel void Wave(float time, int grid_size, __global float *data) { __local int ll[128]; int i = get_global_id(0); int o = get_global_id(1); data[o*grid_size*4+i*4+2] = sin((float)o/grid_size*6.283185308f*2+time/4)+cos((float)i/grid_size*6.283185308f*2+time/4);; //hej[0] = data[o*grid_size*4+i*4+2] ; }