yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
b415760d7
master
GPU Printing
This example demonstrates how supporting for printing formatted messages from GPU shader code can be implemented in application code, using language and API features provided by Slang.
Overview
If you want to read the code here, start with kernels.slang, which contains a compute shader entry point showing how simple printing operations in shader code can be made.
Once you see the client code, you will probably want to understand the implementation, so that you can add these features to your own codebase.
The GPU/shader part of the implementation resides in printing.slang, which provides a stand-alone Slang module intended to be brought into your code with import.
The comments in that file explain how the low-level implementaiton encoding of print data into buffers is performed, and then also shows how Slang language mechanisms can be used to wrap that low-level implementation in usable and extensible syntax.
The CPU part of the implementation resides in gpu-printing.{h,cpp}, which are responsible for taking GPU-generated buffers encoded by the code above, and translating it into host-side calls to C printf() and other console printing operations.
The CPU code also shows how to use the Slang reflection API to extract information from a compiled program to enable printing of strings by their hash codes.
The main.cpp file implements a small host application that loads the compute shader and executes it using the D3D11 API.
The code in this file is not especially relevant to the printing system.
Adding printing support to your own codebase
The code in this example is meant to provide a starting point for applications/frameworks/engines that want to allow shader code to print messages, for debugging logging, etc.
You can start by copying the gpu-printing.{h,cpp}, gpu-printing-ops.h, and printing.slang files into your codebase, and then modifying them to meet your needs.
The implementation presented here is not feature-complete, so you may want to extend and customize it by:
-
Adapting it to use the graphics API or wrapper layer appropriate to your codebase
-
Making more GPU data types printable (including types specific to your application)
-
Adding overloads of
println()andprintf()to support more arguments -
Customizing the encoding of print commands to make better use of space based on application-specific constraints
-
Handling extended
printf()formatting (width, precision, etc.) in the CPU code
Caveats
This code is not battle-tested, and it makes no promises about security.
It is probable that a malformed or malicious GPU shader could write data into the "print buffer" that causes the CPU code to invoke printf() or other C standard library functions with invalid arguments.
In this implementation, GPU printing commands are only "flushed" by the CPU on draw/dispatch boundaries. This means that the printing approach here cannot easily be used to diagnose deadlocks, infinite loops, or hardware/driver crashes. Extending the implementation to better support such cases would likely depend on using platform- or hardware-specific knowledge or functionality.
1GPU Printing 2============ 3 4This example demonstrates how supporting for printing formatted messages from GPU shader code can be implemented in application code, using language and API features provided by Slang. 5 6Overview 7-------- 8 9If you want to read the code here, start with `kernels.slang`, which contains a compute shader entry point showing how simple printing operations in shader code can be made. 10Once you see the client code, you will probably want to understand the implementation, so that you can add these features to your own codebase. 11 12The GPU/shader part of the implementation resides in `printing.slang`, which provides a stand-alone Slang module intended to be brought into your code with `import`. 13The comments in that file explain how the low-level implementaiton encoding of print data into buffers is performed, and then also shows how Slang language mechanisms can be used to wrap that low-level implementation in usable and extensible syntax. 14 15The CPU part of the implementation resides in `gpu-printing.{h,cpp}`, which are responsible for taking GPU-generated buffers encoded by the code above, and translating it into host-side calls to C `printf()` and other console printing operations. 16The CPU code also shows how to use the Slang reflection API to extract information from a compiled program to enable printing of strings by their hash codes. 17 18The `main.cpp` file implements a small host application that loads the compute shader and executes it using the D3D11 API. 19The code in this file is not especially relevant to the printing system. 20 21Adding printing support to your own codebase 22-------------------------------------------- 23 24The code in this example is meant to provide a starting point for applications/frameworks/engines that want to allow shader code to print messages, for debugging logging, etc. 25You can start by copying the `gpu-printing.{h,cpp}`, `gpu-printing-ops.h`, and `printing.slang` files into your codebase, and then modifying them to meet your needs. 26 27The implementation presented here is not feature-complete, so you may want to extend and customize it by: 28 29* Adapting it to use the graphics API or wrapper layer appropriate to your codebase 30 31* Making more GPU data types printable (including types specific to your application) 32 33* Adding overloads of `println()` and `printf()` to support more arguments 34 35* Customizing the encoding of print commands to make better use of space based on application-specific constraints 36 37* Handling extended `printf()` formatting (width, precision, etc.) in the CPU code 38 39Caveats 40------- 41 42This code is not battle-tested, and it makes no promises about security. 43It is probable that a malformed or malicious GPU shader could write data into the "print buffer" that causes the CPU code to invoke `printf()` or other C standard library functions with invalid arguments. 44 45In this implementation, GPU printing commands are only "flushed" by the CPU on draw/dispatch boundaries. 46This means that the printing approach here cannot easily be used to diagnose deadlocks, infinite loops, or hardware/driver crashes. 47Extending the implementation to better support such cases would likely depend on using platform- or hardware-specific knowledge or functionality.