Game Development Community

Torque 3D DirectX11 Port - DirectX 9 refactor

by Lopuska · in Torque 3D Professional · 03/11/2014 (6:23 pm) · 81 replies

Hi everybody!

I'm Lopuska, I'm from Italy.
I'm taking some of my time to try a conversion of Torque3D from DirectX9 to DirectX11.
This is my current status, there's still a lot of work to do...
I don't have many times in this period, if anyone would like to help me for this big task it would be great!

Before that, I've done a lot of code refactoring on D3D9 layer, also to make porting to D3D11 easier.
Here there are change list about DirectX9 refactoring:

- Better *.cpp organization / code cleaning
- Removed D3DX calls dependencies. (deprecated by Microsoft)
- Removed D3D9 XBOX360 old reference.
- Shader/Constant buffer refactor with a custom parser.
- gfxD3D9Shader total refactor.
- Mipmap autogen fixes
- Minor fixes under cubemap
- Speed improved on texture loading
- Removed 24bit format (deprecated since DX10) (i convert it to 32bit)
There is a better explanation about it on code as comment.
- Some performance improvement (especially under a lot of dynamic_cast used in wrong context)
- Removed fixed pipeline (deprecated by Microsoft since DX10)
- Many other minor bug fixed.

With this patch, you don't need DirectX SDK installed anymore. It's deprecated as well. You just need Visual Studio with its windows SDK.

Everything is inside d3d9.h/d3dcompiler.h -> d3d9.lib/d3dcompiler.lib
So, to try this patch, remove from linker input d3dx9.lib and dxerr.lib and DX_SDK includes to avoid conflicts and errors during compile/link.
(You can also remove d3d9.lib input linker because since this is Microsoft specific code, I've included it from #pragma directive inside *.cpp)

To remove fixed pipeline, I've used the same method used by Luis Anton Rebollo.
So if the community want to merge this part of code with Luis' work, it will be very easy because from high-level GFX layer you just need to make the same calls that Luis already do for OpenGL.

With the actual DirectX9 implementation, the situation should be:

//from precipitation.cpp

if(RenderDevice == OpenGL) //ugly!
GFX->setupGenericShaders(GFXDevice::GSTexture);
else if(RenderDevice == Direct3D9)
GFX->disableShaders();

With this Patch we have just:
GFX->setupGenericShaders(GFXDevice::GSTexture); //Now also DirectX use shaders without FFP

The main branch is:
https://github.com/Lopuska/Torque3D/tree/D3D9_D3D11_R%26D

For DirectX11 situation, I'll start a new thread.

byee!

Lopuska
#21
03/13/2014 (12:25 pm)
I am using VS 2012 and Windows 8 x64

I replaced source files from the zip, removed from the includes the directory of DX_SDK and from linker the d3dx9.lib, dxerr.lib and d3d9.lib dependencies.

Also replaced
GFX->disableShaders();
to
GFX->setupGenericShaders();
with find/replace tool.

After compile if you get an error at startup about missing D3Dcompiler_46.dll just copy it to your game directory.
It can be found in C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin
#22
03/13/2014 (1:38 pm)
Yeah I did all that, no luck. I'm guessing you're having an easier time because you're using Windows 8. If I could just get this to work I could do up a tutorial and upload the changes to github.
#23
03/13/2014 (2:36 pm)
Dang windows 8 ;P...Looks like I may have to upgrade from windows 7, unless someone gets it working and share how it was done!
I should have sometime this weekend and I may give this a try.
#24
03/13/2014 (2:45 pm)
If you guys are having issues with getting the windows 8 SDK to work. For now can try using the DX SDK(June 2010) version and just replace a couple lines of code to make it compatible.

Find
res = D3DCompileFromFile(realPath.getFullPath().utf16(), defines, smD3DInclude, "main", target, flags, 0, &code, &errorBuff);

in D3D9Shader.cpp in the _compileShader function.

replace it with
// Add a #line pragma so that error and warning messages
// returned by the HLSL compiler report the right file.
String linePragma = String::ToString( "#line 1 \"%s\"\r\n", realPath.getFullPath().c_str() );
      U32 linePragmaLen = linePragma.length();
	  
	  FrameAllocatorMarker fam;
	  char* buffer = NULL;
     
	  U32 bufSize = s.getStreamSize();
	   
      buffer = (char *)fam.alloc( bufSize + linePragmaLen + 1 );
      dStrncpy( buffer, linePragma.c_str(), linePragmaLen );
      s.read( bufSize, buffer + linePragmaLen );
      buffer[bufSize+linePragmaLen] = 0;

	  

	  res =   D3DCompile(buffer, bufSize+linePragmaLen,NULL , defines, smD3DInclude, "main", target, flags, 0, &code,
		  &errorBuff);
#25
03/13/2014 (3:20 pm)
Hi all! Sorry for my late reply...
I didn't realize that it would become a valued thread :D

@ZeroFault
Good job! Yes, it seems that D3DCompileFromFile it's a new d3d call from last Windows 8/8.1 SDK.
I've already replaced with the traditional D3DCompile.

@ZeroFault, thanasimos:
Can you show me a screenshot of the brush not rendered correctly?
I can fix that immediately.

@All:
Replacing D3DCompileFromFile with D3DCompile, you should be able to compile also from Windows 7 and Visual Studio 2010 (With last one, obviously you need DirectX SDK installed separately).

I've also made some other "stupid" fix.

I'm working on DirectX11... but I need probably some help.
Is there any programmer that can help me for the conversion?
I have already 40% of job done.
#26
03/13/2014 (4:16 pm)
@Anis
screenshot

looks like the grid renders correctly where the mouse is. The outline of the circle renders away from the grid.
#27
03/13/2014 (5:09 pm)
Here the code up on github including the latest fix and signmotions VS2013 fixes:

github.com/andr3wmac/Torque3D/tree/d3d9-refactor

Compiles and works fine on my end, thanks for the help guys. Haven't had time to test it much yet, will report back with my findings as soon as possible.
#28
03/15/2014 (12:12 am)
Okay, I think I fixed the brush not rendering correctly.
In guiTSControl.cpp at around line 123 in _drawLine function you'll find
GFXStateBlockDesc desc;
desc.setCullMode(GFXCullNone);
desc.setZReadWrite(false);
desc.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
GFX->setStateBlockByDesc( desc );

change it to this

GFXStateBlockDesc desc;
desc.setCullMode(GFXCullNone);
desc.setZReadWrite(false);
desc.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
GFX->setupGenericShaders(); // D3D9Refractor fix
GFX->setStateBlockByDesc( desc );

let me know if this works for you guys.
#29
03/15/2014 (1:11 am)
Hi Anis :D

A really interesting project, thanks for all the work.

There has to start talking about the necessary changes in the GFX layer for a future with DX11 and OpenGL4.
#30
03/15/2014 (4:15 am)
@ZeroFault

Works perfectly, thanks!
#31
03/15/2014 (8:01 am)
@Luis if openGL 4 ever gets started, ensure that you use openGL 4.0 as some machines don't go far past that. Even some of the new mac computers can now run 4.1 which is awesome, but I wouldn't go to like 4.3 or something.
#32
03/15/2014 (8:28 am)
@ZeroFault:

Good stuff, I added the fix to my branch.
#33
03/15/2014 (11:18 am)
yes! i forgot to include guiTSControl.cpp in the package.
Thanks to catch that! :D
#34
03/15/2014 (12:09 pm)
Ok, i post here the ultimate d3d9 patch.
All bugs reported by this thread have been resolved.
I've also resolved many other issues.

1) Download patch:
https://dl.dropboxusercontent.com/u/65607302/D3D9_FINAL_REFACTOR_D3D9.rar

