Sign in to follow this  
Madcap

Reference Counting Problem

Recommended Posts

Hi,

I wish to register my math classes with Angelscript and use reference counting to manage the memory.  However, the problem I have with this is the need to add an integer to the class to keep track of the number of references to the object used (addref/release methods).  The reason for this is that my math classes work with OpenGL, so when I pass an array (pointer) of matrices or vectors, they will work directly with opengl shader programs.  Adding an integer would break this and was wondering if there's a way to solve this problem without using external reference counters which would need a lookup and slow the whole thing down.  I'm hoping somebody can help me overcome this problem.

Thanks.

C++

Edited by Madcap

Share this post


Link to post
Share on other sites

Do you really need the vector and matrix types to be reference types? Couldn't they be value types? In my own math library I treat them as value types.

Unfortunately safety and performance do not always go hand-in-hand. Perhaps you should not expose the opengl shader programs directly to the script, and instead have proxy functions that transform the data prepared by the script into the format expected by the opengl shaders.

If you do not really bother with safety you can relax the controls in the script compiler by setting the engine property asEP_ALLOW_UNSAFE_REFERENCES to true. That might help allow you to work a little closer to the way C++ does without requiring the matrix and vector types to be reference types.

Along this way of thinking you can also provide functions to allow the scripts to manually allocate and free memory for matrices and vectors. You could register a matrix_ptr type to represent a pointer to a matrix allocated on the heap, then have implicit cast operators to cast to a matrix type.

 

Share this post


Link to post
Share on other sites

I just felt it was easier to share the math classes and pass objects to and from the scripts as pointers, whilst storing some of them in the scripts.  The problem I was having is the added 4 bytes from the counter.

Considering the openGL function takes a float pointer and that I would normally do the selection to build this array of data in the rendering loop, I decided I could probably just use a Union.  Something like this:

union
{
	struct
	{
		float x, y, z;
	};

	struct
	{
		float array[3];
	};
};

I can then just use obj.array and push that into a float array to then pass to openGL.  I'll look into some of your suggestions (I've only had a few days with AS).   

Edited by Madcap

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this