• Create Account

## Support Directx 9 and Directx 11 (both)

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.

17 replies to this topic

### #1angelmu88  Members

228
Like
1Likes
Like

Posted 01 September 2012 - 07:56 AM

Hi!
I have my own graphic engine written in DirectX 9.0c. Now I want to add new DirectX 11 features like hardware tesselation, but I don't want to rewrite the whole code, I just want to support both DirectX 9 and DirectX 11. For instance, Unreal let you switch between a DirectX 9 renderer and a DirectX 11 one.
How could I do that as soon as posible?
Thanks!

### #2Hodgman  Moderators

49429
Like
4Likes
Like

Posted 01 September 2012 - 08:42 AM

but I don't want to rewrite the whole code

Any of your code that interacts with the Direct3D API will need to be re-written.

Have you written your own D3D wrapper so that only some of your code interacts with D3D directly (and the rest interacts with your wrapper), or does your whole engine use parts of D3D?

### #3angelmu88  Members

228
Like
1Likes
Like

Posted 01 September 2012 - 08:59 AM

but I don't want to rewrite the whole code

Any of your code that interacts with the Direct3D API will need to be re-written.

Have you written your own D3D wrapper so that only some of your code interacts with D3D directly (and the rest interacts with your wrapper), or does your whole engine use parts of D3D?

No, not all my code interacts with D3D. In most cases I tried to create a hierarchical model, so I have classes that interacts directly with D3D (basic classes) and classes that only use those basic classes and do not interact with D3D. is that what you mean?

### #4kubera  Members

1528
Like
1Likes
Like

Posted 01 September 2012 - 09:13 AM

It would work on legacy and future Windows versions.

### #5Hodgman  Moderators

49429
Like
3Likes
Like

Posted 01 September 2012 - 09:49 AM

In most cases I tried to create a hierarchical model, so I have classes that interacts directly with D3D (basic classes) and classes that only use those basic classes and do not interact with D3D. is that what you mean?

Yes that's what I meant. So, the lower level of your hierarchy, which deals directly with D3D9, will have to be entirely re-written
Large parts of it may be similar in the D3D11 rewrite, but none of it will likely be reusable without modification.

I would recommend separating your D3D9 and D3D11 code via #ifdefs, and only compiling for one at a time (which means you've got two different EXEs - one for people on WinXP, and one for people on Win7).
However, you could alternatively make your "wrapper" into abstract-base-classes with virtual methods if that's more attractive to you.

### #6angelmu88  Members

228
Like
1Likes
Like

Posted 01 September 2012 - 10:11 AM

In most cases I tried to create a hierarchical model, so I have classes that interacts directly with D3D (basic classes) and classes that only use those basic classes and do not interact with D3D. is that what you mean?

Yes that's what I meant. So, the lower level of your hierarchy, which deals directly with D3D9, will have to be entirely re-written
Large parts of it may be similar in the D3D11 rewrite, but none of it will likely be reusable without modification.

I would recommend separating your D3D9 and D3D11 code via #ifdefs, and only compiling for one at a time (which means you've got two different EXEs - one for people on WinXP, and one for people on Win7).
However, you could alternatively make your "wrapper" into abstract-base-classes with virtual methods if that's more attractive to you.

The good thing about the second option (virtual methods) is that I only have one .exe and I can decide wheter I use D3D9 or D3D11 at run-time, isn't it?

Actually I was looking in the Crysis 2 installation folder (Crysis 2 let you use both D3D9 and D3D11) and I found only one .exe(Crysis2.exe) and two dll's :one for D3D9 (CryRenderD3D9.dll) and another for D3D11(CryRenderD3D11.dll); So I supose they're using something similar to the "virtual method approach". Am I right?

Edited by angelmu88, 01 September 2012 - 10:13 AM.

### #7hupsilardee  Members

491
Like
3Likes
Like

Posted 01 September 2012 - 11:48 AM

In most cases I tried to create a hierarchical model, so I have classes that interacts directly with D3D (basic classes) and classes that only use those basic classes and do not interact with D3D. is that what you mean?

Yes that's what I meant. So, the lower level of your hierarchy, which deals directly with D3D9, will have to be entirely re-written
Large parts of it may be similar in the D3D11 rewrite, but none of it will likely be reusable without modification.

