Programming Memoirs

3D (depth) composition of CUDA ray traced images with OpenGL rasterized images using CUDA Driver API

Depth composition of CUDA ray traced image with OpenGL rasterised object transformation gizmos

Ray tracing is a great method of generating synthetic images. It has many benefits over traditionally used (e.g. in computer game) rasterization. Ray tracing stays great up to the moment when you need to render e.g. a line segment placed in your 3D space (which is potential occluded by other 3D objects).

Why would you want to ray trace a line or a line segment? Say you want to create some kind of transformation gizmo for you 3D objects which blends nicely into the scene, or a bounding box depicting the boundaries of an object, or include wire frame of meshes in your ray traced scene, or …  There are many potential uses.

You cannot just mathematically test for camera ray vs. line segment collision and expect the line segment to appear on the ray traced rendering. Chance of a camera ray colliding a line segment are too slim for the line segment to be visible.

You can try simulating a line by drawing a thin cylinder, or an ‘x’ made of 2 quads. But this is far from an elegant solution. Additionally, your ‘line’ stops being a mathematical line, as it suddenly has a width. Thus, it becomes thicker closer to the camera, and thinner further away from it (considering you use perspective projection).

Fortunately there is a solution for drawing lines in ray traced content. The solution involves using some some kind of rasterization based renderer, such as OpenGL to draw the lines (or any objects) separately from the ray tracing pass and then performing a 3D composition of the two images.  The problem becomes even more interesting if you obtain the ray traced image using CUDA and CUDA Driver API.

CUDA and OpenGL interoperability using CUDA Driver API

Before we compose CUDA ray traced content with OpenGL rasterised content we need to obtain the former. If you’re a CUDA programmer you know that CUDA does not posses any native graphics output and You have to manually take care of displaying the generated data. One of the solutions for this is to write pixel data into a OpenGL Pixel Buffer Object (PBO), copying the content of PBO to an OpenGL texture, and rendering a quad textured using this texture into the viewport. Fortunately for us CUDA and OpenGL interoperability is quite robust and this can be done quite easily, even when using lower-level CUDA Driver API.

// general initialisation
init_CUDA_and_OpenGL();
set_up_camera(camera);

GLuint pbo; // Pixel buffer object
GLuint tex; // Texture
unsigned int width = 512; // horizontal render resolution
unsigned int height = 512; // vertical render resolution

// Init PBO
glGenBuffersARB(1, &pbo);
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, width*height*sizeof(GLubyte)*4, 0, GL_STREAM_DRAW_ARB);
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);

// Init Texture
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);

// Allocate CUDA graphic Resource (Notice that I'm using Driver API function calls)
CUgraphicsResource * cuda_pbo_resource = (CUgraphicsResource*) malloc(sizeof(CUgraphicsResource));
cuGraphicsGLRegisterBuffer(cuda_pbo_resource, // returns pointer to the CUDA resource
 pbo, // we pass here the OpenGL resource
 CU_GRAPHICS_MAP_RESOURCE_FLAGS_WRITE_DISCARD); // Flag to aid optimisation

Rendering pass #1 — Ray tracing in CUDA

We render our scene (only solid objects, i.e. no lines etc.) using ray tracing. We store the raw pixel data in a PBO mapped as CUDA resource.
Note that PBO resource has to be unmapped from CUDA for OpenGL to be able to reliably access it. Thus we map the PBO resource to CUDA only for the time of ray tracing.

// map PBO resource
cuGraphicsMapResources(1, cuda_pbo_resource, 0); //mapping is only temporary

// get pointer in CUDA memory space for to the resource
CUdeviceptr d_pbo; // CUDA pointer through which the mapped graphics resources may be accessed
size_t num_bytes; //size of memory which may be accessed from that pointer
cuGraphicsResourceGetMappedPointer(&d_pbo, &num_bytes, *cuda_pbo_resource); // the d_pbo will be passed to rendering kernel

// Start ray tracing kernel
start_CUDA_ray_tracing_kernel(d_pbo, num_bytes); // render to PBO. Pass the pointer to the memory space where the data is to be saved

// unmap PBO resource
cuGraphicsUnmapResources(1, cuda_pbo_resource, 0);

After the PBO is filled with data by CUDA kernel we copy the content of the PBO to an OpenGL texture. On current hardware this step takes less than a milisecond for a 24 bits per pixel with a 512×512 resolution, thus it’s quite fast. Alternatively. You could attempt to render from CUDA directly to OpenGL texture memory space.


// copy from PBO to texture
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
glBindTexture(GL_TEXTURE_2D, tex);  // bind our texture!
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, 0); // copy data
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0); // unbind PBO

