Commit Graph

83 Commits

Author SHA1 Message Date
Ryan Harrison
cf1bf1c6ae Update external/ to SPIR-V 1.5
Rolled the hashes used for glslang, SPIRV-Tools, and SPIRV-Headers to
HEAD, which includes the update to 1.5.

Added passing '--amb' to glslang, so I didn't have to explicitly set
bindings in a large number of test shaders that currently don't, and
now glslang considers them invalid.

Marked all shaders that no longer pass spirv-val as .invalid.
2019-09-18 16:04:27 -04:00
Chip Davis
cb35934248 MSL: Support dynamic offsets for buffers in argument buffers.
Vulkan has two types of buffer descriptors,
`VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC` and
`VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC`, which allow the client to
offset the buffers by an amount given when the descriptor set is bound
to a pipeline. Metal provides no direct support for this when the buffer
in question is in an argument buffer, so once again we're on our own.
These offsets cannot be stored or associated in any way with the
argument buffer itself, because they are set at bind time.  Different
pipelines may have different offsets set. Therefore, we must use a
separate buffer, not in any argument buffer, to hold these offsets. Then
the shader must manually offset the buffer pointer.

This change fully supports arrays, including arrays of arrays, even
though Vulkan forbids them. It does not, however, support runtime
arrays. Perhaps later.
2019-09-05 23:29:00 -05:00
Chip Davis
103817009c MSL: Force storage images on iOS to use discrete descriptors.
Writable textures cannot use argument buffers on iOS. They must be
passed as arguments directly to the shader function. Since we won't know
if a given storage image will have the `NonWritable` decoration at the
time we encode the argument buffer, we must therefore pass all storage
images as discrete arguments. Previously, we were throwing an error if
we encountered an argument buffer with a writable texture in it on iOS.
2019-09-05 11:01:05 -05:00
Chip Davis
39dce88d3b MSL: Add support for sampler Y'CbCr conversion.
This change introduces functions and in one case, a class, to support
the `VK_KHR_sampler_ycbcr_conversion` extension. Except in the case of
GBGR8 and BGRG8 formats, for which Metal natively supports implicit
chroma reconstruction, we're on our own here. We have to do everything
ourselves. Much of the complexity comes from the need to support
multiple planes, which must now be passed to functions that use the
corresponding combined image-samplers. The rest is from the actual
Y'CbCr conversion itself, which requires additional post-processing of
the sample retrieved from the image.

Passing sampled images to a function was a particular problem. To
support this, I've added a new class which is emitted to MSL shaders
that pass sampled images with Y'CbCr conversions attached around. It
can handle sampled images with or without Y'CbCr conversion. This is an
awful abomination that should not exist, but I'm worried that there's
some shader out there which does this. This support requires Metal 2.0
to work properly, because it uses default-constructed texture objects,
which were only added in MSL 2. I'm not even going to get into arrays of
combined image-samplers--that's a whole other can of worms.  They are
deliberately unsupported in this change.

I've taken the liberty of refactoring the support for texture swizzling
while I'm at it. It's now treated as a post-processing step similar to
Y'CbCr conversion. I'd like to think this is cleaner than having
everything in `to_function_name()`/`to_function_args()`. It still looks
really hairy, though. I did, however, get rid of the explicit type
arguments to `spvGatherSwizzle()`/`spvGatherCompareSwizzle()`.

Update the C API. In addition to supporting this new functionality, add
some compiler options that I added in previous changes, but for which I
neglected to update the C API.
2019-09-01 18:35:53 -05:00
Thomas Roughton
91b2f34a3d Update tests to account for all non-entry-point functions being inlined 2019-08-30 09:39:06 +12:00
Hans-Kristian Arntzen
9436cd3036 MSL: Deal with array copies from and to threadgroup. 2019-08-27 13:18:01 +02:00
Chip Davis
fb5ee4cb5c MSL: Adjust BuiltInWorkgroupId for vkCmdDispatchBase().
This command allows the caller to set the base value of
`BuiltInWorkgroupId`, and thus of `BuiltInGlobalInvocationId`. Metal
provides no direct support for this... but it does provide a builtin,
`[[grid_origin]]`, normally used to pass the base values for the stage
input region, which we will now abuse to pass the dispatch base and
avoid burning a buffer binding.

