yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
2.0 KiB57 linesraw
1// gpu-printing.h
2#pragma once
3
4// This file provides the CPU side support for a basic GPU
5// printing system. The GPU implementation of the system
6// is in `printing.slang`.
7
8// The host side of the system needs to be able to load
9// strings that were specified in Slang shader code, and
10// for that it will use the Slang reflection API.
11//
12#include "slang.h"
13
14// We also need a way to store the data for strings that
15// were used in shader code, and we will go ahead and
16// use the C++ STL for that, in order to make this
17// code moderately portable.
18//
19#include <map>
20#include <string>
21
22/// Stores state used for executing print commands generated by GPU shaders
23struct GPUPrinting
24{
25public:
26    /// Load any string literals used by a Slang program.
27    ///
28    /// The `slangReflection` should be the layout and reflection
29    /// object for a Slang shader program that might need to produce
30    /// printed output. This function will load any strings
31    /// referenced by the program into its database for mapping
32    /// string hashes back to the original strings.
33    ///
34    void loadStrings(slang::ProgramLayout* slangReflection);
35
36    /// Process a buffer of GPU printing commands and write output to `stdout`.
37    ///
38    /// This function attempts to read print commands from the buffer
39    /// pointed to by `data` and execute them to produce output.
40    ///
41    /// The buffer pointed at by `data` (of size `dataSize`) should be allocated
42    /// in host-visible memory.
43    ///
44    /// Before executing GPU work, the first four bytes pointed to by `data`
45    /// should have been cleared to zero.
46    ///
47    /// If GPU work has attempted to write more data than the buffer
48    /// can fit, a warning will be printed to `stderr`, and printing commands
49    /// that could not fit completely in the buffer will be skipped.
50    ///
51    void processGPUPrintCommands(const void* data, size_t dataSize);
52
53private:
54    typedef int StringHash;
55
56    std::map<StringHash, std::string> m_hashedStrings;
57};