Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

[glsl-in] add support for .length() #2017

Merged
merged 10 commits into from
Aug 8, 2022
Merged

Conversation

SpaceCat-Chan
Copy link
Contributor

this fixes #2016

i see two reasons you may not want to merge this

  1. this assumes that .length() is only valid on arrays and never has any other meaning
  2. this assumes that .length() has the same semantics as Expression::ArrayLength, which it may not have

other than that there should be no problems, other than that this was done in a hasty way

@JCapucho
Copy link
Collaborator

JCapucho commented Aug 6, 2022

this fixes #2016

thank you for working on this

i see two reasons you may not want to merge this

1. this assumes that .length() is only valid on arrays and never has any other meaning

According to the glsl specification this should be the case

2. this assumes that .length() has the same semantics as `Expression::ArrayLength`, which it may not have

There's a slight difference, Expression::ArrayLength follows wgsl semantics as almost all of naga's IR follows, in wgsl arrayLength returns a u32 which is glsl's uint, meanwhile .length() should return a int so you need to add a cast.

other than that there should be no problems, other than that this was done in a hasty way

I do have another issue with the current implementation, it specializes the parsing on .length() which is the only method on the base glsl specification, but I don't know if there any extensions that add other methods or might appear new extensions that do so, so I think we should parse a HirExprKind::Method and then handle this in lowering, this also has the added benefit that error messages can be better instead of no such field

@SpaceCat-Chan
Copy link
Contributor Author

latest commit adds a cast to Sint, and makes the parsing parse any method and then only specializes on .length in lowering instead

Copy link
Collaborator

@JCapucho JCapucho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing work, I wasn't expecting you to include argument handling but nice that you added it.

There are some issues that need to be addressed and I also added some nits.

Also please add a test (you can add it to expressions.frag)

src/front/glsl/parser/expressions.rs Outdated Show resolved Hide resolved
src/front/glsl/parser/expressions.rs Outdated Show resolved Hide resolved
src/front/glsl/context.rs Outdated Show resolved Hide resolved
src/front/glsl/context.rs Outdated Show resolved Hide resolved
src/front/glsl/context.rs Outdated Show resolved Hide resolved
src/front/glsl/ast.rs Outdated Show resolved Hide resolved
src/front/glsl/ast.rs Outdated Show resolved Hide resolved
src/front/glsl/context.rs Show resolved Hide resolved
src/front/glsl/context.rs Show resolved Hide resolved
Copy link
Collaborator

@JCapucho JCapucho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing work, but I noticed while testing your changes that ArrayLength only works on pointer to dynamic arrays, while glsl expects .length to work on all arrays.

To fix this we would need to check the type of the expression and if it's an array of fixed size or a pointer to it pass it as a constant.

Copy link
Collaborator

@JCapucho JCapucho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost perfect just left some nits and ask that you add a test for constant size arrays.

Comment on lines 1391 to 1392
match array_type {
&TypeInner::Array {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer de referencing the match expression

Suggested change
match array_type {
&TypeInner::Array {
match *array_type {
TypeInner::Array {

Comment on lines 1393 to 1395
base: _,
size: crate::ArraySize::Constant(size),
stride: _,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer using ellipsis ..

Suggested change
base: _,
size: crate::ArraySize::Constant(size),
stride: _,
size: crate::ArraySize::Constant(size),
..

stmt.hir_exprs[expr].meta,
body,
);
self.conversion(&mut array_length, meta, ScalarKind::Sint, 4)?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use forced_conversion in this case because constant array sizes might already be Sint

Suggested change
self.conversion(&mut array_length, meta, ScalarKind::Sint, 4)?;
self.forced_conversion(&mut array_length, meta, ScalarKind::Sint, 4)?;

tests/in/glsl/expressions.frag Show resolved Hide resolved
@JCapucho
Copy link
Collaborator

JCapucho commented Aug 8, 2022

Sorry I might have misled you but since we are already lowering with a non Lhs context we don't need to check if the array is wrapped in a pointer.

Comment on lines 1415 to 1421
self.forced_conversion(
parser,
&mut array_length,
meta,
ScalarKind::Sint,
4,
)?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only meant the previous branch, in here we can be sure it will be an Uint and a cast will be neeeded

Copy link
Collaborator

@JCapucho JCapucho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thanks for your contribution.

@JCapucho JCapucho merged commit f2624ea into gfx-rs:master Aug 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[glsl-in] naga rejects .length() unconditionally
2 participants