Advertisement

ID3DInclude Interface Example

Started by April 22, 2018 01:38 PM
6 comments, last by fs1 6 years, 9 months ago

I have been trying to see how the ID3DInclude, and how its methods Open and Close work.

I would like to add a custom path for the D3DCompile function to search for some of my includes.

I have not found any working example. Could someone point me on how to implement these functions? I would like D3DCompile to look at a custom C:\Folder path for some of the include files.

Thanks

I've used it a long time ago so maybe can't remember well. Basically, Open and close are callback functions which get invoked when compiling your shaders with the D3DCompile API. When entering the Open function, you will receive a filename string in pFileName parameter, while also a file type in the include type parameter. The file type will be D3D_INCLUDE_SYSTEM if your include looks like this:


#include <myHeader.h>

The file type will be D3D_INCLUDE_LOCAL when your include looks like this:


#include "myHeader.h"

In both cases, you must load the file by hand, but the file type gives you a hint where to look for the file. What the system path or the local path will be depends on you, usually the local path would be the current working directory and any folder in there which was specified if you have something like "Additional include directories". The system path also depends on you, probably your engine source directory, or anything... So you load your file with eg. std::ifstream, read all of the data and if everything is successful, you will return the data in the ppData function argument and the data size in bytes in the pBytes function argument and the function should return S_OK.

Else, let your function return E_FAIL if you couldn't load the file.

If everything was successful, the Close function will be invoked at the end of shader compilation and you can free the data.

Apologies but I don't have code example of this, but should be fairly straight forward.

Btw, maybe it's not what you want, but you can also just call fxc.exe and include paths will be handled for you, you can specify additional include directories with -I command line argument.

Advertisement

Thanks @turanszkij for your explanation.

I will play with it and let you know how it goes.

thanks

I have some code here that uses it to add a custom include path, in case you need a reference. And just a heads up: the interface is totally different for dxc (the new open source shader compiler for DX12), but it also has a simple way to add an include path like you would for a C++ compiler.

On 4/23/2018 at 4:01 AM, MJP said:

I have some code here that uses it to add a custom include path, in case you need a reference. And just a heads up: the interface is totally different for dxc (the new open source shader compiler for DX12), but it also has a simple way to add an include path like you would for a C++ compiler.

Great thanks so much for the example. Appreciate all the help, it is working now.

Just one more question. In the D3D_INCLUDE_SYSTEM case, how do you handle the file path if you have different sub-folders to search in? Do you need to work on a loop to find the right file in the whole sub-folders system?

Thanks

Yes, you would have to loop over all possible include directories until you find one that has the file you're looking for. This is basically how C/C++ compilers work with multiple include directories. Just be careful if a file exists in two different include directories, since you'll probably end up resolving the include directory that's earlier in the list (C/C++ compilers work this way too).

Advertisement

Thanks @MJP for all the clarification.

Regards

This topic is closed to new replies.

Advertisement