2) Remove D3D9 folder inside source/gfx/
Install patch replacing code and adding the new shaders.

3) Open Visual Studio Remove D3D9 content from visual studio filter and add all content from source/gfx/D3D9/

4) Search on all solution:

GFX->disableShaders();

and replace all founds with:

GFX->setupGenericShaders();

5) Compile and launch! :D

@Andrew:
Please can you update the d3d9-refactor repository with this final patch?

Thanks =)
#35
03/15/2014 (12:15 pm)
@Anis
Thanks for the update! Also, if you need help with the DX11 port I would be happy to help.
#36
03/15/2014 (12:52 pm)
That is so awesome Anis! :D
#37
03/16/2014 (7:47 am)
@ Jeff h I disagree with you on your statement on Opengl. I would go all the way to 4.3, with OGL you can have it not use features that are not supported on that platform.


Why favor supporting DX11 and not OGL 4.3 ? You logic is not sound as you seem to favor one platform over the other.

Now as for the Mac users and Linux open source driver users only being able to use 4.1 and Mesa 3.2 ( and some of the 4.0 features) that is their own fault for choosing and using those platforms.

Now for me I don't use Mesa on Linux, I use the Nvidia binary blob that supports OGL 4.3. Most Linux gamers that I know use binary blobs from the graphic card vendors to game with.
#38
03/16/2014 (12:25 pm)
Latest patch has been pushed. I also altered the project generation files to remove the old references to DX SDK and replaced it with Windows 8 SDK paths. XAudio has been temporarily disabled as well.

You should now be able to use the project generator and it should compile without any alterations needed.

Here's the url to the repo again:
github.com/andr3wmac/Torque3D/tree/d3d9-refactor
#39
03/19/2014 (8:05 am)
A new BIG update is coming on the repo for DirectX9 layer... ;)