ranks parameter in many C# directx calls

Started by
8 comments, last by Zarmax 21 years, 3 months ago
What the heck is this "ranks" paramter in a number of C# directX calls? For example: public Array LockRectangle( Type typeLock, int level, LockFlags flags, int[] ranks ); I can''t find a clue anywhere in the wonderful documentation...
Advertisement
it determines how many dimensions the array regturned has.

400 entries can be returned as one array of 400 items, or an array of 20x20 items.

Stumbled over it too :-) I had to ask.

Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)
RegardsThomas TomiczekTHONA Consulting Ltd.(Microsoft MVP C#/.NET)
Ah... ok IC.

like int[,] data = (int[,])textureMap.LockRectangle(typeof(int), 0, 0, localWidth, localHeight);

Would give me a 2 dimensional array. Makes sense.

Now, out of curiosity, is there any performance penalty for using a 2d array vs. a 1d array in C#? (more language C# specific, but applies here)

The reason I ask is I want to find the fastest way to blit from an array of ints (or bytes) to a texture for things like a real-time light map, or fractal cloud map. Probably only update the texture once per 100 frames or so, but still want it as fast as possible.
quote:Original post by Zarmax
Ah... ok IC.

like int[,] data = (int[,])textureMap.LockRectangle(typeof(int), 0, 0, localWidth, localHeight);

Would give me a 2 dimensional array. Makes sense.


Try

int[,] data = (int[,])textureMap.LockRectangle(typeof(int), 0, 0, new int[]{ localWidth, localHeight } );

instead. The ranks have to be in an array.
Actually they don''t have to be in an array. The ranks parameter is marked with the ParamArrayAttribute (''params'' in C# syntax) so it''s like C varargs.
quote:Original post by Anonymous Poster
Actually they don''t have to be in an array. The ranks parameter is marked with the ParamArrayAttribute (''params'' in C# syntax) so it''s like C varargs.


I never knew that. Thanks. Wouldn''t this confuse overloaded functions however?
quote:Original post by Zarmax
Now, out of curiosity, is there any performance penalty for using a 2d array vs. a 1d array in C#? (more language C# specific, but applies here)


Yes. Now, the main problem is that arrays do index boundary checks. A two dimensional array has two checks, a one dimensional one. If you are for speed, then:

(a) get a one dimensional array.
(b) declare an unsafe block and use pointers.

B is especially brutal - you can get tremendous speed bosts by accessing array elements by pointers. NORMALLY this is not worth it, but if you create vertices etc., then this can be pretty significant for THIS part (means: wait for the anylysis whether this is not premature optimisation anyway).

I found that getting an array is pretty slow compared to have my own array, and then use the lock method giving me a graphics stream. This way I can keep one byte array around and dont get a new one every lock - the byte array is anyway NOT a pointer to the memory area returned from the native lock method, but a separate object. Negative: you gan not get data out of a graphics stream, but who reads out of vertex buffers anyway :-)


Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)
RegardsThomas TomiczekTHONA Consulting Ltd.(Microsoft MVP C#/.NET)
quote:Original post by sark
I never knew that. Thanks. Wouldn''t this confuse overloaded functions however?

A params argument must be the last argument in the parameter list, and there can only be one of them.

There is no room for confusion, really.


"The fact that a believer is happier than a skeptic is no more to the point than the fact that a drunken man is happier than a sober one." -- George Bernhard Shaw
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
quote:Original post by Arild Fines
A params argument must be the last argument in the parameter list, and there can only be one of them.

There is no room for confusion, really.



What i meant was:


  public class whatever{		public static void Method(int blah, params int[] ranks)		{		}		public static void Method(int blah1, int blah2, int blah3)		{		}}  


It turns out with Method(1, 2, 3) it calls the second function instead of the first. Which makes sense, i guess.
>>Which makes sense, i guess.

Yes - both functions have a different signature.

This topic is closed to new replies.

Advertisement