• Advertisement
Sign in to follow this  

Support Directx 9 and Directx 11 (both)

This topic is 1965 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
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?

Share this post


Link to post
Share on other sites

[quote name='angelmu88' timestamp='1346507798' post='4975431']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?
[/quote]

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?

Share this post


Link to post
Share on other sites
You would also link the program against both DX in Delay Download DLL or load them by LoadLibrary.
It would work on legacy and future Windows versions.

Share this post


Link to post
Share on other sites

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 sad.png
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 [font=courier new,courier,monospace]#ifdef[/font]s, 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.

Share this post


Link to post
Share on other sites

[quote name='angelmu88' timestamp='1346511579' post='4975439']
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 sad.png
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 [font=courier new,courier,monospace]#ifdef[/font]s, 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.
[/quote]

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

Share this post


Link to post
Share on other sites

[quote name='Hodgman' timestamp='1346514567' post='4975460']
[quote name='angelmu88' timestamp='1346511579' post='4975439']
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 sad.png
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 [font=courier new,courier,monospace]#ifdef[/font]s, 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.
[/quote]

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?
[/quote]

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

Share this post


Link to post
Share on other sites

[quote name='angelmu88' timestamp='1346515907' post='4975466']
[quote name='Hodgman' timestamp='1346514567' post='4975460']
[quote name='angelmu88' timestamp='1346511579' post='4975439']
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 sad.png
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 [font=courier new,courier,monospace]#ifdef[/font]s, 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.
[/quote]

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?
[/quote]

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
[/quote]

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

Share this post


Link to post
Share on other sites


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

Share this post


Link to post
Share on other sites


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
[/quote]

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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
2. Load DLL
3. Get pointer to creation function
4. Use creation function to create renderer
5. Use returned object pointer as normal

No mess, no fuss.

Share this post


Link to post
Share on other sites

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
2. Load DLL
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!

Share this post


Link to post
Share on other sites

[quote name='angelmu88' timestamp='1346507798' post='4975431']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?
[/quote]

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

Share this post


Link to post
Share on other sites

[quote name='Hodgman' timestamp='1346510528' post='4975436']
[quote name='angelmu88' timestamp='1346507798' post='4975431']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].
[/quote]wait what about feature levels? http://msdn.microsoft.com/en-us/library/windows/desktop/ff476876(v=vs.85).aspx
[/quote]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 :/

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement