summaryrefslogtreecommitdiffstats
path: root/tests/experiments/interface
diff options
context:
space:
mode:
Diffstat (limited to 'tests/experiments/interface')
-rw-r--r--tests/experiments/interface/README.md5
-rw-r--r--tests/experiments/interface/associated-type-2.slang61
-rw-r--r--tests/experiments/interface/associated-type.slang59
-rw-r--r--tests/experiments/interface/resource-associated-type.slang63
-rw-r--r--tests/experiments/interface/return-interface-2.slang55
-rw-r--r--tests/experiments/interface/return-interface.slang50
6 files changed, 293 insertions, 0 deletions
diff --git a/tests/experiments/interface/README.md b/tests/experiments/interface/README.md
new file mode 100644
index 000000000..655c5e508
--- /dev/null
+++ b/tests/experiments/interface/README.md
@@ -0,0 +1,5 @@
+Interface Experiments
+=====================
+
+All tests are disabled.
+
diff --git a/tests/experiments/interface/associated-type-2.slang b/tests/experiments/interface/associated-type-2.slang
new file mode 100644
index 000000000..2dd3779d9
--- /dev/null
+++ b/tests/experiments/interface/associated-type-2.slang
@@ -0,0 +1,61 @@
+//DISABLE_TEST:SIMPLE:-target dxil -entry computeMain -profile cs_6_2
+
+/*
+Testing how an associated type can be used.
+
+Here we try and only use Type returned in IInterface with methods on IInterface, since presubably the
+impl knows how to deal with it.
+
+Doesn't work.
+
+.slang(52): error 30019: expected an expression of type 'Type', got 'Type'
+ outputBuffer[dispatchThreadID.x] = x + intf.getValue(r, 16);
+
+*/
+
+struct SomeType
+{
+ int getValue(int index) { return index + offset; }
+
+ int offset;
+};
+
+interface IInterface
+{
+ associatedtype Type;
+
+ Type get(int offset);
+ int getValue(Type type, int index);
+};
+
+struct InterfaceImpl : IInterface
+{
+ typedef SomeType Type;
+
+ Type get(int offset)
+ {
+ Type v;
+ v.offset = offset;
+ return v;
+ };
+ int getValue(Type type, int index)
+ {
+ return type.getValue(index);
+ }
+};
+
+RWStructuredBuffer<int> outputBuffer;
+
+[numthreads(4, 4, 1)]
+void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
+{
+ int x = dispatchThreadID.x;
+
+ InterfaceImpl impl;
+
+ IInterface intf = impl;
+
+ var r = intf.get(16);
+
+ outputBuffer[dispatchThreadID.x] = x + intf.getValue(r, 16);
+} \ No newline at end of file
diff --git a/tests/experiments/interface/associated-type.slang b/tests/experiments/interface/associated-type.slang
new file mode 100644
index 000000000..2cc4f7c68
--- /dev/null
+++ b/tests/experiments/interface/associated-type.slang
@@ -0,0 +1,59 @@
+//TEST_TEST:SIMPLE:-target dxil -entry computeMain -profile cs_6_2
+
+/*
+Testing how an associated type can be used.
+
+In this example because we get the Type from the interface, it doesn't know what actual type it is.
+
+.slang(43): error 30027: 'getValue' is not a member of 'Type'.
+ outputBuffer[dispatchThreadID.x] = x + r.getValue(x);
+
+From an error message point of view this perhaps somewhat confusing because there aren't any methods on Type in this context.
+
+It would be possible for an implementation to determine intf is impl, and do the right thing, although not in general.
+*/
+
+struct SomeType
+{
+ int getValue(int index) { return index + offset; }
+
+ int offset;
+};
+
+interface IInterface
+{
+ associatedtype Type;
+
+ Type get(int offset);
+};
+
+struct InterfaceImpl : IInterface
+{
+ typedef SomeType Type;
+
+ Type get(int offset)
+ {
+ Type v;
+ v.offset = offset;
+ return v;
+ };
+};
+
+RWStructuredBuffer<int> outputBuffer;
+
+[numthreads(4, 4, 1)]
+void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
+{
+ int x = dispatchThreadID.x;
+
+ InterfaceImpl impl;
+
+ IInterface intf = impl;
+
+ // Works if we get from impl. Doesn't work if we get from the interface
+ // saying that 'Type' doesn't define getValue.
+
+ var r = intf.get(16);
+
+ outputBuffer[dispatchThreadID.x] = x + r.getValue(x);
+} \ No newline at end of file
diff --git a/tests/experiments/interface/resource-associated-type.slang b/tests/experiments/interface/resource-associated-type.slang
new file mode 100644
index 000000000..e5d96a309
--- /dev/null
+++ b/tests/experiments/interface/resource-associated-type.slang
@@ -0,0 +1,63 @@
+//DISABLE_TEST:SIMPLE:-target dxil -entry computeMain -profile cs_6_2
+
+/*
+
+Doesn't work:
+
+.slang(51): error 30027: 'getValue' is not a member of 'Type'.
+ outputBuffer[dispatchThreadID.x] = x + r.getValue(x);
+ ^
+ */
+
+interface ISomeInterface
+{
+ int getValue(int index);
+};
+
+struct SomeType : ISomeInterface
+{
+ int getValue(int index) { return data.Load(index + offset); }
+
+ int offset;
+ ByteAddressBuffer data;
+};
+
+interface IInterface
+{
+ associatedtype Type;
+
+ Type get(int offset);
+};
+
+struct InterfaceImpl : IInterface
+{
+ typedef SomeType Type;
+
+ Type get(int offset)
+ {
+ Type a;
+ a.offset = offset;
+ a.data = byteAddressBuffer;
+ return a;
+ };
+};
+
+ByteAddressBuffer byteAddressBuffer;
+RWStructuredBuffer<int> outputBuffer;
+
+[numthreads(4, 4, 1)]
+void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
+{
+ int x = dispatchThreadID.x;
+
+ InterfaceImpl impl;
+
+ IInterface intf = impl;
+
+ // Works if we get from impl. Doesn't work if we get from the interface
+ // saying that 'Type' doesn't define getValue.
+
+ var r = intf.get(16);
+
+ outputBuffer[dispatchThreadID.x] = x + r.getValue(x);
+} \ No newline at end of file
diff --git a/tests/experiments/interface/return-interface-2.slang b/tests/experiments/interface/return-interface-2.slang
new file mode 100644
index 000000000..1aa6fc5e3
--- /dev/null
+++ b/tests/experiments/interface/return-interface-2.slang
@@ -0,0 +1,55 @@
+//TEST_TEST:SIMPLE:-target dxil -entry computeMain -profile cs_6_2
+
+/*
+Testing returning an interface type.
+
+Fails with:
+
+.slang(6): error 41011: type 'SomeType' does not fit in the size required by its conforming interface.
+struct SomeType : IGetValue
+^~~~~~
+
+That's understandable in so far as dynamic dispatch appears to assume anyValue(16) by default. On the other hand, the compiler will know all conforming types (well at linkage anyway), and so potentially could work out an appropriate size.
+*/
+
+interface IGetValue
+{
+ int getValue(int index);
+};
+
+struct SomeType : IGetValue
+{
+ __init(int inOffset) { offset = inOffset; another = float4(inOffset); }
+ int getValue(int index) { return index + offset; }
+ int offset;
+ float4 another;
+};
+
+interface IInterface
+{
+ IGetValue get(int offset);
+};
+
+struct InterfaceImpl : IInterface
+{
+ IGetValue get(int offset)
+ {
+ return SomeType(offset);
+ };
+};
+
+RWStructuredBuffer<int> outputBuffer;
+
+[numthreads(4, 4, 1)]
+void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
+{
+ int x = dispatchThreadID.x;
+
+ InterfaceImpl impl;
+
+ IInterface intf = impl;
+
+ var r = intf.get(16);
+
+ outputBuffer[dispatchThreadID.x] = x + r.getValue(x);
+} \ No newline at end of file
diff --git a/tests/experiments/interface/return-interface.slang b/tests/experiments/interface/return-interface.slang
new file mode 100644
index 000000000..d7e55e40c
--- /dev/null
+++ b/tests/experiments/interface/return-interface.slang
@@ -0,0 +1,50 @@
+//TEST_TEST:SIMPLE:-target dxil -entry computeMain -profile cs_6_2
+
+/*
+Testing returning an interface type.
+
+Works.
+
+Uses dynamic dispatch like code - but in the end doesn't require any switches (there is only a single impl). That means it could in principal remove all the extraeous code used assuming it does dynamically dispatch.
+ */
+
+interface IGetValue
+{
+ int getValue(int index);
+};
+
+struct SomeType : IGetValue
+{
+ __init(int inOffset) { offset = inOffset; }
+ int getValue(int index) { return index + offset; }
+ int offset;
+};
+
+interface IInterface
+{
+ IGetValue get(int offset);
+};
+
+struct InterfaceImpl : IInterface
+{
+ IGetValue get(int offset)
+ {
+ return SomeType(offset);
+ };
+};
+
+RWStructuredBuffer<int> outputBuffer;
+
+[numthreads(4, 4, 1)]
+void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
+{
+ int x = dispatchThreadID.x;
+
+ InterfaceImpl impl;
+
+ IInterface intf = impl;
+
+ var r = intf.get(16);
+
+ outputBuffer[dispatchThreadID.x] = x + r.getValue(x);
+} \ No newline at end of file