Gpuweb: WGSL: Proposal - square brackets for constructing an array

Created on 26 Oct 2020  路  6Comments  路  Source: gpuweb/gpuweb

I propose that square brackets in the context of an expression (not following an identifier) should construct an array<T, N>, with inferred T and N.

array<T,N>(e1,...,eN) is the same as [e1,...,eN], where T is the type of all elements, and N is the number of elements in the array.

It is a compile error if all the element are not of the same type.
It is a compile error if the array is empty.

For example:

var arr3_f32 : array<f32, 3> = array<f32, 3>(1.0, 2.0, 3.0);
var arr3_i32 : array<i32, 3> = array<i32, 3>(1, 2, 3);
var arr2_bool : array<bool, 2> = array<bool, 2>(true, false);
var arr2_vec3_f32 : array<vec3<f32>, 2> = array<vec3<f32>, 2>(vec3<f32>(1.0, 2.0, 3.0), vec3<f32>(4.0, 5.0, 6.0));

would be semantically identical to:

var arr3_f32 : array<f32, 3> = [1.0, 2.0, 3.0];
var arr3_i32 : array<i32, 3> = [1, 2, 3];
var arr2_bool : array<bool, 2> = [true, false];
var arr2_vec3_f32 : array<vec3<f32>, 2> = [vec3<f32>(1.0, 2.0, 3.0), vec3<f32>(4.0, 5.0, 6.0)];

The texture builtins proposal in #1176 includes a named parameter for explicit gradients, which of the type array<vec2<T>, 2>.
Actually passing one of these is extremely verbose:

    var gradients : array<vec2<f32>, 2> = array<vec2<f32>, 2>(calcDDX(), calcDDY());
    color = textureSample(tex2D, s, coords, grad: gradients);

With this proposal, this would become:

    color = textureSample(tex2D, s, coords, grad: [calcDDX(), calcDDY()]);
wgsl

All 6 comments

    var gradients : array<vec2<f32>, 2> = array<vec2<f32>, 2>(calcDDX(), calcDDY());

I think we decided the type annotation is supposed to become optional? making it:

    var gradients = array<vec2<f32>, 2>(calcDDX(), calcDDY());

I think we decided the type annotation is supposed to become optional?

Ah great - I'm new to the world of WGSL, and I wasn't too sure if/why they were required.
Does the spec need to be updated? Specifically:

  : IDENT COLON type_decl

Anyway, if it's optional - awesome.

That said, I still think this removes a lot of compiler-inferable smell, especially for the gradient case.

color = textureSample(tex2D, s, coords, grad: [calcDDX(), calcDDY()]);

Is more readable than:

color = textureSample(tex2D, s, coords, grad: array<vec2<f32>, 2>(calcDDX(), calcDDY()));

See https://github.com/gpuweb/gpuweb/issues/723 for type inference discussion.

Ok #723 is that all expressions have known types. It's a future direction to actually eliminate the extra verbiage. I think the path is relatively clear.

So the rough consensus is that we'll not need the extra array syntax of this proposal. Correct?

Discussed at the 2020-11-10 meeting.

Resolved: Go to post-MVP

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Nielsbishere picture Nielsbishere  路  5Comments

kainino0x picture kainino0x  路  5Comments

yukunxie picture yukunxie  路  6Comments

kainino0x picture kainino0x  路  3Comments

dneto0 picture dneto0  路  6Comments