首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# SharpDX使用嵌入式资源从文件编译

C# SharpDX使用嵌入式资源从文件编译
EN

Stack Overflow用户
提问于 2018-10-16 15:54:07
回答 1查看 433关注 0票数 0

您好,我有几个文件,现在是嵌入式资源,但问题是使用:

代码语言:javascript
复制
CompilationResult result = SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile(
            fileName,
            entryPoint,
            profile,
            shaderFlags,
            include: FileIncludeHandler.Default,
            defines: defines);

Put错误:System.IO.FileNotFoundException: 'Unable to find file'。我发现还有其他可以从源代码编译的函数:

代码语言:javascript
复制
CompilationResult result = SharpDX.D3DCompiler.ShaderBytecode.Compile(data,profile,shaderFlags);

为了从嵌入的资源文件中读取,我使用了这个小类:Link 1

用法如下所示:

代码语言:javascript
复制
string data = ResourceHelper.GetEmbeddedResource(fileName);
CompilationResult result = SharpDX.D3DCompiler.ShaderBytecode.Compile(data,profile,shaderFlags);

但是现在我得到了这个错误:

代码语言:javascript
复制
System.ArgumentNullException: 'Value cannot be null.
Parameter name: entryPoint'

为了检查app是否加载了所有嵌入的资源,我使用了这个小方法,它返回所有嵌入的资源

代码语言:javascript
复制
string[] zz = Assembly.GetExecutingAssembly().GetManifestResourceNames();
        MessageBox.Show(string.Join("\n", zz));

我错过了什么?

编辑1:

这是我如何使用它的完整方法:

代码语言:javascript
复制
public static ShaderBytecode CompileShader(string fileName, string entryPoint, string profile, ShaderMacro[] defines = null)
    {
        var shaderFlags = ShaderFlags.None;
        var assembly = Assembly.GetExecutingAssembly();
        using (Stream stream = assembly.GetManifestResourceStream(fileName))
        {
            using (var reader = new StreamReader(stream))
            {
                CompilationResult result = SharpDX.D3DCompiler.ShaderBytecode.Compile(reader.ReadToEnd(),entrypoint,profile,shaderFlags);

                /*CompilationResult result = SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile(
                    fileName,
                    entryPoint,
                    profile,
                    shaderFlags,
                    include: FileIncludeHandler.Default,
                    defines: defines);*/
                return new ShaderBytecode(result);
            }
        }    
    }

实际用法:

代码语言:javascript
复制
string MainName = "my_project";
_shaders["standardVS"] = D3DUtility.CompileShader(MainName+".Shaders.Default.hlsl", "VS", "vs_5_1");
//50 other files...

Image 1

编辑2:添加了编译方法的入口点,但现在它抛出错误:

代码语言:javascript
复制
System.NullReferenceException: 'Object reference not set to an instance of an object.'

在:

代码语言:javascript
复制
return new ShaderBytecode(result);

Execption处理程序显示如下:

代码语言:javascript
复制
Message="C\FileLocation: error X1505: No include handler specified, can't perform a #include. Use D3DX APIs or provide your own include handler.\n"

编辑3:使用@J. van Langen更新方法如下所示:

代码语言:javascript
复制
System.Exception: 'C:\Users\test\source\repos\myapp\Debug\Resources\DemoScene\unknown(14,10-30): error X1505: No include handler specified, can't perform a #include. Use D3DX APIs or provide your own include handler.'

result.Message = result.Message="error CS0452: The type 'ShaderBytecode' must be a reference type in order to use it as parameter 'T' in the generic type or method 'CompilationResultBase<T>'"

编辑4:我想我找到了抛出错误的原因,因为它包含在其中:Pastebin

EN

回答 1

Stack Overflow用户

发布于 2018-10-16 16:02:53

你不应该使用CompileFromFile,而应该只使用Compile,你需要重载Compile(string shaderSource, string entryPoint, string profile, ShaderFlags shaderFlags, .....,其余的参数使用default。

下面是我做过的一个老项目的例子:

代码语言:javascript
复制
static GradientRenderer()
{
    // This may be changed to GetType()   (see 'The new reflection API')
    var assembly = typeof(GradientRenderer).GetTypeInfo().Assembly;

    //string[] resources = assembly.GetManifestResourceNames();

    string code;

    // use the full filename with namespace
    using (var stream = assembly.GetManifestResourceStream("MirageDX11.Renderers.Gradient.Gradient.hlsl"))
    using (var reader = new StreamReader(stream))
        // read the whole content to a string.
        code = reader.ReadToEnd();

    var shaderFlags = ShaderFlags.None;

#if DEBUG
    shaderFlags |= ShaderFlags.Debug;
    shaderFlags |= ShaderFlags.SkipOptimization;
#endif

    // Compile the vertex shader and the pixel shader  "VS" & "PS" => entrypoint
    _vertexShaderByteCode = ShaderBytecode.Compile(code, "VS", "vs_5_0", shaderFlags);
    _pixelShaderByteCode = ShaderBytecode.Compile(code, "PS", "ps_5_0", shaderFlags);
}

这是着色器

代码语言:javascript
复制
struct VertexIn
{
    float3 PosL : POSITION;
    float4 Color : COLOR;
};

struct VertexOut
{
    float4 PosH : SV_POSITION;
    float4 Color: COLOR;
};

VertexOut VS(VertexIn vin)
{
    VertexOut vout;
    vout.PosH = float4(vin.PosL, 1.0f);
    vout.Color = vin.Color;
    return vout;
}

float4 PS(VertexOut pin) : SV_Target
{
    float4 value = pin.Color;
    value.w = 1.0f;
    return value;
}

有关如何捕获着色器错误的更新:

我已经修改了你的方法来捕获着色器编译错误。它将引发一个异常,并显示一条有用的消息。

代码语言:javascript
复制
public static ShaderBytecode CompileShader(string fileName, string entryPoint, string profile, ShaderMacro[] defines = null)
{
    var shaderFlags = ShaderFlags.None;
    var assembly = Assembly.GetExecutingAssembly();
    using (Stream stream = assembly.GetManifestResourceStream(fileName))
    {
        using (var reader = new StreamReader(stream))
        {
            CompilationResult result = SharpDX.D3DCompiler.ShaderBytecode.Compile(reader.ReadToEnd(),entrypoint,profile,shaderFlags);

            // when the Bytecode == null, means that an error has occurred
            if (result.Bytecode == null)
                throw new InvalidOperationException(result.Message);

            // removed old code in comment...

            return new ShaderBytecode(result);
        }
    }    
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52830509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档