Jump to content

  • Log In with Google      Sign In   
  • Create Account

Supporting both DirectX & OpenGL


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 KamRandle   Members   -  Reputation: 136

Like
2Likes
Like

Posted 01 January 2014 - 12:17 PM

Hello!  I re-writing my game engine from scratch and was trying to decide whether I should support DirectX & OpenGL or just DirectX?  I understand that OpenGL is cross-platform while DirectX is restricted to Windows & the Xbox.  I also have a couple more questions:

 

  1. I am developing on Windows using Visual Studio 2013 Ultimate.  If I were to implement OpenGL,  how would I build for different platforms? Example: If I wanted to build on maybe, Linux or Mac.  Would I have to build my project on that operating system?
  2. If I do develop for both API's, how should I setup my project? Should I create two separate projects? Or keep them both separated in one project using enums, and interfaces? Example: 
    enum NXAPIChoice
    {
        NXAPI_DirectX,
        NXAPI_OpenGL,
    }
    
    NXAPIChoice NXGame::DefaultAPI = NXAPIChoice::NXAPI_DirectX;
    
    void NXGame::FinalInitialize()
    {
        int sWidth = 0, sHeight = 0;
    			InitializeWindows(sWidth, sHeight);
    			SetupGPU(sWidth, sHeight);
    
    			//TODO: [HIGH PRIORITY] Initialize Input
    			//TODO: [HIGH PRIORITY] Initialize Audio
    
    			Initialize();
    
    			//TODO: [HIGH PRIORITY] Create & Start GameTime
    }
    
    void NXGame::SetupGPU(int width, int height)
    {
        //stuff...
        NXGPU::Initialize(DefaultAPI, width, height, ...other);
    }
    
    void NXGPU::Initialize(NXAPIChoice choice, int width, int height, ...other)
    {
        if (choice == NXAPIChoice::NXAPI_DirectX)
           InitializeDirect3D(width, height, D3D_FEATURE_LEVEL_11_0, ...other);
        else
           InitializeOpenGL(width, height, GL_VERSION, ...other)
    }
    

    Also if I were to develop for both, and I do have to compile my code on other platforms (linux or mac), will I be able to skip my DirectX code (assuming it would cause errors because DX is not available) and just compile GL code? Is it even worth adding support for both? Thanks! smile.png

[EDIT: Or perhaps, shall I finish my engine with DirectX first (creating empty methods for GL in the process) and just start working on GL support (fill in the empty methods) after DirectX is fully implemented?]


Edited by KamRandle, 01 January 2014 - 12:23 PM.


Sponsor:

#2 L. Spiro   Crossbones+   -  Reputation: 13595

Like
3Likes
Like

Posted 01 January 2014 - 03:59 PM

OpenGL is cross-platform

It aims to be.

Would I have to build my project on that operating system?

It is always best to do so.

Should I create two separate projects?

Yes. The days of a virtual interface and run-time selectable graphics API are long gone.

will I be able to skip my DirectX code (assuming it would cause errors because DX is not available) and just compile GL code?

If you work your macros properly.

shall I finish my engine with DirectX first

Doing them at the same time is preferred if possible given your experience with both of them.
You can’t really make good decisions for your interface without thinking about both of them at the same time, and implementing them at the same time tests your decisions.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#3 rpiller   Members   -  Reputation: 683

Like
0Likes
Like

Posted 01 January 2014 - 04:05 PM

If I was new to this stuff I would finish the engine with one, then make it again with the other, then I feel like I'd truly know how to make it the 3rd time with both. This would be if you are trying to use this as a learning experience of course. If you already know how both work well enough then this wouldn't be the way to go about it.



#4 SeanMiddleditch   Members   -  Reputation: 5851

Like
2Likes
Like

Posted 01 January 2014 - 04:28 PM

Hello!  I re-writing my game engine from scratch and was trying to decide whether I should support DirectX & OpenGL or just DirectX?  I understand that OpenGL is cross-platform while DirectX is restricted to Windows & the Xbox.  I also have a couple more questions:


