summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/cpu-com-example/main.cpp56
-rw-r--r--examples/cpu-com-example/shader.slang13
2 files changed, 51 insertions, 18 deletions
diff --git a/examples/cpu-com-example/main.cpp b/examples/cpu-com-example/main.cpp
index 2e9eeed79..dedbf69f5 100644
--- a/examples/cpu-com-example/main.cpp
+++ b/examples/cpu-com-example/main.cpp
@@ -7,7 +7,6 @@
#include <slang-com-ptr.h>
#include <slang-com-helper.h>
-
// This includes a useful small function for setting up the prelude (described more further below).
#include "../../source/core/slang-test-tool-util.h"
@@ -21,8 +20,9 @@ using namespace Slang;
class IDoThings : public ISlangUnknown
{
public:
- virtual int SLANG_MCALL doThing(int a, int b) = 0;
- virtual int SLANG_MCALL calcHash(const char* in) = 0;
+ virtual SLANG_NO_THROW int SLANG_MCALL doThing(int a, int b) = 0;
+ virtual SLANG_NO_THROW int SLANG_MCALL calcHash(const char* in) = 0;
+ virtual SLANG_NO_THROW void SLANG_MCALL printMessage(const char* in) = 0;
};
static int _calcHash(const char* in)
@@ -36,7 +36,7 @@ static int _calcHash(const char* in)
return hash;
}
-class DoThings :public IDoThings
+class DoThings : public IDoThings
{
public:
// We don't need queryInterface for this impl, or ref counting
@@ -45,17 +45,21 @@ public:
virtual SLANG_NO_THROW uint32_t SLANG_MCALL release() SLANG_OVERRIDE { return 1; }
// IDoThings
- virtual int SLANG_MCALL doThing(int a, int b) SLANG_OVERRIDE { return a + b + 1; }
- virtual int SLANG_MCALL calcHash(const char* in) SLANG_OVERRIDE { return (int)_calcHash(in); }
+ virtual SLANG_NO_THROW int SLANG_MCALL doThing(int a, int b) SLANG_OVERRIDE { return a + b + 1; }
+ virtual SLANG_NO_THROW int SLANG_MCALL calcHash(const char* in) SLANG_OVERRIDE { return (int)_calcHash(in); }
+ virtual SLANG_NO_THROW void SLANG_MCALL printMessage(const char* in) SLANG_OVERRIDE { printf("%s\n", in); }
};
static SlangResult _innerMain(int argc, char** argv)
{
+ // NOTE! This example only works if `slang-llvm` or a C++ compiler that Slang supports is available.
+
// Create the session
ComPtr<slang::IGlobalSession> slangSession;
slangSession.attach(spCreateSession(NULL));
// Set up the prelude
+ // NOTE: This isn't strictly necessary, as preludes are embedded in the binary.
TestToolUtil::setSessionDefaultPreludeFromExePath(argv[0], slangSession);
// Create a compile request
@@ -93,6 +97,19 @@ static SlangResult _innerMain(int argc, char** argv)
ComPtr<ISlangSharedLibrary> sharedLibrary;
SLANG_RETURN_ON_FAIL(request->getTargetHostCallable(0, sharedLibrary.writeRef()));
+ DoThings doThings;
+
+ {
+ auto doThingsPtr = (IDoThings**)sharedLibrary->findSymbolAddressByName("globalDoThings");
+ if (!doThingsPtr)
+ {
+ return SLANG_FAIL;
+ }
+ // Set the global interface
+ *doThingsPtr = &doThings;
+ }
+
+ // Test a free function
{
typedef const char* (*Func)(const char*);
Func func = (Func)sharedLibrary->findFuncByName("getString");
@@ -107,25 +124,34 @@ static SlangResult _innerMain(int argc, char** argv)
SLANG_ASSERT(text == returnedText);
}
- {
- typedef int (*Func)(const char* text, IDoThings* doThings);
+ // Test hash
+ {
+ typedef int (*Func)(const char* text);
Func func = (Func)sharedLibrary->findFuncByName("calcHash");
-
if (!func)
{
return SLANG_FAIL;
}
- DoThings doThings;
-
String text("Hello");
-
- const int hash = func(text.getBuffer(), &doThings);
-
+ const int hash = func(text.getBuffer());
SLANG_ASSERT(hash == _calcHash(text.getBuffer()));
}
-
+
+ // Test printing
+ {
+ typedef void (*Func)(const char* text);
+
+ Func func = (Func)sharedLibrary->findFuncByName("printMessage");
+
+ if (!func)
+ {
+ return SLANG_FAIL;
+ }
+ func("Hello World!");
+ }
+
return SLANG_OK;
}
diff --git a/examples/cpu-com-example/shader.slang b/examples/cpu-com-example/shader.slang
index b0fe259be..44b1f5b81 100644
--- a/examples/cpu-com-example/shader.slang
+++ b/examples/cpu-com-example/shader.slang
@@ -1,6 +1,8 @@
// shader.slang
-// Example of using 'NativeString'
+// Example using 'NativeString' and COM interface
+
+public __global __extern_cpp IDoThings globalDoThings;
public __extern_cpp NativeString getString(NativeString in)
{
@@ -12,10 +14,15 @@ interface IDoThings
{
int doThing(int a, int b);
int calcHash(NativeString in);
+ void printMessage(NativeString nativeString);
}
-public __extern_cpp int calcHash(NativeString text, IDoThings doThings)
+public __extern_cpp int calcHash(NativeString text)
{
- return doThings.calcHash(text);
+ return globalDoThings.calcHash(text);
}
+public __extern_cpp void printMessage(NativeString text)
+{
+ return globalDoThings.printMessage(text);
+}