Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

glsl-in: Fix missing stores for local declarations #2029

Merged
merged 2 commits into from
Aug 24, 2022

Conversation

adeline-sparks
Copy link
Contributor

Previously, if a local variable was declared with a constant value, we
would elide the store and instead give the variable an initial value (as
if it was a global variable). This caused variables to not be
re-initialized each time through a loop.

Fixes #1935, but the issue seems broad and might fix others.

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.

There are some problems with how the problem is handled but the overall approach is good, you should also change add_local_var to make the init field always None

# src/front/glsl/variables.rs:646
-                init: decl.init,
+                init: None,

src/lib.rs Outdated Show resolved Hide resolved
Ok(res) => Some(res),
// Note that even if we solve the constant, we only use the solution for global constants.
// Locals have to be re-assigned every time they are encountered.
Ok(res) if ctx.external => Some(res),
Copy link
Collaborator

Choose a reason for hiding this comment

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

This check should be moved before the constant is solved, no point in wasting time processing it if it isn't going to be used

Previously, if a local variable was declared with a constant value, we
would elide the store and instead give the variable an initial value (as
if it was a global variable). This caused variables to not be
re-initialized each time through a loop.
// If the variable is global and const qualified, solve the initializer for a constant
// and use that as the variable's initial value.
let is_const = ctx.qualifiers.storage.0 == StorageQualifier::Const;
let maybe_constant = match (init, ctx.external, is_const) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will prevent non constant globals from being evaluated, try this instead:

let maybe_constant = if ctx.external {
  match parser.solve_constant(ctx.ctx, root, meta) {
      Ok(res) => Some(res),
      // If the declaration is external (global scope) and is constant qualified
      // then the initializer must be a constant expression
      Err(err) is_const => return Err(err),
      _ => None,
  }
} else {
  None
};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, thanks! There's a lot of cases here and I still didn't have it clear in my head.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, if we have a non-constant qualified global with an initializer that can't be solved, should that also be an error?

Copy link
Collaborator

Choose a reason for hiding this comment

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

No glsl allows globals to refer to other globals which can't be constified

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fascinating, ok! I will go with this. Thanks again!

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.

Thanks for your contribution

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

Successfully merging this pull request may close these issues.

[glsl-in][wgsl-out] Nested loop miscompilation
2 participants