Commit Graph

535 Commits

Author SHA1 Message Date
Chip Davis
ba8593b112 Fix formatting. 2019-02-20 09:19:25 -06:00
Chip Davis
8095434dc4 MSL: Drop stores to nonexistent tess levels.
In SPIR-V, there are always two inner levels and four outer levels, even
if the input patch isn't a quad patch. But in MSL, due to requirements
imposed by Metal, only one inner level and three outer levels exist when
the input patch is a triangle patch. We must explicitly ignore any write
to the nonexistent second inner and fourth outer levels in this case.
2019-02-20 09:11:24 -06:00
Chip Davis
c8ee9fbe76 MSL: Expand quad gl_TessCoord to a float3.
This is the actual SPIR-V type of the builtin. We forced to a `float2`
in the declaration because that's what Metal wants.
2019-02-20 09:11:24 -06:00
Hans-Kristian Arntzen
58f264c99d
Merge pull request #865 from KhronosGroup/fix-863
Always value-cast FP16 constants instead of using literals.
2019-02-20 14:58:44 +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
Hans-Kristian Arntzen
056a0ba27e Fix case where a struct is loaded which contains a row-major matrix. 2019-02-20 12:19:00 +01:00
Chip Davis
41d9424233 MSL: Add an option to set the tessellation domain origin.
This is intended to be used to support `VK_KHR_maintenance2`'s
tessellation domain origin feature. If `tess_domain_origin_lower_left`
is `true`, the `v` coordinate will be inverted with respect to the
domain. Additionally, in `Triangles` mode, the `v` and `w` coordinates
will be swapped. This is because the winding order is interpreted
differently in lower-left mode.
2019-02-18 14:25:42 -06:00
Chip Davis
08863c1e28 Don't set any aliases or do any flattening for arrayed per-vertex I/O.
We already handle all that specially.
2019-02-15 17:24:16 -06:00
Chip Davis
6b7988046d Handle blocks of patch I/O.
In this case, each member of the block will be decorated with
`DecorationPatch`, rather than the block variable having the decoration.
2019-02-15 17:21:38 -06:00
Chip Davis
e75add42c9 MSL: Add support for tessellation evaluation shaders.
These are mapped to Metal's post-tessellation vertex functions. The
semantic difference is much less here, so this change should be simpler
than the previous one. There are still some hairy parts, though.

In MSL, the array of control point data is represented by a special
type, `patch_control_point<T>`, where `T` is a valid stage-input type.
This object must be embedded inside the patch-level stage input. For
this reason, I've added a new type to the type system to represent this.

On Mac, the number of input control points to the function must be
specified in the `patch()` attribute. This is optional on iOS.
SPIRV-Cross takes this from the `OutputVertices` execution mode; the
intent is that if it's not set in the shader itself, MoltenVK will set
it from the tessellation control shader. If you're translating these
offline, you'll have to update the control point count manually, since
this number must match the number that is passed to the
`drawPatches:...` family of methods.

Fixes #120.
2019-02-14 10:00:08 -06:00
Hans-Kristian Arntzen
cbd76e7c3b Run format_all.sh. 2019-02-14 09:28:46 +01:00
Hans-Kristian Arntzen
878c502f96 MSL: Hoist out complicated tesc workaround code. 2019-02-14 09:28:17 +01:00
Chip Davis
13df78bebf Unflatten inputs when copying to outputs.
This should fix a whole host of issues related to structs in the `Input`
class in a tessellation control shader.

Also, use pointer arithmetic instead of dereferencing the `ops` array.
This is critical in case we wind up stepping beyond the bounds of the
array.
2019-02-13 12:37:24 -06:00
Chip Davis
83b7e66218 Throw an error if the shader specifies isoline tessellation. 2019-02-11 17:21:36 -06:00
Chip Davis
0bb6bbda22 Never flatten outputs when capturing them.
There's no need to do so, since these are not stage-out structs being
returned, but regular structures being written to a buffer. This also
neatly avoids issues writing to composite (e.g. arrayed) per-patch
outputs from a tessellation control shader.
2019-02-11 17:18:54 -06:00
Chip Davis
8860a97d4a Fix formatting of uint32_t casts. 2019-02-11 16:14:00 -06:00
Chip Davis
1919eb1b46 Pass the original pointer type to ensure_correct_attribute_type().
This prevents us from overwriting the variable's type with a non-pointer
type.
2019-02-11 16:07:43 -06:00
Chip Davis
eb89c3a428 MSL: Add support for tessellation control shaders.
These are transpiled to kernel functions that write the output of the
shader to three buffers: one for per-vertex varyings, one for per-patch
varyings, and one for the tessellation levels. This structure is
mandated by the way Metal works, where the tessellation factors are
supplied to the draw method in their own buffer, while the per-patch and
per-vertex varyings are supplied as though they were vertex attributes;
since they have different step rates, they must be in separate buffers.

