Skip to main content
gamedev.net
🔒 Locked

[.net] array problem.

Started by avianRR 5 replies 1.1k views
Original Post
avianRR
avianRR
Ok, in C++ I have a list of vertex's that I create as follows. VERTEX *VertexList; VertexList = (VERTEX *)malloc(sizeof(VERTEX) * nVerts); I tried converting it to C# as follows. protected VERTEX[] VertexList; VertexList = new VERTEX[VertexListLength]; but it dosn't create a list of VERTEX's it creates a list of VERTEX pointers. I need them in a continuos block of memory so they can be pushed to the vertex buffer. I'm still haveing problems understanding how .NET does this stuff and I'm totally stumped. I've created nigthmarish C++ code when it comes to pointers and had it work with no memory leaks or invalid refferences but .NET class stuff is beyond me.
------------------------------Piggies, I need more piggies![pig][pig][pig][pig][pig][pig]------------------------------Do not invoke the wrath of the Irken elite. [flaming]
Bob Janova
Bob Janova
If you want vertices to be value types, declare it as a struct (not a class). I think an array of value types ends up in adjacent memory.

May I also suggest you look at Managed DirectX/XNA? ... or something like Tao.OpenGL? That way you don't have to deal with low level pointer stuff that's hard in C#.
Scet
Scet
Quote:
Original post by Bob Janova
May I also suggest you look at Managed DirectX/XNA? ... or something like Tao.OpenGL? That way you don't have to deal with low level pointer stuff that's hard in C#.


Actually in those you can only pass sequential struct data to vertex buffers as well. It not the same as pointers, but he still has to be careful.

Also you can tell the compiler that you want the structs to be sequential by including System.Runtime.InteropServices and then putting [StructLayout( LayoutKind.Sequential )] in front of it.

[StructLayout( LayoutKind.Sequential )]public struct SomeStruct{}
avianRR
avianRR
Thanks, I'm useing Tao for the rendering. The problem is that I need the optimized performance. I've also figured out some stuff about marshaling. All the problems I'm having with everything being treated as pointer is causing a LOT of performance problems.
My meshes morph at run time and need to change every frame. They are also very large. 100,000 + tri's. I want to use .NET so I can solve the user interface problems I'm having but the hit on the rendering performance is not acceptable. The C++ code is getting 200+ frames with a simple mesh (1,000-2,000 tri's) The C# code is getting at most 20 frams with the same mesh and from what I can tell it's all the fault of pointer and pointer management.
I tried changing the VERTEX classes to structs as well as some other objects that I'm having the same problem with and it seams to treat it the same way. Well, a few minor differences.
I think I'm gonna scrap all the converted code I've done (it's too damn inefficient anyway) and try to find a way to create a mixed project. C++ for the performance code (in a library?) and C#/.NET for the user interface.
------------------------------Piggies, I need more piggies![pig][pig][pig][pig][pig][pig]------------------------------Do not invoke the wrath of the Irken elite. [flaming]
Scet
Scet
I'm using Tao as well and I haven't really noticed any perform problems. For most OpenGL calls all you have to give it is a reference to the first element in the array and the number of elements.

Quote:

All the problems I'm having with everything being treated as pointer is causing a LOT of performance problems.


Structs are not treated as pointers, they are value types like int or float which is why they can be stored sequentially in memory. In C++ structs and classes are virtually the same, but in C# they are very different. I suspect that your performance problems have nothing to do with Tao since it's just a wrapper over OpenGL, the problem is probably an inefficient conversion of C++ code.
avianRR
avianRR
It's not Tao. It's the pointers. I'm doing some very complex manipulation of some of the vertex data. Here's one of the problems I have.

struct VERTEX{
float x,y,z;
void Mid(VERTEX v1,VERTEX v2);

}

void A_Function(int i1,int i2){
VERTEX v1,v2,v3;
v1 = VertexList[i1]; // <--- works fine
v2 = VertexList[i2]; // <--- works fine
v3 = new VERTEX(); // <--- without this line...
v3.Mid(v1,v2); // <--- this line give an error for a null refference.
}

while there is a way I can rewrite this function there are other more complex structs that it would be VERY difficult to find solutions for this. Right now I had to put in a lot of new's to solve them and I'm almost positive that's one of the problems. There are others though.
I'm still gonna look at using seperate languages. However I'm having trouble getting my C++ class library to be visible in C#.
------------------------------Piggies, I need more piggies![pig][pig][pig][pig][pig][pig]------------------------------Do not invoke the wrath of the Irken elite. [flaming]
vermilion_wizard
vermilion_wizard
struct VERTEX{float x,y,z;void Mid(VERTEX v1,VERTEX v2);}void A_Function(int i1,int i2){VERTEX v1,v2,v3;v1 = VertexList[i1]; // <--- works finev2 = VertexList[i2]; // <--- works finev3 = new VERTEX(); // <--- without this line...v3.Mid(v1,v2); // <--- this line give an error for a null refference.}


Something's not right here, because this shouldn't happen. That line may give you an uninitialized variable error, but if VERTEX is a struct, you cannot get a null reference there (unless something inside the Mid function is causing it). Also, if you are getting an uninitialized variable error, this will only happen at compile time, and null reference exceptions only occur during runtime.

Topic Locked

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

Sign in to reply to this topic.