Commit Graph
100 Commits
Author SHA1 Message Date
João Capucho f038537e61 glsl-in: Add tests for output parameters casts 2023-02-08 11:39:09 +00:00
João Capucho 0a2e93c098 glsl-in: Perform output parameters implicit casts
Glsl defines under `Function Definitions` (Paragraph 6.1 in glsl 4.60),
the following:

> When function calls are resolved, an exact type match for all
> the arguments is sought.
> (...)
> If no exact match is found, then the implicit conversions in
> section “Implicit Conversions” will be applied to find a match.
> (...)
> Mismatched types on output parameters (out or inout) must have a
> conversion from the formal parameter type to the calling argument type.

The glsl frontend wasn't performing this implicit cast for output parameters.

This commit fixes that by using a proxy write, this
creates a spill variable with the correct type and in the call
prologue a conversion is made back to the original type and the
value is stored in the original variable.
2023-02-08 11:39:09 +00:00
João Capucho fc81a20f53 spv-in: Don't apply interpolation to fragment shaders outputs (#2239)
SPIR-V doesn't allow the `Flat`, `NoPerspective`, `Sample` or
`Centroid` decorations on fragment shaders outputs, but the spirv
frontend was applying default interpolation to all outputs
unconditionally.

This wasn't an issue for most shaders since they output floats and the
default values for them don't interfere with SPIR-V semantics, but if
the shader returned a uint or int the interpolation would be set to
`Flat` which as stated above is disallowed.