Displaying CUDA rendered content with OpenGL

We can now render the texture onto a quad which is filling the viewport (using orthogonal projection).


// select matrix mode
glMatrixMode(GL_PROJECTION); // we select to  work with projection matrix stack
glLoadIdentity(); // make sure the there is nothing on the stack

glOrtho(0,1, 0,1,
0,             // zNear
999.0f);    // zFar

// clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST); // we dont need it right now

// draw binded textured to a quad filling the viewport
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
	glTexCoord2f(0, 0); glVertex2f(0, 0);
	glTexCoord2f(1, 0); glVertex2f(1, 0);
	glTexCoord2f(1, 1); glVertex2f(1, 1);
	glTexCoord2f(0, 1); glVertex2f(0, 1);
glEnd();

glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);  // unbind texture

Rendering pass #2 — Rasterisation with OpenGL

We render the whole 3D scene (solid object only) once more, this time using OpenGL. You would probably want to switch the view mode from orthogonal to perspective projection with the same parameters as you used in your ray tracer (i.e. camera position, FOV, camera ‘look at’ point). I recommend setting the perspective manually using glFrustum() as it allows to set the camera parameters in a similar manner as you have probably used them in the CUDA kernel for ray tracing.

Note, that now we want to render the scene only to the OpenGL depth buffer — set you color buffer channel’s masks to false, and have depth testing enabled. Also, we will not need any OpenGL lights, textures, fog etc. — have them disabled.  As we are rasterizing only depth information this additional rendering pass is quite fast even for complex scenes.


// set perspective to the same as in the ray tracing pass
GLdouble fl_left = camera->plane_height;    // width of left
GLdouble fl_right = -camera->plane_height; // width of right (note the MINUS)
GLdouble fl_top = camera->plane_width;	// width of top
GLdouble fl_bottom = -camera->plane_width; // width of bottom (note the MINUS)
GLdouble zNear = camera->plane_distance;  // near clipping distance
GLdouble zFar = camera->plane_distance + 100; // far clipping distance

glFrustum(fl_left, fl_right, fl_bottom, fl_top, zNear, zFar);

gluLookAt(
 // camera position
 camera->origin.x,
 camera->origin.y,
 camera->origin.z,
 // look_at is stored as a normal vector, thus we add it to the camera position to determine a point in space where to look
 camera->origin.x + camera->look_at.x,
 camera->origin.y + camera->look_at.y,
 camera->origin.z + camera->look_at.z,
 // camera up direction
 camera->up.x,
 -camera->up.y,
 camera->up.z);

// render only to depth buffer
glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE); // we dont want to render to color buffer
glEnable(GL_DEPTH_TEST); // it is important to be enabled now

// Render the meshes in the scene to depth buffer only (do not render lines yet)
render_scene_objects_with_OpenGL();

Now, we want to enable color mask, and while having the depth testing still on, we rasterize remaining objects (lines, points etc.) to the color and depth buffers of OpenGL. However we can choose to also rasterize any additional solid objects at this point, basically any OpenGL scene!

glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); // we now want to render colors
render_lines_and_misc_stuff();

As the depth buffer already contains scene depth information, rasterized object and ray traced objects will seem to occlude each other in a manner which you would expect. :-) You can also achieve a partial occlution by CUDA object (transparency effect), but first rasterizing the misc. OpenGL objects partly transparent with GL_DEPTH_TEST disabled, and later rendering them fully opaque with depth testing enabled.

Oh, don’t forget to swap buffers if you were using double buffering!

Limitations

The major limitation of this method is the obvious fact that the lines (or any misc OpenGL rendered content) present behind translucent object (which in ray tracing refract light) won’t appear refracted. Same goes for reflections. Fortunately, for transformation gismos, bounding boxes etc. this is not really a problem. It might be even desirable.

Additional resources

You might also want to check this resource for more on depth composition of images in OpenGL.

More on PBOs can be found here and here.

Triers CUDA ray tracing tutorial — a great tutorial with source code available.

Hope this helps.

