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() 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 initializeFrontendFeatures(angle::FrontendFeatures *features) const override; 113 114 void populateFeatureList(angle::FeatureList *features) override; 115 116 bool isValidNativeWindow(EGLNativeWindowType window) const override; 117 118 egl::Error validateClientBuffer(const egl::Config *configuration, 119 EGLenum buftype, 120 EGLClientBuffer clientBuffer, 121 const egl::AttributeMap &attribs) const override; 122 123 egl::Error validateImageClientBuffer(const gl::Context *context, 124 EGLenum target, 125 EGLClientBuffer clientBuffer, 126 const egl::AttributeMap &attribs) const override; 127 128 egl::ConfigSet generateConfigs() override; 129 130 gl::Caps getNativeCaps() const; 131 const gl::TextureCapsMap &getNativeTextureCaps() const; 132 const gl::Extensions &getNativeExtensions() const; 133 const gl::Limitations &getNativeLimitations() const; 134 const ShPixelLocalStorageOptions &getNativePixelLocalStorageOptions() const; getFeatures()135 const angle::FeaturesMtl &getFeatures() const { return mFeatures; } 136 137 // Check whether either of the specified iOS or Mac GPU family is supported 138 bool supportsEitherGPUFamily(uint8_t iOSFamily, uint8_t macFamily) const; 139 bool supportsAppleGPUFamily(uint8_t iOSFamily) const; 140 bool supportsMacGPUFamily(uint8_t macFamily) const; 141 bool supportsMetal2_1() const; 142 bool supportsMetal2_2() const; 143 bool supportsDepth24Stencil8PixelFormat() const; 144 bool supports32BitFloatFiltering() const; 145 bool isAMD() const; 146 bool isAMDBronzeDriver() const; 147 bool isAMDFireProDevice() const; 148 bool isIntel() const; 149 bool isNVIDIA() const; 150 bool isSimulator() const; 151 getMetalDevice()152 id<MTLDevice> getMetalDevice() const { return mMetalDevice; } 153 cmdQueue()154 mtl::CommandQueue &cmdQueue() { return mCmdQueue; } getFormatTable()155 const mtl::FormatTable &getFormatTable() const { return mFormatTable; } getUtils()156 mtl::RenderUtils &getUtils() { return *mUtils; } getStateCache()157 mtl::StateCache &getStateCache() { return mStateCache; } getLibraryCache()158 mtl::LibraryCache &getLibraryCache() { return mLibraryCache; } getMaxColorTargetBits()159 uint32_t getMaxColorTargetBits() { return mMaxColorTargetBits; } hasFragmentMemoryBarriers()160 bool hasFragmentMemoryBarriers() const { return mHasFragmentMemoryBarriers; } 161 162 id<MTLLibrary> getDefaultShadersLib(); 163 getPixelFormat(angle::FormatID angleFormatId)164 const mtl::Format &getPixelFormat(angle::FormatID angleFormatId) const 165 { 166 return mFormatTable.getPixelFormat(angleFormatId); 167 } getNativeFormatCaps(MTLPixelFormat mtlFormat)168 const mtl::FormatCaps &getNativeFormatCaps(MTLPixelFormat mtlFormat) const 169 { 170 return mFormatTable.getNativeFormatCaps(mtlFormat); 171 } 172 173 // See mtl::FormatTable::getVertexFormat() getVertexFormat(angle::FormatID angleFormatId,bool tightlyPacked)174 const mtl::VertexFormat &getVertexFormat(angle::FormatID angleFormatId, 175 bool tightlyPacked) const 176 { 177 return mFormatTable.getVertexFormat(angleFormatId, tightlyPacked); 178 } 179 #if ANGLE_MTL_EVENT_AVAILABLE 180 mtl::AutoObjCObj<MTLSharedEventListener> getOrCreateSharedEventListener(); 181 #endif 182 183 protected: 184 void generateExtensions(egl::DisplayExtensions *outExtensions) const override; 185 void generateCaps(egl::Caps *outCaps) const override; 186 187 private: 188 angle::Result initializeImpl(egl::Display *display); 189 void ensureCapsInitialized() const; 190 void initializeCaps() const; 191 void initializeExtensions() const; 192 void initializeTextureCaps() const; 193 void initializeFeatures(); 194 void initializeLimitations(); 195 EGLenum EGLDrawingBufferTextureTarget(); 196 mtl::AutoObjCPtr<id<MTLDevice>> getMetalDeviceMatchingAttribute( 197 const egl::AttributeMap &attribs); 198 angle::Result initializeShaderLibrary(); 199 200 egl::Display *mDisplay; 201 202 mtl::AutoObjCPtr<id<MTLDevice>> mMetalDevice = nil; 203 uint32_t mMetalDeviceVendorId = 0; 204 205 // Expensive-to-compute AMD Bronze driver detection 206 mutable bool mComputedAMDBronze = false; 207 mutable bool mIsAMDBronze = false; 208 209 mtl::CommandQueue mCmdQueue; 210 211 mutable mtl::FormatTable mFormatTable; 212 mtl::StateCache mStateCache; 213 mtl::LibraryCache mLibraryCache; 214 std::unique_ptr<mtl::RenderUtils> mUtils; 215 216 // Built-in Shaders 217 mtl::AutoObjCPtr<id<MTLLibrary>> mDefaultShaders; 218 #if ANGLE_MTL_EVENT_AVAILABLE 219 mtl::AutoObjCObj<MTLSharedEventListener> mSharedEventListener; 220 #endif 221 222 mutable bool mCapsInitialized; 223 mutable gl::TextureCapsMap mNativeTextureCaps; 224 mutable gl::Extensions mNativeExtensions; 225 mutable gl::Caps mNativeCaps; 226 mutable gl::Limitations mNativeLimitations; 227 mutable ShPixelLocalStorageOptions mNativePLSOptions; 228 mutable uint32_t mMaxColorTargetBits = 0; 229 mutable bool mHasFragmentMemoryBarriers; 230 231 angle::FeaturesMtl mFeatures; 232 }; 233 234 } // namespace rx 235 236 #endif /* LIBANGLE_RENDERER_METAL_DISPLAYMTL_H_ */ 237