This commit fixes the issue by only running the default interpolation
code when constructing the entry point and if the stage/IO allow it.
2023-02-03 11:48:23 +01:00
João Capucho c371e7039d Implement the new checks for readonly stencils (#3443)
wgpu currently checks if the `write_mask` is 0 to determine wether a
stencil is used as readonly or not. However Webgpu contains a more
complex ruleset that also checks the cull mode and face operations to
determine if the stencil is readonly or not.

This commit brings these new rules to wgpu.
2023-02-01 21:13:46 +01:00
João Capucho 0074c68ec4 valid: Check dependencies between functions calls
This commit enforces the forward dependency rules on the IR across
functions and their calls, this fixes a crash in a later stage of the
validator.
2023-01-31 07:35:31 -08:00
João Capucho bebaac93b6 valid: Fix handle dependency validation
The handle dependency validation code was using the handle's index
directly while trying to erase the handle type. This would cause the
validator to crash while processing the first handle of the arena since
it would be trying to construct a `NonZeroU32` with a zero.

This commit fixes the issue by adding 1 to the index which not only
fixes this panic but also makes so that the created Handle is equal to
the passed handle (minus the type that was erased)

It also fixes the error message not including the subject's kind
2023-01-31 07:35:31 -08:00
João Capucho ae049edc49 glsl-in: Add not vector relational builtin 2023-01-31 13:29:37 +00:00
João Capucho 191d71c107 glsl-in: Add test for relational vector builtins 2023-01-31 13:29:37 +00:00
João Capucho 372a715434 glsl-in: Add double overloads for relational vector builtins
All relational vector builtins that operate on floats can also operate
on doubles but these overloads were not present. This commit fixes that.
2023-01-31 13:29:37 +00:00
João Capucho 08366c8fd0 glsl-in: Add bool overloads for relational vector builtins
The `equal` and `notEqual` builtins also operate on boolean vectors but
the overloads accepting them were not added. This commit fixes that.
2023-01-31 13:29:37 +00:00
João Capucho d1c29534c8 glsl-in: Fix position propagation in lowering (#2079)
When lowering `Select` expressions the position could be wrongfully
updated from `AccessBase { constant_index: false }` to
`AccessBase { constant_index: true }` this caused dynamic indexing
in an array behind a structure to fail if it was stored in a constant.

Furthermore the position could also be updated from `Rhs` to
`AccessBase`, this could cause issues because `AccessBase` doesn't
load variables (which `Rhs` does), so accessing a member from a
structure behind a pointer would return the wrong result.
2022-12-02 10:48:20 +01:00
João Capucho e07fd981c2 glsl-in: Update initializer list type when parsing (#2066)
Previously when parsing an initializer list, the type passed to
`parse_initializer` was not updated to reflect the child type, this
caused implicit conversions to not work and nested initializer lists to
not be allowed.
2022-10-05 11:17:25 -07:00
João Capucho c61f61ca0e dx12: Add support for push constants (#2866) 2022-09-19 20:47:24 -04:00
João Capucho 1a99c1cf06 wgsl-in: Fix bad span in assignment lhs error
The wgsl frontend was emiting errors for lhs expressions on assignments
that weren't references using a span that didn't skip blankspaces
causing the span to look weird (like starting at the end of the previous
line)

This is fixed by consuming the blankspace before constructing the span
2022-09-12 13:02:36 -07:00
João Capucho ff69053bf2 valid: Check regular functions don't have bindings
Checks that all arguments and return types have no bindings in regular functions.
2022-09-10 10:31:11 -07:00
João Capucho db1321cfb6 wgsl-in: Splat on compound assignments
Compound assignments on wgsl follow the same semantics as their
underlying operation, this includes the splatting behavior when mixing
scalar and vector operands, which was done for binary operations but not
for compound assignments.
2022-09-10 10:17:20 -07:00
João Capucho 7d0e9847b0 Remove the glsl-validate feature
When it was introduced it was supposed to allow for fast compiles by
skipping glsl specific validation, but as it turns the subset of glsl that's
compilable code is already pretty close to the subset of valid glsl code.

So the current code gated behind glsl-validate amounts to a single branch that
isn't even performance sensitive, and most of the validation is not specific to
glsl and is made by naga's validator which can be turned off, so the original
goal of fast compile times by disabling validation can still be accomplished.
2022-09-05 19:37:39 -07:00
João Capucho d9c9fe6218 glsl-in: Migrate to SymbolTable 2022-09-05 13:30:55 +01:00
João Capucho 544f14ad2f Add some methods to SymbolTable and implement Debug 2022-09-05 13:30:55 +01:00
João Capucho d64d78ff0b wgsl-in: Implement lexical scopes
Previously the wgsl frontend wasn't aware of lexical scopes causing all
variables and named expressions to share a single function scope, this
meant that if a variable was defined in a block with the same name as a
variable in the function body, the variable in the function body would
be lost and exiting the block all references to the variable in the
function body would be replaced with the variable of the block.

This commit fixes that by using the previously introduced `SymbolTable`
to track the lexical and perform the variable lookups, scopes are pushed
and popped as defined in the wgsl specification.
2022-09-01 17:31:50 -07:00
João Capucho 66f5506e10 Add a frontend agnostic symbol table
Adds a new type `SymbolTable` to allow sharing code between the
frontends related to variable name lookup and lexical scope management,
this way improvements in variable lookup can be shared among all
frontends.
2022-09-01 17:31:50 -07:00
João Capucho 9df243c42c hlsl-out: Add support for push constants (#2005)
Push constants need to be configured by the consumer which must pass the
bind target of the constant buffer used for the push constants.
2022-08-29 11:58:02 +01:00
João Capucho 6f4003ca9b Fix clippy lints for 1.63 (#2026) 2022-08-15 21:53:14 -04:00
João Capucho c6f34fa4d8 dot-out: Improvements
Improves the dot backend output by:
- Linking new nodes to the end of other blocks, instead of the beginning
- Generating merge nodes for conditional statements
- Generating connections from break/continue nodes to their target
- Introducing a "cfg only" mode that only generates statements
2022-08-03 15:01:26 -07:00
João Capucho b746e0a420 spv-in: Fix incorrect translation of SMod (#1995)
Since spirv's SMod doesn't map to naga's IR modulo operator the
instruction is mapped into it's direct formula, this formula requires
some casts from int to float and back.

These casts were present but their `convert` field was left to `None`
which implied that they were bitcasts, causing some wildly incorrect
results.

This commit fixes it by setting the `convert` field to the same size as
the result type.
2022-06-30 11:34:42 -04:00
João Capucho 67ef37ae99 Add support for 'break if' to IR, wgsl-in, and all backends. 2022-06-24 17:47:07 -07:00
João Capucho 6d78f1c06d hlsl-out: Fix indentation for continuing block
The continuing block in hlsl was being generated with the same indentation as the loop body
2022-06-17 16:34:23 +02:00
João Capucho cafdb160ae glsl-out: Fix indentation for continuing block 2022-06-17 16:17:22 +02:00
João Capucho 357da63076 Patch ron version (#1986)
Require at least version 0.7.1 of ron, this version changed how floating points are
serialized by forcing them to always have the decimal part, this makes it backwards
incompatible with our tests because we do a syntatic diff and not a semantic one.
2022-06-15 15:17:59 -04:00
João Capucho 98bc8fedf9 Apply comments 2022-06-14 16:04:08 -07:00
João Capucho ae58fbf4d0 Add tests 2022-06-14 16:04:08 -07:00
João Capucho 70743ee7dc glsl-in: Perform casts in int only math functions
Some functions like abs only accept signed integers while naga's IR
accepts both signed and unsigned integers, this wasn't accounted for in
the glsl backend causing it to produce code that wouldn't type check.

This commit addresses it by performing casts from uint to int of the
function argument and then back from int to uint for the function
return.
2022-06-14 16:04:08 -07:00
João Capucho ae50f31d4f glsl-in: Don't allow empty last case in switch
The GLSL spec forbids switch statements with an empty last case, so we
check that now and throw an error if necessary
2022-06-14 22:31:27 +01:00
João Capucho dab932e8ce glsl-in: Fix last case falltrough and empty switch
GLSL allows the last case of a switch statement to not have a `break`
statement causing it to be marked as fall-trough, naga's IR on the other
hand doesn't allow the last case to be fall-trough, this is fixed by
force marking it in the glsl frontend as not fall-trough.

GLSL also allows empty switch statements and without default cases,
naga's IR requires there be a default case, this is fixed by adding an
empty default case in the glsl frontend if no default case was present
in the switch statement.
2022-06-14 22:31:27 +01:00
João Capucho 52bb25179b glsl-in: Splat inputs for smoothstep if needed
Glsl defines two overloads for smoothstep that accept `min` and `max` as
scalars and the value as a vector, naga's IR is stricter and only allows
operators with the same dimensions, so this inputs must be splatted.
2022-06-14 13:15:08 -07:00
João Capucho c7e6769001 Add tests 2022-06-14 13:14:43 -07:00
João Capucho 46bc882d19 msl: don't rely on cached expressions
Expressions marked for caching might not be cached, this can happen for
example when these expressions are pre emitted (for example Contants).
2022-06-14 13:14:43 -07:00
João Capucho b5a046982f glsl-out: don't rely on cached expressions
Expressions marked for caching might not be cached, this can happen for
example when these expressions are pre emitted (for example Contants).
2022-06-14 13:14:43 -07:00
João CapuchoandJasper St. Pierre 81e2b006e1 glsl-in: Add test for nested depth texture calls
Co-authored-by: Jasper St. Pierre <jstpierre@mecheye.net>
2022-06-14 13:11:30 -07:00
João Capucho 3d3e5ae00d glsl-in: Fix parameter not changing to depth
The conversion from sampled images to depth changed the `argument` type
but didn't change the `parameter` type (`argument` without pointer
indirection) causing 2+ deep propagation of depth images with function
arguments to fail.
2022-06-14 13:11:30 -07:00
João Capucho e461d30865 glsl-in: Fix matrix multiplication check
The previous check compared rows to rows and columns to columns but
multiplication of matrices only needs the columns of the left matrix to
be equal to the rows of the right matrix.
2022-05-30 22:54:14 +01:00
João Capucho cff744dc89 spv-in: Implement OpBitReverse and OpBitCount 2022-05-30 22:51:50 +01:00
João Capucho 9e75332283 glsl-in: Stop emitter in conditional
This wasn't being done when there were no implicit conversions, causing
the emitter to panic.
2022-05-30 13:27:06 -07:00
João Capucho 0aa7681165 glsl-out: Implement bounds checks for ImageLoad (#1889)
* glsl-out: Implement bounds checks for `ImageLoad`

* Enable image bounds check snapshot tests for GLSL.

In addition to the snapshot.rs changes, this entails adding an entry
point function to `bounds-check-image-restrict.wgsl` and
`bounds-check-image-rzsw.wgsl`, including appropriate data in the
param.ron files.

* Apply comments

Snapshot test changes:
Co-authored-by: Jim Blandy <jimb@red-bean.com>
2022-05-30 12:13:58 -07:00
João Capucho c7e245c083 Update README for glsl-in (#1951) 2022-05-29 19:50:30 -04:00
João Capucho 4cd997a3eb glsl-out: Fix storage format for Rgba8Unorm 2022-05-28 14:30:12 -07:00
João Capucho cf32c2b7f3 glsl-out: Fix feature search in expressions
It was also searching in all entry points instead of only in the current
one.

All samples queries need the ARB_shader_texture_image_samples extension
so we need to check if any samples queries are made and if so request
the extension.
2022-05-05 19:06:33 +01:00
João Capucho 883a88580f glsl-in: Apply comments 2022-05-01 13:52:04 -07:00
João Capucho 6a62d009ea glsl-in: Improve the context emitter interface
Adds a new method `emit_restart` to perform the common operation of flushing
the `emitter` to the body and starting it again.

Adds documentation to all context emitter related methods
2022-05-01 13:52:04 -07:00
João Capucho 1c02de537b glsl-in: Fix the ternary to behave as an if
The GLSL specification defines that only one of the branches should be
evaluated making the ternary operator syntatic sugar for an `if`.
2022-05-01 13:52:04 -07:00
João Capucho 954c1f6f4b glsl-in: Add tests for constants NaN handling (#1817) 2022-04-11 16:53:03 -07:00
João Capucho 4146cb24d0 glsl-in: Allow nested accesses in lhs positions
Also fixes access to runtime sized arrays behind named blocks
2022-03-27 21:47:51 -07:00
João Capucho 21f89b65e4 glsl-in: use forced conversions for vector/matrix constructors
The spec defines that for vector and matrix constructors all arguments
should use the same conversions as scalar constructors (forced conversions)
2022-03-27 21:44:17 -07:00
João Capucho 767d970312 glsl-in: Resume expression emit after imageStore
This wasn't done before which caused the frontend to crash if anything
appeared after it.
2022-03-27 21:42:02 -07:00
João Capucho 54ef2e39a0 glsl-in: Allow multiple array specifiers 2022-03-27 00:18:20 -07:00
João Capucho b568bfbbdf glsl-in: Fix memory qualifiers being inverted
Adds some documentation to better explain how the memory qualifier works
troughout the parser and some storage textures tests.
2022-03-27 00:02:26 -07:00
João Capucho f054aa94f4 glsl-in: Support arrays as input/output types
Like it was already done for structs, arrays are flattened and each
element is attributed a location that is it's index + the array base
location.
2022-03-03 21:42:48 -08:00
João Capucho a2bcdda81c glsl-in: Fix freestanding constructor parsing 2022-03-03 21:39:45 -08:00
João Capucho 51e66eceb2 glsl-in: Allow backtracking in the parser 2022-03-03 21:39:45 -08:00
João Capucho af8dd4b3bd glsl-in: Fix matrix by scalar operations 2022-03-02 22:27:24 -08:00
João Capucho 48320db507 glsl-in: Fix matrix by matrix division 2022-03-02 22:27:24 -08:00
João Capucho 7f485d262b glsl-in: Fix matrix comparisons 2022-03-02 22:27:24 -08:00
João Capucho d449d2e665 glsl-in: Add support for texelFetchOffset 2022-03-01 21:06:50 -08:00
João Capucho e672974d9d gles: Fix texture description for bgra formats (#2520) 2022-03-01 14:43:57 -05:00
João Capucho 98e7add6a2 glsl-out: texture function fixes
- samplerCubeShadow requires vec3 gradients
- Bias must be written after offset
- sampler1D hack on GLES requires vec2 gradients
- textureSize doesn't accept a lod argument for MS samplers
- sampler1DArray hack on GLES requires a y component on imageLoad
2022-02-23 23:23:05 -05:00
João Capucho 419a6c69f9 spv-out: Use OpImageQuerySize for MS images 2022-02-23 23:23:05 -05:00
João Capucho 1ccfc8991e glsl-in: Add test for conditional builtin inject 2022-02-19 10:42:44 -05:00
João Capucho 9f62feee87 glsl-in: inject sampler2DMSArray builtins on use 2022-02-19 10:42:44 -05:00
João Capucho 66d59eb7ac glsl-in: inject samplerCubeArray builtins on use
Arrayed cube images require a special capabilities in some backends, so
like how we already do with doubles, we now only inject them if a call
uses one as an argument.

This required some refractoring on the builtins handling of variations
but now the interface is cleaner and adding new variations (if needed)
should be easier.
2022-02-18 20:29:23 -05:00
João Capucho 9e4f678c13 glsl-in: Add tests for images 2022-02-16 21:56:32 +00:00
João Capucho 2ad4c13c98 Add support for image builtin functions 2022-02-16 21:56:32 +00:00
João Capucho 27cbce5b5d glsl-in: Add support for image declarations 2022-02-16 21:56:32 +00:00
João Capucho d40522329b spv-in: align array stride for undecorated arrays 2022-02-12 23:26:12 -05:00
João Capucho 1f14f7ea2a glsl-in: Texture builtins fixes
This one grew out of hand quick. Initially it was just replacing the bit
loops with a function driving the declarations to make the code sharing
better and support different scalar kinds.

Now it includes a lot of fixes:
- `textureSize` now also returns the array layers as the last component
  on the return (only for arrayed textures)
- `textureSize` now supports multisampled textures
- the `texture` family of functions now consumes a `vec3` coordinate
  vector for all 1D shadow textures
- Shadow textures can use the bias version of `texture` functions
  (temporarily disabled since naga doesn't support it)
- 3D textures can be used in `textureProj`
- `sampler2DArrayShadow` can't be used in `textureLod` or in `texture` with bias
- Cube textures cannot be used with offsets

Updates the tests to cover all functions
2022-02-10 10:11:28 -05:00
João Capucho e621acc09d glsl-in: Type qualifiers rework (#1713)
The previous implementation had many issues, most importantly it didn't
allow to implement (at least in a sane way) some future features like
default qualifiers and format qualifiers.

This implementations centralizes the qualifiers into a struct with a
hashmap for layout qualifiers, error reporting is also centralized which
means that `add_global_var` and `add_local_var` no longer need to
duplicate effort and the ugly loop + match combo is gone.

Finally some minor fixes are also done as part of this rework, like
a default location being provided for input/outputs variables and layout
qualifiers being allowed to be repeated (overwriting the previous one)
like the spec defines.
2022-02-04 15:06:49 -05:00
João Capucho 5f948e9b12 glsl-in: texelFetch accept multisampled textures (#1715) 2022-02-04 15:05:49 -05:00
João Capucho 54c3f5190c glsl-in: Fix panic when culling nested block (#1714)
Under certatin circumstances the parser would panic because of culling
using an index out of bound, for example take the following code:

```glsl
{
    x = 1; // Index 0 on nested block
    discard; // Index 1 on nested block
} // Index 0 on block
```

The parser seeing this would attempt to cull the block using the start
index of 1 which would be out of bound and panic. Now instead the nested
block properly does it's internal culling and the outer block culls
everything after the nested block only.
2022-02-04 14:21:16 -05:00
João Capucho dcd07f0391 Update to upstream naga 2022-01-22 18:36:26 -05:00
João Capucho 83d0d16177 hal/gl: Allow push constants trough emulation
Uses freestanding uniforms for push constants
2022-01-22 18:36:26 -05:00
João Capucho 81dc67402a glsl-out: push constants use anonymous uniforms
Previously this was done with UBOs but this posed some problems when
integrating with wgpu (see https://github.com/gfx-rs/wgpu/pull/2400)
2022-01-21 21:13:12 -05:00
João Capucho a1840beb1a glsl-out: Add test for push constants 2022-01-16 19:32:51 -05:00
João Capucho 91ca923553 glsl-out: Add support for push constant emulation 2022-01-16 19:32:51 -05:00
João Capucho b42bf8768e glsl-out: Write correct argument name when skipping samplers 2022-01-16 00:47:35 -05:00
João Capucho 70b5ddaaad spv-out: use OpFRem instead of OpFMod 2022-01-07 20:19:15 -05:00
João Capucho a8929603ac spv-in: OpVectorInsertDynamic allow unsigned index 2022-01-07 11:08:04 -05:00
João Capucho eeb3de6f86 spv-in: sign agnostic unary operations 2022-01-07 11:03:44 -05:00
João Capucho e2fc32387b spv-in: more sign agnostic binary operations 2022-01-07 11:03:44 -05:00
João Capucho 743d058b49 glsl-out: Use constructors shorthand for zero init 2022-01-04 13:52:00 +00:00
João Capucho b9c4893112 glsl-out: Write struct initializers 2022-01-04 13:52:00 +00:00
João Capucho 32205e3097 glsl-out: write array initializers 2022-01-04 13:52:00 +00:00
João Capucho 79233ba5e5 glsl-out: Fix array constants constructor 2022-01-04 13:52:00 +00:00
João Capucho 1d0f484b37 glsl-in: fix composite constructors (#1631)
* glsl-in: Remove unneeded mutability from reference

* glsl-in: Fix composite constructors

In the recent rework of the constructors it seems that the logic for
composite types (arrays and structs) was accidentally removed by me.
2021-12-28 20:16:56 -05:00
João Capucho 5a26606a09 glsl-in: Fix using swizzle as out arguments (#1632)
This worked at one point but because of some changes to the IR it was
temporarily broken.
2021-12-27 19:09:33 -05:00
João Capucho e1552fd9d5 wgsl-in: Implement matrix construction from scalar 2021-12-17 10:39:31 -05:00
João Capucho 33c1daecee glsl-in: Cull the body of do while if needed 2021-12-16 18:42:59 -05:00
João Capucho 322be0772e spv-in: Implement CompositeInsert for arrays
Only adds support for unspecialized arrays
2021-12-16 16:31:51 -05:00
João Capucho 12baa1e909 spv-in: Implement OpCopyObject
Treat it as a noop since the IR has no concept of copying objects
2021-12-16 13:11:20 -05:00
João Capucho 2a8d64fc59 spv-in: Implement remaining sign agnostic ops 2021-12-16 11:22:16 -05:00
João Capucho cf0c31cc85 spv-in: Allow branch weights in OpBranchConditional 2021-12-16 10:58:27 -05:00
João Capucho 9c75f3cdce glsl-in: Flatten entry point arguments
The IR doesn't allow having structs has bindings for entry point
input/output, so the glsl frontend must flatten them.

Glsl defines that the locations are sequential from the block base location.
2021-12-16 10:55:06 -05:00