Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling shader for MonoGame

Tags:

c#

.net

monogame

I'm using VS 2013 to and trying to get a pixelshader to work properly. I've had this shader working in XNA 4 so I'm pretty certain it's ok.

I'm trying to compile the shader using the 2MGFX tool

Just running

2MGFX.exe AlphaMap.fx AlphaMap.fxg

Works and I get my compiled AlphaMap.fxg file.

However when trying to use/load this file in MonoGame I get:

The MGFX effect is the wrong profile for this platform!

The fix for this seems to be to add /DX11 to the 2MGFX command, but then I get this error instead:

Pixel shader 'PixelShaderFunction' must be SM 4.0 level 9.1 or higher! Failed to compile the input file 'AlphaMap.fx'!

What am I doing wrong?

Code for shader.

uniform extern texture ScreenTexture;  
sampler screen = sampler_state 
{
    // get the texture we are trying to render.
    Texture = <ScreenTexture>;
};

uniform extern texture MaskTexture;  
sampler mask = sampler_state
{
    Texture = <MaskTexture>;
};

// here we do the real work. 
float4 PixelShaderFunction(float2 inCoord: TEXCOORD0) : COLOR
{

    float4 color = tex2D(screen, inCoord);
    color.rgba = color.rgba - tex2D(mask, inCoord).r;
    return color;
}

technique
{
    pass P0
    {
        // changed this to reflect fex answer
        PixelShader = compile ps_4_0_level_9_1 PixelShaderFunction();
    }
}

EDIT

The answer by fex makes me able to load the effect but it dosent seem to work now.

Im using it like this:

    Texture2D Planet = _Common.ContentManager.Load<Texture2D>("Materials/RedPlanet512");
    Texture2D AlphaMapp = _Common.ContentManager.Load<Texture2D>("Materials/Dots2");
    Effect AlphaShader = _Common.ContentManager.Load<Effect>("Effects/AlphaMap");

    AlphaShader.Parameters["MaskTexture"].SetValue(AlphaMapp);

    spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, AlphaShader, _Common.Camera.View);

    spriteBatch.Draw(Planet,
        new Vector2(0, 0),
        null, Color.White, 0f,
        new Vector2(Planet.Width / 2, Planet.Height / 2),
        1f, SpriteEffects.None, 1f);
    spriteBatch.End();

These are the textures im using:

http://www.syntaxwarriors.com/wp-content/gallery/alphamapping-gallery/redplanet512.png http://www.syntaxwarriors.com/wp-content/gallery/alphamapping-gallery/whitedots_0.png

like image 664
JensB Avatar asked Mar 22 '23 03:03

JensB


1 Answers

Try to change this line:

    PixelShader = compile ps_2_0 PixelShaderFunction();

into:

    PixelShader = compile ps_4_0_level_9_1 PixelShaderFunction();

By the way - why don't you use MonoGame Content template?

like image 139
fex Avatar answered Apr 05 '23 00:04

fex