summaryrefslogtreecommitdiffstats
path: root/examples/gpu-printing/gpu-printing.h
blob: 84c03654804ac41b56552703a5370c613ea35828 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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;
};