Fix crash when creating texture view on textures from Metal swap chain
For the textures got from Metal swap chain, their "framebufferOnly" may be true, which means they can only be used as attachments in a render pass, and they are not allowed to be used in MTLRenderCommandEncoder, MTLBlitCommandEncoder or MTLComputeCommandEncoder. So currently Dawn examples all crash when METAL_DEVICE_WRAPPER_TYPE = 1 is set into environmental variables. This patch adds checks on the situations that we do not need to create a Metal texture view: 1. We create Metal texture only when the usage of the texture includes Sampled or Storage. 2. We won't create Metal texture view if the view uses the same format as the original texture, the whole mipmap levels and array slices. 3. We use the original MTLTexture and set the slice and level in MTLRenderPassDescriptor. Furthermore, with this patch, "setFramebufferOnly" is set to true only when the usage passed to configure is a subset of (OutputAttachment | Present). BUG=dawn:69 Change-Id: Ie2670f383c16eafa3b1c6f99126922e940721174 Reviewed-on: https://dawn-review.googlesource.com/c/3400 Reviewed-by: Corentin Wallez <cwallez@chromium.org> Reviewed-by: Kai Ninomiya <kainino@chromium.org> Commit-Queue: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
parent
92700bfccd
commit
5dee56f39c
|
@ -64,7 +64,10 @@ namespace dawn_native { namespace metal {
|
|||
}
|
||||
|
||||
descriptor.colorAttachments[i].texture =
|
||||
ToBackend(attachmentInfo.view)->GetMTLTexture();
|
||||
ToBackend(attachmentInfo.view->GetTexture())->GetMTLTexture();
|
||||
descriptor.colorAttachments[i].level = attachmentInfo.view->GetBaseMipLevel();
|
||||
descriptor.colorAttachments[i].slice = attachmentInfo.view->GetBaseArrayLayer();
|
||||
|
||||
descriptor.colorAttachments[i].storeAction = MTLStoreActionStore;
|
||||
}
|
||||
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
#include "dawn_native/metal/DeviceMTL.h"
|
||||
|
||||
namespace dawn_native { namespace metal {
|
||||
|
||||
MTLPixelFormat MetalPixelFormat(dawn::TextureFormat format) {
|
||||
switch (format) {
|
||||
case dawn::TextureFormat::R8G8B8A8Unorm:
|
||||
|
@ -40,6 +39,12 @@ namespace dawn_native { namespace metal {
|
|||
}
|
||||
|
||||
namespace {
|
||||
bool UsageNeedsTextureView(dawn::TextureUsageBit usage) {
|
||||
constexpr dawn::TextureUsageBit kUsageNeedsTextureView =
|
||||
dawn::TextureUsageBit::Storage | dawn::TextureUsageBit::Sampled;
|
||||
return usage & kUsageNeedsTextureView;
|
||||
}
|
||||
|
||||
MTLTextureUsage MetalTextureUsage(dawn::TextureUsageBit usage) {
|
||||
MTLTextureUsage result = MTLTextureUsageUnknown; // This is 0
|
||||
|
||||
|
@ -55,9 +60,9 @@ namespace dawn_native { namespace metal {
|
|||
result |= MTLTextureUsageRenderTarget;
|
||||
}
|
||||
|
||||
// TODO(jiawei.shao@intel.com): investigate if we should skip setting this flag when the
|
||||
// texture is only used as a render target.
|
||||
if (UsageNeedsTextureView(usage)) {
|
||||
result |= MTLTextureUsagePixelFormatView;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -85,6 +90,31 @@ namespace dawn_native { namespace metal {
|
|||
return MTLTextureType2D;
|
||||
}
|
||||
}
|
||||
|
||||
bool RequiresCreatingNewTextureView(const TextureBase* texture,
|
||||
const TextureViewDescriptor* textureViewDescriptor) {
|
||||
if (texture->GetFormat() != textureViewDescriptor->format) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (texture->GetArrayLayers() != textureViewDescriptor->layerCount) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (texture->GetNumMipLevels() != textureViewDescriptor->levelCount) {
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (textureViewDescriptor->dimension) {
|
||||
case dawn::TextureViewDimension::Cube:
|
||||
case dawn::TextureViewDimension::CubeArray:
|
||||
return true;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Texture::Texture(Device* device, const TextureDescriptor* descriptor)
|
||||
|
@ -121,27 +151,33 @@ namespace dawn_native { namespace metal {
|
|||
return mMtlTexture;
|
||||
}
|
||||
|
||||
// TODO(jiawei.shao@intel.com): use the original texture directly when the descriptor covers the
|
||||
// whole texture in the same format (for example, when CreateDefaultTextureView() is called).
|
||||
TextureView::TextureView(TextureBase* texture, const TextureViewDescriptor* descriptor)
|
||||
: TextureViewBase(texture, descriptor) {
|
||||
id<MTLTexture> mtlTexture = ToBackend(texture)->GetMTLTexture();
|
||||
|
||||
if (!UsageNeedsTextureView(texture->GetUsage())) {
|
||||
mMtlTextureView = nil;
|
||||
} else if (!RequiresCreatingNewTextureView(texture, descriptor)) {
|
||||
mMtlTextureView = [mtlTexture retain];
|
||||
} else {
|
||||
MTLPixelFormat format = MetalPixelFormat(descriptor->format);
|
||||
MTLTextureType textureViewType = MetalTextureViewType(descriptor->dimension);
|
||||
auto mipLevelRange = NSMakeRange(descriptor->baseMipLevel, descriptor->levelCount);
|
||||
auto arrayLayerRange = NSMakeRange(descriptor->baseArrayLayer, descriptor->layerCount);
|
||||
|
||||
id<MTLTexture> mtlTexture = ToBackend(texture)->GetMTLTexture();
|
||||
mMtlTextureView = [mtlTexture newTextureViewWithPixelFormat:format
|
||||
textureType:textureViewType
|
||||
levels:mipLevelRange
|
||||
slices:arrayLayerRange];
|
||||
}
|
||||
}
|
||||
|
||||
TextureView::~TextureView() {
|
||||
[mMtlTextureView release];
|
||||
}
|
||||
|
||||
id<MTLTexture> TextureView::GetMTLTexture() {
|
||||
ASSERT(mMtlTextureView != nil);
|
||||
return mMtlTextureView;
|
||||
}
|
||||
}} // namespace dawn_native::metal
|
||||
|
|
|
@ -41,7 +41,7 @@ namespace utils {
|
|||
}
|
||||
|
||||
dawnSwapChainError Configure(dawnTextureFormat format,
|
||||
dawnTextureUsageBit,
|
||||
dawnTextureUsageBit usage,
|
||||
uint32_t width,
|
||||
uint32_t height) {
|
||||
if (format != DAWN_TEXTURE_FORMAT_B8_G8_R8_A8_UNORM) {
|
||||
|
@ -60,9 +60,15 @@ namespace utils {
|
|||
mLayer = [CAMetalLayer layer];
|
||||
[mLayer setDevice:mMtlDevice];
|
||||
[mLayer setPixelFormat:MTLPixelFormatBGRA8Unorm];
|
||||
[mLayer setFramebufferOnly:YES];
|
||||
[mLayer setDrawableSize:size];
|
||||
|
||||
constexpr uint32_t kFramebufferOnlyTextureUsages =
|
||||
DAWN_TEXTURE_USAGE_BIT_OUTPUT_ATTACHMENT | DAWN_TEXTURE_USAGE_BIT_PRESENT;
|
||||
bool hasOnlyFramebufferUsages = !(usage & (~kFramebufferOnlyTextureUsages));
|
||||
if (hasOnlyFramebufferUsages) {
|
||||
[mLayer setFramebufferOnly:YES];
|
||||
}
|
||||
|
||||
[contentView setLayer:mLayer];
|
||||
|
||||
return DAWN_SWAP_CHAIN_NO_ERROR;
|
||||
|
|
Loading…
Reference in New Issue