From c724803e4e80a755a6cc72ba6609a0f01afeb0c6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20M=C3=BCller?= <tmueller@nvidia.com>
Date: Mon, 9 Jan 2023 10:01:46 +0100
Subject: [PATCH] Fix potential crash when using WSL with some drivers

---
 include/neural-graphics-primitives/common.h |  2 ++
 src/common.cu                               | 15 +++++++++++++++
 src/render_buffer.cu                        | 14 --------------
 src/testbed.cu                              |  4 ++--
 4 files changed, 19 insertions(+), 16 deletions(-)

diff --git a/include/neural-graphics-primitives/common.h b/include/neural-graphics-primitives/common.h
index a4a2c05..6e2b1e4 100644
--- a/include/neural-graphics-primitives/common.h
+++ b/include/neural-graphics-primitives/common.h
@@ -63,6 +63,8 @@
 
 NGP_NAMESPACE_BEGIN
 
+bool is_wsl();
+
 filesystem::path get_executable_dir();
 filesystem::path get_root_dir();
 
diff --git a/src/common.cu b/src/common.cu
index 2ee41f1..a0b4389 100644
--- a/src/common.cu
+++ b/src/common.cu
@@ -28,6 +28,21 @@ namespace fs = filesystem;
 
 NGP_NAMESPACE_BEGIN
 
+bool is_wsl() {
+#ifdef _WIN32
+	return false;
+#else
+	fs::path path = "/proc/sys/kernel/osrelease";
+	if (!path.exists()) {
+		return false;
+	}
+
+	std::ifstream f{path.str()};
+	std::string content((std::istreambuf_iterator<char>(f)), (std::istreambuf_iterator<char>()));
+	return content.find("microsoft") != std::string::npos;
+#endif
+}
+
 fs::path get_executable_dir() {
 #ifdef _WIN32
 	WCHAR path[MAX_PATH];
diff --git a/src/render_buffer.cu b/src/render_buffer.cu
index 12ab753..e70aae0 100644
--- a/src/render_buffer.cu
+++ b/src/render_buffer.cu
@@ -174,20 +174,6 @@ void GLTexture::resize(const Vector2i& new_size, int n_channels, bool is_8bit) {
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 }
 
-static bool is_wsl() {
-#ifdef _WIN32
-	return false;
-#else
-	fs::path path = "/proc/sys/kernel/osrelease";
-	if (!path.exists()) {
-		return false;
-	}
-
-	std::ifstream f{path.str()};
-	std::string content((std::istreambuf_iterator<char>(f)), (std::istreambuf_iterator<char>()));
-	return content.find("microsoft") != std::string::npos;
-#endif
-}
 
 GLTexture::CUDAMapping::CUDAMapping(GLuint texture_id, const Vector2i& size) : m_size{size} {
 	static bool s_is_cuda_interop_supported = !is_wsl();
diff --git a/src/testbed.cu b/src/testbed.cu
index c50d269..119b011 100644
--- a/src/testbed.cu
+++ b/src/testbed.cu
@@ -2892,7 +2892,7 @@ Testbed::Testbed(ETestbedMode mode) {
 #ifdef NGP_GUI
 	// Ensure we're running on the GPU that'll host our GUI. To do so, try creating a dummy
 	// OpenGL context, figure out the GPU it's running on, and then kill that context again.
-	if (glfwInit()) {
+	if (!is_wsl() && glfwInit()) {
 		glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
 		GLFWwindow* offscreen_context = glfwCreateWindow(640, 480, "", NULL, NULL);
 
@@ -2912,11 +2912,11 @@ Testbed::Testbed(ETestbedMode mode) {
 
 		glfwTerminate();
 	}
+#endif
 
 	// Reset our stream, which was allocated on the originally active device,
 	// to make sure it corresponds to the now active device.
 	m_stream = {};
-#endif
 
 	int active_device = cuda_device();
 	int active_compute_capability = cuda_compute_capability();
-- 
GitLab