blob: feae33f1d0fe525d728585ecf7375371c538793a [file] [log] [blame]
Corentin Wallez4a9ef4e2018-07-18 11:40:26 +02001// Copyright 2017 The Dawn Authors
Corentin Wallez0ba55502017-06-14 15:46:59 -04002//
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 Wallezfffe6df2017-07-06 14:41:13 -040015#include "backend/metal/TextureMTL.h"
Corentin Wallez0ba55502017-06-14 15:46:59 -040016
Corentin Wallez0f2e7b62018-06-18 18:19:31 -040017#include "backend/metal/DeviceMTL.h"
Corentin Wallez0ba55502017-06-14 15:46:59 -040018
Corentin Wallezf58d84d2017-11-24 14:12:44 -050019namespace backend { namespace metal {
Corentin Wallez0ba55502017-06-14 15:46:59 -040020
Austin Eng476e5cb2017-08-03 12:17:14 -040021 MTLPixelFormat MetalPixelFormat(nxt::TextureFormat format) {
22 switch (format) {
23 case nxt::TextureFormat::R8G8B8A8Unorm:
24 return MTLPixelFormatRGBA8Unorm;
Stephen Whitecd4f8a22018-01-22 14:09:08 -050025 case nxt::TextureFormat::R8G8Unorm:
26 return MTLPixelFormatRG8Unorm;
27 case nxt::TextureFormat::R8Unorm:
28 return MTLPixelFormatR8Unorm;
Austin Engfb19c362017-08-14 12:49:41 -040029 case nxt::TextureFormat::R8G8B8A8Uint:
30 return MTLPixelFormatRGBA8Uint;
Stephen Whitecd4f8a22018-01-22 14:09:08 -050031 case nxt::TextureFormat::R8G8Uint:
32 return MTLPixelFormatRG8Uint;
33 case nxt::TextureFormat::R8Uint:
34 return MTLPixelFormatR8Uint;
Corentin Walleze862a332017-09-21 13:12:49 -040035 case nxt::TextureFormat::B8G8R8A8Unorm:
36 return MTLPixelFormatBGRA8Unorm;
Austin Eng476e5cb2017-08-03 12:17:14 -040037 case nxt::TextureFormat::D32FloatS8Uint:
38 return MTLPixelFormatDepth32Float_Stencil8;
Corentin Wallez0ba55502017-06-14 15:46:59 -040039 }
Austin Eng476e5cb2017-08-03 12:17:14 -040040 }
Corentin Wallez8308b1c2017-07-03 23:02:49 -040041
Austin Eng476e5cb2017-08-03 12:17:14 -040042 namespace {
Corentin Wallez8308b1c2017-07-03 23:02:49 -040043 MTLTextureUsage MetalTextureUsage(nxt::TextureUsageBit usage) {
Corentin Wallezf58d84d2017-11-24 14:12:44 -050044 MTLTextureUsage result = MTLTextureUsageUnknown; // This is 0
Corentin Wallez8308b1c2017-07-03 23:02:49 -040045
46 if (usage & (nxt::TextureUsageBit::Storage)) {
47 result |= MTLTextureUsageShaderWrite | MTLTextureUsageShaderRead;
48 }
49
50 if (usage & (nxt::TextureUsageBit::Sampled)) {
51 result |= MTLTextureUsageShaderRead;
52 }
53
Kai Ninomiyacb2d6d82017-07-07 16:06:14 -070054 if (usage & (nxt::TextureUsageBit::OutputAttachment)) {
Corentin Wallez8308b1c2017-07-03 23:02:49 -040055 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 Wallez0ba55502017-06-14 15:46:59 -040067 }
68
Corentin Wallezf58d84d2017-11-24 14:12:44 -050069 Texture::Texture(TextureBuilder* builder) : TextureBase(builder) {
Corentin Wallez0ba55502017-06-14 15:46:59 -040070 auto desc = [MTLTextureDescriptor new];
71 [desc autorelease];
Corentin Wallez8308b1c2017-07-03 23:02:49 -040072 desc.textureType = MetalTextureType(GetDimension());
Corentin Wallezd8c068f2018-07-09 15:15:07 +020073 desc.usage = MetalTextureUsage(GetAllowedUsage());
Corentin Wallez8308b1c2017-07-03 23:02:49 -040074 desc.pixelFormat = MetalPixelFormat(GetFormat());
Corentin Wallez0ba55502017-06-14 15:46:59 -040075 desc.width = GetWidth();
76 desc.height = GetHeight();
77 desc.depth = GetDepth();
78 desc.mipmapLevelCount = GetNumMipLevels();
79 desc.arrayLength = 1;
Kai Ninomiyafec8c582017-07-17 12:03:16 -040080 desc.storageMode = MTLStorageModePrivate;
Corentin Wallez0ba55502017-06-14 15:46:59 -040081
82 auto mtlDevice = ToBackend(builder->GetDevice())->GetMTLDevice();
Corentin Wallezb0c75a52017-11-23 15:52:29 -050083 mMtlTexture = [mtlDevice newTextureWithDescriptor:desc];
Corentin Wallez0ba55502017-06-14 15:46:59 -040084 }
85
Kai Ninomiya35bf4242017-07-19 15:41:17 -070086 Texture::Texture(TextureBuilder* builder, id<MTLTexture> mtlTexture)
Corentin Wallezb0c75a52017-11-23 15:52:29 -050087 : TextureBase(builder), mMtlTexture(mtlTexture) {
88 [mMtlTexture retain];
Kai Ninomiya35bf4242017-07-19 15:41:17 -070089 }
90
Corentin Wallez0ba55502017-06-14 15:46:59 -040091 Texture::~Texture() {
Corentin Wallezb0c75a52017-11-23 15:52:29 -050092 [mMtlTexture release];
Corentin Wallez0ba55502017-06-14 15:46:59 -040093 }
94
95 id<MTLTexture> Texture::GetMTLTexture() {
Corentin Wallezb0c75a52017-11-23 15:52:29 -050096 return mMtlTexture;
Corentin Wallez0ba55502017-06-14 15:46:59 -040097 }
98
Corentin Wallezf58d84d2017-11-24 14:12:44 -050099 TextureView::TextureView(TextureViewBuilder* builder) : TextureViewBase(builder) {
Corentin Wallez0ba55502017-06-14 15:46:59 -0400100 }
Corentin Wallezf58d84d2017-11-24 14:12:44 -0500101
102}} // namespace backend::metal