Support using a layer of a texture as a color attachment on OpenGL

This patch implements using a layer of a texture as a color attachment
on OpenGL by using glFramebufferTextureLayer. As WebGPU won't support
rendering into a texture view whose format is different from the
original texture, it is correct to use glFramebufferTexture on the
OpenGL backends.

BUG=dawn:16

Change-Id: Iab6d905ee119bb9bb2d1bb212134cf757483c4d4
Reviewed-on: https://dawn-review.googlesource.com/c/3560
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Commit-Queue: Jiawei Shao <jiawei.shao@intel.com>
diff --git a/src/dawn_native/opengl/CommandBufferGL.cpp b/src/dawn_native/opengl/CommandBufferGL.cpp
index 6602db8..d076185 100644
--- a/src/dawn_native/opengl/CommandBufferGL.cpp
+++ b/src/dawn_native/opengl/CommandBufferGL.cpp
@@ -488,8 +488,14 @@
                 GLuint texture = ToBackend(textureView->GetTexture())->GetHandle();
 
                 // Attach color buffers.
-                glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D,
-                                       texture, 0);
+                if (textureView->GetTexture()->GetArrayLayers() == 1) {
+                    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i,
+                                           GL_TEXTURE_2D, texture, textureView->GetBaseMipLevel());
+                } else {
+                    glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i,
+                                              texture, textureView->GetBaseMipLevel(),
+                                              textureView->GetBaseArrayLayer());
+                }
                 drawBuffers[i] = GL_COLOR_ATTACHMENT0 + i;
                 attachmentCount = i + 1;
 
diff --git a/src/tests/end2end/TextureViewTests.cpp b/src/tests/end2end/TextureViewTests.cpp
index 1ba98b6..470ed9f 100644
--- a/src/tests/end2end/TextureViewTests.cpp
+++ b/src/tests/end2end/TextureViewTests.cpp
@@ -631,5 +631,4 @@
 
 DAWN_INSTANTIATE_TEST(TextureViewSamplingTest, D3D12Backend, MetalBackend, OpenGLBackend, VulkanBackend)
 
-// TODO(jiawei.shao@intel.com): support using a layer of a texture as color attachment on OpenGL
-DAWN_INSTANTIATE_TEST(TextureViewRenderingTest, D3D12Backend, MetalBackend, VulkanBackend)
+DAWN_INSTANTIATE_TEST(TextureViewRenderingTest, D3D12Backend, MetalBackend, OpenGLBackend, VulkanBackend)