sample:cuda-samples/Samples/5_Domain_Specific/simpleGL at master · NVIDIA/cuda-samples · GitHub
Hi, I’m trying to implement the aforementioned sample into an existing (non CUDA project). I can compile stuff just fine and for now I’m using the helper functions provided in the sample repo, but I don’t see anything (just a black screen)
I have made attempts to simplify the sample as much as possible (I have done the same with the sample - it still works).
my code:
kernel.cuh
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <windows.h>
#include <vector_types.h>
#include<GL/glew.h>
#include <cuda_runtime.h>
#include <cuda_gl_interop.h>
#include "cuda/helper_functions.h" // includes cuda.h and cuda_runtime_api.h
#include "cuda/helper_cuda.h" // helper functions for CUDA error check
void runTest();
void createVBO(GLuint* vbo, struct cudaGraphicsResource** vbo_res,
unsigned int vbo_res_flags);
void display();
void runCuda(struct cudaGraphicsResource** vbo_resource);
kernel.cu
#include "kernel.cuh"
const unsigned int mesh_width = 100;
const unsigned int mesh_height = 100;
GLuint vbo;
struct cudaGraphicsResource* cuda_vbo_resource;
void* d_vbo_buffer = NULL;
float g_fAnim = 0.0;
__global__ void simple_vbo_kernel(float4* pos, unsigned int width, unsigned int height, float time)
{
unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;
unsigned int y = blockIdx.y * blockDim.y + threadIdx.y;
// calculate uv coordinates
float u = x / (float)width;
float v = y / (float)height;
u = u * 2.0f - 1.0f;
v = v * 2.0f - 1.0f;
// calculate simple sine wave pattern
float freq = 4.0f;
float w = sinf(u * freq + time) * cosf(v * freq + time) * 0.5f;
// write output vertex
pos[y * width + x] = make_float4(u, w, v, 1.0f);
}
void runTest()
{
glViewport(0, 0, 600, 600);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
createVBO(&vbo, &cuda_vbo_resource, cudaGraphicsMapFlagsWriteDiscard);
runCuda(&cuda_vbo_resource);
}
void createVBO(GLuint* vbo, struct cudaGraphicsResource** vbo_res,
unsigned int vbo_res_flags)
{
assert(vbo);
glGenBuffers(1, vbo);
glBindBuffer(GL_ARRAY_BUFFER, *vbo);
unsigned int size = mesh_width * mesh_height * 4 * sizeof(float);
glBufferData(GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
checkCudaErrors(cudaGraphicsGLRegisterBuffer(vbo_res, *vbo, vbo_res_flags));
}
void display()
{
runCuda(&cuda_vbo_resource);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexPointer(4, GL_FLOAT, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glColor3f(1.0, 0.0, 0.0);
glDrawArrays(GL_POINTS, 0, mesh_width * mesh_height);
glDisableClientState(GL_VERTEX_ARRAY);
g_fAnim += 0.01f;
}
void launch_kernel(float4* pos, unsigned int mesh_width,
unsigned int mesh_height, float time)
{
dim3 block(8, 8, 1);
dim3 grid(mesh_width / block.x, mesh_height / block.y, 1);
simple_vbo_kernel << < grid, block >> > (pos, mesh_width, mesh_height, time);
}
void runCuda(struct cudaGraphicsResource** vbo_resource)
{
float4* dptr;
checkCudaErrors(cudaGraphicsMapResources(1, vbo_resource, 0));
size_t num_bytes;
checkCudaErrors(cudaGraphicsResourceGetMappedPointer((void**)&dptr, &num_bytes,
*vbo_resource));
launch_kernel(dptr, mesh_width, mesh_height, g_fAnim);
checkCudaErrors(cudaGraphicsUnmapResources(1, vbo_resource, 0));
}
I apologize for dumping so much code, but I honestly have no idea what is wrong. In my main I call the runTest()
method first and then I call the display()
function on every frame. I’m using GLFW and my setup args look something like this:
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
any help would be greatly appreciated.