My first shader: pipeline not building

I’m trying shaders for the first time in MonoGame. My only previous experience was with a couple of shaders I dabbled with in XNA, so I don’t really know what I was doing.

Using a recent binary install of MonoGame I have used the Pipeline tool to successfully build textures and music. I added a Shader .fx file that I knew worked in XNA to the project, set the Importer to “Effect Importer - MonoGame” and the Processor to “Effect - MonoGame”. When I first built the pipeline complained that it could not find D3DCompiler_43.dll - which as far as I can tell does not come with the MonoGame installer or the git repository. I did manage to find a copy of this DLL of unknown heritage lurking in a project sopmeone else had built which I must have doewnloaded sometime. I copied this to “C:\Program Files (x86)\MSBuild\MonoGame\v3.0\Tools” and now when I try to build my content project I get:

Content/Shaders/FadeToGrey.fx: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

Does this mean it’s working but MonoGame doesn’t like the format of my Shader? This shader worked in XNA:

// TODO: add effect parameters here.

sampler screen : register(s0);

texture source;
sampler map = sampler_state
{
	Texture = <source>;
};

float greyness = 0;

struct VertexShaderOutput
{
	float2 TexCoord : TEXCOORD0;
};

float4 FadeToGreyPixelShader(VertexShaderOutput input) : COLOR0
{
    // TODO: add your pixel shader code here.
	float4 col = tex2D(map,input.TexCoord);
	float intensity = col.r/3 + col.g/3 + col.b/3;
	return float4(greyness*intensity+((1-greyness) * col.r), greyness*intensity+((1-greyness)*col.g), greyness*intensity+((1-greyness)*col.b), col.a);
}

technique FadeToGreyTechnique
{
    pass Pass1
    {
        PixelShader = compile ps_2_0 FadeToGreyPixelShader();
    }
}

I have no idea what I’ve done wrong or how to fix it. Can you help?

Are you on a 32-bit version of Windows, by any chance? The MonoGame Content Pipeline only supports 64-bit versions of windows. See here for a further discussion - as I understand it, the decision is a pragmatic one based (a) on the time it would take to maintain multiple versions of the pipeline, and (b) the number of people affected by it being only 64-bit.

I’ve read the following discussion

…but am reluctant to run the installer specified as it is for DirectX 9.0c, and according to dxdiag I already have DirectX 11 installed.

Cheers TGJones - I’m running Windows 8.1 on a Surface Pro 3, 64-bit. But it could well be that the DLL I “found” is a 32-bit version?

Can’t find a DX11 SDK installer on the Microsoft site :frowning: will keep looking

Well, I installed that DirectX 9.0c link anyway and seem to be getting a bit further now:

    Content/Shaders/FadeToGrey.fx: error: Processor 'EffectProcessor' had unexpected failure!
System.Exception: Invalid profile ''. Pixel shader 'FadeToGreyPixelShader' must be SM 4.0 level 9.1 or higher!
   at TwoMGFX.PassInfo.ValidateShaderModels(ShaderProfile profile)
   at TwoMGFX.EffectObject.CompileEffect(ShaderInfo shaderInfo, String& errorsAndWarnings)
   at Microsoft.Xna.Framework.Content.Pipeline.Processors.EffectProcessor.Process(EffectContent input, ContentProcessorContext context)
   at Microsoft.Xna.Framework.Content.Pipeline.ContentProcessor`2.Microsoft.Xna.Framework.Content.Pipeline.IContentProcessor.Process(Object input, ContentProcessorContext context)
   at MonoGame.Framework.Content.Pipeline.Builder.PipelineManager.ProcessContent(PipelineBuildEvent pipelineEvent)

I guess my problem now is one of not knowing modern HLSL, rather than anything to do with MonoGame…

I think you’ll need to change it to this:

PixelShader = compile vs_4_0_level_9_1 FadeToGreyPixelShader();

There’s a more detailed explanation here, under “Effect Writing Tips”.