Skip to content

vec4 un-loop design #201

@bvssvni

Description

@bvssvni

When you need this:

(x(b[0]), x(b[1]), x(b[2]), x(b[3]))

In Dyon you can just type:

vec4 j x(b[j])

This unrolls the loop in the AST to a vec4, replacing j with numbers 0, 1, 2, 3.

The reason there are no {} brackets is because the loop does not work under composition. Neither can you use continue or break.

It is different from the other loop that do inference but are restricted by items only when indexing. For example, a[i] can be used with the other loops, but not a[(i+1)%3]. The latter works fine with a vec4 un-loop.

s(vec4, f64) for looking up scalar in vector

This is useful in combination with the vec4 un-loop.

vec3 and vec2 un-loops

The vec3 and vec2 un-loops sets the rest of the components to 0:

println(vec3 i i+1) // prints `(1, 2, 3)`
println(vec2 i i+1) // prints `(1, 2)`

Examples

Random values:

vec4 _ random()

Convert array of length 3 into a vec4:

p := [1, 2, 3]
println(vec4 i if i == 3 { 0 } else { p[i] }) // prints `(1, 2, 3, 0)`

Multiply two 4x4 matrices (row major):

multiply_matrix_matrix(a: [vec4], b: [vec4]) =
    sift i { vec4 j a[i] *. vec4 k s(b[k], j) }

Swizzle to (z, x, y, w):

a := (x, y, z, w)
println(vec4 i s(a, if i == 3 { i } else { (i+1)%3 })) // prints `(z, x, y, w)`

Motivation

This is designed for:

  • Flexible initialization of vec4 values
  • Reduce typing
  • Reduce bugs
  • Improve readability

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions