GetData

Hi, I’m trying to extract the colors of the pixels from a texture, but I’m jumping an error. My code is:

Color[] data = new Color[texture.Width * texture.Height];
texture.GetData(data);

And the error is:

HRESULT: [0x887A0005], Module: [SharpDX.DXGI], ApiCode: [DXGI_ERROR_DEVICE_REMOVED/DeviceRemoved], Message: Unknown

If anyone has any idea what may be happening. Thanks in advance.

Give more details of your test hardware…

8 gb de ram, i5 4ta, gtx 650 1gb

But the error also gives me on my two laptops, on my other desktop PC and on the office PC, I’m not sure it’s a hardware problem.

I’m not sure this is the way to use GetData. At least i’ve never seen it used this way with an array of Color.
Have you tried with an array of bytes? Sizexsizeynbbytes per channel I think

I cannot understand what you are saying… Can you clarify?

Try with an int array:

int[] data = new int[texture.Width * texture.Height];
texture.GetData(data);

I just tried this in a test project I have on my computer and didn’t get any runtime errors. My code is no different than yours…

_texture = this.Content.Load<Texture2D>("sprite");

Color[] data = new Color[_texture.Width * _texture.Height];
_texture.GetData<Color>(data);

Where are you calling this code? The error says device removed, are you doing it either before your device is created, or after it’s been disposed?

1 Like

It is not a problem of how or where I call the code. I ended up trying other pc (apart from the ones I had already tried), and in some it gives the error and in others it does not.

If you can reproduce this on some machines, but not on others, all using the same code… that is very strange.

As I said, the error message does seem to suggest that something funny is going on when interacting with the device. I haven’t written a lot of pure DirectX code, but from what little I’ve written as well as debugging through some weird issues in SlimDX once, I do remember it was a little finicky.

Can you boil this down to a small program that you can pass around, then paste the code for that? Also, if you’ve got hardware that you can 100% reproduce this on, it might be a good idea to grab the code for MonoGame, build it, then attach a debugger to your program and see if you can get any insights.

Sorry you’re having such a strange issue… things like this are extra frustrating :frowning:

Device removed is a strange error for this did you try adding the color typecast
Were is this being called from and are you doing anything unorthodox with the graphics device object.
Try a barebones test case.

New project, just with those two extra lines and the initialization of the texture. It is the same error in some pcs and not in others. I leave the code …

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Game4
{
///


/// This is the main type for your game.
///

public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D texture;

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

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        // TODO: use this.Content to load your game content here

        texture = Content.Load<Texture2D>("sign");


        Color[] data = new Color[texture.Width * texture.Height];
        texture.GetData(data);
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// game-specific content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();

        // TODO: Add your update logic here

        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here

        base.Draw(gameTime);
    }
}

}

You really need to give details of Which systems it does work on and which it does not.

Have they got Integrated Graphics or Dedicated or a mixture of both… I am suspecting the ones the error occurs on are laptops which have dual graphics systems…

EDIT

Simply saying it works on one and not the other, I would pick a needle in a haystack over that description any day!

Hmmm. Here is a link that explaines the error-code:

I had such an error, if I remember correctly, when I was toying around with texture-formats and the resulting getData-call wanted to access textures-coordinates that were out of bounds.
I don’t see that’s the case with your code and I didn’t encounter that error a second time…
Color should be the default for load-texture, shouldn’t it? What’s the format of the texture in the pipeline-tool?
Is that format available on every CPU driver? (I know that Color is…)

1 Like

Try moving the function out of load and calling it with a keypress.
Try to save the texture as a PNG see if you get a error.
You could also try to wrap the call with a try catch to attempt to get extra info.
Try using a small texture.


Ya you usually get a system array access violation for out of bounds.
I thought invalid format to at first but color should work as monogame converts hides everything to color I think.
I’m pretty sure I got this error to but i cant remember why either.
device errors usually mean its the card complaining. Sorry I can’t test it as my comps down ATM.

1 Like

The problem is that I remember that I got some errors like that by guys having broken GPU installations. Not many though… 3 or 4 at most (out of 780) and they ‘fixed’ them by re-installing the drivers or throwing away the old cards. Was not happy with that since I wanted to fix bugs and not advice my customers to change gear :slight_smile:
And the problem I was referring to is, that I don’t remember if that error was among those I just mentioned or not. Sorry.

In the original project I call the function in the Update, when an event is triggered and I always use png.

I wanted to use the GetData for the pixel to pixel collisions, in the end I ended up implementing them with a bitmap, but I am forced to have textures outside the Content to make the map of collisions to the textures that use that type of collisions.

In short, another mystery xd.

That error is an odd one that I have only come across once before. I never found the exact cause, but I believe it was because the GPU was crashing due to a texture format that we were using. The GPU crashed, therefore the OS thought the graphics card had been removed. This was several years ago, but I seem to recall that we were using 16-bit surface format for render targets on a Surface RT tablet. Changing it to Color seemed to make the error go away, but it was a shame to lose all that memory just to avoid a GPU crash.

Also, turn on DirectX Debug Layer to see if it shows an extra information. Open your Start menu (tap Windows key) and type ‘dxcpl’. Press Enter.

Click on “Edit List…” and add your executable.

Enable native code debugging in your project properties.

Debug the game in VS now and you will get a bunch of extra output in the Output Window. This may help diagnose the cause.

Remember to remove the application from the debug list in the DirectX Control Panel afterwards.

5 Likes