【问题标题】:Cannot compile the same shader in slimdx that works in xna?无法在 xna 中工作的 slimdx 中编译相同的着色器?
【发布时间】:2012-06-20 02:41:48
【问题描述】:

我正在尝试在 slimdx 中加载一个 FX 文件,我已经使用 XNA 4.0 加载和编译了这个精确的 FX 文件,但是我遇到了 slimdx 错误,这是我的加载代码。

    using SlimDX.Direct3D11;
    using SlimDX.D3DCompiler;

    public static Effect LoadFXShader(string path)
    {
        Effect shader;
        using (var bytecode = ShaderBytecode.CompileFromFile(path, null, "fx_2_0", ShaderFlags.None, EffectFlags.None))
            shader = new Effect(Devices.GPU.GraphicsDevice, bytecode);

        return shader;
    }

这是着色器:

#define TEXTURE_TILE_SIZE 16

struct VertexToPixel
{
    float4 Position     : POSITION;    
    float2 TextureCoords: TEXCOORD1;
};

struct PixelToFrame
{
    float4 Color : COLOR0;
};

//------- Constants --------
float4x4 xView;
float4x4 xProjection;
float4x4 xWorld;
float4x4 preViewProjection;
//float random;

//------- Texture Samplers --------

Texture TextureAtlas;
sampler TextureSampler = sampler_state { texture = <TextureAtlas>; magfilter = Point; minfilter = point; mipfilter=linear; AddressU = mirror; AddressV = mirror;};

//------- Technique: Textured --------

VertexToPixel TexturedVS( byte4 inPos : POSITION, float2 inTexCoords: TEXCOORD0)
{   
    inPos.w = 1;
    VertexToPixel Output = (VertexToPixel)0;
    float4x4 preViewProjection = mul (xView, xProjection);
    float4x4 preWorldViewProjection = mul (xWorld, preViewProjection);

    Output.Position = mul(inPos, preWorldViewProjection);

    Output.TextureCoords = inTexCoords / TEXTURE_TILE_SIZE;

    return Output;
}

PixelToFrame TexturedPS(VertexToPixel PSIn) 
{
    PixelToFrame Output = (PixelToFrame)0;

    Output.Color = tex2D(TextureSampler, PSIn.TextureCoords);

    if(Output.Color.a != 1)
        clip(-1);

    return Output;
}

technique Textured
{
    pass Pass0
    {   
        VertexShader = compile vs_2_0 TexturedVS();
        PixelShader  = compile ps_2_0 TexturedPS();
    }
}

现在这个精确的着色器在 XNA 中工作正常,但在 slimdx 中我得到了错误

ChunkDefault.fx(28,27): error X3000: unrecognized identifier 'byte4'

【问题讨论】:

    标签: c# shader slimdx


    【解决方案1】:

    如果我没记错的话,byte4 来自 XNA(使用着色器模型 2.0)的reach/hd 配置文件。 您正在使用 directx11,但正在尝试将其编译为 Shader Model 2 配置文件。这是不支持的。您必须重写着色器以适应着色器模型 5 配置文件 (fx_5_0) 或使用功能级别 D3D_FEATURE_LEVEL_9_1 构建您的设备,并将您的着色器编译为目标 ps_4_0_level_9_1。

    您的着色器中的一些东西在着色器模型 5 中不起作用。例如,sample_state 是 Directx9 的东西。从 Directx10 及更高版本开始,您必须使用 samplerstate 组。(See here for more info) 例如:

    SamplerState MeshTextureSampler
    {
        Filter = MIN_MAG_MIP_LINEAR;
        AddressU = Wrap;
        AddressV = Wrap;
    };
    

    其他一些不受支持的内容包括:

    magfilter = Point;
    

    你必须把那个转换成这样的过滤器:

    Filter = MIN_MAG_LINEAR_MIP_POINT;
    

    这些是我扫描时发现的。希望对你有所帮助。

    【讨论】:

    • 我怎样才能使用 byte4 呢?如果我必须使用浮点数,内存使用会增加很多。
    • 在这种情况下,您可以使用 uint4 而不是 byte4。那应该可以。
    • 我意识到它在那个地方是 byte4 还是 float4 实际上并不重要,因为它没有存储在内存中。 uint4 将使用与 float4 相同的内存量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多