The kernel is expected to be run in a workgroup whose size is the
greater of the number of input or output control points. It uses Metal's
support for vertex-style stage input to a compute shader to get the
input values; therefore, at least one instance must run per input point.
Meanwhile, Vulkan mandates that it run at least once per output point.
Overrunning the output array is a concern, but any values written should
either be discarded or overwritten by subsequent patches. I'm probably
going to put some slop space in the buffer when I integrate this into
MoltenVK to be on the safe side.
2019-02-07 08:51:22 -06:00
Hans-Kristian Arntzen
d9ed3dcc7a
Merge pull request #848 from cdavis5e/capture-output-buffer
MSL: Add a setting to capture vertex shader output to a buffer.
2019-02-07 15:11:41 +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
f55253dc1b On second thought, don't use a feature struct for the aux buffer. 2019-02-06 14:45:26 -06:00
Chip Davis
d86adbe550 Add a structure to hold optional members of the aux buffer.
Programs can query the version to know what features are present, and
turn them on and off at will.
2019-02-06 14:26:06 -06:00
Hans-Kristian Arntzen
d5385190ff
Merge pull request #850 from KhronosGroup/fix-846
Support LUTs in single-function CFGs on Private storage class.
2019-02-06 11:34:30 +01:00
Hans-Kristian Arntzen
3e584f2c3f Support LUTs in single-function CFGs on Private storage class.
Fairly common pattern in unoptimized SPIR-V. Support this case as well.
2019-02-06 10:38:59 +01:00
Chip Davis
0757fae511 MSL: Stop passing the aux buffer around.
Since we pass the component swizzle around now, there's no need to pass
it to every function that takes a sampled image.
2019-02-05 20:04:32 -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
Chip Davis
ef0b1fc841 Move assertions after the check for equal types.
`bitcast_glsl_op()` is sometimes called for `Boolean` types, e.g. for
specialization constants. We don't want the assert to trip if this is
going to be a no-op anyway.
2019-01-31 14:28:21 -06:00
Hans-Kristian Arntzen
2ed171e525 GLSL/MSL: Implement 8-bit part of VK_KHR_shader_float16_int8.
Storage was in place already, so mostly just dealing with bitcasts and
constants.

