axmol/core/renderer/backend/metal/DeviceInfoMTL.mm

459 lines
16 KiB
Plaintext
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
2022-04-25 19:15:46 +08:00
Copyright (c) 2020 C4games Ltd
2019-11-23 20:27:39 +08:00
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "DeviceInfoMTL.h"
#include "base/Macros.h"
#include "UtilsMTL.h"
NS_AX_BACKEND_BEGIN
2019-11-23 20:27:39 +08:00
namespace
{
int getMaxVertexAttributes(FeatureSet featureSet)
{
int maxAttributes = 0;
switch (featureSet)
2019-11-23 20:27:39 +08:00
{
case FeatureSet::FeatureSet_iOS_GPUFamily1_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily4_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v5:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v5:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily4_v2:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v1:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v2:
case FeatureSet::FeatureSet_macOS_ReadWriteTextureTier2:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v3:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v4:
case FeatureSet::FeatureSet_macOS_GPUFamily2_v1:
maxAttributes = 31;
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
return maxAttributes;
}
int getMaxTextureEntries(FeatureSet featureSet)
{
int maxTextureEntries = 0;
switch (featureSet)
2019-11-23 20:27:39 +08:00
{
case FeatureSet::FeatureSet_iOS_GPUFamily1_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily4_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v5:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v5:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily4_v2:
maxTextureEntries = 31;
break;
case FeatureSet::FeatureSet_macOS_GPUFamily1_v1:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v2:
case FeatureSet::FeatureSet_macOS_ReadWriteTextureTier2:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v3:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v4:
case FeatureSet::FeatureSet_macOS_GPUFamily2_v1:
maxTextureEntries = 128;
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
return maxTextureEntries;
}
int getMaxSamplerEntries(FeatureSet featureSet)
{
int maxSamplerEntries = 0;
switch (featureSet)
2019-11-23 20:27:39 +08:00
{
case FeatureSet::FeatureSet_iOS_GPUFamily1_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily4_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v5:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v5:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily4_v2:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v1:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v2:
case FeatureSet::FeatureSet_macOS_ReadWriteTextureTier2:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v3:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v4:
case FeatureSet::FeatureSet_macOS_GPUFamily2_v1:
maxSamplerEntries = 16;
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
return maxSamplerEntries;
}
int getMaxTextureWidthHeight(FeatureSet featureSet)
{
int maxTextureSize = 0;
switch (featureSet)
2019-11-23 20:27:39 +08:00
{
case FeatureSet::FeatureSet_iOS_GPUFamily1_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v1:
maxTextureSize = 4096;
break;
case FeatureSet::FeatureSet_iOS_GPUFamily1_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v5:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v5:
maxTextureSize = 8192;
break;
case FeatureSet::FeatureSet_iOS_GPUFamily3_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily4_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily4_v2:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v1:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v2:
case FeatureSet::FeatureSet_macOS_ReadWriteTextureTier2:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v3:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v4:
case FeatureSet::FeatureSet_macOS_GPUFamily2_v1:
maxTextureSize = 16384;
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
return maxTextureSize;
}
const char* featureSetToString(FeatureSet featureSet)
{
switch (featureSet)
2019-11-23 20:27:39 +08:00
{
case FeatureSet::FeatureSet_iOS_GPUFamily1_v1:
return "iOS_GPUFamily1_v1";
case FeatureSet::FeatureSet_iOS_GPUFamily2_v1:
return "iOS_GPUFamily2_v1";
case FeatureSet::FeatureSet_iOS_GPUFamily1_v2:
return "iOS_GPUFamily1_v2";
case FeatureSet::FeatureSet_iOS_GPUFamily2_v2:
return "iOS_GPUFamily2_v2";
case FeatureSet::FeatureSet_iOS_GPUFamily1_v3:
return "iOS_GPUFamily1_v3";
case FeatureSet::FeatureSet_iOS_GPUFamily2_v3:
return "iOS_GPUFamily2_v3";
case FeatureSet::FeatureSet_iOS_GPUFamily1_v4:
return "iOS_GPUFamily1_v4";
case FeatureSet::FeatureSet_iOS_GPUFamily2_v4:
return "iOS_GPUFamily2_v4";
case FeatureSet::FeatureSet_iOS_GPUFamily1_v5:
return "iOS_GPUFamily1_v5";
case FeatureSet::FeatureSet_iOS_GPUFamily2_v5:
return "iOS_GPUFamily2_v5";
case FeatureSet::FeatureSet_iOS_GPUFamily3_v1:
return "iOS_GPUFamily3_v1";
case FeatureSet::FeatureSet_iOS_GPUFamily3_v2:
return "iOS_GPUFamily3_v2";
case FeatureSet::FeatureSet_iOS_GPUFamily3_v3:
return "iOS_GPUFamily3_v3";
case FeatureSet::FeatureSet_iOS_GPUFamily4_v1:
return "iOS_GPUFamily4_v1";
case FeatureSet::FeatureSet_iOS_GPUFamily3_v4:
return "iOS_GPUFamily3_v4";
case FeatureSet::FeatureSet_iOS_GPUFamily4_v2:
return "iOS_GPUFamily4_v2";
case FeatureSet::FeatureSet_macOS_GPUFamily1_v1:
return "macOS_GPUFamily1_v1";
case FeatureSet::FeatureSet_macOS_GPUFamily1_v2:
return "macOS_GPUFamily1_v2";
case FeatureSet::FeatureSet_macOS_ReadWriteTextureTier2:
return "macOS_ReadWriteTextureTier2";
case FeatureSet::FeatureSet_macOS_GPUFamily1_v3:
return "macOS_GPUFamily1_v3";
case FeatureSet::FeatureSet_macOS_GPUFamily1_v4:
return "macOS_GPUFamily1_v4";
case FeatureSet::FeatureSet_macOS_GPUFamily2_v1:
return "macOS_GPUFamily2_v1";
default:
break;
2019-11-23 20:27:39 +08:00
}
return "";
}
bool supportPVRTC(FeatureSet featureSet)
{
switch (featureSet)
2019-11-23 20:27:39 +08:00
{
case FeatureSet::FeatureSet_iOS_GPUFamily1_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v5:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v5:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily4_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily4_v2:
return true;
case FeatureSet::FeatureSet_macOS_GPUFamily1_v1:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v2:
case FeatureSet::FeatureSet_macOS_ReadWriteTextureTier2:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v3:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v4:
case FeatureSet::FeatureSet_macOS_GPUFamily2_v1:
2019-11-23 20:27:39 +08:00
return false;
default:
break;
2019-11-23 20:27:39 +08:00
}
return false;
}
bool supportEACETC(FeatureSet featureSet)
{
switch (featureSet)
2019-11-23 20:27:39 +08:00
{
case FeatureSet::FeatureSet_iOS_GPUFamily1_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v5:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v5:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily4_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily4_v2:
return true;
case FeatureSet::FeatureSet_macOS_GPUFamily1_v1:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v2:
case FeatureSet::FeatureSet_macOS_ReadWriteTextureTier2:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v3:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v4:
case FeatureSet::FeatureSet_macOS_GPUFamily2_v1:
2019-11-23 20:27:39 +08:00
return false;
default:
break;
2019-11-23 20:27:39 +08:00
}
return false;
}
bool supportASTC(FeatureSet featureSet)
{
switch (featureSet)
2019-11-23 20:27:39 +08:00
{
case FeatureSet::FeatureSet_iOS_GPUFamily1_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v5:
return false;
case FeatureSet::FeatureSet_iOS_GPUFamily2_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v5:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily4_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily4_v2:
return true;
case FeatureSet::FeatureSet_macOS_GPUFamily1_v1:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v2:
case FeatureSet::FeatureSet_macOS_ReadWriteTextureTier2:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v3:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v4:
case FeatureSet::FeatureSet_macOS_GPUFamily2_v1:
2019-11-23 20:27:39 +08:00
return false;
default:
break;
2019-11-23 20:27:39 +08:00
}
return false;
}
bool supportS3TC(FeatureSet featureSet)
{
switch (featureSet)
2019-11-23 20:27:39 +08:00
{
case FeatureSet::FeatureSet_iOS_GPUFamily1_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily1_v5:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily2_v5:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v2:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v3:
case FeatureSet::FeatureSet_iOS_GPUFamily4_v1:
case FeatureSet::FeatureSet_iOS_GPUFamily3_v4:
case FeatureSet::FeatureSet_iOS_GPUFamily4_v2:
2019-11-23 20:27:39 +08:00
return false;
case FeatureSet::FeatureSet_macOS_GPUFamily1_v1:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v2:
case FeatureSet::FeatureSet_macOS_ReadWriteTextureTier2:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v3:
case FeatureSet::FeatureSet_macOS_GPUFamily1_v4:
case FeatureSet::FeatureSet_macOS_GPUFamily2_v1:
return true;
default:
break;
2019-11-23 20:27:39 +08:00
}
return false;
}
2019-11-23 20:27:39 +08:00
}
bool DeviceInfoMTL::_isDepth24Stencil8PixelFormatSupported = false;
2019-11-23 20:27:39 +08:00
DeviceInfoMTL::DeviceInfoMTL(id<MTLDevice> device)
{
_deviceName = [device.name UTF8String];
2022-07-16 10:43:05 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_IOS)
2019-11-23 20:27:39 +08:00
const FeatureSet minRequiredFeatureSet = FeatureSet::FeatureSet_iOS_GPUFamily1_v1;
const FeatureSet maxKnownFeatureSet = FeatureSet::FeatureSet_iOS_GPUFamily4_v2;
2019-11-23 20:27:39 +08:00
#else
const FeatureSet minRequiredFeatureSet = FeatureSet::FeatureSet_macOS_GPUFamily1_v1;
const FeatureSet maxKnownFeatureSet = FeatureSet::FeatureSet_macOS_GPUFamily2_v1;
2019-11-23 20:27:39 +08:00
_isDepth24Stencil8PixelFormatSupported = [device isDepth24Stencil8PixelFormatSupported];
#endif
2019-11-23 20:27:39 +08:00
for (auto featureSet = maxKnownFeatureSet; featureSet >= minRequiredFeatureSet; --featureSet)
{
if ([device supportsFeatureSet:MTLFeatureSet(featureSet)])
{
_featureSet = featureSet;
break;
}
}
UtilsMTL::initGPUTextureFormats();
2019-11-23 20:27:39 +08:00
}
bool DeviceInfoMTL::init()
{
_maxAttributes = getMaxVertexAttributes(_featureSet);
2019-11-23 20:27:39 +08:00
_maxSamplesAllowed = getMaxSamplerEntries(_featureSet);
_maxTextureUnits = getMaxTextureEntries(_featureSet);
_maxTextureSize = getMaxTextureWidthHeight(_featureSet);
2019-11-23 20:27:39 +08:00
return true;
}
const char* DeviceInfoMTL::getVendor() const
{
return "";
}
const char* DeviceInfoMTL::getRenderer() const
{
return _deviceName.c_str();
}
const char* DeviceInfoMTL::getVersion() const
{
return featureSetToString(_featureSet);
}
const char* DeviceInfoMTL::getExtension() const
{
return "";
}
bool DeviceInfoMTL::checkForFeatureSupported(FeatureType feature)
{
bool featureSupported = false;
switch (feature)
{
case FeatureType::PVRTC:
featureSupported = supportPVRTC(_featureSet);
break;
case FeatureType::ETC1:
2020-09-01 00:03:49 +08:00
case FeatureType::ETC2:
2019-11-23 20:27:39 +08:00
featureSupported = supportEACETC(_featureSet);
break;
case FeatureType::S3TC:
featureSupported = supportS3TC(_featureSet);
break;
case FeatureType::IMG_FORMAT_BGRA8888:
featureSupported = true;
break;
case FeatureType::PACKED_DEPTH_STENCIL:
featureSupported = _isDepth24Stencil8PixelFormatSupported;
break;
case FeatureType::ASTC:
featureSupported = supportASTC(_featureSet);
break;
default:
break;
}
return featureSupported;
}
NS_AX_BACKEND_END