且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

绘制多维数据集时内存不足异常

更新时间:2023-01-14 13:46:31

顶点缓冲区的非托管资源。垃圾收集器的不知道的,他们使用的是一大堆的非托管内存(和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在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.)