Exisiting CUDA OpenGL: How to use it with CPU instead of GPU?

Hi,

I’m a absolute newbie to OpenGL. I’ve found a CUDA mandelbrot set example where OpenGL is used to show it on the display.

I now wanted to program the same application but without use of CUDA. The sense is that I want to switch between CPU and GPU while executing.

But I don’t know how to transform the existing CUDA OpenGL instruction in that way, that I can use it for my CPU calculation.

The buffer and texture are initialised as following:

GLint bsize;  

	

glGenBuffers(1, &buffer);

glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);

glBufferData(GL_PIXEL_UNPACK_BUFFER,Bpp * sizeof(unsigned char) * width * height, NULL, GL_STREAM_DRAW);	

glGetBufferParameteriv(GL_PIXEL_UNPACK_BUFFER, GL_BUFFER_SIZE, &bsize); 

	

if ((GLuint)bsize != (Bpp * sizeof(unsigned char) * width * height)) 

	{

		printf("Buffer object (%d) has incorrect size (%d).\n",(unsigned)buffer, (unsigned)bsize);

		cudaThreadExit();

		exit(-1);

	}

glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);  

cudaGLRegisterBufferObject(buffer);

glGenTextures(1, &texid); 

glBindTexture(GL_TEXTURE_2D, texid);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height,  0, GL_RGB, GL_UNSIGNED_BYTE, NULL);  

glBindTexture(GL_TEXTURE_2D, 0);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glPixelStorei(GL_PACK_ALIGNMENT, 1);

Now Cuda uses it in the following way:

cudaGLMapBufferObject((void**)&data, buffer);

	

// Calling of the CUDA Hostfunction

	calculateHost( data, centerX, centerY, zoom, width, height );

	

cudaGLUnmapBufferObject(buffer);

data is an pointer to unsigned char

I want to give my CPUfunction the same Parameters as the CUDA hostfunktion (unsigned char* data, centerX, centerY, zoom, width, height )

But I don’t know how the code it. :( As far as I can say it the variable data is the problem because it needs to be a pointer to unsigned char.

If I understand well your problem, you want to do the calculation on the CPU and just use OpenGL for the display of the result ?
If yes, you can keep the whole structure of the mandelbrot example.
You make your calculation on the CPU, you copy the array on the GPU by using CUDA and you call cudaGLMapBufferObject() as written in the source code.

You understood it right. But how do you mean “copy the array on the CPU”? From where do I copy it?

Sorry, I meant to say “copy the array on the GPU” instead of “copy the array on the CPU”.
But - if I remember well - you have the possibility to do the calculation on the CPU - with the MandelbrotGold.cpp - and display the result with OpenGL. To do that, you just have to look for a variable called gRunCPU and put it at true.