OpenGL cuda textures

First of all anyone who knows how to solve this please email-me

Im trying to convert a VBOs code, that works on the CPU perfectly. In this version I don’t use vertex mapping but I also have that version.

  1. This only works for the vertex and colors data in CUDA.

  2. The cuda and CPU code follows.

  3. Sugestions why it doens’t work (and improvements to the cuda and CPU always welcomed)?

static GLsizeiptr WorldPositionSize = 4 * 2 * sizeof(GLfloat);

static GLfloat WorldPositionData[] =

{

	 0.0f, 0.0f, 

	 0.0f, 1.0f, 

	 1.0f, 1.0f, 

	 1.0f, 0.0f

};

static GLsizeiptr WorldTexcoordsSize = 4 * 2 * sizeof(GLfloat);

static GLfloat WorldTexcoordsData[] =

{	

	1.0f, 1.0f, 

	1.0f, 0.0f, 

	0.0f, 0.0f, 

	0.0f, 1.0f

};

static GLsizeiptr WorldColorSize = 4 * 4 * sizeof(GLfloat);

static GLfloat WorldColorData[] =

{

	1.0f, 1.0f, 1.0f, 1.0f, 

	1.0f, 1.0f, 1.0f, 1.0f, 

	1.0f, 1.0f, 1.0f, 1.0f, 

	1.0f, 1.0f, 1.0f, 1.0f

};

static const GLsizei WorldVertexCount = 4; 

static const int BufferSize = 3;

static GLuint Buffer[3];

enum

{

	POSITION_OBJECT = 0, 

	COLOR_OBJECT = 1, 

	TEXCOORD_OBJECT = 2

};

void draw_world(void) {

glPushMatrix();

glBindTexture(GL_TEXTURE_2D, world_texture); //bind our texture to our shape

	

_draw_vbo(WorldPositionSize, WorldColorSize, WorldTexcoordsSize, 

	WorldPositionData, WorldColorData , WorldTexcoordsData, WorldVertexCount, GL_QUADS);

	

glPopMatrix();

}

void _draw_vbo(GLsizeiptr PositionSize, GLsizeiptr ColorSize, GLsizeiptr TexcoordSize, 

				GLfloat* PositionData, GLfloat* ColorData , GLfloat* TexcoordData, 

				GLsizei VertexCount, GLenum DrawPrimitive) {

	// bind VBO in order to use

	glBindBuffer(GL_ARRAY_BUFFER, Buffer);

	// upload data to VBO

	glBufferData(GL_ARRAY_BUFFER, ColorSize, ColorData, GL_STREAM_DRAW);

	glColorPointer(4, GL_FLOAT, 0, 0);

	// bind VBO in order to use

	glBindBuffer(GL_ARRAY_BUFFER, Buffer[POSITION_OBJECT]);

	// upload data to VBO

	glBufferData(GL_ARRAY_BUFFER, PositionSize, PositionData, GL_STREAM_DRAW);

	glVertexPointer(2, GL_FLOAT, 0, 0);

	 // bind VBO in order to use

	glBindBuffer(GL_ARRAY_BUFFER, Buffer[TEXCOORD_OBJECT]);	

	// upload data to VBO

	glBufferData(GL_ARRAY_BUFFER, TexcoordSize, TexcoordData, GL_STREAM_DRAW);

	glTexCoordPointer(2, GL_FLOAT, 0, 0);

	glEnableClientState(GL_VERTEX_ARRAY);

	glEnableClientState(GL_COLOR_ARRAY);

	glEnableClientState(GL_TEXTURE_COORD_ARRAY);

	glDrawArrays(DrawPrimitive, 0, VertexCount);

	 glDisableClientState(GL_TEXTURE_COORD_ARRAY);

	glDisableClientState(GL_COLOR_ARRAY);

	glDisableClientState(GL_VERTEX_ARRAY);

}

int load_texture(const char *filename, int width, int height)

