amerkoleci/Vortice.Windows

D3DCompileFromFile Include error.

Steph55 opened this issue · 2 comments

I'm trying to use D3DCompileFromFile() with the following include handler:

namespace DxEav.CustomShaders;
public class ShaderIncludeHandler : Include
    {
        string ShaderDir;
        string[] SystemDirectories;

        public ShaderIncludeHandler(string shaderDir, string[] systemDirectories)
        { 
            ShaderDir = shaderDir;
            SystemDirectories = systemDirectories;
        }
        public Stream Open(IncludeType includeType, string fileName, Stream parentStream)
        {
            string finalPath = null;
            switch (includeType)
            {
                case IncludeType.Local:
                    finalPath = Path.Combine(ShaderDir, fileName);
                    break;
                case IncludeType.System:
                    foreach (string dir in SystemDirectories)
                    {
                        string finalPathT = Path.Combine(dir, fileName);
                        if (File.Exists(finalPathT))
                        {
                            finalPath = finalPathT;
                            break;
                        }
                    }
                    break;
                default:
                    throw new Exception("Error");
            }
            if (finalPath == null) throw new Exception($"System file not found: {fileName}.");
            return new FileStream(finalPath, FileMode.Open);
        }

        public void Close(Stream stream)
        {
            stream?.Dispose();
        }
    }

However, when I call the compile function, it get the following exception, and the open callback is never called:

System.Runtime.CompilerServices.SwitchExpressionException : 'Non-exhaustive switch expression failed to match its input.
SwitchExpressionException_UnmatchedValue'
Unmatched value was DxEav.CustomShaders.ShaderIncludeHandler.

Here is the code I use to call D3DCompileFromFile():

            var shaderMacros = new ShaderMacro[]
            {
                new ShaderMacro("T", "ps_5_0"),
                new ShaderMacro("D", "D2D_FUNCTION"),
                new ShaderMacro("D", "D2D_ENTRY=main"),
                new ShaderMacro("Fl", pathCompilation),
                new ShaderMacro(null, null) 
            };

            string[] systemDirectories = {
                "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.19041.0\\um",
                "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.19041.0\\shared",
                "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.19041.0\\winrt",
                "C:\\Program Files (x86)\\Windows Kits\\NETFXSDK\\4.8\\Include\\um" };

            var includeHandler = new ShaderIncludeHandler(shaderDirectory, systemDirectories);

            var result = Compiler.CompileFromFile(
                sourceFile,
                shaderMacros,
                includeHandler,
                "main",
                "ps_5_0",
                ShaderFlags.None,
                EffectFlags.None,
                out Blob blob,
                out Blob errorBlob);

Did I do something wrong or is there a bug in Vortice marshalling? Thanks!

Your ShaderIncludeHandler need to subclass CallbackBase and Include interface

Thank you very much for your answer and for your good library!