I would recommend separating your D3D9 and D3D11 code via #ifdefs, and only compiling for one at a time (which means you've got two different EXEs - one for people on WinXP, and one for people on Win7).
However, you could alternatively make your "wrapper" into abstract-base-classes with virtual methods if that's more attractive to you.

The good thing about the second option (virtual methods) is that I only have one .exe and I can decide wheter I use D3D9 or D3D11 at run-time, isn't it?

Actually I was looking in the Crysis 2 installation folder (Crysis 2 let you use both D3D9 and D3D11) and I found only one .exe(Crysis2.exe) and two dll's :one for D3D9 (CryRenderD3D9.dll) and another for D3D11(CryRenderD3D11.dll); So I supose they're using something similar to the "virtual method approach". Am I right?

Actually if ou think about it, this means they used the #ifdef approach, because there are 2 different DLLs. I expect this is because there is overhead with virtual functions (very very small), and they want their graphics to run as quickly as possible

### #8angelmu88  Members

228
Like
1Likes
Like

Posted 01 September 2012 - 12:15 PM

In most cases I tried to create a hierarchical model, so I have classes that interacts directly with D3D (basic classes) and classes that only use those basic classes and do not interact with D3D. is that what you mean?

Yes that's what I meant. So, the lower level of your hierarchy, which deals directly with D3D9, will have to be entirely re-written
Large parts of it may be similar in the D3D11 rewrite, but none of it will likely be reusable without modification.

I would recommend separating your D3D9 and D3D11 code via #ifdefs, and only compiling for one at a time (which means you've got two different EXEs - one for people on WinXP, and one for people on Win7).
However, you could alternatively make your "wrapper" into abstract-base-classes with virtual methods if that's more attractive to you.

The good thing about the second option (virtual methods) is that I only have one .exe and I can decide wheter I use D3D9 or D3D11 at run-time, isn't it?

Actually I was looking in the Crysis 2 installation folder (Crysis 2 let you use both D3D9 and D3D11) and I found only one .exe(Crysis2.exe) and two dll's :one for D3D9 (CryRenderD3D9.dll) and another for D3D11(CryRenderD3D11.dll); So I supose they're using something similar to the "virtual method approach". Am I right?

Actually if ou think about it, this means they used the #ifdef approach, because there are 2 different DLLs. I expect this is because there is overhead with virtual functions (very very small), and they want their graphics to run as quickly as possible

But #ifdef is a preprocessor directive, that means you have to recompile every time you change from D3D9 to D3D11 (or from D3D11 to D3D9). You could aslo have two .exe files but as I said there's only one .exe and I don't think they recompile every time you enable or disable D3D11 in the options menu. Maybe there's another option I'm missing.

Edited by angelmu88, 01 September 2012 - 01:19 PM.

### #9joeblack  Members

299
Like
1Likes
Like

Posted 02 September 2012 - 02:18 AM

But #ifdef is a preprocessor directive, that means you have to recompile every time you change from D3D9 to D3D11 (or from D3D11 to D3D9). You could aslo have two .exe files but as I said there's only one .exe and I don't think they recompile every time you enable or disable D3D11 in the options menu. Maybe there's another option I'm missing.

Hi i think they use one interface for their rendering system and derive renderers from this interface. It's really clean and easy solution, you can also switch between renderers at runtime. And about virtual method overhead -> don't bother unless you have milions of calls per frame

### #10angelmu88  Members

228
Like
0Likes
Like

Posted 02 September 2012 - 03:22 AM

Hi i think they use one interface for their rendering system and derive renderers from this interface. It's really clean and easy solution, you can also switch between renderers at runtime. And about virtual method overhead -> don't bother unless you have milions of calls per frame

Yes. I agree with you. I think they have a Renderer Interface and they store the derived renderers in CryRenderD3D9.dll and CryRenderD3D11.dll.
I also think when you start the game, depending on the renderer selected, they do something like this:

[source lang="cpp"]#include "CryRenderD3D9.h"#include "CryRenderD3D11.h"Renderer* renderer;if(D3D11enabled) renderer = new D3D11Renderer();else renderer = new D3D9Renderer();[/source]

On second thought this is not completely dynamic because you have to reset the App every time you change the renderer but you don't need two .exe files like in the #ifdef approach. I think Crysis 2 does something similar because if you change the renderer in the Crysis 2 option menu you have to reset the game.

