Comparison of OpenGL and Direct3D explained

This article compares two computer graphics APIs:

  1. Direct3D is a proprietary API designed by Microsoft Corporation for hardware 3D acceleration on the Windows platform
  2. OpenGL is an open standard API that provides a number of functions for the rendering of 2D and 3D graphics and is available on most modern operating systems. If hardware 3D acceleration is present, OpenGL can use it.

OpenGL and Direct3D are both implemented in the display driver.

Following is a comparison of the two APIs, structured around various considerations mostly relevant to game development.

Portability

Direct3D is officially implemented only on Microsoft’s Windows’ family of operating systems, including embedded versions used in the Xbox family of video game consoles and Sega’s Dreamcast. Several partially functional ports of the Direct3D API have been made by Wine, a project to port common Windows APIs to Unix-like operating systems, and Cedega, a proprietary fork of Wine, but this is impeded due to the interdependence of DirectX on many other components of Windows, and because Direct3D’s proprietary nature requires reverse engineering, a difficult process.

OpenGL has implementations available across many platforms including Microsoft Windows, UNIX-based systems such as Mac OS X, Linux, and the PlayStation 3 game console. Variants of OpenGL run on the Nintendo GameCube and Wii game consoles and the Nintendo DS[1] and PlayStation Portable handheld systems. OpenGL was chosen as the primary graphics library for iPhone, Android (mobile device platform) and Symbian OS. With the exception of Windows and the Xbox, all operating systems that allow for hardware-accelerated 3D graphics utilize OpenGL as their primary 3D graphics API.

Microsoft’s OpenGL driver, bundled with Windows versions before Windows Vista, provides no hardware acceleration or direct support for extensions. These versions of Windows thus require that users install installable client drivers (ICDs) developed by GPU manufacturers for OpenGL hardware support. These ICDs are, in virtually all cases, bundled with the standard driver download package from the hardware vendor (IHV), so installing recent graphics drivers is sufficient to provide hardware OpenGL support.

Windows Vista provides for three OpenGL implementations. The first maps OpenGL calls to Direct3D ones. This allows for hardware acceleration of OpenGL through version 1.4 from a standard install, but features of versions after 1.4 must be accessed as extensions. The second uses legacy ICDs available for Windows XP. This will disable the Aero desktop but otherwise functions as expected. The third is a full implementation that will work alongside the desktop and still allow access to OpenGL 2 on the GPU.[2]

Ease of use

Direct3D

The first version of Direct3D elicited widespread criticism because even simple operations, such as state changes, required the creation and submission of objects called “execute buffers”. In contrast, in OpenGL most state changes can be performed with single function calls. The Direct3D model frustrated many programmers. The most famous complaint was probably made by high-profile game developer John Carmack in the .plan file in which he urged Microsoft to abandon Direct3D in favor of OpenGL.[3] Chris Hecker made a similar request in an “Open Letter to Microsoft” in the April-May 1997 issue of Game Developer Magazine.[4]

Version 5 (the second version, named to reflect its release as part of DirectX 5) replaced execute buffers with the new DrawPrimitive API, but it was still considered cumbersome. Chris Hecker’s “Open Letter to Microsoft” referred to DrawPrimitive as “an immature and poorly-designed clone of OpenGL that’s missing some of the architectural decisions that make OpenGL fast.”[4]

Despite the controversy, Microsoft continued to evolve the API. A detailed history of the releases and the features added is given on the Direct3D page.

Some former critics of Direct3D acknowledge that now Direct3D is at least comparable to OpenGL in terms of capabilities and ease of use. In January 2007, John Carmack said that “…DX9 is really quite a good API level. Even with the D3D side of things, where I know I have a long history of people thinking I’m antagonistic against it. Microsoft has done a very, very good job of sensibly evolving it at each step—they’re not worried about breaking backwards compatibility—and it’s a pretty clean API. I especially like the work I’m doing on the 360, and it’s probably the best graphics API as far as a sensibly designed thing that I’ve worked with.”[5]

