Shader work fine with openGL, but didn't with directX project


I try to make a shader to do celshading “toon effect” it works fine on my openGL test project, but when I try to implement it on my windows project a got this error :

{“An error occurred while preparing to draw. This is probably because the current vertex declaration does not include all the elements required by the current vertex shader. The current vertex declaration includes these elements: SV_Position0, NORMAL0.”}

Here my .fx file :

   #if OPENGL	
   #define VS_SHADERMODEL vs_3_0
   #define PS_SHADERMODEL ps_3_0
   #define VS_SHADERMODEL vs_4_0_level_9_1
   #define PS_SHADERMODEL ps_4_0_level_9_1
   float4x4 World;
   float4x4 View;
   float4x4 Projection;
   float4x4 WorldInverseTranspose;
   float4 MeshColor;
   float3 DiffuseLightDirection = float3(0, 0, 0);
   float4 DiffuseColor = float4(1, 1, 1, 1);
   struct VertexShaderInput {
   	float4 Position : SV_Position0;         // The position of the vertex
   	float3 Normal   : NORMAL0;              // The vertex's normal
   	float4 Color    : COLOR0;               // The vertex's color
   struct VertexShaderOutput {
   	float4 Position : SV_Position0;
   	float4 Color    : COLOR0;
   	float3 Normal   : NORMAL0;
   VertexShaderOutput MainVS(in VertexShaderInput input) {
   	VertexShaderOutput output = (VertexShaderOutput)0;
   	// Transform the position
   	float4 worldPosition = mul(input.Position, World);
   	float4 viewPosition = mul(worldPosition, View);
   	output.Position = mul(viewPosition, Projection);
   	// Transform the normal
   	output.Normal = normalize(mul(input.Normal, WorldInverseTranspose));
   	output.Color = input.Color;
   	return output;
   float4 MainPS(VertexShaderOutput input) : COLOR0 {
   	// Calculate diffuse light amount
   	float intensity = dot(normalize(DiffuseLightDirection), input.Normal);
   	if (intensity < 0)
   		intensity = 0;
   	// Calculate what would normally be the final color, including texturing and diffuse lighting
   	float4 color = MeshColor * DiffuseColor * DiffuseColor.a;
   	float light;
   	if (intensity > 0.01)
   		light = 1.1;
   	else if (intensity > 0.0002)
   		light = 1;
   		light = .4;
   	color.rgb *= light;
   	return color;
   technique Toon {
   	pass P0 {
   		VertexShader = compile VS_SHADERMODEL MainVS();
   		PixelShader = compile PS_SHADERMODEL MainPS();

And the C# code :

        foreach (ModelMesh mesh in GameData.Bank.getModel(this.Model).Meshes) {
            Matrix nworld = mesh.ParentBone.Transform * * Matrix.CreateScale(ModelScale);
            foreach (Effect effect in mesh.Effects) {
                effect.Parameters["DiffuseColor"].SetValue(new Vector4(1, 1, 1, lightIntensity));

the exception occurs when mesh.Draw(); is called

I’m new with HLSL and 3D programing and don’t understand what is wrong with my code :sweat:

Hi again, I have made some tests

Replace my shader by the default template “NewEffect.fx” and… same error.
Make a new project from the directx template and “NewEffect.fx” … same error.

wtf the default template aren’t compatible ? or I just did something stupid somewhere… :no_mouth:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;

    namespace test {
        public class Game1 : Game {
            private GraphicsDeviceManager graphics;
            private SpriteBatch spriteBatch;
            private Model model;
            private Vector3 position;
            private float angle;
            private Matrix world = Matrix.CreateTranslation(new Vector3(0, 0, 0));
            private Matrix view;
            private Matrix projection;

            private Effect Shader;
            private RenderTarget2D celTarget;

            private KeyboardState keyboard;

            public Game1() {
       = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";

            protected override void Initialize() {
       = 1000;
       = 1000;
                this.angle = 0;
                this.position = new Vector3(0, -250, 0);
                this.projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(70), 1f, 0.1f, 10000f);
                Vector3 rotatedOffset = Vector3.Transform(new Vector3(0, 500, 500), Matrix.CreateRotationY(3.1415f*0.25f));
                this.view = Matrix.CreateLookAt(rotatedOffset, new Vector3(0, 0, 0), new Vector3(0, 1, 0));
                this.celTarget = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, false, SurfaceFormat.Color, DepthFormat.Depth24);
                this.keyboard = Keyboard.GetState();

            protected override void LoadContent() {
                // Create a new SpriteBatch, which can be used to draw textures.
                this.spriteBatch = new SpriteBatch(GraphicsDevice);
                this.model = Content.Load<Model>("Ship");
                // Effect
                this.Shader = Content.Load<Effect>("eff");
                ChangeEffectUsedByModel(this.model, this.Shader);

            protected override void UnloadContent() {

            protected override void Update(GameTime gameTime) {
                // Allows the game to exit
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

                KeyboardState nkeyboard = Keyboard.GetState();
                if (nkeyboard.IsKeyDown(Keys.Left) && this.keyboard.IsKeyUp(Keys.Left))
                if (nkeyboard.IsKeyDown(Keys.Right) && this.keyboard.IsKeyUp(Keys.Right))

       = Matrix.CreateRotationY(this.angle * 3.1415f / 4f) * Matrix.CreateTranslation(position);
                this.keyboard = nkeyboard;

            protected override void Draw(GameTime gameTime) {
                GraphicsDevice.DepthStencilState = DepthStencilState.Default;
                GraphicsDevice.BlendState = BlendState.Opaque;
                GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
                GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
                GraphicsDevice.Clear(Color.FromNonPremultiplied(255, 255, 255, 0));

                DrawModel(this.model,, this.view, this.projection);

            private void DrawModel(Model _model, Matrix _world, Matrix _view, Matrix _projection) {
                foreach (ModelMesh mesh in _model.Meshes) {
                    Matrix nworld = mesh.ParentBone.Transform * _world;
                    foreach (Effect effect in mesh.Effects) {

            /// <summary>  
            /// Alters a model so it will draw using a custom effect, while preserving  
            /// whatever textures were set on it as part of the original effects.  
            /// </summary>  
            static void ChangeEffectUsedByModel(Model model, Effect replacementEffect) {
                // Table mapping the original effects to our replacement versions.  
                Dictionary<Effect, Effect> effectMapping = new Dictionary<Effect, Effect>();

                foreach (ModelMesh mesh in model.Meshes) {
                    // Scan over all the effects currently on the mesh.  
                    foreach (BasicEffect oldEffect in mesh.Effects) {
                        // If we haven't already seen this effect...  
                        if (!effectMapping.ContainsKey(oldEffect)) {
                            // Make a clone of our replacement effect. We can't just use  
                            // it directly, because the same effect might need to be  
                            // applied several times to different parts of the model using  
                            // a different texture each time, so we need a fresh copy each  
                            // time we want to set a different texture into it.  
                            Effect newEffect = replacementEffect.Clone();

                            // Copy across the material color from the original effect.  
                            //newEffect.Parameters["MeshColor"].SetValue(new Vector4(oldEffect.DiffuseColor, 1f));
                            effectMapping.Add(oldEffect, newEffect);

                    // Now that we've found all the effects in use on this mesh,  
                    // update it to use our new replacement versions.  
                    foreach (ModelMeshPart meshPart in mesh.MeshParts) {
                        meshPart.Effect = effectMapping[meshPart.Effect];

            // =========================


I use Visual Studio 2015 and monogame 3.5

Did I forget something? :worried: