Custom Shader Help

I have been trying to make the transition from XNA over to Monogame and custom shaders have been kicking my ass. I’ve read a fair number of posts here and different things around the internet but as much as I try and I cannot seem to get even the simplest of custom shader to work. All other content assets seem to be working fine.

My environment is

Windows 8.1 x64
VS2013 Ultimate
Monogame 3.2

My solution consists of 3 projects.

Primary MonoGame Project (Using the Monogame Windows dll)
Proxy XNA 4 Project
XNA 4 Content Project (Monogame Content Process has been added as a reference, inside the csproj I have MonoGamePlatform set to Windows)

I have the effect file setup as follows
Build Action Compile
Content Importer: Effect - XNA Framework
Content Processor: MonoGame Effect

Below is a simple shader that I ported over to MonoGame (I updated the POSITION0 as I had read about that in certain places). This works fine in XNA.

float4x4 View;
float4x4 Projection;

int Time;

texture PhotoTexture;
sampler PhotoSampler = sampler_state 
{

	texture = <PhotoTexture>;
	AddressU = Wrap;
	AddressV = Wrap;
	MinFilter = Anisotropic;
	MagFilter = Anisotropic;
	MipFilter = Point;

};

struct VertexShaderInput
{
    float4 Position : SV_POSITION;
	float3 Normal   : NORMAL0;
	float2 UV : TEXCOORD0;

};

struct VertexShaderOutput
{
    float4 Position : SV_POSITION;	
	float3 Normal   : NORMAL0;
	float2 UV : TEXCOORD0;

};

VertexShaderOutput VertexShaderFunction(VertexShaderInput Input)
{

    VertexShaderOutput Output;

	Output.Position = mul(Input.Position, mul(View, Projection));
	Output.Normal = Input.Normal;
	Output.UV = Input.UV;

    return Output;

}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{

	float4 Color = tex2D(PhotoSampler, input.UV);
	
	float Alpha = clamp((Time - input.Normal.x) * input.Normal.y / 255, 0, 1);

    return float4(Color.x, Color.y, Color.z, Alpha);

}

technique Technique1
{

    pass Pass1
    {

        VertexShader = compile vs_3_0 VertexShaderFunction();
        PixelShader = compile ps_3_0 PixelShaderFunction();

    }

}

The content compiles without error but when loading it I get the

An exception of type 'System.Exception' occurred in MonoGame.Framework.dll but was not handled in user code

Additional information: The MGFX file is corrupt!

Problem

I really love the concept of MonoGame and would love to be able to continue some of my XNA projects in it. I know there probably is information that is needed that I have left out so just ask away if you need more details. I only need this to work for traditional windows desktop applications, and would like to target DX11.

Any help would be greatly appreciated.

Thanks

did you read this ?

Use the DX11 feature levels vs_4_0_level_9_1 or ps_4_0_level_9_1 when targeting Windows 8 Metro applications and wanting to support all devices. Higher shader models work, but might not run on all Windows 8 systems.

that might be the problem. There are some quirks in shaders for monogame.
Like, you cant use constants, and booleans (maybe this was changed).
So you should try to search a bit in the old forums if some more stuff happens (or ask here hehe)!

I had read that a couple times and I always get a compile error. Just tried again to make sure I wasn’t entirely insane.

error X3041: unsupported compiler target 'vs_4_0_level_9_1'

I am specifically not targeting Metro applications. I am targeting Windows desktop applications, or want to target that. I’ve invested hours searching and reading posts and I still don’t know what I’m doing wrong.

It may be time to fork develop and step through the code that’s pissed at me.

This occurs when the header bytes of the effect are incorrect. This usually means your not loading a MGFX file… probably a normal XNA FX file… which we don’t support.

My guess is that the MonoGame Effect pipeline processor is not detected the platform and is falling back to the stock XNA FX processor.

Make sure you’ve added the <MonoGamePlatform>Windows8<MonoGamePlatform> (yes Windows8) to the project that triggers the building of your .contentproj.

Thanks for the advice Tom. I already had that in my Content csproj. I swapped out the dll for the source and saw that the Platform was incoming as none form the environment variable.

I am pretty sure the issue was that the ContentProcess build targets weren’t being invoked. For now I copied over the before and after target for setting the environment variable and that worked. Is there something special I need to do to make sure the MonoGame build targets are invoked?

Did you include the <Import Project="$(MSBuildExtensionsPath)\MonoGame\v3.0\MonoGame.ContentPipeline.targets" /> in your game project as well?

After that all you can do is set Visual Studio to dump verbose logging and try to pick out why they are not being executed.

All this mess is why we’re not supporting this type of Visual Studio integration of content building in the future.