From fd41f30c0b96beec4ddd821b70268b47e2280017 Mon Sep 17 00:00:00 2001 From: Stephen White Date: Tue, 29 Mar 2022 18:52:23 +0000 Subject: [PATCH] OpenGL: refactor FramebufferTexture* calls. Change-Id: Iee67027431d791d6425e0cc502f6b6a23bc160d6 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/85141 Reviewed-by: Austin Eng Commit-Queue: Stephen White --- src/dawn/native/opengl/CommandBufferGL.cpp | 57 ++++------------------ src/dawn/native/opengl/TextureGL.cpp | 15 ++++++ src/dawn/native/opengl/TextureGL.h | 1 + 3 files changed, 25 insertions(+), 48 deletions(-) diff --git a/src/dawn/native/opengl/CommandBufferGL.cpp b/src/dawn/native/opengl/CommandBufferGL.cpp index a549c82296..8971979836 100644 --- a/src/dawn/native/opengl/CommandBufferGL.cpp +++ b/src/dawn/native/opengl/CommandBufferGL.cpp @@ -390,35 +390,15 @@ namespace dawn::native::opengl { gl.GenFramebuffers(1, &writeFbo); } - const TextureBase* colorTexture = - renderPass->colorAttachments[i].view->GetTexture(); - ASSERT(colorTexture->IsMultisampledTexture()); - ASSERT(colorTexture->GetArrayLayers() == 1); - ASSERT(renderPass->colorAttachments[i].view->GetBaseMipLevel() == 0); + TextureView* colorView = ToBackend(renderPass->colorAttachments[i].view.Get()); - GLuint colorHandle = ToBackend(colorTexture)->GetHandle(); gl.BindFramebuffer(GL_READ_FRAMEBUFFER, readFbo); - gl.FramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, - ToBackend(colorTexture)->GetGLTarget(), colorHandle, 0); + colorView->BindToFramebuffer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0); - const TextureBase* resolveTexture = - renderPass->colorAttachments[i].resolveTarget->GetTexture(); - GLuint resolveTextureHandle = ToBackend(resolveTexture)->GetHandle(); - GLuint resolveTargetMipmapLevel = - renderPass->colorAttachments[i].resolveTarget->GetBaseMipLevel(); + TextureView* resolveView = + ToBackend(renderPass->colorAttachments[i].resolveTarget.Get()); gl.BindFramebuffer(GL_DRAW_FRAMEBUFFER, writeFbo); - if (resolveTexture->GetArrayLayers() == 1) { - gl.FramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, - GL_TEXTURE_2D, resolveTextureHandle, - resolveTargetMipmapLevel); - } else { - GLuint resolveTargetArrayLayer = - renderPass->colorAttachments[i].resolveTarget->GetBaseArrayLayer(); - gl.FramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, - resolveTextureHandle, resolveTargetMipmapLevel, - resolveTargetArrayLayer); - } - + resolveView->BindToFramebuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0); gl.BlitFramebuffer(0, 0, renderPass->width, renderPass->height, 0, 0, renderPass->width, renderPass->height, GL_COLOR_BUFFER_BIT, GL_NEAREST); @@ -889,21 +869,11 @@ namespace dawn::native::opengl { ColorAttachmentIndex attachmentCount(uint8_t(0)); for (ColorAttachmentIndex i : IterateBitSet(renderPass->attachmentState->GetColorAttachmentsMask())) { - TextureViewBase* textureView = renderPass->colorAttachments[i].view.Get(); - GLuint texture = ToBackend(textureView->GetTexture())->GetHandle(); - + TextureView* textureView = ToBackend(renderPass->colorAttachments[i].view.Get()); GLenum glAttachment = GL_COLOR_ATTACHMENT0 + static_cast(i); // Attach color buffers. - if (textureView->GetTexture()->GetArrayLayers() == 1) { - GLenum target = ToBackend(textureView->GetTexture())->GetGLTarget(); - gl.FramebufferTexture2D(GL_DRAW_FRAMEBUFFER, glAttachment, target, texture, - textureView->GetBaseMipLevel()); - } else { - gl.FramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, glAttachment, texture, - textureView->GetBaseMipLevel(), - textureView->GetBaseArrayLayer()); - } + textureView->BindToFramebuffer(GL_DRAW_FRAMEBUFFER, glAttachment); drawBuffers[i] = glAttachment; attachmentCount = i; attachmentCount++; @@ -911,8 +881,7 @@ namespace dawn::native::opengl { gl.DrawBuffers(static_cast(attachmentCount), drawBuffers.data()); if (renderPass->attachmentState->HasDepthStencilAttachment()) { - TextureViewBase* textureView = renderPass->depthStencilAttachment.view.Get(); - GLuint texture = ToBackend(textureView->GetTexture())->GetHandle(); + TextureView* textureView = ToBackend(renderPass->depthStencilAttachment.view.Get()); const Format& format = textureView->GetTexture()->GetFormat(); // Attach depth/stencil buffer. @@ -927,15 +896,7 @@ namespace dawn::native::opengl { UNREACHABLE(); } - if (textureView->GetTexture()->GetArrayLayers() == 1) { - GLenum target = ToBackend(textureView->GetTexture())->GetGLTarget(); - gl.FramebufferTexture2D(GL_DRAW_FRAMEBUFFER, glAttachment, target, texture, - textureView->GetBaseMipLevel()); - } else { - gl.FramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, glAttachment, texture, - textureView->GetBaseMipLevel(), - textureView->GetBaseArrayLayer()); - } + textureView->BindToFramebuffer(GL_DRAW_FRAMEBUFFER, glAttachment); } } diff --git a/src/dawn/native/opengl/TextureGL.cpp b/src/dawn/native/opengl/TextureGL.cpp index 2971a85004..3687848d27 100644 --- a/src/dawn/native/opengl/TextureGL.cpp +++ b/src/dawn/native/opengl/TextureGL.cpp @@ -586,4 +586,19 @@ namespace dawn::native::opengl { return mTarget; } + void TextureView::BindToFramebuffer(GLenum target, GLenum attachment) { + const OpenGLFunctions& gl = ToBackend(GetDevice())->gl; + + // Use the texture's handle and target, and the view's base mip level and base array layer + GLuint handle = ToBackend(GetTexture())->GetHandle(); + GLuint textarget = ToBackend(GetTexture())->GetGLTarget(); + GLuint mipLevel = GetBaseMipLevel(); + + if (textarget == GL_TEXTURE_2D_ARRAY || textarget == GL_TEXTURE_3D) { + gl.FramebufferTextureLayer(target, attachment, handle, mipLevel, GetBaseArrayLayer()); + } else { + gl.FramebufferTexture2D(target, attachment, textarget, handle, mipLevel); + } + } + } // namespace dawn::native::opengl diff --git a/src/dawn/native/opengl/TextureGL.h b/src/dawn/native/opengl/TextureGL.h index 74b34b9b3b..897022ca9e 100644 --- a/src/dawn/native/opengl/TextureGL.h +++ b/src/dawn/native/opengl/TextureGL.h @@ -54,6 +54,7 @@ namespace dawn::native::opengl { GLuint GetHandle() const; GLenum GetGLTarget() const; + void BindToFramebuffer(GLenum target, GLenum attachment); private: ~TextureView() override;