summaryrefslogtreecommitdiffstats
path: root/examples/gpu-printing/kernels.slang
diff options
context:
space:
mode:
authorTim Foley <tfoleyNV@users.noreply.github.com>2020-02-03 17:36:32 -0800
committerGitHub <noreply@github.com>2020-02-03 17:36:32 -0800
commitb415760d7f166eaad7fa27daa09edc9a8964c37e (patch)
tree1d9035d18dc6767ed4c4100a289365bd08a02514 /examples/gpu-printing/kernels.slang
parent2c1fbf8330efc34b85e09ee9b101c6a55327778a (diff)
Initial steps on GPU printing example (#1197)
* Initial steps on GPU printing example This change is checkpointing work on a new Slang example that shows how a "GPU `printf()`" can be implemented almost entirely in user space thanks to a combination of Slang language and API features. The example is not perfect as it stands today due to limitations in our current handling of hashed string literals: * At call sites where a string literal is passed, we currently have to explicitly invoke `getStringHash()` to get the hash code, because we don't currently support `String` as a function argument/parameter type. * On the implementation side, because strings are passed as their `int` hash codes, we can't tell them apart from ordinary `int` arguments. The current code handles this by assuming an `int` is *always* a hashed string, which obviously isn't appropriate. There are plenty of other limitations in the implementation presented, but the above are the two main things I'd like to address in follow-up work. I would like to checkpoint this work on the application first, in order to keep work on the Slang implementation and the example as separate as possible. * typo
Diffstat (limited to 'examples/gpu-printing/kernels.slang')
-rw-r--r--examples/gpu-printing/kernels.slang38
1 files changed, 38 insertions, 0 deletions
diff --git a/examples/gpu-printing/kernels.slang b/examples/gpu-printing/kernels.slang
new file mode 100644
index 000000000..ec4533958
--- /dev/null
+++ b/examples/gpu-printing/kernels.slang
@@ -0,0 +1,38 @@
+// kernels.slang
+
+// This file demonstrates how ordinary shader code can make use of
+// a standalone GPU printing library implemented in `printing.slang`.
+//
+// The first step for using a module of Slang code is to `import` it.
+//
+import printing;
+//
+// The `import` declaration above brings all the types and functions
+// declared in the `printing` module (in `printing.slang`) into
+// scope so that our code can use them.
+
+// For simplicity, we will define a single compute shader that does
+// some simple printing.
+//
+[shader("compute")]
+[numthreads(32)]
+void computeMain(uint3 tid : SV_DispatchThreadID)
+{
+ // The `printing` module defines two main printing routines.
+ //
+ // The first is a `println` function in the style of Java,
+ // which takes zero or more arguments and prints them all
+ // followed by a newline.
+ //
+ // HACK: We are having to explicitly call `getStringHash` here
+ // because the print implementation wants to write out strings
+ // in terms of their hash code, and the current Slang implementation
+ // of `getStringHash` only applies to string literals.
+ //
+ println(getStringHash("hello from thread number "), tid.x);
+
+ // The second facility supported by `printing.slang` is a C-style
+ // `printf()` function.
+ //
+ printf(getStringHash("printf from thread 0x%x\n"), tid.x);
+}