Some design features of Direct3D have remained unchanged since the first version, most notably its reliance on Microsoft’s COM. One advantage of using COM is that the API can be used in any COM-aware language, notably C++, Delphi, C# and Visual Basic .NET.

OpenGL

OpenGL is a specification based on the C programming language. It is built around the concept of a state machine, though more recent OpenGL versions have transformed it into much more of an object based system. Though the specification is built on C, it can be implemented in other languages as well. As an API, OpenGL does not depend on any particular language feature, and can be made callable from almost any programming languages with the proper bindings. Such bindings exist for VB, Ada, Pascal, Delphi, Python, Perl, Haskell, Java and C#, just to name a few.

Comparison

In general, Direct3D is designed to be a 3D hardware interface. The feature set of Direct3D is derived from the feature set of what hardware provides. OpenGL, on the other hand, is designed to be a 3D rendering system that may be hardware accelerated. These two APIs are fundamentally designed under two separate modes of thought. The fact that the two APIs have become so similar in functionality shows how well hardware is converging into user functionality.

Even so, there are functional differences in how the two APIs work. Direct3D expects the application to manage hardware resources; OpenGL makes the implementation do it. This tradeoff for OpenGL decreases difficulty in developing for the API, while at the same time increasing the complexity of creating an implementation (or driver) that performs well. With Direct3D, the developer must manage hardware resources independently; however, the implementation is simpler, and developers have the flexibility to allocate resources in the most efficient way possible for their application.

Until about 2005, another functional difference between the APIs was the way they handled rendering to textures: the Direct3D method (SetRenderTarget) is convenient, while previous versions of OpenGL required the manipulation of P-buffers (pixel buffers). This was cumbersome and risky: if the programmer’s codepath was different from that anticipated by the driver manufacturer, the code would have fallen back to software rendering, causing a substantial performance drop. However, widespread support for the “frame buffer objects” extension, which provided an OpenGL equivalent of the Direct3D method, successfully addressed this shortcoming, and the “render target” feature of OpenGL brought OpenGL up to par with Direct3D in this respect.

Outside of a few minor functional differences which have mostly been addressed over the years, the two APIs provide nearly the same level of functionality.

Performance

Shortly after the establishment of both Direct3D and OpenGL as viable graphics libraries, Microsoft and SGI engaged in what has been called the “API Wars”. Much of the argument revolved around which API offered superior performance. This question was relevant due to the very high cost of graphics accelerators during this time, which meant the consumer market was using software renderers implemented by Microsoft for both Direct3D and OpenGL.

Early debate

Microsoft had marketed Direct3D as faster based on in-house performance comparisons of these two software libraries. The performance deficit was blamed on the rigorous specification and conformance required of OpenGL. This perception was changed at the 1996 SIGGRAPH (Special Interest Group on Computer Graphics) conference. At that time, SGI challenged Microsoft with their own optimized Windows software implementation of OpenGL called CosmoGL which in various demos matched or exceeded the performance of Direct3D. For SGI, this was a critical milestone as it showed that OpenGL’s poor software rendering performance was due to Microsoft’s reference OpenGL implementation, and not to design flaws in OpenGL itself.

On the other hand, software rendering by the 3D API was largely irrelevant for both Direct3D and OpenGL applications. Not many DirectX applications used Direct3D’s software rendering, preferring to perform their own software rendering using DirectDraw's facilities to access the display hardware. As for OpenGL applications, hardware support was expected, and the hardware was so much faster that software fallback by the OpenGL application constituted a rude surprise to the OpenGL developer.

In any case, by the time SGI had demonstrated that OpenGL software rendering performance could be competitive with that of Direct3D, software rendering was fast becoming irrelevant due to the widespread availability of inexpensive 3D graphics hardware. By 1998, even the much-maligned S3 Virge was substantially faster than the fastest Pentium II running Direct3D’s MMX rasterizer.

Mode switching

A more substantive and modern performance difference arises because of the structure of the hardware drivers provided by hardware developers. Under DirectX, IHV drivers are kernel-mode drivers installed into the operating system. The user-mode portion of the API is handled by the DirectX runtime provided by Microsoft. Under OpenGL however, the IHV driver is broken into two parts: a user-mode portion that implements the OpenGL API, and a kernel-mode driver that is called by the user-mode portion.