252 ResponsesLeave one →

  1. [url=http://ataraxgen.com/]buy atarax 25 mg[/url]

    Reply
  2. [url=http://acyclovirzov.com/]aciclovir tablets[/url]

    Reply
  3. [url=http://tretinoin24.com/]best tretinoin cream[/url]

    Reply
  4. [url=https://prednisonexr.com/]prednizone for sale[/url]

    Reply
  5. [url=http://prednisonexr.com/]prednisone 7.5 mg[/url] [url=http://ventolin24.com/]ventolin generic[/url]

    Reply
  6. [url=http://prednisone100.com/]prednisone 50[/url] [url=http://indocin24.com/]indocin[/url]

    Reply
  7. [url=https://synthroid24.com/]synthroid 15 mcg[/url] [url=https://motilium24.com/]buy motilium[/url] [url=https://tetracyclinecaps.com/]tetracycline 500 mg coupon[/url] [url=https://metforminmd.com/]price of metformin[/url] [url=https://sildenafilrem.com/]sildenafil 20 mg[/url]

    Reply
  8. [url=http://stromectoltab.com/]stromectol 3 mg tablet[/url] [url=http://sildenafilrem.com/]sildenafil 20 mg price[/url] [url=http://prednisolonester.com/]buy prednisolone 25mg tablets[/url] [url=http://ventolin2020.com/]ventolin coupon[/url] [url=http://metforminmd.com/]buy generic metformin[/url]

    Reply
  9. What necessary words… super, a brilliant phrase


    I apologise, but, in my opinion, you are mistaken. Let’s discuss it. Write to me in PM, we will talk. virginity girls pussies, take a girls virginity а также [url=https://mypureclub.com/]article[/url] virginity test for girls

    Reply
  10. [url=https://metforminmd.com/]metformin script cost[/url] [url=https://fluoxetinetb.com/]fluoxetine hcl 20 mg[/url]

    Reply
  11. [url=http://prednisone100.com/]prednisone 10 mg tablet[/url] [url=http://tretinoin24.com/]generic tretinoin[/url]

    Reply
  12. Wow plenty of terrific tips. [url=https://acyclovir-buy.com/]acyclovir cream[/url]

    Reply
  13. [url=http://azithromycinzpak.com/]azithromycin 500mg[/url]

    Reply
  14. Este neurotransmisor es el que relaja los vasos sanguíneos del cuerpo cavernoso, facilitando la entrada de sangre en el mismo. Se ha denominado tradicionalmente como impotencia a la incapacidad de realizar el acto sexual por no alcanzar o mantener la suficiente erección del pene para introducirlo o mantenerlo introducido en la vagina de la mujer durante el tiempo normal del coito.

    Reply
  15. sex arab ru [url=https://sexjk.com/xnxx]ءىءء[/url]

    Reply
  16. [url=http://colchicine365.com/]buy colchicine australia[/url]

    Reply
  17. granny arab sex [url=https://sexjk.com/%D9%85%D9%86%D8%B2%D9%84%D9%8A-%D8%A7%D8%AA%D8%B4-%D8%AF%D9%8A-%D8%B9%D8%B1%D8%A8%D9%8A-%D9%85%D8%AC%D8%A7%D9%86%D9%8A]مواقع سكس[/url]

    Reply
  18. [url=http://metformin2.com/]metformin 500mg[/url] [url=http://colchicine365.com/]colchicine 0.6 price[/url] [url=http://stromectoltab.com/]stromectol buy[/url] [url=http://albuterolgen.com/]albuterol 83[/url] [url=http://ventolin2020.com/]ventolin coupon[/url]

    Reply
  19. [url=http://sildenafilrem.com/]sildenafil 100mg price online[/url] [url=http://ventolin24.com/]ventolin for sale[/url]

    Reply
  20. [url=https://motilium24.com/]buy motilium australia[/url] [url=https://tetracyclinecaps.com/]tetracycline 3[/url]

    Reply
  21. [url=http://avodartgen.com/]avodart 0 5 mg[/url] [url=http://furosemidelab.com/]medicine furosemide 20 mg[/url] [url=http://fluoxetine1000.com/]fluoxetine cap 20mg[/url] [url=http://prednisonetl.com/]prednisone 20 mg[/url] [url=http://vardenafill.com/]vardenafil generic[/url] [url=http://albendazole365.com/]albendazole otc usa[/url] [url=http://celebrexotc.com/]celebrex[/url] [url=http://ventolin911.com/]can i buy ventolin over the counter australia[/url]

    Reply
  22. [url=http://fluoxetine1000.com/]fluoxetine 30 mg cost[/url] [url=http://celebrexotc.com/]buy celebrex online[/url] [url=http://vardenafill.com/]vardenafil price comparison[/url] [url=http://sildenafilvg.com/]sildenafil 50 mg coupon[/url]

    Reply
  23. [url=https://metformin360.com/]buying online metformin from india[/url]

    Reply
  24. [url=http://diflucan5.com/]buy diflucan[/url] [url=http://avodart24.com/]avodart price usa[/url] [url=http://albuterol365.com/]otc albuterol[/url] [url=http://ventolin911.com/]buy ventolin[/url] [url=http://tadalafilcia.com/]tadalafil 20mg[/url]

    Reply
  25. [url=http://metformin360.com/]cost of metformin in mexico[/url]

    Reply
  26. [url=https://furosemidetab.com/]furosemide 20 mg tabs[/url]

    Reply
  27. [url=http://arimidexsale.com/]arimidex brand name[/url]

    Reply
  28. [url=http://fluoxetine1000.com/]fluoxetine 30 mg[/url]

    Reply
  29. It is remarkable, very amusing phrase


    I consider, that you are not right. I am assured. I can defend the position. tube sex army, chubby sex tube, [url=https://sexsaoy.com/%D9%82%D8%B5%D8%B5%20%D8%AC%D9%86%D8%B3%D9%8A%D8%A9]Sex stories[/url] horse tube sex

    Reply
  30. [url=https://celebrexotc.com/]celebrex[/url]

    Reply
  31. [url=http://metformin360.com/]metformin glucophage[/url] [url=http://celebrexotc.com/]celebrex 100mg price usa[/url] [url=http://vardenafill.com/]vardenafil generic[/url]

    Reply
  32. [url=http://amoxicillincaps.com/]amoxicillin 500 mg[/url]

    Reply
  33. [url=https://wratotadaper.ml]wratotadaper.ml[/url]

    Reply
  34. You are not right. Let’s discuss.


    This situation is familiar to me. Let’s discuss. sport machen gesundheit, was ist das gesundheit и [url=https://norman-stuttgart.de]Norman Stuttgart[/url] geburtstag und gesundheit

    Reply
  35. [url=http://furosemidelab.com/]cost of furosemide[/url]

    Reply
  36. [url=https://prednisonetl.com/]generic prednisone[/url]

    Reply
  37. [url=https://rinesuri.tk]rinesuri.tk[/url]
    [url=https://litibtadust.tk]litibtadust.tk[/url]
    [url=https://boicrocovpoi.tk]boicrocovpoi.tk[/url]
    [url=https://termacongdertme.gq]milf bent over pics [/url]
    [url=https://intravlothickspec.ml]intravlothickspec.ml[/url]

    Reply
  38. Los hombres con diabetes o afecciones cardíacas corren un riesgo mayor de desarrollar DE, al igual que los que consumen tabaco o tienen un sobrepeso grave.

    Reply
  39. [url=http://diflucan5.com/]diflucan 150 mg[/url] [url=http://avodartgen.com/]avodart generic[/url] [url=http://furosemide1.com/]medication furosemide 40 mg[/url] [url=http://albendazole365.com/]albendazole 400 mg[/url] [url=http://diflucan10.com/]can you buy diflucan over the counter[/url] [url=http://prednisonester.com/]prednisone 5mg tablets price[/url]

    Reply
  40. Certainly. So happens. Let’s discuss this question.


    Excuse please, that I interrupt you. magic movies, movies скачать, [url=https://tpist.org/]tpist.org[/url] movies kiss

    Reply
  41. The excellent message, I congratulate)))))


    It agree, it is the amusing information movies and, movies love, [url=https://song-words.net/]song-words.net[/url] google movies

    Reply
  42. In my opinion it already was discussed.


    It is remarkable, very good message ero movies, gangbang movies, [url=https://mcmbackpacks.com/]mcmbackpacks.com[/url] youtube movies

    Reply
  43. [url=http://vardenafill.com/]vardenafil generic[/url]

    Reply
  44. I think, that you are not right. Let’s discuss. Write to me in PM, we will talk.


    It is remarkable, very good message movies uncensored, geo movies, [url=https://karlgroves-sandbox.com/]karlgroves-sandbox.com[/url] christmas movies

    Reply
  45. You are mistaken. Let’s discuss. Write to me in PM.


    Prompt, where I can find more information on this question? softcore movies, shark movies, [url=https://hirojs.com/]hirojs.com[/url] movies 4k

    Reply
  46. The authoritative point of view, curiously..


    I am ready to help you, set questions. italian movies, m movies, [url=https://hesterstreettroupe.com/]hesterstreettroupe.com[/url] road movies

    Reply
  47. I think, that you commit an error. I suggest it to discuss. Write to me in PM, we will communicate.


    You have hit the mark. It is excellent thought. I support you. teeny movies, p movies, [url=https://fomoaf.com/]fomoaf.com[/url] movies sexy

    Reply
  48. I am am excited too with this question. Prompt, where I can read about it?


    I can suggest to visit to you a site, with an information large quantity on a theme interesting you. okko movies, movies torrent, [url=https://evokewebstudio.com/]evokewebstudio.com[/url] swingers movies

    Reply

Leave a Reply