add support for vectors as operands to math operations

This commit is contained in:
りき萌 2024-10-22 21:58:17 +02:00
parent 5b7d9586ea
commit 37c575748b
2 changed files with 131 additions and 14 deletions

View file

@ -1,3 +1,5 @@
use core::ops::{Add, Div, Mul, Neg, Sub};
use alloc::vec::Vec;
use crate::{bytecode::TagId, compiler::ClosureSpec, system::ChunkId};
@ -97,6 +99,17 @@ pub struct Vec4 {
pub w: f32,
}
impl From<f32> for Vec4 {
fn from(value: f32) -> Self {
Self {
x: value,
y: value,
z: value,
w: value,
}
}
}
impl From<Vec2> for Vec4 {
fn from(value: Vec2) -> Self {
Self {
@ -108,6 +121,82 @@ impl From<Vec2> for Vec4 {
}
}
impl From<Rgba> for Vec4 {
fn from(value: Rgba) -> Self {
Self {
x: value.r,
y: value.g,
z: value.b,
w: value.a,
}
}
}
impl Add for Vec4 {
type Output = Vec4;
fn add(self, rhs: Self) -> Self::Output {
Self {
x: self.x + rhs.x,
y: self.y + rhs.y,
z: self.z + rhs.z,
w: self.w + rhs.w,
}
}
}
impl Sub for Vec4 {
type Output = Vec4;
fn sub(self, rhs: Self) -> Self::Output {
Self {
x: self.x - rhs.x,
y: self.y - rhs.y,
z: self.z - rhs.z,
w: self.w - rhs.w,
}
}
}
impl Mul for Vec4 {
type Output = Vec4;
fn mul(self, rhs: Self) -> Self::Output {
Self {
x: self.x * rhs.x,
y: self.y * rhs.y,
z: self.z * rhs.z,
w: self.w * rhs.w,
}
}
}
impl Div for Vec4 {
type Output = Vec4;
fn div(self, rhs: Self) -> Self::Output {
Self {
x: self.x / rhs.x,
y: self.y / rhs.y,
z: self.z / rhs.z,
w: self.w / rhs.w,
}
}
}
impl Neg for Vec4 {
type Output = Vec4;
fn neg(self) -> Self::Output {
Self {
x: -self.x,
y: -self.y,
z: -self.z,
w: -self.w,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
#[repr(C)]
pub struct Rgba {
@ -117,6 +206,17 @@ pub struct Rgba {
pub a: f32,
}
impl From<Vec4> for Rgba {
fn from(value: Vec4) -> Self {
Self {
r: value.x,
g: value.y,
b: value.z,
a: value.w,
}
}
}
// NOTE: This is not a pointer, because IDs are safer and easier to clone.
//
// Since this only ever refers to refs inside the current VM, there is no need to walk through all