Some C++ functionality not available in C# [???]

G

Guest

Hi,

[A]. I am using managed code ( C# )
. I am working on a performance critical application that involve 3D
Graphics.
[C]. To achieve optimal performance I try to copy data as less as possible.
[D]. The view port is represented by a 3D Vector ( or DirectX.Vector3 )
[E]. The coordinates of this vector vary and are calculated on runtime by
combination of Euler matrix transformations.
[F]. Getting the Translation ( position ) of the view port from the Matrix
require me to copy the XYZ coordinates each time a frame is drawn.
[G]. Using C++ it is NOT need to copy the Translation coordinates
( The following sample shows how ).

Following is a C++ sample for achieving what was just
described ( what is the C# equivalent [???] )
struct Vector
{
float X;
float Y;
float Z;
};

struct Matrix
{
public:
float M11;
float M12;
float M13;
 
S

Sylvain Lafontaine

Yeah, with the unsafe mode of C#, you can use pointers and pin down memory
locations; which should give you many of the possibilities of C++ (but not
necessarily with the same syntaxe). However, in your case, you should use
C++ Managed instead if you want to make heavy use of pointers.

S. L.
 
S

Sylvain Lafontaine

Also, I'm not really sure about the performance hit of copying some data
here and there in comparaison of the number of CPU cycles it takes to apply
mathematical operations on them.

Before working on the ultimate performance, you should first work on
finishing your product; then, if you have some performance issues, you ask
your clients to wait one week or two before buying their next computer. The
more powerfull CPU that they will get by waiting a few weeks will more than
compensate for the small performance hit.

S. L.

Sylvain Lafontaine said:
Yeah, with the unsafe mode of C#, you can use pointers and pin down memory
locations; which should give you many of the possibilities of C++ (but not
necessarily with the same syntaxe). However, in your case, you should use
C++ Managed instead if you want to make heavy use of pointers.

S. L.

Nadav said:
Hi,

[A]. I am using managed code ( C# )
. I am working on a performance critical application that involve 3D
Graphics.
[C]. To achieve optimal performance I try to copy data as less as
possible.
[D]. The view port is represented by a 3D Vector ( or DirectX.Vector3 )
[E]. The coordinates of this vector vary and are calculated on runtime by
combination of Euler matrix transformations.
[F]. Getting the Translation ( position ) of the view port from the
Matrix
require me to copy the XYZ coordinates each time a frame is drawn.
[G]. Using C++ it is NOT need to copy the Translation coordinates
( The following sample shows how ).

Following is a C++ sample for achieving what was just
described ( what is the C# equivalent [???] )
struct Vector
{
float X;
float Y;
float Z;
};

struct Matrix
{
public:
float M11;
float M12;
float M13;
.
.
.
float M41; // X Translation
float M42; // Y Translation
float M43; // Z Translation
float M44;

float &X;
float &Y;
float &Z;

inline operator Vector*() { return static_cast<Vector*>(&X); }

Matrix() : X(M41), Y(M42), Z(M43) {}
};

I am using managed code (C#), is it possible to achive the same
Vector/Matrix association using managed code???? How?

Thanks,
Nadav
nadavrub A T gmail D O T com

 
S

sadhu

I didn't know that people would go to such levels to avoid copying...
Heck you should be programming in assembler, not C++. You're defeating
the type system in the worst possible way. Your code looks extremely
fragile to me, it's going to crash at runtime if Vector changes and
someone forgets to change Matrix..

Anyway, in .NET, the following code
class Vector
{
....
}
void GetVector()
{
return new Vector(x,y,z);
}

returns a reference to hte created vector and not a copy. It's like
returning a reference in C++. I think that's what you're trying to
avoid.

Regards
Senthil
 
G

Guest

Well, You hit the point, this exactly what i am trying to achieve, variables
M41, M42, M43 of the matrix ) represent the translation of a point in 3D
space, the same variables can be used to describe the same point using a
Vector, referring bought of the types to the same memory address will save
the burden of making sure the vector is updated each time the matrix change
and vide-versa...
 
G

Guest

Well ,Indeed, usage of managed C++/unsafe C# may resolve the problem just
described BUT concerning usage of 'Vector*' is required at a scope external
to the method ( where the matrix memory is pinned ) it may point to an
invalid memory address ( resulting of GC memory compacting )... I guess
managed code (whether safe or unsafe) doesn't support this kind of
functionality... It would be nice if it would be added in future versions of
the .NET framework...

Sylvain Lafontaine said:
Yeah, with the unsafe mode of C#, you can use pointers and pin down memory
locations; which should give you many of the possibilities of C++ (but not
necessarily with the same syntaxe). However, in your case, you should use
C++ Managed instead if you want to make heavy use of pointers.

S. L.

Nadav said:
Hi,

[A]. I am using managed code ( C# )
. I am working on a performance critical application that involve 3D
Graphics.
[C]. To achieve optimal performance I try to copy data as less as
possible.
[D]. The view port is represented by a 3D Vector ( or DirectX.Vector3 )
[E]. The coordinates of this vector vary and are calculated on runtime by
combination of Euler matrix transformations.
[F]. Getting the Translation ( position ) of the view port from the Matrix
require me to copy the XYZ coordinates each time a frame is drawn.
[G]. Using C++ it is NOT need to copy the Translation coordinates
( The following sample shows how ).

Following is a C++ sample for achieving what was just
described ( what is the C# equivalent [???] )
struct Vector
{
float X;
float Y;
float Z;
};

struct Matrix
{
public:
float M11;
float M12;
float M13;
.
.
.
float M41; // X Translation
float M42; // Y Translation
float M43; // Z Translation
float M44;

float &X;
float &Y;
float &Z;

inline operator Vector*() { return static_cast<Vector*>(&X); }

Matrix() : X(M41), Y(M42), Z(M43) {}
};

I am using managed code (C#), is it possible to achive the same
Vector/Matrix association using managed code???? How?

Thanks,
Nadav
nadavrub A T gmail D O T com

 
S

Sylvain Lafontaine

Pinned managed memory and unmanaged memory (memory directly allocated by
C++) won't be affected by the GC. Of course, Vector* must be allocated on
the heap and not on the stack.

S. L.

Nadav said:
Well ,Indeed, usage of managed C++/unsafe C# may resolve the problem just
described BUT concerning usage of 'Vector*' is required at a scope
external
to the method ( where the matrix memory is pinned ) it may point to an
invalid memory address ( resulting of GC memory compacting )... I guess
managed code (whether safe or unsafe) doesn't support this kind of
functionality... It would be nice if it would be added in future versions
of
the .NET framework...

Sylvain Lafontaine said:
Yeah, with the unsafe mode of C#, you can use pointers and pin down
memory
locations; which should give you many of the possibilities of C++ (but
not
necessarily with the same syntaxe). However, in your case, you should
use
C++ Managed instead if you want to make heavy use of pointers.

S. L.

Nadav said:
Hi,

[A]. I am using managed code ( C# )
. I am working on a performance critical application that involve 3D
Graphics.
[C]. To achieve optimal performance I try to copy data as less as
possible.
[D]. The view port is represented by a 3D Vector ( or DirectX.Vector3 )
[E]. The coordinates of this vector vary and are calculated on runtime
by
combination of Euler matrix transformations.
[F]. Getting the Translation ( position ) of the view port from the
Matrix
require me to copy the XYZ coordinates each time a frame is drawn.
[G]. Using C++ it is NOT need to copy the Translation coordinates
( The following sample shows how ).

Following is a C++ sample for achieving what was just
described ( what is the C# equivalent [???] )
struct Vector
{
float X;
float Y;
float Z;
};

struct Matrix
{
public:
float M11;
float M12;
float M13;
.
.
.
float M41; // X Translation
float M42; // Y Translation
float M43; // Z Translation
float M44;

float &X;
float &Y;
float &Z;

inline operator Vector*() { return static_cast<Vector*>(&X); }

Matrix() : X(M41), Y(M42), Z(M43) {}
};

I am using managed code (C#), is it possible to achive the same
Vector/Matrix association using managed code???? How?

Thanks,
Nadav
nadavrub A T gmail D O T com

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top