Commit Graph

392 Commits

Author SHA1 Message Date
Hans-Kristian Arntzen
5d82d32e0f Roll dependencies. 2021-01-08 10:41:51 +01:00
Hans-Kristian Arntzen
a4a9b53b5b MSL: Always enable Outputs in vertex stages.
Subsequent stages can legally attempt to read from these variables,
which causes compilation failure.

Always make sure we emit user outputs in vertex shaders if they are
active in the entry point.
2021-01-07 11:24:47 +01:00
Chip Davis
fd738e3387 MSL: Adjust FragCoord for sample-rate shading.
In Metal, the `[[position]]` input to a fragment shader remains at
fragment center, even at sample rate, like OpenGL and Direct3D. In
Vulkan, however, when the fragment shader runs at sample rate, the
`FragCoord` builtin moves to the sample position in the framebuffer,
instead of the fragment center. To account for this difference, adjust
the `FragCoord`, if present, by the sample position. The -0.5 offset is
because the fragment center is at (0.5, 0.5).

Also, add an option to force sample-rate shading in a fragment shader.
Since Metal has no explicit control for this, this is done by adding a
dummy `[[sample_id]]` which is otherwise unused, if none is already
present. This is intended to be used from e.g. MoltenVK when a
pipeline's `minSampleShading` value is nonzero.

Instead of checking if any `Input` variables have `Sample`
interpolation, I've elected to check that the `SampleRateShading`
capability is present. Since `SampleId`, `SamplePosition`, and the
`Sample` interpolation decoration require this cap, this should be
equivalent for any valid SPIR-V module. If this isn't acceptable, let me
know.
2020-11-23 10:30:24 -06:00
Hans-Kristian Arntzen
1ee2d13873 MSL: Add missing reference file. 2020-11-11 16:25:01 +01:00
Chip Davis
aca9b6879a MSL: Support pull-model interpolation on MSL 2.3+.
New in MSL 2.3 is a template that can be used in the place of a scalar
type in a stage-in struct. This template has methods which interpolate
the varying at the given points. Curiously, you can't set interpolation
attributes on such a varying; perspective-correctness is encoded in the
type, while interpolation must be done using one of the methods. This
makes using this somewhat awkward from SPIRV-Cross, requiring us to jump
through a bunch of hoops to make this all work.

Using varyings from functions in particular is a pain point, requiring
us to pass the stage-in struct itself around. An alternative is to pass
references to the interpolants; except this will fall over badly with
composite types, which naturally must be flattened.  As with
tessellation, dynamic indexing isn't supported with pull-model
interpolation. This is because of the need to reference the original
struct member in order to call one of the pull-model interpolation
methods on it. Also, this is done at the variable level; this means that
if one varying in a struct is used with the pull-model functions, then
the entire struct is emitted as pull-model interpolants.

For some reason, this was not documented in the MSL spec, though there
is a property on `MTLDevice`, `supportsPullModelInterpolation`,
indicating support for this, which *is* documented. This does not appear
to be implemented yet for AMD: it returns `NO` from
`supportsPullModelInterpolation`, and pipelines with shaders using the
templates fail to compile. It *is* implemeted for Intel. It's probably
also implemented for Apple GPUs: on Apple Silicon, OpenGL calls down to
Metal, and it wouldn't be possible to use the interpolation functions
without this implemented in Metal.

Based on my testing, where SPIR-V and GLSL have the offset relative to
the pixel center, in Metal it appears to be relative to the pixel's
upper-left corner, as in HLSL. Therefore, I've added an offset 0.4375,
i.e. one half minus one sixteenth, to all arguments to
`interpolate_at_offset()`.

This also fixes a long-standing bug: if a pull-model interpolation
function is used on a varying, make sure that varying is declared. We
were already doing this only for the AMD pull-model function,
`interpolateAtVertexAMD()`; for reasons which are completely beyond me,
we weren't doing this for the base interpolation functions. I also note
that there are no tests for the interpolation functions for GLSL or
HLSL.
2020-11-05 11:57:45 -06:00
Chip Davis
547c29f7bb MSL: Allow Bias and Grad arguments with comparison on Mac in MSL 2.3.
I kept the code to replace constant zero arguments, because `Bias` and
`Grad` still have some problems on desktop GPUs.

