0

In glsl and hlsl, I can define a function like this:

float voronoi(vec2 x, out int2 cell) {
    cell = ...
    return ...
}

However, it doesn't seem like this is possible in wgsl.

What's the intended replacement for this? I guess I could define a VoronoiResult struct, but it seems overly boilerplate heavy:

struct VoronoiResult {
    cell: vec2<i32>;
    distance: f32;
};

fn voronoi(x: vec2<f32>) -> VoronoiResult {
    // ...
    var ret: VoronoiResult;
    ret.distance = distance;
    ret.cell = cell;
    return ret;
}
bobbaluba
  • 3,584
  • 2
  • 31
  • 45

1 Answers1

1

The equivalent would be to use a pointer argument:

fn voronoi(x: vec2<f32>, cell: ptr<function, vec2<i32>>) -> f32 {
    *cell = vec2(1, 2);
    return 1.f;
}

@compute @workgroup_size(1)
fn main() {
  var a: vec2<i32>;
  var f = voronoi(vec2(1.f, 1.f), &a);
}

This produces the HLSL:

float voronoi(float2 x, inout int2 cell) {
  cell = int2(1, 2);
  return 1.0f;
}

[numthreads(1, 1, 1)]
void main() {
  int2 a = int2(0, 0);
  float f = voronoi((1.0f).xx, a);
  return;
}

You can also make the struct version shorter by using the struct initializer:

struct Out {
  cell: vec2<i32>,
  val: f32,
}

fn voronoi(x: vec2<f32>) -> Out {
    return Out(vec2(1, 2), 1.f);
}

@compute @workgroup_size(1)
fn main() {
  var f = voronoi(vec2(1.f, 1.f));
}
dj2
  • 9,534
  • 4
  • 29
  • 52