More accurately, OpenGL supports Linux, OSX, Windows, iOS, and Android. DirectX supports Windows and Windows Phone. Every console - XBox included - uses a custom API. The XBox one certainly resembles DirectX pretty closely, but it's not identical. The PS3 does not support OpenGL, though it does support an infrequently-used non-standard GLES1-derived API with Cg shaders that led many to think that the PS3 uses GL (most games used the entirely proprietary libgcm, though).

The point being, if you really want to be ported to all main platforms, you _must_ support multiple graphics API. OpenGL doesn't get you as far as some might think.

Even if you use OpenGL, there's several variants you need to support (OpenGL Core on OSX, OpenGL Compatibility for most other desktop OSes, GL|ES for mobiles).
 

I am developing on Windows using Visual Studio 2013 Ultimate.  If I were to implement OpenGL,  how would I build for different platforms? Example: If I wanted to build on maybe, Linux or Mac.  Would I have to build my project on that operating system?


You can do what's called a "cross compilation" (a compiler that runs on one OS/architecture but makes binaries for a different one). There are cross compilers available for Windows, even some commercial ones that integrate with Visual Studio. This is how mobile development is done, after all, since you don't actually compile on a phone.

Usually though you'd just port your build system and then build on an actual Linux/OSX machine. You can use something like CMake or SCons to handle builds on both Windows and other platforms. You'd continue using Visual C++'s cl.exe to compile on Windows and then use GCC or Clang when compiling on OSX or Linux.
 

If I do develop for both API's, how should I setup my project? Should I create two separate projects? Or keep them both separated in one project using enums, and interfaces?


Whichever you find easier. It doesn't matter much at all.

I personally prefer building a separate static lib for each underlying API and then only building/linking the one I am currently using. The public interface is a header, with the implementation files being separate for DX, GL, or so on.

