Hash :
6ddbfa39
Author :
Date :
2022-05-09T09:06:16
Metal: Log the shader source when a shader fails to translate Refactor Metal logging to include a message string. Bug: chromium:1322521 Change-Id: I3a7b5c36fcf140b3664ad96a9da924819326bf94 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3634725 Reviewed-by: Quyen Le <lehoangquyen@chromium.org> Commit-Queue: Kenneth Russell <kbr@chromium.org> Reviewed-by: Kenneth Russell <kbr@chromium.org>
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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
//
// 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_common.h:
// Declares common constants, template classes, and mtl::Context - the MTLDevice container &
// error handler base class.
//
#ifndef LIBANGLE_RENDERER_METAL_MTL_COMMON_H_
#define LIBANGLE_RENDERER_METAL_MTL_COMMON_H_
#import <Metal/Metal.h>
#include <TargetConditionals.h>
#include <string>
#include "common/Optional.h"
#include "common/PackedEnums.h"
#include "common/angleutils.h"
#include "common/apple_platform_utils.h"
#include "libANGLE/Constants.h"
#include "libANGLE/ImageIndex.h"
#include "libANGLE/Version.h"
#include "libANGLE/angletypes.h"
#if TARGET_OS_IPHONE
# if !defined(__IPHONE_11_0)
# define __IPHONE_11_0 110000
# endif
# if !defined(ANGLE_IOS_DEPLOY_TARGET)
# define ANGLE_IOS_DEPLOY_TARGET __IPHONE_11_0
# endif
# if !defined(__IPHONE_OS_VERSION_MAX_ALLOWED)
# define __IPHONE_OS_VERSION_MAX_ALLOWED __IPHONE_11_0
# endif
# if !defined(__TV_OS_VERSION_MAX_ALLOWED)
# define __TV_OS_VERSION_MAX_ALLOWED __IPHONE_11_0
# endif
#endif
#if !defined(TARGET_OS_MACCATALYST)
# define TARGET_OS_MACCATALYST 0
#endif
#if defined(__ARM_ARCH)
# define ANGLE_MTL_ARM (__ARM_ARCH != 0)
#else
# define ANGLE_MTL_ARM 0
#endif
#define ANGLE_MTL_OBJC_SCOPE @autoreleasepool
#if !__has_feature(objc_arc)
# define ANGLE_MTL_AUTORELEASE autorelease
# define ANGLE_MTL_RETAIN retain
# define ANGLE_MTL_RELEASE release
#else
# define ANGLE_MTL_AUTORELEASE self
# define ANGLE_MTL_RETAIN self
# define ANGLE_MTL_RELEASE self
#endif
#define ANGLE_MTL_UNUSED __attribute__((unused))
#if defined(ANGLE_MTL_ENABLE_TRACE)
# define ANGLE_MTL_LOG(...) NSLog(@__VA_ARGS__)
#else
# define ANGLE_MTL_LOG(...) (void)0
#endif
namespace egl
{
class Display;
class Image;
class Surface;
} // namespace egl
#define ANGLE_GL_OBJECTS_X(PROC) \
PROC(Buffer) \
PROC(Context) \
PROC(Framebuffer) \
PROC(MemoryObject) \
PROC(Query) \
PROC(Program) \
PROC(Sampler) \
PROC(Semaphore) \
PROC(Texture) \
PROC(TransformFeedback) \
PROC(VertexArray)
#define ANGLE_PRE_DECLARE_OBJECT(OBJ) class OBJ;
namespace gl
{
ANGLE_GL_OBJECTS_X(ANGLE_PRE_DECLARE_OBJECT)
} // namespace gl
#define ANGLE_PRE_DECLARE_MTL_OBJECT(OBJ) class OBJ##Mtl;
namespace rx
{
class DisplayMtl;
class ContextMtl;
class FramebufferMtl;
class BufferMtl;
class ImageMtl;
class VertexArrayMtl;
class TextureMtl;
class ProgramMtl;
class SamplerMtl;
class TransformFeedbackMtl;
ANGLE_GL_OBJECTS_X(ANGLE_PRE_DECLARE_MTL_OBJECT)
namespace mtl
{
// NOTE(hqle): support variable max number of vertex attributes
constexpr uint32_t kMaxVertexAttribs = gl::MAX_VERTEX_ATTRIBS;
// NOTE(hqle): support variable max number of render targets
constexpr uint32_t kMaxRenderTargets = 4;
constexpr uint32_t kMaxShaderUBOs = 12;
constexpr uint32_t kMaxUBOSize = 16384;
constexpr uint32_t kMaxShaderXFBs = gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS;
// The max size of a buffer that will be allocated in shared memory.
// NOTE(hqle): This is just a hint. There is no official document on what is the max allowed size
// for shared memory.
constexpr size_t kSharedMemBufferMaxBufSizeHint = 128 * 1024;
constexpr size_t kDefaultAttributeSize = 4 * sizeof(float);
// Metal limits
constexpr uint32_t kMaxShaderBuffers = 31;
constexpr uint32_t kMaxShaderSamplers = 16;
constexpr size_t kInlineConstDataMaxSize = 4 * 1024;
constexpr size_t kDefaultUniformsMaxSize = 4 * 1024;
constexpr uint32_t kMaxViewports = 1;
// Restrict in-flight resource usage to 400 MB.
// A render pass can use more than 400MB, but the command buffer
// will be flushed next time
constexpr const size_t kMaximumResidentMemorySizeInBytes = 400 * 1024 * 1024;
constexpr uint32_t kVertexAttribBufferStrideAlignment = 4;
// Alignment requirement for offset passed to setVertex|FragmentBuffer
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
constexpr uint32_t kUniformBufferSettingOffsetMinAlignment = 256;
#else
constexpr uint32_t kUniformBufferSettingOffsetMinAlignment = 4;
#endif
constexpr uint32_t kIndexBufferOffsetAlignment = 4;
constexpr uint32_t kArgumentBufferOffsetAlignment = kUniformBufferSettingOffsetMinAlignment;
constexpr uint32_t kTextureToBufferBlittingAlignment = 256;
// Front end binding limits
constexpr uint32_t kMaxGLSamplerBindings = 2 * kMaxShaderSamplers;
constexpr uint32_t kMaxGLUBOBindings = 2 * kMaxShaderUBOs;
// Binding index start for vertex data buffers:
constexpr uint32_t kVboBindingIndexStart = 0;
// Binding index for default attribute buffer:
constexpr uint32_t kDefaultAttribsBindingIndex = kVboBindingIndexStart + kMaxVertexAttribs;
// Binding index for driver uniforms:
constexpr uint32_t kDriverUniformsBindingIndex = kDefaultAttribsBindingIndex + 1;
// Binding index for default uniforms:
constexpr uint32_t kDefaultUniformsBindingIndex = kDefaultAttribsBindingIndex + 3;
// Binding index for Transform Feedback Buffers (4)
constexpr uint32_t kTransformFeedbackBindingIndex = kDefaultUniformsBindingIndex + 1;
// Binding index for shadow samplers' compare modes
constexpr uint32_t kShadowSamplerCompareModesBindingIndex = kTransformFeedbackBindingIndex + 4;
// Binding index for UBO's argument buffer
constexpr uint32_t kUBOArgumentBufferBindingIndex = kShadowSamplerCompareModesBindingIndex + 1;
constexpr uint32_t kStencilMaskAll = 0xff; // Only 8 bits stencil is supported
static const char *kUnassignedAttributeString = " __unassigned_attribute__";
// This special constant is used to indicate that a particular vertex descriptor's buffer layout
// index is unused.
constexpr MTLVertexStepFunction kVertexStepFunctionInvalid =
static_cast<MTLVertexStepFunction>(0xff);
constexpr int kEmulatedAlphaValue = 1;
constexpr size_t kOcclusionQueryResultSize = sizeof(uint64_t);
constexpr gl::Version kMaxSupportedGLVersion = gl::Version(3, 0);
// Work-around the enum is not available on macOS
#if (TARGET_OS_OSX && (__MAC_OS_X_VERSION_MAX_ALLOWED < 110000)) || TARGET_OS_MACCATALYST
constexpr MTLBlitOption kBlitOptionRowLinearPVRTC = MTLBlitOptionNone;
#else
constexpr MTLBlitOption kBlitOptionRowLinearPVRTC = MTLBlitOptionRowLinearPVRTC;
#endif
#if defined(__IPHONE_13_0) || defined(__MAC_10_15)
# define ANGLE_MTL_SWIZZLE_AVAILABLE 1
using TextureSwizzleChannels = MTLTextureSwizzleChannels;
using RenderStages = MTLRenderStages;
constexpr MTLRenderStages kRenderStageVertex = MTLRenderStageVertex;
constexpr MTLRenderStages kRenderStageFragment = MTLRenderStageFragment;
#else
# define ANGLE_MTL_SWIZZLE_AVAILABLE 0
using TextureSwizzleChannels = int;
using RenderStages = int;
constexpr RenderStages kRenderStageVertex = 1;
constexpr RenderStages kRenderStageFragment = 2;
#endif
enum class PixelType
{
Int,
UInt,
Float,
EnumCount,
};
template <typename T>
struct ImplTypeHelper;
// clang-format off
#define ANGLE_IMPL_TYPE_HELPER_GL(OBJ) \
template<> \
struct ImplTypeHelper<gl::OBJ> \
{ \
using ImplType = OBJ##Mtl; \
};
// clang-format on
ANGLE_GL_OBJECTS_X(ANGLE_IMPL_TYPE_HELPER_GL)
template <>
struct ImplTypeHelper<egl::Display>
{
using ImplType = DisplayMtl;
};
template <>
struct ImplTypeHelper<egl::Image>
{
using ImplType = ImageMtl;
};
template <typename T>
using GetImplType = typename ImplTypeHelper<T>::ImplType;
template <typename T>
GetImplType<T> *GetImpl(const T *glObject)
{
return GetImplAs<GetImplType<T>>(glObject);
}
// This class wraps Objective-C pointer inside, it will manage the lifetime of
// the Objective-C pointer. Changing pointer is not supported outside subclass.
template <typename T>
class WrappedObject
{
public:
WrappedObject() = default;
~WrappedObject() { release(); }
bool valid() const { return (mMetalObject != nil); }
T get() const { return mMetalObject; }
inline void reset() { release(); }
operator T() const { return get(); }
protected:
inline void set(T obj) { retainAssign(obj); }
void retainAssign(T obj)
{
#if !__has_feature(objc_arc)
T retained = obj;
[retained retain];
#endif
release();
mMetalObject = obj;
}
void unretainAssign(T obj)
{
release();
mMetalObject = obj;
}
private:
void release()
{
#if !__has_feature(objc_arc)
[mMetalObject release];
#endif
mMetalObject = nil;
}
T mMetalObject = nil;
};
// Because ARC enablement is a compile-time choice, and we compile this header
// both ways, we need a separate copy of our code when ARC is enabled.
#if __has_feature(objc_arc)
# define adoptObjCObj adoptObjCObjArc
#endif
template <typename T>
class AutoObjCPtr;
template <typename T>
using AutoObjCObj = AutoObjCPtr<T *>;
template <typename U>
AutoObjCObj<U> adoptObjCObj(U *NS_RELEASES_ARGUMENT) __attribute__((__warn_unused_result__));
// This class is similar to WrappedObject, however, it allows changing the
// internal pointer with public methods.
template <typename T>
class AutoObjCPtr : public WrappedObject<T>
{
public:
using ParentType = WrappedObject<T>;
AutoObjCPtr() {}
AutoObjCPtr(const std::nullptr_t &theNull) {}
AutoObjCPtr(const AutoObjCPtr &src) { this->retainAssign(src.get()); }
AutoObjCPtr(AutoObjCPtr &&src) { this->transfer(std::forward<AutoObjCPtr>(src)); }
// Take ownership of the pointer
AutoObjCPtr(T &&src)
{
this->retainAssign(src);
src = nil;
}
AutoObjCPtr &operator=(const AutoObjCPtr &src)
{
this->retainAssign(src.get());
return *this;
}
AutoObjCPtr &operator=(AutoObjCPtr &&src)
{
this->transfer(std::forward<AutoObjCPtr>(src));
return *this;
}
// Take ownership of the pointer
AutoObjCPtr &operator=(T &&src)
{
this->retainAssign(src);
src = nil;
return *this;
}
AutoObjCPtr &operator=(std::nullptr_t theNull)
{
this->set(nil);
return *this;
}
bool operator==(const AutoObjCPtr &rhs) const { return (*this) == rhs.get(); }
bool operator==(T rhs) const { return this->get() == rhs; }
bool operator==(std::nullptr_t theNull) const { return this->get() == nullptr; }
bool operator!=(std::nullptr_t) const { return this->get() != nullptr; }
inline operator bool() { return this->get(); }
bool operator!=(const AutoObjCPtr &rhs) const { return (*this) != rhs.get(); }
bool operator!=(T rhs) const { return this->get() != rhs; }
using ParentType::retainAssign;
template <typename U>
friend AutoObjCObj<U> adoptObjCObj(U *NS_RELEASES_ARGUMENT)
__attribute__((__warn_unused_result__));
private:
enum AdoptTag
{
Adopt
};
AutoObjCPtr(T src, AdoptTag) { this->unretainAssign(src); }
void transfer(AutoObjCPtr &&src)
{
this->retainAssign(std::move(src.get()));
src.reset();
}
};
template <typename U>
inline AutoObjCObj<U> adoptObjCObj(U *NS_RELEASES_ARGUMENT src)
{
#if __has_feature(objc_arc)
return src;
#elif defined(OBJC_NO_GC)
return AutoObjCPtr<U *>(src, AutoObjCPtr<U *>::Adopt);
#else
# error "ObjC GC not supported."
#endif
}
// NOTE: SharedEvent is only declared on iOS 12.0+ or mac 10.14+
#if defined(__IPHONE_12_0) || defined(__MAC_10_14)
# define ANGLE_MTL_EVENT_AVAILABLE 1
using SharedEventRef = AutoObjCPtr<id<MTLSharedEvent>>;
#else
# define ANGLE_MTL_EVENT_AVAILABLE 0
using SharedEventRef = AutoObjCObj<NSObject>;
#endif
// The native image index used by Metal back-end, the image index uses native mipmap level instead
// of "virtual" level modified by OpenGL's base level.
using MipmapNativeLevel = gl::LevelIndexWrapper<uint32_t>;
constexpr MipmapNativeLevel kZeroNativeMipLevel(0);
class ImageNativeIndexIterator;
class ImageNativeIndex final
{
public:
ImageNativeIndex() = delete;
ImageNativeIndex(const gl::ImageIndex &src, GLint baseLevel)
{
mNativeIndex = gl::ImageIndex::MakeFromType(src.getType(), src.getLevelIndex() - baseLevel,
src.getLayerIndex(), src.getLayerCount());
}
static ImageNativeIndex FromBaseZeroGLIndex(const gl::ImageIndex &src)
{
return ImageNativeIndex(src, 0);
}
MipmapNativeLevel getNativeLevel() const
{
return MipmapNativeLevel(mNativeIndex.getLevelIndex());
}
gl::TextureType getType() const { return mNativeIndex.getType(); }
GLint getLayerIndex() const { return mNativeIndex.getLayerIndex(); }
GLint getLayerCount() const { return mNativeIndex.getLayerCount(); }
GLint cubeMapFaceIndex() const { return mNativeIndex.cubeMapFaceIndex(); }
bool isLayered() const { return mNativeIndex.isLayered(); }
bool hasLayer() const { return mNativeIndex.hasLayer(); }
bool has3DLayer() const { return mNativeIndex.has3DLayer(); }
bool usesTex3D() const { return mNativeIndex.usesTex3D(); }
bool valid() const { return mNativeIndex.valid(); }
ImageNativeIndexIterator getLayerIterator(GLint layerCount) const;
private:
gl::ImageIndex mNativeIndex;
};
class ImageNativeIndexIterator final
{
public:
ImageNativeIndex next() { return ImageNativeIndex(mNativeIndexIte.next(), 0); }
ImageNativeIndex current() const { return ImageNativeIndex(mNativeIndexIte.current(), 0); }
bool hasNext() const { return mNativeIndexIte.hasNext(); }
private:
// This class is only constructable from ImageNativeIndex
friend class ImageNativeIndex;
explicit ImageNativeIndexIterator(const gl::ImageIndexIterator &baseZeroSrc)
: mNativeIndexIte(baseZeroSrc)
{}
gl::ImageIndexIterator mNativeIndexIte;
};
using ClearColorValueBytes = std::array<uint8_t, 4 * sizeof(float)>;
class ClearColorValue
{
public:
constexpr ClearColorValue()
: mType(PixelType::Float), mRedF(0), mGreenF(0), mBlueF(0), mAlphaF(0)
{}
constexpr ClearColorValue(float r, float g, float b, float a)
: mType(PixelType::Float), mRedF(r), mGreenF(g), mBlueF(b), mAlphaF(a)
{}
constexpr ClearColorValue(int32_t r, int32_t g, int32_t b, int32_t a)
: mType(PixelType::Int), mRedI(r), mGreenI(g), mBlueI(b), mAlphaI(a)
{}
constexpr ClearColorValue(uint32_t r, uint32_t g, uint32_t b, uint32_t a)
: mType(PixelType::UInt), mRedU(r), mGreenU(g), mBlueU(b), mAlphaU(a)
{}
constexpr ClearColorValue(const ClearColorValue &src)
: mType(src.mType), mValueBytes(src.mValueBytes)
{}
MTLClearColor toMTLClearColor() const;
PixelType getType() const { return mType; }
const ClearColorValueBytes &getValueBytes() const { return mValueBytes; }
ClearColorValue &operator=(const ClearColorValue &src);
void setAsFloat(float r, float g, float b, float a);
void setAsInt(int32_t r, int32_t g, int32_t b, int32_t a);
void setAsUInt(uint32_t r, uint32_t g, uint32_t b, uint32_t a);
private:
PixelType mType;
union
{
struct
{
float mRedF, mGreenF, mBlueF, mAlphaF;
};
struct
{
int32_t mRedI, mGreenI, mBlueI, mAlphaI;
};
struct
{
uint32_t mRedU, mGreenU, mBlueU, mAlphaU;
};
ClearColorValueBytes mValueBytes;
};
};
class CommandQueue;
class ErrorHandler
{
public:
virtual ~ErrorHandler() {}
virtual void handleError(GLenum error,
const char *message,
const char *file,
const char *function,
unsigned int line) = 0;
virtual void handleError(NSError *error,
const char *message,
const char *file,
const char *function,
unsigned int line) = 0;
};
class Context : public ErrorHandler
{
public:
Context(DisplayMtl *displayMtl);
mtl::CommandQueue &cmdQueue();
DisplayMtl *getDisplay() const { return mDisplay; }
protected:
DisplayMtl *mDisplay;
};
std::string FormatMetalErrorMessage(GLenum errorCode);
std::string FormatMetalErrorMessage(NSError *error);
#define ANGLE_MTL_HANDLE_ERROR(context, message, error) \
context->handleError(error, message, __FILE__, ANGLE_FUNCTION, __LINE__)
#define ANGLE_MTL_CHECK(context, test, error) \
do \
{ \
if (ANGLE_UNLIKELY(!(test))) \
{ \
context->handleError(error, mtl::FormatMetalErrorMessage(error).c_str(), __FILE__, \
ANGLE_FUNCTION, __LINE__); \
return angle::Result::Stop; \
} \
} while (0)
#define ANGLE_MTL_TRY(context, test) ANGLE_MTL_CHECK(context, test, GL_INVALID_OPERATION)
#define ANGLE_MTL_UNREACHABLE(context) \
UNREACHABLE(); \
ANGLE_MTL_TRY(context, false)
} // namespace mtl
} // namespace rx
#endif /* LIBANGLE_RENDERER_METAL_MTL_COMMON_H_ */