Commit Graph

101 Commits

Author SHA1 Message Date
steve-lunarg
a2e7531057 HLSL: inter-stage structure splitting.
This adds structure splitting, which among other things will enable GS support where input structs
are passed, and thus become input arrays of structs in the GS inputs.  That is a common GS case.

The salient points of this PR are:

* Structure splitting has been changed from "always between stages" to "only into the VS and out of
  the PS".  It had previously happened between stages because it's not legal to pass a struct
  containing a builtin IO variable.

* Structs passed between stages are now split into a struct containing ONLY user types, and a
  collection of loose builtin IO variables, if any.  The user-part is passed as a normal struct
  between stages, which is valid SPIR-V now that the builtin IO is removed.

* Internal to the shader, a sanitized struct (with IO qualifiers removed) is used, so that e.g,
  functions can work unmodified.

* If a builtin IO such as Position occurs in an arrayed struct, for example as an input to a GS,
  the array reference is moved to the split-off loose variable, which is given the array dimension
  itself.

When passing things around inside the shader, such as over a function call, the the original type
is used in a sanitized form that removes the builtIn qualifications and makes them temporaries.
This means internal function calls do not have to change.  However, the type when returned from
the shader will be member-wise copied from the internal sanitized one to the external type.
The sanitized type is used in variable declarations.

When copying split types and unsplit, if a sub-struct contains only user variables, it is copied
as a single entity to avoid more AST verbosity.

Above strategy arrived at with talks with @johnkslang.

This is a big complex change.  I'm inclined to leave it as a WIP until it can get some exposure to
real world cases.
2016-12-26 10:11:15 -07:00
John Kessenich
e795cc915c Merge pull request #621 from steve-lunarg/recursive-flattening
HLSL: Recursive composite flattening
2016-12-08 11:18:07 -07:00
steve-lunarg
a2b01a0da8 HLSL: Recursive composite flattening
This PR implements recursive type flattening.  For example, an array of structs of other structs
can be flattened to individual member variables at the shader interface.

This is sufficient for many purposes, e.g, uniforms containing opaque types, but is not sufficient
for geometry shader arrayed inputs.  That will be handled separately with structure splitting,
 which is not implemented by this PR.  In the meantime, that case is detected and triggers an error.

The recursive flattening extends the following three aspects of single-level flattening:

- Flattening of structures to individual members with names such as "foo[0].samp[1]";

- Turning constant references to the nested composite type into a reference to a particular
  flattened member.

- Shadow copies between arrays of flattened members and the nested composite type.

Previous single-level flattening only flattened at the shader interface, and that is unchanged by this PR.
Internally, shadow copies are, such as if the type is passed to a function.

Also, the reasons for flattening are unchanged.  Uniforms containing opaque types, and interface struct
types are flattened.  (The latter will change with structure splitting).

One existing test changes: hlsl.structin.vert, which did in fact contain a nested composite type to be
flattened.

Two new tests are added: hlsl.structarray.flatten.frag, and hlsl.structarray.flatten.geom (currently
issues an error until type splitting is online).

The process of arriving at the individual member from chained postfix expressions is more complex than
it was with one level.  See large-ish comment above HlslParseContext::flatten() for details.
2016-12-07 14:40:01 -07:00
steve-lunarg
05f75142d6 HLSL: opcode specific promotion rules for interlocked ops
PR #577 addresses most but not all of the intrinsic promotion problems.
This PR resolves all known cases in the remainder.

Interlocked ops need special promotion rules because at the time
of function selection, the first argument has not been converted
to a buffer object.  It's just an int or uint, but you don't want
to convert THAT argument, because that implies converting the
buffer object itself.  Rather, you can convert other arguments,
but want to stay in the same "family" of functions.  E.g, if
the first interlocked arg is a uint, use only the uint family,
never the int family, you can convert the other args as you please.

This PR allows making such opcode and arg specific choices by
passing the op and arg to the convertible lambda.  The code in
the new test "hlsl.promote.atomic.frag" would not compile without
this change, but it must compile.

