Open GL error with libraries

Hello Im currently using 9800GT and 9500GT in widows platform. Im using Visual Studio 2008 for programming. I have solved the problem regarding multithreaded programming libraries my ignoring libcmt.lib (a default library). However, even if I have plugged in every directories for additional dependencies, program still shots errors bellow.

1>Resampling_GL.obj : error LNK2019: unresolved external symbol _glutPostRedisplay referenced in function “void __cdecl display(void)” (?display@@YAXXZ)
1>Resampling_GL.obj : error LNK2019: unresolved external symbol _glutSwapBuffers referenced in function “void __cdecl display(void)” (?display@@YAXXZ)
1>Resampling_GL.obj : error LNK2019: unresolved external symbol _glutMainLoop referenced in function _main
1>Resampling_GL.obj : error LNK2019: unresolved external symbol _glutDisplayFunc referenced in function _main

Can somebody help me with this? My short experience make me impossible to solve the problem. Thank you

Oh actually I have changed the header file from freeglut.h to glut.h and the problem above was solved. However program stopped running and according to debug, it seems like there is hardward language problem. So Im not sure if using glut.h instead of freeglut.h is okay. Can somebody help me with this? Thank you.

glut was originally written back in the 90’s, and its author stopped developing it over a decade ago. Because of licensing issues, no one but the author could add or build code off of it. Consequently, freeglut was made as a fully open source alternative. Freeglut adds some new functionality, and has complete backward compatibility with glut (with the exception of a few functions, but you weren’t using any of those). I suspect you were including the directory with the glut.h header in your project, but not the one with freeglut.h (when installing CUDA, they’re in different directories IIRC).

You can use glut instead of freeglut, and that’s fine - you’ll just be missing out on some useful function, such as glutMainLoopEvent(), and instead will be stuck with using glut’s draconian glutMainLoop(). What was your hardware language problem? If I had to hazard a guess, it’s probably a consequence of trying to use CUDA and OpenGL interoperability, as that can be a bit tricky.

This is the error message when I run the program using debuging

st-chance exception at 0x75639673 in Resampling_GL.exe: Microsoft C++ exception: cudaError_enum at memory location 0x002df858…
First-chance exception at 0x75639673 in Resampling_GL.exe: Microsoft C++ exception: cudaError_enum at memory location 0x002df854…
First-chance exception at 0x75639673 in Resampling_GL.exe: Microsoft C++ exception: cudaError at memory location 0x002df8a0…
First-chance exception at 0x75639673 in Resampling_GL.exe: Microsoft C++ exception: cudaError_enum at memory location 0x002df8ac…
First-chance exception at 0x75639673 in Resampling_GL.exe: Microsoft C++ exception: cudaError at memory location 0x002df8e0…

and couple of theses

‘Resampling_GL.exe’: Loaded ‘C:\Users\RETINALOCT2\Desktop\Jay\OCTProcessor\gpu_testers\Resampling_GL\Resampling_GL\freeglut.dll’, Binary was not built with debug information.

Is anybody familiar with this? I have no idea how to fix it… Thank you for helping me out. There has been wonderful improvements. Thank you.

// includes, system

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

// includes, GL

#include <GL/glew.h>

//#include <GL/freeglut.h>

#include <GL/glut.h>

// includes CUDA <--> C++ interlops 

#include <cuda_runtime.h>

#include <cutil_inline.h>

#include <cutil_gl_inline.h>

#include <cutil_gl_error.h>

#include <cuda_gl_interop.h>

//define variables

#define XDIM 1000 //original value 1000

#define YDIM 1024 //original value 1024

#define FRAMES 20

#define START_WAVELENGTH 7.5226E+02

#define WAVELENGTH_SPACING 8.6969E-02	

#define SECOND_ORDER_CORRECTION -4.2264E-06

#define THIRD_ORDER_CORRECTION 8.9511E-10

#define FOURTH_ORDER_CORRECTION -1.7987E-13

#define LINE_LENGTH 1024	//Note same as YDIM, should be fixed

#define CAPTURE_LINE_OFFSET 800

#define RESAMPLE_POINTS 1024

// variables into cuda

unsigned char a[XDIM*XDIM*YDIM];

float resamp_1D[LINE_LENGTH*2], original_cpu[FRAMES*XDIM*YDIM], *dev_original;

float k_resampledspacing;

cuComplex *d_in;

float *dev_k_resampledspacing, *dc_subtracted;

int *dev_frame, *dev_sum;

float *dev_resamp, *dev_b;

// pbo variables

GLuint pbo;

struct cudaGraphicsResource *cuda_pbo_resource;

char inputfile[] = "joecornearadialvolume.unp";

extern "C"

void launch_kernel(int* sum, float* original_data, int* frame_num, cuComplex* d, float* k_resamp, float* dc_subtracted, float* resample, float4 *ptr);

// Read Data from file