{

	GLuint texture;

	unsigned char *data;

	FILE * file;

	//The following code will read in our RAW file

	fopen_s(&file, filename, "rb");

	if(file == NULL ) 

		return 0;

	data =(unsigned char *)malloc(width * height * 3);

	fread(data, width * height * 3, 1, file);

	fclose(file);

	glGenTextures(1, &texture); //generate the texture with the loaded data

	glBindTexture(GL_TEXTURE_2D, texture); //bind the texture to it's array

	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); //set texture environment parameters

	// when texture area is small, bilinear filter the closest mipmap

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

	// when texture area is large, bilinear filter the first mipmap

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	//the texture wraps over at the edges(repeat)

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

	// build our texture mipmaps

	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);

	free(data); //free the texture

	return texture; //return whether it was successfull

}

//free a given texture

void free_texture(GLuint texture )

{

	glDeleteTextures(1, &texture);

}
extern "C" void _draw_world(_GRID *g) {

	unsigned int worldVertexCount = 4;

	unsigned int worldColorSize = worldVertexCount * sizeof(float4);

	unsigned int worldPositionSize = worldVertexCount * sizeof(float4);

	// bind VBO in order to use

	glBindBuffer(GL_ARRAY_BUFFER, Buffer[POSITION_OBJECT]);

	// upload data to VBO

	glBufferData(GL_ARRAY_BUFFER, worldPositionSize, 0, GL_STREAM_DRAW);

	glBindBuffer(GL_ARRAY_BUFFER, 0);

	// register buffer object with CUDA

	CUDA_SAFE_CALL(cudaGLRegisterBufferObject(Buffer[POSITION_OBJECT]));

	CUT_CHECK_ERROR("cudaGLRegisterBufferObject execution failed");

	// bind VBO data to set up

	glBindBuffer(GL_ARRAY_BUFFER, Buffer);

	// upload data to VBO

	glBufferData(GL_ARRAY_BUFFER, worldColorSize, 0, GL_STREAM_DRAW);

	glBindBuffer(GL_ARRAY_BUFFER, 0);

	// register buffer object with CUDA

	CUDA_SAFE_CALL(cudaGLRegisterBufferObject(Buffer));

	CUT_CHECK_ERROR("cudaGLRegisterBufferObject execution failed");

	// bind VBO in order to use

	glBindBuffer(GL_ARRAY_BUFFER, Buffer[TEXCOORD_OBJECT]);

	// upload data to VBO

	glBufferData(GL_ARRAY_BUFFER, worldPositionSize, 0, GL_STREAM_DRAW);

	glBindBuffer(GL_ARRAY_BUFFER, 0);

	// register buffer object with CUDA

	CUDA_SAFE_CALL(cudaGLRegisterBufferObject(Buffer[TEXCOORD_OBJECT]));

	CUT_CHECK_ERROR("cudaGLRegisterBufferObject execution failed");

	// map OpenGL buffer object for writing from CUDA

	CUDA_SAFE_CALL(cudaGLMapBufferObject((void**)&vbo_pos, Buffer[POSITION_OBJECT]));

	CUT_CHECK_ERROR("cudaGLMapBufferObject execution failed");

	// map OpenGL buffer object for writing from CUDA

	CUDA_SAFE_CALL(cudaGLMapBufferObject((void**)&vbo_clr, Buffer));

	CUT_CHECK_ERROR("cudaGLMapBufferObject execution failed");

	// map OpenGL buffer object for writing from CUDA

	CUDA_SAFE_CALL(cudaGLMapBufferObject((void**)&vbo_tex, Buffer[TEXCOORD_OBJECT]));

	CUT_CHECK_ERROR("cudaGLMapBufferObject execution failed");

	clr = make_float4(1.0f, 1.0f, 1.0f, 1.0f);

	_set_world_vbo_data<<<1, 4>>>(vbo_pos, vbo_clr, vbo_pos, clr);

	// check if kernel execution generated an error

	CUT_CHECK_ERROR("Kernel _set_world_vbo_data execution failed");

	// unmap buffer object

	CUDA_SAFE_CALL(cudaGLUnmapBufferObject(Buffer[POSITION_OBJECT]));

	CUT_CHECK_ERROR("cudaGLUnmapBufferObject execution failed");

	// unmap buffer object

	CUDA_SAFE_CALL(cudaGLUnmapBufferObject(Buffer));

	CUT_CHECK_ERROR("cudaGLUnmapBufferObject execution failed");

	// unmap buffer object

	CUDA_SAFE_CALL(cudaGLUnmapBufferObject(Buffer[TEXCOORD_OBJECT]));

	CUT_CHECK_ERROR("cudaGLUnmapBufferObject execution failed");

	// bind to VBO position pointer

	glBindBuffer(GL_ARRAY_BUFFER, Buffer[POSITION_OBJECT]);

	glVertexPointer(4, GL_FLOAT, 0, 0);

	

	// bind to VBO color pointer

	   glBindBuffer(GL_ARRAY_BUFFER, Buffer);

	glColorPointer(4, GL_FLOAT, 0, 0);

	// bind to VBO texcoord pointer

		glBindBuffer(GL_ARRAY_BUFFER, Buffer[TEXCOORD_OBJECT]);

	glTexCoordPointer(4, GL_FLOAT, 0, 0);

	// register/map/and set pointers on vbo data

	//_set_vbo_CUDA(g, worldPositionSize, worldColorSize, worldPositionSize, 0, 0, 1.0f); 

	

	// draw vbos

	glPushMatrix();

	glEnable(GL_TEXTURE_2D); //enable texturing

	glBindTexture(GL_TEXTURE_2D, world_texture); //bind our texture to our shape

	_draw_vbo(GL_QUADS, worldVertexCount);

	glDisable(GL_TEXTURE_2D);

	glPopMatrix();

	// unregister vbos in cuda

	_unregister_vbo_CUDA(); 

}