Also, it provides better handling of downconversions (to "worse"
types), which are permitted in HLSL.  The existing method of
selecting upconversions is unchanged, but if that doesn't find
any valid ones, then it will allow downconversions.  In effect
this always uses an upconversion if there is one.
2016-12-07 12:00:32 -07:00
John Kessenich
21b11f4cc1 Merge branch 'intrinsic-promotion' of https://github.com/steve-lunarg/glslang into steve-lunarg-intrinsic-promotion 2016-12-03 13:27:22 -07:00
John Kessenich
98ad485321 HLSL: Support {...} initializer lists that are too short. 2016-11-27 17:39:07 -07:00
steve-lunarg
ef33ec0925 HLSL: add intrinsic function implicit promotions
This PR handles implicit promotions for intrinsics when there is no exact match,
such as for example clamp(int, bool, float).  In this case the int and bool will
be promoted to a float, and the clamp(float, float, float) form used.

These promotions can be mixed with shape conversions, e.g, clamp(int, bool2, float2).

Output conversions are handled either via the existing addOutputArgumentConversion
function, which this PR generalizes to handle either aggregates or unaries, or by
intrinsic decomposition.  If there are methods or intrinsics to be decomposed,
then decomposition is responsible for any output conversions, which turns out to
happen automatically in all current cases.  This can be revisited once inout
conversions are in place.

Some cases of actual ambiguity were fixed in several tests, e.g, spv.register.autoassign.*

Some intrinsics with only uint versions were expanded to signed ints natively, where the
underlying AST and SPIR-V supports that.  E.g, countbits.  This avoids extraneous
conversion nodes.

A new function promoteAggregate is added, and used by findFunction.  This is essentially
a generalization of the "promote 1st or 2nd arg" algorithm in promoteBinary.

The actual selection proceeds in three steps, as described in the comments in
hlslParseContext::findFunction:

1. Attempt an exact match.  If found, use it.
2. If not, obtain the operator from step 1, and promote arguments.
3. Re-select the intrinsic overload from the results of step 2.
2016-11-23 10:36:34 -07:00
John Kessenich
e122f053bb Merge pull request #599 from steve-lunarg/gs
HLSL: Add GS support
2016-11-23 00:29:30 -07:00
John Kessenich
6e848daf45 Merge pull request #596 from steve-lunarg/hlsl-intrinsic-parsing
HLSL: use HLSL parser for HLSL intrinsic prototypes, enable int/bool mats
2016-11-23 00:19:40 -07:00
steve-lunarg
f49cdf4183 WIP: HLSL: Add GS support
This PR adds:

[maxvertexcount(n)] attributes

point/line/triangle/lineadj/triangleadj qualifiers

PointStream/LineStream/TriangleStream templatized types

Append method on above template types

RestartStrip method on above template types.
2016-11-21 18:25:08 -07:00
steve-lunarg
75fd223f03 HLSL: allow "sample" as a valid identifier.
HLSL has keywords for various interpolation modifiers such as "linear",
"centroid", "sample", etc.  Of these, "sample" appears to be special,
as it is also accepted as an identifier string, where the others are not.

This PR adds this ability, so the construct "int sample = 42;" no longer
produces a compilation error.

New test = hlsl.identifier.sample.frag
2016-11-16 13:22:11 -07:00
steve-lunarg
0842dbb39a HLSL: use HLSL parser to parse HLSL intrinsic prototypes, enable int/bool mats
This PR adds a CreateParseContext() fn analogous to CreateBuiltInParseables(),
to create a language specific built in parser.  (This code was present before
but not encapsualted in a fn).  This can now be used to create a source language
specific parser for builtins.

Along with this, the code creating HLSL intrinsic prototypes can now produce
them in HLSL syntax, rather than GLSL syntax.  This relaxes certain prior
restrictions at the parser level.  Lower layers (e.g, SPIR-V) may still have
such restrictions, such as around Nx1 matrices: this code does not impact
that.

This PR also fleshes out matrix types for bools and ints, both of which were
partially in place before.  This was easier than maintaining the restrictions
in the HLSL prototype generator to avoid creating protoypes with those types.

Many tests change because the result type from intrinsics moves from "global"
to "temp".

