Skip to main content
GameDev.net gamedev.net
🔒 Locked

Avoiding .toArray() with DX vertex buffers in C#

Started by Carbon_PhD Jul 3, 2011 at 4:39 AM 11 replies 2.6k views
Original Post
Carbon_PhD
Carbon_PhD
Ok so i have profiled my program as it keeps running out of memory and one of the most expensive operations for memory is using the .ToArray() method with a List.

The list is continuously changing (both size and values) so i am continuously updating the VertexBuffers with the following code.
[color=green]//Set all vertex buffers for streaming new data
_straightVertexBuffer.SetData(_straightFibreVertices.ToArray(), 0, [color=#2b91af]LockFlags.Discard);
_catmullVertexBuffer.SetData(_catmullSplineVertices.ToArray(), 0, [color=#2b91af]LockFlags.Discard);
_springVertexBuffer.SetData(_springVertices.ToArray(), 0, [color=#2b91af]LockFlags.Discard);[font=arial, verdana, tahoma, sans-serif]Unfortunately the VB does not like it when i simply setdata with the list itself hence why i have used the ToArray() method. The list itself is cleared and repopulated when required and the VB is subsequently updated. Am i using too much memory by creating the new array and is there a way of disposing it or is it something else?[/font][font="arial, verdana, tahoma, sans-serif"]Anyone any ideas?[/font][font="arial, verdana, tahoma, sans-serif"]Thanks [/font]
Carbon_PhD
Carbon_PhD

Uh... any reason why your VB isn't just an Array to begin with?


Well the VertexBuffer and corresponding index buffer are what im using to create a DirectX mesh which i thought was right?

As for the actual vertexdata, i am using a list instead of an array as the length is continuously changing and i am constantly manipulating the data.

Thanks
smasherprog
smasherprog
Use an array as the above poster suggests. Using a list, then converting that to an array is verryyyy bad. The fact that the size changes doesn't mean you should use a list, nor does the fact that you modify the contents. Just make sure the array is big enough to hold the data, modify the array, then set your vertex buffers.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.
Carbon_PhD
Carbon_PhD

Use an array as the above poster suggests. Using a list, then converting that to an array is verryyyy bad. The fact that the size changes doesn't mean you should use a list, nor does the fact that you modify the contents. Just make sure the array is big enough to hold the data, modify the array, then set your vertex buffers.


Ok i see so its just best to stick with an array then? That sucks as i was enjoying the functions of lists and didn't want to have to keep iteratively clearing the arrays and repopulating them...

The VB itself does accept the lists and compiles but throws an error - "Operation is not valid due to the current state of the object". But i guess there isn't a way around this.

Cheers
smasherprog
smasherprog
don't clear the array. Just make sure there is enough space available for what ever operation you are going to do, and then work on the array.

You really don't need to shrink it either if the size does not vary much, i.e. by 10k is nothing on modern computers, so it is better to simply leave the extra space at the end unused because you might use it on the next iteration.

working on arrays is MUCH faster than on lists.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.
Carbon_PhD
Carbon_PhD

don't clear the array. Just make sure there is enough space available for what ever operation you are going to do, and then work on the array.

You really don't need to shrink it either if the size does not vary much, i.e. by 10k is nothing on modern computers, so it is better to simply leave the extra space at the end unused because you might use it on the next iteration.

working on arrays is MUCH faster than on lists.


Ok thanks for this. I have been using the profiler quite a lot am realising all my programming flaws one by one. The problems i am having are not necessarily speed just memory at this stage.

Cheers


smasherprog
smasherprog
lists consume more memory as well compared to arrays

this is for the C++ list ( Edited due to the post below correcting me)
If you have a list with 500 elements, each element being a 3 component position, the total size used to store in a list on a 32 bit machine is
4 * 2* 500 * 12 = 48,000 bytes
64 bit machine 8 * 2* 500 * 12 = 96,000 bytes

The 32 version of an array's size is 4 + 200 * 12 = 2,404 bytes
The 64 version of an array's size is 8 + 200 * 12 = 2,408 bytes

So, linked lists are not only slower, but also consume alot more memory because of the two pointers held per node.
Lists are generally used if there are alot of insertions and removals from random locations.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.
return0
return0
The C# Base Class Library List is a dynamically resizable array (a vector).
smasherprog
smasherprog
You are right.... Ok OK So the above post is good for c++ then. I learn something new every day.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.
Carbon_PhD
Carbon_PhD
Wow I never realised it was that much memory usage. No wonder i am running out of memory ... I use a lot of lists as I thought I was being clever...!
smasherprog
smasherprog
Those numbers were for c++, not c#. it was an error on my part.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.
Carbon_PhD
Carbon_PhD
ok so i have one more cheeky question. While you were very right in terms of the efficiency of Arrays over lists, i tried switching the following code:

_catmullShellMesh = [color="blue"]new [color="#2b91af"]Mesh(_meshIBCounter, _meshVBCounter, [color="#2b91af"]MeshFlags.Managed, [color="#2b91af"]CustomVertex.[color="#2b91af"]PositionColored.Format, _DXDevice);
_catmullShellMesh.VertexBuffer.SetData(_colouredCatmulltVertices.ToArray(), 0, [color="#2b91af"]LockFlags.Discard);
_catmullShellMesh.IndexBuffer.SetData(_catmullIndexArray.ToArray(), 0, [color="#2b91af"]LockFlags.Discard);
to this
_catmullShellMesh = [color="blue"]new [color="#2b91af"]Mesh(_meshIBCounter, _meshVBCounter, [color="#2b91af"]MeshFlags.Managed, [color="#2b91af"]CustomVertex.[color="#2b91af"]PositionColored.Format, _DXDevice);
_catmullShellMesh.VertexBuffer.SetData(_catmullVertexData, 0, [color="#2b91af"]LockFlags.Discard);
_catmullShellMesh.IndexBuffer.SetData(_catmullIndexData, 0, [color="#2b91af"]LockFlags.Discard);


But the code only works when i specify the exact right size for the vertexdata array and the index buffer array. So obviously when i oversize the arrays it doesnt work.
Now obviously if i oversize the first two terms in creating the mesh, it will work but old data that is not overwritten is displayed. Or am i just being stupid and making a big mistake?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.