Hash :
d33a2222
Author :
Date :
2021-04-26T16:56:15
Upstream Apple's direct-to-Metal backend: compile libANGLE. This change is meant to merge the metal backend changes from Apple's direct-to-Metal backend. Taken from Kyle Piddington's CL: https://chromium-review.googlesource.com/c/angle/angle/+/2857366/ The goal of this CL is to merge the metal backend code in a state that compiles, but not to switch the Metal backend over to using the direct-to-metal backend yet. Bug: angleproject:5505 Bug: angleproject:6127 Change-Id: If6783e06e0086b3a1dd25c6f53caca5cfc96cb86 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2950067 Reviewed-by: Jonah Ryan-Davis <jonahr@google.com> Reviewed-by: Kenneth Russell <kbr@chromium.org> Commit-Queue: Jonah Ryan-Davis <jonahr@google.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
//
// Copyright 2019 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// mtl_format_utils.mm:
// Implements Format conversion utilities classes that convert from angle formats
// to respective MTLPixelFormat and MTLVertexFormat.
//
#include "libANGLE/renderer/metal/mtl_format_utils.h"
#include "common/debug.h"
#include "libANGLE/renderer/Format.h"
#include "libANGLE/renderer/load_functions_table.h"
#include "libANGLE/renderer/metal/DisplayMtl.h"
namespace rx
{
namespace mtl
{
namespace
{
bool OverrideTextureCaps(const DisplayMtl *display, angle::FormatID formatId, gl::TextureCaps *caps)
{
// NOTE(hqle): Auto generate this.
switch (formatId)
{
// NOTE: even though iOS devices don't support filtering depth textures, we still report as
// supported here in order for the OES_depth_texture extension to be enabled.
// During draw call, the filter modes will be converted to nearest.
case angle::FormatID::D16_UNORM:
case angle::FormatID::D24_UNORM_S8_UINT:
case angle::FormatID::D32_FLOAT_S8X24_UINT:
case angle::FormatID::D32_FLOAT:
case angle::FormatID::D32_UNORM:
caps->texturable = caps->filterable = caps->textureAttachment = caps->renderbuffer =
true;
return true;
default:
// NOTE(hqle): Handle more cases
return false;
}
}
void GenerateTextureCapsMap(const FormatTable &formatTable,
const DisplayMtl *display,
gl::TextureCapsMap *capsMapOut,
std::vector<GLenum> *compressedFormatsOut,
uint32_t *maxSamplesOut)
{
auto &textureCapsMap = *capsMapOut;
auto &compressedFormats = *compressedFormatsOut;
compressedFormats.clear();
auto formatVerifier = [&](const gl::InternalFormat &internalFormatInfo) {
angle::FormatID angleFormatId =
angle::Format::InternalFormatToID(internalFormatInfo.sizedInternalFormat);
const Format &mtlFormat = formatTable.getPixelFormat(angleFormatId);
if (!mtlFormat.valid())
{
return;
}
const FormatCaps &formatCaps = mtlFormat.getCaps();
const angle::Format &intendedAngleFormat = mtlFormat.intendedAngleFormat();
gl::TextureCaps textureCaps;
// First let check whether we can override certain special cases.
if (!OverrideTextureCaps(display, mtlFormat.intendedFormatId, &textureCaps))
{
// Fill the texture caps using pixel format's caps
textureCaps.filterable = mtlFormat.getCaps().filterable;
textureCaps.renderbuffer =
mtlFormat.getCaps().colorRenderable || mtlFormat.getCaps().depthRenderable;
textureCaps.texturable = true;
textureCaps.textureAttachment = textureCaps.renderbuffer;
textureCaps.blendable = mtlFormat.getCaps().blendable;
}
if (formatCaps.multisample)
{
constexpr uint32_t sampleCounts[] = {2, 4, 8};
for (auto sampleCount : sampleCounts)
{
if ([display->getMetalDevice() supportsTextureSampleCount:sampleCount])
{
textureCaps.sampleCounts.insert(sampleCount);
*maxSamplesOut = std::max(*maxSamplesOut, sampleCount);
}
}
}
textureCapsMap.set(mtlFormat.intendedFormatId, textureCaps);
if (intendedAngleFormat.isBlock)
{
compressedFormats.push_back(intendedAngleFormat.glInternalFormat);
}
};
// Texture caps map.
const gl::FormatSet &internalFormats = gl::GetAllSizedInternalFormats();
for (const auto internalFormat : internalFormats)
{
const gl::InternalFormat &internalFormatInfo =
gl::GetSizedInternalFormatInfo(internalFormat);
formatVerifier(internalFormatInfo);
}
}
} // namespace
// FormatBase implementation
const angle::Format &FormatBase::actualAngleFormat() const
{
return angle::Format::Get(actualFormatId);
}
const angle::Format &FormatBase::intendedAngleFormat() const
{
return angle::Format::Get(intendedFormatId);
}
// Format implementation
const gl::InternalFormat &Format::intendedInternalFormat() const
{
return gl::GetSizedInternalFormatInfo(intendedAngleFormat().glInternalFormat);
}
const gl::InternalFormat &Format::actualInternalFormat() const
{
return gl::GetSizedInternalFormatInfo(actualAngleFormat().glInternalFormat);
}
bool Format::needConversion(angle::FormatID srcFormatId) const
{
if ((srcFormatId == angle::FormatID::BC1_RGB_UNORM_BLOCK &&
actualFormatId == angle::FormatID::BC1_RGBA_UNORM_BLOCK) ||
(srcFormatId == angle::FormatID::BC1_RGB_UNORM_SRGB_BLOCK &&
actualFormatId == angle::FormatID::BC1_RGBA_UNORM_SRGB_BLOCK))
{
// When texture swizzling is available, DXT1 RGB format will be swizzled with RGB1.
// WebGL allows unswizzled mapping when swizzling is not available. No need to convert.
return false;
}
return srcFormatId != actualFormatId;
}
bool Format::isPVRTC() const
{
switch (metalFormat)
{
#if (TARGET_OS_IOS && !TARGET_OS_MACCATALYST) || \
(TARGET_OS_OSX && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 101600))
case MTLPixelFormatPVRTC_RGB_2BPP:
case MTLPixelFormatPVRTC_RGB_2BPP_sRGB:
case MTLPixelFormatPVRTC_RGB_4BPP:
case MTLPixelFormatPVRTC_RGB_4BPP_sRGB:
case MTLPixelFormatPVRTC_RGBA_2BPP:
case MTLPixelFormatPVRTC_RGBA_2BPP_sRGB:
case MTLPixelFormatPVRTC_RGBA_4BPP:
case MTLPixelFormatPVRTC_RGBA_4BPP_sRGB:
return true;
#endif
default:
return false;
}
}
// FormatTable implementation
angle::Result FormatTable::initialize(const DisplayMtl *display)
{
mMaxSamples = 0;
// Initialize native format caps
initNativeFormatCaps(display);
for (size_t i = 0; i < angle::kNumANGLEFormats; ++i)
{
const auto formatId = static_cast<angle::FormatID>(i);
mPixelFormatTable[i].init(display, formatId);
mPixelFormatTable[i].caps = &mNativePixelFormatCapsTable[mPixelFormatTable[i].metalFormat];
if (mPixelFormatTable[i].actualFormatId != mPixelFormatTable[i].intendedFormatId)
{
mPixelFormatTable[i].textureLoadFunctions = angle::GetLoadFunctionsMap(
mPixelFormatTable[i].intendedAngleFormat().glInternalFormat,
mPixelFormatTable[i].actualFormatId);
}
mVertexFormatTables[0][i].init(formatId, false);
mVertexFormatTables[1][i].init(formatId, true);
}
return angle::Result::Continue;
}
void FormatTable::generateTextureCaps(const DisplayMtl *display,
gl::TextureCapsMap *capsMapOut,
std::vector<GLenum> *compressedFormatsOut)
{
GenerateTextureCapsMap(*this, display, capsMapOut, compressedFormatsOut, &mMaxSamples);
}
const Format &FormatTable::getPixelFormat(angle::FormatID angleFormatId) const
{
return mPixelFormatTable[static_cast<size_t>(angleFormatId)];
}
const FormatCaps &FormatTable::getNativeFormatCaps(MTLPixelFormat mtlFormat) const
{
ASSERT(mNativePixelFormatCapsTable.count(mtlFormat));
return mNativePixelFormatCapsTable.at(mtlFormat);
}
const VertexFormat &FormatTable::getVertexFormat(angle::FormatID angleFormatId,
bool tightlyPacked) const
{
auto tableIdx = tightlyPacked ? 1 : 0;
return mVertexFormatTables[tableIdx][static_cast<size_t>(angleFormatId)];
}
void FormatTable::setFormatCaps(MTLPixelFormat formatId,
bool filterable,
bool writable,
bool blendable,
bool multisample,
bool resolve,
bool colorRenderable)
{
setFormatCaps(formatId, filterable, writable, blendable, multisample, resolve, colorRenderable,
false, 0);
}
void FormatTable::setFormatCaps(MTLPixelFormat formatId,
bool filterable,
bool writable,
bool blendable,
bool multisample,
bool resolve,
bool colorRenderable,
NSUInteger pixelBytes,
NSUInteger channels)
{
setFormatCaps(formatId, filterable, writable, blendable, multisample, resolve, colorRenderable,
false, pixelBytes, channels);
}
void FormatTable::setFormatCaps(MTLPixelFormat formatId,
bool filterable,
bool writable,
bool blendable,
bool multisample,
bool resolve,
bool colorRenderable,
bool depthRenderable)
{
setFormatCaps(formatId, filterable, writable, blendable, multisample, resolve, colorRenderable,
depthRenderable, 0, 0);
}
void FormatTable::setFormatCaps(MTLPixelFormat id,
bool filterable,
bool writable,
bool blendable,
bool multisample,
bool resolve,
bool colorRenderable,
bool depthRenderable,
NSUInteger pixelBytes,
NSUInteger channels)
{
mNativePixelFormatCapsTable[id].filterable = filterable;
mNativePixelFormatCapsTable[id].writable = writable;
mNativePixelFormatCapsTable[id].colorRenderable = colorRenderable;
mNativePixelFormatCapsTable[id].depthRenderable = depthRenderable;
mNativePixelFormatCapsTable[id].blendable = blendable;
mNativePixelFormatCapsTable[id].multisample = multisample;
mNativePixelFormatCapsTable[id].resolve = resolve;
mNativePixelFormatCapsTable[id].pixelBytes = pixelBytes;
mNativePixelFormatCapsTable[id].pixelBytesMSAA = pixelBytes;
mNativePixelFormatCapsTable[id].channels = channels;
if (channels != 0)
mNativePixelFormatCapsTable[id].alignment = MAX(pixelBytes / channels, 1U);
}
void FormatTable::setCompressedFormatCaps(MTLPixelFormat formatId, bool filterable)
{
setFormatCaps(formatId, filterable, false, false, false, false, false, false);
}
void FormatTable::adjustFormatCapsForDevice(id<MTLDevice> device,
MTLPixelFormat id,
bool supportsiOS2,
bool supportsiOS4)
{
#if !(TARGET_OS_OSX || TARGET_OS_MACCATALYST)
NSUInteger pixelBytesRender = mNativePixelFormatCapsTable[id].pixelBytes;
NSUInteger pixelBytesRenderMSAA = mNativePixelFormatCapsTable[id].pixelBytesMSAA;
NSUInteger alignment = mNativePixelFormatCapsTable[id].alignment;
// Override the current pixelBytesRender
# define SPECIFIC(_pixelFormat, _pixelBytesRender) \
case _pixelFormat: \
pixelBytesRender = _pixelBytesRender; \
pixelBytesRenderMSAA = _pixelBytesRender; \
alignment = \
supportsiOS4 ? _pixelBytesRender / mNativePixelFormatCapsTable[id].channels : 4; \
break
// Override the current pixel bytes render, and MSAA
# define SPECIFIC_MSAA(_pixelFormat, _pixelBytesRender, _pixelBytesRenderMSAA) \
case _pixelFormat: \
pixelBytesRender = _pixelBytesRender; \
pixelBytesRenderMSAA = _pixelBytesRenderMSAA; \
alignment = \
supportsiOS4 ? _pixelBytesRender / mNativePixelFormatCapsTable[id].channels : 4; \
break
// Override the current pixelBytesRender, and alignment
# define SPECIFIC_ALIGN(_pixelFormat, _pixelBytesRender, _alignment) \
case _pixelFormat: \
pixelBytesRender = _pixelBytesRender; \
pixelBytesRenderMSAA = _pixelBytesRender; \
alignment = _alignment; \
break
if (!mNativePixelFormatCapsTable[id].compressed)
{
// On AppleGPUFamily4+, there is no 4byte minimum requirement for render targets
uint32_t minSize = supportsiOS4 ? 1U : 4U;
pixelBytesRender = MAX(mNativePixelFormatCapsTable[id].pixelBytes, minSize);
pixelBytesRenderMSAA = pixelBytesRender;
alignment =
supportsiOS4 ? MAX(pixelBytesRender / mNativePixelFormatCapsTable[id].channels, 1U) : 4;
}
// This list of tables starts from a general multi-platform table,
// to specific platforms (i.e. ios2, ios4) inheriting from the previous tables
// Start off with the general case
switch ((NSUInteger)id)
{
SPECIFIC(MTLPixelFormatB5G6R5Unorm, 4U);
SPECIFIC(MTLPixelFormatA1BGR5Unorm, 4U);
SPECIFIC(MTLPixelFormatABGR4Unorm, 4U);
SPECIFIC(MTLPixelFormatBGR5A1Unorm, 4U);
SPECIFIC(MTLPixelFormatRGBA8Unorm, 4U);
SPECIFIC(MTLPixelFormatBGRA8Unorm, 4U);
SPECIFIC_MSAA(MTLPixelFormatRGBA8Unorm_sRGB, 4U, 8U);
SPECIFIC_MSAA(MTLPixelFormatBGRA8Unorm_sRGB, 4U, 8U);
SPECIFIC_MSAA(MTLPixelFormatRGBA8Snorm, 4U, 8U);
SPECIFIC_MSAA(MTLPixelFormatRGB10A2Uint, 4U, 8U);
SPECIFIC(MTLPixelFormatRGB10A2Unorm, 8U);
SPECIFIC(MTLPixelFormatBGR10A2Unorm, 8U);
SPECIFIC(MTLPixelFormatRG11B10Float, 8U);
SPECIFIC(MTLPixelFormatRGB9E5Float, 8U);
SPECIFIC(MTLPixelFormatStencil8, 1U);
}
// Override based ios2
if (supportsiOS2)
{
switch ((NSUInteger)id)
{
SPECIFIC(MTLPixelFormatB5G6R5Unorm, 8U);
SPECIFIC(MTLPixelFormatA1BGR5Unorm, 8U);
SPECIFIC(MTLPixelFormatABGR4Unorm, 8U);
SPECIFIC(MTLPixelFormatBGR5A1Unorm, 8U);
SPECIFIC_MSAA(MTLPixelFormatRGBA8Unorm, 4U, 8U);
SPECIFIC_MSAA(MTLPixelFormatBGRA8Unorm, 4U, 8U);
}
}
// Override based on ios4
if (supportsiOS4)
{
switch ((NSUInteger)id)
{
SPECIFIC_ALIGN(MTLPixelFormatB5G6R5Unorm, 6U, 2U);
SPECIFIC(MTLPixelFormatRGBA8Unorm, 4U);
SPECIFIC(MTLPixelFormatBGRA8Unorm, 4U);
SPECIFIC(MTLPixelFormatRGBA8Unorm_sRGB, 4U);
SPECIFIC(MTLPixelFormatBGRA8Unorm_sRGB, 4U);
SPECIFIC(MTLPixelFormatRGBA8Snorm, 4U);
SPECIFIC_ALIGN(MTLPixelFormatRGB10A2Unorm, 4U, 4U);
SPECIFIC_ALIGN(MTLPixelFormatBGR10A2Unorm, 4U, 4U);
SPECIFIC(MTLPixelFormatRGB10A2Uint, 8U);
SPECIFIC_ALIGN(MTLPixelFormatRG11B10Float, 4U, 4U);
SPECIFIC_ALIGN(MTLPixelFormatRGB9E5Float, 4U, 4U);
}
}
mNativePixelFormatCapsTable[id].pixelBytes = pixelBytesRender;
mNativePixelFormatCapsTable[id].pixelBytesMSAA = pixelBytesRenderMSAA;
mNativePixelFormatCapsTable[id].alignment = alignment;
# undef SPECIFIC
# undef SPECIFIC_ALIGN
# undef SPECIFIC_MSAA
#endif
// macOS does not need to perform any additoinal adjustment. These values are only used to check
// valid MRT sizes on iOS.
}
void FormatTable::initNativeFormatCaps(const DisplayMtl *display)
{
initNativeFormatCapsAutogen(display);
}
} // namespace mtl
} // namespace rx