Several new tests are added for the new types.
2016-11-16 11:19:22 -07:00
John Kessenich
0bf06d3cf5 Merge pull request #576 from steve-lunarg/uav-registers
Add UAV (image) binding offset and HLSL register class support
2016-11-14 09:39:46 -07:00
steve-lunarg
d9cb832f9c HLSL: allow promotion from 1-vector types to scalars, e.g, float<-float1
Previously, an error was thrown when assigning a float1 to a scalar float,
or similar for other basic types.  This allows that.

Also, this allows calling functions accepting scalars with float1 params,
so for example sin(float1) will work.  This is a minor change in
HlslParseContext::findFunction().
2016-11-13 14:44:46 -07:00
steve-lunarg
a22f7dbb71 HLSL: Allow expressions in attributes
For example:

[numthreads(2+2, 2*3, (1+FOO)*BAR)]

This will result in a thread count (4, 6, 8).
2016-11-11 08:23:03 -07:00
steve-lunarg
9088be4c07 Add UAV (image) binding offset and HLSL register support
This PR adds:

1. The "u" register class for RW* objects.

2. --shift-image-bindings (== --sib), analogous to --shift-texture-bindings etc.

3. Case insensitive reg classes.

4. Tests for above.
2016-11-01 14:44:54 -06:00
John Kessenich
89df3c2dcb Merge pull request #572 from steve-lunarg/numthreads
HLSL: implement numthreads for compute shaders
2016-11-01 00:25:06 -06:00
steve-lunarg
3226b0835c HLSL: Add min*{float,int,uint} types
These HLSL types are guaranteed to have at least the given number of bits, but may have more.

min{16,10}float is mapped to EbtFloat at medium precision -> SPIRV RelaxedPrecision
min{16,12}int and min16uint are mapped to mediump -> SPIR-V RelaxedPrecision
2016-10-31 12:46:05 -06:00
steve-lunarg
1868b14435 HLSL: implement numthreads for compute shaders
This PR adds handling of the numthreads attribute for compute shaders, as well as a general
infrastructure for returning attribute values from acceptAttributes, which may be needed in other
cases, e.g, unroll(x), or merely to know if some attribute without params was given.

A map of enum values from TAttributeType to TIntermAggregate nodes is built and returned.  It
can be queried with operator[] on the map.  In the future there may be a need to also handle
strings (e.g, for patchconstantfunc), and those can be easily added into the class if needed.

New test is in hlsl.numthreads.comp.
2016-10-31 09:28:17 -06:00
John Kessenich
51634468da Merge pull request #568 from steve-lunarg/logicalop-fix
HLSL: allow component-wise operations for logical || and &&.
2016-10-26 23:01:16 -06:00
steve-lunarg
27939caa86 HLSL: allow component-wise operations for logical || and &&.
HLSL || and && can operate component-wise.
2016-10-26 12:54:56 -06:00
steve-lunarg
85244d7486 HLSL: Enable component-wise vector comparisons from operators
This PR only changes a few lines of code, but is subtle.

In HLSL, comparison operators (<,>,<=,>=,==,!=) operate component-wise
when given a vector operand.  If a whole vector equality or inequality is
desired, then all() or any() can be used on the resulting bool vector.

This PR enables this change.  Existing shape conversion is used when
one of the two arguments is a vector and one is a scalar.

Some existing HLSL tests had assumed == and != meant vector-wise
instead of component-wise comparisons.  These tests have been changed
to add an explicit any() or all() to the test source.  This verifably
does not change the final SPIR-V binary relative to the old behavior
for == and !=.  The AST does change for the (now explicit, formerly
implicit) any() and all().  Also, a few tests changes where they
previously had the return type wrong, e.g, from a vec < vec comparison
in hlsl.shapeConv.frag.

Promotion of comparison opcodes to vector forms
(EOpEqual->EOpVectorEqual) is handled in promoteBinary(), as is setting
the proper vector type of the result.

EOpVectorEqual and EOpVectorNotEqual are now accepted as either
aggregate or binary nodes, similar to how the other operators are
handled.  Partial support already existed for this: it has been
fleshed out in the printing functions in intermOut.cpp.

