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 | d37523f | 2018-07-24 13:53:51 +0200 | [diff] [blame] | 15 | #include "dawn_native/metal/TextureMTL.h" |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 16 | |
Corentin Wallez | b495e48 | 2019-09-18 04:32:52 +0000 | [diff] [blame] | 17 | #include "common/Platform.h" |
Corentin Wallez | d37523f | 2018-07-24 13:53:51 +0200 | [diff] [blame] | 18 | #include "dawn_native/metal/DeviceMTL.h" |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 19 | |
Corentin Wallez | 49a65d0 | 2018-07-24 16:45:45 +0200 | [diff] [blame] | 20 | namespace dawn_native { namespace metal { |
Corentin Wallez | 8308b1c | 2017-07-03 23:02:49 -0400 | [diff] [blame] | 21 | |
Austin Eng | 476e5cb | 2017-08-03 12:17:14 -0400 | [diff] [blame] | 22 | namespace { |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 23 | bool UsageNeedsTextureView(wgpu::TextureUsage usage) { |
| 24 | constexpr wgpu::TextureUsage kUsageNeedsTextureView = |
| 25 | wgpu::TextureUsage::Storage | wgpu::TextureUsage::Sampled; |
Jiawei Shao | 5dee56f | 2018-12-29 10:47:28 +0000 | [diff] [blame] | 26 | return usage & kUsageNeedsTextureView; |
| 27 | } |
| 28 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 29 | MTLTextureUsage MetalTextureUsage(wgpu::TextureUsage usage) { |
Corentin Wallez | f58d84d | 2017-11-24 14:12:44 -0500 | [diff] [blame] | 30 | MTLTextureUsage result = MTLTextureUsageUnknown; // This is 0 |
Corentin Wallez | 8308b1c | 2017-07-03 23:02:49 -0400 | [diff] [blame] | 31 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 32 | if (usage & (wgpu::TextureUsage::Storage)) { |
Corentin Wallez | 8308b1c | 2017-07-03 23:02:49 -0400 | [diff] [blame] | 33 | result |= MTLTextureUsageShaderWrite | MTLTextureUsageShaderRead; |
| 34 | } |
| 35 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 36 | if (usage & (wgpu::TextureUsage::Sampled)) { |
Corentin Wallez | 8308b1c | 2017-07-03 23:02:49 -0400 | [diff] [blame] | 37 | result |= MTLTextureUsageShaderRead; |
| 38 | } |
| 39 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 40 | if (usage & (wgpu::TextureUsage::OutputAttachment)) { |
Corentin Wallez | 8308b1c | 2017-07-03 23:02:49 -0400 | [diff] [blame] | 41 | result |= MTLTextureUsageRenderTarget; |
| 42 | } |
| 43 | |
Jiawei Shao | 5dee56f | 2018-12-29 10:47:28 +0000 | [diff] [blame] | 44 | if (UsageNeedsTextureView(usage)) { |
| 45 | result |= MTLTextureUsagePixelFormatView; |
| 46 | } |
Jiawei Shao | e8d12b4 | 2018-10-26 06:29:38 +0000 | [diff] [blame] | 47 | |
Corentin Wallez | 8308b1c | 2017-07-03 23:02:49 -0400 | [diff] [blame] | 48 | return result; |
| 49 | } |
| 50 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 51 | MTLTextureType MetalTextureType(wgpu::TextureDimension dimension, |
Jiawei Shao | 865cad8 | 2019-04-09 08:04:59 +0000 | [diff] [blame] | 52 | unsigned int arrayLayers, |
| 53 | unsigned int sampleCount) { |
Corentin Wallez | 8308b1c | 2017-07-03 23:02:49 -0400 | [diff] [blame] | 54 | switch (dimension) { |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 55 | case wgpu::TextureDimension::e2D: |
Jiawei Shao | 865cad8 | 2019-04-09 08:04:59 +0000 | [diff] [blame] | 56 | if (sampleCount > 1) { |
| 57 | ASSERT(arrayLayers == 1); |
| 58 | return MTLTextureType2DMultisample; |
| 59 | } else { |
| 60 | return (arrayLayers > 1) ? MTLTextureType2DArray : MTLTextureType2D; |
| 61 | } |
Corentin Wallez | f07e85c | 2019-07-15 20:47:56 +0000 | [diff] [blame] | 62 | default: |
| 63 | UNREACHABLE(); |
Corentin Wallez | 8308b1c | 2017-07-03 23:02:49 -0400 | [diff] [blame] | 64 | } |
| 65 | } |
Jiawei Shao | e8d12b4 | 2018-10-26 06:29:38 +0000 | [diff] [blame] | 66 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 67 | MTLTextureType MetalTextureViewType(wgpu::TextureViewDimension dimension, |
Jiawei Shao | 865cad8 | 2019-04-09 08:04:59 +0000 | [diff] [blame] | 68 | unsigned int sampleCount) { |
Jiawei Shao | e8d12b4 | 2018-10-26 06:29:38 +0000 | [diff] [blame] | 69 | switch (dimension) { |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 70 | case wgpu::TextureViewDimension::e2D: |
Jiawei Shao | 865cad8 | 2019-04-09 08:04:59 +0000 | [diff] [blame] | 71 | return (sampleCount > 1) ? MTLTextureType2DMultisample : MTLTextureType2D; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 72 | case wgpu::TextureViewDimension::e2DArray: |
Jiawei Shao | e8d12b4 | 2018-10-26 06:29:38 +0000 | [diff] [blame] | 73 | return MTLTextureType2DArray; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 74 | case wgpu::TextureViewDimension::Cube: |
Jiawei Shao | 5ab84ec | 2018-11-16 12:11:20 +0000 | [diff] [blame] | 75 | return MTLTextureTypeCube; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 76 | case wgpu::TextureViewDimension::CubeArray: |
Jiawei Shao | 5ab84ec | 2018-11-16 12:11:20 +0000 | [diff] [blame] | 77 | return MTLTextureTypeCubeArray; |
Jiawei Shao | e8d12b4 | 2018-10-26 06:29:38 +0000 | [diff] [blame] | 78 | default: |
| 79 | UNREACHABLE(); |
| 80 | return MTLTextureType2D; |
| 81 | } |
| 82 | } |
Jiawei Shao | 5dee56f | 2018-12-29 10:47:28 +0000 | [diff] [blame] | 83 | |
| 84 | bool RequiresCreatingNewTextureView(const TextureBase* texture, |
| 85 | const TextureViewDescriptor* textureViewDescriptor) { |
Corentin Wallez | a92f83b | 2019-06-21 10:16:15 +0000 | [diff] [blame] | 86 | if (texture->GetFormat().format != textureViewDescriptor->format) { |
Jiawei Shao | 5dee56f | 2018-12-29 10:47:28 +0000 | [diff] [blame] | 87 | return true; |
| 88 | } |
| 89 | |
Jiawei Shao | 2030166 | 2019-02-21 00:45:19 +0000 | [diff] [blame] | 90 | if (texture->GetArrayLayers() != textureViewDescriptor->arrayLayerCount) { |
Jiawei Shao | 5dee56f | 2018-12-29 10:47:28 +0000 | [diff] [blame] | 91 | return true; |
| 92 | } |
| 93 | |
Jiawei Shao | 2030166 | 2019-02-21 00:45:19 +0000 | [diff] [blame] | 94 | if (texture->GetNumMipLevels() != textureViewDescriptor->mipLevelCount) { |
Jiawei Shao | 5dee56f | 2018-12-29 10:47:28 +0000 | [diff] [blame] | 95 | return true; |
| 96 | } |
| 97 | |
| 98 | switch (textureViewDescriptor->dimension) { |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 99 | case wgpu::TextureViewDimension::Cube: |
| 100 | case wgpu::TextureViewDimension::CubeArray: |
Jiawei Shao | 5dee56f | 2018-12-29 10:47:28 +0000 | [diff] [blame] | 101 | return true; |
| 102 | default: |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | return false; |
| 107 | } |
Corentin Wallez | 0cdf9e0 | 2019-03-01 12:01:18 +0000 | [diff] [blame] | 108 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 109 | ResultOrError<wgpu::TextureFormat> GetFormatEquivalentToIOSurfaceFormat(uint32_t format) { |
Corentin Wallez | 0cdf9e0 | 2019-03-01 12:01:18 +0000 | [diff] [blame] | 110 | switch (format) { |
Corentin Wallez | 56f3a7b | 2019-08-01 17:58:05 +0000 | [diff] [blame] | 111 | case 'RGBA': |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 112 | return wgpu::TextureFormat::RGBA8Unorm; |
Corentin Wallez | 0cdf9e0 | 2019-03-01 12:01:18 +0000 | [diff] [blame] | 113 | case 'BGRA': |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 114 | return wgpu::TextureFormat::BGRA8Unorm; |
Corentin Wallez | 0cdf9e0 | 2019-03-01 12:01:18 +0000 | [diff] [blame] | 115 | case '2C08': |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 116 | return wgpu::TextureFormat::RG8Unorm; |
Corentin Wallez | 0cdf9e0 | 2019-03-01 12:01:18 +0000 | [diff] [blame] | 117 | case 'L008': |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 118 | return wgpu::TextureFormat::R8Unorm; |
Corentin Wallez | 0cdf9e0 | 2019-03-01 12:01:18 +0000 | [diff] [blame] | 119 | default: |
| 120 | return DAWN_VALIDATION_ERROR("Unsupported IOSurface format"); |
| 121 | } |
| 122 | } |
Corentin Wallez | b495e48 | 2019-09-18 04:32:52 +0000 | [diff] [blame] | 123 | |
| 124 | #if defined(DAWN_PLATFORM_MACOS) |
| 125 | MTLStorageMode kIOSurfaceStorageMode = MTLStorageModeManaged; |
| 126 | #elif defined(DAWN_PLATFORM_IOS) |
| 127 | MTLStorageMode kIOSurfaceStorageMode = MTLStorageModePrivate; |
| 128 | #else |
| 129 | # error "Unsupported Apple platform." |
| 130 | #endif |
Corentin Wallez | 0cdf9e0 | 2019-03-01 12:01:18 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 133 | MTLPixelFormat MetalPixelFormat(wgpu::TextureFormat format) { |
Corentin Wallez | 0cdf9e0 | 2019-03-01 12:01:18 +0000 | [diff] [blame] | 134 | switch (format) { |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 135 | case wgpu::TextureFormat::R8Unorm: |
Corentin Wallez | 0cdf9e0 | 2019-03-01 12:01:18 +0000 | [diff] [blame] | 136 | return MTLPixelFormatR8Unorm; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 137 | case wgpu::TextureFormat::R8Snorm: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 138 | return MTLPixelFormatR8Snorm; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 139 | case wgpu::TextureFormat::R8Uint: |
Corentin Wallez | 0cdf9e0 | 2019-03-01 12:01:18 +0000 | [diff] [blame] | 140 | return MTLPixelFormatR8Uint; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 141 | case wgpu::TextureFormat::R8Sint: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 142 | return MTLPixelFormatR8Sint; |
| 143 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 144 | case wgpu::TextureFormat::R16Uint: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 145 | return MTLPixelFormatR16Uint; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 146 | case wgpu::TextureFormat::R16Sint: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 147 | return MTLPixelFormatR16Sint; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 148 | case wgpu::TextureFormat::R16Float: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 149 | return MTLPixelFormatR16Float; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 150 | case wgpu::TextureFormat::RG8Unorm: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 151 | return MTLPixelFormatRG8Unorm; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 152 | case wgpu::TextureFormat::RG8Snorm: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 153 | return MTLPixelFormatRG8Snorm; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 154 | case wgpu::TextureFormat::RG8Uint: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 155 | return MTLPixelFormatRG8Uint; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 156 | case wgpu::TextureFormat::RG8Sint: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 157 | return MTLPixelFormatRG8Sint; |
| 158 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 159 | case wgpu::TextureFormat::R32Uint: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 160 | return MTLPixelFormatR32Uint; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 161 | case wgpu::TextureFormat::R32Sint: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 162 | return MTLPixelFormatR32Sint; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 163 | case wgpu::TextureFormat::R32Float: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 164 | return MTLPixelFormatR32Float; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 165 | case wgpu::TextureFormat::RG16Uint: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 166 | return MTLPixelFormatRG16Uint; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 167 | case wgpu::TextureFormat::RG16Sint: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 168 | return MTLPixelFormatRG16Sint; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 169 | case wgpu::TextureFormat::RG16Float: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 170 | return MTLPixelFormatRG16Float; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 171 | case wgpu::TextureFormat::RGBA8Unorm: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 172 | return MTLPixelFormatRGBA8Unorm; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 173 | case wgpu::TextureFormat::RGBA8UnormSrgb: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 174 | return MTLPixelFormatRGBA8Unorm_sRGB; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 175 | case wgpu::TextureFormat::RGBA8Snorm: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 176 | return MTLPixelFormatRGBA8Snorm; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 177 | case wgpu::TextureFormat::RGBA8Uint: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 178 | return MTLPixelFormatRGBA8Uint; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 179 | case wgpu::TextureFormat::RGBA8Sint: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 180 | return MTLPixelFormatRGBA8Sint; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 181 | case wgpu::TextureFormat::BGRA8Unorm: |
Corentin Wallez | 0cdf9e0 | 2019-03-01 12:01:18 +0000 | [diff] [blame] | 182 | return MTLPixelFormatBGRA8Unorm; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 183 | case wgpu::TextureFormat::BGRA8UnormSrgb: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 184 | return MTLPixelFormatBGRA8Unorm_sRGB; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 185 | case wgpu::TextureFormat::RGB10A2Unorm: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 186 | return MTLPixelFormatRGB10A2Unorm; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 187 | case wgpu::TextureFormat::RG11B10Float: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 188 | return MTLPixelFormatRG11B10Float; |
| 189 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 190 | case wgpu::TextureFormat::RG32Uint: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 191 | return MTLPixelFormatRG32Uint; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 192 | case wgpu::TextureFormat::RG32Sint: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 193 | return MTLPixelFormatRG32Sint; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 194 | case wgpu::TextureFormat::RG32Float: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 195 | return MTLPixelFormatRG32Float; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 196 | case wgpu::TextureFormat::RGBA16Uint: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 197 | return MTLPixelFormatRGBA16Uint; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 198 | case wgpu::TextureFormat::RGBA16Sint: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 199 | return MTLPixelFormatRGBA16Sint; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 200 | case wgpu::TextureFormat::RGBA16Float: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 201 | return MTLPixelFormatRGBA16Float; |
| 202 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 203 | case wgpu::TextureFormat::RGBA32Uint: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 204 | return MTLPixelFormatRGBA32Uint; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 205 | case wgpu::TextureFormat::RGBA32Sint: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 206 | return MTLPixelFormatRGBA32Sint; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 207 | case wgpu::TextureFormat::RGBA32Float: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 208 | return MTLPixelFormatRGBA32Float; |
| 209 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 210 | case wgpu::TextureFormat::Depth32Float: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 211 | return MTLPixelFormatDepth32Float; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 212 | case wgpu::TextureFormat::Depth24Plus: |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 213 | return MTLPixelFormatDepth32Float; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 214 | case wgpu::TextureFormat::Depth24PlusStencil8: |
Corentin Wallez | 0cdf9e0 | 2019-03-01 12:01:18 +0000 | [diff] [blame] | 215 | return MTLPixelFormatDepth32Float_Stencil8; |
Corentin Wallez | 7843076 | 2019-07-08 09:28:51 +0000 | [diff] [blame] | 216 | |
Corentin Wallez | b629c50 | 2019-10-08 07:44:21 +0000 | [diff] [blame] | 217 | #if defined(DAWN_PLATFORM_MACOS) |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 218 | case wgpu::TextureFormat::BC1RGBAUnorm: |
Jiawei Shao | 5580970 | 2019-07-17 00:00:10 +0000 | [diff] [blame] | 219 | return MTLPixelFormatBC1_RGBA; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 220 | case wgpu::TextureFormat::BC1RGBAUnormSrgb: |
Jiawei Shao | 5580970 | 2019-07-17 00:00:10 +0000 | [diff] [blame] | 221 | return MTLPixelFormatBC1_RGBA_sRGB; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 222 | case wgpu::TextureFormat::BC2RGBAUnorm: |
Jiawei Shao | 5580970 | 2019-07-17 00:00:10 +0000 | [diff] [blame] | 223 | return MTLPixelFormatBC2_RGBA; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 224 | case wgpu::TextureFormat::BC2RGBAUnormSrgb: |
Jiawei Shao | 5580970 | 2019-07-17 00:00:10 +0000 | [diff] [blame] | 225 | return MTLPixelFormatBC2_RGBA_sRGB; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 226 | case wgpu::TextureFormat::BC3RGBAUnorm: |
Jiawei Shao | 5580970 | 2019-07-17 00:00:10 +0000 | [diff] [blame] | 227 | return MTLPixelFormatBC3_RGBA; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 228 | case wgpu::TextureFormat::BC3RGBAUnormSrgb: |
Jiawei Shao | 5580970 | 2019-07-17 00:00:10 +0000 | [diff] [blame] | 229 | return MTLPixelFormatBC3_RGBA_sRGB; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 230 | case wgpu::TextureFormat::BC4RSnorm: |
Jiawei Shao | 5580970 | 2019-07-17 00:00:10 +0000 | [diff] [blame] | 231 | return MTLPixelFormatBC4_RSnorm; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 232 | case wgpu::TextureFormat::BC4RUnorm: |
Jiawei Shao | 5580970 | 2019-07-17 00:00:10 +0000 | [diff] [blame] | 233 | return MTLPixelFormatBC4_RUnorm; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 234 | case wgpu::TextureFormat::BC5RGSnorm: |
Jiawei Shao | ea2d558 | 2019-07-10 23:58:13 +0000 | [diff] [blame] | 235 | return MTLPixelFormatBC5_RGSnorm; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 236 | case wgpu::TextureFormat::BC5RGUnorm: |
Jiawei Shao | ea2d558 | 2019-07-10 23:58:13 +0000 | [diff] [blame] | 237 | return MTLPixelFormatBC5_RGUnorm; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 238 | case wgpu::TextureFormat::BC6HRGBSfloat: |
Jiawei Shao | 5580970 | 2019-07-17 00:00:10 +0000 | [diff] [blame] | 239 | return MTLPixelFormatBC6H_RGBFloat; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 240 | case wgpu::TextureFormat::BC6HRGBUfloat: |
Jiawei Shao | 5580970 | 2019-07-17 00:00:10 +0000 | [diff] [blame] | 241 | return MTLPixelFormatBC6H_RGBUfloat; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 242 | case wgpu::TextureFormat::BC7RGBAUnorm: |
Jiawei Shao | 5580970 | 2019-07-17 00:00:10 +0000 | [diff] [blame] | 243 | return MTLPixelFormatBC7_RGBAUnorm; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 244 | case wgpu::TextureFormat::BC7RGBAUnormSrgb: |
Jiawei Shao | 5580970 | 2019-07-17 00:00:10 +0000 | [diff] [blame] | 245 | return MTLPixelFormatBC7_RGBAUnorm_sRGB; |
Corentin Wallez | b629c50 | 2019-10-08 07:44:21 +0000 | [diff] [blame] | 246 | #endif |
Jiawei Shao | ea2d558 | 2019-07-10 23:58:13 +0000 | [diff] [blame] | 247 | |
Jiawei Shao | c2750ab | 2019-06-01 02:30:51 +0000 | [diff] [blame] | 248 | default: |
| 249 | UNREACHABLE(); |
Corentin Wallez | 0cdf9e0 | 2019-03-01 12:01:18 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | MaybeError ValidateIOSurfaceCanBeWrapped(const DeviceBase*, |
| 254 | const TextureDescriptor* descriptor, |
| 255 | IOSurfaceRef ioSurface, |
| 256 | uint32_t plane) { |
| 257 | // IOSurfaceGetPlaneCount can return 0 for non-planar IOSurfaces but we will treat |
| 258 | // non-planar like it is a single plane. |
| 259 | size_t surfacePlaneCount = std::max(size_t(1), IOSurfaceGetPlaneCount(ioSurface)); |
| 260 | if (plane >= surfacePlaneCount) { |
| 261 | return DAWN_VALIDATION_ERROR("IOSurface plane doesn't exist"); |
| 262 | } |
| 263 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 264 | if (descriptor->dimension != wgpu::TextureDimension::e2D) { |
Corentin Wallez | 0cdf9e0 | 2019-03-01 12:01:18 +0000 | [diff] [blame] | 265 | return DAWN_VALIDATION_ERROR("IOSurface texture must be 2D"); |
| 266 | } |
| 267 | |
| 268 | if (descriptor->mipLevelCount != 1) { |
| 269 | return DAWN_VALIDATION_ERROR("IOSurface mip level count must be 1"); |
| 270 | } |
| 271 | |
| 272 | if (descriptor->arrayLayerCount != 1) { |
| 273 | return DAWN_VALIDATION_ERROR("IOSurface array layer count must be 1"); |
| 274 | } |
| 275 | |
| 276 | if (descriptor->sampleCount != 1) { |
| 277 | return DAWN_VALIDATION_ERROR("IOSurface sample count must be 1"); |
| 278 | } |
| 279 | |
| 280 | if (descriptor->size.width != IOSurfaceGetWidthOfPlane(ioSurface, plane) || |
| 281 | descriptor->size.height != IOSurfaceGetHeightOfPlane(ioSurface, plane) || |
| 282 | descriptor->size.depth != 1) { |
| 283 | return DAWN_VALIDATION_ERROR("IOSurface size doesn't match descriptor"); |
| 284 | } |
| 285 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame^] | 286 | wgpu::TextureFormat ioSurfaceFormat; |
Corentin Wallez | 0cdf9e0 | 2019-03-01 12:01:18 +0000 | [diff] [blame] | 287 | DAWN_TRY_ASSIGN(ioSurfaceFormat, |
| 288 | GetFormatEquivalentToIOSurfaceFormat(IOSurfaceGetPixelFormat(ioSurface))); |
| 289 | if (descriptor->format != ioSurfaceFormat) { |
| 290 | return DAWN_VALIDATION_ERROR("IOSurface format doesn't match descriptor"); |
| 291 | } |
| 292 | |
| 293 | return {}; |
| 294 | } |
| 295 | |
| 296 | MTLTextureDescriptor* CreateMetalTextureDescriptor(const TextureDescriptor* descriptor) { |
| 297 | MTLTextureDescriptor* mtlDesc = [MTLTextureDescriptor new]; |
Jiawei Shao | 865cad8 | 2019-04-09 08:04:59 +0000 | [diff] [blame] | 298 | mtlDesc.textureType = MetalTextureType(descriptor->dimension, descriptor->arrayLayerCount, |
| 299 | descriptor->sampleCount); |
Corentin Wallez | 0cdf9e0 | 2019-03-01 12:01:18 +0000 | [diff] [blame] | 300 | mtlDesc.usage = MetalTextureUsage(descriptor->usage); |
| 301 | mtlDesc.pixelFormat = MetalPixelFormat(descriptor->format); |
| 302 | |
| 303 | mtlDesc.width = descriptor->size.width; |
| 304 | mtlDesc.height = descriptor->size.height; |
| 305 | mtlDesc.depth = descriptor->size.depth; |
| 306 | |
| 307 | mtlDesc.mipmapLevelCount = descriptor->mipLevelCount; |
| 308 | mtlDesc.arrayLength = descriptor->arrayLayerCount; |
| 309 | mtlDesc.storageMode = MTLStorageModePrivate; |
| 310 | |
Jiawei Shao | 865cad8 | 2019-04-09 08:04:59 +0000 | [diff] [blame] | 311 | mtlDesc.sampleCount = descriptor->sampleCount; |
| 312 | |
Corentin Wallez | 0cdf9e0 | 2019-03-01 12:01:18 +0000 | [diff] [blame] | 313 | return mtlDesc; |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 314 | } |
| 315 | |
Jiawei Shao | 425428f | 2018-08-27 08:44:48 +0800 | [diff] [blame] | 316 | Texture::Texture(Device* device, const TextureDescriptor* descriptor) |
Natasha Lee | cae68ff | 2019-03-27 22:04:10 +0000 | [diff] [blame] | 317 | : TextureBase(device, descriptor, TextureState::OwnedInternal) { |
Corentin Wallez | 0cdf9e0 | 2019-03-01 12:01:18 +0000 | [diff] [blame] | 318 | MTLTextureDescriptor* mtlDesc = CreateMetalTextureDescriptor(descriptor); |
| 319 | mMtlTexture = [device->GetMTLDevice() newTextureWithDescriptor:mtlDesc]; |
| 320 | [mtlDesc release]; |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 321 | } |
| 322 | |
Jiawei Shao | 425428f | 2018-08-27 08:44:48 +0800 | [diff] [blame] | 323 | Texture::Texture(Device* device, const TextureDescriptor* descriptor, id<MTLTexture> mtlTexture) |
Natasha Lee | cae68ff | 2019-03-27 22:04:10 +0000 | [diff] [blame] | 324 | : TextureBase(device, descriptor, TextureState::OwnedInternal), mMtlTexture(mtlTexture) { |
Corentin Wallez | b0c75a5 | 2017-11-23 15:52:29 -0500 | [diff] [blame] | 325 | [mMtlTexture retain]; |
Kai Ninomiya | 35bf424 | 2017-07-19 15:41:17 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Corentin Wallez | 0cdf9e0 | 2019-03-01 12:01:18 +0000 | [diff] [blame] | 328 | Texture::Texture(Device* device, |
| 329 | const TextureDescriptor* descriptor, |
| 330 | IOSurfaceRef ioSurface, |
| 331 | uint32_t plane) |
Natasha Lee | cae68ff | 2019-03-27 22:04:10 +0000 | [diff] [blame] | 332 | : TextureBase(device, descriptor, TextureState::OwnedInternal) { |
Corentin Wallez | 0cdf9e0 | 2019-03-01 12:01:18 +0000 | [diff] [blame] | 333 | MTLTextureDescriptor* mtlDesc = CreateMetalTextureDescriptor(descriptor); |
Corentin Wallez | b495e48 | 2019-09-18 04:32:52 +0000 | [diff] [blame] | 334 | mtlDesc.storageMode = kIOSurfaceStorageMode; |
Corentin Wallez | 0cdf9e0 | 2019-03-01 12:01:18 +0000 | [diff] [blame] | 335 | mMtlTexture = [device->GetMTLDevice() newTextureWithDescriptor:mtlDesc |
| 336 | iosurface:ioSurface |
| 337 | plane:plane]; |
| 338 | [mtlDesc release]; |
| 339 | } |
| 340 | |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 341 | Texture::~Texture() { |
Natasha Lee | 20b0c33 | 2019-04-01 19:49:04 +0000 | [diff] [blame] | 342 | DestroyInternal(); |
Natasha Lee | cae68ff | 2019-03-27 22:04:10 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void Texture::DestroyImpl() { |
Rafael Cintron | b2b2ef5 | 2019-10-18 00:37:42 +0000 | [diff] [blame] | 346 | if (GetTextureState() == TextureState::OwnedInternal) { |
| 347 | [mMtlTexture release]; |
| 348 | mMtlTexture = nil; |
| 349 | } |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | id<MTLTexture> Texture::GetMTLTexture() { |
Corentin Wallez | b0c75a5 | 2017-11-23 15:52:29 -0500 | [diff] [blame] | 353 | return mMtlTexture; |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 354 | } |
| 355 | |
Jiawei Shao | aef480b | 2018-10-18 06:00:09 +0000 | [diff] [blame] | 356 | TextureView::TextureView(TextureBase* texture, const TextureViewDescriptor* descriptor) |
| 357 | : TextureViewBase(texture, descriptor) { |
Jiawei Shao | e8d12b4 | 2018-10-26 06:29:38 +0000 | [diff] [blame] | 358 | id<MTLTexture> mtlTexture = ToBackend(texture)->GetMTLTexture(); |
Jiawei Shao | 5dee56f | 2018-12-29 10:47:28 +0000 | [diff] [blame] | 359 | |
| 360 | if (!UsageNeedsTextureView(texture->GetUsage())) { |
| 361 | mMtlTextureView = nil; |
| 362 | } else if (!RequiresCreatingNewTextureView(texture, descriptor)) { |
| 363 | mMtlTextureView = [mtlTexture retain]; |
| 364 | } else { |
| 365 | MTLPixelFormat format = MetalPixelFormat(descriptor->format); |
Jiawei Shao | 865cad8 | 2019-04-09 08:04:59 +0000 | [diff] [blame] | 366 | MTLTextureType textureViewType = |
| 367 | MetalTextureViewType(descriptor->dimension, texture->GetSampleCount()); |
Jiawei Shao | 2030166 | 2019-02-21 00:45:19 +0000 | [diff] [blame] | 368 | auto mipLevelRange = NSMakeRange(descriptor->baseMipLevel, descriptor->mipLevelCount); |
| 369 | auto arrayLayerRange = |
| 370 | NSMakeRange(descriptor->baseArrayLayer, descriptor->arrayLayerCount); |
Jiawei Shao | 5dee56f | 2018-12-29 10:47:28 +0000 | [diff] [blame] | 371 | |
| 372 | mMtlTextureView = [mtlTexture newTextureViewWithPixelFormat:format |
| 373 | textureType:textureViewType |
| 374 | levels:mipLevelRange |
| 375 | slices:arrayLayerRange]; |
| 376 | } |
Corentin Wallez | 0ba5550 | 2017-06-14 15:46:59 -0400 | [diff] [blame] | 377 | } |
Corentin Wallez | f58d84d | 2017-11-24 14:12:44 -0500 | [diff] [blame] | 378 | |
Jiawei Shao | e8d12b4 | 2018-10-26 06:29:38 +0000 | [diff] [blame] | 379 | TextureView::~TextureView() { |
| 380 | [mMtlTextureView release]; |
| 381 | } |
| 382 | |
| 383 | id<MTLTexture> TextureView::GetMTLTexture() { |
Jiawei Shao | 5dee56f | 2018-12-29 10:47:28 +0000 | [diff] [blame] | 384 | ASSERT(mMtlTextureView != nil); |
Jiawei Shao | e8d12b4 | 2018-10-26 06:29:38 +0000 | [diff] [blame] | 385 | return mMtlTextureView; |
| 386 | } |
Corentin Wallez | 49a65d0 | 2018-07-24 16:45:45 +0200 | [diff] [blame] | 387 | }} // namespace dawn_native::metal |