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 // mtl_utils.h: 7 // Declares utilities functions that create Metal shaders, convert from angle enums 8 // to Metal enums and so on. 9 // 10 11 #ifndef LIBANGLE_RENDERER_METAL_MTL_UTILS_H_ 12 #define LIBANGLE_RENDERER_METAL_MTL_UTILS_H_ 13 14 #import <Metal/Metal.h> 15 16 #include "angle_gl.h" 17 #include "common/PackedEnums.h" 18 #include "libANGLE/Context.h" 19 #include "libANGLE/Texture.h" 20 #include "libANGLE/renderer/metal/mtl_format_utils.h" 21 #include "libANGLE/renderer/metal/mtl_resources.h" 22 #include "libANGLE/renderer/metal/mtl_state_cache.h" 23 24 namespace rx 25 { 26 namespace mtl 27 { 28 29 NS_ASSUME_NONNULL_BEGIN 30 31 angle::Result InitializeTextureContents(const gl::Context *context, 32 const TextureRef &texture, 33 const Format &textureObjFormat, 34 const gl::ImageIndex &index); 35 36 MTLViewport GetViewport(const gl::Rectangle &rect, double znear = 0, double zfar = 1); 37 MTLViewport GetViewportFlipY(const gl::Rectangle &rect, 38 NSUInteger screenHeight, 39 double znear = 0, 40 double zfar = 1); 41 MTLViewport GetViewport(const gl::Rectangle &rect, 42 NSUInteger screenHeight, 43 bool flipY, 44 double znear = 0, 45 double zfar = 1); 46 MTLScissorRect GetScissorRect(const gl::Rectangle &rect, 47 NSUInteger screenHeight = 0, 48 bool flipY = false); 49 50 AutoObjCPtr<id<MTLLibrary>> CreateShaderLibrary(id<MTLDevice> metalDevice, 51 const std::string &source, 52 AutoObjCPtr<NSError *> *error); 53 54 AutoObjCPtr<id<MTLLibrary>> CreateShaderLibrary(id<MTLDevice> metalDevice, 55 const char *source, 56 size_t sourceLen, 57 AutoObjCPtr<NSError *> *error); 58 59 AutoObjCPtr<id<MTLLibrary>> CreateShaderLibraryFromBinary(id<MTLDevice> metalDevice, 60 const uint8_t *binarySource, 61 size_t binarySourceLen, 62 AutoObjCPtr<NSError *> *error); 63 64 // Need to define invalid enum value since Metal doesn't define it 65 constexpr MTLTextureType MTLTextureTypeInvalid = static_cast<MTLTextureType>(NSUIntegerMax); 66 static_assert(sizeof(MTLTextureType) == sizeof(NSUInteger), 67 "MTLTextureType is supposed to be based on NSUInteger"); 68 69 constexpr MTLPrimitiveType MTLPrimitiveTypeInvalid = static_cast<MTLPrimitiveType>(NSUIntegerMax); 70 static_assert(sizeof(MTLPrimitiveType) == sizeof(NSUInteger), 71 "MTLPrimitiveType is supposed to be based on NSUInteger"); 72 73 constexpr MTLIndexType MTLIndexTypeInvalid = static_cast<MTLIndexType>(NSUIntegerMax); 74 static_assert(sizeof(MTLIndexType) == sizeof(NSUInteger), 75 "MTLIndexType is supposed to be based on NSUInteger"); 76 77 MTLTextureType GetTextureType(gl::TextureType glType); 78 79 MTLSamplerMinMagFilter GetFilter(GLenum filter); 80 MTLSamplerMipFilter GetMipmapFilter(GLenum filter); 81 MTLSamplerAddressMode GetSamplerAddressMode(GLenum wrap); 82 83 MTLBlendFactor GetBlendFactor(GLenum factor); 84 MTLBlendOperation GetBlendOp(GLenum op); 85 86 MTLCompareFunction GetCompareFunc(GLenum func); 87 MTLStencilOperation GetStencilOp(GLenum op); 88 89 MTLWinding GetFontfaceWinding(GLenum frontFaceMode, bool invert); 90 91 PrimitiveTopologyClass GetPrimitiveTopologyClass(gl::PrimitiveMode mode); 92 MTLPrimitiveType GetPrimitiveType(gl::PrimitiveMode mode); 93 MTLIndexType GetIndexType(gl::DrawElementsType type); 94 95 // Useful to set clear color for texture originally having no alpha in GL, but backend's format 96 // has alpha channel. 97 MTLClearColor EmulatedAlphaClearColor(MTLClearColor color, MTLColorWriteMask colorMask); 98 99 NS_ASSUME_NONNULL_END 100 } // namespace mtl 101 } // namespace rx 102 103 #endif /* LIBANGLE_RENDERER_METAL_MTL_UTILS_H_ */ 104