I tried to port this example to WINDOWS with GLFW, since I don't have access to Linux box .. but the only thing I get is the clear color and nothing comes up ..
Did others get this example to work / Did I miss something here?
I do not even get the original image, before the sort either ...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thrust/device_vector.h>
#include <thrust/sort.h>
#include <GL/glew.h>
#include <GL/glfw.h>
#include <cuda_gl_interop.h>
const int WIDTH=800;
const int HEIGHT=800;
const int DIM = 800;
GLuint bufferObj;
cudaGraphicsResource *resource;
struct sort_functor
{
__host__ __device__
bool operator()(uchar4 left, uchar4 right) const
{
return (left.y < right.y);
}
};
// create a green/black pattern
__global__ void kernel(uchar4 *ptr) {
// map from threadIdx/BlockIdx to pixel position
int x = threadIdx.x + blockIdx.x * blockDim.x;
int y = threadIdx.y + blockIdx.y * blockDim.y;
int offset = x + y * blockDim.x * gridDim.x;
// now calculate the value at that position
float fx = x / (float)DIM - 0.5f;
float fy = y / (float)DIM - 0.5f;
unsigned char green = 128 + 127 * sin(abs(fx * 100) - abs(fy * 100));
// accessing uchar4 vs unsigned char*
ptr[offset].x = 0;
ptr[offset].y = green;
ptr[offset].z = 0;
ptr[offset].w = 255;
}
static void sort_pixels(){
cudaGraphicsMapResources(1, &resource, NULL);
uchar4* devPtr;
size_t size;
cudaGraphicsResourceGetMappedPointer((void**)&devPtr, &size, resource);
thrust::device_ptr<uchar4> tptr = thrust::device_pointer_cast(devPtr);
thrust::sort(tptr, tptr + (DIM*DIM), sort_functor());
cudaGraphicsUnmapResources(1, &resource, NULL);
}
void GLFWCALL Keyboard_Callback(int key, int action)
{
if (key == 32 && action == GLFW_PRESS)
sort_pixels();
return;
}
int main ()
{
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
return -1;
}
glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
if( !glfwOpenWindow( WIDTH, HEIGHT, 0,0,0,0, 32,0, GLFW_WINDOW ) )
{
fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible.\n" );
glfwTerminate();
return -1;
}
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
glfwSetWindowTitle( "Sort Test" );
glfwEnable( GLFW_STICKY_KEYS );
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearColor(0.087, 0.087, 0.087, 1.0);
glfwSetKeyCallback(Keyboard_Callback);
// SORT CODE
glGenBuffers(1, &bufferObj);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, bufferObj);
glBufferData(GL_PIXEL_UNPACK_BUFFER, DIM * DIM * 4, NULL, GL_DYNAMIC_DRAW);
cudaGraphicsGLRegisterBuffer(&resource, bufferObj, cudaGraphicsMapFlagsNone);
cudaGraphicsMapResources(1, &resource, NULL);
uchar4* devPtr;
size_t size;
cudaGraphicsResourceGetMappedPointer((void**)&devPtr, &size, resource);
dim3 grid(DIM / 16, DIM / 16);
dim3 threads(16, 16);
kernel << <grid, threads >> >(devPtr);
cudaGraphicsUnmapResources(1, &resource, NULL);
do{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDrawPixels(DIM, DIM, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glfwSwapBuffers();
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
glfwGetWindowParam( GLFW_OPENED ) );
// Close OpenGL window and terminate GLFW
glfwTerminate();
return 0;
}
Edit:
I added :
static void HandleError(cudaError_t err, int line) {
if (err != cudaSuccess) {
printf("%s in %s at line %d\n", cudaGetErrorString(err), "main", line);
exit(EXIT_FAILURE);
}
std::cout << "What happened : " << line << " " << cudaGetErrorString(err)<< std::endl;
}
#define HANDLE_ERROR( err ) (HandleError( err, __FILE__, __LINE__ ))
with and example call:
HandleError(cudaGraphicsGLRegisterBuffer(&resource, bufferObj, cudaGraphicsMapFlagsNone), 134);
Per the comments, no CUDA errors? so does that mean there is something wrong with the binding of the PBO or draw commands?