extern "C" void _unregister_vbo_CUDA(void) 

{

	// unregister buffer object with CUDA

	CUDA_SAFE_CALL(cudaGLUnregisterBufferObject(Buffer[POSITION_OBJECT]));

	CUT_CHECK_ERROR("cudaGLUnregisterBufferObject execution failed");

	

	// unregister buffer object with CUDA

	CUDA_SAFE_CALL(cudaGLUnregisterBufferObject(Buffer));

	CUT_CHECK_ERROR("cudaGLUnregisterBufferObject execution failed");

	// unregister buffer object with CUDA

	CUDA_SAFE_CALL(cudaGLUnregisterBufferObject(Buffer[TEXCOORD_OBJECT]));

	CUT_CHECK_ERROR("cudaGLUnregisterBufferObject execution failed");

}

extern "C" void _draw_vbo(GLenum DrawPrimitive, GLsizei VertexCount) 

{

	glEnableClientState(GL_VERTEX_ARRAY);

	glEnableClientState(GL_COLOR_ARRAY);

	glEnableClientState(GL_TEXTURE_COORD_ARRAY);

	glDrawArrays(DrawPrimitive, 0, VertexCount);

	glDisableClientState(GL_TEXTURE_COORD_ARRAY);

	glDisableClientState(GL_COLOR_ARRAY);

	glDisableClientState(GL_VERTEX_ARRAY);

}

int load_texture(const char *filename, int width, int height)

{

	GLuint texture;

	unsigned char *data;

	FILE * file;

	//The following code will read in our RAW file

	fopen_s(&file, filename, "rb");

	if(file == NULL ) 

		return 0;

	data =(unsigned char *)malloc(width * height * 3);

	fread(data, width * height * 3, 1, file);

	fclose(file);

	glGenTextures(1, &texture); //generate the texture with the loaded data

	glBindTexture(GL_TEXTURE_2D, texture); //bind the texture to it's array

	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); //set texture environment parameters

	// when texture area is small, bilinear filter the closest mipmap

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

	// when texture area is large, bilinear filter the first mipmap

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	//the texture wraps over at the edges(repeat)

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

	// build our texture mipmaps

	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);

	free(data); //free the texture

	return texture; //return whether it was successfull

}

//free a given texture

void free_texture(GLuint texture )

{

	glDeleteTextures(1, &texture);

}