Simplies some of the bitcasting logic, and this exposed some bugs in the
implementation. Refactor to use correct width integers with explicit bitcast opcodes.
2019-01-30 15:45:24 +01:00
Hans-Kristian Arntzen
2edee351f0 Run format_all.sh. 2019-01-30 13:42:50 +01: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
3e09879131 Support initializers on StorageClassOutput. 2019-01-30 10:29:08 +01:00
Hans-Kristian Arntzen
5ff12d780b Run format_all.sh. 2019-01-28 15:20:30 +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
18a4accd2f HLSL/MSL: Fix texture projection with Dref.
We need to divide the Dref by q.
2019-01-28 10:25:13 +01:00
Hans-Kristian Arntzen
437fc87a89 MSL: Deal with resource name aliasing.
Apparently we didn't use those yet. MSL seems to be able to alias struct
types and variable types to a degree, so that's why it has escaped
testing until now.
2019-01-18 16:27:57 +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
3aa08f764e MSL: Fix image load/store for short vectors.
Same fixes as for GLSL.
2019-01-17 14:54:29 +01:00
Hans-Kristian Arntzen
522c4eea97
Merge pull request #832 from KhronosGroup/fix-828
MSL: Support std140 packing rules for float[] and float2[]
2019-01-17 14:30:06 +01:00
Hans-Kristian Arntzen
73d9da7070 Avoid unintentional name conflict with HLSL backend. 2019-01-17 12:21:16 +01:00
Hans-Kristian Arntzen
76bf6d0c34 Fixup some MSL comments. 2019-01-17 11:47:37 +01:00
Hans-Kristian Arntzen
432aaed737 Need to know the original packed type when unpacking loads. 2019-01-17 11:39:46 +01:00
Hans-Kristian Arntzen
40e7723051 Run format_all.sh. 2019-01-17 11:29:50 +01:00
Hans-Kristian Arntzen
de7e5ccd8b Refactor out packed expressions to extended decorations.
Can't safely just cast to the original enum without lots of hacks.
2019-01-17 11:28:51 +01:00
Hans-Kristian Arntzen
72377366d3 Replace custom use of DecorationCPacked with an explicit one.
Will need to use more variants of this decoration, so might as well make
it clearer what is going on with CPacked.
2019-01-17 10:36:56 +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
Chip Davis
1d7d910765 MSL: Fix some types I missed when implementing variable pointers. 2019-01-16 16:15:57 -06:00
Hans-Kristian Arntzen
64ca1ec677 MSL: Start considering float[] and float2[] in std140 layout. 2019-01-16 16:16:39 +01:00
Hans-Kristian Arntzen
9e3a41ad00
Merge pull request #821 from cdavis5e/pass-sampled-images
MSL: Fix passing a sampled image to a function.
2019-01-15 09:05:54 +01:00
Chip Davis
664df22d12 MSL: Fix passing a sampled image to a function.
In the past, SPIRV-Cross threw an error in this case because it couldn't
work out which swizzle from the auxiliary buffer needs to be passed.
Now, we pass the swizzle around with the texture object, like a combined
image-sampler and its associated sampler.
2019-01-14 09:29:31 -06:00
Hans-Kristian Arntzen
b8033d7525 MSL: Add option to pad fragment outputs.
If not enough components are provided in the shader,
the shader MSL compiler throws an error rather than make components
undefined. This hurts portability, so we need to add explicit padding
here.
2019-01-14 15:11:52 +01:00
Hans-Kristian Arntzen
7ee04936ac MSL: Fix case where we pass arrays to functions by value.
MSL does not support value semantics for arrays (sigh), so we need to
force constant references and deal with copies if we have a different
address space than what we end up guessing.
2019-01-14 11:00:14 +01:00
Chip Davis
c4b08bd770 MSL: Add more illegal identifiers.
Add most macros from the Metal standard library headers that aren't in
the reserved namespace (i.e. those that don't start with `_`).
2019-01-14 00:08:09 -06:00
Hans-Kristian Arntzen
6e1c3ccb72 Run format_all.sh. 2019-01-11 12:56:00 +01:00
Hans-Kristian Arntzen
2fb9aa251e Workaround bugs on MSVC.
Bug:
https://developercommunity.visualstudio.com/content/problem/303996/c-error-c2668-ambiguous-overloaded-in-lambda-with.html
2019-01-11 09:29:28 +01:00
Hans-Kristian Arntzen
b629878f45 Make meta a hashmap.
A flat array was consuming way too much memory and was far too slow to
initialize properly with a very large ID bound (8 million IDs, showed up as #1 hotspot in perf).

Meta struct does not have to be in-order as we never iterate over it in
a meaningful way, so using a hashmap here is reasonable. Very few IDs
should need decorations or meta-data, so this should also be a quite
decent memory save.

For the pathological case, a 6x uplift was observed.
2019-01-10 14:04:01 +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
Hans-Kristian Arntzen
5345756cab MSL: Support composites inside I/O blocks
I had to refactor the existing add_interface_block as it was
getting extremely large. Now it's all split up into different readable
functions.
2019-01-09 09:33:10 +01:00
Hans-Kristian Arntzen
9c47b2837e
Merge pull request #807 from cdavis5e/variable-pointers
MSL: Support SPV_KHR_variable_pointers.
2019-01-09 09:07:43 +01:00
Chip Davis
fc02b3d656 Rename get_non_pointer_type() methods.
This better reflects their purpose now.
2019-01-08 12:55:22 -06:00
Chip Davis
a046f7a878 Add missing break. 2019-01-08 12:55:22 -06:00
Chip Davis
3394f53734 MSL: Fix mapping of identity-swizzled components.
Before, if any component was not identity-mapped, those components that
were still identity-mapped were set to 0. Now we properly leave them
alone.
2019-01-07 11:20:13 -06:00
Chip Davis
3bfb2f94d4 MSL: Support SPV_KHR_variable_pointers.
This allows shaders to declare and use pointer-type variables. Pointers
may be loaded and stored, be the result of an `OpSelect`, be passed to
and returned from functions, and even be passed as inputs to the `OpPhi`
instruction. All types of pointers may be used as variable pointers.
Variable pointers to storage buffers and workgroup memory may even be
loaded from and stored to, as though they were ordinary variables. In
addition, this enables using an interior pointer to an array as though
it were an array pointer itself using the `OpPtrAccessChain`
instruction.

This is a rather large and involved change, mostly because this is
somewhat complicated with a lot of moving parts. It's a wonder
SPIRV-Cross's output is largely unchanged. Indeed, many of these changes
are to accomplish exactly that! Perhaps the largest source of changes
was the violation of the assumption that, when emitting types, the
pointer type didn't matter.

One of the test cases added by the change doesn't optimize very well;
the output of `spirv-opt` here is invalid SPIR-V. I need to file a bug
with SPIRV-Tools about this.

I wanted to test that variable pointers to images worked too, but I
couldn't figure out how to propagate the access qualifier properly--in
MSL, it's part of the type, so getting this right is important. I've
punted on that for now.
2019-01-07 11:19:10 -06:00
Hans-Kristian Arntzen
5b8762223d Run format_all.sh. 2019-01-07 10:01:28 +01:00
Hans-Kristian Arntzen
649ce3c7bb MSL: Workaround missing gradient2d() for sampler_compare. 2019-01-07 10:01:00 +01:00
Hans-Kristian Arntzen
acae607703 Register implied expression reads in OpLoad/OpAccessChain.
This is required to avoid relying on complex sub-expression elimination
in compilers, and generates cleaner code.

The problem case is if a complex expression is used in an access chain,
like:

Composite comp = buffer[texture(...)];
vec4 a = comp.a + comp.b + comp.c;

Before, we did not have common subexpression tracking for
OpLoad/OpAccessChain, so we easily ended up with code like:

vec4 a = buffer[texture(...)].a + buffer[texture(...)].b + buffer[texture(...)].c;

A good compiler will optimize this, but we should not rely on it, and
forcing texture(...) to a temporary also looks better.

The solution is to add a vector "implied_expression_reads", which works
similarly to expression_dependencies. We also need an extra mechanism in
to_expression which lets us skip expression read checking and do it
later. E.g. for expr -> access chain -> load, we should only trigger
a read of expr when using the loaded expression.
2019-01-04 14:56:12 +01:00
Hans-Kristian Arntzen
318c17cbb2 Nonfunctional: Update copyright headers for 2019. 2019-01-04 12:38:35 +01:00
Bill Hollings
ab329a7906 MSL don't emit memory_scope after MSL 2.0. 2018-12-11 16:28:29 -05:00
Chip Davis
6db79b80c1 MSL: Use an enum instead of two mutually exclusive booleans.
NFCI.
2018-12-04 13:54:29 -06:00
Bill Hollings
2cd54e4e6d
Merge pull request #779 from cdavis5e/force-signedness
MSL: Force signedness of shader vertex attributes to match the host.
2018-12-04 09:37:55 -05:00
Chip Davis
06d483459b MSL: Force signedness of shader vertex attributes to match the host.
Based on a patch by Stefan Dösinger.

Metal cannot do signedness conversion on vertex attributes, and for good
reason. Putting a `uint4` into an `int4`, or a `char4` into a `uint4`,
would lose those values that are outside the range of the target type.
But putting a `uchar4` into a `short4` or an `int4`, or a `ushort4` into
an `int4`, should work. In that case, force the signedness in the shader
to match the declared type of the host.

Unfortunately, I don't really know how to automatically test this. This
remapping is done based on input parameters normally supplied by
MoltenVK. I'm not sure how we'd set this up for the command-line
`spirv-cross` tool.
2018-11-28 17:53:56 -06:00
Hans-Kristian Arntzen
61f1d8b2cf Support gl_HelperInvocation on GLSL and MSL.
There is no obvious builtin for this on HLSL.
2018-11-28 15:18:43 +01:00
Hans-Kristian Arntzen
510e1475c6
Merge pull request #756 from cdavis5e/relaxed-block-layout-2
MSL: Also pack 2- and 4- element vectors when necessary.
2018-11-15 10:09:09 +01:00
Chip Davis
6d675ae6a2 Correct carry/borrow bit checks.
Don't use `addsat()`/`subsat()`; that'll erroneously flag cases where
the sum is exactly the maximum integer value, or the difference is
exactly 0. Also, correct the condition for the `select()` function; it's
basically `mix()` with a boolean factor.

(What was I *thinking*?)
2018-11-14 10:13:56 -06:00
Chip Davis
cf2a890e4f MSL: Support extended arithmetic opcodes. 2018-11-13 17:33:03 -06:00
Chip Davis
bed4918cb5 MSL: Also pack 2- and 4- element vectors when necessary.
This is also needed for `VK_KHR_relaxed_block_layout` support.
2018-11-13 17:31:47 -06:00
Connor McLaughlin
1dd676c1de MSL: Emit wrapper for SSign (sign() for int types)
Metal does not define the sign() function for integer types, only
floating-point types.
2018-11-08 13:08:34 +10:00
Hans-Kristian Arntzen
cf5e1c2801
Merge pull request #743 from cdavis5e/relaxed-block-layout
MSL: Also pack members at unaligned offsets.
2018-11-07 19:38:56 +01:00
Chip Davis
e50eecfeeb MSL: Also pack members at unaligned offsets.
This is necessary to support `VK_KHR_relaxed_block_layout`.
2018-11-07 09:42:54 -06:00
Connor McLaughlin
801431b45b MSL: Print early_fragment_tests specifier before fragment
The compiler in 10.14 reports an error that the attribute cannot be
applied to types if the specifier is printed before fragment.
2018-11-07 21:54:19 +10:00
Chip Davis
0d949e11ff Support bitcasts of 16-bit types. 2018-11-05 14:56:36 -06:00
Chip Davis
ca4744ab72 Support constants of 16-bit integral type in GLSL and MSL.
Constants of 8-bit type aren't supported in GLSL, since there's no
extension letting you use them.
2018-11-02 14:39:55 -05:00
Chip Davis
117ccf407c Use specific base types for 8- and 16-bit integers. 2018-11-01 17:45:10 -05:00
Chip Davis
1fb27b4cda Add support for 8- and 16-bit types to GLSL and MSL.
In GLSL, 8-bit types require GL_EXT_shader_8bit_storage. 16-bit types
can use either GL_AMD_gpu_shader_int16/GL_AMD_gpu_shader_half_float or
GL_EXT_shader_16bit_storage.
2018-11-01 10:20:57 -05: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
6e99fcf695 Run format_all.sh. 2018-11-01 11:23:48 +01:00
Hans-Kristian Arntzen
62db535b3f Update tests. 2018-11-01 11:23:48 +01:00
Hans-Kristian Arntzen
5bcf02f7c9 Hoist out parsing module from spirv_cross::Compiler.
This is a large refactor which splits out the SPIR-V parser from
Compiler and moves it into its more appropriately named Parser module.

The Parser is responsible for building a ParsedIR structure which is
then consumed by one or more compilers.

Compiler can take a ParsedIR by value or move reference. This should
allow for optimal case for both multiple compilations and single
compilation scenarios.
2018-10-19 12:01:31 +02:00
Hans-Kristian Arntzen
a697299bc1 Refactor MSL to use SPIRCombinedImageSampler.
Avoids special "meta" data to express this type.
Makes MSL implementation in line with HLSL.
2018-10-05 09:49:57 +02:00
Hans-Kristian Arntzen
519565b030
Merge pull request #718 from cdavis5e/op-image-sampled-image
MSL: Handle OpImage on OpSampledImage expressions.
2018-10-04 21:30:08 +02:00
Chip Davis
9919fbbe0d MSL: Handle OpImage on OpSampledImage expressions.
I have seen this happen. The included test case is one such case.
2018-10-03 11:48:46 -05:00
Chip Davis
010fecc466 MSL: Swizzle gathers on depth textures as well.
Might as well.
2018-10-03 11:47:13 -05:00
Chip Davis
b7433c01ee Minor cleanups.
Throw an error for cases we don't support. Add a blank line after each
local array declaration.
2018-09-27 11:01:46 -05:00
Chip Davis
2506046cb4 Merge remote-tracking branch 'origin' into resource-arrays-msl 2018-09-27 10:50:16 -05:00
Hans-Kristian Arntzen
c07c303999 Use GL_EXT_samplerless_texture_functions in Vulkan GLSL. 2018-09-27 13:36:38 +02:00
Chip Davis
3a9af9681c MSL: Expand arrays of buffers passed as input.
Even as of Metal 2.1, MSL still doesn't support arrays of buffers
directly. Therefore, we must manually expand them. In the prologue, we
define arrays holding the argument pointers; these arrays are what the
transpiled code ends up referencing. We might be able to do similar
things for textures and samplers prior to MSL 2.0.

Speaking of which, also enable texture arrays on iOS MSL 1.2.
2018-09-26 20:48:09 -05:00
Hans-Kristian Arntzen
69b034f26e
Merge pull request #706 from cdavis5e/component-swizzle
MSL: Add an option to insert texture swizzles into generated shaders.
2018-09-25 10:06:03 +02:00
Chip Davis
7107f40f99 Provide feedback on whether or not the auxiliary buffer is needed. 2018-09-24 13:38:27 -05:00
Chip Davis
7956b002eb Give up on non-aliased sampled image parameters.
This needs extra work to map them back to the original resource.
2018-09-24 12:42:39 -05:00
Chip Davis
db7a40ce77 Use is_sampled_image_type() elsewhere. 2018-09-24 12:33:11 -05:00
Chip Davis
8855ea0a3e Move is_sampled_image_type() onto the Compiler class.
While I'm at it, don't use a bitwise op with a `bool` variable.
Apparently, MSVC doesn't like that.
2018-09-24 12:24:58 -05:00
Chip Davis
c11374c3cf Don't override Compiler::analyze_image_and_sampler_usage().
Just add our own separate function for analyzing sampled image usage.
2018-09-24 12:10:27 -05:00
Hans-Kristian Arntzen
34014886e3
Merge pull request #710 from cdavis5e/buffer-image-reads
MSL: Add spvTexelBufferCoord for buffer image reads, too.
2018-09-24 10:22:15 +02:00
Chip Davis
7cb817e40e Add spvTexelBufferCoord for buffer image reads, too.
I should've caught this when I fixed this for writes.
2018-09-23 14:37:03 -05:00
Chip Davis
4302c5abfb Pass the swizzle constants as a buffer.
It'll be useful to have an "auxiliary buffer" for other builtins--e.g.
`DrawIndex` (which should be easier to implement now), or `ViewIndex`
when someone gets around to implementing multiview.

Pass this buffer to leaf functions as well.

Test that we handle this for integer textures as well.
2018-09-22 19:36:11 -05:00
Chip Davis
c793868417 Pack texture component swizzles by bytes. 2018-09-22 19:15:15 -05:00
Chip Davis
7fff65a811 Remove extraneous space in enum class decl. 2018-09-21 13:52:20 -05:00
Bill Hollings
daa831f59d Fix integer precision warnings on assignments. 2018-09-20 16:10:42 -04:00
Chip Davis
2583321657 MSL: Add an option to insert texture swizzles into generated shaders.
It's intended to be used with MoltenVK to support arbitrary
`VkComponentMapping` settings. The idea is that MoltenVK will pass a
buffer (which it set to some buffer index that isn't being used)
containing packed versions of the `VkComponentMapping` struct, one for
each sampled image.

Yes, this is horribly ugly. It is unfortunately necessary. Much of the
ugliness is to support swizzling gather operations, where we need to
alter the component that the gather operates on--something complicated
by the `gather()` method requiring the passed-in component to be a
constant expression. It doesn't even support swizzling gathers on depth
textures, though I could add that if it turns out we need it.
2018-09-19 22:32:24 -05:00
Chip Davis
ec857f6778 Cast uses of Layer and ViewportIndex to the expected type. 2018-09-19 09:13:30 -05:00
Chip Davis
0e9ad14ba6 MSL: Handle the ViewportIndex builtin.
This requires MSL 2.0+.

Also, force `ViewportIndex` and `Layer` to be defined as the correct
type, which is always `uint` in MSL.

Since Metal doesn't yet have geometry shaders, the vertex shader (or
tessellation evaluation shader == "post-tessellation vertex shader" in
Metal jargon) is the only kind of shader that can set this output. This
currently requires an extension to Vulkan, which causes validation of
the SPIR-V binaries for the test cases to fail. Therefore, the test
cases are marked "invalid", even though they're actually perfectly valid
SPIR-V--they just won't work without the
`SPV_EXT_shader_viewport_index_layer` extension.
2018-09-18 09:52:30 -05:00
Chip Davis
7dcfed888a Use a hook to emit a local for the sample position.
That way, we don't have to handle it specially when constructing a call.
2018-09-17 11:51:09 -05:00
Chip Davis
72fc1cce53 Merge remote-tracking branch 'origin' into msl-sample-pos 2018-09-17 11:20:34 -05:00
Hans-Kristian Arntzen
a77880787d
Merge pull request #698 from KhronosGroup/fix-695
MSL: Support global I/O block and struct Input/Output usage.
2018-09-17 14:54:58 +02:00
Hans-Kristian Arntzen
340957a3ab Make fixup_hooks more flexible.
No reason why it needs to return a string.
Callbacks can just do one or more statements themselves.
2018-09-17 14:06:44 +02:00
Hans-Kristian Arntzen
4aead55ca6 Remove dead comment. 2018-09-17 13:58:48 +02:00
Hans-Kristian Arntzen
49ac538a64 Remove maybe_assign_input_struct.
This is obsolete and wrong since we already unflatten I/O structs.
2018-09-17 13:51:02 +02:00
Chip Davis
39bc101e82 MSL: Handle the SamplePosition builtin.
This is somewhat tricky, because in MSL this value is obtained through a
function, `get_sample_position()`. Since the call expression is an
rvalue, it can't be passed by reference, so functions get a copy
instead.

This was the last piece preventing us from turning on sample-rate
shading support in MoltenVK.
2018-09-13 09:34:28 -05:00
Hans-Kristian Arntzen
1bbb4032c8
Merge pull request #693 from cdavis5e/msl-atomic-inc-dec
MSL: Fix OpAtomicIIncrement and OpAtomicIDecrement.
2018-09-13 16:19:27 +02:00
Hans-Kristian Arntzen
d310060f92 MSL: Support global I/O block and struct Input/Output usage.
Implement this by flattening outputs and unflattening inputs explicitly.
This allows us to pass down a single struct instead of dealing with the
insanity that would be passing down each flattened member separately.

Remove stage_uniforms_var_id.
Seems to be dead code. Naked uniforms do not exist in SPIR-V for Vulkan,
which this seems to have been intended for. It was also unused elsewhere.
2018-09-13 16:04:24 +02:00
Chip Davis
06edf804ac Clarify name of this parameter. 2018-09-13 08:56:23 -05:00
Hans-Kristian Arntzen
71bb7785ac MSL: textureQueryLod() is not supported.
Don't bother with hacky workaround unless required.
2018-09-13 13:44:46 +02:00
Hans-Kristian Arntzen
89e3b8ff0d Run format_all.sh. 2018-09-12 10:53:50 +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
Chip Davis
41eb5c43b5 MSL: Fix OpAtomicIIncrement and OpAtomicIDecrement.
We were passing a constant '1' to `emit_atomic_func_op()`--which caused
us to refer to SPIR-V value `%1`, which is almost certainly not what we
want! What we really want is to add/subtract the literal constant '1'
to/from the memory location.
2018-09-11 17:29:54 -05:00
Hans-Kristian Arntzen
403011e973
Merge pull request #684 from cdavis5e/msl-builtin-vector-cast
MSL: Cast uses of builtin vectors to their declared SPIR-V type.
2018-09-11 19:59:58 +02:00
Chip Davis
6757ef8512 Use bitcast_to_builtin_load() instead of hacking to_expression().
This only affects the builtin when it is used, and not when it's passed
to a function. It's a lot cleaner than the way I was doing it before.

Remove the `to_expression()` hack.
2018-09-11 11:15:17 -05:00
Chip Davis
acb3fac747 Opt for a simple value cast in lieu of a bitcast. 2018-09-10 14:05:36 -05:00
Hans-Kristian Arntzen
b114889102 Only declare typed initializer list for non-array types.
Also, cleanup now redundant constant_expression virtualization for MSL.
2018-09-10 10:04:17 +02:00
Chip Davis
f7dad9da66 MSL: Cast uses of builtin vectors to their declared SPIR-V type.
In SPIR-V, builtin integral vectors can be either signed or unsigned,
but in MSL they're always unsigned. Unfortunately, the MSL spec forbids
implicit conversions between vector types--even if the corresponding
scalar types would implicitly convert. If you try, the result is a
cryptic error message such as:

```
program_source:37:60: error: cannot convert between vector values of different size ('int4' (aka 'vector_int4') and 'vector_uint4' (vector of 4 'unsigned int' values))
            float4 r3 = as_type<float4>((as_type<int4>(r0) * gl_LocalInvocationID.xyyy) + as_type<int4>(r2));
                                         ~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~
```

Therefore, uses of these builtins must be explicitly cast, since the
rest of the binary likely assumes that the builtin is of its declared
type.
2018-09-08 21:17:54 -05:00
Hans-Kristian Arntzen
9ffd4172b4
Merge pull request #680 from cdavis5e/msl-varying-components
MSL: Account for components when assigning locations to varyings.
2018-09-07 16:01:53 +02:00
Hans-Kristian Arntzen
32823b0838 MSL: Do not emit function constants for version < 1.2. 2018-09-07 09:33:34 +02:00
Chip Davis
4b99fdd5d0 MSL: Account for components when assigning locations to varyings.
Two varyings (vertex outputs/fragment inputs) might have the same
location but be in different components--e.g. the compiler may have
packed what were two different varyings into a single varying vector.
Giving both varyings the same `[[user]]` attribute won't work--it may
yield unexpected results, or flat out fail to link. We could eventually
pack such varyings into a single vector, but that would require us to
handle the case where the varyings are different types--e.g. a `float`
and a `uint` packed into the same vector. For now, it seems most
prudent to give them unique `[[user]]` locations and let Apple's
compiler work out the best way to pack them.
2018-09-06 13:52:33 -05:00
Chip Davis
674f97a40e Handle interpolation qualifiers on the entire struct, too. 2018-09-06 12:29:42 -05:00
Chip Davis
9e6469bd40 MSL: Handle interpolation qualifiers. 2018-09-05 12:02:07 -05:00
Chip Davis
680ef9d773 MSL: Correct number of words to skip in OpImageWrite.
The length field in `Instruction` doesn't include the initial
opcode/length word. We only need to skip three words instead of four.
2018-09-05 10:02:25 -05:00
Chip Davis
9fbe39c9c0 MSL: Emit spvTexelBufferCoord() on ImageWrite to a Buffer as well.
This is necessary to get the coordinates to give to the texture's
`write()` method.
2018-09-04 12:14:34 -05:00
Hans-Kristian Arntzen
917ca818ed
Merge pull request #673 from cdavis5e/min-max-clamp
MSL: Emit F{Min,Max,Clamp} as fast:: and N{Min,Max,Clamp} as precise::.
2018-09-04 15:15:18 +02:00
Hans-Kristian Arntzen
0c1d4d8b6a MSL: Support texture2d_ms_array. 2018-09-03 11:02:31 +02:00
Hans-Kristian Arntzen
778f998cd2 MSL: Throw error on multisampled array textures. 2018-09-03 10:21:59 +02:00
Chip Davis
27af716c3a MSL: Emit F{Min,Max,Clamp} as fast:: and N{Min,Max,Clamp} as precise::.
This roughly matches their semantics in SPIR-V and MSL. For `FMin`,
`FMax`, and `FClamp`, and the Metal functions `fast::min()`,
`fast::max()`, and `fast::clamp()`, the result is undefined if any
operand is NaN. For the 'N' operations and their corresponding MSL
`precise::` functions, the result is consistent with IEEE 754 (first
non-NaN wins; result is NaN if all operands are NaN).

We can only do this with 32-bit floats, though, because Metal only
provides these variants for `float`. `half` only has one variant of
these functions that is presumably consistent with IEEE 754. I guess
that's OK; the SPIR-V spec only says that `F{Min,Max,Clamp}` are
undefined for NaNs. Performance might suffer, though.
2018-09-01 23:01:46 -05:00
Chip Davis
d3233690cb MSL: Support unordered relational operators.
The SPIR-V spec says that these check if the operands either are
unordered or satisfy the given condition. So that's just what we'll do,
using Metal's `isunordered()` stdlib function. Apple's optimizers ought
to be able to collapse that to a single unordered compare.
2018-08-31 13:54:42 -05:00
Chip Davis
2ee8ebbc62 Throw an exception anytime we try to compile DrawIndex to MSL. 2018-08-29 12:05:33 -05:00
Chip Davis
97d01b6450 Punt on DrawIndex in MSL for now.
Metal doesn't properly support this.
2018-08-29 10:21:42 -05:00
Chip Davis
fcad019e11 Support the shader_draw_parameters extension. 2018-08-29 10:07:21 -05:00
Chip Davis
1fd8cd9468 [MSL] Give the FragDepth builtin a type of float. 2018-08-28 13:47:50 -05:00
Hans-Kristian Arntzen
87de951105 MSL: Fix naming issue of aliased global variables.
When the name of an alias global variable collides with a global
declaration, MSL would emit inconsistent names, sometimes with the
naming fix, sometimes without, because names were being tracked in two
separate meta blocks. Fix this by always redirecting parameter naming to
the original base variable as necessary.
2018-08-27 09:59:55 +02:00