Algebra math implementation in Game Engine

Hello everyone!

We are evaluating Haxe as our scripting language with HashLink as target (compiled to C in final builds). Our current in-house scripting language has been working fine but it’s old and we don’t really have will power to maintain it in the long run.

Long story short, major concern is performance. Our current solution allows us to intechange data including structures between engine and scripting language. This allows us to do the following:

void VectorMulMatrix(Vector& vOut, const Vector& v, const Matrix& m) {
vOut = v * m;
}

and expose it scripts as:

import struct Vector { // structure is “aliased” with C++ implementation “by hand”
var x,y,z : float;
}

import operator* (a : ref Vector, b : ref Matrix) : vector;

Our old VM made sure that there are no copies of data made so when the VectorMulMatrix function is called the arguments point to memory managed directly by scripts.

How can I replicate this in Haxe/Hashlink?

Sepcifically:

  • being able to declare “C” like structures that will directly alias engine data
  • being able to pass this data in and out without any copying/marshalling/boxing etc.

If I can’t replicate it 1-1 what is the current best practice for this?

Thanks for any help!

Hey RexDrex, welcome!

I’ve not explored HashLink native interfacing yet but here’s an example for how you can achieve this with hxcpp (so the type is defined in C++ and passed around in haxe)

For HashLink, I’d recommend checking out the source code for Kha

Which supports HashLink/C and hxcpp – I don’t know anything about the details however, @RobDangerous is the brains behind it all

You should be able to get pretty great performance out of native haxe vector math too – I’ve been writing this: GitHub - haxiomic/vector-math: Write shaders in haxe: GLSL vector operations in haxe, complete with swizzles and all for my own projects – most vector and matrix elements wind up as stack variables when haxe is compiled so the they get auto-vectorized pretty well. That lib requires haxe 4.2 which is currently a nightly build Haxe git builds

being able to declare “C” like structures that will directly alias engine data

There are a few options for that, but all of them have inconvenients.
You could add hl_type *type to each of your c structs, and then use classes in haxe.

// c side
typedef struct {
   hl_type *type;
   int x;
   int y;
   int z;
} vector;

// in haxe
class Vec {
   var x:Int;
   var y:Int;
   var z:Int;
}

You could also use classes with the @:struct metadata without having to change your structs on the c side, but that messes up the signature of the function so you’d have to manually define the type string.

Or you could use abstracts + macros as I did in one of my projects :
C usage, C macro
Haxe usage, Haxe macro

being able to pass this data in and out without any copying/marshalling/boxing etc.

Afaik that should work fine as long as you only pass around basic types and pointers.