Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have an game which builds for WindowsGL and Windows8. The WindowsGL works fine, but the Windows8 build throws an error when trying to instantiate a new Texture2D.

The Code:

var texture = new Texture2D(CurrentGame.SpriteBatch.GraphicsDevice, width, 1); // Error thrown here...
texture.setData(FunctionThatReturnsColors());

You can find the rest of the code on Github.

The Error:

SharpDX.SharpDXException was unhandled by user code
  HResult=-2147024809
  Message=HRESULT: [0x80070057], Module: [Unknown], ApiCode: [Unknown/Unknown], Message: The parameter is incorrect.

  Source=SharpDX
  StackTrace:
       at SharpDX.Result.CheckError()
       at SharpDX.Direct3D11.Device.CreateTexture2D(Texture2DDescription& descRef, DataBox[] initialDataRef, Texture2D texture2DOut)
       at SharpDX.Direct3D11.Texture2D..ctor(Device device, Texture2DDescription description)
       at Microsoft.Xna.Framework.Graphics.Texture2D..ctor(GraphicsDevice graphicsDevice, Int32 width, Int32 height, Boolean mipmap, SurfaceFormat format, Boolean renderTarget)
       at Microsoft.Xna.Framework.Graphics.Texture2D..ctor(GraphicsDevice graphicsDevice, Int32 width, Int32 height)
       at BrewmasterEngine.Graphics.Content.Gradient.CreateHorizontal(Int32 width, Color left, Color right) in c:\Projects\Personal\GitHub\BrewmasterEngine\BrewmasterEngine\Graphics\Content\Gradient.cs:line 16
       at SampleGame.Menu.Widgets.GradientBackground.UpdateBounds(Object sender, EventArgs args) in c:\Projects\Personal\GitHub\BrewmasterEngine\SampleGame\Menu\Widgets\GradientBackground.cs:line 39
       at SampleGame.Menu.Widgets.GradientBackground..ctor(Color start, Color stop, Int32 scrollamount, Single scrollspeed, Boolean horizontal) in c:\Projects\Personal\GitHub\BrewmasterEngine\SampleGame\Menu\Widgets\GradientBackground.cs:line 25
       at SampleGame.Scenes.IntroScene.Load(Action done) in c:\Projects\Personal\GitHub\BrewmasterEngine\SampleGame\Scenes\IntroScene.cs:line 23
       at BrewmasterEngine.Scenes.Scene.LoadScene(Action`1 callback) in c:\Projects\Personal\GitHub\BrewmasterEngine\BrewmasterEngine\Scenes\Scene.cs:line 89
       at BrewmasterEngine.Scenes.SceneManager.Load(String sceneName, Action`1 callback) in c:\Projects\Personal\GitHub\BrewmasterEngine\BrewmasterEngine\Scenes\SceneManager.cs:line 69
       at BrewmasterEngine.Scenes.SceneManager.LoadDefaultScene(Action`1 callback) in c:\Projects\Personal\GitHub\BrewmasterEngine\BrewmasterEngine\Scenes\SceneManager.cs:line 83
       at BrewmasterEngine.Framework.Game2D.LoadContent() in c:\Projects\Personal\GitHub\BrewmasterEngine\BrewmasterEngine\Framework\Game2D.cs:line 117
       at Microsoft.Xna.Framework.Game.Initialize()
       at BrewmasterEngine.Framework.Game2D.Initialize() in c:\Projects\Personal\GitHub\BrewmasterEngine\BrewmasterEngine\Framework\Game2D.cs:line 105
       at Microsoft.Xna.Framework.Game.DoInitialize()
       at Microsoft.Xna.Framework.Game.Run(GameRunBehavior runBehavior)
       at Microsoft.Xna.Framework.Game.Run()
       at Microsoft.Xna.Framework.MetroFrameworkView`1.Run()
  InnerException: 

Is this an error that needs to be solved in MonoGame, or is there something that I need to do differently in my engine and game?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

This is actually an issue in MonoGame, if it was properly supporting the GraphicsProfiles introduced in XNA 4.0 the MaxTextureSize would be 2048 for Reach and 4096 for HiDef. You can check the Capabiities of your hardware by querying through SharpDX directly. Once you have access to a D3D9Driver instance, you'll have to create this yourself as I don't think there's a way to get from MonoGame, use the D3D9DeviceCaps to query the MaxTextureWidth and MaxTextureHeight.

share|improve this answer
add comment

Turns out the issue was not initializing and loading content in the correct order. I am building an engine and had to refactor it to work as a library for Windows 8 apps. When I shuffled some code around I removed some XNA-specific code and put in back in the wrong place.

Here's the basic flow of things:

  1. Constructor
    • Instantiate GraphicsDeviceManage
    • Set Content.RootDirectory (optional)
  2. Initialize
    • Update graphics options (optional)
  3. LoadContent
    • Instantiate SpriteBatch, using your GraphicsDeviceManage from before
    • Load the content for the game.

Update:

The actual error was that the texture I was trying to create was too big on the Surface RT. After looking in to it a bit, this is related to the graphics card and driver. My Surface RT has a limit of 2048(211), but my laptop goes up to 8192(213).

Here is a snippet that allows you to determine the maximum texture size based on each device and 'caches' the result. (PLEASE update it if you know a more elegant way!)

/// <summary>
/// Max texture size supported by the current device.
/// </summary>
public static int MaxTextureSize
{
   get
    {
        if (maxTextureSize == 0)
            maxTextureSize = CalculateMaxTextureSize();
         return maxTextureSize;
    }
}
private static int maxTextureSize;
private static int CalculateMaxTextureSize()
{
    var maxSize = 0;
    var i = 8;
    while (i < 20)
    {
        try
        {
            var size = (int)Math.Pow(2, i);
            new Texture2D(SpriteBatch.GraphicsDevice, size, 1);
            maxSize = size;

            size++;
            new Texture2D(SpriteBatch.GraphicsDevice, size, 1);
            maxSize = size;
        }
        catch (Exception e)
        {
            break;
        }
        i++;
    }
    return maxSize;
}

If you know the maximum size of a texture then you can just scale accordingly!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.