Split #defines for OpenGL and OpenGL ES backends.

This permits enabling the OpenGL and OpenGL ES backends independently.
This change also enables the OpenGL ES backend on Windows.

This will cause the end2end tests to run on OpenGL ES on the
GPU-less bots, via SwANGLE.

Bug: dawn:580

Change-Id: I43d514b18862d176610b95e97013a67723ddac20
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/50881
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Stephen White <senorblanco@chromium.org>
diff --git a/examples/SampleUtils.cpp b/examples/SampleUtils.cpp
index 3a03728..8c7ea0c 100644
--- a/examples/SampleUtils.cpp
+++ b/examples/SampleUtils.cpp
@@ -72,7 +72,9 @@
 static wgpu::BackendType backendType = wgpu::BackendType::Metal;
 #elif defined(DAWN_ENABLE_BACKEND_VULKAN)
 static wgpu::BackendType backendType = wgpu::BackendType::Vulkan;
-#elif defined(DAWN_ENABLE_BACKEND_OPENGL)
+#elif defined(DAWN_ENABLE_BACKEND_OPENGLES)
+static wgpu::BackendType backendType = wgpu::BackendType::OpenGLES;
+#elif defined(DAWN_ENABLE_BACKEND_DESKTOP_GL)
 static wgpu::BackendType backendType = wgpu::BackendType::OpenGL;
 #else
 #    error
diff --git a/scripts/dawn_features.gni b/scripts/dawn_features.gni
index 117c661..444cf37 100644
--- a/scripts/dawn_features.gni
+++ b/scripts/dawn_features.gni
@@ -62,7 +62,11 @@
 
   # Enables the compilation of Dawn's OpenGL backend
   # (best effort, non-conformant)
-  dawn_enable_opengl = is_linux && !is_chromeos
+  dawn_enable_desktop_gl = is_linux && !is_chromeos
+
+  # Enables the compilation of Dawn's OpenGLES backend
+  # (WebGPU/Compat subset)
+  dawn_enable_opengles = (is_linux && !is_chromeos) || is_win
 
   # Enables the compilation of Dawn's Vulkan backend
   # Disables vulkan when compiling for UWP, since UWP only supports d3d
@@ -97,3 +101,6 @@
 # UWP only supports CoreWindow for windowing
 dawn_supports_glfw_for_windowing =
     (is_win && !dawn_is_winuwp) || (is_linux && !is_chromeos) || is_mac
+
+# Much of the backend code is shared, so define a convenience var.
+dawn_enable_opengl = dawn_enable_opengles || dawn_enable_desktop_gl
diff --git a/src/common/BUILD.gn b/src/common/BUILD.gn
index ebb8771..6b93bcb 100644
--- a/src/common/BUILD.gn
+++ b/src/common/BUILD.gn
@@ -71,6 +71,12 @@
   if (dawn_enable_opengl) {
     defines += [ "DAWN_ENABLE_BACKEND_OPENGL" ]
   }
+  if (dawn_enable_desktop_gl) {
+    defines += [ "DAWN_ENABLE_BACKEND_DESKTOP_GL" ]
+  }
+  if (dawn_enable_opengles) {
+    defines += [ "DAWN_ENABLE_BACKEND_OPENGLES" ]
+  }
   if (dawn_enable_vulkan) {
     defines += [ "DAWN_ENABLE_BACKEND_VULKAN" ]
   }
diff --git a/src/dawn_native/Instance.cpp b/src/dawn_native/Instance.cpp
index 7d65e4d..2b88f5a 100644
--- a/src/dawn_native/Instance.cpp
+++ b/src/dawn_native/Instance.cpp
@@ -150,10 +150,12 @@
         Register(vulkan::Connect(this, true), wgpu::BackendType::Vulkan);
 #    endif  // defined(DAWN_ENABLE_SWIFTSHADER)
 #endif      // defined(DAWN_ENABLE_BACKEND_VULKAN)
-#if defined(DAWN_ENABLE_BACKEND_OPENGL)
+#if defined(DAWN_ENABLE_BACKEND_DESKTOP_GL)
         Register(opengl::Connect(this, wgpu::BackendType::OpenGL), wgpu::BackendType::OpenGL);
+#endif  // defined(DAWN_ENABLE_BACKEND_DESKTOP_GL)
+#if defined(DAWN_ENABLE_BACKEND_OPENGLES)
         Register(opengl::Connect(this, wgpu::BackendType::OpenGLES), wgpu::BackendType::OpenGLES);
