7
\$\begingroup\$

We usually use OpenGL or DirectX to work with the GPU. If I want to use the CPU to render 2D/3D graphics, how does it work and what API can I use for it on Windows?

I know there is no benefit in using the CPU for graphics when GPUs exist, but I'd still like to understand how it works.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ There is actually a benefit. Sometimes rasterization on the CPU can help with occlusion culling if you're heavily GPU bound. \$\endgroup\$
    – RandyGaul
    May 22, 2014 at 19:49

3 Answers 3

7
\$\begingroup\$

GDI (Graphics Device Interface) is the software renderer under Windows. Basically any language/runtime platform under Windows that is not GPU-accelerated is going to be using GDI under the hood at some level. While Java AWT might use GDI directly via the C code that the Java runtime is written in, something like Flash running in Chrome will OTOH be using GDI if the desktop isn't GPU accelerated, vs DirectX if it is.

I know that there is no benefit of using the CPU for this purpose, but only to understand the operation.

Not at all. It can be a lot easier to simply plot lines, pixels and images, than to know the ins and outs of GPU-based graphics programming. If you intend to write native code to access GDI directly, maybe check out the gdiplus lib, as outlined in this tutorial. Good luck!

EDIT: For accuracy, it may also be worth noting that there are CPU implementations of OpenGL; Mesa being the common example. So, strictly speaking, OpenGL does not exclude software rendering. But that's worth mentioning only if you really want to be pedantic!

\$\endgroup\$
1
  • \$\begingroup\$ It's not true that all non GPU-accelerated drawing code will use GDI at some level. WARP exists so that apps and the OS shell itself can be coded against Direct3D 11 and gracefully degrade to software rendering if required. You won't find GDI in Windows 8 "modern" apps, for example. \$\endgroup\$
    – Trillian
    May 25, 2014 at 1:00
5
\$\begingroup\$

Actually, starting with Windows 7, Direct3D 11 is your answer. Of course the API defaults to using a GPU if you have one, but you can create a Direct3D device targeting the Windows Advanced Rasterization Platform (WARP), which is meant to be a high performance software rasterizer supporting the Direct3D API. You should not expect performance anywhere as close as that of a GPU, but it should be pretty good as far as CPU rasterization goes since it exists for Windows to support systems without hardware acceleration. Direct3D also provides a software reference rasterizer, but it's only designed for correctness and is terribly slow.

For more details on Direct3D 11 rasterizers, see the D3D_DRIVER_TYPE enumeration.

Edit: But can it run Crysis? Yes!

\$\endgroup\$
2
\$\begingroup\$

I wouldn't we so fast in jumping to "there's no benefit" conclusion. Whilst GPUs are absurdly fast at processing triangles, not all of the images are made using triangle rasterization!

For example, voxel-octree based renderers (used, for example, in medicine, for visualizing CT scans) often utilize multicore CPUs and write raw pixels basing on completely different algorithms. That can also be true for some raytracing algorithms.

In general, the simplest way to render using CPU is to output the data to, for example, a BMP file. You can also use OpenGL to create a window, and simply write raw pixel data to a texture and render over the whole screen.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ +1 Very pertinent point about raytracing... Although strictly speaking, this is a comment, not an answer ;) Bear in mind for next time, or you may get downvoted by the angry, clamouring masses. \$\endgroup\$
    – Engineer
    May 23, 2014 at 14:52
  • \$\begingroup\$ @NickWiggill hey, I mentioned rendering to OGL texture; I could elaborate on that, I guess. \$\endgroup\$ May 23, 2014 at 14:56
  • \$\begingroup\$ Also, there are applications where you might want to rasterize stuff in software in parallel to whatever the GPU is doing. E.g. low res rasterization of scene for occlusion culling. \$\endgroup\$
    – TravisG
    May 23, 2014 at 15:01
  • \$\begingroup\$ @TravisG hm, that sounds interesing. Almost like asking for another question :) \$\endgroup\$ May 23, 2014 at 15:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .