yum-mirror/slang

Making it easier to work with shaders

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

Jay KwakHandle debug-layer messages in a separate channel (#7988)aefd1e3e0

master
3.7 KiB147 linesraw
1// slang-support.h
2#pragma once
3
4#include "core/slang-std-writers.h"
5#include "options.h"
6#include "shader-input-layout.h"
7#include "slang.h"
8
9#include <slang-rhi.h>
10
11namespace renderer_test
12{
13
14/// Bridge from core debug callback to RHI debug callback
15/// This allows core callbacks to receive messages from RHI systems
16/// TODO: We should replace rhi::IDebugCallback with Slang::IDebugCallback.
17class CoreToRHIDebugBridge : public rhi::IDebugCallback
18{
19public:
20    void setCoreCallback(Slang::IDebugCallback* coreCallback) { m_coreCallback = coreCallback; }
21
22    virtual SLANG_NO_THROW void SLANG_MCALL handleMessage(
23        rhi::DebugMessageType type,
24        rhi::DebugMessageSource source,
25        const char* message) override
26    {
27        if (m_coreCallback)
28        {
29            // Convert RHI types to core types
30            Slang::DebugMessageType coreType = static_cast<Slang::DebugMessageType>(type);
31            Slang::DebugMessageSource coreSource = static_cast<Slang::DebugMessageSource>(source);
32            m_coreCallback->handleMessage(coreType, coreSource, message);
33        }
34    }
35
36private:
37    Slang::IDebugCallback* m_coreCallback = nullptr;
38};
39
40/// Core debug callback that captures debug messages in a string buffer
41class CoreDebugCallback : public Slang::IDebugCallback
42{
43public:
44    virtual SLANG_NO_THROW void SLANG_MCALL handleMessage(
45        Slang::DebugMessageType type,
46        Slang::DebugMessageSource source,
47        const char* message) override
48    {
49        SLANG_UNUSED(source);
50
51        // Only capture error messages
52        if (type == Slang::DebugMessageType::Error)
53        {
54            m_buf << message;
55            if (message[strlen(message) - 1] != '\n')
56            {
57                m_buf << '\n';
58            }
59        }
60    }
61
62    void clear() { m_buf.clear(); }
63    Slang::String getString() { return m_buf.toString(); }
64
65private:
66    Slang::StringBuilder m_buf;
67};
68
69struct ShaderCompileRequest
70{
71    struct SourceInfo
72    {
73        char const* path;
74
75        // The data may either be source text (in which
76        // case it can be assumed to be nul-terminated with
77        // `dataEnd` pointing at the terminator), or
78        // raw binary data (in which case `dataEnd` points
79        // at the end of the buffer).
80        char const* dataBegin;
81        char const* dataEnd;
82    };
83
84    struct EntryPoint
85    {
86        char const* name = nullptr;
87        SlangStage slangStage;
88    };
89
90    struct TypeConformance
91    {
92    public:
93        Slang::String derivedTypeName;
94        Slang::String baseTypeName;
95        Slang::Int idOverride;
96    };
97
98    SourceInfo source;
99    Slang::List<EntryPoint> entryPoints;
100
101    Slang::List<Slang::String> globalSpecializationArgs;
102    Slang::List<Slang::String> entryPointSpecializationArgs;
103    Slang::List<TypeConformance> typeConformances;
104};
105
106
107struct ShaderCompilerUtil
108{
109    struct Input
110    {
111        SlangCompileTarget target;
112        SlangSourceLanguage sourceLanguage;
113        SlangPassThrough passThrough;
114        Slang::String profile;
115    };
116
117    struct Output
118    {
119        void set(slang::IComponentType* slangProgram);
120        void reset();
121        ~Output() { reset(); }
122
123        ComPtr<slang::IComponentType> slangProgram;
124        ShaderProgramDesc desc = {};
125
126        ComPtr<slang::ISession> m_session = nullptr;
127
128        slang::IGlobalSession* globalSession = nullptr;
129    };
130
131    struct OutputAndLayout
132    {
133        Output output;
134        ShaderInputLayout layout;
135        Slang::String sourcePath;
136    };
137
138    // Wrapper for compileProgram
139    static SlangResult compileWithLayout(
140        slang::IGlobalSession* globalSession,
141        const Options& options,
142        const ShaderCompilerUtil::Input& input,
143        OutputAndLayout& output);
144};
145
146
147} // namespace renderer_test