dotnet / Silk.NET

The high-speed OpenGL, OpenCL, OpenAL, OpenXR, GLFW, SDL, Vulkan, Assimp, WebGPU, and DirectX bindings library your mother warned you about.
https://dotnet.github.io/Silk.NET
MIT License
3.87k stars 377 forks source link

[ID3DInclude] Custom include handler for D3D.Compile2() #2205

Open d3-denis-zaporozhets opened 1 month ago

d3-denis-zaporozhets commented 1 month ago

Hi. I am trying to implement custom include handler for D3DCompiler. In C++ there is ID3DInclude which developer could inherit from and then override methods, but here in C# there is no such possibility because of how structs work in C#.

So, my question is - how to implement custom include handler in Silk.Net?

Sizaar commented 1 week ago

You don't need to implement your own handler if all you need is just a simple include behavior, just pass (ID3DInclude*)1 as value for pInclude argument in Compile/Compile2.

If you need more elaborate handler logic you can use this:

static unsafe int Open(ID3DInclude* include, D3DIncludeType IncludeType, byte* pFileName, void* pParentData, void** ppData, uint* pBytes)
{
    // Your implementation here
}

static unsafe int Close(ID3DInclude* include, void* pData)
{
    // Your implementation here
}

var lpVtbl = stackalloc void*[2];

delegate*<ID3DInclude*, D3DIncludeType, byte*, void*, void**, uint*, int> d0 = &Open;
lpVtbl[0] = d0;

delegate*<ID3DInclude*, void*, int> d1 = &Close;
lpVtbl[1] = d1;

var include = new ID3DInclude(lpVtbl);

You can read more about Open/Close functions here: https://learn.microsoft.com/en-us/windows/win32/api/d3dcommon/nf-d3dcommon-id3dinclude-open https://learn.microsoft.com/en-us/windows/win32/api/d3dcommon/nf-d3dcommon-id3dinclude-close