You can take that a bit further and build them as DLLs (or whatever the platform's shared object format is) and allow runtime switchable system, though really Windows is the only platform that gives you multiple realistic choices but there's no good reason to use anything but DX on Windows if you support it. I stick with static polymorphism here and don't bother with virtual interfaces; they buy you nothing.
 

Also if I were to develop for both, and I do have to compile my code on other platforms (linux or mac), will I be able to skip my DirectX code (assuming it would cause errors because DX is not available) and just compile GL code?


Just don't compile the .cpp files that have the DX code and wrap any DX-using code in #ifdef's. Pretty simple to conditionally compile code or whole modules.
 

Is it even worth adding support for both?


Depends on your goals. If you are only planning to support Windows, there is absolutely no good reason to prefer OpenGL over DirectX. If you want to support the other desktop OSes they all only support OpenGL - which Windows also supports - so there's a strong argument to be made to only spend time implementing OpenGL support. If you want to support most mobiles and desktop, you're still going to need to support multiple renderers (even if GL and GL|ES are very similar, the kinds of things you can do on the desktop don't perform well on mobile) so you still need to deal with multiple APIs and conditional code. If you want Windows Phone, you must have DX.
 

Or perhaps, shall I finish my engine with DirectX first (creating empty methods for GL in the process) and just start working on GL support (fill in the empty methods) after DirectX is fully implemented?


This is the path I recommend, assuming you mean Direct3D 11. DX11 is both easier to use than modern OpenGL (though if you stick to old deprecated fixed-function stuff the GL API is definitely simpler) and it tends to force you to learn how to think the "right way" (which applies equally well to GL) while OpenGL can easily lead you to believe that very bad things are correct (partly because GL3+ was designed around some incorrect guesses about where hardware was going and GL is still struggling under some of those poor choices and it's not obvious in GL when you're doing something you shouldn't).

There is _zero_ reason to ever use D3D10 (the only OS that supported it exclusively is Vista which also supports 111 with a service pack) and I wouldn't recommend D3D9 for any new code; if you really need to support XP in the 2-5 years it'll take at minimum for your game to come out, OpenGL will cover you and support other platforms, too.


As one last reminder, note that your graphics code is _hardly_ the only bit of platform-specific code you'll have to write. The native, low-level APIs for threading, file I/O, memory management, window management, networking, input, audio, and so on all differ between every platform. If you think you'll just be writing OpenGL code on Windows and then compiling it for Linux or OSX you might be in for a surprise. There are libraries to consider using that paper over the problem. SDL for instance covers input, windows, simple file I/O, simple audio, and so on, though only on desktops and mobiles (not any consoles that I know of). FMOD or Wwyse cover more advanced audio needs on every major gaming platform. There are networking libraries that support a wide variety of platforms. You're still likely going to have a good deal of platform-specific code outside of graphics, however, and you're going to need to make a lot of good choices when writing you're Windows-only good to have any hope of compiling on Linux or OS without a very long time spent porting.

#5 richardurich   Members   -  Reputation: 1187

Like
-2Likes
Like

Posted 01 January 2014 - 06:11 PM

I'd honestly recommend switching to OpenGL if you feel comfortable with the idea. Choosing DirectX means you either choose to limit your audience or you choose to limit your graphics options. Even if your game isn't coming out for 5 years, the same situation that exists today with Windows XP not supporting DX11 will exist with Windows 7 not supporting DX13 or whatever. OpenGL doesn't have that problem since driver manufacturers control support levels. And if there's any chance your game would sell in China or other countries that lag further behind on OS adoption, the impact of using DirectX will really, really hurt. Even in America, you're still talking about a larger group than all Mac and Linux users combined.

 

Is there a reason you're hesitant to use OpenGL? If you have tools that only work with DirectX and make your life insanely easier, obviously you should stick with DirectX. But if you're just worried OpenGL is slower or harder to use or something, just jump in and you'll find out all the FUD is unjustified.



#6 Satharis   Members   -  Reputation: 983

Like
2Likes
Like

Posted 02 January 2014 - 02:35 AM

Yes. The days of a virtual interface and run-time selectable graphics API are long gone.

Maybe I'm just being stupid but what is the reasoning behind that?

#7 Vortez   Crossbones+   -  Reputation: 2698

Like
0Likes
Like

Posted 02 January 2014 - 09:20 AM

That's what i did in my 3d engine in my signature, have a look at it if you wish (Camera.h and Camera.cpp are good example). If i want to use opengl, i simply declalre

a macro COMPILE_FOR_OPENGL, if i want dx9, COMPILE_FOR_DIRECTX9 and so on. It's a bit of pain to write at first, but it had the advantage of being

extremly simple to switch between apis. The other solution would be separate interfaces in dlls i guess.

 

Both have advantages and inconveniances, it's a double edged sword kinda deal.


Edited by Vortez, 02 January 2014 - 09:25 AM.


#8 L. Spiro   Crossbones+   -  Reputation: 13595

Like
2Likes
Like

Posted 02 January 2014 - 12:35 PM

Yes. The days of a virtual interface and run-time selectable graphics API are long gone.

Maybe I'm just being stupid but what is the reasoning behind that?

  • Performance: Reducing virtual calls is usually a micro-optimization but when it’s virtual for every SetTexture(), SetBlendMode(), etc. it can add up. Plus other locations in code that have to become run-time if/else’s instead of just macro-excluded lines of code. With other options readily available there is just no need for the overhead.
  • Code maintenance: But you still need macros around the Direct3D parts for your ports for Linux and OS X, so the macro spaghetti still exists to some degree.  On some platforms it is both and on some platforms it is only one.  It is easier to just make it, “On some platforms it is this and on others that—no cross-overs.”
  • OpenGL on Windows is generally just not a good idea.  OpenGL hasn’t had any form of consistency standards by which vendors are required to adhere until very recently, meaning it may run as expected on some cards and not on others.  Again from #2, it is just best to use Direct3D on Windows and OpenGL on everything else.
#3 is the main reason.  Direct3D is near 100% consistent on all Windows machines and OpenGL more like 80%.  On Mac OS X OpenGL is near 100% consistent.  Linux OpenGL is somewhere between.  So you always want to use Direct3D on Windows; there is never really any reason for overlapping builds.
 
 
 
L. Spiro

Edited by L. Spiro, 03 January 2014 - 07:04 AM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#9 KamRandle   Members   -  Reputation: 136

Like
0Likes
Like

Posted 02 January 2014 - 12:47 PM

Hello everyone! Thanks for the answers. I think I may just write my DirectX version first. It is being developed alongside a game that I am using it for. It will be my first game to be released. After I am finished, then I'll focus on GL versions. How many people do you guys think use Bootcamp on OS X? Thanks! :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS