CUDA randomly freezes when using cudaStreamSynchronize with OpenGL interop

It loops fine, however after some time it randomly freezes on cudaStreamSynchronize call after calling cudaGraphicsMapResources on my texture. Here’s my code:

cudaArray_t cuda_array;

cuda_assert(cudaGraphicsMapResources(1, &cudaTexture, stream));
cuda_assert(cudaGraphicsSubResourceGetMappedArray(&cuda_array, cudaTexture, 0, 0));
  
cudaResourceDesc resDesc {};
resDesc.resType = cudaResourceTypeArray;
resDesc.res.array.array = cuda_array;
cudaSurfaceObject_t surface;
cuda_assert(cudaCreateSurfaceObject(&surface, &resDesc));

render<<<map.countChunks(), dim3(CHUNK_SIZE, CHUNK_SIZE), 0, stream>>>(surface, map.getChunks(), step, minX, minY, maxX, maxY);
cuda_assert(cudaPeekAtLastError());

cuda_assert(cudaDestroySurfaceObject(surface));

cuda_assert(cudaGraphicsUnmapResources(1, &cudaTexture, stream));

cuda_assert(cudaStreamSynchronize(stream));

Here’s how I register the texture:

glGenTextures(1, &dataTexture);
glBindTexture(GL_TEXTURE_2D, dataTexture);
  
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

cuda_assert(cudaGraphicsGLRegisterImage(&cudaTexture, dataTexture, GL_TEXTURE_2D, cudaGraphicsRegisterFlagsNone));

I solved the problem. I was writing ouside of the bound texture.

1 Like