1 // 2 // Copyright 2019 The ANGLE Project Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 // 6 // DisplayMtl.h: 7 // Defines the class interface for DisplayMtl, implementing DisplayImpl. 8 // 9 10 #ifndef LIBANGLE_RENDERER_METAL_DISPLAYMTL_H_ 11 #define LIBANGLE_RENDERER_METAL_DISPLAYMTL_H_ 12 13 #include "common/PackedEnums.h" 14 #include "libANGLE/angletypes.h" 15 #include "libANGLE/renderer/DisplayImpl.h" 16 #include "libANGLE/renderer/ShareGroupImpl.h" 17 #include "libANGLE/renderer/metal/mtl_command_buffer.h" 18 #include "libANGLE/renderer/metal/mtl_context_device.h" 19 #include "libANGLE/renderer/metal/mtl_format_utils.h" 20 #include "libANGLE/renderer/metal/mtl_library_cache.h" 21 #include "libANGLE/renderer/metal/mtl_render_utils.h" 22 #include "libANGLE/renderer/metal/mtl_state_cache.h" 23 #include "libANGLE/renderer/metal/mtl_utils.h" 24 #include "platform/autogen/FeaturesMtl_autogen.h" 25 26 namespace egl 27 { 28 class Surface; 29 } 30 31 namespace rx 32 { 33 class ShareGroupMtl : public ShareGroupImpl 34 { 35 public: ShareGroupMtl(const egl::ShareGroupState & state)36 ShareGroupMtl(const egl::ShareGroupState &state) : ShareGroupImpl(state) {} 37 }; 38 39 class ContextMtl; 40 41 struct DefaultShaderAsyncInfoMtl; 42 43 class DisplayMtl : public DisplayImpl 44 { 45 public: 46 DisplayMtl(const egl::DisplayState &state); 47 ~DisplayMtl() override; 48 49 egl::Error initialize(egl::Display *display) override; 50 void terminate() override; 51 getDisplay()52 egl::Display *getDisplay() const { return mDisplay; } 53 54 bool testDeviceLost() override; 55 egl::Error restoreLostDevice(const egl::Display *display) override; 56 57 std::string getRendererDescription() override; 58 std::string getVendorString() override; 59 std::string getVersionString(bool includeFullVersion) override; 60 61 DeviceImpl *createDevice() override; 62 63 egl::Error waitClient(const gl::Context *context) override; 64 egl::Error waitNative(const gl::Context *context, EGLint engine) override; 65 66 egl::Error waitUntilWorkScheduled() override; 67 68 SurfaceImpl *createWindowSurface(const egl::SurfaceState &state, 69 EGLNativeWindowType window, 70 const egl::AttributeMap &attribs) override; 71 SurfaceImpl *createPbufferSurface(const egl::SurfaceState &state, 72 const egl::AttributeMap &attribs) override; 73 SurfaceImpl *createPbufferFromClientBuffer(const egl::SurfaceState &state, 74 EGLenum buftype, 75 EGLClientBuffer clientBuffer, 76 const egl::AttributeMap &attribs) override; 77 SurfaceImpl *createPixmapSurface(const egl::SurfaceState &state, 78 NativePixmapType nativePixmap, 79 const egl::AttributeMap &attribs) override; 80 81 ImageImpl *createImage(const egl::ImageState &state, 82 const gl::Context *context, 83 EGLenum target, 84 const egl::AttributeMap &attribs) override; 85 86 ContextImpl *createContext(const gl::State &state, 87 gl::ErrorSet *errorSet, 88 const egl::Config *configuration, 89 const gl::Context *shareContext, 90 const egl::AttributeMap &attribs) override; 91 92 StreamProducerImpl *createStreamProducerD3DTexture(egl::Stream::ConsumerType consumerType, 93 const egl::AttributeMap &attribs) override; 94 95 ShareGroupImpl *createShareGroup(const egl::ShareGroupState &state) override; 96 97 ExternalImageSiblingImpl *createExternalImageSibling(const gl::Context *context, 98 EGLenum target, 99 EGLClientBuffer buffer, 100 const egl::AttributeMap &attribs) override; 101 gl::Version getMaxSupportedESVersion() const override; 102 gl::Version getMaxConformantESVersion() const override; 103 Optional<gl::Version> getMaxSupportedDesktopVersion() const override; 104 105 EGLSyncImpl *createSync(const egl::AttributeMap &attribs) override; 106 107 egl::Error makeCurrent(egl::Display *display, 108 egl::Surface *drawSurface, 109 egl::Surface *readSurface, 110 gl::Context *context) override; 111 112 void populateFeatureList(angle::FeatureList *features) override; 113 114 bool isValidNativeWindow(EGLNativeWindowType window) const override; 115 116 egl::Error validateClientBuffer(const egl::Config *configuration, 117 EGLenum buftype, 118 EGLClientBuffer clientBuffer, 119 const egl::AttributeMap &attribs) const override; 120 121 egl::Error validateImageClientBuffer(const gl::Context *context, 122 EGLenum target, 123 EGLClientBuffer clientBuffer, 124 const egl::AttributeMap &attribs) const override; 125 126 egl::ConfigSet generateConfigs() override; 127 128 gl::Caps getNativeCaps() const; 129 const gl::TextureCapsMap &getNativeTextureCaps() const; 130 const gl::Extensions &getNativeExtensions() const; 131 const gl::Limitations &getNativeLimitations() const; 132 const ShPixelLocalStorageOptions &getNativePixelLocalStorageOptions() const; getFeatures()133 const angle::FeaturesMtl &getFeatures() const { return mFeatures; } 134 135 // Check whether either of the specified iOS or Mac GPU family is supported 136 bool supportsEitherGPUFamily(uint8_t iOSFamily, uint8_t macFamily) const; 137 bool supportsAppleGPUFamily(uint8_t iOSFamily) const; 138 bool supportsMacGPUFamily(uint8_t macFamily) const; 139 bool supportsMetal2_1() const; 140 bool supportsMetal2_2() const; 141 bool supportsDepth24Stencil8PixelFormat() const; 142 bool supports32BitFloatFiltering() const; 143 bool isAMD() const; 144 bool isAMDBronzeDriver() const; 145 bool isIntel() const; 146 bool isNVIDIA() const; 147 bool isSimulator() const; 148 getMetalDevice()149 id<MTLDevice> getMetalDevice() const { return mMetalDevice; } 150 cmdQueue()151 mtl::CommandQueue &cmdQueue() { return mCmdQueue; } getFormatTable()152 const mtl::FormatTable &getFormatTable() const { return mFormatTable; } getUtils()153 mtl::RenderUtils &getUtils() { return *mUtils; } getStateCache()154 mtl::StateCache &getStateCache() { return mStateCache; } getLibraryCache()155 mtl::LibraryCache &getLibraryCache() { return mLibraryCache; } getMaxColorTargetBits()156 uint32_t getMaxColorTargetBits() { return mMaxColorTargetBits; } hasFragmentMemoryBarriers()157 bool hasFragmentMemoryBarriers() const { return mHasFragmentMemoryBarriers; } 158 159 id<MTLLibrary> getDefaultShadersLib(); 160 getPixelFormat(angle::FormatID angleFormatId)161 const mtl::Format &getPixelFormat(angle::FormatID angleFormatId) const 162 { 163 return mFormatTable.getPixelFormat(angleFormatId); 164 } getNativeFormatCaps(MTLPixelFormat mtlFormat)165 const mtl::FormatCaps &getNativeFormatCaps(MTLPixelFormat mtlFormat) const 166 { 167 return mFormatTable.getNativeFormatCaps(mtlFormat); 168 } 169 170 // See mtl::FormatTable::getVertexFormat() getVertexFormat(angle::FormatID angleFormatId,bool tightlyPacked)171 const mtl::VertexFormat &getVertexFormat(angle::FormatID angleFormatId, 172 bool tightlyPacked) const 173 { 174 return mFormatTable.getVertexFormat(angleFormatId, tightlyPacked); 175 } 176 #if ANGLE_MTL_EVENT_AVAILABLE 177 mtl::AutoObjCObj<MTLSharedEventListener> getOrCreateSharedEventListener(); 178 #endif 179 180 protected: 181 void generateExtensions(egl::DisplayExtensions *outExtensions) const override; 182 void generateCaps(egl::Caps *outCaps) const override; 183 184 private: 185 angle::Result initializeImpl(egl::Display *display); 186 void ensureCapsInitialized() const; 187 void initializeCaps() const; 188 void initializeExtensions() const; 189 void initializeTextureCaps() const; 190 void initializeFeatures(); 191 void initializeLimitations(); 192 EGLenum EGLDrawingBufferTextureTarget(); 193 mtl::AutoObjCPtr<id<MTLDevice>> getMetalDeviceMatchingAttribute( 194 const egl::AttributeMap &attribs); 195 angle::Result initializeShaderLibrary(); 196 197 egl::Display *mDisplay; 198 199 mtl::AutoObjCPtr<id<MTLDevice>> mMetalDevice = nil; 200 uint32_t mMetalDeviceVendorId = 0; 201 202 // Expensive-to-compute AMD Bronze driver detection 203 mutable bool mComputedAMDBronze = false; 204 mutable bool mIsAMDBronze = false; 205 206 mtl::CommandQueue mCmdQueue; 207 208 mutable mtl::FormatTable mFormatTable; 209 mtl::StateCache mStateCache; 210 mtl::LibraryCache mLibraryCache; 211 std::unique_ptr<mtl::RenderUtils> mUtils; 212 213 // Built-in Shaders 214 mtl::AutoObjCPtr<id<MTLLibrary>> mDefaultShaders; 215 #if ANGLE_MTL_EVENT_AVAILABLE 216 mtl::AutoObjCObj<MTLSharedEventListener> mSharedEventListener; 217 #endif 218 219 mutable bool mCapsInitialized; 220 mutable gl::TextureCapsMap mNativeTextureCaps; 221 mutable gl::Extensions mNativeExtensions; 222 mutable gl::Caps mNativeCaps; 223 mutable gl::Limitations mNativeLimitations; 224 mutable ShPixelLocalStorageOptions mNativePLSOptions; 225 mutable uint32_t mMaxColorTargetBits = 0; 226 mutable bool mHasFragmentMemoryBarriers; 227 228 angle::FeaturesMtl mFeatures; 229 }; 230 231 } // namespace rx 232 233 #endif /* LIBANGLE_RENDERER_METAL_DISPLAYMTL_H_ */ 234