The reason this is an issue is because calling kernel-mode operations from user-mode requires performing a system call (i.e. making the CPU switch to kernel mode). This is a slow operation, taking on the order of microseconds to complete.[6] During this time, the CPU is unable to perform any operations. As such, a performance optimization would be to minimize the number of times this switching operation must be performed. For example, if the GPU’s command buffer is full of rendering data, the API could simply store the requested rendering call in a temporary buffer and, when the command buffer is close to being empty, it can perform a switch to kernel-mode and add a number of stored commands all at once. This is known as marshalling.

Because Direct3D IHV drivers are kernel-mode, and the user-mode code is out of the IHV’s hand, there is no chance for such optimizations to occur. Because the Direct3D runtime, the user-mode portion that implements the API, cannot have explicit knowledge of the driver’s inner workings, it cannot effectively support marshalling. This means that every D3D call that sends commands to the hardware must perform a kernel-mode switch. This has led to a number of behaviors with regard to using D3D, the most important being the need for submitting large batches of triangles in one function call.[7]

Since OpenGL’s IHV drivers have a user-mode component to them, IHVs have the ability to implement marshalling, thus improving performance. There is still kernel-mode switching, but the theoretical maximum number of switches under OpenGL implementations is simply equal to the Direct3D standard behavior.

Direct3D 10, the release included with Windows Vista[8], allows portions of drivers to run in user-mode, thus allowing IHVs to implement marshalling, thus bringing the two back into relative performance parity. The Mac OS X OpenGL system implements a very similar system, where IHVs implement a simpler version of the OpenGL API (with both user and kernel mode components), and Apple’s additions to the runtime provide the direct interface to the user code, as well as some basic work to make IHVs’ jobs easier.

Structure

OpenGL, originally designed for then-powerful SGI workstations, includes a number of features, like stereo rendering and the "imaging subset", that are of limited utility for games. The API as a whole contains about 250 calls, but only a subset of perhaps 100 are useful for game development. However, no official gaming-specific subset was ever defined. MiniGL, released by 3Dfx as a stopgap measure to support glQuake, might have served as a starting point, but additional features like stencil were soon adopted by games, and support for the entire OpenGL standard continued. Today, workstations and consumer machines use the same architectures and operating systems, and so modern incarnations of the OpenGL standard still include these features, although only special workstation-class video cards accelerate them.

Extensions

The OpenGL extension mechanism is probably the most heavily disputed difference between the two APIs. OpenGL includes a mechanism where any driver can advertise its own extensions to the API, thus introducing new functionality such as blend modes, new ways of transferring data to the GPU, or different texture wrapping parameters. This allows new functionality to be exposed quickly, but can lead to confusion if different vendors implement similar extensions with different APIs. Many of these extensions are periodically standardized by the OpenGL Architecture Review Board (ARB), and some are made a core part of future OpenGL revisions.

On the other hand, Direct3D is specified by one vendor (Microsoft) only, leading to a more consistent API, but denying access to vendor-specific features. NVIDIA’s UltraShadow technology,[9] for instance, is not available in the stock Direct3D APIs at the time of writing. It should be noted that Direct3D does support texture format extensions (via FourCC). These were once little-known and rarely used, but are now used for DXT texture compression.

When graphics cards added support for pixel shaders (known on OpenGL as “fragment programs”), Direct3D provided a single “Pixel Shader 1.1” (PS1.1) standard which the GeForce 3 and up, and Radeon 8500 and up, claimed compatibility with. Under OpenGL the same functionality was accessed through a variety of custom extensions.

