绘制多维数据集时内存不足异常 [英] OutOfMemory Exception when drawing cube

查看:154
本文介绍了绘制多维数据集时内存不足异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有平旋转立方体的类。每次我旋转立方体我重装了缓冲区,多维数据集的新值。

i have a class that draws and rotates a cube. every time i rotate the cube i reload the buffer with the new values for the cube.

    public void LoadBuffer(GraphicsDevice graphicsDevice)
    {
        buffer = new VertexBuffer(graphicsDevice, VertexPositionNormalTexture.VertexDeclaration, triangles * 3, BufferUsage.None);
        buffer.SetData<VertexPositionNormalTexture>(verts);
        graphicsDevice.SetVertexBuffer(buffer);
    }

    public void Draw(GraphicsDevice graphicsDevice)
    {
        graphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, triangles);
    }

然后调用Game.Draw的Cube.Draw方法

then call the Cube.Draw method in Game.Draw

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(ClearOptions.DepthBuffer | ClearOptions.Target, Color.White, 1f, 0);

        basicEffect.Parameters["WorldViewProj"].SetValue(world * view * projection);

        EffectPass pass = basicEffect.CurrentTechnique.Passes[0];
        if (pass != null)
        {
            pass.Apply();
            cube1.LoadBuffer(GraphicsDevice);
            cube1.Draw(GraphicsDevice);
            cube2.LoadBuffer(GraphicsDevice);
            cube2.Draw(GraphicsDevice);
            cube3.LoadBuffer(GraphicsDevice);
            cube3.Draw(GraphicsDevice);
        }
        base.Draw(gameTime);
    }

在几分钟左右,我上线了内存不足的异常:

after a couple of minutes or so i get an OutOfMemory Exception on the line:

buffer.SetData<VertexPositionNormalTexture>(verts);

可能有人请解释为什么会发生什么我能做些什么来解决这个问题。

could somebody please explain why this is happening and what i can do to solve it.

推荐答案

顶点缓冲区的非托管资源。垃圾收集器的不知道的,他们使用的是一大堆的非托管内存(和GPU资源)在幕后。它只知道的是管理内存的微小点点每一个使用它。

Vertex buffers are unmanaged resources. The garbage collector doesn't know that they are using a whole bunch of unmanaged memory (and GPU resources) behind the scenes. All it knows about is the tiny little bit of managed memory that each one uses.

我说更多的非托管资源的XNA在<一个href="http://stackoverflow.com/questions/3050188/c-xna-load-objects-to-the-memory-how-it-works/3050639#3050639">my这个问题的答案。

I speak more about unmanaged resources in XNA in my answer to this question.

您可以致电的Dispose()每个 VertexBuffer 你漏呢(之前,但绘制完成后,因为它仍然在使用!),以释放非托管资源。这将避免内存不足的错误,但仍然会很慢!

You could call Dispose() on each VertexBuffer before you leak it (but after drawing finishes, as it will still be in use!), to release the unmanaged resources. This will avoid the out of memory error, but will still be very slow!

你真正应该做的是建立最低限度的必要顶点缓冲区的只有一次的。理想的地方要做到这一点是在你的 LoadContent 函数(然后的Dispose()它们在你的 UnloadContent 函数)。如果你有一大堆的立方体,所有你需要的是描述一个立方体,你每次重用你画一个立方体的时间单个顶点缓冲区。

What you really should be doing is creating the minimum necessary vertex buffers only once. The ideal place to do this is in your LoadContent function (and then Dispose() them in your UnloadContent function). If you have a whole bunch of cubes, all you need is a single vertex buffer that describes a cube, which you reuse every time you draw a cube.

显然,你不想来绘制所有的冰块在同一个地方。这是世界矩阵是什么。每次你画一个立方体,集 BasicEffect.World 来的变换矩阵为立方体,并调用应用()

Obviously you don't want to draw all your cubes in the same place. This is what the World matrix is for. Each time you draw a cube, set BasicEffect.World to your transformation matrix for that cube and call Apply().

(您设置的方式 WorldViewProj 直接是确定了。但是用漂亮的API是,好,更好。)

(The way you are setting WorldViewProj directly is ok too. But using the nice API is, well, nicer.)

如果旋转是你想要的,使用 Matrix.CreateFromYawPitchRoll(偏航,俯仰,滚动)来创建转换矩阵。

If rotation is what you want, use Matrix.CreateFromYawPitchRoll(yaw, pitch, roll) to create your transformation matrix.

有关更多细节,你的问题是类似另一个问题我有回答

For more details about this, your problem is similar to another question I have answered.

(需要注意的是,如果顶点本身的确实的改变每一帧,你应该使用的 DrawUserPrimitives 。但是请注意,这仍然要比让顶点着色器的GPU处理任何转换速度较慢。)

(Note that, if the vertices themselves really do change each frame, you should use DrawUserPrimitives. But note that this is still considerably slower than letting the vertex shader on the GPU handle any transformations.)

这篇关于绘制多维数据集时内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