【问题标题】:Upgraded MonoGame and now I can't get my shader to compile升级了 MonoGame,现在我无法编译着色器
【发布时间】:2016-03-14 23:55:11
【问题描述】:

我将我的 monogame 升级到了最新版本。最初在管道工具中构建着色器时,我收到错误 Vertex shader 'SpriteVertexShader' must be SM 4.0 level 9.1 or higher!所以我将 ps_2_0 更改为 ps_4_0_level_9_1 但现在我得到了错误:

错误 X3004:未声明的标识符“SecondPassTextureSampler”

有人知道如何解决这个问题吗?

#include "Macros.fxh"

texture Bitmap;

sampler2D FirstPassTexture = sampler_state{
    Texture = (Bitmap);
    MagFilter = Linear;
    MinFilter = Linear;
    AddressU = Clamp;
    AddressV = Clamp;
};

sampler2D SecondPassTexture = sampler_state{
    Texture = (Bitmap);
    MagFilter = Linear;
    MinFilter = Linear;
    AddressU = Clamp;
    AddressV = Clamp;
};

#define KERNEL_RADIUS 7
#define KERNEL_WIDTH (2*KERNEL_RADIUS + 1)

// Uniforms
BEGIN_CONSTANTS
    float kernel[KERNEL_WIDTH];
    float2 offsets[KERNEL_WIDTH];

MATRIX_CONSTANTS
    float4x4 MatrixTransform    _vs(c0) _cb(c0);
END_CONSTANTS

struct VSOutput
{
    float4 position     : SV_Position;
    float4 color        : COLOR0;
    float2 texCoord     : TEXCOORD0;
};

VSOutput SpriteVertexShader(    float4 position : SV_Position,
                                float4 color    : COLOR0,
                                float2 texCoord : TEXCOORD0)
{
    VSOutput output;
    output.position = mul(position, MatrixTransform);
    output.color = color;
    output.texCoord = texCoord;
    return output;
}

float4 gaussH(VSOutput input): SV_Target0
{
    float4 color = float4(0,0,0,0);
    for(int i = 0; i < KERNEL_WIDTH; ++i)
        color += SAMPLE_TEXTURE(SecondPassTexture, (input.texCoord + float2(offsets[i].x, 0.0) )) * kernel[i];
    return color * input.color;
}

float4 gaussV(VSOutput input): SV_Target0
{
    float4 color = float4(0.0,0.0,0.0,0);
    for(int i = 0; i < KERNEL_WIDTH; ++i)
        color += SAMPLE_TEXTURE(SecondPassTexture, (input.texCoord + float2(0.0, offsets[i].y) )) * kernel[i];
    return color * input.color;
}

float4 gaussVGlow(VSOutput input): SV_Target0
{
    float4 color = float4(1.0,1.0,1.0,0);
    for(int i = 0; i < KERNEL_WIDTH; ++i)
    {
        float alpha = SAMPLE_TEXTURE(SecondPassTexture, (input.texCoord + float2(0.0, offsets[i].y) )).a * kernel[i];
        color.a += alpha;
    }
    // This will make stripes on top of the glow
    /*
    float m = smoothstep(0.45, 0.55, 0.5*cos(25.0*atan2(input.texCoord.y-0.5, input.texCoord.x-0.5))+0.5) * 0.5 + 0.5;
    color.a *= m;
    */
    color.a = pow(color.a, 0.5);
    color.rgb *= color.a; // Yeah, you have to pre multiply your alpha -- either that or render with premultiply option
    return color * input.color;
}

technique Blur {
    pass p0 {
        VertexShader = compile vs_4_0_level_9_1 SpriteVertexShader();
        PixelShader = compile ps_4_0_level_9_1 gaussH();
    }
    pass p1 {
        VertexShader = compile vs_4_0_level_9_1 SpriteVertexShader();
        PixelShader = compile ps_4_0_level_9_1 gaussV();
    }
    pass p1Glow {
        VertexShader = compile vs_4_0_level_9_1 SpriteVertexShader();
        PixelShader = compile ps_4_0_level_9_1 gaussVGlow();
    }
}

Macros.fxh 是:http://pastebin.com/y51kFfii

【问题讨论】:

  • 您的着色器看起来不像 GLSL; techniquepass 是什么?此外,SecondPassTextureSampler 不会出现在着色器中;你确定这是你的着色器失败而不是你的 C# 代码吗?
  • 感谢您的浏览。 techinque 和 pass 来自我链接的 XNA 宏。我认为 SecondPassTextureSampler 会自动创建。我什至没有运行 C# 代码,只是运行 Monogame 管道工具。

标签: opengl shader monogame


【解决方案1】:

来自 Monogame 论坛: 你会得到一个错误,因为你使用宏 SAMPLE_TEXTURE 进行采样,但不要使用宏 DECLARE_TEXTURE 来声明纹理。 如果要使用 Macros.fxh 中的宏,则纹理需要命名为 SecondPassTexture,采样器需要命名为 SecondPassTextureSampler。 (不要更改调用 SAMPLE_TEXTURE 的行。)

如果您查看宏(_Macro.fxh,第 31 行),代码

SAMPLE_TEXTURE(SecondPassTexture, (input.texCoord + float2(0.0, offsets[i].y) )) 展开为

SecondPassTexture.Sample(SecondPassTextureSampler, (input.texCoord + float2(0.0, offsets[i].y) )) 但在你的情况下(原帖)它应该是

Bitmap.Sample(SecondPassTexture, (input.texCoord + float2(0.0, offsets[i].y) )) 这是一个更简单的解决方案,您可以尝试: 只需将所有 SAMPLE_TEXTURE 替换为 tex2D。 (保留原帖中的采样器。)

【讨论】:

  • 另外说明,我遇到了其他问题,我发现在我之前的安装之上安装 MonoGame 3.5 很糟糕,卸载并重新安装修复了一些问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多