SamplerState.LinearWrap black rectangle

I want to draw a repeating image using the following code:

Rectangle backgroundRect = new Rectangle(0, 0, background.Width, background.Height);
gameRef.spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
gameRef.spriteBatch.Draw (background, new Rectangle (0, 0, 500, 200), backgroundRect, Color.Purple);
gameRef.spriteBatch.End ();

When i execute this code, i only see a black rectangle with a 500px width and a 200px height. When i change LinearWrap to LinearClamp i see the image being stretched, so the image gets loaded.

Is this a bug in monogame or am i doing something wrong?

Thanks!

I’m getting this also trying to draw text on iOS using a SpriteFont. Setting the SamplerState to LinearClamp draws fine, setting it to LinearWrap draws just black boxes.

I did some more testing, and this is related to the texture size in question.

I created 4 different sized textures: 512x512, 400x400, 512x256 and 400x230.
Then creating a basic MonoGame for iOS project, and drawing these four textures, using LinearClamp you get:

Switching to LinearWrap you get:

This is a serious bug!

This problem was discussed back in 2014 in relation to problems running on Android and apparently fixed, some mention of problems in ESTexture2D.cs - see a post in StackOverflow here.

Code used:

private Texture2D[] _textures = new Texture2D[4];

protected override void LoadContent()
{
    // Create a new SpriteBatch, which can be used to draw textures.
    spriteBatch = new SpriteBatch(GraphicsDevice);

    //TODO: Use Content to load your game content here 

    for (int i=0; i<4; i++)
    {
         var fileStream = TitleContainer.OpenStream("Content/Main/LayoutTest" + (i+1).ToString() + ".jpg");
        _textures[i] = Texture2D.FromStream(GraphicsDevice, fileStream);
        fileStream.Dispose();
    }
}

protected override void Draw(GameTime gameTime)
{
    graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

    //TODO: Add your drawing code here

    int bx = 5;
    int by = 5;
    int w2 = Window.ClientBounds.Width / 2;
    int h2 = Window.ClientBounds.Height / 2;
    int w  = w2 - bx - bx;
    int h  = h2 - by - by;

    Rectangle[] r = new Rectangle[4]
    {
        new Rectangle(bx,      by,      w, h),
        new Rectangle(w2 + bx, by,      w, h),
        new Rectangle(bx,      h2 + by, w, h),
        new Rectangle(w2 + bx, h2 + by, w, h),
    };

    for (int i=0; i<4; i++)
    {
        spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearClamp, null, null, null, null);
        // spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, null);
        spriteBatch.Draw(_textures[i], r[i], Color.White);
        spriteBatch.End();
    }

    base.Draw(gameTime);
}

I’ve created a GitHub issue for this here.

1 Like