Hacker News
Zig Structs of Arrays (2024)
smallstepforman
|next
[-]
Meanwhile, game engines need operator overloading for adding/multiplying vectors (spatial transforms, lighting, physics) and core zig design philosophy prevents operator overloading.
Blind leading the blind. Disclaimer - I do professional rendering engines.
the__alchemist
|root
|parent
|next
[-]
This is a frustrating decision. My use cases for low level languages overlap closely with my use cases for vectors (etc) with operator overloading. It was one of the first things which put a bad taste in my mouth about Zig.
geysersam
|root
|parent
|next
|previous
[-]
aaaashley
|root
|parent
|next
[-]
(physics_data.velocity + omega * change) * frame_delta_time
to
physics_data.velocity.add(omega.mul(change)).mul(frame_delta_time)
We learn to read and think about math a certain way, which is incompatible with Zig. Also, Zig's design philosophy of "reading code over writing code" is incompatible with the kind of small modification-test-cycles required when doing games, and creative programming in general. So Zig is sort of DOA anyway for that kind of thing.
But I've been using Zig for non-game projects and it's been fantastic, so definitely not "Blind leading the blind" for the overall language design, imo.
smj-edison
|root
|parent
[-]
math("(v + Ω * c) * Δt", .{ .v = physics_data.velocity, .@"Ω" = omega, .c = change, .@"Δt" = frame_delta_time})
I know this is already possible with comptime, though I haven't implemented it yet since I haven't needed vector math in what I'm working on currently. Can't decide whether using math names is better or worse than using the full variable names though.
dnautics
|root
|parent
|next
[-]
smj-edison
|root
|parent
[-]
aaaashley
|root
|parent
|next
|previous
[-]
stouset
|root
|parent
|previous
[-]
smj-edison
|root
|parent
[-]
I'd argue though that the real disadvantage to having overloadable arithmetic is that you're limited to one implementation. This is actually my biggest beef with Rust, namely traits/type classes. It locks you into a single implementation when you may want to do something different based on the context. Zig pushes the dispatch decision to the callsite, not a trait subsystem (see how Zig implements hash mays for example). So I'd personally prefer to use a DSL, since it lets me specify what type of dispatch to use.
hmry
|root
|parent
|previous
[-]
x = x.add(step.mul(2)).mod(width)
Or in C x = imod(iadd(x, imul(step, 2)), width)
vs x = (x + 2*step) % width
For me the answer is very simple: Operators make it easier to read the code which makes it easier to spot bugs. It also makes it easier to turn formulas from textbooks into code.If 50% of the code you're working with is using vectors and matrices, not having operators for those parts is quite annoying.
Note that you can have vector operators without overloading, e.g. Odin has built in vector and matrix types.
But personally I think it's better to give the user more power instead of only letting the compiler author pick which types to allow operators on. Like how Java overloads + but only on the String class. Why do they get to do it, but not me?
applfanboysbgon
|root
|parent
|next
[-]
dnautics
|root
|parent
|next
|previous
[-]
const @"<+>" = @import("operator_module").plus;
...
const x = (a <+> b);
Ciantic
|root
|parent
|next
|previous
[-]
awesan
|root
|parent
|next
|previous
[-]
The general technique of SoA is pretty useful both in games and other applications, but of course I cannot speak to the specific use-case you are describing.
nvme0n1p1
|root
|parent
[-]
e4m2
|root
|parent
[-]
That being said, the parent commenter is actually referring to other recent proposals as opposed to existing `@Vector` functionality:
beepbooptheory
|root
|parent
|previous
[-]
Also does one really need operator overloading? That feels a little strong. I've gotten by with functions just fine.. Does that make the GPU not like me Mr. wise engineer?
Sweepi
|next
|previous
[-]
https://6it.dev/blog/infographics-operation-costs-in-cpu-clo...