There is an existing defect around shape conversion with 1-vectors, but
that is orthogonal to this PR and not addressed by it.
2016-10-26 08:50:10 -06:00
John Kessenich
04e2dc164e Merge pull request #558 from steve-lunarg/image-atomics
HLSL: phase 4 of RWTexture support: add image atomics
2016-10-20 19:09:55 -06:00
steve-lunarg
22322361d6 HLSL: phase 4 of rwtexture support: add image atomics
This PR will turn Interlocked* intrinsics using rwtexture or rwbuffer
object as the first parameter into the proper OpImageAtomic* operations.
2016-10-19 10:25:23 -06:00
steve-lunarg
e5921f1309 HLSL: Fix unary and binary operator type conversion issues
This fixes defects as follows:

1. handleLvalue could be called on a non-L-value, and it shouldn't be.

2. HLSL allows unary negation on non-bool values.  TUnaryOperator::promote
   can now promote other types (e.g, int, float) to bool for this op.

3. HLSL allows binary logical operations (&&, ||) on arbitrary types, similar
   (2).

4. HLSL allows mod operation on arbitrary types, which will be promoted.
   E.g, int % float -> float % float.
2016-10-18 16:56:37 -06:00
John Kessenich
b50fd17acb HLSL: Support SV_Coverage and SV_DispatchThreadId; catch SV_GroupIndex. 2016-10-16 12:12:11 -06:00
John Kessenich
bf8a6ef750 Merge pull request #551 from steve-lunarg/rwbuffers-fmt
HLSL: phase 3 of rwtexture support: add sub-vec4 capabilities
2016-10-15 23:03:38 -06:00
John Kessenich
062b239d10 Merge pull request #549 from steve-lunarg/multidim-array
HLSL: allow multi-dimensional arrays
2016-10-15 22:43:43 -06:00
steve-lunarg
8b0227ced9 HLSL: phase 3b: Texture methods remember and return vector size.
Also makes a (correct) test change for global -> temp vars.
2016-10-14 18:44:32 -06:00
steve-lunarg
4f2da27aec HLSL: phase 3a: Add sub-vec4 rwtexture formats (qualifier.layoutFormat)
This PR sets the TQualifier layoutFormat according to the HLSL image type.
For instance:

  RWTexture1D <float2> g_tTex1df2;

becomes ElfRg32f.  Similar on Buffers, e.g, Buffer<float4> mybuffer;

The return type for image and buffer loads is now taken from the storage format.
Also, the qualifier for the return type is now (properly) a temp, not a global.
2016-10-14 18:44:32 -06:00
steve-lunarg
7b211a370b HLSL: allow multi-dimensional arrays
All the underpinnings are there; this just parses multiple array dimensions
and passes them through to the existing mechanisms.

Also, minor comment fixes, and add a new test for multi-dim arrays.
2016-10-13 12:32:04 -06:00
steve-lunarg
07830e805b HLSL: phase 2d: minor cleanup, & allow operator[] on non-rw textures
Improve comments.
A few tweaked lines allow [] on non-rw tx.  Add test case for this.
Improve VectorTimesScalar handling.
2016-10-12 12:39:44 -06:00
steve-lunarg
6b43d274e7 HLSL: phase 2a: add r-value operator[] for RWTexture/RWBuffer
This commit adds r-value support for RW textures and buffers.
Supported is:

- Function in parameter conversions
- conversion of rvalue use to imageLoad
2016-10-12 12:39:44 -06:00
John Kessenich
087a454af2 HLSL: Add shape conversions for return values. 2016-10-06 16:56:54 -06:00
John Kessenich
ed33e05762 HLSL: Do structure conversion for return type struct-punning on non-entry-point functions. 2016-10-06 13:06:13 -06:00
steve-lunarg
bb0183f817 HLSL: phase 1: add RWTexture and RWBuffer
There's a lot to do for RWTexture and RWBuffer, so it will be broken up into
several PRs.  This is #1.

This adds RWTexture and RWBuffer support, with the following limitations:
  * Only 4 component formats supported
  * No operator[] yet

Those will be added in other PRs.