-#endif  // defined(DAWN_ENABLE_BACKEND_OPENGL)
+#endif  // defined(DAWN_ENABLE_BACKEND_OPENGLES)
 #if defined(DAWN_ENABLE_BACKEND_NULL)
         Register(null::Connect(this), wgpu::BackendType::Null);
 #endif  // defined(DAWN_ENABLE_BACKEND_NULL)
diff --git a/src/tests/DawnTest.cpp b/src/tests/DawnTest.cpp
index b1c9691..7b76c08 100644
--- a/src/tests/DawnTest.cpp
+++ b/src/tests/DawnTest.cpp
@@ -386,7 +386,7 @@
     instance->SetBackendValidationLevel(mBackendValidationLevel);
     instance->DiscoverDefaultAdapters();
 
-#ifdef DAWN_ENABLE_BACKEND_OPENGL
+#ifdef DAWN_ENABLE_BACKEND_DESKTOP_GL
     if (!glfwInit()) {
         return instance;
     }
@@ -403,11 +403,16 @@
     dawn_native::opengl::AdapterDiscoveryOptions adapterOptions;
     adapterOptions.getProc = reinterpret_cast<void* (*)(const char*)>(glfwGetProcAddress);
     instance->DiscoverAdapters(&adapterOptions);
+#endif  // DAWN_ENABLE_BACKEND_DESKTOP_GL
+
+#ifdef DAWN_ENABLE_BACKEND_OPENGLES
 
     if (GetEnvironmentVar("ANGLE_DEFAULT_PLATFORM").empty()) {
         SetEnvironmentVar("ANGLE_DEFAULT_PLATFORM", "swiftshader");
     }
-
+    if (!glfwInit()) {
+        return instance;
+    }
     glfwDefaultWindowHints();
     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
@@ -419,10 +424,10 @@
 
     glfwMakeContextCurrent(mOpenGLESWindow);
     dawn_native::opengl::AdapterDiscoveryOptionsES adapterOptionsES;
-    adapterOptionsES.getProc = adapterOptions.getProc;
+    adapterOptionsES.getProc = reinterpret_cast<void* (*)(const char*)>(glfwGetProcAddress);
     instance->DiscoverAdapters(&adapterOptionsES);
     glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE);
-#endif  // DAWN_ENABLE_BACKEND_OPENGL
+#endif  // DAWN_ENABLE_BACKEND_OPENGLES
 
     return instance;
 }
@@ -903,13 +908,16 @@
 
     device.SetUncapturedErrorCallback(OnDeviceError, this);
     device.SetDeviceLostCallback(OnDeviceLost, this);
-#if defined(DAWN_ENABLE_BACKEND_OPENGL)
+#if defined(DAWN_ENABLE_BACKEND_DESKTOP_GL)
     if (IsOpenGL()) {
         glfwMakeContextCurrent(gTestEnv->GetOpenGLWindow());
-    } else if (IsOpenGLES()) {
+    }
+#endif  // defined(DAWN_ENABLE_BACKEND_DESKTOP_GL)
+#if defined(DAWN_ENABLE_BACKEND_OPENGLES)
+    if (IsOpenGLES()) {
         glfwMakeContextCurrent(gTestEnv->GetOpenGLESWindow());
     }
-#endif
+#endif  // defined(DAWN_ENABLE_BACKEND_OPENGLES)
 }
 
 void DawnTestBase::TearDown() {
diff --git a/src/utils/BackendBinding.cpp b/src/utils/BackendBinding.cpp
index f0b79ee..15562de 100644
--- a/src/utils/BackendBinding.cpp
+++ b/src/utils/BackendBinding.cpp
@@ -86,8 +86,12 @@
                 return CreateNullBinding(window, device);
 #endif
 
-#if defined(DAWN_ENABLE_BACKEND_OPENGL)
+#if defined(DAWN_ENABLE_BACKEND_DESKTOP_GL)
             case wgpu::BackendType::OpenGL:
+                return CreateOpenGLBinding(window, device);
+#endif
+
+#if defined(DAWN_ENABLE_BACKEND_OPENGLES)
             case wgpu::BackendType::OpenGLES:
                 return CreateOpenGLBinding(window, device);
 #endif