`[[grid_origin]]`, as part of Metal's support for compute stage input,
requires MSL 1.2. For 1.0 and 1.1, we're forced to provide a buffer.

(Curiously, this builtin was undocumented until the MSL 2.2 release. Go
figure.)
2019-07-24 08:56:15 -05:00
Hans-Kristian Arntzen
47a18b9f1b Simplify row-major matrix/vector multiplies. 2019-07-23 10:56:57 +02:00
Hans-Kristian Arntzen
be2fccd837 Tests run clean. 2019-07-22 10:23:39 +02:00
Chip Davis
058f1a0933 MSL: Handle coherent, volatile, and restrict.
This maps them to their MSL equivalents. I've mapped `Coherent` to
`volatile` since MSL doesn't have anything weaker than `volatile` but
stronger than nothing.

As part of this, I had to remove the implicit `volatile` added for
atomic operation casts. If the buffer is already `coherent` or
`volatile`, then we would add a second `volatile`, which would be
redundant. I think this is OK even when the buffer *doesn't* have
`coherent`: `T *` is implicitly convertible to `volatile T *`, but not
vice-versa. It seems to compile OK at any rate. (Note that the
non-`volatile` overloads of the atomic functions documented in the spec
aren't present in the MSL 2.2 stdlib headers.)

`restrict` is tricky, because in MSL, as in C++, it needs to go *after*
the asterisk or ampersand for the pointer type it's modifying.

Another issue is that, in the `Simple`, `GLSL450`, and `Vulkan` memory
models, `Restrict` is the default (i.e. does not need to be specified);
but MSL likely follows the `OpenCL` model where `Aliased` is the
default. We probably need to implicitly set either `Restrict` or
`Aliased` depending on the module's declared memory model.
2019-07-11 10:22:30 -05:00
Chip Davis
28454facbb MSL: Handle packed matrices.
The old method of using a different unpacked matrix type doesn't work
for scalar alignment. It certainly wouldn't have any effect for a square
matrix, since the number of columns and rows are the same. So now we'll
store them as arrays of packed vectors.
2019-07-10 18:37:31 -05:00
Hans-Kristian Arntzen
f8b084de61 MSL/HLSL: Support OpOuterProduct. 2019-07-01 10:57:27 +02:00
Hans-Kristian Arntzen
ff87419607 Deal with scalar input values for distance/length/normalize.
HLSL and MSL don't support it, so fall back to simpler intrinsics.
2019-06-28 11:20:14 +02:00
Hans-Kristian Arntzen
e2c95bdcbc MSL: Rewrite how resource indices are fallback-assigned.
We used to use the Binding decoration for this, but this method is
hopelessly broken. If no explicit MSL resource remapping exists, we
remap automatically in a manner which should always "just work".
2019-06-21 12:54:08 +02:00
Hans-Kristian Arntzen
a6798d06a2 MSL: Error out on int64_t/uint64_t buffer members.
Not supported for whatever reason.
2019-06-19 10:14:46 +02:00
Hans-Kristian Arntzen
a6b71ae999 MSL: Support 64-bit integers. 2019-06-19 09:55:00 +02:00
Hans-Kristian Arntzen
314efdcc42 MSL: Fix declaration of unused input variables.
In multiple-entry-point modules, we declared builtin inputs which were
not supposed to be used for that entry point.

Fix this, by being more strict when checking which builtins to emit.
2019-05-31 13:23:34 +02:00
Hans-Kristian Arntzen
42e64597a7 OpArrayLength must trigger active variables. 2019-05-27 16:44:02 +02:00
Hans-Kristian Arntzen
7b9e0fb428 MSL: Implement OpArrayLength.
This gets rather complicated because MSL does not support OpArrayLength
natively. We need to pass down a buffer which contains buffer sizes, and
we compute the array length on-demand.

Support both discrete descriptors as well as argument buffers.
2019-05-27 16:13:09 +02:00
Hans-Kristian Arntzen
55ff233526 MSL: Add test case for complex type alias. 2019-05-23 15:05:30 +02:00
Hans-Kristian Arntzen
96492648d4 MSL: Fix struct declaration order with complex type aliases.
MSL generally emits the aliases, which means we cannot always place the
master type first, unlike GLSL and HLSL. The logic fix is just to
reorder after we have tagged types with packing information, rather than
doing it in the parser fixup.
2019-05-23 14:54:04 +02:00
Hans-Kristian Arntzen
eaf7afed97 MSL: Support argument buffers and image swizzling.
Change aux buffer to swizzle buffer.
There is no good reason to expand the aux buffer, so name it
appropriately.

Make the code cleaner by emitting a straight pointer to uint rather than
a dummy struct which only contains a single unsized array member anyways.

This will also end up being very similar to how we implement swizzle
buffers for argument buffers.

Do not use implied binding if it overflows int32_t.
2019-05-18 10:30:06 +02:00
Chip Davis
9d9415754b MSL: Add support for subgroup operations.
Some support for subgroups is present starting in Metal 2.0 on both iOS
and macOS. macOS gains more complete support in 10.14 (Metal 2.1).

Some restrictions are present. On iOS and on macOS 10.13, the
implementation of `OpGroupNonUniformElect` is incorrect: if thread 0 has
already terminated or is not executing a conditional branch, the first
thread that *is* will falsely believe itself not to be. Unfortunately,
this operation is part of the "basic" feature set; without it, subgroups
cannot be supported at all.

The `SubgroupSize` and `SubgroupLocalInvocationId` builtins are only
available in compute shaders (and, by extension, tessellation control
shaders), despite SPIR-V making them available in all stages. This
limits the usefulness of some of the subgroup operations in fragment
shaders.

Although Metal on macOS supports some clustered, inclusive, and
exclusive operations, it does not support them all. In particular,
inclusive and exclusive min, max, and, or, and xor; as well as cluster
sizes other than 4 are not supported. If this becomes a problem, they
could be emulated, but at a significant performance cost due to the need
for non-uniform operations.
2019-05-15 17:40:04 -05:00
Bill Hollings
efbe7ca16f MSL: Fix infinite CAS loop on atomic_compare_exchange_weak_explicit(). 2019-04-05 21:28:57 -04:00
Hans-Kristian Arntzen
0909975655 MSL: Declare gl_WorkGroupSize constant with [[maybe_unused]].
Avoids ugly warnings on nearly every compute shader.
We could do analysis to detect whether we need to emit this constant,
but it's a bit tedious to figure out if an OpConstantComponent is
actually used by opcodes, so just make it simple.
2019-03-28 10:54:18 +01:00
Hans-Kristian Arntzen
e2aadf8995 Rename "push descriptor set" to "discrete descriptor set".
Check for case where iOS doesn't support writable argument buffer
textures.
2019-03-15 21:53:21 +01:00
Hans-Kristian Arntzen
b3380ec9dd MSL: Support VK_KHR_push_descriptor.
If we have argument buffers, we also need to support using plain
descriptor sets for certain cases where API wants it.
2019-03-15 14:08:47 +01:00
Hans-Kristian Arntzen
e47a77d596 MSL: Implement Metal 2.0 indirect argument buffers. 2019-03-15 11:01:27 +01:00
Hans-Kristian Arntzen
4ef51331b2 Always value-cast FP16 constants instead of using literals.
GL_NV_gpu_shader5 doesn't support "hf", so to avoid lots of complicated
workarounds, just value-cast the half literals.
2019-02-20 12:30:01 +01:00
Chip Davis
056c0e207d Take the vertex count from any indirect parameters passed.
This is necessary to deal with indirect draws, where the draw parameters
are given in a buffer instead of passed by the CPU. For normal draws,
the draw parameters are set with Metal's `setVertexBytes:` method.

This undoes the change to add the vertex count to the aux buffer,
rendering that entire discussion largely moot. Oh well. It was a
discussion that needed to happen anyway.
2019-02-06 15:17:14 -06:00
Chip Davis
c51e5b7911 MSL: Add a setting to capture vertex shader output to a buffer.
This will be necessary to support transform feedback, as well as
tessellation shaders.
2019-02-05 20:00:10 -06:00
Hans-Kristian Arntzen
4e7777c443 Update to latest glslang/SPIRV-Tools.
Fix various bugs along the way.
2019-01-30 13:41:57 +01:00
Hans-Kristian Arntzen
912fde95f1 MSL: Use correct size for structs.
Need to align the size of structs to the natural alignment.
2019-01-28 15:20:30 +01:00
Hans-Kristian Arntzen
217eb5b5f9 MSL: Add a preliminary check for bad arrays of structs.
ArrayStride can be larger than the declared struct size.
We have no obvious solution for now, but warn about it in the MSL output
for the time being.
2019-01-28 15:20:30 +01:00
Hans-Kristian Arntzen
8c632da461 MSL: Use correct alignment rule for whole structs.
Structs are aligned as you would expect in MSL (maximum member
alignment), and it is not minimum 16 bytes like in std140.

Also rename the dummy "pad" members to a reserved naming scheme.
2019-01-28 15:20:30 +01:00
Hans-Kristian Arntzen
1040cf6cc1
Merge pull request #831 from cdavis5e/force-recompile-hooks
MSL: Hoist fixup hooks in entry_point_args() out of the compile loop.
2019-01-17 19:42:05 +01:00
Chip Davis
f500d2f70c MSL: Hoist fixup hooks in entry_point_args() out of the compile loop.
Otherwise, in the event of a forced recompile, we could end up adding
them twice.
2019-01-17 10:18:38 -06:00
Hans-Kristian Arntzen
f4b74f2f4f Add basic test for std140 small vector arrays. 2019-01-17 11:29:09 +01:00
Hans-Kristian Arntzen
15b52bee48 Deal with packing/unpacking on store.
Still a bit buggy, since we cannot deduce between float2[] and
packed_float2. Need a deeper refactor to plumb this through ...
2019-01-17 10:06:23 +01:00
Hans-Kristian Arntzen
d92de00cc1 Rewrite how IDs are iterated over.
This is a fairly fundamental change on how IDs are handled.
It serves many purposes:

- Improve performance. We only need to iterate over IDs which are
  relevant at any one time.
- Makes sure we iterate through IDs in SPIR-V module declaration order
  rather than ID space. IDs don't have to be monotonically increasing,
  which was an assumption SPIRV-Cross used to have. It has apparently
  never been a problem until now.
- Support LUTs of structs. We do this by interleaving declaration of
  constants and struct types in SPIR-V module order.

To support this, the ParsedIR interface needed to change slightly.
Before setting any ID with variant_set<T> we let ParsedIR know
that an ID with a specific type has been added. The surface for change
should be minimal.

ParsedIR will maintain a per-type list of IDs which the cross-compiler
will need to consider for later.

Instead of looping over ir.ids[] (which can be extremely large), we loop
over types now, using:

ir.for_each_typed_id<SPIRVariable>([&](uint32_t id, SPIRVariable &var) {
	handle_variable(var);
});

Now we make sure that we're never looking at irrelevant types.
2019-01-10 12:52:56 +01:00
Chip Davis
0d949e11ff Support bitcasts of 16-bit types. 2018-11-05 14:56:36 -06:00
Hans-Kristian Arntzen
480acdad18 Deal with OpSpecConstantOp used as array size.
When trying to validate buffer sizes, we usually need to bail out when
using SpecConstantOps, but for some very specific cases where we allow
unsized arrays currently, we can safely allow "unknown" sized arrays as
well.

This is probably the best we can do, when we have even more difficult
cases than this, we throw a more sensible error message.
2018-11-01 14:58:02 +01:00
Hans-Kristian Arntzen
d4ad1e266a Add test case for local_size_x_id and some complex cases. 2018-11-01 11:23:48 +01:00
Hans-Kristian Arntzen
af75ef005f Update glslang and SPIRV-Tools.
A lot of changes in spirv-opt output.
Some new invalid SPIR-V was found but most of them were not significant
for SPIRV-Cross, so just marked them as invalid.
2018-09-27 11:10:22 +02:00
Hans-Kristian Arntzen
2f65a1583e MSL: Support array-of-arrays composite construction. 2018-09-12 10:25:51 +02:00
Hans-Kristian Arntzen
38d19821d4 MSL: Support copying array of arrays. 2018-09-12 09:54:55 +02:00
Hans-Kristian Arntzen
aa17a02efb Add test shader for composite array initialization. 2018-09-10 10:05:00 +02:00
Hans-Kristian Arntzen
04b149feb0 Fix image load/store on cube arrays in MSL. 2018-05-25 12:43:25 +02:00
Hans-Kristian Arntzen
6b3da831be Declare read-only SSBOs as const device in MSL. 2018-05-25 10:14:05 +02:00
Hans-Kristian Arntzen
bcaae84c76 Deal with scoping for Private variables. 2018-05-16 10:49:30 +02:00