This PR supports declarations and the Load & GetDimensions methods.  New tests are
added.
2016-10-06 10:51:52 -06:00
David Neto
1d3a966106 Gtests can be run on another source tree
The gtest executable accepts a --test-root option to specify
a root directory for test files.  It defaults to the Test directory
in the source tree from which the executable is built.

For example, this lets us run test exectuables built with MinGW on Linux
on a Windows machine with its own copy of the source tree.
2016-10-05 10:28:32 -04:00
steve-lunarg
2199c2404b HLSL: fix for flattening assignments from non-symbol R-values.
If a member-wise assignment from a non-flattened struct to a flattened struct sees a complex R-value
(not a symbol), it now creates a temporary to hold that value, to avoid repeating the R-value.
This avoids, e.g, duplicating a whole function call.  Also, it avoids re-using the AST node, making a
new one for each member inside the member loop.

The latter (re-use of AST node) was also an issue in the GetDimensions intrinsic decomposition,
so this PR fixes that one too.
2016-10-04 17:07:45 -06:00
John Kessenich
df98cc26d5 Add amend ability for anonymous blocks, so they can grow between function bodies. 2016-09-29 23:59:26 -06:00
steve-lunarg
265c0618b1 HLSL: allow implicit array sizing.
In HLSL array sizes need not be provided explicitly in all circumstances.
For example, this is valid (note no number between the [ ]):

  // no explicit array size
  uniform float g_array[] = { 1, 2, 3, 4, 5 };

This PR does not attempt to validate most invalid cases.

A new test is added to verify the resulting linker objects.
2016-09-27 14:28:26 -06:00
steve-lunarg
e0b9debda2 Flatten uniform arrays
This checkin adds a --flatten-uniform-arrays option which can break
uniform arrays of samplers, textures, or UBOs up into individual
scalars named (e.g) myarray[0], myarray[1], etc.  These appear as
individual linkage objects.

Code notes:

- shouldFlatten internally calls shouldFlattenIO, and shouldFlattenUniform,
  but is the only flattening query directly called.

- flattenVariable will handle structs or arrays (but not yet arrayed structs;
  this is tested an an error is generated).

- There's some error checking around unhandled situations.  E.g, flattening
  uniform arrays with initializer lists is not implemented.

- This piggybacks on as much of the existing mechanism for struct flattening
  as it can.  E.g, it uses the same flattenMap, and the same
  flattenAccess() method.

- handleAssign() has been generalized to cope with either structs or arrays.

- Extended test infrastructure to test flattening ability.
2016-09-22 08:47:48 -06:00
John Kessenich
86f7138706 HLSL: Add string basic type and recognize string declaration grammar.
This includes the "< decl ; decl ; >" syntax which has its own namespace.
This functionality is not implemented, just silently accepted.
2016-09-19 20:29:45 -06:00
John Kessenich
d2ce838a58 HLSL: Handle flatten for reads from flatten structs and parameter passing. 2016-09-16 20:24:14 -06:00
John Kessenich
cd0a78a0d9 HLSL: Flatten vertex input and fragment output structures.
Vulkan can't handle structures into the vertex stage or out
of the fragment stage.
2016-09-10 11:09:24 -06:00
steve-lunarg
297ae211f1 WIP: HLSL: Treat HLSL rows as GLSL columns.
WIP: HLSL: EOpGenMul arg reversal
2016-09-09 12:02:42 -06:00
John Kessenich
07350f3382 HLSL: Handle "fake" entry points, by undoing their built-in variable declarations. 2016-09-02 20:24:07 -06:00
John Kessenich
9e079535a0 HLSL: Handle greater/less depth modes. Fixes issue #489. 2016-09-02 20:05:52 -06:00
John Kessenich
6a70eb7161 HLSL: Emulate write-to-output on return-from-entry-point, for return value.
This fixes issue #487 and #480.
It also correctly handles output parameters from the entry point.
2016-08-28 20:13:07 -06:00
steve-lunarg
36e87d0871 HLSL: add precise keyword 2016-08-25 08:48:54 -06:00
John Kessenich
fcc0aa3b64 HLSL: Switch to generic selector, but using GLSL #version 400 rules.
Next step is to modify for HLSL rules.
2016-08-24 18:34:43 -06:00