`Bias` works on AMD GPUs. `Grad` does not. Both work on Intel. Still
needs testing on NV. It will definitely work with Apple GPUs.
2020-10-30 11:14:59 -05:00
Chip Davis
d48d2a95c7 MSL: Allow post-depth coverage on Mac in MSL 2.3.
It's still only supported on Apple GPUs, but Macs will have those soon.
2020-10-27 22:07:01 -05:00
Chip Davis
064ed448b9 MSL: Don't remove periods from swizzle buffer index exprs. 2020-10-20 17:47:40 -05:00
Chip Davis
5845e009ea MSL: Handle Offset and Grad operands for 1D-as-2D textures. 2020-10-15 12:51:00 -05:00
Chip Davis
3e6010d8c5 MSL: Don't use a bitcast for tessellation levels in tesc shaders.
`half` cannot be bitcasted to `float`, because the two types are not the
same size. Use an expanding cast instead.

We were already doing this for stores to the tessellation levels; why I
didn't also do this for loads is beyond me.
2020-10-14 18:35:59 -05:00
Chip Davis
21d38f74ce MSL: Fix calculation of atomic image buffer address.
Fix reversed coordinates: `y` should be used to calculate the row
address. Align row address to the row stride.

I've made the row alignment a function constant; this makes it possible
to override it at pipeline compile time.

Honestly, I don't know how this worked at all for Epic. It definitely
didn't work in the CTS prior to this.
2020-10-13 20:51:56 -05:00
Chip Davis
7a5d0d6b29 MSL: Add missing interlock handling to atomic image buffers. 2020-10-13 11:44:17 -05:00
Hans-Kristian Arntzen
fab6ad234e
Merge pull request #1486 from cdavis5e/atomic-image-argument-buffer
MSL: Support atomic access to images from argument buffers.
2020-10-13 12:55:43 +02:00
Chip Davis
9cafea6cf8 MSL: Support atomic access to images from argument buffers.
This was not added when Epic contributed atomic image support.

Fixes #1484.
2020-10-13 02:37:18 -05:00
Chip Davis
2219c4a392 MSL: Support SPV_EXT_demote_to_helper_invocation for MSL 2.3.
MSL 2.3 has everything needed to support this extension on all
platforms. The existing `discard_fragment()` function was given demote
semantics, similar to Direct3D, and the `simd_is_helper_thread()`
function was finally added to iOS.

I've left the old test alone. Should I remove it in favor of these?
2020-10-13 00:25:32 -05:00
dan sinclair
9880b05572 Roll dependencies.
This CL rolls the spirv-tools, spirv-headers and glslang dependencies.
2020-09-22 12:31:38 -04:00
Chip Davis
4cf840ee7b MSL: Support layered input attachments.
These need to use arrayed texture types, or Metal will complain when
binding the resource. The target layer is addressed relative to the
Layer output by the vertex pipeline, or to the ViewIndex if in a
multiview pipeline. Unlike with the s/t coordinates, Vulkan does not
forbid non-zero layer coordinates here, though this cannot be expressed
in Vulkan GLSL.

Supporting 3D textures will require additional work. Part of the problem
is that Metal does not allow texture views to subset a 3D texture, so we
need some way to pass the base depth to the shader.
2020-09-02 09:18:25 -05:00
Chip Davis
cab7335e64 MSL: Don't set the layer for multiview if the device doesn't support it.
Some older iOS devices don't support layered rendering. In that case,
don't set `[[render_target_array_index]]`, because the compiler will
reject the shader in that case. The client will then have to unroll the
render pass manually.
2020-09-01 19:30:28 -05:00
Chip Davis
53080ecca8 MSL: Fix multiview view index calculation with a non-zero base instance.
Account for a non-zero base instance when calculating the view index and
the "real" instance index. Before, it was likely broken with a non-zero
base instance, since the calculated instance index could be less than
the base instance.
2020-08-31 20:33:44 -05:00
Hans-Kristian Arntzen
a07441568e Overhaul how we deal with reserved identifiers.
- Do not silently drop reserved identifiers in the parser. This makes it
  possible to reflect identifiers which are reserved by the
  cross-compiler module.
- Instead of dropping the name, emit _RESERVED_IDENTIFIER_FIXUP in the
  source to make it clear that a name has been rewritten.
