mitsuba-renderer / drjit

Dr.Jit — A Just-In-Time-Compiler for Differentiable Rendering
BSD 3-Clause "New" or "Revised" License
563 stars 40 forks source link

Is array of references no longer possible? #201

Closed YunHsiao closed 9 months ago

YunHsiao commented 9 months ago

By requiring array element types to be default constructible, references are no longer a valid array element type:

using FloatP = Packet<float, 8>;

FloatP a, b, c; // Generated from different structures

Array<const FloatP&, 3> v{a, b, c}; // Error: Type underlying drjit::Array must be default-constructible!

Is there any workarounds for making a vector made of references without copying?

rtabbara commented 9 months ago

Hi @YunHsiao ,

If you knew ahead of time the final array size you could go the other way and pre-allocate your array and reference the individual elements

using FloatP = Packet<float, 8>;

Array<FloatP, 3> v;
FloatP& a = v[0];
FloatP& b = v[1];
FloatP& c = v[2];

If that's perhaps a bit too unwieldy for your purposes I would say having an Array<FloatP*, 3> would be the way to go.

YunHsiao commented 9 months ago

Oh I just realized the data structure layout I use is is not quite right:

using FloatP = Packet<float, 8>;
// Declare a custom struct `MyStruct` using DRJIT_STRUCT infrastructures

using FloatX = DynamicArray<FloatP>;
using MyStructX_DoNotUse = MyStruct<FloatX>; // Total SoA layout, very painful to use packet-wise on struct member level

using MyStructP = MyStruct<FloatP>;
using MyStructX = DynamicArray<MyStructP>; // AoSoA layout, much better for packet traversal

Falls straight into the trap, haha. Closing this.