Workaround GL error when creating texture view from external GL textures

This patch adds a workaround on the GL error when we use glTextureView()
on a GL texture which is created outside DAWN and not configured by
glTexStorage*d(). glTextureView() is only allowed to be used on the
textures configured by glTexStorage*D(). When the external GL texture
is configured by glTexImage2D() (for example, textures from GLFW),
calling glTextureView() will cause an INVALID_OPERATION error.

To workaround this issue, we refer the solution on the Metal backend
that we avoid calling glTextureView() on the following senarios:
1. We may call glTextureView() only when the usage of the texture
   includes Sampled or Storage.
2. We won't call glTextureView() if the view uses the same format as the
   original texture, the whole mipmap levels and array slices.

BUG=dawn:16

Change-Id: Ibdfaa122ac061a2e2bb47f76e0030f1d0fc548a2
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/5780
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Commit-Queue: Jiawei Shao <jiawei.shao@intel.com>
This commit is contained in:
Jiawei Shao 2019-03-25 00:54:58 +00:00 committed by Commit Bot service account
parent 07950e80fe
commit 24930b382a
2 changed files with 53 additions and 10 deletions

View File

@ -83,6 +83,37 @@ namespace dawn_native { namespace opengl {
return handle; return handle;
} }
bool UsageNeedsTextureView(dawn::TextureUsageBit usage) {
constexpr dawn::TextureUsageBit kUsageNeedingTextureView =
dawn::TextureUsageBit::Storage | dawn::TextureUsageBit::Sampled;
return usage & kUsageNeedingTextureView;
}
bool RequiresCreatingNewTextureView(const TextureBase* texture,
const TextureViewDescriptor* textureViewDescriptor) {
if (texture->GetFormat() != textureViewDescriptor->format) {
return true;
}
if (texture->GetArrayLayers() != textureViewDescriptor->arrayLayerCount) {
return true;
}
if (texture->GetNumMipLevels() != textureViewDescriptor->mipLevelCount) {
return true;
}
switch (textureViewDescriptor->dimension) {
case dawn::TextureViewDimension::Cube:
case dawn::TextureViewDimension::CubeArray:
return true;
default:
break;
}
return false;
}
} // namespace } // namespace
// Texture // Texture
@ -144,24 +175,35 @@ namespace dawn_native { namespace opengl {
// TextureView // TextureView
TextureView::TextureView(TextureBase* texture, const TextureViewDescriptor* descriptor) TextureView::TextureView(TextureBase* texture, const TextureViewDescriptor* descriptor)
: TextureViewBase(texture, descriptor) { : TextureViewBase(texture, descriptor), mOwnsHandle(false) {
mTarget = TargetForTextureViewDimension(descriptor->dimension); mTarget = TargetForTextureViewDimension(descriptor->dimension);
// glTextureView() is supported on OpenGL version >= 4.3 if (!UsageNeedsTextureView(texture->GetUsage())) {
// TODO(jiawei.shao@intel.com): support texture view on OpenGL version <= 4.2 mHandle = 0;
mHandle = GenTexture(); } else if (!RequiresCreatingNewTextureView(texture, descriptor)) {
const Texture* textureGL = ToBackend(texture); mHandle = ToBackend(texture)->GetHandle();
TextureFormatInfo textureViewFormat = GetGLFormatInfo(descriptor->format); } else {
glTextureView(mHandle, mTarget, textureGL->GetHandle(), textureViewFormat.internalFormat, // glTextureView() is supported on OpenGL version >= 4.3
descriptor->baseMipLevel, descriptor->mipLevelCount, // TODO(jiawei.shao@intel.com): support texture view on OpenGL version <= 4.2
descriptor->baseArrayLayer, descriptor->arrayLayerCount); mHandle = GenTexture();
const Texture* textureGL = ToBackend(texture);
TextureFormatInfo textureViewFormat = GetGLFormatInfo(descriptor->format);
glTextureView(mHandle, mTarget, textureGL->GetHandle(),
textureViewFormat.internalFormat, descriptor->baseMipLevel,
descriptor->mipLevelCount, descriptor->baseArrayLayer,
descriptor->arrayLayerCount);
mOwnsHandle = true;
}
} }
TextureView::~TextureView() { TextureView::~TextureView() {
glDeleteTextures(1, &mHandle); if (mOwnsHandle) {
glDeleteTextures(1, &mHandle);
}
} }
GLuint TextureView::GetHandle() const { GLuint TextureView::GetHandle() const {
ASSERT(mHandle != 0);
return mHandle; return mHandle;
} }

View File

@ -55,6 +55,7 @@ namespace dawn_native { namespace opengl {
private: private:
GLuint mHandle; GLuint mHandle;
GLenum mTarget; GLenum mTarget;
bool mOwnsHandle;
}; };
}} // namespace dawn_native::opengl }} // namespace dawn_native::opengl