.[ ČeskéHry.cz ].

Laboratoř ČeskýchHer.cz - PasteBin

Vložit nový kód

ČeskéHry.cz - KOMUNITA HERNÍCH VÝVOJÁŘŮ

  1. COLLECTION -12 AGE
    2 hod
  2. COLLECTION -12 AGE
    2 hod
  3. COLLECTION -12 AGE
    2 hod
  4. COLLECTION -12 AGE
    2 hod
  5. COLLECTION -12 AGE
    2 hod
  6. COLLECTION -12 AGE
    2 hod
  7. COLLECTION -12 AGE
    2 hod
  8. COLLECTION -12 AGE
    2 hod
  9. COLLECTION -12 AGE
    2 hod
  10. dgfhhd
    4 hod
Link: http://nopaste.ceske-hry.cz/subdom/nopaste222887
Zaslal: nou
Popis: priklad na OpenGL/OpenCL interoperabilitu
Jazyk: C++
Vloženo: 10.2.2010, 10:19
Stáhnout jako soubor
  1. #include <stdlib.h>
  2. #include <SDL/SDL.h>
  3. #include <GL/gl.h>
  4. #include <GL/glu.h>
  5. #include <GL/glext.h>
  6. #include <GL/glx.h>
  7. #include <CL/cl.h>
  8. #include <CL/cl_gl.h>
  9. #include <math.h>
  10. #include <sys/time.h>
  11. #include <iostream>
  12.  
  13. using namespace std;
  14.  
  15. GLXContext gl_context;
  16. Display *display;
  17.  
  18. GLuint vbo;
  19. GLuint indicies;
  20.  
  21. cl_context cl_c;
  22. cl_command_queue queue;
  23. cl_program program;
  24. cl_kernel kernel;
  25. cl_mem cl_m;
  26. cl_mem cl_m2;
  27. timeval now,old,res;
  28.  
  29. const int grid_size = 2048;
  30.  
  31. const char source[] = "#include \"kernel.cl\"";
  32. const char *kernel_source[] = { source };
  33.  
  34. float rott = 0;
  35.  
  36. const int w = 1024;
  37. const int h = 768;
  38.  
  39. void InitGL()
  40. {
  41. glClearColor(0.0f, 0.5f, 0.0f, 0.0f);
  42. glViewport(0,0,w,h);
  43. glClearDepth(1.0f);
  44. glMatrixMode(GL_PROJECTION);
  45. glLoadIdentity();
  46. //glFrustum(-0.5, 500, -0.5, 500.0, 1.0, 100.0);
  47. gluPerspective(40.0f, (float)w/h, 0.1f, 100.0f);
  48. glMatrixMode(GL_MODELVIEW);
  49. glLoadIdentity();
  50.  
  51. glGenBuffers(1, &vbo);
  52. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  53. float *grid = new float[grid_size*grid_size*4];
  54. for(int i=0;i<grid_size;i++)
  55. {
  56. for(int o=0;o<grid_size;o++)
  57. {
  58. grid[i*grid_size*4+o*4] = (float)o/grid_size*10.0-5;
  59. grid[i*grid_size*4+o*4+1] = (float)i/grid_size*10.0-5;
  60. grid[i*grid_size*4+o*4+2] = sin((float)o/grid_size*6.283185308*2)+cos((float)i/grid_size*6.283185308*2);
  61. grid[i*grid_size*4+o*4+3] = 1.0f;
  62. }
  63. }
  64. glBufferData(GL_ARRAY_BUFFER, sizeof(float)*grid_size*grid_size*4, grid, GL_STREAM_DRAW);
  65. delete [] grid;
  66.  
  67. unsigned int *ind = new unsigned int[(grid_size-1)*(grid_size-1)*6];
  68. for(int i=0;i<(grid_size-1);i++)
  69. {
  70. for(int o=0;o<(grid_size-1);o++)
  71. {
  72. ind[i*(grid_size-1)*6+o*6] = i*(grid_size)+o;
  73. ind[i*(grid_size-1)*6+o*6+1] = i*(grid_size)+o+1;
  74. ind[i*(grid_size-1)*6+o*6+2] = (i+1)*(grid_size)+o+1;
  75. ind[i*(grid_size-1)*6+o*6+3] = i*(grid_size)+o;
  76. ind[i*(grid_size-1)*6+o*6+4] = (i+1)*(grid_size)+o+1;
  77. ind[i*(grid_size-1)*6+o*6+5] = (i+1)*(grid_size)+o;
  78. }
  79. }
  80. glGenBuffers(1, &indicies);
  81. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicies);
  82. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*(grid_size-1)*(grid_size-1)*6, ind, GL_STATIC_DRAW);
  83. delete [] ind;
  84. //glPolygonMode(GL_BACK, GL_LINE);
  85.  
  86. printf("GL inicializated %d\n", glGetError());
  87. }
  88.  
  89. void InitCL()
  90. {
  91. cl_int err_code;
  92. cl_platform_id platform;
  93. err_code = clGetPlatformIDs(1, &platform, NULL);
  94. if(err_code)
  95. {
  96. printf("Unable get platform %d\n", err_code);
  97. exit(1);
  98. }
  99. cl_context_properties cl_properties[] = {CL_GL_CONTEXT_KHR, (cl_context_properties)gl_context,
  100. CL_GLX_DISPLAY_KHR, (cl_context_properties)display,
  101. CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0};
  102. cl_c = clCreateContextFromType(cl_properties, CL_DEVICE_TYPE_GPU, NULL, NULL, &err_code);
  103. if(err_code)
  104. {
  105. printf("Unable create context %d\n", err_code);
  106. exit(1);
  107. }
  108.  
  109. cl_device_id device;
  110. clGetContextInfo(cl_c, CL_CONTEXT_DEVICES, sizeof(cl_device_id), &device, NULL);
  111. queue = clCreateCommandQueue(cl_c, device, NULL, &err_code);
  112. if(err_code)
  113. {
  114. printf("Unable create command queue %d\n", err_code);
  115. exit(1);
  116. }
  117. size_t source_len[] = { sizeof(source)-1 };
  118. //const char s[] = {kernel_source , 0};
  119. program = clCreateProgramWithSource(cl_c, 1, kernel_source, source_len, &err_code);
  120. if(err_code)
  121. {
  122. printf("Unable create program %d\n", err_code);
  123. exit(1);
  124. }
  125. err_code = clBuildProgram(program, 1, &device, "-I.", NULL, NULL);
  126. if(err_code)
  127. {
  128. printf("Unable build program %d\n", err_code);
  129. }
  130. char log[10000];
  131. clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 10000, log, NULL);
  132. printf("BUILD LOG:\n%s", log);
  133. if(err_code)exit(0);
  134. kernel = clCreateKernel(program, "Wave", &err_code);
  135. if(err_code)
  136. {
  137. printf("Unable create kernel %d\n", err_code);
  138. }
  139. size_t size;
  140. clGetKernelWorkGroupInfo(kernel, device, CL_KERNEL_WORK_GROUP_SIZE, sizeof(size_t), &size, NULL);
  141. cout << "kernel work group size: " << size << endl;
  142. clGetKernelWorkGroupInfo(kernel, device, CL_KERNEL_COMPILE_WORK_GROUP_SIZE, sizeof(size_t), &size, NULL);
  143. cout << "kernel compile work group size: " << size << endl;
  144.  
  145. cl_m2 = clCreateBuffer(cl_c, CL_MEM_READ_WRITE, sizeof(float)*grid_size*grid_size*4, NULL, &err_code);
  146. if(err_code)
  147. {
  148. printf("Unable create buffer %d\n", err_code);
  149. exit(1);
  150. }
  151.  
  152. }
  153.  
  154. void RunCL()
  155. {
  156. cl_int err_code;
  157. glFinish();
  158. err_code = clEnqueueAcquireGLObjects(queue, 1, &cl_m, 0, NULL, NULL);
  159. if(err_code)printf("Unable acquire object %d\n", err_code);
  160. err_code = clSetKernelArg(kernel, 0, sizeof(float), &rott);
  161. err_code |= clSetKernelArg(kernel, 1, sizeof(int), &grid_size);
  162. err_code |= clSetKernelArg(kernel, 2, sizeof(cl_mem), &cl_m);
  163. if(err_code)printf("Unable set arguments %d\n", err_code);
  164.  
  165. size_t work[] = {grid_size, grid_size};
  166. clFinish(queue);
  167. err_code = clEnqueueNDRangeKernel(queue, kernel, 2, NULL, work, NULL, 0, NULL, NULL);
  168. //clFinish(queue);
  169.  
  170. if(err_code)
  171. {
  172. printf("Unable run a kernel %d\n", err_code);
  173. exit(1);
  174. }
  175. err_code = clEnqueueReleaseGLObjects(queue, 1, &cl_m, 0, NULL, NULL);
  176. if(err_code)printf("Unable release object %d\n", err_code);
  177. clFinish(queue);
  178. gettimeofday(&now,NULL);
  179. timersub(&now, &old,&res);
  180. old = now;
  181. //printf("Cas %f\n",res.tv_sec + res.tv_usec/1000000.0f);;
  182. }
  183.  
  184. void DrawGL()
  185. {
  186. static int last_time = SDL_GetTicks();
  187. static int fps = 0;
  188. if(SDL_GetTicks() - last_time > 1000)
  189. {
  190. cout << "FPS: " << fps << endl;
  191. fps = 0;
  192. last_time = SDL_GetTicks();
  193. }
  194. fps++;
  195. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  196. glLoadIdentity();
  197. glTranslatef( 0.0f, 0.0f, -20.0f );
  198. glRotatef(rott, 1.0f, 0.0f, 0.0f);
  199. rott += 0.4f;
  200.  
  201. /*glBegin(GL_QUADS);
  202. glVertex3f(0.0f, 0.0f, 0.0f);
  203. glVertex3f(5.0f, 0.0f, 0.0f);
  204. glVertex3f(5.0f, 5.0f, 0.0f);
  205. glVertex3f(0.0f, 5.0f, 0.0f);
  206. glEnd();*/
  207.  
  208. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicies);
  209. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  210. glEnableClientState(GL_VERTEX_ARRAY);
  211. glVertexPointer(4, GL_FLOAT, 0, 0);
  212. glDrawElements(GL_TRIANGLES, (grid_size-1)*(grid_size-1)*6, GL_UNSIGNED_INT, NULL);
  213. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  214. glBindBuffer(GL_ARRAY_BUFFER, 0);
  215. glDisableClientState(GL_VERTEX_ARRAY);
  216. //printf("%d\n", glGetError());
  217. SDL_GL_SwapBuffers();
  218. //glFinish();
  219. }
  220.  
  221. int main ( int argc, char** argv )
  222. {
  223. // initialize SDL video
  224. if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  225. {
  226. printf( "Unable to init SDL: %s\n", SDL_GetError() );
  227. return 1;
  228. }
  229.  
  230. // make sure SDL cleans up before exit
  231. atexit(SDL_Quit);
  232.  
  233. // create a new window
  234. //SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
  235. //SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);
  236. SDL_Surface* screen = SDL_SetVideoMode(w, h, 32, SDL_OPENGL|SDL_GL_DOUBLEBUFFER|SDL_HWSURFACE);
  237. if ( !screen )
  238. {
  239. printf("Unable to set 640x480 video: %s\n", SDL_GetError());
  240. return 1;
  241. }
  242. SDL_WM_SetCaption("OpenCL - OpenGL interoperability example", NULL);
  243.  
  244. GLint tex_size;
  245. glGetIntegerv(GL_MAX_TEXTURE_SIZE, &tex_size);
  246. cout << "Maximum texture size " << tex_size << endl;
  247.  
  248. gl_context = glXGetCurrentContext();
  249. display = glXGetCurrentDisplay();
  250.  
  251. InitCL();
  252. InitGL();
  253.  
  254. cl_int err_code;
  255. cl_m = clCreateFromGLBuffer(cl_c, CL_MEM_READ_WRITE, vbo, &err_code);
  256. size_t size;
  257. clGetMemObjectInfo(cl_m, CL_MEM_SIZE, sizeof(size_t), &size, NULL);
  258.  
  259. cout << cl_m << " " << size/1024/1024 << " MiB" << endl;
  260. if(err_code)
  261. {
  262. printf("Unable share GL object %d\n", err_code);
  263. }
  264.  
  265. // program main loop
  266. bool done = false;
  267. while (!done)
  268. {
  269. // message processing loop
  270. SDL_Event event;
  271. while (SDL_PollEvent(&event))
  272. {
  273. // check for messages
  274. switch (event.type)
  275. {
  276. // exit if the window is closed
  277. case SDL_QUIT:
  278. done = true;
  279. cout << "quit" << endl;
  280. break;
  281.  
  282. // check for keypresses
  283. case SDL_KEYDOWN:
  284. {
  285. if(event.key.keysym.sym == SDLK_r)
  286. {
  287. RunCL();
  288. }
  289. // exit if ESCAPE is pressed
  290. if (event.key.keysym.sym == SDLK_ESCAPE)
  291. done = true;
  292. break;
  293. }
  294. } // end switch
  295. } // end of message processing
  296.  
  297. DrawGL();
  298. //RunCL();
  299. //SDL_Delay(10);
  300. } // end main loop
  301.  
  302. return 0;
  303. }
  304.  
  305. /*********************************
  306. kernel.cl
  307. *********************************/
  308. //#pragma OPENCL EXTENSION cl_khr_gl_sharing : enable
  309. __kernel void Wave(float time, int grid_size, __global float *data)
  310. {
  311.  
  312. __local int ll[128];
  313. int i = get_global_id(0);
  314. int o = get_global_id(1);
  315. 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);;
  316. //hej[0] = data[o*grid_size*4+i*4+2] ;
  317. }
  318.