MonoGame升级,现在我无法编辑着色器

时间:2016-03-14 23:55:11

标签: opengl shader monogame

我将我的monogame升级到最新版本。在开始在管道工具中构建着色器时,我得到了错误Vertex着色器' SpriteVertexShader'必须是SM 4.0级9.1或更高!所以我将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

1 个答案:

答案 0 :(得分:0)

来自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))) 这是一个更简单的解决方案,您可以尝试: 只需用tex2D替换所有SAMPLE_TEXTURE即可。 (将采样器留在原始帖子中。)