Programming Memoirs

Faking anti-aliasing in CUDA graphic output

Faking anti-aliasing of CUDA content with OpenGL

Recently I’ve been working on a CUDA ray tracing application which uses OpenGL for CUDA graphics output. I was slightly annoyed by the rough, aliased, edges of my output, but I wanted to avoid implementing a fully fledged anti-aliasing solution. This short post described how to fake anti-aliasing in your CUDA output in a highly computationally efficient (and simple) manner by using OpenGL’s texture filtering.

One way to display CUDA output is to render it to an OpenGL PBO, copy it to a texture and texture it on a quad covering whole OpenGL view port, see here for more info. In a typical situation one texture pixel (texel) maps directly to corresponding screen pixel.

However, if you shift the textured quad, or the viewport, slightly, so that there is no direct mapping between texels and pixels you will enforce OpenGL to applying subtle linear filtering to the texture. Thus, you’ll get rid of those nasty aliased edges. See the image for side-by-side comparison.

Shifting the viewport:


float offset = 0.998; // slighly shifted from 1.0
glOrtho(-offset, offset,
           -offset, offset,
           0, 1000);  // zNear, zFar

Remember to have OpenGL texture filtering set to GL_LINEAR for the texture:

GL_LINEAR returns the weighted average of the four texture elements that are closest to the centre of the pixel being textured


GLuint tex; // Texture to which PBO content from CUDA is copied

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_LINEAR);  // GL_LINEAR
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // GL_LINEAR
glBindTexture(GL_TEXTURE_2D, 0);

Pros:

  • it’s lighting-fast the whole ‘anti-aliasing’ process is done by rasterization hardware of your GPU
  • it’s dead simple to implement — which means little changes to your existing code

Cons:

  • It’s still not true anti-aliasing; thus the image gets slightly blurred also in low-contrast areas (which is not the case with true AA)
  • It’s not a perfect solution — it works best for close to diagonal edges (like to ones on the image). if a given edge is close to being horizontal/vertical the results are far less impressive.

248 ResponsesLeave one →

  1. [url=http://buymetformin.shop/]price of metformin without insurance[/url]

    Reply
  2. [url=http://accutane.digital/]accutane generic over the counter[/url]

    Reply
  3. [url=https://plavix.shop/]plavix 75 mg daily[/url]

    Reply
  4. [url=http://fildena.cfd/]fildena 100 online[/url]

    Reply
  5. [url=https://cipro.shop/]ciprofloxacin for sale[/url]

    Reply
  6. [url=https://ivermectin.science/]stromectol cream[/url]

    Reply
  7. [url=http://buyacyclovir.shop/]acyclovir 5 cream price[/url]

    Reply
  8. [url=http://ivermectin.cool/]ivermectin 400 mg[/url]

    Reply
  9. [url=http://fildena.cfd/]fildena 200mg[/url]

    Reply
  10. [url=http://ivermectin.cool/]ivermectin 3 mg tablet dosage[/url]

    Reply
  11. [url=https://amoxicillin.directory/]can i buy amoxicillin over the counter in usa[/url]

    Reply
  12. [url=http://retina.best/]tretinoin 20 mg[/url]

    Reply
  13. [url=http://buyacyclovir.shop/]acyclovir cost without insurance[/url]

    Reply
  14. [url=https://atarax.store/]atarax 25 mg[/url]

    Reply
  15. [url=http://tretinoin.shop/]generic retin a coupon[/url]

    Reply
  16. [url=https://zoloft.cfd/]1000 mg zoloft[/url]

    Reply
  17. [url=http://amoxicillin.boutique/]augmentin 875 mg pill[/url]

    Reply
  18. [url=https://buyhydroxychloroquine.shop/]quineprox 750mg[/url]

    Reply
  19. [url=http://amoxicillin.boutique/]augmentin 1000[/url]

    Reply
  20. [url=https://anafranil.store/]anafranil for sale[/url]

    Reply
  21. [url=https://ivermectin.fyi/]ivermectin 6 mg tablets[/url]

    Reply
  22. [url=http://ivermectin.world/]stromectol ivermectin buy[/url]

    Reply
  23. [url=http://buyantabuse.shop/]disulfiram tablets 1mg[/url]

    Reply
  24. [url=https://buyventolin.shop/]order ventolin[/url]

    Reply
  25. [url=https://buyhydroxychloroquine.shop/]buy plaquenil online[/url]

    Reply
  26. [url=http://amoxicillin.boutique/]trimox capsule[/url]

    Reply
  27. [url=https://trazodone.works/]2 trazodone[/url]

    Reply
  28. [url=https://trazodone.works/]trazodone 434 mg[/url]

    Reply
  29. [url=https://disulfiram.store/]antabuse costs[/url]

    Reply
  30. [url=http://buytretinoin.shop/]retin a 05 cost[/url]

    Reply
  31. [url=http://buytretinoin.shop/]tretinoin 10mg capsules[/url]

    Reply
  32. [url=https://disulfiram.store/]buy disulfiram[/url]

    Reply
  33. [url=https://trazodone.works/]trazodone over the counter[/url]

    Reply
  34. [url=https://ivermectin.ink/]stromectol ebay[/url]

    Reply
  35. [url=http://ivermectin.world/]ivermectin 12 mg[/url]

    Reply
  36. [url=http://albuterol.guru/]canada to usa ventolin[/url]

    Reply
  37. [url=http://tretinoin.shop/]retin a cream 0.5 mg[/url]

    Reply
  38. [url=https://ivermectin.fyi/]price of ivermectin liquid[/url]

    Reply
  39. [url=https://atarax.store/]atarax tablet 10 mg[/url]

    Reply
  40. [url=http://amoxicillin.boutique/]rx augmentin[/url]

    Reply
  41. [url=https://anafranil.store/]anafranil[/url]

    Reply
  42. [url=http://augmentin.guru/]augmentin 125mg[/url]

    Reply
  43. [url=https://augmentin.guru/]buy augmentin 625mg[/url]

    Reply
  44. [url=http://zoloft.life/]zoloft 500mg[/url]

    Reply
  45. [url=https://finasteride.cfd/]finasteride 5mg coupon[/url]

    Reply
  46. [url=https://chloroquine.shop/]chloroquine uk[/url]

    Reply
  47. [url=https://augmentin.guru/]over the counter augmentin[/url]

    Reply
  48. [url=http://finasteride.cfd/]finasteride tablets 1mg price in india[/url]

    Reply

Leave a Reply