smourier / DirectN

Direct interop Code for .NET Framework, .NET Core and .NET 5+ : DXGI, WIC, DirectX 9 to 12, Direct2D, Direct Write, Direct Composition, Media Foundation, WASAPI, CodecAPI, GDI, Spatial Audio, DVD, Windows Media Player, UWP DXInterop, WinUI3, etc.
MIT License
311 stars 28 forks source link

missing a way to pass 'D3D_COMPILE_STANDARD_FILE_INCLUDE' macro to shader compiler #15

Closed fbolefeysot closed 2 years ago

fbolefeysot commented 2 years ago

Hi Thanks for your great work on DirectN ! FYI I'm switching my game project from SharpDx to DirectN, so I might be posting a few issues or missing stuff I find along the way...

Here's one : In regular DirectX, a shader can be compiled with the pInclude parameter set to D3D_COMPILE_STANDARD_FILE_INCLUDE to use a default include handler. Looks like this macro isn't available in DirectN, and as far as I can find, there isn't an equivalent constant which can be passed instead. And while we're in the compiler, I also could not find the definition for D3DXSHADER Compiler flags. Thanks

smourier commented 2 years ago

Hi,

Yes the parser misses some definitions it cannot read, D3D_COMPILE_STANDARD_FILEINCLUDE is such one. Other D3DCOMPILE*** flags are skipped too. Some headers are really raw "C" oriented...

D3D_COMPILE_STANDARD_FILE_INCLUDE is just a raw 1 cast:

#define D3D_COMPILE_STANDARD_FILE_INCLUDE ((ID3DInclude*)(UINT_PTR)1)

But the current D3DCompile functions only takes a ID3DInclude reference, so you won't be able to pass 1 with .NET. This can be fixed by redefining D3DCompile for example like this with pInclude as an IntPtr (note you can keep both versions as different overloads):

    [DllImport("D3DCompiler_47", ExactSpelling = true)]
    public static extern HRESULT D3DCompile(/* _In_reads_bytes_(SrcDataSize) */ IntPtr pSrcData, /* _In_ */ IntPtr SrcDataSize, /* _In_opt_ */ [MarshalAs(UnmanagedType.LPStr)] string pSourceName, /* _In_reads_opt_(_Inexpressible_(pDefines->Name != NULL)) */ [MarshalAs(UnmanagedType.LPArray)] _D3D_SHADER_MACRO[] pDefines, /* _In_opt_ */ IntPtr pInclude, /* _In_opt_ */ [MarshalAs(UnmanagedType.LPStr)] string pEntrypoint, /* _In_ */ [MarshalAs(UnmanagedType.LPStr)] string pTarget, /* _In_ */ uint Flags1, /* _In_ */ uint Flags2, /* _Out_ */ out ID3D10Blob ppCode, /* _Always_(_Outptr_opt_result_maybenull_) */ out ID3D10Blob ppErrorMsgs);

As for D3DXSHADER, it's a DX9 header and DirectN doesn't include DX9, only Direct technologies included with the Windows SDK.

fbolefeysot commented 2 years ago

thanks, it works like a charm !