How can I add a custom shader to a Monogame Windows Phone project?

I created a custom shader(Effect1.fx) with XNA and I want to use it in a new Monogame Windows Phone project. I read that I must convert the .fx file to a .mgfxo file, but I don’t know how to do that.
How can I convert the .fx file to a .mgfxo file?

It’s actually, really really simple…but also can be really really hard to find if you don’t know where to look >_> It doesn’t help that there’s not many places online that give you a thorough guide XD

So, here’s what’cha gotta do :slight_smile:

step1: Have an .fx file (which you do :D)
step2: Locate the converter (this step took me forever >>) I found it in C:/Program Files(x86)/MSBuild/Monogame/V3.0 The converter is called 2MGFX
step3: Convert ^
^ …but how? O_o This step also took me forever >_>
You need to make a batch file to handle it. This is mine:

2MGFX.exe %~n1.fx %~n1.mgfx

Place that batch file into the v3.0 folder.

Now, place your .fx file into the v3.0 folder, and drag it onto the batch file.
If all goes well, no errors will pop up, and a new .mgfx file will be in the folder.

That should be it :slight_smile:

Note that there are arguments for the converter, such as /DX11 for windows 8 shaders using DX11.

Hope that helps ^^

It’s not working. I get the following error mesage: Failed to parse the input file “Effect1.fx”. What is wrong?

Not really sure, never had that problem. A quick google search implies that it happens mainly with users of windows 8 and/or Macs.

This thread may or may not help ><

maybe this one too:

I have an iMac but both Mac OS X and Windows 8 are installed. I don’t know what I should do to get it to work.

post your shader. I’ll see if I can compile it on my end ^^. That’ll help rule out some possibilities.

Since I’m a new user I apparently can’t reply more then 3 times here ~_~


It compiled fine on my computer (Windows 7).

If you are using Windows 8, did you make sure to use the DX 11 command in the batch file? ( /DX11 )

My shader:

sampler finalImage : register(s0); 
sampler displacementMap : register(s1); 

float4 main(float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0 
float displacement = tex2D(displacementMap, texCoord);   
texCoord += displacement * 0.2;    
return tex2D(finalImage, texCoord); 

technique Refraction 
pass pass_0 
    PixelShader = compile ps_2_0 main(); 

You have to use ps_4_0_level_9_3 when targeting windows phone, as well as vs_4_0_level_9_3 if you are going to use vertex shaders in the future.

Where can I change that?

Look at the last line of your shader! :wink:

I changed it to:

pass pass_0
PixelShader = compile ps_4_0_level_9_3 main();

But I get the same error message. What is wrong? Could someone try to convert the shader. Maybe it’s working on your system.

I use the following batch file to convert the shader. The batch file is located in my C:\Program Files (x86)\MSBuild\MonoGame\v3.0 directory.
Batch file:

2MGFX.exe %~n1.fx %~n1.mgfx

I’m not doing it that way. I do it the way it’s described here:
I think that’s much more intuitive and easier than dealing with batch files…

I always get this error message when I try to build my solution:

Unable to load dll ‘d3dcompiler_43.dll’ the specified module could not be found

What is wrong?

Install the latest DirectX

Thanx, that solved the problem. But now I get another error message when I try to build my solution:

Errors compiling C:.…\Effect1.fx:
C:.…\Effect1.fx(19,17): error X3041: unsupported compiler target ‘ps_4_0_level_9_3’

What is wong?

Try changing the target, see here Specifying Compiler Targets - Win32 apps | Microsoft Learn

FWIW I use ps_4_0_level_9_1

Just remembered you are using Windows phone so should probably keep that target. Are you using the /DX11 switch with 2mgfx? Not sure I can help much more as I’m new to this myself as you may tell from my HLSL problem post!

I get the same error message with ps_4_0_level_9_1. I use Visual Studio Express 2013 for Windows Desktop, maybe it’s not working with this version of Visual Studio?
I haven’t tried the 2mgfx tool. How can I use it?

2mgfx is in the monogame source under monogame/tools if you’re building it. If not then do a search in your monogame install as I don’t know where it would be installed.

I put my .fx file in C:\Program Files (x86)\MSBuild\MonoGame\v3.0 and dragged it on the tool(2MGFX.exe), but almost nothing happened. A command prompt window opened, but disappeared very quickly. I can’t find the .xnb file. What is wrong?

2mgfx is a command line tool so open a command prompt and run it from there. Assuming your effect file is Effect1.fx then if you used
2mgfx Effect1.fx /DX11
at a command prompt then it should compile and produce an Effect1.mgfxo file for you.