- Document what is reserved and not.
2020-08-21 16:33:27 +02:00
Le Hoang Quyen
ab8eb70af1 Fix #1445: MSL: Enclose args when convert distance(a,b) to abs(a-b) 2020-08-13 21:16:08 +08:00
Chip Davis
3347b1076d MSL: Fix handling of matrices and structs in the output control point array.
Prior to this point, we were treating them as flattened, as they are in
old-style tessellation control shaders, and still are for structs in
new-style shaders. This is not true for outputs; output composites are
not flattened at all. This semantic mismatch broke a Vulkan CTS test.
It should now pass.
2020-08-03 17:18:18 -05:00
Tomek Ponitka
18f23c47d9 Enabling setting a fixed sampleMask in Metal fragment shaders.
In Metal render pipelines don't have an option to set a sampleMask
parameter, the only way to get that functionality is to set the
sample_mask output of the fragment shader to this value directly.
We also need to take care to combine the fixed sample mask with the
one that the shader might possibly output.
2020-07-24 11:19:46 +02:00
Chip Davis
688c5fcbda MSL: Add support for processing more than one patch per workgroup.
This should hopefully reduce underutilization of the GPU, especially on
GPUs where the thread execution width is greater than the number of
control points.

This also simplifies initialization by reading the buffer directly
instead of using Metal's vertex-attribute-in-compute support. It turns
out the only way in which shader stages are allowed to differ in their
interfaces is in the number of components per vector; the base type must
be the same. Since we are using the raw buffer instead of attributes, we
can now also emit arrays and matrices directly into the buffer, instead
of flattening them and then unpacking them. Structs are still flattened,
however; this is due to the need to handle vectors with fewer components
than were output, and I think handling this while also directly emitting
structs could get ugly.

Another advantage of this scheme is that the extra invocations needed to
read the attributes when there were more input than output points are
now no more. The number of threads per workgroup is now lcm(SIMD-size,
output control points). This should ensure we always process a whole
number of patches per workgroup.

To avoid complexity handling indices in the tessellation control shader,
I've also changed the way vertex shaders for tessellation are handled.
They are now compute kernels using Metal's support for vertex-style
stage input. This lets us always emit vertices into the buffer in order
of vertex shader execution. Now we no longer have to deal with indexing
in the tessellation control shader. This also fixes a long-standing
issue where if an index were greater than the number of vertices to
draw, the vertex shader would wind up writing outside the buffer, and
the vertex would be lost.

This is a breaking change, and I know SPIRV-Cross has other clients, so
I've hidden this behind an option for now. In the future, I want to
remove this option and make it the default.
2020-07-23 17:59:54 -05:00
dan sinclair
c4f3d4ae29 Roll GLSLang, SPIRV-Headers and SPIRV-Tools.
This Cl updates the various dependencies and the test file outputs.
2020-07-22 23:03:11 -04:00
dan sinclair
63fbdaca93 Roll deps.
This CL updates the GLSLang and SPIRV-Tools depedencies and updates test
files as needed.
2020-07-06 11:24:30 -04:00
Hans-Kristian Arntzen
711300baed MSL: Do not emit swizzled writes in packing fixups.
Similar to scalar access chain fix, this causes a read-modify-write on
memory we're not supposed to write to.
2020-07-06 10:03:46 +02:00
Hans-Kristian Arntzen
fa5b206d97 MSL: Workaround broken vector -> scalar access chain in MSL.
On MSL, the compiler refuses to allow access chains into a normal vector type.
What happens in practice instead is a read-modify-write where a vector type is
loaded, modified and written back.

The workaround is to convert a vector into a pointer-to-scalar before
the access chain continues to add the scalar index.
2020-07-06 10:03:44 +02:00
Hans-Kristian Arntzen
eb0f0323d3 HLSL: Workaround FXC bugs with degenerate switch blocks.
When we see a switch block which only contains one default block, emit a
do {} while(false) statement instead, which is far more idiomatic and
readable anyways.
2020-06-23 15:39:04 +02:00
Hans-Kristian Arntzen
02db4c1f16 MSL: Add tests for array copies in and out of buffers. 2020-06-18 11:59:02 +02:00
Chip Davis
5281d9997e MSL: Fix up input variables' vector lengths in all stages.
Metal is picky about interface matching. If the types don't match
exactly, down to the number of vector components, Metal fails pipline
compilation. To support pipelines where the number of components
consumed by the fragment shader is less than that produced by the vertex
shader, we have to fix up the fragment shader to accept all the
components produced.
2020-06-16 14:50:30 -05:00
dan sinclair
8bf916f575 Roll dependencies
This CL updates the GLSLang, SPIRV-Tools and SPIRV-Headers dependencies.
2020-05-20 10:27:51 -04:00
Hans-Kristian Arntzen
107ab7c2b7 MSL: Avoid packed arrays in more cases.
Extend the array stride relaxation to non-packed arrays as well, as
long as the array in question contains a single array element.
2020-05-06 10:27:12 +02:00
Hans-Kristian Arntzen
de3698f0e0 Add missing reference files from PR merge. 2020-05-06 10:08:01 +02:00
dan sinclair
171c646474 Roll GLSLang, SPIRV-Tools and SPIRV-Headers.
This CL rolls the various dependencies and updates tests to match.
2020-04-23 10:53:49 -04:00
Hans-Kristian Arntzen
17ad62eea4 MSL: Support edge case with DX layout in scalar block layout.
DX may emit ArrayStride and MatrixStride of 16, but the size of the
object does not align with that and expect to pack other members inside
its last member.

