summaryrefslogtreecommitdiffstats
path: root/examples/gpu-printing/gpu-printing.h
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/gpu-printing.h
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/gpu-printing.h')
-rw-r--r--examples/gpu-printing/gpu-printing.h57
1 files changed, 57 insertions, 0 deletions
diff --git a/examples/gpu-printing/gpu-printing.h b/examples/gpu-printing/gpu-printing.h
new file mode 100644
index 000000000..81c2e615f
--- /dev/null
+++ b/examples/gpu-printing/gpu-printing.h
@@ -0,0 +1,57 @@
+// gpu-printing.h
+#pragma once
+
+// This file provides the CPU side support for a basic GPU
+// printing system. The GPU implementation of the system
+// is in `printing.slang`.
+
+// The host side of the system needs to be able to load
+// strings that were specified in Slang shader code, and
+// for that it will use the Slang reflection API.
+//
+#include <slang.h>
+
+// We also need a way to store the data for strings that
+// were used in shader code, and we will go ahead and
+// use the C++ STL for that, in order to make this
+// code moderately portable.
+//
+#include <map>
+#include <string>
+
+ /// Stores state used for executing print commands generated by GPU shaders
+struct GPUPrinting
+{
+public:
+ /// Load any string literals used by a Slang program.
+ ///
+ /// The `slangReflection` should be the layout and reflection
+ /// object for a Slang shader program that might need to produce
+ /// printed output. This function will load any strings
+ /// referenced by the program into its database for mapping
+ /// string hashes back to the original strings.
+ ///
+ void loadStrings(slang::ProgramLayout* slangReflection);
+
+ /// Process a buffer of GPU printing commands and write output to `stdout`.
+ ///
+ /// This function attempts to read print commands from the buffer
+ /// pointed to by `data` and execute them to produce output.
+ ///
+ /// The buffer pointed at by `data` (of size `dataSize`) should be allocated
+ /// in host-visible memory.
+ ///
+ /// Before executing GPU work, the first four bytes pointed to by `data`
+ /// should have been cleared to zero.
+ ///
+ /// If GPU work has attempted to write more data than the buffer
+ /// can fit, a warning will be printed to `stderr`, and printing commands
+ /// that could not fit completely in the buffer will be skipped.
+ ///
+ void processGPUPrintCommands(const void* data, size_t dataSize);
+
+private:
+ typedef int StringHash;
+
+ std::map<StringHash, std::string> m_hashedStrings;
+};