Edited by angelmu88, 02 September 2012 - 03:36 AM.

### #11L. Spiro  Members

24838
Like
3Likes
Like

Posted 02 September 2012 - 03:56 AM

They are using #ifdef. One build compiles to a DirectX 9.0 .DLL, another to a DirectX 11 .DLL. The .EXE has no idea what DirectX is and is not linking to either of them.
The .DLL simply provides an agnostic interface for rendering and the .EXE loads one or the other at run-time based on parameters passed to it.

L. Spiro

### #12angelmu88  Members

228
Like
0Likes
Like

Posted 02 September 2012 - 04:28 AM

They are using #ifdef. One build compiles to a DirectX 9.0 .DLL, another to a DirectX 11 .DLL. The .EXE has no idea what DirectX is and is not linking to either of them.
The .DLL simply provides an agnostic interface for rendering and the .EXE loads one or the other at run-time based on parameters passed to it.

L. Spiro

Yes, I'm not saying they didn't use #ifdef to create the dlls, what I say is that the .exe does something similar to the code I posted (the .exe doesn't use #ifdef).

Edited by angelmu88, 02 September 2012 - 04:29 AM.

### #13L. Spiro  Members

24838
Like
1Likes
Like

Posted 02 September 2012 - 06:18 AM

Actually the .EXE will only have branches for which DLL to load.
Both DLL’s probably expose the same classes, so the EXE is more likely to just do a check on the input flags and branch for just the DLL, followed by something like:

Render * prRenderer = new Renderer();

L. Spiro

### #14angelmu88  Members

228
Like
0Likes
Like

Posted 02 September 2012 - 07:02 AM

Actually the .EXE will only have branches for which DLL to load.
Both DLL’s probably expose the same classes, so the EXE is more likely to just do a check on the input flags and branch for just the DLL, followed by something like:

Render * prRenderer = new Renderer();

L. Spiro

what would be the code for that flag-based branching?
I can't figure it out without using LoadLibrary, and I don't like LoadLibrary because then you have to get pointers to every function you use (In addition there's overhead when calling through a function pointer).

Edited by angelmu88, 02 September 2012 - 07:18 AM.

### #15phantom  Members

10781
Like
3Likes
Like

Posted 02 September 2012 - 07:29 AM

You'd only have to get a function pointer for creating the Renderer; after that you use the returned pointer as normal.

The cost is going to be no worse than a non-inlined function call at that point.

1. Decide which DLL to load
3. Get pointer to creation function
4. Use creation function to create renderer
5. Use returned object pointer as normal

No mess, no fuss.

### #16angelmu88  Members

228
Like
0Likes
Like

Posted 02 September 2012 - 08:03 AM

You'd only have to get a function pointer for creating the Renderer; after that you use the returned pointer as normal.

The cost is going to be no worse than a non-inlined function call at that point.

1. Decide which DLL to load
3. Get pointer to creation function
4. Use creation function to create renderer
5. Use returned object pointer as normal

No mess, no fuss.

I got it.
Thank you very much!

### #17mrheisenberg  Members

360
Like
0Likes
Like

Posted 04 September 2012 - 01:11 PM

but I don't want to rewrite the whole code

Any of your code that interacts with the Direct3D API will need to be re-written.

Have you written your own D3D wrapper so that only some of your code interacts with D3D directly (and the rest interacts with your wrapper), or does your whole engine use parts of D3D?

wait what about feature levels? http://msdn.microsoft.com/en-us/library/windows/desktop/ff476876(v=vs.85).aspx

### #18Hodgman  Moderators

49429
Like
1Likes
Like

Posted 04 September 2012 - 11:03 PM

but I don't want to rewrite the whole code

Any of your code that interacts with the Direct3D9 API will need to be re-written [for the Direct3D11 API].

wait what about feature levels? http://msdn.microsoft.com/en-us/library/windows/desktop/ff476876(v=vs.85).aspx

Features levels are a part of the D3D11 API that allows you to use that API on older hardware (e.g. DX9 compatible hardware).
If you wanted to use the "9 on 11" feature level, you'd still have to port your renderer over to the D3D11 API first.

Also, for some stupid reason, there's no DX11 feature-level that corresponds to SM3.0, so if you want to support SM3 (360/PS3 level hardware) then you've still got to maintain the DX9 version of your renderer :/

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.