In theory, the Microsoft approach allows a single code path to support both brands of card, whereas under OpenGL the programmer had to write two separate systems. In reality, though, because of the limits on pixel processing of those early cards, Pixel Shader 1.1 was nothing more than a pseudo-assembly language version of the NVIDIA-specific OpenGL extensions. For the most part, the only cards that claimed PS 1.1 functionality were NVIDIA cards, and that is because they were built for it natively. When the Radeon 8500 was released, Microsoft released an update to Direct3D that included Pixel Shader 1.4, which was nothing more than a pseudo-assembly language version of the ATI-specific OpenGL extensions. The only cards that claimed PS 1.4 support were ATI cards because they were designed with the precise hardware necessary to make that functionality happen. In terms of early pixel shaders, Direct3D’s attempt at a single code path fared no better than the OpenGL mechanism.

Fortunately, this situation only existed for a short time under both APIs. Second-generation pixel shading cards were much more similar in functionality, with each architecture evolving towards the same kind of pixel processing conclusion. As such, Pixel Shader 2.0 allowed a unified code path under Direct3D. Around the same time OpenGL introduced its own ARB-approved vertex and pixel shader extensions (GL_ARB_vertex_program and GL_ARB_fragment_program), and both sets of cards supported this standard as well.

Users

Professional graphics

OpenGL has always seen more use in the professional graphics market than DirectX, while DirectX is used mostly for computer games. (The term professional is used here to refer to the professional production and display of graphics, such as in computer animated films and scientific visualisation, as opposed to games where the graphics produced are for the end user’s personal, rather than professional, use.)

At one point many professional graphics cards only supported OpenGL, however, now any professional card which works on the Windows platform also supports Direct3D.

The reasons for OpenGL’s advantage in the professional market is partly historical. Many professional graphics applications (for example, Softimage|3D, Alias PowerAnimator) were originally written in IRIS GL for high-end SGI workstations, then ported to OpenGL. Even long after SGI ceased to dominate the market, many professional graphics cards only supported OpenGL.

The many extra features of OpenGL that were previously mentioned as not useful for game development are also a factor in OpenGL’s professional market advantage, because many of them are useful in professional applications.

The other reason for OpenGL’s advantage is marketing and design. DirectX is a set of APIs that were not marketed towards professional graphics applications. Indeed, they were not even designed with those applications in mind. DirectX was an API designed for low-level, high-performance hardware access for the purpose of game development. OpenGL is a much more general purpose 3D API, so it provides features that aren’t necessarily exclusive towards any particular kind of user.

Gaming

In the earliest days of 3D accelerated gaming, performance and reliability were key benchmarks and several 3D accelerator cards competed against each other for dominance. Software was written specifically for a particular brand of graphics card. However, over the years, OpenGL and Direct3D emerged as software layers above the hardware, mainly because of industry support for a cross-hardware graphics library. Competition between the two rose as each game developer would choose either one or the other. In the 1990s, OpenGL dominated due to a better design of the interface. In 2000s game developers began to favor Direct3D also only because of the dominance of the Windows operating system in the gaming market. In the second half of 2000s, with the appearance of the Sixth and Seventh generation console, the desktop gaming market is declining in favor of consoles. Because of the diverse set of platforms and APIs, the need for a well-supported cross-platform library is driving increased adoption of OpenGL.

See also

External links

Notes and References

  1. http://devkitpro.sourceforge.net/devkitProWiki/libnds/a00036.html devkitPro videoGL reference
  2. Web site: more comments…. Kam. VedBrat. MSDN Blogs. 2006-02-22. 2008-06-07.
  3. http://www.bluesnews.com/archives/carmack122396.html John Carmack’s 12/23/96
  4. http://chrishecker.com/images/3/33/Gdmogl.pdf "An Open Letter to Microsoft" by Chris Hecker
  5. http://www.gameinformer.com/News/Story/200701/N07.0109.1737.15034.htm CES 2007: John Carmack And Todd Hollenshead Speak
  6. Book: Spinellis, Diomidis. Code Quality: The Open Source Perspective. 2006. Addison Wesley. 0-321-16607-8. 182–183.
  7. http://nexe.gamedev.net/directknowledge/default.asp?p=Batching Is it better to use one call to DrawPrimitive or many?
  8. http://msdn.microsoft.com/en-us/library/bb173477.aspx Graphics APIs in Windows Vista
  9. http://www.nvidia.com/object/feature_ultrashadow.html UltraShadow Technology