yum-mirror/slang

Making it easier to work with shaders

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

Ellie Hermaszewskaformatf65d756bf

master
9.9 KiB192 linesraw
1// debug-helper-functions.h
2#pragma once
3#include "debug-base.h"
4#include "debug-buffer.h"
5#include "debug-command-buffer.h"
6#include "debug-command-queue.h"
7#include "debug-device.h"
8#include "debug-fence.h"
9#include "debug-framebuffer.h"
10#include "debug-pipeline-state.h"
11#include "debug-query.h"
12#include "debug-render-pass.h"
13#include "debug-resource-views.h"
14#include "debug-sampler-state.h"
15#include "debug-shader-object.h"
16#include "debug-shader-program.h"
17#include "debug-shader-table.h"
18#include "debug-swap-chain.h"
19#include "debug-texture.h"
20#include "debug-transient-heap.h"
21#include "debug-vertex-layout.h"
22
23namespace gfx
24{
25using namespace Slang;
26
27namespace debug
28{
29
30#ifdef __FUNCSIG__
31#define SLANG_FUNC_SIG __FUNCSIG__
32#elif defined(__PRETTY_FUNCTION__)
33#define SLANG_FUNC_SIG __FUNCSIG__
34#elif defined(__FUNCTION__)
35#define SLANG_FUNC_SIG __FUNCTION__
36#else
37#define SLANG_FUNC_SIG "UnknownFunction"
38#endif
39
40extern thread_local const char* _currentFunctionName;
41struct SetCurrentFuncRAII
42{
43    SetCurrentFuncRAII(const char* funcName) { _currentFunctionName = funcName; }
44    ~SetCurrentFuncRAII() { _currentFunctionName = nullptr; }
45};
46#define SLANG_GFX_API_FUNC SetCurrentFuncRAII setFuncNameRAII(SLANG_FUNC_SIG)
47#define SLANG_GFX_API_FUNC_NAME(x) SetCurrentFuncRAII setFuncNameRAII(x)
48
49/// Returns the public API function name from a `SLANG_FUNC_SIG` string.
50String _gfxGetFuncName(const char* input);
51
52template<typename... TArgs>
53char* _gfxDiagnoseFormat(
54    char* buffer,            // Initial buffer to output formatted string.
55    size_t shortBufferSize,  // Size of the initial buffer.
56    List<char>& bufferArray, // A list for allocating a large buffer if needed.
57    const char* format,      // The format string.
58    TArgs... args)
59{
60    int length = sprintf_s(buffer, shortBufferSize, format, args...);
61    if (length < 0)
62        return buffer;
63    if (length > 255)
64    {
65        bufferArray.setCount(length + 1);
66        buffer = bufferArray.getBuffer();
67        sprintf_s(buffer, bufferArray.getCount(), format, args...);
68    }
69    return buffer;
70}
71
72template<typename... TArgs>
73void _gfxDiagnoseImpl(DebugMessageType type, const char* format, TArgs... args)
74{
75    char shortBuffer[256];
76    List<char> bufferArray;
77    auto buffer =
78        _gfxDiagnoseFormat(shortBuffer, sizeof(shortBuffer), bufferArray, format, args...);
79    getDebugCallback()->handleMessage(type, DebugMessageSource::Layer, buffer);
80}
81
82#define GFX_DIAGNOSE_ERROR(message)                                                                \
83    _gfxDiagnoseImpl(                                                                              \
84        DebugMessageType::Error,                                                                   \
85        "%s: %s",                                                                                  \
86        _gfxGetFuncName(_currentFunctionName ? _currentFunctionName : SLANG_FUNC_SIG).getBuffer(), \
87        message)
88#define GFX_DIAGNOSE_WARNING(message)                                                              \
89    _gfxDiagnoseImpl(                                                                              \
90        DebugMessageType::Warning,                                                                 \
91        "%s: %s",                                                                                  \
92        _gfxGetFuncName(_currentFunctionName ? _currentFunctionName : SLANG_FUNC_SIG).getBuffer(), \
93        message)
94#define GFX_DIAGNOSE_INFO(message)                                                                 \
95    _gfxDiagnoseImpl(                                                                              \
96        DebugMessageType::Info,                                                                    \
97        "%s: %s",                                                                                  \
98        _gfxGetFuncName(_currentFunctionName ? _currentFunctionName : SLANG_FUNC_SIG).getBuffer(), \
99        message)
100#define GFX_DIAGNOSE_FORMAT(type, format, ...)                                            \
101    {                                                                                     \
102        char shortBuffer[256];                                                            \
103        List<char> bufferArray;                                                           \
104        auto message = _gfxDiagnoseFormat(                                                \
105            shortBuffer,                                                                  \
106            sizeof(shortBuffer),                                                          \
107            bufferArray,                                                                  \
108            format,                                                                       \
109            __VA_ARGS__);                                                                 \
110        _gfxDiagnoseImpl(                                                                 \
111            type,                                                                         \
112            "%s: %s",                                                                     \
113            _gfxGetFuncName(_currentFunctionName ? _currentFunctionName : SLANG_FUNC_SIG) \
114                .getBuffer(),                                                             \
115            message);                                                                     \
116    }
117#define GFX_DIAGNOSE_ERROR_FORMAT(...) GFX_DIAGNOSE_FORMAT(DebugMessageType::Error, __VA_ARGS__)
118
119#define SLANG_GFX_DEBUG_GET_INTERFACE_IMPL(typeName)                                    \
120    I##typeName* Debug##typeName::getInterface(const Slang::Guid& guid)                 \
121    {                                                                                   \
122        return (guid == GfxGUID::IID_ISlangUnknown || guid == GfxGUID::IID_I##typeName) \
123                   ? static_cast<I##typeName*>(this)                                    \
124                   : nullptr;                                                           \
125    }
126#define SLANG_GFX_DEBUG_GET_INTERFACE_IMPL_PARENT(typeName, parentType)                   \
127    I##typeName* Debug##typeName::getInterface(const Slang::Guid& guid)                   \
128    {                                                                                     \
129        return (guid == GfxGUID::IID_ISlangUnknown || guid == GfxGUID::IID_I##typeName || \
130                guid == GfxGUID::IID_I##parentType)                                       \
131                   ? static_cast<I##typeName*>(this)                                      \
132                   : nullptr;                                                             \
133    }
134
135// Utility conversion functions to get Debug* object or the inner object from a user provided
136// pointer.
137#define SLANG_GFX_DEBUG_GET_OBJ_IMPL(type)                                         \
138    inline Debug##type* getDebugObj(I##type* ptr)                                  \
139    {                                                                              \
140        return static_cast<Debug##type*>(static_cast<DebugObject<I##type>*>(ptr)); \
141    }                                                                              \
142    inline I##type* getInnerObj(I##type* ptr)                                      \
143    {                                                                              \
144        if (!ptr)                                                                  \
145            return nullptr;                                                        \
146        auto debugObj = getDebugObj(ptr);                                          \
147        return debugObj->baseObject;                                               \
148    }
149
150#define SLANG_GFX_DEBUG_GET_OBJ_IMPL_UNOWNED(type)                                        \
151    inline Debug##type* getDebugObj(I##type* ptr)                                         \
152    {                                                                                     \
153        return static_cast<Debug##type*>(static_cast<UnownedDebugObject<I##type>*>(ptr)); \
154    }                                                                                     \
155    inline I##type* getInnerObj(I##type* ptr)                                             \
156    {                                                                                     \
157        if (!ptr)                                                                         \
158            return nullptr;                                                               \
159        auto debugObj = getDebugObj(ptr);                                                 \
160        return debugObj->baseObject;                                                      \
161    }
162
163SLANG_GFX_DEBUG_GET_OBJ_IMPL(Device)
164SLANG_GFX_DEBUG_GET_OBJ_IMPL(BufferResource)
165SLANG_GFX_DEBUG_GET_OBJ_IMPL(TextureResource)
166SLANG_GFX_DEBUG_GET_OBJ_IMPL(CommandBuffer)
167SLANG_GFX_DEBUG_GET_OBJ_IMPL(CommandQueue)
168SLANG_GFX_DEBUG_GET_OBJ_IMPL_UNOWNED(ComputeCommandEncoder)
169SLANG_GFX_DEBUG_GET_OBJ_IMPL_UNOWNED(RenderCommandEncoder)
170SLANG_GFX_DEBUG_GET_OBJ_IMPL_UNOWNED(ResourceCommandEncoder)
171SLANG_GFX_DEBUG_GET_OBJ_IMPL_UNOWNED(RayTracingCommandEncoder)
172SLANG_GFX_DEBUG_GET_OBJ_IMPL(Framebuffer)
173SLANG_GFX_DEBUG_GET_OBJ_IMPL(FramebufferLayout)
174SLANG_GFX_DEBUG_GET_OBJ_IMPL(InputLayout)
175SLANG_GFX_DEBUG_GET_OBJ_IMPL(RenderPassLayout)
176SLANG_GFX_DEBUG_GET_OBJ_IMPL(PipelineState)
177SLANG_GFX_DEBUG_GET_OBJ_IMPL(ResourceView)
178SLANG_GFX_DEBUG_GET_OBJ_IMPL(SamplerState)
179SLANG_GFX_DEBUG_GET_OBJ_IMPL(ShaderObject)
180SLANG_GFX_DEBUG_GET_OBJ_IMPL(ShaderProgram)
181SLANG_GFX_DEBUG_GET_OBJ_IMPL(Swapchain)
182SLANG_GFX_DEBUG_GET_OBJ_IMPL(TransientResourceHeap)
183SLANG_GFX_DEBUG_GET_OBJ_IMPL(QueryPool)
184SLANG_GFX_DEBUG_GET_OBJ_IMPL(AccelerationStructure)
185SLANG_GFX_DEBUG_GET_OBJ_IMPL(Fence)
186SLANG_GFX_DEBUG_GET_OBJ_IMPL(ShaderTable)
187
188void validateAccelerationStructureBuildInputs(
189    const IAccelerationStructure::BuildInputs& buildInputs);
190
191} // namespace debug
192} // namespace gfx