From 2482271fffbe85efc1bd7efcf74a76f6ed436012 Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 29 Apr 2021 14:19:51 -0700 Subject: `gfx` DebugCallback and debug layer. (#1822) * `gfx` DebugCallback and debug layer. --- examples/example-base/example-base.cpp | 51 ++++++++++++++++++++++++++++++++++ examples/example-base/example-base.h | 3 ++ 2 files changed, 54 insertions(+) (limited to 'examples/example-base') diff --git a/examples/example-base/example-base.cpp b/examples/example-base/example-base.cpp index a75ddf247..04f938697 100644 --- a/examples/example-base/example-base.cpp +++ b/examples/example-base/example-base.cpp @@ -25,6 +25,10 @@ Slang::Result WindowedAppBase::initializeBase(const char* title, int width, int gWindow->events.sizeChanged = Slang::Action<>(this, &WindowedAppBase::windowSizeChanged); // Initialize the rendering layer. +#ifdef _DEBUG + // Enable debug layer in debug config. + gfxEnableDebugLayer(); +#endif IDevice::Desc deviceDesc = {}; // deviceDesc.slang.targetFlags = SLANG_TARGET_FLAG_DUMP_IR; gfx::Result res = gfxCreateDevice(&deviceDesc, gDevice.writeRef()); @@ -170,6 +174,53 @@ int64_t getCurrentTime() { return std::chrono::high_resolution_clock::now().time int64_t getTimerFrequency() { return std::chrono::high_resolution_clock::period::den; } +class DebugCallback : public IDebugCallback +{ +public: + virtual SLANG_NO_THROW void SLANG_MCALL + handleMessage(DebugMessageType type, DebugMessageSource source, const char* message) override + { + const char* typeStr = ""; + switch (type) + { + case DebugMessageType::Info: + typeStr = "INFO: "; + break; + case DebugMessageType::Warning: + typeStr = "WARNING: "; + break; + case DebugMessageType::Error: + typeStr = "ERROR: "; + break; + default: + break; + } + const char* sourceStr = "[GraphicsLayer]: "; + switch (source) + { + case DebugMessageSource::Slang: + sourceStr = "[Slang]: "; + break; + case DebugMessageSource::Driver: + sourceStr = "[Driver]: "; + break; + } + printf("%s%s%s\n", sourceStr, typeStr, message); +#ifdef _WIN32 + OutputDebugStringA(sourceStr); + OutputDebugStringA(typeStr); + OutputDebugStringW(String(message).toWString()); + OutputDebugStringW(L"\n"); +#endif + } +}; + +void initDebugCallback() +{ + static DebugCallback callback = {}; + gfxSetDebugCallback(&callback); +} + #ifdef _WIN32 void _Win32OutputDebugString(const char* str) { OutputDebugStringW(Slang::String(str).toWString().begin()); } #endif diff --git a/examples/example-base/example-base.h b/examples/example-base/example-base.h index 67f722c11..a1c46aa53 100644 --- a/examples/example-base/example-base.h +++ b/examples/example-base/example-base.h @@ -72,10 +72,13 @@ inline void diagnoseIfNeeded(slang::IBlob* diagnosticsBlob) } } +void initDebugCallback(); template int innerMain() { + initDebugCallback(); + TApp app; if (SLANG_FAILED(app.initialize())) -- cgit v1.2.3