Corentin Wallez | 4a9ef4e | 2018-07-18 11:40:26 +0200 | [diff] [blame^] | 1 | // Copyright 2017 The Dawn Authors |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Corentin Wallez | fffe6df | 2017-07-06 14:41:13 -0400 | [diff] [blame] | 15 | #include "backend/metal/TextureMTL.h" |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 16 | |
Corentin Wallez | 0f2e7b6 | 2018-06-18 18:19:31 -0400 | [diff] [blame] | 17 | #include "backend/metal/DeviceMTL.h" |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 18 | |
Corentin Wallez | f58d84d | 2017-11-24 14:12:44 -0500 | [diff] [blame] | 19 | namespace backend { namespace metal { |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 20 | |
Austin Eng | 476e5cb | 2017-08-03 12:17:14 -0400 | [diff] [blame] | 21 | MTLPixelFormat MetalPixelFormat(nxt::TextureFormat format) { |
| 22 | switch (format) { |
| 23 | case nxt::TextureFormat::R8G8B8A8Unorm: |
| 24 | return MTLPixelFormatRGBA8Unorm; |
Stephen White | cd4f8a2 | 2018-01-22 14:09:08 -0500 | [diff] [blame] | 25 | case nxt::TextureFormat::R8G8Unorm: |
| 26 | return MTLPixelFormatRG8Unorm; |
| 27 | case nxt::TextureFormat::R8Unorm: |
| 28 | return MTLPixelFormatR8Unorm; |
Austin Eng | fb19c36 | 2017-08-14 12:49:41 -0400 | [diff] [blame] | 29 | case nxt::TextureFormat::R8G8B8A8Uint: |
| 30 | return MTLPixelFormatRGBA8Uint; |
Stephen White | cd4f8a2 | 2018-01-22 14:09:08 -0500 | [diff] [blame] | 31 | case nxt::TextureFormat::R8G8Uint: |
| 32 | return MTLPixelFormatRG8Uint; |
| 33 | case nxt::TextureFormat::R8Uint: |
| 34 | return MTLPixelFormatR8Uint; |
Corentin Wallez | e862a33 | 2017-09-21 13:12:49 -0400 | [diff] [blame] | 35 | case nxt::TextureFormat::B8G8R8A8Unorm: |
| 36 | return MTLPixelFormatBGRA8Unorm; |
Austin Eng | 476e5cb | 2017-08-03 12:17:14 -0400 | [diff] [blame] | 37 | case nxt::TextureFormat::D32FloatS8Uint: |
| 38 | return MTLPixelFormatDepth32Float_Stencil8; |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 39 | } |
Austin Eng | 476e5cb | 2017-08-03 12:17:14 -0400 | [diff] [blame] | 40 | } |
Corentin Wallez | 8308b1c | 2017-07-03 23:02:49 -0400 | [diff] [blame] | 41 | |
Austin Eng | 476e5cb | 2017-08-03 12:17:14 -0400 | [diff] [blame] | 42 | namespace { |
Corentin Wallez | 8308b1c | 2017-07-03 23:02:49 -0400 | [diff] [blame] | 43 | MTLTextureUsage MetalTextureUsage(nxt::TextureUsageBit usage) { |
Corentin Wallez | f58d84d | 2017-11-24 14:12:44 -0500 | [diff] [blame] | 44 | MTLTextureUsage result = MTLTextureUsageUnknown; // This is 0 |
Corentin Wallez | 8308b1c | 2017-07-03 23:02:49 -0400 | [diff] [blame] | 45 | |
| 46 | if (usage & (nxt::TextureUsageBit::Storage)) { |
| 47 | result |= MTLTextureUsageShaderWrite | MTLTextureUsageShaderRead; |
| 48 | } |
| 49 | |
| 50 | if (usage & (nxt::TextureUsageBit::Sampled)) { |
| 51 | result |= MTLTextureUsageShaderRead; |
| 52 | } |
| 53 | |
Kai Ninomiya | cb2d6d8 | 2017-07-07 16:06:14 -0700 | [diff] [blame] | 54 | if (usage & (nxt::TextureUsageBit::OutputAttachment)) { |
Corentin Wallez | 8308b1c | 2017-07-03 23:02:49 -0400 | [diff] [blame] | 55 | result |= MTLTextureUsageRenderTarget; |
| 56 | } |
| 57 | |
| 58 | return result; |
| 59 | } |
| 60 | |
| 61 | MTLTextureType MetalTextureType(nxt::TextureDimension dimension) { |
| 62 | switch (dimension) { |
| 63 | case nxt::TextureDimension::e2D: |
| 64 | return MTLTextureType2D; |
| 65 | } |
| 66 | } |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Corentin Wallez | f58d84d | 2017-11-24 14:12:44 -0500 | [diff] [blame] | 69 | Texture::Texture(TextureBuilder* builder) : TextureBase(builder) { |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 70 | auto desc = [MTLTextureDescriptor new]; |
| 71 | [desc autorelease]; |
Corentin Wallez | 8308b1c | 2017-07-03 23:02:49 -0400 | [diff] [blame] | 72 | desc.textureType = MetalTextureType(GetDimension()); |
Corentin Wallez | d8c068f | 2018-07-09 15:15:07 +0200 | [diff] [blame] | 73 | desc.usage = MetalTextureUsage(GetAllowedUsage()); |
Corentin Wallez | 8308b1c | 2017-07-03 23:02:49 -0400 | [diff] [blame] | 74 | desc.pixelFormat = MetalPixelFormat(GetFormat()); |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 75 | desc.width = GetWidth(); |
| 76 | desc.height = GetHeight(); |
| 77 | desc.depth = GetDepth(); |
| 78 | desc.mipmapLevelCount = GetNumMipLevels(); |
| 79 | desc.arrayLength = 1; |
Kai Ninomiya | fec8c58 | 2017-07-17 12:03:16 -0400 | [diff] [blame] | 80 | desc.storageMode = MTLStorageModePrivate; |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 81 | |
| 82 | auto mtlDevice = ToBackend(builder->GetDevice())->GetMTLDevice(); |
Corentin Wallez | b0c75a5 | 2017-11-23 15:52:29 -0500 | [diff] [blame] | 83 | mMtlTexture = [mtlDevice newTextureWithDescriptor:desc]; |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 84 | } |
| 85 | |
Kai Ninomiya | 35bf424 | 2017-07-19 15:41:17 -0700 | [diff] [blame] | 86 | Texture::Texture(TextureBuilder* builder, id<MTLTexture> mtlTexture) |
Corentin Wallez | b0c75a5 | 2017-11-23 15:52:29 -0500 | [diff] [blame] | 87 | : TextureBase(builder), mMtlTexture(mtlTexture) { |
| 88 | [mMtlTexture retain]; |
Kai Ninomiya | 35bf424 | 2017-07-19 15:41:17 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 91 | Texture::~Texture() { |
Corentin Wallez | b0c75a5 | 2017-11-23 15:52:29 -0500 | [diff] [blame] | 92 | [mMtlTexture release]; |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | id<MTLTexture> Texture::GetMTLTexture() { |
Corentin Wallez | b0c75a5 | 2017-11-23 15:52:29 -0500 | [diff] [blame] | 96 | return mMtlTexture; |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 97 | } |
| 98 | |
Corentin Wallez | f58d84d | 2017-11-24 14:12:44 -0500 | [diff] [blame] | 99 | TextureView::TextureView(TextureViewBuilder* builder) : TextureViewBase(builder) { |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 100 | } |
Corentin Wallez | f58d84d | 2017-11-24 14:12:44 -0500 | [diff] [blame] | 101 | |
| 102 | }} // namespace backend::metal |