void readData() {

	FILE *fp;

	long size;

	fp=fopen(inputfile, "rb");

	if (fp==NULL) perror ("Error opening data file");

	else {

		fseek (fp, 0, SEEK_END);

		size=ftell (fp);

		printf ("Size of joecornearadialvolume.unp: %ld bytes.\n",size);

		rewind (fp);

	}

	fread(a, 2, XDIM*YDIM*FRAMES, fp);

	fclose(fp);

}

void convertor(){

	for(int i=0;i<XDIM*YDIM*FRAMES; i++){

		original_cpu[i] = (float)a[i]; 

	}

}

void calc_resample_coefficients() {

	float lambda[LINE_LENGTH];

	float k_sampled[LINE_LENGTH];

	float k_resampled[LINE_LENGTH];

	float lambda0 = START_WAVELENGTH;

	float lambda1 = WAVELENGTH_SPACING;

	float lambda2 = SECOND_ORDER_CORRECTION;

	float lambda3 = THIRD_ORDER_CORRECTION;

	float lambda4 = FOURTH_ORDER_CORRECTION;

	float kmax;

	float kmin;

	

	for (int y=0; y<LINE_LENGTH; y++) {

		lambda[y] = lambda0 + lambda1*(y+CAPTURE_LINE_OFFSET) +

				lambda2*pow(float(y+CAPTURE_LINE_OFFSET), 2) +

				lambda3*pow(float(y+CAPTURE_LINE_OFFSET), 3) +

				lambda4*pow(float(y+CAPTURE_LINE_OFFSET), 4);

		k_sampled[y] = 1000000./lambda[y];

		}

	kmin = 1000000./lambda[0];		//wavenumber in mm^-1

	kmax = 1000000./lambda[LINE_LENGTH-1];

	k_resampledspacing = (kmin - kmax)/(LINE_LENGTH-1);

	for (int y=0; y<LINE_LENGTH; y++) {

		k_resampled[y] = kmin + (float(y)/(LINE_LENGTH - 1))*(kmax - kmin);

	}

	for (int y=0; y<LINE_LENGTH; y++) {

		for (int i=0; i<LINE_LENGTH; i++) {

			if (k_resampled[y] >= k_sampled[i] && k_resampled[y+1] < k_sampled[i]) {

				resamp_1D[y*2+0] = float(i);

				resamp_1D[y*2 +1] = k_resampled[y] - k_sampled[i];

			break;

			}

		}

	}

}

//simply use cudaGraphicsMapFlagsWriteDiscard as cudaGraphicsResource

void createVBO(GLuint* pixel_buffer, struct cudaGraphicsResource **cuda_pixel_resource)

{

	// create buffer object

	glGenBuffers(1, pixel_buffer);

	glBindBuffer(GL_ARRAY_BUFFER, *pixel_buffer);

	//register buffer on cuda

	unsigned int size = XDIM * YDIM * 4 * sizeof(float); 

	glBufferData(GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW); 

	glBindBuffer(GL_ARRAY_BUFFER, 0); 

	cutilSafeCall(cudaGraphicsGLRegisterBuffer(cuda_pixel_resource, *pixel_buffer, cudaGraphicsMapFlagsWriteDiscard));

}

void display() { 

	// Map buffer object for writing from CUDA 

	float4* pix_buff; 

	cudaGraphicsMapResources(1, &cuda_pbo_resource, 0); 

	size_t num_bytes; 

	cudaGraphicsResourceGetMappedPointer((void**)&pix_buff, &num_bytes, cuda_pbo_resource);

	// Execute kernel 

	// will execute by calling kernel.cu file

	launch_kernel(dev_sum, dev_original, dev_frame, d_in, dev_k_resampledspacing, dc_subtracted, dev_resamp, pix_buff);

	// Unmap buffer object 

	cudaGraphicsUnmapResources(1, &cuda_pbo_resource, 0);

	//////////////////////////////////////////////////////////

	// Under is the part I do not understand clear

	//////////////////////////////////////////////////////////

	// copy to Open_gl texture

	// Render from buffer object 

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

	glBindBuffer(GL_ARRAY_BUFFER, pbo); 

	glVertexPointer(4, GL_FLOAT, 0, 0); 

	glEnableClientState(GL_VERTEX_ARRAY); 

	glDrawArrays(GL_POINTS, 0, XDIM * YDIM); 

	glDisableClientState(GL_VERTEX_ARRAY); 

	

	// Swap buffers 

	glutSwapBuffers(); 

	glutPostRedisplay(); 

}

void deleteVBO() { 

	cudaGraphicsUnregisterResource(cuda_pbo_resource); 

	glDeleteBuffers(1, &pbo);

}

