Error when setting render target in monomac

I start the game and everything works but after about 15 seconts the game crashes and I get this error :


Error :

System.InvalidOperationException: The combination of internal formats of the attached images violates an implementation-dependent set of restrictions.
at at Microsoft.Xna.Framework.Graphics.GraphicsDevice.ApplyRenderTargets (Microsoft.Xna.Framework.Graphics.RenderTargetBinding[]) <IL 0x00245, 0x006ab>
at at Microsoft.Xna.Framework.Graphics.GraphicsDevice.SetRenderTargets (Microsoft.Xna.Framework.Graphics.RenderTargetBinding[]) <IL 0x0009a, 0x0021b>
at at Microsoft.Xna.Framework.Graphics.GraphicsDevice.SetRenderTarget (Microsoft.Xna.Framework.Graphics.RenderTarget2D) <IL 0x0002c, 0x000eb>
at TileMap.Map.MakeMap (Microsoft.Xna.Framework.Graphics.SpriteBatch,Microsoft.Xna.Framework.Graphics.GraphicsDevice) [0x0001e] in /Users/VinnieH01/Projects/TileMap/TileMap/Map.cs:55
at TileMap.Game1.Draw (Microsoft.Xna.Framework.GameTime) [0x00013] in /Users/VinnieH01/Projects/TileMap/TileMap/Game1.cs:106
at at Microsoft.Xna.Framework.Game.DoDraw (Microsoft.Xna.Framework.GameTime) <IL 0x00026, 0x0009e>
at at Microsoft.Xna.Framework.Game.Tick () <IL 0x001e3, 0x00adb>
at at Microsoft.Xna.Framework.GameWindow.OnRenderFrame (MonoMac.OpenGL.FrameEventArgs) <IL 0x00049, 0x000ef>
at at MonoMac.OpenGL.MonoMacGameView.RenderScene () <IL 0x000ee, 0x0048b>
at at MonoMac.OpenGL.MonoMacGameView.m__1 () <IL 0x00002, 0x0002b>
at at MonoMac.Foundation.NSActionDispatcher.Apply () <IL 0x00007, 0x0002f>
at at (wrapper dynamic-method) object.[MonoMac.Foundation.NSActionDispatcher.Void Apply()] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector) <IL 0x0000b, 0x00047>
at at (wrapper native-to-managed) object.[MonoMac.Foundation.NSActionDispatcher.Void Apply()] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector) <IL 0x0004f, 0x0010b>
at at (wrapper managed-to-native) MonoMac.AppKit.NSApplication.NSApplicationMain (int,string[]) <0x00012>
at at MonoMac.AppKit.NSApplication.Main (string[]) <IL 0x00041, 0x000f3>
at TileMap.Program.Main (string[]) [0x0001d] in /Users/VinnieH01/Projects/TileMap/TileMap/Program.cs:18

As you can see there is a lot of stuff about monomac, So it probably has with that to do.

This is my code :

using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Collections.Generic;

namespace TileMap
{
public class Map
{
List<Texture2D> tileTextures;

Texture2D map;

RenderTarget2D renderTarget2d;

int tileSize;

Vector2 scale;

int[,] mapData;


public Map()
{
    tileTextures = new List<Texture2D> ();

    tileSize = 64;

    scale = new Vector2(8, 8);

    mapData = new int[,]
    {
        {2,2,2,2,2},
        {1,1,1,1,1},
        {1,1,1,1,1},
        {1,1,1,1,1},
        {1,1,1,1,1},
    };
}

public void setTiles(Texture2D Air, Texture2D Dirt, Texture2D Grass)
{
    /* index : 0 */ tileTextures.Add (Air);
    /* index : 1 */ tileTextures.Add (Dirt);
    /* index : 2 */ tileTextures.Add (Grass);  
}

public Texture2D getMap()
{
    return map;
}

public void MakeMap(SpriteBatch spriteBatch, GraphicsDevice graphicsDevice)
{
    renderTarget2d = new RenderTarget2D(graphicsDevice, 1281, 721);
    graphicsDevice.SetRenderTarget(renderTarget2d);

    spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null);

    graphicsDevice.Clear(Color.DarkBlue);

    for (int y = 0; y < mapData.GetLength (0); y++) 
    {
        for (int x = 0; x < mapData.GetLength (1); x++) 
        {
            int index = mapData [y, x];
            Texture2D texture = tileTextures[index];

            spriteBatch.Draw(texture, new Vector2(x * tileSize + 1, y * tileSize + 1), null, Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
        }
    }

    spriteBatch.End();


    graphicsDevice.SetRenderTarget(null);

    map = renderTarget2d;
}
}
}

I call the MapMap method every draw call in the Game1 class. How can I get rid of the error and what is causing it?

I have no idea of mac, but the render target size (1281,721) is very odd. Some GPUs need the RT to have at least a width being a multiple of 4. You could try other sizes (i.e. 1024x1024) to discard this is not the problem

It has nothing to do with the size Im afraid, I’ve tried alot of different sizes, But if I do smaller sizes the longer it takes for the error to appear

If you’re running that piece of code several times (I didn’t read that bi), I think you should either reuse the RenderTarget or dispose after using it (If possible reuse it , and will give you better performance)