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:
Jiawei Shao
2018-12-29 10:47:28 +00:00
committed by Commit Bot service account
parent 92700bfccd
commit 5dee56f39c
3 changed files with 63 additions and 18 deletions

View File

@@ -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;