The workaround is to emit array size/col/row one less than we expect and
rely on padding to carve out a "dead zone" for the last member.
2020-04-20 15:29:24 +02:00
Hans-Kristian Arntzen
ebf463674d MSL: Allow removing clip distance user varyings.
Only safe if user knows that subsequent shader stage will not read clip
distance.
2020-04-20 09:58:40 +02:00
Chip Davis
96f7008aa8 MSL: Force disabled fragment builtins to have the right name.
DXVK emits SPIR-V where fragment shader builtins have names derived from
DXBC assembly, e.g. `oDepth` for `FragDepth`. When we declared the
disabled output, we used this name, but when referencing it, we
continued to use the GLSL name. This breaks compilation.
2020-04-15 19:25:18 -05:00
Chip Davis
495e48de44 MSL: Only disable output variables in fragment shaders.
Forgot to do this in #1319.

Fixes #1322.
2020-04-15 12:14:57 -05:00
Chip Davis
b29f83c383 MSL: Add options to control emission of fragment outputs.
Like with `point_size` when not rendering points, Metal complains when
writing to a variable using the `[[depth]]` qualifier when no depth
buffer be attached. In that case, we must avoid emitting `FragDepth`,
just like with `PointSize`.

I assume it will also complain if there be no stencil attachment and the
shader write to `[[stencil]]`, or it write to `[[color(n)]]` but there
be no color attachment at n.
2020-04-13 15:29:11 -05:00
Hans-Kristian Arntzen
d91e134500 MSL: Add native array test for composite array initialization. 2020-02-24 13:34:51 +01:00
Hans-Kristian Arntzen
20b28f72fa MSL: Reinstate workaround for returning arrays. 2020-02-24 13:04:10 +01:00
Hans-Kristian Arntzen
c9d4f9cd74 MSL: Add a workaround path to force native arrays for everything. 2020-02-24 12:47:14 +01:00
Dan Sinclair
7ec16b64c8 Roll GLSLang, SPIRV-Tools and SPIRV-Headers
This CL rolls the various dependencies of SPIRV-Cross and updates the
tests as needed.
2020-02-19 10:13:52 -05:00
Chip Davis
ae6c05f6f4 MSL: Move inline uniform blocks to the end of the argument buffer.
Limit inline blocks to one per descriptor set.

This should avoid the need for complicated code to calculate the
argument buffer ID stride of an inline uniform block. If there's demand
for more inline blocks, we can revisit this.
2020-01-25 13:40:51 -06:00
Chip Davis
fedbc35315 MSL: Support inline uniform blocks in argument buffers.
Here, the inline uniform block is explicit: we instantiate the buffer
block itself in the argument buffer, instead of a pointer to the buffer.
I just hope this will work with the `MTLArgumentDescriptor` API...

Note that Metal recursively assigns individual members of embedded
structs IDs. This means for automatic assignment that we have to
calculate the binding stride for a given buffer block. For MoltenVK,
we'll simply increment the ID by the size of the inline uniform block.
Then the later IDs will never conflict with the inline uniform block. We
can get away with this because Metal doesn't require that IDs be
contiguous, only monotonically increasing.
2020-01-24 18:51:24 -06:00
Hans-Kristian Arntzen
93f3265fe0 MSL: Deal with packing vectors for vertex input/fragment output. 2020-01-07 14:14:31 +01:00
Hans-Kristian Arntzen
a3fe9756d2 MSL: Support ClipDistance as an input stage variable.
MSL does not support this, so we have to emulate it by passing it around
as a varying between stages. We use a special "user(clipN)" attribute
for this rather than locN which is used for user varyings.
2019-12-02 13:19:42 +01:00
Hans-Kristian Arntzen
b85ab5f5ff MSL: Fix automatic binding allocation for image atomic buffers.
The Primary decoration was used by the atomic buffer, causing the
texture binding to be potentially overlapping with other resources.
2019-11-28 11:07:44 +01:00
Dan Sinclair
d409210ee5 Move all .invalid shaders into no-opt folders. 2019-11-05 13:19:19 -05:00