Skip to main content
GameDev.net gamedev.net
🔒 Locked

How to use the Include class?

Started by MrSparkle Sep 4, 2006 at 9:23 AM 4 replies 850+ views
Original Post
MrSparkle
MrSparkle
Hi, the Effect.FromFile() method takes an argument called includeFile:

C#

public static Effect FromFile(
    Device device,
    string sourceDataFile,
    Include includeFile,
    string skipConstants,
    ShaderFlags flags,
    EffectPool pool
);
 
C++ 

public: 
static Effect^ FromFile(
    Device^ device,
    String^ sourceDataFile,
    Include^ includeFile,
    String^ skipConstants,
    ShaderFlags flags,
    EffectPool^ pool
); 
The Include class is an abstract class so I have to use my own derived class. So far, I understand this concept, but how do I write my own Include class? I have to override the Open() method, so what should this method do? Can anyone help me on how to implement my own Include class? Thanks, Christian
Nik02
Nik02
You should fill the ppData and pBytes of the Open function with your include content in ASCII and it's size in bytes, respectively.
Niko Suni
MrSparkle
MrSparkle
The compiler complains that there are no such variables in this context...

Do I really have to write my own method to read the content of the include files?

What if I use more than one #include file in the shader? How does DirectX identify the #include file used at a specific line in the shader? Can someone explain how this works internally?

I don't understand this at all, so any help is appreciated.

Christian
sirob
sirob
Like Namethatnobodyelsetook explained in the last topic you posted, the Open method gets called once* per file that gets included in the hlsl source.
Whenever this happens, you use the several parameters Open takes, to help you understand which file you're supposed to load. This method is then expected to load the data from where it is (either on disk, or in a package file, or wherever), and point the provided pointers at it.

When Close is called, this data is to be released.

* note that according to what Namethatnobodyelsetook said, Open might be called multiple times for the same file.

Hope this helps.
Sirob Yes.» - status: Work-O-Rama.
Namethatnobodyelsetook
Namethatnobodyelsetook
It's much like asking how "fopen" knows which file to open, because it's only one function. You call it once per file, and tell it which file you'd like to open via arguments. CreateWindow can open multiple windows of various types by calling it multiple times with different arguments.

In much the same way, the effects compiler calls your Include.Open function whenever it needs to read an include file, and Include.Close when it's done and you it's safe for you to free the memory you allocated.

Most games don't leave files loose on disk, they're packed into one file, often compressed. This is why it's up to you to read the data... so you can store and read the data any way you want. It's also why functions like D3DXCreateTextureFromFileInMemory exist. It would be somewhat useful for D3DX to have a generic standard Include that just opened file on disk, but it's so trivial to write that they didn't bother.

Could someone who knows C# whip together a quick little sample Include for MrSparkle? I could write a C++ version if you think it would be helpful for you.
MrSparkle
MrSparkle
Thank you all for helping me! I do now understand what's going on, and it's really not difficult to do this in C#:

class IncludeFiles : Include{  public override Stream Open(IncludeType includeType, string filename)  {      FileStream includeFile = new FileStream(filename, FileMode.Open);    return includeFile;  }} // class IncludeFile


That's all ;)

The only thing that's strange is that Effect.FromFile() also calls IncludeFile.Open() for the original shader file, not only for the includes. I guess, I'll find a workaround for this!

Thanks again!
Christian

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.