summaryrefslogtreecommitdiffstats
path: root/examples/example-base
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2021-04-16 10:34:26 -0700
committerGitHub <noreply@github.com>2021-04-16 10:34:26 -0700
commit79e92395f8ce3d92c446e3bb3250d19ce33decd5 (patch)
tree2ac277fa299200da72cf03a2b5b96338f66aee5d /examples/example-base
parentbad484d838590d0a2aaf1b5b8ac820634af2decb (diff)
Update `model-viewer` example and fixing compiler bugs. (#1795)
Diffstat (limited to 'examples/example-base')
-rw-r--r--examples/example-base/example-base.cpp15
-rw-r--r--examples/example-base/example-base.h38
2 files changed, 53 insertions, 0 deletions
diff --git a/examples/example-base/example-base.cpp b/examples/example-base/example-base.cpp
index b06bbdf7e..09891e555 100644
--- a/examples/example-base/example-base.cpp
+++ b/examples/example-base/example-base.cpp
@@ -1,4 +1,10 @@
#include "example-base.h"
+#include <chrono>
+
+#ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
+#include <Windows.h>
+#endif
using namespace Slang;
using namespace gfx;
@@ -20,6 +26,7 @@ Slang::Result WindowedAppBase::initializeBase(const char* title, int width, int
// Initialize the rendering layer.
IDevice::Desc deviceDesc = {};
+ // deviceDesc.slang.targetFlags = SLANG_TARGET_FLAG_DUMP_IR;
gfx::Result res = gfxCreateDevice(&deviceDesc, gDevice.writeRef());
if (SLANG_FAILED(res))
return res;
@@ -158,3 +165,11 @@ void WindowedAppBase::windowSizeChanged()
}
}
}
+
+int64_t getCurrentTime() { return std::chrono::high_resolution_clock::now().time_since_epoch().count(); }
+
+int64_t getTimerFrequency() { return std::chrono::high_resolution_clock::period::den; }
+
+#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 ab7522f06..67f722c11 100644
--- a/examples/example-base/example-base.h
+++ b/examples/example-base/example-base.h
@@ -4,6 +4,10 @@
#include "tools/platform/window.h"
#include "source/core/slang-basic.h"
+#ifdef _WIN32
+void _Win32OutputDebugString(const char* str);
+#endif
+
struct WindowedAppBase
{
protected:
@@ -35,6 +39,40 @@ public:
virtual void finalize() { gQueue->wait(); }
};
+int64_t getCurrentTime();
+int64_t getTimerFrequency();
+
+template<typename ... TArgs> inline void reportError(const char* format, TArgs... args)
+{
+ printf(format, args...);
+#ifdef _WIN32
+ char buffer[4096];
+ sprintf_s(buffer, format, args...);
+ _Win32OutputDebugString(buffer);
+#endif
+}
+
+template <typename... TArgs> inline void log(const char* format, TArgs... args)
+{
+ reportError(format, args...);
+}
+
+// Many Slang API functions return detailed diagnostic information
+// (error messages, warnings, etc.) as a "blob" of data, or return
+// a null blob pointer instead if there were no issues.
+//
+// For convenience, we define a subroutine that will dump the information
+// in a diagnostic blob if one is produced, and skip it otherwise.
+//
+inline void diagnoseIfNeeded(slang::IBlob* diagnosticsBlob)
+{
+ if (diagnosticsBlob != nullptr)
+ {
+ reportError("%s", (const char*)diagnosticsBlob->getBufferPointer());
+ }
+}
+
+
template<typename TApp>
int innerMain()
{