Exemplo n.º 1
0
void InitCL()
{
  int i;
  cl_platform_id myplatform;
  cl_device_id *mydevice;
  cl_int err;
  char* oclsource; 
  size_t program_length;
  unsigned int gpudevcount;

  err = RGUGetPlatformID(&myplatform);

  err = clGetDeviceIDs(myplatform,CL_DEVICE_TYPE_GPU,0,NULL,&gpudevcount);
  mydevice = new cl_device_id[gpudevcount];
  err = clGetDeviceIDs(myplatform,CL_DEVICE_TYPE_GPU,gpudevcount,mydevice,NULL);

  // You need all these to get full interoperability with OpenGL:
  cl_context_properties props[] = {
    CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(),
    CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(),
    CL_CONTEXT_PLATFORM, (cl_context_properties)myplatform,
    0};

  mycontext = clCreateContext(props,1,&mydevice[0],NULL,NULL,&err);
  mycommandqueue = clCreateCommandQueue(mycontext,mydevice[0],0,&err);

  oclsource = RGULoadProgSource("particle_system.cl", "", &program_length);
  myprogram = clCreateProgramWithSource(mycontext,1,(const char **)&oclsource,
					&program_length, &err);
  if(err==CL_SUCCESS) fprintf(stderr,"create ok\n");
  else fprintf(stderr,"create err %d\n",err);
  clBuildProgram(myprogram, 0, NULL, NULL, NULL, NULL);
  mykernel = clCreateKernel(myprogram, "VVerlet", &err);
  if(err==CL_SUCCESS) fprintf(stderr,"build ok\n");
  else fprintf(stderr,"build err %d\n",err);
  glBindBuffer(GL_ARRAY_BUFFER, OGL_VBO);
  glBufferData(GL_ARRAY_BUFFER, DATA_SIZE, &host_position[0][0], GL_DYNAMIC_DRAW);
  oclvbo = clCreateFromGLBuffer(mycontext,CL_MEM_WRITE_ONLY,OGL_VBO,&err);

  dev_velocity = clCreateBuffer(mycontext,CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR,
				DATA_SIZE,&host_velocity[0][0],&err); 

  dev_rseed = clCreateBuffer(mycontext,CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR,
			     NUMBER_OF_PARTICLES*sizeof(float),&host_rseed[0],&err);

  oclcbo = clCreateBuffer(mycontext,CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR,
          DATA_SIZE,&host_color[0],&err);

  clSetKernelArg(mykernel,0,sizeof(cl_mem),(void *)&oclvbo);
  clSetKernelArg(mykernel,1,sizeof(cl_mem),(void *)&dev_velocity);
  clSetKernelArg(mykernel,2,sizeof(cl_mem),(void *)&dev_rseed);
  clSetKernelArg(mykernel,3,sizeof(cl_mem),(void *)&oclcbo);
}
Exemplo n.º 2
0
void initCL()
{
	size_t program_length;
	const char *header;
	int err;
	unsigned int gpudevcount;

	err = RGUGetPlatformID(&myplatform);

	err = clGetDeviceIDs(myplatform,CL_DEVICE_TYPE_GPU,0,NULL,&gpudevcount);
	fprintf(stderr,"device count %d\n",gpudevcount);

	mydevice = new cl_device_id[gpudevcount];
	err = clGetDeviceIDs(myplatform,CL_DEVICE_TYPE_GPU,gpudevcount,mydevice,NULL);

	cl_context_properties props[] = {
		CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(),
		CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(),
		CL_CONTEXT_PLATFORM, (cl_context_properties)myplatform, 
		0};
	mycontext = clCreateContext(props,1,&mydevice[0],NULL,NULL,&err);
	fprintf(stderr,"context creation %d\n",err);

	mycq = clCreateCommandQueue(mycontext,mydevice[0],0,&err);
	fprintf(stderr,"command queue %d\n",err);

	header = RGULoadProgSource("wave8.h","",&program_length);
	oclsource = RGULoadProgSource("wave8.cl",header,&program_length);

	myprogram = clCreateProgramWithSource(mycontext,1,
		(const char **)&oclsource, &program_length, &err);
	fprintf(stderr,"create program %d\n",err);

	clBuildProgram(myprogram, 0, NULL, NULL, NULL, NULL);


	/*
	 * It would be nice to know why the build failed instead of an obscure integer
	 * error code.
	 */
	mykrn_update = clCreateKernel(myprogram, "update", &err);
	if(err==CL_SUCCESS) fprintf(stderr,"kernel build update ok\n");
	else {
		fprintf(stderr,"kernel build err update %d\n",err);
		size_t length;
	    char buffer[8192];
	    clGetProgramBuildInfo(myprogram, mydevice[0], CL_PROGRAM_BUILD_LOG,
	    	sizeof(buffer), buffer, &length);
	    printf("%s\n",buffer);
	}

	mykrn_heights = clCreateKernel(myprogram, "heights", &err);
	if(err==CL_SUCCESS) fprintf(stderr,"kernel build heights ok\n");
	else {
		fprintf(stderr,"kernel build err heights %d\n",err);
		size_t length;
	    char buffer[8192];
	    clGetProgramBuildInfo(myprogram, mydevice[0], CL_PROGRAM_BUILD_LOG,
	    	sizeof(buffer), buffer, &length);
	    printf("%s\n",buffer);
	}

	mykrn_normals = clCreateKernel(myprogram, "normals", &err);
	if(err==CL_SUCCESS) fprintf(stderr,"kernel build normals ok\n");
	else {
		fprintf(stderr,"kernel build err normals %d\n",err);
		size_t length;
	    char buffer[8192];
	    clGetProgramBuildInfo(myprogram, mydevice[0], CL_PROGRAM_BUILD_LOG,
	    	sizeof(buffer), buffer, &length);
	    printf("%s\n",buffer);
	}

	mykrn_colors = clCreateKernel(myprogram, "colors", &err);
	if(err==CL_SUCCESS) fprintf(stderr,"kernel build colors ok\n");
	else {
		fprintf(stderr,"kernel build err colors %d\n",err);
		size_t length;
	    char buffer[8192];
	    clGetProgramBuildInfo(myprogram, mydevice[0], CL_PROGRAM_BUILD_LOG,
	    	sizeof(buffer), buffer, &length);
	    printf("%s\n",buffer);
	    exit(0);
	}
}