diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2022-02-09 11:26:04 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-09 11:26:04 -0500 |
| commit | 160111a0e27c9325ddfc49a53fbb82d3c6f06c90 (patch) | |
| tree | f71557856792e01bd4f55ad3013b16946bc866b6 | |
| parent | d06a78d935b2743494d47ed5cd3f36e38ac9c5ac (diff) | |
Generic experiments (#2119)
* #include an absolute path didn't work - because paths were taken to always be relative.
* Small fixes.
Added compiler crash with generic defined in a function.
Added enum-flags test that works (by limiting backing type to int), and using __EnumType constraint.
* Add comment about crash.
* Disable crashing test.
| -rw-r--r-- | tests/experiments/generic/enum-int-flags-2.slang | 45 | ||||
| -rw-r--r-- | tests/experiments/generic/equality-3.slang | 4 | ||||
| -rw-r--r-- | tests/experiments/generic/equality-5.slang | 4 | ||||
| -rw-r--r-- | tests/experiments/generic/generic-in-function.slang | 30 |
4 files changed, 79 insertions, 4 deletions
diff --git a/tests/experiments/generic/enum-int-flags-2.slang b/tests/experiments/generic/enum-int-flags-2.slang new file mode 100644 index 000000000..c5283fb7e --- /dev/null +++ b/tests/experiments/generic/enum-int-flags-2.slang @@ -0,0 +1,45 @@ +//DISABLE_TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj + +/* A test to use generics to treat an enum as a set. +Here we simplify previous example, by not allowing the backing type be +a specialization parameter (and just use int). + +Moreover here E is constrained to __EnumType. + +Works +*/ + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name outputBuffer +RWStructuredBuffer<float> outputBuffer; + +enum Enum +{ + A = 0x1, + B = 0x2, + C = 0x4, +}; + +__generic<E : __EnumType> +struct Flags +{ + [mutating] void set(E e) { value |= (int)e; } + bool isSet(E e) { return (((int)e) & value) != 0; } + int value = 0; +}; + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + int index = dispatchThreadID.x; + + Flags<Enum> flags; + + if (index & 1) + { + flags.set(Enum::A); + } + bool isASet = flags.isSet(Enum::A); + + outputBuffer[index] = isASet ? 2 : 1; +} + diff --git a/tests/experiments/generic/equality-3.slang b/tests/experiments/generic/equality-3.slang index e5e42aedf..033b90415 100644 --- a/tests/experiments/generic/equality-3.slang +++ b/tests/experiments/generic/equality-3.slang @@ -2,7 +2,9 @@ /* A test for equality around interface types -This style is discussed in the documentation here: +This style is discussed in the documentation here (on This type): + +https://github.com/shader-slang/slang/blob/master/docs/user-guide/04-interfaces-generics.md Still has limitiation that it only works for an an implementation of an interface. */ diff --git a/tests/experiments/generic/equality-5.slang b/tests/experiments/generic/equality-5.slang index 41cadbbfc..0f2932d41 100644 --- a/tests/experiments/generic/equality-5.slang +++ b/tests/experiments/generic/equality-5.slang @@ -2,11 +2,9 @@ /* A test for equality around interface types -This is an attempt to get the *outside* impl of equality to work. The simple case does, but just to throw -a spanner in the works, lets mix in some inheritance. +This is an attempt to get the *outside* impl of equality to work. The simple case does, but just to throw a spanner in the works, lets mix in some inheritance. An issue here (perhaps) is that this will compile - the isEqual implementation will just slice and probably not do what the implementer expected. - */ //TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name outputBuffer diff --git a/tests/experiments/generic/generic-in-function.slang b/tests/experiments/generic/generic-in-function.slang new file mode 100644 index 000000000..7a45d43de --- /dev/null +++ b/tests/experiments/generic/generic-in-function.slang @@ -0,0 +1,30 @@ +//DISABLE_TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj + +/* It appears the use of a generic defined within a function appears to trigger a crash + + +``` +void Type::accept(ITypeVisitor* visitor, void* extra) +``` + +As this is nullptr. +*/ + +RWStructuredBuffer<float> outputBuffer; + +int doThing() +{ + int getValue<let N : int>() { return N; } + + return getValue<10>(); +} + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + uint tid = dispatchThreadID.x; + + float inVal = float(tid); + + outputBuffer[tid] = doThing(); +} |