int main() { 

	// Pre-Calculation

	readData();

	convertor(); //convert values into single float

	calc_resample_coefficients();

	//Cuda Memory Allocation

	int frame = 0, sum =0;

	cudaMalloc((void**)&dev_frame, sizeof(int));

	cudaMalloc((void**)&dev_sum, sizeof(int));

	cudaMalloc((void**)&dev_original, sizeof(float)*FRAMES*XDIM*YDIM);

	cudaMalloc((void**)&d_in,sizeof(cuComplex)*YDIM*XDIM);

	cudaMalloc((void**)&dev_resamp, sizeof(float)*LINE_LENGTH*2);

	cudaMalloc((void**)&dev_k_resampledspacing, sizeof(float));

	cudaMalloc((void**)&dc_subtracted, sizeof(float)*XDIM*YDIM);

		

	cudaMemcpy(dev_k_resampledspacing, &k_resampledspacing, sizeof(float), cudaMemcpyHostToDevice);

	cudaMemcpy(dev_resamp, resamp_1D, sizeof(float)*LINE_LENGTH*2, cudaMemcpyHostToDevice);

	cudaMemcpy(dev_original, original_cpu, sizeof(float)*FRAMES*XDIM*YDIM, cudaMemcpyHostToDevice);

	cudaMemcpy(dev_frame, &frame, sizeof(int), cudaMemcpyHostToDevice);

	cudaMemcpy(dev_sum, &sum, sizeof(int), cudaMemcpyHostToDevice);

	// Explicitly set device 

	cudaGLSetGLDevice(0); 

	glutDisplayFunc(display);

	

	// Create buffer object and register it with CUDA 

	createVBO(&pbo, &cuda_pbo_resource);

	// Loop 

	glutMainLoop(); 

}

This is the actual code. Will this help? Thank you.

It looks like you’re having a CUDA call throw an exception - I can’t tell from eyeballing the code where at however. If you step through line-by-line in a debugger, what line does it throw the exception at?

If I set the breakpoint to creatVBO on main function it shoots the exception and exit program. I cant find anywhere it is wrong on creatVBO… Thank you

Actually I added

glutInit(argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(window_width, window_height);
glutCreateWindow(“Cuda GL Interop (VBO)”);

in between cudaGLSetGLDevice(0) and glutDisplayFunc(display) for initialization.
I get this error at glutInitDisplayMode

Unhandled exception at 0x69958ea7 in Resampling_GL.exe: 0xC0000005: Access violation reading location 0x00000001.

It seems like it cannot access to argc, argv in first place. Do I have to initialize these values as well? Thank you

those are the arguments to your main function. In actuality, you don’t really need those arguments to glutInit() - you only need to include them if you have some command-line based input flags for glut (which I assume you don’t). Thus, you can fake it by something like this, IIRC:

int main(){

   ...

   int numArgs = 0;

   glutInit(&numArgs, NULL);

   ...

}

Thanks this solves my problem. It says now that I have unexpected driver error on

cutilSafeCall(cudaGraphicsGLRegisterBuffer(cuda_pixel_resource, *pixel_buffer, cudaGraphicsMapFlagsWriteDiscard));

on createVBO function. Im not sure about why this problem is arising since following is the code for createVBO which follows the convention

void createVBO(GLuint* pixel_buffer, struct cudaGraphicsResource **cuda_pixel_resource)

{

// create buffer object

glGenBuffers(1, pixel_buffer);

glBindBuffer(GL_ARRAY_BUFFER, *pixel_buffer);

//register buffer on cuda

unsigned int size = XDIM * YDIM * 4 * sizeof(float); 

glBufferData(GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW); 

glBindBuffer(GL_ARRAY_BUFFER, 0); 

cutilSafeCall(cudaGraphicsGLRegisterBuffer(cuda_pixel_resource, *pixel_buffer, cudaGraphicsMapFlagsWriteDiscard));

}

Can someone help me? Thank you

Without the specific error message I’m going to be guessing here, but since it’s crashing on your CUDA call, odds are your CUDA context isn’t being created properly. I’ve only worked on CUDA <–> OpenGL interop with the driver api, so I can’t speak to this directly, but for the driver api, your OpenGL context should be created with you glutInit(…) call, and your CUDA context should be created afterwards via cuGLCtxCreate(…). My advice would be to add error checking to your CUDA and OpenGL calls, and instead of using the cutilSafeCall, rather assign your cudaGraphicsGLRegisterBuffer(…) call to a cudaError variable and check it against [post=‘these’]http://developer.download.nvidia.com/compute/DevZone/docs/html/C/doc/html/group__CUDART__TYPES_g3f51e3575c2178246db0a94a430e0038.html#g3f51e3575c2178246db0a94a430e0038[/post] to see the exact cause of the failure.

For me cudaGraphicsGLRegisterBuffer was giving an error until i did not called
glewInit();

Hope thats missing in your case also,

Gaszton

Hi Ive used CUT_ERROR_CHECK instead of cutilSafeCall and it says that Im having some access violation regarding default dll as following.

Unhandled exception at 0x5c3e9d2f (msvcr90d.dll) in Resampling_GL.exe: 0xC0000005: Access violation reading location 0x000027d9.

Is there anything wrong with this? Thank you.
Regards,
Jaehong Yoon