Hash :
113c5e29
Author :
Date :
2020-05-11T22:49:03
Metal: deferred render command encoder creation. MTLRenderCommandEncoder creation will be deferred until a render pass ends. Commands will be stored into a back-end owned buffer during render pass. At the end of the render pass, those commands will be re-encoded into an actual MTLRenderCommandEncoder. Benefits: - Useful for future implementation of occlusion query buffer where it could be allocated right before the end of a render pass to be big enough to store all queries within the render pass. - It's possible to change load option (deferred clear) as long as there is no draw call issued yet. This is not implemented yet. - Possibility of commands' re-ordering. Bug: angleproject:2634 Change-Id: I1348716aa882c0540d9120bf175d8dac13fb58bd Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2193196 Commit-Queue: Jamie Madill <jmadill@chromium.org> Reviewed-by: Jonah Ryan-Davis <jonahr@google.com> Reviewed-by: Jamie Madill <jmadill@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
//
// 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/Version.h"
#include "libANGLE/angletypes.h"
#if TARGET_OS_IPHONE
# if !defined(ANGLE_IOS_DEPLOY_TARGET)
# define ANGLE_IOS_DEPLOY_TARGET __IPHONE_11_0
# endif
#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;
} // namespace egl
#define ANGLE_GL_OBJECTS_X(PROC) \
PROC(Buffer) \
PROC(Context) \
PROC(Framebuffer) \
PROC(MemoryObject) \
PROC(Query) \
PROC(Program) \
PROC(Semaphore) \
PROC(Texture) \
PROC(TransformFeedback) \
PROC(VertexArray)
#define ANGLE_PRE_DECLARE_OBJECT(OBJ) class OBJ;
namespace gl
{
struct Rectangle;
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 VertexArrayMtl;
class TextureMtl;
class ProgramMtl;
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 = 1;
constexpr size_t kDefaultAttributeSize = 4 * sizeof(float);
// Metal limits
constexpr uint32_t kMaxShaderBuffers = 31;
constexpr uint32_t kMaxShaderSamplers = 16;
constexpr size_t kDefaultUniformsMaxSize = 4 * 1024;
constexpr uint32_t kMaxViewports = 1;
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;
// Front end binding limits
constexpr uint32_t kMaxGLSamplerBindings = 2 * kMaxShaderSamplers;
// 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;
constexpr uint32_t kStencilMaskAll = 0xff; // Only 8 bits stencil is supported
constexpr float kEmulatedAlphaValue = 1.0f;
// NOTE(hqle): Support ES 3.0.
constexpr gl::Version kMaxSupportedGLVersion = gl::Version(2, 0);
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 <typename T>
using GetImplType = typename ImplTypeHelper<T>::ImplType;
template <typename T>
GetImplType<T> *GetImpl(const T *_Nonnull 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)
{
T retained = obj;
#if !__has_feature(objc_arc)
[retained retain];
#endif
release();
mMetalObject = obj;
}
private:
void release()
{
#if !__has_feature(objc_arc)
[mMetalObject release];
#endif
mMetalObject = nil;
}
T mMetalObject = nil;
};
// 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=(const 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==(const std::nullptr_t &theNull) const { return this->get(); }
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;
private:
void transfer(AutoObjCPtr &&src)
{
this->retainAssign(std::move(src.get()));
src.reset();
}
};
template <typename T>
using AutoObjCObj = AutoObjCPtr<T *>;
struct ClearOptions
{
Optional<MTLClearColor> clearColor;
Optional<float> clearDepth;
Optional<uint32_t> clearStencil;
};
class CommandQueue;
class ErrorHandler
{
public:
virtual ~ErrorHandler() {}
virtual void handleError(GLenum error,
const char *file,
const char *function,
unsigned int line) = 0;
virtual void handleError(NSError *_Nullable error,
const char *file,
const char *function,
unsigned int line) = 0;
};
class Context : public ErrorHandler
{
public:
Context(DisplayMtl *displayMtl);
_Nullable id<MTLDevice> getMetalDevice() const;
mtl::CommandQueue &cmdQueue();
DisplayMtl *getDisplay() const { return mDisplay; }
protected:
DisplayMtl *mDisplay;
};
#define ANGLE_MTL_CHECK(context, test, error) \
do \
{ \
if (ANGLE_UNLIKELY(!(test))) \
{ \
context->handleError(error, __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_ */